Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.

Commit afb0ca8

Browse files
committed
fix(BmpImage): 修改加载出错提示
1 parent b46bb81 commit afb0ca8

4 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/BmpFile/include/BmpImage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class BmpImage {
4646
BmpType getBmpFileType() const;
4747

4848
protected:
49+
bool imageLoaded = false;
4950
BmpFileHeader bmpFileHeader{};
5051
BmpInfoHeader bmpInfoHeader{};
5152
std::ifstream inputFile = nullptr; //Bmp文件流

src/BmpFile/src/BmpImage.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "BmpImage.h"
1212

13+
#include <QMessageBox>
1314
#include <glog/logging.h>
1415

1516
BmpImage::~BmpImage() {
@@ -20,20 +21,26 @@ void BmpImage::readImage(const std::string &filename) {
2021
inputFile = std::ifstream(filename, std::ios::binary);
2122
if (!inputFile) {
2223
LOG(ERROR) << "Failed to open file: " << filename << std::endl;
24+
QMessageBox::warning(nullptr, "无法打开图片", QString::asprintf("无法打开文件: %s", filename.c_str()));
2325
return;
2426
}
2527
inputFile.read(reinterpret_cast<char *>(&bmpFileHeader), sizeof(BmpFileHeader));
2628
if (bmpFileHeader.signature != 0x4D42) {
2729
LOG(ERROR) << "The file is not a valid BMP file. " << std::endl;
30+
QMessageBox::warning(nullptr, "无法打开图片", "此文件不是有效的BMP文件");
2831
return;
2932
}
3033
inputFile.read(reinterpret_cast<char *>(&bmpInfoHeader), sizeof(BmpInfoHeader));
34+
imageLoaded = true;
3135
}
3236

3337
void BmpImage::saveImage(const std::string &filename) {}
3438
void BmpImage::toQPixMap(QPixmap &pixmap) {}
3539

3640
BmpType BmpImage::getBmpFileType() const {
41+
if (!this->imageLoaded) {
42+
return LOAD_FAIL;
43+
}
3744
switch (bmpInfoHeader.bitsPerPixel) {
3845
case 1: return ONE_BIT;
3946
case 8: return EIGHT_BIT;

src/Factory/src/BmpFactory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ BmpImage *BmpFactory::createBmpImage(const std::string &filepath) {
2929
case UNKNOWN_BIT:
3030
QMessageBox::warning(nullptr, "无法加载图片", "不支持的BMP文件格式");
3131
LOG(ERROR) << "未知的bmp文件格式" << std::endl;
32+
case LOAD_FAIL:
3233
return nullptr;
3334
}
3435
return nullptr;

src/include/BmpDefinition.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ struct BmpPixelInfo {
9595
};
9696

9797
enum BmpType {
98-
UNKNOWN_BIT = -1,
98+
LOAD_FAIL = -1,
99+
UNKNOWN_BIT = -2,
99100
ONE_BIT = 1,
100101
EIGHT_BIT = 8,
101102
TWENTY_FOUR_BIT = 24

0 commit comments

Comments
 (0)