2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 #include "opj_apps_config.h"
45 #ifdef OPJ_HAVE_LIBTIFF
47 #endif /* OPJ_HAVE_LIBTIFF */
49 #ifdef OPJ_HAVE_LIBPNG
52 #endif /* OPJ_HAVE_LIBPNG */
58 * Get logarithm of an integer and round downwards.
62 static int int_floorlog2(int a)
65 for (l = 0; a > 1; l++) {
71 /* -->> -->> -->> -->>
75 <<-- <<-- <<-- <<-- */
77 #ifdef INFORMATION_ONLY
78 /* TGA header definition. */
80 unsigned char id_length; /* Image id field length */
81 unsigned char colour_map_type; /* Colour map type */
82 unsigned char image_type; /* Image type */
84 ** Colour map specification
86 unsigned short colour_map_index; /* First entry index */
87 unsigned short colour_map_length; /* Colour map length */
88 unsigned char colour_map_entry_size; /* Colour map entry size */
90 ** Image specification
92 unsigned short x_origin; /* x origin of image */
93 unsigned short y_origin; /* u origin of image */
94 unsigned short image_width; /* Image width */
95 unsigned short image_height; /* Image height */
96 unsigned char pixel_depth; /* Pixel depth */
97 unsigned char image_desc; /* Image descriptor */
99 #endif /* INFORMATION_ONLY */
101 static unsigned short get_ushort(unsigned short val)
104 #ifdef OPJ_BIG_ENDIAN
105 return (((val & 0xff) << 8) + (val >> 8));
112 #define TGA_HEADER_SIZE 18
114 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
115 unsigned int *width, unsigned int *height, int *flip_image)
119 unsigned char id_len, cmap_type, image_type;
120 unsigned char pixel_depth, image_desc;
121 unsigned short cmap_index, cmap_len, cmap_entry_size;
122 unsigned short x_origin, y_origin, image_w, image_h;
124 if (!bits_per_pixel || !width || !height || !flip_image) {
127 tga = (unsigned char*)malloc(18);
129 if (fread(tga, TGA_HEADER_SIZE, 1, fp) != 1) {
131 "\nError: fread return a number of element different from the expected.\n");
134 id_len = (unsigned char)tga[0];
135 cmap_type = (unsigned char)tga[1];
136 image_type = (unsigned char)tga[2];
137 cmap_index = get_ushort(*(unsigned short*)(&tga[3]));
138 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
139 cmap_entry_size = (unsigned char)tga[7];
142 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
143 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
144 image_w = get_ushort(*(unsigned short*)(&tga[12]));
145 image_h = get_ushort(*(unsigned short*)(&tga[14]));
146 pixel_depth = (unsigned char)tga[16];
147 image_desc = (unsigned char)tga[17];
151 *bits_per_pixel = (unsigned int)pixel_depth;
152 *width = (unsigned int)image_w;
153 *height = (unsigned int)image_h;
155 /* Ignore tga identifier, if present ... */
157 unsigned char *id = (unsigned char *) malloc(id_len);
158 if (!fread(id, id_len, 1, fp)) {
160 "\nError: fread return a number of element different from the expected.\n");
167 /* Test for compressed formats ... not yet supported ...
168 // Note :- 9 - RLE encoded palettized.
169 // 10 - RLE encoded RGB. */
170 if (image_type > 8) {
171 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
175 *flip_image = !(image_desc & 32);
177 /* Palettized formats are not yet supported, skip over the palette, if present ... */
178 palette_size = cmap_len * (cmap_entry_size / 8);
180 if (palette_size > 0) {
181 fprintf(stderr, "File contains a palette - not yet supported.");
182 fseek(fp, palette_size, SEEK_CUR);
187 #ifdef OPJ_BIG_ENDIAN
189 static inline uint16_t swap16(uint16_t x)
191 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
196 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
199 unsigned short image_w, image_h, us0;
200 unsigned char uc0, image_type;
201 unsigned char pixel_depth, image_desc;
203 if (!bits_per_pixel || !width || !height) {
209 if (bits_per_pixel < 256) {
210 pixel_depth = (unsigned char)bits_per_pixel;
212 fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header");
217 if (fwrite(&uc0, 1, 1, fp) != 1) {
218 goto fails; /* id_length */
220 if (fwrite(&uc0, 1, 1, fp) != 1) {
221 goto fails; /* colour_map_type */
224 image_type = 2; /* Uncompressed. */
225 if (fwrite(&image_type, 1, 1, fp) != 1) {
230 if (fwrite(&us0, 2, 1, fp) != 1) {
231 goto fails; /* colour_map_index */
233 if (fwrite(&us0, 2, 1, fp) != 1) {
234 goto fails; /* colour_map_length */
236 if (fwrite(&uc0, 1, 1, fp) != 1) {
237 goto fails; /* colour_map_entry_size */
240 if (fwrite(&us0, 2, 1, fp) != 1) {
241 goto fails; /* x_origin */
243 if (fwrite(&us0, 2, 1, fp) != 1) {
244 goto fails; /* y_origin */
247 image_w = (unsigned short)width;
248 image_h = (unsigned short) height;
250 #ifndef OPJ_BIG_ENDIAN
251 if (fwrite(&image_w, 2, 1, fp) != 1) {
254 if (fwrite(&image_h, 2, 1, fp) != 1) {
258 image_w = swap16(image_w);
259 image_h = swap16(image_h);
260 if (fwrite(&image_w, 2, 1, fp) != 1) {
263 if (fwrite(&image_h, 2, 1, fp) != 1) {
268 if (fwrite(&pixel_depth, 1, 1, fp) != 1) {
272 image_desc = 8; /* 8 bits per component. */
277 if (fwrite(&image_desc, 1, 1, fp) != 1) {
284 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
288 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters)
292 unsigned int image_width, image_height, pixel_bit_depth;
295 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
297 OPJ_COLOR_SPACE color_space;
300 int subsampling_dx, subsampling_dy;
303 f = fopen(filename, "rb");
305 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
309 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height,
314 /* We currently only support 24 & 32 bit tga's ... */
315 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
319 /* initialize image components */
320 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
322 mono = (pixel_bit_depth == 8) ||
323 (pixel_bit_depth == 16); /* Mono with & without alpha. */
324 save_alpha = (pixel_bit_depth == 16) ||
325 (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
328 color_space = CLRSPC_GRAY;
329 numcomps = save_alpha ? 2 : 1;
331 numcomps = save_alpha ? 4 : 3;
332 color_space = CLRSPC_SRGB;
335 subsampling_dx = parameters->subsampling_dx;
336 subsampling_dy = parameters->subsampling_dy;
338 for (i = 0; i < numcomps; i++) {
339 cmptparm[i].prec = 8;
341 cmptparm[i].sgnd = 0;
342 cmptparm[i].dx = subsampling_dx;
343 cmptparm[i].dy = subsampling_dy;
344 cmptparm[i].w = image_width;
345 cmptparm[i].h = image_height;
348 /* create the image */
349 image = opj_image_create(numcomps, &cmptparm[0], color_space);
355 /* set image offset and reference grid */
356 image->x0 = parameters->image_offset_x0;
357 image->y0 = parameters->image_offset_y0;
358 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 +
359 (image_width - 1) * subsampling_dx + 1;
360 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 +
361 (image_height - 1) * subsampling_dy + 1;
364 for (y = 0; y < image_height; y++) {
368 index = (image_height - y - 1) * image_width;
370 index = y * image_width;
374 for (x = 0; x < image_width; x++) {
375 unsigned char r, g, b;
377 if (!fread(&b, 1, 1, f)) {
379 "\nError: fread return a number of element different from the expected.\n");
380 opj_image_destroy(image);
383 if (!fread(&g, 1, 1, f)) {
385 "\nError: fread return a number of element different from the expected.\n");
386 opj_image_destroy(image);
389 if (!fread(&r, 1, 1, f)) {
391 "\nError: fread return a number of element different from the expected.\n");
392 opj_image_destroy(image);
396 image->comps[0].data[index] = r;
397 image->comps[1].data[index] = g;
398 image->comps[2].data[index] = b;
401 } else if (numcomps == 4) {
402 for (x = 0; x < image_width; x++) {
403 unsigned char r, g, b, a;
404 if (!fread(&b, 1, 1, f)) {
406 "\nError: fread return a number of element different from the expected.\n");
407 opj_image_destroy(image);
410 if (!fread(&g, 1, 1, f)) {
412 "\nError: fread return a number of element different from the expected.\n");
413 opj_image_destroy(image);
416 if (!fread(&r, 1, 1, f)) {
418 "\nError: fread return a number of element different from the expected.\n");
419 opj_image_destroy(image);
422 if (!fread(&a, 1, 1, f)) {
424 "\nError: fread return a number of element different from the expected.\n");
425 opj_image_destroy(image);
429 image->comps[0].data[index] = r;
430 image->comps[1].data[index] = g;
431 image->comps[2].data[index] = b;
432 image->comps[3].data[index] = a;
436 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
442 int imagetotga(opj_image_t * image, const char *outfile)
444 int width, height, bpp, x, y;
445 opj_bool write_alpha;
446 int i, adjustR, adjustG, adjustB;
447 unsigned int alpha_channel;
454 fdest = fopen(outfile, "wb");
456 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
460 for (i = 0; i < image->numcomps - 1; i++) {
461 if ((image->comps[0].dx != image->comps[i + 1].dx)
462 || (image->comps[0].dy != image->comps[i + 1].dy)
463 || (image->comps[0].prec != image->comps[i + 1].prec)) {
465 "Unable to create a tga file with such J2K image charateristics.");
470 width = image->comps[0].w;
471 height = image->comps[0].h;
473 /* Mono with alpha, or RGB with alpha. */
474 write_alpha = (image->numcomps == 2) || (image->numcomps == 4);
476 /* Write TGA header */
477 bpp = write_alpha ? 32 : 24;
478 if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
482 alpha_channel = image->numcomps - 1;
484 scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
486 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
487 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
488 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
490 for (y = 0; y < height; y++) {
491 unsigned int index = y * width;
493 for (x = 0; x < width; x++, index++) {
494 r = (float)(image->comps[0].data[index] + adjustR);
496 if (image->numcomps > 2) {
497 g = (float)(image->comps[1].data[index] + adjustG);
498 b = (float)(image->comps[2].data[index] + adjustB);
499 } else { /* Greyscale ... */
504 /* TGA format writes BGR ... */
505 value = (unsigned char)(b * scale);
506 res = fwrite(&value, 1, 1, fdest);
508 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
512 value = (unsigned char)(g * scale);
513 res = fwrite(&value, 1, 1, fdest);
515 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
519 value = (unsigned char)(r * scale);
520 res = fwrite(&value, 1, 1, fdest);
522 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
527 a = (float)(image->comps[alpha_channel].data[index]);
528 value = (unsigned char)(a * scale);
529 res = fwrite(&value, 1, 1, fdest);
531 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
541 /* -->> -->> -->> -->>
545 <<-- <<-- <<-- <<-- */
547 /* WORD defines a two byte word */
548 typedef unsigned short int WORD;
550 /* DWORD defines a four byte word */
551 typedef unsigned int DWORD;
554 WORD bfType; /* 'BM' for Bitmap (19776) */
555 DWORD bfSize; /* Size of the file */
556 WORD bfReserved1; /* Reserved : 0 */
557 WORD bfReserved2; /* Reserved : 0 */
558 DWORD bfOffBits; /* Offset */
559 } BITMAPFILEHEADER_t;
562 DWORD biSize; /* Size of the structure in bytes */
563 DWORD biWidth; /* Width of the image in pixels */
564 DWORD biHeight; /* Heigth of the image in pixels */
565 WORD biPlanes; /* 1 */
566 WORD biBitCount; /* Number of color bits by pixels */
567 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
568 DWORD biSizeImage; /* Size of the image in bytes */
569 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
570 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
571 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
572 DWORD biClrImportant; /* Number of important color (0: ALL) */
573 } BITMAPINFOHEADER_t;
575 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
577 int subsampling_dx = parameters->subsampling_dx;
578 int subsampling_dy = parameters->subsampling_dy;
580 int i, numcomps, w, h;
581 OPJ_COLOR_SPACE color_space;
582 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
583 opj_image_t * image = NULL;
586 BITMAPFILEHEADER_t File_h;
587 BITMAPINFOHEADER_t Info_h;
589 unsigned char *table_R, *table_G, *table_B;
590 unsigned int j, PAD = 0;
597 IN = fopen(filename, "rb");
599 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
603 File_h.bfType = getc(IN);
604 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
606 if (File_h.bfType != 19778) {
607 fprintf(stderr, "Error, not a BMP file!\n");
613 File_h.bfSize = getc(IN);
614 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
615 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
616 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
618 File_h.bfReserved1 = getc(IN);
619 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
621 File_h.bfReserved2 = getc(IN);
622 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
624 File_h.bfOffBits = getc(IN);
625 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
626 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
627 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
632 Info_h.biSize = getc(IN);
633 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
634 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
635 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
637 if (Info_h.biSize != 40) {
638 fprintf(stderr, "Error, unknown BMP header size %d\n", Info_h.biSize);
642 Info_h.biWidth = getc(IN);
643 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
644 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
645 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
648 Info_h.biHeight = getc(IN);
649 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
650 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
651 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
654 Info_h.biPlanes = getc(IN);
655 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
657 Info_h.biBitCount = getc(IN);
658 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
660 Info_h.biCompression = getc(IN);
661 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
662 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
663 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
665 Info_h.biSizeImage = getc(IN);
666 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
667 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
668 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
670 Info_h.biXpelsPerMeter = getc(IN);
671 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
672 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
673 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
675 Info_h.biYpelsPerMeter = getc(IN);
676 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
677 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
678 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
680 Info_h.biClrUsed = getc(IN);
681 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
682 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
683 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
685 Info_h.biClrImportant = getc(IN);
686 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
687 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
688 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
690 /* Read the data and store them in the OUT file */
692 if (Info_h.biBitCount == 24) {
694 color_space = CLRSPC_SRGB;
695 /* initialize image components */
696 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
697 for (i = 0; i < numcomps; i++) {
698 cmptparm[i].prec = 8;
700 cmptparm[i].sgnd = 0;
701 cmptparm[i].dx = subsampling_dx;
702 cmptparm[i].dy = subsampling_dy;
706 /* create the image */
707 image = opj_image_create(numcomps, &cmptparm[0], color_space);
713 /* set image offset and reference grid */
714 image->x0 = parameters->image_offset_x0;
715 image->y0 = parameters->image_offset_y0;
716 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
717 (w - 1) * subsampling_dx + 1;
718 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
719 (h - 1) * subsampling_dy + 1;
723 /* Place the cursor at the beginning of the image information */
724 fseek(IN, 0, SEEK_SET);
725 fseek(IN, File_h.bfOffBits, SEEK_SET);
730 /* PAD = 4 - (3 * W) % 4; */
731 /* PAD = (PAD == 4) ? 0 : PAD; */
732 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
734 RGB = (unsigned char *)
735 malloc((3 * W + PAD) * H * sizeof(unsigned char));
737 if (fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H,
738 IN) != (3 * W + PAD) * H) {
740 opj_image_destroy(image);
742 "\nError: fread return a number of element different from the expected.\n");
748 for (y = 0; y < (int)H; y++) {
749 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
750 for (x = 0; x < (int)W; x++) {
751 unsigned char *pixel = &scanline[3 * x];
752 image->comps[0].data[index] = pixel[2]; /* R */
753 image->comps[1].data[index] = pixel[1]; /* G */
754 image->comps[2].data[index] = pixel[0]; /* B */
759 }/* if (Info_h.biBitCount == 24) */
760 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { /*RGB */
761 if (Info_h.biClrUsed == 0) {
762 Info_h.biClrUsed = 256;
763 } else if (Info_h.biClrUsed > 256) {
764 Info_h.biClrUsed = 256;
767 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
768 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
769 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
772 for (j = 0; j < Info_h.biClrUsed; j++) {
773 table_B[j] = (unsigned char)getc(IN);
774 table_G[j] = (unsigned char)getc(IN);
775 table_R[j] = (unsigned char)getc(IN);
778 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
784 /* Place the cursor at the beginning of the image information */
785 fseek(IN, 0, SEEK_SET);
786 fseek(IN, File_h.bfOffBits, SEEK_SET);
790 if (Info_h.biWidth % 2) {
794 numcomps = gray_scale ? 1 : 3;
795 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
796 /* initialize image components */
797 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
798 for (i = 0; i < numcomps; i++) {
799 cmptparm[i].prec = 8;
801 cmptparm[i].sgnd = 0;
802 cmptparm[i].dx = subsampling_dx;
803 cmptparm[i].dy = subsampling_dy;
807 /* create the image */
808 image = opj_image_create(numcomps, &cmptparm[0], color_space);
817 /* set image offset and reference grid */
818 image->x0 = parameters->image_offset_x0;
819 image->y0 = parameters->image_offset_y0;
820 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
821 (w - 1) * subsampling_dx + 1;
822 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
823 (h - 1) * subsampling_dy + 1;
827 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
829 if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) {
834 opj_image_destroy(image);
836 "\nError: fread return a number of element different from the expected.\n");
841 for (j = 0; j < W * H; j++) {
842 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
843 image->comps[0].data[index] =
844 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
851 for (j = 0; j < W * H; j++) {
852 if ((j % W < W - 1 && Info_h.biWidth % 2)
853 || !(Info_h.biWidth % 2)) {
854 unsigned char pixel_index =
855 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
856 image->comps[0].data[index] = table_R[pixel_index];
857 image->comps[1].data[index] = table_G[pixel_index];
858 image->comps[2].data[index] = table_B[pixel_index];
868 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/
869 unsigned char *pix, *beyond;
870 int *gray, *red, *green, *blue;
871 unsigned int x, y, max;
875 if (Info_h.biClrUsed == 0) {
876 Info_h.biClrUsed = 256;
877 } else if (Info_h.biClrUsed > 256) {
878 Info_h.biClrUsed = 256;
881 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
882 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
883 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
886 for (j = 0; j < Info_h.biClrUsed; j++) {
887 table_B[j] = (unsigned char)getc(IN);
888 table_G[j] = (unsigned char)getc(IN);
889 table_R[j] = (unsigned char)getc(IN);
891 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
898 numcomps = gray_scale ? 1 : 3;
899 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
900 /* initialize image components */
901 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
902 for (i = 0; i < numcomps; i++) {
903 cmptparm[i].prec = 8;
905 cmptparm[i].sgnd = 0;
906 cmptparm[i].dx = subsampling_dx;
907 cmptparm[i].dy = subsampling_dy;
911 /* create the image */
912 image = opj_image_create(numcomps, &cmptparm[0], color_space);
921 /* set image offset and reference grid */
922 image->x0 = parameters->image_offset_x0;
923 image->y0 = parameters->image_offset_y0;
924 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
925 - 1) * subsampling_dx + 1;
926 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
927 - 1) * subsampling_dy + 1;
931 /* Place the cursor at the beginning of the image information */
932 fseek(IN, 0, SEEK_SET);
933 fseek(IN, File_h.bfOffBits, SEEK_SET);
937 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
938 beyond = RGB + W * H;
948 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++) {
949 *pix = (unsigned char)c1;
954 if (c == 0x00) { /* EOL */
957 pix = RGB + x + (H - y - 1) * W;
958 } else if (c == 0x01) { /* EOP */
960 } else if (c == 0x02) { /* MOVE by dxdy */
965 pix = RGB + (H - y - 1) * W + x;
966 } else { /* 03 .. 255 */
968 for (; i < c && x < W && pix < beyond; i++, x++, pix++) {
970 *pix = (unsigned char)c1;
972 if (c & 1) { /* skip padding byte */
980 gray = image->comps[0].data;
987 *gray++ = table_R[uc];
990 /*int *red, *green, *blue;*/
992 red = image->comps[0].data;
993 green = image->comps[1].data;
994 blue = image->comps[2].data;
1001 *red++ = table_R[uc];
1002 *green++ = table_G[uc];
1003 *blue++ = table_B[uc];
1013 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1014 "is not yet implemented [%d]\n", Info_h.biBitCount);
1020 int imagetobmp(opj_image_t * image, const char *outfile)
1025 int adjustR, adjustG, adjustB;
1027 if (image->comps[0].prec < 8) {
1028 fprintf(stderr, "Unsupported precision: %d\n", image->comps[0].prec);
1031 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1032 && image->comps[1].dx == image->comps[2].dx
1033 && image->comps[0].dy == image->comps[1].dy
1034 && image->comps[1].dy == image->comps[2].dy
1035 && image->comps[0].prec == image->comps[1].prec
1036 && image->comps[1].prec == image->comps[2].prec) {
1038 /* -->> -->> -->> -->>
1040 <<-- <<-- <<-- <<-- */
1042 fdest = fopen(outfile, "wb");
1044 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1048 w = image->comps[0].w;
1049 h = image->comps[0].h;
1051 fprintf(fdest, "BM");
1055 fprintf(fdest, "%c%c%c%c",
1056 (unsigned char)(h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1057 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1058 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1059 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1060 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1061 ((0) >> 24) & 0xff);
1062 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff,
1063 ((54) >> 24) & 0xff);
1067 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1068 ((40) >> 24) & 0xff);
1069 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1070 (unsigned char)((w) >> 8) & 0xff,
1071 (unsigned char)((w) >> 16) & 0xff,
1072 (unsigned char)((w) >> 24) & 0xff);
1073 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1074 (unsigned char)((h) >> 8) & 0xff,
1075 (unsigned char)((h) >> 16) & 0xff,
1076 (unsigned char)((h) >> 24) & 0xff);
1077 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1078 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1079 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1080 ((0) >> 24) & 0xff);
1081 fprintf(fdest, "%c%c%c%c", (unsigned char)(3 * h * w + 3 * h * (w % 2)) & 0xff,
1082 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1083 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1084 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1085 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1086 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1087 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1088 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1089 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1090 ((0) >> 24) & 0xff);
1091 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1092 ((0) >> 24) & 0xff);
1094 if (image->comps[0].prec > 8) {
1095 adjustR = image->comps[0].prec - 8;
1096 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1097 image->comps[0].prec);
1101 if (image->comps[1].prec > 8) {
1102 adjustG = image->comps[1].prec - 8;
1103 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n",
1104 image->comps[1].prec);
1108 if (image->comps[2].prec > 8) {
1109 adjustB = image->comps[2].prec - 8;
1110 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n",
1111 image->comps[2].prec);
1116 for (i = 0; i < w * h; i++) {
1117 unsigned char rc, gc, bc;
1120 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1121 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1122 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1128 rc = (unsigned char)r;
1130 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1131 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1132 g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
1138 gc = (unsigned char)g;
1140 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1141 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1142 b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
1148 bc = (unsigned char)b;
1150 fprintf(fdest, "%c%c%c", bc, gc, rc);
1152 if ((i + 1) % w == 0) {
1153 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) { /* ADD */
1154 fprintf(fdest, "%c", 0);
1159 } else { /* Gray-scale */
1161 /* -->> -->> -->> -->>
1162 8 bits non code (Gray scale)
1163 <<-- <<-- <<-- <<-- */
1165 fdest = fopen(outfile, "wb");
1166 w = image->comps[0].w;
1167 h = image->comps[0].h;
1169 fprintf(fdest, "BM");
1173 fprintf(fdest, "%c%c%c%c",
1174 (unsigned char)(h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1175 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1176 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1177 (unsigned char)((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1178 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1179 ((0) >> 24) & 0xff);
1180 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1181 ((54 + 1024) >> 16) & 0xff,
1182 ((54 + 1024) >> 24) & 0xff);
1186 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1187 ((40) >> 24) & 0xff);
1188 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1189 (unsigned char)((w) >> 8) & 0xff,
1190 (unsigned char)((w) >> 16) & 0xff,
1191 (unsigned char)((w) >> 24) & 0xff);
1192 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1193 (unsigned char)((h) >> 8) & 0xff,
1194 (unsigned char)((h) >> 16) & 0xff,
1195 (unsigned char)((h) >> 24) & 0xff);
1196 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1197 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1198 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1199 ((0) >> 24) & 0xff);
1200 fprintf(fdest, "%c%c%c%c", (unsigned char)(h * w + h * (w % 2)) & 0xff,
1201 (unsigned char)((h * w + h * (w % 2)) >> 8) & 0xff,
1202 (unsigned char)((h * w + h * (w % 2)) >> 16) & 0xff,
1203 (unsigned char)((h * w + h * (w % 2)) >> 24) & 0xff);
1204 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1205 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1206 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1207 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1208 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1209 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1210 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1211 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1213 if (image->comps[0].prec > 8) {
1214 adjustR = image->comps[0].prec - 8;
1215 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1216 image->comps[0].prec);
1221 for (i = 0; i < 256; i++) {
1222 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1225 for (i = 0; i < w * h; i++) {
1228 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1229 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1230 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1237 fprintf(fdest, "%c", (unsigned char)r);
1239 if ((i + 1) % w == 0) {
1240 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) { /* ADD */
1241 fprintf(fdest, "%c", 0);
1251 /* -->> -->> -->> -->>
1255 <<-- <<-- <<-- <<-- */
1258 static unsigned char readuchar(FILE * f)
1261 if (!fread(&c1, 1, 1, f)) {
1263 "\nError: fread return a number of element different from the expected.\n");
1269 static unsigned short readushort(FILE * f, int bigendian)
1271 unsigned char c1, c2;
1272 if (!fread(&c1, 1, 1, f)) {
1274 "\nError: fread return a number of element different from the expected.\n");
1277 if (!fread(&c2, 1, 1, f)) {
1279 "\nError: fread return a number of element different from the expected.\n");
1283 return (c1 << 8) + c2;
1285 return (c2 << 8) + c1;
1289 static unsigned int readuint(FILE * f, int bigendian)
1291 unsigned char c1, c2, c3, c4;
1292 if (!fread(&c1, 1, 1, f)) {
1294 "\nError: fread return a number of element different from the expected.\n");
1297 if (!fread(&c2, 1, 1, f)) {
1299 "\nError: fread return a number of element different from the expected.\n");
1302 if (!fread(&c3, 1, 1, f)) {
1304 "\nError: fread return a number of element different from the expected.\n");
1307 if (!fread(&c4, 1, 1, f)) {
1309 "\nError: fread return a number of element different from the expected.\n");
1313 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1315 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1319 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
1323 int i, numcomps, max;
1324 OPJ_COLOR_SPACE color_space;
1325 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1326 opj_image_t * image = NULL;
1327 int adjustS, ushift, dshift, force8;
1329 char endian1, endian2, sign;
1334 opj_image_comp_t *comp = NULL;
1337 color_space = CLRSPC_GRAY;
1339 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1343 f = fopen(filename, "rb");
1345 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1349 fseek(f, 0, SEEK_SET);
1350 if (fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1,
1351 &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
1353 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1359 while (signtmp[i] != '\0') {
1360 if (signtmp[i] == '-') {
1367 if (endian1 == 'M' && endian2 == 'L') {
1369 } else if (endian2 == 'M' && endian1 == 'L') {
1372 fprintf(stderr, "Bad pgx header, please check input file\n");
1376 /* initialize image component */
1378 cmptparm.x0 = parameters->image_offset_x0;
1379 cmptparm.y0 = parameters->image_offset_y0;
1380 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
1381 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1382 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
1383 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1393 dshift = prec - ushift;
1394 if (cmptparm.sgnd) {
1395 adjustS = (1 << (prec - 1));
1402 ushift = dshift = force8 = adjustS = 0;
1405 cmptparm.prec = prec;
1406 cmptparm.bpp = prec;
1407 cmptparm.dx = parameters->subsampling_dx;
1408 cmptparm.dy = parameters->subsampling_dy;
1410 /* create the image */
1411 image = opj_image_create(numcomps, &cmptparm, color_space);
1416 /* set image offset and reference grid */
1417 image->x0 = cmptparm.x0;
1418 image->y0 = cmptparm.x0;
1419 image->x1 = cmptparm.w;
1420 image->y1 = cmptparm.h;
1422 /* set image data */
1424 comp = &image->comps[0];
1426 for (i = 0; i < w * h; i++) {
1429 v = readuchar(f) + adjustS;
1430 v = (v << ushift) + (v >> dshift);
1431 comp->data[i] = (unsigned char)v;
1439 if (comp->prec == 8) {
1443 v = (char) readuchar(f);
1445 } else if (comp->prec <= 16) {
1447 v = readushort(f, bigendian);
1449 v = (short) readushort(f, bigendian);
1453 v = readuint(f, bigendian);
1455 v = (int) readuint(f, bigendian);
1464 comp->bpp = int_floorlog2(max) + 1;
1469 int imagetopgx(opj_image_t * image, const char *outfile)
1475 for (compno = 0; compno < image->numcomps; compno++) {
1476 opj_image_comp_t *comp = &image->comps[compno];
1477 char bname[256]; /* buffer for name */
1478 char *name = bname; /* pointer */
1481 const size_t olen = strlen(outfile);
1482 const size_t dotpos = olen - 4;
1483 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1484 if (outfile[dotpos] != '.') {
1485 /* `pgx` was recognized but there is no dot at expected position */
1486 fprintf(stderr, "ERROR -> Impossible happen.");
1490 name = (char*)malloc(total + 1);
1492 strncpy(name, outfile, dotpos);
1493 /*if (image->numcomps > 1) {*/
1494 sprintf(name + dotpos, "_%d.pgx", compno);
1496 strcpy(name+dotpos, ".pgx");
1498 fdest = fopen(name, "wb");
1500 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1503 /* don't need name anymore */
1508 w = image->comps[compno].w;
1509 h = image->comps[compno].h;
1511 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1512 if (comp->prec <= 8) {
1514 } else if (comp->prec <= 16) {
1519 for (i = 0; i < w * h; i++) {
1520 int v = image->comps[compno].data[i];
1521 for (j = nbytes - 1; j >= 0; j--) {
1522 char byte = (char)(v >> (j * 8));
1523 res = fwrite(&byte, 1, 1, fdest);
1525 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1536 /* -->> -->> -->> -->>
1540 <<-- <<-- <<-- <<-- */
1543 int width, height, maxval, depth, format;
1544 char rgb, rgba, gray, graya, bw;
1548 static char *skip_white(char *s)
1551 if (*s == '\n' || *s == '\r') {
1563 static char *skip_int(char *start, int *out_n)
1571 s = skip_white(start);
1585 *out_n = atoi(start);
1590 static char *skip_idf(char *start, char out_idf[256])
1595 s = skip_white(start);
1602 if (isalpha(*s) || *s == '_') {
1610 strncpy(out_idf, start, 255);
1615 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1618 int format, have_wh, end, ttype;
1619 char idf[256], type[256];
1622 if (fgets(line, 250, reader) == NULL) {
1623 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1627 if (line[0] != 'P') {
1628 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1631 format = atoi(line + 1);
1632 if (format < 1 || format > 7) {
1633 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1636 ph->format = format;
1637 ttype = end = have_wh = 0;
1639 while (fgets(line, 250, reader)) {
1647 s = skip_idf(s, idf);
1649 if (s == NULL || *s == 0) {
1653 if (strcmp(idf, "ENDHDR") == 0) {
1657 if (strcmp(idf, "WIDTH") == 0) {
1658 s = skip_int(s, &ph->width);
1659 if (s == NULL || *s == 0) {
1665 if (strcmp(idf, "HEIGHT") == 0) {
1666 s = skip_int(s, &ph->height);
1667 if (s == NULL || *s == 0) {
1673 if (strcmp(idf, "DEPTH") == 0) {
1674 s = skip_int(s, &ph->depth);
1675 if (s == NULL || *s == 0) {
1681 if (strcmp(idf, "MAXVAL") == 0) {
1682 s = skip_int(s, &ph->maxval);
1683 if (s == NULL || *s == 0) {
1689 if (strcmp(idf, "TUPLTYPE") == 0) {
1690 s = skip_idf(s, type);
1691 if (s == NULL || *s == 0) {
1695 if (strcmp(type, "BLACKANDWHITE") == 0) {
1700 if (strcmp(type, "GRAYSCALE") == 0) {
1705 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1710 if (strcmp(type, "RGB") == 0) {
1715 if (strcmp(type, "RGB_ALPHA") == 0) {
1720 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1723 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1725 } /* if(format == 7) */
1728 s = skip_int(s, &ph->width);
1730 s = skip_int(s, &ph->height);
1734 if (format == 1 || format == 4) {
1740 if (format == 2 || format == 3 || format == 5 || format == 6) {
1741 /* P2, P3, P5, P6: */
1742 s = skip_int(s, &ph->maxval);
1744 if (ph->maxval > 65535) {
1749 }/* while(fgets( ) */
1750 if (format == 2 || format == 3 || format > 4) {
1751 if (ph->maxval < 1 || ph->maxval > 65535) {
1755 if (ph->width < 1 || ph->height < 1) {
1761 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1764 if (ph->depth < 1 || ph->depth > 4) {
1768 if (ph->width && ph->height && ph->depth & ph->maxval && ttype) {
1772 if (format != 1 && format != 4) {
1773 if (ph->width && ph->height && ph->maxval) {
1777 if (ph->width && ph->height) {
1785 static int has_prec(int val)
1835 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1837 int subsampling_dx = parameters->subsampling_dx;
1838 int subsampling_dy = parameters->subsampling_dy;
1841 int i, compno, numcomps, w, h, prec, format;
1842 OPJ_COLOR_SPACE color_space;
1843 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1844 opj_image_t * image = NULL;
1845 struct pnm_header header_info;
1847 if ((fp = fopen(filename, "rb")) == NULL) {
1848 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1851 memset(&header_info, 0, sizeof(struct pnm_header));
1853 read_pnm_header(fp, &header_info);
1855 if (!header_info.ok) {
1860 format = header_info.format;
1863 case 1: /* ascii bitmap */
1864 case 4: /* raw bitmap */
1868 case 2: /* ascii greymap */
1869 case 5: /* raw greymap */
1873 case 3: /* ascii pixmap */
1874 case 6: /* raw pixmap */
1878 case 7: /* arbitrary map */
1879 numcomps = header_info.depth;
1887 color_space = CLRSPC_GRAY; /* GRAY, GRAYA */
1889 color_space = CLRSPC_SRGB; /* RGB, RGBA */
1892 prec = has_prec(header_info.maxval);
1898 w = header_info.width;
1899 h = header_info.height;
1900 subsampling_dx = parameters->subsampling_dx;
1901 subsampling_dy = parameters->subsampling_dy;
1903 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1905 for (i = 0; i < numcomps; i++) {
1906 cmptparm[i].prec = prec;
1907 cmptparm[i].bpp = prec;
1908 cmptparm[i].sgnd = 0;
1909 cmptparm[i].dx = subsampling_dx;
1910 cmptparm[i].dy = subsampling_dy;
1914 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1921 /* set image offset and reference grid */
1922 image->x0 = parameters->image_offset_x0;
1923 image->y0 = parameters->image_offset_y0;
1924 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1925 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1927 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1930 for (i = 0; i < w * h; i++) {
1931 for (compno = 0; compno < numcomps; compno++) {
1933 if (fscanf(fp, "%u", &index) != 1) {
1935 "\nWARNING: fscanf return a number of element different from the expected.\n");
1938 image->comps[compno].data[i] = (index * 255) / header_info.maxval;
1941 } else if ((format == 5)
1944 && (header_info.gray || header_info.graya
1945 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1946 unsigned char c0, c1, one;
1950 for (i = 0; i < w * h; i++) {
1951 for (compno = 0; compno < numcomps; compno++) {
1952 if (!fread(&c0, 1, 1, fp)) {
1954 "\nError: fread return a number of element different from the expected.\n");
1957 image->comps[compno].data[i] = c0;
1959 if (!fread(&c1, 1, 1, fp)) {
1961 "\nError: fread return a number of element different from the expected.\n");
1964 image->comps[compno].data[i] = ((c0 << 8) | c1);
1968 } else if (format == 1) { /* ascii bitmap */
1969 for (i = 0; i < w * h; i++) {
1972 if (fscanf(fp, "%u", &index) != 1) {
1974 "\nWARNING: fscanf return a number of element different from the expected.\n");
1977 image->comps[0].data[i] = (index ? 0 : 255);
1979 } else if (format == 4) {
1984 for (y = 0; y < h; ++y) {
1988 for (x = 0; x < w; ++x) {
1991 uc = (unsigned char)getc(fp);
1993 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
1998 } else if ((format == 7 && header_info.bw)) { /*MONO*/
2001 for (i = 0; i < w * h; ++i) {
2002 if (!fread(&uc, 1, 1, fp)) {
2004 "\nError: fread return a number of element different from the expected.\n");
2006 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
2014 int imagetopnm(opj_image_t * image, const char *outfile)
2016 int *red, *green, *blue, *alpha;
2018 int i, compno, ncomp;
2019 int adjustR, adjustG, adjustB, adjustA;
2020 int fails, two, want_gray, has_alpha, triple;
2023 const char *tmp = outfile;
2026 if ((prec = image->comps[0].prec) > 16) {
2027 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
2028 "\n\t: refused.\n", __FILE__, __LINE__, prec);
2031 two = has_alpha = 0;
2033 ncomp = image->numcomps;
2039 want_gray = (*tmp == 'g' || *tmp == 'G');
2040 ncomp = image->numcomps;
2046 if (ncomp == 2 /* GRAYA */
2047 || (ncomp > 2 /* RGB, RGBA */
2048 && image->comps[0].dx == image->comps[1].dx
2049 && image->comps[1].dx == image->comps[2].dx
2050 && image->comps[0].dy == image->comps[1].dy
2051 && image->comps[1].dy == image->comps[2].dy
2052 && image->comps[0].prec == image->comps[1].prec
2053 && image->comps[1].prec == image->comps[2].prec
2055 fdest = fopen(outfile, "wb");
2058 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
2062 triple = (ncomp > 2);
2063 wr = image->comps[0].w;
2064 hr = image->comps[0].h;
2065 max = (1 << prec) - 1;
2066 has_alpha = (ncomp == 4 || ncomp == 2);
2068 red = image->comps[0].data;
2071 green = image->comps[1].data;
2072 blue = image->comps[2].data;
2074 green = blue = NULL;
2078 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
2080 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
2081 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2082 wr, hr, ncomp, max, tt);
2083 alpha = image->comps[ncomp - 1].data;
2084 adjustA = (image->comps[ncomp - 1].sgnd ?
2085 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2087 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2088 opj_version(), wr, hr, max);
2091 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2094 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2095 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2097 adjustG = adjustB = 0;
2100 for (i = 0; i < wr * hr; ++i) {
2105 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2108 v = *green + adjustG;
2111 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2113 v = *blue + adjustB;
2116 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2121 v = *alpha + adjustA;
2124 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2132 fprintf(fdest, "%c", (unsigned char)*red++);
2134 fprintf(fdest, "%c%c", (unsigned char)*green++, (unsigned char)*blue++);
2138 fprintf(fdest, "%c", (unsigned char)*alpha++);
2149 if (image->numcomps > ncomp) {
2150 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2151 fprintf(stderr, " is written to the file\n");
2153 destname = (char*)malloc(strlen(outfile) + 8);
2155 for (compno = 0; compno < ncomp; compno++) {
2157 sprintf(destname, "%d.%s", compno, outfile);
2159 sprintf(destname, "%s", outfile);
2162 fdest = fopen(destname, "wb");
2164 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2168 wr = image->comps[compno].w;
2169 hr = image->comps[compno].h;
2170 prec = image->comps[compno].prec;
2171 max = (1 << prec) - 1;
2173 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2174 opj_version(), wr, hr, max);
2176 red = image->comps[compno].data;
2178 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2181 for (i = 0; i < wr * hr; i++) {
2185 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2190 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2193 } else { /* prec <= 8 */
2194 for (i = 0; i < wr * hr; ++i) {
2195 fprintf(fdest, "%c", (unsigned char)(*red + adjustR));
2206 #ifdef OPJ_HAVE_LIBTIFF
2207 /* -->> -->> -->> -->>
2211 <<-- <<-- <<-- <<-- */
2213 int imagetotif(opj_image_t * image, const char *outfile)
2215 int width, height, imgsize;
2216 int bps, index, adjust, sgnd;
2217 int ushift, dshift, has_alpha, force16;
2223 ushift = dshift = force16 = has_alpha = 0;
2224 bps = image->comps[0].prec;
2226 if (bps > 8 && bps < 16) {
2228 dshift = bps - ushift;
2233 if (bps != 8 && bps != 16) {
2234 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2236 fprintf(stderr, "\tAborting\n");
2239 tif = TIFFOpen(outfile, "wb");
2242 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2245 sgnd = image->comps[0].sgnd;
2246 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2248 if (image->numcomps >= 3
2249 && image->comps[0].dx == image->comps[1].dx
2250 && image->comps[1].dx == image->comps[2].dx
2251 && image->comps[0].dy == image->comps[1].dy
2252 && image->comps[1].dy == image->comps[2].dy
2253 && image->comps[0].prec == image->comps[1].prec
2254 && image->comps[1].prec == image->comps[2].prec) {
2255 has_alpha = (image->numcomps == 4);
2257 width = image->comps[0].w;
2258 height = image->comps[0].h;
2259 imgsize = width * height ;
2261 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2262 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2263 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2264 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2265 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2266 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2267 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2268 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2269 strip_size = TIFFStripSize(tif);
2270 buf = _TIFFmalloc(strip_size);
2273 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2274 unsigned char *dat8;
2275 tsize_t i, ssize, last_i = 0;
2277 ssize = TIFFStripSize(tif);
2278 dat8 = (unsigned char*)buf;
2281 step = 3 + has_alpha;
2284 for (i = 0; i < ssize - restx; i += step) {
2287 if (index < imgsize) {
2288 r = image->comps[0].data[index];
2289 g = image->comps[1].data[index];
2290 b = image->comps[2].data[index];
2292 a = image->comps[3].data[index];
2317 if (last_i < ssize) {
2318 for (i = last_i; i < ssize; i += step) {
2321 if (index < imgsize) {
2322 r = image->comps[0].data[index];
2323 g = image->comps[1].data[index];
2324 b = image->comps[2].data[index];
2326 a = image->comps[3].data[index];
2338 if (i + 1 < ssize) {
2343 if (i + 2 < ssize) {
2349 if (i + 3 < ssize) {
2360 }/*if(last_i < ssize)*/
2363 else if (bps == 16) {
2364 step = 6 + has_alpha + has_alpha;
2367 for (i = 0; i < ssize - restx ; i += step) {
2370 if (index < imgsize) {
2371 r = image->comps[0].data[index];
2372 g = image->comps[1].data[index];
2373 b = image->comps[2].data[index];
2375 a = image->comps[3].data[index];
2387 r = (r << ushift) + (r >> dshift);
2388 g = (g << ushift) + (g >> dshift);
2389 b = (b << ushift) + (b >> dshift);
2391 a = (a << ushift) + (a >> dshift);
2394 dat8[i + 0] = r; /*LSB*/
2395 dat8[i + 1] = (r >> 8); /*MSB*/
2397 dat8[i + 3] = (g >> 8);
2399 dat8[i + 5] = (b >> 8);
2402 dat8[i + 7] = (a >> 8);
2411 if (last_i < ssize) {
2412 for (i = last_i ; i < ssize ; i += step) {
2415 if (index < imgsize) {
2416 r = image->comps[0].data[index];
2417 g = image->comps[1].data[index];
2418 b = image->comps[2].data[index];
2420 a = image->comps[3].data[index];
2432 r = (r << ushift) + (r >> dshift);
2433 g = (g << ushift) + (g >> dshift);
2434 b = (b << ushift) + (b >> dshift);
2436 a = (a << ushift) + (a >> dshift);
2439 dat8[i + 0] = r; /*LSB*/
2440 if (i + 1 < ssize) {
2441 dat8[i + 1] = (r >> 8);
2445 if (i + 2 < ssize) {
2450 if (i + 3 < ssize) {
2451 dat8[i + 3] = (g >> 8);
2455 if (i + 4 < ssize) {
2460 if (i + 5 < ssize) {
2461 dat8[i + 5] = (b >> 8);
2467 if (i + 6 < ssize) {
2472 if (i + 7 < ssize) {
2473 dat8[i + 7] = (a >> 8);
2483 }/*if(last_i < ssize)*/
2486 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2487 }/*for(strip = 0; )*/
2489 _TIFFfree((void*)buf);
2495 if (image->numcomps == 1 /* GRAY */
2496 || (image->numcomps == 2 /* GRAY_ALPHA */
2497 && image->comps[0].dx == image->comps[1].dx
2498 && image->comps[0].dy == image->comps[1].dy
2499 && image->comps[0].prec == image->comps[1].prec)) {
2502 has_alpha = (image->numcomps == 2);
2504 width = image->comps[0].w;
2505 height = image->comps[0].h;
2506 imgsize = width * height;
2509 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2510 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2511 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2512 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2513 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2514 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2515 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2516 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2518 /* Get a buffer for the data */
2519 strip_size = TIFFStripSize(tif);
2520 buf = _TIFFmalloc(strip_size);
2523 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2524 unsigned char *dat8;
2525 tsize_t i, ssize = TIFFStripSize(tif);
2526 dat8 = (unsigned char*)buf;
2529 step = 1 + has_alpha;
2531 for (i = 0; i < ssize; i += step) {
2532 if (index < imgsize) {
2535 r = image->comps[0].data[index];
2537 a = image->comps[1].data[index];
2556 else if (bps == 16) {
2557 step = 2 + has_alpha + has_alpha;
2559 for (i = 0; i < ssize; i += step) {
2560 if (index < imgsize) {
2563 r = image->comps[0].data[index];
2565 a = image->comps[1].data[index];
2575 r = (r << ushift) + (r >> dshift);
2577 a = (a << ushift) + (a >> dshift);
2580 dat8[i + 0] = r; /*LSB*/
2581 dat8[i + 1] = r >> 8; /*MSB*/
2584 dat8[i + 3] = a >> 8;
2587 }/*if(index < imgsize)*/
2593 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2604 fprintf(stderr, "imagetotif: Bad color format.\n"
2605 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2606 fprintf(stderr, "\tFOUND: numcomps(%d)\n\tAborting\n",
2613 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2614 * CINEMA : 12 bit precision
2616 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2618 int subsampling_dx = parameters->subsampling_dx;
2619 int subsampling_dy = parameters->subsampling_dy;
2624 int j, numcomps, w, h, index;
2625 OPJ_COLOR_SPACE color_space;
2626 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2627 opj_image_t *image = NULL;
2630 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2631 unsigned int tiWidth, tiHeight;
2633 tif = TIFFOpen(filename, "r");
2636 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2639 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2640 tiWidth = tiHeight = 0;
2642 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2643 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2644 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2645 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2646 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2647 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2648 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2653 unsigned short b = tiBps, p = tiPhoto;
2655 if (tiBps != 8 && tiBps != 16 && tiBps != 12) {
2658 if (tiPhoto != 1 && tiPhoto != 2) {
2664 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits"
2665 " implemented\n", tiBps);
2667 fprintf(stderr, "tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2668 " and GRAY(A) has been implemented\n", (int) tiPhoto);
2670 fprintf(stderr, "\tAborting\n");
2676 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2678 uint16 extrasamples;
2680 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2681 &extrasamples, &sampleinfo);
2683 if (extrasamples >= 1) {
2684 switch (sampleinfo[0]) {
2685 case EXTRASAMPLE_UNSPECIFIED:
2686 /* Workaround for some images without correct info about alpha channel
2693 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2694 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2698 } else /* extrasamples == 0 */
2699 if (tiSpp == 4 || tiSpp == 2) {
2704 /* initialize image components
2706 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2708 if (tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */
2709 numcomps = 3 + has_alpha;
2710 color_space = CLRSPC_SRGB;
2712 for (j = 0; j < numcomps; j++) {
2713 if (parameters->cp_cinema) {
2714 cmptparm[j].prec = 12;
2715 cmptparm[j].bpp = 12;
2717 cmptparm[j].prec = tiBps;
2718 cmptparm[j].bpp = tiBps;
2720 cmptparm[j].dx = subsampling_dx;
2721 cmptparm[j].dy = subsampling_dy;
2726 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2732 /* set image offset and reference grid
2734 image->x0 = parameters->image_offset_x0;
2735 image->y0 = parameters->image_offset_y0;
2736 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2737 image->x0 + (w - 1) * subsampling_dx + 1;
2738 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2739 image->y0 + (h - 1) * subsampling_dy + 1;
2741 buf = _TIFFmalloc(TIFFStripSize(tif));
2743 strip_size = TIFFStripSize(tif);
2745 imgsize = image->comps[0].w * image->comps[0].h ;
2746 /* Read the Image components
2748 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2749 unsigned char *dat8;
2752 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2753 dat8 = (unsigned char*)buf;
2756 step = 6 + has_alpha + has_alpha;
2758 for (i = 0; i < ssize; i += step) {
2759 if (index < imgsize) {
2760 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0]; /* R */
2761 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2]; /* G */
2762 image->comps[2].data[index] = (dat8[i + 5] << 8) | dat8[i + 4]; /* B */
2764 image->comps[3].data[index] = (dat8[i + 7] << 8) | dat8[i + 6];
2767 if (parameters->cp_cinema) {
2768 /* Rounding 16 to 12 bits
2770 image->comps[0].data[index] =
2771 (image->comps[0].data[index] + 0x08) >> 4 ;
2772 image->comps[1].data[index] =
2773 (image->comps[1].data[index] + 0x08) >> 4 ;
2774 image->comps[2].data[index] =
2775 (image->comps[2].data[index] + 0x08) >> 4 ;
2777 image->comps[3].data[index] =
2778 (image->comps[3].data[index] + 0x08) >> 4 ;
2785 }/*if(tiBps == 16)*/
2786 else if (tiBps == 8) {
2787 step = 3 + has_alpha;
2789 for (i = 0; i < ssize; i += step) {
2790 if (index < imgsize) {
2791 image->comps[0].data[index] = dat8[i + 0]; /* R */
2792 image->comps[1].data[index] = dat8[i + 1]; /* G */
2793 image->comps[2].data[index] = dat8[i + 2]; /* B */
2795 image->comps[3].data[index] = dat8[i + 3];
2798 if (parameters->cp_cinema) {
2799 /* Rounding 8 to 12 bits
2801 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2802 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2803 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2805 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2814 }/*if( tiBps == 8)*/
2815 else if (tiBps == 12) { /* CINEMA file */
2818 for (i = 0; i < ssize; i += step) {
2819 if ((index < imgsize) & (index + 1 < imgsize)) {
2820 image->comps[0].data[index] = (dat8[i + 0] << 4) | (dat8[i + 1] >> 4);
2821 image->comps[1].data[index] = ((dat8[i + 1] & 0x0f) << 8) | dat8[i + 2];
2823 image->comps[2].data[index] = (dat8[i + 3] << 4) | (dat8[i + 4] >> 4);
2824 image->comps[0].data[index + 1] = ((dat8[i + 4] & 0x0f) << 8) | dat8[i + 5];
2826 image->comps[1].data[index + 1] = (dat8[i + 6] << 4) |
2828 image->comps[2].data[index + 1] = ((dat8[i + 7] & 0x0f) << 8) | dat8[i + 8];
2836 }/*for(strip = 0; )*/
2844 if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */
2845 numcomps = 1 + has_alpha;
2846 color_space = CLRSPC_GRAY;
2848 for (j = 0; j < numcomps; ++j) {
2849 cmptparm[j].prec = tiBps;
2850 cmptparm[j].bpp = tiBps;
2851 cmptparm[j].dx = subsampling_dx;
2852 cmptparm[j].dy = subsampling_dy;
2856 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2862 /* set image offset and reference grid
2864 image->x0 = parameters->image_offset_x0;
2865 image->y0 = parameters->image_offset_y0;
2866 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2867 image->x0 + (w - 1) * subsampling_dx + 1;
2868 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2869 image->y0 + (h - 1) * subsampling_dy + 1;
2871 buf = _TIFFmalloc(TIFFStripSize(tif));
2873 strip_size = TIFFStripSize(tif);
2875 imgsize = image->comps[0].w * image->comps[0].h ;
2876 /* Read the Image components
2878 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2879 unsigned char *dat8;
2883 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2884 dat8 = (unsigned char*)buf;
2887 step = 2 + has_alpha + has_alpha;
2889 for (i = 0; i < ssize; i += step) {
2890 if (index < imgsize) {
2891 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0];
2893 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2];
2900 } else if (tiBps == 8) {
2901 step = 1 + has_alpha;
2903 for (i = 0; i < ssize; i += step) {
2904 if (index < imgsize) {
2905 image->comps[0].data[index] = dat8[i + 0];
2907 image->comps[1].data[index] = dat8[i + 1];
2926 #endif /* OPJ_HAVE_LIBTIFF */
2928 /* -->> -->> -->> -->>
2932 <<-- <<-- <<-- <<-- */
2934 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2935 raw_cparameters_t *raw_cp)
2937 int subsampling_dx = parameters->subsampling_dx;
2938 int subsampling_dy = parameters->subsampling_dy;
2941 int i, compno, numcomps, w, h;
2942 OPJ_COLOR_SPACE color_space;
2943 opj_image_cmptparm_t *cmptparm;
2944 opj_image_t * image = NULL;
2947 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2948 raw_cp->rawBitDepth)) == 0) {
2949 fprintf(stderr, "\nError: invalid raw image parameters\n");
2950 fprintf(stderr, "Please use the Format option -F:\n");
2952 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2953 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2954 fprintf(stderr, "Aborting\n");
2958 f = fopen(filename, "rb");
2960 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2961 fprintf(stderr, "Aborting\n");
2964 numcomps = raw_cp->rawComp;
2965 color_space = CLRSPC_SRGB;
2966 w = raw_cp->rawWidth;
2967 h = raw_cp->rawHeight;
2968 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(
2969 opj_image_cmptparm_t));
2971 /* initialize image components */
2972 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2973 for (i = 0; i < numcomps; i++) {
2974 cmptparm[i].prec = raw_cp->rawBitDepth;
2975 cmptparm[i].bpp = raw_cp->rawBitDepth;
2976 cmptparm[i].sgnd = raw_cp->rawSigned;
2977 cmptparm[i].dx = subsampling_dx;
2978 cmptparm[i].dy = subsampling_dy;
2982 /* create the image */
2983 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2988 /* set image offset and reference grid */
2989 image->x0 = parameters->image_offset_x0;
2990 image->y0 = parameters->image_offset_y0;
2991 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2992 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2994 if (raw_cp->rawBitDepth <= 8) {
2995 unsigned char value = 0;
2996 for (compno = 0; compno < numcomps; compno++) {
2997 for (i = 0; i < w * h; i++) {
2998 if (!fread(&value, 1, 1, f)) {
2999 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3003 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
3006 } else if (raw_cp->rawBitDepth <= 16) {
3007 unsigned short value;
3008 for (compno = 0; compno < numcomps; compno++) {
3009 for (i = 0; i < w * h; i++) {
3011 if (!fread(&temp, 1, 1, f)) {
3012 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3017 if (!fread(&temp, 1, 1, f)) {
3018 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3023 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
3028 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3033 if (fread(&ch, 1, 1, f)) {
3034 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
3041 int imagetoraw(opj_image_t * image, const char *outfile)
3043 FILE *rawFile = NULL;
3050 if ((image->numcomps * image->x1 * image->y1) == 0) {
3051 fprintf(stderr, "\nError: invalid raw image parameters\n");
3055 rawFile = fopen(outfile, "wb");
3057 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3061 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
3063 for (compno = 0; compno < image->numcomps; compno++) {
3064 fprintf(stdout, "Component %d characteristics: %dx%dx%d %s\n", compno,
3065 image->comps[compno].w,
3066 image->comps[compno].h, image->comps[compno].prec,
3067 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
3069 w = image->comps[compno].w;
3070 h = image->comps[compno].h;
3072 if (image->comps[compno].prec <= 8) {
3073 if (image->comps[compno].sgnd == 1) {
3075 int mask = (1 << image->comps[compno].prec) - 1;
3076 ptr = image->comps[compno].data;
3077 for (line = 0; line < h; line++) {
3078 for (row = 0; row < w; row++) {
3079 curr = (signed char)(*ptr & mask);
3080 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
3082 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3088 } else if (image->comps[compno].sgnd == 0) {
3090 int mask = (1 << image->comps[compno].prec) - 1;
3091 ptr = image->comps[compno].data;
3092 for (line = 0; line < h; line++) {
3093 for (row = 0; row < w; row++) {
3094 curr = (unsigned char)(*ptr & mask);
3095 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
3097 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3104 } else if (image->comps[compno].prec <= 16) {
3105 if (image->comps[compno].sgnd == 1) {
3106 signed short int curr;
3107 int mask = (1 << image->comps[compno].prec) - 1;
3108 ptr = image->comps[compno].data;
3109 for (line = 0; line < h; line++) {
3110 for (row = 0; row < w; row++) {
3112 curr = (signed short int)(*ptr & mask);
3113 temp = (unsigned char)(curr >> 8);
3114 res = fwrite(&temp, 1, 1, rawFile);
3116 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3119 temp = (unsigned char) curr;
3120 res = fwrite(&temp, 1, 1, rawFile);
3122 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3128 } else if (image->comps[compno].sgnd == 0) {
3129 unsigned short int curr;
3130 int mask = (1 << image->comps[compno].prec) - 1;
3131 ptr = image->comps[compno].data;
3132 for (line = 0; line < h; line++) {
3133 for (row = 0; row < w; row++) {
3135 curr = (unsigned short int)(*ptr & mask);
3136 temp = (unsigned char)(curr >> 8);
3137 res = fwrite(&temp, 1, 1, rawFile);
3139 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3142 temp = (unsigned char) curr;
3143 res = fwrite(&temp, 1, 1, rawFile);
3145 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3152 } else if (image->comps[compno].prec <= 32) {
3153 fprintf(stderr, "More than 16 bits per component no handled yet\n");
3156 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
3164 #ifdef OPJ_HAVE_LIBPNG
3166 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3167 #define MAGIC_SIZE 8
3168 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3170 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3174 double gamma, display_exponent;
3175 int bit_depth, interlace_type, compression_type, filter_type;
3177 png_uint_32 resx, resy;
3179 png_uint_32 width, height;
3180 int color_type, has_alpha, is16;
3183 unsigned char **rows;
3186 opj_image_cmptparm_t cmptparm[4];
3188 unsigned int nr_comp;
3190 unsigned char sigbuf[8];
3192 if ((reader = fopen(read_idf, "rb")) == NULL) {
3193 fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
3200 if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3201 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
3202 fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
3205 /* libpng-VERSION/example.c:
3206 * PC : screen_gamma = 2.2;
3207 * Mac: screen_gamma = 1.7 or 1.0;
3209 display_exponent = 2.2;
3211 if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3212 NULL, NULL, NULL)) == NULL) {
3215 if ((info = png_create_info_struct(png)) == NULL) {
3219 if (setjmp(png_jmpbuf(png))) {
3223 png_init_io(png, reader);
3224 png_set_sig_bytes(png, MAGIC_SIZE);
3226 png_read_info(png, info);
3228 if (png_get_IHDR(png, info, &width, &height,
3229 &bit_depth, &color_type, &interlace_type,
3230 &compression_type, &filter_type) == 0) {
3234 /* png_set_expand():
3235 * expand paletted images to RGB, expand grayscale images of
3236 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3237 * to alpha channels.
3239 if (color_type == PNG_COLOR_TYPE_PALETTE) {
3240 png_set_expand(png);
3241 } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
3242 png_set_expand(png);
3245 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
3246 png_set_expand(png);
3249 is16 = (bit_depth == 16);
3251 /* GRAY => RGB; GRAY_ALPHA => RGBA
3253 if (color_type == PNG_COLOR_TYPE_GRAY
3254 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
3255 png_set_gray_to_rgb(png);
3257 (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
3258 PNG_COLOR_TYPE_RGB_ALPHA);
3260 if (!png_get_gAMA(png, info, &gamma)) {
3264 png_set_gamma(png, display_exponent, gamma);
3266 png_read_update_info(png, info);
3268 png_get_pHYs(png, info, &resx, &resy, &unit);
3270 color_type = png_get_color_type(png, info);
3272 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3274 nr_comp = 3 + has_alpha;
3276 bit_depth = png_get_bit_depth(png, info);
3278 rows = (unsigned char**)calloc(height + 1, sizeof(unsigned char*));
3279 for (i = 0; i < height; ++i) {
3280 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png, info));
3283 png_read_image(png, rows);
3285 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3287 sub_dx = params->subsampling_dx;
3288 sub_dy = params->subsampling_dy;
3290 for (i = 0; i < nr_comp; ++i) {
3291 cmptparm[i].prec = bit_depth;
3292 /* bits_per_pixel: 8 or 16 */
3293 cmptparm[i].bpp = bit_depth;
3294 cmptparm[i].sgnd = 0;
3295 cmptparm[i].dx = sub_dx;
3296 cmptparm[i].dy = sub_dy;
3297 cmptparm[i].w = width;
3298 cmptparm[i].h = height;
3301 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3303 if (image == NULL) {
3307 image->x0 = params->image_offset_x0;
3308 image->y0 = params->image_offset_y0;
3309 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3310 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3312 r = image->comps[0].data;
3313 g = image->comps[1].data;
3314 b = image->comps[2].data;
3315 a = image->comps[3].data;
3317 for (i = 0; i < height; ++i) {
3320 for (j = 0; j < width; ++j) {
3322 *r++ = s[0] << 8 | s[1];
3325 *g++ = s[0] << 8 | s[1];
3328 *b++ = s[0] << 8 | s[1];
3332 *a++ = s[0] << 8 | s[1];
3349 for (i = 0; i < height; ++i) {
3355 png_destroy_read_struct(&png, &info, NULL);
3364 int imagetopng(opj_image_t * image, const char *write_idf)
3369 int *red, *green, *blue, *alpha;
3370 unsigned char *row_buf, *d;
3371 int has_alpha, width, height, nr_comp, color_type;
3372 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3373 int prec, ushift, dshift, is16, force16, force8;
3374 unsigned short mask = 0xffff;
3375 png_color_8 sig_bit;
3377 is16 = force16 = force8 = ushift = dshift = 0;
3379 prec = image->comps[0].prec;
3380 nr_comp = image->numcomps;
3382 if (prec > 8 && prec < 16) {
3384 dshift = prec - ushift;
3387 } else if (prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */
3389 dshift = 8 - ushift;
3394 if (prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) {
3395 fprintf(stderr, "imagetopng: can not create %s"
3396 "\n\twrong bit_depth %d\n", write_idf, prec);
3399 writer = fopen(write_idf, "wb");
3401 if (writer == NULL) {
3408 /* Create and initialize the png_struct with the desired error handler
3409 * functions. If you want to use the default stderr and longjump method,
3410 * you can supply NULL for the last three parameters. We also check that
3411 * the library version is compatible with the one used at compile time,
3412 * in case we are using dynamically linked libraries. REQUIRED.
3414 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3416 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3422 /* Allocate/initialize the image information data. REQUIRED
3424 info = png_create_info_struct(png);
3430 /* Set error handling. REQUIRED if you are not supplying your own
3431 * error handling functions in the png_create_write_struct() call.
3433 if (setjmp(png_jmpbuf(png))) {
3437 /* I/O initialization functions is REQUIRED
3439 png_init_io(png, writer);
3441 /* Set the image information here. Width and height are up to 2^31,
3442 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3443 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3444 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3445 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3446 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3447 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3452 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3453 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3454 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3455 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3458 png_set_compression_level(png, Z_BEST_COMPRESSION);
3462 } else if (prec == 8) {
3464 } else if (prec == 4) {
3466 } else if (prec == 2) {
3468 } else if (prec == 1) {
3473 && image->comps[0].dx == image->comps[1].dx
3474 && image->comps[1].dx == image->comps[2].dx
3475 && image->comps[0].dy == image->comps[1].dy
3476 && image->comps[1].dy == image->comps[2].dy
3477 && image->comps[0].prec == image->comps[1].prec
3478 && image->comps[1].prec == image->comps[2].prec) {
3481 has_alpha = (nr_comp > 3);
3483 is16 = (prec == 16);
3485 width = image->comps[0].w;
3486 height = image->comps[0].h;
3488 red = image->comps[0].data;
3489 green = image->comps[1].data;
3490 blue = image->comps[2].data;
3492 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3495 sig_bit.alpha = prec;
3496 alpha = image->comps[3].data;
3497 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3498 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3502 color_type = PNG_COLOR_TYPE_RGB;
3505 png_set_sBIT(png, info, &sig_bit);
3507 png_set_IHDR(png, info, width, height, prec,
3510 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3511 /*=============================*/
3512 png_write_info(png, info);
3513 /*=============================*/
3515 png_set_packing(png);
3517 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3518 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3519 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3521 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3523 for (y = 0; y < height; ++y) {
3526 for (x = 0; x < width; ++x) {
3532 v = (v << ushift) + (v >> dshift);
3535 *d++ = (unsigned char)(v >> 8);
3536 *d++ = (unsigned char)v;
3538 v = *green + adjustG;
3542 v = (v << ushift) + (v >> dshift);
3545 *d++ = (unsigned char)(v >> 8);
3546 *d++ = (unsigned char)v;
3548 v = *blue + adjustB;
3552 v = (v << ushift) + (v >> dshift);
3555 *d++ = (unsigned char)(v >> 8);
3556 *d++ = (unsigned char)v;
3559 v = *alpha + adjustA;
3563 v = (v << ushift) + (v >> dshift);
3566 *d++ = (unsigned char)(v >> 8);
3567 *d++ = (unsigned char)v;
3576 v = (v << ushift) + (v >> dshift);
3579 *d++ = (unsigned char)(v & mask);
3581 v = *green + adjustG;
3585 v = (v << ushift) + (v >> dshift);
3588 *d++ = (unsigned char)(v & mask);
3590 v = *blue + adjustB;
3594 v = (v << ushift) + (v >> dshift);
3597 *d++ = (unsigned char)(v & mask);
3600 v = *alpha + adjustA;
3604 v = (v << ushift) + (v >> dshift);
3607 *d++ = (unsigned char)(v & mask);
3611 png_write_row(png, row_buf);
3617 else if (nr_comp == 1 /* GRAY */
3618 || (nr_comp == 2 /* GRAY_ALPHA */
3619 && image->comps[0].dx == image->comps[1].dx
3620 && image->comps[0].dy == image->comps[1].dy
3621 && image->comps[0].prec == image->comps[1].prec)) {
3624 red = image->comps[0].data;
3626 sig_bit.gray = prec;
3627 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3630 color_type = PNG_COLOR_TYPE_GRAY;
3634 sig_bit.alpha = prec;
3635 alpha = image->comps[1].data;
3636 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3637 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3639 width = image->comps[0].w;
3640 height = image->comps[0].h;
3642 png_set_IHDR(png, info, width, height, sig_bit.gray,
3645 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3647 png_set_sBIT(png, info, &sig_bit);
3648 /*=============================*/
3649 png_write_info(png, info);
3650 /*=============================*/
3651 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3654 png_set_packing(png);
3658 row_buf = (unsigned char*)
3659 malloc(width * nr_comp * sizeof(unsigned short));
3661 for (y = 0; y < height; ++y) {
3664 for (x = 0; x < width; ++x) {
3669 v = (v << ushift) + (v >> dshift);
3672 *d++ = (unsigned char)(v >> 8);
3673 *d++ = (unsigned char)v;
3679 v = (v << ushift) + (v >> dshift);
3682 *d++ = (unsigned char)(v >> 8);
3683 *d++ = (unsigned char)v;
3686 png_write_row(png, row_buf);
3690 } else { /* prec <= 8 */
3691 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3693 for (y = 0; y < height; ++y) {
3696 for (x = 0; x < width; ++x) {
3701 v = (v << ushift) + (v >> dshift);
3704 *d++ = (unsigned char)(v & mask);
3707 v = *alpha + adjustA;
3711 v = (v << ushift) + (v >> dshift);
3714 *d++ = (unsigned char)(v & mask);
3718 png_write_row(png, row_buf);
3724 fprintf(stderr, "imagetopng: can not create %s\n", write_idf);
3727 png_write_end(png, info);
3734 png_destroy_write_struct(&png, &info);
3744 #endif /* OPJ_HAVE_LIBPNG */