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.
35 /* -->> -->> -->> -->>
39 <<-- <<-- <<-- <<-- */
41 /* UINT2 defines a two byte word */
42 typedef unsigned short int UINT2;
44 /* UINT4 defines a four byte word */
45 typedef unsigned long int UINT4;
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 */
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) */
69 int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, int subsampling_dy, int Dim[2])
72 FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL;
73 BITMAPFILEHEADER_t File_h;
74 BITMAPINFOHEADER_t Info_h;
76 unsigned char *table_R, *table_G, *table_B;
77 int i, w, h, PAD, type = 0;
78 int gray_scale = 1, not_end_file = 1, line = 0, col = 0;
82 IN = fopen(filename, "rb");
84 fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
88 File_h.bfType = getc(IN);
89 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
91 if (File_h.bfType != 19778) {
92 printf("Error, not a BMP file!\n");
97 File_h.bfSize = getc(IN);
98 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
99 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
100 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
102 File_h.bfReserved1 = getc(IN);
103 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
105 File_h.bfReserved2 = getc(IN);
106 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
108 File_h.bfOffBits = getc(IN);
109 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
110 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
111 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
116 Info_h.biSize = getc(IN);
117 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
118 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
119 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
121 Info_h.biWidth = getc(IN);
122 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
123 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
124 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
127 Info_h.biHeight = getc(IN);
128 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
129 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
130 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
133 Info_h.biPlanes = getc(IN);
134 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
136 Info_h.biBitCount = getc(IN);
137 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
139 Info_h.biCompression = getc(IN);
140 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
141 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
142 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
144 Info_h.biSizeImage = getc(IN);
145 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
146 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
147 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
149 Info_h.biXpelsPerMeter = getc(IN);
150 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
151 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
152 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
154 Info_h.biYpelsPerMeter = getc(IN);
155 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
156 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
157 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
159 Info_h.biClrUsed = getc(IN);
160 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
161 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
162 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
164 Info_h.biClrImportant = getc(IN);
165 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
166 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
167 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
169 /* Read the data and store them in the OUT file */
171 if (Info_h.biBitCount == 24) {
174 img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1;
175 img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1;
177 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
178 for (i = 0; i < img->numcomps; i++) {
179 img->comps[i].prec = 8;
180 img->comps[i].bpp = 8;
181 img->comps[i].sgnd = 0;
182 img->comps[i].dx = subsampling_dx;
183 img->comps[i].dy = subsampling_dy;
185 Compo0 = fopen("Compo0", "wb");
187 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
189 Compo1 = fopen("Compo1", "wb");
191 fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
193 Compo2 = fopen("Compo2", "wb");
195 fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
198 /* Place the cursor at the beginning of the image information */
199 fseek(IN, 0, SEEK_SET);
200 fseek(IN, File_h.bfOffBits, SEEK_SET);
205 // PAD = 4 - (3 * W) % 4;
206 // PAD = (PAD == 4) ? 0 : PAD;
207 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
210 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
212 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
214 for (i = 0; i < (3 * W + PAD) * H; i++)
217 int Wp = 3 * W + PAD;
219 elmt = RGB[(H - (i/Wp + 1)) * Wp + i % Wp];
220 if ((i % Wp) < (3 * W))
225 fprintf(Compo2, "%c", elmt);
229 fprintf(Compo1, "%c", elmt);
233 fprintf(Compo0, "%c", elmt);
244 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
247 img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1;
248 img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1;
250 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
251 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
252 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
254 for (i = 0; i < Info_h.biClrUsed; i++) {
255 table_B[i] = getc(IN);
256 table_G[i] = getc(IN);
257 table_R[i] = getc(IN);
259 if (table_R[i] != table_G[i] && table_R[i] != table_B[i] && table_G[i] != table_B[i])
263 /* Place the cursor at the beginning of the image information */
264 fseek(IN, 0, SEEK_SET);
265 fseek(IN, File_h.bfOffBits, SEEK_SET);
269 if (Info_h.biWidth % 2)
272 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
274 fread(RGB, sizeof(unsigned char), W * H, IN);
277 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
278 img->comps[0].prec = 8;
279 img->comps[0].bpp = 8;
280 img->comps[0].sgnd = 0;
281 img->comps[0].dx = subsampling_dx;
282 img->comps[0].dy = subsampling_dy;
283 Compo0 = fopen("Compo0", "wb");
285 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
287 for (i = 0; i < W * H; i++) {
288 if ((i % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
289 fprintf(Compo0, "%c", table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
294 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
295 for (i = 0; i < img->numcomps; i++) {
296 img->comps[i].prec = 8;
297 img->comps[i].bpp = 8;
298 img->comps[i].sgnd = 0;
299 img->comps[i].dx = subsampling_dx;
300 img->comps[i].dy = subsampling_dy;
303 Compo0 = fopen("Compo0", "wb");
305 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
307 Compo1 = fopen("Compo1", "wb");
309 fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
311 Compo2 = fopen("Compo2", "wb");
313 fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
316 for (i = 0; i < W * H; i++) {
317 if ((i % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
318 fprintf(Compo0, "%c", table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
319 fprintf(Compo1, "%c", table_G[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
320 fprintf(Compo2, "%c", table_B[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
330 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
333 img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1;
334 img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1;
336 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
337 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
338 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
340 for (i = 0; i < Info_h.biClrUsed; i++) {
341 table_B[i] = getc(IN);
342 table_G[i] = getc(IN);
343 table_R[i] = getc(IN);
345 if (table_R[i] != table_G[i] && table_R[i] != table_B[i] && table_G[i] != table_B[i])
349 /* Place the cursor at the beginning of the image information */
350 fseek(IN, 0, SEEK_SET);
351 fseek(IN, File_h.bfOffBits, SEEK_SET);
355 img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
356 img->comps[0].prec = 8;
357 img->comps[0].bpp = 8;
358 img->comps[0].sgnd = 0;
359 img->comps[0].dx = subsampling_dx;
360 img->comps[0].dy = subsampling_dy;
361 Compo0 = fopen("Compo0", "wb");
363 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
367 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
368 for (i = 0; i < img->numcomps; i++) {
369 img->comps[i].prec = 8;
370 img->comps[i].bpp = 8;
371 img->comps[i].sgnd = 0;
372 img->comps[i].dx = subsampling_dx;
373 img->comps[i].dy = subsampling_dy;
375 Compo0 = fopen("Compo0", "wb");
377 fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
379 Compo1 = fopen("Compo1", "wb");
381 fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
383 Compo2 = fopen("Compo2", "wb");
385 fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
389 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
391 while (not_end_file) {
395 for (i = 0; i < (int) v; i++) {
396 RGB[line * Info_h.biWidth + col] = v2;
411 printf("No Delta supported\n");
415 for (i = 0; i < v; i++) {
417 RGB[line * Info_h.biWidth + col] = v2;
426 for (line = 0; line < Info_h.biHeight; line++)
427 for (col = 0; col < Info_h.biWidth; col++)
428 fprintf(Compo0, "%c", table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]);
431 for (line = 0; line < Info_h.biHeight; line++)
432 for (col = 0; col < Info_h.biWidth; col++) {
433 fprintf(Compo0, "%c", table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]);
434 fprintf(Compo1, "%c", table_G[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]);
435 fprintf(Compo2, "%c", table_B[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]);
443 fprintf(stderr,"Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
451 /* -->> -->> -->> -->>
455 <<-- <<-- <<-- <<-- */
458 unsigned char readuchar(FILE * f)
465 unsigned short readushort(FILE * f, int bigendian)
467 unsigned char c1, c2;
471 return (c1 << 8) + c2;
473 return (c2 << 8) + c1;
476 unsigned int readuint(FILE * f, int bigendian)
478 unsigned char c1, c2, c3, c4;
484 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
486 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
489 int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
490 int subsampling_dx, int subsampling_dy, int Dim[2],
495 int i, compno, bandno;
496 char str[256], endian[16];
502 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
503 for (compno = 0; compno < img->numcomps; compno++) {
508 comp = &img->comps[compno];
509 sprintf(str, "%s", filename);
510 f = fopen(str, "rb");
512 fprintf(stderr, "Failed to open %s for reading !\n", str);
515 if (fscanf(f, "PG %s %c %d %d %d", endian, &sign, &prec, &w, &h) == 5)
518 if (!strcmp(endian, "ML"))
526 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
530 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
534 if (w != img->x1 || h != img->y1)
544 comp->dx = subsampling_dx;
545 comp->dy = subsampling_dy;
549 cp.ty0 + bandno * cp.tdy <
550 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
553 sprintf(tmp, "bandtile%d", bandno); /* bandtile file */
554 src = fopen(tmp, "wb");
556 fprintf(stderr, "failed to open %s for writing !\n", tmp);
558 for (i = 0; i < w * h; i++) {
560 if (i == Y1 * w / subsampling_dy && tdy != -1) { /* bandtile is full */
563 sprintf(tmp, "bandtile%d", bandno);
564 src = fopen(tmp, "wb");
566 fprintf(stderr, "failed to open %s for writing !\n", tmp);
569 cp.ty0 + bandno * cp.tdy <
570 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
573 if (comp->prec <= 8) {
577 v = (char) readuchar(f);
579 } else if (comp->prec <= 16) {
581 v = readushort(f, bigendian);
583 v = (short) readushort(f, bigendian);
587 v = readuint(f, bigendian);
589 v = (int) readuint(f, bigendian);
594 fprintf(src, "%d ", v);
601 comp->bpp = int_floorlog2(max) + 1;
606 /* -->> -->> -->> -->>
610 <<-- <<-- <<-- <<-- */
612 int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
613 int subsampling_dy, int Dim[2])
616 FILE *Compo0, *Compo1, *Compo2;
622 f = fopen(filename, "rb");
625 "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
636 if (fgetc(f) == '#') {
637 fseek(f, 0, SEEK_SET);
639 fgets(comment, 256, f);
640 fscanf(f, "%d %d\n255", &w, &h);
642 fseek(f, 0, SEEK_SET);
643 fscanf(f, "P2\n%d %d\n255", &w, &h);
650 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
654 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
659 img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
660 img->comps[0].prec = 8;
661 img->comps[0].bpp = 8;
662 img->comps[0].sgnd = 0;
663 img->comps[0].dx = subsampling_dx;
664 img->comps[0].dy = subsampling_dy;
666 Compo0 = fopen("Compo0", "wb");
669 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
671 for (i = 0; i < w * h; i++) {
674 fprintf(Compo0, "%c", l);
677 } else if (value == '5') {
679 if (fgetc(f) == '#') {
680 fseek(f, 0, SEEK_SET);
682 fgets(comment, 256, f);
683 fscanf(f, "%d %d\n255", &w, &h);
685 fseek(f, 0, SEEK_SET);
686 fscanf(f, "P5\n%d %d\n255", &w, &h);
693 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
697 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
702 img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
703 img->comps[0].prec = 8;
704 img->comps[0].bpp = 8;
705 img->comps[0].sgnd = 0;
706 img->comps[0].dx = subsampling_dx;
707 img->comps[0].dy = subsampling_dy;
708 Compo0 = fopen("Compo0", "wb");
711 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
713 for (i = 0; i < w * h; i++) {
716 fwrite(&l, 1, 1, Compo0);
719 } else if (value == '3') {
721 if (fgetc(f) == '#') {
722 fseek(f, 0, SEEK_SET);
724 fgets(comment, 256, f);
725 fscanf(f, "%d %d\n255", &w, &h);
727 fseek(f, 0, SEEK_SET);
728 fscanf(f, "P3\n%d %d\n255", &w, &h);
735 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
739 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
743 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
744 for (i = 0; i < img->numcomps; i++) {
745 img->comps[i].prec = 8;
746 img->comps[i].bpp = 8;
747 img->comps[i].sgnd = 0;
748 img->comps[i].dx = subsampling_dx;
749 img->comps[i].dy = subsampling_dy;
751 Compo0 = fopen("Compo0", "wb");
754 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
757 Compo1 = fopen("Compo1", "wb");
760 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
763 Compo2 = fopen("Compo2", "wb");
766 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
769 for (i = 0; i < w * h; i++) {
770 unsigned int r, g, b;
774 fprintf(Compo0, "%c", r);
775 fprintf(Compo1, "%c", g);
776 fprintf(Compo2, "%c", b);
781 } else if (value == '6') {
783 if (fgetc(f) == '#') {
784 fseek(f, 0, SEEK_SET);
786 fgets(comment, 256, f);
787 fscanf(f, "%d %d\n255", &w, &h);
789 fseek(f, 0, SEEK_SET);
790 fscanf(f, "P6\n%d %d\n255", &w, &h);
797 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
801 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
805 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
806 for (i = 0; i < img->numcomps; i++) {
807 img->comps[i].prec = 8;
808 img->comps[i].bpp = 8;
809 img->comps[i].sgnd = 0;
810 img->comps[i].dx = subsampling_dx;
811 img->comps[i].dy = subsampling_dy;
813 Compo0 = fopen("Compo0", "wb");
816 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
819 Compo1 = fopen("Compo1", "wb");
822 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
825 Compo2 = fopen("Compo2", "wb");
828 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
831 for (i = 0; i < w * h; i++) {
832 unsigned char r, g, b;
836 fwrite(&r, 1, 1, Compo0);
837 fwrite(&g, 1, 1, Compo1);
838 fwrite(&b, 1, 1, Compo2);