Welcome to mirror list, hosted at ThFree Co, Russian Federation.

jp2.c « intern « imbuf « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 390f2502ee77f38aededd038e543ef5886d623fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Campbell Barton
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/imbuf/intern/jp2.c
 *  \ingroup imbuf
 */

#include "MEM_guardedalloc.h"

#include "BLI_math.h"
#include "BLI_fileops.h"

#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"
#include "IMB_filetype.h"

#include "IMB_colormanagement.h"
#include "IMB_colormanagement_intern.h"

#include "openjpeg.h"

// #define JP2_FILEHEADER_SIZE 14  /* UNUSED */

static const char JP2_HEAD[] = {0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A};
static const char J2K_HEAD[] = {0xFF, 0x4F, 0xFF, 0x51, 0x00};

/* We only need this because of how the presets are set */
/* this typedef is copied from 'openjpeg-1.5.0/applications/codec/image_to_j2k.c' */
typedef struct img_folder {
	/** The directory path of the folder containing input images*/
	char *imgdirpath;
	/** Output format*/
	char *out_format;
	/** Enable option*/
	char set_imgdir;
	/** Enable Cod Format for output*/
	char set_out_format;
	/** User specified rate stored in case of cinema option*/
	float *rates;
} img_fol_t;

enum {
    DCP_CINEMA2K = 3,
    DCP_CINEMA4K = 4,
};

static bool check_jp2(const unsigned char *mem) /* J2K_CFMT */
{
	return memcmp(JP2_HEAD, mem, sizeof(JP2_HEAD)) ? 0 : 1;
}

static bool check_j2k(const unsigned char *mem) /* J2K_CFMT */
{
	return memcmp(J2K_HEAD, mem, sizeof(J2K_HEAD)) ? 0 : 1;
}

int imb_is_a_jp2(const unsigned char *buf)
{
	return check_jp2(buf);
}

/**
 * sample error callback expecting a FILE* client object
 */
static void error_callback(const char *msg, void *client_data)
{
	FILE *stream = (FILE *)client_data;
	fprintf(stream, "[ERROR] %s", msg);
}
/**
 * sample warning callback expecting a FILE* client object
 */
static void warning_callback(const char *msg, void *client_data)
{
	FILE *stream = (FILE *)client_data;
	fprintf(stream, "[WARNING] %s", msg);
}
/**
 * sample debug callback expecting no client object
 */
static void info_callback(const char *msg, void *client_data)
{
	(void)client_data;
	fprintf(stdout, "[INFO] %s", msg);
}

