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");
1361 while (signtmp[i] != '\0') {
1362 if (signtmp[i] == '-') {
1369 if (endian1 == 'M' && endian2 == 'L') {
1371 } else if (endian2 == 'M' && endian1 == 'L') {
1374 fprintf(stderr, "Bad pgx header, please check input file\n");
1379 /* initialize image component */
1381 cmptparm.x0 = parameters->image_offset_x0;
1382 cmptparm.y0 = parameters->image_offset_y0;
1383 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
1384 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1385 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
1386 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1396 dshift = prec - ushift;
1397 if (cmptparm.sgnd) {
1398 adjustS = (1 << (prec - 1));
1405 ushift = dshift = force8 = adjustS = 0;
1408 cmptparm.prec = prec;
1409 cmptparm.bpp = prec;
1410 cmptparm.dx = parameters->subsampling_dx;
1411 cmptparm.dy = parameters->subsampling_dy;
1413 /* create the image */
1414 image = opj_image_create(numcomps, &cmptparm, color_space);
1419 /* set image offset and reference grid */
1420 image->x0 = cmptparm.x0;
1421 image->y0 = cmptparm.x0;
1422 image->x1 = cmptparm.w;
1423 image->y1 = cmptparm.h;
1425 /* set image data */
1427 comp = &image->comps[0];
1429 for (i = 0; i < w * h; i++) {
1432 v = readuchar(f) + adjustS;
1433 v = (v << ushift) + (v >> dshift);
1434 comp->data[i] = (unsigned char)v;
1442 if (comp->prec == 8) {
1446 v = (char) readuchar(f);
1448 } else if (comp->prec <= 16) {
1450 v = readushort(f, bigendian);
1452 v = (short) readushort(f, bigendian);
1456 v = readuint(f, bigendian);
1458 v = (int) readuint(f, bigendian);
1467 comp->bpp = int_floorlog2(max) + 1;
1472 int imagetopgx(opj_image_t * image, const char *outfile)
1478 for (compno = 0; compno < image->numcomps; compno++) {
1479 opj_image_comp_t *comp = &image->comps[compno];
1480 char bname[256]; /* buffer for name */
1481 char *name = bname; /* pointer */
1484 const size_t olen = strlen(outfile);
1485 const size_t dotpos = olen - 4;
1486 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1487 if (outfile[dotpos] != '.') {
1488 /* `pgx` was recognized but there is no dot at expected position */
1489 fprintf(stderr, "ERROR -> Impossible happen.");
1493 name = (char*)malloc(total + 1);
1495 strncpy(name, outfile, dotpos);
1496 /*if (image->numcomps > 1) {*/
1497 sprintf(name + dotpos, "_%d.pgx", compno);
1499 strcpy(name+dotpos, ".pgx");
1501 fdest = fopen(name, "wb");
1503 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1506 /* don't need name anymore */
1511 w = image->comps[compno].w;
1512 h = image->comps[compno].h;
1514 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1515 if (comp->prec <= 8) {
1517 } else if (comp->prec <= 16) {
1522 for (i = 0; i < w * h; i++) {
1523 int v = image->comps[compno].data[i];
1524 for (j = nbytes - 1; j >= 0; j--) {
1525 char byte = (char)(v >> (j * 8));
1526 res = fwrite(&byte, 1, 1, fdest);
1528 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1540 /* -->> -->> -->> -->>
1544 <<-- <<-- <<-- <<-- */
1547 int width, height, maxval, depth, format;
1548 char rgb, rgba, gray, graya, bw;
1552 static char *skip_white(char *s)
1555 if (*s == '\n' || *s == '\r') {
1567 static char *skip_int(char *start, int *out_n)
1575 s = skip_white(start);
1589 *out_n = atoi(start);
1594 static char *skip_idf(char *start, char out_idf[256])
1599 s = skip_white(start);
1606 if (isalpha(*s) || *s == '_') {
1614 strncpy(out_idf, start, 255);
1619 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1622 int format, have_wh, end, ttype;
1623 char idf[256], type[256];
1626 if (fgets(line, 250, reader) == NULL) {
1627 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1631 if (line[0] != 'P') {
1632 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1635 format = atoi(line + 1);
1636 if (format < 1 || format > 7) {
1637 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1640 ph->format = format;
1641 ttype = end = have_wh = 0;
1643 while (fgets(line, 250, reader)) {
1651 s = skip_idf(s, idf);
1653 if (s == NULL || *s == 0) {
1657 if (strcmp(idf, "ENDHDR") == 0) {
1661 if (strcmp(idf, "WIDTH") == 0) {
1662 s = skip_int(s, &ph->width);
1663 if (s == NULL || *s == 0) {
1669 if (strcmp(idf, "HEIGHT") == 0) {
1670 s = skip_int(s, &ph->height);
1671 if (s == NULL || *s == 0) {
1677 if (strcmp(idf, "DEPTH") == 0) {
1678 s = skip_int(s, &ph->depth);
1679 if (s == NULL || *s == 0) {
1685 if (strcmp(idf, "MAXVAL") == 0) {
1686 s = skip_int(s, &ph->maxval);
1687 if (s == NULL || *s == 0) {
1693 if (strcmp(idf, "TUPLTYPE") == 0) {
1694 s = skip_idf(s, type);
1695 if (s == NULL || *s == 0) {
1699 if (strcmp(type, "BLACKANDWHITE") == 0) {
1704 if (strcmp(type, "GRAYSCALE") == 0) {
1709 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1714 if (strcmp(type, "RGB") == 0) {
1719 if (strcmp(type, "RGB_ALPHA") == 0) {
1724 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1727 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1729 } /* if(format == 7) */
1732 s = skip_int(s, &ph->width);
1734 s = skip_int(s, &ph->height);
1738 if (format == 1 || format == 4) {
1744 if (format == 2 || format == 3 || format == 5 || format == 6) {
1745 /* P2, P3, P5, P6: */
1746 s = skip_int(s, &ph->maxval);
1748 if (ph->maxval > 65535) {
1753 }/* while(fgets( ) */
1754 if (format == 2 || format == 3 || format > 4) {
1755 if (ph->maxval < 1 || ph->maxval > 65535) {
1759 if (ph->width < 1 || ph->height < 1) {
1765 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1768 if (ph->depth < 1 || ph->depth > 4) {
1772 if (ph->width && ph->height && ph->depth & ph->maxval && ttype) {
1776 if (format != 1 && format != 4) {
1777 if (ph->width && ph->height && ph->maxval) {
1781 if (ph->width && ph->height) {
1789 static int has_prec(int val)
1839 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1841 int subsampling_dx = parameters->subsampling_dx;
1842 int subsampling_dy = parameters->subsampling_dy;
1845 int i, compno, numcomps, w, h, prec, format;
1846 OPJ_COLOR_SPACE color_space;
1847 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1848 opj_image_t * image = NULL;
1849 struct pnm_header header_info;
1851 if ((fp = fopen(filename, "rb")) == NULL) {
1852 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1855 memset(&header_info, 0, sizeof(struct pnm_header));
1857 read_pnm_header(fp, &header_info);
1859 if (!header_info.ok) {
1864 format = header_info.format;
1867 case 1: /* ascii bitmap */
1868 case 4: /* raw bitmap */
1872 case 2: /* ascii greymap */
1873 case 5: /* raw greymap */
1877 case 3: /* ascii pixmap */
1878 case 6: /* raw pixmap */
1882 case 7: /* arbitrary map */
1883 numcomps = header_info.depth;
1891 color_space = CLRSPC_GRAY; /* GRAY, GRAYA */
1893 color_space = CLRSPC_SRGB; /* RGB, RGBA */
1896 prec = has_prec(header_info.maxval);
1902 w = header_info.width;
1903 h = header_info.height;
1904 subsampling_dx = parameters->subsampling_dx;
1905 subsampling_dy = parameters->subsampling_dy;
1907 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1909 for (i = 0; i < numcomps; i++) {
1910 cmptparm[i].prec = prec;
1911 cmptparm[i].bpp = prec;
1912 cmptparm[i].sgnd = 0;
1913 cmptparm[i].dx = subsampling_dx;
1914 cmptparm[i].dy = subsampling_dy;
1918 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1925 /* set image offset and reference grid */
1926 image->x0 = parameters->image_offset_x0;
1927 image->y0 = parameters->image_offset_y0;
1928 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1929 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1931 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1934 for (i = 0; i < w * h; i++) {
1935 for (compno = 0; compno < numcomps; compno++) {
1937 if (fscanf(fp, "%u", &index) != 1) {
1939 "\nWARNING: fscanf return a number of element different from the expected.\n");
1942 image->comps[compno].data[i] = (index * 255) / header_info.maxval;
1945 } else if ((format == 5)
1948 && (header_info.gray || header_info.graya
1949 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1950 unsigned char c0, c1, one;
1954 for (i = 0; i < w * h; i++) {
1955 for (compno = 0; compno < numcomps; compno++) {
1956 if (!fread(&c0, 1, 1, fp)) {
1958 "\nError: fread return a number of element different from the expected.\n");
1961 image->comps[compno].data[i] = c0;
1963 if (!fread(&c1, 1, 1, fp)) {
1965 "\nError: fread return a number of element different from the expected.\n");
1968 image->comps[compno].data[i] = ((c0 << 8) | c1);
1972 } else if (format == 1) { /* ascii bitmap */
1973 for (i = 0; i < w * h; i++) {
1976 if (fscanf(fp, "%u", &index) != 1) {
1978 "\nWARNING: fscanf return a number of element different from the expected.\n");
1981 image->comps[0].data[i] = (index ? 0 : 255);
1983 } else if (format == 4) {
1988 for (y = 0; y < h; ++y) {
1992 for (x = 0; x < w; ++x) {
1995 uc = (unsigned char)getc(fp);
1997 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
2002 } else if ((format == 7 && header_info.bw)) { /*MONO*/
2005 for (i = 0; i < w * h; ++i) {
2006 if (!fread(&uc, 1, 1, fp)) {
2008 "\nError: fread return a number of element different from the expected.\n");
2010 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
2018 int imagetopnm(opj_image_t * image, const char *outfile)
2020 int *red, *green, *blue, *alpha;
2022 int i, compno, ncomp;
2023 int adjustR, adjustG, adjustB, adjustA;
2024 int fails, two, want_gray, has_alpha, triple;
2027 const char *tmp = outfile;
2030 if ((prec = image->comps[0].prec) > 16) {
2031 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
2032 "\n\t: refused.\n", __FILE__, __LINE__, prec);
2035 two = has_alpha = 0;
2037 ncomp = image->numcomps;
2043 want_gray = (*tmp == 'g' || *tmp == 'G');
2044 ncomp = image->numcomps;
2050 if (ncomp == 2 /* GRAYA */
2051 || (ncomp > 2 /* RGB, RGBA */
2052 && image->comps[0].dx == image->comps[1].dx
2053 && image->comps[1].dx == image->comps[2].dx
2054 && image->comps[0].dy == image->comps[1].dy
2055 && image->comps[1].dy == image->comps[2].dy
2056 && image->comps[0].prec == image->comps[1].prec
2057 && image->comps[1].prec == image->comps[2].prec
2059 fdest = fopen(outfile, "wb");
2062 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
2066 triple = (ncomp > 2);
2067 wr = image->comps[0].w;
2068 hr = image->comps[0].h;
2069 max = (1 << prec) - 1;
2070 has_alpha = (ncomp == 4 || ncomp == 2);
2072 red = image->comps[0].data;
2075 green = image->comps[1].data;
2076 blue = image->comps[2].data;
2078 green = blue = NULL;
2082 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
2084 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
2085 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2086 wr, hr, ncomp, max, tt);
2087 alpha = image->comps[ncomp - 1].data;
2088 adjustA = (image->comps[ncomp - 1].sgnd ?
2089 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2091 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2092 opj_version(), wr, hr, max);
2095 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2098 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2099 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2101 adjustG = adjustB = 0;
2104 for (i = 0; i < wr * hr; ++i) {
2109 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2112 v = *green + adjustG;
2115 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2117 v = *blue + adjustB;
2120 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2125 v = *alpha + adjustA;
2128 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2136 fprintf(fdest, "%c", (unsigned char)*red++);
2138 fprintf(fdest, "%c%c", (unsigned char)*green++, (unsigned char)*blue++);
2142 fprintf(fdest, "%c", (unsigned char)*alpha++);
2153 if (image->numcomps > ncomp) {
2154 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2155 fprintf(stderr, " is written to the file\n");
2157 destname = (char*)malloc(strlen(outfile) + 8);
2159 for (compno = 0; compno < ncomp; compno++) {
2161 sprintf(destname, "%d.%s", compno, outfile);
2163 sprintf(destname, "%s", outfile);
2166 fdest = fopen(destname, "wb");
2168 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2172 wr = image->comps[compno].w;
2173 hr = image->comps[compno].h;
2174 prec = image->comps[compno].prec;
2175 max = (1 << prec) - 1;
2177 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2178 opj_version(), wr, hr, max);
2180 red = image->comps[compno].data;
2182 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2185 for (i = 0; i < wr * hr; i++) {
2189 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2194 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2197 } else { /* prec <= 8 */
2198 for (i = 0; i < wr * hr; ++i) {
2199 fprintf(fdest, "%c", (unsigned char)(*red + adjustR));
2210 #ifdef OPJ_HAVE_LIBTIFF
2211 /* -->> -->> -->> -->>
2215 <<-- <<-- <<-- <<-- */
2217 int imagetotif(opj_image_t * image, const char *outfile)
2219 int width, height, imgsize;
2220 int bps, index, adjust, sgnd;
2221 int ushift, dshift, has_alpha, force16;
2227 ushift = dshift = force16 = has_alpha = 0;
2228 bps = image->comps[0].prec;
2230 if (bps > 8 && bps < 16) {
2232 dshift = bps - ushift;
2237 if (bps != 8 && bps != 16) {
2238 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2240 fprintf(stderr, "\tAborting\n");
2243 tif = TIFFOpen(outfile, "wb");
2246 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2249 sgnd = image->comps[0].sgnd;
2250 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2252 if (image->numcomps >= 3
2253 && image->comps[0].dx == image->comps[1].dx
2254 && image->comps[1].dx == image->comps[2].dx
2255 && image->comps[0].dy == image->comps[1].dy
2256 && image->comps[1].dy == image->comps[2].dy
2257 && image->comps[0].prec == image->comps[1].prec
2258 && image->comps[1].prec == image->comps[2].prec) {
2259 has_alpha = (image->numcomps == 4);
2261 width = image->comps[0].w;
2262 height = image->comps[0].h;
2263 imgsize = width * height ;
2265 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2266 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2267 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2268 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2269 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2270 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2271 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2272 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2273 strip_size = TIFFStripSize(tif);
2274 buf = _TIFFmalloc(strip_size);
2277 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2278 unsigned char *dat8;
2279 tsize_t i, ssize, last_i = 0;
2281 ssize = TIFFStripSize(tif);
2282 dat8 = (unsigned char*)buf;
2285 step = 3 + has_alpha;
2288 for (i = 0; i < ssize - restx; i += step) {
2291 if (index < imgsize) {
2292 r = image->comps[0].data[index];
2293 g = image->comps[1].data[index];
2294 b = image->comps[2].data[index];
2296 a = image->comps[3].data[index];
2321 if (last_i < ssize) {
2322 for (i = last_i; i < ssize; i += step) {
2325 if (index < imgsize) {
2326 r = image->comps[0].data[index];
2327 g = image->comps[1].data[index];
2328 b = image->comps[2].data[index];
2330 a = image->comps[3].data[index];
2342 if (i + 1 < ssize) {
2347 if (i + 2 < ssize) {
2353 if (i + 3 < ssize) {
2364 }/*if(last_i < ssize)*/
2367 else if (bps == 16) {
2368 step = 6 + has_alpha + has_alpha;
2371 for (i = 0; i < ssize - restx ; i += step) {
2374 if (index < imgsize) {
2375 r = image->comps[0].data[index];
2376 g = image->comps[1].data[index];
2377 b = image->comps[2].data[index];
2379 a = image->comps[3].data[index];
2391 r = (r << ushift) + (r >> dshift);
2392 g = (g << ushift) + (g >> dshift);
2393 b = (b << ushift) + (b >> dshift);
2395 a = (a << ushift) + (a >> dshift);
2398 dat8[i + 0] = r; /*LSB*/
2399 dat8[i + 1] = (r >> 8); /*MSB*/
2401 dat8[i + 3] = (g >> 8);
2403 dat8[i + 5] = (b >> 8);
2406 dat8[i + 7] = (a >> 8);
2415 if (last_i < ssize) {
2416 for (i = last_i ; i < ssize ; i += step) {
2419 if (index < imgsize) {
2420 r = image->comps[0].data[index];
2421 g = image->comps[1].data[index];
2422 b = image->comps[2].data[index];
2424 a = image->comps[3].data[index];
2436 r = (r << ushift) + (r >> dshift);
2437 g = (g << ushift) + (g >> dshift);
2438 b = (b << ushift) + (b >> dshift);
2440 a = (a << ushift) + (a >> dshift);
2443 dat8[i + 0] = r; /*LSB*/
2444 if (i + 1 < ssize) {
2445 dat8[i + 1] = (r >> 8);
2449 if (i + 2 < ssize) {
2454 if (i + 3 < ssize) {
2455 dat8[i + 3] = (g >> 8);
2459 if (i + 4 < ssize) {
2464 if (i + 5 < ssize) {
2465 dat8[i + 5] = (b >> 8);
2471 if (i + 6 < ssize) {
2476 if (i + 7 < ssize) {
2477 dat8[i + 7] = (a >> 8);
2487 }/*if(last_i < ssize)*/
2490 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2491 }/*for(strip = 0; )*/
2493 _TIFFfree((void*)buf);
2499 if (image->numcomps == 1 /* GRAY */
2500 || (image->numcomps == 2 /* GRAY_ALPHA */
2501 && image->comps[0].dx == image->comps[1].dx
2502 && image->comps[0].dy == image->comps[1].dy
2503 && image->comps[0].prec == image->comps[1].prec)) {
2506 has_alpha = (image->numcomps == 2);
2508 width = image->comps[0].w;
2509 height = image->comps[0].h;
2510 imgsize = width * height;
2513 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2514 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2515 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2516 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2517 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2518 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2519 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2520 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2522 /* Get a buffer for the data */
2523 strip_size = TIFFStripSize(tif);
2524 buf = _TIFFmalloc(strip_size);
2527 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2528 unsigned char *dat8;
2529 tsize_t i, ssize = TIFFStripSize(tif);
2530 dat8 = (unsigned char*)buf;
2533 step = 1 + has_alpha;
2535 for (i = 0; i < ssize; i += step) {
2536 if (index < imgsize) {
2539 r = image->comps[0].data[index];
2541 a = image->comps[1].data[index];
2560 else if (bps == 16) {
2561 step = 2 + has_alpha + has_alpha;
2563 for (i = 0; i < ssize; i += step) {
2564 if (index < imgsize) {
2567 r = image->comps[0].data[index];
2569 a = image->comps[1].data[index];
2579 r = (r << ushift) + (r >> dshift);
2581 a = (a << ushift) + (a >> dshift);
2584 dat8[i + 0] = r; /*LSB*/
2585 dat8[i + 1] = r >> 8; /*MSB*/
2588 dat8[i + 3] = a >> 8;
2591 }/*if(index < imgsize)*/
2597 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2608 fprintf(stderr, "imagetotif: Bad color format.\n"
2609 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2610 fprintf(stderr, "\tFOUND: numcomps(%d)\n\tAborting\n",
2617 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2618 * CINEMA : 12 bit precision
2620 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2622 int subsampling_dx = parameters->subsampling_dx;
2623 int subsampling_dy = parameters->subsampling_dy;
2628 int j, numcomps, w, h, index;
2629 OPJ_COLOR_SPACE color_space;
2630 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2631 opj_image_t *image = NULL;
2634 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2635 unsigned int tiWidth, tiHeight;
2637 tif = TIFFOpen(filename, "r");
2640 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2643 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2644 tiWidth = tiHeight = 0;
2646 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2647 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2648 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2649 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2650 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2651 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2652 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2657 unsigned short b = tiBps, p = tiPhoto;
2659 if (tiBps != 8 && tiBps != 16 && tiBps != 12) {
2662 if (tiPhoto != 1 && tiPhoto != 2) {
2668 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits"
2669 " implemented\n", tiBps);
2671 fprintf(stderr, "tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2672 " and GRAY(A) has been implemented\n", (int) tiPhoto);
2674 fprintf(stderr, "\tAborting\n");
2680 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2682 uint16 extrasamples;
2684 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2685 &extrasamples, &sampleinfo);
2687 if (extrasamples >= 1) {
2688 switch (sampleinfo[0]) {
2689 case EXTRASAMPLE_UNSPECIFIED:
2690 /* Workaround for some images without correct info about alpha channel
2697 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2698 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2702 } else /* extrasamples == 0 */
2703 if (tiSpp == 4 || tiSpp == 2) {
2708 /* initialize image components
2710 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2712 if (tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */
2713 numcomps = 3 + has_alpha;
2714 color_space = CLRSPC_SRGB;
2716 for (j = 0; j < numcomps; j++) {
2717 if (parameters->cp_cinema) {
2718 cmptparm[j].prec = 12;
2719 cmptparm[j].bpp = 12;
2721 cmptparm[j].prec = tiBps;
2722 cmptparm[j].bpp = tiBps;
2724 cmptparm[j].dx = subsampling_dx;
2725 cmptparm[j].dy = subsampling_dy;
2730 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2736 /* set image offset and reference grid
2738 image->x0 = parameters->image_offset_x0;
2739 image->y0 = parameters->image_offset_y0;
2740 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2741 image->x0 + (w - 1) * subsampling_dx + 1;
2742 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2743 image->y0 + (h - 1) * subsampling_dy + 1;
2745 buf = _TIFFmalloc(TIFFStripSize(tif));
2747 strip_size = TIFFStripSize(tif);
2749 imgsize = image->comps[0].w * image->comps[0].h ;
2750 /* Read the Image components
2752 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2753 unsigned char *dat8;
2756 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2757 dat8 = (unsigned char*)buf;
2760 step = 6 + has_alpha + has_alpha;
2762 for (i = 0; i < ssize; i += step) {
2763 if (index < imgsize) {
2764 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0]; /* R */
2765 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2]; /* G */
2766 image->comps[2].data[index] = (dat8[i + 5] << 8) | dat8[i + 4]; /* B */
2768 image->comps[3].data[index] = (dat8[i + 7] << 8) | dat8[i + 6];
2771 if (parameters->cp_cinema) {
2772 /* Rounding 16 to 12 bits
2774 image->comps[0].data[index] =
2775 (image->comps[0].data[index] + 0x08) >> 4 ;
2776 image->comps[1].data[index] =
2777 (image->comps[1].data[index] + 0x08) >> 4 ;
2778 image->comps[2].data[index] =
2779 (image->comps[2].data[index] + 0x08) >> 4 ;
2781 image->comps[3].data[index] =
2782 (image->comps[3].data[index] + 0x08) >> 4 ;
2789 }/*if(tiBps == 16)*/
2790 else if (tiBps == 8) {
2791 step = 3 + has_alpha;
2793 for (i = 0; i < ssize; i += step) {
2794 if (index < imgsize) {
2795 image->comps[0].data[index] = dat8[i + 0]; /* R */
2796 image->comps[1].data[index] = dat8[i + 1]; /* G */
2797 image->comps[2].data[index] = dat8[i + 2]; /* B */
2799 image->comps[3].data[index] = dat8[i + 3];
2802 if (parameters->cp_cinema) {
2803 /* Rounding 8 to 12 bits
2805 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2806 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2807 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2809 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2818 }/*if( tiBps == 8)*/
2819 else if (tiBps == 12) { /* CINEMA file */
2822 for (i = 0; i < ssize; i += step) {
2823 if ((index < imgsize) & (index + 1 < imgsize)) {
2824 image->comps[0].data[index] = (dat8[i + 0] << 4) | (dat8[i + 1] >> 4);
2825 image->comps[1].data[index] = ((dat8[i + 1] & 0x0f) << 8) | dat8[i + 2];
2827 image->comps[2].data[index] = (dat8[i + 3] << 4) | (dat8[i + 4] >> 4);
2828 image->comps[0].data[index + 1] = ((dat8[i + 4] & 0x0f) << 8) | dat8[i + 5];
2830 image->comps[1].data[index + 1] = (dat8[i + 6] << 4) |
2832 image->comps[2].data[index + 1] = ((dat8[i + 7] & 0x0f) << 8) | dat8[i + 8];
2840 }/*for(strip = 0; )*/
2848 if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */
2849 numcomps = 1 + has_alpha;
2850 color_space = CLRSPC_GRAY;
2852 for (j = 0; j < numcomps; ++j) {
2853 cmptparm[j].prec = tiBps;
2854 cmptparm[j].bpp = tiBps;
2855 cmptparm[j].dx = subsampling_dx;
2856 cmptparm[j].dy = subsampling_dy;
2860 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2866 /* set image offset and reference grid
2868 image->x0 = parameters->image_offset_x0;
2869 image->y0 = parameters->image_offset_y0;
2870 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2871 image->x0 + (w - 1) * subsampling_dx + 1;
2872 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2873 image->y0 + (h - 1) * subsampling_dy + 1;
2875 buf = _TIFFmalloc(TIFFStripSize(tif));
2877 strip_size = TIFFStripSize(tif);
2879 imgsize = image->comps[0].w * image->comps[0].h ;
2880 /* Read the Image components
2882 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2883 unsigned char *dat8;
2887 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2888 dat8 = (unsigned char*)buf;
2891 step = 2 + has_alpha + has_alpha;
2893 for (i = 0; i < ssize; i += step) {
2894 if (index < imgsize) {
2895 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0];
2897 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2];
2904 } else if (tiBps == 8) {
2905 step = 1 + has_alpha;
2907 for (i = 0; i < ssize; i += step) {
2908 if (index < imgsize) {
2909 image->comps[0].data[index] = dat8[i + 0];
2911 image->comps[1].data[index] = dat8[i + 1];
2930 #endif /* OPJ_HAVE_LIBTIFF */
2932 /* -->> -->> -->> -->>
2936 <<-- <<-- <<-- <<-- */
2938 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2939 raw_cparameters_t *raw_cp)
2941 int subsampling_dx = parameters->subsampling_dx;
2942 int subsampling_dy = parameters->subsampling_dy;
2945 int i, compno, numcomps, w, h;
2946 OPJ_COLOR_SPACE color_space;
2947 opj_image_cmptparm_t *cmptparm;
2948 opj_image_t * image = NULL;
2951 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2952 raw_cp->rawBitDepth)) == 0) {
2953 fprintf(stderr, "\nError: invalid raw image parameters\n");
2954 fprintf(stderr, "Please use the Format option -F:\n");
2956 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2957 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2958 fprintf(stderr, "Aborting\n");
2962 f = fopen(filename, "rb");
2964 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2965 fprintf(stderr, "Aborting\n");
2968 numcomps = raw_cp->rawComp;
2969 color_space = CLRSPC_SRGB;
2970 w = raw_cp->rawWidth;
2971 h = raw_cp->rawHeight;
2972 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(
2973 opj_image_cmptparm_t));
2975 /* initialize image components */
2976 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2977 for (i = 0; i < numcomps; i++) {
2978 cmptparm[i].prec = raw_cp->rawBitDepth;
2979 cmptparm[i].bpp = raw_cp->rawBitDepth;
2980 cmptparm[i].sgnd = raw_cp->rawSigned;
2981 cmptparm[i].dx = subsampling_dx;
2982 cmptparm[i].dy = subsampling_dy;
2986 /* create the image */
2987 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2992 /* set image offset and reference grid */
2993 image->x0 = parameters->image_offset_x0;
2994 image->y0 = parameters->image_offset_y0;
2995 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2996 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2998 if (raw_cp->rawBitDepth <= 8) {
2999 unsigned char value = 0;
3000 for (compno = 0; compno < numcomps; compno++) {
3001 for (i = 0; i < w * h; i++) {
3002 if (!fread(&value, 1, 1, f)) {
3003 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3007 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
3010 } else if (raw_cp->rawBitDepth <= 16) {
3011 unsigned short value;
3012 for (compno = 0; compno < numcomps; compno++) {
3013 for (i = 0; i < w * h; i++) {
3015 if (!fread(&temp, 1, 1, f)) {
3016 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3021 if (!fread(&temp, 1, 1, f)) {
3022 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3027 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
3032 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3037 if (fread(&ch, 1, 1, f)) {
3038 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
3045 int imagetoraw(opj_image_t * image, const char *outfile)
3047 FILE *rawFile = NULL;
3054 if ((image->numcomps * image->x1 * image->y1) == 0) {
3055 fprintf(stderr, "\nError: invalid raw image parameters\n");
3059 rawFile = fopen(outfile, "wb");
3061 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3065 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
3067 for (compno = 0; compno < image->numcomps; compno++) {
3068 fprintf(stdout, "Component %d characteristics: %dx%dx%d %s\n", compno,
3069 image->comps[compno].w,
3070 image->comps[compno].h, image->comps[compno].prec,
3071 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
3073 w = image->comps[compno].w;
3074 h = image->comps[compno].h;
3076 if (image->comps[compno].prec <= 8) {
3077 if (image->comps[compno].sgnd == 1) {
3079 int mask = (1 << image->comps[compno].prec) - 1;
3080 ptr = image->comps[compno].data;
3081 for (line = 0; line < h; line++) {
3082 for (row = 0; row < w; row++) {
3083 curr = (signed char)(*ptr & mask);
3084 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
3086 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3092 } else if (image->comps[compno].sgnd == 0) {
3094 int mask = (1 << image->comps[compno].prec) - 1;
3095 ptr = image->comps[compno].data;
3096 for (line = 0; line < h; line++) {
3097 for (row = 0; row < w; row++) {
3098 curr = (unsigned char)(*ptr & mask);
3099 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
3101 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3108 } else if (image->comps[compno].prec <= 16) {
3109 if (image->comps[compno].sgnd == 1) {
3110 signed short int curr;
3111 int mask = (1 << image->comps[compno].prec) - 1;
3112 ptr = image->comps[compno].data;
3113 for (line = 0; line < h; line++) {
3114 for (row = 0; row < w; row++) {
3116 curr = (signed short int)(*ptr & mask);
3117 temp = (unsigned char)(curr >> 8);
3118 res = fwrite(&temp, 1, 1, rawFile);
3120 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3123 temp = (unsigned char) curr;
3124 res = fwrite(&temp, 1, 1, rawFile);
3126 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3132 } else if (image->comps[compno].sgnd == 0) {
3133 unsigned short int curr;
3134 int mask = (1 << image->comps[compno].prec) - 1;
3135 ptr = image->comps[compno].data;
3136 for (line = 0; line < h; line++) {
3137 for (row = 0; row < w; row++) {
3139 curr = (unsigned short int)(*ptr & mask);
3140 temp = (unsigned char)(curr >> 8);
3141 res = fwrite(&temp, 1, 1, rawFile);
3143 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3146 temp = (unsigned char) curr;
3147 res = fwrite(&temp, 1, 1, rawFile);
3149 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3156 } else if (image->comps[compno].prec <= 32) {
3157 fprintf(stderr, "More than 16 bits per component no handled yet\n");
3160 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
3168 #ifdef OPJ_HAVE_LIBPNG
3170 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3171 #define MAGIC_SIZE 8
3172 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3174 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3178 double gamma, display_exponent;
3179 int bit_depth, interlace_type, compression_type, filter_type;
3181 png_uint_32 resx, resy;
3183 png_uint_32 width, height;
3184 int color_type, has_alpha, is16;
3187 unsigned char **rows;
3190 opj_image_cmptparm_t cmptparm[4];
3192 unsigned int nr_comp;
3194 unsigned char sigbuf[8];
3196 if ((reader = fopen(read_idf, "rb")) == NULL) {
3197 fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
3204 if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3205 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
3206 fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
3209 /* libpng-VERSION/example.c:
3210 * PC : screen_gamma = 2.2;
3211 * Mac: screen_gamma = 1.7 or 1.0;
3213 display_exponent = 2.2;
3215 if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3216 NULL, NULL, NULL)) == NULL) {
3219 if ((info = png_create_info_struct(png)) == NULL) {
3223 if (setjmp(png_jmpbuf(png))) {
3227 png_init_io(png, reader);
3228 png_set_sig_bytes(png, MAGIC_SIZE);
3230 png_read_info(png, info);
3232 if (png_get_IHDR(png, info, &width, &height,
3233 &bit_depth, &color_type, &interlace_type,
3234 &compression_type, &filter_type) == 0) {
3238 /* png_set_expand():
3239 * expand paletted images to RGB, expand grayscale images of
3240 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3241 * to alpha channels.
3243 if (color_type == PNG_COLOR_TYPE_PALETTE) {
3244 png_set_expand(png);
3245 } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
3246 png_set_expand(png);
3249 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
3250 png_set_expand(png);
3253 is16 = (bit_depth == 16);
3255 /* GRAY => RGB; GRAY_ALPHA => RGBA
3257 if (color_type == PNG_COLOR_TYPE_GRAY
3258 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
3259 png_set_gray_to_rgb(png);
3261 (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
3262 PNG_COLOR_TYPE_RGB_ALPHA);
3264 if (!png_get_gAMA(png, info, &gamma)) {
3268 png_set_gamma(png, display_exponent, gamma);
3270 png_read_update_info(png, info);
3272 png_get_pHYs(png, info, &resx, &resy, &unit);
3274 color_type = png_get_color_type(png, info);
3276 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3278 nr_comp = 3 + has_alpha;
3280 bit_depth = png_get_bit_depth(png, info);
3282 rows = (unsigned char**)calloc(height + 1, sizeof(unsigned char*));
3283 for (i = 0; i < height; ++i) {
3284 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png, info));
3287 png_read_image(png, rows);
3289 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3291 sub_dx = params->subsampling_dx;
3292 sub_dy = params->subsampling_dy;
3294 for (i = 0; i < nr_comp; ++i) {
3295 cmptparm[i].prec = bit_depth;
3296 /* bits_per_pixel: 8 or 16 */
3297 cmptparm[i].bpp = bit_depth;
3298 cmptparm[i].sgnd = 0;
3299 cmptparm[i].dx = sub_dx;
3300 cmptparm[i].dy = sub_dy;
3301 cmptparm[i].w = width;
3302 cmptparm[i].h = height;
3305 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3307 if (image == NULL) {
3311 image->x0 = params->image_offset_x0;
3312 image->y0 = params->image_offset_y0;
3313 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3314 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3316 r = image->comps[0].data;
3317 g = image->comps[1].data;
3318 b = image->comps[2].data;
3319 a = image->comps[3].data;
3321 for (i = 0; i < height; ++i) {
3324 for (j = 0; j < width; ++j) {
3326 *r++ = s[0] << 8 | s[1];
3329 *g++ = s[0] << 8 | s[1];
3332 *b++ = s[0] << 8 | s[1];
3336 *a++ = s[0] << 8 | s[1];
3353 for (i = 0; i < height; ++i) {
3359 png_destroy_read_struct(&png, &info, NULL);
3368 int imagetopng(opj_image_t * image, const char *write_idf)
3373 int *red, *green, *blue, *alpha;
3374 unsigned char *row_buf, *d;
3375 int has_alpha, width, height, nr_comp, color_type;
3376 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3377 int prec, ushift, dshift, is16, force16, force8;
3378 unsigned short mask = 0xffff;
3379 png_color_8 sig_bit;
3381 is16 = force16 = force8 = ushift = dshift = 0;
3383 prec = image->comps[0].prec;
3384 nr_comp = image->numcomps;
3386 if (prec > 8 && prec < 16) {
3388 dshift = prec - ushift;
3391 } else if (prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */
3393 dshift = 8 - ushift;
3398 if (prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) {
3399 fprintf(stderr, "imagetopng: can not create %s"
3400 "\n\twrong bit_depth %d\n", write_idf, prec);
3403 writer = fopen(write_idf, "wb");
3405 if (writer == NULL) {
3412 /* Create and initialize the png_struct with the desired error handler
3413 * functions. If you want to use the default stderr and longjump method,
3414 * you can supply NULL for the last three parameters. We also check that
3415 * the library version is compatible with the one used at compile time,
3416 * in case we are using dynamically linked libraries. REQUIRED.
3418 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3420 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3426 /* Allocate/initialize the image information data. REQUIRED
3428 info = png_create_info_struct(png);
3434 /* Set error handling. REQUIRED if you are not supplying your own
3435 * error handling functions in the png_create_write_struct() call.
3437 if (setjmp(png_jmpbuf(png))) {
3441 /* I/O initialization functions is REQUIRED
3443 png_init_io(png, writer);
3445 /* Set the image information here. Width and height are up to 2^31,
3446 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3447 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3448 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3449 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3450 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3451 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3456 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3457 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3458 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3459 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3462 png_set_compression_level(png, Z_BEST_COMPRESSION);
3466 } else if (prec == 8) {
3468 } else if (prec == 4) {
3470 } else if (prec == 2) {
3472 } else if (prec == 1) {
3477 && image->comps[0].dx == image->comps[1].dx
3478 && image->comps[1].dx == image->comps[2].dx
3479 && image->comps[0].dy == image->comps[1].dy
3480 && image->comps[1].dy == image->comps[2].dy
3481 && image->comps[0].prec == image->comps[1].prec
3482 && image->comps[1].prec == image->comps[2].prec) {
3485 has_alpha = (nr_comp > 3);
3487 is16 = (prec == 16);
3489 width = image->comps[0].w;
3490 height = image->comps[0].h;
3492 red = image->comps[0].data;
3493 green = image->comps[1].data;
3494 blue = image->comps[2].data;
3496 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3499 sig_bit.alpha = prec;
3500 alpha = image->comps[3].data;
3501 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3502 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3506 color_type = PNG_COLOR_TYPE_RGB;
3509 png_set_sBIT(png, info, &sig_bit);
3511 png_set_IHDR(png, info, width, height, prec,
3514 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3515 /*=============================*/
3516 png_write_info(png, info);
3517 /*=============================*/
3519 png_set_packing(png);
3521 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3522 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3523 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3525 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3527 for (y = 0; y < height; ++y) {
3530 for (x = 0; x < width; ++x) {
3536 v = (v << ushift) + (v >> dshift);
3539 *d++ = (unsigned char)(v >> 8);
3540 *d++ = (unsigned char)v;
3542 v = *green + adjustG;
3546 v = (v << ushift) + (v >> dshift);
3549 *d++ = (unsigned char)(v >> 8);
3550 *d++ = (unsigned char)v;
3552 v = *blue + adjustB;
3556 v = (v << ushift) + (v >> dshift);
3559 *d++ = (unsigned char)(v >> 8);
3560 *d++ = (unsigned char)v;
3563 v = *alpha + adjustA;
3567 v = (v << ushift) + (v >> dshift);
3570 *d++ = (unsigned char)(v >> 8);
3571 *d++ = (unsigned char)v;
3580 v = (v << ushift) + (v >> dshift);
3583 *d++ = (unsigned char)(v & mask);
3585 v = *green + adjustG;
3589 v = (v << ushift) + (v >> dshift);
3592 *d++ = (unsigned char)(v & mask);
3594 v = *blue + adjustB;
3598 v = (v << ushift) + (v >> dshift);
3601 *d++ = (unsigned char)(v & mask);
3604 v = *alpha + adjustA;
3608 v = (v << ushift) + (v >> dshift);
3611 *d++ = (unsigned char)(v & mask);
3615 png_write_row(png, row_buf);
3621 else if (nr_comp == 1 /* GRAY */
3622 || (nr_comp == 2 /* GRAY_ALPHA */
3623 && image->comps[0].dx == image->comps[1].dx
3624 && image->comps[0].dy == image->comps[1].dy
3625 && image->comps[0].prec == image->comps[1].prec)) {
3628 red = image->comps[0].data;
3630 sig_bit.gray = prec;
3631 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3634 color_type = PNG_COLOR_TYPE_GRAY;
3638 sig_bit.alpha = prec;
3639 alpha = image->comps[1].data;
3640 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3641 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3643 width = image->comps[0].w;
3644 height = image->comps[0].h;
3646 png_set_IHDR(png, info, width, height, sig_bit.gray,
3649 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3651 png_set_sBIT(png, info, &sig_bit);
3652 /*=============================*/
3653 png_write_info(png, info);
3654 /*=============================*/
3655 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3658 png_set_packing(png);
3662 row_buf = (unsigned char*)
3663 malloc(width * nr_comp * sizeof(unsigned short));
3665 for (y = 0; y < height; ++y) {
3668 for (x = 0; x < width; ++x) {
3673 v = (v << ushift) + (v >> dshift);
3676 *d++ = (unsigned char)(v >> 8);
3677 *d++ = (unsigned char)v;
3683 v = (v << ushift) + (v >> dshift);
3686 *d++ = (unsigned char)(v >> 8);
3687 *d++ = (unsigned char)v;
3690 png_write_row(png, row_buf);
3694 } else { /* prec <= 8 */
3695 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3697 for (y = 0; y < height; ++y) {
3700 for (x = 0; x < width; ++x) {
3705 v = (v << ushift) + (v >> dshift);
3708 *d++ = (unsigned char)(v & mask);
3711 v = *alpha + adjustA;
3715 v = (v << ushift) + (v >> dshift);
3718 *d++ = (unsigned char)(v & mask);
3722 png_write_row(png, row_buf);
3728 fprintf(stderr, "imagetopng: can not create %s\n", write_idf);
3731 png_write_end(png, info);
3738 png_destroy_write_struct(&png, &info);
3748 #endif /* OPJ_HAVE_LIBPNG */