IMG_jpg.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 10 Nov 2009 06:00:59 +0000
changeset 237 d1a4fae44bd0
parent 236 cce1251de477
child 273 b1c1ec3a8d49
permissions -rw-r--r--
In the process of tracking down bug #868, made ImageIO the default for building using command line tools, and cleaned up the ImageIO image loading code.
slouken@0
     1
/*
slouken@53
     2
    SDL_image:  An example image loading library for use with SDL
slouken@186
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@121
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@121
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@121
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@121
    15
    You should have received a copy of the GNU Lesser General Public
slouken@121
    16
    License along with this library; if not, write to the Free Software
slouken@121
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@53
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@237
    23
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
slouken@237
    24
slouken@0
    25
/* This is a JPEG image file loading framework */
slouken@0
    26
slouken@0
    27
#include <stdio.h>
slouken@7
    28
#include <string.h>
slouken@61
    29
#include <setjmp.h>
slouken@0
    30
slouken@0
    31
#include "SDL_image.h"
slouken@0
    32
slouken@0
    33
#ifdef LOAD_JPG
slouken@0
    34
slouken@0
    35
#include <jpeglib.h>
slouken@0
    36
slouken@0
    37
/* Define this for fast loading and not as good image quality */
slouken@0
    38
/*#define FAST_JPEG*/
slouken@0
    39
slouken@123
    40
/* Define this for quicker (but less perfect) JPEG identification */
slouken@123
    41
#define FAST_IS_JPEG
slouken@123
    42
slouken@143
    43
