diff options
-rw-r--r-- | tools/bmp2rb.c | 221 |
1 files changed, 131 insertions, 90 deletions
diff --git a/tools/bmp2rb.c b/tools/bmp2rb.c index a2a983b5d9..d527221840 100644 --- a/tools/bmp2rb.c +++ b/tools/bmp2rb.c | |||
@@ -94,93 +94,102 @@ int read_bmp_file(char* filename, | |||
94 | int *get_height, /* in pixels */ | 94 | int *get_height, /* in pixels */ |
95 | char **bitmap) | 95 | char **bitmap) |
96 | { | 96 | { |
97 | struct Fileheader fh; | 97 | struct Fileheader fh; |
98 | struct RGBQUAD palette[2]; /* two colors only */ | 98 | struct RGBQUAD palette[2]; /* two colors only */ |
99 | 99 | ||
100 | unsigned int bitmap_width, bitmap_height; | 100 | unsigned int bitmap_width, bitmap_height; |
101 | 101 | ||
102 | long PaddedWidth; | 102 | long PaddedWidth; |
103 | int background; | 103 | int background; |
104 | int fd = open(filename, O_RDONLY); | 104 | int fd = open(filename, O_RDONLY); |
105 | long size; | 105 | long size; |
106 | unsigned int row, col; | 106 | unsigned int row, col; |
107 | int l; | 107 | int l; |
108 | unsigned char *bmp; | 108 | unsigned char *bmp; |
109 | int width; | 109 | int width; |
110 | 110 | int depth; | |
111 | if(fd == -1) | 111 | |
112 | { | 112 | if(fd == -1) |
113 | debugf("error - can't open '%s'\n", filename); | 113 | { |
114 | return 1; | 114 | debugf("error - can't open '%s'\n", filename); |
115 | } | 115 | return 1; |
116 | else | 116 | } |
117 | else | ||
117 | { | 118 | { |
118 | if(read(fd, &fh, sizeof(struct Fileheader)) != | 119 | if(read(fd, &fh, sizeof(struct Fileheader)) != |
119 | sizeof(struct Fileheader)) | 120 | sizeof(struct Fileheader)) |
120 | { | 121 | { |
121 | debugf("error - can't Read Fileheader Stucture\n"); | 122 | debugf("error - can't Read Fileheader Stucture\n"); |
122 | close(fd); | 123 | close(fd); |
123 | return 2; | 124 | return 2; |
124 | } | 125 | } |
125 | 126 | ||
126 | /* Exit if not monochrome */ | 127 | /* Exit if more than 8 bits */ |
127 | if(readshort(fh.BitCount) > 8) | 128 | depth = readshort(fh.BitCount); |
128 | { | 129 | if(depth > 8) |
129 | debugf("error - Bitmap must be less than 8, got %d\n", | 130 | { |
130 | readshort(fh.BitCount)); | 131 | debugf("error - Bitmap uses more than 8 bit depth, got %d\n", |
131 | close(fd); | 132 | depth); |
132 | return 2; | 133 | close(fd); |
133 | } | 134 | return 2; |
134 | 135 | } | |
135 | /* Exit if too wide */ | 136 | |
136 | if(readlong(fh.Width) > 112) | 137 | /* Exit if too wide */ |
137 | { | 138 | if(readlong(fh.Width) > 112) |
138 | debugf("error - Bitmap is too wide (%d pixels, max is 112)\n", | 139 | { |
139 | readlong(fh.Width)); | 140 | debugf("error - Bitmap is too wide (%d pixels, max is 112)\n", |
140 | close(fd); | 141 | readlong(fh.Width)); |
141 | return 3; | 142 | close(fd); |
142 | } | 143 | return 3; |
143 | 144 | } | |
144 | /* Exit if too high */ | 145 | |
145 | if(readlong(fh.Height) > 64) | 146 | /* Exit if too high */ |
146 | { | 147 | if(readlong(fh.Height) > 64) |
147 | debugf("error - Bitmap is too high (%d pixels, max is 64)\n", | 148 | { |
148 | readlong(fh.Height)); | 149 | debugf("error - Bitmap is too high (%d pixels, max is 64)\n", |
149 | close(fd); | 150 | readlong(fh.Height)); |
150 | return 4; | 151 | close(fd); |
151 | } | 152 | return 4; |
152 | 153 | } | |
153 | for(l=0;l < 2;l++) | 154 | |
154 | { | 155 | for(l=0;l < 2;l++) |
155 | if(read(fd, &palette[l],sizeof(struct RGBQUAD)) != | 156 | { |
156 | sizeof(struct RGBQUAD)) | 157 | if(read(fd, &palette[l],sizeof(struct RGBQUAD)) != |
157 | { | 158 | sizeof(struct RGBQUAD)) |
158 | debugf("error - Can't read bitmap's color palette\n"); | 159 | { |
159 | close(fd); | 160 | debugf("error - Can't read bitmap's color palette\n"); |
160 | return 5; | 161 | close(fd); |
161 | } | 162 | return 5; |
162 | } | 163 | } |
163 | /* pass the other palettes */ | 164 | } |
164 | lseek(fd, 254*sizeof(struct RGBQUAD), SEEK_CUR); | 165 | if(depth == 8 ) { |
165 | 166 | /* pass the other palettes */ | |
166 | /* Try to guess the foreground and background colors. | 167 | lseek(fd, 254*sizeof(struct RGBQUAD), SEEK_CUR); |
167 | We assume that the foreground color is the darkest. */ | 168 | } |
168 | if(((int)palette[0].rgbRed + | 169 | |
169 | (int)palette[0].rgbGreen + | 170 | /* Try to guess the foreground and background colors. |
170 | (int)palette[0].rgbBlue) > | 171 | We assume that the foreground color is the darkest. */ |
171 | ((int)palette[1].rgbRed + | 172 | if(((int)palette[0].rgbRed + |
172 | (int)palette[1].rgbGreen + | 173 | (int)palette[0].rgbGreen + |
173 | (int)palette[1].rgbBlue)) | 174 | (int)palette[0].rgbBlue) > |
174 | { | 175 | ((int)palette[1].rgbRed + |
176 | (int)palette[1].rgbGreen + | ||
177 | (int)palette[1].rgbBlue)) | ||
178 | { | ||
175 | background = 0; | 179 | background = 0; |
176 | } | 180 | } |
177 | else | 181 | else |
178 | { | 182 | { |
179 | background = 1; | 183 | background = 1; |
180 | } | 184 | } |
181 | 185 | ||
182 | width = readlong(fh.Width); | 186 | width = readlong(fh.Width); |
183 | PaddedWidth = ((width+3)&(~0x3)); | 187 | |
188 | if(depth == 8) | ||
189 | PaddedWidth = ((width+3)&(~0x3)); /* aligned 4-bytes boundaries */ | ||
190 | else | ||
191 | PaddedWidth = ((width+31)&(~0x1f))/8; | ||
192 | |||
184 | size = PaddedWidth*readlong(fh.Height); | 193 | size = PaddedWidth*readlong(fh.Height); |
185 | 194 | ||
186 | bmp = (unsigned char *)malloc(size); | 195 | bmp = (unsigned char *)malloc(size); |
@@ -207,18 +216,50 @@ int read_bmp_file(char* filename, | |||
207 | *get_width = bitmap_width; | 216 | *get_width = bitmap_width; |
208 | *get_height = bitmap_height; | 217 | *get_height = bitmap_height; |
209 | 218 | ||
210 | /* Now convert the bitmap into an array with 1 byte per pixel, | 219 | if(depth == 8) |
211 | exactly the size of the image */ | 220 | { |
212 | 221 | /* Now convert the bitmap into an array with 1 byte per pixel, | |
213 | for(row = 0;row < bitmap_height;row++) { | 222 | exactly the size of the image */ |
214 | for(col = 0;col < bitmap_width;col++) { | 223 | for(row = 0;row < bitmap_height;row++) { |
215 | if(bmp[(bitmap_height-1 -row) * PaddedWidth + col]) { | 224 | for(col = 0;col < bitmap_width;col++) { |
216 | (*bitmap)[ (row/8) * bitmap_width + col ] &= ~ (1<<(row&7)); | 225 | if(bmp[(bitmap_height-1 -row) * PaddedWidth + col]) { |
226 | (*bitmap)[ (row/8) * bitmap_width + col ] &= | ||
227 | ~ (1<<(row&7)); | ||
228 | } | ||
229 | else { | ||
230 | (*bitmap)[ (row/8) * bitmap_width + col ] |= | ||
231 | 1<<(row&7); | ||
232 | } | ||
233 | } | ||
217 | } | 234 | } |
218 | else { | 235 | } |
219 | (*bitmap)[ (row/8) * bitmap_width + col ] |= 1<<(row&7); | 236 | else |
237 | { | ||
238 | int bit; | ||
239 | int byte; | ||
240 | /* monocrome BMP conversion uses 8 pixels per byte */ | ||
241 | for(row = 0; row < bitmap_height; row++) { | ||
242 | bit = 7; | ||
243 | byte = 0; | ||
244 | for(col = 0;col < bitmap_width;col++) { | ||
245 | if((bmp[(bitmap_height - row - 1) * PaddedWidth + byte] & | ||
246 | (1 << bit))) { | ||
247 | (*bitmap)[(row/8) * bitmap_width + col ] &= | ||
248 | ~(1<<(row&7)); | ||
249 | } | ||
250 | else { | ||
251 | (*bitmap)[(row/8) * bitmap_width + col ] |= | ||
252 | 1<<(row&7); | ||
253 | } | ||
254 | if(bit) { | ||
255 | bit--; | ||
256 | } | ||
257 | else { | ||
258 | bit = 7; | ||
259 | byte++; | ||
260 | } | ||
261 | } | ||
220 | } | 262 | } |
221 | } | ||
222 | } | 263 | } |
223 | 264 | ||
224 | free(bmp); | 265 | free(bmp); |