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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2002 by Linus Nielsen Feltzing
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
/*********************************************************************
*
* Converts BMP files to Rockbox bitmap format
*
* 1999-05-03 Linus Nielsen Feltzing
*
**********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "debug.h"
#include "file.h"
#if 0
#ifdef __GNUC__
#define STRUCT_PACKED __attribute__((packed))
#else
#define STRUCT_PACKED
#pragma pack (push, 2)
#endif
struct Fileheader
{
unsigned short Type; /* signature - 'BM' */
unsigned long Size; /* file size in bytes */
unsigned short Reserved1; /* 0 */
unsigned short Reserved2; /* 0 */
unsigned long OffBits; /* offset to bitmap */
unsigned long StructSize; /* size of this struct (40) */
unsigned long Width; /* bmap width in pixels */
unsigned long Height; /* bmap height in pixels */
unsigned short Planes; /* num planes - always 1 */
unsigned short BitCount; /* bits per pixel */
unsigned long Compression; /* compression flag */
unsigned long SizeImage; /* image size in bytes */
long XPelsPerMeter; /* horz resolution */
long YPelsPerMeter; /* vert resolution */
unsigned long ClrUsed; /* 0 -> color table size */
unsigned long ClrImportant; /* important color count */
} STRUCT_PACKED;
struct RGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
} STRUCT_PACKED;
#ifdef LITTLE_ENDIAN
#define readshort(x) x
#define readlong(x) x
#else
#define readshort(x) (((x&0xff00)>>8)|((x&0x00ff)<<8))
#define readlong(x) (((x&0xff000000)>>24)| \
((x&0x00ff0000)>>8) | \
((x&0x0000ff00)<<8) | \
((x&0x000000ff)<<24))
#endif
/*********************************************************************
* read_bmp_file()
*
* Reads a 8bit BMP file and puts the data in a 1-pixel-per-byte
* array. Returns 0 on success.
*
**********************************************/
int read_bmp_file(char* filename,
int *get_width, /* in pixels */
int *get_height, /* in pixels */
char *bitmap)
{
struct Fileheader fh;
struct RGBQUAD palette[2]; /* two colors only */
unsigned int bitmap_width, bitmap_height;
long PaddedWidth;
int background;
int fd = open(filename, O_RDONLY);
long size;
unsigned int row, col;
int l;
unsigned char *bmp;
int width;
if(fd == -1)
{
debugf("error - can't open '%s'\n", filename);
return 1;
}
else
{
if(read(fd, &fh, sizeof(struct Fileheader)) !=
sizeof(struct Fileheader))
{
debugf("error - can't Read Fileheader Stucture\n");
close(fd);
return 2;
}
/* Exit if not monochrome */
if(readshort(fh.BitCount) > 8)
{
debugf("error - Bitmap must be less than 8, got %d\n",
readshort(fh.BitCount));
close(fd);
return 2;
}
/* Exit if too wide */
if(readlong(fh.Width) > 112)
{
debugf("error - Bitmap is too wide (%d pixels, max is 112)\n",
readlong(fh.Width));
close(fd);
return 3;
}
debugf("Bitmap is %d pixels wide\n", readlong(fh.Width));
/* Exit if too high */
if(readlong(fh.Height) > 64)
{
debugf("error - Bitmap is too high (%d pixels, max is 64)\n",
readlong(fh.Height));
close(fd);
return 4;
}
debugf("Bitmap is %d pixels heigh\n", readlong(fh.Height));
for(l=0;l < 2;l++)
{
if(read(fd, &palette[l],sizeof(struct RGBQUAD)) !=
sizeof(struct RGBQUAD))
{
debugf("error - Can't read bitmap's color palette\n");
close(fd);
return 5;
}
}
/* pass the other palettes */
lseek(fd, 254*sizeof(struct RGBQUAD), SEEK_CUR);
/* Try to guess the foreground and background colors.
We assume that the foreground color is the darkest. */
if(((int)palette[0].rgbRed +
(int)palette[0].rgbGreen +
(int)palette[0].rgbBlue) >
((int)palette[1].rgbRed +
(int)palette[1].rgbGreen +
(int)palette[1].rgbBlue))
{
background = 0;
}
else
{
background = 1;
}
/* width = readlong(fh.Width)*readshort(fh.BitCount); */
width = readlong(fh.Width);
/* PaddedWidth = ((width+31)&(~0x1f))/8; */
PaddedWidth = ((width+3)&(~0x3));
size = PaddedWidth*readlong(fh.Height);
bmp = (unsigned char *)malloc(size);
if(bmp == NULL)
{
debugf("error - Out of memory\n");
close(fd);
return 6;
}
else
{
if(read(fd, (unsigned char*)bmp,(long)size) != size) {
debugf("error - Can't read image\n");
close(fd);
return 7;
}
}
bitmap_height = readlong(fh.Height);
bitmap_width = readlong(fh.Width);
*get_width = bitmap_width;
*get_height = bitmap_height;
/* Now convert the bitmap into an array with 1 byte per pixel,
exactly the size of the image */
for(row = 0;row < bitmap_height;row++) {
for(col = 0;col < bitmap_width;col++) {
if(bmp[(bitmap_height-1 -row) * PaddedWidth + col]) {
bitmap[ (row/8) * bitmap_width + col ] &= ~ (1<<(row&7));
}
else {
bitmap[ (row/8) * bitmap_width + col ] |= 1<<(row&7);
}
}
}
free(bmp);
}
close(fd);
return 0; /* success */
}
#endif /* 0 */
|