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.
50 #include "windirent.h"
57 #define strcasecmp _stricmp
58 #define strncasecmp _strnicmp
62 #include <sys/resource.h>
63 #include <sys/times.h>
66 #include "opj_apps_config.h"
68 #include "opj_getopt.h"
72 #include "format_defs.h"
73 #include "opj_string.h"
75 typedef struct dircnt {
76 /** Buffer for holding images read from Directory*/
78 /** Pointer to the buffer*/
82 typedef struct img_folder {
83 /** The directory path of the folder containing input images*/
89 /** Enable Cod Format for output*/
93 static void encode_help_display(void)
96 "\nThis is the opj_compress utility from the OpenJPEG project.\n"
97 "It compresses various image formats with the JPEG 2000 algorithm.\n"
98 "It has been compiled against openjp2 library v%s.\n\n", opj_version());
100 fprintf(stdout, "Default encoding options:\n");
101 fprintf(stdout, "-------------------------\n");
102 fprintf(stdout, "\n");
103 fprintf(stdout, " * Lossless\n");
104 fprintf(stdout, " * 1 tile\n");
105 fprintf(stdout, " * RGB->YCC conversion if at least 3 components\n");
106 fprintf(stdout, " * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
107 fprintf(stdout, " * Size of code-block : 64 x 64\n");
108 fprintf(stdout, " * Number of resolutions: 6\n");
109 fprintf(stdout, " * No SOP marker in the codestream\n");
110 fprintf(stdout, " * No EPH marker in the codestream\n");
111 fprintf(stdout, " * No sub-sampling in x or y direction\n");
112 fprintf(stdout, " * No mode switch activated\n");
113 fprintf(stdout, " * Progression order: LRCP\n");
115 fprintf(stdout, " * No index file\n");
116 #endif /* FIXME_INDEX */
117 fprintf(stdout, " * No ROI upshifted\n");
118 fprintf(stdout, " * No offset of the origin of the image\n");
119 fprintf(stdout, " * No offset of the origin of the tiles\n");
120 fprintf(stdout, " * Reversible DWT 5-3\n");
123 fprintf(stdout, " * No JPWL protection\n");
124 #endif /* USE_JPWL */
126 fprintf(stdout, "\n");
128 fprintf(stdout, "Note:\n");
129 fprintf(stdout, "-----\n");
130 fprintf(stdout, "\n");
132 "The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
133 fprintf(stdout, "COD and QCD never appear in the tile_header.\n");
134 fprintf(stdout, "\n");
136 fprintf(stdout, "Parameters:\n");
137 fprintf(stdout, "-----------\n");
138 fprintf(stdout, "\n");
139 fprintf(stdout, "Required Parameters (except with -h):\n");
140 fprintf(stdout, "One of the two options -ImgDir or -i must be used\n");
141 fprintf(stdout, "\n");
142 fprintf(stdout, "-i <file>\n");
143 fprintf(stdout, " Input file\n");
145 " Known extensions are <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|TIFF|RAW|YUV|RAWL|TGA>\n");
146 fprintf(stdout, " If used, '-o <file>' must be provided\n");
147 fprintf(stdout, "-o <compressed file>\n");
148 fprintf(stdout, " Output file (accepted extensions are j2k or jp2).\n");
149 fprintf(stdout, "-ImgDir <dir>\n");
150 fprintf(stdout, " Image file Directory path (example ../Images) \n");
151 fprintf(stdout, " When using this option -OutFor must be used\n");
152 fprintf(stdout, "-OutFor <J2K|J2C|JP2>\n");
153 fprintf(stdout, " Output format for compressed files.\n");
154 fprintf(stdout, " Required only if -ImgDir is used\n");
156 "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
157 fprintf(stdout, " Characteristics of the raw or yuv input image\n");
159 " If subsampling is omitted, 1x1 is assumed for all components\n");
160 fprintf(stdout, " Example: -F 512,512,3,8,u@1x1:2x2:2x2\n");
162 " for raw or yuv 512x512 size with 4:2:0 subsampling\n");
163 fprintf(stdout, " Required only if RAW or RAWL input file is provided.\n");
164 fprintf(stdout, "\n");
165 fprintf(stdout, "Optional Parameters:\n");
166 fprintf(stdout, "\n");
167 fprintf(stdout, "-h\n");
168 fprintf(stdout, " Display the help information.\n");
169 fprintf(stdout, "-r <compression ratio>,<compression ratio>,...\n");
170 fprintf(stdout, " Different compression ratios for successive layers.\n");
172 " The rate specified for each quality level is the desired\n");
173 fprintf(stdout, " compression factor (use 1 for lossless)\n");
174 fprintf(stdout, " Decreasing ratios required.\n");
175 fprintf(stdout, " Example: -r 20,10,1 means \n");
176 fprintf(stdout, " quality layer 1: compress 20x, \n");
177 fprintf(stdout, " quality layer 2: compress 10x \n");
178 fprintf(stdout, " quality layer 3: compress lossless\n");
179 fprintf(stdout, " Options -r and -q cannot be used together.\n");
180 fprintf(stdout, "-q <psnr value>,<psnr value>,<psnr value>,...\n");
181 fprintf(stdout, " Different psnr for successive layers (-q 30,40,50).\n");
182 fprintf(stdout, " Increasing PSNR values required, except 0 which can\n");
183 fprintf(stdout, " be used for the last layer to indicate it is lossless.\n");
184 fprintf(stdout, " Options -r and -q cannot be used together.\n");
185 fprintf(stdout, "-n <number of resolutions>\n");
186 fprintf(stdout, " Number of resolutions.\n");
188 " It corresponds to the number of DWT decompositions +1. \n");
189 fprintf(stdout, " Default: 6.\n");
190 fprintf(stdout, "-TargetBitDepth <target bit depth>\n");
191 fprintf(stdout, " Target bit depth.\n");
192 fprintf(stdout, " Number of bits per component to use from input image\n");
193 fprintf(stdout, " if all bits are unwanted.\n");
194 fprintf(stdout, " (Currently only implemented for TIF.)\n");
195 fprintf(stdout, "-b <cblk width>,<cblk height>\n");
197 " Code-block size. The dimension must respect the constraint \n");
199 " defined in the JPEG-2000 standard (no dimension smaller than 4 \n");
201 " or greater than 1024, no code-block with more than 4096 coefficients).\n");
202 fprintf(stdout, " The maximum value authorized is 64x64. \n");
203 fprintf(stdout, " Default: 64x64.\n");
205 "-c [<prec width>,<prec height>],[<prec width>,<prec height>],...\n");
206 fprintf(stdout, " Precinct size. Values specified must be power of 2. \n");
208 " Multiple records may be supplied, in which case the first record refers\n");
210 " to the highest resolution level and subsequent records to lower \n");
212 " resolution levels. The last specified record is halved successively for each \n");
213 fprintf(stdout, " remaining lower resolution levels.\n");
214 fprintf(stdout, " Default: 2^15x2^15 at each resolution.\n");
215 fprintf(stdout, "-t <tile width>,<tile height>\n");
216 fprintf(stdout, " Tile size.\n");
218 " Default: the dimension of the whole image, thus only one tile.\n");
219 fprintf(stdout, "-p <LRCP|RLCP|RPCL|PCRL|CPRL>\n");
220 fprintf(stdout, " Progression order.\n");
221 fprintf(stdout, " Default: LRCP.\n");
222 fprintf(stdout, "-s <subX,subY>\n");
223 fprintf(stdout, " Subsampling factor.\n");
224 fprintf(stdout, " Subsampling bigger than 2 can produce error\n");
225 fprintf(stdout, " Default: no subsampling.\n");
227 "-POC <progression order change>/<progression order change>/...\n");
228 fprintf(stdout, " Progression order change.\n");
230 " The syntax of a progression order change is the following:\n");
232 " T<tile>=<resStart>,<compStart>,<layerEnd>,<resEnd>,<compEnd>,<progOrder>\n");
233 fprintf(stdout, " Example: -POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL\n");
234 fprintf(stdout, "-SOP\n");
235 fprintf(stdout, " Write SOP marker before each packet.\n");
236 fprintf(stdout, "-EPH\n");
237 fprintf(stdout, " Write EPH marker after each header packet.\n");
238 fprintf(stdout, "-PLT\n");
239 fprintf(stdout, " Write PLT marker in tile-part header.\n");
240 fprintf(stdout, "-TLM\n");
241 fprintf(stdout, " Write TLM marker in main header.\n");
242 fprintf(stdout, "-M <key value>\n");
243 fprintf(stdout, " Mode switch.\n");
244 fprintf(stdout, " [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
245 fprintf(stdout, " 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)]\n");
246 fprintf(stdout, " Indicate multiple modes by adding their values.\n");
248 " Example: RESTART(4) + RESET(2) + SEGMARK(32) => -M 38\n");
249 fprintf(stdout, "-TP <R|L|C>\n");
250 fprintf(stdout, " Divide packets of every tile into tile-parts.\n");
252 " Division is made by grouping Resolutions (R), Layers (L)\n");
253 fprintf(stdout, " or Components (C).\n");
255 fprintf(stdout, "-x <index file>\n");
256 fprintf(stdout, " Create an index file.\n");
257 #endif /*FIXME_INDEX*/
258 fprintf(stdout, "-ROI c=<component index>,U=<upshifting value>\n");
259 fprintf(stdout, " Quantization indices upshifted for a component. \n");
261 " Warning: This option does not implement the usual ROI (Region of Interest).\n");
263 " It should be understood as a 'Component of Interest'. It offers the \n");
265 " possibility to upshift the value of a component during quantization step.\n");
267 " The value after c= is the component number [0, 1, 2, ...] and the value \n");
269 " after U= is the value of upshifting. U must be in the range [0, 37].\n");
270 fprintf(stdout, "-d <image offset X,image offset Y>\n");
271 fprintf(stdout, " Offset of the origin of the image.\n");
272 fprintf(stdout, "-T <tile offset X,tile offset Y>\n");
273 fprintf(stdout, " Offset of the origin of the tiles.\n");
274 fprintf(stdout, "-I\n");
275 fprintf(stdout, " Use the irreversible DWT 9-7.\n");
276 fprintf(stdout, "-mct <0|1|2>\n");
278 " Explicitly specifies if a Multiple Component Transform has to be used.\n");
279 fprintf(stdout, " 0: no MCT ; 1: RGB->YCC conversion ; 2: custom MCT.\n");
281 " If custom MCT, \"-m\" option has to be used (see hereunder).\n");
283 " By default, RGB->YCC conversion is used if there are 3 components or more,\n");
284 fprintf(stdout, " no conversion otherwise.\n");
285 fprintf(stdout, "-m <file>\n");
287 " Use array-based MCT, values are coma separated, line by line\n");
289 " No specific separators between lines, no space allowed between values.\n");
291 " If this option is used, it automatically sets \"-mct\" option to 2.\n");
292 fprintf(stdout, "-cinema2K <24|48>\n");
293 fprintf(stdout, " Digital Cinema 2K profile compliant codestream.\n");
295 " Need to specify the frames per second for a 2K resolution.\n");
296 fprintf(stdout, " Only 24 or 48 fps are currently allowed.\n");
297 fprintf(stdout, "-cinema4K\n");
298 fprintf(stdout, " Digital Cinema 4K profile compliant codestream.\n");
299 fprintf(stdout, " Frames per second not required. Default value is 24fps.\n");
300 fprintf(stdout, "-IMF <PROFILE>[,mainlevel=X][,sublevel=Y][,framerate=FPS]\n");
301 fprintf(stdout, " Interoperable Master Format compliant codestream.\n");
302 fprintf(stdout, " <PROFILE>=2K, 4K, 8K, 2K_R, 4K_R or 8K_R.\n");
303 fprintf(stdout, " X >= 0 and X <= 11.\n");
304 fprintf(stdout, " Y >= 0 and Y <= 9.\n");
306 " framerate > 0 may be specified to enhance checks and set maximum bit rate when Y > 0.\n");
307 fprintf(stdout, "-GuardBits value\n");
309 " Number of guard bits in [0,7] range. Usually 1 or 2 (default value).\n");
310 fprintf(stdout, "-jpip\n");
311 fprintf(stdout, " Write jpip codestream index box in JP2 output file.\n");
312 fprintf(stdout, " Currently supports only RPCL order.\n");
313 fprintf(stdout, "-C <comment>\n");
314 fprintf(stdout, " Add <comment> in the comment marker segment.\n");
315 if (opj_has_thread_support()) {
316 fprintf(stdout, "-threads <num_threads|ALL_CPUS>\n"
317 " Number of threads to use for encoding or ALL_CPUS for all available cores.\n");
321 fprintf(stdout, "-W <params>\n");
322 fprintf(stdout, " Adoption of JPWL (Part 11) capabilities (-W params)\n");
324 " The <params> field can be written and repeated in any order:\n");
325 fprintf(stdout, " [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
326 fprintf(stdout, " ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
328 " h selects the header error protection (EPB): 'type' can be\n");
330 " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
332 " if 'tilepart' is absent, it is for main and tile headers\n");
333 fprintf(stdout, " if 'tilepart' is present, it applies from that tile\n");
335 " onwards, up to the next h<> spec, or to the last tilepart\n");
336 fprintf(stdout, " in the codestream (max. %d specs)\n",
337 JPWL_MAX_NO_TILESPECS);
339 " p selects the packet error protection (EEP/UEP with EPBs)\n");
340 fprintf(stdout, " to be applied to raw or yuv data: 'type' can be\n");
342 " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
344 " if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
346 " if 'tilepart:pack' is present, it applies from that tile\n");
348 " and that packet onwards, up to the next packet spec\n");
350 " or to the last packet in the last tilepart in the stream\n");
351 fprintf(stdout, " (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS);
353 " s enables sensitivity data insertion (ESD): 'method' can be\n");
355 " [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
356 fprintf(stdout, " 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
357 fprintf(stdout, " if 'tilepart' is absent, it is for main header only\n");
358 fprintf(stdout, " if 'tilepart' is present, it applies from that tile\n");
360 " onwards, up to the next s<> spec, or to the last tilepart\n");
361 fprintf(stdout, " in the codestream (max. %d specs)\n",
362 JPWL_MAX_NO_TILESPECS);
363 fprintf(stdout, " g determines the addressing mode: <range> can be\n");
364 fprintf(stdout, " [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
366 " a determines the size of data addressing: <addr> can be\n");
368 " 2/4 bytes (small/large codestreams). If not set, auto-mode\n");
370 " z determines the size of sensitivity values: <size> can be\n");
372 " 1/2 bytes, for the transformed pseudo-floating point value\n");
373 fprintf(stdout, " ex.:\n");
375 " h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
376 fprintf(stdout, " s0=6,s3=-1,a=0,g=1,z=1\n");
377 fprintf(stdout, " means\n");
379 " predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
381 " CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
382 fprintf(stdout, " UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
384 " UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
385 fprintf(stdout, " UEP rs default for packets of tilepart 1,\n");
386 fprintf(stdout, " no UEP for packets 0 to 19 of tilepart 3,\n");
388 " UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
389 fprintf(stdout, " relative sensitivity ESD for MH,\n");
391 " TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
393 " size of addresses and 1 byte for each sensitivity value\n");
394 fprintf(stdout, " ex.:\n");
395 fprintf(stdout, " h,s,p\n");
396 fprintf(stdout, " means\n");
398 " default protection to headers (MH and TPHs) as well as\n");
399 fprintf(stdout, " data packets, one ESD in MH\n");
401 " N.B.: use the following recommendations when specifying\n");
402 fprintf(stdout, " the JPWL parameters list\n");
404 " - when you use UEP, always pair the 'p' option with 'h'\n");
405 #endif /* USE_JPWL */
407 fprintf(stdout, "\n");
409 fprintf(stdout, "Index structure:\n");
410 fprintf(stdout, "----------------\n");
411 fprintf(stdout, "\n");
412 fprintf(stdout, "Image_height Image_width\n");
413 fprintf(stdout, "progression order\n");
414 fprintf(stdout, "Tiles_size_X Tiles_size_Y\n");
415 fprintf(stdout, "Tiles_nb_X Tiles_nb_Y\n");
416 fprintf(stdout, "Components_nb\n");
417 fprintf(stdout, "Layers_nb\n");
418 fprintf(stdout, "decomposition_levels\n");
419 fprintf(stdout, "[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
420 fprintf(stdout, " [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
421 fprintf(stdout, "Main_header_start_position\n");
422 fprintf(stdout, "Main_header_end_position\n");
423 fprintf(stdout, "Codestream_size\n");
424 fprintf(stdout, "\n");
425 fprintf(stdout, "INFO ON TILES\n");
427 "tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
429 "Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
431 "Tile_1 '' '' '' '' '' '' ''\n");
432 fprintf(stdout, "...\n");
434 "Tile_Nt '' '' '' '' '' '' ''\n");
435 fprintf(stdout, "...\n");
436 fprintf(stdout, "TILE 0 DETAILS\n");
437 fprintf(stdout, "part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
438 fprintf(stdout, "...\n");
439 fprintf(stdout, "Progression_string\n");
441 "pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
443 "Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
444 fprintf(stdout, "...\n");
446 "Tpacket_Np '' '' '' '' '' '' '' ''\n");
447 fprintf(stdout, "MaxDisto\n");
448 fprintf(stdout, "TotalDisto\n\n");
449 #endif /*FIXME_INDEX*/
452 static OPJ_PROG_ORDER give_progression(const char progression[4])
454 if (strncmp(progression, "LRCP", 4) == 0) {
457 if (strncmp(progression, "RLCP", 4) == 0) {
460 if (strncmp(progression, "RPCL", 4) == 0) {
463 if (strncmp(progression, "PCRL", 4) == 0) {
466 if (strncmp(progression, "CPRL", 4) == 0) {
470 return OPJ_PROG_UNKNOWN;
473 static unsigned int get_num_images(char *imgdirpath)
476 struct dirent* content;
477 unsigned int num_images = 0;
479 /*Reading the input images from given input directory*/
481 dir = opendir(imgdirpath);
483 fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
488 while ((content = readdir(dir)) != NULL) {
489 if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
492 if (num_images == UINT_MAX) {
493 fprintf(stderr, "Too many files in folder %s\n", imgdirpath);
503 static int load_images(dircnt_t *dirptr, char *imgdirpath)
506 struct dirent* content;
509 /*Reading the input images from given input directory*/
511 dir = opendir(imgdirpath);
513 fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
516 fprintf(stderr, "Folder opened successfully\n");
519 while ((content = readdir(dir)) != NULL) {
520 if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
524 strcpy(dirptr->filename[i], content->d_name);
531 static int get_file_format(char *filename)
534 static const char *extension[] = {
535 "pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "tiff", "raw", "yuv", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc"
537 static const int format[] = {
538 PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, TIF_DFMT, RAW_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT
540 char * ext = strrchr(filename, '.');
545 for (i = 0; i < sizeof(format) / sizeof(*format); i++) {
546 if (strcasecmp(ext, extension[i]) == 0) {
553 static char * get_file_name(char *name)
555 char *fname = strtok(name, ".");
559 static char get_next_file(unsigned int imageno, dircnt_t *dirptr,
561 opj_cparameters_t *parameters)
563 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
564 outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
565 char *temp_p, temp1[OPJ_PATH_LEN] = "";
567 strcpy(image_filename, dirptr->filename[imageno]);
568 fprintf(stderr, "File Number %u \"%s\"\n", imageno, image_filename);
569 parameters->decod_format = get_file_format(image_filename);
570 if (parameters->decod_format == -1) {
573 if (strlen(img_fol->imgdirpath) + 1 + strlen(image_filename) + 1 > sizeof(
577 strcpy(infilename, img_fol->imgdirpath);
578 strcat(infilename, "/");
579 strcat(infilename, image_filename);
580 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
586 strcpy(temp_ofname, get_file_name(image_filename));
587 while ((temp_p = strtok(NULL, ".")) != NULL) {
588 strcat(temp_ofname, temp1);
589 sprintf(temp1, ".%s", temp_p);
591 if (img_fol->set_out_format == 1) {
592 if (strlen(img_fol->imgdirpath) + 1 + strlen(temp_ofname) + 1 + strlen(
593 img_fol->out_format) + 1 > sizeof(outfilename)) {
596 strcpy(outfilename, img_fol->imgdirpath);
597 strcat(outfilename, "/");
598 strcat(outfilename, temp_ofname);
599 strcat(outfilename, ".");
600 strcat(outfilename, img_fol->out_format);
601 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
609 /* ------------------------------------------------------------------------------------ */
611 static int parse_cmdline_encoder(int argc, char **argv,
612 opj_cparameters_t *parameters,
613 img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename,
614 size_t indexfilename_size,
620 unsigned int* pTarget_bitdepth)
624 opj_option_t long_option[] = {
625 {"cinema2K", REQ_ARG, NULL, 'w'},
626 {"cinema4K", NO_ARG, NULL, 'y'},
627 {"ImgDir", REQ_ARG, NULL, 'z'},
628 {"TP", REQ_ARG, NULL, 'u'},
629 {"SOP", NO_ARG, NULL, 'S'},
630 {"EPH", NO_ARG, NULL, 'E'},
631 {"OutFor", REQ_ARG, NULL, 'O'},
632 {"POC", REQ_ARG, NULL, 'P'},
633 {"ROI", REQ_ARG, NULL, 'R'},
634 {"jpip", NO_ARG, NULL, 'J'},
635 {"mct", REQ_ARG, NULL, 'Y'},
636 {"IMF", REQ_ARG, NULL, 'Z'},
637 {"PLT", NO_ARG, NULL, 'A'},
638 {"threads", REQ_ARG, NULL, 'B'},
639 {"TLM", NO_ARG, NULL, 'D'},
640 {"TargetBitDepth", REQ_ARG, NULL, 'X'},
641 {"GuardBits", REQ_ARG, NULL, 'G'}
644 /* parse the command line */
645 const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:X:G:"
648 #endif /* USE_JPWL */
651 totlen = sizeof(long_option);
652 img_fol->set_out_format = 0;
653 raw_cp->rawWidth = 0;
656 c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
661 case 'i': { /* input file */
662 char *infile = opj_optarg;
663 parameters->decod_format = get_file_format(infile);
664 switch (parameters->decod_format) {
676 "[ERROR] Unknown input file format: %s \n"
677 " Known file formats are *.pnm, *.pgm, *.ppm, *.pgx, *png, *.bmp, *.tif(f), *.raw, *.yuv or *.tga\n",
681 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
687 /* ----------------------------------------------------- */
689 case 'o': { /* output file */
690 char *outfile = opj_optarg;
691 parameters->cod_format = get_file_format(outfile);
692 switch (parameters->cod_format) {
698 "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
701 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
708 /* ----------------------------------------------------- */
709 case 'O': { /* output format */
711 char *of = opj_optarg;
712 sprintf(outformat, ".%s", of);
713 img_fol->set_out_format = 1;
714 parameters->cod_format = get_file_format(outformat);
715 switch (parameters->cod_format) {
718 img_fol->out_format = opj_optarg;
721 fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n");
728 /* ----------------------------------------------------- */
731 case 'r': { /* rates rates/distorsion */
732 char *s = opj_optarg;
733 parameters->tcp_numlayers = 0;
734 while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) ==
736 parameters->tcp_numlayers++;
737 while (*s && *s != ',') {
745 parameters->cp_disto_alloc = 1;
749 /* ----------------------------------------------------- */
752 case 'F': { /* Raw image format parameters */
753 OPJ_BOOL wrong = OPJ_FALSE;
758 int width, height, bitdepth, ncomp;
760 OPJ_BOOL raw_signed = OPJ_FALSE;
761 substr2 = strchr(opj_optarg, '@');
762 if (substr2 == NULL) {
763 len = (OPJ_UINT32) strlen(opj_optarg);
765 len = (OPJ_UINT32)(substr2 - opj_optarg);
766 substr2++; /* skip '@' character */
768 substr1 = (char*) malloc((len + 1) * sizeof(char));
769 if (substr1 == NULL) {
772 memcpy(substr1, opj_optarg, len);
774 if (sscanf(substr1, "%d,%d,%d,%d,%c", &width, &height, &ncomp, &bitdepth,
777 raw_signed = OPJ_TRUE;
778 } else if (signo == 'u') {
779 raw_signed = OPJ_FALSE;
790 raw_cp->rawWidth = width;
791 raw_cp->rawHeight = height;
792 raw_cp->rawComp = ncomp;
793 raw_cp->rawBitDepth = bitdepth;
794 raw_cp->rawSigned = raw_signed;
795 raw_cp->rawComps = (raw_comp_cparameters_t*) malloc(((OPJ_UINT32)(
796 ncomp)) * sizeof(raw_comp_cparameters_t));
797 if (raw_cp->rawComps == NULL) {
801 for (compno = 0; compno < ncomp && !wrong; compno++) {
802 if (substr2 == NULL) {
803 raw_cp->rawComps[compno].dx = lastdx;
804 raw_cp->rawComps[compno].dy = lastdy;
807 sep = strchr(substr2, ':');
809 if (sscanf(substr2, "%dx%d", &dx, &dy) == 2) {
812 raw_cp->rawComps[compno].dx = dx;
813 raw_cp->rawComps[compno].dy = dy;
819 if (sscanf(substr2, "%dx%d:%s", &dx, &dy, substr2) == 3) {
820 raw_cp->rawComps[compno].dx = dx;
821 raw_cp->rawComps[compno].dy = dy;
831 fprintf(stderr, "\nError: invalid raw or yuv image parameters\n");
832 fprintf(stderr, "Please use the Format option -F:\n");
834 "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
836 "If subsampling is omitted, 1x1 is assumed for all components\n");
838 "Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
840 " for raw or yuv 512x512 size with 4:2:0 subsampling\n");
841 fprintf(stderr, "Aborting.\n");
847 /* ----------------------------------------------------- */
849 case 'q': { /* add fixed_quality */
850 char *s = opj_optarg;
851 while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers])
853 parameters->tcp_numlayers++;
854 while (*s && *s != ',') {
862 parameters->cp_fixed_quality = 1;
867 /* ----------------------------------------------------- */
869 case 'f': { /* mod fixed_quality (before : -q) */
870 int *row = NULL, *col = NULL;
871 OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0;
873 char *s = opj_optarg;
874 sscanf(s, "%u", &numlayers);
880 parameters->tcp_numlayers = (int)numlayers;
881 numresolution = (OPJ_UINT32)parameters->numresolution;
882 matrix_width = numresolution * 3;
883 parameters->cp_matrice = (int *) malloc(sizeof(int) * numlayers * matrix_width);
884 if (parameters->cp_matrice == NULL) {
889 for (i = 0; i < numlayers; i++) {
890 row = ¶meters->cp_matrice[i * matrix_width];
892 parameters->tcp_rates[i] = 1;
893 sscanf(s, "%d,", &col[0]);
900 for (j = 1; j < numresolution; j++) {
902 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
914 if (i < numlayers - 1) {
918 parameters->cp_fixed_alloc = 1;
922 /* ----------------------------------------------------- */
924 case 't': { /* tiles */
925 sscanf(opj_optarg, "%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy);
926 parameters->tile_size_on = OPJ_TRUE;
930 /* ----------------------------------------------------- */
931 case 'X': { /* target bitdepth */
932 char *s = opj_optarg;
933 sscanf(s, "%u", pTarget_bitdepth);
934 if (*pTarget_bitdepth == 0) {
935 fprintf(stderr, "Target bitdepth must be at least 1 bit.\n");
941 /* ----------------------------------------------------- */
942 case 'G': { /* guard bits */
943 char *s = opj_optarg;
944 sscanf(s, "%d", pOutGuardBits);
948 /* ----------------------------------------------------- */
950 case 'n': { /* resolution */
951 sscanf(opj_optarg, "%d", ¶meters->numresolution);
955 /* ----------------------------------------------------- */
956 case 'c': { /* precinct dimension */
960 char *s = opj_optarg;
964 ret = sscanf(s, "[%d,%d]%c", ¶meters->prcw_init[res_spec],
965 ¶meters->prch_init[res_spec], &sep);
966 if (!(ret == 2 && sep == 0) && !(ret == 3 && sep == ',')) {
967 fprintf(stderr, "\nError: could not parse precinct dimension: '%s' %x\n", s,
969 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n");
972 parameters->csty |= 0x01;
974 s = strpbrk(s, "]") + 2;
975 } while (sep == ',');
976 parameters->res_spec = res_spec;
980 /* ----------------------------------------------------- */
982 case 'b': { /* code-block dimension */
983 int cblockw_init = 0, cblockh_init = 0;
984 sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init);
985 if (cblockw_init > 1024 || cblockw_init < 4 ||
986 cblockh_init > 1024 || cblockh_init < 4 ||
987 cblockw_init * cblockh_init > 4096) {
989 "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
990 " * width*height<=4096\n * 4<=width,height<= 1024\n\n");
993 parameters->cblockw_init = cblockw_init;
994 parameters->cblockh_init = cblockh_init;
998 /* ----------------------------------------------------- */
1000 case 'x': { /* creation of index file */
1001 if (opj_strcpy_s(indexfilename, indexfilename_size, opj_optarg) != 0) {
1004 /* FIXME ADE INDEX >> */
1006 "[WARNING] Index file generation is currently broken.\n"
1007 " '-x' option ignored.\n");
1008 /* << FIXME ADE INDEX */
1012 /* ----------------------------------------------------- */
1014 case 'p': { /* progression order */
1015 char progression[5];
1017 strncpy(progression, opj_optarg, 4);
1019 parameters->prog_order = give_progression(progression);
1020 if (parameters->prog_order == -1) {
1021 fprintf(stderr, "Unrecognized progression order "
1022 "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
1028 /* ----------------------------------------------------- */
1030 case 's': { /* subsampling factor */
1031 if (sscanf(opj_optarg, "%d,%d", ¶meters->subsampling_dx,
1032 ¶meters->subsampling_dy) != 2) {
1033 fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
1039 /* ----------------------------------------------------- */
1041 case 'd': { /* coordonnate of the reference grid */
1042 if (sscanf(opj_optarg, "%d,%d", ¶meters->image_offset_x0,
1043 ¶meters->image_offset_y0) != 2) {
1044 fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
1045 "error !! [-d x0,y0]\n");
1051 /* ----------------------------------------------------- */
1053 case 'h': /* display an help description */
1054 encode_help_display();
1057 /* ----------------------------------------------------- */
1059 case 'P': { /* POC */
1060 int numpocs = 0; /* number of progression order change (POC) default 0 */
1061 opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
1063 char *s = opj_optarg;
1064 POC = parameters->POC;
1066 while (sscanf(s, "T%u=%u,%u,%u,%u,%u,%4s", &POC[numpocs].tile,
1067 &POC[numpocs].resno0, &POC[numpocs].compno0,
1068 &POC[numpocs].layno1, &POC[numpocs].resno1,
1069 &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
1070 POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
1072 while (*s && *s != '/') {
1080 parameters->numpocs = (OPJ_UINT32)numpocs;
1084 /* ------------------------------------------------------ */
1086 case 'S': { /* SOP marker */
1087 parameters->csty |= 0x02;
1091 /* ------------------------------------------------------ */
1093 case 'E': { /* EPH marker */
1094 parameters->csty |= 0x04;
1098 /* ------------------------------------------------------ */
1100 case 'M': { /* Mode switch pas tous au point !! */
1102 if (sscanf(opj_optarg, "%d", &value) == 1) {
1103 for (i = 0; i <= 5; i++) {
1104 int cache = value & (1 << i);
1106 parameters->mode |= (1 << i);
1113 /* ------------------------------------------------------ */
1115 case 'R': { /* ROI */
1116 if (sscanf(opj_optarg, "c=%d,U=%d", ¶meters->roi_compno,
1117 ¶meters->roi_shift) != 2) {
1118 fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n");
1124 /* ------------------------------------------------------ */
1126 case 'T': { /* Tile offset */
1127 if (sscanf(opj_optarg, "%d,%d", ¶meters->cp_tx0,
1128 ¶meters->cp_ty0) != 2) {
1129 fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
1135 /* ------------------------------------------------------ */
1137 case 'C': { /* add a comment */
1138 parameters->cp_comment = (char*)malloc(strlen(opj_optarg) + 1);
1139 if (parameters->cp_comment) {
1140 strcpy(parameters->cp_comment, opj_optarg);
1146 /* ------------------------------------------------------ */
1148 case 'I': { /* reversible or not */
1149 parameters->irreversible = 1;
1153 /* ------------------------------------------------------ */
1155 case 'u': { /* Tile part generation*/
1156 parameters->tp_flag = opj_optarg[0];
1157 parameters->tp_on = 1;
1161 /* ------------------------------------------------------ */
1163 case 'z': { /* Image Directory path */
1164 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
1165 if (img_fol->imgdirpath == NULL) {
1168 strcpy(img_fol->imgdirpath, opj_optarg);
1169 img_fol->set_imgdir = 1;
1173 /* ------------------------------------------------------ */
1175 case 'w': { /* Digital Cinema 2K profile compliance*/
1177 sscanf(opj_optarg, "%d", &fps);
1179 parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
1180 parameters->max_comp_size = OPJ_CINEMA_24_COMP;
1181 parameters->max_cs_size = OPJ_CINEMA_24_CS;
1182 } else if (fps == 48) {
1183 parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
1184 parameters->max_comp_size = OPJ_CINEMA_48_COMP;
1185 parameters->max_cs_size = OPJ_CINEMA_48_CS;
1187 fprintf(stderr, "Incorrect value!! must be 24 or 48\n");
1190 fprintf(stdout, "CINEMA 2K profile activated\n"
1191 "Other options specified could be overridden\n");
1196 /* ------------------------------------------------------ */
1198 case 'y': { /* Digital Cinema 4K profile compliance*/
1199 parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
1200 fprintf(stdout, "CINEMA 4K profile activated\n"
1201 "Other options specified could be overridden\n");
1205 /* ------------------------------------------------------ */
1207 case 'Z': { /* IMF profile*/
1213 "Wrong value for -IMF. Should be "
1214 "<PROFILE>[,mainlevel=X][,sublevel=Y][,framerate=FPS] where <PROFILE> is one "
1215 "of 2K/4K/8K/2K_R/4K_R/8K_R.\n";
1218 comma = strstr(opj_optarg, ",mainlevel=");
1219 if (comma && sscanf(comma + 1, "mainlevel=%d", &mainlevel) != 1) {
1220 fprintf(stderr, "%s", msg);
1224 comma = strstr(opj_optarg, ",sublevel=");
1225 if (comma && sscanf(comma + 1, "sublevel=%d", &sublevel) != 1) {
1226 fprintf(stderr, "%s", msg);
1230 comma = strstr(opj_optarg, ",framerate=");
1231 if (comma && sscanf(comma + 1, "framerate=%d", &framerate) != 1) {
1232 fprintf(stderr, "%s", msg);
1236 comma = strchr(opj_optarg, ',');
1237 if (comma != NULL) {
1241 if (strcmp(opj_optarg, "2K") == 0) {
1242 profile = OPJ_PROFILE_IMF_2K;
1243 } else if (strcmp(opj_optarg, "4K") == 0) {
1244 profile = OPJ_PROFILE_IMF_4K;
1245 } else if (strcmp(opj_optarg, "8K") == 0) {
1246 profile = OPJ_PROFILE_IMF_8K;
1247 } else if (strcmp(opj_optarg, "2K_R") == 0) {
1248 profile = OPJ_PROFILE_IMF_2K_R;
1249 } else if (strcmp(opj_optarg, "4K_R") == 0) {
1250 profile = OPJ_PROFILE_IMF_4K_R;
1251 } else if (strcmp(opj_optarg, "8K_R") == 0) {
1252 profile = OPJ_PROFILE_IMF_8K_R;
1254 fprintf(stderr, "%s", msg);
1258 if (!(mainlevel >= 0 && mainlevel <= 15)) {
1259 /* Voluntarily rough validation. More fine grained done in library */
1260 fprintf(stderr, "Invalid mainlevel value.\n");
1263 if (!(sublevel >= 0 && sublevel <= 15)) {
1264 /* Voluntarily rough validation. More fine grained done in library */
1265 fprintf(stderr, "Invalid sublevel value.\n");
1268 parameters->rsiz = (OPJ_UINT16)(profile | (sublevel << 4) | mainlevel);
1270 fprintf(stdout, "IMF profile activated\n"
1271 "Other options specified could be overridden\n");
1273 if (pOutFramerate) {
1274 *pOutFramerate = framerate;
1276 if (framerate > 0 && sublevel > 0 && sublevel <= 9) {
1277 const int limitMBitsSec[] = {
1279 OPJ_IMF_SUBLEVEL_1_MBITSSEC,
1280 OPJ_IMF_SUBLEVEL_2_MBITSSEC,
1281 OPJ_IMF_SUBLEVEL_3_MBITSSEC,
1282 OPJ_IMF_SUBLEVEL_4_MBITSSEC,
1283 OPJ_IMF_SUBLEVEL_5_MBITSSEC,
1284 OPJ_IMF_SUBLEVEL_6_MBITSSEC,
1285 OPJ_IMF_SUBLEVEL_7_MBITSSEC,
1286 OPJ_IMF_SUBLEVEL_8_MBITSSEC,
1287 OPJ_IMF_SUBLEVEL_9_MBITSSEC
1289 parameters->max_cs_size = limitMBitsSec[sublevel] * (1000 * 1000 / 8) /
1291 fprintf(stdout, "Setting max codestream size to %d bytes.\n",
1292 parameters->max_cs_size);
1297 /* ------------------------------------------------------ */
1299 case 'Y': { /* Shall we do an MCT ? 0:no_mct;1:rgb->ycc;2:custom mct (-m option required)*/
1301 sscanf(opj_optarg, "%d", &mct_mode);
1302 if (mct_mode < 0 || mct_mode > 2) {
1304 "MCT incorrect value!! Current accepted values are 0, 1 or 2.\n");
1307 parameters->tcp_mct = (char) mct_mode;
1311 /* ------------------------------------------------------ */
1314 case 'm': { /* mct input file */
1315 char *lFilename = opj_optarg;
1318 float *lCurrentDoublePtr;
1321 int lNbComp = 0, lTotalComp, lMctComp, i2;
1322 size_t lStrLen, lStrFread;
1325 FILE * lFile = fopen(lFilename, "r");
1326 if (lFile == NULL) {
1330 /* Set size of file and read its content*/
1331 fseek(lFile, 0, SEEK_END);
1332 lStrLen = (size_t)ftell(lFile);
1333 fseek(lFile, 0, SEEK_SET);
1334 lMatrix = (char *) malloc(lStrLen + 1);
1335 if (lMatrix == NULL) {
1339 lStrFread = fread(lMatrix, 1, lStrLen, lFile);
1341 if (lStrLen != lStrFread) {
1346 lMatrix[lStrLen] = 0;
1347 lCurrentPtr = lMatrix;
1349 /* replace ',' by 0 */
1350 while (*lCurrentPtr != 0) {
1351 if (*lCurrentPtr == ' ') {
1358 lCurrentPtr = lMatrix;
1360 lNbComp = (int)(sqrt(4 * lNbComp + 1) / 2. - 0.5);
1361 lMctComp = lNbComp * lNbComp;
1362 lTotalComp = lMctComp + lNbComp;
1363 lSpace = (float *) malloc((size_t)lTotalComp * sizeof(float));
1364 if (lSpace == NULL) {
1368 lCurrentDoublePtr = lSpace;
1369 for (i2 = 0; i2 < lMctComp; ++i2) {
1370 lStrLen = strlen(lCurrentPtr) + 1;
1371 *lCurrentDoublePtr++ = (float) atof(lCurrentPtr);
1372 lCurrentPtr += lStrLen;
1375 l_int_ptr = (int*) lCurrentDoublePtr;
1376 for (i2 = 0; i2 < lNbComp; ++i2) {
1377 lStrLen = strlen(lCurrentPtr) + 1;
1378 *l_int_ptr++ = atoi(lCurrentPtr);
1379 lCurrentPtr += lStrLen;
1382 /* TODO should not be here ! */
1383 opj_set_MCT(parameters, lSpace, (int *)(lSpace + lMctComp),
1384 (OPJ_UINT32)lNbComp);
1393 /* ------------------------------------------------------ */
1397 /* ------------------------------------------------------ */
1399 case 'W': { /* JPWL capabilities switched on */
1401 int hprot, pprot, sens, addr, size, range;
1403 /* we need to enable indexing */
1404 if (!indexfilename || !*indexfilename) {
1405 if (opj_strcpy_s(indexfilename, indexfilename_size,
1406 JPWL_PRIVATEINDEX_NAME) != 0) {
1411 /* search for different protection methods */
1413 /* break the option in comma points and parse the result */
1414 token = strtok(opj_optarg, ",");
1415 while (token != NULL) {
1417 /* search header error protection method */
1418 if (*token == 'h') {
1420 static int tile = 0, tilespec = 0, lasttileno = 0;
1422 hprot = 1; /* predefined method */
1424 if (sscanf(token, "h=%d", &hprot) == 1) {
1425 /* Main header, specified */
1426 if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
1427 ((hprot >= 37) && (hprot <= 128)))) {
1428 fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n",
1432 parameters->jpwl_hprot_MH = hprot;
1434 } else if (sscanf(token, "h%d=%d", &tile, &hprot) == 2) {
1435 /* Tile part header, specified */
1436 if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
1437 ((hprot >= 37) && (hprot <= 128)))) {
1438 fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n",
1444 "ERROR -> invalid tile part number on protection method t = %d\n", tile);
1447 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1448 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
1449 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
1452 } else if (sscanf(token, "h%d", &tile) == 1) {
1453 /* Tile part header, unspecified */
1456 "ERROR -> invalid tile part number on protection method t = %d\n", tile);
1459 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1460 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
1461 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
1465 } else if (!strcmp(token, "h")) {
1466 /* Main header, unspecified */
1467 parameters->jpwl_hprot_MH = hprot;
1470 fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
1476 /* search packet error protection method */
1477 if (*token == 'p') {
1479 static int pack = 0, tile = 0, packspec = 0;
1481 pprot = 1; /* predefined method */
1483 if (sscanf(token, "p=%d", &pprot) == 1) {
1484 /* Method for all tiles and all packets */
1485 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1486 ((pprot >= 37) && (pprot <= 128)))) {
1487 fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n",
1491 parameters->jpwl_pprot_tileno[0] = 0;
1492 parameters->jpwl_pprot_packno[0] = 0;
1493 parameters->jpwl_pprot[0] = pprot;
1495 } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) {
1496 /* method specified from that tile on */
1497 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1498 ((pprot >= 37) && (pprot <= 128)))) {
1499 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1504 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1507 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1508 parameters->jpwl_pprot_tileno[packspec] = tile;
1509 parameters->jpwl_pprot_packno[packspec] = 0;
1510 parameters->jpwl_pprot[packspec++] = pprot;
1513 } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) {
1514 /* method fully specified from that tile and that packet on */
1515 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1516 ((pprot >= 37) && (pprot <= 128)))) {
1517 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1522 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1526 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
1530 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1531 parameters->jpwl_pprot_tileno[packspec] = tile;
1532 parameters->jpwl_pprot_packno[packspec] = pack;
1533 parameters->jpwl_pprot[packspec++] = pprot;
1536 } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) {
1537 /* default method from that tile and that packet on */
1538 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
1539 ((pprot >= 37) && (pprot <= 128)))) {
1540 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
1545 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1549 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
1553 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1554 parameters->jpwl_pprot_tileno[packspec] = tile;
1555 parameters->jpwl_pprot_packno[packspec] = pack;
1556 parameters->jpwl_pprot[packspec++] = pprot;
1559 } else if (sscanf(token, "p%d", &tile) == 1) {
1560 /* default from a tile on */
1563 "ERROR -> invalid tile part number on protection method p = %d\n", tile);
1566 if (packspec < JPWL_MAX_NO_PACKSPECS) {
1567 parameters->jpwl_pprot_tileno[packspec] = tile;
1568 parameters->jpwl_pprot_packno[packspec] = 0;
1569 parameters->jpwl_pprot[packspec++] = pprot;
1573 } else if (!strcmp(token, "p")) {
1575 parameters->jpwl_pprot_tileno[0] = 0;
1576 parameters->jpwl_pprot_packno[0] = 0;
1577 parameters->jpwl_pprot[0] = pprot;
1580 fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
1586 /* search sensitivity method */
1587 if (*token == 's') {
1589 static int tile = 0, tilespec = 0, lasttileno = 0;
1591 sens = 0; /* predefined: relative error */
1593 if (sscanf(token, "s=%d", &sens) == 1) {
1594 /* Main header, specified */
1595 if ((sens < -1) || (sens > 7)) {
1596 fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n",
1600 parameters->jpwl_sens_MH = sens;
1602 } else if (sscanf(token, "s%d=%d", &tile, &sens) == 2) {
1603 /* Tile part header, specified */
1604 if ((sens < -1) || (sens > 7)) {
1605 fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n",
1611 "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
1614 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1615 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
1616 parameters->jpwl_sens_TPH[tilespec++] = sens;
1619 } else if (sscanf(token, "s%d", &tile) == 1) {
1620 /* Tile part header, unspecified */
1623 "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
1626 if (tilespec < JPWL_MAX_NO_TILESPECS) {
1627 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
1628 parameters->jpwl_sens_TPH[tilespec++] = hprot;
1631 } else if (!strcmp(token, "s")) {
1632 /* Main header, unspecified */
1633 parameters->jpwl_sens_MH = sens;
1636 fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token);
1640 parameters->jpwl_sens_size = 2; /* 2 bytes for default size */
1643 /* search addressing size */
1644 if (*token == 'a') {
1647 addr = 0; /* predefined: auto */
1649 if (sscanf(token, "a=%d", &addr) == 1) {
1651 if ((addr != 0) && (addr != 2) && (addr != 4)) {
1652 fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr);
1655 parameters->jpwl_sens_addr = addr;
1657 } else if (!strcmp(token, "a")) {
1659 parameters->jpwl_sens_addr = addr; /* auto for default size */
1662 fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token);
1668 /* search sensitivity size */
1669 if (*token == 'z') {
1672 size = 1; /* predefined: 1 byte */
1674 if (sscanf(token, "z=%d", &size) == 1) {
1676 if ((size != 0) && (size != 1) && (size != 2)) {
1677 fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size);
1680 parameters->jpwl_sens_size = size;
1682 } else if (!strcmp(token, "a")) {
1684 parameters->jpwl_sens_size = size; /* 1 for default size */
1687 fprintf(stderr, "ERROR -> invalid size selection = %s\n", token);
1693 /* search range method */
1694 if (*token == 'g') {
1697 range = 0; /* predefined: 0 (packet) */
1699 if (sscanf(token, "g=%d", &range) == 1) {
1701 if ((range < 0) || (range > 3)) {
1702 fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range);
1705 parameters->jpwl_sens_range = range;
1707 } else if (!strcmp(token, "g")) {
1709 parameters->jpwl_sens_range = range;
1712 fprintf(stderr, "ERROR -> invalid range selection = %s\n", token);
1718 /* next token or bust */
1719 token = strtok(NULL, ",");
1724 fprintf(stdout, "Info: JPWL capabilities enabled\n");
1725 parameters->jpwl_epc_on = OPJ_TRUE;
1729 #endif /* USE_JPWL */
1731 /* ------------------------------------------------------ */
1733 case 'J': { /* jpip on */
1734 parameters->jpip_on = OPJ_TRUE;
1737 /* ------------------------------------------------------ */
1739 case 'A': { /* PLT markers */
1740 *pOutPLT = OPJ_TRUE;
1744 /* ----------------------------------------------------- */
1745 case 'B': { /* Number of threads */
1746 if (strcmp(opj_optarg, "ALL_CPUS") == 0) {
1747 *pOutNumThreads = opj_get_num_cpus();
1748 if (*pOutNumThreads == 1) {
1749 *pOutNumThreads = 0;
1752 sscanf(opj_optarg, "%d", pOutNumThreads);
1756 /* ------------------------------------------------------ */
1758 case 'D': { /* TLM markers */
1759 *pOutTLM = OPJ_TRUE;
1763 /* ------------------------------------------------------ */
1767 fprintf(stderr, "[WARNING] An invalid option has been ignored\n");
1772 if (img_fol->set_imgdir == 1) {
1773 if (!(parameters->infile[0] == 0)) {
1774 fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together !!\n");
1777 if (img_fol->set_out_format == 0) {
1779 "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
1780 fprintf(stderr, "Only one format allowed! Valid formats are j2k and jp2!!\n");
1783 if (!((parameters->outfile[0] == 0))) {
1784 fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together !!\n");
1785 fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n");
1789 if ((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
1790 fprintf(stderr, "[ERROR] Required parameters are missing\n"
1791 "Example: %s -i image.pgm -o image.j2k\n", argv[0]);
1792 fprintf(stderr, " Help: %s -h\n", argv[0]);
1797 if ((parameters->decod_format == RAW_DFMT ||
1798 parameters->decod_format == RAWL_DFMT)
1799 && (raw_cp->rawWidth == 0)) {
1800 fprintf(stderr, "[ERROR] invalid raw or yuv image parameters\n");
1801 fprintf(stderr, "Please use the Format option -F:\n");
1803 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
1804 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
1805 fprintf(stderr, "Aborting\n");
1809 if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc ||
1810 parameters->cp_fixed_quality)
1811 && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^
1812 parameters->cp_fixed_quality))) {
1813 fprintf(stderr, "[ERROR] options -r -q and -f cannot be used together !!\n");
1815 } /* mod fixed_quality */
1818 /* if no rate entered, lossless by default */
1819 /* Note: post v2.2.0, this is no longer necessary, but for released */
1820 /* versions at the time of writing, this is needed to avoid crashes */
1821 if (parameters->tcp_numlayers == 0) {
1822 parameters->tcp_rates[0] = 0;
1823 parameters->tcp_numlayers++;
1824 parameters->cp_disto_alloc = 1;
1827 if ((parameters->cp_tx0 > parameters->image_offset_x0) ||
1828 (parameters->cp_ty0 > parameters->image_offset_y0)) {
1830 "[ERROR] Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
1831 parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0,
1832 parameters->image_offset_y0);
1836 for (i = 0; i < parameters->numpocs; i++) {
1837 if (parameters->POC[i].prg == -1) {
1839 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
1844 /* If subsampled image is provided, automatically disable MCT */
1845 if (((parameters->decod_format == RAW_DFMT) ||
1846 (parameters->decod_format == RAWL_DFMT))
1847 && (((raw_cp->rawComp > 1) && ((raw_cp->rawComps[1].dx > 1) ||
1848 (raw_cp->rawComps[1].dy > 1)))
1849 || ((raw_cp->rawComp > 2) && ((raw_cp->rawComps[2].dx > 1) ||
1850 (raw_cp->rawComps[2].dy > 1)))
1852 parameters->tcp_mct = 0;
1859 /* -------------------------------------------------------------------------- */
1862 sample error debug callback expecting no client object
1864 static void error_callback(const char *msg, void *client_data)
1867 fprintf(stdout, "[ERROR] %s", msg);
1870 sample warning debug callback expecting no client object
1872 static void warning_callback(const char *msg, void *client_data)
1875 fprintf(stdout, "[WARNING] %s", msg);
1878 sample debug callback expecting no client object
1880 static void info_callback(const char *msg, void *client_data)
1883 fprintf(stdout, "[INFO] %s", msg);
1886 OPJ_FLOAT64 opj_clock(void)
1889 /* _WIN32: use QueryPerformance (very accurate) */
1890 LARGE_INTEGER freq, t ;
1891 /* freq is the clock speed of the CPU */
1892 QueryPerformanceFrequency(&freq) ;
1893 /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
1894 /* t is the high resolution performance counter (see MSDN) */
1895 QueryPerformanceCounter(& t) ;
1896 return freq.QuadPart ? ((OPJ_FLOAT64) t.QuadPart / (OPJ_FLOAT64) freq.QuadPart)
1899 /* Unix or Linux: use resource usage */
1901 OPJ_FLOAT64 procTime;
1902 /* (1) Get the rusage data structure at this moment (man getrusage) */
1904 /* (2) What is the elapsed time ? - CPU time = User time + System time */
1905 /* (2a) Get the seconds */
1906 procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
1907 /* (2b) More precisely! Get the microseconds part ! */
1908 return (procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) *
1914 /* -------------------------------------------------------------------------- */
1918 /* -------------------------------------------------------------------------- */
1919 int main(int argc, char **argv)
1922 opj_cparameters_t parameters; /* compression parameters */
1924 opj_stream_t *l_stream = 00;
1925 opj_codec_t* l_codec = 00;
1926 opj_image_t *image = NULL;
1927 raw_cparameters_t raw_cp;
1928 OPJ_SIZE_T num_compressed_files = 0;
1930 char indexfilename[OPJ_PATH_LEN]; /* index file name */
1932 unsigned int i, num_images, imageno;
1934 dircnt_t *dirptr = NULL;
1939 OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
1940 OPJ_UINT32 l_nb_tiles = 4;
1942 OPJ_FLOAT64 t = opj_clock();
1944 OPJ_BOOL PLT = OPJ_FALSE;
1945 OPJ_BOOL TLM = OPJ_FALSE;
1946 int num_threads = 0;
1947 int guard_bits = -1;
1949 /** desired bitdepth from input file */
1950 unsigned int target_bitdepth = 0;
1952 /* set encoding parameters to default values */
1953 opj_set_default_encoder_parameters(¶meters);
1955 /* Initialize indexfilename and img_fol */
1957 memset(&img_fol, 0, sizeof(img_fol_t));
1959 /* raw_cp initialization */
1960 raw_cp.rawBitDepth = 0;
1962 raw_cp.rawComps = 0;
1963 raw_cp.rawHeight = 0;
1964 raw_cp.rawSigned = 0;
1965 raw_cp.rawWidth = 0;
1967 /* parse input and get user encoding parameters */
1968 parameters.tcp_mct = (char)
1969 255; /* This will be set later according to the input image or the provided option */
1970 if (parse_cmdline_encoder(argc, argv, ¶meters, &img_fol, &raw_cp,
1971 indexfilename, sizeof(indexfilename), &framerate, &PLT, &TLM,
1972 &guard_bits, &num_threads, &target_bitdepth) == 1) {
1977 /* Read directory if necessary */
1978 if (img_fol.set_imgdir == 1) {
1979 num_images = get_num_images(img_fol.imgdirpath);
1980 if (num_images == 0) {
1981 fprintf(stdout, "Folder is empty\n");
1985 dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
1987 dirptr->filename_buf = (char*)calloc(num_images, OPJ_PATH_LEN * sizeof(
1988 char)); /* Stores at max 10 image file names*/
1989 dirptr->filename = (char**) calloc(num_images, sizeof(char*));
1990 if (!dirptr->filename_buf) {
1994 for (i = 0; i < num_images; i++) {
1995 dirptr->filename[i] = dirptr->filename_buf + (size_t)i * OPJ_PATH_LEN;
1998 if (load_images(dirptr, img_fol.imgdirpath) == 1) {
2006 /*Encoding image one by one*/
2007 for (imageno = 0; imageno < num_images; imageno++) {
2009 fprintf(stderr, "\n");
2011 if (img_fol.set_imgdir == 1) {
2012 if (get_next_file(imageno, dirptr, &img_fol, ¶meters)) {
2013 fprintf(stderr, "skipping file...\n");
2018 switch (parameters.decod_format) {
2029 fprintf(stderr, "skipping file...\n");
2033 /* decode the source image */
2034 /* ----------------------- */
2036 switch (parameters.decod_format) {
2038 image = pgxtoimage(parameters.infile, ¶meters);
2040 fprintf(stderr, "Unable to load pgx file\n");
2047 image = pnmtoimage(parameters.infile, ¶meters);
2049 fprintf(stderr, "Unable to load pnm file\n");
2056 image = bmptoimage(parameters.infile, ¶meters);
2058 fprintf(stderr, "Unable to load bmp file\n");
2064 #ifdef OPJ_HAVE_LIBTIFF
2066 image = tiftoimage(parameters.infile, ¶meters, target_bitdepth);
2068 fprintf(stderr, "Unable to load tif(f) file\n");
2073 #endif /* OPJ_HAVE_LIBTIFF */
2076 image = rawtoimage(parameters.infile, ¶meters, &raw_cp);
2078 fprintf(stderr, "Unable to load raw or yuv file\n");
2085 image = rawltoimage(parameters.infile, ¶meters, &raw_cp);
2087 fprintf(stderr, "Unable to load raw file\n");
2094 image = tgatoimage(parameters.infile, ¶meters);
2096 fprintf(stderr, "Unable to load tga file\n");
2102 #ifdef OPJ_HAVE_LIBPNG
2104 image = pngtoimage(parameters.infile, ¶meters);
2106 fprintf(stderr, "Unable to load png file\n");
2111 #endif /* OPJ_HAVE_LIBPNG */
2114 /* Can happen if input file is TIF(F) or PNG
2115 * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
2118 fprintf(stderr, "Unable to load file: got no image\n");
2123 /* Decide if MCT should be used */
2124 if (parameters.tcp_mct == (char)
2125 255) { /* mct mode has not been set in commandline */
2126 parameters.tcp_mct = (image->numcomps >= 3) ? 1 : 0;
2127 } else { /* mct mode has been set in commandline */
2128 if ((parameters.tcp_mct == 1) && (image->numcomps < 3)) {
2129 fprintf(stderr, "RGB->YCC conversion cannot be used:\n");
2130 fprintf(stderr, "Input image has less than 3 components\n");
2134 if ((parameters.tcp_mct == 2) && (!parameters.mct_data)) {
2135 fprintf(stderr, "Custom MCT has been set but no array-based MCT\n");
2136 fprintf(stderr, "has been provided. Aborting.\n");
2142 if (OPJ_IS_IMF(parameters.rsiz) && framerate > 0) {
2143 const int mainlevel = OPJ_GET_IMF_MAINLEVEL(parameters.rsiz);
2144 if (mainlevel > 0 && mainlevel <= OPJ_IMF_MAINLEVEL_MAX) {
2145 const int limitMSamplesSec[] = {
2147 OPJ_IMF_MAINLEVEL_1_MSAMPLESEC,
2148 OPJ_IMF_MAINLEVEL_2_MSAMPLESEC,
2149 OPJ_IMF_MAINLEVEL_3_MSAMPLESEC,
2150 OPJ_IMF_MAINLEVEL_4_MSAMPLESEC,
2151 OPJ_IMF_MAINLEVEL_5_MSAMPLESEC,
2152 OPJ_IMF_MAINLEVEL_6_MSAMPLESEC,
2153 OPJ_IMF_MAINLEVEL_7_MSAMPLESEC,
2154 OPJ_IMF_MAINLEVEL_8_MSAMPLESEC,
2155 OPJ_IMF_MAINLEVEL_9_MSAMPLESEC,
2156 OPJ_IMF_MAINLEVEL_10_MSAMPLESEC,
2157 OPJ_IMF_MAINLEVEL_11_MSAMPLESEC
2159 OPJ_UINT32 avgcomponents = image->numcomps;
2160 double msamplespersec;
2161 if (image->numcomps == 3 &&
2162 image->comps[1].dx == 2 &&
2163 image->comps[1].dy == 2) {
2166 msamplespersec = (double)image->x1 * image->y1 * avgcomponents * framerate /
2168 if (msamplespersec > limitMSamplesSec[mainlevel]) {
2170 "Warning: MSamples/sec is %f, whereas limit is %d.\n",
2172 limitMSamplesSec[mainlevel]);
2177 /* encode the destination image */
2178 /* ---------------------------- */
2180 switch (parameters.cod_format) {
2181 case J2K_CFMT: { /* JPEG-2000 codestream */
2182 /* Get a decoder handle */
2183 l_codec = opj_create_compress(OPJ_CODEC_J2K);
2186 case JP2_CFMT: { /* JPEG 2000 compressed image data */
2187 /* Get a decoder handle */
2188 l_codec = opj_create_compress(OPJ_CODEC_JP2);
2192 fprintf(stderr, "skipping file..\n");
2193 opj_stream_destroy(l_stream);
2197 /* catch events using our callbacks and give a local context */
2198 opj_set_info_handler(l_codec, info_callback, 00);
2199 opj_set_warning_handler(l_codec, warning_callback, 00);
2200 opj_set_error_handler(l_codec, error_callback, 00);
2203 parameters.cp_tx0 = 0;
2204 parameters.cp_ty0 = 0;
2205 parameters.tile_size_on = OPJ_TRUE;
2206 parameters.cp_tdx = 512;
2207 parameters.cp_tdy = 512;
2209 if (! opj_setup_encoder(l_codec, ¶meters, image)) {
2210 fprintf(stderr, "failed to encode image: opj_setup_encoder\n");
2211 opj_destroy_codec(l_codec);
2212 opj_image_destroy(image);
2218 const char* options[4] = { NULL, NULL, NULL, NULL };
2220 char szGuardBits[32];
2222 options[iOpt++] = "PLT=YES";
2225 options[iOpt++] = "TLM=YES";
2227 if (guard_bits >= 0) {
2228 sprintf(szGuardBits, "GUARD_BITS=%d", guard_bits);
2229 options[iOpt++] = szGuardBits;
2231 if (iOpt > 0 && !opj_encoder_set_extra_options(l_codec, options)) {
2232 fprintf(stderr, "failed to encode image: opj_encoder_set_extra_options\n");
2233 opj_destroy_codec(l_codec);
2234 opj_image_destroy(image);
2240 if (num_threads >= 1 &&
2241 !opj_codec_set_threads(l_codec, num_threads)) {
2242 fprintf(stderr, "failed to set number of threads\n");
2243 opj_destroy_codec(l_codec);
2244 opj_image_destroy(image);
2249 /* open a byte stream for writing and allocate memory for all tiles */
2250 l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE);
2256 /* encode the image */
2257 bSuccess = opj_start_compress(l_codec, image, l_stream);
2259 fprintf(stderr, "failed to encode image: opj_start_compress\n");
2261 if (bSuccess && bUseTiles) {
2263 OPJ_UINT32 l_data_size = 512 * 512 * 3;
2264 l_data = (OPJ_BYTE*) calloc(1, l_data_size);
2265 if (l_data == NULL) {
2269 for (i = 0; i < l_nb_tiles; ++i) {
2270 if (! opj_write_tile(l_codec, i, l_data, l_data_size, l_stream)) {
2271 fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %u!\n",
2273 opj_stream_destroy(l_stream);
2274 opj_destroy_codec(l_codec);
2275 opj_image_destroy(image);
2282 bSuccess = bSuccess && opj_encode(l_codec, l_stream);
2284 fprintf(stderr, "failed to encode image: opj_encode\n");
2287 bSuccess = bSuccess && opj_end_compress(l_codec, l_stream);
2289 fprintf(stderr, "failed to encode image: opj_end_compress\n");
2293 opj_stream_destroy(l_stream);
2294 opj_destroy_codec(l_codec);
2295 opj_image_destroy(image);
2296 fprintf(stderr, "failed to encode image\n");
2297 remove(parameters.outfile);
2302 num_compressed_files++;
2303 fprintf(stdout, "[INFO] Generated outfile %s\n", parameters.outfile);
2304 /* close and free the byte stream */
2305 opj_stream_destroy(l_stream);
2307 /* free remaining compression structures */
2308 opj_destroy_codec(l_codec);
2310 /* free image data */
2311 opj_image_destroy(image);
2315 t = opj_clock() - t;
2316 if (num_compressed_files) {
2317 fprintf(stdout, "encode time: %d ms \n",
2318 (int)((t * 1000.0) / (OPJ_FLOAT64)num_compressed_files));
2324 if (parameters.cp_comment) {
2325 free(parameters.cp_comment);
2327 if (parameters.cp_matrice) {
2328 free(parameters.cp_matrice);
2330 if (raw_cp.rawComps) {
2331 free(raw_cp.rawComps);
2333 if (img_fol.imgdirpath) {
2334 free(img_fol.imgdirpath);
2337 if (dirptr->filename_buf) {
2338 free(dirptr->filename_buf);
2340 if (dirptr->filename) {
2341 free(dirptr->filename);