Added layer option '-l' to the command line. This option allows user to
[openjpeg.git] / codec / j2k_to_image.c
1 /* Copyright (c) 2001 David Janssens
2 * Copyright (c) 2002-2003 Yannick Verschueren
3 * Copyright (c) 2002-2003 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
4
5 * All rights reserved. 
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28
29
30 //MEMORY LEAK
31
32 #ifdef _DEBUG
33
34 #define _CRTDBG_MAP_ALLOC
35
36 #include <stdlib.h>  // Must be included first
37
38 #include <crtdbg.h>
39
40 #endif
41
42 //MEM
43
44
45
46 #include <openjpeg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #ifndef DONT_HAVE_GETOPT
51 #include <getopt.h>
52 #else
53 #include "compat/getopt.h"
54 #endif
55
56 void usage_display(char *prgm)
57 {
58   fprintf(stdout,"Usage:\n");
59   fprintf(stdout,"  %s...\n",prgm);
60   fprintf(stdout,"  -i <compressed file>\n");
61   fprintf(stdout,"    REQUIRED\n");
62   fprintf(stdout,"    Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
63   fprintf(stdout,"    is identified based on its suffix.\n");
64   fprintf(stdout,"  -o <decompressed file>\n");
65   fprintf(stdout,"    REQUIRED\n");
66   fprintf(stdout,"    Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
67   fprintf(stdout,"    BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
68   fprintf(stdout,"    filename is given, there will be as many output files as there are\n");
69   fprintf(stdout,"    components: an indice starting from 0 will then be appended to the\n");
70   fprintf(stdout,"    output filename, just before the \"pgx\" extension. If a PGM filename\n");
71   fprintf(stdout,"    is given and there are more than one component, only the first component\n");
72   fprintf(stdout,"    will be written to the file.\n");
73   fprintf(stdout,"  -r <reduce factor>\n");
74   fprintf(stdout,"    Set the number of highest resolution levels to be discarded. The\n");
75   fprintf(stdout,"    image resolution is effectively divided by 2 to the power of the\n");
76   fprintf(stdout,"    number of discarded levels. The reduce factor is limited by the\n");
77   fprintf(stdout,"    smallest total number of decomposition levels among tiles.\n");
78   fprintf(stdout,"  -l <number of quality layers to decode>\n");
79   fprintf(stdout,"    Set the maximum number of quality layers to decode. If there are\n");
80   fprintf(stdout,"    less quality layers than the specified number, all the quality layers\n");
81   fprintf(stdout,"    are decoded.\n");
82   fprintf(stdout,"  -u\n");
83   fprintf(stdout,"    print an usage statement\n");
84   fprintf(stdout,"\n");
85 }
86
87 int main(int argc, char **argv)
88 {
89   FILE *fsrc=NULL;
90   FILE *fdest=NULL;
91   char *infile=NULL;
92   char *outfile=NULL;
93   char *tmp=NULL;
94   char S1, S2, S3;
95   
96   char *src=NULL; 
97   char *dest=NULL;
98   
99   int len;
100   
101   j2k_image_t img;
102   j2k_cp_t cp;
103   jp2_struct_t *jp2_struct=NULL;
104   
105   int w, wr, wrr, h, hr, hrr, max;
106   int i, compno, pad, j;
107   int adjust;
108   
109   cp.layer=0;
110   cp.reduce=0;
111   cp.decod_format=-1;
112   cp.cod_format=-1;
113   
114   while (1) {
115     int c = getopt(argc, argv,"i:o:r:l:u");
116     if (c == -1)
117       break;
118     switch (c) {
119       
120       //Input file
121     case 'i':
122       infile = optarg;
123       tmp = optarg;
124       while (*tmp) {
125         tmp++;
126       }
127       tmp--;
128       S3 = *tmp;
129       tmp--;
130       S2 = *tmp;
131       tmp--;
132       S1 = *tmp;
133       
134       /* J2K format */
135       if ((S1 == 'j' && S2 == '2' && S3 == 'k')
136         || (S1 == 'J' && S2 == '2' && S3 == 'K') 
137         || (S1 == 'j' && S2 == '2' && S3 == 'c')
138         || (S1 == 'J' && S2 == '2' && S3 == 'C')) {
139         cp.cod_format=J2K_CFMT;
140         break;
141       }
142       
143       /* JP2 format */
144       if ((S1 == 'j' && S2 == 'p' && S3 == '2')
145         || (S1 == 'J' && S2 == 'P' && S3 == '2')) {
146         cp.cod_format=JP2_CFMT;
147         break;
148       }
149       
150       /* JPT format */
151       if ((S1 == 'j' && S2 == 'p' && S3 == 't')
152         || (S1 == 'J' && S2 == 'P' && S3 == 'T')) {
153         cp.cod_format=JPT_CFMT;
154         break;
155       }
156       
157       fprintf(stderr,
158         "j2k_to_image : Unknown input image format *.%c%c%c [only *.j2k, *.jp2, *.jpc or *.jpt]!! \n",
159         S1, S2, S3);
160       return 1;
161       break;
162       
163       /* ----------------------------------------------------- */
164       
165       //Output file
166     case 'o':
167       outfile = optarg;
168       tmp = optarg;
169       while (*tmp) {
170         tmp++;
171       }
172       tmp--;
173       S3 = *tmp;
174       tmp--;
175       S2 = *tmp;
176       tmp--;
177       S1 = *tmp;
178       
179       // PGX format      
180       if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
181         || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
182         cp.decod_format = PGX_DFMT;
183         break;
184       }
185       
186       // PxM format 
187       if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
188         || (S1 == 'P' && S2 == 'N' && S3 == 'M') 
189         || (S1 == 'p' && S2 == 'g' && S3 == 'm')
190         || (S1 == 'P' && S2 == 'G' && S3 == 'M') 
191         || (S1 == 'P' && S2 == 'P' && S3 == 'M')
192         || (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
193         cp.decod_format = PXM_DFMT;
194         break;
195       }
196       
197       // BMP format 
198       if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
199         || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
200         cp.decod_format = BMP_DFMT;
201         break;
202       }
203       
204       // otherwise : error
205       fprintf(stderr,
206         "!! Unrecognized output image format *.%c%c%c [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n",
207         S1, S2, S3);
208       
209       return 1;
210       break;
211       
212       /* ----------------------------------------------------- */
213       
214       //Reduce option
215     case 'r':
216       tmp=optarg;
217       sscanf(tmp, "%d", &cp.reduce);
218       break;
219       
220       /* ----------------------------------------------------- */
221       
222       //Layering option
223     case 'l':
224       tmp=optarg;
225       sscanf(tmp, "%d", &cp.layer);
226       break;
227       
228       /* ----------------------------------------------------- */
229       
230     case 'u':                   
231       usage_display(argv[0]);
232       return 0;
233       break;
234       /* ----------------------------------------------------- */
235       
236     default:
237       fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c,optarg);
238       break;
239       
240     }
241   }
242   
243   //Check required arguments
244   //------------------------
245   if (!infile || !outfile) {
246     fprintf(stderr,"ERROR -> At least one required argument is missing\nCheck j2k_to_image -u for usage information\n");
247     return 1;
248   }
249   
250   //Read the input file and put it in memory
251   //----------------------------------------
252   fsrc = fopen(infile, "rb");
253   if (!fsrc) {
254     fprintf(stderr, "ERROR -> failed to open %s for reading\n", infile);
255     return 1;
256   }
257   fseek(fsrc, 0, SEEK_END);
258   len = ftell(fsrc);
259   fseek(fsrc, 0, SEEK_SET);
260   src = (char *) malloc(len);
261   fread(src, 1, len, fsrc);
262   fclose(fsrc);
263   
264   //Decode the code-stream
265   //----------------------
266   switch(cp.cod_format) {
267     
268   case J2K_CFMT:
269     if (!j2k_decode(src, len, &img, &cp)) {
270       fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
271       return 1;
272     }
273     break;
274     
275   case JP2_CFMT:
276     jp2_struct = (jp2_struct_t *) malloc(sizeof(jp2_struct_t));
277     jp2_struct->image = &img;
278     
279     if (jp2_read_struct(src, jp2_struct, len)) {
280       fprintf(stderr, "ERROR -> j2k_to_image: failed to decode jp2 structure!\n");
281       return 1;
282     }
283     
284     if (!j2k_decode(src + jp2_struct->j2k_codestream_offset, jp2_struct->j2k_codestream_len, &img, &cp)) {
285       fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
286       return 1;
287     }
288     
289     /* Insert code here if you want to create actions on jp2_struct before deleting it */
290     
291     free(jp2_struct);
292     break;
293     
294   case JPT_CFMT:
295     if (!j2k_decode_jpt_stream(src, len, &img, &cp)) {
296       fprintf(stderr, "ERROR -> j2k_to_image: failed to decode JPT-file!\n");
297       return 1;
298     }
299     break;
300     
301   default:
302     fprintf(stderr,
303       "ERROR -> j2k_to_image : Unknown input image format\n");
304     return 1;
305     break;
306   }
307   
308   //Free the memory containing the code-stream
309   //------------------------------------------
310   
311   free(src);
312   
313   
314   //Create output image
315   //-------------------
316   
317   /* ---------------------------- / */
318   /* /                            / */
319   /* /  FORMAT : PNM, PGM or PPM  / */
320   /* /                            / */
321   /* ---------------------------- / */
322   
323   switch (cp.decod_format) {
324   case PXM_DFMT:                        /* PNM PGM PPM */
325     
326     tmp=outfile;
327     while (*tmp) {
328       tmp++;
329     }
330     tmp--;
331     tmp--;
332     S2 = *tmp;
333     
334     if (img.numcomps == 3 && img.comps[0].dx == img.comps[1].dx
335       && img.comps[1].dx == img.comps[2].dx
336       && img.comps[0].dy == img.comps[1].dy
337       && img.comps[1].dy == img.comps[2].dy
338       && img.comps[0].prec == img.comps[1].prec
339       && img.comps[1].prec == img.comps[2].prec
340       && S2 !='g' && S2 !='G') {
341       
342       fdest = fopen(outfile, "wb");
343       if (!fdest) {
344         fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
345         return 1;
346       }
347       
348       w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
349       // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor),img.comps[0].dx);
350       wr = img.comps[0].w;
351       wrr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
352       
353       h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
354       // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
355       hr = img.comps[0].h;
356       hrr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
357       
358       max = img.comps[0].prec > 8 ? 255 : (1 << img.comps[0].prec) - 1;
359       
360       img.comps[0].x0 =
361         int_ceildivpow2(img.comps[0].x0 -
362         int_ceildiv(img.x0, img.comps[0].dx),
363         img.comps[0].factor);
364       img.comps[0].y0 =
365         int_ceildivpow2(img.comps[0].y0 -
366         int_ceildiv(img.y0, img.comps[0].dy),
367         img.comps[0].factor);
368       
369       
370       fprintf(fdest, "P6\n# %d %d %d %d %d\n%d %d\n%d\n",
371         cp.tcps[cp.tileno[0]].tccps[0].numresolutions, w, h,
372         img.comps[0].x0, img.comps[0].y0, wrr, hrr, max);
373       adjust = img.comps[0].prec > 8 ? img.comps[0].prec - 8 : 0;
374       for (i = 0; i < wrr * hrr; i++) {
375         char r, g, b;
376         r = img.comps[0].data[i / wrr * wr + i % wrr];
377         r += (img.comps[0].sgnd ? 1 << (img.comps[0].prec - 1) : 0);
378         r = r >> adjust;
379         
380         g = img.comps[1].data[i / wrr * wr + i % wrr];
381         g += (img.comps[1].sgnd ? 1 << (img.comps[1].prec - 1) : 0);
382         g = g >> adjust;
383         
384         b = img.comps[2].data[i / wrr * wr + i % wrr];
385         b += (img.comps[2].sgnd ? 1 << (img.comps[2].prec - 1) : 0);
386         b = b >> adjust;
387         
388         fprintf(fdest, "%c%c%c", r, g, b);
389       }
390       free(img.comps[0].data);
391       free(img.comps[1].data);
392       free(img.comps[2].data);
393       fclose(fdest);
394       
395     } else {
396       int ncomp=(S2=='g' || S2=='G')?1:img.numcomps;
397       if (img.numcomps>ncomp) {
398         fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
399         fprintf(stderr,"           is written to the file\n");
400       }
401       for (compno = 0; compno < ncomp; compno++) {
402         char name[256];
403         if (ncomp > 1) {
404           sprintf(name, "%d.%s", compno, outfile);
405         } else {
406           sprintf(name, "%s", outfile);
407         }
408         
409         fdest = fopen(name, "wb");
410         if (!fdest) {
411           fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
412           return 1;
413         }
414         
415         w = int_ceildiv(img.x1 - img.x0, img.comps[compno].dx);
416         // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor),img.comps[compno].dx);
417         wr = img.comps[compno].w;
418         wrr =
419           int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
420         
421         h = int_ceildiv(img.y1 - img.y0, img.comps[compno].dy);
422         // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[compno].dy);
423         hr = img.comps[compno].h;
424         hrr =
425           int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
426         
427         max =
428           img.comps[compno].prec >
429           8 ? 255 : (1 << img.comps[compno].prec) - 1;
430         
431         img.comps[compno].x0 =
432           int_ceildivpow2(img.comps[compno].x0 -
433           int_ceildiv(img.x0,
434                                       img.comps[compno].dx),
435                                       img.comps[compno].factor);
436         img.comps[compno].y0 =
437           int_ceildivpow2(img.comps[compno].y0 -
438           int_ceildiv(img.y0,
439                                       img.comps[compno].dy),
440                                       img.comps[compno].factor);
441         
442         fprintf(fdest, "P5\n# %d %d %d %d %d\n%d %d\n%d\n",
443           cp.tcps[cp.tileno[0]].tccps[compno].
444           numresolutions, w, h, img.comps[compno].x0,
445           img.comps[compno].y0, wrr, hrr, max);
446         adjust =
447           img.comps[compno].prec > 8 ? img.comps[compno].prec - 8 : 0;
448         for (i = 0; i < wrr * hrr; i++) {
449           char l;
450           l = img.comps[compno].data[i / wrr * wr + i % wrr];
451           l += (img.comps[compno].
452             sgnd ? 1 << (img.comps[compno].prec - 1) : 0);
453           l = l >> adjust;
454           fprintf(fdest, "%c", l);
455         }
456         fclose(fdest);
457         free(img.comps[compno].data);
458       }
459     }
460     break;
461     
462     /* ------------------------ / */
463     /* /                        / */
464     /* /     FORMAT : PGX       / */
465     /* /                        / */
466     /* /----------------------- / */
467   case PGX_DFMT:                        /* PGX */
468     for (compno = 0; compno < img.numcomps; compno++) {
469       j2k_comp_t *comp = &img.comps[compno];
470       char name[256];
471       int nbytes = 0;
472       tmp = outfile;
473       while (*tmp) {
474         tmp++;
475       }
476       while (*tmp!='.') {
477         tmp--;
478       }
479       *tmp='\0';
480       //if (img.numcomps > 1)
481       sprintf(name, "%s-%d.pgx", outfile, compno);
482       
483       //else
484       
485       //sprintf(name, "%s.pgx", outfile);
486       
487       fdest = fopen(name, "wb");
488       if (!fdest) {
489         fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
490         return 1;
491       }
492       
493       // w = int_ceildiv(img.x1 - img.x0, comp->dx);
494       // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), comp->dx);
495       w = img.comps[compno].w;
496       wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
497       
498       // h = int_ceildiv(img.y1 - img.y0, comp->dy);
499       // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), comp->dy);
500       h = img.comps[compno].h;
501       hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
502       
503       fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+',
504         comp->prec, wr, hr);
505       
506       if (comp->prec <= 8)
507         nbytes = 1;
508       
509       else if (comp->prec <= 16)
510         nbytes = 2;
511       
512       else
513         nbytes = 4;
514       for (i = 0; i < wr * hr; i++) {
515         int v = img.comps[compno].data[i / wr * w + i % wr];
516         
517         for (j = nbytes - 1; j >= 0; j--) {
518           
519           char byte = (char) (v >> (j * 8));
520           
521           fwrite(&byte, 1, 1, fdest);
522           
523         }
524       }
525       free(img.comps[compno].data);
526       fclose(fdest);
527     }
528     break;
529     
530     /* ------------------------ / */
531     /* /                        / */
532     /* /     FORMAT : BMP       / */
533     /* /                        / */
534     /* /----------------------- / */
535     
536   case BMP_DFMT:                        /* BMP */
537     if (img.numcomps == 3 && img.comps[0].dx == img.comps[1].dx
538       && img.comps[1].dx == img.comps[2].dx
539       && img.comps[0].dy == img.comps[1].dy
540       && img.comps[1].dy == img.comps[2].dy
541       && img.comps[0].prec == img.comps[1].prec
542       && img.comps[1].prec == img.comps[2].prec) {
543       /* -->> -->> -->> -->>
544       
545        24 bits color
546        
547       <<-- <<-- <<-- <<-- */
548       
549       fdest = fopen(outfile, "wb");
550       if (!fdest) {
551         fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
552         return 1;
553       }
554       
555       // w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
556       // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), img.comps[0].dx);
557       w = img.comps[0].w;
558       wr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
559       
560       // h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
561       // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
562       h = img.comps[0].h;
563       hr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
564       
565       fprintf(fdest, "BM");
566       
567       /* FILE HEADER */
568       /* ------------- */
569       fprintf(fdest, "%c%c%c%c",
570         (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) +
571         54) & 0xff,
572         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
573         >> 8) & 0xff,
574         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
575         >> 16) & 0xff,
576         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
577         >> 24) & 0xff);
578       fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
579         ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
580       fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
581         ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
582       
583       /* INFO HEADER   */
584       /* ------------- */
585       fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
586         ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
587       fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
588         (unsigned char) ((wr) >> 8) & 0xff,
589         (unsigned char) ((wr) >> 16) & 0xff,
590         (unsigned char) ((wr) >> 24) & 0xff);
591       fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
592         (unsigned char) ((hr) >> 8) & 0xff,
593         (unsigned char) ((hr) >> 16) & 0xff,
594         (unsigned char) ((hr) >> 24) & 0xff);
595       fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
596       fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
597       fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
598         ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
599       fprintf(fdest, "%c%c%c%c",
600         (unsigned char) (3 * hr * wr +
601         3 * hr * (wr % 2)) & 0xff,
602         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
603         8) & 0xff,
604         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
605         16) & 0xff,
606         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
607         24) & 0xff);
608       fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
609         ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
610       fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
611         ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
612       fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
613         ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
614       fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
615         ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
616       
617       for (i = 0; i < wr * hr; i++) {
618         unsigned char R, G, B;
619         /* a modifier */
620         // R = img.comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
621         R = img.comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
622         // G = img.comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
623         G = img.comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
624         // B = img.comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
625         B = img.comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
626         fprintf(fdest, "%c%c%c", B, G, R);
627         
628         if ((i + 1) % wr == 0) {
629           for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--)       /* ADD */
630             fprintf(fdest, "%c", 0);
631         }
632       }
633       fclose(fdest);
634       free(img.comps[1].data);
635       free(img.comps[2].data);
636     } else {                    /* Gray-scale */
637       
638                                 /* -->> -->> -->> -->>
639                                 
640                                  8 bits non code (Gray scale)
641                                  
642         <<-- <<-- <<-- <<-- */
643       fdest = fopen(argv[2], "wb");
644       // w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
645       // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), img.comps[0].dx);
646       w = img.comps[0].w;
647       wr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
648       
649       // h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
650       // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
651       h = img.comps[0].h;
652       hr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
653       
654       fprintf(fdest, "BM");
655       
656       /* FILE HEADER */
657       /* ------------- */
658       fprintf(fdest, "%c%c%c%c",
659         (unsigned char) (hr * wr + 54 + 1024 +
660         hr * (wr % 2)) & 0xff,
661         (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2))
662         >> 8) & 0xff,
663         (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2))
664         >> 16) & 0xff,
665         (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2))
666         >> 24) & 0xff);
667       fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
668         ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
669       fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff,
670         ((54 + 1024) >> 8) & 0xff, ((54 + 1024) >> 16) & 0xff,
671         ((54 + 1024) >> 24) & 0xff);
672       
673       /* INFO HEADER */
674       /* ------------- */
675       fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
676         ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
677       fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
678         (unsigned char) ((wr) >> 8) & 0xff,
679         (unsigned char) ((wr) >> 16) & 0xff,
680         (unsigned char) ((wr) >> 24) & 0xff);
681       fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
682         (unsigned char) ((hr) >> 8) & 0xff,
683         (unsigned char) ((hr) >> 16) & 0xff,
684         (unsigned char) ((hr) >> 24) & 0xff);
685       fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
686       fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
687       fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
688         ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
689       fprintf(fdest, "%c%c%c%c",
690         (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
691         (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) &
692         0xff,
693         (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) &
694         0xff,
695         (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
696       fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
697         ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
698       fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
699         ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
700       fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
701         ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
702       fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
703         ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
704     }
705     
706     for (i = 0; i < 256; i++) {
707       fprintf(fdest, "%c%c%c%c", i, i, i, 0);
708     }
709     
710     for (i = 0; i < wr * hr; i++) {
711       /* a modifier !! */
712       // fprintf(fdest, "%c", img.comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
713       fprintf(fdest, "%c",
714         img.comps[0].data[w * hr - ((i) / (wr) + 1) * w +
715         (i) % (wr)]);
716         /*if (((i + 1) % w == 0 && w % 2))
717       fprintf(fdest, "%c", 0); */
718       if ((i + 1) % wr == 0) {
719         for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--)     /* ADD */
720           fprintf(fdest, "%c", 0);
721       }
722     }
723     fclose(fdest);
724     free(img.comps[0].data);
725     break;
726
727   default:
728     fprintf(stderr,
729       "ERROR -> j2k_to_image : Unknown output image format\n");
730     return 1;
731     break;
732   }
733   
734   
735   // Free remaining structures
736   //--------------------------
737   j2k_dec_release();
738   
739   
740   
741   // Check memory leaks if debug mode
742   //---------------------------------
743   
744 #ifdef _DEBUG
745   
746   _CrtDumpMemoryLeaks();
747   
748 #endif
749   
750   return 0;
751 }