Jpeg files and sdl_surface trouble

Hi everyone,

anyone here using IJG’s code together with SDL?
I have a strange problem:
For testing purposes I load a jpeg file into a SDL_Surface (SDL_Surface* srf
= IMG_Load_RW).
The file’s color depth is 24 bit.
After this step, I save the pixel data of the surface in another jpeg file
in the way it’s stated in example.c.
Unfortunately, this results mainly in garbage: the image has a tilt and is
gray (no, I didn’t want to make it a gray picture).
void saveJPG()


int height;

int width;

struct jpeg_compress_struct cinfo;

struct jpeg_error_mgr jerr;

FILE* outfile;

JSAMPROW row_pointer[1];

int row_stride;

SDL_Surface* srf = IMG_Load_RW("test.jpg", 1);

height = srf->h;

width = srf->w;

cinfo.err = jpeg_std_error(&jerr);


quality = 90;

if ((outfile = fopen(fileName, "wb")) == NULL) {

    fprintf(stderr, "can't open %s\n", fileName);



jpeg_stdio_dest(&cinfo, outfile);

cinfo.image_width = width;

cinfo.image_height = height;

cinfo.input_components = 3;

cinfo.in_color_space = JCS_RGB;


jpeg_set_quality(&cinfo, colorDepth, TRUE);

jpeg_start_compress(&cinfo, TRUE);

row_stride = width * 3;

while (cinfo.next_scanline < cinfo.image_height)


    row_pointer[0] = & ((Uint8*)srf->pixels)[cinfo.next_scanline *


    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);






What am I doing wrong? Please help!

Many thanks in advance!