Skip to content

Commit

Permalink
feat(Texture): add support for A4R4G4B4 and A1R5G5B5 formats
Browse files Browse the repository at this point in the history
Intended to fix Try/OpenGothic#668
  • Loading branch information
lmichaelis committed Aug 18, 2024
1 parent f6ded81 commit 466be10
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ namespace zenkit {
std::uint16_t g : 6;
std::uint16_t r : 5;
};

struct a4r4g4b4 {
std::uint16_t b : 4;
std::uint16_t g : 4;
std::uint16_t r : 4;
std::uint16_t a : 4;
};

struct a1r5g5b5 {
std::uint16_t b : 5;
std::uint16_t g : 5;
std::uint16_t r : 5;
std::uint16_t a : 1;
};
#pragma pack(pop)

/// \brief Calculates the size in bytes of a texture at the given mipmap level.
Expand Down Expand Up @@ -149,6 +163,28 @@ namespace zenkit {

break;
}
case TextureFormat::A4R4G4B4: {
for (auto i = 0u; i < width * height; ++i) {
auto* argb = reinterpret_cast<a4r4g4b4 const*>(&bytes[i * 2]);
conv[i * 4 + 0] = argb->r * 17;
conv[i * 4 + 1] = argb->g * 17;
conv[i * 4 + 2] = argb->b * 17;
conv[i * 4 + 3] = argb->a * 17;
}

break;
}
case TextureFormat::A1R5G5B5: {
for (auto i = 0u; i < width * height; ++i) {
auto* argb = reinterpret_cast<a1r5g5b5 const*>(&bytes[i * 2]);
conv[i * 4 + 0] = static_cast<uint8_t>(static_cast<float>(argb->r) * 8.225806452f);
conv[i * 4 + 1] = static_cast<uint8_t>(static_cast<float>(argb->g) * 8.225806452f);
conv[i * 4 + 2] = static_cast<uint8_t>(static_cast<float>(argb->b) * 8.225806452f);
conv[i * 4 + 3] = argb->a * 255;
}

break;
}
default:
throw ParserError {"texture",
"cannot convert format to rgba: " + std::to_string(static_cast<int32_t>(src))};
Expand Down

0 comments on commit 466be10

Please sign in to comment.