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);
1537 /* -->> -->> -->> -->>
1541 <<-- <<-- <<-- <<-- */
1544 int width, height, maxval, depth, format;
1545 char rgb, rgba, gray, graya, bw;
1549 static char *skip_white(char *s)
1552 if (*s == '\n' || *s == '\r') {
1564 static char *skip_int(char *start, int *out_n)
1572 s = skip_white(start);
1586 *out_n = atoi(start);
1591 static char *skip_idf(char *start, char out_idf[256])
1596 s = skip_white(start);
1603 if (isalpha(*s) || *s == '_') {
1611 strncpy(out_idf, start, 255);
1616 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1619 int format, have_wh, end, ttype;
1620 char idf[256], type[256];
1623 if (fgets(line, 250, reader) == NULL) {
1624 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1628 if (line[0] != 'P') {
1629 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1632 format = atoi(line + 1);
1633 if (format < 1 || format > 7) {
1634 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1637 ph->format = format;
1638 ttype = end = have_wh = 0;
1640 while (fgets(line, 250, reader)) {
1648 s = skip_idf(s, idf);
1650 if (s == NULL || *s == 0) {
1654 if (strcmp(idf, "ENDHDR") == 0) {
1658 if (strcmp(idf, "WIDTH") == 0) {
1659 s = skip_int(s, &ph->width);
1660 if (s == NULL || *s == 0) {
1666 if (strcmp(idf, "HEIGHT") == 0) {
1667 s = skip_int(s, &ph->height);
1668 if (s == NULL || *s == 0) {
1674 if (strcmp(idf, "DEPTH") == 0) {
1675 s = skip_int(s, &ph->depth);
1676 if (s == NULL || *s == 0) {
1682 if (strcmp(idf, "MAXVAL") == 0) {
1683 s = skip_int(s, &ph->maxval);
1684 if (s == NULL || *s == 0) {
1690 if (strcmp(idf, "TUPLTYPE") == 0) {
1691 s = skip_idf(s, type);
1692 if (s == NULL || *s == 0) {
1696 if (strcmp(type, "BLACKANDWHITE") == 0) {
1701 if (strcmp(type, "GRAYSCALE") == 0) {
1706 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1711 if (strcmp(type, "RGB") == 0) {
1716 if (strcmp(type, "RGB_ALPHA") == 0) {
1721 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1724 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1726 } /* if(format == 7) */
1729 s = skip_int(s, &ph->width);
1731 s = skip_int(s, &ph->height);
1735 if (format == 1 || format == 4) {
1741 if (format == 2 || format == 3 || format == 5 || format == 6) {
1742 /* P2, P3, P5, P6: */
1743 s = skip_int(s, &ph->maxval);
1745 if (ph->maxval > 65535) {
1750 }/* while(fgets( ) */
1751 if (format == 2 || format == 3 || format > 4) {
1752 if (ph->maxval < 1 || ph->maxval > 65535) {
1756 if (ph->width < 1 || ph->height < 1) {
1762 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1765 if (ph->depth < 1 || ph->depth > 4) {
1769 if (ph->width && ph->height && ph->depth & ph->maxval && ttype) {
1773 if (format != 1 && format != 4) {
1774 if (ph->width && ph->height && ph->maxval) {
1778 if (ph->width && ph->height) {
1786 static int has_prec(int val)
1836 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1838 int subsampling_dx = parameters->subsampling_dx;
1839 int subsampling_dy = parameters->subsampling_dy;
1842 int i, compno, numcomps, w, h, prec, format;
1843 OPJ_COLOR_SPACE color_space;
1844 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1845 opj_image_t * image = NULL;
1846 struct pnm_header header_info;
1848 if ((fp = fopen(filename, "rb")) == NULL) {
1849 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1852 memset(&header_info, 0, sizeof(struct pnm_header));
1854 read_pnm_header(fp, &header_info);
1856 if (!header_info.ok) {
1861 format = header_info.format;
1864 case 1: /* ascii bitmap */
1865 case 4: /* raw bitmap */
1869 case 2: /* ascii greymap */
1870 case 5: /* raw greymap */
1874 case 3: /* ascii pixmap */
1875 case 6: /* raw pixmap */
1879 case 7: /* arbitrary map */
1880 numcomps = header_info.depth;
1888 color_space = CLRSPC_GRAY; /* GRAY, GRAYA */
1890 color_space = CLRSPC_SRGB; /* RGB, RGBA */
1893 prec = has_prec(header_info.maxval);
1899 w = header_info.width;
1900 h = header_info.height;
1901 subsampling_dx = parameters->subsampling_dx;
1902 subsampling_dy = parameters->subsampling_dy;
1904 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1906 for (i = 0; i < numcomps; i++) {
1907 cmptparm[i].prec = prec;
1908 cmptparm[i].bpp = prec;
1909 cmptparm[i].sgnd = 0;
1910 cmptparm[i].dx = subsampling_dx;
1911 cmptparm[i].dy = subsampling_dy;
1915 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1922 /* set image offset and reference grid */
1923 image->x0 = parameters->image_offset_x0;
1924 image->y0 = parameters->image_offset_y0;
1925 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1926 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1928 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1931 for (i = 0; i < w * h; i++) {
1932 for (compno = 0; compno < numcomps; compno++) {
1934 if (fscanf(fp, "%u", &index) != 1) {
1936 "\nWARNING: fscanf return a number of element different from the expected.\n");
1939 image->comps[compno].data[i] = (index * 255) / header_info.maxval;
1942 } else if ((format == 5)
1945 && (header_info.gray || header_info.graya
1946 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1947 unsigned char c0, c1, one;
1951 for (i = 0; i < w * h; i++) {
1952 for (compno = 0; compno < numcomps; compno++) {
1953 if (!fread(&c0, 1, 1, fp)) {
1955 "\nError: fread return a number of element different from the expected.\n");
1958 image->comps[compno].data[i] = c0;
1960 if (!fread(&c1, 1, 1, fp)) {
1962 "\nError: fread return a number of element different from the expected.\n");
1965 image->comps[compno].data[i] = ((c0 << 8) | c1);
1969 } else if (format == 1) { /* ascii bitmap */
1970 for (i = 0; i < w * h; i++) {
1973 if (fscanf(fp, "%u", &index) != 1) {
1975 "\nWARNING: fscanf return a number of element different from the expected.\n");
1978 image->comps[0].data[i] = (index ? 0 : 255);
1980 } else if (format == 4) {
1985 for (y = 0; y < h; ++y) {
1989 for (x = 0; x < w; ++x) {
1992 uc = (unsigned char)getc(fp);
1994 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
1999 } else if ((format == 7 && header_info.bw)) { /*MONO*/
2002 for (i = 0; i < w * h; ++i) {
2003 if (!fread(&uc, 1, 1, fp)) {
2005 "\nError: fread return a number of element different from the expected.\n");
2007 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
2015 int imagetopnm(opj_image_t * image, const char *outfile)
2017 int *red, *green, *blue, *alpha;
2019 int i, compno, ncomp;
2020 int adjustR, adjustG, adjustB, adjustA;
2021 int fails, two, want_gray, has_alpha, triple;
2024 const char *tmp = outfile;
2027 if ((prec = image->comps[0].prec) > 16) {
2028 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
2029 "\n\t: refused.\n", __FILE__, __LINE__, prec);
2032 two = has_alpha = 0;
2034 ncomp = image->numcomps;
2040 want_gray = (*tmp == 'g' || *tmp == 'G');
2041 ncomp = image->numcomps;
2047 if (ncomp == 2 /* GRAYA */
2048 || (ncomp > 2 /* RGB, RGBA */
2049 && image->comps[0].dx == image->comps[1].dx
2050 && image->comps[1].dx == image->comps[2].dx
2051 && image->comps[0].dy == image->comps[1].dy
2052 && image->comps[1].dy == image->comps[2].dy
2053 && image->comps[0].prec == image->comps[1].prec
2054 && image->comps[1].prec == image->comps[2].prec
2056 fdest = fopen(outfile, "wb");
2059 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
2063 triple = (ncomp > 2);
2064 wr = image->comps[0].w;
2065 hr = image->comps[0].h;
2066 max = (1 << prec) - 1;
2067 has_alpha = (ncomp == 4 || ncomp == 2);
2069 red = image->comps[0].data;
2072 green = image->comps[1].data;
2073 blue = image->comps[2].data;
2075 green = blue = NULL;
2079 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
2081 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
2082 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2083 wr, hr, ncomp, max, tt);
2084 alpha = image->comps[ncomp - 1].data;
2085 adjustA = (image->comps[ncomp - 1].sgnd ?
2086 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2088 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2089 opj_version(), wr, hr, max);
2092 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2095 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2096 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2098 adjustG = adjustB = 0;
2101 for (i = 0; i < wr * hr; ++i) {
2106 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2109 v = *green + adjustG;
2112 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2114 v = *blue + adjustB;
2117 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2122 v = *alpha + adjustA;
2125 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2133 fprintf(fdest, "%c", (unsigned char)*red++);
2135 fprintf(fdest, "%c%c", (unsigned char)*green++, (unsigned char)*blue++);
2139 fprintf(fdest, "%c", (unsigned char)*alpha++);
2150 if (image->numcomps > ncomp) {
2151 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2152 fprintf(stderr, " is written to the file\n");
2154 destname = (char*)malloc(strlen(outfile) + 8);
2156 for (compno = 0; compno < ncomp; compno++) {
2158 sprintf(destname, "%d.%s", compno, outfile);
2160 sprintf(destname, "%s", outfile);
2163 fdest = fopen(destname, "wb");
2165 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2169 wr = image->comps[compno].w;
2170 hr = image->comps[compno].h;
2171 prec = image->comps[compno].prec;
2172 max = (1 << prec) - 1;
2174 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2175 opj_version(), wr, hr, max);
2177 red = image->comps[compno].data;
2179 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2182 for (i = 0; i < wr * hr; i++) {
2186 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2191 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2194 } else { /* prec <= 8 */
2195 for (i = 0; i < wr * hr; ++i) {
2196 fprintf(fdest, "%c", (unsigned char)(*red + adjustR));
2207 #ifdef OPJ_HAVE_LIBTIFF
2208 /* -->> -->> -->> -->>
2212 <<-- <<-- <<-- <<-- */
2214 int imagetotif(opj_image_t * image, const char *outfile)
2216 int width, height, imgsize;
2217 int bps, index, adjust, sgnd;
2218 int ushift, dshift, has_alpha, force16;
2224 ushift = dshift = force16 = has_alpha = 0;
2225 bps = image->comps[0].prec;
2227 if (bps > 8 && bps < 16) {
2229 dshift = bps - ushift;
2234 if (bps != 8 && bps != 16) {
2235 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2237 fprintf(stderr, "\tAborting\n");
2240 tif = TIFFOpen(outfile, "wb");
2243 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2246 sgnd = image->comps[0].sgnd;
2247 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2249 if (image->numcomps >= 3
2250 && image->comps[0].dx == image->comps[1].dx
2251 && image->comps[1].dx == image->comps[2].dx
2252 && image->comps[0].dy == image->comps[1].dy
2253 && image->comps[1].dy == image->comps[2].dy
2254 && image->comps[0].prec == image->comps[1].prec
2255 && image->comps[1].prec == image->comps[2].prec) {
2256 has_alpha = (image->numcomps == 4);
2258 width = image->comps[0].w;
2259 height = image->comps[0].h;
2260 imgsize = width * height ;
2262 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2263 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2264 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2265 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2266 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2267 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2268 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2269 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2270 strip_size = TIFFStripSize(tif);
2271 buf = _TIFFmalloc(strip_size);
2274 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2275 unsigned char *dat8;
2276 tsize_t i, ssize, last_i = 0;
2278 ssize = TIFFStripSize(tif);
2279 dat8 = (unsigned char*)buf;
2282 step = 3 + has_alpha;
2285 for (i = 0; i < ssize - restx; i += step) {
2288 if (index < imgsize) {
2289 r = image->comps[0].data[index];
2290 g = image->comps[1].data[index];
2291 b = image->comps[2].data[index];
2293 a = image->comps[3].data[index];
2318 if (last_i < ssize) {
2319 for (i = last_i; i < ssize; i += step) {
2322 if (index < imgsize) {
2323 r = image->comps[0].data[index];
2324 g = image->comps[1].data[index];
2325 b = image->comps[2].data[index];
2327 a = image->comps[3].data[index];
2339 if (i + 1 < ssize) {
2344 if (i + 2 < ssize) {
2350 if (i + 3 < ssize) {
2361 }/*if(last_i < ssize)*/
2364 else if (bps == 16) {
2365 step = 6 + has_alpha + has_alpha;
2368 for (i = 0; i < ssize - restx ; i += step) {
2371 if (index < imgsize) {
2372 r = image->comps[0].data[index];
2373 g = image->comps[1].data[index];
2374 b = image->comps[2].data[index];
2376 a = image->comps[3].data[index];
2388 r = (r << ushift) + (r >> dshift);
2389 g = (g << ushift) + (g >> dshift);
2390 b = (b << ushift) + (b >> dshift);
2392 a = (a << ushift) + (a >> dshift);
2395 dat8[i + 0] = r; /*LSB*/
2396 dat8[i + 1] = (r >> 8); /*MSB*/
2398 dat8[i + 3] = (g >> 8);
2400 dat8[i + 5] = (b >> 8);
2403 dat8[i + 7] = (a >> 8);
2412 if (last_i < ssize) {
2413 for (i = last_i ; i < ssize ; i += step) {
2416 if (index < imgsize) {
2417 r = image->comps[0].data[index];
2418 g = image->comps[1].data[index];
2419 b = image->comps[2].data[index];
2421 a = image->comps[3].data[index];
2433 r = (r << ushift) + (r >> dshift);
2434 g = (g << ushift) + (g >> dshift);
2435 b = (b << ushift) + (b >> dshift);
2437 a = (a << ushift) + (a >> dshift);
2440 dat8[i + 0] = r; /*LSB*/
2441 if (i + 1 < ssize) {
2442 dat8[i + 1] = (r >> 8);
2446 if (i + 2 < ssize) {
2451 if (i + 3 < ssize) {
2452 dat8[i + 3] = (g >> 8);
2456 if (i + 4 < ssize) {
2461 if (i + 5 < ssize) {
2462 dat8[i + 5] = (b >> 8);
2468 if (i + 6 < ssize) {
2473 if (i + 7 < ssize) {
2474 dat8[i + 7] = (a >> 8);
2484 }/*if(last_i < ssize)*/
2487 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2488 }/*for(strip = 0; )*/
2490 _TIFFfree((void*)buf);
2496 if (image->numcomps == 1 /* GRAY */
2497 || (image->numcomps == 2 /* GRAY_ALPHA */
2498 && image->comps[0].dx == image->comps[1].dx
2499 && image->comps[0].dy == image->comps[1].dy
2500 && image->comps[0].prec == image->comps[1].prec)) {
2503 has_alpha = (image->numcomps == 2);
2505 width = image->comps[0].w;
2506 height = image->comps[0].h;
2507 imgsize = width * height;
2510 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2511 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2512 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2513 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2514 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2515 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2516 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2517 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2519 /* Get a buffer for the data */
2520 strip_size = TIFFStripSize(tif);
2521 buf = _TIFFmalloc(strip_size);
2524 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2525 unsigned char *dat8;
2526 tsize_t i, ssize = TIFFStripSize(tif);
2527 dat8 = (unsigned char*)buf;
2530 step = 1 + has_alpha;
2532 for (i = 0; i < ssize; i += step) {
2533 if (index < imgsize) {
2536 r = image->comps[0].data[index];
2538 a = image->comps[1].data[index];
2557 else if (bps == 16) {
2558 step = 2 + has_alpha + has_alpha;
2560 for (i = 0; i < ssize; i += step) {
2561 if (index < imgsize) {
2564 r = image->comps[0].data[index];
2566 a = image->comps[1].data[index];
2576 r = (r << ushift) + (r >> dshift);
2578 a = (a << ushift) + (a >> dshift);
2581 dat8[i + 0] = r; /*LSB*/
2582 dat8[i + 1] = r >> 8; /*MSB*/
2585 dat8[i + 3] = a >> 8;
2588 }/*if(index < imgsize)*/
2594 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2605 fprintf(stderr, "imagetotif: Bad color format.\n"
2606 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2607 fprintf(stderr, "\tFOUND: numcomps(%d)\n\tAborting\n",
2614 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2615 * CINEMA : 12 bit precision
2617 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2619 int subsampling_dx = parameters->subsampling_dx;
2620 int subsampling_dy = parameters->subsampling_dy;
2625 int j, numcomps, w, h, index;
2626 OPJ_COLOR_SPACE color_space;
2627 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2628 opj_image_t *image = NULL;
2631 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2632 unsigned int tiWidth, tiHeight;
2634 tif = TIFFOpen(filename, "r");
2637 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2640 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2641 tiWidth = tiHeight = 0;
2643 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2644 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2645 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2646 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2647 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2648 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2649 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2654 unsigned short b = tiBps, p = tiPhoto;
2656 if (tiBps != 8 && tiBps != 16 && tiBps != 12) {
2659 if (tiPhoto != 1 && tiPhoto != 2) {
2665 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits"
2666 " implemented\n", tiBps);
2668 fprintf(stderr, "tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2669 " and GRAY(A) has been implemented\n", (int) tiPhoto);
2671 fprintf(stderr, "\tAborting\n");
2677 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2679 uint16 extrasamples;
2681 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2682 &extrasamples, &sampleinfo);
2684 if (extrasamples >= 1) {
2685 switch (sampleinfo[0]) {
2686 case EXTRASAMPLE_UNSPECIFIED:
2687 /* Workaround for some images without correct info about alpha channel
2694 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2695 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2699 } else /* extrasamples == 0 */
2700 if (tiSpp == 4 || tiSpp == 2) {
2705 /* initialize image components
2707 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2709 if (tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */
2710 numcomps = 3 + has_alpha;
2711 color_space = CLRSPC_SRGB;
2713 for (j = 0; j < numcomps; j++) {
2714 if (parameters->cp_cinema) {
2715 cmptparm[j].prec = 12;
2716 cmptparm[j].bpp = 12;
2718 cmptparm[j].prec = tiBps;
2719 cmptparm[j].bpp = tiBps;
2721 cmptparm[j].dx = subsampling_dx;
2722 cmptparm[j].dy = subsampling_dy;
2727 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2733 /* set image offset and reference grid
2735 image->x0 = parameters->image_offset_x0;
2736 image->y0 = parameters->image_offset_y0;
2737 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2738 image->x0 + (w - 1) * subsampling_dx + 1;
2739 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2740 image->y0 + (h - 1) * subsampling_dy + 1;
2742 buf = _TIFFmalloc(TIFFStripSize(tif));
2744 strip_size = TIFFStripSize(tif);
2746 imgsize = image->comps[0].w * image->comps[0].h ;
2747 /* Read the Image components
2749 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2750 unsigned char *dat8;
2753 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2754 dat8 = (unsigned char*)buf;
2757 step = 6 + has_alpha + has_alpha;
2759 for (i = 0; i < ssize; i += step) {
2760 if (index < imgsize) {
2761 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0]; /* R */
2762 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2]; /* G */
2763 image->comps[2].data[index] = (dat8[i + 5] << 8) | dat8[i + 4]; /* B */
2765 image->comps[3].data[index] = (dat8[i + 7] << 8) | dat8[i + 6];
2768 if (parameters->cp_cinema) {
2769 /* Rounding 16 to 12 bits
2771 image->comps[0].data[index] =
2772 (image->comps[0].data[index] + 0x08) >> 4 ;
2773 image->comps[1].data[index] =
2774 (image->comps[1].data[index] + 0x08) >> 4 ;
2775 image->comps[2].data[index] =
2776 (image->comps[2].data[index] + 0x08) >> 4 ;
2778 image->comps[3].data[index] =
2779 (image->comps[3].data[index] + 0x08) >> 4 ;
2786 }/*if(tiBps == 16)*/
2787 else if (tiBps == 8) {
2788 step = 3 + has_alpha;
2790 for (i = 0; i < ssize; i += step) {
2791 if (index < imgsize) {
2792 image->comps[0].data[index] = dat8[i + 0]; /* R */
2793 image->comps[1].data[index] = dat8[i + 1]; /* G */
2794 image->comps[2].data[index] = dat8[i + 2]; /* B */
2796 image->comps[3].data[index] = dat8[i + 3];
2799 if (parameters->cp_cinema) {
2800 /* Rounding 8 to 12 bits
2802 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2803 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2804 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2806 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2815 }/*if( tiBps == 8)*/
2816 else if (tiBps == 12) { /* CINEMA file */
2819 for (i = 0; i < ssize; i += step) {
2820 if ((index < imgsize) & (index + 1 < imgsize)) {
2821 image->comps[0].data[index] = (dat8[i + 0] << 4) | (dat8[i + 1] >> 4);
2822 image->comps[1].data[index] = ((dat8[i + 1] & 0x0f) << 8) | dat8[i + 2];
2824 image->comps[2].data[index] = (dat8[i + 3] << 4) | (dat8[i + 4] >> 4);
2825 image->comps[0].data[index + 1] = ((dat8[i + 4] & 0x0f) << 8) | dat8[i + 5];
2827 image->comps[1].data[index + 1] = (dat8[i + 6] << 4) |
2829 image->comps[2].data[index + 1] = ((dat8[i + 7] & 0x0f) << 8) | dat8[i + 8];
2837 }/*for(strip = 0; )*/
2845 if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */
2846 numcomps = 1 + has_alpha;
2847 color_space = CLRSPC_GRAY;
2849 for (j = 0; j < numcomps; ++j) {
2850 cmptparm[j].prec = tiBps;
2851 cmptparm[j].bpp = tiBps;
2852 cmptparm[j].dx = subsampling_dx;
2853 cmptparm[j].dy = subsampling_dy;
2857 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2863 /* set image offset and reference grid
2865 image->x0 = parameters->image_offset_x0;
2866 image->y0 = parameters->image_offset_y0;
2867 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2868 image->x0 + (w - 1) * subsampling_dx + 1;
2869 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2870 image->y0 + (h - 1) * subsampling_dy + 1;
2872 buf = _TIFFmalloc(TIFFStripSize(tif));
2874 strip_size = TIFFStripSize(tif);
2876 imgsize = image->comps[0].w * image->comps[0].h ;
2877 /* Read the Image components
2879 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2880 unsigned char *dat8;
2884 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2885 dat8 = (unsigned char*)buf;
2888 step = 2 + has_alpha + has_alpha;
2890 for (i = 0; i < ssize; i += step) {
2891 if (index < imgsize) {
2892 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0];
2894 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2];
2901 } else if (tiBps == 8) {
2902 step = 1 + has_alpha;
2904 for (i = 0; i < ssize; i += step) {
2905 if (index < imgsize) {
2906 image->comps[0].data[index] = dat8[i + 0];
2908 image->comps[1].data[index] = dat8[i + 1];
2927 #endif /* OPJ_HAVE_LIBTIFF */
2929 /* -->> -->> -->> -->>
2933 <<-- <<-- <<-- <<-- */
2935 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2936 raw_cparameters_t *raw_cp)
2938 int subsampling_dx = parameters->subsampling_dx;
2939 int subsampling_dy = parameters->subsampling_dy;
2942 int i, compno, numcomps, w, h;
2943 OPJ_COLOR_SPACE color_space;
2944 opj_image_cmptparm_t *cmptparm;
2945 opj_image_t * image = NULL;
2948 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2949 raw_cp->rawBitDepth)) == 0) {
2950 fprintf(stderr, "\nError: invalid raw image parameters\n");
2951 fprintf(stderr, "Please use the Format option -F:\n");
2953 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2954 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2955 fprintf(stderr, "Aborting\n");
2959 f = fopen(filename, "rb");
2961 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2962 fprintf(stderr, "Aborting\n");
2965 numcomps = raw_cp->rawComp;
2966 color_space = CLRSPC_SRGB;
2967 w = raw_cp->rawWidth;
2968 h = raw_cp->rawHeight;
2969 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(
2970 opj_image_cmptparm_t));
2972 /* initialize image components */
2973 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2974 for (i = 0; i < numcomps; i++) {
2975 cmptparm[i].prec = raw_cp->rawBitDepth;
2976 cmptparm[i].bpp = raw_cp->rawBitDepth;
2977 cmptparm[i].sgnd = raw_cp->rawSigned;
2978 cmptparm[i].dx = subsampling_dx;
2979 cmptparm[i].dy = subsampling_dy;
2983 /* create the image */
2984 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2989 /* set image offset and reference grid */
2990 image->x0 = parameters->image_offset_x0;
2991 image->y0 = parameters->image_offset_y0;
2992 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2993 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2995 if (raw_cp->rawBitDepth <= 8) {
2996 unsigned char value = 0;
2997 for (compno = 0; compno < numcomps; compno++) {
2998 for (i = 0; i < w * h; i++) {
2999 if (!fread(&value, 1, 1, f)) {
3000 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3004 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
3007 } else if (raw_cp->rawBitDepth <= 16) {
3008 unsigned short value;
3009 for (compno = 0; compno < numcomps; compno++) {
3010 for (i = 0; i < w * h; i++) {
3012 if (!fread(&temp, 1, 1, f)) {
3013 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3018 if (!fread(&temp, 1, 1, f)) {
3019 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3024 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
3029 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3034 if (fread(&ch, 1, 1, f)) {
3035 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
3042 int imagetoraw(opj_image_t * image, const char *outfile)
3044 FILE *rawFile = NULL;
3051 if ((image->numcomps * image->x1 * image->y1) == 0) {
3052 fprintf(stderr, "\nError: invalid raw image parameters\n");
3056 rawFile = fopen(outfile, "wb");
3058 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3062 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
3064 for (compno = 0; compno < image->numcomps; compno++) {
3065 fprintf(stdout, "Component %d characteristics: %dx%dx%d %s\n", compno,
3066 image->comps[compno].w,
3067 image->comps[compno].h, image->comps[compno].prec,
3068 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
3070 w = image->comps[compno].w;
3071 h = image->comps[compno].h;
3073 if (image->comps[compno].prec <= 8) {
3074 if (image->comps[compno].sgnd == 1) {
3076 int mask = (1 << image->comps[compno].prec) - 1;
3077 ptr = image->comps[compno].data;
3078 for (line = 0; line < h; line++) {
3079 for (row = 0; row < w; row++) {
3080 curr = (signed char)(*ptr & mask);
3081 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
3083 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3089 } else if (image->comps[compno].sgnd == 0) {
3091 int mask = (1 << image->comps[compno].prec) - 1;
3092 ptr = image->comps[compno].data;
3093 for (line = 0; line < h; line++) {
3094 for (row = 0; row < w; row++) {
3095 curr = (unsigned char)(*ptr & mask);
3096 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
3098 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3105 } else if (image->comps[compno].prec <= 16) {
3106 if (image->comps[compno].sgnd == 1) {
3107 signed short int curr;
3108 int mask = (1 << image->comps[compno].prec) - 1;
3109 ptr = image->comps[compno].data;
3110 for (line = 0; line < h; line++) {
3111 for (row = 0; row < w; row++) {
3113 curr = (signed short int)(*ptr & mask);
3114 temp = (unsigned char)(curr >> 8);
3115 res = fwrite(&temp, 1, 1, rawFile);
3117 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3120 temp = (unsigned char) curr;
3121 res = fwrite(&temp, 1, 1, rawFile);
3123 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3129 } else if (image->comps[compno].sgnd == 0) {
3130 unsigned short int curr;
3131 int mask = (1 << image->comps[compno].prec) - 1;
3132 ptr = image->comps[compno].data;
3133 for (line = 0; line < h; line++) {
3134 for (row = 0; row < w; row++) {
3136 curr = (unsigned short int)(*ptr & mask);
3137 temp = (unsigned char)(curr >> 8);
3138 res = fwrite(&temp, 1, 1, rawFile);
3140 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3143 temp = (unsigned char) curr;
3144 res = fwrite(&temp, 1, 1, rawFile);
3146 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3153 } else if (image->comps[compno].prec <= 32) {
3154 fprintf(stderr, "More than 16 bits per component no handled yet\n");
3157 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
3165 #ifdef OPJ_HAVE_LIBPNG
3167 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3168 #define MAGIC_SIZE 8
3169 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3171 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3175 double gamma, display_exponent;
3176 int bit_depth, interlace_type, compression_type, filter_type;
3178 png_uint_32 resx, resy;
3180 png_uint_32 width, height;
3181 int color_type, has_alpha, is16;
3184 unsigned char **rows;
3187 opj_image_cmptparm_t cmptparm[4];
3189 unsigned int nr_comp;
3191 unsigned char sigbuf[8];
3193 if ((reader = fopen(read_idf, "rb")) == NULL) {
3194 fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
3201 if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3202 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
3203 fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
3206 /* libpng-VERSION/example.c:
3207 * PC : screen_gamma = 2.2;
3208 * Mac: screen_gamma = 1.7 or 1.0;
3210 display_exponent = 2.2;
3212 if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3213 NULL, NULL, NULL)) == NULL) {
3216 if ((info = png_create_info_struct(png)) == NULL) {
3220 if (setjmp(png_jmpbuf(png))) {
3224 png_init_io(png, reader);
3225 png_set_sig_bytes(png, MAGIC_SIZE);
3227 png_read_info(png, info);
3229 if (png_get_IHDR(png, info, &width, &height,
3230 &bit_depth, &color_type, &interlace_type,
3231 &compression_type, &filter_type) == 0) {
3235 /* png_set_expand():
3236 * expand paletted images to RGB, expand grayscale images of
3237 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3238 * to alpha channels.
3240 if (color_type == PNG_COLOR_TYPE_PALETTE) {
3241 png_set_expand(png);
3242 } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
3243 png_set_expand(png);
3246 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
3247 png_set_expand(png);
3250 is16 = (bit_depth == 16);
3252 /* GRAY => RGB; GRAY_ALPHA => RGBA
3254 if (color_type == PNG_COLOR_TYPE_GRAY
3255 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
3256 png_set_gray_to_rgb(png);
3258 (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
3259 PNG_COLOR_TYPE_RGB_ALPHA);
3261 if (!png_get_gAMA(png, info, &gamma)) {
3265 png_set_gamma(png, display_exponent, gamma);
3267 png_read_update_info(png, info);
3269 png_get_pHYs(png, info, &resx, &resy, &unit);
3271 color_type = png_get_color_type(png, info);
3273 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3275 nr_comp = 3 + has_alpha;
3277 bit_depth = png_get_bit_depth(png, info);
3279 rows = (unsigned char**)calloc(height + 1, sizeof(unsigned char*));
3280 for (i = 0; i < height; ++i) {
3281 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png, info));
3284 png_read_image(png, rows);
3286 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3288 sub_dx = params->subsampling_dx;
3289 sub_dy = params->subsampling_dy;
3291 for (i = 0; i < nr_comp; ++i) {
3292 cmptparm[i].prec = bit_depth;
3293 /* bits_per_pixel: 8 or 16 */
3294 cmptparm[i].bpp = bit_depth;
3295 cmptparm[i].sgnd = 0;
3296 cmptparm[i].dx = sub_dx;
3297 cmptparm[i].dy = sub_dy;
3298 cmptparm[i].w = width;
3299 cmptparm[i].h = height;
3302 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3304 if (image == NULL) {
3308 image->x0 = params->image_offset_x0;
3309 image->y0 = params->image_offset_y0;
3310 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3311 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3313 r = image->comps[0].data;
3314 g = image->comps[1].data;
3315 b = image->comps[2].data;
3316 a = image->comps[3].data;
3318 for (i = 0; i < height; ++i) {
3321 for (j = 0; j < width; ++j) {
3323 *r++ = s[0] << 8 | s[1];
3326 *g++ = s[0] << 8 | s[1];
3329 *b++ = s[0] << 8 | s[1];
3333 *a++ = s[0] << 8 | s[1];
3350 for (i = 0; i < height; ++i) {
3356 png_destroy_read_struct(&png, &info, NULL);
3365 int imagetopng(opj_image_t * image, const char *write_idf)
3370 int *red, *green, *blue, *alpha;
3371 unsigned char *row_buf, *d;
3372 int has_alpha, width, height, nr_comp, color_type;
3373 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3374 int prec, ushift, dshift, is16, force16, force8;
3375 unsigned short mask = 0xffff;
3376 png_color_8 sig_bit;
3378 is16 = force16 = force8 = ushift = dshift = 0;
3380 prec = image->comps[0].prec;
3381 nr_comp = image->numcomps;
3383 if (prec > 8 && prec < 16) {
3385 dshift = prec - ushift;
3388 } else if (prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */
3390 dshift = 8 - ushift;
3395 if (prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) {
3396 fprintf(stderr, "imagetopng: can not create %s"
3397 "\n\twrong bit_depth %d\n", write_idf, prec);
3400 writer = fopen(write_idf, "wb");
3402 if (writer == NULL) {
3409 /* Create and initialize the png_struct with the desired error handler
3410 * functions. If you want to use the default stderr and longjump method,
3411 * you can supply NULL for the last three parameters. We also check that
3412 * the library version is compatible with the one used at compile time,
3413 * in case we are using dynamically linked libraries. REQUIRED.
3415 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3417 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3423 /* Allocate/initialize the image information data. REQUIRED
3425 info = png_create_info_struct(png);
3431 /* Set error handling. REQUIRED if you are not supplying your own
3432 * error handling functions in the png_create_write_struct() call.
3434 if (setjmp(png_jmpbuf(png))) {
3438 /* I/O initialization functions is REQUIRED
3440 png_init_io(png, writer);
3442 /* Set the image information here. Width and height are up to 2^31,
3443 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3444 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3445 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3446 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3447 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3448 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3453 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3454 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3455 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3456 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3459 png_set_compression_level(png, Z_BEST_COMPRESSION);
3463 } else if (prec == 8) {
3465 } else if (prec == 4) {
3467 } else if (prec == 2) {
3469 } else if (prec == 1) {
3474 && image->comps[0].dx == image->comps[1].dx
3475 && image->comps[1].dx == image->comps[2].dx
3476 && image->comps[0].dy == image->comps[1].dy
3477 && image->comps[1].dy == image->comps[2].dy
3478 && image->comps[0].prec == image->comps[1].prec
3479 && image->comps[1].prec == image->comps[2].prec) {
3482 has_alpha = (nr_comp > 3);
3484 is16 = (prec == 16);
3486 width = image->comps[0].w;
3487 height = image->comps[0].h;
3489 red = image->comps[0].data;
3490 green = image->comps[1].data;
3491 blue = image->comps[2].data;
3493 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3496 sig_bit.alpha = prec;
3497 alpha = image->comps[3].data;
3498 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3499 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3503 color_type = PNG_COLOR_TYPE_RGB;
3506 png_set_sBIT(png, info, &sig_bit);
3508 png_set_IHDR(png, info, width, height, prec,
3511 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3512 /*=============================*/
3513 png_write_info(png, info);
3514 /*=============================*/
3516 png_set_packing(png);
3518 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3519 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3520 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3522 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3524 for (y = 0; y < height; ++y) {
3527 for (x = 0; x < width; ++x) {
3533 v = (v << ushift) + (v >> dshift);
3536 *d++ = (unsigned char)(v >> 8);
3537 *d++ = (unsigned char)v;
3539 v = *green + adjustG;
3543 v = (v << ushift) + (v >> dshift);
3546 *d++ = (unsigned char)(v >> 8);
3547 *d++ = (unsigned char)v;
3549 v = *blue + adjustB;
3553 v = (v << ushift) + (v >> dshift);
3556 *d++ = (unsigned char)(v >> 8);
3557 *d++ = (unsigned char)v;
3560 v = *alpha + adjustA;
3564 v = (v << ushift) + (v >> dshift);
3567 *d++ = (unsigned char)(v >> 8);
3568 *d++ = (unsigned char)v;
3577 v = (v << ushift) + (v >> dshift);
3580 *d++ = (unsigned char)(v & mask);
3582 v = *green + adjustG;
3586 v = (v << ushift) + (v >> dshift);
3589 *d++ = (unsigned char)(v & mask);
3591 v = *blue + adjustB;
3595 v = (v << ushift) + (v >> dshift);
3598 *d++ = (unsigned char)(v & mask);
3601 v = *alpha + adjustA;
3605 v = (v << ushift) + (v >> dshift);
3608 *d++ = (unsigned char)(v & mask);
3612 png_write_row(png, row_buf);
3618 else if (nr_comp == 1 /* GRAY */
3619 || (nr_comp == 2 /* GRAY_ALPHA */
3620 && image->comps[0].dx == image->comps[1].dx
3621 && image->comps[0].dy == image->comps[1].dy
3622 && image->comps[0].prec == image->comps[1].prec)) {
3625 red = image->comps[0].data;
3627 sig_bit.gray = prec;
3628 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3631 color_type = PNG_COLOR_TYPE_GRAY;
3635 sig_bit.alpha = prec;
3636 alpha = image->comps[1].data;
3637 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3638 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3640 width = image->comps[0].w;
3641 height = image->comps[0].h;
3643 png_set_IHDR(png, info, width, height, sig_bit.gray,
3646 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3648 png_set_sBIT(png, info, &sig_bit);
3649 /*=============================*/
3650 png_write_info(png, info);
3651 /*=============================*/
3652 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3655 png_set_packing(png);
3659 row_buf = (unsigned char*)
3660 malloc(width * nr_comp * sizeof(unsigned short));
3662 for (y = 0; y < height; ++y) {
3665 for (x = 0; x < width; ++x) {
3670 v = (v << ushift) + (v >> dshift);
3673 *d++ = (unsigned char)(v >> 8);
3674 *d++ = (unsigned char)v;
3680 v = (v << ushift) + (v >> dshift);
3683 *d++ = (unsigned char)(v >> 8);
3684 *d++ = (unsigned char)v;
3687 png_write_row(png, row_buf);
3691 } else { /* prec <= 8 */
3692 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3694 for (y = 0; y < height; ++y) {
3697 for (x = 0; x < width; ++x) {
3702 v = (v << ushift) + (v >> dshift);
3705 *d++ = (unsigned char)(v & mask);
3708 v = *alpha + adjustA;
3712 v = (v << ushift) + (v >> dshift);
3715 *d++ = (unsigned char)(v & mask);
3719 png_write_row(png, row_buf);
3725 fprintf(stderr, "imagetopng: can not create %s\n", write_idf);
3728 png_write_end(png, info);
3735 png_destroy_write_struct(&png, &info);
3745 #endif /* OPJ_HAVE_LIBPNG */