@@ -59,30 +59,6 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
5959 size_t height = (size_t )p_header.bmp_info_header .bmp_height ;
6060 size_t bits_per_pixel = (size_t )p_header.bmp_info_header .bmp_bit_count ;
6161
62- // Check whether we can load it
63-
64- if (bits_per_pixel == 1 ) {
65- // Requires bit unpacking...
66- ERR_FAIL_COND_V_MSG (width % 8 != 0 , ERR_UNAVAILABLE,
67- 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)));
68- ERR_FAIL_COND_V_MSG (height % 8 != 0 , ERR_UNAVAILABLE,
69- 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)));
70-
71- } else if (bits_per_pixel == 2 ) {
72- // Requires bit unpacking...
73- ERR_FAIL_COND_V_MSG (width % 4 != 0 , ERR_UNAVAILABLE,
74- vformat (" 2-bpp BMP images must have a width that is a multiple of 4, but the imported BMP is %d pixels wide." , int (width)));
75- ERR_FAIL_COND_V_MSG (height % 4 != 0 , ERR_UNAVAILABLE,
76- vformat (" 2-bpp BMP images must have a height that is a multiple of 4, but the imported BMP is %d pixels tall." , int (height)));
77-
78- } else if (bits_per_pixel == 4 ) {
79- // Requires bit unpacking...
80- ERR_FAIL_COND_V_MSG (width % 2 != 0 , ERR_UNAVAILABLE,
81- 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)));
82- ERR_FAIL_COND_V_MSG (height % 2 != 0 , ERR_UNAVAILABLE,
83- 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)));
84- }
85-
8662 // Image data (might be indexed)
8763 Vector<uint8_t > data;
8864 int data_len = 0 ;
@@ -98,55 +74,32 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
9874 uint8_t *data_w = data.ptrw ();
9975 uint8_t *write_buffer = data_w;
10076
101- const uint32_t width_bytes = width * bits_per_pixel / 8 ;
102- const uint32_t line_width = (width_bytes + 3 ) & ~3 ;
77+ const uint32_t width_bytes = ( width * bits_per_pixel + 7 ) / 8 ;
78+ const uint32_t line_width = (width_bytes + 3 ) & ~3 ; // Padded to 4 bytes.
10379
104- // The actual data traversal is determined by
105- // the data width in case of 8/4/2/1 bit images
106- const uint32_t w = bits_per_pixel >= 16 ? width : width_bytes;
10780 const uint8_t *line = p_buffer + (line_width * (height - 1 ));
10881 const uint8_t *end_buffer = p_buffer + p_header.bmp_file_header .bmp_file_size - p_header.bmp_file_header .bmp_file_offset ;
82+ ERR_FAIL_COND_V (line + line_width > end_buffer, ERR_FILE_CORRUPT);
10983
11084 for (uint64_t i = 0 ; i < height; i++) {
11185 const uint8_t *line_ptr = line;
11286
113- for (unsigned int j = 0 ; j < w; j++) {
114- ERR_FAIL_COND_V (line_ptr >= end_buffer, ERR_FILE_CORRUPT);
87+ for (unsigned int j = 0 ; j < width; j++) {
11588 switch (bits_per_pixel) {
11689 case 1 : {
117- uint8_t color_index = *line_ptr ;
90+ write_buffer[index] = (line[(j * bits_per_pixel) / 8 ] >> ( 8 - bits_per_pixel * ( 1 + j % 8 ))) & 0x01 ;
11891
119- write_buffer[index + 0 ] = (color_index >> 7 ) & 1 ;
120- write_buffer[index + 1 ] = (color_index >> 6 ) & 1 ;
121- write_buffer[index + 2 ] = (color_index >> 5 ) & 1 ;
122- write_buffer[index + 3 ] = (color_index >> 4 ) & 1 ;
123- write_buffer[index + 4 ] = (color_index >> 3 ) & 1 ;
124- write_buffer[index + 5 ] = (color_index >> 2 ) & 1 ;
125- write_buffer[index + 6 ] = (color_index >> 1 ) & 1 ;
126- write_buffer[index + 7 ] = (color_index >> 0 ) & 1 ;
127-
128- index += 8 ;
129- line_ptr += 1 ;
92+ index++;
13093 } break ;
13194 case 2 : {
132- uint8_t color_index = *line_ptr ;
95+ write_buffer[index] = (line[(j * bits_per_pixel) / 8 ] >> ( 8 - bits_per_pixel * ( 1 + j % 4 ))) & 0x03 ;
13396
134- write_buffer[index + 0 ] = (color_index >> 6 ) & 3 ;
135- write_buffer[index + 1 ] = (color_index >> 4 ) & 3 ;
136- write_buffer[index + 2 ] = (color_index >> 2 ) & 3 ;
137- write_buffer[index + 3 ] = color_index & 3 ;
138-
139- index += 4 ;
140- line_ptr += 1 ;
97+ index++;
14198 } break ;
14299 case 4 : {
143- uint8_t color_index = *line_ptr;
144-
145- write_buffer[index + 0 ] = (color_index >> 4 ) & 0x0f ;
146- write_buffer[index + 1 ] = color_index & 0x0f ;
100+ write_buffer[index] = (line[(j * bits_per_pixel) / 8 ] >> (8 - bits_per_pixel * (1 + j % 2 ))) & 0x0f ;
147101
148- index += 2 ;
149- line_ptr += 1 ;
102+ index++;
150103 } break ;
151104 case 8 : {
152105 uint8_t color_index = *line_ptr;
0 commit comments