2 SDL_image: An example image loading library for use with SDL
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
25 /* This is a JPEG image file loading framework */
31 #include "SDL_image.h"
37 /* Define this for fast loading and not as good image quality */
40 /* Define this for quicker (but less perfect) JPEG identification */
46 void (*jpeg_calc_output_dimensions) (j_decompress_ptr cinfo);
47 void (*jpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
48 void (*jpeg_destroy_decompress) (j_decompress_ptr cinfo);
49 boolean (*jpeg_finish_decompress) (j_decompress_ptr cinfo);
50 int (*jpeg_read_header) (j_decompress_ptr cinfo, boolean require_image);
51 JDIMENSION (*jpeg_read_scanlines) (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines);
52 boolean (*jpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
53 boolean (*jpeg_start_decompress) (j_decompress_ptr cinfo);
54 struct jpeg_error_mgr * (*jpeg_std_error) (struct jpeg_error_mgr * err);
57 #ifdef LOAD_JPG_DYNAMIC
60 if ( lib.loaded == 0 ) {
61 lib.handle = SDL_LoadObject(LOAD_JPG_DYNAMIC);
62 if ( lib.handle == NULL ) {
65 lib.jpeg_calc_output_dimensions =
66 (void (*) (j_decompress_ptr))
67 SDL_LoadFunction(lib.handle, "jpeg_calc_output_dimensions");
68 if ( lib.jpeg_calc_output_dimensions == NULL ) {
69 SDL_UnloadObject(lib.handle);
72 lib.jpeg_CreateDecompress =
73 (void (*) (j_decompress_ptr, int, size_t))
74 SDL_LoadFunction(lib.handle, "jpeg_CreateDecompress");
75 if ( lib.jpeg_CreateDecompress == NULL ) {
76 SDL_UnloadObject(lib.handle);
79 lib.jpeg_destroy_decompress =
80 (void (*) (j_decompress_ptr))
81 SDL_LoadFunction(lib.handle, "jpeg_destroy_decompress");
82 if ( lib.jpeg_destroy_decompress == NULL ) {
83 SDL_UnloadObject(lib.handle);
86 lib.jpeg_finish_decompress =
87 (boolean (*) (j_decompress_ptr))
88 SDL_LoadFunction(lib.handle, "jpeg_finish_decompress");
89 if ( lib.jpeg_finish_decompress == NULL ) {
90 SDL_UnloadObject(lib.handle);
93 lib.jpeg_read_header =
94 (int (*) (j_decompress_ptr, boolean))
95 SDL_LoadFunction(lib.handle, "jpeg_read_header");
96 if ( lib.jpeg_read_header == NULL ) {
97 SDL_UnloadObject(lib.handle);
100 lib.jpeg_read_scanlines =
101 (JDIMENSION (*) (j_decompress_ptr, JSAMPARRAY, JDIMENSION))
102 SDL_LoadFunction(lib.handle, "jpeg_read_scanlines");
103 if ( lib.jpeg_read_scanlines == NULL ) {
104 SDL_UnloadObject(lib.handle);
107 lib.jpeg_resync_to_restart =
108 (boolean (*) (j_decompress_ptr, int))
109 SDL_LoadFunction(lib.handle, "jpeg_resync_to_restart");
110 if ( lib.jpeg_resync_to_restart == NULL ) {
111 SDL_UnloadObject(lib.handle);
114 lib.jpeg_start_decompress =
115 (boolean (*) (j_decompress_ptr))
116 SDL_LoadFunction(lib.handle, "jpeg_start_decompress");
117 if ( lib.jpeg_start_decompress == NULL ) {
118 SDL_UnloadObject(lib.handle);
122 (struct jpeg_error_mgr * (*) (struct jpeg_error_mgr *))
123 SDL_LoadFunction(lib.handle, "jpeg_std_error");
124 if ( lib.jpeg_std_error == NULL ) {
125 SDL_UnloadObject(lib.handle);
135 if ( lib.loaded == 0 ) {
138 if ( lib.loaded == 1 ) {
139 SDL_UnloadObject(lib.handle);
146 if ( lib.loaded == 0 ) {
147 lib.jpeg_calc_output_dimensions = jpeg_calc_output_dimensions;
148 lib.jpeg_CreateDecompress = jpeg_CreateDecompress;
149 lib.jpeg_destroy_decompress = jpeg_destroy_decompress;
150 lib.jpeg_finish_decompress = jpeg_finish_decompress;
151 lib.jpeg_read_header = jpeg_read_header;
152 lib.jpeg_read_scanlines = jpeg_read_scanlines;
153 lib.jpeg_resync_to_restart = jpeg_resync_to_restart;
154 lib.jpeg_start_decompress = jpeg_start_decompress;
155 lib.jpeg_std_error = jpeg_std_error;
163 if ( lib.loaded == 0 ) {
166 if ( lib.loaded == 1 ) {
170 #endif /* LOAD_JPG_DYNAMIC */
172 /* See if an image is contained in a data source */
173 int IMG_isJPG(SDL_RWops *src)
180 /* This detection code is by Steaphan Greene <stea@cs.binghamton.edu> */
181 /* Blame me, not Sam, if this doesn't work right. */
182 /* And don't forget to report the problem to the the sdl list too! */
186 start = SDL_RWtell(src);
189 if ( SDL_RWread(src, magic, 2, 1) ) {
190 if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
192 while (is_JPG == 1) {
193 if(SDL_RWread(src, magic, 1, 2) != 2) {
195 } else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
197 } else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
198 /* Extra padding in JPEG (legal) */
199 /* or this is data and we are scanning */
200 SDL_RWseek(src, -1, RW_SEEK_CUR);
201 } else if(magic[1] == 0xD9) {
202 /* Got to end of good JPEG */
204 } else if( (in_scan == 1) && (magic[1] == 0x00) ) {
205 /* This is an encoded 0xFF within the data */
206 } else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
207 /* These have nothing else */
208 } else if(SDL_RWread(src, magic+2, 1, 2) != 2) {
211 /* Yes, it's big-endian */
215 start = SDL_RWtell(src);
216 size = (magic[2] << 8) + magic[3];
217 end = SDL_RWseek(src, size-2, RW_SEEK_CUR);
218 if ( end != start + size - 2 ) is_JPG = 0;
219 if ( magic[1] == 0xDA ) {
220 /* Now comes the actual JPEG meat */
222 /* Ok, I'm convinced. It is a JPEG. */
225 /* I'm not convinced. Prove it! */
233 SDL_RWseek(src, start, RW_SEEK_SET);
237 #define INPUT_BUFFER_SIZE 4096
239 struct jpeg_source_mgr pub;
242 Uint8 buffer[INPUT_BUFFER_SIZE];
246 * Initialize source --- called by jpeg_read_header
247 * before any data is actually read.
249 static void init_source (j_decompress_ptr cinfo)
251 /* We don't actually need to do anything */
256 * Fill the input buffer --- called whenever buffer is emptied.
258 static int fill_input_buffer (j_decompress_ptr cinfo)
260 my_source_mgr * src = (my_source_mgr *) cinfo->src;
263 nbytes = SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
265 /* Insert a fake EOI marker */
266 src->buffer[0] = (Uint8) 0xFF;
267 src->buffer[1] = (Uint8) JPEG_EOI;
270 src->pub.next_input_byte = src->buffer;
271 src->pub.bytes_in_buffer = nbytes;
278 * Skip data --- used to skip over a potentially large amount of
279 * uninteresting data (such as an APPn marker).
281 * Writers of suspendable-input applications must note that skip_input_data
282 * is not granted the right to give a suspension return. If the skip extends
283 * beyond the data currently in the buffer, the buffer can be marked empty so
284 * that the next read will cause a fill_input_buffer call that can suspend.
285 * Arranging for additional bytes to be discarded before reloading the input
286 * buffer is the application writer's problem.
288 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
290 my_source_mgr * src = (my_source_mgr *) cinfo->src;
292 /* Just a dumb implementation for now. Could use fseek() except
293 * it doesn't work on pipes. Not clear that being smart is worth
294 * any trouble anyway --- large skips are infrequent.
297 while (num_bytes > (long) src->pub.bytes_in_buffer) {
298 num_bytes -= (long) src->pub.bytes_in_buffer;
299 (void) src->pub.fill_input_buffer(cinfo);
300 /* note we assume that fill_input_buffer will never
301 * return FALSE, so suspension need not be handled.
304 src->pub.next_input_byte += (size_t) num_bytes;
305 src->pub.bytes_in_buffer -= (size_t) num_bytes;
310 * Terminate source --- called by jpeg_finish_decompress
311 * after all data has been read.
313 static void term_source (j_decompress_ptr cinfo)
315 /* We don't actually need to do anything */
320 * Prepare for input from a stdio stream.
321 * The caller must have already opened the stream, and is responsible
322 * for closing it after finishing decompression.
324 static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
328 /* The source object and input buffer are made permanent so that a series
329 * of JPEG images can be read from the same file by calling jpeg_stdio_src
330 * only before the first one. (If we discarded the buffer at the end of
331 * one image, we'd likely lose the start of the next one.)
332 * This makes it unsafe to use this manager and a different source
333 * manager serially with the same JPEG object. Caveat programmer.
335 if (cinfo->src == NULL) { /* first time for this JPEG object? */
336 cinfo->src = (struct jpeg_source_mgr *)
337 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
338 sizeof(my_source_mgr));
339 src = (my_source_mgr *) cinfo->src;
342 src = (my_source_mgr *) cinfo->src;
343 src->pub.init_source = init_source;
344 src->pub.fill_input_buffer = fill_input_buffer;
345 src->pub.skip_input_data = skip_input_data;
346 src->pub.resync_to_restart = lib.jpeg_resync_to_restart; /* use default method */
347 src->pub.term_source = term_source;
349 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
350 src->pub.next_input_byte = NULL; /* until buffer loaded */
353 struct my_error_mgr {
354 struct jpeg_error_mgr errmgr;
358 static void my_error_exit(j_common_ptr cinfo)
360 struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
361 longjmp(err->escape, 1);
364 static void output_no_message(j_common_ptr cinfo)
369 /* Load a JPEG type image from an SDL datasource */
370 SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
373 struct jpeg_decompress_struct cinfo;
375 SDL_Surface *volatile surface = NULL;
376 struct my_error_mgr jerr;
379 /* The error message has been set in SDL_RWFromFile */
382 start = SDL_RWtell(src);
384 if ( !IMG_Init(IMG_INIT_JPG) ) {
388 /* Create a decompression structure and load the JPEG header */
389 cinfo.err = lib.jpeg_std_error(&jerr.errmgr);
390 jerr.errmgr.error_exit = my_error_exit;
391 jerr.errmgr.output_message = output_no_message;
392 if(setjmp(jerr.escape)) {
393 /* If we get here, libjpeg found an error */
394 lib.jpeg_destroy_decompress(&cinfo);
395 if ( surface != NULL ) {
396 SDL_FreeSurface(surface);
398 SDL_RWseek(src, start, RW_SEEK_SET);
399 IMG_SetError("JPEG loading error");
403 lib.jpeg_create_decompress(&cinfo);
404 jpeg_SDL_RW_src(&cinfo, src);
405 lib.jpeg_read_header(&cinfo, TRUE);
407 if(cinfo.num_components == 4) {
408 /* Set 32-bit Raw output */
409 cinfo.out_color_space = JCS_CMYK;
410 cinfo.quantize_colors = FALSE;
411 lib.jpeg_calc_output_dimensions(&cinfo);
413 /* Allocate an output surface to hold the image */
414 surface = SDL_AllocSurface(SDL_SWSURFACE,
415 cinfo.output_width, cinfo.output_height, 32,
416 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
417 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
419 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
422 /* Set 24-bit RGB output */
423 cinfo.out_color_space = JCS_RGB;
424 cinfo.quantize_colors = FALSE;
427 cinfo.scale_denom = 1;
428 cinfo.dct_method = JDCT_FASTEST;
429 cinfo.do_fancy_upsampling = FALSE;
431 lib.jpeg_calc_output_dimensions(&cinfo);
433 /* Allocate an output surface to hold the image */
434 surface = SDL_AllocSurface(SDL_SWSURFACE,
435 cinfo.output_width, cinfo.output_height, 24,
436 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
437 0x0000FF, 0x00FF00, 0xFF0000,
439 0xFF0000, 0x00FF00, 0x0000FF,
444 if ( surface == NULL ) {
445 lib.jpeg_destroy_decompress(&cinfo);
446 SDL_RWseek(src, start, RW_SEEK_SET);
447 IMG_SetError("Out of memory");
451 /* Decompress the image */
452 lib.jpeg_start_decompress(&cinfo);
453 while ( cinfo.output_scanline < cinfo.output_height ) {
454 rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
455 cinfo.output_scanline * surface->pitch;
456 lib.jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
458 lib.jpeg_finish_decompress(&cinfo);
459 lib.jpeg_destroy_decompress(&cinfo);
468 IMG_SetError("JPEG images are not supported");
476 /* See if an image is contained in a data source */
477 int IMG_isJPG(SDL_RWops *src)
482 /* Load a JPEG type image from an SDL datasource */
483 SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
488 #endif /* LOAD_JPG */
490 #endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */