33#define JP2_FILEHEADER_SIZE 12
36 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A};
37static const char J2K_HEAD[] = {0xFF, 0x4F, 0xFF, 0x51, 0x00};
79 return OPJ_CODEC_UNKNOWN;
112#define PIXEL_LOOPER_BEGIN(_rect) \
113 for (y = h - 1; y != uint(-1); y--) { \
114 for (i = y * w, i_next = (y + 1) * w; i < i_next; i++, _rect += 4) {
116#define PIXEL_LOOPER_BEGIN_CHANNELS(_rect, _channels) \
117 for (y = h - 1; y != uint(-1); y--) { \
118 for (i = y * w, i_next = (y + 1) * w; i < i_next; i++, _rect += _channels) {
120#define PIXEL_LOOPER_END \
136 if (comp.w ==
w && comp.h == h) {
142 if (comp.data ==
nullptr || comp.w == 0 || comp.h == 0) {
145 return r_buffer.
data();
149 const int *src_row = comp.data + size_t(
uint64_t(
y) * comp.h / h) * size_t(comp.w);
150 int *dst_row = r_buffer.
data() + size_t(
y) * size_t(
w);
155 return r_buffer.
data();
176 OPJ_UINT32 l_nb_read;
178 if (p_file->
cur + p_nb_bytes < p_file->buf + p_file->
len) {
179 l_nb_read = p_nb_bytes;
182 l_nb_read = (OPJ_UINT32)(p_file->
buf + p_file->
len - p_file->
cur);
184 memcpy(p_buffer, p_file->
cur, l_nb_read);
185 p_file->
cur += l_nb_read;
187 return l_nb_read ? l_nb_read : ((OPJ_SIZE_T)-1);
191static OPJ_SIZE_T opj_write_from_buffer(
void *p_buffer, OPJ_SIZE_T p_nb_bytes,
void *p_user_data)
193 struct BufInfo *p_file = p_user_data;
194 memcpy(p_file->cur, p_buffer, p_nb_bytes);
195 p_file->cur += p_nb_bytes;
196 p_file->len += p_nb_bytes;
204 if (p_file->
cur + p_nb_bytes < p_file->buf + p_file->
len) {
205 p_file->
cur += p_nb_bytes;
209 return (OPJ_OFF_T)-1;
215 if (p_nb_bytes < p_file->
len) {
216 p_file->
cur = p_file->
buf + p_nb_bytes;
229 OPJ_BOOL p_is_read_stream)
231 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
232 if (l_stream ==
nullptr) {
236 opj_stream_set_user_data_length(l_stream, p_file->
len);
239 opj_stream_set_write_function(l_stream, opj_write_from_buffer);
255 FILE *f =
static_cast<FILE *
>(p_user_data);
261 FILE *p_file =
static_cast<FILE *
>(p_user_data);
262 OPJ_OFF_T file_length = 0;
264 fseek(p_file, 0, SEEK_END);
265 file_length = ftell(p_file);
266 fseek(p_file, 0, SEEK_SET);
268 return (OPJ_UINT64)file_length;
273 FILE *p_file =
static_cast<FILE *
>(p_user_data);
274 OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file);
275 return l_nb_read ? l_nb_read : (OPJ_SIZE_T)-1;
280 FILE *p_file =
static_cast<FILE *
>(p_user_data);
281 return fwrite(p_buffer, 1, p_nb_bytes, p_file);
286 FILE *p_file =
static_cast<FILE *
>(p_user_data);
287 if (fseek(p_file, p_nb_bytes, SEEK_CUR)) {
295 FILE *p_file =
static_cast<FILE *
>(p_user_data);
296 if (fseek(p_file, p_nb_bytes, SEEK_SET)) {
309 OPJ_BOOL p_is_read_stream,
312 FILE *p_file =
BLI_fopen(filepath, p_is_read_stream ?
"rb" :
"wb");
313 if (p_file ==
nullptr) {
317 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
318 if (l_stream ==
nullptr) {
339 OPJ_CODEC_FORMAT p_format,
341 ImFileColorSpace &r_colorspace);
351 buf_wrapper.
buf = mem;
352 buf_wrapper.
cur = mem;
353 buf_wrapper.
len = OPJ_OFF_T(size);
355 &buf_wrapper, OPJ_J2K_STREAM_CHUNK_SIZE,
true);
357 opj_stream_destroy(stream);
362 const OPJ_CODEC_FORMAT format,
366 if (format == OPJ_CODEC_UNKNOWN) {
370 ImBuf *ibuf =
nullptr;
371 bool use_float =
false;
372 bool use_alpha =
false;
375 long signed_offsets[4] = {0, 0, 0, 0};
376 int float_divs[4] = {1, 1, 1, 1};
378 uint i, i_next,
w, h;
381 const int *r, *g, *
b, *a;
385 opj_dparameters_t parameters;
387 opj_image_t *
image =
nullptr;
388 opj_codec_t *codec =
nullptr;
391 opj_set_default_decoder_parameters(¶meters);
396 codec = opj_create_decompress(format);
406 if (opj_setup_decoder(codec, ¶meters) ==
false) {
410 if (opj_read_header(stream, codec, &
image) ==
false) {
416 if (opj_decode(codec, stream,
image) ==
false) {
426 used_comps = std::min<uint>(
image->numcomps, 4);
431 for (i = 0; i < used_comps; i++) {
433 h = std::max(h,
uint(
image->comps[i].h));
436 switch (
image->numcomps) {
460 if (
image->comps[i].prec > 8) {
464 if (
image->comps[i].sgnd) {
465 signed_offsets[i] = long(1) << (
image->comps[i].prec - 1);
469 float_divs[i] = (1 <<
image->comps[i].prec) - 1;
474 if (ibuf ==
nullptr) {
479 ibuf->
ftype = IMB_FTYPE_JP2;
490 if (
image->numcomps < 3) {
506 if (
image->numcomps < 3) {
510 rect_float[0] = rect_float[1] = rect_float[2] =
float(r[i] + signed_offsets[0]) /
512 rect_float[3] = (a[i] + signed_offsets[1]) / float_divs[1];
518 rect_float[0] = rect_float[1] = rect_float[2] =
float(r[i] + signed_offsets[0]) /
520 rect_float[3] = 1.0f;
529 rect_float[0] =
float(r[i] + signed_offsets[0]) / float_divs[0];
530 rect_float[1] =
float(g[i] + signed_offsets[1]) / float_divs[1];
531 rect_float[2] =
float(
b[i] + signed_offsets[2]) / float_divs[2];
532 rect_float[3] =
float(a[i] + signed_offsets[3]) / float_divs[3];
538 rect_float[0] =
float(r[i] + signed_offsets[0]) / float_divs[0];
539 rect_float[1] =
float(g[i] + signed_offsets[1]) / float_divs[1];
540 rect_float[2] =
float(
b[i] + signed_offsets[2]) / float_divs[2];
541 rect_float[3] = 1.0f;
550 if (
image->numcomps < 3) {
554 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
555 rect_uchar[3] = a[i] + signed_offsets[1];
561 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
571 rect_uchar[0] = r[i] + signed_offsets[0];
572 rect_uchar[1] = g[i] + signed_offsets[1];
573 rect_uchar[2] =
b[i] + signed_offsets[2];
574 rect_uchar[3] = a[i] + signed_offsets[3];
580 rect_uchar[0] = r[i] + signed_offsets[0];
581 rect_uchar[1] = g[i] + signed_offsets[1];
582 rect_uchar[2] =
b[i] + signed_offsets[2];
598 opj_destroy_codec(codec);
602 opj_image_destroy(
image);
609static opj_image_t *rawtoimage(
const char *
filename,
610 opj_cparameters_t *parameters,
611 raw_cparameters_t *raw_cp)
618# define UPSAMPLE_8_TO_12(_val) ((_val << 4) | (_val & ((1 << 4) - 1)))
619# define UPSAMPLE_8_TO_16(_val) ((_val << 8) + _val)
621# define DOWNSAMPLE_FLOAT_TO_8BIT(_val) \
622 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : int(255.0f * (_val)))
623# define DOWNSAMPLE_FLOAT_TO_12BIT(_val) \
624 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : int(4095.0f * (_val)))
625# define DOWNSAMPLE_FLOAT_TO_16BIT(_val) \
626 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : int(65535.0f * (_val)))
631 return (_val << 4) | (_val & ((1 << 4) - 1));
635 return (_val << 8) + _val;
640 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : int(255.0f * (_val)));
644 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : int(4095.0f * (_val)));
648 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : int(65535.0f * (_val)));
663#define CINEMA_24_CS 1302083
664#define CINEMA_48_CS 651041
665#define COMP_24_CS 1041666
666#define COMP_48_CS 520833
674 POC[0].resno1 = numres - 1;
676 POC[0].prg1 = OPJ_CPRL;
678 POC[1].resno0 = numres - 1;
681 POC[1].resno1 = numres;
683 POC[1].prg1 = OPJ_CPRL;
689 parameters->tile_size_on = 0;
690 parameters->cp_tdx = 1;
691 parameters->cp_tdy = 1;
694 parameters->tp_flag =
'C';
695 parameters->tp_on = 1;
698 parameters->cp_tx0 = 0;
699 parameters->cp_ty0 = 0;
700 parameters->image_offset_x0 = 0;
701 parameters->image_offset_y0 = 0;
704 parameters->cblockw_init = 32;
705 parameters->cblockh_init = 32;
706 parameters->csty |= 0x01;
709 parameters->prog_order = OPJ_CPRL;
712 parameters->roi_compno = -1;
714 parameters->subsampling_dx = 1;
715 parameters->subsampling_dy = 1;
718 parameters->irreversible = 1;
728 switch (parameters->cp_cinema) {
729 case OPJ_CINEMA2K_24:
730 case OPJ_CINEMA2K_48:
731 parameters->numresolution = std::min(parameters->numresolution, 6);
732 if (!((
image->comps[0].w == 2048) || (
image->comps[0].h == 1080))) {
734 "Image coordinates %u x %u is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
735 "(2K profile) compliance requires that at least one of coordinates match 2048 x "
739 parameters->cp_rsiz = OPJ_STD_RSIZ;
742 parameters->cp_rsiz = OPJ_CINEMA2K;
746 case OPJ_CINEMA4K_24:
747 if (parameters->numresolution < 1) {
748 parameters->numresolution = 1;
750 else if (parameters->numresolution > 7) {
751 parameters->numresolution = 7;
753 if (!((
image->comps[0].w == 4096) || (
image->comps[0].h == 2160))) {
755 "Image coordinates %u x %u is not 4K compliant.\nJPEG Digital Cinema Profile-4"
756 "(4K profile) compliance requires that at least one of coordinates match 4096 x "
760 parameters->cp_rsiz = OPJ_STD_RSIZ;
763 parameters->cp_rsiz = OPJ_CINEMA4K;
765 parameters->numpocs =
init_4K_poc(parameters->POC, parameters->numresolution);
772 switch (parameters->cp_cinema) {
773 case OPJ_CINEMA2K_24:
774 case OPJ_CINEMA4K_24:
775 for (i = 0; i < parameters->tcp_numlayers; i++) {
777 if (img_fol->
rates[i] == 0) {
784 image->comps[0].prec) /
793 parameters->tcp_rates[i] = img_fol->
rates[i];
800 case OPJ_CINEMA2K_48:
801 for (i = 0; i < parameters->tcp_numlayers; i++) {
803 if (img_fol->
rates[i] == 0) {
810 image->comps[0].prec) /
819 parameters->tcp_rates[i] = img_fol->
rates[i];
829 parameters->cp_disto_alloc = 1;
840 float *rect_float, from_straight[4];
842 uint subsampling_dx = parameters->subsampling_dx;
843 uint subsampling_dy = parameters->subsampling_dy;
845 uint i, i_next, numcomps,
w, h, prec;
848 OPJ_COLOR_SPACE color_space;
849 opj_image_cmptparm_t cmptparm[4];
850 opj_image_t *
image =
nullptr;
868 if (ibuf->
x == 4096 || ibuf->
y == 2160) {
869 parameters->cp_cinema = OPJ_CINEMA4K_24;
873 parameters->cp_cinema = OPJ_CINEMA2K_48;
876 parameters->cp_cinema = OPJ_CINEMA2K_24;
879 if (parameters->cp_cinema) {
882 for (i = 0; i < parameters->tcp_numlayers; i++) {
883 img_fol.
rates[i] = parameters->tcp_rates[i];
888 color_space = (ibuf->
foptions.
flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
920 color_space = OPJ_CLRSPC_GRAY;
923 color_space = (ibuf->
foptions.
flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
931 memset(&cmptparm, 0,
sizeof(opj_image_cmptparm_t[4]));
932 for (i = 0; i < numcomps; i++) {
933 cmptparm[i].prec = prec;
935#if (OPJ_VERSION_MAJOR < 2) || (OPJ_VERSION_MAJOR == 2 && OPJ_VERSION_MINOR < 5)
936 cmptparm[i].bpp = prec;
938 cmptparm[i].sgnd = 0;
939 cmptparm[i].dx = subsampling_dx;
940 cmptparm[i].dy = subsampling_dy;
945 image = opj_image_create(numcomps, &cmptparm[0], color_space);
952 image->x0 = parameters->image_offset_x0;
953 image->y0 = parameters->image_offset_y0;
962 r =
image->comps[0].data;
963 g = (numcomps >= 3) ?
image->comps[1].data :
nullptr;
964 b = (numcomps >= 3) ?
image->comps[2].data :
nullptr;
965 a = (numcomps == 4) ?
image->comps[3].data : (numcomps == 2) ?
image->comps[1].data :
nullptr;
967 if (rect_float && rect_uchar && prec == 8) {
969 rect_float =
nullptr;
978 if (channels_in_float == 4) {
994 else if (channels_in_float == 3) {
1027 if (channels_in_float == 4) {
1043 else if (channels_in_float == 3) {
1076 if (channels_in_float == 4) {
1092 else if (channels_in_float == 3) {
1130 r[i] = rect_uchar[0];
1132 g[i] = rect_uchar[1];
1135 b[i] = rect_uchar[2];
1138 a[i] = rect_uchar[3];
1179 parameters->tcp_mct =
image->numcomps == 3 ? 1 : 0;
1181 if (parameters->cp_cinema) {
1185 if (img_fol.
rates) {
1186 MEM_delete(img_fol.
rates);
1197 filepath, OPJ_J2K_STREAM_CHUNK_SIZE,
false,
nullptr);
1198 if (stream ==
nullptr) {
1202 opj_stream_destroy(stream);
1211 opj_cparameters_t parameters;
1212 opj_image_t *
image =
nullptr;
1215 opj_set_default_encoder_parameters(¶meters);
1220 parameters.tcp_rates[0] = ((100 - quality) / 90.0f * 99.0f) + 1;
1222 parameters.tcp_numlayers = 1;
1223 parameters.cp_disto_alloc = 1;
1228 constexpr int64_t megapixels_threshold = 12 * 1000 * 1000;
1230 if (total_pixels >= megapixels_threshold) {
1231 parameters.tile_size_on = OPJ_TRUE;
1232 parameters.cp_tdx = 1024;
1233 parameters.cp_tdy = 1024;
1234 parameters.cp_tx0 = 0;
1235 parameters.cp_ty0 = 0;
1240 opj_codec_t *codec =
nullptr;
1245 OPJ_CODEC_FORMAT format = OPJ_CODEC_JP2;
1247 format = OPJ_CODEC_J2K;
1250 format = OPJ_CODEC_JP2;
1253 codec = opj_create_compress(format);
1263 if (opj_setup_encoder(codec, ¶meters,
image) ==
false) {
1272 if (opj_start_compress(codec,
image, stream) ==
false) {
1275 if (opj_encode(codec, stream) ==
false) {
1278 if (opj_end_compress(codec, stream) ==
false) {
1288 opj_destroy_codec(codec);
1293 opj_image_destroy(
image);
File and directory operations.
#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)
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
void fill(const T &value) const
void reinitialize(const int64_t new_size)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
std::string filename(const std::string_view &str, size_t pos)
const char * imb_file_extensions_jp2[]
ImBuf * imb_load_jp2(const uchar *mem, size_t size, ImBufFlags flags, ImFileColorSpace &r_colorspace)
static void opj_free_from_file(void *p_user_data)
unsigned __int64 uint64_t
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_12BIT(const float _val)
bool imb_save_jp2(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
static const char JP2_HEAD[]
static OPJ_CODEC_FORMAT format_from_header(const uchar mem[JP2_FILEHEADER_SIZE], const size_t size)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
BLI_INLINE int UPSAMPLE_8_TO_12(const uchar _val)
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static ImBuf * imb_load_jp2_stream(opj_stream_t *stream, OPJ_CODEC_FORMAT p_format, ImBufFlags flags, ImFileColorSpace &r_colorspace)
static OPJ_BOOL opj_seek_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
static void error_callback(const char *msg, void *)
static void info_callback(const char *msg, void *)
int BLI_system_thread_count()
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_16BIT(const float _val)
static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
static float channel_colormanage_noop(float value)
static bool check_jp2(const uchar *mem, const size_t size)
static void opj_read_from_buffer_free(void *)
static opj_stream_t * opj_stream_create_from_buffer(BufInfo *p_file, OPJ_UINT32 p_size, OPJ_BOOL p_is_read_stream)
static OPJ_UINT64 opj_get_data_length_from_file(void *p_user_data)
static OPJ_SIZE_T opj_read_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
static OPJ_SIZE_T opj_read_from_buffer(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_8BIT(const float _val)
BLI_INLINE int UPSAMPLE_8_TO_16(const uchar _val)
static void warning_callback(const char *msg, void *)
static const char J2K_HEAD[]
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static void cinema_parameters(opj_cparameters_t *parameters)
static OPJ_OFF_T opj_skip_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
static OPJ_SIZE_T opj_write_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
static bool check_j2k(const uchar *mem, const size_t size)
static int init_4K_poc(opj_poc_t *POC, int numres)
void IMB_byte_from_float(ImBuf *ibuf)
bool imb_is_a_jp2(const uchar *buf, size_t size)
bool imb_save_jp2_stream(ImBuf *ibuf, opj_stream_t *stream, ImBufFlags flags)
static const int * jp2_component_at_size(const opj_image_comp_t &comp, const uint w, const uint h, Array< int > &r_buffer)
static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
static opj_image_t * ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters)
float linearrgb_to_srgb(float c)
static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *image, img_fol_t *img_fol)
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
static opj_stream_t * opj_stream_create_from_file(const char *filepath, OPJ_UINT32 p_size, OPJ_BOOL p_is_read_stream, FILE **r_file)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const ColorSpace * colorspace
ImbFormatOptions foptions
ImBufFloatBuffer float_buffer
uint8_t * byte_data_for_write()
bool colorspace_is_data() const
float * float_data_for_write()