diff options
Diffstat (limited to 'modules/bmp')
-rw-r--r-- | modules/bmp/SCsub | 4 | ||||
-rw-r--r-- | modules/bmp/config.py | 1 | ||||
-rw-r--r-- | modules/bmp/image_loader_bmp.cpp | 82 | ||||
-rw-r--r-- | modules/bmp/register_types.cpp | 2 | ||||
-rw-r--r-- | modules/bmp/register_types.h | 5 |
5 files changed, 52 insertions, 42 deletions
diff --git a/modules/bmp/SCsub b/modules/bmp/SCsub index e7da7cf108..4f3405ff28 100644 --- a/modules/bmp/SCsub +++ b/modules/bmp/SCsub @@ -1,7 +1,7 @@ #!/usr/bin/env python -Import('env') -Import('env_modules') +Import("env") +Import("env_modules") env_bmp = env_modules.Clone() diff --git a/modules/bmp/config.py b/modules/bmp/config.py index 1c8cd12a2d..d22f9454ed 100644 --- a/modules/bmp/config.py +++ b/modules/bmp/config.py @@ -1,5 +1,6 @@ def can_build(env, platform): return True + def configure(env): pass diff --git a/modules/bmp/image_loader_bmp.cpp b/modules/bmp/image_loader_bmp.cpp index 5ce6d59daa..757afeb9e3 100644 --- a/modules/bmp/image_loader_bmp.cpp +++ b/modules/bmp/image_loader_bmp.cpp @@ -35,11 +35,11 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image, const uint8_t *p_color_buffer, const uint32_t color_table_size, const bmp_header_s &p_header) { - Error err = OK; - if (p_buffer == NULL) + if (p_buffer == nullptr) { err = FAILED; + } if (err == OK) { size_t index = 0; @@ -51,21 +51,24 @@ 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) - PoolVector<uint8_t> data; + Vector<uint8_t> data; int data_len = 0; if (bits_per_pixel <= 8) { // indexed @@ -73,11 +76,11 @@ 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); - PoolVector<uint8_t>::Write data_w = data.write(); - uint8_t *write_buffer = data_w.ptr(); + uint8_t *data_w = data.ptrw(); + uint8_t *write_buffer = data_w; const uint32_t width_bytes = width * bits_per_pixel / 8; const uint32_t line_width = (width_bytes + 3) & ~3; @@ -151,18 +154,18 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image, line -= line_width; } - if (p_color_buffer == NULL || color_table_size == 0) { // regular pixels + if (p_color_buffer == nullptr || color_table_size == 0) { // regular pixels - p_image->create(width, height, 0, Image::FORMAT_RGBA8, data); + p_image->create(width, height, false, Image::FORMAT_RGBA8, data); } else { // data is in indexed format, extend it // Palette data - PoolVector<uint8_t> palette_data; + Vector<uint8_t> palette_data; palette_data.resize(color_table_size * 4); - PoolVector<uint8_t>::Write palette_data_w = palette_data.write(); - uint8_t *pal = palette_data_w.ptr(); + uint8_t *palette_data_w = palette_data.ptrw(); + uint8_t *pal = palette_data_w; const uint8_t *cb = p_color_buffer; @@ -177,11 +180,11 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image, cb += 4; } // Extend palette to image - PoolVector<uint8_t> extended_data; + Vector<uint8_t> extended_data; extended_data.resize(data.size() * 4); - PoolVector<uint8_t>::Write ex_w = extended_data.write(); - uint8_t *dest = ex_w.ptr(); + uint8_t *ex_w = extended_data.ptrw(); + uint8_t *dest = ex_w; const int num_pixels = width * height; @@ -193,7 +196,7 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image, dest += 4; } - p_image->create(width, height, 0, Image::FORMAT_RGBA8, extended_data); + p_image->create(width, height, false, Image::FORMAT_RGBA8, extended_data); } } return err; @@ -201,7 +204,6 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image, Error ImageLoaderBMP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) { - bmp_header_s bmp_header; Error err = ERR_INVALID_DATA; @@ -217,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(); @@ -238,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 @@ -257,30 +261,31 @@ 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())); } - PoolVector<uint8_t> bmp_color_table; + Vector<uint8_t> bmp_color_table; // Color table is usually 4 bytes per color -> [B][G][R][0] bmp_color_table.resize(color_table_size * 4); - PoolVector<uint8_t>::Write bmp_color_table_w = bmp_color_table.write(); - f->get_buffer(bmp_color_table_w.ptr(), color_table_size * 4); + uint8_t *bmp_color_table_w = bmp_color_table.ptrw(); + f->get_buffer(bmp_color_table_w, color_table_size * 4); f->seek(bmp_header.bmp_file_header.bmp_file_offset); uint32_t bmp_buffer_size = (bmp_header.bmp_file_header.bmp_file_size - bmp_header.bmp_file_header.bmp_file_offset); - PoolVector<uint8_t> bmp_buffer; + Vector<uint8_t> bmp_buffer; err = bmp_buffer.resize(bmp_buffer_size); if (err == OK) { - PoolVector<uint8_t>::Write bmp_buffer_w = bmp_buffer.write(); - f->get_buffer(bmp_buffer_w.ptr(), bmp_buffer_size); + uint8_t *bmp_buffer_w = bmp_buffer.ptrw(); + f->get_buffer(bmp_buffer_w, bmp_buffer_size); - PoolVector<uint8_t>::Read bmp_buffer_r = bmp_buffer.read(); - PoolVector<uint8_t>::Read bmp_color_table_r = bmp_color_table.read(); - err = convert_to_image(p_image, bmp_buffer_r.ptr(), - bmp_color_table_r.ptr(), color_table_size, bmp_header); + const uint8_t *bmp_buffer_r = bmp_buffer.ptr(); + const uint8_t *bmp_color_table_r = bmp_color_table.ptr(); + err = convert_to_image(p_image, bmp_buffer_r, + bmp_color_table_r, color_table_size, bmp_header); } f->close(); } @@ -290,7 +295,6 @@ Error ImageLoaderBMP::load_image(Ref<Image> p_image, FileAccess *f, void ImageLoaderBMP::get_recognized_extensions( List<String> *p_extensions) const { - p_extensions->push_back("bmp"); } diff --git a/modules/bmp/register_types.cpp b/modules/bmp/register_types.cpp index d5cc6c5eb3..6220e956d6 100644 --- a/modules/bmp/register_types.cpp +++ b/modules/bmp/register_types.cpp @@ -32,7 +32,7 @@ #include "image_loader_bmp.h" -static ImageLoaderBMP *image_loader_bmp = NULL; +static ImageLoaderBMP *image_loader_bmp = nullptr; void register_bmp_types() { image_loader_bmp = memnew(ImageLoaderBMP); diff --git a/modules/bmp/register_types.h b/modules/bmp/register_types.h index 398716eaa1..e7561dc32d 100644 --- a/modules/bmp/register_types.h +++ b/modules/bmp/register_types.h @@ -28,5 +28,10 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef BMP_REGISTER_TYPES_H +#define BMP_REGISTER_TYPES_H + void register_bmp_types(); void unregister_bmp_types(); + +#endif // BMP_REGISTER_TYPES_H |