static struct {
slouken@143
    44
	int loaded;
slouken@143
    45
	void *handle;
slouken@143
    46
	void (*jpeg_calc_output_dimensions) (j_decompress_ptr cinfo);
slouken@143
    47
	void (*jpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
slouken@143
    48
	void (*jpeg_destroy_decompress) (j_decompress_ptr cinfo);
slouken@143
    49
	boolean (*jpeg_finish_decompress) (j_decompress_ptr cinfo);
slouken@143
    50
	int (*jpeg_read_header) (j_decompress_ptr cinfo, boolean require_image);
slouken@143
    51
	JDIMENSION (*jpeg_read_scanlines) (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines);
slouken@143
    52
	boolean (*jpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
slouken@143
    53
	boolean (*jpeg_start_decompress) (j_decompress_ptr cinfo);
slouken@143
    54
	struct jpeg_error_mgr * (*jpeg_std_error) (struct jpeg_error_mgr * err);
slouken@143
    55
} lib;
slouken@143
    56
slouken@143
    57
#ifdef LOAD_JPG_DYNAMIC
slouken@205
    58
int IMG_InitJPG()
slouken@143
    59
{
slouken@143
    60
	if ( lib.loaded == 0 ) {
slouken@143
    61
		lib.handle = SDL_LoadObject(LOAD_JPG_DYNAMIC);
slouken@143
    62
		if ( lib.handle == NULL ) {
slouken@143
    63
			return -1;
slouken@143
    64
		}
slouken@143
    65
		lib.jpeg_calc_output_dimensions =
slouken@143
    66
			(void (*) (j_decompress_ptr))
slouken@143
    67
			SDL_LoadFunction(lib.handle, "jpeg_calc_output_dimensions");
slouken@143
    68
		if ( lib.jpeg_calc_output_dimensions == NULL ) {
slouken@143
    69
			SDL_UnloadObject(lib.handle);
slouken@143
    70
			return -1;
slouken@143
    71
		}
slouken@143
    72
		lib.jpeg_CreateDecompress = 
slouken@143
    73
			(void (*) (j_decompress_ptr, int, size_t))
slouken@143
    74
			SDL_LoadFunction(lib.handle, "jpeg_CreateDecompress");
slouken@143
    75
		if ( lib.jpeg_CreateDecompress == NULL ) {
slouken@143
    76
			SDL_UnloadObject(lib.handle);
slouken@143
    77
			return -1;
slouken@143
    78
		}
slouken@143
    79
		lib.jpeg_destroy_decompress = 
slouken@143
    80
			(void (*) (j_decompress_ptr))
slouken@143
    81
			SDL_LoadFunction(lib.handle, "jpeg_destroy_decompress");
slouken@143
    82
		if ( lib.jpeg_destroy_decompress == NULL ) {
slouken@143
    83
			SDL_UnloadObject(lib.handle);
slouken@143
    84
			return -1;
slouken@143
    85
		}
slouken@143
    86
		lib.jpeg_finish_decompress = 
slouken@143
    87
			(boolean (*) (j_decompress_ptr))
slouken@143
    88
			SDL_LoadFunction(lib.handle, "jpeg_finish_decompress");
slouken@143
    89
		if ( lib.jpeg_finish_decompress == NULL ) {
slouken@143
    90
			SDL_UnloadObject(lib.handle);
slouken@143
    91
			return -1;
slouken@143
    92
		}
slouken@143
    93
		lib.jpeg_read_header = 
slouken@143
    94
			(int (*) (j_decompress_ptr, boolean))
slouken@143
    95
			SDL_LoadFunction(lib.handle, "jpeg_read_header");
slouken@143
    96
		if ( lib.jpeg_read_header == NULL ) {
slouken@143
    97
			SDL_UnloadObject(lib.handle);
slouken@143
    98
			return -1;
slouken@143
    99
		}
slouken@143
   100
		lib.jpeg_read_scanlines = 
slouken@143
   101
			(JDIMENSION (*) (j_decompress_ptr, JSAMPARRAY, JDIMENSION))
slouken@143
   102
			SDL_LoadFunction(lib.handle, "jpeg_read_scanlines");
slouken@143
   103
		if ( lib.jpeg_read_scanlines == NULL ) {
slouken@143
   104
			SDL_UnloadObject(lib.handle);
slouken@143
   105
			return -1;
slouken@143
   106
		}
slouken@143
   107
		lib.jpeg_resync_to_restart = 
slouken@143
   108
			(boolean (*) (j_decompress_ptr, int))
slouken@143
   109
			SDL_LoadFunction(lib.handle, "jpeg_resync_to_restart");
slouken@143
   110
		if ( lib.jpeg_resync_to_restart == NULL ) {
slouken@143
   111
			SDL_UnloadObject(lib.handle);
slouken@143
   112
			return -1;
slouken@143
   113
		}
slouken@143
   114
		lib.jpeg_start_decompress = 
slouken@143
   115
			(boolean (*) (j_decompress_ptr))
slouken@143
   116
			SDL_LoadFunction(lib.handle, "jpeg_start_decompress");
slouken@143
   117
		if ( lib.jpeg_start_decompress == NULL ) {
slouken@143
   118
			SDL_UnloadObject(lib.handle);
slouken@143
   119
			return -1;
slouken@143
   120
		}
slouken@143
   121
		lib.jpeg_std_error = 
slouken@143
   122
			(struct jpeg_error_mgr * (*) (struct jpeg_error_mgr *))
slouken@143
   123
			SDL_LoadFunction(lib.handle, "jpeg_std_error");
slouken@143
   124
		if ( lib.jpeg_std_error == NULL ) {
slouken@143
   125
			SDL_UnloadObject(lib.handle);
slouken@143
   126
			return -1;
slouken@143
   127
		}
slouken@143
   128
	}
slouken@143
   129
	++lib.loaded;
slouken@143
   130
slouken@143
   131
	return 0;
slouken@143
   132
}
slouken@205
   133
void IMG_QuitJPG()
slouken@143
   134
{
slouken@143
   135
	if ( lib.loaded == 0 ) {
slouken@143
   136
		return;
slouken@143
   137
	}
slouken@143
   138
	if ( lib.loaded == 1 ) {
slouken@143
   139
		SDL_UnloadObject(lib.handle);
slouken@143
   140
	}
slouken@143
   141
	--lib.loaded;
slouken@143
   142
}
slouken@143
   143
#else
slouken@205
   144
int IMG_InitJPG()
slouken@143
   145
{
slouken@143
   146
	if ( lib.loaded == 0 ) {
slouken@143
   147
		lib.jpeg_calc_output_dimensions = jpeg_calc_output_dimensions;
slouken@143
   148
		lib.jpeg_CreateDecompress = jpeg_CreateDecompress;
slouken@143
   149
		lib.jpeg_destroy_decompress = jpeg_destroy_decompress;
slouken@143
   150
		lib.jpeg_finish_decompress = jpeg_finish_decompress;
slouken@143
   151
		lib.jpeg_read_header = jpeg_read_header;
slouken@143
   152
		lib.jpeg_read_scanlines = jpeg_read_scanlines;
slouken@143
   153
		lib.jpeg_resync_to_restart = jpeg_resync_to_restart;
slouken@143
   154
		lib.jpeg_start_decompress = jpeg_start_decompress;
slouken@143
   155
		lib.jpeg_std_error = jpeg_std_error;
slouken@143
   156
	}
slouken@143
   157
	++lib.loaded;
slouken@146
   158
slouken@146
   159
	return 0;
slouken@143
   160
}
slouken@205
   161
void IMG_QuitJPG()
slouken@143
   162
{
slouken@143
   163
	if ( lib.loaded == 0 ) {
slouken@143
   164
		return;
slouken@143
   165
	}
slouken@143
   166
	if ( lib.loaded == 1 ) {
slouken@143
   167
	}
slouken@143
   168
	--lib.loaded;
slouken@143
   169
}
slouken@143
   170
#endif /* LOAD_JPG_DYNAMIC */
slouken@143
   171
slouken@0
   172
/* See if an image is contained in a data source */
slouken@0
   173
int IMG_isJPG(SDL_RWops *src)
slouken@0
   174
{
slouken@117
   175
	int start;
slouken@0
   176
	int is_JPG;
slouken@123
   177
	int in_scan;
slouken@0
   178
	Uint8 magic[4];
slouken@0
   179
slouken@123
   180
	/* This detection code is by Steaphan Greene <stea@cs.binghamton.edu> */
slouken@123
   181
	/* Blame me, not Sam, if this doesn't work right. */
slouken@123
   182
	/* And don't forget to report the problem to the the sdl list too! */
slouken@123
   183
icculus@154
   184
	if ( !src )
icculus@154
   185
		return 0;
slouken@117
   186
	start = SDL_RWtell(src);
slouken@0
   187
	is_JPG = 0;
slouken@123
   188
	in_scan = 0;
slouken@0
   189
	if ( SDL_RWread(src, magic, 2, 1) ) {
slouken@0
   190
		if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
slouken@123
   191
			is_JPG = 1;
slouken@123
   192
			while (is_JPG == 1) {
slouken@123
   193
				if(SDL_RWread(src, magic, 1, 2) != 2) {
slouken@123
   194
					is_JPG = 0;
slouken@123
   195
				} else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
slouken@123
   196
					is_JPG = 0;
slouken@123
   197
				} else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
slouken@123
   198
					/* Extra padding in JPEG (legal) */
slouken@123
   199
					/* or this is data and we are scanning */
slouken@236
   200
					SDL_RWseek(src, -1, RW_SEEK_CUR);
slouken@123
   201
				} else if(magic[1] == 0xD9) {
slouken@123
   202
					/* Got to end of good JPEG */
slouken@123
   203
					break;
slouken@123
   204
				} else if( (in_scan == 1) && (magic[1] == 0x00) ) {
slouken@123
   205
					/* This is an encoded 0xFF within the data */
slouken@123
   206
				} else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
slouken@123
   207
					/* These have nothing else */
slouken@123
   208
				} else if(SDL_RWread(src, magic+2, 1, 2) != 2) {
slouken@123
   209
					is_JPG = 0;
slouken@123
   210
				} else {
slouken@123
   211
					/* Yes, it's big-endian */
slouken@123
   212
					Uint32 start;
slouken@123
   213
					Uint32 size;
slouken@123
   214
					Uint32 end;
slouken@123
   215
					start = SDL_RWtell(src);
slouken@123
   216
					size = (magic[2] << 8) + magic[3];
slouken@236
   217
					end = SDL_RWseek(src, size-2, RW_SEEK_CUR);
slouken@123
   218
					if ( end != start + size - 2 ) is_JPG = 0;
slouken@123
   219
					if ( magic[1] == 0xDA ) {
slouken@123
   220
						/* Now comes the actual JPEG meat */
slouken@123
   221
#ifdef	FAST_IS_JPEG
slouken@123
   222
						/* Ok, I'm convinced.  It is a JPEG. */
slouken@123
   223
						break;
slouken@123
   224
#else
slouken@123
   225
						/* I'm not convinced.  Prove it! */
slouken@123
   226
						in_scan = 1;
slouken@123
   227
#endif
slouken@123
   228
					}
slouken@123
   229
				}
slouken@0
   230
			}
slouken@0
   231
		}
