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.
32 #include "opj_config.h"
39 #define BYTE_ORDER LITTLE_ENDIAN
41 #include <machine/endian.h>
48 #endif /* HAVE_LIBTIFF */
53 #endif /* HAVE_LIBPNG */
55 #include "../libopenjpeg/openjpeg.h"
59 * Get logarithm of an integer and round downwards.
63 static int int_floorlog2(int a) {
65 for (l = 0; a > 1; l++) {
71 /* -->> -->> -->> -->>
75 <<-- <<-- <<-- <<-- */
77 // TGA header definition.
78 #pragma pack(push,1) // Pack structure byte aligned
79 typedef struct tga_header
81 unsigned char id_length; /* Image id field length */
82 unsigned char colour_map_type; /* Colour map type */
83 unsigned char image_type; /* Image type */
85 ** Colour map specification
87 unsigned short colour_map_index; /* First entry index */
88 unsigned short colour_map_length; /* Colour map length */
89 unsigned char colour_map_entry_size; /* Colour map entry size */
91 ** Image specification
93 unsigned short x_origin; /* x origin of image */
94 unsigned short y_origin; /* u origin of image */
95 unsigned short image_width; /* Image width */
96 unsigned short image_height; /* Image height */
97 unsigned char pixel_depth; /* Pixel depth */
98 unsigned char image_desc; /* Image descriptor */
100 #pragma pack(pop) // Return to normal structure packing alignment.
102 int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
103 unsigned int *width, unsigned int *height, int *flip_image)
108 if (!bits_per_pixel || !width || !height || !flip_image)
112 fread((unsigned char*)&tga, sizeof(tga_header), 1, fp);
114 *bits_per_pixel = tga.pixel_depth;
116 *width = tga.image_width;
117 *height = tga.image_height ;
119 // Ignore tga identifier, if present ...
122 unsigned char *id = (unsigned char *) malloc(tga.id_length);
123 fread(id, tga.id_length, 1, fp);
127 // Test for compressed formats ... not yet supported ...
128 // Note :- 9 - RLE encoded palettized.
129 // 10 - RLE encoded RGB.
130 if (tga.image_type > 8)
132 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
136 *flip_image = !(tga.image_desc & 32);
138 // Palettized formats are not yet supported, skip over the palette, if present ...
139 palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
143 fprintf(stderr, "File contains a palette - not yet supported.");
144 fseek(fp, palette_size, SEEK_CUR);
149 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
154 if (!bits_per_pixel || !width || !height)
157 memset(&tga, 0, sizeof(tga_header));
159 tga.pixel_depth = bits_per_pixel;
160 tga.image_width = width;
161 tga.image_height = height;
162 tga.image_type = 2; // Uncompressed.
163 tga.image_desc = 8; // 8 bits per component.
166 tga.image_desc |= 32;
169 fwrite((unsigned char*)&tga, sizeof(tga_header), 1, fp);
174 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
177 unsigned int image_width, image_height, pixel_bit_depth;
180 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
182 OPJ_COLOR_SPACE color_space;
185 int subsampling_dx, subsampling_dy;
188 f = fopen(filename, "rb");
190 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
194 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
197 // We currently only support 24 & 32 bit tga's ...
198 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
201 /* initialize image components */
202 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
204 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha.
205 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
208 color_space = CLRSPC_GRAY;
209 numcomps = save_alpha ? 2 : 1;
212 numcomps = save_alpha ? 4 : 3;
213 color_space = CLRSPC_SRGB;
216 subsampling_dx = parameters->subsampling_dx;
217 subsampling_dy = parameters->subsampling_dy;
219 for (i = 0; i < numcomps; i++) {
220 cmptparm[i].prec = 8;
222 cmptparm[i].sgnd = 0;
223 cmptparm[i].dx = subsampling_dx;
224 cmptparm[i].dy = subsampling_dy;
225 cmptparm[i].w = image_width;
226 cmptparm[i].h = image_height;
229 /* create the image */
230 image = opj_image_create(numcomps, &cmptparm[0], color_space);
235 /* set image offset and reference grid */
236 image->x0 = parameters->image_offset_x0;
237 image->y0 = parameters->image_offset_y0;
238 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
239 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
242 for (y=0; y < image_height; y++)
247 index = (image_height-y-1)*image_width;
249 index = y*image_width;
253 for (x=0;x<image_width;x++)
260 image->comps[0].data[index]=r;
261 image->comps[1].data[index]=g;
262 image->comps[2].data[index]=b;
266 else if (numcomps==4)
268 for (x=0;x<image_width;x++)
270 unsigned char r,g,b,a;
276 image->comps[0].data[index]=r;
277 image->comps[1].data[index]=g;
278 image->comps[2].data[index]=b;
279 image->comps[3].data[index]=a;
284 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
290 int imagetotga(opj_image_t * image, const char *outfile) {
291 int width, height, bpp, x, y;
294 unsigned int alpha_channel;
300 fdest = fopen(outfile, "wb");
302 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
306 for (i = 0; i < image->numcomps-1; i++) {
307 if ((image->comps[0].dx != image->comps[i+1].dx)
308 ||(image->comps[0].dy != image->comps[i+1].dy)
309 ||(image->comps[0].prec != image->comps[i+1].prec)) {
310 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
315 width = image->comps[0].w;
316 height = image->comps[0].h;
318 // Mono with alpha, or RGB with alpha.
319 write_alpha = (image->numcomps==2) || (image->numcomps==4);
322 bpp = write_alpha ? 32 : 24;
323 if (!tga_writeheader(fdest, bpp, width , height, true))
326 alpha_channel = image->numcomps-1;
328 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
330 for (y=0; y < height; y++) {
331 unsigned int index=y*width;
333 for (x=0; x < width; x++, index++) {
334 r = (float)(image->comps[0].data[index]);
336 if (image->numcomps>2) {
337 g = (float)(image->comps[1].data[index]);
338 b = (float)(image->comps[2].data[index]);
340 else {// Greyscale ...
345 // TGA format writes BGR ...
346 value = (unsigned char)(b*scale);
347 fwrite(&value,1,1,fdest);
349 value = (unsigned char)(g*scale);
350 fwrite(&value,1,1,fdest);
352 value = (unsigned char)(r*scale);
353 fwrite(&value,1,1,fdest);
356 a = (float)(image->comps[alpha_channel].data[index]);
357 value = (unsigned char)(a*scale);
358 fwrite(&value,1,1,fdest);
366 /* -->> -->> -->> -->>
370 <<-- <<-- <<-- <<-- */
372 /* WORD defines a two byte word */
373 typedef unsigned short int WORD;
375 /* DWORD defines a four byte word */
376 typedef unsigned long int DWORD;
379 WORD bfType; /* 'BM' for Bitmap (19776) */
380 DWORD bfSize; /* Size of the file */
381 WORD bfReserved1; /* Reserved : 0 */
382 WORD bfReserved2; /* Reserved : 0 */
383 DWORD bfOffBits; /* Offset */
384 } BITMAPFILEHEADER_t;
387 DWORD biSize; /* Size of the structure in bytes */
388 DWORD biWidth; /* Width of the image in pixels */
389 DWORD biHeight; /* Heigth of the image in pixels */
390 WORD biPlanes; /* 1 */
391 WORD biBitCount; /* Number of color bits by pixels */
392 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
393 DWORD biSizeImage; /* Size of the image in bytes */
394 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
395 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
396 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
397 DWORD biClrImportant; /* Number of important color (0: ALL) */
398 } BITMAPINFOHEADER_t;
400 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
401 int subsampling_dx = parameters->subsampling_dx;
402 int subsampling_dy = parameters->subsampling_dy;
404 int i, numcomps, w, h;
405 OPJ_COLOR_SPACE color_space;
406 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
407 opj_image_t * image = NULL;
410 BITMAPFILEHEADER_t File_h;
411 BITMAPINFOHEADER_t Info_h;
413 unsigned char *table_R, *table_G, *table_B;
414 unsigned int j, PAD = 0;
417 int gray_scale = 1, not_end_file = 1;
419 unsigned int line = 0, col = 0;
423 IN = fopen(filename, "rb");
425 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
429 File_h.bfType = getc(IN);
430 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
432 if (File_h.bfType != 19778) {
433 fprintf(stderr,"Error, not a BMP file!\n");
438 File_h.bfSize = getc(IN);
439 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
440 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
441 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
443 File_h.bfReserved1 = getc(IN);
444 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
446 File_h.bfReserved2 = getc(IN);
447 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
449 File_h.bfOffBits = getc(IN);
450 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
451 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
452 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
457 Info_h.biSize = getc(IN);
458 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
459 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
460 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
462 Info_h.biWidth = getc(IN);
463 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
464 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
465 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
468 Info_h.biHeight = getc(IN);
469 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
470 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
471 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
474 Info_h.biPlanes = getc(IN);
475 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
477 Info_h.biBitCount = getc(IN);
478 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
480 Info_h.biCompression = getc(IN);
481 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
482 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
483 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
485 Info_h.biSizeImage = getc(IN);
486 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
487 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
488 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
490 Info_h.biXpelsPerMeter = getc(IN);
491 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
492 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
493 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
495 Info_h.biYpelsPerMeter = getc(IN);
496 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
497 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
498 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
500 Info_h.biClrUsed = getc(IN);
501 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
502 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
503 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
505 Info_h.biClrImportant = getc(IN);
506 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
507 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
508 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
510 /* Read the data and store them in the OUT file */
512 if (Info_h.biBitCount == 24) {
514 color_space = CLRSPC_SRGB;
515 /* initialize image components */
516 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
517 for(i = 0; i < numcomps; i++) {
518 cmptparm[i].prec = 8;
520 cmptparm[i].sgnd = 0;
521 cmptparm[i].dx = subsampling_dx;
522 cmptparm[i].dy = subsampling_dy;
526 /* create the image */
527 image = opj_image_create(numcomps, &cmptparm[0], color_space);
533 /* set image offset and reference grid */
534 image->x0 = parameters->image_offset_x0;
535 image->y0 = parameters->image_offset_y0;
536 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
537 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
541 /* Place the cursor at the beginning of the image information */
542 fseek(IN, 0, SEEK_SET);
543 fseek(IN, File_h.bfOffBits, SEEK_SET);
548 /* PAD = 4 - (3 * W) % 4; */
549 /* PAD = (PAD == 4) ? 0 : PAD; */
550 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
552 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
554 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
558 for(y = 0; y < (int)H; y++) {
559 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
560 for(x = 0; x < (int)W; x++) {
561 unsigned char *pixel = &scanline[3 * x];
562 image->comps[0].data[index] = pixel[2]; /* R */
563 image->comps[1].data[index] = pixel[1]; /* G */
564 image->comps[2].data[index] = pixel[0]; /* B */
571 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
572 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
573 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
574 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
576 for (j = 0; j < Info_h.biClrUsed; j++) {
577 table_B[j] = getc(IN);
578 table_G[j] = getc(IN);
579 table_R[j] = getc(IN);
581 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
585 /* Place the cursor at the beginning of the image information */
586 fseek(IN, 0, SEEK_SET);
587 fseek(IN, File_h.bfOffBits, SEEK_SET);
591 if (Info_h.biWidth % 2)
594 numcomps = gray_scale ? 1 : 3;
595 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
596 /* initialize image components */
597 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
598 for(i = 0; i < numcomps; i++) {
599 cmptparm[i].prec = 8;
601 cmptparm[i].sgnd = 0;
602 cmptparm[i].dx = subsampling_dx;
603 cmptparm[i].dy = subsampling_dy;
607 /* create the image */
608 image = opj_image_create(numcomps, &cmptparm[0], color_space);
614 /* set image offset and reference grid */
615 image->x0 = parameters->image_offset_x0;
616 image->y0 = parameters->image_offset_y0;
617 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
618 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
622 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
624 fread(RGB, sizeof(unsigned char), W * H, IN);
627 for (j = 0; j < W * H; j++) {
628 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
629 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
636 for (j = 0; j < W * H; j++) {
637 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
638 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
639 image->comps[0].data[index] = table_R[pixel_index];
640 image->comps[1].data[index] = table_G[pixel_index];
641 image->comps[2].data[index] = table_B[pixel_index];
650 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
651 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
652 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
653 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
655 for (j = 0; j < Info_h.biClrUsed; j++) {
656 table_B[j] = getc(IN);
657 table_G[j] = getc(IN);
658 table_R[j] = getc(IN);
660 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
664 numcomps = gray_scale ? 1 : 3;
665 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
666 /* initialize image components */
667 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
668 for(i = 0; i < numcomps; i++) {
669 cmptparm[i].prec = 8;
671 cmptparm[i].sgnd = 0;
672 cmptparm[i].dx = subsampling_dx;
673 cmptparm[i].dy = subsampling_dy;
677 /* create the image */
678 image = opj_image_create(numcomps, &cmptparm[0], color_space);
684 /* set image offset and reference grid */
685 image->x0 = parameters->image_offset_x0;
686 image->y0 = parameters->image_offset_y0;
687 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
688 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
692 /* Place the cursor at the beginning of the image information */
693 fseek(IN, 0, SEEK_SET);
694 fseek(IN, File_h.bfOffBits, SEEK_SET);
696 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
698 while (not_end_file) {
702 for (i = 0; i < (int) v; i++) {
703 RGB[line * Info_h.biWidth + col] = v2;
718 fprintf(stderr,"No Delta supported\n");
719 opj_image_destroy(image);
723 for (i = 0; i < v; i++) {
725 RGB[line * Info_h.biWidth + col] = v2;
736 for (line = 0; line < Info_h.biHeight; line++) {
737 for (col = 0; col < Info_h.biWidth; col++) {
738 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
744 for (line = 0; line < Info_h.biHeight; line++) {
745 for (col = 0; col < Info_h.biWidth; col++) {
746 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
747 image->comps[0].data[index] = table_R[pixel_index];
748 image->comps[1].data[index] = table_G[pixel_index];
749 image->comps[2].data[index] = table_B[pixel_index];
760 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
768 int imagetobmp(opj_image_t * image, const char *outfile) {
772 int adjustR, adjustG, adjustB;
774 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
775 && image->comps[1].dx == image->comps[2].dx
776 && image->comps[0].dy == image->comps[1].dy
777 && image->comps[1].dy == image->comps[2].dy
778 && image->comps[0].prec == image->comps[1].prec
779 && image->comps[1].prec == image->comps[2].prec) {
781 /* -->> -->> -->> -->>
783 <<-- <<-- <<-- <<-- */
785 fdest = fopen(outfile, "wb");
787 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
791 w = image->comps[0].w;
792 h = image->comps[0].h;
794 fprintf(fdest, "BM");
798 fprintf(fdest, "%c%c%c%c",
799 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
800 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
801 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
802 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
803 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
804 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
808 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
809 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
810 (unsigned char) ((w) >> 8) & 0xff,
811 (unsigned char) ((w) >> 16) & 0xff,
812 (unsigned char) ((w) >> 24) & 0xff);
813 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
814 (unsigned char) ((h) >> 8) & 0xff,
815 (unsigned char) ((h) >> 16) & 0xff,
816 (unsigned char) ((h) >> 24) & 0xff);
817 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
818 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
819 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
820 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
821 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
822 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
823 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
824 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 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", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
827 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
829 if (image->comps[0].prec > 8) {
830 adjustR = image->comps[0].prec - 8;
831 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
835 if (image->comps[1].prec > 8) {
836 adjustG = image->comps[1].prec - 8;
837 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
841 if (image->comps[2].prec > 8) {
842 adjustB = image->comps[2].prec - 8;
843 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
848 for (i = 0; i < w * h; i++) {
849 unsigned char rc, gc, bc;
852 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
853 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
854 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
855 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
856 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
857 gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
858 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
859 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
860 bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
862 fprintf(fdest, "%c%c%c", bc, gc, rc);
864 if ((i + 1) % w == 0) {
865 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
866 fprintf(fdest, "%c", 0);
870 } else { /* Gray-scale */
872 /* -->> -->> -->> -->>
873 8 bits non code (Gray scale)
874 <<-- <<-- <<-- <<-- */
876 fdest = fopen(outfile, "wb");
877 w = image->comps[0].w;
878 h = image->comps[0].h;
880 fprintf(fdest, "BM");
884 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
885 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
886 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
887 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
888 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
889 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
890 ((54 + 1024) >> 16) & 0xff,
891 ((54 + 1024) >> 24) & 0xff);
895 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
896 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
897 (unsigned char) ((w) >> 8) & 0xff,
898 (unsigned char) ((w) >> 16) & 0xff,
899 (unsigned char) ((w) >> 24) & 0xff);
900 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
901 (unsigned char) ((h) >> 8) & 0xff,
902 (unsigned char) ((h) >> 16) & 0xff,
903 (unsigned char) ((h) >> 24) & 0xff);
904 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
905 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
906 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
907 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
908 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
909 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
910 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
911 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
912 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
913 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
914 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
916 if (image->comps[0].prec > 8) {
917 adjustR = image->comps[0].prec - 8;
918 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
922 for (i = 0; i < 256; i++) {
923 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
926 for (i = 0; i < w * h; i++) {
930 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
931 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
932 rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
934 fprintf(fdest, "%c", rc);
936 if ((i + 1) % w == 0) {
937 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
938 fprintf(fdest, "%c", 0);
947 /* -->> -->> -->> -->>
951 <<-- <<-- <<-- <<-- */
954 unsigned char readuchar(FILE * f)
961 unsigned short readushort(FILE * f, int bigendian)
963 unsigned char c1, c2;
967 return (c1 << 8) + c2;
969 return (c2 << 8) + c1;
972 unsigned int readuint(FILE * f, int bigendian)
974 unsigned char c1, c2, c3, c4;
980 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
982 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
985 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
988 int i, numcomps, max;
989 OPJ_COLOR_SPACE color_space;
990 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
991 opj_image_t * image = NULL;
993 char endian1,endian2,sign;
998 opj_image_comp_t *comp = NULL;
1001 color_space = CLRSPC_GRAY;
1003 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1007 f = fopen(filename, "rb");
1009 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1013 fseek(f, 0, SEEK_SET);
1014 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
1018 while (signtmp[i]!='\0') {
1019 if (signtmp[i]=='-') sign='-';
1024 if (endian1=='M' && endian2=='L') {
1026 } else if (endian2=='M' && endian1=='L') {
1029 fprintf(stderr, "Bad pgx header, please check input file\n");
1033 /* initialize image component */
1035 cmptparm.x0 = parameters->image_offset_x0;
1036 cmptparm.y0 = parameters->image_offset_y0;
1037 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1038 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1045 cmptparm.prec = prec;
1046 cmptparm.bpp = prec;
1047 cmptparm.dx = parameters->subsampling_dx;
1048 cmptparm.dy = parameters->subsampling_dy;
1050 /* create the image */
1051 image = opj_image_create(numcomps, &cmptparm, color_space);
1056 /* set image offset and reference grid */
1057 image->x0 = cmptparm.x0;
1058 image->y0 = cmptparm.x0;
1059 image->x1 = cmptparm.w;
1060 image->y1 = cmptparm.h;
1062 /* set image data */
1064 comp = &image->comps[0];
1066 for (i = 0; i < w * h; i++) {
1068 if (comp->prec <= 8) {
1072 v = (char) readuchar(f);
1074 } else if (comp->prec <= 16) {
1076 v = readushort(f, bigendian);
1078 v = (short) readushort(f, bigendian);
1082 v = readuint(f, bigendian);
1084 v = (int) readuint(f, bigendian);
1092 comp->bpp = int_floorlog2(max) + 1;
1097 int imagetopgx(opj_image_t * image, const char *outfile) {
1102 for (compno = 0; compno < image->numcomps; compno++) {
1103 opj_image_comp_t *comp = &image->comps[compno];
1104 char bname[256]; /* buffer for name */
1105 char *name = bname; /* pointer */
1107 const size_t olen = strlen(outfile);
1108 const size_t dotpos = olen - 4;
1109 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1110 if( outfile[dotpos] != '.' ) {
1111 /* `pgx` was recognized but there is no dot at expected position */
1112 fprintf(stderr, "ERROR -> Impossible happen." );
1116 name = (char*)malloc(total+1);
1118 strncpy(name, outfile, dotpos);
1119 if (image->numcomps > 1) {
1120 sprintf(name+dotpos, "-%d.pgx", compno);
1122 strcpy(name+dotpos, ".pgx");
1124 fdest = fopen(name, "wb");
1126 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1129 /* dont need name anymore */
1134 w = image->comps[compno].w;
1135 h = image->comps[compno].h;
1137 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1138 if (comp->prec <= 8) {
1140 } else if (comp->prec <= 16) {
1145 for (i = 0; i < w * h; i++) {
1146 int v = image->comps[compno].data[i];
1147 for (j = nbytes - 1; j >= 0; j--) {
1148 char byte = (char) (v >> (j * 8));
1149 fwrite(&byte, 1, 1, fdest);
1158 /* -->> -->> -->> -->>
1162 <<-- <<-- <<-- <<-- */
1164 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1165 int subsampling_dx = parameters->subsampling_dx;
1166 int subsampling_dy = parameters->subsampling_dy;
1169 int i, compno, numcomps, w, h;
1170 OPJ_COLOR_SPACE color_space;
1171 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
1172 opj_image_t * image = NULL;
1175 f = fopen(filename, "rb");
1177 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1181 if (fgetc(f) != 'P')
1186 case '2': /* greyscale image type */
1189 color_space = CLRSPC_GRAY;
1192 case '3': /* RGB image type */
1195 color_space = CLRSPC_SRGB;
1206 while(fgetc(f) == '#') while(fgetc(f) != '\n');
1208 fseek(f, -1, SEEK_CUR);
1209 fscanf(f, "%d %d\n255", &w, &h);
1210 fgetc(f); /* <cr><lf> */
1212 /* initialize image components */
1213 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1214 for(i = 0; i < numcomps; i++) {
1215 cmptparm[i].prec = 8;
1216 cmptparm[i].bpp = 8;
1217 cmptparm[i].sgnd = 0;
1218 cmptparm[i].dx = subsampling_dx;
1219 cmptparm[i].dy = subsampling_dy;
1223 /* create the image */
1224 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1230 /* set image offset and reference grid */
1231 image->x0 = parameters->image_offset_x0;
1232 image->y0 = parameters->image_offset_y0;
1233 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1234 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1236 /* set image data */
1238 if ((value == '2') || (value == '3')) { /* ASCII */
1239 for (i = 0; i < w * h; i++) {
1240 for(compno = 0; compno < numcomps; compno++) {
1241 unsigned int index = 0;
1242 fscanf(f, "%u", &index);
1243 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1244 image->comps[compno].data[i] = index;
1247 } else if ((value == '5') || (value == '6')) { /* BINARY */
1248 for (i = 0; i < w * h; i++) {
1249 for(compno = 0; compno < numcomps; compno++) {
1250 unsigned char index = 0;
1251 fread(&index, 1, 1, f);
1252 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1253 image->comps[compno].data[i] = index;
1263 int imagetopnm(opj_image_t * image, const char *outfile)
1265 int *red, *green, *blue, *alpha;
1267 int i, compno, ncomp;
1268 int adjustR, adjustG, adjustB;
1269 int fails, is16, force16, want_gray, has_alpha;
1270 int prec, opj_prec, ushift, dshift, v;
1272 const char *tmp = outfile;
1275 if((opj_prec = image->comps[0].prec) > 16)
1277 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1278 "\n\t: refused.\n",__FILE__,__LINE__,opj_prec);
1282 is16 = force16 = ushift = dshift = has_alpha = 0; fails = 1;
1284 if(prec > 8 && prec < 16)
1286 prec = 16; force16 = 1;
1289 while (*tmp) ++tmp; tmp -= 2;
1290 want_gray = (*tmp == 'g' || *tmp == 'G');
1291 ncomp = image->numcomps;
1294 && image->comps[0].dx == image->comps[1].dx
1295 && image->comps[1].dx == image->comps[2].dx
1296 && image->comps[0].dy == image->comps[1].dy
1297 && image->comps[1].dy == image->comps[2].dy
1298 && image->comps[0].prec == image->comps[1].prec
1299 && image->comps[1].prec == image->comps[2].prec
1303 fdest = fopen(outfile, "wb");
1307 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1310 wr = image->comps[0].w; hr = image->comps[0].h;
1312 max = (1<<prec) - 1; has_alpha = (ncomp == 4);
1314 is16 = (prec == 16);
1316 red = image->comps[0].data;
1317 green = image->comps[1].data;
1318 blue = image->comps[2].data;
1322 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH 4\n"
1323 "MAXVAL %d\nTUPLTYPE RGB_ALPHA\nENDHDR\n", opj_version(),
1325 alpha = image->comps[3].data;
1329 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1330 opj_version(), wr, hr, max);
1335 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
1337 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1338 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1339 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1341 for(i = 0; i < wr * hr; ++i)
1345 /* MSB first: 'man ppm' */
1347 v = *red + adjustR; ++red;
1349 if(force16) { v = (v<<ushift) + (v>>dshift); }
1351 #if BYTE_ORDER == LITTLE_ENDIAN
1352 fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
1354 fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
1356 v = *green + adjustG; ++green;
1358 if(force16) { v = (v<<ushift) + (v>>dshift); }
1360 #if BYTE_ORDER == LITTLE_ENDIAN
1361 fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
1363 fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
1366 v = *blue + adjustB; ++blue;
1368 if(force16) { v = (v<<ushift) + (v>>dshift); }
1370 #if BYTE_ORDER == LITTLE_ENDIAN
1371 fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
1373 fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
1380 if(force16) { v = (v<<ushift) + (v>>dshift); }
1382 #if BYTE_ORDER == LITTLE_ENDIAN
1383 fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
1385 fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
1393 fprintf(fdest, "%c%c%c",(unsigned char)*red++, (unsigned char)*green++,
1394 (unsigned char)*blue++);
1397 fprintf(fdest, "%c", (unsigned char)*alpha++);
1401 fclose(fdest); return 0;
1406 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
1409 if(want_gray) ncomp = 1;
1410 //FIXME: with[out] alpha ?
1411 has_alpha = 0; alpha = NULL;
1413 if (image->numcomps > ncomp)
1415 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1416 fprintf(stderr," is written to the file\n");
1418 destname = (char*)malloc(strlen(outfile) + 8);
1420 for (compno = 0; compno < ncomp; compno++)
1423 sprintf(destname, "%d.%s", compno, outfile);
1425 sprintf(destname, "%s", outfile);
1427 fdest = fopen(destname, "wb");
1430 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1434 wr = image->comps[compno].w; hr = image->comps[compno].h;
1436 max = (1<<prec) - 1;
1438 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
1439 opj_version(), wr, hr, max);
1441 red = image->comps[compno].data;
1443 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1447 /* MSB first: 'man ppm' */
1449 for (i = 0; i < wr * hr; i++)
1452 v = *red + adjustR; ++red;
1454 if(force16) { v = (v<<ushift) + (v>>dshift); }
1456 #if BYTE_ORDER == LITTLE_ENDIAN
1457 fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
1459 fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
1466 if(force16) { v = (v<<ushift) + (v>>dshift); }
1468 #if BYTE_ORDER == LITTLE_ENDIAN
1469 fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
1471 fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
1477 else /* prec <= 8 */
1479 for(i = 0; i < wr * hr; ++i)
1481 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
1492 /* -->> -->> -->> -->>
1496 <<-- <<-- <<-- <<-- */
1498 typedef struct tiff_infoheader{
1499 DWORD tiWidth; // Width of Image in pixel
1500 DWORD tiHeight; // Height of Image in pixel
1501 DWORD tiPhoto; // Photometric
1502 WORD tiBps; // Bits per sample
1503 WORD tiSf; // Sample Format
1504 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1505 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1508 int imagetotif(opj_image_t * image, const char *outfile) {
1509 int width, height, imgsize;
1510 int bps,index,adjust = 0;
1511 unsigned int last_i=0;
1517 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1518 && image->comps[1].dx == image->comps[2].dx
1519 && image->comps[0].dy == image->comps[1].dy
1520 && image->comps[1].dy == image->comps[2].dy
1521 && image->comps[0].prec == image->comps[1].prec
1522 && image->comps[1].prec == image->comps[2].prec) {
1528 tif = TIFFOpen(outfile, "wb");
1530 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1534 width = image->comps[0].w;
1535 height = image->comps[0].h;
1536 imgsize = width * height ;
1537 bps = image->comps[0].prec;
1539 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1540 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1541 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1542 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1543 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1544 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1545 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1546 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1548 /* Get a buffer for the data */
1549 strip_size=TIFFStripSize(tif);
1550 buf = _TIFFmalloc(strip_size);
1552 adjust = image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0;
1553 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1554 unsigned char *dat8;
1556 ssize = TIFFStripSize(tif);
1557 dat8 = (unsigned char*)buf;
1558 if (image->comps[0].prec == 8){
1559 for (i=0; i<ssize-2; i+=3) { // 8 bits per pixel
1560 int r = 0,g = 0,b = 0;
1561 if(index < imgsize){
1562 r = image->comps[0].data[index];
1563 g = image->comps[1].data[index];
1564 b = image->comps[2].data[index];
1565 if (image->comps[0].sgnd){
1570 dat8[i+0] = r ; // R
1571 dat8[i+1] = g ; // G
1572 dat8[i+2] = b ; // B
1579 for (i=last_i; i<ssize; i+=3) { // 8 bits per pixel
1580 int r = 0,g = 0,b = 0;
1581 if(index < imgsize){
1582 r = image->comps[0].data[index];
1583 g = image->comps[1].data[index];
1584 b = image->comps[2].data[index];
1585 if (image->comps[0].sgnd){
1590 dat8[i+0] = r ; // R
1591 if(i+1 <ssize) dat8[i+1] = g ; else break;// G
1592 if(i+2 <ssize) dat8[i+2] = b ; else break;// B
1598 }else if (image->comps[0].prec == 12){
1599 for (i=0; i<ssize-8; i+=9) { // 12 bits per pixel
1600 int r = 0,g = 0,b = 0;
1601 int r1 = 0,g1 = 0,b1 = 0;
1602 if((index < imgsize)&(index+1 < imgsize)){
1603 r = image->comps[0].data[index];
1604 g = image->comps[1].data[index];
1605 b = image->comps[2].data[index];
1606 r1 = image->comps[0].data[index+1];
1607 g1 = image->comps[1].data[index+1];
1608 b1 = image->comps[2].data[index+1];
1609 if (image->comps[0].sgnd){
1617 dat8[i+0] = (r >> 4);
1618 dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f);
1620 dat8[i+3] = (b >> 4);
1621 dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1623 dat8[i+6] = (g1 >> 4);
1624 dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);
1632 for (i= last_i; i<ssize; i+=9) { // 12 bits per pixel
1633 int r = 0,g = 0,b = 0;
1634 int r1 = 0,g1 = 0,b1 = 0;
1635 if((index < imgsize)&(index+1 < imgsize)){
1636 r = image->comps[0].data[index];
1637 g = image->comps[1].data[index];
1638 b = image->comps[2].data[index];
1639 r1 = image->comps[0].data[index+1];
1640 g1 = image->comps[1].data[index+1];
1641 b1 = image->comps[2].data[index+1];
1642 if (image->comps[0].sgnd){
1650 dat8[i+0] = (r >> 4);
1651 if(i+1 <ssize) dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f); else break;
1652 if(i+2 <ssize) dat8[i+2] = g ; else break;
1653 if(i+3 <ssize) dat8[i+3] = (b >> 4); else break;
1654 if(i+4 <ssize) dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);else break;
1655 if(i+5 <ssize) dat8[i+5] = r1; else break;
1656 if(i+6 <ssize) dat8[i+6] = (g1 >> 4); else break;
1657 if(i+7 <ssize) dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);else break;
1658 if(i+8 <ssize) dat8[i+8] = b1; else break;
1664 }else if (image->comps[0].prec == 16){
1665 for (i=0 ; i<ssize-5 ; i+=6) { // 16 bits per pixel
1666 int r = 0,g = 0,b = 0;
1667 if(index < imgsize){
1668 r = image->comps[0].data[index];
1669 g = image->comps[1].data[index];
1670 b = image->comps[2].data[index];
1671 if (image->comps[0].sgnd){
1677 dat8[i+1] = (r >> 8);//MSB
1679 dat8[i+3] = (g >> 8);
1681 dat8[i+5] = (b >> 8);
1688 for (i=0 ; i<ssize ; i+=6) { // 16 bits per pixel
1689 int r = 0,g = 0,b = 0;
1690 if(index < imgsize){
1691 r = image->comps[0].data[index];
1692 g = image->comps[1].data[index];
1693 b = image->comps[2].data[index];
1694 if (image->comps[0].sgnd){
1700 if(i+1 <ssize) dat8[i+1] = (r >> 8);else break;//MSB
1701 if(i+2 <ssize) dat8[i+2] = g; else break;
1702 if(i+3 <ssize) dat8[i+3] = (g >> 8);else break;
1703 if(i+4 <ssize) dat8[i+4] = b; else break;
1704 if(i+5 <ssize) dat8[i+5] = (b >> 8);else break;
1711 fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1712 fprintf(stderr,"Aborting\n");
1715 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
1717 _TIFFfree((void*)buf);
1719 }else if (image->numcomps == 1){
1724 tif = TIFFOpen(outfile, "wb");
1726 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1730 width = image->comps[0].w;
1731 height = image->comps[0].h;
1732 imgsize = width * height;
1733 bps = image->comps[0].prec;
1736 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1737 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1738 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1739 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1740 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1741 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1742 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1743 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1745 /* Get a buffer for the data */
1746 strip_size = TIFFStripSize(tif);
1747 buf = _TIFFmalloc(strip_size);
1749 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1750 unsigned char *dat8;
1752 dat8 = (unsigned char*)buf;
1753 if (image->comps[0].prec == 8){
1754 for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
1755 if(index < imgsize){
1757 r = image->comps[0].data[index];
1758 if (image->comps[0].sgnd){
1766 }else if (image->comps[0].prec == 12){
1767 for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
1768 if(index < imgsize){
1770 r = image->comps[0].data[index];
1771 r1 = image->comps[0].data[index+1];
1772 if (image->comps[0].sgnd){
1776 dat8[i+0] = (r >> 4);
1777 dat8[i+1] = ((r & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1783 }else if (image->comps[0].prec == 16){
1784 for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
1785 if(index < imgsize){
1787 r = image->comps[0].data[index];
1788 if (image->comps[0].sgnd){
1798 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1799 fprintf(stderr,"Aborting\n");
1802 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
1807 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1808 fprintf(stderr,"Aborting\n");
1814 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
1816 int subsampling_dx = parameters->subsampling_dx;
1817 int subsampling_dy = parameters->subsampling_dy;
1819 tiff_infoheader_t Info;
1823 int j, numcomps, w, h,index;
1824 OPJ_COLOR_SPACE color_space;
1825 opj_image_cmptparm_t cmptparm[3];
1826 opj_image_t * image = NULL;
1829 tif = TIFFOpen(filename, "r");
1832 fprintf(stderr, "Failed to open %s for reading\n", filename);
1836 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1837 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1838 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1839 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1840 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1842 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1843 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1847 if (Info.tiPhoto == 2) {
1853 color_space = CLRSPC_SRGB;
1854 /* initialize image components*/
1855 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1856 for(j = 0; j < numcomps; j++) {
1857 if (parameters->cp_cinema) {
1858 cmptparm[j].prec = 12;
1859 cmptparm[j].bpp = 12;
1861 cmptparm[j].prec = Info.tiBps;
1862 cmptparm[j].bpp = Info.tiBps;
1864 cmptparm[j].sgnd = 0;
1865 cmptparm[j].dx = subsampling_dx;
1866 cmptparm[j].dy = subsampling_dy;
1870 /* create the image*/
1871 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1877 /* set image offset and reference grid */
1878 image->x0 = parameters->image_offset_x0;
1879 image->y0 = parameters->image_offset_y0;
1880 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1881 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1883 buf = _TIFFmalloc(TIFFStripSize(tif));
1885 strip_size=TIFFStripSize(tif);
1887 imgsize = image->comps[0].w * image->comps[0].h ;
1888 /* Read the Image components*/
1889 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1890 unsigned char *dat8;
1892 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1893 dat8 = (unsigned char*)buf;
1895 if (Info.tiBps==12){
1896 for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
1897 if((index < imgsize)&(index+1 < imgsize)){
1898 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
1899 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1900 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
1901 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1902 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
1903 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1909 else if( Info.tiBps==16){
1910 for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
1911 if(index < imgsize){
1912 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
1913 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
1914 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
1915 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1916 image->comps[0].data[index] = (image->comps[0].data[index] + 0x08) >> 4 ;
1917 image->comps[1].data[index] = (image->comps[1].data[index] + 0x08) >> 4 ;
1918 image->comps[2].data[index] = (image->comps[2].data[index] + 0x08) >> 4 ;
1925 else if ( Info.tiBps==8){
1926 for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
1927 if(index < imgsize){
1928 image->comps[0].data[index] = dat8[i+0];// R
1929 image->comps[1].data[index] = dat8[i+1];// G
1930 image->comps[2].data[index] = dat8[i+2];// B
1931 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1932 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
1933 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
1934 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
1942 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1943 fprintf(stderr,"Aborting\n");
1950 }else if(Info.tiPhoto == 1) {
1956 color_space = CLRSPC_GRAY;
1957 /* initialize image components*/
1958 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1959 cmptparm[0].prec = Info.tiBps;
1960 cmptparm[0].bpp = Info.tiBps;
1961 cmptparm[0].sgnd = 0;
1962 cmptparm[0].dx = subsampling_dx;
1963 cmptparm[0].dy = subsampling_dy;
1967 /* create the image*/
1968 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1973 /* set image offset and reference grid */
1974 image->x0 = parameters->image_offset_x0;
1975 image->y0 = parameters->image_offset_y0;
1976 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1977 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1979 buf = _TIFFmalloc(TIFFStripSize(tif));
1981 strip_size = TIFFStripSize(tif);
1983 imgsize = image->comps[0].w * image->comps[0].h ;
1984 /* Read the Image components*/
1985 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1986 unsigned char *dat8;
1988 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1989 dat8 = (unsigned char*)buf;
1991 if (Info.tiBps==12){
1992 for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
1993 if(index < imgsize){
1994 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
1995 image->comps[0].data[index+1] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2001 else if( Info.tiBps==16){
2002 for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
2003 if(index < imgsize){
2004 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2010 else if ( Info.tiBps==8){
2011 for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
2012 if(index < imgsize){
2013 image->comps[0].data[index] = dat8[i+0];
2020 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
2021 fprintf(stderr,"Aborting\n");
2029 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
2030 fprintf(stderr,"Aborting\n");
2036 #endif /* HAVE_LIBTIFF */
2038 /* -->> -->> -->> -->>
2042 <<-- <<-- <<-- <<-- */
2044 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2045 int subsampling_dx = parameters->subsampling_dx;
2046 int subsampling_dy = parameters->subsampling_dy;
2049 int i, compno, numcomps, w, h;
2050 OPJ_COLOR_SPACE color_space;
2051 opj_image_cmptparm_t *cmptparm;
2052 opj_image_t * image = NULL;
2055 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2057 fprintf(stderr,"\nError: invalid raw image parameters\n");
2058 fprintf(stderr,"Please use the Format option -F:\n");
2059 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2060 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2061 fprintf(stderr,"Aborting\n");
2065 f = fopen(filename, "rb");
2067 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2068 fprintf(stderr,"Aborting\n");
2071 numcomps = raw_cp->rawComp;
2072 color_space = CLRSPC_SRGB;
2073 w = raw_cp->rawWidth;
2074 h = raw_cp->rawHeight;
2075 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2077 /* initialize image components */
2078 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2079 for(i = 0; i < numcomps; i++) {
2080 cmptparm[i].prec = raw_cp->rawBitDepth;
2081 cmptparm[i].bpp = raw_cp->rawBitDepth;
2082 cmptparm[i].sgnd = raw_cp->rawSigned;
2083 cmptparm[i].dx = subsampling_dx;
2084 cmptparm[i].dy = subsampling_dy;
2088 /* create the image */
2089 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2094 /* set image offset and reference grid */
2095 image->x0 = parameters->image_offset_x0;
2096 image->y0 = parameters->image_offset_y0;
2097 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2098 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2100 if(raw_cp->rawBitDepth <= 8)
2102 unsigned char value = 0;
2103 for(compno = 0; compno < numcomps; compno++) {
2104 for (i = 0; i < w * h; i++) {
2105 if (!fread(&value, 1, 1, f)) {
2106 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2109 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2113 else if(raw_cp->rawBitDepth <= 16)
2115 unsigned short value;
2116 for(compno = 0; compno < numcomps; compno++) {
2117 for (i = 0; i < w * h; i++) {
2119 if (!fread(&temp, 1, 1, f)) {
2120 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2124 if (!fread(&temp, 1, 1, f)) {
2125 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2129 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2134 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2138 if (fread(&ch, 1, 1, f)) {
2139 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2146 int imagetoraw(opj_image_t * image, const char *outfile)
2148 FILE *rawFile = NULL;
2154 if((image->numcomps * image->x1 * image->y1) == 0)
2156 fprintf(stderr,"\nError: invalid raw image parameters\n");
2160 rawFile = fopen(outfile, "wb");
2162 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2166 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2168 for(compno = 0; compno < image->numcomps; compno++)
2170 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2171 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2173 w = image->comps[compno].w;
2174 h = image->comps[compno].h;
2176 if(image->comps[compno].prec <= 8)
2178 if(image->comps[compno].sgnd == 1)
2181 int mask = (1 << image->comps[compno].prec) - 1;
2182 ptr = image->comps[compno].data;
2183 for (line = 0; line < h; line++) {
2184 for(row = 0; row < w; row++) {
2185 curr = (signed char) (*ptr & mask);
2186 fwrite(&curr, sizeof(signed char), 1, rawFile);
2191 else if(image->comps[compno].sgnd == 0)
2194 int mask = (1 << image->comps[compno].prec) - 1;
2195 ptr = image->comps[compno].data;
2196 for (line = 0; line < h; line++) {
2197 for(row = 0; row < w; row++) {
2198 curr = (unsigned char) (*ptr & mask);
2199 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2205 else if(image->comps[compno].prec <= 16)
2207 if(image->comps[compno].sgnd == 1)
2209 signed short int curr;
2210 int mask = (1 << image->comps[compno].prec) - 1;
2211 ptr = image->comps[compno].data;
2212 for (line = 0; line < h; line++) {
2213 for(row = 0; row < w; row++) {
2215 curr = (signed short int) (*ptr & mask);
2216 temp = (unsigned char) (curr >> 8);
2217 fwrite(&temp, 1, 1, rawFile);
2218 temp = (unsigned char) curr;
2219 fwrite(&temp, 1, 1, rawFile);
2224 else if(image->comps[compno].sgnd == 0)
2226 unsigned short int curr;
2227 int mask = (1 << image->comps[compno].prec) - 1;
2228 ptr = image->comps[compno].data;
2229 for (line = 0; line < h; line++) {
2230 for(row = 0; row < w; row++) {
2232 curr = (unsigned short int) (*ptr & mask);
2233 temp = (unsigned char) (curr >> 8);
2234 fwrite(&temp, 1, 1, rawFile);
2235 temp = (unsigned char) curr;
2236 fwrite(&temp, 1, 1, rawFile);
2242 else if (image->comps[compno].prec <= 32)
2244 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2249 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2259 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
2260 #define MAGIC_SIZE 8
2261 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
2263 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
2267 double gamma, display_exponent;
2268 int bit_depth, interlace_type,compression_type, filter_type;
2270 png_uint_32 resx, resy;
2272 png_uint_32 width, height;
2273 int color_type, has_alpha, is16;
2276 unsigned char **rows;
2279 opj_image_cmptparm_t cmptparm[4];
2281 unsigned int nr_comp;
2283 unsigned char sigbuf[8];
2285 if((reader = fopen(read_idf, "rb")) == NULL)
2287 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
2290 image = NULL; png = NULL; rows = NULL;
2292 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
2293 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
2295 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
2298 /* libpng-VERSION/example.c:
2299 * PC : screen_gamma = 2.2;
2300 * Mac: screen_gamma = 1.7 or 1.0;
2302 display_exponent = 2.2;
2304 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
2305 NULL, NULL, NULL)) == NULL)
2307 if((info = png_create_info_struct(png)) == NULL)
2310 if(setjmp(png_jmpbuf(png)))
2313 png_init_io(png, reader);
2314 png_set_sig_bytes(png, MAGIC_SIZE);
2316 png_read_info(png, info);
2318 if(png_get_IHDR(png, info, &width, &height,
2319 &bit_depth, &color_type, &interlace_type,
2320 &compression_type, &filter_type) == 0)
2323 /* png_set_expand():
2324 * expand paletted images to RGB, expand grayscale images of
2325 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
2326 * to alpha channels.
2328 if(color_type == PNG_COLOR_TYPE_PALETTE)
2329 png_set_expand(png);
2331 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2332 png_set_expand(png);
2334 if(png_get_valid(png, info, PNG_INFO_tRNS))
2335 png_set_expand(png);
2337 is16 = (bit_depth == 16);
2339 /* GRAY => RGB; GRAY_ALPHA => RGBA
2341 if(color_type == PNG_COLOR_TYPE_GRAY
2342 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2344 png_set_gray_to_rgb(png);
2346 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
2347 PNG_COLOR_TYPE_RGB_ALPHA);
2349 if( !png_get_gAMA(png, info, &gamma))
2352 png_set_gamma(png, display_exponent, gamma);
2354 png_read_update_info(png, info);
2356 png_get_pHYs(png, info, &resx, &resy, &unit);
2358 color_type = png_get_color_type(png, info);
2360 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
2362 nr_comp = 3 + has_alpha;
2364 bit_depth = png_get_bit_depth(png, info);
2366 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
2367 for(i = 0; i < height; ++i)
2368 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
2370 png_read_image(png, rows);
2372 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
2374 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
2376 for(i = 0; i < nr_comp; ++i)
2378 cmptparm[i].prec = bit_depth;
2379 /* bits_per_pixel: 8 or 16 */
2380 cmptparm[i].bpp = bit_depth;
2381 cmptparm[i].sgnd = 0;
2382 cmptparm[i].dx = sub_dx;
2383 cmptparm[i].dy = sub_dy;
2384 cmptparm[i].w = width;
2385 cmptparm[i].h = height;
2388 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
2390 if(image == NULL) goto fin;
2392 image->x0 = params->image_offset_x0;
2393 image->y0 = params->image_offset_y0;
2394 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
2395 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
2397 r = image->comps[0].data;
2398 g = image->comps[1].data;
2399 b = image->comps[2].data;
2400 a = image->comps[3].data;
2402 for(i = 0; i < height; ++i)
2406 for(j = 0; j < width; ++j)
2410 *r++ = s[0]<<8|s[1]; s += 2;
2412 *g++ = s[0]<<8|s[1]; s += 2;
2414 *b++ = s[0]<<8|s[1]; s += 2;
2416 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
2420 *r++ = *s++; *g++ = *s++; *b++ = *s++;
2422 if(has_alpha) *a++ = *s++;
2428 for(i = 0; i < height; ++i)
2433 png_destroy_read_struct(&png, &info, NULL);
2441 int imagetopng(opj_image_t * image, const char *write_idf)
2446 int *red, *green, *blue, *alpha;
2447 unsigned char *row_buf, *d;
2448 int has_alpha, width, height, nr_comp, color_type;
2449 int adjustR, adjustG, adjustB, x, y, fails, is16, force16;
2450 int opj_prec, prec, ushift, dshift;
2451 unsigned short mask = 0xffff;
2452 png_color_8 sig_bit;
2454 is16 = force16 = ushift = dshift = 0; fails = 1;
2455 prec = opj_prec = image->comps[0].prec;
2457 if(prec > 8 && prec < 16)
2459 prec = 16; force16 = 1;
2461 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
2463 fprintf(stderr,"imagetopng: can not create %s"
2464 "\n\twrong bit_depth %d\n", write_idf, prec);
2467 writer = fopen(write_idf, "wb");
2469 if(writer == NULL) return fails;
2471 info = NULL; has_alpha = 0;
2473 /* Create and initialize the png_struct with the desired error handler
2474 * functions. If you want to use the default stderr and longjump method,
2475 * you can supply NULL for the last three parameters. We also check that
2476 * the library version is compatible with the one used at compile time,
2477 * in case we are using dynamically linked libraries. REQUIRED.
2479 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
2481 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
2483 if(png == NULL) goto fin;
2485 /* Allocate/initialize the image information data. REQUIRED
2487 info = png_create_info_struct(png);
2489 if(info == NULL) goto fin;
2491 /* Set error handling. REQUIRED if you are not supplying your own
2492 * error handling functions in the png_create_write_struct() call.
2494 if(setjmp(png_jmpbuf(png))) goto fin;
2496 /* I/O initialization functions is REQUIRED
2498 png_init_io(png, writer);
2500 /* Set the image information here. Width and height are up to 2^31,
2501 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
2502 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
2503 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
2504 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
2505 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
2506 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
2509 png_set_compression_level(png, Z_BEST_COMPRESSION);
2511 if(prec == 16) mask = 0xffff;
2513 if(prec == 8) mask = 0x00ff;
2515 if(prec == 4) mask = 0x000f;
2517 if(prec == 2) mask = 0x0003;
2519 if(prec == 1) mask = 0x0001;
2521 nr_comp = image->numcomps;
2524 && image->comps[0].dx == image->comps[1].dx
2525 && image->comps[1].dx == image->comps[2].dx
2526 && image->comps[0].dy == image->comps[1].dy
2527 && image->comps[1].dy == image->comps[2].dy
2528 && image->comps[0].prec == image->comps[1].prec
2529 && image->comps[1].prec == image->comps[2].prec)
2533 has_alpha = (nr_comp > 3);
2535 is16 = (prec == 16);
2537 width = image->comps[0].w;
2538 height = image->comps[0].h;
2540 red = image->comps[0].data;
2541 green = image->comps[1].data;
2542 blue = image->comps[2].data;
2544 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
2548 sig_bit.alpha = prec;
2549 alpha = image->comps[3].data;
2550 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
2554 sig_bit.alpha = 0; alpha = NULL;
2555 color_type = PNG_COLOR_TYPE_RGB;
2557 png_set_sBIT(png, info, &sig_bit);
2559 png_set_IHDR(png, info, width, height, prec,
2562 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2564 /*=============================*/
2565 png_write_info(png, info);
2566 /*=============================*/
2569 png_set_packing(png);
2573 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
2575 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2576 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2577 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2579 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
2581 for(y = 0; y < height; ++y)
2585 for(x = 0; x < width; ++x)
2589 /* Network byte order */
2590 v = *red + adjustR; ++red;
2592 if(force16) { v = (v<<ushift) + (v>>dshift); }
2594 #if BYTE_ORDER == LITTLE_ENDIAN
2595 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2597 *d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
2599 v = *green + adjustG; ++green;
2601 if(force16) { v = (v<<ushift) + (v>>dshift); }
2603 #if BYTE_ORDER == LITTLE_ENDIAN
2604 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2606 *d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
2609 v = *blue + adjustB; ++blue;
2611 if(force16) { v = (v<<ushift) + (v>>dshift); }
2613 #if BYTE_ORDER == LITTLE_ENDIAN
2614 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2616 *d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
2623 if(force16) { v = (v<<ushift) + (v>>dshift); }
2625 #if BYTE_ORDER == LITTLE_ENDIAN
2626 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2628 *d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
2633 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
2634 *d++ = (unsigned char)((*green + adjustG) & mask); ++green;
2635 *d++ = (unsigned char)((*blue + adjustB) & mask); ++blue;
2639 *d++ = (unsigned char)(*alpha & mask); ++alpha;
2643 png_write_row(png, row_buf);
2650 if(nr_comp == 1 /* GRAY */
2651 || ( nr_comp == 2 /* GRAY_ALPHA */
2652 && image->comps[0].dx == image->comps[1].dx
2653 && image->comps[0].dy == image->comps[1].dy
2654 && image->comps[0].prec == image->comps[1].prec))
2658 red = image->comps[0].data;
2662 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
2665 sig_bit.gray = prec;
2666 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
2668 color_type = PNG_COLOR_TYPE_GRAY;
2672 has_alpha = 1; sig_bit.alpha = prec;
2673 alpha = image->comps[1].data;
2674 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
2676 width = image->comps[0].w;
2677 height = image->comps[0].h;
2679 png_set_IHDR(png, info, width, height, sig_bit.gray,
2682 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2684 png_set_sBIT(png, info, &sig_bit);
2685 /*=============================*/
2686 png_write_info(png, info);
2687 /*=============================*/
2688 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2692 png_set_packing(png);
2697 /* Network byte order */
2700 row_buf = (unsigned char*)
2701 malloc(width * nr_comp * sizeof(unsigned short));
2703 for(y = 0; y < height; ++y)
2707 for(x = 0; x < width; ++x)
2709 v = *red + adjustR; ++red;
2711 if(force16) { v = (v<<ushift) + (v>>dshift); }
2713 #if BYTE_ORDER == LITTLE_ENDIAN
2714 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2716 *d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
2723 if(force16) { v = (v<<ushift) + (v>>dshift); }
2725 #if BYTE_ORDER == LITTLE_ENDIAN
2726 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2728 *d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
2732 png_write_row(png, row_buf);
2737 else /* prec <= 8 */
2739 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
2741 for(y = 0; y < height; ++y)
2745 for(x = 0; x < width; ++x)
2747 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
2751 *d++ = (unsigned char)(*alpha & mask); ++alpha;
2755 png_write_row(png, row_buf);
2763 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
2766 png_write_end(png, info);
2774 png_destroy_write_struct(&png, &info);
2778 if(fails) remove(write_idf);
2782 #endif /* HAVE_LIBPNG */