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 fread((unsigned char*)&tga, sizeof(tga_header), 1, fp);
107 *bits_per_pixel = tga.pixel_depth;
109 *width = tga.image_width;
110 *height = tga.image_height ;
112 // Ignore tga identifier, if present ...
115 unsigned char *id = (unsigned char *) malloc(tga.id_length);
116 fread(id, tga.id_length, 1, fp);
120 // Test for compressed formats ... not yet supported ...
121 // Note :- 9 - RLE encoded palettized.
122 // 10 - RLE encoded RGB.
123 if (tga.image_type > 8)
125 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
129 *flip_image = !(tga.image_desc & 32);
131 // Palettized formats are not yet supported, skip over the palette, if present ...
132 palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
136 fprintf(stderr, "File contains a palette - not yet supported.");
137 fseek(fp, palette_size, SEEK_CUR);
142 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
147 if (!bits_per_pixel || !width || !height)
150 memset(&tga, 0, sizeof(tga_header));
152 tga.pixel_depth = bits_per_pixel;
153 tga.image_width = width;
154 tga.image_height = height;
155 tga.image_type = 2; // Uncompressed.
156 tga.image_desc = 8; // 8 bits per component.
159 tga.image_desc |= 32;
162 fwrite((unsigned char*)&tga, sizeof(tga_header), 1, fp);
167 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
170 unsigned int image_width, image_height, pixel_bit_depth;
173 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
175 OPJ_COLOR_SPACE color_space;
178 int subsampling_dx, subsampling_dy;
181 f = fopen(filename, "rb");
183 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
187 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
190 // We currently only support 24 & 32 bit tga's ...
191 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
194 /* initialize image components */
195 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
197 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha.
198 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
201 color_space = CLRSPC_GRAY;
202 numcomps = save_alpha ? 2 : 1;
205 numcomps = save_alpha ? 4 : 3;
206 color_space = CLRSPC_SRGB;
209 subsampling_dx = parameters->subsampling_dx;
210 subsampling_dy = parameters->subsampling_dy;
212 for (i = 0; i < numcomps; i++) {
213 cmptparm[i].prec = 8;
215 cmptparm[i].sgnd = 0;
216 cmptparm[i].dx = subsampling_dx;
217 cmptparm[i].dy = subsampling_dy;
218 cmptparm[i].w = image_width;
219 cmptparm[i].h = image_height;
222 /* create the image */
223 image = opj_image_create(numcomps, &cmptparm[0], color_space);
228 /* set image offset and reference grid */
229 image->x0 = parameters->image_offset_x0;
230 image->y0 = parameters->image_offset_y0;
231 image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
232 image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
235 for (y=0; y < image_height; y++)
240 index = (image_height-y-1)*image_width;
242 index = y*image_width;
246 for (x=0;x<image_width;x++)
253 image->comps[0].data[index]=r;
254 image->comps[1].data[index]=g;
255 image->comps[2].data[index]=b;
259 else if (numcomps==4)
261 for (x=0;x<image_width;x++)
263 unsigned char r,g,b,a;
269 image->comps[0].data[index]=r;
270 image->comps[1].data[index]=g;
271 image->comps[2].data[index]=b;
272 image->comps[3].data[index]=a;
277 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
283 int imagetotga(opj_image_t * image, const char *outfile) {
284 int width, height, bpp, x, y;
285 opj_bool write_alpha;
287 unsigned int alpha_channel;
293 fdest = fopen(outfile, "wb");
295 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
299 for (i = 0; i < image->numcomps-1; i++) {
300 if ((image->comps[0].dx != image->comps[i+1].dx)
301 ||(image->comps[0].dy != image->comps[i+1].dy)
302 ||(image->comps[0].prec != image->comps[i+1].prec)) {
303 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
308 width = image->comps[0].w;
309 height = image->comps[0].h;
311 // Mono with alpha, or RGB with alpha.
312 write_alpha = (image->numcomps==2) || (image->numcomps==4);
315 bpp = write_alpha ? 32 : 24;
316 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
319 alpha_channel = image->numcomps-1;
321 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
323 for (y=0; y < height; y++) {
324 unsigned int index=y*width;
326 for (x=0; x < width; x++, index++) {
327 r = (float)(image->comps[0].data[index]);
329 if (image->numcomps>2) {
330 g = (float)(image->comps[1].data[index]);
331 b = (float)(image->comps[2].data[index]);
333 else {// Greyscale ...
338 // TGA format writes BGR ...
339 value = (unsigned char)(b*scale);
340 fwrite(&value,1,1,fdest);
342 value = (unsigned char)(g*scale);
343 fwrite(&value,1,1,fdest);
345 value = (unsigned char)(r*scale);
346 fwrite(&value,1,1,fdest);
349 a = (float)(image->comps[alpha_channel].data[index]);
350 value = (unsigned char)(a*scale);
351 fwrite(&value,1,1,fdest);
359 /* -->> -->> -->> -->>
363 <<-- <<-- <<-- <<-- */
365 /* WORD defines a two byte word */
366 typedef unsigned short int WORD;
368 /* DWORD defines a four byte word */
369 typedef unsigned long int DWORD;
372 WORD bfType; /* 'BM' for Bitmap (19776) */
373 DWORD bfSize; /* Size of the file */
374 WORD bfReserved1; /* Reserved : 0 */
375 WORD bfReserved2; /* Reserved : 0 */
376 DWORD bfOffBits; /* Offset */
377 } BITMAPFILEHEADER_t;
380 DWORD biSize; /* Size of the structure in bytes */
381 DWORD biWidth; /* Width of the image in pixels */
382 DWORD biHeight; /* Heigth of the image in pixels */
383 WORD biPlanes; /* 1 */
384 WORD biBitCount; /* Number of color bits by pixels */
385 DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
386 DWORD biSizeImage; /* Size of the image in bytes */
387 DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
388 DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
389 DWORD biClrUsed; /* Number of color used in the image (0: ALL) */
390 DWORD biClrImportant; /* Number of important color (0: ALL) */
391 } BITMAPINFOHEADER_t;
393 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
395 int subsampling_dx = parameters->subsampling_dx;
396 int subsampling_dy = parameters->subsampling_dy;
398 int i, numcomps, w, h;
399 OPJ_COLOR_SPACE color_space;
400 opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
401 opj_image_t * image = NULL;
404 BITMAPFILEHEADER_t File_h;
405 BITMAPINFOHEADER_t Info_h;
407 unsigned char *table_R, *table_G, *table_B;
408 unsigned int j, PAD = 0;
411 int gray_scale = 1, not_end_file = 1;
413 unsigned int line = 0, col = 0;
417 IN = fopen(filename, "rb");
420 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
424 File_h.bfType = getc(IN);
425 File_h.bfType = (getc(IN) << 8) + File_h.bfType;
427 if (File_h.bfType != 19778)
429 fprintf(stderr,"Error, not a BMP file!\n");
435 File_h.bfSize = getc(IN);
436 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
437 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
438 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
440 File_h.bfReserved1 = getc(IN);
441 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
443 File_h.bfReserved2 = getc(IN);
444 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
446 File_h.bfOffBits = getc(IN);
447 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
448 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
449 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
454 Info_h.biSize = getc(IN);
455 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
456 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
457 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
459 if(Info_h.biSize != 40)
461 fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize);
465 Info_h.biWidth = getc(IN);
466 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
467 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
468 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
471 Info_h.biHeight = getc(IN);
472 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
473 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
474 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
477 Info_h.biPlanes = getc(IN);
478 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
480 Info_h.biBitCount = getc(IN);
481 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
483 Info_h.biCompression = getc(IN);
484 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
485 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
486 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
488 Info_h.biSizeImage = getc(IN);
489 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
490 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
491 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
493 Info_h.biXpelsPerMeter = getc(IN);
494 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
495 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
496 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
498 Info_h.biYpelsPerMeter = getc(IN);
499 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
500 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
501 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
503 Info_h.biClrUsed = getc(IN);
504 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
505 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
506 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
508 Info_h.biClrImportant = getc(IN);
509 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
510 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
511 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
513 /* Read the data and store them in the OUT file */
515 if (Info_h.biBitCount == 24)
518 color_space = CLRSPC_SRGB;
519 /* initialize image components */
520 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
521 for(i = 0; i < numcomps; i++)
523 cmptparm[i].prec = 8;
525 cmptparm[i].sgnd = 0;
526 cmptparm[i].dx = subsampling_dx;
527 cmptparm[i].dy = subsampling_dy;
531 /* create the image */
532 image = opj_image_create(numcomps, &cmptparm[0], color_space);
539 /* set image offset and reference grid */
540 image->x0 = parameters->image_offset_x0;
541 image->y0 = parameters->image_offset_y0;
542 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
543 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
547 /* Place the cursor at the beginning of the image information */
548 fseek(IN, 0, SEEK_SET);
549 fseek(IN, File_h.bfOffBits, SEEK_SET);
554 /* PAD = 4 - (3 * W) % 4; */
555 /* PAD = (PAD == 4) ? 0 : PAD; */
556 PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
558 RGB = (unsigned char *)
559 malloc((3 * W + PAD) * H * sizeof(unsigned char));
561 fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
565 for(y = 0; y < (int)H; y++)
567 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
568 for(x = 0; x < (int)W; x++)
570 unsigned char *pixel = &scanline[3 * x];
571 image->comps[0].data[index] = pixel[2]; /* R */
572 image->comps[1].data[index] = pixel[1]; /* G */
573 image->comps[2].data[index] = pixel[0]; /* B */
578 }/* if (Info_h.biBitCount == 24) */
580 if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)//RGB
582 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
584 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
586 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
587 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
588 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
591 for (j = 0; j < Info_h.biClrUsed; j++)
593 table_B[j] = getc(IN);
594 table_G[j] = getc(IN);
595 table_R[j] = getc(IN);
598 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
600 if(has_color) gray_scale = 0;
602 /* Place the cursor at the beginning of the image information */
603 fseek(IN, 0, SEEK_SET);
604 fseek(IN, File_h.bfOffBits, SEEK_SET);
608 if (Info_h.biWidth % 2)
611 numcomps = gray_scale ? 1 : 3;
612 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
613 /* initialize image components */
614 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
615 for(i = 0; i < numcomps; i++)
617 cmptparm[i].prec = 8;
619 cmptparm[i].sgnd = 0;
620 cmptparm[i].dx = subsampling_dx;
621 cmptparm[i].dy = subsampling_dy;
625 /* create the image */
626 image = opj_image_create(numcomps, &cmptparm[0], color_space);
630 free(table_R); free(table_G); free(table_B);
634 /* set image offset and reference grid */
635 image->x0 = parameters->image_offset_x0;
636 image->y0 = parameters->image_offset_y0;
637 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
638 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
642 RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
644 fread(RGB, sizeof(unsigned char), W * H, IN);
648 for (j = 0; j < W * H; j++)
650 if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
652 image->comps[0].data[index] =
653 table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
662 for (j = 0; j < W * H; j++)
664 if ((j % W < W - 1 && Info_h.biWidth % 2)
665 || !(Info_h.biWidth % 2))
667 unsigned char pixel_index =
668 RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
669 image->comps[0].data[index] = table_R[pixel_index];
670 image->comps[1].data[index] = table_G[pixel_index];
671 image->comps[2].data[index] = table_B[pixel_index];
682 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)//RLE8
684 unsigned char *pix, *beyond;
685 unsigned int *gray, *red, *green, *blue;
686 unsigned int x, y, max;
690 if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
692 if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
694 table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
695 table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
696 table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
699 for (j = 0; j < Info_h.biClrUsed; j++)
701 table_B[j] = getc(IN);
702 table_G[j] = getc(IN);
703 table_R[j] = getc(IN);
706 !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
709 if(has_color) gray_scale = 0;
711 numcomps = gray_scale ? 1 : 3;
712 color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
713 /* initialize image components */
714 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
715 for(i = 0; i < numcomps; i++)
717 cmptparm[i].prec = 8;
719 cmptparm[i].sgnd = 0;
720 cmptparm[i].dx = subsampling_dx;
721 cmptparm[i].dy = subsampling_dy;
725 /* create the image */
726 image = opj_image_create(numcomps, &cmptparm[0], color_space);
730 free(table_R); free(table_G); free(table_B);
734 /* set image offset and reference grid */
735 image->x0 = parameters->image_offset_x0;
736 image->y0 = parameters->image_offset_y0;
737 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
738 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
742 /* Place the cursor at the beginning of the image information */
743 fseek(IN, 0, SEEK_SET);
744 fseek(IN, File_h.bfOffBits, SEEK_SET);
748 RGB = (unsigned char *)calloc(1, W * H * sizeof(unsigned char));
749 beyond = RGB + W * H;
761 for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
768 if(c == 0x00) /* EOL */
770 x = 0; ++y; pix = RGB + x + (H - y - 1) * W;
773 if(c == 0x01) /* EOP */
776 if(c == 0x02) /* MOVE by dxdy */
778 c = getc(IN); x += c;
779 c = getc(IN); y += c;
780 pix = RGB + (H - y - 1) * W + x;
785 for(; i < c && x < W && pix < beyond; i++, x++, pix++)
790 if(c & 1) /* skip padding byte */
800 gray = image->comps[0].data;
808 *gray++ = table_R[uc];
813 int *red, *green, *blue;
815 red = image->comps[0].data;
816 green = image->comps[1].data;
817 blue = image->comps[2].data;
825 *red++ = table_R[uc];
826 *green++ = table_G[uc];
827 *blue++ = table_B[uc];
831 free(table_R); free(table_G); free(table_B);
836 "Other system than 24 bits/pixels or 8 bits (no RLE coding) "
837 "is not yet implemented [%d]\n", Info_h.biBitCount);
843 int imagetobmp(opj_image_t * image, const char *outfile) {
847 int adjustR, adjustG, adjustB;
849 if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
850 && image->comps[1].dx == image->comps[2].dx
851 && image->comps[0].dy == image->comps[1].dy
852 && image->comps[1].dy == image->comps[2].dy
853 && image->comps[0].prec == image->comps[1].prec
854 && image->comps[1].prec == image->comps[2].prec) {
856 /* -->> -->> -->> -->>
858 <<-- <<-- <<-- <<-- */
860 fdest = fopen(outfile, "wb");
862 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
866 w = image->comps[0].w;
867 h = image->comps[0].h;
869 fprintf(fdest, "BM");
873 fprintf(fdest, "%c%c%c%c",
874 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
875 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
876 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
877 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
878 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
879 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
883 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
884 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
885 (unsigned char) ((w) >> 8) & 0xff,
886 (unsigned char) ((w) >> 16) & 0xff,
887 (unsigned char) ((w) >> 24) & 0xff);
888 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
889 (unsigned char) ((h) >> 8) & 0xff,
890 (unsigned char) ((h) >> 16) & 0xff,
891 (unsigned char) ((h) >> 24) & 0xff);
892 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
893 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
894 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
895 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
896 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
897 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
898 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
899 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
900 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
901 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
902 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
904 if (image->comps[0].prec > 8) {
905 adjustR = image->comps[0].prec - 8;
906 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
910 if (image->comps[1].prec > 8) {
911 adjustG = image->comps[1].prec - 8;
912 printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
916 if (image->comps[2].prec > 8) {
917 adjustB = image->comps[2].prec - 8;
918 printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
923 for (i = 0; i < w * h; i++) {
924 unsigned char rc, gc, bc;
927 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
928 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
929 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
930 if(r > 255) r = 255; else if(r < 0) r = 0;
931 rc = (unsigned char)r;
933 g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
934 g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
935 g = ((g >> adjustG)+((g >> (adjustG-1))%2));
936 if(g > 255) g = 255; else if(g < 0) g = 0;
937 gc = (unsigned char)g;
939 b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
940 b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
941 b = ((b >> adjustB)+((b >> (adjustB-1))%2));
942 if(b > 255) b = 255; else if(b < 0) b = 0;
943 bc = (unsigned char)b;
945 fprintf(fdest, "%c%c%c", bc, gc, rc);
947 if ((i + 1) % w == 0) {
948 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
949 fprintf(fdest, "%c", 0);
953 } else { /* Gray-scale */
955 /* -->> -->> -->> -->>
956 8 bits non code (Gray scale)
957 <<-- <<-- <<-- <<-- */
959 fdest = fopen(outfile, "wb");
960 w = image->comps[0].w;
961 h = image->comps[0].h;
963 fprintf(fdest, "BM");
967 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
968 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
969 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
970 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 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", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
973 ((54 + 1024) >> 16) & 0xff,
974 ((54 + 1024) >> 24) & 0xff);
978 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
979 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
980 (unsigned char) ((w) >> 8) & 0xff,
981 (unsigned char) ((w) >> 16) & 0xff,
982 (unsigned char) ((w) >> 24) & 0xff);
983 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
984 (unsigned char) ((h) >> 8) & 0xff,
985 (unsigned char) ((h) >> 16) & 0xff,
986 (unsigned char) ((h) >> 24) & 0xff);
987 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
988 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
989 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
990 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
991 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
992 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
993 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
994 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
995 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
996 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
997 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
999 if (image->comps[0].prec > 8) {
1000 adjustR = image->comps[0].prec - 8;
1001 printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1005 for (i = 0; i < 256; i++) {
1006 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1009 for (i = 0; i < w * h; i++) {
1013 r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1014 r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1015 r = ((r >> adjustR)+((r >> (adjustR-1))%2));
1016 if(r > 255) r = 255; else if(r < 0) r = 0;
1018 fprintf(fdest, "%c", (unsigned char)r);
1020 if ((i + 1) % w == 0) {
1021 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
1022 fprintf(fdest, "%c", 0);
1031 /* -->> -->> -->> -->>
1035 <<-- <<-- <<-- <<-- */
1038 unsigned char readuchar(FILE * f)
1041 fread(&c1, 1, 1, f);
1045 unsigned short readushort(FILE * f, int bigendian)
1047 unsigned char c1, c2;
1048 fread(&c1, 1, 1, f);
1049 fread(&c2, 1, 1, f);
1051 return (c1 << 8) + c2;
1053 return (c2 << 8) + c1;
1056 unsigned int readuint(FILE * f, int bigendian)
1058 unsigned char c1, c2, c3, c4;
1059 fread(&c1, 1, 1, f);
1060 fread(&c2, 1, 1, f);
1061 fread(&c3, 1, 1, f);
1062 fread(&c4, 1, 1, f);
1064 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
1066 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1069 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1072 int i, numcomps, max;
1073 OPJ_COLOR_SPACE color_space;
1074 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1075 opj_image_t * image = NULL;
1077 char endian1,endian2,sign;
1082 opj_image_comp_t *comp = NULL;
1085 color_space = CLRSPC_GRAY;
1087 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1091 f = fopen(filename, "rb");
1093 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1097 fseek(f, 0, SEEK_SET);
1098 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
1102 while (signtmp[i]!='\0') {
1103 if (signtmp[i]=='-') sign='-';
1108 if (endian1=='M' && endian2=='L') {
1110 } else if (endian2=='M' && endian1=='L') {
1113 fprintf(stderr, "Bad pgx header, please check input file\n");
1117 /* initialize image component */
1119 cmptparm.x0 = parameters->image_offset_x0;
1120 cmptparm.y0 = parameters->image_offset_y0;
1121 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1122 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1129 cmptparm.prec = prec;
1130 cmptparm.bpp = prec;
1131 cmptparm.dx = parameters->subsampling_dx;
1132 cmptparm.dy = parameters->subsampling_dy;
1134 /* create the image */
1135 image = opj_image_create(numcomps, &cmptparm, color_space);
1140 /* set image offset and reference grid */
1141 image->x0 = cmptparm.x0;
1142 image->y0 = cmptparm.x0;
1143 image->x1 = cmptparm.w;
1144 image->y1 = cmptparm.h;
1146 /* set image data */
1148 comp = &image->comps[0];
1150 for (i = 0; i < w * h; i++) {
1152 if (comp->prec <= 8) {
1156 v = (char) readuchar(f);
1158 } else if (comp->prec <= 16) {
1160 v = readushort(f, bigendian);
1162 v = (short) readushort(f, bigendian);
1166 v = readuint(f, bigendian);
1168 v = (int) readuint(f, bigendian);
1176 comp->bpp = int_floorlog2(max) + 1;
1181 int imagetopgx(opj_image_t * image, const char *outfile) {
1186 for (compno = 0; compno < image->numcomps; compno++) {
1187 opj_image_comp_t *comp = &image->comps[compno];
1188 char bname[256]; /* buffer for name */
1189 char *name = bname; /* pointer */
1191 const size_t olen = strlen(outfile);
1192 const size_t dotpos = olen - 4;
1193 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1194 if( outfile[dotpos] != '.' ) {
1195 /* `pgx` was recognized but there is no dot at expected position */
1196 fprintf(stderr, "ERROR -> Impossible happen." );
1200 name = (char*)malloc(total+1);
1202 strncpy(name, outfile, dotpos);
1203 if (image->numcomps > 1) {
1204 sprintf(name+dotpos, "-%d.pgx", compno);
1206 strcpy(name+dotpos, ".pgx");
1208 fdest = fopen(name, "wb");
1210 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1213 /* dont need name anymore */
1218 w = image->comps[compno].w;
1219 h = image->comps[compno].h;
1221 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1222 if (comp->prec <= 8) {
1224 } else if (comp->prec <= 16) {
1229 for (i = 0; i < w * h; i++) {
1230 int v = image->comps[compno].data[i];
1231 for (j = nbytes - 1; j >= 0; j--) {
1232 char byte = (char) (v >> (j * 8));
1233 fwrite(&byte, 1, 1, fdest);
1242 /* -->> -->> -->> -->>
1246 <<-- <<-- <<-- <<-- */
1250 int width, height, maxval, depth, format;
1251 char rgb, rgba, gray, graya, bw;
1255 static char *skip_white(char *s)
1259 if(*s == '\n' || *s == '\r') return NULL;
1260 if(isspace(*s)) { ++s; continue; }
1266 static char *skip_int(char *start, int *out_n)
1271 *out_n = 0; s = start;
1273 s = skip_white(start);
1274 if(s == NULL) return NULL;
1279 if( !isdigit(*s)) break;
1282 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1286 static char *skip_idf(char *start, char out_idf[256])
1291 s = skip_white(start);
1292 if(s == NULL) return NULL;
1297 if(isalpha(*s) || *s == '_') { ++s; continue; }
1300 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1304 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1307 int format, have_wh, end, ttype;
1308 char idf[256], type[256];
1311 fgets(line, 250, reader);
1315 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1317 format = atoi(line + 1);
1318 if(format < 1 || format > 7)
1320 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1323 ph->format = format;
1324 ttype = end = have_wh = 0;
1326 while(fgets(line, 250, reader))
1328 if(*line == '#') continue;
1334 s = skip_idf(s, idf);
1336 if(s == NULL || *s == 0) return;
1338 if(strcmp(idf, "ENDHDR") == 0)
1342 if(strcmp(idf, "WIDTH") == 0)
1344 s = skip_int(s, &ph->width);
1345 if(s == NULL || *s == 0) return;
1349 if(strcmp(idf, "HEIGHT") == 0)
1351 s = skip_int(s, &ph->height);
1352 if(s == NULL || *s == 0) return;
1356 if(strcmp(idf, "DEPTH") == 0)
1358 s = skip_int(s, &ph->depth);
1359 if(s == NULL || *s == 0) return;
1363 if(strcmp(idf, "MAXVAL") == 0)
1365 s = skip_int(s, &ph->maxval);
1366 if(s == NULL || *s == 0) return;
1370 if(strcmp(idf, "TUPLTYPE") == 0)
1372 s = skip_idf(s, type);
1373 if(s == NULL || *s == 0) return;
1375 if(strcmp(type, "BLACKANDWHITE") == 0)
1377 ph->bw = 1; ttype = 1; continue;
1379 if(strcmp(type, "GRAYSCALE") == 0)
1381 ph->gray = 1; ttype = 1; continue;
1383 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1385 ph->graya = 1; ttype = 1; continue;
1387 if(strcmp(type, "RGB") == 0)
1389 ph->rgb = 1; ttype = 1; continue;
1391 if(strcmp(type, "RGB_ALPHA") == 0)
1393 ph->rgba = 1; ttype = 1; continue;
1395 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1398 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1400 } /* if(format == 7) */
1404 s = skip_int(s, &ph->width);
1406 s = skip_int(s, &ph->height);
1410 if(format == 1 || format == 4) break;
1414 if(format == 2 || format == 3 || format == 5 || format == 6)
1416 /* P2, P3, P5, P6: */
1417 s = skip_int(s, &ph->maxval);
1419 if(ph->maxval > 65535) return;
1422 }/* while(fgets( ) */
1423 if(format == 2 || format == 3 || format > 4)
1425 if(ph->maxval < 1 || ph->maxval > 65535) return;
1427 if(ph->width < 1 || ph->height < 1) return;
1433 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1435 if(ph->depth < 1 || ph->depth > 4) return;
1437 if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
1442 if(format != 1 && format != 4)
1444 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1448 if(ph->width && ph->height) ph->ok = 1;
1454 static int has_prec(int val)
1456 if(val < 2) return 1;
1457 if(val < 4) return 2;
1458 if(val < 8) return 3;
1459 if(val < 16) return 4;
1460 if(val < 32) return 5;
1461 if(val < 64) return 6;
1462 if(val < 128) return 7;
1463 if(val < 256) return 8;
1464 if(val < 512) return 9;
1465 if(val < 1024) return 10;
1466 if(val < 2048) return 11;
1467 if(val < 4096) return 12;
1468 if(val < 8192) return 13;
1469 if(val < 16384) return 14;
1470 if(val < 32768) return 15;
1474 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1475 int subsampling_dx = parameters->subsampling_dx;
1476 int subsampling_dy = parameters->subsampling_dy;
1479 int i, compno, numcomps, w, h, prec, format;
1480 OPJ_COLOR_SPACE color_space;
1481 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1482 opj_image_t * image = NULL;
1483 struct pnm_header header_info;
1485 if((fp = fopen(filename, "rb")) == NULL)
1487 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1490 memset(&header_info, 0, sizeof(struct pnm_header));
1492 read_pnm_header(fp, &header_info);
1494 if(!header_info.ok) { fclose(fp); return NULL; }
1496 format = header_info.format;
1500 case 1: /* ascii bitmap */
1501 case 4: /* raw bitmap */
1505 case 2: /* ascii greymap */
1506 case 5: /* raw greymap */
1510 case 3: /* ascii pixmap */
1511 case 6: /* raw pixmap */
1515 case 7: /* arbitrary map */
1516 numcomps = header_info.depth;
1519 default: fclose(fp); return NULL;
1522 color_space = CLRSPC_GRAY;/* GRAY, GRAYA */
1524 color_space = CLRSPC_SRGB;/* RGB, RGBA */
1526 prec = has_prec(header_info.maxval);
1528 if(prec < 8) prec = 8;
1530 w = header_info.width;
1531 h = header_info.height;
1532 subsampling_dx = parameters->subsampling_dx;
1533 subsampling_dy = parameters->subsampling_dy;
1535 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
1537 for(i = 0; i < numcomps; i++)
1539 cmptparm[i].prec = prec;
1540 cmptparm[i].bpp = prec;
1541 cmptparm[i].sgnd = 0;
1542 cmptparm[i].dx = subsampling_dx;
1543 cmptparm[i].dy = subsampling_dy;
1547 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1549 if(!image) { fclose(fp); return NULL; }
1551 /* set image offset and reference grid */
1552 image->x0 = parameters->image_offset_x0;
1553 image->y0 = parameters->image_offset_y0;
1554 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
1555 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
1557 if((format == 2) || (format == 3)) /* ascii pixmap */
1561 for (i = 0; i < w * h; i++)
1563 for(compno = 0; compno < numcomps; compno++)
1566 fscanf(fp, "%u", &index);
1568 image->comps[compno].data[i] = (index * 255)/header_info.maxval;
1576 && ( header_info.gray || header_info.graya
1577 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1579 unsigned char c0, c1, one;
1583 for (i = 0; i < w * h; i++)
1585 for(compno = 0; compno < numcomps; compno++)
1587 fread(&c0, 1, 1, fp);
1590 image->comps[compno].data[i] = c0;
1594 fread(&c1, 1, 1, fp);
1596 image->comps[compno].data[i] = ((c0<<8) | c1);
1602 if(format == 1) /* ascii bitmap */
1604 for (i = 0; i < w * h; i++)
1608 fscanf(fp, "%u", &index);
1610 image->comps[0].data[i] = (index?0:255);
1620 for(y = 0; y < h; ++y)
1624 for(x = 0; x < w; ++x)
1629 uc = (unsigned char)getc(fp);
1631 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1637 if((format == 7 && header_info.bw)) //MONO
1641 for(i = 0; i < w * h; ++i)
1643 fread(&uc, 1, 1, fp);
1644 image->comps[0].data[i] = (uc & 1)?0:255;
1652 int imagetopnm(opj_image_t * image, const char *outfile)
1654 int *red, *green, *blue, *alpha;
1656 int i, compno, ncomp;
1657 int adjustR, adjustG, adjustB, adjustA;
1658 int fails, two, want_gray, has_alpha, triple;
1661 const char *tmp = outfile;
1664 if((prec = image->comps[0].prec) > 16)
1666 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1667 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1670 two = has_alpha = 0; fails = 1;
1671 ncomp = image->numcomps;
1673 while (*tmp) ++tmp; tmp -= 2;
1674 want_gray = (*tmp == 'g' || *tmp == 'G');
1675 ncomp = image->numcomps;
1677 if(want_gray) ncomp = 1;
1679 if (ncomp == 2 /* GRAYA */
1680 || (ncomp > 2 /* RGB, RGBA */
1681 && image->comps[0].dx == image->comps[1].dx
1682 && image->comps[1].dx == image->comps[2].dx
1683 && image->comps[0].dy == image->comps[1].dy
1684 && image->comps[1].dy == image->comps[2].dy
1685 && image->comps[0].prec == image->comps[1].prec
1686 && image->comps[1].prec == image->comps[2].prec
1689 fdest = fopen(outfile, "wb");
1693 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1697 triple = (ncomp > 2);
1698 wr = image->comps[0].w; hr = image->comps[0].h;
1699 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1701 red = image->comps[0].data;
1705 green = image->comps[1].data;
1706 blue = image->comps[2].data;
1708 else green = blue = NULL;
1712 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1714 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1715 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1716 wr, hr, ncomp, max, tt);
1717 alpha = image->comps[ncomp - 1].data;
1718 adjustA = (image->comps[ncomp - 1].sgnd ?
1719 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1723 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1724 opj_version(), wr, hr, max);
1725 alpha = NULL; adjustA = 0;
1727 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1731 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1732 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1734 else adjustG = adjustB = 0;
1736 for(i = 0; i < wr * hr; ++i)
1740 v = *red + adjustR; ++red;
1742 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1746 v = *green + adjustG; ++green;
1748 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1750 v = *blue + adjustB; ++blue;
1752 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1758 v = *alpha + adjustA; ++alpha;
1760 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1768 fprintf(fdest, "%c", (unsigned char)*red++);
1770 fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++);
1773 fprintf(fdest, "%c", (unsigned char)*alpha++);
1777 fclose(fdest); return 0;
1782 if (image->numcomps > ncomp)
1784 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1785 fprintf(stderr," is written to the file\n");
1787 destname = (char*)malloc(strlen(outfile) + 8);
1789 for (compno = 0; compno < ncomp; compno++)
1792 sprintf(destname, "%d.%s", compno, outfile);
1794 sprintf(destname, "%s", outfile);
1796 fdest = fopen(destname, "wb");
1799 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1803 wr = image->comps[compno].w; hr = image->comps[compno].h;
1804 prec = image->comps[compno].prec;
1805 max = (1<<prec) - 1;
1807 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
1808 opj_version(), wr, hr, max);
1810 red = image->comps[compno].data;
1812 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1816 for (i = 0; i < wr * hr; i++)
1818 v = *red + adjustR; ++red;
1820 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1826 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1830 else /* prec <= 8 */
1832 for(i = 0; i < wr * hr; ++i)
1834 fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
1845 /* -->> -->> -->> -->>
1849 <<-- <<-- <<-- <<-- */
1851 typedef struct tiff_infoheader{
1852 DWORD tiWidth; // Width of Image in pixel
1853 DWORD tiHeight; // Height of Image in pixel
1854 DWORD tiPhoto; // Photometric
1855 WORD tiBps; // Bits per sample
1856 WORD tiSf; // Sample Format
1857 WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
1858 WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
1861 int imagetotif(opj_image_t * image, const char *outfile)
1863 int width, height, imgsize;
1864 int bps,index,adjust, sgnd;
1865 int ushift, dshift, has_alpha, force16;
1866 unsigned int last_i=0;
1872 ushift = dshift = force16 = has_alpha = 0;
1873 bps = image->comps[0].prec;
1875 if(bps > 8 && bps < 16)
1877 ushift = 16 - bps; dshift = bps - ushift;
1878 bps = 16; force16 = 1;
1881 if(bps != 8 && bps != 16)
1883 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n",
1885 fprintf(stderr,"\tAborting\n");
1888 tif = TIFFOpen(outfile, "wb");
1892 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
1895 sgnd = image->comps[0].sgnd;
1896 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
1898 if(image->numcomps >= 3
1899 && image->comps[0].dx == image->comps[1].dx
1900 && image->comps[1].dx == image->comps[2].dx
1901 && image->comps[0].dy == image->comps[1].dy
1902 && image->comps[1].dy == image->comps[2].dy
1903 && image->comps[0].prec == image->comps[1].prec
1904 && image->comps[1].prec == image->comps[2].prec)
1906 has_alpha = (image->numcomps == 4);
1908 width = image->comps[0].w;
1909 height = image->comps[0].h;
1910 imgsize = width * height ;
1912 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1913 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1914 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha);
1915 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1916 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1917 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1918 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1919 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1920 strip_size = TIFFStripSize(tif);
1921 buf = _TIFFmalloc(strip_size);
1924 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
1926 unsigned char *dat8;
1928 ssize = TIFFStripSize(tif);
1929 dat8 = (unsigned char*)buf;
1934 step = 3 + has_alpha;
1937 for(i=0; i < ssize - restx; i += step)
1943 r = image->comps[0].data[index];
1944 g = image->comps[1].data[index];
1945 b = image->comps[2].data[index];
1946 if(has_alpha) a = image->comps[3].data[index];
1953 if(has_alpha) a += adjust;
1958 if(has_alpha) dat8[i+3] = a;
1969 for(i = last_i; i < ssize; i += step)
1975 r = image->comps[0].data[index];
1976 g = image->comps[1].data[index];
1977 b = image->comps[2].data[index];
1978 if(has_alpha) a = image->comps[3].data[index];
1985 if(has_alpha) a += adjust;
1988 if(i+1 < ssize) dat8[i+1] = g ; else break;
1989 if(i+2 < ssize) dat8[i+2] = b ; else break;
1992 if(i+3 < ssize) dat8[i+3] = a ; else break;
1999 }//if(last_i < ssize)
2005 step = 6 + has_alpha + has_alpha;
2008 for(i = 0; i < ssize - restx ; i += step)
2014 r = image->comps[0].data[index];
2015 g = image->comps[1].data[index];
2016 b = image->comps[2].data[index];
2017 if(has_alpha) a = image->comps[3].data[index];
2024 if(has_alpha) a += adjust;
2028 r = (r<<ushift) + (r>>dshift);
2029 g = (g<<ushift) + (g>>dshift);
2030 b = (b<<ushift) + (b>>dshift);
2031 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2034 dat8[i+1] = (r >> 8);//MSB
2036 dat8[i+3] = (g >> 8);
2038 dat8[i+5] = (b >> 8);
2042 dat8[i+7] = (a >> 8);
2053 for(i = last_i ; i < ssize ; i += step)
2059 r = image->comps[0].data[index];
2060 g = image->comps[1].data[index];
2061 b = image->comps[2].data[index];
2062 if(has_alpha) a = image->comps[3].data[index];
2069 if(has_alpha) a += adjust;
2073 r = (r<<ushift) + (r>>dshift);
2074 g = (g<<ushift) + (g>>dshift);
2075 b = (b<<ushift) + (b>>dshift);
2076 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2079 if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB
2080 if(i+2 < ssize) dat8[i+2] = g; else break;
2081 if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
2082 if(i+4 < ssize) dat8[i+4] = b; else break;
2083 if(i+5 < ssize) dat8[i+5] = (b >> 8);else break;
2087 if(i+6 < ssize) dat8[i+6] = a; else break;
2088 if(i+7 < ssize) dat8[i+7] = (a >> 8); else break;
2095 }//if(last_i < ssize)
2098 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2101 _TIFFfree((void*)buf);
2107 if(image->numcomps == 1 /* GRAY */
2108 || ( image->numcomps == 2 /* GRAY_ALPHA */
2109 && image->comps[0].dx == image->comps[1].dx
2110 && image->comps[0].dy == image->comps[1].dy
2111 && image->comps[0].prec == image->comps[1].prec))
2115 has_alpha = (image->numcomps == 2);
2117 width = image->comps[0].w;
2118 height = image->comps[0].h;
2119 imgsize = width * height;
2122 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
2123 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
2124 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha);
2125 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
2126 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
2127 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
2128 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
2129 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
2131 /* Get a buffer for the data */
2132 strip_size = TIFFStripSize(tif);
2133 buf = _TIFFmalloc(strip_size);
2136 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2138 unsigned char *dat8;
2139 tsize_t i, ssize = TIFFStripSize(tif);
2140 dat8 = (unsigned char*)buf;
2144 step = 1 + has_alpha;
2146 for(i=0; i < ssize; i += step)
2152 r = image->comps[0].data[index];
2153 if(has_alpha) a = image->comps[1].data[index];
2158 if(has_alpha) a += adjust;
2161 if(has_alpha) dat8[i+1] = a;
2171 step = 2 + has_alpha + has_alpha;
2173 for(i=0; i < ssize; i += step)
2179 r = image->comps[0].data[index];
2180 if(has_alpha) a = image->comps[1].data[index];
2185 if(has_alpha) a += adjust;
2189 r = (r<<ushift) + (r>>dshift);
2190 if(has_alpha) a = (a<<ushift) + (a>>dshift);
2193 dat8[i+1] = r >> 8;//MSB
2200 }//if(index < imgsize)
2205 (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
2216 fprintf(stderr,"imagetotif: Bad color format.\n"
2217 "\tOnly RGB(A) and GRAY(A) has been implemented\n");
2218 fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n",
2225 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
2226 * CINEMA : 12 bit precision
2228 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
2230 int subsampling_dx = parameters->subsampling_dx;
2231 int subsampling_dy = parameters->subsampling_dy;
2233 tiff_infoheader_t Info;
2237 int j, numcomps, w, h,index;
2238 OPJ_COLOR_SPACE color_space;
2239 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
2240 opj_image_t *image = NULL;
2244 tif = TIFFOpen(filename, "r");
2248 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
2251 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
2252 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
2253 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
2254 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
2255 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
2257 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
2258 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
2265 if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
2266 if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
2271 fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
2272 " implemented\n",Info.tiBps);
2275 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
2276 " and GRAY(A) has been implemented\n",(int) Info.tiPhoto);
2278 fprintf(stderr,"\tAborting\n");
2285 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
2287 uint16 extrasamples;
2289 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
2290 &extrasamples, &sampleinfo);
2292 if(extrasamples >= 1)
2294 switch(sampleinfo[0])
2296 case EXTRASAMPLE_UNSPECIFIED:
2297 /* Workaround for some images without correct info about alpha channel
2303 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
2304 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
2310 /* initialize image components
2312 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
2314 if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
2316 numcomps = 3 + has_alpha;
2317 color_space = CLRSPC_SRGB;
2319 for(j = 0; j < numcomps; j++)
2321 if(parameters->cp_cinema)
2323 cmptparm[j].prec = 12;
2324 cmptparm[j].bpp = 12;
2328 cmptparm[j].prec = Info.tiBps;
2329 cmptparm[j].bpp = Info.tiBps;
2331 cmptparm[j].dx = subsampling_dx;
2332 cmptparm[j].dy = subsampling_dy;
2337 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2344 /* set image offset and reference grid
2346 image->x0 = parameters->image_offset_x0;
2347 image->y0 = parameters->image_offset_y0;
2348 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2349 image->x0 + (w - 1) * subsampling_dx + 1;
2350 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2351 image->y0 + (h - 1) * subsampling_dy + 1;
2353 buf = _TIFFmalloc(TIFFStripSize(tif));
2355 strip_size=TIFFStripSize(tif);
2357 imgsize = image->comps[0].w * image->comps[0].h ;
2358 /* Read the Image components
2360 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2362 unsigned char *dat8;
2365 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2366 dat8 = (unsigned char*)buf;
2368 if(Info.tiBps == 16)
2370 step = 6 + has_alpha + has_alpha;
2372 for(i = 0; i < ssize; i += step)
2376 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
2377 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
2378 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
2380 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
2382 if(parameters->cp_cinema)
2384 /* Rounding 16 to 12 bits
2386 image->comps[0].data[index] =
2387 (image->comps[0].data[index] + 0x08) >> 4 ;
2388 image->comps[1].data[index] =
2389 (image->comps[1].data[index] + 0x08) >> 4 ;
2390 image->comps[2].data[index] =
2391 (image->comps[2].data[index] + 0x08) >> 4 ;
2393 image->comps[3].data[index] =
2394 (image->comps[3].data[index] + 0x08) >> 4 ;
2401 }//if(Info.tiBps == 16)
2405 step = 3 + has_alpha;
2407 for(i = 0; i < ssize; i += step)
2411 image->comps[0].data[index] = dat8[i+0];// R
2412 image->comps[1].data[index] = dat8[i+1];// G
2413 image->comps[2].data[index] = dat8[i+2];// B
2415 image->comps[3].data[index] = dat8[i+3];
2417 if(parameters->cp_cinema)
2419 /* Rounding 8 to 12 bits
2421 image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
2422 image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
2423 image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
2425 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
2432 }//if( Info.tiBps == 8)
2434 if(Info.tiBps == 12)/* CINEMA file */
2438 for(i = 0; i < ssize; i += step)
2440 if((index < imgsize)&(index+1 < imgsize))
2442 image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
2443 image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
2445 image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
2446 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
2448 image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
2449 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
2465 if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
2467 numcomps = 1 + has_alpha;
2468 color_space = CLRSPC_GRAY;
2470 for(j = 0; j < numcomps; ++j)
2472 cmptparm[j].prec = Info.tiBps;
2473 cmptparm[j].bpp = Info.tiBps;
2474 cmptparm[j].dx = subsampling_dx;
2475 cmptparm[j].dy = subsampling_dy;
2479 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2486 /* set image offset and reference grid
2488 image->x0 = parameters->image_offset_x0;
2489 image->y0 = parameters->image_offset_y0;
2490 image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
2491 image->x0 + (w - 1) * subsampling_dx + 1;
2492 image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
2493 image->y0 + (h - 1) * subsampling_dy + 1;
2495 buf = _TIFFmalloc(TIFFStripSize(tif));
2497 strip_size = TIFFStripSize(tif);
2499 imgsize = image->comps[0].w * image->comps[0].h ;
2500 /* Read the Image components
2502 for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
2504 unsigned char *dat8;
2508 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
2509 dat8 = (unsigned char*)buf;
2511 if(Info.tiBps == 16)
2513 step = 2 + has_alpha + has_alpha;
2515 for(i = 0; i < ssize; i += step)
2519 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
2521 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
2531 step = 1 + has_alpha;
2533 for(i = 0; i < ssize; i += step)
2537 image->comps[0].data[index] = dat8[i+0];
2539 image->comps[1].data[index] = dat8[i+1];
2557 #endif /* HAVE_LIBTIFF */
2559 /* -->> -->> -->> -->>
2563 <<-- <<-- <<-- <<-- */
2565 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2566 int subsampling_dx = parameters->subsampling_dx;
2567 int subsampling_dy = parameters->subsampling_dy;
2570 int i, compno, numcomps, w, h;
2571 OPJ_COLOR_SPACE color_space;
2572 opj_image_cmptparm_t *cmptparm;
2573 opj_image_t * image = NULL;
2576 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
2578 fprintf(stderr,"\nError: invalid raw image parameters\n");
2579 fprintf(stderr,"Please use the Format option -F:\n");
2580 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
2581 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
2582 fprintf(stderr,"Aborting\n");
2586 f = fopen(filename, "rb");
2588 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2589 fprintf(stderr,"Aborting\n");
2592 numcomps = raw_cp->rawComp;
2593 color_space = CLRSPC_SRGB;
2594 w = raw_cp->rawWidth;
2595 h = raw_cp->rawHeight;
2596 cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2598 /* initialize image components */
2599 memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2600 for(i = 0; i < numcomps; i++) {
2601 cmptparm[i].prec = raw_cp->rawBitDepth;
2602 cmptparm[i].bpp = raw_cp->rawBitDepth;
2603 cmptparm[i].sgnd = raw_cp->rawSigned;
2604 cmptparm[i].dx = subsampling_dx;
2605 cmptparm[i].dy = subsampling_dy;
2609 /* create the image */
2610 image = opj_image_create(numcomps, &cmptparm[0], color_space);
2615 /* set image offset and reference grid */
2616 image->x0 = parameters->image_offset_x0;
2617 image->y0 = parameters->image_offset_y0;
2618 image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
2619 image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
2621 if(raw_cp->rawBitDepth <= 8)
2623 unsigned char value = 0;
2624 for(compno = 0; compno < numcomps; compno++) {
2625 for (i = 0; i < w * h; i++) {
2626 if (!fread(&value, 1, 1, f)) {
2627 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2630 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2634 else if(raw_cp->rawBitDepth <= 16)
2636 unsigned short value;
2637 for(compno = 0; compno < numcomps; compno++) {
2638 for (i = 0; i < w * h; i++) {
2640 if (!fread(&temp, 1, 1, f)) {
2641 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2645 if (!fread(&temp, 1, 1, f)) {
2646 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2650 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2655 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2659 if (fread(&ch, 1, 1, f)) {
2660 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2667 int imagetoraw(opj_image_t * image, const char *outfile)
2669 FILE *rawFile = NULL;
2675 if((image->numcomps * image->x1 * image->y1) == 0)
2677 fprintf(stderr,"\nError: invalid raw image parameters\n");
2681 rawFile = fopen(outfile, "wb");
2683 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2687 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2689 for(compno = 0; compno < image->numcomps; compno++)
2691 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2692 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2694 w = image->comps[compno].w;
2695 h = image->comps[compno].h;
2697 if(image->comps[compno].prec <= 8)
2699 if(image->comps[compno].sgnd == 1)
2702 int mask = (1 << image->comps[compno].prec) - 1;
2703 ptr = image->comps[compno].data;
2704 for (line = 0; line < h; line++) {
2705 for(row = 0; row < w; row++) {
2706 curr = (signed char) (*ptr & mask);
2707 fwrite(&curr, sizeof(signed char), 1, rawFile);
2712 else if(image->comps[compno].sgnd == 0)
2715 int mask = (1 << image->comps[compno].prec) - 1;
2716 ptr = image->comps[compno].data;
2717 for (line = 0; line < h; line++) {
2718 for(row = 0; row < w; row++) {
2719 curr = (unsigned char) (*ptr & mask);
2720 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2726 else if(image->comps[compno].prec <= 16)
2728 if(image->comps[compno].sgnd == 1)
2730 signed short int curr;
2731 int mask = (1 << image->comps[compno].prec) - 1;
2732 ptr = image->comps[compno].data;
2733 for (line = 0; line < h; line++) {
2734 for(row = 0; row < w; row++) {
2736 curr = (signed short int) (*ptr & mask);
2737 temp = (unsigned char) (curr >> 8);
2738 fwrite(&temp, 1, 1, rawFile);
2739 temp = (unsigned char) curr;
2740 fwrite(&temp, 1, 1, rawFile);
2745 else if(image->comps[compno].sgnd == 0)
2747 unsigned short int curr;
2748 int mask = (1 << image->comps[compno].prec) - 1;
2749 ptr = image->comps[compno].data;
2750 for (line = 0; line < h; line++) {
2751 for(row = 0; row < w; row++) {
2753 curr = (unsigned short int) (*ptr & mask);
2754 temp = (unsigned char) (curr >> 8);
2755 fwrite(&temp, 1, 1, rawFile);
2756 temp = (unsigned char) curr;
2757 fwrite(&temp, 1, 1, rawFile);
2763 else if (image->comps[compno].prec <= 32)
2765 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2770 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2780 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
2781 #define MAGIC_SIZE 8
2782 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
2784 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
2788 double gamma, display_exponent;
2789 int bit_depth, interlace_type,compression_type, filter_type;
2791 png_uint_32 resx, resy;
2793 png_uint_32 width, height;
2794 int color_type, has_alpha, is16;
2797 unsigned char **rows;
2800 opj_image_cmptparm_t cmptparm[4];
2802 unsigned int nr_comp;
2804 unsigned char sigbuf[8];
2806 if((reader = fopen(read_idf, "rb")) == NULL)
2808 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
2811 image = NULL; png = NULL; rows = NULL;
2813 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
2814 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
2816 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
2819 /* libpng-VERSION/example.c:
2820 * PC : screen_gamma = 2.2;
2821 * Mac: screen_gamma = 1.7 or 1.0;
2823 display_exponent = 2.2;
2825 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
2826 NULL, NULL, NULL)) == NULL)
2828 if((info = png_create_info_struct(png)) == NULL)
2831 if(setjmp(png_jmpbuf(png)))
2834 png_init_io(png, reader);
2835 png_set_sig_bytes(png, MAGIC_SIZE);
2837 png_read_info(png, info);
2839 if(png_get_IHDR(png, info, &width, &height,
2840 &bit_depth, &color_type, &interlace_type,
2841 &compression_type, &filter_type) == 0)
2844 /* png_set_expand():
2845 * expand paletted images to RGB, expand grayscale images of
2846 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
2847 * to alpha channels.
2849 if(color_type == PNG_COLOR_TYPE_PALETTE)
2850 png_set_expand(png);
2852 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2853 png_set_expand(png);
2855 if(png_get_valid(png, info, PNG_INFO_tRNS))
2856 png_set_expand(png);
2858 is16 = (bit_depth == 16);
2860 /* GRAY => RGB; GRAY_ALPHA => RGBA
2862 if(color_type == PNG_COLOR_TYPE_GRAY
2863 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2865 png_set_gray_to_rgb(png);
2867 (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
2868 PNG_COLOR_TYPE_RGB_ALPHA);
2870 if( !png_get_gAMA(png, info, &gamma))
2873 png_set_gamma(png, display_exponent, gamma);
2875 png_read_update_info(png, info);
2877 png_get_pHYs(png, info, &resx, &resy, &unit);
2879 color_type = png_get_color_type(png, info);
2881 has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
2883 nr_comp = 3 + has_alpha;
2885 bit_depth = png_get_bit_depth(png, info);
2887 rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
2888 for(i = 0; i < height; ++i)
2889 rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
2891 png_read_image(png, rows);
2893 memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
2895 sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
2897 for(i = 0; i < nr_comp; ++i)
2899 cmptparm[i].prec = bit_depth;
2900 /* bits_per_pixel: 8 or 16 */
2901 cmptparm[i].bpp = bit_depth;
2902 cmptparm[i].sgnd = 0;
2903 cmptparm[i].dx = sub_dx;
2904 cmptparm[i].dy = sub_dy;
2905 cmptparm[i].w = width;
2906 cmptparm[i].h = height;
2909 image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
2911 if(image == NULL) goto fin;
2913 image->x0 = params->image_offset_x0;
2914 image->y0 = params->image_offset_y0;
2915 image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
2916 image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
2918 r = image->comps[0].data;
2919 g = image->comps[1].data;
2920 b = image->comps[2].data;
2921 a = image->comps[3].data;
2923 for(i = 0; i < height; ++i)
2927 for(j = 0; j < width; ++j)
2931 *r++ = s[0]<<8|s[1]; s += 2;
2933 *g++ = s[0]<<8|s[1]; s += 2;
2935 *b++ = s[0]<<8|s[1]; s += 2;
2937 if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
2941 *r++ = *s++; *g++ = *s++; *b++ = *s++;
2943 if(has_alpha) *a++ = *s++;
2949 for(i = 0; i < height; ++i)
2954 png_destroy_read_struct(&png, &info, NULL);
2962 int imagetopng(opj_image_t * image, const char *write_idf)
2967 int *red, *green, *blue, *alpha;
2968 unsigned char *row_buf, *d;
2969 int has_alpha, width, height, nr_comp, color_type;
2970 int adjustR, adjustG, adjustB, x, y, fails, is16, force16;
2971 int opj_prec, prec, ushift, dshift;
2972 unsigned short mask = 0xffff;
2973 png_color_8 sig_bit;
2975 is16 = force16 = ushift = dshift = 0; fails = 1;
2976 prec = opj_prec = image->comps[0].prec;
2978 if(prec > 8 && prec < 16)
2980 prec = 16; force16 = 1;
2982 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
2984 fprintf(stderr,"imagetopng: can not create %s"
2985 "\n\twrong bit_depth %d\n", write_idf, prec);
2988 writer = fopen(write_idf, "wb");
2990 if(writer == NULL) return fails;
2992 info = NULL; has_alpha = 0;
2994 /* Create and initialize the png_struct with the desired error handler
2995 * functions. If you want to use the default stderr and longjump method,
2996 * you can supply NULL for the last three parameters. We also check that
2997 * the library version is compatible with the one used at compile time,
2998 * in case we are using dynamically linked libraries. REQUIRED.
3000 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
3002 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
3004 if(png == NULL) goto fin;
3006 /* Allocate/initialize the image information data. REQUIRED
3008 info = png_create_info_struct(png);
3010 if(info == NULL) goto fin;
3012 /* Set error handling. REQUIRED if you are not supplying your own
3013 * error handling functions in the png_create_write_struct() call.
3015 if(setjmp(png_jmpbuf(png))) goto fin;
3017 /* I/O initialization functions is REQUIRED
3019 png_init_io(png, writer);
3021 /* Set the image information here. Width and height are up to 2^31,
3022 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
3023 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
3024 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
3025 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
3026 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
3027 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
3030 png_set_compression_level(png, Z_BEST_COMPRESSION);
3032 if(prec == 16) mask = 0xffff;
3034 if(prec == 8) mask = 0x00ff;
3036 if(prec == 4) mask = 0x000f;
3038 if(prec == 2) mask = 0x0003;
3040 if(prec == 1) mask = 0x0001;
3042 nr_comp = image->numcomps;
3045 && image->comps[0].dx == image->comps[1].dx
3046 && image->comps[1].dx == image->comps[2].dx
3047 && image->comps[0].dy == image->comps[1].dy
3048 && image->comps[1].dy == image->comps[2].dy
3049 && image->comps[0].prec == image->comps[1].prec
3050 && image->comps[1].prec == image->comps[2].prec)
3054 has_alpha = (nr_comp > 3);
3056 is16 = (prec == 16);
3058 width = image->comps[0].w;
3059 height = image->comps[0].h;
3061 red = image->comps[0].data;
3062 green = image->comps[1].data;
3063 blue = image->comps[2].data;
3065 sig_bit.red = sig_bit.green = sig_bit.blue = prec;
3069 sig_bit.alpha = prec;
3070 alpha = image->comps[3].data;
3071 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3075 sig_bit.alpha = 0; alpha = NULL;
3076 color_type = PNG_COLOR_TYPE_RGB;
3078 png_set_sBIT(png, info, &sig_bit);
3080 png_set_IHDR(png, info, width, height, prec,
3083 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3085 /*=============================*/
3086 png_write_info(png, info);
3087 /*=============================*/
3090 png_set_packing(png);
3094 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3096 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3097 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
3098 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
3100 row_buf = (unsigned char*)malloc(width * nr_comp * 2);
3102 for(y = 0; y < height; ++y)
3106 for(x = 0; x < width; ++x)
3110 v = *red + adjustR; ++red;
3112 if(force16) { v = (v<<ushift) + (v>>dshift); }
3114 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3116 v = *green + adjustG; ++green;
3118 if(force16) { v = (v<<ushift) + (v>>dshift); }
3120 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3122 v = *blue + adjustB; ++blue;
3124 if(force16) { v = (v<<ushift) + (v>>dshift); }
3126 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3132 if(force16) { v = (v<<ushift) + (v>>dshift); }
3134 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3138 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3139 *d++ = (unsigned char)((*green + adjustG) & mask); ++green;
3140 *d++ = (unsigned char)((*blue + adjustB) & mask); ++blue;
3144 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3148 png_write_row(png, row_buf);
3155 if(nr_comp == 1 /* GRAY */
3156 || ( nr_comp == 2 /* GRAY_ALPHA */
3157 && image->comps[0].dx == image->comps[1].dx
3158 && image->comps[0].dy == image->comps[1].dy
3159 && image->comps[0].prec == image->comps[1].prec))
3163 red = image->comps[0].data;
3167 ushift = 16 - opj_prec; dshift = opj_prec - ushift;
3169 sig_bit.gray = prec;
3170 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
3172 color_type = PNG_COLOR_TYPE_GRAY;
3176 has_alpha = 1; sig_bit.alpha = prec;
3177 alpha = image->comps[1].data;
3178 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3180 width = image->comps[0].w;
3181 height = image->comps[0].h;
3183 png_set_IHDR(png, info, width, height, sig_bit.gray,
3186 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3188 png_set_sBIT(png, info, &sig_bit);
3189 /*=============================*/
3190 png_write_info(png, info);
3191 /*=============================*/
3192 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
3196 png_set_packing(png);
3201 row_buf = (unsigned char*)
3202 malloc(width * nr_comp * sizeof(unsigned short));
3204 for(y = 0; y < height; ++y)
3208 for(x = 0; x < width; ++x)
3210 v = *red + adjustR; ++red;
3212 if(force16) { v = (v<<ushift) + (v>>dshift); }
3214 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3220 if(force16) { v = (v<<ushift) + (v>>dshift); }
3222 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
3225 png_write_row(png, row_buf);
3230 else /* prec <= 8 */
3232 row_buf = (unsigned char*)calloc(width, nr_comp * 2);
3234 for(y = 0; y < height; ++y)
3238 for(x = 0; x < width; ++x)
3240 *d++ = (unsigned char)((*red + adjustR) & mask); ++red;
3244 *d++ = (unsigned char)(*alpha & mask); ++alpha;
3248 png_write_row(png, row_buf);
3256 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
3259 png_write_end(png, info);
3267 png_destroy_write_struct(&png, &info);
3271 if(fails) remove(write_idf);
3275 #endif /* HAVE_LIBPNG */