27#define JP2_FILEHEADER_SIZE 12
30 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A};
31static const char J2K_HEAD[] = {0xFF, 0x4F, 0xFF, 0x51, 0x00};
73 return OPJ_CODEC_UNKNOWN;
106#define PIXEL_LOOPER_BEGIN(_rect) \
107 for (y = h - 1; y != uint(-1); y--) { \
108 for (i = y * w, i_next = (y + 1) * w; i < i_next; i++, _rect += 4) {
110#define PIXEL_LOOPER_BEGIN_CHANNELS(_rect, _channels) \
111 for (y = h - 1; y != uint(-1); y--) { \
112 for (i = y * w, i_next = (y + 1) * w; i < i_next; i++, _rect += _channels) {
114#define PIXEL_LOOPER_END \
137 OPJ_UINT32 l_nb_read;
139 if (p_file->
cur + p_nb_bytes < p_file->buf + p_file->
len) {
140 l_nb_read = p_nb_bytes;
143 l_nb_read = (OPJ_UINT32)(p_file->
buf + p_file->
len - p_file->
cur);
145 memcpy(p_buffer, p_file->
cur, l_nb_read);
146 p_file->
cur += l_nb_read;
148 return l_nb_read ? l_nb_read : ((OPJ_SIZE_T)-1);
152static OPJ_SIZE_T opj_write_from_buffer(
void *p_buffer, OPJ_SIZE_T p_nb_bytes,
void *p_user_data)
154 struct BufInfo *p_file = p_user_data;
155 memcpy(p_file->
cur, p_buffer, p_nb_bytes);
156 p_file->
cur += p_nb_bytes;
157 p_file->
len += p_nb_bytes;
165 if (p_file->
cur + p_nb_bytes < p_file->
buf + p_file->
len) {
166 p_file->
cur += p_nb_bytes;
170 return (OPJ_OFF_T)-1;
176 if (p_nb_bytes < p_file->
len) {
177 p_file->
cur = p_file->
buf + p_nb_bytes;
190 OPJ_BOOL p_is_read_stream)
192 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
193 if (l_stream ==
nullptr) {
197 opj_stream_set_user_data_length(l_stream, p_file->
len);
200 opj_stream_set_write_function(l_stream, opj_write_from_buffer);
216 FILE *f =
static_cast<FILE *
>(p_user_data);
222 FILE *p_file =
static_cast<FILE *
>(p_user_data);
223 OPJ_OFF_T file_length = 0;
225 fseek(p_file, 0, SEEK_END);
226 file_length = ftell(p_file);
227 fseek(p_file, 0, SEEK_SET);
229 return (OPJ_UINT64)file_length;
234 FILE *p_file =
static_cast<FILE *
>(p_user_data);
235 OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file);
236 return l_nb_read ? l_nb_read : (OPJ_SIZE_T)-1;
241 FILE *p_file =
static_cast<FILE *
>(p_user_data);
242 return fwrite(p_buffer, 1, p_nb_bytes, p_file);
247 FILE *p_file =
static_cast<FILE *
>(p_user_data);
248 if (fseek(p_file, p_nb_bytes, SEEK_CUR)) {
256 FILE *p_file =
static_cast<FILE *
>(p_user_data);
257 if (fseek(p_file, p_nb_bytes, SEEK_SET)) {
270 OPJ_BOOL p_is_read_stream,
273 FILE *p_file =
BLI_fopen(filepath, p_is_read_stream ?
"rb" :
"wb");
274 if (p_file ==
nullptr) {
278 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
279 if (l_stream ==
nullptr) {
300 OPJ_CODEC_FORMAT p_format,
309 buf_wrapper.
buf = mem;
310 buf_wrapper.
cur = mem;
311 buf_wrapper.
len = OPJ_OFF_T(
size);
313 &buf_wrapper, OPJ_J2K_STREAM_CHUNK_SIZE,
true);
315 opj_stream_destroy(stream);
321 FILE *p_file =
nullptr;
324 filepath, OPJ_J2K_STREAM_CHUNK_SIZE,
true, &p_file);
329 if (fread(mem,
sizeof(mem), 1, p_file) !=
sizeof(mem)) {
330 opj_stream_destroy(stream);
334 fseek(p_file, 0, SEEK_SET);
338 opj_stream_destroy(stream);
343 const OPJ_CODEC_FORMAT
format,
347 if (
format == OPJ_CODEC_UNKNOWN) {
351 ImBuf *ibuf =
nullptr;
352 bool use_float =
false;
353 bool use_alpha =
false;
355 long signed_offsets[4] = {0, 0, 0, 0};
356 int float_divs[4] = {1, 1, 1, 1};
358 uint i, i_next,
w, h, planes;
360 const int *r, *g, *
b, *a;
362 opj_dparameters_t parameters;
364 opj_image_t *image =
nullptr;
365 opj_codec_t *codec =
nullptr;
368 opj_set_default_decoder_parameters(¶meters);
373 codec = opj_create_decompress(
format);
383 if (opj_setup_decoder(codec, ¶meters) ==
false) {
387 if (opj_read_header(stream, codec, &image) ==
false) {
393 if (opj_decode(codec, stream, image) ==
false) {
398 if ((image->numcomps * image->x1 * image->y1) == 0) {
403 w = image->comps[0].w;
404 h = image->comps[0].h;
406 switch (image->numcomps) {
419 i = std::min<uint>(
i, 4);
424 if (image->comps[
i].prec > 8) {
428 if (image->comps[
i].sgnd) {
429 signed_offsets[
i] =
long(1) << (image->comps[
i].prec - 1);
433 float_divs[
i] = (1 << image->comps[
i].prec) - 1;
438 if (ibuf ==
nullptr) {
442 ibuf->
ftype = IMB_FTYPE_JP2;
453 if (image->numcomps < 3) {
454 r = image->comps[0].data;
458 a = image->comps[1].data;
460 rect_float[0] = rect_float[1] = rect_float[2] =
float(r[
i] + signed_offsets[0]) /
462 rect_float[3] = (a[
i] + signed_offsets[1]) / float_divs[1];
468 rect_float[0] = rect_float[1] = rect_float[2] =
float(r[
i] + signed_offsets[0]) /
470 rect_float[3] = 1.0f;
476 r = image->comps[0].data;
477 g = image->comps[1].data;
478 b = image->comps[2].data;
482 a = image->comps[3].data;
484 rect_float[0] =
float(r[
i] + signed_offsets[0]) / float_divs[0];
485 rect_float[1] =
float(g[
i] + signed_offsets[1]) / float_divs[1];
486 rect_float[2] =
float(
b[
i] + signed_offsets[2]) / float_divs[2];
487 rect_float[3] =
float(a[
i] + signed_offsets[3]) / float_divs[3];
493 rect_float[0] =
float(r[
i] + signed_offsets[0]) / float_divs[0];
494 rect_float[1] =
float(g[
i] + signed_offsets[1]) / float_divs[1];
495 rect_float[2] =
float(
b[
i] + signed_offsets[2]) / float_divs[2];
496 rect_float[3] = 1.0f;
505 if (image->numcomps < 3) {
506 r = image->comps[0].data;
510 a = image->comps[3].data;
512 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[
i] + signed_offsets[0]);
513 rect_uchar[3] = a[
i] + signed_offsets[1];
519 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[
i] + signed_offsets[0]);
526 r = image->comps[0].data;
527 g = image->comps[1].data;
528 b = image->comps[2].data;
532 a = image->comps[3].data;
534 rect_uchar[0] = r[
i] + signed_offsets[0];
535 rect_uchar[1] = g[
i] + signed_offsets[1];
536 rect_uchar[2] =
b[
i] + signed_offsets[2];
537 rect_uchar[3] = a[
i] + signed_offsets[3];
543 rect_uchar[0] = r[
i] + signed_offsets[0];
544 rect_uchar[1] = g[
i] + signed_offsets[1];
545 rect_uchar[2] =
b[
i] + signed_offsets[2];
561 opj_destroy_codec(codec);
565 opj_image_destroy(image);
572static opj_image_t *rawtoimage(
const char *filename,
573 opj_cparameters_t *parameters,
574 raw_cparameters_t *raw_cp)
581# define UPSAMPLE_8_TO_12(_val) ((_val << 4) | (_val & ((1 << 4) - 1)))
582# define UPSAMPLE_8_TO_16(_val) ((_val << 8) + _val)
584# define DOWNSAMPLE_FLOAT_TO_8BIT(_val) \
585 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : int(255.0f * (_val)))
586# define DOWNSAMPLE_FLOAT_TO_12BIT(_val) \
587 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : int(4095.0f * (_val)))
588# define DOWNSAMPLE_FLOAT_TO_16BIT(_val) \
589 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : int(65535.0f * (_val)))
594 return (_val << 4) | (_val & ((1 << 4) - 1));
598 return (_val << 8) + _val;
603 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : int(255.0f * (_val)));
607 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : int(4095.0f * (_val)));
611 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : int(65535.0f * (_val)));
626#define CINEMA_24_CS 1302083
627#define CINEMA_48_CS 651041
628#define COMP_24_CS 1041666
629#define COMP_48_CS 520833
637 POC[0].resno1 = numres - 1;
639 POC[0].prg1 = OPJ_CPRL;
641 POC[1].resno0 = numres - 1;
644 POC[1].resno1 = numres;
646 POC[1].prg1 = OPJ_CPRL;
652 parameters->tile_size_on = 0;
653 parameters->cp_tdx = 1;
654 parameters->cp_tdy = 1;
657 parameters->tp_flag =
'C';
658 parameters->tp_on = 1;
661 parameters->cp_tx0 = 0;
662 parameters->cp_ty0 = 0;
663 parameters->image_offset_x0 = 0;
664 parameters->image_offset_y0 = 0;
667 parameters->cblockw_init = 32;
668 parameters->cblockh_init = 32;
669 parameters->csty |= 0x01;
672 parameters->prog_order = OPJ_CPRL;
675 parameters->roi_compno = -1;
677 parameters->subsampling_dx = 1;
678 parameters->subsampling_dy = 1;
681 parameters->irreversible = 1;
691 switch (parameters->cp_cinema) {
692 case OPJ_CINEMA2K_24:
693 case OPJ_CINEMA2K_48:
694 parameters->numresolution = std::min(parameters->numresolution, 6);
695 if (!((image->comps[0].w == 2048) || (image->comps[0].h == 1080))) {
697 "Image coordinates %u x %u is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
698 "(2K profile) compliance requires that at least one of coordinates match 2048 x "
702 parameters->cp_rsiz = OPJ_STD_RSIZ;
705 parameters->cp_rsiz = OPJ_CINEMA2K;
709 case OPJ_CINEMA4K_24:
710 if (parameters->numresolution < 1) {
711 parameters->numresolution = 1;
713 else if (parameters->numresolution > 7) {
714 parameters->numresolution = 7;
716 if (!((image->comps[0].w == 4096) || (image->comps[0].h == 2160))) {
718 "Image coordinates %u x %u is not 4K compliant.\nJPEG Digital Cinema Profile-4"
719 "(4K profile) compliance requires that at least one of coordinates match 4096 x "
723 parameters->cp_rsiz = OPJ_STD_RSIZ;
726 parameters->cp_rsiz = OPJ_CINEMA4K;
728 parameters->numpocs =
init_4K_poc(parameters->POC, parameters->numresolution);
735 switch (parameters->cp_cinema) {
736 case OPJ_CINEMA2K_24:
737 case OPJ_CINEMA4K_24:
738 for (
i = 0;
i < parameters->tcp_numlayers;
i++) {
740 if (img_fol->
rates[
i] == 0) {
741 parameters->tcp_rates[0] =
float(image->numcomps * image->comps[0].w *
742 image->comps[0].h * image->comps[0].prec) /
743 (
CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
746 temp_rate =
float(image->numcomps * image->comps[0].w * image->comps[0].h *
747 image->comps[0].prec) /
748 (img_fol->
rates[
i] * 8 * image->comps[0].dx * image->comps[0].dy);
750 parameters->tcp_rates[
i] =
float(image->numcomps * image->comps[0].w *
751 image->comps[0].h * image->comps[0].prec) /
756 parameters->tcp_rates[
i] = img_fol->
rates[
i];
763 case OPJ_CINEMA2K_48:
764 for (
i = 0;
i < parameters->tcp_numlayers;
i++) {
766 if (img_fol->
rates[
i] == 0) {
767 parameters->tcp_rates[0] =
float(image->numcomps * image->comps[0].w *
768 image->comps[0].h * image->comps[0].prec) /
769 (
CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
772 temp_rate =
float(image->numcomps * image->comps[0].w * image->comps[0].h *
773 image->comps[0].prec) /
774 (img_fol->
rates[
i] * 8 * image->comps[0].dx * image->comps[0].dy);
776 parameters->tcp_rates[0] =
float(image->numcomps * image->comps[0].w *
777 image->comps[0].h * image->comps[0].prec) /
782 parameters->tcp_rates[
i] = img_fol->
rates[
i];
792 parameters->cp_disto_alloc = 1;
803 float *rect_float, from_straight[4];
805 uint subsampling_dx = parameters->subsampling_dx;
806 uint subsampling_dy = parameters->subsampling_dy;
808 uint i, i_next, numcomps,
w, h, prec;
811 OPJ_COLOR_SPACE color_space;
812 opj_image_cmptparm_t cmptparm[4];
813 opj_image_t *image =
nullptr;
831 if (ibuf->
x == 4096 || ibuf->
y == 2160) {
832 parameters->cp_cinema = OPJ_CINEMA4K_24;
836 parameters->cp_cinema = OPJ_CINEMA2K_48;
839 parameters->cp_cinema = OPJ_CINEMA2K_24;
842 if (parameters->cp_cinema) {
844 for (
i = 0;
i < parameters->tcp_numlayers;
i++) {
845 img_fol.
rates[
i] = parameters->tcp_rates[
i];
850 color_space = (ibuf->
foptions.
flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
856 color_space = (ibuf->
foptions.
flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
870 numcomps = (ibuf->
planes == 32) ? 4 : 3;
877 memset(&cmptparm, 0,
sizeof(opj_image_cmptparm_t[4]));
878 for (
i = 0;
i < numcomps;
i++) {
879 cmptparm[
i].prec = prec;
881#if (OPJ_VERSION_MAJOR < 2) || (OPJ_VERSION_MAJOR == 2 && OPJ_VERSION_MINOR < 5)
882 cmptparm[
i].bpp = prec;
884 cmptparm[
i].sgnd = 0;
885 cmptparm[
i].dx = subsampling_dx;
886 cmptparm[
i].dy = subsampling_dy;
891 image = opj_image_create(numcomps, &cmptparm[0], color_space);
898 image->x0 = parameters->image_offset_x0;
899 image->y0 = parameters->image_offset_y0;
900 image->x1 = image->x0 + (
w - 1) * subsampling_dx + 1 + image->x0;
901 image->y1 = image->y0 + (h - 1) * subsampling_dy + 1 + image->y0;
908 r = image->comps[0].data;
909 g = image->comps[1].data;
910 b = image->comps[2].data;
911 a = (numcomps == 4) ? image->comps[3].data :
nullptr;
913 if (rect_float && rect_uchar && prec == 8) {
915 rect_float =
nullptr;
924 if (channels_in_float == 4) {
934 else if (channels_in_float == 3) {
953 if (channels_in_float == 4) {
962 else if (channels_in_float == 3) {
982 if (channels_in_float == 4) {
992 else if (channels_in_float == 3) {
1011 if (channels_in_float == 4) {
1020 else if (channels_in_float == 3) {
1039 if (numcomps == 4) {
1040 if (channels_in_float == 4) {
1050 else if (channels_in_float == 3) {
1069 if (channels_in_float == 4) {
1078 else if (channels_in_float == 3) {
1101 if (numcomps == 4) {
1103 r[
i] = rect_uchar[0];
1104 g[
i] = rect_uchar[1];
1105 b[
i] = rect_uchar[2];
1106 a[
i] = rect_uchar[3];
1112 r[
i] = rect_uchar[0];
1113 g[
i] = rect_uchar[1];
1114 b[
i] = rect_uchar[2];
1121 if (numcomps == 4) {
1141 if (numcomps == 4) {
1163 parameters->tcp_mct = image->numcomps == 3 ? 1 : 0;
1165 if (parameters->cp_cinema) {
1169 if (img_fol.
rates) {
1181 filepath, OPJ_J2K_STREAM_CHUNK_SIZE,
false,
nullptr);
1182 if (stream ==
nullptr) {
1186 opj_stream_destroy(stream);
1195 opj_cparameters_t parameters;
1196 opj_image_t *image =
nullptr;
1199 opj_set_default_encoder_parameters(¶meters);
1204 parameters.tcp_rates[0] = ((100 - quality) / 90.0f * 99.0f) + 1;
1206 parameters.tcp_numlayers = 1;
1207 parameters.cp_disto_alloc = 1;
1211 opj_codec_t *codec =
nullptr;
1216 OPJ_CODEC_FORMAT
format = OPJ_CODEC_JP2;
1224 codec = opj_create_compress(
format);
1234 if (opj_setup_encoder(codec, ¶meters, image) ==
false) {
1238 if (opj_start_compress(codec, image, stream) ==
false) {
1241 if (opj_encode(codec, stream) ==
false) {
1244 if (opj_end_compress(codec, stream) ==
false) {
1254 opj_destroy_codec(codec);
1259 opj_image_destroy(image);
File and directory operations.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
float linearrgb_to_srgb(float c)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
#define CLOG_STR_ERROR(clg_ref, str)
#define CLOG_STR_WARN(clg_ref, str)
#define CLOG_STR_INFO(clg_ref, str)
void IMB_byte_from_float(ImBuf *ibuf)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
@ IMB_COLORMANAGE_IS_DATA
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
void MEM_freeN(void *vmemh)
static void(* error_callback)(const char *)
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImbFormatOptions foptions
ImBufByteBuffer byte_buffer