1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
112
113
114
115
116
117
118
119
120
121
122
123
124
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/graphics/jpegimages.h"
#include "gate/results.h"
#include "gate/debugging.h"
#if defined(GATE_EXTLIB_LIBJPEG)
# define GATE_GRAPHICS_JPEG_USE_LIBJPEG
#else
# define GATE_GRAPHICS_JPEG_NO_IMPL
#endif
#if defined(GATE_GRAPHICS_JPEG_USE_LIBJPEG)
#include <stdio.h>
#include "jpeglib.h"
#include "jerror.h"
#include <setjmp.h>
typedef struct gate_jpeg_param_class
{
gate_stream_t* stream;
unsigned char buffer[GATE_MAX_STACK_COPYBUFFER_LENGTH];
gate_result_t result;
jmp_buf emergency_exit;
} gate_jpeg_param_t;
typedef struct gate_jpeg_error_mgr_class
{
struct jpeg_error_mgr error_mgr;
gate_jpeg_param_t* ptr_param;
} gate_jpeg_error_mgr_t;
static void gate_jpeg_error_exit(j_common_ptr cinfo)
{
/* just do nothing (instead of terminating the process which is the default) */
gate_jpeg_error_mgr_t* ptr_err;
gate_jpeg_param_t* param;
GATE_DEBUG_ASSERT(cinfo != NULL);
ptr_err = (gate_jpeg_error_mgr_t*)cinfo->err;
GATE_DEBUG_ASSERT(ptr_err != NULL);
param = ptr_err->ptr_param;
GATE_DEBUG_ASSERT(param != NULL);
GATE_DEBUG_TRACE_MSG_VALUE("jpeg_error", cinfo->err->msg_code);
switch (cinfo->err->msg_code)
{
case JERR_UNKNOWN_MARKER:
{
GATE_DEBUG_TRACE("ignore jpeg_error: unknown marker");
/* ignore, do nothing */
break;
}
case JERR_SOI_DUPLICATE:
{
GATE_DEBUG_TRACE("ignore jpeg_error: soi duplicate");
/* ignore, do nothing*/
break;
}
case JERR_BAD_LIB_VERSION:
{
GATE_DEBUG_TRACE_MSG_VALUE("jpeg_error: bad lib version", JPEG_LIB_VERSION);
/* no break ! */
}
default:
{
param->result = GATE_RESULT_FAILED;
longjmp(param->emergency_exit, 1);
break;<--- Consecutive return, break, continue, goto or throw statements are unnecessary. [+]Consecutive return, break, continue, goto or throw statements are unnecessary. The second statement can never be executed, and so should be removed.
}
}
}
static void gate_jpeg_init_source(struct jpeg_decompress_struct* decompress)
{
gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)decompress->client_data;
decompress->src->next_input_byte = user_param->buffer;
decompress->src->bytes_in_buffer = 0;
}
static boolean gate_jpeg_fill_input_buffer(struct jpeg_decompress_struct* decompress)
{
gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)decompress->client_data;
gate_size_t bufferused;
gate_result_t result = gate_stream_read_block(user_param->stream, (char*)&user_param->buffer[0], sizeof(user_param->buffer), &bufferused);
if (GATE_FAILED(result))
{
user_param->result = result;
return FALSE;
}
else
{
decompress->src->next_input_byte = user_param->buffer;
decompress->src->bytes_in_buffer = bufferused;
return TRUE;
}
}
static void gate_jpeg_skip_input_data(struct jpeg_decompress_struct* decompress, long num_bytes)
{
size_t nbytes;<--- The scope of the variable 'nbytes' can be reduced. [+]The scope of the variable 'nbytes' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (num_bytes > 0)
{
nbytes = (size_t)num_bytes;
while (nbytes > decompress->src->bytes_in_buffer)
{
nbytes -= decompress->src->bytes_in_buffer;
if (!(*decompress->src->fill_input_buffer)(decompress))
{
/* error or EOF */
decompress->src->bytes_in_buffer = 0;
return;
}
}
decompress->src->next_input_byte += nbytes;
decompress->src->bytes_in_buffer -= nbytes;
}
}
static boolean gate_jpeg_resync_to_restart(struct jpeg_decompress_struct* decompress, int desired)
{
GATE_UNUSED_ARG(decompress);
GATE_UNUSED_ARG(desired);
return TRUE;
}
static void gate_jpeg_term_source(struct jpeg_decompress_struct* decompress)
{
GATE_UNUSED_ARG(decompress);
}
static boolean gate_jpeg_marker_parser(j_decompress_ptr cinfo)
{
GATE_UNUSED_ARG(cinfo);
/* additional markers are just "accepted" */
return TRUE;
}
gate_result_t gate_jpegimage_load_data(struct jpeg_decompress_struct* decompress, gate_jpeg_param_t* user_param, gate_rasterimage_t* image)
{
gate_result_t ret = GATE_RESULT_FAILED;
static const JDIMENSION jpeg_lines_to_read = 1;
char buffer[4096 * 3];
char* ptrbuffer = &buffer[0];
int jpeg_result;<--- The scope of the variable 'jpeg_result' can be reduced. [+]The scope of the variable 'jpeg_result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_uint32_t scanline_length;
char* ptrbytes;
gate_color_t* ptrpixels;<--- The scope of the variable 'ptrpixels' can be reduced. [+]The scope of the variable 'ptrpixels' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_uint32_t x;
gate_rasterimage_t raster = GATE_INIT_EMPTY;
do
{
jpeg_result = jpeg_read_header(decompress, TRUE);
if (jpeg_result != JPEG_HEADER_OK)
{
ret = GATE_RESULT_INVALIDHEADER;
break;
}
if (FALSE == jpeg_start_decompress(decompress))
{
ret = GATE_RESULT_FAILED;
break;
}
if (NULL == gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_DEFAULT,
(gate_uint32_t)decompress->output_width,
(gate_uint32_t)decompress->output_height, NULL))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ptrpixels = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0);
scanline_length = (gate_uint32_t)decompress->output_components * raster.width;
if (scanline_length > sizeof(buffer))
{
ptrbuffer = (char*)gate_mem_alloc(scanline_length);
if (ptrbuffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
while (decompress->output_scanline < decompress->output_height)
{
if (jpeg_lines_to_read != jpeg_read_scanlines(decompress, (JSAMPARRAY)&ptrbuffer, jpeg_lines_to_read))
{
if (GATE_SUCCEEDED(user_param->result))
{
user_param->result = GATE_RESULT_INVALIDINPUT;
}
break;
}
ptrbytes = ptrbuffer;
switch (decompress->output_components)
{
case 1:
{
for (x = 0; x != raster.width; ++x)
{
ptrpixels->r = *ptrbytes;
ptrpixels->g = *ptrbytes;
ptrpixels->b = *ptrbytes;
ptrpixels->a = 255;
++ptrbytes;
++ptrpixels;
}
break;
}
case 3:
{
for (x = 0; x != raster.width; ++x)
{
ptrpixels->r = *(ptrbytes++);
ptrpixels->g = *(ptrbytes++);
ptrpixels->b = *(ptrbytes++);
ptrpixels->a = 255;
++ptrpixels;
}
break;
}
default:
{
decompress->output_scanline = raster.height;
user_param->result = GATE_RESULT_NOTSUPPORTED;
break;
}
}
}
jpeg_finish_decompress(decompress);
ret = user_param->result;
} while (0);
if (GATE_SUCCEEDED(ret))
{
gate_mem_copy(image, &raster, sizeof(raster));
gate_mem_clear(&raster, sizeof(raster));
}
gate_rasterimage_release(&raster);
if ((ptrbuffer != &buffer[0]) && (ptrbuffer != NULL))
{
gate_mem_dealloc(ptrbuffer);
}
return ret;
}
gate_result_t gate_jpegimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags)
{
gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
gate_jpeg_error_mgr_t error_mgr = GATE_INIT_EMPTY;
struct jpeg_source_mgr source_mgr = GATE_INIT_EMPTY;
struct jpeg_decompress_struct decompress = GATE_INIT_EMPTY;
gate_jpeg_param_t user_param = GATE_INIT_EMPTY;
user_param.stream = srcstream;
user_param.result = GATE_RESULT_OK;
decompress.err = jpeg_std_error(&error_mgr.error_mgr);
error_mgr.error_mgr.error_exit = &gate_jpeg_error_exit;
error_mgr.ptr_param = &user_param;
jpeg_create_decompress(&decompress);
source_mgr.fill_input_buffer = &gate_jpeg_fill_input_buffer;
source_mgr.init_source = &gate_jpeg_init_source;
source_mgr.resync_to_restart = &gate_jpeg_resync_to_restart;
source_mgr.skip_input_data = &gate_jpeg_skip_input_data;
source_mgr.term_source = &gate_jpeg_term_source;
source_mgr.bytes_in_buffer = 0;
source_mgr.next_input_byte = NULL;
decompress.src = &source_mgr;
decompress.client_data = &user_param;
jpeg_set_marker_processor(&decompress, JPEG_COM, &gate_jpeg_marker_parser);
jpeg_set_marker_processor(&decompress, JPEG_APP0 + 12, &gate_jpeg_marker_parser);
do
{
if (0 == setjmp(user_param.emergency_exit))
{
ret = gate_jpegimage_load_data(&decompress, &user_param, image);
}
else /* setjmp error case */
{
/* jpeg emergency exit point reached */
ret = GATE_RESULT_CRITICALERROR;
}
} while (0);
jpeg_destroy_decompress(&decompress);
return ret;
}
static void gate_jpeg_init_destination(struct jpeg_compress_struct* compress)
{
gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)compress->client_data;
compress->dest->free_in_buffer = sizeof(user_param->buffer);
compress->dest->next_output_byte = &user_param->buffer[0];
}
static boolean gate_jpeg_flush_output_buffer(struct jpeg_compress_struct* compress, gate_jpeg_param_t* user_param, gate_size_t bufferused)
{
gate_result_t result;
gate_size_t byteswritten;
if (bufferused != 0)
{
result = gate_stream_write_block(user_param->stream, (char const*)user_param->buffer, bufferused, &byteswritten);
if (GATE_FAILED(result))
{
user_param->result = result;
return FALSE;
}
}
gate_jpeg_init_destination(compress);
return TRUE;
}
static boolean gate_jpeg_empty_output_buffer(struct jpeg_compress_struct* compress)
{
gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)compress->client_data;
return gate_jpeg_flush_output_buffer(compress, user_param, sizeof(user_param->buffer));
}
static void gate_jpeg_term_destination(struct jpeg_compress_struct* compress)
{
gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)compress->client_data;
GATE_DEBUG_ASSERT(sizeof(user_param->buffer) >= compress->dest->free_in_buffer);
gate_jpeg_flush_output_buffer(compress, user_param, sizeof(user_param->buffer) - compress->dest->free_in_buffer);
}
static gate_result_t gate_jpegimage_save_data(gate_rasterimage_t const* image,
struct jpeg_destination_mgr* destination_mgr,
struct jpeg_compress_struct* compress,
gate_jpeg_param_t* user_param,
gate_uint8_t qualitypercent)
{
gate_result_t ret = GATE_RESULT_FAILED;
unsigned char buffer[4096 * 3];
unsigned char* ptrbuffer = &buffer[0];
gate_size_t required_buffer_size;
unsigned int y;
gate_uint8_t const* ptrpixels;
void(*line_writer)(gate_uint8_t*, gate_uint8_t const*, gate_size_t) = NULL;
do
{
switch (image->pixel_format)
{
case GATE_IMAGE_PIXELFORMAT_RGBA:
case GATE_IMAGE_PIXELFORMAT_RGB32: line_writer = &gate_color_convert_rgb_from_rgba_32; break;
case GATE_IMAGE_PIXELFORMAT_BGRA:
case GATE_IMAGE_PIXELFORMAT_BGR32: line_writer = &gate_color_convert_rgb_from_bgra_32; break;
case GATE_IMAGE_PIXELFORMAT_RGB24: line_writer = &gate_color_convert_rgb_from_rgb_24; break;
case GATE_IMAGE_PIXELFORMAT_BGR24: line_writer = &gate_color_convert_rgb_from_bgr_24; break;
case GATE_IMAGE_PIXELFORMAT_RGB555: line_writer = &gate_color_convert_rgb_from_rgb_15; break;
case GATE_IMAGE_PIXELFORMAT_RGB565: line_writer = &gate_color_convert_rgb_from_rgb_16; break;
case GATE_IMAGE_PIXELFORMAT_ARGB4: line_writer = &gate_color_convert_rgb_from_argb_16; break;
case GATE_IMAGE_PIXELFORMAT_YUV2: line_writer = &gate_color_convert_rgb_from_yuv2_16; break;
case GATE_IMAGE_PIXELFORMAT_PAL8: line_writer = &gate_color_convert_rgb_from_pal_8; break;
case GATE_IMAGE_PIXELFORMAT_GRAY8: line_writer = &gate_color_convert_rgb_from_gray_8; break;
default:
break;
}
if (NULL == line_writer)
{
ret = GATE_RESULT_NOTSUPPORTED;
break;
}
required_buffer_size = (gate_size_t)image->width * 3;
if (required_buffer_size > sizeof(buffer))
{
ptrbuffer = (unsigned char*)gate_mem_alloc(required_buffer_size);
if (ptrbuffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
jpeg_create_compress(compress);
compress->in_color_space = JCS_RGB;
compress->input_components = 3;
compress->image_width = (JDIMENSION)image->width;
compress->image_height = (JDIMENSION)image->height;
compress->dct_method = JDCT_FLOAT;
jpeg_set_defaults(compress);
compress->client_data = user_param;
compress->dest = destination_mgr;
jpeg_set_quality(compress, (int)((qualitypercent > 100) ? 100 : qualitypercent), TRUE);
jpeg_start_compress(compress, TRUE);
for (y = 0; y != image->height; ++y)
{
ptrpixels = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y);
line_writer(ptrbuffer, ptrpixels, image->width);
if (1 != jpeg_write_scanlines(compress, (JSAMPARRAY)&ptrbuffer, 1))
{
/* failed to write one line */
if (GATE_SUCCEEDED(user_param->result))
{
user_param->result = GATE_RESULT_INVALIDOUTPUT;
break;
}
}
}
jpeg_finish_compress(compress);
jpeg_destroy_compress(compress);
ret = user_param->result;
} while (0);
if ((ptrbuffer != &buffer[0]) && (ptrbuffer != NULL))
{
gate_mem_dealloc(ptrbuffer);
}
return ret;
}
gate_result_t gate_jpegimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags)
{
gate_result_t ret;
gate_jpeg_param_t user_param = GATE_INIT_EMPTY;
gate_jpeg_error_mgr_t error_mgr = GATE_INIT_EMPTY;
struct jpeg_destination_mgr destination_mgr = GATE_INIT_EMPTY;
struct jpeg_compress_struct compress = GATE_INIT_EMPTY;
const gate_uint16_t qa0to15 = (gate_uint16_t)GATE_IMAGE_FLAG_DECODE_QUALITY(flags);
const gate_uint8_t qualitypercent = (qa0to15 > 0)
? (gate_uint8_t)(qa0to15 * 100 / 15)
: 90;
user_param.result = GATE_RESULT_OK;
user_param.stream = deststream;
compress.err = jpeg_std_error(&error_mgr.error_mgr);
error_mgr.error_mgr.error_exit = &gate_jpeg_error_exit;
error_mgr.ptr_param = &user_param;
destination_mgr.empty_output_buffer = &gate_jpeg_empty_output_buffer;
destination_mgr.init_destination = &gate_jpeg_init_destination;
destination_mgr.term_destination = &gate_jpeg_term_destination;
destination_mgr.free_in_buffer = 0;
destination_mgr.next_output_byte = NULL;
do
{
if (0 == setjmp(user_param.emergency_exit))
{
ret = gate_jpegimage_save_data(image, &destination_mgr, &compress, &user_param, qualitypercent);
}
else /* setjmp error case */
{
/* jpeg emergency exit point reached */
ret = GATE_RESULT_CRITICALERROR;
}
} while (0);
return ret;
}
#endif /* GATE_GRAPHICS_JPEG_USE_LIBJPEG */
#if defined(GATE_GRAPHICS_JPEG_NO_IMPL)
gate_result_t gate_jpegimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags)
{
(void)srcstream;
(void)image;
(void)flags;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_jpegimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags)
{
(void)image;
(void)deststream;
(void)flags;
return GATE_RESULT_NOTIMPLEMENTED;
}
#endif
|