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
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
36 /* -->> -->> -->> -->>
40 <<-- <<-- <<-- <<-- */
42 /* UINT2 defines a two byte word */
43 typedef unsigned short int UINT2;
45 /* UINT4 defines a four byte word */
46 typedef unsigned long int UINT4;
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 */
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) */
70 int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, int subsampling_dy, int Dim[2])
73 FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL;
74 BITMAPFILEHEADER_t File_h;
75 BITMAPINFOHEADER_t Info_h;
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;
83 IN = fopen(filename, "rb");
85 fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
89 File_h.bfType = getc(IN);
90 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
92 if (File_h.bfType != 19778) {
93 printf("Error, not a BMP file!\n");
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;
103 File_h.bfReserved1 = getc(IN);
104 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
106 File_h.bfReserved2 = getc(IN);
107 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
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;
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;
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;
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;
134 Info_h.biPlanes = getc(IN);
135 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
137 Info_h.biBitCount = getc(IN);
138 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
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;
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;
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;
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;
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;
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;
170 /* Read the data and store them in the OUT file */
172 if (Info_h.biBitCount == 24) {
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;
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;
186 Compo0 = fopen("Compo0", "wb");
188 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
190 Compo1 = fopen("Compo1", "wb");
192 fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
194 Compo2 = fopen("Compo2", "wb");
196 fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
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);
206 // PAD = 4 - (3 * W) % 4;
207 // PAD = (PAD == 4) ? 0 : PAD;
208 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
211 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
213 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
215 for (i = 0; i < (3 * W + PAD) * H; i++)
218 int Wp = 3 * W + PAD;
220 elmt = RGB[(H - (i/Wp + 1)) * Wp + i % Wp];
221 if ((i % Wp) < (3 * W))
226 fprintf(Compo2, "%c", elmt);
230 fprintf(Compo1, "%c", elmt);
234 fprintf(Compo0, "%c", elmt);
245 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
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;
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));
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);
260 if (table_R[i] != table_G[i] && table_R[i] != table_B[i] && table_G[i] != table_B[i])
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);
270 if (Info_h.biWidth % 2)
273 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
275 fread(RGB, sizeof(unsigned char), W * H, IN);
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");
286 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
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)]]);
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;
304 Compo0 = fopen("Compo0", "wb");
306 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
308 Compo1 = fopen("Compo1", "wb");
310 fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
312 Compo2 = fopen("Compo2", "wb");
314 fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
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)]]);
331 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 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;
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));
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);
346 if (table_R[i] != table_G[i] && table_R[i] != table_B[i] && table_G[i] != table_B[i])
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);
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");
364 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
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;
376 Compo0 = fopen("Compo0", "wb");
378 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
380 Compo1 = fopen("Compo1", "wb");
382 fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
384 Compo2 = fopen("Compo2", "wb");
386 fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
390 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
392 while (not_end_file) {
396 for (i = 0; i < (int) v; i++) {
397 RGB[line * Info_h.biWidth + col] = v2;
412 printf("No Delta supported\n");
416 for (i = 0; i < v; i++) {
418 RGB[line * Info_h.biWidth + col] = v2;
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]]);
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]]);
444 fprintf(stderr,"Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
452 /* -->> -->> -->> -->>
456 <<-- <<-- <<-- <<-- */
459 unsigned char readuchar(FILE * f)
466 unsigned short readushort(FILE * f, int bigendian)
468 unsigned char c1, c2;
472 return (c1 << 8) + c2;
474 return (c2 << 8) + c1;
477 unsigned int readuint(FILE * f, int bigendian)
479 unsigned char c1, c2, c3, c4;
485 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
487 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
490 int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
491 int subsampling_dx, int subsampling_dy, int Dim[2],
496 int i, compno, bandno;
497 char str[256], endian[16];
503 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
504 for (compno = 0; compno < img->numcomps; compno++) {
509 comp = &img->comps[compno];
510 sprintf(str, "%s", filename);
511 f = fopen(str, "rb");
513 fprintf(stderr, "Failed to open %s for reading !\n", str);
516 if (fscanf(f, "PG %s %c %d %d %d", endian, &sign, &prec, &w, &h) == 5)
519 if (!strcmp(endian, "ML"))
527 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
531 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
535 if (w != img->x1 || h != img->y1)
545 comp->dx = subsampling_dx;
546 comp->dy = subsampling_dy;
550 cp.ty0 + bandno * cp.tdy <
551 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
554 sprintf(tmp, "bandtile%d", bandno); /* bandtile file */
555 src = fopen(tmp, "wb");
557 fprintf(stderr, "failed to open %s for writing !\n", tmp);
559 for (i = 0; i < w * h; i++) {
561 if (i == Y1 * w / subsampling_dy && tdy != -1) { /* bandtile is full */
564 sprintf(tmp, "bandtile%d", bandno);
565 src = fopen(tmp, "wb");
567 fprintf(stderr, "failed to open %s for writing !\n", tmp);
570 cp.ty0 + bandno * cp.tdy <
571 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
574 if (comp->prec <= 8) {
578 v = (char) readuchar(f);
580 } else if (comp->prec <= 16) {
582 v = readushort(f, bigendian);
584 v = (short) readushort(f, bigendian);
588 v = readuint(f, bigendian);
590 v = (int) readuint(f, bigendian);
595 fprintf(src, "%d ", v);
602 comp->bpp = int_floorlog2(max) + 1;
607 /* -->> -->> -->> -->>
611 <<-- <<-- <<-- <<-- */
613 int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
614 int subsampling_dy, int Dim[2])
617 FILE *Compo0, *Compo1, *Compo2;
623 f = fopen(filename, "rb");
626 "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
637 if (fgetc(f) == '#') {
638 fseek(f, 0, SEEK_SET);
640 fgets(comment, 256, f);
641 fscanf(f, "%d %d\n255", &w, &h);
643 fseek(f, 0, SEEK_SET);
644 fscanf(f, "P2\n%d %d\n255", &w, &h);
651 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
655 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
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;
667 Compo0 = fopen("Compo0", "wb");
670 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
672 for (i = 0; i < w * h; i++) {
675 fprintf(Compo0, "%c", l);
678 } else if (value == '5') {
680 if (fgetc(f) == '#') {
681 fseek(f, 0, SEEK_SET);
683 fgets(comment, 256, f);
684 fscanf(f, "%d %d\n255", &w, &h);
686 fseek(f, 0, SEEK_SET);
687 fscanf(f, "P5\n%d %d\n255", &w, &h);
694 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
698 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
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");
712 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
714 for (i = 0; i < w * h; i++) {
717 fwrite(&l, 1, 1, Compo0);
720 } else if (value == '3') {
722 if (fgetc(f) == '#') {
723 fseek(f, 0, SEEK_SET);
725 fgets(comment, 256, f);
726 fscanf(f, "%d %d\n255", &w, &h);
728 fseek(f, 0, SEEK_SET);
729 fscanf(f, "P3\n%d %d\n255", &w, &h);
736 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
740 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
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;
752 Compo0 = fopen("Compo0", "wb");
755 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
758 Compo1 = fopen("Compo1", "wb");
761 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
764 Compo2 = fopen("Compo2", "wb");
767 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
770 for (i = 0; i < w * h; i++) {
771 unsigned int r, g, b;
775 fprintf(Compo0, "%c", r);
776 fprintf(Compo1, "%c", g);
777 fprintf(Compo2, "%c", b);
782 } else if (value == '6') {
784 if (fgetc(f) == '#') {
785 fseek(f, 0, SEEK_SET);
787 fgets(comment, 256, f);
788 fscanf(f, "%d %d\n255", &w, &h);
790 fseek(f, 0, SEEK_SET);
791 fscanf(f, "P6\n%d %d\n255", &w, &h);
798 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
802 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
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;
814 Compo0 = fopen("Compo0", "wb");
817 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
820 Compo1 = fopen("Compo1", "wb");
823 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
826 Compo2 = fopen("Compo2", "wb");
829 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
832 for (i = 0; i < w * h; i++) {
833 unsigned char r, g, b;
837 fwrite(&r, 1, 1, Compo0);
838 fwrite(&g, 1, 1, Compo1);
839 fwrite(&b, 1, 1, Compo2);