Coverage Report

Created: 2025-01-11 06:55

/src/mupdf/source/fitz/load-jpx.c
Line
Count
Source (jump to first uncovered line)
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 "pixmap-imp.h"
26
27
#include <assert.h>
28
#include <string.h>
29
30
#if FZ_ENABLE_JPX
31
32
static void
33
jpx_ycc_to_rgb(fz_context *ctx, fz_pixmap *pix, int cbsign, int crsign)
34
0
{
35
0
  int w = pix->w;
36
0
  int h = pix->h;
37
0
  int stride = pix->stride;
38
0
  int x, y;
39
40
0
  for (y = 0; y < h; y++)
41
0
  {
42
0
    unsigned char * row = &pix->samples[stride * y];
43
0
    for (x = 0; x < w; x++)
44
0
    {
45
0
      int ycc[3];
46
0
      ycc[0] = row[x * 3 + 0];
47
0
      ycc[1] = row[x * 3 + 1];
48
0
      ycc[2] = row[x * 3 + 2];
49
50
      /* consciously skip Y */
51
0
      if (cbsign)
52
0
        ycc[1] -= 128;
53
0
      if (crsign)
54
0
        ycc[2] -= 128;
55
56
0
      row[x * 3 + 0] = fz_clampi(ycc[0] + 1.402f * ycc[2], 0, 255);
57
0
      row[x * 3 + 1] = fz_clampi(ycc[0] - 0.34413f * ycc[1] - 0.71414f * ycc[2], 0, 255);
58
0
      row[x * 3 + 2] = fz_clampi(ycc[0] + 1.772f * ycc[1], 0, 255);
59
0
    }
60
0
  }
61
0
}
62
63
#include <openjpeg.h>
64
65
typedef struct
66
{
67
  int width;
68
  int height;
69
  fz_colorspace *cs;
70
  int xres;
71
  int yres;
72
} fz_jpxd;
73
74
typedef struct
75
{
76
  const unsigned char *data;
77
  OPJ_SIZE_T size;
78
  OPJ_SIZE_T pos;
79
} stream_block;
80
81
/* OpenJPEG does not provide a safe mechanism to intercept
82
 * allocations. In the latest version all allocations go
83
 * though opj_malloc etc, but no context is passed around.
84
 *
85
 * In order to ensure that allocations throughout mupdf
86
 * are done consistently, we implement opj_malloc etc as
87
 * functions that call down to fz_malloc etc. These
88
 * require context variables, so we lock and unlock around
89
 * calls to openjpeg. Any attempt to call through
90
 * without setting these will be detected.
91
 *
92
 * It is therefore vital that any fz_lock/fz_unlock
93
 * handlers are shared between all the fz_contexts in
94
 * use at a time.
95
 */
96
97
/* Potentially we can write different versions
98
 * of get_context and set_context for different
99
 * threading systems.
100
 */
