2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 #include "opj_apps_config.h"
46 #ifdef OPJ_HAVE_LIBTIFF
48 #endif /* OPJ_HAVE_LIBTIFF */
50 #ifdef OPJ_HAVE_LIBPNG
53 #endif /* OPJ_HAVE_LIBPNG */
59 * Get logarithm of an integer and round downwards.
63 static int int_floorlog2(int a)
66 for (l = 0; a > 1; l++) {
72 /* -->> -->> -->> -->>
76 <<-- <<-- <<-- <<-- */
78 #ifdef INFORMATION_ONLY
79 /* TGA header definition. */
81 unsigned char id_length; /* Image id field length */
82 unsigned char colour_map_type; /* Colour map type */
83 unsigned char image_type; /* Image type */
85 ** Colour map specification
87 unsigned short colour_map_index; /* First entry index */
88 unsigned short colour_map_length; /* Colour map length */
89 unsigned char colour_map_entry_size; /* Colour map entry size */
91 ** Image specification
93 unsigned short x_origin; /* x origin of image */
94 unsigned short y_origin; /* u origin of image */
95 unsigned short image_width; /* Image width */
96 unsigned short image_height; /* Image height */
97 unsigned char pixel_depth; /* Pixel depth */
98 unsigned char image_desc; /* Image descriptor */
100 #endif /* INFORMATION_ONLY */
102 static unsigned short get_ushort(unsigned short val)
105 #ifdef OPJ_BIG_ENDIAN
106 return (((val & 0xff) << 8) + (val >> 8));
113 #define TGA_HEADER_SIZE 18
115 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
116 unsigned int *width, unsigned int *height, int *flip_image)
120 unsigned char id_len, cmap_type, image_type;
121 unsigned char pixel_depth, image_desc;
122 unsigned short cmap_index, cmap_len, cmap_entry_size;
123 unsigned short x_origin, y_origin, image_w, image_h;
125 if (!bits_per_pixel || !width || !height || !flip_image) {
128 tga = (unsigned char*)malloc(18);
130 if (fread(tga, TGA_HEADER_SIZE, 1, fp) != 1) {
132 "\nError: fread return a number of element different from the expected.\n");
136 id_len = (unsigned char)tga[0];
137 cmap_type = (unsigned char)tga[1];
138 image_type = (unsigned char)tga[2];
139 cmap_index = get_ushort(*(unsigned short*)(&tga[3]));
140 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
141 cmap_entry_size = (unsigned char)tga[7];
144 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
145 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
146 image_w = get_ushort(*(unsigned short*)(&tga[12]));
147 image_h = get_ushort(*(unsigned short*)(&tga[14]));
148 pixel_depth = (unsigned char)tga[16];
149 image_desc = (unsigned char)tga[17];
153 *bits_per_pixel = (unsigned int)pixel_depth;
154 *width = (unsigned int)image_w;
155 *height = (unsigned int)image_h;
157 /* Ignore tga identifier, if present ... */
159 unsigned char *id = (unsigned char *) malloc(id_len);
160 if (!fread(id, id_len, 1, fp)) {
162 "\nError: fread return a number of element different from the expected.\n");
169 /* Test for compressed formats ... not yet supported ...
170 // Note :- 9 - RLE encoded palettized.
171 // 10 - RLE encoded RGB. */
172 if (image_type > 8) {
173 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
177 *flip_image = !(image_desc & 32);
179 /* Palettized formats are not yet supported, skip over the palette, if present ... */
180 palette_size = cmap_len * (cmap_entry_size / 8);
182 if (palette_size > 0) {
183 fprintf(stderr, "File contains a palette - not yet supported.");
184 fseek(fp, palette_size, SEEK_CUR);
189 #ifdef OPJ_BIG_ENDIAN
191 static inline uint16_t swap16(uint16_t x)
193 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
198 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
201 unsigned short image_w, image_h, us0;
202 unsigned char uc0, image_type;
203 unsigned char pixel_depth, image_desc;
205 if (!bits_per_pixel || !width || !height) {
211 if (bits_per_pixel < 256) {
212 pixel_depth = (unsigned char)bits_per_pixel;
214 fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header");
219 if (fwrite(&uc0, 1, 1, fp) != 1) {
220 goto fails; /* id_length */
222 if (fwrite(&uc0, 1, 1, fp) != 1) {
223 goto fails; /* colour_map_type */
226 image_type = 2; /* Uncompressed. */
227 if (fwrite(&image_type, 1, 1, fp) != 1) {
232 if (fwrite(&us0, 2, 1, fp) != 1) {
233 goto fails; /* colour_map_index */
235 if (fwrite(&us0, 2, 1, fp) != 1) {
236 goto fails; /* colour_map_length */
238 if (fwrite(&uc0, 1, 1, fp) != 1) {
239 goto fails; /* colour_map_entry_size */
242 if (fwrite(&us0, 2, 1, fp) != 1) {
243 goto fails; /* x_origin */
245 if (fwrite(&us0, 2, 1, fp) != 1) {
246 goto fails; /* y_origin */
249 image_w = (unsigned short)width;
250 image_h = (unsigned short) height;
252 #ifndef OPJ_BIG_ENDIAN
253 if (fwrite(&image_w, 2, 1, fp) != 1) {
256 if (fwrite(&image_h, 2, 1, fp) != 1) {
260 image_w = swap16(image_w);
261 image_h = swap16(image_h);
262 if (fwrite(&image_w, 2, 1, fp) != 1) {
265 if (fwrite(&image_h, 2, 1, fp) != 1) {
270 if (fwrite(&pixel_depth, 1, 1, fp) != 1) {
274 image_desc = 8; /* 8 bits per component. */
279 if (fwrite(&image_desc, 1, 1, fp) != 1) {
286 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
290 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters)
294 unsigned int image_width, image_height, pixel_bit_depth;
297 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
299 OPJ_COLOR_SPACE color_space;
302 int subsampling_dx, subsampling_dy;
305 f = fopen(filename, "rb");
307 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
311 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height,
316 /* We currently only support 24 & 32 bit tga's ... */
317 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
321 /* initialize image components */
322 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
324 mono = (pixel_bit_depth == 8) ||
325 (pixel_bit_depth == 16); /* Mono with & without alpha. */
326 save_alpha = (pixel_bit_depth == 16) ||
327 (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
330 color_space = CLRSPC_GRAY;
331 numcomps = save_alpha ? 2 : 1;
333 numcomps = save_alpha ? 4 : 3;
334 color_space = CLRSPC_SRGB;
337 subsampling_dx = parameters->subsampling_dx;
338 subsampling_dy = parameters->subsampling_dy;
340 for (i = 0; i < numcomps; i++) {
341 cmptparm[i].prec = 8;
343 cmptparm[i].sgnd = 0;
344 cmptparm[i].dx = subsampling_dx;
345 cmptparm[i].dy = subsampling_dy;
346 cmptparm[i].w = image_width;
347 cmptparm[i].h = image_height;
350 /* create the image */
351 image = opj_image_create(numcomps, &cmptparm[0], color_space);
357 /* set image offset and reference grid */
358 image->x0 = parameters->image_offset_x0;
359 image->y0 = parameters->image_offset_y0;
360 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 +
361 (image_width - 1) * subsampling_dx + 1;
362 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 +
363 (image_height - 1) * subsampling_dy + 1;
366 for (y = 0; y < image_height; y++) {
370 index = (image_height - y - 1) * image_width;
372 index = y * image_width;
376 for (x = 0; x < image_width; x++) {
377 unsigned char r, g, b;
379 if (!fread(&b, 1, 1, f)) {
381 "\nError: fread return a number of element different from the expected.\n");
382 opj_image_destroy(image);
385 if (!fread(&g, 1, 1, f)) {
387 "\nError: fread return a number of element different from the expected.\n");
388 opj_image_destroy(image);
391 if (!fread(&r, 1, 1, f)) {
393 "\nError: fread return a number of element different from the expected.\n");
394 opj_image_destroy(image);
398 image->comps[0].data[index] = r;
399 image->comps[1].data[index] = g;
400 image->comps[2].data[index] = b;
403 } else if (numcomps == 4) {
404 for (x = 0; x < image_width; x++) {
405 unsigned char r, g, b, a;
406 if (!fread(&b, 1, 1, f)) {
408 "\nError: fread return a number of element different from the expected.\n");
409 opj_image_destroy(image);
412 if (!fread(&g, 1, 1, f)) {
414 "\nError: fread return a number of element different from the expected.\n");
415 opj_image_destroy(image);
418 if (!fread(&r, 1, 1, f)) {
420 "\nError: fread return a number of element different from the expected.\n");
421 opj_image_destroy(image);
424 if (!fread(&a, 1, 1, f)) {
426 "\nError: fread return a number of element different from the expected.\n");
427 opj_image_destroy(image);
431 image->comps[0].data[index] = r;
432 image->comps[1].data[index] = g;
433 image->comps[2].data[index] = b;
434 image->comps[3].data[index] = a;
438 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
444 int imagetotga(opj_image_t * image, const char *outfile)
446 int width, height, bpp, x, y;
447 opj_bool write_alpha;
448 int i, adjustR, adjustG = 0, adjustB = 0;
449 unsigned int alpha_channel;
456 fdest = fopen(outfile, "wb");
458 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
462 for (i = 0; i < image->numcomps - 1; i++) {
463 if ((image->comps[0].dx != image->comps[i + 1].dx)
464 || (image->comps[0].dy != image->comps[i + 1].dy)
465 || (image->comps[0].prec != image->comps[i + 1].prec)) {
467 "Unable to create a tga file with such J2K image charateristics.");
472 width = image->comps[0].w;
473 height = image->comps[0].h;
475 /* Mono with alpha, or RGB with alpha. */
476 write_alpha = (image->numcomps == 2) || (image->numcomps == 4);
478 /* Write TGA header */
479 bpp = write_alpha ? 32 : 24;
480 if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
484 alpha_channel = image->numcomps - 1;
486 scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
488 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
489 if (image->numcomps >= 3) {
490 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
491 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
494 for (y = 0; y < height; y++) {
495 unsigned int index = y * width;
497 for (x = 0; x < width; x++, index++) {
498 r = (float)(image->comps[0].data[index] + adjustR);
500 if (image->numcomps > 2) {
501 g = (float)(image->comps[1].data[index] + adjustG);
502 b = (float)(image->comps[2].data[index] + adjustB);
503 } else { /* Greyscale ... */
508 /* TGA format writes BGR ... */
509 value = (unsigned char)(b * scale);
510 res = fwrite(&value, 1, 1, fdest);
512 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
516 value = (unsigned char)(g * scale);
517 res = fwrite(&value, 1, 1, fdest);
519 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
523 value = (unsigned char)(r * scale);
524 res = fwrite(&value, 1, 1, fdest);
526 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
531 a = (float)(image->comps[alpha_channel].data[index]);
532 value = (unsigned char)(a * scale);
533 res = fwrite(&value, 1, 1, fdest);
535 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
545 /* -->> -->> -->> -->>
549 <<-- <<-- <<-- <<-- */
551 /* WORD defines a two byte word */
552 typedef unsigned short int WORD;
554 /* DWORD defines a four byte word */
555 typedef unsigned int DWORD;
558 WORD bfType; /* 'BM' for Bitmap (19776) */
559 DWORD bfSize; /* Size of the file */
560 WORD bfReserved1; /* Reserved : 0 */
561 WORD bfReserved2; /* Reserved : 0 */
562 DWORD bfOffBits; /* Offset */
563 } BITMAPFILEHEADER_t;
566 DWORD biSize; /* Size of the structure in bytes */
567 DWORD biWidth; /* Width of the image in pixels */
568 DWORD biHeight; /* Height of the image in pixels */
569 WORD biPlanes; /* 1 */
570 WORD biBitCount; /* Number of color bits by pixels */
571 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
572 DWORD biSizeImage; /* Size of the image in bytes */
573 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
574 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
575 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
576 DWORD biClrImportant; /* Number of important color (0: ALL) */
577 } BITMAPINFOHEADER_t;
579 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
581 int subsampling_dx = parameters->subsampling_dx;
582 int subsampling_dy = parameters->subsampling_dy;
584 int i, numcomps, w, h;
585 OPJ_COLOR_SPACE color_space;
586 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
587 opj_image_t * image = NULL;
590 BITMAPFILEHEADER_t File_h;
591 BITMAPINFOHEADER_t Info_h;
593 unsigned char *table_R, *table_G, *table_B;
594 unsigned int j, PAD = 0;
601 IN = fopen(filename, "rb");
603 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
607 File_h.bfType = getc(IN);
608 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
610 if (File_h.bfType != 19778) {
611 fprintf(stderr, "Error, not a BMP file!\n");
617 File_h.bfSize = getc(IN);
618 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
619 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
620 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
622 File_h.bfReserved1 = getc(IN);
623 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
625 File_h.bfReserved2 = getc(IN);
626 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
628 File_h.bfOffBits = getc(IN);
629 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
630 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
631 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
636 Info_h.biSize = getc(IN);
637 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
638 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
639 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
641 if (Info_h.biSize != 40) {
642 fprintf(stderr, "Error, unknown BMP header size %d\n", Info_h.biSize);
646 Info_h.biWidth = getc(IN);
647 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
648 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
649 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
652 Info_h.biHeight = getc(IN);
653 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
654 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
655 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
658 Info_h.biPlanes = getc(IN);
659 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
661 Info_h.biBitCount = getc(IN);
662 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
664 Info_h.biCompression = getc(IN);
665 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
666 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
667 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
669 Info_h.biSizeImage = getc(IN);
670 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
671 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
672 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
674 Info_h.biXpelsPerMeter = getc(IN);
675 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
676 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
677 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
679 Info_h.biYpelsPerMeter = getc(IN);
680 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
681 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
682 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
684 Info_h.biClrUsed = getc(IN);
685 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
686 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
687 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
689 Info_h.biClrImportant = getc(IN);
690 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
691 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
692 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
694 /* Read the data and store them in the OUT file */
696 if (Info_h.biBitCount == 24) {
698 color_space = CLRSPC_SRGB;
699 /* initialize image components */
700 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
701 for (i = 0; i < numcomps; i++) {
702 cmptparm[i].prec = 8;
704 cmptparm[i].sgnd = 0;
705 cmptparm[i].dx = subsampling_dx;
706 cmptparm[i].dy = subsampling_dy;
710 /* create the image */
711 image = opj_image_create(numcomps, &cmptparm[0], color_space);
717 /* set image offset and reference grid */
718 image->x0 = parameters->image_offset_x0;
719 image->y0 = parameters->image_offset_y0;
720 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
721 (w - 1) * subsampling_dx + 1;
722 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
723 (h - 1) * subsampling_dy + 1;
727 /* Place the cursor at the beginning of the image information */
728 fseek(IN, 0, SEEK_SET);
729 fseek(IN, File_h.bfOffBits, SEEK_SET);
734 /* PAD = 4 - (3 * W) % 4; */
735 /* PAD = (PAD == 4) ? 0 : PAD; */
736 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
738 RGB = (unsigned char *)
739 malloc((3 * W + PAD) * H * sizeof(unsigned char));
741 if (fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H,
742 IN) != (3 * W + PAD) * H) {
745 opj_image_destroy(image);
747 "\nError: fread return a number of element different from the expected.\n");
753 for (y = 0; y < (int)H; y++) {
754 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
755 for (x = 0; x < (int)W; x++) {
756 unsigned char *pixel = &scanline[3 * x];
757 image->comps[0].data[index] = pixel[2]; /* R */
758 image->comps[1].data[index] = pixel[1]; /* G */
759 image->comps[2].data[index] = pixel[0]; /* B */
764 }/* if (Info_h.biBitCount == 24) */
765 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { /*RGB */
766 if (Info_h.biClrUsed == 0) {
767 Info_h.biClrUsed = 256;
768 } else if (Info_h.biClrUsed > 256) {
769 Info_h.biClrUsed = 256;
772 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
773 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
774 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
777 for (j = 0; j < Info_h.biClrUsed; j++) {
778 table_B[j] = (unsigned char)getc(IN);
779 table_G[j] = (unsigned char)getc(IN);
780 table_R[j] = (unsigned char)getc(IN);
783 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
789 /* Place the cursor at the beginning of the image information */
790 fseek(IN, 0, SEEK_SET);
791 fseek(IN, File_h.bfOffBits, SEEK_SET);
795 if (Info_h.biWidth % 2) {
799 numcomps = gray_scale ? 1 : 3;
800 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
801 /* initialize image components */
802 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
803 for (i = 0; i < numcomps; i++) {
804 cmptparm[i].prec = 8;
806 cmptparm[i].sgnd = 0;
807 cmptparm[i].dx = subsampling_dx;
808 cmptparm[i].dy = subsampling_dy;
812 /* create the image */
813 image = opj_image_create(numcomps, &cmptparm[0], color_space);
822 /* set image offset and reference grid */
823 image->x0 = parameters->image_offset_x0;
824 image->y0 = parameters->image_offset_y0;
825 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 +
826 (w - 1) * subsampling_dx + 1;
827 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 +
828 (h - 1) * subsampling_dy + 1;
832 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
834 if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) {
839 opj_image_destroy(image);
841 "\nError: fread return a number of element different from the expected.\n");
846 for (j = 0; j < W * H; j++) {
847 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
848 image->comps[0].data[index] =
849 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
856 for (j = 0; j < W * H; j++) {
857 if ((j % W < W - 1 && Info_h.biWidth % 2)
858 || !(Info_h.biWidth % 2)) {
859 unsigned char pixel_index =
860 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
861 image->comps[0].data[index] = table_R[pixel_index];
862 image->comps[1].data[index] = table_G[pixel_index];
863 image->comps[2].data[index] = table_B[pixel_index];
873 else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/
874 unsigned char *pix, *beyond;
875 int *gray, *red, *green, *blue;
876 unsigned int x, y, max;
880 if (Info_h.biClrUsed == 0) {
881 Info_h.biClrUsed = 256;
882 } else if (Info_h.biClrUsed > 256) {
883 Info_h.biClrUsed = 256;
886 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
887 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
888 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
891 for (j = 0; j < Info_h.biClrUsed; j++) {
892 table_B[j] = (unsigned char)getc(IN);
893 table_G[j] = (unsigned char)getc(IN);
894 table_R[j] = (unsigned char)getc(IN);
896 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
903 numcomps = gray_scale ? 1 : 3;
904 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
905 /* initialize image components */
906 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
907 for (i = 0; i < numcomps; i++) {
908 cmptparm[i].prec = 8;
910 cmptparm[i].sgnd = 0;
911 cmptparm[i].dx = subsampling_dx;
912 cmptparm[i].dy = subsampling_dy;
916 /* create the image */
917 image = opj_image_create(numcomps, &cmptparm[0], color_space);
926 /* set image offset and reference grid */
927 image->x0 = parameters->image_offset_x0;
928 image->y0 = parameters->image_offset_y0;
929 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
930 - 1) * subsampling_dx + 1;
931 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
932 - 1) * subsampling_dy + 1;
936 /* Place the cursor at the beginning of the image information */
937 fseek(IN, 0, SEEK_SET);
938 fseek(IN, File_h.bfOffBits, SEEK_SET);
942 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
943 beyond = RGB + W * H;
953 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++) {
954 *pix = (unsigned char)c1;
959 if (c == 0x00) { /* EOL */
962 pix = RGB + x + (H - y - 1) * W;
963 } else if (c == 0x01) { /* EOP */
965 } else if (c == 0x02) { /* MOVE by dxdy */
970 pix = RGB + (H - y - 1) * W + x;
971 } else { /* 03 .. 255 */
973 for (; i < c && x < W && pix < beyond; i++, x++, pix++) {
975 *pix = (unsigned char)c1;
977 if (c & 1) { /* skip padding byte */
985 gray = image->comps[0].data;
992 *gray++ = table_R[uc];
995 /*int *red, *green, *blue;*/
997 red = image->comps[0].data;
998 green = image->comps[1].data;
999 blue = image->comps[2].data;
1006 *red++ = table_R[uc];
1007 *green++ = table_G[uc];
1008 *blue++ = table_B[uc];
1018 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1019 "is not yet implemented [%d]\n", Info_h.biBitCount);
1025 int imagetobmp(opj_image_t * image, const char *outfile)
1030 int adjustR, adjustG, adjustB;
1032 if (image->comps[0].prec < 8) {
1033 fprintf(stderr, "Unsupported precision: %d\n", image->comps[0].prec);
1036 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1037 && image->comps[1].dx == image->comps[2].dx
1038 && image->comps[0].dy == image->comps[1].dy
1039 && image->comps[1].dy == image->comps[2].dy
1040 && image->comps[0].prec == image->comps[1].prec
1041 && image->comps[1].prec == image->comps[2].prec) {
1043 /* -->> -->> -->> -->>
1045 <<-- <<-- <<-- <<-- */
1047 fdest = fopen(outfile, "wb");
1049 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1053 w = image->comps[0].w;
1054 h = image->comps[0].h;
1056 fprintf(fdest, "BM");
1060 fprintf(fdest, "%c%c%c%c",
1061 (unsigned char)(h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1062 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1063 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1064 (unsigned char)((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1065 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1066 ((0) >> 24) & 0xff);
1067 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff,
1068 ((54) >> 24) & 0xff);
1072 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1073 ((40) >> 24) & 0xff);
1074 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1075 (unsigned char)((w) >> 8) & 0xff,
1076 (unsigned char)((w) >> 16) & 0xff,
1077 (unsigned char)((w) >> 24) & 0xff);
1078 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1079 (unsigned char)((h) >> 8) & 0xff,
1080 (unsigned char)((h) >> 16) & 0xff,
1081 (unsigned char)((h) >> 24) & 0xff);
1082 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1083 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1084 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1085 ((0) >> 24) & 0xff);
1086 fprintf(fdest, "%c%c%c%c", (unsigned char)(3 * h * w + 3 * h * (w % 2)) & 0xff,
1087 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1088 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1089 (unsigned char)((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1090 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1091 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1092 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1093 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1094 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1095 ((0) >> 24) & 0xff);
1096 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1097 ((0) >> 24) & 0xff);
1099 if (image->comps[0].prec > 8) {
1100 adjustR = image->comps[0].prec - 8;
1101 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1102 image->comps[0].prec);
1106 if (image->comps[1].prec > 8) {
1107 adjustG = image->comps[1].prec - 8;
1108 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n",
1109 image->comps[1].prec);
1113 if (image->comps[2].prec > 8) {
1114 adjustB = image->comps[2].prec - 8;
1115 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n",
1116 image->comps[2].prec);
1121 for (i = 0; i < w * h; i++) {
1122 unsigned char rc, gc, bc;
1125 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1126 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1127 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1133 rc = (unsigned char)r;
1135 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1136 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1137 g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
1143 gc = (unsigned char)g;
1145 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1146 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1147 b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
1153 bc = (unsigned char)b;
1155 fprintf(fdest, "%c%c%c", bc, gc, rc);
1157 if ((i + 1) % w == 0) {
1158 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) { /* ADD */
1159 fprintf(fdest, "%c", 0);
1164 } else { /* Gray-scale */
1166 /* -->> -->> -->> -->>
1167 8 bits non code (Gray scale)
1168 <<-- <<-- <<-- <<-- */
1170 fdest = fopen(outfile, "wb");
1171 w = image->comps[0].w;
1172 h = image->comps[0].h;
1174 fprintf(fdest, "BM");
1178 fprintf(fdest, "%c%c%c%c",
1179 (unsigned char)(h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1180 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1181 (unsigned char)((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1182 (unsigned char)((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1183 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1184 ((0) >> 24) & 0xff);
1185 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1186 ((54 + 1024) >> 16) & 0xff,
1187 ((54 + 1024) >> 24) & 0xff);
1191 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
1192 ((40) >> 24) & 0xff);
1193 fprintf(fdest, "%c%c%c%c", (unsigned char)((w) & 0xff),
1194 (unsigned char)((w) >> 8) & 0xff,
1195 (unsigned char)((w) >> 16) & 0xff,
1196 (unsigned char)((w) >> 24) & 0xff);
1197 fprintf(fdest, "%c%c%c%c", (unsigned char)((h) & 0xff),
1198 (unsigned char)((h) >> 8) & 0xff,
1199 (unsigned char)((h) >> 16) & 0xff,
1200 (unsigned char)((h) >> 24) & 0xff);
1201 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1202 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1203 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1204 ((0) >> 24) & 0xff);
1205 fprintf(fdest, "%c%c%c%c", (unsigned char)(h * w + h * (w % 2)) & 0xff,
1206 (unsigned char)((h * w + h * (w % 2)) >> 8) & 0xff,
1207 (unsigned char)((h * w + h * (w % 2)) >> 16) & 0xff,
1208 (unsigned char)((h * w + h * (w % 2)) >> 24) & 0xff);
1209 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1210 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1211 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1212 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1213 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1214 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1215 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1216 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1218 if (image->comps[0].prec > 8) {
1219 adjustR = image->comps[0].prec - 8;
1220 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1221 image->comps[0].prec);
1226 for (i = 0; i < 256; i++) {
1227 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1230 for (i = 0; i < w * h; i++) {
1233 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1234 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1235 r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1242 fprintf(fdest, "%c", (unsigned char)r);
1244 if ((i + 1) % w == 0) {
1245 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) { /* ADD */
1246 fprintf(fdest, "%c", 0);
1256 /* -->> -->> -->> -->>
1260 <<-- <<-- <<-- <<-- */
1263 static unsigned char readuchar(FILE * f)
1266 if (!fread(&c1, 1, 1, f)) {
1268 "\nError: fread return a number of element different from the expected.\n");
1274 static unsigned short readushort(FILE * f, int bigendian)
1276 unsigned char c1, c2;
1277 if (!fread(&c1, 1, 1, f)) {
1279 "\nError: fread return a number of element different from the expected.\n");
1282 if (!fread(&c2, 1, 1, f)) {
1284 "\nError: fread return a number of element different from the expected.\n");
1288 return (c1 << 8) + c2;
1290 return (c2 << 8) + c1;
1294 static unsigned int readuint(FILE * f, int bigendian)
1296 unsigned char c1, c2, c3, c4;
1297 if (!fread(&c1, 1, 1, f)) {
1299 "\nError: fread return a number of element different from the expected.\n");
1302 if (!fread(&c2, 1, 1, f)) {
1304 "\nError: fread return a number of element different from the expected.\n");
1307 if (!fread(&c3, 1, 1, f)) {
1309 "\nError: fread return a number of element different from the expected.\n");
1312 if (!fread(&c4, 1, 1, f)) {
1314 "\nError: fread return a number of element different from the expected.\n");
1318 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1320 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1324 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
1328 int i, numcomps, max;
1329 OPJ_COLOR_SPACE color_space;
1330 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1331 opj_image_t * image = NULL;
1332 int adjustS, ushift, dshift, force8;
1334 char endian1, endian2, sign;
1339 opj_image_comp_t *comp = NULL;
1342 color_space = CLRSPC_GRAY;
1344 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1348 f = fopen(filename, "rb");
1350 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1354 fseek(f, 0, SEEK_SET);
1355 if (fscanf(f, "PG%31[ \t]%c%c%31[ \t+-]%d%31[ \t]%d%31[ \t]%d", temp, &endian1,
1356 &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
1358 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1365 while (signtmp[i] != '\0') {
1366 if (signtmp[i] == '-') {
1373 if (endian1 == 'M' && endian2 == 'L') {
1375 } else if (endian2 == 'M' && endian1 == 'L') {
1378 fprintf(stderr, "Bad pgx header, please check input file\n");
1383 /* initialize image component */
1385 cmptparm.x0 = parameters->image_offset_x0;
1386 cmptparm.y0 = parameters->image_offset_y0;
1387 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
1388 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1389 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
1390 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1400 dshift = prec - ushift;
1401 if (cmptparm.sgnd) {
1402 adjustS = (1 << (prec - 1));
1409 ushift = dshift = force8 = adjustS = 0;
1412 cmptparm.prec = prec;
1413 cmptparm.bpp = prec;
1414 cmptparm.dx = parameters->subsampling_dx;
1415 cmptparm.dy = parameters->subsampling_dy;
1417 /* create the image */
1418 image = opj_image_create(numcomps, &cmptparm, color_space);
1423 /* set image offset and reference grid */
1424 image->x0 = cmptparm.x0;
1425 image->y0 = cmptparm.x0;
1426 image->x1 = cmptparm.w;
1427 image->y1 = cmptparm.h;
1429 /* set image data */
1431 comp = &image->comps[0];
1433 for (i = 0; i < w * h; i++) {
1436 v = readuchar(f) + adjustS;
1437 v = (v << ushift) + (v >> dshift);
1438 comp->data[i] = (unsigned char)v;
1446 if (comp->prec == 8) {
1450 v = (char) readuchar(f);
1452 } else if (comp->prec <= 16) {
1454 v = readushort(f, bigendian);
1456 v = (short) readushort(f, bigendian);
1460 v = readuint(f, bigendian);
1462 v = (int) readuint(f, bigendian);
1471 comp->bpp = int_floorlog2(max) + 1;
1476 int imagetopgx(opj_image_t * image, const char *outfile)
1482 for (compno = 0; compno < image->numcomps; compno++) {
1483 opj_image_comp_t *comp = &image->comps[compno];
1484 char bname[256]; /* buffer for name */
1485 char *name = bname; /* pointer */
1488 const size_t olen = strlen(outfile);
1489 const size_t dotpos = olen - 4;
1490 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1491 if (outfile[dotpos] != '.') {
1492 /* `pgx` was recognized but there is no dot at expected position */
1493 fprintf(stderr, "ERROR -> Impossible happen.");
1497 name = (char*)malloc(total + 1);
1499 strncpy(name, outfile, dotpos);
1500 /*if (image->numcomps > 1) {*/
1501 sprintf(name + dotpos, "_%d.pgx", compno);
1503 strcpy(name+dotpos, ".pgx");
1505 fdest = fopen(name, "wb");
1507 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1510 /* don't need name anymore */
1515 w = image->comps[compno].w;
1516 h = image->comps[compno].h;
1518 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1519 if (comp->prec <= 8) {
1521 } else if (comp->prec <= 16) {
1526 for (i = 0; i < w * h; i++) {
1527 int v = image->comps[compno].data[i];
1528 for (j = nbytes - 1; j >= 0; j--) {
1529 char byte = (char)(v >> (j * 8));
1530 res = fwrite(&byte, 1, 1, fdest);
1532 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1544 /* -->> -->> -->> -->>
1548 <<-- <<-- <<-- <<-- */
1551 int width, height, maxval, depth, format;
1552 char rgb, rgba, gray, graya, bw;
1556 static char *skip_white(char *s)
1559 if (*s == '\n' || *s == '\r') {
1571 static char *skip_int(char *start, int *out_n)
1579 s = skip_white(start);
1593 *out_n = atoi(start);
1598 static char *skip_idf(char *start, char out_idf[256])
1603 s = skip_white(start);
1610 if (isalpha(*s) || *s == '_') {
1618 strncpy(out_idf, start, 255);
1623 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1626 int format, have_wh, end, ttype;
1627 char idf[256], type[256];
1630 if (fgets(line, 250, reader) == NULL) {
1631 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1635 if (line[0] != 'P') {
1636 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1639 format = atoi(line + 1);
1640 if (format < 1 || format > 7) {
1641 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1644 ph->format = format;
1645 ttype = end = have_wh = 0;
1647 while (fgets(line, 250, reader)) {
1655 s = skip_idf(s, idf);
1657 if (s == NULL || *s == 0) {
1661 if (strcmp(idf, "ENDHDR") == 0) {
1665 if (strcmp(idf, "WIDTH") == 0) {
1666 s = skip_int(s, &ph->width);
1667 if (s == NULL || *s == 0) {
1673 if (strcmp(idf, "HEIGHT") == 0) {
1674 s = skip_int(s, &ph->height);
1675 if (s == NULL || *s == 0) {
1681 if (strcmp(idf, "DEPTH") == 0) {
1682 s = skip_int(s, &ph->depth);
1683 if (s == NULL || *s == 0) {
1689 if (strcmp(idf, "MAXVAL") == 0) {
1690 s = skip_int(s, &ph->maxval);
1691 if (s == NULL || *s == 0) {
1697 if (strcmp(idf, "TUPLTYPE") == 0) {
1698 s = skip_idf(s, type);
1699 if (s == NULL || *s == 0) {
1703 if (strcmp(type, "BLACKANDWHITE") == 0) {
1708 if (strcmp(type, "GRAYSCALE") == 0) {
1713 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1718 if (strcmp(type, "RGB") == 0) {
1723 if (strcmp(type, "RGB_ALPHA") == 0) {
1728 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1731 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1733 } /* if(format == 7) */
1736 s = skip_int(s, &ph->width);
1738 s = skip_int(s, &ph->height);
1742 if (format == 1 || format == 4) {
1748 if (format == 2 || format == 3 || format == 5 || format == 6) {
1749 /* P2, P3, P5, P6: */
1750 s = skip_int(s, &ph->maxval);
1752 if (ph->maxval > 65535) {
1757 }/* while(fgets( ) */
1758 if (format == 2 || format == 3 || format > 4) {
1759 if (ph->maxval < 1 || ph->maxval > 65535) {
1763 if (ph->width < 1 || ph->height < 1) {
1769 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1772 if (ph->depth < 1 || ph->depth > 4) {
1776 if (ph->width && ph->height && ph->depth & ph->maxval && ttype) {
1780 if (format != 1 && format != 4) {
1781 if (ph->width && ph->height && ph->maxval) {
1785 if (ph->width && ph->height) {
1793 static int has_prec(int val)
1843 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1845 int subsampling_dx = parameters->subsampling_dx;
1846 int subsampling_dy = parameters->subsampling_dy;
1849 int i, compno, numcomps, w, h, prec, format;
1850 OPJ_COLOR_SPACE color_space;
1851 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1852 opj_image_t * image = NULL;
1853 struct pnm_header header_info;
1855 if ((fp = fopen(filename, "rb")) == NULL) {
1856 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1859 memset(&header_info, 0, sizeof(struct pnm_header));
1861 read_pnm_header(fp, &header_info);
1863 if (!header_info.ok) {
1868 /* This limitation could be removed by making sure to use size_t below */
1869 if (header_info.height != 0 &&
1870 header_info.width > INT_MAX / header_info.height) {
1871 fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n",
1872 header_info.width, header_info.height);
1877 format = header_info.format;
1880 case 1: /* ascii bitmap */
1881 case 4: /* raw bitmap */
1885 case 2: /* ascii greymap */
1886 case 5: /* raw greymap */
1890 case 3: /* ascii pixmap */
1891 case 6: /* raw pixmap */
1895 case 7: /* arbitrary map */
1896 numcomps = header_info.depth;
1904 color_space = CLRSPC_GRAY; /* GRAY, GRAYA */
1906 color_space = CLRSPC_SRGB; /* RGB, RGBA */
1909 prec = has_prec(header_info.maxval);
1915 w = header_info.width;
1916 h = header_info.height;
1917 subsampling_dx = parameters->subsampling_dx;
1918 subsampling_dy = parameters->subsampling_dy;
1920 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1922 for (i = 0; i < numcomps; i++) {
1923 cmptparm[i].prec = prec;
1924 cmptparm[i].bpp = prec;
1925 cmptparm[i].sgnd = 0;
1926 cmptparm[i].dx = subsampling_dx;
1927 cmptparm[i].dy = subsampling_dy;
1931 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1938 /* set image offset and reference grid */
1939 image->x0 = parameters->image_offset_x0;
1940 image->y0 = parameters->image_offset_y0;
1941 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1942 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1944 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1947 for (i = 0; i < w * h; i++) {
1948 for (compno = 0; compno < numcomps; compno++) {
1950 if (fscanf(fp, "%u", &index) != 1) {
1952 "\nWARNING: fscanf return a number of element different from the expected.\n");
1955 image->comps[compno].data[i] = (index * 255) / header_info.maxval;
1958 } else if ((format == 5)
1961 && (header_info.gray || header_info.graya
1962 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1963 unsigned char c0, c1, one;
1967 for (i = 0; i < w * h; i++) {
1968 for (compno = 0; compno < numcomps; compno++) {
1969 if (!fread(&c0, 1, 1, fp)) {
1971 "\nError: fread return a number of element different from the expected.\n");
1974 image->comps[compno].data[i] = c0;
1976 if (!fread(&c1, 1, 1, fp)) {
1978 "\nError: fread return a number of element different from the expected.\n");
1981 image->comps[compno].data[i] = ((c0 << 8) | c1);
1985 } else if (format == 1) { /* ascii bitmap */
1986 for (i = 0; i < w * h; i++) {
1989 if (fscanf(fp, "%u", &index) != 1) {
1991 "\nWARNING: fscanf return a number of element different from the expected.\n");
1994 image->comps[0].data[i] = (index ? 0 : 255);
1996 } else if (format == 4) {
2001 for (y = 0; y < h; ++y) {
2005 for (x = 0; x < w; ++x) {
2008 uc = (unsigned char)getc(fp);
2010 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
2015 } else if ((format == 7 && header_info.bw)) { /*MONO*/
2018 for (i = 0; i < w * h; ++i) {
2019 if (!fread(&uc, 1, 1, fp)) {
2021 "\nError: fread return a number of element different from the expected.\n");
2023 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
2031 int imagetopnm(opj_image_t * image, const char *outfile)
2033 int *red, *green, *blue, *alpha;
2035 int i, compno, ncomp;
2036 int adjustR, adjustG, adjustB, adjustA;
2037 int fails, two, want_gray, has_alpha, triple;
2040 const char *tmp = outfile;
2043 if ((prec = image->comps[0].prec) > 16) {
2044 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
2045 "\n\t: refused.\n", __FILE__, __LINE__, prec);
2048 two = has_alpha = 0;
2050 ncomp = image->numcomps;
2056 want_gray = (*tmp == 'g' || *tmp == 'G');
2057 ncomp = image->numcomps;
2063 if (ncomp == 2 /* GRAYA */
2064 || (ncomp > 2 /* RGB, RGBA */
2065 && image->comps[0].dx == image->comps[1].dx
2066 && image->comps[1].dx == image->comps[2].dx
2067 && image->comps[0].dy == image->comps[1].dy
2068 && image->comps[1].dy == image->comps[2].dy
2069 && image->comps[0].prec == image->comps[1].prec
2070 && image->comps[1].prec == image->comps[2].prec
2072 fdest = fopen(outfile, "wb");
2075 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
2079 triple = (ncomp > 2);
2080 wr = image->comps[0].w;
2081 hr = image->comps[0].h;
2082 max = (1 << prec) - 1;
2083 has_alpha = (ncomp == 4 || ncomp == 2);
2085 red = image->comps[0].data;
2088 green = image->comps[1].data;
2089 blue = image->comps[2].data;
2091 green = blue = NULL;
2095 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
2097 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
2098 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2099 wr, hr, ncomp, max, tt);
2100 alpha = image->comps[ncomp - 1].data;
2101 adjustA = (image->comps[ncomp - 1].sgnd ?
2102 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2104 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2105 opj_version(), wr, hr, max);
2108 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2111 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2112 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2114 adjustG = adjustB = 0;
2117 for (i = 0; i < wr * hr; ++i) {
2122 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2125 v = *green + adjustG;
2128 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2130 v = *blue + adjustB;
2133 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2138 v = *alpha + adjustA;
2141 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2149 fprintf(fdest, "%c", (unsigned char)*red++);
2151 fprintf(fdest, "%c%c", (unsigned char)*green++, (unsigned char)*blue++);
2155 fprintf(fdest, "%c", (unsigned char)*alpha++);
2166 if (image->numcomps > ncomp) {
2167 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2168 fprintf(stderr, " is written to the file\n");
2170 destname = (char*)malloc(strlen(outfile) + 8);
2172 for (compno = 0; compno < ncomp; compno++) {
2174 sprintf(destname, "%d.%s", compno, outfile);
2176 sprintf(destname, "%s", outfile);
2179 fdest = fopen(destname, "wb");
2181 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2185 wr = image->comps[compno].w;
2186 hr = image->comps[compno].h;
2187 prec = image->comps[compno].prec;
2188 max = (1 << prec) - 1;
2190 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2191 opj_version(), wr, hr, max);
2193 red = image->comps[compno].data;
2195 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2198 for (i = 0; i < wr * hr; i++) {
2202 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2207 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2210 } else { /* prec <= 8 */
2211 for (i = 0; i < wr * hr; ++i) {
2212 fprintf(fdest, "%c", (unsigned char)(*red + adjustR));
2223 #ifdef OPJ_HAVE_LIBTIFF
2224 /* -->> -->> -->> -->>
2228 <<-- <<-- <<-- <<-- */
2230 int imagetotif(opj_image_t * image, const char *outfile)
2232 int width, height, imgsize;
2233 int bps, index, adjust, sgnd;
2234 int ushift, dshift, has_alpha, force16;
2240 ushift = dshift = force16 = has_alpha = 0;
2241 bps = image->comps[0].prec;
2243 if (bps > 8 && bps < 16) {
2245 dshift = bps - ushift;
2250 if (bps != 8 && bps != 16) {
2251 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2253 fprintf(stderr, "\tAborting\n");
2256 tif = TIFFOpen(outfile, "wb");
2259 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2262 sgnd = image->comps[0].sgnd;
2263 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2265 if (image->numcomps >= 3
2266 && image->comps[0].dx == image->comps[1].dx
2267 && image->comps[1].dx == image->comps[2].dx
2268 && image->comps[0].dy == image->comps[1].dy
2269 && image->comps[1].dy == image->comps[2].dy
2270 && image->comps[0].prec == image->comps[1].prec
2271 && image->comps[1].prec == image->comps[2].prec) {
2272 has_alpha = (image->numcomps == 4);
2274 width = image->comps[0].w;
2275 height = image->comps[0].h;
2276 imgsize = width * height ;
2278 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2279 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2280 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2281 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2282 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2283 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2284 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2285 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2286 strip_size = TIFFStripSize(tif);
2287 buf = _TIFFmalloc(strip_size);
2290 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2291 unsigned char *dat8;
2292 tsize_t i, ssize, last_i = 0;
2294 ssize = TIFFStripSize(tif);
2295 dat8 = (unsigned char*)buf;
2298 step = 3 + has_alpha;
2301 for (i = 0; i < ssize - restx; i += step) {
2304 if (index < imgsize) {
2305 r = image->comps[0].data[index];
2306 g = image->comps[1].data[index];
2307 b = image->comps[2].data[index];
2309 a = image->comps[3].data[index];
2334 if (last_i < ssize) {
2335 for (i = last_i; i < ssize; i += step) {
2338 if (index < imgsize) {
2339 r = image->comps[0].data[index];
2340 g = image->comps[1].data[index];
2341 b = image->comps[2].data[index];
2343 a = image->comps[3].data[index];
2355 if (i + 1 < ssize) {
2360 if (i + 2 < ssize) {
2366 if (i + 3 < ssize) {
2377 }/*if(last_i < ssize)*/
2380 else if (bps == 16) {
2381 step = 6 + has_alpha + has_alpha;
2384 for (i = 0; i < ssize - restx ; i += step) {
2387 if (index < imgsize) {
2388 r = image->comps[0].data[index];
2389 g = image->comps[1].data[index];
2390 b = image->comps[2].data[index];
2392 a = image->comps[3].data[index];
2404 r = (r << ushift) + (r >> dshift);
2405 g = (g << ushift) + (g >> dshift);
2406 b = (b << ushift) + (b >> dshift);
2408 a = (a << ushift) + (a >> dshift);
2411 dat8[i + 0] = r; /*LSB*/
2412 dat8[i + 1] = (r >> 8); /*MSB*/
2414 dat8[i + 3] = (g >> 8);
2416 dat8[i + 5] = (b >> 8);
2419 dat8[i + 7] = (a >> 8);
2428 if (last_i < ssize) {
2429 for (i = last_i ; i < ssize ; i += step) {
2432 if (index < imgsize) {
2433 r = image->comps[0].data[index];
2434 g = image->comps[1].data[index];
2435 b = image->comps[2].data[index];
2437 a = image->comps[3].data[index];
2449 r = (r << ushift) + (r >> dshift);
2450 g = (g << ushift) + (g >> dshift);
2451 b = (b << ushift) + (b >> dshift);
2453 a = (a << ushift) + (a >> dshift);
2456 dat8[i + 0] = r; /*LSB*/
2457 if (i + 1 < ssize) {
2458 dat8[i + 1] = (r >> 8);
2462 if (i + 2 < ssize) {
2467 if (i + 3 < ssize) {
2468 dat8[i + 3] = (g >> 8);
2472 if (i + 4 < ssize) {
2477 if (i + 5 < ssize) {
2478 dat8[i + 5] = (b >> 8);
2484 if (i + 6 < ssize) {
2489 if (i + 7 < ssize) {
2490 dat8[i + 7] = (a >> 8);
2500 }/*if(last_i < ssize)*/
2503 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2504 }/*for(strip = 0; )*/
2506 _TIFFfree((void*)buf);
2512 if (image->numcomps == 1 /* GRAY */
2513 || (image->numcomps == 2 /* GRAY_ALPHA */
2514 && image->comps[0].dx == image->comps[1].dx
2515 && image->comps[0].dy == image->comps[1].dy
2516 && image->comps[0].prec == image->comps[1].prec)) {
2519 has_alpha = (image->numcomps == 2);
2521 width = image->comps[0].w;
2522 height = image->comps[0].h;
2523 imgsize = width * height;
2526 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2527 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2528 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2529 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2530 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2531 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2532 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2533 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2535 /* Get a buffer for the data */
2536 strip_size = TIFFStripSize(tif);
2537 buf = _TIFFmalloc(strip_size);
2540 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2541 unsigned char *dat8;
2542 tsize_t i, ssize = TIFFStripSize(tif);
2543 dat8 = (unsigned char*)buf;
2546 step = 1 + has_alpha;
2548 for (i = 0; i < ssize; i += step) {
2549 if (index < imgsize) {
2552 r = image->comps[0].data[index];
2554 a = image->comps[1].data[index];
2573 else if (bps == 16) {
2574 step = 2 + has_alpha + has_alpha;
2576 for (i = 0; i < ssize; i += step) {
2577 if (index < imgsize) {
2580 r = image->comps[0].data[index];
2582 a = image->comps[1].data[index];
2592 r = (r << ushift) + (r >> dshift);
2594 a = (a << ushift) + (a >> dshift);
2597 dat8[i + 0] = r; /*LSB*/
2598 dat8[i + 1] = r >> 8; /*MSB*/
2601 dat8[i + 3] = a >> 8;
2604 }/*if(index < imgsize)*/
2610 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2621 fprintf(stderr, "imagetotif: Bad color format.\n"
2622 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2623 fprintf(stderr, "\tFOUND: numcomps(%d)\n\tAborting\n",
2630 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2631 * CINEMA : 12 bit precision
2633 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2635 int subsampling_dx = parameters->subsampling_dx;
2636 int subsampling_dy = parameters->subsampling_dy;
2641 int j, numcomps, w, h, index;
2642 OPJ_COLOR_SPACE color_space;
2643 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2644 opj_image_t *image = NULL;
2647 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2648 unsigned int tiWidth, tiHeight;
2650 tif = TIFFOpen(filename, "r");
2653 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2656 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2657 tiWidth = tiHeight = 0;
2659 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2660 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2661 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2662 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2663 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2664 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2665 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2670 unsigned short b = tiBps, p = tiPhoto;
2672 if (tiBps != 8 && tiBps != 16 && tiBps != 12) {
2675 if (tiPhoto != 1 && tiPhoto != 2) {
2681 fprintf(stderr, "imagetotif: Bits=%d, Only 8 and 16 bits"
2682 " implemented\n", tiBps);
2684 fprintf(stderr, "tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2685 " and GRAY(A) has been implemented\n", (int) tiPhoto);
2687 fprintf(stderr, "\tAborting\n");
2693 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2695 uint16 extrasamples;
2697 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2698 &extrasamples, &sampleinfo);
2700 if (extrasamples >= 1) {
2701 switch (sampleinfo[0]) {
2702 case EXTRASAMPLE_UNSPECIFIED:
2703 /* Workaround for some images without correct info about alpha channel
2710 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2711 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2715 } else /* extrasamples == 0 */
2716 if (tiSpp == 4 || tiSpp == 2) {
2721 /* initialize image components
2723 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2725 if (tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */
2726 numcomps = 3 + has_alpha;
2727 color_space = CLRSPC_SRGB;
2729 for (j = 0; j < numcomps; j++) {
2730 if (parameters->cp_cinema) {
2731 cmptparm[j].prec = 12;
2732 cmptparm[j].bpp = 12;
2734 cmptparm[j].prec = tiBps;
2735 cmptparm[j].bpp = tiBps;
2737 cmptparm[j].dx = subsampling_dx;
2738 cmptparm[j].dy = subsampling_dy;
2743 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2749 /* set image offset and reference grid
2751 image->x0 = parameters->image_offset_x0;
2752 image->y0 = parameters->image_offset_y0;
2753 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2754 image->x0 + (w - 1) * subsampling_dx + 1;
2755 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2756 image->y0 + (h - 1) * subsampling_dy + 1;
2758 buf = _TIFFmalloc(TIFFStripSize(tif));
2760 strip_size = TIFFStripSize(tif);
2762 imgsize = image->comps[0].w * image->comps[0].h ;
2763 /* Read the Image components
2765 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2766 unsigned char *dat8;
2769 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2770 dat8 = (unsigned char*)buf;
2773 step = 6 + has_alpha + has_alpha;
2775 for (i = 0; i < ssize; i += step) {
2776 if (index < imgsize) {
2777 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0]; /* R */
2778 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2]; /* G */
2779 image->comps[2].data[index] = (dat8[i + 5] << 8) | dat8[i + 4]; /* B */
2781 image->comps[3].data[index] = (dat8[i + 7] << 8) | dat8[i + 6];
2784 if (parameters->cp_cinema) {
2785 /* Rounding 16 to 12 bits
2787 image->comps[0].data[index] =
2788 (image->comps[0].data[index] + 0x08) >> 4 ;
2789 image->comps[1].data[index] =
2790 (image->comps[1].data[index] + 0x08) >> 4 ;
2791 image->comps[2].data[index] =
2792 (image->comps[2].data[index] + 0x08) >> 4 ;
2794 image->comps[3].data[index] =
2795 (image->comps[3].data[index] + 0x08) >> 4 ;
2802 }/*if(tiBps == 16)*/
2803 else if (tiBps == 8) {
2804 step = 3 + has_alpha;
2806 for (i = 0; i < ssize; i += step) {
2807 if (index < imgsize) {
2808 image->comps[0].data[index] = dat8[i + 0]; /* R */
2809 image->comps[1].data[index] = dat8[i + 1]; /* G */
2810 image->comps[2].data[index] = dat8[i + 2]; /* B */
2812 image->comps[3].data[index] = dat8[i + 3];
2815 if (parameters->cp_cinema) {
2816 /* Rounding 8 to 12 bits
2818 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2819 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2820 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2822 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2831 }/*if( tiBps == 8)*/
2832 else if (tiBps == 12) { /* CINEMA file */
2835 for (i = 0; i < ssize; i += step) {
2836 if ((index < imgsize) & (index + 1 < imgsize)) {
2837 image->comps[0].data[index] = (dat8[i + 0] << 4) | (dat8[i + 1] >> 4);
2838 image->comps[1].data[index] = ((dat8[i + 1] & 0x0f) << 8) | dat8[i + 2];
2840 image->comps[2].data[index] = (dat8[i + 3] << 4) | (dat8[i + 4] >> 4);
2841 image->comps[0].data[index + 1] = ((dat8[i + 4] & 0x0f) << 8) | dat8[i + 5];
2843 image->comps[1].data[index + 1] = (dat8[i + 6] << 4) |
2845 image->comps[2].data[index + 1] = ((dat8[i + 7] & 0x0f) << 8) | dat8[i + 8];
2853 }/*for(strip = 0; )*/
2861 if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */
2862 numcomps = 1 + has_alpha;
2863 color_space = CLRSPC_GRAY;
2865 for (j = 0; j < numcomps; ++j) {
2866 cmptparm[j].prec = tiBps;
2867 cmptparm[j].bpp = tiBps;
2868 cmptparm[j].dx = subsampling_dx;
2869 cmptparm[j].dy = subsampling_dy;
2873 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2879 /* set image offset and reference grid
2881 image->x0 = parameters->image_offset_x0;
2882 image->y0 = parameters->image_offset_y0;
2883 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2884 image->x0 + (w - 1) * subsampling_dx + 1;
2885 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2886 image->y0 + (h - 1) * subsampling_dy + 1;
2888 buf = _TIFFmalloc(TIFFStripSize(tif));
2890 strip_size = TIFFStripSize(tif);
2892 imgsize = image->comps[0].w * image->comps[0].h ;
2893 /* Read the Image components
2895 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
2896 unsigned char *dat8;
2900 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2901 dat8 = (unsigned char*)buf;
2904 step = 2 + has_alpha + has_alpha;
2906 for (i = 0; i < ssize; i += step) {
2907 if (index < imgsize) {
2908 image->comps[0].data[index] = (dat8[i + 1] << 8) | dat8[i + 0];
2910 image->comps[1].data[index] = (dat8[i + 3] << 8) | dat8[i + 2];
2917 } else if (tiBps == 8) {
2918 step = 1 + has_alpha;
2920 for (i = 0; i < ssize; i += step) {
2921 if (index < imgsize) {
2922 image->comps[0].data[index] = dat8[i + 0];
2924 image->comps[1].data[index] = dat8[i + 1];
2943 #endif /* OPJ_HAVE_LIBTIFF */
2945 /* -->> -->> -->> -->>
2949 <<-- <<-- <<-- <<-- */
2951 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2952 raw_cparameters_t *raw_cp)
2954 int subsampling_dx = parameters->subsampling_dx;
2955 int subsampling_dy = parameters->subsampling_dy;
2958 int i, compno, numcomps, w, h;
2959 OPJ_COLOR_SPACE color_space;
2960 opj_image_cmptparm_t *cmptparm;
2961 opj_image_t * image = NULL;
2964 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2965 raw_cp->rawBitDepth)) == 0) {
2966 fprintf(stderr, "\nError: invalid raw image parameters\n");
2967 fprintf(stderr, "Please use the Format option -F:\n");
2969 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2970 fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2971 fprintf(stderr, "Aborting\n");
2975 f = fopen(filename, "rb");
2977 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2978 fprintf(stderr, "Aborting\n");
2981 numcomps = raw_cp->rawComp;
2982 color_space = CLRSPC_SRGB;
2983 w = raw_cp->rawWidth;
2984 h = raw_cp->rawHeight;
2985 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(
2986 opj_image_cmptparm_t));
2988 /* initialize image components */
2989 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2990 for (i = 0; i < numcomps; i++) {
2991 cmptparm[i].prec = raw_cp->rawBitDepth;
2992 cmptparm[i].bpp = raw_cp->rawBitDepth;
2993 cmptparm[i].sgnd = raw_cp->rawSigned;
2994 cmptparm[i].dx = subsampling_dx;
2995 cmptparm[i].dy = subsampling_dy;
2999 /* create the image */
3000 image = opj_image_create(numcomps, &cmptparm[0], color_space);
3005 /* set image offset and reference grid */
3006 image->x0 = parameters->image_offset_x0;
3007 image->y0 = parameters->image_offset_y0;
3008 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
3009 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
3011 if (raw_cp->rawBitDepth <= 8) {
3012 unsigned char value = 0;
3013 for (compno = 0; compno < numcomps; compno++) {
3014 for (i = 0; i < w * h; i++) {
3015 if (!fread(&value, 1, 1, f)) {
3016 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3020 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
3023 } else if (raw_cp->rawBitDepth <= 16) {
3024 unsigned short value;
3025 for (compno = 0; compno < numcomps; compno++) {
3026 for (i = 0; i < w * h; i++) {
3028 if (!fread(&temp, 1, 1, f)) {
3029 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3034 if (!fread(&temp, 1, 1, f)) {
3035 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
3040 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
3045 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3050 if (fread(&ch, 1, 1, f)) {
3051 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
3058 int imagetoraw(opj_image_t * image, const char *outfile)
3060 FILE *rawFile = NULL;
3067 if ((image->numcomps * image->x1 * image->y1) == 0) {
3068 fprintf(stderr, "\nError: invalid raw image parameters\n");
3072 rawFile = fopen(outfile, "wb");
3074 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3078 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
3080 for (compno = 0; compno < image->numcomps; compno++) {
3081 fprintf(stdout, "Component %d characteristics: %dx%dx%d %s\n", compno,
3082 image->comps[compno].w,
3083 image->comps[compno].h, image->comps[compno].prec,
3084 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
3086 w = image->comps[compno].w;
3087 h = image->comps[compno].h;
3089 if (image->comps[compno].prec <= 8) {
3090 if (image->comps[compno].sgnd == 1) {
3092 int mask = (1 << image->comps[compno].prec) - 1;
3093 ptr = image->comps[compno].data;
3094 for (line = 0; line < h; line++) {
3095 for (row = 0; row < w; row++) {
3096 curr = (signed char)(*ptr & mask);
3097 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
3099 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3105 } else if (image->comps[compno].sgnd == 0) {
3107 int mask = (1 << image->comps[compno].prec) - 1;
3108 ptr = image->comps[compno].data;
3109 for (line = 0; line < h; line++) {
3110 for (row = 0; row < w; row++) {
3111 curr = (unsigned char)(*ptr & mask);
3112 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
3114 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3121 } else if (image->comps[compno].prec <= 16) {
3122 if (image->comps[compno].sgnd == 1) {
3123 signed short int curr;
3124 int mask = (1 << image->comps[compno].prec) - 1;
3125 ptr = image->comps[compno].data;
3126 for (line = 0; line < h; line++) {
3127 for (row = 0; row < w; row++) {
3129 curr = (signed short int)(*ptr & mask);
3130 temp = (unsigned char)(curr >> 8);
3131 res = fwrite(&temp, 1, 1, rawFile);
3133 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3136 temp = (unsigned char) curr;
3137 res = fwrite(&temp, 1, 1, rawFile);
3139 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3145 } else if (image->comps[compno].sgnd == 0) {
3146 unsigned short int curr;
3147 int mask = (1 << image->comps[compno].prec) - 1;
3148 ptr = image->comps[compno].data;
3149 for (line = 0; line < h; line++) {
3150 for (row = 0; row < w; row++) {
3152 curr = (unsigned short int)(*ptr & mask);
3153 temp = (unsigned char)(curr >> 8);
3154 res = fwrite(&temp, 1, 1, rawFile);
3156 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3159 temp = (unsigned char) curr;
3160 res = fwrite(&temp, 1, 1, rawFile);
3162 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3169 } else if (image->comps[compno].prec <= 32) {
3170 fprintf(stderr, "More than 16 bits per component no handled yet\n");
3173 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
3181 #ifdef OPJ_HAVE_LIBPNG
3183 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3184 #define MAGIC_SIZE 8
3185 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3187 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3191 double gamma, display_exponent;
3192 int bit_depth, interlace_type, compression_type, filter_type;
3194 png_uint_32 resx, resy;
3196 png_uint_32 width, height;
3197 int color_type, has_alpha, is16;
3200 unsigned char **rows;
3203 opj_image_cmptparm_t cmptparm[4];
3205 unsigned int nr_comp;
3207 unsigned char sigbuf[8];
3209 if ((reader = fopen(read_idf, "rb")) == NULL) {
3210 fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
3217 if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3218 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
3219 fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
3222 /* libpng-VERSION/example.c:
3223 * PC : screen_gamma = 2.2;
3224 * Mac: screen_gamma = 1.7 or 1.0;
3226 display_exponent = 2.2;
3228 if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3229 NULL, NULL, NULL)) == NULL) {
3232 if ((info = png_create_info_struct(png)) == NULL) {
3236 if (setjmp(png_jmpbuf(png))) {
3240 png_init_io(png, reader);
3241 png_set_sig_bytes(png, MAGIC_SIZE);
3243 png_read_info(png, info);
3245 if (png_get_IHDR(png, info, &width, &height,
3246 &bit_depth, &color_type, &interlace_type,
3247 &compression_type, &filter_type) == 0) {
3251 /* png_set_expand():
3252 * expand paletted images to RGB, expand grayscale images of
3253 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3254 * to alpha channels.
3256 if (color_type == PNG_COLOR_TYPE_PALETTE) {
3257 png_set_expand(png);
3258 } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
3259 png_set_expand(png);
3262 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
3263 png_set_expand(png);
3266 is16 = (bit_depth == 16);
3268 /* GRAY => RGB; GRAY_ALPHA => RGBA
3270 if (color_type == PNG_COLOR_TYPE_GRAY
3271 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
3272 png_set_gray_to_rgb(png);
3274 (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
3275 PNG_COLOR_TYPE_RGB_ALPHA);
3277 if (!png_get_gAMA(png, info, &gamma)) {
3281 png_set_gamma(png, display_exponent, gamma);
3283 png_read_update_info(png, info);
3285 png_get_pHYs(png, info, &resx, &resy, &unit);
3287 color_type = png_get_color_type(png, info);
3289 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3291 nr_comp = 3 + has_alpha;
3293 bit_depth = png_get_bit_depth(png, info);
3295 rows = (unsigned char**)calloc(height + 1, sizeof(unsigned char*));
3296 for (i = 0; i < height; ++i) {
3297 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png, info));
3300 png_read_image(png, rows);
3302 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3304 sub_dx = params->subsampling_dx;
3305 sub_dy = params->subsampling_dy;
3307 for (i = 0; i < nr_comp; ++i) {
3308 cmptparm[i].prec = bit_depth;
3309 /* bits_per_pixel: 8 or 16 */
3310 cmptparm[i].bpp = bit_depth;
3311 cmptparm[i].sgnd = 0;
3312 cmptparm[i].dx = sub_dx;
3313 cmptparm[i].dy = sub_dy;
3314 cmptparm[i].w = width;
3315 cmptparm[i].h = height;
3318 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3320 if (image == NULL) {
3324 image->x0 = params->image_offset_x0;
3325 image->y0 = params->image_offset_y0;
3326 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3327 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3329 r = image->comps[0].data;
3330 g = image->comps[1].data;
3331 b = image->comps[2].data;
3332 a = image->comps[3].data;
3334 for (i = 0; i < height; ++i) {
3337 for (j = 0; j < width; ++j) {
3339 *r++ = s[0] << 8 | s[1];
3342 *g++ = s[0] << 8 | s[1];
3345 *b++ = s[0] << 8 | s[1];
3349 *a++ = s[0] << 8 | s[1];
3366 for (i = 0; i < height; ++i) {
3372 png_destroy_read_struct(&png, &info, NULL);
3381 int imagetopng(opj_image_t * image, const char *write_idf)
3386 int *red, *green, *blue, *alpha;
3387 unsigned char *row_buf, *d;
3388 int has_alpha, width, height, nr_comp, color_type;
3389 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3390 int prec, ushift, dshift, is16, force16, force8;
3391 unsigned short mask = 0xffff;
3392 png_color_8 sig_bit;
3394 is16 = force16 = force8 = ushift = dshift = 0;
3396 prec = image->comps[0].prec;
3397 nr_comp = image->numcomps;
3399 if (prec > 8 && prec < 16) {
3401 dshift = prec - ushift;
3404 } else if (prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */
3406 dshift = 8 - ushift;
3411 if (prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) {
3412 fprintf(stderr, "imagetopng: can not create %s"
3413 "\n\twrong bit_depth %d\n", write_idf, prec);
3416 writer = fopen(write_idf, "wb");
3418 if (writer == NULL) {
3425 /* Create and initialize the png_struct with the desired error handler
3426 * functions. If you want to use the default stderr and longjump method,
3427 * you can supply NULL for the last three parameters. We also check that
3428 * the library version is compatible with the one used at compile time,
3429 * in case we are using dynamically linked libraries. REQUIRED.
3431 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3433 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3439 /* Allocate/initialize the image information data. REQUIRED
3441 info = png_create_info_struct(png);
3447 /* Set error handling. REQUIRED if you are not supplying your own
3448 * error handling functions in the png_create_write_struct() call.
3450 if (setjmp(png_jmpbuf(png))) {
3454 /* I/O initialization functions is REQUIRED
3456 png_init_io(png, writer);
3458 /* Set the image information here. Width and height are up to 2^31,
3459 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3460 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3461 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3462 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3463 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3464 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3469 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3470 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3471 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3472 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3475 png_set_compression_level(png, Z_BEST_COMPRESSION);
3479 } else if (prec == 8) {
3481 } else if (prec == 4) {
3483 } else if (prec == 2) {
3485 } else if (prec == 1) {
3490 && image->comps[0].dx == image->comps[1].dx
3491 && image->comps[1].dx == image->comps[2].dx
3492 && image->comps[0].dy == image->comps[1].dy
3493 && image->comps[1].dy == image->comps[2].dy
3494 && image->comps[0].prec == image->comps[1].prec
3495 && image->comps[1].prec == image->comps[2].prec) {
3498 has_alpha = (nr_comp > 3);
3500 is16 = (prec == 16);
3502 width = image->comps[0].w;
3503 height = image->comps[0].h;
3505 red = image->comps[0].data;
3506 green = image->comps[1].data;
3507 blue = image->comps[2].data;
3509 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3512 sig_bit.alpha = prec;
3513 alpha = image->comps[3].data;
3514 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3515 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3519 color_type = PNG_COLOR_TYPE_RGB;
3522 png_set_sBIT(png, info, &sig_bit);
3524 png_set_IHDR(png, info, width, height, prec,
3527 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3528 /*=============================*/
3529 png_write_info(png, info);
3530 /*=============================*/
3532 png_set_packing(png);
3534 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3535 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3536 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3538 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3540 for (y = 0; y < height; ++y) {
3543 for (x = 0; x < width; ++x) {
3549 v = (v << ushift) + (v >> dshift);
3552 *d++ = (unsigned char)(v >> 8);
3553 *d++ = (unsigned char)v;
3555 v = *green + adjustG;
3559 v = (v << ushift) + (v >> dshift);
3562 *d++ = (unsigned char)(v >> 8);
3563 *d++ = (unsigned char)v;
3565 v = *blue + adjustB;
3569 v = (v << ushift) + (v >> dshift);
3572 *d++ = (unsigned char)(v >> 8);
3573 *d++ = (unsigned char)v;
3576 v = *alpha + adjustA;
3580 v = (v << ushift) + (v >> dshift);
3583 *d++ = (unsigned char)(v >> 8);
3584 *d++ = (unsigned char)v;
3593 v = (v << ushift) + (v >> dshift);
3596 *d++ = (unsigned char)(v & mask);
3598 v = *green + adjustG;
3602 v = (v << ushift) + (v >> dshift);
3605 *d++ = (unsigned char)(v & mask);
3607 v = *blue + adjustB;
3611 v = (v << ushift) + (v >> dshift);
3614 *d++ = (unsigned char)(v & mask);
3617 v = *alpha + adjustA;
3621 v = (v << ushift) + (v >> dshift);
3624 *d++ = (unsigned char)(v & mask);
3628 png_write_row(png, row_buf);
3634 else if (nr_comp == 1 /* GRAY */
3635 || (nr_comp == 2 /* GRAY_ALPHA */
3636 && image->comps[0].dx == image->comps[1].dx
3637 && image->comps[0].dy == image->comps[1].dy
3638 && image->comps[0].prec == image->comps[1].prec)) {
3641 red = image->comps[0].data;
3643 sig_bit.gray = prec;
3644 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3647 color_type = PNG_COLOR_TYPE_GRAY;
3651 sig_bit.alpha = prec;
3652 alpha = image->comps[1].data;
3653 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3654 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3656 width = image->comps[0].w;
3657 height = image->comps[0].h;
3659 png_set_IHDR(png, info, width, height, sig_bit.gray,
3662 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3664 png_set_sBIT(png, info, &sig_bit);
3665 /*=============================*/
3666 png_write_info(png, info);
3667 /*=============================*/
3668 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3671 png_set_packing(png);
3675 row_buf = (unsigned char*)
3676 malloc(width * nr_comp * sizeof(unsigned short));
3678 for (y = 0; y < height; ++y) {
3681 for (x = 0; x < width; ++x) {
3686 v = (v << ushift) + (v >> dshift);
3689 *d++ = (unsigned char)(v >> 8);
3690 *d++ = (unsigned char)v;
3696 v = (v << ushift) + (v >> dshift);
3699 *d++ = (unsigned char)(v >> 8);
3700 *d++ = (unsigned char)v;
3703 png_write_row(png, row_buf);
3707 } else { /* prec <= 8 */
3708 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3710 for (y = 0; y < height; ++y) {
3713 for (x = 0; x < width; ++x) {
3718 v = (v << ushift) + (v >> dshift);
3721 *d++ = (unsigned char)(v & mask);
3724 v = *alpha + adjustA;
3728 v = (v << ushift) + (v >> dshift);
3731 *d++ = (unsigned char)(v & mask);
3735 png_write_row(png, row_buf);
3741 fprintf(stderr, "imagetopng: can not create %s\n", write_idf);
3744 png_write_end(png, info);
3751 png_destroy_write_struct(&png, &info);
3761 #endif /* OPJ_HAVE_LIBPNG */