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