add info on how to build doxygen doc with autotools
[openjpeg.git] / codec / convert.c
1 /*
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
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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.
31  */
32 #include <opj_config.h>
33 #define TEST_WITH_GAMMA
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #ifdef HAVE_LIBTIFF
39 #ifdef WIN32
40 #include "tiffio.h"
41 #else
42 #include <tiffio.h>
43 #endif /* WIN32 */
44 #endif /* HAVE_LIBTIFF */
45
46 #ifdef HAVE_LIBPNG
47 #ifdef WIN32
48 #include "png.h"
49 #else
50 #include <png.h>
51 #endif /* WIN32 */
52 #endif /* HAVE_LIBPNG */
53
54 #include "openjpeg.h"
55 #include "convert.h"
56
57 /*
58  * Get logarithm of an integer and round downwards.
59  *
60  * log2(a)
61  */
62 static int int_floorlog2(int a) {
63         int l;
64         for (l = 0; a > 1; l++) {
65                 a >>= 1;
66         }
67         return l;
68 }
69
70 /*
71  * Divide an integer by a power of 2 and round upwards.
72  *
73  * a divided by 2^b
74  */
75 static int int_ceildivpow2(int a, int b) {
76         return (a + (1 << b) - 1) >> b;
77 }
78
79 /*
80  * Divide an integer and round upwards.
81  *
82  * a divided by b
83  */
84 static int int_ceildiv(int a, int b) {
85         return (a + b - 1) / b;
86 }
87
88
89 /* -->> -->> -->> -->>
90
91   TGA IMAGE FORMAT
92
93  <<-- <<-- <<-- <<-- */
94
95 // TGA header definition.
96 #pragma pack(push,1) // Pack structure byte aligned
97 typedef struct tga_header
98 {                           
99     unsigned char   id_length;              /* Image id field length    */
100     unsigned char   colour_map_type;        /* Colour map type          */
101     unsigned char   image_type;             /* Image type               */
102     /*
103     ** Colour map specification
104     */
105     unsigned short  colour_map_index;       /* First entry index        */
106     unsigned short  colour_map_length;      /* Colour map length        */
107     unsigned char   colour_map_entry_size;  /* Colour map entry size    */
108     /*
109     ** Image specification
110     */
111     unsigned short  x_origin;               /* x origin of image        */
112     unsigned short  y_origin;               /* u origin of image        */
113     unsigned short  image_width;            /* Image width              */
114     unsigned short  image_height;           /* Image height             */
115     unsigned char   pixel_depth;            /* Pixel depth              */
116     unsigned char   image_desc;             /* Image descriptor         */
117 } tga_header;
118 #pragma pack(pop) // Return to normal structure packing alignment.
119
120 int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, 
121         unsigned int *width, unsigned int *height, int *flip_image)
122 {
123         int palette_size;
124         tga_header tga ;
125
126         if (!bits_per_pixel || !width || !height || !flip_image)
127                 return 0;
128         
129         // Read TGA header
130         fread((unsigned char*)&tga, sizeof(tga_header), 1, fp);
131
132         *bits_per_pixel = tga.pixel_depth;
133         
134         *width  = tga.image_width;
135         *height = tga.image_height ;
136
137         // Ignore tga identifier, if present ...
138         if (tga.id_length)
139         {
140                 unsigned char *id = (unsigned char *) malloc(tga.id_length);
141                 fread(id, tga.id_length, 1, fp);
142                 free(id);  
143         }
144
145         // Test for compressed formats ... not yet supported ...
146         // Note :-  9 - RLE encoded palettized.
147         //                 10 - RLE encoded RGB.
148         if (tga.image_type > 8)
149         {
150                 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
151                 return 0 ;
152         }
153
154         *flip_image = !(tga.image_desc & 32);
155
156         // Palettized formats are not yet supported, skip over the palette, if present ... 
157         palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
158         
159         if (palette_size>0)
160         {
161                 fprintf(stderr, "File contains a palette - not yet supported.");
162                 fseek(fp, palette_size, SEEK_CUR);
163         }
164         return 1;
165 }
166
167 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, 
168         bool flip_image)
169 {
170         tga_header tga;
171
172         if (!bits_per_pixel || !width || !height)
173                 return 0;
174
175         memset(&tga, 0, sizeof(tga_header));
176
177         tga.pixel_depth = bits_per_pixel;
178         tga.image_width  = width;
179         tga.image_height = height;
180         tga.image_type = 2; // Uncompressed.
181         tga.image_desc = 8; // 8 bits per component.
182
183         if (flip_image)
184                 tga.image_desc |= 32;
185
186         // Write TGA header
187         fwrite((unsigned char*)&tga, sizeof(tga_header), 1, fp);
188
189         return 1;
190 }
191
192 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
193         FILE *f;
194         opj_image_t *image;
195         unsigned int image_width, image_height, pixel_bit_depth;
196         unsigned int x, y;
197         int flip_image=0;
198         opj_image_cmptparm_t cmptparm[4];       /* maximum 4 components */
199         int numcomps;
200         OPJ_COLOR_SPACE color_space;
201         bool mono ;
202         bool save_alpha;
203         int subsampling_dx, subsampling_dy;
204         int i;  
205
206         f = fopen(filename, "rb");
207         if (!f) {
208                 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
209                 return 0;
210         }
211
212         if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
213                 return NULL;
214
215         // We currently only support 24 & 32 bit tga's ...
216         if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
217                 return NULL;
218
219         /* initialize image components */   
220         memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
221
222         mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16);  // Mono with & without alpha.
223         save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
224
225         if (mono) {
226                 color_space = CLRSPC_GRAY;
227                 numcomps = save_alpha ? 2 : 1;
228         }       
229         else {
230                 numcomps = save_alpha ? 4 : 3;
231                 color_space = CLRSPC_SRGB;
232         }
233
234         subsampling_dx = parameters->subsampling_dx;
235         subsampling_dy = parameters->subsampling_dy;
236
237         for (i = 0; i < numcomps; i++) {
238                 cmptparm[i].prec = 8;
239                 cmptparm[i].bpp = 8;
240                 cmptparm[i].sgnd = 0;
241                 cmptparm[i].dx = subsampling_dx;
242                 cmptparm[i].dy = subsampling_dy;
243                 cmptparm[i].w = image_width;
244                 cmptparm[i].h = image_height;
245         }
246
247         /* create the image */
248         image = opj_image_create(numcomps, &cmptparm[0], color_space);
249
250         if (!image)
251                 return NULL;
252
253         /* set image offset and reference grid */
254         image->x0 = parameters->image_offset_x0;
255         image->y0 = parameters->image_offset_y0;
256         image->x1 =     !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
257         image->y1 =     !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
258
259         /* set image data */
260         for (y=0; y < image_height; y++) 
261         {
262                 int index;
263
264                 if (flip_image)
265                         index = (image_height-y-1)*image_width;
266                 else
267                         index = y*image_width;
268
269                 if (numcomps==3)
270                 {
271                         for (x=0;x<image_width;x++) 
272                         {
273                                 unsigned char r,g,b;
274                                 fread(&b, 1, 1, f);
275                                 fread(&g, 1, 1, f);
276                                 fread(&r, 1, 1, f);
277
278                                 image->comps[0].data[index]=r;
279                                 image->comps[1].data[index]=g;
280                                 image->comps[2].data[index]=b;
281                                 index++;
282                         }
283                 }
284                 else if (numcomps==4)
285                 {
286                         for (x=0;x<image_width;x++) 
287                         {
288                                 unsigned char r,g,b,a;
289                                 fread(&b, 1, 1, f);
290                                 fread(&g, 1, 1, f);
291                                 fread(&r, 1, 1, f);
292                                 fread(&a, 1, 1, f);
293
294                                 image->comps[0].data[index]=r;
295                                 image->comps[1].data[index]=g;
296                                 image->comps[2].data[index]=b;
297                                 image->comps[3].data[index]=a;
298                                 index++;
299                         }
300                 }
301                 else {
302                         fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
303                 }
304         }       
305         return image;
306 }
307
308 int imagetotga(opj_image_t * image, const char *outfile) {
309         int width, height, bpp, x, y;
310         bool write_alpha;
311         int i;
312         unsigned int alpha_channel;
313         float r,g,b,a;
314         unsigned char value;
315         float scale;
316         FILE *fdest;
317
318         fdest = fopen(outfile, "wb");
319         if (!fdest) {
320                 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
321                 return 1;
322         }
323
324         for (i = 0; i < image->numcomps-1; i++) {
325                 if ((image->comps[0].dx != image->comps[i+1].dx) 
326                         ||(image->comps[0].dy != image->comps[i+1].dy) 
327                         ||(image->comps[0].prec != image->comps[i+1].prec))     {
328       fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
329       return 1;
330    }
331         }
332
333         width = image->comps[0].w;
334         height = image->comps[0].h; 
335
336         // Mono with alpha, or RGB with alpha.
337         write_alpha = (image->numcomps==2) || (image->numcomps==4);   
338
339         // Write TGA header 
340         bpp = write_alpha ? 32 : 24;
341         if (!tga_writeheader(fdest, bpp, width , height, true))
342                 return 1;
343
344         alpha_channel = image->numcomps-1; 
345
346         scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
347
348         for (y=0; y < height; y++) {
349                 unsigned int index=y*width;
350
351                 for (x=0; x < width; x++, index++)      {
352                         r = (float)(image->comps[0].data[index]);
353
354                         if (image->numcomps>2) {
355                                 g = (float)(image->comps[1].data[index]);
356                                 b = (float)(image->comps[2].data[index]);
357                         }
358                         else  {// Greyscale ...
359                                 g = r;
360                                 b = r;
361                         }
362
363                         // TGA format writes BGR ...
364                         value = (unsigned char)(b*scale);
365                         fwrite(&value,1,1,fdest);
366
367                         value = (unsigned char)(g*scale);
368                         fwrite(&value,1,1,fdest);
369
370                         value = (unsigned char)(r*scale);
371                         fwrite(&value,1,1,fdest);
372
373                         if (write_alpha) {
374                                 a = (float)(image->comps[alpha_channel].data[index]);
375                                 value = (unsigned char)(a*scale);
376                                 fwrite(&value,1,1,fdest);
377                         }
378                 }
379         }
380
381         return 0;
382 }
383
384 /* -->> -->> -->> -->>
385
386   BMP IMAGE FORMAT
387
388  <<-- <<-- <<-- <<-- */
389
390 /* WORD defines a two byte word */
391 typedef unsigned short int WORD;
392
393 /* DWORD defines a four byte word */
394 typedef unsigned long int DWORD;
395
396 typedef struct {
397   WORD bfType;                  /* 'BM' for Bitmap (19776) */
398   DWORD bfSize;                 /* Size of the file        */
399   WORD bfReserved1;             /* Reserved : 0            */
400   WORD bfReserved2;             /* Reserved : 0            */
401   DWORD bfOffBits;              /* Offset                  */
402 } BITMAPFILEHEADER_t;
403
404 typedef struct {
405   DWORD biSize;                 /* Size of the structure in bytes */
406   DWORD biWidth;                /* Width of the image in pixels */
407   DWORD biHeight;               /* Heigth of the image in pixels */
408   WORD biPlanes;                /* 1 */
409   WORD biBitCount;              /* Number of color bits by pixels */
410   DWORD biCompression;          /* Type of encoding 0: none 1: RLE8 2: RLE4 */
411   DWORD biSizeImage;            /* Size of the image in bytes */
412   DWORD biXpelsPerMeter;        /* Horizontal (X) resolution in pixels/meter */
413   DWORD biYpelsPerMeter;        /* Vertical (Y) resolution in pixels/meter */
414   DWORD biClrUsed;              /* Number of color used in the image (0: ALL) */
415   DWORD biClrImportant;         /* Number of important color (0: ALL) */
416 } BITMAPINFOHEADER_t;
417
418 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
419         int subsampling_dx = parameters->subsampling_dx;
420         int subsampling_dy = parameters->subsampling_dy;
421
422         int i, numcomps, w, h;
423         OPJ_COLOR_SPACE color_space;
424         opj_image_cmptparm_t cmptparm[3];       /* maximum of 3 components */
425         opj_image_t * image = NULL;
426
427         FILE *IN;
428         BITMAPFILEHEADER_t File_h;
429         BITMAPINFOHEADER_t Info_h;
430         unsigned char *RGB;
431         unsigned char *table_R, *table_G, *table_B;
432         unsigned int j, PAD = 0;
433
434         int x, y, index;
435         int gray_scale = 1, not_end_file = 1; 
436
437         unsigned int line = 0, col = 0;
438         unsigned char v, v2;
439         DWORD W, H;
440   
441         IN = fopen(filename, "rb");
442         if (!IN) {
443                 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
444                 return 0;
445         }
446         
447         File_h.bfType = getc(IN);
448         File_h.bfType = (getc(IN) << 8) + File_h.bfType;
449         
450         if (File_h.bfType != 19778) {
451                 fprintf(stderr,"Error, not a BMP file!\n");
452                 return 0;
453         } else {
454                 /* FILE HEADER */
455                 /* ------------- */
456                 File_h.bfSize = getc(IN);
457                 File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
458                 File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
459                 File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
460
461                 File_h.bfReserved1 = getc(IN);
462                 File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
463
464                 File_h.bfReserved2 = getc(IN);
465                 File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
466
467                 File_h.bfOffBits = getc(IN);
468                 File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
469                 File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
470                 File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
471
472                 /* INFO HEADER */
473                 /* ------------- */
474
475                 Info_h.biSize = getc(IN);
476                 Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
477                 Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
478                 Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
479
480                 Info_h.biWidth = getc(IN);
481                 Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
482                 Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
483                 Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
484                 w = Info_h.biWidth;
485
486                 Info_h.biHeight = getc(IN);
487                 Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
488                 Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
489                 Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
490                 h = Info_h.biHeight;
491
492                 Info_h.biPlanes = getc(IN);
493                 Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
494
495                 Info_h.biBitCount = getc(IN);
496                 Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
497
498                 Info_h.biCompression = getc(IN);
499                 Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
500                 Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
501                 Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
502
503                 Info_h.biSizeImage = getc(IN);
504                 Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
505                 Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
506                 Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
507
508                 Info_h.biXpelsPerMeter = getc(IN);
509                 Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
510                 Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
511                 Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
512
513                 Info_h.biYpelsPerMeter = getc(IN);
514                 Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
515                 Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
516                 Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
517
518                 Info_h.biClrUsed = getc(IN);
519                 Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
520                 Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
521                 Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
522
523                 Info_h.biClrImportant = getc(IN);
524                 Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
525                 Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
526                 Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
527
528                 /* Read the data and store them in the OUT file */
529     
530                 if (Info_h.biBitCount == 24) {
531                         numcomps = 3;
532                         color_space = CLRSPC_SRGB;
533                         /* initialize image components */
534                         memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
535                         for(i = 0; i < numcomps; i++) {
536                                 cmptparm[i].prec = 8;
537                                 cmptparm[i].bpp = 8;
538                                 cmptparm[i].sgnd = 0;
539                                 cmptparm[i].dx = subsampling_dx;
540                                 cmptparm[i].dy = subsampling_dy;
541                                 cmptparm[i].w = w;
542                                 cmptparm[i].h = h;
543                         }
544                         /* create the image */
545                         image = opj_image_create(numcomps, &cmptparm[0], color_space);
546                         if(!image) {
547                                 fclose(IN);
548                                 return NULL;
549                         }
550
551                         /* set image offset and reference grid */
552                         image->x0 = parameters->image_offset_x0;
553                         image->y0 = parameters->image_offset_y0;
554                         image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
555                         image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
556
557                         /* set image data */
558
559                         /* Place the cursor at the beginning of the image information */
560                         fseek(IN, 0, SEEK_SET);
561                         fseek(IN, File_h.bfOffBits, SEEK_SET);
562                         
563                         W = Info_h.biWidth;
564                         H = Info_h.biHeight;
565
566                         /* PAD = 4 - (3 * W) % 4; */
567                         /* PAD = (PAD == 4) ? 0 : PAD; */
568                         PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
569                         
570                         RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
571                         
572                         fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
573                         
574                         index = 0;
575
576                         for(y = 0; y < (int)H; y++) {
577                                 unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
578                                 for(x = 0; x < (int)W; x++) {
579                                         unsigned char *pixel = &scanline[3 * x];
580                                         image->comps[0].data[index] = pixel[2]; /* R */
581                                         image->comps[1].data[index] = pixel[1]; /* G */
582                                         image->comps[2].data[index] = pixel[0]; /* B */
583                                         index++;
584                                 }
585                         }
586
587                         free(RGB);
588
589                 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
590                         table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
591                         table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
592                         table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
593                         
594                         for (j = 0; j < Info_h.biClrUsed; j++) {
595                                 table_B[j] = getc(IN);
596                                 table_G[j] = getc(IN);
597                                 table_R[j] = getc(IN);
598                                 getc(IN);
599                                 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
600                                         gray_scale = 0;
601                         }
602                         
603                         /* Place the cursor at the beginning of the image information */
604                         fseek(IN, 0, SEEK_SET);
605                         fseek(IN, File_h.bfOffBits, SEEK_SET);
606                         
607                         W = Info_h.biWidth;
608                         H = Info_h.biHeight;
609                         if (Info_h.biWidth % 2)
610                                 W++;
611                         
612                         numcomps = gray_scale ? 1 : 3;
613                         color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
614                         /* initialize image components */
615                         memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
616                         for(i = 0; i < numcomps; i++) {
617                                 cmptparm[i].prec = 8;
618                                 cmptparm[i].bpp = 8;
619                                 cmptparm[i].sgnd = 0;
620                                 cmptparm[i].dx = subsampling_dx;
621                                 cmptparm[i].dy = subsampling_dy;
622                                 cmptparm[i].w = w;
623                                 cmptparm[i].h = h;
624                         }
625                         /* create the image */
626                         image = opj_image_create(numcomps, &cmptparm[0], color_space);
627                         if(!image) {
628                                 fclose(IN);
629                                 return NULL;
630                         }
631
632                         /* set image offset and reference grid */
633                         image->x0 = parameters->image_offset_x0;
634                         image->y0 = parameters->image_offset_y0;
635                         image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
636                         image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
637
638                         /* set image data */
639
640                         RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
641                         
642                         fread(RGB, sizeof(unsigned char), W * H, IN);
643                         if (gray_scale) {
644                                 index = 0;
645                                 for (j = 0; j < W * H; j++) {
646                                         if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
647                                                 image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
648                                                 index++;
649                                         }
650                                 }
651
652                         } else {                
653                                 index = 0;
654                                 for (j = 0; j < W * H; j++) {
655                                         if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
656                                                 unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
657                                                 image->comps[0].data[index] = table_R[pixel_index];
658                                                 image->comps[1].data[index] = table_G[pixel_index];
659                                                 image->comps[2].data[index] = table_B[pixel_index];
660                                                 index++;
661                                         }
662                                 }
663                         }
664                         free(RGB);
665       free(table_R);
666       free(table_G);
667       free(table_B);
668                 } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {                               
669                         table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
670                         table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
671                         table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
672                         
673                         for (j = 0; j < Info_h.biClrUsed; j++) {
674                                 table_B[j] = getc(IN);
675                                 table_G[j] = getc(IN);
676                                 table_R[j] = getc(IN);
677                                 getc(IN);
678                                 if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
679                                         gray_scale = 0;
680                         }
681
682                         numcomps = gray_scale ? 1 : 3;
683                         color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
684                         /* initialize image components */
685                         memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
686                         for(i = 0; i < numcomps; i++) {
687                                 cmptparm[i].prec = 8;
688                                 cmptparm[i].bpp = 8;
689                                 cmptparm[i].sgnd = 0;
690                                 cmptparm[i].dx = subsampling_dx;
691                                 cmptparm[i].dy = subsampling_dy;
692                                 cmptparm[i].w = w;
693                                 cmptparm[i].h = h;
694                         }
695                         /* create the image */
696                         image = opj_image_create(numcomps, &cmptparm[0], color_space);
697                         if(!image) {
698                                 fclose(IN);
699                                 return NULL;
700                         }
701
702                         /* set image offset and reference grid */
703                         image->x0 = parameters->image_offset_x0;
704                         image->y0 = parameters->image_offset_y0;
705                         image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
706                         image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
707
708                         /* set image data */
709                         
710                         /* Place the cursor at the beginning of the image information */
711                         fseek(IN, 0, SEEK_SET);
712                         fseek(IN, File_h.bfOffBits, SEEK_SET);
713                         
714                         RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
715             
716                         while (not_end_file) {
717                                 v = getc(IN);
718                                 if (v) {
719                                         v2 = getc(IN);
720                                         for (i = 0; i < (int) v; i++) {
721                                                 RGB[line * Info_h.biWidth + col] = v2;
722                                                 col++;
723                                         }
724                                 } else {
725                                         v = getc(IN);
726                                         switch (v) {
727                                                 case 0:
728                                                         col = 0;
729                                                         line++;
730                                                         break;
731                                                 case 1:
732                                                         line++;
733                                                         not_end_file = 0;
734                                                         break;
735                                                 case 2:
736                                                         fprintf(stderr,"No Delta supported\n");
737                                                         opj_image_destroy(image);
738                                                         fclose(IN);
739                                                         return NULL;
740                                                 default:
741                                                         for (i = 0; i < v; i++) {
742                                                                 v2 = getc(IN);
743                                                                 RGB[line * Info_h.biWidth + col] = v2;
744                                                                 col++;
745                                                         }
746                                                         if (v % 2)
747                                                                 v2 = getc(IN);
748                                                         break;
749                                         }
750                                 }
751                         }
752                         if (gray_scale) {
753                                 index = 0;
754                                 for (line = 0; line < Info_h.biHeight; line++) {
755                                         for (col = 0; col < Info_h.biWidth; col++) {
756                                                 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
757                                                 index++;
758                                         }
759                                 }
760                         } else {
761                                 index = 0;
762                                 for (line = 0; line < Info_h.biHeight; line++) {
763                                         for (col = 0; col < Info_h.biWidth; col++) {
764                                                 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
765                                                 image->comps[0].data[index] = table_R[pixel_index];
766                                                 image->comps[1].data[index] = table_G[pixel_index];
767                                                 image->comps[2].data[index] = table_B[pixel_index];
768                                                 index++;
769                                         }
770                                 }
771                         }
772                         free(RGB);
773       free(table_R);
774       free(table_G);
775       free(table_B);
776         } else {
777                 fprintf(stderr, 
778                         "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
779         }
780         fclose(IN);
781  }
782  
783  return image;
784 }
785
786 int imagetobmp(opj_image_t * image, const char *outfile) {
787         int w, h;
788         int i, pad;
789         FILE *fdest = NULL;
790         int adjustR, adjustG, adjustB;
791
792         if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
793                 && image->comps[1].dx == image->comps[2].dx
794                 && image->comps[0].dy == image->comps[1].dy
795                 && image->comps[1].dy == image->comps[2].dy
796                 && image->comps[0].prec == image->comps[1].prec
797                 && image->comps[1].prec == image->comps[2].prec) {
798                 
799                 /* -->> -->> -->> -->>    
800                 24 bits color       
801                 <<-- <<-- <<-- <<-- */
802             
803                 fdest = fopen(outfile, "wb");
804                 if (!fdest) {
805                         fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
806                         return 1;
807                 }
808             
809                 w = image->comps[0].w;      
810                 h = image->comps[0].h;
811             
812                 fprintf(fdest, "BM");
813             
814                 /* FILE HEADER */
815                 /* ------------- */
816                 fprintf(fdest, "%c%c%c%c",
817                         (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
818                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54)     >> 8) & 0xff,
819                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54)     >> 16) & 0xff,
820                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54)     >> 24) & 0xff);
821                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
822                 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
823             
824                 /* INFO HEADER   */
825                 /* ------------- */
826                 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,     ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
827                 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
828                         (unsigned char) ((w) >> 8) & 0xff,
829                         (unsigned char) ((w) >> 16) & 0xff,
830                         (unsigned char) ((w) >> 24) & 0xff);
831                 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
832                         (unsigned char) ((h) >> 8) & 0xff,
833                         (unsigned char) ((h) >> 16) & 0xff,
834                         (unsigned char) ((h) >> 24) & 0xff);
835                 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
836                 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
837                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
838                 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
839                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
840                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
841                         (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
842                 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
843                 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
844                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
845                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
846             
847                 if (image->comps[0].prec > 8) {
848                         adjustR = image->comps[0].prec - 8;
849                         printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
850                 }
851                 else 
852                         adjustR = 0;
853                 if (image->comps[1].prec > 8) {
854                         adjustG = image->comps[1].prec - 8;
855                         printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
856                 }
857                 else 
858                         adjustG = 0;
859                 if (image->comps[2].prec > 8) {
860                         adjustB = image->comps[2].prec - 8;
861                         printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
862                 }
863                 else 
864                         adjustB = 0;
865
866                 for (i = 0; i < w * h; i++) {
867                         unsigned char rc, gc, bc;
868                         int r, g, b;
869                                                         
870                         r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
871                         r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
872                         rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
873                         g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
874                         g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
875                         gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
876                         b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
877                         b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
878                         bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
879
880                         fprintf(fdest, "%c%c%c", bc, gc, rc);
881                         
882                         if ((i + 1) % w == 0) {
883                                 for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--)   /* ADD */
884                                         fprintf(fdest, "%c", 0);
885                         }
886                 }
887                 fclose(fdest);
888         } else {                        /* Gray-scale */
889
890                 /* -->> -->> -->> -->>
891                 8 bits non code (Gray scale)
892                 <<-- <<-- <<-- <<-- */
893
894                 fdest = fopen(outfile, "wb");
895                 w = image->comps[0].w;      
896                 h = image->comps[0].h;
897             
898                 fprintf(fdest, "BM");
899             
900                 /* FILE HEADER */
901                 /* ------------- */
902                 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
903                         (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
904                         (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
905                         (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
906                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
907                 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, 
908                         ((54 + 1024) >> 16) & 0xff,
909                         ((54 + 1024) >> 24) & 0xff);
910             
911                 /* INFO HEADER */
912                 /* ------------- */
913                 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,     ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
914                 fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
915                         (unsigned char) ((w) >> 8) & 0xff,
916                         (unsigned char) ((w) >> 16) & 0xff,
917                         (unsigned char) ((w) >> 24) & 0xff);
918                 fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
919                         (unsigned char) ((h) >> 8) & 0xff,
920                         (unsigned char) ((h) >> 16) & 0xff,
921                         (unsigned char) ((h) >> 24) & 0xff);
922                 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
923                 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
924                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
925                 fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
926                         (unsigned char) ((h * w + h * (w % 2)) >> 8) &  0xff,
927                         (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
928                         (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
929                 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
930                 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
931                 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
932                 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
933
934                 if (image->comps[0].prec > 8) {
935                         adjustR = image->comps[0].prec - 8;
936                         printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
937                 }else 
938                         adjustR = 0;
939
940                 for (i = 0; i < 256; i++) {
941                         fprintf(fdest, "%c%c%c%c", i, i, i, 0);
942                 }
943
944                 for (i = 0; i < w * h; i++) {
945                         unsigned char rc;
946                         int r;
947                         
948                         r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
949                         r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
950                         rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
951                         
952                         fprintf(fdest, "%c", rc);
953
954                         if ((i + 1) % w == 0) {
955                                 for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--)       /* ADD */
956                                         fprintf(fdest, "%c", 0);
957                         }
958                 }
959                 fclose(fdest);
960         }
961
962         return 0;
963 }
964
965 /* -->> -->> -->> -->>
966
967 PGX IMAGE FORMAT
968
969 <<-- <<-- <<-- <<-- */
970
971
972 unsigned char readuchar(FILE * f)
973 {
974   unsigned char c1;
975   fread(&c1, 1, 1, f);
976   return c1;
977 }
978
979 unsigned short readushort(FILE * f, int bigendian)
980 {
981   unsigned char c1, c2;
982   fread(&c1, 1, 1, f);
983   fread(&c2, 1, 1, f);
984   if (bigendian)
985     return (c1 << 8) + c2;
986   else
987     return (c2 << 8) + c1;
988 }
989
990 unsigned int readuint(FILE * f, int bigendian)
991 {
992   unsigned char c1, c2, c3, c4;
993   fread(&c1, 1, 1, f);
994   fread(&c2, 1, 1, f);
995   fread(&c3, 1, 1, f);
996   fread(&c4, 1, 1, f);
997   if (bigendian)
998     return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
999   else
1000     return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
1001 }
1002
1003 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1004         FILE *f = NULL;
1005         int w, h, prec;
1006         int i, numcomps, max;
1007         OPJ_COLOR_SPACE color_space;
1008         opj_image_cmptparm_t cmptparm;  /* maximum of 1 component  */
1009         opj_image_t * image = NULL;
1010
1011         char endian1,endian2,sign;
1012         char signtmp[32];
1013
1014         char temp[32];
1015         int bigendian;
1016         opj_image_comp_t *comp = NULL;
1017
1018         numcomps = 1;
1019         color_space = CLRSPC_GRAY;
1020
1021         memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1022
1023         max = 0;
1024
1025         f = fopen(filename, "rb");
1026         if (!f) {
1027           fprintf(stderr, "Failed to open %s for reading !\n", filename);
1028           return NULL;
1029         }
1030
1031         fseek(f, 0, SEEK_SET);
1032         fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
1033         
1034         i=0;
1035         sign='+';               
1036         while (signtmp[i]!='\0') {
1037                 if (signtmp[i]=='-') sign='-';
1038                 i++;
1039         }
1040         
1041         fgetc(f);
1042         if (endian1=='M' && endian2=='L') {
1043                 bigendian = 1;
1044         } else if (endian2=='M' && endian1=='L') {
1045                 bigendian = 0;
1046         } else {
1047                 fprintf(stderr, "Bad pgx header, please check input file\n");
1048                 return NULL;
1049         }
1050
1051         /* initialize image component */
1052
1053         cmptparm.x0 = parameters->image_offset_x0;
1054         cmptparm.y0 = parameters->image_offset_y0;
1055         cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
1056         cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
1057         
1058         if (sign == '-') {
1059                 cmptparm.sgnd = 1;
1060         } else {
1061                 cmptparm.sgnd = 0;
1062         }
1063         cmptparm.prec = prec;
1064         cmptparm.bpp = prec;
1065         cmptparm.dx = parameters->subsampling_dx;
1066         cmptparm.dy = parameters->subsampling_dy;
1067         
1068         /* create the image */
1069         image = opj_image_create(numcomps, &cmptparm, color_space);
1070         if(!image) {
1071                 fclose(f);
1072                 return NULL;
1073         }
1074         /* set image offset and reference grid */
1075         image->x0 = cmptparm.x0;
1076         image->y0 = cmptparm.x0;
1077         image->x1 = cmptparm.w;
1078         image->y1 = cmptparm.h;
1079
1080         /* set image data */
1081
1082         comp = &image->comps[0];
1083
1084         for (i = 0; i < w * h; i++) {
1085                 int v;
1086                 if (comp->prec <= 8) {
1087                         if (!comp->sgnd) {
1088                                 v = readuchar(f);
1089                         } else {
1090                                 v = (char) readuchar(f);
1091                         }
1092                 } else if (comp->prec <= 16) {
1093                         if (!comp->sgnd) {
1094                                 v = readushort(f, bigendian);
1095                         } else {
1096                                 v = (short) readushort(f, bigendian);
1097                         }
1098                 } else {
1099                         if (!comp->sgnd) {
1100                                 v = readuint(f, bigendian);
1101                         } else {
1102                                 v = (int) readuint(f, bigendian);
1103                         }
1104                 }
1105                 if (v > max)
1106                         max = v;
1107                 comp->data[i] = v;
1108         }
1109         fclose(f);
1110         comp->bpp = int_floorlog2(max) + 1;
1111
1112         return image;
1113 }
1114
1115 int imagetopgx(opj_image_t * image, const char *outfile) {
1116         int w, h;
1117         int i, j, compno;
1118         FILE *fdest = NULL;
1119
1120         for (compno = 0; compno < image->numcomps; compno++) {
1121                 opj_image_comp_t *comp = &image->comps[compno];
1122                 char bname[256]; /* buffer for name */
1123     char *name = bname; /* pointer */
1124     int nbytes = 0;
1125     const size_t olen = strlen(outfile);
1126     const size_t dotpos = olen - 4;
1127     const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1128     if( outfile[dotpos] != '.' ) {
1129       /* `pgx` was recognized but there is no dot at expected position */
1130       fprintf(stderr, "ERROR -> Impossible happen." );
1131       return 1;
1132       }
1133     if( total > 256 ) {
1134       name = (char*)malloc(total+1);
1135       }
1136     strncpy(name, outfile, dotpos);
1137                 if (image->numcomps > 1) {
1138                         sprintf(name+dotpos, "-%d.pgx", compno);
1139                 } else {
1140                         strcpy(name+dotpos, ".pgx");
1141                 }
1142                 fdest = fopen(name, "wb");
1143                 if (!fdest) {
1144                         fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1145                         return 1;
1146                 }
1147     /* dont need name anymore */
1148     if( total > 256 ) {
1149       free(name);
1150       }
1151
1152                 w = image->comps[compno].w;
1153                 h = image->comps[compno].h;
1154             
1155                 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
1156                 if (comp->prec <= 8) {
1157                         nbytes = 1;
1158                 } else if (comp->prec <= 16) {
1159                         nbytes = 2;
1160                 } else {
1161                         nbytes = 4;
1162                 }
1163                 for (i = 0; i < w * h; i++) {
1164                         int v = image->comps[compno].data[i];
1165                         for (j = nbytes - 1; j >= 0; j--) {
1166                                 char byte = (char) (v >> (j * 8));
1167                                 fwrite(&byte, 1, 1, fdest);
1168                         }
1169                 }
1170                 fclose(fdest);
1171         }
1172
1173         return 0;
1174 }
1175
1176 /* -->> -->> -->> -->>
1177
1178 PNM IMAGE FORMAT
1179
1180 <<-- <<-- <<-- <<-- */
1181
1182 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1183         int subsampling_dx = parameters->subsampling_dx;
1184         int subsampling_dy = parameters->subsampling_dy;
1185
1186         FILE *f = NULL;
1187         int i, compno, numcomps, w, h;
1188         OPJ_COLOR_SPACE color_space;
1189         opj_image_cmptparm_t cmptparm[3];       /* maximum of 3 components */
1190         opj_image_t * image = NULL;
1191         char value;
1192         
1193         f = fopen(filename, "rb");
1194         if (!f) {
1195                 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1196                 return 0;
1197         }
1198
1199         if (fgetc(f) != 'P')
1200                 return 0;
1201         value = fgetc(f);
1202
1203                 switch(value) {
1204                         case '2':       /* greyscale image type */
1205                         case '5':
1206                                 numcomps = 1;
1207                                 color_space = CLRSPC_GRAY;
1208                                 break;
1209                                 
1210                         case '3':       /* RGB image type */
1211                         case '6':
1212                                 numcomps = 3;
1213                                 color_space = CLRSPC_SRGB;
1214                                 break;
1215                                 
1216                         default:
1217                                 fclose(f);
1218                                 return NULL;
1219                 }
1220                 
1221                 fgetc(f);
1222                 
1223                 /* skip comments */
1224                 while(fgetc(f) == '#') while(fgetc(f) != '\n');
1225                 
1226                 fseek(f, -1, SEEK_CUR);
1227                 fscanf(f, "%d %d\n255", &w, &h);                        
1228                 fgetc(f);       /* <cr><lf> */
1229                 
1230         /* initialize image components */
1231         memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1232         for(i = 0; i < numcomps; i++) {
1233                 cmptparm[i].prec = 8;
1234                 cmptparm[i].bpp = 8;
1235                 cmptparm[i].sgnd = 0;
1236                 cmptparm[i].dx = subsampling_dx;
1237                 cmptparm[i].dy = subsampling_dy;
1238                 cmptparm[i].w = w;
1239                 cmptparm[i].h = h;
1240         }
1241         /* create the image */
1242         image = opj_image_create(numcomps, &cmptparm[0], color_space);
1243         if(!image) {
1244                 fclose(f);
1245                 return NULL;
1246         }
1247
1248         /* set image offset and reference grid */
1249         image->x0 = parameters->image_offset_x0;
1250         image->y0 = parameters->image_offset_y0;
1251         image->x1 = parameters->image_offset_x0 + (w - 1) *     subsampling_dx + 1;
1252         image->y1 = parameters->image_offset_y0 + (h - 1) *     subsampling_dy + 1;
1253
1254         /* set image data */
1255
1256         if ((value == '2') || (value == '3')) { /* ASCII */
1257                 for (i = 0; i < w * h; i++) {
1258                         for(compno = 0; compno < numcomps; compno++) {
1259                                 unsigned int index = 0;
1260                                 fscanf(f, "%u", &index);
1261                                 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1262                                 image->comps[compno].data[i] = index;
1263                         }
1264                 }
1265         } else if ((value == '5') || (value == '6')) {  /* BINARY */
1266                 for (i = 0; i < w * h; i++) {
1267                         for(compno = 0; compno < numcomps; compno++) {
1268                                 unsigned char index = 0;
1269                                 fread(&index, 1, 1, f);
1270                                 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
1271                                 image->comps[compno].data[i] = index;
1272                         }
1273                 }
1274         }
1275
1276         fclose(f);
1277
1278         return image;
1279 }
1280
1281 int imagetopnm(opj_image_t * image, const char *outfile) {
1282         int w, wr, h, hr, max;
1283         int i, compno;
1284         int adjustR, adjustG, adjustB, adjustX;
1285         FILE *fdest = NULL;
1286         char S2;
1287         const char *tmp = outfile;
1288
1289         while (*tmp) {
1290                 tmp++;
1291         }
1292         tmp--;
1293         tmp--;
1294         S2 = *tmp;
1295
1296         if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1297                 && image->comps[1].dx == image->comps[2].dx
1298                 && image->comps[0].dy == image->comps[1].dy
1299                 && image->comps[1].dy == image->comps[2].dy
1300                 && image->comps[0].prec == image->comps[1].prec
1301                 && image->comps[1].prec == image->comps[2].prec
1302                 && S2 !='g' && S2 !='G') {
1303
1304                 fdest = fopen(outfile, "wb");
1305                 if (!fdest) {
1306                         fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1307                         return 1;
1308                 }
1309
1310                 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
1311                 wr = image->comps[0].w;
1312         
1313                 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
1314                 hr = image->comps[0].h;
1315             
1316                 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
1317             
1318                 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
1319                 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 -       int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
1320
1321                 fprintf(fdest, "P6\n%d %d\n%d\n", wr, hr, max);
1322
1323                 if (image->comps[0].prec > 8) {
1324                         adjustR = image->comps[0].prec - 8;
1325                         printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
1326                 }
1327                 else 
1328                         adjustR = 0;
1329                 if (image->comps[1].prec > 8) {
1330                         adjustG = image->comps[1].prec - 8;
1331                         printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
1332                 }
1333                 else 
1334                         adjustG = 0;
1335                 if (image->comps[2].prec > 8) {
1336                         adjustB = image->comps[2].prec - 8;
1337                         printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
1338                 }
1339                 else 
1340                         adjustB = 0;
1341
1342
1343                 for (i = 0; i < wr * hr; i++) {
1344                         int r, g, b;
1345                         unsigned char rc,gc,bc;
1346                         r = image->comps[0].data[i];
1347                         r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1348                         rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
1349
1350                         g = image->comps[1].data[i];
1351                         g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1352                         gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
1353                         
1354                         b = image->comps[2].data[i];
1355                         b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1356                         bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
1357                         
1358                         fprintf(fdest, "%c%c%c", rc, gc, bc);
1359                 }
1360                 fclose(fdest);
1361
1362         } else {
1363                 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1364                 if (image->numcomps > ncomp) {
1365                         fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1366                         fprintf(stderr,"           is written to the file\n");
1367                 }
1368                 for (compno = 0; compno < ncomp; compno++) {
1369                         char name[256];
1370                         if (ncomp > 1) {
1371                                 sprintf(name, "%d.%s", compno, outfile);
1372                         } else {
1373                                 sprintf(name, "%s", outfile);
1374                         }
1375                         
1376                         fdest = fopen(name, "wb");
1377                         if (!fdest) {
1378                                 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1379                                 return 1;
1380                         }
1381             
1382                         w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1383                         wr = image->comps[compno].w;
1384                         
1385                         h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1386                         hr = image->comps[compno].h;
1387                         
1388                         max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1389                         
1390                         image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1391                         image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1392                         
1393                         fprintf(fdest, "P5\n%d %d\n%d\n", wr, hr, max);
1394                         
1395                         if (image->comps[compno].prec > 8) {
1396                                 adjustX = image->comps[0].prec - 8;
1397                                 printf("PNM CONVERSION: Truncating component %d from %d bits to 8 bits\n",compno, image->comps[compno].prec);
1398                         }
1399                         else 
1400                                 adjustX = 0;
1401                         
1402                         for (i = 0; i < wr * hr; i++) {
1403                                 int l;
1404                                 unsigned char lc;
1405                                 l = image->comps[compno].data[i];
1406                                 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1407                                 lc = (unsigned char) ((l >> adjustX)+((l >> (adjustX-1))%2));
1408                                 fprintf(fdest, "%c", lc);
1409                         }
1410                         fclose(fdest);
1411                 }
1412         }
1413
1414         return 0;
1415 }
1416
1417 #ifdef HAVE_LIBTIFF
1418 /* -->> -->> -->> -->>
1419
1420         TIFF IMAGE FORMAT
1421
1422  <<-- <<-- <<-- <<-- */
1423
1424 typedef struct tiff_infoheader{
1425         DWORD tiWidth;  // Width of Image in pixel
1426         DWORD tiHeight; // Height of Image in pixel
1427         DWORD tiPhoto;  // Photometric
1428         WORD  tiBps;    // Bits per sample
1429         WORD  tiSf;             // Sample Format
1430         WORD  tiSpp;    // Sample per pixel 1-bilevel,gray scale , 2- RGB
1431         WORD  tiPC;     // Planar config (1-Interleaved, 2-Planarcomp)
1432 }tiff_infoheader_t;
1433
1434 int imagetotif(opj_image_t * image, const char *outfile) {
1435         int width, height, imgsize;
1436         int bps,index,adjust = 0;
1437         int last_i=0;
1438         TIFF *tif;
1439         tdata_t buf;
1440         tstrip_t strip;
1441         tsize_t strip_size;
1442
1443         if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
1444                 && image->comps[1].dx == image->comps[2].dx
1445                 && image->comps[0].dy == image->comps[1].dy
1446                 && image->comps[1].dy == image->comps[2].dy
1447                 && image->comps[0].prec == image->comps[1].prec
1448                 && image->comps[1].prec == image->comps[2].prec) {
1449
1450                         /* -->> -->> -->>    
1451                         RGB color           
1452                         <<-- <<-- <<-- */
1453
1454                         tif = TIFFOpen(outfile, "wb"); 
1455                         if (!tif) {
1456                                 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1457                                 return 1;
1458                         }
1459
1460                         width   = image->comps[0].w;
1461                         height  = image->comps[0].h;
1462                         imgsize = width * height ;
1463                         bps             = image->comps[0].prec;
1464                         /* Set tags */
1465                         TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1466                         TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1467                         TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1468                         TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1469                         TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1470                         TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1471                         TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1472                         TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1473
1474                         /* Get a buffer for the data */
1475                         strip_size=TIFFStripSize(tif);
1476                         buf = _TIFFmalloc(strip_size);
1477                         index=0;                
1478                         adjust = image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0;
1479                         for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1480                                 unsigned char *dat8;
1481                                 int i, ssize;
1482                                 ssize = TIFFStripSize(tif);
1483                                 dat8 = (unsigned char*)buf;
1484                                 if (image->comps[0].prec == 8){
1485                                         for (i=0; i<ssize-2; i+=3) {    // 8 bits per pixel 
1486                                                 int r = 0,g = 0,b = 0;
1487                                                 if(index < imgsize){
1488                                                         r = image->comps[0].data[index];
1489                                                         g = image->comps[1].data[index];
1490                                                         b = image->comps[2].data[index];
1491                                                         if (image->comps[0].sgnd){                      
1492                                                                 r += adjust;
1493                                                                 g += adjust;
1494                                                                 b += adjust;
1495                                                         }
1496                                                         dat8[i+0] = r ; // R 
1497                                                         dat8[i+1] = g ; // G 
1498                                                         dat8[i+2] = b ; // B 
1499                                                         index++;
1500                                                         last_i = i+3;
1501                                                 }else
1502                                                         break;
1503                                         }
1504                                         if(last_i < ssize){
1505                                                 for (i=last_i; i<ssize; i+=3) { // 8 bits per pixel 
1506                                                         int r = 0,g = 0,b = 0;
1507                                                         if(index < imgsize){
1508                                                                 r = image->comps[0].data[index];
1509                                                                 g = image->comps[1].data[index];
1510                                                                 b = image->comps[2].data[index];
1511                                                                 if (image->comps[0].sgnd){                      
1512                                                                         r += adjust;
1513                                                                         g += adjust;
1514                                                                         b += adjust;
1515                                                                 }
1516                                                                 dat8[i+0] = r ; // R 
1517                                                                 if(i+1 <ssize) dat8[i+1] = g ;  else break;// G 
1518                                                                 if(i+2 <ssize) dat8[i+2] = b ;  else break;// B 
1519                                                                 index++;
1520                                                         }else
1521                                                                 break;
1522                                                 }
1523                                         }
1524                                 }else if (image->comps[0].prec == 12){
1525                                         for (i=0; i<ssize-8; i+=9) {    // 12 bits per pixel 
1526                                                 int r = 0,g = 0,b = 0;
1527                                                 int r1 = 0,g1 = 0,b1 = 0;
1528                                                 if((index < imgsize)&(index+1 < imgsize)){
1529                                                         r  = image->comps[0].data[index];
1530                                                         g  = image->comps[1].data[index];
1531                                                         b  = image->comps[2].data[index];
1532                                                         r1 = image->comps[0].data[index+1];
1533                                                         g1 = image->comps[1].data[index+1];
1534                                                         b1 = image->comps[2].data[index+1];
1535                                                         if (image->comps[0].sgnd){                                                                                                              
1536                                                                 r  += adjust;
1537                                                                 g  += adjust;
1538                                                                 b  += adjust;
1539                                                                 r1 += adjust;
1540                                                                 g1 += adjust;
1541                                                                 b1 += adjust;
1542                                                         }
1543                                                         dat8[i+0] = (r >> 4);
1544                                                         dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f);
1545                                                         dat8[i+2] = g ;         
1546                                                         dat8[i+3] = (b >> 4);
1547                                                         dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1548                                                         dat8[i+5] = r1;         
1549                                                         dat8[i+6] = (g1 >> 4);
1550                                                         dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);
1551                                                         dat8[i+8] = b1;
1552                                                         index+=2;
1553                                                         last_i = i+9;
1554                                                 }else
1555                                                         break;
1556                                         }
1557                                         if(last_i < ssize){
1558                                                 for (i= last_i; i<ssize; i+=9) {        // 12 bits per pixel 
1559                                                         int r = 0,g = 0,b = 0;
1560                                                         int r1 = 0,g1 = 0,b1 = 0;
1561                                                         if((index < imgsize)&(index+1 < imgsize)){
1562                                                                 r  = image->comps[0].data[index];
1563                                                                 g  = image->comps[1].data[index];
1564                                                                 b  = image->comps[2].data[index];
1565                                                                 r1 = image->comps[0].data[index+1];
1566                                                                 g1 = image->comps[1].data[index+1];
1567                                                                 b1 = image->comps[2].data[index+1];
1568                                                                 if (image->comps[0].sgnd){                                                                                                              
1569                                                                         r  += adjust;
1570                                                                         g  += adjust;
1571                                                                         b  += adjust;
1572                                                                         r1 += adjust;
1573                                                                         g1 += adjust;
1574                                                                         b1 += adjust;
1575                                                                 }
1576                                                                 dat8[i+0] = (r >> 4);
1577                                                                 if(i+1 <ssize) dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f); else break;
1578                                                                 if(i+2 <ssize) dat8[i+2] = g ;                  else break;
1579                                                                 if(i+3 <ssize) dat8[i+3] = (b >> 4);    else break;
1580                                                                 if(i+4 <ssize) dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);else break;
1581                                                                 if(i+5 <ssize) dat8[i+5] = r1;                  else break;
1582                                                                 if(i+6 <ssize) dat8[i+6] = (g1 >> 4);   else break;
1583                                                                 if(i+7 <ssize) dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);else break;
1584                                                                 if(i+8 <ssize) dat8[i+8] = b1;                  else break;
1585                                                                 index+=2;
1586                                                         }else
1587                                                                 break;
1588                                                 }
1589                                         }
1590                                 }else if (image->comps[0].prec == 16){
1591                                         for (i=0 ; i<ssize-5 ; i+=6) {  // 16 bits per pixel 
1592                                                 int r = 0,g = 0,b = 0;
1593                                                 if(index < imgsize){
1594                                                         r = image->comps[0].data[index];
1595                                                         g = image->comps[1].data[index];
1596                                                         b = image->comps[2].data[index];
1597                                                         if (image->comps[0].sgnd){
1598                                                         r += adjust;
1599                                                         g += adjust;
1600                                                         b += adjust;
1601                                                         }
1602                                                         dat8[i+0] =  r;//LSB
1603                                                         dat8[i+1] = (r >> 8);//MSB       
1604                                                         dat8[i+2] =  g;         
1605                                                         dat8[i+3] = (g >> 8);
1606                                                         dat8[i+4] =  b; 
1607                                                         dat8[i+5] = (b >> 8);
1608                                                         index++;
1609                                                         last_i = i+6;
1610                                                 }else
1611                                                         break; 
1612                                         }
1613                                         if(last_i < ssize){
1614                                                 for (i=0 ; i<ssize ; i+=6) {    // 16 bits per pixel 
1615                                                         int r = 0,g = 0,b = 0;
1616                                                         if(index < imgsize){
1617                                                                 r = image->comps[0].data[index];
1618                                                                 g = image->comps[1].data[index];
1619                                                                 b = image->comps[2].data[index];
1620                                                                 if (image->comps[0].sgnd){
1621                                                                         r += adjust;
1622                                                                         g += adjust;
1623                                                                         b += adjust;
1624                                                                 }
1625                                                                 dat8[i+0] =  r;//LSB
1626                                                                 if(i+1 <ssize) dat8[i+1] = (r >> 8);else break;//MSB     
1627                                                                 if(i+2 <ssize) dat8[i+2] =  g;          else break;
1628                                                                 if(i+3 <ssize) dat8[i+3] = (g >> 8);else break;
1629                                                                 if(i+4 <ssize) dat8[i+4] =  b;          else break;
1630                                                                 if(i+5 <ssize) dat8[i+5] = (b >> 8);else break;
1631                                                                 index++;
1632                                                         }else
1633                                                                 break; 
1634                                                 }                                               
1635                                         }
1636                                 }else{
1637                                         fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1638                                         fprintf(stderr,"Aborting\n");
1639                                         return 1;
1640                                 }
1641                                 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1642                         }
1643                         _TIFFfree(buf);
1644                         TIFFClose(tif);
1645                 }else if (image->numcomps == 1){
1646                         /* -->> -->> -->>    
1647                         Black and White     
1648                         <<-- <<-- <<-- */
1649
1650                         tif = TIFFOpen(outfile, "wb"); 
1651                         if (!tif) {
1652                                 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1653                                 return 1;
1654                         }
1655
1656                         width   = image->comps[0].w;
1657                         height  = image->comps[0].h;
1658                         imgsize = width * height;
1659                         bps             = image->comps[0].prec;
1660
1661                         /* Set tags */
1662                         TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
1663                         TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
1664                         TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1665                         TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1666                         TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1667                         TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1668                         TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1669                         TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
1670
1671                         /* Get a buffer for the data */
1672                         strip_size = TIFFStripSize(tif);
1673                         buf = _TIFFmalloc(strip_size);
1674                         index = 0;                      
1675                         for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1676                                 unsigned char *dat8;
1677                                 int i;
1678                                 dat8 = (unsigned char*)buf;
1679                                 if (image->comps[0].prec == 8){
1680                                         for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel 
1681                                                 if(index < imgsize){
1682                                                         int r = 0;
1683                                                         r = image->comps[0].data[index];
1684                                                         if (image->comps[0].sgnd){
1685                                                                 r  += adjust;
1686                                                         }
1687                                                         dat8[i+0] = r;
1688                                                         index++;
1689                                                 }else
1690                                                         break; 
1691                                         }
1692                                 }else if (image->comps[0].prec == 12){
1693                                         for (i = 0; i<TIFFStripSize(tif); i+=3) {       // 12 bits per pixel 
1694                                                 if(index < imgsize){
1695                                                         int r = 0, r1 = 0;
1696                                                         r  = image->comps[0].data[index];
1697                                                         r1 = image->comps[0].data[index+1];
1698                                                         if (image->comps[0].sgnd){
1699                                                                 r  += adjust;
1700                                                                 r1 += adjust;
1701                                                         }
1702                                                         dat8[i+0] = (r >> 4);
1703                                                         dat8[i+1] = ((r & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
1704                                                         dat8[i+2] = r1 ;
1705                                                         index+=2;
1706                                                 }else
1707                                                         break; 
1708                                         }
1709                                 }else if (image->comps[0].prec == 16){
1710                                         for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel 
1711                                                 if(index < imgsize){
1712                                                         int r = 0;
1713                                                         r = image->comps[0].data[index];
1714                                                         if (image->comps[0].sgnd){
1715                                                                 r  += adjust;
1716                                                         }
1717                                                         dat8[i+0] = r;
1718                                                         dat8[i+1] = r >> 8;
1719                                                         index++;
1720                                                 }else
1721                                                         break; 
1722                                         }
1723                                 }else{
1724                                         fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
1725                                         fprintf(stderr,"Aborting\n");
1726                                         return 1;
1727                                 }
1728                                 TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
1729                         }
1730                         _TIFFfree(buf);
1731                         TIFFClose(tif);
1732                 }else{
1733                         fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1734                         fprintf(stderr,"Aborting\n");
1735                         return 1;
1736                 }
1737                 return 0;
1738 }
1739
1740 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
1741 {
1742         int subsampling_dx = parameters->subsampling_dx;
1743         int subsampling_dy = parameters->subsampling_dy;
1744         TIFF *tif;
1745         tiff_infoheader_t Info;
1746         tdata_t buf;
1747         tstrip_t strip;
1748         tsize_t strip_size;
1749         int j, numcomps, w, h,index;
1750         OPJ_COLOR_SPACE color_space;
1751         opj_image_cmptparm_t cmptparm[3];
1752         opj_image_t * image = NULL;
1753         int imgsize = 0;
1754
1755         tif = TIFFOpen(filename, "r");
1756
1757         if (!tif) {
1758                 fprintf(stderr, "Failed to open %s for reading\n", filename);
1759                 return 0;
1760         }
1761
1762         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
1763         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
1764         TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
1765         TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
1766         TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
1767         Info.tiPhoto = 0;
1768         TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
1769         TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
1770         w= Info.tiWidth;
1771         h= Info.tiHeight;
1772         
1773         if (Info.tiPhoto == 2) { 
1774                 /* -->> -->> -->>    
1775                 RGB color           
1776                 <<-- <<-- <<-- */
1777
1778                 numcomps = 3;
1779                 color_space = CLRSPC_SRGB;
1780                 /* initialize image components*/ 
1781                 memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
1782                 for(j = 0; j < numcomps; j++) {
1783                         if (parameters->cp_cinema) {
1784                                 cmptparm[j].prec = 12;
1785                                 cmptparm[j].bpp = 12;
1786                         }else{
1787                                 cmptparm[j].prec = Info.tiBps;
1788                                 cmptparm[j].bpp = Info.tiBps;
1789                         }
1790                         cmptparm[j].sgnd = 0;
1791                         cmptparm[j].dx = subsampling_dx;
1792                         cmptparm[j].dy = subsampling_dy;
1793                         cmptparm[j].w = w;
1794                         cmptparm[j].h = h;
1795                 }
1796                 /* create the image*/ 
1797                 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1798                 if(!image) {
1799                         TIFFClose(tif);
1800                         return NULL;
1801                 }
1802
1803                 /* set image offset and reference grid */
1804                 image->x0 = parameters->image_offset_x0;
1805                 image->y0 = parameters->image_offset_y0;
1806                 image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1807                 image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1808
1809                 buf = _TIFFmalloc(TIFFStripSize(tif));
1810                 strip_size=0;
1811                 strip_size=TIFFStripSize(tif);
1812                 index = 0;
1813                 imgsize = image->comps[0].w * image->comps[0].h ;
1814                 /* Read the Image components*/
1815                 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1816                         unsigned char *dat8;
1817                         int i, ssize;
1818                         ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1819                         dat8 = (unsigned char*)buf;
1820
1821                         if (Info.tiBps==12){
1822                                 for (i=0; i<ssize; i+=9) {      /*12 bits per pixel*/
1823                                         if((index < imgsize)&(index+1 < imgsize)){
1824                                                 image->comps[0].data[index]   = ( dat8[i+0]<<4 )                |(dat8[i+1]>>4);
1825                                                 image->comps[1].data[index]   = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1826                                                 image->comps[2].data[index]   = ( dat8[i+3]<<4)                 |(dat8[i+4]>>4);
1827                                                 image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
1828                                                 image->comps[1].data[index+1] = ( dat8[i+6] <<4)                |(dat8[i+7]>>4);
1829                                                 image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
1830                                                 index+=2;
1831                                         }else
1832                                                 break;
1833                                 }
1834                         }
1835                         else if( Info.tiBps==16){
1836                                 for (i=0; i<ssize; i+=6) {      /* 16 bits per pixel */
1837                                         if(index < imgsize){
1838                                                 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R 
1839                                                 image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G 
1840                                                 image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B 
1841                                                 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1842                                                         image->comps[0].data[index] = (image->comps[0].data[index] + 0x08) >> 4 ;
1843                                                         image->comps[1].data[index] = (image->comps[1].data[index] + 0x08) >> 4 ;
1844                                                         image->comps[2].data[index] = (image->comps[2].data[index] + 0x08) >> 4 ;
1845                                                 }
1846                                                 index++;
1847                                         }else
1848                                                 break;
1849                                 }
1850                         }
1851                         else if ( Info.tiBps==8){
1852                                 for (i=0; i<ssize; i+=3) {      /* 8 bits per pixel */
1853                                         if(index < imgsize){
1854                                                 image->comps[0].data[index] = dat8[i+0];// R 
1855                                                 image->comps[1].data[index] = dat8[i+1];// G 
1856                                                 image->comps[2].data[index] = dat8[i+2];// B 
1857                                                 if(parameters->cp_cinema){/* Rounding to 12 bits*/
1858                                                         image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
1859                                                         image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
1860                                                         image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
1861                                                 }
1862                                                 index++;
1863                                         }else
1864                                                 break;
1865                                 }
1866                         }
1867                         else{
1868                                 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1869                                 fprintf(stderr,"Aborting\n");
1870                                 return NULL;
1871                         }
1872                 }
1873
1874                 _TIFFfree(buf);
1875                 TIFFClose(tif);
1876         }else if(Info.tiPhoto == 1) { 
1877                 /* -->> -->> -->>    
1878                 Black and White
1879                 <<-- <<-- <<-- */
1880
1881                 numcomps = 1;
1882                 color_space = CLRSPC_GRAY;
1883                 /* initialize image components*/ 
1884                 memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
1885                 cmptparm[0].prec = Info.tiBps;
1886                 cmptparm[0].bpp = Info.tiBps;
1887                 cmptparm[0].sgnd = 0;
1888                 cmptparm[0].dx = subsampling_dx;
1889                 cmptparm[0].dy = subsampling_dy;
1890                 cmptparm[0].w = w;
1891                 cmptparm[0].h = h;
1892
1893                 /* create the image*/ 
1894                 image = opj_image_create(numcomps, &cmptparm[0], color_space);
1895                 if(!image) {
1896                         TIFFClose(tif);
1897                         return NULL;
1898                 }
1899                 /* set image offset and reference grid */
1900                 image->x0 = parameters->image_offset_x0;
1901                 image->y0 = parameters->image_offset_y0;
1902                 image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
1903                 image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
1904
1905                 buf = _TIFFmalloc(TIFFStripSize(tif));
1906                 strip_size = 0;
1907                 strip_size = TIFFStripSize(tif);
1908                 index = 0;
1909                 imgsize = image->comps[0].w * image->comps[0].h ;
1910                 /* Read the Image components*/
1911                 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
1912                         unsigned char *dat8;
1913                         int i, ssize;
1914                         ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1915                         dat8 = (unsigned char*)buf;
1916
1917                         if (Info.tiBps==12){
1918                                 for (i=0; i<ssize; i+=3) {      /* 12 bits per pixel*/
1919                                         if(index < imgsize){
1920                                                 image->comps[0].data[index]   = ( dat8[i+0]<<4 )                |(dat8[i+1]>>4) ;
1921                                                 image->comps[0].data[index+1] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
1922                                                 index+=2;
1923                                         }else
1924                                                 break;
1925                                 }
1926                         }
1927                         else if( Info.tiBps==16){
1928                                 for (i=0; i<ssize; i+=2) {      /* 16 bits per pixel */
1929                                         if(index < imgsize){
1930                                                 image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
1931                                                 index++;
1932                                         }else
1933                                                 break;
1934                                 }
1935                         }
1936                         else if ( Info.tiBps==8){
1937                                 for (i=0; i<ssize; i+=1) {      /* 8 bits per pixel */
1938                                         if(index < imgsize){
1939                                                 image->comps[0].data[index] = dat8[i+0];
1940                                                 index++;
1941                                         }else
1942                                                 break;
1943                                 }
1944                         }
1945                         else{
1946                                 fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
1947                                 fprintf(stderr,"Aborting\n");
1948                                 return NULL;
1949                         }
1950                 }
1951
1952                 _TIFFfree(buf);
1953                 TIFFClose(tif);
1954         }else{
1955                 fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
1956                 fprintf(stderr,"Aborting\n");
1957                 return NULL;
1958         }
1959         return image;
1960 }
1961
1962 #endif /* HAVE_LIBTIFF */
1963
1964 /* -->> -->> -->> -->>
1965
1966         RAW IMAGE FORMAT
1967
1968  <<-- <<-- <<-- <<-- */
1969
1970 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
1971         int subsampling_dx = parameters->subsampling_dx;
1972         int subsampling_dy = parameters->subsampling_dy;
1973
1974         FILE *f = NULL;
1975         int i, compno, numcomps, w, h;
1976         OPJ_COLOR_SPACE color_space;
1977         opj_image_cmptparm_t *cmptparm; 
1978         opj_image_t * image = NULL;
1979         unsigned short ch;
1980         
1981         if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
1982         {
1983                 fprintf(stderr,"\nError: invalid raw image parameters\n");
1984                 fprintf(stderr,"Please use the Format option -F:\n");
1985                 fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
1986                 fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
1987                 fprintf(stderr,"Aborting\n");
1988                 return NULL;
1989         }
1990
1991         f = fopen(filename, "rb");
1992         if (!f) {
1993                 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1994                 fprintf(stderr,"Aborting\n");
1995                 return NULL;
1996         }
1997         numcomps = raw_cp->rawComp;
1998         color_space = CLRSPC_SRGB;
1999         w = raw_cp->rawWidth;
2000         h = raw_cp->rawHeight;
2001         cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
2002         
2003         /* initialize image components */       
2004         memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
2005         for(i = 0; i < numcomps; i++) {         
2006                 cmptparm[i].prec = raw_cp->rawBitDepth;
2007                 cmptparm[i].bpp = raw_cp->rawBitDepth;
2008                 cmptparm[i].sgnd = raw_cp->rawSigned;
2009                 cmptparm[i].dx = subsampling_dx;
2010                 cmptparm[i].dy = subsampling_dy;
2011                 cmptparm[i].w = w;
2012                 cmptparm[i].h = h;
2013         }
2014         /* create the image */
2015         image = opj_image_create(numcomps, &cmptparm[0], color_space);
2016         if(!image) {
2017                 fclose(f);
2018                 return NULL;
2019         }
2020         /* set image offset and reference grid */
2021         image->x0 = parameters->image_offset_x0;
2022         image->y0 = parameters->image_offset_y0;
2023         image->x1 = parameters->image_offset_x0 + (w - 1) *     subsampling_dx + 1;
2024         image->y1 = parameters->image_offset_y0 + (h - 1) *     subsampling_dy + 1;
2025
2026         if(raw_cp->rawBitDepth <= 8)
2027         {
2028                 unsigned char value = 0;
2029                 for(compno = 0; compno < numcomps; compno++) {
2030                         for (i = 0; i < w * h; i++) {
2031                                 if (!fread(&value, 1, 1, f)) {
2032                                         fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2033                                         return NULL;
2034                                 }
2035                                 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2036                         }
2037                 }
2038         }
2039         else if(raw_cp->rawBitDepth <= 16)
2040         {
2041                 unsigned short value;
2042                 for(compno = 0; compno < numcomps; compno++) {
2043                         for (i = 0; i < w * h; i++) {
2044                                 unsigned char temp;
2045                                 if (!fread(&temp, 1, 1, f)) {
2046                                         fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2047                                         return NULL;
2048                                 }
2049                                 value = temp << 8;
2050                                 if (!fread(&temp, 1, 1, f)) {
2051                                         fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2052                                         return NULL;
2053                                 }
2054                                 value += temp;
2055                                 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2056                         }
2057                 }
2058         }
2059         else {
2060                 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2061                 return NULL;
2062         }
2063
2064         if (fread(&ch, 1, 1, f)) {
2065                 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2066         }
2067         fclose(f);
2068
2069         return image;
2070 }
2071
2072 int imagetoraw(opj_image_t * image, const char *outfile)
2073 {
2074         FILE *rawFile = NULL;
2075         int compno;
2076         int w, h;
2077         int line, row;
2078         int *ptr;
2079
2080         if((image->numcomps * image->x1 * image->y1) == 0)
2081         {
2082                 fprintf(stderr,"\nError: invalid raw image parameters\n");
2083                 return 1;
2084         }
2085
2086         rawFile = fopen(outfile, "wb");
2087         if (!rawFile) {
2088                 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2089                 return 1;
2090         }
2091
2092         fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2093
2094         for(compno = 0; compno < image->numcomps; compno++)
2095         {
2096                 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2097                         image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2098
2099                 w = image->comps[compno].w;
2100                 h = image->comps[compno].h;
2101
2102                 if(image->comps[compno].prec <= 8)
2103                 {
2104                         if(image->comps[compno].sgnd == 1)
2105                         {
2106                                 signed char curr;
2107                                 int mask = (1 << image->comps[compno].prec) - 1;
2108                                 ptr = image->comps[compno].data;
2109                                 for (line = 0; line < h; line++) {
2110                                         for(row = 0; row < w; row++)    {                               
2111                                                 curr = (signed char) (*ptr & mask);
2112                                                 fwrite(&curr, sizeof(signed char), 1, rawFile);
2113                                                 ptr++;
2114                                         }
2115                                 }
2116                         }
2117                         else if(image->comps[compno].sgnd == 0)
2118                         {
2119                                 unsigned char curr;
2120                                 int mask = (1 << image->comps[compno].prec) - 1;
2121                                 ptr = image->comps[compno].data;
2122                                 for (line = 0; line < h; line++) {
2123                                         for(row = 0; row < w; row++)    {       
2124                                                 curr = (unsigned char) (*ptr & mask);
2125                                                 fwrite(&curr, sizeof(unsigned char), 1, rawFile);
2126                                                 ptr++;
2127                                         }
2128                                 }
2129                         }
2130                 }
2131                 else if(image->comps[compno].prec <= 16)
2132                 {
2133                         if(image->comps[compno].sgnd == 1)
2134                         {
2135                                 signed short int curr;
2136                                 int mask = (1 << image->comps[compno].prec) - 1;
2137                                 ptr = image->comps[compno].data;
2138                                 for (line = 0; line < h; line++) {
2139                                         for(row = 0; row < w; row++)    {                                       
2140                                                 unsigned char temp;
2141                                                 curr = (signed short int) (*ptr & mask);
2142                                                 temp = (unsigned char) (curr >> 8);
2143                                                 fwrite(&temp, 1, 1, rawFile);
2144                                                 temp = (unsigned char) curr;
2145                                                 fwrite(&temp, 1, 1, rawFile);
2146                                                 ptr++;
2147                                         }
2148                                 }
2149                         }
2150                         else if(image->comps[compno].sgnd == 0)
2151                         {
2152                                 unsigned short int curr;
2153                                 int mask = (1 << image->comps[compno].prec) - 1;
2154                                 ptr = image->comps[compno].data;
2155                                 for (line = 0; line < h; line++) {
2156                                         for(row = 0; row < w; row++)    {                               
2157                                                 unsigned char temp;
2158                                                 curr = (unsigned short int) (*ptr & mask);
2159                                                 temp = (unsigned char) (curr >> 8);
2160                                                 fwrite(&temp, 1, 1, rawFile);
2161                                                 temp = (unsigned char) curr;
2162                                                 fwrite(&temp, 1, 1, rawFile);
2163                                                 ptr++;
2164                                         }
2165                                 }
2166                         }
2167                 }
2168                 else if (image->comps[compno].prec <= 32)
2169                 {
2170                         fprintf(stderr,"More than 16 bits per component no handled yet\n");
2171                         return 1;
2172                 }
2173                 else
2174                 {
2175                         fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2176                         return 1;
2177                 }
2178         }
2179         fclose(rawFile);
2180         return 0;
2181 }
2182
2183 #ifdef HAVE_LIBPNG
2184
2185 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
2186 #define MAGIC_SIZE 8
2187 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
2188
2189 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
2190 #ifdef WIN32
2191 {
2192         printf("Error. PNG format is not yet handled under windows\n");
2193         return NULL;
2194 }
2195 #else
2196 {
2197         png_structp  png;
2198         png_infop    info;
2199         double gamma, display_exponent;
2200         int bit_depth, interlace_type,compression_type, filter_type;
2201         int unit;
2202         png_uint_32 resx, resy;
2203         unsigned int i, j;
2204         png_uint_32  width, height;
2205         int color_type, has_alpha, is16;
2206         unsigned char *s;
2207         FILE *reader;
2208         unsigned char **rows;
2209 /* j2k: */
2210         opj_image_t *image;
2211         opj_image_cmptparm_t cmptparm[4];
2212         int sub_dx, sub_dy;
2213         unsigned int nr_comp;
2214         int *r, *g, *b, *a;
2215         unsigned char sigbuf[8];
2216
2217         if((reader = fopen(read_idf, "rb")) == NULL)
2218    {
2219         fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
2220         return NULL;
2221    }
2222         image = NULL; png = NULL; rows = NULL;
2223
2224         if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
2225         || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
2226    {
2227         fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
2228         goto fin;
2229    }
2230 /* libpng-VERSION/example.c: 
2231  * PC : screen_gamma = 2.2;
2232  * Mac: screen_gamma = 1.7 or 1.0;
2233 */
2234         display_exponent = 2.2;
2235
2236         if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
2237                                     NULL, NULL, NULL)) == NULL)
2238           goto fin;
2239         if((info = png_create_info_struct(png)) == NULL)
2240           goto fin;
2241
2242         if(setjmp(png_jmpbuf(png)))
2243           goto fin;
2244
2245         png_init_io(png, reader);
2246         png_set_sig_bytes(png, MAGIC_SIZE);
2247
2248         png_read_info(png, info);
2249
2250         if(png_get_IHDR(png, info, &width, &height,
2251                 &bit_depth, &color_type, &interlace_type, 
2252                 &compression_type, &filter_type) == 0)
2253          goto fin;
2254
2255 /* png_set_expand():
2256  * expand paletted images to RGB, expand grayscale images of
2257  * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
2258  * to alpha channels.
2259 */
2260         if(color_type == PNG_COLOR_TYPE_PALETTE)
2261           png_set_expand(png);
2262         else
2263         if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2264           png_set_expand(png);
2265
2266         if(png_get_valid(png, info, PNG_INFO_tRNS))
2267           png_set_expand(png);
2268
2269         is16 = (bit_depth == 16);
2270
2271 /* GRAY => RGB; GRAY_ALPHA => RGBA
2272 */
2273         if(color_type == PNG_COLOR_TYPE_GRAY
2274         || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2275    {
2276         png_set_gray_to_rgb(png);
2277         color_type = 
2278          (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
2279                 PNG_COLOR_TYPE_RGB_ALPHA);
2280    }
2281         if( !png_get_gAMA(png, info, &gamma))
2282           gamma = 0.45455;
2283
2284         png_set_gamma(png, display_exponent, gamma);
2285
2286         png_read_update_info(png, info);
2287
2288         png_get_pHYs(png, info, &resx, &resy, &unit);
2289
2290         color_type = png_get_color_type(png, info);
2291
2292         has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
2293
2294         nr_comp = 3 + has_alpha;
2295
2296         bit_depth = png_get_bit_depth(png, info);
2297
2298         rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
2299         for(i = 0; i < height; ++i)
2300          rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
2301
2302         png_read_image(png, rows);
2303
2304         memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
2305
2306         sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
2307
2308         for(i = 0; i < nr_comp; ++i)
2309    {
2310         cmptparm[i].prec = bit_depth;
2311 /* bits_per_pixel: 8 or 16 */
2312         cmptparm[i].bpp = bit_depth;
2313         cmptparm[i].sgnd = 0;
2314         cmptparm[i].dx = sub_dx;
2315         cmptparm[i].dy = sub_dy;
2316         cmptparm[i].w = width;
2317         cmptparm[i].h = height;
2318    }
2319
2320         image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
2321
2322         if(image == NULL) goto fin;
2323
2324     image->x0 = params->image_offset_x0;
2325     image->y0 = params->image_offset_y0;
2326     image->x1 = image->x0 + (width  - 1) * sub_dx + 1 + image->x0;
2327     image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0;
2328
2329         r = image->comps[0].data;
2330         g = image->comps[1].data;
2331         b = image->comps[2].data;
2332         a = image->comps[3].data;
2333
2334         for(i = 0; i < height; ++i)
2335    {
2336         s = rows[i];
2337
2338         for(j = 0; j < width; ++j)
2339   {
2340         if(is16)
2341  {
2342         *r++ = s[0]<<8|s[1]; s += 2;
2343
2344         *g++ = s[0]<<8|s[1]; s += 2;
2345         
2346         *b++ = s[0]<<8|s[1]; s += 2;
2347         
2348         if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
2349
2350         continue;
2351  }
2352         *r++ = *s++; *g++ = *s++; *b++ = *s++;
2353
2354         if(has_alpha) *a++ = *s++;
2355   }
2356    }
2357 fin:
2358         if(rows)
2359    {
2360         for(i = 0; i < height; ++i)
2361          free(rows[i]);
2362         free(rows);
2363    }
2364         if(png)
2365           png_destroy_read_struct(&png, &info, NULL);
2366
2367         fclose(reader);
2368
2369         return image;
2370
2371 }/* pngtoimage() */
2372 #endif /* WIN32 */
2373
2374 int imagetopng(opj_image_t * image, const char *write_idf)
2375 #ifdef WIN32
2376 {
2377         printf("Error. PNG format is not yet handled under windows\n");
2378         return -1;
2379 }
2380 #else
2381 {
2382         FILE *writer;
2383         png_structp png;
2384         png_infop info;
2385         int *red, *green, *blue, *alpha;
2386         unsigned char *row_buf, *d;
2387         int has_alpha, width, height, nr_comp, color_type;
2388         int adjust, x, y, fails, is16;
2389         int opj_prec, prec;
2390         unsigned short mask;
2391         png_color_8 sig_bit;
2392
2393         prec = opj_prec = image->comps[0].prec;
2394
2395         if(prec > 8) prec = 16;
2396
2397         if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
2398   {
2399         fprintf(stderr,"imagetopng: can not create %s"
2400          "\n\twrong bit_depth %d\n", write_idf, prec);
2401         return 1;
2402   }
2403         writer = fopen(write_idf, "wb");
2404
2405         if(writer == NULL) return 1;
2406
2407         info = NULL; fails = 1; has_alpha = 0;
2408
2409 /* Create and initialize the png_struct with the desired error handler
2410  * functions.  If you want to use the default stderr and longjump method,
2411  * you can supply NULL for the last three parameters.  We also check that
2412  * the library version is compatible with the one used at compile time,
2413  * in case we are using dynamically linked libraries.  REQUIRED.
2414 */
2415         png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
2416                 NULL, NULL, NULL);
2417 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
2418
2419         if(png == NULL) goto fin;
2420
2421 /* Allocate/initialize the image information data.  REQUIRED 
2422 */
2423         info = png_create_info_struct(png);
2424
2425         if(info == NULL) goto fin;
2426
2427 /* Set error handling.  REQUIRED if you are not supplying your own
2428  * error handling functions in the png_create_write_struct() call.
2429 */
2430         if(setjmp(png_jmpbuf(png))) goto fin;
2431
2432 /* I/O initialization functions is REQUIRED 
2433 */
2434         png_init_io(png, writer);
2435
2436 /* Set the image information here.  Width and height are up to 2^31,
2437  * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
2438  * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
2439  * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
2440  * or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
2441  * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
2442  * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. 
2443  * REQUIRED
2444 */
2445         png_set_compression_level(png, Z_BEST_COMPRESSION);
2446
2447         adjust = 0;
2448         if(image->comps[0].sgnd) adjust = 1 << (opj_prec - 1);
2449
2450         if(prec == 8) mask = 0x00ff;
2451         else
2452         if(prec == 4) mask = 0x000f;
2453         else
2454         if(prec == 2) mask = 0x0003;
2455         else
2456         if(prec == 1) mask = 0x0001;
2457         else
2458         if(opj_prec == 12) mask = 0x0fff;
2459         else
2460         mask = 0xffff;
2461
2462         nr_comp = image->numcomps;
2463
2464         if(nr_comp >= 3
2465     && image->comps[0].dx == image->comps[1].dx
2466     && image->comps[1].dx == image->comps[2].dx
2467     && image->comps[0].dy == image->comps[1].dy
2468     && image->comps[1].dy == image->comps[2].dy
2469     && image->comps[0].prec == image->comps[1].prec
2470     && image->comps[1].prec == image->comps[2].prec)
2471    {
2472     has_alpha = (nr_comp > 3); 
2473
2474         is16 = (prec == 16);
2475         
2476     width = image->comps[0].w;
2477     height = image->comps[0].h;
2478
2479         red = image->comps[0].data;
2480         green = image->comps[1].data;
2481         blue = image->comps[2].data;
2482
2483     sig_bit.red = sig_bit.green = sig_bit.blue = prec;
2484
2485         if(has_alpha) 
2486   {
2487         sig_bit.alpha = prec;
2488         alpha = image->comps[3].data; 
2489         color_type = PNG_COLOR_TYPE_RGB_ALPHA;
2490   }
2491         else 
2492   {
2493         sig_bit.alpha = 0; alpha = NULL;
2494         color_type = PNG_COLOR_TYPE_RGB;
2495   }
2496         png_set_sBIT(png, info, &sig_bit);
2497
2498         png_set_IHDR(png, info, width, height, prec, 
2499          color_type,
2500          PNG_INTERLACE_NONE,
2501          PNG_COMPRESSION_TYPE_BASE,  PNG_FILTER_TYPE_BASE);
2502
2503 /*=============================*/
2504         png_write_info(png, info);
2505 /*=============================*/
2506
2507 /* Shift the pixels up to a legal bit depth and fill in
2508  * as appropriate to correctly scale the image.
2509 */
2510         if(opj_prec > 8 && opj_prec < 16)
2511   {
2512         png_set_shift(png, &sig_bit);
2513   }
2514         if(opj_prec < 8)
2515   {
2516         png_set_packing(png);
2517   }
2518
2519         row_buf = (unsigned char*)malloc(width * nr_comp * 2);
2520
2521         for(y = 0; y < height; ++y)
2522   {
2523         d = row_buf;
2524
2525         for(x = 0; x < width; ++x)
2526  {
2527                 if(is16)
2528            {
2529 /* Network byte order */
2530                 unsigned short v;
2531
2532                 v = (unsigned short)((*red + adjust) & mask); ++red;
2533             *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2534                 v = (unsigned short)((*green + adjust) & mask); ++green;
2535             *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2536                 v = (unsigned short)((*blue + adjust) & mask); ++blue;
2537             *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2538
2539                 if(has_alpha)
2540           {
2541                 v = (unsigned short)((*alpha + adjust) & mask); ++alpha;
2542                 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
2543           }
2544                 continue;
2545            }
2546                 *d++ = (unsigned char)((*red + adjust) & mask); ++red;
2547                 *d++ = (unsigned char)((*green + adjust) & mask); ++green;
2548                 *d++ = (unsigned char)((*blue + adjust) & mask); ++blue;
2549
2550                 if(has_alpha)
2551            {
2552                 *d++ = (unsigned char)((*alpha + adjust) & mask); ++alpha;
2553            }
2554  }      /* for(x) */
2555
2556         png_write_row(png, row_buf);
2557
2558   }     /* for(y) */
2559         free(row_buf);
2560
2561    }/* nr_comp >= 3 */
2562         else
2563         if(nr_comp == 1 /* GRAY */
2564         || (   nr_comp == 2 /* GRAY_ALPHA */
2565                 && image->comps[0].dx == image->comps[1].dx
2566                 && image->comps[0].dy == image->comps[1].dy
2567                 && image->comps[0].prec == image->comps[1].prec))
2568    {
2569         red = image->comps[0].data;
2570
2571     sig_bit.gray = prec;
2572     sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
2573         alpha = NULL;
2574         color_type = PNG_COLOR_TYPE_GRAY;
2575
2576     if(nr_comp == 2) 
2577   { 
2578         has_alpha = 1; sig_bit.alpha = prec;
2579         alpha = image->comps[1].data;
2580         color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
2581   }
2582     width = image->comps[0].w;
2583     height = image->comps[0].h;
2584
2585         png_set_IHDR(png, info, width, height, sig_bit.gray,
2586      color_type,
2587      PNG_INTERLACE_NONE,
2588      PNG_COMPRESSION_TYPE_BASE,  PNG_FILTER_TYPE_BASE);
2589
2590         png_set_sBIT(png, info, &sig_bit);
2591 #ifdef TEST_WITH_GAMMA
2592     png_set_gAMA(png, info, 1.0);
2593 #endif
2594 /*=============================*/
2595         png_write_info(png, info);
2596 /*=============================*/
2597
2598 /* Shift the pixels up to a legal bit depth and fill in
2599  * as appropriate to correctly scale the image.
2600 */
2601         if(opj_prec > 8 && opj_prec < 16)
2602   {
2603         png_set_shift(png, &sig_bit);
2604   }
2605         if(opj_prec < 8)
2606   {
2607         png_set_packing(png);
2608   }
2609
2610         if(prec > 8)
2611   {
2612 /* Network byte order */
2613         unsigned short v;
2614
2615         row_buf = (unsigned char*)
2616          malloc(width * nr_comp * sizeof(unsigned short));
2617
2618         if(opj_prec == 12) mask = 0x0fff;
2619
2620         for(y = 0; y < height; ++y)
2621  {
2622         d = row_buf;
2623
2624                 for(x = 0; x < width; ++x)
2625            {
2626                 v = (unsigned short)((*red + adjust) & mask); ++red;
2627                 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)(v & 0xff);
2628
2629                 if(has_alpha)
2630           {
2631                 v = (unsigned short)((*alpha + adjust) & mask); ++alpha;
2632                 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)(v & 0xff);
2633           }
2634            }/* for(x) */
2635         png_write_row(png, row_buf);
2636
2637  }      /* for(y) */
2638         free(row_buf);
2639   }
2640         else /* prec <= 8 */
2641   {
2642         row_buf = (unsigned char*)calloc(width, nr_comp * 2);
2643
2644         for(y = 0; y < height; ++y)
2645  {
2646         d = row_buf;
2647
2648                 for(x = 0; x < width; ++x)
2649            {
2650                 *d++ = (unsigned char)((*red + adjust) & mask); ++red;
2651
2652                 if(has_alpha)
2653           {
2654                 *d++ = (unsigned char)((*alpha + adjust) & mask); ++alpha;
2655           }
2656            }/* for(x) */
2657
2658         png_write_row(png, row_buf);
2659
2660  }      /* for(y) */
2661         free(row_buf);
2662   }
2663    }
2664         else
2665    {
2666         fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
2667         goto fin;
2668    }
2669         png_write_end(png, info);
2670
2671         fails = 0;
2672
2673 fin:
2674
2675         if(png)
2676    {
2677     png_destroy_write_struct(&png, &info);
2678    }
2679         fclose(writer);
2680
2681         return fails;
2682 }/* imagetopng() */
2683 #endif /* WIN32 */
2684 #endif /* HAVE_LIBPNG */
2685