MFC开发位图BitMap图像的读取与存储.pdf

上传人:飞****9 文档编号:127901746 上传时间:2020-04-07 格式:PDF 页数:3 大小:72.32KB
返回 下载 相关 举报
MFC开发位图BitMap图像的读取与存储.pdf_第1页
第1页 / 共3页
MFC开发位图BitMap图像的读取与存储.pdf_第2页
第2页 / 共3页
MFC开发位图BitMap图像的读取与存储.pdf_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《MFC开发位图BitMap图像的读取与存储.pdf》由会员分享,可在线阅读,更多相关《MFC开发位图BitMap图像的读取与存储.pdf(3页珍藏版)》请在金锄头文库上搜索。

1、MFC开发位图BitMap图像的读取与存储 做图像处理时的源文件一般要用无损的图像文件格式 位图 BitMap 是windows系统下可存储无压缩图像的文件格式 要实现位图文 件的读取和存储 首先要明白位图文件的的存储数据结构 位图文件由四部分依序组成 BITMAPFILEHEADER BITMAPINFOHEADER 调 色板 Image Data 1 BITMAPFILEHEADER结构的长度是固定的14个字节 描述文件的有关信息 其数据结构是 cpp view plaincopy 01 typedefstruct tagBITMAPFILEHEADER 02 WORD bfType mu

2、st be 0 x4D42 03 DWORD bfSize the size of the whole bitmap file 04 WORD bfReserved1 05 WORD bfReserved2 06 DWORD bfOffBits the sum bits of BITMAPFILEHEADER BITMAPINFOHEADER and RGBQUAD the index byte of the image data 07 BITMAPFILEHEADER FAR LPBITMAPFILEHEADER PBITMAPFILEHEADER 2 BITMAPINFOHEADER结构的

3、长度是固定的40个字节 描述图像的有关信息 其数据结构是 cpp view plaincopy 01 typedefstruct tagBITMAPINFOHEADER 02 DWORD biSize the size of this struct it is 40 bytes 03 LONG biWidth the width of image data the unit is pixel 04 LONG biHeight the height of image data the unit is pixel 05 WORD biPlanes must be 1 06 WORD biBitCo

4、unt the bit count of each pixel usually be 1 4 8 or 24 07 DWORD biCompression is this image compressed 0 indicates no compression 08 DWORD biSizeImage the size of image data 09 LONG biXPelsPerMeter 10 LONG biYPelsPerMeter 11 DWORD biClrUsed 12 DWORD biClrImportant 13 BITMAPINFOHEADER FAR LPBITMAPINF

5、OHEADER PBITMAPINFOHEADER 值得注意的是 其中biSizeImage指的是实际图像数据的大小 以字节为单位 其计算公式是 宽 高 其中宽必须是4的整数倍 如果不 是整数倍 则取大于宽的离4的整数倍最近的数值 这个要求可能是因为现在的计算机大都是32位4字节的 计算机每次读取4字节 这样 每行的像素可以整数次读取完成 3 调色板 现在的计算机大都是32位或是更高 于是图像数据可用真彩色24位表达的 即每个像素均由24bit表示 每8bit表示RGB三色中的 一色 但以前的计算机处理能力较差 图像用1位 4位或8位 即BITMAPINFOHEADER中的biBitCount

6、不是24 这时又想表达出RGB色彩 就需要调色板 调色板即使将图像数据中使用的一种颜色对应到RGB颜色中 这样图像数据中的像素值就是一个索引值 真正的像素值是 这个索引值对应的调色板中的值 调色板是一个数组 数组中每个元素就是一个rgb颜色 对于8位图像 最多可表达256种颜色 调色板的 大小就是256 调色板数组中每个元素的数据结构 cpp view plaincopy 01 typedefstruct tagRGBQUAD 02 BYTE rgbBlue 03 BYTE rgbGreen 04 BYTE rgbRed 05 BYTE rgbReserved 06 RGBQUAD 07 ty

