File: tex_codec.h

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (244 lines) | stat: -rw-r--r-- 8,855 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Copyright (C) 2010 Wildfire Games.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * support routines and interface for texture codecs.
 */

#ifndef INCLUDED_TEX_CODEC
#define INCLUDED_TEX_CODEC

#include "tex.h"
#include "tex_internal.h"	// for codec's convenience

/**
 * virtual method table for TexCodecs.
 * rationale: this works in C and also allows storing name and next in vtbl.
 * 'template method'-style interface to increase code reuse and
 * simplify writing new codecs.
 **/
class ITexCodec
{
public:
	/**
	 * decode the file into a Tex structure.
	 *
	 * @param data input data array (non-const, because the texture
	 * may have to be flipped in-place - see "texture orientation").
	 * @param size [bytes] of data, always >= 4
	 *   (this is usually enough to compare the header's "magic" field,
	 *    and no legitimate file will be smaller)
	 * @param t output texture object
	 * @return Status
	 **/
	virtual Status decode(u8* data, size_t size, Tex* RESTRICT t) const = 0;

	/**
	 * encode the texture data into the codec's file format (in memory).
	 *
	 * @param t input texture object. note: non-const because encoding may
	 * require a Tex::transform.
	 * @param da output data array, allocated by codec.
	 * rationale: some codecs cannot calculate the output size beforehand
	 * (e.g. PNG output via libpng), so the output memory cannot be allocated
	 * by the caller.
	 * @return Status
	 **/
	virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const = 0;

	/**
	 * transform the texture's pixel format.
	 *
	 * @param t texture object
	 * @param transforms: OR-ed combination of TEX_* flags that are to
	 * be changed. note: the codec needs only handle situations specific
	 * to its format; generic pixel format transforms are handled by
	 * the caller.
	 **/
	virtual Status transform(Tex* t, size_t transforms) const = 0;

	/**
	 * indicate if the data appears to be an instance of this codec's header,
	 * i.e. can this codec decode it?
	 *
	 * @param file input data; only guaranteed to be 4 bytes!
	 * (this should be enough to examine the header's 'magic' field)
	 * @return bool
	 **/
	virtual bool is_hdr(const u8* file) const = 0;

	/**
	 * is the extension that of a file format supported by this codec?
	 *
	 * rationale: cannot just return the extension string and have
	 * caller compare it (-> smaller code) because a codec's file format
	 * may have several valid extensions (e.g. jpg and jpeg).
	 *
	 * @param extension (including '.')
	 * @return bool
	 **/
	virtual bool is_ext(const OsPath& extension) const = 0;

	/**
	 * return size of the file header supported by this codec.
	 *
	 * @param file the specific header to return length of (taking its
	 * variable-length fields into account). if NULL, return minimum
	 * guaranteed header size, i.e. the header without any
	 * variable-length fields.
	 * @return size [bytes]
	 **/
	virtual size_t hdr_size(const u8* file) const = 0;

	/**
	 * name of codec for debug purposes. typically set via TEX_CODEC_REGISTER.
	 **/
	virtual const wchar_t* get_name() const = 0;

	virtual ~ITexCodec() {}
};

class TexCodecPng:ITexCodec {
public:
	virtual Status decode(u8* data, size_t size, Tex* RESTRICT t) const;
	virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
	virtual Status transform(Tex* t, size_t transforms) const;
	virtual bool is_hdr(const u8* file) const;
	virtual bool is_ext(const OsPath& extension) const;
	virtual size_t hdr_size(const u8* file) const;
	virtual const wchar_t* get_name() const {
		static const wchar_t *name = L"png";
		return name;
	};
};

class TexCodecDds:ITexCodec {
public:
	virtual Status decode(u8* data, size_t size, Tex* RESTRICT t) const;
	virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
	virtual Status transform(Tex* t, size_t transforms) const;
	virtual bool is_hdr(const u8* file) const;
	virtual bool is_ext(const OsPath& extension) const;
	virtual size_t hdr_size(const u8* file) const;
	virtual const wchar_t* get_name() const {
		static const wchar_t *name = L"dds";
		return name;
	};
};

