2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
16 * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
17 * Copyright (c) 2012, CS Systemes d'Information, France
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
49 #include "windirent.h"
56 #define strcasecmp _stricmp
57 #define strncasecmp _strnicmp
61 #include <sys/resource.h>
62 #include <sys/times.h>
65 #include "opj_apps_config.h"
67 #include "opj_getopt.h"
71 #include "format_defs.h"
72 #include "opj_string.h"
74 typedef struct dircnt {
75 /** Buffer for holding images read from Directory*/
77 /** Pointer to the buffer*/
81 typedef struct img_folder {
82 /** The directory path of the folder containing input images*/
88 /** Enable Cod Format for output*/
92 static void encode_help_display(void)
95 "\nThis is the opj_compress utility from the OpenJPEG project.\n"
96 "It compresses various image formats with the JPEG 2000 algorithm.\n"
97 "It has been compiled against openjp2 library v%s.\n\n", opj_version());
99 fprintf(stdout, "Default encoding options:\n");
100 fprintf(stdout, "-------------------------\n");
101 fprintf(stdout, "\n");
102 fprintf(stdout, " * Lossless\n");
103 fprintf(stdout, " * 1 tile\n");
104 fprintf(stdout, " * RGB->YCC conversion if at least 3 components\n");
105 fprintf(stdout, " * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
106 fprintf(stdout, " * Size of code-block : 64 x 64\n");
107 fprintf(stdout, " * Number of resolutions: 6\n");
108 fprintf(stdout, " * No SOP marker in the codestream\n");
109 fprintf(stdout, " * No EPH marker in the codestream\n");
110 fprintf(stdout, " * No sub-sampling in x or y direction\n");
111 fprintf(stdout, " * No mode switch activated\n");
112 fprintf(stdout, " * Progression order: LRCP\n");
114 fprintf(stdout, " * No index file\n");
115 #endif /* FIXME_INDEX */
116 fprintf(stdout, " * No ROI upshifted\n");
117 fprintf(stdout, " * No offset of the origin of the image\n");
118 fprintf(stdout, " * No offset of the origin of the tiles\n");
119 fprintf(stdout, " * Reversible DWT 5-3\n");
122 fprintf(stdout, " * No JPWL protection\n");
123 #endif /* USE_JPWL */
125 fprintf(stdout, "\n");
127 fprintf(stdout, "Note:\n");
128 fprintf(stdout, "-----\n");
129 fprintf(stdout, "\n");
131 "The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
132 fprintf(stdout, "COD and QCD never appear in the tile_header.\n");
133 fprintf(stdout, "\n");
135 fprintf(stdout, "Parameters:\n");
136 fprintf(stdout, "-----------\n");
137 fprintf(stdout, "\n");
138 fprintf(stdout, "Required Parameters (except with -h):\n");
139 fprintf(stdout, "One of the two options -ImgDir or -i must be used\n");
140 fprintf(stdout, "\n");
141 fprintf(stdout, "-i <file>\n");
142 fprintf(stdout, " Input file\n");
144 " Known extensions are <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n");
145 fprintf(stdout, " If used, '-o <file>' must be provided\n");
146 fprintf(stdout, "-o <compressed file>\n");
147 fprintf(stdout, " Output file (accepted extensions are j2k or jp2).\n");
148 fprintf(stdout, "-ImgDir <dir>\n");
149 fprintf(stdout, " Image file Directory path (example ../Images) \n");
150 fprintf(stdout, " When using this option -OutFor must be used\n");
151 fprintf(stdout, "-OutFor <J2K|J2C|JP2>\n");
152 fprintf(stdout, " Output format for compressed files.\n");
153 fprintf(stdout, " Required only if -ImgDir is used\n");
155 "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
156 fprintf(stdout, " Characteristics of the raw input image\n");
158 " If subsampling is omitted, 1x1 is assumed for all components\n");
159 fprintf(stdout, " Example: -F 512,512,3,8,u@1x1:2x2:2x2\n");
161 " for raw 512x512 image with 4:2:0 subsampling\n");
162 fprintf(stdout, " Required only if RAW or RAWL input file is provided.\n");
163 fprintf(stdout, "\n");
164 fprintf(stdout, "Optional Parameters:\n");
165 fprintf(stdout, "\n");
166 fprintf(stdout, "-h\n");
167 fprintf(stdout, " Display the help information.\n");
168 fprintf(stdout, "-r <compression ratio>,<compression ratio>,...\n");
169 fprintf(stdout, " Different compression ratios for successive layers.\n");
171 " The rate specified for each quality level is the desired\n");
172 fprintf(stdout, " compression factor.\n");
173 fprintf(stdout, " Decreasing ratios required.\n");
174 fprintf(stdout, " Example: -r 20,10,1 means \n");
175 fprintf(stdout, " quality layer 1: compress 20x, \n");
176 fprintf(stdout, " quality layer 2: compress 10x \n");
177 fprintf(stdout, " quality layer 3: compress lossless\n");
178 fprintf(stdout, " Options -r and -q cannot be used together.\n");
179 fprintf(stdout, "-q <psnr value>,<psnr value>,<psnr value>,...\n");
180 fprintf(stdout, " Different psnr for successive layers (-q 30,40,50).\n");
181 fprintf(stdout, " Increasing PSNR values required.\n");
182 fprintf(stdout, " Options -r and -q cannot be used together.\n");
183 fprintf(stdout, "-n <number of resolutions>\n");
184 fprintf(stdout, " Number of resolutions.\n");
186 " It corresponds to the number of DWT decompositions +1. \n");
187 fprintf(stdout, " Default: 6.\n");
188 fprintf(stdout, "-b <cblk width>,<cblk height>\n");
190 " Code-block size. The dimension must respect the constraint \n");
192 " defined in the JPEG-2000 standard (no dimension smaller than 4 \n");
194 " or greater than 1024, no code-block with more than 4096 coefficients).\n");
195 fprintf(stdout, " The maximum value authorized is 64x64. \n");
196 fprintf(stdout, " Default: 64x64.\n");
198 "-c [<prec width>,<prec height>],[<prec width>,<prec height>],...\n");
199 fprintf(stdout, " Precinct size. Values specified must be power of 2. \n");
201 " Multiple records may be supplied, in which case the first record refers\n");
203 " to the highest resolution level and subsequent records to lower \n");
205 " resolution levels. The last specified record is right-shifted for each \n");
206 fprintf(stdout, " remaining lower resolution levels.\n");
207 fprintf(stdout, " Default: 215x215 at each resolution.\n");
208 fprintf(stdout, "-t <tile width>,<tile height>\n");
209 fprintf(stdout, " Tile size.\n");
211 " Default: the dimension of the whole image, thus only one tile.\n");
212 fprintf(stdout, "-p <LRCP|RLCP|RPCL|PCRL|CPRL>\n");
213 fprintf(stdout, " Progression order.\n");
214 fprintf(stdout, " Default: LRCP.\n");
215 fprintf(stdout, "-s <subX,subY>\n");
216 fprintf(stdout, " Subsampling factor.\n");
217 fprintf(stdout, " Subsampling bigger than 2 can produce error\n");
218 fprintf(stdout, " Default: no subsampling.\n");
220 "-POC <progression order change>/<progression order change>/...\n");
221 fprintf(stdout, " Progression order change.\n");
223 " The syntax of a progression order change is the following:\n");
225 " T<tile>=<resStart>,<compStart>,<layerEnd>,<resEnd>,<compEnd>,<progOrder>\n");
226 fprintf(stdout, " Example: -POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL\n");
227 fprintf(stdout, "-SOP\n");
228 fprintf(stdout, " Write SOP marker before each packet.\n");
229 fprintf(stdout, "-EPH\n");
230 fprintf(stdout, " Write EPH marker after each header packet.\n");
231 fprintf(stdout, "-M <key value>\n");
232 fprintf(stdout, " Mode switch.\n");
233 fprintf(stdout, " [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
234 fprintf(stdout, " 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)]\n");
235 fprintf(stdout, " Indicate multiple modes by adding their values.\n");
237 " Example: RESTART(4) + RESET(2) + SEGMARK(32) => -M 38\n");
238 fprintf(stdout, "-TP <R|L|C>\n");
239 fprintf(stdout, " Divide packets of every tile into tile-parts.\n");
241 " Division is made by grouping Resolutions (R), Layers (L)\n");
242 fprintf(stdout, " or Components (C).\n");
244 fprintf(stdout, "-x <index file>\n");
245 fprintf(stdout, " Create an index file.\n");
246 #endif /*FIXME_INDEX*/
247 fprintf(stdout, "-ROI c=<component index>,U=<upshifting value>\n");
248 fprintf(stdout, " Quantization indices upshifted for a component. \n");
250 " Warning: This option does not implement the usual ROI (Region of Interest).\n");
252 " It should be understood as a 'Component of Interest'. It offers the \n");
254 " possibility to upshift the value of a component during quantization step.\n");
256 " The value after c= is the component number [0, 1, 2, ...] and the value \n");
258 " after U= is the value of upshifting. U must be in the range [0, 37].\n");
259 fprintf(stdout, "-d <image offset X,image offset Y>\n");
260 fprintf(stdout, " Offset of the origin of the image.\n");
261 fprintf(stdout, "-T <tile offset X,tile offset Y>\n");
262 fprintf(stdout, " Offset of the origin of the tiles.\n");
263 fprintf(stdout, "-I\n");
264 fprintf(stdout, " Use the irreversible DWT 9-7.\n");
265 fprintf(stdout, "-mct <0|1|2>\n");
267 " Explicitly specifies if a Multiple Component Transform has to be used.\n");
268 fprintf(stdout, " 0: no MCT ; 1: RGB->YCC conversion ; 2: custom MCT.\n");
270 " If custom MCT, \"-m\" option has to be used (see hereunder).\n");
272 " By default, RGB->YCC conversion is used if there are 3 components or more,\n");
273 fprintf(stdout, " no conversion otherwise.\n");
274 fprintf(stdout, "-m <file>\n");
276 " Use array-based MCT, values are coma separated, line by line\n");
278 " No specific separators between lines, no space allowed between values.\n");
280 " If this option is used, it automatically sets \"-mct\" option to 2.\n");
281 fprintf(stdout, "-cinema2K <24|48>\n");
282 fprintf(stdout, " Digital Cinema 2K profile compliant codestream.\n");
284 " Need to specify the frames per second for a 2K resolution.\n");
285 fprintf(stdout, " Only 24 or 48 fps are currently allowed.\n");
286 fprintf(stdout, "-cinema4K\n");
287 fprintf(stdout, " Digital Cinema 4K profile compliant codestream.\n");
288 fprintf(stdout, " Frames per second not required. Default value is 24fps.\n");
289 fprintf(stdout, "-jpip\n");
290 fprintf(stdout, " Write jpip codestream index box in JP2 output file.\n");
291 fprintf(stdout, " Currently supports only RPCL order.\n");
292 fprintf(stdout, "-C <comment>\n");
293 fprintf(stdout, " Add <comment> in the comment marker segment.\n");
296 fprintf(stdout, "-W <params>\n");
297 fprintf(stdout, " Adoption of JPWL (Part 11) capabilities (-W params)\n");
299 " The <params> field can be written and repeated in any order:\n");
300 fprintf(stdout, " [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
301 fprintf(stdout, " ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
303 " h selects the header error protection (EPB): 'type' can be\n");
305 " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
307 " if 'tilepart' is absent, it is for main and tile headers\n");
308 fprintf(stdout, " if 'tilepart' is present, it applies from that tile\n");
310 " onwards, up to the next h<> spec, or to the last tilepart\n");
311 fprintf(stdout, " in the codestream (max. %d specs)\n",
312 JPWL_MAX_NO_TILESPECS);
314 " p selects the packet error protection (EEP/UEP with EPBs)\n");
315 fprintf(stdout, " to be applied to raw data: 'type' can be\n");
317 " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
319 " if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
321 " if 'tilepart:pack' is present, it applies from that tile\n");
323 " and that packet onwards, up to the next packet spec\n");
325 " or to the last packet in the last tilepart in the stream\n");
326 fprintf(stdout, " (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS);
328 " s enables sensitivity data insertion (ESD): 'method' can be\n");
330 " [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
331 fprintf(stdout, " 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
332 fprintf(stdout, " if 'tilepart' is absent, it is for main header only\n");
333 fprintf(stdout, " if 'tilepart' is present, it applies from that tile\n");
335 " onwards, up to the next s<> spec, or to the last tilepart\n");
336 fprintf(stdout, " in the codestream (max. %d specs)\n",
337 JPWL_MAX_NO_TILESPECS);
338 fprintf(stdout, " g determines the addressing mode: <range> can be\n");
339 fprintf(stdout, " [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
341 " a determines the size of data addressing: <addr> can be\n");
343 " 2/4 bytes (small/large codestreams). If not set, auto-mode\n");
345 " z determines the size of sensitivity values: <size> can be\n");
347 " 1/2 bytes, for the transformed pseudo-floating point value\n");
348 fprintf(stdout, " ex.:\n");
350 " h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
351 fprintf(stdout, " s0=6,s3=-1,a=0,g=1,z=1\n");
352 fprintf(stdout, " means\n");
354 " predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
356 " CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
357 fprintf(stdout, " UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
359 " UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
360 fprintf(stdout, " UEP rs default for packets of tilepart 1,\n");
361 fprintf(stdout, " no UEP for packets 0 to 19 of tilepart 3,\n");
363 " UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
364 fprintf(stdout, " relative sensitivity ESD for MH,\n");
366 " TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
368 " size of addresses and 1 byte for each sensitivity value\n");
369 fprintf(stdout, " ex.:\n");
370 fprintf(stdout, " h,s,p\n");
371 fprintf(stdout, " means\n");
373 " default protection to headers (MH and TPHs) as well as\n");
374 fprintf(stdout, " data packets, one ESD in MH\n");
376 " N.B.: use the following recommendations when specifying\n");
377 fprintf(stdout, " the JPWL parameters list\n");
379 " - when you use UEP, always pair the 'p' option with 'h'\n");
380 #endif /* USE_JPWL */
382 fprintf(stdout, "\n");
384 fprintf(stdout, "Index structure:\n");
385 fprintf(stdout, "----------------\n");
386 fprintf(stdout, "\n");
387 fprintf(stdout, "Image_height Image_width\n");
388 fprintf(stdout, "progression order\n");
389 fprintf(stdout, "Tiles_size_X Tiles_size_Y\n");
390 fprintf(stdout, "Tiles_nb_X Tiles_nb_Y\n");
391 fprintf(stdout, "Components_nb\n");
392 fprintf(stdout, "Layers_nb\n");
393 fprintf(stdout, "decomposition_levels\n");
394 fprintf(stdout, "[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
395 fprintf(stdout, " [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
396 fprintf(stdout, "Main_header_start_position\n");
397 fprintf(stdout, "Main_header_end_position\n");
398 fprintf(stdout, "Codestream_size\n");
399 fprintf(stdout, "\n");
400 fprintf(stdout, "INFO ON TILES\n");
402 "tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
404 "Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
406 "Tile_1 '' '' '' '' '' '' ''\n");
407 fprintf(stdout, "...\n");
409 "Tile_Nt '' '' '' '' '' '' ''\n");
410 fprintf(stdout, "...\n");
411 fprintf(stdout, "TILE 0 DETAILS\n");
412 fprintf(stdout, "part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
413 fprintf(stdout, "...\n");
414 fprintf(stdout, "Progression_string\n");
416 "pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
418 "Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
419 fprintf(stdout, "...\n");
421 "Tpacket_Np '' '' '' '' '' '' '' ''\n");
422 fprintf(stdout, "MaxDisto\n");
423 fprintf(stdout, "TotalDisto\n\n");
424 #endif /*FIXME_INDEX*/
427 static OPJ_PROG_ORDER give_progression(const char progression[4])
429 if (strncmp(progression, "LRCP", 4) == 0) {
432 if (strncmp(progression, "RLCP", 4) == 0) {
435 if (strncmp(progression, "RPCL", 4) == 0) {
438 if (strncmp(progression, "PCRL", 4) == 0) {
441 if (strncmp(progression, "CPRL", 4) == 0) {
445 return OPJ_PROG_UNKNOWN;
448 static unsigned int get_num_images(char *imgdirpath)
451 struct dirent* content;
452 unsigned int num_images = 0;
454 /*Reading the input images from given input directory*/
456 dir = opendir(imgdirpath);
458 fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
463 while ((content = readdir(dir)) != NULL) {
464 if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
473 static int load_images(dircnt_t *dirptr, char *imgdirpath)
476 struct dirent* content;
479 /*Reading the input images from given input directory*/
481 dir = opendir(imgdirpath);
483 fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
486 fprintf(stderr, "Folder opened successfully\n");
489 while ((content = readdir(dir)) != NULL) {
490 if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
494 strcpy(dirptr->filename[i], content->d_name);
501 static int get_file_format(char *filename)
504 static const char *extension[] = {
505 "pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc"
507 static const int format[] = {
508 PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT
510 char * ext = strrchr(filename, '.');
515 for (i = 0; i < sizeof(format) / sizeof(*format); i++) {
516 if (strcasecmp(ext, extension[i]) == 0) {
523 static char * get_file_name(char *name)
525 char *fname = strtok(name, ".");
529 static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
530 opj_cparameters_t *parameters)
532 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
533 outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
534 char *temp_p, temp1[OPJ_PATH_LEN] = "";
536 strcpy(image_filename, dirptr->filename[imageno]);
537 fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
538 parameters->decod_format = get_file_format(image_filename);
539 if (parameters->decod_format == -1) {
542 sprintf(infilename, "%s/%s", img_fol->imgdirpath, image_filename);
543 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
549 strcpy(temp_ofname, get_file_name(image_filename));
550 while ((temp_p = strtok(NULL, ".")) != NULL) {
551 strcat(temp_ofname, temp1);
552 sprintf(temp1, ".%s", temp_p);
554 if (img_fol->set_out_format == 1) {
555 sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
556 img_fol->out_format);
557 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
565 /* ------------------------------------------------------------------------------------ */
567 static int parse_cmdline_encoder(int argc, char **argv,
568 opj_cparameters_t *parameters,
569 img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename,
570 size_t indexfilename_size)
574 opj_option_t long_option[] = {
575 {"cinema2K", REQ_ARG, NULL, 'w'},
576 {"cinema4K", NO_ARG, NULL, 'y'},
577 {"ImgDir", REQ_ARG, NULL, 'z'},
578 {"TP", REQ_ARG, NULL, 'u'},
579 {"SOP", NO_ARG, NULL, 'S'},
580 {"EPH", NO_ARG, NULL, 'E'},
581 {"OutFor", REQ_ARG, NULL, 'O'},
582 {"POC", REQ_ARG, NULL, 'P'},
583 {"ROI", REQ_ARG, NULL, 'R'},
584 {"jpip", NO_ARG, NULL, 'J'},
585 {"mct", REQ_ARG, NULL, 'Y'}
588 /* parse the command line */
589 const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:"
592 #endif /* USE_JPWL */
595 totlen = sizeof(long_option);
596 img_fol->set_out_format = 0;
597 raw_cp->rawWidth = 0;
600 c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
605 case 'i': { /* input file */
606 char *infile = opj_optarg;
607 parameters->decod_format = get_file_format(infile);
608 switch (parameters->decod_format) {
620 "[ERROR] Unknown input file format: %s \n"
621 " Known file formats are *.pnm, *.pgm, *.ppm, *.pgx, *png, *.bmp, *.tif, *.raw or *.tga\n",
625 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
631 /* ----------------------------------------------------- */
633 case 'o': { /* output file */
634 char *outfile = opj_optarg;
635 parameters->cod_format = get_file_format(outfile);
636 switch (parameters->cod_format) {
642 "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
645 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
652 /* ----------------------------------------------------- */
653 case 'O': { /* output format */
655 char *of = opj_optarg;
656 sprintf(outformat, ".%s", of);
657 img_fol->set_out_format = 1;
658 parameters->cod_format = get_file_format(outformat);
659 switch (parameters->cod_format) {
662 img_fol->out_format = opj_optarg;
665 fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n");
672 /* ----------------------------------------------------- */
675 case 'r': { /* rates rates/distorsion */
676 char *s = opj_optarg;
677 parameters->tcp_numlayers = 0;
678 while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) ==
680 parameters->tcp_numlayers++;
681 while (*s && *s != ',') {
689 parameters->cp_disto_alloc = 1;
693 /* ----------------------------------------------------- */
696 case 'F': { /* Raw image format parameters */
697 OPJ_BOOL wrong = OPJ_FALSE;
702 int width, height, bitdepth, ncomp;
704 OPJ_BOOL raw_signed = OPJ_FALSE;
705 substr2 = strchr(opj_optarg, '@');
706 if (substr2 == NULL) {
707 len = (OPJ_UINT32) strlen(opj_optarg);
709 len = (OPJ_UINT32)(substr2 - opj_optarg);
710 substr2++; /* skip '@' character */
712 substr1 = (char*) malloc((len + 1) * sizeof(char));
713 if (substr1 == NULL) {
716 memcpy(substr1, opj_optarg, len);
718 if (sscanf(substr1, "%d,%d,%d,%d,%c", &width, &height, &ncomp, &bitdepth,
721 raw_signed = OPJ_TRUE;
722 } else if (signo == 'u') {
723 raw_signed = OPJ_FALSE;
734 raw_cp->rawWidth = width;
735 raw_cp->rawHeight = height;
736 raw_cp->rawComp = ncomp;
737 raw_cp->rawBitDepth = bitdepth;
738 raw_cp->rawSigned = raw_signed;
739 raw_cp->rawComps = (raw_comp_cparameters_t*) malloc(((OPJ_UINT32)(
740 ncomp)) * sizeof(raw_comp_cparameters_t));
741 if (raw_cp->rawComps == NULL) {
745 for (compno = 0; compno < ncomp && !wrong; compno++) {
746 if (substr2 == NULL) {
747 raw_cp->rawComps[compno].dx = lastdx;
748 raw_cp->rawComps[compno].dy = lastdy;
751 sep = strchr(substr2, ':');
753 if (sscanf(substr2, "%dx%d", &dx, &dy) == 2) {
756 raw_cp->rawComps[compno].dx = dx;
757 raw_cp->rawComps[compno].dy = dy;
763 if (sscanf(substr2, "%dx%d:%s", &dx, &dy, substr2) == 3) {
764 raw_cp->rawComps[compno].dx = dx;
765 raw_cp->rawComps[compno].dy = dy;
775 fprintf(stderr, "\nError: invalid raw image parameters\n");
776 fprintf(stderr, "Please use the Format option -F:\n");
778 "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
780 "If subsampling is omitted, 1x1 is assumed for all components\n");
782 "Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
783 fprintf(stderr, " for raw 512x512 image with 4:2:0 subsampling\n");
784 fprintf(stderr, "Aborting.\n");
790 /* ----------------------------------------------------- */
792 case 'q': { /* add fixed_quality */
793 char *s = opj_optarg;
794 while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers])
796 parameters->tcp_numlayers++;
797 while (*s && *s != ',') {
805 parameters->cp_fixed_quality = 1;
810 /* ----------------------------------------------------- */
812 case 'f': { /* mod fixed_quality (before : -q) */
813 int *row = NULL, *col = NULL;
814 OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0;
816 char *s = opj_optarg;
817 sscanf(s, "%u", &numlayers);
823 parameters->tcp_numlayers = (int)numlayers;
824 numresolution = (OPJ_UINT32)parameters->numresolution;
825 matrix_width = numresolution * 3;
826 parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
827 if (parameters->cp_matrice == NULL) {
832 for (i = 0; i < numlayers; i++) {
833 row = ¶meters->cp_matrice[i * matrix_width];
835 parameters->tcp_rates[i] = 1;
836 sscanf(s, "%d,", &col[0]);
843 for (j = 1; j < numresolution; j++) {
845 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
857 if (i < numlayers - 1) {
861 parameters->cp_fixed_alloc = 1;
865 /* ----------------------------------------------------- */
867 case 't': { /* tiles */
868 sscanf(opj_optarg, "%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy);
869 parameters->tile_size_on = OPJ_TRUE;
873 /* ----------------------------------------------------- */
875 case 'n': { /* resolution */
876 sscanf(opj_optarg, "%d", ¶meters->numresolution);
880 /* ----------------------------------------------------- */
881 case 'c': { /* precinct dimension */
885 char *s = opj_optarg;
889 ret = sscanf(s, "[%d,%d]%c", ¶meters->prcw_init[res_spec],
890 ¶meters->prch_init[res_spec], &sep);
891 if (!(ret == 2 && sep == 0) && !(ret == 3 && sep == ',')) {
892 fprintf(stderr, "\nError: could not parse precinct dimension: '%s' %x\n", s,
894 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n");
897 parameters->csty |= 0x01;
899 s = strpbrk(s, "]") + 2;
900 } while (sep == ',');
901 parameters->res_spec = res_spec;
905 /* ----------------------------------------------------- */
907 case 'b': { /* code-block dimension */
908 int cblockw_init = 0, cblockh_init = 0;
909 sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init);
910 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
911 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
913 "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
914 " * width*height<=4096\n * 4<=width,height<= 1024\n\n");
917 parameters->cblockw_init = cblockw_init;
918 parameters->cblockh_init = cblockh_init;
922 /* ----------------------------------------------------- */
924 case 'x': { /* creation of index file */
925 if (opj_strcpy_s(indexfilename, indexfilename_size, opj_optarg) != 0) {
928 /* FIXME ADE INDEX >> */
930 "[WARNING] Index file generation is currently broken.\n"
931 " '-x' option ignored.\n");
932 /* << FIXME ADE INDEX */
936 /* ----------------------------------------------------- */
938 case 'p': { /* progression order */
941 strncpy(progression, opj_optarg, 4);
942 parameters->prog_order = give_progression(progression);
943 if (parameters->prog_order == -1) {
944 fprintf(stderr, "Unrecognized progression order "
945 "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
951 /* ----------------------------------------------------- */
953 case 's': { /* subsampling factor */
954 if (sscanf(opj_optarg, "%d,%d", ¶meters->subsampling_dx,
955 ¶meters->subsampling_dy) != 2) {
956 fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
962 /* ----------------------------------------------------- */
964 case 'd': { /* coordonnate of the reference grid */
965 if (sscanf(opj_optarg, "%d,%d", ¶meters->image_offset_x0,
966 ¶meters->image_offset_y0) != 2) {
967 fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
968 "error !! [-d x0,y0]\n");
974 /* ----------------------------------------------------- */
976 case 'h': /* display an help description */
977 encode_help_display();
980 /* ----------------------------------------------------- */
982 case 'P': { /* POC */
983 int numpocs = 0; /* number of progression order change (POC) default 0 */
984 opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
986 char *s = opj_optarg;
987 POC = parameters->POC;
989 while (sscanf(s, "T%u=%u,%u,%u,%u,%u,%4s", &POC[numpocs].tile,
990 &POC[numpocs].resno0, &POC[numpocs].compno0,
991 &POC[numpocs].layno1, &POC[numpocs].resno1,
992 &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
993 POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
995 while (*s && *s != '/') {
1003 parameters->numpocs = (OPJ_UINT32)numpocs;
1007 /* ------------------------------------------------------ */
1009 case 'S': { /* SOP marker */
1010 parameters->csty |= 0x02;
1014 /* ------------------------------------------------------ */
1016 case 'E': { /* EPH marker */
1017 parameters->csty |= 0x04;
1021 /* ------------------------------------------------------ */
1023 case 'M': { /* Mode switch pas tous au point !! */
1025 if (sscanf(opj_optarg, "%d", &value) == 1) {
1026 for (i = 0; i <= 5; i++) {
1027 int cache = value & (1 << i);
1029 parameters->mode |= (1 << i);
1036 /* ------------------------------------------------------ */
1038 case 'R': { /* ROI */
1039 if (sscanf(opj_optarg, "c=%d,U=%d", ¶meters->roi_compno,
1040 ¶meters->roi_shift) != 2) {
1041 fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n");
1047 /* ------------------------------------------------------ */
1049 case 'T': { /* Tile offset */
1050 if (sscanf(opj_optarg, "%d,%d", ¶meters->cp_tx0,
1051 ¶meters->cp_ty0) != 2) {
1052 fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
1058 /* ------------------------------------------------------ */
1060 case 'C': { /* add a comment */
1061 parameters->cp_comment = (char*)malloc(strlen(opj_optarg) + 1);
1062 if (parameters->cp_comment) {
1063 strcpy(parameters->cp_comment, opj_optarg);
1069 /* ------------------------------------------------------ */
1071 case 'I': { /* reversible or not */
1072 parameters->irreversible = 1;
1076 /* ------------------------------------------------------ */
1078 case 'u': { /* Tile part generation*/
1079 parameters->tp_flag = opj_optarg[0];
1080 parameters->tp_on = 1;
1084 /* ------------------------------------------------------ */
1086 case 'z': { /* Image Directory path */
1087 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
1088 if (img_fol->imgdirpath == NULL) {
1091 strcpy(img_fol->imgdirpath, opj_optarg);
1092 img_fol->set_imgdir = 1;
1096 /* ------------------------------------------------------ */
1098 case 'w': { /* Digital Cinema 2K profile compliance*/
1100 sscanf(opj_optarg, "%d", &fps);
1102 parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
1103 parameters->max_comp_size = OPJ_CINEMA_24_COMP;
1104 parameters->max_cs_size = OPJ_CINEMA_24_CS;
1105 } else if (fps == 48) {
1106 parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
1107 parameters->max_comp_size = OPJ_CINEMA_48_COMP;
1108 parameters->max_cs_size = OPJ_CINEMA_48_CS;
1110 fprintf(stderr, "Incorrect value!! must be 24 or 48\n");
1113 fprintf(stdout, "CINEMA 2K profile activated\n"
1114 "Other options specified could be overridden\n");
1119 /* ------------------------------------------------------ */
1121 case 'y': { /* Digital Cinema 4K profile compliance*/
1122 parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
1123 fprintf(stdout, "CINEMA 4K profile activated\n"
1124 "Other options specified could be overridden\n");
1128 /* ------------------------------------------------------ */
1130 case 'Y': { /* Shall we do an MCT ? 0:no_mct;1:rgb->ycc;2:custom mct (-m option required)*/
1132 sscanf(opj_optarg, "%d", &mct_mode);
1133 if (mct_mode < 0 || mct_mode > 2) {
1135 "MCT incorrect value!! Current accepted values are 0, 1 or 2.\n");
1138 parameters->tcp_mct = (char) mct_mode;
1142 /* ------------------------------------------------------ */
1145 case 'm': { /* mct input file */
1146 char *lFilename = opj_optarg;
1149 float *lCurrentDoublePtr;
1152 int lNbComp = 0, lTotalComp, lMctComp, i2;
1153 size_t lStrLen, lStrFread;
1156 FILE * lFile = fopen(lFilename, "r");
1157 if (lFile == NULL) {
1161 /* Set size of file and read its content*/
1162 fseek(lFile, 0, SEEK_END);
1163 lStrLen = (size_t)ftell(lFile);
1164 fseek(lFile, 0, SEEK_SET);
1165 lMatrix = (char *) malloc(lStrLen + 1);
1166 if (lMatrix == NULL) {
1170 lStrFread = fread(lMatrix, 1, lStrLen, lFile);
1172 if (lStrLen != lStrFread) {
1177 lMatrix[lStrLen] = 0;
1178 lCurrentPtr = lMatrix;
1180 /* replace ',' by 0 */
1181 while (*lCurrentPtr != 0) {
1182 if (*lCurrentPtr == ' ') {
1189 lCurrentPtr = lMatrix;
1191 lNbComp = (int)(sqrt(4 * lNbComp + 1) / 2. - 0.5);
1192 lMctComp = lNbComp * lNbComp;
1193 lTotalComp = lMctComp + lNbComp;
1194 lSpace = (float *) malloc((size_t)lTotalComp * sizeof(float));
1195 if (lSpace == NULL) {
1199 lCurrentDoublePtr = lSpace;
1200 for (i2 = 0; i2 < lMctComp; ++i2) {
1201 lStrLen = strlen(lCurrentPtr) + 1;
1202 *lCurrentDoublePtr++ = (float) atof(lCurrentPtr);
1203 lCurrentPtr += lStrLen;
1206 l_int_ptr = (int*) lCurrentDoublePtr;
1207 for (i2 = 0; i2 < lNbComp; ++i2) {
1208 lStrLen = strlen(lCurrentPtr) + 1;
1209 *l_int_ptr++ = atoi(lCurrentPtr);
1210 lCurrentPtr += lStrLen;
1213 /* TODO should not be here ! */
1214 opj_set_MCT(parameters, lSpace, (int *)(lSpace + lMctComp),
1215 (OPJ_UINT32)lNbComp);
1224 /* ------------------------------------------------------ */
1228 /* ------------------------------------------------------ */
1230 case 'W': { /* JPWL capabilities switched on */
1232 int hprot, pprot, sens, addr, size, range;
1234 /* we need to enable indexing */
1235 if (!indexfilename || !*indexfilename) {
1236 if (opj_strcpy_s(indexfilename, indexfilename_size,
1237 JPWL_PRIVATEINDEX_NAME) != 0) {
1242 /* search for different protection methods */
1244 /* break the option in comma points and parse the result */
1245 token = strtok(opj_optarg, ",");
1246 while (token != NULL) {
1248 /* search header error protection method */
1249 if (*token == 'h') {
1251 static int tile = 0, tilespec = 0, lasttileno = 0;
1253 hprot = 1; /* predefined method */
1255 if (sscanf(token, "h=%d", &hprot) == 1) {
1256 /* Main header, specified */
1257 if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
1258 ((hprot >= 37) && (hprot <= 128)))) {
1259 fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n",
1263 parameters->jpwl_hprot_MH = hprot;
1265 } else if (sscanf(token, "h%d=%d", &tile, &hprot) == 2) {
1266 /* Tile part header, specified */
1267 if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
1268 ((hprot >= 37) && (hprot <= 128)))) {
1269 fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n",
1275 "ERROR -> invalid tile part number on protection method t = %d\n", tile);
1278 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1279 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
1280 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
1283 } else if (sscanf(token, "h%d", &tile) == 1) {
1284 /* Tile part header, unspecified */
1287 "ERROR -> invalid tile part number on protection method t = %d\n", tile);
1290 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1291 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
1292 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
1296 } else if (!strcmp(token, "h")) {
1297 /* Main header, unspecified */
1298 parameters->jpwl_hprot_MH = hprot;
1301 fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
1307 /* search packet error protection method */
1308 if (*token == 'p') {
1310 static int pack = 0, tile = 0, packspec = 0;
1312 pprot = 1; /* predefined method */
1314 if (sscanf(token, "p=%d", &pprot) == 1) {
1315 /* Method for all tiles and all packets */
1316 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1317 ((pprot >= 37) && (pprot <= 128)))) {
1318 fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n",
1322 parameters->jpwl_pprot_tileno[0] = 0;
1323 parameters->jpwl_pprot_packno[0] = 0;
1324 parameters->jpwl_pprot[0] = pprot;
1326 } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) {
1327 /* method specified from that tile on */
1328 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1329 ((pprot >= 37) && (pprot <= 128)))) {
1330 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1335 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1338 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1339 parameters->jpwl_pprot_tileno[packspec] = tile;
1340 parameters->jpwl_pprot_packno[packspec] = 0;
1341 parameters->jpwl_pprot[packspec++] = pprot;
1344 } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) {
1345 /* method fully specified from that tile and that packet on */
1346 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1347 ((pprot >= 37) && (pprot <= 128)))) {
1348 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1353 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1357 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
1361 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1362 parameters->jpwl_pprot_tileno[packspec] = tile;
1363 parameters->jpwl_pprot_packno[packspec] = pack;
1364 parameters->jpwl_pprot[packspec++] = pprot;
1367 } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) {
1368 /* default method from that tile and that packet on */
1369 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1370 ((pprot >= 37) && (pprot <= 128)))) {
1371 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1376 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1380 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
1384 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1385 parameters->jpwl_pprot_tileno[packspec] = tile;
1386 parameters->jpwl_pprot_packno[packspec] = pack;
1387 parameters->jpwl_pprot[packspec++] = pprot;
1390 } else if (sscanf(token, "p%d", &tile) == 1) {
1391 /* default from a tile on */
1394 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1397 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1398 parameters->jpwl_pprot_tileno[packspec] = tile;
1399 parameters->jpwl_pprot_packno[packspec] = 0;
1400 parameters->jpwl_pprot[packspec++] = pprot;
1404 } else if (!strcmp(token, "p")) {
1406 parameters->jpwl_pprot_tileno[0] = 0;
1407 parameters->jpwl_pprot_packno[0] = 0;
1408 parameters->jpwl_pprot[0] = pprot;
1411 fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
1417 /* search sensitivity method */
1418 if (*token == 's') {
1420 static int tile = 0, tilespec = 0, lasttileno = 0;
1422 sens = 0; /* predefined: relative error */
1424 if (sscanf(token, "s=%d", &sens) == 1) {
1425 /* Main header, specified */
1426 if ((sens < -1) || (sens > 7)) {
1427 fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n",
1431 parameters->jpwl_sens_MH = sens;
1433 } else if (sscanf(token, "s%d=%d", &tile, &sens) == 2) {
1434 /* Tile part header, specified */
1435 if ((sens < -1) || (sens > 7)) {
1436 fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n",
1442 "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
1445 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1446 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
1447 parameters->jpwl_sens_TPH[tilespec++] = sens;
1450 } else if (sscanf(token, "s%d", &tile) == 1) {
1451 /* Tile part header, unspecified */
1454 "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
1457 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1458 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
1459 parameters->jpwl_sens_TPH[tilespec++] = hprot;
1462 } else if (!strcmp(token, "s")) {
1463 /* Main header, unspecified */
1464 parameters->jpwl_sens_MH = sens;
1467 fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token);
1471 parameters->jpwl_sens_size = 2; /* 2 bytes for default size */
1474 /* search addressing size */
1475 if (*token == 'a') {
1478 addr = 0; /* predefined: auto */
1480 if (sscanf(token, "a=%d", &addr) == 1) {
1482 if ((addr != 0) && (addr != 2) && (addr != 4)) {
1483 fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr);
1486 parameters->jpwl_sens_addr = addr;
1488 } else if (!strcmp(token, "a")) {
1490 parameters->jpwl_sens_addr = addr; /* auto for default size */
1493 fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token);
1499 /* search sensitivity size */
1500 if (*token == 'z') {
1503 size = 1; /* predefined: 1 byte */
1505 if (sscanf(token, "z=%d", &size) == 1) {
1507 if ((size != 0) && (size != 1) && (size != 2)) {
1508 fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size);
1511 parameters->jpwl_sens_size = size;
1513 } else if (!strcmp(token, "a")) {
1515 parameters->jpwl_sens_size = size; /* 1 for default size */
1518 fprintf(stderr, "ERROR -> invalid size selection = %s\n", token);
1524 /* search range method */
1525 if (*token == 'g') {
1528 range = 0; /* predefined: 0 (packet) */
1530 if (sscanf(token, "g=%d", &range) == 1) {
1532 if ((range < 0) || (range > 3)) {
1533 fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range);
1536 parameters->jpwl_sens_range = range;
1538 } else if (!strcmp(token, "g")) {
1540 parameters->jpwl_sens_range = range;
1543 fprintf(stderr, "ERROR -> invalid range selection = %s\n", token);
1549 /* next token or bust */
1550 token = strtok(NULL, ",");
1555 fprintf(stdout, "Info: JPWL capabilities enabled\n");
1556 parameters->jpwl_epc_on = OPJ_TRUE;
1560 #endif /* USE_JPWL */
1562 /* ------------------------------------------------------ */
1564 case 'J': { /* jpip on */
1565 parameters->jpip_on = OPJ_TRUE;
1568 /* ------------------------------------------------------ */
1572 fprintf(stderr, "[WARNING] An invalid option has been ignored\n");
1577 if (img_fol->set_imgdir == 1) {
1578 if (!(parameters->infile[0] == 0)) {
1579 fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together !!\n");
1582 if (img_fol->set_out_format == 0) {
1584 "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
1585 fprintf(stderr, "Only one format allowed! Valid formats are j2k and jp2!!\n");
1588 if (!((parameters->outfile[0] == 0))) {
1589 fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together !!\n");
1590 fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n");
1594 if ((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
1595 fprintf(stderr, "[ERROR] Required parameters are missing\n"
1596 "Example: %s -i image.pgm -o image.j2k\n", argv[0]);
1597 fprintf(stderr, " Help: %s -h\n", argv[0]);
1602 if ((parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0)
1603 || (parameters->decod_format == RAWL_DFMT && raw_cp->rawWidth == 0)) {
1604 fprintf(stderr, "[ERROR] invalid raw image parameters\n");
1605 fprintf(stderr, "Please use the Format option -F:\n");
1607 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
1608 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
1609 fprintf(stderr, "Aborting\n");
1613 if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc ||
1614 parameters->cp_fixed_quality)
1615 && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^
1616 parameters->cp_fixed_quality))) {
1617 fprintf(stderr, "[ERROR] options -r -q and -f cannot be used together !!\n");
1619 } /* mod fixed_quality */
1622 /* if no rate entered, lossless by default */
1623 /* Note: post v2.2.0, this is no longer necessary, but for released */
1624 /* versions at the time of writing, this is needed to avoid crashes */
1625 if (parameters->tcp_numlayers == 0) {
1626 parameters->tcp_rates[0] = 0;
1627 parameters->tcp_numlayers++;
1628 parameters->cp_disto_alloc = 1;
1631 if ((parameters->cp_tx0 > parameters->image_offset_x0) ||
1632 (parameters->cp_ty0 > parameters->image_offset_y0)) {
1634 "[ERROR] Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
1635 parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0,
1636 parameters->image_offset_y0);
1640 for (i = 0; i < parameters->numpocs; i++) {
1641 if (parameters->POC[i].prg == -1) {
1643 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
1648 /* If subsampled image is provided, automatically disable MCT */
1649 if (((parameters->decod_format == RAW_DFMT) ||
1650 (parameters->decod_format == RAWL_DFMT))
1651 && (((raw_cp->rawComp > 1) && ((raw_cp->rawComps[1].dx > 1) ||
1652 (raw_cp->rawComps[1].dy > 1)))
1653 || ((raw_cp->rawComp > 2) && ((raw_cp->rawComps[2].dx > 1) ||
1654 (raw_cp->rawComps[2].dy > 1)))
1656 parameters->tcp_mct = 0;
1663 /* -------------------------------------------------------------------------- */
1666 sample error debug callback expecting no client object
1668 static void error_callback(const char *msg, void *client_data)
1671 fprintf(stdout, "[ERROR] %s", msg);
1674 sample warning debug callback expecting no client object
1676 static void warning_callback(const char *msg, void *client_data)
1679 fprintf(stdout, "[WARNING] %s", msg);
1682 sample debug callback expecting no client object
1684 static void info_callback(const char *msg, void *client_data)
1687 fprintf(stdout, "[INFO] %s", msg);
1690 OPJ_FLOAT64 opj_clock(void)
1693 /* _WIN32: use QueryPerformance (very accurate) */
1694 LARGE_INTEGER freq, t ;
1695 /* freq is the clock speed of the CPU */
1696 QueryPerformanceFrequency(&freq) ;
1697 /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
1698 /* t is the high resolution performance counter (see MSDN) */
1699 QueryPerformanceCounter(& t) ;
1700 return freq.QuadPart ? ((OPJ_FLOAT64) t.QuadPart / (OPJ_FLOAT64) freq.QuadPart)
1703 /* Unix or Linux: use resource usage */
1705 OPJ_FLOAT64 procTime;
1706 /* (1) Get the rusage data structure at this moment (man getrusage) */
1708 /* (2) What is the elapsed time ? - CPU time = User time + System time */
1709 /* (2a) Get the seconds */
1710 procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
1711 /* (2b) More precisely! Get the microseconds part ! */
1712 return (procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) *
1718 /* -------------------------------------------------------------------------- */
1722 /* -------------------------------------------------------------------------- */
1723 int main(int argc, char **argv)
1726 opj_cparameters_t parameters; /* compression parameters */
1728 opj_stream_t *l_stream = 00;
1729 opj_codec_t* l_codec = 00;
1730 opj_image_t *image = NULL;
1731 raw_cparameters_t raw_cp;
1732 OPJ_SIZE_T num_compressed_files = 0;
1734 char indexfilename[OPJ_PATH_LEN]; /* index file name */
1736 unsigned int i, num_images, imageno;
1738 dircnt_t *dirptr = NULL;
1741 OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
1742 OPJ_UINT32 l_nb_tiles = 4;
1743 OPJ_FLOAT64 t = opj_clock();
1745 /* set encoding parameters to default values */
1746 opj_set_default_encoder_parameters(¶meters);
1748 /* Initialize indexfilename and img_fol */
1750 memset(&img_fol, 0, sizeof(img_fol_t));
1752 /* raw_cp initialization */
1753 raw_cp.rawBitDepth = 0;
1755 raw_cp.rawComps = 0;
1756 raw_cp.rawHeight = 0;
1757 raw_cp.rawSigned = 0;
1758 raw_cp.rawWidth = 0;
1760 /* parse input and get user encoding parameters */
1761 parameters.tcp_mct = (char)
1762 255; /* This will be set later according to the input image or the provided option */
1763 if (parse_cmdline_encoder(argc, argv, ¶meters, &img_fol, &raw_cp,
1764 indexfilename, sizeof(indexfilename)) == 1) {
1768 /* Read directory if necessary */
1769 if (img_fol.set_imgdir == 1) {
1770 num_images = get_num_images(img_fol.imgdirpath);
1771 dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
1773 dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
1774 char)); /* Stores at max 10 image file names*/
1775 dirptr->filename = (char**) malloc(num_images * sizeof(char*));
1776 if (!dirptr->filename_buf) {
1779 for (i = 0; i < num_images; i++) {
1780 dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN;
1783 if (load_images(dirptr, img_fol.imgdirpath) == 1) {
1786 if (num_images == 0) {
1787 fprintf(stdout, "Folder is empty\n");
1793 /*Encoding image one by one*/
1794 for (imageno = 0; imageno < num_images; imageno++) {
1796 fprintf(stderr, "\n");
1798 if (img_fol.set_imgdir == 1) {
1799 if (get_next_file((int)imageno, dirptr, &img_fol, ¶meters)) {
1800 fprintf(stderr, "skipping file...\n");
1805 switch (parameters.decod_format) {
1822 fprintf(stderr, "skipping file...\n");
1826 /* decode the source image */
1827 /* ----------------------- */
1829 switch (parameters.decod_format) {
1831 image = pgxtoimage(parameters.infile, ¶meters);
1833 fprintf(stderr, "Unable to load pgx file\n");
1839 image = pnmtoimage(parameters.infile, ¶meters);
1841 fprintf(stderr, "Unable to load pnm file\n");
1847 image = bmptoimage(parameters.infile, ¶meters);
1849 fprintf(stderr, "Unable to load bmp file\n");
1854 #ifdef OPJ_HAVE_LIBTIFF
1856 image = tiftoimage(parameters.infile, ¶meters);
1858 fprintf(stderr, "Unable to load tiff file\n");
1862 #endif /* OPJ_HAVE_LIBTIFF */
1865 image = rawtoimage(parameters.infile, ¶meters, &raw_cp);
1867 fprintf(stderr, "Unable to load raw file\n");
1873 image = rawltoimage(parameters.infile, ¶meters, &raw_cp);
1875 fprintf(stderr, "Unable to load raw file\n");
1881 image = tgatoimage(parameters.infile, ¶meters);
1883 fprintf(stderr, "Unable to load tga file\n");
1888 #ifdef OPJ_HAVE_LIBPNG
1890 image = pngtoimage(parameters.infile, ¶meters);
1892 fprintf(stderr, "Unable to load png file\n");
1896 #endif /* OPJ_HAVE_LIBPNG */
1899 /* Can happen if input file is TIFF or PNG
1900 * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
1903 fprintf(stderr, "Unable to load file: got no image\n");
1907 /* Decide if MCT should be used */
1908 if (parameters.tcp_mct == (char)
1909 255) { /* mct mode has not been set in commandline */
1910 parameters.tcp_mct = (image->numcomps >= 3) ? 1 : 0;
1911 } else { /* mct mode has been set in commandline */
1912 if ((parameters.tcp_mct == 1) && (image->numcomps < 3)) {
1913 fprintf(stderr, "RGB->YCC conversion cannot be used:\n");
1914 fprintf(stderr, "Input image has less than 3 components\n");
1917 if ((parameters.tcp_mct == 2) && (!parameters.mct_data)) {
1918 fprintf(stderr, "Custom MCT has been set but no array-based MCT\n");
1919 fprintf(stderr, "has been provided. Aborting.\n");
1924 /* encode the destination image */
1925 /* ---------------------------- */
1927 switch (parameters.cod_format) {
1928 case J2K_CFMT: { /* JPEG-2000 codestream */
1929 /* Get a decoder handle */
1930 l_codec = opj_create_compress(OPJ_CODEC_J2K);
1933 case JP2_CFMT: { /* JPEG 2000 compressed image data */
1934 /* Get a decoder handle */
1935 l_codec = opj_create_compress(OPJ_CODEC_JP2);
1939 fprintf(stderr, "skipping file..\n");
1940 opj_stream_destroy(l_stream);
1944 /* catch events using our callbacks and give a local context */
1945 opj_set_info_handler(l_codec, info_callback, 00);
1946 opj_set_warning_handler(l_codec, warning_callback, 00);
1947 opj_set_error_handler(l_codec, error_callback, 00);
1950 parameters.cp_tx0 = 0;
1951 parameters.cp_ty0 = 0;
1952 parameters.tile_size_on = OPJ_TRUE;
1953 parameters.cp_tdx = 512;
1954 parameters.cp_tdy = 512;
1956 if (! opj_setup_encoder(l_codec, ¶meters, image)) {
1957 fprintf(stderr, "failed to encode image: opj_setup_encoder\n");
1958 opj_destroy_codec(l_codec);
1959 opj_image_destroy(image);
1963 /* open a byte stream for writing and allocate memory for all tiles */
1964 l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE);
1969 /* encode the image */
1970 bSuccess = opj_start_compress(l_codec, image, l_stream);
1972 fprintf(stderr, "failed to encode image: opj_start_compress\n");
1974 if (bSuccess && bUseTiles) {
1976 OPJ_UINT32 l_data_size = 512 * 512 * 3;
1977 l_data = (OPJ_BYTE*) calloc(1, l_data_size);
1978 if (l_data == NULL) {
1981 for (i = 0; i < l_nb_tiles; ++i) {
1982 if (! opj_write_tile(l_codec, i, l_data, l_data_size, l_stream)) {
1983 fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",
1985 opj_stream_destroy(l_stream);
1986 opj_destroy_codec(l_codec);
1987 opj_image_destroy(image);
1993 bSuccess = bSuccess && opj_encode(l_codec, l_stream);
1995 fprintf(stderr, "failed to encode image: opj_encode\n");
1998 bSuccess = bSuccess && opj_end_compress(l_codec, l_stream);
2000 fprintf(stderr, "failed to encode image: opj_end_compress\n");
2004 opj_stream_destroy(l_stream);
2005 opj_destroy_codec(l_codec);
2006 opj_image_destroy(image);
2007 fprintf(stderr, "failed to encode image\n");
2008 remove(parameters.outfile);
2012 num_compressed_files++;
2013 fprintf(stdout, "[INFO] Generated outfile %s\n", parameters.outfile);
2014 /* close and free the byte stream */
2015 opj_stream_destroy(l_stream);
2017 /* free remaining compression structures */
2018 opj_destroy_codec(l_codec);
2020 /* free image data */
2021 opj_image_destroy(image);
2025 /* free user parameters structure */
2026 if (parameters.cp_comment) {
2027 free(parameters.cp_comment);
2029 if (parameters.cp_matrice) {
2030 free(parameters.cp_matrice);
2032 if (raw_cp.rawComps) {
2033 free(raw_cp.rawComps);
2036 t = opj_clock() - t;
2037 if (num_compressed_files) {
2038 fprintf(stdout, "encode time: %d ms \n",
2039 (int)((t * 1000.0) / (OPJ_FLOAT64)num_compressed_files));
2045 if (parameters.cp_comment) {
2046 free(parameters.cp_comment);
2048 if (parameters.cp_matrice) {
2049 free(parameters.cp_matrice);
2051 if (raw_cp.rawComps) {
2052 free(raw_cp.rawComps);
2054 if (img_fol.imgdirpath) {
2055 free(img_fol.imgdirpath);
2058 if (dirptr->filename_buf) {
2059 free(dirptr->filename_buf);
2061 if (dirptr->filename) {
2062 free(dirptr->filename);