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 char* return_value_fgets = fgets(line, 250, reader);
1412 if (!strcmp(return_value_fgets,line))
1414 fprintf(stderr,"\nWARNING: fgets return a value different that the first argument");
1415 free(return_value_fgets);
1418 free(return_value_fgets);
1422 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1424 format = atoi(line + 1);
1425 if(format < 1 || format > 7)
1427 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1430 ph->format = format;
1431 ttype = end = have_wh = 0;
1433 while(fgets(line, 250, reader))
1435 if(*line == '#') continue;
1441 s = skip_idf(s, idf);
1443 if(s == NULL || *s == 0) return;
1445 if(strcmp(idf, "ENDHDR") == 0)
1449 if(strcmp(idf, "WIDTH") == 0)
1451 s = skip_int(s, &ph->width);
1452 if(s == NULL || *s == 0) return;
1456 if(strcmp(idf, "HEIGHT") == 0)
1458 s = skip_int(s, &ph->height);
1459 if(s == NULL || *s == 0) return;
1463 if(strcmp(idf, "DEPTH") == 0)
1465 s = skip_int(s, &ph->depth);
1466 if(s == NULL || *s == 0) return;
1470 if(strcmp(idf, "MAXVAL") == 0)
1472 s = skip_int(s, &ph->maxval);
1473 if(s == NULL || *s == 0) return;
1477 if(strcmp(idf, "TUPLTYPE") == 0)
1479 s = skip_idf(s, type);
1480 if(s == NULL || *s == 0) return;
1482 if(strcmp(type, "BLACKANDWHITE") == 0)
1484 ph->bw = 1; ttype = 1; continue;
1486 if(strcmp(type, "GRAYSCALE") == 0)
1488 ph->gray = 1; ttype = 1; continue;
1490 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1492 ph->graya = 1; ttype = 1; continue;
1494 if(strcmp(type, "RGB") == 0)
1496 ph->rgb = 1; ttype = 1; continue;
1498 if(strcmp(type, "RGB_ALPHA") == 0)
1500 ph->rgba = 1; ttype = 1; continue;
1502 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1505 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1507 } /* if(format == 7) */
1511 s = skip_int(s, &ph->width);
1513 s = skip_int(s, &ph->height);
1517 if(format == 1 || format == 4) break;
1521 if(format == 2 || format == 3 || format == 5 || format == 6)
1523 /* P2, P3, P5, P6: */
1524 s = skip_int(s, &ph->maxval);
1526 if(ph->maxval > 65535) return;
1529 }/* while(fgets( ) */
1530 if(format == 2 || format == 3 || format > 4)
1532 if(ph->maxval < 1 || ph->maxval > 65535) return;
1534 if(ph->width < 1 || ph->height < 1) return;
1540 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1542 if(ph->depth < 1 || ph->depth > 4) return;
1544 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1549 if(format != 1 && format != 4)
1551 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1555 if(ph->width && ph->height) ph->ok = 1;
1561 static int has_prec(int val)
1563 if(val < 2) return 1;
1564 if(val < 4) return 2;
1565 if(val < 8) return 3;
1566 if(val < 16) return 4;
1567 if(val < 32) return 5;
1568 if(val < 64) return 6;
1569 if(val < 128) return 7;
1570 if(val < 256) return 8;
1571 if(val < 512) return 9;
1572 if(val < 1024) return 10;
1573 if(val < 2048) return 11;
1574 if(val < 4096) return 12;
1575 if(val < 8192) return 13;
1576 if(val < 16384) return 14;
1577 if(val < 32768) return 15;
1581 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1582 int subsampling_dx = parameters->subsampling_dx;
1583 int subsampling_dy = parameters->subsampling_dy;
1586 int i, compno, numcomps, w, h, prec, format;
1587 OPJ_COLOR_SPACE color_space;
1588 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1589 opj_image_t * image = NULL;
1590 struct pnm_header header_info;
1592 if((fp = fopen(filename, "rb")) == NULL)
1594 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1597 memset(&header_info, 0, sizeof(struct pnm_header));
1599 read_pnm_header(fp, &header_info);
1601 if(!header_info.ok) { fclose(fp); return NULL; }
1603 format = header_info.format;
1607 case 1: /* ascii bitmap */
1608 case 4: /* raw bitmap */
1612 case 2: /* ascii greymap */
1613 case 5: /* raw greymap */
1617 case 3: /* ascii pixmap */
1618 case 6: /* raw pixmap */
1622 case 7: /* arbitrary map */
1623 numcomps = header_info.depth;
1626 default: fclose(fp); return NULL;
1629 color_space = CLRSPC_GRAY;/* GRAY, GRAYA */
1631 color_space = CLRSPC_SRGB;/* RGB, RGBA */
1633 prec = has_prec(header_info.maxval);
1635 if(prec < 8) prec = 8;
1637 w = header_info.width;
1638 h = header_info.height;
1639 subsampling_dx = parameters->subsampling_dx;
1640 subsampling_dy = parameters->subsampling_dy;
1642 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1644 for(i = 0; i < numcomps; i++)
1646 cmptparm[i].prec = prec;
1647 cmptparm[i].bpp = prec;
1648 cmptparm[i].sgnd = 0;
1649 cmptparm[i].dx = subsampling_dx;
1650 cmptparm[i].dy = subsampling_dy;
1654 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1656 if(!image) { fclose(fp); return NULL; }
1658 /* set image offset and reference grid */
1659 image->x0 = parameters->image_offset_x0;
1660 image->y0 = parameters->image_offset_y0;
1661 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1662 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1664 if((format == 2) || (format == 3)) /* ascii pixmap */
1668 for (i = 0; i < w * h; i++)
1670 for(compno = 0; compno < numcomps; compno++)
1673 if (fscanf(fp, "%u", &index) != 1)
1674 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1676 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1684 && ( header_info.gray || header_info.graya
1685 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1687 unsigned char c0, c1, one;
1691 for (i = 0; i < w * h; i++)
1693 for(compno = 0; compno < numcomps; compno++)
1695 if ( !fread(&c0, 1, 1, fp) )
1696 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1699 image->comps[compno].data[i] = c0;
1703 if ( !fread(&c1, 1, 1, fp) )
1704 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1706 image->comps[compno].data[i] = ((c0<<8) | c1);
1712 if(format == 1) /* ascii bitmap */
1714 for (i = 0; i < w * h; i++)
1718 if ( fscanf(fp, "%u", &index) != 1)
1719 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1721 image->comps[0].data[i] = (index?0:255);
1731 for(y = 0; y < h; ++y)
1735 for(x = 0; x < w; ++x)
1740 uc = (unsigned char)getc(fp);
1742 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1748 if((format == 7 && header_info.bw)) //MONO
1752 for(i = 0; i < w * h; ++i)
1754 if ( !fread(&uc, 1, 1, fp) )
1755 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1756 image->comps[0].data[i] = (uc & 1)?0:255;
1764 int imagetopnm(opj_image_t * image, const char *outfile)
1766 int *red, *green, *blue, *alpha;
1768 int i, compno, ncomp;
1769 int adjustR, adjustG, adjustB, adjustA;
1770 int fails, two, want_gray, has_alpha, triple;
1773 const char *tmp = outfile;
1776 if((prec = image->comps[0].prec) > 16)
1778 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1779 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1782 two = has_alpha = 0; fails = 1;
1783 ncomp = image->numcomps;
1785 while (*tmp) ++tmp; tmp -= 2;
1786 want_gray = (*tmp == 'g' || *tmp == 'G');
1787 ncomp = image->numcomps;
1789 if(want_gray) ncomp = 1;
1791 if (ncomp == 2 /* GRAYA */
1792 || (ncomp > 2 /* RGB, RGBA */
1793 && image->comps[0].dx == image->comps[1].dx
1794 && image->comps[1].dx == image->comps[2].dx
1795 && image->comps[0].dy == image->comps[1].dy
1796 && image->comps[1].dy == image->comps[2].dy
1797 && image->comps[0].prec == image->comps[1].prec
1798 && image->comps[1].prec == image->comps[2].prec
1801 fdest = fopen(outfile, "wb");
1805 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1809 triple = (ncomp > 2);
1810 wr = image->comps[0].w; hr = image->comps[0].h;
1811 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1813 red = image->comps[0].data;
1817 green = image->comps[1].data;
1818 blue = image->comps[2].data;
1820 else green = blue = NULL;
1824 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1826 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1827 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1828 wr, hr, ncomp, max, tt);
1829 alpha = image->comps[ncomp - 1].data;
1830 adjustA = (image->comps[ncomp - 1].sgnd ?
1831 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1835 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1836 opj_version(), wr, hr, max);
1837 alpha = NULL; adjustA = 0;
1839 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1843 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1844 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1846 else adjustG = adjustB = 0;
1848 for(i = 0; i < wr * hr; ++i)
1852 v = *red + adjustR; ++red;
1854 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1858 v = *green + adjustG; ++green;
1860 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1862 v = *blue + adjustB; ++blue;
1864 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1870 v = *alpha + adjustA; ++alpha;
1872 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1880 fprintf(fdest, "%c", (unsigned char)*red++);
1882 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
1885 fprintf(fdest, "%c", (unsigned char)*alpha++);
1889 fclose(fdest); return 0;
1894 if (image->numcomps > ncomp)
1896 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1897 fprintf(stderr," is written to the file\n");
1899 destname = (char*)malloc(strlen(outfile) + 8);
1901 for (compno = 0; compno < ncomp; compno++)
1904 sprintf(destname, "%d.%s", compno, outfile);
1906 sprintf(destname, "%s", outfile);
1908 fdest = fopen(destname, "wb");
1911 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1915 wr = image->comps[compno].w; hr = image->comps[compno].h;
1916 prec = image->comps[compno].prec;
1917 max = (1<<prec) - 1;
1919 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
1920 opj_version(), wr, hr, max);
1922 red = image->comps[compno].data;
1924 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1928 for (i = 0; i < wr * hr; i++)
1930 v = *red + adjustR; ++red;
1932 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1938 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1942 else /* prec <= 8 */
1944 for(i = 0; i < wr * hr; ++i)
1946 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
1957 /* -->> -->> -->> -->>
1961 <<-- <<-- <<-- <<-- */
1963 typedef struct tiff_infoheader{
1964 DWORD tiWidth; // Width of Image in pixel
1965 DWORD tiHeight; // Height of Image in pixel
1966 DWORD tiPhoto; // Photometric
1967 WORD tiBps; // Bits per sample
1968 WORD tiSf; // Sample Format
1969 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1970 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1973 int imagetotif(opj_image_t * image, const char *outfile)
1975 int width, height, imgsize;
1976 int bps,index,adjust, sgnd;
1977 int ushift, dshift, has_alpha, force16;
1983 ushift = dshift = force16 = has_alpha = 0;
1984 bps = image->comps[0].prec;
1986 if(bps > 8 && bps < 16)
1988 ushift = 16 - bps; dshift = bps - ushift;
1989 bps = 16; force16 = 1;
1992 if(bps != 8 && bps != 16)
1994 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
1996 fprintf(stderr,"\tAborting\n");
1999 tif = TIFFOpen(outfile, "wb");
2003 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
2006 sgnd = image->comps[0].sgnd;
2007 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
2009 if(image->numcomps >= 3
2010 && image->comps[0].dx == image->comps[1].dx
2011 && image->comps[1].dx == image->comps[2].dx
2012 && image->comps[0].dy == image->comps[1].dy
2013 && image->comps[1].dy == image->comps[2].dy
2014 && image->comps[0].prec == image->comps[1].prec
2015 && image->comps[1].prec == image->comps[2].prec)
2017 has_alpha = (image->numcomps == 4);
2019 width = image->comps[0].w;
2020 height = image->comps[0].h;
2021 imgsize = width * height ;
2023 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2024 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2025 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
2026 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2027 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2028 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2029 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2030 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2031 strip_size = TIFFStripSize(tif);
2032 buf = _TIFFmalloc(strip_size);
2035 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2037 unsigned char *dat8;
2038 tsize_t i, ssize, last_i = 0;
2040 ssize = TIFFStripSize(tif);
2041 dat8 = (unsigned char*)buf;
2045 step = 3 + has_alpha;
2048 for(i=0; i < ssize - restx; i += step)
2054 r = image->comps[0].data[index];
2055 g = image->comps[1].data[index];
2056 b = image->comps[2].data[index];
2057 if(has_alpha) a = image->comps[3].data[index];
2064 if(has_alpha) a += adjust;
2069 if(has_alpha) dat8[i+3] = a;
2080 for(i = last_i; i < ssize; i += step)
2086 r = image->comps[0].data[index];
2087 g = image->comps[1].data[index];
2088 b = image->comps[2].data[index];
2089 if(has_alpha) a = image->comps[3].data[index];
2096 if(has_alpha) a += adjust;
2099 if(i+1 < ssize) dat8[i+1] = g ; else break;
2100 if(i+2 < ssize) dat8[i+2] = b ; else break;
2103 if(i+3 < ssize) dat8[i+3] = a ; else break;
2110 }//if(last_i < ssize)
2116 step = 6 + has_alpha + has_alpha;
2119 for(i = 0; i < ssize - restx ; i += step)
2125 r = image->comps[0].data[index];
2126 g = image->comps[1].data[index];
2127 b = image->comps[2].data[index];
2128 if(has_alpha) a = image->comps[3].data[index];
2135 if(has_alpha) a += adjust;
2139 r = (r<<ushift) + (r>>dshift);
2140 g = (g<<ushift) + (g>>dshift);
2141 b = (b<<ushift) + (b>>dshift);
2142 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2145 dat8[i+1] = (r >> 8);//MSB
2147 dat8[i+3] = (g >> 8);
2149 dat8[i+5] = (b >> 8);
2153 dat8[i+7] = (a >> 8);
2164 for(i = last_i ; i < ssize ; i += step)
2170 r = image->comps[0].data[index];
2171 g = image->comps[1].data[index];
2172 b = image->comps[2].data[index];
2173 if(has_alpha) a = image->comps[3].data[index];
2180 if(has_alpha) a += adjust;
2184 r = (r<<ushift) + (r>>dshift);
2185 g = (g<<ushift) + (g>>dshift);
2186 b = (b<<ushift) + (b>>dshift);
2187 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2190 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB
2191 if(i+2 < ssize) dat8[i+2] = g; else break;
2192 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2193 if(i+4 < ssize) dat8[i+4] = b; else break;
2194 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2198 if(i+6 < ssize) dat8[i+6] = a; else break;
2199 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2206 }//if(last_i < ssize)
2209 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2212 _TIFFfree((void*)buf);
2218 if(image->numcomps == 1 /* GRAY */
2219 || ( image->numcomps == 2 /* GRAY_ALPHA */
2220 && image->comps[0].dx == image->comps[1].dx
2221 && image->comps[0].dy == image->comps[1].dy
2222 && image->comps[0].prec == image->comps[1].prec))
2226 has_alpha = (image->numcomps == 2);
2228 width = image->comps[0].w;
2229 height = image->comps[0].h;
2230 imgsize = width * height;
2233 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2234 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2235 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2236 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2237 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2238 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2239 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2240 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2242 /* Get a buffer for the data */
2243 strip_size = TIFFStripSize(tif);
2244 buf = _TIFFmalloc(strip_size);
2247 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2249 unsigned char *dat8;
2250 tsize_t i, ssize = TIFFStripSize(tif);
2251 dat8 = (unsigned char*)buf;
2255 step = 1 + has_alpha;
2257 for(i=0; i < ssize; i += step)
2263 r = image->comps[0].data[index];
2264 if(has_alpha) a = image->comps[1].data[index];
2269 if(has_alpha) a += adjust;
2272 if(has_alpha) dat8[i+1] = a;
2282 step = 2 + has_alpha + has_alpha;
2284 for(i=0; i < ssize; i += step)
2290 r = image->comps[0].data[index];
2291 if(has_alpha) a = image->comps[1].data[index];
2296 if(has_alpha) a += adjust;
2300 r = (r<<ushift) + (r>>dshift);
2301 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2304 dat8[i+1] = r >> 8;//MSB
2311 }//if(index < imgsize)
2316 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2327 fprintf(stderr,"imagetotif: Bad color format.\n"
2328 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2329 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2336 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2337 * CINEMA : 12 bit precision
2339 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2341 int subsampling_dx = parameters->subsampling_dx;
2342 int subsampling_dy = parameters->subsampling_dy;
2344 tiff_infoheader_t Info;
2348 int j, numcomps, w, h,index;
2349 OPJ_COLOR_SPACE color_space;
2350 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2351 opj_image_t *image = NULL;
2355 tif = TIFFOpen(filename, "r");
2359 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2362 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
2363 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
2364 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
2365 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
2366 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
2368 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
2369 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
2376 if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
2377 if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
2382 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2383 " implemented\n",Info.tiBps);
2386 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2387 " and GRAY(A) has been implemented\n",(int) Info.tiPhoto);
2389 fprintf(stderr,"\tAborting\n");
2396 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2398 uint16 extrasamples;
2400 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2401 &extrasamples, &sampleinfo);
2403 if(extrasamples >= 1)
2405 switch(sampleinfo[0])
2407 case EXTRASAMPLE_UNSPECIFIED:
2408 /* Workaround for some images without correct info about alpha channel
2414 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2415 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2421 /* initialize image components
2423 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2425 if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2427 numcomps = 3 + has_alpha;
2428 color_space = CLRSPC_SRGB;
2430 for(j = 0; j < numcomps; j++)
2432 if(parameters->cp_cinema)
2434 cmptparm[j].prec = 12;
2435 cmptparm[j].bpp = 12;
2439 cmptparm[j].prec = Info.tiBps;
2440 cmptparm[j].bpp = Info.tiBps;
2442 cmptparm[j].dx = subsampling_dx;
2443 cmptparm[j].dy = subsampling_dy;
2448 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2455 /* set image offset and reference grid
2457 image->x0 = parameters->image_offset_x0;
2458 image->y0 = parameters->image_offset_y0;
2459 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2460 image->x0 + (w - 1) * subsampling_dx + 1;
2461 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2462 image->y0 + (h - 1) * subsampling_dy + 1;
2464 buf = _TIFFmalloc(TIFFStripSize(tif));
2466 strip_size=TIFFStripSize(tif);
2468 imgsize = image->comps[0].w * image->comps[0].h ;
2469 /* Read the Image components
2471 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2473 unsigned char *dat8;
2476 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2477 dat8 = (unsigned char*)buf;
2479 if(Info.tiBps == 16)
2481 step = 6 + has_alpha + has_alpha;
2483 for(i = 0; i < ssize; i += step)
2487 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
2488 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
2489 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
2491 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2493 if(parameters->cp_cinema)
2495 /* Rounding 16 to 12 bits
2497 image->comps[0].data[index] =
2498 (image->comps[0].data[index] + 0x08) >> 4 ;
2499 image->comps[1].data[index] =
2500 (image->comps[1].data[index] + 0x08) >> 4 ;
2501 image->comps[2].data[index] =
2502 (image->comps[2].data[index] + 0x08) >> 4 ;
2504 image->comps[3].data[index] =
2505 (image->comps[3].data[index] + 0x08) >> 4 ;
2512 }//if(Info.tiBps == 16)
2516 step = 3 + has_alpha;
2518 for(i = 0; i < ssize; i += step)
2522 image->comps[0].data[index] = dat8[i+0];// R
2523 image->comps[1].data[index] = dat8[i+1];// G
2524 image->comps[2].data[index] = dat8[i+2];// B
2526 image->comps[3].data[index] = dat8[i+3];
2528 if(parameters->cp_cinema)
2530 /* Rounding 8 to 12 bits
2532 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2533 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2534 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2536 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2543 }//if( Info.tiBps == 8)
2545 if(Info.tiBps == 12)/* CINEMA file */
2549 for(i = 0; i < ssize; i += step)
2551 if((index < imgsize)&(index+1 < imgsize))
2553 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2554 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2556 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2557 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2559 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2560 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2576 if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2578 numcomps = 1 + has_alpha;
2579 color_space = CLRSPC_GRAY;
2581 for(j = 0; j < numcomps; ++j)
2583 cmptparm[j].prec = Info.tiBps;
2584 cmptparm[j].bpp = Info.tiBps;
2585 cmptparm[j].dx = subsampling_dx;
2586 cmptparm[j].dy = subsampling_dy;
2590 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2597 /* set image offset and reference grid
2599 image->x0 = parameters->image_offset_x0;
2600 image->y0 = parameters->image_offset_y0;
2601 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2602 image->x0 + (w - 1) * subsampling_dx + 1;
2603 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2604 image->y0 + (h - 1) * subsampling_dy + 1;
2606 buf = _TIFFmalloc(TIFFStripSize(tif));
2608 strip_size = TIFFStripSize(tif);
2610 imgsize = image->comps[0].w * image->comps[0].h ;
2611 /* Read the Image components
2613 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2615 unsigned char *dat8;
2619 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2620 dat8 = (unsigned char*)buf;
2622 if(Info.tiBps == 16)
2624 step = 2 + has_alpha + has_alpha;
2626 for(i = 0; i < ssize; i += step)
2630 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2632 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2642 step = 1 + has_alpha;
2644 for(i = 0; i < ssize; i += step)
2648 image->comps[0].data[index] = dat8[i+0];
2650 image->comps[1].data[index] = dat8[i+1];
2668 #endif /* HAVE_LIBTIFF */
2670 /* -->> -->> -->> -->>
2674 <<-- <<-- <<-- <<-- */
2676 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2677 int subsampling_dx = parameters->subsampling_dx;
2678 int subsampling_dy = parameters->subsampling_dy;
2681 int i, compno, numcomps, w, h;
2682 OPJ_COLOR_SPACE color_space;
2683 opj_image_cmptparm_t *cmptparm;
2684 opj_image_t * image = NULL;
2687 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2689 fprintf(stderr,"\nError: invalid raw image parameters\n");
2690 fprintf(stderr,"Please use the Format option -F:\n");
2691 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2692 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2693 fprintf(stderr,"Aborting\n");
2697 f = fopen(filename, "rb");
2699 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2700 fprintf(stderr,"Aborting\n");
2703 numcomps = raw_cp->rawComp;
2704 color_space = CLRSPC_SRGB;
2705 w = raw_cp->rawWidth;
2706 h = raw_cp->rawHeight;
2707 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2709 /* initialize image components */
2710 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2711 for(i = 0; i < numcomps; i++) {
2712 cmptparm[i].prec = raw_cp->rawBitDepth;
2713 cmptparm[i].bpp = raw_cp->rawBitDepth;
2714 cmptparm[i].sgnd = raw_cp->rawSigned;
2715 cmptparm[i].dx = subsampling_dx;
2716 cmptparm[i].dy = subsampling_dy;
2720 /* create the image */
2721 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2726 /* set image offset and reference grid */
2727 image->x0 = parameters->image_offset_x0;
2728 image->y0 = parameters->image_offset_y0;
2729 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2730 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2732 if(raw_cp->rawBitDepth <= 8)
2734 unsigned char value = 0;
2735 for(compno = 0; compno < numcomps; compno++) {
2736 for (i = 0; i < w * h; i++) {
2737 if (!fread(&value, 1, 1, f)) {
2738 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2741 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2745 else if(raw_cp->rawBitDepth <= 16)
2747 unsigned short value;
2748 for(compno = 0; compno < numcomps; compno++) {
2749 for (i = 0; i < w * h; i++) {
2751 if (!fread(&temp, 1, 1, f)) {
2752 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2756 if (!fread(&temp, 1, 1, f)) {
2757 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2761 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2766 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2770 if (fread(&ch, 1, 1, f)) {
2771 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2778 int imagetoraw(opj_image_t * image, const char *outfile)
2780 FILE *rawFile = NULL;
2786 if((image->numcomps * image->x1 * image->y1) == 0)
2788 fprintf(stderr,"\nError: invalid raw image parameters\n");
2792 rawFile = fopen(outfile, "wb");
2794 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2798 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2800 for(compno = 0; compno < image->numcomps; compno++)
2802 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2803 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2805 w = image->comps[compno].w;
2806 h = image->comps[compno].h;
2808 if(image->comps[compno].prec <= 8)
2810 if(image->comps[compno].sgnd == 1)
2813 int mask = (1 << image->comps[compno].prec) - 1;
2814 ptr = image->comps[compno].data;
2815 for (line = 0; line < h; line++) {
2816 for(row = 0; row < w; row++) {
2817 curr = (signed char) (*ptr & mask);
2818 fwrite(&curr, sizeof(signed char), 1, rawFile);
2823 else if(image->comps[compno].sgnd == 0)
2826 int mask = (1 << image->comps[compno].prec) - 1;
2827 ptr = image->comps[compno].data;
2828 for (line = 0; line < h; line++) {
2829 for(row = 0; row < w; row++) {
2830 curr = (unsigned char) (*ptr & mask);
2831 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2837 else if(image->comps[compno].prec <= 16)
2839 if(image->comps[compno].sgnd == 1)
2841 signed short int curr;
2842 int mask = (1 << image->comps[compno].prec) - 1;
2843 ptr = image->comps[compno].data;
2844 for (line = 0; line < h; line++) {
2845 for(row = 0; row < w; row++) {
2847 curr = (signed short int) (*ptr & mask);
2848 temp = (unsigned char) (curr >> 8);
2849 fwrite(&temp, 1, 1, rawFile);
2850 temp = (unsigned char) curr;
2851 fwrite(&temp, 1, 1, rawFile);
2856 else if(image->comps[compno].sgnd == 0)
2858 unsigned short int curr;
2859 int mask = (1 << image->comps[compno].prec) - 1;
2860 ptr = image->comps[compno].data;
2861 for (line = 0; line < h; line++) {
2862 for(row = 0; row < w; row++) {
2864 curr = (unsigned short int) (*ptr & mask);
2865 temp = (unsigned char) (curr >> 8);
2866 fwrite(&temp, 1, 1, rawFile);
2867 temp = (unsigned char) curr;
2868 fwrite(&temp, 1, 1, rawFile);
2874 else if (image->comps[compno].prec <= 32)
2876 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2881 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2891 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
2892 #define MAGIC_SIZE 8
2893 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
2895 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
2899 double gamma, display_exponent;
2900 int bit_depth, interlace_type,compression_type, filter_type;
2902 png_uint_32 resx, resy;
2904 png_uint_32 width, height;
2905 int color_type, has_alpha, is16;
2908 unsigned char **rows;
2911 opj_image_cmptparm_t cmptparm[4];
2913 unsigned int nr_comp;
2915 unsigned char sigbuf[8];
2917 if((reader = fopen(read_idf, "rb")) == NULL)
2919 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
2922 image = NULL; png = NULL; rows = NULL;
2924 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
2925 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
2927 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
2930 /* libpng-VERSION/example.c:
2931 * PC : screen_gamma = 2.2;
2932 * Mac: screen_gamma = 1.7 or 1.0;
2934 display_exponent = 2.2;
2936 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
2937 NULL, NULL, NULL)) == NULL)
2939 if((info = png_create_info_struct(png)) == NULL)
2942 if(setjmp(png_jmpbuf(png)))
2945 png_init_io(png, reader);
2946 png_set_sig_bytes(png, MAGIC_SIZE);
2948 png_read_info(png, info);
2950 if(png_get_IHDR(png, info, &width, &height,
2951 &bit_depth, &color_type, &interlace_type,
2952 &compression_type, &filter_type) == 0)
2955 /* png_set_expand():
2956 * expand paletted images to RGB, expand grayscale images of
2957 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
2958 * to alpha channels.
2960 if(color_type == PNG_COLOR_TYPE_PALETTE)
2961 png_set_expand(png);
2963 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2964 png_set_expand(png);
2966 if(png_get_valid(png, info, PNG_INFO_tRNS))
2967 png_set_expand(png);
2969 is16 = (bit_depth == 16);
2971 /* GRAY => RGB; GRAY_ALPHA => RGBA
2973 if(color_type == PNG_COLOR_TYPE_GRAY
2974 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2976 png_set_gray_to_rgb(png);
2978 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
2979 PNG_COLOR_TYPE_RGB_ALPHA);
2981 if( !png_get_gAMA(png, info, &gamma))
2984 png_set_gamma(png, display_exponent, gamma);
2986 png_read_update_info(png, info);
2988 png_get_pHYs(png, info, &resx, &resy, &unit);
2990 color_type = png_get_color_type(png, info);
2992 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
2994 nr_comp = 3 + has_alpha;
2996 bit_depth = png_get_bit_depth(png, info);
2998 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
2999 for(i = 0; i < height; ++i)
3000 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
3002 png_read_image(png, rows);
3004 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
3006 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
3008 for(i = 0; i < nr_comp; ++i)
3010 cmptparm[i].prec = bit_depth;
3011 /* bits_per_pixel: 8 or 16 */
3012 cmptparm[i].bpp = bit_depth;
3013 cmptparm[i].sgnd = 0;
3014 cmptparm[i].dx = sub_dx;
3015 cmptparm[i].dy = sub_dy;
3016 cmptparm[i].w = width;
3017 cmptparm[i].h = height;
3020 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
3022 if(image == NULL) goto fin;
3024 image->x0 = params->image_offset_x0;
3025 image->y0 = params->image_offset_y0;
3026 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
3027 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
3029 r = image->comps[0].data;
3030 g = image->comps[1].data;
3031 b = image->comps[2].data;
3032 a = image->comps[3].data;
3034 for(i = 0; i < height; ++i)
3038 for(j = 0; j < width; ++j)
3042 *r++ = s[0]<<8|s[1]; s += 2;
3044 *g++ = s[0]<<8|s[1]; s += 2;
3046 *b++ = s[0]<<8|s[1]; s += 2;
3048 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
3052 *r++ = *s++; *g++ = *s++; *b++ = *s++;
3054 if(has_alpha) *a++ = *s++;
3060 for(i = 0; i < height; ++i)
3065 png_destroy_read_struct(&png, &info, NULL);
3073 int imagetopng(opj_image_t * image, const char *write_idf)
3078 int *red, *green, *blue, *alpha;
3079 unsigned char *row_buf, *d;
3080 int has_alpha, width, height, nr_comp, color_type;
3081 int adjustR, adjustG, adjustB, x, y, fails, is16, force16;
3082 int opj_prec, prec, ushift, dshift;
3083 unsigned short mask = 0xffff;
3084 png_color_8 sig_bit;
3086 is16 = force16 = ushift = dshift = 0; fails = 1;
3087 prec = opj_prec = image->comps[0].prec;
3089 if(prec > 8 && prec < 16)
3091 prec = 16; force16 = 1;
3093 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
3095 fprintf(stderr,"imagetopng: can not create %s"
3096 "\n\twrong bit_depth %d\n", write_idf, prec);
3099 writer = fopen(write_idf, "wb");
3101 if(writer == NULL) return fails;
3103 info = NULL; has_alpha = 0;
3105 /* Create and initialize the png_struct with the desired error handler
3106 * functions. If you want to use the default stderr and longjump method,
3107 * you can supply NULL for the last three parameters. We also check that
3108 * the library version is compatible with the one used at compile time,
3109 * in case we are using dynamically linked libraries. REQUIRED.
3111 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3113 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3115 if(png == NULL) goto fin;
3117 /* Allocate/initialize the image information data. REQUIRED
3119 info = png_create_info_struct(png);
3121 if(info == NULL) goto fin;
3123 /* Set error handling. REQUIRED if you are not supplying your own
3124 * error handling functions in the png_create_write_struct() call.
3126 if(setjmp(png_jmpbuf(png))) goto fin;
3128 /* I/O initialization functions is REQUIRED
3130 png_init_io(png, writer);
3132 /* Set the image information here. Width and height are up to 2^31,
3133 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3134 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3135 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3136 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3137 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3138 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3141 png_set_compression_level(png, Z_BEST_COMPRESSION);
3143 if(prec == 16) mask = 0xffff;
3145 if(prec == 8) mask = 0x00ff;
3147 if(prec == 4) mask = 0x000f;
3149 if(prec == 2) mask = 0x0003;
3151 if(prec == 1) mask = 0x0001;
3153 nr_comp = image->numcomps;
3156 && image->comps[0].dx == image->comps[1].dx
3157 && image->comps[1].dx == image->comps[2].dx
3158 && image->comps[0].dy == image->comps[1].dy
3159 && image->comps[1].dy == image->comps[2].dy
3160 && image->comps[0].prec == image->comps[1].prec
3161 && image->comps[1].prec == image->comps[2].prec)
3165 has_alpha = (nr_comp > 3);
3167 is16 = (prec == 16);
3169 width = image->comps[0].w;
3170 height = image->comps[0].h;
3172 red = image->comps[0].data;
3173 green = image->comps[1].data;
3174 blue = image->comps[2].data;
3176 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3180 sig_bit.alpha = prec;
3181 alpha = image->comps[3].data;
3182 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3186 sig_bit.alpha = 0; alpha = NULL;
3187 color_type = PNG_COLOR_TYPE_RGB;
3189 png_set_sBIT(png, info, &sig_bit);
3191 png_set_IHDR(png, info, width, height, prec,
3194 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3196 /*=============================*/
3197 png_write_info(png, info);
3198 /*=============================*/
3201 png_set_packing(png);
3205 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3207 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3208 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3209 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3211 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3213 for(y = 0; y < height; ++y)
3217 for(x = 0; x < width; ++x)
3221 v = *red + adjustR; ++red;
3223 if(force16) { v = (v<<ushift) + (v>>dshift); }
3225 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3227 v = *green + adjustG; ++green;
3229 if(force16) { v = (v<<ushift) + (v>>dshift); }
3231 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3233 v = *blue + adjustB; ++blue;
3235 if(force16) { v = (v<<ushift) + (v>>dshift); }
3237 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3243 if(force16) { v = (v<<ushift) + (v>>dshift); }
3245 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3249 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3250 *d++ = (unsigned char)((*green + adjustG) & mask); ++green;
3251 *d++ = (unsigned char)((*blue + adjustB) & mask); ++blue;
3255 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3259 png_write_row(png, row_buf);
3266 if(nr_comp == 1 /* GRAY */
3267 || ( nr_comp == 2 /* GRAY_ALPHA */
3268 && image->comps[0].dx == image->comps[1].dx
3269 && image->comps[0].dy == image->comps[1].dy
3270 && image->comps[0].prec == image->comps[1].prec))
3274 red = image->comps[0].data;
3278 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3280 sig_bit.gray = prec;
3281 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3283 color_type = PNG_COLOR_TYPE_GRAY;
3287 has_alpha = 1; sig_bit.alpha = prec;
3288 alpha = image->comps[1].data;
3289 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3291 width = image->comps[0].w;
3292 height = image->comps[0].h;
3294 png_set_IHDR(png, info, width, height, sig_bit.gray,
3297 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3299 png_set_sBIT(png, info, &sig_bit);
3300 /*=============================*/
3301 png_write_info(png, info);
3302 /*=============================*/
3303 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3307 png_set_packing(png);
3312 row_buf = (unsigned char*)
3313 malloc(width * nr_comp * sizeof(unsigned short));
3315 for(y = 0; y < height; ++y)
3319 for(x = 0; x < width; ++x)
3321 v = *red + adjustR; ++red;
3323 if(force16) { v = (v<<ushift) + (v>>dshift); }
3325 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3331 if(force16) { v = (v<<ushift) + (v>>dshift); }
3333 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3336 png_write_row(png, row_buf);
3341 else /* prec <= 8 */
3343 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3345 for(y = 0; y < height; ++y)
3349 for(x = 0; x < width; ++x)
3351 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3355 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3359 png_write_row(png, row_buf);
3367 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3370 png_write_end(png, info);
3378 png_destroy_write_struct(&png, &info);
3382 if(fails) remove(write_idf);
3386 #endif /* HAVE_LIBPNG */