Debian Codegraph @

libs/crunch/crnlib.h

raw symbols SCIP
1 // File: crnlib.h - Advanced DXTn texture compression library.
2 // Copyright (c) 2010-2016 Richard Geldreich, Jr. and Binomial LLC
3 // See copyright notice and license at the end of this file.
4 //
5 // This header file contains the public crnlib declarations for DXTn,
6 // clustered DXTn, and CRN compression/decompression.
7 //
8 // Note: This library does NOT need to be linked into your game executable if
9 // all you want to do is transcode .CRN files to raw DXTn bits at run-time.
10 // The crn_decomp.h header file library contains all the code necessary for
11 // decompression.
12 //
13 // Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
14 #ifndef CRNLIB_H
15 #define CRNLIB_H
16
17 #ifdef _MSC_VER
18 #pragma warning(disable : 4127) // conditional expression is constant
19 #endif
20
21 #define CRNLIB_VERSION 104
22
23 #define CRNLIB_SUPPORT_ATI_COMPRESS 0
24 #define CRNLIB_SUPPORT_SQUISH 0
25
26 typedef unsigned char crn_uint8;
27 typedef unsigned short crn_uint16;
28 typedef unsigned int crn_uint32;
29 typedef signed char crn_int8;
30 typedef signed short crn_int16;
31 typedef signed int crn_int32;
32 typedef unsigned int crn_bool;
33
34 // crnlib can compress to these file types.
35 enum crn_file_type {
36 // .CRN
37 cCRNFileTypeCRN = 0,
38
39 // .DDS using regular DXT or clustered DXT
40 cCRNFileTypeDDS,
41
42 cCRNFileTypeForceDWORD = 0xFFFFFFFF
43 };
44
45 // Supported compressed pixel formats.
46 // Basically all the standard DX9 formats, with some swizzled DXT5 formats
47 // (most of them supported by ATI's Compressonator), along with some ATI/X360 GPU specific formats.
48 enum crn_format {
49 cCRNFmtInvalid = -1,
50
51 cCRNFmtDXT1 = 0,
52
53 cCRNFmtFirstValid = cCRNFmtDXT1,
54
55 // cCRNFmtDXT3 is not currently supported when writing to CRN - only DDS.
56 cCRNFmtDXT3,
57
58 cCRNFmtDXT5,
59
60 // Various DXT5 derivatives
61 cCRNFmtDXT5_CCxY, // Luma-chroma
62 cCRNFmtDXT5_xGxR, // Swizzled 2-component
63 cCRNFmtDXT5_xGBR, // Swizzled 3-component
64 cCRNFmtDXT5_AGBR, // Swizzled 4-component
65
66 // ATI 3DC and X360 DXN
67 cCRNFmtDXN_XY,
68 cCRNFmtDXN_YX,
69
70 // DXT5 alpha blocks only
71 cCRNFmtDXT5A,
72
73 cCRNFmtETC1,
74 cCRNFmtETC2,
75 cCRNFmtETC2A,
76 cCRNFmtETC1S,
77 cCRNFmtETC2AS,
78
79 cCRNFmtTotal,
80
81 cCRNFmtForceDWORD = 0xFFFFFFFF
82 };
83
84 // Various library/file format limits.
85 enum crn_limits {
86 // Max. mipmap level resolution on any axis.
87 cCRNMaxLevelResolution = 4096,
88
89 cCRNMinPaletteSize = 8,
90 cCRNMaxPaletteSize = 8192,
91
92 cCRNMaxFaces = 6,
93 cCRNMaxLevels = 16,
94
95 cCRNMaxHelperThreads = 15,
96
97 cCRNMinQualityLevel = 0,
98 cCRNMaxQualityLevel = 255
99 };
100
101 // CRN/DDS compression flags.
102 // See the m_flags member in the crn_comp_params struct, below.
103 enum crn_comp_flags {
104 // Enables perceptual colorspace distance metrics if set.
105 // Important: Be sure to disable this when compressing non-sRGB colorspace images, like normal maps!
106 // Default: Set
107 cCRNCompFlagPerceptual = 1,
108
109 // Enables (up to) 8x8 macroblock usage if set. If disabled, only 4x4 blocks are allowed.
110 // Compression ratio will be lower when disabled, but may cut down on blocky artifacts because the process used to determine
111 // where large macroblocks can be used without artifacts isn't perfect.
112 // Default: Set.
113 cCRNCompFlagHierarchical = 2,
114
115 // cCRNCompFlagQuick disables several output file optimizations - intended for things like quicker previews.
116 // Default: Not set.
117 cCRNCompFlagQuick = 4,
118
119 // DXT1: OK to use DXT1 alpha blocks for better quality or DXT1A transparency.
120 // DXT5: OK to use both DXT5 block types.
121 // Currently only used when writing to .DDS files, as .CRN uses only a subset of the possible DXTn block types.
122 // Default: Set.
123 cCRNCompFlagUseBothBlockTypes = 8,
124
125 // OK to use DXT1A transparent indices to encode black (assumes pixel shader ignores fetched alpha).
126 // Currently only used when writing to .DDS files, .CRN never uses alpha blocks.
127 // Default: Not set.
128 cCRNCompFlagUseTransparentIndicesForBlack = 16,
129
130 // Disables endpoint caching, for more deterministic output.
131 // Currently only used when writing to .DDS files.
132 // Default: Not set.
133 cCRNCompFlagDisableEndpointCaching = 32,
134
135 // If enabled, use the cCRNColorEndpointPaletteSize, etc. params to control the CRN palette sizes. Only useful when writing to .CRN files.
136 // Default: Not set.
137 cCRNCompFlagManualPaletteSizes = 64,
138
139 // If enabled, DXT1A alpha blocks are used to encode single bit transparency.
140 // Default: Not set.
141 cCRNCompFlagDXT1AForTransparency = 128,
142
143 // If enabled, the DXT1 compressor's color distance metric assumes the pixel shader will be converting the fetched RGB results to luma (Y part of YCbCr).
144 // This increases quality when compressing grayscale images, because the compressor can spread the luma error amoung all three channels (i.e. it can generate blocks
145 // with some chroma present if doing so will ultimately lead to lower luma error).
146 // Only enable on grayscale source images.
147 // Default: Not set.
148 cCRNCompFlagGrayscaleSampling = 256,
149
150 // If enabled, debug information will be output during compression.
151 // Default: Not set.
152 cCRNCompFlagDebugging = 0x80000000,
153
154 cCRNCompFlagForceDWORD = 0xFFFFFFFF
155 };
156
157 // Controls DXTn quality vs. speed control - only used when compressing to .DDS.
158 enum crn_dxt_quality {
159 cCRNDXTQualitySuperFast,
160 cCRNDXTQualityFast,
161 cCRNDXTQualityNormal,
162 cCRNDXTQualityBetter,
163 cCRNDXTQualityUber,
164
165 cCRNDXTQualityTotal,
166
167 cCRNDXTQualityForceDWORD = 0xFFFFFFFF
168 };
169
170 // Which DXTn compressor to use when compressing to plain (non-clustered) .DDS.
171 enum crn_dxt_compressor_type {
172 cCRNDXTCompressorCRN, // Use crnlib's ETC1 or DXTc block compressor (default, highest quality, comparable or better than ati_compress or squish, and crnlib's ETC1 is a lot fasterw with similiar quality to Erricson's)
173 cCRNDXTCompressorCRNF, // Use crnlib's "fast" DXTc block compressor
174 cCRNDXTCompressorRYG, // Use RYG's DXTc block compressor (low quality, but very fast)
175
176 #if CRNLIB_SUPPORT_ATI_COMPRESS
177 cCRNDXTCompressorATI,
178 #endif
179
180 #if CRNLIB_SUPPORT_SQUISH
181 cCRNDXTCompressorSquish,
182 #endif
183
184 cCRNTotalDXTCompressors,
185
186 cCRNDXTCompressorForceDWORD = 0xFFFFFFFF
187 };
188
189 // Progress callback function.
190 // Processing will stop prematurely (and fail) if the callback returns false.
191 // phase_index, total_phases - high level progress
192 // subphase_index, total_subphases - progress within current phase
193 typedef crn_bool (*crn_progress_callback_func)(crn_uint32 phase_index, crn_uint32 total_phases, crn_uint32 subphase_index, crn_uint32 total_subphases, void* pUser_data_ptr);
194
195 // CRN/DDS compression parameters struct.
196 struct crn_comp_params {
197 inline crn_comp_params() { clear(); }
198
199 // Clear struct to default parameters.
200 inline void clear() {
201 m_size_of_obj = sizeof(*this);
202 m_file_type = cCRNFileTypeCRN;
203 m_faces = 1;
204 m_width = 0;
205 m_height = 0;
206 m_levels = 1;
207 m_format = cCRNFmtDXT1;
208 m_flags = cCRNCompFlagPerceptual | cCRNCompFlagHierarchical | cCRNCompFlagUseBothBlockTypes;
209
210 for (crn_uint32 f = 0; f < cCRNMaxFaces; f++)
211 for (crn_uint32 l = 0; l < cCRNMaxLevels; l++)
212 m_pImages[f][l] = NULL;
213
214 m_target_bitrate = 0.0f;
215 m_quality_level = cCRNMaxQualityLevel;
216 m_dxt1a_alpha_threshold = 128;
217 m_dxt_quality = cCRNDXTQualityUber;
218 m_dxt_compressor_type = cCRNDXTCompressorCRN;
219 m_alpha_component = 3;
220
221 m_crn_adaptive_tile_color_psnr_derating = 2.0f;
222 m_crn_adaptive_tile_alpha_psnr_derating = 2.0f;
223 m_crn_color_endpoint_palette_size = 0;
224 m_crn_color_selector_palette_size = 0;
225 m_crn_alpha_endpoint_palette_size = 0;
226 m_crn_alpha_selector_palette_size = 0;
227
228 m_num_helper_threads = 0;
229 m_userdata0 = 1;
230 m_userdata1 = 0;
231 m_pProgress_func = NULL;
232 m_pProgress_func_data = NULL;
233 }
234
235 inline bool operator==(const crn_comp_params& rhs) const {
236 #define CRNLIB_COMP(x) \
237 do { \
238 if ((x) != (rhs.x)) \
239 return false; \
240 } while (0)
241 CRNLIB_COMP(m_size_of_obj);
242 CRNLIB_COMP(m_file_type);
243 CRNLIB_COMP(m_faces);
244 CRNLIB_COMP(m_width);
245 CRNLIB_COMP(m_height);
246 CRNLIB_COMP(m_levels);
247 CRNLIB_COMP(m_format);
248 CRNLIB_COMP(m_flags);
249 CRNLIB_COMP(m_target_bitrate);
250 CRNLIB_COMP(m_quality_level);
251 CRNLIB_COMP(m_dxt1a_alpha_threshold);
252 CRNLIB_COMP(m_dxt_quality);
253 CRNLIB_COMP(m_dxt_compressor_type);
254 CRNLIB_COMP(m_alpha_component);
255 CRNLIB_COMP(m_crn_adaptive_tile_color_psnr_derating);
256 CRNLIB_COMP(m_crn_adaptive_tile_alpha_psnr_derating);
257 CRNLIB_COMP(m_crn_color_endpoint_palette_size);
258 CRNLIB_COMP(m_crn_color_selector_palette_size);
259 CRNLIB_COMP(m_crn_alpha_endpoint_palette_size);
260 CRNLIB_COMP(m_crn_alpha_selector_palette_size);
261 CRNLIB_COMP(m_num_helper_threads);
262 CRNLIB_COMP(m_userdata0);
263 CRNLIB_COMP(m_userdata1);
264 CRNLIB_COMP(m_pProgress_func);
265 CRNLIB_COMP(m_pProgress_func_data);
266
267 for (crn_uint32 f = 0; f < cCRNMaxFaces; f++)
268 for (crn_uint32 l = 0; l < cCRNMaxLevels; l++)
269 CRNLIB_COMP(m_pImages[f][l]);
270
271 #undef CRNLIB_COMP
272 return true;
273 }
274
275 // Returns true if the input parameters are reasonable.
276 inline bool check() const {
277 if ((m_file_type > cCRNFileTypeDDS) ||
278 (((int)m_quality_level < (int)cCRNMinQualityLevel) || ((int)m_quality_level > (int)cCRNMaxQualityLevel)) ||
279 (m_dxt1a_alpha_threshold > 255) ||
280 ((m_faces != 1) && (m_faces != 6)) ||
281 ((m_width < 1) || (m_width > cCRNMaxLevelResolution)) ||
282 ((m_height < 1) || (m_height > cCRNMaxLevelResolution)) ||
283 ((m_levels < 1) || (m_levels > cCRNMaxLevels)) ||
284 ((m_format < cCRNFmtDXT1) || (m_format >= cCRNFmtTotal)) ||
285 ((m_crn_color_endpoint_palette_size) && ((m_crn_color_endpoint_palette_size < cCRNMinPaletteSize) || (m_crn_color_endpoint_palette_size > cCRNMaxPaletteSize))) ||
286 ((m_crn_color_selector_palette_size) && ((m_crn_color_selector_palette_size < cCRNMinPaletteSize) || (m_crn_color_selector_palette_size > cCRNMaxPaletteSize))) ||
287 ((m_crn_alpha_endpoint_palette_size) && ((m_crn_alpha_endpoint_palette_size < cCRNMinPaletteSize) || (m_crn_alpha_endpoint_palette_size > cCRNMaxPaletteSize))) ||
288 ((m_crn_alpha_selector_palette_size) && ((m_crn_alpha_selector_palette_size < cCRNMinPaletteSize) || (m_crn_alpha_selector_palette_size > cCRNMaxPaletteSize))) ||
289 (m_alpha_component > 3) ||
290 (m_num_helper_threads > cCRNMaxHelperThreads) ||
291 (m_dxt_quality > cCRNDXTQualityUber) ||
292 (m_dxt_compressor_type >= cCRNTotalDXTCompressors)) {
293 return false;
294 }
295 return true;
296 }
297
298 // Helper to set/get flags from m_flags member.
299 inline bool get_flag(crn_comp_flags flag) const { return (m_flags & flag) != 0; }
300 inline void set_flag(crn_comp_flags flag, bool val) {
301 m_flags &= ~flag;
302 if (val)
303 m_flags |= flag;
304 }
305
306 crn_uint32 m_size_of_obj;
307
308 crn_file_type m_file_type; // Output file type: cCRNFileTypeCRN or cCRNFileTypeDDS.
309
310 crn_uint32 m_faces; // 1 (2D map) or 6 (cubemap)
311 crn_uint32 m_width; // [1,cCRNMaxLevelResolution], non-power of 2 OK, non-square OK
312 crn_uint32 m_height; // [1,cCRNMaxLevelResolution], non-power of 2 OK, non-square OK
313 crn_uint32 m_levels; // [1,cCRNMaxLevelResolution], non-power of 2 OK, non-square OK
314
315 crn_format m_format; // Output pixel format.
316
317 crn_uint32 m_flags; // see crn_comp_flags enum
318
319 // Array of pointers to 32bpp input images.
320 const crn_uint32* m_pImages[cCRNMaxFaces][cCRNMaxLevels];
321
322 // Target bitrate - if non-zero, the compressor will use an interpolative search to find the
323 // highest quality level that is <= the target bitrate. If it fails to find a bitrate high enough, it'll
324 // try disabling adaptive block sizes (cCRNCompFlagHierarchical flag) and redo the search. This process can be pretty slow.
325 float m_target_bitrate;
326
327 // Desired quality level.
328 // Currently, CRN and DDS quality levels are not compatible with eachother from an image quality standpoint.
329 crn_uint32 m_quality_level; // [cCRNMinQualityLevel, cCRNMaxQualityLevel]
330
331 // DXTn compression parameters.
332 crn_uint32 m_dxt1a_alpha_threshold;
333 crn_dxt_quality m_dxt_quality;
334 crn_dxt_compressor_type m_dxt_compressor_type;
335
336 // Alpha channel's component. Defaults to 3.
337 crn_uint32 m_alpha_component;
338
339 // Various low-level CRN specific parameters.
340 float m_crn_adaptive_tile_color_psnr_derating;
341 float m_crn_adaptive_tile_alpha_psnr_derating;
342
343 crn_uint32 m_crn_color_endpoint_palette_size; // [cCRNMinPaletteSize,cCRNMaxPaletteSize]
344 crn_uint32 m_crn_color_selector_palette_size; // [cCRNMinPaletteSize,cCRNMaxPaletteSize]
345
346 crn_uint32 m_crn_alpha_endpoint_palette_size; // [cCRNMinPaletteSize,cCRNMaxPaletteSize]
347 crn_uint32 m_crn_alpha_selector_palette_size; // [cCRNMinPaletteSize,cCRNMaxPaletteSize]
348
349 // Number of helper threads to create during compression. 0=no threading.
350 crn_uint32 m_num_helper_threads;
351
352 // CRN userdata0 and userdata1 members, which are written directly to the header of the output file.
353 crn_uint32 m_userdata0;
354 crn_uint32 m_userdata1;
355
356 // User provided progress callback.
357 crn_progress_callback_func m_pProgress_func;
358 void* m_pProgress_func_data;
359 };
360
361 // Mipmap generator's mode.
362 enum crn_mip_mode {
363 cCRNMipModeUseSourceOrGenerateMips, // Use source texture's mipmaps if it has any, otherwise generate new mipmaps
364 cCRNMipModeUseSourceMips, // Use source texture's mipmaps if it has any, otherwise the output has no mipmaps
365 cCRNMipModeGenerateMips, // Always generate new mipmaps
366 cCRNMipModeNoMips, // Output texture has no mipmaps
367
368 cCRNMipModeTotal,
369
370 cCRNModeForceDWORD = 0xFFFFFFFF
371 };
372
373 const char* crn_get_mip_mode_desc(crn_mip_mode m);
374 const char* crn_get_mip_mode_name(crn_mip_mode m);
375
376 // Mipmap generator's filter kernel.
377 enum crn_mip_filter {
378 cCRNMipFilterBox,
379 cCRNMipFilterTent,
380 cCRNMipFilterLanczos4,
381 cCRNMipFilterMitchell,
382 cCRNMipFilterKaiser, // Kaiser=default mipmap filter
383
384 cCRNMipFilterTotal,
385
386 cCRNMipFilterForceDWORD = 0xFFFFFFFF
387 };
388
389 const char* crn_get_mip_filter_name(crn_mip_filter f);
390
391 // Mipmap generator's scale mode.
392 enum crn_scale_mode {
393 cCRNSMDisabled,
394 cCRNSMAbsolute,
395 cCRNSMRelative,
396 cCRNSMLowerPow2,
397 cCRNSMNearestPow2,
398 cCRNSMNextPow2,
399
400 cCRNSMTotal,
401
402 cCRNSMForceDWORD = 0xFFFFFFFF
403 };
404
405 const char* crn_get_scale_mode_desc(crn_scale_mode sm);
406
407 // Mipmap generator parameters.
408 struct crn_mipmap_params {
409 inline crn_mipmap_params() { clear(); }
410
411 inline void clear() {
412 m_size_of_obj = sizeof(*this);
413 m_mode = cCRNMipModeUseSourceOrGenerateMips;
414 m_filter = cCRNMipFilterKaiser;
415 m_gamma_filtering = true;
416 m_gamma = 2.2f;
417 // Default "blurriness" factor of .9 actually sharpens the output a little.
418 m_blurriness = .9f;
419 m_renormalize = false;
420 m_rtopmip = false;
421 m_tiled = false;
422 m_max_levels = cCRNMaxLevels;
423 m_min_mip_size = 1;
424
425 m_scale_mode = cCRNSMDisabled;
426 m_scale_x = 1.0f;
427 m_scale_y = 1.0f;
428
429 m_window_left = 0;
430 m_window_top = 0;
431 m_window_right = 0;
432 m_window_bottom = 0;
433
434 m_clamp_scale = false;
435 m_clamp_width = 0;
436 m_clamp_height = 0;
437 }
438
439 inline bool check() const { return true; }
440
441 inline bool operator==(const crn_mipmap_params& rhs) const {
442 #define CRNLIB_COMP(x) \
443 do { \
444 if ((x) != (rhs.x)) \
445 return false; \
446 } while (0)
447 CRNLIB_COMP(m_size_of_obj);
448 CRNLIB_COMP(m_mode);
449 CRNLIB_COMP(m_filter);
450 CRNLIB_COMP(m_gamma_filtering);
451 CRNLIB_COMP(m_gamma);
452 CRNLIB_COMP(m_blurriness);
453 CRNLIB_COMP(m_renormalize);
454 CRNLIB_COMP(m_rtopmip);
455 CRNLIB_COMP(m_tiled);
456 CRNLIB_COMP(m_max_levels);
457 CRNLIB_COMP(m_min_mip_size);
458 CRNLIB_COMP(m_scale_mode);
459 CRNLIB_COMP(m_scale_x);
460 CRNLIB_COMP(m_scale_y);
461 CRNLIB_COMP(m_window_left);
462 CRNLIB_COMP(m_window_top);
463 CRNLIB_COMP(m_window_right);
464 CRNLIB_COMP(m_window_bottom);
465 CRNLIB_COMP(m_clamp_scale);
466 CRNLIB_COMP(m_clamp_width);
467 CRNLIB_COMP(m_clamp_height);
468 return true;
469 #undef CRNLIB_COMP
470 }
471 crn_uint32 m_size_of_obj;
472
473 crn_mip_mode m_mode;
474 crn_mip_filter m_filter;
475
476 crn_bool m_gamma_filtering;
477 float m_gamma;
478
479 float m_blurriness;
480
481 crn_uint32 m_max_levels;
482 crn_uint32 m_min_mip_size;
483
484 crn_bool m_renormalize;
485 crn_bool m_rtopmip;
486 crn_bool m_tiled;
487
488 crn_scale_mode m_scale_mode;
489 float m_scale_x;
490 float m_scale_y;
491
492 crn_uint32 m_window_left;
493 crn_uint32 m_window_top;
494 crn_uint32 m_window_right;
495 crn_uint32 m_window_bottom;
496
497 crn_bool m_clamp_scale;
498 crn_uint32 m_clamp_width;
499 crn_uint32 m_clamp_height;
500 };
501
502 // -------- High-level helper function definitions for CDN/DDS compression.
503
504 #ifndef CRNLIB_MIN_ALLOC_ALIGNMENT
505 #define CRNLIB_MIN_ALLOC_ALIGNMENT sizeof(size_t) * 2
506 #endif
507
508 // Function to set an optional user provided memory allocation/reallocation/msize routines.
509 // By default, crnlib just uses malloc(), free(), etc. for all allocations.
510 typedef void* (*crn_realloc_func)(void* p, size_t size, size_t* pActual_size, bool movable, void* pUser_data);
511 typedef size_t (*crn_msize_func)(void* p, void* pUser_data);
512 void crn_set_memory_callbacks(crn_realloc_func pRealloc, crn_msize_func pMSize, void* pUser_data);
513
514 // Frees memory blocks allocated by crn_compress(), crn_decompress_crn_to_dds(), or crn_decompress_dds_to_images().
515 void crn_free_block(void* pBlock);
516
517 // Compresses a 32-bit/pixel texture to either: a regular DX9 DDS file, a "clustered" (or reduced entropy) DX9 DDS file, or a CRN file in memory.
518 // Input parameters:
519 // comp_params is the compression parameters struct, defined above.
520 // compressed_size will be set to the size of the returned memory block containing the output file.
521 // The returned block must be freed by calling crn_free_block().
522 // *pActual_quality_level will be set to the actual quality level used to compress the image. May be NULL.
523 // *pActual_bitrate will be set to the output file's effective bitrate, possibly taking into account LZMA compression. May be NULL.
524 // Return value:
525 // The compressed file data, or NULL on failure.
526 // compressed_size will be set to the size of the returned memory buffer.
527 // Notes:
528 // A "regular" DDS file is compressed using normal DXTn compression at the specified DXT quality level.
529 // A "clustered" DDS file is compressed using clustered DXTn compression to either the target bitrate or the specified integer quality factor.
530 // The output file is a standard DX9 format DDS file, except the compressor assumes you will be later losslessly compressing the DDS output file using the LZMA algorithm.
531 // A texture is defined as an array of 1 or 6 "faces" (6 faces=cubemap), where each "face" consists of between [1,cCRNMaxLevels] mipmap levels.
532 // Mipmap levels are simple 32-bit 2D images with a pitch of width*sizeof(uint32), arranged in the usual raster order (top scanline first).
533 // The image pixels may be grayscale (YYYX bytes in memory), grayscale/alpha (YYYA in memory), 24-bit (RGBX in memory), or 32-bit (RGBA) colors (where "X"=don't care).
534 // RGB color data is generally assumed to be in the sRGB colorspace. If not, be sure to clear the "cCRNCompFlagPerceptual" in the crn_comp_params struct!
535 void* crn_compress(const crn_comp_params& comp_params, crn_uint32& compressed_size, crn_uint32* pActual_quality_level = NULL, float* pActual_bitrate = NULL);
536
537 // Like the above function, except this function can also do things like generate mipmaps, and resize or crop the input texture before compression.
538 // The actual operations performed are controlled by the crn_mipmap_params struct members.
539 // Be sure to set the "m_gamma_filtering" member of crn_mipmap_params to false if the input texture is not sRGB.
540 void* crn_compress(const crn_comp_params& comp_params, const crn_mipmap_params& mip_params, crn_uint32& compressed_size, crn_uint32* pActual_quality_level = NULL, float* pActual_bitrate = NULL);
541
542 // Transcodes an entire CRN file to DDS using the crn_decomp.h header file library to do most of the heavy lifting.
543 // The output DDS file's format is guaranteed to be one of the DXTn formats in the crn_format enum.
544 // This is a fast operation, because the CRN format is explicitly designed to be efficiently transcodable to DXTn.
545 // For more control over decompression, see the lower-level helper functions in crn_decomp.h, which do not depend at all on crnlib.
546 void* crn_decompress_crn_to_dds(const void* pCRN_file_data, crn_uint32& file_size);
547
548 // Decompresses an entire DDS file in any supported format to uncompressed 32-bit/pixel image(s).
549 // See the crnlib::pixel_format enum in inc/dds_defs.h for a list of the supported DDS formats.
550 // You are responsible for freeing each image block, either by calling crn_free_all_images() or manually calling crn_free_block() on each image pointer.
551 struct crn_texture_desc {
552 crn_uint32 m_faces;
553 crn_uint32 m_width;
554 crn_uint32 m_height;
555 crn_uint32 m_levels;
556 crn_uint32 m_fmt_fourcc; // Same as crnlib::pixel_format
557 };
558 bool crn_decompress_dds_to_images(const void* pDDS_file_data, crn_uint32 dds_file_size, crn_uint32** ppImages, crn_texture_desc& tex_desc);
559
560 // Frees all images allocated by crn_decompress_dds_to_images().
561 void crn_free_all_images(crn_uint32** ppImages, const crn_texture_desc& desc);
562
563 // -------- crn_format related helpers functions.
564
565 // Returns the FOURCC format equivalent to the specified crn_format.
566 crn_uint32 crn_get_format_fourcc(crn_format fmt);
567
568 // Returns the crn_format's bits per texel.
569 crn_uint32 crn_get_format_bits_per_texel(crn_format fmt);
570
571 // Returns the crn_format's number of bytes per block.
572 crn_uint32 crn_get_bytes_per_dxt_block(crn_format fmt);
573
574 // Returns the non-swizzled, basic DXTn version of the specified crn_format.
575 // This is the format you would supply D3D or OpenGL.
576 crn_format crn_get_fundamental_dxt_format(crn_format fmt);
577
578 // -------- String helpers.
579
580 // Converts a crn_file_type to a string.
581 const char* crn_get_file_type_ext(crn_file_type file_type);
582
583 // Converts a crn_format to a string.
584 const char* crn_get_format_string(crn_format fmt);
585
586 // Converts a crn_dxt_quality to a string.
587 const char* crn_get_dxt_quality_string(crn_dxt_quality q);
588
589 // -------- Low-level DXTn 4x4 block compressor API
590
591 // crnlib's DXTn endpoint optimizer actually supports any number of source pixels (i.e. from 1 to thousands, not just 16),
592 // but for simplicity this API only supports 4x4 texel blocks.
593 typedef void* crn_block_compressor_context_t;
594
595 // Create a DXTn block compressor.
596 // This function only supports the basic/nonswizzled "fundamental" formats: DXT1, DXT3, DXT5, DXT5A, DXN_XY and DXN_YX.
597 // Avoid calling this multiple times if you intend on compressing many blocks, because it allocates some memory.
598 crn_block_compressor_context_t crn_create_block_compressor(const crn_comp_params& params);
599
600 // Compresses a block of 16 pixels to the destination DXTn block.
601 // pDst_block should be 8 (for DXT1/DXT5A) or 16 bytes (all the others).
602 // pPixels should be an array of 16 crn_uint32's. Each crn_uint32 must be r,g,b,a (r is always first) in memory.
603 void crn_compress_block(crn_block_compressor_context_t pContext, const crn_uint32* pPixels, void* pDst_block);
604
605 // Frees a DXTn block compressor.
606 void crn_free_block_compressor(crn_block_compressor_context_t pContext);
607
608 // Unpacks a compressed block to pDst_pixels.
609 // pSrc_block should be 8 (for DXT1/DXT5A) or 16 bytes (all the others).
610 // pDst_pixel should be an array of 16 crn_uint32's. Each uint32 will be r,g,b,a (r is always first) in memory.
611 // crn_fmt should be one of the "fundamental" formats: DXT1, DXT3, DXT5, DXT5A, DXN_XY and DXN_YX.
612 // The various swizzled DXT5 formats (such as cCRNFmtDXT5_xGBR, etc.) will be unpacked as if they where plain DXT5.
613 // Returns false if the crn_fmt is invalid.
614 bool crn_decompress_block(const void* pSrc_block, crn_uint32* pDst_pixels, crn_format crn_fmt);
615
616 #endif // CRNLIB_H
617
618 //------------------------------------------------------------------------------
619 //
620 // crnlib uses the ZLIB license:
621 // http://opensource.org/licenses/Zlib
622 //
623 // Copyright (c) 2010-2016 Richard Geldreich, Jr. and Binomial LLC
624 //
625 // This software is provided 'as-is', without any express or implied
626 // warranty. In no event will the authors be held liable for any damages
627 // arising from the use of this software.
628 //
629 // Permission is granted to anyone to use this software for any purpose,
630 // including commercial applications, and to alter it and redistribute it
631 // freely, subject to the following restrictions:
632 //
633 // 1. The origin of this software must not be misrepresented; you must not
634 // claim that you wrote the original software. If you use this software
635 // in a product, an acknowledgment in the product documentation would be
636 // appreciated but is not required.
637 //
638 // 2. Altered source versions must be plainly marked as such, and must not be
639 // misrepresented as being the original software.
640 //
641 // 3. This notice may not be removed or altered from any source distribution.
642 //
643 //------------------------------------------------------------------------------