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;
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 for (y=0; y < height; y++) {
430 unsigned int index=y*width;
432 for (x=0; x < width; x++, index++) {
433 r = (float)(image->comps[0].data[index]);
435 if (image->numcomps>2) {
436 g = (float)(image->comps[1].data[index]);
437 b = (float)(image->comps[2].data[index]);
439 else {// Greyscale ...
444 // TGA format writes BGR ...
445 value = (unsigned char)(b*scale);
446 fwrite(&value,1,1,fdest);
448 value = (unsigned char)(g*scale);
449 fwrite(&value,1,1,fdest);
451 value = (unsigned char)(r*scale);
452 fwrite(&value,1,1,fdest);
455 a = (float)(image->comps[alpha_channel].data[index]);
456 value = (unsigned char)(a*scale);
457 fwrite(&value,1,1,fdest);
465 /* -->> -->> -->> -->>
469 <<-- <<-- <<-- <<-- */
471 /* WORD defines a two byte word */
472 typedef unsigned short int WORD;
474 /* DWORD defines a four byte word */
475 typedef unsigned long int DWORD;
478 WORD bfType; /* 'BM' for Bitmap (19776) */
479 DWORD bfSize; /* Size of the file */
480 WORD bfReserved1; /* Reserved : 0 */
481 WORD bfReserved2; /* Reserved : 0 */
482 DWORD bfOffBits; /* Offset */
483 } BITMAPFILEHEADER_t;
486 DWORD biSize; /* Size of the structure in bytes */
487 DWORD biWidth; /* Width of the image in pixels */
488 DWORD biHeight; /* Heigth of the image in pixels */
489 WORD biPlanes; /* 1 */
490 WORD biBitCount; /* Number of color bits by pixels */
491 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
492 DWORD biSizeImage; /* Size of the image in bytes */
493 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
494 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
495 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
496 DWORD biClrImportant; /* Number of important color (0: ALL) */
497 } BITMAPINFOHEADER_t;
499 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
501 int subsampling_dx = parameters->subsampling_dx;
502 int subsampling_dy = parameters->subsampling_dy;
504 int i, numcomps, w, h;
505 OPJ_COLOR_SPACE color_space;
506 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
507 opj_image_t * image = NULL;
510 BITMAPFILEHEADER_t File_h;
511 BITMAPINFOHEADER_t Info_h;
513 unsigned char *table_R, *table_G, *table_B;
514 unsigned int j, PAD = 0;
521 IN = fopen(filename, "rb");
524 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
528 File_h.bfType = getc(IN);
529 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
531 if (File_h.bfType != 19778)
533 fprintf(stderr,"Error, not a BMP file!\n");
539 File_h.bfSize = getc(IN);
540 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
541 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
542 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
544 File_h.bfReserved1 = getc(IN);
545 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
547 File_h.bfReserved2 = getc(IN);
548 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
550 File_h.bfOffBits = getc(IN);
551 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
552 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
553 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
558 Info_h.biSize = getc(IN);
559 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
560 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
561 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
563 if(Info_h.biSize != 40)
565 fprintf(stderr,"Error, unknown BMP header size %ld\n", Info_h.biSize);
569 Info_h.biWidth = getc(IN);
570 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
571 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
572 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
575 Info_h.biHeight = getc(IN);
576 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
577 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
578 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
581 Info_h.biPlanes = getc(IN);
582 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
584 Info_h.biBitCount = getc(IN);
585 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
587 Info_h.biCompression = getc(IN);
588 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
589 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
590 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
592 Info_h.biSizeImage = getc(IN);
593 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
594 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
595 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
597 Info_h.biXpelsPerMeter = getc(IN);
598 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
599 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
600 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
602 Info_h.biYpelsPerMeter = getc(IN);
603 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
604 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
605 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
607 Info_h.biClrUsed = getc(IN);
608 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
609 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
610 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
612 Info_h.biClrImportant = getc(IN);
613 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
614 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
615 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
617 /* Read the data and store them in the OUT file */
619 if (Info_h.biBitCount == 24)
622 color_space = CLRSPC_SRGB;
623 /* initialize image components */
624 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
625 for(i = 0; i < numcomps; i++)
627 cmptparm[i].prec = 8;
629 cmptparm[i].sgnd = 0;
630 cmptparm[i].dx = subsampling_dx;
631 cmptparm[i].dy = subsampling_dy;
635 /* create the image */
636 image = opj_image_create(numcomps, &cmptparm[0], color_space);
643 /* set image offset and reference grid */
644 image->x0 = parameters->image_offset_x0;
645 image->y0 = parameters->image_offset_y0;
646 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
647 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
651 /* Place the cursor at the beginning of the image information */
652 fseek(IN, 0, SEEK_SET);
653 fseek(IN, File_h.bfOffBits, SEEK_SET);
658 /* PAD = 4 - (3 * W) % 4; */
659 /* PAD = (PAD == 4) ? 0 : PAD; */
660 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
662 RGB = (unsigned char *)
663 malloc((3 * W + PAD) * H * sizeof(unsigned char));
665 if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
668 opj_image_destroy(image);
669 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
675 for(y = 0; y < (int)H; y++)
677 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
678 for(x = 0; x < (int)W; x++)
680 unsigned char *pixel = &scanline[3 * x];
681 image->comps[0].data[index] = pixel[2]; /* R */
682 image->comps[1].data[index] = pixel[1]; /* G */
683 image->comps[2].data[index] = pixel[0]; /* B */
688 }/* if (Info_h.biBitCount == 24) */
690 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)//RGB
692 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
694 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
696 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
697 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
698 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
701 for (j = 0; j < Info_h.biClrUsed; j++)
703 table_B[j] = (unsigned char)getc(IN);
704 table_G[j] = (unsigned char)getc(IN);
705 table_R[j] = (unsigned char)getc(IN);
708 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
710 if(has_color) gray_scale = 0;
712 /* Place the cursor at the beginning of the image information */
713 fseek(IN, 0, SEEK_SET);
714 fseek(IN, File_h.bfOffBits, SEEK_SET);
718 if (Info_h.biWidth % 2)
721 numcomps = gray_scale ? 1 : 3;
722 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
723 /* initialize image components */
724 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
725 for(i = 0; i < numcomps; i++)
727 cmptparm[i].prec = 8;
729 cmptparm[i].sgnd = 0;
730 cmptparm[i].dx = subsampling_dx;
731 cmptparm[i].dy = subsampling_dy;
735 /* create the image */
736 image = opj_image_create(numcomps, &cmptparm[0], color_space);
740 free(table_R); free(table_G); free(table_B);
744 /* set image offset and reference grid */
745 image->x0 = parameters->image_offset_x0;
746 image->y0 = parameters->image_offset_y0;
747 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
748 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
752 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
754 if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
760 opj_image_destroy(image);
761 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
767 for (j = 0; j < W * H; j++)
769 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
771 image->comps[0].data[index] =
772 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
781 for (j = 0; j < W * H; j++)
783 if ((j % W < W - 1 && Info_h.biWidth % 2)
784 || !(Info_h.biWidth % 2))
786 unsigned char pixel_index =
787 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
788 image->comps[0].data[index] = table_R[pixel_index];
789 image->comps[1].data[index] = table_G[pixel_index];
790 image->comps[2].data[index] = table_B[pixel_index];
801 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)//RLE8
803 unsigned char *pix, *beyond;
804 int *gray, *red, *green, *blue;
805 unsigned int x, y, max;
809 if (Info_h.biClrUsed == 0)
810 Info_h.biClrUsed = 256;
811 else if (Info_h.biClrUsed > 256)
812 Info_h.biClrUsed = 256;
814 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
815 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
816 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
819 for (j = 0; j < Info_h.biClrUsed; j++)
821 table_B[j] = (unsigned char)getc(IN);
822 table_G[j] = (unsigned char)getc(IN);
823 table_R[j] = (unsigned char)getc(IN);
825 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
831 numcomps = gray_scale ? 1 : 3;
832 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
833 /* initialize image components */
834 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
835 for (i = 0; i < numcomps; i++)
837 cmptparm[i].prec = 8;
839 cmptparm[i].sgnd = 0;
840 cmptparm[i].dx = subsampling_dx;
841 cmptparm[i].dy = subsampling_dy;
845 /* create the image */
846 image = opj_image_create(numcomps, &cmptparm[0], color_space);
856 /* set image offset and reference grid */
857 image->x0 = parameters->image_offset_x0;
858 image->y0 = parameters->image_offset_y0;
859 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
860 - 1) * subsampling_dx + 1;
861 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
862 - 1) * subsampling_dy + 1;
866 /* Place the cursor at the beginning of the image information */
867 fseek(IN, 0, SEEK_SET);
868 fseek(IN, File_h.bfOffBits, SEEK_SET);
872 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
873 beyond = RGB + W * H;
885 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
886 *pix = (unsigned char)c1;
892 if (c == 0x00) /* EOL */
896 pix = RGB + x + (H - y - 1) * W;
898 else if (c == 0x01) /* EOP */
900 else if (c == 0x02) /* MOVE by dxdy */
906 pix = RGB + (H - y - 1) * W + x;
911 for (; i < c && x < W && pix < beyond; i++, x++, pix++)
914 *pix = (unsigned char)c1;
916 if (c & 1) /* skip padding byte */
924 gray = image->comps[0].data;
932 *gray++ = table_R[uc];
937 //int *red, *green, *blue;
939 red = image->comps[0].data;
940 green = image->comps[1].data;
941 blue = image->comps[2].data;
949 *red++ = table_R[uc];
950 *green++ = table_G[uc];
951 *blue++ = table_B[uc];
962 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
963 "is not yet implemented [%d]\n", Info_h.biBitCount);
969 int imagetobmp(opj_image_t * image, const char *outfile) {
973 int adjustR, adjustG, adjustB;
975 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
976 && image->comps[1].dx == image->comps[2].dx
977 && image->comps[0].dy == image->comps[1].dy
978 && image->comps[1].dy == image->comps[2].dy
979 && image->comps[0].prec == image->comps[1].prec
980 && image->comps[1].prec == image->comps[2].prec) {
982 /* -->> -->> -->> -->>
984 <<-- <<-- <<-- <<-- */
986 fdest = fopen(outfile, "wb");
988 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
992 w = image->comps[0].w;
993 h = image->comps[0].h;
995 fprintf(fdest, "BM");
999 fprintf(fdest, "%c%c%c%c",
1000 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1001 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1002 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1003 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1004 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1005 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
1009 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1010 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1011 (unsigned char) ((w) >> 8) & 0xff,
1012 (unsigned char) ((w) >> 16) & 0xff,
1013 (unsigned char) ((w) >> 24) & 0xff);
1014 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1015 (unsigned char) ((h) >> 8) & 0xff,
1016 (unsigned char) ((h) >> 16) & 0xff,
1017 (unsigned char) ((h) >> 24) & 0xff);
1018 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1019 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1020 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1021 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
1022 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1023 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1024 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1025 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1026 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1027 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1028 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1030 if (image->comps[0].prec > 8) {
1031 adjustR = image->comps[0].prec - 8;
1032 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1036 if (image->comps[1].prec > 8) {
1037 adjustG = image->comps[1].prec - 8;
1038 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1042 if (image->comps[2].prec > 8) {
1043 adjustB = image->comps[2].prec - 8;
1044 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1049 for (i = 0; i < w * h; i++) {
1050 unsigned char rc, gc, bc;
1053 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1054 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1055 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1056 if(r > 255) r = 255; else if(r < 0) r = 0;
1057 rc = (unsigned char)r;
1059 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1060 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1061 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
1062 if(g > 255) g = 255; else if(g < 0) g = 0;
1063 gc = (unsigned char)g;
1065 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1066 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1067 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
1068 if(b > 255) b = 255; else if(b < 0) b = 0;
1069 bc = (unsigned char)b;
1071 fprintf(fdest, "%c%c%c", bc, gc, rc);
1073 if ((i + 1) % w == 0) {
1074 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
1075 fprintf(fdest, "%c", 0);
1079 } else { /* Gray-scale */
1081 /* -->> -->> -->> -->>
1082 8 bits non code (Gray scale)
1083 <<-- <<-- <<-- <<-- */
1085 fdest = fopen(outfile, "wb");
1086 w = image->comps[0].w;
1087 h = image->comps[0].h;
1089 fprintf(fdest, "BM");
1093 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1094 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1095 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1096 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1097 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1098 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1099 ((54 + 1024) >> 16) & 0xff,
1100 ((54 + 1024) >> 24) & 0xff);
1104 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1105 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1106 (unsigned char) ((w) >> 8) & 0xff,
1107 (unsigned char) ((w) >> 16) & 0xff,
1108 (unsigned char) ((w) >> 24) & 0xff);
1109 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1110 (unsigned char) ((h) >> 8) & 0xff,
1111 (unsigned char) ((h) >> 16) & 0xff,
1112 (unsigned char) ((h) >> 24) & 0xff);
1113 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1114 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1115 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1116 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
1117 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
1118 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
1119 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
1120 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1121 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1122 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1123 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1125 if (image->comps[0].prec > 8) {
1126 adjustR = image->comps[0].prec - 8;
1127 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1131 for (i = 0; i < 256; i++) {
1132 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1135 for (i = 0; i < w * h; i++) {
1138 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1139 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1140 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1141 if(r > 255) r = 255; else if(r < 0) r = 0;
1143 fprintf(fdest, "%c", (unsigned char)r);
1145 if ((i + 1) % w == 0) {
1146 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1147 fprintf(fdest, "%c", 0);
1156 /* -->> -->> -->> -->>
1160 <<-- <<-- <<-- <<-- */
1163 unsigned char readuchar(FILE * f)
1166 if ( !fread(&c1, 1, 1, f) )
1168 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1174 unsigned short readushort(FILE * f, int bigendian)
1176 unsigned char c1, c2;
1177 if ( !fread(&c1, 1, 1, f) )
1179 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1182 if ( !fread(&c2, 1, 1, f) )
1184 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1188 return (c1 << 8) + c2;
1190 return (c2 << 8) + c1;
1193 unsigned int readuint(FILE * f, int bigendian)
1195 unsigned char c1, c2, c3, c4;
1196 if ( !fread(&c1, 1, 1, f) )
1198 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1201 if ( !fread(&c2, 1, 1, f) )
1203 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1206 if ( !fread(&c3, 1, 1, f) )
1208 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1211 if ( !fread(&c4, 1, 1, f) )
1213 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1217 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1219 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1222 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1225 int i, numcomps, max;
1226 OPJ_COLOR_SPACE color_space;
1227 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1228 opj_image_t * image = NULL;
1230 char endian1,endian2,sign;
1235 opj_image_comp_t *comp = NULL;
1238 color_space = CLRSPC_GRAY;
1240 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1244 f = fopen(filename, "rb");
1246 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1250 fseek(f, 0, SEEK_SET);
1251 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1252 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1258 while (signtmp[i]!='\0') {
1259 if (signtmp[i]=='-') sign='-';
1264 if (endian1=='M' && endian2=='L') {
1266 } else if (endian2=='M' && endian1=='L') {
1269 fprintf(stderr, "Bad pgx header, please check input file\n");
1273 /* initialize image component */
1275 cmptparm.x0 = parameters->image_offset_x0;
1276 cmptparm.y0 = parameters->image_offset_y0;
1277 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1278 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1285 cmptparm.prec = prec;
1286 cmptparm.bpp = prec;
1287 cmptparm.dx = parameters->subsampling_dx;
1288 cmptparm.dy = parameters->subsampling_dy;
1290 /* create the image */
1291 image = opj_image_create(numcomps, &cmptparm, color_space);
1296 /* set image offset and reference grid */
1297 image->x0 = cmptparm.x0;
1298 image->y0 = cmptparm.x0;
1299 image->x1 = cmptparm.w;
1300 image->y1 = cmptparm.h;
1302 /* set image data */
1304 comp = &image->comps[0];
1306 for (i = 0; i < w * h; i++) {
1308 if (comp->prec <= 8) {
1312 v = (char) readuchar(f);
1314 } else if (comp->prec <= 16) {
1316 v = readushort(f, bigendian);
1318 v = (short) readushort(f, bigendian);
1322 v = readuint(f, bigendian);
1324 v = (int) readuint(f, bigendian);
1332 comp->bpp = int_floorlog2(max) + 1;
1337 int imagetopgx(opj_image_t * image, const char *outfile) {
1342 for (compno = 0; compno < image->numcomps; compno++) {
1343 opj_image_comp_t *comp = &image->comps[compno];
1344 char bname[256]; /* buffer for name */
1345 char *name = bname; /* pointer */
1347 const size_t olen = strlen(outfile);
1348 const size_t dotpos = olen - 4;
1349 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1350 if( outfile[dotpos] != '.' ) {
1351 /* `pgx` was recognized but there is no dot at expected position */
1352 fprintf(stderr, "ERROR -> Impossible happen." );
1356 name = (char*)malloc(total+1);
1358 strncpy(name, outfile, dotpos);
1359 //if (image->numcomps > 1) {
1360 sprintf(name+dotpos, "_%d.pgx", compno);
1362 strcpy(name+dotpos, ".pgx");
1364 fdest = fopen(name, "wb");
1366 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1369 /* dont need name anymore */
1374 w = image->comps[compno].w;
1375 h = image->comps[compno].h;
1377 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1378 if (comp->prec <= 8) {
1380 } else if (comp->prec <= 16) {
1385 for (i = 0; i < w * h; i++) {
1386 int v = image->comps[compno].data[i];
1387 for (j = nbytes - 1; j >= 0; j--) {
1388 char byte = (char) (v >> (j * 8));
1389 fwrite(&byte, 1, 1, fdest);
1398 /* -->> -->> -->> -->>
1402 <<-- <<-- <<-- <<-- */
1406 int width, height, maxval, depth, format;
1407 char rgb, rgba, gray, graya, bw;
1411 static char *skip_white(char *s)
1415 if(*s == '\n' || *s == '\r') return NULL;
1416 if(isspace(*s)) { ++s; continue; }
1422 static char *skip_int(char *start, int *out_n)
1427 *out_n = 0; s = start;
1429 s = skip_white(start);
1430 if(s == NULL) return NULL;
1435 if( !isdigit(*s)) break;
1438 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1442 static char *skip_idf(char *start, char out_idf[256])
1447 s = skip_white(start);
1448 if(s == NULL) return NULL;
1453 if(isalpha(*s) || *s == '_') { ++s; continue; }
1456 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1460 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1463 int format, have_wh, end, ttype;
1464 char idf[256], type[256];
1467 if (fgets(line, 250, reader) == NULL)
1469 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1475 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1477 format = atoi(line + 1);
1478 if(format < 1 || format > 7)
1480 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1483 ph->format = format;
1484 ttype = end = have_wh = 0;
1486 while(fgets(line, 250, reader))
1488 if(*line == '#') continue;
1494 s = skip_idf(s, idf);
1496 if(s == NULL || *s == 0) return;
1498 if(strcmp(idf, "ENDHDR") == 0)
1502 if(strcmp(idf, "WIDTH") == 0)
1504 s = skip_int(s, &ph->width);
1505 if(s == NULL || *s == 0) return;
1509 if(strcmp(idf, "HEIGHT") == 0)
1511 s = skip_int(s, &ph->height);
1512 if(s == NULL || *s == 0) return;
1516 if(strcmp(idf, "DEPTH") == 0)
1518 s = skip_int(s, &ph->depth);
1519 if(s == NULL || *s == 0) return;
1523 if(strcmp(idf, "MAXVAL") == 0)
1525 s = skip_int(s, &ph->maxval);
1526 if(s == NULL || *s == 0) return;
1530 if(strcmp(idf, "TUPLTYPE") == 0)
1532 s = skip_idf(s, type);
1533 if(s == NULL || *s == 0) return;
1535 if(strcmp(type, "BLACKANDWHITE") == 0)
1537 ph->bw = 1; ttype = 1; continue;
1539 if(strcmp(type, "GRAYSCALE") == 0)
1541 ph->gray = 1; ttype = 1; continue;
1543 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1545 ph->graya = 1; ttype = 1; continue;
1547 if(strcmp(type, "RGB") == 0)
1549 ph->rgb = 1; ttype = 1; continue;
1551 if(strcmp(type, "RGB_ALPHA") == 0)
1553 ph->rgba = 1; ttype = 1; continue;
1555 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1558 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1560 } /* if(format == 7) */
1564 s = skip_int(s, &ph->width);
1566 s = skip_int(s, &ph->height);
1570 if(format == 1 || format == 4) break;
1574 if(format == 2 || format == 3 || format == 5 || format == 6)
1576 /* P2, P3, P5, P6: */
1577 s = skip_int(s, &ph->maxval);
1579 if(ph->maxval > 65535) return;
1582 }/* while(fgets( ) */
1583 if(format == 2 || format == 3 || format > 4)
1585 if(ph->maxval < 1 || ph->maxval > 65535) return;
1587 if(ph->width < 1 || ph->height < 1) return;
1593 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1595 if(ph->depth < 1 || ph->depth > 4) return;
1597 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1602 if(format != 1 && format != 4)
1604 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1608 if(ph->width && ph->height) ph->ok = 1;
1614 static int has_prec(int val)
1616 if(val < 2) return 1;
1617 if(val < 4) return 2;
1618 if(val < 8) return 3;
1619 if(val < 16) return 4;
1620 if(val < 32) return 5;
1621 if(val < 64) return 6;
1622 if(val < 128) return 7;
1623 if(val < 256) return 8;
1624 if(val < 512) return 9;
1625 if(val < 1024) return 10;
1626 if(val < 2048) return 11;
1627 if(val < 4096) return 12;
1628 if(val < 8192) return 13;
1629 if(val < 16384) return 14;
1630 if(val < 32768) return 15;
1634 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1635 int subsampling_dx = parameters->subsampling_dx;
1636 int subsampling_dy = parameters->subsampling_dy;
1639 int i, compno, numcomps, w, h, prec, format;
1640 OPJ_COLOR_SPACE color_space;
1641 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1642 opj_image_t * image = NULL;
1643 struct pnm_header header_info;
1645 if((fp = fopen(filename, "rb")) == NULL)
1647 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1650 memset(&header_info, 0, sizeof(struct pnm_header));
1652 read_pnm_header(fp, &header_info);
1654 if(!header_info.ok) { fclose(fp); return NULL; }
1656 format = header_info.format;
1660 case 1: /* ascii bitmap */
1661 case 4: /* raw bitmap */
1665 case 2: /* ascii greymap */
1666 case 5: /* raw greymap */
1670 case 3: /* ascii pixmap */
1671 case 6: /* raw pixmap */
1675 case 7: /* arbitrary map */
1676 numcomps = header_info.depth;
1679 default: fclose(fp); return NULL;
1682 color_space = CLRSPC_GRAY;/* GRAY, GRAYA */
1684 color_space = CLRSPC_SRGB;/* RGB, RGBA */
1686 prec = has_prec(header_info.maxval);
1688 if(prec < 8) prec = 8;
1690 w = header_info.width;
1691 h = header_info.height;
1692 subsampling_dx = parameters->subsampling_dx;
1693 subsampling_dy = parameters->subsampling_dy;
1695 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1697 for(i = 0; i < numcomps; i++)
1699 cmptparm[i].prec = prec;
1700 cmptparm[i].bpp = prec;
1701 cmptparm[i].sgnd = 0;
1702 cmptparm[i].dx = subsampling_dx;
1703 cmptparm[i].dy = subsampling_dy;
1707 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1709 if(!image) { fclose(fp); return NULL; }
1711 /* set image offset and reference grid */
1712 image->x0 = parameters->image_offset_x0;
1713 image->y0 = parameters->image_offset_y0;
1714 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1715 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1717 if((format == 2) || (format == 3)) /* ascii pixmap */
1721 for (i = 0; i < w * h; i++)
1723 for(compno = 0; compno < numcomps; compno++)
1726 if (fscanf(fp, "%u", &index) != 1)
1727 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1729 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1737 && ( header_info.gray || header_info.graya
1738 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1740 unsigned char c0, c1, one;
1744 for (i = 0; i < w * h; i++)
1746 for(compno = 0; compno < numcomps; compno++)
1748 if ( !fread(&c0, 1, 1, fp) )
1749 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1752 image->comps[compno].data[i] = c0;
1756 if ( !fread(&c1, 1, 1, fp) )
1757 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1759 image->comps[compno].data[i] = ((c0<<8) | c1);
1765 if(format == 1) /* ascii bitmap */
1767 for (i = 0; i < w * h; i++)
1771 if ( fscanf(fp, "%u", &index) != 1)
1772 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1774 image->comps[0].data[i] = (index?0:255);
1784 for(y = 0; y < h; ++y)
1788 for(x = 0; x < w; ++x)
1793 uc = (unsigned char)getc(fp);
1795 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1801 if((format == 7 && header_info.bw)) //MONO
1805 for(i = 0; i < w * h; ++i)
1807 if ( !fread(&uc, 1, 1, fp) )
1808 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1809 image->comps[0].data[i] = (uc & 1)?0:255;
1817 int imagetopnm(opj_image_t * image, const char *outfile)
1819 int *red, *green, *blue, *alpha;
1821 int i, compno, ncomp;
1822 int adjustR, adjustG, adjustB, adjustA;
1823 int fails, two, want_gray, has_alpha, triple;
1826 const char *tmp = outfile;
1829 if((prec = image->comps[0].prec) > 16)
1831 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1832 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1835 two = has_alpha = 0; fails = 1;
1836 ncomp = image->numcomps;
1838 while (*tmp) ++tmp; tmp -= 2;
1839 want_gray = (*tmp == 'g' || *tmp == 'G');
1840 ncomp = image->numcomps;
1842 if(want_gray) ncomp = 1;
1844 if (ncomp == 2 /* GRAYA */
1845 || (ncomp > 2 /* RGB, RGBA */
1846 && image->comps[0].dx == image->comps[1].dx
1847 && image->comps[1].dx == image->comps[2].dx
1848 && image->comps[0].dy == image->comps[1].dy
1849 && image->comps[1].dy == image->comps[2].dy
1850 && image->comps[0].prec == image->comps[1].prec
1851 && image->comps[1].prec == image->comps[2].prec
1854 fdest = fopen(outfile, "wb");
1858 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1862 triple = (ncomp > 2);
1863 wr = image->comps[0].w; hr = image->comps[0].h;
1864 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1866 red = image->comps[0].data;
1870 green = image->comps[1].data;
1871 blue = image->comps[2].data;
1873 else green = blue = NULL;
1877 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1879 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1880 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1881 wr, hr, ncomp, max, tt);
1882 alpha = image->comps[ncomp - 1].data;
1883 adjustA = (image->comps[ncomp - 1].sgnd ?
1884 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1888 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1889 opj_version(), wr, hr, max);
1890 alpha = NULL; adjustA = 0;
1892 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1896 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1897 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1899 else adjustG = adjustB = 0;
1901 for(i = 0; i < wr * hr; ++i)
1905 v = *red + adjustR; ++red;
1907 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1911 v = *green + adjustG; ++green;
1913 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1915 v = *blue + adjustB; ++blue;
1917 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1923 v = *alpha + adjustA; ++alpha;
1925 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1933 fprintf(fdest, "%c", (unsigned char)*red++);
1935 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
1938 fprintf(fdest, "%c", (unsigned char)*alpha++);
1942 fclose(fdest); return 0;
1947 if (image->numcomps > ncomp)
1949 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1950 fprintf(stderr," is written to the file\n");
1952 destname = (char*)malloc(strlen(outfile) + 8);
1954 for (compno = 0; compno < ncomp; compno++)
1957 sprintf(destname, "%d.%s", compno, outfile);
1959 sprintf(destname, "%s", outfile);
1961 fdest = fopen(destname, "wb");
1964 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1968 wr = image->comps[compno].w; hr = image->comps[compno].h;
1969 prec = image->comps[compno].prec;
1970 max = (1<<prec) - 1;
1972 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
1973 opj_version(), wr, hr, max);
1975 red = image->comps[compno].data;
1977 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1981 for (i = 0; i < wr * hr; i++)
1983 v = *red + adjustR; ++red;
1985 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1991 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1995 else /* prec <= 8 */
1997 for(i = 0; i < wr * hr; ++i)
1999 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
2010 /* -->> -->> -->> -->>
2014 <<-- <<-- <<-- <<-- */
2016 typedef struct tiff_infoheader{
2017 DWORD tiWidth; // Width of Image in pixel
2018 DWORD tiHeight; // Height of Image in pixel
2019 DWORD tiPhoto; // Photometric
2020 WORD tiBps; // Bits per sample
2021 WORD tiSf; // Sample Format
2022 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
2023 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
2026 int imagetotif(opj_image_t * image, const char *outfile)
2028 int width, height, imgsize;
2029 int bps,index,adjust, sgnd;
2030 int ushift, dshift, has_alpha, force16;
2036 ushift = dshift = force16 = has_alpha = 0;
2037 bps = image->comps[0].prec;
2039 if(bps > 8 && bps < 16)
2041 ushift = 16 - bps; dshift = bps - ushift;
2042 bps = 16; force16 = 1;
2045 if(bps != 8 && bps != 16)
2047 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2049 fprintf(stderr,"\tAborting\n");
2052 tif = TIFFOpen(outfile, "wb");
2056 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2059 sgnd = image->comps[0].sgnd;
2060 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2062 if(image->numcomps >= 3
2063 && image->comps[0].dx == image->comps[1].dx
2064 && image->comps[1].dx == image->comps[2].dx
2065 && image->comps[0].dy == image->comps[1].dy
2066 && image->comps[1].dy == image->comps[2].dy
2067 && image->comps[0].prec == image->comps[1].prec
2068 && image->comps[1].prec == image->comps[2].prec)
2070 has_alpha = (image->numcomps == 4);
2072 width = image->comps[0].w;
2073 height = image->comps[0].h;
2074 imgsize = width * height ;
2076 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2077 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2078 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2079 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2080 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2081 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2082 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2083 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2084 strip_size = TIFFStripSize(tif);
2085 buf = _TIFFmalloc(strip_size);
2088 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2090 unsigned char *dat8;
2091 tsize_t i, ssize, last_i = 0;
2093 ssize = TIFFStripSize(tif);
2094 dat8 = (unsigned char*)buf;
2098 step = 3 + has_alpha;
2101 for(i=0; i < ssize - restx; i += step)
2107 r = image->comps[0].data[index];
2108 g = image->comps[1].data[index];
2109 b = image->comps[2].data[index];
2110 if(has_alpha) a = image->comps[3].data[index];
2117 if(has_alpha) a += adjust;
2122 if(has_alpha) dat8[i+3] = a;
2133 for(i = last_i; i < ssize; i += step)
2139 r = image->comps[0].data[index];
2140 g = image->comps[1].data[index];
2141 b = image->comps[2].data[index];
2142 if(has_alpha) a = image->comps[3].data[index];
2149 if(has_alpha) a += adjust;
2152 if(i+1 < ssize) dat8[i+1] = g ; else break;
2153 if(i+2 < ssize) dat8[i+2] = b ; else break;
2156 if(i+3 < ssize) dat8[i+3] = a ; else break;
2163 }//if(last_i < ssize)
2169 step = 6 + has_alpha + has_alpha;
2172 for(i = 0; i < ssize - restx ; i += step)
2178 r = image->comps[0].data[index];
2179 g = image->comps[1].data[index];
2180 b = image->comps[2].data[index];
2181 if(has_alpha) a = image->comps[3].data[index];
2188 if(has_alpha) a += adjust;
2192 r = (r<<ushift) + (r>>dshift);
2193 g = (g<<ushift) + (g>>dshift);
2194 b = (b<<ushift) + (b>>dshift);
2195 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2198 dat8[i+1] = (r >> 8);//MSB
2200 dat8[i+3] = (g >> 8);
2202 dat8[i+5] = (b >> 8);
2206 dat8[i+7] = (a >> 8);
2217 for(i = last_i ; i < ssize ; i += step)
2223 r = image->comps[0].data[index];
2224 g = image->comps[1].data[index];
2225 b = image->comps[2].data[index];
2226 if(has_alpha) a = image->comps[3].data[index];
2233 if(has_alpha) a += adjust;
2237 r = (r<<ushift) + (r>>dshift);
2238 g = (g<<ushift) + (g>>dshift);
2239 b = (b<<ushift) + (b>>dshift);
2240 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2243 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB
2244 if(i+2 < ssize) dat8[i+2] = g; else break;
2245 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2246 if(i+4 < ssize) dat8[i+4] = b; else break;
2247 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2251 if(i+6 < ssize) dat8[i+6] = a; else break;
2252 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2259 }//if(last_i < ssize)
2262 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2265 _TIFFfree((void*)buf);
2271 if(image->numcomps == 1 /* GRAY */
2272 || ( image->numcomps == 2 /* GRAY_ALPHA */
2273 && image->comps[0].dx == image->comps[1].dx
2274 && image->comps[0].dy == image->comps[1].dy
2275 && image->comps[0].prec == image->comps[1].prec))
2279 has_alpha = (image->numcomps == 2);
2281 width = image->comps[0].w;
2282 height = image->comps[0].h;
2283 imgsize = width * height;
2286 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2287 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2288 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2289 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2290 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2291 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2292 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2293 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2295 /* Get a buffer for the data */
2296 strip_size = TIFFStripSize(tif);
2297 buf = _TIFFmalloc(strip_size);
2300 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2302 unsigned char *dat8;
2303 tsize_t i, ssize = TIFFStripSize(tif);
2304 dat8 = (unsigned char*)buf;
2308 step = 1 + has_alpha;
2310 for(i=0; i < ssize; i += step)
2316 r = image->comps[0].data[index];
2317 if(has_alpha) a = image->comps[1].data[index];
2322 if(has_alpha) a += adjust;
2325 if(has_alpha) dat8[i+1] = a;
2335 step = 2 + has_alpha + has_alpha;
2337 for(i=0; i < ssize; i += step)
2343 r = image->comps[0].data[index];
2344 if(has_alpha) a = image->comps[1].data[index];
2349 if(has_alpha) a += adjust;
2353 r = (r<<ushift) + (r>>dshift);
2354 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2357 dat8[i+1] = r >> 8;//MSB
2364 }//if(index < imgsize)
2369 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2380 fprintf(stderr,"imagetotif: Bad color format.\n"
2381 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2382 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2389 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2390 * CINEMA : 12 bit precision
2392 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2394 int subsampling_dx = parameters->subsampling_dx;
2395 int subsampling_dy = parameters->subsampling_dy;
2397 tiff_infoheader_t Info;
2401 int j, numcomps, w, h,index;
2402 OPJ_COLOR_SPACE color_space;
2403 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2404 opj_image_t *image = NULL;
2408 tif = TIFFOpen(filename, "r");
2412 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2415 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
2416 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
2417 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
2418 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
2419 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
2421 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
2422 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
2429 if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
2430 if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
2435 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2436 " implemented\n",Info.tiBps);
2439 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2440 " and GRAY(A) has been implemented\n",(int) Info.tiPhoto);
2442 fprintf(stderr,"\tAborting\n");
2449 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2451 uint16 extrasamples;
2453 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2454 &extrasamples, &sampleinfo);
2456 if(extrasamples >= 1)
2458 switch(sampleinfo[0])
2460 case EXTRASAMPLE_UNSPECIFIED:
2461 /* Workaround for some images without correct info about alpha channel
2467 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2468 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2474 /* initialize image components
2476 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2478 if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2480 numcomps = 3 + has_alpha;
2481 color_space = CLRSPC_SRGB;
2483 for(j = 0; j < numcomps; j++)
2485 if(parameters->cp_cinema)
2487 cmptparm[j].prec = 12;
2488 cmptparm[j].bpp = 12;
2492 cmptparm[j].prec = Info.tiBps;
2493 cmptparm[j].bpp = Info.tiBps;
2495 cmptparm[j].dx = subsampling_dx;
2496 cmptparm[j].dy = subsampling_dy;
2501 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2508 /* set image offset and reference grid
2510 image->x0 = parameters->image_offset_x0;
2511 image->y0 = parameters->image_offset_y0;
2512 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2513 image->x0 + (w - 1) * subsampling_dx + 1;
2514 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2515 image->y0 + (h - 1) * subsampling_dy + 1;
2517 buf = _TIFFmalloc(TIFFStripSize(tif));
2519 strip_size=TIFFStripSize(tif);
2521 imgsize = image->comps[0].w * image->comps[0].h ;
2522 /* Read the Image components
2524 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2526 unsigned char *dat8;
2529 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2530 dat8 = (unsigned char*)buf;
2532 if(Info.tiBps == 16)
2534 step = 6 + has_alpha + has_alpha;
2536 for(i = 0; i < ssize; i += step)
2540 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
2541 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
2542 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
2544 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2546 if(parameters->cp_cinema)
2548 /* Rounding 16 to 12 bits
2550 image->comps[0].data[index] =
2551 (image->comps[0].data[index] + 0x08) >> 4 ;
2552 image->comps[1].data[index] =
2553 (image->comps[1].data[index] + 0x08) >> 4 ;
2554 image->comps[2].data[index] =
2555 (image->comps[2].data[index] + 0x08) >> 4 ;
2557 image->comps[3].data[index] =
2558 (image->comps[3].data[index] + 0x08) >> 4 ;
2565 }//if(Info.tiBps == 16)
2569 step = 3 + has_alpha;
2571 for(i = 0; i < ssize; i += step)
2575 image->comps[0].data[index] = dat8[i+0];// R
2576 image->comps[1].data[index] = dat8[i+1];// G
2577 image->comps[2].data[index] = dat8[i+2];// B
2579 image->comps[3].data[index] = dat8[i+3];
2581 if(parameters->cp_cinema)
2583 /* Rounding 8 to 12 bits
2585 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2586 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2587 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2589 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2596 }//if( Info.tiBps == 8)
2598 if(Info.tiBps == 12)/* CINEMA file */
2602 for(i = 0; i < ssize; i += step)
2604 if((index < imgsize)&(index+1 < imgsize))
2606 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2607 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2609 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2610 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2612 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2613 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2629 if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2631 numcomps = 1 + has_alpha;
2632 color_space = CLRSPC_GRAY;
2634 for(j = 0; j < numcomps; ++j)
2636 cmptparm[j].prec = Info.tiBps;
2637 cmptparm[j].bpp = Info.tiBps;
2638 cmptparm[j].dx = subsampling_dx;
2639 cmptparm[j].dy = subsampling_dy;
2643 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2650 /* set image offset and reference grid
2652 image->x0 = parameters->image_offset_x0;
2653 image->y0 = parameters->image_offset_y0;
2654 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2655 image->x0 + (w - 1) * subsampling_dx + 1;
2656 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2657 image->y0 + (h - 1) * subsampling_dy + 1;
2659 buf = _TIFFmalloc(TIFFStripSize(tif));
2661 strip_size = TIFFStripSize(tif);
2663 imgsize = image->comps[0].w * image->comps[0].h ;
2664 /* Read the Image components
2666 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2668 unsigned char *dat8;
2672 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2673 dat8 = (unsigned char*)buf;
2675 if(Info.tiBps == 16)
2677 step = 2 + has_alpha + has_alpha;
2679 for(i = 0; i < ssize; i += step)
2683 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2685 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2695 step = 1 + has_alpha;
2697 for(i = 0; i < ssize; i += step)
2701 image->comps[0].data[index] = dat8[i+0];
2703 image->comps[1].data[index] = dat8[i+1];
2721 #endif /* HAVE_LIBTIFF */
2723 /* -->> -->> -->> -->>
2727 <<-- <<-- <<-- <<-- */
2729 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2730 int subsampling_dx = parameters->subsampling_dx;
2731 int subsampling_dy = parameters->subsampling_dy;
2734 int i, compno, numcomps, w, h;
2735 OPJ_COLOR_SPACE color_space;
2736 opj_image_cmptparm_t *cmptparm;
2737 opj_image_t * image = NULL;
2740 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2742 fprintf(stderr,"\nError: invalid raw image parameters\n");
2743 fprintf(stderr,"Please use the Format option -F:\n");
2744 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2745 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2746 fprintf(stderr,"Aborting\n");
2750 f = fopen(filename, "rb");
2752 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2753 fprintf(stderr,"Aborting\n");
2756 numcomps = raw_cp->rawComp;
2757 color_space = CLRSPC_SRGB;
2758 w = raw_cp->rawWidth;
2759 h = raw_cp->rawHeight;
2760 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2762 /* initialize image components */
2763 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2764 for(i = 0; i < numcomps; i++) {
2765 cmptparm[i].prec = raw_cp->rawBitDepth;
2766 cmptparm[i].bpp = raw_cp->rawBitDepth;
2767 cmptparm[i].sgnd = raw_cp->rawSigned;
2768 cmptparm[i].dx = subsampling_dx;
2769 cmptparm[i].dy = subsampling_dy;
2773 /* create the image */
2774 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2779 /* set image offset and reference grid */
2780 image->x0 = parameters->image_offset_x0;
2781 image->y0 = parameters->image_offset_y0;
2782 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2783 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2785 if(raw_cp->rawBitDepth <= 8)
2787 unsigned char value = 0;
2788 for(compno = 0; compno < numcomps; compno++) {
2789 for (i = 0; i < w * h; i++) {
2790 if (!fread(&value, 1, 1, f)) {
2791 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2794 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2798 else if(raw_cp->rawBitDepth <= 16)
2800 unsigned short value;
2801 for(compno = 0; compno < numcomps; compno++) {
2802 for (i = 0; i < w * h; i++) {
2804 if (!fread(&temp, 1, 1, f)) {
2805 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2809 if (!fread(&temp, 1, 1, f)) {
2810 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2814 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2819 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2823 if (fread(&ch, 1, 1, f)) {
2824 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2831 int imagetoraw(opj_image_t * image, const char *outfile)
2833 FILE *rawFile = NULL;
2839 if((image->numcomps * image->x1 * image->y1) == 0)
2841 fprintf(stderr,"\nError: invalid raw image parameters\n");
2845 rawFile = fopen(outfile, "wb");
2847 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2851 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2853 for(compno = 0; compno < image->numcomps; compno++)
2855 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2856 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2858 w = image->comps[compno].w;
2859 h = image->comps[compno].h;
2861 if(image->comps[compno].prec <= 8)
2863 if(image->comps[compno].sgnd == 1)
2866 int mask = (1 << image->comps[compno].prec) - 1;
2867 ptr = image->comps[compno].data;
2868 for (line = 0; line < h; line++) {
2869 for(row = 0; row < w; row++) {
2870 curr = (signed char) (*ptr & mask);
2871 fwrite(&curr, sizeof(signed char), 1, rawFile);
2876 else if(image->comps[compno].sgnd == 0)
2879 int mask = (1 << image->comps[compno].prec) - 1;
2880 ptr = image->comps[compno].data;
2881 for (line = 0; line < h; line++) {
2882 for(row = 0; row < w; row++) {
2883 curr = (unsigned char) (*ptr & mask);
2884 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2890 else if(image->comps[compno].prec <= 16)
2892 if(image->comps[compno].sgnd == 1)
2894 signed short int curr;
2895 int mask = (1 << image->comps[compno].prec) - 1;
2896 ptr = image->comps[compno].data;
2897 for (line = 0; line < h; line++) {
2898 for(row = 0; row < w; row++) {
2900 curr = (signed short int) (*ptr & mask);
2901 temp = (unsigned char) (curr >> 8);
2902 fwrite(&temp, 1, 1, rawFile);
2903 temp = (unsigned char) curr;
2904 fwrite(&temp, 1, 1, rawFile);
2909 else if(image->comps[compno].sgnd == 0)
2911 unsigned short int curr;
2912 int mask = (1 << image->comps[compno].prec) - 1;
2913 ptr = image->comps[compno].data;
2914 for (line = 0; line < h; line++) {
2915 for(row = 0; row < w; row++) {
2917 curr = (unsigned short int) (*ptr & mask);
2918 temp = (unsigned char) (curr >> 8);
2919 fwrite(&temp, 1, 1, rawFile);
2920 temp = (unsigned char) curr;
2921 fwrite(&temp, 1, 1, rawFile);
2927 else if (image->comps[compno].prec <= 32)
2929 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2934 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2944 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
2945 #define MAGIC_SIZE 8
2946 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
2948 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
2952 double gamma, display_exponent;
2953 int bit_depth, interlace_type,compression_type, filter_type;
2955 png_uint_32 resx, resy;
2957 png_uint_32 width, height;
2958 int color_type, has_alpha, is16;
2961 unsigned char **rows;
2964 opj_image_cmptparm_t cmptparm[4];
2966 unsigned int nr_comp;
2968 unsigned char sigbuf[8];
2970 if((reader = fopen(read_idf, "rb")) == NULL)
2972 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
2975 image = NULL; png = NULL; rows = NULL;
2977 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
2978 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
2980 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
2983 /* libpng-VERSION/example.c:
2984 * PC : screen_gamma = 2.2;
2985 * Mac: screen_gamma = 1.7 or 1.0;
2987 display_exponent = 2.2;
2989 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
2990 NULL, NULL, NULL)) == NULL)
2992 if((info = png_create_info_struct(png)) == NULL)
2995 if(setjmp(png_jmpbuf(png)))
2998 png_init_io(png, reader);
2999 png_set_sig_bytes(png, MAGIC_SIZE);
3001 png_read_info(png, info);
3003 if(png_get_IHDR(png, info, &width, &height,
3004 &bit_depth, &color_type, &interlace_type,
3005 &compression_type, &filter_type) == 0)
3008 /* png_set_expand():
3009 * expand paletted images to RGB, expand grayscale images of
3010 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3011 * to alpha channels.
3013 if(color_type == PNG_COLOR_TYPE_PALETTE)
3014 png_set_expand(png);
3016 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3017 png_set_expand(png);
3019 if(png_get_valid(png, info, PNG_INFO_tRNS))
3020 png_set_expand(png);
3022 is16 = (bit_depth == 16);
3024 /* GRAY => RGB; GRAY_ALPHA => RGBA
3026 if(color_type == PNG_COLOR_TYPE_GRAY
3027 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3029 png_set_gray_to_rgb(png);
3031 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
3032 PNG_COLOR_TYPE_RGB_ALPHA);
3034 if( !png_get_gAMA(png, info, &gamma))
3037 png_set_gamma(png, display_exponent, gamma);
3039 png_read_update_info(png, info);
3041 png_get_pHYs(png, info, &resx, &resy, &unit);
3043 color_type = png_get_color_type(png, info);
3045 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3047 nr_comp = 3 + has_alpha;
3049 bit_depth = png_get_bit_depth(png, info);
3051 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
3052 for(i = 0; i < height; ++i)
3053 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
3055 png_read_image(png, rows);
3057 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3059 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3061 for(i = 0; i < nr_comp; ++i)
3063 cmptparm[i].prec = bit_depth;
3064 /* bits_per_pixel: 8 or 16 */
3065 cmptparm[i].bpp = bit_depth;
3066 cmptparm[i].sgnd = 0;
3067 cmptparm[i].dx = sub_dx;
3068 cmptparm[i].dy = sub_dy;
3069 cmptparm[i].w = width;
3070 cmptparm[i].h = height;
3073 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3075 if(image == NULL) goto fin;
3077 image->x0 = params->image_offset_x0;
3078 image->y0 = params->image_offset_y0;
3079 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3080 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3082 r = image->comps[0].data;
3083 g = image->comps[1].data;
3084 b = image->comps[2].data;
3085 a = image->comps[3].data;
3087 for(i = 0; i < height; ++i)
3091 for(j = 0; j < width; ++j)
3095 *r++ = s[0]<<8|s[1]; s += 2;
3097 *g++ = s[0]<<8|s[1]; s += 2;
3099 *b++ = s[0]<<8|s[1]; s += 2;
3101 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3105 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3107 if(has_alpha) *a++ = *s++;
3113 for(i = 0; i < height; ++i)
3118 png_destroy_read_struct(&png, &info, NULL);
3126 int imagetopng(opj_image_t * image, const char *write_idf)
3131 int *red, *green, *blue, *alpha;
3132 unsigned char *row_buf, *d;
3133 int has_alpha, width, height, nr_comp, color_type;
3134 int adjustR, adjustG, adjustB, x, y, fails, is16, force16;
3135 int opj_prec, prec, ushift, dshift;
3136 unsigned short mask = 0xffff;
3137 png_color_8 sig_bit;
3139 is16 = force16 = ushift = dshift = 0; fails = 1;
3140 prec = opj_prec = image->comps[0].prec;
3142 if(prec > 8 && prec < 16)
3144 prec = 16; force16 = 1;
3146 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3148 fprintf(stderr,"imagetopng: can not create %s"
3149 "\n\twrong bit_depth %d\n", write_idf, prec);
3152 writer = fopen(write_idf, "wb");
3154 if(writer == NULL) return fails;
3156 info = NULL; has_alpha = 0;
3158 /* Create and initialize the png_struct with the desired error handler
3159 * functions. If you want to use the default stderr and longjump method,
3160 * you can supply NULL for the last three parameters. We also check that
3161 * the library version is compatible with the one used at compile time,
3162 * in case we are using dynamically linked libraries. REQUIRED.
3164 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3166 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3168 if(png == NULL) goto fin;
3170 /* Allocate/initialize the image information data. REQUIRED
3172 info = png_create_info_struct(png);
3174 if(info == NULL) goto fin;
3176 /* Set error handling. REQUIRED if you are not supplying your own
3177 * error handling functions in the png_create_write_struct() call.
3179 if(setjmp(png_jmpbuf(png))) goto fin;
3181 /* I/O initialization functions is REQUIRED
3183 png_init_io(png, writer);
3185 /* Set the image information here. Width and height are up to 2^31,
3186 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3187 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3188 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3189 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3190 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3191 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3194 png_set_compression_level(png, Z_BEST_COMPRESSION);
3196 if(prec == 16) mask = 0xffff;
3198 if(prec == 8) mask = 0x00ff;
3200 if(prec == 4) mask = 0x000f;
3202 if(prec == 2) mask = 0x0003;
3204 if(prec == 1) mask = 0x0001;
3206 nr_comp = image->numcomps;
3209 && image->comps[0].dx == image->comps[1].dx
3210 && image->comps[1].dx == image->comps[2].dx
3211 && image->comps[0].dy == image->comps[1].dy
3212 && image->comps[1].dy == image->comps[2].dy
3213 && image->comps[0].prec == image->comps[1].prec
3214 && image->comps[1].prec == image->comps[2].prec)
3218 has_alpha = (nr_comp > 3);
3220 is16 = (prec == 16);
3222 width = image->comps[0].w;
3223 height = image->comps[0].h;
3225 red = image->comps[0].data;
3226 green = image->comps[1].data;
3227 blue = image->comps[2].data;
3229 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3233 sig_bit.alpha = prec;
3234 alpha = image->comps[3].data;
3235 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3239 sig_bit.alpha = 0; alpha = NULL;
3240 color_type = PNG_COLOR_TYPE_RGB;
3242 png_set_sBIT(png, info, &sig_bit);
3244 png_set_IHDR(png, info, width, height, prec,
3247 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3249 /*=============================*/
3250 png_write_info(png, info);
3251 /*=============================*/
3254 png_set_packing(png);
3258 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3260 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3261 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3262 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3264 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3266 for(y = 0; y < height; ++y)
3270 for(x = 0; x < width; ++x)
3274 v = *red + adjustR; ++red;
3276 if(force16) { v = (v<<ushift) + (v>>dshift); }
3278 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3280 v = *green + adjustG; ++green;
3282 if(force16) { v = (v<<ushift) + (v>>dshift); }
3284 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3286 v = *blue + adjustB; ++blue;
3288 if(force16) { v = (v<<ushift) + (v>>dshift); }
3290 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3296 if(force16) { v = (v<<ushift) + (v>>dshift); }
3298 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3302 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3303 *d++ = (unsigned char)((*green + adjustG) & mask); ++green;
3304 *d++ = (unsigned char)((*blue + adjustB) & mask); ++blue;
3308 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3312 png_write_row(png, row_buf);
3319 if(nr_comp == 1 /* GRAY */
3320 || ( nr_comp == 2 /* GRAY_ALPHA */
3321 && image->comps[0].dx == image->comps[1].dx
3322 && image->comps[0].dy == image->comps[1].dy
3323 && image->comps[0].prec == image->comps[1].prec))
3327 red = image->comps[0].data;
3331 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3333 sig_bit.gray = prec;
3334 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3336 color_type = PNG_COLOR_TYPE_GRAY;
3340 has_alpha = 1; sig_bit.alpha = prec;
3341 alpha = image->comps[1].data;
3342 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3344 width = image->comps[0].w;
3345 height = image->comps[0].h;
3347 png_set_IHDR(png, info, width, height, sig_bit.gray,
3350 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3352 png_set_sBIT(png, info, &sig_bit);
3353 /*=============================*/
3354 png_write_info(png, info);
3355 /*=============================*/
3356 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3360 png_set_packing(png);
3365 row_buf = (unsigned char*)
3366 malloc(width * nr_comp * sizeof(unsigned short));
3368 for(y = 0; y < height; ++y)
3372 for(x = 0; x < width; ++x)
3374 v = *red + adjustR; ++red;
3376 if(force16) { v = (v<<ushift) + (v>>dshift); }
3378 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3384 if(force16) { v = (v<<ushift) + (v>>dshift); }
3386 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3389 png_write_row(png, row_buf);
3394 else /* prec <= 8 */
3396 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3398 for(y = 0; y < height; ++y)
3402 for(x = 0; x < width; ++x)
3404 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3408 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3412 png_write_row(png, row_buf);
3420 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3423 png_write_end(png, info);
3431 png_destroy_write_struct(&png, &info);
3435 if(fails) remove(write_idf);
3439 #endif /* HAVE_LIBPNG */