Removed unused parameter in fprintf at line 618
[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
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 *
230                                  sizeof(unsigned char));
231
232       fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
233
234       for (j = 0; j < (3 * W + PAD) * H; j++) {
235         unsigned char elmt;
236         int Wp = 3 * W + PAD;
237
238         elmt = RGB[(H - (j / Wp + 1)) * Wp + j % Wp];
239         if ((j % Wp) < (3 * W)) {
240           switch (type) {
241           case 0:
242             fprintf(Compo2, "%c", elmt);
243             type = 1;
244             break;
245           case 1:
246             fprintf(Compo1, "%c", elmt);
247             type = 2;
248             break;
249           case 2:
250             fprintf(Compo0, "%c", elmt);
251             type = 0;
252             break;
253           }
254         }
255       }
256
257       fclose(Compo0);
258       fclose(Compo1);
259       fclose(Compo2);
260       free(RGB);
261     } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
262       img->x0 = Dim[0];
263       img->y0 = Dim[1];
264       img->x1 =
265         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
266                                                            1) *
267         subsampling_dx + 1;
268       img->y1 =
269         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
270                                                            1) *
271         subsampling_dy + 1;
272
273       table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
274       table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
275       table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
276
277       for (j = 0; j < Info_h.biClrUsed; j++) {
278         table_B[j] = getc(IN);
279         table_G[j] = getc(IN);
280         table_R[j] = getc(IN);
281         getc(IN);
282         if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
283             && table_G[j] != table_B[j])
284           gray_scale = 0;
285       }
286
287       /* Place the cursor at the beginning of the image information */
288       fseek(IN, 0, SEEK_SET);
289       fseek(IN, File_h.bfOffBits, SEEK_SET);
290
291       W = Info_h.biWidth;
292       H = Info_h.biHeight;
293       if (Info_h.biWidth % 2)
294         W++;
295
296       RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
297
298       fread(RGB, sizeof(unsigned char), W * H, IN);
299       if (gray_scale) {
300         img->numcomps = 1;
301         img->comps =
302           (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
303         img->comps[0].prec = 8;
304         img->comps[0].bpp = 8;
305         img->comps[0].sgnd = 0;
306         img->comps[0].dx = subsampling_dx;
307         img->comps[0].dy = subsampling_dy;
308         Compo0 = fopen("Compo0", "wb");
309         if (!Compo0) {
310           fprintf(stderr,
311                   "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
312         }
313         for (j = 0; j < W * H; j++) {
314           if ((j % W < W - 1 && Info_h.biWidth % 2)
315               || !(Info_h.biWidth % 2))
316             fprintf(Compo0, "%c",
317                     table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
318         }
319         fclose(Compo0);
320       } else {
321         img->numcomps = 3;
322         img->comps =
323           (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
324         for (i = 0; i < img->numcomps; i++) {
325           img->comps[i].prec = 8;
326           img->comps[i].bpp = 8;
327           img->comps[i].sgnd = 0;
328           img->comps[i].dx = subsampling_dx;
329           img->comps[i].dy = subsampling_dy;
330         }
331
332         Compo0 = fopen("Compo0", "wb");
333         if (!Compo0) {
334           fprintf(stderr,
335                   "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
336         }
337         Compo1 = fopen("Compo1", "wb");
338         if (!Compo1) {
339           fprintf(stderr,
340                   "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
341         }
342         Compo2 = fopen("Compo2", "wb");
343         if (!Compo2) {
344           fprintf(stderr,
345                   "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
346         }
347
348         for (j = 0; j < W * H; j++) {
349           if ((j % W < W - 1 && Info_h.biWidth % 2)
350               || !(Info_h.biWidth % 2)) {
351             fprintf(Compo0, "%c",
352                     table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
353             fprintf(Compo1, "%c",
354                     table_G[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
355             fprintf(Compo2, "%c",
356                     table_B[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
357           }
358
359         }
360         fclose(Compo0);
361         fclose(Compo1);
362         fclose(Compo2);
363       }
364       free(RGB);
365
366     } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
367       img->x0 = Dim[0];
368       img->y0 = Dim[1];
369       img->x1 =
370         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
371                                                            1) *
372         subsampling_dx + 1;
373       img->y1 =
374         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
375                                                            1) *
376         subsampling_dy + 1;
377
378       table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
379       table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
380       table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
381
382       for (j = 0; j < Info_h.biClrUsed; j++) {
383         table_B[j] = getc(IN);
384         table_G[j] = getc(IN);
385         table_R[j] = getc(IN);
386         getc(IN);
387         if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
388             && table_G[j] != table_B[j])
389           gray_scale = 0;
390       }
391
392       /* Place the cursor at the beginning of the image information */
393       fseek(IN, 0, SEEK_SET);
394       fseek(IN, File_h.bfOffBits, SEEK_SET);
395
396       if (gray_scale) {
397         img->numcomps = 1;
398         img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
399         img->comps[0].prec = 8;
400         img->comps[0].bpp = 8;
401         img->comps[0].sgnd = 0;
402         img->comps[0].dx = subsampling_dx;
403         img->comps[0].dy = subsampling_dy;
404         Compo0 = fopen("Compo0", "wb");
405         if (!Compo0) {
406           fprintf(stderr,
407                   "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
408         }
409       } else {
410         img->numcomps = 3;
411         img->comps =
412           (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
413         for (i = 0; i < img->numcomps; i++) {
414           img->comps[i].prec = 8;
415           img->comps[i].bpp = 8;
416           img->comps[i].sgnd = 0;
417           img->comps[i].dx = subsampling_dx;
418           img->comps[i].dy = subsampling_dy;
419         }
420         Compo0 = fopen("Compo0", "wb");
421         if (!Compo0) {
422           fprintf(stderr,
423                   "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
424         }
425         Compo1 = fopen("Compo1", "wb");
426         if (!Compo1) {
427           fprintf(stderr,
428                   "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
429         }
430         Compo2 = fopen("Compo2", "wb");
431         if (!Compo2) {
432           fprintf(stderr,
433                   "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
434         }
435       }
436
437       RGB =
438         (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight *
439                                  sizeof(unsigned char));
440
441       while (not_end_file) {
442         v = getc(IN);
443         if (v) {
444           v2 = getc(IN);
445           for (i = 0; i < (int) v; i++) {
446             RGB[line * Info_h.biWidth + col] = v2;
447             col++;
448           }
449         } else {
450           v = getc(IN);
451           switch (v) {
452           case 0:
453             col = 0;
454             line++;
455             break;
456           case 1:
457             line++;
458             not_end_file = 0;
459             break;
460           case 2:
461             fprintf(stderr,"No Delta supported\n");
462             return 1;
463             break;
464           default:
465             for (i = 0; i < v; i++) {
466               v2 = getc(IN);
467               RGB[line * Info_h.biWidth + col] = v2;
468               col++;
469             }
470             if (v % 2)
471               v2 = getc(IN);
472           }
473         }
474       }
475       if (gray_scale) {
476         for (line = 0; line < Info_h.biHeight; line++)
477           for (col = 0; col < Info_h.biWidth; col++)
478             fprintf(Compo0, "%c", table_R[(int)
479                                           RGB[(Info_h.biHeight - line -
480                                                1) * Info_h.biWidth +
481                                               col]]);
482         fclose(Compo0);
483       } else {
484         for (line = 0; line < Info_h.biHeight; line++)
485           for (col = 0; col < Info_h.biWidth; col++) {
486             fprintf(Compo0, "%c", table_R[(int)
487                                           RGB[(Info_h.biHeight - line -
488                                                1) * Info_h.biWidth +
489                                               col]]);
490             fprintf(Compo1, "%c", table_G[(int)
491                                           RGB[(Info_h.biHeight - line -
492                                                1) * Info_h.biWidth +
493                                               col]]);
494             fprintf(Compo2, "%c", table_B[(int)
495                                           RGB[(Info_h.biHeight - line -
496                                                1) * Info_h.biWidth +
497                                               col]]);
498           }
499         fclose(Compo0);
500         fclose(Compo1);
501         fclose(Compo2);
502       }
503       free(RGB);
504     } else
505       fprintf(stderr,
506               "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
507               Info_h.biBitCount);
508
509     fclose(IN);
510     return 1;
511   }
512 }
513
514         /* -->> -->> -->> -->>
515
516            PGX IMAGE FORMAT
517
518            <<-- <<-- <<-- <<-- */
519
520
521 unsigned char readuchar(FILE * f)
522 {
523   unsigned char c1;
524   fread(&c1, 1, 1, f);
525   return c1;
526 }
527
528 unsigned short readushort(FILE * f, int bigendian)
529 {
530   unsigned char c1, c2;
531   fread(&c1, 1, 1, f);
532   fread(&c2, 1, 1, f);
533   if (bigendian)
534     return (c1 << 8) + c2;
535   else
536     return (c2 << 8) + c1;
537 }
538
539 unsigned int readuint(FILE * f, int bigendian)
540 {
541   unsigned char c1, c2, c3, c4;
542   fread(&c1, 1, 1, f);
543   fread(&c2, 1, 1, f);
544   fread(&c3, 1, 1, f);
545   fread(&c4, 1, 1, f);
546   if (bigendian)
547     return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
548   else
549     return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
550 }
551
552 int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
553                int subsampling_dx, int subsampling_dy, int Dim[2],
554                j2k_cp_t cp)
555 {
556   FILE *f;
557   int w, h, prec;
558   int i, compno, bandno;
559   char str[256]; 
560
561   char endian1,endian2,sign;
562   char signtmp[32];
563
564   char temp[32];
565   int bigendian;
566   j2k_comp_t *comp;
567
568   img->numcomps = 1;
569   img->color_space = 2;
570   img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
571   for (compno = 0; compno < img->numcomps; compno++) {
572     FILE *src;
573     char tmp[16];
574     int max = 0;
575     int Y1;
576
577
578     comp = &img->comps[compno];
579     sprintf(str, "%s", filename);
580     
581
582     f = fopen(str, "rb");
583     if (!f) {
584       fprintf(stderr, "Failed to open %s for reading !\n", str);
585       return 0;
586     }
587
588
589
590     fseek(f, 0, SEEK_SET);
591
592     fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
593
594     
595
596     i=0;
597
598     sign='+';
599
600     while (signtmp[i]!='\0') {
601
602       if (signtmp[i]=='-') sign='-';
603
604       i++;
605
606     }
607
608     
609
610     fgetc(f);
611     if (endian1=='M' && endian2=='L')
612       bigendian = 1;
613     else if (endian2=='M' && endian1=='L')
614       bigendian = 0;
615
616     else {
617
618       fprintf(stderr, "Bad pgx header, please check input file\n");
619
620       return 0;
621
622     }
623
624
625     if (compno == 0) {
626       img->x0 = Dim[0];
627       img->y0 = Dim[1];
628       img->x1 =
629         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
630         1) *
631         subsampling_dx + 1;
632       img->y1 =
633         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
634         1) *
635         subsampling_dy + 1;
636     } else {
637       if (w != img->x1 || h != img->y1)
638         return 0;
639     }
640     
641     if (sign == '-') {
642       comp->sgnd = 1;
643     } else {
644       comp->sgnd = 0;
645     }
646     comp->prec = prec;
647     comp->dx = subsampling_dx;
648     comp->dy = subsampling_dy;
649     bandno = 1;
650     
651     Y1 = cp.ty0 + bandno * cp.tdy <
652       img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
653     Y1 -= img->y0;
654     
655     sprintf(tmp, "bandtile%d", bandno); /* bandtile file */
656     src = fopen(tmp, "wb");
657     if (!src) {
658       fprintf(stderr, "failed to open %s for writing !\n", tmp);
659     }
660     for (i = 0; i < w * h; i++) {
661       int v;
662       if (i == Y1 * w / subsampling_dy && tdy != -1) {  /* bandtile is full */
663         fclose(src);
664         bandno++;
665         sprintf(tmp, "bandtile%d", bandno);
666         src = fopen(tmp, "wb");
667         if (!src) {
668           fprintf(stderr, "failed to open %s for writing !\n", tmp);
669         }
670         Y1 = cp.ty0 + bandno * cp.tdy <
671           img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
672         Y1 -= img->y0;
673       }
674       if (comp->prec <= 8) {
675         if (!comp->sgnd) {
676           v = readuchar(f);
677         } else {
678           v = (char) readuchar(f);
679         }
680       } else if (comp->prec <= 16) {
681         if (!comp->sgnd) {
682           v = readushort(f, bigendian);
683         } else {
684           v = (short) readushort(f, bigendian);
685         }
686       } else {
687         if (!comp->sgnd) {
688           v = readuint(f, bigendian);
689         } else {
690           v = (int) readuint(f, bigendian);
691         }
692       }
693       if (v > max)
694         max = v;
695       fprintf(src, "%d ", v);
696     }
697     fclose(f);
698     fclose(src);
699     comp->bpp = int_floorlog2(max) + 1;
700   }
701   return 1;
702 }
703
704 /* -->> -->> -->> -->>
705
706   PNM IMAGE FORMAT
707
708  <<-- <<-- <<-- <<-- */
709
710 int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
711                int subsampling_dy, int Dim[2])
712 {
713   FILE *f;
714   FILE *Compo0, *Compo1, *Compo2;
715   int w, h;
716   int i;
717   char value;
718   char comment[256];
719
720   f = fopen(filename, "rb");
721   if (!f) {
722     fprintf(stderr,
723             "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
724             filename);
725     return 0;
726   }
727
728   if (fgetc(f) != 'P')
729     return 0;
730   value = fgetc(f);
731
732   if (value == '2') {
733     fgetc(f);
734     if (fgetc(f) == '#') {
735       fseek(f, 0, SEEK_SET);
736       fscanf(f, "P2\n");
737       fgets(comment, 256, f);
738       fscanf(f, "%d %d\n255", &w, &h);
739     } else {
740       fseek(f, 0, SEEK_SET);
741       fscanf(f, "P2\n%d %d\n255", &w, &h);
742     }
743
744     fgetc(f);
745     img->x0 = Dim[0];
746     img->y0 = Dim[1];
747     img->x1 =
748       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
749                                                          1) *
750       subsampling_dx + 1;
751     img->y1 =
752       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
753                                                          1) *
754       subsampling_dy + 1;
755
756     img->numcomps = 1;
757     img->color_space = 2;
758     img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
759     img->comps[0].prec = 8;
760     img->comps[0].bpp = 8;
761     img->comps[0].sgnd = 0;
762     img->comps[0].dx = subsampling_dx;
763     img->comps[0].dy = subsampling_dy;
764
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 int l;
772       fscanf(f, "%d", &l);
773       fprintf(Compo0, "%c", l);
774     }
775     fclose(Compo0);
776   } else if (value == '5') {
777     fgetc(f);
778     if (fgetc(f) == '#') {
779       fseek(f, 0, SEEK_SET);
780       fscanf(f, "P5\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, "P5\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
800     img->numcomps = 1;
801     img->color_space = 2;
802     img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
803     img->comps[0].prec = 8;
804     img->comps[0].bpp = 8;
805     img->comps[0].sgnd = 0;
806     img->comps[0].dx = subsampling_dx;
807     img->comps[0].dy = subsampling_dy;
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     for (i = 0; i < w * h; i++) {
814       unsigned char l;
815       fread(&l, 1, 1, f);
816       fwrite(&l, 1, 1, Compo0);
817     }
818     fclose(Compo0);
819   } else if (value == '3') {
820     fgetc(f);
821     if (fgetc(f) == '#') {
822       fseek(f, 0, SEEK_SET);
823       fscanf(f, "P3\n");
824       fgets(comment, 256, f);
825       fscanf(f, "%d %d\n255", &w, &h);
826     } else {
827       fseek(f, 0, SEEK_SET);
828       fscanf(f, "P3\n%d %d\n255", &w, &h);
829     }
830
831     fgetc(f);
832     img->x0 = Dim[0];
833     img->y0 = Dim[1];
834     img->x1 =
835       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
836                                                          1) *
837       subsampling_dx + 1;
838     img->y1 =
839       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
840                                                          1) *
841       subsampling_dy + 1;
842     img->numcomps = 3;
843     img->color_space = 1;
844     img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
845     for (i = 0; i < img->numcomps; i++) {
846       img->comps[i].prec = 8;
847       img->comps[i].bpp = 8;
848       img->comps[i].sgnd = 0;
849       img->comps[i].dx = subsampling_dx;
850       img->comps[i].dy = subsampling_dy;
851     }
852     Compo0 = fopen("Compo0", "wb");
853     if (!Compo0) {
854       fprintf(stderr,
855               "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
856     }
857
858     Compo1 = fopen("Compo1", "wb");
859     if (!Compo1) {
860       fprintf(stderr,
861               "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
862     }
863
864     Compo2 = fopen("Compo2", "wb");
865     if (!Compo2) {
866       fprintf(stderr,
867               "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
868     }
869
870     for (i = 0; i < w * h; i++) {
871       unsigned int r, g, b;
872       fscanf(f, "%d", &r);
873       fscanf(f, "%d", &g);
874       fscanf(f, "%d", &b);
875       fprintf(Compo0, "%c", r);
876       fprintf(Compo1, "%c", g);
877       fprintf(Compo2, "%c", b);
878     }
879     fclose(Compo0);
880     fclose(Compo1);
881     fclose(Compo2);
882   } else if (value == '6') {
883     fgetc(f);
884     if (fgetc(f) == '#') {
885       fseek(f, 0, SEEK_SET);
886       fscanf(f, "P6\n");
887       fgets(comment, 256, f);
888       fscanf(f, "%d %d\n255", &w, &h);
889     } else {
890       fseek(f, 0, SEEK_SET);
891       fscanf(f, "P6\n%d %d\n255", &w, &h);
892     }
893
894     fgetc(f);
895     img->x0 = Dim[0];
896     img->y0 = Dim[1];
897     img->x1 =
898       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
899                                                          1) *
900       subsampling_dx + 1;
901     img->y1 =
902       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
903                                                          1) *
904       subsampling_dy + 1;
905     img->numcomps = 3;
906     img->color_space = 1;
907     img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
908     for (i = 0; i < img->numcomps; i++) {
909       img->comps[i].prec = 8;
910       img->comps[i].bpp = 8;
911       img->comps[i].sgnd = 0;
912       img->comps[i].dx = subsampling_dx;
913       img->comps[i].dy = subsampling_dy;
914     }
915     Compo0 = fopen("Compo0", "wb");
916     if (!Compo0) {
917       fprintf(stderr,
918               "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
919     }
920
921     Compo1 = fopen("Compo1", "wb");
922     if (!Compo1) {
923       fprintf(stderr,
924               "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
925     }
926
927     Compo2 = fopen("Compo2", "wb");
928     if (!Compo2) {
929       fprintf(stderr,
930               "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
931     }
932
933     for (i = 0; i < w * h; i++) {
934       unsigned char r, g, b;
935       fread(&r, 1, 1, f);
936       fread(&g, 1, 1, f);
937       fread(&b, 1, 1, f);
938       fwrite(&r, 1, 1, Compo0);
939       fwrite(&g, 1, 1, Compo1);
940       fwrite(&b, 1, 1, Compo2);
941     }
942     fclose(Compo0);
943     fclose(Compo1);
944     fclose(Compo2);
945   } else {
946     return 0;
947   }
948   fclose(f);
949   return 1;
950 }