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"
40 * Get logarithm of an integer and round downwards.
44 static int int_floorlog2(int a) {
46 for (l = 0; a > 1; l++) {
53 * Divide an integer by a power of 2 and round upwards.
57 static int int_ceildivpow2(int a, int b) {
58 return (a + (1 << b) - 1) >> b;
62 * Divide an integer and round upwards.
66 static int int_ceildiv(int a, int b) {
67 return (a + b - 1) / b;
70 /* -->> -->> -->> -->>
74 <<-- <<-- <<-- <<-- */
76 /* WORD defines a two byte word */
77 typedef unsigned short int WORD;
79 /* DWORD defines a four byte word */
80 typedef unsigned long int DWORD;
83 WORD bfType; /* 'BM' for Bitmap (19776) */
84 DWORD bfSize; /* Size of the file */
85 WORD bfReserved1; /* Reserved : 0 */
86 WORD bfReserved2; /* Reserved : 0 */
87 DWORD bfOffBits; /* Offset */
91 DWORD biSize; /* Size of the structure in bytes */
92 DWORD biWidth; /* Width of the image in pixels */
93 DWORD biHeight; /* Heigth of the image in pixels */
94 WORD biPlanes; /* 1 */
95 WORD biBitCount; /* Number of color bits by pixels */
96 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
97 DWORD biSizeImage; /* Size of the image in bytes */
98 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
99 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
100 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
101 DWORD biClrImportant; /* Number of important color (0: ALL) */
102 } BITMAPINFOHEADER_t;
104 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
105 int subsampling_dx = parameters->subsampling_dx;
106 int subsampling_dy = parameters->subsampling_dy;
108 int i, numcomps, w, h;
109 OPJ_COLOR_SPACE color_space;
110 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
111 opj_image_t * image = NULL;
114 BITMAPFILEHEADER_t File_h;
115 BITMAPINFOHEADER_t Info_h;
117 unsigned char *table_R, *table_G, *table_B;
118 unsigned int j, PAD = 0;
121 int gray_scale = 1, not_end_file = 1;
123 unsigned int line = 0, col = 0;
127 IN = fopen(filename, "rb");
129 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
133 File_h.bfType = getc(IN);
134 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
136 if (File_h.bfType != 19778) {
137 fprintf(stderr,"Error, not a BMP file!\n");
142 File_h.bfSize = getc(IN);
143 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
144 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
145 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
147 File_h.bfReserved1 = getc(IN);
148 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
150 File_h.bfReserved2 = getc(IN);
151 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
153 File_h.bfOffBits = getc(IN);
154 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
155 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
156 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
161 Info_h.biSize = getc(IN);
162 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
163 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
164 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
166 Info_h.biWidth = getc(IN);
167 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
168 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
169 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
172 Info_h.biHeight = getc(IN);
173 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
174 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
175 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
178 Info_h.biPlanes = getc(IN);
179 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
181 Info_h.biBitCount = getc(IN);
182 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
184 Info_h.biCompression = getc(IN);
185 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
186 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
187 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
189 Info_h.biSizeImage = getc(IN);
190 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
191 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
192 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
194 Info_h.biXpelsPerMeter = getc(IN);
195 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
196 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
197 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
199 Info_h.biYpelsPerMeter = getc(IN);
200 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
201 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
202 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
204 Info_h.biClrUsed = getc(IN);
205 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
206 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
207 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
209 Info_h.biClrImportant = getc(IN);
210 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
211 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
212 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
214 /* Read the data and store them in the OUT file */
216 if (Info_h.biBitCount == 24) {
218 color_space = CLRSPC_SRGB;
219 /* initialize image components */
220 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
221 for(i = 0; i < numcomps; i++) {
222 cmptparm[i].prec = 8;
224 cmptparm[i].sgnd = 0;
225 cmptparm[i].dx = subsampling_dx;
226 cmptparm[i].dy = subsampling_dy;
230 /* create the image */
231 image = opj_image_create(numcomps, &cmptparm[0], color_space);
237 /* set image offset and reference grid */
238 image->x0 = parameters->image_offset_x0;
239 image->y0 = parameters->image_offset_y0;
240 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
241 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
245 /* Place the cursor at the beginning of the image information */
246 fseek(IN, 0, SEEK_SET);
247 fseek(IN, File_h.bfOffBits, SEEK_SET);
252 /* PAD = 4 - (3 * W) % 4; */
253 /* PAD = (PAD == 4) ? 0 : PAD; */
254 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
256 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
258 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
262 for(y = 0; y < (int)H; y++) {
263 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
264 for(x = 0; x < (int)W; x++) {
265 unsigned char *pixel = &scanline[3 * x];
266 image->comps[0].data[index] = pixel[2]; /* R */
267 image->comps[1].data[index] = pixel[1]; /* G */
268 image->comps[2].data[index] = pixel[0]; /* B */
275 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
276 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
277 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
278 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
280 for (j = 0; j < Info_h.biClrUsed; j++) {
281 table_B[j] = getc(IN);
282 table_G[j] = getc(IN);
283 table_R[j] = getc(IN);
285 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
289 /* Place the cursor at the beginning of the image information */
290 fseek(IN, 0, SEEK_SET);
291 fseek(IN, File_h.bfOffBits, SEEK_SET);
295 if (Info_h.biWidth % 2)
298 numcomps = gray_scale ? 1 : 3;
299 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
300 /* initialize image components */
301 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
302 for(i = 0; i < numcomps; i++) {
303 cmptparm[i].prec = 8;
305 cmptparm[i].sgnd = 0;
306 cmptparm[i].dx = subsampling_dx;
307 cmptparm[i].dy = subsampling_dy;
311 /* create the image */
312 image = opj_image_create(numcomps, &cmptparm[0], color_space);
318 /* set image offset and reference grid */
319 image->x0 = parameters->image_offset_x0;
320 image->y0 = parameters->image_offset_y0;
321 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
322 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
326 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
328 fread(RGB, sizeof(unsigned char), W * H, IN);
331 for (j = 0; j < W * H; j++) {
332 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
333 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
340 for (j = 0; j < W * H; j++) {
341 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
342 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
343 image->comps[0].data[index] = table_R[pixel_index];
344 image->comps[1].data[index] = table_G[pixel_index];
345 image->comps[2].data[index] = table_B[pixel_index];
354 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
355 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
356 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
357 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
359 for (j = 0; j < Info_h.biClrUsed; j++) {
360 table_B[j] = getc(IN);
361 table_G[j] = getc(IN);
362 table_R[j] = getc(IN);
364 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
368 numcomps = gray_scale ? 1 : 3;
369 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
370 /* initialize image components */
371 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
372 for(i = 0; i < numcomps; i++) {
373 cmptparm[i].prec = 8;
375 cmptparm[i].sgnd = 0;
376 cmptparm[i].dx = subsampling_dx;
377 cmptparm[i].dy = subsampling_dy;
381 /* create the image */
382 image = opj_image_create(numcomps, &cmptparm[0], color_space);
388 /* set image offset and reference grid */
389 image->x0 = parameters->image_offset_x0;
390 image->y0 = parameters->image_offset_y0;
391 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
392 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
396 /* Place the cursor at the beginning of the image information */
397 fseek(IN, 0, SEEK_SET);
398 fseek(IN, File_h.bfOffBits, SEEK_SET);
400 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
402 while (not_end_file) {
406 for (i = 0; i < (int) v; i++) {
407 RGB[line * Info_h.biWidth + col] = v2;
422 fprintf(stderr,"No Delta supported\n");
423 opj_image_destroy(image);
427 for (i = 0; i < v; i++) {
429 RGB[line * Info_h.biWidth + col] = v2;
440 for (line = 0; line < Info_h.biHeight; line++) {
441 for (col = 0; col < Info_h.biWidth; col++) {
442 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
448 for (line = 0; line < Info_h.biHeight; line++) {
449 for (col = 0; col < Info_h.biWidth; col++) {
450 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
451 image->comps[0].data[index] = table_R[pixel_index];
452 image->comps[1].data[index] = table_G[pixel_index];
453 image->comps[2].data[index] = table_B[pixel_index];
464 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
472 int imagetobmp(opj_image_t * image, const char *outfile) {
477 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
478 && image->comps[1].dx == image->comps[2].dx
479 && image->comps[0].dy == image->comps[1].dy
480 && image->comps[1].dy == image->comps[2].dy
481 && image->comps[0].prec == image->comps[1].prec
482 && image->comps[1].prec == image->comps[2].prec) {
484 /* -->> -->> -->> -->>
486 <<-- <<-- <<-- <<-- */
488 fdest = fopen(outfile, "wb");
490 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
494 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
495 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
496 w = image->comps[0].w;
497 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
499 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
500 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
501 h = image->comps[0].h;
502 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
504 fprintf(fdest, "BM");
508 fprintf(fdest, "%c%c%c%c",
509 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
510 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
511 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
512 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
513 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
514 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
518 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
519 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
520 (unsigned char) ((wr) >> 8) & 0xff,
521 (unsigned char) ((wr) >> 16) & 0xff,
522 (unsigned char) ((wr) >> 24) & 0xff);
523 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
524 (unsigned char) ((hr) >> 8) & 0xff,
525 (unsigned char) ((hr) >> 16) & 0xff,
526 (unsigned char) ((hr) >> 24) & 0xff);
527 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
528 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
529 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
530 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
531 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
532 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
533 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
536 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
537 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
539 for (i = 0; i < wr * hr; i++) {
540 unsigned char R, G, B;
542 /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
543 R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
544 /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
545 G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
546 /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
547 B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
548 fprintf(fdest, "%c%c%c", B, G, R);
550 if ((i + 1) % wr == 0) {
551 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
552 fprintf(fdest, "%c", 0);
556 } else { /* Gray-scale */
558 /* -->> -->> -->> -->>
559 8 bits non code (Gray scale)
560 <<-- <<-- <<-- <<-- */
562 fdest = fopen(outfile, "wb");
563 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
564 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
565 w = image->comps[0].w;
566 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
568 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
569 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
570 h = image->comps[0].h;
571 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
573 fprintf(fdest, "BM");
577 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
578 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
579 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
580 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
581 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
582 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
583 ((54 + 1024) >> 16) & 0xff,
584 ((54 + 1024) >> 24) & 0xff);
588 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
589 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
590 (unsigned char) ((wr) >> 8) & 0xff,
591 (unsigned char) ((wr) >> 16) & 0xff,
592 (unsigned char) ((wr) >> 24) & 0xff);
593 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
594 (unsigned char) ((hr) >> 8) & 0xff,
595 (unsigned char) ((hr) >> 16) & 0xff,
596 (unsigned char) ((hr) >> 24) & 0xff);
597 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
598 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
599 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
600 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
601 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
602 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
603 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
606 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
607 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
609 for (i = 0; i < 256; i++) {
610 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
613 for (i = 0; i < wr * hr; i++) {
615 /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
616 fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
617 /*if (((i + 1) % w == 0 && w % 2))
618 fprintf(fdest, "%c", 0); */
619 if ((i + 1) % wr == 0) {
620 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
621 fprintf(fdest, "%c", 0);
630 /* -->> -->> -->> -->>
634 <<-- <<-- <<-- <<-- */
637 unsigned char readuchar(FILE * f)
644 unsigned short readushort(FILE * f, int bigendian)
646 unsigned char c1, c2;
650 return (c1 << 8) + c2;
652 return (c2 << 8) + c1;
655 unsigned int readuint(FILE * f, int bigendian)
657 unsigned char c1, c2, c3, c4;
663 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
665 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
668 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
671 int i, numcomps, max;
672 OPJ_COLOR_SPACE color_space;
673 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
674 opj_image_t * image = NULL;
676 char endian1,endian2,sign;
681 opj_image_comp_t *comp = NULL;
684 color_space = CLRSPC_GRAY;
686 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
690 f = fopen(filename, "rb");
692 fprintf(stderr, "Failed to open %s for reading !\n", filename);
696 fseek(f, 0, SEEK_SET);
697 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
701 while (signtmp[i]!='\0') {
702 if (signtmp[i]=='-') sign='-';
707 if (endian1=='M' && endian2=='L') {
709 } else if (endian2=='M' && endian1=='L') {
712 fprintf(stderr, "Bad pgx header, please check input file\n");
716 /* initialize image component */
718 cmptparm.x0 = parameters->image_offset_x0;
719 cmptparm.y0 = parameters->image_offset_y0;
720 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
721 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
728 cmptparm.prec = prec;
730 cmptparm.dx = parameters->subsampling_dx;
731 cmptparm.dy = parameters->subsampling_dy;
733 /* create the image */
734 image = opj_image_create(numcomps, &cmptparm, color_space);
739 /* set image offset and reference grid */
740 image->x0 = cmptparm.x0;
741 image->y0 = cmptparm.x0;
742 image->x1 = cmptparm.w;
743 image->y1 = cmptparm.h;
747 comp = &image->comps[0];
749 for (i = 0; i < w * h; i++) {
751 if (comp->prec <= 8) {
755 v = (char) readuchar(f);
757 } else if (comp->prec <= 16) {
759 v = readushort(f, bigendian);
761 v = (short) readushort(f, bigendian);
765 v = readuint(f, bigendian);
767 v = (int) readuint(f, bigendian);
775 comp->bpp = int_floorlog2(max) + 1;
780 int imagetopgx(opj_image_t * image, const char *outfile) {
785 for (compno = 0; compno < image->numcomps; compno++) {
786 opj_image_comp_t *comp = &image->comps[compno];
787 char bname[256]; /* buffer for name */
788 char *name = bname; /* pointer */
790 const size_t olen = strlen(outfile);
791 const size_t dotpos = olen - 4;
792 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
793 if( outfile[dotpos] != '.' ) {
794 /* `pgx` was recognized but there is no dot at expected position */
795 fprintf(stderr, "ERROR -> Impossible happen." );
799 name = (char*)malloc(total+1);
801 strncpy(name, outfile, dotpos);
802 if (image->numcomps > 1) {
803 sprintf(name+dotpos, "-%d.pgx", compno);
805 strcpy(name+dotpos, ".pgx");
807 fdest = fopen(name, "wb");
809 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
812 /* dont need name anymore */
816 /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */
817 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */
818 w = image->comps[compno].w;
819 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
821 /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */
822 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */
823 h = image->comps[compno].h;
824 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
826 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
827 if (comp->prec <= 8) {
829 } else if (comp->prec <= 16) {
834 for (i = 0; i < wr * hr; i++) {
835 int v = image->comps[compno].data[i / wr * w + i % wr];
836 for (j = nbytes - 1; j >= 0; j--) {
837 char byte = (char) (v >> (j * 8));
838 fwrite(&byte, 1, 1, fdest);
847 /* -->> -->> -->> -->>
851 <<-- <<-- <<-- <<-- */
853 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
854 int subsampling_dx = parameters->subsampling_dx;
855 int subsampling_dy = parameters->subsampling_dy;
858 int i, compno, numcomps, w, h;
859 OPJ_COLOR_SPACE color_space;
860 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
861 opj_image_t * image = NULL;
864 f = fopen(filename, "rb");
866 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
875 case '2': /* greyscale image type */
878 color_space = CLRSPC_GRAY;
881 case '3': /* RGB image type */
884 color_space = CLRSPC_SRGB;
895 while(fgetc(f) == '#') while(fgetc(f) != '\n');
897 fseek(f, -1, SEEK_CUR);
898 fscanf(f, "%d %d\n255", &w, &h);
899 fgetc(f); /* <cr><lf> */
901 /* initialize image components */
902 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
903 for(i = 0; i < numcomps; i++) {
904 cmptparm[i].prec = 8;
906 cmptparm[i].sgnd = 0;
907 cmptparm[i].dx = subsampling_dx;
908 cmptparm[i].dy = subsampling_dy;
912 /* create the image */
913 image = opj_image_create(numcomps, &cmptparm[0], color_space);
919 /* set image offset and reference grid */
920 image->x0 = parameters->image_offset_x0;
921 image->y0 = parameters->image_offset_y0;
922 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
923 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
927 if ((value == '2') || (value == '3')) { /* ASCII */
928 for (i = 0; i < w * h; i++) {
929 for(compno = 0; compno < numcomps; compno++) {
930 unsigned int index = 0;
931 fscanf(f, "%u", &index);
932 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
933 image->comps[compno].data[i] = index;
936 } else if ((value == '5') || (value == '6')) { /* BINARY */
937 for (i = 0; i < w * h; i++) {
938 for(compno = 0; compno < numcomps; compno++) {
939 unsigned char index = 0;
940 fread(&index, 1, 1, f);
941 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
942 image->comps[compno].data[i] = index;
952 int imagetopnm(opj_image_t * image, const char *outfile) {
953 int w, wr, wrr, h, hr, hrr, max;
958 const char *tmp = outfile;
967 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
968 && image->comps[1].dx == image->comps[2].dx
969 && image->comps[0].dy == image->comps[1].dy
970 && image->comps[1].dy == image->comps[2].dy
971 && image->comps[0].prec == image->comps[1].prec
972 && image->comps[1].prec == image->comps[2].prec
973 && S2 !='g' && S2 !='G') {
975 fdest = fopen(outfile, "wb");
977 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
981 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
982 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */
983 wr = image->comps[0].w;
984 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
986 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
987 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
988 hr = image->comps[0].h;
989 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
991 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
993 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
994 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
996 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
997 adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
998 for (i = 0; i < wrr * hrr; i++) {
1000 unsigned char rc,gc,bc;
1001 r = image->comps[0].data[i / wrr * wr + i % wrr];
1002 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1003 rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
1005 g = image->comps[1].data[i / wrr * wr + i % wrr];
1006 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1007 gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
1009 b = image->comps[2].data[i / wrr * wr + i % wrr];
1010 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1011 bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
1013 fprintf(fdest, "%c%c%c", rc, gc, bc);
1017 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1018 if (image->numcomps>ncomp) {
1019 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1020 fprintf(stderr," is written to the file\n");
1022 for (compno = 0; compno < ncomp; compno++) {
1025 sprintf(name, "%d.%s", compno, outfile);
1027 sprintf(name, "%s", outfile);
1030 fdest = fopen(name, "wb");
1032 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1036 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1037 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */
1038 wr = image->comps[compno].w;
1039 wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1041 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1042 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */
1043 hr = image->comps[compno].h;
1044 hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1046 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1048 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1049 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1051 fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1052 adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1054 for (i = 0; i < wrr * hrr; i++) {
1057 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1058 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1059 lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1060 fprintf(fdest, "%c", lc);
1069 /* -->> -->> -->> -->>
1073 <<-- <<-- <<-- <<-- */
1075 typedef struct tiff_infoheader{
1076 DWORD tiWidth; // Width of Image in pixel
1077 DWORD tiHeight; // Height of Image in pixel
1078 DWORD tiPhoto; // Photometric
1079 WORD tiBps; // Bits per sample
1080 WORD tiSf; // Sample Format
1081 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1082 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1085 int imagetotif(opj_image_t * image, const char *outfile) {
1093 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1094 && image->comps[1].dx == image->comps[2].dx
1095 && image->comps[0].dy == image->comps[1].dy
1096 && image->comps[1].dy == image->comps[2].dy
1097 && image->comps[0].prec == image->comps[1].prec
1098 && image->comps[1].prec == image->comps[2].prec) {
1104 tif = TIFFOpen(outfile, "wb");
1106 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1110 width = image->comps[0].w;
1111 height= 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<TIFFStripSize(tif); i+=3) { // 8 bits per pixel
1134 dat8[i+0] = image->comps[0].data[index] ; // R
1135 dat8[i+1] = image->comps[1].data[index] ; // G
1136 dat8[i+2] = image->comps[2].data[index] ; // B
1139 }else if (image->comps[0].prec == 12){
1140 for (i=0; i<TIFFStripSize(tif); i+=9) { // 12 bits per pixel
1141 dat8[i+0] = (image->comps[0].data[index]>>8)<<4 | (image->comps[0].data[index]>>4);
1142 dat8[i+1] = (image->comps[0].data[index]<<4)|((image->comps[1].data[index]>>8)& 0x0f);
1143 dat8[i+2] = (image->comps[1].data[index]);
1144 dat8[i+3] = (image->comps[2].data[index]>>8)<<4 | (image->comps[2].data[index]>>4);
1145 dat8[i+4] = (image->comps[2].data[index]<<4)|((image->comps[1].data[index+1]>>8)& 0x0f);
1146 dat8[i+5] = (image->comps[0].data[index+1]);
1147 dat8[i+6] = (image->comps[1].data[index+1]>>8)<<4 | (image->comps[1].data[index+1]>>4);
1148 dat8[i+7] = (image->comps[1].data[index+1]<<4)|((image->comps[2].data[index+1]>>8)& 0x0f);
1149 dat8[i+8] = (image->comps[2].data[index+1]);
1152 }else if (image->comps[0].prec == 16){
1153 for (i=0; i<TIFFStripSize(tif); i+=6) { // 16 bits per pixel
1154 dat8[i+0] = image->comps[0].data[index];//LSB
1155 dat8[i+1] = (image->comps[0].data[index]>> 8);//MSB
1156 dat8[i+2] = image->comps[1].data[index];
1157 dat8[i+3] = (image->comps[1].data[index]>> 8);
1158 dat8[i+4] = image->comps[2].data[index];
1159 dat8[i+5] = (image->comps[2].data[index]>> 8);
1163 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1164 fprintf(stderr,"Aborting\n");
1167 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1171 }else if (image->numcomps == 1){
1176 tif = TIFFOpen(outfile, "wb");
1178 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1182 width = image->comps[0].w;
1183 height= image->comps[0].h;
1184 bps = image->comps[0].prec;
1187 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1188 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1189 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1190 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1191 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1192 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1193 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1194 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1196 /* Get a buffer for the data */
1197 buf = _TIFFmalloc(TIFFStripSize(tif));
1200 strip_size = TIFFStripSize(tif);
1201 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1202 unsigned char *dat8;
1205 if (image->comps[0].prec == 8){
1206 for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
1207 dat8[i+0] = image->comps[0].data[index] ;
1210 }else if (image->comps[0].prec == 12){
1211 for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
1212 dat8[i+0] = (image->comps[0].data[index]>>8)<<4 | (image->comps[0].data[index]>>4);
1213 dat8[i+1] = (image->comps[0].data[index]<<4)|((image->comps[0].data[index+1]>>8)& 0x0f);
1214 dat8[i+2] = (image->comps[0].data[index+1]);
1217 }else if (image->comps[0].prec == 16){
1218 for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
1219 dat8[i+0] = image->comps[0].data[index];
1220 dat8[i+1] = (image->comps[0].data[index]>> 8);
1224 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1225 fprintf(stderr,"Aborting\n");
1228 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1233 fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
1234 fprintf(stderr,"Aborting\n");
1240 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
1242 int subsampling_dx = parameters->subsampling_dx;
1243 int subsampling_dy = parameters->subsampling_dy;
1245 tiff_infoheader_t Info;
1249 int j, numcomps, w, h,index;
1250 OPJ_COLOR_SPACE color_space;
1251 opj_image_cmptparm_t cmptparm[3];
1252 opj_image_t * image = NULL;
1254 tif = TIFFOpen(filename, "r");
1257 fprintf(stderr, "Failed to open %s for reading\n", filename);
1261 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1262 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1263 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1264 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1265 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1267 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1268 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1272 if (Info.tiPhoto == 2) {
1278 color_space = CLRSPC_SRGB;
1279 /* initialize image components*/
1280 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1281 for(j = 0; j < numcomps; j++) {
1282 if (parameters->cp_cinema) {
1283 cmptparm[j].prec = 12;
1284 cmptparm[j].bpp = 12;
1286 cmptparm[j].prec = Info.tiBps;
1287 cmptparm[j].bpp = Info.tiBps;
1289 cmptparm[j].sgnd = 0;
1290 cmptparm[j].dx = subsampling_dx;
1291 cmptparm[j].dy = subsampling_dy;
1295 /* create the image*/
1296 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1302 /* set image offset and reference grid */
1303 image->x0 = parameters->image_offset_x0;
1304 image->y0 = parameters->image_offset_y0;
1305 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1306 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1308 buf = _TIFFmalloc(TIFFStripSize(tif));
1310 strip_size=TIFFStripSize(tif);
1312 /* Read the Image components*/
1313 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1314 unsigned char *dat8;
1316 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1319 if (Info.tiBps==12){
1320 for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
1321 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
1322 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1323 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
1324 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1325 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
1326 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1330 else if( Info.tiBps==16){
1331 for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
1332 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
1333 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
1334 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
1335 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1336 image->comps[0].data[index] = (image->comps[0].data[index] + 0x08) >> 4 ;
1337 image->comps[1].data[index] = (image->comps[1].data[index] + 0x08) >> 4 ;
1338 image->comps[2].data[index] = (image->comps[2].data[index] + 0x08) >> 4 ;
1343 else if ( Info.tiBps==8){
1344 for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
1345 image->comps[0].data[index] = dat8[i+0]; // R
1346 image->comps[1].data[index] = dat8[i+1]; // G
1347 image->comps[2].data[index] = dat8[i+2]; // B
1348 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1349 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
1350 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
1351 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
1357 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1358 fprintf(stderr,"Aborting\n");
1365 }else if(Info.tiPhoto == 1) {
1371 color_space = CLRSPC_GRAY;
1372 /* initialize image components*/
1373 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1374 cmptparm[0].prec = Info.tiBps;
1375 cmptparm[0].bpp = Info.tiBps;
1376 cmptparm[0].sgnd = 0;
1377 cmptparm[0].dx = subsampling_dx;
1378 cmptparm[0].dy = subsampling_dy;
1382 /* create the image*/
1383 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1388 /* set image offset and reference grid */
1389 image->x0 = parameters->image_offset_x0;
1390 image->y0 = parameters->image_offset_y0;
1391 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1392 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1394 buf = _TIFFmalloc(TIFFStripSize(tif));
1396 strip_size = TIFFStripSize(tif);
1398 /* Read the Image components*/
1399 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1400 unsigned char *dat8;
1402 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1405 if (Info.tiBps==12){
1406 for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
1407 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
1408 image->comps[0].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1412 else if( Info.tiBps==16){
1413 for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
1414 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
1418 else if ( Info.tiBps==8){
1419 for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
1420 image->comps[0].data[index] = dat8[i+0];
1425 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1426 fprintf(stderr,"Aborting\n");
1434 fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
1435 fprintf(stderr,"Aborting\n");
1441 /* -->> -->> -->> -->>
1445 <<-- <<-- <<-- <<-- */
1447 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
1448 int subsampling_dx = parameters->subsampling_dx;
1449 int subsampling_dy = parameters->subsampling_dy;
1452 int i, compno, numcomps, w, h;
1453 OPJ_COLOR_SPACE color_space;
1454 opj_image_cmptparm_t *cmptparm;
1455 opj_image_t * image = NULL;
1458 if((raw_cp->rawWidth * raw_cp->rawHeight * raw_cp->rawComp * raw_cp->rawBitDepth) == 0)
1460 fprintf(stderr,"\nError: invalid raw image parameters\n");
1461 fprintf(stderr,"Please use the Format option -F:\n");
1462 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
1463 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
1464 fprintf(stderr,"Aborting\n");
1468 f = fopen(filename, "rb");
1470 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1471 fprintf(stderr,"Aborting\n");
1474 numcomps = raw_cp->rawComp;
1475 color_space = CLRSPC_SRGB;
1476 w = raw_cp->rawWidth;
1477 h = raw_cp->rawHeight;
1478 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
1480 /* initialize image components */
1481 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1482 for(i = 0; i < numcomps; i++) {
1483 cmptparm[i].prec = raw_cp->rawBitDepth;
1484 cmptparm[i].bpp = raw_cp->rawBitDepth;
1485 cmptparm[i].sgnd = raw_cp->rawSigned;
1486 cmptparm[i].dx = subsampling_dx;
1487 cmptparm[i].dy = subsampling_dy;
1491 /* create the image */
1492 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1498 /* set image offset and reference grid */
1499 image->x0 = parameters->image_offset_x0;
1500 image->y0 = parameters->image_offset_y0;
1501 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1502 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1504 if(raw_cp->rawBitDepth <= 8)
1506 unsigned char value = 0;
1507 for(compno = 0; compno < numcomps; compno++) {
1508 for (i = 0; i < w * h; i++) {
1509 if (!fread(&value, 1, 1, f)) {
1510 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
1513 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
1519 unsigned short value = 0;
1520 for(compno = 0; compno < numcomps; compno++) {
1521 for (i = 0; i < w * h; i++) {
1522 if (!fread(&value, 2, 1, f)) {
1523 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
1526 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
1531 if (fread(&ch, 1, 1, f)) {
1532 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
1540 int imagetoraw(opj_image_t * image, const char *outfile)
1542 FILE *rawFile = NULL;
1543 int compno, pixelsToWrite, offset, cont;
1545 if((image->numcomps * image->x1 * image->y1) == 0)
1547 fprintf(stderr,"\nError: invalid raw image parameters\n");
1551 rawFile = fopen(outfile, "wb");
1553 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
1557 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
1559 for(compno = 0; compno < image->numcomps; compno++)
1561 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
1562 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
1564 pixelsToWrite = image->comps[compno].w * image->comps[compno].h;
1567 if(image->comps[compno].prec <= 8)
1569 if(image->comps[compno].sgnd == 1)
1572 int mask = (1 << image->comps[compno].prec) - 1;
1573 for(cont = 0; cont < pixelsToWrite; cont++)
1575 curr = (signed char) (image->comps[compno].data[cont] & mask);
1576 fwrite(&curr, sizeof(signed char), 1, rawFile);
1579 else if(image->comps[compno].sgnd == 0)
1582 int mask = (1 << image->comps[compno].prec) - 1;
1583 for(cont = 0; cont < pixelsToWrite; cont++)
1585 curr = (unsigned char) (image->comps[compno].data[cont] & mask);
1586 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
1590 else if(image->comps[compno].prec <= 16)
1592 if(image->comps[compno].sgnd == 1)
1594 signed short int curr;
1595 int mask = (1 << image->comps[compno].prec) - 1;
1596 for(cont = 0; cont < pixelsToWrite; cont++)
1598 curr = (signed short int) (image->comps[compno].data[cont] & mask);
1599 fwrite(&curr, sizeof(signed short int), 1, rawFile);
1602 else if(image->comps[compno].sgnd == 0)
1604 unsigned short int curr;
1605 int mask = (1 << image->comps[compno].prec) - 1;
1606 for(cont = 0; cont < pixelsToWrite; cont++)
1608 curr = (unsigned short int) (image->comps[compno].data[cont] & mask);
1609 fwrite(&curr, sizeof(unsigned short int), 1, rawFile);
1613 else if (image->comps[compno].prec <= 32)
1620 fprintf(stderr,"\nError: invalid precision\n");