slouken@0
   232
	}
slouken@236
   233
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@0
   234
	return(is_JPG);
slouken@0
   235
}
slouken@0
   236
slouken@0
   237
#define INPUT_BUFFER_SIZE	4096
slouken@0
   238
typedef struct {
slouken@0
   239
	struct jpeg_source_mgr pub;
slouken@0
   240
slouken@0
   241
	SDL_RWops *ctx;
slouken@0
   242
	Uint8 buffer[INPUT_BUFFER_SIZE];
slouken@0
   243
} my_source_mgr;
slouken@0
   244
slouken@0
   245
/*
slouken@0
   246
 * Initialize source --- called by jpeg_read_header
slouken@0
   247
 * before any data is actually read.
slouken@0
   248
 */
slouken@27
   249
static void init_source (j_decompress_ptr cinfo)
slouken@0
   250
{
slouken@0
   251
	/* We don't actually need to do anything */
slouken@0
   252
	return;
slouken@0
   253
}
slouken@0
   254
slouken@0
   255
/*
slouken@0
   256
 * Fill the input buffer --- called whenever buffer is emptied.
slouken@0
   257
 */
slouken@27
   258
static int fill_input_buffer (j_decompress_ptr cinfo)
slouken@0
   259
{
slouken@0
   260
	my_source_mgr * src = (my_source_mgr *) cinfo->src;
slouken@0
   261
	int nbytes;
slouken@0
   262
slouken@0
   263
	nbytes = SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
slouken@0
   264
	if (nbytes <= 0) {
slouken@0
   265
		/* Insert a fake EOI marker */
slouken@0
   266
		src->buffer[0] = (Uint8) 0xFF;
slouken@0
   267
		src->buffer[1] = (Uint8) JPEG_EOI;
slouken@0
   268
		nbytes = 2;
slouken@0
   269
	}
slouken@0
   270
	src->pub.next_input_byte = src->buffer;
slouken@0
   271
	src->pub.bytes_in_buffer = nbytes;
slouken@0
   272
slouken@0
   273
	return TRUE;
slouken@0
   274
}
slouken@0
   275
