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.
36 * Get logarithm of an integer and round downwards.
40 static int int_floorlog2(int a) {
42 for (l = 0; a > 1; l++) {
49 * Divide an integer by a power of 2 and round upwards.
53 static int int_ceildivpow2(int a, int b) {
54 return (a + (1 << b) - 1) >> b;
58 * Divide an integer and round upwards.
62 static int int_ceildiv(int a, int b) {
63 return (a + b - 1) / b;
66 /* -->> -->> -->> -->>
70 <<-- <<-- <<-- <<-- */
72 /* WORD defines a two byte word */
73 typedef unsigned short int WORD;
75 /* DWORD defines a four byte word */
76 typedef unsigned long int DWORD;
79 WORD bfType; /* 'BM' for Bitmap (19776) */
80 DWORD bfSize; /* Size of the file */
81 WORD bfReserved1; /* Reserved : 0 */
82 WORD bfReserved2; /* Reserved : 0 */
83 DWORD bfOffBits; /* Offset */
87 DWORD biSize; /* Size of the structure in bytes */
88 DWORD biWidth; /* Width of the image in pixels */
89 DWORD biHeight; /* Heigth of the image in pixels */
90 WORD biPlanes; /* 1 */
91 WORD biBitCount; /* Number of color bits by pixels */
92 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
93 DWORD biSizeImage; /* Size of the image in bytes */
94 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
95 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
96 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
97 DWORD biClrImportant; /* Number of important color (0: ALL) */
100 opj_image_t* bmptoimage(char *filename, opj_cparameters_t *parameters) {
101 int subsampling_dx = parameters->subsampling_dx;
102 int subsampling_dy = parameters->subsampling_dy;
104 int i, numcomps, w, h;
105 OPJ_COLOR_SPACE color_space;
106 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
107 opj_image_t * image = NULL;
110 BITMAPFILEHEADER_t File_h;
111 BITMAPINFOHEADER_t Info_h;
113 unsigned char *table_R, *table_G, *table_B;
114 unsigned int j, PAD = 0;
117 int gray_scale = 1, not_end_file = 1;
119 unsigned int line = 0, col = 0;
123 IN = fopen(filename, "rb");
125 fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
129 File_h.bfType = getc(IN);
130 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
132 if (File_h.bfType != 19778) {
133 fprintf(stderr,"Error, not a BMP file!\n");
138 File_h.bfSize = getc(IN);
139 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
140 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
141 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
143 File_h.bfReserved1 = getc(IN);
144 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
146 File_h.bfReserved2 = getc(IN);
147 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
149 File_h.bfOffBits = getc(IN);
150 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
151 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
152 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
157 Info_h.biSize = getc(IN);
158 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
159 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
160 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
162 Info_h.biWidth = getc(IN);
163 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
164 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
165 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
168 Info_h.biHeight = getc(IN);
169 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
170 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
171 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
174 Info_h.biPlanes = getc(IN);
175 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
177 Info_h.biBitCount = getc(IN);
178 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
180 Info_h.biCompression = getc(IN);
181 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
182 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
183 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
185 Info_h.biSizeImage = getc(IN);
186 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
187 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
188 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
190 Info_h.biXpelsPerMeter = getc(IN);
191 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
192 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
193 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
195 Info_h.biYpelsPerMeter = getc(IN);
196 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
197 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
198 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
200 Info_h.biClrUsed = getc(IN);
201 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
202 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
203 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
205 Info_h.biClrImportant = getc(IN);
206 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
207 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
208 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
210 /* Read the data and store them in the OUT file */
212 if (Info_h.biBitCount == 24) {
214 color_space = CLRSPC_SRGB;
215 /* initialize image components */
216 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
217 for(i = 0; i < numcomps; i++) {
218 cmptparm[i].prec = 8;
220 cmptparm[i].sgnd = 0;
221 cmptparm[i].dx = subsampling_dx;
222 cmptparm[i].dy = subsampling_dy;
226 /* create the image */
227 image = opj_image_create(numcomps, &cmptparm[0], color_space);
233 /* set image offset and reference grid */
234 image->x0 = parameters->image_offset_x0;
235 image->y0 = parameters->image_offset_y0;
236 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
237 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
241 /* Place the cursor at the beginning of the image information */
242 fseek(IN, 0, SEEK_SET);
243 fseek(IN, File_h.bfOffBits, SEEK_SET);
248 /* PAD = 4 - (3 * W) % 4; */
249 /* PAD = (PAD == 4) ? 0 : PAD; */
250 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
252 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
254 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
258 for(y = 0; y < (int)H; y++) {
259 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
260 for(x = 0; x < (int)W; x++) {
261 unsigned char *pixel = &scanline[3 * x];
262 image->comps[0].data[index] = pixel[2]; /* R */
263 image->comps[1].data[index] = pixel[1]; /* G */
264 image->comps[2].data[index] = pixel[0]; /* B */
271 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
272 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
273 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
274 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
276 for (j = 0; j < Info_h.biClrUsed; j++) {
277 table_B[j] = getc(IN);
278 table_G[j] = getc(IN);
279 table_R[j] = getc(IN);
281 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
285 /* Place the cursor at the beginning of the image information */
286 fseek(IN, 0, SEEK_SET);
287 fseek(IN, File_h.bfOffBits, SEEK_SET);
291 if (Info_h.biWidth % 2)
294 numcomps = gray_scale ? 1 : 3;
295 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
296 /* initialize image components */
297 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
298 for(i = 0; i < numcomps; i++) {
299 cmptparm[i].prec = 8;
301 cmptparm[i].sgnd = 0;
302 cmptparm[i].dx = subsampling_dx;
303 cmptparm[i].dy = subsampling_dy;
307 /* create the image */
308 image = opj_image_create(numcomps, &cmptparm[0], color_space);
314 /* set image offset and reference grid */
315 image->x0 = parameters->image_offset_x0;
316 image->y0 = parameters->image_offset_y0;
317 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
318 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
322 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
324 fread(RGB, sizeof(unsigned char), W * H, IN);
327 for (j = 0; j < W * H; j++) {
328 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
329 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
336 for (j = 0; j < W * H; j++) {
337 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
338 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
339 image->comps[0].data[index] = table_R[pixel_index];
340 image->comps[1].data[index] = table_G[pixel_index];
341 image->comps[2].data[index] = table_B[pixel_index];
348 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
349 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
350 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
351 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
353 for (j = 0; j < Info_h.biClrUsed; j++) {
354 table_B[j] = getc(IN);
355 table_G[j] = getc(IN);
356 table_R[j] = getc(IN);
358 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
362 numcomps = gray_scale ? 1 : 3;
363 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
364 /* initialize image components */
365 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
366 for(i = 0; i < numcomps; i++) {
367 cmptparm[i].prec = 8;
369 cmptparm[i].sgnd = 0;
370 cmptparm[i].dx = subsampling_dx;
371 cmptparm[i].dy = subsampling_dy;
375 /* create the image */
376 image = opj_image_create(numcomps, &cmptparm[0], color_space);
382 /* set image offset and reference grid */
383 image->x0 = parameters->image_offset_x0;
384 image->y0 = parameters->image_offset_y0;
385 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
386 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
390 /* Place the cursor at the beginning of the image information */
391 fseek(IN, 0, SEEK_SET);
392 fseek(IN, File_h.bfOffBits, SEEK_SET);
394 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
396 while (not_end_file) {
400 for (i = 0; i < (int) v; i++) {
401 RGB[line * Info_h.biWidth + col] = v2;
416 fprintf(stderr,"No Delta supported\n");
417 opj_image_destroy(image);
421 for (i = 0; i < v; i++) {
423 RGB[line * Info_h.biWidth + col] = v2;
434 for (line = 0; line < Info_h.biHeight; line++) {
435 for (col = 0; col < Info_h.biWidth; col++) {
436 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
442 for (line = 0; line < Info_h.biHeight; line++) {
443 for (col = 0; col < Info_h.biWidth; col++) {
444 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
445 image->comps[0].data[index] = table_R[pixel_index];
446 image->comps[1].data[index] = table_G[pixel_index];
447 image->comps[2].data[index] = table_B[pixel_index];
455 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
463 int imagetobmp(opj_image_t * image, char *outfile) {
468 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
469 && image->comps[1].dx == image->comps[2].dx
470 && image->comps[0].dy == image->comps[1].dy
471 && image->comps[1].dy == image->comps[2].dy
472 && image->comps[0].prec == image->comps[1].prec
473 && image->comps[1].prec == image->comps[2].prec) {
475 /* -->> -->> -->> -->>
477 <<-- <<-- <<-- <<-- */
479 fdest = fopen(outfile, "wb");
481 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
485 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
486 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
487 w = image->comps[0].w;
488 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
490 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
491 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
492 h = image->comps[0].h;
493 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
495 fprintf(fdest, "BM");
499 fprintf(fdest, "%c%c%c%c",
500 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
501 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
502 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
503 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
504 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
505 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
509 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
510 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
511 (unsigned char) ((wr) >> 8) & 0xff,
512 (unsigned char) ((wr) >> 16) & 0xff,
513 (unsigned char) ((wr) >> 24) & 0xff);
514 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
515 (unsigned char) ((hr) >> 8) & 0xff,
516 (unsigned char) ((hr) >> 16) & 0xff,
517 (unsigned char) ((hr) >> 24) & 0xff);
518 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
519 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
520 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
521 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
522 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
523 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
524 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
525 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
526 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
527 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
528 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
530 for (i = 0; i < wr * hr; i++) {
531 unsigned char R, G, B;
533 /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
534 R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
535 /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
536 G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
537 /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
538 B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
539 fprintf(fdest, "%c%c%c", B, G, R);
541 if ((i + 1) % wr == 0) {
542 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
543 fprintf(fdest, "%c", 0);
547 } else { /* Gray-scale */
549 /* -->> -->> -->> -->>
550 8 bits non code (Gray scale)
551 <<-- <<-- <<-- <<-- */
553 fdest = fopen(outfile, "wb");
554 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
555 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
556 w = image->comps[0].w;
557 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
559 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
560 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
561 h = image->comps[0].h;
562 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
564 fprintf(fdest, "BM");
568 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
569 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
570 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
571 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
572 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
573 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
574 ((54 + 1024) >> 16) & 0xff,
575 ((54 + 1024) >> 24) & 0xff);
579 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
580 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
581 (unsigned char) ((wr) >> 8) & 0xff,
582 (unsigned char) ((wr) >> 16) & 0xff,
583 (unsigned char) ((wr) >> 24) & 0xff);
584 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
585 (unsigned char) ((hr) >> 8) & 0xff,
586 (unsigned char) ((hr) >> 16) & 0xff,
587 (unsigned char) ((hr) >> 24) & 0xff);
588 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
589 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
590 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
591 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
592 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
593 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
594 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
595 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
596 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
597 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
598 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
600 for (i = 0; i < 256; i++) {
601 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
604 for (i = 0; i < wr * hr; i++) {
606 /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
607 fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
608 /*if (((i + 1) % w == 0 && w % 2))
609 fprintf(fdest, "%c", 0); */
610 if ((i + 1) % wr == 0) {
611 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
612 fprintf(fdest, "%c", 0);
621 /* -->> -->> -->> -->>
625 <<-- <<-- <<-- <<-- */
628 unsigned char readuchar(FILE * f)
635 unsigned short readushort(FILE * f, int bigendian)
637 unsigned char c1, c2;
641 return (c1 << 8) + c2;
643 return (c2 << 8) + c1;
646 unsigned int readuint(FILE * f, int bigendian)
648 unsigned char c1, c2, c3, c4;
654 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
656 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
659 opj_image_t* pgxtoimage(char *filename, opj_cparameters_t *parameters) {
662 int i, numcomps, max;
663 OPJ_COLOR_SPACE color_space;
664 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
665 opj_image_t * image = NULL;
667 char endian1,endian2,sign;
672 opj_image_comp_t *comp = NULL;
675 color_space = CLRSPC_GRAY;
677 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
681 f = fopen(filename, "rb");
683 fprintf(stderr, "Failed to open %s for reading !\n", filename);
687 fseek(f, 0, SEEK_SET);
688 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
692 while (signtmp[i]!='\0') {
693 if (signtmp[i]=='-') sign='-';
698 if (endian1=='M' && endian2=='L') {
700 } else if (endian2=='M' && endian1=='L') {
703 fprintf(stderr, "Bad pgx header, please check input file\n");
707 /* initialize image component */
709 cmptparm.x0 = parameters->image_offset_x0;
710 cmptparm.y0 = parameters->image_offset_y0;
711 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
712 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
719 cmptparm.prec = prec;
721 cmptparm.dx = parameters->subsampling_dx;
722 cmptparm.dy = parameters->subsampling_dy;
724 /* create the image */
725 image = opj_image_create(numcomps, &cmptparm, color_space);
730 /* set image offset and reference grid */
731 image->x0 = cmptparm.x0;
732 image->y0 = cmptparm.x0;
733 image->x1 = cmptparm.w;
734 image->y1 = cmptparm.h;
738 comp = &image->comps[0];
740 for (i = 0; i < w * h; i++) {
742 if (comp->prec <= 8) {
746 v = (char) readuchar(f);
748 } else if (comp->prec <= 16) {
750 v = readushort(f, bigendian);
752 v = (short) readushort(f, bigendian);
756 v = readuint(f, bigendian);
758 v = (int) readuint(f, bigendian);
766 comp->bpp = int_floorlog2(max) + 1;
771 int imagetopgx(opj_image_t * image, char *outfile) {
776 for (compno = 0; compno < image->numcomps; compno++) {
777 opj_image_comp_t *comp = &image->comps[compno];
789 if (image->numcomps > 1) {
790 sprintf(name, "%s-%d.pgx", outfile, compno);
792 sprintf(name, "%s.pgx", outfile);
794 fdest = fopen(name, "wb");
796 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
799 /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */
800 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */
801 w = image->comps[compno].w;
802 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
804 /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */
805 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */
806 h = image->comps[compno].h;
807 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
809 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
810 if (comp->prec <= 8) {
812 } else if (comp->prec <= 16) {
817 for (i = 0; i < wr * hr; i++) {
818 int v = image->comps[compno].data[i / wr * w + i % wr];
819 for (j = nbytes - 1; j >= 0; j--) {
820 char byte = (char) (v >> (j * 8));
821 fwrite(&byte, 1, 1, fdest);
830 /* -->> -->> -->> -->>
834 <<-- <<-- <<-- <<-- */
836 opj_image_t* pnmtoimage(char *filename, opj_cparameters_t *parameters) {
837 int subsampling_dx = parameters->subsampling_dx;
838 int subsampling_dy = parameters->subsampling_dy;
841 int i, compno, numcomps, w, h;
842 OPJ_COLOR_SPACE color_space;
843 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
844 opj_image_t * image = NULL;
847 f = fopen(filename, "rb");
849 fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
858 case '2': /* greyscale image type */
861 color_space = CLRSPC_GRAY;
864 case '3': /* RGB image type */
867 color_space = CLRSPC_SRGB;
878 while(fgetc(f) == '#') while(fgetc(f) != '\n');
880 fseek(f, -1, SEEK_CUR);
881 fscanf(f, "%d %d\n255", &w, &h);
882 fgetc(f); /* <cr><lf> */
884 /* initialize image components */
885 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
886 for(i = 0; i < numcomps; i++) {
887 cmptparm[i].prec = 8;
889 cmptparm[i].sgnd = 0;
890 cmptparm[i].dx = subsampling_dx;
891 cmptparm[i].dy = subsampling_dy;
895 /* create the image */
896 image = opj_image_create(numcomps, &cmptparm[0], color_space);
902 /* set image offset and reference grid */
903 image->x0 = parameters->image_offset_x0;
904 image->y0 = parameters->image_offset_y0;
905 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
906 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
910 if ((value == '2') || (value == '3')) { /* ASCII */
911 for (i = 0; i < w * h; i++) {
912 for(compno = 0; compno < numcomps; compno++) {
913 unsigned int index = 0;
914 fscanf(f, "%u", &index);
915 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
916 image->comps[compno].data[i] = index;
919 } else if ((value == '5') || (value == '6')) { /* BINARY */
920 for (i = 0; i < w * h; i++) {
921 for(compno = 0; compno < numcomps; compno++) {
922 unsigned char index = 0;
923 fread(&index, 1, 1, f);
924 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
925 image->comps[compno].data[i] = index;
935 int imagetopnm(opj_image_t * image, char *outfile) {
936 int w, wr, wrr, h, hr, hrr, max;
950 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
951 && image->comps[1].dx == image->comps[2].dx
952 && image->comps[0].dy == image->comps[1].dy
953 && image->comps[1].dy == image->comps[2].dy
954 && image->comps[0].prec == image->comps[1].prec
955 && image->comps[1].prec == image->comps[2].prec
956 && S2 !='g' && S2 !='G') {
958 fdest = fopen(outfile, "wb");
960 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
964 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
965 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */
966 wr = image->comps[0].w;
967 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
969 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
970 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
971 hr = image->comps[0].h;
972 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
974 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
976 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
977 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
979 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
980 adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
981 for (i = 0; i < wrr * hrr; i++) {
983 unsigned char rc,gc,bc;
984 r = image->comps[0].data[i / wrr * wr + i % wrr];
985 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
986 rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
988 g = image->comps[1].data[i / wrr * wr + i % wrr];
989 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
990 gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
992 b = image->comps[2].data[i / wrr * wr + i % wrr];
993 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
994 bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
996 fprintf(fdest, "%c%c%c", rc, gc, bc);
1000 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1001 if (image->numcomps>ncomp) {
1002 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1003 fprintf(stderr," is written to the file\n");
1005 for (compno = 0; compno < ncomp; compno++) {
1008 sprintf(name, "%d.%s", compno, outfile);
1010 sprintf(name, "%s", outfile);
1013 fdest = fopen(name, "wb");
1015 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1019 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1020 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */
1021 wr = image->comps[compno].w;
1022 wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1024 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1025 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */
1026 hr = image->comps[compno].h;
1027 hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1029 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1031 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1032 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1034 fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1035 adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1037 for (i = 0; i < wrr * hrr; i++) {
1040 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1041 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1042 lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1043 fprintf(fdest, "%c", lc);