NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
stexture.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <filesystem>
15#include <string>
16#include <vector>
17
19#include "NNL/common/io.hpp"
21
22namespace nnl {
23
30
36struct SPixel {
37 u8 r;
38 u8 g;
39 u8 b;
40 u8 a;
46 operator u32() const { return (a << 24) | (b << 16) | (g << 8) | r; }
47};
48
63
77struct STexture {
78 std::string name;
79
80 unsigned int width = 0;
81
82 unsigned int height = 0;
83
85
87
88 std::vector<SPixel> bitmap;
89
91
98 void Resize(unsigned int new_width, unsigned int new_height);
99
108 [[nodiscard]] static STexture Import(const std::filesystem::path& path, bool flip = false);
109
118 [[nodiscard]] static STexture Import(BufferView buffer, bool flip = false);
119
131 [[nodiscard]] static STexture GenerateChessPattern(unsigned int width = 128, unsigned int height = 128);
132
139 void ExportPNG(const std::filesystem::path& path, bool flip = false) const;
140
147 [[nodiscard]] Buffer ExportPNG(bool flip = false) const;
148
152 void FlipV();
153
162
171
180 void QuantizeAlpha(unsigned int levels = 256);
181
187 bool HasAlpha() const;
188};
189
190
191} // namespace nnl
Contains macros and definitions for fixed-width types.
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
Reader implementation for read-only memory buffers.
Definition io.hpp:598
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
std::vector< SPixel > bitmap
Pixel data of the texture.
Definition stexture.hpp:88
STextureFilter min_filter
Texture sampling filter.
Definition stexture.hpp:84
void QuantizeAlpha(unsigned int levels=256)
Reduces alpha channel precision to the specified number of levels.
void FlipV()
Flips the texture vertically.
unsigned int height
Height of the texture in pixels.
Definition stexture.hpp:82
STextureFilter mag_filter
Texture sampling filter.
Definition stexture.hpp:86
u8 b
Blue channel [0, 255].
Definition stexture.hpp:39
u8 g
Green channel [0, 255].
Definition stexture.hpp:38
SValue extras
Any additional custom data.
Definition stexture.hpp:90
Buffer ExportPNG(bool flip=false) const
Exports the texture to a PNG file.
static STexture Import(BufferView buffer, bool flip=false)
Constructs a texture from the specified buffer.
void Resize(unsigned int new_width, unsigned int new_height)
Resizes the texture.
void AlignWidth()
Aligns the width to the next power of 2.
void AlignHeight()
Aligns the height to the next power of 2.
static STexture GenerateChessPattern(unsigned int width=128, unsigned int height=128)
Creates a chessboard pattern texture.
unsigned int width
Width of the texture in pixels.
Definition stexture.hpp:80
std::string name
An optional name for the texture.
Definition stexture.hpp:78
u8 r
Red channel [0, 255].
Definition stexture.hpp:37
static STexture Import(const std::filesystem::path &path, bool flip=false)
Constructs a texture from the specified file path.
u8 a
Alpha channel [0, 255].
Definition stexture.hpp:40
bool HasAlpha() const
Checks if the texture contains any transparent pixels.
void ExportPNG(const std::filesystem::path &path, bool flip=false) const
Exports the texture to a PNG file.
Represents a pixel with red, green, blue, and alpha color channels.
Definition stexture.hpp:36
Represents a simple texture.
Definition stexture.hpp:77
STextureFilter
Specifies the filtering methods used for texture sampling.
Definition stexture.hpp:55
@ kLinear
Linear filtering.
Definition stexture.hpp:57
@ kLinearMipmapLinear
Linear filtering with mipmap selection.
Definition stexture.hpp:61
@ kNearestMipmapNearest
Nearest filtering with mipmap selection.
Definition stexture.hpp:58
@ kNearest
Nearest-neighbor filtering.
Definition stexture.hpp:56
@ kLinearMipmapNearest
Linear filtering with mipmap selection.
Definition stexture.hpp:59
@ kNearestMipmapLinear
Nearest filtering with mipmap selection.
Definition stexture.hpp:60
A class representing JSON-like values (null, bool, numbers, strings, arrays, or objects)
Definition svalue.hpp:41
Provides classes for reading and writing binary data to and from various sources.
Definition exception.hpp:56
Provides a class for representing "extras" fields for custom metadata.