Skip to content

Commit

Permalink
Add more error explanations in the BMP image loader
Browse files Browse the repository at this point in the history
This closes godotengine#32166 and closes godotengine#30629.
  • Loading branch information
Calinou committed Aug 18, 2020
1 parent ba1109a commit 40485e2
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions modules/bmp/image_loader_bmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,

if (bits_per_pixel == 1) {
// Requires bit unpacking...
ERR_FAIL_COND_V(width % 8 != 0, ERR_UNAVAILABLE);
ERR_FAIL_COND_V(height % 8 != 0, ERR_UNAVAILABLE);
ERR_FAIL_COND_V_MSG(width % 8 != 0, ERR_UNAVAILABLE,
vformat("1-bpp BMP images must have a width that is a multiple of 8, but the imported BMP is %d pixels wide.", int(width)));
ERR_FAIL_COND_V_MSG(height % 8 != 0, ERR_UNAVAILABLE,
vformat("1-bpp BMP images must have a height that is a multiple of 8, but the imported BMP is %d pixels tall.", int(height)));

} else if (bits_per_pixel == 4) {
// Requires bit unpacking...
ERR_FAIL_COND_V(width % 2 != 0, ERR_UNAVAILABLE);
ERR_FAIL_COND_V(height % 2 != 0, ERR_UNAVAILABLE);
ERR_FAIL_COND_V_MSG(width % 2 != 0, ERR_UNAVAILABLE,
vformat("4-bpp BMP images must have a width that is a multiple of 2, but the imported BMP is %d pixels wide.", int(width)));
ERR_FAIL_COND_V_MSG(height % 2 != 0, ERR_UNAVAILABLE,
vformat("4-bpp BMP images must have a height that is a multiple of 2, but the imported BMP is %d pixels tall.", int(height)));

} else if (bits_per_pixel == 16) {
ERR_FAIL_V(ERR_UNAVAILABLE);
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "16-bpp BMP images are not supported.");
}

// Image data (might be indexed)
Expand All @@ -72,7 +76,7 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
} else { // color
data_len = width * height * 4;
}
ERR_FAIL_COND_V(data_len == 0, ERR_BUG);
ERR_FAIL_COND_V_MSG(data_len == 0, ERR_BUG, "Couldn't parse the BMP image data.");
err = data.resize(data_len);

uint8_t *data_w = data.ptrw();
Expand Down Expand Up @@ -215,13 +219,15 @@ Error ImageLoaderBMP::load_image(Ref<Image> p_image, FileAccess *f,

// Info Header
bmp_header.bmp_info_header.bmp_header_size = f->get_32();
ERR_FAIL_COND_V(bmp_header.bmp_info_header.bmp_header_size < BITMAP_INFO_HEADER_MIN_SIZE, ERR_FILE_CORRUPT);
ERR_FAIL_COND_V_MSG(bmp_header.bmp_info_header.bmp_header_size < BITMAP_INFO_HEADER_MIN_SIZE, ERR_FILE_CORRUPT,
vformat("Couldn't parse the BMP info header. The file is likely corrupt: %s", f->get_path()));

bmp_header.bmp_info_header.bmp_width = f->get_32();
bmp_header.bmp_info_header.bmp_height = f->get_32();

bmp_header.bmp_info_header.bmp_planes = f->get_16();
ERR_FAIL_COND_V(bmp_header.bmp_info_header.bmp_planes != 1, ERR_FILE_CORRUPT);
ERR_FAIL_COND_V_MSG(bmp_header.bmp_info_header.bmp_planes != 1, ERR_FILE_CORRUPT,
vformat("Couldn't parse the BMP planes. The file is likely corrupt: %s", f->get_path()));

bmp_header.bmp_info_header.bmp_bit_count = f->get_16();
bmp_header.bmp_info_header.bmp_compression = f->get_32();
Expand All @@ -236,10 +242,10 @@ Error ImageLoaderBMP::load_image(Ref<Image> p_image, FileAccess *f,
case BI_RLE4:
case BI_CMYKRLE8:
case BI_CMYKRLE4: {
// Stop parsing
String bmp_path = f->get_path();
// Stop parsing.
f->close();
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Compressed BMP files are not supported: " + bmp_path + ".");
ERR_FAIL_V_MSG(ERR_UNAVAILABLE,
vformat("Compressed BMP files are not supported: %s", f->get_path()));
} break;
}
// Don't rely on sizeof(bmp_file_header) as structure padding
Expand All @@ -255,7 +261,8 @@ Error ImageLoaderBMP::load_image(Ref<Image> p_image, FileAccess *f,
if (bmp_header.bmp_info_header.bmp_bit_count <= 8) {
// Support 256 colors max
color_table_size = 1 << bmp_header.bmp_info_header.bmp_bit_count;
ERR_FAIL_COND_V(color_table_size == 0, ERR_BUG);
ERR_FAIL_COND_V_MSG(color_table_size == 0, ERR_BUG,
vformat("Couldn't parse the BMP color table: %s", f->get_path()));
}

Vector<uint8_t> bmp_color_table;
Expand Down

0 comments on commit 40485e2

Please sign in to comment.