Various corrections to avoid "signed/unsigned mismatch" warnings during compilation
[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   unsigned int j, w, h, PAD, type = 0;
79   int i;
80   int gray_scale = 1, not_end_file = 1; 
81   unsigned int line = 0, col = 0;
82   unsigned char v, v2;
83   UINT4 W, H;
84
85   IN = fopen(filename, "rb");
86   if (!IN) {
87     fprintf(stderr,
88             "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
89             filename);
90     return 0;
91   }
92
93   File_h.bfType = getc(IN);
94   File_h.bfType = (getc(IN) << 8) + File_h.bfType;
95
96   if (File_h.bfType != 19778) {
97     printf("Error, not a BMP file!\n");
98     return 0;
99   } else {
100     /* FILE HEADER */
101     /* ------------- */
102     File_h.bfSize = getc(IN);
103     File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
104     File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
105     File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
106
107     File_h.bfReserved1 = getc(IN);
108     File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
109
110     File_h.bfReserved2 = getc(IN);
111     File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
112
113     File_h.bfOffBits = getc(IN);
114     File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
115     File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
116     File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
117
118     /* INFO HEADER */
119     /* ------------- */
120
121     Info_h.biSize = getc(IN);
122     Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
123     Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
124     Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
125
126     Info_h.biWidth = getc(IN);
127     Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
128     Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
129     Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
130     w = Info_h.biWidth;
131
132     Info_h.biHeight = getc(IN);
133     Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
134     Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
135     Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
136     h = Info_h.biHeight;
137
138     Info_h.biPlanes = getc(IN);
139     Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
140
141     Info_h.biBitCount = getc(IN);
142     Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
143
144     Info_h.biCompression = getc(IN);
145     Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
146     Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
147     Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
148
149     Info_h.biSizeImage = getc(IN);
150     Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
151     Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
152     Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
153
154     Info_h.biXpelsPerMeter = getc(IN);
155     Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
156     Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
157     Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
158
159     Info_h.biYpelsPerMeter = getc(IN);
160     Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
161     Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
162     Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
163
164     Info_h.biClrUsed = getc(IN);
165     Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
166     Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
167     Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
168
169     Info_h.biClrImportant = getc(IN);
170     Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
171     Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
172     Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
173
174     /* Read the data and store them in the OUT file */
175
176     if (Info_h.biBitCount == 24) {
177       img->x0 = Dim[0];
178       img->y0 = Dim[1];
179       img->x1 =
180         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
181                                                            1) *
182         subsampling_dx + 1;
183       img->y1 =
184         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
185                                                            1) *
186         subsampling_dy + 1;
187       img->numcomps = 3;
188       img->comps =
189         (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
190       for (i = 0; i < img->numcomps; i++) {
191         img->comps[i].prec = 8;
192         img->comps[i].bpp = 8;
193         img->comps[i].sgnd = 0;
194         img->comps[i].dx = subsampling_dx;
195         img->comps[i].dy = subsampling_dy;
196       }
197       Compo0 = fopen("Compo0", "wb");
198       if (!Compo0) {
199         fprintf(stderr,
200                 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
201       }
202       Compo1 = fopen("Compo1", "wb");
203       if (!Compo1) {
204         fprintf(stderr,
205                 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
206       }
207       Compo2 = fopen("Compo2", "wb");
208       if (!Compo2) {
209         fprintf(stderr,
210                 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
211       }
212
213       /* Place the cursor at the beginning of the image information */
214       fseek(IN, 0, SEEK_SET);
215       fseek(IN, File_h.bfOffBits, SEEK_SET);
216
217       W = Info_h.biWidth;
218       H = Info_h.biHeight;
219
220       // PAD = 4 - (3 * W) % 4;
221       // PAD = (PAD == 4) ? 0 : PAD;
222       PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
223
224
225       RGB =
226         (unsigned char *) malloc((3 * W + PAD) * H *
227                                  sizeof(unsigned char));
228
229       fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
230
231       for (j = 0; j < (3 * W + PAD) * H; j++) {
232         unsigned char elmt;
233         int Wp = 3 * W + PAD;
234
235         elmt = RGB[(H - (j / Wp + 1)) * Wp + j % Wp];
236         if ((j % Wp) < (3 * W)) {
237           switch (type) {
238           case 0:
239             fprintf(Compo2, "%c", elmt);
240             type = 1;
241             break;
242           case 1:
243             fprintf(Compo1, "%c", elmt);
244             type = 2;
245             break;
246           case 2:
247             fprintf(Compo0, "%c", elmt);
248             type = 0;
249             break;
250           }
251         }
252       }
253
254       fclose(Compo0);
255       fclose(Compo1);
256       fclose(Compo2);
257       free(RGB);
258     } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
259       img->x0 = Dim[0];
260       img->y0 = Dim[1];
261       img->x1 =
262         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
263                                                            1) *
264         subsampling_dx + 1;
265       img->y1 =
266         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
267                                                            1) *
268         subsampling_dy + 1;
269
270       table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
271       table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
272       table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
273
274       for (j = 0; j < Info_h.biClrUsed; j++) {
275         table_B[j] = getc(IN);
276         table_G[j] = getc(IN);
277         table_R[j] = getc(IN);
278         getc(IN);
279         if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
280             && table_G[j] != table_B[j])
281           gray_scale = 0;
282       }
283
284       /* Place the cursor at the beginning of the image information */
285       fseek(IN, 0, SEEK_SET);
286       fseek(IN, File_h.bfOffBits, SEEK_SET);
287
288       W = Info_h.biWidth;
289       H = Info_h.biHeight;
290       if (Info_h.biWidth % 2)
291         W++;
292
293       RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
294
295       fread(RGB, sizeof(unsigned char), W * H, IN);
296       if (gray_scale) {
297         img->numcomps = 1;
298         img->comps =
299           (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
300         img->comps[0].prec = 8;
301         img->comps[0].bpp = 8;
302         img->comps[0].sgnd = 0;
303         img->comps[0].dx = subsampling_dx;
304         img->comps[0].dy = subsampling_dy;
305         Compo0 = fopen("Compo0", "wb");
306         if (!Compo0) {
307           fprintf(stderr,
308                   "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
309         }
310         for (j = 0; j < W * H; j++) {
311           if ((j % W < W - 1 && Info_h.biWidth % 2)
312               || !(Info_h.biWidth % 2))
313             fprintf(Compo0, "%c",
314                     table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
315         }
316         fclose(Compo0);
317       } else {
318         img->numcomps = 3;
319         img->comps =
320           (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
321         for (i = 0; i < img->numcomps; i++) {
322           img->comps[i].prec = 8;
323           img->comps[i].bpp = 8;
324           img->comps[i].sgnd = 0;
325           img->comps[i].dx = subsampling_dx;
326           img->comps[i].dy = subsampling_dy;
327         }
328
329         Compo0 = fopen("Compo0", "wb");
330         if (!Compo0) {
331           fprintf(stderr,
332                   "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
333         }
334         Compo1 = fopen("Compo1", "wb");
335         if (!Compo1) {
336           fprintf(stderr,
337                   "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
338         }
339         Compo2 = fopen("Compo2", "wb");
340         if (!Compo2) {
341           fprintf(stderr,
342                   "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
343         }
344
345         for (j = 0; j < W * H; j++) {
346           if ((j % W < W - 1 && Info_h.biWidth % 2)
347               || !(Info_h.biWidth % 2)) {
348             fprintf(Compo0, "%c",
349                     table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
350             fprintf(Compo1, "%c",
351                     table_G[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
352             fprintf(Compo2, "%c",
353                     table_B[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
354           }
355
356         }
357         fclose(Compo0);
358         fclose(Compo1);
359         fclose(Compo2);
360       }
361       free(RGB);
362
363     } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
364       img->x0 = Dim[0];
365       img->y0 = Dim[1];
366       img->x1 =
367         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
368                                                            1) *
369         subsampling_dx + 1;
370       img->y1 =
371         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
372                                                            1) *
373         subsampling_dy + 1;
374
375       table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
376       table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
377       table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
378
379       for (j = 0; j < Info_h.biClrUsed; j++) {
380         table_B[j] = getc(IN);
381         table_G[j] = getc(IN);
382         table_R[j] = getc(IN);
383         getc(IN);
384         if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
385             && table_G[j] != table_B[j])
386           gray_scale = 0;
387       }
388
389       /* Place the cursor at the beginning of the image information */
390       fseek(IN, 0, SEEK_SET);
391       fseek(IN, File_h.bfOffBits, SEEK_SET);
392
393       if (gray_scale) {
394         img->numcomps = 1;
395         img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
396         img->comps[0].prec = 8;
397         img->comps[0].bpp = 8;
398         img->comps[0].sgnd = 0;
399         img->comps[0].dx = subsampling_dx;
400         img->comps[0].dy = subsampling_dy;
401         Compo0 = fopen("Compo0", "wb");
402         if (!Compo0) {
403           fprintf(stderr,
404                   "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
405         }
406       } else {
407         img->numcomps = 3;
408         img->comps =
409           (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
410         for (i = 0; i < img->numcomps; i++) {
411           img->comps[i].prec = 8;
412           img->comps[i].bpp = 8;
413           img->comps[i].sgnd = 0;
414           img->comps[i].dx = subsampling_dx;
415           img->comps[i].dy = subsampling_dy;
416         }
417         Compo0 = fopen("Compo0", "wb");
418         if (!Compo0) {
419           fprintf(stderr,
420                   "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
421         }
422         Compo1 = fopen("Compo1", "wb");
423         if (!Compo1) {
424           fprintf(stderr,
425                   "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
426         }
427         Compo2 = fopen("Compo2", "wb");
428         if (!Compo2) {
429           fprintf(stderr,
430                   "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
431         }
432       }
433
434       RGB =
435         (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight *
436                                  sizeof(unsigned char));
437
438       while (not_end_file) {
439         v = getc(IN);
440         if (v) {
441           v2 = getc(IN);
442           for (i = 0; i < (int) v; i++) {
443             RGB[line * Info_h.biWidth + col] = v2;
444             col++;
445           }
446         } else {
447           v = getc(IN);
448           switch (v) {
449           case 0:
450             col = 0;
451             line++;
452             break;
453           case 1:
454             line++;
455             not_end_file = 0;
456             break;
457           case 2:
458             printf("No Delta supported\n");
459             return 1;
460             break;
461           default:
462             for (i = 0; i < v; i++) {
463               v2 = getc(IN);
464               RGB[line * Info_h.biWidth + col] = v2;
465               col++;
466             }
467             if (v % 2)
468               v2 = getc(IN);
469           }
470         }
471       }
472       if (gray_scale) {
473         for (line = 0; line < Info_h.biHeight; line++)
474           for (col = 0; col < Info_h.biWidth; col++)
475             fprintf(Compo0, "%c", table_R[(int)
476                                           RGB[(Info_h.biHeight - line -
477                                                1) * Info_h.biWidth +
478                                               col]]);
479         fclose(Compo0);
480       } else {
481         for (line = 0; line < Info_h.biHeight; line++)
482           for (col = 0; col < Info_h.biWidth; col++) {
483             fprintf(Compo0, "%c", table_R[(int)
484                                           RGB[(Info_h.biHeight - line -
485                                                1) * Info_h.biWidth +
486                                               col]]);
487             fprintf(Compo1, "%c", table_G[(int)
488                                           RGB[(Info_h.biHeight - line -
489                                                1) * Info_h.biWidth +
490                                               col]]);
491             fprintf(Compo2, "%c", table_B[(int)
492                                           RGB[(Info_h.biHeight - line -
493                                                1) * Info_h.biWidth +
494                                               col]]);
495           }
496         fclose(Compo0);
497         fclose(Compo1);
498         fclose(Compo2);
499       }
500       free(RGB);
501     } else
502       fprintf(stderr,
503               "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
504               Info_h.biBitCount);
505
506     fclose(IN);
507     return 1;
508   }
509 }
510
511         /* -->> -->> -->> -->>
512
513            PGX IMAGE FORMAT
514
515            <<-- <<-- <<-- <<-- */
516
517
518 unsigned char readuchar(FILE * f)
519 {
520   unsigned char c1;
521   fread(&c1, 1, 1, f);
522   return c1;
523 }
524
525 unsigned short readushort(FILE * f, int bigendian)
526 {
527   unsigned char c1, c2;
528   fread(&c1, 1, 1, f);
529   fread(&c2, 1, 1, f);
530   if (bigendian)
531     return (c1 << 8) + c2;
532   else
533     return (c2 << 8) + c1;
534 }
535
536 unsigned int readuint(FILE * f, int bigendian)
537 {
538   unsigned char c1, c2, c3, c4;
539   fread(&c1, 1, 1, f);
540   fread(&c2, 1, 1, f);
541   fread(&c3, 1, 1, f);
542   fread(&c4, 1, 1, f);
543   if (bigendian)
544     return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
545   else
546     return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
547 }
548
549 int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
550                int subsampling_dx, int subsampling_dy, int Dim[2],
551                j2k_cp_t cp)
552 {
553   FILE *f;
554   int w, h, prec;
555   int i, compno, bandno;
556   char str[256], endian[16];
557   char sign;
558   int bigendian;
559   j2k_comp_t *comp;
560
561   img->numcomps = 1;
562   img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
563   for (compno = 0; compno < img->numcomps; compno++) {
564     FILE *src;
565     char tmp[16];
566     int max = 0;
567     int Y1;
568     comp = &img->comps[compno];
569     sprintf(str, "%s", filename);
570     f = fopen(str, "rb");
571     if (!f) {
572       fprintf(stderr, "Failed to open %s for reading !\n", str);
573       return 0;
574     }
575     if (fscanf(f, "PG %s %c %d %d %d", endian, &sign, &prec, &w, &h) == 5) {
576       fgetc(f);
577       if (!strcmp(endian, "ML"))
578         bigendian = 1;
579       else
580         bigendian = 0;
581       if (compno == 0) {
582         img->x0 = Dim[0];
583         img->y0 = Dim[1];
584         img->x1 =
585           !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
586                                                              1) *
587           subsampling_dx + 1;
588         img->y1 =
589           !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
590                                                              1) *
591           subsampling_dy + 1;
592       } else {
593         if (w != img->x1 || h != img->y1)
594           return 0;
595       }
596
597       if (sign == '-') {
598         comp->sgnd = 1;
599       } else {
600         comp->sgnd = 0;
601       }
602       comp->prec = prec;
603       comp->dx = subsampling_dx;
604       comp->dy = subsampling_dy;
605       bandno = 1;
606
607       Y1 = cp.ty0 + bandno * cp.tdy <
608         img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
609       Y1 -= img->y0;
610
611       sprintf(tmp, "bandtile%d", bandno);       /* bandtile file */
612       src = fopen(tmp, "wb");
613       if (!src) {
614         fprintf(stderr, "failed to open %s for writing !\n", tmp);
615       }
616       for (i = 0; i < w * h; i++) {
617         int v;
618         if (i == Y1 * w / subsampling_dy && tdy != -1) {        /* bandtile is full */
619           fclose(src);
620           bandno++;
621           sprintf(tmp, "bandtile%d", bandno);
622           src = fopen(tmp, "wb");
623           if (!src) {
624             fprintf(stderr, "failed to open %s for writing !\n", tmp);
625           }
626           Y1 = cp.ty0 + bandno * cp.tdy <
627             img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
628           Y1 -= img->y0;
629         }
630         if (comp->prec <= 8) {
631           if (!comp->sgnd) {
632             v = readuchar(f);
633           } else {
634             v = (char) readuchar(f);
635           }
636         } else if (comp->prec <= 16) {
637           if (!comp->sgnd) {
638             v = readushort(f, bigendian);
639           } else {
640             v = (short) readushort(f, bigendian);
641           }
642         } else {
643           if (!comp->sgnd) {
644             v = readuint(f, bigendian);
645           } else {
646             v = (int) readuint(f, bigendian);
647           }
648         }
649         if (v > max)
650           max = v;
651         fprintf(src, "%d ", v);
652       }
653     } else {
654       return 0;
655     }
656     fclose(f);
657     fclose(src);
658     comp->bpp = int_floorlog2(max) + 1;
659   }
660   return 1;
661 }
662
663 /* -->> -->> -->> -->>
664
665   PNM IMAGE FORMAT
666
667  <<-- <<-- <<-- <<-- */
668
669 int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
670                int subsampling_dy, int Dim[2])
671 {
672   FILE *f;
673   FILE *Compo0, *Compo1, *Compo2;
674   int w, h;
675   int i;
676   char value;
677   char comment[256];
678
679   f = fopen(filename, "rb");
680   if (!f) {
681     fprintf(stderr,
682             "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
683             filename);
684     return 0;
685   }
686
687   if (fgetc(f) != 'P')
688     return 0;
689   value = fgetc(f);
690
691   if (value == '2') {
692     fgetc(f);
693     if (fgetc(f) == '#') {
694       fseek(f, 0, SEEK_SET);
695       fscanf(f, "P2\n");
696       fgets(comment, 256, f);
697       fscanf(f, "%d %d\n255", &w, &h);
698     } else {
699       fseek(f, 0, SEEK_SET);
700       fscanf(f, "P2\n%d %d\n255", &w, &h);
701     }
702
703     fgetc(f);
704     img->x0 = Dim[0];
705     img->y0 = Dim[1];
706     img->x1 =
707       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
708                                                          1) *
709       subsampling_dx + 1;
710     img->y1 =
711       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
712                                                          1) *
713       subsampling_dy + 1;
714
715     img->numcomps = 1;
716     img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
717     img->comps[0].prec = 8;
718     img->comps[0].bpp = 8;
719     img->comps[0].sgnd = 0;
720     img->comps[0].dx = subsampling_dx;
721     img->comps[0].dy = subsampling_dy;
722
723     Compo0 = fopen("Compo0", "wb");
724     if (!Compo0) {
725       fprintf(stderr,
726               "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
727     }
728     for (i = 0; i < w * h; i++) {
729       unsigned int l;
730       fscanf(f, "%d", &l);
731       fprintf(Compo0, "%c", l);
732     }
733     fclose(Compo0);
734   } else if (value == '5') {
735     fgetc(f);
736     if (fgetc(f) == '#') {
737       fseek(f, 0, SEEK_SET);
738       fscanf(f, "P5\n");
739       fgets(comment, 256, f);
740       fscanf(f, "%d %d\n255", &w, &h);
741     } else {
742       fseek(f, 0, SEEK_SET);
743       fscanf(f, "P5\n%d %d\n255", &w, &h);
744     }
745
746     fgetc(f);
747     img->x0 = Dim[0];
748     img->y0 = Dim[1];
749     img->x1 =
750       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
751                                                          1) *
752       subsampling_dx + 1;
753     img->y1 =
754       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
755                                                          1) *
756       subsampling_dy + 1;
757
758     img->numcomps = 1;
759     img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
760     img->comps[0].prec = 8;
761     img->comps[0].bpp = 8;
762     img->comps[0].sgnd = 0;
763     img->comps[0].dx = subsampling_dx;
764     img->comps[0].dy = subsampling_dy;
765     Compo0 = fopen("Compo0", "wb");
766     if (!Compo0) {
767       fprintf(stderr,
768               "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
769     }
770     for (i = 0; i < w * h; i++) {
771       unsigned char l;
772       fread(&l, 1, 1, f);
773       fwrite(&l, 1, 1, Compo0);
774     }
775     fclose(Compo0);
776   } else if (value == '3') {
777     fgetc(f);
778     if (fgetc(f) == '#') {
779       fseek(f, 0, SEEK_SET);
780       fscanf(f, "P3\n");
781       fgets(comment, 256, f);
782       fscanf(f, "%d %d\n255", &w, &h);
783     } else {
784       fseek(f, 0, SEEK_SET);
785       fscanf(f, "P3\n%d %d\n255", &w, &h);
786     }
787
788     fgetc(f);
789     img->x0 = Dim[0];
790     img->y0 = Dim[1];
791     img->x1 =
792       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
793                                                          1) *
794       subsampling_dx + 1;
795     img->y1 =
796       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
797                                                          1) *
798       subsampling_dy + 1;
799     img->numcomps = 3;
800     img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
801     for (i = 0; i < img->numcomps; i++) {
802       img->comps[i].prec = 8;
803       img->comps[i].bpp = 8;
804       img->comps[i].sgnd = 0;
805       img->comps[i].dx = subsampling_dx;
806       img->comps[i].dy = subsampling_dy;
807     }
808     Compo0 = fopen("Compo0", "wb");
809     if (!Compo0) {
810       fprintf(stderr,
811               "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
812     }
813
814     Compo1 = fopen("Compo1", "wb");
815     if (!Compo1) {
816       fprintf(stderr,
817               "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
818     }
819
820     Compo2 = fopen("Compo2", "wb");
821     if (!Compo2) {
822       fprintf(stderr,
823               "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
824     }
825
826     for (i = 0; i < w * h; i++) {
827       unsigned int r, g, b;
828       fscanf(f, "%d", &r);
829       fscanf(f, "%d", &g);
830       fscanf(f, "%d", &b);
831       fprintf(Compo0, "%c", r);
832       fprintf(Compo1, "%c", g);
833       fprintf(Compo2, "%c", b);
834     }
835     fclose(Compo0);
836     fclose(Compo1);
837     fclose(Compo2);
838   } else if (value == '6') {
839     fgetc(f);
840     if (fgetc(f) == '#') {
841       fseek(f, 0, SEEK_SET);
842       fscanf(f, "P6\n");
843       fgets(comment, 256, f);
844       fscanf(f, "%d %d\n255", &w, &h);
845     } else {
846       fseek(f, 0, SEEK_SET);
847       fscanf(f, "P6\n%d %d\n255", &w, &h);
848     }
849
850     fgetc(f);
851     img->x0 = Dim[0];
852     img->y0 = Dim[1];
853     img->x1 =
854       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
855                                                          1) *
856       subsampling_dx + 1;
857     img->y1 =
858       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
859                                                          1) *
860       subsampling_dy + 1;
861     img->numcomps = 3;
862     img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
863     for (i = 0; i < img->numcomps; i++) {
864       img->comps[i].prec = 8;
865       img->comps[i].bpp = 8;
866       img->comps[i].sgnd = 0;
867       img->comps[i].dx = subsampling_dx;
868       img->comps[i].dy = subsampling_dy;
869     }
870     Compo0 = fopen("Compo0", "wb");
871     if (!Compo0) {
872       fprintf(stderr,
873               "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
874     }
875
876     Compo1 = fopen("Compo1", "wb");
877     if (!Compo1) {
878       fprintf(stderr,
879               "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
880     }
881
882     Compo2 = fopen("Compo2", "wb");
883     if (!Compo2) {
884       fprintf(stderr,
885               "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
886     }
887
888     for (i = 0; i < w * h; i++) {
889       unsigned char r, g, b;
890       fread(&r, 1, 1, f);
891       fread(&g, 1, 1, f);
892       fread(&b, 1, 1, f);
893       fwrite(&r, 1, 1, Compo0);
894       fwrite(&g, 1, 1, Compo1);
895       fwrite(&b, 1, 1, Compo2);
896     }
897     fclose(Compo0);
898     fclose(Compo1);
899     fclose(Compo2);
900   } else {
901     return 0;
902   }
903   fclose(f);
904   return 1;
905 }