NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
texture.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <math.h>
15
16#include <cstring>
17#include <map>
18#include <vector>
19
21#include "NNL/common/io.hpp"
24
25namespace nnl {
36namespace texture {
42
47constexpr unsigned int kMinDimension = 1;
53constexpr unsigned int kMinByteBufferWidth = 16;
60constexpr unsigned int kMaxDimension = 1024;
61
66constexpr std::size_t kMaxMipMapLvl = 7;
67
72constexpr std::size_t kMaxTextureLvl = 1 + kMaxMipMapLvl;
73
84enum class TextureFormat : u16 {
85 kCLUT8 = 5,
87 kCLUT4 = 4,
93};
94
101enum class ClutFormat : u8 {
106};
107
131
155
178
191using TextureContainer = std::vector<Texture>;
192
216
233std::vector<STexture> Convert(const TextureContainer& texture_container, bool mipmaps = false);
234
248TextureContainer Convert(std::vector<STexture>&& images, const ConvertParam& texture_params = {});
249
250TextureContainer Convert(std::vector<STexture>&& images, const std::vector<ConvertParam>& texture_params);
251
263bool IsOfType(BufferView buffer);
264
265bool IsOfType(const std::filesystem::path& path);
266
267bool IsOfType(Reader& f);
268
284
285TextureContainer Import(const std::filesystem::path& path);
286
288
299[[nodiscard]] Buffer Export(const TextureContainer& texture_container);
300
301void Export(const TextureContainer& texture_container, const std::filesystem::path& path);
302
303void Export(const TextureContainer& texture_container, Writer& f); // Main
305
311
329Texture Convert(STexture&& image, const ConvertParam& tex_param = {});
345STexture Convert(const Texture& texture, unsigned int level = 0);
346
417Buffer UnswizzleFromMem(BufferView texptr, u32 bufw, u32 height, u32 bytes_per_pixel);
436Buffer SwizzleFromMem(Buffer& texptr, u32 bufw, u32 height, u32 bytes_per_pixel);
479std::vector<SPixel> GeneratePaletteMedian(const STexture& source_image, std::size_t num_colors = 256);
494std::vector<SPixel> GeneratePaletteNaive(const STexture& source_image, std::size_t num_colors = 256);
511std::vector<u8> ApplyPalette(const STexture& image, const std::vector<SPixel>& palette, bool dither = true);
512
528Buffer AlignBufferWidth(BufferView image_buffer, unsigned int width, unsigned int height, unsigned int bpp,
529 unsigned int buffer_width);
545Buffer DealignBufferWidth(BufferView image_buffer, unsigned int width, unsigned int height, unsigned int bpp,
546 unsigned int buffer_width);
561
589std::vector<ConvertParam> GenerateConvertParam(const TextureContainer& texture_container);
590
592
593namespace raw {
594
608
615constexpr u32 kMagicBytes = 0x88'88'00'01;
616
617constexpr u32 kColorEntriesInBlock = 16;
618
619NNL_PACK(struct RHeader {
620 u32 magic_bytes = kMagicBytes; // 0x0
621 u32 offset_textures = 0; // 0x4
622 u32 offset_texture_data = 0; // 0x8
623 u32 offset_clut_offsets = 0; // 0xC
624 u16 num_textures = 0; // 0x10
625 u16 num_texture_data = 0; // 0x12
626 u16 num_clut_offsets = 0; // 0x14
627 u16 unknown = 0; // 0x16
628});
629
630static_assert(sizeof(RHeader) == 0x18);
631
632NNL_PACK(struct RTexture {
633 u16 texture_format = 0; // 0x0
634 u16 index_texture_data = 0; // 0x2
635 u8 num_texture_data_mipmaps = 0; // 0x4
636 u8 swizzled = false; // 0x5
637 u8 filter_minifying = 0; // 0x6
638 u8 filter_magnifying = 0; // 0x7
639 u16 index_clut = 0; // 0x8
640 u8 clut_load_size = 0; // 0xA how many blocks of 16 colors to load
641 u8 clut_format = 0; // 0xB
642});
643
644static_assert(sizeof(RTexture) == 0xC);
645
646NNL_PACK(struct RTextureData {
647 u32 offset_bitmap = 0; // 0x0
648 u16 width = 0; // 0x4
649 u16 height = 0; // 0x6
650 u16 buffer_width = 0; // 0x8
651 u16 height_rounded = 0; // 0xA same as height or rounded to 8 (filled dynamically)
652 u32 size = 0; // 0xC
653 u32 address = 0; // 0x10 filled dynamically
654});
655
656static_assert(sizeof(RTextureData) == 0x14);
657
658struct RTextureContainer {
659 RHeader header;
660 std::vector<RTexture> textures;
661 std::vector<RTextureData> texture_data;
662 std::vector<u32> clut_offsets;
663 // Maps of offsets (used in the structures above) to their data:
664 std::map<u32, Buffer> bitmaps;
665 std::map<u32, Buffer> cluts;
666};
667
668TextureContainer Convert(RTextureContainer&& rtexture_container);
669
670RTextureContainer Parse(Reader& f);
672} // namespace raw
673
674} // namespace texture
675
676} // namespace nnl
Contains macros and definitions for fixed-width types.
std::uint16_t u16
16-bit unsigned integer
Definition fixed_type.hpp:62
std::uint32_t u32
32-bit unsigned integer
Definition fixed_type.hpp:60
std::uint8_t u8
8-bit unsigned integer
Definition fixed_type.hpp:64
#define NNL_PACK(...)
A structure packing directive.
Definition fixed_type.hpp:41
Reader implementation for read-only memory buffers.
Definition io.hpp:598
Abstract class for writing data.
Definition io.hpp:136
Abstract class for reading data.
Definition io.hpp:78
std::vector< u8 > Buffer
A type alias for std::vector<u8> that denotes a raw, contiguous memory region that may be interpreted...
Definition io.hpp:40
Represents a simple texture.
Definition stexture.hpp:77
std::vector< u8 > ApplyPalette(const STexture &image, const std::vector< SPixel > &palette, bool dither=true)
Applies a color palette to the image.
Buffer ConvertRGBA4444ToRGBA8888(BufferView bitmap4444)
Converts an image buffer from RGBA4444 format to RGBA8888 format.
Buffer ConvertRGB565ToRGBA8888(BufferView bitmap565)
Converts an image buffer from RGB565 format to RGBA8888 format.
Buffer UnswizzleFromMem(BufferView texptr, u32 bufw, u32 height, u32 bytes_per_pixel)
Unswizzles texture data.
Buffer ConvertRGBA5551ToRGBA8888(BufferView bitmap5551)
Converts an image buffer from RGBA5551 format to RGBA8888 format.
ConvertParam GenerateConvertParam(const Texture &texture)
Generates conversion parameters from a given texture.
Buffer AlignBufferWidth(BufferView image_buffer, unsigned int width, unsigned int height, unsigned int bpp, unsigned int buffer_width)
Aligns the buffer width to a desired buffer width.
u32 QuickTexHash(BufferView data)
Computes a quick hash for a texture.
Buffer DealignBufferWidth(BufferView image_buffer, unsigned int width, unsigned int height, unsigned int bpp, unsigned int buffer_width)
Removes padding from an aligned image buffer.
std::vector< SPixel > GeneratePaletteMedian(const STexture &source_image, std::size_t num_colors=256)
Creates a color palette using the median cut algorithm.
Buffer SwizzleFromMem(Buffer &texptr, u32 bufw, u32 height, u32 bytes_per_pixel)
Swizzles texture data.
Buffer ConvertIndexed8To4(BufferView clut8, u32 width)
Converts an 8-bit indexed texture to a 4-bit indexed texture.
std::vector< SPixel > GeneratePaletteNaive(const STexture &source_image, std::size_t num_colors=256)
Creates a color palette by extracting the first N unique colors from the image.
Buffer ConvertRGBA8888ToRGBA4444(BufferView bitmap8888)
Converts an image buffer from RGBA8888 format to RGBA4444 format.
Buffer ConvertRGBA8888ToRGBA5551(BufferView bitmap8888)
Converts an image buffer from RGBA8888 format to RGBA5551 format.
Buffer ConvertRGBA8888ToRGB565(BufferView bitmap8888)
Converts an image buffer from RGBA8888 format to RGB565 format.
Buffer DeindexClut8ToRGBA8888(BufferView indexed_image, BufferView palette)
Converts an 8-bit indexed image buffer to an RGBA8888 buffer.
Buffer ConvertIndexed4To8(BufferView clut4, u32 width)
Converts a 4-bit indexed texture to an 8-bit indexed texture.
utl::static_vector< TextureData, kMaxTextureLvl > texture_data
Definition texture.hpp:166
bool swizzled
Definition texture.hpp:171
TextureFilter mag_filter
Definition texture.hpp:174
bool clut_dither
Definition texture.hpp:205
u16 buffer_width
Definition texture.hpp:150
ClutFormat clut_format
Definition texture.hpp:203
Buffer bitmap_buffer
Raw bitmap data. Its interpretation depends on the texture_format.
Definition texture.hpp:153
ClutFormat clut_format
Format of the CLUT data.
Definition texture.hpp:176
u16 height
Definition texture.hpp:148
bool swizzle
Definition texture.hpp:213
Buffer clut_buffer
Definition texture.hpp:164
unsigned int max_mipmap_lvl
The maximum number of mipmap levels to generate.
Definition texture.hpp:208
TextureFormat texture_format
The format the texture will be converted to.
Definition texture.hpp:202
TextureFilter min_filter
Minification filter method for texture sampling.
Definition texture.hpp:173
TextureFormat texture_format
Format of the texture data.
Definition texture.hpp:170
u16 width
Width of the texture in pixels. It must be a power of 2.
Definition texture.hpp:147
bool gen_small_mipmap
Definition texture.hpp:209
unsigned int max_width_height
Definition texture.hpp:211
Structure representing a texture.
Definition texture.hpp:163
Structure representing image data of a texture.
Definition texture.hpp:146
Structure to hold parameters for texture conversion.
Definition texture.hpp:201
TextureFormat
Enumeration of texture formats.
Definition texture.hpp:84
std::vector< STexture > Convert(const TextureContainer &texture_container, bool mipmaps=false)
Converts a container of textures to a simplified representation.
constexpr unsigned int kMaxDimension
Maximum allowed width and height of a texture in pixels.
Definition texture.hpp:60
constexpr std::size_t kMaxMipMapLvl
The maximum number of mipmap levels that can be used.
Definition texture.hpp:66
TextureFilter
Enumeration of texture filtering methods.
Definition texture.hpp:122
constexpr std::size_t kMaxTextureLvl
The maximum number of texture levels.
Definition texture.hpp:72
std::vector< Texture > TextureContainer
Type alias for a container of textures.
Definition texture.hpp:191
bool IsOfType(BufferView buffer)
Tests if the provided file is a texture container.
ClutFormat
Enumeration of color formats used in Color Lookup Tables (CLUT).
Definition texture.hpp:101
Buffer Export(const TextureContainer &texture_container)
Converts a texture container to its binary file representation.
constexpr unsigned int kMinDimension
Minimum allowed width and height of a texture in pixels.
Definition texture.hpp:47
constexpr unsigned int kMinByteBufferWidth
Minimum width of a texture buffer in bytes (as expected by the GE). The actual pixel width can be sma...
Definition texture.hpp:53
TextureContainer Import(BufferView buffer)
Parses a binary file and converts it to a structured texture container.
@ kCLUT4
Definition texture.hpp:87
@ kRGB565
16-bit color format. No CLUT.
Definition texture.hpp:92
@ kRGBA8888
32-bit color format with 8 bits per channel. No CLUT.
Definition texture.hpp:89
@ kCLUT8
Definition texture.hpp:85
@ kRGBA4444
16-bit color format with 4 bits per channel. No CLUT.
Definition texture.hpp:90
@ kRGBA5551
16-bit color format. No CLUT.
Definition texture.hpp:91
@ kLinear
Linear filtering.
Definition texture.hpp:124
@ kLinearMipmapLinear
Linear filtering with mipmap selection.
Definition texture.hpp:129
@ kNearestMipmapNearest
Nearest filtering with mipmap selection.
Definition texture.hpp:126
@ kNearest
Nearest-neighbor filtering.
Definition texture.hpp:123
@ kLinearMipmapNearest
Linear filtering with mipmap selection.
Definition texture.hpp:127
@ kNearestMipmapLinear
Nearest filtering with mipmap selection.
Definition texture.hpp:128
@ kRGBA8888
32-bit color format. 8 bits per channel.
Definition texture.hpp:102
constexpr u32 kMagicBytes
Magic bytes used to identify the texture format.
Definition texture.hpp:615
std::vector like class with a fixed-size inline storage (aka std::inplace_vector)
Definition static_vector.hpp:52
Provides classes for reading and writing binary data to and from various sources.
Contains structures and functions for working with in-game textures.
Definition texture.hpp:36
Definition exception.hpp:56
Defines the static_vector class that functions like std::vector but stores its elements within the ob...
Provides data structures for representing essential components of a texture.