1#include "mupdf/fitz.h"
2
3#ifdef HAVE_JPEGXR
4
5#include <math.h>
6#include <string.h>
7
8#include <jpegxr.h>
9
10struct info
11{
12 fz_context *ctx;
13
14 float xres, yres;
15 int width, height;
16 int format;
17 int has_alpha;
18 int has_premul;
19
20 int comps, stride;
21 unsigned char *samples;
22 fz_colorspace *cspace;
23};
24
25static const char *
26jxr_error_string(int rc)
27{
28 switch (rc)
29 {
30 case JXR_EC_OK: return "No error";
31 default:
32 case JXR_EC_ERROR: return "Unspecified error";
33 case JXR_EC_BADMAGIC: return "Stream lacks proper magic number";
34 case JXR_EC_FEATURE_NOT_IMPLEMENTED: return "Feature not implemented";
35 case JXR_EC_IO: return "Error reading/writing data";
36 case JXR_EC_BADFORMAT: return "Bad file format";
37 }
38}
39
40struct {
41 jxrc_t_pixelFormat format;
42 int comps;
43} pixelformats[] = {
44 {JXRC_FMT_BlackWhite, 1},
45 {JXRC_FMT_8bppGray, 1},
46 {JXRC_FMT_16bppGray, 1},
47 {JXRC_FMT_16bppGrayFixedPoint, 1},
48 {JXRC_FMT_16bppGrayHalf, 1},
49 {JXRC_FMT_32bppGrayFixedPoint, 1},
50 {JXRC_FMT_32bppGrayFloat, 1},
51 {JXRC_FMT_16bppBGR555, 3},
52 {JXRC_FMT_16bppBGR565, 3},
53 {JXRC_FMT_24bppBGR, 3},
54 {JXRC_FMT_24bppRGB, 3},
55 {JXRC_FMT_32bppBGR101010, 3},
56 {JXRC_FMT_32bppBGRA, 3},
57 {JXRC_FMT_32bppBGR, 3},
58 {JXRC_FMT_32bppPBGRA, 3},
59 {JXRC_FMT_48bppRGBFixedPoint, 3},
60 {JXRC_FMT_48bppRGBHalf, 3},
61 {JXRC_FMT_48bppRGB, 3},
62 {JXRC_FMT_64bppPRGBA, 3},
63 {JXRC_FMT_64bppRGBAFixedPoint, 3},
64 {JXRC_FMT_64bppRGBAHalf, 3},
65 {JXRC_FMT_64bppRGBA, 3},
66 {JXRC_FMT_64bppRGBFixedPoint, 3},
67 {JXRC_FMT_64bppRGBHalf, 3},
68 {JXRC_FMT_96bppRGBFixedPoint, 3},
69 {JXRC_FMT_128bppPRGBAFloat, 3},
70 {JXRC_FMT_128bppRGBAFixedPoint, 3},
71 {JXRC_FMT_128bppRGBAFloat, 3},
72 {JXRC_FMT_128bppRGBFixedPoint, 3},
73 {JXRC_FMT_128bppRGBFloat, 3},
74 {JXRC_FMT_32bppRGBE, 3},
75 {JXRC_FMT_32bppCMYK, 4},
76 {JXRC_FMT_40bppCMYKAlpha, 4},
77 {JXRC_FMT_64bppCMYK, 4},
78 {JXRC_FMT_80bppCMYKAlpha, 4},
79 {JXRC_FMT_24bpp3Channels, 3},
80 {JXRC_FMT_32bpp3ChannelsAlpha, 3},
81 {JXRC_FMT_32bpp4Channels, 4},
82 {JXRC_FMT_40bpp4ChannelsAlpha, 4},
83 {JXRC_FMT_40bpp5Channels, 5},
84 {JXRC_FMT_48bpp3Channels, 3},
85 {JXRC_FMT_48bpp5ChannelsAlpha, 5},
86 {JXRC_FMT_48bpp6Channels, 6},
87 {JXRC_FMT_56bpp6ChannelsAlpha, 6},
88 {JXRC_FMT_56bpp7Channels, 7},
89 {JXRC_FMT_64bpp3ChannelsAlpha, 3},
90 {JXRC_FMT_64bpp4Channels, 4},
91 {JXRC_FMT_64bpp7ChannelsAlpha, 7},
92 {JXRC_FMT_64bpp8Channels, 8},
93 {JXRC_FMT_72bpp8ChannelsAlpha, 8},
94 {JXRC_FMT_80bpp4ChannelsAlpha, 4},
95 {JXRC_FMT_80bpp5Channels, 5},
96 {JXRC_FMT_96bpp5ChannelsAlpha, 5},
97 {JXRC_FMT_96bpp6Channels, 6},
98 {JXRC_FMT_112bpp6ChannelsAlpha, 6},
99 {JXRC_FMT_112bpp7Channels, 7},
100 {JXRC_FMT_128bpp7ChannelsAlpha, 7},
101 {JXRC_FMT_128bpp8Channels, 8},
102 {JXRC_FMT_144bpp8ChannelsAlpha, 8},
103};
104
105static inline float
106float32_from_int32_bits(int v)
107{
108 return *((float*) &v);
109}
110
111static inline float
112float32_from_float16(int v)
113{
114 int s = (v >> 15) & 0x1;
115 int e = (v >> 10) & 0x1f;
116 int m = (v >> 0) & 0x3ff;
117 int i = (s << 31) | ((e - 15 + 127) << 23) | (m << 13);
118 return float32_from_int32_bits(i);
119}
120
121static inline float
122sRGB_from_scRGB(float v)
123{
124 if (v <= 0.0031308f)
125 return v * 12.92f;
126 return 1.055f * powf(v, 1.0f / 2.4f) - 0.055f;
127}
128
129static inline void
130jxr_unpack_sample(fz_context *ctx, struct info *info, jxr_image_t image, int *sp, unsigned char *dp)
131{
132 int k, bpc, comps, alpha;
133 float v;
134
135 if (info->format == JXRC_FMT_32bppRGBE)
136 {
137 dp[0] = sRGB_from_scRGB(ldexpf(sp[0], sp[3] - 128 - 8)) * 255 + 0.5f;
138 dp[1] = sRGB_from_scRGB(ldexpf(sp[1], sp[3] - 128 - 8)) * 255 + 0.5f;
139 dp[2] = sRGB_from_scRGB(ldexpf(sp[2], sp[3] - 128 - 8)) * 255 + 0.5f;
140 return;
141 }
142 if (info->format == JXRC_FMT_16bppBGR565)
143 {
144 dp[0] = sp[0] << 3;
145 dp[1] = sp[1] << 2;
146 dp[2] = sp[2] << 3;
147 return;
148 }
149
150 comps = fz_mini(fz_colorspace_n(ctx, info->cspace), jxr_get_IMAGE_CHANNELS(image));
151 alpha = jxr_get_ALPHACHANNEL_FLAG(image);
152 bpc = jxr_get_CONTAINER_BPC(image);
153
154 for (k = 0; k < comps + alpha; k++)
155 {
156 switch (bpc)
157 {
158 default: fz_throw(ctx, FZ_ERROR_GENERIC, "unknown sample type: %d", bpc);
159 case JXR_BD1WHITE1: dp[k] = sp[k] ? 255 : 0; break;
160 case JXR_BD1BLACK1: dp[k] = sp[k] ? 0 : 255; break;
161 case JXR_BD5: dp[k] = sp[k] << 3; break;
162 case JXR_BD8: dp[k] = sp[k]; break;
163 case JXR_BD10: dp[k] = sp[k] >> 2; break;
164 case JXR_BD16: dp[k] = sp[k] >> 8; break;
165
166 case JXR_BD16S:
167 v = sp[k] * (1.0f / (1 << 13));
168 goto decode_float32;
169 case JXR_BD32S:
170 v = sp[k] * (1.0f / (1 << 24));
171 goto decode_float32;
172 case JXR_BD16F:
173 v = float32_from_float16(sp[k]);
174 goto decode_float32;
175 case JXR_BD32F:
176 v = float32_from_int32_bits(sp[k]);
177 goto decode_float32;
178 decode_float32:
179 if (k < comps)
180 dp[k] = sRGB_from_scRGB(fz_clamp(v, 0, 1)) * 255 + 0.5f;
181 else
182 dp[k] = fz_clamp(v, 0, 1) * 255 + 0.5f;
183 break;
184 }
185 }
186}
187
188static inline void
189jxr_unpack_alpha_sample(fz_context *ctx, struct info *info, jxr_image_t image, int *sp, unsigned char *dp)
190{
191 int bpc = jxr_get_CONTAINER_BPC(image);
192 switch (bpc)
193 {
194 default: fz_throw(ctx, FZ_ERROR_GENERIC, "unknown alpha sample type: %d", bpc);
195 case JXR_BD8: dp[0] = sp[0]; break;
196 case JXR_BD10: dp[0] = sp[0] >> 2; break;
197 case JXR_BD16: dp[0] = sp[0] >> 8; break;
198
199 case JXR_BD16S:
200 dp[0] = fz_clamp(sp[0] * (1.0f / (1 << 13)), 0, 1) * 255 + 0.5f;
201 break;
202 case JXR_BD32S:
203 dp[0] = fz_clamp(sp[0] * (1.0f / (1 << 24)), 0, 1) * 255 + 0.5f;
204 break;
205 case JXR_BD16F:
206 dp[0] = fz_clamp(float32_from_float16(sp[0]), 0, 1) * 255 + 0.5f;
207 break;
208 case JXR_BD32F:
209 dp[0] = fz_clamp(float32_from_int32_bits(sp[0]), 0, 1) * 255 + 0.5f;
210 break;
211 }
212}
213
214static void
215jxr_decode_block(jxr_image_t image, int mx, int my, int *data)
216{
217 struct info *info = jxr_get_user_data(image);
218 fz_context *ctx = info->ctx;
219 unsigned char *p;
220 int x, y, n1;
221
222 mx *= 16;
223 my *= 16;
224
225 n1 = fz_colorspace_n(ctx, info->cspace) + 1;
226 for (y = 0; y < 16; y++)
227 {
228 if ((my + y) >= info->height)
229 return;
230
231 p = info->samples + (my + y) * info->stride + mx * n1;
232
233 for (x = 0; x < 16; x++)
234 {
235 if ((mx + x) < info->width)
236 {
237 jxr_unpack_sample(ctx, info, image, data, p);
238 p += n1;
239 }
240
241 data += jxr_get_IMAGE_CHANNELS(image) + jxr_get_ALPHACHANNEL_FLAG(image);
242 data += (info->format == JXRC_FMT_32bppRGBE ? 1 : 0);
243 }
244 }
245}
246
247static void
248jxr_decode_block_alpha(jxr_image_t image, int mx, int my, int *data)
249{
250 struct info *info = jxr_get_user_data(image);
251 fz_context *ctx = info->ctx;
252 unsigned char *p;
253 int x, y, n;
254
255 mx *= 16;
256 my *= 16;
257
258 n = fz_colorspace_n(ctx, info->cspace);
259 for (y = 0; y < 16; y++)
260 {
261 if ((my + y) >= info->height)
262 return;
263
264 p = info->samples + (my + y) * info->stride + mx * (n + 1);
265
266 for (x = 0; x < 16; x++)
267 {
268 if ((mx + x) < info->width)
269 {
270 jxr_unpack_alpha_sample(ctx, info, image, data, p + n);
271 p += n + 1;
272 }
273
274 data++;
275 }
276 }
277}
278
279static void
280jxr_read_image(fz_context *ctx, const unsigned char *data, int size, struct info *info, int only_metadata)
281{
282 jxr_container_t container;
283 jxr_image_t image = NULL;
284 jxr_image_t alpha = NULL;
285 int rc, i;
286
287 fz_var(image);
288 fz_var(alpha);
289
290 fz_try(ctx)
291 {
292 container = jxr_create_container();
293
294 rc = jxr_read_image_container_memory(container, (unsigned char *)data, size);
295 if (rc < 0)
296 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot read jxr image container: %s", jxr_error_string(rc));
297
298 info->xres = jxrc_width_resolution(container, 0);
299 info->yres = jxrc_height_resolution(container, 0);
300 info->width = jxrc_image_width(container, 0);
301 info->height = jxrc_image_height(container, 0);
302
303 info->format = jxrc_image_pixelformat(container, 0);
304
305 for (i = 0; i < nelem(pixelformats); i++)
306 if (pixelformats[i].format == info->format)
307 {
308 info->comps = pixelformats[i].comps;
309 break;
310 }
311 if (i == nelem(pixelformats))
312 fz_throw(ctx, FZ_ERROR_GENERIC, "unsupported pixel format: %u", info->format);
313
314 if (info->comps == 1)
315 info->cspace = fz_device_gray(ctx);
316 else if (info->comps == 3)
317 info->cspace = fz_device_rgb(ctx);
318 else if (info->comps >= 4)
319 info->cspace = fz_device_cmyk(ctx);
320
321 info->stride = info->width * (fz_colorspace_n(ctx, info->cspace) + 1);
322
323 if (!only_metadata)
324 {
325 unsigned long image_offset;
326 unsigned char image_band;
327 unsigned long alpha_offset;
328 unsigned char alpha_band;
329
330 info->ctx = ctx;
331 info->samples = fz_malloc(ctx, info->stride * info->height);
332 memset(info->samples, 0xff, info->stride * info->height);
333
334 image_offset = jxrc_image_offset(container, 0);
335 image_band = jxrc_image_band_presence(container, 0);
336 alpha_offset = jxrc_alpha_offset(container, 0);
337 alpha_band = jxrc_alpha_band_presence(container, 0);
338
339 image = jxr_create_input();
340
341 jxr_set_PROFILE_IDC(image, 111);
342 jxr_set_LEVEL_IDC(image, 255);
343 jxr_set_pixel_format(image, info->format);
344 jxr_set_container_parameters(image, info->format,
345 info->width, info->height, alpha_offset,
346 image_band, alpha_band, 0);
347
348 jxr_set_user_data(image, info);
349 jxr_set_block_output(image, jxr_decode_block);
350
351 rc = jxr_read_image_bitstream_memory(image, (unsigned char *)data + image_offset, size - image_offset);
352 if (rc < 0)
353 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot read jxr image: %s", jxr_error_string(rc));
354
355 if (info->format == JXRC_FMT_32bppPBGRA ||
356 info->format == JXRC_FMT_64bppPRGBA ||
357 info->format == JXRC_FMT_128bppPRGBAFloat)
358 info->has_premul = 1;
359
360 if (jxr_get_ALPHACHANNEL_FLAG(image))
361 info->has_alpha = 1;
362
363 if (alpha_offset > 0)
364 {
365 info->has_alpha = 1;
366
367 alpha = jxr_create_input();
368
369 jxr_set_PROFILE_IDC(alpha, 111);
370 jxr_set_LEVEL_IDC(alpha, 255);
371 jxr_set_pixel_format(alpha, info->format);
372 jxr_set_container_parameters(alpha, info->format,
373 info->width, info->height, alpha_offset,
374 image_band, alpha_band, 1);
375
376 jxr_set_user_data(alpha, info);
377 jxr_set_block_output(alpha, jxr_decode_block_alpha);
378
379 rc = jxr_read_image_bitstream_memory(alpha, (unsigned char *)data + alpha_offset, size - alpha_offset);
380 if (rc < 0)
381 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot read jxr image: %s", jxr_error_string(rc));
382 }
383 }
384 }
385 fz_always(ctx)
386 {
387 if (alpha)
388 jxr_destroy(alpha);
389 if (image)
390 jxr_destroy(image);
391 jxr_destroy_container(container);
392 }
393 fz_catch(ctx)
394 {
395 fz_rethrow(ctx);
396 }
397}
398
399fz_pixmap *
400fz_load_jxr(fz_context *ctx, const unsigned char *data, size_t size)
401{
402 struct info info = { 0 };
403 fz_pixmap *image = NULL;
404
405 fz_var(image);
406
407 fz_try(ctx)
408 {
409 jxr_read_image(ctx, data, size, &info, 0);
410
411 image = fz_new_pixmap(ctx, info.cspace, info.width, info.height, NULL, 1);
412
413 image->xres = info.xres;
414 image->yres = info.yres;
415
416 fz_unpack_tile(ctx, image, info.samples, fz_colorspace_n(ctx, info.cspace) + 1, 8, info.stride, 0);
417 if (info.has_alpha && !info.has_premul)
418 fz_premultiply_pixmap(ctx, image);
419 }
420 fz_always(ctx)
421 {
422 fz_free(ctx, info.samples);
423 }
424 fz_catch(ctx)
425 {
426 fz_drop_pixmap(ctx, image);
427 fz_rethrow(ctx);
428 }
429
430 return image;
431}
432
433void
434fz_load_jxr_info(fz_context *ctx, const unsigned char *data, size_t size, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
435{
436 struct info info = { 0 };
437
438 jxr_read_image(ctx, data, size, &info, 1);
439 *cspacep = fz_keep_colorspace(ctx, info.cspace); /* info.cspace is a borrowed device colorspace */
440 *wp = info.width;
441 *hp = info.height;
442 *xresp = info.xres;
443 *yresp = info.yres;
444}
445#else /* HAVE_JPEGXR */
446
447fz_pixmap *
448fz_load_jxr(fz_context *ctx, const unsigned char *data, size_t size)
449{
450 fz_throw(ctx, FZ_ERROR_GENERIC, "JPEG-XR codec is not available");
451}
452
453void
454fz_load_jxr_info(fz_context *ctx, const unsigned char *data, size_t size, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
455{
456 fz_throw(ctx, FZ_ERROR_GENERIC, "JPEG-XR codec is not available");
457}
458
459#endif /* HAVE_JPEGXR */
460