7、pedef RGBQUAD FAR LPRGBQUAD 4 图像数据对于1位图像 1个像素用1bit存储 对于24位图像 1个像素用24bit存储 位图文件的数据是从下而上 从左而右存储的 所以 说 读取的时候 最先读到的是图像左下方的像素 最后读取的是图像右上方的图像 用c 写的位图文件的读取与存储方法 类结构 BitMap h cpp view plaincopy 01 include 02 class BitMap 03 04 public 05 BitMap 06 BitMap 07 protected 08 BITMAPFILEHEADER fileHeader 09 BITMAPI

8、NFOHEADER infoHeader 10 public 11 int width p height p bitCount 12 unsigned char dataBuf 13 LPRGBQUAD colorTable 14 bool Read char fileName 15 bool Write char fileName 16 方法 BitMap cpp cpp view plaincopy 01 include BitMap h 02 include 03 include 04 include 05 usingnamespace std 06 define NULL 0 07 0

9、8 BitMap BitMap 09 BitMap BitMap 10 11 read bitmap info from a file 12 bool BitMap Read char fileName 13 14 FILE f fopen fileName rb open file 15 if f NULL returnfalse 16 17 fread read BITMAPFILEHEADER 18 19 fread read BITMAPINFOHEADER 20 width p infoHeader biWidth 21 height p infoHeader biHeight 22

10、 bitCount infoHeader biBitCount 23 24 if bitCount 8 if colorTable exist read colorTable 25 26 colorTable new RGBQUAD 256 27 fread 28 29 30 dataBuf new unsigned char infoHeader biSizeImage read image data 31 fread dataBuf 1 infoHeader biSizeImage f 32 33 fclose f close file 34 returntrue 35 36 37 wri

11、te bitmap info to a file 38 bool BitMap Write char fileName 39 40 FILE f fopen fileName wb create or open file to be written 41 if f NULL returnfalse 42 43 int colorTableSize 0 if bitcount is 24 there is no color table 44 if bitCount 8 if bitcount is 8 the size of color table is 256 4 4B is the size

12、 of RGBQUAD 45 colorTableSize sizeof RGBQUAD 256 46 47 int headerSize sizeof BITMAPFILEHEADER sizeof BITMAPINFOHEADER colorTableSize the size of the header of bmp file 48 int lineSize width p bitCount 8 3 4 4 the size of each line in bmp file 49 int dataSize lineSize height p the size of the image d

13、ata of bmp file 50 51 fileHeader bfType 0 x4D42 set the attribute of BITMAPFILEHEADER 52 fileHeader bfSize headerSize dataSize 53 fileHeader bfReserved1 0 54 fileHeader bfReserved2 0 55 fileHeader bfOffBits headerSize 56 57 infoHeader biSize 40 set the attribute of BITMAPINFOHEADER 58 infoHeader biW

14、idth width p 59 infoHeader biHeight height p 60 infoHeader biPlanes 1 61 infoHeader biBitCount bitCount 62 infoHeader biCompression 0 63 infoHeader biSizeImage dataSize 64 infoHeader biClrImportant 0 65 infoHeader biXPelsPerMeter 0 66 infoHeader biYPelsPerMeter 0 67 68 fwrite write the data of BITFI

15、LEHEADER to bmp file 69 fwrite write the data of BITINFOHEADER to bmp file 70 if bitCount 8 if color table exists write the data of color table to bmp file 71 72 colorTable new RGBQUAD 256 73 fwrite 74 75 fwrite dataBuf 1 dataSize f write the image data to bmp file 76 77 fclose f data writting is finished close the bmp file 78 return true 79 程序入口 cpp view plaincopy 01 void main 02 03 BitMap bm new BitMap 04 bm Read nv BMP 05 bm Write nvnew bmp 06 delete bm 07

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 初中教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号