fixed various minor warnings occuring under gcc, added corrections proposed in the...
[openjpeg.git] / codec / image_to_j2k.c
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5  * Copyright (c) 2005, Herv� Drolon, FreeImage Team
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "openjpeg.h"
35 #include "compat/getopt.h"
36 #include "convert.h"
37
38 #ifndef WIN32
39 #define stricmp strcasecmp
40 #define strnicmp strncasecmp
41 #endif
42
43 /* ----------------------------------------------------------------------- */
44
45 #define J2K_CFMT 0
46 #define JP2_CFMT 1
47 #define JPT_CFMT 2
48 #define MJ2_CFMT 3
49 #define PXM_DFMT 0
50 #define PGX_DFMT 1
51 #define BMP_DFMT 2
52 #define YUV_DFMT 3
53
54 /* ----------------------------------------------------------------------- */
55
56 void encode_help_display() {
57         fprintf(stdout,"HELP\n----\n\n");
58         fprintf(stdout,"- the -h option displays this help information on screen\n\n");
59
60
61         fprintf(stdout,"List of parameters for the JPEG 2000 encoder:\n");
62         fprintf(stdout,"\n");
63         fprintf(stdout,"REMARKS:\n");
64         fprintf(stdout,"---------\n");
65         fprintf(stdout,"\n");
66         fprintf(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
67         fprintf(stdout,"COD and QCD never appear in the tile_header.\n");
68         fprintf(stdout,"\n");
69         fprintf(stdout,"- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
70         fprintf(stdout,"color image.  You need enough disk space memory (twice the original) to encode \n");
71         fprintf(stdout,"the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
72         fprintf(stdout,"\n");
73         fprintf(stdout,"By default:\n");
74         fprintf(stdout,"------------\n");
75         fprintf(stdout,"\n");
76         fprintf(stdout," * Lossless\n");
77         fprintf(stdout," * 1 tile\n");
78         fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
79         fprintf(stdout," * Size of code-block : 64 x 64\n");
80         fprintf(stdout," * Number of resolutions: 6\n");
81         fprintf(stdout," * No SOP marker in the codestream\n");
82         fprintf(stdout," * No EPH marker in the codestream\n");
83         fprintf(stdout," * No sub-sampling in x or y direction\n");
84         fprintf(stdout," * No mode switch activated\n");
85         fprintf(stdout," * Progression order: LRCP\n");
86         fprintf(stdout," * No index file\n");
87         fprintf(stdout," * No ROI upshifted\n");
88         fprintf(stdout," * No offset of the origin of the image\n");
89         fprintf(stdout," * No offset of the origin of the tiles\n");
90         fprintf(stdout," * Reversible DWT 5-3\n");
91         fprintf(stdout,"\n");
92         fprintf(stdout,"Parameters:\n");
93         fprintf(stdout,"------------\n");
94         fprintf(stdout,"\n");
95         fprintf(stdout,"Required Parameters (except with -h):\n");
96         fprintf(stdout,"\n");
97         fprintf(stdout,"-i           : source file  (-i source.pnm also *.pgm, *.ppm) \n");
98         fprintf(stdout,"\n");
99         fprintf(stdout,"-o           : destination file (-o dest.j2k or .jp2) \n");
100         fprintf(stdout,"\n");
101         fprintf(stdout,"Optional Parameters:\n");
102         fprintf(stdout,"\n");
103         fprintf(stdout,"-h           : display the help information \n ");
104         fprintf(stdout,"\n");
105         fprintf(stdout,"-r           : different compression ratios for successive layers (-r 20,10,5)\n ");
106         fprintf(stdout,"                 - The rate specified for each quality level is the desired \n");
107         fprintf(stdout,"                   compression factor.\n");
108         fprintf(stdout,"                   Example: -r 20,10,1 means quality 1: compress 20x, \n");
109         fprintf(stdout,"                     quality 2: compress 10x and quality 3: compress lossless\n");
110         fprintf(stdout,"\n");
111         fprintf(stdout,"               (options -r and -q cannot be used together)\n ");
112         fprintf(stdout,"\n");
113
114         fprintf(stdout,"-q           : different psnr for successive layers (-q 30,40,50) \n ");
115
116         fprintf(stdout,"               (options -r and -q cannot be used together)\n ");
117
118         fprintf(stdout,"\n");
119         fprintf(stdout,"-n           : number of resolutions (-n 3) \n");
120         fprintf(stdout,"\n");
121         fprintf(stdout,"-b           : size of code block (-b 32,32) \n");
122         fprintf(stdout,"\n");
123         fprintf(stdout,"-c           : size of precinct (-c 128,128) \n");
124         fprintf(stdout,"\n");
125         fprintf(stdout,"-t           : size of tile (-t 512,512) \n");
126         fprintf(stdout,"\n");
127         fprintf(stdout,"-p           : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
128         fprintf(stdout,"\n");
129         fprintf(stdout,"-s           : subsampling factor (-s 2,2) [-s X,Y] \n");
130         fprintf(stdout,"             Remark: subsampling bigger than 2 can produce error\n");
131         fprintf(stdout,"\n");
132         fprintf(stdout,"-SOP         : write SOP marker before each packet \n");
133         fprintf(stdout,"\n");
134         fprintf(stdout,"-EPH         : write EPH marker after each header packet \n");
135         fprintf(stdout,"\n");
136         fprintf(stdout,"-M           : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
137         fprintf(stdout,"                 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
138         fprintf(stdout,"                 Indicate multiple modes by adding their values. \n");
139         fprintf(stdout,"                 ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
140         fprintf(stdout,"\n");
141         fprintf(stdout,"-x           : create an index file *.Idx (-x index_name.Idx) \n");
142         fprintf(stdout,"\n");
143         fprintf(stdout,"-ROI         : c=%%d,U=%%d : quantization indices upshifted \n");
144         fprintf(stdout,"               for component c=%%d [%%d = 0,1,2]\n");
145         fprintf(stdout,"               with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
146         fprintf(stdout,"\n");
147         fprintf(stdout,"-d           : offset of the origin of the image (-d 150,300) \n");
148         fprintf(stdout,"\n");
149         fprintf(stdout,"-T           : offset of the origin of the tiles (-T 100,75) \n");
150         fprintf(stdout,"\n");
151         fprintf(stdout,"-I           : use the irreversible DWT 9-7 (-I) \n");
152         fprintf(stdout,"\n");
153         fprintf(stdout,"IMPORTANT:\n");
154         fprintf(stdout,"-----------\n");
155         fprintf(stdout,"\n");
156         fprintf(stdout,"The index file has the structure below:\n");
157         fprintf(stdout,"---------------------------------------\n");
158         fprintf(stdout,"\n");
159         fprintf(stdout,"Image_height Image_width\n");
160         fprintf(stdout,"progression order\n");
161         fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
162         fprintf(stdout,"Components_nb\n");
163         fprintf(stdout,"Layers_nb\n");
164         fprintf(stdout,"decomposition_levels\n");
165         fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
166         fprintf(stdout,"   [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
167         fprintf(stdout,"Main_header_end_position\n");
168         fprintf(stdout,"Codestream_size\n");
169         fprintf(stdout,"Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
170         fprintf(stdout,"Tile_1   ''           ''        ''        ''       ''    ''\n");
171         fprintf(stdout,"...\n");
172         fprintf(stdout,"Tile_Nt   ''           ''        ''        ''       ''    ''\n");
173         fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
174         fprintf(stdout,"...\n");
175         fprintf(stdout,"Tpacket_Np ''   ''    ''   ''    ''       ''       ''     ''\n");
176
177         fprintf(stdout,"MaxDisto\n");
178
179         fprintf(stdout,"TotalDisto\n\n");
180 }
181
182 OPJ_PROG_ORDER give_progression(char progression[4]) {
183         if(strncmp(progression, "LRCP", 4) == 0) {
184                 return LRCP;
185         }
186         if(strncmp(progression, "RLCP", 4) == 0) {
187                 return RLCP;
188         }
189         if(strncmp(progression, "RPCL", 4) == 0) {
190                 return RPCL;
191         }
192         if(strncmp(progression, "PCRL", 4) == 0) {
193                 return PCRL;
194         }
195         if(strncmp(progression, "CPRL", 4) == 0) {
196                 return CPRL;
197         }
198
199         return PROG_UNKNOWN;
200 }
201
202 int get_file_format(char *filename) {
203         unsigned int i;
204         static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "j2k", "jp2" };
205         static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, J2K_CFMT, JP2_CFMT };
206         char * ext = strrchr(filename, '.') + 1;
207         for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
208                 if(strnicmp(ext, extension[i], 3) == 0) {
209                         return format[i];
210                 }
211         }
212
213         return -1;
214 }
215
216 /* ------------------------------------------------------------------------------------ */
217
218 int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters) {
219         int i, j;
220
221         /* parse the command line */
222
223         while (1) {
224                 int c = getopt(argc, argv, "i:o:r:q:f:t:n:c:b:x:p:s:d:hP:S:E:M:R:T:C:I");
225                 if (c == -1)
226                         break;
227                 switch (c) {
228                         case 'i':                       /* input file */
229                         {
230                                 char *infile = optarg;
231                                 parameters->decod_format = get_file_format(infile);
232                                 switch(parameters->decod_format) {
233                                         case PGX_DFMT:
234                                         case PXM_DFMT:
235                                         case BMP_DFMT:
236                                                 break;
237                                         default:
238                                                 fprintf(stderr, 
239                                                         "!! Unrecognized format for infile : %s [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n", 
240                                                         infile);
241                                                 return 1;
242                                                 break;
243                                 }
244                                 strncpy(parameters->infile, infile, MAX_PATH);
245                         }
246                         break;
247                                 
248                                 /* ----------------------------------------------------- */
249
250                         case 'o':                       /* output file */
251                         {
252                                 char *outfile = optarg;
253                                 parameters->cod_format = get_file_format(outfile);
254                                 switch(parameters->cod_format) {
255                                         case J2K_CFMT:
256                                         case JP2_CFMT:
257                                                 break;
258                                         default:
259                                                 fprintf(stderr, "Unknown output format image %s [only *.j2k, *.jp2]!! \n", outfile);
260                                                 return 1;
261                                                 break;
262                                 }
263                                 strncpy(parameters->outfile, outfile, MAX_PATH);
264                         }
265                         break;
266
267                                 /* ----------------------------------------------------- */
268                         
269                         case 'r':                       /* rates rates/distorsion */
270                         {
271                                 char *s = optarg;
272                                 while (sscanf(s, "%d", &parameters->tcp_rates[parameters->tcp_numlayers]) == 1) {
273                                         parameters->tcp_numlayers++;
274                                         while (*s && *s != ',') {
275                                                 s++;
276                                         }
277                                         if (!*s)
278                                                 break;
279                                         s++;
280                                 }
281                                 parameters->cp_disto_alloc = 1;
282                         }
283                         break;
284                                 
285                                 /* ----------------------------------------------------- */
286                         
287                         case 'q':                       /* add fixed_quality */
288                         {
289                                 char *s = optarg;
290                                 while (sscanf(s, "%f", &parameters->tcp_distoratio[parameters->tcp_numlayers]) == 1) {
291                                         parameters->tcp_numlayers++;
292                                         while (*s && *s != ',') {
293                                                 s++;
294                                         }
295                                         if (!*s)
296                                                 break;
297                                         s++;
298                                 }
299                                 parameters->cp_fixed_quality = 1;
300                         }
301                         break;
302                                 
303                                 /* dda */
304                                 /* ----------------------------------------------------- */
305
306                         case 'f':                       /* mod fixed_quality (before : -q) */
307                         {
308                                 int *row = NULL, *col = NULL;
309                                 int numlayers = 0, numresolution = 0, matrix_width = 0;
310
311                                 char *s = optarg;
312                                 sscanf(s, "%d", &numlayers);
313                                 s++;
314                                 if (numlayers > 9)
315                                         s++;
316
317                                 parameters->tcp_numlayers = numlayers;
318                                 numresolution = parameters->numresolution;
319                                 matrix_width = numresolution * 3;
320                                 parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
321                                 s = s + 2;
322
323                                 for (i = 0; i < numlayers; i++) {
324                                         row = &parameters->cp_matrice[i * matrix_width];
325                                         col = row;
326                                         parameters->tcp_rates[i] = 1;
327                                         sscanf(s, "%d,", &col[0]);
328                                         s += 2;
329                                         if (col[0] > 9)
330                                                 s++;
331                                         col[1] = 0;
332                                         col[2] = 0;
333                                         for (j = 1; j < numresolution; j++) {
334                                                 col += 3;
335                                                 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
336                                                 s += 6;
337                                                 if (col[0] > 9)
338                                                         s++;
339                                                 if (col[1] > 9)
340                                                         s++;
341                                                 if (col[2] > 9)
342                                                         s++;
343                                         }
344                                         if (i < numlayers - 1)
345                                                 s++;
346                                 }
347                                 parameters->cp_fixed_alloc = 1;
348                         }
349                         break;
350                                 
351                                 /* ----------------------------------------------------- */
352
353                         case 't':                       /* tiles */
354                         {
355                                 sscanf(optarg, "%d,%d", &parameters->cp_tdx, &parameters->cp_tdy);
356                                 parameters->tile_size_on = true;
357                         }
358                         break;
359                                 
360                                 /* ----------------------------------------------------- */
361                         
362                         case 'n':                       /* resolution */
363                         {
364                                 sscanf(optarg, "%d", &parameters->numresolution);
365                         }
366                         break;
367                                 
368                                 /* ----------------------------------------------------- */
369                         case 'c':                       /* precinct dimension */
370                         {
371                                 char sep;
372                                 int res_spec = 0;
373
374                                 char *s = optarg;
375                                 do {
376                                         sep = 0;
377                                         sscanf(s, "[%d,%d]%c", &parameters->prcw_init[res_spec], &parameters->prch_init[res_spec], &sep);
378                                         parameters->csty |= 0x01;
379                                         res_spec++;
380                                         s = strpbrk(s, "]") + 2;
381                                 }
382                                 while (sep == ',');
383                                 parameters->res_spec = res_spec;
384                         }
385                         break;
386                                 
387                                 /* ----------------------------------------------------- */
388                         
389                         case 'b':                       /* code-block dimension */
390                         {
391                                 int cblockw_init = 0, cblockh_init = 0;
392                                 sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
393                                 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
394                                         || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
395                                         fprintf(stderr,
396                                                 "!! Size of code_block error (option -b) !!\n\nRestriction :\n    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
397                                         return 1;
398                                 }
399                                 parameters->cblockw_init = cblockw_init;
400                                 parameters->cblockh_init = cblockh_init;
401                         }
402                         break;
403                                 
404                                 /* ----------------------------------------------------- */
405                         
406                         case 'x':                       /* creation of index file */
407                         {
408                                 char *index = optarg;
409                                 strncpy(parameters->index, index, MAX_PATH);
410                                 parameters->index_on = 1;
411                         }
412                         break;
413                                 
414                                 /* ----------------------------------------------------- */
415                         
416                         case 'p':                       /* progression order */
417                         {
418                                 char progression[4];
419
420                                 strncpy(progression, optarg, 4);
421                                 parameters->prog_order = give_progression(progression);
422                                 if (parameters->prog_order == -1) {
423                                         fprintf(stderr, "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
424                                         return 1;
425                                 }
426                         }
427                         break;
428                                 
429                                 /* ----------------------------------------------------- */
430                         
431                         case 's':                       /* subsampling factor */
432                         {
433                                 if (sscanf(optarg, "%d,%d", &parameters->subsampling_dx, &parameters->subsampling_dy) != 2) {
434                                         fprintf(stderr, "'-s' sub-sampling argument error !  [-s dx,dy]\n");
435                                         return 1;
436                                 }
437                         }
438                         break;
439                                 
440                                 /* ----------------------------------------------------- */
441                         
442                         case 'd':                       /* coordonnate of the reference grid */
443                         {
444                                 if (sscanf(optarg, "%d,%d", &parameters->image_offset_x0, &parameters->image_offset_y0) != 2) {
445                                         fprintf(stderr, "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
446                                         return 1;
447                                 }
448                         }
449                         break;
450                                 
451                                 /* ----------------------------------------------------- */
452                         
453                         case 'h':                       /* display an help description */
454                         {
455                                 encode_help_display();
456                                 return 1;
457                         }
458                         break;
459                                 
460                                 /* ----------------------------------------------------- */
461
462                         case 'P':                       /* POC */
463                         {
464                                 int numpocs = 0;                /* number of progression order change (POC) default 0 */
465                                 opj_poc_t *POC = NULL;  /* POC : used in case of Progression order change */
466
467                                 char *s = optarg;
468                                 POC = parameters->POC;
469
470                                 fprintf(stderr, "/----------------------------------\\\n");
471                                 fprintf(stderr, "|  POC option not fully tested !!  |\n");
472                                 fprintf(stderr, "\\----------------------------------/\n");
473                                 
474                                 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
475                                         &POC[numpocs].resno0, &POC[numpocs].compno0,
476                                         &POC[numpocs].layno1, &POC[numpocs].resno1,
477                                         &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
478                                         POC[numpocs].prg = give_progression(POC[numpocs].progorder);
479                                         /* POC[numpocs].tile; */
480                                         numpocs++;
481                                         while (*s && *s != '/') {
482                                                 s++;
483                                         }
484                                         if (!*s) {
485                                                 break;
486                                         }
487                                         s++;
488                                 }
489                                 parameters->numpocs = numpocs;
490                         }
491                         break;
492                                 
493                                 /* ------------------------------------------------------ */
494                                 
495                         case 'S':                       /* SOP marker */
496                         {
497                                 parameters->csty |= 0x02;
498                         }
499                         break;
500                                 
501                                 /* ------------------------------------------------------ */
502                         
503                         case 'E':                       /* EPH marker */
504                         {
505                                 parameters->csty |= 0x04;
506                         }
507                         break;
508                                 
509                                 /* ------------------------------------------------------ */
510                         
511                         case 'M':                       /* Mode switch pas tous au point !! */
512                         {
513                                 int value = 0;
514                                 if (sscanf(optarg, "%d", &value) == 1) {
515                                         for (i = 0; i <= 5; i++) {
516                                                 int cache = value & (1 << i);
517                                                 if (cache)
518                                                         parameters->mode |= (1 << i);
519                                         }
520                                 }
521                         }
522                         break;
523                                 
524                                 /* ------------------------------------------------------ */
525                         
526                         case 'R':                       /* ROI */
527                         {
528                                 if (sscanf(optarg, "OI:c=%d,U=%d", &parameters->roi_compno, &parameters->roi_shift) != 2) {
529                                         fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
530                                         return 1;
531                                 }
532                         }
533                         break;
534                                 
535                                 /* ------------------------------------------------------ */
536                         
537                         case 'T':                       /* Tile offset */
538                         {
539                                 if (sscanf(optarg, "%d,%d", &parameters->cp_tx0, &parameters->cp_ty0) != 2) {
540                                         fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
541                                         return 1;
542                                 }
543                         }
544                         break;
545                                 
546                                 /* ------------------------------------------------------ */
547                                 
548                         case 'C':                       /* add a comment */
549                         {
550                                 parameters->cp_comment = (char*)malloc(strlen(optarg) + 1);
551                                 if(parameters->cp_comment) {
552                                         strcpy(parameters->cp_comment, optarg);
553                                 }
554                         }
555                         break;
556                                 
557                                 /* ------------------------------------------------------ */
558                         
559                         case 'I':                       /* reversible or not */
560                         {
561                                 parameters->irreversible = 1;
562                         }
563                         break;
564                                 
565                                 /* ------------------------------------------------------ */
566                         
567                         default:
568                                 fprintf(stderr, "ERROR -> this option is not valid \"-%c %s\"\n", c, optarg);
569                                 return 1;
570                 }
571         }
572
573         /* check for possible errors */
574
575         if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
576                 fprintf(stderr, "usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
577                 return 1;
578         }
579
580         if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality)
581                 && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^ parameters->cp_fixed_quality))) {
582                 fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n");
583                 return 1;
584         }                               /* mod fixed_quality */
585
586         /* if no rate entered, lossless by default */
587         if (parameters->tcp_numlayers == 0) {
588                 parameters->tcp_rates[0] = 0;   /* MOD antonin : losslessbug */
589                 parameters->tcp_numlayers++;
590                 parameters->cp_disto_alloc = 1;
591         }
592
593         if((parameters->cp_tx0 > parameters->image_offset_x0) || (parameters->cp_ty0 > parameters->image_offset_y0)) {
594                 fprintf(stderr,
595                         "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
596                         parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0, parameters->image_offset_y0);
597                 return 1;
598         }
599
600         for (i = 0; i < parameters->numpocs; i++) {
601                 if (parameters->POC[i].prg == -1) {
602                         fprintf(stderr,
603                                 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
604                                 i + 1);
605                 }
606         }
607
608         return 0;
609 }
610
611 /* -------------------------------------------------------------------------- */
612
613 /**
614 sample error callback expecting a FILE* client object
615 */
616 void error_callback(const char *msg, void *client_data) {
617         FILE *stream = (FILE*)client_data;
618         fprintf(stream, "[ERROR] %s", msg);
619 }
620 /**
621 sample warning callback expecting a FILE* client object
622 */
623 void warning_callback(const char *msg, void *client_data) {
624         FILE *stream = (FILE*)client_data;
625         fprintf(stream, "[WARNING] %s", msg);
626 }
627 /**
628 sample debug callback expecting a FILE* client object
629 */
630 void info_callback(const char *msg, void *client_data) {
631         FILE *stream = (FILE*)client_data;
632         fprintf(stream, "[INFO] %s", msg);
633 }
634
635 /* -------------------------------------------------------------------------- */
636
637 int main(int argc, char **argv) {
638         bool bSuccess;
639         bool delete_comment = true;
640         opj_cparameters_t parameters;   /* compression parameters */
641         opj_event_mgr_t event_mgr;              /* event manager */
642         opj_image_t *image = NULL;
643
644         /* 
645         configure the event callbacks (not required)
646         setting of each callback is optionnal 
647         */
648         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
649         event_mgr.error_handler = error_callback;
650         event_mgr.warning_handler = warning_callback;
651         event_mgr.info_handler = info_callback;
652
653         /* set encoding parameters to default values */
654         opj_set_default_encoder_parameters(&parameters);
655
656         /* parse input and get user encoding parameters */
657         if(parse_cmdline_encoder(argc, argv, &parameters) == 1) {
658                 return 0;
659         }
660
661         if(parameters.cp_comment == NULL) {
662                 parameters.cp_comment = "Created by OpenJPEG version 0.9";
663                 /* no need to delete parameters.cp_comment on exit */
664                 delete_comment = false;
665         }
666
667         /* decode the source image */
668         /* ----------------------- */
669
670         switch (parameters.decod_format) {
671                 case PGX_DFMT:
672                         image = pgxtoimage(parameters.infile, &parameters);
673                         if (!image) {
674                                 fprintf(stderr, " unable to load pgx file\n");
675                                 return 1;
676                         }
677                         break;
678                 
679                 case PXM_DFMT:
680                         image = pnmtoimage(parameters.infile, &parameters);
681                         if (!image) {
682                                 fprintf(stderr, " not a pnm file\n");
683                                 return 1;
684                         }
685                         break;
686
687                 case BMP_DFMT:
688                         image = bmptoimage(parameters.infile, &parameters);
689                         if (!image) {
690                                 fprintf(stderr, " not a bmp file\n");
691                                 return 1;
692                         }
693                         break;
694         }
695
696         /* encode the destination image */
697         /* ---------------------------- */
698
699         if (parameters.cod_format == J2K_CFMT) {        /* J2K format output */
700                 int codestream_length;
701                 opj_cio_t *cio = NULL;
702                 FILE *f = NULL;
703
704                 /* get a J2K compressor handle */
705                 opj_cinfo_t* cinfo = opj_create_compress(CODEC_J2K);
706
707                 /* catch events using our callbacks and give a local context */
708                 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);                   
709
710                 /* setup the encoder parameters using the current image and using user parameters */
711                 opj_setup_encoder(cinfo, &parameters, image);
712
713                 /* open a byte stream for writing */
714                 /* allocate memory for all tiles */
715                 cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
716
717                 /* encode the image */
718                 bSuccess = opj_encode(cinfo, cio, image, parameters.index);
719                 if (!bSuccess) {
720                         opj_cio_close(cio);
721                         fprintf(stderr, "failed to encode image\n");
722                         return 1;
723                 }
724                 codestream_length = cio_tell(cio);
725
726                 /* write the buffer to disk */
727                 f = fopen(parameters.outfile, "wb");
728                 if (!f) {
729                         fprintf(stderr, "failed to open %s for writing\n", parameters.outfile);
730                         return 1;
731                 }
732                 fwrite(cio->buffer, 1, codestream_length, f);
733                 fclose(f);
734
735                 /* close and free the byte stream */
736                 opj_cio_close(cio);
737
738                 /* free remaining compression structures */
739                 opj_destroy_compress(cinfo);
740
741         } else {                        /* JP2 format output */
742                 int codestream_length;
743                 opj_cio_t *cio = NULL;
744                 FILE *f = NULL;
745
746                 /* get a JP2 compressor handle */
747                 opj_cinfo_t* cinfo = opj_create_compress(CODEC_JP2);
748
749                 /* catch events using our callbacks and give a local context */
750                 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);                   
751
752                 /* setup the encoder parameters using the current image and using user parameters */
753                 opj_setup_encoder(cinfo, &parameters, image);
754
755                 /* open a byte stream for writing */
756                 /* allocate memory for all tiles */
757                 cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
758
759                 /* encode the image */
760                 bSuccess = opj_encode(cinfo, cio, image, parameters.index);
761                 if (!bSuccess) {
762                         opj_cio_close(cio);
763                         fprintf(stderr, "failed to encode image\n");
764                         return 1;
765                 }
766                 codestream_length = cio_tell(cio);
767
768                 /* write the buffer to disk */
769                 f = fopen(parameters.outfile, "wb");
770                 if (!f) {
771                         fprintf(stderr, "failed to open %s for writing\n", parameters.outfile);
772                         return 1;
773                 }
774                 fwrite(cio->buffer, 1, codestream_length, f);
775                 fclose(f);
776
777                 /* close and free the byte stream */
778                 opj_cio_close(cio);
779
780                 /* free remaining compression structures */
781                 opj_destroy_compress(cinfo);
782
783         }
784
785         /* free user parameters structure */
786         if(delete_comment) {
787                 if(parameters.cp_comment) free(parameters.cp_comment);
788         }
789         if(parameters.cp_matrice) free(parameters.cp_matrice);
790
791         /* free image data */
792         opj_image_destroy(image);
793
794         return 0;
795 }
796