Pyrogenesis
13997
|
#include "lib/res/handle.h"
#include "lib/os_path.h"
#include "lib/file/vfs/vfs_path.h"
#include "lib/allocators/dynarray.h"
Go to the source code of this file.
Classes | |
struct | Tex |
stores all data describing an image. More... | |
struct | ScopedTex |
Namespaces | |
ERR | |
Introduction | |
WARN | |
INFO | |
Typedefs | |
typedef void(* | MipmapCB )(size_t level, size_t level_w, size_t level_h, const u8 *RESTRICT level_data, size_t level_data_size, void *RESTRICT cbData) |
callback function for each mipmap level. More... | |
Enumerations | |
enum | TexFlags { TEX_DXT = 0x7, DXT1A = 7, TEX_BGR = 0x08, TEX_ALPHA = 0x10, TEX_GREY = 0x20, TEX_BOTTOM_UP = 0x40, TEX_TOP_DOWN = 0x80, TEX_ORIENTATION = TEX_BOTTOM_UP|TEX_TOP_DOWN, TEX_MIPMAPS = 0x100, TEX_UNDEFINED_FLAGS = ~0x1FF } |
flags describing the pixel format. More... | |
Functions | |
Status | tex_validate (const Tex *t) |
Is the texture object valid and self-consistent? More... | |
void | tex_set_global_orientation (int orientation) |
Set the orientation to which all loaded images will automatically be converted (excepting file formats that don't specify their orientation, i.e. More... | |
void | tex_codec_register_all () |
Manually register codecs. More... | |
void | tex_codec_unregister_all () |
remove all codecs that have been registered. More... | |
Status | tex_decode (const shared_ptr< u8 > &data, size_t data_size, Tex *t) |
decode an in-memory texture file into texture object. More... | |
Status | tex_encode (Tex *t, const OsPath &extension, DynArray *da) |
encode a texture into a memory buffer in the desired file format. More... | |
Status | tex_wrap (size_t w, size_t h, size_t bpp, size_t flags, const shared_ptr< u8 > &data, size_t ofs, Tex *t) |
store the given image data into a Tex object; this will be as if it had been loaded via tex_load. More... | |
void | tex_free (Tex *t) |
free all resources associated with the image and make further use of it impossible. More... | |
Status | tex_transform (Tex *t, size_t transforms) |
Change <t>'s pixel format. More... | |
Status | tex_transform_to (Tex *t, size_t new_flags) |
Change <t>'s pixel format (2nd version) (note: this is equivalent to tex_transform(t, t->flags^new_flags). More... | |
u8 * | tex_get_data (const Tex *t) |
rationale: since Tex is a struct, its fields are accessible to callers. More... | |
u32 | tex_get_average_colour (const Tex *t) |
return the ARGB value of the 1x1 mipmap level of the texture. More... | |
size_t | tex_img_size (const Tex *t) |
return total byte size of the image pixels. More... | |
void | tex_util_foreach_mipmap (size_t w, size_t h, size_t bpp, const u8 *data, int levels_to_skip, size_t data_padding, MipmapCB cb, void *RESTRICT cbData) |
for a series of mipmaps stored from base to highest, call back for each level. More... | |
bool | tex_is_known_extension (const VfsPath &pathname) |
Is the file's extension that of a texture format supported by tex_load? More... | |
size_t | tex_hdr_size (const VfsPath &filename) |
return the minimum header size (i.e. More... | |
Variables | |
const Status | ERR::TEX_UNKNOWN_FORMAT = -120100 |
const Status | ERR::TEX_INCOMPLETE_HEADER = -120101 |
const Status | ERR::TEX_FMT_INVALID = -120102 |
const Status | ERR::TEX_INVALID_COLOR_TYPE = -120103 |
const Status | ERR::TEX_NOT_8BIT_PRECISION = -120104 |
const Status | ERR::TEX_INVALID_LAYOUT = -120105 |
const Status | ERR::TEX_COMPRESSED = -120106 |
const Status | ERR::TEX_INVALID_SIZE = -120107 |
const Status | WARN::TEX_INVALID_DATA = +120108 |
const Status | INFO::TEX_CODEC_CANNOT_HANDLE = +120109 |
const int | TEX_BASE_LEVEL_ONLY = -1 |
special value for levels_to_skip: the callback will only be called for the base mipmap level (i.e. More... | |
typedef void(* MipmapCB)(size_t level, size_t level_w, size_t level_h, const u8 *RESTRICT level_data, size_t level_data_size, void *RESTRICT cbData) |
callback function for each mipmap level.
level | number; 0 for base level (i.e. 100%), or the first one in case some were skipped. |
level_w,level_h | pixel dimensions (powers of 2, never 0) |
level_data | the level's texels |
level_data_size | [bytes] |
cbData | passed through from tex_util_foreach_mipmap. |
enum TexFlags |
flags describing the pixel format.
these are to be interpreted as deviations from "plain" format, i.e. uncompressed RGB.
void tex_codec_register_all | ( | ) |
Manually register codecs.
must be called before first use of a codec (e.g. loading a texture).
This would normally be taken care of by TEX_CODEC_REGISTER, but no longer works when building as a static library. Workaround: hard-code a list of codecs in tex_codec.cpp and call their registration functions.
Definition at line 134 of file tex_codec.cpp.
void tex_codec_unregister_all | ( | ) |
remove all codecs that have been registered.
Definition at line 56 of file tex_codec.cpp.
decode an in-memory texture file into texture object.
FYI, currently BMP, TGA, JPG, JP2, PNG, DDS are supported - but don't rely on this (not all codecs may be included).
data | Input data. |
data_size | Its size [bytes]. |
t | Output texture object. |
void tex_free | ( | Tex * | t | ) |
rationale: since Tex is a struct, its fields are accessible to callers.
this is more for C compatibility than convenience; the following should be used instead of direct access to the corresponding fields because they take care of some dirty work. return a pointer to the image data (pixels), taking into account any header(s) that may come before it.
t | input texture object |
size_t tex_hdr_size | ( | const VfsPath & | filename | ) |
return the minimum header size (i.e.
offset to pixel data) of the file format corresponding to the filename.
rationale: this can be used to optimize calls to tex_write: when allocating the buffer that will hold the image, allocate this much extra and pass the pointer as base+hdr_size. this allows writing the header directly into the output buffer and makes for zero-copy IO.
filename | Filename; only the extension (that after '.') is used. case-insensitive. |
size_t tex_img_size | ( | const Tex * | t | ) |
bool tex_is_known_extension | ( | const VfsPath & | pathname | ) |
Is the file's extension that of a texture format supported by tex_load?
Rationale: tex_load complains if the given file is of an unsupported type. this API allows users to preempt that warning (by checking the filename themselves), and also provides for e.g. enumerating only images in a file picker. an alternative might be a flag to suppress warning about invalid files, but this is open to misuse.
pathname | Only the extension (starting with '.') is used. case-insensitive. |
void tex_set_global_orientation | ( | int | orientation | ) |
void tex_util_foreach_mipmap | ( | size_t | w, |
size_t | h, | ||
size_t | bpp, | ||
const u8 * | data, | ||
int | levels_to_skip, | ||
size_t | data_padding, | ||
MipmapCB | cb, | ||
void *RESTRICT | cbData | ||
) |
for a series of mipmaps stored from base to highest, call back for each level.
w,h | Pixel dimensions. |
bpp | Bits per pixel. |
data | Series of mipmaps. |
levels_to_skip | Number of levels (counting from base) to skip, or TEX_BASE_LEVEL_ONLY to only call back for the base image. Rationale: this avoids needing to special case for images with or without mipmaps. |
data_padding | Minimum pixel dimensions of mipmap levels. This is used in S3TC images, where each level is actually stored in 4x4 blocks. usually 1 to indicate levels are consecutive. |
cb | MipmapCB to call. |
cbData | Extra data to pass to cb. |
Status tex_wrap | ( | size_t | w, |
size_t | h, | ||
size_t | bpp, | ||
size_t | flags, | ||
const shared_ptr< u8 > & | data, | ||
size_t | ofs, | ||
Tex * | t | ||
) |
store the given image data into a Tex object; this will be as if it had been loaded via tex_load.
rationale: support for in-memory images is necessary for emulation of glCompressedTexImage2D and useful overall. however, we don't want to provide an alternate interface for each API; these would have to be changed whenever fields are added to Tex. instead, provide one entry point for specifying images. note: since we do not know how <img> was allocated, the caller must free it themselves (after calling tex_free, which is required regardless of alloc type).
we need only add bookkeeping information and "wrap" it in our Tex struct, hence the name.
w,h | Pixel dimensions. |
bpp | Bits per pixel. |
flags | TexFlags. |
data | Img texture data. note: size is calculated from other params. |
ofs | |
t | output texture object. |