Coverage Report

Created: 2023-06-07 06:20

/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
4.48k
{
106
4.48k
  opj_secret = ctx;
107
4.48k
}
108
109
static fz_context *get_opj_context(void)
110
21.0M
{
111
21.0M
  return opj_secret;
112
21.0M
}
113
114
void opj_lock(fz_context *ctx)
115
2.24k
{
116
2.24k
  fz_lock(ctx, FZ_LOCK_FREETYPE);
117
118
2.24k
  set_opj_context(ctx);
119
2.24k
}
120
121
void opj_unlock(fz_context *ctx)
122
2.24k
{
123
2.24k
  set_opj_context(NULL);
124
125
2.24k
  fz_unlock(ctx, FZ_LOCK_FREETYPE);
126
2.24k
}
127
128
void *opj_malloc(size_t size)
129
4.17M
{
130
4.17M
  fz_context *ctx = get_opj_context();
131
132
4.17M
  assert(ctx != NULL);
133
134
4.17M
  return Memento_label(fz_malloc_no_throw(ctx, size), "opj_malloc");
135
4.17M
}
136
137
void *opj_calloc(size_t n, size_t size)
138
5.81M
{
139
5.81M
  fz_context *ctx = get_opj_context();
140
141
5.81M
  assert(ctx != NULL);
142
143
5.81M
  return fz_calloc_no_throw(ctx, n, size);
144
5.81M
}
145
146
void *opj_realloc(void *ptr, size_t size)
147
629k
{
148
629k
  fz_context *ctx = get_opj_context();
149
150
629k
  assert(ctx != NULL);
151
152
629k
  return fz_realloc_no_throw(ctx, ptr, size);
153
629k
}
154
155
void opj_free(void *ptr)
156
10.3M
{
157
10.3M
  fz_context *ctx = get_opj_context();
158
159
10.3M
  assert(ctx != NULL);
160
161
10.3M
  fz_free(ctx, ptr);
162
10.3M
}
163
164
static void * opj_aligned_malloc_n(size_t alignment, size_t size)
165
87.3k
{
166
87.3k
  uint8_t *ptr;
167
87.3k
  size_t off;
168
169
87.3k
  if (size == 0)
170
3
    return NULL;
171
172
87.3k
  size += alignment + sizeof(uint8_t);
173
87.3k
  ptr = opj_malloc(size);
174
87.3k
  if (ptr == NULL)
175
0
    return NULL;
176
87.3k
  off = alignment-(((int)(intptr_t)ptr) & (alignment - 1));
177
87.3k
  ptr[off-1] = (uint8_t)off;
178
87.3k
  return ptr + off;
179
87.3k
}
180
181
void * opj_aligned_malloc(size_t size)
182
87.0k
{
183
87.0k
  return opj_aligned_malloc_n(16, size);
184
87.0k
}
185
186
void * opj_aligned_32_malloc(size_t size)
187
322
{
188
322
  return opj_aligned_malloc_n(32, size);
189
322
}
190
191
void opj_aligned_free(void* ptr_)
192
1.35M
{
193
1.35M
  uint8_t *ptr = (uint8_t *)ptr_;
194
1.35M
  uint8_t off;
195
1.35M
  if (ptr == NULL)
196
1.27M
    return;
197
198
87.3k
  off = ptr[-1];
199
87.3k
  opj_free((void *)(((unsigned char *)ptr) - off));
200
87.3k
}
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
6.65k
{
212
6.65k
  fz_context *ctx = (fz_context *)client_data;
213
6.65k
  char buf[200];
214
6.65k
  size_t n;
215
6.65k
  fz_strlcpy(buf, msg, sizeof buf);
216
6.65k
  n = strlen(buf);
217
6.65k
  if (buf[n-1] == '\n')
218
6.65k
    buf[n-1] = 0;
219
6.65k
  fz_warn(ctx, "openjpeg error: %s", buf);
220
6.65k
}
221
222
static void fz_opj_warning_callback(const char *msg, void *client_data)
223
5.95M
{
224
5.95M
  fz_context *ctx = (fz_context *)client_data;
225
5.95M
  char buf[200];
226
5.95M
  size_t n;
227
5.95M
  fz_strlcpy(buf, msg, sizeof buf);
228
5.95M
  n = strlen(buf);
229
5.95M
  if (buf[n-1] == '\n')
230
5.95M
    buf[n-1] = 0;
231
5.95M
  fz_warn(ctx, "openjpeg warning: %s", buf);
232
5.95M
}
233
234
static void fz_opj_info_callback(const char *msg, void *client_data)
235
10.4k
{
236
  /* fz_warn("openjpeg info: %s", msg); */
237
10.4k
}
238
239
static OPJ_SIZE_T fz_opj_stream_read(void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
240
2.64k
{
241
2.64k
  stream_block *sb = (stream_block *)p_user_data;
242
2.64k
  OPJ_SIZE_T len;
243
244
2.64k
  len = sb->size - sb->pos;
245
2.64k
  if (len == 0)
246
34
    return (OPJ_SIZE_T)-1; /* End of file! */
247
2.61k
  if (len > p_nb_bytes)
248
0
    len = p_nb_bytes;
249
2.61k
  memcpy(p_buffer, sb->data + sb->pos, len);
250
2.61k
  sb->pos += len;
251
2.61k
  return len;
252
2.64k
}
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
513
{
266
513
  stream_block *sb = (stream_block *)p_user_data;
267
268
513
  if (seek_pos > (OPJ_OFF_T)sb->size)
269
0
    return OPJ_FALSE;
270
513
  sb->pos = seek_pos;
271
513
  return OPJ_TRUE;
272
513
}
273
274
static int32_t
275
safe_mul32(fz_context *ctx, int32_t a, int32_t b)
276
1.43k
{
277
1.43k
  int64_t res = ((int64_t)a) * b;
278
1.43k
  int32_t res32 = (int32_t)res;
279
280
1.43k
  if ((res32) != res)
281
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "Overflow while decoding jpx");
282
1.43k
  return res32;
283
1.43k
}
284
285
static int32_t
286
safe_mla32(fz_context *ctx, int32_t a, int32_t b, int32_t c)
287
1.43k
{
288
1.43k
  int64_t res = ((int64_t)a) * b + c;
289
1.43k
  int32_t res32 = (int32_t)res;
290
291
1.43k
  if ((res32) != res)
292
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "Overflow while decoding jpx");
293
1.43k
  return res32;
