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);
422 for (i = 0; i < v; i++) {
424 RGB[line * Info_h.biWidth + col] = v2;
435 for (line = 0; line < Info_h.biHeight; line++) {
436 for (col = 0; col < Info_h.biWidth; col++) {
437 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
443 for (line = 0; line < Info_h.biHeight; line++) {
444 for (col = 0; col < Info_h.biWidth; col++) {
445 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
446 image->comps[0].data[index] = table_R[pixel_index];
447 image->comps[1].data[index] = table_G[pixel_index];
448 image->comps[2].data[index] = table_B[pixel_index];
456 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
464 int imagetobmp(opj_image_t * image, char *outfile) {
469 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
470 && image->comps[1].dx == image->comps[2].dx
471 && image->comps[0].dy == image->comps[1].dy
472 && image->comps[1].dy == image->comps[2].dy
473 && image->comps[0].prec == image->comps[1].prec
474 && image->comps[1].prec == image->comps[2].prec) {
476 /* -->> -->> -->> -->>
478 <<-- <<-- <<-- <<-- */
480 fdest = fopen(outfile, "wb");
482 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
486 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
487 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
488 w = image->comps[0].w;
489 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
491 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
492 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
493 h = image->comps[0].h;
494 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
496 fprintf(fdest, "BM");
500 fprintf(fdest, "%c%c%c%c",
501 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
502 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
503 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
504 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
505 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
506 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
510 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
511 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
512 (unsigned char) ((wr) >> 8) & 0xff,
513 (unsigned char) ((wr) >> 16) & 0xff,
514 (unsigned char) ((wr) >> 24) & 0xff);
515 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
516 (unsigned char) ((hr) >> 8) & 0xff,
517 (unsigned char) ((hr) >> 16) & 0xff,
518 (unsigned char) ((hr) >> 24) & 0xff);
519 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
520 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
521 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
522 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
523 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
524 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
525 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
528 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
529 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
531 for (i = 0; i < wr * hr; i++) {
532 unsigned char R, G, B;
534 /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
535 R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
536 /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
537 G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
538 /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
539 B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
540 fprintf(fdest, "%c%c%c", B, G, R);
542 if ((i + 1) % wr == 0) {
543 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
544 fprintf(fdest, "%c", 0);
548 } else { /* Gray-scale */
550 /* -->> -->> -->> -->>
551 8 bits non code (Gray scale)
552 <<-- <<-- <<-- <<-- */
554 fdest = fopen(outfile, "wb");
555 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
556 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
557 w = image->comps[0].w;
558 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
560 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
561 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
562 h = image->comps[0].h;
563 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
565 fprintf(fdest, "BM");
569 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
570 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
571 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
572 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
573 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
574 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
575 ((54 + 1024) >> 16) & 0xff,
576 ((54 + 1024) >> 24) & 0xff);
580 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
581 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
582 (unsigned char) ((wr) >> 8) & 0xff,
583 (unsigned char) ((wr) >> 16) & 0xff,
584 (unsigned char) ((wr) >> 24) & 0xff);
585 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
586 (unsigned char) ((hr) >> 8) & 0xff,
587 (unsigned char) ((hr) >> 16) & 0xff,
588 (unsigned char) ((hr) >> 24) & 0xff);
589 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
590 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
591 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
592 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
593 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
594 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
595 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
598 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
599 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
602 for (i = 0; i < 256; i++) {
603 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
606 for (i = 0; i < wr * hr; i++) {
608 /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
609 fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
610 /*if (((i + 1) % w == 0 && w % 2))
611 fprintf(fdest, "%c", 0); */
612 if ((i + 1) % wr == 0) {
613 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
614 fprintf(fdest, "%c", 0);
622 /* -->> -->> -->> -->>
626 <<-- <<-- <<-- <<-- */
629 unsigned char readuchar(FILE * f)
636 unsigned short readushort(FILE * f, int bigendian)
638 unsigned char c1, c2;
642 return (c1 << 8) + c2;
644 return (c2 << 8) + c1;
647 unsigned int readuint(FILE * f, int bigendian)
649 unsigned char c1, c2, c3, c4;
655 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
657 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
660 opj_image_t* pgxtoimage(char *filename, opj_cparameters_t *parameters) {
663 int i, numcomps, max;
664 OPJ_COLOR_SPACE color_space;
665 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
666 opj_image_t * image = NULL;
668 char endian1,endian2,sign;
673 opj_image_comp_t *comp = NULL;
676 color_space = CLRSPC_GRAY;
678 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
682 f = fopen(filename, "rb");
684 fprintf(stderr, "Failed to open %s for reading !\n", filename);
688 fseek(f, 0, SEEK_SET);
689 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
693 while (signtmp[i]!='\0') {
694 if (signtmp[i]=='-') sign='-';
699 if (endian1=='M' && endian2=='L') {
701 } else if (endian2=='M' && endian1=='L') {
704 fprintf(stderr, "Bad pgx header, please check input file\n");
708 /* initialize image component */
710 cmptparm.x0 = parameters->image_offset_x0;
711 cmptparm.y0 = parameters->image_offset_y0;
712 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
713 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
720 cmptparm.prec = prec;
722 cmptparm.dx = parameters->subsampling_dx;
723 cmptparm.dy = parameters->subsampling_dy;
725 /* create the image */
726 image = opj_image_create(numcomps, &cmptparm, color_space);
731 /* set image offset and reference grid */
732 image->x0 = cmptparm.x0;
733 image->y0 = cmptparm.x0;
734 image->x1 = cmptparm.w;
735 image->y1 = cmptparm.h;
739 comp = &image->comps[0];
741 for (i = 0; i < w * h; i++) {
743 if (comp->prec <= 8) {
747 v = (char) readuchar(f);
749 } else if (comp->prec <= 16) {
751 v = readushort(f, bigendian);
753 v = (short) readushort(f, bigendian);
757 v = readuint(f, bigendian);
759 v = (int) readuint(f, bigendian);
767 comp->bpp = int_floorlog2(max) + 1;
772 int imagetopgx(opj_image_t * image, char *outfile) {
777 for (compno = 0; compno < image->numcomps; compno++) {
778 opj_image_comp_t *comp = &image->comps[compno];
790 if (image->numcomps > 1) {
791 sprintf(name, "%s-%d.pgx", outfile, compno);
793 sprintf(name, "%s.pgx", outfile);
795 fdest = fopen(name, "wb");
797 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
800 /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */
801 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */
802 w = image->comps[compno].w;
803 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
805 /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */
806 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */
807 h = image->comps[compno].h;
808 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
810 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
811 if (comp->prec <= 8) {
813 } else if (comp->prec <= 16) {
818 for (i = 0; i < wr * hr; i++) {
819 int v = image->comps[compno].data[i / wr * w + i % wr];
820 for (j = nbytes - 1; j >= 0; j--) {
821 char byte = (char) (v >> (j * 8));
822 fwrite(&byte, 1, 1, fdest);
831 /* -->> -->> -->> -->>
835 <<-- <<-- <<-- <<-- */
837 opj_image_t* pnmtoimage(char *filename, opj_cparameters_t *parameters) {
838 int subsampling_dx = parameters->subsampling_dx;
839 int subsampling_dy = parameters->subsampling_dy;
842 int i, compno, numcomps, w, h;
843 OPJ_COLOR_SPACE color_space;
844 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
845 opj_image_t * image = NULL;
848 f = fopen(filename, "rb");
850 fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
859 case '2': /* greyscale image type */
862 color_space = CLRSPC_GRAY;
865 case '3': /* RGB image type */
868 color_space = CLRSPC_SRGB;
879 while(fgetc(f) == '#') while(fgetc(f) != '\n');
881 fseek(f, -1, SEEK_CUR);
882 fscanf(f, "%d %d\n255", &w, &h);
883 fgetc(f); /* <cr><lf> */
885 /* initialize image components */
886 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
887 for(i = 0; i < numcomps; i++) {
888 cmptparm[i].prec = 8;
890 cmptparm[i].sgnd = 0;
891 cmptparm[i].dx = subsampling_dx;
892 cmptparm[i].dy = subsampling_dy;
896 /* create the image */
897 image = opj_image_create(numcomps, &cmptparm[0], color_space);
903 /* set image offset and reference grid */
904 image->x0 = parameters->image_offset_x0;
905 image->y0 = parameters->image_offset_y0;
906 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
907 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
911 if ((value == '2') || (value == '3')) { /* ASCII */
912 for (i = 0; i < w * h; i++) {
913 for(compno = 0; compno < numcomps; compno++) {
914 unsigned int index = 0;
915 fscanf(f, "%u", &index);
916 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
917 image->comps[compno].data[i] = index;
920 } else if ((value == '5') || (value == '6')) { /* BINARY */
921 for (i = 0; i < w * h; i++) {
922 for(compno = 0; compno < numcomps; compno++) {
923 unsigned char index = 0;
924 fread(&index, 1, 1, f);
925 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
926 image->comps[compno].data[i] = index;
936 int imagetopnm(opj_image_t * image, char *outfile) {
937 int w, wr, wrr, h, hr, hrr, max;
951 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
952 && image->comps[1].dx == image->comps[2].dx
953 && image->comps[0].dy == image->comps[1].dy
954 && image->comps[1].dy == image->comps[2].dy
955 && image->comps[0].prec == image->comps[1].prec
956 && image->comps[1].prec == image->comps[2].prec
957 && S2 !='g' && S2 !='G') {
959 fdest = fopen(outfile, "wb");
961 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
965 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
966 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */
967 wr = image->comps[0].w;
968 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
970 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
971 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
972 hr = image->comps[0].h;
973 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
975 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
977 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
978 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
980 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
981 adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
982 for (i = 0; i < wrr * hrr; i++) {
984 unsigned char rc,gc,bc;
985 r = image->comps[0].data[i / wrr * wr + i % wrr];
986 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
987 rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
989 g = image->comps[1].data[i / wrr * wr + i % wrr];
990 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
991 gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
993 b = image->comps[2].data[i / wrr * wr + i % wrr];
994 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
995 bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
997 fprintf(fdest, "%c%c%c", rc, gc, bc);
1001 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1002 if (image->numcomps>ncomp) {
1003 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1004 fprintf(stderr," is written to the file\n");
1006 for (compno = 0; compno < ncomp; compno++) {
1009 sprintf(name, "%d.%s", compno, outfile);
1011 sprintf(name, "%s", outfile);
1014 fdest = fopen(name, "wb");
1016 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1020 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1021 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */
1022 wr = image->comps[compno].w;
1023 wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1025 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1026 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */
1027 hr = image->comps[compno].h;
1028 hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1030 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1032 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1033 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1035 fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1036 adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1038 for (i = 0; i < wrr * hrr; i++) {
1041 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1042 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1043 lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1044 fprintf(fdest, "%c", lc);