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