Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Macros | Functions | Variables
tex.cpp File Reference
#include "precompiled.h"
#include "tex.h"
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include "lib/timer.h"
#include "lib/bits.h"
#include "lib/allocators/shared_ptr.h"
#include "lib/sysdep/cpu.h"
#include "tex_codec.h"

Go to the source code of this file.

Classes

struct  CreateLevelData
 

Macros

#define CHECK_TEX(t)   RETURN_STATUS_IF_ERR(tex_validate(t))
 

Functions

 STATUS_ADD_DEFINITIONS (texStatusDefinitions)
 
Status tex_validate (const Tex *t)
 Is the texture object valid and self-consistent? More...
 
Status tex_validate_plain_format (size_t bpp, size_t flags)
 check if the given texture format is acceptable: 8bpp grey, 24bpp color or 32bpp color+alpha (BGR / upside down are permitted). More...
 
void tex_util_foreach_mipmap (size_t w, size_t h, size_t bpp, const u8 *pixels, 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...
 
static void create_level (size_t level, size_t level_w, size_t level_h, const u8 *RESTRICT level_data, size_t level_dataSize, void *RESTRICT cbData)
 
static Status add_mipmaps (Tex *t, size_t w, size_t h, size_t bpp, void *newData, size_t dataSize)
 
 TIMER_ADD_CLIENT (tc_plain_transform)
 
static Status plain_transform (Tex *t, size_t transforms)
 
 TIMER_ADD_CLIENT (tc_transform)
 
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...
 
void tex_set_global_orientation (int o)
 Set the orientation to which all loaded images will automatically be converted (excepting file formats that don't specify their orientation, i.e. More...
 
static void flip_to_global_orientation (Tex *t)
 
bool tex_orientations_match (size_t src_flags, size_t dst_orientation)
 indicate if the two vertical orientations match. More...
 
bool tex_is_known_extension (const VfsPath &pathname)
 Is the file's extension that of a texture format supported by tex_load? 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...
 
u8tex_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...
 
static void add_level_size (size_t level, size_t level_w, size_t level_h, const u8 *RESTRICT level_data, size_t level_dataSize, void *RESTRICT cbData)
 
size_t tex_img_size (const Tex *t)
 return total byte size of the image pixels. More...
 
size_t tex_hdr_size (const VfsPath &filename)
 return the minimum header size (i.e. More...
 
Status tex_decode (const shared_ptr< u8 > &data, size_t dataSize, 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...
 

Variables

static const StatusDefinition texStatusDefinitions []
 
static int global_orientation = TEX_TOP_DOWN
 

Macro Definition Documentation

#define CHECK_TEX (   t)    RETURN_STATUS_IF_ERR(tex_validate(t))

Definition at line 94 of file tex.cpp.

Function Documentation

static void add_level_size ( size_t  level,
size_t  level_w,
size_t  level_h,
const u8 *RESTRICT  level_data,
size_t  level_dataSize,
void *RESTRICT  cbData 
)
static

Definition at line 674 of file tex.cpp.

static Status add_mipmaps ( Tex t,
size_t  w,
size_t  h,
size_t  bpp,
void *  newData,
size_t  dataSize 
)
static

Definition at line 253 of file tex.cpp.

static void create_level ( size_t  level,
size_t  level_w,
size_t  level_h,
const u8 *RESTRICT  level_data,
size_t  level_dataSize,
void *RESTRICT  cbData 
)
static

Definition at line 186 of file tex.cpp.

static void flip_to_global_orientation ( Tex t)
static

Definition at line 520 of file tex.cpp.

static Status plain_transform ( Tex t,
size_t  transforms 
)
static

Definition at line 287 of file tex.cpp.

STATUS_ADD_DEFINITIONS ( texStatusDefinitions  )
Status tex_decode ( const shared_ptr< u8 > &  data,
size_t  data_size,
Tex t 
)

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).

Parameters
dataInput data.
data_sizeIts size [bytes].
tOutput texture object.
Returns
Status.

Definition at line 717 of file tex.cpp.

Status tex_encode ( Tex t,
const OsPath extension,
DynArray da 
)

encode a texture into a memory buffer in the desired file format.

Parameters
tInput texture object.
extension(including '.').
daOutput memory array. Allocated here; caller must free it when no longer needed. Invalid unless function succeeds.
Returns
Status

Definition at line 750 of file tex.cpp.

void tex_free ( Tex t)

free all resources associated with the image and make further use of it impossible.

Parameters
ttexture object (note: not zeroed afterwards; see impl)
Returns
Status

Definition at line 610 of file tex.cpp.

u32 tex_get_average_colour ( const Tex t)

return the ARGB value of the 1x1 mipmap level of the texture.

Parameters
tinput texture object
Returns
ARGB value (or 0 if texture does not have mipmaps)

Definition at line 641 of file tex.cpp.

u8* tex_get_data ( const 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.

Parameters
tinput texture object
Returns
pointer to data returned by mem_get_ptr (holds reference)!

Definition at line 629 of file tex.cpp.

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.

Parameters
filenameFilename; only the extension (that after '.') is used. case-insensitive.
Returns
size [bytes] or 0 on error (i.e. no codec found).

Definition at line 703 of file tex.cpp.

size_t tex_img_size ( const Tex t)

return total byte size of the image pixels.

(including mipmaps!) rationale: this is preferable to calculating manually because it's less error-prone (e.g. confusing bits_per_pixel with bytes).

Parameters
tinput texture object
Returns
size [bytes]

Definition at line 683 of file tex.cpp.

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.

Parameters
pathnameOnly the extension (starting with '.') is used. case-insensitive.
Returns
bool

Definition at line 570 of file tex.cpp.

bool tex_orientations_match ( size_t  src_flags,
size_t  dst_orientation 
)

indicate if the two vertical orientations match.

used by tex_codec.

Parameters
src_flagsTexFlags, used to extract the orientation. we ask for this instead of src_orientation so callers don't have to mask off TEX_ORIENTATION.
dst_orientationorientation to compare against. can be one of TEX_BOTTOM_UP, TEX_TOP_DOWN, or 0 for the "global orientation".
Returns
bool

Definition at line 548 of file tex.cpp.

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.

DDS). See "Default Orientation" in docs.

Parameters
orientationEither TEX_BOTTOM_UP or TEX_TOP_DOWN.

Definition at line 513 of file tex.cpp.

Status tex_transform ( Tex t,
size_t  transforms 
)

Change <t>'s pixel format.

Parameters
tInput texture object.
transformsTexFlags that are to be flipped.
Returns
Status

Definition at line 467 of file tex.cpp.

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).

Parameters
tInput texture object.
new_flagsdesired new value of TexFlags.
Returns
Status

Definition at line 494 of file tex.cpp.

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.

Parameters
w,hPixel dimensions.
bppBits per pixel.
dataSeries of mipmaps.
levels_to_skipNumber 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_paddingMinimum 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.
cbMipmapCB to call.
cbDataExtra data to pass to cb.

Definition at line 132 of file tex.cpp.

Status tex_validate ( const Tex t)

Is the texture object valid and self-consistent?

Parameters
t
Returns
Status

Definition at line 60 of file tex.cpp.

Status tex_validate_plain_format ( size_t  bpp,
size_t  flags 
)

check if the given texture format is acceptable: 8bpp grey, 24bpp color or 32bpp color+alpha (BGR / upside down are permitted).

basically, this is the "plain" format understood by all codecs and tex_codec_plain_transform.

Parameters
bppbits per pixel
flagsTexFlags
Returns
Status

Definition at line 101 of file tex.cpp.

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.

Parameters
w,hPixel dimensions.
bppBits per pixel.
flagsTexFlags.
dataImg texture data. note: size is calculated from other params.
ofs
toutput texture object.
Returns
Status

Definition at line 593 of file tex.cpp.

TIMER_ADD_CLIENT ( tc_plain_transform  )
TIMER_ADD_CLIENT ( tc_transform  )

Variable Documentation

int global_orientation = TEX_TOP_DOWN
static

Definition at line 508 of file tex.cpp.

const StatusDefinition texStatusDefinitions[]
static
Initial value:
= {
{ ERR::TEX_FMT_INVALID, L"Invalid/unsupported texture format" },
{ ERR::TEX_INVALID_COLOR_TYPE, L"Invalid color type" },
{ ERR::TEX_NOT_8BIT_PRECISION, L"Not 8-bit channel precision" },
{ ERR::TEX_INVALID_LAYOUT, L"Unsupported texel layout, e.g. right-to-left" },
{ ERR::TEX_COMPRESSED, L"Unsupported texture compression" },
{ WARN::TEX_INVALID_DATA, L"Warning: invalid texel data encountered" },
{ ERR::TEX_INVALID_SIZE, L"Texture size is incorrect" },
{ INFO::TEX_CODEC_CANNOT_HANDLE, L"Texture codec cannot handle the given format" }
}
const Status TEX_INVALID_COLOR_TYPE
Definition: tex.h:118
const Status TEX_CODEC_CANNOT_HANDLE
Definition: tex.h:132
const Status TEX_COMPRESSED
Definition: tex.h:121
const Status TEX_FMT_INVALID
Definition: tex.h:117
const Status TEX_NOT_8BIT_PRECISION
Definition: tex.h:119
const Status TEX_INVALID_DATA
Definition: tex.h:127
const Status TEX_INVALID_LAYOUT
Definition: tex.h:120
const Status TEX_INVALID_SIZE
Definition: tex.h:122

Definition at line 42 of file tex.cpp.