This commit is contained in:
+4849
File diff suppressed because it is too large
Load Diff
+552
@@ -0,0 +1,552 @@
|
||||
// File: crnlib.h - Advanced DXTn texture compression library.
|
||||
// Copyright (c) 2010-2011 Tenacious Software LLC
|
||||
// See copyright notice and license at the end of this file.
|
||||
//
|
||||
// This header file contains the public crnlib declarations for DXTn and
|
||||
// clustered DXTn compression/decompression.
|
||||
//
|
||||
// Note: This library does NOT need to be linked into your game executable if
|
||||
// all you want to do is transcode .CRN files to raw DXTn bits at run-time.
|
||||
// The crn_decomp.h header file library contains all the code necessary for
|
||||
// decompression.
|
||||
//
|
||||
#ifndef CRNLIB_H
|
||||
#define CRNLIB_H
|
||||
|
||||
#define CRNLIB_VERSION 100
|
||||
|
||||
typedef unsigned char crn_uint8;
|
||||
typedef unsigned short crn_uint16;
|
||||
typedef unsigned int crn_uint32;
|
||||
typedef signed char crn_int8;
|
||||
typedef signed short crn_int16;
|
||||
typedef signed int crn_int32;
|
||||
typedef unsigned int crn_bool;
|
||||
|
||||
// crnlib can compress to these file types.
|
||||
enum crn_file_type
|
||||
{
|
||||
// .CRN
|
||||
cCRNFileTypeCRN = 0,
|
||||
|
||||
// .DDS using regular DXT or clustered DXT
|
||||
cCRNFileTypeDDS,
|
||||
|
||||
cCRNFileTypeForceDWORD = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
// Supported compressed pixel formats.
|
||||
// Basically all the standard DX9 formats, with some swizzled DXT5 formats
|
||||
// (most of them supported by ATI's Compressonator), along with some ATI/X360 GPU specific formats.
|
||||
enum crn_format
|
||||
{
|
||||
cCRNFmtInvalid = -1,
|
||||
|
||||
cCRNFmtDXT1 = 0,
|
||||
|
||||
cCRNFmtFirstValid = cCRNFmtDXT1,
|
||||
|
||||
// cCRNFmtDXT3 is not currently supported when writing to CRN - only DDS.
|
||||
cCRNFmtDXT3,
|
||||
|
||||
cCRNFmtDXT5,
|
||||
|
||||
// Various DXT5 derivatives
|
||||
cCRNFmtDXT5_CCxY, // Luma-chroma
|
||||
cCRNFmtDXT5_xGxR, // Swizzled 2-component
|
||||
cCRNFmtDXT5_xGBR, // Swizzled 3-component
|
||||
cCRNFmtDXT5_AGBR, // Swizzled 4-component
|
||||
|
||||
// ATI 3DC and X360 DXN
|
||||
cCRNFmtDXN_XY,
|
||||
cCRNFmtDXN_YX,
|
||||
|
||||
// DXT5 alpha blocks only
|
||||
cCRNFmtDXT5A,
|
||||
|
||||
cCRNFmtTotal,
|
||||
|
||||
cCRNFmtForceDWORD = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
// Various library/file format limits.
|
||||
enum crn_limits
|
||||
{
|
||||
// Max. mipmap level resolution on any axis (will be doubled to 8k in next release).
|
||||
cCRNMaxLevelResolution = 4096,
|
||||
|
||||
cCRNMinPaletteSize = 8,
|
||||
cCRNMaxPaletteSize = 8192,
|
||||
|
||||
cCRNMaxFaces = 6,
|
||||
cCRNMaxLevels = 16,
|
||||
|
||||
cCRNMaxHelperThreads = 16,
|
||||
|
||||
cCRNMinQualityLevel = 0,
|
||||
cCRNMaxQualityLevel = 255
|
||||
};
|
||||
|
||||
// CRN/DDS compression flags.
|
||||
// See the m_flags member in the crn_comp_params struct, below.
|
||||
enum crn_comp_flags
|
||||
{
|
||||
// Enables perceptual colorspace distance metrics if set.
|
||||
// Important: Be sure to disable this when compressing non-sRGB colorspace images, like normal maps!
|
||||
// Default: Set
|
||||
cCRNCompFlagPerceptual = 1,
|
||||
|
||||
// Enables (up to) 8x8 macroblock usage if set. If disabled, only 4x4 blocks are allowed.
|
||||
// Compression ratio will be lower when disabled, but may cut down on blocky artifacts because the process used to determine
|
||||
// where large macroblocks can be used without artifacts isn't perfect.
|
||||
// Default: Set.
|
||||
cCRNCompFlagHierarchical = 2,
|
||||
|
||||
// cCRNCompFlagQuick disables several output file optimizations - intended for things like quicker previews.
|
||||
// Default: Not set.
|
||||
cCRNCompFlagQuick = 4,
|
||||
|
||||
// DXT1: OK to use DXT1 alpha blocks for better quality or DXT1A transparency.
|
||||
// DXT5: OK to use both DXT5 block types.
|
||||
// Currently only used when writing to .DDS files, as .CRN uses only a subset of the possible DXTn block types.
|
||||
// Default: Set.
|
||||
cCRNCompFlagUseBothBlockTypes = 8,
|
||||
|
||||
// OK to use DXT1A transparent indices to encode black (assumes pixel shader ignores fetched alpha).
|
||||
// Currently only used when writing to .DDS files, .CRN never uses alpha blocks.
|
||||
// Default: Not set.
|
||||
cCRNCompFlagUseTransparentIndicesForBlack = 16,
|
||||
|
||||
// Disables endpoint caching, for more deterministic output.
|
||||
// Currently only used when writing to .DDS files.
|
||||
// Default: Not set.
|
||||
cCRNCompFlagDisableEndpointCaching = 32,
|
||||
|
||||
// If enabled, use the cCRNColorEndpointPaletteSize, etc. params to control the CRN palette sizes. Only useful when writing to .CRN files.
|
||||
// Default: Not set.
|
||||
cCRNCompFlagManualPaletteSizes = 64,
|
||||
|
||||
// If enabled, DXT1A alpha blocks are used to encode single bit transparency.
|
||||
// Default: Not set.
|
||||
cCRNCompFlagDXT1AForTransparency = 128,
|
||||
|
||||
// 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).
|
||||
// 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
|
||||
// with some chroma present if doing so will ultimately lead to lower luma error).
|
||||
// Only enable on grayscale source images.
|
||||
// Default: Not set.
|
||||
cCRNCompFlagGrayscaleSampling = 256,
|
||||
|
||||
// If enabled, debug information will be output during compression.
|
||||
// Default: Not set.
|
||||
cCRNCompFlagDebugging = 0x80000000,
|
||||
|
||||
cCRNCompFlagForceDWORD = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
// Controls DXTn quality vs. speed control - only used when compressing to .DDS.
|
||||
enum crn_dxt_quality
|
||||
{
|
||||
cCRNDXTQualitySuperFast,
|
||||
cCRNDXTQualityFast,
|
||||
cCRNDXTQualityNormal,
|
||||
cCRNDXTQualityBetter,
|
||||
cCRNDXTQualityUber,
|
||||
|
||||
cCRNDXTQualityTotal,
|
||||
|
||||
cCRNDXTQualityForceDWORD = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
// Which DXTn compressor to use when compressing to .DDS.
|
||||
enum crn_dxt_compressor_type
|
||||
{
|
||||
cCRNDXTCompressorCRN,
|
||||
cCRNDXTCompressorCRNF,
|
||||
cCRNDXTCompressorRYG,
|
||||
|
||||
cCRNTotalDXTCompressors,
|
||||
|
||||
cCRNDXTCompressorForceDWORD = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
// Compression will stop prematurely (and fail) if the callback returns false.
|
||||
// phase_index, total_phases - high level progress
|
||||
// subphase_index, total_subphases - progress within current phase
|
||||
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);
|
||||
|
||||
// CRN/DDS compression parameters struct.
|
||||
struct crn_comp_params
|
||||
{
|
||||
inline crn_comp_params() { clear(); }
|
||||
|
||||
inline void clear()
|
||||
{
|
||||
m_size_of_obj = sizeof(*this);
|
||||
m_file_type = cCRNFileTypeCRN;
|
||||
m_faces = 1;
|
||||
m_width = 0;
|
||||
m_height = 0;
|
||||
m_levels = 1;
|
||||
m_format = cCRNFmtDXT1;
|
||||
m_flags = cCRNCompFlagPerceptual | cCRNCompFlagHierarchical | cCRNCompFlagUseBothBlockTypes;
|
||||
|
||||
for (crn_uint32 f = 0; f < cCRNMaxFaces; f++)
|
||||
for (crn_uint32 l = 0; l < cCRNMaxLevels; l++)
|
||||
m_pImages[f][l] = NULL;
|
||||
|
||||
m_target_bitrate = 0.0f;
|
||||
m_quality_level = cCRNMaxQualityLevel;
|
||||
m_dxt1a_alpha_threshold = 128;
|
||||
m_dxt_quality = cCRNDXTQualityUber;
|
||||
m_dxt_compressor_type = cCRNDXTCompressorCRN;
|
||||
m_alpha_component = 3;
|
||||
|
||||
m_crn_adaptive_tile_color_psnr_derating = 2.0f;
|
||||
m_crn_adaptive_tile_alpha_psnr_derating = 2.0f;
|
||||
m_crn_color_endpoint_palette_size = 0;
|
||||
m_crn_color_selector_palette_size = 0;
|
||||
m_crn_alpha_endpoint_palette_size = 0;
|
||||
m_crn_alpha_selector_palette_size = 0;
|
||||
|
||||
m_num_helper_threads = 0;
|
||||
m_userdata0 = 0;
|
||||
m_userdata1 = 0;
|
||||
m_pProgress_func = NULL;
|
||||
m_pProgress_func_data = NULL;
|
||||
}
|
||||
|
||||
// Returns true if the input parameters are reasonable.
|
||||
inline bool check() const
|
||||
{
|
||||
if ( (m_file_type > cCRNFileTypeDDS) ||
|
||||
(((int)m_quality_level < (int)cCRNMinQualityLevel) || ((int)m_quality_level > (int)cCRNMaxQualityLevel)) ||
|
||||
(m_dxt1a_alpha_threshold > 255) ||
|
||||
((m_faces != 1) && (m_faces != 6)) ||
|
||||
((m_width < 1) || (m_width > cCRNMaxLevelResolution)) ||
|
||||
((m_height < 1) || (m_height > cCRNMaxLevelResolution)) ||
|
||||
((m_levels < 1) || (m_levels > cCRNMaxLevels)) ||
|
||||
((m_format < cCRNFmtDXT1) || (m_format >= cCRNFmtTotal)) ||
|
||||
((m_crn_color_endpoint_palette_size) && ((m_crn_color_endpoint_palette_size < cCRNMinPaletteSize) || (m_crn_color_endpoint_palette_size > cCRNMaxPaletteSize))) ||
|
||||
((m_crn_color_selector_palette_size) && ((m_crn_color_selector_palette_size < cCRNMinPaletteSize) || (m_crn_color_selector_palette_size > cCRNMaxPaletteSize))) ||
|
||||
((m_crn_alpha_endpoint_palette_size) && ((m_crn_alpha_endpoint_palette_size < cCRNMinPaletteSize) || (m_crn_alpha_endpoint_palette_size > cCRNMaxPaletteSize))) ||
|
||||
((m_crn_alpha_selector_palette_size) && ((m_crn_alpha_selector_palette_size < cCRNMinPaletteSize) || (m_crn_alpha_selector_palette_size > cCRNMaxPaletteSize))) ||
|
||||
(m_alpha_component > 3) ||
|
||||
(m_num_helper_threads > cCRNMaxHelperThreads) ||
|
||||
(m_dxt_quality > cCRNDXTQualityUber) ||
|
||||
(m_dxt_compressor_type >= cCRNTotalDXTCompressors) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helper to set/get flags from m_flags member.
|
||||
inline bool get_flag(crn_comp_flags flag) const { return (m_flags & flag) != 0; }
|
||||
inline void set_flag(crn_comp_flags flag, bool val) { m_flags &= ~flag; if (val) m_flags |= flag; }
|
||||
|
||||
crn_uint32 m_size_of_obj;
|
||||
|
||||
crn_file_type m_file_type; // Output file type: cCRNFileTypeCRN or cCRNFileTypeDDS.
|
||||
|
||||
crn_uint32 m_faces; // 1 (2D map) or 6 (cubemap)
|
||||
crn_uint32 m_width; // [1,cCRNMaxLevelResolution], non-power of 2 OK, non-square OK
|
||||
crn_uint32 m_height; // [1,cCRNMaxLevelResolution], non-power of 2 OK, non-square OK
|
||||
crn_uint32 m_levels; // [1,cCRNMaxLevelResolution], non-power of 2 OK, non-square OK
|
||||
|
||||
crn_format m_format; // Output pixel format.
|
||||
|
||||
crn_uint32 m_flags; // see crn_comp_flags enum
|
||||
|
||||
// Array of pointers to 32bpp input images.
|
||||
const crn_uint32* m_pImages[cCRNMaxFaces][cCRNMaxLevels];
|
||||
|
||||
// Target bitrate - if non-zero, the compressor will use an interpolative search to find the
|
||||
// highest quality level that is <= the target bitrate. If it fails to find a bitrate high enough, it'll
|
||||
// disabling adaptive block sizes (cCRNCompFlagHierarchical flag) and try again. This process can be pretty slow.
|
||||
float m_target_bitrate;
|
||||
|
||||
// Desired quality level.
|
||||
// Currently, CRN and DDS quality levels are not compatible with eachother from an image quality standpoint.
|
||||
crn_uint32 m_quality_level; // [cCRNMinQualityLevel, cCRNMaxQualityLevel]
|
||||
|
||||
// DXTn compression parameters.
|
||||
crn_uint32 m_dxt1a_alpha_threshold;
|
||||
crn_dxt_quality m_dxt_quality;
|
||||
crn_dxt_compressor_type m_dxt_compressor_type;
|
||||
|
||||
// Alpha channel's component. Defaults to 3.
|
||||
crn_uint32 m_alpha_component;
|
||||
|
||||
// Various low-level CRN specific parameters.
|
||||
float m_crn_adaptive_tile_color_psnr_derating;
|
||||
float m_crn_adaptive_tile_alpha_psnr_derating;
|
||||
|
||||
crn_uint32 m_crn_color_endpoint_palette_size; // [cCRNMinPaletteSize,cCRNMaxPaletteSize]
|
||||
crn_uint32 m_crn_color_selector_palette_size; // [cCRNMinPaletteSize,cCRNMaxPaletteSize]
|
||||
|
||||
crn_uint32 m_crn_alpha_endpoint_palette_size; // [cCRNMinPaletteSize,cCRNMaxPaletteSize]
|
||||
crn_uint32 m_crn_alpha_selector_palette_size; // [cCRNMinPaletteSize,cCRNMaxPaletteSize]
|
||||
|
||||
// Number of helper threads to create to assist the compressor. 0=no threading.
|
||||
crn_uint32 m_num_helper_threads;
|
||||
|
||||
// CRN userdata0 and userdata1 members, which are written directly to the header of the output file.
|
||||
crn_uint32 m_userdata0;
|
||||
crn_uint32 m_userdata1;
|
||||
|
||||
// User provided progress callback.
|
||||
crn_progress_callback_func m_pProgress_func;
|
||||
void* m_pProgress_func_data;
|
||||
};
|
||||
|
||||
// Mipmap generator's mode.
|
||||
enum crn_mip_mode
|
||||
{
|
||||
cCRNMipModeUseSourceOrGenerateMips,
|
||||
cCRNMipModeUseSourceMips,
|
||||
cCRNMipModeGenerateMips,
|
||||
cCRNMipModeNoMips,
|
||||
|
||||
cCRNMipModeTotal,
|
||||
|
||||
cCRNModeForceDWORD = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
const wchar_t* crn_get_mip_mode_desc(crn_mip_mode m);
|
||||
const wchar_t* crn_get_mip_mode_name(crn_mip_mode m);
|
||||
|
||||
// Mipmap generator's filter kernel.
|
||||
enum crn_mip_filter
|
||||
{
|
||||
cCRNMipFilterBox,
|
||||
cCRNMipFilterTent,
|
||||
cCRNMipFilterLanczos4,
|
||||
cCRNMipFilterMitchell,
|
||||
cCRNMipFilterKaiser,
|
||||
|
||||
cCRNMipFilterTotal,
|
||||
|
||||
cCRNMipFilterForceDWORD = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
const char* crn_get_mip_filter_name(crn_mip_filter f);
|
||||
|
||||
// Mipmap generator's scale mode.
|
||||
enum crn_scale_mode
|
||||
{
|
||||
cCRNSMDisabled,
|
||||
cCRNSMAbsolute,
|
||||
cCRNSMRelative,
|
||||
cCRNSMLowerPow2,
|
||||
cCRNSMNearestPow2,
|
||||
cCRNSMNextPow2,
|
||||
|
||||
cCRNSMTotal,
|
||||
|
||||
cCRNSMForceDWORD = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
const wchar_t* crn_get_scale_mode_desc(crn_scale_mode sm);
|
||||
|
||||
// Mipmap generator parameters.
|
||||
struct crn_mipmap_params
|
||||
{
|
||||
inline crn_mipmap_params() { clear(); }
|
||||
|
||||
inline void clear()
|
||||
{
|
||||
m_size_of_obj = sizeof(*this);
|
||||
m_mode = cCRNMipModeUseSourceOrGenerateMips;
|
||||
m_filter = cCRNMipFilterKaiser;
|
||||
m_gamma_filtering = true;
|
||||
m_gamma = 2.2f;
|
||||
m_blurriness = .9f;
|
||||
m_renormalize = false;
|
||||
m_tiled = false;
|
||||
m_max_levels = cCRNMaxLevels;
|
||||
m_min_mip_size = 1;
|
||||
|
||||
m_scale_mode = cCRNSMDisabled;
|
||||
m_scale_x = 1.0f;
|
||||
m_scale_y = 1.0f;
|
||||
|
||||
m_window_left = 0;
|
||||
m_window_top = 0;
|
||||
m_window_right = 0;
|
||||
m_window_bottom = 0;
|
||||
|
||||
m_clamp_scale = false;
|
||||
m_clamp_width = 0;
|
||||
m_clamp_height = 0;
|
||||
}
|
||||
|
||||
inline bool check() const { return true; }
|
||||
|
||||
crn_uint32 m_size_of_obj;
|
||||
|
||||
crn_mip_mode m_mode;
|
||||
crn_mip_filter m_filter;
|
||||
|
||||
crn_bool m_gamma_filtering;
|
||||
float m_gamma;
|
||||
|
||||
float m_blurriness;
|
||||
|
||||
crn_uint32 m_max_levels;
|
||||
crn_uint32 m_min_mip_size;
|
||||
|
||||
crn_bool m_renormalize;
|
||||
crn_bool m_tiled;
|
||||
|
||||
crn_scale_mode m_scale_mode;
|
||||
float m_scale_x;
|
||||
float m_scale_y;
|
||||
|
||||
crn_uint32 m_window_left;
|
||||
crn_uint32 m_window_top;
|
||||
crn_uint32 m_window_right;
|
||||
crn_uint32 m_window_bottom;
|
||||
|
||||
crn_bool m_clamp_scale;
|
||||
crn_uint32 m_clamp_width;
|
||||
crn_uint32 m_clamp_height;
|
||||
};
|
||||
|
||||
// -------- High-level helper function definitions for CDN/DDS compression.
|
||||
|
||||
#ifndef CRNLIB_MIN_ALLOC_ALIGNMENT
|
||||
#define CRNLIB_MIN_ALLOC_ALIGNMENT sizeof(size_t) * 2
|
||||
#endif
|
||||
|
||||
// Function to set an optional user provided memory allocation/reallocation/msize routines.
|
||||
// By default, crnlib just uses malloc(), free(), etc. for all allocations.
|
||||
typedef void* (*crn_realloc_func)(void* p, size_t size, size_t* pActual_size, bool movable, void* pUser_data);
|
||||
typedef size_t (*crn_msize_func)(void* p, void* pUser_data);
|
||||
void crn_set_memory_callbacks(crn_realloc_func pRealloc, crn_msize_func pMSize, void* pUser_data);
|
||||
|
||||
// Frees memory blocks allocated by crn_compress(), crn_decompress_crn_to_dds(), or crn_decompress_dds_to_images().
|
||||
void crn_free_block(void *pBlock);
|
||||
|
||||
// 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.
|
||||
// Input parameters:
|
||||
// comp_params is the compression parameters struct, defined above.
|
||||
// compressed_size will be set to the size of the returned memory block containing the output file.
|
||||
// The returned block must be freed by calling crn_free_block().
|
||||
// *pActual_quality_level will be set to the actual quality level used to compress the image. May be NULL.
|
||||
// *pActual_bitrate will be set to the output file's effective bitrate, possibly taking into account LZMA compression. May be NULL.
|
||||
// Return value:
|
||||
// The compressed file data, or NULL on failure.
|
||||
// compressed_size will be set to the size of the returned memory buffer.
|
||||
// Notes:
|
||||
// A "regular" DDS file is compressed using normal DXTn compression at the specified DXT quality level.
|
||||
// A "clustered" DDS file is compressed using clustered DXTn compression to either the target bitrate or the specified integer quality factor.
|
||||
// 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.
|
||||
// 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.
|
||||
// Mipmap levels are simple 32-bit 2D images with a pitch of width*sizeof(uint32), arranged in the usual raster order (top scanline first).
|
||||
// The image pixels may be grayscale (YYYX), grayscale/alpha (YYYA), 24-bit RGBX, or 32-bit RGBA colors (where "X"=don't care).
|
||||
// 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!
|
||||
void *crn_compress(const crn_comp_params &comp_params, crn_uint32 &compressed_size, crn_uint32 *pActual_quality_level = NULL, float *pActual_bitrate = NULL);
|
||||
|
||||
// Like the above function, except this function can also do things like generate mipmaps, and resize or crop the input texture before compression.
|
||||
// The actual operations performed are controlled by the crn_mipmap_params struct members.
|
||||
// Be sure to set the "m_gamma_filtering" member of crn_mipmap_params to false if the input texture is not sRGB.
|
||||
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);
|
||||
|
||||
// Transcodes an entire CRN file to DDS using the crn_decomp.h header file library to do most of the heavy lifting.
|
||||
// The output DDS file's format is guaranteed to be one of the DXTn formats in the crn_format enum.
|
||||
// This is a fast operation, because the CRN format is explicitly designed to be efficiently transcodable to DXTn.
|
||||
// For more control over decompression, see the lower-level helper functions in crn_decomp.h, which do not depend at all on crnlib.
|
||||
void *crn_decompress_crn_to_dds(const void *pCRN_file_data, crn_uint32 &file_size);
|
||||
|
||||
// Decompresses an entire DDS file in any supported format to uncompressed 32-bit/pixel image(s).
|
||||
// See the crnlib::pixel_format in inc/dds_defs.h for a list of the supported DDS formats.
|
||||
// You are responsible for freeing each image, either by calling crn_free_all_images() or manually calling crn_free_block() on each image pointer.
|
||||
struct crn_texture_desc
|
||||
{
|
||||
crn_uint32 m_faces;
|
||||
crn_uint32 m_width;
|
||||
crn_uint32 m_height;
|
||||
crn_uint32 m_levels;
|
||||
crn_uint32 m_fmt_fourcc; // Same as crnlib::pixel_format
|
||||
};
|
||||
bool crn_decompress_dds_to_images(const void *pDDS_file_data, crn_uint32 dds_file_size, crn_uint32 **ppImages, crn_texture_desc &tex_desc);
|
||||
|
||||
// Frees all images allocated by crn_decompress_dds_to_images().
|
||||
void crn_free_all_images(crn_uint32 **ppImages, const crn_texture_desc &desc);
|
||||
|
||||
// -------- crn_format related helpers functions.
|
||||
|
||||
// Returns the FOURCC format equivalent to the specified crn_format.
|
||||
crn_uint32 crn_get_format_fourcc(crn_format fmt);
|
||||
|
||||
// Returns the crn_format's bits per texel.
|
||||
crn_uint32 crn_get_format_bits_per_texel(crn_format fmt);
|
||||
|
||||
// Returns the crn_format's number of bytes per block.
|
||||
crn_uint32 crn_get_bytes_per_dxt_block(crn_format fmt);
|
||||
|
||||
// Returns the non-swizzled, basic DXTn version of the specified crn_format.
|
||||
// This is the format you would supply D3D or OpenGL.
|
||||
crn_format crn_get_fundamental_dxt_format(crn_format fmt);
|
||||
|
||||
// -------- String helpers.
|
||||
|
||||
// Converts a crn_file_type to a string.
|
||||
const wchar_t* crn_get_file_type_ext(crn_file_type file_type);
|
||||
const char* crn_get_file_type_exta(crn_file_type file_type);
|
||||
|
||||
// Converts a crn_format to a string.
|
||||
const char* crn_get_format_stringa(crn_format fmt);
|
||||
const wchar_t* crn_get_format_string(crn_format fmt);
|
||||
|
||||
// Converts a crn_dxt_quality to a string.
|
||||
const wchar_t* crn_get_dxt_quality_string(crn_dxt_quality q);
|
||||
const char* crn_get_dxt_quality_stringa(crn_dxt_quality q);
|
||||
|
||||
// -------- Low-level DXTn 4x4 block compressor API
|
||||
|
||||
// crnlib's DXTn endpoint optimizer actually supports any number of source pixels (i.e. from 1 to thousands, not just 16),
|
||||
// but for simplicity this API only supports 4x4 texel blocks.
|
||||
typedef void *crn_block_compressor_context_t;
|
||||
|
||||
// Create a DXTn block compressor.
|
||||
// Notes this function only supports the basic/nonswizzled DXTn formats (DXT1, DXT3, DXT5, DXT5A, DXN_XY and DXN_YX).
|
||||
// Avoid calling this multiple times if you intend on compressing many blocks, because it allocates some memory.
|
||||
crn_block_compressor_context_t crn_create_block_compressor(const crn_comp_params ¶ms);
|
||||
|
||||
// Compresses a block of 16 pixels to the destination DXTn block.
|
||||
// pDst_block should be 8 (for DXT1/DXT5A) or 16 bytes (all the others).
|
||||
void crn_compress_block(crn_block_compressor_context_t pContext, const crn_uint32 *pPixels, void *pDst_block);
|
||||
|
||||
// Frees a DXTn block compressor.
|
||||
void crn_free_block_compressor(crn_block_compressor_context_t pContext);
|
||||
|
||||
#endif // CRNLIB_H
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
// crnlib uses the ZLIB license:
|
||||
// http://opensource.org/licenses/Zlib
|
||||
//
|
||||
// Copyright (c) 2010-2011 Tenacious Software LLC
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
// File: dds_defs.h
|
||||
// DX9 .DDS file header definitions.
|
||||
#ifndef CRNLIB_DDS_DEFS_H
|
||||
#define CRNLIB_DDS_DEFS_H
|
||||
|
||||
#include "crnlib.h"
|
||||
|
||||
#define CRNLIB_PIXEL_FMT_FOURCC(a, b, c, d) ((a) | ((b) << 8U) | ((c) << 16U) | ((d) << 24U))
|
||||
|
||||
namespace crnlib
|
||||
{
|
||||
enum pixel_format
|
||||
{
|
||||
PIXEL_FMT_INVALID = 0,
|
||||
|
||||
PIXEL_FMT_DXT1 = CRNLIB_PIXEL_FMT_FOURCC('D', 'X', 'T', '1'),
|
||||
PIXEL_FMT_DXT2 = CRNLIB_PIXEL_FMT_FOURCC('D', 'X', 'T', '2'),
|
||||
PIXEL_FMT_DXT3 = CRNLIB_PIXEL_FMT_FOURCC('D', 'X', 'T', '3'),
|
||||
PIXEL_FMT_DXT4 = CRNLIB_PIXEL_FMT_FOURCC('D', 'X', 'T', '4'),
|
||||
PIXEL_FMT_DXT5 = CRNLIB_PIXEL_FMT_FOURCC('D', 'X', 'T', '5'),
|
||||
PIXEL_FMT_3DC = CRNLIB_PIXEL_FMT_FOURCC('A', 'T', 'I', '2'), // DXN_YX
|
||||
PIXEL_FMT_DXN = CRNLIB_PIXEL_FMT_FOURCC('A', '2', 'X', 'Y'), // DXN_XY
|
||||
PIXEL_FMT_DXT5A = CRNLIB_PIXEL_FMT_FOURCC('A', 'T', 'I', '1'), // ATI1N, http://developer.amd.com/media/gpu_assets/Radeon_X1x00_Programming_Guide.pdf
|
||||
|
||||
// Non-standard, crnlib-specific pixel formats (some of these are supported by ATI's compressonator)
|
||||
PIXEL_FMT_DXT5_CCxY = CRNLIB_PIXEL_FMT_FOURCC('C', 'C', 'x', 'Y'),
|
||||
PIXEL_FMT_DXT5_xGxR = CRNLIB_PIXEL_FMT_FOURCC('x', 'G', 'x', 'R'),
|
||||
PIXEL_FMT_DXT5_xGBR = CRNLIB_PIXEL_FMT_FOURCC('x', 'G', 'B', 'R'),
|
||||
PIXEL_FMT_DXT5_AGBR = CRNLIB_PIXEL_FMT_FOURCC('A', 'G', 'B', 'R'),
|
||||
|
||||
PIXEL_FMT_DXT1A = CRNLIB_PIXEL_FMT_FOURCC('D', 'X', '1', 'A'),
|
||||
|
||||
PIXEL_FMT_R8G8B8 = CRNLIB_PIXEL_FMT_FOURCC('R', 'G', 'B', 'x'),
|
||||
PIXEL_FMT_L8 = CRNLIB_PIXEL_FMT_FOURCC('L', 'x', 'x', 'x'),
|
||||
PIXEL_FMT_A8 = CRNLIB_PIXEL_FMT_FOURCC('x', 'x', 'x', 'A'),
|
||||
PIXEL_FMT_A8L8 = CRNLIB_PIXEL_FMT_FOURCC('L', 'x', 'x', 'A'),
|
||||
PIXEL_FMT_A8R8G8B8 = CRNLIB_PIXEL_FMT_FOURCC('R', 'G', 'B', 'A')
|
||||
};
|
||||
|
||||
const crn_uint32 cDDSMaxImageDimensions = 8192U;
|
||||
|
||||
// Total size of header is sizeof(uint32)+cDDSSizeofDDSurfaceDesc2;
|
||||
const crn_uint32 cDDSSizeofDDSurfaceDesc2 = 124;
|
||||
|
||||
// "DDS "
|
||||
const crn_uint32 cDDSFileSignature = 0x20534444;
|
||||
|
||||
struct DDCOLORKEY
|
||||
{
|
||||
crn_uint32 dwUnused0;
|
||||
crn_uint32 dwUnused1;
|
||||
};
|
||||
|
||||
struct DDPIXELFORMAT
|
||||
{
|
||||
crn_uint32 dwSize;
|
||||
crn_uint32 dwFlags;
|
||||
crn_uint32 dwFourCC;
|
||||
crn_uint32 dwRGBBitCount; // ATI compressonator and crnlib sometimes place a FOURCC code here
|
||||
crn_uint32 dwRBitMask;
|
||||
crn_uint32 dwGBitMask;
|
||||
crn_uint32 dwBBitMask;
|
||||
crn_uint32 dwRGBAlphaBitMask;
|
||||
};
|
||||
|
||||
struct DDSCAPS2
|
||||
{
|
||||
crn_uint32 dwCaps;
|
||||
crn_uint32 dwCaps2;
|
||||
crn_uint32 dwCaps3;
|
||||
crn_uint32 dwCaps4;
|
||||
};
|
||||
|
||||
struct DDSURFACEDESC2
|
||||
{
|
||||
crn_uint32 dwSize;
|
||||
crn_uint32 dwFlags;
|
||||
crn_uint32 dwHeight;
|
||||
crn_uint32 dwWidth;
|
||||
union
|
||||
{
|
||||
crn_int32 lPitch;
|
||||
crn_uint32 dwLinearSize;
|
||||
};
|
||||
crn_uint32 dwBackBufferCount;
|
||||
crn_uint32 dwMipMapCount;
|
||||
crn_uint32 dwAlphaBitDepth;
|
||||
crn_uint32 dwUnused0;
|
||||
crn_uint32 lpSurface;
|
||||
DDCOLORKEY unused0;
|
||||
DDCOLORKEY unused1;
|
||||
DDCOLORKEY unused2;
|
||||
DDCOLORKEY unused3;
|
||||
DDPIXELFORMAT ddpfPixelFormat;
|
||||
DDSCAPS2 ddsCaps;
|
||||
crn_uint32 dwUnused1;
|
||||
};
|
||||
|
||||
const crn_uint32 DDSD_CAPS = 0x00000001;
|
||||
const crn_uint32 DDSD_HEIGHT = 0x00000002;
|
||||
const crn_uint32 DDSD_WIDTH = 0x00000004;
|
||||
const crn_uint32 DDSD_PITCH = 0x00000008;
|
||||
|
||||
const crn_uint32 DDSD_BACKBUFFERCOUNT = 0x00000020;
|
||||
const crn_uint32 DDSD_ZBUFFERBITDEPTH = 0x00000040;
|
||||
const crn_uint32 DDSD_ALPHABITDEPTH = 0x00000080;
|
||||
|
||||
const crn_uint32 DDSD_LPSURFACE = 0x00000800;
|
||||
|
||||
const crn_uint32 DDSD_PIXELFORMAT = 0x00001000;
|
||||
const crn_uint32 DDSD_CKDESTOVERLAY = 0x00002000;
|
||||
const crn_uint32 DDSD_CKDESTBLT = 0x00004000;
|
||||
const crn_uint32 DDSD_CKSRCOVERLAY = 0x00008000;
|
||||
|
||||
const crn_uint32 DDSD_CKSRCBLT = 0x00010000;
|
||||
const crn_uint32 DDSD_MIPMAPCOUNT = 0x00020000;
|
||||
const crn_uint32 DDSD_REFRESHRATE = 0x00040000;
|
||||
const crn_uint32 DDSD_LINEARSIZE = 0x00080000;
|
||||
|
||||
const crn_uint32 DDSD_TEXTURESTAGE = 0x00100000;
|
||||
const crn_uint32 DDSD_FVF = 0x00200000;
|
||||
const crn_uint32 DDSD_SRCVBHANDLE = 0x00400000;
|
||||
const crn_uint32 DDSD_DEPTH = 0x00800000;
|
||||
|
||||
const crn_uint32 DDSD_ALL = 0x00fff9ee;
|
||||
|
||||
const crn_uint32 DDPF_ALPHAPIXELS = 0x00000001;
|
||||
const crn_uint32 DDPF_ALPHA = 0x00000002;
|
||||
const crn_uint32 DDPF_FOURCC = 0x00000004;
|
||||
const crn_uint32 DDPF_PALETTEINDEXED8 = 0x00000020;
|
||||
const crn_uint32 DDPF_RGB = 0x00000040;
|
||||
const crn_uint32 DDPF_LUMINANCE = 0x00020000;
|
||||
|
||||
const crn_uint32 DDSCAPS_COMPLEX = 0x00000008;
|
||||
const crn_uint32 DDSCAPS_TEXTURE = 0x00001000;
|
||||
const crn_uint32 DDSCAPS_MIPMAP = 0x00400000;
|
||||
|
||||
const crn_uint32 DDSCAPS2_CUBEMAP = 0x00000200;
|
||||
const crn_uint32 DDSCAPS2_CUBEMAP_POSITIVEX = 0x00000400;
|
||||
const crn_uint32 DDSCAPS2_CUBEMAP_NEGATIVEX = 0x00000800;
|
||||
|
||||
const crn_uint32 DDSCAPS2_CUBEMAP_POSITIVEY = 0x00001000;
|
||||
const crn_uint32 DDSCAPS2_CUBEMAP_NEGATIVEY = 0x00002000;
|
||||
const crn_uint32 DDSCAPS2_CUBEMAP_POSITIVEZ = 0x00004000;
|
||||
const crn_uint32 DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x00008000;
|
||||
|
||||
const crn_uint32 DDSCAPS2_VOLUME = 0x00200000;
|
||||
|
||||
} // namespace crnlib
|
||||
|
||||
#endif // CRNLIB_DDS_DEFS_H
|
||||
Reference in New Issue
Block a user