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 ORDER_BIGENDIAN
98 return( ((val & 0xff) << 8) + (val >> 8) );
105 #define TGA_HEADER_SIZE 18
107 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 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
185 unsigned short image_w, image_h, us0;
186 unsigned char uc0, image_type;
187 unsigned char pixel_depth, image_desc;
189 if (!bits_per_pixel || !width || !height)
194 if ( bits_per_pixel < 256 )
195 pixel_depth = (unsigned char)bits_per_pixel;
197 fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
202 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; // id_length
203 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; // colour_map_type
205 image_type = 2; // Uncompressed.
206 if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
209 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; // colour_map_index
210 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; // colour_map_length
211 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; // colour_map_entry_size
213 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; // x_origin
214 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; // y_origin
216 image_w = (unsigned short)width;
217 image_h = (unsigned short) height;
219 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
220 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
222 if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
224 image_desc = 8; // 8 bits per component.
228 if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails;
233 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
237 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
240 unsigned int image_width, image_height, pixel_bit_depth;
243 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
245 OPJ_COLOR_SPACE color_space;
248 int subsampling_dx, subsampling_dy;
251 f = fopen(filename, "rb");
253 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
257 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
260 // We currently only support 24 & 32 bit tga's ...
261 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
264 /* initialize image components */
265 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
267 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha.
268 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
271 color_space = CLRSPC_GRAY;
272 numcomps = save_alpha ? 2 : 1;
275 numcomps = save_alpha ? 4 : 3;
276 color_space = CLRSPC_SRGB;
279 subsampling_dx = parameters->subsampling_dx;
280 subsampling_dy = parameters->subsampling_dy;
282 for (i = 0; i < numcomps; i++) {
283 cmptparm[i].prec = 8;
285 cmptparm[i].sgnd = 0;
286 cmptparm[i].dx = subsampling_dx;
287 cmptparm[i].dy = subsampling_dy;
288 cmptparm[i].w = image_width;
289 cmptparm[i].h = image_height;
292 /* create the image */
293 image = opj_image_create(numcomps, &cmptparm[0], color_space);
298 /* set image offset and reference grid */
299 image->x0 = parameters->image_offset_x0;
300 image->y0 = parameters->image_offset_y0;
301 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
302 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
305 for (y=0; y < image_height; y++)
310 index = (image_height-y-1)*image_width;
312 index = y*image_width;
316 for (x=0;x<image_width;x++)
320 if( !fread(&b, 1, 1, f) )
322 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
323 opj_image_destroy(image);
326 if ( !fread(&g, 1, 1, f) )
328 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
329 opj_image_destroy(image);
332 if ( !fread(&r, 1, 1, f) )
334 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
335 opj_image_destroy(image);
339 image->comps[0].data[index]=r;
340 image->comps[1].data[index]=g;
341 image->comps[2].data[index]=b;
345 else if (numcomps==4)
347 for (x=0;x<image_width;x++)
349 unsigned char r,g,b,a;
350 if ( !fread(&b, 1, 1, f) )
352 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
353 opj_image_destroy(image);
356 if ( !fread(&g, 1, 1, f) )
358 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
359 opj_image_destroy(image);
362 if ( !fread(&r, 1, 1, f) )
364 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
365 opj_image_destroy(image);
368 if ( !fread(&a, 1, 1, f) )
370 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
371 opj_image_destroy(image);
375 image->comps[0].data[index]=r;
376 image->comps[1].data[index]=g;
377 image->comps[2].data[index]=b;
378 image->comps[3].data[index]=a;
383 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
389 int imagetotga(opj_image_t * image, const char *outfile) {
390 int width, height, bpp, x, y;
391 opj_bool write_alpha;
392 int i, adjustR, adjustG, adjustB;
393 unsigned int alpha_channel;
399 fdest = fopen(outfile, "wb");
401 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
405 for (i = 0; i < image->numcomps-1; i++) {
406 if ((image->comps[0].dx != image->comps[i+1].dx)
407 ||(image->comps[0].dy != image->comps[i+1].dy)
408 ||(image->comps[0].prec != image->comps[i+1].prec)) {
409 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
414 width = image->comps[0].w;
415 height = image->comps[0].h;
417 // Mono with alpha, or RGB with alpha.
418 write_alpha = (image->numcomps==2) || (image->numcomps==4);
421 bpp = write_alpha ? 32 : 24;
422 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
425 alpha_channel = image->numcomps-1;
427 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
429 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
430 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
431 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
433 for (y=0; y < height; y++) {
434 unsigned int index=y*width;
436 for (x=0; x < width; x++, index++) {
437 r = (float)(image->comps[0].data[index] + adjustR);
439 if (image->numcomps>2) {
440 g = (float)(image->comps[1].data[index] + adjustG);
441 b = (float)(image->comps[2].data[index] + adjustB);
443 else {// Greyscale ...
448 // TGA format writes BGR ...
449 value = (unsigned char)(b*scale);
450 fwrite(&value,1,1,fdest);
452 value = (unsigned char)(g*scale);
453 fwrite(&value,1,1,fdest);
455 value = (unsigned char)(r*scale);
456 fwrite(&value,1,1,fdest);
459 a = (float)(image->comps[alpha_channel].data[index]);
460 value = (unsigned char)(a*scale);
461 fwrite(&value,1,1,fdest);
469 /* -->> -->> -->> -->>
473 <<-- <<-- <<-- <<-- */
475 /* WORD defines a two byte word */
476 typedef unsigned short int WORD;
478 /* DWORD defines a four byte word */
479 typedef unsigned long int DWORD;
482 WORD bfType; /* 'BM' for Bitmap (19776) */
483 DWORD bfSize; /* Size of the file */
484 WORD bfReserved1; /* Reserved : 0 */
485 WORD bfReserved2; /* Reserved : 0 */
486 DWORD bfOffBits; /* Offset */
487 } BITMAPFILEHEADER_t;
490 DWORD biSize; /* Size of the structure in bytes */
491 DWORD biWidth; /* Width of the image in pixels */
492 DWORD biHeight; /* Heigth of the image in pixels */
493 WORD biPlanes; /* 1 */
494 WORD biBitCount; /* Number of color bits by pixels */
495 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
496 DWORD biSizeImage; /* Size of the image in bytes */
497 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
498 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
499 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
500 DWORD biClrImportant; /* Number of important color (0: ALL) */
501 } BITMAPINFOHEADER_t;
503 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
505 int subsampling_dx = parameters->subsampling_dx;
506 int subsampling_dy = parameters->subsampling_dy;
508 int i, numcomps, w, h;
509 OPJ_COLOR_SPACE color_space;
510 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
511 opj_image_t * image = NULL;
514 BITMAPFILEHEADER_t File_h;
515 BITMAPINFOHEADER_t Info_h;
517 unsigned char *table_R, *table_G, *table_B;
518 unsigned int j, PAD = 0;
525 IN = fopen(filename, "rb");
528 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
532 File_h.bfType = getc(IN);
533 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
535 if (File_h.bfType != 19778)
537 fprintf(stderr,"Error, not a BMP file!\n");
543 File_h.bfSize = getc(IN);
544 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
545 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
546 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
548 File_h.bfReserved1 = getc(IN);
549 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
551 File_h.bfReserved2 = getc(IN);
552 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
554 File_h.bfOffBits = getc(IN);
555 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
556 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
557 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
562 Info_h.biSize = getc(IN);
563 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
564 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
565 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
567 if(Info_h.biSize != 40)
569 fprintf(stderr,"Error, unknown BMP header size %ld\n", Info_h.biSize);
573 Info_h.biWidth = getc(IN);
574 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
575 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
576 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
579 Info_h.biHeight = getc(IN);
580 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
581 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
582 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
585 Info_h.biPlanes = getc(IN);
586 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
588 Info_h.biBitCount = getc(IN);
589 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
591 Info_h.biCompression = getc(IN);
592 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
593 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
594 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
596 Info_h.biSizeImage = getc(IN);
597 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
598 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
599 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
601 Info_h.biXpelsPerMeter = getc(IN);
602 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
603 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
604 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
606 Info_h.biYpelsPerMeter = getc(IN);
607 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
608 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
609 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
611 Info_h.biClrUsed = getc(IN);
612 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
613 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
614 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
616 Info_h.biClrImportant = getc(IN);
617 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
618 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
619 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
621 /* Read the data and store them in the OUT file */
623 if (Info_h.biBitCount == 24)
626 color_space = CLRSPC_SRGB;
627 /* initialize image components */
628 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
629 for(i = 0; i < numcomps; i++)
631 cmptparm[i].prec = 8;
633 cmptparm[i].sgnd = 0;
634 cmptparm[i].dx = subsampling_dx;
635 cmptparm[i].dy = subsampling_dy;
639 /* create the image */
640 image = opj_image_create(numcomps, &cmptparm[0], color_space);
647 /* set image offset and reference grid */
648 image->x0 = parameters->image_offset_x0;
649 image->y0 = parameters->image_offset_y0;
650 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
651 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
655 /* Place the cursor at the beginning of the image information */
656 fseek(IN, 0, SEEK_SET);
657 fseek(IN, File_h.bfOffBits, SEEK_SET);
662 /* PAD = 4 - (3 * W) % 4; */
663 /* PAD = (PAD == 4) ? 0 : PAD; */
664 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
666 RGB = (unsigned char *)
667 malloc((3 * W + PAD) * H * sizeof(unsigned char));
669 if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
672 opj_image_destroy(image);
673 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
679 for(y = 0; y < (int)H; y++)
681 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
682 for(x = 0; x < (int)W; x++)
684 unsigned char *pixel = &scanline[3 * x];
685 image->comps[0].data[index] = pixel[2]; /* R */
686 image->comps[1].data[index] = pixel[1]; /* G */
687 image->comps[2].data[index] = pixel[0]; /* B */
692 }/* if (Info_h.biBitCount == 24) */
694 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)//RGB
696 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
698 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
700 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
701 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
702 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
705 for (j = 0; j < Info_h.biClrUsed; j++)
707 table_B[j] = (unsigned char)getc(IN);
708 table_G[j] = (unsigned char)getc(IN);
709 table_R[j] = (unsigned char)getc(IN);
712 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
714 if(has_color) gray_scale = 0;
716 /* Place the cursor at the beginning of the image information */
717 fseek(IN, 0, SEEK_SET);
718 fseek(IN, File_h.bfOffBits, SEEK_SET);
722 if (Info_h.biWidth % 2)
725 numcomps = gray_scale ? 1 : 3;
726 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
727 /* initialize image components */
728 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
729 for(i = 0; i < numcomps; i++)
731 cmptparm[i].prec = 8;
733 cmptparm[i].sgnd = 0;
734 cmptparm[i].dx = subsampling_dx;
735 cmptparm[i].dy = subsampling_dy;
739 /* create the image */
740 image = opj_image_create(numcomps, &cmptparm[0], color_space);
744 free(table_R); free(table_G); free(table_B);
748 /* set image offset and reference grid */
749 image->x0 = parameters->image_offset_x0;
750 image->y0 = parameters->image_offset_y0;
751 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
752 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
756 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
758 if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
764 opj_image_destroy(image);
765 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
771 for (j = 0; j < W * H; j++)
773 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
775 image->comps[0].data[index] =
776 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
785 for (j = 0; j < W * H; j++)
787 if ((j % W < W - 1 && Info_h.biWidth % 2)
788 || !(Info_h.biWidth % 2))
790 unsigned char pixel_index =
791 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
792 image->comps[0].data[index] = table_R[pixel_index];
793 image->comps[1].data[index] = table_G[pixel_index];
794 image->comps[2].data[index] = table_B[pixel_index];
805 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)//RLE8
807 unsigned char *pix, *beyond;
808 int *gray, *red, *green, *blue;
809 unsigned int x, y, max;
813 if (Info_h.biClrUsed == 0)
814 Info_h.biClrUsed = 256;
815 else if (Info_h.biClrUsed > 256)
816 Info_h.biClrUsed = 256;
818 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
819 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
820 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
823 for (j = 0; j < Info_h.biClrUsed; j++)
825 table_B[j] = (unsigned char)getc(IN);
826 table_G[j] = (unsigned char)getc(IN);
827 table_R[j] = (unsigned char)getc(IN);
829 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
835 numcomps = gray_scale ? 1 : 3;
836 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
837 /* initialize image components */
838 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
839 for (i = 0; i < numcomps; i++)
841 cmptparm[i].prec = 8;
843 cmptparm[i].sgnd = 0;
844 cmptparm[i].dx = subsampling_dx;
845 cmptparm[i].dy = subsampling_dy;
849 /* create the image */
850 image = opj_image_create(numcomps, &cmptparm[0], color_space);
860 /* set image offset and reference grid */
861 image->x0 = parameters->image_offset_x0;
862 image->y0 = parameters->image_offset_y0;
863 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
864 - 1) * subsampling_dx + 1;
865 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
866 - 1) * subsampling_dy + 1;
870 /* Place the cursor at the beginning of the image information */
871 fseek(IN, 0, SEEK_SET);
872 fseek(IN, File_h.bfOffBits, SEEK_SET);
876 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
877 beyond = RGB + W * H;
889 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
890 *pix = (unsigned char)c1;
896 if (c == 0x00) /* EOL */
900 pix = RGB + x + (H - y - 1) * W;
902 else if (c == 0x01) /* EOP */
904 else if (c == 0x02) /* MOVE by dxdy */
910 pix = RGB + (H - y - 1) * W + x;
915 for (; i < c && x < W && pix < beyond; i++, x++, pix++)
918 *pix = (unsigned char)c1;
920 if (c & 1) /* skip padding byte */
928 gray = image->comps[0].data;
936 *gray++ = table_R[uc];
941 //int *red, *green, *blue;
943 red = image->comps[0].data;
944 green = image->comps[1].data;
945 blue = image->comps[2].data;
953 *red++ = table_R[uc];
954 *green++ = table_G[uc];
955 *blue++ = table_B[uc];
966 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
967 "is not yet implemented [%d]\n", Info_h.biBitCount);
973 int imagetobmp(opj_image_t * image, const char *outfile) {
977 int adjustR, adjustG, adjustB;
979 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
980 && image->comps[1].dx == image->comps[2].dx
981 && image->comps[0].dy == image->comps[1].dy
982 && image->comps[1].dy == image->comps[2].dy
983 && image->comps[0].prec == image->comps[1].prec
984 && image->comps[1].prec == image->comps[2].prec) {
986 /* -->> -->> -->> -->>
988 <<-- <<-- <<-- <<-- */
990 fdest = fopen(outfile, "wb");
992 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
996 w = image->comps[0].w;
997 h = image->comps[0].h;
999 fprintf(fdest, "BM");
1003 fprintf(fdest, "%c%c%c%c",
1004 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1005 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1006 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1007 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1008 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1009 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
1013 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1014 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1015 (unsigned char) ((w) >> 8) & 0xff,
1016 (unsigned char) ((w) >> 16) & 0xff,
1017 (unsigned char) ((w) >> 24) & 0xff);
1018 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1019 (unsigned char) ((h) >> 8) & 0xff,
1020 (unsigned char) ((h) >> 16) & 0xff,
1021 (unsigned char) ((h) >> 24) & 0xff);
1022 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1023 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1024 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1025 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
1026 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1027 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1028 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1029 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1030 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1031 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1032 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1034 if (image->comps[0].prec > 8) {
1035 adjustR = image->comps[0].prec - 8;
1036 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1040 if (image->comps[1].prec > 8) {
1041 adjustG = image->comps[1].prec - 8;
1042 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1046 if (image->comps[2].prec > 8) {
1047 adjustB = image->comps[2].prec - 8;
1048 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1053 for (i = 0; i < w * h; i++) {
1054 unsigned char rc, gc, bc;
1057 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1058 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1059 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1060 if(r > 255) r = 255; else if(r < 0) r = 0;
1061 rc = (unsigned char)r;
1063 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1064 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1065 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
1066 if(g > 255) g = 255; else if(g < 0) g = 0;
1067 gc = (unsigned char)g;
1069 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1070 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1071 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
1072 if(b > 255) b = 255; else if(b < 0) b = 0;
1073 bc = (unsigned char)b;
1075 fprintf(fdest, "%c%c%c", bc, gc, rc);
1077 if ((i + 1) % w == 0) {
1078 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
1079 fprintf(fdest, "%c", 0);
1083 } else { /* Gray-scale */
1085 /* -->> -->> -->> -->>
1086 8 bits non code (Gray scale)
1087 <<-- <<-- <<-- <<-- */
1089 fdest = fopen(outfile, "wb");
1090 w = image->comps[0].w;
1091 h = image->comps[0].h;
1093 fprintf(fdest, "BM");
1097 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1098 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1099 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1100 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1101 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1102 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1103 ((54 + 1024) >> 16) & 0xff,
1104 ((54 + 1024) >> 24) & 0xff);
1108 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1109 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1110 (unsigned char) ((w) >> 8) & 0xff,
1111 (unsigned char) ((w) >> 16) & 0xff,
1112 (unsigned char) ((w) >> 24) & 0xff);
1113 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1114 (unsigned char) ((h) >> 8) & 0xff,
1115 (unsigned char) ((h) >> 16) & 0xff,
1116 (unsigned char) ((h) >> 24) & 0xff);
1117 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1118 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1119 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1120 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
1121 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
1122 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
1123 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
1124 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1125 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1126 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1127 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1129 if (image->comps[0].prec > 8) {
1130 adjustR = image->comps[0].prec - 8;
1131 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1135 for (i = 0; i < 256; i++) {
1136 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1139 for (i = 0; i < w * h; i++) {
1142 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1143 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1144 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1145 if(r > 255) r = 255; else if(r < 0) r = 0;
1147 fprintf(fdest, "%c", (unsigned char)r);
1149 if ((i + 1) % w == 0) {
1150 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1151 fprintf(fdest, "%c", 0);
1160 /* -->> -->> -->> -->>
1164 <<-- <<-- <<-- <<-- */
1167 unsigned char readuchar(FILE * f)
1170 if ( !fread(&c1, 1, 1, f) )
1172 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1178 unsigned short readushort(FILE * f, int bigendian)
1180 unsigned char c1, c2;
1181 if ( !fread(&c1, 1, 1, f) )
1183 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1186 if ( !fread(&c2, 1, 1, f) )
1188 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1192 return (c1 << 8) + c2;
1194 return (c2 << 8) + c1;
1197 unsigned int readuint(FILE * f, int bigendian)
1199 unsigned char c1, c2, c3, c4;
1200 if ( !fread(&c1, 1, 1, f) )
1202 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1205 if ( !fread(&c2, 1, 1, f) )
1207 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1210 if ( !fread(&c3, 1, 1, f) )
1212 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1215 if ( !fread(&c4, 1, 1, f) )
1217 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1221 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1223 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1226 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1229 int i, numcomps, max;
1230 OPJ_COLOR_SPACE color_space;
1231 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1232 opj_image_t * image = NULL;
1234 char endian1,endian2,sign;
1239 opj_image_comp_t *comp = NULL;
1242 color_space = CLRSPC_GRAY;
1244 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1248 f = fopen(filename, "rb");
1250 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1254 fseek(f, 0, SEEK_SET);
1255 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1256 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1262 while (signtmp[i]!='\0') {
1263 if (signtmp[i]=='-') sign='-';
1268 if (endian1=='M' && endian2=='L') {
1270 } else if (endian2=='M' && endian1=='L') {
1273 fprintf(stderr, "Bad pgx header, please check input file\n");
1277 /* initialize image component */
1279 cmptparm.x0 = parameters->image_offset_x0;
1280 cmptparm.y0 = parameters->image_offset_y0;
1281 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1282 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1289 cmptparm.prec = prec;
1290 cmptparm.bpp = prec;
1291 cmptparm.dx = parameters->subsampling_dx;
1292 cmptparm.dy = parameters->subsampling_dy;
1294 /* create the image */
1295 image = opj_image_create(numcomps, &cmptparm, color_space);
1300 /* set image offset and reference grid */
1301 image->x0 = cmptparm.x0;
1302 image->y0 = cmptparm.x0;
1303 image->x1 = cmptparm.w;
1304 image->y1 = cmptparm.h;
1306 /* set image data */
1308 comp = &image->comps[0];
1310 for (i = 0; i < w * h; i++) {
1312 if (comp->prec <= 8) {
1316 v = (char) readuchar(f);
1318 } else if (comp->prec <= 16) {
1320 v = readushort(f, bigendian);
1322 v = (short) readushort(f, bigendian);
1326 v = readuint(f, bigendian);
1328 v = (int) readuint(f, bigendian);
1336 comp->bpp = int_floorlog2(max) + 1;
1341 int imagetopgx(opj_image_t * image, const char *outfile) {
1346 for (compno = 0; compno < image->numcomps; compno++) {
1347 opj_image_comp_t *comp = &image->comps[compno];
1348 char bname[256]; /* buffer for name */
1349 char *name = bname; /* pointer */
1351 const size_t olen = strlen(outfile);
1352 const size_t dotpos = olen - 4;
1353 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1354 if( outfile[dotpos] != '.' ) {
1355 /* `pgx` was recognized but there is no dot at expected position */
1356 fprintf(stderr, "ERROR -> Impossible happen." );
1360 name = (char*)malloc(total+1);
1362 strncpy(name, outfile, dotpos);
1363 //if (image->numcomps > 1) {
1364 sprintf(name+dotpos, "_%d.pgx", compno);
1366 strcpy(name+dotpos, ".pgx");
1368 fdest = fopen(name, "wb");
1370 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1373 /* dont need name anymore */
1378 w = image->comps[compno].w;
1379 h = image->comps[compno].h;
1381 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1382 if (comp->prec <= 8) {
1384 } else if (comp->prec <= 16) {
1389 for (i = 0; i < w * h; i++) {
1390 int v = image->comps[compno].data[i];
1391 for (j = nbytes - 1; j >= 0; j--) {
1392 char byte = (char) (v >> (j * 8));
1393 fwrite(&byte, 1, 1, fdest);
1402 /* -->> -->> -->> -->>
1406 <<-- <<-- <<-- <<-- */
1410 int width, height, maxval, depth, format;
1411 char rgb, rgba, gray, graya, bw;
1415 static char *skip_white(char *s)
1419 if(*s == '\n' || *s == '\r') return NULL;
1420 if(isspace(*s)) { ++s; continue; }
1426 static char *skip_int(char *start, int *out_n)
1431 *out_n = 0; s = start;
1433 s = skip_white(start);
1434 if(s == NULL) return NULL;
1439 if( !isdigit(*s)) break;
1442 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1446 static char *skip_idf(char *start, char out_idf[256])
1451 s = skip_white(start);
1452 if(s == NULL) return NULL;
1457 if(isalpha(*s) || *s == '_') { ++s; continue; }
1460 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1464 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1467 int format, have_wh, end, ttype;
1468 char idf[256], type[256];
1471 if (fgets(line, 250, reader) == NULL)
1473 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1479 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1481 format = atoi(line + 1);
1482 if(format < 1 || format > 7)
1484 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1487 ph->format = format;
1488 ttype = end = have_wh = 0;
1490 while(fgets(line, 250, reader))
1492 if(*line == '#') continue;
1498 s = skip_idf(s, idf);
1500 if(s == NULL || *s == 0) return;
1502 if(strcmp(idf, "ENDHDR") == 0)
1506 if(strcmp(idf, "WIDTH") == 0)
1508 s = skip_int(s, &ph->width);
1509 if(s == NULL || *s == 0) return;
1513 if(strcmp(idf, "HEIGHT") == 0)
1515 s = skip_int(s, &ph->height);
1516 if(s == NULL || *s == 0) return;
1520 if(strcmp(idf, "DEPTH") == 0)
1522 s = skip_int(s, &ph->depth);
1523 if(s == NULL || *s == 0) return;
1527 if(strcmp(idf, "MAXVAL") == 0)
1529 s = skip_int(s, &ph->maxval);
1530 if(s == NULL || *s == 0) return;
1534 if(strcmp(idf, "TUPLTYPE") == 0)
1536 s = skip_idf(s, type);
1537 if(s == NULL || *s == 0) return;
1539 if(strcmp(type, "BLACKANDWHITE") == 0)
1541 ph->bw = 1; ttype = 1; continue;
1543 if(strcmp(type, "GRAYSCALE") == 0)
1545 ph->gray = 1; ttype = 1; continue;
1547 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1549 ph->graya = 1; ttype = 1; continue;
1551 if(strcmp(type, "RGB") == 0)
1553 ph->rgb = 1; ttype = 1; continue;
1555 if(strcmp(type, "RGB_ALPHA") == 0)
1557 ph->rgba = 1; ttype = 1; continue;
1559 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1562 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1564 } /* if(format == 7) */
1568 s = skip_int(s, &ph->width);
1570 s = skip_int(s, &ph->height);
1574 if(format == 1 || format == 4) break;
1578 if(format == 2 || format == 3 || format == 5 || format == 6)
1580 /* P2, P3, P5, P6: */
1581 s = skip_int(s, &ph->maxval);
1583 if(ph->maxval > 65535) return;
1586 }/* while(fgets( ) */
1587 if(format == 2 || format == 3 || format > 4)
1589 if(ph->maxval < 1 || ph->maxval > 65535) return;
1591 if(ph->width < 1 || ph->height < 1) return;
1597 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1599 if(ph->depth < 1 || ph->depth > 4) return;
1601 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1606 if(format != 1 && format != 4)
1608 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1612 if(ph->width && ph->height) ph->ok = 1;
1618 static int has_prec(int val)
1620 if(val < 2) return 1;
1621 if(val < 4) return 2;
1622 if(val < 8) return 3;
1623 if(val < 16) return 4;
1624 if(val < 32) return 5;
1625 if(val < 64) return 6;
1626 if(val < 128) return 7;
1627 if(val < 256) return 8;
1628 if(val < 512) return 9;
1629 if(val < 1024) return 10;
1630 if(val < 2048) return 11;
1631 if(val < 4096) return 12;
1632 if(val < 8192) return 13;
1633 if(val < 16384) return 14;
1634 if(val < 32768) return 15;
1638 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1639 int subsampling_dx = parameters->subsampling_dx;
1640 int subsampling_dy = parameters->subsampling_dy;
1643 int i, compno, numcomps, w, h, prec, format;
1644 OPJ_COLOR_SPACE color_space;
1645 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1646 opj_image_t * image = NULL;
1647 struct pnm_header header_info;
1649 if((fp = fopen(filename, "rb")) == NULL)
1651 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1654 memset(&header_info, 0, sizeof(struct pnm_header));
1656 read_pnm_header(fp, &header_info);
1658 if(!header_info.ok) { fclose(fp); return NULL; }
1660 format = header_info.format;
1664 case 1: /* ascii bitmap */
1665 case 4: /* raw bitmap */
1669 case 2: /* ascii greymap */
1670 case 5: /* raw greymap */
1674 case 3: /* ascii pixmap */
1675 case 6: /* raw pixmap */
1679 case 7: /* arbitrary map */
1680 numcomps = header_info.depth;
1683 default: fclose(fp); return NULL;
1686 color_space = CLRSPC_GRAY;/* GRAY, GRAYA */
1688 color_space = CLRSPC_SRGB;/* RGB, RGBA */
1690 prec = has_prec(header_info.maxval);
1692 if(prec < 8) prec = 8;
1694 w = header_info.width;
1695 h = header_info.height;
1696 subsampling_dx = parameters->subsampling_dx;
1697 subsampling_dy = parameters->subsampling_dy;
1699 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1701 for(i = 0; i < numcomps; i++)
1703 cmptparm[i].prec = prec;
1704 cmptparm[i].bpp = prec;
1705 cmptparm[i].sgnd = 0;
1706 cmptparm[i].dx = subsampling_dx;
1707 cmptparm[i].dy = subsampling_dy;
1711 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1713 if(!image) { fclose(fp); return NULL; }
1715 /* set image offset and reference grid */
1716 image->x0 = parameters->image_offset_x0;
1717 image->y0 = parameters->image_offset_y0;
1718 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1719 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1721 if((format == 2) || (format == 3)) /* ascii pixmap */
1725 for (i = 0; i < w * h; i++)
1727 for(compno = 0; compno < numcomps; compno++)
1730 if (fscanf(fp, "%u", &index) != 1)
1731 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1733 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1741 && ( header_info.gray || header_info.graya
1742 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1744 unsigned char c0, c1, one;
1748 for (i = 0; i < w * h; i++)
1750 for(compno = 0; compno < numcomps; compno++)
1752 if ( !fread(&c0, 1, 1, fp) )
1753 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1756 image->comps[compno].data[i] = c0;
1760 if ( !fread(&c1, 1, 1, fp) )
1761 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1763 image->comps[compno].data[i] = ((c0<<8) | c1);
1769 if(format == 1) /* ascii bitmap */
1771 for (i = 0; i < w * h; i++)
1775 if ( fscanf(fp, "%u", &index) != 1)
1776 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1778 image->comps[0].data[i] = (index?0:255);
1788 for(y = 0; y < h; ++y)
1792 for(x = 0; x < w; ++x)
1797 uc = (unsigned char)getc(fp);
1799 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1805 if((format == 7 && header_info.bw)) //MONO
1809 for(i = 0; i < w * h; ++i)
1811 if ( !fread(&uc, 1, 1, fp) )
1812 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1813 image->comps[0].data[i] = (uc & 1)?0:255;
1821 int imagetopnm(opj_image_t * image, const char *outfile)
1823 int *red, *green, *blue, *alpha;
1825 int i, compno, ncomp;
1826 int adjustR, adjustG, adjustB, adjustA;
1827 int fails, two, want_gray, has_alpha, triple;
1830 const char *tmp = outfile;
1833 if((prec = image->comps[0].prec) > 16)
1835 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1836 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1839 two = has_alpha = 0; fails = 1;
1840 ncomp = image->numcomps;
1842 while (*tmp) ++tmp; tmp -= 2;
1843 want_gray = (*tmp == 'g' || *tmp == 'G');
1844 ncomp = image->numcomps;
1846 if(want_gray) ncomp = 1;
1848 if (ncomp == 2 /* GRAYA */
1849 || (ncomp > 2 /* RGB, RGBA */
1850 && image->comps[0].dx == image->comps[1].dx
1851 && image->comps[1].dx == image->comps[2].dx
1852 && image->comps[0].dy == image->comps[1].dy
1853 && image->comps[1].dy == image->comps[2].dy
1854 && image->comps[0].prec == image->comps[1].prec
1855 && image->comps[1].prec == image->comps[2].prec
1858 fdest = fopen(outfile, "wb");
1862 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1866 triple = (ncomp > 2);
1867 wr = image->comps[0].w; hr = image->comps[0].h;
1868 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1870 red = image->comps[0].data;
1874 green = image->comps[1].data;
1875 blue = image->comps[2].data;
1877 else green = blue = NULL;
1881 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1883 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1884 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1885 wr, hr, ncomp, max, tt);
1886 alpha = image->comps[ncomp - 1].data;
1887 adjustA = (image->comps[ncomp - 1].sgnd ?
1888 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1892 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1893 opj_version(), wr, hr, max);
1894 alpha = NULL; adjustA = 0;
1896 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1900 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1901 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1903 else adjustG = adjustB = 0;
1905 for(i = 0; i < wr * hr; ++i)
1909 v = *red + adjustR; ++red;
1911 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1915 v = *green + adjustG; ++green;
1917 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1919 v = *blue + adjustB; ++blue;
1921 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1927 v = *alpha + adjustA; ++alpha;
1929 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1937 fprintf(fdest, "%c", (unsigned char)*red++);
1939 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
1942 fprintf(fdest, "%c", (unsigned char)*alpha++);
1946 fclose(fdest); return 0;
1951 if (image->numcomps > ncomp)
1953 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1954 fprintf(stderr," is written to the file\n");
1956 destname = (char*)malloc(strlen(outfile) + 8);
1958 for (compno = 0; compno < ncomp; compno++)
1961 sprintf(destname, "%d.%s", compno, outfile);
1963 sprintf(destname, "%s", outfile);
1965 fdest = fopen(destname, "wb");
1968 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1972 wr = image->comps[compno].w; hr = image->comps[compno].h;
1973 prec = image->comps[compno].prec;
1974 max = (1<<prec) - 1;
1976 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
1977 opj_version(), wr, hr, max);
1979 red = image->comps[compno].data;
1981 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1985 for (i = 0; i < wr * hr; i++)
1987 v = *red + adjustR; ++red;
1989 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1995 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1999 else /* prec <= 8 */
2001 for(i = 0; i < wr * hr; ++i)
2003 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
2014 /* -->> -->> -->> -->>
2018 <<-- <<-- <<-- <<-- */
2020 typedef struct tiff_infoheader{
2021 DWORD tiWidth; // Width of Image in pixel
2022 DWORD tiHeight; // Height of Image in pixel
2023 DWORD tiPhoto; // Photometric
2024 WORD tiBps; // Bits per sample
2025 WORD tiSf; // Sample Format
2026 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
2027 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
2030 int imagetotif(opj_image_t * image, const char *outfile)
2032 int width, height, imgsize;
2033 int bps,index,adjust, sgnd;
2034 int ushift, dshift, has_alpha, force16;
2040 ushift = dshift = force16 = has_alpha = 0;
2041 bps = image->comps[0].prec;
2043 if(bps > 8 && bps < 16)
2045 ushift = 16 - bps; dshift = bps - ushift;
2046 bps = 16; force16 = 1;
2049 if(bps != 8 && bps != 16)
2051 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2053 fprintf(stderr,"\tAborting\n");
2056 tif = TIFFOpen(outfile, "wb");
2060 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2063 sgnd = image->comps[0].sgnd;
2064 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2066 if(image->numcomps >= 3
2067 && image->comps[0].dx == image->comps[1].dx
2068 && image->comps[1].dx == image->comps[2].dx
2069 && image->comps[0].dy == image->comps[1].dy
2070 && image->comps[1].dy == image->comps[2].dy
2071 && image->comps[0].prec == image->comps[1].prec
2072 && image->comps[1].prec == image->comps[2].prec)
2074 has_alpha = (image->numcomps == 4);
2076 width = image->comps[0].w;
2077 height = image->comps[0].h;
2078 imgsize = width * height ;
2080 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2081 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2082 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2083 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2084 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2085 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2086 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2087 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2088 strip_size = TIFFStripSize(tif);
2089 buf = _TIFFmalloc(strip_size);
2092 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2094 unsigned char *dat8;
2095 tsize_t i, ssize, last_i = 0;
2097 ssize = TIFFStripSize(tif);
2098 dat8 = (unsigned char*)buf;
2102 step = 3 + has_alpha;
2105 for(i=0; i < ssize - restx; i += step)
2111 r = image->comps[0].data[index];
2112 g = image->comps[1].data[index];
2113 b = image->comps[2].data[index];
2114 if(has_alpha) a = image->comps[3].data[index];
2121 if(has_alpha) a += adjust;
2126 if(has_alpha) dat8[i+3] = a;
2137 for(i = last_i; i < ssize; i += step)
2143 r = image->comps[0].data[index];
2144 g = image->comps[1].data[index];
2145 b = image->comps[2].data[index];
2146 if(has_alpha) a = image->comps[3].data[index];
2153 if(has_alpha) a += adjust;
2156 if(i+1 < ssize) dat8[i+1] = g ; else break;
2157 if(i+2 < ssize) dat8[i+2] = b ; else break;
2160 if(i+3 < ssize) dat8[i+3] = a ; else break;
2167 }//if(last_i < ssize)
2173 step = 6 + has_alpha + has_alpha;
2176 for(i = 0; i < ssize - restx ; i += step)
2182 r = image->comps[0].data[index];
2183 g = image->comps[1].data[index];
2184 b = image->comps[2].data[index];
2185 if(has_alpha) a = image->comps[3].data[index];
2192 if(has_alpha) a += adjust;
2196 r = (r<<ushift) + (r>>dshift);
2197 g = (g<<ushift) + (g>>dshift);
2198 b = (b<<ushift) + (b>>dshift);
2199 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2202 dat8[i+1] = (r >> 8);//MSB
2204 dat8[i+3] = (g >> 8);
2206 dat8[i+5] = (b >> 8);
2210 dat8[i+7] = (a >> 8);
2221 for(i = last_i ; i < ssize ; i += step)
2227 r = image->comps[0].data[index];
2228 g = image->comps[1].data[index];
2229 b = image->comps[2].data[index];
2230 if(has_alpha) a = image->comps[3].data[index];
2237 if(has_alpha) a += adjust;
2241 r = (r<<ushift) + (r>>dshift);
2242 g = (g<<ushift) + (g>>dshift);
2243 b = (b<<ushift) + (b>>dshift);
2244 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2247 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB
2248 if(i+2 < ssize) dat8[i+2] = g; else break;
2249 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2250 if(i+4 < ssize) dat8[i+4] = b; else break;
2251 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2255 if(i+6 < ssize) dat8[i+6] = a; else break;
2256 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2263 }//if(last_i < ssize)
2266 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2269 _TIFFfree((void*)buf);
2275 if(image->numcomps == 1 /* GRAY */
2276 || ( image->numcomps == 2 /* GRAY_ALPHA */
2277 && image->comps[0].dx == image->comps[1].dx
2278 && image->comps[0].dy == image->comps[1].dy
2279 && image->comps[0].prec == image->comps[1].prec))
2283 has_alpha = (image->numcomps == 2);
2285 width = image->comps[0].w;
2286 height = image->comps[0].h;
2287 imgsize = width * height;
2290 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2291 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2292 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2293 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2294 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2295 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2296 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2297 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2299 /* Get a buffer for the data */
2300 strip_size = TIFFStripSize(tif);
2301 buf = _TIFFmalloc(strip_size);
2304 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2306 unsigned char *dat8;
2307 tsize_t i, ssize = TIFFStripSize(tif);
2308 dat8 = (unsigned char*)buf;
2312 step = 1 + has_alpha;
2314 for(i=0; i < ssize; i += step)
2320 r = image->comps[0].data[index];
2321 if(has_alpha) a = image->comps[1].data[index];
2326 if(has_alpha) a += adjust;
2329 if(has_alpha) dat8[i+1] = a;
2339 step = 2 + has_alpha + has_alpha;
2341 for(i=0; i < ssize; i += step)
2347 r = image->comps[0].data[index];
2348 if(has_alpha) a = image->comps[1].data[index];
2353 if(has_alpha) a += adjust;
2357 r = (r<<ushift) + (r>>dshift);
2358 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2361 dat8[i+1] = r >> 8;//MSB
2368 }//if(index < imgsize)
2373 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2384 fprintf(stderr,"imagetotif: Bad color format.\n"
2385 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2386 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2393 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2394 * CINEMA : 12 bit precision
2396 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2398 int subsampling_dx = parameters->subsampling_dx;
2399 int subsampling_dy = parameters->subsampling_dy;
2401 tiff_infoheader_t Info;
2405 int j, numcomps, w, h,index;
2406 OPJ_COLOR_SPACE color_space;
2407 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2408 opj_image_t *image = NULL;
2412 tif = TIFFOpen(filename, "r");
2416 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2419 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
2420 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
2421 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
2422 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
2423 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
2425 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
2426 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
2433 if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
2434 if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
2439 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2440 " implemented\n",Info.tiBps);
2443 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2444 " and GRAY(A) has been implemented\n",(int) Info.tiPhoto);
2446 fprintf(stderr,"\tAborting\n");
2453 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2455 uint16 extrasamples;
2457 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2458 &extrasamples, &sampleinfo);
2460 if(extrasamples >= 1)
2462 switch(sampleinfo[0])
2464 case EXTRASAMPLE_UNSPECIFIED:
2465 /* Workaround for some images without correct info about alpha channel
2471 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2472 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2478 /* initialize image components
2480 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2482 if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2484 numcomps = 3 + has_alpha;
2485 color_space = CLRSPC_SRGB;
2487 for(j = 0; j < numcomps; j++)
2489 if(parameters->cp_cinema)
2491 cmptparm[j].prec = 12;
2492 cmptparm[j].bpp = 12;
2496 cmptparm[j].prec = Info.tiBps;
2497 cmptparm[j].bpp = Info.tiBps;
2499 cmptparm[j].dx = subsampling_dx;
2500 cmptparm[j].dy = subsampling_dy;
2505 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2512 /* set image offset and reference grid
2514 image->x0 = parameters->image_offset_x0;
2515 image->y0 = parameters->image_offset_y0;
2516 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2517 image->x0 + (w - 1) * subsampling_dx + 1;
2518 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2519 image->y0 + (h - 1) * subsampling_dy + 1;
2521 buf = _TIFFmalloc(TIFFStripSize(tif));
2523 strip_size=TIFFStripSize(tif);
2525 imgsize = image->comps[0].w * image->comps[0].h ;
2526 /* Read the Image components
2528 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2530 unsigned char *dat8;
2533 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2534 dat8 = (unsigned char*)buf;
2536 if(Info.tiBps == 16)
2538 step = 6 + has_alpha + has_alpha;
2540 for(i = 0; i < ssize; i += step)
2544 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
2545 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
2546 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
2548 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2550 if(parameters->cp_cinema)
2552 /* Rounding 16 to 12 bits
2554 image->comps[0].data[index] =
2555 (image->comps[0].data[index] + 0x08) >> 4 ;
2556 image->comps[1].data[index] =
2557 (image->comps[1].data[index] + 0x08) >> 4 ;
2558 image->comps[2].data[index] =
2559 (image->comps[2].data[index] + 0x08) >> 4 ;
2561 image->comps[3].data[index] =
2562 (image->comps[3].data[index] + 0x08) >> 4 ;
2569 }//if(Info.tiBps == 16)
2573 step = 3 + has_alpha;
2575 for(i = 0; i < ssize; i += step)
2579 image->comps[0].data[index] = dat8[i+0];// R
2580 image->comps[1].data[index] = dat8[i+1];// G
2581 image->comps[2].data[index] = dat8[i+2];// B
2583 image->comps[3].data[index] = dat8[i+3];
2585 if(parameters->cp_cinema)
2587 /* Rounding 8 to 12 bits
2589 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2590 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2591 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2593 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2600 }//if( Info.tiBps == 8)
2602 if(Info.tiBps == 12)/* CINEMA file */
2606 for(i = 0; i < ssize; i += step)
2608 if((index < imgsize)&(index+1 < imgsize))
2610 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2611 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2613 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2614 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2616 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2617 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2633 if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2635 numcomps = 1 + has_alpha;
2636 color_space = CLRSPC_GRAY;
2638 for(j = 0; j < numcomps; ++j)
2640 cmptparm[j].prec = Info.tiBps;
2641 cmptparm[j].bpp = Info.tiBps;
2642 cmptparm[j].dx = subsampling_dx;
2643 cmptparm[j].dy = subsampling_dy;
2647 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2654 /* set image offset and reference grid
2656 image->x0 = parameters->image_offset_x0;
2657 image->y0 = parameters->image_offset_y0;
2658 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2659 image->x0 + (w - 1) * subsampling_dx + 1;
2660 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2661 image->y0 + (h - 1) * subsampling_dy + 1;
2663 buf = _TIFFmalloc(TIFFStripSize(tif));
2665 strip_size = TIFFStripSize(tif);
2667 imgsize = image->comps[0].w * image->comps[0].h ;
2668 /* Read the Image components
2670 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2672 unsigned char *dat8;
2676 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2677 dat8 = (unsigned char*)buf;
2679 if(Info.tiBps == 16)
2681 step = 2 + has_alpha + has_alpha;
2683 for(i = 0; i < ssize; i += step)
2687 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2689 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2699 step = 1 + has_alpha;
2701 for(i = 0; i < ssize; i += step)
2705 image->comps[0].data[index] = dat8[i+0];
2707 image->comps[1].data[index] = dat8[i+1];
2725 #endif /* HAVE_LIBTIFF */
2727 /* -->> -->> -->> -->>
2731 <<-- <<-- <<-- <<-- */
2733 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2734 int subsampling_dx = parameters->subsampling_dx;
2735 int subsampling_dy = parameters->subsampling_dy;
2738 int i, compno, numcomps, w, h;
2739 OPJ_COLOR_SPACE color_space;
2740 opj_image_cmptparm_t *cmptparm;
2741 opj_image_t * image = NULL;
2744 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2746 fprintf(stderr,"\nError: invalid raw image parameters\n");
2747 fprintf(stderr,"Please use the Format option -F:\n");
2748 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2749 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2750 fprintf(stderr,"Aborting\n");
2754 f = fopen(filename, "rb");
2756 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2757 fprintf(stderr,"Aborting\n");
2760 numcomps = raw_cp->rawComp;
2761 color_space = CLRSPC_SRGB;
2762 w = raw_cp->rawWidth;
2763 h = raw_cp->rawHeight;
2764 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2766 /* initialize image components */
2767 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2768 for(i = 0; i < numcomps; i++) {
2769 cmptparm[i].prec = raw_cp->rawBitDepth;
2770 cmptparm[i].bpp = raw_cp->rawBitDepth;
2771 cmptparm[i].sgnd = raw_cp->rawSigned;
2772 cmptparm[i].dx = subsampling_dx;
2773 cmptparm[i].dy = subsampling_dy;
2777 /* create the image */
2778 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2783 /* set image offset and reference grid */
2784 image->x0 = parameters->image_offset_x0;
2785 image->y0 = parameters->image_offset_y0;
2786 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2787 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2789 if(raw_cp->rawBitDepth <= 8)
2791 unsigned char value = 0;
2792 for(compno = 0; compno < numcomps; compno++) {
2793 for (i = 0; i < w * h; i++) {
2794 if (!fread(&value, 1, 1, f)) {
2795 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2798 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2802 else if(raw_cp->rawBitDepth <= 16)
2804 unsigned short value;
2805 for(compno = 0; compno < numcomps; compno++) {
2806 for (i = 0; i < w * h; i++) {
2808 if (!fread(&temp, 1, 1, f)) {
2809 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2813 if (!fread(&temp, 1, 1, f)) {
2814 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2818 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2823 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2827 if (fread(&ch, 1, 1, f)) {
2828 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2835 int imagetoraw(opj_image_t * image, const char *outfile)
2837 FILE *rawFile = NULL;
2843 if((image->numcomps * image->x1 * image->y1) == 0)
2845 fprintf(stderr,"\nError: invalid raw image parameters\n");
2849 rawFile = fopen(outfile, "wb");
2851 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2855 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2857 for(compno = 0; compno < image->numcomps; compno++)
2859 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2860 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2862 w = image->comps[compno].w;
2863 h = image->comps[compno].h;
2865 if(image->comps[compno].prec <= 8)
2867 if(image->comps[compno].sgnd == 1)
2870 int mask = (1 << image->comps[compno].prec) - 1;
2871 ptr = image->comps[compno].data;
2872 for (line = 0; line < h; line++) {
2873 for(row = 0; row < w; row++) {
2874 curr = (signed char) (*ptr & mask);
2875 fwrite(&curr, sizeof(signed char), 1, rawFile);
2880 else if(image->comps[compno].sgnd == 0)
2883 int mask = (1 << image->comps[compno].prec) - 1;
2884 ptr = image->comps[compno].data;
2885 for (line = 0; line < h; line++) {
2886 for(row = 0; row < w; row++) {
2887 curr = (unsigned char) (*ptr & mask);
2888 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2894 else if(image->comps[compno].prec <= 16)
2896 if(image->comps[compno].sgnd == 1)
2898 signed short int curr;
2899 int mask = (1 << image->comps[compno].prec) - 1;
2900 ptr = image->comps[compno].data;
2901 for (line = 0; line < h; line++) {
2902 for(row = 0; row < w; row++) {
2904 curr = (signed short int) (*ptr & mask);
2905 temp = (unsigned char) (curr >> 8);
2906 fwrite(&temp, 1, 1, rawFile);
2907 temp = (unsigned char) curr;
2908 fwrite(&temp, 1, 1, rawFile);
2913 else if(image->comps[compno].sgnd == 0)
2915 unsigned short int curr;
2916 int mask = (1 << image->comps[compno].prec) - 1;
2917 ptr = image->comps[compno].data;
2918 for (line = 0; line < h; line++) {
2919 for(row = 0; row < w; row++) {
2921 curr = (unsigned short int) (*ptr & mask);
2922 temp = (unsigned char) (curr >> 8);
2923 fwrite(&temp, 1, 1, rawFile);
2924 temp = (unsigned char) curr;
2925 fwrite(&temp, 1, 1, rawFile);
2931 else if (image->comps[compno].prec <= 32)
2933 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2938 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2948 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
2949 #define MAGIC_SIZE 8
2950 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
2952 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
2956 double gamma, display_exponent;
2957 int bit_depth, interlace_type,compression_type, filter_type;
2959 png_uint_32 resx, resy;
2961 png_uint_32 width, height;
2962 int color_type, has_alpha, is16;
2965 unsigned char **rows;
2968 opj_image_cmptparm_t cmptparm[4];
2970 unsigned int nr_comp;
2972 unsigned char sigbuf[8];
2974 if((reader = fopen(read_idf, "rb")) == NULL)
2976 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
2979 image = NULL; png = NULL; rows = NULL;
2981 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
2982 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
2984 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
2987 /* libpng-VERSION/example.c:
2988 * PC : screen_gamma = 2.2;
2989 * Mac: screen_gamma = 1.7 or 1.0;
2991 display_exponent = 2.2;
2993 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
2994 NULL, NULL, NULL)) == NULL)
2996 if((info = png_create_info_struct(png)) == NULL)
2999 if(setjmp(png_jmpbuf(png)))
3002 png_init_io(png, reader);
3003 png_set_sig_bytes(png, MAGIC_SIZE);
3005 png_read_info(png, info);
3007 if(png_get_IHDR(png, info, &width, &height,
3008 &bit_depth, &color_type, &interlace_type,
3009 &compression_type, &filter_type) == 0)
3012 /* png_set_expand():
3013 * expand paletted images to RGB, expand grayscale images of
3014 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3015 * to alpha channels.
3017 if(color_type == PNG_COLOR_TYPE_PALETTE)
3018 png_set_expand(png);
3020 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3021 png_set_expand(png);
3023 if(png_get_valid(png, info, PNG_INFO_tRNS))
3024 png_set_expand(png);
3026 is16 = (bit_depth == 16);
3028 /* GRAY => RGB; GRAY_ALPHA => RGBA
3030 if(color_type == PNG_COLOR_TYPE_GRAY
3031 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3033 png_set_gray_to_rgb(png);
3035 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
3036 PNG_COLOR_TYPE_RGB_ALPHA);
3038 if( !png_get_gAMA(png, info, &gamma))
3041 png_set_gamma(png, display_exponent, gamma);
3043 png_read_update_info(png, info);
3045 png_get_pHYs(png, info, &resx, &resy, &unit);
3047 color_type = png_get_color_type(png, info);
3049 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3051 nr_comp = 3 + has_alpha;
3053 bit_depth = png_get_bit_depth(png, info);
3055 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
3056 for(i = 0; i < height; ++i)
3057 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
3059 png_read_image(png, rows);
3061 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3063 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3065 for(i = 0; i < nr_comp; ++i)
3067 cmptparm[i].prec = bit_depth;
3068 /* bits_per_pixel: 8 or 16 */
3069 cmptparm[i].bpp = bit_depth;
3070 cmptparm[i].sgnd = 0;
3071 cmptparm[i].dx = sub_dx;
3072 cmptparm[i].dy = sub_dy;
3073 cmptparm[i].w = width;
3074 cmptparm[i].h = height;
3077 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3079 if(image == NULL) goto fin;
3081 image->x0 = params->image_offset_x0;
3082 image->y0 = params->image_offset_y0;
3083 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3084 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3086 r = image->comps[0].data;
3087 g = image->comps[1].data;
3088 b = image->comps[2].data;
3089 a = image->comps[3].data;
3091 for(i = 0; i < height; ++i)
3095 for(j = 0; j < width; ++j)
3099 *r++ = s[0]<<8|s[1]; s += 2;
3101 *g++ = s[0]<<8|s[1]; s += 2;
3103 *b++ = s[0]<<8|s[1]; s += 2;
3105 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3109 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3111 if(has_alpha) *a++ = *s++;
3117 for(i = 0; i < height; ++i)
3122 png_destroy_read_struct(&png, &info, NULL);
3130 int imagetopng(opj_image_t * image, const char *write_idf)
3135 int *red, *green, *blue, *alpha;
3136 unsigned char *row_buf, *d;
3137 int has_alpha, width, height, nr_comp, color_type;
3138 int adjustR, adjustG, adjustB, x, y, fails, is16, force16;
3139 int opj_prec, prec, ushift, dshift;
3140 unsigned short mask = 0xffff;
3141 png_color_8 sig_bit;
3143 is16 = force16 = ushift = dshift = 0; fails = 1;
3144 prec = opj_prec = image->comps[0].prec;
3146 if(prec > 8 && prec < 16)
3148 prec = 16; force16 = 1;
3150 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3152 fprintf(stderr,"imagetopng: can not create %s"
3153 "\n\twrong bit_depth %d\n", write_idf, prec);
3156 writer = fopen(write_idf, "wb");
3158 if(writer == NULL) return fails;
3160 info = NULL; has_alpha = 0;
3162 /* Create and initialize the png_struct with the desired error handler
3163 * functions. If you want to use the default stderr and longjump method,
3164 * you can supply NULL for the last three parameters. We also check that
3165 * the library version is compatible with the one used at compile time,
3166 * in case we are using dynamically linked libraries. REQUIRED.
3168 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3170 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3172 if(png == NULL) goto fin;
3174 /* Allocate/initialize the image information data. REQUIRED
3176 info = png_create_info_struct(png);
3178 if(info == NULL) goto fin;
3180 /* Set error handling. REQUIRED if you are not supplying your own
3181 * error handling functions in the png_create_write_struct() call.
3183 if(setjmp(png_jmpbuf(png))) goto fin;
3185 /* I/O initialization functions is REQUIRED
3187 png_init_io(png, writer);
3189 /* Set the image information here. Width and height are up to 2^31,
3190 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3191 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3192 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3193 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3194 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3195 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3198 png_set_compression_level(png, Z_BEST_COMPRESSION);
3200 if(prec == 16) mask = 0xffff;
3202 if(prec == 8) mask = 0x00ff;
3204 if(prec == 4) mask = 0x000f;
3206 if(prec == 2) mask = 0x0003;
3208 if(prec == 1) mask = 0x0001;
3210 nr_comp = image->numcomps;
3213 && image->comps[0].dx == image->comps[1].dx
3214 && image->comps[1].dx == image->comps[2].dx
3215 && image->comps[0].dy == image->comps[1].dy
3216 && image->comps[1].dy == image->comps[2].dy
3217 && image->comps[0].prec == image->comps[1].prec
3218 && image->comps[1].prec == image->comps[2].prec)
3222 has_alpha = (nr_comp > 3);
3224 is16 = (prec == 16);
3226 width = image->comps[0].w;
3227 height = image->comps[0].h;
3229 red = image->comps[0].data;
3230 green = image->comps[1].data;
3231 blue = image->comps[2].data;
3233 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3237 sig_bit.alpha = prec;
3238 alpha = image->comps[3].data;
3239 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3243 sig_bit.alpha = 0; alpha = NULL;
3244 color_type = PNG_COLOR_TYPE_RGB;
3246 png_set_sBIT(png, info, &sig_bit);
3248 png_set_IHDR(png, info, width, height, prec,
3251 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3253 /*=============================*/
3254 png_write_info(png, info);
3255 /*=============================*/
3258 png_set_packing(png);
3262 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3264 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3265 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3266 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3268 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3270 for(y = 0; y < height; ++y)
3274 for(x = 0; x < width; ++x)
3278 v = *red + adjustR; ++red;
3280 if(force16) { v = (v<<ushift) + (v>>dshift); }
3282 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3284 v = *green + adjustG; ++green;
3286 if(force16) { v = (v<<ushift) + (v>>dshift); }
3288 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3290 v = *blue + adjustB; ++blue;
3292 if(force16) { v = (v<<ushift) + (v>>dshift); }
3294 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3300 if(force16) { v = (v<<ushift) + (v>>dshift); }
3302 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3306 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3307 *d++ = (unsigned char)((*green + adjustG) & mask); ++green;
3308 *d++ = (unsigned char)((*blue + adjustB) & mask); ++blue;
3312 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3316 png_write_row(png, row_buf);
3323 if(nr_comp == 1 /* GRAY */
3324 || ( nr_comp == 2 /* GRAY_ALPHA */
3325 && image->comps[0].dx == image->comps[1].dx
3326 && image->comps[0].dy == image->comps[1].dy
3327 && image->comps[0].prec == image->comps[1].prec))
3331 red = image->comps[0].data;
3335 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3337 sig_bit.gray = prec;
3338 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3340 color_type = PNG_COLOR_TYPE_GRAY;
3344 has_alpha = 1; sig_bit.alpha = prec;
3345 alpha = image->comps[1].data;
3346 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3348 width = image->comps[0].w;
3349 height = image->comps[0].h;
3351 png_set_IHDR(png, info, width, height, sig_bit.gray,
3354 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3356 png_set_sBIT(png, info, &sig_bit);
3357 /*=============================*/
3358 png_write_info(png, info);
3359 /*=============================*/
3360 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3364 png_set_packing(png);
3369 row_buf = (unsigned char*)
3370 malloc(width * nr_comp * sizeof(unsigned short));
3372 for(y = 0; y < height; ++y)
3376 for(x = 0; x < width; ++x)
3378 v = *red + adjustR; ++red;
3380 if(force16) { v = (v<<ushift) + (v>>dshift); }
3382 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3388 if(force16) { v = (v<<ushift) + (v>>dshift); }
3390 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3393 png_write_row(png, row_buf);
3398 else /* prec <= 8 */
3400 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3402 for(y = 0; y < height; ++y)
3406 for(x = 0; x < width; ++x)
3408 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3412 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3416 png_write_row(png, row_buf);
3424 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3427 png_write_end(png, info);
3435 png_destroy_write_struct(&png, &info);
3439 if(fails) remove(write_idf);
3443 #endif /* HAVE_LIBPNG */