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 // TGA header definition.
71 #pragma pack(push,1) // Pack structure byte aligned
72 typedef struct tga_header
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 #pragma pack(pop) // Return to normal structure packing alignment.
95 int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
96 unsigned int *width, unsigned int *height, int *flip_image)
101 if (!bits_per_pixel || !width || !height || !flip_image)
105 if ( fread((unsigned char*)&tga, sizeof(tga_header), 1, fp) != 1 )
107 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
111 *bits_per_pixel = tga.pixel_depth;
113 *width = tga.image_width;
114 *height = tga.image_height ;
116 // Ignore tga identifier, if present ...
119 unsigned char *id = (unsigned char *) malloc(tga.id_length);
120 if ( !fread(id, tga.id_length, 1, fp) )
122 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
129 // Test for compressed formats ... not yet supported ...
130 // Note :- 9 - RLE encoded palettized.
131 // 10 - RLE encoded RGB.
132 if (tga.image_type > 8)
134 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
138 *flip_image = !(tga.image_desc & 32);
140 // Palettized formats are not yet supported, skip over the palette, if present ...
141 palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
145 fprintf(stderr, "File contains a palette - not yet supported.");
146 fseek(fp, palette_size, SEEK_CUR);
151 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
156 if (!bits_per_pixel || !width || !height)
159 memset(&tga, 0, sizeof(tga_header));
161 if ( bits_per_pixel < 256 )
162 tga.pixel_depth = (unsigned char)bits_per_pixel;
164 fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
167 tga.image_width = (unsigned short)width;
168 tga.image_height = (unsigned short)height;
169 tga.image_type = 2; // Uncompressed.
170 tga.image_desc = 8; // 8 bits per component.
173 tga.image_desc |= 32;
176 fwrite((unsigned char*)&tga, sizeof(tga_header), 1, fp);
181 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
184 unsigned int image_width, image_height, pixel_bit_depth;
187 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
189 OPJ_COLOR_SPACE color_space;
192 int subsampling_dx, subsampling_dy;
195 f = fopen(filename, "rb");
197 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
201 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
204 // We currently only support 24 & 32 bit tga's ...
205 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
208 /* initialize image components */
209 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
211 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha.
212 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
215 color_space = CLRSPC_GRAY;
216 numcomps = save_alpha ? 2 : 1;
219 numcomps = save_alpha ? 4 : 3;
220 color_space = CLRSPC_SRGB;
223 subsampling_dx = parameters->subsampling_dx;
224 subsampling_dy = parameters->subsampling_dy;
226 for (i = 0; i < numcomps; i++) {
227 cmptparm[i].prec = 8;
229 cmptparm[i].sgnd = 0;
230 cmptparm[i].dx = subsampling_dx;
231 cmptparm[i].dy = subsampling_dy;
232 cmptparm[i].w = image_width;
233 cmptparm[i].h = image_height;
236 /* create the image */
237 image = opj_image_create(numcomps, &cmptparm[0], color_space);
242 /* set image offset and reference grid */
243 image->x0 = parameters->image_offset_x0;
244 image->y0 = parameters->image_offset_y0;
245 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
246 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
249 for (y=0; y < image_height; y++)
254 index = (image_height-y-1)*image_width;
256 index = y*image_width;
260 for (x=0;x<image_width;x++)
264 if( !fread(&b, 1, 1, f) )
266 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
267 opj_image_destroy(image);
270 if ( !fread(&g, 1, 1, f) )
272 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
273 opj_image_destroy(image);
276 if ( !fread(&r, 1, 1, f) )
278 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
279 opj_image_destroy(image);
283 image->comps[0].data[index]=r;
284 image->comps[1].data[index]=g;
285 image->comps[2].data[index]=b;
289 else if (numcomps==4)
291 for (x=0;x<image_width;x++)
293 unsigned char r,g,b,a;
294 if ( !fread(&b, 1, 1, f) )
296 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
297 opj_image_destroy(image);
300 if ( !fread(&g, 1, 1, f) )
302 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
303 opj_image_destroy(image);
306 if ( !fread(&r, 1, 1, f) )
308 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
309 opj_image_destroy(image);
312 if ( !fread(&a, 1, 1, f) )
314 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
315 opj_image_destroy(image);
319 image->comps[0].data[index]=r;
320 image->comps[1].data[index]=g;
321 image->comps[2].data[index]=b;
322 image->comps[3].data[index]=a;
327 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
333 int imagetotga(opj_image_t * image, const char *outfile) {
334 int width, height, bpp, x, y;
335 opj_bool write_alpha;
337 unsigned int alpha_channel;
343 fdest = fopen(outfile, "wb");
345 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
349 for (i = 0; i < image->numcomps-1; i++) {
350 if ((image->comps[0].dx != image->comps[i+1].dx)
351 ||(image->comps[0].dy != image->comps[i+1].dy)
352 ||(image->comps[0].prec != image->comps[i+1].prec)) {
353 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
358 width = image->comps[0].w;
359 height = image->comps[0].h;
361 // Mono with alpha, or RGB with alpha.
362 write_alpha = (image->numcomps==2) || (image->numcomps==4);
365 bpp = write_alpha ? 32 : 24;
366 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
369 alpha_channel = image->numcomps-1;
371 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
373 for (y=0; y < height; y++) {
374 unsigned int index=y*width;
376 for (x=0; x < width; x++, index++) {
377 r = (float)(image->comps[0].data[index]);
379 if (image->numcomps>2) {
380 g = (float)(image->comps[1].data[index]);
381 b = (float)(image->comps[2].data[index]);
383 else {// Greyscale ...
388 // TGA format writes BGR ...
389 value = (unsigned char)(b*scale);
390 fwrite(&value,1,1,fdest);
392 value = (unsigned char)(g*scale);
393 fwrite(&value,1,1,fdest);
395 value = (unsigned char)(r*scale);
396 fwrite(&value,1,1,fdest);
399 a = (float)(image->comps[alpha_channel].data[index]);
400 value = (unsigned char)(a*scale);
401 fwrite(&value,1,1,fdest);
409 /* -->> -->> -->> -->>
413 <<-- <<-- <<-- <<-- */
415 /* WORD defines a two byte word */
416 typedef unsigned short int WORD;
418 /* DWORD defines a four byte word */
419 typedef unsigned long int DWORD;
422 WORD bfType; /* 'BM' for Bitmap (19776) */
423 DWORD bfSize; /* Size of the file */
424 WORD bfReserved1; /* Reserved : 0 */
425 WORD bfReserved2; /* Reserved : 0 */
426 DWORD bfOffBits; /* Offset */
427 } BITMAPFILEHEADER_t;
430 DWORD biSize; /* Size of the structure in bytes */
431 DWORD biWidth; /* Width of the image in pixels */
432 DWORD biHeight; /* Heigth of the image in pixels */
433 WORD biPlanes; /* 1 */
434 WORD biBitCount; /* Number of color bits by pixels */
435 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
436 DWORD biSizeImage; /* Size of the image in bytes */
437 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
438 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
439 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
440 DWORD biClrImportant; /* Number of important color (0: ALL) */
441 } BITMAPINFOHEADER_t;
443 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
445 int subsampling_dx = parameters->subsampling_dx;
446 int subsampling_dy = parameters->subsampling_dy;
448 int i, numcomps, w, h;
449 OPJ_COLOR_SPACE color_space;
450 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
451 opj_image_t * image = NULL;
454 BITMAPFILEHEADER_t File_h;
455 BITMAPINFOHEADER_t Info_h;
457 unsigned char *table_R, *table_G, *table_B;
458 unsigned int j, PAD = 0;
465 IN = fopen(filename, "rb");
468 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
472 File_h.bfType = getc(IN);
473 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
475 if (File_h.bfType != 19778)
477 fprintf(stderr,"Error, not a BMP file!\n");
483 File_h.bfSize = getc(IN);
484 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
485 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
486 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
488 File_h.bfReserved1 = getc(IN);
489 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
491 File_h.bfReserved2 = getc(IN);
492 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
494 File_h.bfOffBits = getc(IN);
495 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
496 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
497 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
502 Info_h.biSize = getc(IN);
503 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
504 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
505 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
507 if(Info_h.biSize != 40)
509 fprintf(stderr,"Error, unknown BMP header size %ld\n", Info_h.biSize);
513 Info_h.biWidth = getc(IN);
514 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
515 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
516 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
519 Info_h.biHeight = getc(IN);
520 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
521 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
522 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
525 Info_h.biPlanes = getc(IN);
526 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
528 Info_h.biBitCount = getc(IN);
529 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
531 Info_h.biCompression = getc(IN);
532 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
533 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
534 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
536 Info_h.biSizeImage = getc(IN);
537 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
538 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
539 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
541 Info_h.biXpelsPerMeter = getc(IN);
542 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
543 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
544 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
546 Info_h.biYpelsPerMeter = getc(IN);
547 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
548 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
549 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
551 Info_h.biClrUsed = getc(IN);
552 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
553 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
554 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
556 Info_h.biClrImportant = getc(IN);
557 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
558 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
559 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
561 /* Read the data and store them in the OUT file */
563 if (Info_h.biBitCount == 24)
566 color_space = CLRSPC_SRGB;
567 /* initialize image components */
568 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
569 for(i = 0; i < numcomps; i++)
571 cmptparm[i].prec = 8;
573 cmptparm[i].sgnd = 0;
574 cmptparm[i].dx = subsampling_dx;
575 cmptparm[i].dy = subsampling_dy;
579 /* create the image */
580 image = opj_image_create(numcomps, &cmptparm[0], color_space);
587 /* set image offset and reference grid */
588 image->x0 = parameters->image_offset_x0;
589 image->y0 = parameters->image_offset_y0;
590 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
591 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
595 /* Place the cursor at the beginning of the image information */
596 fseek(IN, 0, SEEK_SET);
597 fseek(IN, File_h.bfOffBits, SEEK_SET);
602 /* PAD = 4 - (3 * W) % 4; */
603 /* PAD = (PAD == 4) ? 0 : PAD; */
604 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
606 RGB = (unsigned char *)
607 malloc((3 * W + PAD) * H * sizeof(unsigned char));
609 if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
612 opj_image_destroy(image);
613 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
619 for(y = 0; y < (int)H; y++)
621 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
622 for(x = 0; x < (int)W; x++)
624 unsigned char *pixel = &scanline[3 * x];
625 image->comps[0].data[index] = pixel[2]; /* R */
626 image->comps[1].data[index] = pixel[1]; /* G */
627 image->comps[2].data[index] = pixel[0]; /* B */
632 }/* if (Info_h.biBitCount == 24) */
634 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)//RGB
636 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
638 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
640 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
641 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
642 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
645 for (j = 0; j < Info_h.biClrUsed; j++)
647 table_B[j] = (unsigned char)getc(IN);
648 table_G[j] = (unsigned char)getc(IN);
649 table_R[j] = (unsigned char)getc(IN);
652 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
654 if(has_color) gray_scale = 0;
656 /* Place the cursor at the beginning of the image information */
657 fseek(IN, 0, SEEK_SET);
658 fseek(IN, File_h.bfOffBits, SEEK_SET);
662 if (Info_h.biWidth % 2)
665 numcomps = gray_scale ? 1 : 3;
666 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
667 /* initialize image components */
668 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
669 for(i = 0; i < numcomps; i++)
671 cmptparm[i].prec = 8;
673 cmptparm[i].sgnd = 0;
674 cmptparm[i].dx = subsampling_dx;
675 cmptparm[i].dy = subsampling_dy;
679 /* create the image */
680 image = opj_image_create(numcomps, &cmptparm[0], color_space);
684 free(table_R); free(table_G); free(table_B);
688 /* set image offset and reference grid */
689 image->x0 = parameters->image_offset_x0;
690 image->y0 = parameters->image_offset_y0;
691 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
692 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
696 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
698 if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
704 opj_image_destroy(image);
705 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
711 for (j = 0; j < W * H; j++)
713 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
715 image->comps[0].data[index] =
716 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
725 for (j = 0; j < W * H; j++)
727 if ((j % W < W - 1 && Info_h.biWidth % 2)
728 || !(Info_h.biWidth % 2))
730 unsigned char pixel_index =
731 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
732 image->comps[0].data[index] = table_R[pixel_index];
733 image->comps[1].data[index] = table_G[pixel_index];
734 image->comps[2].data[index] = table_B[pixel_index];
745 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)//RLE8
747 unsigned char *pix, *beyond;
748 int *gray, *red, *green, *blue;
749 unsigned int x, y, max;
753 if (Info_h.biClrUsed == 0)
754 Info_h.biClrUsed = 256;
755 else if (Info_h.biClrUsed > 256)
756 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);
769 has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
775 numcomps = gray_scale ? 1 : 3;
776 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
777 /* initialize image components */
778 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
779 for (i = 0; i < numcomps; i++)
781 cmptparm[i].prec = 8;
783 cmptparm[i].sgnd = 0;
784 cmptparm[i].dx = subsampling_dx;
785 cmptparm[i].dy = subsampling_dy;
789 /* create the image */
790 image = opj_image_create(numcomps, &cmptparm[0], color_space);
800 /* set image offset and reference grid */
801 image->x0 = parameters->image_offset_x0;
802 image->y0 = parameters->image_offset_y0;
803 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
804 - 1) * subsampling_dx + 1;
805 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
806 - 1) * subsampling_dy + 1;
810 /* Place the cursor at the beginning of the image information */
811 fseek(IN, 0, SEEK_SET);
812 fseek(IN, File_h.bfOffBits, SEEK_SET);
816 RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
817 beyond = RGB + W * H;
829 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
830 *pix = (unsigned char)c1;
836 if (c == 0x00) /* EOL */
840 pix = RGB + x + (H - y - 1) * W;
842 else if (c == 0x01) /* EOP */
844 else if (c == 0x02) /* MOVE by dxdy */
850 pix = RGB + (H - y - 1) * W + x;
855 for (; i < c && x < W && pix < beyond; i++, x++, pix++)
858 *pix = (unsigned char)c1;
860 if (c & 1) /* skip padding byte */
868 gray = image->comps[0].data;
876 *gray++ = table_R[uc];
881 //int *red, *green, *blue;
883 red = image->comps[0].data;
884 green = image->comps[1].data;
885 blue = image->comps[2].data;
893 *red++ = table_R[uc];
894 *green++ = table_G[uc];
895 *blue++ = table_B[uc];
906 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
907 "is not yet implemented [%d]\n", Info_h.biBitCount);
913 int imagetobmp(opj_image_t * image, const char *outfile) {
917 int adjustR, adjustG, adjustB;
919 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
920 && image->comps[1].dx == image->comps[2].dx
921 && image->comps[0].dy == image->comps[1].dy
922 && image->comps[1].dy == image->comps[2].dy
923 && image->comps[0].prec == image->comps[1].prec
924 && image->comps[1].prec == image->comps[2].prec) {
926 /* -->> -->> -->> -->>
928 <<-- <<-- <<-- <<-- */
930 fdest = fopen(outfile, "wb");
932 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
936 w = image->comps[0].w;
937 h = image->comps[0].h;
939 fprintf(fdest, "BM");
943 fprintf(fdest, "%c%c%c%c",
944 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
945 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
946 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
947 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
948 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
949 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
953 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
954 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
955 (unsigned char) ((w) >> 8) & 0xff,
956 (unsigned char) ((w) >> 16) & 0xff,
957 (unsigned char) ((w) >> 24) & 0xff);
958 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
959 (unsigned char) ((h) >> 8) & 0xff,
960 (unsigned char) ((h) >> 16) & 0xff,
961 (unsigned char) ((h) >> 24) & 0xff);
962 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
963 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
964 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
965 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
966 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
967 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
968 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
969 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
970 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
971 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
972 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
974 if (image->comps[0].prec > 8) {
975 adjustR = image->comps[0].prec - 8;
976 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
980 if (image->comps[1].prec > 8) {
981 adjustG = image->comps[1].prec - 8;
982 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
986 if (image->comps[2].prec > 8) {
987 adjustB = image->comps[2].prec - 8;
988 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
993 for (i = 0; i < w * h; i++) {
994 unsigned char rc, gc, bc;
997 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
998 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
999 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1000 if(r > 255) r = 255; else if(r < 0) r = 0;
1001 rc = (unsigned char)r;
1003 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1004 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1005 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
1006 if(g > 255) g = 255; else if(g < 0) g = 0;
1007 gc = (unsigned char)g;
1009 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1010 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1011 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
1012 if(b > 255) b = 255; else if(b < 0) b = 0;
1013 bc = (unsigned char)b;
1015 fprintf(fdest, "%c%c%c", bc, gc, rc);
1017 if ((i + 1) % w == 0) {
1018 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
1019 fprintf(fdest, "%c", 0);
1023 } else { /* Gray-scale */
1025 /* -->> -->> -->> -->>
1026 8 bits non code (Gray scale)
1027 <<-- <<-- <<-- <<-- */
1029 fdest = fopen(outfile, "wb");
1030 w = image->comps[0].w;
1031 h = image->comps[0].h;
1033 fprintf(fdest, "BM");
1037 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
1038 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
1039 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
1040 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
1041 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1042 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
1043 ((54 + 1024) >> 16) & 0xff,
1044 ((54 + 1024) >> 24) & 0xff);
1048 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
1049 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
1050 (unsigned char) ((w) >> 8) & 0xff,
1051 (unsigned char) ((w) >> 16) & 0xff,
1052 (unsigned char) ((w) >> 24) & 0xff);
1053 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
1054 (unsigned char) ((h) >> 8) & 0xff,
1055 (unsigned char) ((h) >> 16) & 0xff,
1056 (unsigned char) ((h) >> 24) & 0xff);
1057 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1058 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1059 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
1060 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
1061 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
1062 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
1063 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
1064 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1065 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1066 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1067 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1069 if (image->comps[0].prec > 8) {
1070 adjustR = image->comps[0].prec - 8;
1071 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1075 for (i = 0; i < 256; i++) {
1076 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1079 for (i = 0; i < w * h; i++) {
1082 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1083 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1084 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1085 if(r > 255) r = 255; else if(r < 0) r = 0;
1087 fprintf(fdest, "%c", (unsigned char)r);
1089 if ((i + 1) % w == 0) {
1090 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1091 fprintf(fdest, "%c", 0);
1100 /* -->> -->> -->> -->>
1104 <<-- <<-- <<-- <<-- */
1107 unsigned char readuchar(FILE * f)
1110 if ( !fread(&c1, 1, 1, f) )
1112 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1118 unsigned short readushort(FILE * f, int bigendian)
1120 unsigned char c1, c2;
1121 if ( !fread(&c1, 1, 1, f) )
1123 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1126 if ( !fread(&c2, 1, 1, f) )
1128 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1132 return (c1 << 8) + c2;
1134 return (c2 << 8) + c1;
1137 unsigned int readuint(FILE * f, int bigendian)
1139 unsigned char c1, c2, c3, c4;
1140 if ( !fread(&c1, 1, 1, f) )
1142 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1145 if ( !fread(&c2, 1, 1, f) )
1147 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1150 if ( !fread(&c3, 1, 1, f) )
1152 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1155 if ( !fread(&c4, 1, 1, f) )
1157 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1161 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1163 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1166 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1169 int i, numcomps, max;
1170 OPJ_COLOR_SPACE color_space;
1171 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1172 opj_image_t * image = NULL;
1174 char endian1,endian2,sign;
1179 opj_image_comp_t *comp = NULL;
1182 color_space = CLRSPC_GRAY;
1184 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1188 f = fopen(filename, "rb");
1190 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1194 fseek(f, 0, SEEK_SET);
1195 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1196 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1202 while (signtmp[i]!='\0') {
1203 if (signtmp[i]=='-') sign='-';
1208 if (endian1=='M' && endian2=='L') {
1210 } else if (endian2=='M' && endian1=='L') {
1213 fprintf(stderr, "Bad pgx header, please check input file\n");
1217 /* initialize image component */
1219 cmptparm.x0 = parameters->image_offset_x0;
1220 cmptparm.y0 = parameters->image_offset_y0;
1221 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1222 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1229 cmptparm.prec = prec;
1230 cmptparm.bpp = prec;
1231 cmptparm.dx = parameters->subsampling_dx;
1232 cmptparm.dy = parameters->subsampling_dy;
1234 /* create the image */
1235 image = opj_image_create(numcomps, &cmptparm, color_space);
1240 /* set image offset and reference grid */
1241 image->x0 = cmptparm.x0;
1242 image->y0 = cmptparm.x0;
1243 image->x1 = cmptparm.w;
1244 image->y1 = cmptparm.h;
1246 /* set image data */
1248 comp = &image->comps[0];
1250 for (i = 0; i < w * h; i++) {
1252 if (comp->prec <= 8) {
1256 v = (char) readuchar(f);
1258 } else if (comp->prec <= 16) {
1260 v = readushort(f, bigendian);
1262 v = (short) readushort(f, bigendian);
1266 v = readuint(f, bigendian);
1268 v = (int) readuint(f, bigendian);
1276 comp->bpp = int_floorlog2(max) + 1;
1281 int imagetopgx(opj_image_t * image, const char *outfile) {
1286 for (compno = 0; compno < image->numcomps; compno++) {
1287 opj_image_comp_t *comp = &image->comps[compno];
1288 char bname[256]; /* buffer for name */
1289 char *name = bname; /* pointer */
1291 const size_t olen = strlen(outfile);
1292 const size_t dotpos = olen - 4;
1293 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1294 if( outfile[dotpos] != '.' ) {
1295 /* `pgx` was recognized but there is no dot at expected position */
1296 fprintf(stderr, "ERROR -> Impossible happen." );
1300 name = (char*)malloc(total+1);
1302 strncpy(name, outfile, dotpos);
1303 //if (image->numcomps > 1) {
1304 sprintf(name+dotpos, "_%d.pgx", compno);
1306 strcpy(name+dotpos, ".pgx");
1308 fdest = fopen(name, "wb");
1310 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1313 /* dont need name anymore */
1318 w = image->comps[compno].w;
1319 h = image->comps[compno].h;
1321 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1322 if (comp->prec <= 8) {
1324 } else if (comp->prec <= 16) {
1329 for (i = 0; i < w * h; i++) {
1330 int v = image->comps[compno].data[i];
1331 for (j = nbytes - 1; j >= 0; j--) {
1332 char byte = (char) (v >> (j * 8));
1333 fwrite(&byte, 1, 1, fdest);
1342 /* -->> -->> -->> -->>
1346 <<-- <<-- <<-- <<-- */
1350 int width, height, maxval, depth, format;
1351 char rgb, rgba, gray, graya, bw;
1355 static char *skip_white(char *s)
1359 if(*s == '\n' || *s == '\r') return NULL;
1360 if(isspace(*s)) { ++s; continue; }
1366 static char *skip_int(char *start, int *out_n)
1371 *out_n = 0; s = start;
1373 s = skip_white(start);
1374 if(s == NULL) return NULL;
1379 if( !isdigit(*s)) break;
1382 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1386 static char *skip_idf(char *start, char out_idf[256])
1391 s = skip_white(start);
1392 if(s == NULL) return NULL;
1397 if(isalpha(*s) || *s == '_') { ++s; continue; }
1400 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1404 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1407 int format, have_wh, end, ttype;
1408 char idf[256], type[256];
1411 if (fgets(line, 250, reader) == NULL)
1413 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1419 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1421 format = atoi(line + 1);
1422 if(format < 1 || format > 7)
1424 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1427 ph->format = format;
1428 ttype = end = have_wh = 0;
1430 while(fgets(line, 250, reader))
1432 if(*line == '#') continue;
1438 s = skip_idf(s, idf);
1440 if(s == NULL || *s == 0) return;
1442 if(strcmp(idf, "ENDHDR") == 0)
1446 if(strcmp(idf, "WIDTH") == 0)
1448 s = skip_int(s, &ph->width);
1449 if(s == NULL || *s == 0) return;
1453 if(strcmp(idf, "HEIGHT") == 0)
1455 s = skip_int(s, &ph->height);
1456 if(s == NULL || *s == 0) return;
1460 if(strcmp(idf, "DEPTH") == 0)
1462 s = skip_int(s, &ph->depth);
1463 if(s == NULL || *s == 0) return;
1467 if(strcmp(idf, "MAXVAL") == 0)
1469 s = skip_int(s, &ph->maxval);
1470 if(s == NULL || *s == 0) return;
1474 if(strcmp(idf, "TUPLTYPE") == 0)
1476 s = skip_idf(s, type);
1477 if(s == NULL || *s == 0) return;
1479 if(strcmp(type, "BLACKANDWHITE") == 0)
1481 ph->bw = 1; ttype = 1; continue;
1483 if(strcmp(type, "GRAYSCALE") == 0)
1485 ph->gray = 1; ttype = 1; continue;
1487 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1489 ph->graya = 1; ttype = 1; continue;
1491 if(strcmp(type, "RGB") == 0)
1493 ph->rgb = 1; ttype = 1; continue;
1495 if(strcmp(type, "RGB_ALPHA") == 0)
1497 ph->rgba = 1; ttype = 1; continue;
1499 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1502 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1504 } /* if(format == 7) */
1508 s = skip_int(s, &ph->width);
1510 s = skip_int(s, &ph->height);
1514 if(format == 1 || format == 4) break;
1518 if(format == 2 || format == 3 || format == 5 || format == 6)
1520 /* P2, P3, P5, P6: */
1521 s = skip_int(s, &ph->maxval);
1523 if(ph->maxval > 65535) return;
1526 }/* while(fgets( ) */
1527 if(format == 2 || format == 3 || format > 4)
1529 if(ph->maxval < 1 || ph->maxval > 65535) return;
1531 if(ph->width < 1 || ph->height < 1) return;
1537 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1539 if(ph->depth < 1 || ph->depth > 4) return;
1541 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1546 if(format != 1 && format != 4)
1548 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1552 if(ph->width && ph->height) ph->ok = 1;
1558 static int has_prec(int val)
1560 if(val < 2) return 1;
1561 if(val < 4) return 2;
1562 if(val < 8) return 3;
1563 if(val < 16) return 4;
1564 if(val < 32) return 5;
1565 if(val < 64) return 6;
1566 if(val < 128) return 7;
1567 if(val < 256) return 8;
1568 if(val < 512) return 9;
1569 if(val < 1024) return 10;
1570 if(val < 2048) return 11;
1571 if(val < 4096) return 12;
1572 if(val < 8192) return 13;
1573 if(val < 16384) return 14;
1574 if(val < 32768) return 15;
1578 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1579 int subsampling_dx = parameters->subsampling_dx;
1580 int subsampling_dy = parameters->subsampling_dy;
1583 int i, compno, numcomps, w, h, prec, format;
1584 OPJ_COLOR_SPACE color_space;
1585 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1586 opj_image_t * image = NULL;
1587 struct pnm_header header_info;
1589 if((fp = fopen(filename, "rb")) == NULL)
1591 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1594 memset(&header_info, 0, sizeof(struct pnm_header));
1596 read_pnm_header(fp, &header_info);
1598 if(!header_info.ok) { fclose(fp); return NULL; }
1600 format = header_info.format;
1604 case 1: /* ascii bitmap */
1605 case 4: /* raw bitmap */
1609 case 2: /* ascii greymap */
1610 case 5: /* raw greymap */
1614 case 3: /* ascii pixmap */
1615 case 6: /* raw pixmap */
1619 case 7: /* arbitrary map */
1620 numcomps = header_info.depth;
1623 default: fclose(fp); return NULL;
1626 color_space = CLRSPC_GRAY;/* GRAY, GRAYA */
1628 color_space = CLRSPC_SRGB;/* RGB, RGBA */
1630 prec = has_prec(header_info.maxval);
1632 if(prec < 8) prec = 8;
1634 w = header_info.width;
1635 h = header_info.height;
1636 subsampling_dx = parameters->subsampling_dx;
1637 subsampling_dy = parameters->subsampling_dy;
1639 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1641 for(i = 0; i < numcomps; i++)
1643 cmptparm[i].prec = prec;
1644 cmptparm[i].bpp = prec;
1645 cmptparm[i].sgnd = 0;
1646 cmptparm[i].dx = subsampling_dx;
1647 cmptparm[i].dy = subsampling_dy;
1651 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1653 if(!image) { fclose(fp); return NULL; }
1655 /* set image offset and reference grid */
1656 image->x0 = parameters->image_offset_x0;
1657 image->y0 = parameters->image_offset_y0;
1658 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1659 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1661 if((format == 2) || (format == 3)) /* ascii pixmap */
1665 for (i = 0; i < w * h; i++)
1667 for(compno = 0; compno < numcomps; compno++)
1670 if (fscanf(fp, "%u", &index) != 1)
1671 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1673 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1681 && ( header_info.gray || header_info.graya
1682 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1684 unsigned char c0, c1, one;
1688 for (i = 0; i < w * h; i++)
1690 for(compno = 0; compno < numcomps; compno++)
1692 if ( !fread(&c0, 1, 1, fp) )
1693 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1696 image->comps[compno].data[i] = c0;
1700 if ( !fread(&c1, 1, 1, fp) )
1701 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1703 image->comps[compno].data[i] = ((c0<<8) | c1);
1709 if(format == 1) /* ascii bitmap */
1711 for (i = 0; i < w * h; i++)
1715 if ( fscanf(fp, "%u", &index) != 1)
1716 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1718 image->comps[0].data[i] = (index?0:255);
1728 for(y = 0; y < h; ++y)
1732 for(x = 0; x < w; ++x)
1737 uc = (unsigned char)getc(fp);
1739 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1745 if((format == 7 && header_info.bw)) //MONO
1749 for(i = 0; i < w * h; ++i)
1751 if ( !fread(&uc, 1, 1, fp) )
1752 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1753 image->comps[0].data[i] = (uc & 1)?0:255;
1761 int imagetopnm(opj_image_t * image, const char *outfile)
1763 int *red, *green, *blue, *alpha;
1765 int i, compno, ncomp;
1766 int adjustR, adjustG, adjustB, adjustA;
1767 int fails, two, want_gray, has_alpha, triple;
1770 const char *tmp = outfile;
1773 if((prec = image->comps[0].prec) > 16)
1775 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1776 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1779 two = has_alpha = 0; fails = 1;
1780 ncomp = image->numcomps;
1782 while (*tmp) ++tmp; tmp -= 2;
1783 want_gray = (*tmp == 'g' || *tmp == 'G');
1784 ncomp = image->numcomps;
1786 if(want_gray) ncomp = 1;
1788 if (ncomp == 2 /* GRAYA */
1789 || (ncomp > 2 /* RGB, RGBA */
1790 && image->comps[0].dx == image->comps[1].dx
1791 && image->comps[1].dx == image->comps[2].dx
1792 && image->comps[0].dy == image->comps[1].dy
1793 && image->comps[1].dy == image->comps[2].dy
1794 && image->comps[0].prec == image->comps[1].prec
1795 && image->comps[1].prec == image->comps[2].prec
1798 fdest = fopen(outfile, "wb");
1802 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1806 triple = (ncomp > 2);
1807 wr = image->comps[0].w; hr = image->comps[0].h;
1808 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1810 red = image->comps[0].data;
1814 green = image->comps[1].data;
1815 blue = image->comps[2].data;
1817 else green = blue = NULL;
1821 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1823 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1824 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1825 wr, hr, ncomp, max, tt);
1826 alpha = image->comps[ncomp - 1].data;
1827 adjustA = (image->comps[ncomp - 1].sgnd ?
1828 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1832 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1833 opj_version(), wr, hr, max);
1834 alpha = NULL; adjustA = 0;
1836 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1840 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1841 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1843 else adjustG = adjustB = 0;
1845 for(i = 0; i < wr * hr; ++i)
1849 v = *red + adjustR; ++red;
1851 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1855 v = *green + adjustG; ++green;
1857 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1859 v = *blue + adjustB; ++blue;
1861 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1867 v = *alpha + adjustA; ++alpha;
1869 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1877 fprintf(fdest, "%c", (unsigned char)*red++);
1879 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
1882 fprintf(fdest, "%c", (unsigned char)*alpha++);
1886 fclose(fdest); return 0;
1891 if (image->numcomps > ncomp)
1893 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1894 fprintf(stderr," is written to the file\n");
1896 destname = (char*)malloc(strlen(outfile) + 8);
1898 for (compno = 0; compno < ncomp; compno++)
1901 sprintf(destname, "%d.%s", compno, outfile);
1903 sprintf(destname, "%s", outfile);
1905 fdest = fopen(destname, "wb");
1908 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1912 wr = image->comps[compno].w; hr = image->comps[compno].h;
1913 prec = image->comps[compno].prec;
1914 max = (1<<prec) - 1;
1916 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
1917 opj_version(), wr, hr, max);
1919 red = image->comps[compno].data;
1921 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1925 for (i = 0; i < wr * hr; i++)
1927 v = *red + adjustR; ++red;
1929 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1935 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1939 else /* prec <= 8 */
1941 for(i = 0; i < wr * hr; ++i)
1943 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
1954 /* -->> -->> -->> -->>
1958 <<-- <<-- <<-- <<-- */
1960 typedef struct tiff_infoheader{
1961 DWORD tiWidth; // Width of Image in pixel
1962 DWORD tiHeight; // Height of Image in pixel
1963 DWORD tiPhoto; // Photometric
1964 WORD tiBps; // Bits per sample
1965 WORD tiSf; // Sample Format
1966 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1967 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1970 int imagetotif(opj_image_t * image, const char *outfile)
1972 int width, height, imgsize;
1973 int bps,index,adjust, sgnd;
1974 int ushift, dshift, has_alpha, force16;
1980 ushift = dshift = force16 = has_alpha = 0;
1981 bps = image->comps[0].prec;
1983 if(bps > 8 && bps < 16)
1985 ushift = 16 - bps; dshift = bps - ushift;
1986 bps = 16; force16 = 1;
1989 if(bps != 8 && bps != 16)
1991 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
1993 fprintf(stderr,"\tAborting\n");
1996 tif = TIFFOpen(outfile, "wb");
2000 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2003 sgnd = image->comps[0].sgnd;
2004 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2006 if(image->numcomps >= 3
2007 && image->comps[0].dx == image->comps[1].dx
2008 && image->comps[1].dx == image->comps[2].dx
2009 && image->comps[0].dy == image->comps[1].dy
2010 && image->comps[1].dy == image->comps[2].dy
2011 && image->comps[0].prec == image->comps[1].prec
2012 && image->comps[1].prec == image->comps[2].prec)
2014 has_alpha = (image->numcomps == 4);
2016 width = image->comps[0].w;
2017 height = image->comps[0].h;
2018 imgsize = width * height ;
2020 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2021 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2022 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2023 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2024 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2025 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2026 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2027 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2028 strip_size = TIFFStripSize(tif);
2029 buf = _TIFFmalloc(strip_size);
2032 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2034 unsigned char *dat8;
2035 tsize_t i, ssize, last_i = 0;
2037 ssize = TIFFStripSize(tif);
2038 dat8 = (unsigned char*)buf;
2042 step = 3 + has_alpha;
2045 for(i=0; i < ssize - restx; i += step)
2051 r = image->comps[0].data[index];
2052 g = image->comps[1].data[index];
2053 b = image->comps[2].data[index];
2054 if(has_alpha) a = image->comps[3].data[index];
2061 if(has_alpha) a += adjust;
2066 if(has_alpha) dat8[i+3] = a;
2077 for(i = last_i; i < ssize; i += step)
2083 r = image->comps[0].data[index];
2084 g = image->comps[1].data[index];
2085 b = image->comps[2].data[index];
2086 if(has_alpha) a = image->comps[3].data[index];
2093 if(has_alpha) a += adjust;
2096 if(i+1 < ssize) dat8[i+1] = g ; else break;
2097 if(i+2 < ssize) dat8[i+2] = b ; else break;
2100 if(i+3 < ssize) dat8[i+3] = a ; else break;
2107 }//if(last_i < ssize)
2113 step = 6 + has_alpha + has_alpha;
2116 for(i = 0; i < ssize - restx ; i += step)
2122 r = image->comps[0].data[index];
2123 g = image->comps[1].data[index];
2124 b = image->comps[2].data[index];
2125 if(has_alpha) a = image->comps[3].data[index];
2132 if(has_alpha) a += adjust;
2136 r = (r<<ushift) + (r>>dshift);
2137 g = (g<<ushift) + (g>>dshift);
2138 b = (b<<ushift) + (b>>dshift);
2139 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2142 dat8[i+1] = (r >> 8);//MSB
2144 dat8[i+3] = (g >> 8);
2146 dat8[i+5] = (b >> 8);
2150 dat8[i+7] = (a >> 8);
2161 for(i = last_i ; i < ssize ; i += step)
2167 r = image->comps[0].data[index];
2168 g = image->comps[1].data[index];
2169 b = image->comps[2].data[index];
2170 if(has_alpha) a = image->comps[3].data[index];
2177 if(has_alpha) a += adjust;
2181 r = (r<<ushift) + (r>>dshift);
2182 g = (g<<ushift) + (g>>dshift);
2183 b = (b<<ushift) + (b>>dshift);
2184 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2187 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB
2188 if(i+2 < ssize) dat8[i+2] = g; else break;
2189 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2190 if(i+4 < ssize) dat8[i+4] = b; else break;
2191 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2195 if(i+6 < ssize) dat8[i+6] = a; else break;
2196 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2203 }//if(last_i < ssize)
2206 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2209 _TIFFfree((void*)buf);
2215 if(image->numcomps == 1 /* GRAY */
2216 || ( image->numcomps == 2 /* GRAY_ALPHA */
2217 && image->comps[0].dx == image->comps[1].dx
2218 && image->comps[0].dy == image->comps[1].dy
2219 && image->comps[0].prec == image->comps[1].prec))
2223 has_alpha = (image->numcomps == 2);
2225 width = image->comps[0].w;
2226 height = image->comps[0].h;
2227 imgsize = width * height;
2230 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2231 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2232 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2233 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2234 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2235 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2236 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2237 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2239 /* Get a buffer for the data */
2240 strip_size = TIFFStripSize(tif);
2241 buf = _TIFFmalloc(strip_size);
2244 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2246 unsigned char *dat8;
2247 tsize_t i, ssize = TIFFStripSize(tif);
2248 dat8 = (unsigned char*)buf;
2252 step = 1 + has_alpha;
2254 for(i=0; i < ssize; i += step)
2260 r = image->comps[0].data[index];
2261 if(has_alpha) a = image->comps[1].data[index];
2266 if(has_alpha) a += adjust;
2269 if(has_alpha) dat8[i+1] = a;
2279 step = 2 + has_alpha + has_alpha;
2281 for(i=0; i < ssize; i += step)
2287 r = image->comps[0].data[index];
2288 if(has_alpha) a = image->comps[1].data[index];
2293 if(has_alpha) a += adjust;
2297 r = (r<<ushift) + (r>>dshift);
2298 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2301 dat8[i+1] = r >> 8;//MSB
2308 }//if(index < imgsize)
2313 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2324 fprintf(stderr,"imagetotif: Bad color format.\n"
2325 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2326 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2333 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2334 * CINEMA : 12 bit precision
2336 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2338 int subsampling_dx = parameters->subsampling_dx;
2339 int subsampling_dy = parameters->subsampling_dy;
2341 tiff_infoheader_t Info;
2345 int j, numcomps, w, h,index;
2346 OPJ_COLOR_SPACE color_space;
2347 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2348 opj_image_t *image = NULL;
2352 tif = TIFFOpen(filename, "r");
2356 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2359 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
2360 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
2361 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
2362 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
2363 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
2365 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
2366 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
2373 if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
2374 if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
2379 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2380 " implemented\n",Info.tiBps);
2383 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2384 " and GRAY(A) has been implemented\n",(int) Info.tiPhoto);
2386 fprintf(stderr,"\tAborting\n");
2393 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2395 uint16 extrasamples;
2397 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2398 &extrasamples, &sampleinfo);
2400 if(extrasamples >= 1)
2402 switch(sampleinfo[0])
2404 case EXTRASAMPLE_UNSPECIFIED:
2405 /* Workaround for some images without correct info about alpha channel
2411 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2412 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2418 /* initialize image components
2420 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2422 if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2424 numcomps = 3 + has_alpha;
2425 color_space = CLRSPC_SRGB;
2427 for(j = 0; j < numcomps; j++)
2429 if(parameters->cp_cinema)
2431 cmptparm[j].prec = 12;
2432 cmptparm[j].bpp = 12;
2436 cmptparm[j].prec = Info.tiBps;
2437 cmptparm[j].bpp = Info.tiBps;
2439 cmptparm[j].dx = subsampling_dx;
2440 cmptparm[j].dy = subsampling_dy;
2445 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2452 /* set image offset and reference grid
2454 image->x0 = parameters->image_offset_x0;
2455 image->y0 = parameters->image_offset_y0;
2456 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2457 image->x0 + (w - 1) * subsampling_dx + 1;
2458 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2459 image->y0 + (h - 1) * subsampling_dy + 1;
2461 buf = _TIFFmalloc(TIFFStripSize(tif));
2463 strip_size=TIFFStripSize(tif);
2465 imgsize = image->comps[0].w * image->comps[0].h ;
2466 /* Read the Image components
2468 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2470 unsigned char *dat8;
2473 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2474 dat8 = (unsigned char*)buf;
2476 if(Info.tiBps == 16)
2478 step = 6 + has_alpha + has_alpha;
2480 for(i = 0; i < ssize; i += step)
2484 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
2485 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
2486 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
2488 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2490 if(parameters->cp_cinema)
2492 /* Rounding 16 to 12 bits
2494 image->comps[0].data[index] =
2495 (image->comps[0].data[index] + 0x08) >> 4 ;
2496 image->comps[1].data[index] =
2497 (image->comps[1].data[index] + 0x08) >> 4 ;
2498 image->comps[2].data[index] =
2499 (image->comps[2].data[index] + 0x08) >> 4 ;
2501 image->comps[3].data[index] =
2502 (image->comps[3].data[index] + 0x08) >> 4 ;
2509 }//if(Info.tiBps == 16)
2513 step = 3 + has_alpha;
2515 for(i = 0; i < ssize; i += step)
2519 image->comps[0].data[index] = dat8[i+0];// R
2520 image->comps[1].data[index] = dat8[i+1];// G
2521 image->comps[2].data[index] = dat8[i+2];// B
2523 image->comps[3].data[index] = dat8[i+3];
2525 if(parameters->cp_cinema)
2527 /* Rounding 8 to 12 bits
2529 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2530 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2531 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2533 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2540 }//if( Info.tiBps == 8)
2542 if(Info.tiBps == 12)/* CINEMA file */
2546 for(i = 0; i < ssize; i += step)
2548 if((index < imgsize)&(index+1 < imgsize))
2550 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2551 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2553 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2554 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2556 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2557 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2573 if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2575 numcomps = 1 + has_alpha;
2576 color_space = CLRSPC_GRAY;
2578 for(j = 0; j < numcomps; ++j)
2580 cmptparm[j].prec = Info.tiBps;
2581 cmptparm[j].bpp = Info.tiBps;
2582 cmptparm[j].dx = subsampling_dx;
2583 cmptparm[j].dy = subsampling_dy;
2587 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2594 /* set image offset and reference grid
2596 image->x0 = parameters->image_offset_x0;
2597 image->y0 = parameters->image_offset_y0;
2598 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2599 image->x0 + (w - 1) * subsampling_dx + 1;
2600 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2601 image->y0 + (h - 1) * subsampling_dy + 1;
2603 buf = _TIFFmalloc(TIFFStripSize(tif));
2605 strip_size = TIFFStripSize(tif);
2607 imgsize = image->comps[0].w * image->comps[0].h ;
2608 /* Read the Image components
2610 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2612 unsigned char *dat8;
2616 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2617 dat8 = (unsigned char*)buf;
2619 if(Info.tiBps == 16)
2621 step = 2 + has_alpha + has_alpha;
2623 for(i = 0; i < ssize; i += step)
2627 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2629 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2639 step = 1 + has_alpha;
2641 for(i = 0; i < ssize; i += step)
2645 image->comps[0].data[index] = dat8[i+0];
2647 image->comps[1].data[index] = dat8[i+1];
2665 #endif /* HAVE_LIBTIFF */
2667 /* -->> -->> -->> -->>
2671 <<-- <<-- <<-- <<-- */
2673 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2674 int subsampling_dx = parameters->subsampling_dx;
2675 int subsampling_dy = parameters->subsampling_dy;
2678 int i, compno, numcomps, w, h;
2679 OPJ_COLOR_SPACE color_space;
2680 opj_image_cmptparm_t *cmptparm;
2681 opj_image_t * image = NULL;
2684 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2686 fprintf(stderr,"\nError: invalid raw image parameters\n");
2687 fprintf(stderr,"Please use the Format option -F:\n");
2688 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2689 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2690 fprintf(stderr,"Aborting\n");
2694 f = fopen(filename, "rb");
2696 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2697 fprintf(stderr,"Aborting\n");
2700 numcomps = raw_cp->rawComp;
2701 color_space = CLRSPC_SRGB;
2702 w = raw_cp->rawWidth;
2703 h = raw_cp->rawHeight;
2704 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2706 /* initialize image components */
2707 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2708 for(i = 0; i < numcomps; i++) {
2709 cmptparm[i].prec = raw_cp->rawBitDepth;
2710 cmptparm[i].bpp = raw_cp->rawBitDepth;
2711 cmptparm[i].sgnd = raw_cp->rawSigned;
2712 cmptparm[i].dx = subsampling_dx;
2713 cmptparm[i].dy = subsampling_dy;
2717 /* create the image */
2718 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2723 /* set image offset and reference grid */
2724 image->x0 = parameters->image_offset_x0;
2725 image->y0 = parameters->image_offset_y0;
2726 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2727 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2729 if(raw_cp->rawBitDepth <= 8)
2731 unsigned char value = 0;
2732 for(compno = 0; compno < numcomps; compno++) {
2733 for (i = 0; i < w * h; i++) {
2734 if (!fread(&value, 1, 1, f)) {
2735 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2738 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2742 else if(raw_cp->rawBitDepth <= 16)
2744 unsigned short value;
2745 for(compno = 0; compno < numcomps; compno++) {
2746 for (i = 0; i < w * h; i++) {
2748 if (!fread(&temp, 1, 1, f)) {
2749 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2753 if (!fread(&temp, 1, 1, f)) {
2754 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2758 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2763 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2767 if (fread(&ch, 1, 1, f)) {
2768 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2775 int imagetoraw(opj_image_t * image, const char *outfile)
2777 FILE *rawFile = NULL;
2783 if((image->numcomps * image->x1 * image->y1) == 0)
2785 fprintf(stderr,"\nError: invalid raw image parameters\n");
2789 rawFile = fopen(outfile, "wb");
2791 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2795 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2797 for(compno = 0; compno < image->numcomps; compno++)
2799 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2800 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2802 w = image->comps[compno].w;
2803 h = image->comps[compno].h;
2805 if(image->comps[compno].prec <= 8)
2807 if(image->comps[compno].sgnd == 1)
2810 int mask = (1 << image->comps[compno].prec) - 1;
2811 ptr = image->comps[compno].data;
2812 for (line = 0; line < h; line++) {
2813 for(row = 0; row < w; row++) {
2814 curr = (signed char) (*ptr & mask);
2815 fwrite(&curr, sizeof(signed char), 1, rawFile);
2820 else if(image->comps[compno].sgnd == 0)
2823 int mask = (1 << image->comps[compno].prec) - 1;
2824 ptr = image->comps[compno].data;
2825 for (line = 0; line < h; line++) {
2826 for(row = 0; row < w; row++) {
2827 curr = (unsigned char) (*ptr & mask);
2828 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2834 else if(image->comps[compno].prec <= 16)
2836 if(image->comps[compno].sgnd == 1)
2838 signed short int curr;
2839 int mask = (1 << image->comps[compno].prec) - 1;
2840 ptr = image->comps[compno].data;
2841 for (line = 0; line < h; line++) {
2842 for(row = 0; row < w; row++) {
2844 curr = (signed short int) (*ptr & mask);
2845 temp = (unsigned char) (curr >> 8);
2846 fwrite(&temp, 1, 1, rawFile);
2847 temp = (unsigned char) curr;
2848 fwrite(&temp, 1, 1, rawFile);
2853 else if(image->comps[compno].sgnd == 0)
2855 unsigned short int curr;
2856 int mask = (1 << image->comps[compno].prec) - 1;
2857 ptr = image->comps[compno].data;
2858 for (line = 0; line < h; line++) {
2859 for(row = 0; row < w; row++) {
2861 curr = (unsigned short int) (*ptr & mask);
2862 temp = (unsigned char) (curr >> 8);
2863 fwrite(&temp, 1, 1, rawFile);
2864 temp = (unsigned char) curr;
2865 fwrite(&temp, 1, 1, rawFile);
2871 else if (image->comps[compno].prec <= 32)
2873 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2878 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2888 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
2889 #define MAGIC_SIZE 8
2890 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
2892 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
2896 double gamma, display_exponent;
2897 int bit_depth, interlace_type,compression_type, filter_type;
2899 png_uint_32 resx, resy;
2901 png_uint_32 width, height;
2902 int color_type, has_alpha, is16;
2905 unsigned char **rows;
2908 opj_image_cmptparm_t cmptparm[4];
2910 unsigned int nr_comp;
2912 unsigned char sigbuf[8];
2914 if((reader = fopen(read_idf, "rb")) == NULL)
2916 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
2919 image = NULL; png = NULL; rows = NULL;
2921 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
2922 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
2924 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
2927 /* libpng-VERSION/example.c:
2928 * PC : screen_gamma = 2.2;
2929 * Mac: screen_gamma = 1.7 or 1.0;
2931 display_exponent = 2.2;
2933 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
2934 NULL, NULL, NULL)) == NULL)
2936 if((info = png_create_info_struct(png)) == NULL)
2939 if(setjmp(png_jmpbuf(png)))
2942 png_init_io(png, reader);
2943 png_set_sig_bytes(png, MAGIC_SIZE);
2945 png_read_info(png, info);
2947 if(png_get_IHDR(png, info, &width, &height,
2948 &bit_depth, &color_type, &interlace_type,
2949 &compression_type, &filter_type) == 0)
2952 /* png_set_expand():
2953 * expand paletted images to RGB, expand grayscale images of
2954 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
2955 * to alpha channels.
2957 if(color_type == PNG_COLOR_TYPE_PALETTE)
2958 png_set_expand(png);
2960 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2961 png_set_expand(png);
2963 if(png_get_valid(png, info, PNG_INFO_tRNS))
2964 png_set_expand(png);
2966 is16 = (bit_depth == 16);
2968 /* GRAY => RGB; GRAY_ALPHA => RGBA
2970 if(color_type == PNG_COLOR_TYPE_GRAY
2971 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2973 png_set_gray_to_rgb(png);
2975 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
2976 PNG_COLOR_TYPE_RGB_ALPHA);
2978 if( !png_get_gAMA(png, info, &gamma))
2981 png_set_gamma(png, display_exponent, gamma);
2983 png_read_update_info(png, info);
2985 png_get_pHYs(png, info, &resx, &resy, &unit);
2987 color_type = png_get_color_type(png, info);
2989 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
2991 nr_comp = 3 + has_alpha;
2993 bit_depth = png_get_bit_depth(png, info);
2995 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
2996 for(i = 0; i < height; ++i)
2997 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
2999 png_read_image(png, rows);
3001 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3003 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3005 for(i = 0; i < nr_comp; ++i)
3007 cmptparm[i].prec = bit_depth;
3008 /* bits_per_pixel: 8 or 16 */
3009 cmptparm[i].bpp = bit_depth;
3010 cmptparm[i].sgnd = 0;
3011 cmptparm[i].dx = sub_dx;
3012 cmptparm[i].dy = sub_dy;
3013 cmptparm[i].w = width;
3014 cmptparm[i].h = height;
3017 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3019 if(image == NULL) goto fin;
3021 image->x0 = params->image_offset_x0;
3022 image->y0 = params->image_offset_y0;
3023 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3024 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3026 r = image->comps[0].data;
3027 g = image->comps[1].data;
3028 b = image->comps[2].data;
3029 a = image->comps[3].data;
3031 for(i = 0; i < height; ++i)
3035 for(j = 0; j < width; ++j)
3039 *r++ = s[0]<<8|s[1]; s += 2;
3041 *g++ = s[0]<<8|s[1]; s += 2;
3043 *b++ = s[0]<<8|s[1]; s += 2;
3045 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3049 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3051 if(has_alpha) *a++ = *s++;
3057 for(i = 0; i < height; ++i)
3062 png_destroy_read_struct(&png, &info, NULL);
3070 int imagetopng(opj_image_t * image, const char *write_idf)
3075 int *red, *green, *blue, *alpha;
3076 unsigned char *row_buf, *d;
3077 int has_alpha, width, height, nr_comp, color_type;
3078 int adjustR, adjustG, adjustB, x, y, fails, is16, force16;
3079 int opj_prec, prec, ushift, dshift;
3080 unsigned short mask = 0xffff;
3081 png_color_8 sig_bit;
3083 is16 = force16 = ushift = dshift = 0; fails = 1;
3084 prec = opj_prec = image->comps[0].prec;
3086 if(prec > 8 && prec < 16)
3088 prec = 16; force16 = 1;
3090 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3092 fprintf(stderr,"imagetopng: can not create %s"
3093 "\n\twrong bit_depth %d\n", write_idf, prec);
3096 writer = fopen(write_idf, "wb");
3098 if(writer == NULL) return fails;
3100 info = NULL; has_alpha = 0;
3102 /* Create and initialize the png_struct with the desired error handler
3103 * functions. If you want to use the default stderr and longjump method,
3104 * you can supply NULL for the last three parameters. We also check that
3105 * the library version is compatible with the one used at compile time,
3106 * in case we are using dynamically linked libraries. REQUIRED.
3108 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3110 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3112 if(png == NULL) goto fin;
3114 /* Allocate/initialize the image information data. REQUIRED
3116 info = png_create_info_struct(png);
3118 if(info == NULL) goto fin;
3120 /* Set error handling. REQUIRED if you are not supplying your own
3121 * error handling functions in the png_create_write_struct() call.
3123 if(setjmp(png_jmpbuf(png))) goto fin;
3125 /* I/O initialization functions is REQUIRED
3127 png_init_io(png, writer);
3129 /* Set the image information here. Width and height are up to 2^31,
3130 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3131 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3132 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3133 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3134 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3135 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3138 png_set_compression_level(png, Z_BEST_COMPRESSION);
3140 if(prec == 16) mask = 0xffff;
3142 if(prec == 8) mask = 0x00ff;
3144 if(prec == 4) mask = 0x000f;
3146 if(prec == 2) mask = 0x0003;
3148 if(prec == 1) mask = 0x0001;
3150 nr_comp = image->numcomps;
3153 && image->comps[0].dx == image->comps[1].dx
3154 && image->comps[1].dx == image->comps[2].dx
3155 && image->comps[0].dy == image->comps[1].dy
3156 && image->comps[1].dy == image->comps[2].dy
3157 && image->comps[0].prec == image->comps[1].prec
3158 && image->comps[1].prec == image->comps[2].prec)
3162 has_alpha = (nr_comp > 3);
3164 is16 = (prec == 16);
3166 width = image->comps[0].w;
3167 height = image->comps[0].h;
3169 red = image->comps[0].data;
3170 green = image->comps[1].data;
3171 blue = image->comps[2].data;
3173 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3177 sig_bit.alpha = prec;
3178 alpha = image->comps[3].data;
3179 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3183 sig_bit.alpha = 0; alpha = NULL;
3184 color_type = PNG_COLOR_TYPE_RGB;
3186 png_set_sBIT(png, info, &sig_bit);
3188 png_set_IHDR(png, info, width, height, prec,
3191 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3193 /*=============================*/
3194 png_write_info(png, info);
3195 /*=============================*/
3198 png_set_packing(png);
3202 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3204 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3205 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3206 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3208 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3210 for(y = 0; y < height; ++y)
3214 for(x = 0; x < width; ++x)
3218 v = *red + adjustR; ++red;
3220 if(force16) { v = (v<<ushift) + (v>>dshift); }
3222 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3224 v = *green + adjustG; ++green;
3226 if(force16) { v = (v<<ushift) + (v>>dshift); }
3228 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3230 v = *blue + adjustB; ++blue;
3232 if(force16) { v = (v<<ushift) + (v>>dshift); }
3234 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3240 if(force16) { v = (v<<ushift) + (v>>dshift); }
3242 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3246 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3247 *d++ = (unsigned char)((*green + adjustG) & mask); ++green;
3248 *d++ = (unsigned char)((*blue + adjustB) & mask); ++blue;
3252 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3256 png_write_row(png, row_buf);
3263 if(nr_comp == 1 /* GRAY */
3264 || ( nr_comp == 2 /* GRAY_ALPHA */
3265 && image->comps[0].dx == image->comps[1].dx
3266 && image->comps[0].dy == image->comps[1].dy
3267 && image->comps[0].prec == image->comps[1].prec))
3271 red = image->comps[0].data;
3275 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3277 sig_bit.gray = prec;
3278 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3280 color_type = PNG_COLOR_TYPE_GRAY;
3284 has_alpha = 1; sig_bit.alpha = prec;
3285 alpha = image->comps[1].data;
3286 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3288 width = image->comps[0].w;
3289 height = image->comps[0].h;
3291 png_set_IHDR(png, info, width, height, sig_bit.gray,
3294 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3296 png_set_sBIT(png, info, &sig_bit);
3297 /*=============================*/
3298 png_write_info(png, info);
3299 /*=============================*/
3300 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3304 png_set_packing(png);
3309 row_buf = (unsigned char*)
3310 malloc(width * nr_comp * sizeof(unsigned short));
3312 for(y = 0; y < height; ++y)
3316 for(x = 0; x < width; ++x)
3318 v = *red + adjustR; ++red;
3320 if(force16) { v = (v<<ushift) + (v>>dshift); }
3322 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3328 if(force16) { v = (v<<ushift) + (v>>dshift); }
3330 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3333 png_write_row(png, row_buf);
3338 else /* prec <= 8 */
3340 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3342 for(y = 0; y < height; ++y)
3346 for(x = 0; x < width; ++x)
3348 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3352 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3356 png_write_row(png, row_buf);
3364 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3367 png_write_end(png, info);
3375 png_destroy_write_struct(&png, &info);
3379 if(fails) remove(write_idf);
3383 #endif /* HAVE_LIBPNG */