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