Reducing size of image pixel data to display in window
I need help figuring out how to reduce the size of an image and display it in a window. Here's what I have so far:
Code:
char *buffer;
int x,y;
int image_width = 1920;
int image_height = 1080;
int preview_width = 304;
int preview_height = 176;
int num_channels = 3; //may be 4 if alpha channel is present
int pixeldata;
int pixelout[4]; //may hold 3 or 4 8-bit pixel values
float calc_width = ((float)img_width/(float)preview_width);
float calc_height = ((float)img_height/(float)preview_height);
float pixelcounter = 0;
for(y=0; y<preview_height; y++){
for(x=0; x<preview_width; x++){
memcpy(&pixeldata,(buffer+(int)pixelcounter),num_channels);
pixelout[0] = (pixeldata&0x000000FF); //red
pixelout[1] = (pixeldata&0x0000FF00)>>8; //green
pixelout[2] = (pixeldata&0x00FF0000)>>16; //blue
//assign bytes to NSBitmapImageRep object
[nsBitmapImageRepObj setPixel:pixelout atX:x y:y];
//skip bytes to reduce width
pixelcounter += (int)calc_width*3;
}
//skip bytes to reduce height
pixelcounter = ((y*(int)calc_height)*(img_width*num_channels));
}
The problem I have is that the preview image gets cut off because the width is divided by an integer and not a float. If I divide it by the float value of calc_width, there is no guarantee that the next read into the buffer will be on the three pixel RGB boundary. The only way to guarantee this is to divide the image by a factor of 3. This works, but then accuracy is compromised.
What I need to do is divide the image width using the float value, then "snap" the result to the nearest red pixel. Making the result a multiple of 3 doesn't work because it just converts calc_width back to an integer.
Sorry if this isn't the best explanation, but hopefully someone will be able to figure out what I'm trying to do!