101
102
static fz_context *opj_secret = NULL;
103
104
static void set_opj_context(fz_context *ctx)
105
6.26k
{
106
6.26k
  opj_secret = ctx;
107
6.26k
}
108
109
static fz_context *get_opj_context(void)
110
104M
{
111
104M
  return opj_secret;
112
104M
}
113
114
void opj_lock(fz_context *ctx)
115
3.13k
{
116
3.13k
  fz_ft_lock(ctx);
117
118
3.13k
  set_opj_context(ctx);
119
3.13k
}
120
121
void opj_unlock(fz_context *ctx)
122
3.13k
{
123
3.13k
  set_opj_context(NULL);
124
125
3.13k
  fz_ft_unlock(ctx);
126
3.13k
}
127
128
void *opj_malloc(size_t size)
129
17.3M
{
130
17.3M
  fz_context *ctx = get_opj_context();
131
132
17.3M
  assert(ctx != NULL);
133
134
17.3M
  return Memento_label(fz_malloc_no_throw(ctx, size), "opj_malloc");
135
17.3M
}
136
137
void *opj_calloc(size_t n, size_t size)
138
34.4M
{
139
34.4M
  fz_context *ctx = get_opj_context();
140
141
34.4M
  assert(ctx != NULL);
142
143
34.4M
  return fz_calloc_no_throw(ctx, n, size);
144
34.4M
}
145
146
void *opj_realloc(void *ptr, size_t size)
147
207k
{
148
207k
  fz_context *ctx = get_opj_context();
149
150
207k
  assert(ctx != NULL);
151
152
207k
  return fz_realloc_no_throw(ctx, ptr, size);
153
207k
}
154
155
void opj_free(void *ptr)
156
52.0M
{
157
52.0M
  fz_context *ctx = get_opj_context();
158
159
52.0M
  assert(ctx != NULL);
160
161
52.0M
  fz_free(ctx, ptr);
162
52.0M
}
163
164
static void * opj_aligned_malloc_n(size_t alignment, size_t size)
165
62.5k
{
166
62.5k
  uint8_t *ptr;
167
62.5k
  size_t off;
168
169
62.5k
  if (size == 0)
170
51
    return NULL;
171
172
62.4k
  size += alignment + sizeof(uint8_t);
173
62.4k
  ptr = opj_malloc(size);
174
62.4k
  if (ptr == NULL)
175
61
    return NULL;
176
62.4k
  off = alignment-(((int)(intptr_t)ptr) & (alignment - 1));
177
62.4k
  ptr[off-1] = (uint8_t)off;
178
62.4k
  return ptr + off;
179
62.4k
}
180
181
void * opj_aligned_malloc(size_t size)
182
62.2k
{
183
62.2k
  return opj_aligned_malloc_n(16, size);
184
62.2k
}
185
186
void * opj_aligned_32_malloc(size_t size)
187
269
{
188
269
  return opj_aligned_malloc_n(32, size);
189
269
}
190
191
void opj_aligned_free(void* ptr_)
192
17.2M
{
193
17.2M
  uint8_t *ptr = (uint8_t *)ptr_;
194
17.2M
  uint8_t off;
195
17.2M
  if (ptr == NULL)
196
17.1M
    return;
197
198
62.4k
  off = ptr[-1];
199
62.4k
  opj_free((void *)(((unsigned char *)ptr) - off));
200
62.4k
}
201
202
#if 0
203
/* UNUSED currently, and moderately tricky, so deferred until required */
204
void * opj_aligned_realloc(void *ptr, size_t size)
205
{
206
  return opj_realloc(ptr, size);
207
}
208
#endif
209
210
static void fz_opj_error_callback(const char *msg, void *client_data)
211
4.37k
{
212
4.37k
  fz_context *ctx = (fz_context *)client_data;
213
4.37k
  char buf[200];
214
4.37k
  size_t n;
215
4.37k
  fz_strlcpy(buf, msg, sizeof buf);
216
4.37k
  n = strlen(buf);
217
4.37k
  if (buf[n-1] == '\n')
218
4.37k
    buf[n-1] = 0;
219
4.37k
  fz_warn(ctx, "openjpeg error: %s", buf);
220
4.37k
}
221
222
static void fz_opj_warning_callback(const char *msg, void *client_data)
223
10.6M
{
224
10.6M
  fz_context *ctx = (fz_context *)client_data;
225
10.6M
  char buf[200];
226
10.6M
  size_t n;
227
10.6M
  fz_strlcpy(buf, msg, sizeof buf);
228
10.6M
  n = strlen(buf);
229
10.6M
  if (buf[n-1] == '\n')
230
10.6M
    buf[n-1] = 0;
231
10.6M
  fz_warn(ctx, "openjpeg warning: %s", buf);
232
10.6M
}
233
234
static void fz_opj_info_callback(const char *msg, void *client_data)
235
13.2k
{
236
  /* fz_warn("openjpeg info: %s", msg); */
237
13.2k
}
238
239
static OPJ_SIZE_T fz_opj_stream_read(void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
240
3.52k
{
241
3.52k
  stream_block *sb = (stream_block *)p_user_data;
242
3.52k
  OPJ_SIZE_T len;
243
244
3.52k
  len = sb->size - sb->pos;
245
3.52k
  if (len == 0)
246
215
    return (OPJ_SIZE_T)-1; /* End of file! */
247
3.30k
  if (len > p_nb_bytes)
248
0
    len = p_nb_bytes;
249
3.30k
  memcpy(p_buffer, sb->data + sb->pos, len);
250
3.30k
  sb->pos += len;
251
3.30k
  return len;
252
3.52k
}
253
254
static OPJ_OFF_T fz_opj_stream_skip(OPJ_OFF_T skip, void * p_user_data)
255
0
{
256
0
  stream_block *sb = (stream_block *)p_user_data;
257
258
0
  if (skip > (OPJ_OFF_T)(sb->size - sb->pos))
259
0
    skip = (OPJ_OFF_T)(sb->size - sb->pos);
260
0
  sb->pos += skip;
261
0
  return sb->pos;
262
0
}
263
264
static OPJ_BOOL fz_opj_stream_seek(OPJ_OFF_T seek_pos, void * p_user_data)
265
255
{
266
255
  stream_block *sb = (stream_block *)p_user_data;
267
268
255
  if (seek_pos > (OPJ_OFF_T)sb->size)
269
0
    return OPJ_FALSE;
270
255
  sb->pos = seek_pos;
271
255
  return OPJ_TRUE;
272
255
}
273
274
static int32_t
275
safe_mul32(fz_context *ctx, int32_t a, int32_t b)
276
1.66k
{
277
1.66k
  int64_t res = ((int64_t)a) * b;
278
1.66k
  int32_t res32 = (int32_t)res;
279
280
1.66k
  if ((res32) != res)
281
0
    fz_throw(ctx, FZ_ERROR_LIMIT, "Overflow while decoding jpx");
282
1.66k
  return res32;
283
1.66k
}
284
285
static int32_t
286
safe_mla32(fz_context *ctx, int32_t a, int32_t b, int32_t c)
287
1.66k
{
288
1.66k
  int64_t res = ((int64_t)a) * b + c;
289
1.66k
  int32_t res32 = (int32_t)res;
290
291
1.66k
  if ((res32) != res)
292
0
    fz_throw(ctx, FZ_ERROR_LIMIT, "Overflow while decoding jpx");
293
1.66k
  return res32;
294
1.66k
}
295
296
static inline void
297
template_copy_comp(unsigned char *dst0, int w, int h, int stride, const OPJ_INT32 *src, int32_t ox, int32_t oy, OPJ_UINT32 cdx, OPJ_UINT32 cdy, OPJ_UINT32 cw, OPJ_UINT32 ch, OPJ_UINT32 sgnd, OPJ_UINT32 prec, int comps)
298
831
{
299
831
  int x, y;
300
301
831
  for (y = ch; oy + cdy <= 0 && y > 0; y--)
302
0
  {
303
0
    oy += cdy;
304
0
    dst0 += cdy * stride;
305
0
    src += cw;
306
0
  }
307
1.95M
  for (; y > 0; y--)
308
1.95M
  {
309
1.95M
    int32_t dymin = oy;
310
1.95M
    int32_t dywid = cdy;
311
1.95M
    unsigned char *dst1 = dst0 + ox * comps;
312
1.95M
    int32_t oox = ox;
313
1.95M
    const OPJ_INT32 *src0 = src;
314
315
1.95M
    if (dymin < 0)
316
0
      dywid += dymin, dst1 -= dymin * stride, dymin = 0;
317
1.95M
    if (dymin >= h)
318
0
      break;
319
1.95M
    if (dymin + dywid > h)
320
121
      dywid = h - dymin;
321
322
1.95M
    for (x = cw; oox + cdx <= 0 && x > 0; x--)
323
0
    {
324
0
      oox += cdx;
325
0
      dst1 += cdx * comps;
326
0
      src0++;
327
0
    }
328
1.72G
    for (; x > 0; x--)
329
1.72G
    {
330
1.72G
      OPJ_INT32 v;
331
1.72G
      int32_t xx, yy;
332
1.72G
      int32_t dxmin = oox;
333
1.72G
      int32_t dxwid = cdx;
334
1.72G
      unsigned char *dst2;
335
336
1.72G
      v = *src0++;
337
338
1.72G
      if (sgnd)
339
9.46M
        v = v + (1 << (prec - 1));
340
1.72G
      if (prec > 8)
341
1.37G
        v = v >> (prec - 8);
342
349M
      else if (prec < 8)
343
4.29M
        v = v << (8 - prec);
344
345
1.72G
      if (dxmin < 0)
346
0
        dxwid += dxmin, dst1 -= dxmin * comps, dxmin = 0;
347
1.72G
      if (dxmin >= w)
348
0
        break;
349
1.72G
      if (dxmin + dxwid > w)
350
370k
        dxwid = w - dxmin;
351
352
1.72G
      dst2 = dst1;
353
6.91G
      for (yy = dywid; yy > 0; yy--)
354
5.19G
      {
355
5.19G
        unsigned char *dst3 = dst2;
356
15.0G
        for (xx = dxwid; xx > 0; xx--)
357
9.81G
        {
358
9.81G
          *dst3 = v;
359
9.81G
          dst3 += comps;
360
9.81G
        }
361
5.19G
        dst2 += stride;
362
5.19G
      }
363
1.72G
      dst1 += comps * cdx;
364
1.72G
      oox += cdx;
365
1.72G
    }
366
1.95M
    dst0 += stride * cdy;
367
1.95M
    src += cw;
368
1.95M
    oy += cdy;
369
1.95M
  }
370
831
}
371
372
static void
373
copy_jpx_to_pixmap(fz_context *ctx, fz_pixmap *img, opj_image_t *jpx)
374
701
{
375
701
  unsigned char *dst;
376
701
  int stride, comps;
377
701
  int w = img->w;
378
701
  int h = img->h;
379
701
  int k;
380
381
701
  stride = fz_pixmap_stride(ctx, img);
382
701
  comps = fz_pixmap_components(ctx, img);
383
701
  dst = fz_pixmap_samples(ctx, img);
384
385
1.53k
  for (k = 0; k < comps; k++)
386
831
  {
387
831
    opj_image_comp_t *comp = &(jpx->comps[k]);
388
831
    OPJ_UINT32 cdx = comp->dx;
389
831
    OPJ_UINT32 cdy = comp->dy;
390
831
    OPJ_UINT32 cw = comp->w;
391
831
    OPJ_UINT32 ch = comp->h;
392
831
    int32_t oy = safe_mul32(ctx, comp->y0, cdy) - jpx->y0;
393
831
    int32_t ox = safe_mul32(ctx, comp->x0, cdx) - jpx->x0;
394
831
    unsigned char *dst0 = dst + oy * stride;
395
831
    int prec = comp->prec;
396
831
    int sgnd = comp->sgnd;
397
398
831
    if (comp->data == NULL)
399
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "No data for JP2 image component %d", k);
400
401
831
    if (fz_colorspace_is_indexed(ctx, img->colorspace))
402
1
    {
403
1
      prec = 8; /* Don't do any scaling! */
404
1
      sgnd = 0;
405
1
    }
406
407
    /* Check that none of the following will overflow. */
408
831
    (void)safe_mla32(ctx, ch, cdy, oy);
409
831
    (void)safe_mla32(ctx, cw, cdx, ox);
410
411
831
    if (cdx == 1 && cdy == 1)
412
670
      template_copy_comp(dst0, w, h, stride, comp->data, ox, oy, 1 /*cdx*/, 1 /*cdy*/, cw, ch, sgnd, prec, comps);
413
161
    else
414
161
      template_copy_comp(dst0, w, h, stride, comp->data, ox, oy, cdx, cdy, cw, ch, sgnd, prec, comps);
415
831
    dst++;
416
831
  }
417
701
}
418
419
static fz_pixmap *
420
jpx_read_image(fz_context *ctx, fz_jpxd *state, const unsigned char *data, size_t size, fz_colorspace *defcs, int onlymeta)
421
3.13k
{
422
3.13k
  fz_pixmap *img = NULL;
423
3.13k
  opj_dparameters_t params;
424
3.13k
  opj_codec_t *codec;
425
3.13k
  opj_image_t *jpx;
426
3.13k
  opj_stream_t *stream;
427
3.13k
  OPJ_CODEC_FORMAT format;
428
3.13k
  int a, n, k;
429
3.13k
  int w, h;
430
3.13k
  stream_block sb;
431
3.13k
  OPJ_UINT32 i;
432
433
3.13k
  fz_var(img);
434
435
3.13k
  if (size < 2)
436
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "not enough data to determine image format");
437
438
  /* Check for SOC marker -- if found we have a bare J2K stream */
