Merge tag 'u-boot-rockchip-20200531' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / tools / bmp_logo.c
index b2ad3d592768747ae0eed5bda884272a58bc47b7..74fcadca63e17c60b19ecd3099478ba511744108 100644 (file)
@@ -2,7 +2,8 @@
 
 enum {
        MODE_GEN_INFO,
-       MODE_GEN_DATA
+       MODE_GEN_DATA,
+       MODE_GEN_BMP
 };
 
 typedef struct bitmap_s {              /* bitmap description */
@@ -16,7 +17,8 @@ typedef struct bitmap_s {             /* bitmap description */
 
 void usage(const char *prog)
 {
-       fprintf(stderr, "Usage: %s [--gen-info|--gen-data] file\n", prog);
+       fprintf(stderr, "Usage: %s [--gen-info|--gen-data|--gen-bmp] file\n",
+               prog);
 }
 
 /*
@@ -73,10 +75,11 @@ void gen_info(bitmap_t *b, uint16_t n_colors)
 int main (int argc, char *argv[])
 {
        int     mode, i, x;
+       int     size;
        FILE    *fp;
        bitmap_t bmp;
        bitmap_t *b = &bmp;
-       uint16_t data_offset, n_colors;
+       uint16_t data_offset, n_colors, hdr_size;
 
        if (argc < 3) {
                usage(argv[0]);
@@ -87,6 +90,8 @@ int main (int argc, char *argv[])
                mode = MODE_GEN_INFO;
        else if (!strcmp(argv[1], "--gen-data"))
                mode = MODE_GEN_DATA;
+       else if (!strcmp(argv[1], "--gen-bmp"))
+               mode = MODE_GEN_BMP;
        else {
                usage(argv[0]);
                exit(EXIT_FAILURE);
@@ -108,7 +113,12 @@ int main (int argc, char *argv[])
        skip_bytes (fp, 8);
        if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1)
                error ("Couldn't read bitmap data offset", fp);
-       skip_bytes (fp, 6);
+       skip_bytes(fp, 2);
+       if (fread(&hdr_size,   sizeof(uint16_t), 1, fp) != 1)
+               error("Couldn't read bitmap header size", fp);
+       if (hdr_size < 40)
+               error("Invalid bitmap header", fp);
+       skip_bytes(fp, 2);
        if (fread (&b->width,   sizeof (uint16_t), 1, fp) != 1)
                error ("Couldn't read bitmap width", fp);
        skip_bytes (fp, 2);
@@ -117,7 +127,7 @@ int main (int argc, char *argv[])
        skip_bytes (fp, 22);
        if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1)
                error ("Couldn't read bitmap colors", fp);
-       skip_bytes (fp, 6);
+       skip_bytes(fp, hdr_size - 34);
 
        /*
         * Repair endianess.
@@ -126,6 +136,7 @@ int main (int argc, char *argv[])
        b->width = le_short(b->width);
        b->height = le_short(b->height);
        n_colors = le_short(n_colors);
+       size = b->width * b->height;
 
        /* assume we are working with an 8-bit file */
        if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
@@ -147,10 +158,6 @@ int main (int argc, char *argv[])
                "#ifndef __BMP_LOGO_DATA_H__\n"
                "#define __BMP_LOGO_DATA_H__\n\n");
 
-       /* allocate memory */
-       if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL)
-               error ("Error allocating memory for file", fp);
-
        /* read and print the palette information */
        printf("unsigned short bmp_logo_palette[] = {\n");
 
@@ -170,21 +177,39 @@ int main (int argc, char *argv[])
        }
 
        /* seek to offset indicated by file header */
-       fseek(fp, (long)data_offset, SEEK_SET);
+       if (mode == MODE_GEN_BMP) {
+               /* copy full bmp file */
+               fseek(fp, 0L, SEEK_END);
+               size = ftell(fp);
+               fseek(fp, 0L, SEEK_SET);
+       } else {
+               fseek(fp, (long)data_offset, SEEK_SET);
+       }
+
+       /* allocate memory */
+       b->data = (uint8_t *)malloc(size);
+       if (!b->data)
+               error("Error allocating memory for file", fp);
 
        /* read the bitmap; leave room for default color map */
        printf ("\n");
        printf ("};\n");
        printf ("\n");
        printf("unsigned char bmp_logo_bitmap[] = {\n");
-       for (i=(b->height-1)*b->width; i>=0; i-=b->width) {
-               for (x = 0; x < b->width; x++) {
-                       b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \
+       if (mode == MODE_GEN_BMP) {
+               /* write full bmp */
+               for (i = 0; i < size; i++)
+                       b->data[i] = (uint8_t)fgetc(fp);
+       } else {
+               for (i = (b->height - 1) * b->width; i >= 0; i -= b->width) {
+                       for (x = 0; x < b->width; x++) {
+                               b->data[i + x] = (uint8_t)fgetc(fp)
                                                + DEFAULT_CMAP_SIZE;
+                       }
                }
        }
 
-       for (i=0; i<(b->height*b->width); ++i) {
+       for (i = 0; i < size; ++i) {
                if ((i%8) == 0)
                        putchar ('\t');
                printf ("0x%02X,%c",