wowlib 0.0.0
Read & write World of Warcraft client files — a C++26 core
Loading...
Searching...
No Matches
wowlib::formats::blp::detail::PaletteCodec Class Reference

The palettized codec. More...

#include <wowlib/formats/blp/codec.hpp>

Public Member Functions

 PaletteCodec (std::uint8_t alphaDepth) noexcept
std::size_t encodedSize (std::uint32_t width, std::uint32_t height) const noexcept
 The exact payload size of a level: one index byte per pixel plus the packed alpha plane.
Result< std::vector< std::uint8_t > > decode (std::span< const std::byte > src, std::span< const CImVector, BlpPaletteSize > palette, std::uint32_t width, std::uint32_t height) const
 Decode one level's index + alpha-plane payload to RGBA8 pixels.
Result< FileBuffer > encode (std::span< const std::uint8_t > rgba, std::span< const CImVector, BlpPaletteSize > palette, std::uint32_t width, std::uint32_t height) const
 Encode RGBA8 pixels against an existing palette: nearest-entry indices plus the packed alpha plane, exactly encodedSize() bytes.
std::array< CImVector, BlpPaletteSize > buildPalette (std::span< const std::uint8_t > rgba) const
 Build a 256-entry palette for an image (median-cut over the opaque RGB values; an image with <= 256 distinct colors gets them verbatim).

Protected Member Functions

std::uint8_t _unpackAlpha (std::span< const std::byte > plane, std::size_t index) const noexcept
 The alpha value of texel index unpacked from the plane at this codec's bit depth (255 when the depth is 0).
void _packAlpha (std::uint8_t alpha, std::span< std::byte > plane, std::size_t index) const noexcept
 Pack one texel's alpha into the plane at this codec's bit depth.

Static Protected Member Functions

static std::uint8_t _nearestIndex (std::span< const CImVector, BlpPaletteSize > palette, std::uint8_t r, std::uint8_t g, std::uint8_t b) noexcept
 The palette entry nearest to (r, g, b) by squared RGB distance.

Detailed Description

The palettized codec.

Constructed with the alpha-plane bit depth (its state — 0, 1, 4 or 8 bits per pixel); decode() looks indices up in the 256-entry BGRX palette and unpacks the trailing alpha plane, encode() maps pixels to nearest palette entries and packs the plane. Palette construction (median-cut quantization) is its own verb so one palette serves every mip level.

Definition at line 135 of file codec.hpp.

Constructor & Destructor Documentation

◆ PaletteCodec()

wowlib::formats::blp::detail::PaletteCodec::PaletteCodec ( std::uint8_t alphaDepth)
inlineexplicitnoexcept
Parameters
alphaDepththe alpha plane's bits per pixel (0, 1, 4 or 8).

Definition at line 138 of file codec.hpp.

Member Function Documentation

◆ _nearestIndex()

std::uint8_t wowlib::formats::blp::detail::PaletteCodec::_nearestIndex ( std::span< const CImVector, BlpPaletteSize > palette,
std::uint8_t r,
std::uint8_t g,
std::uint8_t b )
staticprotectednoexcept

The palette entry nearest to (r, g, b) by squared RGB distance.

Parameters
palettethe color table.
rthe red value.
gthe green value.
bthe blue value.
Returns
the nearest entry's index.

Definition at line 358 of file blp.cpp.

References wowlib::formats::blp::BlpPaletteSize.

Referenced by encode().

◆ _packAlpha()

void wowlib::formats::blp::detail::PaletteCodec::_packAlpha ( std::uint8_t alpha,
std::span< std::byte > plane,
std::size_t index ) const
protectednoexcept

Pack one texel's alpha into the plane at this codec's bit depth.

Parameters
alphathe 8-bit alpha value.
planethe packed plane bytes (pre-sized, zero-filled).
indexthe texel ordinal.

Definition at line 340 of file blp.cpp.

Referenced by encode().

◆ _unpackAlpha()

std::uint8_t wowlib::formats::blp::detail::PaletteCodec::_unpackAlpha ( std::span< const std::byte > plane,
std::size_t index ) const
protectednoexcept

The alpha value of texel index unpacked from the plane at this codec's bit depth (255 when the depth is 0).

Parameters
planethe packed alpha plane bytes.
indexthe texel ordinal.
Returns
the 8-bit alpha value.

Definition at line 320 of file blp.cpp.

Referenced by decode().

◆ buildPalette()

std::array< CImVector, BlpPaletteSize > wowlib::formats::blp::detail::PaletteCodec::buildPalette ( std::span< const std::uint8_t > rgba) const
nodiscard

Build a 256-entry palette for an image (median-cut over the opaque RGB values; an image with <= 256 distinct colors gets them verbatim).

Parameters
rgbathe level-0 RGBA bytes.
Returns
the color table (unused tail entries zero).

Definition at line 419 of file blp.cpp.

References wowlib::formats::blp::BlpPaletteSize.

Referenced by wowlib::formats::blp::BLP::encode().

◆ decode()

Result< std::vector< std::uint8_t > > wowlib::formats::blp::detail::PaletteCodec::decode ( std::span< const std::byte > src,
std::span< const CImVector, BlpPaletteSize > palette,
std::uint32_t width,
std::uint32_t height ) const
nodiscard

Decode one level's index + alpha-plane payload to RGBA8 pixels.

Parameters
srcthe on-disk payload bytes; short payloads decode their missing texels transparent black.
palettethe file's 256-entry color table.
widththe level width in pixels.
heightthe level height in pixels.
Returns
width * height * 4 RGBA bytes.

Definition at line 377 of file blp.cpp.

References _unpackAlpha(), wowlib::formats::common::CImVector::b, wowlib::formats::common::CImVector::g, and wowlib::formats::common::CImVector::r.

Referenced by wowlib::formats::blp::BLP::decode().

◆ encode()

Result< FileBuffer > wowlib::formats::blp::detail::PaletteCodec::encode ( std::span< const std::uint8_t > rgba,
std::span< const CImVector, BlpPaletteSize > palette,
std::uint32_t width,
std::uint32_t height ) const
nodiscard

Encode RGBA8 pixels against an existing palette: nearest-entry indices plus the packed alpha plane, exactly encodedSize() bytes.

Parameters
rgbawidth * height * 4 RGBA bytes.
palettethe color table to index into.
widththe level width in pixels.
heightthe level height in pixels.
Returns
the payload bytes.

Definition at line 395 of file blp.cpp.

References _nearestIndex(), _packAlpha(), and encodedSize().

Referenced by wowlib::formats::blp::BLP::encode().

◆ encodedSize()

std::size_t wowlib::formats::blp::detail::PaletteCodec::encodedSize ( std::uint32_t width,
std::uint32_t height ) const
inlinenodiscardnoexcept

The exact payload size of a level: one index byte per pixel plus the packed alpha plane.

Parameters
widththe level width in pixels.
heightthe level height in pixels.
Returns
the encoded byte count.

Definition at line 146 of file codec.hpp.

Referenced by encode().


The documentation for this class was generated from the following files: