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