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) {
742 opj_image_destroy(image);
744 "\nError: fread return a number of element different from the expected.\n");
750 for (y = 0; y < (int)H; y++) {
751 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
752 for (x = 0; x < (int)W; x++) {
753 unsigned char *pixel = &scanline[3 * x];
754 image->comps[0].data[index] = pixel[2]; /* R */
755 image->comps[1].data[index] = pixel[1]; /* G */
756 image->comps[2].data[index] = pixel[0]; /* B */
761 }/* if (Info_h.biBitCount == 24) */
762 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { /*RGB */
763 if (Info_h.biClrUsed == 0) {
764 Info_h.biClrUsed = 256;
765 } else if (Info_h.biClrUsed > 256) {
766 Info_h.biClrUsed = 256;
769 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
770 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
771 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
774 for (j = 0; j < Info_h.biClrUsed; j++) {
775 table_B[j] = (unsigned char)getc(IN);
776 table_G[j] = (unsigned char)getc(IN);
777 table_R[j] = (unsigned char)getc(IN);
780 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
786 /* Place the cursor at the beginning of the image information */
787 fseek(IN, 0, SEEK_SET);
788 fseek(IN, File_h.bfOffBits, SEEK_SET);
792 if (Info_h.biWidth % 2) {
796 numcomps = gray_scale ? 1 : 3;
797 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
798 /* initialize image components */
799 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
800 for (i = 0; i < numcomps; i++) {
801 cmptparm[i].prec = 8;
803 cmptparm[i].sgnd = 0;
804 cmptparm[i].dx = subsampling_dx;
805 cmptparm[i].dy = subsampling_dy;
809 /* create the image */
810 image = opj_image_create(numcomps, &cmptparm[0], color_space);
819 /* set image offset and reference grid */
820 image->x0 = parameters->image_offset_x0;
821 image->y0 = parameters->image_offset_y0;
822 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
823 (w - 1) * subsampling_dx + 1;
824 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
825 (h - 1) * subsampling_dy + 1;
829 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
831 if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) {
836 opj_image_destroy(image);
838 "\nError: fread return a number of element different from the expected.\n");
843 for (j = 0; j < W * H; j++) {
844 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
845 image->comps[0].data[index] =
846 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
853 for (j = 0; j < W * H; j++) {
854 if ((j % W < W - 1 && Info_h.biWidth % 2)
855 || !(Info_h.biWidth % 2)) {
856 unsigned char pixel_index =
857 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
858 image->comps[0].data[index] = table_R[pixel_index];
859 image->comps[1].data[index] = table_G[pixel_index];
860 image->comps[2].data[index] = table_B[pixel_index];
870 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/
871 unsigned char *pix, *beyond;
872 int *gray, *red, *green, *blue;
873 unsigned int x, y, max;
877 if (Info_h.biClrUsed == 0) {
878 Info_h.biClrUsed = 256;
879 } else if (Info_h.biClrUsed > 256) {
880 Info_h.biClrUsed = 256;
883 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
884 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
885 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
888 for (j = 0; j < Info_h.biClrUsed; j++) {
889 table_B[j] = (unsigned char)getc(IN);
890 table_G[j] = (unsigned char)getc(IN);
891 table_R[j] = (unsigned char)getc(IN);
893 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
900 numcomps = gray_scale ? 1 : 3;
901 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
902 /* initialize image components */
903 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
904 for (i = 0; i < numcomps; i++) {
905 cmptparm[i].prec = 8;
907 cmptparm[i].sgnd = 0;
908 cmptparm[i].dx = subsampling_dx;
909 cmptparm[i].dy = subsampling_dy;
913 /* create the image */
914 image = opj_image_create(numcomps, &cmptparm[0], color_space);
923 /* set image offset and reference grid */
924 image->x0 = parameters->image_offset_x0;
925 image->y0 = parameters->image_offset_y0;
926 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
927 - 1) * subsampling_dx + 1;
928 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
929 - 1) * subsampling_dy + 1;
933 /* Place the cursor at the beginning of the image information */
934 fseek(IN, 0, SEEK_SET);
935 fseek(IN, File_h.bfOffBits, SEEK_SET);
939 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
940 beyond = RGB + W * H;
950 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++) {
951 *pix = (unsigned char)c1;
956 if (c == 0x00) { /* EOL */
959 pix = RGB + x + (H - y - 1) * W;
960 } else if (c == 0x01) { /* EOP */
962 } else if (c == 0x02) { /* MOVE by dxdy */
967 pix = RGB + (H - y - 1) * W + x;
968 } else { /* 03 .. 255 */
970 for (; i < c && x < W && pix < beyond; i++, x++, pix++) {
972 *pix = (unsigned char)c1;
974 if (c & 1) { /* skip padding byte */
982 gray = image->comps[0].data;
989 *gray++ = table_R[uc];
992 /*int *red, *green, *blue;*/
994 red = image->comps[0].data;
995 green = image->comps[1].data;
996 blue = image->comps[2].data;
1003 *red++ = table_R[uc];
1004 *green++ = table_G[uc];
1005 *blue++ = table_B[uc];
1015 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1016 "is not yet implemented [%d]\n", Info_h.biBitCount);
1022 int imagetobmp(opj_image_t * image, const char *outfile)
1027 int adjustR, adjustG, adjustB;
1029 if (image->comps[0].prec < 8) {
1030 fprintf(stderr, "Unsupported precision: %d\n", image->comps[0].prec);
1033 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1034 && image->comps[1].dx == image->comps[2].dx
1035 && image->comps[0].dy == image->comps[1].dy
1036 && image->comps[1].dy == image->comps[2].dy
1037 && image->comps[0].prec == image->comps[1].prec
1038 && image->comps[1].prec == image->comps[2].prec) {
1040 /* -->> -->> -->> -->>
1042 <<-- <<-- <<-- <<-- */
1044 fdest = fopen(outfile, "wb");
1046 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1050 w = image->comps[0].w;
1051 h = image->comps[0].h;
1053 fprintf(fdest, "BM");
1057 fprintf(fdest, "%c%c%c%c",
1058 (unsigned char)(h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1059 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1060 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1061 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1062 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1063 ((0) >> 24) & 0xff);
1064 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff,
1065 ((54) >> 24) & 0xff);
1069 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1070 ((40) >> 24) & 0xff);
1071 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1072 (unsigned char)((w) >> 8) & 0xff,
1073 (unsigned char)((w) >> 16) & 0xff,
1074 (unsigned char)((w) >> 24) & 0xff);
1075 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1076 (unsigned char)((h) >> 8) & 0xff,
1077 (unsigned char)((h) >> 16) & 0xff,
1078 (unsigned char)((h) >> 24) & 0xff);
1079 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1080 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1081 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1082 ((0) >> 24) & 0xff);
1083 fprintf(fdest, "%c%c%c%c", (unsigned char)(3 * h * w + 3 * h * (w % 2)) & 0xff,
1084 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1085 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1086 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1087 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1088 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1089 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1090 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1091 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1092 ((0) >> 24) & 0xff);
1093 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1094 ((0) >> 24) & 0xff);
1096 if (image->comps[0].prec > 8) {
1097 adjustR = image->comps[0].prec - 8;
1098 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1099 image->comps[0].prec);
1103 if (image->comps[1].prec > 8) {
1104 adjustG = image->comps[1].prec - 8;
1105 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n",
1106 image->comps[1].prec);
1110 if (image->comps[2].prec > 8) {
1111 adjustB = image->comps[2].prec - 8;
1112 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n",
1113 image->comps[2].prec);
1118 for (i = 0; i < w * h; i++) {
1119 unsigned char rc, gc, bc;
1122 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1123 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1124 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1130 rc = (unsigned char)r;
1132 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1133 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1134 g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
1140 gc = (unsigned char)g;
1142 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1143 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1144 b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
1150 bc = (unsigned char)b;
1152 fprintf(fdest, "%c%c%c", bc, gc, rc);
1154 if ((i + 1) % w == 0) {
1155 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) { /* ADD */
1156 fprintf(fdest, "%c", 0);
1161 } else { /* Gray-scale */
1163 /* -->> -->> -->> -->>
1164 8 bits non code (Gray scale)
1165 <<-- <<-- <<-- <<-- */
1167 fdest = fopen(outfile, "wb");
1168 w = image->comps[0].w;
1169 h = image->comps[0].h;
1171 fprintf(fdest, "BM");
1175 fprintf(fdest, "%c%c%c%c",
1176 (unsigned char)(h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1177 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1178 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1179 (unsigned char)((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1180 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1181 ((0) >> 24) & 0xff);
1182 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1183 ((54 + 1024) >> 16) & 0xff,
1184 ((54 + 1024) >> 24) & 0xff);
1188 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1189 ((40) >> 24) & 0xff);
1190 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1191 (unsigned char)((w) >> 8) & 0xff,
1192 (unsigned char)((w) >> 16) & 0xff,
1193 (unsigned char)((w) >> 24) & 0xff);
1194 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1195 (unsigned char)((h) >> 8) & 0xff,
1196 (unsigned char)((h) >> 16) & 0xff,
1197 (unsigned char)((h) >> 24) & 0xff);
1198 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1199 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1200 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1201 ((0) >> 24) & 0xff);
1202 fprintf(fdest, "%c%c%c%c", (unsigned char)(h * w + h * (w % 2)) & 0xff,
1203 (unsigned char)((h * w + h * (w % 2)) >> 8) & 0xff,
1204 (unsigned char)((h * w + h * (w % 2)) >> 16) & 0xff,
1205 (unsigned char)((h * w + h * (w % 2)) >> 24) & 0xff);
1206 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1207 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1208 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1209 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1210 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1211 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1212 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1213 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1215 if (image->comps[0].prec > 8) {
1216 adjustR = image->comps[0].prec - 8;
1217 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1218 image->comps[0].prec);
1223 for (i = 0; i < 256; i++) {
1224 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1227 for (i = 0; i < w * h; i++) {
1230 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1231 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1232 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1239 fprintf(fdest, "%c", (unsigned char)r);
1241 if ((i + 1) % w == 0) {
1242 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) { /* ADD */
1243 fprintf(fdest, "%c", 0);
1253 /* -->> -->> -->> -->>
1257 <<-- <<-- <<-- <<-- */
1260 static unsigned char readuchar(FILE * f)
1263 if (!fread(&c1, 1, 1, f)) {
1265 "\nError: fread return a number of element different from the expected.\n");
1271 static unsigned short readushort(FILE * f, int bigendian)
1273 unsigned char c1, c2;
1274 if (!fread(&c1, 1, 1, f)) {
1276 "\nError: fread return a number of element different from the expected.\n");
1279 if (!fread(&c2, 1, 1, f)) {
1281 "\nError: fread return a number of element different from the expected.\n");
1285 return (c1 << 8) + c2;
1287 return (c2 << 8) + c1;
1291 static unsigned int readuint(FILE * f, int bigendian)
1293 unsigned char c1, c2, c3, c4;
1294 if (!fread(&c1, 1, 1, f)) {
1296 "\nError: fread return a number of element different from the expected.\n");
1299 if (!fread(&c2, 1, 1, f)) {
1301 "\nError: fread return a number of element different from the expected.\n");
1304 if (!fread(&c3, 1, 1, f)) {
1306 "\nError: fread return a number of element different from the expected.\n");
1309 if (!fread(&c4, 1, 1, f)) {
1311 "\nError: fread return a number of element different from the expected.\n");
1315 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1317 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1321 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
1325 int i, numcomps, max;
1326 OPJ_COLOR_SPACE color_space;
1327 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1328 opj_image_t * image = NULL;
1329 int adjustS, ushift, dshift, force8;
1331 char endian1, endian2, sign;
1336 opj_image_comp_t *comp = NULL;
1339 color_space = CLRSPC_GRAY;
1341 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1345 f = fopen(filename, "rb");
1347 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1351 fseek(f, 0, SEEK_SET);
1352 if (fscanf(f, "PG%31[ \t]%c%c%31[ \t+-]%d%31[ \t]%d%31[ \t]%d", temp, &endian1,
1353 &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
1355 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1362 while (signtmp[i] != '\0') {
1363 if (signtmp[i] == '-') {
1370 if (endian1 == 'M' && endian2 == 'L') {
1372 } else if (endian2 == 'M' && endian1 == 'L') {
1375 fprintf(stderr, "Bad pgx header, please check input file\n");
1380 /* initialize image component */
1382 cmptparm.x0 = parameters->image_offset_x0;
1383 cmptparm.y0 = parameters->image_offset_y0;
1384 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
1385 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1386 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
1387 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1397 dshift = prec - ushift;
1398 if (cmptparm.sgnd) {
1399 adjustS = (1 << (prec - 1));
1406 ushift = dshift = force8 = adjustS = 0;
1409 cmptparm.prec = prec;
1410 cmptparm.bpp = prec;
1411 cmptparm.dx = parameters->subsampling_dx;
1412 cmptparm.dy = parameters->subsampling_dy;
1414 /* create the image */
1415 image = opj_image_create(numcomps, &cmptparm, color_space);
1420 /* set image offset and reference grid */
1421 image->x0 = cmptparm.x0;
1422 image->y0 = cmptparm.x0;
1423 image->x1 = cmptparm.w;
1424 image->y1 = cmptparm.h;
1426 /* set image data */
1428 comp = &image->comps[0];
1430 for (i = 0; i < w * h; i++) {
1433 v = readuchar(f) + adjustS;
1434 v = (v << ushift) + (v >> dshift);
1435 comp->data[i] = (unsigned char)v;
1443 if (comp->prec == 8) {
1447 v = (char) readuchar(f);
1449 } else if (comp->prec <= 16) {
1451 v = readushort(f, bigendian);
1453 v = (short) readushort(f, bigendian);
1457 v = readuint(f, bigendian);
1459 v = (int) readuint(f, bigendian);
1468 comp->bpp = int_floorlog2(max) + 1;
1473 int imagetopgx(opj_image_t * image, const char *outfile)
1479 for (compno = 0; compno < image->numcomps; compno++) {
1480 opj_image_comp_t *comp = &image->comps[compno];
1481 char bname[256]; /* buffer for name */
1482 char *name = bname; /* pointer */
1485 const size_t olen = strlen(outfile);
1486 const size_t dotpos = olen - 4;
1487 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1488 if (outfile[dotpos] != '.') {
1489 /* `pgx` was recognized but there is no dot at expected position */
1490 fprintf(stderr, "ERROR -> Impossible happen.");
1494 name = (char*)malloc(total + 1);
1496 strncpy(name, outfile, dotpos);
1497 /*if (image->numcomps > 1) {*/
1498 sprintf(name + dotpos, "_%d.pgx", compno);
1500 strcpy(name+dotpos, ".pgx");
1502 fdest = fopen(name, "wb");
1504 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1507 /* don't need name anymore */
1512 w = image->comps[compno].w;
1513 h = image->comps[compno].h;
1515 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1516 if (comp->prec <= 8) {
1518 } else if (comp->prec <= 16) {
1523 for (i = 0; i < w * h; i++) {
1524 int v = image->comps[compno].data[i];
1525 for (j = nbytes - 1; j >= 0; j--) {
1526 char byte = (char)(v >> (j * 8));
1527 res = fwrite(&byte, 1, 1, fdest);
1529 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1541 /* -->> -->> -->> -->>
1545 <<-- <<-- <<-- <<-- */
1548 int width, height, maxval, depth, format;
1549 char rgb, rgba, gray, graya, bw;
1553 static char *skip_white(char *s)
1556 if (*s == '\n' || *s == '\r') {
1568 static char *skip_int(char *start, int *out_n)
1576 s = skip_white(start);
1590 *out_n = atoi(start);
1595 static char *skip_idf(char *start, char out_idf[256])
1600 s = skip_white(start);
1607 if (isalpha(*s) || *s == '_') {
1615 strncpy(out_idf, start, 255);
1620 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1623 int format, have_wh, end, ttype;
1624 char idf[256], type[256];
1627 if (fgets(line, 250, reader) == NULL) {
1628 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1632 if (line[0] != 'P') {
1633 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1636 format = atoi(line + 1);
1637 if (format < 1 || format > 7) {
1638 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1641 ph->format = format;
1642 ttype = end = have_wh = 0;
1644 while (fgets(line, 250, reader)) {
1652 s = skip_idf(s, idf);
1654 if (s == NULL || *s == 0) {
1658 if (strcmp(idf, "ENDHDR") == 0) {
1662 if (strcmp(idf, "WIDTH") == 0) {
1663 s = skip_int(s, &ph->width);
1664 if (s == NULL || *s == 0) {
1670 if (strcmp(idf, "HEIGHT") == 0) {
1671 s = skip_int(s, &ph->height);
1672 if (s == NULL || *s == 0) {
1678 if (strcmp(idf, "DEPTH") == 0) {
1679 s = skip_int(s, &ph->depth);
1680 if (s == NULL || *s == 0) {
1686 if (strcmp(idf, "MAXVAL") == 0) {
1687 s = skip_int(s, &ph->maxval);
1688 if (s == NULL || *s == 0) {
1694 if (strcmp(idf, "TUPLTYPE") == 0) {
1695 s = skip_idf(s, type);
1696 if (s == NULL || *s == 0) {
1700 if (strcmp(type, "BLACKANDWHITE") == 0) {
1705 if (strcmp(type, "GRAYSCALE") == 0) {
1710 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1715 if (strcmp(type, "RGB") == 0) {
1720 if (strcmp(type, "RGB_ALPHA") == 0) {
1725 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1728 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1730 } /* if(format == 7) */
1733 s = skip_int(s, &ph->width);
1735 s = skip_int(s, &ph->height);
1739 if (format == 1 || format == 4) {
1745 if (format == 2 || format == 3 || format == 5 || format == 6) {
1746 /* P2, P3, P5, P6: */
1747 s = skip_int(s, &ph->maxval);
1749 if (ph->maxval > 65535) {
1754 }/* while(fgets( ) */
1755 if (format == 2 || format == 3 || format > 4) {
1756 if (ph->maxval < 1 || ph->maxval > 65535) {
1760 if (ph->width < 1 || ph->height < 1) {
1766 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1769 if (ph->depth < 1 || ph->depth > 4) {
1773 if (ph->width && ph->height && ph->depth & ph->maxval && ttype) {
1777 if (format != 1 && format != 4) {
1778 if (ph->width && ph->height && ph->maxval) {
1782 if (ph->width && ph->height) {
1790 static int has_prec(int val)
1840 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1842 int subsampling_dx = parameters->subsampling_dx;
1843 int subsampling_dy = parameters->subsampling_dy;
1846 int i, compno, numcomps, w, h, prec, format;
1847 OPJ_COLOR_SPACE color_space;
1848 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1849 opj_image_t * image = NULL;
1850 struct pnm_header header_info;
1852 if ((fp = fopen(filename, "rb")) == NULL) {
1853 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1856 memset(&header_info, 0, sizeof(struct pnm_header));
1858 read_pnm_header(fp, &header_info);
1860 if (!header_info.ok) {
1865 format = header_info.format;
1868 case 1: /* ascii bitmap */
1869 case 4: /* raw bitmap */
1873 case 2: /* ascii greymap */
1874 case 5: /* raw greymap */
1878 case 3: /* ascii pixmap */
1879 case 6: /* raw pixmap */
1883 case 7: /* arbitrary map */
1884 numcomps = header_info.depth;
1892 color_space = CLRSPC_GRAY; /* GRAY, GRAYA */
1894 color_space = CLRSPC_SRGB; /* RGB, RGBA */
1897 prec = has_prec(header_info.maxval);
1903 w = header_info.width;
1904 h = header_info.height;
1905 subsampling_dx = parameters->subsampling_dx;
1906 subsampling_dy = parameters->subsampling_dy;
1908 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1910 for (i = 0; i < numcomps; i++) {
1911 cmptparm[i].prec = prec;
1912 cmptparm[i].bpp = prec;
1913 cmptparm[i].sgnd = 0;
1914 cmptparm[i].dx = subsampling_dx;
1915 cmptparm[i].dy = subsampling_dy;
1919 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1926 /* set image offset and reference grid */
1927 image->x0 = parameters->image_offset_x0;
1928 image->y0 = parameters->image_offset_y0;
1929 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1930 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1932 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1935 for (i = 0; i < w * h; i++) {
1936 for (compno = 0; compno < numcomps; compno++) {
1938 if (fscanf(fp, "%u", &index) != 1) {
1940 "\nWARNING: fscanf return a number of element different from the expected.\n");
1943 image->comps[compno].data[i] = (index * 255) / header_info.maxval;
1946 } else if ((format == 5)
1949 && (header_info.gray || header_info.graya
1950 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1951 unsigned char c0, c1, one;
1955 for (i = 0; i < w * h; i++) {
1956 for (compno = 0; compno < numcomps; compno++) {
1957 if (!fread(&c0, 1, 1, fp)) {
1959 "\nError: fread return a number of element different from the expected.\n");
1962 image->comps[compno].data[i] = c0;
1964 if (!fread(&c1, 1, 1, fp)) {
1966 "\nError: fread return a number of element different from the expected.\n");
1969 image->comps[compno].data[i] = ((c0 << 8) | c1);
1973 } else if (format == 1) { /* ascii bitmap */
1974 for (i = 0; i < w * h; i++) {
1977 if (fscanf(fp, "%u", &index) != 1) {
1979 "\nWARNING: fscanf return a number of element different from the expected.\n");
1982 image->comps[0].data[i] = (index ? 0 : 255);
1984 } else if (format == 4) {
1989 for (y = 0; y < h; ++y) {
1993 for (x = 0; x < w; ++x) {
1996 uc = (unsigned char)getc(fp);
1998 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
2003 } else if ((format == 7 && header_info.bw)) { /*MONO*/
2006 for (i = 0; i < w * h; ++i) {
2007 if (!fread(&uc, 1, 1, fp)) {
2009 "\nError: fread return a number of element different from the expected.\n");
2011 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
2019 int imagetopnm(opj_image_t * image, const char *outfile)
2021 int *red, *green, *blue, *alpha;
2023 int i, compno, ncomp;
2024 int adjustR, adjustG, adjustB, adjustA;
2025 int fails, two, want_gray, has_alpha, triple;
2028 const char *tmp = outfile;
2031 if ((prec = image->comps[0].prec) > 16) {
2032 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
2033 "\n\t: refused.\n", __FILE__, __LINE__, prec);
2036 two = has_alpha = 0;
2038 ncomp = image->numcomps;
2044 want_gray = (*tmp == 'g' || *tmp == 'G');
2045 ncomp = image->numcomps;
2051 if (ncomp == 2 /* GRAYA */
2052 || (ncomp > 2 /* RGB, RGBA */
2053 && image->comps[0].dx == image->comps[1].dx
2054 && image->comps[1].dx == image->comps[2].dx
2055 && image->comps[0].dy == image->comps[1].dy
2056 && image->comps[1].dy == image->comps[2].dy
2057 && image->comps[0].prec == image->comps[1].prec
2058 && image->comps[1].prec == image->comps[2].prec
2060 fdest = fopen(outfile, "wb");
2063 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
2067 triple = (ncomp > 2);
2068 wr = image->comps[0].w;
2069 hr = image->comps[0].h;
2070 max = (1 << prec) - 1;
2071 has_alpha = (ncomp == 4 || ncomp == 2);
2073 red = image->comps[0].data;
2076 green = image->comps[1].data;
2077 blue = image->comps[2].data;
2079 green = blue = NULL;
2083 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
2085 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
2086 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2087 wr, hr, ncomp, max, tt);
2088 alpha = image->comps[ncomp - 1].data;
2089 adjustA = (image->comps[ncomp - 1].sgnd ?
2090 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2092 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2093 opj_version(), wr, hr, max);
2096 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2099 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2100 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2102 adjustG = adjustB = 0;
2105 for (i = 0; i < wr * hr; ++i) {
2110 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2113 v = *green + adjustG;
2116 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2118 v = *blue + adjustB;
2121 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2126 v = *alpha + adjustA;
2129 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2137 fprintf(fdest, "%c", (unsigned char)*red++);
2139 fprintf(fdest, "%c%c", (unsigned char)*green++, (unsigned char)*blue++);
2143 fprintf(fdest, "%c", (unsigned char)*alpha++);
2154 if (image->numcomps > ncomp) {
2155 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2156 fprintf(stderr, " is written to the file\n");
2158 destname = (char*)malloc(strlen(outfile) + 8);
2160 for (compno = 0; compno < ncomp; compno++) {
2162 sprintf(destname, "%d.%s", compno, outfile);
2164 sprintf(destname, "%s", outfile);
2167 fdest = fopen(destname, "wb");
2169 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2173 wr = image->comps[compno].w;
2174 hr = image->comps[compno].h;
2175 prec = image->comps[compno].prec;
2176 max = (1 << prec) - 1;
2178 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2179 opj_version(), wr, hr, max);
2181 red = image->comps[compno].data;
2183 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2186 for (i = 0; i < wr * hr; i++) {
2190 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2195 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2198 } else { /* prec <= 8 */
2199 for (i = 0; i < wr * hr; ++i) {
2200 fprintf(fdest, "%c", (unsigned char)(*red + adjustR));
2211 #ifdef OPJ_HAVE_LIBTIFF
2212 /* -->> -->> -->> -->>
2216 <<-- <<-- <<-- <<-- */
2218 int imagetotif(opj_image_t * image, const char *outfile)
2220 int width, height, imgsize;
2221 int bps, index, adjust, sgnd;
2222 int ushift, dshift, has_alpha, force16;
2228 ushift = dshift = force16 = has_alpha = 0;
2229 bps = image->comps[0].prec;
2231 if (bps > 8 && bps < 16) {
2233 dshift = bps - ushift;
2238 if (bps != 8 && bps != 16) {
2239 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2241 fprintf(stderr, "\tAborting\n");
2244 tif = TIFFOpen(outfile, "wb");
2247 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2250 sgnd = image->comps[0].sgnd;
2251 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2253 if (image->numcomps >= 3
2254 && image->comps[0].dx == image->comps[1].dx
2255 && image->comps[1].dx == image->comps[2].dx
2256 && image->comps[0].dy == image->comps[1].dy
2257 && image->comps[1].dy == image->comps[2].dy
2258 && image->comps[0].prec == image->comps[1].prec
2259 && image->comps[1].prec == image->comps[2].prec) {
2260 has_alpha = (image->numcomps == 4);
2262 width = image->comps[0].w;
2263 height = image->comps[0].h;
2264 imgsize = width * height ;
2266 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2267 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2268 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2269 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2270 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2271 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2272 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2273 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2274 strip_size = TIFFStripSize(tif);
2275 buf = _TIFFmalloc(strip_size);
2278 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2279 unsigned char *dat8;
2280 tsize_t i, ssize, last_i = 0;
2282 ssize = TIFFStripSize(tif);
2283 dat8 = (unsigned char*)buf;
2286 step = 3 + has_alpha;
2289 for (i = 0; i < ssize - restx; i += step) {
2292 if (index < imgsize) {
2293 r = image->comps[0].data[index];
2294 g = image->comps[1].data[index];
2295 b = image->comps[2].data[index];
2297 a = image->comps[3].data[index];
2322 if (last_i < ssize) {
2323 for (i = last_i; i < ssize; i += step) {
2326 if (index < imgsize) {
2327 r = image->comps[0].data[index];
2328 g = image->comps[1].data[index];
2329 b = image->comps[2].data[index];
2331 a = image->comps[3].data[index];
2343 if (i + 1 < ssize) {
2348 if (i + 2 < ssize) {
2354 if (i + 3 < ssize) {
2365 }/*if(last_i < ssize)*/
2368 else if (bps == 16) {
2369 step = 6 + has_alpha + has_alpha;
2372 for (i = 0; i < ssize - restx ; i += step) {
2375 if (index < imgsize) {
2376 r = image->comps[0].data[index];
2377 g = image->comps[1].data[index];
2378 b = image->comps[2].data[index];
2380 a = image->comps[3].data[index];
2392 r = (r << ushift) + (r >> dshift);
2393 g = (g << ushift) + (g >> dshift);
2394 b = (b << ushift) + (b >> dshift);
2396 a = (a << ushift) + (a >> dshift);
2399 dat8[i + 0] = r; /*LSB*/
2400 dat8[i + 1] = (r >> 8); /*MSB*/
2402 dat8[i + 3] = (g >> 8);
2404 dat8[i + 5] = (b >> 8);
2407 dat8[i + 7] = (a >> 8);
2416 if (last_i < ssize) {
2417 for (i = last_i ; i < ssize ; i += step) {
2420 if (index < imgsize) {
2421 r = image->comps[0].data[index];
2422 g = image->comps[1].data[index];
2423 b = image->comps[2].data[index];
2425 a = image->comps[3].data[index];
2437 r = (r << ushift) + (r >> dshift);
2438 g = (g << ushift) + (g >> dshift);
2439 b = (b << ushift) + (b >> dshift);
2441 a = (a << ushift) + (a >> dshift);
2444 dat8[i + 0] = r; /*LSB*/
2445 if (i + 1 < ssize) {
2446 dat8[i + 1] = (r >> 8);
2450 if (i + 2 < ssize) {
2455 if (i + 3 < ssize) {
2456 dat8[i + 3] = (g >> 8);
2460 if (i + 4 < ssize) {
2465 if (i + 5 < ssize) {
2466 dat8[i + 5] = (b >> 8);
2472 if (i + 6 < ssize) {
2477 if (i + 7 < ssize) {
2478 dat8[i + 7] = (a >> 8);
2488 }/*if(last_i < ssize)*/
2491 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2492 }/*for(strip = 0; )*/
2494 _TIFFfree((void*)buf);
2500 if (image->numcomps == 1 /* GRAY */
2501 || (image->numcomps == 2 /* GRAY_ALPHA */
2502 && image->comps[0].dx == image->comps[1].dx
2503 && image->comps[0].dy == image->comps[1].dy
2504 && image->comps[0].prec == image->comps[1].prec)) {
2507 has_alpha = (image->numcomps == 2);
2509 width = image->comps[0].w;
2510 height = image->comps[0].h;
2511 imgsize = width * height;
2514 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2515 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2516 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2517 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2518 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2519 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2520 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2521 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2523 /* Get a buffer for the data */
2524 strip_size = TIFFStripSize(tif);
2525 buf = _TIFFmalloc(strip_size);
2528 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2529 unsigned char *dat8;
2530 tsize_t i, ssize = TIFFStripSize(tif);
2531 dat8 = (unsigned char*)buf;
2534 step = 1 + has_alpha;
2536 for (i = 0; i < ssize; i += step) {
2537 if (index < imgsize) {
2540 r = image->comps[0].data[index];
2542 a = image->comps[1].data[index];
2561 else if (bps == 16) {
2562 step = 2 + has_alpha + has_alpha;
2564 for (i = 0; i < ssize; i += step) {
2565 if (index < imgsize) {
2568 r = image->comps[0].data[index];
2570 a = image->comps[1].data[index];
2580 r = (r << ushift) + (r >> dshift);
2582 a = (a << ushift) + (a >> dshift);
2585 dat8[i + 0] = r; /*LSB*/
2586 dat8[i + 1] = r >> 8; /*MSB*/
2589 dat8[i + 3] = a >> 8;
2592 }/*if(index < imgsize)*/
2598 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2609 fprintf(stderr, "imagetotif: Bad color format.\n"
2610 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2611 fprintf(stderr, "\tFOUND: numcomps(%d)\n\tAborting\n",
2618 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2619 * CINEMA : 12 bit precision
2621 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2623 int subsampling_dx = parameters->subsampling_dx;
2624 int subsampling_dy = parameters->subsampling_dy;
2629 int j, numcomps, w, h, index;
2630 OPJ_COLOR_SPACE color_space;
2631 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2632 opj_image_t *image = NULL;
2635 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2636 unsigned int tiWidth, tiHeight;
2638 tif = TIFFOpen(filename, "r");
2641 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2644 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2645 tiWidth = tiHeight = 0;
2647 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2648 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2649 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2650 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2651 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2652 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2653 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2658 unsigned short b = tiBps, p = tiPhoto;
2660 if (tiBps != 8 && tiBps != 16 && tiBps != 12) {
2663 if (tiPhoto != 1 && tiPhoto != 2) {
2669 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits"
2670 " implemented\n", tiBps);
2672 fprintf(stderr, "tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2673 " and GRAY(A) has been implemented\n", (int) tiPhoto);
2675 fprintf(stderr, "\tAborting\n");
2681 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2683 uint16 extrasamples;
2685 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2686 &extrasamples, &sampleinfo);
2688 if (extrasamples >= 1) {
2689 switch (sampleinfo[0]) {
2690 case EXTRASAMPLE_UNSPECIFIED:
2691 /* Workaround for some images without correct info about alpha channel
2698 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2699 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2703 } else /* extrasamples == 0 */
2704 if (tiSpp == 4 || tiSpp == 2) {
2709 /* initialize image components
2711 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2713 if (tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */
2714 numcomps = 3 + has_alpha;
2715 color_space = CLRSPC_SRGB;
2717 for (j = 0; j < numcomps; j++) {
2718 if (parameters->cp_cinema) {
2719 cmptparm[j].prec = 12;
2720 cmptparm[j].bpp = 12;
2722 cmptparm[j].prec = tiBps;
2723 cmptparm[j].bpp = tiBps;
2725 cmptparm[j].dx = subsampling_dx;
2726 cmptparm[j].dy = subsampling_dy;
2731 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2737 /* set image offset and reference grid
2739 image->x0 = parameters->image_offset_x0;
2740 image->y0 = parameters->image_offset_y0;
2741 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2742 image->x0 + (w - 1) * subsampling_dx + 1;
2743 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2744 image->y0 + (h - 1) * subsampling_dy + 1;
2746 buf = _TIFFmalloc(TIFFStripSize(tif));
2748 strip_size = TIFFStripSize(tif);
2750 imgsize = image->comps[0].w * image->comps[0].h ;
2751 /* Read the Image components
2753 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2754 unsigned char *dat8;
2757 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2758 dat8 = (unsigned char*)buf;
2761 step = 6 + has_alpha + has_alpha;
2763 for (i = 0; i < ssize; i += step) {
2764 if (index < imgsize) {
2765 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0]; /* R */
2766 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2]; /* G */
2767 image->comps[2].data[index] = (dat8[i + 5] << 8) | dat8[i + 4]; /* B */
2769 image->comps[3].data[index] = (dat8[i + 7] << 8) | dat8[i + 6];
2772 if (parameters->cp_cinema) {
2773 /* Rounding 16 to 12 bits
2775 image->comps[0].data[index] =
2776 (image->comps[0].data[index] + 0x08) >> 4 ;
2777 image->comps[1].data[index] =
2778 (image->comps[1].data[index] + 0x08) >> 4 ;
2779 image->comps[2].data[index] =
2780 (image->comps[2].data[index] + 0x08) >> 4 ;
2782 image->comps[3].data[index] =
2783 (image->comps[3].data[index] + 0x08) >> 4 ;
2790 }/*if(tiBps == 16)*/
2791 else if (tiBps == 8) {
2792 step = 3 + has_alpha;
2794 for (i = 0; i < ssize; i += step) {
2795 if (index < imgsize) {
2796 image->comps[0].data[index] = dat8[i + 0]; /* R */
2797 image->comps[1].data[index] = dat8[i + 1]; /* G */
2798 image->comps[2].data[index] = dat8[i + 2]; /* B */
2800 image->comps[3].data[index] = dat8[i + 3];
2803 if (parameters->cp_cinema) {
2804 /* Rounding 8 to 12 bits
2806 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2807 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2808 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2810 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2819 }/*if( tiBps == 8)*/
2820 else if (tiBps == 12) { /* CINEMA file */
2823 for (i = 0; i < ssize; i += step) {
2824 if ((index < imgsize) & (index + 1 < imgsize)) {
2825 image->comps[0].data[index] = (dat8[i + 0] << 4) | (dat8[i + 1] >> 4);
2826 image->comps[1].data[index] = ((dat8[i + 1] & 0x0f) << 8) | dat8[i + 2];
2828 image->comps[2].data[index] = (dat8[i + 3] << 4) | (dat8[i + 4] >> 4);
2829 image->comps[0].data[index + 1] = ((dat8[i + 4] & 0x0f) << 8) | dat8[i + 5];
2831 image->comps[1].data[index + 1] = (dat8[i + 6] << 4) |
2833 image->comps[2].data[index + 1] = ((dat8[i + 7] & 0x0f) << 8) | dat8[i + 8];
2841 }/*for(strip = 0; )*/
2849 if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */
2850 numcomps = 1 + has_alpha;
2851 color_space = CLRSPC_GRAY;
2853 for (j = 0; j < numcomps; ++j) {
2854 cmptparm[j].prec = tiBps;
2855 cmptparm[j].bpp = tiBps;
2856 cmptparm[j].dx = subsampling_dx;
2857 cmptparm[j].dy = subsampling_dy;
2861 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2867 /* set image offset and reference grid
2869 image->x0 = parameters->image_offset_x0;
2870 image->y0 = parameters->image_offset_y0;
2871 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2872 image->x0 + (w - 1) * subsampling_dx + 1;
2873 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2874 image->y0 + (h - 1) * subsampling_dy + 1;
2876 buf = _TIFFmalloc(TIFFStripSize(tif));
2878 strip_size = TIFFStripSize(tif);
2880 imgsize = image->comps[0].w * image->comps[0].h ;
2881 /* Read the Image components
2883 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2884 unsigned char *dat8;
2888 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2889 dat8 = (unsigned char*)buf;
2892 step = 2 + has_alpha + has_alpha;
2894 for (i = 0; i < ssize; i += step) {
2895 if (index < imgsize) {
2896 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0];
2898 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2];
2905 } else if (tiBps == 8) {
2906 step = 1 + has_alpha;
2908 for (i = 0; i < ssize; i += step) {
2909 if (index < imgsize) {
2910 image->comps[0].data[index] = dat8[i + 0];
2912 image->comps[1].data[index] = dat8[i + 1];
2931 #endif /* OPJ_HAVE_LIBTIFF */
2933 /* -->> -->> -->> -->>
2937 <<-- <<-- <<-- <<-- */
2939 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2940 raw_cparameters_t *raw_cp)
2942 int subsampling_dx = parameters->subsampling_dx;
2943 int subsampling_dy = parameters->subsampling_dy;
2946 int i, compno, numcomps, w, h;
2947 OPJ_COLOR_SPACE color_space;
2948 opj_image_cmptparm_t *cmptparm;
2949 opj_image_t * image = NULL;
2952 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2953 raw_cp->rawBitDepth)) == 0) {
2954 fprintf(stderr, "\nError: invalid raw image parameters\n");
2955 fprintf(stderr, "Please use the Format option -F:\n");
2957 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2958 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2959 fprintf(stderr, "Aborting\n");
2963 f = fopen(filename, "rb");
2965 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2966 fprintf(stderr, "Aborting\n");
2969 numcomps = raw_cp->rawComp;
2970 color_space = CLRSPC_SRGB;
2971 w = raw_cp->rawWidth;
2972 h = raw_cp->rawHeight;
2973 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(
2974 opj_image_cmptparm_t));
2976 /* initialize image components */
2977 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2978 for (i = 0; i < numcomps; i++) {
2979 cmptparm[i].prec = raw_cp->rawBitDepth;
2980 cmptparm[i].bpp = raw_cp->rawBitDepth;
2981 cmptparm[i].sgnd = raw_cp->rawSigned;
2982 cmptparm[i].dx = subsampling_dx;
2983 cmptparm[i].dy = subsampling_dy;
2987 /* create the image */
2988 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2993 /* set image offset and reference grid */
2994 image->x0 = parameters->image_offset_x0;
2995 image->y0 = parameters->image_offset_y0;
2996 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2997 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2999 if (raw_cp->rawBitDepth <= 8) {
3000 unsigned char value = 0;
3001 for (compno = 0; compno < numcomps; compno++) {
3002 for (i = 0; i < w * h; i++) {
3003 if (!fread(&value, 1, 1, f)) {
3004 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3008 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
3011 } else if (raw_cp->rawBitDepth <= 16) {
3012 unsigned short value;
3013 for (compno = 0; compno < numcomps; compno++) {
3014 for (i = 0; i < w * h; i++) {
3016 if (!fread(&temp, 1, 1, f)) {
3017 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3022 if (!fread(&temp, 1, 1, f)) {
3023 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3028 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
3033 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3038 if (fread(&ch, 1, 1, f)) {
3039 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
3046 int imagetoraw(opj_image_t * image, const char *outfile)
3048 FILE *rawFile = NULL;
3055 if ((image->numcomps * image->x1 * image->y1) == 0) {
3056 fprintf(stderr, "\nError: invalid raw image parameters\n");
3060 rawFile = fopen(outfile, "wb");
3062 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3066 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
3068 for (compno = 0; compno < image->numcomps; compno++) {
3069 fprintf(stdout, "Component %d characteristics: %dx%dx%d %s\n", compno,
3070 image->comps[compno].w,
3071 image->comps[compno].h, image->comps[compno].prec,
3072 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
3074 w = image->comps[compno].w;
3075 h = image->comps[compno].h;
3077 if (image->comps[compno].prec <= 8) {
3078 if (image->comps[compno].sgnd == 1) {
3080 int mask = (1 << image->comps[compno].prec) - 1;
3081 ptr = image->comps[compno].data;
3082 for (line = 0; line < h; line++) {
3083 for (row = 0; row < w; row++) {
3084 curr = (signed char)(*ptr & mask);
3085 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
3087 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3093 } else if (image->comps[compno].sgnd == 0) {
3095 int mask = (1 << image->comps[compno].prec) - 1;
3096 ptr = image->comps[compno].data;
3097 for (line = 0; line < h; line++) {
3098 for (row = 0; row < w; row++) {
3099 curr = (unsigned char)(*ptr & mask);
3100 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
3102 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3109 } else if (image->comps[compno].prec <= 16) {
3110 if (image->comps[compno].sgnd == 1) {
3111 signed short int curr;
3112 int mask = (1 << image->comps[compno].prec) - 1;
3113 ptr = image->comps[compno].data;
3114 for (line = 0; line < h; line++) {
3115 for (row = 0; row < w; row++) {
3117 curr = (signed short int)(*ptr & mask);
3118 temp = (unsigned char)(curr >> 8);
3119 res = fwrite(&temp, 1, 1, rawFile);
3121 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3124 temp = (unsigned char) curr;
3125 res = fwrite(&temp, 1, 1, rawFile);
3127 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3133 } else if (image->comps[compno].sgnd == 0) {
3134 unsigned short int curr;
3135 int mask = (1 << image->comps[compno].prec) - 1;
3136 ptr = image->comps[compno].data;
3137 for (line = 0; line < h; line++) {
3138 for (row = 0; row < w; row++) {
3140 curr = (unsigned short int)(*ptr & mask);
3141 temp = (unsigned char)(curr >> 8);
3142 res = fwrite(&temp, 1, 1, rawFile);
3144 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3147 temp = (unsigned char) curr;
3148 res = fwrite(&temp, 1, 1, rawFile);
3150 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3157 } else if (image->comps[compno].prec <= 32) {
3158 fprintf(stderr, "More than 16 bits per component no handled yet\n");
3161 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
3169 #ifdef OPJ_HAVE_LIBPNG
3171 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3172 #define MAGIC_SIZE 8
3173 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3175 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3179 double gamma, display_exponent;
3180 int bit_depth, interlace_type, compression_type, filter_type;
3182 png_uint_32 resx, resy;
3184 png_uint_32 width, height;
3185 int color_type, has_alpha, is16;
3188 unsigned char **rows;
3191 opj_image_cmptparm_t cmptparm[4];
3193 unsigned int nr_comp;
3195 unsigned char sigbuf[8];
3197 if ((reader = fopen(read_idf, "rb")) == NULL) {
3198 fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
3205 if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3206 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
3207 fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
3210 /* libpng-VERSION/example.c:
3211 * PC : screen_gamma = 2.2;
3212 * Mac: screen_gamma = 1.7 or 1.0;
3214 display_exponent = 2.2;
3216 if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3217 NULL, NULL, NULL)) == NULL) {
3220 if ((info = png_create_info_struct(png)) == NULL) {
3224 if (setjmp(png_jmpbuf(png))) {
3228 png_init_io(png, reader);
3229 png_set_sig_bytes(png, MAGIC_SIZE);
3231 png_read_info(png, info);
3233 if (png_get_IHDR(png, info, &width, &height,
3234 &bit_depth, &color_type, &interlace_type,
3235 &compression_type, &filter_type) == 0) {
3239 /* png_set_expand():
3240 * expand paletted images to RGB, expand grayscale images of
3241 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3242 * to alpha channels.
3244 if (color_type == PNG_COLOR_TYPE_PALETTE) {
3245 png_set_expand(png);
3246 } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
3247 png_set_expand(png);
3250 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
3251 png_set_expand(png);
3254 is16 = (bit_depth == 16);
3256 /* GRAY => RGB; GRAY_ALPHA => RGBA
3258 if (color_type == PNG_COLOR_TYPE_GRAY
3259 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
3260 png_set_gray_to_rgb(png);
3262 (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
3263 PNG_COLOR_TYPE_RGB_ALPHA);
3265 if (!png_get_gAMA(png, info, &gamma)) {
3269 png_set_gamma(png, display_exponent, gamma);
3271 png_read_update_info(png, info);
3273 png_get_pHYs(png, info, &resx, &resy, &unit);
3275 color_type = png_get_color_type(png, info);
3277 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3279 nr_comp = 3 + has_alpha;
3281 bit_depth = png_get_bit_depth(png, info);
3283 rows = (unsigned char**)calloc(height + 1, sizeof(unsigned char*));
3284 for (i = 0; i < height; ++i) {
3285 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png, info));
3288 png_read_image(png, rows);
3290 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3292 sub_dx = params->subsampling_dx;
3293 sub_dy = params->subsampling_dy;
3295 for (i = 0; i < nr_comp; ++i) {
3296 cmptparm[i].prec = bit_depth;
3297 /* bits_per_pixel: 8 or 16 */
3298 cmptparm[i].bpp = bit_depth;
3299 cmptparm[i].sgnd = 0;
3300 cmptparm[i].dx = sub_dx;
3301 cmptparm[i].dy = sub_dy;
3302 cmptparm[i].w = width;
3303 cmptparm[i].h = height;
3306 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3308 if (image == NULL) {
3312 image->x0 = params->image_offset_x0;
3313 image->y0 = params->image_offset_y0;
3314 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3315 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3317 r = image->comps[0].data;
3318 g = image->comps[1].data;
3319 b = image->comps[2].data;
3320 a = image->comps[3].data;
3322 for (i = 0; i < height; ++i) {
3325 for (j = 0; j < width; ++j) {
3327 *r++ = s[0] << 8 | s[1];
3330 *g++ = s[0] << 8 | s[1];
3333 *b++ = s[0] << 8 | s[1];
3337 *a++ = s[0] << 8 | s[1];
3354 for (i = 0; i < height; ++i) {
3360 png_destroy_read_struct(&png, &info, NULL);
3369 int imagetopng(opj_image_t * image, const char *write_idf)
3374 int *red, *green, *blue, *alpha;
3375 unsigned char *row_buf, *d;
3376 int has_alpha, width, height, nr_comp, color_type;
3377 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3378 int prec, ushift, dshift, is16, force16, force8;
3379 unsigned short mask = 0xffff;
3380 png_color_8 sig_bit;
3382 is16 = force16 = force8 = ushift = dshift = 0;
3384 prec = image->comps[0].prec;
3385 nr_comp = image->numcomps;
3387 if (prec > 8 && prec < 16) {
3389 dshift = prec - ushift;
3392 } else if (prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */
3394 dshift = 8 - ushift;
3399 if (prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) {
3400 fprintf(stderr, "imagetopng: can not create %s"
3401 "\n\twrong bit_depth %d\n", write_idf, prec);
3404 writer = fopen(write_idf, "wb");
3406 if (writer == NULL) {
3413 /* Create and initialize the png_struct with the desired error handler
3414 * functions. If you want to use the default stderr and longjump method,
3415 * you can supply NULL for the last three parameters. We also check that
3416 * the library version is compatible with the one used at compile time,
3417 * in case we are using dynamically linked libraries. REQUIRED.
3419 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3421 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3427 /* Allocate/initialize the image information data. REQUIRED
3429 info = png_create_info_struct(png);
3435 /* Set error handling. REQUIRED if you are not supplying your own
3436 * error handling functions in the png_create_write_struct() call.
3438 if (setjmp(png_jmpbuf(png))) {
3442 /* I/O initialization functions is REQUIRED
3444 png_init_io(png, writer);
3446 /* Set the image information here. Width and height are up to 2^31,
3447 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3448 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3449 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3450 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3451 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3452 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3457 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3458 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3459 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3460 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3463 png_set_compression_level(png, Z_BEST_COMPRESSION);
3467 } else if (prec == 8) {
3469 } else if (prec == 4) {
3471 } else if (prec == 2) {
3473 } else if (prec == 1) {
3478 && image->comps[0].dx == image->comps[1].dx
3479 && image->comps[1].dx == image->comps[2].dx
3480 && image->comps[0].dy == image->comps[1].dy
3481 && image->comps[1].dy == image->comps[2].dy
3482 && image->comps[0].prec == image->comps[1].prec
3483 && image->comps[1].prec == image->comps[2].prec) {
3486 has_alpha = (nr_comp > 3);
3488 is16 = (prec == 16);
3490 width = image->comps[0].w;
3491 height = image->comps[0].h;
3493 red = image->comps[0].data;
3494 green = image->comps[1].data;
3495 blue = image->comps[2].data;
3497 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3500 sig_bit.alpha = prec;
3501 alpha = image->comps[3].data;
3502 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3503 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3507 color_type = PNG_COLOR_TYPE_RGB;
3510 png_set_sBIT(png, info, &sig_bit);
3512 png_set_IHDR(png, info, width, height, prec,
3515 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3516 /*=============================*/
3517 png_write_info(png, info);
3518 /*=============================*/
3520 png_set_packing(png);
3522 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3523 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3524 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3526 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3528 for (y = 0; y < height; ++y) {
3531 for (x = 0; x < width; ++x) {
3537 v = (v << ushift) + (v >> dshift);
3540 *d++ = (unsigned char)(v >> 8);
3541 *d++ = (unsigned char)v;
3543 v = *green + adjustG;
3547 v = (v << ushift) + (v >> dshift);
3550 *d++ = (unsigned char)(v >> 8);
3551 *d++ = (unsigned char)v;
3553 v = *blue + adjustB;
3557 v = (v << ushift) + (v >> dshift);
3560 *d++ = (unsigned char)(v >> 8);
3561 *d++ = (unsigned char)v;
3564 v = *alpha + adjustA;
3568 v = (v << ushift) + (v >> dshift);
3571 *d++ = (unsigned char)(v >> 8);
3572 *d++ = (unsigned char)v;
3581 v = (v << ushift) + (v >> dshift);
3584 *d++ = (unsigned char)(v & mask);
3586 v = *green + adjustG;
3590 v = (v << ushift) + (v >> dshift);
3593 *d++ = (unsigned char)(v & mask);
3595 v = *blue + adjustB;
3599 v = (v << ushift) + (v >> dshift);
3602 *d++ = (unsigned char)(v & mask);
3605 v = *alpha + adjustA;
3609 v = (v << ushift) + (v >> dshift);
3612 *d++ = (unsigned char)(v & mask);
3616 png_write_row(png, row_buf);
3622 else if (nr_comp == 1 /* GRAY */
3623 || (nr_comp == 2 /* GRAY_ALPHA */
3624 && image->comps[0].dx == image->comps[1].dx
3625 && image->comps[0].dy == image->comps[1].dy
3626 && image->comps[0].prec == image->comps[1].prec)) {
3629 red = image->comps[0].data;
3631 sig_bit.gray = prec;
3632 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3635 color_type = PNG_COLOR_TYPE_GRAY;
3639 sig_bit.alpha = prec;
3640 alpha = image->comps[1].data;
3641 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3642 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3644 width = image->comps[0].w;
3645 height = image->comps[0].h;
3647 png_set_IHDR(png, info, width, height, sig_bit.gray,
3650 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3652 png_set_sBIT(png, info, &sig_bit);
3653 /*=============================*/
3654 png_write_info(png, info);
3655 /*=============================*/
3656 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3659 png_set_packing(png);
3663 row_buf = (unsigned char*)
3664 malloc(width * nr_comp * sizeof(unsigned short));
3666 for (y = 0; y < height; ++y) {
3669 for (x = 0; x < width; ++x) {
3674 v = (v << ushift) + (v >> dshift);
3677 *d++ = (unsigned char)(v >> 8);
3678 *d++ = (unsigned char)v;
3684 v = (v << ushift) + (v >> dshift);
3687 *d++ = (unsigned char)(v >> 8);
3688 *d++ = (unsigned char)v;
3691 png_write_row(png, row_buf);
3695 } else { /* prec <= 8 */
3696 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3698 for (y = 0; y < height; ++y) {
3701 for (x = 0; x < width; ++x) {
3706 v = (v << ushift) + (v >> dshift);
3709 *d++ = (unsigned char)(v & mask);
3712 v = *alpha + adjustA;
3716 v = (v << ushift) + (v >> dshift);
3719 *d++ = (unsigned char)(v & mask);
3723 png_write_row(png, row_buf);
3729 fprintf(stderr, "imagetopng: can not create %s\n", write_idf);
3732 png_write_end(png, info);
3739 png_destroy_write_struct(&png, &info);
3749 #endif /* OPJ_HAVE_LIBPNG */