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 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
108 unsigned int *width, unsigned int *height, int *flip_image)
112 unsigned char id_len, cmap_type, image_type;
113 unsigned char pixel_depth, image_desc;
114 unsigned short cmap_index, cmap_len, cmap_entry_size;
115 unsigned short x_origin, y_origin, image_w, image_h;
117 if (!bits_per_pixel || !width || !height || !flip_image)
119 tga = (unsigned char*)malloc(18);
121 if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 )
123 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
126 id_len = (unsigned char)tga[0];
127 cmap_type = (unsigned char)tga[1];
128 image_type = (unsigned char)tga[2];
129 cmap_index = get_ushort(*(unsigned short*)(&tga[3]));
130 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
131 cmap_entry_size = (unsigned char)tga[7];
134 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
135 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
136 image_w = get_ushort(*(unsigned short*)(&tga[12]));
137 image_h = get_ushort(*(unsigned short*)(&tga[14]));
138 pixel_depth = (unsigned char)tga[16];
139 image_desc = (unsigned char)tga[17];
143 *bits_per_pixel = (unsigned int)pixel_depth;
144 *width = (unsigned int)image_w;
145 *height = (unsigned int)image_h;
147 /* Ignore tga identifier, if present ... */
150 unsigned char *id = (unsigned char *) malloc(id_len);
151 if ( !fread(id, id_len, 1, fp) )
153 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
160 /* Test for compressed formats ... not yet supported ...
161 // Note :- 9 - RLE encoded palettized.
162 // 10 - RLE encoded RGB. */
165 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
169 *flip_image = !(image_desc & 32);
171 /* Palettized formats are not yet supported, skip over the palette, if present ... */
172 palette_size = cmap_len * (cmap_entry_size/8);
176 fprintf(stderr, "File contains a palette - not yet supported.");
177 fseek(fp, palette_size, SEEK_CUR);
182 static 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;
400 fdest = fopen(outfile, "wb");
402 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
406 for (i = 0; i < image->numcomps-1; i++) {
407 if ((image->comps[0].dx != image->comps[i+1].dx)
408 ||(image->comps[0].dy != image->comps[i+1].dy)
409 ||(image->comps[0].prec != image->comps[i+1].prec)) {
410 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
415 width = image->comps[0].w;
416 height = image->comps[0].h;
418 /* Mono with alpha, or RGB with alpha. */
419 write_alpha = (image->numcomps==2) || (image->numcomps==4);
421 /* Write TGA header */
422 bpp = write_alpha ? 32 : 24;
423 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
426 alpha_channel = image->numcomps-1;
428 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
430 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
431 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
432 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
434 for (y=0; y < height; y++) {
435 unsigned int index=y*width;
437 for (x=0; x < width; x++, index++) {
438 r = (float)(image->comps[0].data[index] + adjustR);
440 if (image->numcomps>2) {
441 g = (float)(image->comps[1].data[index] + adjustG);
442 b = (float)(image->comps[2].data[index] + adjustB);
444 else {/* Greyscale ... */
449 /* TGA format writes BGR ... */
450 value = (unsigned char)(b*scale);
451 res = fwrite(&value,1,1,fdest);
453 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
457 value = (unsigned char)(g*scale);
458 res = fwrite(&value,1,1,fdest);
460 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
464 value = (unsigned char)(r*scale);
465 res = fwrite(&value,1,1,fdest);
467 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
472 a = (float)(image->comps[alpha_channel].data[index]);
473 value = (unsigned char)(a*scale);
474 res = fwrite(&value,1,1,fdest);
476 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
486 /* -->> -->> -->> -->>
490 <<-- <<-- <<-- <<-- */
492 /* WORD defines a two byte word */
493 typedef unsigned short int WORD;
495 /* DWORD defines a four byte word */
496 typedef unsigned long int DWORD;
499 WORD bfType; /* 'BM' for Bitmap (19776) */
500 DWORD bfSize; /* Size of the file */
501 WORD bfReserved1; /* Reserved : 0 */
502 WORD bfReserved2; /* Reserved : 0 */
503 DWORD bfOffBits; /* Offset */
504 } BITMAPFILEHEADER_t;
507 DWORD biSize; /* Size of the structure in bytes */
508 DWORD biWidth; /* Width of the image in pixels */
509 DWORD biHeight; /* Heigth of the image in pixels */
510 WORD biPlanes; /* 1 */
511 WORD biBitCount; /* Number of color bits by pixels */
512 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
513 DWORD biSizeImage; /* Size of the image in bytes */
514 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
515 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
516 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
517 DWORD biClrImportant; /* Number of important color (0: ALL) */
518 } BITMAPINFOHEADER_t;
520 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
522 int subsampling_dx = parameters->subsampling_dx;
523 int subsampling_dy = parameters->subsampling_dy;
525 int i, numcomps, w, h;
526 OPJ_COLOR_SPACE color_space;
527 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
528 opj_image_t * image = NULL;
531 BITMAPFILEHEADER_t File_h;
532 BITMAPINFOHEADER_t Info_h;
534 unsigned char *table_R, *table_G, *table_B;
535 unsigned int j, PAD = 0;
542 IN = fopen(filename, "rb");
545 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
549 File_h.bfType = getc(IN);
550 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
552 if (File_h.bfType != 19778)
554 fprintf(stderr,"Error, not a BMP file!\n");
560 File_h.bfSize = getc(IN);
561 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
562 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
563 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
565 File_h.bfReserved1 = getc(IN);
566 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
568 File_h.bfReserved2 = getc(IN);
569 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
571 File_h.bfOffBits = getc(IN);
572 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
573 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
574 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
579 Info_h.biSize = getc(IN);
580 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
581 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
582 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
584 if(Info_h.biSize != 40)
586 fprintf(stderr,"Error, unknown BMP header size %ld\n", Info_h.biSize);
590 Info_h.biWidth = getc(IN);
591 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
592 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
593 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
596 Info_h.biHeight = getc(IN);
597 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
598 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
599 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
602 Info_h.biPlanes = getc(IN);
603 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
605 Info_h.biBitCount = getc(IN);
606 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
608 Info_h.biCompression = getc(IN);
609 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
610 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
611 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
613 Info_h.biSizeImage = getc(IN);
614 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
615 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
616 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
618 Info_h.biXpelsPerMeter = getc(IN);
619 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
620 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
621 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
623 Info_h.biYpelsPerMeter = getc(IN);
624 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
625 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
626 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
628 Info_h.biClrUsed = getc(IN);
629 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
630 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
631 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
633 Info_h.biClrImportant = getc(IN);
634 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
635 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
636 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
638 /* Read the data and store them in the OUT file */
640 if (Info_h.biBitCount == 24)
643 color_space = CLRSPC_SRGB;
644 /* initialize image components */
645 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
646 for(i = 0; i < numcomps; i++)
648 cmptparm[i].prec = 8;
650 cmptparm[i].sgnd = 0;
651 cmptparm[i].dx = subsampling_dx;
652 cmptparm[i].dy = subsampling_dy;
656 /* create the image */
657 image = opj_image_create(numcomps, &cmptparm[0], color_space);
664 /* set image offset and reference grid */
665 image->x0 = parameters->image_offset_x0;
666 image->y0 = parameters->image_offset_y0;
667 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
668 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
672 /* Place the cursor at the beginning of the image information */
673 fseek(IN, 0, SEEK_SET);
674 fseek(IN, File_h.bfOffBits, SEEK_SET);
679 /* PAD = 4 - (3 * W) % 4; */
680 /* PAD = (PAD == 4) ? 0 : PAD; */
681 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
683 RGB = (unsigned char *)
684 malloc((3 * W + PAD) * H * sizeof(unsigned char));
686 if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
689 opj_image_destroy(image);
690 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
696 for(y = 0; y < (int)H; y++)
698 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
699 for(x = 0; x < (int)W; x++)
701 unsigned char *pixel = &scanline[3 * x];
702 image->comps[0].data[index] = pixel[2]; /* R */
703 image->comps[1].data[index] = pixel[1]; /* G */
704 image->comps[2].data[index] = pixel[0]; /* B */
709 }/* if (Info_h.biBitCount == 24) */
711 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
713 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
715 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
717 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
718 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
719 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
722 for (j = 0; j < Info_h.biClrUsed; j++)
724 table_B[j] = (unsigned char)getc(IN);
725 table_G[j] = (unsigned char)getc(IN);
726 table_R[j] = (unsigned char)getc(IN);
729 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
731 if(has_color) gray_scale = 0;
733 /* Place the cursor at the beginning of the image information */
734 fseek(IN, 0, SEEK_SET);
735 fseek(IN, File_h.bfOffBits, SEEK_SET);
739 if (Info_h.biWidth % 2)
742 numcomps = gray_scale ? 1 : 3;
743 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
744 /* initialize image components */
745 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
746 for(i = 0; i < numcomps; i++)
748 cmptparm[i].prec = 8;
750 cmptparm[i].sgnd = 0;
751 cmptparm[i].dx = subsampling_dx;
752 cmptparm[i].dy = subsampling_dy;
756 /* create the image */
757 image = opj_image_create(numcomps, &cmptparm[0], color_space);
761 free(table_R); free(table_G); free(table_B);
765 /* set image offset and reference grid */
766 image->x0 = parameters->image_offset_x0;
767 image->y0 = parameters->image_offset_y0;
768 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
769 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
773 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
775 if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
781 opj_image_destroy(image);
782 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
788 for (j = 0; j < W * H; j++)
790 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
792 image->comps[0].data[index] =
793 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
802 for (j = 0; j < W * H; j++)
804 if ((j % W < W - 1 && Info_h.biWidth % 2)
805 || !(Info_h.biWidth % 2))
807 unsigned char pixel_index =
808 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
809 image->comps[0].data[index] = table_R[pixel_index];
810 image->comps[1].data[index] = table_G[pixel_index];
811 image->comps[2].data[index] = table_B[pixel_index];
822 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
824 unsigned char *pix, *beyond;
825 int *gray, *red, *green, *blue;
826 unsigned int x, y, max;
830 if (Info_h.biClrUsed == 0)
831 Info_h.biClrUsed = 256;
832 else if (Info_h.biClrUsed > 256)
833 Info_h.biClrUsed = 256;
835 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
836 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
837 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
840 for (j = 0; j < Info_h.biClrUsed; j++)
842 table_B[j] = (unsigned char)getc(IN);
843 table_G[j] = (unsigned char)getc(IN);
844 table_R[j] = (unsigned char)getc(IN);
846 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
852 numcomps = gray_scale ? 1 : 3;
853 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
854 /* initialize image components */
855 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
856 for (i = 0; i < numcomps; i++)
858 cmptparm[i].prec = 8;
860 cmptparm[i].sgnd = 0;
861 cmptparm[i].dx = subsampling_dx;
862 cmptparm[i].dy = subsampling_dy;
866 /* create the image */
867 image = opj_image_create(numcomps, &cmptparm[0], color_space);
877 /* set image offset and reference grid */
878 image->x0 = parameters->image_offset_x0;
879 image->y0 = parameters->image_offset_y0;
880 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
881 - 1) * subsampling_dx + 1;
882 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
883 - 1) * subsampling_dy + 1;
887 /* Place the cursor at the beginning of the image information */
888 fseek(IN, 0, SEEK_SET);
889 fseek(IN, File_h.bfOffBits, SEEK_SET);
893 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
894 beyond = RGB + W * H;
906 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
907 *pix = (unsigned char)c1;
913 if (c == 0x00) /* EOL */
917 pix = RGB + x + (H - y - 1) * W;
919 else if (c == 0x01) /* EOP */
921 else if (c == 0x02) /* MOVE by dxdy */
927 pix = RGB + (H - y - 1) * W + x;
932 for (; i < c && x < W && pix < beyond; i++, x++, pix++)
935 *pix = (unsigned char)c1;
937 if (c & 1) /* skip padding byte */
945 gray = image->comps[0].data;
953 *gray++ = table_R[uc];
958 /*int *red, *green, *blue;*/
960 red = image->comps[0].data;
961 green = image->comps[1].data;
962 blue = image->comps[2].data;
970 *red++ = table_R[uc];
971 *green++ = table_G[uc];
972 *blue++ = table_B[uc];
983 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
984 "is not yet implemented [%d]\n", Info_h.biBitCount);
990 int imagetobmp(opj_image_t * image, const char *outfile) {
994 int adjustR, adjustG, adjustB;
996 if (image->comps[0].prec < 8) {
997 fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec);
1000 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1001 && image->comps[1].dx == image->comps[2].dx
1002 && image->comps[0].dy == image->comps[1].dy
1003 && image->comps[1].dy == image->comps[2].dy
1004 && image->comps[0].prec == image->comps[1].prec
1005 && image->comps[1].prec == image->comps[2].prec) {
1007 /* -->> -->> -->> -->>
1009 <<-- <<-- <<-- <<-- */
1011 fdest = fopen(outfile, "wb");
1013 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1017 w = image->comps[0].w;
1018 h = image->comps[0].h;
1020 fprintf(fdest, "BM");
1024 fprintf(fdest, "%c%c%c%c",
1025 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1026 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1027 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1028 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1029 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1030 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
1034 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1035 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1036 (unsigned char) ((w) >> 8) & 0xff,
1037 (unsigned char) ((w) >> 16) & 0xff,
1038 (unsigned char) ((w) >> 24) & 0xff);
1039 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1040 (unsigned char) ((h) >> 8) & 0xff,
1041 (unsigned char) ((h) >> 16) & 0xff,
1042 (unsigned char) ((h) >> 24) & 0xff);
1043 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1044 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1045 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1046 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
1047 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1048 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1049 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1050 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1051 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1052 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1053 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1055 if (image->comps[0].prec > 8) {
1056 adjustR = image->comps[0].prec - 8;
1057 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1061 if (image->comps[1].prec > 8) {
1062 adjustG = image->comps[1].prec - 8;
1063 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1067 if (image->comps[2].prec > 8) {
1068 adjustB = image->comps[2].prec - 8;
1069 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1074 for (i = 0; i < w * h; i++) {
1075 unsigned char rc, gc, bc;
1078 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1079 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1080 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1081 if(r > 255) r = 255; else if(r < 0) r = 0;
1082 rc = (unsigned char)r;
1084 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1085 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1086 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
1087 if(g > 255) g = 255; else if(g < 0) g = 0;
1088 gc = (unsigned char)g;
1090 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1091 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1092 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
1093 if(b > 255) b = 255; else if(b < 0) b = 0;
1094 bc = (unsigned char)b;
1096 fprintf(fdest, "%c%c%c", bc, gc, rc);
1098 if ((i + 1) % w == 0) {
1099 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
1100 fprintf(fdest, "%c", 0);
1104 } else { /* Gray-scale */
1106 /* -->> -->> -->> -->>
1107 8 bits non code (Gray scale)
1108 <<-- <<-- <<-- <<-- */
1110 fdest = fopen(outfile, "wb");
1111 w = image->comps[0].w;
1112 h = image->comps[0].h;
1114 fprintf(fdest, "BM");
1118 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1119 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1120 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1121 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1122 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1123 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1124 ((54 + 1024) >> 16) & 0xff,
1125 ((54 + 1024) >> 24) & 0xff);
1129 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1130 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1131 (unsigned char) ((w) >> 8) & 0xff,
1132 (unsigned char) ((w) >> 16) & 0xff,
1133 (unsigned char) ((w) >> 24) & 0xff);
1134 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1135 (unsigned char) ((h) >> 8) & 0xff,
1136 (unsigned char) ((h) >> 16) & 0xff,
1137 (unsigned char) ((h) >> 24) & 0xff);
1138 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1139 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1140 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1141 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
1142 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
1143 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
1144 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
1145 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1146 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1147 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1148 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1150 if (image->comps[0].prec > 8) {
1151 adjustR = image->comps[0].prec - 8;
1152 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1156 for (i = 0; i < 256; i++) {
1157 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1160 for (i = 0; i < w * h; i++) {
1163 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1164 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1165 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1166 if(r > 255) r = 255; else if(r < 0) r = 0;
1168 fprintf(fdest, "%c", (unsigned char)r);
1170 if ((i + 1) % w == 0) {
1171 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1172 fprintf(fdest, "%c", 0);
1181 /* -->> -->> -->> -->>
1185 <<-- <<-- <<-- <<-- */
1188 static unsigned char readuchar(FILE * f)
1191 if ( !fread(&c1, 1, 1, f) )
1193 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1199 static unsigned short readushort(FILE * f, int bigendian)
1201 unsigned char c1, c2;
1202 if ( !fread(&c1, 1, 1, f) )
1204 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1207 if ( !fread(&c2, 1, 1, f) )
1209 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1213 return (c1 << 8) + c2;
1215 return (c2 << 8) + c1;
1218 static unsigned int readuint(FILE * f, int bigendian)
1220 unsigned char c1, c2, c3, c4;
1221 if ( !fread(&c1, 1, 1, f) )
1223 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1226 if ( !fread(&c2, 1, 1, f) )
1228 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1231 if ( !fread(&c3, 1, 1, f) )
1233 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1236 if ( !fread(&c4, 1, 1, f) )
1238 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1242 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1244 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1247 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1250 int i, numcomps, max;
1251 OPJ_COLOR_SPACE color_space;
1252 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1253 opj_image_t * image = NULL;
1255 char endian1,endian2,sign;
1260 opj_image_comp_t *comp = NULL;
1263 color_space = CLRSPC_GRAY;
1265 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1269 f = fopen(filename, "rb");
1271 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1275 fseek(f, 0, SEEK_SET);
1276 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1277 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1283 while (signtmp[i]!='\0') {
1284 if (signtmp[i]=='-') sign='-';
1289 if (endian1=='M' && endian2=='L') {
1291 } else if (endian2=='M' && endian1=='L') {
1294 fprintf(stderr, "Bad pgx header, please check input file\n");
1298 /* initialize image component */
1300 cmptparm.x0 = parameters->image_offset_x0;
1301 cmptparm.y0 = parameters->image_offset_y0;
1302 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1303 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1310 cmptparm.prec = prec;
1311 cmptparm.bpp = prec;
1312 cmptparm.dx = parameters->subsampling_dx;
1313 cmptparm.dy = parameters->subsampling_dy;
1315 /* create the image */
1316 image = opj_image_create(numcomps, &cmptparm, color_space);
1321 /* set image offset and reference grid */
1322 image->x0 = cmptparm.x0;
1323 image->y0 = cmptparm.x0;
1324 image->x1 = cmptparm.w;
1325 image->y1 = cmptparm.h;
1327 /* set image data */
1329 comp = &image->comps[0];
1331 for (i = 0; i < w * h; i++) {
1333 if (comp->prec <= 8) {
1337 v = (char) readuchar(f);
1339 } else if (comp->prec <= 16) {
1341 v = readushort(f, bigendian);
1343 v = (short) readushort(f, bigendian);
1347 v = readuint(f, bigendian);
1349 v = (int) readuint(f, bigendian);
1357 comp->bpp = int_floorlog2(max) + 1;
1362 int imagetopgx(opj_image_t * image, const char *outfile) {
1367 for (compno = 0; compno < image->numcomps; compno++) {
1368 opj_image_comp_t *comp = &image->comps[compno];
1369 char bname[256]; /* buffer for name */
1370 char *name = bname; /* pointer */
1373 const size_t olen = strlen(outfile);
1374 const size_t dotpos = olen - 4;
1375 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1376 if( outfile[dotpos] != '.' ) {
1377 /* `pgx` was recognized but there is no dot at expected position */
1378 fprintf(stderr, "ERROR -> Impossible happen." );
1382 name = (char*)malloc(total+1);
1384 strncpy(name, outfile, dotpos);
1385 /*if (image->numcomps > 1) {*/
1386 sprintf(name+dotpos, "_%d.pgx", compno);
1388 strcpy(name+dotpos, ".pgx");
1390 fdest = fopen(name, "wb");
1392 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1395 /* dont need name anymore */
1400 w = image->comps[compno].w;
1401 h = image->comps[compno].h;
1403 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1404 if (comp->prec <= 8) {
1406 } else if (comp->prec <= 16) {
1411 for (i = 0; i < w * h; i++) {
1412 int v = image->comps[compno].data[i];
1413 for (j = nbytes - 1; j >= 0; j--) {
1414 char byte = (char) (v >> (j * 8));
1415 res = fwrite(&byte, 1, 1, fdest);
1417 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1428 /* -->> -->> -->> -->>
1432 <<-- <<-- <<-- <<-- */
1436 int width, height, maxval, depth, format;
1437 char rgb, rgba, gray, graya, bw;
1441 static char *skip_white(char *s)
1445 if(*s == '\n' || *s == '\r') return NULL;
1446 if(isspace(*s)) { ++s; continue; }
1452 static char *skip_int(char *start, int *out_n)
1457 *out_n = 0; s = start;
1459 s = skip_white(start);
1460 if(s == NULL) return NULL;
1465 if( !isdigit(*s)) break;
1468 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1472 static char *skip_idf(char *start, char out_idf[256])
1477 s = skip_white(start);
1478 if(s == NULL) return NULL;
1483 if(isalpha(*s) || *s == '_') { ++s; continue; }
1486 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1490 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1493 int format, have_wh, end, ttype;
1494 char idf[256], type[256];
1497 if (fgets(line, 250, reader) == NULL)
1499 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1505 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1507 format = atoi(line + 1);
1508 if(format < 1 || format > 7)
1510 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1513 ph->format = format;
1514 ttype = end = have_wh = 0;
1516 while(fgets(line, 250, reader))
1518 if(*line == '#') continue;
1524 s = skip_idf(s, idf);
1526 if(s == NULL || *s == 0) return;
1528 if(strcmp(idf, "ENDHDR") == 0)
1532 if(strcmp(idf, "WIDTH") == 0)
1534 s = skip_int(s, &ph->width);
1535 if(s == NULL || *s == 0) return;
1539 if(strcmp(idf, "HEIGHT") == 0)
1541 s = skip_int(s, &ph->height);
1542 if(s == NULL || *s == 0) return;
1546 if(strcmp(idf, "DEPTH") == 0)
1548 s = skip_int(s, &ph->depth);
1549 if(s == NULL || *s == 0) return;
1553 if(strcmp(idf, "MAXVAL") == 0)
1555 s = skip_int(s, &ph->maxval);
1556 if(s == NULL || *s == 0) return;
1560 if(strcmp(idf, "TUPLTYPE") == 0)
1562 s = skip_idf(s, type);
1563 if(s == NULL || *s == 0) return;
1565 if(strcmp(type, "BLACKANDWHITE") == 0)
1567 ph->bw = 1; ttype = 1; continue;
1569 if(strcmp(type, "GRAYSCALE") == 0)
1571 ph->gray = 1; ttype = 1; continue;
1573 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1575 ph->graya = 1; ttype = 1; continue;
1577 if(strcmp(type, "RGB") == 0)
1579 ph->rgb = 1; ttype = 1; continue;
1581 if(strcmp(type, "RGB_ALPHA") == 0)
1583 ph->rgba = 1; ttype = 1; continue;
1585 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1588 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1590 } /* if(format == 7) */
1594 s = skip_int(s, &ph->width);
1596 s = skip_int(s, &ph->height);
1600 if(format == 1 || format == 4) break;
1604 if(format == 2 || format == 3 || format == 5 || format == 6)
1606 /* P2, P3, P5, P6: */
1607 s = skip_int(s, &ph->maxval);
1609 if(ph->maxval > 65535) return;
1612 }/* while(fgets( ) */
1613 if(format == 2 || format == 3 || format > 4)
1615 if(ph->maxval < 1 || ph->maxval > 65535) return;
1617 if(ph->width < 1 || ph->height < 1) return;
1623 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1625 if(ph->depth < 1 || ph->depth > 4) return;
1627 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1632 if(format != 1 && format != 4)
1634 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1638 if(ph->width && ph->height) ph->ok = 1;
1644 static int has_prec(int val)
1646 if(val < 2) return 1;
1647 if(val < 4) return 2;
1648 if(val < 8) return 3;
1649 if(val < 16) return 4;
1650 if(val < 32) return 5;
1651 if(val < 64) return 6;
1652 if(val < 128) return 7;
1653 if(val < 256) return 8;
1654 if(val < 512) return 9;
1655 if(val < 1024) return 10;
1656 if(val < 2048) return 11;
1657 if(val < 4096) return 12;
1658 if(val < 8192) return 13;
1659 if(val < 16384) return 14;
1660 if(val < 32768) return 15;
1664 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1665 int subsampling_dx = parameters->subsampling_dx;
1666 int subsampling_dy = parameters->subsampling_dy;
1669 int i, compno, numcomps, w, h, prec, format;
1670 OPJ_COLOR_SPACE color_space;
1671 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1672 opj_image_t * image = NULL;
1673 struct pnm_header header_info;
1675 if((fp = fopen(filename, "rb")) == NULL)
1677 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1680 memset(&header_info, 0, sizeof(struct pnm_header));
1682 read_pnm_header(fp, &header_info);
1684 if(!header_info.ok) { fclose(fp); return NULL; }
1686 format = header_info.format;
1690 case 1: /* ascii bitmap */
1691 case 4: /* raw bitmap */
1695 case 2: /* ascii greymap */
1696 case 5: /* raw greymap */
1700 case 3: /* ascii pixmap */
1701 case 6: /* raw pixmap */
1705 case 7: /* arbitrary map */
1706 numcomps = header_info.depth;
1709 default: fclose(fp); return NULL;
1712 color_space = CLRSPC_GRAY;/* GRAY, GRAYA */
1714 color_space = CLRSPC_SRGB;/* RGB, RGBA */
1716 prec = has_prec(header_info.maxval);
1718 if(prec < 8) prec = 8;
1720 w = header_info.width;
1721 h = header_info.height;
1722 subsampling_dx = parameters->subsampling_dx;
1723 subsampling_dy = parameters->subsampling_dy;
1725 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1727 for(i = 0; i < numcomps; i++)
1729 cmptparm[i].prec = prec;
1730 cmptparm[i].bpp = prec;
1731 cmptparm[i].sgnd = 0;
1732 cmptparm[i].dx = subsampling_dx;
1733 cmptparm[i].dy = subsampling_dy;
1737 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1739 if(!image) { fclose(fp); return NULL; }
1741 /* set image offset and reference grid */
1742 image->x0 = parameters->image_offset_x0;
1743 image->y0 = parameters->image_offset_y0;
1744 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1745 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1747 if((format == 2) || (format == 3)) /* ascii pixmap */
1751 for (i = 0; i < w * h; i++)
1753 for(compno = 0; compno < numcomps; compno++)
1756 if (fscanf(fp, "%u", &index) != 1)
1757 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1759 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1767 && ( header_info.gray || header_info.graya
1768 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1770 unsigned char c0, c1, one;
1774 for (i = 0; i < w * h; i++)
1776 for(compno = 0; compno < numcomps; compno++)
1778 if ( !fread(&c0, 1, 1, fp) )
1779 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1782 image->comps[compno].data[i] = c0;
1786 if ( !fread(&c1, 1, 1, fp) )
1787 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1789 image->comps[compno].data[i] = ((c0<<8) | c1);
1795 if(format == 1) /* ascii bitmap */
1797 for (i = 0; i < w * h; i++)
1801 if ( fscanf(fp, "%u", &index) != 1)
1802 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1804 image->comps[0].data[i] = (index?0:255);
1814 for(y = 0; y < h; ++y)
1818 for(x = 0; x < w; ++x)
1823 uc = (unsigned char)getc(fp);
1825 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1831 if((format == 7 && header_info.bw)) /*MONO*/
1835 for(i = 0; i < w * h; ++i)
1837 if ( !fread(&uc, 1, 1, fp) )
1838 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1839 image->comps[0].data[i] = (uc & 1)?0:255;
1847 int imagetopnm(opj_image_t * image, const char *outfile)
1849 int *red, *green, *blue, *alpha;
1851 int i, compno, ncomp;
1852 int adjustR, adjustG, adjustB, adjustA;
1853 int fails, two, want_gray, has_alpha, triple;
1856 const char *tmp = outfile;
1859 if((prec = image->comps[0].prec) > 16)
1861 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1862 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1865 two = has_alpha = 0; fails = 1;
1866 ncomp = image->numcomps;
1868 while (*tmp) ++tmp; tmp -= 2;
1869 want_gray = (*tmp == 'g' || *tmp == 'G');
1870 ncomp = image->numcomps;
1872 if(want_gray) ncomp = 1;
1874 if (ncomp == 2 /* GRAYA */
1875 || (ncomp > 2 /* RGB, RGBA */
1876 && image->comps[0].dx == image->comps[1].dx
1877 && image->comps[1].dx == image->comps[2].dx
1878 && image->comps[0].dy == image->comps[1].dy
1879 && image->comps[1].dy == image->comps[2].dy
1880 && image->comps[0].prec == image->comps[1].prec
1881 && image->comps[1].prec == image->comps[2].prec
1884 fdest = fopen(outfile, "wb");
1888 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1892 triple = (ncomp > 2);
1893 wr = image->comps[0].w; hr = image->comps[0].h;
1894 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1896 red = image->comps[0].data;
1900 green = image->comps[1].data;
1901 blue = image->comps[2].data;
1903 else green = blue = NULL;
1907 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1909 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1910 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1911 wr, hr, ncomp, max, tt);
1912 alpha = image->comps[ncomp - 1].data;
1913 adjustA = (image->comps[ncomp - 1].sgnd ?
1914 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1918 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1919 opj_version(), wr, hr, max);
1922 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1926 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1927 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1929 else adjustG = adjustB = 0;
1931 for(i = 0; i < wr * hr; ++i)
1935 v = *red + adjustR; ++red;
1937 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1941 v = *green + adjustG; ++green;
1943 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1945 v = *blue + adjustB; ++blue;
1947 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1953 v = *alpha + adjustA; ++alpha;
1955 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1963 fprintf(fdest, "%c", (unsigned char)*red++);
1965 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
1968 fprintf(fdest, "%c", (unsigned char)*alpha++);
1972 fclose(fdest); return 0;
1977 if (image->numcomps > ncomp)
1979 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1980 fprintf(stderr," is written to the file\n");
1982 destname = (char*)malloc(strlen(outfile) + 8);
1984 for (compno = 0; compno < ncomp; compno++)
1987 sprintf(destname, "%d.%s", compno, outfile);
1989 sprintf(destname, "%s", outfile);
1991 fdest = fopen(destname, "wb");
1994 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1998 wr = image->comps[compno].w; hr = image->comps[compno].h;
1999 prec = image->comps[compno].prec;
2000 max = (1<<prec) - 1;
2002 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2003 opj_version(), wr, hr, max);
2005 red = image->comps[compno].data;
2007 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2011 for (i = 0; i < wr * hr; i++)
2013 v = *red + adjustR; ++red;
2015 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2021 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2025 else /* prec <= 8 */
2027 for(i = 0; i < wr * hr; ++i)
2029 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
2040 /* -->> -->> -->> -->>
2044 <<-- <<-- <<-- <<-- */
2046 typedef struct tiff_infoheader{
2047 DWORD tiWidth; /* Width of Image in pixel*/
2048 DWORD tiHeight; /* Height of Image in pixel */
2049 DWORD tiPhoto; /* Photometric */
2050 WORD tiBps; /* Bits per sample */
2051 WORD tiSf; /* Sample Format */
2052 WORD tiSpp; /* Sample per pixel 1-bilevel,gray scale , 2- RGB */
2053 WORD tiPC; /* Planar config (1-Interleaved, 2-Planarcomp) */
2056 int imagetotif(opj_image_t * image, const char *outfile)
2058 int width, height, imgsize;
2059 int bps,index,adjust, sgnd;
2060 int ushift, dshift, has_alpha, force16;
2066 ushift = dshift = force16 = has_alpha = 0;
2067 bps = image->comps[0].prec;
2069 if(bps > 8 && bps < 16)
2071 ushift = 16 - bps; dshift = bps - ushift;
2072 bps = 16; force16 = 1;
2075 if(bps != 8 && bps != 16)
2077 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2079 fprintf(stderr,"\tAborting\n");
2082 tif = TIFFOpen(outfile, "wb");
2086 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2089 sgnd = image->comps[0].sgnd;
2090 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2092 if(image->numcomps >= 3
2093 && image->comps[0].dx == image->comps[1].dx
2094 && image->comps[1].dx == image->comps[2].dx
2095 && image->comps[0].dy == image->comps[1].dy
2096 && image->comps[1].dy == image->comps[2].dy
2097 && image->comps[0].prec == image->comps[1].prec
2098 && image->comps[1].prec == image->comps[2].prec)
2100 has_alpha = (image->numcomps == 4);
2102 width = image->comps[0].w;
2103 height = image->comps[0].h;
2104 imgsize = width * height ;
2106 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2107 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2108 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2109 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2110 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2111 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2112 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2113 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2114 strip_size = TIFFStripSize(tif);
2115 buf = _TIFFmalloc(strip_size);
2118 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2120 unsigned char *dat8;
2121 tsize_t i, ssize, last_i = 0;
2123 ssize = TIFFStripSize(tif);
2124 dat8 = (unsigned char*)buf;
2128 step = 3 + has_alpha;
2131 for(i=0; i < ssize - restx; i += step)
2137 r = image->comps[0].data[index];
2138 g = image->comps[1].data[index];
2139 b = image->comps[2].data[index];
2140 if(has_alpha) a = image->comps[3].data[index];
2147 if(has_alpha) a += adjust;
2152 if(has_alpha) dat8[i+3] = a;
2163 for(i = last_i; i < ssize; i += step)
2169 r = image->comps[0].data[index];
2170 g = image->comps[1].data[index];
2171 b = image->comps[2].data[index];
2172 if(has_alpha) a = image->comps[3].data[index];
2179 if(has_alpha) a += adjust;
2182 if(i+1 < ssize) dat8[i+1] = g ; else break;
2183 if(i+2 < ssize) dat8[i+2] = b ; else break;
2186 if(i+3 < ssize) dat8[i+3] = a ; else break;
2193 }/*if(last_i < ssize)*/
2199 step = 6 + has_alpha + has_alpha;
2202 for(i = 0; i < ssize - restx ; i += step)
2208 r = image->comps[0].data[index];
2209 g = image->comps[1].data[index];
2210 b = image->comps[2].data[index];
2211 if(has_alpha) a = image->comps[3].data[index];
2218 if(has_alpha) a += adjust;
2222 r = (r<<ushift) + (r>>dshift);
2223 g = (g<<ushift) + (g>>dshift);
2224 b = (b<<ushift) + (b>>dshift);
2225 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2227 dat8[i+0] = r;/*LSB*/
2228 dat8[i+1] = (r >> 8);/*MSB*/
2230 dat8[i+3] = (g >> 8);
2232 dat8[i+5] = (b >> 8);
2236 dat8[i+7] = (a >> 8);
2247 for(i = last_i ; i < ssize ; i += step)
2253 r = image->comps[0].data[index];
2254 g = image->comps[1].data[index];
2255 b = image->comps[2].data[index];
2256 if(has_alpha) a = image->comps[3].data[index];
2263 if(has_alpha) a += adjust;
2267 r = (r<<ushift) + (r>>dshift);
2268 g = (g<<ushift) + (g>>dshift);
2269 b = (b<<ushift) + (b>>dshift);
2270 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2272 dat8[i+0] = r;/*LSB*/
2273 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/
2274 if(i+2 < ssize) dat8[i+2] = g; else break;
2275 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2276 if(i+4 < ssize) dat8[i+4] = b; else break;
2277 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2281 if(i+6 < ssize) dat8[i+6] = a; else break;
2282 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2289 }/*if(last_i < ssize)*/
2292 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2293 }/*for(strip = 0; )*/
2295 _TIFFfree((void*)buf);
2301 if(image->numcomps == 1 /* GRAY */
2302 || ( image->numcomps == 2 /* GRAY_ALPHA */
2303 && image->comps[0].dx == image->comps[1].dx
2304 && image->comps[0].dy == image->comps[1].dy
2305 && image->comps[0].prec == image->comps[1].prec))
2309 has_alpha = (image->numcomps == 2);
2311 width = image->comps[0].w;
2312 height = image->comps[0].h;
2313 imgsize = width * height;
2316 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2317 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2318 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2319 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2320 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2321 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2322 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2323 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2325 /* Get a buffer for the data */
2326 strip_size = TIFFStripSize(tif);
2327 buf = _TIFFmalloc(strip_size);
2330 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2332 unsigned char *dat8;
2333 tsize_t i, ssize = TIFFStripSize(tif);
2334 dat8 = (unsigned char*)buf;
2338 step = 1 + has_alpha;
2340 for(i=0; i < ssize; i += step)
2346 r = image->comps[0].data[index];
2347 if(has_alpha) a = image->comps[1].data[index];
2352 if(has_alpha) a += adjust;
2355 if(has_alpha) dat8[i+1] = a;
2365 step = 2 + has_alpha + has_alpha;
2367 for(i=0; i < ssize; i += step)
2373 r = image->comps[0].data[index];
2374 if(has_alpha) a = image->comps[1].data[index];
2379 if(has_alpha) a += adjust;
2383 r = (r<<ushift) + (r>>dshift);
2384 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2386 dat8[i+0] = r;/*LSB*/
2387 dat8[i+1] = r >> 8;/*MSB*/
2394 }/*if(index < imgsize)*/
2399 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2410 fprintf(stderr,"imagetotif: Bad color format.\n"
2411 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2412 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2419 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2420 * CINEMA : 12 bit precision
2422 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2424 int subsampling_dx = parameters->subsampling_dx;
2425 int subsampling_dy = parameters->subsampling_dy;
2427 tiff_infoheader_t Info;
2431 int j, numcomps, w, h,index;
2432 OPJ_COLOR_SPACE color_space;
2433 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2434 opj_image_t *image = NULL;
2438 tif = TIFFOpen(filename, "r");
2442 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2445 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
2446 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
2447 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
2448 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
2449 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
2451 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
2452 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
2459 if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
2460 if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
2465 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2466 " implemented\n",Info.tiBps);
2469 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2470 " and GRAY(A) has been implemented\n",(int) Info.tiPhoto);
2472 fprintf(stderr,"\tAborting\n");
2479 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2481 uint16 extrasamples;
2483 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2484 &extrasamples, &sampleinfo);
2486 if(extrasamples >= 1)
2488 switch(sampleinfo[0])
2490 case EXTRASAMPLE_UNSPECIFIED:
2491 /* Workaround for some images without correct info about alpha channel
2497 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2498 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2504 /* initialize image components
2506 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2508 if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2510 numcomps = 3 + has_alpha;
2511 color_space = CLRSPC_SRGB;
2513 for(j = 0; j < numcomps; j++)
2515 if(parameters->cp_cinema)
2517 cmptparm[j].prec = 12;
2518 cmptparm[j].bpp = 12;
2522 cmptparm[j].prec = Info.tiBps;
2523 cmptparm[j].bpp = Info.tiBps;
2525 cmptparm[j].dx = subsampling_dx;
2526 cmptparm[j].dy = subsampling_dy;
2531 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2538 /* set image offset and reference grid
2540 image->x0 = parameters->image_offset_x0;
2541 image->y0 = parameters->image_offset_y0;
2542 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2543 image->x0 + (w - 1) * subsampling_dx + 1;
2544 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2545 image->y0 + (h - 1) * subsampling_dy + 1;
2547 buf = _TIFFmalloc(TIFFStripSize(tif));
2549 strip_size=TIFFStripSize(tif);
2551 imgsize = image->comps[0].w * image->comps[0].h ;
2552 /* Read the Image components
2554 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2556 unsigned char *dat8;
2559 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2560 dat8 = (unsigned char*)buf;
2562 if(Info.tiBps == 16)
2564 step = 6 + has_alpha + has_alpha;
2566 for(i = 0; i < ssize; i += step)
2570 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */
2571 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
2572 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
2574 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2576 if(parameters->cp_cinema)
2578 /* Rounding 16 to 12 bits
2580 image->comps[0].data[index] =
2581 (image->comps[0].data[index] + 0x08) >> 4 ;
2582 image->comps[1].data[index] =
2583 (image->comps[1].data[index] + 0x08) >> 4 ;
2584 image->comps[2].data[index] =
2585 (image->comps[2].data[index] + 0x08) >> 4 ;
2587 image->comps[3].data[index] =
2588 (image->comps[3].data[index] + 0x08) >> 4 ;
2595 }/*if(Info.tiBps == 16)*/
2599 step = 3 + has_alpha;
2601 for(i = 0; i < ssize; i += step)
2605 image->comps[0].data[index] = dat8[i+0];/* R */
2606 image->comps[1].data[index] = dat8[i+1];/* G */
2607 image->comps[2].data[index] = dat8[i+2];/* B */
2609 image->comps[3].data[index] = dat8[i+3];
2611 if(parameters->cp_cinema)
2613 /* Rounding 8 to 12 bits
2615 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2616 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2617 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2619 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2626 }/*if( Info.tiBps == 8)*/
2628 if(Info.tiBps == 12)/* CINEMA file */
2632 for(i = 0; i < ssize; i += step)
2634 if((index < imgsize)&(index+1 < imgsize))
2636 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2637 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2639 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2640 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2642 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2643 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2651 }/*for(strip = 0; )*/
2659 if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2661 numcomps = 1 + has_alpha;
2662 color_space = CLRSPC_GRAY;
2664 for(j = 0; j < numcomps; ++j)
2666 cmptparm[j].prec = Info.tiBps;
2667 cmptparm[j].bpp = Info.tiBps;
2668 cmptparm[j].dx = subsampling_dx;
2669 cmptparm[j].dy = subsampling_dy;
2673 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2680 /* set image offset and reference grid
2682 image->x0 = parameters->image_offset_x0;
2683 image->y0 = parameters->image_offset_y0;
2684 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2685 image->x0 + (w - 1) * subsampling_dx + 1;
2686 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2687 image->y0 + (h - 1) * subsampling_dy + 1;
2689 buf = _TIFFmalloc(TIFFStripSize(tif));
2691 strip_size = TIFFStripSize(tif);
2693 imgsize = image->comps[0].w * image->comps[0].h ;
2694 /* Read the Image components
2696 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2698 unsigned char *dat8;
2702 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2703 dat8 = (unsigned char*)buf;
2705 if(Info.tiBps == 16)
2707 step = 2 + has_alpha + has_alpha;
2709 for(i = 0; i < ssize; i += step)
2713 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2715 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2725 step = 1 + has_alpha;
2727 for(i = 0; i < ssize; i += step)
2731 image->comps[0].data[index] = dat8[i+0];
2733 image->comps[1].data[index] = dat8[i+1];
2751 #endif /* HAVE_LIBTIFF */
2753 /* -->> -->> -->> -->>
2757 <<-- <<-- <<-- <<-- */
2759 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2760 int subsampling_dx = parameters->subsampling_dx;
2761 int subsampling_dy = parameters->subsampling_dy;
2764 int i, compno, numcomps, w, h;
2765 OPJ_COLOR_SPACE color_space;
2766 opj_image_cmptparm_t *cmptparm;
2767 opj_image_t * image = NULL;
2770 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2772 fprintf(stderr,"\nError: invalid raw image parameters\n");
2773 fprintf(stderr,"Please use the Format option -F:\n");
2774 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2775 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2776 fprintf(stderr,"Aborting\n");
2780 f = fopen(filename, "rb");
2782 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2783 fprintf(stderr,"Aborting\n");
2786 numcomps = raw_cp->rawComp;
2787 color_space = CLRSPC_SRGB;
2788 w = raw_cp->rawWidth;
2789 h = raw_cp->rawHeight;
2790 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2792 /* initialize image components */
2793 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2794 for(i = 0; i < numcomps; i++) {
2795 cmptparm[i].prec = raw_cp->rawBitDepth;
2796 cmptparm[i].bpp = raw_cp->rawBitDepth;
2797 cmptparm[i].sgnd = raw_cp->rawSigned;
2798 cmptparm[i].dx = subsampling_dx;
2799 cmptparm[i].dy = subsampling_dy;
2803 /* create the image */
2804 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2809 /* set image offset and reference grid */
2810 image->x0 = parameters->image_offset_x0;
2811 image->y0 = parameters->image_offset_y0;
2812 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2813 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2815 if(raw_cp->rawBitDepth <= 8)
2817 unsigned char value = 0;
2818 for(compno = 0; compno < numcomps; compno++) {
2819 for (i = 0; i < w * h; i++) {
2820 if (!fread(&value, 1, 1, f)) {
2821 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2824 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2828 else if(raw_cp->rawBitDepth <= 16)
2830 unsigned short value;
2831 for(compno = 0; compno < numcomps; compno++) {
2832 for (i = 0; i < w * h; i++) {
2834 if (!fread(&temp, 1, 1, f)) {
2835 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2839 if (!fread(&temp, 1, 1, f)) {
2840 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2844 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2849 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2853 if (fread(&ch, 1, 1, f)) {
2854 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2861 int imagetoraw(opj_image_t * image, const char *outfile)
2863 FILE *rawFile = NULL;
2870 if((image->numcomps * image->x1 * image->y1) == 0)
2872 fprintf(stderr,"\nError: invalid raw image parameters\n");
2876 rawFile = fopen(outfile, "wb");
2878 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2882 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2884 for(compno = 0; compno < image->numcomps; compno++)
2886 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2887 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2889 w = image->comps[compno].w;
2890 h = image->comps[compno].h;
2892 if(image->comps[compno].prec <= 8)
2894 if(image->comps[compno].sgnd == 1)
2897 int mask = (1 << image->comps[compno].prec) - 1;
2898 ptr = image->comps[compno].data;
2899 for (line = 0; line < h; line++) {
2900 for(row = 0; row < w; row++) {
2901 curr = (signed char) (*ptr & mask);
2902 res = fwrite(&curr, sizeof(signed char), 1, rawFile);
2904 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2911 else if(image->comps[compno].sgnd == 0)
2914 int mask = (1 << image->comps[compno].prec) - 1;
2915 ptr = image->comps[compno].data;
2916 for (line = 0; line < h; line++) {
2917 for(row = 0; row < w; row++) {
2918 curr = (unsigned char) (*ptr & mask);
2919 res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2921 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2929 else if(image->comps[compno].prec <= 16)
2931 if(image->comps[compno].sgnd == 1)
2933 signed short int curr;
2934 int mask = (1 << image->comps[compno].prec) - 1;
2935 ptr = image->comps[compno].data;
2936 for (line = 0; line < h; line++) {
2937 for(row = 0; row < w; row++) {
2939 curr = (signed short int) (*ptr & mask);
2940 temp = (unsigned char) (curr >> 8);
2941 res = fwrite(&temp, 1, 1, rawFile);
2943 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2946 temp = (unsigned char) curr;
2947 res = fwrite(&temp, 1, 1, rawFile);
2949 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2956 else if(image->comps[compno].sgnd == 0)
2958 unsigned short int curr;
2959 int mask = (1 << image->comps[compno].prec) - 1;
2960 ptr = image->comps[compno].data;
2961 for (line = 0; line < h; line++) {
2962 for(row = 0; row < w; row++) {
2964 curr = (unsigned short int) (*ptr & mask);
2965 temp = (unsigned char) (curr >> 8);
2966 res = fwrite(&temp, 1, 1, rawFile);
2968 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2971 temp = (unsigned char) curr;
2972 res = fwrite(&temp, 1, 1, rawFile);
2974 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2982 else if (image->comps[compno].prec <= 32)
2984 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2989 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2999 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3000 #define MAGIC_SIZE 8
3001 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3003 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3007 double gamma, display_exponent;
3008 int bit_depth, interlace_type,compression_type, filter_type;
3010 png_uint_32 resx, resy;
3012 png_uint_32 width, height;
3013 int color_type, has_alpha, is16;
3016 unsigned char **rows;
3019 opj_image_cmptparm_t cmptparm[4];
3021 unsigned int nr_comp;
3023 unsigned char sigbuf[8];
3025 if((reader = fopen(read_idf, "rb")) == NULL)
3027 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
3030 image = NULL; png = NULL; rows = NULL;
3032 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3033 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
3035 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
3038 /* libpng-VERSION/example.c:
3039 * PC : screen_gamma = 2.2;
3040 * Mac: screen_gamma = 1.7 or 1.0;
3042 display_exponent = 2.2;
3044 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3045 NULL, NULL, NULL)) == NULL)
3047 if((info = png_create_info_struct(png)) == NULL)
3050 if(setjmp(png_jmpbuf(png)))
3053 png_init_io(png, reader);
3054 png_set_sig_bytes(png, MAGIC_SIZE);
3056 png_read_info(png, info);
3058 if(png_get_IHDR(png, info, &width, &height,
3059 &bit_depth, &color_type, &interlace_type,
3060 &compression_type, &filter_type) == 0)
3063 /* png_set_expand():
3064 * expand paletted images to RGB, expand grayscale images of
3065 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3066 * to alpha channels.
3068 if(color_type == PNG_COLOR_TYPE_PALETTE)
3069 png_set_expand(png);
3071 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3072 png_set_expand(png);
3074 if(png_get_valid(png, info, PNG_INFO_tRNS))
3075 png_set_expand(png);
3077 is16 = (bit_depth == 16);
3079 /* GRAY => RGB; GRAY_ALPHA => RGBA
3081 if(color_type == PNG_COLOR_TYPE_GRAY
3082 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3084 png_set_gray_to_rgb(png);
3086 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
3087 PNG_COLOR_TYPE_RGB_ALPHA);
3089 if( !png_get_gAMA(png, info, &gamma))
3092 png_set_gamma(png, display_exponent, gamma);
3094 png_read_update_info(png, info);
3096 png_get_pHYs(png, info, &resx, &resy, &unit);
3098 color_type = png_get_color_type(png, info);
3100 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3102 nr_comp = 3 + has_alpha;
3104 bit_depth = png_get_bit_depth(png, info);
3106 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
3107 for(i = 0; i < height; ++i)
3108 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
3110 png_read_image(png, rows);
3112 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3114 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3116 for(i = 0; i < nr_comp; ++i)
3118 cmptparm[i].prec = bit_depth;
3119 /* bits_per_pixel: 8 or 16 */
3120 cmptparm[i].bpp = bit_depth;
3121 cmptparm[i].sgnd = 0;
3122 cmptparm[i].dx = sub_dx;
3123 cmptparm[i].dy = sub_dy;
3124 cmptparm[i].w = width;
3125 cmptparm[i].h = height;
3128 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3130 if(image == NULL) goto fin;
3132 image->x0 = params->image_offset_x0;
3133 image->y0 = params->image_offset_y0;
3134 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3135 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3137 r = image->comps[0].data;
3138 g = image->comps[1].data;
3139 b = image->comps[2].data;
3140 a = image->comps[3].data;
3142 for(i = 0; i < height; ++i)
3146 for(j = 0; j < width; ++j)
3150 *r++ = s[0]<<8|s[1]; s += 2;
3152 *g++ = s[0]<<8|s[1]; s += 2;
3154 *b++ = s[0]<<8|s[1]; s += 2;
3156 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3160 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3162 if(has_alpha) *a++ = *s++;
3168 for(i = 0; i < height; ++i)
3173 png_destroy_read_struct(&png, &info, NULL);
3181 int imagetopng(opj_image_t * image, const char *write_idf)
3186 int *red, *green, *blue, *alpha;
3187 unsigned char *row_buf, *d;
3188 int has_alpha, width, height, nr_comp, color_type;
3189 int adjustR, adjustG, adjustB, x, y, fails, is16, force16;
3190 int opj_prec, prec, ushift, dshift;
3191 unsigned short mask = 0xffff;
3192 png_color_8 sig_bit;
3194 is16 = force16 = ushift = dshift = 0; fails = 1;
3195 prec = opj_prec = image->comps[0].prec;
3197 if(prec > 8 && prec < 16)
3199 prec = 16; force16 = 1;
3201 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3203 fprintf(stderr,"imagetopng: can not create %s"
3204 "\n\twrong bit_depth %d\n", write_idf, prec);
3207 writer = fopen(write_idf, "wb");
3209 if(writer == NULL) return fails;
3211 info = NULL; has_alpha = 0;
3213 /* Create and initialize the png_struct with the desired error handler
3214 * functions. If you want to use the default stderr and longjump method,
3215 * you can supply NULL for the last three parameters. We also check that
3216 * the library version is compatible with the one used at compile time,
3217 * in case we are using dynamically linked libraries. REQUIRED.
3219 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3221 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3223 if(png == NULL) goto fin;
3225 /* Allocate/initialize the image information data. REQUIRED
3227 info = png_create_info_struct(png);
3229 if(info == NULL) goto fin;
3231 /* Set error handling. REQUIRED if you are not supplying your own
3232 * error handling functions in the png_create_write_struct() call.
3234 if(setjmp(png_jmpbuf(png))) goto fin;
3236 /* I/O initialization functions is REQUIRED
3238 png_init_io(png, writer);
3240 /* Set the image information here. Width and height are up to 2^31,
3241 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3242 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3243 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3244 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3245 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3246 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3249 png_set_compression_level(png, Z_BEST_COMPRESSION);
3251 if(prec == 16) mask = 0xffff;
3253 if(prec == 8) mask = 0x00ff;
3255 if(prec == 4) mask = 0x000f;
3257 if(prec == 2) mask = 0x0003;
3259 if(prec == 1) mask = 0x0001;
3261 nr_comp = image->numcomps;
3264 && image->comps[0].dx == image->comps[1].dx
3265 && image->comps[1].dx == image->comps[2].dx
3266 && image->comps[0].dy == image->comps[1].dy
3267 && image->comps[1].dy == image->comps[2].dy
3268 && image->comps[0].prec == image->comps[1].prec
3269 && image->comps[1].prec == image->comps[2].prec)
3273 has_alpha = (nr_comp > 3);
3275 is16 = (prec == 16);
3277 width = image->comps[0].w;
3278 height = image->comps[0].h;
3280 red = image->comps[0].data;
3281 green = image->comps[1].data;
3282 blue = image->comps[2].data;
3284 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3288 sig_bit.alpha = prec;
3289 alpha = image->comps[3].data;
3290 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3294 sig_bit.alpha = 0; alpha = NULL;
3295 color_type = PNG_COLOR_TYPE_RGB;
3297 png_set_sBIT(png, info, &sig_bit);
3299 png_set_IHDR(png, info, width, height, prec,
3302 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3304 /*=============================*/
3305 png_write_info(png, info);
3306 /*=============================*/
3309 png_set_packing(png);
3313 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3315 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3316 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3317 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3319 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3321 for(y = 0; y < height; ++y)
3325 for(x = 0; x < width; ++x)
3329 v = *red + adjustR; ++red;
3331 if(force16) { v = (v<<ushift) + (v>>dshift); }
3333 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3335 v = *green + adjustG; ++green;
3337 if(force16) { v = (v<<ushift) + (v>>dshift); }
3339 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3341 v = *blue + adjustB; ++blue;
3343 if(force16) { v = (v<<ushift) + (v>>dshift); }
3345 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3351 if(force16) { v = (v<<ushift) + (v>>dshift); }
3353 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3357 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3358 *d++ = (unsigned char)((*green + adjustG) & mask); ++green;
3359 *d++ = (unsigned char)((*blue + adjustB) & mask); ++blue;
3363 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3367 png_write_row(png, row_buf);
3374 if(nr_comp == 1 /* GRAY */
3375 || ( nr_comp == 2 /* GRAY_ALPHA */
3376 && image->comps[0].dx == image->comps[1].dx
3377 && image->comps[0].dy == image->comps[1].dy
3378 && image->comps[0].prec == image->comps[1].prec))
3382 red = image->comps[0].data;
3386 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3388 sig_bit.gray = prec;
3389 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3391 color_type = PNG_COLOR_TYPE_GRAY;
3395 has_alpha = 1; sig_bit.alpha = prec;
3396 alpha = image->comps[1].data;
3397 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3399 width = image->comps[0].w;
3400 height = image->comps[0].h;
3402 png_set_IHDR(png, info, width, height, sig_bit.gray,
3405 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3407 png_set_sBIT(png, info, &sig_bit);
3408 /*=============================*/
3409 png_write_info(png, info);
3410 /*=============================*/
3411 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3415 png_set_packing(png);
3420 row_buf = (unsigned char*)
3421 malloc(width * nr_comp * sizeof(unsigned short));
3423 for(y = 0; y < height; ++y)
3427 for(x = 0; x < width; ++x)
3429 v = *red + adjustR; ++red;
3431 if(force16) { v = (v<<ushift) + (v>>dshift); }
3433 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3439 if(force16) { v = (v<<ushift) + (v>>dshift); }
3441 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3444 png_write_row(png, row_buf);
3449 else /* prec <= 8 */
3451 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3453 for(y = 0; y < height; ++y)
3457 for(x = 0; x < width; ++x)
3459 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3463 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3467 png_write_row(png, row_buf);
3475 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3478 png_write_end(png, info);
3486 png_destroy_write_struct(&png, &info);
3490 if(fails) remove(write_idf);
3494 #endif /* HAVE_LIBPNG */