slouken@0
   276
slouken@0
   277
/*
slouken@0
   278
 * Skip data --- used to skip over a potentially large amount of
slouken@0
   279
 * uninteresting data (such as an APPn marker).
slouken@0
   280
 *
slouken@0
   281
 * Writers of suspendable-input applications must note that skip_input_data
slouken@0
   282
 * is not granted the right to give a suspension return.  If the skip extends
slouken@0
   283
 * beyond the data currently in the buffer, the buffer can be marked empty so
slouken@0
   284
 * that the next read will cause a fill_input_buffer call that can suspend.
slouken@0
   285
 * Arranging for additional bytes to be discarded before reloading the input
slouken@0
   286
 * buffer is the application writer's problem.
slouken@0
   287
 */
slouken@27
   288
static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
slouken@0
   289
{
slouken@0
   290
	my_source_mgr * src = (my_source_mgr *) cinfo->src;
slouken@0
   291
slouken@0
   292
	/* Just a dumb implementation for now.	Could use fseek() except
slouken@0
   293
	 * it doesn't work on pipes.  Not clear that being smart is worth
slouken@0
   294
	 * any trouble anyway --- large skips are infrequent.
slouken@0
   295
	 */
slouken@0
   296
	if (num_bytes > 0) {
slouken@0
   297
		while (num_bytes > (long) src->pub.bytes_in_buffer) {
slouken@0
   298
			num_bytes -= (long) src->pub.bytes_in_buffer;
slouken@0
   299
			(void) src->pub.fill_input_buffer(cinfo);
slouken@0
   300
			/* note we assume that fill_input_buffer will never
slouken@0
   301
			 * return FALSE, so suspension need not be handled.
slouken@0
   302
			 */
slouken@0
   303
		}
slouken@0
   304
		src->pub.next_input_byte += (size_t) num_bytes;
slouken@0
   305
		src->pub.bytes_in_buffer -= (size_t) num_bytes;
slouken@0
   306
	}
slouken@0
   307
}
slouken@0
   308
slouken@0
   309
/*
slouken@0
   310
 * Terminate source --- called by jpeg_finish_decompress
slouken@0
   311
 * after all data has been read.
slouken@0
   312
 */
slouken@27
   313
static void term_source (j_decompress_ptr cinfo)
slouken@0
   314
{
slouken@0
   315
	/* We don't actually need to do anything */
slouken@0
   316
	return;
slouken@0
   317
}
slouken@0
   318
slouken@0
   319
