/src/ghostpdl/base/sjpx_openjpeg.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2021 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato, |
13 | | CA 94945, U.S.A., +1(415)492-9861, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | |
18 | | /* opj filter implementation using OpenJPeg library */ |
19 | | |
20 | | #include "memory_.h" |
21 | | #include "gserrors.h" |
22 | | #include "gdebug.h" |
23 | | #include "strimpl.h" |
24 | | #include "sjpx_openjpeg.h" |
25 | | #include "gxsync.h" |
26 | | #include "assert_.h" |
27 | | #if !defined(SHARE_JPX) || (SHARE_JPX == 0) |
28 | | #include "opj_malloc.h" |
29 | | #endif |
30 | | /* Some locking to get around the criminal lack of context |
31 | | * in the openjpeg library. */ |
32 | | #if !defined(SHARE_JPX) || (SHARE_JPX == 0) |
33 | | static gs_memory_t *opj_memory; |
34 | | #endif |
35 | | |
36 | | int sjpxd_create(gs_memory_t *mem) |
37 | 89.2k | { |
38 | 89.2k | #if !defined(SHARE_JPX) || (SHARE_JPX == 0) |
39 | 89.2k | gs_lib_ctx_t *ctx = mem->gs_lib_ctx; |
40 | | |
41 | 89.2k | ctx->sjpxd_private = gx_monitor_label(gx_monitor_alloc(mem), "sjpxd_monitor"); |
42 | 89.2k | if (ctx->sjpxd_private == NULL) |
43 | 0 | return gs_error_VMerror; |
44 | 89.2k | #endif |
45 | 89.2k | return 0; |
46 | 89.2k | } |
47 | | |
48 | | void sjpxd_destroy(gs_memory_t *mem) |
49 | 89.2k | { |
50 | 89.2k | #if !defined(SHARE_JPX) || (SHARE_JPX == 0) |
51 | 89.2k | gs_lib_ctx_t *ctx = mem->gs_lib_ctx; |
52 | | |
53 | 89.2k | gx_monitor_free((gx_monitor_t *)ctx->sjpxd_private); |
54 | 89.2k | ctx->sjpxd_private = NULL; |
55 | 89.2k | #endif |
56 | 89.2k | } |
57 | | |
58 | | static int opj_lock(gs_memory_t *mem) |
59 | 146k | { |
60 | 146k | #if !defined(SHARE_JPX) || (SHARE_JPX == 0) |
61 | 146k | int ret; |
62 | | |
63 | 146k | gs_lib_ctx_t *ctx = mem->gs_lib_ctx; |
64 | | |
65 | 146k | ret = gx_monitor_enter((gx_monitor_t *)ctx->sjpxd_private); |
66 | 146k | assert(opj_memory == NULL); |
67 | 146k | opj_memory = mem->non_gc_memory; |
68 | 146k | return ret; |
69 | | #else |
70 | | return 0; |
71 | | #endif |
72 | 146k | } |
73 | | |
74 | | static int opj_unlock(gs_memory_t *mem) |
75 | 146k | { |
76 | 146k | #if !defined(SHARE_JPX) || (SHARE_JPX == 0) |
77 | 146k | gs_lib_ctx_t *ctx = mem->gs_lib_ctx; |
78 | | |
79 | 146k | assert(opj_memory != NULL); |
80 | 146k | opj_memory = NULL; |
81 | 146k | return gx_monitor_leave((gx_monitor_t *)ctx->sjpxd_private); |
82 | | #else |
83 | | return 0; |
84 | | #endif |
85 | 146k | } |
86 | | |
87 | | #if !defined(SHARE_JPX) || (SHARE_JPX == 0) |
88 | | /* Allocation routines that use the memory pointer given above */ |
89 | | void *opj_malloc(size_t size) |
90 | 2.13M | { |
91 | 2.13M | if (size == 0) |
92 | 0 | return NULL; |
93 | | |
94 | 2.13M | assert(opj_memory != NULL); |
95 | | |
96 | 2.13M | if (size > (size_t) ARCH_MAX_UINT) |
97 | 0 | return NULL; |
98 | | |
99 | 2.13M | return (void *)gs_alloc_bytes(opj_memory, size, "opj_malloc"); |
100 | 2.13M | } |
101 | | |
102 | | void *opj_calloc(size_t n, size_t size) |
103 | 1.18M | { |
104 | 1.18M | void *ptr; |
105 | | |
106 | | /* FIXME: Check for overflow? */ |
107 | 1.18M | size *= n; |
108 | | |
109 | 1.18M | ptr = opj_malloc(size); |
110 | 1.18M | if (ptr) |
111 | 1.18M | memset(ptr, 0, size); |
112 | 1.18M | return ptr; |
113 | 1.18M | } |
114 | | |
115 | | void *opj_realloc(void *ptr, size_t size) |
116 | 276k | { |
117 | 276k | if (ptr == NULL) |
118 | 116k | return opj_malloc(size); |
119 | | |
120 | 159k | if (size == 0) |
121 | 0 | { |
122 | 0 | opj_free(ptr); |
123 | 0 | return NULL; |
124 | 0 | } |
125 | | |
126 | 159k | return gs_resize_object(opj_memory, ptr, size, "opj_malloc"); |
127 | 159k | } |
128 | | |
129 | | void opj_free(void *ptr) |
130 | 2.14M | { |
131 | 2.14M | gs_free_object(opj_memory, ptr, "opj_malloc"); |
132 | 2.14M | } |
133 | | |
134 | | static inline void * opj_aligned_malloc_n(size_t size, size_t align) |
135 | 103k | { |
136 | 103k | uint8_t *ptr; |
137 | 103k | int off; |
138 | | |
139 | 103k | if (size == 0) |
140 | 0 | return NULL; |
141 | | |
142 | 103k | size += align + sizeof(uint8_t); |
143 | 103k | ptr = opj_malloc(size); |
144 | 103k | if (ptr == NULL) |
145 | 0 | return NULL; |
146 | 103k | off = align - (((int)(intptr_t)ptr) & (align - 1)); |
147 | 103k | ptr[off-1] = off; |
148 | 103k | return ptr + off; |
149 | 103k | } |
150 | | |
151 | | void * opj_aligned_malloc(size_t size) |
152 | 103k | { |
153 | 103k | return opj_aligned_malloc_n(size, 16); |
154 | 103k | } |
155 | | |
156 | | void *opj_aligned_32_malloc(size_t size) |
157 | 576 | { |
158 | 576 | return opj_aligned_malloc_n(size, 32); |
159 | 576 | } |
160 | | |
161 | | void opj_aligned_free(void* ptr_) |
162 | 636k | { |
163 | 636k | uint8_t *ptr = (uint8_t *)ptr_; |
164 | 636k | uint8_t off; |
165 | 636k | if (ptr == NULL) |
166 | 532k | return; |
167 | | |
168 | 103k | off = ptr[-1]; |
169 | 103k | opj_free((void *)(((unsigned char *)ptr) - off)); |
170 | 103k | } |
171 | | |
172 | | #if 0 |
173 | | /* UNUSED currently, and moderately tricky, so deferred until required */ |
174 | | void * opj_aligned_realloc(void *ptr, size_t size) |
175 | | { |
176 | | return opj_realloc(ptr, size); |
177 | | } |
178 | | #endif |
179 | | #endif |
180 | | |
181 | | gs_private_st_simple(st_jpxd_state, stream_jpxd_state, |
182 | | "JPXDecode filter state"); /* creates a gc object for our state, |
183 | | defined in sjpx.h */ |
184 | | |
185 | | static int s_opjd_accumulate_input(stream_jpxd_state *state, stream_cursor_read * pr); |
186 | | |
187 | | static OPJ_SIZE_T sjpx_stream_read(void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data) |
188 | 4.61k | { |
189 | 4.61k | stream_block *sb = (stream_block *)p_user_data; |
190 | 4.61k | OPJ_SIZE_T len; |
191 | | |
192 | 4.61k | len = sb->fill - sb->pos; |
193 | 4.61k | if (sb->fill < sb->pos) |
194 | 0 | len = 0; |
195 | 4.61k | if (len == 0) |
196 | 418 | return (OPJ_SIZE_T)-1; /* End of file! */ |
197 | 4.19k | if ((OPJ_SIZE_T)len > p_nb_bytes) |
198 | 0 | len = p_nb_bytes; |
199 | 4.19k | memcpy(p_buffer, sb->data + sb->pos, len); |
200 | 4.19k | sb->pos += len; |
201 | 4.19k | return len; |
202 | 4.61k | } |
203 | | |
204 | | static OPJ_OFF_T sjpx_stream_skip(OPJ_OFF_T skip, void * p_user_data) |
205 | 57 | { |
206 | 57 | stream_block *sb = (stream_block *)p_user_data; |
207 | | |
208 | 57 | if (skip > sb->fill - sb->pos) |
209 | 57 | skip = sb->fill - sb->pos; |
210 | 57 | sb->pos += skip; |
211 | 57 | return sb->pos; |
212 | 57 | } |
213 | | |
214 | | static OPJ_BOOL sjpx_stream_seek(OPJ_OFF_T seek_pos, void * p_user_data) |
215 | 2.05k | { |
216 | 2.05k | stream_block *sb = (stream_block *)p_user_data; |
217 | | |
218 | 2.05k | if (seek_pos > sb->fill) |
219 | 32 | return OPJ_FALSE; |
220 | 2.02k | sb->pos = seek_pos; |
221 | 2.02k | return OPJ_TRUE; |
222 | 2.05k | } |
223 | | |
224 | | static void sjpx_error_callback(const char *msg, void *ptr) |
225 | 4.03k | { |
226 | 4.03k | dlprintf1("openjpeg error: %s", msg); |
227 | 4.03k | } |
228 | | |
229 | | static void sjpx_info_callback(const char *msg, void *ptr) |
230 | 17.4k | { |
231 | | #ifdef DEBUG |
232 | | /* prevent too many messages during normal build */ |
233 | | dlprintf1("openjpeg info: %s", msg); |
234 | | #endif |
235 | 17.4k | } |
236 | | |
237 | | static void sjpx_warning_callback(const char *msg, void *ptr) |
238 | 101k | { |
239 | | #ifdef DEBUG |
240 | | /* prevent too many messages during normal build */ |
241 | | dlprintf1("openjpeg warning: %s", msg); |
242 | | #endif |
243 | 101k | } |
244 | | |
245 | | /* initialize the stream */ |
246 | | static int |
247 | | s_opjd_init(stream_state * ss) |
248 | 2.44k | { |
249 | 2.44k | stream_jpxd_state *const state = (stream_jpxd_state *) ss; |
250 | 2.44k | state->codec = NULL; |
251 | | |
252 | 2.44k | state->image = NULL; |
253 | 2.44k | state->sb.data= NULL; |
254 | 2.44k | state->sb.size = 0; |
255 | 2.44k | state->sb.pos = 0; |
256 | 2.44k | state->sb.fill = 0; |
257 | 2.44k | state->out_offset = 0; |
258 | 2.44k | state->pdata = NULL; |
259 | 2.44k | state->sign_comps = NULL; |
260 | 2.44k | state->stream = NULL; |
261 | 2.44k | state->row_data = NULL; |
262 | | |
263 | 2.44k | return 0; |
264 | 2.44k | } |
265 | | |
266 | | /* setting the codec format, |
267 | | allocating the stream and image structures, and |
268 | | initializing the decoder. |
269 | | */ |
270 | | static int |
271 | | s_opjd_set_codec_format(stream_state * ss, OPJ_CODEC_FORMAT format) |
272 | 2.38k | { |
273 | 2.38k | stream_jpxd_state *const state = (stream_jpxd_state *) ss; |
274 | 2.38k | opj_dparameters_t parameters; /* decompression parameters */ |
275 | | |
276 | | /* set decoding parameters to default values */ |
277 | 2.38k | opj_set_default_decoder_parameters(¶meters); |
278 | | |
279 | | /* get a decoder handle */ |
280 | 2.38k | state->codec = opj_create_decompress(format); |
281 | 2.38k | if (state->codec == NULL) |
282 | 0 | return_error(gs_error_VMerror); |
283 | | |
284 | | /* catch events using our callbacks */ |
285 | 2.38k | opj_set_error_handler(state->codec, sjpx_error_callback, stderr); |
286 | 2.38k | opj_set_info_handler(state->codec, sjpx_info_callback, stderr); |
287 | 2.38k | opj_set_warning_handler(state->codec, sjpx_warning_callback, stderr); |
288 | | |
289 | 2.38k | if (state->colorspace == gs_jpx_cs_indexed) { |
290 | 0 | parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG; |
291 | 0 | } |
292 | | |
293 | | /* setup the decoder decoding parameters using user parameters */ |
294 | 2.38k | if (!opj_setup_decoder(state->codec, ¶meters)) |
295 | 0 | { |
296 | 0 | dlprintf("openjpeg: failed to setup the decoder!\n"); |
297 | 0 | return ERRC; |
298 | 0 | } |
299 | | |
300 | | /* open a byte stream */ |
301 | 2.38k | state->stream = opj_stream_default_create(OPJ_TRUE); |
302 | 2.38k | if (state->stream == NULL) |
303 | 0 | { |
304 | 0 | dlprintf("openjpeg: failed to open a byte stream!\n"); |
305 | 0 | return ERRC; |
306 | 0 | } |
307 | | |
308 | 2.38k | opj_stream_set_read_function(state->stream, sjpx_stream_read); |
309 | 2.38k | opj_stream_set_skip_function(state->stream, sjpx_stream_skip); |
310 | 2.38k | opj_stream_set_seek_function(state->stream, sjpx_stream_seek); |
311 | | |
312 | 2.38k | return 0; |
313 | 2.38k | } |
314 | | |
315 | | static void |
316 | | ycc_to_rgb_8(unsigned char *row, unsigned long row_size) |
317 | 0 | { |
318 | 0 | unsigned char y; |
319 | 0 | signed char u, v; |
320 | 0 | int r,g,b; |
321 | 0 | do |
322 | 0 | { |
323 | 0 | y = row[0]; |
324 | 0 | u = row[1] - 128; |
325 | 0 | v = row[2] - 128; |
326 | 0 | r = (int)((double)y + 1.402 * v); |
327 | 0 | if (r < 0) |
328 | 0 | r = 0; |
329 | 0 | if (r > 255) |
330 | 0 | r = 255; |
331 | 0 | g = (int)((double)y - 0.34413 * u - 0.71414 * v); |
332 | 0 | if (g < 0) |
333 | 0 | g = 0; |
334 | 0 | if (g > 255) |
335 | 0 | g = 255; |
336 | 0 | b = (int)((double)y + 1.772 * u); |
337 | 0 | if (b < 0) |
338 | 0 | b = 0; |
339 | 0 | if (b > 255) |
340 | 0 | b = 255; |
341 | 0 | row[0] = r; |
342 | 0 | row[1] = g; |
343 | 0 | row[2] = b; |
344 | 0 | row += 3; |
345 | 0 | row_size -= 3; |
346 | 0 | } |
347 | 0 | while (row_size); |
348 | 0 | } |
349 | | |
350 | | static void |
351 | | ycc_to_rgb_16(unsigned char *row, unsigned long row_size) |
352 | 0 | { |
353 | 0 | unsigned short y; |
354 | 0 | signed short u, v; |
355 | 0 | int r,g,b; |
356 | 0 | do |
357 | 0 | { |
358 | 0 | y = (row[0]<<8) | row[1]; |
359 | 0 | u = ((row[2]<<8) | row[3]) - 32768; |
360 | 0 | v = ((row[4]<<8) | row[5]) - 32768; |
361 | 0 | r = (int)((double)y + 1.402 * v); |
362 | 0 | if (r < 0) |
363 | 0 | r = 0; |
364 | 0 | if (r > 65535) |
365 | 0 | r = 65535; |
366 | 0 | g = (int)((double)y - 0.34413 * u - 0.71414 * v); |
367 | 0 | if (g < 0) |
368 | 0 | g = 0; |
369 | 0 | if (g > 65535) |
370 | 0 | g = 65535; |
371 | 0 | b = (int)((double)y + 1.772 * u); |
372 | 0 | if (b < 0) |
373 | 0 | b = 0; |
374 | 0 | if (b > 65535) |
375 | 0 | b = 65535; |
376 | 0 | row[0] = r>>8; |
377 | 0 | row[1] = r; |
378 | 0 | row[2] = g>>8; |
379 | 0 | row[3] = g; |
380 | 0 | row[4] = b>>8; |
381 | 0 | row[5] = b; |
382 | 0 | row += 6; |
383 | 0 | row_size -= 6; |
384 | 0 | } |
385 | 0 | while (row_size); |
386 | 0 | } |
387 | | |
388 | | static int decode_image(stream_jpxd_state * const state) |
389 | 2.38k | { |
390 | 2.38k | int numprimcomp = 0, alpha_comp = -1, compno, rowbytes; |
391 | | |
392 | | /* read header */ |
393 | 2.38k | if (!opj_read_header(state->stream, state->codec, &(state->image))) |
394 | 71 | { |
395 | 71 | dlprintf("openjpeg: failed to read header\n"); |
396 | 71 | return ERRC; |
397 | 71 | } |
398 | | |
399 | | /* decode the stream and fill the image structure */ |
400 | 2.31k | if (!opj_decode(state->codec, state->stream, state->image)) |
401 | 1.36k | { |
402 | 1.36k | dlprintf("openjpeg: failed to decode image!\n"); |
403 | 1.36k | return ERRC; |
404 | 1.36k | } |
405 | | |
406 | | /* check dimension and prec */ |
407 | 946 | if (state->image->numcomps == 0) |
408 | 0 | return ERRC; |
409 | | |
410 | 946 | state->width = state->image->comps[0].w; |
411 | 946 | state->height = state->image->comps[0].h; |
412 | 946 | state->bpp = state->image->comps[0].prec; |
413 | 946 | state->samescale = true; |
414 | 1.88k | for(compno = 1; compno < state->image->numcomps; compno++) |
415 | 938 | { |
416 | 938 | if (state->bpp != state->image->comps[compno].prec) |
417 | 0 | return ERRC; /* Not supported. */ |
418 | 938 | if (state->width < state->image->comps[compno].w) |
419 | 0 | state->width = state->image->comps[compno].w; |
420 | 938 | if (state->height < state->image->comps[compno].h) |
421 | 0 | state->height = state->image->comps[compno].h; |
422 | 938 | if (state->image->comps[compno].dx != state->image->comps[0].dx || |
423 | 938 | state->image->comps[compno].dy != state->image->comps[0].dy) |
424 | 0 | state->samescale = false; |
425 | 938 | } |
426 | | |
427 | | /* find alpha component and regular colour component by channel definition */ |
428 | 2.83k | for (compno = 0; compno < state->image->numcomps; compno++) |
429 | 1.88k | { |
430 | 1.88k | if (state->image->comps[compno].alpha == 0x00) |
431 | 1.88k | numprimcomp++; |
432 | 0 | else if (state->image->comps[compno].alpha == 0x01 || state->image->comps[compno].alpha == 0x02) |
433 | 0 | alpha_comp = compno; |
434 | 1.88k | } |
435 | | |
436 | | /* color space and number of components */ |
437 | 946 | switch(state->image->color_space) |
438 | 946 | { |
439 | 477 | case OPJ_CLRSPC_GRAY: |
440 | 477 | state->colorspace = gs_jpx_cs_gray; |
441 | 477 | if (numprimcomp > 1) { |
442 | 0 | dmprintf1(state->memory, "openjpeg warning: Ignoring extra components for %d component Gray data.\n", numprimcomp); |
443 | 0 | numprimcomp = 1; |
444 | 0 | } |
445 | 477 | break; |
446 | 469 | case OPJ_CLRSPC_SRGB: |
447 | 469 | case OPJ_CLRSPC_SYCC: |
448 | 469 | case OPJ_CLRSPC_EYCC: |
449 | 469 | state->colorspace = gs_jpx_cs_rgb; |
450 | 469 | if (numprimcomp > 3) { |
451 | 0 | dmprintf1(state->memory, "openjpeg warning: Ignoring extra components for %d component RGB data.\n", numprimcomp); |
452 | 0 | numprimcomp = 3; |
453 | 0 | } |
454 | 469 | break; |
455 | 0 | case OPJ_CLRSPC_CMYK: |
456 | 0 | state->colorspace = gs_jpx_cs_cmyk; |
457 | 0 | if (numprimcomp > 4) { |
458 | 0 | dmprintf1(state->memory, "openjpeg warning: Ignoring extra components for %d component CMYK data.\n", numprimcomp); |
459 | 0 | numprimcomp = 4; |
460 | 0 | } |
461 | 0 | break; |
462 | 0 | default: |
463 | 0 | case OPJ_CLRSPC_UNSPECIFIED: |
464 | 0 | case OPJ_CLRSPC_UNKNOWN: |
465 | 0 | if (numprimcomp == 1) { |
466 | 0 | dmprintf1(state->memory, "openjpeg warning: unspec CS. %d component so assuming gray.\n", numprimcomp); |
467 | 0 | state->colorspace = gs_jpx_cs_gray; |
468 | 0 | } else if (numprimcomp == 4) { |
469 | 0 | dmprintf1(state->memory, "openjpeg warning: unspec CS. %d components so assuming CMYK.\n", numprimcomp); |
470 | 0 | state->colorspace = gs_jpx_cs_cmyk; |
471 | 0 | } else { |
472 | | /* Note, numprimcomp > 4 possible here. Bug 694909. |
473 | | Trust that it is RGB though. Do not set numprimcomp */ |
474 | 0 | dmprintf1(state->memory, "openjpeg warning: unspec CS. %d components. Assuming data RGB.\n", numprimcomp); |
475 | 0 | state->colorspace = gs_jpx_cs_rgb; |
476 | 0 | } |
477 | 0 | break; |
478 | 946 | } |
479 | | |
480 | 946 | state->alpha_comp = -1; |
481 | 946 | if (state->alpha) |
482 | 0 | { |
483 | 0 | state->alpha_comp = alpha_comp; |
484 | 0 | state->out_numcomps = 1; |
485 | 0 | } |
486 | 946 | else |
487 | 946 | state->out_numcomps = numprimcomp; |
488 | | |
489 | | /* round up bpp 12->16 */ |
490 | 946 | if (state->bpp == 12) |
491 | 0 | state->bpp = 16; |
492 | | |
493 | | /* calculate total data */ |
494 | 946 | rowbytes = (state->width*state->bpp*state->out_numcomps+7)/8; |
495 | 946 | state->totalbytes = (ulong)rowbytes*state->height; |
496 | | |
497 | 946 | state->pdata = (int **)gs_alloc_byte_array(state->memory->non_gc_memory, sizeof(int*)*state->image->numcomps, 1, "decode_image(pdata)"); |
498 | 946 | if (!state->pdata) |
499 | 0 | return_error(gs_error_VMerror); |
500 | | |
501 | | /* compensate for signed data (signed => unsigned) */ |
502 | 946 | state->sign_comps = (int *)gs_alloc_byte_array(state->memory->non_gc_memory, sizeof(int)*state->image->numcomps, 1, "decode_image(sign_comps)"); |
503 | 946 | if (!state->sign_comps) |
504 | 0 | return_error(gs_error_VMerror); |
505 | | |
506 | 2.83k | for(compno = 0; compno < state->image->numcomps; compno++) |
507 | 1.88k | { |
508 | 1.88k | if (state->image->comps[compno].sgnd) |
509 | 0 | state->sign_comps[compno] = ((state->bpp%8)==0) ? 0x80 : (1<<(state->bpp-1)); |
510 | 1.88k | else |
511 | 1.88k | state->sign_comps[compno] = 0; |
512 | 1.88k | } |
513 | | |
514 | 946 | return 0; |
515 | 946 | } |
516 | | |
517 | | static int process_one_trunk(stream_jpxd_state * const state, stream_cursor_write * pw) |
518 | 298k | { |
519 | | /* read data from image to pw */ |
520 | 298k | unsigned long out_size = pw->limit - pw->ptr; |
521 | 298k | int bytepp1 = state->bpp/8; /* bytes / pixel for one output component */ |
522 | 298k | int bytepp = state->out_numcomps*state->bpp/8; /* bytes / pixel all components */ |
523 | 298k | unsigned long write_size = min(out_size-(bytepp?(out_size%bytepp):0), state->totalbytes-state->out_offset); |
524 | 298k | int shift_bit = state->bpp-state->image->comps[0].prec; /*difference between input and output bit-depth*/ |
525 | 298k | int img_numcomps = min(state->out_numcomps, state->image->numcomps); /* the actual number of channel data used */ |
526 | 298k | int compno; |
527 | 298k | unsigned long il; |
528 | 298k | int i; |
529 | 298k | int b; |
530 | 298k | byte *row; |
531 | 298k | unsigned int x_offset; |
532 | 298k | unsigned int y_offset; |
533 | 298k | unsigned int row_size = (state->width * state->out_numcomps * state->bpp + 7)>>3; |
534 | | |
535 | | /* If nothing to write, nothing to do */ |
536 | 298k | if (write_size == 0) |
537 | 3 | return 0; |
538 | | |
539 | 298k | if (state->row_data == NULL) |
540 | 946 | { |
541 | 946 | state->row_data = gs_alloc_byte_array(state->memory->non_gc_memory, row_size, 1, "jpxd_openjpeg(row_data)"); |
542 | 946 | if (state->row_data == NULL) |
543 | 0 | return gs_error_VMerror; |
544 | 946 | } |
545 | | |
546 | 625k | while (state->out_offset != state->totalbytes) |
547 | 625k | { |
548 | 625k | y_offset = state->out_offset / row_size; |
549 | 625k | x_offset = state->out_offset % row_size; |
550 | | |
551 | 625k | if (x_offset == 0) |
552 | 384k | { |
553 | | /* Decode another rows worth */ |
554 | 384k | row = state->row_data; |
555 | 384k | if (state->alpha && state->alpha_comp == -1) |
556 | 0 | { |
557 | | /* return 0xff for all */ |
558 | 0 | memset(row, 0xff, row_size); |
559 | 0 | } |
560 | 384k | else if (state->samescale) |
561 | 384k | { |
562 | 384k | if (state->alpha) |
563 | 0 | state->pdata[0] = &(state->image->comps[state->alpha_comp].data[y_offset * state->width]); |
564 | 384k | else |
565 | 384k | { |
566 | 1.15M | for (compno=0; compno<img_numcomps; compno++) |
567 | 774k | state->pdata[compno] = &(state->image->comps[compno].data[y_offset * state->width]); |
568 | 384k | } |
569 | 384k | if (shift_bit == 0 && state->bpp == 8) /* optimized for the most common case */ |
570 | 384k | { |
571 | 151M | for (i = state->width; i > 0; i--) |
572 | 460M | for (compno=0; compno<img_numcomps; compno++) |
573 | 310M | *row++ = *(state->pdata[compno]++) + state->sign_comps[compno]; /* copy input buffer to output */ |
574 | 384k | } |
575 | 0 | else if ((state->bpp%8)==0) |
576 | 0 | { |
577 | 0 | for (i = state->width; i > 0; i--) |
578 | 0 | { |
579 | 0 | for (compno=0; compno<img_numcomps; compno++) |
580 | 0 | { |
581 | 0 | for (b=0; b<bytepp1; b++) |
582 | 0 | *row++ = (((*(state->pdata[compno]) << shift_bit) >> (8*(bytepp1-b-1)))) |
583 | 0 | + (b==0 ? state->sign_comps[compno] : 0); /* split and shift input int to output bytes */ |
584 | 0 | state->pdata[compno]++; |
585 | 0 | } |
586 | 0 | } |
587 | 0 | } |
588 | 0 | else |
589 | 0 | { |
590 | | /* shift_bit = 0, bpp < 8 */ |
591 | 0 | int bt=0; |
592 | 0 | int bit_pos = 0; |
593 | 0 | for (i = state->width; i > 0; i--) |
594 | 0 | { |
595 | 0 | for (compno=0; compno<img_numcomps; compno++) |
596 | 0 | { |
597 | 0 | bt <<= state->bpp; |
598 | 0 | bt += *(state->pdata[compno]++) + state->sign_comps[compno]; |
599 | 0 | bit_pos += state->bpp; |
600 | 0 | if (bit_pos >= 8) |
601 | 0 | { |
602 | 0 | *row++ = bt >> (bit_pos-8); |
603 | 0 | bit_pos -= 8; |
604 | 0 | bt &= (1<<bit_pos)-1; |
605 | 0 | } |
606 | 0 | } |
607 | 0 | } |
608 | 0 | if (bit_pos != 0) |
609 | 0 | { |
610 | | /* row padding */ |
611 | 0 | *row++ = bt << (8 - bit_pos); |
612 | 0 | bit_pos = 0; |
613 | 0 | bt = 0; |
614 | 0 | } |
615 | 0 | } |
616 | 384k | } |
617 | 0 | else if ((state->bpp%8)==0) |
618 | 0 | { |
619 | | /* sampling required */ |
620 | 0 | if (state->alpha) |
621 | 0 | { |
622 | 0 | for (i = 0; i < state->width; i++) |
623 | 0 | { |
624 | 0 | int in_offset_scaled = (y_offset/state->image->comps[state->alpha_comp].dy*state->width + i)/state->image->comps[state->alpha_comp].dx; |
625 | 0 | for (b=0; b<bytepp1; b++) |
626 | 0 | *row++ = (((state->image->comps[state->alpha_comp].data[in_offset_scaled] << shift_bit) >> (8*(bytepp1-b-1)))) |
627 | 0 | + (b==0 ? state->sign_comps[state->alpha_comp] : 0); |
628 | 0 | } |
629 | 0 | } |
630 | 0 | else |
631 | 0 | { |
632 | 0 | for (i = 0; i < state->width; i++) |
633 | 0 | { |
634 | 0 | for (compno=0; compno<img_numcomps; compno++) |
635 | 0 | { |
636 | 0 | int in_offset_scaled = (y_offset/state->image->comps[compno].dy*state->width + i)/state->image->comps[compno].dx; |
637 | 0 | for (b=0; b<bytepp1; b++) |
638 | 0 | *row++ = (((state->image->comps[compno].data[in_offset_scaled] << shift_bit) >> (8*(bytepp1-b-1)))) |
639 | 0 | + (b==0 ? state->sign_comps[compno] : 0); |
640 | 0 | } |
641 | 0 | } |
642 | 0 | } |
643 | 0 | } |
644 | 0 | else |
645 | 0 | { |
646 | 0 | int compno = state->alpha ? state->alpha_comp : 0; |
647 | 0 | int bt=0; |
648 | 0 | int ppbyte1 = 8/state->bpp; |
649 | | /* sampling required */ |
650 | | /* only grayscale can have such bit-depth, also shift_bit = 0, bpp < 8 */ |
651 | 0 | for (i = 0; i < state->width; i++) |
652 | 0 | { |
653 | 0 | for (b=0; b<ppbyte1; b++) |
654 | 0 | { |
655 | 0 | int in_offset_scaled = (y_offset/state->image->comps[compno].dy*state->width + i)/state->image->comps[compno].dx; |
656 | 0 | bt = bt<<state->bpp; |
657 | 0 | bt += state->image->comps[compno].data[in_offset_scaled] + state->sign_comps[compno]; |
658 | 0 | } |
659 | 0 | *row++ = bt; |
660 | 0 | } |
661 | 0 | } |
662 | | |
663 | 384k | if (state->image->color_space == OPJ_CLRSPC_SYCC || state->image->color_space == OPJ_CLRSPC_EYCC) |
664 | 0 | { |
665 | | /* bpp >= 8 always, as bpp < 8 only for grayscale */ |
666 | 0 | if (state->bpp == 8) |
667 | 0 | ycc_to_rgb_8(state->row_data, row_size); |
668 | 0 | else |
669 | 0 | ycc_to_rgb_16(state->row_data, row_size); |
670 | 0 | } |
671 | 384k | } |
672 | | |
673 | 625k | pw->ptr++; |
674 | 625k | il = (write_size > (unsigned long)(row_size - x_offset)) ? (row_size - x_offset) : (unsigned int)write_size; |
675 | 625k | memcpy(pw->ptr, &state->row_data[x_offset], il); |
676 | 625k | pw->ptr += il; |
677 | 625k | pw->ptr--; |
678 | 625k | state->out_offset += il; |
679 | 625k | write_size -= il; |
680 | 625k | if (write_size == 0) |
681 | 298k | break; |
682 | 625k | } |
683 | | |
684 | 298k | if (state->out_offset == state->totalbytes) |
685 | 946 | return EOFC; /* all data returned */ |
686 | 297k | else |
687 | 297k | return 1; /* need more calls */ |
688 | 298k | } |
689 | | |
690 | | /* process a section of the input and return any decoded data. |
691 | | see strimpl.h for return codes. |
692 | | */ |
693 | | static int |
694 | | s_opjd_process(stream_state * ss, stream_cursor_read * pr, |
695 | | stream_cursor_write * pw, bool last) |
696 | 444k | { |
697 | 444k | stream_jpxd_state *const state = (stream_jpxd_state *) ss; |
698 | 444k | long in_size = pr->limit - pr->ptr; |
699 | 444k | int locked = 0; |
700 | 444k | int code; |
701 | | |
702 | 444k | if (in_size > 0) |
703 | 144k | { |
704 | 144k | if (state->PassThrough && state->PassThroughfn) { |
705 | 23.5k | if (state->PassThrough && state->PassThroughfn && !state->StartedPassThrough) { |
706 | 260 | state->StartedPassThrough = 1; |
707 | 260 | (state->PassThroughfn)(state->device, NULL, 1); |
708 | 260 | } |
709 | 23.5k | (state->PassThroughfn)(state->device, (byte *)pr->ptr + 1, (byte *)pr->limit - (byte *)pr->ptr); |
710 | 23.5k | } |
711 | | |
712 | | /* buffer available data */ |
713 | 144k | code = opj_lock(ss->memory); |
714 | 144k | if (code < 0) return code; |
715 | 144k | locked = 1; |
716 | | |
717 | 144k | code = s_opjd_accumulate_input(state, pr); |
718 | 144k | if (code < 0) { |
719 | 0 | (void)opj_unlock(ss->memory); |
720 | 0 | return code; |
721 | 0 | } |
722 | | |
723 | 144k | if (state->codec == NULL) { |
724 | | /* state->sb.size is non-zero after successful |
725 | | accumulate_input(); 1 is probably extremely rare */ |
726 | 2.38k | if (state->sb.data[0] == 0xFF && ((state->sb.size == 1) || (state->sb.data[1] == 0x4F))) |
727 | 2 | code = s_opjd_set_codec_format(ss, OPJ_CODEC_J2K); |
728 | 2.38k | else |
729 | 2.38k | code = s_opjd_set_codec_format(ss, OPJ_CODEC_JP2); |
730 | 2.38k | if (code < 0) |
731 | 0 | { |
732 | 0 | (void)opj_unlock(ss->memory); |
733 | 0 | return code; |
734 | 0 | } |
735 | 2.38k | } |
736 | 144k | } |
737 | | |
738 | 444k | if (last == 1) |
739 | 300k | { |
740 | 300k | if (state->image == NULL) |
741 | 2.38k | { |
742 | 2.38k | int ret; |
743 | | |
744 | 2.38k | if (locked == 0) |
745 | 0 | { |
746 | 0 | ret = opj_lock(ss->memory); |
747 | 0 | if (ret < 0) return ret; |
748 | 0 | locked = 1; |
749 | 0 | } |
750 | | |
751 | 2.38k | #if OPJ_VERSION_MAJOR >= 2 && OPJ_VERSION_MINOR >= 1 |
752 | 2.38k | opj_stream_set_user_data(state->stream, &(state->sb), NULL); |
753 | | #else |
754 | | opj_stream_set_user_data(state->stream, &(state->sb)); |
755 | | #endif |
756 | 2.38k | opj_stream_set_user_data_length(state->stream, state->sb.size); |
757 | 2.38k | ret = decode_image(state); |
758 | 2.38k | if (ret != 0) |
759 | 1.43k | { |
760 | 1.43k | (void)opj_unlock(ss->memory); |
761 | 1.43k | return ret; |
762 | 1.43k | } |
763 | 2.38k | } |
764 | | |
765 | 298k | if (locked) |
766 | 946 | { |
767 | 946 | code = opj_unlock(ss->memory); |
768 | 946 | if (code < 0) return code; |
769 | 946 | } |
770 | | |
771 | | /* copy out available data */ |
772 | 298k | return process_one_trunk(state, pw); |
773 | | |
774 | 298k | } |
775 | | |
776 | 144k | if (locked) |
777 | 142k | return opj_unlock(ss->memory); |
778 | | |
779 | | /* ask for more data */ |
780 | 2.38k | return 0; |
781 | 144k | } |
782 | | |
783 | | /* Set the defaults */ |
784 | | static void |
785 | 2.44k | s_opjd_set_defaults(stream_state * ss) { |
786 | 2.44k | stream_jpxd_state *const state = (stream_jpxd_state *) ss; |
787 | | |
788 | 2.44k | state->alpha = false; |
789 | 2.44k | state->colorspace = gs_jpx_cs_rgb; |
790 | 2.44k | state->StartedPassThrough = 0; |
791 | 2.44k | state->PassThrough = 0; |
792 | 2.44k | state->PassThroughfn = NULL; |
793 | 2.44k | state->device = (void *)NULL; |
794 | 2.44k | } |
795 | | |
796 | | /* stream release. |
797 | | free all our decoder state. |
798 | | */ |
799 | | static void |
800 | | s_opjd_release(stream_state *ss) |
801 | 2.44k | { |
802 | 2.44k | stream_jpxd_state *const state = (stream_jpxd_state *) ss; |
803 | | |
804 | 2.44k | if (state->PassThrough && state->PassThroughfn && state->StartedPassThrough) { |
805 | 260 | state->StartedPassThrough = 0; |
806 | 260 | (state->PassThroughfn)(state->device, NULL, 0); |
807 | 260 | } |
808 | | /* empty stream or failed to accumulate */ |
809 | 2.44k | if (state->codec == NULL) |
810 | 55 | return; |
811 | | |
812 | 2.38k | (void)opj_lock(ss->memory); |
813 | | |
814 | | /* free image data structure */ |
815 | 2.38k | if (state->image) |
816 | 2.31k | opj_image_destroy(state->image); |
817 | | |
818 | | /* free stream */ |
819 | 2.38k | if (state->stream) |
820 | 2.38k | opj_stream_destroy(state->stream); |
821 | | |
822 | | /* free decoder handle */ |
823 | 2.38k | if (state->codec) |
824 | 2.38k | opj_destroy_codec(state->codec); |
825 | | |
826 | 2.38k | (void)opj_unlock(ss->memory); |
827 | | |
828 | | /* free input buffer */ |
829 | 2.38k | if (state->sb.data) |
830 | 2.38k | gs_free_object(state->memory->non_gc_memory, state->sb.data, "s_opjd_release(sb.data)"); |
831 | | |
832 | 2.38k | if (state->pdata) |
833 | 946 | gs_free_object(state->memory->non_gc_memory, state->pdata, "s_opjd_release(pdata)"); |
834 | | |
835 | 2.38k | if (state->sign_comps) |
836 | 946 | gs_free_object(state->memory->non_gc_memory, state->sign_comps, "s_opjd_release(sign_comps)"); |
837 | | |
838 | 2.38k | if (state->row_data) |
839 | 946 | gs_free_object(state->memory->non_gc_memory, state->row_data, "s_opjd_release(row_data)"); |
840 | 2.38k | } |
841 | | |
842 | | |
843 | | static int |
844 | | s_opjd_accumulate_input(stream_jpxd_state *state, stream_cursor_read * pr) |
845 | 144k | { |
846 | 144k | long in_size = pr->limit - pr->ptr; |
847 | | |
848 | | /* grow the input buffer if needed */ |
849 | 144k | if (state->sb.size < state->sb.fill + in_size) |
850 | 13.1k | { |
851 | 13.1k | unsigned char *new_buf; |
852 | 13.1k | unsigned long new_size = state->sb.size==0 ? in_size : state->sb.size; |
853 | | |
854 | 23.9k | while (new_size < state->sb.fill + in_size) |
855 | 10.7k | new_size = new_size << 1; |
856 | | |
857 | 13.1k | if_debug1('s', "[s]opj growing input buffer to %lu bytes\n", |
858 | 13.1k | new_size); |
859 | 13.1k | if (state->sb.data == NULL) |
860 | 2.38k | new_buf = (byte *) gs_alloc_byte_array(state->memory->non_gc_memory, new_size, 1, "s_opjd_accumulate_input(alloc)"); |
861 | 10.7k | else |
862 | 10.7k | new_buf = (byte *) gs_resize_object(state->memory->non_gc_memory, state->sb.data, new_size, "s_opjd_accumulate_input(resize)"); |
863 | 13.1k | if (new_buf == NULL) return_error( gs_error_VMerror); |
864 | | |
865 | 13.1k | state->sb.data = new_buf; |
866 | 13.1k | state->sb.size = new_size; |
867 | 13.1k | } |
868 | | |
869 | | /* copy the available input into our buffer */ |
870 | | /* note that the gs stream library uses offset-by-one |
871 | | indexing of its buffers while we use zero indexing */ |
872 | 144k | memcpy(state->sb.data + state->sb.fill, pr->ptr + 1, in_size); |
873 | 144k | state->sb.fill += in_size; |
874 | 144k | pr->ptr += in_size; |
875 | | |
876 | 144k | return 0; |
877 | 144k | } |
878 | | |
879 | | /* stream template */ |
880 | | const stream_template s_jpxd_template = { |
881 | | &st_jpxd_state, |
882 | | s_opjd_init, |
883 | | s_opjd_process, |
884 | | 1024, 1024, /* min in and out buffer sizes we can handle |
885 | | should be ~32k,64k for efficiency? */ |
886 | | s_opjd_release, |
887 | | s_opjd_set_defaults |
888 | | }; |