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