575eec11495a3c5d20f43597217123fb701aa741
[openjpeg.git] / codec / convert.c
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2002-2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <openjpeg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35 /* -->> -->> -->> -->>
36
37   BMP IMAGE FORMAT
38
39  <<-- <<-- <<-- <<-- */
40
41 /* UINT2 defines a two byte word */
42 typedef unsigned short int UINT2;
43
44 /* UINT4 defines a four byte word */
45 typedef unsigned long int UINT4;
46
47 typedef struct {
48     UINT2 bfType;               /* 'BM' for Bitmap (19776) */
49     UINT4 bfSize;               /* Size of the file        */
50     UINT2 bfReserved1;          /* Reserved : 0            */
51     UINT2 bfReserved2;          /* Reserved : 0            */
52     UINT4 bfOffBits;            /* Offset                  */
53 } BITMAPFILEHEADER_t;
54
55 typedef struct {
56     UINT4 biSize;               /* Size of the structure in bytes */
57     UINT4 biWidth;              /* Width of the image in pixels */
58     UINT4 biHeight;             /* Heigth of the image in pixels */
59     UINT2 biPlanes;             /* 1 */
60     UINT2 biBitCount;           /* Number of color bits by pixels */
61     UINT4 biCompression;        /* Type of encoding 0: none 1: RLE8 2: RLE4 */
62     UINT4 biSizeImage;          /* Size of the image in bytes */
63     UINT4 biXpelsPerMeter;      /* Horizontal (X) resolution in pixels/meter */
64     UINT4 biYpelsPerMeter;      /* Vertical (Y) resolution in pixels/meter */
65     UINT4 biClrUsed;            /* Number of color used in the image (0: ALL) */
66     UINT4 biClrImportant;       /* Number of important color (0: ALL) */
67 } BITMAPINFOHEADER_t;
68
69 int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
70                int subsampling_dy, int Dim[2])
71 {
72     FILE *IN;
73     FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL;
74     BITMAPFILEHEADER_t File_h;
75     BITMAPINFOHEADER_t Info_h;
76     unsigned char *RGB;
77     unsigned char *table_R, *table_G, *table_B;
78     int i, w, h, PAD, type = 0;
79     int gray_scale = 1, not_end_file = 1, line = 0, col = 0;
80     unsigned char v, v2;
81     UINT4 W, H;
82
83     IN = fopen(filename, "rb");
84     if (!IN) {
85         fprintf(stderr,
86                 "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
87                 filename);
88         return 0;
89     }
90
91     File_h.bfType = getc(IN);
92     File_h.bfType = (getc(IN) << 8) + File_h.bfType;
93
94     if (File_h.bfType != 19778) {
95         printf("Error, not a BMP file!\n");
96         return 0;
97     } else {
98         /* FILE HEADER */
99         /* ------------- */
100         File_h.bfSize = getc(IN);
101         File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
102         File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
103         File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
104
105         File_h.bfReserved1 = getc(IN);
106         File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
107
108         File_h.bfReserved2 = getc(IN);
109         File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
110
111         File_h.bfOffBits = getc(IN);
112         File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
113         File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
114         File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
115
116         /* INFO HEADER */
117         /* ------------- */
118
119         Info_h.biSize = getc(IN);
120         Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
121         Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
122         Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
123
124         Info_h.biWidth = getc(IN);
125         Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
126         Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
127         Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
128         w = Info_h.biWidth;
129
130         Info_h.biHeight = getc(IN);
131         Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
132         Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
133         Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
134         h = Info_h.biHeight;
135
136         Info_h.biPlanes = getc(IN);
137         Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
138
139         Info_h.biBitCount = getc(IN);
140         Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
141
142         Info_h.biCompression = getc(IN);
143         Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
144         Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
145         Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
146
147         Info_h.biSizeImage = getc(IN);
148         Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
149         Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
150         Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
151
152         Info_h.biXpelsPerMeter = getc(IN);
153         Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
154         Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
155         Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
156
157         Info_h.biYpelsPerMeter = getc(IN);
158         Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
159         Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
160         Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
161
162         Info_h.biClrUsed = getc(IN);
163         Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
164         Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
165         Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
166
167         Info_h.biClrImportant = getc(IN);
168         Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
169         Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
170         Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
171
172         /* Read the data and store them in the OUT file */
173
174         if (Info_h.biBitCount == 24) {
175             img->x0 = Dim[0];
176             img->y0 = Dim[1];
177             img->x1 =
178                 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
179                                                                    1) *
180                 subsampling_dx + 1;
181             img->y1 =
182                 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
183                                                                    1) *
184                 subsampling_dy + 1;
185             img->numcomps = 3;
186             img->comps =
187                 (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
188             for (i = 0; i < img->numcomps; i++) {
189                 img->comps[i].prec = 8;
190                 img->comps[i].bpp = 8;
191                 img->comps[i].sgnd = 0;
192                 img->comps[i].dx = subsampling_dx;
193                 img->comps[i].dy = subsampling_dy;
194             }
195             Compo0 = fopen("Compo0", "wb");
196             if (!Compo0) {
197                 fprintf(stderr,
198                         "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
199             }
200             Compo1 = fopen("Compo1", "wb");
201             if (!Compo1) {
202                 fprintf(stderr,
203                         "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
204             }
205             Compo2 = fopen("Compo2", "wb");
206             if (!Compo2) {
207                 fprintf(stderr,
208                         "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
209             }
210
211             /* Place the cursor at the beginning of the image information */
212             fseek(IN, 0, SEEK_SET);
213             fseek(IN, File_h.bfOffBits, SEEK_SET);
214
215             W = Info_h.biWidth;
216             H = Info_h.biHeight;
217
218             // PAD = 4 - (3 * W) % 4;
219             // PAD = (PAD == 4) ? 0 : PAD;
220             PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
221
222
223             RGB =
224                 (unsigned char *) malloc((3 * W + PAD) * H *
225                                          sizeof(unsigned char));
226
227             fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
228
229             for (i = 0; i < (3 * W + PAD) * H; i++) {
230                 unsigned char elmt;
231                 int Wp = 3 * W + PAD;
232
233                 elmt = RGB[(H - (i / Wp + 1)) * Wp + i % Wp];
234                 if ((i % Wp) < (3 * W)) {
235                     switch (type) {
236                     case 0:
237                         fprintf(Compo2, "%c", elmt);
238                         type = 1;
239                         break;
240                     case 1:
241                         fprintf(Compo1, "%c", elmt);
242                         type = 2;
243                         break;
244                     case 2:
245                         fprintf(Compo0, "%c", elmt);
246                         type = 0;
247                         break;
248                     }
249                 }
250             }
251
252             fclose(Compo0);
253             fclose(Compo1);
254             fclose(Compo2);
255             free(RGB);
256         } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
257             img->x0 = Dim[0];
258             img->y0 = Dim[1];
259             img->x1 =
260                 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
261                                                                    1) *
262                 subsampling_dx + 1;
263             img->y1 =
264                 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
265                                                                    1) *
266                 subsampling_dy + 1;
267
268             table_R =
269                 (unsigned char *) malloc(256 * sizeof(unsigned char));
270             table_G =
271                 (unsigned char *) malloc(256 * sizeof(unsigned char));
272             table_B =
273                 (unsigned char *) malloc(256 * sizeof(unsigned char));
274
275             for (i = 0; i < Info_h.biClrUsed; i++) {
276                 table_B[i] = getc(IN);
277                 table_G[i] = getc(IN);
278                 table_R[i] = getc(IN);
279                 getc(IN);
280                 if (table_R[i] != table_G[i] && table_R[i] != table_B[i]
281                     && table_G[i] != table_B[i])
282                     gray_scale = 0;
283             }
284
285             /* Place the cursor at the beginning of the image information */
286             fseek(IN, 0, SEEK_SET);
287             fseek(IN, File_h.bfOffBits, SEEK_SET);
288
289             W = Info_h.biWidth;
290             H = Info_h.biHeight;
291             if (Info_h.biWidth % 2)
292                 W++;
293
294             RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
295
296             fread(RGB, sizeof(unsigned char), W * H, IN);
297             if (gray_scale) {
298                 img->numcomps = 1;
299                 img->comps =
300                     (j2k_comp_t *) malloc(img->numcomps *
301                                           sizeof(j2k_comp_t));
302                 img->comps[0].prec = 8;
303                 img->comps[0].bpp = 8;
304                 img->comps[0].sgnd = 0;
305                 img->comps[0].dx = subsampling_dx;
306                 img->comps[0].dy = subsampling_dy;
307                 Compo0 = fopen("Compo0", "wb");
308                 if (!Compo0) {
309                     fprintf(stderr,
310                             "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
311                 }
312                 for (i = 0; i < W * H; i++) {
313                     if ((i % W < W - 1 && Info_h.biWidth % 2)
314                         || !(Info_h.biWidth % 2))
315                         fprintf(Compo0, "%c",
316                                 table_R[RGB
317                                         [W * H - ((i) / (W) + 1) * W +
318                                          (i) % (W)]]);
319                 }
320                 fclose(Compo0);
321             } else {
322                 img->numcomps = 3;
323                 img->comps =
324                     (j2k_comp_t *) malloc(img->numcomps *
325                                           sizeof(j2k_comp_t));
326                 for (i = 0; i < img->numcomps; i++) {
327                     img->comps[i].prec = 8;
328                     img->comps[i].bpp = 8;
329                     img->comps[i].sgnd = 0;
330                     img->comps[i].dx = subsampling_dx;
331                     img->comps[i].dy = subsampling_dy;
332                 }
333
334                 Compo0 = fopen("Compo0", "wb");
335                 if (!Compo0) {
336                     fprintf(stderr,
337                             "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
338                 }
339                 Compo1 = fopen("Compo1", "wb");
340                 if (!Compo1) {
341                     fprintf(stderr,
342                             "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
343                 }
344                 Compo2 = fopen("Compo2", "wb");
345                 if (!Compo2) {
346                     fprintf(stderr,
347                             "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
348                 }
349
350                 for (i = 0; i < W * H; i++) {
351                     if ((i % W < W - 1 && Info_h.biWidth % 2)
352                         || !(Info_h.biWidth % 2)) {
353                         fprintf(Compo0, "%c",
354                                 table_R[RGB
355                                         [W * H - ((i) / (W) + 1) * W +
356                                          (i) % (W)]]);
357                         fprintf(Compo1, "%c",
358                                 table_G[RGB
359                                         [W * H - ((i) / (W) + 1) * W +
360                                          (i) % (W)]]);
361                         fprintf(Compo2, "%c",
362                                 table_B[RGB
363                                         [W * H - ((i) / (W) + 1) * W +
364                                          (i) % (W)]]);
365                     }
366
367                 }
368                 fclose(Compo0);
369                 fclose(Compo1);
370                 fclose(Compo2);
371             }
372             free(RGB);
373
374         } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
375             img->x0 = Dim[0];
376             img->y0 = Dim[1];
377             img->x1 =
378                 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
379                                                                    1) *
380                 subsampling_dx + 1;
381             img->y1 =
382                 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
383                                                                    1) *
384                 subsampling_dy + 1;
385
386             table_R =
387                 (unsigned char *) malloc(256 * sizeof(unsigned char));
388             table_G =
389                 (unsigned char *) malloc(256 * sizeof(unsigned char));
390             table_B =
391                 (unsigned char *) malloc(256 * sizeof(unsigned char));
392
393             for (i = 0; i < Info_h.biClrUsed; i++) {
394                 table_B[i] = getc(IN);
395                 table_G[i] = getc(IN);
396                 table_R[i] = getc(IN);
397                 getc(IN);
398                 if (table_R[i] != table_G[i] && table_R[i] != table_B[i]
399                     && table_G[i] != table_B[i])
400                     gray_scale = 0;
401             }
402
403             /* Place the cursor at the beginning of the image information */
404             fseek(IN, 0, SEEK_SET);
405             fseek(IN, File_h.bfOffBits, SEEK_SET);
406
407             if (gray_scale) {
408                 img->numcomps = 1;
409                 img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
410                 img->comps[0].prec = 8;
411                 img->comps[0].bpp = 8;
412                 img->comps[0].sgnd = 0;
413                 img->comps[0].dx = subsampling_dx;
414                 img->comps[0].dy = subsampling_dy;
415                 Compo0 = fopen("Compo0", "wb");
416                 if (!Compo0) {
417                     fprintf(stderr,
418                             "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
419                 }
420             } else {
421                 img->numcomps = 3;
422                 img->comps =
423                     (j2k_comp_t *) malloc(img->numcomps *
424                                           sizeof(j2k_comp_t));
425                 for (i = 0; i < img->numcomps; i++) {
426                     img->comps[i].prec = 8;
427                     img->comps[i].bpp = 8;
428                     img->comps[i].sgnd = 0;
429                     img->comps[i].dx = subsampling_dx;
430                     img->comps[i].dy = subsampling_dy;
431                 }
432                 Compo0 = fopen("Compo0", "wb");
433                 if (!Compo0) {
434                     fprintf(stderr,
435                             "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
436                 }
437                 Compo1 = fopen("Compo1", "wb");
438                 if (!Compo1) {
439                     fprintf(stderr,
440                             "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
441                 }
442                 Compo2 = fopen("Compo2", "wb");
443                 if (!Compo2) {
444                     fprintf(stderr,
445                             "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
446                 }
447             }
448
449             RGB =
450                 (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight *
451                                          sizeof(unsigned char));
452
453             while (not_end_file) {
454                 v = getc(IN);
455                 if (v) {
456                     v2 = getc(IN);
457                     for (i = 0; i < (int) v; i++) {
458                         RGB[line * Info_h.biWidth + col] = v2;
459                         col++;
460                     }
461                 } else {
462                     v = getc(IN);
463                     switch (v) {
464                     case 0:
465                         col = 0;
466                         line++;
467                         break;
468                     case 1:
469                         line++;
470                         not_end_file = 0;
471                         break;
472                     case 2:
473                         printf("No Delta supported\n");
474                         return 1;
475                         break;
476                     default:
477                         for (i = 0; i < v; i++) {
478                             v2 = getc(IN);
479                             RGB[line * Info_h.biWidth + col] = v2;
480                             col++;
481                         }
482                         if (v % 2)
483                             v2 = getc(IN);
484                     }
485                 }
486             }
487             if (gray_scale) {
488                 for (line = 0; line < Info_h.biHeight; line++)
489                     for (col = 0; col < Info_h.biWidth; col++)
490                         fprintf(Compo0, "%c",
491                                 table_R[(int)
492                                         RGB[(Info_h.biHeight - line -
493                                              1) * Info_h.biWidth + col]]);
494                 fclose(Compo0);
495             } else {
496                 for (line = 0; line < Info_h.biHeight; line++)
497                     for (col = 0; col < Info_h.biWidth; col++) {
498                         fprintf(Compo0, "%c",
499                                 table_R[(int)
500                                         RGB[(Info_h.biHeight - line -
501                                              1) * Info_h.biWidth + col]]);
502                         fprintf(Compo1, "%c",
503                                 table_G[(int)
504                                         RGB[(Info_h.biHeight - line -
505                                              1) * Info_h.biWidth + col]]);
506                         fprintf(Compo2, "%c",
507                                 table_B[(int)
508                                         RGB[(Info_h.biHeight - line -
509                                              1) * Info_h.biWidth + col]]);
510                     }
511                 fclose(Compo0);
512                 fclose(Compo1);
513                 fclose(Compo2);
514             }
515             free(RGB);
516         } else
517             fprintf(stderr,
518                     "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
519                     Info_h.biBitCount);
520
521         fclose(IN);
522         return 1;
523     }
524 }
525
526         /* -->> -->> -->> -->>
527
528            PGX IMAGE FORMAT
529
530            <<-- <<-- <<-- <<-- */
531
532
533 unsigned char readuchar(FILE * f)
534 {
535     unsigned char c1;
536     fread(&c1, 1, 1, f);
537     return c1;
538 }
539
540 unsigned short readushort(FILE * f, int bigendian)
541 {
542     unsigned char c1, c2;
543     fread(&c1, 1, 1, f);
544     fread(&c2, 1, 1, f);
545     if (bigendian)
546         return (c1 << 8) + c2;
547     else
548         return (c2 << 8) + c1;
549 }
550
551 unsigned int readuint(FILE * f, int bigendian)
552 {
553     unsigned char c1, c2, c3, c4;
554     fread(&c1, 1, 1, f);
555     fread(&c2, 1, 1, f);
556     fread(&c3, 1, 1, f);
557     fread(&c4, 1, 1, f);
558     if (bigendian)
559         return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
560     else
561         return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
562 }
563
564 int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
565                int subsampling_dx, int subsampling_dy, int Dim[2],
566                j2k_cp_t cp)
567 {
568     FILE *f;
569     int w, h, prec;
570     int i, compno, bandno;
571     char str[256], endian[16];
572     char sign;
573     int bigendian;
574     j2k_comp_t *comp;
575
576     img->numcomps = 1;
577     img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
578     for (compno = 0; compno < img->numcomps; compno++) {
579         FILE *src;
580         char tmp[16];
581         int max = 0;
582         int Y1;
583         comp = &img->comps[compno];
584         sprintf(str, "%s", filename);
585         f = fopen(str, "rb");
586         if (!f) {
587             fprintf(stderr, "Failed to open %s for reading !\n", str);
588             return 0;
589         }
590         if (fscanf(f, "PG %s %c %d %d %d", endian, &sign, &prec, &w, &h) ==
591             5) {
592             fgetc(f);
593             if (!strcmp(endian, "ML"))
594                 bigendian = 1;
595             else
596                 bigendian = 0;
597             if (compno == 0) {
598                 img->x0 = Dim[0];
599                 img->y0 = Dim[1];
600                 img->x1 =
601                     !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
602                                                                        1) *
603                     subsampling_dx + 1;
604                 img->y1 =
605                     !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
606                                                                        1) *
607                     subsampling_dy + 1;
608             } else {
609                 if (w != img->x1 || h != img->y1)
610                     return 0;
611             }
612
613             if (sign == '-') {
614                 comp->sgnd = 1;
615             } else {
616                 comp->sgnd = 0;
617             }
618             comp->prec = prec;
619             comp->dx = subsampling_dx;
620             comp->dy = subsampling_dy;
621             bandno = 1;
622
623             Y1 = cp.ty0 + bandno * cp.tdy <
624                 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
625             Y1 -= img->y0;
626
627             sprintf(tmp, "bandtile%d", bandno); /* bandtile file */
628             src = fopen(tmp, "wb");
629             if (!src) {
630                 fprintf(stderr, "failed to open %s for writing !\n", tmp);
631             }
632             for (i = 0; i < w * h; i++) {
633                 int v;
634                 if (i == Y1 * w / subsampling_dy && tdy != -1) {        /* bandtile is full */
635                     fclose(src);
636                     bandno++;
637                     sprintf(tmp, "bandtile%d", bandno);
638                     src = fopen(tmp, "wb");
639                     if (!src) {
640                         fprintf(stderr,
641                                 "failed to open %s for writing !\n", tmp);
642                     }
643                     Y1 = cp.ty0 + bandno * cp.tdy <
644                         img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
645                     Y1 -= img->y0;
646                 }
647                 if (comp->prec <= 8) {
648                     if (!comp->sgnd) {
649                         v = readuchar(f);
650                     } else {
651                         v = (char) readuchar(f);
652                     }
653                 } else if (comp->prec <= 16) {
654                     if (!comp->sgnd) {
655                         v = readushort(f, bigendian);
656                     } else {
657                         v = (short) readushort(f, bigendian);
658                     }
659                 } else {
660                     if (!comp->sgnd) {
661                         v = readuint(f, bigendian);
662                     } else {
663                         v = (int) readuint(f, bigendian);
664                     }
665                 }
666                 if (v > max)
667                     max = v;
668                 fprintf(src, "%d ", v);
669             }
670         } else {
671             return 0;
672         }
673         fclose(f);
674         fclose(src);
675         comp->bpp = int_floorlog2(max) + 1;
676     }
677     return 1;
678 }
679
680 /* -->> -->> -->> -->>
681
682   PNM IMAGE FORMAT
683
684  <<-- <<-- <<-- <<-- */
685
686 int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
687                int subsampling_dy, int Dim[2])
688 {
689     FILE *f;
690     FILE *Compo0, *Compo1, *Compo2;
691     int w, h;
692     int i;
693     char value;
694     char comment[256];
695
696     f = fopen(filename, "rb");
697     if (!f) {
698         fprintf(stderr,
699                 "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
700                 filename);
701         return 0;
702     }
703
704     if (fgetc(f) != 'P')
705         return 0;
706     value = fgetc(f);
707
708     if (value == '2') {
709         fgetc(f);
710         if (fgetc(f) == '#') {
711             fseek(f, 0, SEEK_SET);
712             fscanf(f, "P2\n");
713             fgets(comment, 256, f);
714             fscanf(f, "%d %d\n255", &w, &h);
715         } else {
716             fseek(f, 0, SEEK_SET);
717             fscanf(f, "P2\n%d %d\n255", &w, &h);
718         }
719
720         fgetc(f);
721         img->x0 = Dim[0];
722         img->y0 = Dim[1];
723         img->x1 =
724             !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
725                                                                1) *
726             subsampling_dx + 1;
727         img->y1 =
728             !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
729                                                                1) *
730             subsampling_dy + 1;
731
732         img->numcomps = 1;
733         img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
734         img->comps[0].prec = 8;
735         img->comps[0].bpp = 8;
736         img->comps[0].sgnd = 0;
737         img->comps[0].dx = subsampling_dx;
738         img->comps[0].dy = subsampling_dy;
739
740         Compo0 = fopen("Compo0", "wb");
741         if (!Compo0) {
742             fprintf(stderr,
743                     "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
744         }
745         for (i = 0; i < w * h; i++) {
746             unsigned int l;
747             fscanf(f, "%d", &l);
748             fprintf(Compo0, "%c", l);
749         }
750         fclose(Compo0);
751     } else if (value == '5') {
752         fgetc(f);
753         if (fgetc(f) == '#') {
754             fseek(f, 0, SEEK_SET);
755             fscanf(f, "P5\n");
756             fgets(comment, 256, f);
757             fscanf(f, "%d %d\n255", &w, &h);
758         } else {
759             fseek(f, 0, SEEK_SET);
760             fscanf(f, "P5\n%d %d\n255", &w, &h);
761         }
762
763         fgetc(f);
764         img->x0 = Dim[0];
765         img->y0 = Dim[1];
766         img->x1 =
767             !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
768                                                                1) *
769             subsampling_dx + 1;
770         img->y1 =
771             !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
772                                                                1) *
773             subsampling_dy + 1;
774
775         img->numcomps = 1;
776         img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
777         img->comps[0].prec = 8;
778         img->comps[0].bpp = 8;
779         img->comps[0].sgnd = 0;
780         img->comps[0].dx = subsampling_dx;
781         img->comps[0].dy = subsampling_dy;
782         Compo0 = fopen("Compo0", "wb");
783         if (!Compo0) {
784             fprintf(stderr,
785                     "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
786         }
787         for (i = 0; i < w * h; i++) {
788             unsigned char l;
789             fread(&l, 1, 1, f);
790             fwrite(&l, 1, 1, Compo0);
791         }
792         fclose(Compo0);
793     } else if (value == '3') {
794         fgetc(f);
795         if (fgetc(f) == '#') {
796             fseek(f, 0, SEEK_SET);
797             fscanf(f, "P3\n");
798             fgets(comment, 256, f);
799             fscanf(f, "%d %d\n255", &w, &h);
800         } else {
801             fseek(f, 0, SEEK_SET);
802             fscanf(f, "P3\n%d %d\n255", &w, &h);
803         }
804
805         fgetc(f);
806         img->x0 = Dim[0];
807         img->y0 = Dim[1];
808         img->x1 =
809             !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
810                                                                1) *
811             subsampling_dx + 1;
812         img->y1 =
813             !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
814                                                                1) *
815             subsampling_dy + 1;
816         img->numcomps = 3;
817         img->comps =
818             (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
819         for (i = 0; i < img->numcomps; i++) {
820             img->comps[i].prec = 8;
821             img->comps[i].bpp = 8;
822             img->comps[i].sgnd = 0;
823             img->comps[i].dx = subsampling_dx;
824             img->comps[i].dy = subsampling_dy;
825         }
826         Compo0 = fopen("Compo0", "wb");
827         if (!Compo0) {
828             fprintf(stderr,
829                     "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
830         }
831
832         Compo1 = fopen("Compo1", "wb");
833         if (!Compo1) {
834             fprintf(stderr,
835                     "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
836         }
837
838         Compo2 = fopen("Compo2", "wb");
839         if (!Compo2) {
840             fprintf(stderr,
841                     "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
842         }
843
844         for (i = 0; i < w * h; i++) {
845             unsigned int r, g, b;
846             fscanf(f, "%d", &r);
847             fscanf(f, "%d", &g);
848             fscanf(f, "%d", &b);
849             fprintf(Compo0, "%c", r);
850             fprintf(Compo1, "%c", g);
851             fprintf(Compo2, "%c", b);
852         }
853         fclose(Compo0);
854         fclose(Compo1);
855         fclose(Compo2);
856     } else if (value == '6') {
857         fgetc(f);
858         if (fgetc(f) == '#') {
859             fseek(f, 0, SEEK_SET);
860             fscanf(f, "P6\n");
861             fgets(comment, 256, f);
862             fscanf(f, "%d %d\n255", &w, &h);
863         } else {
864             fseek(f, 0, SEEK_SET);
865             fscanf(f, "P6\n%d %d\n255", &w, &h);
866         }
867
868         fgetc(f);
869         img->x0 = Dim[0];
870         img->y0 = Dim[1];
871         img->x1 =
872             !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
873                                                                1) *
874             subsampling_dx + 1;
875         img->y1 =
876             !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
877                                                                1) *
878             subsampling_dy + 1;
879         img->numcomps = 3;
880         img->comps =
881             (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
882         for (i = 0; i < img->numcomps; i++) {
883             img->comps[i].prec = 8;
884             img->comps[i].bpp = 8;
885             img->comps[i].sgnd = 0;
886             img->comps[i].dx = subsampling_dx;
887             img->comps[i].dy = subsampling_dy;
888         }
889         Compo0 = fopen("Compo0", "wb");
890         if (!Compo0) {
891             fprintf(stderr,
892                     "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
893         }
894
895         Compo1 = fopen("Compo1", "wb");
896         if (!Compo1) {
897             fprintf(stderr,
898                     "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
899         }
900
901         Compo2 = fopen("Compo2", "wb");
902         if (!Compo2) {
903             fprintf(stderr,
904                     "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
905         }
906
907         for (i = 0; i < w * h; i++) {
908             unsigned char r, g, b;
909             fread(&r, 1, 1, f);
910             fread(&g, 1, 1, f);
911             fread(&b, 1, 1, f);
912             fwrite(&r, 1, 1, Compo0);
913             fwrite(&g, 1, 1, Compo1);
914             fwrite(&b, 1, 1, Compo2);
915         }
916         fclose(Compo0);
917         fclose(Compo1);
918         fclose(Compo2);
919     } else {
920         return 0;
921     }
922     fclose(f);
923     return 1;
924 }