Update for version 0.8
[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 #include <openjpeg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 //#include <unistd.h>
34
35 int ceildiv(int a, int b)
36 {
37         return (a + b - 1) / b;
38 }
39
40 int main(int argc, char **argv)
41 {
42         FILE *f;
43         char *src, *src_name;
44         char *dest, S1, S2, S3;
45         int len;
46         j2k_image_t *img;
47         j2k_cp_t *cp;
48         int w, h, max;
49         int i, image_type = -1, compno;
50         int adjust;
51
52         if (argc < 3) {
53                 fprintf(stderr, "usage: %s j2k-file pnm-file\n", argv[0]);
54                 return 1;
55         }
56
57         f = fopen(argv[1], "rb");
58         if (!f) {
59                 fprintf(stderr, "failed to open %s for reading\n", argv[1]);
60                 return 1;
61         }
62
63         dest = argv[2];
64
65         while (*dest) {
66                 dest++;
67         }
68         dest--;
69         S3 = *dest;
70         dest--;
71         S2 = *dest;
72         dest--;
73         S1 = *dest;
74
75         if ((S1 == 'p' && S2 == 'g' && S3 == 'x') || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
76                 image_type = 0;
77         }
78
79         if ((S1 == 'p' && S2 == 'n' && S3 == 'm')|| (S1 == 'P' && S2 == 'N' && S3 == 'M') || 
80             (S1 == 'p' && S2 == 'g' && S3 == 'm')|| (S1 == 'P' && S2 == 'G' && S3 == 'M') || 
81             (S1 == 'P' && S2 == 'P' && S3 == 'M')|| (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
82                 image_type = 1;
83         }
84
85         if ((S1 == 'b' && S2 == 'm' && S3 == 'p') || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
86                 image_type = 2;
87         }
88
89         if (image_type == -1) {
90                 fprintf(stderr, "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",S1,S2,S3);
91                 return 1;
92         }
93
94         fseek(f, 0, SEEK_END);
95         len = ftell(f);
96         fseek(f, 0, SEEK_SET);
97         src = (char *) malloc(len);
98         fread(src, 1, len, f);
99         fclose(f);
100
101         src_name=argv[1];
102         while (*src_name) {
103                 src_name++;
104         }
105         src_name--;
106         S3 = *src_name;
107         src_name--;
108         S2 = *src_name;
109         src_name--;
110         S1 = *src_name;
111
112         if (S1 == 'j' && S2 == '2' && S3 == 'k')
113           {
114             if (!j2k_decode(src, len, &img, &cp)) {
115               fprintf(stderr, "j2k_to_image: failed to decode image!\n");
116               return 1;
117             }
118           }
119         else
120           {
121             if (S1 == 'j' && S2 == 'p' && S3 == 't')
122               { 
123                 if (!j2k_decode_jpt_stream(src, len, &img, &cp)) {
124                   fprintf(stderr, "j2k_to_image: failed to decode image!\n");
125                   return 1;
126                 }
127               }
128             else
129               {
130                 fprintf(stderr,"j2k_to_image : Unknown format image *.%c%c%c [only *.j2k or *.jpt]!! \n",S1,S2,S3);
131                 return 1;
132               }
133           }
134
135         free(src);
136         /* ------------------  CREATE OUT IMAGE WITH THE RIGHT FORMAT ----------------------- */
137
138         /* ---------------------------- / */
139         /* /                            / */
140         /* /  FORMAT : PNM, PGM or PPM  / */
141         /* /                            / */
142         /* ---------------------------- / */
143         
144         switch (image_type)
145           {     
146           case 1: /* PNM PGM PPM*/
147             if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
148                 && img->comps[1].dx == img->comps[2].dx
149                 && img->comps[0].dy == img->comps[1].dy
150                 && img->comps[1].dy == img->comps[2].dy
151                 && img->comps[0].prec == img->comps[1].prec
152                 && img->comps[1].prec == img->comps[2].prec) 
153               {
154                 f = fopen(argv[2], "wb");
155                 w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
156                 h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
157                 //max = (1 << img->comps[0].prec) - 1;
158                 max =img->comps[0].prec>8? 255:(1 << img->comps[0].prec) - 1;
159                 fprintf(f, "P6\n%d %d\n%d\n", w, h, max);
160                 adjust=img->comps[0].prec>8?img->comps[0].prec-8:0;
161                 for (i = 0; i < w * h; i++) 
162                   {
163                     char r, g, b;
164                     r = img->comps[0].data[i];
165                     r+=(img->comps[0].sgnd? 1 << (img->comps[0].prec-1):0);
166                     r=r>>adjust;                         
167                     
168                     g = img->comps[1].data[i];
169                     g+=(img->comps[1].sgnd? 1 << (img->comps[1].prec-1):0);
170                     g=g>>adjust;
171                     
172                     b = img->comps[2].data[i];
173                     b+=(img->comps[2].sgnd? 1 << (img->comps[2].prec-1):0);
174                     b=b>>adjust;
175                     
176                     fprintf(f, "%c%c%c", r, g, b);
177                   }
178                 fclose(f);
179               } else 
180                 { 
181                   for (compno = 0; compno < img->numcomps; compno++) 
182                     {
183                       char name[256];
184                       if (img->numcomps > 1) {
185                         sprintf(name, "%d.%s", compno, argv[2]);
186                       } else 
187                         {
188                           sprintf(name, "%s", argv[2]);
189                         }
190                       f = fopen(name, "wb");
191                       w = ceildiv(img->x1 - img->x0, img->comps[compno].dx);
192                       h = ceildiv(img->y1 - img->y0, img->comps[compno].dy);
193                       max =img->comps[compno].prec>8? 255:(1 << img->comps[compno].prec) - 1;
194                       fprintf(f, "P5\n%d %d\n%d\n", w, h, max);
195                       adjust=img->comps[compno].prec>8?img->comps[compno].prec-8:0;
196                       for (i = 0; i < w * h; i++) 
197                         {
198                           char l;
199                           l = img->comps[compno].data[i];
200                           l+=(img->comps[compno].sgnd? 1 << (img->comps[compno].prec-1):0);
201                           l=l>>adjust;
202                           fprintf(f, "%c", l);
203                         }
204                       fclose(f);
205                     }
206                 }
207             break ;
208             
209             /* ------------------------ / */
210             /* /                        / */
211             /* /     FORMAT : PGX       / */
212             /* /                        / */
213             /* /----------------------- / */
214           case 0: /* PGX */
215             for (compno = 0; compno < img->numcomps; compno++) 
216               {
217                 j2k_comp_t *comp = &img->comps[compno];
218                 char name[256];
219                 if (img->numcomps>1)
220                   sprintf(name, "%d_%s", compno, argv[2]);
221                 else
222                   sprintf(name, "%s", argv[2]);
223                 f = fopen(name, "wb");
224                 w = ceildiv(img->x1 - img->x0, comp->dx);
225                 h = ceildiv(img->y1 - img->y0, comp->dy);
226                 fprintf(f, "PG LM %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
227                 for (i = 0; i < w * h; i++) 
228                   {
229                     int v = img->comps[compno].data[i];
230                     if (comp->prec <= 8) 
231                       {
232                         char c = (char) v;
233                         fwrite(&c, 1, 1, f);
234                       } else if (comp->prec <= 16) 
235                         {
236                           short s = (short) v;
237                           fwrite(&s, 2, 1, f);
238                         } else 
239                           {
240                             fwrite(&v, 4, 1, f);
241                           }
242                   }
243                 fclose(f);
244               }
245             break ;
246             
247             /* ------------------------ / */
248             /* /                        / */
249             /* /     FORMAT : BMP       / */
250             /* /                        / */
251             /* /----------------------- / */
252             
253           case 2:  /* BMP */
254             if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
255                 && img->comps[1].dx == img->comps[2].dx
256                 && img->comps[0].dy == img->comps[1].dy
257                 && img->comps[1].dy == img->comps[2].dy
258                 && img->comps[0].prec == img->comps[1].prec
259                 && img->comps[1].prec == img->comps[2].prec) 
260               {
261                 /* -->> -->> -->> -->>
262                    
263                    24 bits color
264                    
265                    <<-- <<-- <<-- <<-- */
266                 
267                 f = fopen(argv[2], "wb");
268                 w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
269                 h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
270                 
271                 fprintf(f, "BM");
272                 
273                 /* FILE HEADER */
274                 /* ------------- */
275                 fprintf(f, "%c%c%c%c",
276                         (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
277                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
278                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
279                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
280                 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
281                 fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
282                 
283                 /* INFO HEADER   */
284                 /* ------------- */
285                 fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
286                 fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),(unsigned char) ((w) >> 8) & 0xff,
287                         (unsigned char) ((w) >> 16) & 0xff, (unsigned char) ((w) >> 24) & 0xff);
288                 fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff), (unsigned char) ((h) >> 8) & 0xff,
289                         (unsigned char) ((h) >> 16) & 0xff, (unsigned char) ((h) >> 24) & 0xff);
290                 fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
291                 fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
292                 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
293                 fprintf(f, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
294                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
295                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
296                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
297                 fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
298                 fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
299                 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
300                 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
301                 
302                 for (i = 0; i < w * h; i++) 
303                   {
304                     unsigned char R, G, B;
305                     
306                     R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
307                     G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
308                     B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
309                     fprintf(f, "%c%c%c", B, G, R);
310                     if (((i + 1) % w == 0 && w % 2))
311                       fprintf(f, "%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff);
312                   }
313                 fclose(f);
314               } else    
315                 {   /* Gray-scale */
316                   
317                   /* -->> -->> -->> -->>
318                      
319                      8 bits non code (Gray scale)
320                      
321                      <<-- <<-- <<-- <<-- */
322                   f = fopen(argv[2], "wb");
323                   w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
324                   h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
325                   
326                   fprintf(f, "BM");
327                   
328                   /* FILE HEADER */
329                   /* ------------- */
330                   fprintf(f, "%c%c%c%c",
331                           (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
332                           (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
333                           (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
334                           (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
335                   fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
336                   fprintf(f, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
337                           ((54 + 1024) >> 16) & 0xff, ((54 + 1024) >> 24) & 0xff);
338                   
339                   /* INFO HEADER */
340                   /* ------------- */
341                   fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
342                   fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff), (unsigned char) ((w) >> 8) & 0xff,
343                           (unsigned char) ((w) >> 16) & 0xff, (unsigned char) ((w) >> 24) & 0xff);
344                   fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff), (unsigned char) ((h) >> 8) & 0xff,
345                           (unsigned char) ((h) >> 16) & 0xff, (unsigned char) ((h) >> 24) & 0xff);
346                   fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
347                   fprintf(f, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
348                   fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
349                   fprintf(f, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
350                           (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
351                           (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
352                           (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
353                   fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
354                   fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
355                   fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
356                   fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
357                 }
358             
359             for (i = 0; i < 256; i++) 
360               {
361                 fprintf(f, "%c%c%c%c", i, i, i, 0);
362               }
363             
364             for (i = 0; i < w * h; i++) 
365               {
366                 fprintf(f, "%c", img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
367                 if (((i + 1) % w == 0 && w % 2))
368                   fprintf(f, "%c", 0);
369               }
370             break;
371           default :
372             break;
373           }
374         
375         return 0;
376 }