439
3.13k
  if (data[0] == 0xFF && data[1] == 0x4F)
440
1.77k
    format = OPJ_CODEC_J2K;
441
1.36k
  else
442
1.36k
    format = OPJ_CODEC_JP2;
443
444
3.13k
  opj_set_default_decoder_parameters(&params);
445
3.13k
  if (fz_colorspace_is_indexed(ctx, defcs))
446
3
    params.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
447
448
3.13k
  codec = opj_create_decompress(format);
449
3.13k
  opj_set_info_handler(codec, fz_opj_info_callback, ctx);
450
3.13k
  opj_set_warning_handler(codec, fz_opj_warning_callback, ctx);
451
3.13k
  opj_set_error_handler(codec, fz_opj_error_callback, ctx);
452
3.13k
  if (!opj_setup_decoder(codec, &params))
453
0
  {
454
0
    opj_destroy_codec(codec);
455
0
    fz_throw(ctx, FZ_ERROR_LIBRARY, "j2k decode failed");
456
0
  }
457
458
3.13k
  stream = opj_stream_default_create(OPJ_TRUE);
459
3.13k
  sb.data = data;
460
3.13k
  sb.pos = 0;
461
3.13k
  sb.size = size;
462
463
3.13k
  opj_stream_set_read_function(stream, fz_opj_stream_read);
