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