#   define PIXEL_LOOPER_BEGIN(_rect)                                          \
	for (y = h - 1; y != (unsigned int)(-1); y--) {                           \
		for (i = y * w, i_next = (y + 1) * w;                                 \
		     i < i_next;                                                      \
		     i++, _rect += 4)                                                 \
		{                                                                     \

#   define PIXEL_LOOPER_BEGIN_CHANNELS(_rect, _channels)                      \
	for (y = h - 1; y != (unsigned int)(-1); y--) {                           \
		for (i = y * w, i_next = (y + 1) * w;                                 \
		     i < i_next;                                                      \
		     i++, _rect += _channels)                                         \
		{                                                                     \

#   define PIXEL_LOOPER_END \
	} \
	} (void)0 \

struct ImBuf *imb_jp2_decode(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
{
	struct ImBuf *ibuf = NULL;
	bool use_float = false; /* for precision higher then 8 use float */
	bool use_alpha = false;
	
	long signed_offsets[4] = {0, 0, 0, 0};
	int float_divs[4] = {1, 1, 1, 1};

	unsigned int i, i_next, w, h, planes;
	unsigned int y;
	int *r, *g, *b, *a; /* matching 'opj_image_comp.data' type */
	bool is_jp2, is_j2k;
	
	opj_dparameters_t parameters;   /* decompression parameters */
	
	opj_event_mgr_t event_mgr;      /* event manager */
	opj_image_t *image = NULL;

	opj_dinfo_t *dinfo = NULL;  /* handle to a decompressor */
	opj_cio_t *cio = NULL;

	is_jp2 = check_jp2(mem);
	is_j2k = check_j2k(mem);

	if (!is_jp2 && !is_j2k)
		return(NULL);

	/* both 8, 12 and 16 bit JP2Ks are default to standard byte colorspace */
	colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);

	/* configure the event callbacks (not required) */
	memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
	event_mgr.error_handler = error_callback;
	event_mgr.warning_handler = warning_callback;
	event_mgr.info_handler = info_callback;


	/* set decoding parameters to default values */
	opj_set_default_decoder_parameters(&parameters);


	/* JPEG 2000 compressed image data */

	/* get a decoder handle */
	dinfo = opj_create_decompress(is_jp2 ? CODEC_JP2 : CODEC_J2K);

	/* catch events using our callbacks and give a local context */
	opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);

	/* setup the decoder decoding parameters using the current image and user parameters */
	opj_setup_decoder(dinfo, &parameters);

	/* open a byte stream */
	/* note, we can't avoid removing 'const' cast here */
	cio = opj_cio_open((opj_common_ptr)dinfo, (unsigned char *)mem, size);

	/* decode the stream and fill the image structure */
	image = opj_decode(dinfo, cio);
	
	if (!image) {
		fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
		opj_destroy_decompress(dinfo);
		opj_cio_close(cio);
		return NULL;
	}

	/* close the byte stream */
	opj_cio_close(cio);


	if ((image->numcomps * image->x1 * image->y1) == 0) {
		fprintf(stderr, "\nError: invalid raw image parameters\n");
		return NULL;
	}
	
	w = image->comps[0].w;
	h = image->comps[0].h;
	
	switch (image->numcomps) {
		case 1: /* Grayscale */
		case 3: /* Color */
			planes = 24;
			use_alpha = false;
			break;
		default: /* 2 or 4 - Grayscale or Color + alpha */
			planes = 32; /* grayscale + alpha */
			use_alpha = true;
			break;
	}
	
	
	i = image->numcomps;
	if (i > 4) i = 4;
	
	while (i) {
		i--;
		
		if (image->comps[i].prec > 8)
			use_float = true;
		
		if (image->comps[i].sgnd)
			signed_offsets[i] =  1 << (image->comps[i].prec - 1);
		
		/* only needed for float images but dosnt hurt to calc this */
		float_divs[i] = (1 << image->comps[i].prec) - 1;
	}
	
	ibuf = IMB_allocImBuf(w, h, planes, use_float ? IB_rectfloat : IB_rect);
	
	if (ibuf == NULL) {
		if (dinfo)
			opj_destroy_decompress(dinfo);
		return NULL;
	}
	
	ibuf->ftype = IMB_FTYPE_JP2;
	if (is_jp2)
		ibuf->foptions.flag |= JP2_JP2;
	else
		ibuf->foptions.flag |= JP2_J2K;
	
	if (use_float) {
		float *rect_float = ibuf->rect_float;

		if (image->numcomps < 3) {
			r = image->comps[0].data;
			a = (use_alpha) ? image->comps[1].data : NULL;

			/* grayscale 12bits+ */
			if (use_alpha) {
				a = image->comps[1].data;
				PIXEL_LOOPER_BEGIN(rect_float) {
					rect_float[0] = rect_float[1] = rect_float[2] = (float)(r[i] + signed_offsets[0]) / float_divs[0];
					rect_float[3] = (a[i] + signed_offsets[1]) / float_divs[1];
				}
				PIXEL_LOOPER_END;
			}
			else {
				PIXEL_LOOPER_BEGIN(rect_float) {
					rect_float[0] = rect_float[1] = rect_float[2] = (float)(r[i] + signed_offsets[0]) / float_divs[0];
					rect_float[3] = 1.0f;
				}
				PIXEL_LOOPER_END;
			}
		}
		else {
			r = image->comps[0].data;
			g = image->comps[1].data;
			b = image->comps[2].data;

			/* rgb or rgba 12bits+ */
			if (use_alpha) {
				a = image->comps[3].data;
				PIXEL_LOOPER_BEGIN(rect_float) {
					rect_float[0] = (float)(r[i] + signed_offsets[0]) / float_divs[0];
					rect_float[1] = (float)(g[i] + signed_offsets[1]) / float_divs[1];
					rect_float[2] = (float)(b[i] + signed_offsets[2]) / float_divs[2];
					rect_float[3] = (float)(a[i] + signed_offsets[3]) / float_divs[3];
				}
				PIXEL_LOOPER_END;
			}
			else {
				PIXEL_LOOPER_BEGIN(rect_float) {
					rect_float[0] = (float)(r[i] + signed_offsets[0]) / float_divs[0];
					rect_float[1] = (float)(g[i] + signed_offsets[1]) / float_divs[1];
					rect_float[2] = (float)(b[i] + signed_offsets[2]) / float_divs[2];
					rect_float[3] = 1.0f;
				}
				PIXEL_LOOPER_END;
			}
		}
		
	}
	else {
		unsigned char *rect_uchar = (unsigned char *)ibuf->rect;

		if (image->numcomps < 3) {
			r = image->comps[0].data;
			a = (use_alpha) ? image->comps[1].data : NULL;

			/* grayscale */
			if (use_alpha) {
				a = image->comps[3].data;
				PIXEL_LOOPER_BEGIN(rect_uchar) {
					rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
					rect_uchar[3] = a[i] + signed_offsets[1];
				}
				PIXEL_LOOPER_END;
			}
			else {
				PIXEL_LOOPER_BEGIN(rect_uchar) {
					rect_uchar[0] = rect_uchar[1] = rect_uchar[2] = (r[i] + signed_offsets[0]);
					rect_uchar[3] = 255;
				}
				PIXEL_LOOPER_END;
			}
		}
		else {
			r = image->comps[0].data;
			g = image->comps[1].data;
			b = image->comps[2].data;

			/* 8bit rgb or rgba */
			if (use_alpha) {
				a = image->comps[3].data;
				PIXEL_LOOPER_BEGIN(rect_uchar) {
					rect_uchar[0] = r[i] + signed_offsets[0];
					rect_uchar[1] = g[i] + signed_offsets[1];
					rect_uchar[2] = b[i] + signed_offsets[2];
					rect_uchar[3] = a[i] + signed_offsets[3];
				}
				PIXEL_LOOPER_END;
			}
			else {
				PIXEL_LOOPER_BEGIN(rect_uchar) {
					rect_uchar[0] = r[i] + signed_offsets[0];
					rect_uchar[1] = g[i] + signed_offsets[1];
					rect_uchar[2] = b[i] + signed_offsets[2];
					rect_uchar[3] = 255;
				}
				PIXEL_LOOPER_END;
			}
		}
	}
	
	/* free remaining structures */
	if (dinfo) {
		opj_destroy_decompress(dinfo);
	}
	
	/* free image data structure */
	opj_image_destroy(image);
	
	if (flags & IB_rect) {
		IMB_rect_from_float(ibuf);
	}
	
	return(ibuf);
}

//static opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp)
/* prec can be 8, 12, 16 */

/* use inline because the float passed can be a function call that would end up being called many times */
#if 0
#define UPSAMPLE_8_TO_12(_val) ((_val << 4) | (_val & ((1 << 4) - 1)))
#define UPSAMPLE_8_TO_16(_val) ((_val << 8) + _val)

#define DOWNSAMPLE_FLOAT_TO_8BIT(_val)  (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : (int)(255.0f * (_val)))
#define DOWNSAMPLE_FLOAT_TO_12BIT(_val) (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : (int)(4095.0f * (_val)))
#define DOWNSAMPLE_FLOAT_TO_16BIT(_val) (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : (int)(65535.0f * (_val)))
#else

BLI_INLINE int UPSAMPLE_8_TO_12(const unsigned char _val)
{
	return (_val << 4) | (_val & ((1 << 4) - 1));
}
BLI_INLINE int UPSAMPLE_8_TO_16(const unsigned char _val)
{
	return (_val << 8) + _val;
}

BLI_INLINE int DOWNSAMPLE_FLOAT_TO_8BIT(const float _val)
{
	return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 255 : (int)(255.0f * (_val)));
}
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_12BIT(const float _val)
{
	return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 4095 : (int)(4095.0f * (_val)));
}
BLI_INLINE int DOWNSAMPLE_FLOAT_TO_16BIT(const float _val)
{
	return (_val) <= 0.0f ? 0 : ((_val) >= 1.0f ? 65535 : (int)(65535.0f * (_val)));
}
#endif

