61static char JP2_HEAD[] = { 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A };
116 opj_dparameters_t parameters;
117 opj_image_t *image = NULL;
119 unsigned char src_header[12] = { 0 };
120 opj_codec_t *d_codec = NULL;
121 OPJ_CODEC_FORMAT codec;
122 opj_stream_t *d_stream = NULL;
126 opj_set_default_decoder_parameters(¶meters);
128 g_strlcpy(parameters.infile, filename,
sizeof(parameters.infile));
135 fsrc = g_fopen(filename,
"rb");
138 fprintf(stderr,
"[j2k_open] Error: failed to open `%s' for reading\n", filename);
141 if(fread(src_header, 1, 12, fsrc) != 12)
144 fprintf(stderr,
"[j2k_open] Error: fread returned a number of elements different from the expected.\n");
159 fprintf(stderr,
"[j2k_open] Error: `%s' has unsupported file format.\n", filename);
166 if(parameters.decod_format ==
J2K_CFMT)
167 codec = OPJ_CODEC_J2K;
168 else if(parameters.decod_format ==
JP2_CFMT)
169 codec = OPJ_CODEC_JP2;
170 else if(parameters.decod_format ==
JPT_CFMT)
171 codec = OPJ_CODEC_JPT;
177 d_codec = opj_create_decompress(codec);
180 fprintf(stderr,
"[j2k_open] Error: failed to create the decoder\n");
194 fprintf(stderr,
"[j2k_open] Error: failed to setup the threads for decoder %s\n", parameters.infile);
195 opj_destroy_codec(d_codec);
200 if(!opj_setup_decoder(d_codec, ¶meters))
202 fprintf(stderr,
"[j2k_open] Error: failed to setup the decoder %s\n", parameters.infile);
203 opj_destroy_codec(d_codec);
207 d_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
210 fprintf(stderr,
"[j2k_open] Error: failed to create the stream from the file %s\n", parameters.infile);
211 opj_destroy_codec(d_codec);
216 if(!opj_read_header(d_stream, d_codec, &image))
218 fprintf(stderr,
"[j2k_open] Error: failed to read the header\n");
219 opj_stream_destroy(d_stream);
220 opj_destroy_codec(d_codec);
221 opj_image_destroy(image);
226 if(!(opj_decode(d_codec, d_stream, image) && opj_end_decompress(d_codec, d_stream)))
228 fprintf(stderr,
"[j2k_open] Error: failed to decode image!\n");
229 opj_destroy_codec(d_codec);
230 opj_stream_destroy(d_stream);
231 opj_image_destroy(image);
236 opj_stream_destroy(d_stream);
240 fprintf(stderr,
"[j2k_open] Error: failed to decode image `%s'\n", filename);
242 goto end_of_the_world;
245 if(image->color_space == OPJ_CLRSPC_SYCC)
250 if(image->icc_profile_buf)
252#if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2)
253 color_apply_icc_profile(image);
256 dt_free(image->icc_profile_buf);
257 image->icc_profile_len = 0;
262 long signed_offsets[4] = { 0, 0, 0, 0 };
263 int float_divs[4] = { 1, 1, 1, 1 };
266 if(image->numcomps == 0 || image->x1 == 0 || image->y1 == 0)
268 fprintf(stderr,
"[j2k_open] Error: invalid raw image parameters in `%s'\n", filename);
270 goto end_of_the_world;
273 for(
int i = 0;
i < image->numcomps;
i++)
275 if(image->comps[
i].w != image->x1 || image->comps[
i].h != image->y1)
277 fprintf(stderr,
"[j2k_open] Error: some component has different size in `%s'\n", filename);
279 goto end_of_the_world;
281 if(image->comps[
i].prec > 16)
283 fprintf(stderr,
"[j2k_open] Error: precision %d is larger than 16 in `%s'\n", image->comps[1].prec,
286 goto end_of_the_world;
290 img->
width = image->x1;
295 img->
dsc.
bpp = 4 *
sizeof(float);
298 img->
flags &= ~DT_IMAGE_RAW;
299 img->
flags &= ~DT_IMAGE_HDR;
300 img->
flags &= ~DT_IMAGE_S_RAW;
307 goto end_of_the_world;
314 goto end_of_the_world;
317 image->numcomps =
MIN(image->numcomps, 4);
319 for(
int i = 0;
i < image->numcomps;
i++)
321 if(image->comps[
i].sgnd) signed_offsets[
i] = 1 << (image->comps[
i].prec - 1);
323 float_divs[
i] = (1 << image->comps[
i].prec) - 1;
332 if(image->numcomps < 3)
335 buf[
i * 4 + 0] = buf[
i * 4 + 1] = buf[
i * 4 + 2] = (
float)(image->comps[0].data[
i] + signed_offsets[0])
341 for(
int k = 0;
k < 3;
k++)
342 buf[
i * 4 +
k] = (
float)(image->comps[
k].data[
i] + signed_offsets[
k]) / float_divs[
k];
349 opj_destroy_codec(d_codec);
352 opj_image_destroy(image);
359 opj_dparameters_t parameters;
360 opj_image_t *image = NULL;
362 unsigned char src_header[12] = { 0 };
363 opj_codec_t *d_codec = NULL;
364 OPJ_CODEC_FORMAT codec;
365 opj_stream_t *d_stream = NULL;
366 unsigned int length = 0;
370 opj_set_default_decoder_parameters(¶meters);
372 g_strlcpy(parameters.infile, filename,
sizeof(parameters.infile));
379 fsrc = g_fopen(filename,
"rb");
382 fprintf(stderr,
"[j2k_read_profile] Error: failed to open `%s' for reading\n", filename);
383 goto another_end_of_the_world;
385 if(fread(src_header, 1, 12, fsrc) != 12)
389 "[j2k_read_profile] Error: fread returned a number of elements different from the expected.\n");
390 goto another_end_of_the_world;
396 codec = OPJ_CODEC_JP2;
400 codec = OPJ_CODEC_J2K;
404 fprintf(stderr,
"[j2k_read_profile] Error: `%s' has unsupported file format.\n", filename);
405 goto another_end_of_the_world;
412 d_codec = opj_create_decompress(codec);
415 fprintf(stderr,
"[j2k_read_profile] Error: failed to create the decoder\n");
420 if(!opj_setup_decoder(d_codec, ¶meters))
422 fprintf(stderr,
"[j2k_read_profile] Error: failed to setup the decoder %s\n", parameters.infile);
426 d_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
429 fprintf(stderr,
"[j2k_read_profile] Error: failed to create the stream from the file %s\n", parameters.infile);
434 if(!opj_read_header(d_stream, d_codec, &image))
436 fprintf(stderr,
"[j2k_read_profile] Error: failed to read the header\n");
437 opj_stream_destroy(d_stream);
438 opj_destroy_codec(d_codec);
439 opj_image_destroy(image);
444 if(!(opj_decode(d_codec, d_stream, image) && opj_end_decompress(d_codec, d_stream)))
446 fprintf(stderr,
"[j2k_read_profile] Error: failed to decode image!\n");
447 opj_destroy_codec(d_codec);
448 opj_stream_destroy(d_stream);
449 opj_image_destroy(image);
459 opj_stream_destroy(d_stream);
463 fprintf(stderr,
"[j2k_read_profile] Error: failed to decode image `%s'\n", filename);
464 goto another_end_of_the_world;
467 if(image->icc_profile_len > 0 && image->icc_profile_buf)
469 length = image->icc_profile_len;
470 *
out = (uint8_t *)g_malloc(image->icc_profile_len);
471 memcpy(*
out, image->icc_profile_buf, image->icc_profile_len);
474another_end_of_the_world:
476 opj_destroy_codec(d_codec);
479 opj_image_destroy(image);
571 int *
d0, *d1, *d2, *
r, *
g, *b;
572 const int *y, *cb, *cr;
573 size_t maxw, maxh,
max;
577 i = img->comps[0].prec;
578 offset = 1 << (
i - 1);
581 maxw = img->comps[0].w;
582 maxh = img->comps[0].h;
585 y = img->comps[0].data;
586 cb = img->comps[1].data;
587 cr = img->comps[2].data;
589 d0 =
r = (
int *)calloc(
max,
sizeof(
int));
590 d1 =
g = (
int *)calloc(
max,
sizeof(
int));
591 d2 = b = (
int *)calloc(
max,
sizeof(
int));
593 for(
i = 0;
i < maxh; ++
i)
595 for(j = 0; j < maxw; j += 2)
613 img->comps[0].data =
d0;
615 img->comps[1].data = d1;
617 img->comps[2].data = d2;
619 img->comps[1].w = maxw;
620 img->comps[1].h = maxh;
621 img->comps[2].w = maxw;
622 img->comps[2].h = maxh;
623 img->comps[1].dx = img->comps[0].dx;
624 img->comps[2].dx = img->comps[0].dx;
625 img->comps[1].dy = img->comps[0].dy;
626 img->comps[2].dy = img->comps[0].dy;
631 const int offset = 1 << (img->comps[0].prec - 1);
632 const int upb = (1 << img->comps[0].prec) - 1;
634 size_t maxw = img->comps[0].w;
635 size_t maxh = img->comps[0].h;
636 size_t max = maxw * maxh;
638 const int *y = img->comps[0].data;
639 const int *cb = img->comps[1].data;
640 const int *cr = img->comps[2].data;
642 int *
d0, *d1, *d2, *
r, *
g, *b;
643 d0 =
r = (
int *)calloc(
max,
sizeof(
int));
644 d1 =
g = (
int *)calloc(
max,
sizeof(
int));
645 d2 = b = (
int *)calloc(
max,
sizeof(
int));
647 for(
int i = 0;
i < maxh;
i += 2)
649 const int *ny = y + maxw;
654 for(
int j = 0; j < maxw; j += 2)
668 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
674 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
688 img->comps[0].data =
d0;
690 img->comps[1].data = d1;
692 img->comps[2].data = d2;
694 img->comps[1].w = maxw;
695 img->comps[1].h = maxh;
696 img->comps[2].w = maxw;
697 img->comps[2].h = maxh;
698 img->comps[1].dx = img->comps[0].dx;
699 img->comps[2].dx = img->comps[0].dx;
700 img->comps[1].dy = img->comps[0].dy;
701 img->comps[2].dy = img->comps[0].dy;
706 if(img->numcomps < 3)
708 img->color_space = OPJ_CLRSPC_GRAY;
712 if((img->comps[0].dx == 1) && (img->comps[1].dx == 2) && (img->comps[2].dx == 2) && (img->comps[0].dy == 1)
713 && (img->comps[1].dy == 2) && (img->comps[2].dy == 2))
717 else if((img->comps[0].dx == 1) && (img->comps[1].dx == 2) && (img->comps[2].dx == 2)
718 && (img->comps[0].dy == 1) && (img->comps[1].dy == 1)
719 && (img->comps[2].dy == 1))
723 else if((img->comps[0].dx == 1) && (img->comps[1].dx == 1) && (img->comps[2].dx == 1)
724 && (img->comps[0].dy == 1) && (img->comps[1].dy == 1)
725 && (img->comps[2].dy == 1))
731 fprintf(stderr,
"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__, __LINE__);
734 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)