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
8 * Copyright (c) 2006-2007, Parvatha Elangovan
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
36 #include "../libs/libtiff/tiffio.h"
39 * Get logarithm of an integer and round downwards.
43 static int int_floorlog2(int a) {
45 for (l = 0; a > 1; l++) {
52 * Divide an integer by a power of 2 and round upwards.
56 static int int_ceildivpow2(int a, int b) {
57 return (a + (1 << b) - 1) >> b;
61 * Divide an integer and round upwards.
65 static int int_ceildiv(int a, int b) {
66 return (a + b - 1) / b;
69 /* -->> -->> -->> -->>
73 <<-- <<-- <<-- <<-- */
75 /* WORD defines a two byte word */
76 typedef unsigned short int WORD;
78 /* DWORD defines a four byte word */
79 typedef unsigned long int DWORD;
82 WORD bfType; /* 'BM' for Bitmap (19776) */
83 DWORD bfSize; /* Size of the file */
84 WORD bfReserved1; /* Reserved : 0 */
85 WORD bfReserved2; /* Reserved : 0 */
86 DWORD bfOffBits; /* Offset */
90 DWORD biSize; /* Size of the structure in bytes */
91 DWORD biWidth; /* Width of the image in pixels */
92 DWORD biHeight; /* Heigth of the image in pixels */
93 WORD biPlanes; /* 1 */
94 WORD biBitCount; /* Number of color bits by pixels */
95 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
96 DWORD biSizeImage; /* Size of the image in bytes */
97 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
98 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
99 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
100 DWORD biClrImportant; /* Number of important color (0: ALL) */
101 } BITMAPINFOHEADER_t;
103 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
104 int subsampling_dx = parameters->subsampling_dx;
105 int subsampling_dy = parameters->subsampling_dy;
107 int i, numcomps, w, h;
108 OPJ_COLOR_SPACE color_space;
109 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
110 opj_image_t * image = NULL;
113 BITMAPFILEHEADER_t File_h;
114 BITMAPINFOHEADER_t Info_h;
116 unsigned char *table_R, *table_G, *table_B;
117 unsigned int j, PAD = 0;
120 int gray_scale = 1, not_end_file = 1;
122 unsigned int line = 0, col = 0;
126 IN = fopen(filename, "rb");
128 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
132 File_h.bfType = getc(IN);
133 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
135 if (File_h.bfType != 19778) {
136 fprintf(stderr,"Error, not a BMP file!\n");
141 File_h.bfSize = getc(IN);
142 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
143 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
144 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
146 File_h.bfReserved1 = getc(IN);
147 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
149 File_h.bfReserved2 = getc(IN);
150 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
152 File_h.bfOffBits = getc(IN);
153 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
154 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
155 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
160 Info_h.biSize = getc(IN);
161 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
162 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
163 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
165 Info_h.biWidth = getc(IN);
166 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
167 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
168 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
171 Info_h.biHeight = getc(IN);
172 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
173 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
174 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
177 Info_h.biPlanes = getc(IN);
178 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
180 Info_h.biBitCount = getc(IN);
181 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
183 Info_h.biCompression = getc(IN);
184 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
185 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
186 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
188 Info_h.biSizeImage = getc(IN);
189 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
190 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
191 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
193 Info_h.biXpelsPerMeter = getc(IN);
194 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
195 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
196 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
198 Info_h.biYpelsPerMeter = getc(IN);
199 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
200 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
201 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
203 Info_h.biClrUsed = getc(IN);
204 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
205 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
206 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
208 Info_h.biClrImportant = getc(IN);
209 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
210 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
211 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
213 /* Read the data and store them in the OUT file */
215 if (Info_h.biBitCount == 24) {
217 color_space = CLRSPC_SRGB;
218 /* initialize image components */
219 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
220 for(i = 0; i < numcomps; i++) {
221 cmptparm[i].prec = 8;
223 cmptparm[i].sgnd = 0;
224 cmptparm[i].dx = subsampling_dx;
225 cmptparm[i].dy = subsampling_dy;
229 /* create the image */
230 image = opj_image_create(numcomps, &cmptparm[0], color_space);
236 /* set image offset and reference grid */
237 image->x0 = parameters->image_offset_x0;
238 image->y0 = parameters->image_offset_y0;
239 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
240 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
244 /* Place the cursor at the beginning of the image information */
245 fseek(IN, 0, SEEK_SET);
246 fseek(IN, File_h.bfOffBits, SEEK_SET);
251 /* PAD = 4 - (3 * W) % 4; */
252 /* PAD = (PAD == 4) ? 0 : PAD; */
253 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
255 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
257 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
261 for(y = 0; y < (int)H; y++) {
262 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
263 for(x = 0; x < (int)W; x++) {
264 unsigned char *pixel = &scanline[3 * x];
265 image->comps[0].data[index] = pixel[2]; /* R */
266 image->comps[1].data[index] = pixel[1]; /* G */
267 image->comps[2].data[index] = pixel[0]; /* B */
274 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
275 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
276 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
277 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
279 for (j = 0; j < Info_h.biClrUsed; j++) {
280 table_B[j] = getc(IN);
281 table_G[j] = getc(IN);
282 table_R[j] = getc(IN);
284 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
288 /* Place the cursor at the beginning of the image information */
289 fseek(IN, 0, SEEK_SET);
290 fseek(IN, File_h.bfOffBits, SEEK_SET);
294 if (Info_h.biWidth % 2)
297 numcomps = gray_scale ? 1 : 3;
298 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
299 /* initialize image components */
300 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
301 for(i = 0; i < numcomps; i++) {
302 cmptparm[i].prec = 8;
304 cmptparm[i].sgnd = 0;
305 cmptparm[i].dx = subsampling_dx;
306 cmptparm[i].dy = subsampling_dy;
310 /* create the image */
311 image = opj_image_create(numcomps, &cmptparm[0], color_space);
317 /* set image offset and reference grid */
318 image->x0 = parameters->image_offset_x0;
319 image->y0 = parameters->image_offset_y0;
320 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
321 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
325 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
327 fread(RGB, sizeof(unsigned char), W * H, IN);
330 for (j = 0; j < W * H; j++) {
331 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
332 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
339 for (j = 0; j < W * H; j++) {
340 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
341 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
342 image->comps[0].data[index] = table_R[pixel_index];
343 image->comps[1].data[index] = table_G[pixel_index];
344 image->comps[2].data[index] = table_B[pixel_index];
353 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
354 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
355 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
356 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
358 for (j = 0; j < Info_h.biClrUsed; j++) {
359 table_B[j] = getc(IN);
360 table_G[j] = getc(IN);
361 table_R[j] = getc(IN);
363 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
367 numcomps = gray_scale ? 1 : 3;
368 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
369 /* initialize image components */
370 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
371 for(i = 0; i < numcomps; i++) {
372 cmptparm[i].prec = 8;
374 cmptparm[i].sgnd = 0;
375 cmptparm[i].dx = subsampling_dx;
376 cmptparm[i].dy = subsampling_dy;
380 /* create the image */
381 image = opj_image_create(numcomps, &cmptparm[0], color_space);
387 /* set image offset and reference grid */
388 image->x0 = parameters->image_offset_x0;
389 image->y0 = parameters->image_offset_y0;
390 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
391 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
395 /* Place the cursor at the beginning of the image information */
396 fseek(IN, 0, SEEK_SET);
397 fseek(IN, File_h.bfOffBits, SEEK_SET);
399 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
401 while (not_end_file) {
405 for (i = 0; i < (int) v; i++) {
406 RGB[line * Info_h.biWidth + col] = v2;
421 fprintf(stderr,"No Delta supported\n");
422 opj_image_destroy(image);
426 for (i = 0; i < v; i++) {
428 RGB[line * Info_h.biWidth + col] = v2;
439 for (line = 0; line < Info_h.biHeight; line++) {
440 for (col = 0; col < Info_h.biWidth; col++) {
441 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
447 for (line = 0; line < Info_h.biHeight; line++) {
448 for (col = 0; col < Info_h.biWidth; col++) {
449 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
450 image->comps[0].data[index] = table_R[pixel_index];
451 image->comps[1].data[index] = table_G[pixel_index];
452 image->comps[2].data[index] = table_B[pixel_index];
463 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
471 int imagetobmp(opj_image_t * image, const char *outfile) {
476 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
477 && image->comps[1].dx == image->comps[2].dx
478 && image->comps[0].dy == image->comps[1].dy
479 && image->comps[1].dy == image->comps[2].dy
480 && image->comps[0].prec == image->comps[1].prec
481 && image->comps[1].prec == image->comps[2].prec) {
483 /* -->> -->> -->> -->>
485 <<-- <<-- <<-- <<-- */
487 fdest = fopen(outfile, "wb");
489 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
493 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
494 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
495 w = image->comps[0].w;
496 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
498 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
499 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
500 h = image->comps[0].h;
501 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
503 fprintf(fdest, "BM");
507 fprintf(fdest, "%c%c%c%c",
508 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
509 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
510 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
511 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
512 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
513 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
517 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
518 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
519 (unsigned char) ((wr) >> 8) & 0xff,
520 (unsigned char) ((wr) >> 16) & 0xff,
521 (unsigned char) ((wr) >> 24) & 0xff);
522 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
523 (unsigned char) ((hr) >> 8) & 0xff,
524 (unsigned char) ((hr) >> 16) & 0xff,
525 (unsigned char) ((hr) >> 24) & 0xff);
526 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
527 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 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", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
530 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
531 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
532 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
533 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
534 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
535 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
536 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
538 for (i = 0; i < wr * hr; i++) {
539 unsigned char R, G, B;
541 /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
542 R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
543 /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
544 G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
545 /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
546 B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
547 fprintf(fdest, "%c%c%c", B, G, R);
549 if ((i + 1) % wr == 0) {
550 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
551 fprintf(fdest, "%c", 0);
555 } else { /* Gray-scale */
557 /* -->> -->> -->> -->>
558 8 bits non code (Gray scale)
559 <<-- <<-- <<-- <<-- */
561 fdest = fopen(outfile, "wb");
562 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
563 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
564 w = image->comps[0].w;
565 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
567 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
568 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
569 h = image->comps[0].h;
570 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
572 fprintf(fdest, "BM");
576 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
577 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
578 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
579 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
580 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
581 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
582 ((54 + 1024) >> 16) & 0xff,
583 ((54 + 1024) >> 24) & 0xff);
587 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
588 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
589 (unsigned char) ((wr) >> 8) & 0xff,
590 (unsigned char) ((wr) >> 16) & 0xff,
591 (unsigned char) ((wr) >> 24) & 0xff);
592 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
593 (unsigned char) ((hr) >> 8) & 0xff,
594 (unsigned char) ((hr) >> 16) & 0xff,
595 (unsigned char) ((hr) >> 24) & 0xff);
596 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
597 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
598 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
599 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
600 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
601 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
602 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
603 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
604 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
605 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
606 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
608 for (i = 0; i < 256; i++) {
609 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
612 for (i = 0; i < wr * hr; i++) {
614 /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
615 fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
616 /*if (((i + 1) % w == 0 && w % 2))
617 fprintf(fdest, "%c", 0); */
618 if ((i + 1) % wr == 0) {
619 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
620 fprintf(fdest, "%c", 0);
629 /* -->> -->> -->> -->>
633 <<-- <<-- <<-- <<-- */
636 unsigned char readuchar(FILE * f)
643 unsigned short readushort(FILE * f, int bigendian)
645 unsigned char c1, c2;
649 return (c1 << 8) + c2;
651 return (c2 << 8) + c1;
654 unsigned int readuint(FILE * f, int bigendian)
656 unsigned char c1, c2, c3, c4;
662 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
664 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
667 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
670 int i, numcomps, max;
671 OPJ_COLOR_SPACE color_space;
672 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
673 opj_image_t * image = NULL;
675 char endian1,endian2,sign;
680 opj_image_comp_t *comp = NULL;
683 color_space = CLRSPC_GRAY;
685 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
689 f = fopen(filename, "rb");
691 fprintf(stderr, "Failed to open %s for reading !\n", filename);
695 fseek(f, 0, SEEK_SET);
696 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
700 while (signtmp[i]!='\0') {
701 if (signtmp[i]=='-') sign='-';
706 if (endian1=='M' && endian2=='L') {
708 } else if (endian2=='M' && endian1=='L') {
711 fprintf(stderr, "Bad pgx header, please check input file\n");
715 /* initialize image component */
717 cmptparm.x0 = parameters->image_offset_x0;
718 cmptparm.y0 = parameters->image_offset_y0;
719 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
720 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
727 cmptparm.prec = prec;
729 cmptparm.dx = parameters->subsampling_dx;
730 cmptparm.dy = parameters->subsampling_dy;
732 /* create the image */
733 image = opj_image_create(numcomps, &cmptparm, color_space);
738 /* set image offset and reference grid */
739 image->x0 = cmptparm.x0;
740 image->y0 = cmptparm.x0;
741 image->x1 = cmptparm.w;
742 image->y1 = cmptparm.h;
746 comp = &image->comps[0];
748 for (i = 0; i < w * h; i++) {
750 if (comp->prec <= 8) {
754 v = (char) readuchar(f);
756 } else if (comp->prec <= 16) {
758 v = readushort(f, bigendian);
760 v = (short) readushort(f, bigendian);
764 v = readuint(f, bigendian);
766 v = (int) readuint(f, bigendian);
774 comp->bpp = int_floorlog2(max) + 1;
779 int imagetopgx(opj_image_t * image, const char *outfile) {
784 for (compno = 0; compno < image->numcomps; compno++) {
785 opj_image_comp_t *comp = &image->comps[compno];
786 char bname[256]; /* buffer for name */
787 char *name = bname; /* pointer */
789 const size_t olen = strlen(outfile);
790 const size_t dotpos = olen - 4;
791 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
792 if( outfile[dotpos] != '.' ) {
793 /* `pgx` was recognized but there is no dot at expected position */
794 fprintf(stderr, "ERROR -> Impossible happen." );
798 name = (char*)malloc(total+1);
800 strncpy(name, outfile, dotpos);
801 if (image->numcomps > 1) {
802 sprintf(name+dotpos, "-%d.pgx", compno);
804 strcpy(name+dotpos, ".pgx");
806 fdest = fopen(name, "wb");
808 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
811 /* dont need name anymore */
815 /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */
816 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */
817 w = image->comps[compno].w;
818 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
820 /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */
821 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */
822 h = image->comps[compno].h;
823 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
825 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
826 if (comp->prec <= 8) {
828 } else if (comp->prec <= 16) {
833 for (i = 0; i < wr * hr; i++) {
834 int v = image->comps[compno].data[i / wr * w + i % wr];
835 for (j = nbytes - 1; j >= 0; j--) {
836 char byte = (char) (v >> (j * 8));
837 fwrite(&byte, 1, 1, fdest);
846 /* -->> -->> -->> -->>
850 <<-- <<-- <<-- <<-- */
852 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
853 int subsampling_dx = parameters->subsampling_dx;
854 int subsampling_dy = parameters->subsampling_dy;
857 int i, compno, numcomps, w, h;
858 OPJ_COLOR_SPACE color_space;
859 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
860 opj_image_t * image = NULL;
863 f = fopen(filename, "rb");
865 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
874 case '2': /* greyscale image type */
877 color_space = CLRSPC_GRAY;
880 case '3': /* RGB image type */
883 color_space = CLRSPC_SRGB;
894 while(fgetc(f) == '#') while(fgetc(f) != '\n');
896 fseek(f, -1, SEEK_CUR);
897 fscanf(f, "%d %d\n255", &w, &h);
898 fgetc(f); /* <cr><lf> */
900 /* initialize image components */
901 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
902 for(i = 0; i < numcomps; i++) {
903 cmptparm[i].prec = 8;
905 cmptparm[i].sgnd = 0;
906 cmptparm[i].dx = subsampling_dx;
907 cmptparm[i].dy = subsampling_dy;
911 /* create the image */
912 image = opj_image_create(numcomps, &cmptparm[0], color_space);
918 /* set image offset and reference grid */
919 image->x0 = parameters->image_offset_x0;
920 image->y0 = parameters->image_offset_y0;
921 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
922 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
926 if ((value == '2') || (value == '3')) { /* ASCII */
927 for (i = 0; i < w * h; i++) {
928 for(compno = 0; compno < numcomps; compno++) {
929 unsigned int index = 0;
930 fscanf(f, "%u", &index);
931 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
932 image->comps[compno].data[i] = index;
935 } else if ((value == '5') || (value == '6')) { /* BINARY */
936 for (i = 0; i < w * h; i++) {
937 for(compno = 0; compno < numcomps; compno++) {
938 unsigned char index = 0;
939 fread(&index, 1, 1, f);
940 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
941 image->comps[compno].data[i] = index;
951 int imagetopnm(opj_image_t * image, const char *outfile) {
952 int w, wr, wrr, h, hr, hrr, max;
957 const char *tmp = outfile;
966 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
967 && image->comps[1].dx == image->comps[2].dx
968 && image->comps[0].dy == image->comps[1].dy
969 && image->comps[1].dy == image->comps[2].dy
970 && image->comps[0].prec == image->comps[1].prec
971 && image->comps[1].prec == image->comps[2].prec
972 && S2 !='g' && S2 !='G') {
974 fdest = fopen(outfile, "wb");
976 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
980 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
981 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */
982 wr = image->comps[0].w;
983 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
985 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
986 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
987 hr = image->comps[0].h;
988 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
990 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
992 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
993 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
995 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
996 adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
997 for (i = 0; i < wrr * hrr; i++) {
999 unsigned char rc,gc,bc;
1000 r = image->comps[0].data[i / wrr * wr + i % wrr];
1001 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1002 rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
1004 g = image->comps[1].data[i / wrr * wr + i % wrr];
1005 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1006 gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
1008 b = image->comps[2].data[i / wrr * wr + i % wrr];
1009 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1010 bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
1012 fprintf(fdest, "%c%c%c", rc, gc, bc);
1016 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1017 if (image->numcomps>ncomp) {
1018 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1019 fprintf(stderr," is written to the file\n");
1021 for (compno = 0; compno < ncomp; compno++) {
1024 sprintf(name, "%d.%s", compno, outfile);
1026 sprintf(name, "%s", outfile);
1029 fdest = fopen(name, "wb");
1031 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1035 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1036 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */
1037 wr = image->comps[compno].w;
1038 wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1040 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1041 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */
1042 hr = image->comps[compno].h;
1043 hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1045 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1047 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1048 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1050 fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1051 adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1053 for (i = 0; i < wrr * hrr; i++) {
1056 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1057 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1058 lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1059 fprintf(fdest, "%c", lc);
1068 /* -->> -->> -->> -->>
1072 <<-- <<-- <<-- <<-- */
1074 typedef struct tiff_infoheader{
1075 DWORD tiWidth; // Width of Image in pixel
1076 DWORD tiHeight; // Height of Image in pixel
1077 DWORD tiPhoto; // Photometric
1078 WORD tiBps; // Bits per sample
1079 WORD tiSf; // Sample Format
1080 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1081 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1084 int imagetotif(opj_image_t * image, const char *outfile) {
1085 int width, height, imgsize;
1092 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1093 && image->comps[1].dx == image->comps[2].dx
1094 && image->comps[0].dy == image->comps[1].dy
1095 && image->comps[1].dy == image->comps[2].dy
1096 && image->comps[0].prec == image->comps[1].prec
1097 && image->comps[1].prec == image->comps[2].prec) {
1103 tif = TIFFOpen(outfile, "wb");
1105 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1109 width = image->comps[0].w;
1110 height = image->comps[0].h;
1111 imgsize = image->comps[0].w * image->comps[0].h ;
1112 bps = image->comps[0].prec;
1114 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1115 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1116 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1117 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1118 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1119 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1120 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1121 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1123 /* Get a buffer for the data */
1124 buf = _TIFFmalloc(TIFFStripSize(tif));
1127 strip_size=TIFFStripSize(tif);
1128 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1129 unsigned char *dat8;
1132 if (image->comps[0].prec == 8){
1133 for (i=0; i < strip_size; i+=3) { // 8 bits per pixel
1134 int r = 0,g = 0,b = 0;
1135 if(index < imgsize){
1136 r = image->comps[0].data[index];
1137 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1138 g = image->comps[1].data[index];
1139 g += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1140 b = image->comps[2].data[index];
1141 b += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1143 dat8[i+0] = r ; // R
1144 dat8[i+1] = g ; // G
1145 dat8[i+2] = b ; // B
1150 }else if (image->comps[0].prec == 12){
1151 for (i=0; i<strip_size; i+=9) { // 12 bits per pixel
1152 int r = 0,g = 0,b = 0;
1153 int r1 = 0,g1 = 0,b1 = 0;
1154 if(index < imgsize){
1155 r = image->comps[0].data[index];
1156 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1157 g = image->comps[1].data[index];
1158 g += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1159 b = image->comps[2].data[index];
1160 b += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1161 r1 = image->comps[0].data[index+1];
1162 r1 += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1163 g1 = image->comps[1].data[index+1];
1164 g1 += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1165 b1 = image->comps[2].data[index+1];
1166 b1 += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1168 dat8[i+0] = (r >> 4);
1169 dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f);
1171 dat8[i+3] = (b >> 4);
1172 dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1174 dat8[i+6] = (g1 >> 4);
1175 dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);
1181 }else if (image->comps[0].prec == 16){
1182 for (i=0; i<strip_size; i+=6) { // 16 bits per pixel
1183 int r = 0,g = 0,b = 0;
1184 if(index < imgsize){
1185 r = image->comps[0].data[index];
1186 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1187 g = image->comps[1].data[index];
1188 g += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1189 b = image->comps[2].data[index];
1190 b += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1193 dat8[i+1] = (r >> 8);//MSB
1195 dat8[i+3] = (g >> 8);
1197 dat8[i+5] = (b >> 8);
1203 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1204 fprintf(stderr,"Aborting\n");
1207 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1211 }else if (image->numcomps == 1){
1216 tif = TIFFOpen(outfile, "wb");
1218 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1222 width = image->comps[0].w;
1223 height= image->comps[0].h;
1224 bps = image->comps[0].prec;
1227 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1228 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1229 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1230 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1231 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1232 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1233 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1234 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1236 /* Get a buffer for the data */
1237 buf = _TIFFmalloc(TIFFStripSize(tif));
1240 strip_size = TIFFStripSize(tif);
1241 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1242 unsigned char *dat8;
1245 if (image->comps[0].prec == 8){
1246 for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
1247 dat8[i+0] = image->comps[0].data[index] ;
1250 }else if (image->comps[0].prec == 12){
1251 for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
1252 dat8[i+0] = (image->comps[0].data[index]>>8)<<4 | (image->comps[0].data[index]>>4);
1253 dat8[i+1] = (image->comps[0].data[index]<<4)|((image->comps[0].data[index+1]>>8)& 0x0f);
1254 dat8[i+2] = (image->comps[0].data[index+1]);
1257 }else if (image->comps[0].prec == 16){
1258 for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
1259 dat8[i+0] = image->comps[0].data[index];
1260 dat8[i+1] = (image->comps[0].data[index]>> 8);
1264 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1265 fprintf(stderr,"Aborting\n");
1268 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1273 fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
1274 fprintf(stderr,"Aborting\n");
1280 opj_image_t* tiftoimage(char *filename, opj_cparameters_t *parameters)
1282 int subsampling_dx = parameters->subsampling_dx;
1283 int subsampling_dy = parameters->subsampling_dy;
1285 tiff_infoheader_t Info;
1289 int j, numcomps, w, h,index;
1290 OPJ_COLOR_SPACE color_space;
1291 opj_image_cmptparm_t cmptparm[3];
1292 opj_image_t * image = NULL;
1295 tif = TIFFOpen(filename, "r");
1298 fprintf(stderr, "Failed to open %s for reading\n", filename);
1302 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1303 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1304 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1305 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1306 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1308 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1309 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1313 if (Info.tiPhoto == 2) {
1319 color_space = CLRSPC_SRGB;
1320 /* initialize image components*/
1321 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1322 for(j = 0; j < numcomps; j++) {
1323 cmptparm[j].prec = Info.tiBps;
1324 cmptparm[j].bpp = Info.tiBps;
1325 cmptparm[j].sgnd = 0;
1326 cmptparm[j].dx = subsampling_dx;
1327 cmptparm[j].dy = subsampling_dy;
1331 /* create the image*/
1332 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1338 /* set image offset and reference grid */
1339 image->x0 = parameters->image_offset_x0;
1340 image->y0 = parameters->image_offset_y0;
1341 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1342 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1344 buf = _TIFFmalloc(TIFFStripSize(tif));
1346 strip_size=TIFFStripSize(tif);
1348 imgsize = image->comps[0].w * image->comps[0].h ;
1349 /* Read the Image components*/
1350 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1351 unsigned char *dat8;
1353 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1356 if (Info.tiBps==12){
1357 for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
1358 if(index < datasize){
1359 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
1360 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1361 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
1362 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1363 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
1364 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1370 else if( Info.tiBps==16){
1371 for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
1372 if(index < datasize){
1373 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
1374 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
1375 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
1381 else if ( Info.tiBps==8){
1382 for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
1383 if(index < datasize){
1384 image->comps[0].data[index] = dat8[i+0]; // R
1385 image->comps[1].data[index] = dat8[i+1]; // G
1386 image->comps[2].data[index] = dat8[i+2]; // B
1393 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1394 fprintf(stderr,"Aborting\n");
1401 }else if(Info.tiPhoto == 1) {
1407 color_space = CLRSPC_GRAY;
1408 /* initialize image components*/
1409 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1410 cmptparm[0].prec = Info.tiBps;
1411 cmptparm[0].bpp = Info.tiBps;
1412 cmptparm[0].sgnd = 0;
1413 cmptparm[0].dx = subsampling_dx;
1414 cmptparm[0].dy = subsampling_dy;
1418 /* create the image*/
1419 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1424 /* set image offset and reference grid */
1425 image->x0 = parameters->image_offset_x0;
1426 image->y0 = parameters->image_offset_y0;
1427 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1428 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1430 buf = _TIFFmalloc(TIFFStripSize(tif));
1432 strip_size = TIFFStripSize(tif);
1434 /* Read the Image components*/
1435 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1436 unsigned char *dat8;
1438 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1441 if (Info.tiBps==12){
1442 for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
1443 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
1444 image->comps[0].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1448 else if( Info.tiBps==16){
1449 for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
1450 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
1454 else if ( Info.tiBps==8){
1455 for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
1456 image->comps[0].data[index] = dat8[i+0];
1461 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1462 fprintf(stderr,"Aborting\n");
1470 fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
1471 fprintf(stderr,"Aborting\n");