bmp转logo的代码

    技术2022-05-13  13

    uboot中bmp转logo的代码,结合下面的资料,很容易理解,作此备份:

    + expand source view plain copy to clipboard print ? ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150 /*    * main.c    *    *  Created on: 2010-4-30    *      Author: admin    */       #include <stdio.h>    #include <stdlib.h>       #if defined(__linux__)    #include <stdint.h>    #else    #ifdef __CYGWIN__    #include "elf.h"    #else    #include <inttypes.h>    #endif    #endif       typedef   struct  bitmap_s {        /* bitmap description */        uint16_t width;       uint16_t height;       uint8_t palette[256*3];       uint8_t *data;   } bitmap_t;      #define DEFAULT_CMAP_SIZE   16  /* size of default color map    */       /*    * Neutralize little endians.    */    uint16_t le_short(uint16_t x)   {       uint16_t val;       uint8_t *p = (uint8_t *)(&x);          val =  (*p++ & 0xff) << 0;       val |= (*p & 0xff) << 8;          return  val;   }      void  skip_bytes ( FILE  *fp,  int  n)   {       while  (n-- > 0)           fgetc (fp);   }      int  main ( int  argc,  char  *argv[])   {       int  i, x;       FILE     *fp;       bitmap_t bmp;       bitmap_t *b = &bmp;       uint16_t data_offset, n_colors;          if  (argc < 2) {           fprintf (stderr, "Usage: %s file/n" , argv[0]);           exit (EXIT_FAILURE);       }          if  ((fp = fopen (argv[1],  "rb" )) == NULL) {           perror (argv[1]);           exit (EXIT_FAILURE);       }          if  (fgetc (fp) !=  'B'  || fgetc (fp) !=  'M' ) {           fprintf (stderr, "%s is not a bitmap file./n" , argv[1]);           exit (EXIT_FAILURE);       }          /*        * read width and height of the image, and the number of colors used;        * ignore the rest        */        skip_bytes (fp, 8);       fread (&data_offset, sizeof  (uint16_t), 1, fp);       skip_bytes (fp, 6);       fread (&b->width,   sizeof  (uint16_t), 1, fp);       skip_bytes (fp, 2);       fread (&b->height,  sizeof  (uint16_t), 1, fp);       skip_bytes (fp, 22);       fread (&n_colors, sizeof  (uint16_t), 1, fp);       skip_bytes (fp, 6);          /*        * Repair endianess.        */        data_offset = le_short(data_offset);       b->width = le_short(b->width);       b->height = le_short(b->height);       n_colors = le_short(n_colors);          /* assume we are working with an 8-bit file */        if  ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {           /* reserve DEFAULT_CMAP_SIZE color map entries for default map */            n_colors = 256 - DEFAULT_CMAP_SIZE;       }          printf ("/*/n"            " * Automatically generated by /"tools/bmp_logo/"/n"            " */n"            " * DO NOT EDIT/n"            " */n"            " *//n/n/n"            "#ifndef __BMP_LOGO_H__/n"            "#define __BMP_LOGO_H__/n/n"            "#define BMP_LOGO_WIDTH/t/t%d/n"            "#define BMP_LOGO_HEIGHT/t/t%d/n"            "#define BMP_LOGO_COLORS/t/t%d/n"            "#define BMP_LOGO_OFFSET/t/t%d/n"            "/n" ,           b->width, b->height, n_colors,           DEFAULT_CMAP_SIZE);          /* allocate memory */        if  ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL) {           fclose (fp);           printf ("Error allocating memory for file %s./n" , argv[1]);           exit (EXIT_FAILURE);       }          /* read and print the palette information */        printf ("unsigned short bmp_logo_palette[] = {/n" );          for  (i=0; i<n_colors; ++i) {           b->palette[(int )(i*3+2)] = fgetc(fp);           b->palette[(int )(i*3+1)] = fgetc(fp);           b->palette[(int )(i*3+0)] = fgetc(fp);           x=fgetc(fp);              printf ("%s0x0%X%X%X,%s" ,               ((i%8) == 0) ? "/t"  :  "  " ,               (b->palette[(int )(i*3+0)] >> 4) & 0x0F,               (b->palette[(int )(i*3+1)] >> 4) & 0x0F,               (b->palette[(int )(i*3+2)] >> 4) & 0x0F,               ((i%8) == 7) ? "/n"  :  ""            );       }          /* seek to offset indicated by file header */        fseek(fp, (long )data_offset, SEEK_SET);          /* 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) /                           + DEFAULT_CMAP_SIZE;           }       }       fclose (fp);          for  (i=0; i<(b->height*b->width); ++i) {           if  ((i%8) == 0)               putchar ('/t' );           printf ("0xX,%c" ,               b->data[i],               ((i%8) == 7) ? '/n'  :  ' '            );       }       printf ("/n"            "};/n/n"            "#endif /* __BMP_LOGO_H__ *//n"        );          return  (0);   }  

    /* * main.c * * Created on: 2010-4-30 * Author: admin */ #include <stdio.h> #include <stdlib.h> #if defined(__linux__) #include <stdint.h> #else #ifdef __CYGWIN__ #include "elf.h" #else #include <inttypes.h> #endif #endif typedef struct bitmap_s { /* bitmap description */ uint16_t width; uint16_t height; uint8_t palette[256*3]; uint8_t *data; } bitmap_t; #define DEFAULT_CMAP_SIZE 16 /* size of default color map */ /* * Neutralize little endians. */ uint16_t le_short(uint16_t x) { uint16_t val; uint8_t *p = (uint8_t *)(&x); val = (*p++ & 0xff) << 0; val |= (*p & 0xff) << 8; return val; } void skip_bytes (FILE *fp, int n) { while (n-- > 0) fgetc (fp); } int main (int argc, char *argv[]) { int i, x; FILE *fp; bitmap_t bmp; bitmap_t *b = &bmp; uint16_t data_offset, n_colors; if (argc < 2) { fprintf (stderr, "Usage: %s file/n", argv[0]); exit (EXIT_FAILURE); } if ((fp = fopen (argv[1], "rb")) == NULL) { perror (argv[1]); exit (EXIT_FAILURE); } if (fgetc (fp) != 'B' || fgetc (fp) != 'M') { fprintf (stderr, "%s is not a bitmap file./n", argv[1]); exit (EXIT_FAILURE); } /* * read width and height of the image, and the number of colors used; * ignore the rest */ skip_bytes (fp, 8); fread (&data_offset, sizeof (uint16_t), 1, fp); skip_bytes (fp, 6); fread (&b->width, sizeof (uint16_t), 1, fp); skip_bytes (fp, 2); fread (&b->height, sizeof (uint16_t), 1, fp); skip_bytes (fp, 22); fread (&n_colors, sizeof (uint16_t), 1, fp); skip_bytes (fp, 6); /* * Repair endianess. */ data_offset = le_short(data_offset); b->width = le_short(b->width); b->height = le_short(b->height); n_colors = le_short(n_colors); /* assume we are working with an 8-bit file */ if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) { /* reserve DEFAULT_CMAP_SIZE color map entries for default map */ n_colors = 256 - DEFAULT_CMAP_SIZE; } printf ("/*/n" " * Automatically generated by /"tools/bmp_logo/"/n" " */n" " * DO NOT EDIT/n" " */n" " *//n/n/n" "#ifndef __BMP_LOGO_H__/n" "#define __BMP_LOGO_H__/n/n" "#define BMP_LOGO_WIDTH/t/t%d/n" "#define BMP_LOGO_HEIGHT/t/t%d/n" "#define BMP_LOGO_COLORS/t/t%d/n" "#define BMP_LOGO_OFFSET/t/t%d/n" "/n", b->width, b->height, n_colors, DEFAULT_CMAP_SIZE); /* allocate memory */ if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL) { fclose (fp); printf ("Error allocating memory for file %s./n", argv[1]); exit (EXIT_FAILURE); } /* read and print the palette information */ printf ("unsigned short bmp_logo_palette[] = {/n"); for (i=0; i<n_colors; ++i) { b->palette[(int)(i*3+2)] = fgetc(fp); b->palette[(int)(i*3+1)] = fgetc(fp); b->palette[(int)(i*3+0)] = fgetc(fp); x=fgetc(fp); printf ("%s0x0%X%X%X,%s", ((i%8) == 0) ? "/t" : " ", (b->palette[(int)(i*3+0)] >> 4) & 0x0F, (b->palette[(int)(i*3+1)] >> 4) & 0x0F, (b->palette[(int)(i*3+2)] >> 4) & 0x0F, ((i%8) == 7) ? "/n" : "" ); } /* seek to offset indicated by file header */ fseek(fp, (long)data_offset, SEEK_SET); /* 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) / + DEFAULT_CMAP_SIZE; } } fclose (fp); for (i=0; i<(b->height*b->width); ++i) { if ((i%8) == 0) putchar ('/t'); printf ("0xX,%c", b->data[i], ((i%8) == 7) ? '/n' : ' ' ); } printf ("/n" "};/n/n" "#endif /* __BMP_LOGO_H__ *//n" ); return (0); }

    位图头

    这部分是识别信息,典型的应用程序会首先普通读取这部分数据以确保的确是位图文件并且没有损坏。

    字节 #0-1 保存位图文件的标识符,这两个字节的典型数据是BM 。 字节 #2-5 使用一个dword 保存位图文件大小。 字节 #6-9 是保留部分,留做以后的扩展使用,对实际的解码格式没有影响。 字节 #10-13 保存位图数据位置的地址偏移 ,也就是起始地址。

    位图信息

    这部分告诉应用程序图像的详细信息,在屏幕上显示图像将会使用这些信息,它从文件的第15个字节开始。

    字节 #14-17 定义以下用来描述影像的区块(BitmapInfoHeader)的大小。它的值是:40 - Windows 3.2、95、NT、12 - OS/2 1.x、240 - OS/2 2.x 字节 #18-21 保存位图宽度(以像素个数表示)。 字节 #22-25 保存位图高度(以像素个数表示)。 字节 #26-27 保存所用彩色位面的个数。不经常使用。 字节 #28-29 保存每个像素的位数,它是图像的颜色深度。常用值是1、4、8(灰阶)和24(彩色)。 字节 #30-33 定义所用的压缩算法。允许的值是0、1、2、3、4、5。 0 - 没有压缩(也用BI_RGB表示) 1 - 行程长度编码 8位/像素(也用BI_RLE8表示) 2 - 行程长度编码4位/像素(也用BI_RLE4表示) 3 - Bit field (也用BI_BITFIELDS表示) 4 - JPEG图像(也用BI_JPEG表示) 5 - PNG图像(也用BI_PNG表示)

    然而,由于大多数位图文件都是不压缩的,所以最常用的值是0。

    字节 #34-37 保存图像大小。这是原始(:en:raw)位图数据的大小,不要与文件大小混淆。 字节 #38-41 保存图像水平方向分辨率。 字节 #42-45 保存图像竖值方向分辨率。 字节 #46-49 保存所用颜色数目。 字节 #50-53 保存所用重要颜色数目。当每个颜色都重要时这个值与颜色数目相等。

    调色板

    这部分定义了图像中所用的颜色。如上所述,位图图像一个像素接着一个像素存储,每个像素使用一个或者多个字节的值表示,所以调色板的目的就是要告诉应用程序这些值所对应的实际颜色。

    典型的位图文件使用RGB 彩色模型。在这种模型中,每种颜色都是由不同强度(从0到最大强度)的红色(R)、绿色(G)和蓝色(B)组成的,也就是说,每种颜色都可以使用红色、绿色和蓝色的值所定义。

    在位图文件的实现中,调色板可以包含很多条目,条目个数就是图像中所使用的颜色的个数。每个条目包含4个字节:其中三个表示红色、绿色和蓝色,第四 个字节没有使用(大多数应用程序将它设为0)。对于每个字节,数值0表示相应的颜色在当前的图像文件中没有使用,而数值255表示那个颜色使用最大的强 度。

    位图数据

    这部分逐个像素表示图像。像素是从下到上、从左到右保存的。每个像素使用一个或者多个字节表示。如果一个图像水平线的字节数不是4的倍数,这行就使用空字节 补齐,通常是ASCII 码0。

    范例: 有一张5*5的图片,应该会有25个pixels,但是因为5不是4的倍数所以会显示成: xxxxx000 xxxxx000 xxxxx000 xxxxx000 xxxxx000

    x代表调色盘的编号 0代表Null_character

    有一张4*4的图片,应该会有16个pixels,但是因为是4的倍数所以会显示成:

    xxxx xxxx xxxx xxxx


    最新回复(0)