2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2006-2007, Parvatha Elangovan
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_apps_config.h"
39 #ifdef OPJ_HAVE_LIBTIFF
41 #endif /* OPJ_HAVE_LIBTIFF */
43 #ifdef OPJ_HAVE_LIBPNG
46 #endif /* OPJ_HAVE_LIBPNG */
52 * Get logarithm of an integer and round downwards.
56 static int int_floorlog2(int a) {
58 for (l = 0; a > 1; l++) {
64 /* -->> -->> -->> -->>
68 <<-- <<-- <<-- <<-- */
70 #ifdef INFORMATION_ONLY
71 /* TGA header definition. */
74 unsigned char id_length; /* Image id field length */
75 unsigned char colour_map_type; /* Colour map type */
76 unsigned char image_type; /* Image type */
78 ** Colour map specification
80 unsigned short colour_map_index; /* First entry index */
81 unsigned short colour_map_length; /* Colour map length */
82 unsigned char colour_map_entry_size; /* Colour map entry size */
84 ** Image specification
86 unsigned short x_origin; /* x origin of image */
87 unsigned short y_origin; /* u origin of image */
88 unsigned short image_width; /* Image width */
89 unsigned short image_height; /* Image height */
90 unsigned char pixel_depth; /* Pixel depth */
91 unsigned char image_desc; /* Image descriptor */
93 #endif /* INFORMATION_ONLY */
95 static unsigned short get_ushort(unsigned short val) {
97 #ifdef WORDS_BIGENDIAN
98 return( ((val & 0xff) << 8) + (val >> 8) );
105 #define TGA_HEADER_SIZE 18
107 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
108 unsigned int *width, unsigned int *height, int *flip_image)
112 unsigned char id_len, /*cmap_type,*/ image_type;
113 unsigned char pixel_depth, image_desc;
114 unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size;
115 unsigned short /*x_origin, y_origin,*/ image_w, image_h;
117 if (!bits_per_pixel || !width || !height || !flip_image)
119 tga = (unsigned char*)malloc(18);
121 if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 )
123 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
126 id_len = (unsigned char)tga[0];
127 /*cmap_type = (unsigned char)tga[1];*/
128 image_type = (unsigned char)tga[2];
129 /*cmap_index = get_ushort(*(unsigned short*)(&tga[3]));*/
130 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
131 cmap_entry_size = (unsigned char)tga[7];
135 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
136 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
138 image_w = get_ushort(*(unsigned short*)(&tga[12]));
139 image_h = get_ushort(*(unsigned short*)(&tga[14]));
140 pixel_depth = (unsigned char)tga[16];
141 image_desc = (unsigned char)tga[17];
145 *bits_per_pixel = (unsigned int)pixel_depth;
146 *width = (unsigned int)image_w;
147 *height = (unsigned int)image_h;
149 /* Ignore tga identifier, if present ... */
152 unsigned char *id = (unsigned char *) malloc(id_len);
153 if ( !fread(id, id_len, 1, fp) )
155 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
162 /* Test for compressed formats ... not yet supported ...
163 // Note :- 9 - RLE encoded palettized.
164 // 10 - RLE encoded RGB. */
167 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
171 *flip_image = !(image_desc & 32);
173 /* Palettized formats are not yet supported, skip over the palette, if present ... */
174 palette_size = cmap_len * (cmap_entry_size/8);
178 fprintf(stderr, "File contains a palette - not yet supported.");
179 fseek(fp, palette_size, SEEK_CUR);
184 #if WORDS_BIGENDIAN == 1
186 static INLINE int16_t swap16(int16_t x)
188 return((((u_int16_t)x & 0x00ffU) << 8) |
189 (((u_int16_t)x & 0xff00U) >> 8));
194 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
197 unsigned short image_w, image_h, us0;
198 unsigned char uc0, image_type;
199 unsigned char pixel_depth, image_desc;
201 if (!bits_per_pixel || !width || !height)
206 if ( bits_per_pixel < 256 )
207 pixel_depth = (unsigned char)bits_per_pixel;
209 fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
214 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */
215 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */
217 image_type = 2; /* Uncompressed. */
218 if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
221 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */
222 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */
223 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */
225 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */
226 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */
228 image_w = (unsigned short)width;
229 image_h = (unsigned short) height;
231 #if WORDS_BIGENDIAN == 0
232 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
233 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
235 image_w = swap16(image_w);
236 image_h = swap16(image_h);
237 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
238 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
241 if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
243 image_desc = 8; /* 8 bits per component. */
247 if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails;
252 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
256 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
259 unsigned int image_width, image_height, pixel_bit_depth;
262 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
264 OPJ_COLOR_SPACE color_space;
267 int subsampling_dx, subsampling_dy;
270 f = fopen(filename, "rb");
272 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
276 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
279 /* We currently only support 24 & 32 bit tga's ... */
280 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
283 /* initialize image components */
284 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
286 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */
287 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
290 color_space = OPJ_CLRSPC_GRAY;
291 numcomps = save_alpha ? 2 : 1;
294 numcomps = save_alpha ? 4 : 3;
295 color_space = OPJ_CLRSPC_SRGB;
298 subsampling_dx = parameters->subsampling_dx;
299 subsampling_dy = parameters->subsampling_dy;
301 for (i = 0; i < numcomps; i++) {
302 cmptparm[i].prec = 8;
304 cmptparm[i].sgnd = 0;
305 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
306 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
307 cmptparm[i].w = image_width;
308 cmptparm[i].h = image_height;
311 /* create the image */
312 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
317 /* set image offset and reference grid */
318 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
319 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
320 image->x1 = !image->x0 ? (OPJ_UINT32)(image_width - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(image_width - 1) * (OPJ_UINT32)subsampling_dx + 1;
321 image->y1 = !image->y0 ? (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1;
324 for (y=0; y < image_height; y++)
329 index = (int)((image_height-y-1)*image_width);
331 index = (int)(y*image_width);
335 for (x=0;x<image_width;x++)
339 if( !fread(&b, 1, 1, f) )
341 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
342 opj_image_destroy(image);
345 if ( !fread(&g, 1, 1, f) )
347 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
348 opj_image_destroy(image);
351 if ( !fread(&r, 1, 1, f) )
353 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
354 opj_image_destroy(image);
358 image->comps[0].data[index]=r;
359 image->comps[1].data[index]=g;
360 image->comps[2].data[index]=b;
364 else if (numcomps==4)
366 for (x=0;x<image_width;x++)
368 unsigned char r,g,b,a;
369 if ( !fread(&b, 1, 1, f) )
371 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
372 opj_image_destroy(image);
375 if ( !fread(&g, 1, 1, f) )
377 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
378 opj_image_destroy(image);
381 if ( !fread(&r, 1, 1, f) )
383 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
384 opj_image_destroy(image);
387 if ( !fread(&a, 1, 1, f) )
389 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
390 opj_image_destroy(image);
394 image->comps[0].data[index]=r;
395 image->comps[1].data[index]=g;
396 image->comps[2].data[index]=b;
397 image->comps[3].data[index]=a;
402 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
408 int imagetotga(opj_image_t * image, const char *outfile) {
409 int width, height, bpp, x, y;
410 OPJ_BOOL write_alpha;
412 int adjustR, adjustG, adjustB, fails;
413 unsigned int alpha_channel;
421 fdest = fopen(outfile, "wb");
423 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
427 for (i = 0; i < image->numcomps-1; i++) {
428 if ((image->comps[0].dx != image->comps[i+1].dx)
429 ||(image->comps[0].dy != image->comps[i+1].dy)
430 ||(image->comps[0].prec != image->comps[i+1].prec)) {
431 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
436 width = (int)image->comps[0].w;
437 height = (int)image->comps[0].h;
439 /* Mono with alpha, or RGB with alpha. */
440 write_alpha = (image->numcomps==2) || (image->numcomps==4);
442 /* Write TGA header */
443 bpp = write_alpha ? 32 : 24;
445 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
448 alpha_channel = image->numcomps-1;
450 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
452 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
453 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
454 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
456 for (y=0; y < height; y++)
458 unsigned int index= (unsigned int)(y*width);
460 for (x=0; x < width; x++, index++)
462 r = (float)(image->comps[0].data[index] + adjustR);
464 if (image->numcomps > 2)
466 g = (float)(image->comps[1].data[index] + adjustG);
467 b = (float)(image->comps[2].data[index] + adjustB);
475 /* TGA format writes BGR ... */
476 if(b > 255.) b = 255.; else if(b < 0.) b = 0.;
477 value = (unsigned char)(b*scale);
478 res = fwrite(&value,1,1,fdest);
482 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
485 if(g > 255.) g = 255.; else if(g < 0.) g = 0.;
486 value = (unsigned char)(g*scale);
487 res = fwrite(&value,1,1,fdest);
491 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
494 if(r > 255.) r = 255.; else if(r < 0.) r = 0.;
495 value = (unsigned char)(r*scale);
496 res = fwrite(&value,1,1,fdest);
500 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
506 a = (float)(image->comps[alpha_channel].data[index]);
507 if(a > 255.) a = 255.; else if(a < 0.) a = 0.;
508 value = (unsigned char)(a*scale);
509 res = fwrite(&value,1,1,fdest);
513 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
526 /* -->> -->> -->> -->>
530 <<-- <<-- <<-- <<-- */
532 /* WORD defines a two byte word */
533 typedef unsigned short int WORD;
535 /* DWORD defines a four byte word */
536 typedef unsigned int DWORD;
539 WORD bfType; /* 'BM' for Bitmap (19776) */
540 DWORD bfSize; /* Size of the file */
541 WORD bfReserved1; /* Reserved : 0 */
542 WORD bfReserved2; /* Reserved : 0 */
543 DWORD bfOffBits; /* Offset */
544 } BITMAPFILEHEADER_t;
547 DWORD biSize; /* Size of the structure in bytes */
548 DWORD biWidth; /* Width of the image in pixels */
549 DWORD biHeight; /* Heigth of the image in pixels */
550 WORD biPlanes; /* 1 */
551 WORD biBitCount; /* Number of color bits by pixels */
552 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
553 DWORD biSizeImage; /* Size of the image in bytes */
554 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
555 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
556 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
557 DWORD biClrImportant; /* Number of important color (0: ALL) */
558 } BITMAPINFOHEADER_t;
560 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
562 int subsampling_dx = parameters->subsampling_dx;
563 int subsampling_dy = parameters->subsampling_dy;
565 int i, numcomps, w, h;
566 OPJ_COLOR_SPACE color_space;
567 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
568 opj_image_t * image = NULL;
571 BITMAPFILEHEADER_t File_h;
572 BITMAPINFOHEADER_t Info_h;
574 unsigned char *table_R, *table_G, *table_B;
575 unsigned int j, PAD = 0;
583 IN = fopen(filename, "rb");
586 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
590 File_h.bfType = (WORD)getc(IN);
591 File_h.bfType = (WORD)((getc(IN) << 8) + File_h.bfType);
593 if (File_h.bfType != 19778)
595 fprintf(stderr,"Error, not a BMP file!\n");
601 File_h.bfSize = (DWORD)getc(IN);
602 File_h.bfSize = (DWORD)(getc(IN) << 8) + File_h.bfSize;
603 File_h.bfSize = (DWORD)(getc(IN) << 16) + File_h.bfSize;
604 File_h.bfSize = (DWORD)(getc(IN) << 24) + File_h.bfSize;
606 File_h.bfReserved1 = (WORD)getc(IN);
607 File_h.bfReserved1 = (WORD)((getc(IN) << 8) + File_h.bfReserved1);
609 File_h.bfReserved2 = (WORD)getc(IN);
610 File_h.bfReserved2 = (WORD)((getc(IN) << 8) + File_h.bfReserved2);
612 File_h.bfOffBits = (DWORD)getc(IN);
613 File_h.bfOffBits = (DWORD)(getc(IN) << 8) + File_h.bfOffBits;
614 File_h.bfOffBits = (DWORD)(getc(IN) << 16) + File_h.bfOffBits;
615 File_h.bfOffBits = (DWORD)(getc(IN) << 24) + File_h.bfOffBits;
620 Info_h.biSize = (DWORD)getc(IN);
621 Info_h.biSize = (DWORD)(getc(IN) << 8) + Info_h.biSize;
622 Info_h.biSize = (DWORD)(getc(IN) << 16) + Info_h.biSize;
623 Info_h.biSize = (DWORD)(getc(IN) << 24) + Info_h.biSize;
625 if(Info_h.biSize != 40)
627 fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize);
631 Info_h.biWidth = (DWORD)getc(IN);
632 Info_h.biWidth = (DWORD)(getc(IN) << 8) + Info_h.biWidth;
633 Info_h.biWidth = (DWORD)(getc(IN) << 16) + Info_h.biWidth;
634 Info_h.biWidth = (DWORD)(getc(IN) << 24) + Info_h.biWidth;
635 w = (int)Info_h.biWidth;
637 Info_h.biHeight = (DWORD)getc(IN);
638 Info_h.biHeight = (DWORD)(getc(IN) << 8) + Info_h.biHeight;
639 Info_h.biHeight = (DWORD)(getc(IN) << 16) + Info_h.biHeight;
640 Info_h.biHeight = (DWORD)(getc(IN) << 24) + Info_h.biHeight;
641 h = (int)Info_h.biHeight;
643 Info_h.biPlanes = (WORD)getc(IN);
644 Info_h.biPlanes = (WORD)((getc(IN) << 8) + Info_h.biPlanes);
646 Info_h.biBitCount = (WORD)getc(IN);
647 Info_h.biBitCount = (WORD)((getc(IN) << 8) + Info_h.biBitCount);
649 Info_h.biCompression = (DWORD)getc(IN);
650 Info_h.biCompression = (DWORD)(getc(IN) << 8) + Info_h.biCompression;
651 Info_h.biCompression = (DWORD)(getc(IN) << 16) + Info_h.biCompression;
652 Info_h.biCompression = (DWORD)(getc(IN) << 24) + Info_h.biCompression;
654 Info_h.biSizeImage = (DWORD)getc(IN);
655 Info_h.biSizeImage = (DWORD)(getc(IN) << 8) + Info_h.biSizeImage;
656 Info_h.biSizeImage = (DWORD)(getc(IN) << 16) + Info_h.biSizeImage;
657 Info_h.biSizeImage = (DWORD)(getc(IN) << 24) + Info_h.biSizeImage;
659 Info_h.biXpelsPerMeter = (DWORD)getc(IN);
660 Info_h.biXpelsPerMeter = (DWORD)(getc(IN) << 8) + Info_h.biXpelsPerMeter;
661 Info_h.biXpelsPerMeter = (DWORD)(getc(IN) << 16) + Info_h.biXpelsPerMeter;
662 Info_h.biXpelsPerMeter = (DWORD)(getc(IN) << 24) + Info_h.biXpelsPerMeter;
664 Info_h.biYpelsPerMeter = (DWORD)getc(IN);
665 Info_h.biYpelsPerMeter = (DWORD)(getc(IN) << 8) + Info_h.biYpelsPerMeter;
666 Info_h.biYpelsPerMeter = (DWORD)(getc(IN) << 16) + Info_h.biYpelsPerMeter;
667 Info_h.biYpelsPerMeter = (DWORD)(getc(IN) << 24) + Info_h.biYpelsPerMeter;
669 Info_h.biClrUsed = (DWORD)getc(IN);
670 Info_h.biClrUsed = (DWORD)(getc(IN) << 8) + Info_h.biClrUsed;
671 Info_h.biClrUsed = (DWORD)(getc(IN) << 16) + Info_h.biClrUsed;
672 Info_h.biClrUsed = (DWORD)(getc(IN) << 24) + Info_h.biClrUsed;
674 Info_h.biClrImportant = (DWORD)getc(IN);
675 Info_h.biClrImportant = (DWORD)(getc(IN) << 8) + Info_h.biClrImportant;
676 Info_h.biClrImportant = (DWORD)(getc(IN) << 16) + Info_h.biClrImportant;
677 Info_h.biClrImportant = (DWORD)(getc(IN) << 24) + Info_h.biClrImportant;
679 /* Read the data and store them in the OUT file */
681 if (Info_h.biBitCount == 24)
684 color_space = OPJ_CLRSPC_SRGB;
685 /* initialize image components */
686 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
687 for(i = 0; i < numcomps; i++)
689 cmptparm[i].prec = 8;
691 cmptparm[i].sgnd = 0;
692 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
693 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
694 cmptparm[i].w = (OPJ_UINT32)w;
695 cmptparm[i].h = (OPJ_UINT32)h;
697 /* create the image */
698 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
705 /* set image offset and reference grid */
706 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
707 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
708 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
709 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
713 /* Place the cursor at the beginning of the image information */
714 fseek(IN, 0, SEEK_SET);
715 fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
720 /* PAD = 4 - (3 * W) % 4; */
721 /* PAD = (PAD == 4) ? 0 : PAD; */
722 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
724 RGB = (unsigned char *)
725 malloc((3 * W + PAD) * H * sizeof(unsigned char));
727 if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
730 opj_image_destroy(image);
731 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
737 for(y = 0; y < H; y++)
739 unsigned char *scanline = RGB + (3 * (unsigned int)W + PAD) * ((unsigned int)H - 1 - (unsigned int)y);
740 for(x = 0; x < W; x++)
742 unsigned char *pixel = &scanline[3 * x];
743 image->comps[0].data[index] = pixel[2]; /* R */
744 image->comps[1].data[index] = pixel[1]; /* G */
745 image->comps[2].data[index] = pixel[0]; /* B */
750 }/* if (Info_h.biBitCount == 24) */
752 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
754 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
756 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
758 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
759 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
760 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
763 for (j = 0; j < Info_h.biClrUsed; j++)
765 table_B[j] = (unsigned char)getc(IN);
766 table_G[j] = (unsigned char)getc(IN);
767 table_R[j] = (unsigned char)getc(IN);
770 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
772 if(has_color) gray_scale = 0;
774 /* Place the cursor at the beginning of the image information */
775 fseek(IN, 0, SEEK_SET);
776 fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
780 if (Info_h.biWidth % 2)
783 numcomps = gray_scale ? 1 : 3;
784 color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB;
785 /* initialize image components */
786 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
787 for(i = 0; i < numcomps; i++)
789 cmptparm[i].prec = 8;
791 cmptparm[i].sgnd = 0;
792 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
793 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
794 cmptparm[i].w = (OPJ_UINT32)w;
795 cmptparm[i].h = (OPJ_UINT32)h;
797 /* create the image */
798 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
802 free(table_R); free(table_G); free(table_B);
806 /* set image offset and reference grid */
807 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
808 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
809 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
810 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
814 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
816 if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
822 opj_image_destroy(image);
823 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
829 for (j = 0; j < W * H; j++)
831 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
833 image->comps[0].data[index] =
834 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
843 for (j = 0; j < W * H; j++)
845 if ((j % W < W - 1 && Info_h.biWidth % 2)
846 || !(Info_h.biWidth % 2))
848 unsigned char pixel_index =
849 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
850 image->comps[0].data[index] = table_R[pixel_index];
851 image->comps[1].data[index] = table_G[pixel_index];
852 image->comps[2].data[index] = table_B[pixel_index];
863 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
865 unsigned char *pix, *beyond;
866 int *gray, *red, *green, *blue;
871 if (Info_h.biClrUsed == 0)
872 Info_h.biClrUsed = 256;
873 else if (Info_h.biClrUsed > 256)
874 Info_h.biClrUsed = 256;
876 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
877 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
878 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
881 for (j = 0; j < Info_h.biClrUsed; j++)
883 table_B[j] = (unsigned char)getc(IN);
884 table_G[j] = (unsigned char)getc(IN);
885 table_R[j] = (unsigned char)getc(IN);
887 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
893 numcomps = gray_scale ? 1 : 3;
894 color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB;
895 /* initialize image components */
896 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
897 for (i = 0; i < numcomps; i++)
899 cmptparm[i].prec = 8;
901 cmptparm[i].sgnd = 0;
902 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
903 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
904 cmptparm[i].w = (OPJ_UINT32)w;
905 cmptparm[i].h = (OPJ_UINT32)h;
907 /* create the image */
908 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
918 /* set image offset and reference grid */
919 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
920 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
921 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
922 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
926 /* Place the cursor at the beginning of the image information */
927 fseek(IN, 0, SEEK_SET);
928 fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
932 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
933 beyond = RGB + W * H;
945 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
946 *pix = (unsigned char)c1;
952 if (c == 0x00) /* EOL */
956 pix = RGB + x + (H - y - 1) * W;
958 else if (c == 0x01) /* EOP */
960 else if (c == 0x02) /* MOVE by dxdy */
963 x += (unsigned int)c;
965 y += (unsigned int)c;
966 pix = RGB + (H - y - 1) * W + x;
971 for (; i < c && x < W && pix < beyond; i++, x++, pix++)
974 *pix = (unsigned char)c1;
976 if (c & 1) /* skip padding byte */
984 gray = image->comps[0].data;
992 *gray++ = table_R[uc];
997 /*int *red, *green, *blue;*/
999 red = image->comps[0].data;
1000 green = image->comps[1].data;
1001 blue = image->comps[2].data;
1009 *red++ = table_R[uc];
1010 *green++ = table_G[uc];
1011 *blue++ = table_B[uc];
1022 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
1023 "is not yet implemented [%d]\n", Info_h.biBitCount);
1029 int imagetobmp(opj_image_t * image, const char *outfile) {
1033 int adjustR, adjustG, adjustB;
1035 if (image->comps[0].prec < 8) {
1036 fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec);
1039 if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
1040 && image->comps[1].dx == image->comps[2].dx
1041 && image->comps[0].dy == image->comps[1].dy
1042 && image->comps[1].dy == image->comps[2].dy
1043 && image->comps[0].prec == image->comps[1].prec
1044 && image->comps[1].prec == image->comps[2].prec) {
1046 /* -->> -->> -->> -->>
1048 <<-- <<-- <<-- <<-- */
1050 fdest = fopen(outfile, "wb");
1052 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1056 w = (int)image->comps[0].w;
1057 h = (int)image->comps[0].h;
1059 fprintf(fdest, "BM");
1063 fprintf(fdest, "%c%c%c%c",
1064 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
1065 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
1066 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
1067 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
1068 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1069 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
1073 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1074 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1075 (unsigned char) ((w) >> 8) & 0xff,
1076 (unsigned char) ((w) >> 16) & 0xff,
1077 (unsigned char) ((w) >> 24) & 0xff);
1078 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1079 (unsigned char) ((h) >> 8) & 0xff,
1080 (unsigned char) ((h) >> 16) & 0xff,
1081 (unsigned char) ((h) >> 24) & 0xff);
1082 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1083 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
1084 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1085 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
1086 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
1087 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
1088 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
1089 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1090 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1091 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1092 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1094 if (image->comps[0].prec > 8) {
1095 adjustR = (int)image->comps[0].prec - 8;
1096 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1100 if (image->comps[1].prec > 8) {
1101 adjustG = (int)image->comps[1].prec - 8;
1102 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1106 if (image->comps[2].prec > 8) {
1107 adjustB = (int)image->comps[2].prec - 8;
1108 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1113 for (i = 0; i < w * h; i++) {
1114 unsigned char rc, gc, bc;
1117 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1118 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1119 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1120 if(r > 255) r = 255; else if(r < 0) r = 0;
1121 rc = (unsigned char)r;
1123 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1124 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1125 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
1126 if(g > 255) g = 255; else if(g < 0) g = 0;
1127 gc = (unsigned char)g;
1129 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1130 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1131 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
1132 if(b > 255) b = 255; else if(b < 0) b = 0;
1133 bc = (unsigned char)b;
1135 fprintf(fdest, "%c%c%c", bc, gc, rc);
1137 if ((i + 1) % w == 0) {
1138 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
1139 fprintf(fdest, "%c", 0);
1143 } else { /* Gray-scale */
1145 /* -->> -->> -->> -->>
1146 8 bits non code (Gray scale)
1147 <<-- <<-- <<-- <<-- */
1149 fdest = fopen(outfile, "wb");
1150 w = (int)image->comps[0].w;
1151 h = (int)image->comps[0].h;
1153 fprintf(fdest, "BM");
1157 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1158 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1159 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1160 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1161 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1162 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1163 ((54 + 1024) >> 16) & 0xff,
1164 ((54 + 1024) >> 24) & 0xff);
1168 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1169 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1170 (unsigned char) ((w) >> 8) & 0xff,
1171 (unsigned char) ((w) >> 16) & 0xff,
1172 (unsigned char) ((w) >> 24) & 0xff);
1173 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1174 (unsigned char) ((h) >> 8) & 0xff,
1175 (unsigned char) ((h) >> 16) & 0xff,
1176 (unsigned char) ((h) >> 24) & 0xff);
1177 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1178 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1179 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1180 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
1181 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
1182 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
1183 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
1184 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1185 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1186 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1187 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1189 if (image->comps[0].prec > 8) {
1190 adjustR = (int)image->comps[0].prec - 8;
1191 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1195 for (i = 0; i < 256; i++) {
1196 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1199 for (i = 0; i < w * h; i++) {
1202 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1203 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1204 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1205 if(r > 255) r = 255; else if(r < 0) r = 0;
1207 fprintf(fdest, "%c", (unsigned char)r);
1209 if ((i + 1) % w == 0) {
1210 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1211 fprintf(fdest, "%c", 0);
1220 /* -->> -->> -->> -->>
1224 <<-- <<-- <<-- <<-- */
1227 static unsigned char readuchar(FILE * f)
1230 if ( !fread(&c1, 1, 1, f) )
1232 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1238 static unsigned short readushort(FILE * f, int bigendian)
1240 unsigned char c1, c2;
1241 if ( !fread(&c1, 1, 1, f) )
1243 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1246 if ( !fread(&c2, 1, 1, f) )
1248 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1252 return (unsigned short)((c1 << 8) + c2);
1254 return (unsigned short)((c2 << 8) + c1);
1257 static unsigned int readuint(FILE * f, int bigendian)
1259 unsigned char c1, c2, c3, c4;
1260 if ( !fread(&c1, 1, 1, f) )
1262 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1265 if ( !fread(&c2, 1, 1, f) )
1267 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1270 if ( !fread(&c3, 1, 1, f) )
1272 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1275 if ( !fread(&c4, 1, 1, f) )
1277 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1281 return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)(c3 << 8) + c4;
1283 return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(c2 << 8) + c1;
1286 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1289 int i, numcomps, max;
1290 OPJ_COLOR_SPACE color_space;
1291 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1292 opj_image_t * image = NULL;
1293 int adjustS, ushift, dshift, force8;
1295 char endian1,endian2,sign;
1300 opj_image_comp_t *comp = NULL;
1303 color_space = OPJ_CLRSPC_GRAY;
1305 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1309 f = fopen(filename, "rb");
1311 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1315 fseek(f, 0, SEEK_SET);
1316 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1317 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1323 while (signtmp[i]!='\0') {
1324 if (signtmp[i]=='-') sign='-';
1329 if (endian1=='M' && endian2=='L') {
1331 } else if (endian2=='M' && endian1=='L') {
1334 fprintf(stderr, "Bad pgx header, please check input file\n");
1338 /* initialize image component */
1340 cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0;
1341 cmptparm.y0 = (OPJ_UINT32)parameters->image_offset_y0;
1342 cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32)((w - 1) * parameters->subsampling_dx + 1) : cmptparm.x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)parameters->subsampling_dx + 1;
1343 cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32)((h - 1) * parameters->subsampling_dy + 1) : cmptparm.y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)parameters->subsampling_dy + 1;
1353 ushift = 8 - prec; dshift = prec - ushift;
1354 if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0;
1358 else ushift = dshift = force8 = adjustS = 0;
1360 cmptparm.prec = (OPJ_UINT32)prec;
1361 cmptparm.bpp = (OPJ_UINT32)prec;
1362 cmptparm.dx = (OPJ_UINT32)parameters->subsampling_dx;
1363 cmptparm.dy = (OPJ_UINT32)parameters->subsampling_dy;
1365 /* create the image */
1366 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space);
1371 /* set image offset and reference grid */
1372 image->x0 = cmptparm.x0;
1373 image->y0 = cmptparm.x0;
1374 image->x1 = cmptparm.w;
1375 image->y1 = cmptparm.h;
1377 /* set image data */
1379 comp = &image->comps[0];
1381 for (i = 0; i < w * h; i++) {
1385 v = readuchar(f) + adjustS;
1386 v = (v<<ushift) + (v>>dshift);
1387 comp->data[i] = (unsigned char)v;
1389 if(v > max) max = v;
1393 if (comp->prec == 8) {
1397 v = (char) readuchar(f);
1399 } else if (comp->prec <= 16) {
1401 v = readushort(f, bigendian);
1403 v = (short) readushort(f, bigendian);
1407 v = (int)readuint(f, bigendian);
1409 v = (int) readuint(f, bigendian);
1417 comp->bpp = (OPJ_UINT32)int_floorlog2(max) + 1;
1422 #define CLAMP(x,a,b) x < a ? a : (x > b ? b : x)
1424 static INLINE int clamp( const int value, const int prec, const int sgnd )
1428 if (prec <= 8) return CLAMP(value,-128,127);
1429 else if (prec <= 16) return CLAMP(value,-32768,32767);
1430 else return CLAMP(value,-2147483647-1,2147483647);
1434 if (prec <= 8) return CLAMP(value,0,255);
1435 else if (prec <= 16) return CLAMP(value,0,65535);
1436 else return value; /*CLAMP(value,0,4294967295);*/
1440 int imagetopgx(opj_image_t * image, const char *outfile)
1443 int i, j, fails = 1;
1444 unsigned int compno;
1447 for (compno = 0; compno < image->numcomps; compno++)
1449 opj_image_comp_t *comp = &image->comps[compno];
1450 char bname[256]; /* buffer for name */
1451 char *name = bname; /* pointer */
1454 const size_t olen = strlen(outfile);
1455 const size_t dotpos = olen - 4;
1456 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1458 if( outfile[dotpos] != '.' )
1460 /* `pgx` was recognized but there is no dot at expected position */
1461 fprintf(stderr, "ERROR -> Impossible happen." );
1466 name = (char*)malloc(total+1);
1468 strncpy(name, outfile, dotpos);
1469 sprintf(name+dotpos, "_%d.pgx", compno);
1470 fdest = fopen(name, "wb");
1471 /* dont need name anymore */
1472 if( total > 256 ) free(name);
1475 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1479 w = (int)image->comps[compno].w;
1480 h = (int)image->comps[compno].h;
1482 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
1485 if (comp->prec <= 8)
1487 else if (comp->prec <= 16)
1492 for (i = 0; i < w * h; i++)
1494 /* FIXME: clamp func is being called within a loop */
1495 const int val = clamp(image->comps[compno].data[i],
1496 (int)comp->prec, (int)comp->sgnd);
1498 for (j = nbytes - 1; j >= 0; j--)
1500 int v = (int)(val >> (j * 8));
1501 unsigned char byte = (unsigned char)v;
1502 res = fwrite(&byte, 1, 1, fdest);
1506 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1511 fclose(fdest); fdest = NULL;
1515 if(fdest) fclose(fdest);
1520 /* -->> -->> -->> -->>
1524 <<-- <<-- <<-- <<-- */
1528 int width, height, maxval, depth, format;
1529 char rgb, rgba, gray, graya, bw;
1533 static char *skip_white(char *s)
1537 if(*s == '\n' || *s == '\r') return NULL;
1538 if(isspace(*s)) { ++s; continue; }
1544 static char *skip_int(char *start, int *out_n)
1549 *out_n = 0; s = start;
1551 s = skip_white(start);
1552 if(s == NULL) return NULL;
1557 if( !isdigit(*s)) break;
1560 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1564 static char *skip_idf(char *start, char out_idf[256])
1569 s = skip_white(start);
1570 if(s == NULL) return NULL;
1575 if(isalpha(*s) || *s == '_') { ++s; continue; }
1578 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1582 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1585 int format, have_wh, end, ttype;
1586 char idf[256], type[256];
1589 if (fgets(line, 250, reader) == NULL)
1591 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1597 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1599 format = atoi(line + 1);
1600 if(format < 1 || format > 7)
1602 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1605 ph->format = format;
1606 ttype = end = have_wh = 0;
1608 while(fgets(line, 250, reader))
1610 if(*line == '#') continue;
1616 s = skip_idf(s, idf);
1618 if(s == NULL || *s == 0) return;
1620 if(strcmp(idf, "ENDHDR") == 0)
1624 if(strcmp(idf, "WIDTH") == 0)
1626 s = skip_int(s, &ph->width);
1627 if(s == NULL || *s == 0) return;
1631 if(strcmp(idf, "HEIGHT") == 0)
1633 s = skip_int(s, &ph->height);
1634 if(s == NULL || *s == 0) return;
1638 if(strcmp(idf, "DEPTH") == 0)
1640 s = skip_int(s, &ph->depth);
1641 if(s == NULL || *s == 0) return;
1645 if(strcmp(idf, "MAXVAL") == 0)
1647 s = skip_int(s, &ph->maxval);
1648 if(s == NULL || *s == 0) return;
1652 if(strcmp(idf, "TUPLTYPE") == 0)
1654 s = skip_idf(s, type);
1655 if(s == NULL || *s == 0) return;
1657 if(strcmp(type, "BLACKANDWHITE") == 0)
1659 ph->bw = 1; ttype = 1; continue;
1661 if(strcmp(type, "GRAYSCALE") == 0)
1663 ph->gray = 1; ttype = 1; continue;
1665 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1667 ph->graya = 1; ttype = 1; continue;
1669 if(strcmp(type, "RGB") == 0)
1671 ph->rgb = 1; ttype = 1; continue;
1673 if(strcmp(type, "RGB_ALPHA") == 0)
1675 ph->rgba = 1; ttype = 1; continue;
1677 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1680 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1682 } /* if(format == 7) */
1686 s = skip_int(s, &ph->width);
1688 s = skip_int(s, &ph->height);
1692 if(format == 1 || format == 4) break;
1696 if(format == 2 || format == 3 || format == 5 || format == 6)
1698 /* P2, P3, P5, P6: */
1699 s = skip_int(s, &ph->maxval);
1701 if(ph->maxval > 65535) return;
1704 }/* while(fgets( ) */
1705 if(format == 2 || format == 3 || format > 4)
1707 if(ph->maxval < 1 || ph->maxval > 65535) return;
1709 if(ph->width < 1 || ph->height < 1) return;
1715 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1717 if(ph->depth < 1 || ph->depth > 4) return;
1719 if(ph->width && ph->height && ph->depth && ph->maxval && ttype)
1724 if(format != 1 && format != 4)
1726 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1730 if(ph->width && ph->height) ph->ok = 1;
1736 static int has_prec(int val)
1738 if(val < 2) return 1;
1739 if(val < 4) return 2;
1740 if(val < 8) return 3;
1741 if(val < 16) return 4;
1742 if(val < 32) return 5;
1743 if(val < 64) return 6;
1744 if(val < 128) return 7;
1745 if(val < 256) return 8;
1746 if(val < 512) return 9;
1747 if(val < 1024) return 10;
1748 if(val < 2048) return 11;
1749 if(val < 4096) return 12;
1750 if(val < 8192) return 13;
1751 if(val < 16384) return 14;
1752 if(val < 32768) return 15;
1756 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1757 int subsampling_dx = parameters->subsampling_dx;
1758 int subsampling_dy = parameters->subsampling_dy;
1761 int i, compno, numcomps, w, h, prec, format;
1762 OPJ_COLOR_SPACE color_space;
1763 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1764 opj_image_t * image = NULL;
1765 struct pnm_header header_info;
1767 if((fp = fopen(filename, "rb")) == NULL)
1769 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1772 memset(&header_info, 0, sizeof(struct pnm_header));
1774 read_pnm_header(fp, &header_info);
1776 if(!header_info.ok) { fclose(fp); return NULL; }
1778 format = header_info.format;
1782 case 1: /* ascii bitmap */
1783 case 4: /* raw bitmap */
1787 case 2: /* ascii greymap */
1788 case 5: /* raw greymap */
1792 case 3: /* ascii pixmap */
1793 case 6: /* raw pixmap */
1797 case 7: /* arbitrary map */
1798 numcomps = header_info.depth;
1801 default: fclose(fp); return NULL;
1804 color_space = OPJ_CLRSPC_GRAY;/* GRAY, GRAYA */
1806 color_space = OPJ_CLRSPC_SRGB;/* RGB, RGBA */
1808 prec = has_prec(header_info.maxval);
1810 if(prec < 8) prec = 8;
1812 w = header_info.width;
1813 h = header_info.height;
1814 subsampling_dx = parameters->subsampling_dx;
1815 subsampling_dy = parameters->subsampling_dy;
1817 memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
1819 for(i = 0; i < numcomps; i++)
1821 cmptparm[i].prec = (OPJ_UINT32)prec;
1822 cmptparm[i].bpp = (OPJ_UINT32)prec;
1823 cmptparm[i].sgnd = 0;
1824 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
1825 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
1826 cmptparm[i].w = (OPJ_UINT32)w;
1827 cmptparm[i].h = (OPJ_UINT32)h;
1829 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
1831 if(!image) { fclose(fp); return NULL; }
1833 /* set image offset and reference grid */
1834 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
1835 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
1836 image->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1);
1837 image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1);
1839 if((format == 2) || (format == 3)) /* ascii pixmap */
1843 for (i = 0; i < w * h; i++)
1845 for(compno = 0; compno < numcomps; compno++)
1848 if (fscanf(fp, "%u", &index) != 1)
1849 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1851 image->comps[compno].data[i] = (OPJ_INT32)(index * 255)/header_info.maxval;
1859 && ( header_info.gray || header_info.graya
1860 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1862 unsigned char c0, c1, one;
1866 for (i = 0; i < w * h; i++)
1868 for(compno = 0; compno < numcomps; compno++)
1870 if ( !fread(&c0, 1, 1, fp) )
1871 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1874 image->comps[compno].data[i] = c0;
1878 if ( !fread(&c1, 1, 1, fp) )
1879 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1881 image->comps[compno].data[i] = ((c0<<8) | c1);
1887 if(format == 1) /* ascii bitmap */
1889 for (i = 0; i < w * h; i++)
1893 if ( fscanf(fp, "%u", &index) != 1)
1894 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1896 image->comps[0].data[i] = (index?0:255);
1906 for(y = 0; y < h; ++y)
1910 for(x = 0; x < w; ++x)
1915 uc = (unsigned char)getc(fp);
1917 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1923 if((format == 7 && header_info.bw)) /*MONO*/
1927 for(i = 0; i < w * h; ++i)
1929 if ( !fread(&uc, 1, 1, fp) )
1930 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1931 image->comps[0].data[i] = (uc & 1)?0:255;
1939 int imagetopnm(opj_image_t * image, const char *outfile)
1941 int *red, *green, *blue, *alpha;
1944 unsigned int compno, ncomp;
1945 int adjustR, adjustG, adjustB, adjustA;
1946 int fails, two, want_gray, has_alpha, triple;
1949 const char *tmp = outfile;
1954 if((prec = (int)image->comps[0].prec) > 16)
1956 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1957 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1960 two = has_alpha = 0; fails = 1;
1961 ncomp = image->numcomps;
1963 while (*tmp) ++tmp; tmp -= 2;
1964 want_gray = (*tmp == 'g' || *tmp == 'G');
1965 ncomp = image->numcomps;
1967 if(want_gray) ncomp = 1;
1969 if (ncomp == 2 /* GRAYA */
1970 || (ncomp > 2 /* RGB, RGBA */
1971 && image->comps[0].dx == image->comps[1].dx
1972 && image->comps[1].dx == image->comps[2].dx
1973 && image->comps[0].dy == image->comps[1].dy
1974 && image->comps[1].dy == image->comps[2].dy
1975 && image->comps[0].prec == image->comps[1].prec
1976 && image->comps[1].prec == image->comps[2].prec
1979 fdest = fopen(outfile, "wb");
1983 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1987 triple = (ncomp > 2);
1988 wr = (int)image->comps[0].w; hr = (int)image->comps[0].h;
1989 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1991 red = image->comps[0].data;
1995 green = image->comps[1].data;
1996 blue = image->comps[2].data;
1998 else green = blue = NULL;
2002 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
2004 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
2005 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2006 wr, hr, ncomp, max, tt);
2007 alpha = image->comps[ncomp - 1].data;
2008 adjustA = (image->comps[ncomp - 1].sgnd ?
2009 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2013 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2014 opj_version(), wr, hr, max);
2017 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2021 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2022 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2024 else adjustG = adjustB = 0;
2026 for(i = 0; i < wr * hr; ++i)
2030 v = *red + adjustR; ++red;
2031 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2034 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2038 v = *green + adjustG; ++green;
2039 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2042 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2044 v = *blue + adjustB; ++blue;
2045 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2048 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2054 v = *alpha + adjustA; ++alpha;
2055 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2058 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2066 if(v > 255) v = 255; else if(v < 0) v = 0;
2068 fprintf(fdest, "%c", (unsigned char)v);
2072 if(v > 255) v = 255; else if(v < 0) v = 0;
2074 fprintf(fdest, "%c", (unsigned char)v);
2076 if(v > 255) v = 255; else if(v < 0) v = 0;
2078 fprintf(fdest, "%c", (unsigned char)v);
2083 if(v > 255) v = 255; else if(v < 0) v = 0;
2085 fprintf(fdest, "%c", (unsigned char)v);
2089 fclose(fdest); return 0;
2094 if (image->numcomps > ncomp)
2096 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
2097 fprintf(stderr," is written to the file\n");
2099 destname = (char*)malloc(strlen(outfile) + 8);
2101 for (compno = 0; compno < ncomp; compno++)
2105 /*sprintf(destname, "%d.%s", compno, outfile);*/
2106 const size_t olen = strlen(outfile);
2107 const size_t dotpos = olen - 4;
2109 strncpy(destname, outfile, dotpos);
2110 sprintf(destname+dotpos, "_%d.pgm", compno);
2113 sprintf(destname, "%s", outfile);
2115 fdest = fopen(destname, "wb");
2118 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2122 wr = (int)image->comps[compno].w; hr = (int)image->comps[compno].h;
2123 prec = (int)image->comps[compno].prec;
2124 max = (1<<prec) - 1;
2126 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2127 opj_version(), wr, hr, max);
2129 red = image->comps[compno].data;
2131 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2135 for (i = 0; i < wr * hr; i++)
2137 v = *red + adjustR; ++red;
2138 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2141 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2146 if(v > 65535) v = 65535; else if(v < 0) v = 0;
2149 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
2153 else /* prec <= 8 */
2155 for(i = 0; i < wr * hr; ++i)
2157 v = *red + adjustR; ++red;
2158 if(v > 255) v = 255; else if(v < 0) v = 0;
2160 fprintf(fdest, "%c", (unsigned char)v);
2170 #ifdef OPJ_HAVE_LIBTIFF
2171 /* -->> -->> -->> -->>
2175 <<-- <<-- <<-- <<-- */
2177 int imagetotif(opj_image_t * image, const char *outfile)
2179 int width, height, imgsize;
2180 int bps,index,adjust, sgnd;
2181 int ushift, dshift, has_alpha, force16;
2187 ushift = dshift = force16 = has_alpha = 0;
2188 bps = (int)image->comps[0].prec;
2190 if(bps > 8 && bps < 16)
2192 ushift = 16 - bps; dshift = bps - ushift;
2193 bps = 16; force16 = 1;
2196 if(bps != 8 && bps != 16)
2198 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
2200 fprintf(stderr,"\tAborting\n");
2203 tif = TIFFOpen(outfile, "wb");
2207 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2210 sgnd = (int)image->comps[0].sgnd;
2211 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2213 if(image->numcomps >= 3
2214 && image->comps[0].dx == image->comps[1].dx
2215 && image->comps[1].dx == image->comps[2].dx
2216 && image->comps[0].dy == image->comps[1].dy
2217 && image->comps[1].dy == image->comps[2].dy
2218 && image->comps[0].prec == image->comps[1].prec
2219 && image->comps[1].prec == image->comps[2].prec)
2221 has_alpha = (image->numcomps == 4);
2223 width = (int)image->comps[0].w;
2224 height = (int)image->comps[0].h;
2225 imgsize = width * height ;
2227 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2228 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2229 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2230 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2231 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2232 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2233 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2234 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2235 strip_size = TIFFStripSize(tif);
2236 buf = _TIFFmalloc(strip_size);
2239 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2241 unsigned char *dat8;
2242 tsize_t i, ssize, last_i = 0;
2244 ssize = TIFFStripSize(tif);
2245 dat8 = (unsigned char*)buf;
2249 step = 3 + has_alpha;
2252 for(i=0; i < ssize - restx; i += step)
2258 r = image->comps[0].data[index];
2259 g = image->comps[1].data[index];
2260 b = image->comps[2].data[index];
2261 if(has_alpha) a = image->comps[3].data[index];
2268 if(has_alpha) a += adjust;
2270 if(r > 255) r = 255; else if(r < 0) r = 0;
2271 dat8[i+0] = (unsigned char)r ;
2272 if(g > 255) g = 255; else if(g < 0) g = 0;
2273 dat8[i+1] = (unsigned char)g ;
2274 if(b > 255) b = 255; else if(b < 0) b = 0;
2275 dat8[i+2] = (unsigned char)b ;
2278 if(a > 255) a = 255; else if(a < 0) a = 0;
2279 dat8[i+3] = (unsigned char)a;
2291 for(i = last_i; i < ssize; i += step)
2297 r = image->comps[0].data[index];
2298 g = image->comps[1].data[index];
2299 b = image->comps[2].data[index];
2300 if(has_alpha) a = image->comps[3].data[index];
2307 if(has_alpha) a += adjust;
2309 if(r > 255) r = 255; else if(r < 0) r = 0;
2310 if(g > 255) g = 255; else if(g < 0) g = 0;
2311 if(b > 255) b = 255; else if(b < 0) b = 0;
2313 dat8[i+0] = (unsigned char)r ;
2314 if(i+1 < ssize) dat8[i+1] = (unsigned char)g ; else break;
2315 if(i+2 < ssize) dat8[i+2] = (unsigned char)b ; else break;
2318 if(a > 255) a = 255; else if(a < 0) a = 0;
2320 if(i+3 < ssize) dat8[i+3] = (unsigned char)a ; else break;
2327 }/*if(last_i < ssize)*/
2333 step = 6 + has_alpha + has_alpha;
2336 for(i = 0; i < ssize - restx ; i += step)
2342 r = image->comps[0].data[index];
2343 g = image->comps[1].data[index];
2344 b = image->comps[2].data[index];
2345 if(has_alpha) a = image->comps[3].data[index];
2352 if(has_alpha) a += adjust;
2356 r = (r<<ushift) + (r>>dshift);
2357 g = (g<<ushift) + (g>>dshift);
2358 b = (b<<ushift) + (b>>dshift);
2359 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2361 if(r > 65535) r = 65535; else if(r < 0) r = 0;
2362 if(g > 65535) g = 65535; else if(g < 0) g = 0;
2363 if(b > 65535) b = 65535; else if(b < 0) b = 0;
2365 dat8[i+0] = (unsigned char)r;/*LSB*/
2366 dat8[i+1] = (unsigned char)(r >> 8);/*MSB*/
2367 dat8[i+2] = (unsigned char)g;
2368 dat8[i+3] = (unsigned char)(g >> 8);
2369 dat8[i+4] = (unsigned char)b;
2370 dat8[i+5] = (unsigned char)(b >> 8);
2373 if(a > 65535) a = 65535; else if(a < 0) a = 0;
2374 dat8[i+6] = (unsigned char)a;
2375 dat8[i+7] = (unsigned char)(a >> 8);
2386 for(i = last_i ; i < ssize ; i += step)
2392 r = image->comps[0].data[index];
2393 g = image->comps[1].data[index];
2394 b = image->comps[2].data[index];
2395 if(has_alpha) a = image->comps[3].data[index];
2402 if(has_alpha) a += adjust;
2406 r = (r<<ushift) + (r>>dshift);
2407 g = (g<<ushift) + (g>>dshift);
2408 b = (b<<ushift) + (b>>dshift);
2409 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2411 if(r > 65535) r = 65535; else if(r < 0) r = 0;
2412 if(g > 65535) g = 65535; else if(g < 0) g = 0;
2413 if(b > 65535) b = 65535; else if(b < 0) b = 0;
2415 dat8[i+0] = (unsigned char) r;/*LSB*/
2416 if(i+1 < ssize) dat8[i+1] = (unsigned char)(r >> 8);else break;/*MSB*/
2417 if(i+2 < ssize) dat8[i+2] = (unsigned char) g; else break;
2418 if(i+3 < ssize) dat8[i+3] = (unsigned char)(g >> 8);else break;
2419 if(i+4 < ssize) dat8[i+4] = (unsigned char) b; else break;
2420 if(i+5 < ssize) dat8[i+5] = (unsigned char)(b >> 8);else break;
2424 if(a > 65535) a = 65535; else if(a < 0) a = 0;
2425 if(i+6 < ssize) dat8[i+6] = (unsigned char)a; else break;
2426 if(i+7 < ssize) dat8[i+7] = (unsigned char)(a >> 8); else break;
2433 }/*if(last_i < ssize)*/
2436 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2437 }/*for(strip = 0; )*/
2439 _TIFFfree((void*)buf);
2445 if(image->numcomps == 1 /* GRAY */
2446 || ( image->numcomps == 2 /* GRAY_ALPHA */
2447 && image->comps[0].dx == image->comps[1].dx
2448 && image->comps[0].dy == image->comps[1].dy
2449 && image->comps[0].prec == image->comps[1].prec))
2453 has_alpha = (image->numcomps == 2);
2455 width = (int)image->comps[0].w;
2456 height = (int)image->comps[0].h;
2457 imgsize = width * height;
2460 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2461 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2462 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2463 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2464 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2465 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2466 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2467 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2469 /* Get a buffer for the data */
2470 strip_size = TIFFStripSize(tif);
2471 buf = _TIFFmalloc(strip_size);
2474 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2476 unsigned char *dat8;
2477 tsize_t i, ssize = TIFFStripSize(tif);
2478 dat8 = (unsigned char*)buf;
2482 step = 1 + has_alpha;
2484 for(i=0; i < ssize; i += step)
2490 r = image->comps[0].data[index];
2491 if(has_alpha) a = image->comps[1].data[index];
2496 if(has_alpha) a += adjust;
2498 if(r > 255) r = 255; else if(r < 0) r = 0;
2499 dat8[i+0] = (unsigned char)r;
2503 if(a > 255) a = 255; else if(a < 0) a = 0;
2504 dat8[i+1] = (unsigned char)a;
2515 step = 2 + has_alpha + has_alpha;
2517 for(i=0; i < ssize; i += step)
2523 r = image->comps[0].data[index];
2524 if(has_alpha) a = image->comps[1].data[index];
2529 if(has_alpha) a += adjust;
2533 r = (r<<ushift) + (r>>dshift);
2534 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2536 if(r > 65535) r = 65535; else if(r < 0) r = 0;
2537 dat8[i+0] = (unsigned char)r;/*LSB*/
2538 dat8[i+1] = (unsigned char)(r >> 8);/*MSB*/
2541 if(a > 65535) a = 65535; else if(a < 0) a = 0;
2542 dat8[i+2] = (unsigned char)a;
2543 dat8[i+3] = (unsigned char)(a >> 8);
2546 }/*if(index < imgsize)*/
2551 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2562 fprintf(stderr,"imagetotif: Bad color format.\n"
2563 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2564 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2571 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2572 * CINEMA : 12 bit precision
2574 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2576 int subsampling_dx = parameters->subsampling_dx;
2577 int subsampling_dy = parameters->subsampling_dy;
2582 int j, numcomps, w, h,index;
2583 OPJ_COLOR_SPACE color_space;
2584 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2585 opj_image_t *image = NULL;
2588 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
2589 unsigned int tiWidth, tiHeight;
2591 tif = TIFFOpen(filename, "r");
2595 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2598 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
2599 tiWidth = tiHeight = 0;
2601 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
2602 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
2603 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
2604 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
2605 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
2606 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
2607 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
2611 if(tiBps != 8 && tiBps != 16 && tiBps != 12) tiBps = 0;
2612 if(tiPhoto != 1 && tiPhoto != 2) tiPhoto = 0;
2614 if( !tiBps || !tiPhoto)
2617 fprintf(stderr,"tiftoimage: Bits=%d, Only 8 and 16 bits"
2618 " implemented\n",tiBps);
2621 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2622 " and GRAY(A) has been implemented\n",(int) tiPhoto);
2624 fprintf(stderr,"\tAborting\n");
2630 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2632 uint16 extrasamples;
2634 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2635 &extrasamples, &sampleinfo);
2637 if(extrasamples >= 1)
2639 switch(sampleinfo[0])
2641 case EXTRASAMPLE_UNSPECIFIED:
2642 /* Workaround for some images without correct info about alpha channel
2648 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2649 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2654 else /* extrasamples == 0 */
2655 if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
2658 /* initialize image components
2660 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2662 if ((tiPhoto == PHOTOMETRIC_RGB) && (parameters->cp_cinema)) {
2663 fprintf(stdout,"WARNING:\n"
2664 "Input image bitdepth is %d bits\n"
2665 "TIF conversion has automatically rescaled to 12-bits\n"
2666 "to comply with cinema profiles.\n",
2670 if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2672 numcomps = 3 + has_alpha;
2673 color_space = OPJ_CLRSPC_SRGB;
2675 /*#define USETILEMODE*/
2676 for(j = 0; j < numcomps; j++)
2678 if(parameters->cp_cinema)
2680 cmptparm[j].prec = 12;
2681 cmptparm[j].bpp = 12;
2685 cmptparm[j].prec = tiBps;
2686 cmptparm[j].bpp = tiBps;
2688 cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
2689 cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
2690 cmptparm[j].w = (OPJ_UINT32)w;
2691 cmptparm[j].h = (OPJ_UINT32)h;
2699 image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
2701 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
2709 /* set image offset and reference grid
2711 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
2712 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
2713 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
2714 image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
2715 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
2716 image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
2718 buf = _TIFFmalloc(TIFFStripSize(tif));
2720 strip_size=TIFFStripSize(tif);
2722 imgsize = (int)(image->comps[0].w * image->comps[0].h);
2723 /* Read the Image components
2725 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2727 unsigned char *dat8;
2730 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2731 dat8 = (unsigned char*)buf;
2735 step = 6 + has_alpha + has_alpha;
2737 for(i = 0; i < ssize; i += step)
2741 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */
2742 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
2743 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
2745 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2747 if(parameters->cp_cinema)
2749 /* Rounding 16 to 12 bits
2751 image->comps[0].data[index] =
2752 (image->comps[0].data[index] + 0x08) >> 4 ;
2753 image->comps[1].data[index] =
2754 (image->comps[1].data[index] + 0x08) >> 4 ;
2755 image->comps[2].data[index] =
2756 (image->comps[2].data[index] + 0x08) >> 4 ;
2758 image->comps[3].data[index] =
2759 (image->comps[3].data[index] + 0x08) >> 4 ;
2766 }/*if(tiBps == 16)*/
2770 step = 3 + has_alpha;
2772 for(i = 0; i < ssize; i += step)
2777 image->comps[0].data[index] = dat8[i+0];/* R */
2778 image->comps[1].data[index] = dat8[i+1];/* G */
2779 image->comps[2].data[index] = dat8[i+2];/* B */
2781 image->comps[3].data[index] = dat8[i+3];
2784 if(parameters->cp_cinema)
2786 /* Rounding 8 to 12 bits
2789 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2790 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2791 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2793 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2801 }/*if( tiBps == 8)*/
2803 if(tiBps == 12)/* CINEMA file */
2807 for(i = 0; i < ssize; i += step)
2809 if((index < imgsize)&(index+1 < imgsize))
2811 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2812 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2814 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2815 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2817 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2818 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2826 }/*for(strip = 0; )*/
2834 if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2836 numcomps = 1 + has_alpha;
2837 color_space = OPJ_CLRSPC_GRAY;
2839 for(j = 0; j < numcomps; ++j)
2841 cmptparm[j].prec = tiBps;
2842 cmptparm[j].bpp = tiBps;
2843 cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
2844 cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
2845 cmptparm[j].w = (OPJ_UINT32)w;
2846 cmptparm[j].h = (OPJ_UINT32)h;
2849 image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
2851 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
2859 /* set image offset and reference grid
2861 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
2862 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
2863 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
2864 image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
2865 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
2866 image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
2868 buf = _TIFFmalloc(TIFFStripSize(tif));
2870 strip_size = TIFFStripSize(tif);
2872 imgsize = (int)(image->comps[0].w * image->comps[0].h);
2873 /* Read the Image components
2875 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2877 unsigned char *dat8;
2881 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2882 dat8 = (unsigned char*)buf;
2886 step = 2 + has_alpha + has_alpha;
2888 for(i = 0; i < ssize; i += step)
2892 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2894 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2904 step = 1 + has_alpha;
2906 for(i = 0; i < ssize; i += step)
2910 image->comps[0].data[index] = dat8[i+0];
2912 image->comps[1].data[index] = dat8[i+1];
2930 #endif /* OPJ_HAVE_LIBTIFF */
2932 /* -->> -->> -->> -->>
2936 <<-- <<-- <<-- <<-- */
2937 static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian) {
2938 int subsampling_dx = parameters->subsampling_dx;
2939 int subsampling_dy = parameters->subsampling_dy;
2942 int i, compno, numcomps, w, h;
2943 OPJ_COLOR_SPACE color_space;
2944 opj_image_cmptparm_t *cmptparm;
2945 opj_image_t * image = NULL;
2948 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2950 fprintf(stderr,"\nError: invalid raw image parameters\n");
2951 fprintf(stderr,"Please use the Format option -F:\n");
2952 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2953 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2954 fprintf(stderr,"Aborting\n");
2958 f = fopen(filename, "rb");
2960 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2961 fprintf(stderr,"Aborting\n");
2964 numcomps = raw_cp->rawComp;
2965 color_space = OPJ_CLRSPC_SRGB;
2966 w = raw_cp->rawWidth;
2967 h = raw_cp->rawHeight;
2968 cmptparm = (opj_image_cmptparm_t*) malloc((size_t)numcomps * sizeof(opj_image_cmptparm_t));
2970 /* initialize image components */
2971 memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
2972 for(i = 0; i < numcomps; i++) {
2973 cmptparm[i].prec = (OPJ_UINT32)raw_cp->rawBitDepth;
2974 cmptparm[i].bpp = (OPJ_UINT32)raw_cp->rawBitDepth;
2975 cmptparm[i].sgnd = (OPJ_UINT32)raw_cp->rawSigned;
2976 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
2977 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
2978 cmptparm[i].w = (OPJ_UINT32)w;
2979 cmptparm[i].h = (OPJ_UINT32)h;
2981 /* create the image */
2982 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
2988 /* set image offset and reference grid */
2989 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
2990 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
2991 image->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
2992 image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
2994 if(raw_cp->rawBitDepth <= 8)
2996 unsigned char value = 0;
2997 for(compno = 0; compno < numcomps; compno++) {
2998 for (i = 0; i < w * h; i++) {
2999 if (!fread(&value, 1, 1, f)) {
3000 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
3003 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
3007 else if(raw_cp->rawBitDepth <= 16)
3009 unsigned short value;
3010 for(compno = 0; compno < numcomps; compno++) {
3011 for (i = 0; i < w * h; i++) {
3012 unsigned char temp1;
3013 unsigned char temp2;
3014 if (!fread(&temp1, 1, 1, f)) {
3015 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
3018 if (!fread(&temp2, 1, 1, f)) {
3019 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
3024 value = (unsigned short)((temp1 << 8) + temp2);
3028 value = (unsigned short)((temp2 << 8) + temp1);
3030 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
3035 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
3039 if (fread(&ch, 1, 1, f)) {
3040 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
3047 opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
3048 return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE);
3051 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
3052 return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE);
3055 static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL big_endian)
3057 FILE *rawFile = NULL;
3059 unsigned int compno;
3061 int line, row, curr, mask;
3066 if((image->numcomps * image->x1 * image->y1) == 0)
3068 fprintf(stderr,"\nError: invalid raw image parameters\n");
3072 rawFile = fopen(outfile, "wb");
3074 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
3079 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
3081 for(compno = 0; compno < image->numcomps; compno++)
3083 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
3084 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
3086 w = (int)image->comps[compno].w;
3087 h = (int)image->comps[compno].h;
3089 if(image->comps[compno].prec <= 8)
3091 if(image->comps[compno].sgnd == 1)
3093 mask = (1 << image->comps[compno].prec) - 1;
3094 ptr = image->comps[compno].data;
3095 for (line = 0; line < h; line++) {
3096 for(row = 0; row < w; row++) {
3098 if(curr > 127) curr = 127; else if(curr < -128) curr = -128;
3099 uc = (unsigned char) (curr & mask);
3100 res = fwrite(&uc, 1, 1, rawFile);
3102 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3109 else if(image->comps[compno].sgnd == 0)
3111 mask = (1 << image->comps[compno].prec) - 1;
3112 ptr = image->comps[compno].data;
3113 for (line = 0; line < h; line++) {
3114 for(row = 0; row < w; row++) {
3116 if(curr > 255) curr = 255; else if(curr < 0) curr = 0;
3117 uc = (unsigned char) (curr & mask);
3118 res = fwrite(&uc, 1, 1, rawFile);
3120 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
3128 else if(image->comps[compno].prec <= 16)
3130 if(image->comps[compno].sgnd == 1)
3132 union { signed short val; signed char vals[2]; } uc16;
3133 mask = (1 << image->comps[compno].prec) - 1;
3134 ptr = image->comps[compno].data;
3135 for (line = 0; line < h; line++) {
3136 for(row = 0; row < w; row++) {
3138 if(curr > 32767 ) curr = 32767; else if( curr < -32768) curr = -32768;
3139 uc16.val = (signed short)(curr & mask);
3140 res = fwrite(uc16.vals, 1, 2, rawFile);
3142 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
3149 else if(image->comps[compno].sgnd == 0)
3151 union { unsigned short val; unsigned char vals[2]; } uc16;
3152 mask = (1 << image->comps[compno].prec) - 1;
3153 ptr = image->comps[compno].data;
3154 for (line = 0; line < h; line++) {
3155 for(row = 0; row < w; row++) {
3157 if(curr > 65536 ) curr = 65536; else if( curr < 0) curr = 0;
3158 uc16.val = (unsigned short)(curr & mask);
3159 res = fwrite(uc16.vals, 1, 2, rawFile);
3161 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
3169 else if (image->comps[compno].prec <= 32)
3171 fprintf(stderr,"More than 16 bits per component no handled yet\n");
3176 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
3186 int imagetoraw(opj_image_t * image, const char *outfile)
3188 return imagetoraw_common(image, outfile, OPJ_TRUE);
3191 int imagetorawl(opj_image_t * image, const char *outfile)
3193 return imagetoraw_common(image, outfile, OPJ_FALSE);
3196 #ifdef OPJ_HAVE_LIBPNG
3198 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
3199 #define MAGIC_SIZE 8
3200 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
3202 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
3206 double gamma, display_exponent;
3207 int bit_depth, interlace_type,compression_type, filter_type;
3209 png_uint_32 resx, resy;
3211 png_uint_32 width, height;
3212 int color_type, has_alpha, is16;
3215 unsigned char **rows;
3218 opj_image_cmptparm_t cmptparm[4];
3220 unsigned int nr_comp;
3222 unsigned char sigbuf[8];
3224 if((reader = fopen(read_idf, "rb")) == NULL)
3226 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
3229 image = NULL; png = NULL; rows = NULL;
3231 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
3232 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
3234 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
3237 /* libpng-VERSION/example.c:
3238 * PC : screen_gamma = 2.2;
3239 * Mac: screen_gamma = 1.7 or 1.0;
3241 display_exponent = 2.2;
3243 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
3244 NULL, NULL, NULL)) == NULL)
3246 if((info = png_create_info_struct(png)) == NULL)
3249 if(setjmp(png_jmpbuf(png)))
3252 png_init_io(png, reader);
3253 png_set_sig_bytes(png, MAGIC_SIZE);
3255 png_read_info(png, info);
3257 if(png_get_IHDR(png, info, &width, &height,
3258 &bit_depth, &color_type, &interlace_type,
3259 &compression_type, &filter_type) == 0)
3262 /* png_set_expand():
3263 * expand paletted images to RGB, expand grayscale images of
3264 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
3265 * to alpha channels.
3267 if(color_type == PNG_COLOR_TYPE_PALETTE)
3268 png_set_expand(png);
3270 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3271 png_set_expand(png);
3273 if(png_get_valid(png, info, PNG_INFO_tRNS))
3274 png_set_expand(png);
3276 is16 = (bit_depth == 16);
3278 /* GRAY => RGB; GRAY_ALPHA => RGBA
3280 if(color_type == PNG_COLOR_TYPE_GRAY
3281 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3283 png_set_gray_to_rgb(png);
3285 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
3286 PNG_COLOR_TYPE_RGB_ALPHA);
3288 if( !png_get_gAMA(png, info, &gamma))
3291 png_set_gamma(png, display_exponent, gamma);
3293 png_read_update_info(png, info);
3295 png_get_pHYs(png, info, &resx, &resy, &unit);
3297 color_type = png_get_color_type(png, info);
3299 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
3301 nr_comp = 3 + (unsigned int)has_alpha;
3303 bit_depth = png_get_bit_depth(png, info);
3305 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
3306 for(i = 0; i < height; ++i)
3307 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
3309 png_read_image(png, rows);
3311 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3313 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3315 for(i = 0; i < nr_comp; ++i)
3317 cmptparm[i].prec = (OPJ_UINT32)bit_depth;
3318 /* bits_per_pixel: 8 or 16 */
3319 cmptparm[i].bpp = (OPJ_UINT32)bit_depth;
3320 cmptparm[i].sgnd = 0;
3321 cmptparm[i].dx = (OPJ_UINT32)sub_dx;
3322 cmptparm[i].dy = (OPJ_UINT32)sub_dy;
3323 cmptparm[i].w = (OPJ_UINT32)width;
3324 cmptparm[i].h = (OPJ_UINT32)height;
3327 image = opj_image_create(nr_comp, &cmptparm[0], OPJ_CLRSPC_SRGB);
3329 if(image == NULL) goto fin;
3331 image->x0 = (OPJ_UINT32)params->image_offset_x0;
3332 image->y0 = (OPJ_UINT32)params->image_offset_y0;
3333 image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32)sub_dx + 1 + image->x0);
3334 image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)sub_dy + 1 + image->y0);
3336 r = image->comps[0].data;
3337 g = image->comps[1].data;
3338 b = image->comps[2].data;
3339 a = image->comps[3].data;
3341 for(i = 0; i < height; ++i)
3345 for(j = 0; j < width; ++j)
3349 *r++ = s[0]<<8|s[1]; s += 2;
3351 *g++ = s[0]<<8|s[1]; s += 2;
3353 *b++ = s[0]<<8|s[1]; s += 2;
3355 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3359 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3361 if(has_alpha) *a++ = *s++;
3367 for(i = 0; i < height; ++i)
3372 png_destroy_read_struct(&png, &info, NULL);
3380 int imagetopng(opj_image_t * image, const char *write_idf)
3385 int *red, *green, *blue, *alpha;
3386 unsigned char *row_buf, *d;
3387 int has_alpha, width, height, nr_comp, color_type;
3388 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
3389 int prec, ushift, dshift, is16, force16, force8;
3390 unsigned short mask = 0xffff;
3391 png_color_8 sig_bit;
3393 is16 = force16 = force8 = ushift = dshift = 0; fails = 1;
3394 prec = (int)image->comps[0].prec;
3395 nr_comp = (int)image->numcomps;
3397 if(prec > 8 && prec < 16)
3399 ushift = 16 - prec; dshift = prec - ushift;
3400 prec = 16; force16 = 1;
3403 if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */
3405 ushift = 8 - prec; dshift = 8 - ushift;
3406 prec = 8; force8 = 1;
3409 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3411 fprintf(stderr,"imagetopng: can not create %s"
3412 "\n\twrong bit_depth %d\n", write_idf, prec);
3415 writer = fopen(write_idf, "wb");
3417 if(writer == NULL) return fails;
3419 info = NULL; has_alpha = 0;
3421 /* Create and initialize the png_struct with the desired error handler
3422 * functions. If you want to use the default stderr and longjump method,
3423 * you can supply NULL for the last three parameters. We also check that
3424 * the library version is compatible with the one used at compile time,
3425 * in case we are using dynamically linked libraries. REQUIRED.
3427 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3429 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3431 if(png == NULL) goto fin;
3433 /* Allocate/initialize the image information data. REQUIRED
3435 info = png_create_info_struct(png);
3437 if(info == NULL) goto fin;
3439 /* Set error handling. REQUIRED if you are not supplying your own
3440 * error handling functions in the png_create_write_struct() call.
3442 if(setjmp(png_jmpbuf(png))) goto fin;
3444 /* I/O initialization functions is REQUIRED
3446 png_init_io(png, writer);
3448 /* Set the image information here. Width and height are up to 2^31,
3449 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3450 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3451 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3452 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3453 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3454 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3459 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
3460 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
3461 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
3462 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
3465 png_set_compression_level(png, Z_BEST_COMPRESSION);
3467 if(prec == 16) mask = 0xffff;
3469 if(prec == 8) mask = 0x00ff;
3471 if(prec == 4) mask = 0x000f;
3473 if(prec == 2) mask = 0x0003;
3475 if(prec == 1) mask = 0x0001;
3478 && image->comps[0].dx == image->comps[1].dx
3479 && image->comps[1].dx == image->comps[2].dx
3480 && image->comps[0].dy == image->comps[1].dy
3481 && image->comps[1].dy == image->comps[2].dy
3482 && image->comps[0].prec == image->comps[1].prec
3483 && image->comps[1].prec == image->comps[2].prec)
3487 has_alpha = (nr_comp > 3);
3489 is16 = (prec == 16);
3491 width = (int)image->comps[0].w;
3492 height = (int)image->comps[0].h;
3494 red = image->comps[0].data;
3495 green = image->comps[1].data;
3496 blue = image->comps[2].data;
3498 sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)prec;
3502 sig_bit.alpha = (png_byte)prec;
3503 alpha = image->comps[3].data;
3504 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3505 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
3509 sig_bit.alpha = 0; alpha = NULL;
3510 color_type = PNG_COLOR_TYPE_RGB;
3513 png_set_sBIT(png, info, &sig_bit);
3515 png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, prec,
3518 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3520 png_set_gamma(png, 2.2, 1./2.2);
3521 png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL);
3522 /*=============================*/
3523 png_write_info(png, info);
3524 /*=============================*/
3527 png_set_packing(png);
3529 printf("%s:%d:sgnd(%d,%d,%d) w(%d) h(%d) alpha(%d)\n",__FILE__,__LINE__,
3530 image->comps[0].sgnd,
3531 image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha);
3533 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3534 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3535 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3537 row_buf = (unsigned char*)malloc((size_t)width * (size_t)nr_comp * 2);
3539 for(y = 0; y < height; ++y)
3543 for(x = 0; x < width; ++x)
3547 v = *red + adjustR; ++red;
3548 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3550 if(force16) { v = (v<<ushift) + (v>>dshift); }
3552 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3554 v = *green + adjustG; ++green;
3555 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3557 if(force16) { v = (v<<ushift) + (v>>dshift); }
3559 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3561 v = *blue + adjustB; ++blue;
3562 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3564 if(force16) { v = (v<<ushift) + (v>>dshift); }
3566 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3570 v = *alpha + adjustA; ++alpha;
3571 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3573 if(force16) { v = (v<<ushift) + (v>>dshift); }
3575 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3580 v = *red + adjustR; ++red;
3581 if(v > 255) v = 255; else if(v < 0) v = 0;
3583 if(force8) { v = (v<<ushift) + (v>>dshift); }
3585 *d++ = (unsigned char)(v & mask);
3587 v = *green + adjustG; ++green;
3588 if(v > 255) v = 255; else if(v < 0) v = 0;
3590 if(force8) { v = (v<<ushift) + (v>>dshift); }
3592 *d++ = (unsigned char)(v & mask);
3594 v = *blue + adjustB; ++blue;
3595 if(v > 255) v = 255; else if(v < 0) v = 0;
3597 if(force8) { v = (v<<ushift) + (v>>dshift); }
3599 *d++ = (unsigned char)(v & mask);
3603 v = *alpha + adjustA; ++alpha;
3604 if(v > 255) v = 255; else if(v < 0) v = 0;
3606 if(force8) { v = (v<<ushift) + (v>>dshift); }
3608 *d++ = (unsigned char)(v & mask);
3612 png_write_row(png, row_buf);
3619 if(nr_comp == 1 /* GRAY */
3620 || ( nr_comp == 2 /* GRAY_ALPHA */
3621 && image->comps[0].dx == image->comps[1].dx
3622 && image->comps[0].dy == image->comps[1].dy
3623 && image->comps[0].prec == image->comps[1].prec))
3627 red = image->comps[0].data;
3629 sig_bit.gray = (png_byte)prec;
3630 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3631 alpha = NULL; adjustA = 0;
3632 color_type = PNG_COLOR_TYPE_GRAY;
3636 has_alpha = 1; sig_bit.alpha = (png_byte)prec;
3637 alpha = image->comps[1].data;
3638 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3639 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3641 width = (int)image->comps[0].w;
3642 height = (int)image->comps[0].h;
3644 png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, sig_bit.gray,
3647 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3649 png_set_sBIT(png, info, &sig_bit);
3651 png_set_gamma(png, 2.2, 1./2.2);
3652 png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL);
3653 /*=============================*/
3654 png_write_info(png, info);
3655 /*=============================*/
3656 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3660 png_set_packing(png);
3665 row_buf = (unsigned char*)
3666 malloc((size_t)width * (size_t)nr_comp * sizeof(unsigned short));
3668 for(y = 0; y < height; ++y)
3672 for(x = 0; x < width; ++x)
3674 v = *red + adjustR; ++red;
3675 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3677 if(force16) { v = (v<<ushift) + (v>>dshift); }
3679 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3684 if(v > 65535) v = 65535; else if(v < 0) v = 0;
3686 if(force16) { v = (v<<ushift) + (v>>dshift); }
3688 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3691 png_write_row(png, row_buf);
3696 else /* prec <= 8 */
3698 row_buf = (unsigned char*)calloc((size_t)width, (size_t)nr_comp * 2);
3700 for(y = 0; y < height; ++y)
3704 for(x = 0; x < width; ++x)
3706 v = *red + adjustR; ++red;
3707 if(v > 255) v = 255; else if(v < 0) v = 0;
3709 if(force8) { v = (v<<ushift) + (v>>dshift); }
3711 *d++ = (unsigned char)(v & mask);
3715 v = *alpha + adjustA; ++alpha;
3716 if(v > 255) v = 255; else if(v < 0) v = 0;
3718 if(force8) { v = (v<<ushift) + (v>>dshift); }
3720 *d++ = (unsigned char)(v & mask);
3724 png_write_row(png, row_buf);
3732 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3735 png_write_end(png, info);
3743 png_destroy_write_struct(&png, &info);
3747 if(fails) remove(write_idf);
3751 #endif /* OPJ_HAVE_LIBPNG */