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