63static char JP2_HEAD[] = { 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A };
118 opj_dparameters_t parameters;
119 opj_image_t *image = NULL;
121 unsigned char src_header[12] = { 0 };
122 opj_codec_t *d_codec = NULL;
123 OPJ_CODEC_FORMAT codec;
124 opj_stream_t *d_stream = NULL;
128 opj_set_default_decoder_parameters(¶meters);
130 g_strlcpy(parameters.infile, filename,
sizeof(parameters.infile));
137 fsrc = g_fopen(filename,
"rb");
140 fprintf(stderr,
"[j2k_open] Error: failed to open `%s' for reading\n", filename);
143 if(fread(src_header, 1, 12, fsrc) != 12)
146 fprintf(stderr,
"[j2k_open] Error: fread returned a number of elements different from the expected.\n");
161 fprintf(stderr,
"[j2k_open] Error: `%s' has unsupported file format.\n", filename);
168 if(parameters.decod_format ==
J2K_CFMT)
169 codec = OPJ_CODEC_J2K;
170 else if(parameters.decod_format ==
JP2_CFMT)
171 codec = OPJ_CODEC_JP2;
172 else if(parameters.decod_format ==
JPT_CFMT)
173 codec = OPJ_CODEC_JPT;
179 d_codec = opj_create_decompress(codec);
182 fprintf(stderr,
"[j2k_open] Error: failed to create the decoder\n");
196 fprintf(stderr,
"[j2k_open] Error: failed to setup the threads for decoder %s\n", parameters.infile);
197 opj_destroy_codec(d_codec);
202 if(!opj_setup_decoder(d_codec, ¶meters))
204 fprintf(stderr,
"[j2k_open] Error: failed to setup the decoder %s\n", parameters.infile);
205 opj_destroy_codec(d_codec);
209 d_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
212 fprintf(stderr,
"[j2k_open] Error: failed to create the stream from the file %s\n", parameters.infile);
213 opj_destroy_codec(d_codec);
218 if(!opj_read_header(d_stream, d_codec, &image))
220 fprintf(stderr,
"[j2k_open] Error: failed to read the header\n");
221 opj_stream_destroy(d_stream);
222 opj_destroy_codec(d_codec);
223 opj_image_destroy(image);
228 if(!(opj_decode(d_codec, d_stream, image) && opj_end_decompress(d_codec, d_stream)))
230 fprintf(stderr,
"[j2k_open] Error: failed to decode image!\n");
231 opj_destroy_codec(d_codec);
232 opj_stream_destroy(d_stream);
233 opj_image_destroy(image);
238 opj_stream_destroy(d_stream);
242 fprintf(stderr,
"[j2k_open] Error: failed to decode image `%s'\n", filename);
244 goto end_of_the_world;
247 if(image->color_space == OPJ_CLRSPC_SYCC)
252 if(image->icc_profile_buf)
254#if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2)
255 color_apply_icc_profile(image);
258 dt_free(image->icc_profile_buf);
259 image->icc_profile_len = 0;
264 long signed_offsets[4] = { 0, 0, 0, 0 };
265 int float_divs[4] = { 1, 1, 1, 1 };
268 if(image->numcomps == 0 || image->x1 == 0 || image->y1 == 0)
270 fprintf(stderr,
"[j2k_open] Error: invalid raw image parameters in `%s'\n", filename);
272 goto end_of_the_world;
275 for(
int i = 0;
i < image->numcomps;
i++)
277 if(image->comps[
i].w != image->x1 || image->comps[
i].h != image->y1)
279 fprintf(stderr,
"[j2k_open] Error: some component has different size in `%s'\n", filename);
281 goto end_of_the_world;
283 if(image->comps[
i].prec > 16)
285 fprintf(stderr,
"[j2k_open] Error: precision %d is larger than 16 in `%s'\n", image->comps[1].prec,
288 goto end_of_the_world;
292 img->
width = image->x1;
297 img->
dsc.
bpp = 4 *
sizeof(float);
300 img->
flags &= ~DT_IMAGE_RAW;
301 img->
flags &= ~DT_IMAGE_HDR;
302 img->
flags &= ~DT_IMAGE_S_RAW;
309 goto end_of_the_world;
316 goto end_of_the_world;
319 image->numcomps =
MIN(image->numcomps, 4);
321 for(
int i = 0;
i < image->numcomps;
i++)
323 if(image->comps[
i].sgnd) signed_offsets[
i] = 1 << (image->comps[
i].prec - 1);
325 float_divs[
i] = (1 << image->comps[
i].prec) - 1;
334 if(image->numcomps < 3)
337 buf[
i * 4 + 0] = buf[
i * 4 + 1] = buf[
i * 4 + 2] = (
float)(image->comps[0].data[
i] + signed_offsets[0])
343 for(
int k = 0;
k < 3;
k++)
344 buf[
i * 4 +
k] = (
float)(image->comps[
k].data[
i] + signed_offsets[
k]) / float_divs[
k];
351 opj_destroy_codec(d_codec);
354 opj_image_destroy(image);
361 opj_dparameters_t parameters;
362 opj_image_t *image = NULL;
364 unsigned char src_header[12] = { 0 };
365 opj_codec_t *d_codec = NULL;
366 OPJ_CODEC_FORMAT codec;
367 opj_stream_t *d_stream = NULL;
368 unsigned int length = 0;
372 opj_set_default_decoder_parameters(¶meters);
374 g_strlcpy(parameters.infile, filename,
sizeof(parameters.infile));
381 fsrc = g_fopen(filename,
"rb");
384 fprintf(stderr,
"[j2k_read_profile] Error: failed to open `%s' for reading\n", filename);
385 goto another_end_of_the_world;
387 if(fread(src_header, 1, 12, fsrc) != 12)
391 "[j2k_read_profile] Error: fread returned a number of elements different from the expected.\n");
392 goto another_end_of_the_world;
398 codec = OPJ_CODEC_JP2;
402 codec = OPJ_CODEC_J2K;
406 fprintf(stderr,
"[j2k_read_profile] Error: `%s' has unsupported file format.\n", filename);
407 goto another_end_of_the_world;
414 d_codec = opj_create_decompress(codec);
417 fprintf(stderr,
"[j2k_read_profile] Error: failed to create the decoder\n");
422 if(!opj_setup_decoder(d_codec, ¶meters))
424 fprintf(stderr,
"[j2k_read_profile] Error: failed to setup the decoder %s\n", parameters.infile);
428 d_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
431 fprintf(stderr,
"[j2k_read_profile] Error: failed to create the stream from the file %s\n", parameters.infile);
436 if(!opj_read_header(d_stream, d_codec, &image))
438 fprintf(stderr,
"[j2k_read_profile] Error: failed to read the header\n");
439 opj_stream_destroy(d_stream);
440 opj_destroy_codec(d_codec);
441 opj_image_destroy(image);
446 if(!(opj_decode(d_codec, d_stream, image) && opj_end_decompress(d_codec, d_stream)))
448 fprintf(stderr,
"[j2k_read_profile] Error: failed to decode image!\n");
449 opj_destroy_codec(d_codec);
450 opj_stream_destroy(d_stream);
451 opj_image_destroy(image);
461 opj_stream_destroy(d_stream);
465 fprintf(stderr,
"[j2k_read_profile] Error: failed to decode image `%s'\n", filename);
466 goto another_end_of_the_world;
469 if(image->icc_profile_len > 0 && image->icc_profile_buf)
471 length = image->icc_profile_len;
472 *
out = (uint8_t *)g_malloc(image->icc_profile_len);
473 memcpy(*
out, image->icc_profile_buf, image->icc_profile_len);
476another_end_of_the_world:
478 opj_destroy_codec(d_codec);
481 opj_image_destroy(image);
573 int *
d0, *d1, *d2, *
r, *
g, *b;
574 const int *y, *cb, *cr;
575 size_t maxw, maxh,
max;
579 i = img->comps[0].prec;
580 offset = 1 << (
i - 1);
583 maxw = img->comps[0].w;
584 maxh = img->comps[0].h;
587 y = img->comps[0].data;
588 cb = img->comps[1].data;
589 cr = img->comps[2].data;
591 d0 =
r = (
int *)calloc(
max,
sizeof(
int));
592 d1 =
g = (
int *)calloc(
max,
sizeof(
int));
593 d2 = b = (
int *)calloc(
max,
sizeof(
int));
595 for(
i = 0;
i < maxh; ++
i)
597 for(j = 0; j < maxw; j += 2)
615 img->comps[0].data =
d0;
617 img->comps[1].data = d1;
619 img->comps[2].data = d2;
621 img->comps[1].w = maxw;
622 img->comps[1].h = maxh;
623 img->comps[2].w = maxw;
624 img->comps[2].h = maxh;
625 img->comps[1].dx = img->comps[0].dx;
626 img->comps[2].dx = img->comps[0].dx;
627 img->comps[1].dy = img->comps[0].dy;
628 img->comps[2].dy = img->comps[0].dy;
633 const int offset = 1 << (img->comps[0].prec - 1);
634 const int upb = (1 << img->comps[0].prec) - 1;
636 size_t maxw = img->comps[0].w;
637 size_t maxh = img->comps[0].h;
638 size_t max = maxw * maxh;
640 const int *y = img->comps[0].data;
641 const int *cb = img->comps[1].data;
642 const int *cr = img->comps[2].data;
644 int *
d0, *d1, *d2, *
r, *
g, *b;
645 d0 =
r = (
int *)calloc(
max,
sizeof(
int));
646 d1 =
g = (
int *)calloc(
max,
sizeof(
int));
647 d2 = b = (
int *)calloc(
max,
sizeof(
int));
649 for(
int i = 0;
i < maxh;
i += 2)
651 const int *ny = y + maxw;
656 for(
int j = 0; j < maxw; j += 2)
670 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
676 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
690 img->comps[0].data =
d0;
692 img->comps[1].data = d1;
694 img->comps[2].data = d2;
696 img->comps[1].w = maxw;
697 img->comps[1].h = maxh;
698 img->comps[2].w = maxw;
699 img->comps[2].h = maxh;
700 img->comps[1].dx = img->comps[0].dx;
701 img->comps[2].dx = img->comps[0].dx;
702 img->comps[1].dy = img->comps[0].dy;
703 img->comps[2].dy = img->comps[0].dy;
708 if(img->numcomps < 3)
710 img->color_space = OPJ_CLRSPC_GRAY;
714 if((img->comps[0].dx == 1) && (img->comps[1].dx == 2) && (img->comps[2].dx == 2) && (img->comps[0].dy == 1)
715 && (img->comps[1].dy == 2) && (img->comps[2].dy == 2))
719 else if((img->comps[0].dx == 1) && (img->comps[1].dx == 2) && (img->comps[2].dx == 2)
720 && (img->comps[0].dy == 1) && (img->comps[1].dy == 1)
721 && (img->comps[2].dy == 1))
725 else if((img->comps[0].dx == 1) && (img->comps[1].dx == 1) && (img->comps[2].dx == 1)
726 && (img->comps[0].dy == 1) && (img->comps[1].dy == 1)
727 && (img->comps[2].dy == 1))
733 fprintf(stderr,
"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__, __LINE__);
736 img->color_space = OPJ_CLRSPC_SRGB;
static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr, int *out_r, int *out_g, int *out_b)