294
1.43k
}
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
715
{
299
715
  int x, y;
300
301
715
  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
286k
  for (; y > 0; y--)
308
286k
  {
309
286k
    int32_t dymin = oy;
310
286k
    int32_t dywid = cdy;
311
286k
    unsigned char *dst1 = dst0 + ox * comps;
312
286k
    int32_t oox = ox;
313
286k
    const OPJ_INT32 *src0 = src;
314
315
286k
    if (dymin < 0)
316
0
      dywid += dymin, dst1 -= dymin * stride, dymin = 0;
317
286k
    if (dymin >= h)
318
0
      break;
319
286k
    if (dymin + dywid > h)
320
20
      dywid = h - dymin;
321
322
286k
    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
122M
    for (; x > 0; x--)
329
121M
    {
330
121M
      OPJ_INT32 v;
331
121M
      int32_t xx, yy;
332
121M
      int32_t dxmin = oox;
333
121M
      int32_t dxwid = cdx;
334
121M
      unsigned char *dst2;
335
336
121M
      v = *src0++;
337
338
121M
      if (sgnd)
339
0
        v = v + (1 << (prec - 1));
340
121M
      if (prec > 8)
341
0
        v = v >> (prec - 8);
342
121M
      else if (prec < 8)
343
0
        v = v << (8 - prec);
344
345
121M
      if (dxmin < 0)
346
0
        dxwid += dxmin, dst1 -= dxmin * comps, dxmin = 0;
347
121M
      if (dxmin >= w)
348
0
        break;
349
121M
      if (dxmin + dxwid > w)
350
0
        dxwid = w - dxmin;
351
352
121M
      dst2 = dst1;
353
245M
      for (yy = dywid; yy > 0; yy--)
354
123M
      {
355
123M
        unsigned char *dst3 = dst2;
356
260M
        for (xx = dxwid; xx > 0; xx--)
357
137M
        {
358
137M
          *dst3 = v;
359
137M
          dst3 += comps;
360
137M
        }
361
123M
        dst2 += stride;
362
123M
      }
363
121M
      dst1 += comps * cdx;
364
121M
      oox += cdx;
365
121M
    }
366
286k
    dst0 += stride * cdy;
367
286k
    src += cw;
368
286k
    oy += cdy;
369
286k
  }
370
715
}
371
372
static void
373
copy_jpx_to_pixmap(fz_context *ctx, fz_pixmap *img, opj_image_t *jpx)
374
305
{
375
305
  unsigned char *dst;
376
305
  int stride, comps;
377
305
  int w = img->w;
378
305
  int h = img->h;
379
305
  int k;
380
381
305
  stride = fz_pixmap_stride(ctx, img);
382
305
  comps = fz_pixmap_components(ctx, img);
383
305
  dst = fz_pixmap_samples(ctx, img);
384
385
1.02k
  for (k = 0; k < comps; k++)
386
715
  {
387
715
    opj_image_comp_t *comp = &(jpx->comps[k]);
388
715
    OPJ_UINT32 cdx = comp->dx;
389
715
    OPJ_UINT32 cdy = comp->dy;
390
715
    OPJ_UINT32 cw = comp->w;
391
715
    OPJ_UINT32 ch = comp->h;
392
715
    int32_t oy = safe_mul32(ctx, comp->y0, cdy) - jpx->y0;
393
715
    int32_t ox = safe_mul32(ctx, comp->x0, cdx) - jpx->x0;
394
715
    unsigned char *dst0 = dst + oy * stride;
395
715
    int prec = comp->prec;
396
715
    int sgnd = comp->sgnd;
397
398
715
    if (comp->data == NULL)
399
0
      fz_throw(ctx, FZ_ERROR_GENERIC, "No data for JP2 image component %d", k);
400
401
715
    if (fz_colorspace_is_indexed(ctx, img->colorspace))
402
0
    {
403
0
      prec = 8; /* Don't do any scaling! */
404
0
      sgnd = 0;
405
0
    }
406
407
    /* Check that none of the following will overflow. */
408
715
    (void)safe_mla32(ctx, ch, cdy, oy);
409
715
    (void)safe_mla32(ctx, cw, cdx, ox);
410
411
715
    if (cdx == 1 && cdy == 1)
412
695
      template_copy_comp(dst0, w, h, stride, comp->data, ox, oy, 1 /*cdx*/, 1 /*cdy*/, cw, ch, sgnd, prec, comps);
413
20
    else
414
20
      template_copy_comp(dst0, w, h, stride, comp->data, ox, oy, cdx, cdy, cw, ch, sgnd, prec, comps);
415
715
    dst++;
416
715
  }
417
305
}
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
2.24k
{
422
2.24k
  fz_pixmap *img = NULL;
423
2.24k
  opj_dparameters_t params;
424
2.24k
  opj_codec_t *codec;
425
2.24k
  opj_image_t *jpx;
426
2.24k
  opj_stream_t *stream;
427
2.24k
  OPJ_CODEC_FORMAT format;
428
2.24k
  int a, n, k;
429
2.24k
  int w, h;
430
2.24k
  stream_block sb;
431
2.24k
  OPJ_UINT32 i;
432
433
2.24k
  fz_var(img);
434
435
2.24k
  if (size < 2)
436
1
    fz_throw(ctx, FZ_ERROR_GENERIC, "not enough data to determine image format");
437
438
  /* Check for SOC marker -- if found we have a bare J2K stream */
439
2.24k
  if (data[0] == 0xFF && data[1] == 0x4F)
440
1
    format = OPJ_CODEC_J2K;
441
2.24k
  else
442
2.24k
    format = OPJ_CODEC_JP2;
443
444
2.24k
  opj_set_default_decoder_parameters(&params);
445
2.24k
  if (fz_colorspace_is_indexed(ctx, defcs))
446
0
    params.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
447
448
2.24k
  codec = opj_create_decompress(format);
449
2.24k
  opj_set_info_handler(codec, fz_opj_info_callback, ctx);
450
2.24k
  opj_set_warning_handler(codec, fz_opj_warning_callback, ctx);
451
2.24k
  opj_set_error_handler(codec, fz_opj_error_callback, ctx);
452
2.24k
  if (!opj_setup_decoder(codec, &params))
453
0
  {
454
0
    opj_destroy_codec(codec);
455
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "j2k decode failed");
456
0
  }
