| 1097 | } |
| 1098 | |
| 1099 | bool BMPBufferLoader(RGBA8Image* image, const void* buffer, size_t buffer_size) |
| 1100 | { |
| 1101 | if (buffer_size < sizeof(BMPFileHeader) + sizeof(BMPInfoHeader)) |
| 1102 | { |
| 1103 | Console.Error("BMP file too small"); |
| 1104 | return false; |
| 1105 | } |
| 1106 | |
| 1107 | const u8* data = static_cast<const u8*>(buffer); |
| 1108 | BMPFileHeader file_header; |
| 1109 | BMPInfoHeader info_header; |
| 1110 | |
| 1111 | std::memcpy(&file_header, data, sizeof(BMPFileHeader)); |
| 1112 | std::memcpy(&info_header, data + sizeof(BMPFileHeader), sizeof(BMPInfoHeader)); |
| 1113 | |
| 1114 | if (file_header.type != 0x4D42) |
| 1115 | { |
| 1116 | Console.Error("Invalid BMP signature"); |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
| 1120 | // Check for extended header versions (V4=108 bytes, V5=124 bytes) |
| 1121 | // We read as BITMAPINFOHEADER (40 bytes) regardless, since extended headers just add fields at the end |
| 1122 | if (info_header.size == 108) |
| 1123 | { |
| 1124 | Console.Warning("BITMAPV4HEADER detected, reading as BITMAPINFOHEADER"); |
| 1125 | } |
| 1126 | else if (info_header.size == 124) |
| 1127 | { |
| 1128 | Console.Warning("BITMAPV5HEADER detected, reading as BITMAPINFOHEADER"); |
| 1129 | } |
| 1130 | else if (info_header.size != 40) |
| 1131 | { |
| 1132 | Console.Warning("Unknown BMP header size: %u, attempting to read as BITMAPINFOHEADER", info_header.size); |
| 1133 | } |
| 1134 | |
| 1135 | if (!IsSupportedBMPFormat(info_header.compression, info_header.bit_count)) |
| 1136 | { |
| 1137 | Console.Error("Unsupported BMP format: compression=%u, bit_count=%u", info_header.compression, info_header.bit_count); |
| 1138 | return false; |
| 1139 | } |
| 1140 | |
| 1141 | const u32 width = static_cast<u32>(std::abs(info_header.width)); |
| 1142 | const u32 height = static_cast<u32>(std::abs(info_header.height)); |
| 1143 | const bool flip_vertical = (info_header.height > 0); |
| 1144 | |
| 1145 | if (width == 0 || height == 0) |
| 1146 | { |
| 1147 | Console.Error("Invalid BMP dimensions: %ux%u", width, height); |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
| 1151 | if (width >= 65536 || height >= 65536) |
| 1152 | { |
| 1153 | Console.Error("BMP dimensions too large: %ux%u", width, height); |
| 1154 | return false; |
| 1155 | } |
| 1156 |
no test coverage detected