Pixel Formats and Channel Flipping

Hi,

So I’m working on an image resampler. Right now, I’m trying to get nearest
neighbor resampling to work. The result is almost successful, except the
red and the blue channels of the final image are swapped.

Right now, I have no idea why. Can someone please explain why the channels
are being flipped?

char input[] = “image_000000.jpg”;
int new_width = 1024;
int new_height = 768;
char output[] = “resized.bmp”;

SDL_Surface* input_image = IMG_Load(input);
SDL_Surface* output_image = SDL_CreateRGBSurface(0, new_width, new_height,
24, 0, 0, 0, 0);

int sample_in_x, sample_in_y, sample_out_x, sample_out_y;
Uint32 sample;
char* pixel_position;
for (int x_pix=0;x_pix<new_width;x_pix++) {
for (int y_pix=0;y_pix<new_height;y_pix++) {
sample_in_x = int ( (float(x_pix)/float( new_width)) *
input_image->w );
sample_in_y = int ( (float(y_pix)/float(new_height)) *
input_image->h );
sample_out_x = int ( (float(x_pix)/float( new_width)) *
output_image->w );
sample_out_y = int ( (float(y_pix)/float(new_height)) *
output_image->h );

    pixel_position = ( char* ) input_image->pixels;
    pixel_position += input_image->pitch                 * sample_in_y;
    pixel_position += input_image->format->BytesPerPixel * sample_in_x;
    memcpy(&sample,pixel_position,input_image->format->BytesPerPixel);

    pixel_position = ( char* ) output_image->pixels;
    pixel_position += output_image->pitch                 *

sample_out_y;
pixel_position += output_image->format->BytesPerPixel *
sample_out_x;
memcpy(pixel_position,&sample,output_image->format->BytesPerPixel);
}
}

SDL_SaveBMP(output_image,output);

Thanks,
Ian

It doesn’t look like you’re considering the possible format differences.
You could try making the new surface use the same format with something
like:
SDL_Surface* output_image = SDL_CreateRGBSurface(0, new_width, new_height,
input_image->format->BitsPerPixel, input_image->format->Rmask,
input_image->format->Gmask, input_image->format->Bmask,
input_image->format->Amask);

Untested, so double-check me.

Jonny DOn Fri, Sep 3, 2010 at 8:12 PM, Ian Mallett wrote:

Hi,

So I’m working on an image resampler. Right now, I’m trying to get nearest
neighbor resampling to work. The result is almost successful, except the
red and the blue channels of the final image are swapped.

Right now, I have no idea why. Can someone please explain why the channels
are being flipped?

char input[] = “image_000000.jpg”;
int new_width = 1024;
int new_height = 768;
char output[] = “resized.bmp”;

SDL_Surface* input_image = IMG_Load(input);
SDL_Surface* output_image = SDL_CreateRGBSurface(0, new_width, new_height,
24, 0, 0, 0, 0);

int sample_in_x, sample_in_y, sample_out_x, sample_out_y;
Uint32 sample;
char* pixel_position;
for (int x_pix=0;x_pix<new_width;x_pix++) {
for (int y_pix=0;y_pix<new_height;y_pix++) {
sample_in_x = int ( (float(x_pix)/float( new_width)) *
input_image->w );
sample_in_y = int ( (float(y_pix)/float(new_height)) *
input_image->h );
sample_out_x = int ( (float(x_pix)/float( new_width)) *
output_image->w );
sample_out_y = int ( (float(y_pix)/float(new_height)) *
output_image->h );

    pixel_position = ( char* ) input_image->pixels;
    pixel_position += input_image->pitch                 * sample_in_y;
    pixel_position += input_image->format->BytesPerPixel * sample_in_x;
    memcpy(&sample,pixel_position,input_image->format->BytesPerPixel);

    pixel_position = ( char* ) output_image->pixels;
    pixel_position += output_image->pitch                 *

sample_out_y;
pixel_position += output_image->format->BytesPerPixel *
sample_out_x;
memcpy(pixel_position,&sample,output_image->format->BytesPerPixel);
}
}

SDL_SaveBMP(output_image,output);

Thanks,
Ian


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi,

Hmmm, that seems to fix it.

The default red, green, and blue masks are 0x0000FF00, 0x00FF0000, and
0xFF000000, respectively. Which is weird. Why is the default this way, and
not masks that would correspond to RGB?

Thanks,
Ian

The RGB masks correspond to the endian order of your computer, IIRC.On Sat, Sep 4, 2010 at 11:50 AM, Ian Mallett wrote:

Hi,

Hmmm, that seems to fix it.

The default red, green, and blue masks are 0x0000FF00, 0x00FF0000, and
0xFF000000, respectively. Which is weird. Why is the default this way, and
not masks that would correspond to RGB?

Thanks,
Ian


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org