RAW生成bmg图片

bmp 格式

百度百科上的介绍:
BMP是英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式,能够被多种Windows应用程序所支持。随着Windows操作系统的流行与丰富的Windows应用程序的开发,BMP位图格式理所当然地被广泛应用。这种格式的特点是包含的图像信息较丰富,几乎不进行压缩,但由此导致了它与生俱生来的缺点–占用磁盘空间过大。所以,目前BMP在单机上比较流行。
由于的是没有经过处理的源数据文件,所以可以直接将raw 数据加上一个头部信息实现bmp文件的转化。
#bmp 头
typedef struct tagBITMAPFILEHEADER {
/* bmfh /
UINT bfType;
DWORD bfSize;
UINT bfReserved1;
UINT bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFO {
/
bmi /
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO;
typedef struct tagBITMAPINFO {
/
bmi /
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO;
其中:
bmiHeader
说明BITMAPINFOHEADER结构,其中包含了有关位图的尺寸及位格式等信息
bmiColors
说明彩色表RGBQUAD结构的阵列,其中包含索引图像的真实RGB值。
BITMAPINFOHEADER结构包含有位图文件的大小、压缩类型和颜色格式,其结构定义为:
typedef struct tagBITMAPINFOHEADER {
/
bmih */
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;

RAW 数据

数据结尾添加00 00。

程序

#include 
#include 
#include 
#include 
#include 
#include 

#define uint8_t unsigned char
#define int32_t int
int saveRawToBmp(uint8_t *pBuf, int32_t width, int32_t height, const char *pFileName)
{
    char heard[54] = { \
            0x42,0x4d,0x30,0x0C,0x01,0x00,0x00,0x00,0x00,0x00, \
            0x36,0x04,0x00,0x00,0x28,0x00,0x00,0x00,0xF5,0x00, \
            0x00,0x00,0x46,0x00,0x00,0x00,0x01,0x00,0x08,0x00, \
            0x00,0x00,0x00,0x00,0xF8,0x0b,0x01,0x00,0x12,0x0b, \
            0x00,0x00,0x12,0x0b,0x00,0x00,0x00,0x00,0x00,0x00, \
            0x00,0x00,0x00,0x00 };
    char patte[1024] = {0};
    char end[2] = {0, 0};
    char color = 0;
    int32_t w = width;
    int32_t h = height;
    int32_t size = w * h;
    int32_t allsize = size + 1080;
    int32_t pos = 0;
    int32_t idx = 0;
    int32_t ret = 0;
    FILE *pFp = NULL;
 
    do
    {
        if ((NULL == pBuf) || (NULL == pFileName))
        {
            ret = -1;
            break;
        }
        if ((width < 0) || (height < 0))
        {
            ret = -2;
            break;
        }
 
        heard[2] = allsize & 0xFF;
        heard[3] = (allsize >> 8) & 0xFF;
        heard[4] = (allsize >> 16) & 0xFF;
        heard[5] = (allsize >> 24) & 0xFF;
 
        heard[18] = w & 0xFF;
        heard[19] = (w >> 8) & 0xFF;
        heard[20] = (w >> 16) & 0xFF;
        heard[21] = (w >> 24) & 0xFF;
 
        heard[22] = h & 0xFF;
        heard[23] = (h >> 8) & 0xFF;
        heard[24] = (h >> 16)  & 0xFF;
        heard[25] = (h >> 24) & 0xFF;
 
        allsize -= 1078;
        heard[34] = allsize & 0xFF;
        heard[35] = (allsize >> 8) & 0xFF;
        heard[36] = (allsize >>  16) & 0xFF;
        heard[37] = (allsize >> 24) &  0xFF;
 
        for (idx = 0; idx < 1024; idx += 4)
        {
            patte[pos++] = color;
            patte[pos++] = color;
            patte[pos++] = color;
            patte[pos++] = 0;
            color++;
        }
 
        //====== Write raw data ======
        pFp = fopen(pFileName, "wb");
        if (NULL == pFp)
        {
            ret = -3;
            break;
        }
 
        fwrite(heard, 1, sizeof(heard), pFp);
        fwrite(patte, 1, sizeof(patte), pFp);
#if 1
        for (idx = height - 1; idx >= 0; idx--)
        {
            fwrite(&pBuf[idx * width], sizeof(uint8_t) * width, 1, pFp);
        }
#else
        fwrite(buf, 1, size, pFp);
#endif
        fwrite(end, 1, sizeof(end), pFp);
 
        fflush(pFp);
        if (0 != fsync(fileno(pFp)))
        {
            ret = -4;
            break;
        }
    } while (0);
 
    if (NULL != pFp)
    {
        fclose(pFp);
        pFp = NULL;
    }
 
    return ret;
}
#define L_COUNT 120
#define H_COUNT 128
#define TOTAL 81920
void main()
{
	unsigned char image[TOTAL] = {0};
	int l_count,h_count,t_count;
	t_count = 0;

	char file_name[] = "1.bmp";
	unsigned char data;
	unsigned short temp_data,temp_date;
	temp_data = 0;
	for(l_count = 0;l_count 

#效果
RAW生成bmg图片_第1张图片
生成一个逐行的渐变图。

你可能感兴趣的:(工具,windows)