Skip to content

Latest commit

 

History

History
276 lines (235 loc) · 7.62 KB

IMG_jpg.c

File metadata and controls

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