class TexCodecTga:ITexCodec {
public:
	virtual Status decode(u8* data, size_t size, Tex* RESTRICT t) const;
	virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
	virtual Status transform(Tex* t, size_t transforms) const;
	virtual bool is_hdr(const u8* file) const;
	virtual bool is_ext(const OsPath& extension) const;
	virtual size_t hdr_size(const u8* file) const;
	virtual const wchar_t* get_name() const {
		static const wchar_t *name = L"tga";
		return name;
	};
};

class TexCodecBmp:ITexCodec {
public:
	virtual Status decode(u8* data, size_t size, Tex* RESTRICT t) const;
	virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
	virtual Status transform(Tex* t, size_t transforms) const;
	virtual bool is_hdr(const u8* file) const;
	virtual bool is_ext(const OsPath& extension) const;
	virtual size_t hdr_size(const u8* file) const;
	virtual const wchar_t* get_name() const {
		static const wchar_t *name = L"bmp";
		return name;
	};
};

/**
 * Find codec that recognizes the desired output file extension.
 *
 * @param extension
 * @param c (out) vtbl of responsible codec
 * @return Status; ERR::RES_UNKNOWN_FORMAT (without warning, because this is
 * called by tex_is_known_extension) if no codec indicates they can
 * handle the given extension.
 **/
extern Status tex_codec_for_filename(const OsPath& extension, const ITexCodec** c);

/**
 * find codec that recognizes the header's magic field.
 *
 * @param data typically contents of file, but need only include the
 * (first 4 bytes of) header.
 * @param data_size [bytes]
 * @param c (out) vtbl of responsible codec
 * @return Status; ERR::RES_UNKNOWN_FORMAT if no codec indicates they can
 * handle the given format (header).
 **/
extern Status tex_codec_for_header(const u8* data, size_t data_size, const ITexCodec** c);

/**
 * transform the texture's pixel format.
 * tries each codec's transform method once, or until one indicates success.
 *
 * @param t texture object
 * @param transforms: OR-ed combination of TEX_* flags that are to
 * be changed.
 * @return Status
 **/
extern Status tex_codec_transform(Tex* t, size_t transforms);

/**
 * allocate an array of row pointers that point into the given texture data.
 * for texture decoders that support output via row pointers (e.g. PNG),
 * this allows flipping the image vertically (useful when matching bottom-up
 * textures to a global orientation) directly, which is much more
 * efficient than transforming later via copying all pixels.
 *
 * @param data the texture data into which row pointers will point.
 * note: we don't allocate it here because this function is
 * needed for encoding, too (where data is already present).
 * @param h height [pixels] of texture.
 * @param pitch size [bytes] of one texture row, i.e. width*bytes_per_pixel.
 * @param src_flags TexFlags of source texture. used to extract its
 * orientation.
 * @param dst_orientation desired orientation of the output data.
 * can be one of TEX_BOTTOM_UP, TEX_TOP_DOWN, or 0 for the
 * "global orientation".
 * depending on src and dst, the row array is flipped if necessary.
 **/
typedef const u8* RowPtr;
extern std::vector<RowPtr> tex_codec_alloc_rows(const u8* data, size_t h, size_t pitch, size_t src_flags, size_t dst_orientation);

/**
 * apply transforms and then copy header and image into output buffer.
 *
 * @param t input texture object
 * @param transforms transformations to be applied to pixel format
 * @param hdr header data
 * @param hdr_size [bytes]
 * @param da output data array (will be expanded as necessary)
 * @return Status
 **/
extern Status tex_codec_write(Tex* t, size_t transforms, const void* hdr, size_t hdr_size, DynArray* da);

#endif	 // #ifndef INCLUDED_TEX_CODEC