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_apps_config.h"
39 #ifdef OPJ_HAVE_LIBTIFF
41 #endif /* OPJ_HAVE_LIBTIFF */
43 #ifdef OPJ_HAVE_LIBPNG
46 #endif /* OPJ_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, v, adjustR, adjustG, adjustB, fails;
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;
441 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
444 alpha_channel = image->numcomps-1;
446 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
448 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
449 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
450 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
452 for (y=0; y < height; y++)
454 unsigned int index=y*width;
456 for (x=0; x < width; x++, index++)
458 r = (float)(image->comps[0].data[index] + adjustR);
460 if (image->numcomps > 2)
462 g = (float)(image->comps[1].data[index] + adjustG);
463 b = (float)(image->comps[2].data[index] + adjustB);
471 /* TGA format writes BGR ... */
472 if(b > 255.) b = 255.; else if(b < 0.) b = 0.;
473 value = (unsigned char)(b*scale);
474 res = fwrite(&value,1,1,fdest);
478 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
481 if(g > 255.) g = 255.; else if(g < 0.) g = 0.;
482 value = (unsigned char)(g*scale);
483 res = fwrite(&value,1,1,fdest);
487 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
490 if(r > 255.) r = 255.; else if(r < 0.) r = 0.;
491 value = (unsigned char)(r*scale);
492 res = fwrite(&value,1,1,fdest);
496 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
502 a = (float)(image->comps[alpha_channel].data[index]);
503 if(a > 255.) a = 255.; else if(a < 0.) a = 0.;
504 value = (unsigned char)(a*scale);
505 res = fwrite(&value,1,1,fdest);
509 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
522 /* -->> -->> -->> -->>
526 <<-- <<-- <<-- <<-- */
528 /* WORD defines a two byte word */
529 typedef unsigned short int WORD;
531 /* DWORD defines a four byte word */
532 typedef unsigned int DWORD;
535 WORD bfType; /* 'BM' for Bitmap (19776) */
536 DWORD bfSize; /* Size of the file */
537 WORD bfReserved1; /* Reserved : 0 */
538 WORD bfReserved2; /* Reserved : 0 */
539 DWORD bfOffBits; /* Offset */
540 } BITMAPFILEHEADER_t;
543 DWORD biSize; /* Size of the structure in bytes */
544 DWORD biWidth; /* Width of the image in pixels */
545 DWORD biHeight; /* Heigth of the image in pixels */
546 WORD biPlanes; /* 1 */
547 WORD biBitCount; /* Number of color bits by pixels */
548 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
549 DWORD biSizeImage; /* Size of the image in bytes */
550 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
551 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
552 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
553 DWORD biClrImportant; /* Number of important color (0: ALL) */
554 } BITMAPINFOHEADER_t;
556 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
558 int subsampling_dx = parameters->subsampling_dx;
559 int subsampling_dy = parameters->subsampling_dy;
561 int i, numcomps, w, h;
562 OPJ_COLOR_SPACE color_space;
563 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
564 opj_image_t * image = NULL;
567 BITMAPFILEHEADER_t File_h;
568 BITMAPINFOHEADER_t Info_h;
570 unsigned char *table_R, *table_G, *table_B;
571 unsigned int j, PAD = 0;
578 IN = fopen(filename, "rb");
581 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
585 File_h.bfType = getc(IN);
586 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
588 if (File_h.bfType != 19778)
590 fprintf(stderr,"Error, not a BMP file!\n");
596 File_h.bfSize = getc(IN);
597 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
598 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
599 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
601 File_h.bfReserved1 = getc(IN);
602 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
604 File_h.bfReserved2 = getc(IN);
605 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
607 File_h.bfOffBits = getc(IN);
608 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
609 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
610 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
615 Info_h.biSize = getc(IN);
616 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
617 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
618 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
620 if(Info_h.biSize != 40)
622 fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize);
626 Info_h.biWidth = getc(IN);
627 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
628 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
629 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
632 Info_h.biHeight = getc(IN);
633 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
634 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
635 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
638 Info_h.biPlanes = getc(IN);
639 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
641 Info_h.biBitCount = getc(IN);
642 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
644 Info_h.biCompression = getc(IN);
645 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
646 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
647 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
649 Info_h.biSizeImage = getc(IN);
650 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
651 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
652 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
654 Info_h.biXpelsPerMeter = getc(IN);
655 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
656 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
657 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
659 Info_h.biYpelsPerMeter = getc(IN);
660 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
661 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
662 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
664 Info_h.biClrUsed = getc(IN);
665 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
666 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
667 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
669 Info_h.biClrImportant = getc(IN);
670 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
671 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
672 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
674 /* Read the data and store them in the OUT file */
676 if (Info_h.biBitCount == 24)
679 color_space = OPJ_CLRSPC_SRGB;
680 /* initialize image components */
681 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
682 for(i = 0; i < numcomps; i++)
684 cmptparm[i].prec = 8;
686 cmptparm[i].sgnd = 0;
687 cmptparm[i].dx = subsampling_dx;
688 cmptparm[i].dy = subsampling_dy;
692 /* create the image */
693 image = opj_image_create(numcomps, &cmptparm[0], color_space);
700 /* set image offset and reference grid */
701 image->x0 = parameters->image_offset_x0;
702 image->y0 = parameters->image_offset_y0;
703 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
704 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
708 /* Place the cursor at the beginning of the image information */
709 fseek(IN, 0, SEEK_SET);
710 fseek(IN, File_h.bfOffBits, SEEK_SET);
715 /* PAD = 4 - (3 * W) % 4; */
716 /* PAD = (PAD == 4) ? 0 : PAD; */
717 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
719 RGB = (unsigned char *)
720 malloc((3 * W + PAD) * H * sizeof(unsigned char));
722 if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
725 opj_image_destroy(image);
726 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
732 for(y = 0; y < (int)H; y++)
734 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
735 for(x = 0; x < (int)W; x++)
737 unsigned char *pixel = &scanline[3 * x];
738 image->comps[0].data[index] = pixel[2]; /* R */
739 image->comps[1].data[index] = pixel[1]; /* G */
740 image->comps[2].data[index] = pixel[0]; /* B */
745 }/* if (Info_h.biBitCount == 24) */
747 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
749 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
751 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
753 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
754 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
755 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
758 for (j = 0; j < Info_h.biClrUsed; j++)
760 table_B[j] = (unsigned char)getc(IN);
761 table_G[j] = (unsigned char)getc(IN);
762 table_R[j] = (unsigned char)getc(IN);
765 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
767 if(has_color) gray_scale = 0;
769 /* Place the cursor at the beginning of the image information */
770 fseek(IN, 0, SEEK_SET);
771 fseek(IN, File_h.bfOffBits, SEEK_SET);
775 if (Info_h.biWidth % 2)
778 numcomps = gray_scale ? 1 : 3;
779 color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB;
780 /* initialize image components */
781 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
782 for(i = 0; i < numcomps; i++)
784 cmptparm[i].prec = 8;
786 cmptparm[i].sgnd = 0;
787 cmptparm[i].dx = subsampling_dx;
788 cmptparm[i].dy = subsampling_dy;
792 /* create the image */
793 image = opj_image_create(numcomps, &cmptparm[0], color_space);
797 free(table_R); free(table_G); free(table_B);
801 /* set image offset and reference grid */
802 image->x0 = parameters->image_offset_x0;
803 image->y0 = parameters->image_offset_y0;
804 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
805 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
809 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
811 if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
817 opj_image_destroy(image);
818 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
824 for (j = 0; j < W * H; j++)
826 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
828 image->comps[0].data[index] =
829 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
838 for (j = 0; j < W * H; j++)
840 if ((j % W < W - 1 && Info_h.biWidth % 2)
841 || !(Info_h.biWidth % 2))
843 unsigned char pixel_index =
844 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
845 image->comps[0].data[index] = table_R[pixel_index];
846 image->comps[1].data[index] = table_G[pixel_index];
847 image->comps[2].data[index] = table_B[pixel_index];
858 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
860 unsigned char *pix, *beyond;
861 int *gray, *red, *green, *blue;
862 unsigned int x, y, max;
866 if (Info_h.biClrUsed == 0)
867 Info_h.biClrUsed = 256;
868 else if (Info_h.biClrUsed > 256)
869 Info_h.biClrUsed = 256;
871 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
872 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
873 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
876 for (j = 0; j < Info_h.biClrUsed; j++)
878 table_B[j] = (unsigned char)getc(IN);
879 table_G[j] = (unsigned char)getc(IN);
880 table_R[j] = (unsigned char)getc(IN);
882 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
888 numcomps = gray_scale ? 1 : 3;
889 color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB;
890 /* initialize image components */
891 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
892 for (i = 0; i < numcomps; i++)
894 cmptparm[i].prec = 8;
896 cmptparm[i].sgnd = 0;
897 cmptparm[i].dx = subsampling_dx;
898 cmptparm[i].dy = subsampling_dy;
902 /* create the image */
903 image = opj_image_create(numcomps, &cmptparm[0], color_space);
913 /* set image offset and reference grid */
914 image->x0 = parameters->image_offset_x0;
915 image->y0 = parameters->image_offset_y0;
916 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
917 - 1) * subsampling_dx + 1;
918 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
919 - 1) * subsampling_dy + 1;
923 /* Place the cursor at the beginning of the image information */
924 fseek(IN, 0, SEEK_SET);
925 fseek(IN, File_h.bfOffBits, SEEK_SET);
929 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
930 beyond = RGB + W * H;
942 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
943 *pix = (unsigned char)c1;
949 if (c == 0x00) /* EOL */
953 pix = RGB + x + (H - y - 1) * W;
955 else if (c == 0x01) /* EOP */
957 else if (c == 0x02) /* MOVE by dxdy */
963 pix = RGB + (H - y - 1) * W + x;
968 for (; i < c && x < W && pix < beyond; i++, x++, pix++)
971 *pix = (unsigned char)c1;
973 if (c & 1) /* skip padding byte */
981 gray = image->comps[0].data;
989 *gray++ = table_R[uc];
994 /*int *red, *green, *blue;*/
996 red = image->comps[0].data;
997 green = image->comps[1].data;
998 blue = image->comps[2].data;
1006 *red++ = table_R[uc];
1007 *green++ = table_G[uc];
1008 *blue++ = table_B[uc];
1019 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1020 "is not yet implemented [%d]\n", Info_h.biBitCount);
1026 int imagetobmp(opj_image_t * image, const char *outfile) {
1030 int adjustR, adjustG, adjustB;
1032 if (image->comps[0].prec < 8) {
1033 fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec);
1036 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1037 && image->comps[1].dx == image->comps[2].dx
1038 && image->comps[0].dy == image->comps[1].dy
1039 && image->comps[1].dy == image->comps[2].dy
1040 && image->comps[0].prec == image->comps[1].prec
1041 && image->comps[1].prec == image->comps[2].prec) {
1043 /* -->> -->> -->> -->>
1045 <<-- <<-- <<-- <<-- */
1047 fdest = fopen(outfile, "wb");
1049 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1053 w = image->comps[0].w;
1054 h = image->comps[0].h;
1056 fprintf(fdest, "BM");
1060 fprintf(fdest, "%c%c%c%c",
1061 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1062 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1063 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1064 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1065 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1066 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
1070 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1071 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1072 (unsigned char) ((w) >> 8) & 0xff,
1073 (unsigned char) ((w) >> 16) & 0xff,
1074 (unsigned char) ((w) >> 24) & 0xff);
1075 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1076 (unsigned char) ((h) >> 8) & 0xff,
1077 (unsigned char) ((h) >> 16) & 0xff,
1078 (unsigned char) ((h) >> 24) & 0xff);
1079 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1080 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1081 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1082 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
1083 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1084 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1085 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1086 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1087 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1088 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1089 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1091 if (image->comps[0].prec > 8) {
1092 adjustR = image->comps[0].prec - 8;
1093 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1097 if (image->comps[1].prec > 8) {
1098 adjustG = image->comps[1].prec - 8;
1099 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1103 if (image->comps[2].prec > 8) {
1104 adjustB = image->comps[2].prec - 8;
1105 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1110 for (i = 0; i < w * h; i++) {
1111 unsigned char rc, gc, bc;
1114 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1115 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1116 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1117 if(r > 255) r = 255; else if(r < 0) r = 0;
1118 rc = (unsigned char)r;
1120 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1121 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1122 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
1123 if(g > 255) g = 255; else if(g < 0) g = 0;
1124 gc = (unsigned char)g;
1126 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1127 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1128 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
1129 if(b > 255) b = 255; else if(b < 0) b = 0;
1130 bc = (unsigned char)b;
1132 fprintf(fdest, "%c%c%c", bc, gc, rc);
1134 if ((i + 1) % w == 0) {
1135 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
1136 fprintf(fdest, "%c", 0);
1140 } else { /* Gray-scale */
1142 /* -->> -->> -->> -->>
1143 8 bits non code (Gray scale)
1144 <<-- <<-- <<-- <<-- */
1146 fdest = fopen(outfile, "wb");
1147 w = image->comps[0].w;
1148 h = image->comps[0].h;
1150 fprintf(fdest, "BM");
1154 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1155 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1156 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1157 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1158 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1159 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1160 ((54 + 1024) >> 16) & 0xff,
1161 ((54 + 1024) >> 24) & 0xff);
1165 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1166 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1167 (unsigned char) ((w) >> 8) & 0xff,
1168 (unsigned char) ((w) >> 16) & 0xff,
1169 (unsigned char) ((w) >> 24) & 0xff);
1170 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1171 (unsigned char) ((h) >> 8) & 0xff,
1172 (unsigned char) ((h) >> 16) & 0xff,
1173 (unsigned char) ((h) >> 24) & 0xff);
1174 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1175 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1176 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1177 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
1178 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
1179 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
1180 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
1181 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1182 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1183 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1184 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1186 if (image->comps[0].prec > 8) {
1187 adjustR = image->comps[0].prec - 8;
1188 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1192 for (i = 0; i < 256; i++) {
1193 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1196 for (i = 0; i < w * h; i++) {
1199 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1200 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1201 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1202 if(r > 255) r = 255; else if(r < 0) r = 0;
1204 fprintf(fdest, "%c", (unsigned char)r);
1206 if ((i + 1) % w == 0) {
1207 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1208 fprintf(fdest, "%c", 0);
1217 /* -->> -->> -->> -->>
1221 <<-- <<-- <<-- <<-- */
1224 static unsigned char readuchar(FILE * f)
1227 if ( !fread(&c1, 1, 1, f) )
1229 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1235 static unsigned short readushort(FILE * f, int bigendian)
1237 unsigned char c1, c2;
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");
1249 return (c1 << 8) + c2;
1251 return (c2 << 8) + c1;
1254 static unsigned int readuint(FILE * f, int bigendian)
1256 unsigned char c1, c2, c3, c4;
1257 if ( !fread(&c1, 1, 1, f) )
1259 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1262 if ( !fread(&c2, 1, 1, f) )
1264 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1267 if ( !fread(&c3, 1, 1, f) )
1269 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1272 if ( !fread(&c4, 1, 1, f) )
1274 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1278 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1280 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1283 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1286 int i, numcomps, max;
1287 OPJ_COLOR_SPACE color_space;
1288 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1289 opj_image_t * image = NULL;
1290 int adjustS, ushift, dshift, force8;
1292 char endian1,endian2,sign;
1297 opj_image_comp_t *comp = NULL;
1300 color_space = OPJ_CLRSPC_GRAY;
1302 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1306 f = fopen(filename, "rb");
1308 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1312 fseek(f, 0, SEEK_SET);
1313 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1314 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1320 while (signtmp[i]!='\0') {
1321 if (signtmp[i]=='-') sign='-';
1326 if (endian1=='M' && endian2=='L') {
1328 } else if (endian2=='M' && endian1=='L') {
1331 fprintf(stderr, "Bad pgx header, please check input file\n");
1335 /* initialize image component */
1337 cmptparm.x0 = parameters->image_offset_x0;
1338 cmptparm.y0 = parameters->image_offset_y0;
1339 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1340 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1350 ushift = 8 - prec; dshift = prec - ushift;
1351 if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0;
1355 else ushift = dshift = force8 = adjustS = 0;
1357 cmptparm.prec = prec;
1358 cmptparm.bpp = prec;
1359 cmptparm.dx = parameters->subsampling_dx;
1360 cmptparm.dy = parameters->subsampling_dy;
1362 /* create the image */
1363 image = opj_image_create(numcomps, &cmptparm, color_space);
1368 /* set image offset and reference grid */
1369 image->x0 = cmptparm.x0;
1370 image->y0 = cmptparm.x0;
1371 image->x1 = cmptparm.w;
1372 image->y1 = cmptparm.h;
1374 /* set image data */
1376 comp = &image->comps[0];
1378 for (i = 0; i < w * h; i++) {
1382 v = readuchar(f) + adjustS;
1383 v = (v<<ushift) + (v>>dshift);
1384 comp->data[i] = (unsigned char)v;
1386 if(v > max) max = v;
1390 if (comp->prec == 8) {
1394 v = (char) readuchar(f);
1396 } else if (comp->prec <= 16) {
1398 v = readushort(f, bigendian);
1400 v = (short) readushort(f, bigendian);
1404 v = readuint(f, bigendian);
1406 v = (int) readuint(f, bigendian);
1414 comp->bpp = int_floorlog2(max) + 1;
1419 int imagetopgx(opj_image_t * image, const char *outfile)
1422 int i, j, compno, fails;
1425 for (compno = 0; compno < image->numcomps; compno++)
1427 opj_image_comp_t *comp = &image->comps[compno];
1428 char bname[256]; /* buffer for name */
1429 char *name = bname; /* pointer */
1432 const size_t olen = strlen(outfile);
1433 const size_t dotpos = olen - 4;
1434 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1436 if( outfile[dotpos] != '.' )
1438 /* `pgx` was recognized but there is no dot at expected position */
1439 fprintf(stderr, "ERROR -> Impossible happen." );
1444 name = (char*)malloc(total+1);
1446 strncpy(name, outfile, dotpos);
1447 /*if (image->numcomps > 1) {*/
1448 sprintf(name+dotpos, "_%d.pgx", compno);
1450 strcpy(name+dotpos, ".pgx");
1452 fdest = fopen(name, "wb");
1455 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1458 /* dont need name anymore */
1463 w = image->comps[compno].w;
1464 h = image->comps[compno].h;
1466 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+',
1469 if (comp->prec <= 8)
1472 if (comp->prec <= 16)
1477 for (i = 0; i < w * h; i++)
1479 int v, val = image->comps[compno].data[i];
1482 for (j = nbytes - 1; j >= 0; j--)
1484 v = (int)(val >> (j * 8));
1485 if(v > 255) v = 255; else if(v < 0) v = 0;
1486 byte = (unsigned char)v;
1487 res = fwrite(&byte, 1, 1, fdest);
1491 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1496 fclose(fdest); fdest = NULL;
1500 if(fdest) fclose(fdest);
1505 /* -->> -->> -->> -->>
1509 <<-- <<-- <<-- <<-- */
1513 int width, height, maxval, depth, format;
1514 char rgb, rgba, gray, graya, bw;
1518 static char *skip_white(char *s)
1522 if(*s == '\n' || *s == '\r') return NULL;
1523 if(isspace(*s)) { ++s; continue; }
1529 static char *skip_int(char *start, int *out_n)
1534 *out_n = 0; s = start;
1536 s = skip_white(start);
1537 if(s == NULL) return NULL;
1542 if( !isdigit(*s)) break;
1545 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1549 static char *skip_idf(char *start, char out_idf[256])
1554 s = skip_white(start);
1555 if(s == NULL) return NULL;
1560 if(isalpha(*s) || *s == '_') { ++s; continue; }
1563 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1567 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1570 int format, have_wh, end, ttype;
1571 char idf[256], type[256];
1574 if (fgets(line, 250, reader) == NULL)
1576 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1582 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1584 format = atoi(line + 1);
1585 if(format < 1 || format > 7)
1587 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1590 ph->format = format;
1591 ttype = end = have_wh = 0;
1593 while(fgets(line, 250, reader))
1595 if(*line == '#') continue;
1601 s = skip_idf(s, idf);
1603 if(s == NULL || *s == 0) return;
1605 if(strcmp(idf, "ENDHDR") == 0)
1609 if(strcmp(idf, "WIDTH") == 0)
1611 s = skip_int(s, &ph->width);
1612 if(s == NULL || *s == 0) return;
1616 if(strcmp(idf, "HEIGHT") == 0)
1618 s = skip_int(s, &ph->height);
1619 if(s == NULL || *s == 0) return;
1623 if(strcmp(idf, "DEPTH") == 0)
1625 s = skip_int(s, &ph->depth);
1626 if(s == NULL || *s == 0) return;
1630 if(strcmp(idf, "MAXVAL") == 0)
1632 s = skip_int(s, &ph->maxval);
1633 if(s == NULL || *s == 0) return;
1637 if(strcmp(idf, "TUPLTYPE") == 0)
1639 s = skip_idf(s, type);
1640 if(s == NULL || *s == 0) return;
1642 if(strcmp(type, "BLACKANDWHITE") == 0)
1644 ph->bw = 1; ttype = 1; continue;
1646 if(strcmp(type, "GRAYSCALE") == 0)
1648 ph->gray = 1; ttype = 1; continue;
1650 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1652 ph->graya = 1; ttype = 1; continue;
1654 if(strcmp(type, "RGB") == 0)
1656 ph->rgb = 1; ttype = 1; continue;
1658 if(strcmp(type, "RGB_ALPHA") == 0)
1660 ph->rgba = 1; ttype = 1; continue;
1662 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1665 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1667 } /* if(format == 7) */
1671 s = skip_int(s, &ph->width);
1673 s = skip_int(s, &ph->height);
1677 if(format == 1 || format == 4) break;
1681 if(format == 2 || format == 3 || format == 5 || format == 6)
1683 /* P2, P3, P5, P6: */
1684 s = skip_int(s, &ph->maxval);
1686 if(ph->maxval > 65535) return;
1689 }/* while(fgets( ) */
1690 if(format == 2 || format == 3 || format > 4)
1692 if(ph->maxval < 1 || ph->maxval > 65535) return;
1694 if(ph->width < 1 || ph->height < 1) return;
1700 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1702 if(ph->depth < 1 || ph->depth > 4) return;
1704 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1709 if(format != 1 && format != 4)
1711 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1715 if(ph->width && ph->height) ph->ok = 1;
1721 static int has_prec(int val)
1723 if(val < 2) return 1;
1724 if(val < 4) return 2;
1725 if(val < 8) return 3;
1726 if(val < 16) return 4;
1727 if(val < 32) return 5;
1728 if(val < 64) return 6;
1729 if(val < 128) return 7;
1730 if(val < 256) return 8;
1731 if(val < 512) return 9;
1732 if(val < 1024) return 10;
1733 if(val < 2048) return 11;
1734 if(val < 4096) return 12;
1735 if(val < 8192) return 13;
1736 if(val < 16384) return 14;
1737 if(val < 32768) return 15;
1741 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1742 int subsampling_dx = parameters->subsampling_dx;
1743 int subsampling_dy = parameters->subsampling_dy;
1746 int i, compno, numcomps, w, h, prec, format;
1747 OPJ_COLOR_SPACE color_space;
1748 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1749 opj_image_t * image = NULL;
1750 struct pnm_header header_info;
1752 if((fp = fopen(filename, "rb")) == NULL)
1754 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1757 memset(&header_info, 0, sizeof(struct pnm_header));
1759 read_pnm_header(fp, &header_info);
1761 if(!header_info.ok) { fclose(fp); return NULL; }
1763 format = header_info.format;
1767 case 1: /* ascii bitmap */
1768 case 4: /* raw bitmap */
1772 case 2: /* ascii greymap */
1773 case 5: /* raw greymap */
1777 case 3: /* ascii pixmap */
1778 case 6: /* raw pixmap */
1782 case 7: /* arbitrary map */
1783 numcomps = header_info.depth;
1786 default: fclose(fp); return NULL;
1789 color_space = OPJ_CLRSPC_GRAY;/* GRAY, GRAYA */
1791 color_space = OPJ_CLRSPC_SRGB;/* RGB, RGBA */
1793 prec = has_prec(header_info.maxval);
1795 if(prec < 8) prec = 8;
1797 w = header_info.width;
1798 h = header_info.height;
1799 subsampling_dx = parameters->subsampling_dx;
1800 subsampling_dy = parameters->subsampling_dy;
1802 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1804 for(i = 0; i < numcomps; i++)
1806 cmptparm[i].prec = prec;
1807 cmptparm[i].bpp = prec;
1808 cmptparm[i].sgnd = 0;
1809 cmptparm[i].dx = subsampling_dx;
1810 cmptparm[i].dy = subsampling_dy;
1814 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1816 if(!image) { fclose(fp); return NULL; }
1818 /* set image offset and reference grid */
1819 image->x0 = parameters->image_offset_x0;
1820 image->y0 = parameters->image_offset_y0;
1821 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1822 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1824 if((format == 2) || (format == 3)) /* ascii pixmap */
1828 for (i = 0; i < w * h; i++)
1830 for(compno = 0; compno < numcomps; compno++)
1833 if (fscanf(fp, "%u", &index) != 1)
1834 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1836 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1844 && ( header_info.gray || header_info.graya
1845 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1847 unsigned char c0, c1, one;
1851 for (i = 0; i < w * h; i++)
1853 for(compno = 0; compno < numcomps; compno++)
1855 if ( !fread(&c0, 1, 1, fp) )
1856 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1859 image->comps[compno].data[i] = c0;
1863 if ( !fread(&c1, 1, 1, fp) )
1864 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1866 image->comps[compno].data[i] = ((c0<<8) | c1);
1872 if(format == 1) /* ascii bitmap */
1874 for (i = 0; i < w * h; i++)
1878 if ( fscanf(fp, "%u", &index) != 1)
1879 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1881 image->comps[0].data[i] = (index?0:255);
1891 for(y = 0; y < h; ++y)
1895 for(x = 0; x < w; ++x)
1900 uc = (unsigned char)getc(fp);
1902 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1908 if((format == 7 && header_info.bw)) /*MONO*/
1912 for(i = 0; i < w * h; ++i)
1914 if ( !fread(&uc, 1, 1, fp) )
1915 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1916 image->comps[0].data[i] = (uc & 1)?0:255;
1924 int imagetopnm(opj_image_t * image, const char *outfile)
1926 int *red, *green, *blue, *alpha;
1928 int i, compno, ncomp;
1929 int adjustR, adjustG, adjustB, adjustA;
1930 int fails, two, want_gray, has_alpha, triple;
1933 const char *tmp = outfile;
1938 if((prec = image->comps[0].prec) > 16)
1940 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1941 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1944 two = has_alpha = 0; fails = 1;
1945 ncomp = image->numcomps;
1947 while (*tmp) ++tmp; tmp -= 2;
1948 want_gray = (*tmp == 'g' || *tmp == 'G');
1949 ncomp = image->numcomps;
1951 if(want_gray) ncomp = 1;
1953 if (ncomp == 2 /* GRAYA */
1954 || (ncomp > 2 /* RGB, RGBA */
1955 && image->comps[0].dx == image->comps[1].dx
1956 && image->comps[1].dx == image->comps[2].dx
1957 && image->comps[0].dy == image->comps[1].dy
1958 && image->comps[1].dy == image->comps[2].dy
1959 && image->comps[0].prec == image->comps[1].prec
1960 && image->comps[1].prec == image->comps[2].prec
1963 fdest = fopen(outfile, "wb");
1967 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1971 triple = (ncomp > 2);
1972 wr = image->comps[0].w; hr = image->comps[0].h;
1973 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1975 red = image->comps[0].data;
1979 green = image->comps[1].data;
1980 blue = image->comps[2].data;
1982 else green = blue = NULL;
1986 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1988 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1989 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1990 wr, hr, ncomp, max, tt);
1991 alpha = image->comps[ncomp - 1].data;
1992 adjustA = (image->comps[ncomp - 1].sgnd ?
1993 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1997 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1998 opj_version(), wr, hr, max);
2001 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2005 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2006 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2008 else adjustG = adjustB = 0;
2010 for(i = 0; i < wr * hr; ++i)
2014 v = *red + adjustR; ++red;
2015 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2018 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2022 v = *green + adjustG; ++green;
2023 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2026 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2028 v = *blue + adjustB; ++blue;
2029 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2032 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2038 v = *alpha + adjustA; ++alpha;
2039 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2042 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2050 if(v > 255) v = 255; else if(v < 0) v = 0;
2052 fprintf(fdest, "%c", (unsigned char)v);
2056 if(v > 255) v = 255; else if(v < 0) v = 0;
2058 fprintf(fdest, "%c", (unsigned char)v);
2060 if(v > 255) v = 255; else if(v < 0) v = 0;
2062 fprintf(fdest, "%c", (unsigned char)v);
2067 if(v > 255) v = 255; else if(v < 0) v = 0;
2069 fprintf(fdest, "%c", (unsigned char)v);
2073 fclose(fdest); return 0;
2078 if (image->numcomps > ncomp)
2080 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
2081 fprintf(stderr," is written to the file\n");
2083 destname = (char*)malloc(strlen(outfile) + 8);
2085 for (compno = 0; compno < ncomp; compno++)
2088 sprintf(destname, "%d.%s", compno, outfile);
2090 sprintf(destname, "%s", outfile);
2092 fdest = fopen(destname, "wb");
2095 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2099 wr = image->comps[compno].w; hr = image->comps[compno].h;
2100 prec = image->comps[compno].prec;
2101 max = (1<<prec) - 1;
2103 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2104 opj_version(), wr, hr, max);
2106 red = image->comps[compno].data;
2108 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2112 for (i = 0; i < wr * hr; i++)
2114 v = *red + adjustR; ++red;
2115 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2118 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2123 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2126 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2130 else /* prec <= 8 */
2132 for(i = 0; i < wr * hr; ++i)
2134 v = *red + adjustR; ++red;
2135 if(v > 255) v = 255; else if(v < 0) v = 0;
2137 fprintf(fdest, "%c", (unsigned char)v);
2147 #ifdef OPJ_HAVE_LIBTIFF
2148 /* -->> -->> -->> -->>
2152 <<-- <<-- <<-- <<-- */
2154 int imagetotif(opj_image_t * image, const char *outfile)
2156 int width, height, imgsize;
2157 int bps,index,adjust, sgnd;
2158 int ushift, dshift, has_alpha, force16;
2164 ushift = dshift = force16 = has_alpha = 0;
2165 bps = image->comps[0].prec;
2167 if(bps > 8 && bps < 16)
2169 ushift = 16 - bps; dshift = bps - ushift;
2170 bps = 16; force16 = 1;
2173 if(bps != 8 && bps != 16)
2175 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2177 fprintf(stderr,"\tAborting\n");
2180 tif = TIFFOpen(outfile, "wb");
2184 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2187 sgnd = image->comps[0].sgnd;
2188 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2190 if(image->numcomps >= 3
2191 && image->comps[0].dx == image->comps[1].dx
2192 && image->comps[1].dx == image->comps[2].dx
2193 && image->comps[0].dy == image->comps[1].dy
2194 && image->comps[1].dy == image->comps[2].dy
2195 && image->comps[0].prec == image->comps[1].prec
2196 && image->comps[1].prec == image->comps[2].prec)
2198 has_alpha = (image->numcomps == 4);
2200 width = image->comps[0].w;
2201 height = image->comps[0].h;
2202 imgsize = width * height ;
2204 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2205 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2206 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2207 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2208 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2209 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2210 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2211 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2212 strip_size = TIFFStripSize(tif);
2213 buf = _TIFFmalloc(strip_size);
2216 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2218 unsigned char *dat8;
2219 tsize_t i, ssize, last_i = 0;
2221 ssize = TIFFStripSize(tif);
2222 dat8 = (unsigned char*)buf;
2226 step = 3 + has_alpha;
2229 for(i=0; i < ssize - restx; i += step)
2235 r = image->comps[0].data[index];
2236 g = image->comps[1].data[index];
2237 b = image->comps[2].data[index];
2238 if(has_alpha) a = image->comps[3].data[index];
2245 if(has_alpha) a += adjust;
2247 if(r > 255) r = 255; else if(r < 0) r = 0;
2249 if(g > 255) g = 255; else if(g < 0) g = 0;
2251 if(b > 255) b = 255; else if(b < 0) b = 0;
2255 if(a > 255) a = 255; else if(a < 0) a = 0;
2268 for(i = last_i; i < ssize; i += step)
2274 r = image->comps[0].data[index];
2275 g = image->comps[1].data[index];
2276 b = image->comps[2].data[index];
2277 if(has_alpha) a = image->comps[3].data[index];
2284 if(has_alpha) a += adjust;
2286 if(r > 255) r = 255; else if(r < 0) r = 0;
2287 if(g > 255) g = 255; else if(g < 0) g = 0;
2288 if(b > 255) b = 255; else if(b < 0) b = 0;
2291 if(i+1 < ssize) dat8[i+1] = g ; else break;
2292 if(i+2 < ssize) dat8[i+2] = b ; else break;
2295 if(a > 255) a = 255; else if(a < 0) a = 0;
2297 if(i+3 < ssize) dat8[i+3] = a ; else break;
2304 }/*if(last_i < ssize)*/
2310 step = 6 + has_alpha + has_alpha;
2313 for(i = 0; i < ssize - restx ; i += step)
2319 r = image->comps[0].data[index];
2320 g = image->comps[1].data[index];
2321 b = image->comps[2].data[index];
2322 if(has_alpha) a = image->comps[3].data[index];
2329 if(has_alpha) a += adjust;
2333 r = (r<<ushift) + (r>>dshift);
2334 g = (g<<ushift) + (g>>dshift);
2335 b = (b<<ushift) + (b>>dshift);
2336 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2338 if(r > 65535) r = 65535; else if(r < 0) r = 0;
2339 if(g > 65535) g = 65535; else if(g < 0) g = 0;
2340 if(b > 65535) b = 65535; else if(b < 0) b = 0;
2342 dat8[i+0] = r;/*LSB*/
2343 dat8[i+1] = (r >> 8);/*MSB*/
2345 dat8[i+3] = (g >> 8);
2347 dat8[i+5] = (b >> 8);
2350 if(a > 65535) a = 65535; else if(a < 0) a = 0;
2352 dat8[i+7] = (a >> 8);
2363 for(i = last_i ; i < ssize ; i += step)
2369 r = image->comps[0].data[index];
2370 g = image->comps[1].data[index];
2371 b = image->comps[2].data[index];
2372 if(has_alpha) a = image->comps[3].data[index];
2379 if(has_alpha) a += adjust;
2383 r = (r<<ushift) + (r>>dshift);
2384 g = (g<<ushift) + (g>>dshift);
2385 b = (b<<ushift) + (b>>dshift);
2386 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2388 if(r > 65535) r = 65535; else if(r < 0) r = 0;
2389 if(g > 65535) g = 65535; else if(g < 0) g = 0;
2390 if(b > 65535) b = 65535; else if(b < 0) b = 0;
2392 dat8[i+0] = r;/*LSB*/
2393 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/
2394 if(i+2 < ssize) dat8[i+2] = g; else break;
2395 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2396 if(i+4 < ssize) dat8[i+4] = b; else break;
2397 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2401 if(a > 65535) a = 65535; else if(a < 0) a = 0;
2402 if(i+6 < ssize) dat8[i+6] = a; else break;
2403 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2410 }/*if(last_i < ssize)*/
2413 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2414 }/*for(strip = 0; )*/
2416 _TIFFfree((void*)buf);
2422 if(image->numcomps == 1 /* GRAY */
2423 || ( image->numcomps == 2 /* GRAY_ALPHA */
2424 && image->comps[0].dx == image->comps[1].dx
2425 && image->comps[0].dy == image->comps[1].dy
2426 && image->comps[0].prec == image->comps[1].prec))
2430 has_alpha = (image->numcomps == 2);
2432 width = image->comps[0].w;
2433 height = image->comps[0].h;
2434 imgsize = width * height;
2437 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2438 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2439 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2440 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2441 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2442 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2443 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2444 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2446 /* Get a buffer for the data */
2447 strip_size = TIFFStripSize(tif);
2448 buf = _TIFFmalloc(strip_size);
2451 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2453 unsigned char *dat8;
2454 tsize_t i, ssize = TIFFStripSize(tif);
2455 dat8 = (unsigned char*)buf;
2459 step = 1 + has_alpha;
2461 for(i=0; i < ssize; i += step)
2467 r = image->comps[0].data[index];
2468 if(has_alpha) a = image->comps[1].data[index];
2473 if(has_alpha) a += adjust;
2475 if(r > 255) r = 255; else if(r < 0) r = 0;
2480 if(a > 255) a = 255; else if(a < 0) a = 0;
2492 step = 2 + has_alpha + has_alpha;
2494 for(i=0; i < ssize; i += step)
2500 r = image->comps[0].data[index];
2501 if(has_alpha) a = image->comps[1].data[index];
2506 if(has_alpha) a += adjust;
2510 r = (r<<ushift) + (r>>dshift);
2511 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2513 if(r > 65535) r = 65535; else if(r < 0) r = 0;
2514 dat8[i+0] = r;/*LSB*/
2515 dat8[i+1] = r >> 8;/*MSB*/
2518 if(a > 65535) a = 65535; else if(a < 0) a = 0;
2523 }/*if(index < imgsize)*/
2528 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2539 fprintf(stderr,"imagetotif: Bad color format.\n"
2540 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2541 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2548 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2549 * CINEMA : 12 bit precision
2551 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2553 int subsampling_dx = parameters->subsampling_dx;
2554 int subsampling_dy = parameters->subsampling_dy;
2559 int j, numcomps, w, h,index;
2560 OPJ_COLOR_SPACE color_space;
2561 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2562 opj_image_t *image = NULL;
2565 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2566 unsigned int tiWidth, tiHeight;
2568 tif = TIFFOpen(filename, "r");
2572 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2575 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2576 tiWidth = tiHeight = 0;
2578 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2579 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2580 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2581 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2582 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2583 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2584 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2588 if(tiBps != 8 && tiBps != 16 && tiBps != 12) tiBps = 0;
2589 if(tiPhoto != 1 && tiPhoto != 2) tiPhoto = 0;
2591 if( !tiBps || !tiPhoto)
2594 fprintf(stderr,"tiftoimage: Bits=%d, Only 8 and 16 bits"
2595 " implemented\n",tiBps);
2598 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2599 " and GRAY(A) has been implemented\n",(int) tiPhoto);
2601 fprintf(stderr,"\tAborting\n");
2607 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2609 uint16 extrasamples;
2611 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2612 &extrasamples, &sampleinfo);
2614 if(extrasamples >= 1)
2616 switch(sampleinfo[0])
2618 case EXTRASAMPLE_UNSPECIFIED:
2619 /* Workaround for some images without correct info about alpha channel
2625 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2626 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2631 else /* extrasamples == 0 */
2632 if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
2635 /* initialize image components
2637 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2639 if ((tiPhoto == PHOTOMETRIC_RGB) && (parameters->cp_cinema)) {
2640 fprintf(stdout,"WARNING:\n"
2641 "Input image bitdepth is %d bits\n"
2642 "TIF conversion has automatically rescaled to 12-bits\n"
2643 "to comply with cinema profiles.\n",
2647 if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2649 numcomps = 3 + has_alpha;
2650 color_space = OPJ_CLRSPC_SRGB;
2652 /*#define USETILEMODE*/
2653 for(j = 0; j < numcomps; j++)
2655 if(parameters->cp_cinema)
2657 cmptparm[j].prec = 12;
2658 cmptparm[j].bpp = 12;
2662 cmptparm[j].prec = tiBps;
2663 cmptparm[j].bpp = tiBps;
2665 cmptparm[j].dx = subsampling_dx;
2666 cmptparm[j].dy = subsampling_dy;
2676 image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
2678 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2686 /* set image offset and reference grid
2688 image->x0 = parameters->image_offset_x0;
2689 image->y0 = parameters->image_offset_y0;
2690 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2691 image->x0 + (w - 1) * subsampling_dx + 1;
2692 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2693 image->y0 + (h - 1) * subsampling_dy + 1;
2695 buf = _TIFFmalloc(TIFFStripSize(tif));
2697 strip_size=TIFFStripSize(tif);
2699 imgsize = image->comps[0].w * image->comps[0].h ;
2700 /* Read the Image components
2702 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2704 unsigned char *dat8;
2707 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2708 dat8 = (unsigned char*)buf;
2712 step = 6 + has_alpha + has_alpha;
2714 for(i = 0; i < ssize; i += step)
2718 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */
2719 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
2720 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
2722 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2724 if(parameters->cp_cinema)
2726 /* Rounding 16 to 12 bits
2728 image->comps[0].data[index] =
2729 (image->comps[0].data[index] + 0x08) >> 4 ;
2730 image->comps[1].data[index] =
2731 (image->comps[1].data[index] + 0x08) >> 4 ;
2732 image->comps[2].data[index] =
2733 (image->comps[2].data[index] + 0x08) >> 4 ;
2735 image->comps[3].data[index] =
2736 (image->comps[3].data[index] + 0x08) >> 4 ;
2743 }/*if(tiBps == 16)*/
2747 step = 3 + has_alpha;
2749 for(i = 0; i < ssize; i += step)
2754 image->comps[0].data[index] = dat8[i+0];/* R */
2755 image->comps[1].data[index] = dat8[i+1];/* G */
2756 image->comps[2].data[index] = dat8[i+2];/* B */
2758 image->comps[3].data[index] = dat8[i+3];
2761 if(parameters->cp_cinema)
2763 /* Rounding 8 to 12 bits
2766 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2767 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2768 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2770 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2778 }/*if( tiBps == 8)*/
2780 if(tiBps == 12)/* CINEMA file */
2784 for(i = 0; i < ssize; i += step)
2786 if((index < imgsize)&(index+1 < imgsize))
2788 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2789 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2791 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2792 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2794 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2795 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2803 }/*for(strip = 0; )*/
2811 if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2813 numcomps = 1 + has_alpha;
2814 color_space = OPJ_CLRSPC_GRAY;
2816 for(j = 0; j < numcomps; ++j)
2818 cmptparm[j].prec = tiBps;
2819 cmptparm[j].bpp = tiBps;
2820 cmptparm[j].dx = subsampling_dx;
2821 cmptparm[j].dy = subsampling_dy;
2826 image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
2828 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2836 /* set image offset and reference grid
2838 image->x0 = parameters->image_offset_x0;
2839 image->y0 = parameters->image_offset_y0;
2840 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2841 image->x0 + (w - 1) * subsampling_dx + 1;
2842 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2843 image->y0 + (h - 1) * subsampling_dy + 1;
2845 buf = _TIFFmalloc(TIFFStripSize(tif));
2847 strip_size = TIFFStripSize(tif);
2849 imgsize = image->comps[0].w * image->comps[0].h ;
2850 /* Read the Image components
2852 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2854 unsigned char *dat8;
2858 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2859 dat8 = (unsigned char*)buf;
2863 step = 2 + has_alpha + has_alpha;
2865 for(i = 0; i < ssize; i += step)
2869 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2871 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2881 step = 1 + has_alpha;
2883 for(i = 0; i < ssize; i += step)
2887 image->comps[0].data[index] = dat8[i+0];
2889 image->comps[1].data[index] = dat8[i+1];
2907 #endif /* OPJ_HAVE_LIBTIFF */
2909 /* -->> -->> -->> -->>
2913 <<-- <<-- <<-- <<-- */
2914 static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian) {
2915 int subsampling_dx = parameters->subsampling_dx;
2916 int subsampling_dy = parameters->subsampling_dy;
2919 int i, compno, numcomps, w, h;
2920 OPJ_COLOR_SPACE color_space;
2921 opj_image_cmptparm_t *cmptparm;
2922 opj_image_t * image = NULL;
2925 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2927 fprintf(stderr,"\nError: invalid raw image parameters\n");
2928 fprintf(stderr,"Please use the Format option -F:\n");
2929 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2930 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2931 fprintf(stderr,"Aborting\n");
2935 f = fopen(filename, "rb");
2937 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2938 fprintf(stderr,"Aborting\n");
2941 numcomps = raw_cp->rawComp;
2942 color_space = OPJ_CLRSPC_SRGB;
2943 w = raw_cp->rawWidth;
2944 h = raw_cp->rawHeight;
2945 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2947 /* initialize image components */
2948 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2949 for(i = 0; i < numcomps; i++) {
2950 cmptparm[i].prec = raw_cp->rawBitDepth;
2951 cmptparm[i].bpp = raw_cp->rawBitDepth;
2952 cmptparm[i].sgnd = raw_cp->rawSigned;
2953 cmptparm[i].dx = subsampling_dx;
2954 cmptparm[i].dy = subsampling_dy;
2958 /* create the image */
2959 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2965 /* set image offset and reference grid */
2966 image->x0 = parameters->image_offset_x0;
2967 image->y0 = parameters->image_offset_y0;
2968 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2969 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2971 if(raw_cp->rawBitDepth <= 8)
2973 unsigned char value = 0;
2974 for(compno = 0; compno < numcomps; compno++) {
2975 for (i = 0; i < w * h; i++) {
2976 if (!fread(&value, 1, 1, f)) {
2977 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2980 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2984 else if(raw_cp->rawBitDepth <= 16)
2986 unsigned short value;
2987 for(compno = 0; compno < numcomps; compno++) {
2988 for (i = 0; i < w * h; i++) {
2989 unsigned char temp1;
2990 unsigned char temp2;
2991 if (!fread(&temp1, 1, 1, f)) {
2992 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2995 if (!fread(&temp2, 1, 1, f)) {
2996 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
3009 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
3014 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3018 if (fread(&ch, 1, 1, f)) {
3019 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
3026 opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
3027 return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE);
3030 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
3031 return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE);
3034 static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL big_endian)
3036 FILE *rawFile = NULL;
3040 int line, row, curr, mask;
3044 if((image->numcomps * image->x1 * image->y1) == 0)
3046 fprintf(stderr,"\nError: invalid raw image parameters\n");
3050 rawFile = fopen(outfile, "wb");
3052 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3057 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
3059 for(compno = 0; compno < image->numcomps; compno++)
3061 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
3062 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
3064 w = image->comps[compno].w;
3065 h = image->comps[compno].h;
3067 if(image->comps[compno].prec <= 8)
3069 if(image->comps[compno].sgnd == 1)
3071 mask = (1 << image->comps[compno].prec) - 1;
3072 ptr = image->comps[compno].data;
3073 for (line = 0; line < h; line++) {
3074 for(row = 0; row < w; row++) {
3076 if(curr > 127) curr = 127; else if(curr < -128) curr = -128;
3077 uc = (unsigned char) (curr & mask);
3078 res = fwrite(&uc, 1, 1, rawFile);
3080 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3087 else if(image->comps[compno].sgnd == 0)
3089 mask = (1 << image->comps[compno].prec) - 1;
3090 ptr = image->comps[compno].data;
3091 for (line = 0; line < h; line++) {
3092 for(row = 0; row < w; row++) {
3094 if(curr > 255) curr = 255; else if(curr < 0) curr = 0;
3095 uc = (unsigned char) (curr & mask);
3096 res = fwrite(&uc, 1, 1, rawFile);
3098 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3106 else if(image->comps[compno].prec <= 16)
3108 if(image->comps[compno].sgnd == 1)
3110 union { signed short val; signed char vals[2] } uc;
3111 mask = (1 << image->comps[compno].prec) - 1;
3112 ptr = image->comps[compno].data;
3113 for (line = 0; line < h; line++) {
3114 for(row = 0; row < w; row++) {
3116 if(curr > 32767 ) curr = 32767; else if( curr < -32768) curr = -32768;
3117 uc.val = (signed short)(curr & mask);
3118 res = fwrite(uc.vals, 1, 2, rawFile);
3120 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
3127 else if(image->comps[compno].sgnd == 0)
3129 union { unsigned short val; unsigned char vals[2] } uc;
3130 mask = (1 << image->comps[compno].prec) - 1;
3131 ptr = image->comps[compno].data;
3132 for (line = 0; line < h; line++) {
3133 for(row = 0; row < w; row++) {
3135 if(curr > 65536 ) curr = 65536; else if( curr < 0) curr = 0;
3136 uc.val = (unsigned short)(curr & mask);
3137 res = fwrite(uc.vals, 1, 2, rawFile);
3139 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
3147 else if (image->comps[compno].prec <= 32)
3149 fprintf(stderr,"More than 16 bits per component no handled yet\n");
3154 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
3164 int imagetoraw(opj_image_t * image, const char *outfile)
3166 return imagetoraw_common(image, outfile, OPJ_TRUE);
3169 int imagetorawl(opj_image_t * image, const char *outfile)
3171 return imagetoraw_common(image, outfile, OPJ_FALSE);
3174 #ifdef OPJ_HAVE_LIBPNG
3176 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3177 #define MAGIC_SIZE 8
3178 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3180 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3184 double gamma, display_exponent;
3185 int bit_depth, interlace_type,compression_type, filter_type;
3187 png_uint_32 resx, resy;
3189 png_uint_32 width, height;
3190 int color_type, has_alpha, is16;
3193 unsigned char **rows;
3196 opj_image_cmptparm_t cmptparm[4];
3198 unsigned int nr_comp;
3200 unsigned char sigbuf[8];
3202 if((reader = fopen(read_idf, "rb")) == NULL)
3204 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
3207 image = NULL; png = NULL; rows = NULL;
3209 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3210 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
3212 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
3215 /* libpng-VERSION/example.c:
3216 * PC : screen_gamma = 2.2;
3217 * Mac: screen_gamma = 1.7 or 1.0;
3219 display_exponent = 2.2;
3221 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3222 NULL, NULL, NULL)) == NULL)
3224 if((info = png_create_info_struct(png)) == NULL)
3227 if(setjmp(png_jmpbuf(png)))
3230 png_init_io(png, reader);
3231 png_set_sig_bytes(png, MAGIC_SIZE);
3233 png_read_info(png, info);
3235 if(png_get_IHDR(png, info, &width, &height,
3236 &bit_depth, &color_type, &interlace_type,
3237 &compression_type, &filter_type) == 0)
3240 /* png_set_expand():
3241 * expand paletted images to RGB, expand grayscale images of
3242 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3243 * to alpha channels.
3245 if(color_type == PNG_COLOR_TYPE_PALETTE)
3246 png_set_expand(png);
3248 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3249 png_set_expand(png);
3251 if(png_get_valid(png, info, PNG_INFO_tRNS))
3252 png_set_expand(png);
3254 is16 = (bit_depth == 16);
3256 /* GRAY => RGB; GRAY_ALPHA => RGBA
3258 if(color_type == PNG_COLOR_TYPE_GRAY
3259 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3261 png_set_gray_to_rgb(png);
3263 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
3264 PNG_COLOR_TYPE_RGB_ALPHA);
3266 if( !png_get_gAMA(png, info, &gamma))
3269 png_set_gamma(png, display_exponent, gamma);
3271 png_read_update_info(png, info);
3273 png_get_pHYs(png, info, &resx, &resy, &unit);
3275 color_type = png_get_color_type(png, info);
3277 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3279 nr_comp = 3 + has_alpha;
3281 bit_depth = png_get_bit_depth(png, info);
3283 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
3284 for(i = 0; i < height; ++i)
3285 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
3287 png_read_image(png, rows);
3289 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3291 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3293 for(i = 0; i < nr_comp; ++i)
3295 cmptparm[i].prec = bit_depth;
3296 /* bits_per_pixel: 8 or 16 */
3297 cmptparm[i].bpp = bit_depth;
3298 cmptparm[i].sgnd = 0;
3299 cmptparm[i].dx = sub_dx;
3300 cmptparm[i].dy = sub_dy;
3301 cmptparm[i].w = width;
3302 cmptparm[i].h = height;
3305 image = opj_image_create(nr_comp, &cmptparm[0], OPJ_CLRSPC_SRGB);
3307 if(image == NULL) goto fin;
3309 image->x0 = params->image_offset_x0;
3310 image->y0 = params->image_offset_y0;
3311 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3312 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3314 r = image->comps[0].data;
3315 g = image->comps[1].data;
3316 b = image->comps[2].data;
3317 a = image->comps[3].data;
3319 for(i = 0; i < height; ++i)
3323 for(j = 0; j < width; ++j)
3327 *r++ = s[0]<<8|s[1]; s += 2;
3329 *g++ = s[0]<<8|s[1]; s += 2;
3331 *b++ = s[0]<<8|s[1]; s += 2;
3333 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3337 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3339 if(has_alpha) *a++ = *s++;
3345 for(i = 0; i < height; ++i)
3350 png_destroy_read_struct(&png, &info, NULL);
3358 int imagetopng(opj_image_t * image, const char *write_idf)
3363 int *red, *green, *blue, *alpha;
3364 unsigned char *row_buf, *d;
3365 int has_alpha, width, height, nr_comp, color_type;
3366 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3367 int prec, ushift, dshift, is16, force16, force8;
3368 unsigned short mask = 0xffff;
3369 png_color_8 sig_bit;
3371 is16 = force16 = force8 = ushift = dshift = 0; fails = 1;
3372 prec = image->comps[0].prec;
3373 nr_comp = image->numcomps;
3375 if(prec > 8 && prec < 16)
3377 ushift = 16 - prec; dshift = prec - ushift;
3378 prec = 16; force16 = 1;
3381 if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */
3383 ushift = 8 - prec; dshift = 8 - ushift;
3384 prec = 8; force8 = 1;
3387 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3389 fprintf(stderr,"imagetopng: can not create %s"
3390 "\n\twrong bit_depth %d\n", write_idf, prec);
3393 writer = fopen(write_idf, "wb");
3395 if(writer == NULL) return fails;
3397 info = NULL; has_alpha = 0;
3399 /* Create and initialize the png_struct with the desired error handler
3400 * functions. If you want to use the default stderr and longjump method,
3401 * you can supply NULL for the last three parameters. We also check that
3402 * the library version is compatible with the one used at compile time,
3403 * in case we are using dynamically linked libraries. REQUIRED.
3405 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3407 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3409 if(png == NULL) goto fin;
3411 /* Allocate/initialize the image information data. REQUIRED
3413 info = png_create_info_struct(png);
3415 if(info == NULL) goto fin;
3417 /* Set error handling. REQUIRED if you are not supplying your own
3418 * error handling functions in the png_create_write_struct() call.
3420 if(setjmp(png_jmpbuf(png))) goto fin;
3422 /* I/O initialization functions is REQUIRED
3424 png_init_io(png, writer);
3426 /* Set the image information here. Width and height are up to 2^31,
3427 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3428 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3429 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3430 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3431 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3432 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3437 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3438 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3439 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3440 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3443 png_set_compression_level(png, Z_BEST_COMPRESSION);
3445 if(prec == 16) mask = 0xffff;
3447 if(prec == 8) mask = 0x00ff;
3449 if(prec == 4) mask = 0x000f;
3451 if(prec == 2) mask = 0x0003;
3453 if(prec == 1) mask = 0x0001;
3456 && image->comps[0].dx == image->comps[1].dx
3457 && image->comps[1].dx == image->comps[2].dx
3458 && image->comps[0].dy == image->comps[1].dy
3459 && image->comps[1].dy == image->comps[2].dy
3460 && image->comps[0].prec == image->comps[1].prec
3461 && image->comps[1].prec == image->comps[2].prec)
3465 has_alpha = (nr_comp > 3);
3467 is16 = (prec == 16);
3469 width = image->comps[0].w;
3470 height = image->comps[0].h;
3472 red = image->comps[0].data;
3473 green = image->comps[1].data;
3474 blue = image->comps[2].data;
3476 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3480 sig_bit.alpha = prec;
3481 alpha = image->comps[3].data;
3482 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3483 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3487 sig_bit.alpha = 0; alpha = NULL;
3488 color_type = PNG_COLOR_TYPE_RGB;
3491 png_set_sBIT(png, info, &sig_bit);
3493 png_set_IHDR(png, info, width, height, prec,
3496 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3498 png_set_gamma(png, 2.2, 1./2.2);
3499 png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL);
3500 /*=============================*/
3501 png_write_info(png, info);
3502 /*=============================*/
3505 png_set_packing(png);
3507 printf("%s:%d:sgnd(%d,%d,%d) w(%d) h(%d) alpha(%d)\n",__FILE__,__LINE__,
3508 image->comps[0].sgnd,
3509 image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha);
3511 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3512 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3513 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3515 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3517 for(y = 0; y < height; ++y)
3521 for(x = 0; x < width; ++x)
3525 v = *red + adjustR; ++red;
3526 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3528 if(force16) { v = (v<<ushift) + (v>>dshift); }
3530 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3532 v = *green + adjustG; ++green;
3533 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3535 if(force16) { v = (v<<ushift) + (v>>dshift); }
3537 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3539 v = *blue + adjustB; ++blue;
3540 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3542 if(force16) { v = (v<<ushift) + (v>>dshift); }
3544 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3548 v = *alpha + adjustA; ++alpha;
3549 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3551 if(force16) { v = (v<<ushift) + (v>>dshift); }
3553 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3558 v = *red + adjustR; ++red;
3559 if(v > 255) v = 255; else if(v < 0) v = 0;
3561 if(force8) { v = (v<<ushift) + (v>>dshift); }
3563 *d++ = (unsigned char)(v & mask);
3565 v = *green + adjustG; ++green;
3566 if(v > 255) v = 255; else if(v < 0) v = 0;
3568 if(force8) { v = (v<<ushift) + (v>>dshift); }
3570 *d++ = (unsigned char)(v & mask);
3572 v = *blue + adjustB; ++blue;
3573 if(v > 255) v = 255; else if(v < 0) v = 0;
3575 if(force8) { v = (v<<ushift) + (v>>dshift); }
3577 *d++ = (unsigned char)(v & mask);
3581 v = *alpha + adjustA; ++alpha;
3582 if(v > 255) v = 255; else if(v < 0) v = 0;
3584 if(force8) { v = (v<<ushift) + (v>>dshift); }
3586 *d++ = (unsigned char)(v & mask);
3590 png_write_row(png, row_buf);
3597 if(nr_comp == 1 /* GRAY */
3598 || ( nr_comp == 2 /* GRAY_ALPHA */
3599 && image->comps[0].dx == image->comps[1].dx
3600 && image->comps[0].dy == image->comps[1].dy
3601 && image->comps[0].prec == image->comps[1].prec))
3605 red = image->comps[0].data;
3607 sig_bit.gray = prec;
3608 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3609 alpha = NULL; adjustA = 0;
3610 color_type = PNG_COLOR_TYPE_GRAY;
3614 has_alpha = 1; sig_bit.alpha = prec;
3615 alpha = image->comps[1].data;
3616 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3617 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3619 width = image->comps[0].w;
3620 height = image->comps[0].h;
3622 png_set_IHDR(png, info, width, height, sig_bit.gray,
3625 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3627 png_set_sBIT(png, info, &sig_bit);
3629 png_set_gamma(png, 2.2, 1./2.2);
3630 png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL);
3631 /*=============================*/
3632 png_write_info(png, info);
3633 /*=============================*/
3634 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3638 png_set_packing(png);
3643 row_buf = (unsigned char*)
3644 malloc(width * nr_comp * sizeof(unsigned short));
3646 for(y = 0; y < height; ++y)
3650 for(x = 0; x < width; ++x)
3652 v = *red + adjustR; ++red;
3653 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3655 if(force16) { v = (v<<ushift) + (v>>dshift); }
3657 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3662 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3664 if(force16) { v = (v<<ushift) + (v>>dshift); }
3666 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3669 png_write_row(png, row_buf);
3674 else /* prec <= 8 */
3676 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3678 for(y = 0; y < height; ++y)
3682 for(x = 0; x < width; ++x)
3684 v = *red + adjustR; ++red;
3685 if(v > 255) v = 255; else if(v < 0) v = 0;
3687 if(force8) { v = (v<<ushift) + (v>>dshift); }
3689 *d++ = (unsigned char)(v & mask);
3693 v = *alpha + adjustA; ++alpha;
3694 if(v > 255) v = 255; else if(v < 0) v = 0;
3696 if(force8) { v = (v<<ushift) + (v>>dshift); }
3698 *d++ = (unsigned char)(v & mask);
3702 png_write_row(png, row_buf);
3710 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3713 png_write_end(png, info);
3721 png_destroy_write_struct(&png, &info);
3725 if(fails) remove(write_idf);
3729 #endif /* OPJ_HAVE_LIBPNG */