2 SDL_image: An example image loading library for use with SDL
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 #if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
24 /* This is a JPEG image file loading framework */
30 #include "SDL_image.h"
36 #ifdef JPEG_TRUE /* MinGW version of jpeg-8.x renamed TRUE to JPEG_TRUE etc. */
37 typedef JPEG_boolean boolean;
38 #define TRUE JPEG_TRUE
39 #define FALSE JPEG_FALSE
42 /* Define this for fast loading and not as good image quality */
45 /* Define this for quicker (but less perfect) JPEG identification */
51 void (*jpeg_calc_output_dimensions) (j_decompress_ptr cinfo);
52 void (*jpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
53 void (*jpeg_destroy_decompress) (j_decompress_ptr cinfo);
54 boolean (*jpeg_finish_decompress) (j_decompress_ptr cinfo);
55 int (*jpeg_read_header) (j_decompress_ptr cinfo, boolean require_image);
56 JDIMENSION (*jpeg_read_scanlines) (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines);
57 boolean (*jpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
58 boolean (*jpeg_start_decompress) (j_decompress_ptr cinfo);
59 struct jpeg_error_mgr * (*jpeg_std_error) (struct jpeg_error_mgr * err);
62 #ifdef LOAD_JPG_DYNAMIC
65 if ( lib.loaded == 0 ) {
66 lib.handle = SDL_LoadObject(LOAD_JPG_DYNAMIC);
67 if ( lib.handle == NULL ) {
70 lib.jpeg_calc_output_dimensions =
71 (void (*) (j_decompress_ptr))
72 SDL_LoadFunction(lib.handle, "jpeg_calc_output_dimensions");
73 if ( lib.jpeg_calc_output_dimensions == NULL ) {
74 SDL_UnloadObject(lib.handle);
77 lib.jpeg_CreateDecompress =
78 (void (*) (j_decompress_ptr, int, size_t))
79 SDL_LoadFunction(lib.handle, "jpeg_CreateDecompress");
80 if ( lib.jpeg_CreateDecompress == NULL ) {
81 SDL_UnloadObject(lib.handle);
84 lib.jpeg_destroy_decompress =
85 (void (*) (j_decompress_ptr))
86 SDL_LoadFunction(lib.handle, "jpeg_destroy_decompress");
87 if ( lib.jpeg_destroy_decompress == NULL ) {
88 SDL_UnloadObject(lib.handle);
91 lib.jpeg_finish_decompress =
92 (boolean (*) (j_decompress_ptr))
93 SDL_LoadFunction(lib.handle, "jpeg_finish_decompress");
94 if ( lib.jpeg_finish_decompress == NULL ) {
95 SDL_UnloadObject(lib.handle);
98 lib.jpeg_read_header =
99 (int (*) (j_decompress_ptr, boolean))
100 SDL_LoadFunction(lib.handle, "jpeg_read_header");
101 if ( lib.jpeg_read_header == NULL ) {
102 SDL_UnloadObject(lib.handle);
105 lib.jpeg_read_scanlines =
106 (JDIMENSION (*) (j_decompress_ptr, JSAMPARRAY, JDIMENSION))
107 SDL_LoadFunction(lib.handle, "jpeg_read_scanlines");
108 if ( lib.jpeg_read_scanlines == NULL ) {
109 SDL_UnloadObject(lib.handle);
112 lib.jpeg_resync_to_restart =
113 (boolean (*) (j_decompress_ptr, int))
114 SDL_LoadFunction(lib.handle, "jpeg_resync_to_restart");
115 if ( lib.jpeg_resync_to_restart == NULL ) {
116 SDL_UnloadObject(lib.handle);
119 lib.jpeg_start_decompress =
120 (boolean (*) (j_decompress_ptr))
121 SDL_LoadFunction(lib.handle, "jpeg_start_decompress");
122 if ( lib.jpeg_start_decompress == NULL ) {
123 SDL_UnloadObject(lib.handle);
127 (struct jpeg_error_mgr * (*) (struct jpeg_error_mgr *))
128 SDL_LoadFunction(lib.handle, "jpeg_std_error");
129 if ( lib.jpeg_std_error == NULL ) {
130 SDL_UnloadObject(lib.handle);
140 if ( lib.loaded == 0 ) {
143 if ( lib.loaded == 1 ) {
144 SDL_UnloadObject(lib.handle);
151 if ( lib.loaded == 0 ) {
152 lib.jpeg_calc_output_dimensions = jpeg_calc_output_dimensions;
153 lib.jpeg_CreateDecompress = jpeg_CreateDecompress;
154 lib.jpeg_destroy_decompress = jpeg_destroy_decompress;
155 lib.jpeg_finish_decompress = jpeg_finish_decompress;
156 lib.jpeg_read_header = jpeg_read_header;
157 lib.jpeg_read_scanlines = jpeg_read_scanlines;
158 lib.jpeg_resync_to_restart = jpeg_resync_to_restart;
159 lib.jpeg_start_decompress = jpeg_start_decompress;
160 lib.jpeg_std_error = jpeg_std_error;
168 if ( lib.loaded == 0 ) {
171 if ( lib.loaded == 1 ) {
175 #endif /* LOAD_JPG_DYNAMIC */
177 /* See if an image is contained in a data source */
178 int IMG_isJPG(SDL_RWops *src)
185 /* This detection code is by Steaphan Greene <stea@cs.binghamton.edu> */
186 /* Blame me, not Sam, if this doesn't work right. */
187 /* And don't forget to report the problem to the the sdl list too! */
191 start = SDL_RWtell(src);
194 if ( SDL_RWread(src, magic, 2, 1) ) {
195 if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
197 while (is_JPG == 1) {
198 if(SDL_RWread(src, magic, 1, 2) != 2) {
200 } else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
202 } else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
203 /* Extra padding in JPEG (legal) */
204 /* or this is data and we are scanning */
205 SDL_RWseek(src, -1, RW_SEEK_CUR);
206 } else if(magic[1] == 0xD9) {
207 /* Got to end of good JPEG */
209 } else if( (in_scan == 1) && (magic[1] == 0x00) ) {
210 /* This is an encoded 0xFF within the data */
211 } else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
212 /* These have nothing else */
213 } else if(SDL_RWread(src, magic+2, 1, 2) != 2) {
216 /* Yes, it's big-endian */
220 innerStart = SDL_RWtell(src);
221 size = (magic[2] << 8) + magic[3];
222 end = SDL_RWseek(src, size-2, RW_SEEK_CUR);
223 if ( end != innerStart + size - 2 ) is_JPG = 0;
224 if ( magic[1] == 0xDA ) {
225 /* Now comes the actual JPEG meat */
227 /* Ok, I'm convinced. It is a JPEG. */
230 /* I'm not convinced. Prove it! */
238 SDL_RWseek(src, start, RW_SEEK_SET);
242 #define INPUT_BUFFER_SIZE 4096
244 struct jpeg_source_mgr pub;
247 Uint8 buffer[INPUT_BUFFER_SIZE];
251 * Initialize source --- called by jpeg_read_header
252 * before any data is actually read.
254 static void init_source (j_decompress_ptr cinfo)
256 /* We don't actually need to do anything */
261 * Fill the input buffer --- called whenever buffer is emptied.
263 static boolean fill_input_buffer (j_decompress_ptr cinfo)
265 my_source_mgr * src = (my_source_mgr *) cinfo->src;
268 nbytes = SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
270 /* Insert a fake EOI marker */
271 src->buffer[0] = (Uint8) 0xFF;
272 src->buffer[1] = (Uint8) JPEG_EOI;
275 src->pub.next_input_byte = src->buffer;
276 src->pub.bytes_in_buffer = nbytes;
283 * Skip data --- used to skip over a potentially large amount of
284 * uninteresting data (such as an APPn marker).
286 * Writers of suspendable-input applications must note that skip_input_data
287 * is not granted the right to give a suspension return. If the skip extends
288 * beyond the data currently in the buffer, the buffer can be marked empty so
289 * that the next read will cause a fill_input_buffer call that can suspend.
290 * Arranging for additional bytes to be discarded before reloading the input
291 * buffer is the application writer's problem.
293 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
295 my_source_mgr * src = (my_source_mgr *) cinfo->src;
297 /* Just a dumb implementation for now. Could use fseek() except
298 * it doesn't work on pipes. Not clear that being smart is worth
299 * any trouble anyway --- large skips are infrequent.
302 while (num_bytes > (long) src->pub.bytes_in_buffer) {
303 num_bytes -= (long) src->pub.bytes_in_buffer;
304 (void) src->pub.fill_input_buffer(cinfo);
305 /* note we assume that fill_input_buffer will never
306 * return FALSE, so suspension need not be handled.
309 src->pub.next_input_byte += (size_t) num_bytes;
310 src->pub.bytes_in_buffer -= (size_t) num_bytes;
315 * Terminate source --- called by jpeg_finish_decompress
316 * after all data has been read.
318 static void term_source (j_decompress_ptr cinfo)
320 /* We don't actually need to do anything */
325 * Prepare for input from a stdio stream.
326 * The caller must have already opened the stream, and is responsible
327 * for closing it after finishing decompression.
329 static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
333 /* The source object and input buffer are made permanent so that a series
334 * of JPEG images can be read from the same file by calling jpeg_stdio_src
335 * only before the first one. (If we discarded the buffer at the end of
336 * one image, we'd likely lose the start of the next one.)
337 * This makes it unsafe to use this manager and a different source
338 * manager serially with the same JPEG object. Caveat programmer.
340 if (cinfo->src == NULL) { /* first time for this JPEG object? */
341 cinfo->src = (struct jpeg_source_mgr *)
342 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
343 sizeof(my_source_mgr));
344 src = (my_source_mgr *) cinfo->src;
347 src = (my_source_mgr *) cinfo->src;
348 src->pub.init_source = init_source;
349 src->pub.fill_input_buffer = fill_input_buffer;
350 src->pub.skip_input_data = skip_input_data;
351 src->pub.resync_to_restart = lib.jpeg_resync_to_restart; /* use default method */
352 src->pub.term_source = term_source;
354 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
355 src->pub.next_input_byte = NULL; /* until buffer loaded */
358 struct my_error_mgr {
359 struct jpeg_error_mgr errmgr;
363 static void my_error_exit(j_common_ptr cinfo)
365 struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
366 longjmp(err->escape, 1);
369 static void output_no_message(j_common_ptr cinfo)
374 /* Load a JPEG type image from an SDL datasource */
375 SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
378 struct jpeg_decompress_struct cinfo;
380 SDL_Surface *volatile surface = NULL;
381 struct my_error_mgr jerr;
384 /* The error message has been set in SDL_RWFromFile */
387 start = SDL_RWtell(src);
389 if ( !IMG_Init(IMG_INIT_JPG) ) {
393 /* Create a decompression structure and load the JPEG header */
394 cinfo.err = lib.jpeg_std_error(&jerr.errmgr);
395 jerr.errmgr.error_exit = my_error_exit;
396 jerr.errmgr.output_message = output_no_message;
397 if(setjmp(jerr.escape)) {
398 /* If we get here, libjpeg found an error */
399 lib.jpeg_destroy_decompress(&cinfo);
400 if ( surface != NULL ) {
401 SDL_FreeSurface(surface);
403 SDL_RWseek(src, start, RW_SEEK_SET);
404 IMG_SetError("JPEG loading error");
408 lib.jpeg_create_decompress(&cinfo);
409 jpeg_SDL_RW_src(&cinfo, src);
410 lib.jpeg_read_header(&cinfo, TRUE);
412 if(cinfo.num_components == 4) {
413 /* Set 32-bit Raw output */
414 cinfo.out_color_space = JCS_CMYK;
415 cinfo.quantize_colors = FALSE;
416 lib.jpeg_calc_output_dimensions(&cinfo);
418 /* Allocate an output surface to hold the image */
419 surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
420 cinfo.output_width, cinfo.output_height, 32,
421 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
422 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
424 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
427 /* Set 24-bit RGB output */
428 cinfo.out_color_space = JCS_RGB;
429 cinfo.quantize_colors = FALSE;
432 cinfo.scale_denom = 1;
433 cinfo.dct_method = JDCT_FASTEST;
434 cinfo.do_fancy_upsampling = FALSE;
436 lib.jpeg_calc_output_dimensions(&cinfo);
438 /* Allocate an output surface to hold the image */
439 surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
440 cinfo.output_width, cinfo.output_height, 24,
441 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
442 0x0000FF, 0x00FF00, 0xFF0000,
444 0xFF0000, 0x00FF00, 0x0000FF,
449 if ( surface == NULL ) {
450 lib.jpeg_destroy_decompress(&cinfo);
451 SDL_RWseek(src, start, RW_SEEK_SET);
452 IMG_SetError("Out of memory");
456 /* Decompress the image */
457 lib.jpeg_start_decompress(&cinfo);
458 while ( cinfo.output_scanline < cinfo.output_height ) {
459 rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
460 cinfo.output_scanline * surface->pitch;
461 lib.jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
463 lib.jpeg_finish_decompress(&cinfo);
464 lib.jpeg_destroy_decompress(&cinfo);
473 IMG_SetError("JPEG images are not supported");
481 /* See if an image is contained in a data source */
482 int IMG_isJPG(SDL_RWops *src)
487 /* Load a JPEG type image from an SDL datasource */
488 SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
493 #endif /* LOAD_JPG */
495 #endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */