]> Creatis software - gdcm.git/blob - src/gdcmopenjpeg/codec/convert.c
ENH: Backport from openjpeg CVS
[gdcm.git] / src / gdcmopenjpeg / codec / convert.c
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5  * Copyright (c) 2005, HervĂ© Drolon, FreeImage Team
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "openjpeg.h"
34
35 /*
36  * Get logarithm of an integer and round downwards.
37  *
38  * log2(a)
39  */
40 static int int_floorlog2(int a) {
41   int l;
42   for (l = 0; a > 1; l++) {
43     a >>= 1;
44   }
45   return l;
46 }
47
48 /*
49  * Divide an integer by a power of 2 and round upwards.
50  *
51  * a divided by 2^b
52  */
53 static int int_ceildivpow2(int a, int b) {
54   return (a + (1 << b) - 1) >> b;
55 }
56
57 /*
58  * Divide an integer and round upwards.
59  *
60  * a divided by b
61  */
62 static int int_ceildiv(int a, int b) {
63   return (a + b - 1) / b;
64 }
65
66 /* -->> -->> -->> -->>
67
68   BMP IMAGE FORMAT
69
70  <<-- <<-- <<-- <<-- */
71
72 /* WORD defines a two byte word */
73 typedef unsigned short int WORD;
74
75 /* DWORD defines a four byte word */
76 typedef unsigned long int DWORD;
77
78 typedef struct {
79   WORD bfType;      /* 'BM' for Bitmap (19776) */
80   DWORD bfSize;      /* Size of the file        */
81   WORD bfReserved1;    /* Reserved : 0            */
82   WORD bfReserved2;    /* Reserved : 0            */
83   DWORD bfOffBits;    /* Offset                  */
84 } BITMAPFILEHEADER_t;
85
86 typedef struct {
87   DWORD biSize;      /* Size of the structure in bytes */
88   DWORD biWidth;    /* Width of the image in pixels */
89   DWORD biHeight;    /* Heigth of the image in pixels */
90   WORD biPlanes;    /* 1 */
91   WORD biBitCount;    /* Number of color bits by pixels */
92   DWORD biCompression;    /* Type of encoding 0: none 1: RLE8 2: RLE4 */
93   DWORD biSizeImage;    /* Size of the image in bytes */
94   DWORD biXpelsPerMeter;  /* Horizontal (X) resolution in pixels/meter */
95   DWORD biYpelsPerMeter;  /* Vertical (Y) resolution in pixels/meter */
96   DWORD biClrUsed;    /* Number of color used in the image (0: ALL) */
97   DWORD biClrImportant;    /* Number of important color (0: ALL) */
98 } BITMAPINFOHEADER_t;
99
100 opj_image_t* bmptoimage(char *filename, opj_cparameters_t *parameters) {
101   int subsampling_dx = parameters->subsampling_dx;
102   int subsampling_dy = parameters->subsampling_dy;
103
104   int i, numcomps, w, h;
105   OPJ_COLOR_SPACE color_space;
106   opj_image_cmptparm_t cmptparm[3];  /* maximum of 3 components */
107   opj_image_t * image = NULL;
108
109   FILE *IN;
110   BITMAPFILEHEADER_t File_h;
111   BITMAPINFOHEADER_t Info_h;
112   unsigned char *RGB;
113   unsigned char *table_R, *table_G, *table_B;
114   unsigned int j, PAD = 0;
115
116   int x, y, index;
117   int gray_scale = 1, not_end_file = 1; 
118
119   unsigned int line = 0, col = 0;
120   unsigned char v, v2;
121   DWORD W, H;
122   
123   IN = fopen(filename, "rb");
124   if (!IN) {
125     fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
126     return 0;
127   }
128   
129   File_h.bfType = getc(IN);
130   File_h.bfType = (getc(IN) << 8) + File_h.bfType;
131   
132   if (File_h.bfType != 19778) {
133     fprintf(stderr,"Error, not a BMP file!\n");
134     return 0;
135   } else {
136     /* FILE HEADER */
137     /* ------------- */
138     File_h.bfSize = getc(IN);
139     File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
140     File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
141     File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
142
143     File_h.bfReserved1 = getc(IN);
144     File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
145
146     File_h.bfReserved2 = getc(IN);
147     File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
148
149     File_h.bfOffBits = getc(IN);
150     File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
151     File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
152     File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
153
154     /* INFO HEADER */
155     /* ------------- */
156
157     Info_h.biSize = getc(IN);
158     Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
159     Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
160     Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
161
162     Info_h.biWidth = getc(IN);
163     Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
164     Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
165     Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
166     w = Info_h.biWidth;
167
168     Info_h.biHeight = getc(IN);
169     Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
170     Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
171     Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
172     h = Info_h.biHeight;
173
174     Info_h.biPlanes = getc(IN);
175     Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
176
177     Info_h.biBitCount = getc(IN);
178     Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
179
180     Info_h.biCompression = getc(IN);
181     Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
182     Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
183     Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
184
185     Info_h.biSizeImage = getc(IN);
186     Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
187     Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
188     Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
189
190     Info_h.biXpelsPerMeter = getc(IN);
191     Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
192     Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
193     Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
194
195     Info_h.biYpelsPerMeter = getc(IN);
196     Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
197     Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
198     Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
199
200     Info_h.biClrUsed = getc(IN);
201     Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
202     Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
203     Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
204
205     Info_h.biClrImportant = getc(IN);
206     Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
207     Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
208     Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
209
210     /* Read the data and store them in the OUT file */
211     
212     if (Info_h.biBitCount == 24) {
213       numcomps = 3;
214       color_space = CLRSPC_SRGB;
215       /* initialize image components */
216       memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
217       for(i = 0; i < numcomps; i++) {
218         cmptparm[i].prec = 8;
219         cmptparm[i].bpp = 8;
220         cmptparm[i].sgnd = 0;
221         cmptparm[i].dx = subsampling_dx;
222         cmptparm[i].dy = subsampling_dy;
223         cmptparm[i].w = w;
224         cmptparm[i].h = h;
225       }
226       /* create the image */
227       image = opj_image_create(numcomps, &cmptparm[0], color_space);
228       if(!image) {
229         fclose(IN);
230         return NULL;
231       }
232
233       /* set image offset and reference grid */
234       image->x0 = parameters->image_offset_x0;
235       image->y0 = parameters->image_offset_y0;
236       image->x1 =  !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
237       image->y1 =  !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
238
239       /* set image data */
240
241       /* Place the cursor at the beginning of the image information */
242       fseek(IN, 0, SEEK_SET);
243       fseek(IN, File_h.bfOffBits, SEEK_SET);
244       
245       W = Info_h.biWidth;
246       H = Info_h.biHeight;
247
248       /* PAD = 4 - (3 * W) % 4; */
249       /* PAD = (PAD == 4) ? 0 : PAD; */
250       PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
251       
252       RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
253       
254       fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
255       
256       index = 0;
257
258       for(y = 0; y < (int)H; y++) {
259         unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
260         for(x = 0; x < (int)W; x++) {
261           unsigned char *pixel = &scanline[3 * x];
262           image->comps[0].data[index] = pixel[2];  /* R */
263           image->comps[1].data[index] = pixel[1];  /* G */
264           image->comps[2].data[index] = pixel[0];  /* B */
265           index++;
266         }
267       }
268
269       free(RGB);
270
271     } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
272       table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
273       table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
274       table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
275       
276       for (j = 0; j < Info_h.biClrUsed; j++) {
277         table_B[j] = getc(IN);
278         table_G[j] = getc(IN);
279         table_R[j] = getc(IN);
280         getc(IN);
281         if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
282           gray_scale = 0;
283       }
284       
285       /* Place the cursor at the beginning of the image information */
286       fseek(IN, 0, SEEK_SET);
287       fseek(IN, File_h.bfOffBits, SEEK_SET);
288       
289       W = Info_h.biWidth;
290       H = Info_h.biHeight;
291       if (Info_h.biWidth % 2)
292         W++;
293       
294       numcomps = gray_scale ? 1 : 3;
295       color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
296       /* initialize image components */
297       memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
298       for(i = 0; i < numcomps; i++) {
299         cmptparm[i].prec = 8;
300         cmptparm[i].bpp = 8;
301         cmptparm[i].sgnd = 0;
302         cmptparm[i].dx = subsampling_dx;
303         cmptparm[i].dy = subsampling_dy;
304         cmptparm[i].w = w;
305         cmptparm[i].h = h;
306       }
307       /* create the image */
308       image = opj_image_create(numcomps, &cmptparm[0], color_space);
309       if(!image) {
310         fclose(IN);
311         return NULL;
312       }
313
314       /* set image offset and reference grid */
315       image->x0 = parameters->image_offset_x0;
316       image->y0 = parameters->image_offset_y0;
317       image->x1 =  !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
318       image->y1 =  !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
319
320       /* set image data */
321
322       RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
323       
324       fread(RGB, sizeof(unsigned char), W * H, IN);
325       if (gray_scale) {
326         index = 0;
327         for (j = 0; j < W * H; j++) {
328           if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
329             image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
330             index++;
331           }
332         }
333
334       } else {    
335         index = 0;
336         for (j = 0; j < W * H; j++) {
337           if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
338             unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
339             image->comps[0].data[index] = table_R[pixel_index];
340             image->comps[1].data[index] = table_G[pixel_index];
341             image->comps[2].data[index] = table_B[pixel_index];
342             index++;
343           }
344         }
345       }
346       free(RGB);
347             
348     } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {        
349       table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
350       table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
351       table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
352       
353       for (j = 0; j < Info_h.biClrUsed; j++) {
354         table_B[j] = getc(IN);
355         table_G[j] = getc(IN);
356         table_R[j] = getc(IN);
357         getc(IN);
358         if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])
359           gray_scale = 0;
360       }
361
362       numcomps = gray_scale ? 1 : 3;
363       color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
364       /* initialize image components */
365       memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
366       for(i = 0; i < numcomps; i++) {
367         cmptparm[i].prec = 8;
368         cmptparm[i].bpp = 8;
369         cmptparm[i].sgnd = 0;
370         cmptparm[i].dx = subsampling_dx;
371         cmptparm[i].dy = subsampling_dy;
372         cmptparm[i].w = w;
373         cmptparm[i].h = h;
374       }
375       /* create the image */
376       image = opj_image_create(numcomps, &cmptparm[0], color_space);
377       if(!image) {
378         fclose(IN);
379         return NULL;
380       }
381
382       /* set image offset and reference grid */
383       image->x0 = parameters->image_offset_x0;
384       image->y0 = parameters->image_offset_y0;
385       image->x1 =  !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
386       image->y1 =  !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
387
388       /* set image data */
389       
390       /* Place the cursor at the beginning of the image information */
391       fseek(IN, 0, SEEK_SET);
392       fseek(IN, File_h.bfOffBits, SEEK_SET);
393       
394       RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
395             
396       while (not_end_file) {
397         v = getc(IN);
398         if (v) {
399           v2 = getc(IN);
400           for (i = 0; i < (int) v; i++) {
401             RGB[line * Info_h.biWidth + col] = v2;
402             col++;
403           }
404         } else {
405           v = getc(IN);
406           switch (v) {
407             case 0:
408               col = 0;
409               line++;
410               break;
411             case 1:
412               line++;
413               not_end_file = 0;
414               break;
415             case 2:
416               fprintf(stderr,"No Delta supported\n");
417               opj_image_destroy(image);
418               fclose(IN);
419               return NULL;
420               break;
421             default:
422               for (i = 0; i < v; i++) {
423                 v2 = getc(IN);
424                 RGB[line * Info_h.biWidth + col] = v2;
425                 col++;
426               }
427               if (v % 2)
428                 v2 = getc(IN);
429               break;
430           }
431         }
432       }
433       if (gray_scale) {
434         index = 0;
435         for (line = 0; line < Info_h.biHeight; line++) {
436           for (col = 0; col < Info_h.biWidth; col++) {
437             image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
438             index++;
439           }
440         }
441       } else {
442         index = 0;
443         for (line = 0; line < Info_h.biHeight; line++) {
444           for (col = 0; col < Info_h.biWidth; col++) {
445             unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
446             image->comps[0].data[index] = table_R[pixel_index];
447             image->comps[1].data[index] = table_G[pixel_index];
448             image->comps[2].data[index] = table_B[pixel_index];
449             index++;
450           }
451         }
452       }
453       free(RGB);
454   } else {
455     fprintf(stderr, 
456       "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
457   }
458   fclose(IN);
459  }
460  
461  return image;
462 }
463
464 int imagetobmp(opj_image_t * image, char *outfile) {
465   int w, wr, h, hr;
466   int i, pad;
467   FILE *fdest = NULL;
468
469   if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
470     && image->comps[1].dx == image->comps[2].dx
471     && image->comps[0].dy == image->comps[1].dy
472     && image->comps[1].dy == image->comps[2].dy
473     && image->comps[0].prec == image->comps[1].prec
474     && image->comps[1].prec == image->comps[2].prec) {
475     
476     /* -->> -->> -->> -->>    
477     24 bits color      
478     <<-- <<-- <<-- <<-- */
479       
480     fdest = fopen(outfile, "wb");
481     if (!fdest) {
482       fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
483       return 1;
484     }
485       
486     /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
487     /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
488     w = image->comps[0].w;
489     wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
490       
491     /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
492     /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
493     h = image->comps[0].h;
494     hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
495       
496     fprintf(fdest, "BM");
497       
498     /* FILE HEADER */
499     /* ------------- */
500     fprintf(fdest, "%c%c%c%c",
501       (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
502       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)  >> 8) & 0xff,
503       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)  >> 16) & 0xff,
504       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)  >> 24) & 0xff);
505     fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
506     fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
507       
508     /* INFO HEADER   */
509     /* ------------- */
510     fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,  ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
511     fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
512       (unsigned char) ((wr) >> 8) & 0xff,
513       (unsigned char) ((wr) >> 16) & 0xff,
514       (unsigned char) ((wr) >> 24) & 0xff);
515     fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
516       (unsigned char) ((hr) >> 8) & 0xff,
517       (unsigned char) ((hr) >> 16) & 0xff,
518       (unsigned char) ((hr) >> 24) & 0xff);
519     fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
520     fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
521     fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
522     fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
523       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
524       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
525       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
526     fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
527     fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,  ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
528     fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
529     fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
530       
531     for (i = 0; i < wr * hr; i++) {
532       unsigned char R, G, B;
533       /* a modifier */
534       /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
535       R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
536       /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
537       G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
538       /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
539       B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
540       fprintf(fdest, "%c%c%c", B, G, R);
541       
542       if ((i + 1) % wr == 0) {
543         for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--)  /* ADD */
544           fprintf(fdest, "%c", 0);
545       }
546     }
547     fclose(fdest);
548   } else {      /* Gray-scale */
549
550     /* -->> -->> -->> -->>
551     8 bits non code (Gray scale)
552     <<-- <<-- <<-- <<-- */
553
554     fdest = fopen(outfile, "wb");
555     /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
556     /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
557     w = image->comps[0].w;
558     wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
559       
560     /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
561     /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
562     h = image->comps[0].h;
563     hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
564       
565     fprintf(fdest, "BM");
566       
567     /* FILE HEADER */
568     /* ------------- */
569     fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
570       (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
571       (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
572       (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
573     fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
574     fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, 
575       ((54 + 1024) >> 16) & 0xff,
576       ((54 + 1024) >> 24) & 0xff);
577       
578     /* INFO HEADER */
579     /* ------------- */
580     fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,  ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
581     fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
582       (unsigned char) ((wr) >> 8) & 0xff,
583       (unsigned char) ((wr) >> 16) & 0xff,
584       (unsigned char) ((wr) >> 24) & 0xff);
585     fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
586       (unsigned char) ((hr) >> 8) & 0xff,
587       (unsigned char) ((hr) >> 16) & 0xff,
588       (unsigned char) ((hr) >> 24) & 0xff);
589     fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
590     fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
591     fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
592     fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
593       (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) &  0xff,
594       (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) &  0xff,
595       (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
596     fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,  ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
597     fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,  ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
598     fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
599     fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
600   }
601
602   for (i = 0; i < 256; i++) {
603     fprintf(fdest, "%c%c%c%c", i, i, i, 0);
604   }
605
606   for (i = 0; i < wr * hr; i++) {
607     /* a modifier !! */
608     /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
609     fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
610     /*if (((i + 1) % w == 0 && w % 2))
611     fprintf(fdest, "%c", 0); */
612     if ((i + 1) % wr == 0) {
613       for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--)  /* ADD */
614         fprintf(fdest, "%c", 0);
615     }
616   }
617   fclose(fdest);
618
619   return 0;
620 }
621
622 /* -->> -->> -->> -->>
623
624 PGX IMAGE FORMAT
625
626 <<-- <<-- <<-- <<-- */
627
628
629 unsigned char readuchar(FILE * f)
630 {
631   unsigned char c1;
632   fread(&c1, 1, 1, f);
633   return c1;
634 }
635
636 unsigned short readushort(FILE * f, int bigendian)
637 {
638   unsigned char c1, c2;
639   fread(&c1, 1, 1, f);
640   fread(&c2, 1, 1, f);
641   if (bigendian)
642     return (c1 << 8) + c2;
643   else
644     return (c2 << 8) + c1;
645 }
646
647 unsigned int readuint(FILE * f, int bigendian)
648 {
649   unsigned char c1, c2, c3, c4;
650   fread(&c1, 1, 1, f);
651   fread(&c2, 1, 1, f);
652   fread(&c3, 1, 1, f);
653   fread(&c4, 1, 1, f);
654   if (bigendian)
655     return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
656   else
657     return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
658 }
659
660 opj_image_t* pgxtoimage(char *filename, opj_cparameters_t *parameters) {
661   FILE *f = NULL;
662   int w, h, prec;
663   int i, numcomps, max;
664   OPJ_COLOR_SPACE color_space;
665   opj_image_cmptparm_t cmptparm;  /* maximum of 1 component  */
666   opj_image_t * image = NULL;
667
668   char endian1,endian2,sign;
669   char signtmp[32];
670
671   char temp[32];
672   int bigendian;
673   opj_image_comp_t *comp = NULL;
674
675   numcomps = 1;
676   color_space = CLRSPC_GRAY;
677
678   memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
679
680   max = 0;
681
682   f = fopen(filename, "rb");
683   if (!f) {
684     fprintf(stderr, "Failed to open %s for reading !\n", filename);
685     return NULL;
686   }
687
688   fseek(f, 0, SEEK_SET);
689   fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
690   
691   i=0;
692   sign='+';    
693   while (signtmp[i]!='\0') {
694     if (signtmp[i]=='-') sign='-';
695     i++;
696   }
697   
698   fgetc(f);
699   if (endian1=='M' && endian2=='L') {
700     bigendian = 1;
701   } else if (endian2=='M' && endian1=='L') {
702     bigendian = 0;
703   } else {
704     fprintf(stderr, "Bad pgx header, please check input file\n");
705     return NULL;
706   }
707
708   /* initialize image component */
709
710   cmptparm.x0 = parameters->image_offset_x0;
711   cmptparm.y0 = parameters->image_offset_y0;
712   cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
713   cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
714   
715   if (sign == '-') {
716     cmptparm.sgnd = 1;
717   } else {
718     cmptparm.sgnd = 0;
719   }
720   cmptparm.prec = prec;
721   cmptparm.bpp = prec;
722   cmptparm.dx = parameters->subsampling_dx;
723   cmptparm.dy = parameters->subsampling_dy;
724   
725   /* create the image */
726   image = opj_image_create(numcomps, &cmptparm, color_space);
727   if(!image) {
728     fclose(f);
729     return NULL;
730   }
731   /* set image offset and reference grid */
732   image->x0 = cmptparm.x0;
733   image->y0 = cmptparm.x0;
734   image->x1 = cmptparm.w;
735   image->y1 = cmptparm.h;
736
737   /* set image data */
738
739   comp = &image->comps[0];
740
741   for (i = 0; i < w * h; i++) {
742     int v;
743     if (comp->prec <= 8) {
744       if (!comp->sgnd) {
745         v = readuchar(f);
746       } else {
747         v = (char) readuchar(f);
748       }
749     } else if (comp->prec <= 16) {
750       if (!comp->sgnd) {
751         v = readushort(f, bigendian);
752       } else {
753         v = (short) readushort(f, bigendian);
754       }
755     } else {
756       if (!comp->sgnd) {
757         v = readuint(f, bigendian);
758       } else {
759         v = (int) readuint(f, bigendian);
760       }
761     }
762     if (v > max)
763       max = v;
764     comp->data[i] = v;
765   }
766   fclose(f);
767   comp->bpp = int_floorlog2(max) + 1;
768
769   return image;
770 }
771
772 int imagetopgx(opj_image_t * image, char *outfile) {
773   int w, wr, h, hr;
774   int i, j, compno;
775   FILE *fdest = NULL;
776
777   for (compno = 0; compno < image->numcomps; compno++) {
778     opj_image_comp_t *comp = &image->comps[compno];
779     char name[256];
780     int nbytes = 0;
781     char *tmp = outfile;
782     while (*tmp) {
783       tmp++;
784     }
785     while (*tmp!='.') {
786       tmp--;
787     }
788     *tmp='\0';
789
790     if (image->numcomps > 1) {
791       sprintf(name, "%s-%d.pgx", outfile, compno);
792     } else {
793       sprintf(name, "%s.pgx", outfile);
794     }
795     fdest = fopen(name, "wb");
796     if (!fdest) {
797       fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
798       return 1;
799     }
800     /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */
801     /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */
802     w = image->comps[compno].w;
803     wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
804       
805     /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */
806     /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */
807     h = image->comps[compno].h;
808     hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
809       
810     fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
811     if (comp->prec <= 8) {
812       nbytes = 1;
813     } else if (comp->prec <= 16) {
814       nbytes = 2;
815     } else {
816       nbytes = 4;
817     }
818     for (i = 0; i < wr * hr; i++) {
819       int v = image->comps[compno].data[i / wr * w + i % wr];
820       for (j = nbytes - 1; j >= 0; j--) {
821         char byte = (char) (v >> (j * 8));
822         fwrite(&byte, 1, 1, fdest);
823       }
824     }
825     fclose(fdest);
826   }
827
828   return 0;
829 }
830
831 /* -->> -->> -->> -->>
832
833 PNM IMAGE FORMAT
834
835 <<-- <<-- <<-- <<-- */
836
837 opj_image_t* pnmtoimage(char *filename, opj_cparameters_t *parameters) {
838   int subsampling_dx = parameters->subsampling_dx;
839   int subsampling_dy = parameters->subsampling_dy;
840
841   FILE *f = NULL;
842   int i, compno, numcomps, w, h;
843   OPJ_COLOR_SPACE color_space;
844   opj_image_cmptparm_t cmptparm[3];  /* maximum of 3 components */
845   opj_image_t * image = NULL;
846   char value;
847
848   f = fopen(filename, "rb");
849   if (!f) {
850     fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
851     return 0;
852   }
853
854   if (fgetc(f) != 'P')
855     return 0;
856   value = fgetc(f);
857
858   switch(value) {
859     case '2':  /* greyscale image type */
860     case '5':
861       numcomps = 1;
862       color_space = CLRSPC_GRAY;
863     break;
864
865     case '3':  /* RGB image type */
866     case '6':
867       numcomps = 3;
868       color_space = CLRSPC_SRGB;
869     break;
870
871     default:
872       fclose(f);
873       return NULL;
874   }
875
876     fgetc(f);
877     
878     /* skip comments */
879     while(fgetc(f) == '#') while(fgetc(f) != '\n');
880     
881     fseek(f, -1, SEEK_CUR);
882     fscanf(f, "%d %d\n255", &w, &h);      
883     fgetc(f);  /* <cr><lf> */
884     
885   /* initialize image components */
886   memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
887   for(i = 0; i < numcomps; i++) {
888     cmptparm[i].prec = 8;
889     cmptparm[i].bpp = 8;
890     cmptparm[i].sgnd = 0;
891     cmptparm[i].dx = subsampling_dx;
892     cmptparm[i].dy = subsampling_dy;
893     cmptparm[i].w = w;
894     cmptparm[i].h = h;
895   }
896   /* create the image */
897   image = opj_image_create(numcomps, &cmptparm[0], color_space);
898   if(!image) {
899     fclose(f);
900     return NULL;
901   }
902
903   /* set image offset and reference grid */
904   image->x0 = parameters->image_offset_x0;
905   image->y0 = parameters->image_offset_y0;
906   image->x1 = parameters->image_offset_x0 + (w - 1) *  subsampling_dx + 1;
907   image->y1 = parameters->image_offset_y0 + (h - 1) *  subsampling_dy + 1;
908
909   /* set image data */
910
911   if ((value == '2') || (value == '3')) {  /* ASCII */
912     for (i = 0; i < w * h; i++) {
913       for(compno = 0; compno < numcomps; compno++) {
914         unsigned int index = 0;
915         fscanf(f, "%u", &index);
916         /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
917         image->comps[compno].data[i] = index;
918       }
919     }
920   } else if ((value == '5') || (value == '6')) {  /* BINARY */
921     for (i = 0; i < w * h; i++) {
922       for(compno = 0; compno < numcomps; compno++) {
923         unsigned char index = 0;
924         fread(&index, 1, 1, f);
925         /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
926         image->comps[compno].data[i] = index;
927       }
928     }
929   }
930
931   fclose(f);
932
933   return image;
934 }
935
936 int imagetopnm(opj_image_t * image, char *outfile) {
937   int w, wr, wrr, h, hr, hrr, max;
938   int i, compno;
939   int adjust;
940   FILE *fdest = NULL;
941   char S2;
942   char *tmp = outfile;
943
944   while (*tmp) {
945     tmp++;
946   }
947   tmp--;
948   tmp--;
949   S2 = *tmp;
950
951   if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
952     && image->comps[1].dx == image->comps[2].dx
953     && image->comps[0].dy == image->comps[1].dy
954     && image->comps[1].dy == image->comps[2].dy
955     && image->comps[0].prec == image->comps[1].prec
956     && image->comps[1].prec == image->comps[2].prec
957     && S2 !='g' && S2 !='G') {
958
959     fdest = fopen(outfile, "wb");
960     if (!fdest) {
961       fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
962       return 1;
963     }
964
965     w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
966     /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */
967     wr = image->comps[0].w;
968     wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
969         
970     h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
971     /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
972     hr = image->comps[0].h;
973     hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
974       
975     max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
976       
977     image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
978     image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 -  int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
979
980     fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
981     adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
982     for (i = 0; i < wrr * hrr; i++) {
983       int r, g, b;
984       unsigned char rc,gc,bc;
985       r = image->comps[0].data[i / wrr * wr + i % wrr];
986       r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
987       rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
988
989       g = image->comps[1].data[i / wrr * wr + i % wrr];
990       g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
991       gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
992       
993       b = image->comps[2].data[i / wrr * wr + i % wrr];
994       b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
995       bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
996       
997       fprintf(fdest, "%c%c%c", rc, gc, bc);
998     }
999     fclose(fdest);
1000   } else {
1001     int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1002     if (image->numcomps>ncomp) {
1003       fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1004       fprintf(stderr,"           is written to the file\n");
1005     }
1006     for (compno = 0; compno < ncomp; compno++) {
1007       char name[256];
1008       if (ncomp > 1) {
1009         sprintf(name, "%d.%s", compno, outfile);
1010       } else {
1011         sprintf(name, "%s", outfile);
1012       }
1013       
1014       fdest = fopen(name, "wb");
1015       if (!fdest) {
1016         fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1017         return 1;
1018       }
1019             
1020       w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1021       /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */
1022       wr = image->comps[compno].w;
1023       wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1024       
1025       h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1026       /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */
1027       hr = image->comps[compno].h;
1028       hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1029       
1030       max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1031       
1032       image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1033       image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1034       
1035       fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1036       adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1037
1038       for (i = 0; i < wrr * hrr; i++) {
1039         int l;
1040         unsigned char lc;
1041         l = image->comps[compno].data[i / wrr * wr + i % wrr];
1042         l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1043         lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1044         fprintf(fdest, "%c", lc);
1045       }
1046       fclose(fdest);
1047     }
1048   }
1049
1050   return 0;
1051 }