2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Herv� Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
37 * Get logarithm of an integer and round downwards.
41 static int int_floorlog2(int a) {
43 for (l = 0; a > 1; l++) {
50 * Divide an integer by a power of 2 and round upwards.
54 static int int_ceildivpow2(int a, int b) {
55 return (a + (1 << b) - 1) >> b;
59 * Divide an integer and round upwards.
63 static int int_ceildiv(int a, int b) {
64 return (a + b - 1) / b;
67 /* -->> -->> -->> -->>
71 <<-- <<-- <<-- <<-- */
73 /* WORD defines a two byte word */
74 typedef unsigned short int WORD;
76 /* DWORD defines a four byte word */
77 typedef unsigned long int DWORD;
80 WORD bfType; /* 'BM' for Bitmap (19776) */
81 DWORD bfSize; /* Size of the file */
82 WORD bfReserved1; /* Reserved : 0 */
83 WORD bfReserved2; /* Reserved : 0 */
84 DWORD bfOffBits; /* Offset */
88 DWORD biSize; /* Size of the structure in bytes */
89 DWORD biWidth; /* Width of the image in pixels */
90 DWORD biHeight; /* Heigth of the image in pixels */
91 WORD biPlanes; /* 1 */
92 WORD biBitCount; /* Number of color bits by pixels */
93 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
94 DWORD biSizeImage; /* Size of the image in bytes */
95 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
96 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
97 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
98 DWORD biClrImportant; /* Number of important color (0: ALL) */
101 opj_image_t* bmptoimage(char *filename, opj_cparameters_t *parameters) {
102 int subsampling_dx = parameters->subsampling_dx;
103 int subsampling_dy = parameters->subsampling_dy;
105 int i, numcomps, w, h;
106 OPJ_COLOR_SPACE color_space;
107 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
108 opj_image_t * image = NULL;
111 BITMAPFILEHEADER_t File_h;
112 BITMAPINFOHEADER_t Info_h;
114 unsigned char *table_R, *table_G, *table_B;
115 unsigned int j, PAD, type = 0;
118 int gray_scale = 1, not_end_file = 1;
120 unsigned int line = 0, col = 0;
124 IN = fopen(filename, "rb");
126 fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
130 File_h.bfType = getc(IN);
131 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
133 if (File_h.bfType != 19778) {
134 fprintf(stderr,"Error, not a BMP file!\n");
139 File_h.bfSize = getc(IN);
140 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
141 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
142 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
144 File_h.bfReserved1 = getc(IN);
145 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
147 File_h.bfReserved2 = getc(IN);
148 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
150 File_h.bfOffBits = getc(IN);
151 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
152 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
153 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
158 Info_h.biSize = getc(IN);
159 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
160 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
161 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
163 Info_h.biWidth = getc(IN);
164 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
165 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
166 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
169 Info_h.biHeight = getc(IN);
170 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
171 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
172 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
175 Info_h.biPlanes = getc(IN);
176 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
178 Info_h.biBitCount = getc(IN);
179 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
181 Info_h.biCompression = getc(IN);
182 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
183 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
184 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
186 Info_h.biSizeImage = getc(IN);
187 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
188 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
189 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
191 Info_h.biXpelsPerMeter = getc(IN);
192 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
193 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
194 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
196 Info_h.biYpelsPerMeter = getc(IN);
197 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
198 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
199 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
201 Info_h.biClrUsed = getc(IN);
202 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
203 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
204 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
206 Info_h.biClrImportant = getc(IN);
207 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
208 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
209 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
211 /* Read the data and store them in the OUT file */
213 if (Info_h.biBitCount == 24) {
215 color_space = CLRSPC_SRGB;
216 /* initialize image components */
217 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
218 for(i = 0; i < numcomps; i++) {
219 cmptparm[i].prec = 8;
221 cmptparm[i].sgnd = 0;
222 cmptparm[i].dx = subsampling_dx;
223 cmptparm[i].dy = subsampling_dy;
227 /* create the image */
228 image = opj_image_create(numcomps, &cmptparm[0], color_space);
234 /* set image offset and reference grid */
235 image->x0 = parameters->image_offset_x0;
236 image->y0 = parameters->image_offset_y0;
237 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
238 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
242 /* Place the cursor at the beginning of the image information */
243 fseek(IN, 0, SEEK_SET);
244 fseek(IN, File_h.bfOffBits, SEEK_SET);
249 // PAD = 4 - (3 * W) % 4;
250 // PAD = (PAD == 4) ? 0 : PAD;
251 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
253 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
255 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
259 for(y = 0; y < (int)H; y++) {
260 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
261 for(x = 0; x < (int)W; x++) {
262 unsigned char *pixel = &scanline[3 * x];
263 image->comps[0].data[index] = pixel[2]; /* R */
264 image->comps[1].data[index] = pixel[1]; /* G */
265 image->comps[2].data[index] = pixel[0]; /* B */
272 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
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] && table_G[j] != table_B[j])
286 /* Place the cursor at the beginning of the image information */
287 fseek(IN, 0, SEEK_SET);
288 fseek(IN, File_h.bfOffBits, SEEK_SET);
292 if (Info_h.biWidth % 2)
295 numcomps = gray_scale ? 1 : 3;
296 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
297 /* initialize image components */
298 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
299 for(i = 0; i < numcomps; i++) {
300 cmptparm[i].prec = 8;
302 cmptparm[i].sgnd = 0;
303 cmptparm[i].dx = subsampling_dx;
304 cmptparm[i].dy = subsampling_dy;
308 /* create the image */
309 image = opj_image_create(numcomps, &cmptparm[0], color_space);
315 /* set image offset and reference grid */
316 image->x0 = parameters->image_offset_x0;
317 image->y0 = parameters->image_offset_y0;
318 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
319 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
323 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
325 fread(RGB, sizeof(unsigned char), W * H, IN);
328 for (j = 0; j < W * H; j++) {
329 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
330 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
337 for (j = 0; j < W * H; j++) {
338 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
339 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
340 image->comps[0].data[index] = table_R[pixel_index];
341 image->comps[1].data[index] = table_G[pixel_index];
342 image->comps[2].data[index] = table_B[pixel_index];
349 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
350 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
351 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
352 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
354 for (j = 0; j < Info_h.biClrUsed; j++) {
355 table_B[j] = getc(IN);
356 table_G[j] = getc(IN);
357 table_R[j] = getc(IN);
359 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
363 numcomps = gray_scale ? 1 : 3;
364 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
365 /* initialize image components */
366 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
367 for(i = 0; i < numcomps; i++) {
368 cmptparm[i].prec = 8;
370 cmptparm[i].sgnd = 0;
371 cmptparm[i].dx = subsampling_dx;
372 cmptparm[i].dy = subsampling_dy;
376 /* create the image */
377 image = opj_image_create(numcomps, &cmptparm[0], color_space);
383 /* set image offset and reference grid */
384 image->x0 = parameters->image_offset_x0;
385 image->y0 = parameters->image_offset_y0;
386 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
387 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
391 /* Place the cursor at the beginning of the image information */
392 fseek(IN, 0, SEEK_SET);
393 fseek(IN, File_h.bfOffBits, SEEK_SET);
395 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
397 while (not_end_file) {
401 for (i = 0; i < (int) v; i++) {
402 RGB[line * Info_h.biWidth + col] = v2;
417 fprintf(stderr,"No Delta supported\n");
418 opj_image_destroy(image);
423 for (i = 0; i < v; i++) {
425 RGB[line * Info_h.biWidth + col] = v2;
436 for (line = 0; line < Info_h.biHeight; line++) {
437 for (col = 0; col < Info_h.biWidth; col++) {
438 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
444 for (line = 0; line < Info_h.biHeight; line++) {
445 for (col = 0; col < Info_h.biWidth; col++) {
446 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
447 image->comps[0].data[index] = table_R[pixel_index];
448 image->comps[1].data[index] = table_G[pixel_index];
449 image->comps[2].data[index] = table_B[pixel_index];
457 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
465 int imagetobmp(opj_image_t * image, char *outfile) {
470 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
471 && image->comps[1].dx == image->comps[2].dx
472 && image->comps[0].dy == image->comps[1].dy
473 && image->comps[1].dy == image->comps[2].dy
474 && image->comps[0].prec == image->comps[1].prec
475 && image->comps[1].prec == image->comps[2].prec) {
477 /* -->> -->> -->> -->>
479 <<-- <<-- <<-- <<-- */
481 fdest = fopen(outfile, "wb");
483 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
487 // w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
488 // wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx);
489 w = image->comps[0].w;
490 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
492 // h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
493 // hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy);
494 h = image->comps[0].h;
495 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
497 fprintf(fdest, "BM");
501 fprintf(fdest, "%c%c%c%c",
502 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
503 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
504 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
505 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
506 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
507 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
511 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
512 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
513 (unsigned char) ((wr) >> 8) & 0xff,
514 (unsigned char) ((wr) >> 16) & 0xff,
515 (unsigned char) ((wr) >> 24) & 0xff);
516 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
517 (unsigned char) ((hr) >> 8) & 0xff,
518 (unsigned char) ((hr) >> 16) & 0xff,
519 (unsigned char) ((hr) >> 24) & 0xff);
520 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
521 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
522 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
523 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
524 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
525 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
526 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
527 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
528 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
529 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
530 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
532 for (i = 0; i < wr * hr; i++) {
533 unsigned char R, G, B;
535 // R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
536 R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
537 // G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
538 G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
539 // B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
540 B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
541 fprintf(fdest, "%c%c%c", B, G, R);
543 if ((i + 1) % wr == 0) {
544 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
545 fprintf(fdest, "%c", 0);
549 } else { /* Gray-scale */
551 /* -->> -->> -->> -->>
552 8 bits non code (Gray scale)
553 <<-- <<-- <<-- <<-- */
555 fdest = fopen(outfile, "wb");
556 // w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
557 // wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx);
558 w = image->comps[0].w;
559 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
561 // h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
562 // hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy);
563 h = image->comps[0].h;
564 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
566 fprintf(fdest, "BM");
570 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
571 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
572 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
573 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
574 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
575 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
576 ((54 + 1024) >> 16) & 0xff,
577 ((54 + 1024) >> 24) & 0xff);
581 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
582 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
583 (unsigned char) ((wr) >> 8) & 0xff,
584 (unsigned char) ((wr) >> 16) & 0xff,
585 (unsigned char) ((wr) >> 24) & 0xff);
586 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
587 (unsigned char) ((hr) >> 8) & 0xff,
588 (unsigned char) ((hr) >> 16) & 0xff,
589 (unsigned char) ((hr) >> 24) & 0xff);
590 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
591 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
592 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
593 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
594 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
595 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
596 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
597 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
598 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
599 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
600 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
603 for (i = 0; i < 256; i++) {
604 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
607 for (i = 0; i < wr * hr; i++) {
609 // fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
610 fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
611 /*if (((i + 1) % w == 0 && w % 2))
612 fprintf(fdest, "%c", 0); */
613 if ((i + 1) % wr == 0) {
614 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
615 fprintf(fdest, "%c", 0);
623 /* -->> -->> -->> -->>
627 <<-- <<-- <<-- <<-- */
630 unsigned char readuchar(FILE * f)
637 unsigned short readushort(FILE * f, int bigendian)
639 unsigned char c1, c2;
643 return (c1 << 8) + c2;
645 return (c2 << 8) + c1;
648 unsigned int readuint(FILE * f, int bigendian)
650 unsigned char c1, c2, c3, c4;
656 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
658 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
661 opj_image_t* pgxtoimage(char *filename, opj_cparameters_t *parameters) {
664 int i, numcomps, max;
665 OPJ_COLOR_SPACE color_space;
666 opj_image_cmptparm_t cmptparm; // maximum of 1 component
667 opj_image_t * image = NULL;
669 char endian1,endian2,sign;
674 opj_image_comp_t *comp = NULL;
677 color_space = CLRSPC_GRAY;
679 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
683 f = fopen(filename, "rb");
685 fprintf(stderr, "Failed to open %s for reading !\n", filename);
689 fseek(f, 0, SEEK_SET);
690 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
694 while (signtmp[i]!='\0') {
695 if (signtmp[i]=='-') sign='-';
700 if (endian1=='M' && endian2=='L') {
702 } else if (endian2=='M' && endian1=='L') {
705 fprintf(stderr, "Bad pgx header, please check input file\n");
709 /* initialize image component */
711 cmptparm.x0 = parameters->image_offset_x0;
712 cmptparm.y0 = parameters->image_offset_y0;
713 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
714 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
721 cmptparm.prec = prec;
723 cmptparm.dx = parameters->subsampling_dx;
724 cmptparm.dy = parameters->subsampling_dy;
726 /* create the image */
727 image = opj_image_create(numcomps, &cmptparm, color_space);
732 /* set image offset and reference grid */
733 image->x0 = cmptparm.x0;
734 image->y0 = cmptparm.x0;
735 image->x1 = cmptparm.w;
736 image->y1 = cmptparm.h;
740 comp = &image->comps[0];
742 for (i = 0; i < w * h; i++) {
744 if (comp->prec <= 8) {
748 v = (char) readuchar(f);
750 } else if (comp->prec <= 16) {
752 v = readushort(f, bigendian);
754 v = (short) readushort(f, bigendian);
758 v = readuint(f, bigendian);
760 v = (int) readuint(f, bigendian);
768 comp->bpp = int_floorlog2(max) + 1;
773 int imagetopgx(opj_image_t * image, char *outfile) {
778 for (compno = 0; compno < image->numcomps; compno++) {
779 opj_image_comp_t *comp = &image->comps[compno];
791 if (image->numcomps > 1) {
792 sprintf(name, "%s-%d.pgx", outfile, compno);
794 sprintf(name, "%s.pgx", outfile);
796 fdest = fopen(name, "wb");
798 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
801 // w = int_ceildiv(image->x1 - image->x0, comp->dx);
802 // wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx);
803 w = image->comps[compno].w;
804 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
806 // h = int_ceildiv(image->y1 - image->y0, comp->dy);
807 // hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy);
808 h = image->comps[compno].h;
809 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
811 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
812 if (comp->prec <= 8) {
814 } else if (comp->prec <= 16) {
819 for (i = 0; i < wr * hr; i++) {
820 int v = image->comps[compno].data[i / wr * w + i % wr];
821 for (j = nbytes - 1; j >= 0; j--) {
822 char byte = (char) (v >> (j * 8));
823 fwrite(&byte, 1, 1, fdest);
832 /* -->> -->> -->> -->>
836 <<-- <<-- <<-- <<-- */
838 opj_image_t* pnmtoimage(char *filename, opj_cparameters_t *parameters) {
839 int tdy = parameters->cp_tdy;
840 int subsampling_dx = parameters->subsampling_dx;
841 int subsampling_dy = parameters->subsampling_dy;
844 int i, compno, numcomps, w, h;
845 OPJ_COLOR_SPACE color_space;
846 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
847 opj_image_t * image = NULL;
851 f = fopen(filename, "rb");
853 fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
862 case '2': /* greyscale image type */
866 color_space = CLRSPC_GRAY;
870 if (fgetc(f) == '#') {
872 fseek(f, 0, SEEK_SET);
875 } else if (value == '5') {
878 fgets(comment, 256, f);
879 fscanf(f, "%d %d\n255", &w, &h);
881 fseek(f, 0, SEEK_SET);
883 fscanf(f, "P2\n%d %d\n255", &w, &h);
884 } else if (value == '5') {
885 fscanf(f, "P5\n%d %d\n255", &w, &h);
889 fgetc(f); /* <cr><lf> */
893 case '3': /* RGB image type */
897 color_space = CLRSPC_SRGB;
901 if (fgetc(f) == '#') {
903 fseek(f, 0, SEEK_SET);
906 } else if (value == '6') {
909 fgets(comment, 256, f);
910 fscanf(f, "%d %d\n255", &w, &h);
912 fseek(f, 0, SEEK_SET);
914 fscanf(f, "P3\n%d %d\n255", &w, &h);
915 } else if (value == '6') {
916 fscanf(f, "P6\n%d %d\n255", &w, &h);
920 fgetc(f); /* <cr><lf> */
929 /* initialize image components */
930 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
931 for(i = 0; i < numcomps; i++) {
932 cmptparm[i].prec = 8;
934 cmptparm[i].sgnd = 0;
935 cmptparm[i].dx = subsampling_dx;
936 cmptparm[i].dy = subsampling_dy;
940 /* create the image */
941 image = opj_image_create(numcomps, &cmptparm[0], color_space);
947 /* set image offset and reference grid */
948 image->x0 = parameters->image_offset_x0;
949 image->y0 = parameters->image_offset_y0;
950 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
951 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
955 if ((value == '2') || (value == '3')) { /* ASCII */
956 for (i = 0; i < w * h; i++) {
957 for(compno = 0; compno < numcomps; compno++) {
958 unsigned int index = 0;
959 fscanf(f, "%d", &index);
960 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
961 image->comps[compno].data[i] = index;
964 } else if ((value == '5') || (value == '6')) { /* BINARY */
965 for (i = 0; i < w * h; i++) {
966 for(compno = 0; compno < numcomps; compno++) {
967 unsigned char index = 0;
968 fread(&index, 1, 1, f);
969 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
970 image->comps[compno].data[i] = index;
980 int imagetopnm(opj_image_t * image, char *outfile) {
981 int w, wr, wrr, h, hr, hrr, max;
995 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
996 && image->comps[1].dx == image->comps[2].dx
997 && image->comps[0].dy == image->comps[1].dy
998 && image->comps[1].dy == image->comps[2].dy
999 && image->comps[0].prec == image->comps[1].prec
1000 && image->comps[1].prec == image->comps[2].prec
1001 && S2 !='g' && S2 !='G') {
1003 fdest = fopen(outfile, "wb");
1005 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1009 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
1010 // wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx);
1011 wr = image->comps[0].w;
1012 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
1014 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
1015 // hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy);
1016 hr = image->comps[0].h;
1017 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
1019 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
1021 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
1022 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
1024 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
1025 adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
1026 for (i = 0; i < wrr * hrr; i++) {
1028 unsigned char rc,gc,bc;
1029 r = image->comps[0].data[i / wrr * wr + i % wrr];
1030 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1031 rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
1033 g = image->comps[1].data[i / wrr * wr + i % wrr];
1034 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1035 gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
1037 b = image->comps[2].data[i / wrr * wr + i % wrr];
1038 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1039 bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
1041 fprintf(fdest, "%c%c%c", rc, gc, bc);
1045 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1046 if (image->numcomps>ncomp) {
1047 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1048 fprintf(stderr," is written to the file\n");
1050 for (compno = 0; compno < ncomp; compno++) {
1053 sprintf(name, "%d.%s", compno, outfile);
1055 sprintf(name, "%s", outfile);
1058 fdest = fopen(name, "wb");
1060 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1064 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1065 // wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx);
1066 wr = image->comps[compno].w;
1067 wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1069 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1070 // hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy);
1071 hr = image->comps[compno].h;
1072 hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1074 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1076 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1077 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1079 fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1080 adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1082 for (i = 0; i < wrr * hrr; i++) {
1085 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1086 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1087 lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1088 fprintf(fdest, "%c", lc);