/* Load a JPEG type image from an SDL datasource */ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src) { int start; struct jpeg_decompress_struct cinfo; JSAMPROW rowptr[1]; SDL_Surface *volatile surface = NULL; struct my_error_mgr jerr; if ( !src ) { /* The error message has been set in SDL_RWFromFile */ return NULL; } start = SDL_RWtell(src); if ( IMG_InitJPG() < 0 ) { return NULL; } /* Create a decompression structure and load the JPEG header */ cinfo.err = lib.jpeg_std_error(&jerr.errmgr); jerr.errmgr.error_exit = my_error_exit; jerr.errmgr.output_message = output_no_message; if(setjmp(jerr.escape)) { /* If we get here, libjpeg found an error */ lib.jpeg_destroy_decompress(&cinfo); if ( surface != NULL ) { SDL_FreeSurface(surface); } SDL_RWseek(src, start, SEEK_SET); IMG_QuitJPG(); IMG_SetError("JPEG loading error"); return NULL; } lib.jpeg_create_decompress(&cinfo); jpeg_SDL_RW_src(&cinfo, src); lib.jpeg_read_header(&cinfo, TRUE); if(cinfo.num_components == 4) { /* Set 32-bit Raw output */ cinfo.out_color_space = JCS_CMYK; cinfo.quantize_colors = FALSE; lib.jpeg_calc_output_dimensions(&cinfo); /* Allocate an output surface to hold the image */ surface = SDL_AllocSurface(SDL_SWSURFACE, cinfo.output_width, cinfo.output_height, 32, #if SDL_BYTEORDER == SDL_LIL_ENDIAN 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); #else 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF); #endif } else {
/* Load a JPEG type image from an SDL datasource */ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src) { struct jpeg_decompress_struct cinfo; JSAMPROW rowptr[1]; SDL_Surface *volatile surface = NULL; struct my_error_mgr jerr; /* Create a decompression structure and load the JPEG header */ cinfo.err = jpeg_std_error(&jerr.errmgr); jerr.errmgr.error_exit = my_error_exit; jerr.errmgr.output_message = output_no_message; if(setjmp(jerr.escape)) { /* If we get here, libjpeg found an error */ jpeg_destroy_decompress(&cinfo); IMG_SetError("JPEG loading error"); SDL_FreeSurface(surface); return NULL; } jpeg_create_decompress(&cinfo); jpeg_SDL_RW_src(&cinfo, src); jpeg_read_header(&cinfo, TRUE); /* Set 24-bit RGB output */ cinfo.out_color_space = JCS_RGB; cinfo.quantize_colors = FALSE; #ifdef FAST_JPEG cinfo.scale_num = 1; cinfo.scale_denom = 1; cinfo.dct_method = JDCT_FASTEST; cinfo.do_fancy_upsampling = FALSE; #endif jpeg_calc_output_dimensions(&cinfo); /* Allocate an output surface to hold the image */ surface = SDL_AllocSurface(SDL_SWSURFACE, cinfo.output_width, cinfo.output_height, 24, #if SDL_BYTEORDER == SDL_LIL_ENDIAN 0x0000FF, 0x00FF00, 0xFF0000, #else 0xFF0000, 0x00FF00, 0x0000FF, #endif 0); if ( surface == NULL ) { IMG_SetError("Out of memory"); goto done; } /* Decompress the image */ jpeg_start_decompress(&cinfo); while ( cinfo.output_scanline < cinfo.output_height ) { rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels + cinfo.output_scanline * surface->pitch; jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1); } jpeg_finish_decompress(&cinfo); /* Clean up and return */ done: jpeg_destroy_decompress(&cinfo); return(surface); }