Image scaling without interpolation


Two reason to scale grayscale image without interpolation:

  • intepolation add new color in image
  • .net GDI mechanism can’t return image in 8 bits – pixel format

Demo project

When a image is scaled with a scaling factor greater then 1, the image get black pixels,
to fill black pixels exist 3 well known algorithms

  • Nearest neighbor
  • Bilinear
  • Bicubic

all 3 algorithms add new color in image

Idea of scalig iamge without interpolation is:

  • to create an temprorary image with dimension computed with scaling factor
  • in temporary image will be a group of pixels for evry pixel from original image
  • to scale temporary image to target image that is small then target image; will get no black pixel when scaling with fator small than 1

With .net Bitmap object the SetPixel method does not work in image with indexed pixel format, shall to work with bitmap data.

//get original bmp data array 
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
	bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.                            
int bytes = bmpData.Stride * bmp.Height;
byte[] pixelValues = new byte[bytes];
// Copy the pixel values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, pixelValues, 0, bytes);
//Unlock the bits.             

The scaling factor for temporary image will be the smallest integer greater then target scaling factor

//compute integer scaling factor             
int integerScalingFactor = ((int)scalingFactor);
if (integerScalingFactor < scalingFactor)

Create temporary image matrix
Temporary image never be a image, only a byte matrix that hold values of pixels

//foreach pixel from original image will be put integerScalingFactor pixels in temporary image
int indexPixelValue = 0;
for (int indexYOriginal = 0, indexY = 0; indexYOriginal < yOriginal; indexYOriginal++,indexY += integerScalingFactor)
	int indexXOriginal = 0;
	for (int indexX = 0; indexXOriginal < xOriginal; indexXOriginal++, indexX += integerScalingFactor)
		for (int indexYFactor = 0; indexYFactor < integerScalingFactor; indexYFactor++)
			for (int indexXFactor = 0; indexXFactor < integerScalingFactor; indexXFactor++)
				integerScaledMatrix[(indexY + indexYFactor), (indexX + indexXFactor)] = pixelValues[indexPixelValue];
	indexPixelValue += bmpDataStride - indexXOriginal;

Compute scaling factor for temporary image to target image
Get scaling factor from division operation between target dimension and actual dimension

//scale integerScaledMatrix to target iamge dimension 
float scalingFactorFinalMatrix = ((float)scaledHeight) / ((float)heightScaledMatrix);

Fill target image pixel data scaling teporary image.
For evry pixel from temporary image get new index in target image using scaling factor

//pixelValuesScaled is target image
for (int y = 0; y < heightScaledMatrix; y++)
	for (int x = 0; x < widthScaledMatrix; x++)
		//with Convert.ToInt32 get the nearest index by floating index
		int yScalata = Convert.ToInt32(y * scalingFactorFinalMatrix);
		int xScalata = Convert.ToInt32(x * scalingFactorFinalMatrix);
		if (yScalata < bmpScaled.Height && xScalata < bmpScaled.Width)
			pixelValuesScaled[bmpDataScaled.Stride * yScalata + xScalata] = integerScaledMatrix[y, x];

At the end set target image data and the color palette

//Copy array data of scaled image in bmpData
System.Runtime.InteropServices.Marshal.Copy(pixelValuesScaled, 0, ptrScaled, bytesScaled);
// Unlock the bits.             
//set color palette of new image to old image color palette 
bmpScaled.Palette = bmp.Palette;

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.