# How do I multiply pixels on an SDL Surface?

Okay so I'm able to put blank pixels into a surface and also draw gradient pixels rectangles,etc

But I don't know how to multiply the pixels on a surface so I was hoping someone could provide me information on this topic.

I was thinking you could get the members pixel and then * it by 2 but that didn't provide results I wanted so I'm now thinking that you have to actually get to the position in bytes in one location to the left and one location to the right and then store it in memory and then * that by 2 am I correct or what?

If so what is it that allows me to do that and how do I do that?

For those who want an explanation I need a PNG Loaded on an SDL Surface to be bigger in size through code rather then image sequence.

for example I alter the pixels on the surface in such a way to make the pixels multiply increasing the size of the png and the SDL Surface.

• Do you want to multiply a color by another color or something else ? Please explain what you're trying to achieve. – Whiskas Oct 5 '12 at 18:29
• I want to make a PNG and SDL Surface bigger in size the effect I was looking for was a zoom effect. – NoobScratcher Oct 5 '12 at 18:37

I found this code which seems to do what you want :

http://fvirtman.free.fr/recueil/02_03_10_resize.c.html

With the nearest algorithm, we fill each pixel of the scaled image using the nearest pixel from the source image. This leads to many possible artifacts.

With the linear algorithm, the goal is to look for each pixel of the scaled image where we are in the source image (this can be between 4 source pixels for example), and do a linear interpolation of the colors of the pixels around the position (which is a real number) in the original image. It's all about smoothing.

You also can look here for more explanation and another scaling algorithm : http://www.brockmann-consult.de/beam/doc/help/general/ResamplingMethods.html

Here is an bilinear interpolation code commented :

float sourceWidth=192.0f;
float sourceHeight=128.0f;

float scaledWidth=sourceWidth*1.5f;
float scaledHeight=sourceheight*1.5f;

// First we need to find a ratio between the source image and the scaled image
// We already know it's 1.5

float ratioX=scaledWidth/sourceWidth;
float ratioY=scaledHeight/sourceHeight;

// Then we need to loop through each scaled image pixel

for(int i=0;i<scaledWidth;i++)
{
for(int j=0;j<scaledHeight;j++)
{
// Time to get the real number position on the source image

float sourceX=(float)i/ratioX;
float sourceY=(float)j/ratioY;

// Then we find the four pixels around our position

int minX=floor(sourceX);
int minY=floor(sourceY);
int maxX=minX+1;
int maxY=minY+1;

// We need to check if the bottom-right pixel is out of the source image

if(maxX>=sourceWidth)
maxX--;

if(maxY>=sourceHeight)
maxY--;

// Now we get the distance between the top-left source pixel and
// The real number position

float dX=sourceX-(float)minX;
float dY=sourceY-(float)minY;

// And for each color component (R,G,B) we apply the formula
// You can find it in the second link I posted

// I assume I have both GetPixelComp and SetPixelComp functions
// And both images represented as source and scaled

for(char k=0;k<3;k++)
{
unsigned char component=(unsigned char)
(GetPixelComp(source,minX,minY,k)*(1-dX)*(1-dY)+ // Top-Left
GetPixelComp(source,maxX,minY,k)*dX*(1-dY)+ // Top-Right
GetPixelComp(source,minX,maxY,k)*(1-dX)*dY+ // Bottom-Left
GetPixelComp(source,maxX,maxY,k)*dX*dY); // Bottom-Right

SetPixelComp(scaled,i,j,k,component);
}
}
}


I hope it helps you understand the principle :)

• So I need too look for each pixel in a non-scaled image and what is a linear interpolation of the pixels around the position? – NoobScratcher Oct 5 '12 at 21:16
• I edited my response to provide more informations. – Whiskas Oct 6 '12 at 6:48
• It helped alot but still don't know why I gotta do all that just too mulitply some pixels on a surface and make shore its center point ?? – NoobScratcher Oct 6 '12 at 20:42
• You can also take the closest pixel instead of interpolating between them, but the result will be highly pixelated, using this method allows you to smooth the image and have a better result. – Whiskas Oct 7 '12 at 7:15