2 * Copyright (c) 2001-2003, David Janssens
\r
3 * Copyright (c) 2002-2003, Yannick Verschueren
\r
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
\r
5 * Copyright (c) 2005, Herv� Drolon, FreeImage Team
\r
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
\r
7 * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
\r
8 * All rights reserved.
\r
10 * Redistribution and use in source and binary forms, with or without
\r
11 * modification, are permitted provided that the following conditions
\r
13 * 1. Redistributions of source code must retain the above copyright
\r
14 * notice, this list of conditions and the following disclaimer.
\r
15 * 2. Redistributions in binary form must reproduce the above copyright
\r
16 * notice, this list of conditions and the following disclaimer in the
\r
17 * documentation and/or other materials provided with the distribution.
\r
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
\r
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
29 * POSSIBILITY OF SUCH DAMAGE.
\r
35 #include "../libjp3dvm/openjpeg.h"
\r
37 #include "convert.h"
\r
40 #define stricmp strcasecmp
\r
41 #define strnicmp strncasecmp
\r
44 /* ----------------------------------------------------------------------- */
\r
46 void encode_help_display() {
\r
47 fprintf(stdout,"List of parameters for the JPEG2000 Part 10 encoder:\n");
\r
48 fprintf(stdout,"------------\n");
\r
49 fprintf(stdout,"\n");
\r
50 fprintf(stdout,"Required Parameters (except with -h):\n");
\r
51 fprintf(stdout,"\n");
\r
52 fprintf(stdout,"-i : source file (-i source.bin or source*.pgx) \n");
\r
53 fprintf(stdout,"\n");
\r
54 fprintf(stdout,"-m : source characteristics file (-m imgfile.img) \n");
\r
55 fprintf(stdout,"\n");
\r
56 fprintf(stdout,"-o : destination file (-o dest.jp3d) \n");
\r
57 fprintf(stdout,"\n");
\r
58 fprintf(stdout,"Optional Parameters:\n");
\r
59 fprintf(stdout,"\n");
\r
60 fprintf(stdout,"-h : display the help information \n ");
\r
61 fprintf(stdout,"\n");
\r
62 fprintf(stdout,"-n : number of resolutions (-n 3,3,3) \n");
\r
63 fprintf(stdout,"\n");
\r
64 fprintf(stdout,"-I : use the irreversible transforms: ICT + DWT 9-7 (-I) \n");
\r
65 fprintf(stdout,"\n");
\r
66 fprintf(stdout,"-C : coding algorithm (-C 2EB) [2EB, 3EB] \n");
\r
67 fprintf(stdout,"\n");
\r
68 fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n ");
\r
69 fprintf(stdout," - The rate specified for each quality level is the desired compression factor.\n");
\r
70 fprintf(stdout," - Rate 1 means lossless compression\n");
\r
71 fprintf(stdout," (options -r and -q cannot be used together)\n ");
\r
72 fprintf(stdout,"\n");
\r
73 fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n ");
\r
74 fprintf(stdout," (options -r and -q cannot be used together)\n ");
\r
75 fprintf(stdout,"\n");
\r
76 fprintf(stdout,"-b : size of code block (-b 32,32,32) \n");
\r
77 fprintf(stdout,"\n");
\r
78 fprintf(stdout,"-c : size of precinct (-c 128,128,128) \n");
\r
79 fprintf(stdout,"\n");
\r
80 fprintf(stdout,"-t : size of tile (-t 512,512,512) \n");
\r
81 fprintf(stdout,"\n");
\r
82 fprintf(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
\r
83 fprintf(stdout,"\n");
\r
84 fprintf(stdout,"-s : subsampling factor (-s 2,2,2) [-s X,Y,Z] \n");
\r
85 fprintf(stdout," - Remark: subsampling bigger than 2 can produce error\n");
\r
86 fprintf(stdout,"\n");
\r
87 fprintf(stdout,"-SOP : write SOP marker before each packet \n");
\r
88 fprintf(stdout,"\n");
\r
89 fprintf(stdout,"-EPH : write EPH marker after each header packet \n");
\r
90 fprintf(stdout,"\n");
\r
91 fprintf(stdout,"-M : code-block style (-M 0) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
\r
92 fprintf(stdout," 8=VSC 16=PTERM 32=SEGSYM 64=3DCTXT] \n");
\r
93 fprintf(stdout," Indicate multiple modes by adding their values. \n");
\r
94 fprintf(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
\r
95 fprintf(stdout,"\n");
\r
96 fprintf(stdout,"-D : define DC offset (-D 12) \n");
\r
97 fprintf(stdout,"\n");
\r
98 fprintf(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n");
\r
99 fprintf(stdout,"\n");
\r
100 fprintf(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
\r
101 fprintf(stdout," for component c=%%d [%%d = 0,1,2]\n");
\r
102 fprintf(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
\r
103 fprintf(stdout,"\n");
\r
104 fprintf(stdout,"-d : offset of the origin of the volume (-d 150,300,100) \n");
\r
105 fprintf(stdout,"\n");
\r
106 fprintf(stdout,"-l : offset of the origin of the tiles (-l 100,75,25) \n");
\r
107 fprintf(stdout,"\n");
\r
108 fprintf(stdout,"\n");
\r
109 fprintf(stdout,"DEFAULT CODING:\n");
\r
110 fprintf(stdout,"------------\n");
\r
111 fprintf(stdout,"\n");
\r
112 fprintf(stdout," * Lossless\n");
\r
113 fprintf(stdout," * 1 tile\n");
\r
114 fprintf(stdout," * Size of precinct : 2^15 x 2^15 x 2^15 (means 1 precinct)\n");
\r
115 fprintf(stdout," * Size of code-block : 64 x 64 x 64\n");
\r
116 fprintf(stdout," * Number of resolutions in x, y and z axis: 3\n");
\r
117 fprintf(stdout," * No SOP marker in the codestream\n");
\r
118 fprintf(stdout," * No EPH marker in the codestream\n");
\r
119 fprintf(stdout," * No sub-sampling in x, y or z direction\n");
\r
120 fprintf(stdout," * No mode switch activated\n");
\r
121 fprintf(stdout," * Progression order: LRCP\n");
\r
122 fprintf(stdout," * No index file\n");
\r
123 fprintf(stdout," * No ROI upshifted\n");
\r
124 fprintf(stdout," * No offset of the origin of the volume\n");
\r
125 fprintf(stdout," * No offset of the origin of the tiles\n");
\r
126 fprintf(stdout," * Reversible DWT 5-3 on each 2D slice\n");
\r
127 fprintf(stdout," * Coding algorithm: 2D-EBCOT \n");
\r
128 fprintf(stdout,"\n");
\r
129 fprintf(stdout,"REMARKS:\n");
\r
130 fprintf(stdout,"---------\n");
\r
131 fprintf(stdout,"\n");
\r
132 fprintf(stdout,"- The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
\r
133 fprintf(stdout,"- COD and QCD markers will never appear in the tile_header.\n");
\r
134 fprintf(stdout,"\n");
\r
135 fprintf(stdout,"- You need enough disk space memory (twice the original) to encode \n");
\r
136 fprintf(stdout,"the volume,i.e. for a 1.5 GB volume you need a minimum of 3GB of disk memory)\n");
\r
137 fprintf(stdout,"\n");
\r
138 fprintf(stdout,"- When loading *.pgx files, a relative path to directory is needed for input argument \n");
\r
139 fprintf(stdout," followed by the common prefix of the slices and a '*' character representing sequential numeration.\n");
\r
140 fprintf(stdout,"( -i relativepath/slices*.pgx )\n");
\r
141 fprintf(stdout,"\n");
\r
142 fprintf(stdout," - The index file has the structure below:\n");
\r
143 fprintf(stdout,"\n");
\r
144 fprintf(stdout,"\t Image_height Image_width Image_depth\n");
\r
145 fprintf(stdout,"\t Progression order: 0 (LRCP)\n");
\r
146 fprintf(stdout,"\t Tiles_size_X Tiles_size_Y Tiles_size_Z\n");
\r
147 fprintf(stdout,"\t Components_nb\n");
\r
148 fprintf(stdout,"\t Layers_nb\n");
\r
149 fprintf(stdout,"\t Decomposition_levels\n");
\r
150 fprintf(stdout,"\t [Precincts_size_X_res_Nr Precincts_size_Y_res_Nr Precincts_size_Z_res_Nr]\n\t ...\n");
\r
151 fprintf(stdout,"\t [Precincts_size_X_res_0 Precincts_size_Y_res_0 Precincts_size_Z_res_0]\n");
\r
152 fprintf(stdout,"\t Main_header_end_position\n");
\r
153 fprintf(stdout,"\t Codestream_size\n");
\r
154 fprintf(stdout,"\t Tile_0 [start_pos end_header end_pos TotalDisto NumPix MaxMSE]\n");
\r
155 fprintf(stdout,"\t ...\n");
\r
156 fprintf(stdout,"\t Tile_Nt [ '' '' '' '' '' '' ]\n");
\r
157 fprintf(stdout,"\t Tpacket_0 [Tile layer res. comp. prec. start_pos end_pos disto]\n");
\r
158 fprintf(stdout,"\t ...\n");
\r
159 fprintf(stdout,"\t Tpacket_Np ['' '' '' '' '' '' '' '' ]\n");
\r
160 fprintf(stdout,"\t MaxDisto\n");
\r
161 fprintf(stdout,"\t TotalDisto\n\n");
\r
162 fprintf(stdout,"\n");
\r
166 OPJ_PROG_ORDER give_progression(char progression[4]) {
\r
167 if(strncmp(progression, "LRCP", 4) == 0) {
\r
170 if(strncmp(progression, "RLCP", 4) == 0) {
\r
173 if(strncmp(progression, "RPCL", 4) == 0) {
\r
176 if(strncmp(progression, "PCRL", 4) == 0) {
\r
179 if(strncmp(progression, "CPRL", 4) == 0) {
\r
183 return PROG_UNKNOWN;
\r
186 OPJ_TRANSFORM give_transform(char transform[4]) {
\r
187 if(strncmp(transform, "2DWT", 4) == 0) {
\r
190 if(strncmp(transform, "3DWT", 4) == 0) {
\r
193 return TRF_UNKNOWN;
\r
196 OPJ_ENTROPY_CODING give_coding(char coding[3]) {
\r
198 if(strncmp(coding, "2EB", 3) == 0) {
\r
201 if(strncmp(coding, "3EB", 3) == 0) {
\r
204 /*if(strncmp(coding, "2GR", 3) == 0) {
\r
207 if(strncmp(coding, "3GR", 3) == 0) {
\r
211 return ENCOD_UNKNOWN;
\r
214 int get_file_format(char *filename) {
\r
216 static const char *extension[] = {"pgx", "bin", "img", "j3d", "jp3d", "j2k"};
\r
217 static const int format[] = { PGX_DFMT, BIN_DFMT, IMG_DFMT, J3D_CFMT, J3D_CFMT, J2K_CFMT};
\r
218 char * ext = strrchr(filename, '.');
\r
221 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
\r
222 if(strnicmp(ext, extension[i], 3) == 0) {
\r
231 /* ------------------------------------------------------------------------------------ */
\r
233 int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters) {
\r
236 /* parse the command line */
\r
239 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");
\r
243 case 'i': /* input file */
\r
245 char *infile = optarg;
\r
246 parameters->decod_format = get_file_format(infile);
\r
247 switch(parameters->decod_format) {
\r
253 fprintf(stdout, "[ERROR] Unrecognized format for infile : %s [accept only *.pgx or *.bin] !!\n\n", infile);
\r
257 strncpy(parameters->infile, infile, MAX_PATH);
\r
258 fprintf(stdout, "[INFO] Infile: %s \n", parameters->infile);
\r
263 /* ----------------------------------------------------- */
\r
264 case 'm': /* input IMG file */
\r
266 char *imgfile = optarg;
\r
267 int imgformat = get_file_format(imgfile);
\r
268 switch(imgformat) {
\r
272 fprintf(stdout, "[ERROR] Unrecognized format for imgfile : %s [accept only *.img] !!\n\n", imgfile);
\r
276 strncpy(parameters->imgfile, imgfile, MAX_PATH);
\r
277 fprintf(stdout, "[INFO] Imgfile: %s Format: %d\n", parameters->imgfile, imgformat);
\r
281 /* ----------------------------------------------------- */
\r
282 case 'o': /* output file */
\r
284 char *outfile = optarg;
\r
285 parameters->cod_format = get_file_format(outfile);
\r
286 switch(parameters->cod_format) {
\r
292 fprintf(stdout, "[ERROR] Unknown output format volume %s [only *.j2k, *.lse3d or *.jp3d]!! \n", outfile);
\r
296 strncpy(parameters->outfile, outfile, MAX_PATH);
\r
297 fprintf(stdout, "[INFO] Outfile: %s \n", parameters->outfile);
\r
301 /* ----------------------------------------------------- */
\r
303 case 'r': /* define compression rates for each layer */
\r
306 while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) == 1) {
\r
307 parameters->tcp_numlayers++;
\r
308 while (*s && *s != ',') {
\r
315 parameters->cp_disto_alloc = 1;
\r
319 /* ----------------------------------------------------- */
\r
321 case 'q': /* define distorsion (PSNR) for each layer */
\r
324 while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers]) == 1) {
\r
325 parameters->tcp_numlayers++;
\r
326 while (*s && *s != ',') {
\r
333 parameters->cp_fixed_quality = 1;
\r
337 /* ----------------------------------------------------- */
\r
341 fprintf(stdout, "/---------------------------------------------------\\\n");
\r
342 fprintf(stdout, "| Fixed layer allocation option not implemented !! |\n");
\r
343 fprintf(stdout, "\\---------------------------------------------------/\n");
\r
344 /*int *row = NULL, *col = NULL;
\r
345 int numlayers = 0, matrix_width = 0;
\r
348 sscanf(s, "%d", &numlayers);
\r
353 parameters->tcp_numlayers = numlayers;
\r
354 matrix_width = parameters->numresolution[0] + parameters->numresolution[1] + parameters->numresolution[2];
\r
355 parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
\r
358 for (i = 0; i < numlayers; i++) {
\r
359 row = ¶meters->cp_matrice[i * matrix_width];
\r
361 parameters->tcp_rates[i] = 1;
\r
362 sscanf(s, "%d,", &col[0]);
\r
368 for (j = 1; j < matrix_width; j++) {
\r
370 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
\r
379 if (i < numlayers - 1)
\r
382 parameters->cp_fixed_alloc = 1; */
\r
386 /* ----------------------------------------------------- */
\r
388 case 't': /* tiles */
\r
390 if (sscanf(optarg, "%d,%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy, ¶meters->cp_tdz) !=3) {
\r
391 fprintf(stdout, "[ERROR] '-t' 'dimensions of tiles' argument error ! [-t tdx,tdy,tdz]\n");
\r
394 parameters->tile_size_on = true;
\r
398 /* ----------------------------------------------------- */
\r
400 case 'n': /* resolution */
\r
403 aux = sscanf(optarg, "%d,%d,%d", ¶meters->numresolution[0], ¶meters->numresolution[1], ¶meters->numresolution[2]);
\r
405 parameters->numresolution[2] = 1;
\r
406 else if (aux == 1) {
\r
407 parameters->numresolution[1] = parameters->numresolution[0];
\r
408 parameters->numresolution[2] = 1;
\r
409 }else if (aux == 0){
\r
410 parameters->numresolution[0] = 1;
\r
411 parameters->numresolution[1] = 1;
\r
412 parameters->numresolution[2] = 1;
\r
417 /* ----------------------------------------------------- */
\r
418 case 'c': /* precinct dimension */
\r
426 aux = sscanf(s, "[%d,%d,%d]%c", ¶meters->prct_init[0][res_spec], ¶meters->prct_init[1][res_spec], ¶meters->prct_init[2][res_spec], &sep);
\r
427 if (sep == ',' && aux != 4) {
\r
428 fprintf(stdout, "[ERROR] '-c' 'dimensions of precincts' argument error ! [-c [prcx_res0,prcy_res0,prcz_res0],...,[prcx_resN,prcy_resN,prcz_resN]]\n");
\r
431 parameters->csty |= 0x01;
\r
433 s = strpbrk(s, "]") + 2;
\r
435 while (sep == ',');
\r
436 parameters->res_spec = res_spec; /* number of precinct size specifications */
\r
440 /* ----------------------------------------------------- */
\r
442 case 'b': /* code-block dimension */
\r
444 int cblockw_init = 0, cblockh_init = 0, cblockl_init = 0;
\r
445 if (sscanf(optarg, "%d,%d,%d", &cblockw_init, &cblockh_init, &cblockl_init) != 3) {
\r
446 fprintf(stdout, "[ERROR] '-b' 'dimensions of codeblocks' argument error ! [-b cblkx,cblky,cblkz]\n");
\r
449 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) {
\r
450 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");
\r
453 parameters->cblock_init[0] = cblockw_init;
\r
454 parameters->cblock_init[1] = cblockh_init;
\r
455 parameters->cblock_init[2] = cblockl_init;
\r
459 /* ----------------------------------------------------- */
\r
461 case 'x': /* creation of index file */
\r
463 char *index = optarg;
\r
464 strncpy(parameters->index, index, MAX_PATH);
\r
465 parameters->index_on = 1;
\r
469 /* ----------------------------------------------------- */
\r
471 case 'p': /* progression order */
\r
473 char progression[4];
\r
475 strncpy(progression, optarg, 4);
\r
476 parameters->prog_order = give_progression(progression);
\r
477 if (parameters->prog_order == -1) {
\r
478 fprintf(stdout, "[ERROR] Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
\r
484 /* ----------------------------------------------------- */
\r
486 case 's': /* subsampling factor */
\r
488 if (sscanf(optarg, "%d,%d,%d", ¶meters->subsampling_dx, ¶meters->subsampling_dy, ¶meters->subsampling_dz) != 2) {
\r
489 fprintf(stdout, "[ERROR] '-s' sub-sampling argument error ! [-s dx,dy,dz]\n");
\r
495 /* ----------------------------------------------------- */
\r
497 case 'd': /* coordonnate of the reference grid */
\r
499 if (sscanf(optarg, "%d,%d,%d", ¶meters->volume_offset_x0, ¶meters->volume_offset_y0, ¶meters->volume_offset_z0) != 3) {
\r
500 fprintf(stdout, "[ERROR] -d 'coordonnate of the reference grid' argument error !! [-d x0,y0,z0]\n");
\r
506 /* ----------------------------------------------------- */
\r
508 case 'h': /* display an help description */
\r
510 encode_help_display();
\r
515 /* ----------------------------------------------------- */
\r
517 case 'P': /* POC */
\r
519 int numpocs = 0; /* number of progression order change (POC) default 0 */
\r
520 opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
\r
523 POC = parameters->POC;
\r
525 fprintf(stdout, "/----------------------------------\\\n");
\r
526 fprintf(stdout, "| POC option not fully tested !! |\n");
\r
527 fprintf(stdout, "\\----------------------------------/\n");
\r
529 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
\r
530 &POC[numpocs].resno0, &POC[numpocs].compno0,
\r
531 &POC[numpocs].layno1, &POC[numpocs].resno1,
\r
532 &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
\r
533 POC[numpocs].prg = give_progression(POC[numpocs].progorder);
\r
534 /* POC[numpocs].tile; */
\r
536 while (*s && *s != '/') {
\r
544 parameters->numpocs = numpocs;
\r
548 /* ------------------------------------------------------ */
\r
550 case 'S': /* SOP marker */
\r
552 parameters->csty |= 0x02;
\r
556 /* ------------------------------------------------------ */
\r
558 case 'E': /* EPH marker */
\r
560 parameters->csty |= 0x04;
\r
564 /* ------------------------------------------------------ */
\r
566 case 'M': /* Codification mode switch */
\r
568 fprintf(stdout, "[INFO] Mode switch option not fully tested !!\n");
\r
570 if (sscanf(optarg, "%d", &value) == 1) {
\r
571 for (i = 0; i <= 6; i++) {
\r
572 int cache = value & (1 << i);
\r
574 parameters->mode |= (1 << i);
\r
580 /* ------------------------------------------------------ */
\r
582 case 'D': /* DCO */
\r
584 if (sscanf(optarg, "%d", ¶meters->dcoffset) != 1) {
\r
585 fprintf(stdout, "[ERROR] DC offset error !! [-D %d]\n",parameters->dcoffset);
\r
591 /* ------------------------------------------------------ */
\r
593 case 'R': /* ROI */
\r
595 if (sscanf(optarg, "OI:c=%d,U=%d", ¶meters->roi_compno, ¶meters->roi_shift) != 2) {
\r
596 fprintf(stdout, "[ERROR] ROI error !! [-ROI:c='compno',U='shift']\n");
\r
602 /* ------------------------------------------------------ */
\r
604 case 'l': /* Tile offset */
\r
606 if (sscanf(optarg, "%d,%d,%d", ¶meters->cp_tx0, ¶meters->cp_ty0, ¶meters->cp_tz0) != 3) {
\r
607 fprintf(stdout, "[ERROR] -l 'tile offset' argument error !! [-l X0,Y0,Z0]");
\r
613 /* ------------------------------------------------------
\r
615 case 'T': // Tranformation of original data (2D-DWT/3D-DWT/3D-RLS/2D-DWT+1D-RLS)
\r
619 strncpy(transform, optarg, 4);
\r
620 parameters->transform_format = give_transform(transform);
\r
621 if (parameters->transform_format == -1) {
\r
622 fprintf(stdout, "[ERROR] -T 'Transform domain' argument error !! [-T 2DWT, 3DWT, 3RLS or 3LSE only]");
\r
628 ------------------------------------------------------ */
\r
630 case 'C': /* Coding of transformed data */
\r
634 strncpy(coding, optarg, 3);
\r
635 parameters->encoding_format = give_coding(coding);
\r
636 if (parameters->encoding_format == -1) {
\r
637 fprintf(stdout, "[ERROR] -C 'Coding algorithm' argument error !! [-C 2EB, 3EB, 2GR, 3GR or GRI only]");
\r
643 /* ------------------------------------------------------ */
\r
645 case 'I': /* reversible or not */
\r
647 parameters->irreversible = 1;
\r
652 fprintf(stdout, "[ERROR] This option is not valid \"-%c %s\"\n", c, optarg);
\r
657 /* check for possible errors */
\r
659 if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
\r
660 fprintf(stdout, "usage: jp3d_vm_enc -i volume-file -o jp3d-file (+ options)\n");
\r
664 if((parameters->decod_format == BIN_DFMT) && (parameters->imgfile[0] == 0)) {
\r
665 fprintf(stdout, "usage: jp3d_vm_enc -i bin-volume-file -m img-file -o jp3d-file (+ options)\n");
\r
669 if((parameters->decod_format != BIN_DFMT) && (parameters->decod_format != PGX_DFMT) && (parameters->decod_format != IMG_DFMT)) {
\r
670 fprintf(stdout, "usage: jp3d_vm_enc -i input-volume-file [*.bin,*.pgx,*.img] -o jp3d-file [*.jp3d,*.j2k] (+ options)\n");
\r
673 if((parameters->cod_format != J3D_CFMT) && (parameters->cod_format != J2K_CFMT)) {
\r
674 fprintf(stdout, "usage: jp3d_vm_enc -i input-volume-file [*.bin,*.pgx,*.img] -o jp3d-file [*.jp3d,*.j2k] (+ options)\n");
\r
678 if((parameters->encoding_format == ENCOD_2GR || parameters->encoding_format == ENCOD_3GR) && parameters->transform_format != TRF_3D_LSE && parameters->transform_format != TRF_3D_RLS) {
\r
679 fprintf(stdout, "[ERROR] Entropy coding options -C [2GR,3GR] are only compatible with predictive-based transform algorithms: -T [3RLS,3LSE].\n");
\r
682 if (parameters->encoding_format == ENCOD_3EB)
\r
683 parameters->mode |= (1 << 6);
\r
685 if ((parameters->mode >> 6) & 1) {
\r
686 parameters->encoding_format = ENCOD_3EB;
\r
689 if((parameters->numresolution[2] == 0 || (parameters->numresolution[1] == 0) || (parameters->numresolution[0] == 0))) {
\r
690 fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions must be greater than 1 in order to perform DWT.\n");
\r
693 if (parameters->numresolution[1] != parameters->numresolution[0]) {
\r
694 fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions in X and Y axis must be the same in this implementation.\n");
\r
698 if (parameters->numresolution[2] > parameters->numresolution[0]) {
\r
699 fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions in Z axis must be lower than in X-Y axis.\n");
\r
703 if (parameters->dcoffset >= 128 && parameters->dcoffset <= -128) {
\r
704 fprintf(stdout, "[ERROR] -D 'DC offset' argument error ! Value must be -128<=DCO<=128.\n");
\r
708 if(parameters->numresolution[2] != 1) {
\r
709 parameters->transform_format = TRF_3D_DWT;
\r
710 //fprintf(stdout, "[Warning] Resolution level in axial dim > 1 : 3D-DWT will be performed... \n");
\r
711 } else if (parameters->numresolution[2] == 1) {
\r
712 parameters->transform_format = TRF_2D_DWT;
\r
713 //fprintf(stdout, "[Warning] Resolution level in axial dim == 1 : 2D-DWT will be performed... \n");
\r
716 if ((parameters->cod_format == J2K_CFMT) && (parameters->transform_format != TRF_2D_DWT || parameters->encoding_format != ENCOD_2EB)) {
\r
717 fprintf(stdout, "[WARNING] Incompatible options -o *.j2k and defined transform or encoding algorithm. Latter will be ignored\n");
\r
718 parameters->transform_format = TRF_2D_DWT;
\r
719 parameters->encoding_format = ENCOD_2EB;
\r
722 if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality) && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_quality))) {
\r
723 fprintf(stdout, "[ERROR] Options -r and -q cannot be used together !!\n");
\r
725 } /* mod fixed_quality */
\r
727 /* if no rate entered, lossless by default */
\r
728 if (parameters->tcp_numlayers == 0) {
\r
729 parameters->tcp_rates[0] = 0.0; /* MOD antonin : losslessbug */
\r
730 parameters->tcp_numlayers++;
\r
731 parameters->cp_disto_alloc = 1;
\r
734 if((parameters->cp_tx0 > parameters->volume_offset_x0) || (parameters->cp_ty0 > parameters->volume_offset_y0) || (parameters->cp_tz0 > parameters->volume_offset_z0)) {
\r
735 fprintf(stdout, "[ERROR] Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) TZO(%d)<=IMG_Z0(%d)\n",
\r
736 parameters->cp_tx0, parameters->volume_offset_x0, parameters->cp_ty0, parameters->volume_offset_y0,
\r
737 parameters->cp_tz0, parameters->volume_offset_z0);
\r
741 for (i = 0; i < parameters->numpocs; i++) {
\r
742 if (parameters->POC[i].prg == -1) {
\r
743 fprintf(stdout,"[ERROR] Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",i + 1);
\r
749 /* -------------------------------------------------------------------------- */
\r
752 sample error callback expecting a FILE* client object
\r
754 void error_callback(const char *msg, void *client_data) {
\r
755 FILE *stream = (FILE*)client_data;
\r
756 fprintf(stream, "[ERROR] %s", msg);
\r
759 sample warning callback expecting a FILE* client object
\r
761 void warning_callback(const char *msg, void *client_data) {
\r
762 FILE *stream = (FILE*)client_data;
\r
763 fprintf(stream, "[WARNING] %s", msg);
\r
766 sample debug callback expecting a FILE* client object
\r
768 void info_callback(const char *msg, void *client_data) {
\r
769 FILE *stream = (FILE*)client_data;
\r
770 fprintf(stream, "[INFO] %s", msg);
\r
773 /* -------------------------------------------------------------------------- */
\r
775 int main(int argc, char **argv) {
\r
777 bool delete_comment = true;
\r
778 opj_cparameters_t parameters; /* compression parameters */
\r
779 opj_event_mgr_t event_mgr; /* event manager */
\r
780 opj_volume_t *volume = NULL;
\r
783 configure the event callbacks (not required)
\r
784 setting of each callback is optionnal
\r
786 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
\r
787 event_mgr.error_handler = error_callback;
\r
788 event_mgr.warning_handler = warning_callback;
\r
789 event_mgr.info_handler = info_callback;
\r
791 /* set encoding parameters to default values */
\r
792 opj_set_default_encoder_parameters(¶meters);
\r
794 /* parse input and get user encoding parameters */
\r
795 if(parse_cmdline_encoder(argc, argv, ¶meters) == 1) {
\r
799 if(parameters.cp_comment == NULL) {
\r
800 parameters.cp_comment = "Created by OpenJPEG version JP3D";
\r
801 /* no need to delete parameters.cp_comment on exit */
\r
802 delete_comment = false;
\r
805 /* encode the destination volume */
\r
806 /* ---------------------------- */
\r
807 if (parameters.cod_format == J3D_CFMT || parameters.cod_format == J2K_CFMT) {
\r
808 int codestream_length, pixels, bitsin;
\r
809 opj_cio_t *cio = NULL;
\r
811 opj_cinfo_t* cinfo = NULL;
\r
813 /* decode the source volume */
\r
814 /* ----------------------- */
\r
815 switch (parameters.decod_format) {
\r
817 fprintf(stdout, "[INFO] Loading pgx file(s)\n");
\r
818 volume = pgxtovolume(parameters.infile, ¶meters);
\r
820 fprintf(stdout, "[ERROR] Unable to load pgx files\n");
\r
826 fprintf(stdout, "[INFO] Loading bin file\n");
\r
827 volume = bintovolume(parameters.infile, parameters.imgfile, ¶meters);
\r
829 fprintf(stdout, "[ERROR] Unable to load bin file\n");
\r
835 fprintf(stdout, "[INFO] Loading img file\n");
\r
836 volume = imgtovolume(parameters.infile, ¶meters);
\r
838 fprintf(stderr, "[ERROR] Unable to load img file\n");
\r
844 /* get a JP3D or J2K compressor handle */
\r
845 if (parameters.cod_format == J3D_CFMT)
\r
846 cinfo = opj_create_compress(CODEC_J3D);
\r
847 else if (parameters.cod_format == J2K_CFMT)
\r
848 cinfo = opj_create_compress(CODEC_J2K);
\r
850 /* catch events using our callbacks and give a local context */
\r
851 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stdout);
\r
853 /* setup the encoder parameters using the current volume and using user parameters */
\r
854 opj_setup_encoder(cinfo, ¶meters, volume);
\r
856 /* open a byte stream for writing */
\r
857 /* allocate memory for all tiles */
\r
858 cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
\r
860 /* encode the volume */
\r
861 //fprintf(stdout, "[INFO] Encode the volume\n");
\r
862 bSuccess = opj_encode(cinfo, cio, volume, parameters.index);
\r
864 opj_cio_close(cio);
\r
865 fprintf(stdout, "[ERROR] Failed to encode volume\n");
\r
868 codestream_length = cio_tell(cio);
\r
869 pixels =(volume->x1 - volume->x0) * (volume->y1 - volume->y0) * (volume->z1 - volume->z0);
\r
870 bitsin = pixels * volume->comps[0].prec;
\r
871 fprintf(stdout, "[RESULT] Volume: %d x %d x %d (x %d bpv)\n Codestream: %d B, Ratio: %5.3f bpv, (%5.3f : 1) \n",
\r
872 (volume->x1 - volume->x0),(volume->y1 - volume->y0),(volume->z1 - volume->z0),volume->comps[0].prec,
\r
873 codestream_length, ((double)codestream_length * 8.0/(double)pixels), ((double)bitsin/(8.0*(double)codestream_length)));
\r
875 /* write the buffer to disk */
\r
876 f = fopen(parameters.outfile, "wb");
\r
878 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", parameters.outfile);
\r
881 fwrite(cio->buffer, 1, codestream_length, f);
\r
884 /* close and free the byte stream */
\r
885 opj_cio_close(cio);
\r
887 /* free remaining compression structures */
\r
888 opj_destroy_compress(cinfo);
\r
890 fprintf(stdout, "[ERROR] Cod_format != JP3d !!! \n");
\r
894 /* free user parameters structure */
\r
895 if(delete_comment) {
\r
896 if(parameters.cp_comment) free(parameters.cp_comment);
\r
898 if(parameters.cp_matrice) free(parameters.cp_matrice);
\r
900 /* free volume data */
\r
901 opj_volume_destroy(volume);
\r