位图图像的读取与显示的源代码(无错误)
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
位图图像,通常被称为BMP(Bitmap)文件格式,是一种常见的图像存储格式,广泛应用于Windows操作系统和许多其他软件中。这种格式以未经压缩的方式存储像素数据,因此它能精确地保留图像的所有细节,但同时也意味着文件大小相对较大。在本文中,我们将深入探讨如何读取和显示位图图像的源代码,确保程序无误且易于理解。 我们需要了解位图文件的结构。一个BMP文件由两个主要部分组成:文件头和位图信息头。文件头包含了文件的基本信息,如文件大小、位图的偏移位置等;位图信息头则包含了图像的具体属性,如宽度、高度、颜色深度等。在C++中,我们可以定义相应的结构体来表示这些头部信息: ```cpp struct BMPHeader { uint16_t fileType; // 文件类型标识,通常为'BM' uint32_t fileSize; // 文件大小,包括头部和图像数据 uint16_t reserved1; // 保留字段,一般为0 uint16_t reserved2; // 保留字段,一般为0 uint32_t offset; // 位图数据在文件中的偏移量 }; struct BitmapInfoHeader { uint32_t size; // 头部大小,通常为40字节 int32_t width; // 图像宽度,以像素为单位 int32_t height; // 图像高度,以像素为单位 uint16_t planes; // 帧数,通常为1 uint16_t bitCount; // 颜色深度,如8位、24位等 uint32_t compression; // 压缩类型,一般为0表示无压缩 uint32_t dataSize; // 图像数据大小 uint32_t horzResolution; // 水平分辨率,每米像素数 uint32_t vertResolution; // 垂直分辨率,每米像素数 uint32_t colorsUsed; // 实际使用的颜色数,若为0,则表示使用调色板的全部颜色 uint32_t colorsImportant; // 重要的颜色数,若为0,则表示所有颜色都重要 }; ``` 接下来,我们需要编写读取BMP文件的函数。这个函数将打开文件,读取头部信息,并根据颜色深度解析图像数据。对于24位色彩深度(即每个像素由红、绿、蓝三个通道组成,每个通道8位),我们可以使用三字节表示一个像素: ```cpp void readBmp(const char* filePath, uint8_t** imageData, int& width, int& height) { // 打开文件 std::ifstream file(filePath, std::ios::binary); if (!file) { throw std::runtime_error("无法打开文件"); } BMPHeader bmpHeader; BitmapInfoHeader bitmapInfo; // 读取文件头和位图信息头 file.read(reinterpret_cast<char*>(&bmpHeader), sizeof(BMPHeader)); file.read(reinterpret_cast<char*>(&bitmapInfo), sizeof(BitmapInfoHeader)); // 检查文件类型和颜色深度 if (bmpHeader.fileType != 0x4D42 || bitmapInfo.bitCount != 24) { throw std::runtime_error("不支持的BMP文件或颜色深度"); } width = abs(bitmapInfo.width); height = abs(bitmapInfo.height); // 分配内存并读取图像数据 *imageData = new uint8_t[width * height * 3]; file.seekg(bmpHeader.offset, std::ios::beg); file.read(reinterpret_cast<char*>(*imageData), bitmapInfo.dataSize); // BMP文件的像素数据是倒序存储的,需要反转行 if (bitmapInfo.height < 0) { for (int i = 0; i < height / 2; ++i) { uint8_t* topRow = *imageData + i * width * 3; uint8_t* bottomRow = *imageData + (height - i - 1) * width * 3; std::swap_ranges(topRow, topRow + width * 3, bottomRow); } } file.close(); } ``` 有了图像数据后,我们可以将其显示在窗口中。这通常需要一个图形库,例如SDL或OpenGL。以下是一个使用SDL的例子: ```cpp #include <SDL2/SDL.h> void displayImage(SDL_Window* window, SDL_Surface* surface, uint8_t* imageData, int width, int height) { SDL_Surface* imageSurface = SDL_CreateRGBSurfaceFrom(imageData, width, height, 24, width * 3, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); SDL_BlitSurface(imageSurface, NULL, surface, NULL); SDL_UpdateWindowSurface(window); // 等待用户按键,然后销毁图像表面 SDL_Event event; while (SDL_PollEvent(&event) && event.type != SDL_QUIT) {} SDL_FreeSurface(imageSurface); } int main() { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("Bitmap Display", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN); SDL_Surface* surface = SDL_GetWindowSurface(window); try { uint8_t* imageData; int width, height; readBmp("Raw2Bmp.bmp", &imageData, width, height); displayImage(window, surface, imageData, width, height); } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } SDL_DestroyWindow(window); SDL_Quit(); delete[] imageData; return 0; } ``` 以上代码展示了如何从BMP文件中读取和显示图像。在实际应用中,你可能需要根据具体需求进行调整,例如添加对不同颜色深度的支持、处理压缩的BMP文件或者使用更高级的图形库来实现更丰富的图像操作。通过这种方式,你可以创建自己的图像处理工具,或者在游戏、图形设计软件等项目中使用这些基本功能。
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/BMP.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
- 1
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- 伫望夕阳2014-05-24嗯嗯,不太复杂,值得初学者参考
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 0
- 资源: 9
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 石油管道巡检-基于YOLOv11的腐蚀检测与泄漏预警算法实现.pdf
- 实时海洋监测-YOLOv11在船舶识别与污染检测中的创新应用.pdf
- 实时羽毛球追踪-YOLOv11运动轨迹预测算法深度剖析.pdf
- 水产养殖监测-YOLOv11水下生物识别与生长状态分析系统.pdf
- 水产养殖创新-YOLOv11实现鱼类行为分析与投喂量智能调控.pdf
- 水产养殖智能化-YOLOv11实现鱼类生长状态实时监控.pdf
- 水产养殖智能化-YOLOv11实现鱼类行为分析与投喂决策.pdf
- 水产养殖智能化管理-YOLOv11实现鱼类行为分析与生长监测.pdf
- 特征融合新思路-YOLOv11双向特征金字塔深度优化方案.pdf
- 水域安全监测-YOLOv11船舶行为识别与非法捕捞实时预警系统.pdf
- 体育赛事分析-YOLOv11运动员动作捕捉与战术轨迹可视化.pdf
- 体育赛事分析-YOLOv11足球运动员动作识别与战术板自动生成.pdf
- 体育训练分析-YOLOv11运动员动作捕捉与姿态估计技术详解.pdf
- 微创手术辅助-YOLOv11实现手术器械实时追踪与导航系统.pdf
- 卫星遥感解析-YOLOv11在土地利用分类中的小目标检测优化策略(地理信息).pdf
- 卫星遥感解析-YOLOv11地表建筑物变化检测与违建识别.pdf
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)