#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIDTH 80
#define HEIGHT 60
#define BIBitCOUNT 8
#define FOUR_BYTES_ALIGN(width) ((((width*BIBitCOUNT)+31)>>5)<<2)
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned int dword;
#pragma pack(push,2) //保持2字节对齐
//位图文件头
typedef struct
{
word bfType; //文件类型
dword bfSize; //文件大小,字节为单位
word bfReserved1; //保留,必须为0
word bfReserved2; //保留,必须为0
dword bfOffBits; //从文件头开始的偏移量
}BitMapFileHeader;
//文图信息头
typedef struct
{
//bmp info head
dword biSize; //该结构的大小
dword biWidth; //图像的宽度,以像素为单位
dword biHeight; //图像的高度,以像素为单位
word biPlanes; //为目标设备说明位面数,其值总是设为1
word biBitCount; //说明比特数/像素
dword biCompression; //图像数据压缩类型
dword biSizeImage; //图像大小,以字节为单位
dword biXPelsPerMeter; //水平分辨率,像素/米
dword biYPelsPerMeter; //垂直分辨率,同上
dword biClrUsed; //位图实际使用的彩色表中的颜色索引数
dword biClrImportant; //对图像显示有重要影响的颜色索引的数目
}BitMapInfoHeader;
//调色板信息
typedef struct
{
byte rgbBlue; //该颜色的蓝色分量
byte rgbGreen; //该颜色的绿色分量
byte rgbRed; //该颜色的红色分量
byte rgbReserved; //保留值
}RGBQuad;
#pragma pack(pop)
BitMapFileHeader biFileHeader;
BitMapInfoHeader biInfoHeader;
RGBQuad rgbQuad[256];
FILE *fd; //定义一个文件类型的指针
/*************************************************************
*初始化bmp文件头部,设置bmp图片
**************************************************************/
void init_bmp_head(void)
{
biFileHeader.bfType = 0x4D42; //文件类型
biFileHeader.bfSize = FOUR_BYTES_ALIGN(WIDTH)*HEIGHT + 54 + 256*sizeof(rgbQuad); //文件大小,字节为单位
biFileHeader.bfReserved1 = 0x0; //保留,必须为0
biFileHeader.bfReserved2 = 0x0; //保留,必须为0
biFileHeader.bfOffBits = 54 + 256*sizeof(RGBQuad); //从文件头开始的偏移量
//bmp info head
biInfoHeader.biSize = 0x28; //该结构的大小
biInfoHeader.biWidth = WIDTH; //图像的宽度,以像素为单位
biInfoHeader.biHeight = HEIGHT; //图像的高度,以像素为单位
biInfoHeader.biPlanes = 0x01; //为目标设备说明位面数,其值总是设为1
biInfoHeader.biBitCount = BIBitCOUNT; //说明比特数/像素
biInfoHeader.biCompression = 0; //图像数据压缩类型
biInfoHeader.biSizeImage = 0;//FOUR_BYTES_ALIGN(WIDTH)*HEIGHT; //图像大小,以字节为单位
biInfoHeader.biXPelsPerMeter = 0x60; //水平分辨率,像素/米
biInfoHeader.biYPelsPerMeter = 0x60; //垂直分辨率,同上
biInfoHeader.biClrUsed = 0; //位图实际使用的彩色表中的颜色索引数
biInfoHeader.biClrImportant = 0; //对图像显示有重要影响的颜色索引的数目
}
int main(void)
{
static char *file_name = "hello.bmp"; //保存文件名的指针
int i, j;
unsigned char *data;
unsigned char *tmp;
init_bmp_head();
for(i=0; i<256; i++)
rgbQuad[i].rgbBlue = rgbQuad[i].rgbGreen = rgbQuad[i].rgbRed = i;
tmp = (unsigned char *)malloc(sizeof(char)*WIDTH*HEIGHT); //申请一段内存
data = (unsigned char *)malloc(sizeof(char)*WIDTH*HEIGHT);
memset(data, 255, sizeof(char)*WIDTH*HEIGHT);
memset(tmp, 255, sizeof(char)*WIDTH*HEIGHT);
// for(i=0; i<WIDTH*HEIGHT; i++)
// {
// tmp[i] = random()%255;
// }
for(i=HEIGHT-1; i>0; i--)
{
for(j=0; j<WIDTH; j++)
{
data[i*WIDTH+j] = tmp[(HEIGHT-1-i)*WIDTH + j];
}
}
fd = fopen(file_name, "wb");
fwrite(&biFileHeader, sizeof(biFileHeader), 1, fd);
fwrite(&biInfoHeader, sizeof(biInfoHeader), 1, fd);
fwrite(rgbQuad, sizeof(rgbQuad), 1, fd);
fwrite(data, WIDTH*HEIGHT, 1, fd);
free(data); //释放申请的内存(重要)
fclose(fd);
printf("Done success!!!\n");
return (0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
create_bmp.tar.gz_bmp 8bit_gray2bmp
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 90 浏览量
2022-09-20
23:55:41
上传
评论
收藏 45KB GZ 举报
温馨提示
共14个文件
mk:3个
bmp:2个
c:1个
create 8bit bmp picture
资源推荐
资源详情
资源评论
收起资源包目录
create_bmp.tar.gz (14个子文件)
create_bmp
main.c 4KB
.settings
language.settings.xml 3KB
.project 762B
.cproject 15KB
main 9KB
Debug
sources.mk 390B
subdir.mk 640B
hello.bmp 6KB
objects.mk 231B
makefile 962B
main.d 18B
main.o 71KB
create_bmp 39KB
test1.bmp 54B
共 14 条
- 1
资源评论
weixin_42653672
- 粉丝: 93
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功