Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/fitz/load-jpeg.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2023 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 #include "mupdf/fitz.h" | |
| 24 | |
| 25 #include <math.h> | |
| 26 #include <stdio.h> | |
| 27 #include <string.h> | |
| 28 #include <limits.h> | |
| 29 | |
| 30 #include <jpeglib.h> | |
| 31 | |
| 32 #ifdef SHARE_JPEG | |
| 33 | |
| 34 #define JZ_CTX_FROM_CINFO(c) (fz_context *)((c)->client_data) | |
| 35 | |
| 36 static void fz_jpg_mem_init(j_common_ptr cinfo, fz_context *ctx) | |
| 37 { | |
| 38 cinfo->client_data = ctx; | |
| 39 } | |
| 40 | |
| 41 #define fz_jpg_mem_term(cinfo) | |
| 42 | |
| 43 #else /* SHARE_JPEG */ | |
| 44 | |
| 45 typedef void * backing_store_ptr; | |
| 46 #include "jmemcust.h" | |
| 47 | |
| 48 #define JZ_CTX_FROM_CINFO(c) (fz_context *)(GET_CUST_MEM_DATA(c)->priv) | |
| 49 | |
| 50 static void * | |
| 51 fz_jpg_mem_alloc(j_common_ptr cinfo, size_t size) | |
| 52 { | |
| 53 fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo); | |
| 54 return fz_malloc_no_throw(ctx, size); | |
| 55 } | |
| 56 | |
| 57 static void | |
| 58 fz_jpg_mem_free(j_common_ptr cinfo, void *object, size_t size) | |
| 59 { | |
| 60 fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo); | |
| 61 fz_free(ctx, object); | |
| 62 } | |
| 63 | |
| 64 static void | |
| 65 fz_jpg_mem_init(j_common_ptr cinfo, fz_context *ctx) | |
| 66 { | |
| 67 jpeg_cust_mem_data *custmptr; | |
| 68 custmptr = fz_malloc_struct(ctx, jpeg_cust_mem_data); | |
| 69 if (!jpeg_cust_mem_init(custmptr, (void *) ctx, NULL, NULL, NULL, | |
| 70 fz_jpg_mem_alloc, fz_jpg_mem_free, | |
| 71 fz_jpg_mem_alloc, fz_jpg_mem_free, NULL)) | |
| 72 { | |
| 73 fz_free(ctx, custmptr); | |
| 74 fz_throw(ctx, FZ_ERROR_LIBRARY, "cannot initialize custom JPEG memory handler"); | |
| 75 } | |
| 76 cinfo->client_data = custmptr; | |
| 77 } | |
| 78 | |
| 79 static void | |
| 80 fz_jpg_mem_term(j_common_ptr cinfo) | |
| 81 { | |
| 82 if (cinfo->client_data) | |
| 83 { | |
| 84 fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo); | |
| 85 fz_free(ctx, cinfo->client_data); | |
| 86 cinfo->client_data = NULL; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 #endif /* SHARE_JPEG */ | |
| 91 | |
| 92 static void output_message(j_common_ptr cinfo) | |
| 93 { | |
| 94 /* swallow message */ | |
| 95 } | |
| 96 | |
| 97 static void error_exit(j_common_ptr cinfo) | |
| 98 { | |
| 99 char msg[JMSG_LENGTH_MAX]; | |
| 100 fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo); | |
| 101 | |
| 102 cinfo->err->format_message(cinfo, msg); | |
| 103 fz_throw(ctx, FZ_ERROR_LIBRARY, "jpeg error: %s", msg); | |
| 104 } | |
| 105 | |
| 106 static void init_source(j_decompress_ptr cinfo) | |
| 107 { | |
| 108 /* nothing to do */ | |
| 109 } | |
| 110 | |
| 111 static void term_source(j_decompress_ptr cinfo) | |
| 112 { | |
| 113 /* nothing to do */ | |
| 114 } | |
| 115 | |
| 116 static boolean fill_input_buffer(j_decompress_ptr cinfo) | |
| 117 { | |
| 118 static unsigned char eoi[2] = { 0xFF, JPEG_EOI }; | |
| 119 struct jpeg_source_mgr *src = cinfo->src; | |
| 120 src->next_input_byte = eoi; | |
| 121 src->bytes_in_buffer = 2; | |
| 122 return 1; | |
| 123 } | |
| 124 | |
| 125 static void skip_input_data(j_decompress_ptr cinfo, long num_bytes) | |
| 126 { | |
| 127 struct jpeg_source_mgr *src = cinfo->src; | |
| 128 if (num_bytes > 0) | |
| 129 { | |
| 130 size_t skip = (size_t)num_bytes; /* size_t may be 64bit */ | |
| 131 if (skip > src->bytes_in_buffer) | |
| 132 skip = (size_t)src->bytes_in_buffer; | |
| 133 src->next_input_byte += skip; | |
| 134 src->bytes_in_buffer -= skip; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 static inline int read_value(const unsigned char *data, int bytes, int is_big_endian) | |
| 139 { | |
| 140 int value = 0; | |
| 141 if (!is_big_endian) | |
| 142 data += bytes; | |
| 143 for (; bytes > 0; bytes--) | |
| 144 value = (value << 8) | (is_big_endian ? *data++ : *--data); | |
| 145 return value; | |
| 146 } | |
| 147 | |
| 148 enum { | |
| 149 MAX_ICC_PARTS = 256 | |
| 150 }; | |
| 151 | |
| 152 static fz_colorspace *extract_icc_profile(fz_context *ctx, jpeg_saved_marker_ptr init_marker, int output_components, fz_colorspace *colorspace) | |
| 153 { | |
| 154 #if FZ_ENABLE_ICC | |
| 155 const char idseq[] = { 'I', 'C', 'C', '_', 'P', 'R', 'O', 'F', 'I', 'L', 'E', '\0'}; | |
| 156 jpeg_saved_marker_ptr marker = init_marker; | |
| 157 fz_buffer *buf = NULL; | |
| 158 fz_colorspace *icc; | |
| 159 int part = 1; | |
| 160 int parts = MAX_ICC_PARTS; | |
| 161 const unsigned char *data; | |
| 162 size_t size; | |
| 163 | |
| 164 fz_var(buf); | |
| 165 | |
| 166 if (init_marker == NULL) | |
| 167 return colorspace; | |
| 168 | |
| 169 fz_try(ctx) | |
| 170 { | |
| 171 while (part < parts && marker != NULL) | |
| 172 { | |
| 173 for (marker = init_marker; marker != NULL; marker = marker->next) | |
| 174 { | |
| 175 if (marker->marker != JPEG_APP0 + 2) | |
| 176 continue; | |
| 177 if (marker->data_length < nelem(idseq) + 2) | |
| 178 continue; | |
| 179 if (memcmp(marker->data, idseq, nelem(idseq))) | |
| 180 continue; | |
| 181 if (marker->data[nelem(idseq)] != part) | |
| 182 continue; | |
| 183 | |
| 184 if (parts == MAX_ICC_PARTS) | |
| 185 parts = marker->data[nelem(idseq) + 1]; | |
| 186 else if (marker->data[nelem(idseq) + 1] != parts) | |
| 187 fz_warn(ctx, "inconsistent number of icc profile chunks in jpeg"); | |
| 188 if (part > parts) | |
| 189 { | |
| 190 fz_warn(ctx, "skipping out of range icc profile chunk in jpeg"); | |
| 191 continue; | |
| 192 } | |
| 193 | |
| 194 data = marker->data + 14; | |
| 195 size = marker->data_length - 14; | |
| 196 | |
| 197 if (!buf) | |
| 198 buf = fz_new_buffer_from_copied_data(ctx, data, size); | |
| 199 else | |
| 200 fz_append_data(ctx, buf, data, size); | |
| 201 | |
| 202 part++; | |
| 203 break; | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 if (buf) | |
| 208 { | |
| 209 icc = fz_new_icc_colorspace(ctx, fz_colorspace_type(ctx, colorspace), 0, NULL, buf); | |
| 210 fz_drop_colorspace(ctx, colorspace); | |
| 211 colorspace = icc; | |
| 212 } | |
| 213 } | |
| 214 fz_always(ctx) | |
| 215 fz_drop_buffer(ctx, buf); | |
| 216 fz_catch(ctx) | |
| 217 { | |
| 218 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 219 fz_report_error(ctx); | |
| 220 fz_warn(ctx, "ignoring embedded ICC profile in JPEG"); | |
| 221 } | |
| 222 | |
| 223 return colorspace; | |
| 224 #else | |
| 225 return colorspace; | |
| 226 #endif | |
| 227 } | |
| 228 | |
| 229 /* Returns true if <x> can be represented as an integer without overflow. | |
| 230 * | |
| 231 * We can't use comparisons such as 'return x < INT_MAX' because INT_MAX is | |
| 232 * not safely convertible to float - it ends up as INT_MAX+1 so the comparison | |
| 233 * doesn't do what we want. | |
| 234 * | |
| 235 * Instead we do a round-trip conversion and return true if this differs by | |
| 236 * less than 1. This relies on high adjacent float values that differ by more | |
| 237 * than 1, actually being exact integers, so the round-trip doesn't change the | |
| 238 * value. | |
| 239 */ | |
| 240 static int float_can_be_int(float x) | |
| 241 { | |
| 242 return fabsf(x - (float)(int) x) < 1; | |
| 243 } | |
| 244 | |
| 245 static uint8_t exif_orientation_to_mupdf[9] = { 0, 1, 5, 3, 7, 6, 4, 8, 2 }; | |
| 246 | |
| 247 static int extract_exif_resolution(jpeg_saved_marker_ptr marker, | |
| 248 int *xres, int *yres, uint8_t *orientation) | |
| 249 { | |
| 250 int is_big_endian, orient; | |
| 251 const unsigned char *data; | |
| 252 unsigned int offset, ifd_len, res_type = 0; | |
| 253 float x_res = 0, y_res = 0; | |
| 254 | |
| 255 if (!marker || marker->marker != JPEG_APP0 + 1 || marker->data_length < 14) | |
| 256 return 0; | |
| 257 data = (const unsigned char *)marker->data; | |
| 258 if (read_value(data, 4, 1) != 0x45786966 /* Exif */ || read_value(data + 4, 2, 1) != 0x0000) | |
| 259 return 0; | |
| 260 if (read_value(data + 6, 4, 1) == 0x49492A00) | |
| 261 is_big_endian = 0; | |
| 262 else if (read_value(data + 6, 4, 1) == 0x4D4D002A) | |
| 263 is_big_endian = 1; | |
| 264 else | |
| 265 return 0; | |
| 266 | |
| 267 offset = read_value(data + 10, 4, is_big_endian) + 6; | |
| 268 if (offset < 14 || offset > marker->data_length - 2) | |
| 269 return 0; | |
| 270 ifd_len = read_value(data + offset, 2, is_big_endian); | |
| 271 for (offset += 2; ifd_len > 0 && offset + 12 < marker->data_length; ifd_len--, offset += 12) | |
| 272 { | |
| 273 int tag = read_value(data + offset, 2, is_big_endian); | |
| 274 int type = read_value(data + offset + 2, 2, is_big_endian); | |
| 275 int count = read_value(data + offset + 4, 4, is_big_endian); | |
| 276 unsigned int value_off = read_value(data + offset + 8, 4, is_big_endian) + 6; | |
| 277 switch (tag) | |
| 278 { | |
| 279 case 0x112: | |
| 280 if (type == 3 && count == 1) { | |
| 281 orient = read_value(data + offset + 8, 2, is_big_endian); | |
| 282 if (orient >= 1 && orient <= 8 && orientation) | |
| 283 *orientation = exif_orientation_to_mupdf[orient]; | |
| 284 } | |
| 285 break; | |
| 286 case 0x11A: | |
| 287 if (type == 5 && value_off > offset && value_off <= marker->data_length - 8) | |
| 288 x_res = 1.0f * read_value(data + value_off, 4, is_big_endian) / read_value(data + value_off + 4, 4, is_big_endian); | |
| 289 break; | |
| 290 case 0x11B: | |
| 291 if (type == 5 && value_off > offset && value_off <= marker->data_length - 8) | |
| 292 y_res = 1.0f * read_value(data + value_off, 4, is_big_endian) / read_value(data + value_off + 4, 4, is_big_endian); | |
| 293 break; | |
| 294 case 0x128: | |
| 295 if (type == 3 && count == 1) | |
| 296 res_type = read_value(data + offset + 8, 2, is_big_endian); | |
| 297 break; | |
| 298 } | |
| 299 } | |
| 300 | |
| 301 if (x_res <= 0 || !float_can_be_int(x_res) || y_res <= 0 || !float_can_be_int(y_res)) | |
| 302 return 0; | |
| 303 if (res_type == 2) | |
| 304 { | |
| 305 *xres = (int)x_res; | |
| 306 *yres = (int)y_res; | |
| 307 } | |
| 308 else if (res_type == 3) | |
| 309 { | |
| 310 *xres = (int)(x_res * 254 / 100); | |
| 311 *yres = (int)(y_res * 254 / 100); | |
| 312 } | |
| 313 else | |
| 314 { | |
| 315 *xres = 0; | |
| 316 *yres = 0; | |
| 317 } | |
| 318 return 1; | |
| 319 } | |
| 320 | |
| 321 static int extract_app13_resolution(jpeg_saved_marker_ptr marker, int *xres, int *yres) | |
| 322 { | |
| 323 const unsigned char *data, *data_end; | |
| 324 | |
| 325 if (!marker || marker->marker != JPEG_APP0 + 13 || marker->data_length < 42 || | |
| 326 strcmp((const char *)marker->data, "Photoshop 3.0") != 0) | |
| 327 { | |
| 328 return 0; | |
| 329 } | |
| 330 | |
| 331 data = (const unsigned char *)marker->data; | |
| 332 data_end = data + marker->data_length; | |
| 333 for (data += 14; data + 12 < data_end; ) { | |
| 334 int data_size = -1; | |
| 335 int tag = read_value(data + 4, 2, 1); | |
| 336 int value_off = 11 + read_value(data + 6, 2, 1); | |
| 337 if (value_off % 2 == 1) | |
| 338 value_off++; | |
| 339 if (read_value(data, 4, 1) == 0x3842494D /* 8BIM */ && value_off <= data_end - data) | |
| 340 data_size = read_value(data + value_off - 4, 4, 1); | |
| 341 if (data_size < 0 || data_size > data_end - data - value_off) | |
| 342 return 0; | |
| 343 if (tag == 0x3ED && data_size == 16) | |
| 344 { | |
| 345 *xres = read_value(data + value_off, 2, 1); | |
| 346 *yres = read_value(data + value_off + 8, 2, 1); | |
| 347 return 1; | |
| 348 } | |
| 349 if (data_size % 2 == 1) | |
| 350 data_size++; | |
| 351 data += value_off + data_size; | |
| 352 } | |
| 353 | |
| 354 return 0; | |
| 355 } | |
| 356 | |
| 357 static void invert_cmyk(unsigned char *p, int n) | |
| 358 { | |
| 359 int i; | |
| 360 for (i = 0; i < n; ++i) | |
| 361 p[i] = 255 - p[i]; | |
| 362 } | |
| 363 | |
| 364 fz_pixmap * | |
| 365 fz_load_jpeg(fz_context *ctx, const unsigned char *rbuf, size_t rlen) | |
| 366 { | |
| 367 struct jpeg_decompress_struct cinfo; | |
| 368 struct jpeg_error_mgr err; | |
| 369 struct jpeg_source_mgr src; | |
| 370 unsigned char *row[1], *sp, *dp; | |
| 371 fz_colorspace *colorspace = NULL; | |
| 372 unsigned int x; | |
| 373 int k; | |
| 374 size_t stride; | |
| 375 fz_pixmap *image = NULL; | |
| 376 | |
| 377 fz_var(colorspace); | |
| 378 fz_var(image); | |
| 379 fz_var(row); | |
| 380 | |
| 381 row[0] = NULL; | |
| 382 | |
| 383 cinfo.mem = NULL; | |
| 384 cinfo.global_state = 0; | |
| 385 cinfo.err = jpeg_std_error(&err); | |
| 386 err.output_message = output_message; | |
| 387 err.error_exit = error_exit; | |
| 388 | |
| 389 cinfo.client_data = NULL; | |
| 390 fz_jpg_mem_init((j_common_ptr)&cinfo, ctx); | |
| 391 | |
| 392 fz_try(ctx) | |
| 393 { | |
| 394 jpeg_create_decompress(&cinfo); | |
| 395 | |
| 396 cinfo.src = &src; | |
| 397 src.init_source = init_source; | |
| 398 src.fill_input_buffer = fill_input_buffer; | |
| 399 src.skip_input_data = skip_input_data; | |
| 400 src.resync_to_restart = jpeg_resync_to_restart; | |
| 401 src.term_source = term_source; | |
| 402 src.next_input_byte = rbuf; | |
| 403 src.bytes_in_buffer = rlen; | |
| 404 | |
| 405 jpeg_save_markers(&cinfo, JPEG_APP0+1, 0xffff); | |
| 406 jpeg_save_markers(&cinfo, JPEG_APP0+13, 0xffff); | |
| 407 jpeg_save_markers(&cinfo, JPEG_APP0+2, 0xffff); | |
| 408 | |
| 409 jpeg_read_header(&cinfo, 1); | |
| 410 | |
| 411 jpeg_start_decompress(&cinfo); | |
| 412 | |
| 413 if (cinfo.output_components == 1) | |
| 414 colorspace = fz_keep_colorspace(ctx, fz_device_gray(ctx)); | |
| 415 else if (cinfo.output_components == 3) | |
| 416 colorspace = fz_keep_colorspace(ctx, fz_device_rgb(ctx)); | |
| 417 else if (cinfo.output_components == 4) | |
| 418 colorspace = fz_keep_colorspace(ctx, fz_device_cmyk(ctx)); | |
| 419 colorspace = extract_icc_profile(ctx, cinfo.marker_list, cinfo.output_components, colorspace); | |
| 420 if (!colorspace) | |
| 421 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot determine colorspace"); | |
| 422 | |
| 423 image = fz_new_pixmap(ctx, colorspace, cinfo.output_width, cinfo.output_height, NULL, 0); | |
| 424 | |
| 425 if (extract_exif_resolution(cinfo.marker_list, &image->xres, &image->yres, NULL)) | |
| 426 /* XPS prefers EXIF resolution to JFIF density */; | |
| 427 else if (extract_app13_resolution(cinfo.marker_list, &image->xres, &image->yres)) | |
| 428 /* XPS prefers APP13 resolution to JFIF density */; | |
| 429 else if (cinfo.density_unit == 1) | |
| 430 { | |
| 431 image->xres = cinfo.X_density; | |
| 432 image->yres = cinfo.Y_density; | |
| 433 } | |
| 434 else if (cinfo.density_unit == 2) | |
| 435 { | |
| 436 image->xres = cinfo.X_density * 254 / 100; | |
| 437 image->yres = cinfo.Y_density * 254 / 100; | |
| 438 } | |
| 439 | |
| 440 if (image->xres <= 0) image->xres = 96; | |
| 441 if (image->yres <= 0) image->yres = 96; | |
| 442 | |
| 443 fz_clear_pixmap(ctx, image); | |
| 444 | |
| 445 row[0] = fz_malloc(ctx, (size_t)cinfo.output_components * cinfo.output_width); | |
| 446 dp = image->samples; | |
| 447 stride = image->stride - image->w * (size_t)image->n; | |
| 448 while (cinfo.output_scanline < cinfo.output_height) | |
| 449 { | |
| 450 jpeg_read_scanlines(&cinfo, row, 1); | |
| 451 | |
| 452 // Invert CMYK polarity for some CMYK images (see comment in filter-dct for details). | |
| 453 if (cinfo.out_color_space == JCS_CMYK && cinfo.Adobe_transform == 2) | |
| 454 invert_cmyk(row[0], image->stride); | |
| 455 | |
| 456 sp = row[0]; | |
| 457 for (x = 0; x < cinfo.output_width; x++) | |
| 458 { | |
| 459 for (k = 0; k < cinfo.output_components; k++) | |
| 460 *dp++ = *sp++; | |
| 461 } | |
| 462 dp += stride; | |
| 463 } | |
| 464 } | |
| 465 fz_always(ctx) | |
| 466 { | |
| 467 fz_drop_colorspace(ctx, colorspace); | |
| 468 fz_free(ctx, row[0]); | |
| 469 row[0] = NULL; | |
| 470 | |
| 471 /* We call jpeg_abort rather than the more usual | |
| 472 * jpeg_finish_decompress here. This has the same effect, | |
| 473 * but doesn't spew warnings if we didn't read enough data etc. | |
| 474 * Annoyingly jpeg_abort can throw | |
| 475 */ | |
| 476 fz_try(ctx) | |
| 477 jpeg_abort((j_common_ptr)&cinfo); | |
| 478 fz_catch(ctx) | |
| 479 { | |
| 480 /* Ignore any errors here */ | |
| 481 } | |
| 482 | |
| 483 jpeg_destroy_decompress(&cinfo); | |
| 484 fz_jpg_mem_term((j_common_ptr)&cinfo); | |
| 485 } | |
| 486 fz_catch(ctx) | |
| 487 { | |
| 488 fz_drop_pixmap(ctx, image); | |
| 489 fz_rethrow(ctx); | |
| 490 } | |
| 491 | |
| 492 return image; | |
| 493 } | |
| 494 | |
| 495 void | |
| 496 fz_load_jpeg_info(fz_context *ctx, const unsigned char *rbuf, size_t rlen, int *xp, int *yp, int *xresp, int *yresp, fz_colorspace **cspacep, uint8_t *orientation) | |
| 497 { | |
| 498 struct jpeg_decompress_struct cinfo; | |
| 499 struct jpeg_error_mgr err; | |
| 500 struct jpeg_source_mgr src; | |
| 501 fz_colorspace *icc = NULL; | |
| 502 | |
| 503 *cspacep = NULL; | |
| 504 if (orientation) | |
| 505 *orientation = 0; | |
| 506 | |
| 507 cinfo.mem = NULL; | |
| 508 cinfo.global_state = 0; | |
| 509 cinfo.err = jpeg_std_error(&err); | |
| 510 err.error_exit = error_exit; | |
| 511 | |
| 512 cinfo.client_data = NULL; | |
| 513 fz_jpg_mem_init((j_common_ptr)&cinfo, ctx); | |
| 514 | |
| 515 fz_try(ctx) | |
| 516 { | |
| 517 jpeg_create_decompress(&cinfo); | |
| 518 | |
| 519 cinfo.src = &src; | |
| 520 src.init_source = init_source; | |
| 521 src.fill_input_buffer = fill_input_buffer; | |
| 522 src.skip_input_data = skip_input_data; | |
| 523 src.resync_to_restart = jpeg_resync_to_restart; | |
| 524 src.term_source = term_source; | |
| 525 src.next_input_byte = rbuf; | |
| 526 src.bytes_in_buffer = rlen; | |
| 527 | |
| 528 jpeg_save_markers(&cinfo, JPEG_APP0+1, 0xffff); | |
| 529 jpeg_save_markers(&cinfo, JPEG_APP0+13, 0xffff); | |
| 530 jpeg_save_markers(&cinfo, JPEG_APP0+2, 0xffff); | |
| 531 | |
| 532 jpeg_read_header(&cinfo, 1); | |
| 533 | |
| 534 *xp = cinfo.image_width; | |
| 535 *yp = cinfo.image_height; | |
| 536 | |
| 537 if (cinfo.num_components == 1) | |
| 538 *cspacep = fz_keep_colorspace(ctx, fz_device_gray(ctx)); | |
| 539 else if (cinfo.num_components == 3) | |
| 540 *cspacep = fz_keep_colorspace(ctx, fz_device_rgb(ctx)); | |
| 541 else if (cinfo.num_components == 4) | |
| 542 *cspacep = fz_keep_colorspace(ctx, fz_device_cmyk(ctx)); | |
| 543 *cspacep = extract_icc_profile(ctx, cinfo.marker_list, cinfo.num_components, *cspacep); | |
| 544 if (!*cspacep) | |
| 545 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot determine colorspace"); | |
| 546 | |
| 547 if (extract_exif_resolution(cinfo.marker_list, xresp, yresp, orientation)) | |
| 548 /* XPS prefers EXIF resolution to JFIF density */; | |
| 549 else if (extract_app13_resolution(cinfo.marker_list, xresp, yresp)) | |
| 550 /* XPS prefers APP13 resolution to JFIF density */; | |
| 551 else if (cinfo.density_unit == 1) | |
| 552 { | |
| 553 *xresp = cinfo.X_density; | |
| 554 *yresp = cinfo.Y_density; | |
| 555 } | |
| 556 else if (cinfo.density_unit == 2) | |
| 557 { | |
| 558 *xresp = cinfo.X_density * 254 / 100; | |
| 559 *yresp = cinfo.Y_density * 254 / 100; | |
| 560 } | |
| 561 else | |
| 562 { | |
| 563 *xresp = 0; | |
| 564 *yresp = 0; | |
| 565 } | |
| 566 | |
| 567 if (*xresp <= 0) *xresp = 96; | |
| 568 if (*yresp <= 0) *yresp = 96; | |
| 569 } | |
| 570 fz_always(ctx) | |
| 571 { | |
| 572 jpeg_destroy_decompress(&cinfo); | |
| 573 fz_jpg_mem_term((j_common_ptr)&cinfo); | |
| 574 } | |
| 575 fz_catch(ctx) | |
| 576 { | |
| 577 fz_drop_colorspace(ctx, icc); | |
| 578 fz_rethrow(ctx); | |
| 579 } | |
| 580 } |
