uboot/tools/bmp_logo.c
<<
>>
Prefs
   1#include "compiler.h"
   2
   3typedef struct bitmap_s {               /* bitmap description */
   4        uint16_t width;
   5        uint16_t height;
   6        uint8_t palette[256*3];
   7        uint8_t *data;
   8} bitmap_t;
   9
  10#define DEFAULT_CMAP_SIZE       16      /* size of default color map    */
  11
  12/*
  13 * Neutralize little endians.
  14 */
  15uint16_t le_short(uint16_t x)
  16{
  17    uint16_t val;
  18    uint8_t *p = (uint8_t *)(&x);
  19
  20    val =  (*p++ & 0xff) << 0;
  21    val |= (*p & 0xff) << 8;
  22
  23    return val;
  24}
  25
  26void skip_bytes (FILE *fp, int n)
  27{
  28        while (n-- > 0)
  29                fgetc (fp);
  30}
  31
  32__attribute__ ((__noreturn__))
  33int error (char * msg, FILE *fp)
  34{
  35        fprintf (stderr, "ERROR: %s\n", msg);
  36
  37        fclose (fp);
  38
  39        exit (EXIT_FAILURE);
  40}
  41
  42int main (int argc, char *argv[])
  43{
  44        int     i, x;
  45        FILE    *fp;
  46        bitmap_t bmp;
  47        bitmap_t *b = &bmp;
  48        uint16_t data_offset, n_colors;
  49
  50        if (argc < 2) {
  51                fprintf (stderr, "Usage: %s file\n", argv[0]);
  52                exit (EXIT_FAILURE);
  53        }
  54
  55        if ((fp = fopen (argv[1], "rb")) == NULL) {
  56                perror (argv[1]);
  57                exit (EXIT_FAILURE);
  58        }
  59
  60        if (fgetc (fp) != 'B' || fgetc (fp) != 'M')
  61                error ("Input file is not a bitmap", fp);
  62
  63        /*
  64         * read width and height of the image, and the number of colors used;
  65         * ignore the rest
  66         */
  67        skip_bytes (fp, 8);
  68        if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1)
  69                error ("Couldn't read bitmap data offset", fp);
  70        skip_bytes (fp, 6);
  71        if (fread (&b->width,   sizeof (uint16_t), 1, fp) != 1)
  72                error ("Couldn't read bitmap width", fp);
  73        skip_bytes (fp, 2);
  74        if (fread (&b->height,  sizeof (uint16_t), 1, fp) != 1)
  75                error ("Couldn't read bitmap height", fp);
  76        skip_bytes (fp, 22);
  77        if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1)
  78                error ("Couldn't read bitmap colors", fp);
  79        skip_bytes (fp, 6);
  80
  81        /*
  82         * Repair endianess.
  83         */
  84        data_offset = le_short(data_offset);
  85        b->width = le_short(b->width);
  86        b->height = le_short(b->height);
  87        n_colors = le_short(n_colors);
  88
  89        /* assume we are working with an 8-bit file */
  90        if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
  91                /* reserve DEFAULT_CMAP_SIZE color map entries for default map */
  92                n_colors = 256 - DEFAULT_CMAP_SIZE;
  93        }
  94
  95        printf ("/*\n"
  96                " * Automatically generated by \"tools/bmp_logo\"\n"
  97                " *\n"
  98                " * DO NOT EDIT\n"
  99                " *\n"
 100                " */\n\n\n"
 101                "#ifndef __BMP_LOGO_H__\n"
 102                "#define __BMP_LOGO_H__\n\n"
 103                "#define BMP_LOGO_WIDTH\t\t%d\n"
 104                "#define BMP_LOGO_HEIGHT\t\t%d\n"
 105                "#define BMP_LOGO_COLORS\t\t%d\n"
 106                "#define BMP_LOGO_OFFSET\t\t%d\n"
 107                "\n",
 108                b->width, b->height, n_colors,
 109                DEFAULT_CMAP_SIZE);
 110
 111        /* allocate memory */
 112        if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL)
 113                error ("Error allocating memory for file", fp);
 114
 115        /* read and print the palette information */
 116        printf ("unsigned short bmp_logo_palette[] = {\n");
 117
 118        for (i=0; i<n_colors; ++i) {
 119                b->palette[(int)(i*3+2)] = fgetc(fp);
 120                b->palette[(int)(i*3+1)] = fgetc(fp);
 121                b->palette[(int)(i*3+0)] = fgetc(fp);
 122                x=fgetc(fp);
 123
 124                printf ("%s0x0%X%X%X,%s",
 125                        ((i%8) == 0) ? "\t" : "  ",
 126                        (b->palette[(int)(i*3+0)] >> 4) & 0x0F,
 127                        (b->palette[(int)(i*3+1)] >> 4) & 0x0F,
 128                        (b->palette[(int)(i*3+2)] >> 4) & 0x0F,
 129                        ((i%8) == 7) ? "\n" : ""
 130                );
 131        }
 132
 133        /* seek to offset indicated by file header */
 134        fseek(fp, (long)data_offset, SEEK_SET);
 135
 136        /* read the bitmap; leave room for default color map */
 137        printf ("\n");
 138        printf ("};\n");
 139        printf ("\n");
 140        printf ("unsigned char bmp_logo_bitmap[] = {\n");
 141        for (i=(b->height-1)*b->width; i>=0; i-=b->width) {
 142                for (x = 0; x < b->width; x++) {
 143                        b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \
 144                                                + DEFAULT_CMAP_SIZE;
 145                }
 146        }
 147        fclose (fp);
 148
 149        for (i=0; i<(b->height*b->width); ++i) {
 150                if ((i%8) == 0)
 151                        putchar ('\t');
 152                printf ("0x%02X,%c",
 153                        b->data[i],
 154                        ((i%8) == 7) ? '\n' : ' '
 155                );
 156        }
 157        printf ("\n"
 158                "};\n\n"
 159                "#endif /* __BMP_LOGO_H__ */\n"
 160        );
 161
 162        return (0);
 163}
 164