Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
tex_codec.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * support routines and interface for texture codecs.
25 */
26
27#ifndef INCLUDED_TEX_CODEC
28#define INCLUDED_TEX_CODEC
29
30#include "lib/code_annotation.h"
31#include "lib/os_path.h"
32#include "lib/status.h"
33
34#include <cstddef>
35#include <vector>
36
37class Tex;
38struct DynArray;
39
40/**
41 * virtual method table for TexCodecs.
42 * rationale: this works in C and also allows storing name and next in vtbl.
43 * 'template method'-style interface to increase code reuse and
44 * simplify writing new codecs.
45 **/
47{
48public:
49 /**
50 * decode the file into a Tex structure.
51 *
52 * @param data input data array (non-const, because the texture
53 * may have to be flipped in-place - see "texture orientation").
54 * @param size [bytes] of data, always >= 4
55 * (this is usually enough to compare the header's "magic" field,
56 * and no legitimate file will be smaller)
57 * @param t output texture object
58 * @return Status
59 **/
60 virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const = 0;
61
62 /**
63 * encode the texture data into the codec's file format (in memory).
64 *
65 * @param t input texture object. note: non-const because encoding may
66 * require a Tex::transform.
67 * @param da output data array, allocated by codec.
68 * rationale: some codecs cannot calculate the output size beforehand
69 * (e.g. PNG output via libpng), so the output memory cannot be allocated
70 * by the caller.
71 * @return Status
72 **/
73 virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const = 0;
74
75 /**
76 * transform the texture's pixel format.
77 *
78 * @param t texture object
79 * @param transforms: OR-ed combination of TEX_* flags that are to
80 * be changed. note: the codec needs only handle situations specific
81 * to its format; generic pixel format transforms are handled by
82 * the caller.
83 **/
84 virtual Status transform(Tex* t, size_t transforms) const = 0;
85
86 /**
87 * indicate if the data appears to be an instance of this codec's header,
88 * i.e. can this codec decode it?
89 *
90 * @param file input data; only guaranteed to be 4 bytes!
91 * (this should be enough to examine the header's 'magic' field)
92 * @return bool
93 **/
94 virtual bool is_hdr(const std::uint8_t* file) const = 0;
95
96 /**
97 * is the extension that of a file format supported by this codec?
98 *
99 * rationale: cannot just return the extension string and have
100 * caller compare it (-> smaller code) because a codec's file format
101 * may have several valid extensions (e.g. jpg and jpeg).
102 *
103 * @param extension (including '.')
104 * @return bool
105 **/
106 virtual bool is_ext(const OsPath& extension) const = 0;
107
108 /**
109 * return size of the file header supported by this codec.
110 *
111 * @param file the specific header to return length of (taking its
112 * variable-length fields into account). if NULL, return minimum
113 * guaranteed header size, i.e. the header without any
114 * variable-length fields.
115 * @return size [bytes]
116 **/
117 virtual size_t hdr_size(const std::uint8_t* file) const = 0;
118
119 /**
120 * name of codec for debug purposes. typically set via TEX_CODEC_REGISTER.
121 **/
122 virtual const wchar_t* get_name() const = 0;
123
124 virtual ~ITexCodec() {}
125};
126
128public:
129 virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const;
130 virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
131 virtual Status transform(Tex* t, size_t transforms) const;
132 virtual bool is_hdr(const std::uint8_t* file) const;
133 virtual bool is_ext(const OsPath& extension) const;
134 virtual size_t hdr_size(const std::uint8_t* file) const;
135 virtual const wchar_t* get_name() const {
136 static const wchar_t *name = L"png";
137 return name;
138 };
139};
140
142public:
143 virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const;
144 virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
145 virtual Status transform(Tex* t, size_t transforms) const;
146 virtual bool is_hdr(const std::uint8_t* file) const;
147 virtual bool is_ext(const OsPath& extension) const;
148 virtual size_t hdr_size(const std::uint8_t* file) const;
149 virtual const wchar_t* get_name() const {
150 static const wchar_t *name = L"dds";
151 return name;
152 };
153};
154
156public:
157 virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const;
158 virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
159 virtual Status transform(Tex* t, size_t transforms) const;
160 virtual bool is_hdr(const std::uint8_t* file) const;
161 virtual bool is_ext(const OsPath& extension) const;
162 virtual size_t hdr_size(const std::uint8_t* file) const;
163 virtual const wchar_t* get_name() const {
164 static const wchar_t *name = L"tga";
165 return name;
166 };
167};
168
170public:
171 virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const;
172 virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
173 virtual Status transform(Tex* t, size_t transforms) const;
174 virtual bool is_hdr(const std::uint8_t* file) const;
175 virtual bool is_ext(const OsPath& extension) const;
176 virtual size_t hdr_size(const std::uint8_t* file) const;
177 virtual const wchar_t* get_name() const {
178 static const wchar_t *name = L"bmp";
179 return name;
180 };
181};
182
183/**
184 * Find codec that recognizes the desired output file extension.
185 *
186 * @param extension
187 * @param c (out) vtbl of responsible codec
188 * @return Status; ERR::RES_UNKNOWN_FORMAT (without warning, because this is
189 * called by tex_is_known_extension) if no codec indicates they can
190 * handle the given extension.
191 **/
192extern Status tex_codec_for_filename(const OsPath& extension, const ITexCodec** c);
193
194/**
195 * find codec that recognizes the header's magic field.
196 *
197 * @param data typically contents of file, but need only include the
198 * (first 4 bytes of) header.
199 * @param data_size [bytes]
200 * @param c (out) vtbl of responsible codec
201 * @return Status; ERR::RES_UNKNOWN_FORMAT if no codec indicates they can
202 * handle the given format (header).
203 **/
204extern Status tex_codec_for_header(const std::uint8_t* data, size_t data_size, const ITexCodec** c);
205
206/**
207 * transform the texture's pixel format.
208 * tries each codec's transform method once, or until one indicates success.
209 *
210 * @param t texture object
211 * @param transforms: OR-ed combination of TEX_* flags that are to
212 * be changed.
213 * @return Status
214 **/
215extern Status tex_codec_transform(Tex* t, size_t transforms);
216
217/**
218 * allocate an array of row pointers that point into the given texture data.
219 * for texture decoders that support output via row pointers (e.g. PNG),
220 * this allows flipping the image vertically (useful when matching bottom-up
221 * textures to a global orientation) directly, which is much more
222 * efficient than transforming later via copying all pixels.
223 *
224 * @param data the texture data into which row pointers will point.
225 * note: we don't allocate it here because this function is
226 * needed for encoding, too (where data is already present).
227 * @param h height [pixels] of texture.
228 * @param pitch size [bytes] of one texture row, i.e. width*bytes_per_pixel.
229 * @param src_flags TexFlags of source texture. used to extract its
230 * orientation.
231 * @param dst_orientation desired orientation of the output data.
232 * can be one of TEX_BOTTOM_UP, TEX_TOP_DOWN, or 0 for the
233 * "global orientation".
234 * depending on src and dst, the row array is flipped if necessary.
235 **/
236typedef const std::uint8_t* RowPtr;
237extern std::vector<RowPtr> tex_codec_alloc_rows(const std::uint8_t* data, size_t h, size_t pitch, size_t src_flags, size_t dst_orientation);
238
239/**
240 * apply transforms and then copy header and image into output buffer.
241 *
242 * @param t input texture object
243 * @param transforms transformations to be applied to pixel format
244 * @param hdr header data
245 * @param hdr_size [bytes]
246 * @param da output data array (will be expanded as necessary)
247 * @return Status
248 **/
249extern Status tex_codec_write(Tex* t, size_t transforms, const void* hdr, size_t hdr_size, DynArray* da);
250
251#endif // #ifndef INCLUDED_TEX_CODEC
virtual method table for TexCodecs.
Definition: tex_codec.h:47
virtual ~ITexCodec()
Definition: tex_codec.h:124
virtual bool is_hdr(const std::uint8_t *file) const =0
indicate if the data appears to be an instance of this codec's header, i.e.
virtual size_t hdr_size(const std::uint8_t *file) const =0
return size of the file header supported by this codec.
virtual Status decode(std::uint8_t *data, size_t size, Tex *RESTRICT t) const =0
decode the file into a Tex structure.
virtual const wchar_t * get_name() const =0
name of codec for debug purposes.
virtual Status encode(Tex *RESTRICT t, DynArray *RESTRICT da) const =0
encode the texture data into the codec's file format (in memory).
virtual Status transform(Tex *t, size_t transforms) const =0
transform the texture's pixel format.
virtual bool is_ext(const OsPath &extension) const =0
is the extension that of a file format supported by this codec?
Definition: path.h:84
Definition: tex_codec.h:169
virtual Status encode(Tex *RESTRICT t, DynArray *RESTRICT da) const
encode the texture data into the codec's file format (in memory).
Definition: tex_bmp.cpp:135
virtual bool is_ext(const OsPath &extension) const
is the extension that of a file format supported by this codec?
Definition: tex_bmp.cpp:85
virtual const wchar_t * get_name() const
name of codec for debug purposes.
Definition: tex_codec.h:177
virtual Status decode(std::uint8_t *data, size_t size, Tex *RESTRICT t) const
decode the file into a Tex structure.
Definition: tex_bmp.cpp:106
virtual bool is_hdr(const std::uint8_t *file) const
indicate if the data appears to be an instance of this codec's header, i.e.
Definition: tex_bmp.cpp:77
virtual Status transform(Tex *t, size_t transforms) const
transform the texture's pixel format.
Definition: tex_bmp.cpp:71
virtual size_t hdr_size(const std::uint8_t *file) const
return size of the file header supported by this codec.
Definition: tex_bmp.cpp:91
Definition: tex_codec.h:141
virtual Status transform(Tex *t, size_t transforms) const
transform the texture's pixel format.
Definition: tex_dds.cpp:642
virtual bool is_hdr(const std::uint8_t *file) const
indicate if the data appears to be an instance of this codec's header, i.e.
Definition: tex_dds.cpp:609
virtual size_t hdr_size(const std::uint8_t *file) const
return size of the file header supported by this codec.
Definition: tex_dds.cpp:621
virtual bool is_ext(const OsPath &extension) const
is the extension that of a file format supported by this codec?
Definition: tex_dds.cpp:615
virtual Status encode(Tex *RESTRICT t, DynArray *RESTRICT da) const
encode the texture data into the codec's file format (in memory).
Definition: tex_dds.cpp:635
virtual const wchar_t * get_name() const
name of codec for debug purposes.
Definition: tex_codec.h:149
virtual Status decode(std::uint8_t *data, size_t size, Tex *RESTRICT t) const
decode the file into a Tex structure.
Definition: tex_dds.cpp:627
Definition: tex_codec.h:127
virtual Status transform(Tex *t, size_t transforms) const
transform the texture's pixel format.
Definition: tex_png.cpp:122
virtual bool is_ext(const OsPath &extension) const
is the extension that of a file format supported by this codec?
Definition: tex_png.cpp:261
virtual Status encode(Tex *RESTRICT t, DynArray *RESTRICT da) const
encode the texture data into the codec's file format (in memory).
Definition: tex_png.cpp:315
virtual bool is_hdr(const std::uint8_t *file) const
indicate if the data appears to be an instance of this codec's header, i.e.
Definition: tex_png.cpp:253
virtual const wchar_t * get_name() const
name of codec for debug purposes.
Definition: tex_codec.h:135
virtual Status decode(std::uint8_t *data, size_t size, Tex *RESTRICT t) const
decode the file into a Tex structure.
Definition: tex_png.cpp:282
virtual size_t hdr_size(const std::uint8_t *file) const
return size of the file header supported by this codec.
Definition: tex_png.cpp:267
Definition: tex_codec.h:155
virtual Status transform(Tex *t, size_t transforms) const
transform the texture's pixel format.
Definition: tex_tga.cpp:77
virtual Status encode(Tex *RESTRICT t, DynArray *RESTRICT da) const
encode the texture data into the codec's file format (in memory).
Definition: tex_tga.cpp:154
virtual size_t hdr_size(const std::uint8_t *file) const
return size of the file header supported by this codec.
Definition: tex_tga.cpp:109
virtual bool is_hdr(const std::uint8_t *file) const
indicate if the data appears to be an instance of this codec's header, i.e.
Definition: tex_tga.cpp:83
virtual const wchar_t * get_name() const
name of codec for debug purposes.
Definition: tex_codec.h:163
virtual Status decode(std::uint8_t *data, size_t size, Tex *RESTRICT t) const
decode the file into a Tex structure.
Definition: tex_tga.cpp:122
virtual bool is_ext(const OsPath &extension) const
is the extension that of a file format supported by this codec?
Definition: tex_tga.cpp:103
Stores all data describing an image.
Definition: tex.h:214
#define RESTRICT
Definition: code_annotation.h:267
std::int64_t Status
Error handling system.
Definition: status.h:174
provides a memory range that can be expanded but doesn't waste physical memory or relocate itself.
Definition: dynarray.h:42
Status tex_codec_for_filename(const OsPath &extension, const ITexCodec **c)
Find codec that recognizes the desired output file extension.
Definition: tex_codec.cpp:51
std::vector< RowPtr > tex_codec_alloc_rows(const std::uint8_t *data, size_t h, size_t pitch, size_t src_flags, size_t dst_orientation)
Definition: tex_codec.cpp:121
Status tex_codec_transform(Tex *t, size_t transforms)
transform the texture's pixel format.
Definition: tex_codec.cpp:85
const std::uint8_t * RowPtr
allocate an array of row pointers that point into the given texture data.
Definition: tex_codec.h:236
Status tex_codec_write(Tex *t, size_t transforms, const void *hdr, size_t hdr_size, DynArray *da)
apply transforms and then copy header and image into output buffer.
Definition: tex_codec.cpp:144
Status tex_codec_for_header(const std::uint8_t *data, size_t data_size, const ITexCodec **c)
find codec that recognizes the header's magic field.
Definition: tex_codec.cpp:67