IMG_jpg.c
changeset 0 76be7dab668c
child 7 e1b6443ffb6b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/IMG_jpg.c	Thu Aug 10 06:05:39 2000 +0000
     1.3 @@ -0,0 +1,241 @@
     1.4 +/*
     1.5 +    IMGLIB:  An example image loading library for use with SDL
     1.6 +    Copyright (C) 1999  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    5635-34 Springhouse Dr.
    1.24 +    Pleasanton, CA 94588 (USA)
    1.25 +    slouken@devolution.com
    1.26 +*/
    1.27 +
    1.28 +/* This is a JPEG image file loading framework */
    1.29 +
    1.30 +#include <stdio.h>
    1.31 +
    1.32 +#include "SDL_image.h"
    1.33 +
    1.34 +#ifdef LOAD_JPG
    1.35 +
    1.36 +#include <jpeglib.h>
    1.37 +
    1.38 +/* Define this for fast loading and not as good image quality */
    1.39 +/*#define FAST_JPEG*/
    1.40 +
    1.41 +/* See if an image is contained in a data source */
    1.42 +int IMG_isJPG(SDL_RWops *src)
    1.43 +{
    1.44 +	int is_JPG;
    1.45 +	Uint8 magic[4];
    1.46 +
    1.47 +	is_JPG = 0;
    1.48 +	if ( SDL_RWread(src, magic, 2, 1) ) {
    1.49 +		if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
    1.50 +			SDL_RWread(src, magic, 4, 1);
    1.51 +			SDL_RWread(src, magic, 4, 1);
    1.52 +			if ( strncmp(magic, "JFIF", 4) == 0 ) {
    1.53 +				is_JPG = 1;
    1.54 +			}
    1.55 +		}
    1.56 +	}
    1.57 +	return(is_JPG);
    1.58 +}
    1.59 +
    1.60 +#define INPUT_BUFFER_SIZE	4096
    1.61 +typedef struct {
    1.62 +	struct jpeg_source_mgr pub;
    1.63 +
    1.64 +	SDL_RWops *ctx;
    1.65 +	Uint8 buffer[INPUT_BUFFER_SIZE];
    1.66 +} my_source_mgr;
    1.67 +
    1.68 +/*
    1.69 + * Initialize source --- called by jpeg_read_header
    1.70 + * before any data is actually read.
    1.71 + */
    1.72 +void _init_source (j_decompress_ptr cinfo)
    1.73 +{
    1.74 +	/* We don't actually need to do anything */
    1.75 +	return;
    1.76 +}
    1.77 +
    1.78 +/*
    1.79 + * Fill the input buffer --- called whenever buffer is emptied.
    1.80 + */
    1.81 +int _fill_input_buffer (j_decompress_ptr cinfo)
    1.82 +{
    1.83 +	my_source_mgr * src = (my_source_mgr *) cinfo->src;
    1.84 +	int nbytes;
    1.85 +
    1.86 +	nbytes = SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
    1.87 +	if (nbytes <= 0) {
    1.88 +		/* Insert a fake EOI marker */
    1.89 +		src->buffer[0] = (Uint8) 0xFF;
    1.90 +		src->buffer[1] = (Uint8) JPEG_EOI;
    1.91 +		nbytes = 2;
    1.92 +	}
    1.93 +	src->pub.next_input_byte = src->buffer;
    1.94 +	src->pub.bytes_in_buffer = nbytes;
    1.95 +
    1.96 +	return TRUE;
    1.97 +}
    1.98 +
    1.99 +
   1.100 +/*
   1.101 + * Skip data --- used to skip over a potentially large amount of
   1.102 + * uninteresting data (such as an APPn marker).
   1.103 + *
   1.104 + * Writers of suspendable-input applications must note that skip_input_data
   1.105 + * is not granted the right to give a suspension return.  If the skip extends
   1.106 + * beyond the data currently in the buffer, the buffer can be marked empty so
   1.107 + * that the next read will cause a fill_input_buffer call that can suspend.
   1.108 + * Arranging for additional bytes to be discarded before reloading the input
   1.109 + * buffer is the application writer's problem.
   1.110 + */
   1.111 +void _skip_input_data (j_decompress_ptr cinfo, long num_bytes)
   1.112 +{
   1.113 +	my_source_mgr * src = (my_source_mgr *) cinfo->src;
   1.114 +
   1.115 +	/* Just a dumb implementation for now.	Could use fseek() except
   1.116 +	 * it doesn't work on pipes.  Not clear that being smart is worth
   1.117 +	 * any trouble anyway --- large skips are infrequent.
   1.118 +	 */
   1.119 +	if (num_bytes > 0) {
   1.120 +		while (num_bytes > (long) src->pub.bytes_in_buffer) {
   1.121 +			num_bytes -= (long) src->pub.bytes_in_buffer;
   1.122 +			(void) src->pub.fill_input_buffer(cinfo);
   1.123 +			/* note we assume that fill_input_buffer will never
   1.124 +			 * return FALSE, so suspension need not be handled.
   1.125 +			 */
   1.126 +		}
   1.127 +		src->pub.next_input_byte += (size_t) num_bytes;
   1.128 +		src->pub.bytes_in_buffer -= (size_t) num_bytes;
   1.129 +	}
   1.130 +}
   1.131 +
   1.132 +/*
   1.133 + * Terminate source --- called by jpeg_finish_decompress
   1.134 + * after all data has been read.
   1.135 + */
   1.136 +void _term_source (j_decompress_ptr cinfo)
   1.137 +{
   1.138 +	/* We don't actually need to do anything */
   1.139 +	return;
   1.140 +}
   1.141 +
   1.142 +/*
   1.143 + * Prepare for input from a stdio stream.
   1.144 + * The caller must have already opened the stream, and is responsible
   1.145 + * for closing it after finishing decompression.
   1.146 + */
   1.147 +void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
   1.148 +{
   1.149 +  my_source_mgr *src;
   1.150 +
   1.151 +  /* The source object and input buffer are made permanent so that a series
   1.152 +   * of JPEG images can be read from the same file by calling jpeg_stdio_src
   1.153 +   * only before the first one.  (If we discarded the buffer at the end of
   1.154 +   * one image, we'd likely lose the start of the next one.)
   1.155 +   * This makes it unsafe to use this manager and a different source
   1.156 +   * manager serially with the same JPEG object.  Caveat programmer.
   1.157 +   */
   1.158 +  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
   1.159 +    cinfo->src = (struct jpeg_source_mgr *)
   1.160 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
   1.161 +				  sizeof(my_source_mgr));
   1.162 +    src = (my_source_mgr *) cinfo->src;
   1.163 +  }
   1.164 +
   1.165 +  src = (my_source_mgr *) cinfo->src;
   1.166 +  src->pub.init_source = _init_source;
   1.167 +  src->pub.fill_input_buffer = _fill_input_buffer;
   1.168 +  src->pub.skip_input_data = _skip_input_data;
   1.169 +  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
   1.170 +  src->pub.term_source = _term_source;
   1.171 +  src->ctx = ctx;
   1.172 +  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
   1.173 +  src->pub.next_input_byte = NULL; /* until buffer loaded */
   1.174 +}
   1.175 +
   1.176 +/* Load a JPEG type image from an SDL datasource */
   1.177 +SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
   1.178 +{
   1.179 +	struct jpeg_error_mgr errmgr;
   1.180 +	struct jpeg_decompress_struct cinfo;
   1.181 +	JSAMPROW rowptr[1];
   1.182 +	SDL_Surface *surface;
   1.183 +
   1.184 +	/* Create a decompression structure and load the JPEG header */
   1.185 +	cinfo.err = jpeg_std_error(&errmgr);
   1.186 +	jpeg_create_decompress(&cinfo);
   1.187 +	jpeg_SDL_RW_src(&cinfo, src);
   1.188 +	jpeg_read_header(&cinfo, TRUE);
   1.189 +
   1.190 +	/* Set 24-bit RGB output */
   1.191 +	cinfo.out_color_space = JCS_RGB;
   1.192 +	cinfo.quantize_colors = FALSE;
   1.193 +#ifdef FAST_JPEG
   1.194 +	cinfo.scale_num   = 1;
   1.195 +	cinfo.scale_denom = 1;
   1.196 +	cinfo.dct_method = JDCT_FASTEST;
   1.197 +	cinfo.do_fancy_upsampling = FALSE;
   1.198 +#endif
   1.199 +	jpeg_calc_output_dimensions(&cinfo);
   1.200 +
   1.201 +	/* Allocate an output surface to hold the image */
   1.202 +	surface = SDL_AllocSurface(SDL_SWSURFACE,
   1.203 +	               cinfo.output_width, cinfo.output_height, 24,
   1.204 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.205 +	                           0x0000FF, 0x00FF00, 0xFF0000,
   1.206 +#else
   1.207 +	                           0xFF0000, 0x00FF00, 0x0000FF,
   1.208 +#endif
   1.209 +				   0);
   1.210 +	if ( surface == NULL ) {
   1.211 +		IMG_SetError("Out of memory");
   1.212 +		goto done;
   1.213 +	}
   1.214 +
   1.215 +	/* Decompress the image */
   1.216 +	jpeg_start_decompress(&cinfo);
   1.217 +	while ( cinfo.output_scanline < cinfo.output_height ) {
   1.218 +		rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
   1.219 +		                    cinfo.output_scanline * surface->pitch;
   1.220 +		jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
   1.221 +	}
   1.222 +	jpeg_finish_decompress(&cinfo);
   1.223 +
   1.224 +	/* Clean up and return */
   1.225 +done:
   1.226 +	jpeg_destroy_decompress(&cinfo);
   1.227 +	return(surface);
   1.228 +}
   1.229 +
   1.230 +#else
   1.231 +
   1.232 +/* See if an image is contained in a data source */
   1.233 +int IMG_isJPG(SDL_RWops *src)
   1.234 +{
   1.235 +	return(0);
   1.236 +}
   1.237 +
   1.238 +/* Load a JPEG type image from an SDL datasource */
   1.239 +SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
   1.240 +{
   1.241 +	return(NULL);
   1.242 +}
   1.243 +
   1.244 +#endif /* LOAD_JPG */