summaryrefslogtreecommitdiff
path: root/tools/convpic/bmp.c
blob: 08293022fdd6c8b1e4f1006f13821058d6e539bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
 *  GraphLCD plugin for the Video Disk Recorder
 *
 *  bmp.c  -  bmp logo class
 *
 *  (C) 2004 Andreas Brachold <vdr04 AT deltab de>
 *  (C) 2001-2004 Carsten Siebholz <c.siebholz AT t-online de>
 **/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program;                                              *
 *   if not, write to the Free Software Foundation, Inc.,                  *
 *   59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *
 *                                                                         *
 ***************************************************************************/

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <cstring>
#include <cstdlib>

#include <glcdgraphics/bitmap.h>
#include <glcdgraphics/image.h>

#include "bmp.h"


#pragma pack(1)
typedef struct BMPH {
    uint16_t  bmpIdentifier;
    uint32_t  bmpFileSize;
    uint32_t  bmpReserved;
    uint32_t  bmpBitmapDataOffset;
    uint32_t  bmpBitmapHeaderSize;
    uint32_t  bmpWidth;
    uint32_t  bmpHeight;
    uint16_t  bmpPlanes;
    uint16_t  bmpBitsPerPixel;
    uint32_t  bmpCompression;
    uint32_t  bmpBitmapDataSize;
    uint32_t  bmpHResolution;
    uint32_t  bmpVResolution;
    uint32_t  bmpColors;
    uint32_t  bmpImportantColors;
} BMPHEADER;  // 54 bytes

typedef struct RGBQ {
    uint8_t  rgbBlue;
    uint8_t  rgbGreen;
    uint8_t  rgbRed;
    uint8_t  rgbReserved;
} RGBQUAD;    // 4 bytes
#pragma pack()


