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"
46 #ifdef OPJ_HAVE_LIBTIFF
48 #endif /* OPJ_HAVE_LIBTIFF */
50 #ifdef OPJ_HAVE_LIBPNG
53 #endif /* OPJ_HAVE_LIBPNG */
59 * Get logarithm of an integer and round downwards.
63 static int int_floorlog2(int a)
66 for (l = 0; a > 1; l++) {
72 /* -->> -->> -->> -->>
76 <<-- <<-- <<-- <<-- */
78 #ifdef INFORMATION_ONLY
79 /* TGA header definition. */
81 unsigned char id_length; /* Image id field length */
82 unsigned char colour_map_type; /* Colour map type */
83 unsigned char image_type; /* Image type */
85 ** Colour map specification
87 unsigned short colour_map_index; /* First entry index */
88 unsigned short colour_map_length; /* Colour map length */
89 unsigned char colour_map_entry_size; /* Colour map entry size */
91 ** Image specification
93 unsigned short x_origin; /* x origin of image */
94 unsigned short y_origin; /* u origin of image */
95 unsigned short image_width; /* Image width */
96 unsigned short image_height; /* Image height */
97 unsigned char pixel_depth; /* Pixel depth */
98 unsigned char image_desc; /* Image descriptor */
100 #endif /* INFORMATION_ONLY */
102 static unsigned short get_ushort(unsigned short val)
105 #ifdef OPJ_BIG_ENDIAN
106 return (((val & 0xff) << 8) + (val >> 8));
113 #define TGA_HEADER_SIZE 18
115 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
116 unsigned int *width, unsigned int *height, int *flip_image)
120 unsigned char id_len, cmap_type, image_type;
121 unsigned char pixel_depth, image_desc;
122 unsigned short cmap_index, cmap_len, cmap_entry_size;
123 unsigned short x_origin, y_origin, image_w, image_h;
125 if (!bits_per_pixel || !width || !height || !flip_image) {
128 tga = (unsigned char*)malloc(18);
130 if (fread(tga, TGA_HEADER_SIZE, 1, fp) != 1) {
132 "\nError: fread return a number of element different from the expected.\n");
136 id_len = (unsigned char)tga[0];
137 cmap_type = (unsigned char)tga[1];
138 image_type = (unsigned char)tga[2];
139 cmap_index = get_ushort(*(unsigned short*)(&tga[3]));
140 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
141 cmap_entry_size = (unsigned char)tga[7];
144 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
145 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
146 image_w = get_ushort(*(unsigned short*)(&tga[12]));
147 image_h = get_ushort(*(unsigned short*)(&tga[14]));
148 pixel_depth = (unsigned char)tga[16];
149 image_desc = (unsigned char)tga[17];
153 *bits_per_pixel = (unsigned int)pixel_depth;
154 *width = (unsigned int)image_w;
155 *height = (unsigned int)image_h;
157 /* Ignore tga identifier, if present ... */
159 unsigned char *id = (unsigned char *) malloc(id_len);
160 if (!fread(id, id_len, 1, fp)) {
162 "\nError: fread return a number of element different from the expected.\n");
169 /* Test for compressed formats ... not yet supported ...
170 // Note :- 9 - RLE encoded palettized.
171 // 10 - RLE encoded RGB. */
172 if (image_type > 8) {
173 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
177 *flip_image = !(image_desc & 32);
179 /* Palettized formats are not yet supported, skip over the palette, if present ... */
180 palette_size = cmap_len * (cmap_entry_size / 8);
182 if (palette_size > 0) {
183 fprintf(stderr, "File contains a palette - not yet supported.");
184 fseek(fp, palette_size, SEEK_CUR);
189 #ifdef OPJ_BIG_ENDIAN
191 static inline uint16_t swap16(uint16_t x)
193 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
198 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
201 unsigned short image_w, image_h, us0;
202 unsigned char uc0, image_type;
203 unsigned char pixel_depth, image_desc;
205 if (!bits_per_pixel || !width || !height) {
211 if (bits_per_pixel < 256) {
212 pixel_depth = (unsigned char)bits_per_pixel;
214 fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header");
219 if (fwrite(&uc0, 1, 1, fp) != 1) {
220 goto fails; /* id_length */
222 if (fwrite(&uc0, 1, 1, fp) != 1) {
223 goto fails; /* colour_map_type */
226 image_type = 2; /* Uncompressed. */
227 if (fwrite(&image_type, 1, 1, fp) != 1) {
232 if (fwrite(&us0, 2, 1, fp) != 1) {
233 goto fails; /* colour_map_index */
235 if (fwrite(&us0, 2, 1, fp) != 1) {
236 goto fails; /* colour_map_length */
238 if (fwrite(&uc0, 1, 1, fp) != 1) {
239 goto fails; /* colour_map_entry_size */
242 if (fwrite(&us0, 2, 1, fp) != 1) {
243 goto fails; /* x_origin */
245 if (fwrite(&us0, 2, 1, fp) != 1) {
246 goto fails; /* y_origin */
249 image_w = (unsigned short)width;
250 image_h = (unsigned short) height;
252 #ifndef OPJ_BIG_ENDIAN
253 if (fwrite(&image_w, 2, 1, fp) != 1) {
256 if (fwrite(&image_h, 2, 1, fp) != 1) {
260 image_w = swap16(image_w);
261 image_h = swap16(image_h);
262 if (fwrite(&image_w, 2, 1, fp) != 1) {
265 if (fwrite(&image_h, 2, 1, fp) != 1) {
270 if (fwrite(&pixel_depth, 1, 1, fp) != 1) {
274 image_desc = 8; /* 8 bits per component. */
279 if (fwrite(&image_desc, 1, 1, fp) != 1) {
286 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
290 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters)
294 unsigned int image_width, image_height, pixel_bit_depth;
297 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
299 OPJ_COLOR_SPACE color_space;
302 int subsampling_dx, subsampling_dy;
305 f = fopen(filename, "rb");
307 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
311 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height,
316 /* We currently only support 24 & 32 bit tga's ... */
317 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
321 /* initialize image components */
322 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
324 mono = (pixel_bit_depth == 8) ||
325 (pixel_bit_depth == 16); /* Mono with & without alpha. */
326 save_alpha = (pixel_bit_depth == 16) ||
327 (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
330 color_space = CLRSPC_GRAY;
331 numcomps = save_alpha ? 2 : 1;
333 numcomps = save_alpha ? 4 : 3;
334 color_space = CLRSPC_SRGB;
337 subsampling_dx = parameters->subsampling_dx;
338 subsampling_dy = parameters->subsampling_dy;
340 for (i = 0; i < numcomps; i++) {
341 cmptparm[i].prec = 8;
343 cmptparm[i].sgnd = 0;
344 cmptparm[i].dx = subsampling_dx;
345 cmptparm[i].dy = subsampling_dy;
346 cmptparm[i].w = image_width;
347 cmptparm[i].h = image_height;
350 /* create the image */
351 image = opj_image_create(numcomps, &cmptparm[0], color_space);
357 /* set image offset and reference grid */
358 image->x0 = parameters->image_offset_x0;
359 image->y0 = parameters->image_offset_y0;
360 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 +
361 (image_width - 1) * subsampling_dx + 1;
362 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 +
363 (image_height - 1) * subsampling_dy + 1;
366 for (y = 0; y < image_height; y++) {
370 index = (image_height - y - 1) * image_width;
372 index = y * image_width;
376 for (x = 0; x < image_width; x++) {
377 unsigned char r, g, b;
379 if (!fread(&b, 1, 1, f)) {
381 "\nError: fread return a number of element different from the expected.\n");
382 opj_image_destroy(image);
385 if (!fread(&g, 1, 1, f)) {
387 "\nError: fread return a number of element different from the expected.\n");
388 opj_image_destroy(image);
391 if (!fread(&r, 1, 1, f)) {
393 "\nError: fread return a number of element different from the expected.\n");
394 opj_image_destroy(image);
398 image->comps[0].data[index] = r;
399 image->comps[1].data[index] = g;
400 image->comps[2].data[index] = b;
403 } else if (numcomps == 4) {
404 for (x = 0; x < image_width; x++) {
405 unsigned char r, g, b, a;
406 if (!fread(&b, 1, 1, f)) {
408 "\nError: fread return a number of element different from the expected.\n");
409 opj_image_destroy(image);
412 if (!fread(&g, 1, 1, f)) {
414 "\nError: fread return a number of element different from the expected.\n");
415 opj_image_destroy(image);
418 if (!fread(&r, 1, 1, f)) {
420 "\nError: fread return a number of element different from the expected.\n");
421 opj_image_destroy(image);
424 if (!fread(&a, 1, 1, f)) {
426 "\nError: fread return a number of element different from the expected.\n");
427 opj_image_destroy(image);
431 image->comps[0].data[index] = r;
432 image->comps[1].data[index] = g;
433 image->comps[2].data[index] = b;
434 image->comps[3].data[index] = a;
438 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
444 int imagetotga(opj_image_t * image, const char *outfile)
446 int width, height, bpp, x, y;
447 opj_bool write_alpha;
448 int i, adjustR, adjustG = 0, adjustB = 0;
449 unsigned int alpha_channel;
456 fdest = fopen(outfile, "wb");
458 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
462 for (i = 0; i < image->numcomps - 1; i++) {
463 if ((image->comps[0].dx != image->comps[i + 1].dx)
464 || (image->comps[0].dy != image->comps[i + 1].dy)
465 || (image->comps[0].prec != image->comps[i + 1].prec)) {
467 "Unable to create a tga file with such J2K image charateristics.");
473 width = image->comps[0].w;
474 height = image->comps[0].h;
476 /* Mono with alpha, or RGB with alpha. */
477 write_alpha = (image->numcomps == 2) || (image->numcomps == 4);
479 /* Write TGA header */
480 bpp = write_alpha ? 32 : 24;
481 if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
486 alpha_channel = image->numcomps - 1;
488 scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
490 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
491 if (image->numcomps >= 3) {
492 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
493 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
496 for (y = 0; y < height; y++) {
497 unsigned int index = y * width;
499 for (x = 0; x < width; x++, index++) {
500 r = (float)(image->comps[0].data[index] + adjustR);
502 if (image->numcomps > 2) {
503 g = (float)(image->comps[1].data[index] + adjustG);
504 b = (float)(image->comps[2].data[index] + adjustB);
505 } else { /* Greyscale ... */
510 /* TGA format writes BGR ... */
511 value = (unsigned char)(b * scale);
512 res = fwrite(&value, 1, 1, fdest);
514 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
519 value = (unsigned char)(g * scale);
520 res = fwrite(&value, 1, 1, fdest);
522 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
527 value = (unsigned char)(r * scale);
528 res = fwrite(&value, 1, 1, fdest);
530 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
536 a = (float)(image->comps[alpha_channel].data[index]);
537 value = (unsigned char)(a * scale);
538 res = fwrite(&value, 1, 1, fdest);
540 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
553 /* -->> -->> -->> -->>
557 <<-- <<-- <<-- <<-- */
559 /* WORD defines a two byte word */
560 typedef unsigned short int WORD;
562 /* DWORD defines a four byte word */
563 typedef unsigned int DWORD;
566 WORD bfType; /* 'BM' for Bitmap (19776) */
567 DWORD bfSize; /* Size of the file */
568 WORD bfReserved1; /* Reserved : 0 */
569 WORD bfReserved2; /* Reserved : 0 */
570 DWORD bfOffBits; /* Offset */
571 } BITMAPFILEHEADER_t;
574 DWORD biSize; /* Size of the structure in bytes */
575 DWORD biWidth; /* Width of the image in pixels */
576 DWORD biHeight; /* Height of the image in pixels */
577 WORD biPlanes; /* 1 */
578 WORD biBitCount; /* Number of color bits by pixels */
579 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
580 DWORD biSizeImage; /* Size of the image in bytes */
581 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
582 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
583 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
584 DWORD biClrImportant; /* Number of important color (0: ALL) */
585 } BITMAPINFOHEADER_t;
587 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
589 int subsampling_dx = parameters->subsampling_dx;
590 int subsampling_dy = parameters->subsampling_dy;
592 int i, numcomps, w, h;
593 OPJ_COLOR_SPACE color_space;
594 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
595 opj_image_t * image = NULL;
598 BITMAPFILEHEADER_t File_h;
599 BITMAPINFOHEADER_t Info_h;
601 unsigned char *table_R, *table_G, *table_B;
602 unsigned int j, PAD = 0;
609 IN = fopen(filename, "rb");
611 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
615 File_h.bfType = getc(IN);
616 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
618 if (File_h.bfType != 19778) {
619 fprintf(stderr, "Error, not a BMP file!\n");
625 File_h.bfSize = getc(IN);
626 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
627 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
628 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
630 File_h.bfReserved1 = getc(IN);
631 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
633 File_h.bfReserved2 = getc(IN);
634 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
636 File_h.bfOffBits = getc(IN);
637 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
638 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
639 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
644 Info_h.biSize = getc(IN);
645 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
646 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
647 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
649 if (Info_h.biSize != 40) {
650 fprintf(stderr, "Error, unknown BMP header size %d\n", Info_h.biSize);
654 Info_h.biWidth = getc(IN);
655 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
656 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
657 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
660 Info_h.biHeight = getc(IN);
661 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
662 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
663 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
666 Info_h.biPlanes = getc(IN);
667 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
669 Info_h.biBitCount = getc(IN);
670 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
672 Info_h.biCompression = getc(IN);
673 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
674 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
675 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
677 Info_h.biSizeImage = getc(IN);
678 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
679 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
680 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
682 Info_h.biXpelsPerMeter = getc(IN);
683 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
684 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
685 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
687 Info_h.biYpelsPerMeter = getc(IN);
688 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
689 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
690 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
692 Info_h.biClrUsed = getc(IN);
693 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
694 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
695 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
697 Info_h.biClrImportant = getc(IN);
698 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
699 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
700 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
702 /* Read the data and store them in the OUT file */
704 if (Info_h.biBitCount == 24) {
706 color_space = CLRSPC_SRGB;
707 /* initialize image components */
708 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
709 for (i = 0; i < numcomps; i++) {
710 cmptparm[i].prec = 8;
712 cmptparm[i].sgnd = 0;
713 cmptparm[i].dx = subsampling_dx;
714 cmptparm[i].dy = subsampling_dy;
718 /* create the image */
719 image = opj_image_create(numcomps, &cmptparm[0], color_space);
725 /* set image offset and reference grid */
726 image->x0 = parameters->image_offset_x0;
727 image->y0 = parameters->image_offset_y0;
728 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
729 (w - 1) * subsampling_dx + 1;
730 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
731 (h - 1) * subsampling_dy + 1;
735 /* Place the cursor at the beginning of the image information */
736 fseek(IN, 0, SEEK_SET);
737 fseek(IN, File_h.bfOffBits, SEEK_SET);
742 /* PAD = 4 - (3 * W) % 4; */
743 /* PAD = (PAD == 4) ? 0 : PAD; */
744 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
746 RGB = (unsigned char *)
747 malloc((3 * W + PAD) * H * sizeof(unsigned char));
749 if (fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H,
750 IN) != (3 * W + PAD) * H) {
753 opj_image_destroy(image);
755 "\nError: fread return a number of element different from the expected.\n");
761 for (y = 0; y < (int)H; y++) {
762 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
763 for (x = 0; x < (int)W; x++) {
764 unsigned char *pixel = &scanline[3 * x];
765 image->comps[0].data[index] = pixel[2]; /* R */
766 image->comps[1].data[index] = pixel[1]; /* G */
767 image->comps[2].data[index] = pixel[0]; /* B */
772 }/* if (Info_h.biBitCount == 24) */
773 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { /*RGB */
774 if (Info_h.biClrUsed == 0) {
775 Info_h.biClrUsed = 256;
776 } else if (Info_h.biClrUsed > 256) {
777 Info_h.biClrUsed = 256;
780 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
781 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
782 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
785 for (j = 0; j < Info_h.biClrUsed; j++) {
786 table_B[j] = (unsigned char)getc(IN);
787 table_G[j] = (unsigned char)getc(IN);
788 table_R[j] = (unsigned char)getc(IN);
791 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
797 /* Place the cursor at the beginning of the image information */
798 fseek(IN, 0, SEEK_SET);
799 fseek(IN, File_h.bfOffBits, SEEK_SET);
803 if (Info_h.biWidth % 2) {
807 numcomps = gray_scale ? 1 : 3;
808 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
809 /* initialize image components */
810 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
811 for (i = 0; i < numcomps; i++) {
812 cmptparm[i].prec = 8;
814 cmptparm[i].sgnd = 0;
815 cmptparm[i].dx = subsampling_dx;
816 cmptparm[i].dy = subsampling_dy;
820 /* create the image */
821 image = opj_image_create(numcomps, &cmptparm[0], color_space);
830 /* set image offset and reference grid */
831 image->x0 = parameters->image_offset_x0;
832 image->y0 = parameters->image_offset_y0;
833 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
834 (w - 1) * subsampling_dx + 1;
835 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
836 (h - 1) * subsampling_dy + 1;
840 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
842 if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) {
848 opj_image_destroy(image);
850 "\nError: fread return a number of element different from the expected.\n");
855 for (j = 0; j < W * H; j++) {
856 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
857 image->comps[0].data[index] =
858 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
865 for (j = 0; j < W * H; j++) {
866 if ((j % W < W - 1 && Info_h.biWidth % 2)
867 || !(Info_h.biWidth % 2)) {
868 unsigned char pixel_index =
869 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
870 image->comps[0].data[index] = table_R[pixel_index];
871 image->comps[1].data[index] = table_G[pixel_index];
872 image->comps[2].data[index] = table_B[pixel_index];
882 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/
883 unsigned char *pix, *beyond;
884 int *gray, *red, *green, *blue;
885 unsigned int x, y, max;
889 if (Info_h.biClrUsed == 0) {
890 Info_h.biClrUsed = 256;
891 } else if (Info_h.biClrUsed > 256) {
892 Info_h.biClrUsed = 256;
895 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
896 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
897 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
900 for (j = 0; j < Info_h.biClrUsed; j++) {
901 table_B[j] = (unsigned char)getc(IN);
902 table_G[j] = (unsigned char)getc(IN);
903 table_R[j] = (unsigned char)getc(IN);
905 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
912 numcomps = gray_scale ? 1 : 3;
913 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
914 /* initialize image components */
915 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
916 for (i = 0; i < numcomps; i++) {
917 cmptparm[i].prec = 8;
919 cmptparm[i].sgnd = 0;
920 cmptparm[i].dx = subsampling_dx;
921 cmptparm[i].dy = subsampling_dy;
925 /* create the image */
926 image = opj_image_create(numcomps, &cmptparm[0], color_space);
935 /* set image offset and reference grid */
936 image->x0 = parameters->image_offset_x0;
937 image->y0 = parameters->image_offset_y0;
938 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
939 - 1) * subsampling_dx + 1;
940 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
941 - 1) * subsampling_dy + 1;
945 /* Place the cursor at the beginning of the image information */
946 fseek(IN, 0, SEEK_SET);
947 fseek(IN, File_h.bfOffBits, SEEK_SET);
951 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
952 beyond = RGB + W * H;
962 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++) {
963 *pix = (unsigned char)c1;
968 if (c == 0x00) { /* EOL */
971 pix = RGB + x + (H - y - 1) * W;
972 } else if (c == 0x01) { /* EOP */
974 } else if (c == 0x02) { /* MOVE by dxdy */
979 pix = RGB + (H - y - 1) * W + x;
980 } else { /* 03 .. 255 */
982 for (; i < c && x < W && pix < beyond; i++, x++, pix++) {
984 *pix = (unsigned char)c1;
986 if (c & 1) { /* skip padding byte */
994 gray = image->comps[0].data;
1001 *gray++ = table_R[uc];
1004 /*int *red, *green, *blue;*/
1006 red = image->comps[0].data;
1007 green = image->comps[1].data;
1008 blue = image->comps[2].data;
1015 *red++ = table_R[uc];
1016 *green++ = table_G[uc];
1017 *blue++ = table_B[uc];
1027 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1028 "is not yet implemented [%d]\n", Info_h.biBitCount);
1034 int imagetobmp(opj_image_t * image, const char *outfile)
1039 int adjustR, adjustG, adjustB;
1041 if (image->comps[0].prec < 8) {
1042 fprintf(stderr, "Unsupported precision: %d\n", image->comps[0].prec);
1045 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1046 && image->comps[1].dx == image->comps[2].dx
1047 && image->comps[0].dy == image->comps[1].dy
1048 && image->comps[1].dy == image->comps[2].dy
1049 && image->comps[0].prec == image->comps[1].prec
1050 && image->comps[1].prec == image->comps[2].prec) {
1052 /* -->> -->> -->> -->>
1054 <<-- <<-- <<-- <<-- */
1056 fdest = fopen(outfile, "wb");
1058 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1062 w = image->comps[0].w;
1063 h = image->comps[0].h;
1065 fprintf(fdest, "BM");
1069 fprintf(fdest, "%c%c%c%c",
1070 (unsigned char)(h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1071 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1072 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1073 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1074 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1075 ((0) >> 24) & 0xff);
1076 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff,
1077 ((54) >> 24) & 0xff);
1081 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1082 ((40) >> 24) & 0xff);
1083 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1084 (unsigned char)((w) >> 8) & 0xff,
1085 (unsigned char)((w) >> 16) & 0xff,
1086 (unsigned char)((w) >> 24) & 0xff);
1087 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1088 (unsigned char)((h) >> 8) & 0xff,
1089 (unsigned char)((h) >> 16) & 0xff,
1090 (unsigned char)((h) >> 24) & 0xff);
1091 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1092 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1093 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1094 ((0) >> 24) & 0xff);
1095 fprintf(fdest, "%c%c%c%c", (unsigned char)(3 * h * w + 3 * h * (w % 2)) & 0xff,
1096 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1097 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1098 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1099 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1100 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1101 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1102 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1103 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1104 ((0) >> 24) & 0xff);
1105 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1106 ((0) >> 24) & 0xff);
1108 if (image->comps[0].prec > 8) {
1109 adjustR = image->comps[0].prec - 8;
1110 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1111 image->comps[0].prec);
1115 if (image->comps[1].prec > 8) {
1116 adjustG = image->comps[1].prec - 8;
1117 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n",
1118 image->comps[1].prec);
1122 if (image->comps[2].prec > 8) {
1123 adjustB = image->comps[2].prec - 8;
1124 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n",
1125 image->comps[2].prec);
1130 for (i = 0; i < w * h; i++) {
1131 unsigned char rc, gc, bc;
1134 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1135 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1136 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1142 rc = (unsigned char)r;
1144 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1145 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1146 g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
1152 gc = (unsigned char)g;
1154 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1155 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1156 b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
1162 bc = (unsigned char)b;
1164 fprintf(fdest, "%c%c%c", bc, gc, rc);
1166 if ((i + 1) % w == 0) {
1167 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) { /* ADD */
1168 fprintf(fdest, "%c", 0);
1173 } else { /* Gray-scale */
1175 /* -->> -->> -->> -->>
1176 8 bits non code (Gray scale)
1177 <<-- <<-- <<-- <<-- */
1179 fdest = fopen(outfile, "wb");
1180 w = image->comps[0].w;
1181 h = image->comps[0].h;
1183 fprintf(fdest, "BM");
1187 fprintf(fdest, "%c%c%c%c",
1188 (unsigned char)(h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1189 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1190 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1191 (unsigned char)((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1192 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1193 ((0) >> 24) & 0xff);
1194 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1195 ((54 + 1024) >> 16) & 0xff,
1196 ((54 + 1024) >> 24) & 0xff);
1200 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1201 ((40) >> 24) & 0xff);
1202 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1203 (unsigned char)((w) >> 8) & 0xff,
1204 (unsigned char)((w) >> 16) & 0xff,
1205 (unsigned char)((w) >> 24) & 0xff);
1206 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1207 (unsigned char)((h) >> 8) & 0xff,
1208 (unsigned char)((h) >> 16) & 0xff,
1209 (unsigned char)((h) >> 24) & 0xff);
1210 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1211 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1212 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1213 ((0) >> 24) & 0xff);
1214 fprintf(fdest, "%c%c%c%c", (unsigned char)(h * w + h * (w % 2)) & 0xff,
1215 (unsigned char)((h * w + h * (w % 2)) >> 8) & 0xff,
1216 (unsigned char)((h * w + h * (w % 2)) >> 16) & 0xff,
1217 (unsigned char)((h * w + h * (w % 2)) >> 24) & 0xff);
1218 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1219 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1220 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1221 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1222 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1223 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1224 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1225 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1227 if (image->comps[0].prec > 8) {
1228 adjustR = image->comps[0].prec - 8;
1229 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1230 image->comps[0].prec);
1235 for (i = 0; i < 256; i++) {
1236 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1239 for (i = 0; i < w * h; i++) {
1242 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1243 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1244 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1251 fprintf(fdest, "%c", (unsigned char)r);
1253 if ((i + 1) % w == 0) {
1254 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) { /* ADD */
1255 fprintf(fdest, "%c", 0);
1265 /* -->> -->> -->> -->>
1269 <<-- <<-- <<-- <<-- */
1272 static unsigned char readuchar(FILE * f)
1275 if (!fread(&c1, 1, 1, f)) {
1277 "\nError: fread return a number of element different from the expected.\n");
1283 static unsigned short readushort(FILE * f, int bigendian)
1285 unsigned char c1, c2;
1286 if (!fread(&c1, 1, 1, f)) {
1288 "\nError: fread return a number of element different from the expected.\n");
1291 if (!fread(&c2, 1, 1, f)) {
1293 "\nError: fread return a number of element different from the expected.\n");
1297 return (c1 << 8) + c2;
1299 return (c2 << 8) + c1;
1303 static unsigned int readuint(FILE * f, int bigendian)
1305 unsigned char c1, c2, c3, c4;
1306 if (!fread(&c1, 1, 1, f)) {
1308 "\nError: fread return a number of element different from the expected.\n");
1311 if (!fread(&c2, 1, 1, f)) {
1313 "\nError: fread return a number of element different from the expected.\n");
1316 if (!fread(&c3, 1, 1, f)) {
1318 "\nError: fread return a number of element different from the expected.\n");
1321 if (!fread(&c4, 1, 1, f)) {
1323 "\nError: fread return a number of element different from the expected.\n");
1327 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1329 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1333 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
1337 int i, numcomps, max;
1338 OPJ_COLOR_SPACE color_space;
1339 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1340 opj_image_t * image = NULL;
1341 int adjustS, ushift, dshift, force8;
1343 char endian1, endian2, sign;
1348 opj_image_comp_t *comp = NULL;
1351 color_space = CLRSPC_GRAY;
1353 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1357 f = fopen(filename, "rb");
1359 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1363 fseek(f, 0, SEEK_SET);
1364 if (fscanf(f, "PG%31[ \t]%c%c%31[ \t+-]%d%31[ \t]%d%31[ \t]%d", temp, &endian1,
1365 &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
1367 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1374 while (signtmp[i] != '\0') {
1375 if (signtmp[i] == '-') {
1382 if (endian1 == 'M' && endian2 == 'L') {
1384 } else if (endian2 == 'M' && endian1 == 'L') {
1387 fprintf(stderr, "Bad pgx header, please check input file\n");
1392 /* initialize image component */
1394 cmptparm.x0 = parameters->image_offset_x0;
1395 cmptparm.y0 = parameters->image_offset_y0;
1396 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
1397 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1398 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
1399 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1409 dshift = prec - ushift;
1410 if (cmptparm.sgnd) {
1411 adjustS = (1 << (prec - 1));
1418 ushift = dshift = force8 = adjustS = 0;
1421 cmptparm.prec = prec;
1422 cmptparm.bpp = prec;
1423 cmptparm.dx = parameters->subsampling_dx;
1424 cmptparm.dy = parameters->subsampling_dy;
1426 /* create the image */
1427 image = opj_image_create(numcomps, &cmptparm, color_space);
1432 /* set image offset and reference grid */
1433 image->x0 = cmptparm.x0;
1434 image->y0 = cmptparm.x0;
1435 image->x1 = cmptparm.w;
1436 image->y1 = cmptparm.h;
1438 /* set image data */
1440 comp = &image->comps[0];
1442 for (i = 0; i < w * h; i++) {
1445 v = readuchar(f) + adjustS;
1446 v = (v << ushift) + (v >> dshift);
1447 comp->data[i] = (unsigned char)v;
1455 if (comp->prec == 8) {
1459 v = (char) readuchar(f);
1461 } else if (comp->prec <= 16) {
1463 v = readushort(f, bigendian);
1465 v = (short) readushort(f, bigendian);
1469 v = readuint(f, bigendian);
1471 v = (int) readuint(f, bigendian);
1480 comp->bpp = int_floorlog2(max) + 1;
1485 int imagetopgx(opj_image_t * image, const char *outfile)
1491 for (compno = 0; compno < image->numcomps; compno++) {
1492 opj_image_comp_t *comp = &image->comps[compno];
1493 char bname[256]; /* buffer for name */
1494 char *name = bname; /* pointer */
1497 const size_t olen = strlen(outfile);
1498 const size_t dotpos = olen - 4;
1499 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1500 if (olen < 4 || outfile[dotpos] != '.') {
1501 /* `pgx` was recognized but there is no dot at expected position */
1502 fprintf(stderr, "ERROR -> Impossible happen.");
1506 name = (char*)malloc(total + 1);
1508 strncpy(name, outfile, dotpos);
1509 /*if (image->numcomps > 1) {*/
1510 sprintf(name + dotpos, "_%d.pgx", compno);
1512 strcpy(name+dotpos, ".pgx");
1514 fdest = fopen(name, "wb");
1516 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1520 /* don't need name anymore */
1525 w = image->comps[compno].w;
1526 h = image->comps[compno].h;
1528 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1529 if (comp->prec <= 8) {
1531 } else if (comp->prec <= 16) {
1536 for (i = 0; i < w * h; i++) {
1537 int v = image->comps[compno].data[i];
1538 for (j = nbytes - 1; j >= 0; j--) {
1539 char byte = (char)(v >> (j * 8));
1540 res = fwrite(&byte, 1, 1, fdest);
1542 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1554 /* -->> -->> -->> -->>
1558 <<-- <<-- <<-- <<-- */
1561 int width, height, maxval, depth, format;
1562 char rgb, rgba, gray, graya, bw;
1566 static char *skip_white(char *s)
1569 if (*s == '\n' || *s == '\r') {
1581 static char *skip_int(char *start, int *out_n)
1589 s = skip_white(start);
1603 *out_n = atoi(start);
1608 static char *skip_idf(char *start, char out_idf[256])
1613 s = skip_white(start);
1620 if (isalpha(*s) || *s == '_') {
1628 strncpy(out_idf, start, 255);
1633 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1636 int format, have_wh, end, ttype;
1637 char idf[256], type[256];
1640 if (fgets(line, 250, reader) == NULL) {
1641 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1645 if (line[0] != 'P') {
1646 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1649 format = atoi(line + 1);
1650 if (format < 1 || format > 7) {
1651 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1654 ph->format = format;
1655 ttype = end = have_wh = 0;
1657 while (fgets(line, 250, reader)) {
1665 s = skip_idf(s, idf);
1667 if (s == NULL || *s == 0) {
1671 if (strcmp(idf, "ENDHDR") == 0) {
1675 if (strcmp(idf, "WIDTH") == 0) {
1676 s = skip_int(s, &ph->width);
1677 if (s == NULL || *s == 0) {
1683 if (strcmp(idf, "HEIGHT") == 0) {
1684 s = skip_int(s, &ph->height);
1685 if (s == NULL || *s == 0) {
1691 if (strcmp(idf, "DEPTH") == 0) {
1692 s = skip_int(s, &ph->depth);
1693 if (s == NULL || *s == 0) {
1699 if (strcmp(idf, "MAXVAL") == 0) {
1700 s = skip_int(s, &ph->maxval);
1701 if (s == NULL || *s == 0) {
1707 if (strcmp(idf, "TUPLTYPE") == 0) {
1708 s = skip_idf(s, type);
1709 if (s == NULL || *s == 0) {
1713 if (strcmp(type, "BLACKANDWHITE") == 0) {
1718 if (strcmp(type, "GRAYSCALE") == 0) {
1723 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1728 if (strcmp(type, "RGB") == 0) {
1733 if (strcmp(type, "RGB_ALPHA") == 0) {
1738 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1741 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1743 } /* if(format == 7) */
1746 s = skip_int(s, &ph->width);
1748 s = skip_int(s, &ph->height);
1752 if (format == 1 || format == 4) {
1758 if (format == 2 || format == 3 || format == 5 || format == 6) {
1759 /* P2, P3, P5, P6: */
1760 s = skip_int(s, &ph->maxval);
1762 if (ph->maxval > 65535) {
1767 }/* while(fgets( ) */
1768 if (format == 2 || format == 3 || format > 4) {
1769 if (ph->maxval < 1 || ph->maxval > 65535) {
1773 if (ph->width < 1 || ph->height < 1) {
1779 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1782 if (ph->depth < 1 || ph->depth > 4) {
1786 if (ph->width && ph->height && ph->depth & ph->maxval && ttype) {
1790 if (format != 1 && format != 4) {
1791 if (ph->width && ph->height && ph->maxval) {
1795 if (ph->width && ph->height) {
1803 static int has_prec(int val)
1853 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1855 int subsampling_dx = parameters->subsampling_dx;
1856 int subsampling_dy = parameters->subsampling_dy;
1859 int i, compno, numcomps, w, h, prec, format;
1860 OPJ_COLOR_SPACE color_space;
1861 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1862 opj_image_t * image = NULL;
1863 struct pnm_header header_info;
1865 if ((fp = fopen(filename, "rb")) == NULL) {
1866 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1869 memset(&header_info, 0, sizeof(struct pnm_header));
1871 read_pnm_header(fp, &header_info);
1873 if (!header_info.ok) {
1878 /* This limitation could be removed by making sure to use size_t below */
1879 if (header_info.height != 0 &&
1880 header_info.width > INT_MAX / header_info.height) {
1881 fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n",
1882 header_info.width, header_info.height);
1887 format = header_info.format;
1890 case 1: /* ascii bitmap */
1891 case 4: /* raw bitmap */
1895 case 2: /* ascii greymap */
1896 case 5: /* raw greymap */
1900 case 3: /* ascii pixmap */
1901 case 6: /* raw pixmap */
1905 case 7: /* arbitrary map */
1906 numcomps = header_info.depth;
1914 color_space = CLRSPC_GRAY; /* GRAY, GRAYA */
1916 color_space = CLRSPC_SRGB; /* RGB, RGBA */
1919 prec = has_prec(header_info.maxval);
1925 w = header_info.width;
1926 h = header_info.height;
1927 subsampling_dx = parameters->subsampling_dx;
1928 subsampling_dy = parameters->subsampling_dy;
1930 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1932 for (i = 0; i < numcomps; i++) {
1933 cmptparm[i].prec = prec;
1934 cmptparm[i].bpp = prec;
1935 cmptparm[i].sgnd = 0;
1936 cmptparm[i].dx = subsampling_dx;
1937 cmptparm[i].dy = subsampling_dy;
1941 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1948 /* set image offset and reference grid */
1949 image->x0 = parameters->image_offset_x0;
1950 image->y0 = parameters->image_offset_y0;
1951 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1952 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1954 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1957 for (i = 0; i < w * h; i++) {
1958 for (compno = 0; compno < numcomps; compno++) {
1960 if (fscanf(fp, "%u", &index) != 1) {
1962 "\nWARNING: fscanf return a number of element different from the expected.\n");
1965 image->comps[compno].data[i] = (index * 255) / header_info.maxval;
1968 } else if ((format == 5)
1971 && (header_info.gray || header_info.graya
1972 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1973 unsigned char c0, c1, one;
1977 for (i = 0; i < w * h; i++) {
1978 for (compno = 0; compno < numcomps; compno++) {
1979 if (!fread(&c0, 1, 1, fp)) {
1981 "\nError: fread return a number of element different from the expected.\n");
1984 image->comps[compno].data[i] = c0;
1986 if (!fread(&c1, 1, 1, fp)) {
1988 "\nError: fread return a number of element different from the expected.\n");
1991 image->comps[compno].data[i] = ((c0 << 8) | c1);
1995 } else if (format == 1) { /* ascii bitmap */
1996 for (i = 0; i < w * h; i++) {
1999 if (fscanf(fp, "%u", &index) != 1) {
2001 "\nWARNING: fscanf return a number of element different from the expected.\n");
2004 image->comps[0].data[i] = (index ? 0 : 255);
2006 } else if (format == 4) {
2011 for (y = 0; y < h; ++y) {
2015 for (x = 0; x < w; ++x) {
2018 uc = (unsigned char)getc(fp);
2020 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
2025 } else if ((format == 7 && header_info.bw)) { /*MONO*/
2028 for (i = 0; i < w * h; ++i) {
2029 if (!fread(&uc, 1, 1, fp)) {
2031 "\nError: fread return a number of element different from the expected.\n");
2033 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
2041 int imagetopnm(opj_image_t * image, const char *outfile)
2043 int *red, *green, *blue, *alpha;
2045 int i, compno, ncomp;
2046 int adjustR, adjustG, adjustB, adjustA;
2047 int fails, two, want_gray, has_alpha, triple;
2050 const char *tmp = outfile;
2053 if ((prec = image->comps[0].prec) > 16) {
2054 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
2055 "\n\t: refused.\n", __FILE__, __LINE__, prec);
2058 two = has_alpha = 0;
2060 ncomp = image->numcomps;
2066 want_gray = (*tmp == 'g' || *tmp == 'G');
2067 ncomp = image->numcomps;
2073 if (ncomp == 2 /* GRAYA */
2074 || (ncomp > 2 /* RGB, RGBA */
2075 && image->comps[0].dx == image->comps[1].dx
2076 && image->comps[1].dx == image->comps[2].dx
2077 && image->comps[0].dy == image->comps[1].dy
2078 && image->comps[1].dy == image->comps[2].dy
2079 && image->comps[0].prec == image->comps[1].prec
2080 && image->comps[1].prec == image->comps[2].prec
2082 fdest = fopen(outfile, "wb");
2085 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
2089 triple = (ncomp > 2);
2090 wr = image->comps[0].w;
2091 hr = image->comps[0].h;
2092 max = (1 << prec) - 1;
2093 has_alpha = (ncomp == 4 || ncomp == 2);
2095 red = image->comps[0].data;
2098 green = image->comps[1].data;
2099 blue = image->comps[2].data;
2101 green = blue = NULL;
2105 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
2107 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
2108 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2109 wr, hr, ncomp, max, tt);
2110 alpha = image->comps[ncomp - 1].data;
2111 adjustA = (image->comps[ncomp - 1].sgnd ?
2112 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2114 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2115 opj_version(), wr, hr, max);
2118 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2121 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2122 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2124 adjustG = adjustB = 0;
2127 for (i = 0; i < wr * hr; ++i) {
2132 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2135 v = *green + adjustG;
2138 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2140 v = *blue + adjustB;
2143 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2148 v = *alpha + adjustA;
2151 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2159 fprintf(fdest, "%c", (unsigned char)*red++);
2161 fprintf(fdest, "%c%c", (unsigned char)*green++, (unsigned char)*blue++);
2165 fprintf(fdest, "%c", (unsigned char)*alpha++);
2176 if (image->numcomps > ncomp) {
2177 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2178 fprintf(stderr, " is written to the file\n");
2180 destname = (char*)malloc(strlen(outfile) + 8);
2182 for (compno = 0; compno < ncomp; compno++) {
2184 sprintf(destname, "%d.%s", compno, outfile);
2186 sprintf(destname, "%s", outfile);
2189 fdest = fopen(destname, "wb");
2191 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2195 wr = image->comps[compno].w;
2196 hr = image->comps[compno].h;
2197 prec = image->comps[compno].prec;
2198 max = (1 << prec) - 1;
2200 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2201 opj_version(), wr, hr, max);
2203 red = image->comps[compno].data;
2205 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2208 for (i = 0; i < wr * hr; i++) {
2212 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2217 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2220 } else { /* prec <= 8 */
2221 for (i = 0; i < wr * hr; ++i) {
2222 fprintf(fdest, "%c", (unsigned char)(*red + adjustR));
2233 #ifdef OPJ_HAVE_LIBTIFF
2234 /* -->> -->> -->> -->>
2238 <<-- <<-- <<-- <<-- */
2240 int imagetotif(opj_image_t * image, const char *outfile)
2242 int width, height, imgsize;
2243 int bps, index, adjust, sgnd;
2244 int ushift, dshift, has_alpha, force16;
2250 ushift = dshift = force16 = has_alpha = 0;
2251 bps = image->comps[0].prec;
2253 if (bps > 8 && bps < 16) {
2255 dshift = bps - ushift;
2260 if (bps != 8 && bps != 16) {
2261 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2263 fprintf(stderr, "\tAborting\n");
2266 tif = TIFFOpen(outfile, "wb");
2269 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2272 sgnd = image->comps[0].sgnd;
2273 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2275 if (image->numcomps >= 3
2276 && image->comps[0].dx == image->comps[1].dx
2277 && image->comps[1].dx == image->comps[2].dx
2278 && image->comps[0].dy == image->comps[1].dy
2279 && image->comps[1].dy == image->comps[2].dy
2280 && image->comps[0].prec == image->comps[1].prec
2281 && image->comps[1].prec == image->comps[2].prec) {
2282 has_alpha = (image->numcomps == 4);
2284 width = image->comps[0].w;
2285 height = image->comps[0].h;
2286 imgsize = width * height ;
2288 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2289 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2290 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2291 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2292 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2293 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2294 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2295 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2296 strip_size = TIFFStripSize(tif);
2297 buf = _TIFFmalloc(strip_size);
2300 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2301 unsigned char *dat8;
2302 tsize_t i, ssize, last_i = 0;
2304 ssize = TIFFStripSize(tif);
2305 dat8 = (unsigned char*)buf;
2308 step = 3 + has_alpha;
2311 for (i = 0; i < ssize - restx; i += step) {
2314 if (index < imgsize) {
2315 r = image->comps[0].data[index];
2316 g = image->comps[1].data[index];
2317 b = image->comps[2].data[index];
2319 a = image->comps[3].data[index];
2344 if (last_i < ssize) {
2345 for (i = last_i; i < ssize; i += step) {
2348 if (index < imgsize) {
2349 r = image->comps[0].data[index];
2350 g = image->comps[1].data[index];
2351 b = image->comps[2].data[index];
2353 a = image->comps[3].data[index];
2365 if (i + 1 < ssize) {
2370 if (i + 2 < ssize) {
2376 if (i + 3 < ssize) {
2387 }/*if(last_i < ssize)*/
2390 else if (bps == 16) {
2391 step = 6 + has_alpha + has_alpha;
2394 for (i = 0; i < ssize - restx ; i += step) {
2397 if (index < imgsize) {
2398 r = image->comps[0].data[index];
2399 g = image->comps[1].data[index];
2400 b = image->comps[2].data[index];
2402 a = image->comps[3].data[index];
2414 r = (r << ushift) + (r >> dshift);
2415 g = (g << ushift) + (g >> dshift);
2416 b = (b << ushift) + (b >> dshift);
2418 a = (a << ushift) + (a >> dshift);
2421 dat8[i + 0] = r; /*LSB*/
2422 dat8[i + 1] = (r >> 8); /*MSB*/
2424 dat8[i + 3] = (g >> 8);
2426 dat8[i + 5] = (b >> 8);
2429 dat8[i + 7] = (a >> 8);
2438 if (last_i < ssize) {
2439 for (i = last_i ; i < ssize ; i += step) {
2442 if (index < imgsize) {
2443 r = image->comps[0].data[index];
2444 g = image->comps[1].data[index];
2445 b = image->comps[2].data[index];
2447 a = image->comps[3].data[index];
2459 r = (r << ushift) + (r >> dshift);
2460 g = (g << ushift) + (g >> dshift);
2461 b = (b << ushift) + (b >> dshift);
2463 a = (a << ushift) + (a >> dshift);
2466 dat8[i + 0] = r; /*LSB*/
2467 if (i + 1 < ssize) {
2468 dat8[i + 1] = (r >> 8);
2472 if (i + 2 < ssize) {
2477 if (i + 3 < ssize) {
2478 dat8[i + 3] = (g >> 8);
2482 if (i + 4 < ssize) {
2487 if (i + 5 < ssize) {
2488 dat8[i + 5] = (b >> 8);
2494 if (i + 6 < ssize) {
2499 if (i + 7 < ssize) {
2500 dat8[i + 7] = (a >> 8);
2510 }/*if(last_i < ssize)*/
2513 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2514 }/*for(strip = 0; )*/
2516 _TIFFfree((void*)buf);
2522 if (image->numcomps == 1 /* GRAY */
2523 || (image->numcomps == 2 /* GRAY_ALPHA */
2524 && image->comps[0].dx == image->comps[1].dx
2525 && image->comps[0].dy == image->comps[1].dy
2526 && image->comps[0].prec == image->comps[1].prec)) {
2529 has_alpha = (image->numcomps == 2);
2531 width = image->comps[0].w;
2532 height = image->comps[0].h;
2533 imgsize = width * height;
2536 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2537 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2538 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2539 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2540 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2541 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2542 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2543 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2545 /* Get a buffer for the data */
2546 strip_size = TIFFStripSize(tif);
2547 buf = _TIFFmalloc(strip_size);
2550 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2551 unsigned char *dat8;
2552 tsize_t i, ssize = TIFFStripSize(tif);
2553 dat8 = (unsigned char*)buf;
2556 step = 1 + has_alpha;
2558 for (i = 0; i < ssize; i += step) {
2559 if (index < imgsize) {
2562 r = image->comps[0].data[index];
2564 a = image->comps[1].data[index];
2583 else if (bps == 16) {
2584 step = 2 + has_alpha + has_alpha;
2586 for (i = 0; i < ssize; i += step) {
2587 if (index < imgsize) {
2590 r = image->comps[0].data[index];
2592 a = image->comps[1].data[index];
2602 r = (r << ushift) + (r >> dshift);
2604 a = (a << ushift) + (a >> dshift);
2607 dat8[i + 0] = r; /*LSB*/
2608 dat8[i + 1] = r >> 8; /*MSB*/
2611 dat8[i + 3] = a >> 8;
2614 }/*if(index < imgsize)*/
2620 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2631 fprintf(stderr, "imagetotif: Bad color format.\n"
2632 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2633 fprintf(stderr, "\tFOUND: numcomps(%d)\n\tAborting\n",
2640 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2641 * CINEMA : 12 bit precision
2643 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2645 int subsampling_dx = parameters->subsampling_dx;
2646 int subsampling_dy = parameters->subsampling_dy;
2651 int j, numcomps, w, h, index;
2652 OPJ_COLOR_SPACE color_space;
2653 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2654 opj_image_t *image = NULL;
2657 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2658 unsigned int tiWidth, tiHeight;
2660 tif = TIFFOpen(filename, "r");
2663 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2666 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2667 tiWidth = tiHeight = 0;
2669 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2670 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2671 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2672 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2673 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2674 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2675 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2680 unsigned short b = tiBps, p = tiPhoto;
2682 if (tiBps != 8 && tiBps != 16 && tiBps != 12) {
2685 if (tiPhoto != 1 && tiPhoto != 2) {
2691 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits"
2692 " implemented\n", tiBps);
2694 fprintf(stderr, "tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2695 " and GRAY(A) has been implemented\n", (int) tiPhoto);
2697 fprintf(stderr, "\tAborting\n");
2703 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2705 uint16 extrasamples;
2707 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2708 &extrasamples, &sampleinfo);
2710 if (extrasamples >= 1) {
2711 switch (sampleinfo[0]) {
2712 case EXTRASAMPLE_UNSPECIFIED:
2713 /* Workaround for some images without correct info about alpha channel
2720 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2721 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2725 } else /* extrasamples == 0 */
2726 if (tiSpp == 4 || tiSpp == 2) {
2731 /* initialize image components
2733 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2735 if (tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */
2736 numcomps = 3 + has_alpha;
2737 color_space = CLRSPC_SRGB;
2739 for (j = 0; j < numcomps; j++) {
2740 if (parameters->cp_cinema) {
2741 cmptparm[j].prec = 12;
2742 cmptparm[j].bpp = 12;
2744 cmptparm[j].prec = tiBps;
2745 cmptparm[j].bpp = tiBps;
2747 cmptparm[j].dx = subsampling_dx;
2748 cmptparm[j].dy = subsampling_dy;
2753 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2759 /* set image offset and reference grid
2761 image->x0 = parameters->image_offset_x0;
2762 image->y0 = parameters->image_offset_y0;
2763 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2764 image->x0 + (w - 1) * subsampling_dx + 1;
2765 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2766 image->y0 + (h - 1) * subsampling_dy + 1;
2768 buf = _TIFFmalloc(TIFFStripSize(tif));
2770 strip_size = TIFFStripSize(tif);
2772 imgsize = image->comps[0].w * image->comps[0].h ;
2773 /* Read the Image components
2775 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2776 unsigned char *dat8;
2779 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2780 dat8 = (unsigned char*)buf;
2783 step = 6 + has_alpha + has_alpha;
2785 for (i = 0; i < ssize; i += step) {
2786 if (index < imgsize) {
2787 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0]; /* R */
2788 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2]; /* G */
2789 image->comps[2].data[index] = (dat8[i + 5] << 8) | dat8[i + 4]; /* B */
2791 image->comps[3].data[index] = (dat8[i + 7] << 8) | dat8[i + 6];
2794 if (parameters->cp_cinema) {
2795 /* Rounding 16 to 12 bits
2797 image->comps[0].data[index] =
2798 (image->comps[0].data[index] + 0x08) >> 4 ;
2799 image->comps[1].data[index] =
2800 (image->comps[1].data[index] + 0x08) >> 4 ;
2801 image->comps[2].data[index] =
2802 (image->comps[2].data[index] + 0x08) >> 4 ;
2804 image->comps[3].data[index] =
2805 (image->comps[3].data[index] + 0x08) >> 4 ;
2812 }/*if(tiBps == 16)*/
2813 else if (tiBps == 8) {
2814 step = 3 + has_alpha;
2816 for (i = 0; i < ssize; i += step) {
2817 if (index < imgsize) {
2818 image->comps[0].data[index] = dat8[i + 0]; /* R */
2819 image->comps[1].data[index] = dat8[i + 1]; /* G */
2820 image->comps[2].data[index] = dat8[i + 2]; /* B */
2822 image->comps[3].data[index] = dat8[i + 3];
2825 if (parameters->cp_cinema) {
2826 /* Rounding 8 to 12 bits
2828 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2829 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2830 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2832 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2841 }/*if( tiBps == 8)*/
2842 else if (tiBps == 12) { /* CINEMA file */
2845 for (i = 0; i < ssize; i += step) {
2846 if ((index < imgsize) & (index + 1 < imgsize)) {
2847 image->comps[0].data[index] = (dat8[i + 0] << 4) | (dat8[i + 1] >> 4);
2848 image->comps[1].data[index] = ((dat8[i + 1] & 0x0f) << 8) | dat8[i + 2];
2850 image->comps[2].data[index] = (dat8[i + 3] << 4) | (dat8[i + 4] >> 4);
2851 image->comps[0].data[index + 1] = ((dat8[i + 4] & 0x0f) << 8) | dat8[i + 5];
2853 image->comps[1].data[index + 1] = (dat8[i + 6] << 4) |
2855 image->comps[2].data[index + 1] = ((dat8[i + 7] & 0x0f) << 8) | dat8[i + 8];
2863 }/*for(strip = 0; )*/
2871 if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */
2872 numcomps = 1 + has_alpha;
2873 color_space = CLRSPC_GRAY;
2875 for (j = 0; j < numcomps; ++j) {
2876 cmptparm[j].prec = tiBps;
2877 cmptparm[j].bpp = tiBps;
2878 cmptparm[j].dx = subsampling_dx;
2879 cmptparm[j].dy = subsampling_dy;
2883 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2889 /* set image offset and reference grid
2891 image->x0 = parameters->image_offset_x0;
2892 image->y0 = parameters->image_offset_y0;
2893 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2894 image->x0 + (w - 1) * subsampling_dx + 1;
2895 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2896 image->y0 + (h - 1) * subsampling_dy + 1;
2898 buf = _TIFFmalloc(TIFFStripSize(tif));
2900 strip_size = TIFFStripSize(tif);
2902 imgsize = image->comps[0].w * image->comps[0].h ;
2903 /* Read the Image components
2905 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2906 unsigned char *dat8;
2910 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2911 dat8 = (unsigned char*)buf;
2914 step = 2 + has_alpha + has_alpha;
2916 for (i = 0; i < ssize; i += step) {
2917 if (index < imgsize) {
2918 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0];
2920 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2];
2927 } else if (tiBps == 8) {
2928 step = 1 + has_alpha;
2930 for (i = 0; i < ssize; i += step) {
2931 if (index < imgsize) {
2932 image->comps[0].data[index] = dat8[i + 0];
2934 image->comps[1].data[index] = dat8[i + 1];
2953 #endif /* OPJ_HAVE_LIBTIFF */
2955 /* -->> -->> -->> -->>
2959 <<-- <<-- <<-- <<-- */
2961 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2962 raw_cparameters_t *raw_cp)
2964 int subsampling_dx = parameters->subsampling_dx;
2965 int subsampling_dy = parameters->subsampling_dy;
2968 int i, compno, numcomps, w, h;
2969 OPJ_COLOR_SPACE color_space;
2970 opj_image_cmptparm_t *cmptparm;
2971 opj_image_t * image = NULL;
2974 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2975 raw_cp->rawBitDepth)) == 0) {
2976 fprintf(stderr, "\nError: invalid raw image parameters\n");
2977 fprintf(stderr, "Please use the Format option -F:\n");
2979 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2980 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2981 fprintf(stderr, "Aborting\n");
2985 f = fopen(filename, "rb");
2987 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2988 fprintf(stderr, "Aborting\n");
2991 numcomps = raw_cp->rawComp;
2992 color_space = CLRSPC_SRGB;
2993 w = raw_cp->rawWidth;
2994 h = raw_cp->rawHeight;
2995 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(
2996 opj_image_cmptparm_t));
2998 /* initialize image components */
2999 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
3000 for (i = 0; i < numcomps; i++) {
3001 cmptparm[i].prec = raw_cp->rawBitDepth;
3002 cmptparm[i].bpp = raw_cp->rawBitDepth;
3003 cmptparm[i].sgnd = raw_cp->rawSigned;
3004 cmptparm[i].dx = subsampling_dx;
3005 cmptparm[i].dy = subsampling_dy;
3009 /* create the image */
3010 image = opj_image_create(numcomps, &cmptparm[0], color_space);
3015 /* set image offset and reference grid */
3016 image->x0 = parameters->image_offset_x0;
3017 image->y0 = parameters->image_offset_y0;
3018 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
3019 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
3021 if (raw_cp->rawBitDepth <= 8) {
3022 unsigned char value = 0;
3023 for (compno = 0; compno < numcomps; compno++) {
3024 for (i = 0; i < w * h; i++) {
3025 if (!fread(&value, 1, 1, f)) {
3026 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3030 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
3033 } else if (raw_cp->rawBitDepth <= 16) {
3034 unsigned short value;
3035 for (compno = 0; compno < numcomps; compno++) {
3036 for (i = 0; i < w * h; i++) {
3038 if (!fread(&temp, 1, 1, f)) {
3039 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3044 if (!fread(&temp, 1, 1, f)) {
3045 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3050 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
3055 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3060 if (fread(&ch, 1, 1, f)) {
3061 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
3068 int imagetoraw(opj_image_t * image, const char *outfile)
3070 FILE *rawFile = NULL;
3077 if ((image->numcomps * image->x1 * image->y1) == 0) {
3078 fprintf(stderr, "\nError: invalid raw image parameters\n");
3082 rawFile = fopen(outfile, "wb");
3084 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3088 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
3090 for (compno = 0; compno < image->numcomps; compno++) {
3091 fprintf(stdout, "Component %d characteristics: %dx%dx%d %s\n", compno,
3092 image->comps[compno].w,
3093 image->comps[compno].h, image->comps[compno].prec,
3094 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
3096 w = image->comps[compno].w;
3097 h = image->comps[compno].h;
3099 if (image->comps[compno].prec <= 8) {
3100 if (image->comps[compno].sgnd == 1) {
3102 int mask = (1 << image->comps[compno].prec) - 1;
3103 ptr = image->comps[compno].data;
3104 for (line = 0; line < h; line++) {
3105 for (row = 0; row < w; row++) {
3106 curr = (signed char)(*ptr & mask);
3107 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
3109 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3115 } else if (image->comps[compno].sgnd == 0) {
3117 int mask = (1 << image->comps[compno].prec) - 1;
3118 ptr = image->comps[compno].data;
3119 for (line = 0; line < h; line++) {
3120 for (row = 0; row < w; row++) {
3121 curr = (unsigned char)(*ptr & mask);
3122 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
3124 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3131 } else if (image->comps[compno].prec <= 16) {
3132 if (image->comps[compno].sgnd == 1) {
3133 signed short int curr;
3134 int mask = (1 << image->comps[compno].prec) - 1;
3135 ptr = image->comps[compno].data;
3136 for (line = 0; line < h; line++) {
3137 for (row = 0; row < w; row++) {
3139 curr = (signed short int)(*ptr & mask);
3140 temp = (unsigned char)(curr >> 8);
3141 res = fwrite(&temp, 1, 1, rawFile);
3143 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3146 temp = (unsigned char) curr;
3147 res = fwrite(&temp, 1, 1, rawFile);
3149 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3155 } else if (image->comps[compno].sgnd == 0) {
3156 unsigned short int curr;
3157 int mask = (1 << image->comps[compno].prec) - 1;
3158 ptr = image->comps[compno].data;
3159 for (line = 0; line < h; line++) {
3160 for (row = 0; row < w; row++) {
3162 curr = (unsigned short int)(*ptr & mask);
3163 temp = (unsigned char)(curr >> 8);
3164 res = fwrite(&temp, 1, 1, rawFile);
3166 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3169 temp = (unsigned char) curr;
3170 res = fwrite(&temp, 1, 1, rawFile);
3172 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3179 } else if (image->comps[compno].prec <= 32) {
3180 fprintf(stderr, "More than 16 bits per component no handled yet\n");
3183 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
3191 #ifdef OPJ_HAVE_LIBPNG
3193 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3194 #define MAGIC_SIZE 8
3195 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3197 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3201 double gamma, display_exponent;
3202 int bit_depth, interlace_type, compression_type, filter_type;
3204 png_uint_32 resx, resy;
3206 png_uint_32 width, height;
3207 int color_type, has_alpha, is16;
3210 unsigned char **rows;
3213 opj_image_cmptparm_t cmptparm[4];
3215 unsigned int nr_comp;
3217 unsigned char sigbuf[8];
3219 if ((reader = fopen(read_idf, "rb")) == NULL) {
3220 fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
3227 if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3228 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
3229 fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
3232 /* libpng-VERSION/example.c:
3233 * PC : screen_gamma = 2.2;
3234 * Mac: screen_gamma = 1.7 or 1.0;
3236 display_exponent = 2.2;
3238 if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3239 NULL, NULL, NULL)) == NULL) {
3242 if ((info = png_create_info_struct(png)) == NULL) {
3246 if (setjmp(png_jmpbuf(png))) {
3250 png_init_io(png, reader);
3251 png_set_sig_bytes(png, MAGIC_SIZE);
3253 png_read_info(png, info);
3255 if (png_get_IHDR(png, info, &width, &height,
3256 &bit_depth, &color_type, &interlace_type,
3257 &compression_type, &filter_type) == 0) {
3261 /* png_set_expand():
3262 * expand paletted images to RGB, expand grayscale images of
3263 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3264 * to alpha channels.
3266 if (color_type == PNG_COLOR_TYPE_PALETTE) {
3267 png_set_expand(png);
3268 } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
3269 png_set_expand(png);
3272 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
3273 png_set_expand(png);
3276 is16 = (bit_depth == 16);
3278 /* GRAY => RGB; GRAY_ALPHA => RGBA
3280 if (color_type == PNG_COLOR_TYPE_GRAY
3281 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
3282 png_set_gray_to_rgb(png);
3284 (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
3285 PNG_COLOR_TYPE_RGB_ALPHA);
3287 if (!png_get_gAMA(png, info, &gamma)) {
3291 png_set_gamma(png, display_exponent, gamma);
3293 png_read_update_info(png, info);
3295 png_get_pHYs(png, info, &resx, &resy, &unit);
3297 color_type = png_get_color_type(png, info);
3299 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3301 nr_comp = 3 + has_alpha;
3303 bit_depth = png_get_bit_depth(png, info);
3305 rows = (unsigned char**)calloc(height + 1, sizeof(unsigned char*));
3306 for (i = 0; i < height; ++i) {
3307 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png, info));
3310 png_read_image(png, rows);
3312 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3314 sub_dx = params->subsampling_dx;
3315 sub_dy = params->subsampling_dy;
3317 for (i = 0; i < nr_comp; ++i) {
3318 cmptparm[i].prec = bit_depth;
3319 /* bits_per_pixel: 8 or 16 */
3320 cmptparm[i].bpp = bit_depth;
3321 cmptparm[i].sgnd = 0;
3322 cmptparm[i].dx = sub_dx;
3323 cmptparm[i].dy = sub_dy;
3324 cmptparm[i].w = width;
3325 cmptparm[i].h = height;
3328 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3330 if (image == NULL) {
3334 image->x0 = params->image_offset_x0;
3335 image->y0 = params->image_offset_y0;
3336 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3337 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3339 r = image->comps[0].data;
3340 g = image->comps[1].data;
3341 b = image->comps[2].data;
3342 a = image->comps[3].data;
3344 for (i = 0; i < height; ++i) {
3347 for (j = 0; j < width; ++j) {
3349 *r++ = s[0] << 8 | s[1];
3352 *g++ = s[0] << 8 | s[1];
3355 *b++ = s[0] << 8 | s[1];
3359 *a++ = s[0] << 8 | s[1];
3376 for (i = 0; i < height; ++i) {
3382 png_destroy_read_struct(&png, &info, NULL);
3391 int imagetopng(opj_image_t * image, const char *write_idf)
3396 int *red, *green, *blue, *alpha;
3397 unsigned char *row_buf, *d;
3398 int has_alpha, width, height, nr_comp, color_type;
3399 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3400 int prec, ushift, dshift, is16, force16, force8;
3401 unsigned short mask = 0xffff;
3402 png_color_8 sig_bit;
3404 is16 = force16 = force8 = ushift = dshift = 0;
3406 prec = image->comps[0].prec;
3407 nr_comp = image->numcomps;
3409 if (prec > 8 && prec < 16) {
3411 dshift = prec - ushift;
3414 } else if (prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */
3416 dshift = 8 - ushift;
3421 if (prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) {
3422 fprintf(stderr, "imagetopng: can not create %s"
3423 "\n\twrong bit_depth %d\n", write_idf, prec);
3426 writer = fopen(write_idf, "wb");
3428 if (writer == NULL) {
3435 /* Create and initialize the png_struct with the desired error handler
3436 * functions. If you want to use the default stderr and longjump method,
3437 * you can supply NULL for the last three parameters. We also check that
3438 * the library version is compatible with the one used at compile time,
3439 * in case we are using dynamically linked libraries. REQUIRED.
3441 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3443 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3449 /* Allocate/initialize the image information data. REQUIRED
3451 info = png_create_info_struct(png);
3457 /* Set error handling. REQUIRED if you are not supplying your own
3458 * error handling functions in the png_create_write_struct() call.
3460 if (setjmp(png_jmpbuf(png))) {
3464 /* I/O initialization functions is REQUIRED
3466 png_init_io(png, writer);
3468 /* Set the image information here. Width and height are up to 2^31,
3469 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3470 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3471 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3472 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3473 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3474 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3479 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3480 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3481 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3482 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3485 png_set_compression_level(png, Z_BEST_COMPRESSION);
3489 } else if (prec == 8) {
3491 } else if (prec == 4) {
3493 } else if (prec == 2) {
3495 } else if (prec == 1) {
3500 && image->comps[0].dx == image->comps[1].dx
3501 && image->comps[1].dx == image->comps[2].dx
3502 && image->comps[0].dy == image->comps[1].dy
3503 && image->comps[1].dy == image->comps[2].dy
3504 && image->comps[0].prec == image->comps[1].prec
3505 && image->comps[1].prec == image->comps[2].prec) {
3508 has_alpha = (nr_comp > 3);
3510 is16 = (prec == 16);
3512 width = image->comps[0].w;
3513 height = image->comps[0].h;
3515 red = image->comps[0].data;
3516 green = image->comps[1].data;
3517 blue = image->comps[2].data;
3519 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3522 sig_bit.alpha = prec;
3523 alpha = image->comps[3].data;
3524 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3525 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3529 color_type = PNG_COLOR_TYPE_RGB;
3532 png_set_sBIT(png, info, &sig_bit);
3534 png_set_IHDR(png, info, width, height, prec,
3537 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3538 /*=============================*/
3539 png_write_info(png, info);
3540 /*=============================*/
3542 png_set_packing(png);
3544 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3545 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3546 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3548 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3550 for (y = 0; y < height; ++y) {
3553 for (x = 0; x < width; ++x) {
3559 v = (v << ushift) + (v >> dshift);
3562 *d++ = (unsigned char)(v >> 8);
3563 *d++ = (unsigned char)v;
3565 v = *green + adjustG;
3569 v = (v << ushift) + (v >> dshift);
3572 *d++ = (unsigned char)(v >> 8);
3573 *d++ = (unsigned char)v;
3575 v = *blue + adjustB;
3579 v = (v << ushift) + (v >> dshift);
3582 *d++ = (unsigned char)(v >> 8);
3583 *d++ = (unsigned char)v;
3586 v = *alpha + adjustA;
3590 v = (v << ushift) + (v >> dshift);
3593 *d++ = (unsigned char)(v >> 8);
3594 *d++ = (unsigned char)v;
3603 v = (v << ushift) + (v >> dshift);
3606 *d++ = (unsigned char)(v & mask);
3608 v = *green + adjustG;
3612 v = (v << ushift) + (v >> dshift);
3615 *d++ = (unsigned char)(v & mask);
3617 v = *blue + adjustB;
3621 v = (v << ushift) + (v >> dshift);
3624 *d++ = (unsigned char)(v & mask);
3627 v = *alpha + adjustA;
3631 v = (v << ushift) + (v >> dshift);
3634 *d++ = (unsigned char)(v & mask);
3638 png_write_row(png, row_buf);
3644 else if (nr_comp == 1 /* GRAY */
3645 || (nr_comp == 2 /* GRAY_ALPHA */
3646 && image->comps[0].dx == image->comps[1].dx
3647 && image->comps[0].dy == image->comps[1].dy
3648 && image->comps[0].prec == image->comps[1].prec)) {
3651 red = image->comps[0].data;
3653 sig_bit.gray = prec;
3654 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3657 color_type = PNG_COLOR_TYPE_GRAY;
3661 sig_bit.alpha = prec;
3662 alpha = image->comps[1].data;
3663 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3664 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3666 width = image->comps[0].w;
3667 height = image->comps[0].h;
3669 png_set_IHDR(png, info, width, height, sig_bit.gray,
3672 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3674 png_set_sBIT(png, info, &sig_bit);
3675 /*=============================*/
3676 png_write_info(png, info);
3677 /*=============================*/
3678 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3681 png_set_packing(png);
3685 row_buf = (unsigned char*)
3686 malloc(width * nr_comp * sizeof(unsigned short));
3688 for (y = 0; y < height; ++y) {
3691 for (x = 0; x < width; ++x) {
3696 v = (v << ushift) + (v >> dshift);
3699 *d++ = (unsigned char)(v >> 8);
3700 *d++ = (unsigned char)v;
3706 v = (v << ushift) + (v >> dshift);
3709 *d++ = (unsigned char)(v >> 8);
3710 *d++ = (unsigned char)v;
3713 png_write_row(png, row_buf);
3717 } else { /* prec <= 8 */
3718 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3720 for (y = 0; y < height; ++y) {
3723 for (x = 0; x < width; ++x) {
3728 v = (v << ushift) + (v >> dshift);
3731 *d++ = (unsigned char)(v & mask);
3734 v = *alpha + adjustA;
3738 v = (v << ushift) + (v >> dshift);
3741 *d++ = (unsigned char)(v & mask);
3745 png_write_row(png, row_buf);
3751 fprintf(stderr, "imagetopng: can not create %s\n", write_idf);
3754 png_write_end(png, info);
3761 png_destroy_write_struct(&png, &info);
3771 #endif /* OPJ_HAVE_LIBPNG */