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