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 = int_ceildiv(image->x1-image->x0, image->comps[0].dx);
314 height = int_ceildiv(image->y1-image->y0, image->comps[0].dy);
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 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
792 h = image->comps[0].h;
793 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
795 fprintf(fdest, "BM");
799 fprintf(fdest, "%c%c%c%c",
800 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
801 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
802 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
803 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
804 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
805 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
809 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
810 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
811 (unsigned char) ((wr) >> 8) & 0xff,
812 (unsigned char) ((wr) >> 16) & 0xff,
813 (unsigned char) ((wr) >> 24) & 0xff);
814 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
815 (unsigned char) ((hr) >> 8) & 0xff,
816 (unsigned char) ((hr) >> 16) & 0xff,
817 (unsigned char) ((hr) >> 24) & 0xff);
818 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
819 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
820 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
821 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
822 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
823 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
824 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
825 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
826 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
827 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
828 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
830 if (image->comps[0].prec > 8) {
831 adjustR = image->comps[0].prec - 8;
832 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
836 if (image->comps[1].prec > 8) {
837 adjustG = image->comps[1].prec - 8;
838 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
842 if (image->comps[2].prec > 8) {
843 adjustB = image->comps[2].prec - 8;
844 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
849 for (i = 0; i < wr * hr; i++) {
850 unsigned char rc, gc, bc;
853 r = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
854 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
855 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
856 g = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
857 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
858 gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
859 b = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
860 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
861 bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
863 fprintf(fdest, "%c%c%c", bc, gc, rc);
865 if ((i + 1) % wr == 0) {
866 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
867 fprintf(fdest, "%c", 0);
871 } else { /* Gray-scale */
873 /* -->> -->> -->> -->>
874 8 bits non code (Gray scale)
875 <<-- <<-- <<-- <<-- */
877 fdest = fopen(outfile, "wb");
878 w = image->comps[0].w;
879 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
881 h = image->comps[0].h;
882 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
884 fprintf(fdest, "BM");
888 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
889 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
890 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
891 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
892 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
893 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
894 ((54 + 1024) >> 16) & 0xff,
895 ((54 + 1024) >> 24) & 0xff);
899 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
900 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
901 (unsigned char) ((wr) >> 8) & 0xff,
902 (unsigned char) ((wr) >> 16) & 0xff,
903 (unsigned char) ((wr) >> 24) & 0xff);
904 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
905 (unsigned char) ((hr) >> 8) & 0xff,
906 (unsigned char) ((hr) >> 16) & 0xff,
907 (unsigned char) ((hr) >> 24) & 0xff);
908 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
909 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
910 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
911 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
912 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
913 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
914 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
915 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
916 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
917 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
918 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
920 if (image->comps[0].prec > 8) {
921 adjustR = image->comps[0].prec - 8;
922 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
925 for (i = 0; i < 256; i++) {
926 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
929 for (i = 0; i < wr * hr; i++) {
933 r = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
934 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
935 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
937 fprintf(fdest, "%c", rc);
939 if ((i + 1) % wr == 0) {
940 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
941 fprintf(fdest, "%c", 0);
950 /* -->> -->> -->> -->>
954 <<-- <<-- <<-- <<-- */
957 unsigned char readuchar(FILE * f)
964 unsigned short readushort(FILE * f, int bigendian)
966 unsigned char c1, c2;
970 return (c1 << 8) + c2;
972 return (c2 << 8) + c1;
975 unsigned int readuint(FILE * f, int bigendian)
977 unsigned char c1, c2, c3, c4;
983 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
985 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
988 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
991 int i, numcomps, max;
992 OPJ_COLOR_SPACE color_space;
993 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
994 opj_image_t * image = NULL;
996 char endian1,endian2,sign;
1001 opj_image_comp_t *comp = NULL;
1004 color_space = CLRSPC_GRAY;
1006 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1010 f = fopen(filename, "rb");
1012 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1016 fseek(f, 0, SEEK_SET);
1017 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
1021 while (signtmp[i]!='\0') {
1022 if (signtmp[i]=='-') sign='-';
1027 if (endian1=='M' && endian2=='L') {
1029 } else if (endian2=='M' && endian1=='L') {
1032 fprintf(stderr, "Bad pgx header, please check input file\n");
1036 /* initialize image component */
1038 cmptparm.x0 = parameters->image_offset_x0;
1039 cmptparm.y0 = parameters->image_offset_y0;
1040 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1041 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1048 cmptparm.prec = prec;
1049 cmptparm.bpp = prec;
1050 cmptparm.dx = parameters->subsampling_dx;
1051 cmptparm.dy = parameters->subsampling_dy;
1053 /* create the image */
1054 image = opj_image_create(numcomps, &cmptparm, color_space);
1059 /* set image offset and reference grid */
1060 image->x0 = cmptparm.x0;
1061 image->y0 = cmptparm.x0;
1062 image->x1 = cmptparm.w;
1063 image->y1 = cmptparm.h;
1065 /* set image data */
1067 comp = &image->comps[0];
1069 for (i = 0; i < w * h; i++) {
1071 if (comp->prec <= 8) {
1075 v = (char) readuchar(f);
1077 } else if (comp->prec <= 16) {
1079 v = readushort(f, bigendian);
1081 v = (short) readushort(f, bigendian);
1085 v = readuint(f, bigendian);
1087 v = (int) readuint(f, bigendian);
1095 comp->bpp = int_floorlog2(max) + 1;
1100 int imagetopgx(opj_image_t * image, const char *outfile) {
1105 for (compno = 0; compno < image->numcomps; compno++) {
1106 opj_image_comp_t *comp = &image->comps[compno];
1107 char bname[256]; /* buffer for name */
1108 char *name = bname; /* pointer */
1110 const size_t olen = strlen(outfile);
1111 const size_t dotpos = olen - 4;
1112 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1113 if( outfile[dotpos] != '.' ) {
1114 /* `pgx` was recognized but there is no dot at expected position */
1115 fprintf(stderr, "ERROR -> Impossible happen." );
1119 name = (char*)malloc(total+1);
1121 strncpy(name, outfile, dotpos);
1122 if (image->numcomps > 1) {
1123 sprintf(name+dotpos, "-%d.pgx", compno);
1125 strcpy(name+dotpos, ".pgx");
1127 fdest = fopen(name, "wb");
1129 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1132 /* dont need name anymore */
1137 w = image->comps[compno].w;
1138 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1140 h = image->comps[compno].h;
1141 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1143 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
1144 if (comp->prec <= 8) {
1146 } else if (comp->prec <= 16) {
1151 for (i = 0; i < wr * hr; i++) {
1152 int v = image->comps[compno].data[i / wr * w + i % wr];
1153 for (j = nbytes - 1; j >= 0; j--) {
1154 char byte = (char) (v >> (j * 8));
1155 fwrite(&byte, 1, 1, fdest);
1164 /* -->> -->> -->> -->>
1168 <<-- <<-- <<-- <<-- */
1170 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1171 int subsampling_dx = parameters->subsampling_dx;
1172 int subsampling_dy = parameters->subsampling_dy;
1175 int i, compno, numcomps, w, h;
1176 OPJ_COLOR_SPACE color_space;
1177 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
1178 opj_image_t * image = NULL;
1181 f = fopen(filename, "rb");
1183 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1187 if (fgetc(f) != 'P')
1192 case '2': /* greyscale image type */
1195 color_space = CLRSPC_GRAY;
1198 case '3': /* RGB image type */
1201 color_space = CLRSPC_SRGB;
1212 while(fgetc(f) == '#') while(fgetc(f) != '\n');
1214 fseek(f, -1, SEEK_CUR);
1215 fscanf(f, "%d %d\n255", &w, &h);
1216 fgetc(f); /* <cr><lf> */
1218 /* initialize image components */
1219 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1220 for(i = 0; i < numcomps; i++) {
1221 cmptparm[i].prec = 8;
1222 cmptparm[i].bpp = 8;
1223 cmptparm[i].sgnd = 0;
1224 cmptparm[i].dx = subsampling_dx;
1225 cmptparm[i].dy = subsampling_dy;
1229 /* create the image */
1230 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1236 /* set image offset and reference grid */
1237 image->x0 = parameters->image_offset_x0;
1238 image->y0 = parameters->image_offset_y0;
1239 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1240 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1242 /* set image data */
1244 if ((value == '2') || (value == '3')) { /* ASCII */
1245 for (i = 0; i < w * h; i++) {
1246 for(compno = 0; compno < numcomps; compno++) {
1247 unsigned int index = 0;
1248 fscanf(f, "%u", &index);
1249 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1250 image->comps[compno].data[i] = index;
1253 } else if ((value == '5') || (value == '6')) { /* BINARY */
1254 for (i = 0; i < w * h; i++) {
1255 for(compno = 0; compno < numcomps; compno++) {
1256 unsigned char index = 0;
1257 fread(&index, 1, 1, f);
1258 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1259 image->comps[compno].data[i] = index;
1269 int imagetopnm(opj_image_t * image, const char *outfile) {
1270 int w, wr, wrr, h, hr, hrr, max;
1272 int adjustR, adjustG, adjustB, adjustX;
1275 const char *tmp = outfile;
1284 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1285 && image->comps[1].dx == image->comps[2].dx
1286 && image->comps[0].dy == image->comps[1].dy
1287 && image->comps[1].dy == image->comps[2].dy
1288 && image->comps[0].prec == image->comps[1].prec
1289 && image->comps[1].prec == image->comps[2].prec
1290 && S2 !='g' && S2 !='G') {
1292 fdest = fopen(outfile, "wb");
1294 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1298 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
1299 wr = image->comps[0].w;
1300 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
1302 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
1303 hr = image->comps[0].h;
1304 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
1306 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
1308 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
1309 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
1311 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
1313 if (image->comps[0].prec > 8) {
1314 adjustR = image->comps[0].prec - 8;
1315 printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1319 if (image->comps[1].prec > 8) {
1320 adjustG = image->comps[1].prec - 8;
1321 printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1325 if (image->comps[2].prec > 8) {
1326 adjustB = image->comps[2].prec - 8;
1327 printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1333 for (i = 0; i < wrr * hrr; i++) {
1335 unsigned char rc,gc,bc;
1336 r = image->comps[0].data[i / wrr * wr + i % wrr];
1337 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1338 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
1340 g = image->comps[1].data[i / wrr * wr + i % wrr];
1341 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1342 gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
1344 b = image->comps[2].data[i / wrr * wr + i % wrr];
1345 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1346 bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
1348 fprintf(fdest, "%c%c%c", rc, gc, bc);
1353 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1354 if (image->numcomps > ncomp) {
1355 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1356 fprintf(stderr," is written to the file\n");
1358 for (compno = 0; compno < ncomp; compno++) {
1361 sprintf(name, "%d.%s", compno, outfile);
1363 sprintf(name, "%s", outfile);
1366 fdest = fopen(name, "wb");
1368 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1372 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1373 wr = image->comps[compno].w;
1374 wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1376 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1377 hr = image->comps[compno].h;
1378 hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1380 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1382 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1383 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1385 fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1387 if (image->comps[compno].prec > 8) {
1388 adjustX = image->comps[0].prec - 8;
1389 printf("PNM CONVERSION: Truncating component %d from %d bits to 8 bits\n",compno, image->comps[compno].prec);
1394 for (i = 0; i < wrr * hrr; i++) {
1397 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1398 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1399 lc = (unsigned char) ((l >> adjustX)+((l >> (adjustX-1))%2));
1400 fprintf(fdest, "%c", lc);
1409 /* -->> -->> -->> -->>
1413 <<-- <<-- <<-- <<-- */
1415 typedef struct tiff_infoheader{
1416 DWORD tiWidth; // Width of Image in pixel
1417 DWORD tiHeight; // Height of Image in pixel
1418 DWORD tiPhoto; // Photometric
1419 WORD tiBps; // Bits per sample
1420 WORD tiSf; // Sample Format
1421 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1422 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1425 int imagetotif(opj_image_t * image, const char *outfile) {
1433 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1434 && image->comps[1].dx == image->comps[2].dx
1435 && image->comps[0].dy == image->comps[1].dy
1436 && image->comps[1].dy == image->comps[2].dy
1437 && image->comps[0].prec == image->comps[1].prec
1438 && image->comps[1].prec == image->comps[2].prec) {
1444 tif = TIFFOpen(outfile, "wb");
1446 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1450 width = image->comps[0].w;
1451 height= image->comps[0].h;
1452 bps = image->comps[0].prec;
1454 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1455 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1456 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1457 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1458 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1459 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1460 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1461 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1463 /* Get a buffer for the data */
1464 buf = _TIFFmalloc(TIFFStripSize(tif));
1467 strip_size=TIFFStripSize(tif);
1468 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1469 unsigned char *dat8;
1472 if (image->comps[0].prec == 8){
1473 for (i=0; i<TIFFStripSize(tif); i+=3) { // 8 bits per pixel
1474 dat8[i+0] = image->comps[0].data[index] ; // R
1475 dat8[i+1] = image->comps[1].data[index] ; // G
1476 dat8[i+2] = image->comps[2].data[index] ; // B
1479 }else if (image->comps[0].prec == 12){
1480 for (i=0; i<TIFFStripSize(tif); i+=9) { // 12 bits per pixel
1481 dat8[i+0] = (image->comps[0].data[index]>>8)<<4 | (image->comps[0].data[index]>>4);
1482 dat8[i+1] = (image->comps[0].data[index]<<4)|((image->comps[1].data[index]>>8)& 0x0f);
1483 dat8[i+2] = (image->comps[1].data[index]);
1484 dat8[i+3] = (image->comps[2].data[index]>>8)<<4 | (image->comps[2].data[index]>>4);
1485 dat8[i+4] = (image->comps[2].data[index]<<4)|((image->comps[0].data[index+1]>>8)& 0x0f);
1486 dat8[i+5] = (image->comps[0].data[index+1]);
1487 dat8[i+6] = (image->comps[1].data[index+1]>>8)<<4 | (image->comps[1].data[index+1]>>4);
1488 dat8[i+7] = (image->comps[1].data[index+1]<<4)|((image->comps[2].data[index+1]>>8)& 0x0f);
1489 dat8[i+8] = (image->comps[2].data[index+1]);
1492 }else if (image->comps[0].prec == 16){
1493 for (i=0; i<TIFFStripSize(tif); i+=6) { // 16 bits per pixel
1494 dat8[i+0] = image->comps[0].data[index];//LSB
1495 dat8[i+1] = (image->comps[0].data[index]>> 8);//MSB
1496 dat8[i+2] = image->comps[1].data[index];
1497 dat8[i+3] = (image->comps[1].data[index]>> 8);
1498 dat8[i+4] = image->comps[2].data[index];
1499 dat8[i+5] = (image->comps[2].data[index]>> 8);
1503 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1504 fprintf(stderr,"Aborting\n");
1507 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1511 }else if (image->numcomps == 1){
1516 tif = TIFFOpen(outfile, "wb");
1518 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1522 width = image->comps[0].w;
1523 height= image->comps[0].h;
1524 bps = image->comps[0].prec;
1527 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1528 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1529 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1530 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1531 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1532 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1533 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1534 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1536 /* Get a buffer for the data */
1537 buf = _TIFFmalloc(TIFFStripSize(tif));
1540 strip_size = TIFFStripSize(tif);
1541 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1542 unsigned char *dat8;
1545 if (image->comps[0].prec == 8){
1546 for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
1547 dat8[i+0] = image->comps[0].data[index] ;
1550 }else if (image->comps[0].prec == 12){
1551 for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
1552 dat8[i+0] = (image->comps[0].data[index]>>8)<<4 | (image->comps[0].data[index]>>4);
1553 dat8[i+1] = (image->comps[0].data[index]<<4)|((image->comps[0].data[index+1]>>8)& 0x0f);
1554 dat8[i+2] = (image->comps[0].data[index+1]);
1557 }else if (image->comps[0].prec == 16){
1558 for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
1559 dat8[i+0] = image->comps[0].data[index];
1560 dat8[i+1] = (image->comps[0].data[index]>> 8);
1564 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1565 fprintf(stderr,"Aborting\n");
1568 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1573 fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
1574 fprintf(stderr,"Aborting\n");
1580 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
1582 int subsampling_dx = parameters->subsampling_dx;
1583 int subsampling_dy = parameters->subsampling_dy;
1585 tiff_infoheader_t Info;
1589 int j, numcomps, w, h,index;
1590 OPJ_COLOR_SPACE color_space;
1591 opj_image_cmptparm_t cmptparm[3];
1592 opj_image_t * image = NULL;
1594 tif = TIFFOpen(filename, "r");
1597 fprintf(stderr, "Failed to open %s for reading\n", filename);
1601 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1602 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1603 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1604 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1605 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1607 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1608 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1612 if (Info.tiPhoto == 2) {
1618 color_space = CLRSPC_SRGB;
1619 /* initialize image components*/
1620 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1621 for(j = 0; j < numcomps; j++) {
1622 if (parameters->cp_cinema) {
1623 cmptparm[j].prec = 12;
1624 cmptparm[j].bpp = 12;
1626 cmptparm[j].prec = Info.tiBps;
1627 cmptparm[j].bpp = Info.tiBps;
1629 cmptparm[j].sgnd = 0;
1630 cmptparm[j].dx = subsampling_dx;
1631 cmptparm[j].dy = subsampling_dy;
1635 /* create the image*/
1636 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1642 /* set image offset and reference grid */
1643 image->x0 = parameters->image_offset_x0;
1644 image->y0 = parameters->image_offset_y0;
1645 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1646 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1648 buf = _TIFFmalloc(TIFFStripSize(tif));
1650 strip_size=TIFFStripSize(tif);
1652 /* Read the Image components*/
1653 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1654 unsigned char *dat8;
1656 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1659 if (Info.tiBps==12){
1660 for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
1661 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
1662 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1663 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
1664 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1665 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
1666 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1670 else if( Info.tiBps==16){
1671 for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
1672 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
1673 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
1674 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
1675 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1676 image->comps[0].data[index] = (image->comps[0].data[index] + 0x08) >> 4 ;
1677 image->comps[1].data[index] = (image->comps[1].data[index] + 0x08) >> 4 ;
1678 image->comps[2].data[index] = (image->comps[2].data[index] + 0x08) >> 4 ;
1683 else if ( Info.tiBps==8){
1684 for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
1685 image->comps[0].data[index] = dat8[i+0]; // R
1686 image->comps[1].data[index] = dat8[i+1]; // G
1687 image->comps[2].data[index] = dat8[i+2]; // B
1688 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1689 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
1690 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
1691 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
1697 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1698 fprintf(stderr,"Aborting\n");
1705 }else if(Info.tiPhoto == 1) {
1711 color_space = CLRSPC_GRAY;
1712 /* initialize image components*/
1713 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1714 cmptparm[0].prec = Info.tiBps;
1715 cmptparm[0].bpp = Info.tiBps;
1716 cmptparm[0].sgnd = 0;
1717 cmptparm[0].dx = subsampling_dx;
1718 cmptparm[0].dy = subsampling_dy;
1722 /* create the image*/
1723 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1728 /* set image offset and reference grid */
1729 image->x0 = parameters->image_offset_x0;
1730 image->y0 = parameters->image_offset_y0;
1731 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1732 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1734 buf = _TIFFmalloc(TIFFStripSize(tif));
1736 strip_size = TIFFStripSize(tif);
1738 /* Read the Image components*/
1739 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1740 unsigned char *dat8;
1742 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1745 if (Info.tiBps==12){
1746 for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
1747 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
1748 image->comps[0].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1752 else if( Info.tiBps==16){
1753 for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
1754 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
1758 else if ( Info.tiBps==8){
1759 for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
1760 image->comps[0].data[index] = dat8[i+0];
1765 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1766 fprintf(stderr,"Aborting\n");
1774 fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
1775 fprintf(stderr,"Aborting\n");
1781 /* -->> -->> -->> -->>
1785 <<-- <<-- <<-- <<-- */
1787 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
1788 int subsampling_dx = parameters->subsampling_dx;
1789 int subsampling_dy = parameters->subsampling_dy;
1792 int i, compno, numcomps, w, h;
1793 OPJ_COLOR_SPACE color_space;
1794 opj_image_cmptparm_t *cmptparm;
1795 opj_image_t * image = NULL;
1798 if((raw_cp->rawWidth * raw_cp->rawHeight * raw_cp->rawComp * raw_cp->rawBitDepth) == 0)
1800 fprintf(stderr,"\nError: invalid raw image parameters\n");
1801 fprintf(stderr,"Please use the Format option -F:\n");
1802 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
1803 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
1804 fprintf(stderr,"Aborting\n");
1808 f = fopen(filename, "rb");
1810 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1811 fprintf(stderr,"Aborting\n");
1814 numcomps = raw_cp->rawComp;
1815 color_space = CLRSPC_SRGB;
1816 w = raw_cp->rawWidth;
1817 h = raw_cp->rawHeight;
1818 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
1820 /* initialize image components */
1821 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1822 for(i = 0; i < numcomps; i++) {
1823 cmptparm[i].prec = raw_cp->rawBitDepth;
1824 cmptparm[i].bpp = raw_cp->rawBitDepth;
1825 cmptparm[i].sgnd = raw_cp->rawSigned;
1826 cmptparm[i].dx = subsampling_dx;
1827 cmptparm[i].dy = subsampling_dy;
1831 /* create the image */
1832 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1838 /* set image offset and reference grid */
1839 image->x0 = parameters->image_offset_x0;
1840 image->y0 = parameters->image_offset_y0;
1841 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1842 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1844 if(raw_cp->rawBitDepth <= 8)
1846 unsigned char value = 0;
1847 for(compno = 0; compno < numcomps; compno++) {
1848 for (i = 0; i < w * h; i++) {
1849 if (!fread(&value, 1, 1, f)) {
1850 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
1853 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
1859 unsigned short value = 0;
1860 for(compno = 0; compno < numcomps; compno++) {
1861 for (i = 0; i < w * h; i++) {
1862 if (!fread(&value, 2, 1, f)) {
1863 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
1866 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
1871 if (fread(&ch, 1, 1, f)) {
1872 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
1880 int imagetoraw(opj_image_t * image, const char *outfile)
1882 FILE *rawFile = NULL;
1883 int compno, pixelsToWrite, offset, cont;
1885 if((image->numcomps * image->x1 * image->y1) == 0)
1887 fprintf(stderr,"\nError: invalid raw image parameters\n");
1891 rawFile = fopen(outfile, "wb");
1893 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
1897 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
1899 for(compno = 0; compno < image->numcomps; compno++)
1901 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
1902 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
1904 pixelsToWrite = image->comps[compno].w * image->comps[compno].h;
1907 if(image->comps[compno].prec <= 8)
1909 if(image->comps[compno].sgnd == 1)
1912 int mask = (1 << image->comps[compno].prec) - 1;
1913 for(cont = 0; cont < pixelsToWrite; cont++)
1915 curr = (signed char) (image->comps[compno].data[cont] & mask);
1916 fwrite(&curr, sizeof(signed char), 1, rawFile);
1919 else if(image->comps[compno].sgnd == 0)
1922 int mask = (1 << image->comps[compno].prec) - 1;
1923 for(cont = 0; cont < pixelsToWrite; cont++)
1925 curr = (unsigned char) (image->comps[compno].data[cont] & mask);
1926 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
1930 else if(image->comps[compno].prec <= 16)
1932 if(image->comps[compno].sgnd == 1)
1934 signed short int curr;
1935 int mask = (1 << image->comps[compno].prec) - 1;
1936 for(cont = 0; cont < pixelsToWrite; cont++)
1938 curr = (signed short int) (image->comps[compno].data[cont] & mask);
1939 fwrite(&curr, sizeof(signed short int), 1, rawFile);
1942 else if(image->comps[compno].sgnd == 0)
1944 unsigned short int curr;
1945 int mask = (1 << image->comps[compno].prec) - 1;
1946 for(cont = 0; cont < pixelsToWrite; cont++)
1948 curr = (unsigned short int) (image->comps[compno].data[cont] & mask);
1949 fwrite(&curr, sizeof(unsigned short int), 1, rawFile);
1953 else if (image->comps[compno].prec <= 32)
1960 fprintf(stderr,"\nError: invalid precision\n");