2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
\r
3 * Copyright (c) 2002-2007, Professor Benoit Macq
\r
4 * Copyright (c) 2001-2003, David Janssens
\r
5 * Copyright (c) 2002-2003, Yannick Verschueren
\r
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
\r
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
\r
8 * Copyright (c) 2006-2007, Parvatha Elangovan
\r
9 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
\r
10 * All rights reserved.
\r
12 * Redistribution and use in source and binary forms, with or without
\r
13 * modification, are permitted provided that the following conditions
\r
15 * 1. Redistributions of source code must retain the above copyright
\r
16 * notice, this list of conditions and the following disclaimer.
\r
17 * 2. Redistributions in binary form must reproduce the above copyright
\r
18 * notice, this list of conditions and the following disclaimer in the
\r
19 * documentation and/or other materials provided with the distribution.
\r
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
\r
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
31 * POSSIBILITY OF SUCH DAMAGE.
\r
38 #define USE_OPJ_DEPRECATED
\r
39 #include "openjpeg.h"
\r
40 #include "compat/getopt.h"
\r
41 #include "convert.h"
\r
46 #define stricmp strcasecmp
\r
47 #define strnicmp strncasecmp
\r
50 /* ----------------------------------------------------------------------- */
\r
64 /* ----------------------------------------------------------------------- */
\r
65 #define CINEMA_24_CS 1302083 /*Codestream length for 24fps*/
\r
66 #define CINEMA_48_CS 651041 /*Codestream length for 48fps*/
\r
67 #define COMP_24_CS 1041666 /*Maximum size per color component for 2K & 4K @ 24fps*/
\r
68 #define COMP_48_CS 520833 /*Maximum size per color component for 2K @ 48fps*/
\r
70 typedef struct dircnt{
\r
71 /** Buffer for holding images read from Directory*/
\r
73 /** Pointer to the buffer*/
\r
77 typedef struct img_folder{
\r
78 /** The directory path of the folder containing input images*/
\r
84 /** Enable Cod Format for output*/
\r
85 char set_out_format;
\r
86 /** User specified rate stored in case of cinema option*/
\r
90 void encode_help_display() {
\r
91 fprintf(stdout,"HELP\n----\n\n");
\r
92 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
\r
95 fprintf(stdout,"List of parameters for the JPEG 2000 "
\r
98 #endif /* USE_JPWL */
\r
101 fprintf(stdout,"\n");
\r
102 fprintf(stdout,"REMARKS:\n");
\r
103 fprintf(stdout,"---------\n");
\r
104 fprintf(stdout,"\n");
\r
105 fprintf(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
\r
106 fprintf(stdout,"COD and QCD never appear in the tile_header.\n");
\r
107 fprintf(stdout,"\n");
\r
108 fprintf(stdout,"By default:\n");
\r
109 fprintf(stdout,"------------\n");
\r
110 fprintf(stdout,"\n");
\r
111 fprintf(stdout," * Lossless\n");
\r
112 fprintf(stdout," * 1 tile\n");
\r
113 fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
\r
114 fprintf(stdout," * Size of code-block : 64 x 64\n");
\r
115 fprintf(stdout," * Number of resolutions: 6\n");
\r
116 fprintf(stdout," * No SOP marker in the codestream\n");
\r
117 fprintf(stdout," * No EPH marker in the codestream\n");
\r
118 fprintf(stdout," * No sub-sampling in x or y direction\n");
\r
119 fprintf(stdout," * No mode switch activated\n");
\r
120 fprintf(stdout," * Progression order: LRCP\n");
\r
121 fprintf(stdout," * No index file\n");
\r
122 fprintf(stdout," * No ROI upshifted\n");
\r
123 fprintf(stdout," * No offset of the origin of the image\n");
\r
124 fprintf(stdout," * No offset of the origin of the tiles\n");
\r
125 fprintf(stdout," * Reversible DWT 5-3\n");
\r
128 fprintf(stdout," * No JPWL protection\n");
\r
129 #endif /* USE_JPWL */
\r
131 fprintf(stdout,"\n");
\r
132 fprintf(stdout,"Parameters:\n");
\r
133 fprintf(stdout,"------------\n");
\r
134 fprintf(stdout,"\n");
\r
135 fprintf(stdout,"Required Parameters (except with -h):\n");
\r
136 fprintf(stdout,"One of the two options -ImgDir or -i must be used\n");
\r
137 fprintf(stdout,"\n");
\r
138 fprintf(stdout,"-ImgDir : Image file Directory path (example ../Images) \n");
\r
139 fprintf(stdout," When using this option -OutFor must be used\n");
\r
140 fprintf(stdout,"\n");
\r
141 fprintf(stdout,"-OutFor \n");
\r
142 fprintf(stdout," REQUIRED only if -ImgDir is used\n");
\r
143 fprintf(stdout," Need to specify only format without filename <BMP> \n");
\r
144 fprintf(stdout," Currently accepts PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA formats\n");
\r
145 fprintf(stdout,"\n");
\r
146 fprintf(stdout,"-i : source file (-i source.pnm also *.pgm, *.ppm, *.bmp, *.tif, *.raw, *.tga) \n");
\r
147 fprintf(stdout," When using this option -o must be used\n");
\r
148 fprintf(stdout,"\n");
\r
149 fprintf(stdout,"-o : destination file (-o dest.j2k or .jp2) \n");
\r
150 fprintf(stdout,"\n");
\r
151 fprintf(stdout,"Optional Parameters:\n");
\r
152 fprintf(stdout,"\n");
\r
153 fprintf(stdout,"-h : display the help information \n ");
\r
154 fprintf(stdout,"\n");
\r
155 fprintf(stdout,"-cinema2K : Digital Cinema 2K profile compliant codestream for 2K resolution.(-cinema2k 24 or 48) \n");
\r
156 fprintf(stdout," Need to specify the frames per second for a 2K resolution. Only 24 or 48 fps is allowed\n");
\r
157 fprintf(stdout,"\n");
\r
158 fprintf(stdout,"-cinema4K : Digital Cinema 4K profile compliant codestream for 4K resolution \n");
\r
159 fprintf(stdout," Frames per second not required. Default value is 24fps\n");
\r
160 fprintf(stdout,"\n");
\r
161 fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n ");
\r
162 fprintf(stdout," - The rate specified for each quality level is the desired \n");
\r
163 fprintf(stdout," compression factor.\n");
\r
164 fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n");
\r
165 fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n");
\r
166 fprintf(stdout,"\n");
\r
167 fprintf(stdout," (options -r and -q cannot be used together)\n ");
\r
168 fprintf(stdout,"\n");
\r
170 fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n ");
\r
172 fprintf(stdout," (options -r and -q cannot be used together)\n ");
\r
174 fprintf(stdout,"\n");
\r
175 fprintf(stdout,"-n : number of resolutions (-n 3) \n");
\r
176 fprintf(stdout,"\n");
\r
177 fprintf(stdout,"-b : size of code block (-b 32,32) \n");
\r
178 fprintf(stdout,"\n");
\r
179 fprintf(stdout,"-c : size of precinct (-c 128,128) \n");
\r
180 fprintf(stdout,"\n");
\r
181 fprintf(stdout,"-t : size of tile (-t 512,512) \n");
\r
182 fprintf(stdout,"\n");
\r
183 fprintf(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
\r
184 fprintf(stdout,"\n");
\r
185 fprintf(stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n");
\r
186 fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n");
\r
187 fprintf(stdout,"\n");
\r
188 fprintf(stdout,"-POC : Progression order change (-POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL) \n");
\r
189 fprintf(stdout," Example: T1=0,0,1,5,3,CPRL \n");
\r
190 fprintf(stdout," : Ttilenumber=Resolution num start,Component num start,Layer num end,Resolution num end,Component num end,Progression order\n");
\r
191 fprintf(stdout,"\n");
\r
192 fprintf(stdout,"-SOP : write SOP marker before each packet \n");
\r
193 fprintf(stdout,"\n");
\r
194 fprintf(stdout,"-EPH : write EPH marker after each header packet \n");
\r
195 fprintf(stdout,"\n");
\r
196 fprintf(stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
\r
197 fprintf(stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
\r
198 fprintf(stdout," Indicate multiple modes by adding their values. \n");
\r
199 fprintf(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
\r
200 fprintf(stdout,"\n");
\r
201 fprintf(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n");
\r
202 fprintf(stdout,"\n");
\r
203 fprintf(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
\r
204 fprintf(stdout," for component c=%%d [%%d = 0,1,2]\n");
\r
205 fprintf(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI c=0,U=25) \n");
\r
206 fprintf(stdout,"\n");
\r
207 fprintf(stdout,"-d : offset of the origin of the image (-d 150,300) \n");
\r
208 fprintf(stdout,"\n");
\r
209 fprintf(stdout,"-T : offset of the origin of the tiles (-T 100,75) \n");
\r
210 fprintf(stdout,"\n");
\r
211 fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n");
\r
212 fprintf(stdout,"\n");
\r
213 fprintf(stdout,"-m : use array-based MCT, values are coma separated, line by line\n");
\r
214 fprintf(stdout," no specific separators between lines, no space allowed between values\n");
\r
215 fprintf(stdout,"\n");
\r
218 fprintf(stdout,"-W : adoption of JPWL (Part 11) capabilities (-W params)\n");
\r
219 fprintf(stdout," The parameters can be written and repeated in any order:\n");
\r
220 fprintf(stdout," [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
\r
221 fprintf(stdout," ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
\r
222 fprintf(stdout,"\n");
\r
223 fprintf(stdout," h selects the header error protection (EPB): 'type' can be\n");
\r
224 fprintf(stdout," [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
\r
225 fprintf(stdout," if 'tilepart' is absent, it is for main and tile headers\n");
\r
226 fprintf(stdout," if 'tilepart' is present, it applies from that tile\n");
\r
227 fprintf(stdout," onwards, up to the next h<> spec, or to the last tilepart\n");
\r
228 fprintf(stdout," in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS);
\r
229 fprintf(stdout,"\n");
\r
230 fprintf(stdout," p selects the packet error protection (EEP/UEP with EPBs)\n");
\r
231 fprintf(stdout," to be applied to raw data: 'type' can be\n");
\r
232 fprintf(stdout," [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
\r
233 fprintf(stdout," if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
\r
234 fprintf(stdout," if 'tilepart:pack' is present, it applies from that tile\n");
\r
235 fprintf(stdout," and that packet onwards, up to the next packet spec\n");
\r
236 fprintf(stdout," or to the last packet in the last tilepart in the stream\n");
\r
237 fprintf(stdout," (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS);
\r
238 fprintf(stdout,"\n");
\r
239 fprintf(stdout," s enables sensitivity data insertion (ESD): 'method' can be\n");
\r
240 fprintf(stdout," [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
\r
241 fprintf(stdout," 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
\r
242 fprintf(stdout," if 'tilepart' is absent, it is for main header only\n");
\r
243 fprintf(stdout," if 'tilepart' is present, it applies from that tile\n");
\r
244 fprintf(stdout," onwards, up to the next s<> spec, or to the last tilepart\n");
\r
245 fprintf(stdout," in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS);
\r
246 fprintf(stdout,"\n");
\r
247 fprintf(stdout," g determines the addressing mode: <range> can be\n");
\r
248 fprintf(stdout," [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
\r
249 fprintf(stdout,"\n");
\r
250 fprintf(stdout," a determines the size of data addressing: <addr> can be\n");
\r
251 fprintf(stdout," 2/4 bytes (small/large codestreams). If not set, auto-mode\n");
\r
252 fprintf(stdout,"\n");
\r
253 fprintf(stdout," z determines the size of sensitivity values: <size> can be\n");
\r
254 fprintf(stdout," 1/2 bytes, for the transformed pseudo-floating point value\n");
\r
255 fprintf(stdout,"\n");
\r
256 fprintf(stdout," ex.:\n");
\r
257 fprintf(stdout," h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
\r
258 fprintf(stdout," s0=6,s3=-1,a=0,g=1,z=1\n");
\r
259 fprintf(stdout," means\n");
\r
260 fprintf(stdout," predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
\r
261 fprintf(stdout," CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
\r
262 fprintf(stdout," UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
\r
263 fprintf(stdout," UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
\r
264 fprintf(stdout," UEP rs default for packets of tilepart 1,\n");
\r
265 fprintf(stdout," no UEP for packets 0 to 19 of tilepart 3,\n");
\r
266 fprintf(stdout," UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
\r
267 fprintf(stdout," relative sensitivity ESD for MH,\n");
\r
268 fprintf(stdout," TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
\r
269 fprintf(stdout," size of addresses and 1 byte for each sensitivity value\n");
\r
270 fprintf(stdout,"\n");
\r
271 fprintf(stdout," ex.:\n");
\r
272 fprintf(stdout," h,s,p\n");
\r
273 fprintf(stdout," means\n");
\r
274 fprintf(stdout," default protection to headers (MH and TPHs) as well as\n");
\r
275 fprintf(stdout," data packets, one ESD in MH\n");
\r
276 fprintf(stdout,"\n");
\r
277 fprintf(stdout," N.B.: use the following recommendations when specifying\n");
\r
278 fprintf(stdout," the JPWL parameters list\n");
\r
279 fprintf(stdout," - when you use UEP, always pair the 'p' option with 'h'\n");
\r
280 fprintf(stdout," \n");
\r
281 #endif /* USE_JPWL */
\r
283 fprintf(stdout,"IMPORTANT:\n");
\r
284 fprintf(stdout,"-----------\n");
\r
285 fprintf(stdout,"\n");
\r
286 fprintf(stdout,"The index file has the structure below:\n");
\r
287 fprintf(stdout,"---------------------------------------\n");
\r
288 fprintf(stdout,"\n");
\r
289 fprintf(stdout,"Image_height Image_width\n");
\r
290 fprintf(stdout,"progression order\n");
\r
291 fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
\r
292 fprintf(stdout,"Tiles_nb_X Tiles_nb_Y\n");
\r
293 fprintf(stdout,"Components_nb\n");
\r
294 fprintf(stdout,"Layers_nb\n");
\r
295 fprintf(stdout,"decomposition_levels\n");
\r
296 fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
\r
297 fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
\r
298 fprintf(stdout,"Main_header_start_position\n");
\r
299 fprintf(stdout,"Main_header_end_position\n");
\r
300 fprintf(stdout,"Codestream_size\n");
\r
301 fprintf(stdout,"\n");
\r
302 fprintf(stdout,"INFO ON TILES\n");
\r
303 fprintf(stdout,"tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
\r
304 fprintf(stdout,"Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
\r
305 fprintf(stdout,"Tile_1 '' '' '' '' '' '' ''\n");
\r
306 fprintf(stdout,"...\n");
\r
307 fprintf(stdout,"Tile_Nt '' '' '' '' '' '' ''\n");
\r
308 fprintf(stdout,"...\n");
\r
309 fprintf(stdout,"TILE 0 DETAILS\n");
\r
310 fprintf(stdout,"part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
\r
311 fprintf(stdout,"...\n");
\r
312 fprintf(stdout,"Progression_string\n");
\r
313 fprintf(stdout,"pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
\r
314 fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
\r
315 fprintf(stdout,"...\n");
\r
316 fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
\r
318 fprintf(stdout,"MaxDisto\n");
\r
320 fprintf(stdout,"TotalDisto\n\n");
\r
323 OPJ_PROG_ORDER give_progression(char progression[4]) {
\r
324 if(strncmp(progression, "LRCP", 4) == 0) {
\r
327 if(strncmp(progression, "RLCP", 4) == 0) {
\r
330 if(strncmp(progression, "RPCL", 4) == 0) {
\r
333 if(strncmp(progression, "PCRL", 4) == 0) {
\r
336 if(strncmp(progression, "CPRL", 4) == 0) {
\r
340 return PROG_UNKNOWN;
\r
343 int get_num_images(char *imgdirpath){
\r
345 struct dirent* content;
\r
346 int num_images = 0;
\r
348 /*Reading the input images from given input directory*/
\r
350 dir= opendir(imgdirpath);
\r
352 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
\r
357 while((content=readdir(dir))!=NULL){
\r
358 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
\r
365 int load_images(dircnt_t *dirptr, char *imgdirpath){
\r
367 struct dirent* content;
\r
370 /*Reading the input images from given input directory*/
\r
372 dir= opendir(imgdirpath);
\r
374 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
\r
377 fprintf(stderr,"Folder opened successfully\n");
\r
380 while((content=readdir(dir))!=NULL){
\r
381 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
\r
384 strcpy(dirptr->filename[i],content->d_name);
\r
390 int get_file_format(char *filename) {
\r
392 static const char *extension[] = {
\r
393 "pgx", "pnm", "pgm", "ppm", "bmp", "tif", "raw", "tga", "j2k", "jp2", "j2c"
\r
395 static const int format[] = {
\r
396 PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT
\r
398 char * ext = strrchr(filename, '.');
\r
402 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
\r
403 if(strnicmp(ext, extension[i], 3) == 0) {
\r
410 char * get_file_name(char *name){
\r
412 fname= (char*)malloc(OPJ_PATH_LEN*sizeof(char));
\r
413 fname= strtok(name,".");
\r
417 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparameters_t *parameters){
\r
418 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
\r
419 char *temp_p, temp1[OPJ_PATH_LEN]="";
\r
421 strcpy(image_filename,dirptr->filename[imageno]);
\r
422 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
\r
423 parameters->decod_format = get_file_format(image_filename);
\r
424 if (parameters->decod_format == -1)
\r
426 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
\r
427 strncpy(parameters->infile, infilename, sizeof(infilename));
\r
430 strcpy(temp_ofname,get_file_name(image_filename));
\r
431 while((temp_p = strtok(NULL,".")) != NULL){
\r
432 strcat(temp_ofname,temp1);
\r
433 sprintf(temp1,".%s",temp_p);
\r
435 if(img_fol->set_out_format==1){
\r
436 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
\r
437 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
\r
442 static int initialise_4K_poc(opj_poc_t *POC, int numres){
\r
444 POC[0].resno0 = 0;
\r
445 POC[0].compno0 = 0;
\r
447 POC[0].resno1 = numres-1;
\r
448 POC[0].compno1 = 3;
\r
449 POC[0].prg1 = CPRL;
\r
451 POC[1].resno0 = numres-1;
\r
452 POC[1].compno0 = 0;
\r
454 POC[1].resno1 = numres;
\r
455 POC[1].compno1 = 3;
\r
456 POC[1].prg1 = CPRL;
\r
460 void cinema_parameters(opj_cparameters_t *parameters){
\r
461 parameters->tile_size_on = false;
\r
462 parameters->cp_tdx=1;
\r
463 parameters->cp_tdy=1;
\r
466 parameters->tp_flag = 'C';
\r
467 parameters->tp_on = 1;
\r
469 /*Tile and Image shall be at (0,0)*/
\r
470 parameters->cp_tx0 = 0;
\r
471 parameters->cp_ty0 = 0;
\r
472 parameters->image_offset_x0 = 0;
\r
473 parameters->image_offset_y0 = 0;
\r
475 /*Codeblock size= 32*32*/
\r
476 parameters->cblockw_init = 32;
\r
477 parameters->cblockh_init = 32;
\r
478 parameters->csty |= 0x01;
\r
480 /*The progression order shall be CPRL*/
\r
481 parameters->prog_order = CPRL;
\r
484 parameters->roi_compno = -1;
\r
486 parameters->subsampling_dx = 1; parameters->subsampling_dy = 1;
\r
488 /* 9-7 transform */
\r
489 parameters->irreversible = 1;
\r
493 void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_fol_t *img_fol){
\r
496 opj_poc_t *POC = NULL;
\r
498 switch (parameters->cp_cinema){
\r
501 if(parameters->numresolution > 6){
\r
502 parameters->numresolution = 6;
\r
504 if (!((image->comps[0].w == 2048) | (image->comps[0].h == 1080))){
\r
505 fprintf(stdout,"Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
\r
506 "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
\r
507 image->comps[0].w,image->comps[0].h);
\r
508 parameters->cp_rsiz = STD_RSIZ;
\r
513 if(parameters->numresolution < 1){
\r
514 parameters->numresolution = 1;
\r
515 }else if(parameters->numresolution > 7){
\r
516 parameters->numresolution = 7;
\r
518 if (!((image->comps[0].w == 4096) | (image->comps[0].h == 2160))){
\r
519 fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
\r
520 "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
\r
521 image->comps[0].w,image->comps[0].h);
\r
522 parameters->cp_rsiz = STD_RSIZ;
\r
524 parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution);
\r
530 switch (parameters->cp_cinema){
\r
533 for(i=0 ; i<parameters->tcp_numlayers ; i++){
\r
535 if (img_fol->rates[i]== 0){
\r
536 parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
\r
537 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
\r
539 temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
\r
540 (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
\r
541 if (temp_rate > CINEMA_24_CS ){
\r
542 parameters->tcp_rates[i]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
\r
543 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
\r
545 parameters->tcp_rates[i]= img_fol->rates[i];
\r
549 parameters->max_comp_size = COMP_24_CS;
\r
553 for(i=0 ; i<parameters->tcp_numlayers ; i++){
\r
555 if (img_fol->rates[i]== 0){
\r
556 parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
\r
557 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
\r
559 temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
\r
560 (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
\r
561 if (temp_rate > CINEMA_48_CS ){
\r
562 parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
\r
563 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
\r
565 parameters->tcp_rates[i]= img_fol->rates[i];
\r
569 parameters->max_comp_size = COMP_48_CS;
\r
574 parameters->cp_disto_alloc = 1;
\r
577 /* ------------------------------------------------------------------------------------ */
\r
579 int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
\r
580 img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename) {
\r
582 option_t long_option[]={
\r
583 {"cinema2K",REQ_ARG, NULL ,'w'},
\r
584 {"cinema4K",NO_ARG, NULL ,'y'},
\r
585 {"ImgDir",REQ_ARG, NULL ,'z'},
\r
586 {"TP",REQ_ARG, NULL ,'v'},
\r
587 {"SOP",NO_ARG, NULL ,'S'},
\r
588 {"EPH",NO_ARG, NULL ,'E'},
\r
589 {"OutFor",REQ_ARG, NULL ,'O'},
\r
590 {"POC",REQ_ARG, NULL ,'P'},
\r
591 {"ROI",REQ_ARG, NULL ,'R'},
\r
594 /* parse the command line */
\r
595 const char optlist[] = "i:o:hr:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:m:"
\r
598 #endif /* USE_JPWL */
\r
601 totlen=sizeof(long_option);
\r
602 img_fol->set_out_format=0;
\r
603 raw_cp->rawWidth = 0;
\r
606 int c = getopt_long(argc, argv, optlist,long_option,totlen);
\r
610 case 'i': /* input file */
\r
612 char *infile = optarg;
\r
613 parameters->decod_format = get_file_format(infile);
\r
614 switch(parameters->decod_format) {
\r
624 "!! Unrecognized format for infile : %s "
\r
625 "[accept only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga] !!\n\n",
\r
629 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
\r
633 /* ----------------------------------------------------- */
\r
635 case 'o': /* output file */
\r
637 char *outfile = optarg;
\r
638 parameters->cod_format = get_file_format(outfile);
\r
639 switch(parameters->cod_format) {
\r
644 fprintf(stderr, "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
\r
647 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
\r
651 /* ----------------------------------------------------- */
\r
652 case 'O': /* output format */
\r
654 char outformat[50];
\r
656 sprintf(outformat,".%s",of);
\r
657 img_fol->set_out_format = 1;
\r
658 parameters->cod_format = get_file_format(outformat);
\r
659 switch(parameters->cod_format) {
\r
662 img_fol->out_format = optarg;
\r
665 fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n");
\r
672 /* ----------------------------------------------------- */
\r
675 case 'r': /* rates rates/distorsion */
\r
678 while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) == 1) {
\r
679 parameters->tcp_numlayers++;
\r
680 while (*s && *s != ',') {
\r
687 parameters->cp_disto_alloc = 1;
\r
691 /* ----------------------------------------------------- */
\r
694 case 'F': /* Raw image format parameters */
\r
698 if (sscanf(s, "%d,%d,%d,%d,%c", &raw_cp->rawWidth, &raw_cp->rawHeight, &raw_cp->rawComp, &raw_cp->rawBitDepth, &signo) == 5) {
\r
699 if (signo == 's') {
\r
700 raw_cp->rawSigned = true;
\r
701 fprintf(stdout,"\nRaw file parameters: %d,%d,%d,%d Signed\n", raw_cp->rawWidth, raw_cp->rawHeight, raw_cp->rawComp, raw_cp->rawBitDepth);
\r
703 else if (signo == 'u') {
\r
704 raw_cp->rawSigned = false;
\r
705 fprintf(stdout,"\nRaw file parameters: %d,%d,%d,%d Unsigned\n", raw_cp->rawWidth, raw_cp->rawHeight, raw_cp->rawComp, raw_cp->rawBitDepth);
\r
708 fprintf(stderr,"\nError: invalid raw image parameters: Unknown sign of raw file\n");
\r
709 fprintf(stderr,"Please use the Format option -F:\n");
\r
710 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
\r
711 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
\r
712 fprintf(stderr,"Aborting\n");
\r
716 fprintf(stderr,"\nError: invalid raw image parameters\n");
\r
717 fprintf(stderr,"Please use the Format option -F:\n");
\r
718 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
\r
719 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
\r
720 fprintf(stderr,"Aborting\n");
\r
726 /* ----------------------------------------------------- */
\r
728 case 'q': /* add fixed_quality */
\r
731 while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers]) == 1) {
\r
732 parameters->tcp_numlayers++;
\r
733 while (*s && *s != ',') {
\r
740 parameters->cp_fixed_quality = 1;
\r
745 /* ----------------------------------------------------- */
\r
747 case 'f': /* mod fixed_quality (before : -q) */
\r
749 int *row = NULL, *col = NULL;
\r
750 int numlayers = 0, numresolution = 0, matrix_width = 0;
\r
753 sscanf(s, "%d", &numlayers);
\r
758 parameters->tcp_numlayers = numlayers;
\r
759 numresolution = parameters->numresolution;
\r
760 matrix_width = numresolution * 3;
\r
761 parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
\r
764 for (i = 0; i < numlayers; i++) {
\r
765 row = ¶meters->cp_matrice[i * matrix_width];
\r
767 parameters->tcp_rates[i] = 1;
\r
768 sscanf(s, "%d,", &col[0]);
\r
774 for (j = 1; j < numresolution; j++) {
\r
776 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
\r
785 if (i < numlayers - 1)
\r
788 parameters->cp_fixed_alloc = 1;
\r
792 /* ----------------------------------------------------- */
\r
794 case 't': /* tiles */
\r
796 sscanf(optarg, "%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy);
\r
797 parameters->tile_size_on = true;
\r
801 /* ----------------------------------------------------- */
\r
803 case 'n': /* resolution */
\r
805 sscanf(optarg, "%d", ¶meters->numresolution);
\r
809 /* ----------------------------------------------------- */
\r
810 case 'c': /* precinct dimension */
\r
818 sscanf(s, "[%d,%d]%c", ¶meters->prcw_init[res_spec],
\r
819 ¶meters->prch_init[res_spec], &sep);
\r
820 parameters->csty |= 0x01;
\r
822 s = strpbrk(s, "]") + 2;
\r
824 while (sep == ',');
\r
825 parameters->res_spec = res_spec;
\r
829 /* ----------------------------------------------------- */
\r
831 case 'b': /* code-block dimension */
\r
833 int cblockw_init = 0, cblockh_init = 0;
\r
834 sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
\r
835 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
\r
836 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
\r
838 "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
\r
839 " * width*height<=4096\n * 4<=width,height<= 1024\n\n");
\r
842 parameters->cblockw_init = cblockw_init;
\r
843 parameters->cblockh_init = cblockh_init;
\r
847 /* ----------------------------------------------------- */
\r
849 case 'x': /* creation of index file */
\r
851 char *index = optarg;
\r
852 strncpy(indexfilename, index, OPJ_PATH_LEN);
\r
856 /* ----------------------------------------------------- */
\r
858 case 'p': /* progression order */
\r
860 char progression[4];
\r
862 strncpy(progression, optarg, 4);
\r
863 parameters->prog_order = give_progression(progression);
\r
864 if (parameters->prog_order == -1) {
\r
865 fprintf(stderr, "Unrecognized progression order "
\r
866 "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
\r
872 /* ----------------------------------------------------- */
\r
874 case 's': /* subsampling factor */
\r
876 if (sscanf(optarg, "%d,%d", ¶meters->subsampling_dx,
\r
877 ¶meters->subsampling_dy) != 2) {
\r
878 fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
\r
884 /* ----------------------------------------------------- */
\r
886 case 'd': /* coordonnate of the reference grid */
\r
888 if (sscanf(optarg, "%d,%d", ¶meters->image_offset_x0,
\r
889 ¶meters->image_offset_y0) != 2) {
\r
890 fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
\r
891 "error !! [-d x0,y0]\n");
\r
897 /* ----------------------------------------------------- */
\r
899 case 'h': /* display an help description */
\r
900 encode_help_display();
\r
903 /* ----------------------------------------------------- */
\r
905 case 'P': /* POC */
\r
907 int numpocs = 0; /* number of progression order change (POC) default 0 */
\r
908 opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
\r
911 POC = parameters->POC;
\r
913 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile,
\r
914 &POC[numpocs].resno0, &POC[numpocs].compno0,
\r
915 &POC[numpocs].layno1, &POC[numpocs].resno1,
\r
916 &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
\r
917 POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
\r
919 while (*s && *s != '/') {
\r
927 parameters->numpocs = numpocs;
\r
931 /* ------------------------------------------------------ */
\r
933 case 'S': /* SOP marker */
\r
935 parameters->csty |= 0x02;
\r
939 /* ------------------------------------------------------ */
\r
941 case 'E': /* EPH marker */
\r
943 parameters->csty |= 0x04;
\r
947 /* ------------------------------------------------------ */
\r
949 case 'M': /* Mode switch pas tous au point !! */
\r
952 if (sscanf(optarg, "%d", &value) == 1) {
\r
953 for (i = 0; i <= 5; i++) {
\r
954 int cache = value & (1 << i);
\r
956 parameters->mode |= (1 << i);
\r
962 /* ------------------------------------------------------ */
\r
964 case 'R': /* ROI */
\r
966 if (sscanf(optarg, "c=%d,U=%d", ¶meters->roi_compno,
\r
967 ¶meters->roi_shift) != 2) {
\r
968 fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n");
\r
974 /* ------------------------------------------------------ */
\r
976 case 'T': /* Tile offset */
\r
978 if (sscanf(optarg, "%d,%d", ¶meters->cp_tx0, ¶meters->cp_ty0) != 2) {
\r
979 fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
\r
985 /* ------------------------------------------------------ */
\r
987 case 'C': /* add a comment */
\r
989 parameters->cp_comment = (char*)malloc(strlen(optarg) + 1);
\r
990 if(parameters->cp_comment) {
\r
991 strcpy(parameters->cp_comment, optarg);
\r
997 /* ------------------------------------------------------ */
\r
999 case 'I': /* reversible or not */
\r
1001 parameters->irreversible = 1;
\r
1005 /* ------------------------------------------------------ */
\r
1007 case 'v': /* Tile part generation*/
\r
1009 parameters->tp_flag = optarg[0];
\r
1010 parameters->tp_on = 1;
\r
1014 /* ------------------------------------------------------ */
\r
1016 case 'z': /* Image Directory path */
\r
1018 img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
\r
1019 strcpy(img_fol->imgdirpath,optarg);
\r
1020 img_fol->set_imgdir=1;
\r
1024 /* ------------------------------------------------------ */
\r
1026 case 'w': /* Digital Cinema 2K profile compliance*/
\r
1029 sscanf(optarg,"%d",&fps);
\r
1031 parameters->cp_cinema = CINEMA2K_24;
\r
1032 }else if(fps == 48 ){
\r
1033 parameters->cp_cinema = CINEMA2K_48;
\r
1035 fprintf(stderr,"Incorrect value!! must be 24 or 48\n");
\r
1038 fprintf(stdout,"CINEMA 2K compliant codestream\n");
\r
1039 parameters->cp_rsiz = CINEMA2K;
\r
1044 /* ------------------------------------------------------ */
\r
1046 case 'y': /* Digital Cinema 4K profile compliance*/
\r
1048 parameters->cp_cinema = CINEMA4K_24;
\r
1049 fprintf(stdout,"CINEMA 4K compliant codestream\n");
\r
1050 parameters->cp_rsiz = CINEMA4K;
\r
1054 case 'm': /* output file */
\r
1056 char *lFilename = optarg;
\r
1058 char *lCurrentPtr ;
\r
1062 float * lCurrentDoublePtr;
\r
1068 FILE * lFile = fopen(lFilename,"r");
\r
1074 fseek(lFile,0,SEEK_END);
\r
1075 lStrLen = ftell(lFile);
\r
1076 fseek(lFile,0,SEEK_SET);
\r
1077 lMatrix = (char *) malloc(lStrLen + 1);
\r
1078 fread(lMatrix,lStrLen,1,lFile);
\r
1080 lMatrix[lStrLen] = 0;
\r
1081 lCurrentPtr = lMatrix;
\r
1083 // replace ',' by 0
\r
1085 (*lCurrentPtr != 0 )
\r
1088 (*lCurrentPtr == ' ')
\r
1096 lCurrentPtr = lMatrix;
\r
1098 lNbComp = (int) (sqrt(4*lNbComp + 1)/2. - 0.5);
\r
1099 lMctComp = lNbComp * lNbComp;
\r
1100 lTotalComp = lMctComp + lNbComp;
\r
1101 lSpace = (float *) malloc(lTotalComp * sizeof(float));
\r
1102 lCurrentDoublePtr = lSpace;
\r
1104 (i=0;i<lMctComp;++i)
\r
1106 lStrLen = strlen(lCurrentPtr) + 1;
\r
1107 *lCurrentDoublePtr++ = (float) atof(lCurrentPtr);
\r
1108 lCurrentPtr += lStrLen;
\r
1110 l_int_ptr = (int*) lCurrentDoublePtr;
\r
1112 (i=0;i<lNbComp;++i)
\r
1114 lStrLen = strlen(lCurrentPtr) + 1;
\r
1115 *l_int_ptr++ = atoi(lCurrentPtr);
\r
1116 lCurrentPtr += lStrLen;
\r
1118 opj_set_MCT(parameters,lSpace,(int *)(lSpace + lMctComp), lNbComp);
\r
1124 /* ------------------------------------------------------ */
\r
1128 /* ------------------------------------------------------ */
\r
1130 case 'W': /* JPWL capabilities switched on */
\r
1132 char *token = NULL;
\r
1133 int hprot, pprot, sens, addr, size, range;
\r
1135 /* we need to enable indexing */
\r
1136 if (!indexfilename) {
\r
1137 strncpy(indexfilename, JPWL_PRIVATEINDEX_NAME, OPJ_PATH_LEN);
\r
1140 /* search for different protection methods */
\r
1142 /* break the option in comma points and parse the result */
\r
1143 token = strtok(optarg, ",");
\r
1144 while(token != NULL) {
\r
1146 /* search header error protection method */
\r
1147 if (*token == 'h') {
\r
1149 static int tile = 0, tilespec = 0, lasttileno = 0;
\r
1151 hprot = 1; /* predefined method */
\r
1153 if(sscanf(token, "h=%d", &hprot) == 1) {
\r
1154 /* Main header, specified */
\r
1155 if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
\r
1156 ((hprot >= 37) && (hprot <= 128)))) {
\r
1157 fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n", hprot);
\r
1160 parameters->jpwl_hprot_MH = hprot;
\r
1162 } else if(sscanf(token, "h%d=%d", &tile, &hprot) == 2) {
\r
1163 /* Tile part header, specified */
\r
1164 if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
\r
1165 ((hprot >= 37) && (hprot <= 128)))) {
\r
1166 fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n", hprot);
\r
1170 fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile);
\r
1173 if (tilespec < JPWL_MAX_NO_TILESPECS) {
\r
1174 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
\r
1175 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
\r
1178 } else if(sscanf(token, "h%d", &tile) == 1) {
\r
1179 /* Tile part header, unspecified */
\r
1181 fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile);
\r
1184 if (tilespec < JPWL_MAX_NO_TILESPECS) {
\r
1185 parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
\r
1186 parameters->jpwl_hprot_TPH[tilespec++] = hprot;
\r
1190 } else if (!strcmp(token, "h")) {
\r
1191 /* Main header, unspecified */
\r
1192 parameters->jpwl_hprot_MH = hprot;
\r
1195 fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
\r
1201 /* search packet error protection method */
\r
1202 if (*token == 'p') {
\r
1204 static int pack = 0, tile = 0, packspec = 0, lastpackno = 0;
\r
1206 pprot = 1; /* predefined method */
\r
1208 if (sscanf(token, "p=%d", &pprot) == 1) {
\r
1209 /* Method for all tiles and all packets */
\r
1210 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
\r
1211 ((pprot >= 37) && (pprot <= 128)))) {
\r
1212 fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n", pprot);
\r
1215 parameters->jpwl_pprot_tileno[0] = 0;
\r
1216 parameters->jpwl_pprot_packno[0] = 0;
\r
1217 parameters->jpwl_pprot[0] = pprot;
\r
1219 } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) {
\r
1220 /* method specified from that tile on */
\r
1221 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
\r
1222 ((pprot >= 37) && (pprot <= 128)))) {
\r
1223 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
\r
1227 fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
\r
1230 if (packspec < JPWL_MAX_NO_PACKSPECS) {
\r
1231 parameters->jpwl_pprot_tileno[packspec] = tile;
\r
1232 parameters->jpwl_pprot_packno[packspec] = 0;
\r
1233 parameters->jpwl_pprot[packspec++] = pprot;
\r
1236 } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) {
\r
1237 /* method fully specified from that tile and that packet on */
\r
1238 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
\r
1239 ((pprot >= 37) && (pprot <= 128)))) {
\r
1240 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
\r
1244 fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
\r
1248 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack);
\r
1251 if (packspec < JPWL_MAX_NO_PACKSPECS) {
\r
1252 parameters->jpwl_pprot_tileno[packspec] = tile;
\r
1253 parameters->jpwl_pprot_packno[packspec] = pack;
\r
1254 parameters->jpwl_pprot[packspec++] = pprot;
\r
1257 } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) {
\r
1258 /* default method from that tile and that packet on */
\r
1259 if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
\r
1260 ((pprot >= 37) && (pprot <= 128)))) {
\r
1261 fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
\r
1265 fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
\r
1269 fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack);
\r
1272 if (packspec < JPWL_MAX_NO_PACKSPECS) {
\r
1273 parameters->jpwl_pprot_tileno[packspec] = tile;
\r
1274 parameters->jpwl_pprot_packno[packspec] = pack;
\r
1275 parameters->jpwl_pprot[packspec++] = pprot;
\r
1278 } else if (sscanf(token, "p%d", &tile) == 1) {
\r
1279 /* default from a tile on */
\r
1281 fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
\r
1284 if (packspec < JPWL_MAX_NO_PACKSPECS) {
\r
1285 parameters->jpwl_pprot_tileno[packspec] = tile;
\r
1286 parameters->jpwl_pprot_packno[packspec] = 0;
\r
1287 parameters->jpwl_pprot[packspec++] = pprot;
\r
1291 } else if (!strcmp(token, "p")) {
\r
1293 parameters->jpwl_pprot_tileno[0] = 0;
\r
1294 parameters->jpwl_pprot_packno[0] = 0;
\r
1295 parameters->jpwl_pprot[0] = pprot;
\r
1298 fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
\r
1304 /* search sensitivity method */
\r
1305 if (*token == 's') {
\r
1307 static int tile = 0, tilespec = 0, lasttileno = 0;
\r
1309 sens = 0; /* predefined: relative error */
\r
1311 if(sscanf(token, "s=%d", &sens) == 1) {
\r
1312 /* Main header, specified */
\r
1313 if ((sens < -1) || (sens > 7)) {
\r
1314 fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n", sens);
\r
1317 parameters->jpwl_sens_MH = sens;
\r
1319 } else if(sscanf(token, "s%d=%d", &tile, &sens) == 2) {
\r
1320 /* Tile part header, specified */
\r
1321 if ((sens < -1) || (sens > 7)) {
\r
1322 fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n", sens);
\r
1326 fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
\r
1329 if (tilespec < JPWL_MAX_NO_TILESPECS) {
\r
1330 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
\r
1331 parameters->jpwl_sens_TPH[tilespec++] = sens;
\r
1334 } else if(sscanf(token, "s%d", &tile) == 1) {
\r
1335 /* Tile part header, unspecified */
\r
1337 fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
\r
1340 if (tilespec < JPWL_MAX_NO_TILESPECS) {
\r
1341 parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
\r
1342 parameters->jpwl_sens_TPH[tilespec++] = hprot;
\r
1345 } else if (!strcmp(token, "s")) {
\r
1346 /* Main header, unspecified */
\r
1347 parameters->jpwl_sens_MH = sens;
\r
1350 fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token);
\r
1354 parameters->jpwl_sens_size = 2; /* 2 bytes for default size */
\r
1357 /* search addressing size */
\r
1358 if (*token == 'a') {
\r
1360 static int tile = 0, tilespec = 0, lasttileno = 0;
\r
1362 addr = 0; /* predefined: auto */
\r
1364 if(sscanf(token, "a=%d", &addr) == 1) {
\r
1366 if ((addr != 0) && (addr != 2) && (addr != 4)) {
\r
1367 fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr);
\r
1370 parameters->jpwl_sens_addr = addr;
\r
1372 } else if (!strcmp(token, "a")) {
\r
1374 parameters->jpwl_sens_addr = addr; /* auto for default size */
\r
1377 fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token);
\r
1383 /* search sensitivity size */
\r
1384 if (*token == 'z') {
\r
1386 static int tile = 0, tilespec = 0, lasttileno = 0;
\r
1388 size = 1; /* predefined: 1 byte */
\r
1390 if(sscanf(token, "z=%d", &size) == 1) {
\r
1392 if ((size != 0) && (size != 1) && (size != 2)) {
\r
1393 fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size);
\r
1396 parameters->jpwl_sens_size = size;
\r
1398 } else if (!strcmp(token, "a")) {
\r
1400 parameters->jpwl_sens_size = size; /* 1 for default size */
\r
1403 fprintf(stderr, "ERROR -> invalid size selection = %s\n", token);
\r
1409 /* search range method */
\r
1410 if (*token == 'g') {
\r
1412 static int tile = 0, tilespec = 0, lasttileno = 0;
\r
1414 range = 0; /* predefined: 0 (packet) */
\r
1416 if(sscanf(token, "g=%d", &range) == 1) {
\r
1418 if ((range < 0) || (range > 3)) {
\r
1419 fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range);
\r
1422 parameters->jpwl_sens_range = range;
\r
1424 } else if (!strcmp(token, "g")) {
\r
1426 parameters->jpwl_sens_range = range;
\r
1429 fprintf(stderr, "ERROR -> invalid range selection = %s\n", token);
\r
1435 /* next token or bust */
\r
1436 token = strtok(NULL, ",");
\r
1441 fprintf(stdout, "Info: JPWL capabilities enabled\n");
\r
1442 parameters->jpwl_epc_on = true;
\r
1446 #endif /* USE_JPWL */
\r
1449 /* ------------------------------------------------------ */
\r
1452 fprintf(stderr, "ERROR -> Command line not valid\n");
\r
1457 /* check for possible errors */
\r
1458 if (parameters->cp_cinema){
\r
1459 if(parameters->tcp_numlayers > 1){
\r
1460 parameters->cp_rsiz = STD_RSIZ;
\r
1461 fprintf(stdout,"Warning: DC profiles do not allow more than one quality layer. The codestream created will not be compliant with the DC profile\n");
\r
1464 if(img_fol->set_imgdir == 1){
\r
1465 if(!(parameters->infile[0] == 0)){
\r
1466 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
\r
1469 if(img_fol->set_out_format == 0){
\r
1470 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
\r
1471 fprintf(stderr, "Only one format allowed! Valid formats are j2k and jp2!!\n");
\r
1474 if(!((parameters->outfile[0] == 0))){
\r
1475 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
\r
1476 fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n");
\r
1480 if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
\r
1481 fprintf(stderr, "Error: One of the options; -i or -ImgDir must be specified\n");
\r
1482 fprintf(stderr, "Error: When using -i; -o must be used\n");
\r
1483 fprintf(stderr, "usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
\r
1488 if (parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0) {
\r
1489 fprintf(stderr,"\nError: invalid raw image parameters\n");
\r
1490 fprintf(stderr,"Please use the Format option -F:\n");
\r
1491 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
\r
1492 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
\r
1493 fprintf(stderr,"Aborting\n");
\r
1497 if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality)
\r
1498 && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^ parameters->cp_fixed_quality))) {
\r
1499 fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n");
\r
1501 } /* mod fixed_quality */
\r
1503 /* if no rate entered, lossless by default */
\r
1504 if (parameters->tcp_numlayers == 0) {
\r
1505 parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */
\r
1506 parameters->tcp_numlayers++;
\r
1507 parameters->cp_disto_alloc = 1;
\r
1510 if((parameters->cp_tx0 > parameters->image_offset_x0) || (parameters->cp_ty0 > parameters->image_offset_y0)) {
\r
1512 "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
\r
1513 parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0, parameters->image_offset_y0);
\r
1517 for (i = 0; i < parameters->numpocs; i++) {
\r
1518 if (parameters->POC[i].prg == -1) {
\r
1520 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
\r
1528 /* -------------------------------------------------------------------------- */
\r
1531 sample error callback expecting a FILE* client object
\r
1533 void error_callback(const char *msg, void *client_data) {
\r
1534 FILE *stream = (FILE*)client_data;
\r
1535 fprintf(stream, "[ERROR] %s", msg);
\r
1538 sample warning callback expecting a FILE* client object
\r
1540 void warning_callback(const char *msg, void *client_data) {
\r
1541 FILE *stream = (FILE*)client_data;
\r
1542 fprintf(stream, "[WARNING] %s", msg);
\r
1545 sample debug callback expecting a FILE* client object
\r
1547 void info_callback(const char *msg, void *client_data) {
\r
1548 FILE *stream = (FILE*)client_data;
\r
1549 fprintf(stream, "[INFO] %s", msg);
\r
1552 /* -------------------------------------------------------------------------- */
\r
1554 int main(int argc, char **argv) {
\r
1556 opj_cparameters_t parameters; /* compression parameters */
\r
1557 img_fol_t img_fol;
\r
1558 opj_image_t *image = NULL;
\r
1562 raw_cparameters_t raw_cp;
\r
1563 opj_codestream_info_t cstr_info; /* Codestream information structure */
\r
1564 char indexfilename[OPJ_PATH_LEN]; /* index file name */
\r
1565 opj_stream_t *cio = 00;
\r
1566 opj_codec_t* cinfo = 00;
\r
1570 configure the event callbacks (not required)
\r
1571 setting of each callback is optionnal
\r
1573 /* set encoding parameters to default values */
\r
1574 opj_set_default_encoder_parameters(¶meters);
\r
1576 /* Initialize indexfilename and img_fol */
\r
1577 *indexfilename = 0;
\r
1578 memset(&img_fol,0,sizeof(img_fol_t));
\r
1580 /* parse input and get user encoding parameters */
\r
1581 if(parse_cmdline_encoder(argc, argv, ¶meters,&img_fol, &raw_cp, indexfilename) == 1) {
\r
1585 if (parameters.cp_cinema){
\r
1586 img_fol.rates = (float*)malloc(parameters.tcp_numlayers * sizeof(float));
\r
1587 for(i=0; i< parameters.tcp_numlayers; i++){
\r
1588 img_fol.rates[i] = parameters.tcp_rates[i];
\r
1590 cinema_parameters(¶meters);
\r
1593 /* Create comment for codestream */
\r
1594 if(parameters.cp_comment == NULL) {
\r
1595 const char comment[] = "Created by OpenJPEG version ";
\r
1596 const size_t clen = strlen(comment);
\r
1597 const char *version = opj_version();
\r
1600 parameters.cp_comment = (char*)malloc(clen+strlen(version)+11);
\r
1601 sprintf(parameters.cp_comment,"%s%s with JPWL", comment, version);
\r
1603 parameters.cp_comment = (char*)malloc(clen+strlen(version)+1);
\r
1604 sprintf(parameters.cp_comment,"%s%s", comment, version);
\r
1609 /* Read directory if necessary */
\r
1610 if(img_fol.set_imgdir==1){
\r
1611 num_images=get_num_images(img_fol.imgdirpath);
\r
1612 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
\r
1614 dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); // Stores at max 10 image file names
\r
1615 dirptr->filename = (char**) malloc(num_images*sizeof(char*));
\r
1616 if(!dirptr->filename_buf){
\r
1619 for(i=0;i<num_images;i++){
\r
1620 dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
\r
1623 if(load_images(dirptr,img_fol.imgdirpath)==1){
\r
1626 if (num_images==0){
\r
1627 fprintf(stdout,"Folder is empty\n");
\r
1633 /*Encoding image one by one*/
\r
1634 for(imageno=0;imageno<num_images;imageno++) {
\r
1636 fprintf(stderr,"\n");
\r
1638 if(img_fol.set_imgdir==1){
\r
1639 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
\r
1640 fprintf(stderr,"skipping file...\n");
\r
1644 switch(parameters.decod_format) {
\r
1658 fprintf(stderr,"skipping file...\n");
\r
1662 /* decode the source image */
\r
1663 /* ----------------------- */
\r
1665 switch (parameters.decod_format) {
\r
1667 image = pgxtoimage(parameters.infile, ¶meters);
\r
1669 fprintf(stderr, "Unable to load pgx file\n");
\r
1675 image = pnmtoimage(parameters.infile, ¶meters);
\r
1677 fprintf(stderr, "Unable to load pnm file\n");
\r
1683 image = bmptoimage(parameters.infile, ¶meters);
\r
1685 fprintf(stderr, "Unable to load bmp file\n");
\r
1691 image = tiftoimage(parameters.infile, ¶meters);
\r
1693 fprintf(stderr, "Unable to load tiff file\n");
\r
1699 image = rawtoimage(parameters.infile, ¶meters, &raw_cp);
\r
1701 fprintf(stderr, "Unable to load raw file\n");
\r
1707 image = tgatoimage(parameters.infile, ¶meters);
\r
1709 fprintf(stderr, "Unable to load tga file\n");
\r
1714 /* Decide if MCT should be used */
\r
1715 parameters.tcp_mct = image->numcomps == 3 ? 1 : 0;
\r
1717 if(parameters.cp_cinema){
\r
1718 cinema_setup_encoder(¶meters,image,&img_fol);
\r
1721 /* encode the destination image */
\r
1722 /* ---------------------------- */
\r
1725 cinfo = parameters.cod_format == J2K_CFMT ? opj_create_compress(CODEC_J2K) : opj_create_compress(CODEC_JP2);
\r
1726 opj_setup_encoder(cinfo, ¶meters, image);
\r
1727 f = fopen(parameters.outfile, "wb");
\r
1731 fprintf(stderr, "failed to encode image\n");
\r
1734 /* open a byte stream for writing */
\r
1735 /* allocate memory for all tiles */
\r
1736 cio = opj_stream_create_default_file_stream(f,false);
\r
1742 /* encode the image */
\r
1743 /*if (*indexfilename) // If need to extract codestream information
\r
1744 bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info);
\r
1746 bSuccess = opj_start_compress(cinfo,image,cio);
\r
1747 bSuccess = bSuccess && opj_encode(cinfo, cio);
\r
1748 bSuccess = bSuccess && opj_end_compress(cinfo, cio);
\r
1753 opj_stream_destroy(cio);
\r
1755 fprintf(stderr, "failed to encode image\n");
\r
1759 fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
\r
1760 /* close and free the byte stream */
\r
1761 opj_stream_destroy(cio);
\r
1764 /* Write the index to disk */
\r
1765 if (*indexfilename) {
\r
1766 bSuccess = write_index_file(&cstr_info, indexfilename);
\r
1768 fprintf(stderr, "Failed to output index file\n");
\r
1772 /* free remaining compression structures */
\r
1773 opj_destroy_codec(cinfo);
\r
1774 if (*indexfilename)
\r
1775 opj_destroy_cstr_info(&cstr_info);
\r
1777 /* free image data */
\r
1778 opj_image_destroy(image);
\r
1781 /* free user parameters structure */
\r
1782 if(parameters.cp_comment) free(parameters.cp_comment);
\r
1783 if(parameters.cp_matrice) free(parameters.cp_matrice);
\r