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);
920 for (i = 0; i < 256; i++) {
921 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
924 for (i = 0; i < w * h; i++) {
928 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
929 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
930 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
932 fprintf(fdest, "%c", rc);
934 if ((i + 1) % w == 0) {
935 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
936 fprintf(fdest, "%c", 0);
945 /* -->> -->> -->> -->>
949 <<-- <<-- <<-- <<-- */
952 unsigned char readuchar(FILE * f)
959 unsigned short readushort(FILE * f, int bigendian)
961 unsigned char c1, c2;
965 return (c1 << 8) + c2;
967 return (c2 << 8) + c1;
970 unsigned int readuint(FILE * f, int bigendian)
972 unsigned char c1, c2, c3, c4;
978 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
980 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
983 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
986 int i, numcomps, max;
987 OPJ_COLOR_SPACE color_space;
988 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
989 opj_image_t * image = NULL;
991 char endian1,endian2,sign;
996 opj_image_comp_t *comp = NULL;
999 color_space = CLRSPC_GRAY;
1001 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1005 f = fopen(filename, "rb");
1007 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1011 fseek(f, 0, SEEK_SET);
1012 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
1016 while (signtmp[i]!='\0') {
1017 if (signtmp[i]=='-') sign='-';
1022 if (endian1=='M' && endian2=='L') {
1024 } else if (endian2=='M' && endian1=='L') {
1027 fprintf(stderr, "Bad pgx header, please check input file\n");
1031 /* initialize image component */
1033 cmptparm.x0 = parameters->image_offset_x0;
1034 cmptparm.y0 = parameters->image_offset_y0;
1035 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1036 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1043 cmptparm.prec = prec;
1044 cmptparm.bpp = prec;
1045 cmptparm.dx = parameters->subsampling_dx;
1046 cmptparm.dy = parameters->subsampling_dy;
1048 /* create the image */
1049 image = opj_image_create(numcomps, &cmptparm, color_space);
1054 /* set image offset and reference grid */
1055 image->x0 = cmptparm.x0;
1056 image->y0 = cmptparm.x0;
1057 image->x1 = cmptparm.w;
1058 image->y1 = cmptparm.h;
1060 /* set image data */
1062 comp = &image->comps[0];
1064 for (i = 0; i < w * h; i++) {
1066 if (comp->prec <= 8) {
1070 v = (char) readuchar(f);
1072 } else if (comp->prec <= 16) {
1074 v = readushort(f, bigendian);
1076 v = (short) readushort(f, bigendian);
1080 v = readuint(f, bigendian);
1082 v = (int) readuint(f, bigendian);
1090 comp->bpp = int_floorlog2(max) + 1;
1095 int imagetopgx(opj_image_t * image, const char *outfile) {
1100 for (compno = 0; compno < image->numcomps; compno++) {
1101 opj_image_comp_t *comp = &image->comps[compno];
1102 char bname[256]; /* buffer for name */
1103 char *name = bname; /* pointer */
1105 const size_t olen = strlen(outfile);
1106 const size_t dotpos = olen - 4;
1107 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1108 if( outfile[dotpos] != '.' ) {
1109 /* `pgx` was recognized but there is no dot at expected position */
1110 fprintf(stderr, "ERROR -> Impossible happen." );
1114 name = (char*)malloc(total+1);
1116 strncpy(name, outfile, dotpos);
1117 if (image->numcomps > 1) {
1118 sprintf(name+dotpos, "-%d.pgx", compno);
1120 strcpy(name+dotpos, ".pgx");
1122 fdest = fopen(name, "wb");
1124 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1127 /* dont need name anymore */
1132 w = image->comps[compno].w;
1133 h = image->comps[compno].h;
1135 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1136 if (comp->prec <= 8) {
1138 } else if (comp->prec <= 16) {
1143 for (i = 0; i < w * h; i++) {
1144 int v = image->comps[compno].data[i];
1145 for (j = nbytes - 1; j >= 0; j--) {
1146 char byte = (char) (v >> (j * 8));
1147 fwrite(&byte, 1, 1, fdest);
1156 /* -->> -->> -->> -->>
1160 <<-- <<-- <<-- <<-- */
1162 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1163 int subsampling_dx = parameters->subsampling_dx;
1164 int subsampling_dy = parameters->subsampling_dy;
1167 int i, compno, numcomps, w, h;
1168 OPJ_COLOR_SPACE color_space;
1169 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
1170 opj_image_t * image = NULL;
1173 f = fopen(filename, "rb");
1175 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1179 if (fgetc(f) != 'P')
1184 case '2': /* greyscale image type */
1187 color_space = CLRSPC_GRAY;
1190 case '3': /* RGB image type */
1193 color_space = CLRSPC_SRGB;
1204 while(fgetc(f) == '#') while(fgetc(f) != '\n');
1206 fseek(f, -1, SEEK_CUR);
1207 fscanf(f, "%d %d\n255", &w, &h);
1208 fgetc(f); /* <cr><lf> */
1210 /* initialize image components */
1211 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1212 for(i = 0; i < numcomps; i++) {
1213 cmptparm[i].prec = 8;
1214 cmptparm[i].bpp = 8;
1215 cmptparm[i].sgnd = 0;
1216 cmptparm[i].dx = subsampling_dx;
1217 cmptparm[i].dy = subsampling_dy;
1221 /* create the image */
1222 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1228 /* set image offset and reference grid */
1229 image->x0 = parameters->image_offset_x0;
1230 image->y0 = parameters->image_offset_y0;
1231 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1232 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1234 /* set image data */
1236 if ((value == '2') || (value == '3')) { /* ASCII */
1237 for (i = 0; i < w * h; i++) {
1238 for(compno = 0; compno < numcomps; compno++) {
1239 unsigned int index = 0;
1240 fscanf(f, "%u", &index);
1241 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1242 image->comps[compno].data[i] = index;
1245 } else if ((value == '5') || (value == '6')) { /* BINARY */
1246 for (i = 0; i < w * h; i++) {
1247 for(compno = 0; compno < numcomps; compno++) {
1248 unsigned char index = 0;
1249 fread(&index, 1, 1, f);
1250 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1251 image->comps[compno].data[i] = index;
1261 int imagetopnm(opj_image_t * image, const char *outfile) {
1262 int w, wr, h, hr, max;
1264 int adjustR, adjustG, adjustB, adjustX;
1267 const char *tmp = outfile;
1276 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1277 && image->comps[1].dx == image->comps[2].dx
1278 && image->comps[0].dy == image->comps[1].dy
1279 && image->comps[1].dy == image->comps[2].dy
1280 && image->comps[0].prec == image->comps[1].prec
1281 && image->comps[1].prec == image->comps[2].prec
1282 && S2 !='g' && S2 !='G') {
1284 fdest = fopen(outfile, "wb");
1286 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1290 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
1291 wr = image->comps[0].w;
1293 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
1294 hr = image->comps[0].h;
1296 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
1298 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
1299 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
1301 fprintf(fdest, "P6\n%d %d\n%d\n", wr, hr, max);
1303 if (image->comps[0].prec > 8) {
1304 adjustR = image->comps[0].prec - 8;
1305 printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1309 if (image->comps[1].prec > 8) {
1310 adjustG = image->comps[1].prec - 8;
1311 printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1315 if (image->comps[2].prec > 8) {
1316 adjustB = image->comps[2].prec - 8;
1317 printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1323 for (i = 0; i < wr * hr; i++) {
1325 unsigned char rc,gc,bc;
1326 r = image->comps[0].data[i];
1327 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1328 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
1330 g = image->comps[1].data[i];
1331 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1332 gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
1334 b = image->comps[2].data[i];
1335 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1336 bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
1338 fprintf(fdest, "%c%c%c", rc, gc, bc);
1343 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1344 if (image->numcomps > ncomp) {
1345 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1346 fprintf(stderr," is written to the file\n");
1348 for (compno = 0; compno < ncomp; compno++) {
1351 sprintf(name, "%d.%s", compno, outfile);
1353 sprintf(name, "%s", outfile);
1356 fdest = fopen(name, "wb");
1358 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1362 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1363 wr = image->comps[compno].w;
1365 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1366 hr = image->comps[compno].h;
1368 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1370 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1371 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1373 fprintf(fdest, "P5\n%d %d\n%d\n", wr, hr, max);
1375 if (image->comps[compno].prec > 8) {
1376 adjustX = image->comps[0].prec - 8;
1377 printf("PNM CONVERSION: Truncating component %d from %d bits to 8 bits\n",compno, image->comps[compno].prec);
1382 for (i = 0; i < wr * hr; i++) {
1385 l = image->comps[compno].data[i];
1386 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1387 lc = (unsigned char) ((l >> adjustX)+((l >> (adjustX-1))%2));
1388 fprintf(fdest, "%c", lc);
1397 /* -->> -->> -->> -->>
1401 <<-- <<-- <<-- <<-- */
1403 typedef struct tiff_infoheader{
1404 DWORD tiWidth; // Width of Image in pixel
1405 DWORD tiHeight; // Height of Image in pixel
1406 DWORD tiPhoto; // Photometric
1407 WORD tiBps; // Bits per sample
1408 WORD tiSf; // Sample Format
1409 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1410 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1413 int imagetotif(opj_image_t * image, const char *outfile) {
1414 int width, height, imgsize;
1415 int bps,index,adjust = 0;
1422 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1423 && image->comps[1].dx == image->comps[2].dx
1424 && image->comps[0].dy == image->comps[1].dy
1425 && image->comps[1].dy == image->comps[2].dy
1426 && image->comps[0].prec == image->comps[1].prec
1427 && image->comps[1].prec == image->comps[2].prec) {
1433 tif = TIFFOpen(outfile, "wb");
1435 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1439 width = image->comps[0].w;
1440 height = image->comps[0].h;
1441 imgsize = width * height ;
1442 bps = image->comps[0].prec;
1444 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1445 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1446 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1447 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1448 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1449 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1450 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1451 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1453 /* Get a buffer for the data */
1454 strip_size=TIFFStripSize(tif);
1455 buf = _TIFFmalloc(strip_size);
1457 adjust = image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0;
1458 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1459 unsigned char *dat8;
1461 ssize = TIFFStripSize(tif);
1463 if (image->comps[0].prec == 8){
1464 for (i=0; i<ssize-2; i+=3) { // 8 bits per pixel
1465 int r = 0,g = 0,b = 0;
1466 if(index < imgsize){
1467 r = image->comps[0].data[index];
1468 g = image->comps[1].data[index];
1469 b = image->comps[2].data[index];
1470 if (image->comps[0].sgnd){
1475 dat8[i+0] = r ; // R
1476 dat8[i+1] = g ; // G
1477 dat8[i+2] = b ; // B
1484 for (i=last_i; i<ssize; i+=3) { // 8 bits per pixel
1485 int r = 0,g = 0,b = 0;
1486 if(index < imgsize){
1487 r = image->comps[0].data[index];
1488 g = image->comps[1].data[index];
1489 b = image->comps[2].data[index];
1490 if (image->comps[0].sgnd){
1495 dat8[i+0] = r ; // R
1496 if(i+1 <ssize) dat8[i+1] = g ; else break;// G
1497 if(i+2 <ssize) dat8[i+2] = b ; else break;// B
1503 }else if (image->comps[0].prec == 12){
1504 for (i=0; i<ssize-8; i+=9) { // 12 bits per pixel
1505 int r = 0,g = 0,b = 0;
1506 int r1 = 0,g1 = 0,b1 = 0;
1507 if((index < imgsize)&(index+1 < imgsize)){
1508 r = image->comps[0].data[index];
1509 g = image->comps[1].data[index];
1510 b = image->comps[2].data[index];
1511 r1 = image->comps[0].data[index+1];
1512 g1 = image->comps[1].data[index+1];
1513 b1 = image->comps[2].data[index+1];
1514 if (image->comps[0].sgnd){
1522 dat8[i+0] = (r >> 4);
1523 dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f);
1525 dat8[i+3] = (b >> 4);
1526 dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1528 dat8[i+6] = (g1 >> 4);
1529 dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);
1537 for (i= last_i; i<ssize; i+=9) { // 12 bits per pixel
1538 int r = 0,g = 0,b = 0;
1539 int r1 = 0,g1 = 0,b1 = 0;
1540 if((index < imgsize)&(index+1 < imgsize)){
1541 r = image->comps[0].data[index];
1542 g = image->comps[1].data[index];
1543 b = image->comps[2].data[index];
1544 r1 = image->comps[0].data[index+1];
1545 g1 = image->comps[1].data[index+1];
1546 b1 = image->comps[2].data[index+1];
1547 if (image->comps[0].sgnd){
1555 dat8[i+0] = (r >> 4);
1556 if(i+1 <ssize) dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f); else break;
1557 if(i+2 <ssize) dat8[i+2] = g ; else break;
1558 if(i+3 <ssize) dat8[i+3] = (b >> 4); else break;
1559 if(i+4 <ssize) dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);else break;
1560 if(i+5 <ssize) dat8[i+5] = r1; else break;
1561 if(i+6 <ssize) dat8[i+6] = (g1 >> 4); else break;
1562 if(i+7 <ssize) dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);else break;
1563 if(i+8 <ssize) dat8[i+8] = b1; else break;
1569 }else if (image->comps[0].prec == 16){
1570 for (i=0 ; i<ssize-5 ; i+=6) { // 16 bits per pixel
1571 int r = 0,g = 0,b = 0;
1572 if(index < imgsize){
1573 r = image->comps[0].data[index];
1574 g = image->comps[1].data[index];
1575 b = image->comps[2].data[index];
1576 if (image->comps[0].sgnd){
1582 dat8[i+1] = (r >> 8);//MSB
1584 dat8[i+3] = (g >> 8);
1586 dat8[i+5] = (b >> 8);
1593 for (i=0 ; i<ssize ; i+=6) { // 16 bits per pixel
1594 int r = 0,g = 0,b = 0;
1595 if(index < imgsize){
1596 r = image->comps[0].data[index];
1597 g = image->comps[1].data[index];
1598 b = image->comps[2].data[index];
1599 if (image->comps[0].sgnd){
1605 if(i+1 <ssize) dat8[i+1] = (r >> 8);else break;//MSB
1606 if(i+2 <ssize) dat8[i+2] = g; else break;
1607 if(i+3 <ssize) dat8[i+3] = (g >> 8);else break;
1608 if(i+4 <ssize) dat8[i+4] = b; else break;
1609 if(i+5 <ssize) dat8[i+5] = (b >> 8);else break;
1616 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1617 fprintf(stderr,"Aborting\n");
1620 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1624 }else if (image->numcomps == 1){
1629 tif = TIFFOpen(outfile, "wb");
1631 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1635 width = image->comps[0].w;
1636 height = image->comps[0].h;
1637 imgsize = width * height;
1638 bps = image->comps[0].prec;
1641 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1642 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1643 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1644 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1645 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1646 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1647 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1648 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1650 /* Get a buffer for the data */
1651 strip_size = TIFFStripSize(tif);
1652 buf = _TIFFmalloc(strip_size);
1654 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1655 unsigned char *dat8;
1658 if (image->comps[0].prec == 8){
1659 for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
1660 if(index < imgsize){
1662 r = image->comps[0].data[index];
1663 if (image->comps[0].sgnd){
1671 }else if (image->comps[0].prec == 12){
1672 for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
1673 if(index < imgsize){
1675 r = image->comps[0].data[index];
1676 r1 = image->comps[0].data[index+1];
1677 if (image->comps[0].sgnd){
1681 dat8[i+0] = (r >> 4);
1682 dat8[i+1] = ((r & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1688 }else if (image->comps[0].prec == 16){
1689 for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
1690 if(index < imgsize){
1692 r = image->comps[0].data[index];
1693 if (image->comps[0].sgnd){
1703 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1704 fprintf(stderr,"Aborting\n");
1707 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1712 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1713 fprintf(stderr,"Aborting\n");
1719 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
1721 int subsampling_dx = parameters->subsampling_dx;
1722 int subsampling_dy = parameters->subsampling_dy;
1724 tiff_infoheader_t Info;
1728 int j, numcomps, w, h,index;
1729 OPJ_COLOR_SPACE color_space;
1730 opj_image_cmptparm_t cmptparm[3];
1731 opj_image_t * image = NULL;
1734 tif = TIFFOpen(filename, "r");
1737 fprintf(stderr, "Failed to open %s for reading\n", filename);
1741 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1742 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1743 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1744 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1745 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1747 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1748 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1752 if (Info.tiPhoto == 2) {
1758 color_space = CLRSPC_SRGB;
1759 /* initialize image components*/
1760 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1761 for(j = 0; j < numcomps; j++) {
1762 if (parameters->cp_cinema) {
1763 cmptparm[j].prec = 12;
1764 cmptparm[j].bpp = 12;
1766 cmptparm[j].prec = Info.tiBps;
1767 cmptparm[j].bpp = Info.tiBps;
1769 cmptparm[j].sgnd = 0;
1770 cmptparm[j].dx = subsampling_dx;
1771 cmptparm[j].dy = subsampling_dy;
1775 /* create the image*/
1776 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1782 /* set image offset and reference grid */
1783 image->x0 = parameters->image_offset_x0;
1784 image->y0 = parameters->image_offset_y0;
1785 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1786 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1788 buf = _TIFFmalloc(TIFFStripSize(tif));
1790 strip_size=TIFFStripSize(tif);
1792 imgsize = image->comps[0].w * image->comps[0].h ;
1793 /* Read the Image components*/
1794 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1795 unsigned char *dat8;
1797 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1800 if (Info.tiBps==12){
1801 for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
1802 if((index < imgsize)&(index+1 < imgsize)){
1803 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
1804 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1805 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
1806 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1807 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
1808 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1814 else if( Info.tiBps==16){
1815 for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
1816 if(index < imgsize){
1817 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
1818 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
1819 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
1820 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1821 image->comps[0].data[index] = (image->comps[0].data[index] + 0x08) >> 4 ;
1822 image->comps[1].data[index] = (image->comps[1].data[index] + 0x08) >> 4 ;
1823 image->comps[2].data[index] = (image->comps[2].data[index] + 0x08) >> 4 ;
1830 else if ( Info.tiBps==8){
1831 for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
1832 if(index < imgsize){
1833 image->comps[0].data[index] = dat8[i+0];// R
1834 image->comps[1].data[index] = dat8[i+1];// G
1835 image->comps[2].data[index] = dat8[i+2];// B
1836 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1837 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
1838 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
1839 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
1847 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1848 fprintf(stderr,"Aborting\n");
1855 }else if(Info.tiPhoto == 1) {
1861 color_space = CLRSPC_GRAY;
1862 /* initialize image components*/
1863 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1864 cmptparm[0].prec = Info.tiBps;
1865 cmptparm[0].bpp = Info.tiBps;
1866 cmptparm[0].sgnd = 0;
1867 cmptparm[0].dx = subsampling_dx;
1868 cmptparm[0].dy = subsampling_dy;
1872 /* create the image*/
1873 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1878 /* set image offset and reference grid */
1879 image->x0 = parameters->image_offset_x0;
1880 image->y0 = parameters->image_offset_y0;
1881 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1882 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1884 buf = _TIFFmalloc(TIFFStripSize(tif));
1886 strip_size = TIFFStripSize(tif);
1888 imgsize = image->comps[0].w * image->comps[0].h ;
1889 /* Read the Image components*/
1890 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1891 unsigned char *dat8;
1893 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1896 if (Info.tiBps==12){
1897 for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
1898 if(index < imgsize){
1899 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
1900 image->comps[0].data[index+1] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1906 else if( Info.tiBps==16){
1907 for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
1908 if(index < imgsize){
1909 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
1915 else if ( Info.tiBps==8){
1916 for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
1917 if(index < imgsize){
1918 image->comps[0].data[index] = dat8[i+0];
1925 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1926 fprintf(stderr,"Aborting\n");
1934 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1935 fprintf(stderr,"Aborting\n");
1941 /* -->> -->> -->> -->>
1945 <<-- <<-- <<-- <<-- */
1947 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
1948 int subsampling_dx = parameters->subsampling_dx;
1949 int subsampling_dy = parameters->subsampling_dy;
1952 int i, compno, numcomps, w, h;
1953 OPJ_COLOR_SPACE color_space;
1954 opj_image_cmptparm_t *cmptparm;
1955 opj_image_t * image = NULL;
1958 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
1960 fprintf(stderr,"\nError: invalid raw image parameters\n");
1961 fprintf(stderr,"Please use the Format option -F:\n");
1962 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
1963 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
1964 fprintf(stderr,"Aborting\n");
1968 f = fopen(filename, "rb");
1970 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1971 fprintf(stderr,"Aborting\n");
1974 numcomps = raw_cp->rawComp;
1975 color_space = CLRSPC_SRGB;
1976 w = raw_cp->rawWidth;
1977 h = raw_cp->rawHeight;
1978 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
1980 /* initialize image components */
1981 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1982 for(i = 0; i < numcomps; i++) {
1983 cmptparm[i].prec = raw_cp->rawBitDepth;
1984 cmptparm[i].bpp = raw_cp->rawBitDepth;
1985 cmptparm[i].sgnd = raw_cp->rawSigned;
1986 cmptparm[i].dx = subsampling_dx;
1987 cmptparm[i].dy = subsampling_dy;
1991 /* create the image */
1992 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1997 /* set image offset and reference grid */
1998 image->x0 = parameters->image_offset_x0;
1999 image->y0 = parameters->image_offset_y0;
2000 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2001 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2003 if(raw_cp->rawBitDepth <= 8)
2005 unsigned char value = 0;
2006 for(compno = 0; compno < numcomps; compno++) {
2007 for (i = 0; i < w * h; i++) {
2008 if (!fread(&value, 1, 1, f)) {
2009 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2012 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2016 else if(raw_cp->rawBitDepth <= 16)
2018 unsigned short value;
2019 for(compno = 0; compno < numcomps; compno++) {
2020 for (i = 0; i < w * h; i++) {
2022 if (!fread(&temp, 1, 1, f)) {
2023 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2027 if (!fread(&temp, 1, 1, f)) {
2028 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2032 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2037 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2041 if (fread(&ch, 1, 1, f)) {
2042 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2049 int imagetoraw(opj_image_t * image, const char *outfile)
2051 FILE *rawFile = NULL;
2057 if((image->numcomps * image->x1 * image->y1) == 0)
2059 fprintf(stderr,"\nError: invalid raw image parameters\n");
2063 rawFile = fopen(outfile, "wb");
2065 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2069 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2071 for(compno = 0; compno < image->numcomps; compno++)
2073 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2074 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2076 w = image->comps[compno].w;
2077 h = image->comps[compno].h;
2079 if(image->comps[compno].prec <= 8)
2081 if(image->comps[compno].sgnd == 1)
2084 int mask = (1 << image->comps[compno].prec) - 1;
2085 ptr = image->comps[compno].data;
2086 for (line = 0; line < h; line++) {
2087 for(row = 0; row < w; row++) {
2088 curr = (signed char) (*ptr & mask);
2089 fwrite(&curr, sizeof(signed char), 1, rawFile);
2094 else if(image->comps[compno].sgnd == 0)
2097 int mask = (1 << image->comps[compno].prec) - 1;
2098 ptr = image->comps[compno].data;
2099 for (line = 0; line < h; line++) {
2100 for(row = 0; row < w; row++) {
2101 curr = (unsigned char) (*ptr & mask);
2102 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2108 else if(image->comps[compno].prec <= 16)
2110 if(image->comps[compno].sgnd == 1)
2112 signed short int curr;
2113 int mask = (1 << image->comps[compno].prec) - 1;
2114 ptr = image->comps[compno].data;
2115 for (line = 0; line < h; line++) {
2116 for(row = 0; row < w; row++) {
2118 curr = (signed short int) (*ptr & mask);
2120 fwrite(&temp, 1, 1, rawFile);
2122 fwrite(&temp, 1, 1, rawFile);
2127 else if(image->comps[compno].sgnd == 0)
2129 unsigned short int curr;
2130 int mask = (1 << image->comps[compno].prec) - 1;
2131 ptr = image->comps[compno].data;
2132 for (line = 0; line < h; line++) {
2133 for(row = 0; row < w; row++) {
2135 curr = (unsigned short int) (*ptr & mask);
2137 fwrite(&temp, 1, 1, rawFile);
2139 fwrite(&temp, 1, 1, rawFile);
2145 else if (image->comps[compno].prec <= 32)
2147 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2152 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);