2 * Basic BMP data process and Raw picture data handle functions.
3 * Could be used to adjust pixel data format, get information, etc.
5 * Copyright (C) 2011 Wayne Xia <xiawenc@cn.ibm.com>
7 * This work is licensed under the terms of the GNU LGPLv3.
9 #include "malloc.h" // malloc_tmphigh
10 #include "string.h" // memcpy
11 #include "util.h" // struct bmp_decdata
14 struct tagRGBQUAD *quadp;
21 #define bmp_load4byte(addr) (*(u32 *)(addr))
22 #define bmp_load2byte(addr) (*(u16 *)(addr))
24 typedef struct tagBITMAPFILEHEADER {
30 } BITMAPFILEHEADER, tagBITMAPFILEHEADER;
32 typedef struct tagBITMAPINFOHEADER {
40 u8 biXPelsPerMeter[4];
41 u8 biYPelsPerMeter[4];
44 } BITMAPINFOHEADER, tagBITMAPINFOHEADER;
46 typedef struct tagRGBQUAD {
51 } RGBQUAD, tagRGBQUAD;
53 /* flat picture data adjusting function
55 * switch the vertical line sequence
56 * arrange horizontal pixel data, add extra space in the dest buffer
59 static void raw_data_format_adjust_24bpp(u8 *src, u8 *dest, int width,
60 int height, int bytes_per_line_dest)
62 int bytes_per_line_src = 3 * width;
64 for (i = 0 ; i < height ; i++) {
65 memcpy(dest + i * bytes_per_line_dest,
66 src + (height - 1 - i) * bytes_per_line_src, bytes_per_line_src);
70 /* allocate decdata struct */
71 struct bmp_decdata *bmp_alloc(void)
73 struct bmp_decdata *bmp = malloc_tmphigh(sizeof(*bmp));
77 /* extract information from bmp file data */
78 int bmp_decode(struct bmp_decdata *bmp, unsigned char *data, int data_size)
83 u16 bmp_filehead = bmp_load2byte(data + 0);
84 if (bmp_filehead != 0x4d42)
86 u32 bmp_recordsize = bmp_load4byte(data + 2);
87 if (bmp_recordsize != data_size)
89 u32 bmp_dataoffset = bmp_load4byte(data + 10);
90 bmp->datap = (unsigned char *)data + bmp_dataoffset;
91 bmp->width = bmp_load4byte(data + 18);
92 bmp->height = bmp_load4byte(data + 22);
93 bmp->bpp = bmp_load2byte(data + 28);
97 /* get bmp properties */
98 void bmp_get_size(struct bmp_decdata *bmp, int *width, int *height)
101 *height = bmp->height;
104 /* flush flat picture data to *pc */
105 int bmp_show(struct bmp_decdata *bmp, unsigned char *pic, int width
106 , int height, int depth, int bytes_per_line_dest)
108 if (bmp->datap == pic)
110 /* now only support 24bpp bmp file */
111 if ((depth == 24) && (bmp->bpp == 24)) {
112 raw_data_format_adjust_24bpp(bmp->datap, pic, width, height,
113 bytes_per_line_dest);