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");
135 id_len = (unsigned char)tga[0];
136 cmap_type = (unsigned char)tga[1];
137 image_type = (unsigned char)tga[2];
138 cmap_index = get_ushort(*(unsigned short*)(&tga[3]));
139 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
140 cmap_entry_size = (unsigned char)tga[7];
143 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
144 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
145 image_w = get_ushort(*(unsigned short*)(&tga[12]));
146 image_h = get_ushort(*(unsigned short*)(&tga[14]));
147 pixel_depth = (unsigned char)tga[16];
148 image_desc = (unsigned char)tga[17];
152 *bits_per_pixel = (unsigned int)pixel_depth;
153 *width = (unsigned int)image_w;
154 *height = (unsigned int)image_h;
156 /* Ignore tga identifier, if present ... */
158 unsigned char *id = (unsigned char *) malloc(id_len);
159 if (!fread(id, id_len, 1, fp)) {
161 "\nError: fread return a number of element different from the expected.\n");
168 /* Test for compressed formats ... not yet supported ...
169 // Note :- 9 - RLE encoded palettized.
170 // 10 - RLE encoded RGB. */
171 if (image_type > 8) {
172 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
176 *flip_image = !(image_desc & 32);
178 /* Palettized formats are not yet supported, skip over the palette, if present ... */
179 palette_size = cmap_len * (cmap_entry_size / 8);
181 if (palette_size > 0) {
182 fprintf(stderr, "File contains a palette - not yet supported.");
183 fseek(fp, palette_size, SEEK_CUR);
188 #ifdef OPJ_BIG_ENDIAN
190 static inline uint16_t swap16(uint16_t x)
192 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
197 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
200 unsigned short image_w, image_h, us0;
201 unsigned char uc0, image_type;
202 unsigned char pixel_depth, image_desc;
204 if (!bits_per_pixel || !width || !height) {
210 if (bits_per_pixel < 256) {
211 pixel_depth = (unsigned char)bits_per_pixel;
213 fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header");
218 if (fwrite(&uc0, 1, 1, fp) != 1) {
219 goto fails; /* id_length */
221 if (fwrite(&uc0, 1, 1, fp) != 1) {
222 goto fails; /* colour_map_type */
225 image_type = 2; /* Uncompressed. */
226 if (fwrite(&image_type, 1, 1, fp) != 1) {
231 if (fwrite(&us0, 2, 1, fp) != 1) {
232 goto fails; /* colour_map_index */
234 if (fwrite(&us0, 2, 1, fp) != 1) {
235 goto fails; /* colour_map_length */
237 if (fwrite(&uc0, 1, 1, fp) != 1) {
238 goto fails; /* colour_map_entry_size */
241 if (fwrite(&us0, 2, 1, fp) != 1) {
242 goto fails; /* x_origin */
244 if (fwrite(&us0, 2, 1, fp) != 1) {
245 goto fails; /* y_origin */
248 image_w = (unsigned short)width;
249 image_h = (unsigned short) height;
251 #ifndef OPJ_BIG_ENDIAN
252 if (fwrite(&image_w, 2, 1, fp) != 1) {
255 if (fwrite(&image_h, 2, 1, fp) != 1) {
259 image_w = swap16(image_w);
260 image_h = swap16(image_h);
261 if (fwrite(&image_w, 2, 1, fp) != 1) {
264 if (fwrite(&image_h, 2, 1, fp) != 1) {
269 if (fwrite(&pixel_depth, 1, 1, fp) != 1) {
273 image_desc = 8; /* 8 bits per component. */
278 if (fwrite(&image_desc, 1, 1, fp) != 1) {
285 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
289 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters)
293 unsigned int image_width, image_height, pixel_bit_depth;
296 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
298 OPJ_COLOR_SPACE color_space;
301 int subsampling_dx, subsampling_dy;
304 f = fopen(filename, "rb");
306 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
310 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height,
315 /* We currently only support 24 & 32 bit tga's ... */
316 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
320 /* initialize image components */
321 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
323 mono = (pixel_bit_depth == 8) ||
324 (pixel_bit_depth == 16); /* Mono with & without alpha. */
325 save_alpha = (pixel_bit_depth == 16) ||
326 (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
329 color_space = CLRSPC_GRAY;
330 numcomps = save_alpha ? 2 : 1;
332 numcomps = save_alpha ? 4 : 3;
333 color_space = CLRSPC_SRGB;
336 subsampling_dx = parameters->subsampling_dx;
337 subsampling_dy = parameters->subsampling_dy;
339 for (i = 0; i < numcomps; i++) {
340 cmptparm[i].prec = 8;
342 cmptparm[i].sgnd = 0;
343 cmptparm[i].dx = subsampling_dx;
344 cmptparm[i].dy = subsampling_dy;
345 cmptparm[i].w = image_width;
346 cmptparm[i].h = image_height;
349 /* create the image */
350 image = opj_image_create(numcomps, &cmptparm[0], color_space);
356 /* set image offset and reference grid */
357 image->x0 = parameters->image_offset_x0;
358 image->y0 = parameters->image_offset_y0;
359 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 +
360 (image_width - 1) * subsampling_dx + 1;
361 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 +
362 (image_height - 1) * subsampling_dy + 1;
365 for (y = 0; y < image_height; y++) {
369 index = (image_height - y - 1) * image_width;
371 index = y * image_width;
375 for (x = 0; x < image_width; x++) {
376 unsigned char r, g, b;
378 if (!fread(&b, 1, 1, f)) {
380 "\nError: fread return a number of element different from the expected.\n");
381 opj_image_destroy(image);
384 if (!fread(&g, 1, 1, f)) {
386 "\nError: fread return a number of element different from the expected.\n");
387 opj_image_destroy(image);
390 if (!fread(&r, 1, 1, f)) {
392 "\nError: fread return a number of element different from the expected.\n");
393 opj_image_destroy(image);
397 image->comps[0].data[index] = r;
398 image->comps[1].data[index] = g;
399 image->comps[2].data[index] = b;
402 } else if (numcomps == 4) {
403 for (x = 0; x < image_width; x++) {
404 unsigned char r, g, b, a;
405 if (!fread(&b, 1, 1, f)) {
407 "\nError: fread return a number of element different from the expected.\n");
408 opj_image_destroy(image);
411 if (!fread(&g, 1, 1, f)) {
413 "\nError: fread return a number of element different from the expected.\n");
414 opj_image_destroy(image);
417 if (!fread(&r, 1, 1, f)) {
419 "\nError: fread return a number of element different from the expected.\n");
420 opj_image_destroy(image);
423 if (!fread(&a, 1, 1, f)) {
425 "\nError: fread return a number of element different from the expected.\n");
426 opj_image_destroy(image);
430 image->comps[0].data[index] = r;
431 image->comps[1].data[index] = g;
432 image->comps[2].data[index] = b;
433 image->comps[3].data[index] = a;
437 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
443 int imagetotga(opj_image_t * image, const char *outfile)
445 int width, height, bpp, x, y;
446 opj_bool write_alpha;
447 int i, adjustR, adjustG, adjustB;
448 unsigned int alpha_channel;
455 fdest = fopen(outfile, "wb");
457 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
461 for (i = 0; i < image->numcomps - 1; i++) {
462 if ((image->comps[0].dx != image->comps[i + 1].dx)
463 || (image->comps[0].dy != image->comps[i + 1].dy)
464 || (image->comps[0].prec != image->comps[i + 1].prec)) {
466 "Unable to create a tga file with such J2K image charateristics.");
471 width = image->comps[0].w;
472 height = image->comps[0].h;
474 /* Mono with alpha, or RGB with alpha. */
475 write_alpha = (image->numcomps == 2) || (image->numcomps == 4);
477 /* Write TGA header */
478 bpp = write_alpha ? 32 : 24;
479 if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
483 alpha_channel = image->numcomps - 1;
485 scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
487 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
488 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
489 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
491 for (y = 0; y < height; y++) {
492 unsigned int index = y * width;
494 for (x = 0; x < width; x++, index++) {
495 r = (float)(image->comps[0].data[index] + adjustR);
497 if (image->numcomps > 2) {
498 g = (float)(image->comps[1].data[index] + adjustG);
499 b = (float)(image->comps[2].data[index] + adjustB);
500 } else { /* Greyscale ... */
505 /* TGA format writes BGR ... */
506 value = (unsigned char)(b * scale);
507 res = fwrite(&value, 1, 1, fdest);
509 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
513 value = (unsigned char)(g * scale);
514 res = fwrite(&value, 1, 1, fdest);
516 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
520 value = (unsigned char)(r * scale);
521 res = fwrite(&value, 1, 1, fdest);
523 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
528 a = (float)(image->comps[alpha_channel].data[index]);
529 value = (unsigned char)(a * scale);
530 res = fwrite(&value, 1, 1, fdest);
532 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
542 /* -->> -->> -->> -->>
546 <<-- <<-- <<-- <<-- */
548 /* WORD defines a two byte word */
549 typedef unsigned short int WORD;
551 /* DWORD defines a four byte word */
552 typedef unsigned int DWORD;
555 WORD bfType; /* 'BM' for Bitmap (19776) */
556 DWORD bfSize; /* Size of the file */
557 WORD bfReserved1; /* Reserved : 0 */
558 WORD bfReserved2; /* Reserved : 0 */
559 DWORD bfOffBits; /* Offset */
560 } BITMAPFILEHEADER_t;
563 DWORD biSize; /* Size of the structure in bytes */
564 DWORD biWidth; /* Width of the image in pixels */
565 DWORD biHeight; /* Height of the image in pixels */
566 WORD biPlanes; /* 1 */
567 WORD biBitCount; /* Number of color bits by pixels */
568 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
569 DWORD biSizeImage; /* Size of the image in bytes */
570 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
571 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
572 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
573 DWORD biClrImportant; /* Number of important color (0: ALL) */
574 } BITMAPINFOHEADER_t;
576 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
578 int subsampling_dx = parameters->subsampling_dx;
579 int subsampling_dy = parameters->subsampling_dy;
581 int i, numcomps, w, h;
582 OPJ_COLOR_SPACE color_space;
583 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
584 opj_image_t * image = NULL;
587 BITMAPFILEHEADER_t File_h;
588 BITMAPINFOHEADER_t Info_h;
590 unsigned char *table_R, *table_G, *table_B;
591 unsigned int j, PAD = 0;
598 IN = fopen(filename, "rb");
600 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
604 File_h.bfType = getc(IN);
605 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
607 if (File_h.bfType != 19778) {
608 fprintf(stderr, "Error, not a BMP file!\n");
614 File_h.bfSize = getc(IN);
615 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
616 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
617 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
619 File_h.bfReserved1 = getc(IN);
620 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
622 File_h.bfReserved2 = getc(IN);
623 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
625 File_h.bfOffBits = getc(IN);
626 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
627 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
628 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
633 Info_h.biSize = getc(IN);
634 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
635 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
636 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
638 if (Info_h.biSize != 40) {
639 fprintf(stderr, "Error, unknown BMP header size %d\n", Info_h.biSize);
643 Info_h.biWidth = getc(IN);
644 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
645 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
646 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
649 Info_h.biHeight = getc(IN);
650 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
651 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
652 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
655 Info_h.biPlanes = getc(IN);
656 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
658 Info_h.biBitCount = getc(IN);
659 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
661 Info_h.biCompression = getc(IN);
662 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
663 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
664 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
666 Info_h.biSizeImage = getc(IN);
667 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
668 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
669 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
671 Info_h.biXpelsPerMeter = getc(IN);
672 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
673 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
674 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
676 Info_h.biYpelsPerMeter = getc(IN);
677 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
678 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
679 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
681 Info_h.biClrUsed = getc(IN);
682 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
683 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
684 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
686 Info_h.biClrImportant = getc(IN);
687 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
688 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
689 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
691 /* Read the data and store them in the OUT file */
693 if (Info_h.biBitCount == 24) {
695 color_space = CLRSPC_SRGB;
696 /* initialize image components */
697 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
698 for (i = 0; i < numcomps; i++) {
699 cmptparm[i].prec = 8;
701 cmptparm[i].sgnd = 0;
702 cmptparm[i].dx = subsampling_dx;
703 cmptparm[i].dy = subsampling_dy;
707 /* create the image */
708 image = opj_image_create(numcomps, &cmptparm[0], color_space);
714 /* set image offset and reference grid */
715 image->x0 = parameters->image_offset_x0;
716 image->y0 = parameters->image_offset_y0;
717 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
718 (w - 1) * subsampling_dx + 1;
719 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
720 (h - 1) * subsampling_dy + 1;
724 /* Place the cursor at the beginning of the image information */
725 fseek(IN, 0, SEEK_SET);
726 fseek(IN, File_h.bfOffBits, SEEK_SET);
731 /* PAD = 4 - (3 * W) % 4; */
732 /* PAD = (PAD == 4) ? 0 : PAD; */
733 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
735 RGB = (unsigned char *)
736 malloc((3 * W + PAD) * H * sizeof(unsigned char));
738 if (fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H,
739 IN) != (3 * W + PAD) * H) {
741 opj_image_destroy(image);
743 "\nError: fread return a number of element different from the expected.\n");
749 for (y = 0; y < (int)H; y++) {
750 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
751 for (x = 0; x < (int)W; x++) {
752 unsigned char *pixel = &scanline[3 * x];
753 image->comps[0].data[index] = pixel[2]; /* R */
754 image->comps[1].data[index] = pixel[1]; /* G */
755 image->comps[2].data[index] = pixel[0]; /* B */
760 }/* if (Info_h.biBitCount == 24) */
761 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { /*RGB */
762 if (Info_h.biClrUsed == 0) {
763 Info_h.biClrUsed = 256;
764 } else if (Info_h.biClrUsed > 256) {
765 Info_h.biClrUsed = 256;
768 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
769 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
770 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
773 for (j = 0; j < Info_h.biClrUsed; j++) {
774 table_B[j] = (unsigned char)getc(IN);
775 table_G[j] = (unsigned char)getc(IN);
776 table_R[j] = (unsigned char)getc(IN);
779 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
785 /* Place the cursor at the beginning of the image information */
786 fseek(IN, 0, SEEK_SET);
787 fseek(IN, File_h.bfOffBits, SEEK_SET);
791 if (Info_h.biWidth % 2) {
795 numcomps = gray_scale ? 1 : 3;
796 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
797 /* initialize image components */
798 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
799 for (i = 0; i < numcomps; i++) {
800 cmptparm[i].prec = 8;
802 cmptparm[i].sgnd = 0;
803 cmptparm[i].dx = subsampling_dx;
804 cmptparm[i].dy = subsampling_dy;
808 /* create the image */
809 image = opj_image_create(numcomps, &cmptparm[0], color_space);
818 /* set image offset and reference grid */
819 image->x0 = parameters->image_offset_x0;
820 image->y0 = parameters->image_offset_y0;
821 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
822 (w - 1) * subsampling_dx + 1;
823 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
824 (h - 1) * subsampling_dy + 1;
828 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
830 if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) {
835 opj_image_destroy(image);
837 "\nError: fread return a number of element different from the expected.\n");
842 for (j = 0; j < W * H; j++) {
843 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
844 image->comps[0].data[index] =
845 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
852 for (j = 0; j < W * H; j++) {
853 if ((j % W < W - 1 && Info_h.biWidth % 2)
854 || !(Info_h.biWidth % 2)) {
855 unsigned char pixel_index =
856 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
857 image->comps[0].data[index] = table_R[pixel_index];
858 image->comps[1].data[index] = table_G[pixel_index];
859 image->comps[2].data[index] = table_B[pixel_index];
869 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/
870 unsigned char *pix, *beyond;
871 int *gray, *red, *green, *blue;
872 unsigned int x, y, max;
876 if (Info_h.biClrUsed == 0) {
877 Info_h.biClrUsed = 256;
878 } else if (Info_h.biClrUsed > 256) {
879 Info_h.biClrUsed = 256;
882 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
883 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
884 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
887 for (j = 0; j < Info_h.biClrUsed; j++) {
888 table_B[j] = (unsigned char)getc(IN);
889 table_G[j] = (unsigned char)getc(IN);
890 table_R[j] = (unsigned char)getc(IN);
892 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
899 numcomps = gray_scale ? 1 : 3;
900 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
901 /* initialize image components */
902 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
903 for (i = 0; i < numcomps; i++) {
904 cmptparm[i].prec = 8;
906 cmptparm[i].sgnd = 0;
907 cmptparm[i].dx = subsampling_dx;
908 cmptparm[i].dy = subsampling_dy;
912 /* create the image */
913 image = opj_image_create(numcomps, &cmptparm[0], color_space);
922 /* set image offset and reference grid */
923 image->x0 = parameters->image_offset_x0;
924 image->y0 = parameters->image_offset_y0;
925 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
926 - 1) * subsampling_dx + 1;
927 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
928 - 1) * subsampling_dy + 1;
932 /* Place the cursor at the beginning of the image information */
933 fseek(IN, 0, SEEK_SET);
934 fseek(IN, File_h.bfOffBits, SEEK_SET);
938 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
939 beyond = RGB + W * H;
949 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++) {
950 *pix = (unsigned char)c1;
955 if (c == 0x00) { /* EOL */
958 pix = RGB + x + (H - y - 1) * W;
959 } else if (c == 0x01) { /* EOP */
961 } else if (c == 0x02) { /* MOVE by dxdy */
966 pix = RGB + (H - y - 1) * W + x;
967 } else { /* 03 .. 255 */
969 for (; i < c && x < W && pix < beyond; i++, x++, pix++) {
971 *pix = (unsigned char)c1;
973 if (c & 1) { /* skip padding byte */
981 gray = image->comps[0].data;
988 *gray++ = table_R[uc];
991 /*int *red, *green, *blue;*/
993 red = image->comps[0].data;
994 green = image->comps[1].data;
995 blue = image->comps[2].data;
1002 *red++ = table_R[uc];
1003 *green++ = table_G[uc];
1004 *blue++ = table_B[uc];
1014 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1015 "is not yet implemented [%d]\n", Info_h.biBitCount);
1021 int imagetobmp(opj_image_t * image, const char *outfile)
1026 int adjustR, adjustG, adjustB;
1028 if (image->comps[0].prec < 8) {
1029 fprintf(stderr, "Unsupported precision: %d\n", image->comps[0].prec);
1032 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1033 && image->comps[1].dx == image->comps[2].dx
1034 && image->comps[0].dy == image->comps[1].dy
1035 && image->comps[1].dy == image->comps[2].dy
1036 && image->comps[0].prec == image->comps[1].prec
1037 && image->comps[1].prec == image->comps[2].prec) {
1039 /* -->> -->> -->> -->>
1041 <<-- <<-- <<-- <<-- */
1043 fdest = fopen(outfile, "wb");
1045 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1049 w = image->comps[0].w;
1050 h = image->comps[0].h;
1052 fprintf(fdest, "BM");
1056 fprintf(fdest, "%c%c%c%c",
1057 (unsigned char)(h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1058 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1059 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1060 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1061 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1062 ((0) >> 24) & 0xff);
1063 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff,
1064 ((54) >> 24) & 0xff);
1068 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1069 ((40) >> 24) & 0xff);
1070 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1071 (unsigned char)((w) >> 8) & 0xff,
1072 (unsigned char)((w) >> 16) & 0xff,
1073 (unsigned char)((w) >> 24) & 0xff);
1074 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1075 (unsigned char)((h) >> 8) & 0xff,
1076 (unsigned char)((h) >> 16) & 0xff,
1077 (unsigned char)((h) >> 24) & 0xff);
1078 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1079 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1080 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1081 ((0) >> 24) & 0xff);
1082 fprintf(fdest, "%c%c%c%c", (unsigned char)(3 * h * w + 3 * h * (w % 2)) & 0xff,
1083 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1084 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1085 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1086 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1087 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1088 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1089 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1090 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1091 ((0) >> 24) & 0xff);
1092 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1093 ((0) >> 24) & 0xff);
1095 if (image->comps[0].prec > 8) {
1096 adjustR = image->comps[0].prec - 8;
1097 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1098 image->comps[0].prec);
1102 if (image->comps[1].prec > 8) {
1103 adjustG = image->comps[1].prec - 8;
1104 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n",
1105 image->comps[1].prec);
1109 if (image->comps[2].prec > 8) {
1110 adjustB = image->comps[2].prec - 8;
1111 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n",
1112 image->comps[2].prec);
1117 for (i = 0; i < w * h; i++) {
1118 unsigned char rc, gc, bc;
1121 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1122 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1123 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1129 rc = (unsigned char)r;
1131 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1132 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1133 g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
1139 gc = (unsigned char)g;
1141 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1142 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1143 b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
1149 bc = (unsigned char)b;
1151 fprintf(fdest, "%c%c%c", bc, gc, rc);
1153 if ((i + 1) % w == 0) {
1154 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) { /* ADD */
1155 fprintf(fdest, "%c", 0);
1160 } else { /* Gray-scale */
1162 /* -->> -->> -->> -->>
1163 8 bits non code (Gray scale)
1164 <<-- <<-- <<-- <<-- */
1166 fdest = fopen(outfile, "wb");
1167 w = image->comps[0].w;
1168 h = image->comps[0].h;
1170 fprintf(fdest, "BM");
1174 fprintf(fdest, "%c%c%c%c",
1175 (unsigned char)(h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1176 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1177 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1178 (unsigned char)((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1179 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1180 ((0) >> 24) & 0xff);
1181 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1182 ((54 + 1024) >> 16) & 0xff,
1183 ((54 + 1024) >> 24) & 0xff);
1187 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1188 ((40) >> 24) & 0xff);
1189 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1190 (unsigned char)((w) >> 8) & 0xff,
1191 (unsigned char)((w) >> 16) & 0xff,
1192 (unsigned char)((w) >> 24) & 0xff);
1193 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1194 (unsigned char)((h) >> 8) & 0xff,
1195 (unsigned char)((h) >> 16) & 0xff,
1196 (unsigned char)((h) >> 24) & 0xff);
1197 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1198 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1199 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1200 ((0) >> 24) & 0xff);
1201 fprintf(fdest, "%c%c%c%c", (unsigned char)(h * w + h * (w % 2)) & 0xff,
1202 (unsigned char)((h * w + h * (w % 2)) >> 8) & 0xff,
1203 (unsigned char)((h * w + h * (w % 2)) >> 16) & 0xff,
1204 (unsigned char)((h * w + h * (w % 2)) >> 24) & 0xff);
1205 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1206 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1207 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1208 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1209 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1210 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1211 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1212 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1214 if (image->comps[0].prec > 8) {
1215 adjustR = image->comps[0].prec - 8;
1216 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1217 image->comps[0].prec);
1222 for (i = 0; i < 256; i++) {
1223 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1226 for (i = 0; i < w * h; i++) {
1229 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1230 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1231 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1238 fprintf(fdest, "%c", (unsigned char)r);
1240 if ((i + 1) % w == 0) {
1241 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) { /* ADD */
1242 fprintf(fdest, "%c", 0);
1252 /* -->> -->> -->> -->>
1256 <<-- <<-- <<-- <<-- */
1259 static unsigned char readuchar(FILE * f)
1262 if (!fread(&c1, 1, 1, f)) {
1264 "\nError: fread return a number of element different from the expected.\n");
1270 static unsigned short readushort(FILE * f, int bigendian)
1272 unsigned char c1, c2;
1273 if (!fread(&c1, 1, 1, f)) {
1275 "\nError: fread return a number of element different from the expected.\n");
1278 if (!fread(&c2, 1, 1, f)) {
1280 "\nError: fread return a number of element different from the expected.\n");
1284 return (c1 << 8) + c2;
1286 return (c2 << 8) + c1;
1290 static unsigned int readuint(FILE * f, int bigendian)
1292 unsigned char c1, c2, c3, c4;
1293 if (!fread(&c1, 1, 1, f)) {
1295 "\nError: fread return a number of element different from the expected.\n");
1298 if (!fread(&c2, 1, 1, f)) {
1300 "\nError: fread return a number of element different from the expected.\n");
1303 if (!fread(&c3, 1, 1, f)) {
1305 "\nError: fread return a number of element different from the expected.\n");
1308 if (!fread(&c4, 1, 1, f)) {
1310 "\nError: fread return a number of element different from the expected.\n");
1314 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1316 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1320 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
1324 int i, numcomps, max;
1325 OPJ_COLOR_SPACE color_space;
1326 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1327 opj_image_t * image = NULL;
1328 int adjustS, ushift, dshift, force8;
1330 char endian1, endian2, sign;
1335 opj_image_comp_t *comp = NULL;
1338 color_space = CLRSPC_GRAY;
1340 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1344 f = fopen(filename, "rb");
1346 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1350 fseek(f, 0, SEEK_SET);
1351 if (fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1,
1352 &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
1354 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1360 while (signtmp[i] != '\0') {
1361 if (signtmp[i] == '-') {
1368 if (endian1 == 'M' && endian2 == 'L') {
1370 } else if (endian2 == 'M' && endian1 == 'L') {
1373 fprintf(stderr, "Bad pgx header, please check input file\n");
1377 /* initialize image component */
1379 cmptparm.x0 = parameters->image_offset_x0;
1380 cmptparm.y0 = parameters->image_offset_y0;
1381 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
1382 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1383 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
1384 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1394 dshift = prec - ushift;
1395 if (cmptparm.sgnd) {
1396 adjustS = (1 << (prec - 1));
1403 ushift = dshift = force8 = adjustS = 0;
1406 cmptparm.prec = prec;
1407 cmptparm.bpp = prec;
1408 cmptparm.dx = parameters->subsampling_dx;
1409 cmptparm.dy = parameters->subsampling_dy;
1411 /* create the image */
1412 image = opj_image_create(numcomps, &cmptparm, color_space);
1417 /* set image offset and reference grid */
1418 image->x0 = cmptparm.x0;
1419 image->y0 = cmptparm.x0;
1420 image->x1 = cmptparm.w;
1421 image->y1 = cmptparm.h;
1423 /* set image data */
1425 comp = &image->comps[0];
1427 for (i = 0; i < w * h; i++) {
1430 v = readuchar(f) + adjustS;
1431 v = (v << ushift) + (v >> dshift);
1432 comp->data[i] = (unsigned char)v;
1440 if (comp->prec == 8) {
1444 v = (char) readuchar(f);
1446 } else if (comp->prec <= 16) {
1448 v = readushort(f, bigendian);
1450 v = (short) readushort(f, bigendian);
1454 v = readuint(f, bigendian);
1456 v = (int) readuint(f, bigendian);
1465 comp->bpp = int_floorlog2(max) + 1;
1470 int imagetopgx(opj_image_t * image, const char *outfile)
1476 for (compno = 0; compno < image->numcomps; compno++) {
1477 opj_image_comp_t *comp = &image->comps[compno];
1478 char bname[256]; /* buffer for name */
1479 char *name = bname; /* pointer */
1482 const size_t olen = strlen(outfile);
1483 const size_t dotpos = olen - 4;
1484 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1485 if (outfile[dotpos] != '.') {
1486 /* `pgx` was recognized but there is no dot at expected position */
1487 fprintf(stderr, "ERROR -> Impossible happen.");
1491 name = (char*)malloc(total + 1);
1493 strncpy(name, outfile, dotpos);
1494 /*if (image->numcomps > 1) {*/
1495 sprintf(name + dotpos, "_%d.pgx", compno);
1497 strcpy(name+dotpos, ".pgx");
1499 fdest = fopen(name, "wb");
1501 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1504 /* don't need name anymore */
1509 w = image->comps[compno].w;
1510 h = image->comps[compno].h;
1512 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1513 if (comp->prec <= 8) {
1515 } else if (comp->prec <= 16) {
1520 for (i = 0; i < w * h; i++) {
1521 int v = image->comps[compno].data[i];
1522 for (j = nbytes - 1; j >= 0; j--) {
1523 char byte = (char)(v >> (j * 8));
1524 res = fwrite(&byte, 1, 1, fdest);
1526 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1538 /* -->> -->> -->> -->>
1542 <<-- <<-- <<-- <<-- */
1545 int width, height, maxval, depth, format;
1546 char rgb, rgba, gray, graya, bw;
1550 static char *skip_white(char *s)
1553 if (*s == '\n' || *s == '\r') {
1565 static char *skip_int(char *start, int *out_n)
1573 s = skip_white(start);
1587 *out_n = atoi(start);
1592 static char *skip_idf(char *start, char out_idf[256])
1597 s = skip_white(start);
1604 if (isalpha(*s) || *s == '_') {
1612 strncpy(out_idf, start, 255);
1617 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1620 int format, have_wh, end, ttype;
1621 char idf[256], type[256];
1624 if (fgets(line, 250, reader) == NULL) {
1625 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1629 if (line[0] != 'P') {
1630 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1633 format = atoi(line + 1);
1634 if (format < 1 || format > 7) {
1635 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1638 ph->format = format;
1639 ttype = end = have_wh = 0;
1641 while (fgets(line, 250, reader)) {
1649 s = skip_idf(s, idf);
1651 if (s == NULL || *s == 0) {
1655 if (strcmp(idf, "ENDHDR") == 0) {
1659 if (strcmp(idf, "WIDTH") == 0) {
1660 s = skip_int(s, &ph->width);
1661 if (s == NULL || *s == 0) {
1667 if (strcmp(idf, "HEIGHT") == 0) {
1668 s = skip_int(s, &ph->height);
1669 if (s == NULL || *s == 0) {
1675 if (strcmp(idf, "DEPTH") == 0) {
1676 s = skip_int(s, &ph->depth);
1677 if (s == NULL || *s == 0) {
1683 if (strcmp(idf, "MAXVAL") == 0) {
1684 s = skip_int(s, &ph->maxval);
1685 if (s == NULL || *s == 0) {
1691 if (strcmp(idf, "TUPLTYPE") == 0) {
1692 s = skip_idf(s, type);
1693 if (s == NULL || *s == 0) {
1697 if (strcmp(type, "BLACKANDWHITE") == 0) {
1702 if (strcmp(type, "GRAYSCALE") == 0) {
1707 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1712 if (strcmp(type, "RGB") == 0) {
1717 if (strcmp(type, "RGB_ALPHA") == 0) {
1722 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1725 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1727 } /* if(format == 7) */
1730 s = skip_int(s, &ph->width);
1732 s = skip_int(s, &ph->height);
1736 if (format == 1 || format == 4) {
1742 if (format == 2 || format == 3 || format == 5 || format == 6) {
1743 /* P2, P3, P5, P6: */
1744 s = skip_int(s, &ph->maxval);
1746 if (ph->maxval > 65535) {
1751 }/* while(fgets( ) */
1752 if (format == 2 || format == 3 || format > 4) {
1753 if (ph->maxval < 1 || ph->maxval > 65535) {
1757 if (ph->width < 1 || ph->height < 1) {
1763 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1766 if (ph->depth < 1 || ph->depth > 4) {
1770 if (ph->width && ph->height && ph->depth & ph->maxval && ttype) {
1774 if (format != 1 && format != 4) {
1775 if (ph->width && ph->height && ph->maxval) {
1779 if (ph->width && ph->height) {
1787 static int has_prec(int val)
1837 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1839 int subsampling_dx = parameters->subsampling_dx;
1840 int subsampling_dy = parameters->subsampling_dy;
1843 int i, compno, numcomps, w, h, prec, format;
1844 OPJ_COLOR_SPACE color_space;
1845 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1846 opj_image_t * image = NULL;
1847 struct pnm_header header_info;
1849 if ((fp = fopen(filename, "rb")) == NULL) {
1850 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1853 memset(&header_info, 0, sizeof(struct pnm_header));
1855 read_pnm_header(fp, &header_info);
1857 if (!header_info.ok) {
1862 format = header_info.format;
1865 case 1: /* ascii bitmap */
1866 case 4: /* raw bitmap */
1870 case 2: /* ascii greymap */
1871 case 5: /* raw greymap */
1875 case 3: /* ascii pixmap */
1876 case 6: /* raw pixmap */
1880 case 7: /* arbitrary map */
1881 numcomps = header_info.depth;
1889 color_space = CLRSPC_GRAY; /* GRAY, GRAYA */
1891 color_space = CLRSPC_SRGB; /* RGB, RGBA */
1894 prec = has_prec(header_info.maxval);
1900 w = header_info.width;
1901 h = header_info.height;
1902 subsampling_dx = parameters->subsampling_dx;
1903 subsampling_dy = parameters->subsampling_dy;
1905 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1907 for (i = 0; i < numcomps; i++) {
1908 cmptparm[i].prec = prec;
1909 cmptparm[i].bpp = prec;
1910 cmptparm[i].sgnd = 0;
1911 cmptparm[i].dx = subsampling_dx;
1912 cmptparm[i].dy = subsampling_dy;
1916 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1923 /* set image offset and reference grid */
1924 image->x0 = parameters->image_offset_x0;
1925 image->y0 = parameters->image_offset_y0;
1926 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1927 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1929 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1932 for (i = 0; i < w * h; i++) {
1933 for (compno = 0; compno < numcomps; compno++) {
1935 if (fscanf(fp, "%u", &index) != 1) {
1937 "\nWARNING: fscanf return a number of element different from the expected.\n");
1940 image->comps[compno].data[i] = (index * 255) / header_info.maxval;
1943 } else if ((format == 5)
1946 && (header_info.gray || header_info.graya
1947 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1948 unsigned char c0, c1, one;
1952 for (i = 0; i < w * h; i++) {
1953 for (compno = 0; compno < numcomps; compno++) {
1954 if (!fread(&c0, 1, 1, fp)) {
1956 "\nError: fread return a number of element different from the expected.\n");
1959 image->comps[compno].data[i] = c0;
1961 if (!fread(&c1, 1, 1, fp)) {
1963 "\nError: fread return a number of element different from the expected.\n");
1966 image->comps[compno].data[i] = ((c0 << 8) | c1);
1970 } else if (format == 1) { /* ascii bitmap */
1971 for (i = 0; i < w * h; i++) {
1974 if (fscanf(fp, "%u", &index) != 1) {
1976 "\nWARNING: fscanf return a number of element different from the expected.\n");
1979 image->comps[0].data[i] = (index ? 0 : 255);
1981 } else if (format == 4) {
1986 for (y = 0; y < h; ++y) {
1990 for (x = 0; x < w; ++x) {
1993 uc = (unsigned char)getc(fp);
1995 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
2000 } else if ((format == 7 && header_info.bw)) { /*MONO*/
2003 for (i = 0; i < w * h; ++i) {
2004 if (!fread(&uc, 1, 1, fp)) {
2006 "\nError: fread return a number of element different from the expected.\n");
2008 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
2016 int imagetopnm(opj_image_t * image, const char *outfile)
2018 int *red, *green, *blue, *alpha;
2020 int i, compno, ncomp;
2021 int adjustR, adjustG, adjustB, adjustA;
2022 int fails, two, want_gray, has_alpha, triple;
2025 const char *tmp = outfile;
2028 if ((prec = image->comps[0].prec) > 16) {
2029 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
2030 "\n\t: refused.\n", __FILE__, __LINE__, prec);
2033 two = has_alpha = 0;
2035 ncomp = image->numcomps;
2041 want_gray = (*tmp == 'g' || *tmp == 'G');
2042 ncomp = image->numcomps;
2048 if (ncomp == 2 /* GRAYA */
2049 || (ncomp > 2 /* RGB, RGBA */
2050 && image->comps[0].dx == image->comps[1].dx
2051 && image->comps[1].dx == image->comps[2].dx
2052 && image->comps[0].dy == image->comps[1].dy
2053 && image->comps[1].dy == image->comps[2].dy
2054 && image->comps[0].prec == image->comps[1].prec
2055 && image->comps[1].prec == image->comps[2].prec
2057 fdest = fopen(outfile, "wb");
2060 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
2064 triple = (ncomp > 2);
2065 wr = image->comps[0].w;
2066 hr = image->comps[0].h;
2067 max = (1 << prec) - 1;
2068 has_alpha = (ncomp == 4 || ncomp == 2);
2070 red = image->comps[0].data;
2073 green = image->comps[1].data;
2074 blue = image->comps[2].data;
2076 green = blue = NULL;
2080 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
2082 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
2083 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2084 wr, hr, ncomp, max, tt);
2085 alpha = image->comps[ncomp - 1].data;
2086 adjustA = (image->comps[ncomp - 1].sgnd ?
2087 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2089 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2090 opj_version(), wr, hr, max);
2093 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2096 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2097 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2099 adjustG = adjustB = 0;
2102 for (i = 0; i < wr * hr; ++i) {
2107 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2110 v = *green + adjustG;
2113 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2115 v = *blue + adjustB;
2118 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2123 v = *alpha + adjustA;
2126 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2134 fprintf(fdest, "%c", (unsigned char)*red++);
2136 fprintf(fdest, "%c%c", (unsigned char)*green++, (unsigned char)*blue++);
2140 fprintf(fdest, "%c", (unsigned char)*alpha++);
2151 if (image->numcomps > ncomp) {
2152 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2153 fprintf(stderr, " is written to the file\n");
2155 destname = (char*)malloc(strlen(outfile) + 8);
2157 for (compno = 0; compno < ncomp; compno++) {
2159 sprintf(destname, "%d.%s", compno, outfile);
2161 sprintf(destname, "%s", outfile);
2164 fdest = fopen(destname, "wb");
2166 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2170 wr = image->comps[compno].w;
2171 hr = image->comps[compno].h;
2172 prec = image->comps[compno].prec;
2173 max = (1 << prec) - 1;
2175 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2176 opj_version(), wr, hr, max);
2178 red = image->comps[compno].data;
2180 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2183 for (i = 0; i < wr * hr; i++) {
2187 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2192 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2195 } else { /* prec <= 8 */
2196 for (i = 0; i < wr * hr; ++i) {
2197 fprintf(fdest, "%c", (unsigned char)(*red + adjustR));
2208 #ifdef OPJ_HAVE_LIBTIFF
2209 /* -->> -->> -->> -->>
2213 <<-- <<-- <<-- <<-- */
2215 int imagetotif(opj_image_t * image, const char *outfile)
2217 int width, height, imgsize;
2218 int bps, index, adjust, sgnd;
2219 int ushift, dshift, has_alpha, force16;
2225 ushift = dshift = force16 = has_alpha = 0;
2226 bps = image->comps[0].prec;
2228 if (bps > 8 && bps < 16) {
2230 dshift = bps - ushift;
2235 if (bps != 8 && bps != 16) {
2236 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2238 fprintf(stderr, "\tAborting\n");
2241 tif = TIFFOpen(outfile, "wb");
2244 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2247 sgnd = image->comps[0].sgnd;
2248 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2250 if (image->numcomps >= 3
2251 && image->comps[0].dx == image->comps[1].dx
2252 && image->comps[1].dx == image->comps[2].dx
2253 && image->comps[0].dy == image->comps[1].dy
2254 && image->comps[1].dy == image->comps[2].dy
2255 && image->comps[0].prec == image->comps[1].prec
2256 && image->comps[1].prec == image->comps[2].prec) {
2257 has_alpha = (image->numcomps == 4);
2259 width = image->comps[0].w;
2260 height = image->comps[0].h;
2261 imgsize = width * height ;
2263 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2264 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2265 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2266 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2267 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2268 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2269 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2270 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2271 strip_size = TIFFStripSize(tif);
2272 buf = _TIFFmalloc(strip_size);
2275 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2276 unsigned char *dat8;
2277 tsize_t i, ssize, last_i = 0;
2279 ssize = TIFFStripSize(tif);
2280 dat8 = (unsigned char*)buf;
2283 step = 3 + has_alpha;
2286 for (i = 0; i < ssize - restx; i += step) {
2289 if (index < imgsize) {
2290 r = image->comps[0].data[index];
2291 g = image->comps[1].data[index];
2292 b = image->comps[2].data[index];
2294 a = image->comps[3].data[index];
2319 if (last_i < ssize) {
2320 for (i = last_i; i < ssize; i += step) {
2323 if (index < imgsize) {
2324 r = image->comps[0].data[index];
2325 g = image->comps[1].data[index];
2326 b = image->comps[2].data[index];
2328 a = image->comps[3].data[index];
2340 if (i + 1 < ssize) {
2345 if (i + 2 < ssize) {
2351 if (i + 3 < ssize) {
2362 }/*if(last_i < ssize)*/
2365 else if (bps == 16) {
2366 step = 6 + has_alpha + has_alpha;
2369 for (i = 0; i < ssize - restx ; i += step) {
2372 if (index < imgsize) {
2373 r = image->comps[0].data[index];
2374 g = image->comps[1].data[index];
2375 b = image->comps[2].data[index];
2377 a = image->comps[3].data[index];
2389 r = (r << ushift) + (r >> dshift);
2390 g = (g << ushift) + (g >> dshift);
2391 b = (b << ushift) + (b >> dshift);
2393 a = (a << ushift) + (a >> dshift);
2396 dat8[i + 0] = r; /*LSB*/
2397 dat8[i + 1] = (r >> 8); /*MSB*/
2399 dat8[i + 3] = (g >> 8);
2401 dat8[i + 5] = (b >> 8);
2404 dat8[i + 7] = (a >> 8);
2413 if (last_i < ssize) {
2414 for (i = last_i ; i < ssize ; i += step) {
2417 if (index < imgsize) {
2418 r = image->comps[0].data[index];
2419 g = image->comps[1].data[index];
2420 b = image->comps[2].data[index];
2422 a = image->comps[3].data[index];
2434 r = (r << ushift) + (r >> dshift);
2435 g = (g << ushift) + (g >> dshift);
2436 b = (b << ushift) + (b >> dshift);
2438 a = (a << ushift) + (a >> dshift);
2441 dat8[i + 0] = r; /*LSB*/
2442 if (i + 1 < ssize) {
2443 dat8[i + 1] = (r >> 8);
2447 if (i + 2 < ssize) {
2452 if (i + 3 < ssize) {
2453 dat8[i + 3] = (g >> 8);
2457 if (i + 4 < ssize) {
2462 if (i + 5 < ssize) {
2463 dat8[i + 5] = (b >> 8);
2469 if (i + 6 < ssize) {
2474 if (i + 7 < ssize) {
2475 dat8[i + 7] = (a >> 8);
2485 }/*if(last_i < ssize)*/
2488 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2489 }/*for(strip = 0; )*/
2491 _TIFFfree((void*)buf);
2497 if (image->numcomps == 1 /* GRAY */
2498 || (image->numcomps == 2 /* GRAY_ALPHA */
2499 && image->comps[0].dx == image->comps[1].dx
2500 && image->comps[0].dy == image->comps[1].dy
2501 && image->comps[0].prec == image->comps[1].prec)) {
2504 has_alpha = (image->numcomps == 2);
2506 width = image->comps[0].w;
2507 height = image->comps[0].h;
2508 imgsize = width * height;
2511 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2512 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2513 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2514 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2515 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2516 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2517 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2518 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2520 /* Get a buffer for the data */
2521 strip_size = TIFFStripSize(tif);
2522 buf = _TIFFmalloc(strip_size);
2525 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2526 unsigned char *dat8;
2527 tsize_t i, ssize = TIFFStripSize(tif);
2528 dat8 = (unsigned char*)buf;
2531 step = 1 + has_alpha;
2533 for (i = 0; i < ssize; i += step) {
2534 if (index < imgsize) {
2537 r = image->comps[0].data[index];
2539 a = image->comps[1].data[index];
2558 else if (bps == 16) {
2559 step = 2 + has_alpha + has_alpha;
2561 for (i = 0; i < ssize; i += step) {
2562 if (index < imgsize) {
2565 r = image->comps[0].data[index];
2567 a = image->comps[1].data[index];
2577 r = (r << ushift) + (r >> dshift);
2579 a = (a << ushift) + (a >> dshift);
2582 dat8[i + 0] = r; /*LSB*/
2583 dat8[i + 1] = r >> 8; /*MSB*/
2586 dat8[i + 3] = a >> 8;
2589 }/*if(index < imgsize)*/
2595 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2606 fprintf(stderr, "imagetotif: Bad color format.\n"
2607 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2608 fprintf(stderr, "\tFOUND: numcomps(%d)\n\tAborting\n",
2615 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2616 * CINEMA : 12 bit precision
2618 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2620 int subsampling_dx = parameters->subsampling_dx;
2621 int subsampling_dy = parameters->subsampling_dy;
2626 int j, numcomps, w, h, index;
2627 OPJ_COLOR_SPACE color_space;
2628 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2629 opj_image_t *image = NULL;
2632 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2633 unsigned int tiWidth, tiHeight;
2635 tif = TIFFOpen(filename, "r");
2638 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2641 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2642 tiWidth = tiHeight = 0;
2644 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2645 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2646 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2647 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2648 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2649 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2650 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2655 unsigned short b = tiBps, p = tiPhoto;
2657 if (tiBps != 8 && tiBps != 16 && tiBps != 12) {
2660 if (tiPhoto != 1 && tiPhoto != 2) {
2666 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits"
2667 " implemented\n", tiBps);
2669 fprintf(stderr, "tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2670 " and GRAY(A) has been implemented\n", (int) tiPhoto);
2672 fprintf(stderr, "\tAborting\n");
2678 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2680 uint16 extrasamples;
2682 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2683 &extrasamples, &sampleinfo);
2685 if (extrasamples >= 1) {
2686 switch (sampleinfo[0]) {
2687 case EXTRASAMPLE_UNSPECIFIED:
2688 /* Workaround for some images without correct info about alpha channel
2695 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2696 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2700 } else /* extrasamples == 0 */
2701 if (tiSpp == 4 || tiSpp == 2) {
2706 /* initialize image components
2708 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2710 if (tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */
2711 numcomps = 3 + has_alpha;
2712 color_space = CLRSPC_SRGB;
2714 for (j = 0; j < numcomps; j++) {
2715 if (parameters->cp_cinema) {
2716 cmptparm[j].prec = 12;
2717 cmptparm[j].bpp = 12;
2719 cmptparm[j].prec = tiBps;
2720 cmptparm[j].bpp = tiBps;
2722 cmptparm[j].dx = subsampling_dx;
2723 cmptparm[j].dy = subsampling_dy;
2728 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2734 /* set image offset and reference grid
2736 image->x0 = parameters->image_offset_x0;
2737 image->y0 = parameters->image_offset_y0;
2738 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2739 image->x0 + (w - 1) * subsampling_dx + 1;
2740 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2741 image->y0 + (h - 1) * subsampling_dy + 1;
2743 buf = _TIFFmalloc(TIFFStripSize(tif));
2745 strip_size = TIFFStripSize(tif);
2747 imgsize = image->comps[0].w * image->comps[0].h ;
2748 /* Read the Image components
2750 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2751 unsigned char *dat8;
2754 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2755 dat8 = (unsigned char*)buf;
2758 step = 6 + has_alpha + has_alpha;
2760 for (i = 0; i < ssize; i += step) {
2761 if (index < imgsize) {
2762 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0]; /* R */
2763 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2]; /* G */
2764 image->comps[2].data[index] = (dat8[i + 5] << 8) | dat8[i + 4]; /* B */
2766 image->comps[3].data[index] = (dat8[i + 7] << 8) | dat8[i + 6];
2769 if (parameters->cp_cinema) {
2770 /* Rounding 16 to 12 bits
2772 image->comps[0].data[index] =
2773 (image->comps[0].data[index] + 0x08) >> 4 ;
2774 image->comps[1].data[index] =
2775 (image->comps[1].data[index] + 0x08) >> 4 ;
2776 image->comps[2].data[index] =
2777 (image->comps[2].data[index] + 0x08) >> 4 ;
2779 image->comps[3].data[index] =
2780 (image->comps[3].data[index] + 0x08) >> 4 ;
2787 }/*if(tiBps == 16)*/
2788 else if (tiBps == 8) {
2789 step = 3 + has_alpha;
2791 for (i = 0; i < ssize; i += step) {
2792 if (index < imgsize) {
2793 image->comps[0].data[index] = dat8[i + 0]; /* R */
2794 image->comps[1].data[index] = dat8[i + 1]; /* G */
2795 image->comps[2].data[index] = dat8[i + 2]; /* B */
2797 image->comps[3].data[index] = dat8[i + 3];
2800 if (parameters->cp_cinema) {
2801 /* Rounding 8 to 12 bits
2803 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2804 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2805 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2807 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2816 }/*if( tiBps == 8)*/
2817 else if (tiBps == 12) { /* CINEMA file */
2820 for (i = 0; i < ssize; i += step) {
2821 if ((index < imgsize) & (index + 1 < imgsize)) {
2822 image->comps[0].data[index] = (dat8[i + 0] << 4) | (dat8[i + 1] >> 4);
2823 image->comps[1].data[index] = ((dat8[i + 1] & 0x0f) << 8) | dat8[i + 2];
2825 image->comps[2].data[index] = (dat8[i + 3] << 4) | (dat8[i + 4] >> 4);
2826 image->comps[0].data[index + 1] = ((dat8[i + 4] & 0x0f) << 8) | dat8[i + 5];
2828 image->comps[1].data[index + 1] = (dat8[i + 6] << 4) |
2830 image->comps[2].data[index + 1] = ((dat8[i + 7] & 0x0f) << 8) | dat8[i + 8];
2838 }/*for(strip = 0; )*/
2846 if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */
2847 numcomps = 1 + has_alpha;
2848 color_space = CLRSPC_GRAY;
2850 for (j = 0; j < numcomps; ++j) {
2851 cmptparm[j].prec = tiBps;
2852 cmptparm[j].bpp = tiBps;
2853 cmptparm[j].dx = subsampling_dx;
2854 cmptparm[j].dy = subsampling_dy;
2858 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2864 /* set image offset and reference grid
2866 image->x0 = parameters->image_offset_x0;
2867 image->y0 = parameters->image_offset_y0;
2868 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2869 image->x0 + (w - 1) * subsampling_dx + 1;
2870 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2871 image->y0 + (h - 1) * subsampling_dy + 1;
2873 buf = _TIFFmalloc(TIFFStripSize(tif));
2875 strip_size = TIFFStripSize(tif);
2877 imgsize = image->comps[0].w * image->comps[0].h ;
2878 /* Read the Image components
2880 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2881 unsigned char *dat8;
2885 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2886 dat8 = (unsigned char*)buf;
2889 step = 2 + has_alpha + has_alpha;
2891 for (i = 0; i < ssize; i += step) {
2892 if (index < imgsize) {
2893 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0];
2895 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2];
2902 } else if (tiBps == 8) {
2903 step = 1 + has_alpha;
2905 for (i = 0; i < ssize; i += step) {
2906 if (index < imgsize) {
2907 image->comps[0].data[index] = dat8[i + 0];
2909 image->comps[1].data[index] = dat8[i + 1];
2928 #endif /* OPJ_HAVE_LIBTIFF */
2930 /* -->> -->> -->> -->>
2934 <<-- <<-- <<-- <<-- */
2936 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2937 raw_cparameters_t *raw_cp)
2939 int subsampling_dx = parameters->subsampling_dx;
2940 int subsampling_dy = parameters->subsampling_dy;
2943 int i, compno, numcomps, w, h;
2944 OPJ_COLOR_SPACE color_space;
2945 opj_image_cmptparm_t *cmptparm;
2946 opj_image_t * image = NULL;
2949 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2950 raw_cp->rawBitDepth)) == 0) {
2951 fprintf(stderr, "\nError: invalid raw image parameters\n");
2952 fprintf(stderr, "Please use the Format option -F:\n");
2954 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2955 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2956 fprintf(stderr, "Aborting\n");
2960 f = fopen(filename, "rb");
2962 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2963 fprintf(stderr, "Aborting\n");
2966 numcomps = raw_cp->rawComp;
2967 color_space = CLRSPC_SRGB;
2968 w = raw_cp->rawWidth;
2969 h = raw_cp->rawHeight;
2970 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(
2971 opj_image_cmptparm_t));
2973 /* initialize image components */
2974 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2975 for (i = 0; i < numcomps; i++) {
2976 cmptparm[i].prec = raw_cp->rawBitDepth;
2977 cmptparm[i].bpp = raw_cp->rawBitDepth;
2978 cmptparm[i].sgnd = raw_cp->rawSigned;
2979 cmptparm[i].dx = subsampling_dx;
2980 cmptparm[i].dy = subsampling_dy;
2984 /* create the image */
2985 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2990 /* set image offset and reference grid */
2991 image->x0 = parameters->image_offset_x0;
2992 image->y0 = parameters->image_offset_y0;
2993 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2994 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2996 if (raw_cp->rawBitDepth <= 8) {
2997 unsigned char value = 0;
2998 for (compno = 0; compno < numcomps; compno++) {
2999 for (i = 0; i < w * h; i++) {
3000 if (!fread(&value, 1, 1, f)) {
3001 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3005 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
3008 } else if (raw_cp->rawBitDepth <= 16) {
3009 unsigned short value;
3010 for (compno = 0; compno < numcomps; compno++) {
3011 for (i = 0; i < w * h; i++) {
3013 if (!fread(&temp, 1, 1, f)) {
3014 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3019 if (!fread(&temp, 1, 1, f)) {
3020 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3025 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
3030 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3035 if (fread(&ch, 1, 1, f)) {
3036 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
3043 int imagetoraw(opj_image_t * image, const char *outfile)
3045 FILE *rawFile = NULL;
3052 if ((image->numcomps * image->x1 * image->y1) == 0) {
3053 fprintf(stderr, "\nError: invalid raw image parameters\n");
3057 rawFile = fopen(outfile, "wb");
3059 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3063 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
3065 for (compno = 0; compno < image->numcomps; compno++) {
3066 fprintf(stdout, "Component %d characteristics: %dx%dx%d %s\n", compno,
3067 image->comps[compno].w,
3068 image->comps[compno].h, image->comps[compno].prec,
3069 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
3071 w = image->comps[compno].w;
3072 h = image->comps[compno].h;
3074 if (image->comps[compno].prec <= 8) {
3075 if (image->comps[compno].sgnd == 1) {
3077 int mask = (1 << image->comps[compno].prec) - 1;
3078 ptr = image->comps[compno].data;
3079 for (line = 0; line < h; line++) {
3080 for (row = 0; row < w; row++) {
3081 curr = (signed char)(*ptr & mask);
3082 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
3084 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3090 } else if (image->comps[compno].sgnd == 0) {
3092 int mask = (1 << image->comps[compno].prec) - 1;
3093 ptr = image->comps[compno].data;
3094 for (line = 0; line < h; line++) {
3095 for (row = 0; row < w; row++) {
3096 curr = (unsigned char)(*ptr & mask);
3097 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
3099 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3106 } else if (image->comps[compno].prec <= 16) {
3107 if (image->comps[compno].sgnd == 1) {
3108 signed short int curr;
3109 int mask = (1 << image->comps[compno].prec) - 1;
3110 ptr = image->comps[compno].data;
3111 for (line = 0; line < h; line++) {
3112 for (row = 0; row < w; row++) {
3114 curr = (signed short int)(*ptr & mask);
3115 temp = (unsigned char)(curr >> 8);
3116 res = fwrite(&temp, 1, 1, rawFile);
3118 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3121 temp = (unsigned char) curr;
3122 res = fwrite(&temp, 1, 1, rawFile);
3124 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3130 } else if (image->comps[compno].sgnd == 0) {
3131 unsigned short int curr;
3132 int mask = (1 << image->comps[compno].prec) - 1;
3133 ptr = image->comps[compno].data;
3134 for (line = 0; line < h; line++) {
3135 for (row = 0; row < w; row++) {
3137 curr = (unsigned short int)(*ptr & mask);
3138 temp = (unsigned char)(curr >> 8);
3139 res = fwrite(&temp, 1, 1, rawFile);
3141 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3144 temp = (unsigned char) curr;
3145 res = fwrite(&temp, 1, 1, rawFile);
3147 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3154 } else if (image->comps[compno].prec <= 32) {
3155 fprintf(stderr, "More than 16 bits per component no handled yet\n");
3158 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
3166 #ifdef OPJ_HAVE_LIBPNG
3168 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3169 #define MAGIC_SIZE 8
3170 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3172 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3176 double gamma, display_exponent;
3177 int bit_depth, interlace_type, compression_type, filter_type;
3179 png_uint_32 resx, resy;
3181 png_uint_32 width, height;
3182 int color_type, has_alpha, is16;
3185 unsigned char **rows;
3188 opj_image_cmptparm_t cmptparm[4];
3190 unsigned int nr_comp;
3192 unsigned char sigbuf[8];
3194 if ((reader = fopen(read_idf, "rb")) == NULL) {
3195 fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
3202 if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3203 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
3204 fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
3207 /* libpng-VERSION/example.c:
3208 * PC : screen_gamma = 2.2;
3209 * Mac: screen_gamma = 1.7 or 1.0;
3211 display_exponent = 2.2;
3213 if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3214 NULL, NULL, NULL)) == NULL) {
3217 if ((info = png_create_info_struct(png)) == NULL) {
3221 if (setjmp(png_jmpbuf(png))) {
3225 png_init_io(png, reader);
3226 png_set_sig_bytes(png, MAGIC_SIZE);
3228 png_read_info(png, info);
3230 if (png_get_IHDR(png, info, &width, &height,
3231 &bit_depth, &color_type, &interlace_type,
3232 &compression_type, &filter_type) == 0) {
3236 /* png_set_expand():
3237 * expand paletted images to RGB, expand grayscale images of
3238 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3239 * to alpha channels.
3241 if (color_type == PNG_COLOR_TYPE_PALETTE) {
3242 png_set_expand(png);
3243 } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
3244 png_set_expand(png);
3247 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
3248 png_set_expand(png);
3251 is16 = (bit_depth == 16);
3253 /* GRAY => RGB; GRAY_ALPHA => RGBA
3255 if (color_type == PNG_COLOR_TYPE_GRAY
3256 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
3257 png_set_gray_to_rgb(png);
3259 (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
3260 PNG_COLOR_TYPE_RGB_ALPHA);
3262 if (!png_get_gAMA(png, info, &gamma)) {
3266 png_set_gamma(png, display_exponent, gamma);
3268 png_read_update_info(png, info);
3270 png_get_pHYs(png, info, &resx, &resy, &unit);
3272 color_type = png_get_color_type(png, info);
3274 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3276 nr_comp = 3 + has_alpha;
3278 bit_depth = png_get_bit_depth(png, info);
3280 rows = (unsigned char**)calloc(height + 1, sizeof(unsigned char*));
3281 for (i = 0; i < height; ++i) {
3282 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png, info));
3285 png_read_image(png, rows);
3287 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3289 sub_dx = params->subsampling_dx;
3290 sub_dy = params->subsampling_dy;
3292 for (i = 0; i < nr_comp; ++i) {
3293 cmptparm[i].prec = bit_depth;
3294 /* bits_per_pixel: 8 or 16 */
3295 cmptparm[i].bpp = bit_depth;
3296 cmptparm[i].sgnd = 0;
3297 cmptparm[i].dx = sub_dx;
3298 cmptparm[i].dy = sub_dy;
3299 cmptparm[i].w = width;
3300 cmptparm[i].h = height;
3303 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3305 if (image == NULL) {
3309 image->x0 = params->image_offset_x0;
3310 image->y0 = params->image_offset_y0;
3311 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3312 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3314 r = image->comps[0].data;
3315 g = image->comps[1].data;
3316 b = image->comps[2].data;
3317 a = image->comps[3].data;
3319 for (i = 0; i < height; ++i) {
3322 for (j = 0; j < width; ++j) {
3324 *r++ = s[0] << 8 | s[1];
3327 *g++ = s[0] << 8 | s[1];
3330 *b++ = s[0] << 8 | s[1];
3334 *a++ = s[0] << 8 | s[1];
3351 for (i = 0; i < height; ++i) {
3357 png_destroy_read_struct(&png, &info, NULL);
3366 int imagetopng(opj_image_t * image, const char *write_idf)
3371 int *red, *green, *blue, *alpha;
3372 unsigned char *row_buf, *d;
3373 int has_alpha, width, height, nr_comp, color_type;
3374 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3375 int prec, ushift, dshift, is16, force16, force8;
3376 unsigned short mask = 0xffff;
3377 png_color_8 sig_bit;
3379 is16 = force16 = force8 = ushift = dshift = 0;
3381 prec = image->comps[0].prec;
3382 nr_comp = image->numcomps;
3384 if (prec > 8 && prec < 16) {
3386 dshift = prec - ushift;
3389 } else if (prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */
3391 dshift = 8 - ushift;
3396 if (prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) {
3397 fprintf(stderr, "imagetopng: can not create %s"
3398 "\n\twrong bit_depth %d\n", write_idf, prec);
3401 writer = fopen(write_idf, "wb");
3403 if (writer == NULL) {
3410 /* Create and initialize the png_struct with the desired error handler
3411 * functions. If you want to use the default stderr and longjump method,
3412 * you can supply NULL for the last three parameters. We also check that
3413 * the library version is compatible with the one used at compile time,
3414 * in case we are using dynamically linked libraries. REQUIRED.
3416 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3418 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3424 /* Allocate/initialize the image information data. REQUIRED
3426 info = png_create_info_struct(png);
3432 /* Set error handling. REQUIRED if you are not supplying your own
3433 * error handling functions in the png_create_write_struct() call.
3435 if (setjmp(png_jmpbuf(png))) {
3439 /* I/O initialization functions is REQUIRED
3441 png_init_io(png, writer);
3443 /* Set the image information here. Width and height are up to 2^31,
3444 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3445 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3446 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3447 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3448 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3449 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3454 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3455 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3456 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3457 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3460 png_set_compression_level(png, Z_BEST_COMPRESSION);
3464 } else if (prec == 8) {
3466 } else if (prec == 4) {
3468 } else if (prec == 2) {
3470 } else if (prec == 1) {
3475 && image->comps[0].dx == image->comps[1].dx
3476 && image->comps[1].dx == image->comps[2].dx
3477 && image->comps[0].dy == image->comps[1].dy
3478 && image->comps[1].dy == image->comps[2].dy
3479 && image->comps[0].prec == image->comps[1].prec
3480 && image->comps[1].prec == image->comps[2].prec) {
3483 has_alpha = (nr_comp > 3);
3485 is16 = (prec == 16);
3487 width = image->comps[0].w;
3488 height = image->comps[0].h;
3490 red = image->comps[0].data;
3491 green = image->comps[1].data;
3492 blue = image->comps[2].data;
3494 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3497 sig_bit.alpha = prec;
3498 alpha = image->comps[3].data;
3499 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3500 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3504 color_type = PNG_COLOR_TYPE_RGB;
3507 png_set_sBIT(png, info, &sig_bit);
3509 png_set_IHDR(png, info, width, height, prec,
3512 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3513 /*=============================*/
3514 png_write_info(png, info);
3515 /*=============================*/
3517 png_set_packing(png);
3519 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3520 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3521 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3523 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3525 for (y = 0; y < height; ++y) {
3528 for (x = 0; x < width; ++x) {
3534 v = (v << ushift) + (v >> dshift);
3537 *d++ = (unsigned char)(v >> 8);
3538 *d++ = (unsigned char)v;
3540 v = *green + adjustG;
3544 v = (v << ushift) + (v >> dshift);
3547 *d++ = (unsigned char)(v >> 8);
3548 *d++ = (unsigned char)v;
3550 v = *blue + adjustB;
3554 v = (v << ushift) + (v >> dshift);
3557 *d++ = (unsigned char)(v >> 8);
3558 *d++ = (unsigned char)v;
3561 v = *alpha + adjustA;
3565 v = (v << ushift) + (v >> dshift);
3568 *d++ = (unsigned char)(v >> 8);
3569 *d++ = (unsigned char)v;
3578 v = (v << ushift) + (v >> dshift);
3581 *d++ = (unsigned char)(v & mask);
3583 v = *green + adjustG;
3587 v = (v << ushift) + (v >> dshift);
3590 *d++ = (unsigned char)(v & mask);
3592 v = *blue + adjustB;
3596 v = (v << ushift) + (v >> dshift);
3599 *d++ = (unsigned char)(v & mask);
3602 v = *alpha + adjustA;
3606 v = (v << ushift) + (v >> dshift);
3609 *d++ = (unsigned char)(v & mask);
3613 png_write_row(png, row_buf);
3619 else if (nr_comp == 1 /* GRAY */
3620 || (nr_comp == 2 /* GRAY_ALPHA */
3621 && image->comps[0].dx == image->comps[1].dx
3622 && image->comps[0].dy == image->comps[1].dy
3623 && image->comps[0].prec == image->comps[1].prec)) {
3626 red = image->comps[0].data;
3628 sig_bit.gray = prec;
3629 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3632 color_type = PNG_COLOR_TYPE_GRAY;
3636 sig_bit.alpha = prec;
3637 alpha = image->comps[1].data;
3638 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3639 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3641 width = image->comps[0].w;
3642 height = image->comps[0].h;
3644 png_set_IHDR(png, info, width, height, sig_bit.gray,
3647 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3649 png_set_sBIT(png, info, &sig_bit);
3650 /*=============================*/
3651 png_write_info(png, info);
3652 /*=============================*/
3653 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3656 png_set_packing(png);
3660 row_buf = (unsigned char*)
3661 malloc(width * nr_comp * sizeof(unsigned short));
3663 for (y = 0; y < height; ++y) {
3666 for (x = 0; x < width; ++x) {
3671 v = (v << ushift) + (v >> dshift);
3674 *d++ = (unsigned char)(v >> 8);
3675 *d++ = (unsigned char)v;
3681 v = (v << ushift) + (v >> dshift);
3684 *d++ = (unsigned char)(v >> 8);
3685 *d++ = (unsigned char)v;
3688 png_write_row(png, row_buf);
3692 } else { /* prec <= 8 */
3693 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3695 for (y = 0; y < height; ++y) {
3698 for (x = 0; x < width; ++x) {
3703 v = (v << ushift) + (v >> dshift);
3706 *d++ = (unsigned char)(v & mask);
3709 v = *alpha + adjustA;
3713 v = (v << ushift) + (v >> dshift);
3716 *d++ = (unsigned char)(v & mask);
3720 png_write_row(png, row_buf);
3726 fprintf(stderr, "imagetopng: can not create %s\n", write_idf);
3729 png_write_end(png, info);
3736 png_destroy_write_struct(&png, &info);
3746 #endif /* OPJ_HAVE_LIBPNG */