Blender V4.3
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
9#include "MEM_guardedalloc.h"
10
11#include "BLI_fileops.h"
12
14#include "IMB_filetype.hh"
15#include "IMB_imbuf.hh"
16#include "IMB_imbuf_types.hh"
17
18#include "openjpeg.h"
19
20#include <cstring>
21
22#define JP2_FILEHEADER_SIZE 12
23
24static const char JP2_HEAD[] = {
25 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A};
26static const char J2K_HEAD[] = {0xFF, 0x4F, 0xFF, 0x51, 0x00};
27
28/* We only need this because of how the presets are set */
29/* this typedef is copied from 'openjpeg-1.5.0/applications/codec/image_to_j2k.c' */
30struct img_fol_t {
40 float *rates;
41};
42
43static bool check_jp2(const uchar *mem, const size_t size) /* J2K_CFMT */
44{
45 if (size < sizeof(JP2_HEAD)) {
46 return false;
47 }
48 return memcmp(JP2_HEAD, mem, sizeof(JP2_HEAD)) ? false : true;
49}
50
51static bool check_j2k(const uchar *mem, const size_t size) /* J2K_CFMT */
52{
53 if (size < sizeof(J2K_HEAD)) {
54 return false;
55 }
56 return memcmp(J2K_HEAD, mem, sizeof(J2K_HEAD)) ? false : true;
57}
58
59static OPJ_CODEC_FORMAT format_from_header(const uchar mem[JP2_FILEHEADER_SIZE], const size_t size)
60{
61 if (check_jp2(mem, size)) {
62 return OPJ_CODEC_JP2;
63 }
64 if (check_j2k(mem, size)) {
65 return OPJ_CODEC_J2K;
66 }
67
68 return OPJ_CODEC_UNKNOWN;
69}
70
71bool imb_is_a_jp2(const uchar *buf, size_t size)
72{
73 return (check_jp2(buf, size) || check_j2k(buf, size));
74}
75
79static void error_callback(const char *msg, void *client_data)
80{
81 FILE *stream = (FILE *)client_data;
82 fprintf(stream, "[ERROR] %s", msg);
83}
87static void warning_callback(const char *msg, void *client_data)
88{
89 FILE *stream = (FILE *)client_data;
90 fprintf(stream, "[WARNING] %s", msg);
91}
92
93#ifndef NDEBUG
97static void info_callback(const char *msg, void *client_data)
98{
99 FILE *stream = (FILE *)client_data;
100 fprintf(stream, "[INFO] %s", msg);
101}
102#endif
103
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) {
107
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) {
111
112#define PIXEL_LOOPER_END \
113 } \
114 } \
115 (void)0
116
117/* -------------------------------------------------------------------- */
121struct BufInfo {
122 const uchar *buf;
123 const uchar *cur;
124 OPJ_OFF_T len;
125};
126
127static void opj_read_from_buffer_free(void * /*p_user_data*/)
128{
129 /* NOP. */
130}
131
132static OPJ_SIZE_T opj_read_from_buffer(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
133{
134 BufInfo *p_file = static_cast<BufInfo *>(p_user_data);
135 OPJ_UINT32 l_nb_read;
136
137 if (p_file->cur + p_nb_bytes < p_file->buf + p_file->len) {
138 l_nb_read = p_nb_bytes;
139 }
140 else {
141 l_nb_read = (OPJ_UINT32)(p_file->buf + p_file->len - p_file->cur);
142 }
143 memcpy(p_buffer, p_file->cur, l_nb_read);
144 p_file->cur += l_nb_read;
145
146 return l_nb_read ? l_nb_read : ((OPJ_SIZE_T)-1);
147}
148
149#if 0
150static OPJ_SIZE_T opj_write_from_buffer(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
151{
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;
156 return p_nb_bytes;
157}
158#endif
159
160static OPJ_OFF_T opj_skip_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
161{
162 BufInfo *p_file = static_cast<BufInfo *>(p_user_data);
163 if (p_file->cur + p_nb_bytes < p_file->buf + p_file->len) {
164 p_file->cur += p_nb_bytes;
165 return p_nb_bytes;
166 }
167 p_file->cur = p_file->buf + p_file->len;
168 return (OPJ_OFF_T)-1;
169}
170
171static OPJ_BOOL opj_seek_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
172{
173 BufInfo *p_file = static_cast<BufInfo *>(p_user_data);
174 if (p_nb_bytes < p_file->len) {
175 p_file->cur = p_file->buf + p_nb_bytes;
176 return OPJ_TRUE;
177 }
178 p_file->cur = p_file->buf + p_file->len;
179 return OPJ_FALSE;
180}
181
186static opj_stream_t *opj_stream_create_from_buffer(BufInfo *p_file,
187 OPJ_UINT32 p_size,
188 OPJ_BOOL p_is_read_stream)
189{
190 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
191 if (l_stream == nullptr) {
192 return nullptr;
193 }
194 opj_stream_set_user_data(l_stream, p_file, opj_read_from_buffer_free);
195 opj_stream_set_user_data_length(l_stream, p_file->len);
196 opj_stream_set_read_function(l_stream, opj_read_from_buffer);
197#if 0 /* UNUSED */
198 opj_stream_set_write_function(l_stream, opj_write_from_buffer);
199#endif
200 opj_stream_set_skip_function(l_stream, opj_skip_from_buffer);
201 opj_stream_set_seek_function(l_stream, opj_seek_from_buffer);
202
203 return l_stream;
204}
205
208/* -------------------------------------------------------------------- */
212static void opj_free_from_file(void *p_user_data)
213{
214 FILE *f = static_cast<FILE *>(p_user_data);
215 fclose(f);
216}
217
218static OPJ_UINT64 opj_get_data_length_from_file(void *p_user_data)
219{
220 FILE *p_file = static_cast<FILE *>(p_user_data);
221 OPJ_OFF_T file_length = 0;
222
223 fseek(p_file, 0, SEEK_END);
224 file_length = ftell(p_file);
225 fseek(p_file, 0, SEEK_SET);
226
227 return (OPJ_UINT64)file_length;
228}
229
230static OPJ_SIZE_T opj_read_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
231{
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;
235}
236
237static OPJ_SIZE_T opj_write_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
238{
239 FILE *p_file = static_cast<FILE *>(p_user_data);
240 return fwrite(p_buffer, 1, p_nb_bytes, p_file);
241}
242
243static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
244{
245 FILE *p_file = static_cast<FILE *>(p_user_data);
246 if (fseek(p_file, p_nb_bytes, SEEK_CUR)) {
247 return -1;
248 }
249 return p_nb_bytes;
250}
251
252static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
253{
254 FILE *p_file = static_cast<FILE *>(p_user_data);
255 if (fseek(p_file, p_nb_bytes, SEEK_SET)) {
256 return OPJ_FALSE;
257 }
258 return OPJ_TRUE;
259}
260
266static opj_stream_t *opj_stream_create_from_file(const char *filepath,
267 OPJ_UINT32 p_size,
268 OPJ_BOOL p_is_read_stream,
269 FILE **r_file)
270{
271 FILE *p_file = BLI_fopen(filepath, p_is_read_stream ? "rb" : "wb");
272 if (p_file == nullptr) {
273 return nullptr;
274 }
275
276 opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream);
277 if (l_stream == nullptr) {
278 fclose(p_file);
279 return nullptr;
280 }
281
282 opj_stream_set_user_data(l_stream, p_file, opj_free_from_file);
283 opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
284 opj_stream_set_write_function(l_stream, opj_write_from_file);
285 opj_stream_set_read_function(l_stream, opj_read_from_file);
286 opj_stream_set_skip_function(l_stream, opj_skip_from_file);
287 opj_stream_set_seek_function(l_stream, opj_seek_from_file);
288
289 if (r_file) {
290 *r_file = p_file;
291 }
292 return l_stream;
293}
294
297static ImBuf *imb_load_jp2_stream(opj_stream_t *stream,
298 OPJ_CODEC_FORMAT p_format,
299 int flags,
300 char colorspace[IM_MAX_SPACE]);
301
302ImBuf *imb_load_jp2(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
303{
304 const OPJ_CODEC_FORMAT format = (size > JP2_FILEHEADER_SIZE) ? format_from_header(mem, size) :
305 OPJ_CODEC_UNKNOWN;
306 BufInfo buf_wrapper = {};
307 buf_wrapper.buf = mem;
308 buf_wrapper.cur = mem;
309 buf_wrapper.len = OPJ_OFF_T(size);
310 opj_stream_t *stream = opj_stream_create_from_buffer(
311 &buf_wrapper, OPJ_J2K_STREAM_CHUNK_SIZE, true);
312 ImBuf *ibuf = imb_load_jp2_stream(stream, format, flags, colorspace);
313 opj_stream_destroy(stream);
314 return ibuf;
315}
316
317ImBuf *imb_load_jp2_filepath(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
318{
319 FILE *p_file = nullptr;
321 opj_stream_t *stream = opj_stream_create_from_file(
322 filepath, OPJ_J2K_STREAM_CHUNK_SIZE, true, &p_file);
323 if (stream) {
324 return nullptr;
325 }
326
327 if (fread(mem, sizeof(mem), 1, p_file) != sizeof(mem)) {
328 opj_stream_destroy(stream);
329 return nullptr;
330 }
331
332 fseek(p_file, 0, SEEK_SET);
333
334 const OPJ_CODEC_FORMAT format = format_from_header(mem, sizeof(mem));
335 ImBuf *ibuf = imb_load_jp2_stream(stream, format, flags, colorspace);
336 opj_stream_destroy(stream);
337 return ibuf;
338}
339
340static ImBuf *imb_load_jp2_stream(opj_stream_t *stream,
341 const OPJ_CODEC_FORMAT format,
342 int flags,
343 char colorspace[IM_MAX_SPACE])
344{
345 if (format == OPJ_CODEC_UNKNOWN) {
346 return nullptr;
347 }
348
349 ImBuf *ibuf = nullptr;
350 bool use_float = false; /* for precision higher than 8 use float */
351 bool use_alpha = false;
352
353 long signed_offsets[4] = {0, 0, 0, 0};
354 int float_divs[4] = {1, 1, 1, 1};
355
356 uint i, i_next, w, h, planes;
357 uint y;
358 const int *r, *g, *b, *a; /* matching 'opj_image_comp.data' type */
359
360 opj_dparameters_t parameters; /* decompression parameters */
361
362 opj_image_t *image = nullptr;
363 opj_codec_t *codec = nullptr; /* handle to a decompressor */
364
365 /* both 8, 12 and 16 bit JP2Ks are default to standard byte colorspace */
367
368 /* set decoding parameters to default values */
369 opj_set_default_decoder_parameters(&parameters);
370
371 /* JPEG 2000 compressed image data */
372
373 /* get a decoder handle */
374 codec = opj_create_decompress(format);
375
376 /* configure the event callbacks (not required) */
377 opj_set_error_handler(codec, error_callback, stderr);
378 opj_set_warning_handler(codec, warning_callback, stderr);
379#ifndef NDEBUG /* too noisy */
380 opj_set_info_handler(codec, info_callback, stderr);
381#endif
382
383 /* setup the decoder decoding parameters using the current image and user parameters */
384 if (opj_setup_decoder(codec, &parameters) == false) {
385 goto finally;
386 }
387
388 if (opj_read_header(stream, codec, &image) == false) {
389 printf("OpenJPEG error: failed to read the header\n");
390 goto finally;
391 }
392
393 /* decode the stream and fill the image structure */
394 if (opj_decode(codec, stream, image) == false) {
395 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
396 goto finally;
397 }
398
399 if ((image->numcomps * image->x1 * image->y1) == 0) {
400 fprintf(stderr, "\nError: invalid raw image parameters\n");
401 goto finally;
402 }
403
404 w = image->comps[0].w;
405 h = image->comps[0].h;
406
407 switch (image->numcomps) {
408 case 1: /* Gray-scale. */
409 case 3: /* Color. */
410 planes = 24;
411 use_alpha = false;
412 break;
413 default: /* 2 or 4 - Gray-scale or Color + alpha. */
414 planes = 32; /* Gray-scale + alpha. */
415 use_alpha = true;
416 break;
417 }
418
419 i = image->numcomps;
420 if (i > 4) {
421 i = 4;
422 }
423
424 while (i) {
425 i--;
426
427 if (image->comps[i].prec > 8) {
428 use_float = true;
429 }
430
431 if (image->comps[i].sgnd) {
432 signed_offsets[i] = long(1) << (image->comps[i].prec - 1);
433 }
434
435 /* only needed for float images but doesn't hurt to calc this */
436 float_divs[i] = (1 << image->comps[i].prec) - 1;
437 }
438
439 ibuf = IMB_allocImBuf(w, h, planes, use_float ? IB_rectfloat : IB_rect);
440
441 if (ibuf == nullptr) {
442 goto finally;
443 }
444
445 ibuf->ftype = IMB_FTYPE_JP2;
446 if (true /*is_jp2*/) {
447 ibuf->foptions.flag |= JP2_JP2;
448 }
449 else {
450 ibuf->foptions.flag |= JP2_J2K;
451 }
452
453 if (use_float) {
454 float *rect_float = ibuf->float_buffer.data;
455
456 if (image->numcomps < 3) {
457 r = image->comps[0].data;
458
459 /* Gray-scale 12bits+ */
460 if (use_alpha) {
461 a = image->comps[1].data;
462 PIXEL_LOOPER_BEGIN (rect_float) {
463 rect_float[0] = rect_float[1] = rect_float[2] = float(r[i] + signed_offsets[0]) /
464 float_divs[0];
465 rect_float[3] = (a[i] + signed_offsets[1]) / float_divs[1];
466 }
468 }
469 else {
470 PIXEL_LOOPER_BEGIN (rect_float) {
471 rect_float[0] = rect_float[1] = rect_float[2] = float(r[i] + signed_offsets[0]) /
472 float_divs[0];
473 rect_float[3] = 1.0f;
474 }
476 }
477 }
478 else {
479 r = image->comps[0].data;
480 g = image->comps[1].data;
481 b = image->comps[2].data;
482
483 /* RGB or RGBA 12bits+ */
484 if (use_alpha) {
485 a = image->comps[3].data;
486 PIXEL_LOOPER_BEGIN (rect_float) {
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];
491 }
493 }
494 else {
495 PIXEL_LOOPER_BEGIN (rect_float) {
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;
500 }
502 }
503 }
504 }
505 else {
506 uchar *rect_uchar = ibuf->byte_buffer.data;
507
508 if (image->numcomps < 3) {
509 r = image->comps[0].data;
510
511 /* Gray-scale. */
512 if (use_alpha) {
513 a = image->comps[3].data;
514 PIXEL_LOOPER_BEGIN (rect_uchar) {
515 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
516 rect_uchar[3] = a[i] + signed_offsets[1];
517 }
519 }
520 else {
521 PIXEL_LOOPER_BEGIN (rect_uchar) {
522 rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
523 rect_uchar[3] = 255;
524 }
526 }
527 }
528 else {
529 r = image->comps[0].data;
530 g = image->comps[1].data;
531 b = image->comps[2].data;
532
533 /* 8bit RGB or RGBA */
534 if (use_alpha) {
535 a = image->comps[3].data;
536 PIXEL_LOOPER_BEGIN (rect_uchar) {
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];
541 }
543 }
544 else {
545 PIXEL_LOOPER_BEGIN (rect_uchar) {
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];
549 rect_uchar[3] = 255;
550 }
552 }
553 }
554 }
555
556 if (flags & IB_rect) {
558 }
559
560finally:
561
562 /* free remaining structures */
563 if (codec) {
564 opj_destroy_codec(codec);
565 }
566
567 if (image) {
568 opj_image_destroy(image);
569 }
570
571 return ibuf;
572}
573
574#if 0
575static opj_image_t *rawtoimage(const char *filename,
576 opj_cparameters_t *parameters,
577 raw_cparameters_t *raw_cp)
578#endif
579/* prec can be 8, 12, 16 */
580
581/* Use inline because the float passed can be a function call
582 * that would end up being called many times. */
583#if 0
584# define UPSAMPLE_8_TO_12(_val) ((_val << 4) | (_val & ((1 << 4) - 1)))
585# define UPSAMPLE_8_TO_16(_val) ((_val << 8) + _val)
586
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)))
593#else
594
596{
597 return (_val << 4) | (_val & ((1 << 4) - 1));
598}
600{
601 return (_val << 8) + _val;
602}
603
605{
606 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : int(255.0f * (_val)));
607}
609{
610 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : int(4095.0f * (_val)));
611}
613{
614 return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : int(65535.0f * (_val)));
615}
616#endif
617
618/*
619 * 2048x1080 (2K) at 24 fps or 48 fps, or 4096x2160 (4K) at 24 fps;
620 * 3x12 bits per pixel, XYZ color space
621 *
622 * - In 2K, for Scope (2.39:1) presentation 2048x858 pixels of the image is used
623 * - In 2K, for Flat (1.85:1) presentation 1998x1080 pixels of the image is used
624 */
625
626/* ****************************** COPIED FROM image_to_j2k.c */
627
628/* ----------------------------------------------------------------------- */
629#define CINEMA_24_CS 1302083 /* Code-stream length for 24fps. */
630#define CINEMA_48_CS 651041 /* Code-stream length for 48fps. */
631#define COMP_24_CS 1041666 /* Maximum size per color component for 2K & 4K @ 24fps. */
632#define COMP_48_CS 520833 /* Maximum size per color component for 2K @ 48fps. */
633
634static int init_4K_poc(opj_poc_t *POC, int numres)
635{
636 POC[0].tile = 1;
637 POC[0].resno0 = 0;
638 POC[0].compno0 = 0;
639 POC[0].layno1 = 1;
640 POC[0].resno1 = numres - 1;
641 POC[0].compno1 = 3;
642 POC[0].prg1 = OPJ_CPRL;
643 POC[1].tile = 1;
644 POC[1].resno0 = numres - 1;
645 POC[1].compno0 = 0;
646 POC[1].layno1 = 1;
647 POC[1].resno1 = numres;
648 POC[1].compno1 = 3;
649 POC[1].prg1 = OPJ_CPRL;
650 return 2;
651}
652
653static void cinema_parameters(opj_cparameters_t *parameters)
654{
655 parameters->tile_size_on = 0; /* false */
656 parameters->cp_tdx = 1;
657 parameters->cp_tdy = 1;
658
659 /* Tile part. */
660 parameters->tp_flag = 'C';
661 parameters->tp_on = 1;
662
663 /* Tile and Image shall be at (0, 0). */
664 parameters->cp_tx0 = 0;
665 parameters->cp_ty0 = 0;
666 parameters->image_offset_x0 = 0;
667 parameters->image_offset_y0 = 0;
668
669 /* Code-block size = 32 * 32. */
670 parameters->cblockw_init = 32;
671 parameters->cblockh_init = 32;
672 parameters->csty |= 0x01;
673
674 /* The progression order shall be CPRL. */
675 parameters->prog_order = OPJ_CPRL;
676
677 /* No ROI */
678 parameters->roi_compno = -1;
679
680 parameters->subsampling_dx = 1;
681 parameters->subsampling_dy = 1;
682
683 /* 9-7 transform */
684 parameters->irreversible = 1;
685}
686
687static void cinema_setup_encoder(opj_cparameters_t *parameters,
688 opj_image_t *image,
689 img_fol_t *img_fol)
690{
691 int i;
692 float temp_rate;
693
694 switch (parameters->cp_cinema) {
695 case OPJ_CINEMA2K_24:
696 case OPJ_CINEMA2K_48:
697 if (parameters->numresolution > 6) {
698 parameters->numresolution = 6;
699 }
700 if (!((image->comps[0].w == 2048) || (image->comps[0].h == 1080))) {
701 fprintf(stdout,
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 "
704 "1080\n",
705 image->comps[0].w,
706 image->comps[0].h);
707 parameters->cp_rsiz = OPJ_STD_RSIZ;
708 }
709 else {
710 parameters->cp_rsiz = OPJ_CINEMA2K;
711 }
712 break;
713
714 case OPJ_CINEMA4K_24:
715 if (parameters->numresolution < 1) {
716 parameters->numresolution = 1;
717 }
718 else if (parameters->numresolution > 7) {
719 parameters->numresolution = 7;
720 }
721 if (!((image->comps[0].w == 4096) || (image->comps[0].h == 2160))) {
722 fprintf(stdout,
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 "
725 "2160\n",
726 image->comps[0].w,
727 image->comps[0].h);
728 parameters->cp_rsiz = OPJ_STD_RSIZ;
729 }
730 else {
731 parameters->cp_rsiz = OPJ_CINEMA4K;
732 }
733 parameters->numpocs = init_4K_poc(parameters->POC, parameters->numresolution);
734 break;
735 case OPJ_OFF:
736 /* do nothing */
737 break;
738 }
739
740 switch (parameters->cp_cinema) {
741 case OPJ_CINEMA2K_24:
742 case OPJ_CINEMA4K_24:
743 for (i = 0; i < parameters->tcp_numlayers; i++) {
744 temp_rate = 0;
745 if (img_fol->rates[i] == 0) {
746 parameters->tcp_rates[0] = float(image->numcomps * image->comps[0].w *
747 image->comps[0].h * image->comps[0].prec) /
748 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
749 }
750 else {
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);
754 if (temp_rate > CINEMA_24_CS) {
755 parameters->tcp_rates[i] = float(image->numcomps * image->comps[0].w *
756 image->comps[0].h * image->comps[0].prec) /
757 (CINEMA_24_CS * 8 * image->comps[0].dx *
758 image->comps[0].dy);
759 }
760 else {
761 parameters->tcp_rates[i] = img_fol->rates[i];
762 }
763 }
764 }
765 parameters->max_comp_size = COMP_24_CS;
766 break;
767
768 case OPJ_CINEMA2K_48:
769 for (i = 0; i < parameters->tcp_numlayers; i++) {
770 temp_rate = 0;
771 if (img_fol->rates[i] == 0) {
772 parameters->tcp_rates[0] = float(image->numcomps * image->comps[0].w *
773 image->comps[0].h * image->comps[0].prec) /
774 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
775 }
776 else {
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);
780 if (temp_rate > CINEMA_48_CS) {
781 parameters->tcp_rates[0] = float(image->numcomps * image->comps[0].w *
782 image->comps[0].h * image->comps[0].prec) /
783 (CINEMA_48_CS * 8 * image->comps[0].dx *
784 image->comps[0].dy);
785 }
786 else {
787 parameters->tcp_rates[i] = img_fol->rates[i];
788 }
789 }
790 }
791 parameters->max_comp_size = COMP_48_CS;
792 break;
793 case OPJ_OFF:
794 /* do nothing */
795 break;
796 }
797 parameters->cp_disto_alloc = 1;
798}
799
800static float channel_colormanage_noop(float value)
801{
802 return value;
803}
804
805static opj_image_t *ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters)
806{
807 uchar *rect_uchar;
808 float *rect_float, from_straight[4];
809
810 uint subsampling_dx = parameters->subsampling_dx;
811 uint subsampling_dy = parameters->subsampling_dy;
812
813 uint i, i_next, numcomps, w, h, prec;
814 uint y;
815 int *r, *g, *b, *a; /* matching 'opj_image_comp.data' type */
816 OPJ_COLOR_SPACE color_space;
817 opj_image_cmptparm_t cmptparm[4]; /* maximum of 4 components */
818 opj_image_t *image = nullptr;
819
820 float (*chanel_colormanage_cb)(float);
821
822 img_fol_t img_fol; /* only needed for cinema presets */
823 memset(&img_fol, 0, sizeof(img_fol_t));
824
826 /* float buffer was managed already, no need in color space conversion */
827 chanel_colormanage_cb = channel_colormanage_noop;
828 }
829 else {
830 /* standard linear-to-SRGB conversion if float buffer wasn't managed */
831 chanel_colormanage_cb = linearrgb_to_srgb;
832 }
833
834 if (ibuf->foptions.flag & JP2_CINE) {
835
836 if (ibuf->x == 4096 || ibuf->y == 2160) {
837 parameters->cp_cinema = OPJ_CINEMA4K_24;
838 }
839 else {
840 if (ibuf->foptions.flag & JP2_CINE_48FPS) {
841 parameters->cp_cinema = OPJ_CINEMA2K_48;
842 }
843 else {
844 parameters->cp_cinema = OPJ_CINEMA2K_24;
845 }
846 }
847 if (parameters->cp_cinema) {
848 img_fol.rates = (float *)MEM_mallocN(parameters->tcp_numlayers * sizeof(float), "jp2_rates");
849 for (i = 0; i < parameters->tcp_numlayers; i++) {
850 img_fol.rates[i] = parameters->tcp_rates[i];
851 }
853 }
854
855 color_space = (ibuf->foptions.flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
856 prec = 12;
857 numcomps = 3;
858 }
859 else {
860 /* Get settings from the imbuf */
861 color_space = (ibuf->foptions.flag & JP2_YCC) ? OPJ_CLRSPC_SYCC : OPJ_CLRSPC_SRGB;
862
863 if (ibuf->foptions.flag & JP2_16BIT) {
864 prec = 16;
865 }
866 else if (ibuf->foptions.flag & JP2_12BIT) {
867 prec = 12;
868 }
869 else {
870 prec = 8;
871 }
872
873 /* 32bit images == alpha channel. */
874 /* Gray-scale not supported yet. */
875 numcomps = (ibuf->planes == 32) ? 4 : 3;
876 }
877
878 w = ibuf->x;
879 h = ibuf->y;
880
881 /* initialize image components */
882 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t[4]));
883 for (i = 0; i < numcomps; i++) {
884 cmptparm[i].prec = prec;
885 /* Deprecated in openjpeg 2.5. */
886#if (OPJ_VERSION_MAJOR < 2) || (OPJ_VERSION_MAJOR == 2 && OPJ_VERSION_MINOR < 5)
887 cmptparm[i].bpp = prec;
888#endif
889 cmptparm[i].sgnd = 0;
890 cmptparm[i].dx = subsampling_dx;
891 cmptparm[i].dy = subsampling_dy;
892 cmptparm[i].w = w;
893 cmptparm[i].h = h;
894 }
895 /* create the image */
896 image = opj_image_create(numcomps, &cmptparm[0], color_space);
897 if (!image) {
898 printf("Error: opj_image_create() failed\n");
899 return nullptr;
900 }
901
902 /* set image offset and reference grid */
903 image->x0 = parameters->image_offset_x0;
904 image->y0 = parameters->image_offset_y0;
905 image->x1 = image->x0 + (w - 1) * subsampling_dx + 1 + image->x0;
906 image->y1 = image->y0 + (h - 1) * subsampling_dy + 1 + image->y0;
907
908 /* set image data */
909 rect_uchar = ibuf->byte_buffer.data;
910 rect_float = ibuf->float_buffer.data;
911
912 /* set the destination channels */
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;
917
918 if (rect_float && rect_uchar && prec == 8) {
919 /* No need to use the floating point buffer, just write the 8 bits from the char buffer */
920 rect_float = nullptr;
921 }
922
923 if (rect_float) {
924 int channels_in_float = ibuf->channels ? ibuf->channels : 4;
925
926 switch (prec) {
927 case 8: /* Convert blenders float color channels to 8, 12 or 16bit ints */
928 if (numcomps == 4) {
929 if (channels_in_float == 4) {
930 PIXEL_LOOPER_BEGIN (rect_float) {
931 premul_to_straight_v4_v4(from_straight, rect_float);
932 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[0]));
933 g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[1]));
934 b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[2]));
935 a[i] = DOWNSAMPLE_FLOAT_TO_8BIT(from_straight[3]);
936 }
938 }
939 else if (channels_in_float == 3) {
940 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
941 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
942 g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[1]));
943 b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[2]));
944 a[i] = 255;
945 }
947 }
948 else {
949 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
950 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
951 g[i] = b[i] = r[i];
952 a[i] = 255;
953 }
955 }
956 }
957 else {
958 if (channels_in_float == 4) {
959 PIXEL_LOOPER_BEGIN (rect_float) {
960 premul_to_straight_v4_v4(from_straight, rect_float);
961 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[0]));
962 g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[1]));
963 b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[2]));
964 }
966 }
967 else if (channels_in_float == 3) {
968 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
969 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
970 g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[1]));
971 b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[2]));
972 }
974 }
975 else {
976 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
977 r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
978 g[i] = b[i] = r[i];
979 }
981 }
982 }
983 break;
984
985 case 12:
986 if (numcomps == 4) {
987 if (channels_in_float == 4) {
988 PIXEL_LOOPER_BEGIN (rect_float) {
989 premul_to_straight_v4_v4(from_straight, rect_float);
990 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[0]));
991 g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[1]));
992 b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[2]));
993 a[i] = DOWNSAMPLE_FLOAT_TO_12BIT(from_straight[3]);
994 }
996 }
997 else if (channels_in_float == 3) {
998 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
999 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
1000 g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[1]));
1001 b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[2]));
1002 a[i] = 4095;
1003 }
1005 }
1006 else {
1007 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
1008 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
1009 g[i] = b[i] = r[i];
1010 a[i] = 4095;
1011 }
1013 }
1014 }
1015 else {
1016 if (channels_in_float == 4) {
1017 PIXEL_LOOPER_BEGIN (rect_float) {
1018 premul_to_straight_v4_v4(from_straight, rect_float);
1019 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[0]));
1020 g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[1]));
1021 b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[2]));
1022 }
1024 }
1025 else if (channels_in_float == 3) {
1026 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
1027 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
1028 g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[1]));
1029 b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[2]));
1030 }
1032 }
1033 else {
1034 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
1035 r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
1036 g[i] = b[i] = r[i];
1037 }
1039 }
1040 }
1041 break;
1042
1043 case 16:
1044 if (numcomps == 4) {
1045 if (channels_in_float == 4) {
1046 PIXEL_LOOPER_BEGIN (rect_float) {
1047 premul_to_straight_v4_v4(from_straight, rect_float);
1048 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[0]));
1049 g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[1]));
1050 b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[2]));
1051 a[i] = DOWNSAMPLE_FLOAT_TO_16BIT(from_straight[3]);
1052 }
1054 }
1055 else if (channels_in_float == 3) {
1056 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
1057 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
1058 g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[1]));
1059 b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[2]));
1060 a[i] = 65535;
1061 }
1063 }
1064 else {
1065 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
1066 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
1067 g[i] = b[i] = r[i];
1068 a[i] = 65535;
1069 }
1071 }
1072 }
1073 else {
1074 if (channels_in_float == 4) {
1075 PIXEL_LOOPER_BEGIN (rect_float) {
1076 premul_to_straight_v4_v4(from_straight, rect_float);
1077 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[0]));
1078 g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[1]));
1079 b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[2]));
1080 }
1082 }
1083 else if (channels_in_float == 3) {
1084 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 3) {
1085 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
1086 g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[1]));
1087 b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[2]));
1088 }
1090 }
1091 else {
1092 PIXEL_LOOPER_BEGIN_CHANNELS (rect_float, 1) {
1093 r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
1094 g[i] = b[i] = r[i];
1095 }
1097 }
1098 }
1099 break;
1100 }
1101 }
1102 else {
1103 /* Just use rect. */
1104 switch (prec) {
1105 case 8:
1106 if (numcomps == 4) {
1107 PIXEL_LOOPER_BEGIN (rect_uchar) {
1108 r[i] = rect_uchar[0];
1109 g[i] = rect_uchar[1];
1110 b[i] = rect_uchar[2];
1111 a[i] = rect_uchar[3];
1112 }
1114 }
1115 else {
1116 PIXEL_LOOPER_BEGIN (rect_uchar) {
1117 r[i] = rect_uchar[0];
1118 g[i] = rect_uchar[1];
1119 b[i] = rect_uchar[2];
1120 }
1122 }
1123 break;
1124
1125 case 12: /* Up Sampling, a bit pointless but best write the bit depth requested */
1126 if (numcomps == 4) {
1127 PIXEL_LOOPER_BEGIN (rect_uchar) {
1128 r[i] = UPSAMPLE_8_TO_12(rect_uchar[0]);
1129 g[i] = UPSAMPLE_8_TO_12(rect_uchar[1]);
1130 b[i] = UPSAMPLE_8_TO_12(rect_uchar[2]);
1131 a[i] = UPSAMPLE_8_TO_12(rect_uchar[3]);
1132 }
1134 }
1135 else {
1136 PIXEL_LOOPER_BEGIN (rect_uchar) {
1137 r[i] = UPSAMPLE_8_TO_12(rect_uchar[0]);
1138 g[i] = UPSAMPLE_8_TO_12(rect_uchar[1]);
1139 b[i] = UPSAMPLE_8_TO_12(rect_uchar[2]);
1140 }
1142 }
1143 break;
1144
1145 case 16:
1146 if (numcomps == 4) {
1147 PIXEL_LOOPER_BEGIN (rect_uchar) {
1148 r[i] = UPSAMPLE_8_TO_16(rect_uchar[0]);
1149 g[i] = UPSAMPLE_8_TO_16(rect_uchar[1]);
1150 b[i] = UPSAMPLE_8_TO_16(rect_uchar[2]);
1151 a[i] = UPSAMPLE_8_TO_16(rect_uchar[3]);
1152 }
1154 }
1155 else {
1156 PIXEL_LOOPER_BEGIN (rect_uchar) {
1157 r[i] = UPSAMPLE_8_TO_16(rect_uchar[0]);
1158 g[i] = UPSAMPLE_8_TO_16(rect_uchar[1]);
1159 b[i] = UPSAMPLE_8_TO_16(rect_uchar[2]);
1160 }
1162 }
1163 break;
1164 }
1165 }
1166
1167 /* Decide if MCT should be used */
1168 parameters->tcp_mct = image->numcomps == 3 ? 1 : 0;
1169
1170 if (parameters->cp_cinema) {
1171 cinema_setup_encoder(parameters, image, &img_fol);
1172 }
1173
1174 if (img_fol.rates) {
1175 MEM_freeN(img_fol.rates);
1176 }
1177
1178 return image;
1179}
1180
1181bool imb_save_jp2_stream(ImBuf *ibuf, opj_stream_t *stream, int flags);
1182
1183bool imb_save_jp2(ImBuf *ibuf, const char *filepath, int flags)
1184{
1185 opj_stream_t *stream = opj_stream_create_from_file(
1186 filepath, OPJ_J2K_STREAM_CHUNK_SIZE, false, nullptr);
1187 if (stream == nullptr) {
1188 return false;
1189 }
1190 const bool ok = imb_save_jp2_stream(ibuf, stream, flags);
1191 opj_stream_destroy(stream);
1192 return ok;
1193}
1194
1195/* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */
1196bool imb_save_jp2_stream(ImBuf *ibuf, opj_stream_t *stream, int /*flags*/)
1197{
1198 int quality = ibuf->foptions.quality;
1199
1200 opj_cparameters_t parameters; /* compression parameters */
1201 opj_image_t *image = nullptr;
1202
1203 /* set encoding parameters to default values */
1204 opj_set_default_encoder_parameters(&parameters);
1205
1206 /* compression ratio */
1207 /* invert range, from 10-100, 100-1
1208 * Where JPEG see's 1 and highest quality (lossless) and 100 is very low quality. */
1209 parameters.tcp_rates[0] = ((100 - quality) / 90.0f * 99.0f) + 1;
1210
1211 parameters.tcp_numlayers = 1; /* only one resolution */
1212 parameters.cp_disto_alloc = 1;
1213
1214 image = ibuftoimage(ibuf, &parameters);
1215
1216 opj_codec_t *codec = nullptr;
1217 bool ok = false;
1218 /* JP2 format output */
1219 {
1220 /* get a JP2 compressor handle */
1221 OPJ_CODEC_FORMAT format = OPJ_CODEC_JP2;
1222 if (ibuf->foptions.flag & JP2_J2K) {
1223 format = OPJ_CODEC_J2K;
1224 }
1225 else if (ibuf->foptions.flag & JP2_JP2) {
1226 format = OPJ_CODEC_JP2;
1227 }
1228
1229 codec = opj_create_compress(format);
1230
1231 /* configure the event callbacks (not required) */
1232 opj_set_error_handler(codec, error_callback, stderr);
1233 opj_set_warning_handler(codec, warning_callback, stderr);
1234#ifndef NDEBUG /* too noisy */
1235 opj_set_info_handler(codec, info_callback, stderr);
1236#endif
1237
1238 /* setup the encoder parameters using the current image and using user parameters */
1239 if (opj_setup_encoder(codec, &parameters, image) == false) {
1240 goto finally;
1241 }
1242
1243 if (opj_start_compress(codec, image, stream) == false) {
1244 goto finally;
1245 }
1246 if (opj_encode(codec, stream) == false) {
1247 goto finally;
1248 }
1249 if (opj_end_compress(codec, stream) == false) {
1250 goto finally;
1251 }
1252 }
1253
1254 ok = true;
1255
1256finally:
1257 /* free remaining compression structures */
1258 if (codec) {
1259 opj_destroy_codec(codec);
1260 }
1261
1262 /* free image data */
1263 if (image) {
1264 opj_image_destroy(image);
1265 }
1266
1267 if (ok == false) {
1268 fprintf(stderr, "failed to encode image\n");
1269 }
1270
1271 return ok;
1272}
#define BLI_INLINE
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)
unsigned char uchar
unsigned int uint
@ COLOR_ROLE_DEFAULT_BYTE
void IMB_rect_from_float(ImBuf *ibuf)
Definition divers.cc:694
#define IM_MAX_SPACE
Definition IMB_imbuf.hh:49
Contains defines and structs used throughout the imbuf module.
@ IMB_COLORMANAGE_IS_DATA
@ IB_rectfloat
@ IB_rect
Read Guarded memory(de)allocation.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
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
#define printf
int len
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)
Definition jp2.cc:108
static OPJ_SIZE_T opj_read_from_buffer(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
Definition jp2.cc:132
static bool check_j2k(const uchar *mem, const size_t size)
Definition jp2.cc:51
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)
Definition jp2.cc:266
BLI_INLINE int UPSAMPLE_8_TO_12(const uchar _val)
Definition jp2.cc:595
ImBuf * imb_load_jp2(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
Definition jp2.cc:302
static void cinema_parameters(opj_cparameters_t *parameters)
Definition jp2.cc:653
static OPJ_SIZE_T opj_write_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
Definition jp2.cc:237
static opj_image_t * ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters)
Definition jp2.cc:805
static OPJ_CODEC_FORMAT format_from_header(const uchar mem[JP2_FILEHEADER_SIZE], const size_t size)
Definition jp2.cc:59
#define PIXEL_LOOPER_END
Definition jp2.cc:112
static OPJ_BOOL opj_seek_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
Definition jp2.cc:171
static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
Definition jp2.cc:252
static const char J2K_HEAD[]
Definition jp2.cc:26
static opj_stream_t * opj_stream_create_from_buffer(BufInfo *p_file, OPJ_UINT32 p_size, OPJ_BOOL p_is_read_stream)
Definition jp2.cc:186
#define JP2_FILEHEADER_SIZE
Definition jp2.cc:22
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_16BIT(const float _val)
Definition jp2.cc:612
#define COMP_24_CS
Definition jp2.cc:631
static OPJ_SIZE_T opj_read_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
Definition jp2.cc:230
static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *image, img_fol_t *img_fol)
Definition jp2.cc:687
static void warning_callback(const char *msg, void *client_data)
Definition jp2.cc:87
bool imb_is_a_jp2(const uchar *buf, size_t size)
Definition jp2.cc:71
static float channel_colormanage_noop(float value)
Definition jp2.cc:800
static OPJ_UINT64 opj_get_data_length_from_file(void *p_user_data)
Definition jp2.cc:218
BLI_INLINE int UPSAMPLE_8_TO_16(const uchar _val)
Definition jp2.cc:599
static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, void *p_user_data)
Definition jp2.cc:243
bool imb_save_jp2_stream(ImBuf *ibuf, opj_stream_t *stream, int flags)
Definition jp2.cc:1196
static OPJ_OFF_T opj_skip_from_buffer(OPJ_OFF_T p_nb_bytes, void *p_user_data)
Definition jp2.cc:160
bool imb_save_jp2(ImBuf *ibuf, const char *filepath, int flags)
Definition jp2.cc:1183
static int init_4K_poc(opj_poc_t *POC, int numres)
Definition jp2.cc:634
#define CINEMA_24_CS
Definition jp2.cc:629
static void opj_read_from_buffer_free(void *)
Definition jp2.cc:127
static ImBuf * imb_load_jp2_stream(opj_stream_t *stream, OPJ_CODEC_FORMAT p_format, int flags, char colorspace[IM_MAX_SPACE])
Definition jp2.cc:340
static bool check_jp2(const uchar *mem, const size_t size)
Definition jp2.cc:43
#define CINEMA_48_CS
Definition jp2.cc:630
#define COMP_48_CS
Definition jp2.cc:632
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_8BIT(const float _val)
Definition jp2.cc:604
static void opj_free_from_file(void *p_user_data)
Definition jp2.cc:212
#define PIXEL_LOOPER_BEGIN(_rect)
Definition jp2.cc:104
static const char JP2_HEAD[]
Definition jp2.cc:24
ImBuf * imb_load_jp2_filepath(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
Definition jp2.cc:317
static void info_callback(const char *msg, void *client_data)
Definition jp2.cc:97
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_12BIT(const float _val)
Definition jp2.cc:608
double parameters[NUM_PARAMETERS]
format
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
static void(* error_callback)(const char *)
const uchar * cur
Definition jp2.cc:123
OPJ_OFF_T len
Definition jp2.cc:124
const uchar * buf
Definition jp2.cc:122
ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImbFormatOptions foptions
ImBufByteBuffer byte_buffer
int colormanage_flag
unsigned char planes
enum eImbFileType ftype
char * out_format
Definition jp2.cc:34
char set_imgdir
Definition jp2.cc:36
char * imgdirpath
Definition jp2.cc:32
char set_out_format
Definition jp2.cc:38
float * rates
Definition jp2.cc:40