Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 2001-2003, David Janssens
  3.  * Copyright (c) 2002-2003, Yannick Verschueren
  4.  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
  5.  * Copyright (c) 2005, Hervé Drolon, FreeImage Team
  6.  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  7.  * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34.  
  35. #include "../libjp3dvm/openjpeg3d.h"
  36. #include "getopt.h"
  37. #include "convert.h"
  38.  
  39. #ifdef _WIN32
  40. #include <windows.h>
  41. #else
  42. #define stricmp strcasecmp
  43. #define strnicmp strncasecmp
  44. #endif /* _WIN32 */
  45.  
  46. /* ----------------------------------------------------------------------- */
  47.  
  48. void encode_help_display() {
  49.         fprintf(stdout,"List of parameters for the JPEG2000 Part 10 encoder:\n");
  50.         fprintf(stdout,"------------\n");
  51.         fprintf(stdout,"\n");
  52.         fprintf(stdout,"Required Parameters (except with -h):\n");
  53.         fprintf(stdout,"\n");
  54.         fprintf(stdout,"-i           : source file  (-i source.bin or source*.pgx) \n");
  55.         fprintf(stdout,"\n");
  56.         fprintf(stdout,"-m           : source characteristics file (-m imgfile.img) \n");
  57.         fprintf(stdout,"\n");
  58.         fprintf(stdout,"-o           : destination file (-o dest.jp3d) \n");
  59.         fprintf(stdout,"\n");
  60.         fprintf(stdout,"Optional Parameters:\n");
  61.         fprintf(stdout,"\n");
  62.         fprintf(stdout,"-h           : display the help information \n ");
  63.         fprintf(stdout,"\n");
  64.         fprintf(stdout,"-n           : number of resolutions (-n 3,3,3) \n");
  65.         fprintf(stdout,"\n");
  66.         fprintf(stdout,"-I           : use the irreversible transforms: ICT + DWT 9-7 (-I) \n");
  67.         fprintf(stdout,"\n");
  68.         fprintf(stdout,"-C           : coding algorithm (-C 2EB) [2EB, 3EB] \n");
  69.         fprintf(stdout,"\n");
  70.         fprintf(stdout,"-r           : different compression ratios for successive layers (-r 20,10,5)\n ");
  71.         fprintf(stdout,"                 - The rate specified for each quality level is the desired compression factor.\n");
  72.         fprintf(stdout,"                 - Rate 1 means lossless compression\n");
  73.         fprintf(stdout,"               (options -r and -q cannot be used together)\n ");
  74.         fprintf(stdout,"\n");
  75.         fprintf(stdout,"-q           : different psnr for successive layers (-q 30,40,50) \n ");
  76.         fprintf(stdout,"               (options -r and -q cannot be used together)\n ");
  77.         fprintf(stdout,"\n");
  78.         fprintf(stdout,"-b           : size of code block (-b 32,32,32) \n");
  79.         fprintf(stdout,"\n");
  80.         fprintf(stdout,"-c           : size of precinct (-c 128,128,128) \n");
  81.         fprintf(stdout,"\n");
  82.         fprintf(stdout,"-t           : size of tile (-t 512,512,512) \n");
  83.         fprintf(stdout,"\n");
  84.         fprintf(stdout,"-p           : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
  85.         fprintf(stdout,"\n");
  86.         fprintf(stdout,"-s           : subsampling factor (-s 2,2,2) [-s X,Y,Z] \n");
  87.         fprintf(stdout,"                          - Remark: subsampling bigger than 2 can produce error\n");
  88.         fprintf(stdout,"\n");
  89.         fprintf(stdout,"-SOP         : write SOP marker before each packet \n");
  90.         fprintf(stdout,"\n");
  91.         fprintf(stdout,"-EPH         : write EPH marker after each header packet \n");
  92.         fprintf(stdout,"\n");
  93.         fprintf(stdout,"-M           : code-block style (-M 0) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
  94.         fprintf(stdout,"                 8=VSC 16=PTERM 32=SEGSYM 64=3DCTXT] \n");
  95.         fprintf(stdout,"                 Indicate multiple modes by adding their values. \n");
  96.         fprintf(stdout,"                 ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
  97.         fprintf(stdout,"\n");
  98.         fprintf(stdout,"-D           : define DC offset (-D 12) \n");
  99.         fprintf(stdout,"\n");
  100.         fprintf(stdout,"-x           : create an index file *.Idx (-x index_name.Idx) \n");
  101.         fprintf(stdout,"\n");
  102.         fprintf(stdout,"-ROI         : c=%%d,U=%%d : quantization indices upshifted \n");
  103.         fprintf(stdout,"               for component c=%%d [%%d = 0,1,2]\n");
  104.         fprintf(stdout,"               with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
  105.         fprintf(stdout,"\n");
  106.         fprintf(stdout,"-d           : offset of the origin of the volume (-d 150,300,100) \n");
  107.         fprintf(stdout,"\n");
  108.         fprintf(stdout,"-l           : offset of the origin of the tiles (-l 100,75,25) \n");
  109.         fprintf(stdout,"\n");
  110.         fprintf(stdout,"\n");
  111.         fprintf(stdout,"DEFAULT CODING:\n");
  112.         fprintf(stdout,"------------\n");
  113.         fprintf(stdout,"\n");
  114.         fprintf(stdout," * Lossless\n");
  115.         fprintf(stdout," * 1 tile\n");
  116.         fprintf(stdout," * Size of precinct : 2^15 x 2^15 x 2^15 (means 1 precinct)\n");
  117.         fprintf(stdout," * Size of code-block : 64 x 64 x 64\n");
  118.         fprintf(stdout," * Number of resolutions in x, y and z axis: 3\n");
  119.         fprintf(stdout," * No SOP marker in the codestream\n");
  120.         fprintf(stdout," * No EPH marker in the codestream\n");
  121.         fprintf(stdout," * No sub-sampling in x, y or z direction\n");
  122.         fprintf(stdout," * No mode switch activated\n");
  123.         fprintf(stdout," * Progression order: LRCP\n");
  124.         fprintf(stdout," * No index file\n");
  125.         fprintf(stdout," * No ROI upshifted\n");
  126.         fprintf(stdout," * No offset of the origin of the volume\n");
  127.         fprintf(stdout," * No offset of the origin of the tiles\n");
  128.         fprintf(stdout," * Reversible DWT 5-3 on each 2D slice\n");
  129.         fprintf(stdout," * Coding algorithm: 2D-EBCOT \n");
  130.         fprintf(stdout,"\n");
  131.         fprintf(stdout,"REMARKS:\n");
  132.         fprintf(stdout,"---------\n");
  133.         fprintf(stdout,"\n");
  134.         fprintf(stdout,"- The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
  135.         fprintf(stdout,"- COD and QCD markers will never appear in the tile_header.\n");
  136.         fprintf(stdout,"\n");
  137.         fprintf(stdout,"- You need enough disk space memory (twice the original) to encode \n");
  138.         fprintf(stdout,"the volume,i.e. for a 1.5 GB volume you need a minimum of 3GB of disk memory)\n");
  139.         fprintf(stdout,"\n");
  140.         fprintf(stdout,"- When loading *.pgx files, a relative path to directory is needed for input argument \n");
  141.         fprintf(stdout," followed by the common prefix of the slices and a '*' character representing sequential numeration.\n");
  142.         fprintf(stdout,"( -i relativepath/slices*.pgx )\n");
  143.         fprintf(stdout,"\n");
  144.         fprintf(stdout," - The index file has the structure below:\n");
  145.         fprintf(stdout,"\n");
  146.         fprintf(stdout,"\t      Image_height Image_width Image_depth\n");
  147.         fprintf(stdout,"\t      Progression order: 0 (LRCP)\n");
  148.         fprintf(stdout,"\t      Tiles_size_X Tiles_size_Y Tiles_size_Z\n");
  149.         fprintf(stdout,"\t      Components_nb\n");
  150.         fprintf(stdout,"\t      Layers_nb\n");
  151.         fprintf(stdout,"\t      Decomposition_levels\n");
  152.         fprintf(stdout,"\t      [Precincts_size_X_res_Nr Precincts_size_Y_res_Nr Precincts_size_Z_res_Nr]\n\t  ...\n");
  153.         fprintf(stdout,"\t      [Precincts_size_X_res_0 Precincts_size_Y_res_0 Precincts_size_Z_res_0]\n");
  154.         fprintf(stdout,"\t      Main_header_end_position\n");
  155.         fprintf(stdout,"\t      Codestream_size\n");
  156.         fprintf(stdout,"\t      Tile_0 [start_pos end_header end_pos TotalDisto NumPix MaxMSE]\n");
  157.         fprintf(stdout,"\t      ...\n");
  158.         fprintf(stdout,"\t      Tile_Nt [  ''         ''        ''        ''       ''    ''  ]\n");
  159.         fprintf(stdout,"\t  Tpacket_0 [Tile layer res. comp. prec. start_pos end_pos disto]\n");
  160.         fprintf(stdout,"\t  ...\n");
  161.         fprintf(stdout,"\t  Tpacket_Np [''   ''    ''   ''    ''       ''       ''     '' ]\n");
  162.         fprintf(stdout,"\t  MaxDisto\n");
  163.         fprintf(stdout,"\t  TotalDisto\n\n");
  164.         fprintf(stdout,"\n");
  165.  
  166. }
  167.  
  168. OPJ_PROG_ORDER give_progression(char progression[4]) {
  169.         if(strncmp(progression, "LRCP", 4) == 0) {
  170.                 return LRCP;
  171.         }
  172.         if(strncmp(progression, "RLCP", 4) == 0) {
  173.                 return RLCP;
  174.         }
  175.         if(strncmp(progression, "RPCL", 4) == 0) {
  176.                 return RPCL;
  177.         }
  178.         if(strncmp(progression, "PCRL", 4) == 0) {
  179.                 return PCRL;
  180.         }
  181.         if(strncmp(progression, "CPRL", 4) == 0) {
  182.                 return CPRL;
  183.         }
  184.  
  185.         return PROG_UNKNOWN;
  186. }
  187.  
  188. OPJ_TRANSFORM give_transform(char transform[4]) {
  189.         if(strncmp(transform, "2DWT", 4) == 0) {
  190.                 return TRF_2D_DWT;
  191.         }
  192.         if(strncmp(transform, "3DWT", 4) == 0) {
  193.                 return TRF_3D_DWT;
  194.         }
  195.         return TRF_UNKNOWN;
  196. }
  197.  
  198. OPJ_ENTROPY_CODING give_coding(char coding[3]) {
  199.  
  200.         if(strncmp(coding, "2EB", 3) == 0) {
  201.                 return ENCOD_2EB;
  202.         }
  203.         if(strncmp(coding, "3EB", 3) == 0) {
  204.                 return ENCOD_3EB;
  205.         }
  206.         /*if(strncmp(coding, "2GR", 3) == 0) {
  207.                 return ENCOD_2GR;
  208.         }
  209.         if(strncmp(coding, "3GR", 3) == 0) {
  210.                 return ENCOD_3GR;
  211.         }*/
  212.  
  213.         return ENCOD_UNKNOWN;
  214. }
  215.  
  216. int get_file_format(char *filename) {
  217.         int i;
  218.         static const char *extension[] = {"pgx", "bin", "img", "j3d", "jp3d", "j2k"};
  219.         static const int format[] = { PGX_DFMT, BIN_DFMT, IMG_DFMT, J3D_CFMT, J3D_CFMT, J2K_CFMT};
  220.         char * ext = strrchr(filename, '.');
  221.         if (ext) {
  222.                 ext++;
  223.         for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
  224.                         if(strnicmp(ext, extension[i], 3) == 0) {
  225.                 return format[i];
  226.                         }
  227.                 }
  228.         }
  229.  
  230.         return -1;
  231. }
  232.  
  233. /* ------------------------------------------------------------------------------------ */
  234.  
  235. int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters) {
  236.         int i, value;
  237.  
  238.         /* parse the command line */
  239.  
  240.         while (1) {
  241.                 int c = getopt(argc, argv, "i:m:o:r:q:f:t:n:c:b:x:p:s:d:hP:S:E:M:D:R:l:T:C:A:I");
  242.                 if (c == -1)
  243.                         break;
  244.                 switch (c) {
  245.                         case 'i':                       /* input file */
  246.                         {
  247.                                 char *infile = optarg;
  248.                                 parameters->decod_format = get_file_format(infile);
  249.                                 switch(parameters->decod_format) {
  250.                                         case PGX_DFMT:
  251.                                         case BIN_DFMT:
  252.                                         case IMG_DFMT:
  253.                                                 break;
  254.                                         default:
  255.                                                 fprintf(stdout, "[ERROR] Unrecognized format for infile : %s [accept only *.pgx or *.bin] !!\n\n", infile);
  256.                                                 return 1;
  257.                                                 break;
  258.                                 }
  259.                                 strncpy(parameters->infile, infile, MAX_PATH);
  260.                                 fprintf(stdout, "[INFO] Infile: %s \n", parameters->infile);
  261.  
  262.                         }
  263.                         break;
  264.                                
  265.                                 /* ----------------------------------------------------- */
  266.                         case 'm':                       /* input IMG file */
  267.                         {
  268.                                 char *imgfile = optarg;
  269.                                 int imgformat = get_file_format(imgfile);
  270.                                 switch(imgformat) {
  271.                                         case IMG_DFMT:
  272.                                                 break;
  273.                                         default:
  274.                                                 fprintf(stdout, "[ERROR] Unrecognized format for imgfile : %s [accept only *.img] !!\n\n", imgfile);
  275.                                                 return 1;
  276.                                                 break;
  277.                                 }
  278.                                 strncpy(parameters->imgfile, imgfile, MAX_PATH);
  279.                                 fprintf(stdout, "[INFO] Imgfile: %s Format: %d\n", parameters->imgfile, imgformat);
  280.                         }
  281.                         break;
  282.                                
  283.                                 /* ----------------------------------------------------- */
  284.                         case 'o':                       /* output file */
  285.                         {
  286.                                 char *outfile = optarg;
  287.                                 parameters->cod_format = get_file_format(outfile);
  288.                                 switch(parameters->cod_format) {
  289.                                         case J3D_CFMT:
  290.                                         case J2K_CFMT:
  291.                                         case LSE_CFMT:
  292.                                                 break;
  293.                                         default:
  294.                                                 fprintf(stdout, "[ERROR] Unknown output format volume %s [only *.j2k, *.lse3d or *.jp3d]!! \n", outfile);
  295.                                                 return 1;
  296.                                                 break;
  297.                                 }
  298.                                 strncpy(parameters->outfile, outfile, MAX_PATH);
  299.                                 fprintf(stdout, "[INFO] Outfile: %s \n", parameters->outfile);
  300.                         }
  301.                         break;
  302.  
  303.                                 /* ----------------------------------------------------- */
  304.                        
  305.                         case 'r':                       /* define compression rates for each layer */
  306.                         {
  307.                                 char *s = optarg;
  308.                                 while (sscanf(s, "%f", &parameters->tcp_rates[parameters->tcp_numlayers]) == 1) {
  309.                                         parameters->tcp_numlayers++;
  310.                                         while (*s && *s != ',') {
  311.                                                 s++;
  312.                                         }
  313.                                         if (!*s)
  314.                                                 break;
  315.                                         s++;
  316.                                 }
  317.                                 parameters->cp_disto_alloc = 1;
  318.                         }
  319.                         break;
  320.                                
  321.                                 /* ----------------------------------------------------- */
  322.                        
  323.                         case 'q':                       /* define distorsion (PSNR) for each layer */
  324.                         {
  325.                                 char *s = optarg;
  326.                                 while (sscanf(s, "%f", &parameters->tcp_distoratio[parameters->tcp_numlayers]) == 1) {
  327.                                         parameters->tcp_numlayers++;
  328.                                         while (*s && *s != ',') {
  329.                                                 s++;
  330.                                         }
  331.                                         if (!*s)
  332.                                                 break;
  333.                                         s++;
  334.                                 }
  335.                                 parameters->cp_fixed_quality = 1;
  336.                         }
  337.                         break;
  338.                                
  339.                                 /* ----------------------------------------------------- */
  340.  
  341.                         case 'f':      
  342.                         {
  343.                                 fprintf(stdout, "/---------------------------------------------------\\\n");
  344.                                 fprintf(stdout, "|  Fixed layer allocation option not implemented !!  |\n");
  345.                                 fprintf(stdout, "\\---------------------------------------------------/\n");
  346.                                 /*int *row = NULL, *col = NULL;
  347.                                 int numlayers = 0, matrix_width = 0;
  348.  
  349.                                 char *s = optarg;
  350.                                 sscanf(s, "%d", &numlayers);
  351.                                 s++;
  352.                                 if (numlayers > 9)
  353.                                         s++;
  354.  
  355.                                 parameters->tcp_numlayers = numlayers;
  356.                                 matrix_width = parameters->numresolution[0] + parameters->numresolution[1] + parameters->numresolution[2];
  357.                                 parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
  358.                                 s = s + 2;
  359.  
  360.                                 for (i = 0; i < numlayers; i++) {
  361.                                         row = &parameters->cp_matrice[i * matrix_width];
  362.                                         col = row;
  363.                                         parameters->tcp_rates[i] = 1;
  364.                                         sscanf(s, "%d,", &col[0]);
  365.                                         s += 2;
  366.                                         if (col[0] > 9)
  367.                                                 s++;
  368.                                         col[1] = 0;
  369.                                         col[2] = 0;
  370.                                         for (j = 1; j < matrix_width; j++) {
  371.                                                 col += 3; j+=2;
  372.                                                 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
  373.                                                 s += 6;
  374.                                                 if (col[0] > 9)
  375.                                                         s++;
  376.                                                 if (col[1] > 9)
  377.                                                         s++;
  378.                                                 if (col[2] > 9)
  379.                                                         s++;
  380.                                         }
  381.                                         if (i < numlayers - 1)
  382.                                                 s++;
  383.                                 }
  384.                                 parameters->cp_fixed_alloc = 1; */
  385.                         }
  386.                         break;
  387.                                
  388.                                 /* ----------------------------------------------------- */
  389.  
  390.                         case 't':                       /* tiles */
  391.                         {
  392.                                 if (sscanf(optarg, "%d,%d,%d", &parameters->cp_tdx, &parameters->cp_tdy, &parameters->cp_tdz) !=3) {
  393.                                         fprintf(stdout, "[ERROR] '-t' 'dimensions of tiles' argument error !  [-t tdx,tdy,tdz]\n");
  394.                                         return 1;
  395.                                 }
  396.                                 parameters->tile_size_on = true;
  397.                         }
  398.                         break;
  399.                                
  400.                                 /* ----------------------------------------------------- */
  401.                        
  402.                         case 'n':                       /* resolution */
  403.                         {
  404.                                 int aux;
  405.                                 aux = sscanf(optarg, "%d,%d,%d", &parameters->numresolution[0], &parameters->numresolution[1], &parameters->numresolution[2]);
  406.                                 if (aux == 2)
  407.                                         parameters->numresolution[2] = 1;
  408.                                 else if (aux == 1) {
  409.                                         parameters->numresolution[1] = parameters->numresolution[0];
  410.                                         parameters->numresolution[2] = 1;
  411.                                 }else if (aux == 0){
  412.                                         parameters->numresolution[0] = 1;
  413.                                         parameters->numresolution[1] = 1;
  414.                                         parameters->numresolution[2] = 1;
  415.                                 }
  416.                         }
  417.                         break;
  418.                                
  419.                                 /* ----------------------------------------------------- */
  420.                         case 'c':                       /* precinct dimension */
  421.                         {
  422.                                 char sep;
  423.                                 int res_spec = 0;
  424.                                 int aux;
  425.                                 char *s = optarg;
  426.                                 do {
  427.                                         sep = 0;
  428.                                         aux = sscanf(s, "[%d,%d,%d]%c", &parameters->prct_init[0][res_spec], &parameters->prct_init[1][res_spec], &parameters->prct_init[2][res_spec], &sep);
  429.                                         if (sep == ',' && aux != 4) {
  430.                                                 fprintf(stdout, "[ERROR] '-c' 'dimensions of precincts' argument error !  [-c [prcx_res0,prcy_res0,prcz_res0],...,[prcx_resN,prcy_resN,prcz_resN]]\n");
  431.                                                 return 1;
  432.                                         }
  433.                                         parameters->csty |= 0x01;
  434.                                         res_spec++;
  435.                                         s = strpbrk(s, "]") + 2;
  436.                                 }
  437.                                 while (sep == ',');
  438.                                 parameters->res_spec = res_spec; /* number of precinct size specifications */
  439.                         }
  440.                         break;
  441.                                
  442.                                 /* ----------------------------------------------------- */
  443.                        
  444.                         case 'b':                       /* code-block dimension */
  445.                         {
  446.                                 int cblockw_init = 0, cblockh_init = 0, cblockl_init = 0;
  447.                                 if (sscanf(optarg, "%d,%d,%d", &cblockw_init, &cblockh_init, &cblockl_init) != 3) {
  448.                                         fprintf(stdout, "[ERROR] '-b' 'dimensions of codeblocks' argument error !  [-b cblkx,cblky,cblkz]\n");
  449.                                         return 1;
  450.                                 }
  451.                                 if (cblockw_init * cblockh_init * cblockl_init > (1<<18) || cblockw_init > 1024 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4 || cblockl_init > 1024 || cblockl_init < 4) {
  452.                                         fprintf(stdout,"[ERROR] Size of code_block error (option -b) !!\n\nRestriction :\n * width*height*length<=4096\n * 4<=width,height,length<= 1024\n\n");
  453.                                         return 1;
  454.                                 }
  455.                                 parameters->cblock_init[0] = cblockw_init;
  456.                                 parameters->cblock_init[1] = cblockh_init;
  457.                                 parameters->cblock_init[2] = cblockl_init;
  458.                         }
  459.                         break;
  460.                                
  461.                                 /* ----------------------------------------------------- */
  462.                        
  463.                         case 'x':                       /* creation of index file */
  464.                         {
  465.                                 char *index = optarg;
  466.                                 strncpy(parameters->index, index, MAX_PATH);
  467.                                 parameters->index_on = 1;
  468.                         }
  469.                         break;
  470.                                
  471.                                 /* ----------------------------------------------------- */
  472.                        
  473.                         case 'p':                       /* progression order */
  474.                         {
  475.                                 char progression[4];
  476.  
  477.                                 strncpy(progression, optarg, 4);
  478.                                 parameters->prog_order = give_progression(progression);
  479.                                 if (parameters->prog_order == -1) {
  480.                                         fprintf(stdout, "[ERROR] Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
  481.                                         return 1;
  482.                                 }
  483.                         }
  484.                         break;
  485.                                
  486.                                 /* ----------------------------------------------------- */
  487.                        
  488.                         case 's':                       /* subsampling factor */
  489.                         {
  490.                                 if (sscanf(optarg, "%d,%d,%d", &parameters->subsampling_dx, &parameters->subsampling_dy, &parameters->subsampling_dz) != 2) {
  491.                                         fprintf(stdout, "[ERROR] '-s' sub-sampling argument error !  [-s dx,dy,dz]\n");
  492.                                         return 1;
  493.                                 }
  494.                         }
  495.                         break;
  496.                                
  497.                                 /* ----------------------------------------------------- */
  498.                        
  499.                         case 'd':                       /* coordonnate of the reference grid */
  500.                         {
  501.                                 if (sscanf(optarg, "%d,%d,%d", &parameters->volume_offset_x0, &parameters->volume_offset_y0, &parameters->volume_offset_z0) != 3) {
  502.                                         fprintf(stdout, "[ERROR] -d 'coordonnate of the reference grid' argument error !! [-d x0,y0,z0]\n");
  503.                                         return 1;
  504.                                 }
  505.                         }
  506.                         break;
  507.                                
  508.                                 /* ----------------------------------------------------- */
  509.                        
  510.                         case 'h':                       /* display an help description */
  511.                         {
  512.                                 encode_help_display();
  513.                                 return 1;
  514.                         }
  515.                         break;
  516.                                
  517.                                 /* ----------------------------------------------------- */
  518.  
  519.                         case 'P':                       /* POC */
  520.                         {
  521.                                 int numpocs = 0;                /* number of progression order change (POC) default 0 */
  522.                                 opj_poc_t *POC = NULL;  /* POC : used in case of Progression order change */
  523.  
  524.                                 char *s = optarg;
  525.                                 POC = parameters->POC;
  526.  
  527.                                 fprintf(stdout, "/----------------------------------\\\n");
  528.                                 fprintf(stdout, "|  POC option not fully tested !!  |\n");
  529.                                 fprintf(stdout, "\\----------------------------------/\n");
  530.                                
  531.                                 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
  532.                                                                                                                 &POC[numpocs].resno0, &POC[numpocs].compno0,
  533.                                                                                                                 &POC[numpocs].layno1, &POC[numpocs].resno1,
  534.                                                                                                                 &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
  535.                                         POC[numpocs].prg = give_progression(POC[numpocs].progorder);
  536.                                         /* POC[numpocs].tile; */
  537.                                         numpocs++;
  538.                                         while (*s && *s != '/') {
  539.                                                 s++;
  540.                                         }
  541.                                         if (!*s) {
  542.                                                 break;
  543.                                         }
  544.                                         s++;
  545.                                 }
  546.                                 parameters->numpocs = numpocs;
  547.                         }
  548.                         break;
  549.                                
  550.                                 /* ------------------------------------------------------ */
  551.                                
  552.                         case 'S':                       /* SOP marker */
  553.                         {
  554.                                 parameters->csty |= 0x02;
  555.                         }
  556.                         break;
  557.                                
  558.                                 /* ------------------------------------------------------ */
  559.                        
  560.                         case 'E':                       /* EPH marker */
  561.                         {
  562.                                 parameters->csty |= 0x04;
  563.                         }
  564.                         break;
  565.                                
  566.                                 /* ------------------------------------------------------ */
  567.                        
  568.                         case 'M':                       /* Codification mode switch */
  569.                         {
  570.                                 fprintf(stdout, "[INFO] Mode switch option not fully tested !!\n");
  571.                                 value = 0;
  572.                                 if (sscanf(optarg, "%d", &value) == 1) {
  573.                                         for (i = 0; i <= 6; i++) {
  574.                                                 int cache = value & (1 << i);
  575.                                                 if (cache)
  576.                                                         parameters->mode |= (1 << i);
  577.                                         }
  578.                                 }
  579.                         }
  580.                         break;
  581.                                
  582.                                 /* ------------------------------------------------------ */
  583.                        
  584.                         case 'D':                       /* DCO */
  585.                         {
  586.                                 if (sscanf(optarg, "%d", &parameters->dcoffset) != 1) {
  587.                                         fprintf(stdout, "[ERROR] DC offset error !! [-D %d]\n",parameters->dcoffset);
  588.                                         return 1;
  589.                                 }
  590.                         }
  591.                         break;
  592.                                
  593.                                 /* ------------------------------------------------------ */
  594.                        
  595.                         case 'R':                       /* ROI */
  596.                         {
  597.                                 if (sscanf(optarg, "OI:c=%d,U=%d", &parameters->roi_compno, &parameters->roi_shift) != 2) {
  598.                                         fprintf(stdout, "[ERROR] ROI error !! [-ROI:c='compno',U='shift']\n");
  599.                                         return 1;
  600.                                 }
  601.                         }
  602.                         break;
  603.                                
  604.                                 /* ------------------------------------------------------ */
  605.                        
  606.                         case 'l':                       /* Tile offset */
  607.                         {
  608.                                 if (sscanf(optarg, "%d,%d,%d", &parameters->cp_tx0, &parameters->cp_ty0, &parameters->cp_tz0) != 3) {
  609.                                         fprintf(stdout, "[ERROR] -l 'tile offset' argument error !! [-l X0,Y0,Z0]");
  610.                                         return 1;
  611.                                 }
  612.                         }
  613.                         break;
  614.                                
  615.                                 /* ------------------------------------------------------
  616.                                
  617.                         case 'T':                       // Tranformation of original data (2D-DWT/3D-DWT/3D-RLS/2D-DWT+1D-RLS)
  618.                         {
  619.                                 char transform[4];
  620.  
  621.                                 strncpy(transform, optarg, 4);
  622.                                 parameters->transform_format = give_transform(transform);
  623.                                 if (parameters->transform_format == -1) {
  624.                                         fprintf(stdout, "[ERROR] -T 'Transform domain' argument error !! [-T 2DWT, 3DWT, 3RLS or 3LSE only]");
  625.                     return 1;
  626.                                 }
  627.                         }
  628.                         break;
  629.                                
  630.                                  ------------------------------------------------------ */
  631.                        
  632.                         case 'C':                       /* Coding of transformed data */
  633.                         {
  634.                                 char coding[3];
  635.  
  636.                                 strncpy(coding, optarg, 3);
  637.                                 parameters->encoding_format = give_coding(coding);
  638.                                 if (parameters->encoding_format == -1) {
  639.                                         fprintf(stdout, "[ERROR] -C 'Coding algorithm' argument error !! [-C 2EB, 3EB, 2GR, 3GR or GRI only]");
  640.                     return 1;
  641.                                 }
  642.                         }
  643.                         break;
  644.                        
  645.                         /* ------------------------------------------------------ */
  646.                        
  647.                         case 'I':                       /* reversible or not */
  648.                         {
  649.                                 parameters->irreversible = 1;
  650.                         }
  651.                         break;
  652.                                
  653.                         default:
  654.                                 fprintf(stdout, "[ERROR] This option is not valid \"-%c %s\"\n", c, optarg);
  655.                                 return 1;
  656.                 }
  657.         }
  658.  
  659.         /* check for possible errors */
  660.  
  661.         if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
  662.                 fprintf(stdout, "usage: jp3d_vm_enc -i volume-file -o jp3d-file (+ options)\n");
  663.                 return 1;
  664.         }
  665.  
  666.         if((parameters->decod_format == BIN_DFMT) && (parameters->imgfile[0] == 0)) {
  667.                 fprintf(stdout, "usage: jp3d_vm_enc -i bin-volume-file -m img-file -o jp3d-file (+ options)\n");
  668.                 return 1;
  669.         }
  670.  
  671.         if((parameters->decod_format != BIN_DFMT) && (parameters->decod_format != PGX_DFMT) && (parameters->decod_format != IMG_DFMT)) {
  672.                 fprintf(stdout, "usage: jp3d_vm_enc -i input-volume-file [*.bin,*.pgx,*.img] -o jp3d-file [*.jp3d,*.j2k] (+ options)\n");
  673.                 return 1;
  674.         }
  675.         if((parameters->cod_format != J3D_CFMT) && (parameters->cod_format != J2K_CFMT)) {
  676.                 fprintf(stdout, "usage: jp3d_vm_enc -i input-volume-file [*.bin,*.pgx,*.img] -o jp3d-file [*.jp3d,*.j2k] (+ options)\n");
  677.                 return 1;
  678.         }
  679.  
  680.         if((parameters->encoding_format == ENCOD_2GR || parameters->encoding_format == ENCOD_3GR) && parameters->transform_format != TRF_3D_LSE && parameters->transform_format != TRF_3D_RLS) {
  681.                 fprintf(stdout, "[ERROR] Entropy coding options -C [2GR,3GR] are only compatible with predictive-based transform algorithms: -T [3RLS,3LSE].\n");
  682.                 return 1;
  683.         }
  684.         if (parameters->encoding_format == ENCOD_3EB)
  685.                 parameters->mode |= (1 << 6);
  686.  
  687.         if ((parameters->mode >> 6) & 1) {
  688.                 parameters->encoding_format = ENCOD_3EB;
  689.         }
  690.  
  691.         if((parameters->numresolution[2] == 0 || (parameters->numresolution[1] == 0) || (parameters->numresolution[0] == 0))) {
  692.                 fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions must be greater than 1 in order to perform DWT.\n");
  693.                 return 1;
  694.         }
  695.         if (parameters->numresolution[1] != parameters->numresolution[0]) {
  696.                 fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions in X and Y axis must be the same in this implementation.\n");
  697.                 return 1;
  698.         }
  699.        
  700.         if (parameters->numresolution[2] > parameters->numresolution[0]) {
  701.                 fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions in Z axis must be lower than in X-Y axis.\n");
  702.                 return 1;
  703.         }
  704.        
  705.         if (parameters->dcoffset >= 128 && parameters->dcoffset <= -128) {
  706.                 fprintf(stdout, "[ERROR] -D 'DC offset' argument error ! Value must be -128<=DCO<=128.\n");
  707.                 return 1;
  708.         }
  709.  
  710.         if(parameters->numresolution[2] != 1) {
  711.                 parameters->transform_format = TRF_3D_DWT;
  712.                 //fprintf(stdout, "[Warning] Resolution level in axial dim > 1 : 3D-DWT will be performed... \n");
  713.         } else if (parameters->numresolution[2] == 1) {
  714.                 parameters->transform_format = TRF_2D_DWT;
  715.                 //fprintf(stdout, "[Warning] Resolution level in axial dim == 1 : 2D-DWT will be performed... \n");
  716.         }
  717.        
  718.         if ((parameters->cod_format == J2K_CFMT) && (parameters->transform_format != TRF_2D_DWT || parameters->encoding_format != ENCOD_2EB)) {
  719.                 fprintf(stdout, "[WARNING] Incompatible options -o *.j2k and defined transform or encoding algorithm. Latter will be ignored\n");
  720.                 parameters->transform_format = TRF_2D_DWT;
  721.                 parameters->encoding_format = ENCOD_2EB;
  722.         }
  723.  
  724.         if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality) && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_quality))) {
  725.                 fprintf(stdout, "[ERROR] Options -r and -q cannot be used together !!\n");
  726.                 return 1;
  727.         }                               /* mod fixed_quality */
  728.  
  729.         /* if no rate entered, lossless by default */
  730.         if (parameters->tcp_numlayers == 0) {
  731.                 parameters->tcp_rates[0] = 0.0; /* MOD antonin : losslessbug */
  732.                 parameters->tcp_numlayers++;
  733.                 parameters->cp_disto_alloc = 1;
  734.         }
  735.  
  736.         if((parameters->cp_tx0 > parameters->volume_offset_x0) || (parameters->cp_ty0 > parameters->volume_offset_y0) || (parameters->cp_tz0 > parameters->volume_offset_z0)) {
  737.                 fprintf(stdout, "[ERROR] Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) TZO(%d)<=IMG_Z0(%d)\n",
  738.                         parameters->cp_tx0, parameters->volume_offset_x0, parameters->cp_ty0, parameters->volume_offset_y0,
  739.                         parameters->cp_tz0, parameters->volume_offset_z0);
  740.                 return 1;
  741.         }
  742.  
  743.         for (i = 0; i < parameters->numpocs; i++) {
  744.                 if (parameters->POC[i].prg == -1) {
  745.                         fprintf(stdout,"[ERROR] Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",i + 1);
  746.                 }
  747.         }
  748.         return 0;
  749. }
  750.  
  751. /* -------------------------------------------------------------------------- */
  752.  
  753. /**
  754. sample error callback expecting a FILE* client object
  755. */
  756. void error_callback(const char *msg, void *client_data) {
  757.         FILE *stream = (FILE*)client_data;
  758.         fprintf(stream, "[ERROR] %s", msg);
  759. }
  760. /**
  761. sample warning callback expecting a FILE* client object
  762. */
  763. void warning_callback(const char *msg, void *client_data) {
  764.         FILE *stream = (FILE*)client_data;
  765.         fprintf(stream, "[WARNING] %s", msg);
  766. }
  767. /**
  768. sample debug callback expecting a FILE* client object
  769. */
  770. void info_callback(const char *msg, void *client_data) {
  771.         FILE *stream = (FILE*)client_data;
  772.         fprintf(stream, "[INFO] %s", msg);
  773. }
  774.  
  775. /* -------------------------------------------------------------------------- */
  776.  
  777. int main(int argc, char **argv) {
  778.         bool bSuccess;
  779.         bool delete_comment = true;
  780.         opj_cparameters_t parameters;   /* compression parameters */
  781.         opj_event_mgr_t event_mgr;              /* event manager */
  782.         opj_volume_t *volume = NULL;
  783.  
  784.         /*
  785.         configure the event callbacks (not required)
  786.         setting of each callback is optionnal
  787.         */
  788.         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
  789.         event_mgr.error_handler = error_callback;
  790.         event_mgr.warning_handler = warning_callback;
  791.         event_mgr.info_handler = info_callback;
  792.  
  793.         /* set encoding parameters to default values */
  794.         opj_set_default_encoder_parameters(&parameters);
  795.  
  796.         /* parse input and get user encoding parameters */
  797.         if(parse_cmdline_encoder(argc, argv, &parameters) == 1) {
  798.                 return 0;
  799.         }
  800.  
  801.         if(parameters.cp_comment == NULL) {
  802.                 parameters.cp_comment = "Created by OpenJPEG version JP3D";
  803.                 /* no need to delete parameters.cp_comment on exit */
  804.                 delete_comment = false;
  805.         }
  806.        
  807.         /* encode the destination volume */
  808.         /* ---------------------------- */
  809.         if (parameters.cod_format == J3D_CFMT || parameters.cod_format == J2K_CFMT)     {
  810.                 int codestream_length, pixels, bitsin;
  811.                 opj_cio_t *cio = NULL;
  812.                 FILE *f = NULL;
  813.                 opj_cinfo_t* cinfo = NULL;
  814.                
  815.                 /* decode the source volume */
  816.                 /* ----------------------- */
  817.                 switch (parameters.decod_format) {
  818.                         case PGX_DFMT:
  819.                                 fprintf(stdout, "[INFO] Loading pgx file(s)\n");
  820.                                 volume = pgxtovolume(parameters.infile, &parameters);
  821.                                 if (!volume) {
  822.                                         fprintf(stdout, "[ERROR] Unable to load pgx files\n");
  823.                                         return 1;
  824.                                 }
  825.                                 break;
  826.                        
  827.                         case BIN_DFMT:
  828.                                 fprintf(stdout, "[INFO] Loading bin file\n");
  829.                                 volume = bintovolume(parameters.infile, parameters.imgfile, &parameters);
  830.                                 if (!volume) {
  831.                                         fprintf(stdout, "[ERROR] Unable to load bin file\n");
  832.                                         return 1;
  833.                                 }
  834.                                 break;
  835.  
  836.                         case IMG_DFMT:
  837.                                 fprintf(stdout, "[INFO] Loading img file\n");
  838.                                 volume = imgtovolume(parameters.infile, &parameters);
  839.                                 if (!volume) {
  840.                                         fprintf(stderr, "[ERROR] Unable to load img file\n");
  841.                                         return 1;
  842.                                 }
  843.                                 break;
  844.                 }
  845.                
  846.                 /* get a JP3D or J2K compressor handle */
  847.                 if (parameters.cod_format == J3D_CFMT)
  848.             cinfo = opj_create_compress(CODEC_J3D);
  849.                 else if (parameters.cod_format == J2K_CFMT)
  850.             cinfo = opj_create_compress(CODEC_J2K);
  851.  
  852.                 /* catch events using our callbacks and give a local context */
  853.                 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stdout);                  
  854.  
  855.                 /* setup the encoder parameters using the current volume and using user parameters */
  856.                 opj_setup_encoder(cinfo, &parameters, volume);
  857.                
  858.                 /* open a byte stream for writing */
  859.                 /* allocate memory for all tiles */
  860.                 cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
  861.  
  862.                 /* encode the volume */
  863.                 //fprintf(stdout, "[INFO] Encode the volume\n");
  864.                 bSuccess = opj_encode(cinfo, cio, volume, parameters.index);
  865.                 if (!bSuccess) {
  866.                         opj_cio_close(cio);
  867.                         fprintf(stdout, "[ERROR] Failed to encode volume\n");
  868.                         return 1;
  869.                 }
  870.                 codestream_length = cio_tell(cio);
  871.                 pixels =(volume->x1 - volume->x0) * (volume->y1 - volume->y0) * (volume->z1 - volume->z0);
  872.                 bitsin = pixels * volume->comps[0].prec;
  873.                 fprintf(stdout, "[RESULT] Volume: %d x %d x %d (x %d bpv)\n Codestream: %d B,  Ratio: %5.3f bpv,  (%5.3f : 1) \n",
  874.                         (volume->x1 - volume->x0),(volume->y1 - volume->y0),(volume->z1 - volume->z0),volume->comps[0].prec,
  875.                         codestream_length, ((double)codestream_length * 8.0/(double)pixels), ((double)bitsin/(8.0*(double)codestream_length)));
  876.  
  877.                 /* write the buffer to disk */
  878.                 f = fopen(parameters.outfile, "wb");
  879.                 if (!f) {
  880.                         fprintf(stdout, "[ERROR] Failed to open %s for writing\n", parameters.outfile);
  881.                         return 1;
  882.                 }
  883.                 fwrite(cio->buffer, 1, codestream_length, f);
  884.                 fclose(f);
  885.  
  886.                 /* close and free the byte stream */
  887.                 opj_cio_close(cio);
  888.  
  889.                 /* free remaining compression structures */
  890.                 opj_destroy_compress(cinfo);
  891.         } else {
  892.                 fprintf(stdout, "[ERROR] Cod_format != JP3d !!! \n");
  893.                 return 1;
  894.         }
  895.  
  896.         /* free user parameters structure */
  897.         if(delete_comment) {
  898.                 if(parameters.cp_comment) free(parameters.cp_comment);
  899.         }
  900.         if(parameters.cp_matrice) free(parameters.cp_matrice);
  901.  
  902.         /* free volume data */
  903.         opj_volume_destroy(volume);
  904.        
  905.         return 0;
  906. }
  907.