Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3.  * Copyright (c) 2002-2007, 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 and Antonin Descampe
  7.  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8.  * Copyright (c) 2006-2007, Parvatha Elangovan
  9.  * Copyright (c) 2007, Patrick Piscaglia (Telemis)
  10.  * All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  22.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31.  * POSSIBILITY OF SUCH DAMAGE.
  32.  */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <jni.h>
  37. #include <math.h>
  38.  
  39. #include "openjpeg.h"
  40. #include "getopt.h"
  41. #include "convert.h"
  42. #include "index.h"
  43. #include "dirent.h"
  44. #include "org_openJpeg_OpenJPEGJavaEncoder.h"
  45.  
  46. #ifndef _WIN32
  47. #define stricmp strcasecmp
  48. #define strnicmp strncasecmp
  49. #endif
  50.  
  51. #include "format_defs.h"
  52.  
  53. #define CINEMA_24_CS 1302083    /*Codestream length for 24fps*/
  54. #define CINEMA_48_CS 651041             /*Codestream length for 48fps*/
  55. #define COMP_24_CS 1041666              /*Maximum size per color component for 2K & 4K @ 24fps*/
  56. #define COMP_48_CS 520833               /*Maximum size per color component for 2K @ 48fps*/
  57.  
  58. extern int get_file_format(char *filename);
  59. extern void error_callback(const char *msg, void *client_data);
  60. extern warning_callback(const char *msg, void *client_data);
  61. extern void info_callback(const char *msg, void *client_data);
  62.  
  63. typedef struct callback_variables {
  64.         JNIEnv *env;
  65.         /** 'jclass' object used to call a Java method from the C */
  66.         jobject *jobj;
  67.         /** 'jclass' object used to call a Java method from the C */
  68.         jmethodID message_mid;
  69.         jmethodID error_mid;
  70. } callback_variables_t;
  71.  
  72. typedef struct dircnt{
  73.         /** Buffer for holding images read from Directory*/
  74.         char *filename_buf;
  75.         /** Pointer to the buffer*/
  76.         char **filename;
  77. }dircnt_t;
  78.  
  79. typedef struct img_folder{
  80.         /** The directory path of the folder containing input images*/
  81.         char *imgdirpath;
  82.         /** Output format*/
  83.         char *out_format;
  84.         /** Enable option*/
  85.         char set_imgdir;
  86.         /** Enable Cod Format for output*/
  87.         char set_out_format;
  88.         /** User specified rate stored in case of cinema option*/
  89.         float *rates;
  90. }img_fol_t;
  91.  
  92. void encode_help_display() {
  93.         fprintf(stdout,"HELP\n----\n\n");
  94.         fprintf(stdout,"- the -h option displays this help information on screen\n\n");
  95.  
  96. /* UniPG>> */
  97.         fprintf(stdout,"List of parameters for the JPEG 2000 "
  98. #ifdef USE_JPWL
  99.                 "+ JPWL "
  100. #endif /* USE_JPWL */
  101.                 "encoder:\n");
  102. /* <<UniPG */
  103.         fprintf(stdout,"\n");
  104.         fprintf(stdout,"REMARKS:\n");
  105.         fprintf(stdout,"---------\n");
  106.         fprintf(stdout,"\n");
  107.         fprintf(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
  108.         fprintf(stdout,"COD and QCD never appear in the tile_header.\n");
  109.         fprintf(stdout,"\n");
  110.         fprintf(stdout,"- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
  111.         fprintf(stdout,"color image.  You need enough disk space memory (twice the original) to encode \n");
  112.         fprintf(stdout,"the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
  113.         fprintf(stdout,"\n");
  114.         fprintf(stdout,"By default:\n");
  115.         fprintf(stdout,"------------\n");
  116.         fprintf(stdout,"\n");
  117.         fprintf(stdout," * Lossless\n");
  118.         fprintf(stdout," * 1 tile\n");
  119.         fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
  120.         fprintf(stdout," * Size of code-block : 64 x 64\n");
  121.         fprintf(stdout," * Number of resolutions: 6\n");
  122.         fprintf(stdout," * No SOP marker in the codestream\n");
  123.         fprintf(stdout," * No EPH marker in the codestream\n");
  124.         fprintf(stdout," * No sub-sampling in x or y direction\n");
  125.         fprintf(stdout," * No mode switch activated\n");
  126.         fprintf(stdout," * Progression order: LRCP\n");
  127.         fprintf(stdout," * No index file\n");
  128.         fprintf(stdout," * No ROI upshifted\n");
  129.         fprintf(stdout," * No offset of the origin of the image\n");
  130.         fprintf(stdout," * No offset of the origin of the tiles\n");
  131.         fprintf(stdout," * Reversible DWT 5-3\n");
  132. /* UniPG>> */
  133. #ifdef USE_JPWL
  134.         fprintf(stdout," * No JPWL protection\n");
  135. #endif /* USE_JPWL */
  136. /* <<UniPG */
  137.         fprintf(stdout,"\n");
  138.         fprintf(stdout,"Parameters:\n");
  139.         fprintf(stdout,"------------\n");
  140.         fprintf(stdout,"\n");
  141.         fprintf(stdout,"Required Parameters (except with -h):\n");
  142.         fprintf(stdout,"One of the two options -ImgDir or -i must be used\n");
  143.         fprintf(stdout,"\n");
  144.         fprintf(stdout,"-ImgDir      : Image file Directory path (example ../Images) \n");
  145.         fprintf(stdout,"    When using this option -OutFor must be used\n");
  146.         fprintf(stdout,"\n");
  147.         fprintf(stdout,"-OutFor \n");
  148.         fprintf(stdout,"    REQUIRED only if -ImgDir is used\n");
  149.         fprintf(stdout,"          Need to specify only format without filename <BMP>  \n");
  150.         fprintf(stdout,"    Currently accepts PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA formats\n");
  151.         fprintf(stdout,"\n");
  152.         fprintf(stdout,"-i           : source file  (-i source.pnm also *.pgm, *.ppm, *.bmp, *.tif, *.raw, *.tga) \n");
  153.         fprintf(stdout,"    When using this option -o must be used\n");
  154.         fprintf(stdout,"\n");
  155.         fprintf(stdout,"-o           : destination file (-o dest.j2k or .jp2) \n");
  156.         fprintf(stdout,"\n");
  157.         fprintf(stdout,"Optional Parameters:\n");
  158.         fprintf(stdout,"\n");
  159.         fprintf(stdout,"-h           : display the help information \n ");
  160.         fprintf(stdout,"\n");
  161.         fprintf(stdout,"-cinema2K    : Digital Cinema 2K profile compliant codestream for 2K resolution.(-cinema2k 24 or 48) \n");
  162.         fprintf(stdout,"          Need to specify the frames per second for a 2K resolution. Only 24 or 48 fps is allowed\n");
  163.         fprintf(stdout,"\n");
  164.         fprintf(stdout,"-cinema4K    : Digital Cinema 4K profile compliant codestream for 4K resolution \n");
  165.         fprintf(stdout,"          Frames per second not required. Default value is 24fps\n");
  166.         fprintf(stdout,"\n");
  167.         fprintf(stdout,"-r           : different compression ratios for successive layers (-r 20,10,5)\n ");
  168.         fprintf(stdout,"                 - The rate specified for each quality level is the desired \n");
  169.         fprintf(stdout,"                   compression factor.\n");
  170.         fprintf(stdout,"                   Example: -r 20,10,1 means quality 1: compress 20x, \n");
  171.         fprintf(stdout,"                     quality 2: compress 10x and quality 3: compress lossless\n");
  172.         fprintf(stdout,"\n");
  173.         fprintf(stdout,"               (options -r and -q cannot be used together)\n ");
  174.         fprintf(stdout,"\n");
  175.  
  176.         fprintf(stdout,"-q           : different psnr for successive layers (-q 30,40,50) \n ");
  177.  
  178.         fprintf(stdout,"               (options -r and -q cannot be used together)\n ");
  179.  
  180.         fprintf(stdout,"\n");
  181.         fprintf(stdout,"-n           : number of resolutions (-n 3) \n");
  182.         fprintf(stdout,"\n");
  183.         fprintf(stdout,"-b           : size of code block (-b 32,32) \n");
  184.         fprintf(stdout,"\n");
  185.         fprintf(stdout,"-c           : size of precinct (-c 128,128) \n");
  186.         fprintf(stdout,"\n");
  187.         fprintf(stdout,"-t           : size of tile (-t 512,512) \n");
  188.         fprintf(stdout,"\n");
  189.         fprintf(stdout,"-p           : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
  190.         fprintf(stdout,"\n");
  191.         fprintf(stdout,"-s           : subsampling factor (-s 2,2) [-s X,Y] \n");
  192.         fprintf(stdout,"             Remark: subsampling bigger than 2 can produce error\n");
  193.         fprintf(stdout,"\n");
  194.         fprintf(stdout,"-POC         : Progression order change (-POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL) \n");
  195.         fprintf(stdout,"      Example: T1=0,0,1,5,3,CPRL \n");
  196.         fprintf(stdout,"                         : Ttilenumber=Resolution num start,Component num start,Layer num end,Resolution num end,Component num end,Progression order\n");
  197.         fprintf(stdout,"\n");
  198.         fprintf(stdout,"-SOP         : write SOP marker before each packet \n");
  199.         fprintf(stdout,"\n");
  200.         fprintf(stdout,"-EPH         : write EPH marker after each header packet \n");
  201.         fprintf(stdout,"\n");
  202.         fprintf(stdout,"-M           : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
  203.         fprintf(stdout,"                 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
  204.         fprintf(stdout,"                 Indicate multiple modes by adding their values. \n");
  205.         fprintf(stdout,"                 ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
  206.         fprintf(stdout,"\n");
  207.         fprintf(stdout,"-x           : create an index file *.Idx (-x index_name.Idx) \n");
  208.         fprintf(stdout,"\n");
  209.         fprintf(stdout,"-ROI         : c=%%d,U=%%d : quantization indices upshifted \n");
  210.         fprintf(stdout,"               for component c=%%d [%%d = 0,1,2]\n");
  211.         fprintf(stdout,"               with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI c=0,U=25) \n");
  212.         fprintf(stdout,"\n");
  213.         fprintf(stdout,"-d           : offset of the origin of the image (-d 150,300) \n");
  214.         fprintf(stdout,"\n");
  215.         fprintf(stdout,"-T           : offset of the origin of the tiles (-T 100,75) \n");
  216.         fprintf(stdout,"\n");
  217.         fprintf(stdout,"-I           : use the irreversible DWT 9-7 (-I) \n");
  218.         fprintf(stdout,"\n");
  219. /* UniPG>> */
  220. #ifdef USE_JPWL
  221.         fprintf(stdout,"-W           : adoption of JPWL (Part 11) capabilities (-W params)\n");
  222.         fprintf(stdout,"               The parameters can be written and repeated in any order:\n");
  223.         fprintf(stdout,"               [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
  224.         fprintf(stdout,"                ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
  225.         fprintf(stdout,"\n");
  226.         fprintf(stdout,"                 h selects the header error protection (EPB): 'type' can be\n");
  227.         fprintf(stdout,"                   [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
  228.         fprintf(stdout,"                   if 'tilepart' is absent, it is for main and tile headers\n");
  229.         fprintf(stdout,"                   if 'tilepart' is present, it applies from that tile\n");
  230.         fprintf(stdout,"                     onwards, up to the next h<> spec, or to the last tilepart\n");
  231.         fprintf(stdout,"                     in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS);
  232.         fprintf(stdout,"\n");
  233.         fprintf(stdout,"                 p selects the packet error protection (EEP/UEP with EPBs)\n");
  234.         fprintf(stdout,"                  to be applied to raw data: 'type' can be\n");
  235.         fprintf(stdout,"                   [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
  236.         fprintf(stdout,"                   if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
  237.         fprintf(stdout,"                   if 'tilepart:pack' is present, it applies from that tile\n");
  238.         fprintf(stdout,"                     and that packet onwards, up to the next packet spec\n");
  239.         fprintf(stdout,"                     or to the last packet in the last tilepart in the stream\n");
  240.         fprintf(stdout,"                     (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS);
  241.         fprintf(stdout,"\n");
  242.         fprintf(stdout,"                 s enables sensitivity data insertion (ESD): 'method' can be\n");
  243.         fprintf(stdout,"                   [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
  244.         fprintf(stdout,"                    4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
  245.         fprintf(stdout,"                   if 'tilepart' is absent, it is for main header only\n");
  246.         fprintf(stdout,"                   if 'tilepart' is present, it applies from that tile\n");
  247.         fprintf(stdout,"                     onwards, up to the next s<> spec, or to the last tilepart\n");
  248.         fprintf(stdout,"                     in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS);
  249.         fprintf(stdout,"\n");
  250.         fprintf(stdout,"                 g determines the addressing mode: <range> can be\n");
  251.         fprintf(stdout,"                   [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
  252.         fprintf(stdout,"\n");
  253.         fprintf(stdout,"                 a determines the size of data addressing: <addr> can be\n");
  254.         fprintf(stdout,"                   2/4 bytes (small/large codestreams). If not set, auto-mode\n");
  255.         fprintf(stdout,"\n");
  256.         fprintf(stdout,"                 z determines the size of sensitivity values: <size> can be\n");
  257.         fprintf(stdout,"                   1/2 bytes, for the transformed pseudo-floating point value\n");
  258.         fprintf(stdout,"\n");
  259.         fprintf(stdout,"                 ex.:\n");
  260.         fprintf(stdout,"                   h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
  261.         fprintf(stdout,"                     s0=6,s3=-1,a=0,g=1,z=1\n");
  262.         fprintf(stdout,"                 means\n");
  263.         fprintf(stdout,"                   predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
  264.         fprintf(stdout,"                   CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
  265.         fprintf(stdout,"                   UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
  266.         fprintf(stdout,"                   UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
  267.         fprintf(stdout,"                   UEP rs default for packets of tilepart 1,\n");
  268.         fprintf(stdout,"                   no UEP for packets 0 to 19 of tilepart 3,\n");
  269.         fprintf(stdout,"                   UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
  270.         fprintf(stdout,"                   relative sensitivity ESD for MH,\n");
  271.         fprintf(stdout,"                   TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
  272.         fprintf(stdout,"                   size of addresses and 1 byte for each sensitivity value\n");
  273.         fprintf(stdout,"\n");
  274.         fprintf(stdout,"                 ex.:\n");
  275.         fprintf(stdout,"                       h,s,p\n");
  276.         fprintf(stdout,"                 means\n");
  277.         fprintf(stdout,"                   default protection to headers (MH and TPHs) as well as\n");
  278.         fprintf(stdout,"                   data packets, one ESD in MH\n");
  279.         fprintf(stdout,"\n");
  280.         fprintf(stdout,"                 N.B.: use the following recommendations when specifying\n");
  281.         fprintf(stdout,"                       the JPWL parameters list\n");
  282.         fprintf(stdout,"                   - when you use UEP, always pair the 'p' option with 'h'\n");
  283.         fprintf(stdout,"                 \n");
  284. #endif /* USE_JPWL */
  285. /* <<UniPG */
  286.         fprintf(stdout,"IMPORTANT:\n");
  287.         fprintf(stdout,"-----------\n");
  288.         fprintf(stdout,"\n");
  289.         fprintf(stdout,"The index file has the structure below:\n");
  290.         fprintf(stdout,"---------------------------------------\n");
  291.         fprintf(stdout,"\n");
  292.         fprintf(stdout,"Image_height Image_width\n");
  293.         fprintf(stdout,"progression order\n");
  294.         fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
  295.         fprintf(stdout,"Tiles_nb_X Tiles_nb_Y\n");
  296.         fprintf(stdout,"Components_nb\n");
  297.         fprintf(stdout,"Layers_nb\n");
  298.         fprintf(stdout,"decomposition_levels\n");
  299.         fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
  300.         fprintf(stdout,"   [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
  301.         fprintf(stdout,"Main_header_start_position\n");
  302.         fprintf(stdout,"Main_header_end_position\n");
  303.         fprintf(stdout,"Codestream_size\n");
  304.         fprintf(stdout,"\n");
  305.         fprintf(stdout,"INFO ON TILES\n");
  306.         fprintf(stdout,"tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
  307.         fprintf(stdout,"Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
  308.         fprintf(stdout,"Tile_1   ''           ''        ''        ''       ''    ''      ''\n");
  309.         fprintf(stdout,"...\n");
  310.         fprintf(stdout,"Tile_Nt   ''           ''        ''        ''       ''    ''     ''\n");
  311.         fprintf(stdout,"...\n");
  312.         fprintf(stdout,"TILE 0 DETAILS\n");
  313.         fprintf(stdout,"part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
  314.         fprintf(stdout,"...\n");
  315.         fprintf(stdout,"Progression_string\n");
  316.         fprintf(stdout,"pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
  317.         fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
  318.         fprintf(stdout,"...\n");
  319.         fprintf(stdout,"Tpacket_Np ''   ''    ''   ''    ''       ''       ''     ''\n");
  320.  
  321.         fprintf(stdout,"MaxDisto\n");
  322.  
  323.         fprintf(stdout,"TotalDisto\n\n");
  324. }
  325.  
  326.  
  327. OPJ_PROG_ORDER give_progression(char progression[4]) {
  328.         if(strncmp(progression, "LRCP", 4) == 0) {
  329.                 return LRCP;
  330.         }
  331.         if(strncmp(progression, "RLCP", 4) == 0) {
  332.                 return RLCP;
  333.         }
  334.         if(strncmp(progression, "RPCL", 4) == 0) {
  335.                 return RPCL;
  336.         }
  337.         if(strncmp(progression, "PCRL", 4) == 0) {
  338.                 return PCRL;
  339.         }
  340.         if(strncmp(progression, "CPRL", 4) == 0) {
  341.                 return CPRL;
  342.         }
  343.  
  344.         return PROG_UNKNOWN;
  345. }
  346.  
  347.  
  348. /// <summary>
  349. /// Get logarithm of an integer and round downwards.
  350. /// </summary>
  351. int int_floorlog2(int a) {
  352.     int l;
  353.     for (l=0; a>1; l++) {
  354.         a>>=1;
  355.     }
  356.     return l;
  357. }
  358.  
  359. static int initialise_4K_poc(opj_poc_t *POC, int numres){
  360.         POC[0].tile  = 1;
  361.         POC[0].resno0  = 0;
  362.         POC[0].compno0 = 0;
  363.         POC[0].layno1  = 1;
  364.         POC[0].resno1  = numres-1;
  365.         POC[0].compno1 = 3;
  366.         POC[0].prg1 = CPRL;
  367.         POC[1].tile  = 1;
  368.         POC[1].resno0  = numres-1;
  369.         POC[1].compno0 = 0;
  370.         POC[1].layno1  = 1;
  371.         POC[1].resno1  = numres;
  372.         POC[1].compno1 = 3;
  373.         POC[1].prg1 = CPRL;
  374.         return 2;
  375. }
  376.  
  377. void cinema_parameters(opj_cparameters_t *parameters){
  378.         parameters->tile_size_on = false;
  379.         parameters->cp_tdx=1;
  380.         parameters->cp_tdy=1;
  381.        
  382.         /*Tile part*/
  383.         parameters->tp_flag = 'C';
  384.         parameters->tp_on = 1;
  385.  
  386.         /*Tile and Image shall be at (0,0)*/
  387.         parameters->cp_tx0 = 0;
  388.         parameters->cp_ty0 = 0;
  389.         parameters->image_offset_x0 = 0;
  390.         parameters->image_offset_y0 = 0;
  391.  
  392.         /*Codeblock size= 32*32*/
  393.         parameters->cblockw_init = 32; 
  394.         parameters->cblockh_init = 32;
  395.         parameters->csty |= 0x01;
  396.  
  397.         /*The progression order shall be CPRL*/
  398.         parameters->prog_order = CPRL;
  399.  
  400.         /* No ROI */
  401.         parameters->roi_compno = -1;
  402.  
  403.         parameters->subsampling_dx = 1;         parameters->subsampling_dy = 1;
  404.  
  405.         /* 9-7 transform */
  406.         parameters->irreversible = 1;
  407.  
  408. }
  409.  
  410. void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_fol_t *img_fol){
  411.         int i;
  412.         float temp_rate;
  413.         opj_poc_t *POC = NULL;
  414.  
  415.         switch (parameters->cp_cinema){
  416.         case CINEMA2K_24:
  417.         case CINEMA2K_48:
  418.                 if(parameters->numresolution > 6){
  419.                         parameters->numresolution = 6;
  420.                 }
  421.                 if (!((image->comps[0].w == 2048) | (image->comps[0].h == 1080))){
  422.                         fprintf(stdout,"Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3"
  423.                                 "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
  424.                                 image->comps[0].w,image->comps[0].h);
  425.                         parameters->cp_rsiz = STD_RSIZ;
  426.                 }
  427.         break;
  428.        
  429.         case CINEMA4K_24:
  430.                 if(parameters->numresolution < 1){
  431.                                 parameters->numresolution = 1;
  432.                         }else if(parameters->numresolution > 7){
  433.                                 parameters->numresolution = 7;
  434.                         }
  435.                 if (!((image->comps[0].w == 4096) | (image->comps[0].h == 2160))){
  436.                         fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
  437.                                 "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
  438.                                 image->comps[0].w,image->comps[0].h);
  439.                         parameters->cp_rsiz = STD_RSIZ;
  440.                 }
  441.                 parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution);
  442.                 break;
  443.         }
  444.  
  445.         switch (parameters->cp_cinema){
  446.                 case CINEMA2K_24:
  447.                 case CINEMA4K_24:
  448.                         for(i=0 ; i<parameters->tcp_numlayers ; i++){
  449.                                 temp_rate = 0 ;
  450.                                 if (img_fol->rates[i]== 0){
  451.                                         parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
  452.                                         (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  453.                                 }else{
  454.                                         temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
  455.                                                 (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
  456.                                         if (temp_rate > CINEMA_24_CS ){
  457.                                                 parameters->tcp_rates[i]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
  458.                                                 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  459.                                         }else{
  460.                                                 parameters->tcp_rates[i]= img_fol->rates[i];
  461.                                         }
  462.                                 }
  463.                         }
  464.                         parameters->max_comp_size = COMP_24_CS;
  465.                         break;
  466.                
  467.                 case CINEMA2K_48:
  468.                         for(i=0 ; i<parameters->tcp_numlayers ; i++){
  469.                                 temp_rate = 0 ;
  470.                                 if (img_fol->rates[i]== 0){
  471.                                         parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
  472.                                         (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  473.                                 }else{
  474.                                         temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
  475.                                                 (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
  476.                                         if (temp_rate > CINEMA_48_CS ){
  477.                                                 parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
  478.                                                 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  479.                                         }else{
  480.                                                 parameters->tcp_rates[i]= img_fol->rates[i];
  481.                                         }
  482.                                 }
  483.                         }
  484.                         parameters->max_comp_size = COMP_48_CS;
  485.                         break;
  486.         }
  487.         parameters->cp_disto_alloc = 1;
  488. }
  489.  
  490.  
  491. /* ------------------------------------------------------------------------------------ */
  492. int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
  493.                                                                                                         img_fol_t *img_fol, char *indexfilename) {
  494.         int i, j,totlen;
  495.         option_t long_option[]={
  496.                 {"cinema2K",REQ_ARG, NULL ,'w'},
  497.                 {"cinema4K",NO_ARG, NULL ,'y'},
  498.                 {"ImgDir",REQ_ARG, NULL ,'z'},
  499.                 {"TP",REQ_ARG, NULL ,'v'},
  500.                 {"SOP",NO_ARG, NULL ,'S'},
  501.                 {"EPH",NO_ARG, NULL ,'E'},
  502.                 {"OutFor",REQ_ARG, NULL ,'O'},
  503.                 {"POC",REQ_ARG, NULL ,'P'},
  504.                 {"ROI",REQ_ARG, NULL ,'R'},
  505.         };
  506.  
  507.         /* parse the command line */
  508. /* UniPG>> */
  509.         const char optlist[] = "i:o:hr:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:"
  510. #ifdef USE_JPWL
  511.                 "W:"
  512. #endif /* USE_JPWL */
  513.                 ;
  514.  
  515.         /*printf("C: parse_cmdline_encoder:");
  516.         for (i=0; i<argc; i++) {
  517.                 printf("[%s]",argv[i]);
  518.         }
  519.         printf("\n");*/
  520.  
  521.         totlen=sizeof(long_option);
  522.         img_fol->set_out_format=0;
  523.         reset_options_reading();
  524.  
  525.         while (1) {
  526.     int c = getopt_long(argc, argv, optlist,long_option,totlen);
  527.                 if (c == -1)
  528.                         break;
  529.                 switch (c) {
  530.  
  531.                                 /* ----------------------------------------------------- */
  532.  
  533.                         case 'o':                       /* output file */
  534.                         {
  535.                                 char *outfile = optarg;
  536.                                 parameters->cod_format = get_file_format(outfile);
  537.                                 switch(parameters->cod_format) {
  538.                                         case J2K_CFMT:
  539.                                         case JP2_CFMT:
  540.                                                 break;
  541.                                         default:
  542.                                                 fprintf(stderr, "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
  543.                                                 return 1;
  544.                                 }
  545.                                 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
  546.                         }
  547.                         break;
  548.  
  549.                                 /* ----------------------------------------------------- */
  550.                         case 'O':                       /* output format */
  551.                                 {
  552.                                         char outformat[50];
  553.                                         char *of = optarg;
  554.                                         sprintf(outformat,".%s",of);
  555.                                         img_fol->set_out_format = 1;
  556.                                         parameters->cod_format = get_file_format(outformat);
  557.                                         switch(parameters->cod_format) {
  558.                                                 case J2K_CFMT:
  559.                                                 case JP2_CFMT:
  560.                                                         img_fol->out_format = optarg;
  561.                                                         break;
  562.                                                 default:
  563.                                                         fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n");
  564.                                                         return 1;
  565.                                         }
  566.                                 }
  567.                                 break;
  568.  
  569.  
  570.                                 /* ----------------------------------------------------- */
  571.  
  572.  
  573.                         case 'r':                       /* rates rates/distorsion */
  574.                         {
  575.                                 char *s = optarg;
  576.                                 while (sscanf(s, "%f", &parameters->tcp_rates[parameters->tcp_numlayers]) == 1) {
  577.                                         parameters->tcp_numlayers++;
  578.                                         while (*s && *s != ',') {
  579.                                                 s++;
  580.                                         }
  581.                                         if (!*s)
  582.                                                 break;
  583.                                         s++;
  584.                                 }
  585.                                 parameters->cp_disto_alloc = 1;
  586.                         }
  587.                         break;
  588.  
  589.                                 /* ----------------------------------------------------- */
  590.  
  591.                         case 'q':                       /* add fixed_quality */
  592.                         {
  593.                                 char *s = optarg;
  594.                                 while (sscanf(s, "%f", &parameters->tcp_distoratio[parameters->tcp_numlayers]) == 1) {
  595.                                         parameters->tcp_numlayers++;
  596.                                         while (*s && *s != ',') {
  597.                                                 s++;
  598.                                         }
  599.                                         if (!*s)
  600.                                                 break;
  601.                                         s++;
  602.                                 }
  603.                                 parameters->cp_fixed_quality = 1;
  604.                         }
  605.                         break;
  606.  
  607.                                 /* dda */
  608.                                 /* ----------------------------------------------------- */
  609.  
  610.                         case 'f':                       /* mod fixed_quality (before : -q) */
  611.                         {
  612.                                 int *row = NULL, *col = NULL;
  613.                                 int numlayers = 0, numresolution = 0, matrix_width = 0;
  614.  
  615.                                 char *s = optarg;
  616.                                 sscanf(s, "%d", &numlayers);
  617.                                 s++;
  618.                                 if (numlayers > 9)
  619.                                         s++;
  620.  
  621.                                 parameters->tcp_numlayers = numlayers;
  622.                                 numresolution = parameters->numresolution;
  623.                                 matrix_width = numresolution * 3;
  624.                                 parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
  625.                                 s = s + 2;
  626.  
  627.                                 for (i = 0; i < numlayers; i++) {
  628.                                         row = &parameters->cp_matrice[i * matrix_width];
  629.                                         col = row;
  630.                                         parameters->tcp_rates[i] = 1;
  631.                                         sscanf(s, "%d,", &col[0]);
  632.                                         s += 2;
  633.                                         if (col[0] > 9)
  634.                                                 s++;
  635.                                         col[1] = 0;
  636.                                         col[2] = 0;
  637.                                         for (j = 1; j < numresolution; j++) {
  638.                                                 col += 3;
  639.                                                 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
  640.                                                 s += 6;
  641.                                                 if (col[0] > 9)
  642.                                                         s++;
  643.                                                 if (col[1] > 9)
  644.                                                         s++;
  645.                                                 if (col[2] > 9)
  646.                                                         s++;
  647.                                         }
  648.                                         if (i < numlayers - 1)
  649.                                                 s++;
  650.                                 }
  651.                                 parameters->cp_fixed_alloc = 1;
  652.                         }
  653.                         break;
  654.  
  655.                                 /* ----------------------------------------------------- */
  656.  
  657.                         case 't':                       /* tiles */
  658.                         {
  659.                                 sscanf(optarg, "%d,%d", &parameters->cp_tdx, &parameters->cp_tdy);
  660.                                 parameters->tile_size_on = true;
  661.                         }
  662.                         break;
  663.  
  664.                                 /* ----------------------------------------------------- */
  665.  
  666.                         case 'n':                       /* resolution */
  667.                         {
  668.                                 sscanf(optarg, "%d", &parameters->numresolution);
  669.                         }
  670.                         break;
  671.  
  672.                                 /* ----------------------------------------------------- */
  673.                         case 'c':                       /* precinct dimension */
  674.                         {
  675.                                 char sep;
  676.                                 int res_spec = 0;
  677.  
  678.                                 char *s = optarg;
  679.                                 do {
  680.                                         sep = 0;
  681.                                         sscanf(s, "[%d,%d]%c", &parameters->prcw_init[res_spec],
  682.                                  &parameters->prch_init[res_spec], &sep);
  683.                                         parameters->csty |= 0x01;
  684.                                         res_spec++;
  685.                                         s = strpbrk(s, "]") + 2;
  686.                                 }
  687.                                 while (sep == ',');
  688.                                 parameters->res_spec = res_spec;
  689.                         }
  690.                         break;
  691.  
  692.                                 /* ----------------------------------------------------- */
  693.  
  694.                         case 'b':                       /* code-block dimension */
  695.                         {
  696.                                 int cblockw_init = 0, cblockh_init = 0;
  697.                                 sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
  698.                                 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
  699.                                         || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
  700.                                         fprintf(stderr,
  701.                                                 "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
  702.             "    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
  703.                                         return 1;
  704.                                 }
  705.                                 parameters->cblockw_init = cblockw_init;
  706.                                 parameters->cblockh_init = cblockh_init;
  707.                         }
  708.                         break;
  709.  
  710.                                 /* ----------------------------------------------------- */
  711.  
  712.                         case 'x':                       /* creation of index file */
  713.                         {
  714.                                 char *index = optarg;
  715.                                 strncpy(indexfilename, index, OPJ_PATH_LEN);
  716.                         }
  717.                         break;
  718.  
  719.                                 /* ----------------------------------------------------- */
  720.  
  721.                         case 'p':                       /* progression order */
  722.                         {
  723.                                 char progression[4];
  724.  
  725.                                 strncpy(progression, optarg, 4);
  726.                                 parameters->prog_order = give_progression(progression);
  727.                                 if (parameters->prog_order == -1) {
  728.                                         fprintf(stderr, "Unrecognized progression order "
  729.             "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
  730.                                         return 1;
  731.                                 }
  732.                         }
  733.                         break;
  734.  
  735.                                 /* ----------------------------------------------------- */
  736.  
  737.                         case 's':                       /* subsampling factor */
  738.                         {
  739.                                 if (sscanf(optarg, "%d,%d", &parameters->subsampling_dx,
  740.                                     &parameters->subsampling_dy) != 2) {
  741.                                         fprintf(stderr, "'-s' sub-sampling argument error !  [-s dx,dy]\n");
  742.                                         return 1;
  743.                                 }
  744.                         }
  745.                         break;
  746.  
  747.                                 /* ----------------------------------------------------- */
  748.  
  749.                         case 'd':                       /* coordonnate of the reference grid */
  750.                         {
  751.                                 if (sscanf(optarg, "%d,%d", &parameters->image_offset_x0,
  752.                                     &parameters->image_offset_y0) != 2) {
  753.                                         fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
  754.             "error !! [-d x0,y0]\n");
  755.                                         return 1;
  756.                                 }
  757.                         }
  758.                         break;
  759.  
  760.                                 /* ----------------------------------------------------- */
  761.  
  762.                         case 'h':                       /* display an help description */
  763.                                 encode_help_display();
  764.                                 return 1;
  765.  
  766.                                 /* ----------------------------------------------------- */
  767.  
  768.                         case 'P':                       /* POC */
  769.                         {
  770.                                 int numpocs = 0;                /* number of progression order change (POC) default 0 */
  771.                                 opj_poc_t *POC = NULL;  /* POC : used in case of Progression order change */
  772.  
  773.                                 char *s = optarg;
  774.                                 POC = parameters->POC;
  775.  
  776.                                 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile,
  777.                                         &POC[numpocs].resno0, &POC[numpocs].compno0,
  778.                                         &POC[numpocs].layno1, &POC[numpocs].resno1,
  779.                                         &POC[numpocs].compno1, &POC[numpocs].progorder) == 7) {
  780.                                         POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
  781.                                         numpocs++;
  782.                                         while (*s && *s != '/') {
  783.                                                 s++;
  784.                                         }
  785.                                         if (!*s) {
  786.                                                 break;
  787.                                         }
  788.                                         s++;
  789.                                 }
  790.                                 parameters->numpocs = numpocs;
  791.                         }
  792.                         break;
  793.  
  794.                                 /* ------------------------------------------------------ */
  795.  
  796.                         case 'S':                       /* SOP marker */
  797.                         {
  798.                                 parameters->csty |= 0x02;
  799.                         }
  800.                         break;
  801.  
  802.                                 /* ------------------------------------------------------ */
  803.  
  804.                         case 'E':                       /* EPH marker */
  805.                         {
  806.                                 parameters->csty |= 0x04;
  807.                         }
  808.                         break;
  809.  
  810.                                 /* ------------------------------------------------------ */
  811.  
  812.                         case 'M':                       /* Mode switch pas tous au point !! */
  813.                         {
  814.                                 int value = 0;
  815.                                 if (sscanf(optarg, "%d", &value) == 1) {
  816.                                         for (i = 0; i <= 5; i++) {
  817.                                                 int cache = value & (1 << i);
  818.                                                 if (cache)
  819.                                                         parameters->mode |= (1 << i);
  820.                                         }
  821.                                 }
  822.                         }
  823.                         break;
  824.  
  825.                                 /* ------------------------------------------------------ */
  826.  
  827.                         case 'R':                       /* ROI */
  828.                         {
  829.                                 if (sscanf(optarg, "c=%d,U=%d", &parameters->roi_compno,
  830.                                            &parameters->roi_shift) != 2) {
  831.                                         fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n");
  832.                                         return 1;
  833.                                 }
  834.                         }
  835.                         break;
  836.  
  837.                                 /* ------------------------------------------------------ */
  838.  
  839.                         case 'T':                       /* Tile offset */
  840.                         {
  841.                                 if (sscanf(optarg, "%d,%d", &parameters->cp_tx0, &parameters->cp_ty0) != 2) {
  842.                                         fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
  843.                                         return 1;
  844.                                 }
  845.                         }
  846.                         break;
  847.  
  848.                                 /* ------------------------------------------------------ */
  849.  
  850.                         case 'C':                       /* add a comment */
  851.                         {
  852.                                 parameters->cp_comment = (char*)malloc(strlen(optarg) + 1);
  853.                                 if(parameters->cp_comment) {
  854.                                         strcpy(parameters->cp_comment, optarg);
  855.                                 }
  856.                         }
  857.                         break;
  858.  
  859.  
  860.                                 /* ------------------------------------------------------ */
  861.  
  862.                         case 'I':                       /* reversible or not */
  863.                         {
  864.                                 parameters->irreversible = 1;
  865.                         }
  866.                         break;
  867.  
  868.                         /* ------------------------------------------------------ */
  869.                        
  870.                         case 'v':                       /* Tile part generation*/
  871.                         {
  872.                                 parameters->tp_flag = optarg[0];
  873.                                 parameters->tp_on = 1;
  874.                         }
  875.                         break; 
  876.  
  877.                                 /* ------------------------------------------------------ */
  878.                        
  879.                         case 'z':                       /* Image Directory path */
  880.                         {
  881.                                 img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
  882.                                 strcpy(img_fol->imgdirpath,optarg);
  883.                                 img_fol->set_imgdir=1;
  884.                         }
  885.                         break;
  886.  
  887.                                 /* ------------------------------------------------------ */
  888.                        
  889.                         case 'w':                       /* Digital Cinema 2K profile compliance*/
  890.                         {
  891.                                 int fps=0;
  892.                                 sscanf(optarg,"%d",&fps);
  893.                                 if(fps == 24){
  894.                                         parameters->cp_cinema = CINEMA2K_24;
  895.                                 }else if(fps == 48 ){
  896.                                         parameters->cp_cinema = CINEMA2K_48;
  897.                                 }else {
  898.                                         fprintf(stderr,"Incorrect value!! must be 24 or 48\n");
  899.                                         return 1;
  900.                                 }
  901.                                 fprintf(stdout,"CINEMA 2K compliant codestream\n");
  902.                                 parameters->cp_rsiz = CINEMA2K;
  903.                                
  904.                         }
  905.                         break;
  906.                                
  907.                                 /* ------------------------------------------------------ */
  908.                        
  909.                         case 'y':                       /* Digital Cinema 4K profile compliance*/
  910.                         {
  911.                                 parameters->cp_cinema = CINEMA4K_24;
  912.                                 fprintf(stdout,"CINEMA 4K compliant codestream\n");
  913.                                 parameters->cp_rsiz = CINEMA4K;
  914.                         }
  915.                         break;
  916.                                
  917.                                 /* ------------------------------------------------------ */
  918.  
  919. /* UniPG>> */
  920. #ifdef USE_JPWL
  921.                                 /* ------------------------------------------------------ */
  922.                        
  923.                         case 'W':                       /* JPWL capabilities switched on */
  924.                         {
  925.                                 char *token = NULL;
  926.                                 int hprot, pprot, sens, addr, size, range;
  927.  
  928.                                 /* we need to enable indexing */
  929.                                 if (!indexfilename) {
  930.                                         strncpy(indexfilename, JPWL_PRIVATEINDEX_NAME, OPJ_PATH_LEN);
  931.                                 }
  932.  
  933.                                 /* search for different protection methods */
  934.  
  935.                                 /* break the option in comma points and parse the result */
  936.                                 token = strtok(optarg, ",");
  937.                                 while(token != NULL) {
  938.  
  939.                                         /* search header error protection method */
  940.                                         if (*token == 'h') {
  941.  
  942.                                                 static int tile = 0, tilespec = 0, lasttileno = 0;
  943.  
  944.                                                 hprot = 1; /* predefined method */
  945.  
  946.                                                 if(sscanf(token, "h=%d", &hprot) == 1) {
  947.                                                         /* Main header, specified */
  948.                                                         if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
  949.                                                                 ((hprot >= 37) && (hprot <= 128)))) {
  950.                                                                 fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n", hprot);
  951.                                                                 return 1;
  952.                                                         }
  953.                                                         parameters->jpwl_hprot_MH = hprot;
  954.  
  955.                                                 } else if(sscanf(token, "h%d=%d", &tile, &hprot) == 2) {
  956.                                                         /* Tile part header, specified */
  957.                                                         if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
  958.                                                                 ((hprot >= 37) && (hprot <= 128)))) {
  959.                                                                 fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n", hprot);
  960.                                                                 return 1;
  961.                                                         }
  962.                                                         if (tile < 0) {
  963.                                                                 fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile);
  964.                                                                 return 1;
  965.                                                         }
  966.                                                         if (tilespec < JPWL_MAX_NO_TILESPECS) {
  967.                                                                 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
  968.                                                                 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
  969.                                                         }
  970.  
  971.                                                 } else if(sscanf(token, "h%d", &tile) == 1) {
  972.                                                         /* Tile part header, unspecified */
  973.                                                         if (tile < 0) {
  974.                                                                 fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile);
  975.                                                                 return 1;
  976.                                                         }
  977.                                                         if (tilespec < JPWL_MAX_NO_TILESPECS) {
  978.                                                                 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
  979.                                                                 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
  980.                                                         }
  981.  
  982.  
  983.                                                 } else if (!strcmp(token, "h")) {
  984.                                                         /* Main header, unspecified */
  985.                                                         parameters->jpwl_hprot_MH = hprot;
  986.  
  987.                                                 } else {
  988.                                                         fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
  989.                                                         return 1;
  990.                                                 };
  991.  
  992.                                         }
  993.  
  994.                                         /* search packet error protection method */
  995.                                         if (*token == 'p') {
  996.  
  997.                                                 static int pack = 0, tile = 0, packspec = 0, lastpackno = 0;
  998.  
  999.                                                 pprot = 1; /* predefined method */
  1000.  
  1001.                                                 if (sscanf(token, "p=%d", &pprot) == 1) {
  1002.                                                         /* Method for all tiles and all packets */
  1003.                                                         if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
  1004.                                                                 ((pprot >= 37) && (pprot <= 128)))) {
  1005.                                                                 fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n", pprot);
  1006.                                                                 return 1;
  1007.                                                         }
  1008.                                                         parameters->jpwl_pprot_tileno[0] = 0;
  1009.                                                         parameters->jpwl_pprot_packno[0] = 0;
  1010.                                                         parameters->jpwl_pprot[0] = pprot;
  1011.  
  1012.                                                 } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) {
  1013.                                                         /* method specified from that tile on */
  1014.                                                         if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
  1015.                                                                 ((pprot >= 37) && (pprot <= 128)))) {
  1016.                                                                 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
  1017.                                                                 return 1;
  1018.                                                         }
  1019.                                                         if (tile < 0) {
  1020.                                                                 fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
  1021.                                                                 return 1;
  1022.                                                         }
  1023.                                                         if (packspec < JPWL_MAX_NO_PACKSPECS) {
  1024.                                                                 parameters->jpwl_pprot_tileno[packspec] = tile;
  1025.                                                                 parameters->jpwl_pprot_packno[packspec] = 0;
  1026.                                                                 parameters->jpwl_pprot[packspec++] = pprot;
  1027.                                                         }
  1028.  
  1029.                                                 } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) {
  1030.                                                         /* method fully specified from that tile and that packet on */
  1031.                                                         if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
  1032.                                                                 ((pprot >= 37) && (pprot <= 128)))) {
  1033.                                                                 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
  1034.                                                                 return 1;
  1035.                                                         }
  1036.                                                         if (tile < 0) {
  1037.                                                                 fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
  1038.                                                                 return 1;
  1039.                                                         }
  1040.                                                         if (pack < 0) {
  1041.                                                                 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack);
  1042.                                                                 return 1;
  1043.                                                         }
  1044.                                                         if (packspec < JPWL_MAX_NO_PACKSPECS) {
  1045.                                                                 parameters->jpwl_pprot_tileno[packspec] = tile;
  1046.                                                                 parameters->jpwl_pprot_packno[packspec] = pack;
  1047.                                                                 parameters->jpwl_pprot[packspec++] = pprot;
  1048.                                                         }
  1049.  
  1050.                                                 } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) {
  1051.                                                         /* default method from that tile and that packet on */
  1052.                                                         if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
  1053.                                                                 ((pprot >= 37) && (pprot <= 128)))) {
  1054.                                                                 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
  1055.                                                                 return 1;
  1056.                                                         }
  1057.                                                         if (tile < 0) {
  1058.                                                                 fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
  1059.                                                                 return 1;
  1060.                                                         }
  1061.                                                         if (pack < 0) {
  1062.                                                                 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack);
  1063.                                                                 return 1;
  1064.                                                         }
  1065.                                                         if (packspec < JPWL_MAX_NO_PACKSPECS) {
  1066.                                                                 parameters->jpwl_pprot_tileno[packspec] = tile;
  1067.                                                                 parameters->jpwl_pprot_packno[packspec] = pack;
  1068.                                                                 parameters->jpwl_pprot[packspec++] = pprot;
  1069.                                                         }
  1070.  
  1071.                                                 } else if (sscanf(token, "p%d", &tile) == 1) {
  1072.                                                         /* default from a tile on */
  1073.                                                         if (tile < 0) {
  1074.                                                                 fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
  1075.                                                                 return 1;
  1076.                                                         }
  1077.                                                         if (packspec < JPWL_MAX_NO_PACKSPECS) {
  1078.                                                                 parameters->jpwl_pprot_tileno[packspec] = tile;
  1079.                                                                 parameters->jpwl_pprot_packno[packspec] = 0;
  1080.                                                                 parameters->jpwl_pprot[packspec++] = pprot;
  1081.                                                         }
  1082.  
  1083.  
  1084.                                                 } else if (!strcmp(token, "p")) {
  1085.                                                         /* all default */
  1086.                                                         parameters->jpwl_pprot_tileno[0] = 0;
  1087.                                                         parameters->jpwl_pprot_packno[0] = 0;
  1088.                                                         parameters->jpwl_pprot[0] = pprot;
  1089.  
  1090.                                                 } else {
  1091.                                                         fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
  1092.                                                         return 1;
  1093.                                                 };
  1094.  
  1095.                                         }
  1096.  
  1097.                                         /* search sensitivity method */
  1098.                                         if (*token == 's') {
  1099.  
  1100.                                                 static int tile = 0, tilespec = 0, lasttileno = 0;
  1101.  
  1102.                                                 sens = 0; /* predefined: relative error */
  1103.  
  1104.                                                 if(sscanf(token, "s=%d", &sens) == 1) {
  1105.                                                         /* Main header, specified */
  1106.                                                         if ((sens < -1) || (sens > 7)) {
  1107.                                                                 fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n", sens);
  1108.                                                                 return 1;
  1109.                                                         }
  1110.                                                         parameters->jpwl_sens_MH = sens;
  1111.  
  1112.                                                 } else if(sscanf(token, "s%d=%d", &tile, &sens) == 2) {
  1113.                                                         /* Tile part header, specified */
  1114.                                                         if ((sens < -1) || (sens > 7)) {
  1115.                                                                 fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n", sens);
  1116.                                                                 return 1;
  1117.                                                         }
  1118.                                                         if (tile < 0) {
  1119.                                                                 fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
  1120.                                                                 return 1;
  1121.                                                         }
  1122.                                                         if (tilespec < JPWL_MAX_NO_TILESPECS) {
  1123.                                                                 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
  1124.                                                                 parameters->jpwl_sens_TPH[tilespec++] = sens;
  1125.                                                         }
  1126.  
  1127.                                                 } else if(sscanf(token, "s%d", &tile) == 1) {
  1128.                                                         /* Tile part header, unspecified */
  1129.                                                         if (tile < 0) {
  1130.                                                                 fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
  1131.                                                                 return 1;
  1132.                                                         }
  1133.                                                         if (tilespec < JPWL_MAX_NO_TILESPECS) {
  1134.                                                                 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
  1135.                                                                 parameters->jpwl_sens_TPH[tilespec++] = hprot;
  1136.                                                         }
  1137.  
  1138.                                                 } else if (!strcmp(token, "s")) {
  1139.                                                         /* Main header, unspecified */
  1140.                                                         parameters->jpwl_sens_MH = sens;
  1141.  
  1142.                                                 } else {
  1143.                                                         fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token);
  1144.                                                         return 1;
  1145.                                                 };
  1146.                                                
  1147.                                                 parameters->jpwl_sens_size = 2; /* 2 bytes for default size */
  1148.                                         }
  1149.  
  1150.                                         /* search addressing size */
  1151.                                         if (*token == 'a') {
  1152.  
  1153.                                                 static int tile = 0, tilespec = 0, lasttileno = 0;
  1154.  
  1155.                                                 addr = 0; /* predefined: auto */
  1156.  
  1157.                                                 if(sscanf(token, "a=%d", &addr) == 1) {
  1158.                                                         /* Specified */
  1159.                                                         if ((addr != 0) && (addr != 2) && (addr != 4)) {
  1160.                                                                 fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr);
  1161.                                                                 return 1;
  1162.                                                         }
  1163.                                                         parameters->jpwl_sens_addr = addr;
  1164.  
  1165.                                                 } else if (!strcmp(token, "a")) {
  1166.                                                         /* default */
  1167.                                                         parameters->jpwl_sens_addr = addr; /* auto for default size */
  1168.  
  1169.                                                 } else {
  1170.                                                         fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token);
  1171.                                                         return 1;
  1172.                                                 };
  1173.                                                
  1174.                                         }
  1175.  
  1176.                                         /* search sensitivity size */
  1177.                                         if (*token == 'z') {
  1178.  
  1179.                                                 static int tile = 0, tilespec = 0, lasttileno = 0;
  1180.  
  1181.                                                 size = 1; /* predefined: 1 byte */
  1182.  
  1183.                                                 if(sscanf(token, "z=%d", &size) == 1) {
  1184.                                                         /* Specified */
  1185.                                                         if ((size != 0) && (size != 1) && (size != 2)) {
  1186.                                                                 fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size);
  1187.                                                                 return 1;
  1188.                                                         }
  1189.                                                         parameters->jpwl_sens_size = size;
  1190.  
  1191.                                                 } else if (!strcmp(token, "a")) {
  1192.                                                         /* default */
  1193.                                                         parameters->jpwl_sens_size = size; /* 1 for default size */
  1194.  
  1195.                                                 } else {
  1196.                                                         fprintf(stderr, "ERROR -> invalid size selection = %s\n", token);
  1197.                                                         return 1;
  1198.                                                 };
  1199.                                                
  1200.                                         }
  1201.  
  1202.                                         /* search range method */
  1203.                                         if (*token == 'g') {
  1204.  
  1205.                                                 static int tile = 0, tilespec = 0, lasttileno = 0;
  1206.  
  1207.                                                 range = 0; /* predefined: 0 (packet) */
  1208.  
  1209.                                                 if(sscanf(token, "g=%d", &range) == 1) {
  1210.                                                         /* Specified */
  1211.                                                         if ((range < 0) || (range > 3)) {
  1212.                                                                 fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range);
  1213.                                                                 return 1;
  1214.                                                         }
  1215.                                                         parameters->jpwl_sens_range = range;
  1216.  
  1217.                                                 } else if (!strcmp(token, "g")) {
  1218.                                                         /* default */
  1219.                                                         parameters->jpwl_sens_range = range;
  1220.  
  1221.                                                 } else {
  1222.                                                         fprintf(stderr, "ERROR -> invalid range selection = %s\n", token);
  1223.                                                         return 1;
  1224.                                                 };
  1225.                                                
  1226.                                         }
  1227.  
  1228.                                         /* next token or bust */
  1229.                                         token = strtok(NULL, ",");
  1230.                                 };
  1231.  
  1232.  
  1233.                                 /* some info */
  1234.                                 fprintf(stdout, "Info: JPWL capabilities enabled\n");
  1235.                                 parameters->jpwl_epc_on = true;
  1236.  
  1237.                         }
  1238.                         break;
  1239. #endif /* USE_JPWL */
  1240. /* <<UniPG */
  1241.  
  1242.                                 /* ------------------------------------------------------ */
  1243.  
  1244.                         default:
  1245.                                 fprintf(stderr, "ERROR -> Command line not valid\n");
  1246.                                 return 1;
  1247.                 }
  1248.         }
  1249.  
  1250.         /* check for possible errors */
  1251.         if (parameters->cp_cinema){
  1252.                 if(parameters->tcp_numlayers > 1){
  1253.                         parameters->cp_rsiz = STD_RSIZ;
  1254.         fprintf(stdout,"Warning: DC profiles do not allow more than one quality layer. The codestream created will not be compliant with the DC profile\n");
  1255.                 }
  1256.         }
  1257.  
  1258.         if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality)
  1259.                 && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^ parameters->cp_fixed_quality))) {
  1260.                 fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n");
  1261.                 return 1;
  1262.         }                               /* mod fixed_quality */
  1263.  
  1264.         /* if no rate entered, lossless by default */
  1265.         if (parameters->tcp_numlayers == 0) {
  1266.                 parameters->tcp_rates[0] = 0;   /* MOD antonin : losslessbug */
  1267.                 parameters->tcp_numlayers++;
  1268.                 parameters->cp_disto_alloc = 1;
  1269.         }
  1270.  
  1271.         if((parameters->cp_tx0 > parameters->image_offset_x0) || (parameters->cp_ty0 > parameters->image_offset_y0)) {
  1272.                 fprintf(stderr,
  1273.                         "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
  1274.                         parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0, parameters->image_offset_y0);
  1275.                 return 1;
  1276.         }
  1277.  
  1278.         for (i = 0; i < parameters->numpocs; i++) {
  1279.                 if (parameters->POC[i].prg == -1) {
  1280.                         fprintf(stderr,
  1281.                                 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
  1282.                                 i + 1);
  1283.                 }
  1284.         }
  1285.  
  1286.         return 0;
  1287. }
  1288.  
  1289.  
  1290. /** 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.
  1291.   @param buffer_size, increased by the length of the compressed index, in number of bytes
  1292.   @return a pointer to a char[]
  1293.   Syntax of the index:
  1294.         one char for the version number (1): one byte because no problem with little endian, big endian etc.
  1295.         one int for each of the following informations:
  1296.                         Image Width    
  1297.                         Image Height   
  1298.                         progression order      
  1299.                         Tile width     
  1300.                         Tile height    
  1301.                         Nb tiles in X  
  1302.                         Nb tiles in Y  
  1303.                         Nb of components       
  1304.                         Nb of layers   
  1305.                         Nb of resolutions      
  1306.  
  1307.                         for each resolution:   
  1308.                                 Precinct width
  1309.                                 Precinct height
  1310.  
  1311.                         End main header position       
  1312.                         codestream size
  1313.  
  1314.                         For each tile: 
  1315.                                 tile number
  1316.                                 tile start pos in codestream
  1317.                                 tile header end position
  1318.                                 tile end position in codestream
  1319.  
  1320.                         For each LRCP, RLCP etc.:      
  1321.                                 packet number
  1322.                                 tile number
  1323.                                 layer number
  1324.                                 resolution number
  1325.                                 component number
  1326.                                 precinct number
  1327.                                 start position in the codestream
  1328.                                 end position of this packet
  1329.   */
  1330. char* create_index_into_byte_array(opj_codestream_info_t *cstr_info, int* buffer_size) {
  1331.         int tileno, compno, layno, resno, precno, pack_nb, x, y;
  1332.         char* buffer = NULL;
  1333.         int buffer_pos = 0;
  1334.         int prec_max = 0;
  1335.        
  1336.         prec_max = 0;
  1337.         for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
  1338.                 for (resno = 0; resno < cstr_info->numdecompos[0] + 1; resno++) {
  1339.                         prec_max = max(prec_max,cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]);
  1340.                 }
  1341.         }
  1342.  
  1343.         // Compute the size of the index buffer, in number of bytes*/
  1344.         *buffer_size =
  1345.                   1 /* version */
  1346.                 + (10 /* image_w until decomposition */
  1347.                 + (cstr_info->numdecompos[0]+1) * 2 /* pdx size for each tile */
  1348.                 + 2 /* main_head_end + codestream_size */
  1349.                 + cstr_info->tw * cstr_info->th * 4 /* tile info, without distorsion info */
  1350.                 + cstr_info->tw*cstr_info->th * cstr_info->numlayers * (cstr_info->numdecompos[0] + 1) * cstr_info->numcomps * prec_max *8
  1351.                   ) * sizeof(int);
  1352.         //printf("C: index buffer size = %d bytes\n", *buffer_size);
  1353.         buffer = (char*) malloc(*buffer_size);
  1354.  
  1355.         if (!buffer) {
  1356.                 //opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to allocate index buffer for writing %d int\n", *buffer_size);
  1357.                 fprintf(stderr, "failed to allocate index buffer for writing %d int\n", *buffer_size);
  1358.                 return 0;
  1359.         }
  1360.        
  1361.         buffer[0] = 1;  // Version stored on a byte
  1362.         buffer++;
  1363.         // Remaining informations are stored on a int.
  1364.         ((int*)buffer)[buffer_pos++] = cstr_info->image_w;
  1365.         ((int*)buffer)[buffer_pos++] = cstr_info->image_h;
  1366.         ((int*)buffer)[buffer_pos++] = cstr_info->prog;
  1367.         ((int*)buffer)[buffer_pos++] = cstr_info->tile_x;
  1368.         ((int*)buffer)[buffer_pos++] = cstr_info->tile_y;
  1369.         ((int*)buffer)[buffer_pos++] = cstr_info->tw;
  1370.         ((int*)buffer)[buffer_pos++] = cstr_info->th;
  1371.         ((int*)buffer)[buffer_pos++] = cstr_info->numcomps;
  1372.         ((int*)buffer)[buffer_pos++] = cstr_info->numlayers;
  1373.         ((int*)buffer)[buffer_pos++] = cstr_info->numdecompos[0];
  1374.        
  1375.         for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) {
  1376.                         /* based on tile 0 */
  1377.                 ((int*)buffer)[buffer_pos++] = (1 << cstr_info->tile[0].pdx[resno]);
  1378.                 ((int*)buffer)[buffer_pos++] = (1 << cstr_info->tile[0].pdx[resno]);
  1379.         }
  1380.         ((int*)buffer)[buffer_pos++] = cstr_info->main_head_end;
  1381.         ((int*)buffer)[buffer_pos++] = cstr_info->codestream_size;
  1382.        
  1383.         for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
  1384.                 ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].tileno;
  1385.                 ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].start_pos;
  1386.                 ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].end_header;
  1387.                 ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].end_pos;
  1388.         }
  1389.        
  1390.         for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
  1391.                 int start_pos, end_pos;
  1392.                 int max_numdecompos = 0;
  1393.                 pack_nb = 0;
  1394.                
  1395.                 for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1396.                         if (max_numdecompos < cstr_info->numdecompos[compno])
  1397.                                 max_numdecompos = cstr_info->numdecompos[compno];
  1398.                 }      
  1399.  
  1400.                 if (cstr_info->prog == LRCP) {  /* LRCP */
  1401.  
  1402.                         for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1403.                                 for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1404.                                         for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1405.                                                 int prec_max;
  1406.                                                 if (resno > cstr_info->numdecompos[compno])
  1407.                                                         break;
  1408.                                                 prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
  1409.                                                 for (precno = 0; precno < prec_max; precno++) {
  1410.                                                         start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1411.                                                         end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1412.                                                         ((int*)buffer)[buffer_pos++] = pack_nb;
  1413.                                                         ((int*)buffer)[buffer_pos++] = tileno;
  1414.                                                         ((int*)buffer)[buffer_pos++] = layno;
  1415.                                                         ((int*)buffer)[buffer_pos++] = resno;
  1416.                                                         ((int*)buffer)[buffer_pos++] = compno;
  1417.                                                         ((int*)buffer)[buffer_pos++] = precno;
  1418.                                                         ((int*)buffer)[buffer_pos++] = start_pos;
  1419.                                                         ((int*)buffer)[buffer_pos++] = end_pos;
  1420.                                                         pack_nb++;
  1421.                                                 }
  1422.                                         }
  1423.                                 }
  1424.                         }
  1425.                 } /* LRCP */
  1426.                 else if (cstr_info->prog == RLCP) {     /* RLCP */
  1427.  
  1428.                         for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1429.                                 for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1430.                                         for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1431.                                                 int prec_max;
  1432.                                                 if (resno > cstr_info->numdecompos[compno])
  1433.                                                         break;
  1434.                                                 prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
  1435.                                                 for (precno = 0; precno < prec_max; precno++) {
  1436.                                                         start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1437.                                                         end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1438.                                                         ((int*)buffer)[buffer_pos++] = pack_nb;
  1439.                                                         ((int*)buffer)[buffer_pos++] = tileno;
  1440.                                                         ((int*)buffer)[buffer_pos++] = resno;
  1441.                                                         ((int*)buffer)[buffer_pos++] = layno;
  1442.                                                         ((int*)buffer)[buffer_pos++] = compno;
  1443.                                                         ((int*)buffer)[buffer_pos++] = precno;
  1444.                                                         ((int*)buffer)[buffer_pos++] = start_pos;
  1445.                                                         ((int*)buffer)[buffer_pos++] = end_pos;
  1446.                                                         pack_nb++;
  1447.                                                 }
  1448.                                         }
  1449.                                 }
  1450.                         }
  1451.                 } /* RLCP */
  1452.                 else if (cstr_info->prog == RPCL) {     /* RPCL */
  1453.  
  1454.                         for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1455.                                 /* I suppose components have same XRsiz, YRsiz */
  1456.                                 int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
  1457.                                 int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
  1458.                                 int x1 = x0 + cstr_info->tile_x;
  1459.                                 int y1 = y0 + cstr_info->tile_y;
  1460.                                 for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1461.                                         int prec_max;
  1462.                                         if (resno > cstr_info->numdecompos[compno])
  1463.                                                 break;
  1464.                                         prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
  1465.                                         for (precno = 0; precno < prec_max; precno++) {
  1466.                                                 int pcnx = cstr_info->tile[tileno].pw[resno];
  1467.                                                 int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
  1468.                                                 int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
  1469.                                                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
  1470.                                                 int precno_y = (int) floor( (float)precno/(float)pcnx );
  1471.                                                 for(y = y0; y < y1; y++) {                                                     
  1472.                                                         if (precno_y*pcy == y ) {
  1473.                                                                 for (x = x0; x < x1; x++) {                                                                    
  1474.                                                                         if (precno_x*pcx == x ) {
  1475.                                                                                 for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1476.                                                                                         start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1477.                                                                                         end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1478.                                                                                         ((int*)buffer)[buffer_pos++] = pack_nb;
  1479.                                                                                         ((int*)buffer)[buffer_pos++] = tileno;
  1480.                                                                                         ((int*)buffer)[buffer_pos++] = resno;
  1481.                                                                                         ((int*)buffer)[buffer_pos++] = precno;
  1482.                                                                                         ((int*)buffer)[buffer_pos++] = compno;
  1483.                                                                                         ((int*)buffer)[buffer_pos++] = layno;
  1484.                                                                                         ((int*)buffer)[buffer_pos++] = start_pos;
  1485.                                                                                         ((int*)buffer)[buffer_pos++] = end_pos;
  1486.                                                                                         pack_nb++;
  1487.                                                                                 }
  1488.                                                                         }
  1489.                                                                 }/* x = x0..x1 */
  1490.                                                         }
  1491.                                                 }  /* y = y0..y1 */
  1492.                                         } /* precno */
  1493.                                 } /* compno */
  1494.                         } /* resno */
  1495.                 } /* RPCL */
  1496.                 else if (cstr_info->prog == PCRL) {     /* PCRL */
  1497.                         /* I suppose components have same XRsiz, YRsiz */
  1498.                         int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
  1499.                         int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
  1500.                         int x1 = x0 + cstr_info->tile_x;
  1501.                         int y1 = y0 + cstr_info->tile_y;
  1502.  
  1503.                         for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1504.                                 for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1505.                                         int prec_max;
  1506.                                         if (resno > cstr_info->numdecompos[compno])
  1507.                                                 break;
  1508.                                         prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
  1509.                                         for (precno = 0; precno < prec_max; precno++) {
  1510.                                                 int pcnx = cstr_info->tile[tileno].pw[resno];
  1511.                                                 int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
  1512.                                                 int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
  1513.                                                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
  1514.                                                 int precno_y = (int) floor( (float)precno/(float)pcnx );
  1515.                                                 for(y = y0; y < y1; y++) {                                                     
  1516.                                                         if (precno_y*pcy == y ) {
  1517.                                                                 for (x = x0; x < x1; x++) {                                                                    
  1518.                                                                         if (precno_x*pcx == x ) {
  1519.                                                                                 for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1520.                                                                                         start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1521.                                                                                         end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1522.                                                                                         ((int*)buffer)[buffer_pos++] = pack_nb;
  1523.                                                                                         ((int*)buffer)[buffer_pos++] = tileno;
  1524.                                                                                         ((int*)buffer)[buffer_pos++] = precno;
  1525.                                                                                         ((int*)buffer)[buffer_pos++] = compno;
  1526.                                                                                         ((int*)buffer)[buffer_pos++] = resno;
  1527.                                                                                         ((int*)buffer)[buffer_pos++] = layno;
  1528.                                                                                         ((int*)buffer)[buffer_pos++] = start_pos;
  1529.                                                                                         ((int*)buffer)[buffer_pos++] = end_pos;
  1530.                                                                                         pack_nb++;
  1531.                                                                                 }
  1532.                                                                         }
  1533.                                                                 }/* x = x0..x1 */
  1534.                                                         }
  1535.                                                 }  /* y = y0..y1 */
  1536.                                         } /* precno */
  1537.                                 } /* resno */
  1538.                         } /* compno */
  1539.                 } /* PCRL */
  1540.                 else {  /* CPRL */
  1541.  
  1542.                         for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1543.                                 /* I suppose components have same XRsiz, YRsiz */
  1544.                                 int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
  1545.                                 int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
  1546.                                 int x1 = x0 + cstr_info->tile_x;
  1547.                                 int y1 = y0 + cstr_info->tile_y;
  1548.                                
  1549.                                 for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1550.                                         int prec_max;
  1551.                                         if (resno > cstr_info->numdecompos[compno])
  1552.                                                 break;
  1553.                                         prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
  1554.                                         for (precno = 0; precno < prec_max; precno++) {
  1555.                                                 int pcnx = cstr_info->tile[tileno].pw[resno];
  1556.                                                 int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
  1557.                                                 int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
  1558.                                                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
  1559.                                                 int precno_y = (int) floor( (float)precno/(float)pcnx );
  1560.                                                 for(y = y0; y < y1; y++) {
  1561.                                                         if (precno_y*pcy == y ) {
  1562.                                                                 for (x = x0; x < x1; x++) {
  1563.                                                                         if (precno_x*pcx == x ) {
  1564.                                                                                 for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1565.                                                                                         start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1566.                                                                                         end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1567.                                                                                         ((int*)buffer)[buffer_pos++] = pack_nb;
  1568.                                                                                         ((int*)buffer)[buffer_pos++] = tileno;
  1569.                                                                                         ((int*)buffer)[buffer_pos++] = compno;
  1570.                                                                                         ((int*)buffer)[buffer_pos++] = precno;
  1571.                                                                                         ((int*)buffer)[buffer_pos++] = resno;
  1572.                                                                                         ((int*)buffer)[buffer_pos++] = layno;
  1573.                                                                                         ((int*)buffer)[buffer_pos++] = start_pos;
  1574.                                                                                         ((int*)buffer)[buffer_pos++] = end_pos;
  1575.                                                                                         pack_nb++;
  1576.                                                                                 }
  1577.                                                                         }
  1578.                                                                 }/* x = x0..x1 */
  1579.                                                         }
  1580.                                                 } /* y = y0..y1 */
  1581.                                         } /* precno */
  1582.                                 } /* resno */
  1583.                         } /* compno */
  1584.                 } /* CPRL */  
  1585.         } /* tileno */
  1586.  
  1587.         if (buffer_pos > *buffer_size) {
  1588.                 //opj_event_msg(j2k->cinfo, EVT_ERROR, "index creation: buffer_pos (%d) > buffer_size (%d)!\n", buffer_pos, *buffer_size);
  1589.                 fprintf(stderr, "index creation: buffer_pos (%d) > buffer_size (%d)!\n", buffer_pos, *buffer_size);
  1590.                 return 0;
  1591.         }
  1592.  
  1593.         return --buffer;
  1594. }
  1595.  
  1596.  
  1597.  
  1598.  
  1599. /* --------------------------------------------------------------------------
  1600.    ------------ Get the image byte[] from the Java object -------------------*/
  1601.  
  1602. opj_image_t* loadImage(opj_cparameters_t *parameters, JNIEnv *env, jobject obj, jclass cls) {
  1603.         int i,max,shift,w,h,depth;
  1604.         opj_image_t * img = NULL;
  1605.         int compno, numcomps;
  1606.         opj_image_t * image = NULL;
  1607.         opj_image_comp_t *comp;
  1608.         opj_image_cmptparm_t cmptparm[3];       /* maximum of 3 components */
  1609.         OPJ_COLOR_SPACE color_space;
  1610.         jfieldID        fid;
  1611.         jint            ji;
  1612.         jbyteArray      jba;
  1613.         jshortArray jsa;
  1614.         jintArray       jia;
  1615.         int                     len;
  1616.         jbyte           *jbBody;
  1617.         jshort          *jsBody;
  1618.         jint            *jiBody;
  1619.         boolean         isCopy;
  1620.  
  1621.         // Image width, height and depth
  1622.         fid = (*env)->GetFieldID(env, cls,"width", "I");
  1623.         ji = (*env)->GetIntField(env, obj, fid);
  1624.         w = ji;
  1625.  
  1626.         fid = (*env)->GetFieldID(env, cls,"height", "I");
  1627.         ji = (*env)->GetIntField(env, obj, fid);
  1628.         h = ji;
  1629.        
  1630.         fid = (*env)->GetFieldID(env, cls,"depth", "I");
  1631.         ji = (*env)->GetIntField(env, obj, fid);
  1632.         depth = ji;
  1633.  
  1634.         // Read the image
  1635.         if (depth <=16) {
  1636.                 numcomps = 1;
  1637.                 color_space = CLRSPC_GRAY;
  1638.         } else {
  1639.                 numcomps = 3;
  1640.                 color_space = CLRSPC_SRGB;
  1641.         }
  1642.         memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
  1643.  
  1644.         if (numcomps == 1) {
  1645.                 cmptparm[0].x0 = parameters->image_offset_x0;
  1646.                 cmptparm[0].y0 = parameters->image_offset_y0;
  1647.                 cmptparm[0].w = !cmptparm[0].x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm[0].x0 + (w - 1) * parameters->subsampling_dx + 1;
  1648.                 cmptparm[0].h = !cmptparm[0].y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm[0].y0 + (h - 1) * parameters->subsampling_dy + 1;
  1649.                 // Java types are always signed but we use them as unsigned types (shift of the negative part of
  1650.                 // the pixels of the images in Telemis before entering the encoder).
  1651.                 cmptparm[0].sgnd = 0;
  1652.                 if (depth<=16)
  1653.                         cmptparm[0].prec=depth;
  1654.                 else
  1655.                         cmptparm[0].prec = 8;
  1656.                 cmptparm[0].bpp = cmptparm[0].prec;
  1657.                 cmptparm[0].dx = parameters->subsampling_dx;
  1658.                 cmptparm[0].dy = parameters->subsampling_dy;
  1659.                 /*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,
  1660.                                         cmptparm[0].h, cmptparm[0].sgnd, cmptparm[0].bpp, cmptparm[0].dx, cmptparm[0].dy, color_space);*/
  1661.         } else {
  1662.                 for(i = 0; i < numcomps; i++) {
  1663.                         cmptparm[i].prec = 8;
  1664.                         cmptparm[i].bpp = 8;
  1665.                         cmptparm[i].sgnd = 0;
  1666.                         cmptparm[i].dx = parameters->subsampling_dx;
  1667.                         cmptparm[i].dy = parameters->subsampling_dy;
  1668.                         cmptparm[i].w = w;
  1669.                         cmptparm[i].h = h;
  1670.                 }
  1671.         }
  1672.        
  1673.         /* create the image */
  1674.         image = opj_image_create(numcomps, &cmptparm[0], color_space);
  1675.  
  1676.         if (!image)
  1677.                 return NULL;
  1678.  
  1679.         if (depth <=16) {
  1680.                 image->numcomps=1;
  1681.         } else {
  1682.                 image->numcomps = 3;
  1683.         }
  1684.  
  1685.         /* set image offset and reference grid */
  1686.         image->x0 = cmptparm[0].x0;
  1687.         image->y0 = cmptparm[0].x0;
  1688.         image->x1 = cmptparm[0].w;
  1689.         image->y1 = cmptparm[0].h;
  1690.  
  1691.         /* set image data */
  1692.         for (compno=0; compno<numcomps; compno++) {
  1693.                 comp = &image->comps[compno];
  1694.                 max = -100000;
  1695.                 if (depth == 8) {
  1696.                         fid = (*env)->GetFieldID(env, cls,"image8", "[B");      // byteArray []
  1697.                         jba = (*env)->GetObjectField(env, obj, fid);
  1698.                         len = (*env)->GetArrayLength(env, jba);
  1699.                        
  1700.                         jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, &isCopy);
  1701.                         //printf("C: before transfering 8 bpp image\n");
  1702.                         if (comp->sgnd) {
  1703.                                 for(i=0; i< len;i++) {
  1704.                                         comp->data[i] = (char) jbBody[i];
  1705.                                         if (comp->data[i] > max) max = comp->data[i];
  1706.                                 }
  1707.                         } else {
  1708.                                 for(i=0; i< len;i++) {
  1709.                                         comp->data[i] = (unsigned char) jbBody[i];
  1710.                                         if (comp->data[i] > max) max = comp->data[i];
  1711.                                 }
  1712.                         }
  1713.                         (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
  1714.                 } else if(depth == 16) {
  1715.                         fid = (*env)->GetFieldID(env, cls,"image16", "[S");     // shortArray []
  1716.                         jsa = (*env)->GetObjectField(env, obj, fid);
  1717.                         len = (*env)->GetArrayLength(env, jsa);
  1718.                        
  1719.                         jsBody = (*env)->GetPrimitiveArrayCritical(env, jsa, &isCopy);
  1720.                         //printf("C: before transfering 16 bpp image\n");
  1721.                         if (comp->sgnd) {       // Special behaviour to deal with signed elements ??
  1722.                                 comp->data[i] = (short) jsBody[i];
  1723.                                 for(i=0; i< len;i++) {
  1724.                                         if (comp->data[i] > max) max = comp->data[i];
  1725.                                 }
  1726.                         } else {
  1727.                                 for(i=0; i< len;i++) {
  1728.                                         comp->data[i] = (unsigned short) jsBody[i];
  1729.                                         if (comp->data[i] > max) max = comp->data[i];
  1730.                                 }
  1731.                         }
  1732.                         (*env)->ReleasePrimitiveArrayCritical(env, jsa, jsBody, 0);
  1733.                 } else if (depth == 24) {
  1734.                         fid = (*env)->GetFieldID(env, cls,"image24", "[I");     // intArray []
  1735.                         jia = (*env)->GetObjectField(env, obj, fid);
  1736.                         len = (*env)->GetArrayLength(env, jia);
  1737.                         shift = compno*8;
  1738.  
  1739.                         jiBody = (*env)->GetPrimitiveArrayCritical(env, jia, &isCopy);
  1740.                         //printf("C: before transfering 24 bpp image (component %d, signed = %d)\n", compno, comp->sgnd);
  1741.                         if (comp->sgnd) {       // Special behaviour to deal with signed elements ?? XXXXX
  1742.                                 for(i=0; i< len;i++) {
  1743.                                         comp->data[i] = ( ((int) jiBody[i]) & (0xFF << shift) ) >> shift;
  1744.                                         if (comp->data[i] > max) max = comp->data[i];
  1745.                                 }
  1746.                         } else {
  1747.                                 for(i=0; i< len;i++) {
  1748.                                         comp->data[i] = ( ((unsigned int) jiBody[i]) & (0xFF << shift) ) >> shift;
  1749.                                         if (comp->data[i] > max) max = comp->data[i];
  1750.                                 }
  1751.                         }
  1752.                         (*env)->ReleasePrimitiveArrayCritical(env, jia, jiBody, 0);
  1753.                 }
  1754.                 comp->bpp = int_floorlog2(max)+1;
  1755.                 comp->prec = comp->bpp;
  1756.                 //printf("C: component %d: max  %d, real bpp = %d\n", compno, max, comp->bpp);
  1757.         }
  1758.         return image;
  1759. }
  1760.  
  1761.  
  1762. /* --------------------------------------------------------------------------
  1763.    --------------------   MAIN METHOD, CALLED BY JAVA -----------------------*/
  1764. JNIEXPORT jlong JNICALL Java_org_openJpeg_OpenJPEGJavaEncoder_internalEncodeImageToJ2K(JNIEnv *env, jobject obj, jobjectArray javaParameters) {
  1765.         int argc;               /* To simulate the command line parameters (taken from the javaParameters variable) and be able to re-use the */
  1766.         char **argv;    /*  'parse_cmdline_decoder' method taken from the j2k_to_image project */
  1767.         bool bSuccess;
  1768.         opj_cparameters_t parameters;   /* compression parameters */
  1769.         img_fol_t img_fol;
  1770.         opj_event_mgr_t event_mgr;              /* event manager */
  1771.         opj_image_t *image = NULL;
  1772.         int i,j,num_images;
  1773.         int imageno;
  1774.         opj_codestream_info_t cstr_info;                /* Codestream information structure */
  1775.         char indexfilename[OPJ_PATH_LEN];       /* index file name */
  1776.  
  1777.         int* compressed_index = NULL;
  1778.         int compressed_index_size=-1;
  1779.         // ==> Access variables to the Java member variables
  1780.         jsize           arraySize;
  1781.         jclass          cls;
  1782.         jobject         object;
  1783.         jboolean        isCopy;
  1784.         jfieldID        fid;
  1785.         jbyteArray      jba;
  1786.         jbyte           *jbBody;
  1787.         callback_variables_t msgErrorCallback_vars;
  1788.         // <== access variable to the Java member variables.
  1789.  
  1790.         // For the encoding and storage into the file
  1791.         opj_cinfo_t* cinfo;
  1792.         int codestream_length;
  1793.         opj_cio_t *cio = NULL;
  1794.         FILE *f = NULL;
  1795.  
  1796.         // JNI reference to the calling class
  1797.         cls = (*env)->GetObjectClass(env, obj);
  1798.        
  1799.         // Pointers to be able to call a Java method for all the info and error messages
  1800.         msgErrorCallback_vars.env = env;
  1801.         msgErrorCallback_vars.jobj = &obj;
  1802.         msgErrorCallback_vars.message_mid = (*env)->GetMethodID(env, cls, "logMessage", "(Ljava/lang/String;)V");
  1803.         msgErrorCallback_vars.error_mid = (*env)->GetMethodID(env, cls, "logError", "(Ljava/lang/String;)V");
  1804.  
  1805.         arraySize = (*env)->GetArrayLength(env, javaParameters);
  1806.         argc = (int) arraySize +1;
  1807.         argv = malloc(argc*sizeof(char*));
  1808.         argv[0] = "ProgramName.exe";    // The program name: useless
  1809.         j=0;
  1810.         for (i=1; i<argc; i++) {
  1811.                 object = (*env)->GetObjectArrayElement(env, javaParameters, i-1);
  1812.                 argv[i] = (*env)->GetStringUTFChars(env, object, &isCopy);
  1813.         }
  1814.  
  1815.         /*printf("C: ");
  1816.         for (i=0; i<argc; i++) {
  1817.                 printf("[%s]",argv[i]);
  1818.         }
  1819.         printf("\n");*/
  1820.  
  1821.         /*
  1822.         configure the event callbacks
  1823.         */
  1824.         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
  1825.         event_mgr.error_handler = error_callback;
  1826.         event_mgr.warning_handler = warning_callback;
  1827.         event_mgr.info_handler = info_callback;
  1828.  
  1829.         /* set encoding parameters to default values */
  1830.         opj_set_default_encoder_parameters(&parameters);
  1831.         parameters.cod_format = J2K_CFMT;
  1832.         //parameters.index_on = 1;
  1833.  
  1834.         /* Initialize indexfilename and img_fol */
  1835.         *indexfilename = 0;
  1836.         memset(&img_fol,0,sizeof(img_fol_t));
  1837.  
  1838.         /* parse input and get user encoding parameters */
  1839.         if (parse_cmdline_encoder(argc, argv, &parameters,&img_fol, indexfilename) == 1) {
  1840.                 // Release the Java arguments array
  1841.                 for (i=1; i<argc; i++)
  1842.                         (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
  1843.                 return -1;
  1844.         }
  1845.  
  1846.         // Release the Java arguments array
  1847.         for (i=1; i<argc; i++)
  1848.                 (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
  1849.  
  1850.         if (parameters.cp_cinema){
  1851.                 cinema_parameters(&parameters);
  1852.         }
  1853.                                
  1854.  
  1855.         /* Create comment for codestream */
  1856.         if(parameters.cp_comment == NULL) {
  1857.                 const char comment[] = "Created by JavaOpenJPEG version ";
  1858.                 const size_t clen = strlen(comment);
  1859.                 const char *version = opj_version();
  1860. /* UniPG>> */
  1861. #ifdef USE_JPWL
  1862.                 parameters.cp_comment = (char*)malloc(clen+strlen(version)+11);
  1863.                 sprintf(parameters.cp_comment,"%s%s with JPWL", comment, version);
  1864. #else
  1865.                 parameters.cp_comment = (char*)malloc(clen+strlen(version)+1);
  1866.                 sprintf(parameters.cp_comment,"%s%s", comment, version);
  1867. #endif
  1868. /* <<UniPG */
  1869.  
  1870.         }
  1871.  
  1872.         /* Read directory if necessary */
  1873.         num_images=1;
  1874.  
  1875.         /*Encoding image one by one*/
  1876.         for(imageno=0;imageno<num_images;imageno++)
  1877.         {
  1878.                 image = NULL;
  1879.                 fprintf(stderr,"\n");
  1880.  
  1881.                 image = loadImage(&parameters, env, obj, cls);
  1882.                 //printf("C: after load image: image = %d\n", image);
  1883.                 if (!image) {
  1884.                         fprintf(stderr, "Unable to load image\n");
  1885.                         return -1;
  1886.                 }
  1887.  
  1888.                 /* Decide if MCT should be used */
  1889.                 parameters.tcp_mct = image->numcomps == 3 ? 1 : 0;
  1890.  
  1891.                 if(parameters.cp_cinema){
  1892.                         cinema_setup_encoder(&parameters,image,&img_fol);
  1893.                 }
  1894.  
  1895.                 /* encode the destination image */
  1896.                 /* ---------------------------- */
  1897.                 /* get a J2K compressor handle */
  1898.                 if (parameters.cod_format == J2K_CFMT) {        /* J2K format output */
  1899.                         cinfo = opj_create_compress(CODEC_J2K);
  1900.                 } else {                                                                        /* JP2 format output */
  1901.                         cinfo = opj_create_compress(CODEC_JP2);
  1902.                 }
  1903.                 /* catch events using our callbacks and give a local context */
  1904.                 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, &msgErrorCallback_vars);
  1905.  
  1906.                 /* setup the encoder parameters using the current image and user parameters */
  1907.                 opj_setup_encoder(cinfo, &parameters, image);
  1908.  
  1909.                 /* open a byte stream for writing */
  1910.                 /* allocate memory for all tiles */
  1911.                 cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
  1912.  
  1913.                 /* encode the image */
  1914.                 bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info);
  1915.                 printf("C: after opj_encode_with_info\n");
  1916.                 if (!bSuccess) {
  1917.                         opj_cio_close(cio);
  1918.                         fprintf(stderr, "failed to encode image\n");
  1919.                         return -1;
  1920.                 }
  1921.                 codestream_length = cio_tell(cio);
  1922.  
  1923.                 /* write the index on disk, if needed (-x 'filename') */
  1924.                 if (*indexfilename) {
  1925.                         bSuccess = write_index_file(&cstr_info, indexfilename);
  1926.                         if (bSuccess) {
  1927.                                 fprintf(stderr, "Failed to output index file into [%s]\n", indexfilename);
  1928.                         }
  1929.                 }
  1930.  
  1931.                 compressed_index = create_index_into_byte_array(&cstr_info, &compressed_index_size);
  1932.                 /* Allocates the Java compressedIndex byte[] and sends this index into the Java object */
  1933.                 fid = (*env)->GetFieldID(env, cls,"compressedIndex", "[B");
  1934.                 jba = (*env)->NewByteArray(env, compressed_index_size+1);
  1935.                 jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, 0);
  1936.                 memcpy(jbBody, compressed_index, compressed_index_size);
  1937.                 (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
  1938.                 (*env)->SetObjectField(env, obj, fid, jba);
  1939.                 free(compressed_index);
  1940.  
  1941.                 /* write the generated codestream to disk ? */
  1942.                 if (parameters.outfile[0]!='\0') {
  1943.                         f = fopen(parameters.outfile, "wb");
  1944.                         if (!f) {
  1945.                                 fprintf(stderr, "failed to open [%s] for writing\n", parameters.outfile);
  1946.                                 return -1;
  1947.                         }
  1948.                         fwrite(cio->buffer, 1, codestream_length, f);
  1949.                         fclose(f);
  1950.                         fprintf(stdout,"Generated outfile [%s]\n",parameters.outfile);
  1951.                 }
  1952.  
  1953.                 /* Write the generated codestream to the Java pre-allocated compressedStream byte[] */
  1954.                 fid = (*env)->GetFieldID(env, cls,"compressedStream", "[B");
  1955.                 jba = (*env)->GetObjectField(env, obj, fid);
  1956.                 jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, 0);
  1957.                 memcpy(jbBody, cio->buffer, codestream_length);
  1958.                 (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
  1959.  
  1960.                 /* close and free the byte stream */
  1961.                 opj_cio_close(cio);
  1962.  
  1963.                 /* free remaining compression structures */
  1964.                 opj_destroy_compress(cinfo);
  1965.                 opj_destroy_cstr_info(&cstr_info);
  1966.  
  1967.                 /* free image data */
  1968.                 opj_image_destroy(image);
  1969.         }
  1970.  
  1971.         /* free user parameters structure */
  1972.   if(parameters.cp_comment) free(parameters.cp_comment);
  1973.         if(parameters.cp_matrice) free(parameters.cp_matrice);
  1974.  
  1975.         return codestream_length;
  1976. }
  1977.