464
3.13k
  opj_stream_set_skip_function(stream, fz_opj_stream_skip);
465
3.13k
  opj_stream_set_seek_function(stream, fz_opj_stream_seek);
466
3.13k
  opj_stream_set_user_data(stream, &sb, NULL);
467
  /* Set the length to avoid an assert */
468
3.13k
  opj_stream_set_user_data_length(stream, size);
469
470
3.13k
  if (!opj_read_header(stream, codec, &jpx))
471
905
  {
472
905
    opj_stream_destroy(stream);
473
905
    opj_destroy_codec(codec);
474
905
    fz_throw(ctx, FZ_ERROR_LIBRARY, "Failed to read JPX header");
475
905
  }
476
477
2.22k
  if (!opj_decode(codec, stream, jpx))
478
888
  {
479
888
    opj_stream_destroy(stream);
480
888
    opj_destroy_codec(codec);
481
888
    opj_image_destroy(jpx);
482
888
    fz_throw(ctx, FZ_ERROR_LIBRARY, "Failed to decode JPX image");
483
888
  }
484
485
1.34k
  opj_stream_destroy(stream);
486
1.34k
  opj_destroy_codec(codec);
487
488
  /* jpx should never be NULL here, but check anyway */
489
1.34k
  if (!jpx)
490
0
    fz_throw(ctx, FZ_ERROR_LIBRARY, "opj_decode failed");
