fixed various minor warnings occuring under icc9 and bcc32, added MSVC project and...
[openjpeg.git] / 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                                                 default:
421                                                         for (i = 0; i < v; i++) {
422                                                                 v2 = getc(IN);
423                                                                 RGB[line * Info_h.biWidth + col] = v2;
424                                                                 col++;
425                                                         }
426                                                         if (v % 2)
427                                                                 v2 = getc(IN);
428                                                         break;
429                                         }
430                                 }
431                         }
432                         if (gray_scale) {
433                                 index = 0;
434                                 for (line = 0; line < Info_h.biHeight; line++) {
435                                         for (col = 0; col < Info_h.biWidth; col++) {
436                                                 image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
437                                                 index++;
438                                         }
439                                 }
440                         } else {
441                                 index = 0;
442                                 for (line = 0; line < Info_h.biHeight; line++) {
443                                         for (col = 0; col < Info_h.biWidth; col++) {
444                                                 unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
445                                                 image->comps[0].data[index] = table_R[pixel_index];
446                                                 image->comps[1].data[index] = table_G[pixel_index];
447                                                 image->comps[2].data[index] = table_B[pixel_index];
448                                                 index++;
449                                         }
450                                 }
451                         }
452                         free(RGB);
453         } else {
454                 fprintf(stderr, 
455                         "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
456         }
457         fclose(IN);
458  }
459  
460  return image;
461 }
462
463 int imagetobmp(opj_image_t * image, char *outfile) {
464         int w, wr, h, hr;
465         int i, pad;
466         FILE *fdest = NULL;
467
468         if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
469                 && image->comps[1].dx == image->comps[2].dx
470                 && image->comps[0].dy == image->comps[1].dy
471                 && image->comps[1].dy == image->comps[2].dy
472                 && image->comps[0].prec == image->comps[1].prec
473                 && image->comps[1].prec == image->comps[2].prec) {
474                 
475                 /* -->> -->> -->> -->>    
476                 24 bits color       
477                 <<-- <<-- <<-- <<-- */
478             
479                 fdest = fopen(outfile, "wb");
480                 if (!fdest) {
481                         fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
482                         return 1;
483                 }
484             
485                 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
486                 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
487                 w = image->comps[0].w;
488                 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
489             
490                 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
491                 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
492                 h = image->comps[0].h;
493                 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
494             
495                 fprintf(fdest, "BM");
496             
497                 /* FILE HEADER */
498                 /* ------------- */
499                 fprintf(fdest, "%c%c%c%c",
500                         (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
501                         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
502                         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
503                         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
504                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
505                 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
506             
507                 /* INFO HEADER   */
508                 /* ------------- */
509                 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,     ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
510                 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
511                         (unsigned char) ((wr) >> 8) & 0xff,
512                         (unsigned char) ((wr) >> 16) & 0xff,
513                         (unsigned char) ((wr) >> 24) & 0xff);
514                 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
515                         (unsigned char) ((hr) >> 8) & 0xff,
516                         (unsigned char) ((hr) >> 16) & 0xff,
517                         (unsigned char) ((hr) >> 24) & 0xff);
518                 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
519                 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
520                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
521                 fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
522                         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
523                         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
524                         (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
525                 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 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", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
528                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
529             
530                 for (i = 0; i < wr * hr; i++) {
531                         unsigned char R, G, B;
532                         /* a modifier */
533                         /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
534                         R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
535                         /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
536                         G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
537                         /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
538                         B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
539                         fprintf(fdest, "%c%c%c", B, G, R);
540                         
541                         if ((i + 1) % wr == 0) {
542                                 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
543                                         fprintf(fdest, "%c", 0);
544                         }
545                 }
546                 fclose(fdest);
547         } else {                        /* Gray-scale */
548
549                 /* -->> -->> -->> -->>
550                 8 bits non code (Gray scale)
551                 <<-- <<-- <<-- <<-- */
552
553                 fdest = fopen(outfile, "wb");
554                 /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
555                 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
556                 w = image->comps[0].w;
557                 wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
558             
559                 /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
560                 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
561                 h = image->comps[0].h;
562                 hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
563             
564                 fprintf(fdest, "BM");
565             
566                 /* FILE HEADER */
567                 /* ------------- */
568                 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
569                         (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
570                         (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
571                         (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
572                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
573                 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, 
574                         ((54 + 1024) >> 16) & 0xff,
575                         ((54 + 1024) >> 24) & 0xff);
576             
577                 /* INFO HEADER */
578                 /* ------------- */
579                 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,     ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
580                 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
581                         (unsigned char) ((wr) >> 8) & 0xff,
582                         (unsigned char) ((wr) >> 16) & 0xff,
583                         (unsigned char) ((wr) >> 24) & 0xff);
584                 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
585                         (unsigned char) ((hr) >> 8) & 0xff,
586                         (unsigned char) ((hr) >> 16) & 0xff,
587                         (unsigned char) ((hr) >> 24) & 0xff);
588                 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
589                 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
590                 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
591                 fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
592                         (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) &      0xff,
593                         (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) &     0xff,
594                         (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
595                 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 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", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
598                 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
599         }
600
601         for (i = 0; i < 256; i++) {
602                 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
603         }
604
605         for (i = 0; i < wr * hr; i++) {
606                 /* a modifier !! */
607                 /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
608                 fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
609                 /*if (((i + 1) % w == 0 && w % 2))
610                 fprintf(fdest, "%c", 0); */
611                 if ((i + 1) % wr == 0) {
612                         for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--)     /* ADD */
613                                 fprintf(fdest, "%c", 0);
614                 }
615         }
616         fclose(fdest);
617
618         return 0;
619 }
620
621 /* -->> -->> -->> -->>
622
623 PGX IMAGE FORMAT
624
625 <<-- <<-- <<-- <<-- */
626
627
628 unsigned char readuchar(FILE * f)
629 {
630   unsigned char c1;
631   fread(&c1, 1, 1, f);
632   return c1;
633 }
634
635 unsigned short readushort(FILE * f, int bigendian)
636 {
637   unsigned char c1, c2;
638   fread(&c1, 1, 1, f);
639   fread(&c2, 1, 1, f);
640   if (bigendian)
641     return (c1 << 8) + c2;
642   else
643     return (c2 << 8) + c1;
644 }
645
646 unsigned int readuint(FILE * f, int bigendian)
647 {
648   unsigned char c1, c2, c3, c4;
649   fread(&c1, 1, 1, f);
650   fread(&c2, 1, 1, f);
651   fread(&c3, 1, 1, f);
652   fread(&c4, 1, 1, f);
653   if (bigendian)
654     return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
655   else
656     return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
657 }
658
659 opj_image_t* pgxtoimage(char *filename, opj_cparameters_t *parameters) {
660         FILE *f = NULL;
661         int w, h, prec;
662         int i, numcomps, max;
663         OPJ_COLOR_SPACE color_space;
664         opj_image_cmptparm_t cmptparm;  /* maximum of 1 component  */
665         opj_image_t * image = NULL;
666
667         char endian1,endian2,sign;
668         char signtmp[32];
669
670         char temp[32];
671         int bigendian;
672         opj_image_comp_t *comp = NULL;
673
674         numcomps = 1;
675         color_space = CLRSPC_GRAY;
676
677         memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
678
679         max = 0;
680
681         f = fopen(filename, "rb");
682         if (!f) {
683           fprintf(stderr, "Failed to open %s for reading !\n", filename);
684           return NULL;
685         }
686
687         fseek(f, 0, SEEK_SET);
688         fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
689         
690         i=0;
691         sign='+';               
692         while (signtmp[i]!='\0') {
693                 if (signtmp[i]=='-') sign='-';
694                 i++;
695         }
696         
697         fgetc(f);
698         if (endian1=='M' && endian2=='L') {
699                 bigendian = 1;
700         } else if (endian2=='M' && endian1=='L') {
701                 bigendian = 0;
702         } else {
703                 fprintf(stderr, "Bad pgx header, please check input file\n");
704                 return NULL;
705         }
706
707         /* initialize image component */
708
709         cmptparm.x0 = parameters->image_offset_x0;
710         cmptparm.y0 = parameters->image_offset_y0;
711         cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
712         cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
713         
714         if (sign == '-') {
715                 cmptparm.sgnd = 1;
716         } else {
717                 cmptparm.sgnd = 0;
718         }
719         cmptparm.prec = prec;
720         cmptparm.bpp = prec;
721         cmptparm.dx = parameters->subsampling_dx;
722         cmptparm.dy = parameters->subsampling_dy;
723         
724         /* create the image */
725         image = opj_image_create(numcomps, &cmptparm, color_space);
726         if(!image) {
727                 fclose(f);
728                 return NULL;
729         }
730         /* set image offset and reference grid */
731         image->x0 = cmptparm.x0;
732         image->y0 = cmptparm.x0;
733         image->x1 = cmptparm.w;
734         image->y1 = cmptparm.h;
735
736         /* set image data */
737
738         comp = &image->comps[0];
739
740         for (i = 0; i < w * h; i++) {
741                 int v;
742                 if (comp->prec <= 8) {
743                         if (!comp->sgnd) {
744                                 v = readuchar(f);
745                         } else {
746                                 v = (char) readuchar(f);
747                         }
748                 } else if (comp->prec <= 16) {
749                         if (!comp->sgnd) {
750                                 v = readushort(f, bigendian);
751                         } else {
752                                 v = (short) readushort(f, bigendian);
753                         }
754                 } else {
755                         if (!comp->sgnd) {
756                                 v = readuint(f, bigendian);
757                         } else {
758                                 v = (int) readuint(f, bigendian);
759                         }
760                 }
761                 if (v > max)
762                         max = v;
763                 comp->data[i] = v;
764         }
765         fclose(f);
766         comp->bpp = int_floorlog2(max) + 1;
767
768         return image;
769 }
770
771 int imagetopgx(opj_image_t * image, char *outfile) {
772         int w, wr, h, hr;
773         int i, j, compno;
774         FILE *fdest = NULL;
775
776         for (compno = 0; compno < image->numcomps; compno++) {
777                 opj_image_comp_t *comp = &image->comps[compno];
778                 char name[256];
779                 int nbytes = 0;
780                 char *tmp = outfile;
781                 while (*tmp) {
782                         tmp++;
783                 }
784                 while (*tmp!='.') {
785                         tmp--;
786                 }
787                 *tmp='\0';
788
789                 if (image->numcomps > 1) {
790                         sprintf(name, "%s-%d.pgx", outfile, compno);
791                 } else {
792                         sprintf(name, "%s.pgx", outfile);
793                 }
794                 fdest = fopen(name, "wb");
795                 if (!fdest) {
796                         fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
797                         return 1;
798                 }
799                 /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */
800                 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */
801                 w = image->comps[compno].w;
802                 wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
803             
804                 /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */
805                 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */
806                 h = image->comps[compno].h;
807                 hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
808             
809                 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
810                 if (comp->prec <= 8) {
811                         nbytes = 1;
812                 } else if (comp->prec <= 16) {
813                         nbytes = 2;
814                 } else {
815                         nbytes = 4;
816                 }
817                 for (i = 0; i < wr * hr; i++) {
818                         int v = image->comps[compno].data[i / wr * w + i % wr];
819                         for (j = nbytes - 1; j >= 0; j--) {
820                                 char byte = (char) (v >> (j * 8));
821                                 fwrite(&byte, 1, 1, fdest);
822                         }
823                 }
824                 fclose(fdest);
825         }
826
827         return 0;
828 }
829
830 /* -->> -->> -->> -->>
831
832 PNM IMAGE FORMAT
833
834 <<-- <<-- <<-- <<-- */
835
836 opj_image_t* pnmtoimage(char *filename, opj_cparameters_t *parameters) {
837         int subsampling_dx = parameters->subsampling_dx;
838         int subsampling_dy = parameters->subsampling_dy;
839
840         FILE *f = NULL;
841         int i, compno, numcomps, w, h;
842         OPJ_COLOR_SPACE color_space;
843         opj_image_cmptparm_t cmptparm[3];       /* maximum of 3 components */
844         opj_image_t * image = NULL;
845         char value;
846         
847         f = fopen(filename, "rb");
848         if (!f) {
849                 fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
850                 return 0;
851         }
852
853         if (fgetc(f) != 'P')
854                 return 0;
855         value = fgetc(f);
856
857                 switch(value) {
858                         case '2':       /* greyscale image type */
859                         case '5':
860                                 numcomps = 1;
861                                 color_space = CLRSPC_GRAY;
862                                 break;
863                                 
864                         case '3':       /* RGB image type */
865                         case '6':
866                                 numcomps = 3;
867                                 color_space = CLRSPC_SRGB;
868                                 break;
869                                 
870                         default:
871                                 fclose(f);
872                                 return NULL;
873                 }
874                 
875                 fgetc(f);
876                 
877                 /* skip comments */
878                 while(fgetc(f) == '#') while(fgetc(f) != '\n');
879                 
880                 fseek(f, -1, SEEK_CUR);
881                 fscanf(f, "%d %d\n255", &w, &h);                        
882                 fgetc(f);       /* <cr><lf> */
883                 
884         /* initialize image components */
885         memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
886         for(i = 0; i < numcomps; i++) {
887                 cmptparm[i].prec = 8;
888                 cmptparm[i].bpp = 8;
889                 cmptparm[i].sgnd = 0;
890                 cmptparm[i].dx = subsampling_dx;
891                 cmptparm[i].dy = subsampling_dy;
892                 cmptparm[i].w = w;
893                 cmptparm[i].h = h;
894         }
895         /* create the image */
896         image = opj_image_create(numcomps, &cmptparm[0], color_space);
897         if(!image) {
898                 fclose(f);
899                 return NULL;
900         }
901
902         /* set image offset and reference grid */
903         image->x0 = parameters->image_offset_x0;
904         image->y0 = parameters->image_offset_y0;
905         image->x1 = parameters->image_offset_x0 + (w - 1) *     subsampling_dx + 1;
906         image->y1 = parameters->image_offset_y0 + (h - 1) *     subsampling_dy + 1;
907
908         /* set image data */
909
910         if ((value == '2') || (value == '3')) { /* ASCII */
911                 for (i = 0; i < w * h; i++) {
912                         for(compno = 0; compno < numcomps; compno++) {
913                                 unsigned int index = 0;
914                                 fscanf(f, "%u", &index);
915                                 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
916                                 image->comps[compno].data[i] = index;
917                         }
918                 }
919         } else if ((value == '5') || (value == '6')) {  /* BINARY */
920                 for (i = 0; i < w * h; i++) {
921                         for(compno = 0; compno < numcomps; compno++) {
922                                 unsigned char index = 0;
923                                 fread(&index, 1, 1, f);
924                                 /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
925                                 image->comps[compno].data[i] = index;
926                         }
927                 }
928         }
929
930         fclose(f);
931
932         return image;
933 }
934
935 int imagetopnm(opj_image_t * image, char *outfile) {
936         int w, wr, wrr, h, hr, hrr, max;
937         int i, compno;
938         int adjust;
939         FILE *fdest = NULL;
940         char S2;
941         char *tmp = outfile;
942
943         while (*tmp) {
944                 tmp++;
945         }
946         tmp--;
947         tmp--;
948         S2 = *tmp;
949
950         if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
951                 && image->comps[1].dx == image->comps[2].dx
952                 && image->comps[0].dy == image->comps[1].dy
953                 && image->comps[1].dy == image->comps[2].dy
954                 && image->comps[0].prec == image->comps[1].prec
955                 && image->comps[1].prec == image->comps[2].prec
956                 && S2 !='g' && S2 !='G') {
957
958                 fdest = fopen(outfile, "wb");
959                 if (!fdest) {
960                         fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
961                         return 1;
962                 }
963
964                 w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
965                 /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */
966                 wr = image->comps[0].w;
967                 wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
968         
969                 h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
970                 /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
971                 hr = image->comps[0].h;
972                 hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
973             
974                 max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
975             
976                 image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
977                 image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 -       int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
978
979                 fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
980                 adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
981                 for (i = 0; i < wrr * hrr; i++) {
982                         int r, g, b;
983                         unsigned char rc,gc,bc;
984                         r = image->comps[0].data[i / wrr * wr + i % wrr];
985                         r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
986                         rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
987
988                         g = image->comps[1].data[i / wrr * wr + i % wrr];
989                         g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
990                         gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
991                         
992                         b = image->comps[2].data[i / wrr * wr + i % wrr];
993                         b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
994                         bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
995                         
996                         fprintf(fdest, "%c%c%c", rc, gc, bc);
997                 }
998                 fclose(fdest);
999         } else {
1000                 int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
1001                 if (image->numcomps>ncomp) {
1002                         fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
1003                         fprintf(stderr,"           is written to the file\n");
1004                 }
1005                 for (compno = 0; compno < ncomp; compno++) {
1006                         char name[256];
1007                         if (ncomp > 1) {
1008                                 sprintf(name, "%d.%s", compno, outfile);
1009                         } else {
1010                                 sprintf(name, "%s", outfile);
1011                         }
1012                         
1013                         fdest = fopen(name, "wb");
1014                         if (!fdest) {
1015                                 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1016                                 return 1;
1017                         }
1018             
1019                         w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1020                         /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */
1021                         wr = image->comps[compno].w;
1022                         wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
1023                         
1024                         h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
1025                         /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */
1026                         hr = image->comps[compno].h;
1027                         hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
1028                         
1029                         max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
1030                         
1031                         image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
1032                         image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
1033                         
1034                         fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
1035                         adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;
1036
1037                         for (i = 0; i < wrr * hrr; i++) {
1038                                 int l;
1039                                 unsigned char lc;
1040                                 l = image->comps[compno].data[i / wrr * wr + i % wrr];
1041                                 l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1042                                 lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
1043                                 fprintf(fdest, "%c", lc);
1044                         }
1045                         fclose(fdest);
1046                 }
1047         }
1048
1049         return 0;
1050 }