/*
slouken@0
   320
 * Prepare for input from a stdio stream.
slouken@0
   321
 * The caller must have already opened the stream, and is responsible
slouken@0
   322
 * for closing it after finishing decompression.
slouken@0
   323
 */
slouken@27
   324
static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
slouken@0
   325
{
slouken@0
   326
  my_source_mgr *src;
slouken@0
   327
slouken@0
   328
  /* The source object and input buffer are made permanent so that a series
slouken@0
   329
   * of JPEG images can be read from the same file by calling jpeg_stdio_src
slouken@0
   330
   * only before the first one.  (If we discarded the buffer at the end of
slouken@0
   331
   * one image, we'd likely lose the start of the next one.)
slouken@0
   332
   * This makes it unsafe to use this manager and a different source
slouken@0
   333
   * manager serially with the same JPEG object.  Caveat programmer.
slouken@0
   334
   */
slouken@0
   335
  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
slouken@0
   336
    cinfo->src = (struct jpeg_source_mgr *)
slouken@0
   337
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
slouken@0
   338
				  sizeof(my_source_mgr));
slouken@0
   339
    src = (my_source_mgr *) cinfo->src;
slouken@0
   340
  }
slouken@0
   341
slouken@0
   342
  src = (my_source_mgr *) cinfo->src;
slouken@27
   343
  src->pub.init_source = init_source;
slouken@27
   344
  src->pub.fill_input_buffer = fill_input_buffer;
slouken@27
   345
  src->pub.skip_input_data = skip_input_data;
slouken@143
   346
  src->pub.resync_to_restart = lib.jpeg_resync_to_restart; /* use default method */
slouken@27
   347
  src->pub.term_source = term_source;
slouken@0
   348
  src->ctx = ctx;
slouken@0
   349
  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
slouken@0
   350
  src->pub.next_input_byte = NULL; /* until buffer loaded */
slouken@0
   351
}
slouken@0
   352
slouken@61
   353
struct my_error_mgr {
slouken@61
   354
	struct jpeg_error_mgr errmgr;
slouken@61
   355
	jmp_buf escape;
slouken@61
   356
};
slouken@61
   357
slouken@61
   358
static void my_error_exit(j_common_ptr cinfo)
slouken@61
   359
{
slouken@61
   360
	struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
slouken@61
   361
	longjmp(err->escape, 1);
slouken@61
   362
}
slouken@61
   363
slouken@61
   364
static void output_no_message(j_common_ptr cinfo)
slouken@61
   365
{
slouken@61
   366
	/* do nothing */
slouken@61
   367
}
slouken@61
   368
slouken@0
   369
