Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tex): add has_opaque_alpha for crunch #58

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 43 additions & 0 deletions src/tex/crunch_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,49 @@ auto CrunchTexture::get_format_as_dxgi() const noexcept -> DXGI_FORMAT
}
}

auto CrunchTexture::has_opaque_alpha() const noexcept -> bool
{
// If format doesn't have alpha, or the image is already compressed.
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);

// Get uncompressed image to check.
crnlib::image_u8 *img;
if (mip->is_packed())
{
img = crnlib::crnlib_new<image_u8>();
mip->get_unpacked_image(*img, 1);
}
else
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
4 changes: 1 addition & 3 deletions src/tex/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,9 @@ template<class Tex>
const Settings &sets,
bool force_alpha) noexcept -> DXGI_FORMAT
{
const auto &tex = file.get();

return guess_best_format(file.get_format_as_dxgi(),
sets.output_format,
GuessBestFormatArgs{.opaque_alpha = !tex.has_alpha(),
GuessBestFormatArgs{.opaque_alpha = file.has_opaque_alpha(),
.allow_compressed = sets.compress
&& can_be_compressed<CrunchTexture>(file),
.force_alpha = force_alpha});
Expand Down
21 changes: 21 additions & 0 deletions tests/tex/crunch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ 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 opaque_file_bc = dir / "in" / u8"opaque_alpha_compressed.dds";
const auto not_opaque_file = dir / "in" / u8"not_opaque_alpha.dds";
const auto not_opaque_file_bc = dir / "in" / u8"not_opaque_alpha_compressed.dds";

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

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

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