-
Notifications
You must be signed in to change notification settings - Fork 17
/
texgenpack.h
343 lines (301 loc) · 14.2 KB
/
texgenpack.h
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
Copyright (c) 2015 Harm Hanemaaijer <fgenfb@yahoo.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define NU_FILE_TYPES 6
#define FILE_TYPE_PNG 0x101
#define FILE_TYPE_PPM 0x102
#define FILE_TYPE_KTX 0x210
#define FILE_TYPE_PKM 0x201
#define FILE_TYPE_DDS 0x212
#define FILE_TYPE_ASTC 0x203
#define FILE_TYPE_IMAGE_BIT 0x100
#define FILE_TYPE_TEXTURE_BIT 0x200
#define FILE_TYPE_MIPMAPS_BIT 0x010
#define FILE_TYPE_UNDEFINED 0x000
#define FILE_TYPE_IMAGE_UNKNOWN 0x100
// Structures.
typedef struct {
unsigned int *pixels;
int width;
int height;
int extended_width;
int extended_height;
int alpha_bits; // 0 for no alpha, 1 if alpha is limited to 0 and 0xFF, 8 otherwise.
int nu_components; // Indicates the number of components.
int bits_per_component; // 8 or 16.
int is_signed; // 1 if the components are signed, 0 if unsigned.
int srgb; // Whether the image is stored in sRGB format.
int is_half_float; // The image pixels are combinations of half-floats. The pixel size is 64-bit.
} Image;
#define TEXTURE_TYPE_UNCOMPRESSED_RGB8 0x2000
#define TEXTURE_TYPE_UNCOMPRESSED_RGBA8 0x2021
#define TEXTURE_TYPE_UNCOMPRESSED_ARGB8 0x2022
#define TEXTURE_TYPE_UNCOMPRESSED_RGB_HALF_FLOAT 0x6402
#define TEXTURE_TYPE_UNCOMPRESSED_RGBA_HALF_FLOAT 0x6422
#define TEXTURE_TYPE_UNCOMPRESSED_RG16 0x2400
#define TEXTURE_TYPE_UNCOMPRESSED_RG_HALF_FLOAT 0x6800
#define TEXTURE_TYPE_UNCOMPRESSED_R16 0x2401
#define TEXTURE_TYPE_UNCOMPRESSED_R_HALF_FLOAT 0x6400
#define TEXTURE_TYPE_UNCOMPRESSED_RG8 0x2001
#define TEXTURE_TYPE_UNCOMPRESSED_R8 0x2002
#define TEXTURE_TYPE_UNCOMPRESSED_SIGNED_RG16 0x2C00
#define TEXTURE_TYPE_UNCOMPRESSED_SIGNED_R16 0x2C01
#define TEXTURE_TYPE_UNCOMPRESSED_SIGNED_RG8 0x2801
#define TEXTURE_TYPE_UNCOMPRESSED_SIGNED_R8 0x2802
#define TEXTURE_TYPE_ETC1 0x0100
#define TEXTURE_TYPE_ETC2_RGB8 0x0101
#define TEXTURE_TYPE_ETC2_EAC 0x0162
#define TEXTURE_TYPE_ETC2_PUNCHTHROUGH 0x0123
#define TEXTURE_TYPE_R11_EAC 0x0400
#define TEXTURE_TYPE_RG11_EAC 0x0440
#define TEXTURE_TYPE_SIGNED_R11_EAC 0x0C00
#define TEXTURE_TYPE_SIGNED_RG11_EAC 0x0C40
#define TEXTURE_TYPE_ETC2_SRGB8 0x1104
#define TEXTURE_TYPE_ETC2_SRGB_PUNCHTHROUGH 0x1125
#define TEXTURE_TYPE_ETC2_SRGB_EAC 0x1166
#define TEXTURE_TYPE_DXT1 0x0200
#define TEXTURE_TYPE_DXT3 0x0261
#define TEXTURE_TYPE_DXT5 0x0262
#define TEXTURE_TYPE_DXT1A 0x0223
#define TEXTURE_TYPE_BPTC 0x0064
#define TEXTURE_TYPE_BPTC_FLOAT 0x4445
#define TEXTURE_TYPE_BPTC_SIGNED_FLOAT 0x4C46
#define TEXTURE_TYPE_RGTC1 0x0001
#define TEXTURE_TYPE_SIGNED_RGTC1 0x0C01
#define TEXTURE_TYPE_RGTC2 0x0041
#define TEXTURE_TYPE_SIGNED_RGTC2 0x0C41
#define TEXTURE_TYPE_RGBA_ASTC_4X4 0x8000
#define TEXTURE_TYPE_RGBA_ASTC_5X4 0x8001
#define TEXTURE_TYPE_RGBA_ASTC_5X5 0x8002
#define TEXTURE_TYPE_RGBA_ASTC_6X5 0x8003
#define TEXTURE_TYPE_RGBA_ASTC_6X6 0x8004
#define TEXTURE_TYPE_RGBA_ASTC_8X5 0x8005
#define TEXTURE_TYPE_RGBA_ASTC_8X6 0x8006
#define TEXTURE_TYPE_RGBA_ASTC_8X8 0x8007
#define TEXTURE_TYPE_RGBA_ASTC_10X5 0x8008
#define TEXTURE_TYPE_RGBA_ASTC_10X6 0x8009
#define TEXTURE_TYPE_RGBA_ASTC_10X8 0x800A
#define TEXTURE_TYPE_RGBA_ASTC_10X10 0x800B
#define TEXTURE_TYPE_RGBA_ASTC_12X10 0x800C
#define TEXTURE_TYPE_RGBA_ASTC_12X12 0x800D
#define TEXTURE_TYPE_SRGB8_ALPHA8_ASTC_4X4 0x9000
#define TEXTURE_TYPE_ALPHA_BIT 0x0020
#define TEXTURE_TYPE_ETC_BIT 0x0100
#define TEXTURE_TYPE_DXTC_BIT 0x0200
#define TEXTURE_TYPE_128BIT_BIT 0x0040
#define TEXTURE_TYPE_16_BIT_COMPONENTS_BIT 0x0400
#define TEXTURE_TYPE_SIGNED_BIT 0x0800
#define TEXTURE_TYPE_SRGB_BIT 0x1000
#define TEXTURE_TYPE_UNCOMPRESSED_BIT 0x2000
#define TEXTURE_TYPE_HALF_FLOAT_BIT 0x4000
#define TEXTURE_TYPE_ASTC_BIT 0x8000
typedef struct {
int type;
int ktx_support;
int dds_support;
const char *text1;
const char *text2;
int block_width; // The block width (1 for uncompressed textures).
int block_height; // The block height (1 for uncompressed textures).
int bits_per_block; // The number of bits per block (per pixel for uncompressed textures).
int internal_bits_per_block; // The number of bits per block as stored internally (per pixel for uncompressed).
int alpha_bits;
int nu_components;
int gl_internal_format;
int gl_format;
int gl_type;
const char *dx_four_cc;
int dx10_format;
uint64_t red_mask, green_mask, blue_mask, alpha_mask;
} TextureInfo;
typedef struct BlockUserData_t BlockUserData;
typedef int (*TextureDecodingFunction)(const unsigned char *bitstring, unsigned int *image_buffer, int flags);
typedef double (*TextureComparisonFunction)(unsigned int *image_buffer, BlockUserData *user_data);
typedef int (*TextureGetModeFunction)(const unsigned char *bitstring);
typedef void (*TextureSetModeFunction)(unsigned char *bitstring, int flags);
typedef struct {
unsigned int *pixels;
int width;
int height;
int extended_width;
int extended_height;
int type;
int bits_per_block; // The bits per block of the real format. The internally used bits per block is
// given by info->internal_bits_per_block.
int block_width;
int block_height;
TextureDecodingFunction decoding_function;
TextureComparisonFunction comparison_function;
TextureComparisonFunction perceptive_comparison_function;
TextureGetModeFunction get_mode_function;
TextureSetModeFunction set_mode_function;
TextureInfo *info;
} Texture;
struct BlockUserData_t {
unsigned int *image_pixels;
int image_rowstride;
int x_offset;
int y_offset;
int flags;
Texture *texture;
unsigned char *alpha_pixels;
unsigned int *colors;
int stop_signalled;
int pass;
unsigned int *texture_pixels;
unsigned int *texture_pixels_above;
unsigned int *texture_pixels_left;
};
typedef void (*CompressCallbackFunction)(BlockUserData *user_data);
// Command line options defined in texgenpack.c
#define COMMAND_COMPRESS 0
#define COMMAND_DECOMPRESS 1
#define COMMAND_COMPARE 2
#define COMMAND_CALIBRATE 3
#define ORIENTATION_DOWN 1
#define ORIENTATION_UP 2
// Compression levels (0 to 50).
enum {
// Compression level class 0 (levels 0 to 7).
// Compress different blocks concurrently, populations_size = 256,
// nu_generations = 100 + level * 25.
COMPRESSION_LEVEL_CLASS_0 = 0,
// Compression level class 1 (levels 8 to 32)
// Compress the same block concurrently, number of tries is equal to
// level value (8 to 32), nu_generations = 100.
COMPRESSION_LEVEL_CLASS_1 = 8,
// Compression level class 2 (levels 33 to 50)
// Compress the same block concurrently, number of tries is 32,
// nu_generations = 100 + 25 * (level - 32)
COMPRESSION_LEVEL_CLASS_2 = 33,
};
// Ultra preset: Compress different blocks concurrenty, nu_generations = 100.
#define SPEED_ULTRA (COMPRESSION_LEVEL_CLASS_0)
// Fast preset: Eight tries per block, nu_generations = 100.
#define SPEED_FAST (COMPRESSION_LEVEL_CLASS_1)
// Medium preset: 16 tries per block, nu_generations = 100.
#define SPEED_MEDIUM (COMPRESSION_LEVEL_CLASS_1 + 8)
// Slow preset 32 tries per block, nu_generations = 100.
#define SPEED_SLOW (COMPRESSION_LEVEL_CLASS_1 + 24)
extern int command;
extern int option_verbose;
extern int option_max_threads;
extern int option_orientation;
extern int option_compression_level;
extern int option_progress;
extern int option_modal_etc2;
extern int option_allowed_modes_etc2;
extern int option_generations;
extern int option_islands;
extern int option_generations_second_pass;
extern int option_islands_second_pass;
extern int option_texture_format;
extern int option_flip_vertical;
extern int option_quiet;
extern int option_block_width;
extern int option_block_height;
extern int option_half_float;
extern int option_deterministic;
extern int option_hdr;
extern int option_perceptive;
// Defined in image.c
void load_image(const char *filename, int filetype, Image *image);
int load_mipmap_images(const char *filename, int filetype, int max_images, Image *image);
void save_image(Image *image, const char *filename, int filetype);
double compare_images(Image *image1, Image *image2);
int load_texture(const char *filename, int filetype, int max_mipmaps, Texture *texture);
void save_texture(Texture *texture, int nu_mipmaps, const char *filename, int filetype);
void convert_texture_to_image(Texture *texture, Image *image);
void destroy_texture(Texture *texture);
void destroy_image(Image *image);
void clone_image(Image *image1, Image *image2);
void clear_image(Image *image);
void pad_image_borders(Image *image);
void check_1bit_alpha(Image *image);
void convert_image_from_srgb_to_rgb(Image *source_image, Image *dest_image);
void convert_image_from_rgb_to_srgb(Image *source_image, Image *dest_image);
void copy_image_to_uncompressed_texture(Image *image, int texture_type, Texture *texture);
void flip_image_vertical(Image *image);
void print_image_info(Image *image);
void calculate_image_dynamic_range(Image *image, float *range_min_out, float *range_max_out);
void convert_image_from_half_float(Image *image, float range_min, float range_max, float gamma);
void convert_image_to_half_float(Image *image);
void extend_half_float_image_to_rgb(Image *image);
void remove_alpha_from_image(Image *image);
void add_alpha_to_image(Image *image);
void convert_image_from_16_bit_format(Image *image);
void convert_image_to_16_bit_format(Image *image, int nu_components, int signed_format);
void convert_image_from_8_bit_format(Image *image);
void convert_image_to_8_bit_format(Image *image, int nu_components, int signed_format);
// Defined in compress.c
void compress_image(Image *image, int texture_type, CompressCallbackFunction func, Texture *texture,
int genetic_parameters, float mutation_prob, float crossover_prob);
// Defined in mipmap.c
void generate_mipmap_level_from_original(Image *source_image, int level, Image *dest_image);
void generate_mipmap_level_from_previous_level(Image *source_image, Image *dest_image);
int count_mipmap_levels(Image *image);
// Defined in file.c
void load_pkm_file(const char *filename, Texture *texture);
void save_pkm_file(Texture *texture, const char *fikename);
int load_ktx_file(const char *filename, int max_mipmaps, Texture *texture);
void save_ktx_file(Texture *texture, int nu_mipmaps, const char *filename);
int load_dds_file(const char *filename, int max_mipmaps, Texture *texture);
void save_dds_file(Texture *texture, int nu_mipmaps, const char *filename);
void load_astc_file(const char *filename, Texture *texture);
void save_astc_file(Texture *texture, const char *filename);
void load_ppm_file(const char *filename, Image *image);
void load_png_file(const char *filename, Image *image);
void save_png_file(Image *image, const char *filename);
// Defined in texture.c
TextureInfo *match_texture_type(int type);
TextureInfo *match_texture_description(const char *s);
TextureInfo *match_ktx_id(int gl_internal_format, int gl_format, int gl_type);
TextureInfo *match_dds_id(const char *four_cc, int dx10_format, uint32_t pixel_format_flags, int bitcount,
uint32_t red_mask, uint32_t green_mask, uint32_t blue_mask, uint32_t alpha_mask);
const char *texture_type_text(int texture_type);
int get_number_of_texture_formats();
const char *get_texture_format_index_text(int i, int j);
void set_texture_decoding_function(Texture *texture, Image *image);
// Defined in compare.c
extern float *half_float_table;
extern float *gamma_corrected_half_float_table;
extern float *normalized_float_table;
double compare_block_any_size_rgba(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_rgb(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_perceptive_4x4_rgb(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_rgba(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_perceptive_4x4_rgba(unsigned int *image_buffer, BlockUserData *user_data);
void calculate_normalized_float_table();
double compare_block_4x4_rgb8_with_half_float(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_rgba8_with_half_float(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_8_bit_components(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_signed_8_bit_components(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_8_bit_components_with_16_bit(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_signed_8_bit_components_with_16_bit(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_r16(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_rg16(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_r16_signed(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_rg16_signed(unsigned int *image_buffer, BlockUserData *user_data);
void calculate_half_float_table();
double compare_block_4x4_rgb_half_float(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_rgba_half_float(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_r_half_float(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_rg_half_float(unsigned int *image_buffer, BlockUserData *user_data);
void calculate_gamma_corrected_half_float_table();
double compare_block_4x4_rgb_half_float_hdr(unsigned int *image_buffer, BlockUserData *user_data);
double compare_block_4x4_rgba_half_float_hdr(unsigned int *image_buffer, BlockUserData *user_data);
// Defined in half_float.c
int halfp2singles(void *target, void *source, int numel);
int singles2halfp(void *target, void *source, int numel);
// Defined in calibrate.c
void calibrate_genetic_parameters(Image *image, int texture_type);