v1.03 prerelease - Full Linux port of crnlib/crunch, in progress - still more testing to do, and some cmd line options (such as -timestamp) don't work under linux yet, but the core stuff (compression/decompression/transcoding) should work fine and performance under Linux is comparable to Windows. The 3 examples haven't been ported yet.

This commit is contained in:
richgel99@gmail.com
2012-04-26 07:14:21 +00:00
parent fffd983ffa
commit f63e26aee6
89 changed files with 3547 additions and 3829 deletions
+103 -121
View File
@@ -1,5 +1,6 @@
// File: crn_decomp.h - CRN texture decompressor v1.01
// File: crn_decomp.h - Fast CRN->DXTc texture transcoder header file library
// Copyright (c) 2010-2012 Rich Geldreich and Tenacious Software LLC
// See Copyright Notice and license at the end of this file.
//
// This single header file contains *all* of the code necessary to unpack .CRN files to raw DXTn bits.
// It does NOT depend on the crn compression library.
@@ -8,17 +9,14 @@
// If CRND_INCLUDE_CRND_H is NOT defined, the header is included.
// If CRND_HEADER_FILE_ONLY is NOT defined, the implementation is included.
// Define PLATFORM_NACL if compiling under native client.
//#define PLATFORM_NACL
#ifndef CRND_INCLUDE_CRND_H
#define CRND_INCLUDE_CRND_H
// Include crnlib header - only to bring in some basic some CRN-related types.
// Include crnlib.h (only to bring in some basic CRN-related types).
#include "crnlib.h"
#define CRND_LIB_VERSION 101
#define CRND_VERSION_STRING "01.01"
#define CRND_LIB_VERSION 103
#define CRND_VERSION_STRING "01.03"
#ifdef _DEBUG
#define CRND_BUILD_DEBUG
@@ -37,10 +35,13 @@ namespace crnd
typedef uint32 uint32;
typedef unsigned int uint;
typedef signed int int32;
#ifndef PLATFORM_NACL
typedef unsigned __int64 uint64;
typedef signed __int64 int64;
#endif
#ifdef __GNUC__
typedef unsigned long long uint64;
typedef long long int64;
#else
typedef unsigned __int64 uint64;
typedef signed __int64 int64;
#endif
// The crnd library assumes all allocation blocks have at least CRND_MIN_ALLOC_ALIGNMENT alignment.
const uint32 CRND_MIN_ALLOC_ALIGNMENT = sizeof(uint32) * 2U;
@@ -129,29 +130,32 @@ namespace crnd
// The crn_file_info.m_struct_size field must be set before calling this function.
bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFile_info);
// Retrieves texture information from the CRN file.
// The crn_texture_info.m_struct_size field must be set before calling this function.
bool crnd_get_texture_info(const void* pData, uint32 data_size, crn_texture_info* pTexture_info);
// Retrieves mipmap level specific information from the CRN file.
// The crn_level_info.m_struct_size field must be set before calling this function.
bool crnd_get_level_info(const void* pData, uint32 data_size, uint32 level_index, crn_level_info* pLevel_info);
// Transcode/unpack context handle.
typedef void* crnd_unpack_context;
// crnd_unpack_begin() - Decompresses the texture's decoder tables and endpoint/selector palettes.
// Once you call this function, you may call crnd_unpack_level() to unpack one or more mip levels.
// Don't call this once per mip level (unless you absolutely must)!
// This function allocated enough memory to hold: Huffman decompression tables, and the endpoint/selector palettes (color and/or alpha).
// This function allocates enough memory to hold: Huffman decompression tables, and the endpoint/selector palettes (color and/or alpha).
// Worst case allocation is approx. 200k, assuming all palettes contain 8192 entries.
// pData must point to a buffer holding all of the compressed data.
// pData must point to a buffer holding all of the compressed .CRN file data.
// This buffer must be stable until crnd_unpack_end() is called.
// Returns NULL on out of memory or if any of the input parameters are invalid.
// Returns NULL if out of memory, or if any of the input parameters are invalid.
crnd_unpack_context crnd_unpack_begin(const void* pData, uint32 data_size);
// Returns the compressed data associated with a context.
// Returns a pointer to the compressed .CRN data associated with a crnd_unpack_context.
// Returns false if any of the input parameters are invalid.
bool crnd_get_data(crnd_unpack_context pContext, const void** ppData, uint32* pData_size);
// crnd_unpack_level() - Unpacks the specified mipmap level to a destination buffer in cached or write combined memory.
// crnd_unpack_level() - Transcodes the specified mipmap level to a destination buffer in cached or write combined memory.
// pContext - Context created by a call to crnd_unpack_begin().
// ppDst - A pointer to an array of 1 or 6 destination buffer pointers. Cubemaps require an array of 6 pointers, 2D textures require an array of 1 pointer.
// dst_size_in_bytes - Optional size of each destination buffer. Only used for debugging - OK to set to UINT32_MAX.
@@ -166,14 +170,14 @@ namespace crnd
// crnd_unpack_level_segmented() - Unpacks the specified mipmap level from a "segmented" CRN file.
// See the crnd_create_segmented_file() API below.
// Segmented files allow the user to control where the compressed mipmaps are stored.
// Segmented files allow the user to control where the compressed mipmap data is stored.
bool crnd_unpack_level_segmented(
crnd_unpack_context pContext,
const void* pSrc, uint32 src_size_in_bytes,
void** ppDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes,
uint32 level_index);
// crnd_unpack_end() - Frees the decompress tables and unpacked palettes associated with the specified context.
// crnd_unpack_end() - Frees the decompress tables and unpacked palettes associated with the specified unpack context.
// Returns false if the context is NULL, or if it points to an invalid context.
// This function frees all memory associated with the context.
bool crnd_unpack_end(crnd_unpack_context pContext);
@@ -189,7 +193,7 @@ namespace crnd
// Returns the compressed size of the texture's header and compression tables (but no levels).
uint32 crnd_get_segmented_file_size(const void* pData, uint32 data_size);
// Creates a "segmented" CRN texture. The new texture will be created at pBase_data, and will be crnd_get_base_data_size() bytes long.
// Creates a "segmented" CRN texture from a normal CRN texture. The new texture will be created at pBase_data, and will be crnd_get_base_data_size() bytes long.
// base_data_size must be >= crnd_get_base_data_size().
// The base data will contain the CRN header and compression tables, but no mipmap data.
bool crnd_create_segmented_file(const void* pData, uint32 data_size, void* pBase_data, uint base_data_size);
@@ -310,12 +314,11 @@ namespace crnd
#include <stdlib.h>
#include <stdio.h>
#ifndef PLATFORM_NACL
#ifdef WIN32
#include <memory.h>
#else
#include <malloc.h>
#include <process.h>
#endif
#endif
#include <stdarg.h>
#include <new> // needed for placement new, _msize, _expand
@@ -343,7 +346,7 @@ namespace crnd
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef
#ifndef
#define NOMINMAX
#endif
#include "windows.h" // only for IsDebuggerPresent(), DebugBreak(), and OutputDebugStringA()
@@ -531,10 +534,8 @@ namespace crnd
CRND_DEFINE_BUILT_IN_TYPE(unsigned int)
CRND_DEFINE_BUILT_IN_TYPE(long)
CRND_DEFINE_BUILT_IN_TYPE(unsigned long)
#ifndef PLATFORM_NACL
CRND_DEFINE_BUILT_IN_TYPE(__int64)
CRND_DEFINE_BUILT_IN_TYPE(unsigned __int64)
#endif
CRND_DEFINE_BUILT_IN_TYPE(int64)
CRND_DEFINE_BUILT_IN_TYPE(uint64)
CRND_DEFINE_BUILT_IN_TYPE(float)
CRND_DEFINE_BUILT_IN_TYPE(double)
CRND_DEFINE_BUILT_IN_TYPE(long double)
@@ -658,7 +659,7 @@ namespace crnd
namespace math
{
const float cNearlyInfinite = 1.0e+37f;
const float cDegToRad = 0.01745329252f;
const float cRadToDeg = 57.29577951f;
@@ -1275,33 +1276,33 @@ namespace crnd
component_type c[cNumComps];
};
color_quad()
inline color_quad()
{
}
color_quad(eClear) :
r(0), g(0), b(0), a(0)
inline color_quad(eClear) :
r(0), g(0), b(0), a(0)
{
}
color_quad(const color_quad& other) :
r(other.r), g(other.g), b(other.b), a(other.a)
inline color_quad(const color_quad& other) :
r(other.r), g(other.g), b(other.b), a(other.a)
{
}
color_quad(parameter_type y, parameter_type alpha = component_traits::cMax)
inline color_quad(parameter_type y, parameter_type alpha = component_traits::cMax)
{
set(y, alpha);
}
color_quad(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha = component_traits::cMax)
inline color_quad(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha = component_traits::cMax)
{
set(red, green, blue, alpha);
}
template<typename other_component_type, typename other_parameter_type>
color_quad(const color_quad<other_component_type, other_parameter_type>& other) :
r(clamp(other.r)), g(clamp(other.g)), b(clamp(other.b)), a(clamp(other.a))
inline color_quad(const color_quad<other_component_type, other_parameter_type>& other) :
r(clamp(other.r)), g(clamp(other.g)), b(clamp(other.b)), a(clamp(other.a))
{
}
@@ -1313,7 +1314,7 @@ namespace crnd
a = 0;
}
color_quad& operator= (const color_quad& other)
inline color_quad& operator= (const color_quad& other)
{
r = other.r;
g = other.g;
@@ -1323,7 +1324,7 @@ namespace crnd
}
template<typename other_component_type, typename other_parameter_type>
color_quad& operator=(const color_quad<other_component_type, other_parameter_type>& other)
inline color_quad& operator=(const color_quad<other_component_type, other_parameter_type>& other)
{
r = clamp(other.r);
g = clamp(other.g);
@@ -1332,7 +1333,7 @@ namespace crnd
return *this;
}
color_quad& set(parameter_type y, parameter_type alpha = component_traits::cMax)
inline color_quad& set(parameter_type y, parameter_type alpha = component_traits::cMax)
{
y = clamp(y);
r = static_cast<component_type>(y);
@@ -1342,7 +1343,7 @@ namespace crnd
return *this;
}
color_quad& set(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha = component_traits::cMax)
inline color_quad& set(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha = component_traits::cMax)
{
r = static_cast<component_type>(clamp(red));
g = static_cast<component_type>(clamp(green));
@@ -1351,7 +1352,7 @@ namespace crnd
return *this;
}
color_quad& set_noclamp_rgba(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha)
inline color_quad& set_noclamp_rgba(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha)
{
r = static_cast<component_type>(red);
g = static_cast<component_type>(green);
@@ -1360,7 +1361,7 @@ namespace crnd
return *this;
}
color_quad& set_noclamp_rgb(parameter_type red, parameter_type green, parameter_type blue)
inline color_quad& set_noclamp_rgb(parameter_type red, parameter_type green, parameter_type blue)
{
r = static_cast<component_type>(red);
g = static_cast<component_type>(green);
@@ -1368,14 +1369,14 @@ namespace crnd
return *this;
}
static parameter_type get_min_comp() { return component_traits::cMin; }
static parameter_type get_max_comp() { return component_traits::cMax; }
static bool get_comps_are_signed() { return component_traits::cSigned; }
static inline parameter_type get_min_comp() { return component_traits::cMin; }
static inline parameter_type get_max_comp() { return component_traits::cMax; }
static inline bool get_comps_are_signed() { return component_traits::cSigned; }
component_type operator[] (uint32 i) const { CRND_ASSERT(i < cNumComps); return c[i]; }
component_type& operator[] (uint32 i) { CRND_ASSERT(i < cNumComps); return c[i]; }
inline component_type operator[] (uint32 i) const { CRND_ASSERT(i < cNumComps); return c[i]; }
inline component_type& operator[] (uint32 i) { CRND_ASSERT(i < cNumComps); return c[i]; }
color_quad& set_component(uint32 i, parameter_type f)
inline color_quad& set_component(uint32 i, parameter_type f)
{
CRND_ASSERT(i < cNumComps);
@@ -1384,14 +1385,14 @@ namespace crnd
return *this;
}
color_quad& clamp(const color_quad& l, const color_quad& h)
inline color_quad& clamp(const color_quad& l, const color_quad& h)
{
for (uint32 i = 0; i < cNumComps; i++)
c[i] = static_cast<component_type>(math::clamp<parameter_type>(c[i], l[i], h[i]));
return *this;
}
color_quad& clamp(parameter_type l, parameter_type h)
inline color_quad& clamp(parameter_type l, parameter_type h)
{
for (uint32 i = 0; i < cNumComps; i++)
c[i] = static_cast<component_type>(math::clamp<parameter_type>(c[i], l, h));
@@ -1409,23 +1410,23 @@ namespace crnd
{
return static_cast<parameter_type>((13938U * r + 46869U * g + 4729U * b + 32768U) >> 16U);
}
uint32 squared_distance(const color_quad& c, bool alpha = true) const
inline uint32 squared_distance(const color_quad& c, bool alpha = true) const
{
return math::square(r - c.r) + math::square(g - c.g) + math::square(b - c.b) + (alpha ? math::square(a - c.a) : 0);
}
bool rgb_equals(const color_quad& rhs) const
inline bool rgb_equals(const color_quad& rhs) const
{
return (r == rhs.r) && (g == rhs.g) && (b == rhs.b);
}
bool operator== (const color_quad& rhs) const
inline bool operator== (const color_quad& rhs) const
{
return (r == rhs.r) && (g == rhs.g) && (b == rhs.b) && (a == rhs.a);
}
bool operator< (const color_quad& rhs) const
inline bool operator< (const color_quad& rhs) const
{
for (uint32 i = 0; i < cNumComps; i++)
{
@@ -1437,76 +1438,76 @@ namespace crnd
return false;
}
color_quad& operator+= (const color_quad& other)
inline color_quad& operator+= (const color_quad& other)
{
for (uint32 i = 0; i < 4; i++)
c[i] = static_cast<component_type>(clamp(c[i] + other.c[i]));
return *this;
}
color_quad& operator-= (const color_quad& other)
inline color_quad& operator-= (const color_quad& other)
{
for (uint32 i = 0; i < 4; i++)
c[i] = static_cast<component_type>(clamp(c[i] - other.c[i]));
return *this;
}
color_quad& operator*= (parameter_type v)
inline color_quad& operator*= (parameter_type v)
{
for (uint32 i = 0; i < 4; i++)
c[i] = static_cast<component_type>(clamp(c[i] * v));
return *this;
}
color_quad& operator/= (parameter_type v)
inline color_quad& operator/= (parameter_type v)
{
for (uint32 i = 0; i < 4; i++)
c[i] = static_cast<component_type>(c[i] / v);
return *this;
}
color_quad get_swizzled(uint32 x, uint32 y, uint32 z, uint32 w) const
inline color_quad get_swizzled(uint32 x, uint32 y, uint32 z, uint32 w) const
{
CRND_ASSERT((x | y | z | w) < 4);
return color_quad(c[x], c[y], c[z], c[w]);
}
friend color_quad operator+ (const color_quad& lhs, const color_quad& rhs)
inline friend color_quad operator+ (const color_quad& lhs, const color_quad& rhs)
{
color_quad result(lhs);
result += rhs;
return result;
}
friend color_quad operator- (const color_quad& lhs, const color_quad& rhs)
inline friend color_quad operator- (const color_quad& lhs, const color_quad& rhs)
{
color_quad result(lhs);
result -= rhs;
return result;
}
friend color_quad operator* (const color_quad& lhs, parameter_type v)
inline friend color_quad operator* (const color_quad& lhs, parameter_type v)
{
color_quad result(lhs);
result *= v;
return result;
}
friend color_quad operator/ (const color_quad& lhs, parameter_type v)
friend inline color_quad operator/ (const color_quad& lhs, parameter_type v)
{
color_quad result(lhs);
result /= v;
return result;
}
friend color_quad operator* (parameter_type v, const color_quad& rhs)
friend inline color_quad operator* (parameter_type v, const color_quad& rhs)
{
color_quad result(rhs);
result *= v;
return result;
}
uint32 get_min_component_index(bool alpha = true) const
inline uint32 get_min_component_index(bool alpha = true) const
{
uint32 index = 0;
uint32 limit = alpha ? cNumComps : (cNumComps - 1);
@@ -1516,7 +1517,7 @@ namespace crnd
return index;
}
uint32 get_max_component_index(bool alpha = true) const
inline uint32 get_max_component_index(bool alpha = true) const
{
uint32 index = 0;
uint32 limit = alpha ? cNumComps : (cNumComps - 1);
@@ -1526,24 +1527,24 @@ namespace crnd
return index;
}
void get_float4(float* pDst)
inline void get_float4(float* pDst)
{
for (uint32 i = 0; i < 4; i++)
pDst[i] = ((*this)[i] - component_traits::cMin) / float(component_traits::cMax - component_traits::cMin);
}
void get_float3(float* pDst)
inline void get_float3(float* pDst)
{
for (uint32 i = 0; i < 3; i++)
pDst[i] = ((*this)[i] - component_traits::cMin) / float(component_traits::cMax - component_traits::cMin);
}
static color_quad make_black()
static inline color_quad make_black()
{
return color_quad(0, 0, 0, component_traits::cMax);
}
static color_quad make_white()
static inline color_quad make_white()
{
return color_quad(component_traits::cMax, component_traits::cMax, component_traits::cMax, component_traits::cMax);
}
@@ -1988,7 +1989,7 @@ namespace crnd
public:
uint32 m_total_syms;
crnd::vector<uint8> m_code_sizes;
crnd::vector<uint8> m_code_sizes;
prefix_coding::decoder_tables* m_pDecode_tables;
private:
@@ -2009,11 +2010,7 @@ namespace crnd
uint32 decode_bits(uint32 num_bits);
uint32 decode(const static_huffman_data_model& model);
#ifdef PLATFORM_NACL
uint32 stop_decoding();
#else
uint64 stop_decoding();
#endif
public:
const uint8* m_pDecode_buf;
@@ -2052,7 +2049,7 @@ namespace crnd
crnd_output_debug_string(buf);
printf(buf);
puts(buf);
if (crnd_is_debugger_present())
crnd_debug_break();
@@ -2421,11 +2418,13 @@ namespace crnd
p_new = ::malloc(size);
if (pActual_size)
#ifdef PLATFORM_NACL
*pActual_size = p_new ? malloc_usable_size(p_new) : 0;
{
#ifdef WIN32
*pActual_size = p_new ? ::_msize(p_new) : 0;
#else
*pActual_size = p_new ? ::_msize(p_new) : 0;
*pActual_size = p_new ? malloc_usable_size(p_new) : 0;
#endif
}
}
else if (!size)
{
@@ -2438,10 +2437,10 @@ namespace crnd
else
{
void* p_final_block = p;
#ifdef PLATFORM_NACL
p_new = ::realloc(p, size);
#else
#ifdef WIN32
p_new = ::_expand(p, size);
#else
p_new = NULL;
#endif
if (p_new)
@@ -2455,11 +2454,13 @@ namespace crnd
}
if (pActual_size)
#ifdef PLATFORM_NACL
*pActual_size = ::malloc_usable_size(p_final_block);
#else
{
#ifdef WIN32
*pActual_size = ::_msize(p_final_block);
#else
*pActual_size = ::malloc_usable_size(p_final_block);
#endif
}
}
return p_new;
@@ -2468,10 +2469,10 @@ namespace crnd
static size_t crnd_default_msize(void* p, void* pUser_data)
{
pUser_data;
#ifdef PLATFORM_NACL
return p ? malloc_usable_size(p) : 0;
#else
#ifdef WIN32
return p ? _msize(p) : 0;
#else
return p ? malloc_usable_size(p) : 0;
#endif
}
@@ -2722,7 +2723,7 @@ namespace crnd
return false;
if ((pHeader->m_levels < 1) || (pHeader->m_levels > utils::compute_max_mips(pHeader->m_width, pHeader->m_height)))
return false;
if ((pHeader->m_format < cCRNFmtDXT1) || (pHeader->m_format >= cCRNFmtTotal))
if (((int)pHeader->m_format < cCRNFmtDXT1) || ((int)pHeader->m_format >= cCRNFmtTotal))
return false;
if (pFile_info)
@@ -3277,38 +3278,20 @@ uint32 symbol_codec::decode(const static_huffman_data_model& model)
return sym;
}
#ifdef PLATFORM_NACL
uint32 symbol_codec::stop_decoding()
{
#if 0
uint32 i = get_bits(4);
uint32 k = get_bits(3);
i, k;
CRND_ASSERT((i == 15) && (k == 3));
#endif
uint32 n = m_pDecode_buf_next - m_pDecode_buf;
return n;
}
#else
uint64 symbol_codec::stop_decoding()
{
#if 0
#if 0
uint32 i = get_bits(4);
uint32 k = get_bits(3);
i, k;
CRND_ASSERT((i == 15) && (k == 3));
#endif
#endif
uint64 n = m_pDecode_buf_next - m_pDecode_buf;
uint64 n = static_cast<uint64>(m_pDecode_buf_next - m_pDecode_buf);
return n;
}
#endif
} // namespace crnd
// File: crnd_dxt_hc_common.cpp
@@ -3591,10 +3574,9 @@ namespace crnd
namespace crnd
{
#if CRND_CREATE_BYTE_STREAMS
static void write_array_to_file(const wchar_t* pFilename, const vector<uint8>& buf)
static void write_array_to_file(const char* pFilename, const vector<uint8>& buf)
{
FILE* pFile;
_wfopen_s(&pFile, pFilename, L"wb");
FILE* pFile = fopen(pFilename, "wb");
fwrite(&buf[0], buf.size(), 1, pFile);
fclose(pFile);
}
@@ -3659,7 +3641,7 @@ namespace crnd
class crn_unpacker
{
public:
crn_unpacker() :
inline crn_unpacker() :
m_magic(cMagicValue),
m_pData(NULL),
m_data_size(0),
@@ -3667,7 +3649,7 @@ namespace crnd
{
}
~crn_unpacker()
inline ~crn_unpacker()
{
m_magic = 0;
}
@@ -4828,15 +4810,15 @@ namespace crnd
// http://opensource.org/licenses/Zlib
//
// Copyright (c) 2010-2012 Rich Geldreich and 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
+143 -66
View File
@@ -2,18 +2,25 @@
// Copyright (c) 2010-2012 Rich Geldreich and 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.
// This header file contains the public crnlib declarations for DXTn,
// clustered DXTn, and CRN 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
// The crn_decomp.h header file library contains all the code necessary for
// decompression.
//
#ifndef CRNLIB_H
#define CRNLIB_H
#define CRNLIB_VERSION 101
#ifdef _MSC_VER
#pragma warning (disable: 4127) // conditional expression is constant
#endif
#define CRNLIB_VERSION 103
#define CRNLIB_SUPPORT_ATI_COMPRESS 0
#define CRNLIB_SUPPORT_SQUISH 0
typedef unsigned char crn_uint8;
typedef unsigned short crn_uint16;
@@ -28,7 +35,7 @@ enum crn_file_type
{
// .CRN
cCRNFileTypeCRN = 0,
// .DDS using regular DXT or clustered DXT
cCRNFileTypeDDS,
@@ -36,21 +43,21 @@ enum crn_file_type
};
// Supported compressed pixel formats.
// Basically all the standard DX9 formats, with some swizzled DXT5 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
@@ -58,7 +65,7 @@ enum crn_format
cCRNFmtDXT5_AGBR, // Swizzled 4-component
// ATI 3DC and X360 DXN
cCRNFmtDXN_XY,
cCRNFmtDXN_XY,
cCRNFmtDXN_YX,
// DXT5 alpha blocks only
@@ -72,7 +79,7 @@ enum crn_format
// Various library/file format limits.
enum crn_limits
{
// Max. mipmap level resolution on any axis (will be doubled to 8k in next release).
// Max. mipmap level resolution on any axis.
cCRNMaxLevelResolution = 4096,
cCRNMinPaletteSize = 8,
@@ -91,27 +98,27 @@ enum crn_limits
// See the m_flags member in the crn_comp_params struct, below.
enum crn_comp_flags
{
// Enables perceptual colorspace distance metrics if set.
// 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,
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
// 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,
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.
// 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.
// Currently only used when writing to .DDS files, as .CRN uses only a subset of the possible DXTn block types.
// Default: Set.
cCRNCompFlagUseBothBlockTypes = 8,
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.
@@ -120,12 +127,12 @@ enum crn_comp_flags
// Disables endpoint caching, for more deterministic output.
// Currently only used when writing to .DDS files.
// Default: Not set.
cCRNCompFlagDisableEndpointCaching = 32,
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,
@@ -136,7 +143,7 @@ enum crn_comp_flags
// 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,
@@ -158,19 +165,27 @@ enum crn_dxt_quality
cCRNDXTQualityForceDWORD = 0xFFFFFFFF
};
// Which DXTn compressor to use when compressing to .DDS.
// Which DXTn compressor to use when compressing to plain (non-clustered) .DDS.
enum crn_dxt_compressor_type
{
cCRNDXTCompressorCRN,
cCRNDXTCompressorCRNF,
cCRNDXTCompressorRYG,
cCRNDXTCompressorCRN, // Use crnlib's DXTc block compressor (default, highest quality, comparable or better than ati_compress or squish)
cCRNDXTCompressorCRNF, // Use crnlib's "fast" DXTc block compressor
cCRNDXTCompressorRYG, // Use RYG's DXTc block compressor (low quality, but very fast)
#if CRNLIB_SUPPORT_ATI_COMPRESS
cCRNDXTCompressorATI,
#endif
#if CRNLIB_SUPPORT_SQUISH
cCRNDXTCompressorSquish,
#endif
cCRNTotalDXTCompressors,
cCRNDXTCompressorForceDWORD = 0xFFFFFFFF
};
// Compression will stop prematurely (and fail) if the callback returns false.
// Progress callback function.
// Processing 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);
@@ -180,6 +195,7 @@ struct crn_comp_params
{
inline crn_comp_params() { clear(); }
// Clear struct to default parameters.
inline void clear()
{
m_size_of_obj = sizeof(*this);
@@ -216,6 +232,43 @@ struct crn_comp_params
m_pProgress_func_data = NULL;
}
inline bool operator== (const crn_comp_params& rhs) const
{
#define CRNLIB_COMP(x) do { if ((x) != (rhs.x)) return false; } while(0)
CRNLIB_COMP(m_size_of_obj);
CRNLIB_COMP(m_file_type);
CRNLIB_COMP(m_faces);
CRNLIB_COMP(m_width);
CRNLIB_COMP(m_height);
CRNLIB_COMP(m_levels);
CRNLIB_COMP(m_format);
CRNLIB_COMP(m_flags);
CRNLIB_COMP(m_target_bitrate);
CRNLIB_COMP(m_quality_level);
CRNLIB_COMP(m_dxt1a_alpha_threshold);
CRNLIB_COMP(m_dxt_quality);
CRNLIB_COMP(m_dxt_compressor_type);
CRNLIB_COMP(m_alpha_component);
CRNLIB_COMP(m_crn_adaptive_tile_color_psnr_derating);
CRNLIB_COMP(m_crn_adaptive_tile_alpha_psnr_derating);
CRNLIB_COMP(m_crn_color_endpoint_palette_size);
CRNLIB_COMP(m_crn_color_selector_palette_size);
CRNLIB_COMP(m_crn_alpha_endpoint_palette_size);
CRNLIB_COMP(m_crn_alpha_selector_palette_size);
CRNLIB_COMP(m_num_helper_threads);
CRNLIB_COMP(m_userdata0);
CRNLIB_COMP(m_userdata1);
CRNLIB_COMP(m_pProgress_func);
CRNLIB_COMP(m_pProgress_func_data);
for (crn_uint32 f = 0; f < cCRNMaxFaces; f++)
for (crn_uint32 l = 0; l < cCRNMaxLevels; l++)
CRNLIB_COMP(m_pImages[f][l]);
#undef CRNLIB_COMP
return true;
}
// Returns true if the input parameters are reasonable.
inline bool check() const
{
@@ -244,16 +297,16 @@ struct crn_comp_params
// 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
@@ -261,20 +314,20 @@ struct crn_comp_params
// 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
// 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.
// try disabling adaptive block sizes (cCRNCompFlagHierarchical flag) and redo the search. This process can be pretty slow.
float m_target_bitrate;
// Desired quality level.
// 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;
@@ -288,7 +341,7 @@ struct crn_comp_params
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.
// Number of helper threads to create during compression. 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.
@@ -303,18 +356,18 @@ struct crn_comp_params
// Mipmap generator's mode.
enum crn_mip_mode
{
cCRNMipModeUseSourceOrGenerateMips,
cCRNMipModeUseSourceMips,
cCRNMipModeGenerateMips,
cCRNMipModeNoMips,
cCRNMipModeUseSourceOrGenerateMips, // Use source texture's mipmaps if it has any, otherwise generate new mipmaps
cCRNMipModeUseSourceMips, // Use source texture's mipmaps if it has any, otherwise the output has no mipmaps
cCRNMipModeGenerateMips, // Always generate new mipmaps
cCRNMipModeNoMips, // Output texture has no mipmaps
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);
const char* crn_get_mip_mode_desc(crn_mip_mode m);
const char* crn_get_mip_mode_name(crn_mip_mode m);
// Mipmap generator's filter kernel.
enum crn_mip_filter
@@ -323,7 +376,7 @@ enum crn_mip_filter
cCRNMipFilterTent,
cCRNMipFilterLanczos4,
cCRNMipFilterMitchell,
cCRNMipFilterKaiser,
cCRNMipFilterKaiser, // Kaiser=default mipmap filter
cCRNMipFilterTotal,
@@ -347,8 +400,8 @@ enum crn_scale_mode
cCRNSMForceDWORD = 0xFFFFFFFF
};
const wchar_t* crn_get_scale_mode_desc(crn_scale_mode sm);
const char* crn_get_scale_mode_desc(crn_scale_mode sm);
// Mipmap generator parameters.
struct crn_mipmap_params
{
@@ -361,6 +414,7 @@ struct crn_mipmap_params
m_filter = cCRNMipFilterKaiser;
m_gamma_filtering = true;
m_gamma = 2.2f;
// Default "blurriness" factor of .9 actually sharpens the output a little.
m_blurriness = .9f;
m_renormalize = false;
m_tiled = false;
@@ -383,19 +437,45 @@ struct crn_mipmap_params
inline bool check() const { return true; }
inline bool operator== (const crn_mipmap_params& rhs) const
{
#define CRNLIB_COMP(x) do { if ((x) != (rhs.x)) return false; } while(0)
CRNLIB_COMP(m_size_of_obj);
CRNLIB_COMP(m_mode);
CRNLIB_COMP(m_filter);
CRNLIB_COMP(m_gamma_filtering);
CRNLIB_COMP(m_gamma);
CRNLIB_COMP(m_blurriness);
CRNLIB_COMP(m_renormalize);
CRNLIB_COMP(m_tiled);
CRNLIB_COMP(m_max_levels);
CRNLIB_COMP(m_min_mip_size);
CRNLIB_COMP(m_scale_mode);
CRNLIB_COMP(m_scale_x);
CRNLIB_COMP(m_scale_y);
CRNLIB_COMP(m_window_left);
CRNLIB_COMP(m_window_top);
CRNLIB_COMP(m_window_right);
CRNLIB_COMP(m_window_bottom);
CRNLIB_COMP(m_clamp_scale);
CRNLIB_COMP(m_clamp_width);
CRNLIB_COMP(m_clamp_height);
return true;
#undef CRNLIB_COMP
}
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;
@@ -440,11 +520,11 @@ void crn_free_block(void *pBlock);
// 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.
// 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).
// 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).
// 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);
@@ -453,15 +533,15 @@ void *crn_compress(const crn_comp_params &comp_params, crn_uint32 &compressed_si
// 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.
// 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.
// See the crnlib::pixel_format enum in inc/dds_defs.h for a list of the supported DDS formats.
// 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.
struct crn_texture_desc
{
crn_uint32 m_faces;
@@ -493,16 +573,13 @@ 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);
const char* crn_get_file_type_ext(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);
const char* 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);
const char* crn_get_dxt_quality_string(crn_dxt_quality q);
// -------- Low-level DXTn 4x4 block compressor API
@@ -530,15 +607,15 @@ void crn_free_block_compressor(crn_block_compressor_context_t pContext);
// http://opensource.org/licenses/Zlib
//
// Copyright (c) 2010-2012 Rich Geldreich and 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