457
458
2.24k
  stream = opj_stream_default_create(OPJ_TRUE);
459
2.24k
  sb.data = data;
460
2.24k
  sb.pos = 0;
461
2.24k
  sb.size = size;
462
463
2.24k
  opj_stream_set_read_function(stream, fz_opj_stream_read);
464
2.24k
  opj_stream_set_skip_function(stream, fz_opj_stream_skip);
465
2.24k
  opj_stream_set_seek_function(stream, fz_opj_stream_seek);
466
2.24k
  opj_stream_set_user_data(stream, &sb, NULL);
467
  /* Set the length to avoid an assert */
468
2.24k
  opj_stream_set_user_data_length(stream, size);
469
470
2.24k
  if (!opj_read_header(stream, codec, &jpx))
471
207
  {
472
207
    opj_stream_destroy(stream);
473
207
    opj_destroy_codec(codec);
474
207
    fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to read JPX header");
475
207
  }
476
477
2.03k
  if (!opj_decode(codec, stream, jpx))
478
1.72k
  {
479
1.72k
    opj_stream_destroy(stream);
480
1.72k
    opj_destroy_codec(codec);
481
1.72k
    opj_image_destroy(jpx);
482
1.72k
    fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to decode JPX image");
483
1.72k
  }
484
485
305
  opj_stream_destroy(stream);
