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