First start for "Manage Settings" and "Browse Themes" (FS#4997 by Mark Bright)
[Rockbox.git] / tools / bmp2rb.c
blob03da5895b81a1ccbbdd08ec9bbfb9a7c309100a4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 /****************************************************************************
21 * Converts BMP files to Rockbox bitmap format
23 * 1999-05-03 Linus Nielsen Feltzing
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)
29 ****************************************************************************/
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdbool.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
40 #define debugf printf
42 #ifdef __GNUC__
43 #define STRUCT_PACKED __attribute__((packed))
44 #else
45 #define STRUCT_PACKED
46 #pragma pack (push, 2)
47 #endif
49 struct Fileheader
51 unsigned short Type; /* signature - 'BM' */
52 unsigned int Size; /* file size in bytes */
53 unsigned short Reserved1; /* 0 */
54 unsigned short Reserved2; /* 0 */
55 unsigned int OffBits; /* offset to bitmap */
56 unsigned int StructSize; /* size of this struct (40) */
57 unsigned int Width; /* bmap width in pixels */
58 unsigned int Height; /* bmap height in pixels */
59 unsigned short Planes; /* num planes - always 1 */
60 unsigned short BitCount; /* bits per pixel */
61 unsigned int Compression; /* compression flag */
62 unsigned int SizeImage; /* image size in bytes */
63 int XPelsPerMeter; /* horz resolution */
64 int YPelsPerMeter; /* vert resolution */
65 unsigned int ClrUsed; /* 0 -> color table size */
66 unsigned int ClrImportant; /* important color count */
67 } STRUCT_PACKED;
69 struct RGBQUAD
71 unsigned char rgbBlue;
72 unsigned char rgbGreen;
73 unsigned char rgbRed;
74 unsigned char rgbReserved;
75 } STRUCT_PACKED;
77 short readshort(void* value)
79 unsigned char* bytes = (unsigned char*) value;
80 return bytes[0] | (bytes[1] << 8);
83 int readint(void* value)
85 unsigned char* bytes = (unsigned char*) value;
86 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
89 unsigned char brightness(struct RGBQUAD color)
91 return (3 * (unsigned int)color.rgbRed + 6 * (unsigned int)color.rgbGreen
92 + (unsigned int)color.rgbBlue) / 10;
95 #ifndef O_BINARY
96 #define O_BINARY 0 /* systems that don't have O_BINARY won't make a difference
97 on text and binary files */
98 #endif
100 /****************************************************************************
101 * read_bmp_file()
103 * Reads an uncompressed BMP file and puts the data in a 4-byte-per-pixel
104 * (RGBQUAD) array. Returns 0 on success.
106 ***************************************************************************/
108 int read_bmp_file(char* filename,
109 int *get_width, /* in pixels */
110 int *get_height, /* in pixels */
111 struct RGBQUAD **bitmap)
113 struct Fileheader fh;
114 struct RGBQUAD palette[256];
116 int fd = open(filename, O_RDONLY| O_BINARY);
117 unsigned short data;
118 unsigned char *bmp;
119 int width, height;
120 int padded_width;
121 int size;
122 int row, col, i;
123 int numcolors, compression;
124 int depth;
126 if (fd == -1)
128 debugf("error - can't open '%s'\n", filename);
129 return 1;
131 if (read(fd, &fh, sizeof(struct Fileheader)) !=
132 sizeof(struct Fileheader))
134 debugf("error - can't Read Fileheader Stucture\n");
135 close(fd);
136 return 2;
139 compression = readint(&fh.Compression);
141 if (compression != 0)
143 debugf("error - Unsupported compression %ld\n", compression);
144 close(fd);
145 return 3;
148 depth = readshort(&fh.BitCount);
150 if (depth <= 8)
152 numcolors = readint(&fh.ClrUsed);
153 if (numcolors == 0)
154 numcolors = 1 << depth;
156 if (read(fd, &palette[0], numcolors * sizeof(struct RGBQUAD))
157 != numcolors * sizeof(struct RGBQUAD))
159 debugf("error - Can't read bitmap's color palette\n");
160 close(fd);
161 return 4;
165 width = readint(&fh.Width);
166 height = readint(&fh.Height);
167 padded_width = ((width * depth + 31) / 8) & ~3; /* aligned 4-bytes boundaries */
169 size = padded_width * height; /* read this many bytes */
170 bmp = (unsigned char *)malloc(size);
171 *bitmap = (struct RGBQUAD *)malloc(width * height * sizeof(struct RGBQUAD));
173 if ((bmp == NULL) || (*bitmap == NULL))
175 debugf("error - Out of memory\n");
176 close(fd);
177 return 5;
180 if (lseek(fd, (off_t)readint(&fh.OffBits), SEEK_SET) < 0)
182 debugf("error - Can't seek to start of image data\n");
183 close(fd);
184 return 6;
186 if (read(fd, (unsigned char*)bmp, (int)size) != size)
188 debugf("error - Can't read image\n");
189 close(fd);
190 return 7;
193 close(fd);
194 *get_width = width;
195 *get_height = height;
197 switch (depth)
199 case 1:
200 for (row = 0; row < height; row++)
201 for (col = 0; col < width; col++)
203 data = (bmp[(height - 1 - row) * padded_width + col / 8]
204 >> (~col & 7)) & 1;
205 (*bitmap)[row * width + col] = palette[data];
207 break;
209 case 4:
210 for (row = 0; row < height; row++)
211 for (col = 0; col < width; col++)
213 data = (bmp[(height - 1 - row) * padded_width + col / 2]
214 >> (4 * (~col & 1))) & 0x0F;
215 (*bitmap)[row * width + col] = palette[data];
217 break;
219 case 8:
220 for (row = 0; row < height; row++)
221 for (col = 0; col < width; col++)
223 data = bmp[(height - 1 - row) * padded_width + col];
224 (*bitmap)[row * width + col] = palette[data];
226 break;
228 case 16:
229 for (row = 0; row < height; row++)
230 for (col = 0; col < width; col++)
232 data = readshort(&bmp[(height - 1 - row) * padded_width + 2 * col]);
233 (*bitmap)[row * width + col].rgbRed =
234 ((data >> 7) & 0xF8) | ((data >> 12) & 0x07);
235 (*bitmap)[row * width + col].rgbGreen =
236 ((data >> 2) & 0xF8) | ((data >> 7) & 0x07);
237 (*bitmap)[row * width + col].rgbBlue =
238 ((data << 3) & 0xF8) | ((data >> 2) & 0x07);
239 (*bitmap)[row * width + col].rgbReserved = 0;
241 break;
243 case 24:
244 for (row = 0; row < height; row++)
245 for (col = 0; col < width; col++)
247 i = (height - 1 - row) * padded_width + 3 * col;
248 (*bitmap)[row * width + col].rgbRed = bmp[i+2];
249 (*bitmap)[row * width + col].rgbGreen = bmp[i+1];
250 (*bitmap)[row * width + col].rgbBlue = bmp[i];
251 (*bitmap)[row * width + col].rgbReserved = 0;
253 break;
255 case 32:
256 for (row = 0; row < height; row++)
257 for (col = 0; col < width; col++)
259 i = (height - 1 - row) * padded_width + 4 * col;
260 (*bitmap)[row * width + col].rgbRed = bmp[i+2];
261 (*bitmap)[row * width + col].rgbGreen = bmp[i+1];
262 (*bitmap)[row * width + col].rgbBlue = bmp[i];
263 (*bitmap)[row * width + col].rgbReserved = 0;
265 break;
267 default: /* should never happen */
268 debugf("error - Unsupported bitmap depth %d.\n", depth);
269 return 8;
272 free(bmp);
274 return 0; /* success */
277 /****************************************************************************
278 * transform_bitmap()
280 * Transform a 4-byte-per-pixel bitmap (RGBQUAD) into one of the supported
281 * destination formats
282 ****************************************************************************/
284 int transform_bitmap(const struct RGBQUAD *src, int width, int height,
285 int format, unsigned short **dest, int *dst_width,
286 int *dst_height, int *dst_depth)
288 int row, col;
289 int dst_w, dst_h, dst_d;
291 switch (format)
293 case 0: /* Archos recorders, Ondio, Gmini 120/SP, Iriver H1x0 monochrome */
294 dst_w = width;
295 dst_h = (height + 7) / 8;
296 dst_d = 8;
297 break;
299 case 1: /* Archos player graphics library */
300 dst_w = (width + 7) / 8;
301 dst_h = height;
302 dst_d = 8;
303 break;
305 case 2: /* Iriver H1x0 4-grey */
306 dst_w = width;
307 dst_h = (height + 3) / 4;
308 dst_d = 8;
309 break;
311 case 3: /* Canonical 8-bit grayscale */
312 dst_w = width;
313 dst_h = height;
314 dst_d = 8;
315 break;
317 case 4: /* 16-bit packed RGB (5-6-5) */
318 case 5: /* 16-bit packed and byte-swapped RGB (5-6-5) */
319 dst_w = width;
320 dst_h = height;
321 dst_d = 16;
322 break;
324 case 6: /* greyscale iPods 4-grey */
325 dst_w = (width + 3) / 4;
326 dst_h = height;
327 dst_d = 8;
328 break;
330 default: /* unknown */
331 debugf("error - Undefined destination format\n");
332 return 1;
335 *dest = (unsigned short *)malloc(dst_w * dst_h * sizeof(short));
336 if (*dest == NULL)
338 debugf("error - Out of memory.\n");
339 return 2;
341 memset(*dest, 0, dst_w * dst_h * sizeof(short));
342 *dst_width = dst_w;
343 *dst_height = dst_h;
344 *dst_depth = dst_d;
346 switch (format)
348 case 0: /* Archos recorders, Ondio, Gmini 120/SP, Iriver H1x0 b&w */
349 for (row = 0; row < height; row++)
350 for (col = 0; col < width; col++)
352 (*dest)[(row/8) * dst_w + col] |=
353 (~brightness(src[row * width + col]) & 0x80) >> (~row & 7);
355 break;
357 case 1: /* Archos player graphics library */
358 for (row = 0; row < height; row++)
359 for (col = 0; col < width; col++)
361 (*dest)[row * dst_w + (col/8)] |=
362 (~brightness(src[row * width + col]) & 0x80) >> (col & 7);
364 break;
366 case 2: /* Iriver H1x0 4-grey */
367 for (row = 0; row < height; row++)
368 for (col = 0; col < width; col++)
370 (*dest)[(row/4) * dst_w + col] |=
371 (~brightness(src[row * width + col]) & 0xC0) >> (2 * (~row & 3));
373 break;
375 case 3: /* Canonical 8-bit grayscale */
376 for (row = 0; row < height; row++)
377 for (col = 0; col < width; col++)
379 (*dest)[row * dst_w + col] = brightness(src[row * width + col]);
381 break;
383 case 4: /* 16-bit packed RGB (5-6-5) */
384 case 5: /* 16-bit packed and byte-swapped RGB (5-6-5) */
385 for (row = 0; row < height; row++)
386 for (col = 0; col < width; col++)
388 unsigned short rgb =
389 (((src[row * width + col].rgbRed >> 3) << 11) |
390 ((src[row * width + col].rgbGreen >> 2) << 5) |
391 ((src[row * width + col].rgbBlue >> 3)));
393 if (format == 4)
394 (*dest)[row * dst_w + col] = rgb;
395 else
396 (*dest)[row * dst_w + col] = ((rgb&0xff00)>>8)|((rgb&0x00ff)<<8);
398 break;
400 case 6: /* greyscale iPods 4-grey */
401 for (row = 0; row < height; row++)
402 for (col = 0; col < width; col++)
404 (*dest)[row * dst_w + (col/4)] |=
405 (~brightness(src[row * width + col]) & 0xC0) >> (2 * (~col & 3));
407 break;
410 return 0;
413 /****************************************************************************
414 * generate_c_source()
416 * Outputs a C source code with the bitmap in an array, accompanied by
417 * some #define's
418 ****************************************************************************/
420 void generate_c_source(char *id, char* header_dir, int width, int height,
421 const unsigned short *t_bitmap, int t_width,
422 int t_height, int t_depth)
424 FILE *f;
425 FILE *fh;
426 int i, a;
427 char header_name[1024];
429 if (!id || !id[0])
430 id = "bitmap";
432 f = stdout;
434 if (header_dir && header_dir[0])
436 snprintf(header_name,sizeof(header_name),"%s/%s.h",header_dir,id);
437 fh = fopen(header_name,"w+");
439 if (fh == NULL)
441 debugf("error - can't open '%s'\n", header_name);
442 return;
444 fprintf(fh,
445 "#define BMPHEIGHT_%s %ld\n"
446 "#define BMPWIDTH_%s %ld\n",
447 id, height, id, width);
448 if (t_depth <= 8)
449 fprintf(fh, "extern const unsigned char %s[];\n", id);
450 else
451 fprintf(fh, "extern const unsigned short %s[];\n", id);
453 fclose(fh);
454 } else {
455 fprintf(f,
456 "#define BMPHEIGHT_%s %ld\n"
457 "#define BMPWIDTH_%s %ld\n",
458 id, height, id, width);
461 if (t_depth <= 8)
462 fprintf(f, "const unsigned char %s[] = {\n", id);
463 else
464 fprintf(f, "const unsigned short %s[] = {\n", id);
466 for (i = 0; i < t_height; i++)
468 for (a = 0; a < t_width; a++)
470 if (t_depth <= 8)
471 fprintf(f, "0x%02x,%c", t_bitmap[i * t_width + a],
472 (a + 1) % 13 ? ' ' : '\n');
473 else
474 fprintf(f, "0x%04x,%c", t_bitmap[i * t_width + a],
475 (a + 1) % 10 ? ' ' : '\n');
477 fprintf(f, "\n");
480 fprintf(f, "\n};\n");
483 /****************************************************************************
484 * generate_ascii()
486 * Outputs an ascii picture of the bitmap
487 ****************************************************************************/
489 void generate_ascii(int width, int height, struct RGBQUAD *bitmap)
491 FILE *f;
492 int x, y;
494 f = stdout;
496 /* for screen output debugging */
497 for (y = 0; y < height; y++)
499 for (x = 0; x < width; x++)
501 fprintf(f, (brightness(bitmap[y * width + x]) & 0x80) ? " " : "*");
503 fprintf(f, "\n");
507 void print_usage(void)
509 printf("Usage: %s [-i <id>] [-a] <bitmap file>\n"
510 "\t-i <id> Bitmap name (default is filename without extension)\n"
511 "\t-h <dir> Create header file in <dir>/<id>.h\n"
512 "\t-a Show ascii picture of bitmap\n"
513 "\t-f <n> Generate destination format n, default = 0\n"
514 "\t 0 Archos recorder, Ondio, Gmini 120/SP, Iriver H1x0 mono\n"
515 "\t 1 Archos player graphics library\n"
516 "\t 2 Iriver H1x0 4-grey\n"
517 "\t 3 Canonical 8-bit grayscale\n"
518 "\t 4 16-bit packed 5-6-5 RGB (iriver H300)\n"
519 "\t 5 16-bit packed and byte-swapped 5-6-5 RGB (iPod)\n"
520 "\t 6 Greayscale iPod 4-grey\n"
521 , APPLICATION_NAME);
522 printf("build date: " __DATE__ "\n\n");
525 int main(int argc, char **argv)
527 char *bmp_filename = NULL;
528 char *id = NULL;
529 char* header_dir = NULL;
530 int i;
531 int ascii = false;
532 int format = 0;
533 struct RGBQUAD *bitmap = NULL;
534 unsigned short *t_bitmap = NULL;
535 int width, height;
536 int t_width, t_height, t_depth;
539 for (i = 1;i < argc;i++)
541 if (argv[i][0] == '-')
543 switch (argv[i][1])
545 case 'h': /* .h filename */
546 if (argv[i][2])
548 header_dir = &argv[i][2];
550 else if (argc > i+1)
552 header_dir = argv[i+1];
553 i++;
555 else
557 print_usage();
558 exit(1);
560 break;
562 case 'i': /* ID */
563 if (argv[i][2])
565 id = &argv[i][2];
567 else if (argc > i+1)
569 id = argv[i+1];
570 i++;
572 else
574 print_usage();
575 exit(1);
577 break;
579 case 'a': /* Ascii art */
580 ascii = true;
581 break;
583 case 'f':
584 if (argv[i][2])
586 format = atoi(&argv[i][2]);
588 else if (argc > i+1)
590 format = atoi(argv[i+1]);
591 i++;
593 else
595 print_usage();
596 exit(1);
598 break;
600 default:
601 print_usage();
602 exit(1);
603 break;
606 else
608 if (!bmp_filename)
610 bmp_filename = argv[i];
612 else
614 print_usage();
615 exit(1);
620 if (!bmp_filename)
622 print_usage();
623 exit(1);
626 if (!id)
628 char *ptr=strrchr(bmp_filename, '/');
629 if (ptr)
630 ptr++;
631 else
632 ptr = bmp_filename;
633 id = strdup(ptr);
634 for (i = 0; id[i]; i++)
635 if (id[i] == '.')
636 id[i] = '\0';
639 if (read_bmp_file(bmp_filename, &width, &height, &bitmap))
640 exit(1);
643 if (ascii)
645 generate_ascii(width, height, bitmap);
647 else
649 if (transform_bitmap(bitmap, width, height, format, &t_bitmap,
650 &t_width, &t_height, &t_depth))
651 exit(1);
652 generate_c_source(id, header_dir, width, height, t_bitmap,
653 t_width, t_height, t_depth);
656 return 0;