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;
71 /* -->> -->> -->> -->>
75 <<-- <<-- <<-- <<-- */
77 // TGA header definition.
78 #pragma pack(push,1) // Pack structure byte aligned
79 typedef struct tga_header
81 uint8 id_length; /* Image id field length */
82 uint8 colour_map_type; /* Colour map type */
83 uint8 image_type; /* Image type */
85 ** Colour map specification
87 uint16 colour_map_index; /* First entry index */
88 uint16 colour_map_length; /* Colour map length */
89 uint8 colour_map_entry_size; /* Colour map entry size */
91 ** Image specification
93 uint16 x_origin; /* x origin of image */
94 uint16 y_origin; /* u origin of image */
95 uint16 image_width; /* Image width */
96 uint16 image_height; /* Image height */
97 uint8 pixel_depth; /* Pixel depth */
98 uint8 image_desc; /* Image descriptor */
100 #pragma pack(pop) // Return to normal structure packing alignment.
102 int tga_readheader(FILE *fp, int *bits_per_pixel, int *width, int *height, int *flip_image)
107 if (!bits_per_pixel || !width || !height || !flip_image)
111 fread((uint8*)&tga, sizeof(tga_header), 1, fp);
113 *bits_per_pixel = tga.pixel_depth;
115 *width = tga.image_width;
116 *height = tga.image_height ;
118 // Ignore tga identifier, if present ...
121 uint8 *id = (uint8 *) malloc(tga.id_length);
122 fread(id, tga.id_length, 1, fp);
126 // Test for compressed formats ... not yet supported ...
127 // Note :- 9 - RLE encoded palettized.
128 // 10 - RLE encoded RGB.
129 if (tga.image_type > 8)
131 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
135 *flip_image = !(tga.image_desc & 32);
137 // Palettized formats are not yet supported, skip over the palette, if present ...
138 palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
142 fprintf(stderr, "File contains a palette - not yet supported.");
143 fseek(fp, palette_size, SEEK_CUR);
148 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, bool flip_image)
152 if (!bits_per_pixel || !width || !height)
155 memset(&tga, 0, sizeof(tga_header));
157 tga.pixel_depth = bits_per_pixel;
158 tga.image_width = width;
159 tga.image_height = height;
160 tga.image_type = 2; // Uncompressed.
161 tga.image_desc = 8; // 8 bits per component.
164 tga.image_desc |= 32;
167 fwrite((uint8*)&tga, sizeof(tga_header), 1, fp);
172 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
175 uint32 image_width, image_height, pixel_bit_depth;
178 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
180 OPJ_COLOR_SPACE color_space;
183 int subsampling_dx, subsampling_dy;
186 f = fopen(filename, "rb");
188 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
192 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
195 // We currently only support 24 & 32 bit tga's ...
196 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
199 /* initialize image components */
200 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
202 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha.
203 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
206 color_space = CLRSPC_GRAY;
207 numcomps = save_alpha ? 2 : 1;
210 numcomps = save_alpha ? 4 : 3;
211 color_space = CLRSPC_SRGB;
214 subsampling_dx = parameters->subsampling_dx;
215 subsampling_dy = parameters->subsampling_dy;
217 for (i = 0; i < numcomps; i++) {
218 cmptparm[i].prec = 8;
220 cmptparm[i].sgnd = 0;
221 cmptparm[i].dx = subsampling_dx;
222 cmptparm[i].dy = subsampling_dy;
223 cmptparm[i].w = image_width;
224 cmptparm[i].h = image_height;
227 /* create the image */
228 image = opj_image_create(numcomps, &cmptparm[0], color_space);
233 /* set image offset and reference grid */
234 image->x0 = parameters->image_offset_x0;
235 image->y0 = parameters->image_offset_y0;
236 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
237 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
240 for (y=0; y < image_height; y++)
245 index = (image_height-y-1)*image_width;
247 index = y*image_width;
251 for (x=0;x<image_width;x++)
258 image->comps[0].data[index]=r;
259 image->comps[1].data[index]=g;
260 image->comps[2].data[index]=b;
264 else if (numcomps==4)
266 for (x=0;x<image_width;x++)
274 image->comps[0].data[index]=r;
275 image->comps[1].data[index]=g;
276 image->comps[2].data[index]=b;
277 image->comps[3].data[index]=a;
282 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
288 int imagetotga(opj_image_t * image, const char *outfile) {
289 int width, height, bpp, x, y;
292 uint32 alpha_channel;
298 fdest = fopen(outfile, "wb");
300 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
304 for (i = 0; i < image->numcomps-1; i++) {
305 if ((image->comps[0].dx != image->comps[i+1].dx)
306 ||(image->comps[0].dy != image->comps[i+1].dy)
307 ||(image->comps[0].prec != image->comps[i+1].prec)) {
308 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
313 width = image->comps[0].w;
314 height = image->comps[0].h;
316 // Mono with alpha, or RGB with alpha.
317 write_alpha = (image->numcomps==2) || (image->numcomps==4);
320 bpp = write_alpha ? 32 : 24;
321 if (!tga_writeheader(fdest, bpp, width , height, true))
324 alpha_channel = image->numcomps-1;
326 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
328 for (y=0; y < height; y++) {
329 uint32 index=y*width;
331 for (x=0; x < width; x++, index++) {
332 r = (float)(image->comps[0].data[index]);
334 if (image->numcomps>2) {
335 g = (float)(image->comps[1].data[index]);
336 b = (float)(image->comps[2].data[index]);
338 else {// Greyscale ...
343 // TGA format writes BGR ...
344 value = (uint8)(b*scale);
345 fwrite(&value,1,1,fdest);
347 value = (uint8)(g*scale);
348 fwrite(&value,1,1,fdest);
350 value = (uint8)(r*scale);
351 fwrite(&value,1,1,fdest);
354 a = (float)(image->comps[alpha_channel].data[index]);
355 value = (uint8)(a*scale);
356 fwrite(&value,1,1,fdest);
364 /* -->> -->> -->> -->>
368 <<-- <<-- <<-- <<-- */
370 /* WORD defines a two byte word */
371 typedef unsigned short int WORD;
373 /* DWORD defines a four byte word */
374 typedef unsigned long int DWORD;
377 WORD bfType; /* 'BM' for Bitmap (19776) */
378 DWORD bfSize; /* Size of the file */
379 WORD bfReserved1; /* Reserved : 0 */
380 WORD bfReserved2; /* Reserved : 0 */
381 DWORD bfOffBits; /* Offset */
382 } BITMAPFILEHEADER_t;
385 DWORD biSize; /* Size of the structure in bytes */
386 DWORD biWidth; /* Width of the image in pixels */
387 DWORD biHeight; /* Heigth of the image in pixels */
388 WORD biPlanes; /* 1 */
389 WORD biBitCount; /* Number of color bits by pixels */
390 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
391 DWORD biSizeImage; /* Size of the image in bytes */
392 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
393 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
394 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
395 DWORD biClrImportant; /* Number of important color (0: ALL) */
396 } BITMAPINFOHEADER_t;
398 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
399 int subsampling_dx = parameters->subsampling_dx;
400 int subsampling_dy = parameters->subsampling_dy;
402 int i, numcomps, w, h;
403 OPJ_COLOR_SPACE color_space;
404 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
405 opj_image_t * image = NULL;
408 BITMAPFILEHEADER_t File_h;
409 BITMAPINFOHEADER_t Info_h;
411 unsigned char *table_R, *table_G, *table_B;
412 unsigned int j, PAD = 0;
415 int gray_scale = 1, not_end_file = 1;
417 unsigned int line = 0, col = 0;
421 IN = fopen(filename, "rb");
423 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
427 File_h.bfType = getc(IN);
428 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
430 if (File_h.bfType != 19778) {
431 fprintf(stderr,"Error, not a BMP file!\n");
436 File_h.bfSize = getc(IN);
437 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
438 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
439 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
441 File_h.bfReserved1 = getc(IN);
442 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
444 File_h.bfReserved2 = getc(IN);
445 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
447 File_h.bfOffBits = getc(IN);
448 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
449 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
450 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
455 Info_h.biSize = getc(IN);
456 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
457 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
458 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
460 Info_h.biWidth = getc(IN);
461 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
462 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
463 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
466 Info_h.biHeight = getc(IN);
467 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
468 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
469 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
472 Info_h.biPlanes = getc(IN);
473 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
475 Info_h.biBitCount = getc(IN);
476 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
478 Info_h.biCompression = getc(IN);
479 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
480 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
481 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
483 Info_h.biSizeImage = getc(IN);
484 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
485 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
486 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
488 Info_h.biXpelsPerMeter = getc(IN);
489 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
490 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
491 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
493 Info_h.biYpelsPerMeter = getc(IN);
494 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
495 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
496 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
498 Info_h.biClrUsed = getc(IN);
499 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
500 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
501 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
503 Info_h.biClrImportant = getc(IN);
504 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
505 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
506 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
508 /* Read the data and store them in the OUT file */
510 if (Info_h.biBitCount == 24) {
512 color_space = CLRSPC_SRGB;
513 /* initialize image components */
514 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
515 for(i = 0; i < numcomps; i++) {
516 cmptparm[i].prec = 8;
518 cmptparm[i].sgnd = 0;
519 cmptparm[i].dx = subsampling_dx;
520 cmptparm[i].dy = subsampling_dy;
524 /* create the image */
525 image = opj_image_create(numcomps, &cmptparm[0], color_space);
531 /* set image offset and reference grid */
532 image->x0 = parameters->image_offset_x0;
533 image->y0 = parameters->image_offset_y0;
534 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
535 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
539 /* Place the cursor at the beginning of the image information */
540 fseek(IN, 0, SEEK_SET);
541 fseek(IN, File_h.bfOffBits, SEEK_SET);
546 /* PAD = 4 - (3 * W) % 4; */
547 /* PAD = (PAD == 4) ? 0 : PAD; */
548 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
550 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
552 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
556 for(y = 0; y < (int)H; y++) {
557 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
558 for(x = 0; x < (int)W; x++) {
559 unsigned char *pixel = &scanline[3 * x];
560 image->comps[0].data[index] = pixel[2]; /* R */
561 image->comps[1].data[index] = pixel[1]; /* G */
562 image->comps[2].data[index] = pixel[0]; /* B */
569 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
570 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
571 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
572 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
574 for (j = 0; j < Info_h.biClrUsed; j++) {
575 table_B[j] = getc(IN);
576 table_G[j] = getc(IN);
577 table_R[j] = getc(IN);
579 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
583 /* Place the cursor at the beginning of the image information */
584 fseek(IN, 0, SEEK_SET);
585 fseek(IN, File_h.bfOffBits, SEEK_SET);
589 if (Info_h.biWidth % 2)
592 numcomps = gray_scale ? 1 : 3;
593 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
594 /* initialize image components */
595 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
596 for(i = 0; i < numcomps; i++) {
597 cmptparm[i].prec = 8;
599 cmptparm[i].sgnd = 0;
600 cmptparm[i].dx = subsampling_dx;
601 cmptparm[i].dy = subsampling_dy;
605 /* create the image */
606 image = opj_image_create(numcomps, &cmptparm[0], color_space);
612 /* set image offset and reference grid */
613 image->x0 = parameters->image_offset_x0;
614 image->y0 = parameters->image_offset_y0;
615 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
616 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
620 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
622 fread(RGB, sizeof(unsigned char), W * H, IN);
625 for (j = 0; j < W * H; j++) {
626 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
627 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
634 for (j = 0; j < W * H; j++) {
635 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
636 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
637 image->comps[0].data[index] = table_R[pixel_index];
638 image->comps[1].data[index] = table_G[pixel_index];
639 image->comps[2].data[index] = table_B[pixel_index];
648 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
649 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
650 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
651 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
653 for (j = 0; j < Info_h.biClrUsed; j++) {
654 table_B[j] = getc(IN);
655 table_G[j] = getc(IN);
656 table_R[j] = getc(IN);
658 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
662 numcomps = gray_scale ? 1 : 3;
663 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
664 /* initialize image components */
665 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
666 for(i = 0; i < numcomps; i++) {
667 cmptparm[i].prec = 8;
669 cmptparm[i].sgnd = 0;
670 cmptparm[i].dx = subsampling_dx;
671 cmptparm[i].dy = subsampling_dy;
675 /* create the image */
676 image = opj_image_create(numcomps, &cmptparm[0], color_space);
682 /* set image offset and reference grid */
683 image->x0 = parameters->image_offset_x0;
684 image->y0 = parameters->image_offset_y0;
685 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
686 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
690 /* Place the cursor at the beginning of the image information */
691 fseek(IN, 0, SEEK_SET);
692 fseek(IN, File_h.bfOffBits, SEEK_SET);
694 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
696 while (not_end_file) {
700 for (i = 0; i < (int) v; i++) {
701 RGB[line * Info_h.biWidth + col] = v2;
716 fprintf(stderr,"No Delta supported\n");
717 opj_image_destroy(image);
721 for (i = 0; i < v; i++) {
723 RGB[line * Info_h.biWidth + col] = v2;
734 for (line = 0; line < Info_h.biHeight; line++) {
735 for (col = 0; col < Info_h.biWidth; col++) {
736 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
742 for (line = 0; line < Info_h.biHeight; line++) {
743 for (col = 0; col < Info_h.biWidth; col++) {
744 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
745 image->comps[0].data[index] = table_R[pixel_index];
746 image->comps[1].data[index] = table_G[pixel_index];
747 image->comps[2].data[index] = table_B[pixel_index];
758 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
766 int imagetobmp(opj_image_t * image, const char *outfile) {
770 int adjustR, adjustG, adjustB;
772 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
773 && image->comps[1].dx == image->comps[2].dx
774 && image->comps[0].dy == image->comps[1].dy
775 && image->comps[1].dy == image->comps[2].dy
776 && image->comps[0].prec == image->comps[1].prec
777 && image->comps[1].prec == image->comps[2].prec) {
779 /* -->> -->> -->> -->>
781 <<-- <<-- <<-- <<-- */
783 fdest = fopen(outfile, "wb");
785 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
789 w = image->comps[0].w;
790 h = image->comps[0].h;
792 fprintf(fdest, "BM");
796 fprintf(fdest, "%c%c%c%c",
797 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
798 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
799 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
800 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
801 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
802 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
806 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
807 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
808 (unsigned char) ((w) >> 8) & 0xff,
809 (unsigned char) ((w) >> 16) & 0xff,
810 (unsigned char) ((w) >> 24) & 0xff);
811 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
812 (unsigned char) ((h) >> 8) & 0xff,
813 (unsigned char) ((h) >> 16) & 0xff,
814 (unsigned char) ((h) >> 24) & 0xff);
815 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
816 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
817 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
818 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
819 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
820 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
821 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
822 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
823 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
824 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
825 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
827 if (image->comps[0].prec > 8) {
828 adjustR = image->comps[0].prec - 8;
829 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
833 if (image->comps[1].prec > 8) {
834 adjustG = image->comps[1].prec - 8;
835 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
839 if (image->comps[2].prec > 8) {
840 adjustB = image->comps[2].prec - 8;
841 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
846 for (i = 0; i < w * h; i++) {
847 unsigned char rc, gc, bc;
850 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
851 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
852 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
853 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
854 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
855 gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
856 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
857 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
858 bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
860 fprintf(fdest, "%c%c%c", bc, gc, rc);
862 if ((i + 1) % w == 0) {
863 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
864 fprintf(fdest, "%c", 0);
868 } else { /* Gray-scale */
870 /* -->> -->> -->> -->>
871 8 bits non code (Gray scale)
872 <<-- <<-- <<-- <<-- */
874 fdest = fopen(outfile, "wb");
875 w = image->comps[0].w;
876 h = image->comps[0].h;
878 fprintf(fdest, "BM");
882 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
883 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
884 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
885 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
886 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
887 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
888 ((54 + 1024) >> 16) & 0xff,
889 ((54 + 1024) >> 24) & 0xff);
893 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
894 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
895 (unsigned char) ((w) >> 8) & 0xff,
896 (unsigned char) ((w) >> 16) & 0xff,
897 (unsigned char) ((w) >> 24) & 0xff);
898 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
899 (unsigned char) ((h) >> 8) & 0xff,
900 (unsigned char) ((h) >> 16) & 0xff,
901 (unsigned char) ((h) >> 24) & 0xff);
902 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
903 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
904 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
905 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
906 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
907 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
908 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
909 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
910 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
911 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
912 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
914 if (image->comps[0].prec > 8) {
915 adjustR = image->comps[0].prec - 8;
916 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
919 for (i = 0; i < 256; i++) {
920 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
923 for (i = 0; i < w * h; i++) {
927 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
928 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
929 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
931 fprintf(fdest, "%c", rc);
933 if ((i + 1) % w == 0) {
934 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
935 fprintf(fdest, "%c", 0);
944 /* -->> -->> -->> -->>
948 <<-- <<-- <<-- <<-- */
951 unsigned char readuchar(FILE * f)
958 unsigned short readushort(FILE * f, int bigendian)
960 unsigned char c1, c2;
964 return (c1 << 8) + c2;
966 return (c2 << 8) + c1;
969 unsigned int readuint(FILE * f, int bigendian)
971 unsigned char c1, c2, c3, c4;
977 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
979 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
982 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
985 int i, numcomps, max;
986 OPJ_COLOR_SPACE color_space;
987 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
988 opj_image_t * image = NULL;
990 char endian1,endian2,sign;
995 opj_image_comp_t *comp = NULL;
998 color_space = CLRSPC_GRAY;
1000 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1004 f = fopen(filename, "rb");
1006 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1010 fseek(f, 0, SEEK_SET);
1011 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
1015 while (signtmp[i]!='\0') {
1016 if (signtmp[i]=='-') sign='-';
1021 if (endian1=='M' && endian2=='L') {
1023 } else if (endian2=='M' && endian1=='L') {
1026 fprintf(stderr, "Bad pgx header, please check input file\n");
1030 /* initialize image component */
1032 cmptparm.x0 = parameters->image_offset_x0;
1033 cmptparm.y0 = parameters->image_offset_y0;
1034 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1035 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1042 cmptparm.prec = prec;
1043 cmptparm.bpp = prec;
1044 cmptparm.dx = parameters->subsampling_dx;
1045 cmptparm.dy = parameters->subsampling_dy;
1047 /* create the image */
1048 image = opj_image_create(numcomps, &cmptparm, color_space);
1053 /* set image offset and reference grid */
1054 image->x0 = cmptparm.x0;
1055 image->y0 = cmptparm.x0;
1056 image->x1 = cmptparm.w;
1057 image->y1 = cmptparm.h;
1059 /* set image data */
1061 comp = &image->comps[0];
1063 for (i = 0; i < w * h; i++) {
1065 if (comp->prec <= 8) {
1069 v = (char) readuchar(f);
1071 } else if (comp->prec <= 16) {
1073 v = readushort(f, bigendian);
1075 v = (short) readushort(f, bigendian);
1079 v = readuint(f, bigendian);
1081 v = (int) readuint(f, bigendian);
1089 comp->bpp = int_floorlog2(max) + 1;
1094 int imagetopgx(opj_image_t * image, const char *outfile) {
1099 for (compno = 0; compno < image->numcomps; compno++) {
1100 opj_image_comp_t *comp = &image->comps[compno];
1101 char bname[256]; /* buffer for name */
1102 char *name = bname; /* pointer */
1104 const size_t olen = strlen(outfile);
1105 const size_t dotpos = olen - 4;
1106 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1107 if( outfile[dotpos] != '.' ) {
1108 /* `pgx` was recognized but there is no dot at expected position */
1109 fprintf(stderr, "ERROR -> Impossible happen." );
1113 name = (char*)malloc(total+1);
1115 strncpy(name, outfile, dotpos);
1116 if (image->numcomps > 1) {
1117 sprintf(name+dotpos, "-%d.pgx", compno);
1119 strcpy(name+dotpos, ".pgx");
1121 fdest = fopen(name, "wb");
1123 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1126 /* dont need name anymore */
1131 w = image->comps[compno].w;
1132 h = image->comps[compno].h;
1134 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1135 if (comp->prec <= 8) {
1137 } else if (comp->prec <= 16) {
1142 for (i = 0; i < w * h; i++) {
1143 int v = image->comps[compno].data[i];
1144 for (j = nbytes - 1; j >= 0; j--) {
1145 char byte = (char) (v >> (j * 8));
1146 fwrite(&byte, 1, 1, fdest);
1155 /* -->> -->> -->> -->>
1159 <<-- <<-- <<-- <<-- */
1161 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1162 int subsampling_dx = parameters->subsampling_dx;
1163 int subsampling_dy = parameters->subsampling_dy;
1166 int i, compno, numcomps, w, h;
1167 OPJ_COLOR_SPACE color_space;
1168 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
1169 opj_image_t * image = NULL;
1172 f = fopen(filename, "rb");
1174 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1178 if (fgetc(f) != 'P')
1183 case '2': /* greyscale image type */
1186 color_space = CLRSPC_GRAY;
1189 case '3': /* RGB image type */
1192 color_space = CLRSPC_SRGB;
1203 while(fgetc(f) == '#') while(fgetc(f) != '\n');
1205 fseek(f, -1, SEEK_CUR);
1206 fscanf(f, "%d %d\n255", &w, &h);
1207 fgetc(f); /* <cr><lf> */
1209 /* initialize image components */
1210 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1211 for(i = 0; i < numcomps; i++) {
1212 cmptparm[i].prec = 8;
1213 cmptparm[i].bpp = 8;
1214 cmptparm[i].sgnd = 0;
1215 cmptparm[i].dx = subsampling_dx;
1216 cmptparm[i].dy = subsampling_dy;
1220 /* create the image */
1221 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1227 /* set image offset and reference grid */
1228 image->x0 = parameters->image_offset_x0;
1229 image->y0 = parameters->image_offset_y0;
1230 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1231 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1233 /* set image data */
1235 if ((value == '2') || (value == '3')) { /* ASCII */
1236 for (i = 0; i < w * h; i++) {
1237 for(compno = 0; compno < numcomps; compno++) {
1238 unsigned int index = 0;
1239 fscanf(f, "%u", &index);
1240 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1241 image->comps[compno].data[i] = index;
1244 } else if ((value == '5') || (value == '6')) { /* BINARY */
1245 for (i = 0; i < w * h; i++) {
1246 for(compno = 0; compno < numcomps; compno++) {
1247 unsigned char index = 0;
1248 fread(&index, 1, 1, f);
1249 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1250 image->comps[compno].data[i] = index;
1260 int imagetopnm(opj_image_t * image, const char *outfile) {
1261 int w, wr, h, hr, max;
1263 int adjustR, adjustG, adjustB, adjustX;
1266 const char *tmp = outfile;
1275 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1276 && image->comps[1].dx == image->comps[2].dx
1277 && image->comps[0].dy == image->comps[1].dy
1278 && image->comps[1].dy == image->comps[2].dy
1279 && image->comps[0].prec == image->comps[1].prec
1280 && image->comps[1].prec == image->comps[2].prec
1281 && S2 !='g' && S2 !='G') {
1283 fdest = fopen(outfile, "wb");
1285 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1289 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
1290 wr = image->comps[0].w;
1292 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
1293 hr = image->comps[0].h;
1295 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
1297 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
1298 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
1300 fprintf(fdest, "P6\n%d %d\n%d\n", wr, hr, max);
1302 if (image->comps[0].prec > 8) {
1303 adjustR = image->comps[0].prec - 8;
1304 printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1308 if (image->comps[1].prec > 8) {
1309 adjustG = image->comps[1].prec - 8;
1310 printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1314 if (image->comps[2].prec > 8) {
1315 adjustB = image->comps[2].prec - 8;
1316 printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1322 for (i = 0; i < wr * hr; i++) {
1324 unsigned char rc,gc,bc;
1325 r = image->comps[0].data[i];
1326 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1327 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
1329 g = image->comps[1].data[i];
1330 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1331 gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
1333 b = image->comps[2].data[i];
1334 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1335 bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
1337 fprintf(fdest, "%c%c%c", rc, gc, bc);
1342 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1343 if (image->numcomps > ncomp) {
1344 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1345 fprintf(stderr," is written to the file\n");
1347 for (compno = 0; compno < ncomp; compno++) {
1350 sprintf(name, "%d.%s", compno, outfile);
1352 sprintf(name, "%s", outfile);
1355 fdest = fopen(name, "wb");
1357 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1361 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1362 wr = image->comps[compno].w;
1364 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1365 hr = image->comps[compno].h;
1367 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1369 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1370 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1372 fprintf(fdest, "P5\n%d %d\n%d\n", wr, hr, max);
1374 if (image->comps[compno].prec > 8) {
1375 adjustX = image->comps[0].prec - 8;
1376 printf("PNM CONVERSION: Truncating component %d from %d bits to 8 bits\n",compno, image->comps[compno].prec);
1381 for (i = 0; i < wr * hr; i++) {
1384 l = image->comps[compno].data[i];
1385 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1386 lc = (unsigned char) ((l >> adjustX)+((l >> (adjustX-1))%2));
1387 fprintf(fdest, "%c", lc);
1396 /* -->> -->> -->> -->>
1400 <<-- <<-- <<-- <<-- */
1402 typedef struct tiff_infoheader{
1403 DWORD tiWidth; // Width of Image in pixel
1404 DWORD tiHeight; // Height of Image in pixel
1405 DWORD tiPhoto; // Photometric
1406 WORD tiBps; // Bits per sample
1407 WORD tiSf; // Sample Format
1408 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1409 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1412 int imagetotif(opj_image_t * image, const char *outfile) {
1413 int width, height, imgsize;
1414 int bps,index,adjust = 0;
1421 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1422 && image->comps[1].dx == image->comps[2].dx
1423 && image->comps[0].dy == image->comps[1].dy
1424 && image->comps[1].dy == image->comps[2].dy
1425 && image->comps[0].prec == image->comps[1].prec
1426 && image->comps[1].prec == image->comps[2].prec) {
1432 tif = TIFFOpen(outfile, "wb");
1434 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1438 width = image->comps[0].w;
1439 height = image->comps[0].h;
1440 imgsize = width * height ;
1441 bps = image->comps[0].prec;
1443 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1444 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1445 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1446 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1447 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1448 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1449 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1450 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1452 /* Get a buffer for the data */
1453 strip_size=TIFFStripSize(tif);
1454 buf = _TIFFmalloc(strip_size);
1456 adjust = image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0;
1457 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1458 unsigned char *dat8;
1460 ssize = TIFFStripSize(tif);
1462 if (image->comps[0].prec == 8){
1463 for (i=0; i<ssize-2; i+=3) { // 8 bits per pixel
1464 int r = 0,g = 0,b = 0;
1465 if(index < imgsize){
1466 r = image->comps[0].data[index];
1467 g = image->comps[1].data[index];
1468 b = image->comps[2].data[index];
1469 if (image->comps[0].sgnd){
1474 dat8[i+0] = r ; // R
1475 dat8[i+1] = g ; // G
1476 dat8[i+2] = b ; // B
1483 for (i=last_i; i<ssize; i+=3) { // 8 bits per pixel
1484 int r = 0,g = 0,b = 0;
1485 if(index < imgsize){
1486 r = image->comps[0].data[index];
1487 g = image->comps[1].data[index];
1488 b = image->comps[2].data[index];
1489 if (image->comps[0].sgnd){
1494 dat8[i+0] = r ; // R
1495 if(i+1 <ssize) dat8[i+1] = g ; else break;// G
1496 if(i+2 <ssize) dat8[i+2] = b ; else break;// B
1502 }else if (image->comps[0].prec == 12){
1503 for (i=0; i<ssize-8; i+=9) { // 12 bits per pixel
1504 int r = 0,g = 0,b = 0;
1505 int r1 = 0,g1 = 0,b1 = 0;
1506 if((index < imgsize)&(index+1 < imgsize)){
1507 r = image->comps[0].data[index];
1508 g = image->comps[1].data[index];
1509 b = image->comps[2].data[index];
1510 r1 = image->comps[0].data[index+1];
1511 g1 = image->comps[1].data[index+1];
1512 b1 = image->comps[2].data[index+1];
1513 if (image->comps[0].sgnd){
1521 dat8[i+0] = (r >> 4);
1522 dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f);
1524 dat8[i+3] = (b >> 4);
1525 dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1527 dat8[i+6] = (g1 >> 4);
1528 dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);
1536 for (i= last_i; i<ssize; i+=9) { // 12 bits per pixel
1537 int r = 0,g = 0,b = 0;
1538 int r1 = 0,g1 = 0,b1 = 0;
1539 if((index < imgsize)&(index+1 < imgsize)){
1540 r = image->comps[0].data[index];
1541 g = image->comps[1].data[index];
1542 b = image->comps[2].data[index];
1543 r1 = image->comps[0].data[index+1];
1544 g1 = image->comps[1].data[index+1];
1545 b1 = image->comps[2].data[index+1];
1546 if (image->comps[0].sgnd){
1554 dat8[i+0] = (r >> 4);
1555 if(i+1 <ssize) dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f); else break;
1556 if(i+2 <ssize) dat8[i+2] = g ; else break;
1557 if(i+3 <ssize) dat8[i+3] = (b >> 4); else break;
1558 if(i+4 <ssize) dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);else break;
1559 if(i+5 <ssize) dat8[i+5] = r1; else break;
1560 if(i+6 <ssize) dat8[i+6] = (g1 >> 4); else break;
1561 if(i+7 <ssize) dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);else break;
1562 if(i+8 <ssize) dat8[i+8] = b1; else break;
1568 }else if (image->comps[0].prec == 16){
1569 for (i=0 ; i<ssize-5 ; i+=6) { // 16 bits per pixel
1570 int r = 0,g = 0,b = 0;
1571 if(index < imgsize){
1572 r = image->comps[0].data[index];
1573 g = image->comps[1].data[index];
1574 b = image->comps[2].data[index];
1575 if (image->comps[0].sgnd){
1581 dat8[i+1] = (r >> 8);//MSB
1583 dat8[i+3] = (g >> 8);
1585 dat8[i+5] = (b >> 8);
1592 for (i=0 ; i<ssize ; i+=6) { // 16 bits per pixel
1593 int r = 0,g = 0,b = 0;
1594 if(index < imgsize){
1595 r = image->comps[0].data[index];
1596 g = image->comps[1].data[index];
1597 b = image->comps[2].data[index];
1598 if (image->comps[0].sgnd){
1604 if(i+1 <ssize) dat8[i+1] = (r >> 8);else break;//MSB
1605 if(i+2 <ssize) dat8[i+2] = g; else break;
1606 if(i+3 <ssize) dat8[i+3] = (g >> 8);else break;
1607 if(i+4 <ssize) dat8[i+4] = b; else break;
1608 if(i+5 <ssize) dat8[i+5] = (b >> 8);else break;
1615 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1616 fprintf(stderr,"Aborting\n");
1619 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1623 }else if (image->numcomps == 1){
1628 tif = TIFFOpen(outfile, "wb");
1630 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1634 width = image->comps[0].w;
1635 height = image->comps[0].h;
1636 imgsize = width * height;
1637 bps = image->comps[0].prec;
1640 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1641 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1642 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1643 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1644 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1645 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1646 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1647 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1649 /* Get a buffer for the data */
1650 strip_size = TIFFStripSize(tif);
1651 buf = _TIFFmalloc(strip_size);
1653 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1654 unsigned char *dat8;
1657 if (image->comps[0].prec == 8){
1658 for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
1659 if(index < imgsize){
1661 r = image->comps[0].data[index];
1662 if (image->comps[0].sgnd){
1670 }else if (image->comps[0].prec == 12){
1671 for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
1672 if(index < imgsize){
1674 r = image->comps[0].data[index];
1675 r1 = image->comps[0].data[index+1];
1676 if (image->comps[0].sgnd){
1680 dat8[i+0] = (r >> 4);
1681 dat8[i+1] = ((r & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1687 }else if (image->comps[0].prec == 16){
1688 for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
1689 if(index < imgsize){
1691 r = image->comps[0].data[index];
1692 if (image->comps[0].sgnd){
1702 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1703 fprintf(stderr,"Aborting\n");
1706 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1711 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1712 fprintf(stderr,"Aborting\n");
1718 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
1720 int subsampling_dx = parameters->subsampling_dx;
1721 int subsampling_dy = parameters->subsampling_dy;
1723 tiff_infoheader_t Info;
1727 int j, numcomps, w, h,index;
1728 OPJ_COLOR_SPACE color_space;
1729 opj_image_cmptparm_t cmptparm[3];
1730 opj_image_t * image = NULL;
1733 tif = TIFFOpen(filename, "r");
1736 fprintf(stderr, "Failed to open %s for reading\n", filename);
1740 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1741 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1742 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1743 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1744 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1746 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1747 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1751 if (Info.tiPhoto == 2) {
1757 color_space = CLRSPC_SRGB;
1758 /* initialize image components*/
1759 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1760 for(j = 0; j < numcomps; j++) {
1761 if (parameters->cp_cinema) {
1762 cmptparm[j].prec = 12;
1763 cmptparm[j].bpp = 12;
1765 cmptparm[j].prec = Info.tiBps;
1766 cmptparm[j].bpp = Info.tiBps;
1768 cmptparm[j].sgnd = 0;
1769 cmptparm[j].dx = subsampling_dx;
1770 cmptparm[j].dy = subsampling_dy;
1774 /* create the image*/
1775 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1781 /* set image offset and reference grid */
1782 image->x0 = parameters->image_offset_x0;
1783 image->y0 = parameters->image_offset_y0;
1784 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1785 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1787 buf = _TIFFmalloc(TIFFStripSize(tif));
1789 strip_size=TIFFStripSize(tif);
1791 imgsize = image->comps[0].w * image->comps[0].h ;
1792 /* Read the Image components*/
1793 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1794 unsigned char *dat8;
1796 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1799 if (Info.tiBps==12){
1800 for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
1801 if((index < imgsize)&(index+1 < imgsize)){
1802 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
1803 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1804 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
1805 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1806 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
1807 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1813 else if( Info.tiBps==16){
1814 for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
1815 if(index < imgsize){
1816 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
1817 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
1818 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
1819 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1820 image->comps[0].data[index] = (image->comps[0].data[index] + 0x08) >> 4 ;
1821 image->comps[1].data[index] = (image->comps[1].data[index] + 0x08) >> 4 ;
1822 image->comps[2].data[index] = (image->comps[2].data[index] + 0x08) >> 4 ;
1829 else if ( Info.tiBps==8){
1830 for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
1831 if(index < imgsize){
1832 image->comps[0].data[index] = dat8[i+0];// R
1833 image->comps[1].data[index] = dat8[i+1];// G
1834 image->comps[2].data[index] = dat8[i+2];// B
1835 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1836 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
1837 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
1838 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
1846 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1847 fprintf(stderr,"Aborting\n");
1854 }else if(Info.tiPhoto == 1) {
1860 color_space = CLRSPC_GRAY;
1861 /* initialize image components*/
1862 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1863 cmptparm[0].prec = Info.tiBps;
1864 cmptparm[0].bpp = Info.tiBps;
1865 cmptparm[0].sgnd = 0;
1866 cmptparm[0].dx = subsampling_dx;
1867 cmptparm[0].dy = subsampling_dy;
1871 /* create the image*/
1872 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1877 /* set image offset and reference grid */
1878 image->x0 = parameters->image_offset_x0;
1879 image->y0 = parameters->image_offset_y0;
1880 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1881 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1883 buf = _TIFFmalloc(TIFFStripSize(tif));
1885 strip_size = TIFFStripSize(tif);
1887 imgsize = image->comps[0].w * image->comps[0].h ;
1888 /* Read the Image components*/
1889 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1890 unsigned char *dat8;
1892 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1895 if (Info.tiBps==12){
1896 for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
1897 if(index < imgsize){
1898 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
1899 image->comps[0].data[index+1] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1905 else if( Info.tiBps==16){
1906 for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
1907 if(index < imgsize){
1908 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
1914 else if ( Info.tiBps==8){
1915 for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
1916 if(index < imgsize){
1917 image->comps[0].data[index] = dat8[i+0];
1924 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1925 fprintf(stderr,"Aborting\n");
1933 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1934 fprintf(stderr,"Aborting\n");
1940 /* -->> -->> -->> -->>
1944 <<-- <<-- <<-- <<-- */
1946 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
1947 int subsampling_dx = parameters->subsampling_dx;
1948 int subsampling_dy = parameters->subsampling_dy;
1951 int i, compno, numcomps, w, h;
1952 OPJ_COLOR_SPACE color_space;
1953 opj_image_cmptparm_t *cmptparm;
1954 opj_image_t * image = NULL;
1957 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
1959 fprintf(stderr,"\nError: invalid raw image parameters\n");
1960 fprintf(stderr,"Please use the Format option -F:\n");
1961 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
1962 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
1963 fprintf(stderr,"Aborting\n");
1967 f = fopen(filename, "rb");
1969 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1970 fprintf(stderr,"Aborting\n");
1973 numcomps = raw_cp->rawComp;
1974 color_space = CLRSPC_SRGB;
1975 w = raw_cp->rawWidth;
1976 h = raw_cp->rawHeight;
1977 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
1979 /* initialize image components */
1980 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1981 for(i = 0; i < numcomps; i++) {
1982 cmptparm[i].prec = raw_cp->rawBitDepth;
1983 cmptparm[i].bpp = raw_cp->rawBitDepth;
1984 cmptparm[i].sgnd = raw_cp->rawSigned;
1985 cmptparm[i].dx = subsampling_dx;
1986 cmptparm[i].dy = subsampling_dy;
1990 /* create the image */
1991 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1996 /* set image offset and reference grid */
1997 image->x0 = parameters->image_offset_x0;
1998 image->y0 = parameters->image_offset_y0;
1999 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2000 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2002 if(raw_cp->rawBitDepth <= 8)
2004 unsigned char value = 0;
2005 for(compno = 0; compno < numcomps; compno++) {
2006 for (i = 0; i < w * h; i++) {
2007 if (!fread(&value, 1, 1, f)) {
2008 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2011 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2017 unsigned short value = 0;
2018 for(compno = 0; compno < numcomps; compno++) {
2019 for (i = 0; i < w * h; i++) {
2020 if (!fread(&value, 2, 1, f)) {
2021 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2024 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2029 if (fread(&ch, 1, 1, f)) {
2030 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2037 int imagetoraw(opj_image_t * image, const char *outfile)
2039 FILE *rawFile = NULL;
2045 if((image->numcomps * image->x1 * image->y1) == 0)
2047 fprintf(stderr,"\nError: invalid raw image parameters\n");
2051 rawFile = fopen(outfile, "wb");
2053 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2057 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2059 for(compno = 0; compno < image->numcomps; compno++)
2061 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2062 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2064 w = image->comps[compno].w;
2065 h = image->comps[compno].h;
2067 if(image->comps[compno].prec <= 8)
2069 if(image->comps[compno].sgnd == 1)
2072 int mask = (1 << image->comps[compno].prec) - 1;
2073 ptr = image->comps[compno].data;
2074 for (line = 0; line < h; line++) {
2075 for(row = 0; row < w; row++) {
2076 curr = (signed char) (*ptr & mask);
2077 fwrite(&curr, sizeof(signed char), 1, rawFile);
2082 else if(image->comps[compno].sgnd == 0)
2085 int mask = (1 << image->comps[compno].prec) - 1;
2086 ptr = image->comps[compno].data;
2087 for (line = 0; line < h; line++) {
2088 for(row = 0; row < w; row++) {
2089 curr = (unsigned char) (*ptr & mask);
2090 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2096 else if(image->comps[compno].prec <= 16)
2098 if(image->comps[compno].sgnd == 1)
2100 signed short int curr;
2101 int mask = (1 << image->comps[compno].prec) - 1;
2102 ptr = image->comps[compno].data;
2103 for (line = 0; line < h; line++) {
2104 for(row = 0; row < w; row++) {
2105 curr = (signed short int) (*ptr & mask);
2106 fwrite(&curr, sizeof(signed short int), 1, rawFile);
2111 else if(image->comps[compno].sgnd == 0)
2113 unsigned short int curr;
2114 int mask = (1 << image->comps[compno].prec) - 1;
2115 ptr = image->comps[compno].data;
2116 for (line = 0; line < h; line++) {
2117 for(row = 0; row < w; row++) {
2118 curr = (unsigned short int) (*ptr & mask);
2119 fwrite(&curr, sizeof(unsigned short int), 1, rawFile);
2125 else if (image->comps[compno].prec <= 32)
2127 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2132 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);