Bitmap文件格式+生成一个BMP文件
Bitmap的文件格式:
1 #define UINT16 unsigned short 2 #define DWORD unsigned int 3 #define WORD short 4 #define LONG int 5 6 // Bitmap File Header ( 14 Bytes ) 7 typedef struct tagBITMAPFILEHEADER 8 { 9 UINT16 bfType; // same as BM in ASCII. 10 DWORD bfSize; // the size of the BMP file in bytes 11 UINT16 bfReserved1; 12 UINT16 bfReserved2; 13 DWORD bfOffBits; // starting address, of the byte where the bitmap image data (Pixel Array) can be found. 14 } BITMAPFILEHEADER, *PBITMAPFILEHEADER; 15 16 // DIB头描述bitmap的信息,包括大小、压缩类型、颜色格式等,DIB头的大小根据版本不同,大小也不同。 17 typedef struct tagBITMAPINFOHEADER 18 { 19 DWORD biSize; // 4, the size of this header (40 bytes) 20 LONG biWidth; // 4, the bitmap width in pixels (signed integer). 21 LONG biHeight; // 4, the bitmap height in pixels (signed integer). 22 WORD biPlanes; // 2, the number of color planes being used. Must be set to 1. 23 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. 24 DWORD biCompression; // 4, the compression method being used. 25 DWORD biSizeImage; // 4, the image size. This is the size of the raw bitmap data, and should not be confused with the file size. 26 LONG biXPelsPerMeter; // 4, the horizontal resolution of the image. 27 LONG biYPelsPerMeter; // 4, the vertical resolution of the image. 28 DWORD biClrUsed; // 4, the number of colors in the color palette, or 0 to default to 2^n. 29 DWORD biClrImportant; // 4, the number of important colors used, or 0 when every color is important; generally ignored. 30 } BITMAPINFOHEADER;
生成一个BMP文件:
1 int bitmap_file_generate( char *name, int width, int height ) 2 { 3 FILE *fp = NULL; 4 unsigned char *pData; 5 unsigned char *pp; 6 int i, j; 7 int iFileSize; 8 9 unsigned char bfType1, bfType2; 10 unsigned int bfSize; 11 unsigned short bfReserved1, bfReserved2; 12 unsigned int bfOffBits; 13 unsigned int biSize; 14 int biWidth, biHeight; 15 unsigned short biPlanes; 16 unsigned short biBitCount; 17 unsigned int biCompression; 18 unsigned int biSizeImage; 19 unsigned int biXPelsPerMeter; 20 unsigned int biYPelsPerMeter; 21 unsigned int biClrUsed; 22 unsigned int biClrImportant; 23 24 // offset[0:1] 25 bfType1 = 'B'; 26 bfType2 = 'M'; 27 // offset[2:5] 28 bfSize = width * height * 3 + 54; 29 // offset[6:7] 30 bfReserved1 = 0; 31 // offsset[8:9] 32 bfReserved2 = 0; 33 // offset[10:13] 34 bfOffBits = 54; 35 // offset[14:17] 36 biSize = 40; 37 // offset[18:21] 38 biWidth = width; 39 // offset[22:25] 40 biHeight = height; 41 // offset[26:27] 42 biPlanes = 1; 43 // offset[28:29] 44 biBitCount = 24; 45 // offset[30:33] 46 biCompression = 0; 47 // offset[34:37] 48 biSizeImage = width * height * 3; 49 // offset[38:41] 50 biXPelsPerMeter = 2835; 51 // offset[42:45] 52 biYPelsPerMeter = 2835; 53 // offset[46:49] 54 biClrUsed = 0; 55 // offset[50:53] 56 biClrImportant = 0; 57 58 59 fp = fopen(name, "wb+"); 60 if( fp == NULL ) 61 { 62 printf("create file err, fopen: %p\n", fp); 63 return -1; 64 } 65 66 pData = (unsigned char *)malloc(biWidth*biHeight*3+54);//malloc(iFileSize); 67 if(pData == NULL) 68 { 69 printf("pData malloc err\n"); 70 return -2; 71 } 72 73 // same as BM in ASCII. 74 pData[0] = bfType1; 75 pData[1] = bfType2; 76 77 // the size of the BMP file in bytes. bmp文件的实际大小 78 pData[2] = (unsigned char)((bfSize>>0) & 0xff); 79 pData[3] = (unsigned char)((bfSize>>8) & 0xff); 80 pData[4] = (unsigned char)((bfSize>>16) & 0xff); 81 pData[5] = (unsigned char)((bfSize>>24) & 0xff); 82 83 // reserved 84 pData[6] = (unsigned char)((bfReserved1>>0) & 0xff); 85 pData[7] = (unsigned char)((bfReserved1>>8) & 0xff); 86 // reserved 87 pData[8] = (unsigned char)((bfReserved2>>0) & 0xff); 88 pData[9] = (unsigned char)((bfReserved2>>0) & 0xff); 89 90 // the offset, i.e. starting address, of the byte where the bitmap image data (Pixel Array) can be found. 91 // 颜色数据的偏移地址,实际为:54 = 14 + 40 92 pData[10] = (unsigned char)((bfOffBits>>0) & 0xff); 93 pData[11] = (unsigned char)((bfOffBits>>8) & 0xff); 94 pData[12] = (unsigned char)((bfOffBits>>16) & 0xff); 95 pData[13] = (unsigned char)((bfOffBits>>24) & 0xff); 96 97 // the size of this header (40 bytes) 98 pData[14] = (unsigned char)((biSize>>0) & 0xff); 99 pData[15] = (unsigned char)((biSize>>8) & 0xff); 100 pData[16] = (unsigned char)((biSize>>16) & 0xff); 101 pData[17] = (unsigned char)((biSize>>24) & 0xff); 102 103 // the bitmap width in pixels (signed integer). 104 pData[18] = (unsigned char)((biWidth>>0) & 0xff); 105 pData[19] = (unsigned char)((biWidth>>8) & 0xff); 106 pData[20] = (unsigned char)((biWidth>>16) & 0xff); 107 pData[21] = (unsigned char)((biWidth>>24) & 0xff); 108 109 // the bitmap height in pixels (signed integer). 110 pData[22] = (unsigned char)((biHeight>>0) & 0xff); 111 pData[23] = (unsigned char)((biHeight>>8) & 0xff); 112 pData[24] = (unsigned char)((biHeight>>16) & 0xff); 113 pData[25] = (unsigned char)((biHeight>>24) & 0xff); 114 115 // the number of color planes being used. Must be set to 1. 116 pData[26] = (unsigned char)((biPlanes>>0) & 0xff); 117 pData[27] = (unsigned char)((biPlanes>>8) & 0xff); 118 119 // the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32. 120 pData[28] = (unsigned char)((biBitCount>>0) & 0xff); 121 pData[29] = (unsigned char)((biBitCount>>8) & 0xff); 122 123 // the compression method being used. See the next table for a list of possible values. 124 pData[30] = (unsigned char)((biCompression>>0) & 0xff); 125 pData[31] = (unsigned char)((biCompression>>8) & 0xff); 126 pData[32] = (unsigned char)((biCompression>>16) & 0xff); 127 pData[33] = (unsigned char)((biCompression>>24) & 0xff); 128 129 // the image size. This is the size of the raw bitmap data, and should not be confused with the file size. 130 pData[34] = (unsigned char)((biSizeImage>>0) & 0xff); 131 pData[35] = (unsigned char)((biSizeImage>>8) & 0xff); 132 pData[36] = (unsigned char)((biSizeImage>>16) & 0xff); 133 pData[37] = (unsigned char)((biSizeImage>>24) & 0xff); 134 135 // the horizontal resolution of the image. (pixel per meter, signed integer) 136 pData[38] = (unsigned char)((biXPelsPerMeter>>0) & 0xff); 137 pData[39] = (unsigned char)((biXPelsPerMeter>>8) & 0xff); 138 pData[40] = (unsigned char)((biXPelsPerMeter>>16) & 0xff); 139 pData[41] = (unsigned char)((biXPelsPerMeter>>24) & 0xff); 140 141 // the vertical resolution of the image. (pixel per meter, signed integer) 142 pData[42] = (unsigned char)((biYPelsPerMeter>>0) & 0xff); 143 pData[43] = (unsigned char)((biYPelsPerMeter>>8) & 0xff); 144 pData[44] = (unsigned char)((biYPelsPerMeter>>16) & 0xff); 145 pData[45] = (unsigned char)((biYPelsPerMeter>>24) & 0xff); 146 147 // the number of colors in the color palette, or 0 to default to 2^n. 148 pData[46] = (unsigned char)((biClrUsed>>0) & 0xff); 149 pData[47] = (unsigned char)((biClrUsed>>8) & 0xff); 150 pData[48] = (unsigned char)((biClrUsed>>16) & 0xff); 151 pData[49] = (unsigned char)((biClrUsed>>24) & 0xff); 152 153 // the number of important colors used, or 0 when every color is important; generally ignored. 154 pData[50] = (unsigned char)((biClrImportant>>0) & 0xff); 155 pData[51] = (unsigned char)((biClrImportant>>8) & 0xff); 156 pData[52] = (unsigned char)((biClrImportant>>16) & 0xff); 157 pData[53] = (unsigned char)((biClrImportant>>24) & 0xff); 158 159 160 pp = &pData[54]; 161 for(i = 0; i < biHeight; i++) 162 { 163 for(j = 0; j < biWidth; j++) 164 { 165 set_color(pp, biWidth, biHeight, i, j, i+j, i, j); // 随便填有点颜色 166 } 167 } 168 169 iFileSize = fwrite(pData, 1, bfSize, fp); 170 printf("generate file iFileSize: %d\n", iFileSize); 171 172 free(pData); 173 174 fclose(fp); 175 176 return 0; 177 }
以上生成的BMP,坐标起始位置(0,0)为左下角,填充颜色:
1 void set_color( unsigned char *pp, int w, int h, 2 int x, int y, unsigned char r, unsigned char g, unsigned char b ) 3 { 4 pp[y*w*3+x*3+0] = (unsigned char)r; 5 pp[y*w*3+x*3+1] = (unsigned char)g; 6 pp[y*w*3+x*3+2] = (unsigned char)b; 7 }
生成一个,~~~~(>_<)~~~~:
1 int main(int argc, char *argv[]) 2 { 3 int ret; 4 char *file_name; 5 6 if( argc != 2 ) 7 { 8 printf("please input bitmap file_name\n"); 9 printf("usage: %s bitmap_file_name\n", argv[0]); 10 return 0; 11 } 12 13 file_name = argv[1]; 14 15 ret = bitmap_file_generate(file_name, 1024*10, 1024*10); 16 17 printf("bitmap_file_generate, ret: %d\n", ret); 18 19 return 0; 20 }
转载于:https://www.cnblogs.com/utank/p/5997516.html
Bitmap文件格式+生成一个BMP文件相关推荐
- Python——生成一个大文件ips.txt,要求1200行 ,每行随机为172.25.254.0/24段的ip(京东二面笔试题)
代码块: from collections import Iterable f = open('/tmp/passwd') print(isinstance(f,Iterable)) for i,it ...
- matlab GUI学习笔记1 如何生成一个GUI文件,及如何修改保存后的.fig文件
最近需要做一个项目的小软件,学习如何用matlab GUI生成一个小软件,带对话框等等.所以这几天对matlab GUI的分步学习进行记录.我的matlab版本是R2017a. 首先是如何在matla ...
- dd命令快速生成一个大文件
有时候测试需要生成一个临时的大文件,可以使用dd命令来完成 xxx:~/test/mnt/sdcard/capture$ dd if=/dev/zero of=test.log bs=1M count ...
- linux每天生成一个日志文件,使Apache每天产生一个日志文件
Linux:使Apache每天产生一个日志文件,解决apache日志文件过大的问题. 方法: 用rotatelogs实现apache日志按日期存放. 操作步骤: 一.在apache的配置文件中找到 E ...
- 使用py2exe生成一个exe文件
在windows中,使用py2exe将py脚本转换成exe文件时,在dist目录下会有zip,dll等其他文件.如果想要只生成一个可单独运行的exe文件,需要在setup.py文件中添加bundle_ ...
- dd命令生成一个大文件
在当前目录生成一个大小为1G的大文件,内容是0 dd if=/dev/zero of=/test count=2 bs=512M if 输入文件 of 输出文件 bs 字节为单位的块大小 count ...
- 将后台日志输出到指定文件中,并且每天生成一个日志文件
主要有以下四部: 一.导包 (我用的是org.apache.log4j.Logger 将 log4j-1.2.17.jar放到lib下面) 二.在web.xm ...
- 非正常关闭vi编辑器时会生成一个.swp文件
正在使用vim命令编辑文件,然后虚拟机突然重启,再次使用vim编辑这个文件时,就会提示下图: Swap file ".hbase-site.xml.swp" already exi ...
- Unity打包的PC项目生成一个EXE文件
准备 首先打包一个测试项目,下面是生成的文件 生成EXE步骤 PS:我这里用的繁体版的压缩软件,有些名称可能会不一样,请对号入座 1.将所有文件添加到压缩文件,修改文件名称 2.进入设置界面 3.选择 ...
最新文章
- 模板 - 欧拉路、欧拉回路(一笔画问题)
- 必须熟悉的vim快捷键操作
- 基于光学导航系统,矩阵变换和3D-2D配准研究
- ipython notebook的安装和使用;pip和easy_install.exe的区别;几个库Scikit-Learn、NumPy、SciPy、Matplotlib的用途...
- 未来计算机的缺陷,新技术将计算机芯片缺陷变为优势
- mysql vim 退不出_字符集问题之 MySQL, Linux终端, vim
- Android硬件抽象层(HAL)概要介绍和学习计划
- 设单片机的晶振频率为6mhz c语言,单片机习题科学出版社.doc
- 作为职场小白,除了要注意自身的言谈举止
- docker-3-常用命令(下)
- 井下作业类毕业论文文献有哪些?
- Java---设计【员工工资管理系统】
- java反混淆器_Java代码加密与反编译(一):利用混淆器工具proGuard对jar包加密...
- 高斯法求解线性方程组
- 微信发红包api php,php版微信公众号接口实现发红包的方法
- Brendan Gregg
- 走进JavaWeb技术世界5:初探Tomcat的HTTP请求过程
- python学习基础第一阶段day11
- limma差异分析谁和谁比很重要吗
- 【Keras+计算机视觉+Tensorflow】生成对抗神经网络中DCGAN、CycleGAN网络的讲解(图文解释 超详细)
热门文章
- vue 同步加载_2019 前端面试题汇总(主要为 Vue)
- python小项目超级大脑抱香_超级大脑教练 - 主页
- Vue第一部分(5):计算属性和过滤器
- dedecms二次开发常用代码
- dubbo全局异常处理_详解Dubbo无法处理自定义异常及解决方案
- Spring Cloud文档阅读笔记-初识Spring Cloud(对Spring Cloud初步了解)
- Qt笔记-窗口程序在任务栏中隐藏及永久置顶
- C++|Qt中QTreeWidget基本操作及完整代码下载
- 武汉科技大学085211计算机,武汉科技大学计算机科学与技术学院
- 怎么跟女生看电影牵手技巧方法