/*
 * 2048x1080 (2K) at 24 fps or 48 fps, or 4096x2160 (4K) at 24 fps; 3x12 bits per pixel, XYZ color space
 *
 * - In 2K, for Scope (2.39:1) presentation 2048x858  pixels of the image is used
 * - In 2K, for Flat  (1.85:1) presentation 1998x1080 pixels of the image is used
 */

/* ****************************** COPIED FROM image_to_j2k.c */

/* ----------------------------------------------------------------------- */
#define CINEMA_24_CS 1302083    /*Codestream length for 24fps*/
#define CINEMA_48_CS 651041     /*Codestream length for 48fps*/
#define COMP_24_CS 1041666      /*Maximum size per color component for 2K & 4K @ 24fps*/
#define COMP_48_CS 520833       /*Maximum size per color component for 2K @ 48fps*/


static int initialise_4K_poc(opj_poc_t *POC, int numres)
{
	POC[0].tile  = 1; 
	POC[0].resno0  = 0; 
	POC[0].compno0 = 0;
	POC[0].layno1  = 1;
	POC[0].resno1  = numres - 1;
	POC[0].compno1 = 3;
	POC[0].prg1 = CPRL;
	POC[1].tile  = 1;
	POC[1].resno0  = numres - 1;
	POC[1].compno0 = 0;
	POC[1].layno1  = 1;
	POC[1].resno1  = numres;
	POC[1].compno1 = 3;
	POC[1].prg1 = CPRL;
	return 2;
}

