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"
41 #endif /* HAVE_LIBTIFF */
46 #endif /* HAVE_LIBPNG */
52 * Get logarithm of an integer and round downwards.
56 static int int_floorlog2(int a) {
58 for (l = 0; a > 1; l++) {
64 /* -->> -->> -->> -->>
68 <<-- <<-- <<-- <<-- */
70 // TGA header definition.
71 #pragma pack(push,1) // Pack structure byte aligned
72 typedef struct tga_header
74 unsigned char id_length; /* Image id field length */
75 unsigned char colour_map_type; /* Colour map type */
76 unsigned char image_type; /* Image type */
78 ** Colour map specification
80 unsigned short colour_map_index; /* First entry index */
81 unsigned short colour_map_length; /* Colour map length */
82 unsigned char colour_map_entry_size; /* Colour map entry size */
84 ** Image specification
86 unsigned short x_origin; /* x origin of image */
87 unsigned short y_origin; /* u origin of image */
88 unsigned short image_width; /* Image width */
89 unsigned short image_height; /* Image height */
90 unsigned char pixel_depth; /* Pixel depth */
91 unsigned char image_desc; /* Image descriptor */
93 #pragma pack(pop) // Return to normal structure packing alignment.
95 int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
96 unsigned int *width, unsigned int *height, int *flip_image)
101 if (!bits_per_pixel || !width || !height || !flip_image)
105 fread((unsigned char*)&tga, sizeof(tga_header), 1, fp);
107 *bits_per_pixel = tga.pixel_depth;
109 *width = tga.image_width;
110 *height = tga.image_height ;
112 // Ignore tga identifier, if present ...
115 unsigned char *id = (unsigned char *) malloc(tga.id_length);
116 fread(id, tga.id_length, 1, fp);
120 // Test for compressed formats ... not yet supported ...
121 // Note :- 9 - RLE encoded palettized.
122 // 10 - RLE encoded RGB.
123 if (tga.image_type > 8)
125 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
129 *flip_image = !(tga.image_desc & 32);
131 // Palettized formats are not yet supported, skip over the palette, if present ...
132 palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
136 fprintf(stderr, "File contains a palette - not yet supported.");
137 fseek(fp, palette_size, SEEK_CUR);
142 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
147 if (!bits_per_pixel || !width || !height)
150 memset(&tga, 0, sizeof(tga_header));
152 tga.pixel_depth = bits_per_pixel;
153 tga.image_width = width;
154 tga.image_height = height;
155 tga.image_type = 2; // Uncompressed.
156 tga.image_desc = 8; // 8 bits per component.
159 tga.image_desc |= 32;
162 fwrite((unsigned char*)&tga, sizeof(tga_header), 1, fp);
167 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
170 unsigned int image_width, image_height, pixel_bit_depth;
173 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
175 OPJ_COLOR_SPACE color_space;
178 int subsampling_dx, subsampling_dy;
181 f = fopen(filename, "rb");
183 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
187 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
190 // We currently only support 24 & 32 bit tga's ...
191 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
194 /* initialize image components */
195 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
197 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha.
198 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
201 color_space = CLRSPC_GRAY;
202 numcomps = save_alpha ? 2 : 1;
205 numcomps = save_alpha ? 4 : 3;
206 color_space = CLRSPC_SRGB;
209 subsampling_dx = parameters->subsampling_dx;
210 subsampling_dy = parameters->subsampling_dy;
212 for (i = 0; i < numcomps; i++) {
213 cmptparm[i].prec = 8;
215 cmptparm[i].sgnd = 0;
216 cmptparm[i].dx = subsampling_dx;
217 cmptparm[i].dy = subsampling_dy;
218 cmptparm[i].w = image_width;
219 cmptparm[i].h = image_height;
222 /* create the image */
223 image = opj_image_create(numcomps, &cmptparm[0], color_space);
228 /* set image offset and reference grid */
229 image->x0 = parameters->image_offset_x0;
230 image->y0 = parameters->image_offset_y0;
231 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
232 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
235 for (y=0; y < image_height; y++)
240 index = (image_height-y-1)*image_width;
242 index = y*image_width;
246 for (x=0;x<image_width;x++)
253 image->comps[0].data[index]=r;
254 image->comps[1].data[index]=g;
255 image->comps[2].data[index]=b;
259 else if (numcomps==4)
261 for (x=0;x<image_width;x++)
263 unsigned char r,g,b,a;
269 image->comps[0].data[index]=r;
270 image->comps[1].data[index]=g;
271 image->comps[2].data[index]=b;
272 image->comps[3].data[index]=a;
277 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
283 int imagetotga(opj_image_t * image, const char *outfile) {
284 int width, height, bpp, x, y;
287 unsigned int alpha_channel;
293 fdest = fopen(outfile, "wb");
295 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
299 for (i = 0; i < image->numcomps-1; i++) {
300 if ((image->comps[0].dx != image->comps[i+1].dx)
301 ||(image->comps[0].dy != image->comps[i+1].dy)
302 ||(image->comps[0].prec != image->comps[i+1].prec)) {
303 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
308 width = image->comps[0].w;
309 height = image->comps[0].h;
311 // Mono with alpha, or RGB with alpha.
312 write_alpha = (image->numcomps==2) || (image->numcomps==4);
315 bpp = write_alpha ? 32 : 24;
316 if (!tga_writeheader(fdest, bpp, width , height, true))
319 alpha_channel = image->numcomps-1;
321 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
323 for (y=0; y < height; y++) {
324 unsigned int index=y*width;
326 for (x=0; x < width; x++, index++) {
327 r = (float)(image->comps[0].data[index]);
329 if (image->numcomps>2) {
330 g = (float)(image->comps[1].data[index]);
331 b = (float)(image->comps[2].data[index]);
333 else {// Greyscale ...
338 // TGA format writes BGR ...
339 value = (unsigned char)(b*scale);
340 fwrite(&value,1,1,fdest);
342 value = (unsigned char)(g*scale);
343 fwrite(&value,1,1,fdest);
345 value = (unsigned char)(r*scale);
346 fwrite(&value,1,1,fdest);
349 a = (float)(image->comps[alpha_channel].data[index]);
350 value = (unsigned char)(a*scale);
351 fwrite(&value,1,1,fdest);
359 /* -->> -->> -->> -->>
363 <<-- <<-- <<-- <<-- */
365 /* WORD defines a two byte word */
366 typedef unsigned short int WORD;
368 /* DWORD defines a four byte word */
369 typedef unsigned long int DWORD;
372 WORD bfType; /* 'BM' for Bitmap (19776) */
373 DWORD bfSize; /* Size of the file */
374 WORD bfReserved1; /* Reserved : 0 */
375 WORD bfReserved2; /* Reserved : 0 */
376 DWORD bfOffBits; /* Offset */
377 } BITMAPFILEHEADER_t;
380 DWORD biSize; /* Size of the structure in bytes */
381 DWORD biWidth; /* Width of the image in pixels */
382 DWORD biHeight; /* Heigth of the image in pixels */
383 WORD biPlanes; /* 1 */
384 WORD biBitCount; /* Number of color bits by pixels */
385 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
386 DWORD biSizeImage; /* Size of the image in bytes */
387 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
388 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
389 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
390 DWORD biClrImportant; /* Number of important color (0: ALL) */
391 } BITMAPINFOHEADER_t;
393 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
394 int subsampling_dx = parameters->subsampling_dx;
395 int subsampling_dy = parameters->subsampling_dy;
397 int i, numcomps, w, h;
398 OPJ_COLOR_SPACE color_space;
399 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
400 opj_image_t * image = NULL;
403 BITMAPFILEHEADER_t File_h;
404 BITMAPINFOHEADER_t Info_h;
406 unsigned char *table_R, *table_G, *table_B;
407 unsigned int j, PAD = 0;
410 int gray_scale = 1, not_end_file = 1;
412 unsigned int line = 0, col = 0;
416 IN = fopen(filename, "rb");
418 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
422 File_h.bfType = getc(IN);
423 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
425 if (File_h.bfType != 19778) {
426 fprintf(stderr,"Error, not a BMP file!\n");
431 File_h.bfSize = getc(IN);
432 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
433 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
434 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
436 File_h.bfReserved1 = getc(IN);
437 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
439 File_h.bfReserved2 = getc(IN);
440 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
442 File_h.bfOffBits = getc(IN);
443 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
444 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
445 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
450 Info_h.biSize = getc(IN);
451 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
452 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
453 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
455 Info_h.biWidth = getc(IN);
456 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
457 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
458 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
461 Info_h.biHeight = getc(IN);
462 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
463 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
464 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
467 Info_h.biPlanes = getc(IN);
468 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
470 Info_h.biBitCount = getc(IN);
471 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
473 Info_h.biCompression = getc(IN);
474 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
475 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
476 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
478 Info_h.biSizeImage = getc(IN);
479 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
480 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
481 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
483 Info_h.biXpelsPerMeter = getc(IN);
484 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
485 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
486 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
488 Info_h.biYpelsPerMeter = getc(IN);
489 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
490 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
491 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
493 Info_h.biClrUsed = getc(IN);
494 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
495 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
496 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
498 Info_h.biClrImportant = getc(IN);
499 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
500 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
501 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
503 /* Read the data and store them in the OUT file */
505 if (Info_h.biBitCount == 24) {
507 color_space = CLRSPC_SRGB;
508 /* initialize image components */
509 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
510 for(i = 0; i < numcomps; i++) {
511 cmptparm[i].prec = 8;
513 cmptparm[i].sgnd = 0;
514 cmptparm[i].dx = subsampling_dx;
515 cmptparm[i].dy = subsampling_dy;
519 /* create the image */
520 image = opj_image_create(numcomps, &cmptparm[0], color_space);
526 /* set image offset and reference grid */
527 image->x0 = parameters->image_offset_x0;
528 image->y0 = parameters->image_offset_y0;
529 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
530 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
534 /* Place the cursor at the beginning of the image information */
535 fseek(IN, 0, SEEK_SET);
536 fseek(IN, File_h.bfOffBits, SEEK_SET);
541 /* PAD = 4 - (3 * W) % 4; */
542 /* PAD = (PAD == 4) ? 0 : PAD; */
543 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
545 RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
547 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
551 for(y = 0; y < (int)H; y++) {
552 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
553 for(x = 0; x < (int)W; x++) {
554 unsigned char *pixel = &scanline[3 * x];
555 image->comps[0].data[index] = pixel[2]; /* R */
556 image->comps[1].data[index] = pixel[1]; /* G */
557 image->comps[2].data[index] = pixel[0]; /* B */
564 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
565 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
566 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
567 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
569 for (j = 0; j < Info_h.biClrUsed; j++) {
570 table_B[j] = getc(IN);
571 table_G[j] = getc(IN);
572 table_R[j] = getc(IN);
574 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
578 /* Place the cursor at the beginning of the image information */
579 fseek(IN, 0, SEEK_SET);
580 fseek(IN, File_h.bfOffBits, SEEK_SET);
584 if (Info_h.biWidth % 2)
587 numcomps = gray_scale ? 1 : 3;
588 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
589 /* initialize image components */
590 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
591 for(i = 0; i < numcomps; i++) {
592 cmptparm[i].prec = 8;
594 cmptparm[i].sgnd = 0;
595 cmptparm[i].dx = subsampling_dx;
596 cmptparm[i].dy = subsampling_dy;
600 /* create the image */
601 image = opj_image_create(numcomps, &cmptparm[0], color_space);
607 /* set image offset and reference grid */
608 image->x0 = parameters->image_offset_x0;
609 image->y0 = parameters->image_offset_y0;
610 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
611 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
615 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
617 fread(RGB, sizeof(unsigned char), W * H, IN);
620 for (j = 0; j < W * H; j++) {
621 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
622 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
629 for (j = 0; j < W * H; j++) {
630 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
631 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
632 image->comps[0].data[index] = table_R[pixel_index];
633 image->comps[1].data[index] = table_G[pixel_index];
634 image->comps[2].data[index] = table_B[pixel_index];
643 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
644 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
645 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
646 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
648 for (j = 0; j < Info_h.biClrUsed; j++) {
649 table_B[j] = getc(IN);
650 table_G[j] = getc(IN);
651 table_R[j] = getc(IN);
653 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
657 numcomps = gray_scale ? 1 : 3;
658 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
659 /* initialize image components */
660 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
661 for(i = 0; i < numcomps; i++) {
662 cmptparm[i].prec = 8;
664 cmptparm[i].sgnd = 0;
665 cmptparm[i].dx = subsampling_dx;
666 cmptparm[i].dy = subsampling_dy;
670 /* create the image */
671 image = opj_image_create(numcomps, &cmptparm[0], color_space);
677 /* set image offset and reference grid */
678 image->x0 = parameters->image_offset_x0;
679 image->y0 = parameters->image_offset_y0;
680 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
681 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
685 /* Place the cursor at the beginning of the image information */
686 fseek(IN, 0, SEEK_SET);
687 fseek(IN, File_h.bfOffBits, SEEK_SET);
689 RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
691 while (not_end_file) {
695 for (i = 0; i < (int) v; i++) {
696 RGB[line * Info_h.biWidth + col] = v2;
711 fprintf(stderr,"No Delta supported\n");
712 opj_image_destroy(image);
716 for (i = 0; i < v; i++) {
718 RGB[line * Info_h.biWidth + col] = v2;
729 for (line = 0; line < Info_h.biHeight; line++) {
730 for (col = 0; col < Info_h.biWidth; col++) {
731 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
737 for (line = 0; line < Info_h.biHeight; line++) {
738 for (col = 0; col < Info_h.biWidth; col++) {
739 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
740 image->comps[0].data[index] = table_R[pixel_index];
741 image->comps[1].data[index] = table_G[pixel_index];
742 image->comps[2].data[index] = table_B[pixel_index];
753 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
761 int imagetobmp(opj_image_t * image, const char *outfile) {
765 int adjustR, adjustG, adjustB;
767 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
768 && image->comps[1].dx == image->comps[2].dx
769 && image->comps[0].dy == image->comps[1].dy
770 && image->comps[1].dy == image->comps[2].dy
771 && image->comps[0].prec == image->comps[1].prec
772 && image->comps[1].prec == image->comps[2].prec) {
774 /* -->> -->> -->> -->>
776 <<-- <<-- <<-- <<-- */
778 fdest = fopen(outfile, "wb");
780 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
784 w = image->comps[0].w;
785 h = image->comps[0].h;
787 fprintf(fdest, "BM");
791 fprintf(fdest, "%c%c%c%c",
792 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
793 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
794 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
795 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
796 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
797 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
801 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
802 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
803 (unsigned char) ((w) >> 8) & 0xff,
804 (unsigned char) ((w) >> 16) & 0xff,
805 (unsigned char) ((w) >> 24) & 0xff);
806 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
807 (unsigned char) ((h) >> 8) & 0xff,
808 (unsigned char) ((h) >> 16) & 0xff,
809 (unsigned char) ((h) >> 24) & 0xff);
810 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
811 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
812 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
813 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
814 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
815 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
816 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
817 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
818 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 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", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
822 if (image->comps[0].prec > 8) {
823 adjustR = image->comps[0].prec - 8;
824 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
828 if (image->comps[1].prec > 8) {
829 adjustG = image->comps[1].prec - 8;
830 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
834 if (image->comps[2].prec > 8) {
835 adjustB = image->comps[2].prec - 8;
836 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
841 for (i = 0; i < w * h; i++) {
842 unsigned char rc, gc, bc;
845 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
846 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
847 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
848 if(r > 255) r = 255; else if(r < 0) r = 0;
849 rc = (unsigned char)r;
851 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
852 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
853 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
854 if(g > 255) g = 255; else if(g < 0) g = 0;
855 gc = (unsigned char)g;
857 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
858 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
859 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
860 if(b > 255) b = 255; else if(b < 0) b = 0;
861 bc = (unsigned char)b;
863 fprintf(fdest, "%c%c%c", bc, gc, rc);
865 if ((i + 1) % w == 0) {
866 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
867 fprintf(fdest, "%c", 0);
871 } else { /* Gray-scale */
873 /* -->> -->> -->> -->>
874 8 bits non code (Gray scale)
875 <<-- <<-- <<-- <<-- */
877 fdest = fopen(outfile, "wb");
878 w = image->comps[0].w;
879 h = image->comps[0].h;
881 fprintf(fdest, "BM");
885 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
886 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
887 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
888 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
889 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
890 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
891 ((54 + 1024) >> 16) & 0xff,
892 ((54 + 1024) >> 24) & 0xff);
896 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
897 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
898 (unsigned char) ((w) >> 8) & 0xff,
899 (unsigned char) ((w) >> 16) & 0xff,
900 (unsigned char) ((w) >> 24) & 0xff);
901 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
902 (unsigned char) ((h) >> 8) & 0xff,
903 (unsigned char) ((h) >> 16) & 0xff,
904 (unsigned char) ((h) >> 24) & 0xff);
905 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
906 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
907 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
908 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
909 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
910 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
911 (unsigned char) ((h * w + h * (w % 2)) >> 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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
914 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
915 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
917 if (image->comps[0].prec > 8) {
918 adjustR = image->comps[0].prec - 8;
919 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
923 for (i = 0; i < 256; i++) {
924 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
927 for (i = 0; i < w * h; i++) {
931 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
932 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
933 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
934 if(r > 255) r = 255; else if(r < 0) r = 0;
936 fprintf(fdest, "%c", (unsigned char)r);
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 <<-- <<-- <<-- <<-- */
1168 int width, height, maxval, depth, format;
1169 char rgb, rgba, gray, graya, bw;
1173 static char *skip_white(char *s)
1177 if(*s == '\n' || *s == '\r') return NULL;
1178 if(isspace(*s)) { ++s; continue; }
1184 static char *skip_int(char *start, int *out_n)
1189 *out_n = 0; s = start;
1191 s = skip_white(start);
1192 if(s == NULL) return NULL;
1197 if( !isdigit(*s)) break;
1200 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1204 static char *skip_idf(char *start, char out_idf[256])
1209 s = skip_white(start);
1210 if(s == NULL) return NULL;
1215 if(isalpha(*s) || *s == '_') { ++s; continue; }
1218 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1222 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1225 int format, have_wh, end, ttype;
1226 char idf[256], type[256];
1229 fgets(line, 250, reader);
1233 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1235 format = atoi(line + 1);
1236 if(format < 1 || format > 7)
1238 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1241 ph->format = format;
1242 ttype = end = have_wh = 0;
1244 while(fgets(line, 250, reader))
1246 if(*line == '#') continue;
1252 s = skip_idf(s, idf);
1254 if(s == NULL || *s == 0) return;
1256 if(strcmp(idf, "ENDHDR") == 0)
1260 if(strcmp(idf, "WIDTH") == 0)
1262 s = skip_int(s, &ph->width);
1263 if(s == NULL || *s == 0) return;
1267 if(strcmp(idf, "HEIGHT") == 0)
1269 s = skip_int(s, &ph->height);
1270 if(s == NULL || *s == 0) return;
1274 if(strcmp(idf, "DEPTH") == 0)
1276 s = skip_int(s, &ph->depth);
1277 if(s == NULL || *s == 0) return;
1281 if(strcmp(idf, "MAXVAL") == 0)
1283 s = skip_int(s, &ph->maxval);
1284 if(s == NULL || *s == 0) return;
1288 if(strcmp(idf, "TUPLTYPE") == 0)
1290 s = skip_idf(s, type);
1291 if(s == NULL || *s == 0) return;
1293 if(strcmp(type, "BLACKANDWHITE") == 0)
1295 ph->bw = 1; ttype = 1; continue;
1297 if(strcmp(type, "GRAYSCALE") == 0)
1299 ph->gray = 1; ttype = 1; continue;
1301 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1303 ph->graya = 1; ttype = 1; continue;
1305 if(strcmp(type, "RGB") == 0)
1307 ph->rgb = 1; ttype = 1; continue;
1309 if(strcmp(type, "RGB_ALPHA") == 0)
1311 ph->rgba = 1; ttype = 1; continue;
1313 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1316 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1318 } /* if(format == 7) */
1322 s = skip_int(s, &ph->width);
1324 s = skip_int(s, &ph->height);
1328 if(format == 1 || format == 4) break;
1332 if(format == 2 || format == 3 || format == 5 || format == 6)
1334 /* P2, P3, P5, P6: */
1335 s = skip_int(s, &ph->maxval);
1337 if(ph->maxval > 65535) return;
1340 }/* while(fgets( ) */
1341 if(format == 2 || format == 3 || format > 4)
1343 if(ph->maxval < 1 || ph->maxval > 65535) return;
1345 if(ph->width < 1 || ph->height < 1) return;
1351 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1353 if(ph->depth < 1 || ph->depth > 4) return;
1355 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1360 if(format != 1 && format != 4)
1362 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1366 if(ph->width && ph->height) ph->ok = 1;
1372 static int has_prec(int val)
1374 if(val < 2) return 1;
1375 if(val < 4) return 2;
1376 if(val < 8) return 3;
1377 if(val < 16) return 4;
1378 if(val < 32) return 5;
1379 if(val < 64) return 6;
1380 if(val < 128) return 7;
1381 if(val < 256) return 8;
1382 if(val < 512) return 9;
1383 if(val < 1024) return 10;
1384 if(val < 2048) return 11;
1385 if(val < 4096) return 12;
1386 if(val < 8192) return 13;
1387 if(val < 16384) return 14;
1388 if(val < 32768) return 15;
1392 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1393 int subsampling_dx = parameters->subsampling_dx;
1394 int subsampling_dy = parameters->subsampling_dy;
1397 int i, compno, numcomps, w, h, prec, format;
1398 OPJ_COLOR_SPACE color_space;
1399 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1400 opj_image_t * image = NULL;
1401 struct pnm_header header_info;
1403 if((fp = fopen(filename, "rb")) == NULL)
1405 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1408 memset(&header_info, 0, sizeof(struct pnm_header));
1410 read_pnm_header(fp, &header_info);
1412 if(!header_info.ok) { fclose(fp); return NULL; }
1414 format = header_info.format;
1418 case 1: /* ascii bitmap */
1419 case 4: /* raw bitmap */
1423 case 2: /* ascii greymap */
1424 case 5: /* raw greymap */
1428 case 3: /* ascii pixmap */
1429 case 6: /* raw pixmap */
1433 case 7: /* arbitrary map */
1434 numcomps = header_info.depth;
1437 default: fclose(fp); return NULL;
1440 color_space = CLRSPC_GRAY;/* GRAY, GRAYA */
1442 color_space = CLRSPC_SRGB;/* RGB, RGBA */
1444 prec = has_prec(header_info.maxval);
1446 if(prec < 8) prec = 8;
1448 w = header_info.width;
1449 h = header_info.height;
1450 subsampling_dx = parameters->subsampling_dx;
1451 subsampling_dy = parameters->subsampling_dy;
1453 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1455 for(i = 0; i < numcomps; i++)
1457 cmptparm[i].prec = prec;
1458 cmptparm[i].bpp = prec;
1459 cmptparm[i].sgnd = 0;
1460 cmptparm[i].dx = subsampling_dx;
1461 cmptparm[i].dy = subsampling_dy;
1465 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1467 if(!image) { fclose(fp); return NULL; }
1469 /* set image offset and reference grid */
1470 image->x0 = parameters->image_offset_x0;
1471 image->y0 = parameters->image_offset_y0;
1472 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1473 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1475 if((format == 2) || (format == 3)) /* ascii pixmap */
1479 for (i = 0; i < w * h; i++)
1481 for(compno = 0; compno < numcomps; compno++)
1484 fscanf(fp, "%u", &index);
1486 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1494 && ( header_info.gray || header_info.graya
1495 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1497 unsigned char c0, c1, one;
1501 for (i = 0; i < w * h; i++)
1503 for(compno = 0; compno < numcomps; compno++)
1505 fread(&c0, 1, 1, fp);
1508 image->comps[compno].data[i] = c0;
1512 fread(&c1, 1, 1, fp);
1514 image->comps[compno].data[i] = ((c0<<8) | c1);
1520 if(format == 1) /* ascii bitmap */
1522 for (i = 0; i < w * h; i++)
1526 fscanf(fp, "%u", &index);
1528 image->comps[0].data[i] = (index?0:255);
1538 for(y = 0; y < h; ++y)
1542 for(x = 0; x < w; ++x)
1547 uc = (unsigned char)getc(fp);
1549 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1555 if((format == 7 && header_info.bw)) //MONO
1559 for(i = 0; i < w * h; ++i)
1561 fread(&uc, 1, 1, fp);
1562 image->comps[0].data[i] = (uc & 1)?0:255;
1570 int imagetopnm(opj_image_t * image, const char *outfile)
1572 int *red, *green, *blue, *alpha;
1574 int i, compno, ncomp;
1575 int adjustR, adjustG, adjustB, adjustA;
1576 int fails, two, want_gray, has_alpha, triple;
1579 const char *tmp = outfile;
1582 if((prec = image->comps[0].prec) > 16)
1584 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1585 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1588 two = has_alpha = 0; fails = 1;
1589 ncomp = image->numcomps;
1591 while (*tmp) ++tmp; tmp -= 2;
1592 want_gray = (*tmp == 'g' || *tmp == 'G');
1593 ncomp = image->numcomps;
1595 if(want_gray) ncomp = 1;
1597 if (ncomp == 2 /* GRAYA */
1598 || (ncomp > 2 /* RGB, RGBA */
1599 && image->comps[0].dx == image->comps[1].dx
1600 && image->comps[1].dx == image->comps[2].dx
1601 && image->comps[0].dy == image->comps[1].dy
1602 && image->comps[1].dy == image->comps[2].dy
1603 && image->comps[0].prec == image->comps[1].prec
1604 && image->comps[1].prec == image->comps[2].prec
1607 fdest = fopen(outfile, "wb");
1611 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1615 triple = (ncomp > 2);
1616 wr = image->comps[0].w; hr = image->comps[0].h;
1617 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1619 red = image->comps[0].data;
1623 green = image->comps[1].data;
1624 blue = image->comps[2].data;
1626 else green = blue = NULL;
1630 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1632 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1633 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1634 wr, hr, ncomp, max, tt);
1635 alpha = image->comps[ncomp - 1].data;
1636 adjustA = (image->comps[ncomp - 1].sgnd ?
1637 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1641 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1642 opj_version(), wr, hr, max);
1643 alpha = NULL; adjustA = 0;
1645 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1649 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1650 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1652 else adjustG = adjustB = 0;
1654 for(i = 0; i < wr * hr; ++i)
1658 v = *red + adjustR; ++red;
1660 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1664 v = *green + adjustG; ++green;
1666 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1668 v = *blue + adjustB; ++blue;
1670 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1676 v = *alpha + adjustA; ++alpha;
1678 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1686 fprintf(fdest, "%c", (unsigned char)*red++);
1688 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
1691 fprintf(fdest, "%c", (unsigned char)*alpha++);
1695 fclose(fdest); return 0;
1700 if (image->numcomps > ncomp)
1702 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1703 fprintf(stderr," is written to the file\n");
1705 destname = (char*)malloc(strlen(outfile) + 8);
1707 for (compno = 0; compno < ncomp; compno++)
1710 sprintf(destname, "%d.%s", compno, outfile);
1712 sprintf(destname, "%s", outfile);
1714 fdest = fopen(destname, "wb");
1717 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1721 wr = image->comps[compno].w; hr = image->comps[compno].h;
1722 prec = image->comps[compno].prec;
1723 max = (1<<prec) - 1;
1725 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
1726 opj_version(), wr, hr, max);
1728 red = image->comps[compno].data;
1730 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1734 for (i = 0; i < wr * hr; i++)
1736 v = *red + adjustR; ++red;
1738 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1744 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1748 else /* prec <= 8 */
1750 for(i = 0; i < wr * hr; ++i)
1752 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
1763 /* -->> -->> -->> -->>
1767 <<-- <<-- <<-- <<-- */
1769 typedef struct tiff_infoheader{
1770 DWORD tiWidth; // Width of Image in pixel
1771 DWORD tiHeight; // Height of Image in pixel
1772 DWORD tiPhoto; // Photometric
1773 WORD tiBps; // Bits per sample
1774 WORD tiSf; // Sample Format
1775 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1776 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1779 int imagetotif(opj_image_t * image, const char *outfile)
1781 int width, height, imgsize;
1782 int bps,index,adjust, sgnd;
1783 int ushift, dshift, has_alpha, force16;
1784 unsigned int last_i=0;
1790 ushift = dshift = force16 = has_alpha = 0;
1791 bps = image->comps[0].prec;
1793 if(bps > 8 && bps < 16)
1795 ushift = 16 - bps; dshift = bps - ushift;
1796 bps = 16; force16 = 1;
1799 if(bps != 8 && bps != 16)
1801 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
1803 fprintf(stderr,"\tAborting\n");
1806 tif = TIFFOpen(outfile, "wb");
1810 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
1813 sgnd = image->comps[0].sgnd;
1814 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
1816 if(image->numcomps >= 3
1817 && image->comps[0].dx == image->comps[1].dx
1818 && image->comps[1].dx == image->comps[2].dx
1819 && image->comps[0].dy == image->comps[1].dy
1820 && image->comps[1].dy == image->comps[2].dy
1821 && image->comps[0].prec == image->comps[1].prec
1822 && image->comps[1].prec == image->comps[2].prec)
1824 has_alpha = (image->numcomps == 4);
1826 width = image->comps[0].w;
1827 height = image->comps[0].h;
1828 imgsize = width * height ;
1830 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1831 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1832 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
1833 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1834 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1835 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1836 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1837 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1838 strip_size = TIFFStripSize(tif);
1839 buf = _TIFFmalloc(strip_size);
1842 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
1844 unsigned char *dat8;
1846 ssize = TIFFStripSize(tif);
1847 dat8 = (unsigned char*)buf;
1852 step = 3 + has_alpha;
1855 for(i=0; i < ssize - restx; i += step)
1861 r = image->comps[0].data[index];
1862 g = image->comps[1].data[index];
1863 b = image->comps[2].data[index];
1864 if(has_alpha) a = image->comps[3].data[index];
1871 if(has_alpha) a += adjust;
1876 if(has_alpha) dat8[i+3] = a;
1887 for(i = last_i; i < ssize; i += step)
1893 r = image->comps[0].data[index];
1894 g = image->comps[1].data[index];
1895 b = image->comps[2].data[index];
1896 if(has_alpha) a = image->comps[3].data[index];
1903 if(has_alpha) a += adjust;
1906 if(i+1 < ssize) dat8[i+1] = g ; else break;
1907 if(i+2 < ssize) dat8[i+2] = b ; else break;
1910 if(i+3 < ssize) dat8[i+3] = a ; else break;
1917 }//if(last_i < ssize)
1923 step = 6 + has_alpha + has_alpha;
1926 for(i = 0; i < ssize - restx ; i += step)
1932 r = image->comps[0].data[index];
1933 g = image->comps[1].data[index];
1934 b = image->comps[2].data[index];
1935 if(has_alpha) a = image->comps[3].data[index];
1942 if(has_alpha) a += adjust;
1946 r = (r<<ushift) + (r>>dshift);
1947 g = (g<<ushift) + (g>>dshift);
1948 b = (b<<ushift) + (b>>dshift);
1949 if(has_alpha) a = (a<<ushift) + (a>>dshift);
1952 dat8[i+1] = (r >> 8);//MSB
1954 dat8[i+3] = (g >> 8);
1956 dat8[i+5] = (b >> 8);
1960 dat8[i+7] = (a >> 8);
1971 for(i = last_i ; i < ssize ; i += step)
1977 r = image->comps[0].data[index];
1978 g = image->comps[1].data[index];
1979 b = image->comps[2].data[index];
1980 if(has_alpha) a = image->comps[3].data[index];
1987 if(has_alpha) a += adjust;
1991 r = (r<<ushift) + (r>>dshift);
1992 g = (g<<ushift) + (g>>dshift);
1993 b = (b<<ushift) + (b>>dshift);
1994 if(has_alpha) a = (a<<ushift) + (a>>dshift);
1997 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB
1998 if(i+2 < ssize) dat8[i+2] = g; else break;
1999 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2000 if(i+4 < ssize) dat8[i+4] = b; else break;
2001 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2005 if(i+6 < ssize) dat8[i+6] = a; else break;
2006 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2013 }//if(last_i < ssize)
2016 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2019 _TIFFfree((void*)buf);
2025 if(image->numcomps == 1 /* GRAY */
2026 || ( image->numcomps == 2 /* GRAY_ALPHA */
2027 && image->comps[0].dx == image->comps[1].dx
2028 && image->comps[0].dy == image->comps[1].dy
2029 && image->comps[0].prec == image->comps[1].prec))
2033 has_alpha = (image->numcomps == 2);
2035 width = image->comps[0].w;
2036 height = image->comps[0].h;
2037 imgsize = width * height;
2040 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2041 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2042 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2043 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2044 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2045 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2046 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2047 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2049 /* Get a buffer for the data */
2050 strip_size = TIFFStripSize(tif);
2051 buf = _TIFFmalloc(strip_size);
2054 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2056 unsigned char *dat8;
2057 tsize_t i, ssize = TIFFStripSize(tif);
2058 dat8 = (unsigned char*)buf;
2062 step = 1 + has_alpha;
2064 for(i=0; i < ssize; i += step)
2070 r = image->comps[0].data[index];
2071 if(has_alpha) a = image->comps[1].data[index];
2076 if(has_alpha) a += adjust;
2079 if(has_alpha) dat8[i+1] = a;
2089 step = 2 + has_alpha + has_alpha;
2091 for(i=0; i < ssize; i += step)
2097 r = image->comps[0].data[index];
2098 if(has_alpha) a = image->comps[1].data[index];
2103 if(has_alpha) a += adjust;
2107 r = (r<<ushift) + (r>>dshift);
2108 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2111 dat8[i+1] = r >> 8;//MSB
2118 }//if(index < imgsize)
2123 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2134 fprintf(stderr,"imagetotif: Bad color format.\n"
2135 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2136 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2143 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2144 * CINEMA : 12 bit precision
2146 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2148 int subsampling_dx = parameters->subsampling_dx;
2149 int subsampling_dy = parameters->subsampling_dy;
2151 tiff_infoheader_t Info;
2155 int j, numcomps, w, h,index;
2156 OPJ_COLOR_SPACE color_space;
2157 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2158 opj_image_t *image = NULL;
2162 tif = TIFFOpen(filename, "r");
2166 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2169 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
2170 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
2171 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
2172 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
2173 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
2175 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
2176 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
2183 if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
2184 if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
2189 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2190 " implemented\n",Info.tiBps);
2193 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2194 " and GRAY(A) has been implemented\n",(int) Info.tiPhoto);
2196 fprintf(stderr,"\tAborting\n");
2203 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2205 uint16 extrasamples;
2207 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2208 &extrasamples, &sampleinfo);
2210 if(extrasamples >= 1)
2212 switch(sampleinfo[0])
2214 case EXTRASAMPLE_UNSPECIFIED:
2215 /* Workaround for some images without correct info about alpha channel
2221 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2222 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2228 /* initialize image components
2230 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2232 if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2234 numcomps = 3 + has_alpha;
2235 color_space = CLRSPC_SRGB;
2237 for(j = 0; j < numcomps; j++)
2239 if(parameters->cp_cinema)
2241 cmptparm[j].prec = 12;
2242 cmptparm[j].bpp = 12;
2246 cmptparm[j].prec = Info.tiBps;
2247 cmptparm[j].bpp = Info.tiBps;
2249 cmptparm[j].dx = subsampling_dx;
2250 cmptparm[j].dy = subsampling_dy;
2255 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2262 /* set image offset and reference grid
2264 image->x0 = parameters->image_offset_x0;
2265 image->y0 = parameters->image_offset_y0;
2266 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2267 image->x0 + (w - 1) * subsampling_dx + 1;
2268 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2269 image->y0 + (h - 1) * subsampling_dy + 1;
2271 buf = _TIFFmalloc(TIFFStripSize(tif));
2273 strip_size=TIFFStripSize(tif);
2275 imgsize = image->comps[0].w * image->comps[0].h ;
2276 /* Read the Image components
2278 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2280 unsigned char *dat8;
2283 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2284 dat8 = (unsigned char*)buf;
2286 if(Info.tiBps == 16)
2288 step = 6 + has_alpha + has_alpha;
2290 for(i = 0; i < ssize; i += step)
2294 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
2295 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
2296 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
2298 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2300 if(parameters->cp_cinema)
2302 /* Rounding 16 to 12 bits
2304 image->comps[0].data[index] =
2305 (image->comps[0].data[index] + 0x08) >> 4 ;
2306 image->comps[1].data[index] =
2307 (image->comps[1].data[index] + 0x08) >> 4 ;
2308 image->comps[2].data[index] =
2309 (image->comps[2].data[index] + 0x08) >> 4 ;
2311 image->comps[3].data[index] =
2312 (image->comps[3].data[index] + 0x08) >> 4 ;
2319 }//if(Info.tiBps == 16)
2323 step = 3 + has_alpha;
2325 for(i = 0; i < ssize; i += step)
2329 image->comps[0].data[index] = dat8[i+0];// R
2330 image->comps[1].data[index] = dat8[i+1];// G
2331 image->comps[2].data[index] = dat8[i+2];// B
2333 image->comps[3].data[index] = dat8[i+3];
2335 if(parameters->cp_cinema)
2337 /* Rounding 8 to 12 bits
2339 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2340 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2341 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2343 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2350 }//if( Info.tiBps == 8)
2352 if(Info.tiBps == 12)/* CINEMA file */
2356 for(i = 0; i < ssize; i += step)
2358 if((index < imgsize)&(index+1 < imgsize))
2360 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2361 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2363 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2364 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2366 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2367 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2383 if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2385 numcomps = 1 + has_alpha;
2386 color_space = CLRSPC_GRAY;
2388 for(j = 0; j < numcomps; ++j)
2390 cmptparm[j].prec = Info.tiBps;
2391 cmptparm[j].bpp = Info.tiBps;
2392 cmptparm[j].dx = subsampling_dx;
2393 cmptparm[j].dy = subsampling_dy;
2397 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2404 /* set image offset and reference grid
2406 image->x0 = parameters->image_offset_x0;
2407 image->y0 = parameters->image_offset_y0;
2408 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2409 image->x0 + (w - 1) * subsampling_dx + 1;
2410 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2411 image->y0 + (h - 1) * subsampling_dy + 1;
2413 buf = _TIFFmalloc(TIFFStripSize(tif));
2415 strip_size = TIFFStripSize(tif);
2417 imgsize = image->comps[0].w * image->comps[0].h ;
2418 /* Read the Image components
2420 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2422 unsigned char *dat8;
2426 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2427 dat8 = (unsigned char*)buf;
2429 if(Info.tiBps == 16)
2431 step = 2 + has_alpha + has_alpha;
2433 for(i = 0; i < ssize; i += step)
2437 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2439 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2449 step = 1 + has_alpha;
2451 for(i = 0; i < ssize; i += step)
2455 image->comps[0].data[index] = dat8[i+0];
2457 image->comps[1].data[index] = dat8[i+1];
2475 #endif /* HAVE_LIBTIFF */
2477 /* -->> -->> -->> -->>
2481 <<-- <<-- <<-- <<-- */
2483 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2484 int subsampling_dx = parameters->subsampling_dx;
2485 int subsampling_dy = parameters->subsampling_dy;
2488 int i, compno, numcomps, w, h;
2489 OPJ_COLOR_SPACE color_space;
2490 opj_image_cmptparm_t *cmptparm;
2491 opj_image_t * image = NULL;
2494 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2496 fprintf(stderr,"\nError: invalid raw image parameters\n");
2497 fprintf(stderr,"Please use the Format option -F:\n");
2498 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2499 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2500 fprintf(stderr,"Aborting\n");
2504 f = fopen(filename, "rb");
2506 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2507 fprintf(stderr,"Aborting\n");
2510 numcomps = raw_cp->rawComp;
2511 color_space = CLRSPC_SRGB;
2512 w = raw_cp->rawWidth;
2513 h = raw_cp->rawHeight;
2514 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2516 /* initialize image components */
2517 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2518 for(i = 0; i < numcomps; i++) {
2519 cmptparm[i].prec = raw_cp->rawBitDepth;
2520 cmptparm[i].bpp = raw_cp->rawBitDepth;
2521 cmptparm[i].sgnd = raw_cp->rawSigned;
2522 cmptparm[i].dx = subsampling_dx;
2523 cmptparm[i].dy = subsampling_dy;
2527 /* create the image */
2528 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2533 /* set image offset and reference grid */
2534 image->x0 = parameters->image_offset_x0;
2535 image->y0 = parameters->image_offset_y0;
2536 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2537 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2539 if(raw_cp->rawBitDepth <= 8)
2541 unsigned char value = 0;
2542 for(compno = 0; compno < numcomps; compno++) {
2543 for (i = 0; i < w * h; i++) {
2544 if (!fread(&value, 1, 1, f)) {
2545 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2548 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2552 else if(raw_cp->rawBitDepth <= 16)
2554 unsigned short value;
2555 for(compno = 0; compno < numcomps; compno++) {
2556 for (i = 0; i < w * h; i++) {
2558 if (!fread(&temp, 1, 1, f)) {
2559 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2563 if (!fread(&temp, 1, 1, f)) {
2564 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2568 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2573 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2577 if (fread(&ch, 1, 1, f)) {
2578 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2585 int imagetoraw(opj_image_t * image, const char *outfile)
2587 FILE *rawFile = NULL;
2593 if((image->numcomps * image->x1 * image->y1) == 0)
2595 fprintf(stderr,"\nError: invalid raw image parameters\n");
2599 rawFile = fopen(outfile, "wb");
2601 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2605 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2607 for(compno = 0; compno < image->numcomps; compno++)
2609 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2610 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2612 w = image->comps[compno].w;
2613 h = image->comps[compno].h;
2615 if(image->comps[compno].prec <= 8)
2617 if(image->comps[compno].sgnd == 1)
2620 int mask = (1 << image->comps[compno].prec) - 1;
2621 ptr = image->comps[compno].data;
2622 for (line = 0; line < h; line++) {
2623 for(row = 0; row < w; row++) {
2624 curr = (signed char) (*ptr & mask);
2625 fwrite(&curr, sizeof(signed char), 1, rawFile);
2630 else if(image->comps[compno].sgnd == 0)
2633 int mask = (1 << image->comps[compno].prec) - 1;
2634 ptr = image->comps[compno].data;
2635 for (line = 0; line < h; line++) {
2636 for(row = 0; row < w; row++) {
2637 curr = (unsigned char) (*ptr & mask);
2638 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2644 else if(image->comps[compno].prec <= 16)
2646 if(image->comps[compno].sgnd == 1)
2648 signed short int curr;
2649 int mask = (1 << image->comps[compno].prec) - 1;
2650 ptr = image->comps[compno].data;
2651 for (line = 0; line < h; line++) {
2652 for(row = 0; row < w; row++) {
2654 curr = (signed short int) (*ptr & mask);
2655 temp = (unsigned char) (curr >> 8);
2656 fwrite(&temp, 1, 1, rawFile);
2657 temp = (unsigned char) curr;
2658 fwrite(&temp, 1, 1, rawFile);
2663 else if(image->comps[compno].sgnd == 0)
2665 unsigned short int curr;
2666 int mask = (1 << image->comps[compno].prec) - 1;
2667 ptr = image->comps[compno].data;
2668 for (line = 0; line < h; line++) {
2669 for(row = 0; row < w; row++) {
2671 curr = (unsigned short int) (*ptr & mask);
2672 temp = (unsigned char) (curr >> 8);
2673 fwrite(&temp, 1, 1, rawFile);
2674 temp = (unsigned char) curr;
2675 fwrite(&temp, 1, 1, rawFile);
2681 else if (image->comps[compno].prec <= 32)
2683 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2688 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2698 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
2699 #define MAGIC_SIZE 8
2700 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
2702 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
2706 double gamma, display_exponent;
2707 int bit_depth, interlace_type,compression_type, filter_type;
2709 png_uint_32 resx, resy;
2711 png_uint_32 width, height;
2712 int color_type, has_alpha, is16;
2715 unsigned char **rows;
2718 opj_image_cmptparm_t cmptparm[4];
2720 unsigned int nr_comp;
2722 unsigned char sigbuf[8];
2724 if((reader = fopen(read_idf, "rb")) == NULL)
2726 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
2729 image = NULL; png = NULL; rows = NULL;
2731 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
2732 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
2734 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
2737 /* libpng-VERSION/example.c:
2738 * PC : screen_gamma = 2.2;
2739 * Mac: screen_gamma = 1.7 or 1.0;
2741 display_exponent = 2.2;
2743 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
2744 NULL, NULL, NULL)) == NULL)
2746 if((info = png_create_info_struct(png)) == NULL)
2749 if(setjmp(png_jmpbuf(png)))
2752 png_init_io(png, reader);
2753 png_set_sig_bytes(png, MAGIC_SIZE);
2755 png_read_info(png, info);
2757 if(png_get_IHDR(png, info, &width, &height,
2758 &bit_depth, &color_type, &interlace_type,
2759 &compression_type, &filter_type) == 0)
2762 /* png_set_expand():
2763 * expand paletted images to RGB, expand grayscale images of
2764 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
2765 * to alpha channels.
2767 if(color_type == PNG_COLOR_TYPE_PALETTE)
2768 png_set_expand(png);
2770 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2771 png_set_expand(png);
2773 if(png_get_valid(png, info, PNG_INFO_tRNS))
2774 png_set_expand(png);
2776 is16 = (bit_depth == 16);
2778 /* GRAY => RGB; GRAY_ALPHA => RGBA
2780 if(color_type == PNG_COLOR_TYPE_GRAY
2781 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2783 png_set_gray_to_rgb(png);
2785 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
2786 PNG_COLOR_TYPE_RGB_ALPHA);
2788 if( !png_get_gAMA(png, info, &gamma))
2791 png_set_gamma(png, display_exponent, gamma);
2793 png_read_update_info(png, info);
2795 png_get_pHYs(png, info, &resx, &resy, &unit);
2797 color_type = png_get_color_type(png, info);
2799 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
2801 nr_comp = 3 + has_alpha;
2803 bit_depth = png_get_bit_depth(png, info);
2805 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
2806 for(i = 0; i < height; ++i)
2807 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
2809 png_read_image(png, rows);
2811 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
2813 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
2815 for(i = 0; i < nr_comp; ++i)
2817 cmptparm[i].prec = bit_depth;
2818 /* bits_per_pixel: 8 or 16 */
2819 cmptparm[i].bpp = bit_depth;
2820 cmptparm[i].sgnd = 0;
2821 cmptparm[i].dx = sub_dx;
2822 cmptparm[i].dy = sub_dy;
2823 cmptparm[i].w = width;
2824 cmptparm[i].h = height;
2827 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
2829 if(image == NULL) goto fin;
2831 image->x0 = params->image_offset_x0;
2832 image->y0 = params->image_offset_y0;
2833 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
2834 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
2836 r = image->comps[0].data;
2837 g = image->comps[1].data;
2838 b = image->comps[2].data;
2839 a = image->comps[3].data;
2841 for(i = 0; i < height; ++i)
2845 for(j = 0; j < width; ++j)
2849 *r++ = s[0]<<8|s[1]; s += 2;
2851 *g++ = s[0]<<8|s[1]; s += 2;
2853 *b++ = s[0]<<8|s[1]; s += 2;
2855 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
2859 *r++ = *s++; *g++ = *s++; *b++ = *s++;
2861 if(has_alpha) *a++ = *s++;
2867 for(i = 0; i < height; ++i)
2872 png_destroy_read_struct(&png, &info, NULL);
2880 int imagetopng(opj_image_t * image, const char *write_idf)
2885 int *red, *green, *blue, *alpha;
2886 unsigned char *row_buf, *d;
2887 int has_alpha, width, height, nr_comp, color_type;
2888 int adjustR, adjustG, adjustB, x, y, fails, is16, force16;
2889 int opj_prec, prec, ushift, dshift;
2890 unsigned short mask = 0xffff;
2891 png_color_8 sig_bit;
2893 is16 = force16 = ushift = dshift = 0; fails = 1;
2894 prec = opj_prec = image->comps[0].prec;
2896 if(prec > 8 && prec < 16)
2898 prec = 16; force16 = 1;
2900 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
2902 fprintf(stderr,"imagetopng: can not create %s"
2903 "\n\twrong bit_depth %d\n", write_idf, prec);
2906 writer = fopen(write_idf, "wb");
2908 if(writer == NULL) return fails;
2910 info = NULL; has_alpha = 0;
2912 /* Create and initialize the png_struct with the desired error handler
2913 * functions. If you want to use the default stderr and longjump method,
2914 * you can supply NULL for the last three parameters. We also check that
2915 * the library version is compatible with the one used at compile time,
2916 * in case we are using dynamically linked libraries. REQUIRED.
2918 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
2920 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
2922 if(png == NULL) goto fin;
2924 /* Allocate/initialize the image information data. REQUIRED
2926 info = png_create_info_struct(png);
2928 if(info == NULL) goto fin;
2930 /* Set error handling. REQUIRED if you are not supplying your own
2931 * error handling functions in the png_create_write_struct() call.
2933 if(setjmp(png_jmpbuf(png))) goto fin;
2935 /* I/O initialization functions is REQUIRED
2937 png_init_io(png, writer);
2939 /* Set the image information here. Width and height are up to 2^31,
2940 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
2941 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
2942 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
2943 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
2944 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
2945 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
2948 png_set_compression_level(png, Z_BEST_COMPRESSION);
2950 if(prec == 16) mask = 0xffff;
2952 if(prec == 8) mask = 0x00ff;
2954 if(prec == 4) mask = 0x000f;
2956 if(prec == 2) mask = 0x0003;
2958 if(prec == 1) mask = 0x0001;
2960 nr_comp = image->numcomps;
2963 && image->comps[0].dx == image->comps[1].dx
2964 && image->comps[1].dx == image->comps[2].dx
2965 && image->comps[0].dy == image->comps[1].dy
2966 && image->comps[1].dy == image->comps[2].dy
2967 && image->comps[0].prec == image->comps[1].prec
2968 && image->comps[1].prec == image->comps[2].prec)
2972 has_alpha = (nr_comp > 3);
2974 is16 = (prec == 16);
2976 width = image->comps[0].w;
2977 height = image->comps[0].h;
2979 red = image->comps[0].data;
2980 green = image->comps[1].data;
2981 blue = image->comps[2].data;
2983 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
2987 sig_bit.alpha = prec;
2988 alpha = image->comps[3].data;
2989 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
2993 sig_bit.alpha = 0; alpha = NULL;
2994 color_type = PNG_COLOR_TYPE_RGB;
2996 png_set_sBIT(png, info, &sig_bit);
2998 png_set_IHDR(png, info, width, height, prec,
3001 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3003 /*=============================*/
3004 png_write_info(png, info);
3005 /*=============================*/
3008 png_set_packing(png);
3012 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3014 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3015 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3016 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3018 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3020 for(y = 0; y < height; ++y)
3024 for(x = 0; x < width; ++x)
3028 v = *red + adjustR; ++red;
3030 if(force16) { v = (v<<ushift) + (v>>dshift); }
3032 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3034 v = *green + adjustG; ++green;
3036 if(force16) { v = (v<<ushift) + (v>>dshift); }
3038 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3040 v = *blue + adjustB; ++blue;
3042 if(force16) { v = (v<<ushift) + (v>>dshift); }
3044 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3050 if(force16) { v = (v<<ushift) + (v>>dshift); }
3052 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3056 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3057 *d++ = (unsigned char)((*green + adjustG) & mask); ++green;
3058 *d++ = (unsigned char)((*blue + adjustB) & mask); ++blue;
3062 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3066 png_write_row(png, row_buf);
3073 if(nr_comp == 1 /* GRAY */
3074 || ( nr_comp == 2 /* GRAY_ALPHA */
3075 && image->comps[0].dx == image->comps[1].dx
3076 && image->comps[0].dy == image->comps[1].dy
3077 && image->comps[0].prec == image->comps[1].prec))
3081 red = image->comps[0].data;
3085 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3087 sig_bit.gray = prec;
3088 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3090 color_type = PNG_COLOR_TYPE_GRAY;
3094 has_alpha = 1; sig_bit.alpha = prec;
3095 alpha = image->comps[1].data;
3096 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3098 width = image->comps[0].w;
3099 height = image->comps[0].h;
3101 png_set_IHDR(png, info, width, height, sig_bit.gray,
3104 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3106 png_set_sBIT(png, info, &sig_bit);
3107 /*=============================*/
3108 png_write_info(png, info);
3109 /*=============================*/
3110 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3114 png_set_packing(png);
3119 row_buf = (unsigned char*)
3120 malloc(width * nr_comp * sizeof(unsigned short));
3122 for(y = 0; y < height; ++y)
3126 for(x = 0; x < width; ++x)
3128 v = *red + adjustR; ++red;
3130 if(force16) { v = (v<<ushift) + (v>>dshift); }
3132 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3138 if(force16) { v = (v<<ushift) + (v>>dshift); }
3140 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3143 png_write_row(png, row_buf);
3148 else /* prec <= 8 */
3150 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3152 for(y = 0; y < height; ++y)
3156 for(x = 0; x < width; ++x)
3158 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3162 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3166 png_write_row(png, row_buf);
3174 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3177 png_write_end(png, info);
3185 png_destroy_write_struct(&png, &info);
3189 if(fails) remove(write_idf);
3193 #endif /* HAVE_LIBPNG */