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