Skip to content

Commit

Permalink
feat(tex): add has_opaque_alpha for crunch
Browse files Browse the repository at this point in the history
  • Loading branch information
mklokocka committed Nov 1, 2024
1 parent 025a891 commit 50ac7c1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
Binary file modified data/crunch.7z
Binary file not shown.
2 changes: 2 additions & 0 deletions include/btu/tex/crunch_texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CrunchTexture
[[nodiscard]] auto get_texture_type() const noexcept -> TextureType;
[[nodiscard]] auto get_format_as_dxgi() const noexcept -> DXGI_FORMAT;

[[nodiscard]] auto has_opaque_alpha() const noexcept -> bool;

[[nodiscard]] auto operator==(const CrunchTexture &) const noexcept -> bool = default;

private:
Expand Down
34 changes: 34 additions & 0 deletions src/tex/crunch_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,40 @@ auto CrunchTexture::get_format_as_dxgi() const noexcept -> DXGI_FORMAT
}
}

auto CrunchTexture::has_opaque_alpha() const noexcept -> bool
{
if (!tex_.has_alpha())
return true;

// Check if there are any miplevels at all.
[[unlikely]] if (!tex_.get_num_levels())
return true;

// Should catch everything.
const uint8 threshold = 254;

for (auto face = 0u; face < tex_.get_num_faces(); face++)
{
const auto *mip = tex_.get_level(face, 0);

const auto img = mip->get_image();

// One last safety check.
[[unlikely]] if (img == nullptr)
return true;

const auto pixels = img->get_pixels();

for (auto i = 0u; i < img->get_total_pixels(); i++)
{
if (pixels[i].a < threshold)
return false;
}
}

return true;
}

auto load_crunch(Path path) noexcept -> tl::expected<CrunchTexture, Error>
{
CrunchTexture tex;
Expand Down
15 changes: 15 additions & 0 deletions tests/tex/crunch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ TEST_CASE("Crunch Tex Memory IO", "[src]")
REQUIRE(*mem_data == fs_data);
}

TEST_CASE("Crunch Tex has_opaque_alpha", "[src]")
{
const auto dir = Path("crunch");
const auto opaque_file = dir / "in" / u8"opaque_alpha.dds";
const auto not_opaque_file = dir / "in" / u8"not_opaque_alpha.dds";

// Load.
auto opaque = require_expected(btu::tex::load_crunch(opaque_file));
auto not_opaque = require_expected(btu::tex::load_crunch(not_opaque_file));

// Test alpha.
CHECK(opaque.has_opaque_alpha());
CHECK_FALSE(not_opaque.has_opaque_alpha());
}

TEST_CASE("Crunch resize", "[src]")
{
test_expected_dir_crunch(u8"crunch_resize", [](auto &&tex) {
Expand Down

0 comments on commit 50ac7c1

Please sign in to comment.