Bitmap文件格式+生成一个BMP文件
Bitmap的文件格式:
#define UINT16 unsigned short
#define DWORD unsigned int
#define WORD short
#define LONG int // Bitmap File Header ( 14 Bytes )
typedef struct tagBITMAPFILEHEADER
{
UINT16 bfType; // same as BM in ASCII.
DWORD bfSize; // the size of the BMP file in bytes
UINT16 bfReserved1;
UINT16 bfReserved2;
DWORD bfOffBits; // starting address, of the byte where the bitmap image data (Pixel Array) can be found.
} BITMAPFILEHEADER, *PBITMAPFILEHEADER; // DIB头描述bitmap的信息,包括大小、压缩类型、颜色格式等,DIB头的大小根据版本不同,大小也不同。
typedef struct tagBITMAPINFOHEADER
{
DWORD biSize; // 4, the size of this header (40 bytes)
LONG biWidth; // 4, the bitmap width in pixels (signed integer).
LONG biHeight; // 4, the bitmap height in pixels (signed integer).
WORD biPlanes; // 2, the number of color planes being used. Must be set to 1.
WORD biBitCount; // 2, the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.
DWORD biCompression; // 4, the compression method being used.
DWORD biSizeImage; // 4, the image size. This is the size of the raw bitmap data, and should not be confused with the file size.
LONG biXPelsPerMeter; // 4, the horizontal resolution of the image.
LONG biYPelsPerMeter; // 4, the vertical resolution of the image.
DWORD biClrUsed; // 4, the number of colors in the color palette, or 0 to default to 2^n.
DWORD biClrImportant; // 4, the number of important colors used, or 0 when every color is important; generally ignored.
} BITMAPINFOHEADER;
生成一个BMP文件:
int bitmap_file_generate( char *name, int width, int height )
{
FILE *fp = NULL;
unsigned char *pData;
unsigned char *pp;
int i, j;
int iFileSize; unsigned char bfType1, bfType2;
unsigned int bfSize;
unsigned short bfReserved1, bfReserved2;
unsigned int bfOffBits;
unsigned int biSize;
int biWidth, biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
unsigned int biXPelsPerMeter;
unsigned int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant; // offset[0:1]
bfType1 = 'B';
bfType2 = 'M';
// offset[2:5]
bfSize = width * height * + ;
// offset[6:7]
bfReserved1 = ;
// offsset[8:9]
bfReserved2 = ;
// offset[10:13]
bfOffBits = ;
// offset[14:17]
biSize = ;
// offset[18:21]
biWidth = width;
// offset[22:25]
biHeight = height;
// offset[26:27]
biPlanes = ;
// offset[28:29]
biBitCount = ;
// offset[30:33]
biCompression = ;
// offset[34:37]
biSizeImage = width * height * ;
// offset[38:41]
biXPelsPerMeter = ;
// offset[42:45]
biYPelsPerMeter = ;
// offset[46:49]
biClrUsed = ;
// offset[50:53]
biClrImportant = ; fp = fopen(name, "wb+");
if( fp == NULL )
{
printf("create file err, fopen: %p\n", fp);
return -;
} pData = (unsigned char *)malloc(biWidth*biHeight*+);//malloc(iFileSize);
if(pData == NULL)
{
printf("pData malloc err\n");
return -;
} // same as BM in ASCII.
pData[] = bfType1;
pData[] = bfType2; // the size of the BMP file in bytes. bmp文件的实际大小
pData[] = (unsigned char)((bfSize>>) & 0xff);
pData[] = (unsigned char)((bfSize>>) & 0xff);
pData[] = (unsigned char)((bfSize>>) & 0xff);
pData[] = (unsigned char)((bfSize>>) & 0xff); // reserved
pData[] = (unsigned char)((bfReserved1>>) & 0xff);
pData[] = (unsigned char)((bfReserved1>>) & 0xff);
// reserved
pData[] = (unsigned char)((bfReserved2>>) & 0xff);
pData[] = (unsigned char)((bfReserved2>>) & 0xff); // the offset, i.e. starting address, of the byte where the bitmap image data (Pixel Array) can be found.
// 颜色数据的偏移地址,实际为:54 = 14 + 40
pData[] = (unsigned char)((bfOffBits>>) & 0xff);
pData[] = (unsigned char)((bfOffBits>>) & 0xff);
pData[] = (unsigned char)((bfOffBits>>) & 0xff);
pData[] = (unsigned char)((bfOffBits>>) & 0xff); // the size of this header (40 bytes)
pData[] = (unsigned char)((biSize>>) & 0xff);
pData[] = (unsigned char)((biSize>>) & 0xff);
pData[] = (unsigned char)((biSize>>) & 0xff);
pData[] = (unsigned char)((biSize>>) & 0xff); // the bitmap width in pixels (signed integer).
pData[] = (unsigned char)((biWidth>>) & 0xff);
pData[] = (unsigned char)((biWidth>>) & 0xff);
pData[] = (unsigned char)((biWidth>>) & 0xff);
pData[] = (unsigned char)((biWidth>>) & 0xff); // the bitmap height in pixels (signed integer).
pData[] = (unsigned char)((biHeight>>) & 0xff);
pData[] = (unsigned char)((biHeight>>) & 0xff);
pData[] = (unsigned char)((biHeight>>) & 0xff);
pData[] = (unsigned char)((biHeight>>) & 0xff); // the number of color planes being used. Must be set to 1.
pData[] = (unsigned char)((biPlanes>>) & 0xff);
pData[] = (unsigned char)((biPlanes>>) & 0xff); // the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.
pData[] = (unsigned char)((biBitCount>>) & 0xff);
pData[] = (unsigned char)((biBitCount>>) & 0xff); // the compression method being used. See the next table for a list of possible values.
pData[] = (unsigned char)((biCompression>>) & 0xff);
pData[] = (unsigned char)((biCompression>>) & 0xff);
pData[] = (unsigned char)((biCompression>>) & 0xff);
pData[] = (unsigned char)((biCompression>>) & 0xff); // the image size. This is the size of the raw bitmap data, and should not be confused with the file size.
pData[] = (unsigned char)((biSizeImage>>) & 0xff);
pData[] = (unsigned char)((biSizeImage>>) & 0xff);
pData[] = (unsigned char)((biSizeImage>>) & 0xff);
pData[] = (unsigned char)((biSizeImage>>) & 0xff); // the horizontal resolution of the image. (pixel per meter, signed integer)
pData[] = (unsigned char)((biXPelsPerMeter>>) & 0xff);
pData[] = (unsigned char)((biXPelsPerMeter>>) & 0xff);
pData[] = (unsigned char)((biXPelsPerMeter>>) & 0xff);
pData[] = (unsigned char)((biXPelsPerMeter>>) & 0xff); // the vertical resolution of the image. (pixel per meter, signed integer)
pData[] = (unsigned char)((biYPelsPerMeter>>) & 0xff);
pData[] = (unsigned char)((biYPelsPerMeter>>) & 0xff);
pData[] = (unsigned char)((biYPelsPerMeter>>) & 0xff);
pData[] = (unsigned char)((biYPelsPerMeter>>) & 0xff); // the number of colors in the color palette, or 0 to default to 2^n.
pData[] = (unsigned char)((biClrUsed>>) & 0xff);
pData[] = (unsigned char)((biClrUsed>>) & 0xff);
pData[] = (unsigned char)((biClrUsed>>) & 0xff);
pData[] = (unsigned char)((biClrUsed>>) & 0xff); // the number of important colors used, or 0 when every color is important; generally ignored.
pData[] = (unsigned char)((biClrImportant>>) & 0xff);
pData[] = (unsigned char)((biClrImportant>>) & 0xff);
pData[] = (unsigned char)((biClrImportant>>) & 0xff);
pData[] = (unsigned char)((biClrImportant>>) & 0xff); pp = &pData[];
for(i = ; i < biHeight; i++)
{
for(j = ; j < biWidth; j++)
{
set_color(pp, biWidth, biHeight, i, j, i+j, i, j); // 随便填有点颜色
}
} iFileSize = fwrite(pData, , bfSize, fp);
printf("generate file iFileSize: %d\n", iFileSize); free(pData); fclose(fp); return ;
}
以上生成的BMP,坐标起始位置(0,0)为左下角,填充颜色:
void set_color( unsigned char *pp, int w, int h,
int x, int y, unsigned char r, unsigned char g, unsigned char b )
{
pp[y*w*+x*+] = (unsigned char)r;
pp[y*w*+x*+] = (unsigned char)g;
pp[y*w*+x*+] = (unsigned char)b;
}
生成一个,~~~~(>_<)~~~~:
int main(int argc, char *argv[])
{
int ret;
char *file_name; if( argc != )
{
printf("please input bitmap file_name\n");
printf("usage: %s bitmap_file_name\n", argv[]);
return ;
} file_name = argv[]; ret = bitmap_file_generate(file_name, *, *); printf("bitmap_file_generate, ret: %d\n", ret); return ;
}
Bitmap文件格式+生成一个BMP文件的更多相关文章
- 用php生成一个excel文件(原理)
1.我们用php来生成一个excel文档来讲述其原理: excel2007里面的文档目录组成部分为: 2.我们使用ZipArchive()方法来生成一个简易的excel文件. 使用方法: 3.代码如下 ...
- Log4j使用笔记:每天生成一个日志文件、按日志大小生成文件
其中TestLog4j.java如下: package cn.zhoucy.test; import org.apache.log4j.Logger; public class TestLog4j { ...
- 非正常关闭vi编辑器时会生成一个.swp文件
非正常关闭vi编辑器时会生成一个.swp文件 关于swp文件 使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一个文件,vi就会生成这么一个.(filename)swp文件以备 ...
- python excel操作 练习:#生成一个excel文件,生成3个sheet,每个sheet的a1写一下sheet的名称。每个sheet有个底色
练习:#生成一个excel文件,生成3个sheet,每个sheet的a1写一下sheet的名称.每个sheet有个底色 #coding=utf-8 from openpyxl import Workb ...
- 原创:无错版!让DEDE只生成一个RSS文件,不分栏目
DEDE为每一个栏目都独立创建一个rss文件, 如果用户要整站订阅相当不方便. 所以需要修改让dede只生成一个rss. 网上大部分帖子要么是抄, 要么是有问题少了步骤. 今天特意整理下. 分享.. ...
- Visual Studio 2012网站如何只生成一个DLL文件
简介: 在Visual Studio 2005,2008,2010版本中,都有Web Deployment工具将网站进行发布,所有代码文件和库文件发布,生成为一个动态链接库文件,而在Visual St ...
- salesforce 零基础学习(五十三)多个文件生成一个zip文件(使用git上封装的代码)
此篇参考git代码:https://github.com/pdalcol/Zippex 学习salesforce可以访问一个朋友的网站:https://www.xgeek.net 首先感谢git上提供 ...
- logback配置每天生成一个日志文件,保存30天的日志文件
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 文件输出格 ...
- log4j配置每天生成一个日志文件
首先需要配置web.xml里面: <servlet-name>log4j-init</servlet-name> <servlet-class>com.free.c ...
随机推荐
- URL优化之IIS7如何开启伪静态
iis7跟IIS6开启伪静态重写的方式不一样,iis6是在网站属性里面的ISAPI筛选器里面添加,但是iis7添加伪静态重写,需要下载一个url重写插件. II7/7.5用的是web.config配置 ...
- Python之路,Day7 - 面向对象编程进阶
本节内容: 面向对象高级语法部分 经典类vs新式类 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 经典类vs新式类 把下面代 ...
- HDU 3033 分组背包变形(每种至少一个)
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- IIC总线解析
IIC简介: IIC 即Inter-Integrated Circuit(集成电路总线),这种总线类型是由飞利浦半导体公司在八十年代初设计出来的,主要是用来连接整体电路(ICS) ,IIC是一种多向控 ...
- R 语言机器学习同步推进~
教材就是传说中的机器学习和R语言--中文版,大家可以去图书馆借来看看~~~,例子都是来自书上的 首先介绍一下KNN算法,KNN还好吧,说白了就是一个算距离的公式然后以统计的方式呈现出来,以二维平面为例 ...
- 第五篇.Bootstrap 排版
使用bootstrap的排版特性可以创建标题,段落,列表及其它内联元素. 标题:bootstrap中定义了从h1-h6的六种标题样式. 内联子标题: 如果需要向任意一个标题添加一个子标题,只需要加上& ...
- leetcode 日记 162. Find Peak Element java python
根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个 ...
- hibernate 连接oracle数据库的配置 (参考)
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC &qu ...
- red hat enterprise 6安装tftp服务
1--->检查是否安装tftp rpm -qa tftp* 2--->安装tftp yum install -y tftp-server 3--->chkconfig --list| ...
- git pull错误
1. Pull is not possible because you have unmerged files. 症状:pull的时候 $ git pull Pull is not possible ...