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