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 fprintf(stderr,"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 - 1) *
185 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
188 img->color_space = 1;
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;
198 Compo0 = fopen("Compo0", "wb");
201 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
203 Compo1 = fopen("Compo1", "wb");
206 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
208 Compo2 = fopen("Compo2", "wb");
211 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
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);
221 /* PAD = 4 - (3 * W) % 4;*/
222 /* PAD = (PAD == 4) ? 0 : PAD; */
223 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
227 (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
229 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
231 for (j = 0; j < (3 * W + PAD) * H; j++) {
233 int Wp = 3 * W + PAD;
235 elmt = RGB[(H - (j / Wp + 1)) * Wp + j % Wp];
236 if ((j % Wp) < (3 * W)) {
239 fprintf(Compo2, "%c", elmt);
243 fprintf(Compo1, "%c", elmt);
247 fprintf(Compo0, "%c", elmt);
258 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
262 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -1) *
265 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -1) *
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));
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);
277 if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
278 && table_G[j] != table_B[j])
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);
288 if (Info_h.biWidth % 2)
291 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
293 fread(RGB, sizeof(unsigned char), W * H, IN);
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");
306 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
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)]]);
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;
327 Compo0 = fopen("Compo0", "wb");
330 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
332 Compo1 = fopen("Compo1", "wb");
335 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
337 Compo2 = fopen("Compo2", "wb");
340 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
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)]]);
361 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
365 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
368 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
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));
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);
380 if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
381 && table_G[j] != table_B[j])
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);
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");
400 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
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;
413 Compo0 = fopen("Compo0", "wb");
416 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
418 Compo1 = fopen("Compo1", "wb");
421 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
423 Compo2 = fopen("Compo2", "wb");
426 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
431 (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight *
432 sizeof(unsigned char));
434 while (not_end_file) {
438 for (i = 0; i < (int) v; i++) {
439 RGB[line * Info_h.biWidth + col] = v2;
454 fprintf(stderr,"No Delta supported\n");
458 for (i = 0; i < v; i++) {
460 RGB[line * Info_h.biWidth + col] = v2;
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]]);
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]]);
496 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
504 /* -->> -->> -->> -->>
508 <<-- <<-- <<-- <<-- */
511 unsigned char readuchar(FILE * f)
518 unsigned short readushort(FILE * f, int bigendian)
520 unsigned char c1, c2;
524 return (c1 << 8) + c2;
526 return (c2 << 8) + c1;
529 unsigned int readuint(FILE * f, int bigendian)
531 unsigned char c1, c2, c3, c4;
537 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
539 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
542 int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
543 int subsampling_dx, int subsampling_dy, int Dim[2],
548 int i, compno, bandno;
551 char endian1,endian2,sign;
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++) {
567 comp = &img->comps[compno];
568 sprintf(str, "%s", filename);
571 f = fopen(str, "rb");
573 fprintf(stderr, "Failed to open %s for reading !\n", str);
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);
584 while (signtmp[i]!='\0') {
585 if (signtmp[i]=='-') sign='-';
590 if (endian1=='M' && endian2=='L')
592 else if (endian2=='M' && endian1=='L')
595 fprintf(stderr, "Bad pgx header, please check input file\n");
602 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
605 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
608 if (w != img->x1 || h != img->y1)
618 comp->dx = subsampling_dx;
619 comp->dy = subsampling_dy;
622 Y1 = cp.ty0 + bandno * cp.tdy <
623 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
626 sprintf(tmp, "bandtile%d", bandno); /* bandtile file */
627 src = fopen(tmp, "wb");
629 fprintf(stderr, "failed to open %s for writing !\n", tmp);
631 for (i = 0; i < w * h; i++) {
633 if (i == Y1 * w / subsampling_dy && tdy != -1) { /* bandtile is full */
636 sprintf(tmp, "bandtile%d", bandno);
637 src = fopen(tmp, "wb");
639 fprintf(stderr, "failed to open %s for writing !\n", tmp);
641 Y1 = cp.ty0 + bandno * cp.tdy <
642 img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
645 if (comp->prec <= 8) {
649 v = (char) readuchar(f);
651 } else if (comp->prec <= 16) {
653 v = readushort(f, bigendian);
655 v = (short) readushort(f, bigendian);
659 v = readuint(f, bigendian);
661 v = (int) readuint(f, bigendian);
666 fprintf(src, "%d ", v);
670 comp->bpp = int_floorlog2(max) + 1;
675 /* -->> -->> -->> -->>
679 <<-- <<-- <<-- <<-- */
681 int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
682 int subsampling_dy, int Dim[2])
685 FILE *Compo0, *Compo1, *Compo2;
691 f = fopen(filename, "rb");
694 "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
705 if (fgetc(f) == '#') {
706 fseek(f, 0, SEEK_SET);
708 fgets(comment, 256, f);
709 fscanf(f, "%d %d\n255", &w, &h);
711 fseek(f, 0, SEEK_SET);
712 fscanf(f, "P2\n%d %d\n255", &w, &h);
719 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
722 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 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;
734 Compo0 = fopen("Compo0", "wb");
737 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
739 for (i = 0; i < w * h; i++) {
742 fprintf(Compo0, "%c", l);
745 } else if (value == '5') {
747 if (fgetc(f) == '#') {
748 fseek(f, 0, SEEK_SET);
750 fgets(comment, 256, f);
751 fscanf(f, "%d %d\n255", &w, &h);
753 fseek(f, 0, SEEK_SET);
754 fscanf(f, "P5\n%d %d\n255", &w, &h);
761 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
764 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 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");
778 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
780 for (i = 0; i < w * h; i++) {
783 fwrite(&l, 1, 1, Compo0);
786 } else if (value == '3') {
788 if (fgetc(f) == '#') {
789 fseek(f, 0, SEEK_SET);
791 fgets(comment, 256, f);
792 fscanf(f, "%d %d\n255", &w, &h);
794 fseek(f, 0, SEEK_SET);
795 fscanf(f, "P3\n%d %d\n255", &w, &h);
802 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
805 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
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;
817 Compo0 = fopen("Compo0", "wb");
820 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
823 Compo1 = fopen("Compo1", "wb");
826 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
829 Compo2 = fopen("Compo2", "wb");
832 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
835 for (i = 0; i < w * h; i++) {
836 unsigned int r, g, b;
840 fprintf(Compo0, "%c", r);
841 fprintf(Compo1, "%c", g);
842 fprintf(Compo2, "%c", b);
847 } else if (value == '6') {
849 if (fgetc(f) == '#') {
850 fseek(f, 0, SEEK_SET);
852 fgets(comment, 256, f);
853 fscanf(f, "%d %d\n255", &w, &h);
855 fseek(f, 0, SEEK_SET);
856 fscanf(f, "P6\n%d %d\n255", &w, &h);
863 !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
866 !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
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;
878 Compo0 = fopen("Compo0", "wb");
881 "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
884 Compo1 = fopen("Compo1", "wb");
887 "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
890 Compo2 = fopen("Compo2", "wb");
893 "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
896 for (i = 0; i < w * h; i++) {
897 unsigned char r, g, b;
901 fwrite(&r, 1, 1, Compo0);
902 fwrite(&g, 1, 1, Compo1);
903 fwrite(&b, 1, 1, Compo2);