22#define JP2_FILEHEADER_SIZE 12
25 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A};
26static const char J2K_HEAD[] = {0xFF, 0x4F, 0xFF, 0x51, 0x00};
68 return OPJ_CODEC_UNKNOWN;
81 FILE *stream = (FILE *)client_data;
82 fprintf(stream,
"[ERROR] %s", msg);
89 FILE *stream = (FILE *)client_data;
90 fprintf(stream,
"[WARNING] %s", msg);
99 FILE *stream = (FILE *)client_data;
100 fprintf(stream,
"[INFO] %s", msg);
104#define PIXEL_LOOPER_BEGIN(_rect) \
105 for (y = h - 1; y != uint(-1); y--) { \
106 for (i = y * w, i_next = (y + 1) * w; i < i_next; i++, _rect += 4) {
108#define PIXEL_LOOPER_BEGIN_CHANNELS(_rect, _channels) \
109 for (y = h - 1; y != uint(-1); y--) { \
110 for (i = y * w, i_next = (y + 1) * w; i < i_next; i++, _rect += _channels) {
112#define PIXEL_LOOPER_END \
135 OPJ_UINT32 l_nb_read;
137 if (p_file->
cur + p_nb_bytes < p_file->buf + p_file->
len) {
138 l_nb_read = p_nb_bytes;
141 l_nb_read = (OPJ_UINT32)(p_file->
buf + p_file->
len - p_file->
cur);
143 memcpy(p_buffer, p_file->
cur, l_nb_read);
144 p_file->
cur += l_nb_read;
146 return l_nb_read ? l_nb_read : ((OPJ_SIZE_T)-1);
150static OPJ_SIZE_T opj_write_from_buffer(
void *p_buffer, OPJ_SIZE_T p_nb_bytes,
void *p_user_data)
152 struct BufInfo *p_file = p_user_data;
153 memcpy(p_file->
cur, p_buffer, p_nb_bytes);
154 p_file->
cur += p_nb_bytes;
155 p_file->
len += p_nb_bytes;
163 if (p_file->
cur + p_nb_bytes < p_file->
buf + p_file->
len) {
164 p_file->
cur += p_nb_bytes;
168 return (OPJ_OFF_T)-1;
174 if (p_nb_bytes < p_file->
len) {
175 p_file->
cur = p_file->
buf + p_nb_bytes;
188 OPJ_BOOL p_is_read_stream)
190 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
191 if (l_stream ==
nullptr) {
195 opj_stream_set_user_data_length(l_stream, p_file->
len);
198 opj_stream_set_write_function(l_stream, opj_write_from_buffer);
214 FILE *f =
static_cast<FILE *
>(p_user_data);
220 FILE *p_file =
static_cast<FILE *
>(p_user_data);
221 OPJ_OFF_T file_length = 0;
223 fseek(p_file, 0, SEEK_END);
224 file_length = ftell(p_file);
225 fseek(p_file, 0, SEEK_SET);
227 return (OPJ_UINT64)file_length;
232 FILE *p_file =
static_cast<FILE *
>(p_user_data);
233 OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file);
234 return l_nb_read ? l_nb_read : (OPJ_SIZE_T)-1;
239 FILE *p_file =
static_cast<FILE *
>(p_user_data);
240 return fwrite(p_buffer, 1, p_nb_bytes, p_file);
245 FILE *p_file =
static_cast<FILE *
>(p_user_data);
246 if (fseek(p_file, p_nb_bytes, SEEK_CUR)) {
254 FILE *p_file =
static_cast<FILE *
>(p_user_data);
255 if (fseek(p_file, p_nb_bytes, SEEK_SET)) {
268 OPJ_BOOL p_is_read_stream,
271 FILE *p_file =
BLI_fopen(filepath, p_is_read_stream ?
"rb" :
"wb");
272 if (p_file ==
nullptr) {
276 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
277 if (l_stream ==
nullptr) {
298 OPJ_CODEC_FORMAT p_format,
307 buf_wrapper.
buf = mem;
308 buf_wrapper.
cur = mem;
309 buf_wrapper.
len = OPJ_OFF_T(size);
311 &buf_wrapper, OPJ_J2K_STREAM_CHUNK_SIZE,
true);
313 opj_stream_destroy(stream);
319 FILE *p_file =
nullptr;
322 filepath, OPJ_J2K_STREAM_CHUNK_SIZE,
true, &p_file);
327 if (fread(mem,
sizeof(mem), 1, p_file) !=
sizeof(mem)) {
328 opj_stream_destroy(stream);
332 fseek(p_file, 0, SEEK_SET);
336 opj_stream_destroy(stream);
341 const OPJ_CODEC_FORMAT
format,
345 if (
format == OPJ_CODEC_UNKNOWN) {
349 ImBuf *ibuf =
nullptr;
350 bool use_float =
false;
351 bool use_alpha =
false;
353 long signed_offsets[4] = {0, 0, 0, 0};
354 int float_divs[4] = {1, 1, 1, 1};
356 uint i, i_next,
w, h, planes;
358 const int *r, *g, *
b, *a;
362 opj_image_t *image =
nullptr;
363 opj_codec_t *codec =
nullptr;
369 opj_set_default_decoder_parameters(&
parameters);
374 codec = opj_create_decompress(
format);
384 if (opj_setup_decoder(codec, &
parameters) ==
false) {
388 if (opj_read_header(stream, codec, &image) ==
false) {
389 printf(
"OpenJPEG error: failed to read the header\n");
394 if (opj_decode(codec, stream, image) ==
false) {
395 fprintf(stderr,
"ERROR -> j2k_to_image: failed to decode image!\n");
399 if ((image->numcomps * image->x1 * image->y1) == 0) {
400 fprintf(stderr,
"\nError: invalid raw image parameters\n");
404 w = image->comps[0].w;
405 h = image->comps[0].h;
407 switch (image->numcomps) {
427 if (image->comps[i].prec > 8) {
431 if (image->comps[i].sgnd) {
432 signed_offsets[i] = long(1) << (image->comps[i].prec - 1);
436 float_divs[i] = (1 << image->comps[i].prec) - 1;
441 if (ibuf ==
nullptr) {
445 ibuf->
ftype = IMB_FTYPE_JP2;
456 if (image->numcomps < 3) {
457 r = image->comps[0].data;
461 a = image->comps[1].data;
463 rect_float[0] = rect_float[1] = rect_float[2] =
float(r[i] + signed_offsets[0]) /
465 rect_float[3] = (a[i] + signed_offsets[1]) / float_divs[1];
471 rect_float[0] = rect_float[1] = rect_float[2] =
float(r[i] + signed_offsets[0]) /
473 rect_float[3] = 1.0f;
479 r = image->comps[0].data;
480 g = image->comps[1].data;
481 b = image->comps[2].data;
485 a = image->comps[3].data;
487 rect_float[0] =
float(r[i] + signed_offsets[0]) / float_divs[0];
488 rect_float[1] =
float(g[i] + signed_offsets[1]) / float_divs[1];
489 rect_float[2] =
float(
b[i] + signed_offsets[2]) / float_divs[2];
490 rect_float[3] =
float(a[i] + signed_offsets[3]) / float_divs[3];
496 rect_float[0] =
float(r[i] + signed_offsets[0]) / float_divs[0];
497 rect_float[1] =
float(g[i] + signed_offsets[1]) / float_divs[1];
498 rect_float[2] =
float(
b[i] + signed_offsets[2]) / float_divs[2];
499 rect_float[3] = 1.0f;
508 if (image->numcomps < 3) {
509 r = image->comps[0].data;
513 a = image->comps[3].data;
515 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
516 rect_uchar[3] = a[i] + signed_offsets[1];
522 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
529 r = image->comps[0].data;
530 g = image->comps[1].data;
531 b = image->comps[2].data;
535 a = image->comps[3].data;
537 rect_uchar[0] = r[i] + signed_offsets[0];
538 rect_uchar[1] = g[i] + signed_offsets[1];
539 rect_uchar[2] =
b[i] + signed_offsets[2];
540 rect_uchar[3] = a[i] + signed_offsets[3];
546 rect_uchar[0] = r[i] + signed_offsets[0];
547 rect_uchar[1] = g[i] + signed_offsets[1];
548 rect_uchar[2] =
b[i] + signed_offsets[2];
564 opj_destroy_codec(codec);
568 opj_image_destroy(image);
575static opj_image_t *rawtoimage(
const char *filename,
577 raw_cparameters_t *raw_cp)
584# define UPSAMPLE_8_TO_12(_val) ((_val << 4) | (_val & ((1 << 4) - 1)))
585# define UPSAMPLE_8_TO_16(_val) ((_val << 8) + _val)
587# define DOWNSAMPLE_FLOAT_TO_8BIT(_val) \
588 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : int(255.0f * (_val)))
589# define DOWNSAMPLE_FLOAT_TO_12BIT(_val) \
590 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : int(4095.0f * (_val)))
591# define DOWNSAMPLE_FLOAT_TO_16BIT(_val) \
592 (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : int(65535.0f * (_val)))
597 return (_val << 4) | (_val & ((1 << 4) - 1));
601 return (_val << 8) + _val;
606 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 :
int(255.0f * (_val)));
610 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 :
int(4095.0f * (_val)));
614 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 :
int(65535.0f * (_val)));
629#define CINEMA_24_CS 1302083
630#define CINEMA_48_CS 651041
631#define COMP_24_CS 1041666
632#define COMP_48_CS 520833
640 POC[0].resno1 = numres - 1;
642 POC[0].prg1 = OPJ_CPRL;
644 POC[1].resno0 = numres - 1;
647 POC[1].resno1 = numres;
649 POC[1].prg1 = OPJ_CPRL;
695 case OPJ_CINEMA2K_24:
696 case OPJ_CINEMA2K_48:
700 if (!((image->comps[0].w == 2048) || (image->comps[0].h == 1080))) {
702 "Image coordinates %u x %u is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
703 "(2K profile) compliance requires that at least one of coordinates match 2048 x "
714 case OPJ_CINEMA4K_24:
721 if (!((image->comps[0].w == 4096) || (image->comps[0].h == 2160))) {
723 "Image coordinates %u x %u is not 4K compliant.\nJPEG Digital Cinema Profile-4"
724 "(4K profile) compliance requires that at least one of coordinates match 4096 x "
741 case OPJ_CINEMA2K_24:
742 case OPJ_CINEMA4K_24:
743 for (i = 0; i <
parameters->tcp_numlayers; i++) {
745 if (img_fol->
rates[i] == 0) {
747 image->comps[0].h * image->comps[0].prec) /
748 (
CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
751 temp_rate =
float(image->numcomps * image->comps[0].w * image->comps[0].h *
752 image->comps[0].prec) /
753 (img_fol->
rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
756 image->comps[0].h * image->comps[0].prec) /
768 case OPJ_CINEMA2K_48:
769 for (i = 0; i <
parameters->tcp_numlayers; i++) {
771 if (img_fol->
rates[i] == 0) {
773 image->comps[0].h * image->comps[0].prec) /
774 (
CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
777 temp_rate =
float(image->numcomps * image->comps[0].w * image->comps[0].h *
778 image->comps[0].prec) /
779 (img_fol->
rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
782 image->comps[0].h * image->comps[0].prec) /
808 float *rect_float, from_straight[4];
813 uint i, i_next, numcomps,
w, h, prec;
816 OPJ_COLOR_SPACE color_space;
817 opj_image_cmptparm_t cmptparm[4];
818 opj_image_t *image =
nullptr;
836 if (ibuf->
x == 4096 || ibuf->
y == 2160) {
849 for (i = 0; i <
parameters->tcp_numlayers; i++) {
855 color_space = (ibuf->
foptions.
flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
861 color_space = (ibuf->
foptions.
flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
875 numcomps = (ibuf->
planes == 32) ? 4 : 3;
882 memset(&cmptparm, 0,
sizeof(opj_image_cmptparm_t[4]));
883 for (i = 0; i < numcomps; i++) {
884 cmptparm[i].prec = prec;
886#if (OPJ_VERSION_MAJOR < 2) || (OPJ_VERSION_MAJOR == 2 && OPJ_VERSION_MINOR < 5)
887 cmptparm[i].bpp = prec;
889 cmptparm[i].sgnd = 0;
890 cmptparm[i].dx = subsampling_dx;
891 cmptparm[i].dy = subsampling_dy;
896 image = opj_image_create(numcomps, &cmptparm[0], color_space);
898 printf(
"Error: opj_image_create() failed\n");
905 image->x1 = image->x0 + (
w - 1) * subsampling_dx + 1 + image->x0;
906 image->y1 = image->y0 + (h - 1) * subsampling_dy + 1 + image->y0;
913 r = image->comps[0].data;
914 g = image->comps[1].data;
915 b = image->comps[2].data;
916 a = (numcomps == 4) ? image->comps[3].data :
nullptr;
918 if (rect_float && rect_uchar && prec == 8) {
920 rect_float =
nullptr;
929 if (channels_in_float == 4) {
939 else if (channels_in_float == 3) {
958 if (channels_in_float == 4) {
967 else if (channels_in_float == 3) {
987 if (channels_in_float == 4) {
997 else if (channels_in_float == 3) {
1016 if (channels_in_float == 4) {
1025 else if (channels_in_float == 3) {
1044 if (numcomps == 4) {
1045 if (channels_in_float == 4) {
1055 else if (channels_in_float == 3) {
1074 if (channels_in_float == 4) {
1083 else if (channels_in_float == 3) {
1106 if (numcomps == 4) {
1108 r[i] = rect_uchar[0];
1109 g[i] = rect_uchar[1];
1110 b[i] = rect_uchar[2];
1111 a[i] = rect_uchar[3];
1117 r[i] = rect_uchar[0];
1118 g[i] = rect_uchar[1];
1119 b[i] = rect_uchar[2];
1126 if (numcomps == 4) {
1146 if (numcomps == 4) {
1168 parameters->tcp_mct = image->numcomps == 3 ? 1 : 0;
1174 if (img_fol.
rates) {
1186 filepath, OPJ_J2K_STREAM_CHUNK_SIZE,
false,
nullptr);
1187 if (stream ==
nullptr) {
1191 opj_stream_destroy(stream);
1201 opj_image_t *image =
nullptr;
1204 opj_set_default_encoder_parameters(&
parameters);
1209 parameters.tcp_rates[0] = ((100 - quality) / 90.0f * 99.0f) + 1;
1216 opj_codec_t *codec =
nullptr;
1221 OPJ_CODEC_FORMAT
format = OPJ_CODEC_JP2;
1229 codec = opj_create_compress(
format);
1239 if (opj_setup_encoder(codec, &
parameters, image) ==
false) {
1243 if (opj_start_compress(codec, image, stream) ==
false) {
1246 if (opj_encode(codec, stream) ==
false) {
1249 if (opj_end_compress(codec, stream) ==
false) {
1259 opj_destroy_codec(codec);
1264 opj_image_destroy(image);
1268 fprintf(stderr,
"failed to encode image\n");
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)
@ COLOR_ROLE_DEFAULT_BYTE
void IMB_rect_from_float(ImBuf *ibuf)
Contains defines and structs used throughout the imbuf module.
@ IMB_COLORMANAGE_IS_DATA
Read Guarded memory(de)allocation.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
void colorspace_set_default_role(char *colorspace, int size, int role)
input_tx image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "preview_img") .compute_source("compositor_compute_preview.glsl") .do_static_compilation(true)
local_group_size(16, 16) .push_constant(Type b
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
struct ImBuf * IMB_allocImBuf(unsigned int, unsigned int, unsigned char, unsigned int)
#define PIXEL_LOOPER_BEGIN_CHANNELS(_rect, _channels)
static OPJ_SIZE_T opj_read_from_buffer(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 opj_stream_t * opj_stream_create_from_file(const char *filepath, OPJ_UINT32 p_size, OPJ_BOOL p_is_read_stream, FILE **r_file)
BLI_INLINE int UPSAMPLE_8_TO_12(const uchar _val)
ImBuf * imb_load_jp2(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
static void cinema_parameters(opj_cparameters_t *parameters)
static OPJ_SIZE_T opj_write_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
static opj_image_t * ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters)
static OPJ_CODEC_FORMAT format_from_header(const uchar mem[JP2_FILEHEADER_SIZE], const size_t size)
static OPJ_BOOL opj_seek_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
static const char J2K_HEAD[]
static opj_stream_t * opj_stream_create_from_buffer(BufInfo *p_file, OPJ_UINT32 p_size, OPJ_BOOL p_is_read_stream)
#define JP2_FILEHEADER_SIZE
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_16BIT(const float _val)
static OPJ_SIZE_T opj_read_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *image, img_fol_t *img_fol)
static void warning_callback(const char *msg, void *client_data)
bool imb_is_a_jp2(const uchar *buf, size_t size)
static float channel_colormanage_noop(float value)
static OPJ_UINT64 opj_get_data_length_from_file(void *p_user_data)
BLI_INLINE int UPSAMPLE_8_TO_16(const uchar _val)
static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
bool imb_save_jp2_stream(ImBuf *ibuf, opj_stream_t *stream, int flags)
static OPJ_OFF_T opj_skip_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
bool imb_save_jp2(ImBuf *ibuf, const char *filepath, int flags)
static int init_4K_poc(opj_poc_t *POC, int numres)
static void opj_read_from_buffer_free(void *)
static ImBuf * imb_load_jp2_stream(opj_stream_t *stream, OPJ_CODEC_FORMAT p_format, int flags, char colorspace[IM_MAX_SPACE])
static bool check_jp2(const uchar *mem, const size_t size)
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_8BIT(const float _val)
static void opj_free_from_file(void *p_user_data)
#define PIXEL_LOOPER_BEGIN(_rect)
static const char JP2_HEAD[]
ImBuf * imb_load_jp2_filepath(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
static void info_callback(const char *msg, void *client_data)
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_12BIT(const float _val)
double parameters[NUM_PARAMETERS]
void *(* MEM_mallocN)(size_t len, const char *str)
void MEM_freeN(void *vmemh)
static void(* error_callback)(const char *)
ImBufFloatBuffer float_buffer
ImbFormatOptions foptions
ImBufByteBuffer byte_buffer