491
492
  /* Count number of alpha and color channels */
493
1.34k
  n = a = 0;
494
2.83k
  for (i = 0; i < jpx->numcomps; ++i)
495
1.49k
  {
496
1.49k
    if (jpx->comps[i].alpha)
497
3
      ++a;
498
1.48k
    else
499
1.48k
      ++n;
500
1.49k
  }
501
502
1.49k
  for (k = 1; k < n + a; k++)
503
152
  {
504
152
    if (!jpx->comps[k].data)
505
0
    {
506
0
      opj_image_destroy(jpx);
507
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "image components are missing data");
508
0
    }
509
152
  }
510
511
1.34k
  w = state->width = jpx->x1 - jpx->x0;
512
1.34k
  h = state->height = jpx->y1 - jpx->y0;
513
1.34k
  state->xres = 72; /* openjpeg does not read the JPEG 2000 resc box */
514
1.34k
  state->yres = 72; /* openjpeg does not read the JPEG 2000 resc box */
515
516
1.34k
  if (w < 0 || h < 0)
517
0
  {
518
0
    opj_image_destroy(jpx);
519
0
    fz_throw(ctx, FZ_ERROR_LIMIT, "Unbelievable size for jpx");
520
0
  }
521
522
1.34k
  state->cs = NULL;
523
524
1.34k
  if (defcs)
525
701
  {
526
701
    if (defcs->n == n)
527
701
      state->cs = fz_keep_colorspace(ctx, defcs);
528
0
    else
529
0
      fz_warn(ctx, "jpx file and dict colorspace do not match");
530
701
  }
531
532
1.34k
#if FZ_ENABLE_ICC
533
1.34k
  if (!state->cs && jpx->icc_profile_buf && jpx->icc_profile_len > 0)
534
0
  {
535
0
    fz_buffer *cbuf = NULL;
536
0
    fz_var(cbuf);
537
538
0
    fz_try(ctx)
539
0
    {
540
0
      cbuf = fz_new_buffer_from_copied_data(ctx, jpx->icc_profile_buf, jpx->icc_profile_len);
541
0
      state->cs = fz_new_icc_colorspace(ctx, FZ_COLORSPACE_NONE, 0, NULL, cbuf);
542
0
    }
543
0
    fz_always(ctx)
544
0
      fz_drop_buffer(ctx, cbuf);
545
0
    fz_catch(ctx)
546
0
    {
547
0
      fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
548
0
      fz_report_error(ctx);
549
0
      fz_warn(ctx, "ignoring embedded ICC profile in JPX");
550
0
    }
551
552
0
    if (state->cs && state->cs->n != n)
553
0
    {
554
0
      fz_warn(ctx, "invalid number of components in ICC profile, ignoring ICC profile in JPX");
555
0
      fz_drop_colorspace(ctx, state->cs);
556
0
      state->cs = NULL;
557
0
    }
558
0
  }
