这个算是上一篇文章的逆过程,就是给定一系列点的RGB值,然后输出一幅bmp位图。代码也是网上找的,独立性比较强,需要使用时直接把代码贴上去就可以了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long BOOL;
typedef long LONG;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
typedef unsigned short WORD;
#include <PshPack2.h>
typedef struct {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BMPFILEHEADER_T;
struct BMPFILEHEADER_S{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
};
typedef struct{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BMPINFOHEADER_T;
void Snapshot( BYTE * pData, int width, int height, char * filename )
{
int size = width*height*3; // 每个像素点3个字节
// 位图第一部分,文件信息
BMPFILEHEADER_T bfh;
bfh.bfType = 0x4d42; //bm
bfh.bfSize = size // data size
+ sizeof( BMPFILEHEADER_T ) // first section size
+ sizeof( BMPINFOHEADER_T ) // second section size
;
bfh.bfReserved1 = 0; // reserved
bfh.bfReserved2 = 0; // reserved
bfh.bfOffBits = bfh.bfSize - size;
// 位图第二部分,数据信息
BMPINFOHEADER_T bih;
bih.biSize = sizeof(BMPINFOHEADER_T);
bih.biWidth = width;
bih.biHeight = height;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = 0;
bih.biSizeImage = size;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
FILE * fp = fopen( filename,"wb" );
if( !fp ) return;
fwrite( &bfh, 1, sizeof(BMPFILEHEADER_T), fp );
fwrite( &bih, 1, sizeof(BMPINFOHEADER_T), fp );
fwrite( pData, 1, size, fp );
fclose( fp );
}
void main()
{
int buffer[256*3]={
0,0,0,
0,0,7,
0,0,15,
0,0,23,
0,0,31,
0,0,39,
0,0,47,
0,0,55,
0,0,63,
0,0,71,
0,0,79,
0,0,87,
0,0,95,
0,0,103,
0,0,111,
0,0,119,
0,0,127,
0,0,135,
0,0,143,
0,0,151,
0,0,159,
0,0,167,
0,0,175,
0,0,183,
0,0,191,
0,0,199,
0,0,207,
0,0,215,
0,0,223,
0,0,231,
0,0,239,
0,0,247,
0,0,255,
0,7,255,
0,15,255,
0,23,255,
0,31,255,
0,39,255,
0,47,255,
0,55,255,
0,63,255,
0,71,255,
0,79,255,
0,87,255,
0,95,255,
0,103,255,
0,111,255,
0,119,255,
0,127,255,
0,135,255,
0,143,255,
0,151,255,
0,159,255,
0,167,255,
0,175,255,
0,183,255,
0,191,255,
0,199,255,
0,207,255,
0,215,255,
0,223,255,
0,231,255,
0,239,255,
0,247,255,
0,255,255,
0,255,247,
0,255,239,
0,255,231,
0,255,223,
0,255,215,
0,255,207,
0,255,199,
0,255,191,
0,255,183,
0,255,175,
0,255,167,
0,255,159,
0,255,151,
0,255,143,
0,255,135,
0,255,127,
0,255,119,
0,255,111,
0,255,103,
0,255,95,
0,255,87,
0,255,79,
0,255,71,
0,255,63,
0,255,55,
0,255,47,
0,255,39,
0,255,31,
0,255,23,
0,255,15,
0,255,7,
0,255,0,
7,255,0,
15,255,0,
23,255,0,
31,255,0,
39,255,0,
47,255,0,
55,255,0,
63,255,0,
71,255,0,
79,255,0,
87,255,0,
95,255,0,
103,255,0,
111,255,0,
119,255,0,
127,255,0,
135,255,0,
143,255,0,
151,255,0,
159,255,0,
167,255,0,
175,255,0,
183,255,0,
191,255,0,
199,255,0,
207,255,0,
215,255,0,
223,255,0,
231,255,0,
239,255,0,
247,255,0,
255,255,0,
255,251,0,
255,247,0,
255,243,0,
255,239,0,
255,235,0,
255,231,0,
255,227,0,
255,223,0,
255,219,0,
255,215,0,
255,211,0,
255,207,0,
255,203,0,
255,199,0,
255,195,0,
255,191,0,
255,187,0,
255,183,0,
255,179,0,
255,175,0,
255,171,0,
255,167,0,
255,163,0,
255,159,0,
255,155,0,
255,151,0,
255,147,0,
255,143,0,
255,139,0,
255,135,0,
255,131,0,
255,127,0,
255,123,0,
255,119,0,
255,115,0,
255,111,0,
255,107,0,
255,103,0,
255,99,0,
255,95,0,
255,91,0,
255,87,0,
255,83,0,
255,79,0,
255,75,0,
255,71,0,
255,67,0,
255,63,0,
255,59,0,
255,55,0,
255,51,0,
255,47,0,
255,43,0,
255,39,0,
255,35,0,
255,31,0,
255,27,0,
255,23,0,
255,19,0,
255,15,0,
255,11,0,
255,7,0,
255,3,0,
255,0,0,
255,4,4,
255,8,8,
255,12,12,
255,16,16,
255,20,20,
255,24,24,
255,28,28,
255,32,32,
255,36,36,
255,40,40,
255,44,44,
255,48,48,
255,52,52,
255,56,56,
255,60,60,
255,64,64,
255,68,68,
255,72,72,
255,76,76,
255,80,80,
255,85,85,
255,89,89,
255,93,93,
255,97,97,
255,101,101,
255,105,105,
255,109,109,
255,113,113,
255,117,117,
255,121,121,
255,125,125,
255,129,129,
255,133,133,
255,137,137,
255,141,141,
255,145,145,
255,149,149,
255,153,153,
255,157,157,
255,161,161,
255,165,165,
255,170,170,
255,174,174,
255,178,178,
255,182,182,
255,186,186,
255,190,190,
255,194,194,
255,198,198,
255,202,202,
255,206,206,
255,210,210,
255,214,214,
255,218,218,
255,222,222,
255,226,226,
255,230,230,
255,234,234,
255,238,238,
255,242,242,
255,246,246,
255,250,250,
255,255,255
};
struct {
BYTE b;
BYTE g;
BYTE r;
} pRGB[16][16]; // 定义位图数据,高,宽
memset( pRGB, 0, sizeof(pRGB) ); // 设置背景为黑色
// 在中间画一个16*16的矩形
for (int i=0;i<16;i++)
{
for (int j=0;j<16;j++)
{
pRGB[i][j].r=buffer[((15-i)*16+j)*3+0];
pRGB[i][j].g=buffer[((15-i)*16+j)*3+1];
pRGB[i][j].b=buffer[((15-i)*16+j)*3+2];
}
}
// 生成BMP图片
Snapshot( ( BYTE*)pRGB, 16, 16, "rgb1.bmp");
}
主要就是各个像素点的RGB值不容易提供,我之前的需求是写一幅伪彩图。然后就在网上找到了一幅伪彩图的RGB值,就是上面的那个256*3的数组,共有256种颜色。
输出的图片如下:
图片比较小,毕竟长宽都只有16个像素。
源码下载:下载地址