486
305
  opj_destroy_codec(codec);
487
488
  /* jpx should never be NULL here, but check anyway */
489
305
  if (!jpx)
490
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "opj_decode failed");
491
492
  /* Count number of alpha and color channels */
493
305
  n = a = 0;
494
1.02k
  for (i = 0; i < jpx->numcomps; ++i)
495
715
  {
496
715
    if (jpx->comps[i].alpha)
497
0
      ++a;
498
715
    else
499
715
      ++n;
500
715
  }
501
502
715
  for (k = 1; k < n + a; k++)
503
410
  {
504
410
    if (!jpx->comps[k].data)
505
0
    {
506
0
      opj_image_destroy(jpx);
507
0
      fz_throw(ctx, FZ_ERROR_GENERIC, "image components are missing data");
508
0
    }
509
410
  }
510
511
305
  w = state->width = jpx->x1 - jpx->x0;
512
305
  h = state->height = jpx->y1 - jpx->y0;
513
305
  state->xres = 72; /* openjpeg does not read the JPEG 2000 resc box */
514
305
  state->yres = 72; /* openjpeg does not read the JPEG 2000 resc box */
515
516
305
  if (w < 0 || h < 0)
517
0
  {
518
0
    opj_image_destroy(jpx);
519
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "Unbelievable size for jpx");
520
0
  }
521
522
305
  state->cs = NULL;
523
524
305
  if (defcs)
525
282
  {
526
282
    if (defcs->n == n)
527
282
      state->cs = fz_keep_colorspace(ctx, defcs);
528
0
    else
529
0
      fz_warn(ctx, "jpx file and dict colorspace do not match");
530
282
  }
531
532
305
#if FZ_ENABLE_ICC
533
305
  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
      fz_warn(ctx, "ignoring embedded ICC profile in JPX");
547
548
0
    if (state->cs && state->cs->n != n)