/* Load a JPEG type image from an SDL datasource */
slouken@0
   370
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
slouken@0
   371
{
slouken@118
   372
	int start;
slouken@0
   373
	struct jpeg_decompress_struct cinfo;
slouken@0
   374
	JSAMPROW rowptr[1];
slouken@61
   375
	SDL_Surface *volatile surface = NULL;
slouken@61
   376
	struct my_error_mgr jerr;
slouken@0
   377
slouken@98
   378
	if ( !src ) {
slouken@98
   379
		/* The error message has been set in SDL_RWFromFile */
slouken@98
   380
		return NULL;
slouken@98
   381
	}
slouken@118
   382
	start = SDL_RWtell(src);
slouken@98
   383
slouken@233
   384
	if ( !IMG_Init(IMG_INIT_JPG) ) {
slouken@143
   385
		return NULL;
slouken@143
   386
	}
slouken@143
   387
slouken@0
   388
	/* Create a decompression structure and load the JPEG header */
slouken@143
   389
	cinfo.err = lib.jpeg_std_error(&jerr.errmgr);
slouken@61
   390
	jerr.errmgr.error_exit = my_error_exit;
slouken@61
   391
	jerr.errmgr.output_message = output_no_message;
slouken@61
   392
	if(setjmp(jerr.escape)) {
slouken@61
   393
		/* If we get here, libjpeg found an error */
slouken@143
   394
		lib.jpeg_destroy_decompress(&cinfo);
slouken@118
   395
		if ( surface != NULL ) {
slouken@118
   396
			SDL_FreeSurface(surface);
slouken@118
   397
		}
slouken@236
   398
		SDL_RWseek(src, start, RW_SEEK_SET);
slouken@61
   399
		IMG_SetError("JPEG loading error");
slouken@61
   400
		return NULL;
slouken@61
   401
	}
slouken@61
   402
slouken@143
   403
	lib.jpeg_create_decompress(&cinfo);
slouken@0
   404
	jpeg_SDL_RW_src(&cinfo, src);
slouken@143
   405
	lib.jpeg_read_header(&cinfo, TRUE);
slouken@0
   406
slouken@123
   407
	if(cinfo.num_components == 4) {
slouken@123
   408
		/* Set 32-bit Raw output */
slouken@123
   409
		cinfo.out_color_space = JCS_CMYK;
slouken@123
   410
		cinfo.quantize_colors = FALSE;
slouken@143
   411
		lib.jpeg_calc_output_dimensions(&cinfo);
slouken@123
   412
slouken@123
   413
		/* Allocate an output surface to hold the image */
slouken@123
   414
		surface = SDL_AllocSurface(SDL_SWSURFACE,
slouken@123
   415
		        cinfo.output_width, cinfo.output_height, 32,
slouken@123
   416
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@123
   417
		                   0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
slouken@123
   418
#else
slouken@123
   419
		                   0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
slouken@123
   420
#endif
slouken@123
   421
	} else {
slouken@123
   422
		/* Set 24-bit RGB output */
slouken@123
   423
		cinfo.out_color_space = JCS_RGB;
slouken@123
   424
		cinfo.quantize_colors = FALSE;
slouken@0
   425
#ifdef FAST_JPEG
slouken@123
   426
		cinfo.scale_num   = 1;
slouken@123
   427
		cinfo.scale_denom = 1;
slouken@123
   428
		cinfo.dct_method = JDCT_FASTEST;
slouken@123
   429
		cinfo.do_fancy_upsampling = FALSE;
slouken@0
   430
#endif
slouken@143
   431
		lib.jpeg_calc_output_dimensions(&cinfo);
slouken@0
   432
slouken@123
   433
		/* Allocate an output surface to hold the image */
slouken@123
   434
		surface = SDL_AllocSurface(SDL_SWSURFACE,
slouken@123
   435
		        cinfo.output_width, cinfo.output_height, 24,
slouken@0
   436
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@123
   437
		                   0x0000FF, 0x00FF00, 0xFF0000,
slouken@0
   438
#else
slouken@123
   439
		                   0xFF0000, 0x00FF00, 0x0000FF,
slouken@0
   440
#endif
slouken@123
   441
		                   0);
slouken@123
   442
	}
slouken@123
   443
slouken@0
   444
	if ( surface == NULL ) {
slouken@143
   445
		lib.jpeg_destroy_decompress(&cinfo);
slouken@236
   446
		SDL_RWseek(src, start, RW_SEEK_SET);
slouken@0
   447
		IMG_SetError("Out of memory");
slouken@118
   448
		return NULL;
slouken@0
   449
	}
slouken@0
   450
slouken@0
   451
	/* Decompress the image */
slouken@143
   452
	lib.jpeg_start_decompress(&cinfo);
slouken@0
   453
	while ( cinfo.output_scanline < cinfo.output_height ) {
slouken@0
   454
		rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
slouken@0
   455
		                    cinfo.output_scanline * surface->pitch;
slouken@143
   456
		lib.jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
slouken@0
   457
	}
slouken@143
   458
	lib.jpeg_finish_decompress(&cinfo);
slouken@143
   459
	lib.jpeg_destroy_decompress(&cinfo);
slouken@143
   460
slouken@0
   461
	return(surface);
slouken@0
   462
}
slouken@0
   463
slouken@0
   464
#else
slouken@0
   465
slouken@205
   466
int IMG_InitJPG()
slouken@205
   467
{
slouken@205
   468
	IMG_SetError("JPEG images are not supported");
slouken@205
   469
	return(-1);
slouken@205
   470
}
slouken@205
   471
slouken@205
   472
void IMG_QuitJPG()
slouken@205
   473
{
slouken@205
   474
}
slouken@205
   475
slouken@0
   476
/* See if an image is contained in a data source */
slouken@0
   477
int IMG_isJPG(SDL_RWops *src)
slouken@0
   478
{
slouken@0
   479
	return(0);
slouken@0
   480
}
slouken@0
   481
slouken@0
   482
/* Load a JPEG type image from an SDL datasource */
slouken@0
   483
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
slouken@0
   484
{
slouken@0
   485
	return(NULL);
slouken@0
   486
}
slouken@0
   487
slouken@0
   488
#endif /* LOAD_JPG */
slouken@237
   489
slouken@237
   490
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */