2 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2014, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux
7 * Copyright (c) 2003-2014, Antonin Descampe
8 * Copyright (c) 2005, Herve Drolon, FreeImage Team
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2007, Patrick Piscaglia (Telemis)
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
42 #include "opj_getopt.h"
46 #include "org_openJpeg_OpenJPEGJavaEncoder.h"
49 #define stricmp strcasecmp
50 #define strnicmp strncasecmp
53 #include "format_defs.h"
55 #define CINEMA_24_CS 1302083 /*Codestream length for 24fps*/
56 #define CINEMA_48_CS 651041 /*Codestream length for 48fps*/
57 #define COMP_24_CS 1041666 /*Maximum size per color component for 2K & 4K @ 24fps*/
58 #define COMP_48_CS 520833 /*Maximum size per color component for 2K @ 48fps*/
60 extern int get_file_format(char *filename);
61 extern void error_callback(const char *msg, void *client_data);
62 extern void warning_callback(const char *msg, void *client_data);
63 extern void info_callback(const char *msg, void *client_data);
65 typedef struct callback_variables {
67 /** 'jclass' object used to call a Java method from the C */
69 /** 'jclass' object used to call a Java method from the C */
70 jmethodID message_mid;
72 } callback_variables_t;
74 typedef struct dircnt {
75 /** Buffer for holding images read from Directory*/
77 /** Pointer to the buffer*/
81 typedef struct img_folder {
82 /** The directory path of the folder containing input images*/
88 /** Enable Cod Format for output*/
90 /** User specified rate stored in case of cinema option*/
94 static void encode_help_display()
96 fprintf(stdout, "HELP\n----\n\n");
97 fprintf(stdout, "- the -h option displays this help information on screen\n\n");
100 fprintf(stdout, "List of parameters for the JPEG 2000 "
103 #endif /* USE_JPWL */
106 fprintf(stdout, "\n");
107 fprintf(stdout, "REMARKS:\n");
108 fprintf(stdout, "---------\n");
109 fprintf(stdout, "\n");
111 "The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
112 fprintf(stdout, "COD and QCD never appear in the tile_header.\n");
113 fprintf(stdout, "\n");
115 "- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
117 "color image. You need enough disk space memory (twice the original) to encode \n");
119 "the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
120 fprintf(stdout, "\n");
121 fprintf(stdout, "By default:\n");
122 fprintf(stdout, "------------\n");
123 fprintf(stdout, "\n");
124 fprintf(stdout, " * Lossless\n");
125 fprintf(stdout, " * 1 tile\n");
126 fprintf(stdout, " * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
127 fprintf(stdout, " * Size of code-block : 64 x 64\n");
128 fprintf(stdout, " * Number of resolutions: 6\n");
129 fprintf(stdout, " * No SOP marker in the codestream\n");
130 fprintf(stdout, " * No EPH marker in the codestream\n");
131 fprintf(stdout, " * No sub-sampling in x or y direction\n");
132 fprintf(stdout, " * No mode switch activated\n");
133 fprintf(stdout, " * Progression order: LRCP\n");
134 fprintf(stdout, " * No index file\n");
135 fprintf(stdout, " * No ROI upshifted\n");
136 fprintf(stdout, " * No offset of the origin of the image\n");
137 fprintf(stdout, " * No offset of the origin of the tiles\n");
138 fprintf(stdout, " * Reversible DWT 5-3\n");
141 fprintf(stdout, " * No JPWL protection\n");
142 #endif /* USE_JPWL */
144 fprintf(stdout, "\n");
145 fprintf(stdout, "Parameters:\n");
146 fprintf(stdout, "------------\n");
147 fprintf(stdout, "\n");
148 fprintf(stdout, "Required Parameters (except with -h):\n");
149 fprintf(stdout, "One of the two options -ImgDir or -i must be used\n");
150 fprintf(stdout, "\n");
152 "-ImgDir : Image file Directory path (example ../Images) \n");
153 fprintf(stdout, " When using this option -OutFor must be used\n");
154 fprintf(stdout, "\n");
155 fprintf(stdout, "-OutFor \n");
156 fprintf(stdout, " REQUIRED only if -ImgDir is used\n");
157 fprintf(stdout, " Need to specify only format without filename <BMP> \n");
159 " Currently accepts PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA formats\n");
160 fprintf(stdout, "\n");
162 "-i : source file (-i source.pnm also *.pgm, *.ppm, *.bmp, *.tif, *.raw, *.tga) \n");
163 fprintf(stdout, " When using this option -o must be used\n");
164 fprintf(stdout, "\n");
165 fprintf(stdout, "-o : destination file (-o dest.j2k or .jp2) \n");
166 fprintf(stdout, "\n");
167 fprintf(stdout, "Optional Parameters:\n");
168 fprintf(stdout, "\n");
169 fprintf(stdout, "-h : display the help information \n ");
170 fprintf(stdout, "\n");
172 "-cinema2K : Digital Cinema 2K profile compliant codestream for 2K resolution.(-cinema2k 24 or 48) \n");
174 " Need to specify the frames per second for a 2K resolution. Only 24 or 48 fps is allowed\n");
175 fprintf(stdout, "\n");
177 "-cinema4K : Digital Cinema 4K profile compliant codestream for 4K resolution \n");
178 fprintf(stdout, " Frames per second not required. Default value is 24fps\n");
179 fprintf(stdout, "\n");
181 "-r : different compression ratios for successive layers (-r 20,10,5)\n ");
183 " - The rate specified for each quality level is the desired \n");
184 fprintf(stdout, " compression factor.\n");
185 fprintf(stdout, " Example: -r 20,10,1 means quality 1: compress 20x, \n");
187 " quality 2: compress 10x and quality 3: compress lossless\n");
188 fprintf(stdout, "\n");
190 " (options -r and -q cannot be used together)\n ");
191 fprintf(stdout, "\n");
194 "-q : different psnr for successive layers (-q 30,40,50) \n ");
197 " (options -r and -q cannot be used together)\n ");
199 fprintf(stdout, "\n");
200 fprintf(stdout, "-n : number of resolutions (-n 3) \n");
201 fprintf(stdout, "\n");
202 fprintf(stdout, "-b : size of code block (-b 32,32) \n");
203 fprintf(stdout, "\n");
204 fprintf(stdout, "-c : size of precinct (-c 128,128) \n");
205 fprintf(stdout, "\n");
206 fprintf(stdout, "-t : size of tile (-t 512,512) \n");
207 fprintf(stdout, "\n");
209 "-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
210 fprintf(stdout, "\n");
211 fprintf(stdout, "-s : subsampling factor (-s 2,2) [-s X,Y] \n");
212 fprintf(stdout, " Remark: subsampling bigger than 2 can produce error\n");
213 fprintf(stdout, "\n");
215 "-POC : Progression order change (-POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL) \n");
216 fprintf(stdout, " Example: T1=0,0,1,5,3,CPRL \n");
218 " : Ttilenumber=Resolution num start,Component num start,Layer num end,Resolution num end,Component num end,Progression order\n");
219 fprintf(stdout, "\n");
220 fprintf(stdout, "-SOP : write SOP marker before each packet \n");
221 fprintf(stdout, "\n");
222 fprintf(stdout, "-EPH : write EPH marker after each header packet \n");
223 fprintf(stdout, "\n");
225 "-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
227 " 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
229 " Indicate multiple modes by adding their values. \n");
231 " ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
232 fprintf(stdout, "\n");
234 "-TP : divide packets of every tile into tile-parts (-TP R) [R, L, C]\n");
235 fprintf(stdout, "\n");
237 "-x : create an index file *.Idx (-x index_name.Idx) \n");
238 fprintf(stdout, "\n");
240 "-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
241 fprintf(stdout, " for component c=%%d [%%d = 0,1,2]\n");
243 " with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI c=0,U=25) \n");
244 fprintf(stdout, "\n");
246 "-d : offset of the origin of the image (-d 150,300) \n");
247 fprintf(stdout, "\n");
249 "-T : offset of the origin of the tiles (-T 100,75) \n");
250 fprintf(stdout, "\n");
251 fprintf(stdout, "-I : use the irreversible DWT 9-7 (-I) \n");
252 fprintf(stdout, "\n");
254 "-jpip : write jpip codestream index box in JP2 output file\n");
255 fprintf(stdout, " NOTICE: currently supports only RPCL order\n");
256 fprintf(stdout, "\n");
260 "-W : adoption of JPWL (Part 11) capabilities (-W params)\n");
262 " The parameters can be written and repeated in any order:\n");
264 " [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
266 " ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
267 fprintf(stdout, "\n");
269 " h selects the header error protection (EPB): 'type' can be\n");
271 " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
273 " if 'tilepart' is absent, it is for main and tile headers\n");
275 " if 'tilepart' is present, it applies from that tile\n");
277 " onwards, up to the next h<> spec, or to the last tilepart\n");
278 fprintf(stdout, " in the codestream (max. %d specs)\n",
279 JPWL_MAX_NO_TILESPECS);
280 fprintf(stdout, "\n");
282 " p selects the packet error protection (EEP/UEP with EPBs)\n");
283 fprintf(stdout, " to be applied to raw data: 'type' can be\n");
285 " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
287 " if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
289 " if 'tilepart:pack' is present, it applies from that tile\n");
291 " and that packet onwards, up to the next packet spec\n");
293 " or to the last packet in the last tilepart in the stream\n");
294 fprintf(stdout, " (max. %d specs)\n",
295 JPWL_MAX_NO_PACKSPECS);
296 fprintf(stdout, "\n");
298 " s enables sensitivity data insertion (ESD): 'method' can be\n");
300 " [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
302 " 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
304 " if 'tilepart' is absent, it is for main header only\n");
306 " if 'tilepart' is present, it applies from that tile\n");
308 " onwards, up to the next s<> spec, or to the last tilepart\n");
309 fprintf(stdout, " in the codestream (max. %d specs)\n",
310 JPWL_MAX_NO_TILESPECS);
311 fprintf(stdout, "\n");
313 " g determines the addressing mode: <range> can be\n");
314 fprintf(stdout, " [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
315 fprintf(stdout, "\n");
317 " a determines the size of data addressing: <addr> can be\n");
319 " 2/4 bytes (small/large codestreams). If not set, auto-mode\n");
320 fprintf(stdout, "\n");
322 " z determines the size of sensitivity values: <size> can be\n");
324 " 1/2 bytes, for the transformed pseudo-floating point value\n");
325 fprintf(stdout, "\n");
326 fprintf(stdout, " ex.:\n");
328 " h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
329 fprintf(stdout, " s0=6,s3=-1,a=0,g=1,z=1\n");
330 fprintf(stdout, " means\n");
332 " predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
334 " CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
336 " UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
338 " UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
340 " UEP rs default for packets of tilepart 1,\n");
342 " no UEP for packets 0 to 19 of tilepart 3,\n");
344 " UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
345 fprintf(stdout, " relative sensitivity ESD for MH,\n");
347 " TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
349 " size of addresses and 1 byte for each sensitivity value\n");
350 fprintf(stdout, "\n");
351 fprintf(stdout, " ex.:\n");
352 fprintf(stdout, " h,s,p\n");
353 fprintf(stdout, " means\n");
355 " default protection to headers (MH and TPHs) as well as\n");
356 fprintf(stdout, " data packets, one ESD in MH\n");
357 fprintf(stdout, "\n");
359 " N.B.: use the following recommendations when specifying\n");
360 fprintf(stdout, " the JPWL parameters list\n");
362 " - when you use UEP, always pair the 'p' option with 'h'\n");
363 fprintf(stdout, " \n");
364 #endif /* USE_JPWL */
366 fprintf(stdout, "IMPORTANT:\n");
367 fprintf(stdout, "-----------\n");
368 fprintf(stdout, "\n");
369 fprintf(stdout, "The index file has the structure below:\n");
370 fprintf(stdout, "---------------------------------------\n");
371 fprintf(stdout, "\n");
372 fprintf(stdout, "Image_height Image_width\n");
373 fprintf(stdout, "progression order\n");
374 fprintf(stdout, "Tiles_size_X Tiles_size_Y\n");
375 fprintf(stdout, "Tiles_nb_X Tiles_nb_Y\n");
376 fprintf(stdout, "Components_nb\n");
377 fprintf(stdout, "Layers_nb\n");
378 fprintf(stdout, "decomposition_levels\n");
379 fprintf(stdout, "[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
380 fprintf(stdout, " [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
381 fprintf(stdout, "Main_header_start_position\n");
382 fprintf(stdout, "Main_header_end_position\n");
383 fprintf(stdout, "Codestream_size\n");
384 fprintf(stdout, "\n");
385 fprintf(stdout, "INFO ON TILES\n");
387 "tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
389 "Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
391 "Tile_1 '' '' '' '' '' '' ''\n");
392 fprintf(stdout, "...\n");
394 "Tile_Nt '' '' '' '' '' '' ''\n");
395 fprintf(stdout, "...\n");
396 fprintf(stdout, "TILE 0 DETAILS\n");
397 fprintf(stdout, "part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
398 fprintf(stdout, "...\n");
399 fprintf(stdout, "Progression_string\n");
401 "pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
403 "Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
404 fprintf(stdout, "...\n");
406 "Tpacket_Np '' '' '' '' '' '' '' ''\n");
408 fprintf(stdout, "MaxDisto\n");
410 fprintf(stdout, "TotalDisto\n\n");
414 static OPJ_PROG_ORDER give_progression(const char progression[4])
416 if (strncmp(progression, "LRCP", 4) == 0) {
419 if (strncmp(progression, "RLCP", 4) == 0) {
422 if (strncmp(progression, "RPCL", 4) == 0) {
425 if (strncmp(progression, "PCRL", 4) == 0) {
428 if (strncmp(progression, "CPRL", 4) == 0) {
435 static int initialise_4K_poc(opj_poc_t *POC, int numres)
441 POC[0].resno1 = numres - 1;
445 POC[1].resno0 = numres - 1;
448 POC[1].resno1 = numres;
454 static void cinema_parameters(opj_cparameters_t *parameters)
456 parameters->tile_size_on = OPJ_FALSE;
457 parameters->cp_tdx = 1;
458 parameters->cp_tdy = 1;
461 parameters->tp_flag = 'C';
462 parameters->tp_on = 1;
464 /*Tile and Image shall be at (0,0)*/
465 parameters->cp_tx0 = 0;
466 parameters->cp_ty0 = 0;
467 parameters->image_offset_x0 = 0;
468 parameters->image_offset_y0 = 0;
470 /*Codeblock size= 32*32*/
471 parameters->cblockw_init = 32;
472 parameters->cblockh_init = 32;
473 parameters->csty |= 0x01;
475 /*The progression order shall be CPRL*/
476 parameters->prog_order = CPRL;
479 parameters->roi_compno = -1;
481 parameters->subsampling_dx = 1;
482 parameters->subsampling_dy = 1;
485 parameters->irreversible = 1;
489 static void cinema_setup_encoder(opj_cparameters_t *parameters,
490 opj_image_t *image, img_fol_t *img_fol)
494 opj_poc_t *POC = NULL;
496 switch (parameters->cp_cinema) {
499 if (parameters->numresolution > 6) {
500 parameters->numresolution = 6;
502 if (!((image->comps[0].w == 2048) | (image->comps[0].h == 1080))) {
504 "Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3"
505 "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
506 image->comps[0].w, image->comps[0].h);
507 parameters->cp_rsiz = STD_RSIZ;
512 if (parameters->numresolution < 1) {
513 parameters->numresolution = 1;
514 } else if (parameters->numresolution > 7) {
515 parameters->numresolution = 7;
517 if (!((image->comps[0].w == 4096) | (image->comps[0].h == 2160))) {
519 "Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
520 "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
521 image->comps[0].w, image->comps[0].h);
522 parameters->cp_rsiz = STD_RSIZ;
524 parameters->numpocs = initialise_4K_poc(parameters->POC,
525 parameters->numresolution);
529 switch (parameters->cp_cinema) {
532 for (i = 0 ; i < parameters->tcp_numlayers ; i++) {
534 if (img_fol->rates[i] == 0) {
535 parameters->tcp_rates[0] = ((float)(image->numcomps * image->comps[0].w *
536 image->comps[0].h * image->comps[0].prec)) /
537 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
539 temp_rate = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h *
540 image->comps[0].prec)) /
541 (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
542 if (temp_rate > CINEMA_24_CS) {
543 parameters->tcp_rates[i] = ((float)(image->numcomps * image->comps[0].w *
544 image->comps[0].h * image->comps[0].prec)) /
545 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
547 parameters->tcp_rates[i] = img_fol->rates[i];
551 parameters->max_comp_size = COMP_24_CS;
555 for (i = 0 ; i < parameters->tcp_numlayers ; i++) {
557 if (img_fol->rates[i] == 0) {
558 parameters->tcp_rates[0] = ((float)(image->numcomps * image->comps[0].w *
559 image->comps[0].h * image->comps[0].prec)) /
560 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
562 temp_rate = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h *
563 image->comps[0].prec)) /
564 (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
565 if (temp_rate > CINEMA_48_CS) {
566 parameters->tcp_rates[0] = ((float)(image->numcomps * image->comps[0].w *
567 image->comps[0].h * image->comps[0].prec)) /
568 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
570 parameters->tcp_rates[i] = img_fol->rates[i];
574 parameters->max_comp_size = COMP_48_CS;
577 parameters->cp_disto_alloc = 1;
581 /* ------------------------------------------------------------------------------------ */
582 static int parse_cmdline_encoder(int argc, char **argv,
583 opj_cparameters_t *parameters,
584 img_fol_t *img_fol, char *indexfilename)
587 opj_option_t long_option[] = {
588 {"cinema2K", REQ_ARG, NULL, 'w'},
589 {"cinema4K", NO_ARG, NULL, 'y'},
590 {"ImgDir", REQ_ARG, NULL, 'z'},
591 {"TP", REQ_ARG, NULL, 'u'},
592 {"SOP", NO_ARG, NULL, 'S'},
593 {"EPH", NO_ARG, NULL, 'E'},
594 {"OutFor", REQ_ARG, NULL, 'O'},
595 {"POC", REQ_ARG, NULL, 'P'},
596 {"ROI", REQ_ARG, NULL, 'R'},
597 {"jpip", NO_ARG, NULL, 'J'}
600 /* parse the command line */
602 const char optlist[] = "i:o:hr:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:"
605 #endif /* USE_JPWL */
608 /*printf("C: parse_cmdline_encoder:");
609 for (i=0; i<argc; i++) {
610 printf("[%s]",argv[i]);
614 totlen = sizeof(long_option);
615 img_fol->set_out_format = 0;
616 reset_options_reading();
619 int c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
625 /* ----------------------------------------------------- */
627 case 'o': { /* output file */
628 char *outfile = opj_optarg;
629 parameters->cod_format = get_file_format(outfile);
630 switch (parameters->cod_format) {
636 "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
639 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile) - 1);
643 /* ----------------------------------------------------- */
644 case 'O': { /* output format */
646 char *of = opj_optarg;
647 sprintf(outformat, ".%s", of);
648 img_fol->set_out_format = 1;
649 parameters->cod_format = get_file_format(outformat);
650 switch (parameters->cod_format) {
653 img_fol->out_format = opj_optarg;
656 fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n");
663 /* ----------------------------------------------------- */
666 case 'r': { /* rates rates/distortion */
667 char *s = opj_optarg;
668 while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) ==
670 parameters->tcp_numlayers++;
671 while (*s && *s != ',') {
679 parameters->cp_disto_alloc = 1;
683 /* ----------------------------------------------------- */
685 case 'q': { /* add fixed_quality */
686 char *s = opj_optarg;
687 while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers])
689 parameters->tcp_numlayers++;
690 while (*s && *s != ',') {
698 parameters->cp_fixed_quality = 1;
703 /* ----------------------------------------------------- */
705 case 'f': { /* mod fixed_quality (before : -q) */
706 int *row = NULL, *col = NULL;
707 int numlayers = 0, numresolution = 0, matrix_width = 0;
709 char *s = opj_optarg;
710 sscanf(s, "%d", &numlayers);
716 parameters->tcp_numlayers = numlayers;
717 numresolution = parameters->numresolution;
718 matrix_width = numresolution * 3;
719 parameters->cp_matrice = (int *) opj_malloc(numlayers * matrix_width * sizeof(
723 for (i = 0; i < numlayers; i++) {
724 row = ¶meters->cp_matrice[i * matrix_width];
726 parameters->tcp_rates[i] = 1;
727 sscanf(s, "%d,", &col[0]);
734 for (j = 1; j < numresolution; j++) {
736 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
748 if (i < numlayers - 1) {
752 parameters->cp_fixed_alloc = 1;
756 /* ----------------------------------------------------- */
758 case 't': { /* tiles */
759 sscanf(opj_optarg, "%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy);
760 parameters->tile_size_on = OPJ_TRUE;
764 /* ----------------------------------------------------- */
766 case 'n': { /* resolution */
767 sscanf(opj_optarg, "%d", ¶meters->numresolution);
771 /* ----------------------------------------------------- */
772 case 'c': { /* precinct dimension */
776 char *s = opj_optarg;
779 sscanf(s, "[%d,%d]%c", ¶meters->prcw_init[res_spec],
780 ¶meters->prch_init[res_spec], &sep);
781 parameters->csty |= 0x01;
783 s = strpbrk(s, "]") + 2;
784 } while (sep == ',');
785 parameters->res_spec = res_spec;
789 /* ----------------------------------------------------- */
791 case 'b': { /* code-block dimension */
792 int cblockw_init = 0, cblockh_init = 0;
793 sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init);
794 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
795 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
797 "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
798 " * width*height<=4096\n * 4<=width,height<= 1024\n\n");
801 parameters->cblockw_init = cblockw_init;
802 parameters->cblockh_init = cblockh_init;
806 /* ----------------------------------------------------- */
808 case 'x': { /* creation of index file */
809 char *index = opj_optarg;
810 strncpy(indexfilename, index, OPJ_PATH_LEN);
814 /* ----------------------------------------------------- */
816 case 'p': { /* progression order */
819 strncpy(progression, opj_optarg, 4);
820 parameters->prog_order = give_progression(progression);
821 if (parameters->prog_order == -1) {
822 fprintf(stderr, "Unrecognized progression order "
823 "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
829 /* ----------------------------------------------------- */
831 case 's': { /* subsampling factor */
832 if (sscanf(opj_optarg, "%d,%d", ¶meters->subsampling_dx,
833 ¶meters->subsampling_dy) != 2) {
834 fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
840 /* ----------------------------------------------------- */
842 case 'd': { /* coordonnate of the reference grid */
843 if (sscanf(opj_optarg, "%d,%d", ¶meters->image_offset_x0,
844 ¶meters->image_offset_y0) != 2) {
845 fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
846 "error !! [-d x0,y0]\n");
852 /* ----------------------------------------------------- */
854 case 'h': /* display an help description */
855 encode_help_display();
858 /* ----------------------------------------------------- */
860 case 'P': { /* POC */
861 int numpocs = 0; /* number of progression order change (POC) default 0 */
862 opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
864 char *s = opj_optarg;
865 POC = parameters->POC;
867 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile,
868 &POC[numpocs].resno0, &POC[numpocs].compno0,
869 &POC[numpocs].layno1, &POC[numpocs].resno1,
870 &POC[numpocs].compno1, &POC[numpocs].progorder) == 7) {
871 POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
873 while (*s && *s != '/') {
881 parameters->numpocs = numpocs;
885 /* ------------------------------------------------------ */
887 case 'S': { /* SOP marker */
888 parameters->csty |= 0x02;
892 /* ------------------------------------------------------ */
894 case 'E': { /* EPH marker */
895 parameters->csty |= 0x04;
899 /* ------------------------------------------------------ */
901 case 'M': { /* Mode switch pas tous au point !! */
903 if (sscanf(opj_optarg, "%d", &value) == 1) {
904 for (i = 0; i <= 5; i++) {
905 int cache = value & (1 << i);
907 parameters->mode |= (1 << i);
914 /* ------------------------------------------------------ */
916 case 'R': { /* ROI */
917 if (sscanf(opj_optarg, "c=%d,U=%d", ¶meters->roi_compno,
918 ¶meters->roi_shift) != 2) {
919 fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n");
925 /* ------------------------------------------------------ */
927 case 'T': { /* Tile offset */
928 if (sscanf(opj_optarg, "%d,%d", ¶meters->cp_tx0,
929 ¶meters->cp_ty0) != 2) {
930 fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
936 /* ------------------------------------------------------ */
938 case 'C': { /* add a comment */
939 parameters->cp_comment = (char*)opj_malloc(strlen(opj_optarg) + 1);
940 if (parameters->cp_comment) {
941 strcpy(parameters->cp_comment, opj_optarg);
947 /* ------------------------------------------------------ */
949 case 'I': { /* reversible or not */
950 parameters->irreversible = 1;
954 /* ------------------------------------------------------ */
956 case 'u': { /* Tile part generation*/
957 parameters->tp_flag = opj_optarg[0];
958 parameters->tp_on = 1;
962 /* ------------------------------------------------------ */
964 case 'z': { /* Image Directory path */
965 img_fol->imgdirpath = (char*)opj_malloc(strlen(opj_optarg) + 1);
966 strcpy(img_fol->imgdirpath, opj_optarg);
967 img_fol->set_imgdir = 1;
971 /* ------------------------------------------------------ */
973 case 'w': { /* Digital Cinema 2K profile compliance*/
975 sscanf(opj_optarg, "%d", &fps);
977 parameters->cp_cinema = CINEMA2K_24;
978 } else if (fps == 48) {
979 parameters->cp_cinema = CINEMA2K_48;
981 fprintf(stderr, "Incorrect value!! must be 24 or 48\n");
984 fprintf(stdout, "CINEMA 2K compliant codestream\n");
985 parameters->cp_rsiz = CINEMA2K;
990 /* ------------------------------------------------------ */
992 case 'y': { /* Digital Cinema 4K profile compliance*/
993 parameters->cp_cinema = CINEMA4K_24;
994 fprintf(stdout, "CINEMA 4K compliant codestream\n");
995 parameters->cp_rsiz = CINEMA4K;
999 /* ------------------------------------------------------ */
1003 /* ------------------------------------------------------ */
1005 case 'W': { /* JPWL capabilities switched on */
1007 int hprot, pprot, sens, addr, size, range;
1009 /* we need to enable indexing */
1010 if (!indexfilename) {
1011 strncpy(indexfilename, JPWL_PRIVATEINDEX_NAME, OPJ_PATH_LEN);
1014 /* search for different protection methods */
1016 /* break the option in comma points and parse the result */
1017 token = strtok(opj_optarg, ",");
1018 while (token != NULL) {
1020 /* search header error protection method */
1021 if (*token == 'h') {
1023 static int tile = 0, tilespec = 0, lasttileno = 0;
1025 hprot = 1; /* predefined method */
1027 if (sscanf(token, "h=%d", &hprot) == 1) {
1028 /* Main header, specified */
1029 if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
1030 ((hprot >= 37) && (hprot <= 128)))) {
1031 fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n",
1035 parameters->jpwl_hprot_MH = hprot;
1037 } else if (sscanf(token, "h%d=%d", &tile, &hprot) == 2) {
1038 /* Tile part header, specified */
1039 if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
1040 ((hprot >= 37) && (hprot <= 128)))) {
1041 fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n",
1047 "ERROR -> invalid tile part number on protection method t = %d\n", tile);
1050 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1051 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
1052 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
1055 } else if (sscanf(token, "h%d", &tile) == 1) {
1056 /* Tile part header, unspecified */
1059 "ERROR -> invalid tile part number on protection method t = %d\n", tile);
1062 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1063 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
1064 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
1068 } else if (!strcmp(token, "h")) {
1069 /* Main header, unspecified */
1070 parameters->jpwl_hprot_MH = hprot;
1073 fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
1079 /* search packet error protection method */
1080 if (*token == 'p') {
1082 static int pack = 0, tile = 0, packspec = 0, lastpackno = 0;
1084 pprot = 1; /* predefined method */
1086 if (sscanf(token, "p=%d", &pprot) == 1) {
1087 /* Method for all tiles and all packets */
1088 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1089 ((pprot >= 37) && (pprot <= 128)))) {
1090 fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n",
1094 parameters->jpwl_pprot_tileno[0] = 0;
1095 parameters->jpwl_pprot_packno[0] = 0;
1096 parameters->jpwl_pprot[0] = pprot;
1098 } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) {
1099 /* method specified from that tile on */
1100 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1101 ((pprot >= 37) && (pprot <= 128)))) {
1102 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1107 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1110 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1111 parameters->jpwl_pprot_tileno[packspec] = tile;
1112 parameters->jpwl_pprot_packno[packspec] = 0;
1113 parameters->jpwl_pprot[packspec++] = pprot;
1116 } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) {
1117 /* method fully specified from that tile and that packet on */
1118 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1119 ((pprot >= 37) && (pprot <= 128)))) {
1120 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1125 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1129 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
1133 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1134 parameters->jpwl_pprot_tileno[packspec] = tile;
1135 parameters->jpwl_pprot_packno[packspec] = pack;
1136 parameters->jpwl_pprot[packspec++] = pprot;
1139 } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) {
1140 /* default method from that tile and that packet on */
1141 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1142 ((pprot >= 37) && (pprot <= 128)))) {
1143 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1148 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1152 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
1156 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1157 parameters->jpwl_pprot_tileno[packspec] = tile;
1158 parameters->jpwl_pprot_packno[packspec] = pack;
1159 parameters->jpwl_pprot[packspec++] = pprot;
1162 } else if (sscanf(token, "p%d", &tile) == 1) {
1163 /* default from a tile on */
1166 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1169 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1170 parameters->jpwl_pprot_tileno[packspec] = tile;
1171 parameters->jpwl_pprot_packno[packspec] = 0;
1172 parameters->jpwl_pprot[packspec++] = pprot;
1176 } else if (!strcmp(token, "p")) {
1178 parameters->jpwl_pprot_tileno[0] = 0;
1179 parameters->jpwl_pprot_packno[0] = 0;
1180 parameters->jpwl_pprot[0] = pprot;
1183 fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
1189 /* search sensitivity method */
1190 if (*token == 's') {
1192 static int tile = 0, tilespec = 0, lasttileno = 0;
1194 sens = 0; /* predefined: relative error */
1196 if (sscanf(token, "s=%d", &sens) == 1) {
1197 /* Main header, specified */
1198 if ((sens < -1) || (sens > 7)) {
1199 fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n",
1203 parameters->jpwl_sens_MH = sens;
1205 } else if (sscanf(token, "s%d=%d", &tile, &sens) == 2) {
1206 /* Tile part header, specified */
1207 if ((sens < -1) || (sens > 7)) {
1208 fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n",
1214 "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
1217 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1218 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
1219 parameters->jpwl_sens_TPH[tilespec++] = sens;
1222 } else if (sscanf(token, "s%d", &tile) == 1) {
1223 /* Tile part header, unspecified */
1226 "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
1229 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1230 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
1231 parameters->jpwl_sens_TPH[tilespec++] = hprot;
1234 } else if (!strcmp(token, "s")) {
1235 /* Main header, unspecified */
1236 parameters->jpwl_sens_MH = sens;
1239 fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token);
1243 parameters->jpwl_sens_size = 2; /* 2 bytes for default size */
1246 /* search addressing size */
1247 if (*token == 'a') {
1249 static int tile = 0, tilespec = 0, lasttileno = 0;
1251 addr = 0; /* predefined: auto */
1253 if (sscanf(token, "a=%d", &addr) == 1) {
1255 if ((addr != 0) && (addr != 2) && (addr != 4)) {
1256 fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr);
1259 parameters->jpwl_sens_addr = addr;
1261 } else if (!strcmp(token, "a")) {
1263 parameters->jpwl_sens_addr = addr; /* auto for default size */
1266 fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token);
1272 /* search sensitivity size */
1273 if (*token == 'z') {
1275 static int tile = 0, tilespec = 0, lasttileno = 0;
1277 size = 1; /* predefined: 1 byte */
1279 if (sscanf(token, "z=%d", &size) == 1) {
1281 if ((size != 0) && (size != 1) && (size != 2)) {
1282 fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size);
1285 parameters->jpwl_sens_size = size;
1287 } else if (!strcmp(token, "a")) {
1289 parameters->jpwl_sens_size = size; /* 1 for default size */
1292 fprintf(stderr, "ERROR -> invalid size selection = %s\n", token);
1298 /* search range method */
1299 if (*token == 'g') {
1301 static int tile = 0, tilespec = 0, lasttileno = 0;
1303 range = 0; /* predefined: 0 (packet) */
1305 if (sscanf(token, "g=%d", &range) == 1) {
1307 if ((range < 0) || (range > 3)) {
1308 fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range);
1311 parameters->jpwl_sens_range = range;
1313 } else if (!strcmp(token, "g")) {
1315 parameters->jpwl_sens_range = range;
1318 fprintf(stderr, "ERROR -> invalid range selection = %s\n", token);
1324 /* next token or bust */
1325 token = strtok(NULL, ",");
1330 fprintf(stdout, "Info: JPWL capabilities enabled\n");
1331 parameters->jpwl_epc_on = true;
1335 #endif /* USE_JPWL */
1337 /* ------------------------------------------------------ */
1340 /* ------------------------------------------------------ */
1343 fprintf(stderr, "ERROR -> Command line not valid\n");
1348 /* check for possible errors */
1349 if (parameters->cp_cinema) {
1350 if (parameters->tcp_numlayers > 1) {
1351 parameters->cp_rsiz = STD_RSIZ;
1353 "Warning: DC profiles do not allow more than one quality layer. The codestream created will not be compliant with the DC profile\n");
1357 if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc ||
1358 parameters->cp_fixed_quality)
1359 && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^
1360 parameters->cp_fixed_quality))) {
1361 fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n");
1363 } /* mod fixed_quality */
1365 /* if no rate entered, lossless by default */
1366 if (parameters->tcp_numlayers == 0) {
1367 parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */
1368 parameters->tcp_numlayers++;
1369 parameters->cp_disto_alloc = 1;
1372 if ((parameters->cp_tx0 > parameters->image_offset_x0) ||
1373 (parameters->cp_ty0 > parameters->image_offset_y0)) {
1375 "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
1376 parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0,
1377 parameters->image_offset_y0);
1381 for (i = 0; i < parameters->numpocs; i++) {
1382 if (parameters->POC[i].prg == -1) {
1384 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
1393 /** Create the same index as j2k_create_index does, but in an int[] instead of in a file ==> easy to pass it back to Java, to transfer it etc.
1394 @param buffer_size, increased by the length of the compressed index, in number of bytes
1395 @return a pointer to a char[]
1396 Syntax of the index:
1397 one char for the version number (1): one byte because no problem with little endian, big endian etc.
1398 one int for each of the following information:
1410 for each resolution:
1414 End main header position
1419 tile start pos in codestream
1420 tile header end position
1421 tile end position in codestream
1423 For each LRCP, RLCP etc.:
1430 start position in the codestream
1431 end position of this packet
1433 static char* create_index_into_byte_array(opj_codestream_info_t *cstr_info,
1436 int tileno, compno, layno, resno, precno, pack_nb, x, y;
1437 char* buffer = NULL;
1442 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
1443 for (resno = 0; resno < cstr_info->numdecompos[0] + 1; resno++) {
1444 prec_max = int_max(prec_max,
1445 cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]);
1449 /* Compute the size of the index buffer, in number of bytes*/
1452 + (10 /* image_w until decomposition */
1453 + (cstr_info->numdecompos[0] + 1) * 2 /* pdx size for each tile */
1454 + 2 /* main_head_end + codestream_size */
1455 + cstr_info->tw * cstr_info->th * 4 /* tile info, without distortion info */
1456 + cstr_info->tw * cstr_info->th * cstr_info->numlayers *
1457 (cstr_info->numdecompos[0] + 1) * cstr_info->numcomps * prec_max * 8
1459 /*printf("C: index buffer size = %d bytes\n", *buffer_size);*/
1460 buffer = (char*) opj_malloc(*buffer_size);
1463 /*opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to allocate index buffer for writing %d int\n", *buffer_size);*/
1464 fprintf(stderr, "failed to allocate index buffer for writing %d int\n",
1469 buffer[0] = 1; /* Version stored on a byte*/
1471 /* Remaining information are stored on a int.*/
1472 ((int*)buffer)[buffer_pos++] = cstr_info->image_w;
1473 ((int*)buffer)[buffer_pos++] = cstr_info->image_h;
1474 ((int*)buffer)[buffer_pos++] = cstr_info->prog;
1475 ((int*)buffer)[buffer_pos++] = cstr_info->tile_x;
1476 ((int*)buffer)[buffer_pos++] = cstr_info->tile_y;
1477 ((int*)buffer)[buffer_pos++] = cstr_info->tw;
1478 ((int*)buffer)[buffer_pos++] = cstr_info->th;
1479 ((int*)buffer)[buffer_pos++] = cstr_info->numcomps;
1480 ((int*)buffer)[buffer_pos++] = cstr_info->numlayers;
1481 ((int*)buffer)[buffer_pos++] = cstr_info->numdecompos[0];
1483 for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) {
1484 /* based on tile 0 */
1485 ((int*)buffer)[buffer_pos++] = (1 << cstr_info->tile[0].pdx[resno]);
1486 ((int*)buffer)[buffer_pos++] = (1 << cstr_info->tile[0].pdx[resno]);
1488 ((int*)buffer)[buffer_pos++] = cstr_info->main_head_end;
1489 ((int*)buffer)[buffer_pos++] = cstr_info->codestream_size;
1491 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
1492 ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].tileno;
1493 ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].start_pos;
1494 ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].end_header;
1495 ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].end_pos;
1498 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
1499 int start_pos, end_pos;
1500 int max_numdecompos = 0;
1503 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1504 if (max_numdecompos < cstr_info->numdecompos[compno]) {
1505 max_numdecompos = cstr_info->numdecompos[compno];
1509 if (cstr_info->prog == LRCP) { /* LRCP */
1511 for (layno = 0; layno < cstr_info->numlayers; layno++) {
1512 for (resno = 0; resno < max_numdecompos + 1; resno++) {
1513 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1515 if (resno > cstr_info->numdecompos[compno]) {
1518 prec_max = cstr_info->tile[tileno].pw[resno] *
1519 cstr_info->tile[tileno].ph[resno];
1520 for (precno = 0; precno < prec_max; precno++) {
1521 start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
1522 end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
1523 ((int*)buffer)[buffer_pos++] = pack_nb;
1524 ((int*)buffer)[buffer_pos++] = tileno;
1525 ((int*)buffer)[buffer_pos++] = layno;
1526 ((int*)buffer)[buffer_pos++] = resno;
1527 ((int*)buffer)[buffer_pos++] = compno;
1528 ((int*)buffer)[buffer_pos++] = precno;
1529 ((int*)buffer)[buffer_pos++] = start_pos;
1530 ((int*)buffer)[buffer_pos++] = end_pos;
1537 else if (cstr_info->prog == RLCP) { /* RLCP */
1539 for (resno = 0; resno < max_numdecompos + 1; resno++) {
1540 for (layno = 0; layno < cstr_info->numlayers; layno++) {
1541 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1543 if (resno > cstr_info->numdecompos[compno]) {
1546 prec_max = cstr_info->tile[tileno].pw[resno] *
1547 cstr_info->tile[tileno].ph[resno];
1548 for (precno = 0; precno < prec_max; precno++) {
1549 start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
1550 end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
1551 ((int*)buffer)[buffer_pos++] = pack_nb;
1552 ((int*)buffer)[buffer_pos++] = tileno;
1553 ((int*)buffer)[buffer_pos++] = resno;
1554 ((int*)buffer)[buffer_pos++] = layno;
1555 ((int*)buffer)[buffer_pos++] = compno;
1556 ((int*)buffer)[buffer_pos++] = precno;
1557 ((int*)buffer)[buffer_pos++] = start_pos;
1558 ((int*)buffer)[buffer_pos++] = end_pos;
1565 else if (cstr_info->prog == RPCL) { /* RPCL */
1567 for (resno = 0; resno < max_numdecompos + 1; resno++) {
1568 /* I suppose components have same XRsiz, YRsiz */
1569 int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
1570 (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
1571 int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
1572 (float)cstr_info->tw) * cstr_info->tile_y;
1573 int x1 = x0 + cstr_info->tile_x;
1574 int y1 = y0 + cstr_info->tile_y;
1575 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1577 if (resno > cstr_info->numdecompos[compno]) {
1580 prec_max = cstr_info->tile[tileno].pw[resno] *
1581 cstr_info->tile[tileno].ph[resno];
1582 for (precno = 0; precno < prec_max; precno++) {
1583 int pcnx = cstr_info->tile[tileno].pw[resno];
1584 int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
1585 cstr_info->numdecompos[compno] - resno);
1586 int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
1587 cstr_info->numdecompos[compno] - resno);
1588 int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
1589 int precno_y = (int) floor((float)precno / (float)pcnx);
1590 for (y = y0; y < y1; y++) {
1591 if (precno_y * pcy == y) {
1592 for (x = x0; x < x1; x++) {
1593 if (precno_x * pcx == x) {
1594 for (layno = 0; layno < cstr_info->numlayers; layno++) {
1595 start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
1596 end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
1597 ((int*)buffer)[buffer_pos++] = pack_nb;
1598 ((int*)buffer)[buffer_pos++] = tileno;
1599 ((int*)buffer)[buffer_pos++] = resno;
1600 ((int*)buffer)[buffer_pos++] = precno;
1601 ((int*)buffer)[buffer_pos++] = compno;
1602 ((int*)buffer)[buffer_pos++] = layno;
1603 ((int*)buffer)[buffer_pos++] = start_pos;
1604 ((int*)buffer)[buffer_pos++] = end_pos;
1615 else if (cstr_info->prog == PCRL) { /* PCRL */
1616 /* I suppose components have same XRsiz, YRsiz */
1617 int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
1618 (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
1619 int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
1620 (float)cstr_info->tw) * cstr_info->tile_y;
1621 int x1 = x0 + cstr_info->tile_x;
1622 int y1 = y0 + cstr_info->tile_y;
1624 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1625 for (resno = 0; resno < max_numdecompos + 1; resno++) {
1627 if (resno > cstr_info->numdecompos[compno]) {
1630 prec_max = cstr_info->tile[tileno].pw[resno] *
1631 cstr_info->tile[tileno].ph[resno];
1632 for (precno = 0; precno < prec_max; precno++) {
1633 int pcnx = cstr_info->tile[tileno].pw[resno];
1634 int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
1635 cstr_info->numdecompos[compno] - resno);
1636 int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
1637 cstr_info->numdecompos[compno] - resno);
1638 int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
1639 int precno_y = (int) floor((float)precno / (float)pcnx);
1640 for (y = y0; y < y1; y++) {
1641 if (precno_y * pcy == y) {
1642 for (x = x0; x < x1; x++) {
1643 if (precno_x * pcx == x) {
1644 for (layno = 0; layno < cstr_info->numlayers; layno++) {
1645 start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
1646 end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
1647 ((int*)buffer)[buffer_pos++] = pack_nb;
1648 ((int*)buffer)[buffer_pos++] = tileno;
1649 ((int*)buffer)[buffer_pos++] = precno;
1650 ((int*)buffer)[buffer_pos++] = compno;
1651 ((int*)buffer)[buffer_pos++] = resno;
1652 ((int*)buffer)[buffer_pos++] = layno;
1653 ((int*)buffer)[buffer_pos++] = start_pos;
1654 ((int*)buffer)[buffer_pos++] = end_pos;
1667 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1668 /* I suppose components have same XRsiz, YRsiz */
1669 int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
1670 (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
1671 int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
1672 (float)cstr_info->tw) * cstr_info->tile_y;
1673 int x1 = x0 + cstr_info->tile_x;
1674 int y1 = y0 + cstr_info->tile_y;
1676 for (resno = 0; resno < max_numdecompos + 1; resno++) {
1678 if (resno > cstr_info->numdecompos[compno]) {
1681 prec_max = cstr_info->tile[tileno].pw[resno] *
1682 cstr_info->tile[tileno].ph[resno];
1683 for (precno = 0; precno < prec_max; precno++) {
1684 int pcnx = cstr_info->tile[tileno].pw[resno];
1685 int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
1686 cstr_info->numdecompos[compno] - resno);
1687 int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
1688 cstr_info->numdecompos[compno] - resno);
1689 int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
1690 int precno_y = (int) floor((float)precno / (float)pcnx);
1691 for (y = y0; y < y1; y++) {
1692 if (precno_y * pcy == y) {
1693 for (x = x0; x < x1; x++) {
1694 if (precno_x * pcx == x) {
1695 for (layno = 0; layno < cstr_info->numlayers; layno++) {
1696 start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
1697 end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
1698 ((int*)buffer)[buffer_pos++] = pack_nb;
1699 ((int*)buffer)[buffer_pos++] = tileno;
1700 ((int*)buffer)[buffer_pos++] = compno;
1701 ((int*)buffer)[buffer_pos++] = precno;
1702 ((int*)buffer)[buffer_pos++] = resno;
1703 ((int*)buffer)[buffer_pos++] = layno;
1704 ((int*)buffer)[buffer_pos++] = start_pos;
1705 ((int*)buffer)[buffer_pos++] = end_pos;
1718 if (buffer_pos > *buffer_size) {
1719 /*opj_event_msg(j2k->cinfo, EVT_ERROR, "index creation: buffer_pos (%d) > buffer_size (%d)!\n", buffer_pos, *buffer_size);*/
1720 fprintf(stderr, "index creation: buffer_pos (%d) > buffer_size (%d)!\n",
1721 buffer_pos, *buffer_size);
1731 /* --------------------------------------------------------------------------
1732 ------------ Get the image byte[] from the Java object -------------------*/
1734 static opj_image_t* loadImage(opj_cparameters_t *parameters, JNIEnv *env,
1735 jobject obj, jclass cls)
1737 int i, max, shift, w, h, depth;
1738 opj_image_t * img = NULL;
1739 int compno, numcomps;
1740 opj_image_t * image = NULL;
1741 opj_image_comp_t *comp;
1742 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
1743 OPJ_COLOR_SPACE color_space;
1755 /* Image width, height and depth*/
1756 fid = (*env)->GetFieldID(env, cls, "width", "I");
1757 ji = (*env)->GetIntField(env, obj, fid);
1760 fid = (*env)->GetFieldID(env, cls, "height", "I");
1761 ji = (*env)->GetIntField(env, obj, fid);
1764 fid = (*env)->GetFieldID(env, cls, "depth", "I");
1765 ji = (*env)->GetIntField(env, obj, fid);
1771 color_space = CLRSPC_GRAY;
1774 color_space = CLRSPC_SRGB;
1776 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1778 if (numcomps == 1) {
1779 cmptparm[0].x0 = parameters->image_offset_x0;
1780 cmptparm[0].y0 = parameters->image_offset_y0;
1781 cmptparm[0].w = !cmptparm[0].x0 ? (w - 1) * parameters->subsampling_dx + 1 :
1782 cmptparm[0].x0 + (w - 1) * parameters->subsampling_dx + 1;
1783 cmptparm[0].h = !cmptparm[0].y0 ? (h - 1) * parameters->subsampling_dy + 1 :
1784 cmptparm[0].y0 + (h - 1) * parameters->subsampling_dy + 1;
1785 /* Java types are always signed but we use them as unsigned types (shift of the negative part of
1786 the pixels of the images in Telemis before entering the encoder).*/
1787 cmptparm[0].sgnd = 0;
1789 cmptparm[0].prec = depth;
1791 cmptparm[0].prec = 8;
1793 cmptparm[0].bpp = cmptparm[0].prec;
1794 cmptparm[0].dx = parameters->subsampling_dx;
1795 cmptparm[0].dy = parameters->subsampling_dy;
1796 /*printf("C: component 0 initialised: x0=%d, y0=%d, w=%d, h=%d, sgnd=%d, bpp=%d, dx=%d, dy=%d, color_space=%d\n", cmptparm[0].x0, cmptparm[0].y0, cmptparm[0].w,
1797 cmptparm[0].h, cmptparm[0].sgnd, cmptparm[0].bpp, cmptparm[0].dx, cmptparm[0].dy, color_space);*/
1799 for (i = 0; i < numcomps; i++) {
1800 cmptparm[i].prec = 8;
1801 cmptparm[i].bpp = 8;
1802 cmptparm[i].sgnd = 0;
1803 cmptparm[i].dx = parameters->subsampling_dx;
1804 cmptparm[i].dy = parameters->subsampling_dy;
1810 /* create the image */
1811 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1818 image->numcomps = 1;
1820 image->numcomps = 3;
1823 /* set image offset and reference grid */
1824 image->x0 = cmptparm[0].x0;
1825 image->y0 = cmptparm[0].x0;
1826 image->x1 = cmptparm[0].w;
1827 image->y1 = cmptparm[0].h;
1829 /* set image data */
1830 for (compno = 0; compno < numcomps; compno++) {
1831 comp = &image->comps[compno];
1834 fid = (*env)->GetFieldID(env, cls, "image8", "[B"); /* byteArray []*/
1835 jba = (*env)->GetObjectField(env, obj, fid);
1836 len = (*env)->GetArrayLength(env, jba);
1838 jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, &isCopy);
1839 /*printf("C: before transferring 8 bpp image\n");*/
1841 for (i = 0; i < len; i++) {
1842 comp->data[i] = (char) jbBody[i];
1843 if (comp->data[i] > max) {
1844 max = comp->data[i];
1848 for (i = 0; i < len; i++) {
1849 comp->data[i] = (unsigned char) jbBody[i];
1850 if (comp->data[i] > max) {
1851 max = comp->data[i];
1855 (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
1856 } else if (depth == 16) {
1857 fid = (*env)->GetFieldID(env, cls, "image16", "[S"); /* shortArray []*/
1858 jsa = (*env)->GetObjectField(env, obj, fid);
1859 len = (*env)->GetArrayLength(env, jsa);
1861 jsBody = (*env)->GetPrimitiveArrayCritical(env, jsa, &isCopy);
1862 /*printf("C: before transferring 16 bpp image\n");*/
1863 if (comp->sgnd) { /* Special behaviour to deal with signed elements ??*/
1864 comp->data[i] = (short) jsBody[i];
1865 for (i = 0; i < len; i++) {
1866 if (comp->data[i] > max) {
1867 max = comp->data[i];
1871 for (i = 0; i < len; i++) {
1872 comp->data[i] = (unsigned short) jsBody[i];
1873 if (comp->data[i] > max) {
1874 max = comp->data[i];
1878 (*env)->ReleasePrimitiveArrayCritical(env, jsa, jsBody, 0);
1879 } else if (depth == 24) {
1880 fid = (*env)->GetFieldID(env, cls, "image24", "[I"); /* intArray []*/
1881 jia = (*env)->GetObjectField(env, obj, fid);
1882 len = (*env)->GetArrayLength(env, jia);
1885 jiBody = (*env)->GetPrimitiveArrayCritical(env, jia, &isCopy);
1886 /*printf("C: before transferring 24 bpp image (component %d, signed = %d)\n", compno, comp->sgnd);*/
1887 if (comp->sgnd) { /* Special behaviour to deal with signed elements ?? XXXXX*/
1888 for (i = 0; i < len; i++) {
1889 comp->data[i] = (((int) jiBody[i]) & (0xFF << shift)) >> shift;
1890 if (comp->data[i] > max) {
1891 max = comp->data[i];
1895 for (i = 0; i < len; i++) {
1896 comp->data[i] = (((unsigned int) jiBody[i]) & (0xFF << shift)) >> shift;
1897 if (comp->data[i] > max) {
1898 max = comp->data[i];
1902 (*env)->ReleasePrimitiveArrayCritical(env, jia, jiBody, 0);
1904 comp->bpp = int_floorlog2(max) + 1;
1905 comp->prec = comp->bpp;
1906 /*printf("C: component %d: max %d, real bpp = %d\n", compno, max, comp->bpp);*/
1912 /* --------------------------------------------------------------------------
1913 -------------------- MAIN METHOD, CALLED BY JAVA -----------------------*/
1914 JNIEXPORT jlong JNICALL
1915 Java_org_openJpeg_OpenJPEGJavaEncoder_internalEncodeImageToJ2K(JNIEnv *env,
1916 jobject obj, jobjectArray javaParameters)
1918 int argc; /* To simulate the command line parameters (taken from the javaParameters variable) and be able to re-use the */
1919 char **argv; /* 'parse_cmdline_decoder' method taken from the j2k_to_image project */
1921 opj_cparameters_t parameters; /* compression parameters */
1923 opj_event_mgr_t event_mgr; /* event manager */
1924 opj_image_t *image = NULL;
1925 int i, j, num_images;
1927 opj_codestream_info_t cstr_info; /* Codestream information structure */
1928 char indexfilename[OPJ_PATH_LEN]; /* index file name */
1930 char* compressed_index = NULL;
1931 int compressed_index_size = -1;
1932 /* ==> Access variables to the Java member variables*/
1940 callback_variables_t msgErrorCallback_vars;
1941 /* <== access variable to the Java member variables.*/
1943 /* For the encoding and storage into the file*/
1945 int codestream_length;
1946 opj_cio_t *cio = NULL;
1949 /* JNI reference to the calling class*/
1950 cls = (*env)->GetObjectClass(env, obj);
1952 /* Pointers to be able to call a Java method for all the info and error messages*/
1953 msgErrorCallback_vars.env = env;
1954 msgErrorCallback_vars.jobj = &obj;
1955 msgErrorCallback_vars.message_mid = (*env)->GetMethodID(env, cls, "logMessage",
1956 "(Ljava/lang/String;)V");
1957 msgErrorCallback_vars.error_mid = (*env)->GetMethodID(env, cls, "logError",
1958 "(Ljava/lang/String;)V");
1960 arraySize = (*env)->GetArrayLength(env, javaParameters);
1961 argc = (int) arraySize + 1;
1962 argv = opj_malloc(argc * sizeof(char*));
1963 argv[0] = "ProgramName.exe"; /* The program name: useless*/
1965 for (i = 1; i < argc; i++) {
1966 object = (*env)->GetObjectArrayElement(env, javaParameters, i - 1);
1967 argv[i] = (char*)(*env)->GetStringUTFChars(env, object, &isCopy);
1971 for (i=0; i<argc; i++) {
1972 printf("[%s]",argv[i]);
1977 configure the event callbacks
1979 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
1980 event_mgr.error_handler = error_callback;
1981 event_mgr.warning_handler = warning_callback;
1982 event_mgr.info_handler = info_callback;
1984 /* set encoding parameters to default values */
1985 opj_set_default_encoder_parameters(¶meters);
1986 parameters.cod_format = J2K_CFMT;
1987 /*parameters.index_on = 1;*/
1989 /* Initialize indexfilename and img_fol */
1991 memset(&img_fol, 0, sizeof(img_fol_t));
1993 /* parse input and get user encoding parameters */
1994 if (parse_cmdline_encoder(argc, argv, ¶meters, &img_fol,
1995 indexfilename) == 1) {
1996 /* Release the Java arguments array*/
1997 for (i = 1; i < argc; i++) {
1998 (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env,
1999 javaParameters, i - 1), argv[i]);
2004 /* Release the Java arguments array*/
2005 for (i = 1; i < argc; i++) {
2006 (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env,
2007 javaParameters, i - 1), argv[i]);
2010 if (parameters.cp_cinema) {
2011 cinema_parameters(¶meters);
2015 /* Create comment for codestream */
2016 if (parameters.cp_comment == NULL) {
2017 const char comment[] = "Created by JavaOpenJPEG version ";
2018 const size_t clen = strlen(comment);
2019 const char *version = opj_version();
2022 parameters.cp_comment = (char*)opj_malloc(clen + strlen(version) + 11);
2023 sprintf(parameters.cp_comment, "%s%s with JPWL", comment, version);
2025 parameters.cp_comment = (char*)opj_malloc(clen + strlen(version) + 1);
2026 sprintf(parameters.cp_comment, "%s%s", comment, version);
2032 /* Read directory if necessary */
2035 /*Encoding image one by one*/
2036 for (imageno = 0; imageno < num_images; imageno++) {
2038 fprintf(stderr, "\n");
2040 image = loadImage(¶meters, env, obj, cls);
2041 /*printf("C: after load image: image = %d\n", image);*/
2043 fprintf(stderr, "Unable to load image\n");
2047 /* Decide if MCT should be used */
2048 parameters.tcp_mct = image->numcomps == 3 ? 1 : 0;
2050 if (parameters.cp_cinema) {
2051 cinema_setup_encoder(¶meters, image, &img_fol);
2054 /* encode the destination image */
2055 /* ---------------------------- */
2056 /* get a J2K compressor handle */
2057 if (parameters.cod_format == J2K_CFMT) { /* J2K format output */
2058 cinfo = opj_create_compress(CODEC_J2K);
2059 } else { /* JP2 format output */
2060 cinfo = opj_create_compress(CODEC_JP2);
2062 /* catch events using our callbacks and give a local context */
2063 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, &msgErrorCallback_vars);
2065 /* setup the encoder parameters using the current image and user parameters */
2066 opj_setup_encoder(cinfo, ¶meters, image);
2068 /* open a byte stream for writing */
2069 /* allocate memory for all tiles */
2070 cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
2072 /* encode the image */
2073 bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info);
2074 printf("C: after opj_encode_with_info\n");
2077 fprintf(stderr, "failed to encode image\n");
2080 codestream_length = cio_tell(cio);
2082 /* write the index on disk, if needed (-x 'filename') */
2083 if (*indexfilename) {
2084 bSuccess = write_index_file(&cstr_info, indexfilename);
2086 fprintf(stderr, "Failed to output index file into [%s]\n", indexfilename);
2090 compressed_index = create_index_into_byte_array(&cstr_info,
2091 &compressed_index_size);
2092 /* Allocates the Java compressedIndex byte[] and sends this index into the Java object */
2093 fid = (*env)->GetFieldID(env, cls, "compressedIndex", "[B");
2094 jba = (*env)->NewByteArray(env, compressed_index_size + 1);
2095 jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, 0);
2096 memcpy(jbBody, compressed_index, compressed_index_size);
2097 (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
2098 (*env)->SetObjectField(env, obj, fid, jba);
2099 opj_free(compressed_index);
2101 /* write the generated codestream to disk ? */
2102 if (parameters.outfile[0] != '\0') {
2103 f = fopen(parameters.outfile, "wb");
2105 fprintf(stderr, "failed to open [%s] for writing\n", parameters.outfile);
2108 fwrite(cio->buffer, 1, codestream_length, f);
2110 fprintf(stdout, "Generated outfile [%s]\n", parameters.outfile);
2113 /* Write the generated codestream to the Java pre-allocated compressedStream byte[] */
2114 fid = (*env)->GetFieldID(env, cls, "compressedStream", "[B");
2115 jba = (*env)->GetObjectField(env, obj, fid);
2116 jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, 0);
2117 memcpy(jbBody, cio->buffer, codestream_length);
2118 (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
2120 /* close and free the byte stream */
2123 /* free remaining compression structures */
2124 opj_destroy_compress(cinfo);
2125 opj_destroy_cstr_info(&cstr_info);
2127 /* free image data */
2128 opj_image_destroy(image);
2131 /* free user parameters structure */
2132 if (parameters.cp_comment) {
2133 opj_free(parameters.cp_comment);
2135 if (parameters.cp_matrice) {
2136 opj_free(parameters.cp_matrice);
2139 return codestream_length;