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