2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * 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(const 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 = 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, "Failed to open %s for reading !!\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];
351 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
352 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
353 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
354 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
356 for (j = 0; j < Info_h.biClrUsed; j++) {
357 table_B[j] = getc(IN);
358 table_G[j] = getc(IN);
359 table_R[j] = getc(IN);
361 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
365 numcomps = gray_scale ? 1 : 3;
366 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
367 /* initialize image components */
368 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
369 for(i = 0; i < numcomps; i++) {
370 cmptparm[i].prec = 8;
372 cmptparm[i].sgnd = 0;
373 cmptparm[i].dx = subsampling_dx;
374 cmptparm[i].dy = subsampling_dy;
378 /* create the image */
379 image = opj_image_create(numcomps, &cmptparm[0], color_space);
385 /* set image offset and reference grid */
386 image->x0 = parameters->image_offset_x0;
387 image->y0 = parameters->image_offset_y0;
388 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
389 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
393 /* Place the cursor at the beginning of the image information */
394 fseek(IN, 0, SEEK_SET);
395 fseek(IN, File_h.bfOffBits, SEEK_SET);
397 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
399 while (not_end_file) {
403 for (i = 0; i < (int) v; i++) {
404 RGB[line * Info_h.biWidth + col] = v2;
419 fprintf(stderr,"No Delta supported\n");
420 opj_image_destroy(image);
424 for (i = 0; i < v; i++) {
426 RGB[line * Info_h.biWidth + col] = v2;
437 for (line = 0; line < Info_h.biHeight; line++) {
438 for (col = 0; col < Info_h.biWidth; col++) {
439 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
445 for (line = 0; line < Info_h.biHeight; line++) {
446 for (col = 0; col < Info_h.biWidth; col++) {
447 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
448 image->comps[0].data[index] = table_R[pixel_index];
449 image->comps[1].data[index] = table_G[pixel_index];
450 image->comps[2].data[index] = table_B[pixel_index];
461 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
469 int imagetobmp(opj_image_t * image, const char *outfile) {
474 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
475 && image->comps[1].dx == image->comps[2].dx
476 && image->comps[0].dy == image->comps[1].dy
477 && image->comps[1].dy == image->comps[2].dy
478 && image->comps[0].prec == image->comps[1].prec
479 && image->comps[1].prec == image->comps[2].prec) {
481 /* -->> -->> -->> -->>
483 <<-- <<-- <<-- <<-- */
485 fdest = fopen(outfile, "wb");
487 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
491 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
492 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
493 w = image->comps[0].w;
494 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
496 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
497 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
498 h = image->comps[0].h;
499 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
501 fprintf(fdest, "BM");
505 fprintf(fdest, "%c%c%c%c",
506 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
507 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
508 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
509 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
510 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
511 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
515 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
516 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
517 (unsigned char) ((wr) >> 8) & 0xff,
518 (unsigned char) ((wr) >> 16) & 0xff,
519 (unsigned char) ((wr) >> 24) & 0xff);
520 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
521 (unsigned char) ((hr) >> 8) & 0xff,
522 (unsigned char) ((hr) >> 16) & 0xff,
523 (unsigned char) ((hr) >> 24) & 0xff);
524 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
525 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
526 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
527 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
528 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
529 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
530 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
531 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
532 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
533 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
534 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
536 for (i = 0; i < wr * hr; i++) {
537 unsigned char R, G, B;
539 /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
540 R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
541 /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
542 G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
543 /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
544 B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
545 fprintf(fdest, "%c%c%c", B, G, R);
547 if ((i + 1) % wr == 0) {
548 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
549 fprintf(fdest, "%c", 0);
553 } else { /* Gray-scale */
555 /* -->> -->> -->> -->>
556 8 bits non code (Gray scale)
557 <<-- <<-- <<-- <<-- */
559 fdest = fopen(outfile, "wb");
560 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
561 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
562 w = image->comps[0].w;
563 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
565 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
566 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
567 h = image->comps[0].h;
568 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
570 fprintf(fdest, "BM");
574 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
575 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
576 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
577 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
578 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
579 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
580 ((54 + 1024) >> 16) & 0xff,
581 ((54 + 1024) >> 24) & 0xff);
585 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
586 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
587 (unsigned char) ((wr) >> 8) & 0xff,
588 (unsigned char) ((wr) >> 16) & 0xff,
589 (unsigned char) ((wr) >> 24) & 0xff);
590 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
591 (unsigned char) ((hr) >> 8) & 0xff,
592 (unsigned char) ((hr) >> 16) & 0xff,
593 (unsigned char) ((hr) >> 24) & 0xff);
594 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
595 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
596 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
597 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
598 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
599 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
600 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
601 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
602 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
603 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
604 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
606 for (i = 0; i < 256; i++) {
607 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
610 for (i = 0; i < wr * hr; i++) {
612 /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
613 fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
614 /*if (((i + 1) % w == 0 && w % 2))
615 fprintf(fdest, "%c", 0); */
616 if ((i + 1) % wr == 0) {
617 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
618 fprintf(fdest, "%c", 0);
627 /* -->> -->> -->> -->>
631 <<-- <<-- <<-- <<-- */
634 unsigned char readuchar(FILE * f)
641 unsigned short readushort(FILE * f, int bigendian)
643 unsigned char c1, c2;
647 return (c1 << 8) + c2;
649 return (c2 << 8) + c1;
652 unsigned int readuint(FILE * f, int bigendian)
654 unsigned char c1, c2, c3, c4;
660 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
662 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
665 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
668 int i, numcomps, max;
669 OPJ_COLOR_SPACE color_space;
670 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
671 opj_image_t * image = NULL;
673 char endian1,endian2,sign;
678 opj_image_comp_t *comp = NULL;
681 color_space = CLRSPC_GRAY;
683 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
687 f = fopen(filename, "rb");
689 fprintf(stderr, "Failed to open %s for reading !\n", filename);
693 fseek(f, 0, SEEK_SET);
694 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
698 while (signtmp[i]!='\0') {
699 if (signtmp[i]=='-') sign='-';
704 if (endian1=='M' && endian2=='L') {
706 } else if (endian2=='M' && endian1=='L') {
709 fprintf(stderr, "Bad pgx header, please check input file\n");
713 /* initialize image component */
715 cmptparm.x0 = parameters->image_offset_x0;
716 cmptparm.y0 = parameters->image_offset_y0;
717 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
718 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
725 cmptparm.prec = prec;
727 cmptparm.dx = parameters->subsampling_dx;
728 cmptparm.dy = parameters->subsampling_dy;
730 /* create the image */
731 image = opj_image_create(numcomps, &cmptparm, color_space);
736 /* set image offset and reference grid */
737 image->x0 = cmptparm.x0;
738 image->y0 = cmptparm.x0;
739 image->x1 = cmptparm.w;
740 image->y1 = cmptparm.h;
744 comp = &image->comps[0];
746 for (i = 0; i < w * h; i++) {
748 if (comp->prec <= 8) {
752 v = (char) readuchar(f);
754 } else if (comp->prec <= 16) {
756 v = readushort(f, bigendian);
758 v = (short) readushort(f, bigendian);
762 v = readuint(f, bigendian);
764 v = (int) readuint(f, bigendian);
772 comp->bpp = int_floorlog2(max) + 1;
777 int imagetopgx(opj_image_t * image, const char *outfile) {
782 for (compno = 0; compno < image->numcomps; compno++) {
783 opj_image_comp_t *comp = &image->comps[compno];
784 char bname[256]; /* buffer for name */
785 char *name = bname; /* pointer */
787 const size_t olen = strlen(outfile);
788 const size_t dotpos = olen - 4;
789 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
790 if( outfile[dotpos] != '.' ) {
791 /* `pgx` was recognized but there is no dot at expected position */
792 fprintf(stderr, "ERROR -> Impossible happen." );
796 name = (char*)malloc(total+1);
798 strncpy(name, outfile, dotpos);
799 if (image->numcomps > 1) {
800 sprintf(name+dotpos, "-%d.pgx", compno);
802 strcpy(name+dotpos, ".pgx");
804 fdest = fopen(name, "wb");
806 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
809 /* dont need name anymore */
813 /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */
814 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */
815 w = image->comps[compno].w;
816 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
818 /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */
819 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */
820 h = image->comps[compno].h;
821 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
823 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
824 if (comp->prec <= 8) {
826 } else if (comp->prec <= 16) {
831 for (i = 0; i < wr * hr; i++) {
832 int v = image->comps[compno].data[i / wr * w + i % wr];
833 for (j = nbytes - 1; j >= 0; j--) {
834 char byte = (char) (v >> (j * 8));
835 fwrite(&byte, 1, 1, fdest);
844 /* -->> -->> -->> -->>
848 <<-- <<-- <<-- <<-- */
850 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
851 int subsampling_dx = parameters->subsampling_dx;
852 int subsampling_dy = parameters->subsampling_dy;
855 int i, compno, numcomps, w, h;
856 OPJ_COLOR_SPACE color_space;
857 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
858 opj_image_t * image = NULL;
861 f = fopen(filename, "rb");
863 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
872 case '2': /* greyscale image type */
875 color_space = CLRSPC_GRAY;
878 case '3': /* RGB image type */
881 color_space = CLRSPC_SRGB;
892 while(fgetc(f) == '#') while(fgetc(f) != '\n');
894 fseek(f, -1, SEEK_CUR);
895 fscanf(f, "%d %d\n255", &w, &h);
896 fgetc(f); /* <cr><lf> */
898 /* initialize image components */
899 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
900 for(i = 0; i < numcomps; i++) {
901 cmptparm[i].prec = 8;
903 cmptparm[i].sgnd = 0;
904 cmptparm[i].dx = subsampling_dx;
905 cmptparm[i].dy = subsampling_dy;
909 /* create the image */
910 image = opj_image_create(numcomps, &cmptparm[0], color_space);
916 /* set image offset and reference grid */
917 image->x0 = parameters->image_offset_x0;
918 image->y0 = parameters->image_offset_y0;
919 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
920 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
924 if ((value == '2') || (value == '3')) { /* ASCII */
925 for (i = 0; i < w * h; i++) {
926 for(compno = 0; compno < numcomps; compno++) {
927 unsigned int index = 0;
928 fscanf(f, "%u", &index);
929 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
930 image->comps[compno].data[i] = index;
933 } else if ((value == '5') || (value == '6')) { /* BINARY */
934 for (i = 0; i < w * h; i++) {
935 for(compno = 0; compno < numcomps; compno++) {
936 unsigned char index = 0;
937 fread(&index, 1, 1, f);
938 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
939 image->comps[compno].data[i] = index;
949 int imagetopnm(opj_image_t * image, const char *outfile) {
950 int w, wr, wrr, h, hr, hrr, max;
955 const char *tmp = outfile;
964 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
965 && image->comps[1].dx == image->comps[2].dx
966 && image->comps[0].dy == image->comps[1].dy
967 && image->comps[1].dy == image->comps[2].dy
968 && image->comps[0].prec == image->comps[1].prec
969 && image->comps[1].prec == image->comps[2].prec
970 && S2 !='g' && S2 !='G') {
972 fdest = fopen(outfile, "wb");
974 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
978 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
979 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */
980 wr = image->comps[0].w;
981 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
983 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
984 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
985 hr = image->comps[0].h;
986 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
988 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
990 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
991 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
993 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
994 adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
995 for (i = 0; i < wrr * hrr; i++) {
997 unsigned char rc,gc,bc;
998 r = image->comps[0].data[i / wrr * wr + i % wrr];
999 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1000 rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
1002 g = image->comps[1].data[i / wrr * wr + i % wrr];
1003 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1004 gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
1006 b = image->comps[2].data[i / wrr * wr + i % wrr];
1007 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1008 bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
1010 fprintf(fdest, "%c%c%c", rc, gc, bc);
1014 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1015 if (image->numcomps>ncomp) {
1016 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1017 fprintf(stderr," is written to the file\n");
1019 for (compno = 0; compno < ncomp; compno++) {
1022 sprintf(name, "%d.%s", compno, outfile);
1024 sprintf(name, "%s", outfile);
1027 fdest = fopen(name, "wb");
1029 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1033 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1034 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */
1035 wr = image->comps[compno].w;
1036 wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1038 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1039 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */
1040 hr = image->comps[compno].h;
1041 hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1043 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1045 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1046 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1048 fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1049 adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1051 for (i = 0; i < wrr * hrr; i++) {
1054 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1055 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1056 lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1057 fprintf(fdest, "%c", lc);