| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/graphics/jpegimages.h" | ||
| 30 | #include "gate/results.h" | ||
| 31 | #include "gate/debugging.h" | ||
| 32 | |||
| 33 | #if defined(GATE_EXTLIB_LIBJPEG) | ||
| 34 | # define GATE_GRAPHICS_JPEG_USE_LIBJPEG 1 | ||
| 35 | #else | ||
| 36 | # if defined(GATE_SYS_WIN) | ||
| 37 | # define GATE_GRAPHICS_JPEG_USE_OLE_IPICTURE 1 | ||
| 38 | # else | ||
| 39 | # define GATE_GRAPHICS_JPEG_NO_IMPL 1 | ||
| 40 | # endif | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #if defined(GATE_GRAPHICS_JPEG_USE_LIBJPEG) | ||
| 44 | |||
| 45 | #include <stdio.h> | ||
| 46 | #include "jpeglib.h" | ||
| 47 | #include "jerror.h" | ||
| 48 | #include <setjmp.h> | ||
| 49 | |||
| 50 | typedef struct gate_jpeg_param_class | ||
| 51 | { | ||
| 52 | gate_stream_t* stream; | ||
| 53 | unsigned char buffer[GATE_MAX_STACK_COPYBUFFER_LENGTH]; | ||
| 54 | gate_result_t result; | ||
| 55 | jmp_buf emergency_exit; | ||
| 56 | } gate_jpeg_param_t; | ||
| 57 | |||
| 58 | typedef struct gate_jpeg_error_mgr_class | ||
| 59 | { | ||
| 60 | struct jpeg_error_mgr error_mgr; | ||
| 61 | gate_jpeg_param_t* ptr_param; | ||
| 62 | } gate_jpeg_error_mgr_t; | ||
| 63 | |||
| 64 | ✗ | static void gate_jpeg_error_exit(j_common_ptr cinfo) | |
| 65 | { | ||
| 66 | /* just do nothing (instead of terminating the process which is the default) */ | ||
| 67 | gate_jpeg_error_mgr_t* ptr_err; | ||
| 68 | gate_jpeg_param_t* param; | ||
| 69 | |||
| 70 | ✗ | GATE_DEBUG_ASSERT(cinfo != NULL); | |
| 71 | ✗ | ptr_err = (gate_jpeg_error_mgr_t*)cinfo->err; | |
| 72 | ✗ | GATE_DEBUG_ASSERT(ptr_err != NULL); | |
| 73 | ✗ | param = ptr_err->ptr_param; | |
| 74 | ✗ | GATE_DEBUG_ASSERT(param != NULL); | |
| 75 | |||
| 76 | ✗ | GATE_DEBUG_TRACE_MSG_VALUE("jpeg_error", cinfo->err->msg_code); | |
| 77 | |||
| 78 | ✗ | switch (cinfo->err->msg_code) | |
| 79 | { | ||
| 80 | ✗ | case JERR_UNKNOWN_MARKER: | |
| 81 | { | ||
| 82 | ✗ | GATE_DEBUG_TRACE("ignore jpeg_error: unknown marker"); | |
| 83 | /* ignore, do nothing */ | ||
| 84 | ✗ | break; | |
| 85 | } | ||
| 86 | ✗ | case JERR_SOI_DUPLICATE: | |
| 87 | { | ||
| 88 | ✗ | GATE_DEBUG_TRACE("ignore jpeg_error: soi duplicate"); | |
| 89 | /* ignore, do nothing*/ | ||
| 90 | ✗ | break; | |
| 91 | } | ||
| 92 | ✗ | case JERR_BAD_LIB_VERSION: | |
| 93 | { | ||
| 94 | ✗ | GATE_DEBUG_TRACE_MSG_VALUE("jpeg_error: bad lib version", JPEG_LIB_VERSION); | |
| 95 | /* the program was compiled with a different ABI than we have now available */ | ||
| 96 | /* no break ! abort required */ | ||
| 97 | } | ||
| 98 | ✗ | default: | |
| 99 | { | ||
| 100 | ✗ | param->result = GATE_RESULT_FAILED; | |
| 101 | ✗ | longjmp(param->emergency_exit, 1); | |
| 102 | } | ||
| 103 | } | ||
| 104 | ✗ | } | |
| 105 | |||
| 106 | 1 | static void gate_jpeg_init_source(struct jpeg_decompress_struct* decompress) | |
| 107 | { | ||
| 108 | 1 | gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)decompress->client_data; | |
| 109 | 1 | decompress->src->next_input_byte = user_param->buffer; | |
| 110 | 1 | decompress->src->bytes_in_buffer = 0; | |
| 111 | 1 | } | |
| 112 | |||
| 113 | 1 | static boolean gate_jpeg_fill_input_buffer(struct jpeg_decompress_struct* decompress) | |
| 114 | { | ||
| 115 | 1 | gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)decompress->client_data; | |
| 116 | gate_size_t bufferused; | ||
| 117 | 1 | gate_result_t result = gate_stream_read_block(user_param->stream, (char*)&user_param->buffer[0], sizeof(user_param->buffer), &bufferused); | |
| 118 | |||
| 119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (GATE_FAILED(result)) |
| 120 | { | ||
| 121 | ✗ | user_param->result = result; | |
| 122 | ✗ | return FALSE; | |
| 123 | } | ||
| 124 | else | ||
| 125 | { | ||
| 126 | 1 | decompress->src->next_input_byte = user_param->buffer; | |
| 127 | 1 | decompress->src->bytes_in_buffer = bufferused; | |
| 128 | 1 | return TRUE; | |
| 129 | } | ||
| 130 | } | ||
| 131 | ✗ | static void gate_jpeg_skip_input_data(struct jpeg_decompress_struct* decompress, long num_bytes) | |
| 132 | { | ||
| 133 | ✗ | if (num_bytes > 0) | |
| 134 | { | ||
| 135 | ✗ | size_t nbytes = (size_t)num_bytes; | |
| 136 | ✗ | while (nbytes > decompress->src->bytes_in_buffer) | |
| 137 | { | ||
| 138 | ✗ | nbytes -= decompress->src->bytes_in_buffer; | |
| 139 | ✗ | if (!(*decompress->src->fill_input_buffer)(decompress)) | |
| 140 | { | ||
| 141 | /* error or EOF */ | ||
| 142 | ✗ | decompress->src->bytes_in_buffer = 0; | |
| 143 | ✗ | return; | |
| 144 | } | ||
| 145 | } | ||
| 146 | ✗ | decompress->src->next_input_byte += nbytes; | |
| 147 | ✗ | decompress->src->bytes_in_buffer -= nbytes; | |
| 148 | } | ||
| 149 | } | ||
| 150 | ✗ | static boolean gate_jpeg_resync_to_restart(struct jpeg_decompress_struct* decompress, int desired) | |
| 151 | { | ||
| 152 | GATE_UNUSED_ARG(decompress); | ||
| 153 | GATE_UNUSED_ARG(desired); | ||
| 154 | ✗ | return TRUE; | |
| 155 | } | ||
| 156 | 1 | static void gate_jpeg_term_source(struct jpeg_decompress_struct* decompress) | |
| 157 | { | ||
| 158 | GATE_UNUSED_ARG(decompress); | ||
| 159 | 1 | } | |
| 160 | |||
| 161 | ✗ | static boolean gate_jpeg_marker_parser(j_decompress_ptr cinfo) | |
| 162 | { | ||
| 163 | GATE_UNUSED_ARG(cinfo); | ||
| 164 | /* additional markers are just "accepted" */ | ||
| 165 | ✗ | return TRUE; | |
| 166 | } | ||
| 167 | |||
| 168 | 1 | gate_result_t gate_jpegimage_load_data(struct jpeg_decompress_struct* decompress, gate_jpeg_param_t* user_param, gate_rasterimage_t* image) | |
| 169 | { | ||
| 170 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 171 | char buffer[4096 * 3]; | ||
| 172 | 1 | char* ptrbuffer = &buffer[0]; | |
| 173 | 1 | gate_rasterimage_t raster = GATE_INIT_EMPTY; | |
| 174 | |||
| 175 | do | ||
| 176 | { | ||
| 177 | int jpeg_result; | ||
| 178 | gate_uint32_t scanline_length; | ||
| 179 | gate_color_t* ptrpixels; | ||
| 180 | |||
| 181 | 1 | jpeg_result = jpeg_read_header(decompress, TRUE); | |
| 182 | |||
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (jpeg_result != JPEG_HEADER_OK) |
| 184 | { | ||
| 185 | ✗ | ret = GATE_RESULT_INVALIDHEADER; | |
| 186 | ✗ | break; | |
| 187 | } | ||
| 188 | |||
| 189 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (FALSE == jpeg_start_decompress(decompress)) |
| 190 | { | ||
| 191 | ✗ | ret = GATE_RESULT_FAILED; | |
| 192 | ✗ | break; | |
| 193 | } | ||
| 194 | |||
| 195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (NULL == gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_DEFAULT, |
| 196 | 1 | (gate_uint32_t)decompress->output_width, | |
| 197 | 1 | (gate_uint32_t)decompress->output_height, NULL)) | |
| 198 | { | ||
| 199 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 200 | ✗ | break; | |
| 201 | } | ||
| 202 | 1 | ptrpixels = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0); | |
| 203 | |||
| 204 | 1 | scanline_length = (gate_uint32_t)decompress->output_components * raster.width; | |
| 205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (scanline_length > sizeof(buffer)) |
| 206 | { | ||
| 207 | ✗ | ptrbuffer = (char*)gate_mem_alloc(scanline_length); | |
| 208 | ✗ | if (ptrbuffer == NULL) | |
| 209 | { | ||
| 210 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 211 | ✗ | break; | |
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | while (decompress->output_scanline < decompress->output_height) |
| 216 | { | ||
| 217 | static const JDIMENSION jpeg_lines_to_read = 1; | ||
| 218 | gate_uint32_t x; | ||
| 219 | char* ptrbytes; | ||
| 220 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | if (jpeg_lines_to_read != jpeg_read_scanlines(decompress, (JSAMPARRAY)&ptrbuffer, jpeg_lines_to_read)) |
| 221 | { | ||
| 222 | ✗ | if (GATE_SUCCEEDED(user_param->result)) | |
| 223 | { | ||
| 224 | ✗ | user_param->result = GATE_RESULT_INVALIDINPUT; | |
| 225 | } | ||
| 226 | ✗ | break; | |
| 227 | } | ||
| 228 | 32 | ptrbytes = ptrbuffer; | |
| 229 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | switch (decompress->output_components) |
| 230 | { | ||
| 231 | ✗ | case 1: | |
| 232 | { | ||
| 233 | ✗ | for (x = 0; x != raster.width; ++x) | |
| 234 | { | ||
| 235 | ✗ | ptrpixels->r = *ptrbytes; | |
| 236 | ✗ | ptrpixels->g = *ptrbytes; | |
| 237 | ✗ | ptrpixels->b = *ptrbytes; | |
| 238 | ✗ | ptrpixels->a = 255; | |
| 239 | ✗ | ++ptrbytes; | |
| 240 | ✗ | ++ptrpixels; | |
| 241 | } | ||
| 242 | ✗ | break; | |
| 243 | } | ||
| 244 | 32 | case 3: | |
| 245 | { | ||
| 246 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != raster.width; ++x) |
| 247 | { | ||
| 248 | 2048 | ptrpixels->r = *(ptrbytes++); | |
| 249 | 2048 | ptrpixels->g = *(ptrbytes++); | |
| 250 | 2048 | ptrpixels->b = *(ptrbytes++); | |
| 251 | 2048 | ptrpixels->a = 255; | |
| 252 | 2048 | ++ptrpixels; | |
| 253 | } | ||
| 254 | 32 | break; | |
| 255 | } | ||
| 256 | ✗ | default: | |
| 257 | { | ||
| 258 | ✗ | decompress->output_scanline = raster.height; | |
| 259 | ✗ | user_param->result = GATE_RESULT_NOTSUPPORTED; | |
| 260 | ✗ | break; | |
| 261 | } | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | 1 | jpeg_finish_decompress(decompress); | |
| 266 | |||
| 267 | 1 | ret = user_param->result; | |
| 268 | |||
| 269 | } while (0); | ||
| 270 | |||
| 271 | |||
| 272 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (GATE_SUCCEEDED(ret)) |
| 273 | { | ||
| 274 | 1 | gate_mem_copy(image, &raster, sizeof(raster)); | |
| 275 | 1 | gate_mem_clear(&raster, sizeof(raster)); | |
| 276 | } | ||
| 277 | |||
| 278 | 1 | gate_rasterimage_release(&raster); | |
| 279 | |||
| 280 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if ((ptrbuffer != &buffer[0]) && (ptrbuffer != NULL)) |
| 281 | { | ||
| 282 | ✗ | gate_mem_dealloc(ptrbuffer); | |
| 283 | } | ||
| 284 | |||
| 285 | 1 | return ret; | |
| 286 | } | ||
| 287 | |||
| 288 | 1 | gate_result_t gate_jpegimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags) | |
| 289 | { | ||
| 290 | 1 | gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED; | |
| 291 | 1 | gate_jpeg_error_mgr_t error_mgr = GATE_INIT_EMPTY; | |
| 292 | 1 | struct jpeg_source_mgr source_mgr = GATE_INIT_EMPTY; | |
| 293 | 1 | struct jpeg_decompress_struct decompress = GATE_INIT_EMPTY; | |
| 294 | 1 | gate_jpeg_param_t user_param = GATE_INIT_EMPTY; | |
| 295 | |||
| 296 | 1 | user_param.stream = srcstream; | |
| 297 | 1 | user_param.result = GATE_RESULT_OK; | |
| 298 | |||
| 299 | 1 | decompress.err = jpeg_std_error(&error_mgr.error_mgr); | |
| 300 | 1 | error_mgr.error_mgr.error_exit = &gate_jpeg_error_exit; | |
| 301 | 1 | error_mgr.ptr_param = &user_param; | |
| 302 | |||
| 303 | 1 | jpeg_create_decompress(&decompress); | |
| 304 | |||
| 305 | 1 | source_mgr.fill_input_buffer = &gate_jpeg_fill_input_buffer; | |
| 306 | 1 | source_mgr.init_source = &gate_jpeg_init_source; | |
| 307 | 1 | source_mgr.resync_to_restart = &gate_jpeg_resync_to_restart; | |
| 308 | 1 | source_mgr.skip_input_data = &gate_jpeg_skip_input_data; | |
| 309 | 1 | source_mgr.term_source = &gate_jpeg_term_source; | |
| 310 | 1 | source_mgr.bytes_in_buffer = 0; | |
| 311 | 1 | source_mgr.next_input_byte = NULL; | |
| 312 | |||
| 313 | 1 | decompress.src = &source_mgr; | |
| 314 | 1 | decompress.client_data = &user_param; | |
| 315 | |||
| 316 | 1 | jpeg_set_marker_processor(&decompress, JPEG_COM, &gate_jpeg_marker_parser); | |
| 317 | 1 | jpeg_set_marker_processor(&decompress, JPEG_APP0 + 12, &gate_jpeg_marker_parser); | |
| 318 | |||
| 319 | do | ||
| 320 | { | ||
| 321 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (0 == setjmp(user_param.emergency_exit)) |
| 322 | { | ||
| 323 | 1 | ret = gate_jpegimage_load_data(&decompress, &user_param, image); | |
| 324 | } | ||
| 325 | else /* setjmp error case */ | ||
| 326 | { | ||
| 327 | /* jpeg emergency exit point reached */ | ||
| 328 | ✗ | ret = GATE_RESULT_CRITICALERROR; | |
| 329 | } | ||
| 330 | } while (0); | ||
| 331 | |||
| 332 | 1 | jpeg_destroy_decompress(&decompress); | |
| 333 | 1 | return ret; | |
| 334 | } | ||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | |||
| 339 | |||
| 340 | 2 | static void gate_jpeg_init_destination(struct jpeg_compress_struct* compress) | |
| 341 | { | ||
| 342 | 2 | gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)compress->client_data; | |
| 343 | 2 | compress->dest->free_in_buffer = sizeof(user_param->buffer); | |
| 344 | 2 | compress->dest->next_output_byte = &user_param->buffer[0]; | |
| 345 | 2 | } | |
| 346 | |||
| 347 | 1 | static boolean gate_jpeg_flush_output_buffer(struct jpeg_compress_struct* compress, gate_jpeg_param_t* user_param, gate_size_t bufferused) | |
| 348 | { | ||
| 349 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (bufferused != 0) |
| 350 | { | ||
| 351 | gate_size_t byteswritten; | ||
| 352 | 1 | gate_result_t result = gate_stream_write_block(user_param->stream, (char const*)user_param->buffer, bufferused, &byteswritten); | |
| 353 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (GATE_FAILED(result)) |
| 354 | { | ||
| 355 | ✗ | user_param->result = result; | |
| 356 | ✗ | return FALSE; | |
| 357 | } | ||
| 358 | } | ||
| 359 | 1 | gate_jpeg_init_destination(compress); | |
| 360 | 1 | return TRUE; | |
| 361 | } | ||
| 362 | |||
| 363 | ✗ | static boolean gate_jpeg_empty_output_buffer(struct jpeg_compress_struct* compress) | |
| 364 | { | ||
| 365 | ✗ | gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)compress->client_data; | |
| 366 | ✗ | return gate_jpeg_flush_output_buffer(compress, user_param, sizeof(user_param->buffer)); | |
| 367 | } | ||
| 368 | |||
| 369 | 1 | static void gate_jpeg_term_destination(struct jpeg_compress_struct* compress) | |
| 370 | { | ||
| 371 | 1 | gate_jpeg_param_t* user_param = (gate_jpeg_param_t*)compress->client_data; | |
| 372 | |||
| 373 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(sizeof(user_param->buffer) >= compress->dest->free_in_buffer); |
| 374 | 1 | gate_jpeg_flush_output_buffer(compress, user_param, sizeof(user_param->buffer) - compress->dest->free_in_buffer); | |
| 375 | 1 | } | |
| 376 | |||
| 377 | 1 | static gate_result_t gate_jpegimage_save_data(gate_rasterimage_t const* image, | |
| 378 | struct jpeg_destination_mgr* destination_mgr, | ||
| 379 | struct jpeg_compress_struct* compress, | ||
| 380 | gate_jpeg_param_t* user_param, | ||
| 381 | gate_uint8_t qualitypercent) | ||
| 382 | { | ||
| 383 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 384 | unsigned char buffer[4096 * 3]; | ||
| 385 | 1 | unsigned char* ptrbuffer = &buffer[0]; | |
| 386 | |||
| 387 | do | ||
| 388 | { | ||
| 389 | gate_size_t required_buffer_size; | ||
| 390 | unsigned int y; | ||
| 391 | 1 | void(*line_writer)(gate_uint8_t*, gate_uint8_t const*, gate_size_t) = NULL; | |
| 392 | |||
| 393 |
1/11✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
1 | switch (image->pixel_format) |
| 394 | { | ||
| 395 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGBA: | |
| 396 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB32: line_writer = &gate_color_convert_rgb_from_rgba_32; break; | |
| 397 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGRA: | |
| 398 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGR32: line_writer = &gate_color_convert_rgb_from_bgra_32; break; | |
| 399 | |||
| 400 | 1 | case GATE_IMAGE_PIXELFORMAT_RGB24: line_writer = &gate_color_convert_rgb_from_rgb_24; break; | |
| 401 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGR24: line_writer = &gate_color_convert_rgb_from_bgr_24; break; | |
| 402 | |||
| 403 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB555: line_writer = &gate_color_convert_rgb_from_rgb_15; break; | |
| 404 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB565: line_writer = &gate_color_convert_rgb_from_rgb_16; break; | |
| 405 | ✗ | case GATE_IMAGE_PIXELFORMAT_ARGB4: line_writer = &gate_color_convert_rgb_from_argb_16; break; | |
| 406 | ✗ | case GATE_IMAGE_PIXELFORMAT_YUV2: line_writer = &gate_color_convert_rgb_from_yuv2_16; break; | |
| 407 | |||
| 408 | ✗ | case GATE_IMAGE_PIXELFORMAT_PAL8: line_writer = &gate_color_convert_rgb_from_pal_8; break; | |
| 409 | ✗ | case GATE_IMAGE_PIXELFORMAT_GRAY8: line_writer = &gate_color_convert_rgb_from_gray_8; break; | |
| 410 | ✗ | default: break; | |
| 411 | } | ||
| 412 | |||
| 413 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (NULL == line_writer) |
| 414 | { | ||
| 415 | ✗ | ret = GATE_RESULT_NOTSUPPORTED; | |
| 416 | ✗ | break; | |
| 417 | } | ||
| 418 | |||
| 419 | 1 | required_buffer_size = (gate_size_t)image->width * 3; | |
| 420 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (required_buffer_size > sizeof(buffer)) |
| 421 | { | ||
| 422 | ✗ | ptrbuffer = (unsigned char*)gate_mem_alloc(required_buffer_size); | |
| 423 | ✗ | if (ptrbuffer == NULL) | |
| 424 | { | ||
| 425 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 426 | ✗ | break; | |
| 427 | } | ||
| 428 | } | ||
| 429 | |||
| 430 | 1 | jpeg_create_compress(compress); | |
| 431 | 1 | compress->in_color_space = JCS_RGB; | |
| 432 | 1 | compress->input_components = 3; | |
| 433 | 1 | compress->image_width = (JDIMENSION)image->width; | |
| 434 | 1 | compress->image_height = (JDIMENSION)image->height; | |
| 435 | 1 | compress->dct_method = JDCT_FLOAT; | |
| 436 | |||
| 437 | 1 | jpeg_set_defaults(compress); | |
| 438 | 1 | compress->client_data = user_param; | |
| 439 | 1 | compress->dest = destination_mgr; | |
| 440 | 1 | jpeg_set_quality(compress, (int)((qualitypercent > 100) ? 100 : qualitypercent), TRUE); | |
| 441 | |||
| 442 | 1 | jpeg_start_compress(compress, TRUE); | |
| 443 | |||
| 444 | |||
| 445 | |||
| 446 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (y = 0; y != image->height; ++y) |
| 447 | { | ||
| 448 | 32 | gate_uint8_t const* ptrpixels = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y); | |
| 449 | 32 | line_writer(ptrbuffer, ptrpixels, image->width); | |
| 450 | |||
| 451 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | if (1 != jpeg_write_scanlines(compress, (JSAMPARRAY)&ptrbuffer, 1)) |
| 452 | { | ||
| 453 | /* failed to write one line */ | ||
| 454 | ✗ | if (GATE_SUCCEEDED(user_param->result)) | |
| 455 | { | ||
| 456 | ✗ | user_param->result = GATE_RESULT_INVALIDOUTPUT; | |
| 457 | ✗ | break; | |
| 458 | } | ||
| 459 | } | ||
| 460 | } | ||
| 461 | |||
| 462 | 1 | jpeg_finish_compress(compress); | |
| 463 | 1 | jpeg_destroy_compress(compress); | |
| 464 | |||
| 465 | 1 | ret = user_param->result; | |
| 466 | } while (0); | ||
| 467 | |||
| 468 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if ((ptrbuffer != &buffer[0]) && (ptrbuffer != NULL)) |
| 469 | { | ||
| 470 | ✗ | gate_mem_dealloc(ptrbuffer); | |
| 471 | } | ||
| 472 | 1 | return ret; | |
| 473 | } | ||
| 474 | |||
| 475 | 1 | gate_result_t gate_jpegimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags) | |
| 476 | { | ||
| 477 | gate_result_t ret; | ||
| 478 | 1 | gate_jpeg_param_t user_param = GATE_INIT_EMPTY; | |
| 479 | 1 | gate_jpeg_error_mgr_t error_mgr = GATE_INIT_EMPTY; | |
| 480 | 1 | struct jpeg_destination_mgr destination_mgr = GATE_INIT_EMPTY; | |
| 481 | 1 | struct jpeg_compress_struct compress = GATE_INIT_EMPTY; | |
| 482 | 1 | const gate_uint16_t qa0to15 = (gate_uint16_t)GATE_IMAGE_FLAG_DECODE_QUALITY(flags); | |
| 483 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | const gate_uint8_t qualitypercent = (qa0to15 > 0) |
| 484 | ✗ | ? (gate_uint8_t)(qa0to15 * 100 / 15) | |
| 485 | : 90; | ||
| 486 | |||
| 487 | 1 | user_param.result = GATE_RESULT_OK; | |
| 488 | 1 | user_param.stream = deststream; | |
| 489 | |||
| 490 | 1 | compress.err = jpeg_std_error(&error_mgr.error_mgr); | |
| 491 | 1 | error_mgr.error_mgr.error_exit = &gate_jpeg_error_exit; | |
| 492 | 1 | error_mgr.ptr_param = &user_param; | |
| 493 | |||
| 494 | 1 | destination_mgr.empty_output_buffer = &gate_jpeg_empty_output_buffer; | |
| 495 | 1 | destination_mgr.init_destination = &gate_jpeg_init_destination; | |
| 496 | 1 | destination_mgr.term_destination = &gate_jpeg_term_destination; | |
| 497 | 1 | destination_mgr.free_in_buffer = 0; | |
| 498 | 1 | destination_mgr.next_output_byte = NULL; | |
| 499 | |||
| 500 | do | ||
| 501 | { | ||
| 502 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (0 == setjmp(user_param.emergency_exit)) |
| 503 | { | ||
| 504 | 1 | ret = gate_jpegimage_save_data(image, &destination_mgr, &compress, &user_param, qualitypercent); | |
| 505 | } | ||
| 506 | else /* setjmp error case */ | ||
| 507 | { | ||
| 508 | /* jpeg emergency exit point reached */ | ||
| 509 | ✗ | ret = GATE_RESULT_CRITICALERROR; | |
| 510 | } | ||
| 511 | } while (0); | ||
| 512 | 1 | return ret; | |
| 513 | } | ||
| 514 | |||
| 515 | #endif /* GATE_GRAPHICS_JPEG_USE_LIBJPEG */ | ||
| 516 | |||
| 517 | |||
| 518 | |||
| 519 | #if defined(GATE_GRAPHICS_JPEG_USE_OLE_IPICTURE) | ||
| 520 | |||
| 521 | #include "gate/graphics/platform/win32_olepicture.h" | ||
| 522 | |||
| 523 | gate_result_t gate_jpegimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags) | ||
| 524 | { | ||
| 525 | return gate_win32_load_ole_picture_file(srcstream, image); | ||
| 526 | } | ||
| 527 | |||
| 528 | gate_result_t gate_jpegimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags) | ||
| 529 | { | ||
| 530 | (void)image; | ||
| 531 | (void)deststream; | ||
| 532 | (void)flags; | ||
| 533 | return GATE_RESULT_NOTIMPLEMENTED; | ||
| 534 | } | ||
| 535 | |||
| 536 | #endif /* GATE_GRAPHICS_JPEG_USE_OLE_IPICTURE */ | ||
| 537 | |||
| 538 | |||
| 539 | |||
| 540 | #if defined(GATE_GRAPHICS_JPEG_NO_IMPL) | ||
| 541 | |||
| 542 | gate_result_t gate_jpegimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags) | ||
| 543 | { | ||
| 544 | (void)srcstream; | ||
| 545 | (void)image; | ||
| 546 | (void)flags; | ||
| 547 | return GATE_RESULT_NOTIMPLEMENTED; | ||
| 548 | } | ||
| 549 | |||
| 550 | gate_result_t gate_jpegimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags) | ||
| 551 | { | ||
| 552 | (void)image; | ||
| 553 | (void)deststream; | ||
| 554 | (void)flags; | ||
| 555 | return GATE_RESULT_NOTIMPLEMENTED; | ||
| 556 | } | ||
| 557 | |||
| 558 | #endif | ||
| 559 | |||
| 560 |