LibGfx/DDSLoader: Allow image dimensions that are not divisible by 4

This commit is contained in:
Tim Ledbetter 2023-10-06 18:51:57 +01:00 committed by Andreas Kling
parent bf75ecdcf7
commit b25efa219b
4 changed files with 22 additions and 6 deletions

View file

@ -8,6 +8,7 @@
#include <AK/DeprecatedString.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/ImageFormats/BMPLoader.h>
#include <LibGfx/ImageFormats/DDSLoader.h>
#include <LibGfx/ImageFormats/GIFLoader.h>
#include <LibGfx/ImageFormats/ICOLoader.h>
#include <LibGfx/ImageFormats/ILBMLoader.h>
@ -617,3 +618,18 @@ TEST_CASE(test_jxl_modular_property_8)
}
}
}
TEST_CASE(test_dds)
{
Array file_names = {
TEST_INPUT("dds/catdog-alert-29x29.dds"sv),
TEST_INPUT("dds/catdog-alert-32x32.dds"sv)
};
for (auto file_name : file_names) {
auto file = MUST(Core::MappedFile::map(file_name));
EXPECT(Gfx::DDSImageDecoderPlugin::sniff(file->bytes()));
auto plugin_decoder = MUST(Gfx::DDSImageDecoderPlugin::create(file->bytes()));
expect_single_frame(*plugin_decoder);
}
}

Binary file not shown.

Binary file not shown.

View file

@ -253,8 +253,8 @@ static ErrorOr<void> decode_dx5_alpha_block(Stream& stream, DDSLoadingContext& c
color[7] = 255;
}
for (size_t y = 0; y < 4; y++) {
for (size_t x = 0; x < 4; x++) {
for (size_t y = 0; y < 4 && bitmap_y + y < static_cast<u64>(context.bitmap->height()); y++) {
for (size_t x = 0; x < 4 && bitmap_x + x < static_cast<u64>(context.bitmap->width()); x++) {
u8 index = 3 * (4 * y + x);
u8 bit_location = floor(index / 8.0);
u8 adjusted_index = index - (bit_location * 8);
@ -284,8 +284,8 @@ static ErrorOr<void> decode_dx3_alpha_block(Stream& stream, DDSLoadingContext& c
u64 alpha_0 = a0 + 256u * (a1 + 256u * (a2 + 256u * (a3 + 256u)));
u64 alpha_1 = a4 + 256u * (a5 + 256u * (a6 + 256u * a7));
for (size_t y = 0; y < 4; y++) {
for (size_t x = 0; x < 4; x++) {
for (size_t y = 0; y < 4 && bitmap_y + y < static_cast<u64>(context.bitmap->height()); y++) {
for (size_t x = 0; x < 4 && bitmap_x + x < static_cast<u64>(context.bitmap->width()); x++) {
u8 code = 4 * (4 * y + x);
if (code >= 32) {
@ -357,8 +357,8 @@ static ErrorOr<void> decode_color_block(Stream& stream, DDSLoadingContext& conte
}
size_t i = 0;
for (size_t y = 0; y < 4; y++) {
for (size_t x = 0; x < 4; x++) {
for (size_t y = 0; y < 4 && bitmap_y + y < static_cast<u64>(context.bitmap->height()); y++) {
for (size_t x = 0; x < 4 && bitmap_x + x < static_cast<u64>(context.bitmap->width()); x++) {
u8 code_byte = (code >> (i * 2)) & 3;
u8 r = rgba[code_byte][0];
u8 g = rgba[code_byte][1];