IMG_jpg.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Jun 2002 17:06:38 +0000
changeset 71 e2eb6980be07
parent 61 a8a23146b406
child 97 e1161bd417c4
permissions -rw-r--r--
Pete Shinners - Thu Jun 20 10:05:54 PDT 2002
* The JPEG loader can now load EXIF format JPEG images
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1999, 2000, 2001  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 /* This is a JPEG image file loading framework */
    26 
    27 #include <stdio.h>
    28 #include <string.h>
    29 #include <setjmp.h>
    30 
    31 #include "SDL_image.h"
    32 
    33 #ifdef LOAD_JPG
    34 
    35 #include <jpeglib.h>
    36 
    37 /* Define this for fast loading and not as good image quality */
    38 /*#define FAST_JPEG*/
    39 
    40 /* See if an image is contained in a data source */
    41 int IMG_isJPG(SDL_RWops *src)
    42 {
    43 	int is_JPG;
    44 	Uint8 magic[4];
    45 
    46 	is_JPG = 0;
    47 	if ( SDL_RWread(src, magic, 2, 1) ) {
    48 		if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
    49 			SDL_RWread(src, magic, 4, 1);
    50 			SDL_RWread(src, magic, 4, 1);
    51 			if ( memcmp((char *)magic, "JFIF", 4) == 0 ||
    52 			     memcmp((char *)magic, "Exif", 4) == 0 ) {
    53 				is_JPG = 1;
    54 			}
    55 		}
    56 	}
    57 	return(is_JPG);
    58 }
    59 
    60 #define INPUT_BUFFER_SIZE	4096
    61 typedef struct {
    62 	struct jpeg_source_mgr pub;
    63 
    64 	SDL_RWops *ctx;
    65 	Uint8 buffer[INPUT_BUFFER_SIZE];
    66 } my_source_mgr;
    67 
    68 /*
    69  * Initialize source --- called by jpeg_read_header
    70  * before any data is actually read.
    71  */
    72 static void init_source (j_decompress_ptr cinfo)
    73 {
    74 	/* We don't actually need to do anything */
    75 	return;
    76 }
    77 
    78 /*
    79  * Fill the input buffer --- called whenever buffer is emptied.
    80  */
    81 static int fill_input_buffer (j_decompress_ptr cinfo)
    82 {
    83 	my_source_mgr * src = (my_source_mgr *) cinfo->src;
    84 	int nbytes;
    85 
    86 	nbytes = SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
    87 	if (nbytes <= 0) {
    88 		/* Insert a fake EOI marker */
    89 		src->buffer[0] = (Uint8) 0xFF;
    90 		src->buffer[1] = (Uint8) JPEG_EOI;
    91 		nbytes = 2;
    92 	}
    93 	src->pub.next_input_byte = src->buffer;
    94 	src->pub.bytes_in_buffer = nbytes;
    95 
    96 	return TRUE;
    97 }
    98 
    99 
   100 /*
   101  * Skip data --- used to skip over a potentially large amount of
   102  * uninteresting data (such as an APPn marker).
   103  *
   104  * Writers of suspendable-input applications must note that skip_input_data
   105  * is not granted the right to give a suspension return.  If the skip extends
   106  * beyond the data currently in the buffer, the buffer can be marked empty so
   107  * that the next read will cause a fill_input_buffer call that can suspend.
   108  * Arranging for additional bytes to be discarded before reloading the input
   109  * buffer is the application writer's problem.
   110  */
   111 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
   112 {
   113 	my_source_mgr * src = (my_source_mgr *) cinfo->src;
   114 
   115 	/* Just a dumb implementation for now.	Could use fseek() except
   116 	 * it doesn't work on pipes.  Not clear that being smart is worth
   117 	 * any trouble anyway --- large skips are infrequent.
   118 	 */
   119 	if (num_bytes > 0) {
   120 		while (num_bytes > (long) src->pub.bytes_in_buffer) {
   121 			num_bytes -= (long) src->pub.bytes_in_buffer;
   122 			(void) src->pub.fill_input_buffer(cinfo);
   123 			/* note we assume that fill_input_buffer will never
   124 			 * return FALSE, so suspension need not be handled.
   125 			 */
   126 		}
   127 		src->pub.next_input_byte += (size_t) num_bytes;
   128 		src->pub.bytes_in_buffer -= (size_t) num_bytes;
   129 	}
   130 }
   131 
   132 /*
   133  * Terminate source --- called by jpeg_finish_decompress
   134  * after all data has been read.
   135  */
   136 static void term_source (j_decompress_ptr cinfo)
   137 {
   138 	/* We don't actually need to do anything */
   139 	return;
   140 }
   141 
   142 /*
   143  * Prepare for input from a stdio stream.
   144  * The caller must have already opened the stream, and is responsible
   145  * for closing it after finishing decompression.
   146  */
   147 static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
   148 {
   149   my_source_mgr *src;
   150 
   151   /* The source object and input buffer are made permanent so that a series
   152    * of JPEG images can be read from the same file by calling jpeg_stdio_src
   153    * only before the first one.  (If we discarded the buffer at the end of
   154    * one image, we'd likely lose the start of the next one.)
   155    * This makes it unsafe to use this manager and a different source
   156    * manager serially with the same JPEG object.  Caveat programmer.
   157    */
   158   if (cinfo->src == NULL) {	/* first time for this JPEG object? */
   159     cinfo->src = (struct jpeg_source_mgr *)
   160       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
   161 				  sizeof(my_source_mgr));
   162     src = (my_source_mgr *) cinfo->src;
   163   }
   164 
   165   src = (my_source_mgr *) cinfo->src;
   166   src->pub.init_source = init_source;
   167   src->pub.fill_input_buffer = fill_input_buffer;
   168   src->pub.skip_input_data = skip_input_data;
   169   src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
   170   src->pub.term_source = term_source;
   171   src->ctx = ctx;
   172   src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
   173   src->pub.next_input_byte = NULL; /* until buffer loaded */
   174 }
   175 
   176 struct my_error_mgr {
   177 	struct jpeg_error_mgr errmgr;
   178 	jmp_buf escape;
   179 };
   180 
   181 static void my_error_exit(j_common_ptr cinfo)
   182 {
   183 	struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
   184 	longjmp(err->escape, 1);
   185 }
   186 
   187 static void output_no_message(j_common_ptr cinfo)
   188 {
   189 	/* do nothing */
   190 }
   191 
   192 /* Load a JPEG type image from an SDL datasource */
   193 SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
   194 {
   195 	struct jpeg_decompress_struct cinfo;
   196 	JSAMPROW rowptr[1];
   197 	SDL_Surface *volatile surface = NULL;
   198 	struct my_error_mgr jerr;
   199 
   200 	/* Create a decompression structure and load the JPEG header */
   201 	cinfo.err = jpeg_std_error(&jerr.errmgr);
   202 	jerr.errmgr.error_exit = my_error_exit;
   203 	jerr.errmgr.output_message = output_no_message;
   204 	if(setjmp(jerr.escape)) {
   205 		/* If we get here, libjpeg found an error */
   206 		jpeg_destroy_decompress(&cinfo);
   207 		IMG_SetError("JPEG loading error");
   208 		SDL_FreeSurface(surface);
   209 		return NULL;
   210 	}
   211 
   212 	jpeg_create_decompress(&cinfo);
   213 	jpeg_SDL_RW_src(&cinfo, src);
   214 	jpeg_read_header(&cinfo, TRUE);
   215 
   216 	/* Set 24-bit RGB output */
   217 	cinfo.out_color_space = JCS_RGB;
   218 	cinfo.quantize_colors = FALSE;
   219 #ifdef FAST_JPEG
   220 	cinfo.scale_num   = 1;
   221 	cinfo.scale_denom = 1;
   222 	cinfo.dct_method = JDCT_FASTEST;
   223 	cinfo.do_fancy_upsampling = FALSE;
   224 #endif
   225 	jpeg_calc_output_dimensions(&cinfo);
   226 
   227 	/* Allocate an output surface to hold the image */
   228 	surface = SDL_AllocSurface(SDL_SWSURFACE,
   229 	               cinfo.output_width, cinfo.output_height, 24,
   230 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   231 	                           0x0000FF, 0x00FF00, 0xFF0000,
   232 #else
   233 	                           0xFF0000, 0x00FF00, 0x0000FF,
   234 #endif
   235 				   0);
   236 	if ( surface == NULL ) {
   237 		IMG_SetError("Out of memory");
   238 		goto done;
   239 	}
   240 
   241 	/* Decompress the image */
   242 	jpeg_start_decompress(&cinfo);
   243 	while ( cinfo.output_scanline < cinfo.output_height ) {
   244 		rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
   245 		                    cinfo.output_scanline * surface->pitch;
   246 		jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
   247 	}
   248 	jpeg_finish_decompress(&cinfo);
   249 
   250 	/* Clean up and return */
   251 done:
   252 	jpeg_destroy_decompress(&cinfo);
   253 	return(surface);
   254 }
   255 
   256 #else
   257 
   258 /* See if an image is contained in a data source */
   259 int IMG_isJPG(SDL_RWops *src)
   260 {
   261 	return(0);
   262 }
   263 
   264 /* Load a JPEG type image from an SDL datasource */
   265 SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
   266 {
   267 	return(NULL);
   268 }
   269 
   270 #endif /* LOAD_JPG */