博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++读取BMP文件
阅读量:4511 次
发布时间:2019-06-08

本文共 2532 字,大约阅读时间需要 8 分钟。

#include 
#include
#include
using namespace std;class CBitmap{public: typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned long DWORD; typedef long LONG; typedef struct{ DWORD bfSize; // file size WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; // offset from file header to pixel data }BITMAPFILEHEADER; typedef struct{ DWORD biSize; // structure size LONG biWidth; LONG biHeight; WORD biPlanes; // must be 1 WORD biBitCount; // bit number of one pixel DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; }BITMAPINFOHEADER; CBitmap(){} ~CBitmap(){} bool load(const string& file) { ifstream fs(file, ios_base::in | ios_base::beg | ios_base::binary); if (!fs.is_open()){ return false; } fs.read((char*)&m_tag, sizeof(WORD)); if (m_tag != 0x4D42){ return false; } fs.read((char*)&m_fileHeader, sizeof(m_fileHeader)); if (fs.gcount() != sizeof(m_fileHeader)){ return false; } fs.read((char*)&m_infoHeader, sizeof(m_infoHeader)); if (fs.gcount() != sizeof(m_infoHeader)){ return false; } m_palette.resize(m_fileHeader.bfOffBits - sizeof(m_tag)-sizeof(m_fileHeader)-sizeof(m_infoHeader)); fs.read((char*)&m_palette[0], m_palette.size()); m_data.resize(m_fileHeader.bfSize - m_fileHeader.bfOffBits); fs.read((char*)&m_data[0], m_data.size()); if (fs.gcount() != m_data.size()){ return false; } fs.close(); return true; } bool save(const string& file) { ofstream fs(file, ios_base::binary); if (!fs.is_open()){ return false; } fs.write((char*)&m_tag, sizeof(WORD)); fs.write((char*)&m_fileHeader, sizeof(m_fileHeader)); fs.write((char*)&m_infoHeader, sizeof(m_infoHeader)); fs.write((char*)&m_palette[0], m_palette.size()); fs.write((char*)&m_data[0], m_data.size()); fs.close(); return true; } WORD m_tag; BITMAPFILEHEADER m_fileHeader; BITMAPINFOHEADER m_infoHeader; vector
m_palette; vector
m_data;};

 

转载于:https://www.cnblogs.com/zhuyingchun/p/5952457.html

你可能感兴趣的文章
Ubuntu系统Linux编译osg库
查看>>
error MSB8008: 指定的平台工具集(v120)未安装或无效 解决办法
查看>>
5.2 面向对象上
查看>>
JSP 总结
查看>>
Host is not allowed to connect to this MySQL server 解决方案
查看>>
JS 数据类型分析及字符串的方法
查看>>
ASP.Net Core 2.2 MVC入门到基本使用系列 (一)
查看>>
ORA-00907: 缺失右括号
查看>>
【原】移动web资源整理
查看>>
第 7 章 —— 代理模式
查看>>
项目介绍&人员介绍
查看>>
服务稳定、高可用
查看>>
第2课第4节_Java面向对象编程_多态性_P【学习笔记】
查看>>
H5横向三栏布局
查看>>
7.分类:基本概念 忌讳
查看>>
PIL Image 转成 wx.Image、wx.Bitmap
查看>>
名词解释
查看>>
确保某值在区间内
查看>>
递归in C++
查看>>
整合wordpress的插件!!
查看>>