Blender V5.3
format_jp2.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "BLI_array.hh"
12#include "BLI_fileops.hh"
13#include "BLI_threads.hh"
14
16#include "IMB_filetype.hh"
17#include "IMB_imbuf.hh"
18#include "IMB_imbuf_types.hh"
19
20#include "CLG_log.h"
21
22#include "openjpeg.h"
23
24#include <algorithm>
25#include <cstring>
26
27namespace blender {
28
29const char *imb_file_extensions_jp2[] = {".jp2", ".j2c", nullptr};
30
31static CLG_LogRef LOG = {"image.openjpeg"};
32
33#define JP2_FILEHEADER_SIZE 12
34
35static const char JP2_HEAD[] = {
36 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A};
37static const char J2K_HEAD[] = {0xFF, 0x4F, 0xFF, 0x51, 0x00};
38
39/* We only need this because of how the presets are set */
40/* this typedef is copied from 'openjpeg-1.5.0/applications/codec/image_to_j2k.c' */
41struct img_fol_t {
51 float *rates;
52};
53
54static bool check_jp2(const uchar *mem, const size_t size) /* J2K_CFMT */
55{
56 if (size < sizeof(JP2_HEAD)) {
57 return false;
58 }
59 return memcmp(JP2_HEAD, mem, sizeof(JP2_HEAD)) ? false : true;
60}
61
62static bool check_j2k(const uchar *mem, const size_t size) /* J2K_CFMT */
63{
64 if (size < sizeof(J2K_HEAD)) {
65 return false;
66 }
67 return memcmp(J2K_HEAD, mem, sizeof(J2K_HEAD)) ? false : true;
68}
69
70static OPJ_CODEC_FORMAT format_from_header(const uchar mem[JP2_FILEHEADER_SIZE], const size_t size)
71{
72 if (check_jp2(mem, size)) {
73 return OPJ_CODEC_JP2;
74 }
75 if (check_j2k(mem, size)) {
76 return OPJ_CODEC_J2K;
77 }
78
79 return OPJ_CODEC_UNKNOWN;
80}
81
82bool imb_is_a_jp2(const uchar *buf, size_t size)
83{
84 return (check_jp2(buf, size) || check_j2k(buf, size));
85}
86
90static void error_callback(const char *msg, void * /*client_data*/)
91{
92 CLOG_STR_ERROR(&LOG, msg);
93}
94
97static void warning_callback(const char *msg, void * /*client_data*/)
98{
99 CLOG_STR_WARN(&LOG, msg);
100}
101
102#ifndef NDEBUG
106static void info_callback(const char *msg, void * /*client_data*/)
107{
108 CLOG_STR_INFO(&LOG, msg);
109}
110#endif
111
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) {
115
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) {
119
120#define PIXEL_LOOPER_END \
121 } \
122 } \
123 (void)0
124
130static const int *jp2_component_at_size(const opj_image_comp_t &comp,
131 const uint w,
132 const uint h,
133 Array<int> &r_buffer)
134{
135 /* Size already matches, just return it. */
136 if (comp.w == w && comp.h == h) {
137 return comp.data;
138 }
139
140 r_buffer.reinitialize(size_t(w) * size_t(h));
141
142 if (comp.data == nullptr || comp.w == 0 || comp.h == 0) {
143 /* Nothing to sample from. */
144 r_buffer.fill(0);
145 return r_buffer.data();
146 }
147
148 for (uint y = 0; y < h; y++) {
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);
151 for (uint x = 0; x < w; x++) {
152 dst_row[x] = src_row[uint64_t(x) * comp.w / w];
153 }
154 }
155 return r_buffer.data();
156}
157
158/* -------------------------------------------------------------------- */
161
162struct BufInfo {
163 const uchar *buf;
164 const uchar *cur;
165 OPJ_OFF_T len;
166};
167
168static void opj_read_from_buffer_free(void * /*p_user_data*/)
169{
170 /* NOP. */
171}
172
173static OPJ_SIZE_T opj_read_from_buffer(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
174{
175 BufInfo *p_file = static_cast<BufInfo *>(p_user_data);
176 OPJ_UINT32 l_nb_read;
177
178 if (p_file->cur + p_nb_bytes < p_file->buf + p_file->len) {
179 l_nb_read = p_nb_bytes;
180 }
181 else {
182 l_nb_read = (OPJ_UINT32)(p_file->buf + p_file->len - p_file->cur);
183 }
184 memcpy(p_buffer, p_file->cur, l_nb_read);
185 p_file->cur += l_nb_read;
186
187 return l_nb_read ? l_nb_read : ((OPJ_SIZE_T)-1);
188}
189
190#if 0
191static OPJ_SIZE_T opj_write_from_buffer(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
192{
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;
197 return p_nb_bytes;
198}
199#endif
200
201static OPJ_OFF_T opj_skip_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
202{
203 BufInfo *p_file = static_cast<BufInfo *>(p_user_data);
204 if (p_file->cur + p_nb_bytes < p_file->buf + p_file->len) {
205 p_file->cur += p_nb_bytes;
206 return p_nb_bytes;
207 }
208 p_file->cur = p_file->buf + p_file->len;
209 return (OPJ_OFF_T)-1;
210}
211
212static OPJ_BOOL opj_seek_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
213{
214 BufInfo *p_file = static_cast<BufInfo *>(p_user_data);
215 if (p_nb_bytes < p_file->len) {
216 p_file->cur = p_file->buf + p_nb_bytes;
217 return OPJ_TRUE;
218 }
219 p_file->cur = p_file->buf + p_file->len;
220 return OPJ_FALSE;
221}
222
227static opj_stream_t *opj_stream_create_from_buffer(BufInfo *p_file,
228 OPJ_UINT32 p_size,
229 OPJ_BOOL p_is_read_stream)
230{
231 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
232 if (l_stream == nullptr) {
233 return nullptr;
234 }
235 opj_stream_set_user_data(l_stream, p_file, opj_read_from_buffer_free);
236 opj_stream_set_user_data_length(l_stream, p_file->len);
237 opj_stream_set_read_function(l_stream, opj_read_from_buffer);
238#if 0 /* UNUSED */
239 opj_stream_set_write_function(l_stream, opj_write_from_buffer);
240#endif
241 opj_stream_set_skip_function(l_stream, opj_skip_from_buffer);
242 opj_stream_set_seek_function(l_stream, opj_seek_from_buffer);
243
244 return l_stream;
245}
246
248
249/* -------------------------------------------------------------------- */
252
253static void opj_free_from_file(void *p_user_data)
254{
255 FILE *f = static_cast<FILE *>(p_user_data);
256 fclose(f);
257}
258
259static OPJ_UINT64 opj_get_data_length_from_file(void *p_user_data)
260{
261 FILE *p_file = static_cast<FILE *>(p_user_data);
262 OPJ_OFF_T file_length = 0;
263
264 fseek(p_file, 0, SEEK_END);
265 file_length = ftell(p_file);
266 fseek(p_file, 0, SEEK_SET);
267
268 return (OPJ_UINT64)file_length;
269}
270
271static OPJ_SIZE_T opj_read_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
272{
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;
276}
277
278static OPJ_SIZE_T opj_write_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
279{
280 FILE *p_file = static_cast<FILE *>(p_user_data);
281 return fwrite(p_buffer, 1, p_nb_bytes, p_file);
282}
283
284static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
285{
286 FILE *p_file = static_cast<FILE *>(p_user_data);
287 if (fseek(p_file, p_nb_bytes, SEEK_CUR)) {
288 return -1;
289 }
290 return p_nb_bytes;
291}
292
293static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
294{
295 FILE *p_file = static_cast<FILE *>(p_user_data);
296 if (fseek(p_file, p_nb_bytes, SEEK_SET)) {
297 return OPJ_FALSE;
298 }
299 return OPJ_TRUE;
300}
301
306
307static opj_stream_t *opj_stream_create_from_file(const char *filepath,
308 OPJ_UINT32 p_size,
309 OPJ_BOOL p_is_read_stream,
310 FILE **r_file)
311{
312 FILE *p_file = BLI_fopen(filepath, p_is_read_stream ? "rb" : "wb");
313 if (p_file == nullptr) {
314 return nullptr;
315 }
316
317 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
318 if (l_stream == nullptr) {
319 fclose(p_file);
320 return nullptr;
321 }
322
323 opj_stream_set_user_data(l_stream, p_file, opj_free_from_file);
324 opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
325 opj_stream_set_write_function(l_stream, opj_write_from_file);
326 opj_stream_set_read_function(l_stream, opj_read_from_file);
327 opj_stream_set_skip_function(l_stream, opj_skip_from_file);
328 opj_stream_set_seek_function(l_stream, opj_seek_from_file);
329
330 if (r_file) {
331 *r_file = p_file;
332 }
333 return l_stream;
334}
335
337
338static ImBuf *imb_load_jp2_stream(opj_stream_t *stream,
339 OPJ_CODEC_FORMAT p_format,
340 ImBufFlags flags,
341 ImFileColorSpace &r_colorspace);
342
344 size_t size,
345 ImBufFlags flags,
346 ImFileColorSpace &r_colorspace)
347{
348 const OPJ_CODEC_FORMAT format = (size > JP2_FILEHEADER_SIZE) ? format_from_header(mem, size) :
349 OPJ_CODEC_UNKNOWN;
350 BufInfo buf_wrapper = {};
351 buf_wrapper.buf = mem;
352 buf_wrapper.cur = mem;
353 buf_wrapper.len = OPJ_OFF_T(size);
354 opj_stream_t *stream = opj_stream_create_from_buffer(
355 &buf_wrapper, OPJ_J2K_STREAM_CHUNK_SIZE, true);
356 ImBuf *ibuf = imb_load_jp2_stream(stream, format, flags, r_colorspace);
357 opj_stream_destroy(stream);
358 return ibuf;
359}
360
361static ImBuf *imb_load_jp2_stream(opj_stream_t *stream,
362 const OPJ_CODEC_FORMAT format,
363 ImBufFlags flags,
364 ImFileColorSpace & /*r_colorspace*/)
365{
366 if (format == OPJ_CODEC_UNKNOWN) {
367 return nullptr;
368 }
369
370 ImBuf *ibuf = nullptr;
371 bool use_float = false; /* for precision higher than 8 use float */
372 bool use_alpha = false;
373 ImColorMode color_mode = ImColorMode::RGBA;
374
375 long signed_offsets[4] = {0, 0, 0, 0};
376 int float_divs[4] = {1, 1, 1, 1};
377
378 uint i, i_next, w, h;
379 uint y;
380 uint used_comps;
381 const int *r, *g, *b, *a; /* matching 'opj_image_comp.data' type */
382 /* Storage for components that had to be scaled up to the image size. */
383 Array<int> comp_buffers[4];
384
385 opj_dparameters_t parameters; /* decompression parameters */
386
387 opj_image_t *image = nullptr;
388 opj_codec_t *codec = nullptr; /* handle to a decompressor */
389
390 /* set decoding parameters to default values */
391 opj_set_default_decoder_parameters(&parameters);
392
393 /* JPEG 2000 compressed image data */
394
395 /* get a decoder handle */
396 codec = opj_create_decompress(format);
397
398 /* configure the event callbacks (not required) */
399 opj_set_error_handler(codec, error_callback, nullptr);
400 opj_set_warning_handler(codec, warning_callback, nullptr);
401#ifndef NDEBUG /* too noisy */
402 opj_set_info_handler(codec, info_callback, nullptr);
403#endif
404
405 /* setup the decoder decoding parameters using the current image and user parameters */
406 if (opj_setup_decoder(codec, &parameters) == false) {
407 goto finally;
408 }
409
410 if (opj_read_header(stream, codec, &image) == false) {
411 CLOG_ERROR(&LOG, "Failed to read the header");
412 goto finally;
413 }
414
415 /* decode the stream and fill the image structure */
416 if (opj_decode(codec, stream, image) == false) {
417 CLOG_ERROR(&LOG, "Failed to decode image!");
418 goto finally;
419 }
420
421 if ((image->numcomps * image->x1 * image->y1) == 0) {
422 CLOG_ERROR(&LOG, "Invalid raw image parameters");
423 goto finally;
424 }
425
426 used_comps = std::min<uint>(image->numcomps, 4);
427
428 /* Components can have different sizes, use the largest one as the image size. */
429 w = 0;
430 h = 0;
431 for (i = 0; i < used_comps; i++) {
432 w = std::max(w, uint(image->comps[i].w));
433 h = std::max(h, uint(image->comps[i].h));
434 }
435
436 switch (image->numcomps) {
437 case 1: /* Gray-scale. */
438 color_mode = ImColorMode::BW;
439 use_alpha = false;
440 break;
441 case 2: /* Gray-scale + alpha. */
442 color_mode = ImColorMode::BW_A;
443 use_alpha = true;
444 break;
445 case 3: /* Color. */
446 color_mode = ImColorMode::RGB;
447 use_alpha = false;
448 break;
449 default: /* 4 or more - assume RGBA. */
450 color_mode = ImColorMode::RGBA;
451 use_alpha = true;
452 break;
453 }
454
455 i = used_comps;
456
457 while (i) {
458 i--;
459
460 if (image->comps[i].prec > 8) {
461 use_float = true;
462 }
463
464 if (image->comps[i].sgnd) {
465 signed_offsets[i] = long(1) << (image->comps[i].prec - 1);
466 }
467
468 /* only needed for float images but doesn't hurt to calc this */
469 float_divs[i] = (1 << image->comps[i].prec) - 1;
470 }
471
473
474 if (ibuf == nullptr) {
475 goto finally;
476 }
477
478 ibuf->color_mode = color_mode;
479 ibuf->ftype = IMB_FTYPE_JP2;
480 if (true /*is_jp2*/) {
481 ibuf->foptions.flag |= JP2_JP2;
482 }
483 else {
484 ibuf->foptions.flag |= JP2_J2K;
485 }
486
487 /* Component data, scaled up to the image size when a component is smaller. */
488 r = jp2_component_at_size(image->comps[0], w, h, comp_buffers[0]);
489 g = b = a = nullptr;
490 if (image->numcomps < 3) {
491 if (use_alpha) {
492 a = jp2_component_at_size(image->comps[1], w, h, comp_buffers[1]);
493 }
494 }
495 else {
496 g = jp2_component_at_size(image->comps[1], w, h, comp_buffers[1]);
497 b = jp2_component_at_size(image->comps[2], w, h, comp_buffers[2]);
498 if (use_alpha) {
499 a = jp2_component_at_size(image->comps[3], w, h, comp_buffers[3]);
500 }
501 }
502
503 if (use_float) {
504 float *rect_float = ibuf->float_data_for_write();
505
506 if (image->numcomps < 3) {
507 /* Gray-scale 12bits+ */
508 if (use_alpha) {
509 PIXEL_LOOPER_BEGIN (rect_float) {
510 rect_float[0] = rect_float[1] = rect_float[2] = float(r[i] + signed_offsets[0]) /
511 float_divs[0];
512 rect_float[3] = (a[i] + signed_offsets[1]) / float_divs[1];
513 }
515 }
516 else {
517 PIXEL_LOOPER_BEGIN (rect_float) {
518 rect_float[0] = rect_float[1] = rect_float[2] = float(r[i] + signed_offsets[0]) /
519 float_divs[0];
520 rect_float[3] = 1.0f;
521 }
523 }
524 }
525 else {
526 /* RGB or RGBA 12bits+ */
527 if (use_alpha) {
528 PIXEL_LOOPER_BEGIN (rect_float) {
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];
533 }
535 }
536 else {
537 PIXEL_LOOPER_BEGIN (rect_float) {
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;
542 }
544 }
545 }
546 }
547 else {
548 uchar *rect_uchar = ibuf->byte_data_for_write();
549
550 if (image->numcomps < 3) {
551 /* Gray-scale. */
552 if (use_alpha) {
553 PIXEL_LOOPER_BEGIN (rect_uchar) {
554 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
555 rect_uchar[3] = a[i] + signed_offsets[1];
556 }
558 }
559 else {
560 PIXEL_LOOPER_BEGIN (rect_uchar) {
561 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
562 rect_uchar[3] = 255;
563 }
565 }
566 }
567 else {
568 /* 8bit RGB or RGBA */
569 if (use_alpha) {
570 PIXEL_LOOPER_BEGIN (rect_uchar) {
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];
575 }
577 }
578 else {
579 PIXEL_LOOPER_BEGIN (rect_uchar) {
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];
583 rect_uchar[3] = 255;
584 }
586 }
587 }
588 }
589
590 if (flag_is_set(flags, ImBufFlags::ByteData)) {
592 }
593
594finally:
595
596 /* free remaining structures */
597 if (codec) {
598 opj_destroy_codec(codec);
599 }
600
601 if (image) {
602 opj_image_destroy(image);
603 }
604
605 return ibuf;
606}
607
608#if 0
609static opj_image_t *rawtoimage(const char *filename,
610 opj_cparameters_t *parameters,
611 raw_cparameters_t *raw_cp)
612#endif
613/* prec can be 8, 12, 16 */
614
615/* Use inline because the float passed can be a function call
616 * that would end up being called many times. */
617#if 0
618# define UPSAMPLE_8_TO_12(_val) ((_val << 4) | (_val & ((1 << 4) - 1)))
619# define UPSAMPLE_8_TO_16(_val) ((_val << 8) + _val)
620
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)))
627#else
628
630{
631 return (_val << 4) | (_val & ((1 << 4) - 1));
632}
634{
635 return (_val << 8) + _val;
636}
637
639{
640 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : int(255.0f * (_val)));
641}
643{
644 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : int(4095.0f * (_val)));
645}
647{
648 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : int(65535.0f * (_val)));
649}
650#endif
651
652/*
653 * 2048x1080 (2K) at 24 fps or 48 fps, or 4096x2160 (4K) at 24 fps;
654 * 3x12 bits per pixel, XYZ color space
655 *
656 * - In 2K, for Scope (2.39:1) presentation 2048x858 pixels of the image is used
657 * - In 2K, for Flat (1.85:1) presentation 1998x1080 pixels of the image is used
658 */
659
660/* ****************************** COPIED FROM image_to_j2k.c */
661
662/* ----------------------------------------------------------------------- */
663#define CINEMA_24_CS 1302083 /* Code-stream length for 24fps. */
664#define CINEMA_48_CS 651041 /* Code-stream length for 48fps. */
665#define COMP_24_CS 1041666 /* Maximum size per color component for 2K & 4K @ 24fps. */
666#define COMP_48_CS 520833 /* Maximum size per color component for 2K @ 48fps. */
667
668static int init_4K_poc(opj_poc_t *POC, int numres)
669{
670 POC[0].tile = 1;
671 POC[0].resno0 = 0;
672 POC[0].compno0 = 0;
673 POC[0].layno1 = 1;
674 POC[0].resno1 = numres - 1;
675 POC[0].compno1 = 3;
676 POC[0].prg1 = OPJ_CPRL;
677 POC[1].tile = 1;
678 POC[1].resno0 = numres - 1;
679 POC[1].compno0 = 0;
680 POC[1].layno1 = 1;
681 POC[1].resno1 = numres;
682 POC[1].compno1 = 3;
683 POC[1].prg1 = OPJ_CPRL;
684 return 2;
685}
686
687static void cinema_parameters(opj_cparameters_t *parameters)
688{
689 parameters->tile_size_on = 0; /* false */
690 parameters->cp_tdx = 1;
691 parameters->cp_tdy = 1;
692
693 /* Tile part. */
694 parameters->tp_flag = 'C';
695 parameters->tp_on = 1;
696
697 /* Tile and Image shall be at (0, 0). */
698 parameters->cp_tx0 = 0;
699 parameters->cp_ty0 = 0;
700 parameters->image_offset_x0 = 0;
701 parameters->image_offset_y0 = 0;
702
703 /* Code-block size = 32 * 32. */
704 parameters->cblockw_init = 32;
705 parameters->cblockh_init = 32;
706 parameters->csty |= 0x01;
707
708 /* The progression order shall be CPRL. */
709 parameters->prog_order = OPJ_CPRL;
710
711 /* No ROI */
712 parameters->roi_compno = -1;
713
714 parameters->subsampling_dx = 1;
715 parameters->subsampling_dy = 1;
716
717 /* 9-7 transform */
718 parameters->irreversible = 1;
719}
720
721static void cinema_setup_encoder(opj_cparameters_t *parameters,
722 opj_image_t *image,
723 img_fol_t *img_fol)
724{
725 int i;
726 float temp_rate;
727
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))) {
733 CLOG_WARN(&LOG,
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 "
736 "1080",
737 image->comps[0].w,
738 image->comps[0].h);
739 parameters->cp_rsiz = OPJ_STD_RSIZ;
740 }
741 else {
742 parameters->cp_rsiz = OPJ_CINEMA2K;
743 }
744 break;
745
746 case OPJ_CINEMA4K_24:
747 if (parameters->numresolution < 1) {
748 parameters->numresolution = 1;
749 }
750 else if (parameters->numresolution > 7) {
751 parameters->numresolution = 7;
752 }
753 if (!((image->comps[0].w == 4096) || (image->comps[0].h == 2160))) {
754 CLOG_WARN(&LOG,
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 "
757 "2160",
758 image->comps[0].w,
759 image->comps[0].h);
760 parameters->cp_rsiz = OPJ_STD_RSIZ;
761 }
762 else {
763 parameters->cp_rsiz = OPJ_CINEMA4K;
764 }
765 parameters->numpocs = init_4K_poc(parameters->POC, parameters->numresolution);
766 break;
767 case OPJ_OFF:
768 /* do nothing */
769 break;
770 }
771
772 switch (parameters->cp_cinema) {
773 case OPJ_CINEMA2K_24:
774 case OPJ_CINEMA4K_24:
775 for (i = 0; i < parameters->tcp_numlayers; i++) {
776 temp_rate = 0;
777 if (img_fol->rates[i] == 0) {
778 parameters->tcp_rates[0] = float(image->numcomps * image->comps[0].w *
779 image->comps[0].h * image->comps[0].prec) /
780 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
781 }
782 else {
783 temp_rate = float(image->numcomps * image->comps[0].w * image->comps[0].h *
784 image->comps[0].prec) /
785 (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
786 if (temp_rate > CINEMA_24_CS) {
787 parameters->tcp_rates[i] = float(image->numcomps * image->comps[0].w *
788 image->comps[0].h * image->comps[0].prec) /
789 (CINEMA_24_CS * 8 * image->comps[0].dx *
790 image->comps[0].dy);
791 }
792 else {
793 parameters->tcp_rates[i] = img_fol->rates[i];
794 }
795 }
796 }
797 parameters->max_comp_size = COMP_24_CS;
798 break;
799
800 case OPJ_CINEMA2K_48:
801 for (i = 0; i < parameters->tcp_numlayers; i++) {
802 temp_rate = 0;
803 if (img_fol->rates[i] == 0) {
804 parameters->tcp_rates[0] = float(image->numcomps * image->comps[0].w *
805 image->comps[0].h * image->comps[0].prec) /
806 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
807 }
808 else {
809 temp_rate = float(image->numcomps * image->comps[0].w * image->comps[0].h *
810 image->comps[0].prec) /
811 (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
812 if (temp_rate > CINEMA_48_CS) {
813 parameters->tcp_rates[0] = float(image->numcomps * image->comps[0].w *
814 image->comps[0].h * image->comps[0].prec) /
815 (CINEMA_48_CS * 8 * image->comps[0].dx *
816 image->comps[0].dy);
817 }
818 else {
819 parameters->tcp_rates[i] = img_fol->rates[i];
820 }
821 }
822 }
823 parameters->max_comp_size = COMP_48_CS;
824 break;
825 case OPJ_OFF:
826 /* do nothing */
827 break;
828 }
829 parameters->cp_disto_alloc = 1;
830}
831
832static float channel_colormanage_noop(float value)
833{
834 return value;
835}
836
837static opj_image_t *ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters)
838{
839 uchar *rect_uchar;
840 float *rect_float, from_straight[4];
841
842 uint subsampling_dx = parameters->subsampling_dx;
843 uint subsampling_dy = parameters->subsampling_dy;
844
845 uint i, i_next, numcomps, w, h, prec;
846 uint y;
847 int *r, *g, *b, *a; /* matching 'opj_image_comp.data' type */
848 OPJ_COLOR_SPACE color_space;
849 opj_image_cmptparm_t cmptparm[4]; /* maximum of 4 components */
850 opj_image_t *image = nullptr;
851
852 float (*chanel_colormanage_cb)(float);
853
854 img_fol_t img_fol; /* only needed for cinema presets */
855 memset(&img_fol, 0, sizeof(img_fol_t));
856
857 if (ibuf->float_buffer.colorspace || ibuf->colorspace_is_data()) {
858 /* float buffer was managed already, no need in color space conversion */
859 chanel_colormanage_cb = channel_colormanage_noop;
860 }
861 else {
862 /* standard linear-to-SRGB conversion if float buffer wasn't managed */
863 chanel_colormanage_cb = linearrgb_to_srgb;
864 }
865
866 if (ibuf->foptions.flag & JP2_CINE) {
867
868 if (ibuf->x == 4096 || ibuf->y == 2160) {
869 parameters->cp_cinema = OPJ_CINEMA4K_24;
870 }
871 else {
872 if (ibuf->foptions.flag & JP2_CINE_48FPS) {
873 parameters->cp_cinema = OPJ_CINEMA2K_48;
874 }
875 else {
876 parameters->cp_cinema = OPJ_CINEMA2K_24;
877 }
878 }
879 if (parameters->cp_cinema) {
880 img_fol.rates = MEM_new_array_uninitialized<float>(size_t(parameters->tcp_numlayers),
881 "jp2_rates");
882 for (i = 0; i < parameters->tcp_numlayers; i++) {
883 img_fol.rates[i] = parameters->tcp_rates[i];
884 }
885 cinema_parameters(parameters);
886 }
887
888 color_space = (ibuf->foptions.flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
889 prec = 12;
890 numcomps = 3;
891 }
892 else {
893 /* Get settings from the imbuf */
894 if (ibuf->foptions.flag & JP2_16BIT) {
895 prec = 16;
896 }
897 else if (ibuf->foptions.flag & JP2_12BIT) {
898 prec = 12;
899 }
900 else {
901 prec = 8;
902 }
903
904 switch (ibuf->color_mode) {
905 case ImColorMode::BW:
906 numcomps = 1;
907 break;
909 numcomps = 2;
910 break;
911 case ImColorMode::RGB:
912 numcomps = 3;
913 break;
915 numcomps = 4;
916 break;
917 }
918
919 if (numcomps <= 2) {
920 color_space = OPJ_CLRSPC_GRAY;
921 }
922 else {
923 color_space = (ibuf->foptions.flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
924 }
925 }
926
927 w = ibuf->x;
928 h = ibuf->y;
929
930 /* initialize image components */
931 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t[4]));
932 for (i = 0; i < numcomps; i++) {
933 cmptparm[i].prec = prec;
934 /* Deprecated in openjpeg 2.5. */
935#if (OPJ_VERSION_MAJOR < 2) || (OPJ_VERSION_MAJOR == 2 && OPJ_VERSION_MINOR < 5)
936 cmptparm[i].bpp = prec;
937#endif
938 cmptparm[i].sgnd = 0;
939 cmptparm[i].dx = subsampling_dx;
940 cmptparm[i].dy = subsampling_dy;
941 cmptparm[i].w = w;
942 cmptparm[i].h = h;
943 }
944 /* create the image */
945 image = opj_image_create(numcomps, &cmptparm[0], color_space);
946 if (!image) {
947 CLOG_ERROR(&LOG, "opj_image_create() failed");
948 return nullptr;
949 }
950
951 /* set image offset and reference grid */
952 image->x0 = parameters->image_offset_x0;
953 image->y0 = parameters->image_offset_y0;
954 image->x1 = image->x0 + (w - 1) * subsampling_dx + 1 + image->x0;
955 image->y1 = image->y0 + (h - 1) * subsampling_dy + 1 + image->y0;
956
957 /* set image data */
958 rect_uchar = ibuf->byte_data_for_write();
959 rect_float = ibuf->float_data_for_write();
960
961 /* Set the destination channels. For gray + alpha (numcomps == 2), alpha is component 1. */
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;
966
967 if (rect_float && rect_uchar && prec == 8) {
968 /* No need to use the floating point buffer, just write the 8 bits from the char buffer */
969 rect_float = nullptr;
970 }
971
972 /* `g`, `b`, `a` are null for components not being written. */
973 if (rect_float) {
974 int channels_in_float = ibuf->channels ? ibuf->channels : 4;
975
976 switch (prec) {
977 case 8: /* Convert blenders float color channels to 8, 12 or 16bit ints */
978 if (channels_in_float == 4) {
979 PIXEL_LOOPER_BEGIN (rect_float) {
980 premul_to_straight_v4_v4(from_straight, rect_float);
981 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[0]));
982 if (g) {
983 g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[1]));
984 }
985 if (b) {
986 b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[2]));
987 }
988 if (a) {
989 a[i] = DOWNSAMPLE_FLOAT_TO_8BIT(from_straight[3]);
990 }
991 }
993 }
994 else if (channels_in_float == 3) {
995 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
996 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
997 if (g) {
998 g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[1]));
999 }
1000 if (b) {
1001 b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[2]));
1002 }
1003 if (a) {
1004 a[i] = 255;
1005 }
1006 }
1008 }
1009 else {
1010 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
1011 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
1012 if (g) {
1013 g[i] = r[i];
1014 }
1015 if (b) {
1016 b[i] = r[i];
1017 }
1018 if (a) {
1019 a[i] = 255;
1020 }
1021 }
1023 }
1024 break;
1025
1026 case 12:
1027 if (channels_in_float == 4) {
1028 PIXEL_LOOPER_BEGIN (rect_float) {
1029 premul_to_straight_v4_v4(from_straight, rect_float);
1030 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[0]));
1031 if (g) {
1032 g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[1]));
1033 }
1034 if (b) {
1035 b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[2]));
1036 }
1037 if (a) {
1038 a[i] = DOWNSAMPLE_FLOAT_TO_12BIT(from_straight[3]);
1039 }
1040 }
1042 }
1043 else if (channels_in_float == 3) {
1044 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
1045 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
1046 if (g) {
1047 g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[1]));
1048 }
1049 if (b) {
1050 b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[2]));
1051 }
1052 if (a) {
1053 a[i] = 4095;
1054 }
1055 }
1057 }
1058 else {
1059 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
1060 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
1061 if (g) {
1062 g[i] = r[i];
1063 }
1064 if (b) {
1065 b[i] = r[i];
1066 }
1067 if (a) {
1068 a[i] = 4095;
1069 }
1070 }
1072 }
1073 break;
1074
1075 case 16:
1076 if (channels_in_float == 4) {
1077 PIXEL_LOOPER_BEGIN (rect_float) {
1078 premul_to_straight_v4_v4(from_straight, rect_float);
1079 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[0]));
1080 if (g) {
1081 g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[1]));
1082 }
1083 if (b) {
1084 b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[2]));
1085 }
1086 if (a) {
1087 a[i] = DOWNSAMPLE_FLOAT_TO_16BIT(from_straight[3]);
1088 }
1089 }
1091 }
1092 else if (channels_in_float == 3) {
1093 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
1094 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
1095 if (g) {
1096 g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[1]));
1097 }
1098 if (b) {
1099 b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[2]));
1100 }
1101 if (a) {
1102 a[i] = 65535;
1103 }
1104 }
1106 }
1107 else {
1108 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
1109 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
1110 if (g) {
1111 g[i] = r[i];
1112 }
1113 if (b) {
1114 b[i] = r[i];
1115 }
1116 if (a) {
1117 a[i] = 65535;
1118 }
1119 }
1121 }
1122 break;
1123 }
1124 }
1125 else {
1126 /* Just use rect. Alpha lives at byte index 3 in the #ImBuf for both RGBA and BW_A. */
1127 switch (prec) {
1128 case 8:
1129 PIXEL_LOOPER_BEGIN (rect_uchar) {
1130 r[i] = rect_uchar[0];
1131 if (g) {
1132 g[i] = rect_uchar[1];
1133 }
1134 if (b) {
1135 b[i] = rect_uchar[2];
1136 }
1137 if (a) {
1138 a[i] = rect_uchar[3];
1139 }
1140 }
1142 break;
1143
1144 case 12: /* Up Sampling, a bit pointless but best write the bit depth requested. */
1145 PIXEL_LOOPER_BEGIN (rect_uchar) {
1146 r[i] = UPSAMPLE_8_TO_12(rect_uchar[0]);
1147 if (g) {
1148 g[i] = UPSAMPLE_8_TO_12(rect_uchar[1]);
1149 }
1150 if (b) {
1151 b[i] = UPSAMPLE_8_TO_12(rect_uchar[2]);
1152 }
1153 if (a) {
1154 a[i] = UPSAMPLE_8_TO_12(rect_uchar[3]);
1155 }
1156 }
1158 break;
1159
1160 case 16:
1161 PIXEL_LOOPER_BEGIN (rect_uchar) {
1162 r[i] = UPSAMPLE_8_TO_16(rect_uchar[0]);
1163 if (g) {
1164 g[i] = UPSAMPLE_8_TO_16(rect_uchar[1]);
1165 }
1166 if (b) {
1167 b[i] = UPSAMPLE_8_TO_16(rect_uchar[2]);
1168 }
1169 if (a) {
1170 a[i] = UPSAMPLE_8_TO_16(rect_uchar[3]);
1171 }
1172 }
1174 break;
1175 }
1176 }
1177
1178 /* Decide if MCT should be used */
1179 parameters->tcp_mct = image->numcomps == 3 ? 1 : 0;
1180
1181 if (parameters->cp_cinema) {
1182 cinema_setup_encoder(parameters, image, &img_fol);
1183 }
1184
1185 if (img_fol.rates) {
1186 MEM_delete(img_fol.rates);
1187 }
1188
1189 return image;
1190}
1191
1192bool imb_save_jp2_stream(ImBuf *ibuf, opj_stream_t *stream, ImBufFlags flags);
1193
1194bool imb_save_jp2(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
1195{
1196 opj_stream_t *stream = opj_stream_create_from_file(
1197 filepath, OPJ_J2K_STREAM_CHUNK_SIZE, false, nullptr);
1198 if (stream == nullptr) {
1199 return false;
1200 }
1201 const bool ok = imb_save_jp2_stream(ibuf, stream, flags);
1202 opj_stream_destroy(stream);
1203 return ok;
1204}
1205
1206/* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */
1207bool imb_save_jp2_stream(ImBuf *ibuf, opj_stream_t *stream, ImBufFlags /*flags*/)
1208{
1209 int quality = ibuf->foptions.quality;
1210
1211 opj_cparameters_t parameters; /* compression parameters */
1212 opj_image_t *image = nullptr;
1213
1214 /* set encoding parameters to default values */
1215 opj_set_default_encoder_parameters(&parameters);
1216
1217 /* compression ratio */
1218 /* invert range, from 10-100, 100-1
1219 * Where JPEG see's 1 and highest quality (lossless) and 100 is very low quality. */
1220 parameters.tcp_rates[0] = ((100 - quality) / 90.0f * 99.0f) + 1;
1221
1222 parameters.tcp_numlayers = 1; /* only one resolution */
1223 parameters.cp_disto_alloc = 1;
1224
1225 /* Enable tiling for better multi-threaded performance on large images.
1226 * Only use tiling for images >= 12 megapixels to avoid overhead on smaller images.
1227 * Note: Cinema profiles override this via cinema_parameters() which sets tile_size_on = 0. */
1228 constexpr int64_t megapixels_threshold = 12 * 1000 * 1000;
1229 const int64_t total_pixels = int64_t(ibuf->x) * int64_t(ibuf->y);
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;
1236 }
1237
1238 image = ibuftoimage(ibuf, &parameters);
1239
1240 opj_codec_t *codec = nullptr;
1241 bool ok = false;
1242 /* JP2 format output */
1243 {
1244 /* get a JP2 compressor handle */
1245 OPJ_CODEC_FORMAT format = OPJ_CODEC_JP2;
1246 if (ibuf->foptions.flag & JP2_J2K) {
1247 format = OPJ_CODEC_J2K;
1248 }
1249 else if (ibuf->foptions.flag & JP2_JP2) {
1250 format = OPJ_CODEC_JP2;
1251 }
1252
1253 codec = opj_create_compress(format);
1254
1255 /* configure the event callbacks (not required) */
1256 opj_set_error_handler(codec, error_callback, nullptr);
1257 opj_set_warning_handler(codec, warning_callback, nullptr);
1258#ifndef NDEBUG /* too noisy */
1259 opj_set_info_handler(codec, info_callback, nullptr);
1260#endif
1261
1262 /* setup the encoder parameters using the current image and using user parameters */
1263 if (opj_setup_encoder(codec, &parameters, image) == false) {
1264 goto finally;
1265 }
1266
1267 /* Enable multi-threaded encoding for faster compression. */
1268 if (!opj_codec_set_threads(codec, BLI_system_thread_count())) {
1269 CLOG_WARN(&LOG, "JP2 encoder: failed to set %d threads", BLI_system_thread_count());
1270 }
1271
1272 if (opj_start_compress(codec, image, stream) == false) {
1273 goto finally;
1274 }
1275 if (opj_encode(codec, stream) == false) {
1276 goto finally;
1277 }
1278 if (opj_end_compress(codec, stream) == false) {
1279 goto finally;
1280 }
1281 }
1282
1283 ok = true;
1284
1285finally:
1286 /* free remaining compression structures */
1287 if (codec) {
1288 opj_destroy_codec(codec);
1289 }
1290
1291 /* free image data */
1292 if (image) {
1293 opj_image_destroy(image);
1294 }
1295
1296 if (ok == false) {
1297 CLOG_ERROR(&LOG, "Failed to encode image");
1298 }
1299
1300 return ok;
1301}
1302
1303} // namespace blender
File and directory operations.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_STR_ERROR(clg_ref, str)
Definition CLG_log.h:196
#define CLOG_STR_WARN(clg_ref, str)
Definition CLG_log.h:197
#define CLOG_STR_INFO(clg_ref, str)
Definition CLG_log.h:198
Read Blender memory allocation and freeing.
#define BLI_INLINE
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
const T * data() const
Definition BLI_array.hh:323
void fill(const T &value) const
Definition BLI_array.hh:283
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
#define PIXEL_LOOPER_BEGIN_CHANNELS(_rect, _channels)
#define PIXEL_LOOPER_END
#define JP2_FILEHEADER_SIZE
Definition format_jp2.cc:33
#define COMP_24_CS
#define CINEMA_24_CS
#define CINEMA_48_CS
#define COMP_48_CS
#define PIXEL_LOOPER_BEGIN(_rect)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
const char * imb_file_extensions_jp2[]
Definition format_jp2.cc:29
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
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
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[]
Definition format_jp2.cc:35
static OPJ_CODEC_FORMAT format_from_header(const uchar mem[JP2_FILEHEADER_SIZE], const size_t size)
Definition format_jp2.cc:70
unsigned int uint
Definition BKE_text.h:55
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 *)
Definition format_jp2.cc:90
static void info_callback(const char *msg, void *)
int BLI_system_thread_count()
Definition threads.cc:258
unsigned char uchar
Definition BKE_text.h:58
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)
Definition format_jp2.cc:54
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 *)
Definition format_jp2.cc:97
static const char J2K_HEAD[]
Definition format_jp2.cc:37
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)
Definition format_jp2.cc:62
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)
Definition format_jp2.cc:82
bool imb_save_jp2_stream(ImBuf *ibuf, opj_stream_t *stream, ImBufFlags flags)
static CLG_LogRef LOG
Definition logging.cpp:12
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 uchar * buf
const uchar * cur
const ColorSpace * colorspace
ImbFormatOptions foptions
ImBufFloatBuffer float_buffer
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
bool colorspace_is_data() const
ImColorMode color_mode
eImbFileType ftype
float * float_data_for_write()
Definition allocimbuf.cc:79