2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2006-2007, Parvatha Elangovan
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_config.h"
41 #endif /* HAVE_LIBTIFF */
46 #endif /* HAVE_LIBPNG */
52 * Get logarithm of an integer and round downwards.
56 static int int_floorlog2(int a) {
58 for (l = 0; a > 1; l++) {
64 /* -->> -->> -->> -->>
68 <<-- <<-- <<-- <<-- */
70 #ifdef INFORMATION_ONLY
71 /* TGA header definition. */
74 unsigned char id_length; /* Image id field length */
75 unsigned char colour_map_type; /* Colour map type */
76 unsigned char image_type; /* Image type */
78 ** Colour map specification
80 unsigned short colour_map_index; /* First entry index */
81 unsigned short colour_map_length; /* Colour map length */
82 unsigned char colour_map_entry_size; /* Colour map entry size */
84 ** Image specification
86 unsigned short x_origin; /* x origin of image */
87 unsigned short y_origin; /* u origin of image */
88 unsigned short image_width; /* Image width */
89 unsigned short image_height; /* Image height */
90 unsigned char pixel_depth; /* Pixel depth */
91 unsigned char image_desc; /* Image descriptor */
93 #endif /* INFORMATION_ONLY */
95 static unsigned short get_ushort(unsigned short val) {
97 #ifdef WORDS_BIGENDIAN
98 return( ((val & 0xff) << 8) + (val >> 8) );
105 #define TGA_HEADER_SIZE 18
107 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
108 unsigned int *width, unsigned int *height, int *flip_image)
112 unsigned char id_len, cmap_type, image_type;
113 unsigned char pixel_depth, image_desc;
114 unsigned short cmap_index, cmap_len, cmap_entry_size;
115 unsigned short x_origin, y_origin, image_w, image_h;
117 if (!bits_per_pixel || !width || !height || !flip_image)
119 tga = (unsigned char*)malloc(18);
121 if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 )
123 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
126 id_len = (unsigned char)tga[0];
127 cmap_type = (unsigned char)tga[1];
128 image_type = (unsigned char)tga[2];
129 cmap_index = get_ushort(*(unsigned short*)(&tga[3]));
130 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
131 cmap_entry_size = (unsigned char)tga[7];
134 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
135 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
136 image_w = get_ushort(*(unsigned short*)(&tga[12]));
137 image_h = get_ushort(*(unsigned short*)(&tga[14]));
138 pixel_depth = (unsigned char)tga[16];
139 image_desc = (unsigned char)tga[17];
143 *bits_per_pixel = (unsigned int)pixel_depth;
144 *width = (unsigned int)image_w;
145 *height = (unsigned int)image_h;
147 /* Ignore tga identifier, if present ... */
150 unsigned char *id = (unsigned char *) malloc(id_len);
151 if ( !fread(id, id_len, 1, fp) )
153 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
160 /* Test for compressed formats ... not yet supported ...
161 // Note :- 9 - RLE encoded palettized.
162 // 10 - RLE encoded RGB. */
165 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
169 *flip_image = !(image_desc & 32);
171 /* Palettized formats are not yet supported, skip over the palette, if present ... */
172 palette_size = cmap_len * (cmap_entry_size/8);
176 fprintf(stderr, "File contains a palette - not yet supported.");
177 fseek(fp, palette_size, SEEK_CUR);
182 #if WORDS_BIGENDIAN == 1
184 static inline int16_t swap16(int16_t x)
186 return((((u_int16_t)x & 0x00ffU) << 8) |
187 (((u_int16_t)x & 0xff00U) >> 8));
192 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
195 unsigned short image_w, image_h, us0;
196 unsigned char uc0, image_type;
197 unsigned char pixel_depth, image_desc;
199 if (!bits_per_pixel || !width || !height)
204 if ( bits_per_pixel < 256 )
205 pixel_depth = (unsigned char)bits_per_pixel;
207 fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
212 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */
213 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */
215 image_type = 2; /* Uncompressed. */
216 if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
219 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */
220 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */
221 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */
223 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */
224 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */
226 image_w = (unsigned short)width;
227 image_h = (unsigned short) height;
229 #if WORDS_BIGENDIAN == 0
230 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
231 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
233 image_w = swap16(image_w);
234 image_h = swap16(image_h);
235 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
236 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
239 if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
241 image_desc = 8; /* 8 bits per component. */
245 if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails;
250 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
254 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
257 unsigned int image_width, image_height, pixel_bit_depth;
260 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
262 OPJ_COLOR_SPACE color_space;
265 int subsampling_dx, subsampling_dy;
268 f = fopen(filename, "rb");
270 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
274 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
277 /* We currently only support 24 & 32 bit tga's ... */
278 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
281 /* initialize image components */
282 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
284 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */
285 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
288 color_space = OPJ_CLRSPC_GRAY;
289 numcomps = save_alpha ? 2 : 1;
292 numcomps = save_alpha ? 4 : 3;
293 color_space = OPJ_CLRSPC_SRGB;
296 subsampling_dx = parameters->subsampling_dx;
297 subsampling_dy = parameters->subsampling_dy;
299 for (i = 0; i < numcomps; i++) {
300 cmptparm[i].prec = 8;
302 cmptparm[i].sgnd = 0;
303 cmptparm[i].dx = subsampling_dx;
304 cmptparm[i].dy = subsampling_dy;
305 cmptparm[i].w = image_width;
306 cmptparm[i].h = image_height;
309 /* create the image */
310 image = opj_image_create(numcomps, &cmptparm[0], color_space);
315 /* set image offset and reference grid */
316 image->x0 = parameters->image_offset_x0;
317 image->y0 = parameters->image_offset_y0;
318 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
319 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
322 for (y=0; y < image_height; y++)
327 index = (image_height-y-1)*image_width;
329 index = y*image_width;
333 for (x=0;x<image_width;x++)
337 if( !fread(&b, 1, 1, f) )
339 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
340 opj_image_destroy(image);
343 if ( !fread(&g, 1, 1, f) )
345 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
346 opj_image_destroy(image);
349 if ( !fread(&r, 1, 1, f) )
351 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
352 opj_image_destroy(image);
356 image->comps[0].data[index]=r;
357 image->comps[1].data[index]=g;
358 image->comps[2].data[index]=b;
362 else if (numcomps==4)
364 for (x=0;x<image_width;x++)
366 unsigned char r,g,b,a;
367 if ( !fread(&b, 1, 1, f) )
369 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
370 opj_image_destroy(image);
373 if ( !fread(&g, 1, 1, f) )
375 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
376 opj_image_destroy(image);
379 if ( !fread(&r, 1, 1, f) )
381 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
382 opj_image_destroy(image);
385 if ( !fread(&a, 1, 1, f) )
387 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
388 opj_image_destroy(image);
392 image->comps[0].data[index]=r;
393 image->comps[1].data[index]=g;
394 image->comps[2].data[index]=b;
395 image->comps[3].data[index]=a;
400 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
406 int imagetotga(opj_image_t * image, const char *outfile) {
407 int width, height, bpp, x, y;
408 OPJ_BOOL write_alpha;
409 int i, adjustR, adjustG, adjustB;
410 unsigned int alpha_channel;
417 fdest = fopen(outfile, "wb");
419 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
423 for (i = 0; i < image->numcomps-1; i++) {
424 if ((image->comps[0].dx != image->comps[i+1].dx)
425 ||(image->comps[0].dy != image->comps[i+1].dy)
426 ||(image->comps[0].prec != image->comps[i+1].prec)) {
427 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
432 width = image->comps[0].w;
433 height = image->comps[0].h;
435 /* Mono with alpha, or RGB with alpha. */
436 write_alpha = (image->numcomps==2) || (image->numcomps==4);
438 /* Write TGA header */
439 bpp = write_alpha ? 32 : 24;
440 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
443 alpha_channel = image->numcomps-1;
445 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
447 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
448 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
449 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
451 for (y=0; y < height; y++) {
452 unsigned int index=y*width;
454 for (x=0; x < width; x++, index++) {
455 r = (float)(image->comps[0].data[index] + adjustR);
457 if (image->numcomps>2) {
458 g = (float)(image->comps[1].data[index] + adjustG);
459 b = (float)(image->comps[2].data[index] + adjustB);
461 else {/* Greyscale ... */
466 /* TGA format writes BGR ... */
467 value = (unsigned char)(b*scale);
468 res = fwrite(&value,1,1,fdest);
470 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
474 value = (unsigned char)(g*scale);
475 res = fwrite(&value,1,1,fdest);
477 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
481 value = (unsigned char)(r*scale);
482 res = fwrite(&value,1,1,fdest);
484 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
489 a = (float)(image->comps[alpha_channel].data[index]);
490 value = (unsigned char)(a*scale);
491 res = fwrite(&value,1,1,fdest);
493 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
503 /* -->> -->> -->> -->>
507 <<-- <<-- <<-- <<-- */
509 /* WORD defines a two byte word */
510 typedef unsigned short int WORD;
512 /* DWORD defines a four byte word */
513 typedef unsigned int DWORD;
516 WORD bfType; /* 'BM' for Bitmap (19776) */
517 DWORD bfSize; /* Size of the file */
518 WORD bfReserved1; /* Reserved : 0 */
519 WORD bfReserved2; /* Reserved : 0 */
520 DWORD bfOffBits; /* Offset */
521 } BITMAPFILEHEADER_t;
524 DWORD biSize; /* Size of the structure in bytes */
525 DWORD biWidth; /* Width of the image in pixels */
526 DWORD biHeight; /* Heigth of the image in pixels */
527 WORD biPlanes; /* 1 */
528 WORD biBitCount; /* Number of color bits by pixels */
529 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
530 DWORD biSizeImage; /* Size of the image in bytes */
531 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
532 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
533 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
534 DWORD biClrImportant; /* Number of important color (0: ALL) */
535 } BITMAPINFOHEADER_t;
537 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
539 int subsampling_dx = parameters->subsampling_dx;
540 int subsampling_dy = parameters->subsampling_dy;
542 int i, numcomps, w, h;
543 OPJ_COLOR_SPACE color_space;
544 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
545 opj_image_t * image = NULL;
548 BITMAPFILEHEADER_t File_h;
549 BITMAPINFOHEADER_t Info_h;
551 unsigned char *table_R, *table_G, *table_B;
552 unsigned int j, PAD = 0;
559 IN = fopen(filename, "rb");
562 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
566 File_h.bfType = getc(IN);
567 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
569 if (File_h.bfType != 19778)
571 fprintf(stderr,"Error, not a BMP file!\n");
577 File_h.bfSize = getc(IN);
578 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
579 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
580 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
582 File_h.bfReserved1 = getc(IN);
583 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
585 File_h.bfReserved2 = getc(IN);
586 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
588 File_h.bfOffBits = getc(IN);
589 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
590 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
591 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
596 Info_h.biSize = getc(IN);
597 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
598 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
599 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
601 if(Info_h.biSize != 40)
603 fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize);
607 Info_h.biWidth = getc(IN);
608 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
609 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
610 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
613 Info_h.biHeight = getc(IN);
614 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
615 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
616 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
619 Info_h.biPlanes = getc(IN);
620 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
622 Info_h.biBitCount = getc(IN);
623 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
625 Info_h.biCompression = getc(IN);
626 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
627 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
628 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
630 Info_h.biSizeImage = getc(IN);
631 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
632 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
633 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
635 Info_h.biXpelsPerMeter = getc(IN);
636 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
637 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
638 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
640 Info_h.biYpelsPerMeter = getc(IN);
641 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
642 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
643 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
645 Info_h.biClrUsed = getc(IN);
646 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
647 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
648 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
650 Info_h.biClrImportant = getc(IN);
651 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
652 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
653 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
655 /* Read the data and store them in the OUT file */
657 if (Info_h.biBitCount == 24)
660 color_space = OPJ_CLRSPC_SRGB;
661 /* initialize image components */
662 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
663 for(i = 0; i < numcomps; i++)
665 cmptparm[i].prec = 8;
667 cmptparm[i].sgnd = 0;
668 cmptparm[i].dx = subsampling_dx;
669 cmptparm[i].dy = subsampling_dy;
673 /* create the image */
674 image = opj_image_create(numcomps, &cmptparm[0], color_space);
681 /* set image offset and reference grid */
682 image->x0 = parameters->image_offset_x0;
683 image->y0 = parameters->image_offset_y0;
684 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
685 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
689 /* Place the cursor at the beginning of the image information */
690 fseek(IN, 0, SEEK_SET);
691 fseek(IN, File_h.bfOffBits, SEEK_SET);
696 /* PAD = 4 - (3 * W) % 4; */
697 /* PAD = (PAD == 4) ? 0 : PAD; */
698 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
700 RGB = (unsigned char *)
701 malloc((3 * W + PAD) * H * sizeof(unsigned char));
703 if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
706 opj_image_destroy(image);
707 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
713 for(y = 0; y < (int)H; y++)
715 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
716 for(x = 0; x < (int)W; x++)
718 unsigned char *pixel = &scanline[3 * x];
719 image->comps[0].data[index] = pixel[2]; /* R */
720 image->comps[1].data[index] = pixel[1]; /* G */
721 image->comps[2].data[index] = pixel[0]; /* B */
726 }/* if (Info_h.biBitCount == 24) */
728 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
730 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
732 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
734 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
735 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
736 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
739 for (j = 0; j < Info_h.biClrUsed; j++)
741 table_B[j] = (unsigned char)getc(IN);
742 table_G[j] = (unsigned char)getc(IN);
743 table_R[j] = (unsigned char)getc(IN);
746 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
748 if(has_color) gray_scale = 0;
750 /* Place the cursor at the beginning of the image information */
751 fseek(IN, 0, SEEK_SET);
752 fseek(IN, File_h.bfOffBits, SEEK_SET);
756 if (Info_h.biWidth % 2)
759 numcomps = gray_scale ? 1 : 3;
760 color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB;
761 /* initialize image components */
762 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
763 for(i = 0; i < numcomps; i++)
765 cmptparm[i].prec = 8;
767 cmptparm[i].sgnd = 0;
768 cmptparm[i].dx = subsampling_dx;
769 cmptparm[i].dy = subsampling_dy;
773 /* create the image */
774 image = opj_image_create(numcomps, &cmptparm[0], color_space);
778 free(table_R); free(table_G); free(table_B);
782 /* set image offset and reference grid */
783 image->x0 = parameters->image_offset_x0;
784 image->y0 = parameters->image_offset_y0;
785 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
786 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
790 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
792 if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
798 opj_image_destroy(image);
799 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
805 for (j = 0; j < W * H; j++)
807 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
809 image->comps[0].data[index] =
810 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
819 for (j = 0; j < W * H; j++)
821 if ((j % W < W - 1 && Info_h.biWidth % 2)
822 || !(Info_h.biWidth % 2))
824 unsigned char pixel_index =
825 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
826 image->comps[0].data[index] = table_R[pixel_index];
827 image->comps[1].data[index] = table_G[pixel_index];
828 image->comps[2].data[index] = table_B[pixel_index];
839 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
841 unsigned char *pix, *beyond;
842 int *gray, *red, *green, *blue;
843 unsigned int x, y, max;
847 if (Info_h.biClrUsed == 0)
848 Info_h.biClrUsed = 256;
849 else if (Info_h.biClrUsed > 256)
850 Info_h.biClrUsed = 256;
852 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
853 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
854 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
857 for (j = 0; j < Info_h.biClrUsed; j++)
859 table_B[j] = (unsigned char)getc(IN);
860 table_G[j] = (unsigned char)getc(IN);
861 table_R[j] = (unsigned char)getc(IN);
863 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
869 numcomps = gray_scale ? 1 : 3;
870 color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB;
871 /* initialize image components */
872 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
873 for (i = 0; i < numcomps; i++)
875 cmptparm[i].prec = 8;
877 cmptparm[i].sgnd = 0;
878 cmptparm[i].dx = subsampling_dx;
879 cmptparm[i].dy = subsampling_dy;
883 /* create the image */
884 image = opj_image_create(numcomps, &cmptparm[0], color_space);
894 /* set image offset and reference grid */
895 image->x0 = parameters->image_offset_x0;
896 image->y0 = parameters->image_offset_y0;
897 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
898 - 1) * subsampling_dx + 1;
899 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
900 - 1) * subsampling_dy + 1;
904 /* Place the cursor at the beginning of the image information */
905 fseek(IN, 0, SEEK_SET);
906 fseek(IN, File_h.bfOffBits, SEEK_SET);
910 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
911 beyond = RGB + W * H;
923 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
924 *pix = (unsigned char)c1;
930 if (c == 0x00) /* EOL */
934 pix = RGB + x + (H - y - 1) * W;
936 else if (c == 0x01) /* EOP */
938 else if (c == 0x02) /* MOVE by dxdy */
944 pix = RGB + (H - y - 1) * W + x;
949 for (; i < c && x < W && pix < beyond; i++, x++, pix++)
952 *pix = (unsigned char)c1;
954 if (c & 1) /* skip padding byte */
962 gray = image->comps[0].data;
970 *gray++ = table_R[uc];
975 /*int *red, *green, *blue;*/
977 red = image->comps[0].data;
978 green = image->comps[1].data;
979 blue = image->comps[2].data;
987 *red++ = table_R[uc];
988 *green++ = table_G[uc];
989 *blue++ = table_B[uc];
1000 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1001 "is not yet implemented [%d]\n", Info_h.biBitCount);
1007 int imagetobmp(opj_image_t * image, const char *outfile) {
1011 int adjustR, adjustG, adjustB;
1013 if (image->comps[0].prec < 8) {
1014 fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec);
1017 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1018 && image->comps[1].dx == image->comps[2].dx
1019 && image->comps[0].dy == image->comps[1].dy
1020 && image->comps[1].dy == image->comps[2].dy
1021 && image->comps[0].prec == image->comps[1].prec
1022 && image->comps[1].prec == image->comps[2].prec) {
1024 /* -->> -->> -->> -->>
1026 <<-- <<-- <<-- <<-- */
1028 fdest = fopen(outfile, "wb");
1030 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1034 w = image->comps[0].w;
1035 h = image->comps[0].h;
1037 fprintf(fdest, "BM");
1041 fprintf(fdest, "%c%c%c%c",
1042 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1043 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1044 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1045 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1046 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1047 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
1051 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1052 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1053 (unsigned char) ((w) >> 8) & 0xff,
1054 (unsigned char) ((w) >> 16) & 0xff,
1055 (unsigned char) ((w) >> 24) & 0xff);
1056 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1057 (unsigned char) ((h) >> 8) & 0xff,
1058 (unsigned char) ((h) >> 16) & 0xff,
1059 (unsigned char) ((h) >> 24) & 0xff);
1060 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1061 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1062 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1063 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
1064 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1065 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1066 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1067 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1068 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1069 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1070 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1072 if (image->comps[0].prec > 8) {
1073 adjustR = image->comps[0].prec - 8;
1074 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1078 if (image->comps[1].prec > 8) {
1079 adjustG = image->comps[1].prec - 8;
1080 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1084 if (image->comps[2].prec > 8) {
1085 adjustB = image->comps[2].prec - 8;
1086 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1091 for (i = 0; i < w * h; i++) {
1092 unsigned char rc, gc, bc;
1095 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1096 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1097 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1098 if(r > 255) r = 255; else if(r < 0) r = 0;
1099 rc = (unsigned char)r;
1101 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1102 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1103 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
1104 if(g > 255) g = 255; else if(g < 0) g = 0;
1105 gc = (unsigned char)g;
1107 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1108 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1109 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
1110 if(b > 255) b = 255; else if(b < 0) b = 0;
1111 bc = (unsigned char)b;
1113 fprintf(fdest, "%c%c%c", bc, gc, rc);
1115 if ((i + 1) % w == 0) {
1116 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
1117 fprintf(fdest, "%c", 0);
1121 } else { /* Gray-scale */
1123 /* -->> -->> -->> -->>
1124 8 bits non code (Gray scale)
1125 <<-- <<-- <<-- <<-- */
1127 fdest = fopen(outfile, "wb");
1128 w = image->comps[0].w;
1129 h = image->comps[0].h;
1131 fprintf(fdest, "BM");
1135 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1136 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1137 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1138 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1139 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1140 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1141 ((54 + 1024) >> 16) & 0xff,
1142 ((54 + 1024) >> 24) & 0xff);
1146 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1147 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1148 (unsigned char) ((w) >> 8) & 0xff,
1149 (unsigned char) ((w) >> 16) & 0xff,
1150 (unsigned char) ((w) >> 24) & 0xff);
1151 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1152 (unsigned char) ((h) >> 8) & 0xff,
1153 (unsigned char) ((h) >> 16) & 0xff,
1154 (unsigned char) ((h) >> 24) & 0xff);
1155 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1156 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1157 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1158 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
1159 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
1160 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
1161 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
1162 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1163 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1164 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1165 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1167 if (image->comps[0].prec > 8) {
1168 adjustR = image->comps[0].prec - 8;
1169 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1173 for (i = 0; i < 256; i++) {
1174 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1177 for (i = 0; i < w * h; i++) {
1180 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1181 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1182 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1183 if(r > 255) r = 255; else if(r < 0) r = 0;
1185 fprintf(fdest, "%c", (unsigned char)r);
1187 if ((i + 1) % w == 0) {
1188 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1189 fprintf(fdest, "%c", 0);
1198 /* -->> -->> -->> -->>
1202 <<-- <<-- <<-- <<-- */
1205 static unsigned char readuchar(FILE * f)
1208 if ( !fread(&c1, 1, 1, f) )
1210 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1216 static unsigned short readushort(FILE * f, int bigendian)
1218 unsigned char c1, c2;
1219 if ( !fread(&c1, 1, 1, f) )
1221 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1224 if ( !fread(&c2, 1, 1, f) )
1226 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1230 return (c1 << 8) + c2;
1232 return (c2 << 8) + c1;
1235 static unsigned int readuint(FILE * f, int bigendian)
1237 unsigned char c1, c2, c3, c4;
1238 if ( !fread(&c1, 1, 1, f) )
1240 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1243 if ( !fread(&c2, 1, 1, f) )
1245 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1248 if ( !fread(&c3, 1, 1, f) )
1250 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1253 if ( !fread(&c4, 1, 1, f) )
1255 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1259 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1261 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1264 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1267 int i, numcomps, max;
1268 OPJ_COLOR_SPACE color_space;
1269 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1270 opj_image_t * image = NULL;
1271 int adjustS, ushift, dshift, force8;
1273 char endian1,endian2,sign;
1278 opj_image_comp_t *comp = NULL;
1281 color_space = OPJ_CLRSPC_GRAY;
1283 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1287 f = fopen(filename, "rb");
1289 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1293 fseek(f, 0, SEEK_SET);
1294 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1295 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1301 while (signtmp[i]!='\0') {
1302 if (signtmp[i]=='-') sign='-';
1307 if (endian1=='M' && endian2=='L') {
1309 } else if (endian2=='M' && endian1=='L') {
1312 fprintf(stderr, "Bad pgx header, please check input file\n");
1316 /* initialize image component */
1318 cmptparm.x0 = parameters->image_offset_x0;
1319 cmptparm.y0 = parameters->image_offset_y0;
1320 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1321 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1331 ushift = 8 - prec; dshift = prec - ushift;
1332 if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0;
1336 else ushift = dshift = force8 = adjustS = 0;
1338 cmptparm.prec = prec;
1339 cmptparm.bpp = prec;
1340 cmptparm.dx = parameters->subsampling_dx;
1341 cmptparm.dy = parameters->subsampling_dy;
1343 /* create the image */
1344 image = opj_image_create(numcomps, &cmptparm, color_space);
1349 /* set image offset and reference grid */
1350 image->x0 = cmptparm.x0;
1351 image->y0 = cmptparm.x0;
1352 image->x1 = cmptparm.w;
1353 image->y1 = cmptparm.h;
1355 /* set image data */
1357 comp = &image->comps[0];
1359 for (i = 0; i < w * h; i++) {
1363 v = readuchar(f) + adjustS;
1364 v = (v<<ushift) + (v>>dshift);
1365 comp->data[i] = (unsigned char)v;
1367 if(v > max) max = v;
1371 if (comp->prec == 8) {
1375 v = (char) readuchar(f);
1377 } else if (comp->prec <= 16) {
1379 v = readushort(f, bigendian);
1381 v = (short) readushort(f, bigendian);
1385 v = readuint(f, bigendian);
1387 v = (int) readuint(f, bigendian);
1395 comp->bpp = int_floorlog2(max) + 1;
1400 int imagetopgx(opj_image_t * image, const char *outfile) {
1405 for (compno = 0; compno < image->numcomps; compno++) {
1406 opj_image_comp_t *comp = &image->comps[compno];
1407 char bname[256]; /* buffer for name */
1408 char *name = bname; /* pointer */
1411 const size_t olen = strlen(outfile);
1412 const size_t dotpos = olen - 4;
1413 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1414 if( outfile[dotpos] != '.' ) {
1415 /* `pgx` was recognized but there is no dot at expected position */
1416 fprintf(stderr, "ERROR -> Impossible happen." );
1420 name = (char*)malloc(total+1);
1422 strncpy(name, outfile, dotpos);
1423 /*if (image->numcomps > 1) {*/
1424 sprintf(name+dotpos, "_%d.pgx", compno);
1426 strcpy(name+dotpos, ".pgx");
1428 fdest = fopen(name, "wb");
1430 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1433 /* dont need name anymore */
1438 w = image->comps[compno].w;
1439 h = image->comps[compno].h;
1441 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1442 if (comp->prec <= 8) {
1444 } else if (comp->prec <= 16) {
1449 for (i = 0; i < w * h; i++) {
1450 int v = image->comps[compno].data[i];
1451 for (j = nbytes - 1; j >= 0; j--) {
1452 char byte = (char) (v >> (j * 8));
1453 res = fwrite(&byte, 1, 1, fdest);
1455 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1466 /* -->> -->> -->> -->>
1470 <<-- <<-- <<-- <<-- */
1474 int width, height, maxval, depth, format;
1475 char rgb, rgba, gray, graya, bw;
1479 static char *skip_white(char *s)
1483 if(*s == '\n' || *s == '\r') return NULL;
1484 if(isspace(*s)) { ++s; continue; }
1490 static char *skip_int(char *start, int *out_n)
1495 *out_n = 0; s = start;
1497 s = skip_white(start);
1498 if(s == NULL) return NULL;
1503 if( !isdigit(*s)) break;
1506 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1510 static char *skip_idf(char *start, char out_idf[256])
1515 s = skip_white(start);
1516 if(s == NULL) return NULL;
1521 if(isalpha(*s) || *s == '_') { ++s; continue; }
1524 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1528 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1531 int format, have_wh, end, ttype;
1532 char idf[256], type[256];
1535 if (fgets(line, 250, reader) == NULL)
1537 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1543 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1545 format = atoi(line + 1);
1546 if(format < 1 || format > 7)
1548 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1551 ph->format = format;
1552 ttype = end = have_wh = 0;
1554 while(fgets(line, 250, reader))
1556 if(*line == '#') continue;
1562 s = skip_idf(s, idf);
1564 if(s == NULL || *s == 0) return;
1566 if(strcmp(idf, "ENDHDR") == 0)
1570 if(strcmp(idf, "WIDTH") == 0)
1572 s = skip_int(s, &ph->width);
1573 if(s == NULL || *s == 0) return;
1577 if(strcmp(idf, "HEIGHT") == 0)
1579 s = skip_int(s, &ph->height);
1580 if(s == NULL || *s == 0) return;
1584 if(strcmp(idf, "DEPTH") == 0)
1586 s = skip_int(s, &ph->depth);
1587 if(s == NULL || *s == 0) return;
1591 if(strcmp(idf, "MAXVAL") == 0)
1593 s = skip_int(s, &ph->maxval);
1594 if(s == NULL || *s == 0) return;
1598 if(strcmp(idf, "TUPLTYPE") == 0)
1600 s = skip_idf(s, type);
1601 if(s == NULL || *s == 0) return;
1603 if(strcmp(type, "BLACKANDWHITE") == 0)
1605 ph->bw = 1; ttype = 1; continue;
1607 if(strcmp(type, "GRAYSCALE") == 0)
1609 ph->gray = 1; ttype = 1; continue;
1611 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1613 ph->graya = 1; ttype = 1; continue;
1615 if(strcmp(type, "RGB") == 0)
1617 ph->rgb = 1; ttype = 1; continue;
1619 if(strcmp(type, "RGB_ALPHA") == 0)
1621 ph->rgba = 1; ttype = 1; continue;
1623 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1626 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1628 } /* if(format == 7) */
1632 s = skip_int(s, &ph->width);
1634 s = skip_int(s, &ph->height);
1638 if(format == 1 || format == 4) break;
1642 if(format == 2 || format == 3 || format == 5 || format == 6)
1644 /* P2, P3, P5, P6: */
1645 s = skip_int(s, &ph->maxval);
1647 if(ph->maxval > 65535) return;
1650 }/* while(fgets( ) */
1651 if(format == 2 || format == 3 || format > 4)
1653 if(ph->maxval < 1 || ph->maxval > 65535) return;
1655 if(ph->width < 1 || ph->height < 1) return;
1661 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1663 if(ph->depth < 1 || ph->depth > 4) return;
1665 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1670 if(format != 1 && format != 4)
1672 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1676 if(ph->width && ph->height) ph->ok = 1;
1682 static int has_prec(int val)
1684 if(val < 2) return 1;
1685 if(val < 4) return 2;
1686 if(val < 8) return 3;
1687 if(val < 16) return 4;
1688 if(val < 32) return 5;
1689 if(val < 64) return 6;
1690 if(val < 128) return 7;
1691 if(val < 256) return 8;
1692 if(val < 512) return 9;
1693 if(val < 1024) return 10;
1694 if(val < 2048) return 11;
1695 if(val < 4096) return 12;
1696 if(val < 8192) return 13;
1697 if(val < 16384) return 14;
1698 if(val < 32768) return 15;
1702 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1703 int subsampling_dx = parameters->subsampling_dx;
1704 int subsampling_dy = parameters->subsampling_dy;
1707 int i, compno, numcomps, w, h, prec, format;
1708 OPJ_COLOR_SPACE color_space;
1709 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1710 opj_image_t * image = NULL;
1711 struct pnm_header header_info;
1713 if((fp = fopen(filename, "rb")) == NULL)
1715 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1718 memset(&header_info, 0, sizeof(struct pnm_header));
1720 read_pnm_header(fp, &header_info);
1722 if(!header_info.ok) { fclose(fp); return NULL; }
1724 format = header_info.format;
1728 case 1: /* ascii bitmap */
1729 case 4: /* raw bitmap */
1733 case 2: /* ascii greymap */
1734 case 5: /* raw greymap */
1738 case 3: /* ascii pixmap */
1739 case 6: /* raw pixmap */
1743 case 7: /* arbitrary map */
1744 numcomps = header_info.depth;
1747 default: fclose(fp); return NULL;
1750 color_space = OPJ_CLRSPC_GRAY;/* GRAY, GRAYA */
1752 color_space = OPJ_CLRSPC_SRGB;/* RGB, RGBA */
1754 prec = has_prec(header_info.maxval);
1756 if(prec < 8) prec = 8;
1758 w = header_info.width;
1759 h = header_info.height;
1760 subsampling_dx = parameters->subsampling_dx;
1761 subsampling_dy = parameters->subsampling_dy;
1763 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1765 for(i = 0; i < numcomps; i++)
1767 cmptparm[i].prec = prec;
1768 cmptparm[i].bpp = prec;
1769 cmptparm[i].sgnd = 0;
1770 cmptparm[i].dx = subsampling_dx;
1771 cmptparm[i].dy = subsampling_dy;
1775 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1777 if(!image) { fclose(fp); return NULL; }
1779 /* set image offset and reference grid */
1780 image->x0 = parameters->image_offset_x0;
1781 image->y0 = parameters->image_offset_y0;
1782 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1783 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1785 if((format == 2) || (format == 3)) /* ascii pixmap */
1789 for (i = 0; i < w * h; i++)
1791 for(compno = 0; compno < numcomps; compno++)
1794 if (fscanf(fp, "%u", &index) != 1)
1795 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1797 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1805 && ( header_info.gray || header_info.graya
1806 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1808 unsigned char c0, c1, one;
1812 for (i = 0; i < w * h; i++)
1814 for(compno = 0; compno < numcomps; compno++)
1816 if ( !fread(&c0, 1, 1, fp) )
1817 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1820 image->comps[compno].data[i] = c0;
1824 if ( !fread(&c1, 1, 1, fp) )
1825 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1827 image->comps[compno].data[i] = ((c0<<8) | c1);
1833 if(format == 1) /* ascii bitmap */
1835 for (i = 0; i < w * h; i++)
1839 if ( fscanf(fp, "%u", &index) != 1)
1840 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1842 image->comps[0].data[i] = (index?0:255);
1852 for(y = 0; y < h; ++y)
1856 for(x = 0; x < w; ++x)
1861 uc = (unsigned char)getc(fp);
1863 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1869 if((format == 7 && header_info.bw)) /*MONO*/
1873 for(i = 0; i < w * h; ++i)
1875 if ( !fread(&uc, 1, 1, fp) )
1876 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1877 image->comps[0].data[i] = (uc & 1)?0:255;
1885 int imagetopnm(opj_image_t * image, const char *outfile)
1887 int *red, *green, *blue, *alpha;
1889 int i, compno, ncomp;
1890 int adjustR, adjustG, adjustB, adjustA;
1891 int fails, two, want_gray, has_alpha, triple;
1894 const char *tmp = outfile;
1897 if((prec = image->comps[0].prec) > 16)
1899 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1900 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1903 two = has_alpha = 0; fails = 1;
1904 ncomp = image->numcomps;
1906 while (*tmp) ++tmp; tmp -= 2;
1907 want_gray = (*tmp == 'g' || *tmp == 'G');
1908 ncomp = image->numcomps;
1910 if(want_gray) ncomp = 1;
1912 if (ncomp == 2 /* GRAYA */
1913 || (ncomp > 2 /* RGB, RGBA */
1914 && image->comps[0].dx == image->comps[1].dx
1915 && image->comps[1].dx == image->comps[2].dx
1916 && image->comps[0].dy == image->comps[1].dy
1917 && image->comps[1].dy == image->comps[2].dy
1918 && image->comps[0].prec == image->comps[1].prec
1919 && image->comps[1].prec == image->comps[2].prec
1922 fdest = fopen(outfile, "wb");
1926 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1930 triple = (ncomp > 2);
1931 wr = image->comps[0].w; hr = image->comps[0].h;
1932 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1934 red = image->comps[0].data;
1938 green = image->comps[1].data;
1939 blue = image->comps[2].data;
1941 else green = blue = NULL;
1945 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1947 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1948 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1949 wr, hr, ncomp, max, tt);
1950 alpha = image->comps[ncomp - 1].data;
1951 adjustA = (image->comps[ncomp - 1].sgnd ?
1952 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1956 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1957 opj_version(), wr, hr, max);
1960 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1964 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1965 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1967 else adjustG = adjustB = 0;
1969 for(i = 0; i < wr * hr; ++i)
1973 v = *red + adjustR; ++red;
1975 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1979 v = *green + adjustG; ++green;
1981 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1983 v = *blue + adjustB; ++blue;
1985 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1991 v = *alpha + adjustA; ++alpha;
1993 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2001 fprintf(fdest, "%c", (unsigned char)*red++);
2003 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
2006 fprintf(fdest, "%c", (unsigned char)*alpha++);
2010 fclose(fdest); return 0;
2015 if (image->numcomps > ncomp)
2017 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
2018 fprintf(stderr," is written to the file\n");
2020 destname = (char*)malloc(strlen(outfile) + 8);
2022 for (compno = 0; compno < ncomp; compno++)
2025 sprintf(destname, "%d.%s", compno, outfile);
2027 sprintf(destname, "%s", outfile);
2029 fdest = fopen(destname, "wb");
2032 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2036 wr = image->comps[compno].w; hr = image->comps[compno].h;
2037 prec = image->comps[compno].prec;
2038 max = (1<<prec) - 1;
2040 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2041 opj_version(), wr, hr, max);
2043 red = image->comps[compno].data;
2045 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2049 for (i = 0; i < wr * hr; i++)
2051 v = *red + adjustR; ++red;
2053 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2059 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2063 else /* prec <= 8 */
2065 for(i = 0; i < wr * hr; ++i)
2067 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
2078 /* -->> -->> -->> -->>
2082 <<-- <<-- <<-- <<-- */
2084 int imagetotif(opj_image_t * image, const char *outfile)
2086 int width, height, imgsize;
2087 int bps,index,adjust, sgnd;
2088 int ushift, dshift, has_alpha, force16;
2094 ushift = dshift = force16 = has_alpha = 0;
2095 bps = image->comps[0].prec;
2097 if(bps > 8 && bps < 16)
2099 ushift = 16 - bps; dshift = bps - ushift;
2100 bps = 16; force16 = 1;
2103 if(bps != 8 && bps != 16)
2105 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2107 fprintf(stderr,"\tAborting\n");
2110 tif = TIFFOpen(outfile, "wb");
2114 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2117 sgnd = image->comps[0].sgnd;
2118 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2120 if(image->numcomps >= 3
2121 && image->comps[0].dx == image->comps[1].dx
2122 && image->comps[1].dx == image->comps[2].dx
2123 && image->comps[0].dy == image->comps[1].dy
2124 && image->comps[1].dy == image->comps[2].dy
2125 && image->comps[0].prec == image->comps[1].prec
2126 && image->comps[1].prec == image->comps[2].prec)
2128 has_alpha = (image->numcomps == 4);
2130 width = image->comps[0].w;
2131 height = image->comps[0].h;
2132 imgsize = width * height ;
2134 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2135 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2136 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2137 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2138 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2139 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2140 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2141 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2142 strip_size = TIFFStripSize(tif);
2143 buf = _TIFFmalloc(strip_size);
2146 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2148 unsigned char *dat8;
2149 tsize_t i, ssize, last_i = 0;
2151 ssize = TIFFStripSize(tif);
2152 dat8 = (unsigned char*)buf;
2156 step = 3 + has_alpha;
2159 for(i=0; i < ssize - restx; i += step)
2165 r = image->comps[0].data[index];
2166 g = image->comps[1].data[index];
2167 b = image->comps[2].data[index];
2168 if(has_alpha) a = image->comps[3].data[index];
2175 if(has_alpha) a += adjust;
2180 if(has_alpha) dat8[i+3] = a;
2191 for(i = last_i; i < ssize; i += step)
2197 r = image->comps[0].data[index];
2198 g = image->comps[1].data[index];
2199 b = image->comps[2].data[index];
2200 if(has_alpha) a = image->comps[3].data[index];
2207 if(has_alpha) a += adjust;
2210 if(i+1 < ssize) dat8[i+1] = g ; else break;
2211 if(i+2 < ssize) dat8[i+2] = b ; else break;
2214 if(i+3 < ssize) dat8[i+3] = a ; else break;
2221 }/*if(last_i < ssize)*/
2227 step = 6 + has_alpha + has_alpha;
2230 for(i = 0; i < ssize - restx ; i += step)
2236 r = image->comps[0].data[index];
2237 g = image->comps[1].data[index];
2238 b = image->comps[2].data[index];
2239 if(has_alpha) a = image->comps[3].data[index];
2246 if(has_alpha) a += adjust;
2250 r = (r<<ushift) + (r>>dshift);
2251 g = (g<<ushift) + (g>>dshift);
2252 b = (b<<ushift) + (b>>dshift);
2253 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2255 dat8[i+0] = r;/*LSB*/
2256 dat8[i+1] = (r >> 8);/*MSB*/
2258 dat8[i+3] = (g >> 8);
2260 dat8[i+5] = (b >> 8);
2264 dat8[i+7] = (a >> 8);
2275 for(i = last_i ; i < ssize ; i += step)
2281 r = image->comps[0].data[index];
2282 g = image->comps[1].data[index];
2283 b = image->comps[2].data[index];
2284 if(has_alpha) a = image->comps[3].data[index];
2291 if(has_alpha) a += adjust;
2295 r = (r<<ushift) + (r>>dshift);
2296 g = (g<<ushift) + (g>>dshift);
2297 b = (b<<ushift) + (b>>dshift);
2298 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2300 dat8[i+0] = r;/*LSB*/
2301 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/
2302 if(i+2 < ssize) dat8[i+2] = g; else break;
2303 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2304 if(i+4 < ssize) dat8[i+4] = b; else break;
2305 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2309 if(i+6 < ssize) dat8[i+6] = a; else break;
2310 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2317 }/*if(last_i < ssize)*/
2320 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2321 }/*for(strip = 0; )*/
2323 _TIFFfree((void*)buf);
2329 if(image->numcomps == 1 /* GRAY */
2330 || ( image->numcomps == 2 /* GRAY_ALPHA */
2331 && image->comps[0].dx == image->comps[1].dx
2332 && image->comps[0].dy == image->comps[1].dy
2333 && image->comps[0].prec == image->comps[1].prec))
2337 has_alpha = (image->numcomps == 2);
2339 width = image->comps[0].w;
2340 height = image->comps[0].h;
2341 imgsize = width * height;
2344 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2345 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2346 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2347 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2348 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2349 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2350 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2351 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2353 /* Get a buffer for the data */
2354 strip_size = TIFFStripSize(tif);
2355 buf = _TIFFmalloc(strip_size);
2358 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2360 unsigned char *dat8;
2361 tsize_t i, ssize = TIFFStripSize(tif);
2362 dat8 = (unsigned char*)buf;
2366 step = 1 + has_alpha;
2368 for(i=0; i < ssize; i += step)
2374 r = image->comps[0].data[index];
2375 if(has_alpha) a = image->comps[1].data[index];
2380 if(has_alpha) a += adjust;
2383 if(has_alpha) dat8[i+1] = a;
2393 step = 2 + has_alpha + has_alpha;
2395 for(i=0; i < ssize; i += step)
2401 r = image->comps[0].data[index];
2402 if(has_alpha) a = image->comps[1].data[index];
2407 if(has_alpha) a += adjust;
2411 r = (r<<ushift) + (r>>dshift);
2412 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2414 dat8[i+0] = r;/*LSB*/
2415 dat8[i+1] = r >> 8;/*MSB*/
2422 }/*if(index < imgsize)*/
2427 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2438 fprintf(stderr,"imagetotif: Bad color format.\n"
2439 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2440 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2447 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2448 * CINEMA : 12 bit precision
2450 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2452 int subsampling_dx = parameters->subsampling_dx;
2453 int subsampling_dy = parameters->subsampling_dy;
2458 int j, numcomps, w, h,index;
2459 OPJ_COLOR_SPACE color_space;
2460 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2461 opj_image_t *image = NULL;
2464 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2465 unsigned int tiWidth, tiHeight;
2467 tif = TIFFOpen(filename, "r");
2471 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2474 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2475 tiWidth = tiHeight = 0;
2477 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2478 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2479 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2480 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2481 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2482 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2483 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2487 if(tiBps != 8 && tiBps != 16 && tiBps != 12) tiBps = 0;
2488 if(tiPhoto != 1 && tiPhoto != 2) tiPhoto = 0;
2490 if( !tiBps || !tiPhoto)
2493 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2494 " implemented\n",tiBps);
2497 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2498 " and GRAY(A) has been implemented\n",(int) tiPhoto);
2500 fprintf(stderr,"\tAborting\n");
2506 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2508 uint16 extrasamples;
2510 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2511 &extrasamples, &sampleinfo);
2513 if(extrasamples >= 1)
2515 switch(sampleinfo[0])
2517 case EXTRASAMPLE_UNSPECIFIED:
2518 /* Workaround for some images without correct info about alpha channel
2524 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2525 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2530 else /* extrasamples == 0 */
2531 if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
2534 /* initialize image components
2536 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2538 if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2540 numcomps = 3 + has_alpha;
2541 color_space = OPJ_CLRSPC_SRGB;
2543 /*#define USETILEMODE*/
2544 for(j = 0; j < numcomps; j++)
2546 if(parameters->cp_cinema)
2548 cmptparm[j].prec = 12;
2549 cmptparm[j].bpp = 12;
2553 cmptparm[j].prec = tiBps;
2554 cmptparm[j].bpp = tiBps;
2556 cmptparm[j].dx = subsampling_dx;
2557 cmptparm[j].dy = subsampling_dy;
2567 image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
2569 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2577 /* set image offset and reference grid
2579 image->x0 = parameters->image_offset_x0;
2580 image->y0 = parameters->image_offset_y0;
2581 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2582 image->x0 + (w - 1) * subsampling_dx + 1;
2583 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2584 image->y0 + (h - 1) * subsampling_dy + 1;
2586 buf = _TIFFmalloc(TIFFStripSize(tif));
2588 strip_size=TIFFStripSize(tif);
2590 imgsize = image->comps[0].w * image->comps[0].h ;
2591 /* Read the Image components
2593 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2595 unsigned char *dat8;
2598 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2599 dat8 = (unsigned char*)buf;
2603 step = 6 + has_alpha + has_alpha;
2605 for(i = 0; i < ssize; i += step)
2609 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */
2610 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
2611 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
2613 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2615 if(parameters->cp_cinema)
2617 /* Rounding 16 to 12 bits
2619 image->comps[0].data[index] =
2620 (image->comps[0].data[index] + 0x08) >> 4 ;
2621 image->comps[1].data[index] =
2622 (image->comps[1].data[index] + 0x08) >> 4 ;
2623 image->comps[2].data[index] =
2624 (image->comps[2].data[index] + 0x08) >> 4 ;
2626 image->comps[3].data[index] =
2627 (image->comps[3].data[index] + 0x08) >> 4 ;
2634 }/*if(tiBps == 16)*/
2638 step = 3 + has_alpha;
2640 for(i = 0; i < ssize; i += step)
2645 image->comps[0].data[index] = dat8[i+0];/* R */
2646 image->comps[1].data[index] = dat8[i+1];/* G */
2647 image->comps[2].data[index] = dat8[i+2];/* B */
2649 image->comps[3].data[index] = dat8[i+3];
2652 if(parameters->cp_cinema)
2654 /* Rounding 8 to 12 bits
2657 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2658 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2659 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2661 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2669 }/*if( tiBps == 8)*/
2671 if(tiBps == 12)/* CINEMA file */
2675 for(i = 0; i < ssize; i += step)
2677 if((index < imgsize)&(index+1 < imgsize))
2679 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2680 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2682 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2683 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2685 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2686 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2694 }/*for(strip = 0; )*/
2702 if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2704 numcomps = 1 + has_alpha;
2705 color_space = OPJ_CLRSPC_GRAY;
2707 for(j = 0; j < numcomps; ++j)
2709 cmptparm[j].prec = tiBps;
2710 cmptparm[j].bpp = tiBps;
2711 cmptparm[j].dx = subsampling_dx;
2712 cmptparm[j].dy = subsampling_dy;
2717 image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
2719 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2727 /* set image offset and reference grid
2729 image->x0 = parameters->image_offset_x0;
2730 image->y0 = parameters->image_offset_y0;
2731 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2732 image->x0 + (w - 1) * subsampling_dx + 1;
2733 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2734 image->y0 + (h - 1) * subsampling_dy + 1;
2736 buf = _TIFFmalloc(TIFFStripSize(tif));
2738 strip_size = TIFFStripSize(tif);
2740 imgsize = image->comps[0].w * image->comps[0].h ;
2741 /* Read the Image components
2743 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2745 unsigned char *dat8;
2749 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2750 dat8 = (unsigned char*)buf;
2754 step = 2 + has_alpha + has_alpha;
2756 for(i = 0; i < ssize; i += step)
2760 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2762 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2772 step = 1 + has_alpha;
2774 for(i = 0; i < ssize; i += step)
2778 image->comps[0].data[index] = dat8[i+0];
2780 image->comps[1].data[index] = dat8[i+1];
2798 #endif /* HAVE_LIBTIFF */
2800 /* -->> -->> -->> -->>
2804 <<-- <<-- <<-- <<-- */
2805 static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian) {
2806 int subsampling_dx = parameters->subsampling_dx;
2807 int subsampling_dy = parameters->subsampling_dy;
2810 int i, compno, numcomps, w, h;
2811 OPJ_COLOR_SPACE color_space;
2812 opj_image_cmptparm_t *cmptparm;
2813 opj_image_t * image = NULL;
2816 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2818 fprintf(stderr,"\nError: invalid raw image parameters\n");
2819 fprintf(stderr,"Please use the Format option -F:\n");
2820 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2821 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2822 fprintf(stderr,"Aborting\n");
2826 f = fopen(filename, "rb");
2828 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2829 fprintf(stderr,"Aborting\n");
2832 numcomps = raw_cp->rawComp;
2833 color_space = OPJ_CLRSPC_SRGB;
2834 w = raw_cp->rawWidth;
2835 h = raw_cp->rawHeight;
2836 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2838 /* initialize image components */
2839 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2840 for(i = 0; i < numcomps; i++) {
2841 cmptparm[i].prec = raw_cp->rawBitDepth;
2842 cmptparm[i].bpp = raw_cp->rawBitDepth;
2843 cmptparm[i].sgnd = raw_cp->rawSigned;
2844 cmptparm[i].dx = subsampling_dx;
2845 cmptparm[i].dy = subsampling_dy;
2849 /* create the image */
2850 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2855 /* set image offset and reference grid */
2856 image->x0 = parameters->image_offset_x0;
2857 image->y0 = parameters->image_offset_y0;
2858 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2859 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2861 if(raw_cp->rawBitDepth <= 8)
2863 unsigned char value = 0;
2864 for(compno = 0; compno < numcomps; compno++) {
2865 for (i = 0; i < w * h; i++) {
2866 if (!fread(&value, 1, 1, f)) {
2867 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2870 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2874 else if(raw_cp->rawBitDepth <= 16)
2876 unsigned short value;
2877 for(compno = 0; compno < numcomps; compno++) {
2878 for (i = 0; i < w * h; i++) {
2879 unsigned char temp1;
2880 unsigned char temp2;
2881 if (!fread(&temp1, 1, 1, f)) {
2882 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2885 if (!fread(&temp2, 1, 1, f)) {
2886 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2899 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2904 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2908 if (fread(&ch, 1, 1, f)) {
2909 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2916 opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2917 return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE);
2920 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2921 return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE);
2924 static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL big_endian)
2926 FILE *rawFile = NULL;
2933 if((image->numcomps * image->x1 * image->y1) == 0)
2935 fprintf(stderr,"\nError: invalid raw image parameters\n");
2939 rawFile = fopen(outfile, "wb");
2941 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2945 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2947 for(compno = 0; compno < image->numcomps; compno++)
2949 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2950 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2952 w = image->comps[compno].w;
2953 h = image->comps[compno].h;
2955 if(image->comps[compno].prec <= 8)
2957 if(image->comps[compno].sgnd == 1)
2960 int mask = (1 << image->comps[compno].prec) - 1;
2961 ptr = image->comps[compno].data;
2962 for (line = 0; line < h; line++) {
2963 for(row = 0; row < w; row++) {
2964 curr = (signed char) (*ptr & mask);
2965 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
2967 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2974 else if(image->comps[compno].sgnd == 0)
2977 int mask = (1 << image->comps[compno].prec) - 1;
2978 ptr = image->comps[compno].data;
2979 for (line = 0; line < h; line++) {
2980 for(row = 0; row < w; row++) {
2981 curr = (unsigned char) (*ptr & mask);
2982 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2984 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2992 else if(image->comps[compno].prec <= 16)
2994 if(image->comps[compno].sgnd == 1)
2996 signed short int curr;
2997 int mask = (1 << image->comps[compno].prec) - 1;
2998 ptr = image->comps[compno].data;
2999 for (line = 0; line < h; line++) {
3000 for(row = 0; row < w; row++) {
3001 unsigned char temp1;
3002 unsigned char temp2;
3003 curr = (signed short int) (*ptr & mask);
3006 temp1 = (unsigned char) (curr >> 8);
3007 temp2 = (unsigned char) curr;
3011 temp2 = (unsigned char) (curr >> 8);
3012 temp1 = (unsigned char) curr;
3014 res = fwrite(&temp1, 1, 1, rawFile);
3016 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3019 res = fwrite(&temp2, 1, 1, rawFile);
3021 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3028 else if(image->comps[compno].sgnd == 0)
3030 unsigned short int curr;
3031 int mask = (1 << image->comps[compno].prec) - 1;
3032 ptr = image->comps[compno].data;
3033 for (line = 0; line < h; line++) {
3034 for(row = 0; row < w; row++) {
3035 unsigned char temp1;
3036 unsigned char temp2;
3037 curr = (unsigned short int) (*ptr & mask);
3040 temp1 = (unsigned char) (curr >> 8);
3041 temp2 = (unsigned char) curr;
3045 temp2 = (unsigned char) (curr >> 8);
3046 temp1 = (unsigned char) curr;
3048 res = fwrite(&temp1, 1, 1, rawFile);
3050 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3053 res = fwrite(&temp2, 1, 1, rawFile);
3055 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3063 else if (image->comps[compno].prec <= 32)
3065 fprintf(stderr,"More than 16 bits per component no handled yet\n");
3070 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
3078 int imagetoraw(opj_image_t * image, const char *outfile)
3080 return imagetoraw_common(image, outfile, OPJ_TRUE);
3083 int imagetorawl(opj_image_t * image, const char *outfile)
3085 return imagetoraw_common(image, outfile, OPJ_FALSE);
3090 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3091 #define MAGIC_SIZE 8
3092 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3094 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3098 double gamma, display_exponent;
3099 int bit_depth, interlace_type,compression_type, filter_type;
3101 png_uint_32 resx, resy;
3103 png_uint_32 width, height;
3104 int color_type, has_alpha, is16;
3107 unsigned char **rows;
3110 opj_image_cmptparm_t cmptparm[4];
3112 unsigned int nr_comp;
3114 unsigned char sigbuf[8];
3116 if((reader = fopen(read_idf, "rb")) == NULL)
3118 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
3121 image = NULL; png = NULL; rows = NULL;
3123 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3124 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
3126 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
3129 /* libpng-VERSION/example.c:
3130 * PC : screen_gamma = 2.2;
3131 * Mac: screen_gamma = 1.7 or 1.0;
3133 display_exponent = 2.2;
3135 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3136 NULL, NULL, NULL)) == NULL)
3138 if((info = png_create_info_struct(png)) == NULL)
3141 if(setjmp(png_jmpbuf(png)))
3144 png_init_io(png, reader);
3145 png_set_sig_bytes(png, MAGIC_SIZE);
3147 png_read_info(png, info);
3149 if(png_get_IHDR(png, info, &width, &height,
3150 &bit_depth, &color_type, &interlace_type,
3151 &compression_type, &filter_type) == 0)
3154 /* png_set_expand():
3155 * expand paletted images to RGB, expand grayscale images of
3156 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3157 * to alpha channels.
3159 if(color_type == PNG_COLOR_TYPE_PALETTE)
3160 png_set_expand(png);
3162 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3163 png_set_expand(png);
3165 if(png_get_valid(png, info, PNG_INFO_tRNS))
3166 png_set_expand(png);
3168 is16 = (bit_depth == 16);
3170 /* GRAY => RGB; GRAY_ALPHA => RGBA
3172 if(color_type == PNG_COLOR_TYPE_GRAY
3173 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3175 png_set_gray_to_rgb(png);
3177 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
3178 PNG_COLOR_TYPE_RGB_ALPHA);
3180 if( !png_get_gAMA(png, info, &gamma))
3183 png_set_gamma(png, display_exponent, gamma);
3185 png_read_update_info(png, info);
3187 png_get_pHYs(png, info, &resx, &resy, &unit);
3189 color_type = png_get_color_type(png, info);
3191 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3193 nr_comp = 3 + has_alpha;
3195 bit_depth = png_get_bit_depth(png, info);
3197 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
3198 for(i = 0; i < height; ++i)
3199 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
3201 png_read_image(png, rows);
3203 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3205 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3207 for(i = 0; i < nr_comp; ++i)
3209 cmptparm[i].prec = bit_depth;
3210 /* bits_per_pixel: 8 or 16 */
3211 cmptparm[i].bpp = bit_depth;
3212 cmptparm[i].sgnd = 0;
3213 cmptparm[i].dx = sub_dx;
3214 cmptparm[i].dy = sub_dy;
3215 cmptparm[i].w = width;
3216 cmptparm[i].h = height;
3219 image = opj_image_create(nr_comp, &cmptparm[0], OPJ_CLRSPC_SRGB);
3221 if(image == NULL) goto fin;
3223 image->x0 = params->image_offset_x0;
3224 image->y0 = params->image_offset_y0;
3225 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3226 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3228 r = image->comps[0].data;
3229 g = image->comps[1].data;
3230 b = image->comps[2].data;
3231 a = image->comps[3].data;
3233 for(i = 0; i < height; ++i)
3237 for(j = 0; j < width; ++j)
3241 *r++ = s[0]<<8|s[1]; s += 2;
3243 *g++ = s[0]<<8|s[1]; s += 2;
3245 *b++ = s[0]<<8|s[1]; s += 2;
3247 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3251 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3253 if(has_alpha) *a++ = *s++;
3259 for(i = 0; i < height; ++i)
3264 png_destroy_read_struct(&png, &info, NULL);
3272 int imagetopng(opj_image_t * image, const char *write_idf)
3277 int *red, *green, *blue, *alpha;
3278 unsigned char *row_buf, *d;
3279 int has_alpha, width, height, nr_comp, color_type;
3280 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3281 int prec, ushift, dshift, is16, force16, force8;
3282 unsigned short mask = 0xffff;
3283 png_color_8 sig_bit;
3285 is16 = force16 = force8 = ushift = dshift = 0; fails = 1;
3286 prec = image->comps[0].prec;
3287 nr_comp = image->numcomps;
3289 if(prec > 8 && prec < 16)
3291 ushift = 16 - prec; dshift = prec - ushift;
3292 prec = 16; force16 = 1;
3295 if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */
3297 ushift = 8 - prec; dshift = 8 - ushift;
3298 prec = 8; force8 = 1;
3301 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3303 fprintf(stderr,"imagetopng: can not create %s"
3304 "\n\twrong bit_depth %d\n", write_idf, prec);
3307 writer = fopen(write_idf, "wb");
3309 if(writer == NULL) return fails;
3311 info = NULL; has_alpha = 0;
3313 /* Create and initialize the png_struct with the desired error handler
3314 * functions. If you want to use the default stderr and longjump method,
3315 * you can supply NULL for the last three parameters. We also check that
3316 * the library version is compatible with the one used at compile time,
3317 * in case we are using dynamically linked libraries. REQUIRED.
3319 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3321 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3323 if(png == NULL) goto fin;
3325 /* Allocate/initialize the image information data. REQUIRED
3327 info = png_create_info_struct(png);
3329 if(info == NULL) goto fin;
3331 /* Set error handling. REQUIRED if you are not supplying your own
3332 * error handling functions in the png_create_write_struct() call.
3334 if(setjmp(png_jmpbuf(png))) goto fin;
3336 /* I/O initialization functions is REQUIRED
3338 png_init_io(png, writer);
3340 /* Set the image information here. Width and height are up to 2^31,
3341 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3342 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3343 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3344 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3345 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3346 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3351 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3352 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3353 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3354 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3357 png_set_compression_level(png, Z_BEST_COMPRESSION);
3359 if(prec == 16) mask = 0xffff;
3361 if(prec == 8) mask = 0x00ff;
3363 if(prec == 4) mask = 0x000f;
3365 if(prec == 2) mask = 0x0003;
3367 if(prec == 1) mask = 0x0001;
3370 && image->comps[0].dx == image->comps[1].dx
3371 && image->comps[1].dx == image->comps[2].dx
3372 && image->comps[0].dy == image->comps[1].dy
3373 && image->comps[1].dy == image->comps[2].dy
3374 && image->comps[0].prec == image->comps[1].prec
3375 && image->comps[1].prec == image->comps[2].prec)
3379 has_alpha = (nr_comp > 3);
3381 is16 = (prec == 16);
3383 width = image->comps[0].w;
3384 height = image->comps[0].h;
3386 red = image->comps[0].data;
3387 green = image->comps[1].data;
3388 blue = image->comps[2].data;
3390 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3394 sig_bit.alpha = prec;
3395 alpha = image->comps[3].data;
3396 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3397 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3401 sig_bit.alpha = 0; alpha = NULL;
3402 color_type = PNG_COLOR_TYPE_RGB;
3405 png_set_sBIT(png, info, &sig_bit);
3407 png_set_IHDR(png, info, width, height, prec,
3410 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3411 /*=============================*/
3412 png_write_info(png, info);
3413 /*=============================*/
3416 png_set_packing(png);
3418 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3419 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3420 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3422 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3424 for(y = 0; y < height; ++y)
3428 for(x = 0; x < width; ++x)
3432 v = *red + adjustR; ++red;
3434 if(force16) { v = (v<<ushift) + (v>>dshift); }
3436 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3438 v = *green + adjustG; ++green;
3440 if(force16) { v = (v<<ushift) + (v>>dshift); }
3442 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3444 v = *blue + adjustB; ++blue;
3446 if(force16) { v = (v<<ushift) + (v>>dshift); }
3448 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3452 v = *alpha + adjustA; ++alpha;
3454 if(force16) { v = (v<<ushift) + (v>>dshift); }
3456 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3461 v = *red + adjustR; ++red;
3463 if(force8) { v = (v<<ushift) + (v>>dshift); }
3465 *d++ = (unsigned char)(v & mask);
3467 v = *green + adjustG; ++green;
3469 if(force8) { v = (v<<ushift) + (v>>dshift); }
3471 *d++ = (unsigned char)(v & mask);
3473 v = *blue + adjustB; ++blue;
3475 if(force8) { v = (v<<ushift) + (v>>dshift); }
3477 *d++ = (unsigned char)(v & mask);
3481 v = *alpha + adjustA; ++alpha;
3483 if(force8) { v = (v<<ushift) + (v>>dshift); }
3485 *d++ = (unsigned char)(v & mask);
3489 png_write_row(png, row_buf);
3496 if(nr_comp == 1 /* GRAY */
3497 || ( nr_comp == 2 /* GRAY_ALPHA */
3498 && image->comps[0].dx == image->comps[1].dx
3499 && image->comps[0].dy == image->comps[1].dy
3500 && image->comps[0].prec == image->comps[1].prec))
3504 red = image->comps[0].data;
3506 sig_bit.gray = prec;
3507 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3508 alpha = NULL; adjustA = 0;
3509 color_type = PNG_COLOR_TYPE_GRAY;
3513 has_alpha = 1; sig_bit.alpha = prec;
3514 alpha = image->comps[1].data;
3515 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3516 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3518 width = image->comps[0].w;
3519 height = image->comps[0].h;
3521 png_set_IHDR(png, info, width, height, sig_bit.gray,
3524 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3526 png_set_sBIT(png, info, &sig_bit);
3527 /*=============================*/
3528 png_write_info(png, info);
3529 /*=============================*/
3530 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3534 png_set_packing(png);
3539 row_buf = (unsigned char*)
3540 malloc(width * nr_comp * sizeof(unsigned short));
3542 for(y = 0; y < height; ++y)
3546 for(x = 0; x < width; ++x)
3548 v = *red + adjustR; ++red;
3550 if(force16) { v = (v<<ushift) + (v>>dshift); }
3552 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3558 if(force16) { v = (v<<ushift) + (v>>dshift); }
3560 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3563 png_write_row(png, row_buf);
3568 else /* prec <= 8 */
3570 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3572 for(y = 0; y < height; ++y)
3576 for(x = 0; x < width; ++x)
3578 v = *red + adjustR; ++red;
3580 if(force8) { v = (v<<ushift) + (v>>dshift); }
3582 *d++ = (unsigned char)(v & mask);
3586 v = *alpha + adjustA; ++alpha;
3588 if(force8) { v = (v<<ushift) + (v>>dshift); }
3590 *d++ = (unsigned char)(v & mask);
3594 png_write_row(png, row_buf);
3602 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3605 png_write_end(png, info);
3613 png_destroy_write_struct(&png, &info);
3617 if(fails) remove(write_idf);
3621 #endif /* HAVE_LIBPNG */