549
0
    {
550
0
      fz_warn(ctx, "invalid number of components in ICC profile, ignoring ICC profile in JPX");
551
0
      fz_drop_colorspace(ctx, state->cs);
552
0
      state->cs = NULL;
553
0
    }
554
0
  }
555
305
#endif
556
557
305
  if (!state->cs)
558
23
  {
559
23
    switch (n)
560
23
    {
561
1
    case 1: state->cs = fz_keep_colorspace(ctx, fz_device_gray(ctx)); break;
562
22
    case 3: state->cs = fz_keep_colorspace(ctx, fz_device_rgb(ctx)); break;
563
0
    case 4: state->cs = fz_keep_colorspace(ctx, fz_device_cmyk(ctx)); break;
564
0
    default:
565
0
      {
566
0
        opj_image_destroy(jpx);
567
0
        fz_throw(ctx, FZ_ERROR_GENERIC, "unsupported number of components: %d", n);
568
0
      }
569
23
    }
570
23
  }
571
572
305
  if (onlymeta)
573
0
  {
574
0
    opj_image_destroy(jpx);
575
0
    return NULL;
576
0
  }
577
578
610
  fz_try(ctx)
579
610
  {
580
305
    a = !!a; /* ignore any superfluous alpha channels */
581
305
    img = fz_new_pixmap(ctx, state->cs, w, h, NULL, a);
582
305
    fz_clear_pixmap_with_value(ctx, img, 0);
583
305
    copy_jpx_to_pixmap(ctx, img, jpx);
584
585
305
    if (jpx->color_space == OPJ_CLRSPC_SYCC && n == 3 && a == 0)
586
0
      jpx_ycc_to_rgb(ctx, img, 1, 1);
587
305
    if (a)
588
0
      fz_premultiply_pixmap(ctx, img);
589
305
  }
590
610
  fz_always(ctx)
591
305
  {
592
305
    fz_drop_colorspace(ctx, state->cs);
593
305
    opj_image_destroy(jpx);
594
305
  }
595
305
  fz_catch(ctx)
596
0
  {
597
0
    fz_drop_pixmap(ctx, img);
598
0
    fz_rethrow(ctx);
599
0
  }
600
601
305
  return img;
602
305
}
603
604
fz_pixmap *
605
fz_load_jpx(fz_context *ctx, const unsigned char *data, size_t size, fz_colorspace *defcs)
606
2.24k
{
607
2.24k
  fz_jpxd state = { 0 };
608
2.24k
  fz_pixmap *pix = NULL;
609
610
4.48k
  fz_try(ctx)
611
4.48k
  {
612
2.24k
    opj_lock(ctx);
613
2.24k
    pix = jpx_read_image(ctx, &state, data, size, defcs, 0);
614
2.24k
  }
615
4.48k
  fz_always(ctx)
616
2.24k
    opj_unlock(ctx);
617
2.24k
  fz_catch(ctx)
618
1.93k
    fz_rethrow(ctx);
619
620
305
  return pix;
621
2.24k
}
622
623
void
624
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)
625
1
{
626
1
  fz_jpxd state = { 0 };
627
628
2
  fz_try(ctx)
629
2
  {
630
1
    opj_lock(ctx);
631
1
    jpx_read_image(ctx, &state, data, size, NULL, 1);
632
1
  }
633
2
  fz_always(ctx)
634
1
    opj_unlock(ctx);
635
1
  fz_catch(ctx)
636
1
    fz_rethrow(ctx);
637
638
0
  *cspacep = state.cs;
639
0
  *wp = state.width;
640
0
  *hp = state.height;
641
0
  *xresp = state.xres;
642
0
  *yresp = state.yres;
643
0
}
644
645
#else /* FZ_ENABLE_JPX */
646
647
fz_pixmap *
648
fz_load_jpx(fz_context *ctx, const unsigned char *data, size_t size, fz_colorspace *defcs)
649
{
650
  fz_throw(ctx, FZ_ERROR_GENERIC, "JPX support disabled");
651
}
652
653
void
654
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)
655
{
656
  fz_throw(ctx, FZ_ERROR_GENERIC, "JPX support disabled");
657
}
658
659
#endif