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"
44 * Get logarithm of an integer and round downwards.
48 static int int_floorlog2(int a) {
50 for (l = 0; a > 1; l++) {
57 * Divide an integer by a power of 2 and round upwards.
61 static int int_ceildivpow2(int a, int b) {
62 return (a + (1 << b) - 1) >> b;
66 * Divide an integer and round upwards.
70 static int int_ceildiv(int a, int b) {
71 return (a + b - 1) / b;
75 /* -->> -->> -->> -->>
79 <<-- <<-- <<-- <<-- */
81 // TGA header definition.
82 #pragma pack(push,1) // Pack structure byte aligned
83 typedef struct tga_header
85 uint8 id_length; /* Image id field length */
86 uint8 colour_map_type; /* Colour map type */
87 uint8 image_type; /* Image type */
89 ** Colour map specification
91 uint16 colour_map_index; /* First entry index */
92 uint16 colour_map_length; /* Colour map length */
93 uint8 colour_map_entry_size; /* Colour map entry size */
95 ** Image specification
97 uint16 x_origin; /* x origin of image */
98 uint16 y_origin; /* u origin of image */
99 uint16 image_width; /* Image width */
100 uint16 image_height; /* Image height */
101 uint8 pixel_depth; /* Pixel depth */
102 uint8 image_desc; /* Image descriptor */
104 #pragma pack(pop) // Return to normal structure packing alignment.
106 int tga_readheader(FILE *fp, uint32 *bits_per_pixel, uint32 *width, uint32 *height, int *flip_image)
111 if (!bits_per_pixel || !width || !height || !flip_image)
115 fread((uint8*)&tga, sizeof(tga_header), 1, fp);
117 *bits_per_pixel = tga.pixel_depth;
119 *width = tga.image_width;
120 *height = tga.image_height ;
122 // Ignore tga identifier, if present ...
125 uint8 *id = (uint8 *) malloc(tga.id_length);
126 fread(id, tga.id_length, 1, fp);
130 // Test for compressed formats ... not yet supported ...
131 // Note :- 9 - RLE encoded palettized.
132 // 10 - RLE encoded RGB.
133 if (tga.image_type > 8)
135 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
139 *flip_image = !(tga.image_desc & 32);
141 // Palettized formats are not yet supported, skip over the palette, if present ...
142 palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
146 fprintf(stderr, "File contains a palette - not yet supported.");
147 fseek(fp, palette_size, SEEK_CUR);
152 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, bool flip_image)
156 if (!bits_per_pixel || !width || !height)
159 memset(&tga, 0, sizeof(tga_header));
161 tga.pixel_depth = bits_per_pixel;
162 tga.image_width = width;
163 tga.image_height = height;
164 tga.image_type = 2; // Uncompressed.
165 tga.image_desc = 8; // 8 bits per component.
168 tga.image_desc |= 32;
171 fwrite((uint8*)&tga, sizeof(tga_header), 1, fp);
176 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
179 uint32 image_width, image_height, pixel_bit_depth;
182 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
184 OPJ_COLOR_SPACE color_space;
187 int subsampling_dx, subsampling_dy;
190 f = fopen(filename, "rb");
192 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
196 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
199 // We currently only support 24 & 32 bit tga's ...
200 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
203 /* initialize image components */
204 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
206 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha.
207 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
210 color_space = CLRSPC_GRAY;
211 numcomps = save_alpha ? 2 : 1;
214 numcomps = save_alpha ? 4 : 3;
215 color_space = CLRSPC_SRGB;
218 subsampling_dx = parameters->subsampling_dx;
219 subsampling_dy = parameters->subsampling_dy;
221 for (i = 0; i < numcomps; i++) {
222 cmptparm[i].prec = 8;
224 cmptparm[i].sgnd = 0;
225 cmptparm[i].dx = subsampling_dx;
226 cmptparm[i].dy = subsampling_dy;
227 cmptparm[i].w = image_width;
228 cmptparm[i].h = image_height;
231 /* create the image */
232 image = opj_image_create(numcomps, &cmptparm[0], color_space);
237 /* set image offset and reference grid */
238 image->x0 = parameters->image_offset_x0;
239 image->y0 = parameters->image_offset_y0;
240 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
241 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
244 for (y=0; y < image_height; y++)
249 index = (image_height-y-1)*image_width;
251 index = y*image_width;
255 for (x=0;x<image_width;x++)
262 image->comps[0].data[index]=r;
263 image->comps[1].data[index]=g;
264 image->comps[2].data[index]=b;
268 else if (numcomps==4)
270 for (x=0;x<image_width;x++)
278 image->comps[0].data[index]=r;
279 image->comps[1].data[index]=g;
280 image->comps[2].data[index]=b;
281 image->comps[3].data[index]=a;
286 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
292 int imagetotga(opj_image_t * image, const char *outfile) {
293 int width, height, bpp, x, y;
296 uint32 alpha_channel;
302 fdest = fopen(outfile, "wb");
304 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
308 for (i = 0; i < image->numcomps-1; i++) {
309 if ((image->comps[0].dx != image->comps[i+1].dx)
310 ||(image->comps[0].dy != image->comps[i+1].dy)
311 ||(image->comps[0].prec != image->comps[i+1].prec)) {
312 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
317 width = image->comps[0].w;
318 height = image->comps[0].h;
320 // Mono with alpha, or RGB with alpha.
321 write_alpha = (image->numcomps==2) || (image->numcomps==4);
324 bpp = write_alpha ? 32 : 24;
325 if (!tga_writeheader(fdest, bpp, width , height, true))
328 alpha_channel = image->numcomps-1;
330 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
332 for (y=0; y < height; y++) {
333 uint32 index=y*width;
335 for (x=0; x < width; x++, index++) {
336 r = (float)(image->comps[0].data[index]);
338 if (image->numcomps>2) {
339 g = (float)(image->comps[1].data[index]);
340 b = (float)(image->comps[2].data[index]);
342 else {// Greyscale ...
347 // TGA format writes BGR ...
348 value = (uint8)(b*scale);
349 fwrite(&value,1,1,fdest);
351 value = (uint8)(g*scale);
352 fwrite(&value,1,1,fdest);
354 value = (uint8)(r*scale);
355 fwrite(&value,1,1,fdest);
358 a = (float)(image->comps[alpha_channel].data[index]);
359 value = (uint8)(a*scale);
360 fwrite(&value,1,1,fdest);
368 /* -->> -->> -->> -->>
372 <<-- <<-- <<-- <<-- */
374 /* WORD defines a two byte word */
375 typedef unsigned short int WORD;
377 /* DWORD defines a four byte word */
378 typedef unsigned long int DWORD;
381 WORD bfType; /* 'BM' for Bitmap (19776) */
382 DWORD bfSize; /* Size of the file */
383 WORD bfReserved1; /* Reserved : 0 */
384 WORD bfReserved2; /* Reserved : 0 */
385 DWORD bfOffBits; /* Offset */
386 } BITMAPFILEHEADER_t;
389 DWORD biSize; /* Size of the structure in bytes */
390 DWORD biWidth; /* Width of the image in pixels */
391 DWORD biHeight; /* Heigth of the image in pixels */
392 WORD biPlanes; /* 1 */
393 WORD biBitCount; /* Number of color bits by pixels */
394 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
395 DWORD biSizeImage; /* Size of the image in bytes */
396 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
397 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
398 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
399 DWORD biClrImportant; /* Number of important color (0: ALL) */
400 } BITMAPINFOHEADER_t;
402 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
403 int subsampling_dx = parameters->subsampling_dx;
404 int subsampling_dy = parameters->subsampling_dy;
406 int i, numcomps, w, h;
407 OPJ_COLOR_SPACE color_space;
408 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
409 opj_image_t * image = NULL;
412 BITMAPFILEHEADER_t File_h;
413 BITMAPINFOHEADER_t Info_h;
415 unsigned char *table_R, *table_G, *table_B;
416 unsigned int j, PAD = 0;
419 int gray_scale = 1, not_end_file = 1;
421 unsigned int line = 0, col = 0;
425 IN = fopen(filename, "rb");
427 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
431 File_h.bfType = getc(IN);
432 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
434 if (File_h.bfType != 19778) {
435 fprintf(stderr,"Error, not a BMP file!\n");
440 File_h.bfSize = getc(IN);
441 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
442 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
443 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
445 File_h.bfReserved1 = getc(IN);
446 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
448 File_h.bfReserved2 = getc(IN);
449 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
451 File_h.bfOffBits = getc(IN);
452 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
453 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
454 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
459 Info_h.biSize = getc(IN);
460 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
461 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
462 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
464 Info_h.biWidth = getc(IN);
465 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
466 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
467 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
470 Info_h.biHeight = getc(IN);
471 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
472 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
473 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
476 Info_h.biPlanes = getc(IN);
477 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
479 Info_h.biBitCount = getc(IN);
480 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
482 Info_h.biCompression = getc(IN);
483 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
484 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
485 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
487 Info_h.biSizeImage = getc(IN);
488 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
489 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
490 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
492 Info_h.biXpelsPerMeter = getc(IN);
493 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
494 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
495 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
497 Info_h.biYpelsPerMeter = getc(IN);
498 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
499 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
500 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
502 Info_h.biClrUsed = getc(IN);
503 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
504 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
505 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
507 Info_h.biClrImportant = getc(IN);
508 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
509 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
510 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
512 /* Read the data and store them in the OUT file */
514 if (Info_h.biBitCount == 24) {
516 color_space = CLRSPC_SRGB;
517 /* initialize image components */
518 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
519 for(i = 0; i < numcomps; i++) {
520 cmptparm[i].prec = 8;
522 cmptparm[i].sgnd = 0;
523 cmptparm[i].dx = subsampling_dx;
524 cmptparm[i].dy = subsampling_dy;
528 /* create the image */
529 image = opj_image_create(numcomps, &cmptparm[0], color_space);
535 /* set image offset and reference grid */
536 image->x0 = parameters->image_offset_x0;
537 image->y0 = parameters->image_offset_y0;
538 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
539 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
543 /* Place the cursor at the beginning of the image information */
544 fseek(IN, 0, SEEK_SET);
545 fseek(IN, File_h.bfOffBits, SEEK_SET);
550 /* PAD = 4 - (3 * W) % 4; */
551 /* PAD = (PAD == 4) ? 0 : PAD; */
552 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
554 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
556 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
560 for(y = 0; y < (int)H; y++) {
561 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
562 for(x = 0; x < (int)W; x++) {
563 unsigned char *pixel = &scanline[3 * x];
564 image->comps[0].data[index] = pixel[2]; /* R */
565 image->comps[1].data[index] = pixel[1]; /* G */
566 image->comps[2].data[index] = pixel[0]; /* B */
573 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
574 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
575 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
576 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
578 for (j = 0; j < Info_h.biClrUsed; j++) {
579 table_B[j] = getc(IN);
580 table_G[j] = getc(IN);
581 table_R[j] = getc(IN);
583 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
587 /* Place the cursor at the beginning of the image information */
588 fseek(IN, 0, SEEK_SET);
589 fseek(IN, File_h.bfOffBits, SEEK_SET);
593 if (Info_h.biWidth % 2)
596 numcomps = gray_scale ? 1 : 3;
597 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
598 /* initialize image components */
599 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
600 for(i = 0; i < numcomps; i++) {
601 cmptparm[i].prec = 8;
603 cmptparm[i].sgnd = 0;
604 cmptparm[i].dx = subsampling_dx;
605 cmptparm[i].dy = subsampling_dy;
609 /* create the image */
610 image = opj_image_create(numcomps, &cmptparm[0], color_space);
616 /* set image offset and reference grid */
617 image->x0 = parameters->image_offset_x0;
618 image->y0 = parameters->image_offset_y0;
619 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
620 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
624 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
626 fread(RGB, sizeof(unsigned char), W * H, IN);
629 for (j = 0; j < W * H; j++) {
630 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
631 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
638 for (j = 0; j < W * H; j++) {
639 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
640 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
641 image->comps[0].data[index] = table_R[pixel_index];
642 image->comps[1].data[index] = table_G[pixel_index];
643 image->comps[2].data[index] = table_B[pixel_index];
652 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
653 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
654 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
655 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
657 for (j = 0; j < Info_h.biClrUsed; j++) {
658 table_B[j] = getc(IN);
659 table_G[j] = getc(IN);
660 table_R[j] = getc(IN);
662 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
666 numcomps = gray_scale ? 1 : 3;
667 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
668 /* initialize image components */
669 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
670 for(i = 0; i < numcomps; i++) {
671 cmptparm[i].prec = 8;
673 cmptparm[i].sgnd = 0;
674 cmptparm[i].dx = subsampling_dx;
675 cmptparm[i].dy = subsampling_dy;
679 /* create the image */
680 image = opj_image_create(numcomps, &cmptparm[0], color_space);
686 /* set image offset and reference grid */
687 image->x0 = parameters->image_offset_x0;
688 image->y0 = parameters->image_offset_y0;
689 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
690 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
694 /* Place the cursor at the beginning of the image information */
695 fseek(IN, 0, SEEK_SET);
696 fseek(IN, File_h.bfOffBits, SEEK_SET);
698 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
700 while (not_end_file) {
704 for (i = 0; i < (int) v; i++) {
705 RGB[line * Info_h.biWidth + col] = v2;
720 fprintf(stderr,"No Delta supported\n");
721 opj_image_destroy(image);
725 for (i = 0; i < v; i++) {
727 RGB[line * Info_h.biWidth + col] = v2;
738 for (line = 0; line < Info_h.biHeight; line++) {
739 for (col = 0; col < Info_h.biWidth; col++) {
740 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
746 for (line = 0; line < Info_h.biHeight; line++) {
747 for (col = 0; col < Info_h.biWidth; col++) {
748 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
749 image->comps[0].data[index] = table_R[pixel_index];
750 image->comps[1].data[index] = table_G[pixel_index];
751 image->comps[2].data[index] = table_B[pixel_index];
762 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
770 int imagetobmp(opj_image_t * image, const char *outfile) {
774 int adjustR, adjustG, adjustB;
776 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
777 && image->comps[1].dx == image->comps[2].dx
778 && image->comps[0].dy == image->comps[1].dy
779 && image->comps[1].dy == image->comps[2].dy
780 && image->comps[0].prec == image->comps[1].prec
781 && image->comps[1].prec == image->comps[2].prec) {
783 /* -->> -->> -->> -->>
785 <<-- <<-- <<-- <<-- */
787 fdest = fopen(outfile, "wb");
789 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
793 w = image->comps[0].w;
794 h = image->comps[0].h;
796 fprintf(fdest, "BM");
800 fprintf(fdest, "%c%c%c%c",
801 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
802 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
803 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
804 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
805 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
806 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
810 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
811 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
812 (unsigned char) ((w) >> 8) & 0xff,
813 (unsigned char) ((w) >> 16) & 0xff,
814 (unsigned char) ((w) >> 24) & 0xff);
815 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
816 (unsigned char) ((h) >> 8) & 0xff,
817 (unsigned char) ((h) >> 16) & 0xff,
818 (unsigned char) ((h) >> 24) & 0xff);
819 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
820 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
821 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
822 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
823 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
824 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
825 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
828 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
829 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
831 if (image->comps[0].prec > 8) {
832 adjustR = image->comps[0].prec - 8;
833 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
837 if (image->comps[1].prec > 8) {
838 adjustG = image->comps[1].prec - 8;
839 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
843 if (image->comps[2].prec > 8) {
844 adjustB = image->comps[2].prec - 8;
845 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
850 for (i = 0; i < w * h; i++) {
851 unsigned char rc, gc, bc;
854 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
855 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
856 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
857 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
858 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
859 gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
860 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
861 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
862 bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
864 fprintf(fdest, "%c%c%c", bc, gc, rc);
866 if ((i + 1) % w == 0) {
867 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
868 fprintf(fdest, "%c", 0);
872 } else { /* Gray-scale */
874 /* -->> -->> -->> -->>
875 8 bits non code (Gray scale)
876 <<-- <<-- <<-- <<-- */
878 fdest = fopen(outfile, "wb");
879 w = image->comps[0].w;
880 h = image->comps[0].h;
882 fprintf(fdest, "BM");
886 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
887 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
888 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
889 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
890 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
891 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
892 ((54 + 1024) >> 16) & 0xff,
893 ((54 + 1024) >> 24) & 0xff);
897 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
898 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
899 (unsigned char) ((w) >> 8) & 0xff,
900 (unsigned char) ((w) >> 16) & 0xff,
901 (unsigned char) ((w) >> 24) & 0xff);
902 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
903 (unsigned char) ((h) >> 8) & 0xff,
904 (unsigned char) ((h) >> 16) & 0xff,
905 (unsigned char) ((h) >> 24) & 0xff);
906 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
907 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
908 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
909 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
910 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
911 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
912 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
913 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
914 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
915 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
916 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
918 if (image->comps[0].prec > 8) {
919 adjustR = image->comps[0].prec - 8;
920 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
924 for (i = 0; i < 256; i++) {
925 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
928 for (i = 0; i < w * h; i++) {
932 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
933 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
934 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
936 fprintf(fdest, "%c", rc);
938 if ((i + 1) % w == 0) {
939 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
940 fprintf(fdest, "%c", 0);
949 /* -->> -->> -->> -->>
953 <<-- <<-- <<-- <<-- */
956 unsigned char readuchar(FILE * f)
963 unsigned short readushort(FILE * f, int bigendian)
965 unsigned char c1, c2;
969 return (c1 << 8) + c2;
971 return (c2 << 8) + c1;
974 unsigned int readuint(FILE * f, int bigendian)
976 unsigned char c1, c2, c3, c4;
982 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
984 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
987 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
990 int i, numcomps, max;
991 OPJ_COLOR_SPACE color_space;
992 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
993 opj_image_t * image = NULL;
995 char endian1,endian2,sign;
1000 opj_image_comp_t *comp = NULL;
1003 color_space = CLRSPC_GRAY;
1005 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1009 f = fopen(filename, "rb");
1011 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1015 fseek(f, 0, SEEK_SET);
1016 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
1020 while (signtmp[i]!='\0') {
1021 if (signtmp[i]=='-') sign='-';
1026 if (endian1=='M' && endian2=='L') {
1028 } else if (endian2=='M' && endian1=='L') {
1031 fprintf(stderr, "Bad pgx header, please check input file\n");
1035 /* initialize image component */
1037 cmptparm.x0 = parameters->image_offset_x0;
1038 cmptparm.y0 = parameters->image_offset_y0;
1039 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1040 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1047 cmptparm.prec = prec;
1048 cmptparm.bpp = prec;
1049 cmptparm.dx = parameters->subsampling_dx;
1050 cmptparm.dy = parameters->subsampling_dy;
1052 /* create the image */
1053 image = opj_image_create(numcomps, &cmptparm, color_space);
1058 /* set image offset and reference grid */
1059 image->x0 = cmptparm.x0;
1060 image->y0 = cmptparm.x0;
1061 image->x1 = cmptparm.w;
1062 image->y1 = cmptparm.h;
1064 /* set image data */
1066 comp = &image->comps[0];
1068 for (i = 0; i < w * h; i++) {
1070 if (comp->prec <= 8) {
1074 v = (char) readuchar(f);
1076 } else if (comp->prec <= 16) {
1078 v = readushort(f, bigendian);
1080 v = (short) readushort(f, bigendian);
1084 v = readuint(f, bigendian);
1086 v = (int) readuint(f, bigendian);
1094 comp->bpp = int_floorlog2(max) + 1;
1099 int imagetopgx(opj_image_t * image, const char *outfile) {
1104 for (compno = 0; compno < image->numcomps; compno++) {
1105 opj_image_comp_t *comp = &image->comps[compno];
1106 char bname[256]; /* buffer for name */
1107 char *name = bname; /* pointer */
1109 const size_t olen = strlen(outfile);
1110 const size_t dotpos = olen - 4;
1111 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1112 if( outfile[dotpos] != '.' ) {
1113 /* `pgx` was recognized but there is no dot at expected position */
1114 fprintf(stderr, "ERROR -> Impossible happen." );
1118 name = (char*)malloc(total+1);
1120 strncpy(name, outfile, dotpos);
1121 if (image->numcomps > 1) {
1122 sprintf(name+dotpos, "-%d.pgx", compno);
1124 strcpy(name+dotpos, ".pgx");
1126 fdest = fopen(name, "wb");
1128 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1131 /* dont need name anymore */
1136 w = image->comps[compno].w;
1137 h = image->comps[compno].h;
1139 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1140 if (comp->prec <= 8) {
1142 } else if (comp->prec <= 16) {
1147 for (i = 0; i < w * h; i++) {
1148 int v = image->comps[compno].data[i];
1149 for (j = nbytes - 1; j >= 0; j--) {
1150 char byte = (char) (v >> (j * 8));
1151 fwrite(&byte, 1, 1, fdest);
1160 /* -->> -->> -->> -->>
1164 <<-- <<-- <<-- <<-- */
1166 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1167 int subsampling_dx = parameters->subsampling_dx;
1168 int subsampling_dy = parameters->subsampling_dy;
1171 int i, compno, numcomps, w, h;
1172 OPJ_COLOR_SPACE color_space;
1173 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
1174 opj_image_t * image = NULL;
1177 f = fopen(filename, "rb");
1179 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1183 if (fgetc(f) != 'P')
1188 case '2': /* greyscale image type */
1191 color_space = CLRSPC_GRAY;
1194 case '3': /* RGB image type */
1197 color_space = CLRSPC_SRGB;
1208 while(fgetc(f) == '#') while(fgetc(f) != '\n');
1210 fseek(f, -1, SEEK_CUR);
1211 fscanf(f, "%d %d\n255", &w, &h);
1212 fgetc(f); /* <cr><lf> */
1214 /* initialize image components */
1215 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1216 for(i = 0; i < numcomps; i++) {
1217 cmptparm[i].prec = 8;
1218 cmptparm[i].bpp = 8;
1219 cmptparm[i].sgnd = 0;
1220 cmptparm[i].dx = subsampling_dx;
1221 cmptparm[i].dy = subsampling_dy;
1225 /* create the image */
1226 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1232 /* set image offset and reference grid */
1233 image->x0 = parameters->image_offset_x0;
1234 image->y0 = parameters->image_offset_y0;
1235 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1236 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1238 /* set image data */
1240 if ((value == '2') || (value == '3')) { /* ASCII */
1241 for (i = 0; i < w * h; i++) {
1242 for(compno = 0; compno < numcomps; compno++) {
1243 unsigned int index = 0;
1244 fscanf(f, "%u", &index);
1245 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1246 image->comps[compno].data[i] = index;
1249 } else if ((value == '5') || (value == '6')) { /* BINARY */
1250 for (i = 0; i < w * h; i++) {
1251 for(compno = 0; compno < numcomps; compno++) {
1252 unsigned char index = 0;
1253 fread(&index, 1, 1, f);
1254 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1255 image->comps[compno].data[i] = index;
1265 int imagetopnm(opj_image_t * image, const char *outfile) {
1266 int w, wr, h, hr, max;
1268 int adjustR, adjustG, adjustB, adjustX;
1271 const char *tmp = outfile;
1280 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1281 && image->comps[1].dx == image->comps[2].dx
1282 && image->comps[0].dy == image->comps[1].dy
1283 && image->comps[1].dy == image->comps[2].dy
1284 && image->comps[0].prec == image->comps[1].prec
1285 && image->comps[1].prec == image->comps[2].prec
1286 && S2 !='g' && S2 !='G') {
1288 fdest = fopen(outfile, "wb");
1290 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1294 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
1295 wr = image->comps[0].w;
1297 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
1298 hr = image->comps[0].h;
1300 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
1302 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
1303 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
1305 fprintf(fdest, "P6\n%d %d\n%d\n", wr, hr, max);
1307 if (image->comps[0].prec > 8) {
1308 adjustR = image->comps[0].prec - 8;
1309 printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1313 if (image->comps[1].prec > 8) {
1314 adjustG = image->comps[1].prec - 8;
1315 printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1319 if (image->comps[2].prec > 8) {
1320 adjustB = image->comps[2].prec - 8;
1321 printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1327 for (i = 0; i < wr * hr; i++) {
1329 unsigned char rc,gc,bc;
1330 r = image->comps[0].data[i];
1331 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1332 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
1334 g = image->comps[1].data[i];
1335 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1336 gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
1338 b = image->comps[2].data[i];
1339 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1340 bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
1342 fprintf(fdest, "%c%c%c", rc, gc, bc);
1347 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1348 if (image->numcomps > ncomp) {
1349 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1350 fprintf(stderr," is written to the file\n");
1352 for (compno = 0; compno < ncomp; compno++) {
1355 sprintf(name, "%d.%s", compno, outfile);
1357 sprintf(name, "%s", outfile);
1360 fdest = fopen(name, "wb");
1362 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1366 w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1367 wr = image->comps[compno].w;
1369 h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1370 hr = image->comps[compno].h;
1372 max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1374 image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1375 image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1377 fprintf(fdest, "P5\n%d %d\n%d\n", wr, hr, max);
1379 if (image->comps[compno].prec > 8) {
1380 adjustX = image->comps[0].prec - 8;
1381 printf("PNM CONVERSION: Truncating component %d from %d bits to 8 bits\n",compno, image->comps[compno].prec);
1386 for (i = 0; i < wr * hr; i++) {
1389 l = image->comps[compno].data[i];
1390 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1391 lc = (unsigned char) ((l >> adjustX)+((l >> (adjustX-1))%2));
1392 fprintf(fdest, "%c", lc);
1401 /* -->> -->> -->> -->>
1405 <<-- <<-- <<-- <<-- */
1407 typedef struct tiff_infoheader{
1408 DWORD tiWidth; // Width of Image in pixel
1409 DWORD tiHeight; // Height of Image in pixel
1410 DWORD tiPhoto; // Photometric
1411 WORD tiBps; // Bits per sample
1412 WORD tiSf; // Sample Format
1413 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1414 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1417 int imagetotif(opj_image_t * image, const char *outfile) {
1418 int width, height, imgsize;
1419 int bps,index,adjust = 0;
1426 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1427 && image->comps[1].dx == image->comps[2].dx
1428 && image->comps[0].dy == image->comps[1].dy
1429 && image->comps[1].dy == image->comps[2].dy
1430 && image->comps[0].prec == image->comps[1].prec
1431 && image->comps[1].prec == image->comps[2].prec) {
1437 tif = TIFFOpen(outfile, "wb");
1439 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1443 width = image->comps[0].w;
1444 height = image->comps[0].h;
1445 imgsize = width * height ;
1446 bps = image->comps[0].prec;
1448 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1449 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1450 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1451 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1452 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1453 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1454 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1455 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1457 /* Get a buffer for the data */
1458 strip_size=TIFFStripSize(tif);
1459 buf = _TIFFmalloc(strip_size);
1461 adjust = image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0;
1462 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1463 unsigned char *dat8;
1465 ssize = TIFFStripSize(tif);
1467 if (image->comps[0].prec == 8){
1468 for (i=0; i<ssize-2; i+=3) { // 8 bits per pixel
1469 int r = 0,g = 0,b = 0;
1470 if(index < imgsize){
1471 r = image->comps[0].data[index];
1472 g = image->comps[1].data[index];
1473 b = image->comps[2].data[index];
1474 if (image->comps[0].sgnd){
1479 dat8[i+0] = r ; // R
1480 dat8[i+1] = g ; // G
1481 dat8[i+2] = b ; // B
1488 for (i=last_i; i<ssize; i+=3) { // 8 bits per pixel
1489 int r = 0,g = 0,b = 0;
1490 if(index < imgsize){
1491 r = image->comps[0].data[index];
1492 g = image->comps[1].data[index];
1493 b = image->comps[2].data[index];
1494 if (image->comps[0].sgnd){
1499 dat8[i+0] = r ; // R
1500 if(i+1 <ssize) dat8[i+1] = g ; else break;// G
1501 if(i+2 <ssize) dat8[i+2] = b ; else break;// B
1507 }else if (image->comps[0].prec == 12){
1508 for (i=0; i<ssize-8; i+=9) { // 12 bits per pixel
1509 int r = 0,g = 0,b = 0;
1510 int r1 = 0,g1 = 0,b1 = 0;
1511 if((index < imgsize)&(index+1 < imgsize)){
1512 r = image->comps[0].data[index];
1513 g = image->comps[1].data[index];
1514 b = image->comps[2].data[index];
1515 r1 = image->comps[0].data[index+1];
1516 g1 = image->comps[1].data[index+1];
1517 b1 = image->comps[2].data[index+1];
1518 if (image->comps[0].sgnd){
1526 dat8[i+0] = (r >> 4);
1527 dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f);
1529 dat8[i+3] = (b >> 4);
1530 dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1532 dat8[i+6] = (g1 >> 4);
1533 dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);
1541 for (i= last_i; i<ssize; i+=9) { // 12 bits per pixel
1542 int r = 0,g = 0,b = 0;
1543 int r1 = 0,g1 = 0,b1 = 0;
1544 if((index < imgsize)&(index+1 < imgsize)){
1545 r = image->comps[0].data[index];
1546 g = image->comps[1].data[index];
1547 b = image->comps[2].data[index];
1548 r1 = image->comps[0].data[index+1];
1549 g1 = image->comps[1].data[index+1];
1550 b1 = image->comps[2].data[index+1];
1551 if (image->comps[0].sgnd){
1559 dat8[i+0] = (r >> 4);
1560 if(i+1 <ssize) dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f); else break;
1561 if(i+2 <ssize) dat8[i+2] = g ; else break;
1562 if(i+3 <ssize) dat8[i+3] = (b >> 4); else break;
1563 if(i+4 <ssize) dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);else break;
1564 if(i+5 <ssize) dat8[i+5] = r1; else break;
1565 if(i+6 <ssize) dat8[i+6] = (g1 >> 4); else break;
1566 if(i+7 <ssize) dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);else break;
1567 if(i+8 <ssize) dat8[i+8] = b1; else break;
1573 }else if (image->comps[0].prec == 16){
1574 for (i=0 ; i<ssize-5 ; i+=6) { // 16 bits per pixel
1575 int r = 0,g = 0,b = 0;
1576 if(index < imgsize){
1577 r = image->comps[0].data[index];
1578 g = image->comps[1].data[index];
1579 b = image->comps[2].data[index];
1580 if (image->comps[0].sgnd){
1586 dat8[i+1] = (r >> 8);//MSB
1588 dat8[i+3] = (g >> 8);
1590 dat8[i+5] = (b >> 8);
1597 for (i=0 ; i<ssize ; i+=6) { // 16 bits per pixel
1598 int r = 0,g = 0,b = 0;
1599 if(index < imgsize){
1600 r = image->comps[0].data[index];
1601 g = image->comps[1].data[index];
1602 b = image->comps[2].data[index];
1603 if (image->comps[0].sgnd){
1609 if(i+1 <ssize) dat8[i+1] = (r >> 8);else break;//MSB
1610 if(i+2 <ssize) dat8[i+2] = g; else break;
1611 if(i+3 <ssize) dat8[i+3] = (g >> 8);else break;
1612 if(i+4 <ssize) dat8[i+4] = b; else break;
1613 if(i+5 <ssize) dat8[i+5] = (b >> 8);else break;
1620 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1621 fprintf(stderr,"Aborting\n");
1624 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1628 }else if (image->numcomps == 1){
1633 tif = TIFFOpen(outfile, "wb");
1635 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1639 width = image->comps[0].w;
1640 height = image->comps[0].h;
1641 imgsize = width * height;
1642 bps = image->comps[0].prec;
1645 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1646 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1647 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1648 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1649 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1650 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1651 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1652 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1654 /* Get a buffer for the data */
1655 strip_size = TIFFStripSize(tif);
1656 buf = _TIFFmalloc(strip_size);
1658 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1659 unsigned char *dat8;
1662 if (image->comps[0].prec == 8){
1663 for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
1664 if(index < imgsize){
1666 r = image->comps[0].data[index];
1667 if (image->comps[0].sgnd){
1675 }else if (image->comps[0].prec == 12){
1676 for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
1677 if(index < imgsize){
1679 r = image->comps[0].data[index];
1680 r1 = image->comps[0].data[index+1];
1681 if (image->comps[0].sgnd){
1685 dat8[i+0] = (r >> 4);
1686 dat8[i+1] = ((r & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1692 }else if (image->comps[0].prec == 16){
1693 for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
1694 if(index < imgsize){
1696 r = image->comps[0].data[index];
1697 if (image->comps[0].sgnd){
1707 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1708 fprintf(stderr,"Aborting\n");
1711 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1716 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1717 fprintf(stderr,"Aborting\n");
1723 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
1725 int subsampling_dx = parameters->subsampling_dx;
1726 int subsampling_dy = parameters->subsampling_dy;
1728 tiff_infoheader_t Info;
1732 int j, numcomps, w, h,index;
1733 OPJ_COLOR_SPACE color_space;
1734 opj_image_cmptparm_t cmptparm[3];
1735 opj_image_t * image = NULL;
1738 tif = TIFFOpen(filename, "r");
1741 fprintf(stderr, "Failed to open %s for reading\n", filename);
1745 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1746 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1747 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1748 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1749 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1751 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1752 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1756 if (Info.tiPhoto == 2) {
1762 color_space = CLRSPC_SRGB;
1763 /* initialize image components*/
1764 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1765 for(j = 0; j < numcomps; j++) {
1766 if (parameters->cp_cinema) {
1767 cmptparm[j].prec = 12;
1768 cmptparm[j].bpp = 12;
1770 cmptparm[j].prec = Info.tiBps;
1771 cmptparm[j].bpp = Info.tiBps;
1773 cmptparm[j].sgnd = 0;
1774 cmptparm[j].dx = subsampling_dx;
1775 cmptparm[j].dy = subsampling_dy;
1779 /* create the image*/
1780 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1786 /* set image offset and reference grid */
1787 image->x0 = parameters->image_offset_x0;
1788 image->y0 = parameters->image_offset_y0;
1789 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1790 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1792 buf = _TIFFmalloc(TIFFStripSize(tif));
1794 strip_size=TIFFStripSize(tif);
1796 imgsize = image->comps[0].w * image->comps[0].h ;
1797 /* Read the Image components*/
1798 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1799 unsigned char *dat8;
1801 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1804 if (Info.tiBps==12){
1805 for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
1806 if((index < imgsize)&(index+1 < imgsize)){
1807 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
1808 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1809 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
1810 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1811 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
1812 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1818 else if( Info.tiBps==16){
1819 for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
1820 if(index < imgsize){
1821 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
1822 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
1823 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
1824 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1825 image->comps[0].data[index] = (image->comps[0].data[index] + 0x08) >> 4 ;
1826 image->comps[1].data[index] = (image->comps[1].data[index] + 0x08) >> 4 ;
1827 image->comps[2].data[index] = (image->comps[2].data[index] + 0x08) >> 4 ;
1834 else if ( Info.tiBps==8){
1835 for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
1836 if(index < imgsize){
1837 image->comps[0].data[index] = dat8[i+0];// R
1838 image->comps[1].data[index] = dat8[i+1];// G
1839 image->comps[2].data[index] = dat8[i+2];// B
1840 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1841 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
1842 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
1843 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
1851 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1852 fprintf(stderr,"Aborting\n");
1859 }else if(Info.tiPhoto == 1) {
1865 color_space = CLRSPC_GRAY;
1866 /* initialize image components*/
1867 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1868 cmptparm[0].prec = Info.tiBps;
1869 cmptparm[0].bpp = Info.tiBps;
1870 cmptparm[0].sgnd = 0;
1871 cmptparm[0].dx = subsampling_dx;
1872 cmptparm[0].dy = subsampling_dy;
1876 /* create the image*/
1877 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1882 /* set image offset and reference grid */
1883 image->x0 = parameters->image_offset_x0;
1884 image->y0 = parameters->image_offset_y0;
1885 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1886 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1888 buf = _TIFFmalloc(TIFFStripSize(tif));
1890 strip_size = TIFFStripSize(tif);
1892 imgsize = image->comps[0].w * image->comps[0].h ;
1893 /* Read the Image components*/
1894 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1895 unsigned char *dat8;
1897 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1900 if (Info.tiBps==12){
1901 for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
1902 if(index < imgsize){
1903 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
1904 image->comps[0].data[index+1] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1910 else if( Info.tiBps==16){
1911 for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
1912 if(index < imgsize){
1913 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
1919 else if ( Info.tiBps==8){
1920 for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
1921 if(index < imgsize){
1922 image->comps[0].data[index] = dat8[i+0];
1929 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1930 fprintf(stderr,"Aborting\n");
1938 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1939 fprintf(stderr,"Aborting\n");
1945 /* -->> -->> -->> -->>
1949 <<-- <<-- <<-- <<-- */
1951 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
1952 int subsampling_dx = parameters->subsampling_dx;
1953 int subsampling_dy = parameters->subsampling_dy;
1956 int i, compno, numcomps, w, h;
1957 OPJ_COLOR_SPACE color_space;
1958 opj_image_cmptparm_t *cmptparm;
1959 opj_image_t * image = NULL;
1962 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
1964 fprintf(stderr,"\nError: invalid raw image parameters\n");
1965 fprintf(stderr,"Please use the Format option -F:\n");
1966 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
1967 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
1968 fprintf(stderr,"Aborting\n");
1972 f = fopen(filename, "rb");
1974 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1975 fprintf(stderr,"Aborting\n");
1978 numcomps = raw_cp->rawComp;
1979 color_space = CLRSPC_SRGB;
1980 w = raw_cp->rawWidth;
1981 h = raw_cp->rawHeight;
1982 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
1984 /* initialize image components */
1985 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1986 for(i = 0; i < numcomps; i++) {
1987 cmptparm[i].prec = raw_cp->rawBitDepth;
1988 cmptparm[i].bpp = raw_cp->rawBitDepth;
1989 cmptparm[i].sgnd = raw_cp->rawSigned;
1990 cmptparm[i].dx = subsampling_dx;
1991 cmptparm[i].dy = subsampling_dy;
1995 /* create the image */
1996 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2001 /* set image offset and reference grid */
2002 image->x0 = parameters->image_offset_x0;
2003 image->y0 = parameters->image_offset_y0;
2004 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2005 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2007 if(raw_cp->rawBitDepth <= 8)
2009 unsigned char value = 0;
2010 for(compno = 0; compno < numcomps; compno++) {
2011 for (i = 0; i < w * h; i++) {
2012 if (!fread(&value, 1, 1, f)) {
2013 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2016 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2020 else if(raw_cp->rawBitDepth <= 16)
2022 unsigned short value;
2023 for(compno = 0; compno < numcomps; compno++) {
2024 for (i = 0; i < w * h; i++) {
2026 if (!fread(&temp, 1, 1, f)) {
2027 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2031 if (!fread(&temp, 1, 1, f)) {
2032 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2036 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2041 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2045 if (fread(&ch, 1, 1, f)) {
2046 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2053 int imagetoraw(opj_image_t * image, const char *outfile)
2055 FILE *rawFile = NULL;
2061 if((image->numcomps * image->x1 * image->y1) == 0)
2063 fprintf(stderr,"\nError: invalid raw image parameters\n");
2067 rawFile = fopen(outfile, "wb");
2069 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2073 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2075 for(compno = 0; compno < image->numcomps; compno++)
2077 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2078 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2080 w = image->comps[compno].w;
2081 h = image->comps[compno].h;
2083 if(image->comps[compno].prec <= 8)
2085 if(image->comps[compno].sgnd == 1)
2088 int mask = (1 << image->comps[compno].prec) - 1;
2089 ptr = image->comps[compno].data;
2090 for (line = 0; line < h; line++) {
2091 for(row = 0; row < w; row++) {
2092 curr = (signed char) (*ptr & mask);
2093 fwrite(&curr, sizeof(signed char), 1, rawFile);
2098 else if(image->comps[compno].sgnd == 0)
2101 int mask = (1 << image->comps[compno].prec) - 1;
2102 ptr = image->comps[compno].data;
2103 for (line = 0; line < h; line++) {
2104 for(row = 0; row < w; row++) {
2105 curr = (unsigned char) (*ptr & mask);
2106 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2112 else if(image->comps[compno].prec <= 16)
2114 if(image->comps[compno].sgnd == 1)
2116 signed short int curr;
2117 int mask = (1 << image->comps[compno].prec) - 1;
2118 ptr = image->comps[compno].data;
2119 for (line = 0; line < h; line++) {
2120 for(row = 0; row < w; row++) {
2122 curr = (signed short int) (*ptr & mask);
2124 fwrite(&temp, 1, 1, rawFile);
2126 fwrite(&temp, 1, 1, rawFile);
2131 else if(image->comps[compno].sgnd == 0)
2133 unsigned short int curr;
2134 int mask = (1 << image->comps[compno].prec) - 1;
2135 ptr = image->comps[compno].data;
2136 for (line = 0; line < h; line++) {
2137 for(row = 0; row < w; row++) {
2139 curr = (unsigned short int) (*ptr & mask);
2141 fwrite(&temp, 1, 1, rawFile);
2143 fwrite(&temp, 1, 1, rawFile);
2149 else if (image->comps[compno].prec <= 32)
2151 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2156 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);