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