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