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) {
64 for (l = 0; a > 1; l++) {
70 /* -->> -->> -->> -->>
74 <<-- <<-- <<-- <<-- */
76 #ifdef INFORMATION_ONLY
77 /* 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) {
103 #ifdef OPJ_BIG_ENDIAN
104 return( ((val & 0xff) << 8) + (val >> 8) );
111 #define TGA_HEADER_SIZE 18
113 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
114 unsigned int *width, unsigned int *height, int *flip_image)
118 unsigned char id_len, cmap_type, image_type;
119 unsigned char pixel_depth, image_desc;
120 unsigned short cmap_index, cmap_len, cmap_entry_size;
121 unsigned short x_origin, y_origin, image_w, image_h;
123 if (!bits_per_pixel || !width || !height || !flip_image)
125 tga = (unsigned char*)malloc(18);
127 if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 )
129 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
132 id_len = (unsigned char)tga[0];
133 cmap_type = (unsigned char)tga[1];
134 image_type = (unsigned char)tga[2];
135 cmap_index = get_ushort(*(unsigned short*)(&tga[3]));
136 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
137 cmap_entry_size = (unsigned char)tga[7];
140 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
141 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
142 image_w = get_ushort(*(unsigned short*)(&tga[12]));
143 image_h = get_ushort(*(unsigned short*)(&tga[14]));
144 pixel_depth = (unsigned char)tga[16];
145 image_desc = (unsigned char)tga[17];
149 *bits_per_pixel = (unsigned int)pixel_depth;
150 *width = (unsigned int)image_w;
151 *height = (unsigned int)image_h;
153 /* Ignore tga identifier, if present ... */
156 unsigned char *id = (unsigned char *) malloc(id_len);
157 if ( !fread(id, id_len, 1, fp) )
159 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
166 /* Test for compressed formats ... not yet supported ...
167 // Note :- 9 - RLE encoded palettized.
168 // 10 - RLE encoded RGB. */
171 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
175 *flip_image = !(image_desc & 32);
177 /* Palettized formats are not yet supported, skip over the palette, if present ... */
178 palette_size = cmap_len * (cmap_entry_size/8);
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)
209 if ( bits_per_pixel < 256 )
210 pixel_depth = (unsigned char)bits_per_pixel;
212 fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
217 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */
218 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */
220 image_type = 2; /* Uncompressed. */
221 if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
224 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */
225 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */
226 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */
228 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */
229 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */
231 image_w = (unsigned short)width;
232 image_h = (unsigned short) height;
234 #ifndef OPJ_BIG_ENDIAN
235 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
236 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
238 image_w = swap16(image_w);
239 image_h = swap16(image_h);
240 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
241 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
244 if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
246 image_desc = 8; /* 8 bits per component. */
250 if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails;
255 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
259 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
262 unsigned int image_width, image_height, pixel_bit_depth;
265 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
267 OPJ_COLOR_SPACE color_space;
270 int subsampling_dx, subsampling_dy;
273 f = fopen(filename, "rb");
275 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
279 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
282 /* We currently only support 24 & 32 bit tga's ... */
283 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
286 /* initialize image components */
287 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
289 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */
290 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
293 color_space = CLRSPC_GRAY;
294 numcomps = save_alpha ? 2 : 1;
297 numcomps = save_alpha ? 4 : 3;
298 color_space = CLRSPC_SRGB;
301 subsampling_dx = parameters->subsampling_dx;
302 subsampling_dy = parameters->subsampling_dy;
304 for (i = 0; i < numcomps; i++) {
305 cmptparm[i].prec = 8;
307 cmptparm[i].sgnd = 0;
308 cmptparm[i].dx = subsampling_dx;
309 cmptparm[i].dy = subsampling_dy;
310 cmptparm[i].w = image_width;
311 cmptparm[i].h = image_height;
314 /* create the image */
315 image = opj_image_create(numcomps, &cmptparm[0], color_space);
320 /* set image offset and reference grid */
321 image->x0 = parameters->image_offset_x0;
322 image->y0 = parameters->image_offset_y0;
323 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
324 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
327 for (y=0; y < image_height; y++)
332 index = (image_height-y-1)*image_width;
334 index = y*image_width;
338 for (x=0;x<image_width;x++)
342 if( !fread(&b, 1, 1, f) )
344 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
345 opj_image_destroy(image);
348 if ( !fread(&g, 1, 1, f) )
350 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
351 opj_image_destroy(image);
354 if ( !fread(&r, 1, 1, f) )
356 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
357 opj_image_destroy(image);
361 image->comps[0].data[index]=r;
362 image->comps[1].data[index]=g;
363 image->comps[2].data[index]=b;
367 else if (numcomps==4)
369 for (x=0;x<image_width;x++)
371 unsigned char r,g,b,a;
372 if ( !fread(&b, 1, 1, f) )
374 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
375 opj_image_destroy(image);
378 if ( !fread(&g, 1, 1, f) )
380 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
381 opj_image_destroy(image);
384 if ( !fread(&r, 1, 1, f) )
386 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
387 opj_image_destroy(image);
390 if ( !fread(&a, 1, 1, f) )
392 fprintf(stderr, "\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;
400 image->comps[3].data[index]=a;
405 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
411 int imagetotga(opj_image_t * image, const char *outfile) {
412 int width, height, bpp, x, y;
413 opj_bool write_alpha;
414 int i, adjustR, adjustG, adjustB;
415 unsigned int alpha_channel;
422 fdest = fopen(outfile, "wb");
424 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
428 for (i = 0; i < image->numcomps-1; i++) {
429 if ((image->comps[0].dx != image->comps[i+1].dx)
430 ||(image->comps[0].dy != image->comps[i+1].dy)
431 ||(image->comps[0].prec != image->comps[i+1].prec)) {
432 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
437 width = image->comps[0].w;
438 height = image->comps[0].h;
440 /* Mono with alpha, or RGB with alpha. */
441 write_alpha = (image->numcomps==2) || (image->numcomps==4);
443 /* Write TGA header */
444 bpp = write_alpha ? 32 : 24;
445 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
448 alpha_channel = image->numcomps-1;
450 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
452 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
453 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
454 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
456 for (y=0; y < height; y++) {
457 unsigned int index=y*width;
459 for (x=0; x < width; x++, index++) {
460 r = (float)(image->comps[0].data[index] + adjustR);
462 if (image->numcomps>2) {
463 g = (float)(image->comps[1].data[index] + adjustG);
464 b = (float)(image->comps[2].data[index] + adjustB);
466 else {/* Greyscale ... */
471 /* TGA format writes BGR ... */
472 value = (unsigned char)(b*scale);
473 res = fwrite(&value,1,1,fdest);
475 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
479 value = (unsigned char)(g*scale);
480 res = fwrite(&value,1,1,fdest);
482 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
486 value = (unsigned char)(r*scale);
487 res = fwrite(&value,1,1,fdest);
489 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
494 a = (float)(image->comps[alpha_channel].data[index]);
495 value = (unsigned char)(a*scale);
496 res = fwrite(&value,1,1,fdest);
498 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
508 /* -->> -->> -->> -->>
512 <<-- <<-- <<-- <<-- */
514 /* WORD defines a two byte word */
515 typedef unsigned short int WORD;
517 /* DWORD defines a four byte word */
518 typedef unsigned int DWORD;
521 WORD bfType; /* 'BM' for Bitmap (19776) */
522 DWORD bfSize; /* Size of the file */
523 WORD bfReserved1; /* Reserved : 0 */
524 WORD bfReserved2; /* Reserved : 0 */
525 DWORD bfOffBits; /* Offset */
526 } BITMAPFILEHEADER_t;
529 DWORD biSize; /* Size of the structure in bytes */
530 DWORD biWidth; /* Width of the image in pixels */
531 DWORD biHeight; /* Heigth of the image in pixels */
532 WORD biPlanes; /* 1 */
533 WORD biBitCount; /* Number of color bits by pixels */
534 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
535 DWORD biSizeImage; /* Size of the image in bytes */
536 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
537 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
538 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
539 DWORD biClrImportant; /* Number of important color (0: ALL) */
540 } BITMAPINFOHEADER_t;
542 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
544 int subsampling_dx = parameters->subsampling_dx;
545 int subsampling_dy = parameters->subsampling_dy;
547 int i, numcomps, w, h;
548 OPJ_COLOR_SPACE color_space;
549 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
550 opj_image_t * image = NULL;
553 BITMAPFILEHEADER_t File_h;
554 BITMAPINFOHEADER_t Info_h;
556 unsigned char *table_R, *table_G, *table_B;
557 unsigned int j, PAD = 0;
564 IN = fopen(filename, "rb");
567 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
571 File_h.bfType = getc(IN);
572 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
574 if (File_h.bfType != 19778)
576 fprintf(stderr,"Error, not a BMP file!\n");
582 File_h.bfSize = getc(IN);
583 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
584 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
585 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
587 File_h.bfReserved1 = getc(IN);
588 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
590 File_h.bfReserved2 = getc(IN);
591 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
593 File_h.bfOffBits = getc(IN);
594 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
595 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
596 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
601 Info_h.biSize = getc(IN);
602 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
603 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
604 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
606 if(Info_h.biSize != 40)
608 fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize);
612 Info_h.biWidth = getc(IN);
613 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
614 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
615 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
618 Info_h.biHeight = getc(IN);
619 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
620 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
621 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
624 Info_h.biPlanes = getc(IN);
625 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
627 Info_h.biBitCount = getc(IN);
628 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
630 Info_h.biCompression = getc(IN);
631 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
632 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
633 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
635 Info_h.biSizeImage = getc(IN);
636 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
637 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
638 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
640 Info_h.biXpelsPerMeter = getc(IN);
641 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
642 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
643 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
645 Info_h.biYpelsPerMeter = getc(IN);
646 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
647 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
648 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
650 Info_h.biClrUsed = getc(IN);
651 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
652 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
653 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
655 Info_h.biClrImportant = getc(IN);
656 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
657 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
658 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
660 /* Read the data and store them in the OUT file */
662 if (Info_h.biBitCount == 24)
665 color_space = CLRSPC_SRGB;
666 /* initialize image components */
667 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
668 for(i = 0; i < numcomps; i++)
670 cmptparm[i].prec = 8;
672 cmptparm[i].sgnd = 0;
673 cmptparm[i].dx = subsampling_dx;
674 cmptparm[i].dy = subsampling_dy;
678 /* create the image */
679 image = opj_image_create(numcomps, &cmptparm[0], color_space);
686 /* set image offset and reference grid */
687 image->x0 = parameters->image_offset_x0;
688 image->y0 = parameters->image_offset_y0;
689 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
690 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
694 /* Place the cursor at the beginning of the image information */
695 fseek(IN, 0, SEEK_SET);
696 fseek(IN, File_h.bfOffBits, SEEK_SET);
701 /* PAD = 4 - (3 * W) % 4; */
702 /* PAD = (PAD == 4) ? 0 : PAD; */
703 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
705 RGB = (unsigned char *)
706 malloc((3 * W + PAD) * H * sizeof(unsigned char));
708 if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
711 opj_image_destroy(image);
712 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
718 for(y = 0; y < (int)H; y++)
720 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
721 for(x = 0; x < (int)W; x++)
723 unsigned char *pixel = &scanline[3 * x];
724 image->comps[0].data[index] = pixel[2]; /* R */
725 image->comps[1].data[index] = pixel[1]; /* G */
726 image->comps[2].data[index] = pixel[0]; /* B */
731 }/* if (Info_h.biBitCount == 24) */
733 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
735 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
737 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
739 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
740 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
741 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
744 for (j = 0; j < Info_h.biClrUsed; j++)
746 table_B[j] = (unsigned char)getc(IN);
747 table_G[j] = (unsigned char)getc(IN);
748 table_R[j] = (unsigned char)getc(IN);
751 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
753 if(has_color) gray_scale = 0;
755 /* Place the cursor at the beginning of the image information */
756 fseek(IN, 0, SEEK_SET);
757 fseek(IN, File_h.bfOffBits, SEEK_SET);
761 if (Info_h.biWidth % 2)
764 numcomps = gray_scale ? 1 : 3;
765 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
766 /* initialize image components */
767 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
768 for(i = 0; i < numcomps; i++)
770 cmptparm[i].prec = 8;
772 cmptparm[i].sgnd = 0;
773 cmptparm[i].dx = subsampling_dx;
774 cmptparm[i].dy = subsampling_dy;
778 /* create the image */
779 image = opj_image_create(numcomps, &cmptparm[0], color_space);
783 free(table_R); free(table_G); free(table_B);
787 /* set image offset and reference grid */
788 image->x0 = parameters->image_offset_x0;
789 image->y0 = parameters->image_offset_y0;
790 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
791 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
795 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
797 if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
803 opj_image_destroy(image);
804 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
810 for (j = 0; j < W * H; j++)
812 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
814 image->comps[0].data[index] =
815 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
824 for (j = 0; j < W * H; j++)
826 if ((j % W < W - 1 && Info_h.biWidth % 2)
827 || !(Info_h.biWidth % 2))
829 unsigned char pixel_index =
830 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
831 image->comps[0].data[index] = table_R[pixel_index];
832 image->comps[1].data[index] = table_G[pixel_index];
833 image->comps[2].data[index] = table_B[pixel_index];
844 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
846 unsigned char *pix, *beyond;
847 int *gray, *red, *green, *blue;
848 unsigned int x, y, max;
852 if (Info_h.biClrUsed == 0)
853 Info_h.biClrUsed = 256;
854 else if (Info_h.biClrUsed > 256)
855 Info_h.biClrUsed = 256;
857 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
858 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
859 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
862 for (j = 0; j < Info_h.biClrUsed; j++)
864 table_B[j] = (unsigned char)getc(IN);
865 table_G[j] = (unsigned char)getc(IN);
866 table_R[j] = (unsigned char)getc(IN);
868 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
874 numcomps = gray_scale ? 1 : 3;
875 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
876 /* initialize image components */
877 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
878 for (i = 0; i < numcomps; i++)
880 cmptparm[i].prec = 8;
882 cmptparm[i].sgnd = 0;
883 cmptparm[i].dx = subsampling_dx;
884 cmptparm[i].dy = subsampling_dy;
888 /* create the image */
889 image = opj_image_create(numcomps, &cmptparm[0], color_space);
899 /* set image offset and reference grid */
900 image->x0 = parameters->image_offset_x0;
901 image->y0 = parameters->image_offset_y0;
902 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
903 - 1) * subsampling_dx + 1;
904 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
905 - 1) * subsampling_dy + 1;
909 /* Place the cursor at the beginning of the image information */
910 fseek(IN, 0, SEEK_SET);
911 fseek(IN, File_h.bfOffBits, SEEK_SET);
915 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
916 beyond = RGB + W * H;
928 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
929 *pix = (unsigned char)c1;
935 if (c == 0x00) /* EOL */
939 pix = RGB + x + (H - y - 1) * W;
941 else if (c == 0x01) /* EOP */
943 else if (c == 0x02) /* MOVE by dxdy */
949 pix = RGB + (H - y - 1) * W + x;
954 for (; i < c && x < W && pix < beyond; i++, x++, pix++)
957 *pix = (unsigned char)c1;
959 if (c & 1) /* skip padding byte */
967 gray = image->comps[0].data;
975 *gray++ = table_R[uc];
980 /*int *red, *green, *blue;*/
982 red = image->comps[0].data;
983 green = image->comps[1].data;
984 blue = image->comps[2].data;
992 *red++ = table_R[uc];
993 *green++ = table_G[uc];
994 *blue++ = table_B[uc];
1005 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1006 "is not yet implemented [%d]\n", Info_h.biBitCount);
1012 int imagetobmp(opj_image_t * image, const char *outfile) {
1016 int adjustR, adjustG, adjustB;
1018 if (image->comps[0].prec < 8) {
1019 fprintf(stderr, "Unsupported precision: %d\n", image->comps[0].prec);
1022 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1023 && image->comps[1].dx == image->comps[2].dx
1024 && image->comps[0].dy == image->comps[1].dy
1025 && image->comps[1].dy == image->comps[2].dy
1026 && image->comps[0].prec == image->comps[1].prec
1027 && image->comps[1].prec == image->comps[2].prec) {
1029 /* -->> -->> -->> -->>
1031 <<-- <<-- <<-- <<-- */
1033 fdest = fopen(outfile, "wb");
1035 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1039 w = image->comps[0].w;
1040 h = image->comps[0].h;
1042 fprintf(fdest, "BM");
1046 fprintf(fdest, "%c%c%c%c",
1047 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1048 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1049 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1050 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1051 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1052 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
1056 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1057 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1058 (unsigned char) ((w) >> 8) & 0xff,
1059 (unsigned char) ((w) >> 16) & 0xff,
1060 (unsigned char) ((w) >> 24) & 0xff);
1061 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1062 (unsigned char) ((h) >> 8) & 0xff,
1063 (unsigned char) ((h) >> 16) & 0xff,
1064 (unsigned char) ((h) >> 24) & 0xff);
1065 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1066 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1067 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1068 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
1069 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1070 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1071 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1072 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1073 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1074 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1075 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1077 if (image->comps[0].prec > 8) {
1078 adjustR = image->comps[0].prec - 8;
1079 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1083 if (image->comps[1].prec > 8) {
1084 adjustG = image->comps[1].prec - 8;
1085 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1089 if (image->comps[2].prec > 8) {
1090 adjustB = image->comps[2].prec - 8;
1091 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1096 for (i = 0; i < w * h; i++) {
1097 unsigned char rc, gc, bc;
1100 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1101 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1102 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1103 if(r > 255) r = 255; else if(r < 0) r = 0;
1104 rc = (unsigned char)r;
1106 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1107 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1108 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
1109 if(g > 255) g = 255; else if(g < 0) g = 0;
1110 gc = (unsigned char)g;
1112 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1113 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1114 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
1115 if(b > 255) b = 255; else if(b < 0) b = 0;
1116 bc = (unsigned char)b;
1118 fprintf(fdest, "%c%c%c", bc, gc, rc);
1120 if ((i + 1) % w == 0) {
1121 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
1122 fprintf(fdest, "%c", 0);
1126 } else { /* Gray-scale */
1128 /* -->> -->> -->> -->>
1129 8 bits non code (Gray scale)
1130 <<-- <<-- <<-- <<-- */
1132 fdest = fopen(outfile, "wb");
1133 w = image->comps[0].w;
1134 h = image->comps[0].h;
1136 fprintf(fdest, "BM");
1140 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1141 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1142 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1143 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1144 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1145 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1146 ((54 + 1024) >> 16) & 0xff,
1147 ((54 + 1024) >> 24) & 0xff);
1151 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1152 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1153 (unsigned char) ((w) >> 8) & 0xff,
1154 (unsigned char) ((w) >> 16) & 0xff,
1155 (unsigned char) ((w) >> 24) & 0xff);
1156 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1157 (unsigned char) ((h) >> 8) & 0xff,
1158 (unsigned char) ((h) >> 16) & 0xff,
1159 (unsigned char) ((h) >> 24) & 0xff);
1160 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1161 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1162 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1163 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
1164 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
1165 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
1166 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
1167 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1168 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1169 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1170 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1172 if (image->comps[0].prec > 8) {
1173 adjustR = image->comps[0].prec - 8;
1174 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1178 for (i = 0; i < 256; i++) {
1179 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1182 for (i = 0; i < w * h; i++) {
1185 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1186 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1187 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1188 if(r > 255) r = 255; else if(r < 0) r = 0;
1190 fprintf(fdest, "%c", (unsigned char)r);
1192 if ((i + 1) % w == 0) {
1193 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1194 fprintf(fdest, "%c", 0);
1203 /* -->> -->> -->> -->>
1207 <<-- <<-- <<-- <<-- */
1210 static unsigned char readuchar(FILE * f)
1213 if ( !fread(&c1, 1, 1, f) )
1215 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1221 static unsigned short readushort(FILE * f, int bigendian)
1223 unsigned char c1, c2;
1224 if ( !fread(&c1, 1, 1, f) )
1226 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1229 if ( !fread(&c2, 1, 1, f) )
1231 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1235 return (c1 << 8) + c2;
1237 return (c2 << 8) + c1;
1240 static unsigned int readuint(FILE * f, int bigendian)
1242 unsigned char c1, c2, c3, c4;
1243 if ( !fread(&c1, 1, 1, f) )
1245 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1248 if ( !fread(&c2, 1, 1, f) )
1250 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1253 if ( !fread(&c3, 1, 1, f) )
1255 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1258 if ( !fread(&c4, 1, 1, f) )
1260 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1264 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1266 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1269 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1272 int i, numcomps, max;
1273 OPJ_COLOR_SPACE color_space;
1274 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1275 opj_image_t * image = NULL;
1276 int adjustS, ushift, dshift, force8;
1278 char endian1,endian2,sign;
1283 opj_image_comp_t *comp = NULL;
1286 color_space = CLRSPC_GRAY;
1288 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1292 f = fopen(filename, "rb");
1294 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1298 fseek(f, 0, SEEK_SET);
1299 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1300 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1306 while (signtmp[i]!='\0') {
1307 if (signtmp[i]=='-') sign='-';
1312 if (endian1=='M' && endian2=='L') {
1314 } else if (endian2=='M' && endian1=='L') {
1317 fprintf(stderr, "Bad pgx header, please check input file\n");
1321 /* initialize image component */
1323 cmptparm.x0 = parameters->image_offset_x0;
1324 cmptparm.y0 = parameters->image_offset_y0;
1325 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1326 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1336 ushift = 8 - prec; dshift = prec - ushift;
1337 if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0;
1341 else ushift = dshift = force8 = adjustS = 0;
1343 cmptparm.prec = prec;
1344 cmptparm.bpp = prec;
1345 cmptparm.dx = parameters->subsampling_dx;
1346 cmptparm.dy = parameters->subsampling_dy;
1348 /* create the image */
1349 image = opj_image_create(numcomps, &cmptparm, color_space);
1354 /* set image offset and reference grid */
1355 image->x0 = cmptparm.x0;
1356 image->y0 = cmptparm.x0;
1357 image->x1 = cmptparm.w;
1358 image->y1 = cmptparm.h;
1360 /* set image data */
1362 comp = &image->comps[0];
1364 for (i = 0; i < w * h; i++) {
1368 v = readuchar(f) + adjustS;
1369 v = (v<<ushift) + (v>>dshift);
1370 comp->data[i] = (unsigned char)v;
1372 if(v > max) max = v;
1376 if (comp->prec == 8) {
1380 v = (char) readuchar(f);
1382 } else if (comp->prec <= 16) {
1384 v = readushort(f, bigendian);
1386 v = (short) readushort(f, bigendian);
1390 v = readuint(f, bigendian);
1392 v = (int) readuint(f, bigendian);
1400 comp->bpp = int_floorlog2(max) + 1;
1405 int imagetopgx(opj_image_t * image, const char *outfile) {
1410 for (compno = 0; compno < image->numcomps; compno++) {
1411 opj_image_comp_t *comp = &image->comps[compno];
1412 char bname[256]; /* buffer for name */
1413 char *name = bname; /* pointer */
1416 const size_t olen = strlen(outfile);
1417 const size_t dotpos = olen - 4;
1418 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1419 if( outfile[dotpos] != '.' ) {
1420 /* `pgx` was recognized but there is no dot at expected position */
1421 fprintf(stderr, "ERROR -> Impossible happen." );
1425 name = (char*)malloc(total+1);
1427 strncpy(name, outfile, dotpos);
1428 /*if (image->numcomps > 1) {*/
1429 sprintf(name+dotpos, "_%d.pgx", compno);
1431 strcpy(name+dotpos, ".pgx");
1433 fdest = fopen(name, "wb");
1435 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1438 /* don't need name anymore */
1443 w = image->comps[compno].w;
1444 h = image->comps[compno].h;
1446 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1447 if (comp->prec <= 8) {
1449 } else if (comp->prec <= 16) {
1454 for (i = 0; i < w * h; i++) {
1455 int v = image->comps[compno].data[i];
1456 for (j = nbytes - 1; j >= 0; j--) {
1457 char byte = (char) (v >> (j * 8));
1458 res = fwrite(&byte, 1, 1, fdest);
1460 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1471 /* -->> -->> -->> -->>
1475 <<-- <<-- <<-- <<-- */
1479 int width, height, maxval, depth, format;
1480 char rgb, rgba, gray, graya, bw;
1484 static char *skip_white(char *s)
1488 if(*s == '\n' || *s == '\r') return NULL;
1489 if(isspace(*s)) { ++s; continue; }
1495 static char *skip_int(char *start, int *out_n)
1500 *out_n = 0; s = start;
1502 s = skip_white(start);
1503 if(s == NULL) return NULL;
1508 if( !isdigit(*s)) break;
1511 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1515 static char *skip_idf(char *start, char out_idf[256])
1520 s = skip_white(start);
1521 if(s == NULL) return NULL;
1526 if(isalpha(*s) || *s == '_') { ++s; continue; }
1529 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1533 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1536 int format, have_wh, end, ttype;
1537 char idf[256], type[256];
1540 if (fgets(line, 250, reader) == NULL)
1542 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1548 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1550 format = atoi(line + 1);
1551 if(format < 1 || format > 7)
1553 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1556 ph->format = format;
1557 ttype = end = have_wh = 0;
1559 while(fgets(line, 250, reader))
1561 if(*line == '#') continue;
1567 s = skip_idf(s, idf);
1569 if(s == NULL || *s == 0) return;
1571 if(strcmp(idf, "ENDHDR") == 0)
1575 if(strcmp(idf, "WIDTH") == 0)
1577 s = skip_int(s, &ph->width);
1578 if(s == NULL || *s == 0) return;
1582 if(strcmp(idf, "HEIGHT") == 0)
1584 s = skip_int(s, &ph->height);
1585 if(s == NULL || *s == 0) return;
1589 if(strcmp(idf, "DEPTH") == 0)
1591 s = skip_int(s, &ph->depth);
1592 if(s == NULL || *s == 0) return;
1596 if(strcmp(idf, "MAXVAL") == 0)
1598 s = skip_int(s, &ph->maxval);
1599 if(s == NULL || *s == 0) return;
1603 if(strcmp(idf, "TUPLTYPE") == 0)
1605 s = skip_idf(s, type);
1606 if(s == NULL || *s == 0) return;
1608 if(strcmp(type, "BLACKANDWHITE") == 0)
1610 ph->bw = 1; ttype = 1; continue;
1612 if(strcmp(type, "GRAYSCALE") == 0)
1614 ph->gray = 1; ttype = 1; continue;
1616 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1618 ph->graya = 1; ttype = 1; continue;
1620 if(strcmp(type, "RGB") == 0)
1622 ph->rgb = 1; ttype = 1; continue;
1624 if(strcmp(type, "RGB_ALPHA") == 0)
1626 ph->rgba = 1; ttype = 1; continue;
1628 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1631 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1633 } /* if(format == 7) */
1637 s = skip_int(s, &ph->width);
1639 s = skip_int(s, &ph->height);
1643 if(format == 1 || format == 4) break;
1647 if(format == 2 || format == 3 || format == 5 || format == 6)
1649 /* P2, P3, P5, P6: */
1650 s = skip_int(s, &ph->maxval);
1652 if(ph->maxval > 65535) return;
1655 }/* while(fgets( ) */
1656 if(format == 2 || format == 3 || format > 4)
1658 if(ph->maxval < 1 || ph->maxval > 65535) return;
1660 if(ph->width < 1 || ph->height < 1) return;
1666 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1668 if(ph->depth < 1 || ph->depth > 4) return;
1670 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1675 if(format != 1 && format != 4)
1677 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1681 if(ph->width && ph->height) ph->ok = 1;
1687 static int has_prec(int val)
1689 if(val < 2) return 1;
1690 if(val < 4) return 2;
1691 if(val < 8) return 3;
1692 if(val < 16) return 4;
1693 if(val < 32) return 5;
1694 if(val < 64) return 6;
1695 if(val < 128) return 7;
1696 if(val < 256) return 8;
1697 if(val < 512) return 9;
1698 if(val < 1024) return 10;
1699 if(val < 2048) return 11;
1700 if(val < 4096) return 12;
1701 if(val < 8192) return 13;
1702 if(val < 16384) return 14;
1703 if(val < 32768) return 15;
1707 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1708 int subsampling_dx = parameters->subsampling_dx;
1709 int subsampling_dy = parameters->subsampling_dy;
1712 int i, compno, numcomps, w, h, prec, format;
1713 OPJ_COLOR_SPACE color_space;
1714 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1715 opj_image_t * image = NULL;
1716 struct pnm_header header_info;
1718 if((fp = fopen(filename, "rb")) == NULL)
1720 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1723 memset(&header_info, 0, sizeof(struct pnm_header));
1725 read_pnm_header(fp, &header_info);
1727 if(!header_info.ok) { fclose(fp); return NULL; }
1729 format = header_info.format;
1733 case 1: /* ascii bitmap */
1734 case 4: /* raw bitmap */
1738 case 2: /* ascii greymap */
1739 case 5: /* raw greymap */
1743 case 3: /* ascii pixmap */
1744 case 6: /* raw pixmap */
1748 case 7: /* arbitrary map */
1749 numcomps = header_info.depth;
1752 default: fclose(fp); return NULL;
1755 color_space = CLRSPC_GRAY;/* GRAY, GRAYA */
1757 color_space = CLRSPC_SRGB;/* RGB, RGBA */
1759 prec = has_prec(header_info.maxval);
1761 if(prec < 8) prec = 8;
1763 w = header_info.width;
1764 h = header_info.height;
1765 subsampling_dx = parameters->subsampling_dx;
1766 subsampling_dy = parameters->subsampling_dy;
1768 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1770 for(i = 0; i < numcomps; i++)
1772 cmptparm[i].prec = prec;
1773 cmptparm[i].bpp = prec;
1774 cmptparm[i].sgnd = 0;
1775 cmptparm[i].dx = subsampling_dx;
1776 cmptparm[i].dy = subsampling_dy;
1780 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1782 if(!image) { fclose(fp); return NULL; }
1784 /* set image offset and reference grid */
1785 image->x0 = parameters->image_offset_x0;
1786 image->y0 = parameters->image_offset_y0;
1787 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1788 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1790 if((format == 2) || (format == 3)) /* ascii pixmap */
1794 for (i = 0; i < w * h; i++)
1796 for(compno = 0; compno < numcomps; compno++)
1799 if (fscanf(fp, "%u", &index) != 1)
1800 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1802 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1810 && ( header_info.gray || header_info.graya
1811 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1813 unsigned char c0, c1, one;
1817 for (i = 0; i < w * h; i++)
1819 for(compno = 0; compno < numcomps; compno++)
1821 if ( !fread(&c0, 1, 1, fp) )
1822 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1825 image->comps[compno].data[i] = c0;
1829 if ( !fread(&c1, 1, 1, fp) )
1830 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1832 image->comps[compno].data[i] = ((c0<<8) | c1);
1838 if(format == 1) /* ascii bitmap */
1840 for (i = 0; i < w * h; i++)
1844 if ( fscanf(fp, "%u", &index) != 1)
1845 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1847 image->comps[0].data[i] = (index?0:255);
1857 for(y = 0; y < h; ++y)
1861 for(x = 0; x < w; ++x)
1866 uc = (unsigned char)getc(fp);
1868 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1874 if((format == 7 && header_info.bw)) /*MONO*/
1878 for(i = 0; i < w * h; ++i)
1880 if ( !fread(&uc, 1, 1, fp) )
1881 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1882 image->comps[0].data[i] = (uc & 1)?0:255;
1890 int imagetopnm(opj_image_t * image, const char *outfile)
1892 int *red, *green, *blue, *alpha;
1894 int i, compno, ncomp;
1895 int adjustR, adjustG, adjustB, adjustA;
1896 int fails, two, want_gray, has_alpha, triple;
1899 const char *tmp = outfile;
1902 if((prec = image->comps[0].prec) > 16)
1904 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1905 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1908 two = has_alpha = 0; fails = 1;
1909 ncomp = image->numcomps;
1911 while (*tmp) ++tmp; tmp -= 2;
1912 want_gray = (*tmp == 'g' || *tmp == 'G');
1913 ncomp = image->numcomps;
1915 if(want_gray) ncomp = 1;
1917 if (ncomp == 2 /* GRAYA */
1918 || (ncomp > 2 /* RGB, RGBA */
1919 && image->comps[0].dx == image->comps[1].dx
1920 && image->comps[1].dx == image->comps[2].dx
1921 && image->comps[0].dy == image->comps[1].dy
1922 && image->comps[1].dy == image->comps[2].dy
1923 && image->comps[0].prec == image->comps[1].prec
1924 && image->comps[1].prec == image->comps[2].prec
1927 fdest = fopen(outfile, "wb");
1931 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1935 triple = (ncomp > 2);
1936 wr = image->comps[0].w; hr = image->comps[0].h;
1937 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1939 red = image->comps[0].data;
1943 green = image->comps[1].data;
1944 blue = image->comps[2].data;
1946 else green = blue = NULL;
1950 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1952 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1953 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1954 wr, hr, ncomp, max, tt);
1955 alpha = image->comps[ncomp - 1].data;
1956 adjustA = (image->comps[ncomp - 1].sgnd ?
1957 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1961 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1962 opj_version(), wr, hr, max);
1965 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1969 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1970 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1972 else adjustG = adjustB = 0;
1974 for(i = 0; i < wr * hr; ++i)
1978 v = *red + adjustR; ++red;
1980 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1984 v = *green + adjustG; ++green;
1986 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1988 v = *blue + adjustB; ++blue;
1990 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1996 v = *alpha + adjustA; ++alpha;
1998 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2006 fprintf(fdest, "%c", (unsigned char)*red++);
2008 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
2011 fprintf(fdest, "%c", (unsigned char)*alpha++);
2015 fclose(fdest); return 0;
2020 if (image->numcomps > ncomp)
2022 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
2023 fprintf(stderr," is written to the file\n");
2025 destname = (char*)malloc(strlen(outfile) + 8);
2027 for (compno = 0; compno < ncomp; compno++)
2030 sprintf(destname, "%d.%s", compno, outfile);
2032 sprintf(destname, "%s", outfile);
2034 fdest = fopen(destname, "wb");
2037 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2041 wr = image->comps[compno].w; hr = image->comps[compno].h;
2042 prec = image->comps[compno].prec;
2043 max = (1<<prec) - 1;
2045 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2046 opj_version(), wr, hr, max);
2048 red = image->comps[compno].data;
2050 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2054 for (i = 0; i < wr * hr; i++)
2056 v = *red + adjustR; ++red;
2058 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2064 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2068 else /* prec <= 8 */
2070 for(i = 0; i < wr * hr; ++i)
2072 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
2082 #ifdef OPJ_HAVE_LIBTIFF
2083 /* -->> -->> -->> -->>
2087 <<-- <<-- <<-- <<-- */
2089 int imagetotif(opj_image_t * image, const char *outfile)
2091 int width, height, imgsize;
2092 int bps,index,adjust, sgnd;
2093 int ushift, dshift, has_alpha, force16;
2099 ushift = dshift = force16 = has_alpha = 0;
2100 bps = image->comps[0].prec;
2102 if(bps > 8 && bps < 16)
2104 ushift = 16 - bps; dshift = bps - ushift;
2105 bps = 16; force16 = 1;
2108 if(bps != 8 && bps != 16)
2110 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2112 fprintf(stderr,"\tAborting\n");
2115 tif = TIFFOpen(outfile, "wb");
2119 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2122 sgnd = image->comps[0].sgnd;
2123 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2125 if(image->numcomps >= 3
2126 && image->comps[0].dx == image->comps[1].dx
2127 && image->comps[1].dx == image->comps[2].dx
2128 && image->comps[0].dy == image->comps[1].dy
2129 && image->comps[1].dy == image->comps[2].dy
2130 && image->comps[0].prec == image->comps[1].prec
2131 && image->comps[1].prec == image->comps[2].prec)
2133 has_alpha = (image->numcomps == 4);
2135 width = image->comps[0].w;
2136 height = image->comps[0].h;
2137 imgsize = width * height ;
2139 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2140 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2141 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2142 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2143 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2144 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2145 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2146 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2147 strip_size = TIFFStripSize(tif);
2148 buf = _TIFFmalloc(strip_size);
2151 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2153 unsigned char *dat8;
2154 tsize_t i, ssize, last_i = 0;
2156 ssize = TIFFStripSize(tif);
2157 dat8 = (unsigned char*)buf;
2161 step = 3 + has_alpha;
2164 for(i=0; i < ssize - restx; i += step)
2170 r = image->comps[0].data[index];
2171 g = image->comps[1].data[index];
2172 b = image->comps[2].data[index];
2173 if(has_alpha) a = image->comps[3].data[index];
2180 if(has_alpha) a += adjust;
2185 if(has_alpha) dat8[i+3] = a;
2196 for(i = last_i; i < ssize; i += step)
2202 r = image->comps[0].data[index];
2203 g = image->comps[1].data[index];
2204 b = image->comps[2].data[index];
2205 if(has_alpha) a = image->comps[3].data[index];
2212 if(has_alpha) a += adjust;
2215 if(i+1 < ssize) dat8[i+1] = g ; else break;
2216 if(i+2 < ssize) dat8[i+2] = b ; else break;
2219 if(i+3 < ssize) dat8[i+3] = a ; else break;
2226 }/*if(last_i < ssize)*/
2232 step = 6 + has_alpha + has_alpha;
2235 for(i = 0; i < ssize - restx ; i += step)
2241 r = image->comps[0].data[index];
2242 g = image->comps[1].data[index];
2243 b = image->comps[2].data[index];
2244 if(has_alpha) a = image->comps[3].data[index];
2251 if(has_alpha) a += adjust;
2255 r = (r<<ushift) + (r>>dshift);
2256 g = (g<<ushift) + (g>>dshift);
2257 b = (b<<ushift) + (b>>dshift);
2258 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2260 dat8[i+0] = r;/*LSB*/
2261 dat8[i+1] = (r >> 8);/*MSB*/
2263 dat8[i+3] = (g >> 8);
2265 dat8[i+5] = (b >> 8);
2269 dat8[i+7] = (a >> 8);
2280 for(i = last_i ; i < ssize ; i += step)
2286 r = image->comps[0].data[index];
2287 g = image->comps[1].data[index];
2288 b = image->comps[2].data[index];
2289 if(has_alpha) a = image->comps[3].data[index];
2296 if(has_alpha) a += adjust;
2300 r = (r<<ushift) + (r>>dshift);
2301 g = (g<<ushift) + (g>>dshift);
2302 b = (b<<ushift) + (b>>dshift);
2303 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2305 dat8[i+0] = r;/*LSB*/
2306 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/
2307 if(i+2 < ssize) dat8[i+2] = g; else break;
2308 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2309 if(i+4 < ssize) dat8[i+4] = b; else break;
2310 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2314 if(i+6 < ssize) dat8[i+6] = a; else break;
2315 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2322 }/*if(last_i < ssize)*/
2325 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2326 }/*for(strip = 0; )*/
2328 _TIFFfree((void*)buf);
2334 if(image->numcomps == 1 /* GRAY */
2335 || ( image->numcomps == 2 /* GRAY_ALPHA */
2336 && image->comps[0].dx == image->comps[1].dx
2337 && image->comps[0].dy == image->comps[1].dy
2338 && image->comps[0].prec == image->comps[1].prec))
2342 has_alpha = (image->numcomps == 2);
2344 width = image->comps[0].w;
2345 height = image->comps[0].h;
2346 imgsize = width * height;
2349 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2350 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2351 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2352 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2353 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2354 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2355 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2356 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2358 /* Get a buffer for the data */
2359 strip_size = TIFFStripSize(tif);
2360 buf = _TIFFmalloc(strip_size);
2363 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2365 unsigned char *dat8;
2366 tsize_t i, ssize = TIFFStripSize(tif);
2367 dat8 = (unsigned char*)buf;
2371 step = 1 + has_alpha;
2373 for(i=0; i < ssize; i += step)
2379 r = image->comps[0].data[index];
2380 if(has_alpha) a = image->comps[1].data[index];
2385 if(has_alpha) a += adjust;
2388 if(has_alpha) dat8[i+1] = a;
2398 step = 2 + has_alpha + has_alpha;
2400 for(i=0; i < ssize; i += step)
2406 r = image->comps[0].data[index];
2407 if(has_alpha) a = image->comps[1].data[index];
2412 if(has_alpha) a += adjust;
2416 r = (r<<ushift) + (r>>dshift);
2417 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2419 dat8[i+0] = r;/*LSB*/
2420 dat8[i+1] = r >> 8;/*MSB*/
2427 }/*if(index < imgsize)*/
2432 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2443 fprintf(stderr,"imagetotif: Bad color format.\n"
2444 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2445 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2452 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2453 * CINEMA : 12 bit precision
2455 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2457 int subsampling_dx = parameters->subsampling_dx;
2458 int subsampling_dy = parameters->subsampling_dy;
2463 int j, numcomps, w, h,index;
2464 OPJ_COLOR_SPACE color_space;
2465 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2466 opj_image_t *image = NULL;
2469 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2470 unsigned int tiWidth, tiHeight;
2472 tif = TIFFOpen(filename, "r");
2476 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2479 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2480 tiWidth = tiHeight = 0;
2482 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2483 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2484 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2485 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2486 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2487 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2488 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2493 unsigned short b = tiBps, p = tiPhoto;
2495 if(tiBps != 8 && tiBps != 16 && tiBps != 12) b = 0;
2496 if(tiPhoto != 1 && tiPhoto != 2) p = 0;
2501 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2502 " implemented\n",tiBps);
2505 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2506 " and GRAY(A) has been implemented\n",(int) tiPhoto);
2508 fprintf(stderr,"\tAborting\n");
2514 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2516 uint16 extrasamples;
2518 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2519 &extrasamples, &sampleinfo);
2521 if(extrasamples >= 1)
2523 switch(sampleinfo[0])
2525 case EXTRASAMPLE_UNSPECIFIED:
2526 /* Workaround for some images without correct info about alpha channel
2532 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2533 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2538 else /* extrasamples == 0 */
2539 if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
2542 /* initialize image components
2544 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2546 if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2548 numcomps = 3 + has_alpha;
2549 color_space = CLRSPC_SRGB;
2551 for(j = 0; j < numcomps; j++)
2553 if(parameters->cp_cinema)
2555 cmptparm[j].prec = 12;
2556 cmptparm[j].bpp = 12;
2560 cmptparm[j].prec = tiBps;
2561 cmptparm[j].bpp = tiBps;
2563 cmptparm[j].dx = subsampling_dx;
2564 cmptparm[j].dy = subsampling_dy;
2569 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2576 /* set image offset and reference grid
2578 image->x0 = parameters->image_offset_x0;
2579 image->y0 = parameters->image_offset_y0;
2580 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2581 image->x0 + (w - 1) * subsampling_dx + 1;
2582 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2583 image->y0 + (h - 1) * subsampling_dy + 1;
2585 buf = _TIFFmalloc(TIFFStripSize(tif));
2587 strip_size=TIFFStripSize(tif);
2589 imgsize = image->comps[0].w * image->comps[0].h ;
2590 /* Read the Image components
2592 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2594 unsigned char *dat8;
2597 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2598 dat8 = (unsigned char*)buf;
2602 step = 6 + has_alpha + has_alpha;
2604 for(i = 0; i < ssize; i += step)
2608 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */
2609 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
2610 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
2612 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2614 if(parameters->cp_cinema)
2616 /* Rounding 16 to 12 bits
2618 image->comps[0].data[index] =
2619 (image->comps[0].data[index] + 0x08) >> 4 ;
2620 image->comps[1].data[index] =
2621 (image->comps[1].data[index] + 0x08) >> 4 ;
2622 image->comps[2].data[index] =
2623 (image->comps[2].data[index] + 0x08) >> 4 ;
2625 image->comps[3].data[index] =
2626 (image->comps[3].data[index] + 0x08) >> 4 ;
2633 }/*if(tiBps == 16)*/
2637 step = 3 + has_alpha;
2639 for(i = 0; i < ssize; i += step)
2643 image->comps[0].data[index] = dat8[i+0];/* R */
2644 image->comps[1].data[index] = dat8[i+1];/* G */
2645 image->comps[2].data[index] = dat8[i+2];/* B */
2647 image->comps[3].data[index] = dat8[i+3];
2649 if(parameters->cp_cinema)
2651 /* Rounding 8 to 12 bits
2653 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2654 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2655 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2657 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2664 }/*if( tiBps == 8)*/
2666 if(tiBps == 12)/* CINEMA file */
2670 for(i = 0; i < ssize; i += step)
2672 if((index < imgsize)&(index+1 < imgsize))
2674 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2675 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2677 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2678 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2680 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2681 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2689 }/*for(strip = 0; )*/
2697 if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2699 numcomps = 1 + has_alpha;
2700 color_space = CLRSPC_GRAY;
2702 for(j = 0; j < numcomps; ++j)
2704 cmptparm[j].prec = tiBps;
2705 cmptparm[j].bpp = tiBps;
2706 cmptparm[j].dx = subsampling_dx;
2707 cmptparm[j].dy = subsampling_dy;
2711 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2718 /* set image offset and reference grid
2720 image->x0 = parameters->image_offset_x0;
2721 image->y0 = parameters->image_offset_y0;
2722 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2723 image->x0 + (w - 1) * subsampling_dx + 1;
2724 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2725 image->y0 + (h - 1) * subsampling_dy + 1;
2727 buf = _TIFFmalloc(TIFFStripSize(tif));
2729 strip_size = TIFFStripSize(tif);
2731 imgsize = image->comps[0].w * image->comps[0].h ;
2732 /* Read the Image components
2734 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2736 unsigned char *dat8;
2740 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2741 dat8 = (unsigned char*)buf;
2745 step = 2 + has_alpha + has_alpha;
2747 for(i = 0; i < ssize; i += step)
2751 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2753 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2763 step = 1 + has_alpha;
2765 for(i = 0; i < ssize; i += step)
2769 image->comps[0].data[index] = dat8[i+0];
2771 image->comps[1].data[index] = dat8[i+1];
2789 #endif /* OPJ_HAVE_LIBTIFF */
2791 /* -->> -->> -->> -->>
2795 <<-- <<-- <<-- <<-- */
2797 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2798 int subsampling_dx = parameters->subsampling_dx;
2799 int subsampling_dy = parameters->subsampling_dy;
2802 int i, compno, numcomps, w, h;
2803 OPJ_COLOR_SPACE color_space;
2804 opj_image_cmptparm_t *cmptparm;
2805 opj_image_t * image = NULL;
2808 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2810 fprintf(stderr,"\nError: invalid raw image parameters\n");
2811 fprintf(stderr,"Please use the Format option -F:\n");
2812 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2813 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2814 fprintf(stderr,"Aborting\n");
2818 f = fopen(filename, "rb");
2820 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2821 fprintf(stderr,"Aborting\n");
2824 numcomps = raw_cp->rawComp;
2825 color_space = CLRSPC_SRGB;
2826 w = raw_cp->rawWidth;
2827 h = raw_cp->rawHeight;
2828 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2830 /* initialize image components */
2831 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2832 for(i = 0; i < numcomps; i++) {
2833 cmptparm[i].prec = raw_cp->rawBitDepth;
2834 cmptparm[i].bpp = raw_cp->rawBitDepth;
2835 cmptparm[i].sgnd = raw_cp->rawSigned;
2836 cmptparm[i].dx = subsampling_dx;
2837 cmptparm[i].dy = subsampling_dy;
2841 /* create the image */
2842 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2847 /* set image offset and reference grid */
2848 image->x0 = parameters->image_offset_x0;
2849 image->y0 = parameters->image_offset_y0;
2850 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2851 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2853 if(raw_cp->rawBitDepth <= 8)
2855 unsigned char value = 0;
2856 for(compno = 0; compno < numcomps; compno++) {
2857 for (i = 0; i < w * h; i++) {
2858 if (!fread(&value, 1, 1, f)) {
2859 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2863 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2867 else if(raw_cp->rawBitDepth <= 16)
2869 unsigned short value;
2870 for(compno = 0; compno < numcomps; compno++) {
2871 for (i = 0; i < w * h; i++) {
2873 if (!fread(&temp, 1, 1, f)) {
2874 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2879 if (!fread(&temp, 1, 1, f)) {
2880 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2885 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2890 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2895 if (fread(&ch, 1, 1, f)) {
2896 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2903 int imagetoraw(opj_image_t * image, const char *outfile)
2905 FILE *rawFile = NULL;
2912 if((image->numcomps * image->x1 * image->y1) == 0)
2914 fprintf(stderr,"\nError: invalid raw image parameters\n");
2918 rawFile = fopen(outfile, "wb");
2920 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2924 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2926 for(compno = 0; compno < image->numcomps; compno++)
2928 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2929 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2931 w = image->comps[compno].w;
2932 h = image->comps[compno].h;
2934 if(image->comps[compno].prec <= 8)
2936 if(image->comps[compno].sgnd == 1)
2939 int mask = (1 << image->comps[compno].prec) - 1;
2940 ptr = image->comps[compno].data;
2941 for (line = 0; line < h; line++) {
2942 for(row = 0; row < w; row++) {
2943 curr = (signed char) (*ptr & mask);
2944 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
2946 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2953 else if(image->comps[compno].sgnd == 0)
2956 int mask = (1 << image->comps[compno].prec) - 1;
2957 ptr = image->comps[compno].data;
2958 for (line = 0; line < h; line++) {
2959 for(row = 0; row < w; row++) {
2960 curr = (unsigned char) (*ptr & mask);
2961 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2963 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2971 else if(image->comps[compno].prec <= 16)
2973 if(image->comps[compno].sgnd == 1)
2975 signed short int curr;
2976 int mask = (1 << image->comps[compno].prec) - 1;
2977 ptr = image->comps[compno].data;
2978 for (line = 0; line < h; line++) {
2979 for(row = 0; row < w; row++) {
2981 curr = (signed short int) (*ptr & mask);
2982 temp = (unsigned char) (curr >> 8);
2983 res = fwrite(&temp, 1, 1, rawFile);
2985 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2988 temp = (unsigned char) curr;
2989 res = fwrite(&temp, 1, 1, rawFile);
2991 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2998 else if(image->comps[compno].sgnd == 0)
3000 unsigned short int curr;
3001 int mask = (1 << image->comps[compno].prec) - 1;
3002 ptr = image->comps[compno].data;
3003 for (line = 0; line < h; line++) {
3004 for(row = 0; row < w; row++) {
3006 curr = (unsigned short int) (*ptr & mask);
3007 temp = (unsigned char) (curr >> 8);
3008 res = fwrite(&temp, 1, 1, rawFile);
3010 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3013 temp = (unsigned char) curr;
3014 res = fwrite(&temp, 1, 1, rawFile);
3016 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3024 else if (image->comps[compno].prec <= 32)
3026 fprintf(stderr,"More than 16 bits per component no handled yet\n");
3031 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
3039 #ifdef OPJ_HAVE_LIBPNG
3041 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3042 #define MAGIC_SIZE 8
3043 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3045 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3049 double gamma, display_exponent;
3050 int bit_depth, interlace_type,compression_type, filter_type;
3052 png_uint_32 resx, resy;
3054 png_uint_32 width, height;
3055 int color_type, has_alpha, is16;
3058 unsigned char **rows;
3061 opj_image_cmptparm_t cmptparm[4];
3063 unsigned int nr_comp;
3065 unsigned char sigbuf[8];
3067 if((reader = fopen(read_idf, "rb")) == NULL)
3069 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
3072 image = NULL; png = NULL; rows = NULL;
3074 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3075 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
3077 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
3080 /* libpng-VERSION/example.c:
3081 * PC : screen_gamma = 2.2;
3082 * Mac: screen_gamma = 1.7 or 1.0;
3084 display_exponent = 2.2;
3086 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3087 NULL, NULL, NULL)) == NULL)
3089 if((info = png_create_info_struct(png)) == NULL)
3092 if(setjmp(png_jmpbuf(png)))
3095 png_init_io(png, reader);
3096 png_set_sig_bytes(png, MAGIC_SIZE);
3098 png_read_info(png, info);
3100 if(png_get_IHDR(png, info, &width, &height,
3101 &bit_depth, &color_type, &interlace_type,
3102 &compression_type, &filter_type) == 0)
3105 /* png_set_expand():
3106 * expand paletted images to RGB, expand grayscale images of
3107 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3108 * to alpha channels.
3110 if(color_type == PNG_COLOR_TYPE_PALETTE)
3111 png_set_expand(png);
3113 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3114 png_set_expand(png);
3116 if(png_get_valid(png, info, PNG_INFO_tRNS))
3117 png_set_expand(png);
3119 is16 = (bit_depth == 16);
3121 /* GRAY => RGB; GRAY_ALPHA => RGBA
3123 if(color_type == PNG_COLOR_TYPE_GRAY
3124 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3126 png_set_gray_to_rgb(png);
3128 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
3129 PNG_COLOR_TYPE_RGB_ALPHA);
3131 if( !png_get_gAMA(png, info, &gamma))
3134 png_set_gamma(png, display_exponent, gamma);
3136 png_read_update_info(png, info);
3138 png_get_pHYs(png, info, &resx, &resy, &unit);
3140 color_type = png_get_color_type(png, info);
3142 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3144 nr_comp = 3 + has_alpha;
3146 bit_depth = png_get_bit_depth(png, info);
3148 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
3149 for(i = 0; i < height; ++i)
3150 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
3152 png_read_image(png, rows);
3154 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3156 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3158 for(i = 0; i < nr_comp; ++i)
3160 cmptparm[i].prec = bit_depth;
3161 /* bits_per_pixel: 8 or 16 */
3162 cmptparm[i].bpp = bit_depth;
3163 cmptparm[i].sgnd = 0;
3164 cmptparm[i].dx = sub_dx;
3165 cmptparm[i].dy = sub_dy;
3166 cmptparm[i].w = width;
3167 cmptparm[i].h = height;
3170 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3172 if(image == NULL) goto fin;
3174 image->x0 = params->image_offset_x0;
3175 image->y0 = params->image_offset_y0;
3176 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3177 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3179 r = image->comps[0].data;
3180 g = image->comps[1].data;
3181 b = image->comps[2].data;
3182 a = image->comps[3].data;
3184 for(i = 0; i < height; ++i)
3188 for(j = 0; j < width; ++j)
3192 *r++ = s[0]<<8|s[1]; s += 2;
3194 *g++ = s[0]<<8|s[1]; s += 2;
3196 *b++ = s[0]<<8|s[1]; s += 2;
3198 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3202 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3204 if(has_alpha) *a++ = *s++;
3210 for(i = 0; i < height; ++i)
3215 png_destroy_read_struct(&png, &info, NULL);
3223 int imagetopng(opj_image_t * image, const char *write_idf)
3228 int *red, *green, *blue, *alpha;
3229 unsigned char *row_buf, *d;
3230 int has_alpha, width, height, nr_comp, color_type;
3231 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3232 int prec, ushift, dshift, is16, force16, force8;
3233 unsigned short mask = 0xffff;
3234 png_color_8 sig_bit;
3236 is16 = force16 = force8 = ushift = dshift = 0; fails = 1;
3237 prec = image->comps[0].prec;
3238 nr_comp = image->numcomps;
3240 if(prec > 8 && prec < 16)
3242 ushift = 16 - prec; dshift = prec - ushift;
3243 prec = 16; force16 = 1;
3246 if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */
3248 ushift = 8 - prec; dshift = 8 - ushift;
3249 prec = 8; force8 = 1;
3252 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3254 fprintf(stderr,"imagetopng: can not create %s"
3255 "\n\twrong bit_depth %d\n", write_idf, prec);
3258 writer = fopen(write_idf, "wb");
3260 if(writer == NULL) return fails;
3262 info = NULL; has_alpha = 0;
3264 /* Create and initialize the png_struct with the desired error handler
3265 * functions. If you want to use the default stderr and longjump method,
3266 * you can supply NULL for the last three parameters. We also check that
3267 * the library version is compatible with the one used at compile time,
3268 * in case we are using dynamically linked libraries. REQUIRED.
3270 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3272 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3274 if(png == NULL) goto fin;
3276 /* Allocate/initialize the image information data. REQUIRED
3278 info = png_create_info_struct(png);
3280 if(info == NULL) goto fin;
3282 /* Set error handling. REQUIRED if you are not supplying your own
3283 * error handling functions in the png_create_write_struct() call.
3285 if(setjmp(png_jmpbuf(png))) goto fin;
3287 /* I/O initialization functions is REQUIRED
3289 png_init_io(png, writer);
3291 /* Set the image information here. Width and height are up to 2^31,
3292 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3293 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3294 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3295 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3296 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3297 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3302 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3303 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3304 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3305 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3308 png_set_compression_level(png, Z_BEST_COMPRESSION);
3310 if(prec == 16) mask = 0xffff;
3312 if(prec == 8) mask = 0x00ff;
3314 if(prec == 4) mask = 0x000f;
3316 if(prec == 2) mask = 0x0003;
3318 if(prec == 1) mask = 0x0001;
3321 && image->comps[0].dx == image->comps[1].dx
3322 && image->comps[1].dx == image->comps[2].dx
3323 && image->comps[0].dy == image->comps[1].dy
3324 && image->comps[1].dy == image->comps[2].dy
3325 && image->comps[0].prec == image->comps[1].prec
3326 && image->comps[1].prec == image->comps[2].prec)
3330 has_alpha = (nr_comp > 3);
3332 is16 = (prec == 16);
3334 width = image->comps[0].w;
3335 height = image->comps[0].h;
3337 red = image->comps[0].data;
3338 green = image->comps[1].data;
3339 blue = image->comps[2].data;
3341 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3345 sig_bit.alpha = prec;
3346 alpha = image->comps[3].data;
3347 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3348 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3352 sig_bit.alpha = 0; alpha = NULL;
3353 color_type = PNG_COLOR_TYPE_RGB;
3356 png_set_sBIT(png, info, &sig_bit);
3358 png_set_IHDR(png, info, width, height, prec,
3361 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3362 /*=============================*/
3363 png_write_info(png, info);
3364 /*=============================*/
3367 png_set_packing(png);
3369 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3370 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3371 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3373 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3375 for(y = 0; y < height; ++y)
3379 for(x = 0; x < width; ++x)
3383 v = *red + adjustR; ++red;
3385 if(force16) { v = (v<<ushift) + (v>>dshift); }
3387 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3389 v = *green + adjustG; ++green;
3391 if(force16) { v = (v<<ushift) + (v>>dshift); }
3393 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3395 v = *blue + adjustB; ++blue;
3397 if(force16) { v = (v<<ushift) + (v>>dshift); }
3399 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3403 v = *alpha + adjustA; ++alpha;
3405 if(force16) { v = (v<<ushift) + (v>>dshift); }
3407 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3412 v = *red + adjustR; ++red;
3414 if(force8) { v = (v<<ushift) + (v>>dshift); }
3416 *d++ = (unsigned char)(v & mask);
3418 v = *green + adjustG; ++green;
3420 if(force8) { v = (v<<ushift) + (v>>dshift); }
3422 *d++ = (unsigned char)(v & mask);
3424 v = *blue + adjustB; ++blue;
3426 if(force8) { v = (v<<ushift) + (v>>dshift); }
3428 *d++ = (unsigned char)(v & mask);
3432 v = *alpha + adjustA; ++alpha;
3434 if(force8) { v = (v<<ushift) + (v>>dshift); }
3436 *d++ = (unsigned char)(v & mask);
3440 png_write_row(png, row_buf);
3447 if(nr_comp == 1 /* GRAY */
3448 || ( nr_comp == 2 /* GRAY_ALPHA */
3449 && image->comps[0].dx == image->comps[1].dx
3450 && image->comps[0].dy == image->comps[1].dy
3451 && image->comps[0].prec == image->comps[1].prec))
3455 red = image->comps[0].data;
3457 sig_bit.gray = prec;
3458 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3459 alpha = NULL; adjustA = 0;
3460 color_type = PNG_COLOR_TYPE_GRAY;
3464 has_alpha = 1; sig_bit.alpha = prec;
3465 alpha = image->comps[1].data;
3466 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3467 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3469 width = image->comps[0].w;
3470 height = image->comps[0].h;
3472 png_set_IHDR(png, info, width, height, sig_bit.gray,
3475 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3477 png_set_sBIT(png, info, &sig_bit);
3478 /*=============================*/
3479 png_write_info(png, info);
3480 /*=============================*/
3481 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3485 png_set_packing(png);
3490 row_buf = (unsigned char*)
3491 malloc(width * nr_comp * sizeof(unsigned short));
3493 for(y = 0; y < height; ++y)
3497 for(x = 0; x < width; ++x)
3499 v = *red + adjustR; ++red;
3501 if(force16) { v = (v<<ushift) + (v>>dshift); }
3503 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3509 if(force16) { v = (v<<ushift) + (v>>dshift); }
3511 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3514 png_write_row(png, row_buf);
3519 else /* prec <= 8 */
3521 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3523 for(y = 0; y < height; ++y)
3527 for(x = 0; x < width; ++x)
3529 v = *red + adjustR; ++red;
3531 if(force8) { v = (v<<ushift) + (v>>dshift); }
3533 *d++ = (unsigned char)(v & mask);
3537 v = *alpha + adjustA; ++alpha;
3539 if(force8) { v = (v<<ushift) + (v>>dshift); }
3541 *d++ = (unsigned char)(v & mask);
3545 png_write_row(png, row_buf);
3553 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3556 png_write_end(png, info);
3564 png_destroy_write_struct(&png, &info);
3568 if(fails) remove(write_idf);
3572 #endif /* OPJ_HAVE_LIBPNG */