Skip to content

Latest commit

 

History

History
599 lines (516 loc) · 19.3 KB

IMG_jpg.c

File metadata and controls

599 lines (516 loc) · 19.3 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Aug 10, 2000
Aug 10, 2000
20
21
22
23
24
*/
/* This is a JPEG image file loading framework */
#include <stdio.h>
Dec 29, 2001
Dec 29, 2001
25
#include <setjmp.h>
Aug 10, 2000
Aug 10, 2000
26
27
28
#include "SDL_image.h"
Sep 18, 2017
Sep 18, 2017
29
30
#if !(defined(__APPLE__) || defined(SDL_IMAGE_USE_WIC_BACKEND)) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
Aug 10, 2000
Aug 10, 2000
31
32
#ifdef LOAD_JPG
Sep 12, 2017
Sep 12, 2017
33
34
#define USE_JPEGLIB
Aug 10, 2000
Aug 10, 2000
35
36
#include <jpeglib.h>
Dec 31, 2011
Dec 31, 2011
37
#ifdef JPEG_TRUE /* MinGW version of jpeg-8.x renamed TRUE to JPEG_TRUE etc. */
May 22, 2013
May 22, 2013
38
39
40
typedef JPEG_boolean boolean;
#define TRUE JPEG_TRUE
#define FALSE JPEG_FALSE
Apr 7, 2011
Apr 7, 2011
41
42
#endif
Aug 10, 2000
Aug 10, 2000
43
44
45
/* Define this for fast loading and not as good image quality */
/*#define FAST_JPEG*/
Apr 20, 2006
Apr 20, 2006
46
47
48
/* Define this for quicker (but less perfect) JPEG identification */
#define FAST_IS_JPEG
May 12, 2006
May 12, 2006
49
static struct {
May 22, 2013
May 22, 2013
50
51
52
53
54
55
56
57
58
59
int loaded;
void *handle;
void (*jpeg_calc_output_dimensions) (j_decompress_ptr cinfo);
void (*jpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
void (*jpeg_destroy_decompress) (j_decompress_ptr cinfo);
boolean (*jpeg_finish_decompress) (j_decompress_ptr cinfo);
int (*jpeg_read_header) (j_decompress_ptr cinfo, boolean require_image);
JDIMENSION (*jpeg_read_scanlines) (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines);
boolean (*jpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
boolean (*jpeg_start_decompress) (j_decompress_ptr cinfo);
Sep 12, 2017
Sep 12, 2017
60
61
62
63
64
65
66
void (*jpeg_CreateCompress) (j_compress_ptr cinfo, int version, size_t structsize);
void (*jpeg_start_compress) (j_compress_ptr cinfo, boolean write_all_tables);
void (*jpeg_set_quality) (j_compress_ptr cinfo, int quality, boolean force_baseline);
void (*jpeg_set_defaults) (j_compress_ptr cinfo);
JDIMENSION (*jpeg_write_scanlines) (j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines);
void (*jpeg_finish_compress) (j_compress_ptr cinfo);
void (*jpeg_destroy_compress) (j_compress_ptr cinfo);
May 22, 2013
May 22, 2013
67
struct jpeg_error_mgr * (*jpeg_std_error) (struct jpeg_error_mgr * err);
May 12, 2006
May 12, 2006
68
69
70
} lib;
#ifdef LOAD_JPG_DYNAMIC
Oct 22, 2017
Oct 22, 2017
71
72
73
74
75
76
77
78
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = (SIG) SDL_LoadFunction(lib.handle, #FUNC); \
if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = FUNC;
#endif
Sep 26, 2009
Sep 26, 2009
79
int IMG_InitJPG()
May 12, 2006
May 12, 2006
80
{
May 22, 2013
May 22, 2013
81
if ( lib.loaded == 0 ) {
Oct 22, 2017
Oct 22, 2017
82
#ifdef LOAD_JPG_DYNAMIC
May 22, 2013
May 22, 2013
83
84
85
86
lib.handle = SDL_LoadObject(LOAD_JPG_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
Oct 22, 2017
Oct 22, 2017
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#endif
FUNCTION_LOADER(jpeg_calc_output_dimensions, void (*) (j_decompress_ptr cinfo))
FUNCTION_LOADER(jpeg_CreateDecompress, void (*) (j_decompress_ptr cinfo, int version, size_t structsize))
FUNCTION_LOADER(jpeg_destroy_decompress, void (*) (j_decompress_ptr cinfo))
FUNCTION_LOADER(jpeg_finish_decompress, boolean (*) (j_decompress_ptr cinfo))
FUNCTION_LOADER(jpeg_read_header, int (*) (j_decompress_ptr cinfo, boolean require_image))
FUNCTION_LOADER(jpeg_read_scanlines, JDIMENSION (*) (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines))
FUNCTION_LOADER(jpeg_resync_to_restart, boolean (*) (j_decompress_ptr cinfo, int desired))
FUNCTION_LOADER(jpeg_start_decompress, boolean (*) (j_decompress_ptr cinfo))
FUNCTION_LOADER(jpeg_CreateCompress, void (*) (j_compress_ptr cinfo, int version, size_t structsize))
FUNCTION_LOADER(jpeg_start_compress, void (*) (j_compress_ptr cinfo, boolean write_all_tables))
FUNCTION_LOADER(jpeg_set_quality, void (*) (j_compress_ptr cinfo, int quality, boolean force_baseline))
FUNCTION_LOADER(jpeg_set_defaults, void (*) (j_compress_ptr cinfo))
FUNCTION_LOADER(jpeg_write_scanlines, JDIMENSION (*) (j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines))
FUNCTION_LOADER(jpeg_finish_compress, void (*) (j_compress_ptr cinfo))
FUNCTION_LOADER(jpeg_destroy_compress, void (*) (j_compress_ptr cinfo))
FUNCTION_LOADER(jpeg_std_error, struct jpeg_error_mgr * (*) (struct jpeg_error_mgr * err))
May 22, 2013
May 22, 2013
104
105
106
107
}
++lib.loaded;
return 0;
May 12, 2006
May 12, 2006
108
}
Sep 26, 2009
Sep 26, 2009
109
void IMG_QuitJPG()
May 12, 2006
May 12, 2006
110
{
May 22, 2013
May 22, 2013
111
112
113
114
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
Oct 22, 2017
Oct 22, 2017
115
#ifdef LOAD_JPG_DYNAMIC
May 22, 2013
May 22, 2013
116
SDL_UnloadObject(lib.handle);
Oct 22, 2017
Oct 22, 2017
117
#endif
May 22, 2013
May 22, 2013
118
119
}
--lib.loaded;
May 12, 2006
May 12, 2006
120
121
}
Aug 10, 2000
Aug 10, 2000
122
123
124
/* See if an image is contained in a data source */
int IMG_isJPG(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
Sint64 start;
int is_JPG;
int in_scan;
Uint8 magic[4];
/* This detection code is by Steaphan Greene <stea@cs.binghamton.edu> */
/* Blame me, not Sam, if this doesn't work right. */
/* And don't forget to report the problem to the the sdl list too! */
if ( !src )
return 0;
start = SDL_RWtell(src);
is_JPG = 0;
in_scan = 0;
if ( SDL_RWread(src, magic, 2, 1) ) {
if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
is_JPG = 1;
while (is_JPG == 1) {
if(SDL_RWread(src, magic, 1, 2) != 2) {
is_JPG = 0;
} else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
is_JPG = 0;
} else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
/* Extra padding in JPEG (legal) */
/* or this is data and we are scanning */
SDL_RWseek(src, -1, RW_SEEK_CUR);
} else if(magic[1] == 0xD9) {
/* Got to end of good JPEG */
break;
} else if( (in_scan == 1) && (magic[1] == 0x00) ) {
/* This is an encoded 0xFF within the data */
} else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
/* These have nothing else */
} else if(SDL_RWread(src, magic+2, 1, 2) != 2) {
is_JPG = 0;
} else {
/* Yes, it's big-endian */
Sint64 innerStart;
Uint32 size;
Sint64 end;
innerStart = SDL_RWtell(src);
size = (magic[2] << 8) + magic[3];
end = SDL_RWseek(src, size-2, RW_SEEK_CUR);
if ( end != innerStart + size - 2 ) is_JPG = 0;
if ( magic[1] == 0xDA ) {
/* Now comes the actual JPEG meat */
#ifdef FAST_IS_JPEG
/* Ok, I'm convinced. It is a JPEG. */
break;
Apr 20, 2006
Apr 20, 2006
174
#else
May 22, 2013
May 22, 2013
175
176
/* I'm not convinced. Prove it! */
in_scan = 1;
Apr 20, 2006
Apr 20, 2006
177
#endif
May 22, 2013
May 22, 2013
178
179
180
181
182
183
184
}
}
}
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_JPG);
Aug 10, 2000
Aug 10, 2000
185
186
}
May 22, 2013
May 22, 2013
187
#define INPUT_BUFFER_SIZE 4096
Aug 10, 2000
Aug 10, 2000
188
typedef struct {
May 22, 2013
May 22, 2013
189
struct jpeg_source_mgr pub;
Aug 10, 2000
Aug 10, 2000
190
May 22, 2013
May 22, 2013
191
192
SDL_RWops *ctx;
Uint8 buffer[INPUT_BUFFER_SIZE];
Aug 10, 2000
Aug 10, 2000
193
194
195
196
197
198
} my_source_mgr;
/*
* Initialize source --- called by jpeg_read_header
* before any data is actually read.
*/
Mar 18, 2001
Mar 18, 2001
199
static void init_source (j_decompress_ptr cinfo)
Aug 10, 2000
Aug 10, 2000
200
{
May 22, 2013
May 22, 2013
201
/* We don't actually need to do anything */
Mar 5, 2020
Mar 5, 2020
202
(void)cinfo;
May 22, 2013
May 22, 2013
203
return;
Aug 10, 2000
Aug 10, 2000
204
205
206
207
208
}
/*
* Fill the input buffer --- called whenever buffer is emptied.
*/
Jan 2, 2012
Jan 2, 2012
209
static boolean fill_input_buffer (j_decompress_ptr cinfo)
Aug 10, 2000
Aug 10, 2000
210
{
May 22, 2013
May 22, 2013
211
212
213
my_source_mgr * src = (my_source_mgr *) cinfo->src;
int nbytes;
Oct 22, 2017
Oct 22, 2017
214
nbytes = (int)SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
May 22, 2013
May 22, 2013
215
216
217
218
219
220
221
222
223
224
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;
Aug 10, 2000
Aug 10, 2000
225
226
227
228
229
230
231
232
233
234
235
236
237
238
}
/*
* 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
239
static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
Aug 10, 2000
Aug 10, 2000
240
{
May 22, 2013
May 22, 2013
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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;
}
Aug 10, 2000
Aug 10, 2000
258
259
260
261
262
263
}
/*
* Terminate source --- called by jpeg_finish_decompress
* after all data has been read.
*/
Mar 18, 2001
Mar 18, 2001
264
static void term_source (j_decompress_ptr cinfo)
Aug 10, 2000
Aug 10, 2000
265
{
May 22, 2013
May 22, 2013
266
/* We don't actually need to do anything */
Mar 5, 2020
Mar 5, 2020
267
(void)cinfo;
May 22, 2013
May 22, 2013
268
return;
Aug 10, 2000
Aug 10, 2000
269
270
271
272
273
274
275
}
/*
* 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
276
static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
Aug 10, 2000
Aug 10, 2000
277
278
279
280
281
282
283
284
285
286
{
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.
*/
May 22, 2013
May 22, 2013
287
if (cinfo->src == NULL) { /* first time for this JPEG object? */
Aug 10, 2000
Aug 10, 2000
288
289
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
May 22, 2013
May 22, 2013
290
sizeof(my_source_mgr));
Aug 10, 2000
Aug 10, 2000
291
292
293
294
src = (my_source_mgr *) cinfo->src;
}
src = (my_source_mgr *) cinfo->src;
Mar 18, 2001
Mar 18, 2001
295
296
297
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
May 12, 2006
May 12, 2006
298
src->pub.resync_to_restart = lib.jpeg_resync_to_restart; /* use default method */
Mar 18, 2001
Mar 18, 2001
299
src->pub.term_source = term_source;
Aug 10, 2000
Aug 10, 2000
300
301
302
303
304
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
305
struct my_error_mgr {
May 22, 2013
May 22, 2013
306
307
struct jpeg_error_mgr errmgr;
jmp_buf escape;
Dec 29, 2001
Dec 29, 2001
308
309
310
311
};
static void my_error_exit(j_common_ptr cinfo)
{
May 22, 2013
May 22, 2013
312
313
struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
longjmp(err->escape, 1);
Dec 29, 2001
Dec 29, 2001
314
315
316
317
}
static void output_no_message(j_common_ptr cinfo)
{
May 22, 2013
May 22, 2013
318
/* do nothing */
Mar 5, 2020
Mar 5, 2020
319
(void)cinfo;
Dec 29, 2001
Dec 29, 2001
320
321
}
Aug 10, 2000
Aug 10, 2000
322
323
324
/* Load a JPEG type image from an SDL datasource */
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
325
326
327
328
329
330
331
332
333
334
335
336
Sint64 start;
struct jpeg_decompress_struct cinfo;
JSAMPROW rowptr[1];
SDL_Surface *volatile surface = NULL;
struct my_error_mgr jerr;
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);
Feb 15, 2016
Feb 15, 2016
337
if ( (IMG_Init(IMG_INIT_JPG) & IMG_INIT_JPG) == 0 ) {
May 22, 2013
May 22, 2013
338
339
340
341
342
343
344
return NULL;
}
/* Create a decompression structure and load the JPEG header */
cinfo.err = lib.jpeg_std_error(&jerr.errmgr);
jerr.errmgr.error_exit = my_error_exit;
jerr.errmgr.output_message = output_no_message;
Mar 5, 2020
Mar 5, 2020
345
346
347
#ifdef _MSC_VER
#pragma warning(disable:4611) /* warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable */
#endif
May 22, 2013
May 22, 2013
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
if(setjmp(jerr.escape)) {
/* If we get here, libjpeg found an error */
lib.jpeg_destroy_decompress(&cinfo);
if ( surface != NULL ) {
SDL_FreeSurface(surface);
}
SDL_RWseek(src, start, RW_SEEK_SET);
IMG_SetError("JPEG loading error");
return NULL;
}
lib.jpeg_create_decompress(&cinfo);
jpeg_SDL_RW_src(&cinfo, src);
lib.jpeg_read_header(&cinfo, TRUE);
if(cinfo.num_components == 4) {
/* Set 32-bit Raw output */
cinfo.out_color_space = JCS_CMYK;
cinfo.quantize_colors = FALSE;
lib.jpeg_calc_output_dimensions(&cinfo);
/* Allocate an output surface to hold the image */
surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
cinfo.output_width, cinfo.output_height, 32,
Apr 20, 2006
Apr 20, 2006
372
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 22, 2013
May 22, 2013
373
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
Apr 20, 2006
Apr 20, 2006
374
#else
May 22, 2013
May 22, 2013
375
0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
Apr 20, 2006
Apr 20, 2006
376
#endif
May 22, 2013
May 22, 2013
377
378
379
380
} else {
/* Set 24-bit RGB output */
cinfo.out_color_space = JCS_RGB;
cinfo.quantize_colors = FALSE;
Aug 10, 2000
Aug 10, 2000
381
#ifdef FAST_JPEG
May 22, 2013
May 22, 2013
382
383
384
385
cinfo.scale_num = 1;
cinfo.scale_denom = 1;
cinfo.dct_method = JDCT_FASTEST;
cinfo.do_fancy_upsampling = FALSE;
Aug 10, 2000
Aug 10, 2000
386
#endif
May 22, 2013
May 22, 2013
387
lib.jpeg_calc_output_dimensions(&cinfo);
Aug 10, 2000
Aug 10, 2000
388
May 22, 2013
May 22, 2013
389
390
391
/* Allocate an output surface to hold the image */
surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
cinfo.output_width, cinfo.output_height, 24,
Aug 10, 2000
Aug 10, 2000
392
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 22, 2013
May 22, 2013
393
0x0000FF, 0x00FF00, 0xFF0000,
Aug 10, 2000
Aug 10, 2000
394
#else
May 22, 2013
May 22, 2013
395
0xFF0000, 0x00FF00, 0x0000FF,
Aug 10, 2000
Aug 10, 2000
396
#endif
May 22, 2013
May 22, 2013
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
0);
}
if ( surface == NULL ) {
lib.jpeg_destroy_decompress(&cinfo);
SDL_RWseek(src, start, RW_SEEK_SET);
IMG_SetError("Out of memory");
return NULL;
}
/* Decompress the image */
lib.jpeg_start_decompress(&cinfo);
while ( cinfo.output_scanline < cinfo.output_height ) {
rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
cinfo.output_scanline * surface->pitch;
lib.jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
}
lib.jpeg_finish_decompress(&cinfo);
lib.jpeg_destroy_decompress(&cinfo);
return(surface);
Aug 10, 2000
Aug 10, 2000
418
419
}
Sep 12, 2017
Sep 12, 2017
420
421
422
423
424
425
426
427
428
429
430
#define OUTPUT_BUFFER_SIZE 4096
typedef struct {
struct jpeg_destination_mgr pub;
SDL_RWops *ctx;
Uint8 buffer[OUTPUT_BUFFER_SIZE];
} my_destination_mgr;
static void init_destination(j_compress_ptr cinfo)
{
/* We don't actually need to do anything */
Mar 5, 2020
Mar 5, 2020
431
(void)cinfo;
Sep 12, 2017
Sep 12, 2017
432
433
434
435
436
437
438
439
440
441
442
443
return;
}
static boolean empty_output_buffer(j_compress_ptr cinfo)
{
my_destination_mgr * dest = (my_destination_mgr *)cinfo->dest;
/* In typical applications, it should write out the *entire* buffer */
SDL_RWwrite(dest->ctx, dest->buffer, 1, OUTPUT_BUFFER_SIZE);
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUFFER_SIZE;
Oct 22, 2017
Oct 22, 2017
444
return TRUE;
Sep 12, 2017
Sep 12, 2017
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
}
static void term_destination(j_compress_ptr cinfo)
{
my_destination_mgr * dest = (my_destination_mgr *)cinfo->dest;
/* In most applications, this must flush any data remaining in the buffer */
SDL_RWwrite(dest->ctx, dest->buffer, 1, OUTPUT_BUFFER_SIZE - dest->pub.free_in_buffer);
}
static void jpeg_SDL_RW_dest(j_compress_ptr cinfo, SDL_RWops *ctx)
{
my_destination_mgr *dest;
if (cinfo->dest == NULL) {
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
sizeof(my_destination_mgr));
dest = (my_destination_mgr *)cinfo->dest;
}
dest = (my_destination_mgr *)cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
dest->ctx = ctx;
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUFFER_SIZE;
}
Sep 12, 2017
Sep 12, 2017
475
static int IMG_SaveJPG_RW_jpeglib(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality)
Sep 12, 2017
Sep 12, 2017
476
{
Oct 6, 2017
Oct 6, 2017
477
478
479
480
481
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
static const Uint32 jpg_format = SDL_PIXELFORMAT_RGB24;
#else
static const Uint32 jpg_format = SDL_PIXELFORMAT_BGR24;
#endif
Sep 12, 2017
Sep 12, 2017
482
483
484
485
struct jpeg_compress_struct cinfo;
struct my_error_mgr jerr;
JSAMPROW row_pointer[1];
SDL_Surface* jpeg_surface = surface;
Sep 12, 2017
Sep 12, 2017
486
int result = -1;
Sep 12, 2017
Sep 12, 2017
487
488
if (!dst) {
Jul 10, 2019
Jul 10, 2019
489
IMG_SetError("Passed NULL dst");
Sep 12, 2017
Sep 12, 2017
490
goto done;
Sep 12, 2017
Sep 12, 2017
491
492
493
}
if (!IMG_Init(IMG_INIT_JPG)) {
Sep 12, 2017
Sep 12, 2017
494
goto done;
Sep 12, 2017
Sep 12, 2017
495
496
497
498
499
500
}
/* Convert surface to format we can save */
if (surface->format->format != jpg_format) {
jpeg_surface = SDL_ConvertSurfaceFormat(surface, jpg_format, 0);
if (!jpeg_surface) {
Sep 12, 2017
Sep 12, 2017
501
goto done;
Sep 12, 2017
Sep 12, 2017
502
503
504
505
506
507
508
509
510
511
512
}
}
/* Create a decompression structure and load the JPEG header */
cinfo.err = lib.jpeg_std_error(&jerr.errmgr);
jerr.errmgr.error_exit = my_error_exit;
jerr.errmgr.output_message = output_no_message;
lib.jpeg_create_compress(&cinfo);
jpeg_SDL_RW_dest(&cinfo, dst);
Nov 19, 2017
Nov 19, 2017
513
514
cinfo.image_width = jpeg_surface->w;
cinfo.image_height = jpeg_surface->h;
Sep 12, 2017
Sep 12, 2017
515
516
517
518
cinfo.in_color_space = JCS_RGB;
cinfo.input_components = 3;
lib.jpeg_set_defaults(&cinfo);
Oct 22, 2017
Oct 22, 2017
519
520
lib.jpeg_set_quality(&cinfo, quality, TRUE);
lib.jpeg_start_compress(&cinfo, TRUE);
Sep 12, 2017
Sep 12, 2017
521
522
while (cinfo.next_scanline < cinfo.image_height) {
Nov 19, 2017
Nov 19, 2017
523
int offset = cinfo.next_scanline * jpeg_surface->pitch;
Sep 12, 2017
Sep 12, 2017
524
525
526
527
528
529
530
531
532
533
534
row_pointer[0] = ((Uint8*)jpeg_surface->pixels) + offset;
lib.jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
lib.jpeg_finish_compress(&cinfo);
lib.jpeg_destroy_compress(&cinfo);
if (jpeg_surface != surface) {
SDL_FreeSurface(jpeg_surface);
}
Sep 12, 2017
Sep 12, 2017
535
result = 0;
Sep 12, 2017
Sep 12, 2017
536
537
538
539
540
541
542
543
done:
if (freedst) {
SDL_RWclose(dst);
}
return result;
}
Aug 10, 2000
Aug 10, 2000
544
#else
Oct 9, 2019
Oct 9, 2019
545
546
547
#if _MSC_VER >= 1300
#pragma warning(disable : 4100) /* warning C4100: 'op' : unreferenced formal parameter */
#endif
Aug 10, 2000
Aug 10, 2000
548
Sep 26, 2009
Sep 26, 2009
549
550
int IMG_InitJPG()
{
May 22, 2013
May 22, 2013
551
552
IMG_SetError("JPEG images are not supported");
return(-1);
Sep 26, 2009
Sep 26, 2009
553
554
555
556
557
558
}
void IMG_QuitJPG()
{
}
Aug 10, 2000
Aug 10, 2000
559
560
561
/* See if an image is contained in a data source */
int IMG_isJPG(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
562
return(0);
Aug 10, 2000
Aug 10, 2000
563
564
565
566
567
}
/* Load a JPEG type image from an SDL datasource */
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
568
return(NULL);
Aug 10, 2000
Aug 10, 2000
569
570
}
Sep 12, 2017
Sep 12, 2017
571
572
573
574
575
576
577
578
579
#endif /* LOAD_JPG */
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */
/* We'll always have JPG save support */
#define SAVE_JPG
#ifdef SAVE_JPG
Sep 12, 2017
Sep 12, 2017
580
581
int IMG_SaveJPG(SDL_Surface *surface, const char *file, int quality)
{
Sep 12, 2017
Sep 12, 2017
582
583
584
585
586
587
SDL_RWops *dst = SDL_RWFromFile(file, "wb");
if (dst) {
return IMG_SaveJPG_RW(surface, dst, 1, quality);
} else {
return -1;
}
Sep 12, 2017
Sep 12, 2017
588
589
590
591
}
int IMG_SaveJPG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality)
{
Sep 12, 2017
Sep 12, 2017
592
593
594
595
596
#ifdef USE_JPEGLIB
return IMG_SaveJPG_RW_jpeglib(surface, dst, freedst, quality);
#else
return IMG_SetError("SDL_image not built with jpeglib, saving not supported");
#endif
Sep 12, 2017
Sep 12, 2017
597
598
}
Sep 12, 2017
Sep 12, 2017
599
#endif /* SAVE_JPG */