Merge pull request #919 from rouault/reformat
[openjpeg.git] / src / bin / jp2 / convertbmp.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses
3  * BSD License, included below. This software may be subject to other third
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2006-2007, Parvatha Elangovan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 #include "opj_apps_config.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44
45 #include "openjpeg.h"
46 #include "convert.h"
47
48 typedef struct {
49     OPJ_UINT16 bfType;      /* 'BM' for Bitmap (19776) */
50     OPJ_UINT32 bfSize;      /* Size of the file        */
51     OPJ_UINT16 bfReserved1; /* Reserved : 0            */
52     OPJ_UINT16 bfReserved2; /* Reserved : 0            */
53     OPJ_UINT32 bfOffBits;   /* Offset                  */
54 } OPJ_BITMAPFILEHEADER;
55
56 typedef struct {
57     OPJ_UINT32 biSize;             /* Size of the structure in bytes */
58     OPJ_UINT32 biWidth;            /* Width of the image in pixels */
59     OPJ_UINT32 biHeight;           /* Heigth of the image in pixels */
60     OPJ_UINT16 biPlanes;           /* 1 */
61     OPJ_UINT16 biBitCount;         /* Number of color bits by pixels */
62     OPJ_UINT32 biCompression;      /* Type of encoding 0: none 1: RLE8 2: RLE4 */
63     OPJ_UINT32 biSizeImage;        /* Size of the image in bytes */
64     OPJ_UINT32 biXpelsPerMeter;    /* Horizontal (X) resolution in pixels/meter */
65     OPJ_UINT32 biYpelsPerMeter;    /* Vertical (Y) resolution in pixels/meter */
66     OPJ_UINT32 biClrUsed;          /* Number of color used in the image (0: ALL) */
67     OPJ_UINT32 biClrImportant;     /* Number of important color (0: ALL) */
68     OPJ_UINT32 biRedMask;          /* Red channel bit mask */
69     OPJ_UINT32 biGreenMask;        /* Green channel bit mask */
70     OPJ_UINT32 biBlueMask;         /* Blue channel bit mask */
71     OPJ_UINT32 biAlphaMask;        /* Alpha channel bit mask */
72     OPJ_UINT32 biColorSpaceType;   /* Color space type */
73     OPJ_UINT8  biColorSpaceEP[36]; /* Color space end points */
74     OPJ_UINT32 biRedGamma;         /* Red channel gamma */
75     OPJ_UINT32 biGreenGamma;       /* Green channel gamma */
76     OPJ_UINT32 biBlueGamma;        /* Blue channel gamma */
77     OPJ_UINT32 biIntent;           /* Intent */
78     OPJ_UINT32 biIccProfileData;   /* ICC profile data */
79     OPJ_UINT32 biIccProfileSize;   /* ICC profile size */
80     OPJ_UINT32 biReserved;         /* Reserved */
81 } OPJ_BITMAPINFOHEADER;
82
83 static void opj_applyLUT8u_8u32s_C1R(
84     OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride,
85     OPJ_INT32* pDst, OPJ_INT32 dstStride,
86     OPJ_UINT8 const* pLUT,
87     OPJ_UINT32 width, OPJ_UINT32 height)
88 {
89     OPJ_UINT32 y;
90
91     for (y = height; y != 0U; --y) {
92         OPJ_UINT32 x;
93
94         for (x = 0; x < width; x++) {
95             pDst[x] = (OPJ_INT32)pLUT[pSrc[x]];
96         }
97         pSrc += srcStride;
98         pDst += dstStride;
99     }
100 }
101
102 static void opj_applyLUT8u_8u32s_C1P3R(
103     OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride,
104     OPJ_INT32* const* pDst, OPJ_INT32 const* pDstStride,
105     OPJ_UINT8 const* const* pLUT,
106     OPJ_UINT32 width, OPJ_UINT32 height)
107 {
108     OPJ_UINT32 y;
109     OPJ_INT32* pR = pDst[0];
110     OPJ_INT32* pG = pDst[1];
111     OPJ_INT32* pB = pDst[2];
112     OPJ_UINT8 const* pLUT_R = pLUT[0];
113     OPJ_UINT8 const* pLUT_G = pLUT[1];
114     OPJ_UINT8 const* pLUT_B = pLUT[2];
115
116     for (y = height; y != 0U; --y) {
117         OPJ_UINT32 x;
118
119         for (x = 0; x < width; x++) {
120             OPJ_UINT8 idx = pSrc[x];
121             pR[x] = (OPJ_INT32)pLUT_R[idx];
122             pG[x] = (OPJ_INT32)pLUT_G[idx];
123             pB[x] = (OPJ_INT32)pLUT_B[idx];
124         }
125         pSrc += srcStride;
126         pR += pDstStride[0];
127         pG += pDstStride[1];
128         pB += pDstStride[2];
129     }
130 }
131
132 static void bmp24toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
133                          opj_image_t* image)
134 {
135     int index;
136     OPJ_UINT32 width, height;
137     OPJ_UINT32 x, y;
138     const OPJ_UINT8 *pSrc = NULL;
139
140     width  = image->comps[0].w;
141     height = image->comps[0].h;
142
143     index = 0;
144     pSrc = pData + (height - 1U) * stride;
145     for (y = 0; y < height; y++) {
146         for (x = 0; x < width; x++) {
147             image->comps[0].data[index] = (OPJ_INT32)pSrc[3 * x + 2]; /* R */
148             image->comps[1].data[index] = (OPJ_INT32)pSrc[3 * x + 1]; /* G */
149             image->comps[2].data[index] = (OPJ_INT32)pSrc[3 * x + 0]; /* B */
150             index++;
151         }
152         pSrc -= stride;
153     }
154 }
155
156 static void bmp_mask_get_shift_and_prec(OPJ_UINT32 mask, OPJ_UINT32* shift,
157                                         OPJ_UINT32* prec)
158 {
159     OPJ_UINT32 l_shift, l_prec;
160
161     l_shift = l_prec = 0U;
162
163     if (mask != 0U) {
164         while ((mask & 1U) == 0U) {
165             mask >>= 1;
166             l_shift++;
167         }
168         while (mask & 1U) {
169             mask >>= 1;
170             l_prec++;
171         }
172     }
173     *shift = l_shift;
174     *prec = l_prec;
175 }
176
177 static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
178                              opj_image_t* image, OPJ_UINT32 redMask, OPJ_UINT32 greenMask,
179                              OPJ_UINT32 blueMask, OPJ_UINT32 alphaMask)
180 {
181     int index;
182     OPJ_UINT32 width, height;
183     OPJ_UINT32 x, y;
184     const OPJ_UINT8 *pSrc = NULL;
185     OPJ_BOOL hasAlpha;
186     OPJ_UINT32 redShift,   redPrec;
187     OPJ_UINT32 greenShift, greenPrec;
188     OPJ_UINT32 blueShift,  bluePrec;
189     OPJ_UINT32 alphaShift, alphaPrec;
190
191     width  = image->comps[0].w;
192     height = image->comps[0].h;
193
194     hasAlpha = image->numcomps > 3U;
195
196     bmp_mask_get_shift_and_prec(redMask,   &redShift,   &redPrec);
197     bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec);
198     bmp_mask_get_shift_and_prec(blueMask,  &blueShift,  &bluePrec);
199     bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
200
201     image->comps[0].bpp = redPrec;
202     image->comps[0].prec = redPrec;
203     image->comps[1].bpp = greenPrec;
204     image->comps[1].prec = greenPrec;
205     image->comps[2].bpp = bluePrec;
206     image->comps[2].prec = bluePrec;
207     if (hasAlpha) {
208         image->comps[3].bpp = alphaPrec;
209         image->comps[3].prec = alphaPrec;
210     }
211
212     index = 0;
213     pSrc = pData + (height - 1U) * stride;
214     for (y = 0; y < height; y++) {
215         for (x = 0; x < width; x++) {
216             OPJ_UINT32 value = 0U;
217
218             value |= ((OPJ_UINT32)pSrc[4 * x + 0]) <<  0;
219             value |= ((OPJ_UINT32)pSrc[4 * x + 1]) <<  8;
220             value |= ((OPJ_UINT32)pSrc[4 * x + 2]) << 16;
221             value |= ((OPJ_UINT32)pSrc[4 * x + 3]) << 24;
222
223             image->comps[0].data[index] = (OPJ_INT32)((value & redMask)   >>
224                                           redShift);   /* R */
225             image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >>
226                                           greenShift); /* G */
227             image->comps[2].data[index] = (OPJ_INT32)((value & blueMask)  >>
228                                           blueShift);  /* B */
229             if (hasAlpha) {
230                 image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask)  >>
231                                               alphaShift);  /* A */
232             }
233             index++;
234         }
235         pSrc -= stride;
236     }
237 }
238
239 static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
240                              opj_image_t* image, OPJ_UINT32 redMask, OPJ_UINT32 greenMask,
241                              OPJ_UINT32 blueMask, OPJ_UINT32 alphaMask)
242 {
243     int index;
244     OPJ_UINT32 width, height;
245     OPJ_UINT32 x, y;
246     const OPJ_UINT8 *pSrc = NULL;
247     OPJ_BOOL hasAlpha;
248     OPJ_UINT32 redShift,   redPrec;
249     OPJ_UINT32 greenShift, greenPrec;
250     OPJ_UINT32 blueShift,  bluePrec;
251     OPJ_UINT32 alphaShift, alphaPrec;
252
253     width  = image->comps[0].w;
254     height = image->comps[0].h;
255
256     hasAlpha = image->numcomps > 3U;
257
258     bmp_mask_get_shift_and_prec(redMask,   &redShift,   &redPrec);
259     bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec);
260     bmp_mask_get_shift_and_prec(blueMask,  &blueShift,  &bluePrec);
261     bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
262
263     image->comps[0].bpp = redPrec;
264     image->comps[0].prec = redPrec;
265     image->comps[1].bpp = greenPrec;
266     image->comps[1].prec = greenPrec;
267     image->comps[2].bpp = bluePrec;
268     image->comps[2].prec = bluePrec;
269     if (hasAlpha) {
270         image->comps[3].bpp = alphaPrec;
271         image->comps[3].prec = alphaPrec;
272     }
273
274     index = 0;
275     pSrc = pData + (height - 1U) * stride;
276     for (y = 0; y < height; y++) {
277         for (x = 0; x < width; x++) {
278             OPJ_UINT32 value = 0U;
279
280             value |= ((OPJ_UINT32)pSrc[2 * x + 0]) <<  0;
281             value |= ((OPJ_UINT32)pSrc[2 * x + 1]) <<  8;
282
283             image->comps[0].data[index] = (OPJ_INT32)((value & redMask)   >>
284                                           redShift);   /* R */
285             image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >>
286                                           greenShift); /* G */
287             image->comps[2].data[index] = (OPJ_INT32)((value & blueMask)  >>
288                                           blueShift);  /* B */
289             if (hasAlpha) {
290                 image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask)  >>
291                                               alphaShift);  /* A */
292             }
293             index++;
294         }
295         pSrc -= stride;
296     }
297 }
298
299 static opj_image_t* bmp8toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
300                                 opj_image_t* image, OPJ_UINT8 const* const* pLUT)
301 {
302     OPJ_UINT32 width, height;
303     const OPJ_UINT8 *pSrc = NULL;
304
305     width  = image->comps[0].w;
306     height = image->comps[0].h;
307
308     pSrc = pData + (height - 1U) * stride;
309     if (image->numcomps == 1U) {
310         opj_applyLUT8u_8u32s_C1R(pSrc, -(OPJ_INT32)stride, image->comps[0].data,
311                                  (OPJ_INT32)width, pLUT[0], width, height);
312     } else {
313         OPJ_INT32* pDst[3];
314         OPJ_INT32  pDstStride[3];
315
316         pDst[0] = image->comps[0].data;
317         pDst[1] = image->comps[1].data;
318         pDst[2] = image->comps[2].data;
319         pDstStride[0] = (OPJ_INT32)width;
320         pDstStride[1] = (OPJ_INT32)width;
321         pDstStride[2] = (OPJ_INT32)width;
322         opj_applyLUT8u_8u32s_C1P3R(pSrc, -(OPJ_INT32)stride, pDst, pDstStride, pLUT,
323                                    width, height);
324     }
325     return image;
326 }
327
328 static OPJ_BOOL bmp_read_file_header(FILE* IN, OPJ_BITMAPFILEHEADER* header)
329 {
330     header->bfType  = (OPJ_UINT16)getc(IN);
331     header->bfType |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
332
333     if (header->bfType != 19778) {
334         fprintf(stderr, "Error, not a BMP file!\n");
335         return OPJ_FALSE;
336     }
337
338     /* FILE HEADER */
339     /* ------------- */
340     header->bfSize  = (OPJ_UINT32)getc(IN);
341     header->bfSize |= (OPJ_UINT32)getc(IN) << 8;
342     header->bfSize |= (OPJ_UINT32)getc(IN) << 16;
343     header->bfSize |= (OPJ_UINT32)getc(IN) << 24;
344
345     header->bfReserved1  = (OPJ_UINT16)getc(IN);
346     header->bfReserved1 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
347
348     header->bfReserved2  = (OPJ_UINT16)getc(IN);
349     header->bfReserved2 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
350
351     header->bfOffBits  = (OPJ_UINT32)getc(IN);
352     header->bfOffBits |= (OPJ_UINT32)getc(IN) << 8;
353     header->bfOffBits |= (OPJ_UINT32)getc(IN) << 16;
354     header->bfOffBits |= (OPJ_UINT32)getc(IN) << 24;
355     return OPJ_TRUE;
356 }
357 static OPJ_BOOL bmp_read_info_header(FILE* IN, OPJ_BITMAPINFOHEADER* header)
358 {
359     memset(header, 0, sizeof(*header));
360     /* INFO HEADER */
361     /* ------------- */
362     header->biSize  = (OPJ_UINT32)getc(IN);
363     header->biSize |= (OPJ_UINT32)getc(IN) << 8;
364     header->biSize |= (OPJ_UINT32)getc(IN) << 16;
365     header->biSize |= (OPJ_UINT32)getc(IN) << 24;
366
367     switch (header->biSize) {
368     case 12U:  /* BITMAPCOREHEADER */
369     case 40U:  /* BITMAPINFOHEADER */
370     case 52U:  /* BITMAPV2INFOHEADER */
371     case 56U:  /* BITMAPV3INFOHEADER */
372     case 108U: /* BITMAPV4HEADER */
373     case 124U: /* BITMAPV5HEADER */
374         break;
375     default:
376         fprintf(stderr, "Error, unknown BMP header size %d\n", header->biSize);
377         return OPJ_FALSE;
378     }
379
380     header->biWidth  = (OPJ_UINT32)getc(IN);
381     header->biWidth |= (OPJ_UINT32)getc(IN) << 8;
382     header->biWidth |= (OPJ_UINT32)getc(IN) << 16;
383     header->biWidth |= (OPJ_UINT32)getc(IN) << 24;
384
385     header->biHeight  = (OPJ_UINT32)getc(IN);
386     header->biHeight |= (OPJ_UINT32)getc(IN) << 8;
387     header->biHeight |= (OPJ_UINT32)getc(IN) << 16;
388     header->biHeight |= (OPJ_UINT32)getc(IN) << 24;
389
390     header->biPlanes  = (OPJ_UINT16)getc(IN);
391     header->biPlanes |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
392
393     header->biBitCount  = (OPJ_UINT16)getc(IN);
394     header->biBitCount |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
395
396     if (header->biSize >= 40U) {
397         header->biCompression  = (OPJ_UINT32)getc(IN);
398         header->biCompression |= (OPJ_UINT32)getc(IN) << 8;
399         header->biCompression |= (OPJ_UINT32)getc(IN) << 16;
400         header->biCompression |= (OPJ_UINT32)getc(IN) << 24;
401
402         header->biSizeImage  = (OPJ_UINT32)getc(IN);
403         header->biSizeImage |= (OPJ_UINT32)getc(IN) << 8;
404         header->biSizeImage |= (OPJ_UINT32)getc(IN) << 16;
405         header->biSizeImage |= (OPJ_UINT32)getc(IN) << 24;
406
407         header->biXpelsPerMeter  = (OPJ_UINT32)getc(IN);
408         header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8;
409         header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16;
410         header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24;
411
412         header->biYpelsPerMeter  = (OPJ_UINT32)getc(IN);
413         header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8;
414         header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16;
415         header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24;
416
417         header->biClrUsed  = (OPJ_UINT32)getc(IN);
418         header->biClrUsed |= (OPJ_UINT32)getc(IN) << 8;
419         header->biClrUsed |= (OPJ_UINT32)getc(IN) << 16;
420         header->biClrUsed |= (OPJ_UINT32)getc(IN) << 24;
421
422         header->biClrImportant  = (OPJ_UINT32)getc(IN);
423         header->biClrImportant |= (OPJ_UINT32)getc(IN) << 8;
424         header->biClrImportant |= (OPJ_UINT32)getc(IN) << 16;
425         header->biClrImportant |= (OPJ_UINT32)getc(IN) << 24;
426     }
427
428     if (header->biSize >= 56U) {
429         header->biRedMask  = (OPJ_UINT32)getc(IN);
430         header->biRedMask |= (OPJ_UINT32)getc(IN) << 8;
431         header->biRedMask |= (OPJ_UINT32)getc(IN) << 16;
432         header->biRedMask |= (OPJ_UINT32)getc(IN) << 24;
433
434         header->biGreenMask  = (OPJ_UINT32)getc(IN);
435         header->biGreenMask |= (OPJ_UINT32)getc(IN) << 8;
436         header->biGreenMask |= (OPJ_UINT32)getc(IN) << 16;
437         header->biGreenMask |= (OPJ_UINT32)getc(IN) << 24;
438
439         header->biBlueMask  = (OPJ_UINT32)getc(IN);
440         header->biBlueMask |= (OPJ_UINT32)getc(IN) << 8;
441         header->biBlueMask |= (OPJ_UINT32)getc(IN) << 16;
442         header->biBlueMask |= (OPJ_UINT32)getc(IN) << 24;
443
444         header->biAlphaMask  = (OPJ_UINT32)getc(IN);
445         header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 8;
446         header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 16;
447         header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 24;
448     }
449
450     if (header->biSize >= 108U) {
451         header->biColorSpaceType  = (OPJ_UINT32)getc(IN);
452         header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 8;
453         header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 16;
454         header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 24;
455
456         if (fread(&(header->biColorSpaceEP), 1U, sizeof(header->biColorSpaceEP),
457                   IN) != sizeof(header->biColorSpaceEP)) {
458             fprintf(stderr, "Error, can't  read BMP header\n");
459             return OPJ_FALSE;
460         }
461
462         header->biRedGamma  = (OPJ_UINT32)getc(IN);
463         header->biRedGamma |= (OPJ_UINT32)getc(IN) << 8;
464         header->biRedGamma |= (OPJ_UINT32)getc(IN) << 16;
465         header->biRedGamma |= (OPJ_UINT32)getc(IN) << 24;
466
467         header->biGreenGamma  = (OPJ_UINT32)getc(IN);
468         header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 8;
469         header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 16;
470         header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 24;
471
472         header->biBlueGamma  = (OPJ_UINT32)getc(IN);
473         header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 8;
474         header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 16;
475         header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 24;
476     }
477
478     if (header->biSize >= 124U) {
479         header->biIntent  = (OPJ_UINT32)getc(IN);
480         header->biIntent |= (OPJ_UINT32)getc(IN) << 8;
481         header->biIntent |= (OPJ_UINT32)getc(IN) << 16;
482         header->biIntent |= (OPJ_UINT32)getc(IN) << 24;
483
484         header->biIccProfileData  = (OPJ_UINT32)getc(IN);
485         header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 8;
486         header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 16;
487         header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 24;
488
489         header->biIccProfileSize  = (OPJ_UINT32)getc(IN);
490         header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 8;
491         header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 16;
492         header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 24;
493
494         header->biReserved  = (OPJ_UINT32)getc(IN);
495         header->biReserved |= (OPJ_UINT32)getc(IN) << 8;
496         header->biReserved |= (OPJ_UINT32)getc(IN) << 16;
497         header->biReserved |= (OPJ_UINT32)getc(IN) << 24;
498     }
499     return OPJ_TRUE;
500 }
501
502 static OPJ_BOOL bmp_read_raw_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride,
503                                   OPJ_UINT32 width, OPJ_UINT32 height)
504 {
505     OPJ_ARG_NOT_USED(width);
506
507     if (fread(pData, sizeof(OPJ_UINT8), stride * height, IN) != (stride * height)) {
508         fprintf(stderr,
509                 "\nError: fread return a number of element different from the expected.\n");
510         return OPJ_FALSE;
511     }
512     return OPJ_TRUE;
513 }
514
515 static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData,
516                                    OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
517 {
518     OPJ_UINT32 x, y;
519     OPJ_UINT8 *pix;
520     const OPJ_UINT8 *beyond;
521
522     beyond = pData + stride * height;
523     pix = pData;
524
525     x = y = 0U;
526     while (y < height) {
527         int c = getc(IN);
528
529         if (c) {
530             int j;
531             OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
532
533             for (j = 0; (j < c) && (x < width) &&
534                     ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
535                 *pix = c1;
536             }
537         } else {
538             c = getc(IN);
539             if (c == 0x00) { /* EOL */
540                 x = 0;
541                 ++y;
542                 pix = pData + y * stride + x;
543             } else if (c == 0x01) { /* EOP */
544                 break;
545             } else if (c == 0x02) { /* MOVE by dxdy */
546                 c = getc(IN);
547                 x += (OPJ_UINT32)c;
548                 c = getc(IN);
549                 y += (OPJ_UINT32)c;
550                 pix = pData + y * stride + x;
551             } else { /* 03 .. 255 */
552                 int j;
553                 for (j = 0; (j < c) && (x < width) &&
554                         ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
555                     OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
556                     *pix = c1;
557                 }
558                 if ((OPJ_UINT32)c & 1U) { /* skip padding byte */
559                     getc(IN);
560                 }
561             }
562         }
563     }/* while() */
564     return OPJ_TRUE;
565 }
566
567 static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData,
568                                    OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
569 {
570     OPJ_UINT32 x, y;
571     OPJ_UINT8 *pix;
572     const OPJ_UINT8 *beyond;
573
574     beyond = pData + stride * height;
575     pix = pData;
576     x = y = 0U;
577     while (y < height) {
578         int c = getc(IN);
579         if (c == EOF) {
580             break;
581         }
582
583         if (c) { /* encoded mode */
584             int j;
585             OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
586
587             for (j = 0; (j < c) && (x < width) &&
588                     ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
589                 *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU));
590             }
591         } else { /* absolute mode */
592             c = getc(IN);
593             if (c == EOF) {
594                 break;
595             }
596
597             if (c == 0x00) { /* EOL */
598                 x = 0;
599                 y++;
600                 pix = pData + y * stride;
601             } else if (c == 0x01) { /* EOP */
602                 break;
603             } else if (c == 0x02) { /* MOVE by dxdy */
604                 c = getc(IN);
605                 x += (OPJ_UINT32)c;
606                 c = getc(IN);
607                 y += (OPJ_UINT32)c;
608                 pix = pData + y * stride + x;
609             } else { /* 03 .. 255 : absolute mode */
610                 int j;
611                 OPJ_UINT8 c1 = 0U;
612
613                 for (j = 0; (j < c) && (x < width) &&
614                         ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
615                     if ((j & 1) == 0) {
616                         c1 = (OPJ_UINT8)getc(IN);
617                     }
618                     *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU));
619                 }
620                 if (((c & 3) == 1) || ((c & 3) == 2)) { /* skip padding byte */
621                     getc(IN);
622                 }
623             }
624         }
625     }  /* while(y < height) */
626     return OPJ_TRUE;
627 }
628
629 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
630 {
631     opj_image_cmptparm_t cmptparm[4];   /* maximum of 4 components */
632     OPJ_UINT8 lut_R[256], lut_G[256], lut_B[256];
633     OPJ_UINT8 const* pLUT[3];
634     opj_image_t * image = NULL;
635     FILE *IN;
636     OPJ_BITMAPFILEHEADER File_h;
637     OPJ_BITMAPINFOHEADER Info_h;
638     OPJ_UINT32 i, palette_len, numcmpts = 1U;
639     OPJ_BOOL l_result = OPJ_FALSE;
640     OPJ_UINT8* pData = NULL;
641     OPJ_UINT32 stride;
642
643     pLUT[0] = lut_R;
644     pLUT[1] = lut_G;
645     pLUT[2] = lut_B;
646
647     IN = fopen(filename, "rb");
648     if (!IN) {
649         fprintf(stderr, "Failed to open %s for reading !!\n", filename);
650         return NULL;
651     }
652
653     if (!bmp_read_file_header(IN, &File_h)) {
654         fclose(IN);
655         return NULL;
656     }
657     if (!bmp_read_info_header(IN, &Info_h)) {
658         fclose(IN);
659         return NULL;
660     }
661
662     /* Load palette */
663     if (Info_h.biBitCount <= 8U) {
664         memset(&lut_R[0], 0, sizeof(lut_R));
665         memset(&lut_G[0], 0, sizeof(lut_G));
666         memset(&lut_B[0], 0, sizeof(lut_B));
667
668         palette_len = Info_h.biClrUsed;
669         if ((palette_len == 0U) && (Info_h.biBitCount <= 8U)) {
670             palette_len = (1U << Info_h.biBitCount);
671         }
672         if (palette_len > 256U) {
673             palette_len = 256U;
674         }
675         if (palette_len > 0U) {
676             OPJ_UINT8 has_color = 0U;
677             for (i = 0U; i < palette_len; i++) {
678                 lut_B[i] = (OPJ_UINT8)getc(IN);
679                 lut_G[i] = (OPJ_UINT8)getc(IN);
680                 lut_R[i] = (OPJ_UINT8)getc(IN);
681                 (void)getc(IN); /* padding */
682                 has_color |= (lut_B[i] ^ lut_G[i]) | (lut_G[i] ^ lut_R[i]);
683             }
684             if (has_color) {
685                 numcmpts = 3U;
686             }
687         }
688     } else {
689         numcmpts = 3U;
690         if ((Info_h.biCompression == 3) && (Info_h.biAlphaMask != 0U)) {
691             numcmpts++;
692         }
693     }
694
695     if (Info_h.biWidth == 0 || Info_h.biHeight == 0) {
696         fclose(IN);
697         return NULL;
698     }
699
700     if (Info_h.biBitCount > (((OPJ_UINT32) - 1) - 31) / Info_h.biWidth) {
701         fclose(IN);
702         return NULL;
703     }
704     stride = ((Info_h.biWidth * Info_h.biBitCount + 31U) / 32U) *
705              4U; /* rows are aligned on 32bits */
706     if (Info_h.biBitCount == 4 &&
707             Info_h.biCompression == 2) { /* RLE 4 gets decoded as 8 bits data for now... */
708         if (8 > (((OPJ_UINT32) - 1) - 31) / Info_h.biWidth) {
709             fclose(IN);
710             return NULL;
711         }
712         stride = ((Info_h.biWidth * 8U + 31U) / 32U) * 4U;
713     }
714
715     if (stride > ((OPJ_UINT32) - 1) / sizeof(OPJ_UINT8) / Info_h.biHeight) {
716         fclose(IN);
717         return NULL;
718     }
719     pData = (OPJ_UINT8 *) calloc(1, stride * Info_h.biHeight * sizeof(OPJ_UINT8));
720     if (pData == NULL) {
721         fclose(IN);
722         return NULL;
723     }
724     /* Place the cursor at the beginning of the image information */
725     fseek(IN, 0, SEEK_SET);
726     fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
727
728     switch (Info_h.biCompression) {
729     case 0:
730     case 3:
731         /* read raw data */
732         l_result = bmp_read_raw_data(IN, pData, stride, Info_h.biWidth,
733                                      Info_h.biHeight);
734         break;
735     case 1:
736         /* read rle8 data */
737         l_result = bmp_read_rle8_data(IN, pData, stride, Info_h.biWidth,
738                                       Info_h.biHeight);
739         break;
740     case 2:
741         /* read rle4 data */
742         l_result = bmp_read_rle4_data(IN, pData, stride, Info_h.biWidth,
743                                       Info_h.biHeight);
744         break;
745     default:
746         fprintf(stderr, "Unsupported BMP compression\n");
747         l_result = OPJ_FALSE;
748         break;
749     }
750     if (!l_result) {
751         free(pData);
752         fclose(IN);
753         return NULL;
754     }
755
756     /* create the image */
757     memset(&cmptparm[0], 0, sizeof(cmptparm));
758     for (i = 0; i < 4U; i++) {
759         cmptparm[i].prec = 8;
760         cmptparm[i].bpp  = 8;
761         cmptparm[i].sgnd = 0;
762         cmptparm[i].dx   = (OPJ_UINT32)parameters->subsampling_dx;
763         cmptparm[i].dy   = (OPJ_UINT32)parameters->subsampling_dy;
764         cmptparm[i].w    = Info_h.biWidth;
765         cmptparm[i].h    = Info_h.biHeight;
766     }
767
768     image = opj_image_create(numcmpts, &cmptparm[0],
769                              (numcmpts == 1U) ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB);
770     if (!image) {
771         fclose(IN);
772         free(pData);
773         return NULL;
774     }
775     if (numcmpts == 4U) {
776         image->comps[3].alpha = 1;
777     }
778
779     /* set image offset and reference grid */
780     image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
781     image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
782     image->x1 = image->x0 + (Info_h.biWidth  - 1U) * (OPJ_UINT32)
783                 parameters->subsampling_dx + 1U;
784     image->y1 = image->y0 + (Info_h.biHeight - 1U) * (OPJ_UINT32)
785                 parameters->subsampling_dy + 1U;
786
787     /* Read the data */
788     if (Info_h.biBitCount == 24 && Info_h.biCompression == 0) { /*RGB */
789         bmp24toimage(pData, stride, image);
790     } else if (Info_h.biBitCount == 8 &&
791                Info_h.biCompression == 0) { /* RGB 8bpp Indexed */
792         bmp8toimage(pData, stride, image, pLUT);
793     } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/
794         bmp8toimage(pData, stride, image, pLUT);
795     } else if (Info_h.biBitCount == 4 && Info_h.biCompression == 2) { /*RLE4*/
796         bmp8toimage(pData, stride, image,
797                     pLUT); /* RLE 4 gets decoded as 8 bits data for now */
798     } else if (Info_h.biBitCount == 32 && Info_h.biCompression == 0) { /* RGBX */
799         bmpmask32toimage(pData, stride, image, 0x00FF0000U, 0x0000FF00U, 0x000000FFU,
800                          0x00000000U);
801     } else if (Info_h.biBitCount == 32 && Info_h.biCompression == 3) { /* bitmask */
802         bmpmask32toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask,
803                          Info_h.biBlueMask, Info_h.biAlphaMask);
804     } else if (Info_h.biBitCount == 16 && Info_h.biCompression == 0) { /* RGBX */
805         bmpmask16toimage(pData, stride, image, 0x7C00U, 0x03E0U, 0x001FU, 0x0000U);
806     } else if (Info_h.biBitCount == 16 && Info_h.biCompression == 3) { /* bitmask */
807         if ((Info_h.biRedMask == 0U) && (Info_h.biGreenMask == 0U) &&
808                 (Info_h.biBlueMask == 0U)) {
809             Info_h.biRedMask   = 0xF800U;
810             Info_h.biGreenMask = 0x07E0U;
811             Info_h.biBlueMask  = 0x001FU;
812         }
813         bmpmask16toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask,
814                          Info_h.biBlueMask, Info_h.biAlphaMask);
815     } else {
816         opj_image_destroy(image);
817         image = NULL;
818         fprintf(stderr,
819                 "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
820                 Info_h.biBitCount);
821     }
822     free(pData);
823     fclose(IN);
824     return image;
825 }
826
827 int imagetobmp(opj_image_t * image, const char *outfile)
828 {
829     int w, h;
830     int i, pad;
831     FILE *fdest = NULL;
832     int adjustR, adjustG, adjustB;
833
834     if (image->comps[0].prec < 8) {
835         fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec);
836         return 1;
837     }
838     if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
839             && image->comps[1].dx == image->comps[2].dx
840             && image->comps[0].dy == image->comps[1].dy
841             && image->comps[1].dy == image->comps[2].dy
842             && image->comps[0].prec == image->comps[1].prec
843             && image->comps[1].prec == image->comps[2].prec) {
844
845         /* -->> -->> -->> -->>
846         24 bits color
847         <<-- <<-- <<-- <<-- */
848
849         fdest = fopen(outfile, "wb");
850         if (!fdest) {
851             fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
852             return 1;
853         }
854
855         w = (int)image->comps[0].w;
856         h = (int)image->comps[0].h;
857
858         fprintf(fdest, "BM");
859
860         /* FILE HEADER */
861         /* ------------- */
862         fprintf(fdest, "%c%c%c%c",
863                 (OPJ_UINT8)(h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
864                 (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
865                 (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
866                 (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
867         fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
868                 ((0) >> 24) & 0xff);
869         fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff,
870                 ((54) >> 24) & 0xff);
871
872         /* INFO HEADER   */
873         /* ------------- */
874         fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
875                 ((40) >> 24) & 0xff);
876         fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)((w) & 0xff),
877                 (OPJ_UINT8)((w) >> 8) & 0xff,
878                 (OPJ_UINT8)((w) >> 16) & 0xff,
879                 (OPJ_UINT8)((w) >> 24) & 0xff);
880         fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)((h) & 0xff),
881                 (OPJ_UINT8)((h) >> 8) & 0xff,
882                 (OPJ_UINT8)((h) >> 16) & 0xff,
883                 (OPJ_UINT8)((h) >> 24) & 0xff);
884         fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
885         fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
886         fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
887                 ((0) >> 24) & 0xff);
888         fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)(3 * h * w + 3 * h * (w % 2)) & 0xff,
889                 (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
890                 (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
891                 (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
892         fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
893                 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
894         fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
895                 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
896         fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
897                 ((0) >> 24) & 0xff);
898         fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
899                 ((0) >> 24) & 0xff);
900
901         if (image->comps[0].prec > 8) {
902             adjustR = (int)image->comps[0].prec - 8;
903             printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
904                    image->comps[0].prec);
905         } else {
906             adjustR = 0;
907         }
908         if (image->comps[1].prec > 8) {
909             adjustG = (int)image->comps[1].prec - 8;
910             printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n",
911                    image->comps[1].prec);
912         } else {
913             adjustG = 0;
914         }
915         if (image->comps[2].prec > 8) {
916             adjustB = (int)image->comps[2].prec - 8;
917             printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n",
918                    image->comps[2].prec);
919         } else {
920             adjustB = 0;
921         }
922
923         for (i = 0; i < w * h; i++) {
924             OPJ_UINT8 rc, gc, bc;
925             int r, g, b;
926
927             r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
928             r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
929             r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
930             if (r > 255) {
931                 r = 255;
932             } else if (r < 0) {
933                 r = 0;
934             }
935             rc = (OPJ_UINT8)r;
936
937             g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
938             g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
939             g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
940             if (g > 255) {
941                 g = 255;
942             } else if (g < 0) {
943                 g = 0;
944             }
945             gc = (OPJ_UINT8)g;
946
947             b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
948             b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
949             b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
950             if (b > 255) {
951                 b = 255;
952             } else if (b < 0) {
953                 b = 0;
954             }
955             bc = (OPJ_UINT8)b;
956
957             fprintf(fdest, "%c%c%c", bc, gc, rc);
958
959             if ((i + 1) % w == 0) {
960                 for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) { /* ADD */
961                     fprintf(fdest, "%c", 0);
962                 }
963             }
964         }
965         fclose(fdest);
966     } else {            /* Gray-scale */
967
968         /* -->> -->> -->> -->>
969         8 bits non code (Gray scale)
970         <<-- <<-- <<-- <<-- */
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         w = (int)image->comps[0].w;
978         h = (int)image->comps[0].h;
979
980         fprintf(fdest, "BM");
981
982         /* FILE HEADER */
983         /* ------------- */
984         fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)(h * w + 54 + 1024 + h * (w % 2)) & 0xff,
985                 (OPJ_UINT8)((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
986                 (OPJ_UINT8)((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
987                 (OPJ_UINT8)((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
988         fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
989                 ((0) >> 24) & 0xff);
990         fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
991                 ((54 + 1024) >> 16) & 0xff,
992                 ((54 + 1024) >> 24) & 0xff);
993
994         /* INFO HEADER */
995         /* ------------- */
996         fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff,
997                 ((40) >> 24) & 0xff);
998         fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)((w) & 0xff),
999                 (OPJ_UINT8)((w) >> 8) & 0xff,
1000                 (OPJ_UINT8)((w) >> 16) & 0xff,
1001                 (OPJ_UINT8)((w) >> 24) & 0xff);
1002         fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)((h) & 0xff),
1003                 (OPJ_UINT8)((h) >> 8) & 0xff,
1004                 (OPJ_UINT8)((h) >> 16) & 0xff,
1005                 (OPJ_UINT8)((h) >> 24) & 0xff);
1006         fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
1007         fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
1008         fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
1009                 ((0) >> 24) & 0xff);
1010         fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)(h * w + h * (w % 2)) & 0xff,
1011                 (OPJ_UINT8)((h * w + h * (w % 2)) >> 8) &  0xff,
1012                 (OPJ_UINT8)((h * w + h * (w % 2)) >> 16) & 0xff,
1013                 (OPJ_UINT8)((h * w + h * (w % 2)) >> 24) & 0xff);
1014         fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1015                 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1016         fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
1017                 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
1018         fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1019                 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1020         fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
1021                 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
1022
1023         if (image->comps[0].prec > 8) {
1024             adjustR = (int)image->comps[0].prec - 8;
1025             printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
1026                    image->comps[0].prec);
1027         } else {
1028             adjustR = 0;
1029         }
1030
1031         for (i = 0; i < 256; i++) {
1032             fprintf(fdest, "%c%c%c%c", i, i, i, 0);
1033         }
1034
1035         for (i = 0; i < w * h; i++) {
1036             int r;
1037
1038             r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
1039             r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1040             r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
1041             if (r > 255) {
1042                 r = 255;
1043             } else if (r < 0) {
1044                 r = 0;
1045             }
1046
1047             fprintf(fdest, "%c", (OPJ_UINT8)r);
1048
1049             if ((i + 1) % w == 0) {
1050                 for (pad = (w % 4) ? (4 - w % 4) : 0; pad > 0; pad--) { /* ADD */
1051                     fprintf(fdest, "%c", 0);
1052                 }
1053             }
1054         }
1055         fclose(fdest);
1056     }
1057
1058     return 0;
1059 }