static void cinema_parameters(opj_cparameters_t *parameters)
{
	parameters->tile_size_on = 0; /* false */
	parameters->cp_tdx = 1;
	parameters->cp_tdy = 1;
	
	/*Tile part*/
	parameters->tp_flag = 'C';
	parameters->tp_on = 1;

	/*Tile and Image shall be at (0, 0)*/
	parameters->cp_tx0 = 0;
	parameters->cp_ty0 = 0;
	parameters->image_offset_x0 = 0;
	parameters->image_offset_y0 = 0;

	/*Codeblock size = 32 * 32*/
	parameters->cblockw_init = 32;
	parameters->cblockh_init = 32;
	parameters->csty |= 0x01;

	/*The progression order shall be CPRL*/
	parameters->prog_order = CPRL;

	/* No ROI */
	parameters->roi_compno = -1;

	parameters->subsampling_dx = 1;     parameters->subsampling_dy = 1;

	/* 9-7 transform */
	parameters->irreversible = 1;
}

static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *image, img_fol_t *img_fol)
{
	int i;
	float temp_rate;

	switch (parameters->cp_cinema) {
		case CINEMA2K_24:
		case CINEMA2K_48:
			if (parameters->numresolution > 6) {
				parameters->numresolution = 6;
			}
			if (!((image->comps[0].w == 2048) || (image->comps[0].h == 1080))) {
				fprintf(stdout, "Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
				        "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
				        image->comps[0].w, image->comps[0].h);
				parameters->cp_rsiz = STD_RSIZ;
			}
			else {
				parameters->cp_rsiz = DCP_CINEMA2K;
			}
			break;
	
		case CINEMA4K_24:
			if (parameters->numresolution < 1) {
				parameters->numresolution = 1;
			}
			else if (parameters->numresolution > 7) {
				parameters->numresolution = 7;
			}
			if (!((image->comps[0].w == 4096) || (image->comps[0].h == 2160))) {
				fprintf(stdout, "Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
				        "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
				        image->comps[0].w, image->comps[0].h);
				parameters->cp_rsiz = STD_RSIZ;
			}
			else {
				parameters->cp_rsiz = DCP_CINEMA2K;
			}
			parameters->numpocs = initialise_4K_poc(parameters->POC, parameters->numresolution);
			break;
		case OFF:
			/* do nothing */
			break;
	}

	switch (parameters->cp_cinema) {
		case CINEMA2K_24:
		case CINEMA4K_24:
			for (i = 0; i < parameters->tcp_numlayers; i++) {
				temp_rate = 0;
				if (img_fol->rates[i] == 0) {
					parameters->tcp_rates[0] = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)) /
					                           (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
				}
				else {
					temp_rate = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)) /
					            (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
					if (temp_rate > CINEMA_24_CS) {
						parameters->tcp_rates[i] = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)) /
						                           (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
					}
					else {
						parameters->tcp_rates[i] = img_fol->rates[i];
					}
				}
			}
			parameters->max_comp_size = COMP_24_CS;
			break;
		
		case CINEMA2K_48:
			for (i = 0; i < parameters->tcp_numlayers; i++) {
				temp_rate = 0;
				if (img_fol->rates[i] == 0) {
					parameters->tcp_rates[0] = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)) /
					                           (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
				}
				else {
					temp_rate = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)) /
					            (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
					if (temp_rate > CINEMA_48_CS) {
						parameters->tcp_rates[0] = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)) /
						                           (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
					}
					else {
						parameters->tcp_rates[i] = img_fol->rates[i];
					}
				}
			}
			parameters->max_comp_size = COMP_48_CS;
			break;
		case OFF:
			/* do nothing */
			break;
	}
	parameters->cp_disto_alloc = 1;
}

static float channel_colormanage_noop(float value)
{
	return value;
}

static opj_image_t *ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters)
{
	unsigned char *rect_uchar;
	float *rect_float, from_straight[4];
	
	unsigned int subsampling_dx = parameters->subsampling_dx;
	unsigned int subsampling_dy = parameters->subsampling_dy;
	
	unsigned int i, i_next, numcomps, w, h, prec;
	unsigned int y;
	int *r, *g, *b, *a; /* matching 'opj_image_comp.data' type */
	OPJ_COLOR_SPACE color_space;
	opj_image_cmptparm_t cmptparm[4];   /* maximum of 4 components */
	opj_image_t *image = NULL;
	
	float (*chanel_colormanage_cb)(float);
	
	img_fol_t img_fol; /* only needed for cinema presets */
	memset(&img_fol, 0, sizeof(img_fol_t));
	
	if (ibuf->float_colorspace) {
		/* float buffer was managed already, no need in color space conversion */
		chanel_colormanage_cb = channel_colormanage_noop;
	}
	else {
		/* standard linear-to-srgb conversion if float buffer wasn't managed */
		chanel_colormanage_cb = linearrgb_to_srgb;
	}
	
	if (ibuf->foptions.flag & JP2_CINE) {
		
		if (ibuf->x == 4096 || ibuf->y == 2160)
			parameters->cp_cinema = CINEMA4K_24;
		else {
			if (ibuf->foptions.flag & JP2_CINE_48FPS) {
				parameters->cp_cinema = CINEMA2K_48;
			}
			else {
				parameters->cp_cinema = CINEMA2K_24;
			}
		}
		if (parameters->cp_cinema) {
			img_fol.rates = (float *)MEM_mallocN(parameters->tcp_numlayers * sizeof(float), "jp2_rates");
			for (i = 0; i < parameters->tcp_numlayers; i++) {
				img_fol.rates[i] = parameters->tcp_rates[i];
			}
			cinema_parameters(parameters);
		}
		
		color_space = (ibuf->foptions.flag & JP2_YCC) ? CLRSPC_SYCC : CLRSPC_SRGB;
		prec = 12;
		numcomps = 3;
	}
	else {
		/* Get settings from the imbuf */
		color_space = (ibuf->foptions.flag & JP2_YCC) ? CLRSPC_SYCC : CLRSPC_SRGB;
		
		if (ibuf->foptions.flag & JP2_16BIT) prec = 16;
		else if (ibuf->foptions.flag & JP2_12BIT) prec = 12;
		else prec = 8;
		
		/* 32bit images == alpha channel */
		/* grayscale not supported yet */
		numcomps = (ibuf->planes == 32) ? 4 : 3;
	}
	
	w = ibuf->x;
	h = ibuf->y;
	
	
	/* initialize image components */
	memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
	for (i = 0; i < numcomps; i++) {
		cmptparm[i].prec = prec;
		cmptparm[i].bpp = prec;
		cmptparm[i].sgnd = 0;
		cmptparm[i].dx = subsampling_dx;
		cmptparm[i].dy = subsampling_dy;
		cmptparm[i].w = w;
		cmptparm[i].h = h;
	}
	/* create the image */
	image = opj_image_create(numcomps, &cmptparm[0], color_space);
	if (!image) {
		printf("Error: opj_image_create() failed\n");
		return NULL;
	}

	/* set image offset and reference grid */
	image->x0 = parameters->image_offset_x0;
	image->y0 = parameters->image_offset_y0;
	image->x1 = image->x0 + (w - 1) * subsampling_dx + 1 + image->x0;
	image->y1 = image->y0 + (h - 1) * subsampling_dy + 1 + image->y0;

	/* set image data */
	rect_uchar = (unsigned char *) ibuf->rect;
	rect_float = ibuf->rect_float;
	
	/* set the destination channels */
	r = image->comps[0].data;
	g = image->comps[1].data;
	b = image->comps[2].data;
	a = (numcomps == 4) ? image->comps[3].data : NULL;

	if (rect_float && rect_uchar && prec == 8) {
		/* No need to use the floating point buffer, just write the 8 bits from the char buffer */
		rect_float = NULL;
	}
	
	if (rect_float) {
		int channels_in_float = ibuf->channels ? ibuf->channels : 4;

		switch (prec) {
			case 8: /* Convert blenders float color channels to 8, 12 or 16bit ints */
				if (numcomps == 4) {
					if (channels_in_float == 4) {
						PIXEL_LOOPER_BEGIN(rect_float)
						{
							premul_to_straight_v4_v4(from_straight, rect_float);
							r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[2]));
							a[i] = DOWNSAMPLE_FLOAT_TO_8BIT(from_straight[3]);
						}
						PIXEL_LOOPER_END;
					}
					else if (channels_in_float == 3) {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 3)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[2]));
							a[i] = 255;
						}
						PIXEL_LOOPER_END;
					}
					else {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 1)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = b[i] = r[i];
							a[i] = 255;
						}
						PIXEL_LOOPER_END;
					}
				}
				else {
					if (channels_in_float == 4) {
						PIXEL_LOOPER_BEGIN(rect_float)
						{
							premul_to_straight_v4_v4(from_straight, rect_float);
							r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(from_straight[2]));
						}
						PIXEL_LOOPER_END;
					}
					else if (channels_in_float == 3) {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 3)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[2]));
						}
						PIXEL_LOOPER_END;
					}
					else {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 1)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_8BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = b[i] = r[i];
						}
						PIXEL_LOOPER_END;
					}
				}
				break;
			
			case 12:
				if (numcomps == 4) {
					if (channels_in_float == 4) {
						PIXEL_LOOPER_BEGIN(rect_float)
						{
							premul_to_straight_v4_v4(from_straight, rect_float);
							r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[2]));
							a[i] = DOWNSAMPLE_FLOAT_TO_12BIT(from_straight[3]);
						}
						PIXEL_LOOPER_END;
					}
					else if (channels_in_float == 3) {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 3)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[2]));
							a[i] = 4095;
						}
						PIXEL_LOOPER_END;
					}
					else {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 1)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = b[i] = r[i];
							a[i] = 4095;
						}
						PIXEL_LOOPER_END;
					}
				}
				else {
					if (channels_in_float == 4) {
						PIXEL_LOOPER_BEGIN(rect_float)
						{
							premul_to_straight_v4_v4(from_straight, rect_float);
							r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(from_straight[2]));
						}
						PIXEL_LOOPER_END;
					}
					else if (channels_in_float == 3) {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 3)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[2]));
						}
						PIXEL_LOOPER_END;
					}
					else {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 1)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_12BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = b[i] = r[i];
						}
						PIXEL_LOOPER_END;
					}
				}
				break;

			case 16:
				if (numcomps == 4) {
					if (channels_in_float == 4) {
						PIXEL_LOOPER_BEGIN(rect_float)
						{
							premul_to_straight_v4_v4(from_straight, rect_float);
							r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[2]));
							a[i] = DOWNSAMPLE_FLOAT_TO_16BIT(from_straight[3]);
						}
						PIXEL_LOOPER_END;
					}
					else if (channels_in_float == 3) {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 3)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[2]));
							a[i] = 65535;
						}
						PIXEL_LOOPER_END;
					}
					else {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 1)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = b[i] = r[i];
							a[i] = 65535;
						}
						PIXEL_LOOPER_END;
					}
				}
				else {
					if (channels_in_float == 4) {
						PIXEL_LOOPER_BEGIN(rect_float)
						{
							premul_to_straight_v4_v4(from_straight, rect_float);
							r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(from_straight[2]));
						}
						PIXEL_LOOPER_END;
					}
					else if (channels_in_float == 3) {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 3)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[1]));
							b[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[2]));
						}
						PIXEL_LOOPER_END;
					}
					else {
						PIXEL_LOOPER_BEGIN_CHANNELS(rect_float, 1)
						{
							r[i] = DOWNSAMPLE_FLOAT_TO_16BIT(chanel_colormanage_cb(rect_float[0]));
							g[i] = b[i] = r[i];
						}
						PIXEL_LOOPER_END;
					}
				}
				break;
		}
	}
	else {
		/* just use rect*/
		switch (prec) {
			case 8:
				if (numcomps == 4) {
					PIXEL_LOOPER_BEGIN(rect_uchar)
					{
						r[i] = rect_uchar[0];
						g[i] = rect_uchar[1];
						b[i] = rect_uchar[2];
						a[i] = rect_uchar[3];
					}
					PIXEL_LOOPER_END;
				}
				else {
					PIXEL_LOOPER_BEGIN(rect_uchar)
					{
						r[i] = rect_uchar[0];
						g[i] = rect_uchar[1];
						b[i] = rect_uchar[2];
					}
					PIXEL_LOOPER_END;
				}
				break;
			
			case 12: /* Up Sampling, a bit pointless but best write the bit depth requested */
				if (numcomps == 4) {
					PIXEL_LOOPER_BEGIN(rect_uchar)
					{
						r[i] = UPSAMPLE_8_TO_12(rect_uchar[0]);
						g[i] = UPSAMPLE_8_TO_12(rect_uchar[1]);
						b[i] = UPSAMPLE_8_TO_12(rect_uchar[2]);
						a[i] = UPSAMPLE_8_TO_12(rect_uchar[3]);
					}
					PIXEL_LOOPER_END;
				}
				else {
					PIXEL_LOOPER_BEGIN(rect_uchar)
					{
						r[i] = UPSAMPLE_8_TO_12(rect_uchar[0]);
						g[i] = UPSAMPLE_8_TO_12(rect_uchar[1]);
						b[i] = UPSAMPLE_8_TO_12(rect_uchar[2]);
					}
					PIXEL_LOOPER_END;
				}
				break;

			case 16:
				if (numcomps == 4) {
					PIXEL_LOOPER_BEGIN(rect_uchar)
					{
						r[i] = UPSAMPLE_8_TO_16(rect_uchar[0]);
						g[i] = UPSAMPLE_8_TO_16(rect_uchar[1]);
						b[i] = UPSAMPLE_8_TO_16(rect_uchar[2]);
						a[i] = UPSAMPLE_8_TO_16(rect_uchar[3]);
					}
					PIXEL_LOOPER_END;
				}
				else {
					PIXEL_LOOPER_BEGIN(rect_uchar)
					{
						r[i] = UPSAMPLE_8_TO_16(rect_uchar[0]);
						g[i] = UPSAMPLE_8_TO_16(rect_uchar[1]);
						b[i] = UPSAMPLE_8_TO_16(rect_uchar[2]);
					}
					PIXEL_LOOPER_END;
				}
				break;
		}
	}
	
	/* Decide if MCT should be used */
	parameters->tcp_mct = image->numcomps == 3 ? 1 : 0;
	
	if (parameters->cp_cinema) {
		cinema_setup_encoder(parameters, image, &img_fol);
	}
	
	if (img_fol.rates)
		MEM_freeN(img_fol.rates);
	
	return image;
}


/* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */
int imb_savejp2(struct ImBuf *ibuf, const char *name, int flags)
{
	int quality = ibuf->foptions.quality;
	
	int bSuccess;
	opj_cparameters_t parameters;   /* compression parameters */
	opj_event_mgr_t event_mgr;      /* event manager */
	opj_image_t *image = NULL;
	
	(void)flags; /* unused */
	
	/*
	 * configure the event callbacks (not required)
	 * setting of each callback is optional
	 */
	memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
	event_mgr.error_handler = error_callback;
	event_mgr.warning_handler = warning_callback;
	event_mgr.info_handler = info_callback;
	
	/* set encoding parameters to default values */
	opj_set_default_encoder_parameters(&parameters);
	
	/* compression ratio */
	/* invert range, from 10-100, 100-1
	 * where jpeg see's 1 and highest quality (lossless) and 100 is very low quality*/
	parameters.tcp_rates[0] = ((100 - quality) / 90.0f * 99.0f) + 1;

	
	parameters.tcp_numlayers = 1; /* only one resolution */
	parameters.cp_disto_alloc = 1;

	image = ibuftoimage(ibuf, &parameters);
	
	
	{   /* JP2 format output */
		int codestream_length;
		opj_cio_t *cio = NULL;
		FILE *f = NULL;
		opj_cinfo_t *cinfo = NULL;

		/* get a JP2 compressor handle */
		if (ibuf->foptions.flag & JP2_JP2)
			cinfo = opj_create_compress(CODEC_JP2);
		else if (ibuf->foptions.flag & JP2_J2K)
			cinfo = opj_create_compress(CODEC_J2K);
		else
			BLI_assert(!"Unsupported codec was specified in save settings");

		/* catch events using our callbacks and give a local context */
		opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);

		/* setup the encoder parameters using the current image and using user parameters */
		opj_setup_encoder(cinfo, &parameters, image);

		/* open a byte stream for writing */
		/* allocate memory for all tiles */
		cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);

		/* encode the image */
		bSuccess = opj_encode(cinfo, cio, image, NULL); /* last arg used to be parameters.index but this deprecated */
		
		if (!bSuccess) {
			opj_cio_close(cio);
			fprintf(stderr, "failed to encode image\n");
			return 0;
		}
		codestream_length = cio_tell(cio);

		/* write the buffer to disk */
		f = BLI_fopen(name, "wb");
		
		if (!f) {
			fprintf(stderr, "failed to open %s for writing\n", name);
			return 1;
		}
		fwrite(cio->buffer, 1, codestream_length, f);
		fclose(f);
		fprintf(stderr, "Generated outfile %s\n", name);
		/* close and free the byte stream */
		opj_cio_close(cio);
		
		/* free remaining compression structures */
		opj_destroy_compress(cinfo);
	}

	/* free image data */
	opj_image_destroy(image);
	
	return 1;
}