diff options
Diffstat (limited to 'tools/bmp2rb.c')
-rw-r--r-- | tools/bmp2rb.c | 771 |
1 files changed, 429 insertions, 342 deletions
diff --git a/tools/bmp2rb.c b/tools/bmp2rb.c index f874fed677..52941fca15 100644 --- a/tools/bmp2rb.c +++ b/tools/bmp2rb.c | |||
@@ -16,13 +16,17 @@ | |||
16 | * KIND, either express or implied. | 16 | * KIND, either express or implied. |
17 | * | 17 | * |
18 | ****************************************************************************/ | 18 | ****************************************************************************/ |
19 | /********************************************************************* | 19 | /**************************************************************************** |
20 | * | 20 | * |
21 | * Converts BMP files to Rockbox bitmap format | 21 | * Converts BMP files to Rockbox bitmap format |
22 | * | 22 | * |
23 | * 1999-05-03 Linus Nielsen Feltzing | 23 | * 1999-05-03 Linus Nielsen Feltzing |
24 | * | 24 | * |
25 | **********************************************/ | 25 | * 2005-07-06 Jens Arnold |
26 | * added reading of 4, 16, 24 and 32 bit bmps | ||
27 | * added 2 new target formats (playergfx and iriver 4-grey) | ||
28 | * | ||
29 | ****************************************************************************/ | ||
26 | 30 | ||
27 | #include <stdio.h> | 31 | #include <stdio.h> |
28 | #include <stdlib.h> | 32 | #include <stdlib.h> |
@@ -44,33 +48,32 @@ | |||
44 | 48 | ||
45 | struct Fileheader | 49 | struct Fileheader |
46 | { | 50 | { |
47 | unsigned short Type; /* signature - 'BM' */ | 51 | unsigned short Type; /* signature - 'BM' */ |
48 | unsigned long Size; /* file size in bytes */ | 52 | unsigned long Size; /* file size in bytes */ |
49 | unsigned short Reserved1; /* 0 */ | 53 | unsigned short Reserved1; /* 0 */ |
50 | unsigned short Reserved2; /* 0 */ | 54 | unsigned short Reserved2; /* 0 */ |
51 | unsigned long OffBits; /* offset to bitmap */ | 55 | unsigned long OffBits; /* offset to bitmap */ |
52 | unsigned long StructSize; /* size of this struct (40) */ | 56 | unsigned long StructSize; /* size of this struct (40) */ |
53 | unsigned long Width; /* bmap width in pixels */ | 57 | unsigned long Width; /* bmap width in pixels */ |
54 | unsigned long Height; /* bmap height in pixels */ | 58 | unsigned long Height; /* bmap height in pixels */ |
55 | unsigned short Planes; /* num planes - always 1 */ | 59 | unsigned short Planes; /* num planes - always 1 */ |
56 | unsigned short BitCount; /* bits per pixel */ | 60 | unsigned short BitCount; /* bits per pixel */ |
57 | unsigned long Compression; /* compression flag */ | 61 | unsigned long Compression; /* compression flag */ |
58 | unsigned long SizeImage; /* image size in bytes */ | 62 | unsigned long SizeImage; /* image size in bytes */ |
59 | long XPelsPerMeter; /* horz resolution */ | 63 | long XPelsPerMeter; /* horz resolution */ |
60 | long YPelsPerMeter; /* vert resolution */ | 64 | long YPelsPerMeter; /* vert resolution */ |
61 | unsigned long ClrUsed; /* 0 -> color table size */ | 65 | unsigned long ClrUsed; /* 0 -> color table size */ |
62 | unsigned long ClrImportant; /* important color count */ | 66 | unsigned long ClrImportant; /* important color count */ |
63 | } STRUCT_PACKED; | 67 | } STRUCT_PACKED; |
64 | 68 | ||
65 | struct RGBQUAD | 69 | struct RGBQUAD |
66 | { | 70 | { |
67 | unsigned char rgbBlue; | 71 | unsigned char rgbBlue; |
68 | unsigned char rgbGreen; | 72 | unsigned char rgbGreen; |
69 | unsigned char rgbRed; | 73 | unsigned char rgbRed; |
70 | unsigned char rgbReserved; | 74 | unsigned char rgbReserved; |
71 | } STRUCT_PACKED; | 75 | } STRUCT_PACKED; |
72 | 76 | ||
73 | |||
74 | short readshort(void* value) | 77 | short readshort(void* value) |
75 | { | 78 | { |
76 | unsigned char* bytes = (unsigned char*) value; | 79 | unsigned char* bytes = (unsigned char*) value; |
@@ -83,366 +86,450 @@ int readlong(void* value) | |||
83 | return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); | 86 | return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); |
84 | } | 87 | } |
85 | 88 | ||
86 | /********************************************************************* | 89 | unsigned char brightness(unsigned char red, unsigned char green, |
90 | unsigned char blue) | ||
91 | { | ||
92 | return (3 * (unsigned int)red + 6 * (unsigned int)green | ||
93 | + (unsigned int)blue) / 10; | ||
94 | } | ||
95 | |||
96 | /**************************************************************************** | ||
87 | * read_bmp_file() | 97 | * read_bmp_file() |
88 | * | 98 | * |
89 | * Reads a 8bit BMP file and puts the data in a 1-pixel-per-byte | 99 | * Reads an uncompressed BMP file and puts the data in a 1-pixel-per-byte |
90 | * array. Returns 0 on success. | 100 | * array, sorted by brightness. Returns 0 on success. |
91 | * | 101 | * |
92 | *********************************************************************/ | 102 | ***************************************************************************/ |
103 | |||
93 | int read_bmp_file(char* filename, | 104 | int read_bmp_file(char* filename, |
94 | int *get_width, /* in pixels */ | 105 | long *get_width, /* in pixels */ |
95 | int *get_height, /* in pixels */ | 106 | long *get_height, /* in pixels */ |
96 | char **bitmap) | 107 | unsigned char **bitmap) |
97 | { | 108 | { |
98 | struct Fileheader fh; | 109 | struct Fileheader fh; |
99 | struct RGBQUAD palette[2]; /* two colors only */ | 110 | struct RGBQUAD palette[256]; |
111 | unsigned char bright[256]; | ||
100 | 112 | ||
101 | unsigned int bitmap_width, bitmap_height; | ||
102 | |||
103 | long PaddedWidth; | ||
104 | int background; | ||
105 | int fd = open(filename, O_RDONLY); | 113 | int fd = open(filename, O_RDONLY); |
106 | long size; | 114 | unsigned short data; |
107 | long allocsize; | ||
108 | unsigned int row, col; | ||
109 | int l; | ||
110 | unsigned char *bmp; | 115 | unsigned char *bmp; |
111 | int width; | 116 | long width, height; |
112 | int height; | 117 | long padded_width; |
118 | long size; | ||
119 | long row, col, i; | ||
120 | long numcolors, compression; | ||
113 | int depth; | 121 | int depth; |
114 | 122 | ||
115 | if(fd == -1) | 123 | if (fd == -1) |
116 | { | 124 | { |
117 | debugf("error - can't open '%s'\n", filename); | 125 | debugf("error - can't open '%s'\n", filename); |
118 | return 1; | 126 | return 1; |
119 | } | 127 | } |
120 | else | 128 | if (read(fd, &fh, sizeof(struct Fileheader)) != |
121 | { | ||
122 | if(read(fd, &fh, sizeof(struct Fileheader)) != | ||
123 | sizeof(struct Fileheader)) | 129 | sizeof(struct Fileheader)) |
124 | { | 130 | { |
125 | debugf("error - can't Read Fileheader Stucture\n"); | 131 | debugf("error - can't Read Fileheader Stucture\n"); |
126 | close(fd); | 132 | close(fd); |
127 | return 2; | 133 | return 2; |
128 | } | 134 | } |
129 | 135 | ||
130 | /* Exit if more than 8 bits */ | 136 | compression = readlong(&fh.Compression); |
131 | depth = readshort(&fh.BitCount); | 137 | |
132 | if(depth > 8) | 138 | if (compression != 0) |
133 | { | 139 | { |
134 | debugf("error - Bitmap uses more than 8 bit depth, got %d\n", | 140 | debugf("error - Unsupported compression %ld\n", compression); |
135 | depth); | 141 | close(fd); |
136 | close(fd); | 142 | return 3; |
137 | return 2; | 143 | } |
138 | } | 144 | |
139 | 145 | depth = readshort(&fh.BitCount); | |
140 | /* Exit if too wide */ | 146 | |
141 | if(readlong(&fh.Width) > 160) | 147 | if (depth <= 8) |
142 | { | 148 | { |
143 | debugf("error - Bitmap is too wide for iRiver models (%d pixels, max is 160)\n", | 149 | numcolors = readlong(&fh.ClrUsed); |
144 | readlong(&fh.Width)); | 150 | if (numcolors == 0) |
145 | return 3; | 151 | numcolors = 1 << depth; |
146 | } | 152 | |
147 | if(readlong(&fh.Width) > 112) | 153 | if (read(fd, &palette[0], numcolors * sizeof(struct RGBQUAD)) |
148 | { | 154 | != numcolors * sizeof(struct RGBQUAD)) |
149 | debugf("info - Bitmap is too wide for Archos models (%d pixels, max is 112)\n", | 155 | { |
150 | readlong(&fh.Width)); | 156 | debugf("error - Can't read bitmap's color palette\n"); |
151 | } | 157 | close(fd); |
152 | 158 | return 4; | |
153 | /* Exit if too high */ | 159 | } |
154 | if(readlong(&fh.Height) > 128) | 160 | |
155 | { | 161 | /* Precalculate weighted brightnesses for the palette */ |
156 | debugf("error - Bitmap is too high for iRiver models (%d pixels, max is 128)\n", | 162 | for (i = 0; i < numcolors; i++) |
157 | readlong(&fh.Height)); | 163 | bright[i] = brightness(palette[i].rgbRed, palette[i].rgbGreen, |
158 | return 4; | 164 | palette[i].rgbBlue); |
159 | } | 165 | } |
160 | if(readlong(&fh.Height) > 64) | 166 | |
161 | { | 167 | width = readlong(&fh.Width); |
162 | debugf("info - Bitmap is too high for Archos models (%d pixels, max is 64)\n", | 168 | height = readlong(&fh.Height); |
163 | readlong(&fh.Height)); | 169 | padded_width = (width * depth / 8 + 3) & ~3; /* aligned 4-bytes boundaries */ |
164 | } | 170 | |
165 | 171 | size = padded_width * height; /* read this many bytes */ | |
166 | for(l=0;l < 2;l++) | 172 | bmp = (unsigned char *)malloc(size); |
167 | { | 173 | *bitmap = (unsigned char *)malloc(width * height); |
168 | if(read(fd, &palette[l],sizeof(struct RGBQUAD)) != | 174 | |
169 | sizeof(struct RGBQUAD)) | 175 | if ((bmp == NULL) || (*bitmap == NULL)) |
170 | { | 176 | { |
171 | debugf("error - Can't read bitmap's color palette\n"); | 177 | debugf("error - Out of memory\n"); |
172 | close(fd); | 178 | close(fd); |
173 | return 5; | 179 | return 5; |
174 | } | 180 | } |
175 | } | 181 | |
176 | if(depth == 8 ) { | 182 | if (lseek(fd, (off_t)readlong(&fh.OffBits), SEEK_SET) < 0) |
177 | /* pass the other palettes */ | 183 | { |
178 | lseek(fd, 254*sizeof(struct RGBQUAD), SEEK_CUR); | 184 | debugf("error - Can't seek to start of image data\n"); |
179 | } | 185 | close(fd); |
180 | 186 | return 6; | |
181 | /* Try to guess the foreground and background colors. | 187 | } |
182 | We assume that the foreground color is the darkest. */ | 188 | if (read(fd, (unsigned char*)bmp, (long)size) != size) |
183 | if(((int)palette[0].rgbRed + | 189 | { |
184 | (int)palette[0].rgbGreen + | 190 | debugf("error - Can't read image\n"); |
185 | (int)palette[0].rgbBlue) > | 191 | close(fd); |
186 | ((int)palette[1].rgbRed + | 192 | return 7; |
187 | (int)palette[1].rgbGreen + | 193 | } |
188 | (int)palette[1].rgbBlue)) | 194 | |
189 | { | 195 | close(fd); |
190 | background = 0; | 196 | *get_width = width; |
191 | } | 197 | *get_height = height; |
192 | else | 198 | |
193 | { | 199 | switch (depth) |
194 | background = 1; | 200 | { |
195 | } | 201 | case 1: |
196 | 202 | for (row = 0; row < height; row++) | |
197 | width = readlong(&fh.Width); | 203 | for (col = 0; col < width; col++) |
198 | 204 | { | |
199 | if(depth == 8) | 205 | data = (bmp[(height - 1 - row) * padded_width + col / 8] |
200 | PaddedWidth = ((width+3)&(~0x3)); /* aligned 4-bytes boundaries */ | 206 | >> (~col & 7)) & 1; |
201 | else | 207 | (*bitmap)[row * width + col] = bright[data]; |
202 | PaddedWidth = ((width+31)&(~0x1f))/8; | 208 | } |
203 | 209 | break; | |
204 | height = readlong(&fh.Height); | 210 | |
205 | 211 | case 4: | |
206 | allocsize = size = PaddedWidth*height; /* read this many bytes */ | 212 | for (row = 0; row < height; row++) |
207 | bmp = (unsigned char *)malloc(size); | 213 | for (col = 0; col < width; col++) |
208 | 214 | { | |
209 | if(height%8) { | 215 | data = (bmp[(height - 1 - row) * padded_width + col / 2] |
210 | /* not even 8 bytes, add up to a full 8 pixels boundary */ | 216 | >> (4 * (~col & 1))) & 0x0F; |
211 | height += 8-(height%8); | 217 | (*bitmap)[row * width + col] = bright[data]; |
212 | allocsize = PaddedWidth*height; /* bytes to alloc */ | 218 | } |
213 | } | 219 | break; |
214 | 220 | ||
215 | *bitmap = (unsigned char *)malloc(allocsize); | 221 | case 8: |
216 | 222 | for (row = 0; row < height; row++) | |
217 | if(bmp == NULL) | 223 | for (col = 0; col < width; col++) |
218 | { | 224 | { |
219 | debugf("error - Out of memory\n"); | 225 | data = bmp[(height - 1 - row) * padded_width + col]; |
220 | close(fd); | 226 | (*bitmap)[row * width + col] = bright[data]; |
221 | return 6; | 227 | } |
222 | } | 228 | break; |
223 | else | 229 | |
224 | { | 230 | case 16: |
225 | if(read(fd, (unsigned char*)bmp,(long)size) != size) { | 231 | for (row = 0; row < height; row++) |
226 | debugf("error - Can't read image\n"); | 232 | for (col = 0; col < width; col++) |
227 | close(fd); | 233 | { |
228 | return 7; | 234 | data = readshort(&bmp[(height - 1 - row) * padded_width + 2 * col]); |
229 | } | 235 | (*bitmap)[row * width + col] = brightness((data >> 7) & 0xF8, |
230 | } | 236 | (data >> 2) & 0xF8, (data << 3) & 0xF8); |
231 | 237 | } | |
232 | bitmap_height = readlong(&fh.Height); | 238 | break; |
233 | bitmap_width = readlong(&fh.Width); | 239 | |
234 | 240 | case 24: | |
235 | *get_width = bitmap_width; | 241 | for (row = 0; row < height; row++) |
236 | *get_height = bitmap_height; | 242 | for (col = 0; col < width; col++) |
237 | 243 | { | |
238 | if(depth == 8) | 244 | i = (height - 1 - row) * padded_width + 3 * col; |
239 | { | 245 | (*bitmap)[row * width + col] = |
240 | /* Now convert the bitmap into an array with 1 byte per pixel, | 246 | brightness(bmp[i+2], bmp[i+1], bmp[i]); |
241 | exactly the size of the image */ | 247 | } |
242 | for(row = 0;row < bitmap_height;row++) { | 248 | break; |
243 | for(col = 0;col < bitmap_width;col++) { | 249 | |
244 | if(bmp[(bitmap_height-1 -row) * PaddedWidth + col]) { | 250 | case 32: |
245 | (*bitmap)[ (row/8) * bitmap_width + col ] &= | 251 | for (row = 0; row < height; row++) |
246 | ~ (1<<(row&7)); | 252 | for (col = 0; col < width; col++) |
247 | } | 253 | { |
248 | else { | 254 | i = (height - 1 - row) * padded_width + 4 * col; |
249 | (*bitmap)[ (row/8) * bitmap_width + col ] |= | 255 | (*bitmap)[row * width + col] = |
250 | 1<<(row&7); | 256 | brightness(bmp[i+2], bmp[i+1], bmp[i]); |
251 | } | 257 | } |
252 | } | 258 | break; |
253 | } | 259 | |
254 | } | 260 | default: /* should never happen */ |
255 | else | 261 | debugf("error - Unsupported bitmap depth %d.\n", depth); |
256 | { | 262 | return 8; |
257 | int bit; | 263 | } |
258 | int byte; | 264 | |
259 | /* monocrome BMP conversion uses 8 pixels per byte */ | 265 | free(bmp); |
260 | for(row = 0; row < bitmap_height; row++) { | 266 | |
261 | bit = 7; | 267 | return 0; /* success */ |
262 | byte = 0; | 268 | } |
263 | for(col = 0;col < bitmap_width;col++) { | 269 | |
264 | if((bmp[(bitmap_height - row - 1) * PaddedWidth + byte] & | 270 | /**************************************************************************** |
265 | (1 << bit))) { | 271 | * transform_bitmap() |
266 | (*bitmap)[(row/8) * bitmap_width + col ] &= | 272 | * |
267 | ~(1<<(row&7)); | 273 | * Transform a 1-byte-per-pixel bitmap into one of the supported |
268 | } | 274 | * destination formats |
269 | else { | 275 | ****************************************************************************/ |
270 | (*bitmap)[(row/8) * bitmap_width + col ] |= | 276 | |
271 | 1<<(row&7); | 277 | int transform_bitmap(const unsigned char *src, long width, long height, |
272 | } | 278 | int format, unsigned char **dest, long *dst_width, |
273 | if(bit) { | 279 | long *dst_height) |
274 | bit--; | 280 | { |
275 | } | 281 | long row, col; |
276 | else { | 282 | long dst_w, dst_h; |
277 | bit = 7; | 283 | |
278 | byte++; | 284 | switch (format) |
279 | } | 285 | { |
280 | } | 286 | case 0: /* Archos recorders, Ondio, Gmini 120/SP, Iriver H1x0 monochrome */ |
281 | } | 287 | dst_w = width; |
282 | } | 288 | dst_h = (height + 7) / 8; |
283 | 289 | break; | |
284 | free(bmp); | 290 | |
285 | 291 | case 1: /* Archos player graphics library */ | |
286 | } | 292 | dst_w = (width + 7) / 8; |
287 | close(fd); | 293 | dst_h = height; |
288 | return 0; /* success */ | 294 | break; |
295 | |||
296 | case 2: /* Iriver H1x0 4-grey */ | ||
297 | dst_w = width; | ||
298 | dst_h = (height + 3) / 4; | ||
299 | break; | ||
300 | |||
301 | default: /* unknown */ | ||
302 | debugf("error - Undefined destination format\n"); | ||
303 | return 1; | ||
304 | } | ||
305 | |||
306 | *dest = (unsigned char *)malloc(dst_w * dst_h); | ||
307 | if (*dest == NULL) | ||
308 | { | ||
309 | debugf("error - Out of memory.\n"); | ||
310 | return 2; | ||
311 | } | ||
312 | memset(*dest, 0, dst_w * dst_h); | ||
313 | *dst_width = dst_w; | ||
314 | *dst_height = dst_h; | ||
315 | |||
316 | switch (format) | ||
317 | { | ||
318 | case 0: /* Archos recorders, Ondio, Gmini 120/SP, Iriver H1x0 b&w */ | ||
319 | for (row = 0; row < height; row++) | ||
320 | for (col = 0; col < width; col++) | ||
321 | { | ||
322 | (*dest)[(row/8) * dst_w + col] |= | ||
323 | (~src[row * width + col] & 0x80) >> (~row & 7); | ||
324 | } | ||
325 | break; | ||
326 | |||
327 | case 1: /* Archos player graphics library */ | ||
328 | for (row = 0; row < height; row++) | ||
329 | for (col = 0; col < width; col++) | ||
330 | { | ||
331 | (*dest)[row * dst_w + (col/8)] |= | ||
332 | (~src[row * width + col] & 0x80) >> (col & 7); | ||
333 | } | ||
334 | break; | ||
335 | |||
336 | case 2: /* Iriver H1x0 4-grey */ | ||
337 | for (row = 0; row < height; row++) | ||
338 | for (col = 0; col < width; col++) | ||
339 | { | ||
340 | (*dest)[(row/4) * dst_w + col] |= | ||
341 | (~src[row * width + col] & 0xC0) >> (2 * (~row & 3)); | ||
342 | } | ||
343 | break; | ||
344 | } | ||
345 | |||
346 | return 0; | ||
289 | } | 347 | } |
290 | 348 | ||
291 | /********************************************************************* | 349 | /**************************************************************************** |
292 | ** generate_c_source() | 350 | * generate_c_source() |
293 | ** | 351 | * |
294 | ** Outputs a C source code with the bitmap in an array, accompanied by | 352 | * Outputs a C source code with the bitmap in an array, accompanied by |
295 | ** some #define's | 353 | * some #define's |
296 | **********************************************************************/ | 354 | ****************************************************************************/ |
297 | void generate_c_source(char *id, int width, int height, unsigned char *bitmap) | 355 | |
356 | void generate_c_source(char *id, long width, long height, | ||
357 | const unsigned char *t_bitmap, long t_width, | ||
358 | long t_height) | ||
298 | { | 359 | { |
299 | FILE *f; | 360 | FILE *f; |
300 | unsigned int i, a, eline; | 361 | long i, a; |
301 | 362 | ||
302 | f = stdout; | 363 | f = stdout; |
303 | 364 | ||
304 | if(!id || !id[0]) | 365 | if (!id || !id[0]) |
305 | id = "bitmap"; | 366 | id = "bitmap"; |
306 | 367 | ||
307 | fprintf(f, | 368 | fprintf(f, |
308 | "#define BMPHEIGHT_%s %d" | 369 | "#define BMPHEIGHT_%s %ld\n" |
309 | "\n#define BMPWIDTH_%s %d" | 370 | "#define BMPWIDTH_%s %ld\n" |
310 | "\nconst unsigned char %s[] = {\n", | 371 | "const unsigned char %s[] = {\n", |
311 | id, height, id, width, id ); | 372 | id, height, id, width, id); |
312 | 373 | ||
313 | for(i=0, eline=0; i< height; i+=8, eline++) { | 374 | for (i = 0; i < t_height; i++) |
314 | for (a=0; a<width; a++) | 375 | { |
315 | fprintf(f, "0x%02x,%c", bitmap[eline*width + a], | 376 | for (a = 0; a < t_width; a++) |
316 | (a+1)%13?' ':'\n'); | 377 | fprintf(f, "0x%02x,%c", t_bitmap[i * t_width + a], |
317 | fprintf(f, "\n"); | 378 | (a + 1) % 13 ? ' ' : '\n'); |
318 | } | 379 | fprintf(f, "\n"); |
319 | 380 | } | |
320 | 381 | ||
321 | fprintf(f, "\n};\n"); | 382 | fprintf(f, "\n};\n"); |
322 | } | 383 | } |
323 | 384 | ||
385 | /**************************************************************************** | ||
386 | * generate_ascii() | ||
387 | * | ||
388 | * Outputs an ascii picture of the bitmap | ||
389 | ****************************************************************************/ | ||
324 | 390 | ||
325 | /********************************************************************* | 391 | void generate_ascii(long width, long height, unsigned char *bitmap) |
326 | ** generate_ascii() | ||
327 | ** | ||
328 | ** Outputs an ascii picture of the bitmap | ||
329 | **********************************************************************/ | ||
330 | void generate_ascii(int width, int height, unsigned char *bitmap) | ||
331 | { | 392 | { |
332 | FILE *f; | 393 | FILE *f; |
333 | unsigned int i, eline; | 394 | long x, y; |
334 | 395 | ||
335 | f = stdout; | 396 | f = stdout; |
336 | 397 | ||
337 | /* for screen output debugging */ | 398 | /* for screen output debugging */ |
338 | for(i=0, eline=0; i< height; i+=8, eline++) { | 399 | for (y = 0; y < height; y++) |
339 | unsigned int x, y; | 400 | { |
340 | for(y=0; y<8 && (i+y < height); y++) { | 401 | for (x = 0; x < width; x++) |
341 | for(x=0; x < width; x++) { | 402 | { |
342 | 403 | fprintf(f, (bitmap[y * width + x] & 0x80) ? " " : "*"); | |
343 | if(bitmap[eline*width + x] & (1<<y)) { | ||
344 | fprintf(f, "*"); | ||
345 | } | ||
346 | else | ||
347 | fprintf(f, " "); | ||
348 | } | ||
349 | fprintf(f, "\n"); | ||
350 | } | 404 | } |
405 | fprintf(f, "\n"); | ||
351 | } | 406 | } |
352 | } | 407 | } |
353 | 408 | ||
354 | void print_usage(void) | 409 | void print_usage(void) |
355 | { | 410 | { |
356 | printf("Usage: %s [-i <id>] [-a] <bitmap file>\n" | 411 | printf("Usage: %s [-i <id>] [-a] <bitmap file>\n" |
357 | "\t-i <id> Bitmap name (default is filename without extension)\n" | 412 | "\t-i <id> Bitmap name (default is filename without extension)\n" |
358 | "\t-a Show ascii picture of bitmap\n", | 413 | "\t-a Show ascii picture of bitmap\n" |
359 | APPLICATION_NAME); | 414 | "\t-f <n> Generate destination format n, default = 0\n" |
360 | printf("build date: " __DATE__ "\n\n"); | 415 | "\t 0 Archos recorder, Ondio, Gmini 120/SP, Iriver H1x0 mono\n" |
416 | "\t 1 Archos player graphics library\n" | ||
417 | "\t 2 Iriver H1x0 4-grey\n" | ||
418 | , APPLICATION_NAME); | ||
419 | printf("build date: " __DATE__ "\n\n"); | ||
361 | } | 420 | } |
362 | 421 | ||
363 | int main(int argc, char **argv) | 422 | int main(int argc, char **argv) |
364 | { | 423 | { |
365 | char *bmp_filename = NULL; | 424 | char *bmp_filename = NULL; |
366 | char *id = NULL; | 425 | char *id = NULL; |
367 | int i; | 426 | int i; |
368 | int height, width; | 427 | int ascii = false; |
369 | int ascii = false; | 428 | int format = 0; |
370 | char* bitmap = NULL; | 429 | unsigned char *bitmap = NULL; |
371 | 430 | unsigned char *t_bitmap = NULL; | |
372 | 431 | long width, height; | |
373 | for(i = 1;i < argc;i++) | 432 | long t_width, t_height; |
374 | { | 433 | |
375 | if(argv[i][0] == '-') | 434 | |
376 | { | 435 | for (i = 1;i < argc;i++) |
377 | switch(argv[i][1]) | 436 | { |
378 | { | 437 | if (argv[i][0] == '-') |
379 | case 'i': /* ID */ | 438 | { |
380 | if(argv[i][2]) | 439 | switch (argv[i][1]) |
381 | { | 440 | { |
382 | id = &argv[i][2]; | 441 | case 'i': /* ID */ |
383 | } | 442 | if (argv[i][2]) |
384 | else if(argc > i+1) | 443 | { |
385 | { | 444 | id = &argv[i][2]; |
386 | id = argv[i+1]; | 445 | } |
387 | i++; | 446 | else if (argc > i+1) |
388 | } | 447 | { |
389 | else | 448 | id = argv[i+1]; |
390 | { | 449 | i++; |
391 | print_usage(); | 450 | } |
392 | exit(1); | 451 | else |
393 | } | 452 | { |
394 | break; | 453 | print_usage(); |
395 | 454 | exit(1); | |
396 | case 'a': /* Assembly */ | 455 | } |
397 | ascii = true; | 456 | break; |
398 | break; | 457 | |
399 | 458 | case 'a': /* Ascii art */ | |
400 | default: | 459 | ascii = true; |
401 | print_usage(); | 460 | break; |
402 | exit(1); | 461 | |
403 | break; | 462 | case 'f': |
404 | } | 463 | if (argv[i][2]) |
405 | } | 464 | { |
406 | else | 465 | format = atoi(&argv[i][2]); |
407 | { | 466 | } |
408 | if(!bmp_filename) | 467 | else if (argc > i+1) |
409 | { | 468 | { |
410 | bmp_filename = argv[i]; | 469 | format = atoi(argv[i+1]); |
411 | } | 470 | i++; |
412 | else | 471 | } |
413 | { | 472 | else |
414 | print_usage(); | 473 | { |
415 | exit(1); | 474 | print_usage(); |
416 | } | 475 | exit(1); |
417 | } | 476 | } |
418 | } | 477 | break; |
419 | 478 | ||
420 | if (!bmp_filename) | 479 | default: |
421 | { | 480 | print_usage(); |
422 | print_usage(); | 481 | exit(1); |
423 | exit(1); | 482 | break; |
424 | } | 483 | } |
425 | 484 | } | |
426 | if (!id) | 485 | else |
427 | { | 486 | { |
428 | char *ptr=strrchr(bmp_filename, '/'); | 487 | if (!bmp_filename) |
429 | if(ptr) | 488 | { |
430 | ptr++; | 489 | bmp_filename = argv[i]; |
431 | else | 490 | } |
432 | ptr = bmp_filename; | 491 | else |
433 | id = strdup(ptr); | 492 | { |
434 | for (i = 0; id[i]; i++) | 493 | print_usage(); |
435 | if (id[i] == '.') | 494 | exit(1); |
436 | id[i] = '\0'; | 495 | } |
437 | } | 496 | } |
438 | 497 | } | |
439 | if (read_bmp_file(bmp_filename, &width, &height, &bitmap)) | 498 | |
440 | return 0; | 499 | if (!bmp_filename) |
441 | 500 | { | |
442 | if (ascii) | 501 | print_usage(); |
443 | generate_ascii(width, height, bitmap); | 502 | exit(1); |
444 | else | 503 | } |
445 | generate_c_source(id, width, height, bitmap); | 504 | |
446 | 505 | if (!id) | |
447 | return 0; | 506 | { |
507 | char *ptr=strrchr(bmp_filename, '/'); | ||
508 | if (ptr) | ||
509 | ptr++; | ||
510 | else | ||
511 | ptr = bmp_filename; | ||
512 | id = strdup(ptr); | ||
513 | for (i = 0; id[i]; i++) | ||
514 | if (id[i] == '.') | ||
515 | id[i] = '\0'; | ||
516 | } | ||
517 | |||
518 | if (read_bmp_file(bmp_filename, &width, &height, &bitmap)) | ||
519 | exit(1); | ||
520 | |||
521 | |||
522 | if (ascii) | ||
523 | { | ||
524 | generate_ascii(width, height, bitmap); | ||
525 | } | ||
526 | else | ||
527 | { | ||
528 | if (transform_bitmap(bitmap, width, height, format, &t_bitmap, | ||
529 | &t_width, &t_height)) | ||
530 | exit(1); | ||
531 | generate_c_source(id, width, height, t_bitmap, t_width, t_height); | ||
532 | } | ||
533 | |||
534 | return 0; | ||
448 | } | 535 | } |