]> Creatis software - gdcm.git/blob - src/gdcmopenjpeg/codec/convert.c
COMP: Fix comp on weird os
[gdcm.git] / src / gdcmopenjpeg / codec / convert.c
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2002-2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <openjpeg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35 /* -->> -->> -->> -->>
36
37   BMP IMAGE FORMAT
38
39  <<-- <<-- <<-- <<-- */
40
41 /* UINT2 defines a two byte word */
42 typedef unsigned short int UINT2;
43
44 /* UINT4 defines a four byte word */
45 typedef unsigned long int UINT4;
46
47 typedef struct {
48   UINT2 bfType;         /* 'BM' for Bitmap (19776) */
49   UINT4 bfSize;         /* Size of the file        */
50   UINT2 bfReserved1;      /* Reserved : 0            */
51   UINT2 bfReserved2;      /* Reserved : 0            */
52   UINT4 bfOffBits;      /* Offset                  */
53 } BITMAPFILEHEADER_t;
54
55 typedef struct {
56   UINT4 biSize;         /* Size of the structure in bytes */
57   UINT4 biWidth;      /* Width of the image in pixels */
58   UINT4 biHeight;      /* Heigth of the image in pixels */
59   UINT2 biPlanes;      /* 1 */
60   UINT2 biBitCount;      /* Number of color bits by pixels */
61   UINT4 biCompression;      /* Type of encoding 0: none 1: RLE8 2: RLE4 */
62   UINT4 biSizeImage;      /* Size of the image in bytes */
63   UINT4 biXpelsPerMeter;   /* Horizontal (X) resolution in pixels/meter */
64   UINT4 biYpelsPerMeter;   /* Vertical (Y) resolution in pixels/meter */
65   UINT4 biClrUsed;      /* Number of color used in the image (0: ALL) */
66   UINT4 biClrImportant;      /* Number of important color (0: ALL) */
67 } BITMAPINFOHEADER_t;
68
69 int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
70           int subsampling_dy, int Dim[2])
71 {
72   FILE *IN;
73   FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL;
74   BITMAPFILEHEADER_t File_h;
75   BITMAPINFOHEADER_t Info_h;
76   unsigned char *RGB;
77   unsigned char *table_R, *table_G, *table_B;
78   unsigned int j, w, h, PAD, type = 0;
79
80   int i;
81   int gray_scale = 1, not_end_file = 1; 
82
83   unsigned int line = 0, col = 0;
84   unsigned char v, v2;
85   UINT4 W, H;
86
87   IN = fopen(filename, "rb");
88   if (!IN) {
89     fprintf(stderr,
90        "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
91        filename);
92     return 0;
93   }
94
95   File_h.bfType = getc(IN);
96   File_h.bfType = (getc(IN) << 8) + File_h.bfType;
97
98   if (File_h.bfType != 19778) {
99     fprintf(stderr,"Error, not a BMP file!\n");
100     return 0;
101   } else {
102     /* FILE HEADER */
103     /* ------------- */
104     File_h.bfSize = getc(IN);
105     File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
106     File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
107     File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
108
109     File_h.bfReserved1 = getc(IN);
110     File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
111
112     File_h.bfReserved2 = getc(IN);
113     File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
114
115     File_h.bfOffBits = getc(IN);
116     File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
117     File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
118     File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
119
120     /* INFO HEADER */
121     /* ------------- */
122
123     Info_h.biSize = getc(IN);
124     Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
125     Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
126     Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
127
128     Info_h.biWidth = getc(IN);
129     Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
130     Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
131     Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
132     w = Info_h.biWidth;
133
134     Info_h.biHeight = getc(IN);
135     Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
136     Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
137     Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
138     h = Info_h.biHeight;
139
140     Info_h.biPlanes = getc(IN);
141     Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
142
143     Info_h.biBitCount = getc(IN);
144     Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
145
146     Info_h.biCompression = getc(IN);
147     Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
148     Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
149     Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
150
151     Info_h.biSizeImage = getc(IN);
152     Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
153     Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
154     Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
155
156     Info_h.biXpelsPerMeter = getc(IN);
157     Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
158     Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
159     Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
160
161     Info_h.biYpelsPerMeter = getc(IN);
162     Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
163     Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
164     Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
165
166     Info_h.biClrUsed = getc(IN);
167     Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
168     Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
169     Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
170
171     Info_h.biClrImportant = getc(IN);
172     Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
173     Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
174     Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
175
176     /* Read the data and store them in the OUT file */
177
178     if (Info_h.biBitCount == 24) {
179       img->x0 = Dim[0];
180       img->y0 = Dim[1];
181       img->x1 =
182         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
183         subsampling_dx + 1;
184       img->y1 =
185         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
186         subsampling_dy + 1;
187       img->numcomps = 3;
188       img->color_space = 1;
189       img->comps =
190         (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
191       for (i = 0; i < img->numcomps; i++) {
192         img->comps[i].prec = 8;
193         img->comps[i].bpp = 8;
194         img->comps[i].sgnd = 0;
195         img->comps[i].dx = subsampling_dx;
196         img->comps[i].dy = subsampling_dy;
197       }
198       Compo0 = fopen("Compo0", "wb");
199       if (!Compo0) {
200         fprintf(stderr,
201           "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
202       }
203       Compo1 = fopen("Compo1", "wb");
204       if (!Compo1) {
205         fprintf(stderr,
206           "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
207       }
208       Compo2 = fopen("Compo2", "wb");
209       if (!Compo2) {
210         fprintf(stderr,
211           "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
212       }
213
214       /* Place the cursor at the beginning of the image information */
215       fseek(IN, 0, SEEK_SET);
216       fseek(IN, File_h.bfOffBits, SEEK_SET);
217
218       W = Info_h.biWidth;
219       H = Info_h.biHeight;
220
221       /* PAD = 4 - (3 * W) % 4;*/
222       /* PAD = (PAD == 4) ? 0 : PAD; */
223       PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
224
225
226       RGB =
227         (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
228
229       fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
230
231       for (j = 0; j < (3 * W + PAD) * H; j++) {
232         unsigned char elmt;
233         int Wp = 3 * W + PAD;
234
235         elmt = RGB[(H - (j / Wp + 1)) * Wp + j % Wp];
236         if ((j % Wp) < (3 * W)) {
237           switch (type) {
238           case 0:
239             fprintf(Compo2, "%c", elmt);
240             type = 1;
241             break;
242           case 1:
243             fprintf(Compo1, "%c", elmt);
244             type = 2;
245             break;
246           case 2:
247             fprintf(Compo0, "%c", elmt);
248             type = 0;
249             break;
250           }
251         }
252       }
253
254       fclose(Compo0);
255       fclose(Compo1);
256       fclose(Compo2);
257       free(RGB);
258     } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
259       img->x0 = Dim[0];
260       img->y0 = Dim[1];
261       img->x1 =
262         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -1) *
263         subsampling_dx + 1;
264       img->y1 =
265         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -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 (j = 0; j < Info_h.biClrUsed; j++) {
273         table_B[j] = getc(IN);
274         table_G[j] = getc(IN);
275         table_R[j] = getc(IN);
276         getc(IN);
277         if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
278          && table_G[j] != table_B[j])
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 (j = 0; j < W * H; j++) {
309           if ((j % W < W - 1 && Info_h.biWidth % 2)
310             || !(Info_h.biWidth % 2))
311             fprintf(Compo0, "%c",
312                table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (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 (j = 0; j < W * H; j++) {
344           if ((j % W < W - 1 && Info_h.biWidth % 2)
345             || !(Info_h.biWidth % 2)) {
346             fprintf(Compo0, "%c",
347               table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
348             fprintf(Compo1, "%c",
349                table_G[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
350             fprintf(Compo2, "%c",
351                table_B[RGB[W * H - ((j) / (W) + 1) * W + (j) % (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 - 1) *
366          subsampling_dx + 1;
367       img->y1 =
368        !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
369         subsampling_dy + 1;
370
371       table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
372       table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
373       table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
374
375       for (j = 0; j < Info_h.biClrUsed; j++) {
376         table_B[j] = getc(IN);
377         table_G[j] = getc(IN);
378         table_R[j] = getc(IN);
379         getc(IN);
380         if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
381          && table_G[j] != table_B[j])
382           gray_scale = 0;
383       }
384
385       /* Place the cursor at the beginning of the image information */
386       fseek(IN, 0, SEEK_SET);
387       fseek(IN, File_h.bfOffBits, SEEK_SET);
388
389       if (gray_scale) {
390         img->numcomps = 1;
391         img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
392         img->comps[0].prec = 8;
393         img->comps[0].bpp = 8;
394         img->comps[0].sgnd = 0;
395         img->comps[0].dx = subsampling_dx;
396         img->comps[0].dy = subsampling_dy;
397         Compo0 = fopen("Compo0", "wb");
398         if (!Compo0) {
399           fprintf(stderr,
400             "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
401         }
402       } else {
403         img->numcomps = 3;
404         img->comps =
405           (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
406         for (i = 0; i < img->numcomps; i++) {
407           img->comps[i].prec = 8;
408           img->comps[i].bpp = 8;
409           img->comps[i].sgnd = 0;
410           img->comps[i].dx = subsampling_dx;
411           img->comps[i].dy = subsampling_dy;
412         }
413         Compo0 = fopen("Compo0", "wb");
414         if (!Compo0) {
415           fprintf(stderr,
416              "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
417         }
418         Compo1 = fopen("Compo1", "wb");
419         if (!Compo1) {
420           fprintf(stderr,
421             "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
422         }
423         Compo2 = fopen("Compo2", "wb");
424         if (!Compo2) {
425           fprintf(stderr,
426              "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
427         }
428       }
429
430       RGB =
431         (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight *
432         sizeof(unsigned char));
433
434       while (not_end_file) {
435         v = getc(IN);
436         if (v) {
437           v2 = getc(IN);
438           for (i = 0; i < (int) v; i++) {
439             RGB[line * Info_h.biWidth + col] = v2;
440             col++;
441           }
442         } else {
443           v = getc(IN);
444           switch (v) {
445           case 0:
446             col = 0;
447             line++;
448             break;
449           case 1:
450             line++;
451             not_end_file = 0;
452             break;
453           case 2:
454             fprintf(stderr,"No Delta supported\n");
455             return 1;
456             break;
457           default:
458             for (i = 0; i < v; i++) {
459               v2 = getc(IN);
460               RGB[line * Info_h.biWidth + col] = v2;
461               col++;
462             }
463             if (v % 2)
464               v2 = getc(IN);
465           }
466         }
467       }
468       
469       if (gray_scale) {
470         for (line = 0; line < Info_h.biHeight; line++)
471           for (col = 0; col < Info_h.biWidth; col++)
472             fprintf(Compo0, "%c", table_R[(int)
473                  RGB[(Info_h.biHeight - line -
474                  1) * Info_h.biWidth + col]]);
475           fclose(Compo0);
476         } else {
477           for (line = 0; line < Info_h.biHeight; line++)
478             for (col = 0; col < Info_h.biWidth; col++) {
479               fprintf(Compo0, "%c", table_R[(int)
480                  RGB[(Info_h.biHeight - line -
481                  1) * Info_h.biWidth + col]]);
482               fprintf(Compo1, "%c", table_G[(int)
483                  RGB[(Info_h.biHeight - line -
484                  1) * Info_h.biWidth + col]]);
485               fprintf(Compo2, "%c", table_B[(int)
486                  RGB[(Info_h.biHeight - line -
487                  1) * Info_h.biWidth + col]]);
488             }
489           fclose(Compo0);
490           fclose(Compo1);
491           fclose(Compo2);
492         }
493         free(RGB);
494       } else
495         fprintf(stderr,
496           "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
497           Info_h.biBitCount);
498
499        fclose(IN);
500        return 1;
501   }
502 }
503
504    /* -->> -->> -->> -->>
505
506       PGX IMAGE FORMAT
507
508       <<-- <<-- <<-- <<-- */
509
510
511 unsigned char readuchar(FILE * f)
512 {
513   unsigned char c1;
514   fread(&c1, 1, 1, f);
515   return c1;
516 }
517
518 unsigned short readushort(FILE * f, int bigendian)
519 {
520   unsigned char c1, c2;
521   fread(&c1, 1, 1, f);
522   fread(&c2, 1, 1, f);
523   if (bigendian)
524     return (c1 << 8) + c2;
525   else
526     return (c2 << 8) + c1;
527 }
528
529 unsigned int readuint(FILE * f, int bigendian)
530 {
531   unsigned char c1, c2, c3, c4;
532   fread(&c1, 1, 1, f);
533   fread(&c2, 1, 1, f);
534   fread(&c3, 1, 1, f);
535   fread(&c4, 1, 1, f);
536   if (bigendian)
537     return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
538   else
539     return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
540 }
541
542 int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
543           int subsampling_dx, int subsampling_dy, int Dim[2],
544           j2k_cp_t cp)
545 {
546   FILE *f;
547   int w, h, prec;
548   int i, compno, bandno;
549   char str[256]; 
550
551   char endian1,endian2,sign;
552   char signtmp[32];
553
554   char temp[32];
555   int bigendian;
556   j2k_comp_t *comp;
557
558   img->numcomps = 1;
559   img->color_space = 2;
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
567     comp = &img->comps[compno];
568     sprintf(str, "%s", filename);
569     
570
571     f = fopen(str, "rb");
572     if (!f) {
573       fprintf(stderr, "Failed to open %s for reading !\n", str);
574       return 0;
575     }
576
577     fseek(f, 0, SEEK_SET);
578     fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
579
580     i=0;
581
582     sign='+';
583
584     while (signtmp[i]!='\0') {
585       if (signtmp[i]=='-') sign='-';
586          i++;
587     }
588
589     fgetc(f);
590     if (endian1=='M' && endian2=='L')
591       bigendian = 1;
592     else if (endian2=='M' && endian1=='L')
593       bigendian = 0;
594     else {
595       fprintf(stderr, "Bad pgx header, please check input file\n");
596       return 0;
597     }
598     if (compno == 0) {
599       img->x0 = Dim[0];
600       img->y0 = Dim[1];
601       img->x1 =
602         !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
603         subsampling_dx + 1;
604       img->y1 =
605         !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
606         subsampling_dy + 1;
607     } else {
608       if (w != img->x1 || h != img->y1)
609         return 0;
610     }
611     
612     if (sign == '-') {
613       comp->sgnd = 1;
614     } else {
615       comp->sgnd = 0;
616     }
617     comp->prec = prec;
618     comp->dx = subsampling_dx;
619     comp->dy = subsampling_dy;
620     bandno = 1;
621     
622     Y1 = cp.ty0 + bandno * cp.tdy <
623       img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
624     Y1 -= img->y0;
625     
626     sprintf(tmp, "bandtile%d", bandno);   /* bandtile file */
627     src = fopen(tmp, "wb");
628     if (!src) {
629       fprintf(stderr, "failed to open %s for writing !\n", tmp);
630     }
631     for (i = 0; i < w * h; i++) {
632       int v;
633       if (i == Y1 * w / subsampling_dy && tdy != -1) {   /* bandtile is full */
634         fclose(src);
635         bandno++;
636         sprintf(tmp, "bandtile%d", bandno);
637         src = fopen(tmp, "wb");
638         if (!src) {
639           fprintf(stderr, "failed to open %s for writing !\n", tmp);
640         }
641         Y1 = cp.ty0 + bandno * cp.tdy <
642         img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
643         Y1 -= img->y0;
644       }
645       if (comp->prec <= 8) {
646         if (!comp->sgnd) {
647           v = readuchar(f);
648         } else {
649           v = (char) readuchar(f);
650         }
651       } else if (comp->prec <= 16) {
652         if (!comp->sgnd) {
653           v = readushort(f, bigendian);
654         } else {
655           v = (short) readushort(f, bigendian);
656         }
657       } else {
658         if (!comp->sgnd) {
659           v = readuint(f, bigendian);
660         } else {
661           v = (int) readuint(f, bigendian);
662         }
663       }
664       if (v > max)
665         max = v;
666       fprintf(src, "%d ", v);
667     }
668     fclose(f);
669     fclose(src);
670     comp->bpp = int_floorlog2(max) + 1;
671   }
672   return 1;
673 }
674
675 /* -->> -->> -->> -->>
676
677   PNM IMAGE FORMAT
678
679  <<-- <<-- <<-- <<-- */
680
681 int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
682           int subsampling_dy, int Dim[2])
683 {
684   FILE *f;
685   FILE *Compo0, *Compo1, *Compo2;
686   int w, h;
687   int i;
688   char value;
689   char comment[256];
690
691   f = fopen(filename, "rb");
692   if (!f) {
693     fprintf(stderr,
694        "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
695        filename);
696     return 0;
697   }
698
699   if (fgetc(f) != 'P')
700     return 0;
701   value = fgetc(f);
702
703   if (value == '2') {
704     fgetc(f);
705     if (fgetc(f) == '#') {
706       fseek(f, 0, SEEK_SET);
707       fscanf(f, "P2\n");
708       fgets(comment, 256, f);
709       fscanf(f, "%d %d\n255", &w, &h);
710     } else {
711       fseek(f, 0, SEEK_SET);
712       fscanf(f, "P2\n%d %d\n255", &w, &h);
713     }
714
715     fgetc(f);
716     img->x0 = Dim[0];
717     img->y0 = Dim[1];
718     img->x1 =
719       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
720       subsampling_dx + 1;
721     img->y1 =
722       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
723       subsampling_dy + 1;
724
725     img->numcomps = 1;
726     img->color_space = 2;
727     img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
728     img->comps[0].prec = 8;
729     img->comps[0].bpp = 8;
730     img->comps[0].sgnd = 0;
731     img->comps[0].dx = subsampling_dx;
732     img->comps[0].dy = subsampling_dy;
733
734     Compo0 = fopen("Compo0", "wb");
735     if (!Compo0) {
736       fprintf(stderr,
737          "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
738     }
739     for (i = 0; i < w * h; i++) {
740       unsigned int l;
741       fscanf(f, "%d", &l);
742       fprintf(Compo0, "%c", l);
743     }
744     fclose(Compo0);
745   } else if (value == '5') {
746     fgetc(f);
747     if (fgetc(f) == '#') {
748       fseek(f, 0, SEEK_SET);
749       fscanf(f, "P5\n");
750       fgets(comment, 256, f);
751       fscanf(f, "%d %d\n255", &w, &h);
752     } else {
753       fseek(f, 0, SEEK_SET);
754       fscanf(f, "P5\n%d %d\n255", &w, &h);
755     }
756
757     fgetc(f);
758     img->x0 = Dim[0];
759     img->y0 = Dim[1];
760     img->x1 =
761       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
762       subsampling_dx + 1;
763     img->y1 =
764       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
765       subsampling_dy + 1;
766
767     img->numcomps = 1;
768     img->color_space = 2;
769     img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
770     img->comps[0].prec = 8;
771     img->comps[0].bpp = 8;
772     img->comps[0].sgnd = 0;
773     img->comps[0].dx = subsampling_dx;
774     img->comps[0].dy = subsampling_dy;
775     Compo0 = fopen("Compo0", "wb");
776     if (!Compo0) {
777       fprintf(stderr,
778          "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
779     }
780     for (i = 0; i < w * h; i++) {
781       unsigned char l;
782       fread(&l, 1, 1, f);
783       fwrite(&l, 1, 1, Compo0);
784     }
785     fclose(Compo0);
786   } else if (value == '3') {
787     fgetc(f);
788     if (fgetc(f) == '#') {
789       fseek(f, 0, SEEK_SET);
790       fscanf(f, "P3\n");
791       fgets(comment, 256, f);
792       fscanf(f, "%d %d\n255", &w, &h);
793     } else {
794       fseek(f, 0, SEEK_SET);
795       fscanf(f, "P3\n%d %d\n255", &w, &h);
796     }
797
798     fgetc(f);
799     img->x0 = Dim[0];
800     img->y0 = Dim[1];
801     img->x1 =
802       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
803       subsampling_dx + 1;
804     img->y1 =
805       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
806       subsampling_dy + 1;
807     img->numcomps = 3;
808     img->color_space = 1;
809     img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
810     for (i = 0; i < img->numcomps; i++) {
811       img->comps[i].prec = 8;
812       img->comps[i].bpp = 8;
813       img->comps[i].sgnd = 0;
814       img->comps[i].dx = subsampling_dx;
815       img->comps[i].dy = subsampling_dy;
816     }
817     Compo0 = fopen("Compo0", "wb");
818     if (!Compo0) {
819       fprintf(stderr,
820          "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
821     }
822
823     Compo1 = fopen("Compo1", "wb");
824     if (!Compo1) {
825       fprintf(stderr,
826          "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
827     }
828
829     Compo2 = fopen("Compo2", "wb");
830     if (!Compo2) {
831       fprintf(stderr,
832          "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
833     }
834
835     for (i = 0; i < w * h; i++) {
836       unsigned int r, g, b;
837       fscanf(f, "%d", &r);
838       fscanf(f, "%d", &g);
839       fscanf(f, "%d", &b);
840       fprintf(Compo0, "%c", r);
841       fprintf(Compo1, "%c", g);
842       fprintf(Compo2, "%c", b);
843     }
844     fclose(Compo0);
845     fclose(Compo1);
846     fclose(Compo2);
847   } else if (value == '6') {
848     fgetc(f);
849     if (fgetc(f) == '#') {
850       fseek(f, 0, SEEK_SET);
851       fscanf(f, "P6\n");
852       fgets(comment, 256, f);
853       fscanf(f, "%d %d\n255", &w, &h);
854     } else {
855       fseek(f, 0, SEEK_SET);
856       fscanf(f, "P6\n%d %d\n255", &w, &h);
857     }
858
859     fgetc(f);
860     img->x0 = Dim[0];
861     img->y0 = Dim[1];
862     img->x1 =
863       !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
864       subsampling_dx + 1;
865     img->y1 =
866       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
867       subsampling_dy + 1;
868     img->numcomps = 3;
869     img->color_space = 1;
870     img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
871     for (i = 0; i < img->numcomps; i++) {
872       img->comps[i].prec = 8;
873       img->comps[i].bpp = 8;
874       img->comps[i].sgnd = 0;
875       img->comps[i].dx = subsampling_dx;
876       img->comps[i].dy = subsampling_dy;
877     }
878     Compo0 = fopen("Compo0", "wb");
879     if (!Compo0) {
880       fprintf(stderr,
881          "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
882     }
883
884     Compo1 = fopen("Compo1", "wb");
885     if (!Compo1) {
886       fprintf(stderr,
887          "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
888     }
889
890     Compo2 = fopen("Compo2", "wb");
891     if (!Compo2) {
892       fprintf(stderr,
893          "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
894     }
895
896     for (i = 0; i < w * h; i++) {
897       unsigned char r, g, b;
898       fread(&r, 1, 1, f);
899       fread(&g, 1, 1, f);
900       fread(&b, 1, 1, f);
901       fwrite(&r, 1, 1, Compo0);
902       fwrite(&g, 1, 1, Compo1);
903       fwrite(&b, 1, 1, Compo2);
904     }
905     fclose(Compo0);
906     fclose(Compo1);
907     fclose(Compo2);
908   } else {
909     return 0;
910   }
911   fclose(f);
912   return 1;
913 }