wowlib 0.0.0
Read & write World of Warcraft client files — a C++26 core
Loading...
Searching...
No Matches
codec.hpp
Go to the documentation of this file.
1#pragma once
2
19
20#include <array>
21#include <cstddef>
22#include <cstdint>
23#include <span>
24#include <vector>
25
27#include <wowlib/core/error.hpp>
30
33 using BlockPixels = std::array<std::uint8_t, 16 * 4>;
34
40 class DxtCodec {
41 public:
43 explicit DxtCodec(PixelFormat format) noexcept : _format{format} {}
44
47 [[nodiscard]]
48 std::size_t blockBytes() const noexcept {
49 return _format == PixelFormat::Dxt1 ? 8 : 16;
50 }
51
57 [[nodiscard]]
58 std::size_t encodedSize(std::uint32_t width, std::uint32_t height) const noexcept {
59 return ((width + 3) / 4) * std::size_t{(height + 3) / 4} * blockBytes();
60 }
61
69 [[nodiscard]]
70 Result<std::vector<std::uint8_t>> decode(std::span<const std::byte> src,
71 std::uint32_t width,
72 std::uint32_t height) const;
73
80 [[nodiscard]]
81 Result<FileBuffer> encode(std::span<const std::uint8_t> rgba, std::uint32_t width, std::uint32_t height) const;
82
83 protected:
90 static void _decodeColorBlock(const std::uint8_t* block, BlockPixels& out, bool punchThrough) noexcept;
91
96 static void _decodeExplicitAlpha(const std::uint8_t* block, BlockPixels& out) noexcept;
97
101 static void _decodeInterpolatedAlpha(const std::uint8_t* block, std::span<std::uint8_t, 16> out) noexcept;
102
107 static void _encodePunchThroughBlock(const BlockPixels& pixels, std::uint8_t* dest) noexcept;
108
118 static BlockPixels _extractBlock(std::span<const std::uint8_t> rgba,
119 std::uint32_t width,
120 std::uint32_t height,
121 std::uint32_t bx,
122 std::uint32_t by) noexcept;
123
124 private:
126 PixelFormat _format;
127 };
128
136 public:
138 explicit PaletteCodec(std::uint8_t alphaDepth) noexcept : _alphaDepth{alphaDepth} {}
139
145 [[nodiscard]]
146 std::size_t encodedSize(std::uint32_t width, std::uint32_t height) const noexcept {
147 const std::size_t texels = std::size_t{width} * height;
148 return texels + (texels * _alphaDepth + 7) / 8;
149 }
150
158 [[nodiscard]]
159 Result<std::vector<std::uint8_t>> decode(std::span<const std::byte> src,
160 std::span<const CImVector, BlpPaletteSize> palette,
161 std::uint32_t width,
162 std::uint32_t height) const;
163
171 [[nodiscard]]
172 Result<FileBuffer> encode(std::span<const std::uint8_t> rgba,
173 std::span<const CImVector, BlpPaletteSize> palette,
174 std::uint32_t width,
175 std::uint32_t height) const;
176
181 [[nodiscard]]
182 std::array<CImVector, BlpPaletteSize> buildPalette(std::span<const std::uint8_t> rgba) const;
183
184 protected:
190 std::uint8_t _unpackAlpha(std::span<const std::byte> plane, std::size_t index) const noexcept;
191
196 void _packAlpha(std::uint8_t alpha, std::span<std::byte> plane, std::size_t index) const noexcept;
197
204 static std::uint8_t _nearestIndex(std::span<const CImVector, BlpPaletteSize> palette,
205 std::uint8_t r,
206 std::uint8_t g,
207 std::uint8_t b) noexcept;
208
209 private:
211 std::uint8_t _alphaDepth;
212 };
213
217 class RawCodec {
218 public:
225 [[nodiscard]]
226 Result<std::vector<std::uint8_t>> decode(std::span<const std::byte> src,
227 std::uint32_t width,
228 std::uint32_t height) const;
229
235 [[nodiscard]]
236 Result<FileBuffer> encode(std::span<const std::uint8_t> rgba, std::uint32_t width, std::uint32_t height) const;
237 };
238
242 public:
250 [[nodiscard]]
251 std::vector<std::uint8_t> downscale(std::span<const std::uint8_t> rgba,
252 std::uint32_t width,
253 std::uint32_t height) const;
254 };
255}
The BLP entity (namespace wowlib::formats::blp): Blizzard's texture format.
The owning byte buffer file contents are read into.
static void _decodeInterpolatedAlpha(const std::uint8_t *block, std::span< std::uint8_t, 16 > out) noexcept
Decode one BC3/BC4 interpolated-alpha block (8 bytes) into 16 values.
Definition blp.cpp:103
static void _decodeColorBlock(const std::uint8_t *block, BlockPixels &out, bool punchThrough) noexcept
Decode one BC1 color block (8 bytes) into 16 RGBA pixels.
Definition blp.cpp:60
static void _encodePunchThroughBlock(const BlockPixels &pixels, std::uint8_t *dest) noexcept
Encode one BC1 punch-through block (color0 <= color1 3-color mode, alpha < 128 texels as index 3) — t...
Definition blp.cpp:139
std::size_t encodedSize(std::uint32_t width, std::uint32_t height) const noexcept
The exact payload size of a level per the block formula ceil(w/4) * ceil(h/4) * blockBytes().
Definition codec.hpp:58
DxtCodec(PixelFormat format) noexcept
Definition codec.hpp:43
Result< std::vector< std::uint8_t > > decode(std::span< const std::byte > src, std::uint32_t width, std::uint32_t height) const
Decode one level's block stream to RGBA8 pixels.
Definition blp.cpp:207
Result< FileBuffer > encode(std::span< const std::uint8_t > rgba, std::uint32_t width, std::uint32_t height) const
Encode RGBA8 pixels to one level's block stream, exactly encodedSize() bytes.
Definition blp.cpp:264
static void _decodeExplicitAlpha(const std::uint8_t *block, BlockPixels &out) noexcept
Decode one BC2 explicit-alpha block (8 bytes, 4 bits per texel) onto the alpha channel of out.
Definition blp.cpp:96
static BlockPixels _extractBlock(std::span< const std::uint8_t > rgba, std::uint32_t width, std::uint32_t height, std::uint32_t bx, std::uint32_t by) noexcept
Extract one 4x4 block from a level, clamping reads at the right/bottom edges (partial blocks repeat t...
Definition blp.cpp:122
std::size_t blockBytes() const noexcept
The byte size of one compressed block (8 for BC1, 16 otherwise).
Definition codec.hpp:48
The mip-chain downscaler: one box-filter halving step per call, the standard non-sRGB-aware average e...
Definition codec.hpp:241
std::vector< std::uint8_t > downscale(std::span< const std::uint8_t > rgba, std::uint32_t width, std::uint32_t height) const
Downscale a level to the next one (dimensions halve, floored at 1); each destination texel averages i...
Definition blp.cpp:556
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,...
Definition blp.cpp:395
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.
Definition blp.cpp:340
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.
Definition codec.hpp:146
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 ...
Definition blp.cpp:320
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 d...
Definition blp.cpp:419
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.
Definition blp.cpp:358
PaletteCodec(std::uint8_t alphaDepth) noexcept
Definition codec.hpp:138
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.
Definition blp.cpp:377
The uncompressed-BGRA codec (ColorEncoding::Bgra / BgraAlt): a pure channel swizzle.
Definition codec.hpp:217
Result< std::vector< std::uint8_t > > decode(std::span< const std::byte > src, std::uint32_t width, std::uint32_t height) const
Decode one level's BGRA bytes to RGBA8 pixels.
Definition blp.cpp:526
Result< FileBuffer > encode(std::span< const std::uint8_t > rgba, std::uint32_t width, std::uint32_t height) const
Encode RGBA8 pixels to one level's BGRA bytes.
Definition blp.cpp:540
The error-handling vocabulary: ErrorCode, Error and the Result<T> alias every fallible wowlib operati...
std::array< std::uint8_t, 16 *4 > BlockPixels
One decoded 4x4 texel block: 16 RGBA pixels, row-major.
Definition codec.hpp:33
PixelFormat
The client-side pixel format hint (the header's preferredFormat byte).
Definition blp.hpp:65
@ Dxt1
BC1: 8-byte blocks, optional 1-bit punch-through alpha.
Definition blp.hpp:66
std::expected< T, Error > Result
Every fallible wowlib operation returns Result<T>; bindings translate the error branch into a target-...
Definition error.hpp:100
The binary-level math and color primitives shared across WoW file formats (wowdev....