559
1.34k
#endif
560
561
1.34k
  if (!state->cs)
562
639
  {
563
639
    switch (n)
564
639
    {
565
626
    case 1: state->cs = fz_keep_colorspace(ctx, fz_device_gray(ctx)); break;
566
10
    case 3: state->cs = fz_keep_colorspace(ctx, fz_device_rgb(ctx)); break;
567
0
    case 4: state->cs = fz_keep_colorspace(ctx, fz_device_cmyk(ctx)); break;
568
3
    default:
569
3
      {
570
3
        opj_image_destroy(jpx);
571
3
        fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported number of components: %d", n);
572
0
      }
573
639
    }
574
639
  }
575
576
1.33k
  if (onlymeta)
577
632
  {
578
632
    opj_image_destroy(jpx);
579
632
    return NULL;
580
632
  }
581
582
1.41k
  fz_try(ctx)
583
1.41k
  {
584
705
    a = !!a; /* ignore any superfluous alpha channels */
585
705
    img = fz_new_pixmap(ctx, state->cs, w, h, NULL, a);
586
705
    fz_clear_pixmap_with_value(ctx, img, 0);
587
705
    copy_jpx_to_pixmap(ctx, img, jpx);
588
589
705
    if (jpx->color_space == OPJ_CLRSPC_SYCC && n == 3 && a == 0)
590
0
      jpx_ycc_to_rgb(ctx, img, 1, 1);
591
705
    if (a)
592
0
      fz_premultiply_pixmap(ctx, img);
593
705
  }
594
1.41k
  fz_always(ctx)
595
705
  {
596
705
    fz_drop_colorspace(ctx, state->cs);
597
705
    opj_image_destroy(jpx);
598
705
  }
599
705
  fz_catch(ctx)
600
4
  {
601
4
    fz_drop_pixmap(ctx, img);
602
4
    fz_rethrow(ctx);
603
4
  }
604
605
701
  return img;
606
705
}
607
608
fz_pixmap *
609
fz_load_jpx(fz_context *ctx, const unsigned char *data, size_t size, fz_colorspace *defcs)
610
1.92k
{
611
1.92k
  fz_jpxd state = { 0 };
612
1.92k
  fz_pixmap *pix = NULL;
613
614
3.85k
  fz_try(ctx)
615
3.85k
  {
616
1.92k
    opj_lock(ctx);
617
1.92k
    pix = jpx_read_image(ctx, &state, data, size, defcs, 0);
618
1.92k
  }
619
3.85k
  fz_always(ctx)
620
1.92k
    opj_unlock(ctx);
621
1.92k
  fz_catch(ctx)
622
1.22k
    fz_rethrow(ctx);
623
624
701
  return pix;
625
1.92k
}
626
627
void
628
fz_load_jpx_info(fz_context *ctx, const unsigned char *data, size_t size, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
629
1.20k
{
630
1.20k
  fz_jpxd state = { 0 };
631
632
2.41k
  fz_try(ctx)
633
2.41k
  {
634
1.20k
    opj_lock(ctx);
635
1.20k
    jpx_read_image(ctx, &state, data, size, NULL, 1);
636
1.20k
  }
637
2.41k
  fz_always(ctx)
638
1.20k
    opj_unlock(ctx);
639
1.20k
  fz_catch(ctx)
640
574
    fz_rethrow(ctx);
641
642
632
  *cspacep = state.cs;
643
632
  *wp = state.width;
644
632
  *hp = state.height;
645
632
  *xresp = state.xres;
646
632
  *yresp = state.yres;
647
632
}
648
649
#else /* FZ_ENABLE_JPX */
650
651
fz_pixmap *
652
fz_load_jpx(fz_context *ctx, const unsigned char *data, size_t size, fz_colorspace *defcs)
653
{
654
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "JPX support disabled");
655
}
656
657
void
658
fz_load_jpx_info(fz_context *ctx, const unsigned char *data, size_t size, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
659
{
660
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "JPX support disabled");
661
}
662
663
#endif