uint8_t bitmask[8]  = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
uint8_t bitmaskl[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
uint8_t bitmaskr[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};

cBMPFile::cBMPFile()
{
}

cBMPFile::~cBMPFile()
{
}

bool cBMPFile::Load(GLCD::cImage & image, const std::string & fileName)
{
    FILE         *fIN;
    BMPHEADER     bmpHeader;
    RGBQUAD      *pPalette;
    char         *pByte;
    char          Dummy;
    long          iNumColors;
    long          iSize;
    uint32_t      x, y;
    uint16_t      iRead;
    uint8_t *  bitmap = NULL;
    bool  bInvert = false;

    if (fileName.length() > 0)
    {
        fIN = fopen(fileName.c_str(), "rb");
        if (fIN)
        {
            if (fread(&bmpHeader, sizeof(BMPHEADER), 1, fIN)!=1)
            {
                fclose(fIN);
                return false;
            }

            // check for Windows BMP
            if (bmpHeader.bmpBitmapHeaderSize != 0x00000028 )
            {
                fprintf(stderr, "ERROR: only Windows BMP images are allowed.\n");
                fclose(fIN);
                return false;
            }

            // check for 2 color
            iNumColors = (1 << bmpHeader.bmpBitsPerPixel);
            if (iNumColors != 2)
            {
                fprintf(stderr, "ERROR: the image has %ld colors, but only images with 2 colors are allowed.\n", iNumColors);
                fclose(fIN);
                return false;
            }

            iSize = bmpHeader.bmpHeight * bmpHeader.bmpWidth;

            pPalette = (RGBQUAD *) malloc( iNumColors*sizeof(RGBQUAD));
            if (!pPalette)
            {
                fprintf(stderr, "ERROR: cannot allocate memory\n");
                fclose(fIN);
                return false;
            }

            if (fread( pPalette, iNumColors*sizeof(RGBQUAD), 1, fIN)!=1)
            {
                free(pPalette);
                fclose(fIN);
                return false;
            }

            // check colors
            if (pPalette->rgbBlue+pPalette->rgbGreen+pPalette->rgbRed <
                    (pPalette+1)->rgbBlue+(pPalette+1)->rgbGreen+(pPalette+1)->rgbRed)
            {
                // index 0 represents 'black', index 1 'white'
                bInvert = !bInvert;
            }
            else
            {
                // index 0 represents 'white', index 1 'black'
            }

            if (fseek(fIN, bmpHeader.bmpBitmapDataOffset, SEEK_SET)==EOF)
            {
                free(pPalette);
                fclose(fIN);
                return false;
            }

            switch (bmpHeader.bmpCompression)
            {
                case 0: // BI_RGB       no compression
                    image.Clear();
                    image.SetWidth(bmpHeader.bmpWidth);
                    image.SetHeight(bmpHeader.bmpHeight);
                    image.SetDelay(100);
                    bitmap = new unsigned char[bmpHeader.bmpHeight * ((bmpHeader.bmpWidth + 7) / 8)];
                    if (!bitmap)
                    {
                        fprintf(stderr, "ERROR: cannot allocate memory\n");
                        free(pPalette);
                        fclose(fIN);
                        image.Clear();
                        return false;
                    }

                    for (y = bmpHeader.bmpHeight; y > 0; y--)
                    {
                        pByte = (char*)bitmap + (y-1)*((bmpHeader.bmpWidth+7)/8);
                        iRead = 0;
                        for (x = 0; x < bmpHeader.bmpWidth / 8; x++)
                        {
                            if (fread(pByte, sizeof(char), 1, fIN) != 1)
                            {
                                delete[] bitmap;
                                free(pPalette);
                                fclose(fIN);
                                image.Clear();
                                return false;
                            }
                            iRead++;
                            if (bInvert)
                                *pByte = *pByte ^ 0xff;
                            pByte++;
                        }

                        if (bmpHeader.bmpWidth % 8)
                        {
                            if (fread(pByte, sizeof(char), 1, fIN) != 1)
                            {
                                delete [] bitmap;
                                free(pPalette);
                                fclose(fIN);
                                image.Clear();
                                return false;
                            }
                            iRead++;
                            if (bInvert)
                                *pByte = *pByte^0xff;
                            *pByte = *pByte & bitmaskl[bmpHeader.bmpWidth%8];
                            pByte++;
                        }

                        // Scan line must be 4-byte-alligned
                        while (iRead % 4)
                        {
                            if (fread(&Dummy, sizeof(char), 1, fIN) != 1)
                            {
                                delete [] bitmap;
                                free(pPalette);
                                fclose(fIN);
                                image.Clear();
                                return false;
                            }
                            iRead++;
                        }
                    }
                    image.AddBitmap(new GLCD::cBitmap(bmpHeader.bmpWidth, bmpHeader.bmpHeight, bitmap));
                    break;
                case 1: // BI_RLE4      RLE 4bit/pixel
                case 2: // BI_RLE8      RLE 8bit/pixel
                case 3: // BI_BITFIELDS
                default:
                    fprintf(stderr, "ERROR: only uncompressed RGB images are allowed.\n");

                    free(pPalette);
                    fclose(fIN);
                    return false;
            }
            fclose(fIN);
        }
        else
        {
            fprintf(stderr, "ERROR: cannot open picture %s\n", fileName.c_str());
        }
    }
    else
    {
        fprintf(stderr, "ERROR: no FileName given!\n");
    }
    return true;
}

bool cBMPFile::Save(const GLCD::cBitmap * bitmap, const std::string & fileName)
{
    FILE         *fOut;
    BMPHEADER     bmpHeader;
    RGBQUAD       bmpColor1, bmpColor2;
    uint32_t      dBDO, dBDSx, dBDS;
    char         *pByte;
    char          Dummy = 0x00;
    uint32_t      x, y;
    uint16_t      iWrote;
    const uint8_t * bmpdata = bitmap->Data();

    if (bitmap
        && bitmap->Width() > 0
        && bitmap->Height() > 0)
    {
        memset(&bmpHeader, 0, sizeof(BMPHEADER));

        dBDO = sizeof(BMPHEADER)+2*sizeof(RGBQUAD);
        dBDSx = ((bitmap->Width() + 7) / 8 + 3) & 0xfffffffc;
        dBDS = dBDSx * bitmap->Height();

        bmpHeader.bmpIdentifier       = 0x4d42; // "BM"
        bmpHeader.bmpFileSize         = dBDO + dBDS;
        bmpHeader.bmpBitmapDataOffset = dBDO;
        bmpHeader.bmpBitmapHeaderSize = 0x28;
        bmpHeader.bmpWidth            = bitmap->Width();
        bmpHeader.bmpHeight           = bitmap->Height();
        bmpHeader.bmpPlanes           = 0x01;
        bmpHeader.bmpBitsPerPixel     = 0x01;
        bmpHeader.bmpCompression      = 0x00;
        bmpHeader.bmpBitmapDataSize   = dBDS;
        bmpHeader.bmpHResolution      = 0xb13;  // 72dpi
        bmpHeader.bmpVResolution      = 0xb13;  // 72dpi
        bmpHeader.bmpColors           = 0x02;
        bmpHeader.bmpImportantColors  = 0x02;

        bmpColor1.rgbBlue     = 0x00;
        bmpColor1.rgbGreen    = 0x00;
        bmpColor1.rgbRed      = 0x00;
        bmpColor1.rgbReserved = 0x00;
        bmpColor2.rgbBlue     = 0xff;
        bmpColor2.rgbGreen    = 0xff;
        bmpColor2.rgbRed      = 0xff;
        bmpColor2.rgbReserved = 0x00;


        fOut = fopen(fileName.c_str(), "wb");
        if (!fOut)
        {
            fprintf(stderr,"Cannot create file: %s\n", fileName.c_str());
            return false;
        }
        fwrite(&bmpHeader, sizeof(BMPHEADER), 1, fOut);
        fwrite(&bmpColor1, sizeof(RGBQUAD), 1, fOut);
        fwrite(&bmpColor2, sizeof(RGBQUAD), 1, fOut);

        for (y=bitmap->Height(); y>0; y--)
        {
            pByte = (char*)bmpdata + (y-1)*((bitmap->Width()+7)/8);
            iWrote = 0;
            for (x=0; x<(uint32_t) bitmap->Width()/8; x++)
            {
                *pByte = *pByte^0xff;
                if (fwrite(pByte, sizeof(char), 1, fOut)!=1)
                {
                    fclose(fOut);
                    return false;
                }
                iWrote++;
                pByte++;
            }
            // Scan line must be 4-byte-alligned
            while (iWrote%4)
            {
                if (fwrite(&Dummy, sizeof(char), 1, fOut)!=1)
                {
                    fclose(fOut);
                    return 3;
                }
                iWrote++;
            }
        }
        fclose(fOut);
    }
    return true;
}

bool cBMPFile::Save(GLCD::cImage & image, const std::string & fileName)
{
    const GLCD::cBitmap * bitmap;

    if (image.Count() == 1)
    {
        bitmap = image.GetBitmap(0);
        if (bitmap)
        {
            if (!Save(bitmap, fileName))
            {
                return false;
            }
        }
    }
    else
    {
        uint16_t i;
        char tmpStr[256];

        for (i = 0; i < image.Count(); i++)
        {
            sprintf(tmpStr, "%.248s.%05d", fileName.c_str(), i);
            bitmap = image.GetBitmap(i);
            if (bitmap)
            {
                if (!Save(bitmap, tmpStr))
                {
                    return false;
                }
            }
        }
    }
    return true;
}