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.
35 #include "../libs/libtiff/tiffio.h"
38 * Get logarithm of an integer and round downwards.
42 static int int_floorlog2(int a) {
44 for (l = 0; a > 1; l++) {
51 * Divide an integer by a power of 2 and round upwards.
55 static int int_ceildivpow2(int a, int b) {
56 return (a + (1 << b) - 1) >> b;
60 * Divide an integer and round upwards.
64 static int int_ceildiv(int a, int b) {
65 return (a + b - 1) / b;
68 /* -->> -->> -->> -->>
72 <<-- <<-- <<-- <<-- */
74 /* WORD defines a two byte word */
75 typedef unsigned short int WORD;
77 /* DWORD defines a four byte word */
78 typedef unsigned long int DWORD;
81 WORD bfType; /* 'BM' for Bitmap (19776) */
82 DWORD bfSize; /* Size of the file */
83 WORD bfReserved1; /* Reserved : 0 */
84 WORD bfReserved2; /* Reserved : 0 */
85 DWORD bfOffBits; /* Offset */
89 DWORD biSize; /* Size of the structure in bytes */
90 DWORD biWidth; /* Width of the image in pixels */
91 DWORD biHeight; /* Heigth of the image in pixels */
92 WORD biPlanes; /* 1 */
93 WORD biBitCount; /* Number of color bits by pixels */
94 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
95 DWORD biSizeImage; /* Size of the image in bytes */
96 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
97 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
98 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
99 DWORD biClrImportant; /* Number of important color (0: ALL) */
100 } BITMAPINFOHEADER_t;
102 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
103 int subsampling_dx = parameters->subsampling_dx;
104 int subsampling_dy = parameters->subsampling_dy;
106 int i, numcomps, w, h;
107 OPJ_COLOR_SPACE color_space;
108 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
109 opj_image_t * image = NULL;
112 BITMAPFILEHEADER_t File_h;
113 BITMAPINFOHEADER_t Info_h;
115 unsigned char *table_R, *table_G, *table_B;
116 unsigned int j, PAD = 0;
119 int gray_scale = 1, not_end_file = 1;
121 unsigned int line = 0, col = 0;
125 IN = fopen(filename, "rb");
127 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
131 File_h.bfType = getc(IN);
132 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
134 if (File_h.bfType != 19778) {
135 fprintf(stderr,"Error, not a BMP file!\n");
140 File_h.bfSize = getc(IN);
141 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
142 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
143 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
145 File_h.bfReserved1 = getc(IN);
146 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
148 File_h.bfReserved2 = getc(IN);
149 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
151 File_h.bfOffBits = getc(IN);
152 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
153 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
154 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
159 Info_h.biSize = getc(IN);
160 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
161 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
162 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
164 Info_h.biWidth = getc(IN);
165 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
166 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
167 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
170 Info_h.biHeight = getc(IN);
171 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
172 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
173 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
176 Info_h.biPlanes = getc(IN);
177 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
179 Info_h.biBitCount = getc(IN);
180 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
182 Info_h.biCompression = getc(IN);
183 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
184 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
185 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
187 Info_h.biSizeImage = getc(IN);
188 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
189 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
190 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
192 Info_h.biXpelsPerMeter = getc(IN);
193 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
194 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
195 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
197 Info_h.biYpelsPerMeter = getc(IN);
198 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
199 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
200 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
202 Info_h.biClrUsed = getc(IN);
203 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
204 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
205 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
207 Info_h.biClrImportant = getc(IN);
208 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
209 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
210 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
212 /* Read the data and store them in the OUT file */
214 if (Info_h.biBitCount == 24) {
216 color_space = CLRSPC_SRGB;
217 /* initialize image components */
218 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
219 for(i = 0; i < numcomps; i++) {
220 cmptparm[i].prec = 8;
222 cmptparm[i].sgnd = 0;
223 cmptparm[i].dx = subsampling_dx;
224 cmptparm[i].dy = subsampling_dy;
228 /* create the image */
229 image = opj_image_create(numcomps, &cmptparm[0], color_space);
235 /* set image offset and reference grid */
236 image->x0 = parameters->image_offset_x0;
237 image->y0 = parameters->image_offset_y0;
238 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
239 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
243 /* Place the cursor at the beginning of the image information */
244 fseek(IN, 0, SEEK_SET);
245 fseek(IN, File_h.bfOffBits, SEEK_SET);
250 /* PAD = 4 - (3 * W) % 4; */
251 /* PAD = (PAD == 4) ? 0 : PAD; */
252 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
254 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
256 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
260 for(y = 0; y < (int)H; y++) {
261 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
262 for(x = 0; x < (int)W; x++) {
263 unsigned char *pixel = &scanline[3 * x];
264 image->comps[0].data[index] = pixel[2]; /* R */
265 image->comps[1].data[index] = pixel[1]; /* G */
266 image->comps[2].data[index] = pixel[0]; /* B */
273 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
274 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
275 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
276 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
278 for (j = 0; j < Info_h.biClrUsed; j++) {
279 table_B[j] = getc(IN);
280 table_G[j] = getc(IN);
281 table_R[j] = getc(IN);
283 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
287 /* Place the cursor at the beginning of the image information */
288 fseek(IN, 0, SEEK_SET);
289 fseek(IN, File_h.bfOffBits, SEEK_SET);
293 if (Info_h.biWidth % 2)
296 numcomps = gray_scale ? 1 : 3;
297 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
298 /* initialize image components */
299 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
300 for(i = 0; i < numcomps; i++) {
301 cmptparm[i].prec = 8;
303 cmptparm[i].sgnd = 0;
304 cmptparm[i].dx = subsampling_dx;
305 cmptparm[i].dy = subsampling_dy;
309 /* create the image */
310 image = opj_image_create(numcomps, &cmptparm[0], color_space);
316 /* set image offset and reference grid */
317 image->x0 = parameters->image_offset_x0;
318 image->y0 = parameters->image_offset_y0;
319 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
320 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
324 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
326 fread(RGB, sizeof(unsigned char), W * H, IN);
329 for (j = 0; j < W * H; j++) {
330 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
331 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
338 for (j = 0; j < W * H; j++) {
339 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
340 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
341 image->comps[0].data[index] = table_R[pixel_index];
342 image->comps[1].data[index] = table_G[pixel_index];
343 image->comps[2].data[index] = table_B[pixel_index];
352 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
353 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
354 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
355 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
357 for (j = 0; j < Info_h.biClrUsed; j++) {
358 table_B[j] = getc(IN);
359 table_G[j] = getc(IN);
360 table_R[j] = getc(IN);
362 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
366 numcomps = gray_scale ? 1 : 3;
367 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
368 /* initialize image components */
369 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
370 for(i = 0; i < numcomps; i++) {
371 cmptparm[i].prec = 8;
373 cmptparm[i].sgnd = 0;
374 cmptparm[i].dx = subsampling_dx;
375 cmptparm[i].dy = subsampling_dy;
379 /* create the image */
380 image = opj_image_create(numcomps, &cmptparm[0], color_space);
386 /* set image offset and reference grid */
387 image->x0 = parameters->image_offset_x0;
388 image->y0 = parameters->image_offset_y0;
389 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
390 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
394 /* Place the cursor at the beginning of the image information */
395 fseek(IN, 0, SEEK_SET);
396 fseek(IN, File_h.bfOffBits, SEEK_SET);
398 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
400 while (not_end_file) {
404 for (i = 0; i < (int) v; i++) {
405 RGB[line * Info_h.biWidth + col] = v2;
420 fprintf(stderr,"No Delta supported\n");
421 opj_image_destroy(image);
425 for (i = 0; i < v; i++) {
427 RGB[line * Info_h.biWidth + col] = v2;
438 for (line = 0; line < Info_h.biHeight; line++) {
439 for (col = 0; col < Info_h.biWidth; col++) {
440 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
446 for (line = 0; line < Info_h.biHeight; line++) {
447 for (col = 0; col < Info_h.biWidth; col++) {
448 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
449 image->comps[0].data[index] = table_R[pixel_index];
450 image->comps[1].data[index] = table_G[pixel_index];
451 image->comps[2].data[index] = table_B[pixel_index];
462 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
470 int imagetobmp(opj_image_t * image, const char *outfile) {
475 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
476 && image->comps[1].dx == image->comps[2].dx
477 && image->comps[0].dy == image->comps[1].dy
478 && image->comps[1].dy == image->comps[2].dy
479 && image->comps[0].prec == image->comps[1].prec
480 && image->comps[1].prec == image->comps[2].prec) {
482 /* -->> -->> -->> -->>
484 <<-- <<-- <<-- <<-- */
486 fdest = fopen(outfile, "wb");
488 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
492 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
493 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
494 w = image->comps[0].w;
495 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
497 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
498 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
499 h = image->comps[0].h;
500 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
502 fprintf(fdest, "BM");
506 fprintf(fdest, "%c%c%c%c",
507 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
508 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
509 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
510 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
511 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
512 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
516 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
517 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
518 (unsigned char) ((wr) >> 8) & 0xff,
519 (unsigned char) ((wr) >> 16) & 0xff,
520 (unsigned char) ((wr) >> 24) & 0xff);
521 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
522 (unsigned char) ((hr) >> 8) & 0xff,
523 (unsigned char) ((hr) >> 16) & 0xff,
524 (unsigned char) ((hr) >> 24) & 0xff);
525 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
526 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
527 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
528 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
529 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
530 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
531 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
534 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
535 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
537 for (i = 0; i < wr * hr; i++) {
538 unsigned char R, G, B;
540 /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
541 R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
542 /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
543 G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
544 /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
545 B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
546 fprintf(fdest, "%c%c%c", B, G, R);
548 if ((i + 1) % wr == 0) {
549 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
550 fprintf(fdest, "%c", 0);
554 } else { /* Gray-scale */
556 /* -->> -->> -->> -->>
557 8 bits non code (Gray scale)
558 <<-- <<-- <<-- <<-- */
560 fdest = fopen(outfile, "wb");
561 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
562 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
563 w = image->comps[0].w;
564 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
566 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
567 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
568 h = image->comps[0].h;
569 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
571 fprintf(fdest, "BM");
575 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
576 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
577 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
578 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
579 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
580 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
581 ((54 + 1024) >> 16) & 0xff,
582 ((54 + 1024) >> 24) & 0xff);
586 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
587 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
588 (unsigned char) ((wr) >> 8) & 0xff,
589 (unsigned char) ((wr) >> 16) & 0xff,
590 (unsigned char) ((wr) >> 24) & 0xff);
591 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
592 (unsigned char) ((hr) >> 8) & 0xff,
593 (unsigned char) ((hr) >> 16) & 0xff,
594 (unsigned char) ((hr) >> 24) & 0xff);
595 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
596 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
597 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
598 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
599 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
600 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
601 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
604 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
605 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
607 for (i = 0; i < 256; i++) {
608 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
611 for (i = 0; i < wr * hr; i++) {
613 /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
614 fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
615 /*if (((i + 1) % w == 0 && w % 2))
616 fprintf(fdest, "%c", 0); */
617 if ((i + 1) % wr == 0) {
618 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
619 fprintf(fdest, "%c", 0);
628 /* -->> -->> -->> -->>
632 <<-- <<-- <<-- <<-- */
635 unsigned char readuchar(FILE * f)
642 unsigned short readushort(FILE * f, int bigendian)
644 unsigned char c1, c2;
648 return (c1 << 8) + c2;
650 return (c2 << 8) + c1;
653 unsigned int readuint(FILE * f, int bigendian)
655 unsigned char c1, c2, c3, c4;
661 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
663 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
666 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
669 int i, numcomps, max;
670 OPJ_COLOR_SPACE color_space;
671 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
672 opj_image_t * image = NULL;
674 char endian1,endian2,sign;
679 opj_image_comp_t *comp = NULL;
682 color_space = CLRSPC_GRAY;
684 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
688 f = fopen(filename, "rb");
690 fprintf(stderr, "Failed to open %s for reading !\n", filename);
694 fseek(f, 0, SEEK_SET);
695 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
699 while (signtmp[i]!='\0') {
700 if (signtmp[i]=='-') sign='-';
705 if (endian1=='M' && endian2=='L') {
707 } else if (endian2=='M' && endian1=='L') {
710 fprintf(stderr, "Bad pgx header, please check input file\n");
714 /* initialize image component */
716 cmptparm.x0 = parameters->image_offset_x0;
717 cmptparm.y0 = parameters->image_offset_y0;
718 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
719 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
726 cmptparm.prec = prec;
728 cmptparm.dx = parameters->subsampling_dx;
729 cmptparm.dy = parameters->subsampling_dy;
731 /* create the image */
732 image = opj_image_create(numcomps, &cmptparm, color_space);
737 /* set image offset and reference grid */
738 image->x0 = cmptparm.x0;
739 image->y0 = cmptparm.x0;
740 image->x1 = cmptparm.w;
741 image->y1 = cmptparm.h;
745 comp = &image->comps[0];
747 for (i = 0; i < w * h; i++) {
749 if (comp->prec <= 8) {
753 v = (char) readuchar(f);
755 } else if (comp->prec <= 16) {
757 v = readushort(f, bigendian);
759 v = (short) readushort(f, bigendian);
763 v = readuint(f, bigendian);
765 v = (int) readuint(f, bigendian);
773 comp->bpp = int_floorlog2(max) + 1;
778 int imagetopgx(opj_image_t * image, const char *outfile) {
783 for (compno = 0; compno < image->numcomps; compno++) {
784 opj_image_comp_t *comp = &image->comps[compno];
785 char bname[256]; /* buffer for name */
786 char *name = bname; /* pointer */
788 const size_t olen = strlen(outfile);
789 const size_t dotpos = olen - 4;
790 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
791 if( outfile[dotpos] != '.' ) {
792 /* `pgx` was recognized but there is no dot at expected position */
793 fprintf(stderr, "ERROR -> Impossible happen." );
797 name = (char*)malloc(total+1);
799 strncpy(name, outfile, dotpos);
800 if (image->numcomps > 1) {
801 sprintf(name+dotpos, "-%d.pgx", compno);
803 strcpy(name+dotpos, ".pgx");
805 fdest = fopen(name, "wb");
807 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
810 /* dont need name anymore */
814 /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */
815 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */
816 w = image->comps[compno].w;
817 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
819 /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */
820 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */
821 h = image->comps[compno].h;
822 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
824 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
825 if (comp->prec <= 8) {
827 } else if (comp->prec <= 16) {
832 for (i = 0; i < wr * hr; i++) {
833 int v = image->comps[compno].data[i / wr * w + i % wr];
834 for (j = nbytes - 1; j >= 0; j--) {
835 char byte = (char) (v >> (j * 8));
836 fwrite(&byte, 1, 1, fdest);
845 /* -->> -->> -->> -->>
849 <<-- <<-- <<-- <<-- */
851 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
852 int subsampling_dx = parameters->subsampling_dx;
853 int subsampling_dy = parameters->subsampling_dy;
856 int i, compno, numcomps, w, h;
857 OPJ_COLOR_SPACE color_space;
858 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
859 opj_image_t * image = NULL;
862 f = fopen(filename, "rb");
864 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
873 case '2': /* greyscale image type */
876 color_space = CLRSPC_GRAY;
879 case '3': /* RGB image type */
882 color_space = CLRSPC_SRGB;
893 while(fgetc(f) == '#') while(fgetc(f) != '\n');
895 fseek(f, -1, SEEK_CUR);
896 fscanf(f, "%d %d\n255", &w, &h);
897 fgetc(f); /* <cr><lf> */
899 /* initialize image components */
900 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
901 for(i = 0; i < numcomps; i++) {
902 cmptparm[i].prec = 8;
904 cmptparm[i].sgnd = 0;
905 cmptparm[i].dx = subsampling_dx;
906 cmptparm[i].dy = subsampling_dy;
910 /* create the image */
911 image = opj_image_create(numcomps, &cmptparm[0], color_space);
917 /* set image offset and reference grid */
918 image->x0 = parameters->image_offset_x0;
919 image->y0 = parameters->image_offset_y0;
920 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
921 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
925 if ((value == '2') || (value == '3')) { /* ASCII */
926 for (i = 0; i < w * h; i++) {
927 for(compno = 0; compno < numcomps; compno++) {
928 unsigned int index = 0;
929 fscanf(f, "%u", &index);
930 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
931 image->comps[compno].data[i] = index;
934 } else if ((value == '5') || (value == '6')) { /* BINARY */
935 for (i = 0; i < w * h; i++) {
936 for(compno = 0; compno < numcomps; compno++) {
937 unsigned char index = 0;
938 fread(&index, 1, 1, f);
939 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
940 image->comps[compno].data[i] = index;
950 int imagetopnm(opj_image_t * image, const char *outfile) {
951 int w, wr, wrr, h, hr, hrr, max;
956 const char *tmp = outfile;
965 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
966 && image->comps[1].dx == image->comps[2].dx
967 && image->comps[0].dy == image->comps[1].dy
968 && image->comps[1].dy == image->comps[2].dy
969 && image->comps[0].prec == image->comps[1].prec
970 && image->comps[1].prec == image->comps[2].prec
971 && S2 !='g' && S2 !='G') {
973 fdest = fopen(outfile, "wb");
975 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
979 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
980 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */
981 wr = image->comps[0].w;
982 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
984 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
985 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
986 hr = image->comps[0].h;
987 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
989 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
991 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
992 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
994 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
995 adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
996 for (i = 0; i < wrr * hrr; i++) {
998 unsigned char rc,gc,bc;
999 r = image->comps[0].data[i / wrr * wr + i % wrr];
1000 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1001 rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
1003 g = image->comps[1].data[i / wrr * wr + i % wrr];
1004 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1005 gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
1007 b = image->comps[2].data[i / wrr * wr + i % wrr];
1008 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1009 bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
1011 fprintf(fdest, "%c%c%c", rc, gc, bc);
1015 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1016 if (image->numcomps>ncomp) {
1017 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1018 fprintf(stderr," is written to the file\n");
1020 for (compno = 0; compno < ncomp; compno++) {
1023 sprintf(name, "%d.%s", compno, outfile);
1025 sprintf(name, "%s", outfile);
1028 fdest = fopen(name, "wb");
1030 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1034 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1035 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */
1036 wr = image->comps[compno].w;
1037 wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1039 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1040 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */
1041 hr = image->comps[compno].h;
1042 hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1044 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1046 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1047 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1049 fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1050 adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1052 for (i = 0; i < wrr * hrr; i++) {
1055 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1056 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1057 lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1058 fprintf(fdest, "%c", lc);
1067 /* -->> -->> -->> -->>
1071 <<-- <<-- <<-- <<-- */
1073 typedef struct tiff_infoheader{
1074 DWORD tiWidth; // Width of Image in pixel
1075 DWORD tiHeight; // Height of Image in pixel
1076 DWORD tiPhoto; // Photometric
1077 WORD tiBps; // Bits per sample
1078 WORD tiSf; // Sample Format
1079 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1080 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1083 int imagetotif(opj_image_t * image, const char *outfile) {
1091 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1092 && image->comps[1].dx == image->comps[2].dx
1093 && image->comps[0].dy == image->comps[1].dy
1094 && image->comps[1].dy == image->comps[2].dy
1095 && image->comps[0].prec == image->comps[1].prec
1096 && image->comps[1].prec == image->comps[2].prec) {
1102 tif = TIFFOpen(outfile, "wb");
1104 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1108 width = image->comps[0].w;
1109 height= image->comps[0].h;
1110 bps = image->comps[0].prec;
1112 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1113 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1114 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1115 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1116 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1117 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1118 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1119 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1121 /* Get a buffer for the data */
1122 buf = _TIFFmalloc(TIFFStripSize(tif));
1125 strip_size=TIFFStripSize(tif);
1126 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1127 unsigned char *dat8;
1130 if (image->comps[0].prec == 8){
1131 for (i=0; i<TIFFStripSize(tif); i+=3) { // 8 bits per pixel
1132 dat8[i+0] = image->comps[0].data[index] ; // R
1133 dat8[i+1] = image->comps[1].data[index] ; // G
1134 dat8[i+2] = image->comps[2].data[index] ; // B
1137 }else if (image->comps[0].prec == 12){
1138 for (i=0; i<TIFFStripSize(tif); i+=9) { // 12 bits per pixel
1139 dat8[i+0] = (image->comps[0].data[index]>>8)<<4 | (image->comps[0].data[index]>>4);
1140 dat8[i+1] = (image->comps[0].data[index]<<4)|((image->comps[1].data[index]>>8)& 0x0f);
1141 dat8[i+2] = (image->comps[1].data[index]);
1142 dat8[i+3] = (image->comps[2].data[index]>>8)<<4 | (image->comps[2].data[index]>>4);
1143 dat8[i+4] = (image->comps[2].data[index]<<4)|((image->comps[1].data[index+1]>>8)& 0x0f);
1144 dat8[i+5] = (image->comps[0].data[index+1]);
1145 dat8[i+6] = (image->comps[1].data[index+1]>>8)<<4 | (image->comps[1].data[index+1]>>4);
1146 dat8[i+7] = (image->comps[1].data[index+1]<<4)|((image->comps[2].data[index+1]>>8)& 0x0f);
1147 dat8[i+8] = (image->comps[2].data[index+1]);
1150 }else if (image->comps[0].prec == 16){
1151 for (i=0; i<TIFFStripSize(tif); i+=6) { // 16 bits per pixel
1152 dat8[i+0] = image->comps[0].data[index];//LSB
1153 dat8[i+1] = (image->comps[0].data[index]>> 8);//MSB
1154 dat8[i+2] = image->comps[1].data[index];
1155 dat8[i+3] = (image->comps[1].data[index]>> 8);
1156 dat8[i+4] = image->comps[2].data[index];
1157 dat8[i+5] = (image->comps[2].data[index]>> 8);
1161 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1162 fprintf(stderr,"Aborting\n");
1165 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1169 }else if (image->numcomps == 1){
1174 tif = TIFFOpen(outfile, "wb");
1176 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1180 width = image->comps[0].w;
1181 height= image->comps[0].h;
1182 bps = image->comps[0].prec;
1185 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1186 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1187 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1188 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1189 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1190 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1191 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1192 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1194 /* Get a buffer for the data */
1195 buf = _TIFFmalloc(TIFFStripSize(tif));
1198 strip_size = TIFFStripSize(tif);
1199 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1200 unsigned char *dat8;
1203 if (image->comps[0].prec == 8){
1204 for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
1205 dat8[i+0] = image->comps[0].data[index] ;
1208 }else if (image->comps[0].prec == 12){
1209 for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
1210 dat8[i+0] = (image->comps[0].data[index]>>8)<<4 | (image->comps[0].data[index]>>4);
1211 dat8[i+1] = (image->comps[0].data[index]<<4)|((image->comps[0].data[index+1]>>8)& 0x0f);
1212 dat8[i+2] = (image->comps[0].data[index+1]);
1215 }else if (image->comps[0].prec == 16){
1216 for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
1217 dat8[i+0] = image->comps[0].data[index];
1218 dat8[i+1] = (image->comps[0].data[index]>> 8);
1222 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1223 fprintf(stderr,"Aborting\n");
1226 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1231 fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
1232 fprintf(stderr,"Aborting\n");
1238 opj_image_t* tiftoimage(char *filename, opj_cparameters_t *parameters)
1240 int subsampling_dx = parameters->subsampling_dx;
1241 int subsampling_dy = parameters->subsampling_dy;
1243 tiff_infoheader_t Info;
1247 int j, numcomps, w, h,index;
1248 OPJ_COLOR_SPACE color_space;
1249 opj_image_cmptparm_t cmptparm[3];
1250 opj_image_t * image = NULL;
1252 tif = TIFFOpen(filename, "r");
1255 fprintf(stderr, "Failed to open %s for reading\n", filename);
1259 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1260 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1261 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1262 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1263 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1265 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1266 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1270 if (Info.tiPhoto == 2) {
1276 color_space = CLRSPC_SRGB;
1277 /* initialize image components*/
1278 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1279 for(j = 0; j < numcomps; j++) {
1280 cmptparm[j].prec = Info.tiBps;
1281 cmptparm[j].bpp = Info.tiBps;
1282 cmptparm[j].sgnd = 0;
1283 cmptparm[j].dx = subsampling_dx;
1284 cmptparm[j].dy = subsampling_dy;
1288 /* create the image*/
1289 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1295 /* set image offset and reference grid */
1296 image->x0 = parameters->image_offset_x0;
1297 image->y0 = parameters->image_offset_y0;
1298 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1299 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1301 buf = _TIFFmalloc(TIFFStripSize(tif));
1303 strip_size=TIFFStripSize(tif);
1305 /* Read the Image components*/
1306 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1307 unsigned char *dat8;
1309 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1312 if (Info.tiBps==12){
1313 for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
1314 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
1315 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1316 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
1317 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1318 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
1319 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1323 else if( Info.tiBps==16){
1324 for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
1325 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
1326 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
1327 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
1331 else if ( Info.tiBps==8){
1332 for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
1333 image->comps[0].data[index] = dat8[i+0]; // R
1334 image->comps[1].data[index] = dat8[i+1]; // G
1335 image->comps[2].data[index] = dat8[i+2]; // B
1340 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1341 fprintf(stderr,"Aborting\n");
1348 }else if(Info.tiPhoto == 1) {
1354 color_space = CLRSPC_GRAY;
1355 /* initialize image components*/
1356 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1357 cmptparm[0].prec = Info.tiBps;
1358 cmptparm[0].bpp = Info.tiBps;
1359 cmptparm[0].sgnd = 0;
1360 cmptparm[0].dx = subsampling_dx;
1361 cmptparm[0].dy = subsampling_dy;
1365 /* create the image*/
1366 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1371 /* set image offset and reference grid */
1372 image->x0 = parameters->image_offset_x0;
1373 image->y0 = parameters->image_offset_y0;
1374 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1375 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1377 buf = _TIFFmalloc(TIFFStripSize(tif));
1379 strip_size = TIFFStripSize(tif);
1381 /* Read the Image components*/
1382 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1383 unsigned char *dat8;
1385 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1388 if (Info.tiBps==12){
1389 for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
1390 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
1391 image->comps[0].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1395 else if( Info.tiBps==16){
1396 for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
1397 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
1401 else if ( Info.tiBps==8){
1402 for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
1403 image->comps[0].data[index] = dat8[i+0];
1408 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1409 fprintf(stderr,"Aborting\n");
1417 fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
1418 fprintf(stderr,"Aborting\n");