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,
70 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 unsigned int j, w, h, PAD, type = 0;
81 int gray_scale = 1, not_end_file = 1;
83 unsigned int line = 0, col = 0;
87 IN = fopen(filename, "rb");
90 "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
95 File_h.bfType = getc(IN);
96 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
98 if (File_h.bfType != 19778) {
99 printf("Error, not a BMP file!\n");
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;
109 File_h.bfReserved1 = getc(IN);
110 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
112 File_h.bfReserved2 = getc(IN);
113 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
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;
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;
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;
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;
140 Info_h.biPlanes = getc(IN);
141 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
143 Info_h.biBitCount = getc(IN);
144 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
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;
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;
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;
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;
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;
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;
176 /* Read the data and store them in the OUT file */
178 if (Info_h.biBitCount == 24) {
182 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
186 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
190 img->color_space = 1;
192 (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
193 for (i = 0; i < img->numcomps; i++) {
194 img->comps[i].prec = 8;
195 img->comps[i].bpp = 8;
196 img->comps[i].sgnd = 0;
197 img->comps[i].dx = subsampling_dx;
198 img->comps[i].dy = subsampling_dy;
200 Compo0 = fopen("Compo0", "wb");
203 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
205 Compo1 = fopen("Compo1", "wb");
208 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
210 Compo2 = fopen("Compo2", "wb");
213 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
216 /* Place the cursor at the beginning of the image information */
217 fseek(IN, 0, SEEK_SET);
218 fseek(IN, File_h.bfOffBits, SEEK_SET);
223 // PAD = 4 - (3 * W) % 4;
224 // PAD = (PAD == 4) ? 0 : PAD;
225 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
229 (unsigned char *) malloc((3 * W + PAD) * H *
230 sizeof(unsigned char));
232 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
234 for (j = 0; j < (3 * W + PAD) * H; j++) {
236 int Wp = 3 * W + PAD;
238 elmt = RGB[(H - (j / Wp + 1)) * Wp + j % Wp];
239 if ((j % Wp) < (3 * W)) {
242 fprintf(Compo2, "%c", elmt);
246 fprintf(Compo1, "%c", elmt);
250 fprintf(Compo0, "%c", elmt);
261 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
265 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
269 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
273 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
274 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
275 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
277 for (j = 0; j < Info_h.biClrUsed; j++) {
278 table_B[j] = getc(IN);
279 table_G[j] = getc(IN);
280 table_R[j] = getc(IN);
282 if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
283 && table_G[j] != table_B[j])
287 /* Place the cursor at the beginning of the image information */
288 fseek(IN, 0, SEEK_SET);
289 fseek(IN, File_h.bfOffBits, SEEK_SET);
293 if (Info_h.biWidth % 2)
296 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
298 fread(RGB, sizeof(unsigned char), W * H, IN);
302 (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
303 img->comps[0].prec = 8;
304 img->comps[0].bpp = 8;
305 img->comps[0].sgnd = 0;
306 img->comps[0].dx = subsampling_dx;
307 img->comps[0].dy = subsampling_dy;
308 Compo0 = fopen("Compo0", "wb");
311 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
313 for (j = 0; j < W * H; j++) {
314 if ((j % W < W - 1 && Info_h.biWidth % 2)
315 || !(Info_h.biWidth % 2))
316 fprintf(Compo0, "%c",
317 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
323 (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
324 for (i = 0; i < img->numcomps; i++) {
325 img->comps[i].prec = 8;
326 img->comps[i].bpp = 8;
327 img->comps[i].sgnd = 0;
328 img->comps[i].dx = subsampling_dx;
329 img->comps[i].dy = subsampling_dy;
332 Compo0 = fopen("Compo0", "wb");
335 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
337 Compo1 = fopen("Compo1", "wb");
340 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
342 Compo2 = fopen("Compo2", "wb");
345 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
348 for (j = 0; j < W * H; j++) {
349 if ((j % W < W - 1 && Info_h.biWidth % 2)
350 || !(Info_h.biWidth % 2)) {
351 fprintf(Compo0, "%c",
352 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
353 fprintf(Compo1, "%c",
354 table_G[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
355 fprintf(Compo2, "%c",
356 table_B[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
366 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
370 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
374 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
378 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
379 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
380 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
382 for (j = 0; j < Info_h.biClrUsed; j++) {
383 table_B[j] = getc(IN);
384 table_G[j] = getc(IN);
385 table_R[j] = getc(IN);
387 if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
388 && table_G[j] != table_B[j])
392 /* Place the cursor at the beginning of the image information */
393 fseek(IN, 0, SEEK_SET);
394 fseek(IN, File_h.bfOffBits, SEEK_SET);
398 img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
399 img->comps[0].prec = 8;
400 img->comps[0].bpp = 8;
401 img->comps[0].sgnd = 0;
402 img->comps[0].dx = subsampling_dx;
403 img->comps[0].dy = subsampling_dy;
404 Compo0 = fopen("Compo0", "wb");
407 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
412 (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
413 for (i = 0; i < img->numcomps; i++) {
414 img->comps[i].prec = 8;
415 img->comps[i].bpp = 8;
416 img->comps[i].sgnd = 0;
417 img->comps[i].dx = subsampling_dx;
418 img->comps[i].dy = subsampling_dy;
420 Compo0 = fopen("Compo0", "wb");
423 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
425 Compo1 = fopen("Compo1", "wb");
428 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
430 Compo2 = fopen("Compo2", "wb");
433 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
438 (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight *
439 sizeof(unsigned char));
441 while (not_end_file) {
445 for (i = 0; i < (int) v; i++) {
446 RGB[line * Info_h.biWidth + col] = v2;
461 printf("No Delta supported\n");
465 for (i = 0; i < v; i++) {
467 RGB[line * Info_h.biWidth + col] = v2;
476 for (line = 0; line < Info_h.biHeight; line++)
477 for (col = 0; col < Info_h.biWidth; col++)
478 fprintf(Compo0, "%c", table_R[(int)
479 RGB[(Info_h.biHeight - line -
480 1) * Info_h.biWidth +
484 for (line = 0; line < Info_h.biHeight; line++)
485 for (col = 0; col < Info_h.biWidth; col++) {
486 fprintf(Compo0, "%c", table_R[(int)
487 RGB[(Info_h.biHeight - line -
488 1) * Info_h.biWidth +
490 fprintf(Compo1, "%c", table_G[(int)
491 RGB[(Info_h.biHeight - line -
492 1) * Info_h.biWidth +
494 fprintf(Compo2, "%c", table_B[(int)
495 RGB[(Info_h.biHeight - line -
496 1) * Info_h.biWidth +
506 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
514 /* -->> -->> -->> -->>
518 <<-- <<-- <<-- <<-- */
521 unsigned char readuchar(FILE * f)
528 unsigned short readushort(FILE * f, int bigendian)
530 unsigned char c1, c2;
534 return (c1 << 8) + c2;
536 return (c2 << 8) + c1;
539 unsigned int readuint(FILE * f, int bigendian)
541 unsigned char c1, c2, c3, c4;
547 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
549 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
552 int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
553 int subsampling_dx, int subsampling_dy, int Dim[2],
558 int i, compno, bandno;
560 char endian1,endian2,sign;
567 img->color_space = 2;
568 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
569 for (compno = 0; compno < img->numcomps; compno++) {
575 comp = &img->comps[compno];
576 sprintf(str, "%s", filename);
578 f = fopen(str, "rb");
580 fprintf(stderr, "Failed to open %s for reading !\n", str);
584 fseek(f, 0, SEEK_SET);
585 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
589 while (signtmp[i]!='\0') {
590 if (signtmp[i]=='-') sign='-';
595 if (endian1=='M' && endian2=='L')
597 else if (endian2=='M' && endian1=='L')
600 fprintf(stderr, "Bad pgx header, please check input file\n", str);
608 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
612 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
616 if (w != img->x1 || h != img->y1)
626 comp->dx = subsampling_dx;
627 comp->dy = subsampling_dy;
630 Y1 = cp.ty0 + bandno * cp.tdy <
631 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
634 sprintf(tmp, "bandtile%d", bandno); /* bandtile file */
635 src = fopen(tmp, "wb");
637 fprintf(stderr, "failed to open %s for writing !\n", tmp);
639 for (i = 0; i < w * h; i++) {
641 if (i == Y1 * w / subsampling_dy && tdy != -1) { /* bandtile is full */
644 sprintf(tmp, "bandtile%d", bandno);
645 src = fopen(tmp, "wb");
647 fprintf(stderr, "failed to open %s for writing !\n", tmp);
649 Y1 = cp.ty0 + bandno * cp.tdy <
650 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
653 if (comp->prec <= 8) {
657 v = (char) readuchar(f);
659 } else if (comp->prec <= 16) {
661 v = readushort(f, bigendian);
663 v = (short) readushort(f, bigendian);
667 v = readuint(f, bigendian);
669 v = (int) readuint(f, bigendian);
674 fprintf(src, "%d ", v);
678 comp->bpp = int_floorlog2(max) + 1;
683 /* -->> -->> -->> -->>
687 <<-- <<-- <<-- <<-- */
689 int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
690 int subsampling_dy, int Dim[2])
693 FILE *Compo0, *Compo1, *Compo2;
699 f = fopen(filename, "rb");
702 "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
713 if (fgetc(f) == '#') {
714 fseek(f, 0, SEEK_SET);
716 fgets(comment, 256, f);
717 fscanf(f, "%d %d\n255", &w, &h);
719 fseek(f, 0, SEEK_SET);
720 fscanf(f, "P2\n%d %d\n255", &w, &h);
727 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
731 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
736 img->color_space = 2;
737 img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
738 img->comps[0].prec = 8;
739 img->comps[0].bpp = 8;
740 img->comps[0].sgnd = 0;
741 img->comps[0].dx = subsampling_dx;
742 img->comps[0].dy = subsampling_dy;
744 Compo0 = fopen("Compo0", "wb");
747 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
749 for (i = 0; i < w * h; i++) {
752 fprintf(Compo0, "%c", l);
755 } else if (value == '5') {
757 if (fgetc(f) == '#') {
758 fseek(f, 0, SEEK_SET);
760 fgets(comment, 256, f);
761 fscanf(f, "%d %d\n255", &w, &h);
763 fseek(f, 0, SEEK_SET);
764 fscanf(f, "P5\n%d %d\n255", &w, &h);
771 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
775 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
780 img->color_space = 2;
781 img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
782 img->comps[0].prec = 8;
783 img->comps[0].bpp = 8;
784 img->comps[0].sgnd = 0;
785 img->comps[0].dx = subsampling_dx;
786 img->comps[0].dy = subsampling_dy;
787 Compo0 = fopen("Compo0", "wb");
790 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
792 for (i = 0; i < w * h; i++) {
795 fwrite(&l, 1, 1, Compo0);
798 } else if (value == '3') {
800 if (fgetc(f) == '#') {
801 fseek(f, 0, SEEK_SET);
803 fgets(comment, 256, f);
804 fscanf(f, "%d %d\n255", &w, &h);
806 fseek(f, 0, SEEK_SET);
807 fscanf(f, "P3\n%d %d\n255", &w, &h);
814 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
818 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
822 img->color_space = 1;
823 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
824 for (i = 0; i < img->numcomps; i++) {
825 img->comps[i].prec = 8;
826 img->comps[i].bpp = 8;
827 img->comps[i].sgnd = 0;
828 img->comps[i].dx = subsampling_dx;
829 img->comps[i].dy = subsampling_dy;
831 Compo0 = fopen("Compo0", "wb");
834 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
837 Compo1 = fopen("Compo1", "wb");
840 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
843 Compo2 = fopen("Compo2", "wb");
846 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
849 for (i = 0; i < w * h; i++) {
850 unsigned int r, g, b;
854 fprintf(Compo0, "%c", r);
855 fprintf(Compo1, "%c", g);
856 fprintf(Compo2, "%c", b);
861 } else if (value == '6') {
863 if (fgetc(f) == '#') {
864 fseek(f, 0, SEEK_SET);
866 fgets(comment, 256, f);
867 fscanf(f, "%d %d\n255", &w, &h);
869 fseek(f, 0, SEEK_SET);
870 fscanf(f, "P6\n%d %d\n255", &w, &h);
877 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
881 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
885 img->color_space = 1;
886 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
887 for (i = 0; i < img->numcomps; i++) {
888 img->comps[i].prec = 8;
889 img->comps[i].bpp = 8;
890 img->comps[i].sgnd = 0;
891 img->comps[i].dx = subsampling_dx;
892 img->comps[i].dy = subsampling_dy;
894 Compo0 = fopen("Compo0", "wb");
897 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
900 Compo1 = fopen("Compo1", "wb");
903 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
906 Compo2 = fopen("Compo2", "wb");
909 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
912 for (i = 0; i < w * h; i++) {
913 unsigned char r, g, b;
917 fwrite(&r, 1, 1, Compo0);
918 fwrite(&g, 1, 1, Compo1);
919 fwrite(&b, 1, 1, Compo2);