1 Commits

Author SHA1 Message Date
richgel99@gmail.com 97b7cb25e5 2012-04-16 00:53:29 +00:00
167 changed files with 6826 additions and 28309 deletions
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
-9
View File
@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_workspace_file>
<Workspace title="Workspace">
<Project filename="crunch/crunch_linux.cbp" active="1">
<Depends filename="crnlib/crnlib_linux.cbp" />
</Project>
<Project filename="crnlib/crnlib_linux.cbp" />
</Workspace>
</CodeBlocks_workspace_file>
-96
View File
@@ -1,96 +0,0 @@
COMPILE_OPTIONS = -O3 -fomit-frame-pointer -ffast-math -fno-math-errno -g -fno-strict-aliasing -Wall -Wno-unused-value -Wno-unused -march=core2
LINKER_OPTIONS = -lpthread -g
OBJECTS = \
crn_arealist.o \
crn_assert.o \
crn_checksum.o \
crn_colorized_console.o \
crn_command_line_params.o \
crn_comp.o \
crn_console.o \
crn_core.o \
crn_data_stream.o \
crn_mipmapped_texture.o \
crn_decomp.o \
crn_dxt1.o \
crn_dxt5a.o \
crn_dxt.o \
crn_dxt_endpoint_refiner.o \
crn_dxt_fast.o \
crn_dxt_hc_common.o \
crn_dxt_hc.o \
crn_dxt_image.o \
crn_dynamic_string.o \
crn_file_utils.o \
crn_find_files.o \
crn_hash.o \
crn_hash_map.o \
crn_huffman_codes.o \
crn_image_utils.o \
crnlib.o \
crn_math.o \
crn_mem.o \
crn_pixel_format.o \
crn_platform.o \
crn_prefix_coding.o \
crn_qdxt1.o \
crn_qdxt5.o \
crn_rand.o \
crn_resample_filters.o \
crn_resampler.o \
crn_ryg_dxt.o \
crn_sparse_bit_array.o \
crn_stb_image.o \
crn_strutils.o \
crn_symbol_codec.o \
crn_texture_file_types.o \
crn_threaded_resampler.o \
crn_threading_pthreads.o \
crn_timer.o \
crn_utils.o \
crn_value.o \
crn_vector.o \
crn_zeng.o \
crn_texture_comp.o \
crn_texture_conversion.o \
crn_dds_comp.o \
crn_lzma_codec.o \
crn_ktx_texture.o \
crn_etc.o \
crn_rg_etc1.o \
crn_miniz.o \
crn_jpge.o \
crn_jpgd.o \
lzma_7zBuf2.o \
lzma_7zBuf.o \
lzma_7zCrc.o \
lzma_7zFile.o \
lzma_7zStream.o \
lzma_Alloc.o \
lzma_Bcj2.o \
lzma_Bra86.o \
lzma_Bra.o \
lzma_BraIA64.o \
lzma_LzFind.o \
lzma_LzmaDec.o \
lzma_LzmaEnc.o \
lzma_LzmaLib.o
all: crunch
%.o: %.cpp
g++ $< -o $@ -c $(COMPILE_OPTIONS)
crunch.o: ../crunch/crunch.cpp
g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
corpus_gen.o: ../crunch/corpus_gen.cpp
g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
corpus_test.o: ../crunch/corpus_test.cpp
g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
crunch: $(OBJECTS) crunch.o corpus_gen.o corpus_test.o
g++ $(OBJECTS) crunch.o corpus_gen.o corpus_test.o -o crunch $(LINKER_OPTIONS)
+2 -1
View File
@@ -3,6 +3,7 @@
// Ported from the PowerView DOS image viewer, a product I wrote back in 1993. Not currently used in the open source release of crnlib.
#include "crn_core.h"
#include "crn_arealist.h"
#include <stdio.h>
#define RECT_DEBUG
@@ -19,7 +20,7 @@ namespace crnlib
#ifdef _MSC_VER
_vsnprintf_s(buf, sizeof(buf), pMsg, args);
#else
vsnprintf(buf, sizeof(buf), pMsg, args);
_vsnprintf(buf, sizeof(buf), pMsg, args);
#endif
va_end(args);
+16 -8
View File
@@ -1,9 +1,8 @@
// File: crn_assert.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif
#include <stdio.h>
static bool g_fail_exceptions;
static bool g_exit_on_failure = true;
@@ -17,11 +16,15 @@ void crnlib_assert(const char* pExp, const char* pFile, unsigned line)
{
char buf[512];
#if defined(WIN32) && defined(_MSC_VER)
sprintf_s(buf, sizeof(buf), "%s(%u): Assertion failed: \"%s\"\n", pFile, line, pExp);
#else
sprintf(buf, "%s(%u): Assertion failed: \"%s\"\n", pFile, line, pExp);
#endif
crnlib_output_debug_string(buf);
fputs(buf, stderr);
printf(buf);
if (crnlib_is_debugger_present())
crnlib_debug_break();
@@ -31,21 +34,22 @@ void crnlib_fail(const char* pExp, const char* pFile, unsigned line)
{
char buf[512];
#if defined(WIN32) && defined(_MSC_VER)
sprintf_s(buf, sizeof(buf), "%s(%u): Failure: \"%s\"\n", pFile, line, pExp);
#else
sprintf(buf, "%s(%u): Failure: \"%s\"\n", pFile, line, pExp);
#endif
crnlib_output_debug_string(buf);
fputs(buf, stderr);
printf(buf);
if (crnlib_is_debugger_present())
crnlib_debug_break();
#if CRNLIB_USE_WIN32_API
if (g_fail_exceptions)
RaiseException(CRNLIB_FAIL_EXCEPTION_CODE, 0, 0, NULL);
else
#endif
if (g_exit_on_failure)
else if (g_exit_on_failure)
exit(EXIT_FAILURE);
}
@@ -54,7 +58,11 @@ void trace(const char* pFmt, va_list args)
if (crnlib_is_debugger_present())
{
char buf[512];
#if defined(WIN32) && defined(_MSC_VER)
vsprintf_s(buf, sizeof(buf), pFmt, args);
#else
vsprintf(buf, pFmt, args);
#endif
crnlib_output_debug_string(buf);
}
-208
View File
@@ -1,208 +0,0 @@
// File: crn_atomics.h
#ifndef CRN_ATOMICS_H
#define CRN_ATOMICS_H
#ifdef WIN32
#pragma once
#endif
#ifdef WIN32
#include "crn_winhdr.h"
#endif
#if defined(__GNUC__) && CRNLIB_PLATFORM_PC
extern __inline__ __attribute__((__always_inline__,__gnu_inline__)) void crnlib_yield_processor()
{
__asm__ __volatile__("pause");
}
#else
CRNLIB_FORCE_INLINE void crnlib_yield_processor()
{
#if CRNLIB_USE_MSVC_INTRINSICS
#if CRNLIB_PLATFORM_PC_X64
_mm_pause();
#else
YieldProcessor();
#endif
#else
// No implementation
#endif
}
#endif
#if CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS
extern "C" __int64 _InterlockedCompareExchange64(__int64 volatile * Destination, __int64 Exchange, __int64 Comperand);
#if defined(_MSC_VER)
#pragma intrinsic(_InterlockedCompareExchange64)
#endif
#endif // CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS
namespace crnlib
{
#if CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS
typedef LONG atomic32_t;
typedef LONGLONG atomic64_t;
// Returns the original value.
inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return InterlockedCompareExchange(pDest, exchange, comparand);
}
// Returns the original value.
inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0);
return _InterlockedCompareExchange64(pDest, exchange, comparand);
}
// Returns the resulting incremented value.
inline atomic32_t atomic_increment32(atomic32_t volatile *pDest)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return InterlockedIncrement(pDest);
}
// Returns the resulting decremented value.
inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return InterlockedDecrement(pDest);
}
// Returns the original value.
inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return InterlockedExchange(pDest, val);
}
// Returns the resulting value.
inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return InterlockedExchangeAdd(pDest, val) + val;
}
// Returns the original value.
inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return InterlockedExchangeAdd(pDest, val);
}
#elif CRNLIB_USE_GCC_ATOMIC_BUILTINS
typedef long atomic32_t;
typedef long long atomic64_t;
// Returns the original value.
inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return __sync_val_compare_and_swap(pDest, comparand, exchange);
}
// Returns the original value.
inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0);
return __sync_val_compare_and_swap(pDest, comparand, exchange);
}
// Returns the resulting incremented value.
inline atomic32_t atomic_increment32(atomic32_t volatile *pDest)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return __sync_add_and_fetch(pDest, 1);
}
// Returns the resulting decremented value.
inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return __sync_sub_and_fetch(pDest, 1);
}
// Returns the original value.
inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return __sync_lock_test_and_set(pDest, val);
}
// Returns the resulting value.
inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return __sync_add_and_fetch(pDest, val);
}
// Returns the original value.
inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return __sync_fetch_and_add(pDest, val);
}
#else
#define CRNLIB_NO_ATOMICS 1
// Atomic ops not supported - but try to do something reasonable. Assumes no threading at all.
typedef long atomic32_t;
typedef long long atomic64_t;
inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
atomic32_t cur = *pDest;
if (cur == comparand)
*pDest = exchange;
return cur;
}
inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0);
atomic64_t cur = *pDest;
if (cur == comparand)
*pDest = exchange;
return cur;
}
inline atomic32_t atomic_increment32(atomic32_t volatile *pDest)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return (*pDest += 1);
}
inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return (*pDest -= 1);
}
inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
atomic32_t cur = *pDest;
*pDest = val;
return cur;
}
inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
return (*pDest += val);
}
inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val)
{
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
atomic32_t cur = *pDest;
*pDest += val;
return cur;
}
#endif
} // namespace crnlib
#endif // CRN_ATOMICS_H
+21 -16
View File
@@ -2,6 +2,7 @@
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#include "crn_data_stream.h"
#include <stdio.h>
namespace crnlib
{
@@ -12,13 +13,13 @@ namespace crnlib
{
}
cfile_stream(FILE* pFile, const char* pFilename, uint attribs, bool has_ownership) :
cfile_stream(FILE* pFile, const wchar_t* pFilename, uint attribs, bool has_ownership) :
data_stream(), m_pFile(NULL), m_size(0), m_ofs(0), m_has_ownership(false)
{
open(pFile, pFilename, attribs, has_ownership);
}
cfile_stream(const char* pFilename, uint attribs = cDataStreamReadable | cDataStreamSeekable, bool open_existing = false) :
cfile_stream(const wchar_t* pFilename, uint attribs = cDataStreamReadable | cDataStreamSeekable, bool open_existing = false) :
data_stream(), m_pFile(NULL), m_size(0), m_ofs(0), m_has_ownership(false)
{
open(pFilename, attribs, open_existing);
@@ -54,7 +55,7 @@ namespace crnlib
return false;
}
bool open(FILE* pFile, const char* pFilename, uint attribs, bool has_ownership)
bool open(FILE* pFile, const wchar_t* pFilename, uint attribs, bool has_ownership)
{
CRNLIB_ASSERT(pFile);
CRNLIB_ASSERT(pFilename);
@@ -66,17 +67,17 @@ namespace crnlib
m_has_ownership = has_ownership;
m_attribs = static_cast<uint16>(attribs);
m_ofs = crn_ftell(m_pFile);
crn_fseek(m_pFile, 0, SEEK_END);
m_size = crn_ftell(m_pFile);
crn_fseek(m_pFile, m_ofs, SEEK_SET);
m_ofs = _ftelli64(m_pFile);
_fseeki64(m_pFile, 0, SEEK_END);
m_size = _ftelli64(m_pFile);
_fseeki64(m_pFile, m_ofs, SEEK_SET);
m_opened = true;
return true;
}
bool open(const char* pFilename, uint attribs = cDataStreamReadable | cDataStreamSeekable, bool open_existing = false)
bool open(const wchar_t* pFilename, uint attribs = cDataStreamReadable | cDataStreamSeekable, bool open_existing = false)
{
CRNLIB_ASSERT(pFilename);
@@ -84,13 +85,13 @@ namespace crnlib
m_attribs = static_cast<uint16>(attribs);
const char* pMode;
const wchar_t* pMode;
if ((is_readable()) && (is_writable()))
pMode = open_existing ? "r+b" : "w+b";
pMode = open_existing ? L"r+b" : L"w+b";
else if (is_writable())
pMode = open_existing ? "ab" : "wb";
pMode = open_existing ? L"ab" : L"wb";
else if (is_readable())
pMode = "rb";
pMode = L"rb";
else
{
set_error();
@@ -98,7 +99,11 @@ namespace crnlib
}
FILE* pFile = NULL;
crn_fopen(&pFile, pFilename, pMode);
#ifdef _MSC_VER
_wfopen_s(&pFile, pFilename, pMode);
#else
pFile = _wfopen(pFilename, pMode);
#endif
m_has_ownership = true;
if (!pFile)
@@ -204,7 +209,7 @@ namespace crnlib
if (static_cast<uint64>(new_ofs) != m_ofs)
{
if (crn_fseek(m_pFile, new_ofs, SEEK_SET) != 0)
if (_fseeki64(m_pFile, new_ofs, SEEK_SET) != 0)
{
set_error();
return false;
@@ -216,7 +221,7 @@ namespace crnlib
return true;
}
static bool read_file_into_array(const char* pFilename, vector<uint8>& buf)
static bool read_file_into_array(const wchar_t* pFilename, vector<uint8>& buf)
{
cfile_stream in_stream(pFilename);
if (!in_stream.is_opened())
@@ -224,7 +229,7 @@ namespace crnlib
return in_stream.read_array(buf);
}
static bool write_array_to_file(const char* pFilename, const vector<uint8>& buf)
static bool write_array_to_file(const wchar_t* pFilename, const vector<uint8>& buf)
{
cfile_stream out_stream(pFilename, cDataStreamWritable|cDataStreamSeekable);
if (!out_stream.is_opened())
+5 -5
View File
@@ -140,7 +140,7 @@ namespace crnlib
inline uint get_num_training_vecs() const { return m_training_vecs.size(); }
const VectorType& get_training_vec(uint index) const { return m_training_vecs[index].first; }
uint get_training_vec_weight(uint index) const { return m_training_vecs[index].second; }
const uint get_training_vec_weight(uint index) const { return m_training_vecs[index].second; }
typedef crnlib::vector< std::pair<VectorType, uint> > training_vec_array;
@@ -170,7 +170,7 @@ namespace crnlib
return m_codebook;
}
uint find_best_codebook_entry(const VectorType& v) const
const uint find_best_codebook_entry(const VectorType& v) const
{
uint cur_node_index = 0;
@@ -218,7 +218,7 @@ namespace crnlib
}
}
uint find_best_codebook_entry_fs(const VectorType& v) const
const uint find_best_codebook_entry_fs(const VectorType& v) const
{
float best_dist = math::cNearlyInfinite;
uint best_index = 0;
@@ -362,7 +362,7 @@ namespace crnlib
void compute_split_estimate(VectorType& left_child_res, VectorType& right_child_res, const vq_node& parent_node)
{
VectorType furthest(0);
VectorType furthest;
double furthest_dist = -1.0f;
for (uint i = 0; i < parent_node.m_vectors.size(); i++)
@@ -377,7 +377,7 @@ namespace crnlib
}
}
VectorType opposite(0);
VectorType opposite;
double opposite_dist = -1.0f;
for (uint i = 0; i < parent_node.m_vectors.size(); i++)
+27 -327
View File
@@ -11,19 +11,8 @@ namespace crnlib
{
cSigned = false,
cFloat = false,
cMin = cUINT8_MIN,
cMax = cUINT8_MAX
};
};
template<> struct color_quad_component_traits<int8>
{
enum
{
cSigned = true,
cFloat = false,
cMin = cINT8_MIN,
cMax = cINT8_MAX
cMin = UINT8_MIN,
cMax = UINT8_MAX
};
};
@@ -33,8 +22,8 @@ namespace crnlib
{
cSigned = true,
cFloat = false,
cMin = cINT16_MIN,
cMax = cINT16_MAX
cMin = INT16_MIN,
cMax = INT16_MAX
};
};
@@ -44,8 +33,8 @@ namespace crnlib
{
cSigned = false,
cFloat = false,
cMin = cUINT16_MIN,
cMax = cUINT16_MAX
cMin = UINT16_MIN,
cMax = UINT16_MAX
};
};
@@ -55,8 +44,8 @@ namespace crnlib
{
cSigned = true,
cFloat = false,
cMin = cINT32_MIN,
cMax = cINT32_MAX
cMin = INT32_MIN,
cMax = INT32_MAX
};
};
@@ -66,8 +55,8 @@ namespace crnlib
{
cSigned = false,
cFloat = false,
cMin = cUINT32_MIN,
cMax = cUINT32_MAX
cMin = UINT32_MIN,
cMax = UINT32_MAX
};
};
@@ -77,8 +66,8 @@ namespace crnlib
{
cSigned = false,
cFloat = true,
cMin = cINT32_MIN,
cMax = cINT32_MAX
cMin = INT32_MIN,
cMax = INT32_MAX
};
};
@@ -88,8 +77,8 @@ namespace crnlib
{
cSigned = false,
cFloat = true,
cMin = cINT32_MIN,
cMax = cINT32_MAX
cMin = INT32_MIN,
cMax = INT32_MAX
};
};
@@ -97,22 +86,21 @@ namespace crnlib
class color_quad : public helpers::rel_ops<color_quad<component_type, parameter_type> >
{
template<typename T>
static inline parameter_type clamp(T v)
static inline T clamp(T v)
{
parameter_type result = static_cast<parameter_type>(v);
if (!component_traits::cFloat)
{
if (v < component_traits::cMin)
result = static_cast<parameter_type>(component_traits::cMin);
v = component_traits::cMin;
else if (v > component_traits::cMax)
result = static_cast<parameter_type>(component_traits::cMax);
v = component_traits::cMax;
}
return result;
return v;
}
#ifdef _MSC_VER
template<>
static inline parameter_type clamp(int v)
static inline int clamp(int v)
{
if (!component_traits::cFloat)
{
@@ -129,7 +117,7 @@ namespace crnlib
v = component_traits::cMax;
}
}
return static_cast<parameter_type>(v);
return v;
}
#endif
@@ -191,7 +179,7 @@ namespace crnlib
template<typename other_component_type, typename other_parameter_type>
inline color_quad(const color_quad<other_component_type, other_parameter_type>& other) :
r(static_cast<component_type>(clamp(other.r))), g(static_cast<component_type>(clamp(other.g))), b(static_cast<component_type>(clamp(other.b))), a(static_cast<component_type>(clamp(other.a)))
r(clamp(other.r)), g(clamp(other.g)), b(clamp(other.b)), a(clamp(other.a))
{
}
@@ -212,21 +200,13 @@ namespace crnlib
return *this;
}
inline color_quad& set_rgb(const color_quad& other)
{
r = other.r;
g = other.g;
b = other.b;
return *this;
}
template<typename other_component_type, typename other_parameter_type>
inline color_quad& operator=(const color_quad<other_component_type, other_parameter_type>& other)
{
r = static_cast<component_type>(clamp(other.r));
g = static_cast<component_type>(clamp(other.g));
b = static_cast<component_type>(clamp(other.b));
a = static_cast<component_type>(clamp(other.a));
r = clamp(other.r);
g = clamp(other.g);
b = clamp(other.b);
a = clamp(other.a);
return *this;
}
@@ -539,7 +519,6 @@ namespace crnlib
};
typedef color_quad<uint8, int> color_quad_u8;
typedef color_quad<int8, int> color_quad_i8;
typedef color_quad<int16, int> color_quad_i16;
typedef color_quad<uint16, int> color_quad_u16;
typedef color_quad<int32, int> color_quad_i32;
@@ -654,7 +633,7 @@ namespace crnlib
const int YR = 19595, YG = 38470, YB = 7471, CB_R = -11059, CB_G = -21709, CB_B = 32768, CR_R = 32768, CR_G = -27439, CR_B = -5329;
// YCbCr->RGB constants, scaled by 2^16
const int R_CR = 91881, B_CB = 116130, G_CR = -46802, G_CB = -22554;
inline int RGB_to_Y(const color_quad_u8& rgb)
{
const int r = rgb[0], g = rgb[1], b = rgb[2];
@@ -684,7 +663,7 @@ namespace crnlib
rgb.b = clamp_component(y + ((B_CB * cb + 32768) >> 16));
rgb.a = 255;
}
// Float RGB->YCbCr constants
const float S = 1.0f/65536.0f;
const float F_YR = S*YR, F_YG = S*YG, F_YB = S*YB, F_CB_R = S*CB_R, F_CB_G = S*CB_G, F_CB_B = S*CB_B, F_CR_R = S*CR_R, F_CR_G = S*CR_G, F_CR_B = S*CR_B;
@@ -711,284 +690,5 @@ namespace crnlib
} // namespace color
// This class purposely trades off speed for extremely flexibility. It can handle any component swizzle, any pixel type from 1-4 components and 1-32 bits/component,
// any pixel size between 1-16 bytes/pixel, any pixel stride, any color_quad data type (signed/unsigned/float 8/16/32 bits/component), and scaled/non-scaled components.
// On the downside, it's freaking slow.
class pixel_packer
{
public:
pixel_packer()
{
clear();
}
pixel_packer(uint num_comps, uint bits_per_comp, int pixel_stride = -1, bool reversed = false)
{
init(num_comps, bits_per_comp, pixel_stride, reversed);
}
pixel_packer(const char* pComp_map, int pixel_stride = -1, int force_comp_size = -1)
{
init(pComp_map, pixel_stride, force_comp_size);
}
void clear()
{
utils::zero_this(this);
}
inline bool is_valid() const { return m_pixel_stride > 0; }
inline uint get_pixel_stride() const { return m_pixel_stride; }
void set_pixel_stride(uint n) { m_pixel_stride = n; }
uint get_num_comps() const { return m_num_comps; }
uint get_comp_size(uint index) const { CRNLIB_ASSERT(index < 4); return m_comp_size[index]; }
uint get_comp_ofs(uint index) const { CRNLIB_ASSERT(index < 4); return m_comp_ofs[index]; }
uint get_comp_max(uint index) const { CRNLIB_ASSERT(index < 4); return m_comp_max[index]; }
bool get_rgb_is_luma() const { return m_rgb_is_luma; }
template<typename color_quad_type>
const void* unpack(const void* p, color_quad_type& color, bool rescale = true) const
{
const uint8* pSrc = static_cast<const uint8*>(p);
for (uint i = 0; i < 4; i++)
{
const uint comp_size = m_comp_size[i];
if (!comp_size)
{
if (color_quad_type::component_traits::cFloat)
color[i] = static_cast< typename color_quad_type::parameter_t >((i == 3) ? 1 : 0);
else
color[i] = static_cast< typename color_quad_type::parameter_t >((i == 3) ? color_quad_type::component_traits::cMax : 0);
continue;
}
uint n = 0, dst_bit_ofs = 0;
uint src_bit_ofs = m_comp_ofs[i];
while (dst_bit_ofs < comp_size)
{
const uint byte_bit_ofs = src_bit_ofs & 7;
n |= ((pSrc[src_bit_ofs >> 3] >> byte_bit_ofs) << dst_bit_ofs);
const uint bits_read = 8 - byte_bit_ofs;
src_bit_ofs += bits_read;
dst_bit_ofs += bits_read;
}
const uint32 mx = m_comp_max[i];
n &= mx;
const uint32 h = static_cast<uint32>(color_quad_type::component_traits::cMax);
if (color_quad_type::component_traits::cFloat)
color.set_component(i, static_cast<typename color_quad_type::parameter_t>(n));
else if (rescale)
color.set_component(i, static_cast<typename color_quad_type::parameter_t>( (static_cast<uint64>(n) * h + (mx >> 1U)) / mx ) );
else if (color_quad_type::component_traits::cSigned)
color.set_component(i, static_cast<typename color_quad_type::parameter_t>(math::minimum<uint32>(n, h)));
else
color.set_component(i, static_cast<typename color_quad_type::parameter_t>(n));
}
if (m_rgb_is_luma)
{
color[0] = color[1];
color[2] = color[1];
}
return pSrc + m_pixel_stride;
}
template<typename color_quad_type>
void* pack(const color_quad_type& color, void* p, bool rescale = true) const
{
uint8* pDst = static_cast<uint8*>(p);
for (uint i = 0; i < 4; i++)
{
const uint comp_size = m_comp_size[i];
if (!comp_size)
continue;
uint32 mx = m_comp_max[i];
uint32 n;
if (color_quad_type::component_traits::cFloat)
{
typename color_quad_type::parameter_t t = color[i];
if (t < 0.0f)
n = 0;
else if (t > static_cast<typename color_quad_type::parameter_t>(mx))
n = mx;
else
n = math::minimum<uint32>(static_cast<uint32>(floor(t + .5f)), mx);
}
else if (rescale)
{
if (color_quad_type::component_traits::cSigned)
n = math::maximum<int>(static_cast<int>(color[i]), 0);
else
n = static_cast<uint32>(color[i]);
const uint32 h = static_cast<uint32>(color_quad_type::component_traits::cMax);
n = static_cast<uint32>((static_cast<uint64>(n) * mx + (h >> 1)) / h);
}
else
{
if (color_quad_type::component_traits::cSigned)
n = math::minimum<uint32>(static_cast<uint32>(math::maximum<int>(static_cast<int>(color[i]), 0)), mx);
else
n = math::minimum<uint32>(static_cast<uint32>(color[i]), mx);
}
uint src_bit_ofs = 0;
uint dst_bit_ofs = m_comp_ofs[i];
while (src_bit_ofs < comp_size)
{
const uint cur_byte_bit_ofs = (dst_bit_ofs & 7);
const uint cur_byte_bits = 8 - cur_byte_bit_ofs;
uint byte_val = pDst[dst_bit_ofs >> 3];
uint bit_mask = (mx << cur_byte_bit_ofs) & 0xFF;
byte_val &= ~bit_mask;
byte_val |= (n << cur_byte_bit_ofs);
pDst[dst_bit_ofs >> 3] = static_cast<uint8>(byte_val);
mx >>= cur_byte_bits;
n >>= cur_byte_bits;
dst_bit_ofs += cur_byte_bits;
src_bit_ofs += cur_byte_bits;
}
}
return pDst + m_pixel_stride;
}
bool init(uint num_comps, uint bits_per_comp, int pixel_stride = -1, bool reversed = false)
{
clear();
if ((num_comps < 1) || (num_comps > 4) || (bits_per_comp < 1) || (bits_per_comp > 32))
{
CRNLIB_ASSERT(0);
return false;
}
for (uint i = 0; i < num_comps; i++)
{
m_comp_size[i] = bits_per_comp;
m_comp_ofs[i] = i * bits_per_comp;
if (reversed)
m_comp_ofs[i] = ((num_comps - 1) * bits_per_comp) - m_comp_ofs[i];
}
for (uint i = 0; i < 4; i++)
m_comp_max[i] = static_cast<uint32>((1ULL << m_comp_size[i]) - 1ULL);
m_pixel_stride = (pixel_stride >= 0) ? pixel_stride : (num_comps * bits_per_comp + 7) / 8;
return true;
}
// Format examples:
// R16G16B16
// B5G6R5
// B5G5R5x1
// Y8A8
// A8R8G8B8
// First component is at LSB in memory. Assumes unsigned integer components, 1-32bits each.
bool init(const char* pComp_map, int pixel_stride = -1, int force_comp_size = -1)
{
clear();
uint cur_bit_ofs = 0;
while (*pComp_map)
{
char c = *pComp_map++;
int comp_index = -1;
if (c == 'R')
comp_index = 0;
else if (c == 'G')
comp_index = 1;
else if (c == 'B')
comp_index = 2;
else if (c == 'A')
comp_index = 3;
else if (c == 'Y')
comp_index = 4;
else if (c != 'x')
return false;
uint comp_size = 0;
uint n = *pComp_map;
if ((n >= '0') && (n <= '9'))
{
comp_size = n - '0';
pComp_map++;
n = *pComp_map;
if ((n >= '0') && (n <= '9'))
{
comp_size = (comp_size * 10) + (n - '0');
pComp_map++;
}
}
if (force_comp_size != -1)
comp_size = force_comp_size;
if ((!comp_size) || (comp_size > 32))
return false;
if (comp_index == 4)
{
if (m_comp_size[0] || m_comp_size[1] || m_comp_size[2])
return false;
//m_comp_ofs[0] = m_comp_ofs[1] = m_comp_ofs[2] = cur_bit_ofs;
//m_comp_size[0] = m_comp_size[1] = m_comp_size[2] = comp_size;
m_comp_ofs[1] = cur_bit_ofs;
m_comp_size[1] = comp_size;
m_rgb_is_luma = true;
m_num_comps++;
}
else if (comp_index >= 0)
{
if (m_comp_size[comp_index])
return false;
m_comp_ofs[comp_index] = cur_bit_ofs;
m_comp_size[comp_index] = comp_size;
m_num_comps++;
}
cur_bit_ofs += comp_size;
}
for (uint i = 0; i < 4; i++)
m_comp_max[i] = static_cast<uint32>((1ULL << m_comp_size[i]) - 1ULL);
if (pixel_stride >= 0)
m_pixel_stride = pixel_stride;
else
m_pixel_stride = (cur_bit_ofs + 7) / 8;
return true;
}
private:
uint m_pixel_stride;
uint m_num_comps;
uint m_comp_size[4];
uint m_comp_ofs[4];
uint m_comp_max[4];
bool m_rgb_is_luma;
};
} // namespace crnlib
+124 -157
View File
@@ -5,67 +5,41 @@
#include "crn_console.h"
#include "crn_cfile_stream.h"
#ifdef WIN32
#define CRNLIB_CMD_LINE_ALLOW_SLASH_PARAMS 1
#endif
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif
namespace crnlib
{
void get_command_line_as_single_string(dynamic_string& cmd_line, int argc, char *argv[])
{
argc, argv;
#if CRNLIB_USE_WIN32_API
cmd_line.set(GetCommandLineA());
#else
cmd_line.clear();
for (int i = 0; i < argc; i++)
{
dynamic_string tmp(argv[i]);
if ((tmp.front() != '"') && (tmp.front() != '-') && (tmp.front() != '@'))
tmp = "\"" + tmp + "\"";
if (cmd_line.get_len())
cmd_line += " ";
cmd_line += tmp;
}
#endif
}
command_line_params::command_line_params()
{
}
void command_line_params::clear()
{
m_params.clear();
m_param_map.clear();
}
bool command_line_params::split_params(const char* p, dynamic_string_array& params)
bool command_line_params::split_params(const wchar_t* p, dynamic_wstring_array& params)
{
bool within_param = false;
bool within_quote = false;
uint ofs = 0;
dynamic_string str;
dynamic_wstring str;
while (p[ofs])
{
const char c = p[ofs];
const wchar_t c = p[ofs];
if (within_param)
{
if (within_quote)
{
if (c == '"')
if (c == L'"')
within_quote = false;
str.append_char(c);
}
else if ((c == ' ') || (c == '\t'))
else if ((c == L' ') || (c == L'\t'))
{
if (!str.is_empty())
{
@@ -76,144 +50,141 @@ namespace crnlib
}
else
{
if (c == '"')
if (c == L'"')
within_quote = true;
str.append_char(c);
}
}
else if ((c != ' ') && (c != '\t'))
else if ((c != L' ') && (c != L'\t'))
{
within_param = true;
if (c == '"')
if (c == L'"')
within_quote = true;
str.append_char(c);
}
ofs++;
}
if (within_quote)
{
console::error("Unmatched quote in command line \"%s\"", p);
console::error(L"Unmatched quote in command line \"%s\"", p);
return false;
}
if (!str.is_empty())
params.push_back(str);
return true;
}
bool command_line_params::load_string_file(const char* pFilename, dynamic_string_array& strings)
bool command_line_params::load_string_file(const wchar_t* pFilename, dynamic_wstring_array& strings)
{
cfile_stream in_stream;
if (!in_stream.open(pFilename, cDataStreamReadable | cDataStreamSeekable))
{
console::error("Unable to open file \"%s\" for reading!", pFilename);
console::error(L"Unable to open file \"%s\" for reading!", pFilename);
return false;
}
dynamic_string ansi_str;
for ( ; ; )
{
if (!in_stream.read_line(ansi_str))
break;
ansi_str.trim();
if (ansi_str.is_empty())
continue;
strings.push_back(dynamic_string(ansi_str.get_ptr()));
strings.push_back(dynamic_wstring(ansi_str.get_ptr()));
}
return true;
}
bool command_line_params::parse(const dynamic_string_array& params, uint n, const param_desc* pParam_desc)
bool command_line_params::parse(const dynamic_wstring_array& params, uint n, const param_desc* pParam_desc)
{
CRNLIB_ASSERT(n && pParam_desc);
m_params = params;
uint arg_index = 0;
while (arg_index < params.size())
{
const uint cur_arg_index = arg_index;
const dynamic_string& src_param = params[arg_index++];
const dynamic_wstring& src_param = params[arg_index++];
if (src_param.is_empty())
continue;
#if CRNLIB_CMD_LINE_ALLOW_SLASH_PARAMS
if ((src_param[0] == '/') || (src_param[0] == '-'))
#else
if (src_param[0] == '-')
#endif
if ((src_param[0] == L'/') || (src_param[0] == L'-'))
{
if (src_param.get_len() < 2)
{
console::error("Invalid command line parameter: \"%s\"", src_param.get_ptr());
console::error(L"Invalid command line parameter: \"%s\"", src_param.get_ptr());
return false;
}
dynamic_string key_str(src_param);
dynamic_wstring key_str(src_param);
key_str.right(1);
int modifier = 0;
char c = key_str[key_str.get_len() - 1];
if (c == '+')
wchar_t c = key_str[key_str.get_len() - 1];
if (c == L'+')
modifier = 1;
else if (c == '-')
else if (c == L'-')
modifier = -1;
if (modifier)
key_str.left(key_str.get_len() - 1);
uint param_index;
for (param_index = 0; param_index < n; param_index++)
if (key_str == pParam_desc[param_index].m_pName)
break;
if (param_index == n)
{
console::error("Unrecognized command line parameter: \"%s\"", src_param.get_ptr());
return false;
console::error(L"Unrecognized command line parameter: \"%s\"", src_param.get_ptr());
return false;
}
const param_desc& desc = pParam_desc[param_index];
const uint cMaxValues = 16;
dynamic_string val_str[cMaxValues];
dynamic_wstring val_str[cMaxValues];
uint num_val_strs = 0;
if (desc.m_num_values)
{
if (desc.m_num_values)
{
CRNLIB_ASSERT(desc.m_num_values <= cMaxValues);
if ((arg_index + desc.m_num_values) > params.size())
{
console::error("Expected %u value(s) after command line parameter: \"%s\"", desc.m_num_values, src_param.get_ptr());
return false;
console::error(L"Expected %u value(s) after command line parameter: \"%s\"", desc.m_num_values, src_param.get_ptr());
return false;
}
for (uint v = 0; v < desc.m_num_values; v++)
val_str[num_val_strs++] = params[arg_index++];
}
dynamic_string_array strings;
if ((desc.m_support_listing_file) && (val_str[0].get_len() >= 2) && (val_str[0][0] == '@'))
}
dynamic_wstring_array strings;
if ((desc.m_support_listing_file) && (val_str[0].get_len() >= 2) && (val_str[0][0] == L'@'))
{
dynamic_string filename(val_str[0]);
dynamic_wstring filename(val_str[0]);
filename.right(1);
filename.unquote();
if (!load_string_file(filename.get_ptr(), strings))
{
console::error("Failed loading listing file \"%s\"!", filename.get_ptr());
console::error(L"Failed loading listing file \"%s\"!", filename.get_ptr());
return false;
}
}
@@ -225,7 +196,7 @@ namespace crnlib
strings.push_back(val_str[v]);
}
}
param_value pv;
pv.m_values.swap(strings);
pv.m_index = cur_arg_index;
@@ -238,18 +209,18 @@ namespace crnlib
pv.m_values.push_back(src_param);
pv.m_values.back().unquote();
pv.m_index = cur_arg_index;
m_param_map.insert(std::make_pair(g_empty_dynamic_string, pv));
m_param_map.insert(std::make_pair(g_empty_dynamic_wstring, pv));
}
}
return true;
}
bool command_line_params::parse(const char* pCmd_line, uint n, const param_desc* pParam_desc, bool skip_first_param)
bool command_line_params::parse(const wchar_t* pCmd_line, uint n, const param_desc* pParam_desc, bool skip_first_param)
{
CRNLIB_ASSERT(n && pParam_desc);
dynamic_string_array p;
dynamic_wstring_array p;
if (!split_params(pCmd_line, p))
return 0;
@@ -261,114 +232,110 @@ namespace crnlib
return parse(p, n, pParam_desc);
}
bool command_line_params::is_param(uint index) const
{
CRNLIB_ASSERT(index < m_params.size());
if (index >= m_params.size())
return false;
const dynamic_string& w = m_params[index];
const dynamic_wstring& w = m_params[index];
if (w.is_empty())
return false;
#if CRNLIB_CMD_LINE_ALLOW_SLASH_PARAMS
return (w.get_len() >= 2) && ((w[0] == '-') || (w[0] == '/'));
#else
return (w.get_len() >= 2) && (w[0] == '-');
#endif
return (w.get_len() >= 2) && ((w[0] == L'-') || (w[0] == L'/'));
}
uint command_line_params::find(uint num_keys, const char** ppKeys, crnlib::vector<param_map_const_iterator>* pIterators, crnlib::vector<uint>* pUnmatched_indices) const
uint command_line_params::find(uint num_keys, const wchar_t** ppKeys, crnlib::vector<param_map_const_iterator>* pIterators, crnlib::vector<uint>* pUnmatched_indices) const
{
CRNLIB_ASSERT(ppKeys);
if (pUnmatched_indices)
{
pUnmatched_indices->resize(m_params.size());
for (uint i = 0; i < m_params.size(); i++)
(*pUnmatched_indices)[i] = i;
}
uint n = 0;
for (uint i = 0; i < num_keys; i++)
{
const char* pKey = ppKeys[i];
const wchar_t* pKey = ppKeys[i];
param_map_const_iterator begin, end;
find(pKey, begin, end);
while (begin != end)
{
if (pIterators)
if (pIterators)
pIterators->push_back(begin);
if (pUnmatched_indices)
{
int k = pUnmatched_indices->find(begin->second.m_index);
if (k >= 0)
pUnmatched_indices->erase_unordered(k);
}
n++;
begin++;
}
}
return n;
}
void command_line_params::find(const char* pKey, param_map_const_iterator& begin, param_map_const_iterator& end) const
void command_line_params::find(const wchar_t* pKey, param_map_const_iterator& begin, param_map_const_iterator& end) const
{
dynamic_string key(pKey);
dynamic_wstring key(pKey);
begin = m_param_map.lower_bound(key);
end = m_param_map.upper_bound(key);
}
uint command_line_params::get_count(const char* pKey) const
uint command_line_params::get_count(const wchar_t* pKey) const
{
param_map_const_iterator begin, end;
find(pKey, begin, end);
uint n = 0;
while (begin != end)
{
n++;
begin++;
}
return n;
}
command_line_params::param_map_const_iterator command_line_params::get_param(const char* pKey, uint index) const
command_line_params::param_map_const_iterator command_line_params::get_param(const wchar_t* pKey, uint index) const
{
param_map_const_iterator begin, end;
find(pKey, begin, end);
if (begin == end)
return m_param_map.end();
uint n = 0;
while ((begin != end) && (n != index))
{
n++;
begin++;
}
if (begin == end)
return m_param_map.end();
return begin;
}
bool command_line_params::has_value(const char* pKey, uint index) const
bool command_line_params::has_value(const wchar_t* pKey, uint index) const
{
return get_num_values(pKey, index) != 0;
}
uint command_line_params::get_num_values(const char* pKey, uint index) const
uint command_line_params::get_num_values(const wchar_t* pKey, uint index) const
{
param_map_const_iterator it = get_param(pKey, index);
@@ -377,76 +344,76 @@ namespace crnlib
return it->second.m_values.size();
}
bool command_line_params::get_value_as_bool(const char* pKey, uint index, bool def) const
bool command_line_params::get_value_as_bool(const wchar_t* pKey, uint index, bool def) const
{
param_map_const_iterator it = get_param(pKey, index);
if (it == end())
return def;
if (it->second.m_modifier)
return it->second.m_modifier > 0;
else
return true;
}
int command_line_params::get_value_as_int(const char* pKey, uint index, int def, int l, int h, uint value_index) const
int command_line_params::get_value_as_int(const wchar_t* pKey, uint index, int def, int l, int h, uint value_index) const
{
param_map_const_iterator it = get_param(pKey, index);
if ((it == end()) || (value_index >= it->second.m_values.size()))
return def;
int val;
const char* p = it->second.m_values[value_index].get_ptr();
const wchar_t* p = it->second.m_values[value_index].get_ptr();
if (!string_to_int(p, val))
{
crnlib::console::warning("Invalid value specified for parameter \"%s\", using default value of %i", pKey, def);
crnlib::console::warning(L"Invalid value specified for parameter \"%s\", using default value of %i", pKey, def);
return def;
}
if (val < l)
{
crnlib::console::warning("Value %i for parameter \"%s\" is out of range, clamping to %i", val, pKey, l);
crnlib::console::warning(L"Value %i for parameter \"%s\" is out of range, clamping to %i", val, pKey, l);
val = l;
}
else if (val > h)
{
crnlib::console::warning("Value %i for parameter \"%s\" is out of range, clamping to %i", val, pKey, h);
crnlib::console::warning(L"Value %i for parameter \"%s\" is out of range, clamping to %i", val, pKey, h);
val = h;
}
return val;
}
float command_line_params::get_value_as_float(const char* pKey, uint index, float def, float l, float h, uint value_index) const
float command_line_params::get_value_as_float(const wchar_t* pKey, uint index, float def, float l, float h, uint value_index) const
{
param_map_const_iterator it = get_param(pKey, index);
if ((it == end()) || (value_index >= it->second.m_values.size()))
return def;
float val;
const char* p = it->second.m_values[value_index].get_ptr();
const wchar_t* p = it->second.m_values[value_index].get_ptr();
if (!string_to_float(p, val))
{
crnlib::console::warning("Invalid value specified for float parameter \"%s\", using default value of %f", pKey, def);
crnlib::console::warning(L"Invalid value specified for float parameter \"%s\", using default value of %f", pKey, def);
return def;
}
if (val < l)
{
crnlib::console::warning("Value %f for parameter \"%s\" is out of range, clamping to %f", val, pKey, l);
crnlib::console::warning(L"Value %f for parameter \"%s\" is out of range, clamping to %f", val, pKey, l);
val = l;
}
else if (val > h)
{
crnlib::console::warning("Value %f for parameter \"%s\" is out of range, clamping to %f", val, pKey, h);
crnlib::console::warning(L"Value %f for parameter \"%s\" is out of range, clamping to %f", val, pKey, h);
val = h;
}
return val;
}
bool command_line_params::get_value_as_string(const char* pKey, uint index, dynamic_string& value, uint value_index) const
bool command_line_params::get_value_as_string(const wchar_t* pKey, uint index, dynamic_wstring& value, uint value_index) const
{
param_map_const_iterator it = get_param(pKey, index);
if ((it == end()) || (value_index >= it->second.m_values.size()))
@@ -458,13 +425,13 @@ namespace crnlib
value = it->second.m_values[value_index];
return true;
}
const dynamic_string& command_line_params::get_value_as_string_or_empty(const char* pKey, uint index, uint value_index) const
const dynamic_wstring& command_line_params::get_value_as_string_or_empty(const wchar_t* pKey, uint index, uint value_index) const
{
param_map_const_iterator it = get_param(pKey, index);
if ((it == end()) || (value_index >= it->second.m_values.size()))
return g_empty_dynamic_string;
return g_empty_dynamic_wstring;
return it->second.m_values[value_index];
}
+47 -51
View File
@@ -6,81 +6,77 @@
namespace crnlib
{
// Returns the command line passed to the app as a string.
// On systems where this isn't trivial, this function combines together the separate arguments, quoting and adding spaces as needed.
void get_command_line_as_single_string(dynamic_string& cmd_line, int argc, char *argv[]);
class command_line_params
{
public:
struct param_value
{
inline param_value() : m_index(0), m_modifier(0) { }
dynamic_string_array m_values;
param_value() : m_index(0), m_modifier(0) { }
dynamic_wstring_array m_values;
uint m_index;
int8 m_modifier;
};
typedef std::multimap<dynamic_string, param_value> param_map;
typedef std::multimap<dynamic_wstring, param_value> param_map;
typedef param_map::const_iterator param_map_const_iterator;
typedef param_map::iterator param_map_iterator;
command_line_params();
void clear();
static bool split_params(const char* p, dynamic_string_array& params);
static bool split_params(const wchar_t* p, dynamic_wstring_array& params);
struct param_desc
{
const char* m_pName;
const wchar_t* m_pName;
uint m_num_values;
bool m_support_listing_file;
};
bool parse(const dynamic_string_array& params, uint n, const param_desc* pParam_desc);
bool parse(const char* pCmd_line, uint n, const param_desc* pParam_desc, bool skip_first_param = true);
const dynamic_string_array& get_array() const { return m_params; }
bool parse(const dynamic_wstring_array& params, uint n, const param_desc* pParam_desc);
bool parse(const wchar_t* pCmd_line, uint n, const param_desc* pParam_desc, bool skip_first_param = true);
const dynamic_wstring_array& get_array() const { return m_params; }
bool is_param(uint index) const;
const param_map& get_map() const { return m_param_map; }
uint get_num_params() const { return static_cast<uint>(m_param_map.size()); }
param_map_const_iterator begin() const { return m_param_map.begin(); }
param_map_const_iterator end() const { return m_param_map.end(); }
uint find(uint num_keys, const char** ppKeys, crnlib::vector<param_map_const_iterator>* pIterators, crnlib::vector<uint>* pUnmatched_indices) const;
void find(const char* pKey, param_map_const_iterator& begin, param_map_const_iterator& end) const;
uint get_count(const char* pKey) const;
uint find(uint num_keys, const wchar_t** ppKeys, crnlib::vector<param_map_const_iterator>* pIterators, crnlib::vector<uint>* pUnmatched_indices) const;
void find(const wchar_t* pKey, param_map_const_iterator& begin, param_map_const_iterator& end) const;
uint get_count(const wchar_t* pKey) const;
// Returns end() if param cannot be found, or index is out of range.
param_map_const_iterator get_param(const char* pKey, uint index) const;
bool has_key(const char* pKey) const { return get_param(pKey, 0) != end(); }
bool has_value(const char* pKey, uint index) const;
uint get_num_values(const char* pKey, uint index) const;
bool get_value_as_bool(const char* pKey, uint index = 0, bool def = false) const;
int get_value_as_int(const char* pKey, uint index, int def, int l = INT_MIN, int h = INT_MAX, uint value_index = 0) const;
float get_value_as_float(const char* pKey, uint index, float def = 0.0f, float l = -math::cNearlyInfinite, float h = math::cNearlyInfinite, uint value_index = 0) const;
bool get_value_as_string(const char* pKey, uint index, dynamic_string& value, uint value_index = 0) const;
const dynamic_string& get_value_as_string_or_empty(const char* pKey, uint index = 0, uint value_index = 0) const;
param_map_const_iterator get_param(const wchar_t* pKey, uint index) const;
bool has_key(const wchar_t* pKey) const { return get_param(pKey, 0) != end(); }
bool has_value(const wchar_t* pKey, uint index) const;
uint get_num_values(const wchar_t* pKey, uint index) const;
bool get_value_as_bool(const wchar_t* pKey, uint index = 0, bool def = false) const;
int get_value_as_int(const wchar_t* pKey, uint index, int def, int l = INT_MIN, int h = INT_MAX, uint value_index = 0) const;
float get_value_as_float(const wchar_t* pKey, uint index, float def = 0.0f, float l = -math::cNearlyInfinite, float h = math::cNearlyInfinite, uint value_index = 0) const;
bool get_value_as_string(const wchar_t* pKey, uint index, dynamic_wstring& value, uint value_index = 0) const;
const dynamic_wstring& get_value_as_string_or_empty(const wchar_t* pKey, uint index = 0, uint value_index = 0) const;
private:
dynamic_string_array m_params;
param_map m_param_map;
static bool load_string_file(const char* pFilename, dynamic_string_array& strings);
dynamic_wstring_array m_params;
param_map m_param_map;
static bool load_string_file(const wchar_t* pFilename, dynamic_wstring_array& strings);
};
} // namespace crnlib
+476 -481
View File
File diff suppressed because it is too large Load Diff
+52 -52
View File
@@ -18,30 +18,30 @@ namespace crnlib
class crn_comp : public itexture_comp
{
CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(crn_comp);
public:
crn_comp();
virtual ~crn_comp();
virtual const char *get_ext() const { return "CRN"; }
virtual const wchar_t *get_ext() const { return L"CRN"; }
virtual bool compress_init(const crn_comp_params& params);
virtual bool compress_pass(const crn_comp_params& params, float *pEffective_bitrate);
virtual void compress_deinit();
virtual const crnlib::vector<uint8>& get_comp_data() const { return m_comp_data; }
virtual crnlib::vector<uint8>& get_comp_data() { return m_comp_data; }
uint get_comp_data_size() const { return m_comp_data.size(); }
const uint8* get_comp_data_ptr() const { return m_comp_data.size() ? &m_comp_data[0] : NULL; }
private:
task_pool m_task_pool;
const crn_comp_params* m_pParams;
image_u8 m_images[cCRNMaxFaces][cCRNMaxLevels];
struct level_tag
struct
{
uint m_width, m_height;
uint m_chunk_width, m_chunk_height;
@@ -50,130 +50,130 @@ namespace crnlib
uint m_first_chunk;
uint m_group_first_chunk;
} m_levels[cCRNMaxLevels];
struct mip_group
{
mip_group() : m_first_chunk(0), m_num_chunks(0) { }
uint m_first_chunk;
uint m_num_chunks;
};
crnlib::vector<mip_group> m_mip_groups;
enum comp
enum comp
{
cColor,
cAlpha0,
cAlpha1,
cNumComps
};
bool m_has_comp[cNumComps];
struct chunk_detail
{
chunk_detail() { utils::zero_object(*this); }
uint m_first_endpoint_index;
uint m_first_selector_index;
};
typedef crnlib::vector<chunk_detail> chunk_detail_vec;
chunk_detail_vec m_chunk_details;
crnlib::vector<uint> m_endpoint_indices[cNumComps];
crnlib::vector<uint> m_selector_indices[cNumComps];
uint m_total_chunks;
dxt_hc::pixel_chunk_vec m_chunks;
crnd::crn_header m_crn_header;
crnlib::vector<uint8> m_comp_data;
dxt_hc m_hvq;
symbol_histogram m_chunk_encoding_hist;
symbol_histogram m_chunk_encoding_hist;
static_huffman_data_model m_chunk_encoding_dm;
symbol_histogram m_endpoint_index_hist[2];
static_huffman_data_model m_endpoint_index_dm[2]; // color, alpha
symbol_histogram m_selector_index_hist[2];
symbol_histogram m_selector_index_hist[2];
static_huffman_data_model m_selector_index_dm[2]; // color, alpha
crnlib::vector<uint8> m_packed_chunks[cCRNMaxLevels];
crnlib::vector<uint8> m_packed_data_models;
crnlib::vector<uint8> m_packed_color_endpoints;
crnlib::vector<uint8> m_packed_color_selectors;
crnlib::vector<uint8> m_packed_color_selectors;
crnlib::vector<uint8> m_packed_alpha_endpoints;
crnlib::vector<uint8> m_packed_alpha_selectors;
crnlib::vector<uint8> m_packed_alpha_selectors;
void clear();
void append_chunks(const image_u8& img, uint num_chunks_x, uint num_chunks_y, dxt_hc::pixel_chunk_vec& chunks, float weight);
static float color_endpoint_similarity_func(uint index_a, uint index_b, void* pContext);
static float alpha_endpoint_similarity_func(uint index_a, uint index_b, void* pContext);
void sort_color_endpoint_codebook(crnlib::vector<uint>& remapping, const crnlib::vector<uint>& endpoints);
void sort_alpha_endpoint_codebook(crnlib::vector<uint>& remapping, const crnlib::vector<uint>& endpoints);
bool pack_color_endpoints(crnlib::vector<uint8>& data, const crnlib::vector<uint>& remapping, const crnlib::vector<uint>& endpoint_indices, uint trial_index);
bool pack_alpha_endpoints(crnlib::vector<uint8>& data, const crnlib::vector<uint>& remapping, const crnlib::vector<uint>& endpoint_indices, uint trial_index);
static float color_selector_similarity_func(uint index_a, uint index_b, void* pContext);
static float alpha_selector_similarity_func(uint index_a, uint index_b, void* pContext);
void sort_selector_codebook(crnlib::vector<uint>& remapping, const crnlib::vector<dxt_hc::selectors>& selectors, const uint8* pTo_linear);
bool pack_selectors(
crnlib::vector<uint8>& packed_data,
const crnlib::vector<uint>& selector_indices,
const crnlib::vector<dxt_hc::selectors>& selectors,
const crnlib::vector<dxt_hc::selectors>& selectors,
const crnlib::vector<uint>& remapping,
uint max_selector_value,
const uint8* pTo_linear,
uint trial_index);
bool alias_images();
void create_chunks();
bool quantize_chunks();
void create_chunk_indices();
bool pack_chunks(
uint first_chunk, uint num_chunks,
bool clear_histograms,
symbol_codec* pCodec,
const crnlib::vector<uint>* pColor_endpoint_remap,
const crnlib::vector<uint>* pColor_endpoint_remap,
const crnlib::vector<uint>* pColor_selector_remap,
const crnlib::vector<uint>* pAlpha_endpoint_remap,
const crnlib::vector<uint>* pAlpha_endpoint_remap,
const crnlib::vector<uint>* pAlpha_selector_remap);
bool pack_chunks_simulation(
uint first_chunk, uint num_chunks,
uint& total_bits,
const crnlib::vector<uint>* pColor_endpoint_remap,
const crnlib::vector<uint>* pColor_endpoint_remap,
const crnlib::vector<uint>* pColor_selector_remap,
const crnlib::vector<uint>* pAlpha_endpoint_remap,
const crnlib::vector<uint>* pAlpha_endpoint_remap,
const crnlib::vector<uint>* pAlpha_selector_remap);
void optimize_color_endpoint_codebook_task(uint64 data, void* pData_ptr);
bool optimize_color_endpoint_codebook(crnlib::vector<uint>& remapping);
void optimize_color_selector_codebook_task(uint64 data, void* pData_ptr);
bool optimize_color_selector_codebook(crnlib::vector<uint>& remapping);
void optimize_alpha_endpoint_codebook_task(uint64 data, void* pData_ptr);
bool optimize_alpha_endpoint_codebook(crnlib::vector<uint>& remapping);
void optimize_alpha_selector_codebook_task(uint64 data, void* pData_ptr);
bool optimize_alpha_selector_codebook(crnlib::vector<uint>& remapping);
bool create_comp_data();
bool pack_data_models();
bool update_progress(uint phase_index, uint subphase_index, uint subphase_total);
bool compress_internal();
static void append_vec(crnlib::vector<uint8>& a, const void* p, uint size);
static void append_vec(crnlib::vector<uint8>& a, const crnlib::vector<uint8>& b);
};
+431
View File
@@ -0,0 +1,431 @@
// File: crn_condition_var.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_condition_var.h"
#include "crn_spinlock.h"
#include "crn_winhdr.h"
namespace crnlib
{
void spinlock::lock(uint32 max_spins, bool yielding, bool memoryBarrier)
{
if (g_number_of_processors <= 1)
max_spins = 1;
uint32 spinCount = 0;
uint32 yieldCount = 0;
for ( ; ; )
{
CRNLIB_ASSUME(sizeof(long) == sizeof(int32));
if (!InterlockedExchange((volatile long*)&m_flag, TRUE))
break;
YieldProcessor();
YieldProcessor();
YieldProcessor();
YieldProcessor();
YieldProcessor();
YieldProcessor();
YieldProcessor();
YieldProcessor();
spinCount++;
if ((yielding) && (spinCount >= max_spins))
{
switch (yieldCount)
{
case 0:
{
spinCount = 0;
Sleep(0);
yieldCount++;
break;
}
case 1:
{
if (g_number_of_processors <= 1)
spinCount = 0;
else
spinCount = max_spins / 2;
Sleep(1);
yieldCount++;
break;
}
case 2:
{
if (g_number_of_processors <= 1)
spinCount = 0;
else
spinCount = max_spins;
Sleep(2);
break;
}
}
}
}
if (memoryBarrier)
{
#ifdef _MSC_VER
MemoryBarrier();
#elif defined(__MINGW32__) && defined(__MINGW64__)
__sync_synchronize();
#endif
}
}
void spinlock::unlock()
{
#ifdef _MSC_VER
MemoryBarrier();
#elif defined(__MINGW32__) && defined(__MINGW64__)
__sync_synchronize();
#endif
m_flag = FALSE;
}
mutex::mutex(unsigned int spin_count)
{
CRNLIB_ASSUME(sizeof(mutex) >= sizeof(CRITICAL_SECTION));
void *p = m_buf;
CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
BOOL status = true;
#ifdef _XBOX
InitializeCriticalSectionAndSpinCount(&m_cs, spin_count);
#else
status = InitializeCriticalSectionAndSpinCount(&m_cs, spin_count);
#endif
if (!status)
crnlib_fail("mutex::mutex: InitializeCriticalSectionAndSpinCount failed", __FILE__, __LINE__);
#ifdef CRNLIB_BUILD_DEBUG
m_lock_count = 0;
#endif
}
mutex::~mutex()
{
void *p = m_buf;
CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
#ifdef CRNLIB_BUILD_DEBUG
if (m_lock_count)
crnlib_assert("mutex::~mutex: mutex is still locked", __FILE__, __LINE__);
#endif
DeleteCriticalSection(&m_cs);
}
void mutex::lock()
{
void *p = m_buf;
CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
EnterCriticalSection(&m_cs);
#ifdef CRNLIB_BUILD_DEBUG
m_lock_count++;
#endif
}
void mutex::unlock()
{
void *p = m_buf;
CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
#ifdef CRNLIB_BUILD_DEBUG
if (!m_lock_count)
crnlib_assert("mutex::unlock: mutex is not locked", __FILE__, __LINE__);
m_lock_count--;
#endif
LeaveCriticalSection(&m_cs);
}
void mutex::set_spin_count(unsigned int count)
{
void *p = m_buf;
CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
SetCriticalSectionSpinCount(&m_cs, count);
}
semaphore::semaphore(int32 initialCount, int32 maximumCount, const char* pName)
{
m_handle = CreateSemaphoreA(NULL, initialCount, maximumCount, pName);
if (NULL == m_handle)
{
CRNLIB_FAIL("semaphore: CreateSemaphore() failed");
}
}
semaphore::~semaphore()
{
if (m_handle)
{
CloseHandle(m_handle);
m_handle = NULL;
}
}
void semaphore::release(int32 releaseCount, int32 *pPreviousCount)
{
CRNLIB_ASSUME(sizeof(LONG) == sizeof(int32));
if (0 == ReleaseSemaphore(m_handle, releaseCount, (LPLONG)pPreviousCount))
{
CRNLIB_FAIL("semaphore: ReleaseSemaphore() failed");
}
}
bool semaphore::wait(uint32 milliseconds)
{
uint32 result = WaitForSingleObject(m_handle, milliseconds);
if (WAIT_FAILED == result)
{
CRNLIB_FAIL("semaphore: WaitForSingleObject() failed");
}
return WAIT_OBJECT_0 == result;
}
event::event(bool manual_reset, bool initial_state, const char* pName)
{
m_handle = CreateEventA(NULL, manual_reset, initial_state, pName);
if (NULL == m_handle)
CRNLIB_FAIL("event: CreateEvent() failed");
}
event::~event()
{
if (m_handle)
{
CloseHandle(m_handle);
m_handle = NULL;
}
}
void event::set(void)
{
SetEvent(m_handle);
}
void event::reset(void)
{
ResetEvent(m_handle);
}
void event::pulse(void)
{
PulseEvent(m_handle);
}
bool event::wait(uint32 milliseconds)
{
uint32 result = WaitForSingleObject(m_handle, milliseconds);
if (result == WAIT_FAILED)
{
CRNLIB_FAIL("event: WaitForSingleObject() failed");
}
return (result == WAIT_OBJECT_0);
}
condition_var::condition_var(uint spin_count) :
m_condition_var_lock(1, 1),
m_tls(TlsAlloc()),
m_cur_age(0),
m_max_waiter_array_index(-1)
{
CRNLIB_ASSERT(TLS_OUT_OF_INDEXES != m_tls);
m_waiters_array_lock.set_spin_count(spin_count);
m_waiters_array_lock.lock();
for (uint i = 0; i < cMaxWaitingThreads; i++)
m_waiters[i].clear();
m_waiters_array_lock.unlock();
}
condition_var::~condition_var()
{
TlsFree(m_tls);
}
void condition_var::lock()
{
uint32 cur_count = get_cur_lock_count();
CRNLIB_ASSERT(cur_count != 0xFFFFFFFF);
cur_count++;
set_cur_lock_count(cur_count);
if (1 == cur_count)
m_condition_var_lock.wait();
}
void condition_var::unlock()
{
uint32 cur_count = get_cur_lock_count();
CRNLIB_ASSERT(cur_count);
cur_count--;
set_cur_lock_count(cur_count);
if (!cur_count)
leave_and_scan();
}
void condition_var::leave_and_scan(int index_to_ignore)
{
m_waiters_array_lock.lock();
uint best_age = 0;
int best_index = -1;
for (int i = 0; i <= m_max_waiter_array_index; i++)
{
waiting_thread& waiter = m_waiters[i];
if ((i != index_to_ignore) && (waiter.m_occupied) && (!waiter.m_satisfied))
{
uint age = m_cur_age - waiter.m_age;
if ((age > best_age) || (best_index < 0))
{
if ((!waiter.m_callback_func) || (waiter.m_callback_func(waiter.m_pCallback_ptr, waiter.m_callback_data)))
{
best_age = age;
best_index = i;
}
}
}
}
if (best_index >= 0)
{
waiting_thread& waiter = m_waiters[best_index];
waiter.m_satisfied = true;
waiter.m_event.set();
m_waiters_array_lock.unlock();
}
else
{
m_waiters_array_lock.unlock();
m_condition_var_lock.release();
}
}
uint32 condition_var::get_cur_lock_count() const
{
return (uint32)((intptr_t)TlsGetValue(m_tls));
}
int condition_var::wait(
pCondition_func pCallback, void* pCallback_data_ptr, uint64 callback_data,
uint num_wait_handles, const void** pWait_handles, uint32 max_time_to_wait)
{
CRNLIB_ASSERT(get_cur_lock_count());
// First, see if the calling thread's condition function is satisfied. If so, there's no need to wait.
if ((pCallback) && (pCallback(pCallback_data_ptr, callback_data)))
return 0;
// Add this thread to the list of waiters.
m_waiters_array_lock.lock();
uint i;
for (i = 0; i < cMaxWaitingThreads; i++)
if (!m_waiters[i].m_occupied)
break;
CRNLIB_VERIFY(i != cMaxWaitingThreads);
m_max_waiter_array_index = math::maximum<int>(m_max_waiter_array_index, i);
waiting_thread& waiter = m_waiters[i];
waiter.m_callback_func = pCallback;
waiter.m_pCallback_ptr = pCallback_data_ptr;
waiter.m_callback_data = callback_data;
waiter.m_satisfied = false;
waiter.m_occupied = true;
waiter.m_age = m_cur_age++;
waiter.m_event.reset();
m_waiters_array_lock.unlock();
// Now leave the condition_var and scan to see if there are any satisfied waiters.
leave_and_scan(i);
// Let's wait for this thread's condition to be satisfied, or until timeout, or until one of the user supplied handles is signaled.
int return_index = 0;
const uint cMaxWaitHandles = 64;
CRNLIB_ASSERT(num_wait_handles < cMaxWaitHandles);
HANDLE handles[cMaxWaitHandles];
handles[0] = waiter.m_event.get_handle();
uint total_handles = 1;
if (num_wait_handles)
{
CRNLIB_ASSERT(pWait_handles);
memcpy(handles + total_handles, pWait_handles, sizeof(HANDLE) * num_wait_handles);
total_handles += num_wait_handles;
}
uint32 result;
if (max_time_to_wait == UINT32_MAX)
{
do
{
result = WaitForMultipleObjects(total_handles, handles, FALSE, 2000);
} while (result == WAIT_TIMEOUT);
}
else
result = WaitForMultipleObjects(total_handles, handles, FALSE, max_time_to_wait);
if ((result == WAIT_ABANDONED) || (result == WAIT_TIMEOUT))
return_index = -1;
else
return_index = result - WAIT_OBJECT_0;
// See if our condition was satisfied, and remove this thread from the waiter list.
m_waiters_array_lock.lock();
const bool was_satisfied = waiter.m_satisfied;
waiter.m_occupied = false;
m_waiters_array_lock.unlock();
if (0 == return_index)
{
CRNLIB_ASSERT(was_satisfied);
}
else
{
// Enter the condition_var if a user supplied handle was signaled. This guarantees that on exit of this function we're still inside the condition_var, no matter
// what happened during the WaitForMultipleObjects() call.
if (!was_satisfied)
m_condition_var_lock.wait();
}
return return_index;
}
void condition_var::set_cur_lock_count(uint32 newCount)
{
TlsSetValue(m_tls, (void*)newCount);
}
} // namespace crnlib
+91
View File
@@ -0,0 +1,91 @@
// File: crn_condition_var.h
// See Copyright Notice and license at the end of inc/crnlib.h
// Inspired by the "monitor" class in "Win32 Multithreaded Programming" by Cohen and Woodring.
// Also see http://en.wikipedia.org/wiki/Monitor_(synchronization)
#pragma once
#include "crn_mutex.h"
#include "crn_event.h"
#include "crn_semaphore.h"
namespace crnlib
{
class condition_var
{
CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(condition_var);
public:
condition_var(uint spin_count = 4096U);
~condition_var();
// Locks the condition_var.
// Recursive locks are supported.
void lock();
// Returns TRUE if the thread owning this condition function should stop waiting.
// This function will always be called from within the condition_var, but it may be called from several different threads!
typedef bool (*pCondition_func)(void* pCallback_data_ptr, uint64 callback_data);
// Temporarily leaves the lock and waits for a condition to be satisfied.
// If pCallback is NULL, this method will return after another thread enters and exits the lock (like a Vista-style condition variable).
// Otherwise, this method will only return when the specified condition function returns TRUE when another thread exits the lock.
// When this method returns, the calling thread will be inside the lock.
// Returns -1 on timeout or error, 0 if the wait was satisfied, or 1 or higher if one of the extra wait handles became signaled.
// It is highly recommended you use a non-null condition callback. If you don't be sure to check for race conditions!
int wait(pCondition_func pCallback = NULL, void* pCallback_data_ptr = NULL, uint64 callback_data = 0,
uint num_wait_handles = 0, const void** pWait_handles = NULL, uint32 max_time_to_wait = UINT32_MAX);
// Unlocks the condition_var. Another thread may be woken up if its condition function has become satisfied.
void unlock();
uint32 get_cur_lock_count() const;
private:
enum { cMaxWaitingThreads = 16, cMaxWaitingThreadsMask = cMaxWaitingThreads - 1 };
semaphore m_condition_var_lock;
mutex m_waiters_array_lock;
uint32 m_tls;
uint m_cur_age;
struct waiting_thread
{
uint64 m_callback_data;
void* m_pCallback_ptr;
pCondition_func m_callback_func;
uint m_age;
bool m_satisfied;
bool m_occupied;
event m_event;
void clear()
{
m_callback_data = 0;
m_pCallback_ptr = NULL;
m_callback_func = NULL;
m_age = 0;
m_satisfied = false;
m_occupied = false;
}
};
waiting_thread m_waiters[cMaxWaitingThreads];
int m_max_waiter_array_index;
void set_cur_lock_count(uint32 newCount);
void leave_and_scan(int index_to_ignore = -1);
};
class scoped_condition_var
{
CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(scoped_condition_var);
public:
inline scoped_condition_var(condition_var& m) : m_condition_var(m) { m_condition_var.lock(); }
inline ~scoped_condition_var() { m_condition_var.unlock(); }
private:
condition_var& m_condition_var;
};
} // namespace crnlib
+36 -27
View File
@@ -3,7 +3,6 @@
#include "crn_core.h"
#include "crn_console.h"
#include "crn_data_stream.h"
#include "crn_threading.h"
namespace crnlib
{
@@ -15,7 +14,6 @@ namespace crnlib
data_stream* console::m_pLog_stream;
mutex* console::m_pMutex;
uint console::m_num_messages[cCMTTotal];
bool console::m_at_beginning_of_line = true;
const uint cConsoleBufSize = 4096;
@@ -50,7 +48,7 @@ namespace crnlib
m_crlf = true;
}
void console::vprintf(eConsoleMessageType type, const char* p, va_list args)
void console::vprintf(eConsoleMessageType type, const wchar_t* p, va_list args)
{
init();
@@ -58,8 +56,12 @@ namespace crnlib
m_num_messages[type]++;
char buf[cConsoleBufSize];
vsprintf_s(buf, cConsoleBufSize, p, args);
wchar_t buf[cConsoleBufSize];
#ifdef _MSC_VER
vswprintf_s(buf, cConsoleBufSize, p, args);
#else
vswprintf(buf, p, args);
#endif
bool handled = false;
@@ -70,41 +72,48 @@ namespace crnlib
handled = true;
}
const char* pPrefix = NULL;
if ((m_prefixes) && (m_at_beginning_of_line))
const wchar_t* pPrefix = NULL;
if (m_prefixes)
{
switch (type)
{
case cDebugConsoleMessage: pPrefix = "Debug: "; break;
case cWarningConsoleMessage: pPrefix = "Warning: "; break;
case cErrorConsoleMessage: pPrefix = "Error: "; break;
case cDebugConsoleMessage: pPrefix = L"Debug: "; break;
case cWarningConsoleMessage: pPrefix = L"Warning: "; break;
case cErrorConsoleMessage: pPrefix = L"Error: "; break;
default: break;
}
}
if ((!m_output_disabled) && (!handled))
{
#ifdef _XBOX
if (pPrefix)
::printf("%s", pPrefix);
::printf(m_crlf ? "%s\n" : "%s", buf);
OutputDebugStringW(pPrefix);
OutputDebugStringW(buf);
if (m_crlf)
OutputDebugStringW(L"\n");
#else
if (pPrefix)
::wprintf(pPrefix);
::wprintf(m_crlf ? L"%s\n" : L"%s", buf);
#endif
}
uint n = strlen(buf);
m_at_beginning_of_line = (m_crlf) || ((n) && (buf[n - 1] == '\n'));
if ((type != cProgressConsoleMessage) && (m_pLog_stream))
{
// Yes this is bad.
dynamic_string tmp_buf(buf);
dynamic_wstring utf16_buf(buf);
tmp_buf.translate_lf_to_crlf();
dynamic_string ansi_buf;
utf16_buf.as_ansi(ansi_buf);
ansi_buf.translate_lf_to_crlf();
m_pLog_stream->printf(m_crlf ? "%s\r\n" : "%s", tmp_buf.get_ptr());
m_pLog_stream->printf(m_crlf ? "%s\r\n" : "%s", ansi_buf.get_ptr());
m_pLog_stream->flush();
}
}
void console::printf(eConsoleMessageType type, const char* p, ...)
void console::printf(eConsoleMessageType type, const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
@@ -112,7 +121,7 @@ namespace crnlib
va_end(args);
}
void console::printf(const char* p, ...)
void console::printf(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
@@ -163,7 +172,7 @@ namespace crnlib
}
}
void console::progress(const char* p, ...)
void console::progress(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
@@ -171,7 +180,7 @@ namespace crnlib
va_end(args);
}
void console::info(const char* p, ...)
void console::info(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
@@ -179,7 +188,7 @@ namespace crnlib
va_end(args);
}
void console::message(const char* p, ...)
void console::message(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
@@ -187,7 +196,7 @@ namespace crnlib
va_end(args);
}
void console::cons(const char* p, ...)
void console::cons(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
@@ -195,7 +204,7 @@ namespace crnlib
va_end(args);
}
void console::debug(const char* p, ...)
void console::debug(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
@@ -203,7 +212,7 @@ namespace crnlib
va_end(args);
}
void console::warning(const char* p, ...)
void console::warning(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
@@ -211,7 +220,7 @@ namespace crnlib
va_end(args);
}
void console::error(const char* p, ...)
void console::error(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
+34 -68
View File
@@ -3,126 +3,92 @@
#pragma once
#include "crn_dynamic_string.h"
#ifdef WIN32
#include <tchar.h>
#include <conio.h>
#endif
namespace crnlib
{
class dynamic_string;
class data_stream;
class mutex;
enum eConsoleMessageType
{
cDebugConsoleMessage, // debugging messages
cProgressConsoleMessage, // progress messages
cInfoConsoleMessage, // ordinary messages
cInfoConsoleMessage, // ordinary messages
cConsoleConsoleMessage, // user console output
cMessageConsoleMessage, // high importance messages
cWarningConsoleMessage, // warnings
cErrorConsoleMessage, // errors
cCMTTotal
};
typedef bool (*console_output_func)(eConsoleMessageType type, const char* pMsg, void* pData);
class console
typedef bool (*console_output_func)(eConsoleMessageType type, const wchar_t* pMsg, void* pData);
class console
{
public:
static void init();
static void deinit();
static bool is_initialized() { return m_pMutex != NULL; }
static void set_default_category(eConsoleMessageType category);
static eConsoleMessageType get_default_category();
static void add_console_output_func(console_output_func pFunc, void* pData);
static void remove_console_output_func(console_output_func pFunc);
static void printf(const char* p, ...);
static void vprintf(eConsoleMessageType type, const char* p, va_list args);
static void printf(eConsoleMessageType type, const char* p, ...);
static void cons(const char* p, ...);
static void debug(const char* p, ...);
static void progress(const char* p, ...);
static void info(const char* p, ...);
static void message(const char* p, ...);
static void warning(const char* p, ...);
static void error(const char* p, ...);
static void printf(const wchar_t* p, ...);
static void vprintf(eConsoleMessageType type, const wchar_t* p, va_list args);
static void printf(eConsoleMessageType type, const wchar_t* p, ...);
static void cons(const wchar_t* p, ...);
static void debug(const wchar_t* p, ...);
static void progress(const wchar_t* p, ...);
static void info(const wchar_t* p, ...);
static void message(const wchar_t* p, ...);
static void warning(const wchar_t* p, ...);
static void error(const wchar_t* p, ...);
// FIXME: All console state is currently global!
static void disable_prefixes();
static void enable_prefixes();
static bool get_prefixes() { return m_prefixes; }
static bool get_at_beginning_of_line() { return m_at_beginning_of_line; }
static void disable_crlf();
static void enable_crlf();
static bool get_crlf() { return m_crlf; }
static void disable_output() { m_output_disabled = true; }
static void enable_output() { m_output_disabled = false; }
static bool get_output_disabled() { return m_output_disabled; }
static void set_log_stream(data_stream* pStream) { m_pLog_stream = pStream; }
static data_stream* get_log_stream() { return m_pLog_stream; }
static uint get_num_messages(eConsoleMessageType type) { return m_num_messages[type]; }
private:
private:
static eConsoleMessageType m_default_category;
struct console_func
struct console_func
{
console_func(console_output_func func = NULL, void* pData = NULL) : m_func(func), m_pData(pData) { }
console_output_func m_func;
void* m_pData;
};
static crnlib::vector<console_func> m_output_funcs;
static bool m_crlf, m_prefixes, m_output_disabled;
static data_stream* m_pLog_stream;
static mutex* m_pMutex;
static uint m_num_messages[cCMTTotal];
static bool m_at_beginning_of_line;
};
#if defined(WIN32)
inline int crn_getch()
{
return _getch();
}
#elif defined(__GNUC__)
#include <termios.h>
#include <unistd.h>
inline int crn_getch()
{
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
#else
inline int crn_getch()
{
printf("crn_getch: Unimplemented");
return 0;
}
#endif
} // namespace crnlib
+2 -9
View File
@@ -1,14 +1,7 @@
// File: crn_core.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif
namespace crnlib
{
const char *g_copyright_str = "Copyright (c) 2010-2012 Rich Geldreich and Tenacious Software LLC";
const char *g_sig_str = "C8cfRlaorj0wLtnMSxrBJxTC85rho2L9hUZKHcBL";
} // namespace crnlib
char *g_copyright_str = "Copyright (c) 2010-2011 Tenacious Software LLC";
char *g_sig_str = "C8cfRlaorj0wLtnMSxrBJxTC85rho2L9hUZKHcBL";
+30 -105
View File
@@ -6,34 +6,38 @@
#pragma warning (disable: 4201) // nonstandard extension used : nameless struct/union
#pragma warning (disable: 4127) // conditional expression is constant
#pragma warning (disable: 4793) // function compiled as native
#pragma warning (disable: 4324) // structure was padded due to __declspec(align())
#endif
#if defined(WIN32) && !defined(CRNLIB_ANSI_CPLUSPLUS)
// MSVC or MinGW, x86 or x64, Win32 API's for threading and Win32 Interlocked API's or GCC built-ins for atomic ops.
#if defined(WIN32)
#if 0
#ifdef NDEBUG
// Ensure checked iterators are disabled. Note: Be sure anything else that links against this lib also #define's this stuff, or remove this crap!
// Ensure checked iterators are disabled.
#define _SECURE_SCL 0
#define _HAS_ITERATOR_DEBUGGING 0
#endif
#ifndef _DLL
// If we're using the DLL form of the run-time libs, we're also going to be enabling exceptions because we'll be building CLR apps.
// Otherwise, we disable exceptions for a small speed boost.
// Otherwise, we disable exceptions for a small (up to 5%) speed boost.
#define _HAS_EXCEPTIONS 0
#endif
#endif
//#define _CRT_SECURE_NO_WARNINGS
#define NOMINMAX
#define CRNLIB_PLATFORM_PC 1
#ifdef _WIN64
#define CRNLIB_PLATFORM_PC_X64 1
#else
#define CRNLIB_PLATFORM_PC_X86 1
#endif
#define CRNLIB_USE_WIN32_API 1
#if defined(__MINGW32__) || defined(__MINGW64__)
#define CRNLIB_USE_GCC_ATOMIC_BUILTINS 1
#else
#define CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS 1
#endif
#define CRNLIB_PLATFORM_PC 1
#if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
#ifdef _WIN64
#define CRNLIB_PLATFORM_PC_X64 1
#define CRNLIB_64BIT_POINTERS 1
#define CRNLIB_CPU_HAS_64BIT_REGISTERS 1
@@ -44,99 +48,15 @@
#define CRNLIB_CPU_HAS_64BIT_REGISTERS 0
#define CRNLIB_LITTLE_ENDIAN_CPU 1
#endif
#define CRNLIB_USE_UNALIGNED_INT_LOADS 1
#define CRNLIB_RESTRICT __restrict
#define CRNLIB_FORCE_INLINE __forceinline
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
#define CRNLIB_USE_MSVC_INTRINSICS 1
#endif
#define CRNLIB_INT64_FORMAT_SPECIFIER "%I64i"
#define CRNLIB_UINT64_FORMAT_SPECIFIER "%I64u"
#define CRNLIB_STDCALL __stdcall
#define CRNLIB_MEMORY_IMPORT_BARRIER
#define CRNLIB_MEMORY_EXPORT_BARRIER
#elif defined(__GNUC__) && !defined(CRNLIB_ANSI_CPLUSPLUS)
// GCC x86 or x64, pthreads for threading and GCC built-ins for atomic ops.
#define CRNLIB_PLATFORM_PC 1
#if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
#define CRNLIB_PLATFORM_PC_X64 1
#define CRNLIB_64BIT_POINTERS 1
#define CRNLIB_CPU_HAS_64BIT_REGISTERS 1
#else
#define CRNLIB_PLATFORM_PC_X86 1
#define CRNLIB_64BIT_POINTERS 0
#define CRNLIB_CPU_HAS_64BIT_REGISTERS 0
#endif
#define CRNLIB_USE_UNALIGNED_INT_LOADS 1
#define CRNLIB_LITTLE_ENDIAN_CPU 1
#define CRNLIB_USE_PTHREADS_API 1
#define CRNLIB_USE_GCC_ATOMIC_BUILTINS 1
#define CRNLIB_RESTRICT
#define CRNLIB_FORCE_INLINE inline __attribute__((__always_inline__,__gnu_inline__))
#define CRNLIB_INT64_FORMAT_SPECIFIER "%lli"
#define CRNLIB_UINT64_FORMAT_SPECIFIER "%llu"
#define CRNLIB_STDCALL
#define CRNLIB_MEMORY_IMPORT_BARRIER
#define CRNLIB_MEMORY_EXPORT_BARRIER
#else
// Vanilla ANSI-C/C++
// No threading support, unaligned loads are NOT okay.
#if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
#define CRNLIB_64BIT_POINTERS 1
#define CRNLIB_CPU_HAS_64BIT_REGISTERS 1
#else
#define CRNLIB_64BIT_POINTERS 0
#define CRNLIB_CPU_HAS_64BIT_REGISTERS 0
#endif
#define CRNLIB_USE_UNALIGNED_INT_LOADS 0
#if __BIG_ENDIAN__
#define CRNLIB_BIG_ENDIAN_CPU 1
#else
#define CRNLIB_LITTLE_ENDIAN_CPU 1
#endif
#define CRNLIB_USE_GCC_ATOMIC_BUILTINS 0
#define CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS 0
#define CRNLIB_RESTRICT
#define CRNLIB_FORCE_INLINE inline
#define CRNLIB_INT64_FORMAT_SPECIFIER "%I64i"
#define CRNLIB_UINT64_FORMAT_SPECIFIER "%I64u"
#define CRNLIB_STDCALL
#define CRNLIB_MEMORY_IMPORT_BARRIER
#define CRNLIB_MEMORY_EXPORT_BARRIER
#endif
#define CRNLIB_SLOW_STRING_LEN_CHECKS 1
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
#include <algorithm>
#include <locale>
#include <memory.h>
#include <limits.h>
#include <algorithm>
#include <errno.h>
#ifdef min
#undef min
@@ -158,15 +78,16 @@
#ifndef NDEBUG
#define NDEBUG
#endif
#ifdef DEBUG
#error DEBUG cannot be defined in CRNLIB_BUILD_RELEASE
#endif
#endif
#include "crn_types.h"
#include "crn_assert.h"
#include "crn_platform.h"
#if defined(WIN32)
#include "crn_mutex.h"
#endif
#include "crn_assert.h"
#include "crn_types.h"
#include "crn_helpers.h"
#include "crn_traits.h"
#include "crn_mem.h"
@@ -174,5 +95,9 @@
#include "crn_utils.h"
#include "crn_hash.h"
#include "crn_vector.h"
#include "crn_timer.h"
#include "crn_win32_timer.h"
#include "crn_win32_threading.h"
#include "crn_dynamic_string.h"
#include "crn_dynamic_wstring.h"
+29 -3
View File
@@ -2,6 +2,7 @@
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_data_stream.h"
#include <stdio.h>
namespace crnlib
{
@@ -11,7 +12,7 @@ namespace crnlib
{
}
data_stream::data_stream(const char* pName, uint attribs) :
data_stream::data_stream(const wchar_t* pName, uint attribs) :
m_name(pName),
m_attribs(static_cast<uint16>(attribs)),
m_opened(false), m_error(false), m_got_cr(false)
@@ -84,11 +85,28 @@ namespace crnlib
va_list args;
va_start(args, p);
dynamic_string buf;
char buf[4096];
#ifdef _MSC_VER
int l = vsprintf_s(buf, sizeof(buf), p, args);
#else
int l = vsprintf(buf, p, args);
#endif
va_end(args);
if (l < 0)
return false;
return write(buf, l) == static_cast<uint>(l);
}
bool data_stream::printf(const wchar_t* p, ...)
{
va_list args;
va_start(args, p);
dynamic_wstring buf;
buf.format_args(p, args);
va_end(args);
return write(buf.get_ptr(), buf.get_len() * sizeof(char)) == buf.get_len() * sizeof(char);
return write(buf.get_ptr(), buf.get_len() * sizeof(wchar_t)) == buf.get_len() * sizeof(wchar_t);
}
bool data_stream::write_line(const dynamic_string& str)
@@ -99,6 +117,14 @@ namespace crnlib
return true;
}
bool data_stream::write_line(const dynamic_wstring& str)
{
if (!str.is_empty())
return write(str.get_ptr(), str.get_len() * sizeof(wchar_t)) == str.get_len() * sizeof(wchar_t);
return true;
}
bool data_stream::read_array(vector<uint8>& buf)
{
if (buf.size() < get_remaining())
+33 -31
View File
@@ -10,80 +10,82 @@ namespace crnlib
cDataStreamWritable = 2,
cDataStreamSeekable = 4
};
const int64 DATA_STREAM_SIZE_UNKNOWN = cINT64_MAX;
const int64 DATA_STREAM_SIZE_INFINITE = cUINT64_MAX;
const int64 DATA_STREAM_SIZE_UNKNOWN = INT64_MAX;
const int64 DATA_STREAM_SIZE_INFINITE = UINT64_MAX;
class data_stream
{
data_stream(const data_stream&);
data_stream& operator= (const data_stream&);
public:
data_stream();
data_stream(const char* pName, uint attribs);
data_stream(const wchar_t* pName, uint attribs);
virtual ~data_stream() { }
virtual data_stream *get_parent() { return NULL; }
virtual bool close() { m_opened = false; m_error = false; m_got_cr = false; return true; }
typedef uint16 attribs_t;
typedef uint16 attribs_t;
inline attribs_t get_attribs() const { return m_attribs; }
inline bool is_opened() const { return m_opened; }
inline bool is_readable() const { return utils::is_bit_set(m_attribs, cDataStreamReadable); }
inline bool is_writable() const { return utils::is_bit_set(m_attribs, cDataStreamWritable); }
inline bool is_seekable() const { return utils::is_bit_set(m_attribs, cDataStreamSeekable); }
inline bool get_error() const { return m_error; }
inline const dynamic_string& get_name() const { return m_name; }
inline void set_name(const char* pName) { m_name.set(pName); }
inline const dynamic_wstring& get_name() const { return m_name; }
inline void set_name(const wchar_t* pName) { m_name.set(pName); }
virtual uint read(void* pBuf, uint len) = 0;
virtual uint64 skip(uint64 len);
virtual uint write(const void* pBuf, uint len) = 0;
virtual bool flush() = 0;
virtual bool is_size_known() const { return true; }
// Returns DATA_STREAM_SIZE_UNKNOWN if size hasn't been determined yet, or DATA_STREAM_SIZE_INFINITE for infinite streams.
virtual uint64 get_size() = 0;
virtual uint64 get_remaining() = 0;
virtual uint64 get_ofs() = 0;
virtual bool seek(int64 ofs, bool relative) = 0;
virtual const void* get_ptr() const { return NULL; }
inline int read_byte() { uint8 c; if (read(&c, 1) != 1) return -1; return c; }
inline bool write_byte(uint8 c) { return write(&c, 1) == 1; }
bool read_line(dynamic_string& str);
bool printf(const char* p, ...);
bool printf(const wchar_t* p, ...);
bool write_line(const dynamic_string& str);
bool write_line(const dynamic_wstring& str);
bool write_bom() { uint16 bom = 0xFEFF; return write(&bom, sizeof(bom)) == sizeof(bom); }
bool read_array(vector<uint8>& buf);
bool write_array(const vector<uint8>& buf);
protected:
dynamic_string m_name;
dynamic_wstring m_name;
attribs_t m_attribs;
bool m_opened : 1;
bool m_error : 1;
bool m_got_cr : 1;
inline void set_error() { m_error = true; }
inline void clear_error() { m_error = false; }
inline void post_seek() { m_got_cr = false; }
};
} // namespace crnlib
-36
View File
@@ -19,8 +19,6 @@ namespace crnlib
data_stream* get_stream() const { return m_pStream; }
void set_stream(data_stream* pStream) { m_pStream = pStream; }
const dynamic_string& get_name() const { return m_pStream ? m_pStream->get_name() : g_empty_dynamic_string; }
bool get_error() { return m_pStream ? m_pStream->get_error() : false; }
bool get_little_endian() const { return m_little_endian; }
@@ -36,30 +34,6 @@ namespace crnlib
return m_pStream->read(pBuf, len) == len;
}
// size = size of each element, count = number of elements, returns actual count of elements written
uint write(const void* pBuf, uint size, uint count)
{
uint actual_size = size * count;
if (!actual_size)
return 0;
uint n = m_pStream->write(pBuf, actual_size);
if (n == actual_size)
return count;
return n / size;
}
// size = size of each element, count = number of elements, returns actual count of elements read
uint read(void* pBuf, uint size, uint count)
{
uint actual_size = size * count;
if (!actual_size)
return 0;
uint n = m_pStream->read(pBuf, actual_size);
if (n == actual_size)
return count;
return n / size;
}
bool write_chars(const char* pBuf, uint len)
{
return write(pBuf, len);
@@ -275,16 +249,6 @@ namespace crnlib
return true;
}
bool read_entire_file(crnlib::vector<uint8>& buf)
{
return m_pStream->read_array(buf);
}
bool write_entire_file(const crnlib::vector<uint8>& buf)
{
return m_pStream->write_array(buf);
}
// Got this idea from the Molly Rocket forums.
// fmt may contain the characters "1", "2", or "4".
+2 -2
View File
@@ -34,7 +34,7 @@ namespace crnlib
}
}
bool dds_comp::create_dds_tex(mipmapped_texture &dds_tex)
bool dds_comp::create_dds_tex(dds_texture &dds_tex)
{
image_u8 images[cCRNMaxFaces][cCRNMaxLevels];
@@ -137,7 +137,7 @@ namespace crnlib
if (!m_pQDXT_state)
{
m_pQDXT_state = crnlib_new<mipmapped_texture::qdxt_state>(m_task_pool);
m_pQDXT_state = crnlib_new<dds_texture::qdxt_state>(m_task_pool);
if (params.m_pProgress_func)
{
+9 -9
View File
@@ -2,7 +2,7 @@
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#include "crn_comp.h"
#include "crn_mipmapped_texture.h"
#include "crn_dds_texture.h"
#include "crn_texture_comp.h"
namespace crnlib
@@ -15,7 +15,7 @@ namespace crnlib
dds_comp();
virtual ~dds_comp();
virtual const char *get_ext() const { return "DDS"; }
virtual const wchar_t *get_ext() const { return L"DDS"; }
virtual bool compress_init(const crn_comp_params& params);
virtual bool compress_pass(const crn_comp_params& params, float *pEffective_bitrate);
@@ -23,25 +23,25 @@ namespace crnlib
virtual const crnlib::vector<uint8>& get_comp_data() const { return m_comp_data; }
virtual crnlib::vector<uint8>& get_comp_data() { return m_comp_data; }
private:
mipmapped_texture m_src_tex;
mipmapped_texture m_packed_tex;
dds_texture m_src_tex;
dds_texture m_packed_tex;
crnlib::vector<uint8> m_comp_data;
const crn_comp_params* m_pParams;
pixel_format m_pixel_fmt;
dxt_image::pack_params m_pack_params;
task_pool m_task_pool;
qdxt1_params m_q1_params;
qdxt5_params m_q5_params;
mipmapped_texture::qdxt_state *m_pQDXT_state;
dds_texture::qdxt_state *m_pQDXT_state;
void clear();
bool create_dds_tex(mipmapped_texture &dds_tex);
bool create_dds_tex(dds_texture &dds_tex);
bool convert_to_dxt(const crn_comp_params& params);
};
File diff suppressed because it is too large Load Diff
@@ -1,4 +1,4 @@
// File: crn_mipmapped_texture.h
// File: crn_dds_texture.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#include "crn_dxt_image.h"
@@ -15,54 +15,37 @@
namespace crnlib
{
extern const vec2I g_vertical_cross_image_offsets[6];
enum orientation_flags_t
{
cOrientationFlagXFlipped = 1,
cOrientationFlagYFlipped = 2,
cDefaultOrientationFlags = 0
};
enum unpack_flags_t
{
cUnpackFlagUncook = 1,
cUnpackFlagUnflip = 2
};
class mip_level
{
friend class mipmapped_texture;
friend class dds_texture;
public:
mip_level();
~mip_level();
mip_level(const mip_level& other);
mip_level& operator= (const mip_level& rhs);
// Assumes ownership.
void assign(image_u8* p, pixel_format fmt = PIXEL_FMT_INVALID, orientation_flags_t orient_flags = cDefaultOrientationFlags);
void assign(dxt_image* p, pixel_format fmt = PIXEL_FMT_INVALID, orientation_flags_t orient_flags = cDefaultOrientationFlags);
void assign(image_u8* p, pixel_format fmt = PIXEL_FMT_INVALID);
void assign(dxt_image* p, pixel_format fmt = PIXEL_FMT_INVALID);
void clear();
inline uint get_width() const { return m_width; }
inline uint get_height() const { return m_height; }
inline uint get_total_pixels() const { return m_width * m_height; }
orientation_flags_t get_orientation_flags() const { return m_orient_flags; }
void set_orientation_flags(orientation_flags_t flags) { m_orient_flags = flags; }
inline image_u8* get_image() const { return m_pImage; }
inline dxt_image* get_dxt_image() const { return m_pDXTImage; }
image_u8* get_unpacked_image(image_u8& tmp, uint unpack_flags) const;
image_u8* get_unpacked_image(image_u8& tmp, bool uncook) const;
inline bool is_packed() const { return m_pDXTImage != NULL; }
inline bool is_valid() const { return (m_pImage != NULL) || (m_pDXTImage != NULL); }
inline pixel_format_helpers::component_flags get_comp_flags() const { return m_comp_flags; }
inline void set_comp_flags(pixel_format_helpers::component_flags comp_flags) { m_comp_flags = comp_flags; }
@@ -70,83 +53,66 @@ namespace crnlib
inline void set_format(pixel_format fmt) { m_format = fmt; }
bool convert(pixel_format fmt, bool cook, const dxt_image::pack_params& p);
bool pack_to_dxt(const image_u8& img, pixel_format fmt, bool cook, const dxt_image::pack_params& p, orientation_flags_t orient_flags = cDefaultOrientationFlags);
bool pack_to_dxt(pixel_format fmt, bool cook, const dxt_image::pack_params& p);
bool pack_to_dxt(const image_u8& img, pixel_format fmt, bool cook, const dxt_image::pack_params& p);
bool pack_to_dxt(pixel_format fmt, bool cook, const dxt_image::pack_params& p);
bool unpack_from_dxt(bool uncook = true);
// Returns true if flipped on either axis.
bool is_flipped() const;
bool is_x_flipped() const;
bool is_y_flipped() const;
bool can_unflip_without_unpacking() const;
// Returns true if unflipped on either axis.
// Will try to flip packed (DXT/ETC) data in-place, if this isn't possible it'll unpack/uncook the mip level then unflip.
bool unflip(bool allow_unpacking_to_flip, bool uncook_during_unpack);
bool set_alpha_to_luma();
bool convert(image_utils::conversion_type conv_type);
bool flip_x();
bool flip_y();
private:
uint m_width;
uint m_height;
pixel_format_helpers::component_flags m_comp_flags;
pixel_format m_format;
image_u8* m_pImage;
dxt_image* m_pDXTImage;
orientation_flags_t m_orient_flags;
void cook_image(image_u8& img) const;
void uncook_image(image_u8& img) const;
};
// A face is an array of mip_level ptr's.
typedef crnlib::vector<mip_level*> mip_ptr_vec;
// And an array of one, six, or N faces make up a texture.
typedef crnlib::vector<mip_ptr_vec> face_vec;
class mipmapped_texture
class dds_texture
{
public:
// Construction/destruction
mipmapped_texture();
~mipmapped_texture();
dds_texture();
~dds_texture();
mipmapped_texture(const mipmapped_texture& other);
mipmapped_texture& operator= (const mipmapped_texture& rhs);
dds_texture(const dds_texture& other);
dds_texture& operator= (const dds_texture& rhs);
void clear();
void init(uint width, uint height, uint levels, uint faces, pixel_format fmt, const char* pName, orientation_flags_t orient_flags);
void init(uint width, uint height, uint levels, uint faces, pixel_format fmt, const wchar_t* pName);
// Assumes ownership.
void assign(face_vec& faces);
void assign(mip_level* pLevel);
void assign(image_u8* p, pixel_format fmt = PIXEL_FMT_INVALID, orientation_flags_t orient_flags = cDefaultOrientationFlags);
void assign(dxt_image* p, pixel_format fmt = PIXEL_FMT_INVALID, orientation_flags_t orient_flags = cDefaultOrientationFlags);
void assign(image_u8* p, pixel_format fmt = PIXEL_FMT_INVALID);
void assign(dxt_image* p, pixel_format fmt = PIXEL_FMT_INVALID);
void set(texture_file_types::format source_file_type, const mipmapped_texture& mipmapped_texture);
void set(texture_file_types::format source_file_type, const dds_texture& dds_texture);
// Accessors
image_u8* get_level_image(uint face, uint level, image_u8& img, uint unpack_flags = cUnpackFlagUncook | cUnpackFlagUnflip) const;
image_u8* get_level_image(uint face, uint level, image_u8& img, bool uncook = true) const;
inline bool is_valid() const { return m_faces.size() > 0; }
const dynamic_string& get_name() const { return m_name; }
void set_name(const dynamic_string& name) { m_name = name; }
const dynamic_wstring& get_name() const { return m_name; }
void set_name(const dynamic_wstring& name) { m_name = name; }
const dynamic_string& get_source_filename() const { return get_name(); }
const dynamic_wstring& get_source_filename() const { return get_name(); }
texture_file_types::format get_source_file_type() const { return m_source_file_type; }
inline uint get_width() const { return m_width; }
@@ -167,37 +133,34 @@ namespace crnlib
inline const mip_level* get_level(uint face, uint mip) const { return m_faces[face][mip]; }
inline mip_level* get_level(uint face, uint mip) { return m_faces[face][mip]; }
bool has_alpha() const;
bool is_normal_map() const;
bool is_vertical_cross() const;
bool is_packed() const;
bool is_packed() const;
texture_type determine_texture_type() const;
const dynamic_string& get_last_error() const { return m_last_error; }
const dynamic_wstring& get_last_error() const { return m_last_error; }
void clear_last_error() { m_last_error.clear(); }
// Reading/writing
// Loading/saving
bool read_dds(const wchar_t* pFilename);
bool read_dds(data_stream_serializer& serializer);
bool write_dds(data_stream_serializer& serializer) const;
bool read_ktx(data_stream_serializer& serializer);
bool write_ktx(data_stream_serializer& serializer) const;
bool read_crn(data_stream_serializer& serializer);
bool read_crn_from_memory(const void *pData, uint data_size, const char* pFilename);
bool write_dds(const wchar_t* pFilename) const;
bool write_dds(data_stream_serializer& serializer) const;
bool load_crn_from_memory(const wchar_t* pFilename, const void *pData, uint data_size);
// If file_format is texture_file_types::cFormatInvalid, the format will be determined from the filename's extension.
bool read_from_file(const char* pFilename, texture_file_types::format file_format = texture_file_types::cFormatInvalid);
bool read_from_stream(data_stream_serializer& serializer, texture_file_types::format file_format = texture_file_types::cFormatInvalid);
bool load_from_file(const wchar_t* pFilename, texture_file_types::format file_format);
bool write_to_file(
const char* pFilename,
texture_file_types::format file_format = texture_file_types::cFormatInvalid,
crn_comp_params* pComp_params = NULL,
uint32* pActual_quality_level = NULL, float* pActual_bitrate = NULL,
uint32 image_write_flags = 0);
const wchar_t* pFilename,
texture_file_types::format file_format,
crn_comp_params* pCRN_comp_params,
uint32 *pActual_quality_level, float *pActual_bitrate);
// Conversion
bool convert(pixel_format fmt, bool cook, const dxt_image::pack_params& p);
bool convert(pixel_format fmt, const dxt_image::pack_params& p);
@@ -205,16 +168,16 @@ namespace crnlib
bool convert(image_utils::conversion_type conv_type);
bool unpack_from_dxt(bool uncook = true);
bool set_alpha_to_luma();
bool set_alpha_to_luma();
void discard_mipmaps();
void discard_mips();
struct resample_params
{
resample_params() :
resample_params() :
m_pFilter("kaiser"),
m_wrapping(false),
m_srgb(false),
@@ -223,8 +186,8 @@ namespace crnlib
m_gamma(1.75f), // or 2.2f
m_multithreaded(true)
{
}
}
const char* m_pFilter;
bool m_wrapping;
bool m_srgb;
@@ -233,45 +196,45 @@ namespace crnlib
float m_gamma;
bool m_multithreaded;
};
bool resize(uint new_width, uint new_height, const resample_params& params);
struct generate_mipmap_params : public resample_params
{
generate_mipmap_params() :
generate_mipmap_params() :
resample_params(),
m_min_mip_size(1),
m_max_mips(0)
{
}
}
uint m_min_mip_size;
uint m_max_mips; // actually the max # of total levels
};
bool generate_mipmaps(const generate_mipmap_params& params, bool force);
bool crop(uint x, uint y, uint width, uint height);
bool vertical_cross_to_cubemap();
// Low-level clustered DXT (QDXT) compression
struct qdxt_state
{
qdxt_state(task_pool& tp) : m_fmt(PIXEL_FMT_INVALID), m_qdxt1(tp), m_qdxt5a(tp), m_qdxt5b(tp)
{
}
pixel_format m_fmt;
qdxt1 m_qdxt1;
qdxt5 m_qdxt5a;
qdxt5 m_qdxt5b;
crnlib::vector<dxt_pixel_block> m_pixel_blocks;
qdxt1_params m_qdxt1_params;
qdxt5_params m_qdxt5_params[2];
bool m_has_blocks[3];
void clear()
{
m_fmt = PIXEL_FMT_INVALID;
@@ -285,55 +248,45 @@ namespace crnlib
utils::zero_object(m_has_blocks);
}
};
bool qdxt_pack_init(qdxt_state& state, mipmapped_texture& dst_tex, const qdxt1_params& dxt1_params, const qdxt5_params& dxt5_params, pixel_format fmt, bool cook);
bool qdxt_pack(qdxt_state& state, mipmapped_texture& dst_tex, const qdxt1_params& dxt1_params, const qdxt5_params& dxt5_params);
void swap(mipmapped_texture& img);
bool check() const;
void set_orientation_flags(orientation_flags_t flags);
// Returns true if any face/miplevel is flipped.
bool is_flipped() const;
bool is_x_flipped() const;
bool is_y_flipped() const;
bool can_unflip_without_unpacking() const;
bool unflip(bool allow_unpacking_to_flip, bool uncook_if_necessary_to_unpack);
bool qdxt_pack_init(qdxt_state& state, dds_texture& dst_tex, const qdxt1_params& dxt1_params, const qdxt5_params& dxt5_params, pixel_format fmt, bool cook);
bool qdxt_pack(qdxt_state& state, dds_texture& dst_tex, const qdxt1_params& dxt1_params, const qdxt5_params& dxt5_params);
void swap(dds_texture& img);
bool flip_y(bool update_orientation_flags);
bool check() const;
private:
dynamic_string m_name;
dynamic_wstring m_name;
uint m_width;
uint m_height;
pixel_format_helpers::component_flags m_comp_flags;
pixel_format m_format;
face_vec m_faces;
texture_file_types::format m_source_file_type;
mutable dynamic_string m_last_error;
mutable dynamic_wstring m_last_error;
inline void clear_last_error() const { m_last_error.clear(); }
inline void set_last_error(const char* p) const { m_last_error = p; }
inline void set_last_error(const wchar_t* p) const { m_last_error = p; }
void free_all_mips();
bool read_regular_image(data_stream_serializer &serializer, texture_file_types::format file_format);
bool write_regular_image(const char* pFilename, uint32 image_write_flags);
bool read_dds_internal(data_stream_serializer& serializer);
bool load_regular(const wchar_t* pFilename, texture_file_types::format file_format);
bool load_dds(const wchar_t* pFilename);
bool load_crn(const wchar_t* pFilename);
void print_crn_comp_params(const crn_comp_params& p);
bool write_comp_texture(const char* pFilename, const crn_comp_params &comp_params, uint32 *pActual_quality_level, float *pActual_bitrate);
void change_dxt1_to_dxt1a();
bool flip_y_helper();
bool save_regular(const wchar_t* pFilename);
bool save_dds(const wchar_t* pFilename);
bool save_comp_texture(const wchar_t* pFilename, const crn_comp_params &comp_params, uint32 *pActual_quality_level, float *pActual_bitrate);
};
inline void swap(mipmapped_texture& a, mipmapped_texture& b)
inline void swap(dds_texture& a, dds_texture& b)
{
a.swap(b);
}
a.swap(b);
}
} // namespace crnlib
+16 -21
View File
@@ -11,47 +11,43 @@ namespace crnlib
{
const uint8 g_dxt5_from_linear[cDXT5SelectorValues] = { 0U, 2U, 3U, 4U, 5U, 6U, 7U, 1U };
const uint8 g_dxt5_to_linear[cDXT5SelectorValues] = { 0U, 7U, 1U, 2U, 3U, 4U, 5U, 6U };
const uint8 g_dxt5_alpha6_to_linear[cDXT5SelectorValues] = { 0U, 5U, 1U, 2U, 3U, 4U, 0U, 0U };
const uint8 g_dxt1_from_linear[cDXT1SelectorValues] = { 0U, 2U, 3U, 1U };
const uint8 g_dxt1_to_linear[cDXT1SelectorValues] = { 0U, 3U, 1U, 2U };
const uint8 g_six_alpha_invert_table[cDXT5SelectorValues] = { 1, 0, 5, 4, 3, 2, 6, 7 };
const uint8 g_eight_alpha_invert_table[cDXT5SelectorValues] = { 1, 0, 7, 6, 5, 4, 3, 2 };
const char* get_dxt_format_string(dxt_format fmt)
const wchar_t* get_dxt_format_string(dxt_format fmt)
{
switch (fmt)
{
case cDXT1: return "DXT1";
case cDXT1A: return "DXT1A";
case cDXT3: return "DXT3";
case cDXT5: return "DXT5";
case cDXT5A: return "DXT5A";
case cDXN_XY: return "DXN_XY";
case cDXN_YX: return "DXN_YX";
case cETC1: return "ETC1";
case cDXT1: return L"DXT1";
case cDXT1A: return L"DXT1A";
case cDXT3: return L"DXT3";
case cDXT5: return L"DXT5";
case cDXT5A: return L"DXT5A";
case cDXN_XY: return L"DXN_XY";
case cDXN_YX: return L"DXN_YX";
default: break;
}
CRNLIB_ASSERT(false);
return "?";
return L"?";
}
const char* get_dxt_compressor_name(crn_dxt_compressor_type c)
const wchar_t* get_dxt_compressor_name(crn_dxt_compressor_type c)
{
switch (c)
{
case cCRNDXTCompressorCRN: return "CRN";
case cCRNDXTCompressorCRNF: return "CRNF";
case cCRNDXTCompressorRYG: return "RYG";
#if CRNLIB_SUPPORT_ATI_COMPRESS
case cCRNDXTCompressorATI: return "ATI";
#endif
case cCRNDXTCompressorCRN: return L"CRN";
case cCRNDXTCompressorCRNF: return L"CRNF";
case cCRNDXTCompressorRYG: return L"RYG";
default: break;
}
CRNLIB_ASSERT(false);
return "?";
return L"?";
}
uint get_dxt_format_bits_per_pixel(dxt_format fmt)
@@ -61,7 +57,6 @@ namespace crnlib
case cDXT1:
case cDXT1A:
case cDXT5A:
case cETC1:
return 4;
case cDXT3:
case cDXT5:
+58 -138
View File
@@ -1,6 +1,6 @@
// File: crn_dxt.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#pragma once
#include "../inc/crnlib.h"
#include "crn_color.h"
#include "crn_vec.h"
@@ -25,7 +25,7 @@ namespace crnlib
cDXT1SelectorBits = 2U,
cDXT1SelectorValues = 1U << cDXT1SelectorBits,
cDXT1SelectorMask = cDXT1SelectorValues - 1U,
cDXTBlockShift = 2U,
cDXTBlockSize = 1U << cDXTBlockShift
};
@@ -33,30 +33,28 @@ namespace crnlib
enum dxt_format
{
cDXTInvalid = -1,
// cDXT1/1A must appear first!
cDXT1,
cDXT1A,
cDXT3,
cDXT5,
cDXT1,
cDXT1A,
cDXT3,
cDXT5,
cDXT5A,
cDXN_XY, // inverted relative to standard ATI2, 360's DXN
cDXN_YX, // standard ATI2,
cETC1 // Ericsson texture compression (color only, 4x4 blocks, 4bpp, 64-bits/block)
cDXN_YX // standard ATI2
};
const float cDXT1MaxLinearValue = 3.0f;
const float cDXT1InvMaxLinearValue = 1.0f/3.0f;
const float cDXT5MaxLinearValue = 7.0f;
const float cDXT5InvMaxLinearValue = 1.0f/7.0f;
// Converts DXT1 raw color selector index to a linear value.
extern const uint8 g_dxt1_to_linear[cDXT1SelectorValues];
// Converts DXT5 raw alpha selector index to a linear value.
extern const uint8 g_dxt5_to_linear[cDXT5SelectorValues];
@@ -65,33 +63,33 @@ namespace crnlib
// Converts DXT5 linear alpha selector index to a raw value (inverse of g_dxt5_to_linear).
extern const uint8 g_dxt5_from_linear[cDXT5SelectorValues];
extern const uint8 g_dxt5_alpha6_to_linear[cDXT5SelectorValues];
extern const uint8 g_six_alpha_invert_table[cDXT5SelectorValues];
extern const uint8 g_eight_alpha_invert_table[cDXT5SelectorValues];
const char* get_dxt_format_string(dxt_format fmt);
const wchar_t* get_dxt_format_string(dxt_format fmt);
uint get_dxt_format_bits_per_pixel(dxt_format fmt);
bool get_dxt_format_has_alpha(dxt_format fmt);
const char* get_dxt_quality_string(crn_dxt_quality q);
const char* get_dxt_compressor_name(crn_dxt_compressor_type c);
const wchar_t* get_dxt_quality_string(crn_dxt_quality q);
const wchar_t* get_dxt_compressor_name(crn_dxt_compressor_type c);
struct dxt1_block
{
uint8 m_low_color[2];
uint8 m_high_color[2];
enum { cNumSelectorBytes = 4 };
uint8 m_selectors[cNumSelectorBytes];
inline void clear()
{
utils::zero_this(this);
}
// These methods assume the in-memory rep is in LE byte order.
inline uint get_low_color() const
{
@@ -102,125 +100,73 @@ namespace crnlib
{
return m_high_color[0] | (m_high_color[1] << 8U);
}
inline void set_low_color(uint16 c)
inline void set_low_color(uint16 c)
{
m_low_color[0] = static_cast<uint8>(c & 0xFF);
m_low_color[1] = static_cast<uint8>((c >> 8) & 0xFF);
}
inline void set_high_color(uint16 c)
{
m_high_color[0] = static_cast<uint8>(c & 0xFF);
m_high_color[1] = static_cast<uint8>((c >> 8) & 0xFF);
}
inline bool is_constant_color_block() const { return get_low_color() == get_high_color(); }
inline bool is_alpha_block() const { return get_low_color() <= get_high_color(); }
inline bool is_non_alpha_block() const { return !is_alpha_block(); }
inline uint get_selector(uint x, uint y) const
{
CRNLIB_ASSERT((x < 4U) && (y < 4U));
return (m_selectors[y] >> (x * cDXT1SelectorBits)) & cDXT1SelectorMask;
}
inline void set_selector(uint x, uint y, uint val)
{
CRNLIB_ASSERT((x < 4U) && (y < 4U) && (val < 4U));
m_selectors[y] &= (~(cDXT1SelectorMask << (x * cDXT1SelectorBits)));
m_selectors[y] |= (val << (x * cDXT1SelectorBits));
}
inline void flip_x(uint w = 4, uint h = 4)
{
for (uint x = 0; x < (w / 2); x++)
{
for (uint y = 0; y < h; y++)
{
const uint c = get_selector(x, y);
set_selector(x, y, get_selector((w - 1) - x, y));
set_selector((w - 1) - x, y, c);
}
}
}
inline void flip_y(uint w = 4, uint h = 4)
{
for (uint y = 0; y < (h / 2); y++)
{
for (uint x = 0; x < w; x++)
{
const uint c = get_selector(x, y);
set_selector(x, y, get_selector(x, (h - 1) - y));
set_selector(x, (h - 1) - y, c);
}
}
}
static uint16 pack_color(const color_quad_u8& color, bool scaled, uint bias = 127U);
static uint16 pack_color(uint r, uint g, uint b, bool scaled, uint bias = 127U);
static color_quad_u8 unpack_color(uint16 packed_color, bool scaled, uint alpha = 255U);
static void unpack_color(uint& r, uint& g, uint& b, uint16 packed_color, bool scaled);
static uint get_block_colors3(color_quad_u8* pDst, uint16 color0, uint16 color1);
static uint get_block_colors3_round(color_quad_u8* pDst, uint16 color0, uint16 color1);
static uint get_block_colors4(color_quad_u8* pDst, uint16 color0, uint16 color1);
static uint get_block_colors4_round(color_quad_u8* pDst, uint16 color0, uint16 color1);
// pDst must point to an array at least cDXT1SelectorValues long.
static uint get_block_colors(color_quad_u8* pDst, uint16 color0, uint16 color1);
static uint get_block_colors_round(color_quad_u8* pDst, uint16 color0, uint16 color1);
static color_quad_u8 unpack_endpoint(uint32 endpoints, uint index, bool scaled, uint alpha = 255U);
static uint pack_endpoints(uint lo, uint hi);
static void get_block_colors_NV5x(color_quad_u8* pDst, uint16 packed_col0, uint16 packed_col1, bool color4);
};
CRNLIB_DEFINE_BITWISE_COPYABLE(dxt1_block);
struct dxt3_block
{
enum { cNumAlphaBytes = 8 };
uint8 m_alpha[cNumAlphaBytes];
void set_alpha(uint x, uint y, uint value, bool scaled);
uint get_alpha(uint x, uint y, bool scaled) const;
inline void flip_x(uint w = 4, uint h = 4)
{
for (uint x = 0; x < (w / 2); x++)
{
for (uint y = 0; y < h; y++)
{
const uint c = get_alpha(x, y, false);
set_alpha(x, y, get_alpha((w - 1) - x, y, false), false);
set_alpha((w - 1) - x, y, c, false);
}
}
}
inline void flip_y(uint w = 4, uint h = 4)
{
for (uint y = 0; y < (h / 2); y++)
{
for (uint x = 0; x < w; x++)
{
const uint c = get_alpha(x, y, false);
set_alpha(x, y, get_alpha(x, (h - 1) - y, false), false);
set_alpha(x, (h - 1) - y, c, false);
}
}
}
};
CRNLIB_DEFINE_BITWISE_COPYABLE(dxt3_block);
struct dxt5_block
{
uint8 m_endpoints[2];
@@ -243,23 +189,23 @@ namespace crnlib
return m_endpoints[1];
}
inline void set_low_alpha(uint i)
inline void set_low_alpha(uint i)
{
CRNLIB_ASSERT(i <= cUINT8_MAX);
CRNLIB_ASSERT(i <= UINT8_MAX);
m_endpoints[0] = static_cast<uint8>(i);
}
inline void set_high_alpha(uint i)
inline void set_high_alpha(uint i)
{
CRNLIB_ASSERT(i <= cUINT8_MAX);
CRNLIB_ASSERT(i <= UINT8_MAX);
m_endpoints[1] = static_cast<uint8>(i);
}
inline bool is_alpha6_block() const { return get_low_alpha() <= get_high_alpha(); }
uint get_endpoints_as_word() const { return m_endpoints[0] | (m_endpoints[1] << 8); }
uint get_selectors_as_word(uint index) { CRNLIB_ASSERT(index < 3); return m_selectors[index * 2] | (m_selectors[index * 2 + 1] << 8); }
inline uint get_selector(uint x, uint y) const
{
CRNLIB_ASSERT((x < 4U) && (y < 4U));
@@ -298,33 +244,7 @@ namespace crnlib
if (byte_index < (cNumSelectorBytes - 1))
m_selectors[byte_index + 1] = static_cast<uint8>(v >> 8);
}
inline void flip_x(uint w = 4, uint h = 4)
{
for (uint x = 0; x < (w / 2); x++)
{
for (uint y = 0; y < h; y++)
{
const uint c = get_selector(x, y);
set_selector(x, y, get_selector((w - 1) - x, y));
set_selector((w - 1) - x, y, c);
}
}
}
inline void flip_y(uint w = 4, uint h = 4)
{
for (uint y = 0; y < (h / 2); y++)
{
for (uint x = 0; x < w; x++)
{
const uint c = get_selector(x, y);
set_selector(x, y, get_selector(x, (h - 1) - y));
set_selector(x, (h - 1) - y, c);
}
}
}
enum { cMaxSelectorValues = 8 };
// Results written to alpha channel.
@@ -340,19 +260,19 @@ namespace crnlib
static uint unpack_endpoint(uint packed, uint index);
static uint pack_endpoints(uint lo, uint hi);
};
CRNLIB_DEFINE_BITWISE_COPYABLE(dxt5_block);
struct dxt_pixel_block
{
color_quad_u8 m_pixels[cDXTBlockSize][cDXTBlockSize]; // [y][x]
inline void clear()
{
utils::zero_object(*this);
utils::zero_object(*this);
}
};
};
CRNLIB_DEFINE_BITWISE_COPYABLE(dxt_pixel_block);
} // namespace crnlib
+317 -350
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -258,7 +258,7 @@ namespace crnlib
struct potential_solution
{
potential_solution() : m_coords(), m_error(cUINT64_MAX), m_alpha_block(false), m_valid(false)
potential_solution() : m_coords(), m_error(UINT64_MAX), m_alpha_block(false), m_valid(false)
{
}
@@ -272,7 +272,7 @@ namespace crnlib
{
m_coords.clear();
m_selectors.resize(0);
m_error = cUINT64_MAX;
m_error = UINT64_MAX;
m_alpha_block = false;
m_valid = false;
}
+1 -1
View File
@@ -62,7 +62,7 @@ namespace crnlib
m_trial_selectors.resize(m_unique_values.size());
m_best_selectors.resize(m_unique_values.size());
r.m_error = cUINT64_MAX;
r.m_error = UINT64_MAX;
for (uint i = 0; i < m_unique_values.size() - 1; i++)
{
+1 -1
View File
@@ -20,7 +20,7 @@ namespace crnlib
m_pParams = &p;
m_pResults = &r;
r.m_error = cUINT64_MAX;
r.m_error = UINT64_MAX;
r.m_low_color = 0;
r.m_high_color = 0;
+1 -1
View File
@@ -19,7 +19,7 @@ namespace crnlib
m_num_pixels(0),
m_pSelectors(NULL),
m_alpha_comp_index(0),
m_error_to_beat(cUINT64_MAX),
m_error_to_beat(UINT64_MAX),
m_dxt1_selectors(true),
m_perceptual(true),
m_highest_quality(true)
+2 -2
View File
@@ -507,7 +507,7 @@ namespace crnlib
static void refine_endpoints2(uint n, const color_quad_u8* pBlock, uint& low16, uint& high16, uint8* pSelectors, float axis[3])
{
uint64 orig_error = determine_error(n, pBlock, low16, high16, cUINT64_MAX);
uint64 orig_error = determine_error(n, pBlock, low16, high16, UINT64_MAX);
if (!orig_error)
return;
@@ -623,7 +623,7 @@ namespace crnlib
{
improved = true;
uint64 cur_error = determine_error(n, pBlock, low16, high16, cUINT64_MAX);
uint64 cur_error = determine_error(n, pBlock, low16, high16, UINT64_MAX);
if (!cur_error)
return;
}
+41 -41
View File
@@ -33,7 +33,7 @@ namespace crnlib
m_has_color_blocks(false),
m_has_alpha0_blocks(false),
m_has_alpha1_blocks(false),
m_main_thread_id(crn_get_current_thread_id()),
m_main_thread_id(get_current_thread_id()),
m_canceled(false),
m_pTask_pool(NULL),
m_prev_phase_index(-1),
@@ -104,7 +104,7 @@ namespace crnlib
bool dxt_hc::compress(const params& p, uint num_chunks, const pixel_chunk* pChunks, task_pool& task_pool)
{
m_pTask_pool = &task_pool;
m_main_thread_id = crn_get_current_thread_id();
m_main_thread_id = get_current_thread_id();
bool result = compress_internal(p, num_chunks, pChunks);
@@ -335,7 +335,7 @@ namespace crnlib
if (m_canceled)
return;
if ((crn_get_current_thread_id() == m_main_thread_id) && ((chunk_index & 511) == 0))
if ((get_current_thread_id() == m_main_thread_id) && ((chunk_index & 511) == 0))
{
if (!update_progress(0, chunk_index, m_num_chunks))
return;
@@ -550,9 +550,9 @@ namespace crnlib
}
}
atomic_increment32(&m_encoding_hist[best_encoding]);
interlocked_increment32(&m_encoding_hist[best_encoding]);
atomic_exchange_add32(&m_total_tiles, g_chunk_encodings[best_encoding].m_num_tiles);
interlocked_exchange_add32(&m_total_tiles, g_chunk_encodings[best_encoding].m_num_tiles);
for (uint q = 0; q < cNumCompressedChunkVecs; q++)
{
@@ -664,15 +664,15 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
{
console::info("Total Pixels: %u, Chunks: %u, Blocks: %u, Adapted Tiles: %u", m_num_chunks * cChunkPixelWidth * cChunkPixelHeight, m_num_chunks, m_num_chunks * cChunkBlockWidth * cChunkBlockHeight, m_total_tiles);
console::info(L"Total Pixels: %u, Chunks: %u, Blocks: %u, Adapted Tiles: %u", m_num_chunks * cChunkPixelWidth * cChunkPixelHeight, m_num_chunks, m_num_chunks * cChunkBlockWidth * cChunkBlockHeight, m_total_tiles);
console::info("Chunk encoding type symbol_histogram: ");
console::info(L"Chunk encoding type symbol_histogram: ");
for (uint e = 0; e < cNumChunkEncodings; e++)
console::info("%u ", m_encoding_hist[e]);
console::info(L"%u ", m_encoding_hist[e]);
console::info("Blocks per chunk encoding type: ");
console::info(L"Blocks per chunk encoding type: ");
for (uint e = 0; e < cNumChunkEncodings; e++)
console::info("%u ", m_encoding_hist[e] * cChunkBlockWidth * cChunkBlockHeight);
console::info(L"%u ", m_encoding_hist[e] * cChunkBlockWidth * cChunkBlockHeight);
}
#endif
@@ -689,7 +689,7 @@ namespace crnlib
if (m_canceled)
return;
if ((crn_get_current_thread_id() == m_main_thread_id) && ((chunk_index & 63) == 0))
if ((get_current_thread_id() == m_main_thread_id) && ((chunk_index & 63) == 0))
{
if (!update_progress(2, chunk_index, m_num_chunks))
return;
@@ -719,7 +719,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Generating color training vectors");
console::info(L"Generating color training vectors");
#endif
const float r_scale = .5f;
@@ -804,7 +804,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Begin color cluster analysis");
console::info(L"Begin color cluster analysis");
timer t;
t.start();
#endif
@@ -816,7 +816,7 @@ namespace crnlib
if (m_params.m_debugging)
{
double total_time = t.get_elapsed_secs();
console::info("Codebook gen time: %3.3fs, Total color clusters: %u", total_time, vq.get_codebook_size());
console::info(L"Codebook gen time: %3.3fs, Total color clusters: %u", total_time, vq.get_codebook_size());
}
#endif
@@ -824,7 +824,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Begin color cluster assignment");
console::info(L"Begin color cluster assignment");
#endif
assign_color_endpoint_clusters_state state(vq, training_vecs);
@@ -850,7 +850,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Completed color cluster assignment");
console::info(L"Completed color cluster assignment");
#endif
return true;
@@ -868,7 +868,7 @@ namespace crnlib
if (m_canceled)
return;
if ((crn_get_current_thread_id() == m_main_thread_id) && ((chunk_index & 63) == 0))
if ((get_current_thread_id() == m_main_thread_id) && ((chunk_index & 63) == 0))
{
if (!update_progress(7, m_num_chunks * a + chunk_index, m_num_chunks * m_num_alpha_blocks))
return;
@@ -899,7 +899,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Generating alpha training vectors");
console::info(L"Generating alpha training vectors");
#endif
determine_alpha_endpoint_clusters_state state;
@@ -966,7 +966,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Begin alpha cluster analysis");
console::info(L"Begin alpha cluster analysis");
timer t;
t.start();
#endif
@@ -978,7 +978,7 @@ namespace crnlib
if (m_params.m_debugging)
{
double total_time = t.get_elapsed_secs();
console::info("Codebook gen time: %3.3fs, Total alpha clusters: %u", total_time, state.m_vq.get_codebook_size());
console::info(L"Codebook gen time: %3.3fs, Total alpha clusters: %u", total_time, state.m_vq.get_codebook_size());
}
#endif
@@ -986,7 +986,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Begin alpha cluster assignment");
console::info(L"Begin alpha cluster assignment");
#endif
for (uint i = 0; i <= m_pTask_pool->get_num_threads(); i++)
@@ -1013,7 +1013,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Completed alpha cluster assignment");
console::info(L"Completed alpha cluster assignment");
#endif
return true;
@@ -1040,7 +1040,7 @@ namespace crnlib
if (m_canceled)
return;
if ((crn_get_current_thread_id() == m_main_thread_id) && ((cluster_index & 63) == 0))
if ((get_current_thread_id() == m_main_thread_id) && ((cluster_index & 63) == 0))
{
if (!update_progress(3, cluster_index, m_color_clusters.size()))
return;
@@ -1151,7 +1151,7 @@ namespace crnlib
if (m_params.m_debugging)
{
if (total_empty_clusters)
console::warning("Total empty color clusters: %u", total_empty_clusters);
console::warning(L"Total empty color clusters: %u", total_empty_clusters);
}
#endif
}
@@ -1163,7 +1163,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Computing optimal color cluster endpoints");
console::info(L"Computing optimal color cluster endpoints");
#endif
for (uint i = 0; i <= m_pTask_pool->get_num_threads(); i++)
@@ -1192,7 +1192,7 @@ namespace crnlib
if (m_canceled)
return;
if ((crn_get_current_thread_id() == m_main_thread_id) && ((cluster_index & 63) == 0))
if ((get_current_thread_id() == m_main_thread_id) && ((cluster_index & 63) == 0))
{
if (!update_progress(8, cluster_index, m_alpha_clusters.size()))
return;
@@ -1311,7 +1311,7 @@ namespace crnlib
if (m_params.m_debugging)
{
if (total_empty_clusters)
console::warning("Total empty alpha clusters: %u", total_empty_clusters);
console::warning(L"Total empty alpha clusters: %u", total_empty_clusters);
}
#endif
}
@@ -1323,7 +1323,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Computing optimal alpha cluster endpoints");
console::info(L"Computing optimal alpha cluster endpoints");
#endif
for (uint i = 0; i <= m_pTask_pool->get_num_threads(); i++)
@@ -1461,7 +1461,7 @@ namespace crnlib
if (m_canceled)
return;
if ((crn_get_current_thread_id() == m_main_thread_id) && ((chunk_index & 127) == 0))
if ((get_current_thread_id() == m_main_thread_id) && ((chunk_index & 127) == 0))
{
if (!update_progress(12 + comp_chunk_index, chunk_index, m_num_chunks))
return;
@@ -1632,7 +1632,7 @@ namespace crnlib
{
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Computing selector training vectors");
console::info(L"Computing selector training vectors");
#endif
const uint cColorDistToWeight = 2000;
@@ -1775,7 +1775,7 @@ namespace crnlib
if (m_params.m_debugging)
{
double total_time = t.get_elapsed_secs();
console::info("Codebook gen time: %3.3fs, Selector codebook size: %u", total_time, selector_vq.get_codebook_size());
console::info(L"Codebook gen time: %3.3fs, Selector codebook size: %u", total_time, selector_vq.get_codebook_size());
}
#endif
@@ -1827,7 +1827,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Refining quantized color selectors");
console::info(L"Refining quantized color selectors");
#endif
uint total_refined_selectors = 0;
@@ -1917,7 +1917,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Total refined pixels: %u, selectors: %u out of %u", total_refined_pixels, total_refined_selectors, total_selectors);
console::info(L"Total refined pixels: %u, selectors: %u out of %u", total_refined_pixels, total_refined_selectors, total_selectors);
#endif
return true;
@@ -1930,7 +1930,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Refining quantized alpha selectors");
console::info(L"Refining quantized alpha selectors");
#endif
uint total_refined_selectors = 0;
@@ -2021,7 +2021,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Total refined pixels: %u, selectors: %u out of %u", total_refined_pixels, total_refined_selectors, total_selectors);
console::info(L"Total refined pixels: %u, selectors: %u out of %u", total_refined_pixels, total_refined_selectors, total_selectors);
#endif
return true;
@@ -2037,7 +2037,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Refining quantized color endpoints");
console::info(L"Refining quantized color endpoints");
#endif
for (uint cluster_index = 0; cluster_index < m_color_clusters.size(); cluster_index++)
@@ -2138,7 +2138,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Total refined pixels: %u, endpoints: %u out of %u", total_refined_pixels, total_refined_tiles, m_color_clusters.size());
console::info(L"Total refined pixels: %u, endpoints: %u out of %u", total_refined_pixels, total_refined_tiles, m_color_clusters.size());
#endif
return true;
@@ -2153,7 +2153,7 @@ namespace crnlib
uint total_refined_pixels = 0;
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Refining quantized alpha endpoints");
console::info(L"Refining quantized alpha endpoints");
#endif
for (uint cluster_index = 0; cluster_index < m_alpha_clusters.size(); cluster_index++)
@@ -2257,7 +2257,7 @@ namespace crnlib
#if CRNLIB_ENABLE_DEBUG_MESSAGES
if (m_params.m_debugging)
console::info("Total refined pixels: %u, endpoints: %u out of %u", total_refined_pixels, total_refined_tiles, m_alpha_clusters.size());
console::info(L"Total refined pixels: %u, endpoints: %u out of %u", total_refined_pixels, total_refined_tiles, m_alpha_clusters.size());
#endif
return true;
@@ -2504,7 +2504,7 @@ namespace crnlib
{
const chunk_tile_desc &tile_desc = encoding_desc.m_tiles[t];
img.unclipped_fill_box(
img.draw_box(
x*8 + tile_desc.m_x_ofs, y*8 + tile_desc.m_y_ofs,
tile_desc.m_width + 1, tile_desc.m_height + 1, color_quad_u8(128, 128, 128, 255));
}
@@ -2515,7 +2515,7 @@ namespace crnlib
bool dxt_hc::update_progress(uint phase_index, uint subphase_index, uint subphase_total)
{
CRNLIB_ASSERT(crn_get_current_thread_id() == m_main_thread_id);
CRNLIB_ASSERT(get_current_thread_id() == m_main_thread_id);
if (!m_params.m_pProgress_func)
return true;
+7 -6
View File
@@ -9,7 +9,8 @@
#include "crn_image.h"
#include "crn_dxt_hc_common.h"
#include "crn_tree_clusterizer.h"
#include "crn_threading.h"
#include "crn_task_pool.h"
#include "crn_spinlock.h"
#define CRN_NO_FUNCTION_DEFINITIONS
#include "../inc/crnlib.h"
@@ -162,8 +163,8 @@ namespace crnlib
uint8 m_selectors[cBlockPixelHeight][cBlockPixelWidth];
uint8 get_by_index(uint i) const { CRNLIB_ASSERT(i < (cBlockPixelWidth * cBlockPixelHeight)); const uint8* p = (const uint8*)m_selectors; return *(p + i); }
void set_by_index(uint i, uint v) { CRNLIB_ASSERT(i < (cBlockPixelWidth * cBlockPixelHeight)); uint8* p = (uint8*)m_selectors; *(p + i) = static_cast<uint8>(v); }
uint8 get_by_index(uint i) const { CRNLIB_ASSERT(i < (cBlockPixelWidth * cBlockPixelHeight)); return *(&m_selectors[0][0] + i); }
void set_by_index(uint i, uint v) { CRNLIB_ASSERT(i < (cBlockPixelWidth * cBlockPixelHeight)); *(&m_selectors[0][0] + i) = static_cast<uint8>(v); }
};
typedef crnlib::vector<selectors> selectors_vec;
@@ -271,9 +272,9 @@ namespace crnlib
};
compressed_chunk_vec m_compressed_chunks[cNumCompressedChunkVecs];
volatile atomic32_t m_encoding_hist[cNumChunkEncodings];
int32 m_encoding_hist[cNumChunkEncodings];
atomic32_t m_total_tiles;
int32 m_total_tiles;
void compress_dxt1_block(
dxt1_endpoint_optimizer::results& results,
@@ -349,7 +350,7 @@ namespace crnlib
pixel_chunk_vec m_dbg_chunk_pixels_final;
crn_thread_id_t m_main_thread_id;
uint32 m_main_thread_id;
bool m_canceled;
task_pool* m_pTask_pool;
+60 -479
View File
@@ -7,8 +7,8 @@
#endif
#include "crn_ryg_dxt.hpp"
#include "crn_dxt_fast.h"
#include "crn_task_pool.h"
#include "crn_console.h"
#include "crn_threading.h"
#if CRNLIB_SUPPORT_ATI_COMPRESS
#ifdef _DLL
@@ -19,10 +19,6 @@
#include "..\ext\ATI_Compress\ATI_Compress.h"
#endif
#include "crn_rg_etc1.h"
#include "crn_etc.h"
#define CRNLIB_USE_RG_ETC1 1
namespace crnlib
{
dxt_image::dxt_image() :
@@ -107,13 +103,12 @@ namespace crnlib
m_blocks_y = (m_height + 3) >> cDXTBlockShift;
m_num_elements_per_block = 2;
if ((fmt == cDXT1) || (fmt == cDXT1A) || (fmt == cDXT5A) || (fmt == cETC1))
if ((fmt == cDXT1) || (fmt == cDXT1A) || (fmt == cDXT5A))
m_num_elements_per_block = 1;
m_total_blocks = m_blocks_x * m_blocks_y;
m_total_elements = m_total_blocks * m_num_elements_per_block;
CRNLIB_ASSUME((uint)cDXT1BytesPerBlock == (uint)cETC1BytesPerBlock);
m_bytes_per_block = cDXT1BytesPerBlock * m_num_elements_per_block;
m_format = fmt;
@@ -123,54 +118,48 @@ namespace crnlib
case cDXT1:
case cDXT1A:
{
m_element_type[0] = cColorDXT1;
m_element_type[0] = cColor;
m_element_component_index[0] = -1;
break;
}
case cDXT3:
{
m_element_type[0] = cAlphaDXT3;
m_element_type[1] = cColorDXT1;
m_element_type[0] = cAlpha3;
m_element_type[1] = cColor;
m_element_component_index[0] = 3;
m_element_component_index[1] = -1;
break;
}
case cDXT5:
{
m_element_type[0] = cAlphaDXT5;
m_element_type[1] = cColorDXT1;
m_element_type[0] = cAlpha5;
m_element_type[1] = cColor;
m_element_component_index[0] = 3;
m_element_component_index[1] = -1;
break;
}
case cDXT5A:
{
m_element_type[0] = cAlphaDXT5;
m_element_type[0] = cAlpha5;
m_element_component_index[0] = 3;
break;
}
case cDXN_XY:
{
m_element_type[0] = cAlphaDXT5;
m_element_type[1] = cAlphaDXT5;
m_element_type[0] = cAlpha5;
m_element_type[1] = cAlpha5;
m_element_component_index[0] = 0;
m_element_component_index[1] = 1;
break;
}
case cDXN_YX:
{
m_element_type[0] = cAlphaDXT5;
m_element_type[1] = cAlphaDXT5;
m_element_type[0] = cAlpha5;
m_element_type[1] = cAlpha5;
m_element_component_index[0] = 1;
m_element_component_index[1] = 0;
break;
}
case cETC1:
{
m_element_type[0] = cColorETC1;
m_element_component_index[0] = -1;
break;
}
default:
{
CRNLIB_ASSERT(0);
@@ -227,8 +216,8 @@ namespace crnlib
dxt_format m_fmt;
const image_u8* m_pImg;
const dxt_image::pack_params* m_pParams;
crn_thread_id_t m_main_thread;
atomic32_t m_canceled;
uint32 m_main_thread;
int32 m_canceled;
};
void dxt_image::init_task(uint64 data, void* pData_ptr)
@@ -238,11 +227,12 @@ namespace crnlib
const image_u8& img = *pInit_params->m_pImg;
const pack_params& p = *pInit_params->m_pParams;
const bool is_main_thread = (crn_get_current_thread_id() == pInit_params->m_main_thread);
const bool is_main_thread = (get_current_thread_id() == pInit_params->m_main_thread);
uint block_index = 0;
set_block_pixels_context optimizer_context;
dxt1_endpoint_optimizer dxt1_optimizer;
dxt5_endpoint_optimizer dxt5_optimizer;
int prev_progress_percentage = -1;
for (uint block_y = 0; block_y < m_blocks_y; block_y++)
@@ -262,7 +252,7 @@ namespace crnlib
prev_progress_percentage = progress_percentage;
if (!(p.m_pProgress_callback)(progress_percentage, p.m_pProgress_callback_user_data_ptr))
{
atomic_exchange32(&pInit_params->m_canceled, CRNLIB_TRUE);
interlocked_exchange32(&pInit_params->m_canceled, CRNLIB_TRUE);
return;
}
}
@@ -290,7 +280,7 @@ namespace crnlib
}
}
set_block_pixels(block_x, block_y, pixels, p, optimizer_context);
set_block_pixels(block_x, block_y, pixels, p, dxt1_optimizer, dxt5_optimizer);
}
}
}
@@ -361,7 +351,7 @@ namespace crnlib
if (fmt == cDXT1A)
{
options.bDXT1UseAlpha = true;
options.bDXT1UseAlpha = TRUE;
options.nAlphaThreshold = (ATI_TC_BYTE)p.m_dxt1a_alpha_threshold;
}
options.bDisableMultiThreading = (p.m_num_helper_threads == 0);
@@ -380,7 +370,7 @@ namespace crnlib
if (p.m_perceptual)
{
options.bUseChannelWeighting = true;
options.bUseChannelWeighting = TRUE;
options.fWeightingRed = .212671f;
options.fWeightingGreen = .715160f;
options.fWeightingBlue = .072169f;
@@ -415,7 +405,7 @@ namespace crnlib
init_params.m_fmt = fmt;
init_params.m_pImg = &img;
init_params.m_pParams = &p;
init_params.m_main_thread = crn_get_current_thread_id();
init_params.m_main_thread = get_current_thread_id();
init_params.m_canceled = false;
for (uint i = 0; i <= p.m_num_helper_threads; i++)
@@ -440,7 +430,6 @@ namespace crnlib
for (uint i = 0; i < cDXTBlockSize * cDXTBlockSize; i++)
pixels[i].set(0, 0, 0, 255);
bool all_blocks_valid = true;
for (uint block_y = 0; block_y < m_blocks_y; block_y++)
{
const uint pixel_ofs_y = block_y * cDXTBlockSize;
@@ -448,8 +437,7 @@ namespace crnlib
for (uint block_x = 0; block_x < m_blocks_x; block_x++)
{
if (!get_block_pixels(block_x, block_y, pixels))
all_blocks_valid = false;
get_block_pixels(block_x, block_y, pixels);
const uint pixel_ofs_x = block_x * cDXTBlockSize;
@@ -469,9 +457,6 @@ namespace crnlib
}
}
if (!all_blocks_valid)
console::error("dxt_image::unpack: One or more invalid blocks encountered!");
img.reset_comp_flags();
img.set_component_valid(0, false);
img.set_component_valid(1, false);
@@ -557,53 +542,7 @@ namespace crnlib
{
switch (m_element_type[element_index])
{
case cColorETC1:
{
const etc1_block& block = *reinterpret_cast<const etc1_block*>(&get_element(block_x, block_y, element_index));
const bool diff_flag = block.get_diff_bit();
const bool flip_flag = block.get_flip_bit();
const uint table_index0 = block.get_inten_table(0);
const uint table_index1 = block.get_inten_table(1);
color_quad_u8 subblock_colors0[4], subblock_colors1[4];
if (diff_flag)
{
const uint16 base_color5 = block.get_base5_color();
const uint16 delta_color3 = block.get_delta3_color();
etc1_block::get_diff_subblock_colors(subblock_colors0, base_color5, table_index0);
etc1_block::get_diff_subblock_colors(subblock_colors1, base_color5, delta_color3, table_index1);
}
else
{
const uint16 base_color4_0 = block.get_base4_color(0);
etc1_block::get_abs_subblock_colors(subblock_colors0, base_color4_0, table_index0);
const uint16 base_color4_1 = block.get_base4_color(1);
etc1_block::get_abs_subblock_colors(subblock_colors1, base_color4_1, table_index1);
}
const uint bx = x & 3;
const uint by = y & 3;
const uint selector_index = block.get_selector(bx, by);
if (flip_flag)
{
if (by <= 2)
result = subblock_colors0[selector_index];
else
result = subblock_colors1[selector_index];
}
else
{
if (bx <= 2)
result = subblock_colors0[selector_index];
else
result = subblock_colors1[selector_index];
}
break;
}
case cColorDXT1:
case cColor:
{
const dxt1_block* pBlock = reinterpret_cast<const dxt1_block*>(&get_element(block_x, block_y, element_index));
@@ -645,7 +584,7 @@ namespace crnlib
break;
}
case cAlphaDXT5:
case cAlpha5:
{
const int comp_index = m_element_component_index[element_index];
@@ -687,7 +626,7 @@ namespace crnlib
break;
}
case cAlphaDXT3:
case cAlpha3:
{
const int comp_index = m_element_component_index[element_index];
@@ -713,7 +652,7 @@ namespace crnlib
switch (m_element_type[element_index])
{
case cColorDXT1:
case cColor:
{
if (m_format <= cDXT1A)
{
@@ -736,7 +675,7 @@ namespace crnlib
break;
}
case cAlphaDXT5:
case cAlpha5:
{
const dxt5_block* pBlock = reinterpret_cast<const dxt5_block*>(&get_element(block_x, block_y, element_index));
@@ -774,7 +713,7 @@ namespace crnlib
}
}
}
case cAlphaDXT3:
case cAlpha3:
{
const dxt3_block* pBlock = reinterpret_cast<const dxt3_block*>(&get_element(block_x, block_y, element_index));
@@ -799,63 +738,7 @@ namespace crnlib
{
switch (m_element_type[element_index])
{
case cColorETC1:
{
etc1_block& block = *reinterpret_cast<etc1_block*>(&get_element(block_x, block_y, element_index));
const bool diff_flag = block.get_diff_bit();
const bool flip_flag = block.get_flip_bit();
const uint table_index0 = block.get_inten_table(0);
const uint table_index1 = block.get_inten_table(1);
color_quad_u8 subblock_colors0[4], subblock_colors1[4];
if (diff_flag)
{
const uint16 base_color5 = block.get_base5_color();
const uint16 delta_color3 = block.get_delta3_color();
etc1_block::get_diff_subblock_colors(subblock_colors0, base_color5, table_index0);
etc1_block::get_diff_subblock_colors(subblock_colors1, base_color5, delta_color3, table_index1);
}
else
{
const uint16 base_color4_0 = block.get_base4_color(0);
etc1_block::get_abs_subblock_colors(subblock_colors0, base_color4_0, table_index0);
const uint16 base_color4_1 = block.get_base4_color(1);
etc1_block::get_abs_subblock_colors(subblock_colors1, base_color4_1, table_index1);
}
const uint bx = x & 3;
const uint by = y & 3;
color_quad_u8* pColors = subblock_colors1;
if (flip_flag)
{
if (by <= 2)
pColors = subblock_colors0;
}
else
{
if (bx <= 2)
pColors = subblock_colors0;
}
uint best_error = UINT_MAX;
uint best_selector = 0;
for (uint i = 0; i < 4; i++)
{
uint error = color::color_distance(perceptual, pColors[i], c, false);
if (error < best_error)
{
best_error = error;
best_selector = i;
}
}
block.set_selector(bx, by, best_selector);
break;
}
case cColorDXT1:
case cColor:
{
dxt1_block* pDXT1_block = reinterpret_cast<dxt1_block*>(pElement);
@@ -884,7 +767,7 @@ namespace crnlib
break;
}
case cAlphaDXT5:
case cAlpha5:
{
dxt5_block* pDXT5_block = reinterpret_cast<dxt5_block*>(pElement);
@@ -911,7 +794,7 @@ namespace crnlib
break;
}
case cAlphaDXT3:
case cAlpha3:
{
const int comp_index = m_element_component_index[element_index];
@@ -926,30 +809,15 @@ namespace crnlib
} // element_index
}
bool dxt_image::get_block_pixels(uint block_x, uint block_y, color_quad_u8* pPixels) const
void dxt_image::get_block_pixels(uint block_x, uint block_y, color_quad_u8* pPixels) const
{
bool success = true;
const element* pElement = &get_element(block_x, block_y, 0);
for (uint element_index = 0; element_index < m_num_elements_per_block; element_index++, pElement++)
{
switch (m_element_type[element_index])
{
case cColorETC1:
{
const etc1_block& block = *reinterpret_cast<const etc1_block*>(&get_element(block_x, block_y, element_index));
// Preserve alpha if the format is something weird (like ETC1 for color and DXT5A for alpha) - which isn't currently supported.
#if CRNLIB_USE_RG_ETC1
if (!rg_etc1::unpack_etc1_block(&block, (uint32*)pPixels, m_format != cETC1))
success = false;
#else
if (!unpack_etc1(block, pPixels, m_format != cETC1))
success = false;
#endif
break;
}
case cColorDXT1:
case cColor:
{
const dxt1_block* pDXT1_block = reinterpret_cast<const dxt1_block*>(pElement);
@@ -970,7 +838,7 @@ namespace crnlib
break;
}
case cAlphaDXT5:
case cAlpha5:
{
const dxt5_block* pDXT5_block = reinterpret_cast<const dxt5_block*>(pElement);
@@ -988,7 +856,7 @@ namespace crnlib
break;
}
case cAlphaDXT3:
case cAlpha3:
{
const dxt3_block* pDXT3_block = reinterpret_cast<const dxt3_block*>(pElement);
@@ -1006,53 +874,21 @@ namespace crnlib
default: break;
}
} // element_index
return success;
}
void dxt_image::set_block_pixels(uint block_x, uint block_y, const color_quad_u8* pPixels, const pack_params& p)
{
set_block_pixels_context context;
set_block_pixels(block_x, block_y, pPixels, p, context);
dxt1_endpoint_optimizer dxt1_optimizer;
dxt5_endpoint_optimizer dxt5_optimizer;
set_block_pixels(block_x, block_y, pPixels, p, dxt1_optimizer, dxt5_optimizer);
}
void dxt_image::set_block_pixels(
uint block_x, uint block_y, const color_quad_u8* pPixels, const pack_params& p,
set_block_pixels_context& context)
dxt1_endpoint_optimizer& dxt1_optimizer, dxt5_endpoint_optimizer& dxt5_optimizer)
{
element* pElement = &get_element(block_x, block_y, 0);
if (m_format == cETC1)
{
etc1_block &dst_block = *reinterpret_cast<etc1_block*>(pElement);
#if CRNLIB_USE_RG_ETC1
rg_etc1::etc1_quality etc_quality = rg_etc1::cHighQuality;
if (p.m_quality <= cCRNDXTQualityFast)
etc_quality = rg_etc1::cLowQuality;
else if (p.m_quality <= cCRNDXTQualityNormal)
etc_quality = rg_etc1::cMediumQuality;
rg_etc1::etc1_pack_params pack_params;
pack_params.m_dithering = p.m_dithering;
//pack_params.m_perceptual = p.m_perceptual;
pack_params.m_quality = etc_quality;
rg_etc1::pack_etc1_block(&dst_block, (uint32*)pPixels, pack_params);
#else
crn_etc_quality etc_quality = cCRNETCQualitySlow;
if (p.m_quality <= cCRNDXTQualityFast)
etc_quality = cCRNETCQualityFast;
else if (p.m_quality <= cCRNDXTQualityNormal)
etc_quality = cCRNETCQualityMedium;
crn_etc1_pack_params pack_params;
pack_params.m_perceptual = p.m_perceptual;
pack_params.m_quality = etc_quality;
pack_params.m_dithering = p.m_dithering;
pack_etc1_block(dst_block, pPixels, pack_params, context.m_etc1_optimizer);
#endif
}
else
#if CRNLIB_SUPPORT_SQUISH
if ((p.m_compressor == cCRNDXTCompressorSquish) && ((m_format == cDXT1) || (m_format == cDXT1A) || (m_format == cDXT3) || (m_format == cDXT5) || (m_format == cDXT5A)))
{
@@ -1127,21 +963,21 @@ namespace crnlib
{
switch (m_element_type[element_index])
{
case cColorDXT1:
case cColor:
{
dxt1_block* pDXT1_block = reinterpret_cast<dxt1_block*>(pElement);
dxt_fast::compress_color_block(pDXT1_block, pPixels, p.m_quality >= cCRNDXTQualityNormal);
break;
}
case cAlphaDXT5:
case cAlpha5:
{
dxt5_block* pDXT5_block = reinterpret_cast<dxt5_block*>(pElement);
dxt_fast::compress_alpha_block(pDXT5_block, pPixels, m_element_component_index[element_index]);
break;
}
case cAlphaDXT3:
case cAlpha3:
{
const int comp_index = m_element_component_index[element_index];
@@ -1158,14 +994,11 @@ namespace crnlib
}
else
{
dxt1_endpoint_optimizer& dxt1_optimizer = context.m_dxt1_optimizer;
dxt5_endpoint_optimizer& dxt5_optimizer = context.m_dxt5_optimizer;
for (uint element_index = 0; element_index < m_num_elements_per_block; element_index++, pElement++)
{
switch (m_element_type[element_index])
{
case cColorDXT1:
case cColor:
{
dxt1_block* pDXT1_block = reinterpret_cast<dxt1_block*>(pElement);
@@ -1217,7 +1050,7 @@ namespace crnlib
break;
}
case cAlphaDXT5:
case cAlpha5:
{
dxt5_block* pDXT5_block = reinterpret_cast<dxt5_block*>(pElement);
@@ -1248,7 +1081,7 @@ namespace crnlib
break;
}
case cAlphaDXT3:
case cAlpha3:
{
const int comp_index = m_element_component_index[element_index];
@@ -1271,23 +1104,7 @@ namespace crnlib
switch (m_element_type[element_index])
{
case cColorETC1:
{
const etc1_block& src_block = *reinterpret_cast<const etc1_block*>(&block);
if (src_block.get_diff_bit())
{
packed_low_endpoint = src_block.get_base5_color();
packed_high_endpoint = src_block.get_delta3_color();
}
else
{
packed_low_endpoint = src_block.get_base4_color(0);
packed_high_endpoint = src_block.get_base4_color(1);
}
break;
}
case cColorDXT1:
case cColor:
{
const dxt1_block& block1 = *reinterpret_cast<const dxt1_block*>(&block);
@@ -1296,7 +1113,7 @@ namespace crnlib
break;
}
case cAlphaDXT5:
case cAlpha5:
{
const dxt5_block& block5 = *reinterpret_cast<const dxt5_block*>(&block);
@@ -1305,7 +1122,7 @@ namespace crnlib
break;
}
case cAlphaDXT3:
case cAlpha3:
{
packed_low_endpoint = 0;
packed_high_endpoint = 255;
@@ -1318,28 +1135,12 @@ namespace crnlib
int dxt_image::get_block_endpoints(uint block_x, uint block_y, uint element_index, color_quad_u8& low_endpoint, color_quad_u8& high_endpoint, bool scaled) const
{
uint l = 0, h = 0;
uint l, h;
get_block_endpoints(block_x, block_y, element_index, l, h);
switch (m_element_type[element_index])
{
case cColorETC1:
{
const etc1_block& src_block = *reinterpret_cast<const etc1_block*>(&get_element(block_x, block_y, element_index));
if (src_block.get_diff_bit())
{
low_endpoint = etc1_block::unpack_color5(static_cast<uint16>(l), scaled);
etc1_block::unpack_color5(high_endpoint, static_cast<uint16>(l), static_cast<uint16>(h), scaled);
}
else
{
low_endpoint = etc1_block::unpack_color4(static_cast<uint16>(l), scaled);
high_endpoint = etc1_block::unpack_color4(static_cast<uint16>(h), scaled);
}
return -1;
}
case cColorDXT1:
case cColor:
{
uint r, g, b;
@@ -1355,7 +1156,7 @@ namespace crnlib
return -1;
}
case cAlphaDXT5:
case cAlpha5:
{
const int component = m_element_component_index[element_index];
@@ -1364,7 +1165,7 @@ namespace crnlib
return component;
}
case cAlphaDXT3:
case cAlpha3:
{
const int component = m_element_component_index[element_index];
@@ -1379,48 +1180,18 @@ namespace crnlib
return 0;
}
uint dxt_image::get_block_colors(uint block_x, uint block_y, uint element_index, color_quad_u8* pColors, uint subblock_index)
uint dxt_image::get_block_colors(uint block_x, uint block_y, uint element_index, color_quad_u8* pColors)
{
const element& block = get_element(block_x, block_y, element_index);
switch (m_element_type[element_index])
{
case cColorETC1:
{
const etc1_block& src_block = *reinterpret_cast<const etc1_block*>(&get_element(block_x, block_y, element_index));
const uint table_index0 = src_block.get_inten_table(0);
const uint table_index1 = src_block.get_inten_table(1);
if (src_block.get_diff_bit())
{
const uint16 base_color5 = src_block.get_base5_color();
const uint16 delta_color3 = src_block.get_delta3_color();
if (subblock_index)
etc1_block::get_diff_subblock_colors(pColors, base_color5, delta_color3, table_index1);
else
etc1_block::get_diff_subblock_colors(pColors, base_color5, table_index0);
}
else
{
if (subblock_index)
{
const uint16 base_color4_1 = src_block.get_base4_color(1);
etc1_block::get_abs_subblock_colors(pColors, base_color4_1, table_index1);
}
else
{
const uint16 base_color4_0 = src_block.get_base4_color(0);
etc1_block::get_abs_subblock_colors(pColors, base_color4_0, table_index0);
}
}
break;
}
case cColorDXT1:
case cColor:
{
const dxt1_block& block1 = *reinterpret_cast<const dxt1_block*>(&block);
return dxt1_block::get_block_colors(pColors, static_cast<uint16>(block1.get_low_color()), static_cast<uint16>(block1.get_high_color()));
}
case cAlphaDXT5:
case cAlpha5:
{
const dxt5_block& block5 = *reinterpret_cast<const dxt5_block*>(&block);
@@ -1434,7 +1205,7 @@ namespace crnlib
return n;
}
case cAlphaDXT3:
case cAlpha3:
{
const int comp_index = m_element_component_index[element_index];
for (uint i = 0; i < 16; i++)
@@ -1448,32 +1219,6 @@ namespace crnlib
return 0;
}
uint dxt_image::get_subblock_index(uint x, uint y, uint element_index) const
{
if (m_element_type[element_index] != cColorETC1)
return 0;
const uint block_x = x >> cDXTBlockShift;
const uint block_y = y >> cDXTBlockShift;
const element& block = get_element(block_x, block_y, element_index);
const etc1_block& src_block = *reinterpret_cast<const etc1_block*>(&block);
if (src_block.get_flip_bit())
{
return ((y & 3) >= 2) ? 1 : 0;
}
else
{
return ((x & 3) >= 2) ? 1 : 0;
}
}
uint dxt_image::get_total_subblocks(uint element_index) const
{
return (m_element_type[element_index] == cColorETC1) ? 2 : 0;
}
uint dxt_image::get_selector(uint x, uint y, uint element_index) const
{
CRNLIB_ASSERT((x < m_width) && (y < m_height));
@@ -1485,22 +1230,17 @@ namespace crnlib
switch (m_element_type[element_index])
{
case cColorETC1:
{
const etc1_block& src_block = *reinterpret_cast<const etc1_block*>(&block);
return src_block.get_selector(x & 3, y & 3);
}
case cColorDXT1:
case cColor:
{
const dxt1_block& block1 = *reinterpret_cast<const dxt1_block*>(&block);
return block1.get_selector(x & 3, y & 3);
}
case cAlphaDXT5:
case cAlpha5:
{
const dxt5_block& block5 = *reinterpret_cast<const dxt5_block*>(&block);
return block5.get_selector(x & 3, y & 3);
}
case cAlphaDXT3:
case cAlpha3:
{
const dxt3_block& block3 = *reinterpret_cast<const dxt3_block*>(&block);
return block3.get_alpha(x & 3, y & 3, false);
@@ -1517,165 +1257,6 @@ namespace crnlib
m_format = cDXT1A;
}
void dxt_image::flip_col(uint x)
{
const uint other_x = (m_blocks_x - 1) - x;
for (uint y = 0; y < m_blocks_y; y++)
{
for (uint e = 0; e < get_elements_per_block(); e++)
{
element tmp[2] = { get_element(x, y, e), get_element(other_x, y, e) };
for (uint i = 0; i < 2; i++)
{
switch (get_element_type(e))
{
case cColorDXT1: reinterpret_cast<dxt1_block*>(&tmp[i])->flip_x(); break;
case cAlphaDXT3: reinterpret_cast<dxt3_block*>(&tmp[i])->flip_x(); break;
case cAlphaDXT5: reinterpret_cast<dxt5_block*>(&tmp[i])->flip_x(); break;
default: CRNLIB_ASSERT(0); break;
}
}
get_element(x, y, e) = tmp[1];
get_element(other_x, y, e) = tmp[0];
}
}
}
void dxt_image::flip_row(uint y)
{
const uint other_y = (m_blocks_y - 1) - y;
for (uint x = 0; x < m_blocks_x; x++)
{
for (uint e = 0; e < get_elements_per_block(); e++)
{
element tmp[2] = { get_element(x, y, e), get_element(x, other_y, e) };
for (uint i = 0; i < 2; i++)
{
switch (get_element_type(e))
{
case cColorDXT1: reinterpret_cast<dxt1_block*>(&tmp[i])->flip_y(); break;
case cAlphaDXT3: reinterpret_cast<dxt3_block*>(&tmp[i])->flip_y(); break;
case cAlphaDXT5: reinterpret_cast<dxt5_block*>(&tmp[i])->flip_y(); break;
default: CRNLIB_ASSERT(0); break;
}
}
get_element(x, y, e) = tmp[1];
get_element(x, other_y, e) = tmp[0];
}
}
}
bool dxt_image::can_flip(uint axis_index)
{
if (m_format == cETC1)
{
// Can't reliably flip ETC1 textures (because of asymmetry in the 555/333 differential coding of subblock colors).
return false;
}
uint d;
if (axis_index)
d = m_height;
else
d = m_width;
if (d & 3)
{
if (d > 4)
return false;
}
return true;
}
bool dxt_image::flip_x()
{
if (m_format == cETC1)
{
// Can't reliably flip ETC1 textures (because of asymmetry in the 555/333 differential coding of subblock colors).
return false;
}
if ((m_width & 3) && (m_width > 4))
return false;
if (m_width == 1)
return true;
const uint mid_x = m_blocks_x / 2;
for (uint x = 0; x < mid_x; x++)
flip_col(x);
if (m_blocks_x & 1)
{
const uint w = math::minimum(m_width, 4U);
for (uint y = 0; y < m_blocks_y; y++)
{
for (uint e = 0; e < get_elements_per_block(); e++)
{
element tmp(get_element(mid_x, y, e));
switch (get_element_type(e))
{
case cColorDXT1: reinterpret_cast<dxt1_block*>(&tmp)->flip_x(w, 4); break;
case cAlphaDXT3: reinterpret_cast<dxt3_block*>(&tmp)->flip_x(w, 4); break;
case cAlphaDXT5: reinterpret_cast<dxt5_block*>(&tmp)->flip_x(w, 4); break;
default: CRNLIB_ASSERT(0); break;
}
get_element(mid_x, y, e) = tmp;
}
}
}
return true;
}
bool dxt_image::flip_y()
{
if (m_format == cETC1)
{
// Can't reliably flip ETC1 textures (because of asymmetry in the 555/333 differential coding of subblock colors).
return false;
}
if ((m_height & 3) && (m_height > 4))
return false;
if (m_height == 1)
return true;
const uint mid_y = m_blocks_y / 2;
for (uint y = 0; y < mid_y; y++)
flip_row(y);
if (m_blocks_y & 1)
{
const uint h = math::minimum(m_height, 4U);
for (uint x = 0; x < m_blocks_x; x++)
{
for (uint e = 0; e < get_elements_per_block(); e++)
{
element tmp(get_element(x, mid_y, e));
switch (get_element_type(e))
{
case cColorDXT1: reinterpret_cast<dxt1_block*>(&tmp)->flip_y(4, h); break;
case cAlphaDXT3: reinterpret_cast<dxt3_block*>(&tmp)->flip_y(4, h); break;
case cAlphaDXT5: reinterpret_cast<dxt5_block*>(&tmp)->flip_y(4, h); break;
default: CRNLIB_ASSERT(0); break;
}
get_element(x, mid_y, e) = tmp;
}
}
}
return true;
}
} // namespace crnlib
+11 -41
View File
@@ -3,10 +3,6 @@
#pragma once
#include "crn_dxt1.h"
#include "crn_dxt5a.h"
#include "crn_etc.h"
#if CRNLIB_SUPPORT_ETC_A1
#include "crn_etc_a1.h"
#endif
#include "crn_image.h"
#define CRNLIB_SUPPORT_ATI_COMPRESS 0
@@ -38,7 +34,7 @@ namespace crnlib
dxt_format get_format() const { return m_format; }
bool has_color() const { return (m_format == cDXT1) || (m_format == cDXT1A) || (m_format == cDXT3) || (m_format == cDXT5) || (m_format == cETC1); }
bool has_color() const { return (m_format == cDXT1) || (m_format == cDXT1A) || (m_format == cDXT3) || (m_format == cDXT5); }
// Will be pretty slow if the image is DXT1, as this method scans for alpha blocks/selectors.
bool has_alpha() const;
@@ -47,12 +43,10 @@ namespace crnlib
{
cUnused = 0,
cColorDXT1, // DXT1 color block
cColor,
cAlphaDXT3, // DXT3 alpha block (only)
cAlphaDXT5, // DXT5 alpha block (only)
cColorETC1, // ETC1 color block
cAlpha3,
cAlpha5,
};
element_type get_element_type(uint element_index) const { CRNLIB_ASSERT(element_index < m_num_elements_per_block); return m_element_type[element_index]; }
@@ -67,8 +61,8 @@ namespace crnlib
uint get_le_word(uint index) const { CRNLIB_ASSERT(index < 4); return m_bytes[index*2] | (m_bytes[index * 2 + 1] << 8); }
uint get_be_word(uint index) const { CRNLIB_ASSERT(index < 4); return m_bytes[index*2 + 1] | (m_bytes[index * 2] << 8); }
void set_le_word(uint index, uint val) { CRNLIB_ASSERT((index < 4) && (val <= cUINT16_MAX)); m_bytes[index*2] = static_cast<uint8>(val & 0xFF); m_bytes[index * 2 + 1] = static_cast<uint8>((val >> 8) & 0xFF); }
void set_be_word(uint index, uint val) { CRNLIB_ASSERT((index < 4) && (val <= cUINT16_MAX)); m_bytes[index*2+1] = static_cast<uint8>(val & 0xFF); m_bytes[index * 2] = static_cast<uint8>((val >> 8) & 0xFF); }
void set_le_word(uint index, uint val) { CRNLIB_ASSERT((index < 4) && (val <= UINT16_MAX)); m_bytes[index*2] = static_cast<uint8>(val & 0xFF); m_bytes[index * 2 + 1] = static_cast<uint8>((val >> 8) & 0xFF); }
void set_be_word(uint index, uint val) { CRNLIB_ASSERT((index < 4) && (val <= UINT16_MAX)); m_bytes[index*2+1] = static_cast<uint8>(val & 0xFF); m_bytes[index * 2] = static_cast<uint8>((val >> 8) & 0xFF); }
void clear()
{
@@ -92,7 +86,6 @@ namespace crnlib
{
m_quality = cCRNDXTQualityUber;
m_perceptual = true;
m_dithering = false;
m_grayscale_sampling = false;
m_use_both_block_types = true;
m_endpoint_caching = true;
@@ -132,7 +125,6 @@ namespace crnlib
crn_dxt_compressor_type m_compressor;
bool m_perceptual;
bool m_dithering;
bool m_grayscale_sampling;
bool m_use_both_block_types;
bool m_endpoint_caching;
@@ -156,7 +148,7 @@ namespace crnlib
void endian_swap();
uint get_total_elements() const { return m_elements.size(); }
uint get_num_elements() const { return m_elements.size(); }
const element_vec& get_element_vec() const { return m_elements; }
element_vec& get_element_vec() { return m_elements; }
@@ -176,19 +168,9 @@ namespace crnlib
void set_pixel(uint x, uint y, const color_quad_u8& c, bool perceptual = true);
// get_block_pixels() only sets those components stored in the image!
bool get_block_pixels(uint block_x, uint block_y, color_quad_u8* pPixels) const;
struct set_block_pixels_context
{
dxt1_endpoint_optimizer m_dxt1_optimizer;
dxt5_endpoint_optimizer m_dxt5_optimizer;
pack_etc1_block_context m_etc1_optimizer;
#if CRNLIB_SUPPORT_ETC_A1
etc_a1::pack_etc1_block_context m_etc1_a1_optimizer;
#endif
};
void get_block_pixels(uint block_x, uint block_y, color_quad_u8* pPixels) const;
void set_block_pixels(uint block_x, uint block_y, const color_quad_u8* pPixels, const pack_params& p, set_block_pixels_context& context);
void set_block_pixels(uint block_x, uint block_y, const color_quad_u8* pPixels, const pack_params& p, dxt1_endpoint_optimizer& dxt1_optimizer, dxt5_endpoint_optimizer& dxt5_optimizer);
void set_block_pixels(uint block_x, uint block_y, const color_quad_u8* pPixels, const pack_params& p);
void get_block_endpoints(uint block_x, uint block_y, uint element_index, uint& packed_low_endpoint, uint& packed_high_endpoint) const;
@@ -199,20 +181,11 @@ namespace crnlib
// pColors should point to a 16 entry array, to handle DXT3.
// Returns the number of block colors: 3, 4, 6, 8, or 16.
uint get_block_colors(uint block_x, uint block_y, uint element_index, color_quad_u8* pColors, uint subblock_index = 0);
uint get_subblock_index(uint x, uint y, uint element_index) const;
uint get_total_subblocks(uint element_index) const;
uint get_block_colors(uint block_x, uint block_y, uint element_index, color_quad_u8* pColors);
uint get_selector(uint x, uint y, uint element_index) const;
void change_dxt1_to_dxt1a();
bool can_flip(uint axis_index);
// Returns true if the texture can actually be flipped.
bool flip_x();
bool flip_y();
private:
element_vec m_elements;
@@ -232,7 +205,7 @@ namespace crnlib
int8 m_element_component_index[2];
element_type m_element_type[2];
dxt_format m_format; // DXT1, 1A, 3, 5, N/3DC, or 5A
dxt_format m_format; // DXT1, 1A, 3, 5, N/3DC, or A
bool init_internal(dxt_format fmt, uint width, uint height);
void init_task(uint64 data, void* pData_ptr);
@@ -240,9 +213,6 @@ namespace crnlib
#if CRNLIB_SUPPORT_ATI_COMPRESS
bool init_ati_compress(dxt_format fmt, const image_u8& img, const pack_params& p);
#endif
void flip_col(uint x);
void flip_row(uint y);
};
} // namespace crnlib
+53 -53
View File
@@ -8,32 +8,32 @@ namespace crnlib
class dynamic_stream : public data_stream
{
public:
dynamic_stream(uint initial_size, const char* pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable) :
dynamic_stream(uint initial_size, const wchar_t* pName = L"dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable) :
data_stream(pName, attribs),
m_ofs(0)
{
open(initial_size, pName, attribs);
}
dynamic_stream(const void* pBuf, uint size, const char* pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable) :
dynamic_stream(const void* pBuf, uint size, const wchar_t* pName = L"dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable) :
data_stream(pName, attribs),
m_ofs(0)
{
open(pBuf, size, pName, attribs);
}
dynamic_stream() :
dynamic_stream() :
data_stream(),
m_ofs(0)
{
open();
}
virtual ~dynamic_stream()
{
}
bool open(uint initial_size = 0, const char* pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
bool open(uint initial_size = 0, const wchar_t* pName = L"dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
{
close();
@@ -41,24 +41,24 @@ namespace crnlib
m_buf.clear();
m_buf.resize(initial_size);
m_ofs = 0;
m_name.set(pName ? pName : "dynamic_stream");
m_name.set(pName ? pName : L"dynamic_stream");
m_attribs = static_cast<attribs_t>(attribs);
return true;
}
bool reopen(const char* pName, uint attribs)
bool reopen(const wchar_t* pName, uint attribs)
{
if (!m_opened)
{
return open(0, pName, attribs);
}
m_name.set(pName ? pName : "dynamic_stream");
m_name.set(pName ? pName : L"dynamic_stream");
m_attribs = static_cast<attribs_t>(attribs);
return true;
}
bool open(const void* pBuf, uint size, const char* pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
bool open(const void* pBuf, uint size, const wchar_t* pName = L"dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
{
if (!m_opened)
{
@@ -70,14 +70,14 @@ namespace crnlib
memcpy(&m_buf[0], pBuf, size);
}
m_ofs = 0;
m_name.set(pName ? pName : "dynamic_stream");
m_name.set(pName ? pName : L"dynamic_stream");
m_attribs = static_cast<attribs_t>(attribs);
return true;
}
return false;
}
virtual bool close()
{
if (m_opened)
@@ -87,10 +87,10 @@ namespace crnlib
m_ofs = 0;
return true;
}
return false;
}
const crnlib::vector<uint8>& get_buf() const { return m_buf; }
crnlib::vector<uint8>& get_buf() { return m_buf; }
@@ -101,102 +101,102 @@ namespace crnlib
m_buf.reserve(size);
}
}
virtual const void* get_ptr() const { return m_buf.empty() ? NULL : &m_buf[0]; }
virtual uint read(void* pBuf, uint len)
{
CRNLIB_ASSERT(pBuf && (len <= 0x7FFFFFFF));
if ((!m_opened) || (!is_readable()) || (!len))
return 0;
CRNLIB_ASSERT(m_ofs <= m_buf.size());
uint bytes_left = m_buf.size() - m_ofs;
len = math::minimum<uint>(len, bytes_left);
if (len)
memcpy(pBuf, &m_buf[m_ofs], len);
m_ofs += len;
return len;
}
virtual uint write(const void* pBuf, uint len)
{
CRNLIB_ASSERT(pBuf && (len <= 0x7FFFFFFF));
if ((!m_opened) || (!is_writable()) || (!len))
return 0;
CRNLIB_ASSERT(m_ofs <= m_buf.size());
uint new_ofs = m_ofs + len;
if (new_ofs > m_buf.size())
m_buf.resize(new_ofs);
memcpy(&m_buf[m_ofs], pBuf, len);
m_ofs = new_ofs;
return len;
}
virtual bool flush()
{
if (!m_opened)
return false;
return true;
}
virtual uint64 get_size()
{
virtual uint64 get_size()
{
if (!m_opened)
return 0;
return m_buf.size();
return m_buf.size();
}
virtual uint64 get_remaining()
{
if (!m_opened)
return 0;
CRNLIB_ASSERT(m_ofs <= m_buf.size());
return m_buf.size() - m_ofs;
}
virtual uint64 get_ofs()
virtual uint64 get_ofs()
{
if (!m_opened)
return 0;
return m_ofs;
}
virtual bool seek(int64 ofs, bool relative)
virtual bool seek(int64 ofs, bool relative)
{
if ((!m_opened) || (!is_seekable()))
return false;
int64 new_ofs = relative ? (m_ofs + ofs) : ofs;
if (new_ofs < 0)
return false;
else if (new_ofs > m_buf.size())
return false;
m_ofs = static_cast<uint>(new_ofs);
post_seek();
return true;
}
private:
crnlib::vector<uint8> m_buf;
uint m_ofs;
+65 -38
View File
@@ -1,7 +1,10 @@
// File: crn_dynamic_string.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_strutils.h"
#include "crn_dynamic_string.h"
#include "crn_dynamic_wstring.h"
#include "crn_winhdr.h"
#include <stdio.h>
namespace crnlib
{
@@ -40,6 +43,51 @@ namespace crnlib
set(other);
}
dynamic_string::dynamic_string(const wchar_t* pStr) :
m_buf_size(0), m_len(0), m_pStr(NULL)
{
set(pStr);
}
dynamic_string& dynamic_string::set(const wchar_t *pStr)
{
uint len = static_cast<uint>(wcslen(pStr));
if (!len)
{
clear();
return *this;
}
const uint num_needed = WideCharToMultiByte(CP_ACP, 0, pStr, len, NULL, 0, NULL, NULL);
if (num_needed <= 0)
{
clear();
return *this;
}
if (!ensure_buf(num_needed, false))
{
clear();
return *this;
}
const uint num_written = WideCharToMultiByte(CP_ACP, 0, pStr, len, get_ptr_raw(), num_needed, NULL, NULL);
CRNLIB_ASSERT(num_written == num_needed);
get_ptr_raw()[num_written] = 0;
m_len = static_cast<uint16>(num_written);
check();
return *this;
}
dynamic_wstring& dynamic_string::as_utf16(dynamic_wstring &buf)
{
buf.set(get_ptr());
return buf;
}
void dynamic_string::clear()
{
check();
@@ -85,7 +133,7 @@ namespace crnlib
{
CRNLIB_ASSERT(p);
const int result = (case_sensitive ? strcmp : crn_stricmp)(get_ptr_priv(), p);
const int result = (case_sensitive ? strcmp : _stricmp)(get_ptr_priv(), p);
if (result < 0)
return -1;
@@ -105,9 +153,9 @@ namespace crnlib
CRNLIB_ASSERT(p);
const uint len = math::minimum<uint>(max_len, static_cast<uint>(strlen(p)));
CRNLIB_ASSERT(len < cUINT16_MAX);
CRNLIB_ASSERT(len < UINT16_MAX);
if ((!len) || (len >= cUINT16_MAX))
if ((!len) || (len >= UINT16_MAX))
clear();
else if ((m_pStr) && (p >= m_pStr) && (p < (m_pStr + m_buf_size)))
{
@@ -158,11 +206,8 @@ namespace crnlib
bool dynamic_string::set_len(uint new_len, char fill_char)
{
if ((new_len >= cUINT16_MAX) || (!fill_char))
{
CRNLIB_ASSERT(0);
if ((new_len >= UINT16_MAX) || (!fill_char))
return false;
}
uint cur_len = m_len;
@@ -181,41 +226,22 @@ namespace crnlib
return true;
}
dynamic_string& dynamic_string::set_from_raw_buf_and_assume_ownership(char *pBuf, uint buf_size_in_chars, uint len_in_chars)
{
CRNLIB_ASSERT(buf_size_in_chars <= cUINT16_MAX);
CRNLIB_ASSERT(math::is_power_of_2(buf_size_in_chars) || (buf_size_in_chars == cUINT16_MAX));
CRNLIB_ASSERT((len_in_chars + 1) <= buf_size_in_chars);
clear();
m_pStr = pBuf;
m_buf_size = static_cast<uint16>(buf_size_in_chars);
m_len = static_cast<uint16>(len_in_chars);
check();
return *this;
}
dynamic_string& dynamic_string::set_from_buf(const void* pBuf, uint buf_size)
{
CRNLIB_ASSERT(pBuf);
if (buf_size >= cUINT16_MAX)
if (buf_size >= UINT16_MAX)
{
clear();
return *this;
}
#ifdef CRNLIB_BUILD_DEBUG
if ((buf_size) && (memchr(pBuf, 0, buf_size) != NULL))
{
CRNLIB_ASSERT(0);
clear();
return *this;
}
#endif
if (ensure_buf(buf_size, false))
{
@@ -543,12 +569,9 @@ namespace crnlib
else
{
CRNLIB_ASSERT(m_buf_size);
CRNLIB_ASSERT((m_buf_size == cUINT16_MAX) || math::is_power_of_2((uint32)m_buf_size));
CRNLIB_ASSERT((m_buf_size == UINT16_MAX) || math::is_power_of_2((uint32)m_buf_size));
CRNLIB_ASSERT(m_len < m_buf_size);
CRNLIB_ASSERT(!m_pStr[m_len]);
#if CRNLIB_SLOW_STRING_LEN_CHECKS
CRNLIB_ASSERT(strlen(m_pStr) == m_len);
#endif
}
}
#endif
@@ -557,9 +580,9 @@ namespace crnlib
{
uint buf_size_needed = len + 1;
CRNLIB_ASSERT(buf_size_needed <= cUINT16_MAX);
CRNLIB_ASSERT(buf_size_needed <= UINT16_MAX);
if (buf_size_needed <= cUINT16_MAX)
if (buf_size_needed <= UINT16_MAX)
{
if (buf_size_needed > m_buf_size)
expand_buf(buf_size_needed, preserve_contents);
@@ -570,7 +593,7 @@ namespace crnlib
bool dynamic_string::expand_buf(uint new_buf_size, bool preserve_contents)
{
new_buf_size = math::minimum<uint>(cUINT16_MAX, math::next_pow2(math::maximum<uint>(m_buf_size, new_buf_size)));
new_buf_size = math::minimum<uint>(UINT16_MAX, math::next_pow2(math::maximum<uint>(m_buf_size, new_buf_size)));
if (new_buf_size != m_buf_size)
{
@@ -602,9 +625,8 @@ namespace crnlib
{
uint buf_left = buf_size;
//if (m_len > cUINT16_MAX)
// return -1;
CRNLIB_ASSUME(sizeof(m_len) == sizeof(uint16));
if (m_len > UINT16_MAX)
return -1;
if (!utils::write_val((uint16)m_len, pBuf, buf_left, little_endian))
return -1;
@@ -665,4 +687,9 @@ namespace crnlib
swap(tmp);
}
dynamic_string& dynamic_string::operator= (const dynamic_wstring& rhs)
{
return set(rhs.get_ptr());
}
} // namespace crnlib
+9 -19
View File
@@ -4,9 +4,12 @@
namespace crnlib
{
enum { cMaxDynamicStringLen = cUINT16_MAX - 1 };
class dynamic_wstring;
class dynamic_string
{
friend class dynamic_wstring;
public:
inline dynamic_string() : m_buf_size(0), m_len(0), m_pStr(NULL) { }
dynamic_string(eVarArg dummy, const char* p, ...);
@@ -16,26 +19,25 @@ namespace crnlib
inline ~dynamic_string() { if (m_pStr) crnlib_delete_array(m_pStr); }
explicit dynamic_string(const wchar_t* pStr);
dynamic_string& set(const wchar_t *pStr);
dynamic_wstring& as_utf16(dynamic_wstring &buf);
// Truncates the string to 0 chars and frees the buffer.
void clear();
void optimize();
// Truncates the string to 0 chars, but does not free the buffer.
void empty();
inline const char *assume_ownership() { const char *p = m_pStr; m_pStr = NULL; m_len = 0; m_buf_size = 0; return p; }
inline uint get_len() const { return m_len; }
inline bool is_empty() const { return !m_len; }
inline const char* get_ptr() const { return m_pStr ? m_pStr : ""; }
inline const char* c_str() const { return get_ptr(); }
inline const char* get_ptr_raw() const { return m_pStr; }
inline char* get_ptr_raw() { return m_pStr; }
inline char front() const { return m_len ? m_pStr[0] : '\0'; }
inline char back() const { return m_len ? m_pStr[m_len - 1] : '\0'; }
inline char operator[] (uint i) const { CRNLIB_ASSERT(i <= m_len); return get_ptr()[i]; }
inline operator size_t() const { return fast_hash(get_ptr(), m_len) ^ fast_hash(&m_len, sizeof(m_len)); }
@@ -72,6 +74,7 @@ namespace crnlib
dynamic_string& set_from_buf(const void* pBuf, uint buf_size);
dynamic_string& operator= (const dynamic_string& rhs) { return set(rhs); }
dynamic_string& operator= (const dynamic_wstring& rhs);
dynamic_string& operator= (const char* p) { return set(p); }
dynamic_string& set_char(uint index, char c);
@@ -127,9 +130,6 @@ namespace crnlib
void translate_lf_to_crlf();
static inline char *create_raw_buffer(uint& buf_size_in_chars);
static inline void free_raw_buffer(char *p) { crnlib_delete_array(p); }
dynamic_string& set_from_raw_buf_and_assume_ownership(char *pBuf, uint buf_size_in_chars, uint len_in_chars);
private:
uint16 m_buf_size;
uint16 m_len;
@@ -160,14 +160,4 @@ namespace crnlib
a.swap(b);
}
inline char *dynamic_string::create_raw_buffer(uint& buf_size_in_chars)
{
if (buf_size_in_chars > cUINT16_MAX)
{
CRNLIB_ASSERT(0);
return NULL;
}
buf_size_in_chars = math::minimum<uint>(cUINT16_MAX, math::next_pow2(buf_size_in_chars));
return crnlib_new_array<char>(buf_size_in_chars);
}
} // namespace crnlib
+715
View File
@@ -0,0 +1,715 @@
// File: crn_dynamic_wstring.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_dynamic_wstring.h"
#include "crn_winhdr.h"
namespace crnlib
{
dynamic_wstring g_empty_dynamic_wstring;
dynamic_wstring::dynamic_wstring(eVarArg dummy, const wchar_t* p, ...) :
m_buf_size(0), m_len(0), m_pStr(NULL)
{
dummy;
CRNLIB_ASSERT(p);
va_list args;
va_start(args, p);
format_args(p, args);
va_end(args);
}
dynamic_wstring::dynamic_wstring(const wchar_t* p) :
m_buf_size(0), m_len(0), m_pStr(NULL)
{
CRNLIB_ASSERT(p);
set(p);
}
dynamic_wstring::dynamic_wstring(const wchar_t* p, uint len) :
m_buf_size(0), m_len(0), m_pStr(NULL)
{
CRNLIB_ASSERT(p);
set_from_buf(p, len);
}
dynamic_wstring::dynamic_wstring(const dynamic_wstring& other) :
m_buf_size(0), m_len(0), m_pStr(NULL)
{
set(other);
}
void dynamic_wstring::clear()
{
check();
if (m_pStr)
{
crnlib_delete_array(m_pStr);
m_pStr = NULL;
m_len = 0;
m_buf_size = 0;
}
}
void dynamic_wstring::empty()
{
truncate(0);
}
void dynamic_wstring::optimize()
{
if (!m_len)
clear();
else
{
uint min_buf_size = math::next_pow2((uint)m_len + 1);
if (m_buf_size > min_buf_size)
{
wchar_t* p = crnlib_new_array<wchar_t>(min_buf_size);
memcpy(p, m_pStr, (m_len + 1) * sizeof(wchar_t));
crnlib_delete_array(m_pStr);
m_pStr = p;
m_buf_size = static_cast<uint16>(min_buf_size);
check();
}
}
}
int dynamic_wstring::compare(const wchar_t* p, bool case_sensitive) const
{
CRNLIB_ASSERT(p);
const int result = (case_sensitive ? wcscmp : _wcsicmp)(get_ptr_priv(), p);
if (result < 0)
return -1;
else if (result > 0)
return 1;
return 0;
}
int dynamic_wstring::compare(const dynamic_wstring& rhs, bool case_sensitive) const
{
return compare(rhs.get_ptr_priv(), case_sensitive);
}
dynamic_wstring& dynamic_wstring::set(const wchar_t* p, uint max_len)
{
CRNLIB_ASSERT(p);
const uint len = math::minimum<uint>(max_len, static_cast<uint>(wcslen(p)));
CRNLIB_ASSERT(len < UINT16_MAX);
if ((!len) || (len >= UINT16_MAX))
clear();
else if ((m_pStr) && (p >= m_pStr) && (p < (m_pStr + m_buf_size)))
{
if (m_pStr != p)
memmove(m_pStr, p, len * sizeof(wchar_t));
m_pStr[len] = L'\0';
m_len = static_cast<uint16>(len);
}
else if (ensure_buf(len, false))
{
m_len = static_cast<uint16>(len);
memcpy(m_pStr, p, (m_len + 1) * sizeof(wchar_t));
}
check();
return *this;
}
dynamic_wstring& dynamic_wstring::set(const dynamic_wstring& other, uint max_len)
{
if (this == &other)
{
if (max_len < m_len)
{
m_pStr[max_len] = L'\0';
m_len = static_cast<uint16>(max_len);
}
}
else
{
const uint len = math::minimum<uint>(max_len, other.m_len);
if (!len)
clear();
else if (ensure_buf(len, false))
{
m_len = static_cast<uint16>(len);
memcpy(m_pStr, other.get_ptr_priv(), m_len * sizeof(wchar_t));
m_pStr[len] = L'\0';
}
}
check();
return *this;
}
bool dynamic_wstring::set_len(uint new_len, wchar_t fill_char)
{
if ((new_len >= UINT16_MAX) || (!fill_char))
return false;
uint cur_len = m_len;
if (ensure_buf(new_len, true))
{
if (new_len > cur_len)
{
for (uint i = 0; i < (new_len - cur_len); i++)
m_pStr[cur_len + i] = fill_char;
}
m_pStr[new_len] = L'\0';
m_len = static_cast<uint16>(new_len);
check();
}
return true;
}
dynamic_wstring& dynamic_wstring::set_from_buf(const void* pBuf, uint buf_size, bool little_endian)
{
CRNLIB_ASSERT(pBuf);
if (buf_size >= UINT16_MAX)
{
clear();
return *this;
}
for (uint i = 0; i < buf_size; i++)
{
if (static_cast<const wchar_t*>(pBuf)[i] == L'\0')
{
CRNLIB_ASSERT(0);
clear();
return *this;
}
}
if (ensure_buf(buf_size, false))
{
utils::copy_words(reinterpret_cast<uint16*>(m_pStr), reinterpret_cast<const uint16*>(pBuf), buf_size, c_crnlib_little_endian_platform != little_endian);
m_pStr[buf_size] = L'\0';
m_len = static_cast<uint16>(buf_size);
check();
}
return *this;
}
dynamic_wstring& dynamic_wstring::set_char(uint index, wchar_t c)
{
CRNLIB_ASSERT(index <= m_len);
if (!c)
truncate(index);
else if (index < m_len)
{
m_pStr[index] = c;
check();
}
else if (index == m_len)
append_char(c);
return *this;
}
dynamic_wstring& dynamic_wstring::append_char(wchar_t c)
{
if (ensure_buf(m_len + 1))
{
m_pStr[m_len] = c;
m_pStr[m_len + 1] = L'\0';
m_len++;
check();
}
return *this;
}
dynamic_wstring& dynamic_wstring::truncate(uint new_len)
{
if (new_len < m_len)
{
m_pStr[new_len] = L'\0';
m_len = static_cast<uint16>(new_len);
check();
}
return *this;
}
dynamic_wstring& dynamic_wstring::tolower()
{
if (m_len)
{
#ifdef _MSC_VER
_wcslwr_s(get_ptr_priv(), m_buf_size);
#else
_wcslwr(get_ptr_priv());
#endif
}
return *this;
}
dynamic_wstring& dynamic_wstring::toupper()
{
if (m_len)
{
#ifdef _MSC_VER
_wcsupr_s(get_ptr_priv(), m_buf_size);
#else
_wcsupr(get_ptr_priv());
#endif
}
return *this;
}
dynamic_wstring& dynamic_wstring::append(const wchar_t* p)
{
CRNLIB_ASSERT(p);
uint len = static_cast<uint>(wcslen(p));
uint new_total_len = m_len + len;
if ((new_total_len) && ensure_buf(new_total_len))
{
memcpy(m_pStr + m_len, p, (len + 1) * sizeof(wchar_t));
m_len = static_cast<uint16>(m_len + len);
check();
}
return *this;
}
dynamic_wstring& dynamic_wstring::append(const dynamic_wstring& other)
{
uint len = other.m_len;
uint new_total_len = m_len + len;
if ((new_total_len) && ensure_buf(new_total_len))
{
memcpy(m_pStr + m_len, other.get_ptr_priv(), (len + 1) * sizeof(wchar_t));
m_len = static_cast<uint16>(m_len + len);
check();
}
return *this;
}
dynamic_wstring operator+ (const wchar_t* p, const dynamic_wstring& a)
{
return dynamic_wstring(p).append(a);
}
dynamic_wstring operator+ (const dynamic_wstring& a, const wchar_t* p)
{
return dynamic_wstring(a).append(p);
}
dynamic_wstring operator+ (const dynamic_wstring& a, const dynamic_wstring& b)
{
return dynamic_wstring(a).append(b);
}
dynamic_wstring& dynamic_wstring::format_args(const wchar_t* p, va_list args)
{
CRNLIB_ASSERT(p);
const uint cBufSize = 4096;
wchar_t buf[cBufSize];
#ifdef _MSC_VER
int l = _vsnwprintf_s(buf, cBufSize, _TRUNCATE, p, args);
#else
int l = _vsnwprintf(buf, cBufSize, p, args);
#endif
if (l <= 0)
clear();
else if (ensure_buf(l, false))
{
memcpy(m_pStr, buf, (l + 1) * sizeof(wchar_t));
m_len = static_cast<uint16>(l);
check();
}
return *this;
}
dynamic_wstring& dynamic_wstring::format(const wchar_t* p, ...)
{
CRNLIB_ASSERT(p);
va_list args;
va_start(args, p);
format_args(p, args);
va_end(args);
return *this;
}
dynamic_wstring& dynamic_wstring::crop(uint start, uint len)
{
if (start >= m_len)
{
clear();
return *this;
}
len = math::minimum<uint>(len, m_len - start);
if (start)
memmove(get_ptr_priv(), get_ptr_priv() + start, len * sizeof(wchar_t));
m_pStr[len] = L'\0';
m_len = static_cast<uint16>(len);
check();
return *this;
}
dynamic_wstring& dynamic_wstring::substring(uint start, uint end)
{
CRNLIB_ASSERT(start <= end);
if (start > end)
return *this;
return crop(start, end - start);
}
dynamic_wstring& dynamic_wstring::left(uint len)
{
return substring(0, len);
}
dynamic_wstring& dynamic_wstring::mid(uint start, uint len)
{
return crop(start, len);
}
dynamic_wstring& dynamic_wstring::right(uint start)
{
return substring(start, get_len());
}
dynamic_wstring& dynamic_wstring::tail(uint num)
{
return substring(math::maximum<int>(static_cast<int>(get_len()) - static_cast<int>(num), 0), get_len());
}
dynamic_wstring& dynamic_wstring::unquote()
{
if (m_len >= 2)
{
if ( ((*this)[0] == L'\"') && ((*this)[m_len - 1] == L'\"') )
{
return mid(1, m_len - 2);
}
}
return *this;
}
int dynamic_wstring::find_left(const wchar_t* p, bool case_sensitive) const
{
CRNLIB_ASSERT(p);
const int p_len = (int)wcslen(p);
for (int i = 0; i <= (m_len - p_len); i++)
if ((case_sensitive ? wcsncmp : _wcsnicmp)(p, &m_pStr[i], p_len) == 0)
return i;
return -1;
}
bool dynamic_wstring::contains(const wchar_t* p, bool case_sensitive) const
{
return find_left(p, case_sensitive) >= 0;
}
uint dynamic_wstring::count_char(wchar_t c) const
{
uint count = 0;
for (uint i = 0; i < m_len; i++)
if (m_pStr[i] == c)
count++;
return count;
}
int dynamic_wstring::find_left(wchar_t c) const
{
for (uint i = 0; i < m_len; i++)
if (m_pStr[i] == c)
return i;
return -1;
}
int dynamic_wstring::find_right(wchar_t c) const
{
for (int i = (int)m_len - 1; i >= 0; i--)
if (m_pStr[i] == c)
return i;
return -1;
}
int dynamic_wstring::find_right(const wchar_t* p, bool case_sensitive) const
{
CRNLIB_ASSERT(p);
const int p_len = (int)wcslen(p);
for (int i = m_len - p_len; i >= 0; i--)
if ((case_sensitive ? wcsncmp : _wcsnicmp)(p, &m_pStr[i], p_len) == 0)
return i;
return -1;
}
dynamic_wstring& dynamic_wstring::trim()
{
int s, e;
for (s = 0; s < (int)m_len; s++)
if (!iswspace(m_pStr[s]))
break;
for (e = m_len - 1; e > s; e--)
if (!iswspace(m_pStr[e]))
break;
return crop(s, e - s + 1);
}
dynamic_wstring& dynamic_wstring::trim_crlf()
{
int s = 0, e;
for (e = m_len - 1; e > s; e--)
if ((m_pStr[e] != 13) && (m_pStr[e] != 10))
break;
return crop(s, e - s + 1);
}
dynamic_wstring& dynamic_wstring::remap(int from_char, int to_char)
{
for (uint i = 0; i < m_len; i++)
if (m_pStr[i] == from_char)
m_pStr[i] = (wchar_t)to_char;
return *this;
}
#ifdef CRNLIB_BUILD_DEBUG
void dynamic_wstring::check() const
{
if (!m_pStr)
{
CRNLIB_ASSERT(!m_buf_size && !m_len);
}
else
{
CRNLIB_ASSERT(m_buf_size);
CRNLIB_ASSERT((m_buf_size == UINT16_MAX) || math::is_power_of_2((uint32)m_buf_size));
CRNLIB_ASSERT(m_len < m_buf_size);
CRNLIB_ASSERT(wcslen(m_pStr) == m_len);
}
}
#endif
bool dynamic_wstring::ensure_buf(uint len, bool preserve_contents)
{
uint buf_size_needed = len + 1;
CRNLIB_ASSERT(buf_size_needed <= UINT16_MAX);
if (buf_size_needed <= UINT16_MAX)
{
if (buf_size_needed > m_buf_size)
expand_buf(buf_size_needed, preserve_contents);
}
return m_buf_size >= buf_size_needed;
}
bool dynamic_wstring::expand_buf(uint new_buf_size, bool preserve_contents)
{
new_buf_size = math::minimum<uint>(UINT16_MAX, math::next_pow2(math::maximum<uint>(m_buf_size, new_buf_size)));
if (new_buf_size != m_buf_size)
{
wchar_t* p = crnlib_new_array<wchar_t>(new_buf_size);
if (preserve_contents)
memcpy(p, get_ptr_priv(), (m_len + 1) * sizeof(wchar_t));
crnlib_delete_array(m_pStr);
m_pStr = p;
m_buf_size = static_cast<uint16>(new_buf_size);
if (preserve_contents)
check();
}
return m_buf_size >= new_buf_size;
}
void dynamic_wstring::swap(dynamic_wstring& other)
{
utils::swap(other.m_buf_size, m_buf_size);
utils::swap(other.m_len, m_len);
utils::swap(other.m_pStr, m_pStr);
}
int dynamic_wstring::serialize(void* pBuf, uint buf_size, bool little_endian) const
{
CRNLIB_ASSERT(pBuf);
uint buf_left = buf_size;
if (m_len > UINT16_MAX)
return -1;
if (!utils::write_val((uint16)m_len, pBuf, buf_left, little_endian))
return -1;
if (buf_left < (m_len * sizeof(wchar_t)))
return -1;
utils::copy_words(reinterpret_cast<uint16*>(pBuf), reinterpret_cast<const uint16*>(get_ptr_priv()), m_len, little_endian != c_crnlib_little_endian_platform);
buf_left -= m_len * sizeof(wchar_t);
return buf_size - buf_left;
}
int dynamic_wstring::deserialize(const void* pBuf, uint buf_size, bool little_endian)
{
CRNLIB_ASSERT(pBuf);
uint buf_left = buf_size;
if (buf_left < sizeof(uint16)) return -1;
uint16 l;
if (!utils::read_obj(l, pBuf, buf_left, little_endian))
return -1;
if (buf_left < (l * sizeof(wchar_t)))
return -1;
set_from_buf(pBuf, l, little_endian);
buf_left -= l * sizeof(wchar_t);
return buf_size - buf_left;
}
dynamic_wstring::dynamic_wstring(const char* p) :
m_buf_size(0), m_len(0), m_pStr(NULL)
{
set(p);
}
dynamic_wstring::dynamic_wstring(const dynamic_string& s) :
m_buf_size(0), m_len(0), m_pStr(NULL)
{
set(s.get_ptr());
}
dynamic_wstring& dynamic_wstring::set(const char* p)
{
CRNLIB_ASSERT(p);
if (!p)
{
clear();
return *this;
}
uint l = static_cast<uint>(strlen(p));
if (!l)
{
clear();
return *this;
}
const uint num_needed = static_cast<uint>(MultiByteToWideChar(CP_ACP, 0, p, l, NULL, 0));
if (!num_needed)
{
clear();
return *this;
}
if (!ensure_buf(num_needed, false))
{
clear();
return *this;
}
const uint num_written = static_cast<uint>(MultiByteToWideChar(CP_ACP, 0, p, l, m_pStr, num_needed));
CRNLIB_ASSERT(num_needed == num_written);
m_pStr[num_written] = L'\0';
m_len = static_cast<uint16>(num_written);
check();
return *this;
}
dynamic_string& dynamic_wstring::as_ansi(dynamic_string& buf)
{
if (!m_len)
{
buf.clear();
return buf;
}
const uint num_needed = WideCharToMultiByte(CP_ACP, 0, m_pStr, m_len, NULL, 0, NULL, NULL);
if (num_needed <= 0)
{
buf.clear();
return buf;
}
if (!buf.ensure_buf(num_needed, false))
{
buf.clear();
return buf;
}
const uint num_written = WideCharToMultiByte(CP_ACP, 0, m_pStr, m_len, buf.get_ptr_raw(), num_needed, NULL, NULL);
CRNLIB_ASSERT(num_written == num_needed);
buf.get_ptr_raw()[num_written] = 0;
buf.m_len = static_cast<uint16>(num_written);
buf.check();
return buf;
}
dynamic_wstring& dynamic_wstring::operator= (const dynamic_string& rhs)
{
return set(rhs.get_ptr());
}
} // namespace crnlib
+159
View File
@@ -0,0 +1,159 @@
// File: crn_dynamic_wstring.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
namespace crnlib
{
// UCS-2 string class (plane 0 characters only)
class dynamic_wstring
{
public:
inline dynamic_wstring() : m_buf_size(0), m_len(0), m_pStr(NULL) { }
dynamic_wstring(eVarArg dummy, const wchar_t* p, ...);
dynamic_wstring(const wchar_t* p);
dynamic_wstring(const wchar_t* p, uint len);
dynamic_wstring(const dynamic_wstring& other);
// Conversion from UCS-2 to ANSI and vice versa
explicit dynamic_wstring(const char* p);
explicit dynamic_wstring(const dynamic_string& s);
dynamic_wstring& set(const char* p);
dynamic_string& as_ansi(dynamic_string& buf);
inline ~dynamic_wstring() { CRNLIB_ASSUME(sizeof(wchar_t) == sizeof(uint16)); if (m_pStr) crnlib_delete_array(m_pStr); }
// Truncates the string to 0 chars and frees the buffer.
void clear();
void optimize();
// Truncates the string to 0 chars, but does not free the buffer.
void empty();
inline uint get_len() const { return m_len; }
inline bool is_empty() const { return !m_len; }
inline const wchar_t* get_ptr() const { return m_pStr ? m_pStr : L""; }
inline const wchar_t* get_ptr_raw() const { return m_pStr; }
inline wchar_t* get_ptr_raw() { return m_pStr; }
inline wchar_t operator[] (uint i) const { CRNLIB_ASSERT(i <= m_len); return get_ptr()[i]; }
inline operator size_t() const { return fast_hash(get_ptr(), m_len * sizeof(wchar_t)) ^ fast_hash(&m_len, sizeof(m_len)); }
int compare(const wchar_t* p, bool case_sensitive = false) const;
int compare(const dynamic_wstring& rhs, bool case_sensitive = false) const;
inline bool operator== (const dynamic_wstring& rhs) const { return compare(rhs) == 0; }
inline bool operator== (const wchar_t* p) const { return compare(p) == 0; }
inline bool operator!= (const dynamic_wstring& rhs) const { return compare(rhs) != 0; }
inline bool operator!= (const wchar_t* p) const { return compare(p) != 0; }
inline bool operator< (const dynamic_wstring& rhs) const { return compare(rhs) < 0; }
inline bool operator< (const wchar_t* p) const { return compare(p) < 0; }
inline bool operator> (const dynamic_wstring& rhs) const { return compare(rhs) > 0; }
inline bool operator> (const wchar_t* p) const { return compare(p) > 0; }
inline bool operator<= (const dynamic_wstring& rhs) const { return compare(rhs) <= 0; }
inline bool operator<= (const wchar_t* p) const { return compare(p) <= 0; }
inline bool operator>= (const dynamic_wstring& rhs) const { return compare(rhs) >= 0; }
inline bool operator>= (const wchar_t* p) const { return compare(p) >= 0; }
friend inline bool operator== (const wchar_t* p, const dynamic_wstring& rhs) { return rhs.compare(p) == 0; }
dynamic_wstring& set(const wchar_t* p, uint max_len = UINT_MAX);
dynamic_wstring& set(const dynamic_wstring& other, uint max_len = UINT_MAX);
bool set_len(uint new_len, wchar_t fill_char = ' ');
// Set from non-zero terminated buffer.
// little_endian is the endianness of the buffer's data
dynamic_wstring& set_from_buf(const void* pBuf, uint buf_size, bool little_endian = c_crnlib_little_endian_platform);
dynamic_wstring& operator= (const dynamic_wstring& rhs) { return set(rhs); }
dynamic_wstring& operator= (const dynamic_string& rhs);
dynamic_wstring& operator= (const wchar_t* p) { return set(p); }
dynamic_wstring& operator= (const char* p) { return set(p); }
dynamic_wstring& set_char(uint index, wchar_t c);
dynamic_wstring& append_char(wchar_t c);
dynamic_wstring& append_char(int c) { CRNLIB_ASSERT((c >= 0) && (c <= 0xFFFF)); return append_char(static_cast<wchar_t>(c)); }
dynamic_wstring& truncate(uint new_len);
dynamic_wstring& tolower();
dynamic_wstring& toupper();
dynamic_wstring& append(const wchar_t* p);
dynamic_wstring& append(const dynamic_wstring& other);
dynamic_wstring& operator += (const wchar_t* p) { return append(p); }
dynamic_wstring& operator += (const dynamic_wstring& other) { return append(other); }
friend dynamic_wstring operator+ (const wchar_t* p, const dynamic_wstring& a);
friend dynamic_wstring operator+ (const dynamic_wstring& a, const wchar_t* p);
friend dynamic_wstring operator+ (const dynamic_wstring& a, const dynamic_wstring& b);
dynamic_wstring& format_args(const wchar_t* p, va_list args);
dynamic_wstring& format(const wchar_t* p, ...);
dynamic_wstring& crop(uint start, uint len);
dynamic_wstring& substring(uint start, uint end);
dynamic_wstring& left(uint len);
dynamic_wstring& mid(uint start, uint len);
dynamic_wstring& right(uint start);
dynamic_wstring& tail(uint num);
dynamic_wstring& unquote();
uint count_char(wchar_t c) const;
int find_left(const wchar_t* p, bool case_sensitive = false) const;
int find_left(wchar_t c) const;
int find_right(wchar_t c) const;
int find_right(const wchar_t* p, bool case_sensitive = false) const;
bool contains(const wchar_t* p, bool case_sensitive = false) const;
dynamic_wstring& trim();
dynamic_wstring& trim_crlf();
dynamic_wstring& remap(int from_char, int to_char);
void swap(dynamic_wstring& other);
int serialize(void* pBuf, uint buf_size, bool little_endian) const;
int deserialize(const void* pBuf, uint buf_size, bool little_endian);
private:
// These values are in characters, not bytes!
uint16 m_buf_size;
uint16 m_len;
wchar_t* m_pStr;
#ifdef CRNLIB_BUILD_DEBUG
void check() const;
#else
void check() const { }
#endif
bool ensure_buf(uint len, bool preserve_contents = true);
bool expand_buf(uint new_buf_size, bool preserve_contents);
const wchar_t* get_ptr_priv() const { return m_pStr ? m_pStr : L""; }
wchar_t* get_ptr_priv() { return (wchar_t*)(m_pStr ? m_pStr : L""); }
};
typedef crnlib::vector<dynamic_wstring> dynamic_wstring_array;
extern dynamic_wstring g_empty_dynamic_wstring;
CRNLIB_DEFINE_BITWISE_MOVABLE(dynamic_wstring);
inline void swap (dynamic_wstring& a, dynamic_wstring& b)
{
a.swap(b);
}
} // namespace crnlib
-1481
View File
File diff suppressed because it is too large Load Diff
-609
View File
@@ -1,609 +0,0 @@
// File: crn_etc.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#include "../inc/crnlib.h"
#include "crn_dxt.h"
namespace crnlib
{
enum etc_constants
{
cETC1BytesPerBlock = 8U,
cETC1SelectorBits = 2U,
cETC1SelectorValues = 1U << cETC1SelectorBits,
cETC1SelectorMask = cETC1SelectorValues - 1U,
cETC1BlockShift = 2U,
cETC1BlockSize = 1U << cETC1BlockShift,
cETC1LSBSelectorIndicesBitOffset = 0,
cETC1MSBSelectorIndicesBitOffset = 16,
cETC1FlipBitOffset = 32,
cETC1DiffBitOffset = 33,
cETC1IntenModifierNumBits = 3,
cETC1IntenModifierValues = 1 << cETC1IntenModifierNumBits,
cETC1RightIntenModifierTableBitOffset = 34,
cETC1LeftIntenModifierTableBitOffset = 37,
// Base+Delta encoding (5 bit bases, 3 bit delta)
cETC1BaseColorCompNumBits = 5,
cETC1BaseColorCompMax = 1 << cETC1BaseColorCompNumBits,
cETC1DeltaColorCompNumBits = 3,
cETC1DeltaColorComp = 1 << cETC1DeltaColorCompNumBits,
cETC1DeltaColorCompMax = 1 << cETC1DeltaColorCompNumBits,
cETC1BaseColor5RBitOffset = 59,
cETC1BaseColor5GBitOffset = 51,
cETC1BaseColor5BBitOffset = 43,
cETC1DeltaColor3RBitOffset = 56,
cETC1DeltaColor3GBitOffset = 48,
cETC1DeltaColor3BBitOffset = 40,
// Absolute (non-delta) encoding (two 4-bit per component bases)
cETC1AbsColorCompNumBits = 4,
cETC1AbsColorCompMax = 1 << cETC1AbsColorCompNumBits,
cETC1AbsColor4R1BitOffset = 60,
cETC1AbsColor4G1BitOffset = 52,
cETC1AbsColor4B1BitOffset = 44,
cETC1AbsColor4R2BitOffset = 56,
cETC1AbsColor4G2BitOffset = 48,
cETC1AbsColor4B2BitOffset = 40,
cETC1ColorDeltaMin = -4,
cETC1ColorDeltaMax = 3,
// Delta3:
// 0 1 2 3 4 5 6 7
// 000 001 010 011 100 101 110 111
// 0 1 2 3 -4 -3 -2 -1
};
extern const int g_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues];
extern const uint8 g_etc1_to_selector_index[cETC1SelectorValues];
extern const uint8 g_selector_index_to_etc1[cETC1SelectorValues];
struct etc1_coord2
{
uint8 m_x, m_y;
};
extern const etc1_coord2 g_etc1_pixel_coords[2][2][8]; // [flipped][subblock][subblock_pixel]
struct etc1_block
{
// big endian uint64:
// bit ofs: 56 48 40 32 24 16 8 0
// byte ofs: b0, b1, b2, b3, b4, b5, b6, b7
union
{
uint64 m_uint64;
uint8 m_bytes[8];
};
uint8 m_low_color[2];
uint8 m_high_color[2];
enum { cNumSelectorBytes = 4 };
uint8 m_selectors[cNumSelectorBytes];
inline void clear()
{
utils::zero_this(this);
}
inline uint get_general_bits(uint ofs, uint num) const
{
CRNLIB_ASSERT((ofs + num) <= 64U);
CRNLIB_ASSERT(num && (num < 32U));
return (utils::read_be64(&m_uint64) >> ofs) & ((1UL << num) - 1UL);
}
inline void set_general_bits(uint ofs, uint num, uint bits)
{
CRNLIB_ASSERT((ofs + num) <= 64U);
CRNLIB_ASSERT(num && (num < 32U));
uint64 x = utils::read_be64(&m_uint64);
uint64 msk = ((1ULL << static_cast<uint64>(num)) - 1ULL) << static_cast<uint64>(ofs);
x &= ~msk;
x |= (static_cast<uint64>(bits) << static_cast<uint64>(ofs));
utils::write_be64(&m_uint64, x);
}
inline uint get_byte_bits(uint ofs, uint num) const
{
CRNLIB_ASSERT((ofs + num) <= 64U);
CRNLIB_ASSERT(num && (num <= 8U));
CRNLIB_ASSERT((ofs >> 3) == ((ofs + num - 1) >> 3));
const uint byte_ofs = 7 - (ofs >> 3);
const uint byte_bit_ofs = ofs & 7;
return (m_bytes[byte_ofs] >> byte_bit_ofs) & ((1 << num) - 1);
}
inline void set_byte_bits(uint ofs, uint num, uint bits)
{
CRNLIB_ASSERT((ofs + num) <= 64U);
CRNLIB_ASSERT(num && (num < 32U));
CRNLIB_ASSERT((ofs >> 3) == ((ofs + num - 1) >> 3));
CRNLIB_ASSERT(bits < (1U << num));
const uint byte_ofs = 7 - (ofs >> 3);
const uint byte_bit_ofs = ofs & 7;
const uint mask = (1 << num) - 1;
m_bytes[byte_ofs] &= ~(mask << byte_bit_ofs);
m_bytes[byte_ofs] |= (bits << byte_bit_ofs);
}
// false = left/right subblocks
// true = upper/lower subblocks
inline bool get_flip_bit() const
{
return (m_bytes[3] & 1) != 0;
}
inline void set_flip_bit(bool flip)
{
m_bytes[3] &= ~1;
m_bytes[3] |= static_cast<uint8>(flip);
}
inline bool get_diff_bit() const
{
return (m_bytes[3] & 2) != 0;
}
inline void set_diff_bit(bool diff)
{
m_bytes[3] &= ~2;
m_bytes[3] |= (static_cast<uint>(diff) << 1);
}
// Returns intensity modifier table (0-7) used by subblock subblock_id.
// subblock_id=0 left/top (CW 1), 1=right/bottom (CW 2)
inline uint get_inten_table(uint subblock_id) const
{
CRNLIB_ASSERT(subblock_id < 2);
const uint ofs = subblock_id ? 2 : 5;
return (m_bytes[3] >> ofs) & 7;
}
// Sets intensity modifier table (0-7) used by subblock subblock_id (0 or 1)
inline void set_inten_table(uint subblock_id, uint t)
{
CRNLIB_ASSERT(subblock_id < 2);
CRNLIB_ASSERT(t < 8);
const uint ofs = subblock_id ? 2 : 5;
m_bytes[3] &= ~(7 << ofs);
m_bytes[3] |= (t << ofs);
}
// Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
inline uint get_selector(uint x, uint y) const
{
CRNLIB_ASSERT((x | y) < 4);
const uint bit_index = x * 4 + y;
const uint byte_bit_ofs = bit_index & 7;
const uint8 *p = &m_bytes[7 - (bit_index >> 3)];
const uint lsb = (p[0] >> byte_bit_ofs) & 1;
const uint msb = (p[-2] >> byte_bit_ofs) & 1;
const uint val = lsb | (msb << 1);
return g_etc1_to_selector_index[val];
}
// Selector "val" ranges from 0-3 and is a direct index into g_etc1_inten_tables.
inline void set_selector(uint x, uint y, uint val)
{
CRNLIB_ASSERT((x | y | val) < 4);
const uint bit_index = x * 4 + y;
uint8 *p = &m_bytes[7 - (bit_index >> 3)];
const uint byte_bit_ofs = bit_index & 7;
const uint mask = 1 << byte_bit_ofs;
const uint etc1_val = g_selector_index_to_etc1[val];
const uint lsb = etc1_val & 1;
const uint msb = etc1_val >> 1;
p[0] &= ~mask;
p[0] |= (lsb << byte_bit_ofs);
p[-2] &= ~mask;
p[-2] |= (msb << byte_bit_ofs);
}
inline void set_base4_color(uint idx, uint16 c)
{
if (idx)
{
set_byte_bits(cETC1AbsColor4R2BitOffset, 4, (c >> 8) & 15);
set_byte_bits(cETC1AbsColor4G2BitOffset, 4, (c >> 4) & 15);
set_byte_bits(cETC1AbsColor4B2BitOffset, 4, c & 15);
}
else
{
set_byte_bits(cETC1AbsColor4R1BitOffset, 4, (c >> 8) & 15);
set_byte_bits(cETC1AbsColor4G1BitOffset, 4, (c >> 4) & 15);
set_byte_bits(cETC1AbsColor4B1BitOffset, 4, c & 15);
}
}
inline uint16 get_base4_color(uint idx) const
{
uint r, g, b;
if (idx)
{
r = get_byte_bits(cETC1AbsColor4R2BitOffset, 4);
g = get_byte_bits(cETC1AbsColor4G2BitOffset, 4);
b = get_byte_bits(cETC1AbsColor4B2BitOffset, 4);
}
else
{
r = get_byte_bits(cETC1AbsColor4R1BitOffset, 4);
g = get_byte_bits(cETC1AbsColor4G1BitOffset, 4);
b = get_byte_bits(cETC1AbsColor4B1BitOffset, 4);
}
return static_cast<uint16>(b | (g << 4U) | (r << 8U));
}
inline void set_base5_color(uint16 c)
{
set_byte_bits(cETC1BaseColor5RBitOffset, 5, (c >> 10) & 31);
set_byte_bits(cETC1BaseColor5GBitOffset, 5, (c >> 5) & 31);
set_byte_bits(cETC1BaseColor5BBitOffset, 5, c & 31);
}
inline uint16 get_base5_color() const
{
const uint r = get_byte_bits(cETC1BaseColor5RBitOffset, 5);
const uint g = get_byte_bits(cETC1BaseColor5GBitOffset, 5);
const uint b = get_byte_bits(cETC1BaseColor5BBitOffset, 5);
return static_cast<uint16>(b | (g << 5U) | (r << 10U));
}
void set_delta3_color(uint16 c)
{
set_byte_bits(cETC1DeltaColor3RBitOffset, 3, (c >> 6) & 7);
set_byte_bits(cETC1DeltaColor3GBitOffset, 3, (c >> 3) & 7);
set_byte_bits(cETC1DeltaColor3BBitOffset, 3, c & 7);
}
inline uint16 get_delta3_color() const
{
const uint r = get_byte_bits(cETC1DeltaColor3RBitOffset, 3);
const uint g = get_byte_bits(cETC1DeltaColor3GBitOffset, 3);
const uint b = get_byte_bits(cETC1DeltaColor3BBitOffset, 3);
return static_cast<uint16>(b | (g << 3U) | (r << 6U));
}
// Base color 5
static uint16 pack_color5(const color_quad_u8& color, bool scaled, uint bias = 127U);
static uint16 pack_color5(uint r, uint g, uint b, bool scaled, uint bias = 127U);
static color_quad_u8 unpack_color5(uint16 packed_color5, bool scaled, uint alpha = 255U);
static void unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color, bool scaled);
static bool unpack_color5(color_quad_u8& result, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha = 255U);
static bool unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha = 255U);
// Delta color 3
// Inputs range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
static uint16 pack_delta3(const color_quad_i16& color);
static uint16 pack_delta3(int r, int g, int b);
// Results range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
static color_quad_i16 unpack_delta3(uint16 packed_delta3);
static void unpack_delta3(int& r, int& g, int& b, uint16 packed_delta3);
// Abs color 4
static uint16 pack_color4(const color_quad_u8& color, bool scaled, uint bias = 127U);
static uint16 pack_color4(uint r, uint g, uint b, bool scaled, uint bias = 127U);
static color_quad_u8 unpack_color4(uint16 packed_color4, bool scaled, uint alpha = 255U);
static void unpack_color4(uint& r, uint& g, uint& b, uint16 packed_color4, bool scaled);
// subblock colors
static void get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint table_idx);
static bool get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint16 packed_delta3, uint table_idx);
static void get_abs_subblock_colors(color_quad_u8* pDst, uint16 packed_color4, uint table_idx);
static inline void unscaled_to_scaled_color(color_quad_u8& dst, const color_quad_u8& src, bool color4)
{
if (color4)
{
dst.r = src.r | (src.r << 4);
dst.g = src.g | (src.g << 4);
dst.b = src.b | (src.b << 4);
}
else
{
dst.r = (src.r >> 2) | (src.r << 3);
dst.g = (src.g >> 2) | (src.g << 3);
dst.b = (src.b >> 2) | (src.b << 3);
}
dst.a = src.a;
}
};
CRNLIB_DEFINE_BITWISE_COPYABLE(etc1_block);
// Returns false if the block is invalid (it will still be unpacked with clamping).
bool unpack_etc1(const etc1_block& block, color_quad_u8 *pDst, bool preserve_alpha = false);
enum crn_etc_quality
{
cCRNETCQualityFast,
cCRNETCQualityMedium,
cCRNETCQualitySlow,
cCRNETCQualityTotal,
cCRNETCQualityForceDWORD = 0xFFFFFFFF
};
struct crn_etc1_pack_params
{
crn_etc_quality m_quality;
bool m_perceptual;
bool m_dithering;
inline crn_etc1_pack_params()
{
clear();
}
void clear()
{
m_quality = cCRNETCQualitySlow;
m_perceptual = true;
m_dithering = false;
}
};
struct etc1_solution_coordinates
{
inline etc1_solution_coordinates() :
m_unscaled_color(0, 0, 0, 0),
m_inten_table(0),
m_color4(false)
{
}
inline etc1_solution_coordinates(uint r, uint g, uint b, uint inten_table, bool color4) :
m_unscaled_color(r, g, b, 255),
m_inten_table(inten_table),
m_color4(color4)
{
}
inline etc1_solution_coordinates(const color_quad_u8& c, uint inten_table, bool color4) :
m_unscaled_color(c),
m_inten_table(inten_table),
m_color4(color4)
{
}
inline etc1_solution_coordinates(const etc1_solution_coordinates& other)
{
*this = other;
}
inline etc1_solution_coordinates& operator= (const etc1_solution_coordinates& rhs)
{
m_unscaled_color = rhs.m_unscaled_color;
m_inten_table = rhs.m_inten_table;
m_color4 = rhs.m_color4;
return *this;
}
inline void clear()
{
m_unscaled_color.clear();
m_inten_table = 0;
m_color4 = false;
}
inline color_quad_u8 get_scaled_color() const
{
int br, bg, bb;
if (m_color4)
{
br = m_unscaled_color.r | (m_unscaled_color.r << 4);
bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
}
else
{
br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
}
return color_quad_u8(br, bg, bb);
}
inline void get_block_colors(color_quad_u8* pBlock_colors)
{
int br, bg, bb;
if (m_color4)
{
br = m_unscaled_color.r | (m_unscaled_color.r << 4);
bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
}
else
{
br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
}
const int* pInten_table = g_etc1_inten_tables[m_inten_table];
pBlock_colors[0].set(br + pInten_table[0], bg + pInten_table[0], bb + pInten_table[0]);
pBlock_colors[1].set(br + pInten_table[1], bg + pInten_table[1], bb + pInten_table[1]);
pBlock_colors[2].set(br + pInten_table[2], bg + pInten_table[2], bb + pInten_table[2]);
pBlock_colors[3].set(br + pInten_table[3], bg + pInten_table[3], bb + pInten_table[3]);
}
color_quad_u8 m_unscaled_color;
uint m_inten_table;
bool m_color4;
};
class etc1_optimizer
{
CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(etc1_optimizer);
public:
etc1_optimizer()
{
clear();
}
void clear()
{
m_pParams = NULL;
m_pResult = NULL;
m_pSorted_luma = NULL;
m_pSorted_luma_indices = NULL;
}
struct params : crn_etc1_pack_params
{
params()
{
clear();
}
params(const crn_etc1_pack_params& base_params) :
crn_etc1_pack_params(base_params)
{
clear_optimizer_params();
}
void clear()
{
crn_etc1_pack_params::clear();
clear_optimizer_params();
}
void clear_optimizer_params()
{
m_num_src_pixels = 0;
m_pSrc_pixels = 0;
m_use_color4 = false;
static const int s_default_scan_delta[] = { 0 };
m_pScan_deltas = s_default_scan_delta;
m_scan_delta_size = 1;
m_base_color5.clear();
m_constrain_against_base_color5 = false;
}
uint m_num_src_pixels;
const color_quad_u8* m_pSrc_pixels;
bool m_use_color4;
const int* m_pScan_deltas;
uint m_scan_delta_size;
color_quad_u8 m_base_color5;
bool m_constrain_against_base_color5;
};
struct results
{
uint64 m_error;
color_quad_u8 m_block_color_unscaled;
uint m_block_inten_table;
uint m_n;
uint8* m_pSelectors;
bool m_block_color4;
inline results& operator= (const results& rhs)
{
m_block_color_unscaled = rhs.m_block_color_unscaled;
m_block_color4 = rhs.m_block_color4;
m_block_inten_table = rhs.m_block_inten_table;
m_error = rhs.m_error;
CRNLIB_ASSERT(m_n == rhs.m_n);
memcpy(m_pSelectors, rhs.m_pSelectors, rhs.m_n);
return *this;
}
};
void init(const params& params, results& result);
bool compute();
private:
struct potential_solution
{
potential_solution() : m_coords(), m_error(cUINT64_MAX), m_valid(false)
{
}
etc1_solution_coordinates m_coords;
crnlib::vector<uint8> m_selectors;
uint64 m_error;
bool m_valid;
void clear()
{
m_coords.clear();
m_selectors.resize(0);
m_error = cUINT64_MAX;
m_valid = false;
}
bool are_selectors_all_equal() const
{
if (m_selectors.empty())
return false;
const uint s = m_selectors[0];
for (uint i = 1; i < m_selectors.size(); i++)
if (m_selectors[i] != s)
return false;
return true;
}
};
const params* m_pParams;
results* m_pResult;
int m_limit;
vec3F m_avg_color;
int m_br, m_bg, m_bb;
crnlib::vector<uint16> m_luma;
crnlib::vector<uint32> m_sorted_luma[2];
const uint32* m_pSorted_luma_indices;
uint32* m_pSorted_luma;
crnlib::vector<uint8> m_selectors;
crnlib::vector<uint8> m_best_selectors;
potential_solution m_best_solution;
potential_solution m_trial_solution;
crnlib::vector<uint8> m_temp_selectors;
bool evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
bool evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
};
struct pack_etc1_block_context
{
etc1_optimizer m_optimizer;
};
void pack_etc1_block_init();
uint64 pack_etc1_block(etc1_block& block, const color_quad_u8* pSrc_pixels, crn_etc1_pack_params& pack_params, pack_etc1_block_context& context);
} // namespace crnlib
+27
View File
@@ -0,0 +1,27 @@
// File: crn_event.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
namespace crnlib
{
class event
{
CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(event);
public:
event(bool manual_reset = false, bool initial_state = false, const char* pName = NULL);
~event();
inline void *get_handle(void) const { return m_handle; }
void set(void);
void reset(void);
void pulse(void);
bool wait(uint32 milliseconds = UINT32_MAX);
private:
void *m_handle;
};
} // namespace crnlib
-578
View File
@@ -1,578 +0,0 @@
// File: crn_file_utils.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_file_utils.h"
#include "crn_strutils.h"
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif
#ifdef WIN32
#include <direct.h>
#endif
#ifdef __GNUC__
#include <sys/stat.h>
#include <sys/stat.h>
#include <libgen.h>
#endif
namespace crnlib
{
#if CRNLIB_USE_WIN32_API
bool file_utils::is_read_only(const char* pFilename)
{
uint32 dst_file_attribs = GetFileAttributesA(pFilename);
if (dst_file_attribs == INVALID_FILE_ATTRIBUTES)
return false;
if (dst_file_attribs & FILE_ATTRIBUTE_READONLY)
return true;
return false;
}
bool file_utils::disable_read_only(const char* pFilename)
{
uint32 dst_file_attribs = GetFileAttributesA(pFilename);
if (dst_file_attribs == INVALID_FILE_ATTRIBUTES)
return false;
if (dst_file_attribs & FILE_ATTRIBUTE_READONLY)
{
dst_file_attribs &= ~FILE_ATTRIBUTE_READONLY;
if (SetFileAttributesA(pFilename, dst_file_attribs))
return true;
}
return false;
}
bool file_utils::is_older_than(const char* pSrcFilename, const char* pDstFilename)
{
WIN32_FILE_ATTRIBUTE_DATA src_file_attribs;
const BOOL src_file_exists = GetFileAttributesExA(pSrcFilename, GetFileExInfoStandard, &src_file_attribs);
WIN32_FILE_ATTRIBUTE_DATA dst_file_attribs;
const BOOL dest_file_exists = GetFileAttributesExA(pDstFilename, GetFileExInfoStandard, &dst_file_attribs);
if ((dest_file_exists) && (src_file_exists))
{
LONG timeComp = CompareFileTime(&src_file_attribs.ftLastWriteTime, &dst_file_attribs.ftLastWriteTime);
if (timeComp < 0)
return true;
}
return false;
}
bool file_utils::does_file_exist(const char* pFilename)
{
const DWORD fullAttributes = GetFileAttributesA(pFilename);
if (fullAttributes == INVALID_FILE_ATTRIBUTES)
return false;
if (fullAttributes & FILE_ATTRIBUTE_DIRECTORY)
return false;
return true;
}
bool file_utils::does_dir_exist(const char* pDir)
{
//-- Get the file attributes.
DWORD fullAttributes = GetFileAttributesA(pDir);
if (fullAttributes == INVALID_FILE_ATTRIBUTES)
return false;
if (fullAttributes & FILE_ATTRIBUTE_DIRECTORY)
return true;
return false;
}
bool file_utils::get_file_size(const char* pFilename, uint64& file_size)
{
file_size = 0;
WIN32_FILE_ATTRIBUTE_DATA attr;
if (0 == GetFileAttributesExA(pFilename, GetFileExInfoStandard, &attr))
return false;
if (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return false;
file_size = static_cast<uint64>(attr.nFileSizeLow) | (static_cast<uint64>(attr.nFileSizeHigh) << 32U);
return true;
}
#elif defined( __GNUC__ )
bool file_utils::is_read_only(const char* pFilename)
{
pFilename;
// TODO
return false;
}
bool file_utils::disable_read_only(const char* pFilename)
{
pFilename;
// TODO
return false;
}
bool file_utils::is_older_than(const char *pSrcFilename, const char* pDstFilename)
{
pSrcFilename, pDstFilename;
// TODO
return false;
}
bool file_utils::does_file_exist(const char* pFilename)
{
struct stat stat_buf;
int result = stat(pFilename, &stat_buf);
if (result)
return false;
if (S_ISREG(stat_buf.st_mode))
return true;
return false;
}
bool file_utils::does_dir_exist(const char* pDir)
{
struct stat stat_buf;
int result = stat(pDir, &stat_buf);
if (result)
return false;
if (S_ISDIR(stat_buf.st_mode) || S_ISLNK(stat_buf.st_mode))
return true;
return false;
}
bool file_utils::get_file_size(const char* pFilename, uint64& file_size)
{
file_size = 0;
struct stat stat_buf;
int result = stat(pFilename, &stat_buf);
if (result)
return false;
if (!S_ISREG(stat_buf.st_mode))
return false;
file_size = stat_buf.st_size;
return true;
}
#else
bool file_utils::is_read_only(const char* pFilename)
{
return false;
}
bool file_utils::disable_read_only(const char* pFilename)
{
pFilename;
// TODO
return false;
}
bool file_utils::is_older_than(const char *pSrcFilename, const char* pDstFilename)
{
return false;
}
bool file_utils::does_file_exist(const char* pFilename)
{
FILE* pFile;
crn_fopen(&pFile, pFilename, "rb");
if (!pFile)
return false;
fclose(pFile);
return true;
}
bool file_utils::does_dir_exist(const char* pDir)
{
return false;
}
bool file_utils::get_file_size(const char* pFilename, uint64& file_size)
{
FILE* pFile;
crn_fopen(&pFile, pFilename, "rb");
if (!pFile)
return false;
crn_fseek(pFile, 0, SEEK_END);
file_size = crn_ftell(pFile);
fclose(pFile);
return true;
}
#endif
bool file_utils::get_file_size(const char* pFilename, uint32& file_size)
{
uint64 file_size64;
if (!get_file_size(pFilename, file_size64))
{
file_size = 0;
return false;
}
if (file_size64 > cUINT32_MAX)
file_size64 = cUINT32_MAX;
file_size = static_cast<uint32>(file_size64);
return true;
}
bool file_utils::is_path_separator(char c)
{
#ifdef WIN32
return (c == '/') || (c == '\\');
#else
return (c == '/');
#endif
}
bool file_utils::is_path_or_drive_separator(char c)
{
#ifdef WIN32
return (c == '/') || (c == '\\') || (c == ':');
#else
return (c == '/');
#endif
}
bool file_utils::is_drive_separator(char c)
{
#ifdef WIN32
return (c == ':');
#else
c;
return false;
#endif
}
bool file_utils::split_path(const char* p, dynamic_string* pDrive, dynamic_string* pDir, dynamic_string* pFilename, dynamic_string* pExt)
{
CRNLIB_ASSERT(p);
#ifdef WIN32
char drive_buf[_MAX_DRIVE];
char dir_buf[_MAX_DIR];
char fname_buf[_MAX_FNAME];
char ext_buf[_MAX_EXT];
#ifdef _MSC_VER
// Compiling with MSVC
errno_t error = _splitpath_s(p,
pDrive ? drive_buf : NULL, pDrive ? _MAX_DRIVE : 0,
pDir ? dir_buf : NULL, pDir ? _MAX_DIR : 0,
pFilename ? fname_buf : NULL, pFilename ? _MAX_FNAME : 0,
pExt ? ext_buf : NULL, pExt ? _MAX_EXT : 0);
if (error != 0)
return false;
#else
// Compiling with MinGW
_splitpath(p,
pDrive ? drive_buf : NULL,
pDir ? dir_buf : NULL,
pFilename ? fname_buf : NULL,
pExt ? ext_buf : NULL);
#endif
if (pDrive) *pDrive = drive_buf;
if (pDir) *pDir = dir_buf;
if (pFilename) *pFilename = fname_buf;
if (pExt) *pExt = ext_buf;
#else
char dirtmp[1024];
char nametmp[1024];
strcpy_safe(dirtmp, sizeof(dirtmp), p);
strcpy_safe(nametmp, sizeof(nametmp), p);
if (pDrive) pDrive->clear();
const char *pDirName = dirname(dirtmp);
if (!pDirName)
return false;
if (pDir)
{
pDir->set(pDirName);
if ((!pDir->is_empty()) && (pDir->back() != '/'))
pDir->append_char('/');
}
const char *pBaseName = basename(nametmp);
if (!pBaseName)
return false;
if (pFilename)
{
pFilename->set(pBaseName);
remove_extension(*pFilename);
}
if (pExt)
{
pExt->set(pBaseName);
get_extension(*pExt);
*pExt = "." + *pExt;
}
#endif // #ifdef WIN32
return true;
}
bool file_utils::split_path(const char* p, dynamic_string& path, dynamic_string& filename)
{
dynamic_string temp_drive, temp_path, temp_ext;
if (!split_path(p, &temp_drive, &temp_path, &filename, &temp_ext))
return false;
filename += temp_ext;
combine_path(path, temp_drive.get_ptr(), temp_path.get_ptr());
return true;
}
bool file_utils::get_pathname(const char* p, dynamic_string& path)
{
dynamic_string temp_drive, temp_path;
if (!split_path(p, &temp_drive, &temp_path, NULL, NULL))
return false;
combine_path(path, temp_drive.get_ptr(), temp_path.get_ptr());
return true;
}
bool file_utils::get_filename(const char* p, dynamic_string& filename)
{
dynamic_string temp_ext;
if (!split_path(p, NULL, NULL, &filename, &temp_ext))
return false;
filename += temp_ext;
return true;
}
void file_utils::combine_path(dynamic_string& dst, const char* pA, const char* pB)
{
dynamic_string temp(pA);
if ((!temp.is_empty()) && (!is_path_separator(pB[0])))
{
char c = temp[temp.get_len() - 1];
if (!is_path_separator(c))
temp.append_char(CRNLIB_PATH_SEPERATOR_CHAR);
}
temp += pB;
dst.swap(temp);
}
void file_utils::combine_path(dynamic_string& dst, const char* pA, const char* pB, const char* pC)
{
combine_path(dst, pA, pB);
combine_path(dst, dst.get_ptr(), pC);
}
bool file_utils::full_path(dynamic_string& path)
{
#ifdef WIN32
char buf[1024];
char* p = _fullpath(buf, path.get_ptr(), sizeof(buf));
if (!p)
return false;
#else
char buf[PATH_MAX];
char* p;
dynamic_string pn, fn;
split_path(path.get_ptr(), pn, fn);
if ((fn == ".") || (fn == ".."))
{
p = realpath(path.get_ptr(), buf);
if (!p)
return false;
path.set(buf);
}
else
{
if (pn.is_empty())
pn = "./";
p = realpath(pn.get_ptr(), buf);
if (!p)
return false;
combine_path(path, buf, fn.get_ptr());
}
#endif
return true;
}
bool file_utils::get_extension(dynamic_string& filename)
{
int sep = -1;
#ifdef WIN32
sep = filename.find_right('\\');
#endif
if (sep < 0)
sep = filename.find_right('/');
int dot = filename.find_right('.');
if (dot < sep)
{
filename.clear();
return false;
}
filename.right(dot + 1);
return true;
}
bool file_utils::remove_extension(dynamic_string& filename)
{
int sep = -1;
#ifdef WIN32
sep = filename.find_right('\\');
#endif
if (sep < 0)
sep = filename.find_right('/');
int dot = filename.find_right('.');
if (dot < sep)
return false;
filename.left(dot);
return true;
}
bool file_utils::create_path(const dynamic_string& fullpath)
{
bool got_unc = false; got_unc;
dynamic_string cur_path;
const int l = fullpath.get_len();
int n = 0;
while (n < l)
{
const char c = fullpath.get_ptr()[n];
const bool sep = is_path_separator(c);
const bool back_sep = is_path_separator(cur_path.back());
const bool is_last_char = (n == (l - 1));
if ( ((sep) && (!back_sep)) || (is_last_char) )
{
if ((is_last_char) && (!sep))
cur_path.append_char(c);
bool valid = !cur_path.is_empty();
#ifdef WIN32
// reject obvious stuff (drives, beginning of UNC paths):
// c:\b\cool
// \\machine\blah
// \cool\blah
if ((cur_path.get_len() == 2) && (cur_path[1] == ':'))
valid = false;
else if ((cur_path.get_len() >= 2) && (cur_path[0] == '\\') && (cur_path[1] == '\\'))
{
if (!got_unc)
valid = false;
got_unc = true;
}
else if (cur_path == "\\")
valid = false;
#endif
if (cur_path == "/")
valid = false;
if ((valid) && (cur_path.get_len()))
{
#ifdef WIN32
_mkdir(cur_path.get_ptr());
#else
mkdir(cur_path.get_ptr(), S_IRWXU | S_IRWXG | S_IRWXO );
#endif
}
}
cur_path.append_char(c);
n++;
}
return true;
}
void file_utils::trim_trailing_seperator(dynamic_string& path)
{
if ((path.get_len()) && (is_path_separator(path.back())))
path.truncate(path.get_len() - 1);
}
// See http://www.codeproject.com/KB/string/wildcmp.aspx
int file_utils::wildcmp(const char* pWild, const char* pString)
{
const char* cp = NULL, *mp = NULL;
while ((*pString) && (*pWild != '*'))
{
if ((*pWild != *pString) && (*pWild != '?'))
return 0;
pWild++;
pString++;
}
// Either *pString=='\0' or *pWild='*' here.
while (*pString)
{
if (*pWild == '*')
{
if (!*++pWild)
return 1;
mp = pWild;
cp = pString+1;
}
else if ((*pWild == *pString) || (*pWild == '?'))
{
pWild++;
pString++;
}
else
{
pWild = mp;
pString = cp++;
}
}
while (*pWild == '*')
pWild++;
return !*pWild;
}
bool file_utils::write_buf_to_file(const char* pPath, const void* pData, size_t data_size)
{
FILE *pFile = NULL;
#ifdef _MSC_VER
// Compiling with MSVC
if (fopen_s(&pFile, pPath, "wb"))
return false;
#else
pFile = fopen(pPath, "wb");
#endif
if (!pFile)
return false;
bool success = fwrite(pData, 1, data_size, pFile) == data_size;
fclose(pFile);
return success;
}
} // namespace crnlib
-43
View File
@@ -1,43 +0,0 @@
// File: crn_file_utils.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
namespace crnlib
{
struct file_utils
{
// Returns true if pSrcFilename is older than pDstFilename
static bool is_read_only(const char* pFilename);
static bool disable_read_only(const char* pFilename);
static bool is_older_than(const char *pSrcFilename, const char* pDstFilename);
static bool does_file_exist(const char* pFilename);
static bool does_dir_exist(const char* pDir);
static bool get_file_size(const char* pFilename, uint64& file_size);
static bool get_file_size(const char* pFilename, uint32& file_size);
static bool is_path_separator(char c);
static bool is_path_or_drive_separator(char c);
static bool is_drive_separator(char c);
static bool split_path(const char* p, dynamic_string* pDrive, dynamic_string* pDir, dynamic_string* pFilename, dynamic_string* pExt);
static bool split_path(const char* p, dynamic_string& path, dynamic_string& filename);
static bool get_pathname(const char* p, dynamic_string& path);
static bool get_filename(const char* p, dynamic_string& filename);
static void combine_path(dynamic_string& dst, const char* pA, const char* pB);
static void combine_path(dynamic_string& dst, const char* pA, const char* pB, const char* pC);
static bool full_path(dynamic_string& path);
static bool get_extension(dynamic_string& filename);
static bool remove_extension(dynamic_string& filename);
static bool create_path(const dynamic_string& path);
static void trim_trailing_seperator(dynamic_string& path);
static int wildcmp(const char* pWild, const char* pString);
static bool write_buf_to_file(const char* pPath, const void* pData, size_t data_size);
}; // struct file_utils
} // namespace crnlib
-287
View File
@@ -1,287 +0,0 @@
// File: crn_win32_find_files.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_find_files.h"
#include "crn_file_utils.h"
#include "crn_strutils.h"
#ifdef CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#elif defined(__GNUC__)
#include <fnmatch.h>
#include <dirent.h>
#endif
namespace crnlib
{
#ifdef CRNLIB_USE_WIN32_API
bool find_files::find(const char* pBasepath, const char* pFilespec, uint flags)
{
m_last_error = S_OK;
m_files.resize(0);
return find_internal(pBasepath, "", pFilespec, flags, 0);
}
bool find_files::find(const char* pSpec, uint flags)
{
dynamic_string find_name(pSpec);
if (!file_utils::full_path(find_name))
return false;
dynamic_string find_pathname, find_filename;
if (!file_utils::split_path(find_name.get_ptr(), find_pathname, find_filename))
return false;
return find(find_pathname.get_ptr(), find_filename.get_ptr(), flags);
}
bool find_files::find_internal(const char* pBasepath, const char* pRelpath, const char* pFilespec, uint flags, int level)
{
WIN32_FIND_DATAA find_data;
dynamic_string filename;
dynamic_string_array child_paths;
if (flags & cFlagRecursive)
{
if (strlen(pRelpath))
file_utils::combine_path(filename, pBasepath, pRelpath, "*");
else
file_utils::combine_path(filename, pBasepath, "*");
HANDLE handle = FindFirstFileA(filename.get_ptr(), &find_data);
if (handle == INVALID_HANDLE_VALUE)
{
HRESULT hres = GetLastError();
if ((level == 0) && (hres != NO_ERROR) && (hres != ERROR_FILE_NOT_FOUND))
{
m_last_error = hres;
return false;
}
}
else
{
do
{
const bool is_dir = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
bool skip = !is_dir;
if (is_dir)
skip = (strcmp(find_data.cFileName, ".") == 0) || (strcmp(find_data.cFileName, "..") == 0);
if (find_data.dwFileAttributes & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY))
skip = true;
if (find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
{
if ((flags & cFlagAllowHidden) == 0)
skip = true;
}
if (!skip)
{
dynamic_string child_path(find_data.cFileName);
if ((!child_path.count_char('?')) && (!child_path.count_char('*')))
child_paths.push_back(child_path);
}
} while (FindNextFileA(handle, &find_data) != 0);
HRESULT hres = GetLastError();
FindClose(handle);
handle = INVALID_HANDLE_VALUE;
if (hres != ERROR_NO_MORE_FILES)
{
m_last_error = hres;
return false;
}
}
}
if (strlen(pRelpath))
file_utils::combine_path(filename, pBasepath, pRelpath, pFilespec);
else
file_utils::combine_path(filename, pBasepath, pFilespec);
HANDLE handle = FindFirstFileA(filename.get_ptr(), &find_data);
if (handle == INVALID_HANDLE_VALUE)
{
HRESULT hres = GetLastError();
if ((level == 0) && (hres != NO_ERROR) && (hres != ERROR_FILE_NOT_FOUND))
{
m_last_error = hres;
return false;
}
}
else
{
do
{
const bool is_dir = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
bool skip = false;
if (is_dir)
skip = (strcmp(find_data.cFileName, ".") == 0) || (strcmp(find_data.cFileName, "..") == 0);
if (find_data.dwFileAttributes & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY))
skip = true;
if (find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
{
if ((flags & cFlagAllowHidden) == 0)
skip = true;
}
if (!skip)
{
if (((is_dir) && (flags & cFlagAllowDirs)) || ((!is_dir) && (flags & cFlagAllowFiles)))
{
m_files.resize(m_files.size() + 1);
file_desc& file = m_files.back();
file.m_is_dir = is_dir;
file.m_base = pBasepath;
file.m_name = find_data.cFileName;
file.m_rel = pRelpath;
if (strlen(pRelpath))
file_utils::combine_path(file.m_fullname, pBasepath, pRelpath, find_data.cFileName);
else
file_utils::combine_path(file.m_fullname, pBasepath, find_data.cFileName);
}
}
} while (FindNextFileA(handle, &find_data) != 0);
HRESULT hres = GetLastError();
FindClose(handle);
if (hres != ERROR_NO_MORE_FILES)
{
m_last_error = hres;
return false;
}
}
for (uint i = 0; i < child_paths.size(); i++)
{
dynamic_string child_path;
if (strlen(pRelpath))
file_utils::combine_path(child_path, pRelpath, child_paths[i].get_ptr());
else
child_path = child_paths[i];
if (!find_internal(pBasepath, child_path.get_ptr(), pFilespec, flags, level + 1))
return false;
}
return true;
}
#elif defined(__GNUC__)
bool find_files::find(const char* pBasepath, const char* pFilespec, uint flags)
{
m_files.resize(0);
return find_internal(pBasepath, "", pFilespec, flags, 0);
}
bool find_files::find(const char* pSpec, uint flags)
{
dynamic_string find_name(pSpec);
if (!file_utils::full_path(find_name))
return false;
dynamic_string find_pathname, find_filename;
if (!file_utils::split_path(find_name.get_ptr(), find_pathname, find_filename))
return false;
return find(find_pathname.get_ptr(), find_filename.get_ptr(), flags);
}
bool find_files::find_internal(const char* pBasepath, const char* pRelpath, const char* pFilespec, uint flags, int level)
{
dynamic_string pathname;
if (strlen(pRelpath))
file_utils::combine_path(pathname, pBasepath, pRelpath);
else
pathname = pBasepath;
if (!pathname.is_empty())
{
char c = pathname.back();
if (c != '/')
pathname += "/";
}
DIR *dp = opendir(pathname.get_ptr());
if (!dp)
return level ? true : false;
dynamic_string_array paths;
for ( ; ; )
{
struct dirent *ep = readdir(dp);
if (!ep)
break;
if ((strcmp(ep->d_name, ".") == 0) || (strcmp(ep->d_name, "..") == 0))
continue;
const bool is_directory = (ep->d_type & DT_DIR) != 0;
const bool is_file = (ep->d_type & DT_REG) != 0;
dynamic_string filename(ep->d_name);
if (is_directory)
{
if (flags & cFlagRecursive)
{
paths.push_back(filename);
}
}
if (((is_file) && (flags & cFlagAllowFiles)) || ((is_directory) && (flags & cFlagAllowDirs)))
{
if (0 == fnmatch(pFilespec, filename.get_ptr(), 0))
{
m_files.resize(m_files.size() + 1);
file_desc& file = m_files.back();
file.m_is_dir = is_directory;
file.m_base = pBasepath;
file.m_rel = pRelpath;
file.m_name = filename;
file.m_fullname = pathname + filename;
}
}
}
closedir(dp);
dp = NULL;
if (flags & cFlagRecursive)
{
for (uint i = 0; i < paths.size(); i++)
{
dynamic_string childpath;
if (strlen(pRelpath))
file_utils::combine_path(childpath, pRelpath, paths[i].get_ptr());
else
childpath = paths[i];
if (!find_internal(pBasepath, childpath.get_ptr(), pFilespec, flags, level + 1))
return false;
}
}
return true;
}
#else
#error Unimplemented
#endif
} // namespace crnlib
-158
View File
@@ -1,158 +0,0 @@
// File: crn_freeimage_image_utils.h
// See Copyright Notice and license at the end of inc/crnlib.h
// Note: This header file requires FreeImage/FreeImagePlus.
#include "crn_image_utils.h"
#include "freeImagePlus.h"
namespace crnlib
{
namespace freeimage_image_utils
{
inline bool load_from_file(image_u8& dest, const wchar_t* pFilename, int fi_flag)
{
fipImage src_image;
if (!src_image.loadU(pFilename, fi_flag))
return false;
const uint orig_bits_per_pixel = src_image.getBitsPerPixel();
const FREE_IMAGE_COLOR_TYPE orig_color_type = src_image.getColorType();
if (!src_image.convertTo32Bits())
return false;
if (src_image.getBitsPerPixel() != 32)
return false;
uint width = src_image.getWidth();
uint height = src_image.getHeight();
dest.resize(src_image.getWidth(), src_image.getHeight(), src_image.getWidth());
color_quad_u8* pDst = dest.get_ptr();
bool grayscale = true;
bool has_alpha = false;
for (uint y = 0; y < height; y++)
{
const BYTE* pSrc = src_image.getScanLine((WORD)(height - 1 - y));
color_quad_u8* pD = pDst;
for (uint x = width; x; x--)
{
color_quad_u8 c;
c.r = pSrc[FI_RGBA_RED];
c.g = pSrc[FI_RGBA_GREEN];
c.b = pSrc[FI_RGBA_BLUE];
c.a = pSrc[FI_RGBA_ALPHA];
if (!c.is_grayscale())
grayscale = false;
has_alpha |= (c.a < 255);
pSrc += 4;
*pD++ = c;
}
pDst += width;
}
dest.reset_comp_flags();
if (grayscale)
dest.set_grayscale(true);
dest.set_component_valid(3, has_alpha || (orig_color_type == FIC_RGBALPHA) || (orig_bits_per_pixel == 32));
return true;
}
const int cSaveLuma = -1;
inline bool save_to_grayscale_file(const wchar_t* pFilename, const image_u8& src, int component, int fi_flag)
{
fipImage dst_image(FIT_BITMAP, (WORD)src.get_width(), (WORD)src.get_height(), 8);
RGBQUAD* p = dst_image.getPalette();
for (uint i = 0; i < dst_image.getPaletteSize(); i++)
{
p[i].rgbRed = (BYTE)i;
p[i].rgbGreen = (BYTE)i;
p[i].rgbBlue = (BYTE)i;
p[i].rgbReserved = 255;
}
for (uint y = 0; y < src.get_height(); y++)
{
const color_quad_u8* pSrc = src.get_scanline(y);
for (uint x = 0; x < src.get_width(); x++)
{
BYTE v;
if (component == cSaveLuma)
v = (BYTE)(*pSrc).get_luma();
else
v = (*pSrc)[component];
dst_image.setPixelIndex(x, src.get_height() - 1 - y, &v);
pSrc++;
}
}
if (!dst_image.saveU(pFilename, fi_flag))
return false;
return true;
}
inline bool save_to_file(const wchar_t* pFilename, const image_u8& src, int fi_flag, bool ignore_alpha = false)
{
const bool save_alpha = src.is_component_valid(3);
uint bpp = (save_alpha && !ignore_alpha) ? 32 : 24;
if (bpp == 32)
{
dynamic_wstring ext(pFilename);
get_extension(ext);
if ((ext == L"jpg") || (ext == L"jpeg") || (ext == L"gif") || (ext == L"jp2"))
bpp = 24;
}
if ((bpp == 24) && (src.is_grayscale()))
return save_to_grayscale_file(pFilename, src, cSaveLuma, fi_flag);
fipImage dst_image(FIT_BITMAP, (WORD)src.get_width(), (WORD)src.get_height(), (WORD)bpp);
for (uint y = 0; y < src.get_height(); y++)
{
for (uint x = 0; x < src.get_width(); x++)
{
color_quad_u8 c(src(x, y));
RGBQUAD quad;
quad.rgbRed = c.r;
quad.rgbGreen = c.g;
quad.rgbBlue = c.b;
if (bpp == 32)
quad.rgbReserved = c.a;
else
quad.rgbReserved = 255;
dst_image.setPixelColor(x, src.get_height() - 1 - y, &quad);
}
}
if (!dst_image.saveU(pFilename, fi_flag))
return false;
return true;
}
} // namespace freeimage_image_utils
} // namespace crnlib
+4 -9
View File
@@ -2,9 +2,8 @@
// See Copyright Notice and license at the end of inc/crnlib.h
//
// Notes:
// stl-like hash map/hash set, with predictable performance across platforms/compilers/C run times/etc.
// Hash function ref: http://www.brpreiss.com/books/opus4/html/page215.html
// Compared for performance against VC9's std::hash_map.
// Compared for speed against VC9's std::hash_map.
// Linear probing, auto resizes on ~50% load factor.
// Uses Knuth's multiplicative method (Fibonacci hashing).
#pragma once
@@ -375,8 +374,6 @@ namespace crnlib
return iterator(*this, m_values.size());
}
// insert_result.first will always point to inserted key/value (or the already existing key/value).
// insert_resutt.second will be true if a new key/value was inserted, or false if the key already existed (in which case first will point to the already existing value).
typedef std::pair<iterator, bool> insert_result;
inline insert_result insert(const Key& k, const Value& v = Value())
@@ -511,19 +508,17 @@ namespace crnlib
scalar_type<Value>::destruct(&p->second);
}
// Moves *pSrc to *pDst efficiently.
// pDst should NOT be constructed on entry.
static inline void move_node(node* pDst, node* pSrc)
{
CRNLIB_ASSERT(!pDst->state);
if (CRNLIB_IS_BITWISE_COPYABLE_OR_MOVABLE(Key) && CRNLIB_IS_BITWISE_COPYABLE_OR_MOVABLE(Value))
if (CRNLIB_IS_BITWISE_MOVABLE(Key) && CRNLIB_IS_BITWISE_MOVABLE(Value))
{
memcpy(pDst, pSrc, sizeof(node));
}
else
{
if (CRNLIB_IS_BITWISE_COPYABLE_OR_MOVABLE(Key))
if (CRNLIB_IS_BITWISE_MOVABLE(Key))
memcpy(&pDst->first, &pSrc->first, sizeof(Key));
else
{
@@ -531,7 +526,7 @@ namespace crnlib
scalar_type<Key>::destruct(&pSrc->first);
}
if (CRNLIB_IS_BITWISE_COPYABLE_OR_MOVABLE(Value))
if (CRNLIB_IS_BITWISE_MOVABLE(Value))
memcpy(&pDst->second, &pSrc->second, sizeof(Value));
else
{
+2 -2
View File
@@ -30,7 +30,7 @@ namespace crnlib
}
template <typename T>
inline void construct_array(T* p, uint n)
void construct_array(T* p, uint n)
{
T* q = p + n;
for ( ; p != q; ++p)
@@ -38,7 +38,7 @@ namespace crnlib
}
template <typename T, typename U>
inline void construct_array(T* p, uint n, const U& init)
void construct_array(T* p, uint n, const U& init)
{
T* q = p + n;
for ( ; p != q; ++p)
+3 -3
View File
@@ -228,7 +228,7 @@ namespace crnlib
sym_freq& sf = state.syms0[num_used_syms];
sf.m_left = (uint16)i;
sf.m_right = cUINT16_MAX;
sf.m_right = UINT16_MAX;
sf.m_freq = freq;
num_used_syms++;
}
@@ -263,8 +263,8 @@ namespace crnlib
#else
// Dummy node
sym_freq& sf = state.syms0[num_used_syms];
sf.m_left = cUINT16_MAX;
sf.m_right = cUINT16_MAX;
sf.m_left = UINT16_MAX;
sf.m_right = UINT16_MAX;
sf.m_freq = UINT_MAX;
uint next_internal_node = num_used_syms + 1;
+44 -155
View File
@@ -4,7 +4,6 @@
#include "crn_color.h"
#include "crn_vec.h"
#include "crn_pixel_format.h"
#include "crn_rect.h"
namespace crnlib
{
@@ -26,7 +25,6 @@ namespace crnlib
{
}
// pitch is in PIXELS, not bytes.
image(uint width, uint height, uint pitch = UINT_MAX, const color_type& background = color_type::make_black(), uint flags = pixel_format_helpers::cDefaultCompFlags) :
m_comp_flags(flags)
{
@@ -46,7 +44,6 @@ namespace crnlib
set_all(background);
}
// pitch is in PIXELS, not bytes.
image(color_type* pPixels, uint width, uint height, uint pitch = UINT_MAX, uint flags = pixel_format_helpers::cDefaultCompFlags)
{
alias(pPixels, width, height, pitch, flags);
@@ -97,7 +94,6 @@ namespace crnlib
*this = other;
}
// pitch is in PIXELS, not bytes.
void alias(color_type* pPixels, uint width, uint height, uint pitch = UINT_MAX, uint flags = pixel_format_helpers::cDefaultCompFlags)
{
m_pixel_buf.clear();
@@ -111,40 +107,6 @@ namespace crnlib
m_comp_flags = flags;
}
// pitch is in PIXELS, not bytes.
bool grant_ownership(color_type* pPixels, uint width, uint height, uint pitch = UINT_MAX, uint flags = pixel_format_helpers::cDefaultCompFlags)
{
if (pitch == UINT_MAX)
pitch = width;
if ((!pPixels) || (!width) || (!height) || (pitch < width))
{
CRNLIB_ASSERT(0);
return false;
}
if (pPixels == get_ptr())
{
CRNLIB_ASSERT(0);
return false;
}
clear();
if (!m_pixel_buf.grant_ownership(pPixels, height * pitch, height * pitch))
return false;
m_pPixels = pPixels;
m_width = width;
m_height = height;
m_pitch = pitch;
m_total = pitch * height;
m_comp_flags = flags;
return true;
}
void clear()
{
m_pPixels = NULL;
@@ -177,34 +139,6 @@ namespace crnlib
m_pPixels[i] = c;
}
void flip_x()
{
const uint half_width = m_width / 2;
for (uint y = 0; y < m_height; y++)
{
for (uint x = 0; x < half_width; x++)
{
color_type c((*this)(x, y));
(*this)(x, y) = (*this)(m_width - 1 - x, y);
(*this)(m_width - 1 - x, y) = c;
}
}
}
void flip_y()
{
const uint half_height = m_height / 2;
for (uint y = 0; y < half_height; y++)
{
for (uint x = 0; x < m_width; x++)
{
color_type c((*this)(x, y));
(*this)(x, y) = (*this)(x, m_height - 1 - y);
(*this)(x, m_height - 1 - y) = c;
}
}
}
void convert_to_grayscale()
{
for (uint y = 0; y < m_height; y++)
@@ -249,16 +183,13 @@ namespace crnlib
bool extract_block(color_type* pDst, uint x, uint y, uint w, uint h, bool flip_xy = false) const
{
if ((x >= m_width) || (y >= m_height))
{
CRNLIB_ASSERT(0);
return false;
}
if (flip_xy)
{
for (uint y_ofs = 0; y_ofs < h; y_ofs++)
for (uint x_ofs = 0; x_ofs < w; x_ofs++)
pDst[x_ofs * h + y_ofs] = get_clamped(x_ofs + x, y_ofs + y); // 5/4/12 - this was incorrectly x_ofs * 4
pDst[x_ofs * 4 + y_ofs] = get_clamped(x_ofs + x, y_ofs + y);
}
else if (((x + w) > m_width) || ((y + h) > m_height))
{
@@ -282,14 +213,10 @@ namespace crnlib
return true;
}
// No clipping!
void unclipped_fill_box(uint x, uint y, uint w, uint h, const color_type& c)
void fill(uint x, uint y, uint w, uint h, const color_type& c)
{
if (((x + w) > m_width) || ((y + h) > m_height))
{
CRNLIB_ASSERT(0);
return;
}
CRNLIB_ASSERT((x + w) <= m_width);
CRNLIB_ASSERT((y + h) <= m_height);
color_type* p = get_scanline(y) + x;
@@ -302,7 +229,7 @@ namespace crnlib
}
}
void draw_rect(int x, int y, uint width, uint height, const color_type& c)
void draw_box(int x, int y, uint width, uint height, const color_type& c)
{
draw_line(x, y, x + width - 1, y, c);
draw_line(x, y, x, y + height - 1, c);
@@ -311,25 +238,13 @@ namespace crnlib
}
// No clipping!
bool unclipped_blit(uint src_x, uint src_y, uint src_w, uint src_h, uint dst_x, uint dst_y, const image& src)
bool copy(uint src_x, uint src_y, uint src_w, uint src_h, uint dst_x, uint dst_y, const image& src)
{
if ((!is_valid()) || (!src.is_valid()))
{
CRNLIB_ASSERT(0);
return false;
}
if ( ((src_x + src_w) > src.get_width()) || ((src_y + src_h) > src.get_height()) )
{
CRNLIB_ASSERT(0);
return false;
}
if ( ((dst_x + src_w) > get_width()) || ((dst_y + src_h) > get_height()) )
{
CRNLIB_ASSERT(0);
return false;
}
const color_type* pS = &src(src_x, src_y);
color_type* pD = &(*this)(dst_x, dst_y);
@@ -347,74 +262,38 @@ namespace crnlib
}
// With clipping.
bool blit(int dst_x, int dst_y, const image& src)
void blit(int dst_x, int dst_y, const image& src)
{
if ((!is_valid()) || (!src.is_valid()))
{
CRNLIB_ASSERT(0);
return false;
}
int src_x = 0;
int src_y = 0;
uint src_x = 0;
uint src_y = 0;
if (dst_x < 0)
{
src_x = -dst_x;
if (src_x >= static_cast<int>(src.get_width()))
return false;
if (src_x >= src.get_width())
return;
dst_x = 0;
}
if (dst_y < 0)
{
src_y = -dst_y;
if (src_y >= static_cast<int>(src.get_height()))
return false;
if (src_y >= src.get_height())
return;
dst_y = 0;
}
if ((dst_x >= (int)m_width) || (dst_y >= (int)m_height))
return false;
return;
uint width = math::minimum(m_width - dst_x, src.get_width() - src_x);
uint height = math::minimum(m_height - dst_y, src.get_height() - src_y);
bool success = unclipped_blit(src_x, src_y, width, height, dst_x, dst_y, src);
bool success = copy(src_x, src_y, width, height, dst_x, dst_y, src);
success;
CRNLIB_ASSERT(success);
return true;
}
// With clipping.
bool blit(int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, const image& src)
{
if ((!is_valid()) || (!src.is_valid()))
{
CRNLIB_ASSERT(0);
return false;
}
rect src_rect(src_x, src_y, src_x + src_w, src_y + src_h);
if (!src_rect.intersect(src.get_bounds()))
return false;
rect dst_rect(dst_x, dst_y, dst_x + src_rect.get_width(), dst_y + src_rect.get_height());
if (!dst_rect.intersect(get_bounds()))
return false;
bool success = unclipped_blit(
src_rect.get_left(), src_rect.get_top(),
math::minimum(src_rect.get_width(), dst_rect.get_width()), math::minimum(src_rect.get_height(), dst_rect.get_height()),
dst_rect.get_left(), dst_rect.get_top(), src);
success;
CRNLIB_ASSERT(success);
return true;
}
// In-place resize of image dimensions (cropping).
bool resize(uint new_width, uint new_height, uint new_pitch = UINT_MAX, const color_type background = color_type::make_black())
{
if (new_pitch == UINT_MAX)
@@ -462,8 +341,6 @@ namespace crnlib
inline uint get_height() const { return m_height; }
inline uint get_total_pixels() const { return m_width * m_height; }
inline rect get_bounds() const { return rect(0, 0, m_width, m_height); }
inline uint get_pitch() const { return m_pitch; }
inline uint get_pitch_in_bytes() const { return m_pitch * sizeof(color_type); }
@@ -491,21 +368,15 @@ namespace crnlib
return m_pPixels[x + y * m_pitch];
}
inline const color_type& get_unclamped(uint x, uint y) const
{
CRNLIB_ASSERT((x < m_width) && (y < m_height));
return m_pPixels[x + y * m_pitch];
}
inline const color_type& get_clamped(int x, int y) const
inline const color_type& get_clamped (int x, int y) const
{
x = math::clamp<int>(x, 0, m_width - 1);
y = math::clamp<int>(y, 0, m_height - 1);
return m_pPixels[x + y * m_pitch];
return (*this)((uint)x, (uint)y);
}
// Sample image with bilinear filtering.
// (x,y) - Continuous coordinates, where pixel centers are at (.5,.5), valid image coords are [0,width] and [0,height].
// (x,y) - Continuous coordinates, where pixel centers are at (.5,.5), valid image coords are (0,width] and (0,height].
void get_filtered(float x, float y, color_type& result) const
{
x -= .5f;
@@ -559,7 +430,7 @@ namespace crnlib
}
}
inline void set_pixel_unclipped(uint x, uint y, const color_type& c)
inline void set_pixel(uint x, uint y, const color_type& c)
{
CRNLIB_ASSERT((x < m_width) && (y < m_height));
m_pPixels[x + y * m_pitch] = c;
@@ -567,7 +438,7 @@ namespace crnlib
inline void set_pixel_clipped(int x, int y, const color_type& c)
{
if ((static_cast<uint>(x) >= m_width) || (static_cast<uint>(y) >= m_height))
if ((x < 0) || (x >= (int)m_width) || (y < 0) || (y >= (int)m_height))
return;
m_pPixels[x + y * m_pitch] = c;
@@ -615,6 +486,7 @@ namespace crnlib
}
int dx = xe - xs, dy = ye - ys;
if (!dx)
{
if (ys > ye)
@@ -631,26 +503,35 @@ namespace crnlib
{
if (dy <= dx)
{
int e = 2 * dy - dx, e_no_inc = 2 * dy, e_inc = 2 * (dy - dx);
int e = 2 * dy - dx;
int e_no_inc = 2 * dy;
int e_inc = 2 * (dy - dx);
rasterize_line(xs, ys, xe, ye, 0, 1, e, e_inc, e_no_inc, color);
}
else
{
int e = 2 * dx - dy, e_no_inc = 2 * dx, e_inc = 2 * (dx - dy);
int e = 2 * dx - dy;
int e_no_inc = 2 * dx;
int e_inc = 2 * (dx - dy);
rasterize_line(xs, ys, xe, ye, 1, 1, e, e_inc, e_no_inc, color);
}
}
else
{
dy = -dy;
if (dy <= dx)
{
int e = 2 * dy - dx, e_no_inc = 2 * dy, e_inc = 2 * (dy - dx);
int e = 2 * dy - dx;
int e_no_inc = 2 * dy;
int e_inc = 2 * (dy - dx);
rasterize_line(xs, ys, xe, ye, 0, -1, e, e_inc, e_no_inc, color);
}
else
{
int e = 2 * dx - dy, e_no_inc = (2 * dx), e_inc = 2 * (dx - dy);
int e = 2 * dx - dy;
int e_no_inc = (2 * dx);
int e_inc = 2 * (dx - dy);
rasterize_line(xe, ye, xs, ys, 1, -1, e, e_inc, e_no_inc, color);
}
}
@@ -676,10 +557,14 @@ namespace crnlib
if (pred)
{
start = ys; end = ye; var = xs;
start = ys;
end = ye;
var = xs;
for (int i = start; i <= end; i++)
{
set_pixel_clipped(var, i, color);
if (e < 0)
e += e_no_inc;
else
@@ -691,10 +576,14 @@ namespace crnlib
}
else
{
start = xs; end = xe; var = ys;
start = xs;
end = xe;
var = ys;
for (int i = start; i <= end; i++)
{
set_pixel_clipped(i, var, color);
if (e < 0)
e += e_no_inc;
else
+62 -260
View File
@@ -6,41 +6,27 @@
#include "crn_resampler.h"
#include "crn_threaded_resampler.h"
#include "crn_strutils.h"
#include "crn_file_utils.h"
#include "crn_threading.h"
#include "crn_miniz.h"
#include "crn_jpge.h"
#include "crn_cfile_stream.h"
#include "crn_mipmapped_texture.h"
#include "crn_buffer_stream.h"
#define STBI_HEADER_FILE_ONLY
#include "crn_stb_image.cpp"
#include "crn_jpgd.h"
#include "crn_pixel_format.h"
namespace crnlib
{
const float cInfinitePSNR = 999999.0f;
const uint CRNLIB_LARGEST_SUPPORTED_IMAGE_DIMENSION = 16384;
namespace image_utils
{
bool read_from_stream_stb(data_stream_serializer &serializer, image_u8& img)
bool load_from_file_stb(const wchar_t* pFilename, image_u8& img)
{
uint8_vec buf;
if (!serializer.read_entire_file(buf))
return false;
int x = 0, y = 0, n = 0;
unsigned char* pData = stbi_load_from_memory(buf.get_ptr(), buf.size_in_bytes(), &x, &y, &n, 4);
unsigned char* pData = stbi_load_w(pFilename, &x, &y, &n, 4);
if (!pData)
return false;
if ((x > (int)CRNLIB_LARGEST_SUPPORTED_IMAGE_DIMENSION) || (y > (int)CRNLIB_LARGEST_SUPPORTED_IMAGE_DIMENSION))
if ((x > 8192) || (y > 8192))
{
stbi_image_free(pData);
return false;
@@ -80,123 +66,30 @@ namespace crnlib
return true;
}
bool read_from_stream_jpgd(data_stream_serializer &serializer, image_u8& img)
bool save_to_file_stb(const wchar_t* pFilename, const image_u8& img, uint save_flags, int comp_index)
{
uint8_vec buf;
if (!serializer.read_entire_file(buf))
return false;
int width = 0, height = 0, actual_comps = 0;
unsigned char *pSrc_img = jpgd::decompress_jpeg_image_from_memory(buf.get_ptr(), buf.size_in_bytes(), &width, &height, &actual_comps, 4);
if (!pSrc_img)
return false;
if (math::maximum(width, height) > (int)CRNLIB_LARGEST_SUPPORTED_IMAGE_DIMENSION)
{
crnlib_free(pSrc_img);
return false;
}
if (!img.grant_ownership(reinterpret_cast<color_quad_u8*>(pSrc_img), width, height))
{
crnlib_free(pSrc_img);
return false;
}
img.reset_comp_flags();
img.set_grayscale(actual_comps == 1);
img.set_component_valid(3, false);
return true;
}
bool read_from_stream(image_u8& dest, data_stream_serializer& serializer, uint read_flags)
{
if (read_flags > cReadFlagsAllFlags)
{
CRNLIB_ASSERT(0);
return false;
}
if (!serializer.get_stream())
{
CRNLIB_ASSERT(0);
return false;
}
dynamic_string ext(serializer.get_name());
file_utils::get_extension(ext);
if ((ext == "jpg") || (ext == "jpeg"))
{
// Use my jpeg decoder by default because it supports progressive jpeg's.
if ((read_flags & cReadFlagForceSTB) == 0)
{
return image_utils::read_from_stream_jpgd(serializer, dest);
}
}
return image_utils::read_from_stream_stb(serializer, dest);
}
bool read_from_file(image_u8& dest, const char* pFilename, uint read_flags)
{
if (read_flags > cReadFlagsAllFlags)
{
CRNLIB_ASSERT(0);
return false;
}
cfile_stream file_stream;
if (!file_stream.open(pFilename))
return false;
data_stream_serializer serializer(file_stream);
return read_from_stream(dest, serializer, read_flags);
}
bool write_to_file(const char* pFilename, const image_u8& img, uint write_flags, int grayscale_comp_index)
{
if ((grayscale_comp_index < -1) || (grayscale_comp_index > 3))
{
CRNLIB_ASSERT(0);
return false;
}
if (!img.get_width())
{
CRNLIB_ASSERT(0);
return false;
}
dynamic_string ext(pFilename);
bool is_jpeg = false;
if (file_utils::get_extension(ext))
bool bSaveBMP = false;
dynamic_wstring ext(pFilename);
if (get_extension(ext))
{
is_jpeg = ((ext == "jpg") || (ext == "jpeg"));
if ((ext != "png") && (ext != "bmp") && (ext != "tga") && (!is_jpeg))
if (ext == L"bmp")
bSaveBMP = true;
else if (ext != L"tga")
{
console::error("crnlib::image_utils::write_to_file: Can only write .BMP, .TGA, .PNG, or .JPG files!\n");
console::error(L"crnlib::image_utils::save_to_file_stb: Can only write .BMP or .TGA files!\n");
return false;
}
}
crnlib::vector<uint8> temp;
uint num_src_chans = 0;
const void *pSrc_img = NULL;
if (is_jpeg)
if ((img.get_comp_flags() & pixel_format_helpers::cCompFlagGrayscale) || (save_flags & image_utils::cSaveGrayscale))
{
write_flags |= cWriteFlagIgnoreAlpha;
}
CRNLIB_ASSERT(comp_index < 4);
if (comp_index > 3) comp_index = 3;
if ((img.get_comp_flags() & pixel_format_helpers::cCompFlagGrayscale) || (write_flags & image_utils::cWriteFlagGrayscale))
{
CRNLIB_ASSERT(grayscale_comp_index < 4);
if (grayscale_comp_index > 3) grayscale_comp_index = 3;
temp.resize(img.get_total_pixels());
crnlib::vector<uint8> temp(img.get_total_pixels());
for (uint y = 0; y < img.get_height(); y++)
{
@@ -209,7 +102,7 @@ namespace crnlib
while (pSrc != pSrc_end)
*pDst++ = (*pSrc++)[1];
}
else if (grayscale_comp_index < 0)
else if (comp_index < 0)
{
while (pSrc != pSrc_end)
*pDst++ = static_cast<uint8>((*pSrc++).get_luma());
@@ -217,16 +110,15 @@ namespace crnlib
else
{
while (pSrc != pSrc_end)
*pDst++ = (*pSrc++)[grayscale_comp_index];
*pDst++ = (*pSrc++)[comp_index];
}
}
pSrc_img = &temp[0];
num_src_chans = 1;
return (bSaveBMP ? stbi_write_bmp_w : stbi_write_tga_w)(pFilename, img.get_width(), img.get_height(), 1, &temp[0]) == CRNLIB_TRUE;
}
else if ((!img.is_component_valid(3)) || (write_flags & cWriteFlagIgnoreAlpha))
else if ((!img.is_component_valid(3)) || (save_flags & cSaveIgnoreAlpha))
{
temp.resize(img.get_total_pixels() * 3);
crnlib::vector<uint8> temp(img.get_total_pixels() * 3);
for (uint y = 0; y < img.get_height(); y++)
{
@@ -246,47 +138,37 @@ namespace crnlib
}
}
num_src_chans = 3;
pSrc_img = &temp[0];
return (bSaveBMP ? stbi_write_bmp_w : stbi_write_tga_w)(pFilename, img.get_width(), img.get_height(), 3, &temp[0]) == CRNLIB_TRUE;
}
else
{
num_src_chans = 4;
pSrc_img = img.get_ptr();
return (bSaveBMP ? stbi_write_bmp_w : stbi_write_tga_w)(pFilename, img.get_width(), img.get_height(), 4, img.get_ptr()) == CRNLIB_TRUE;
}
}
bool success = false;
if (ext == "png")
{
size_t png_image_size = 0;
void *pPNG_image_data = tdefl_write_image_to_png_file_in_memory(pSrc_img, img.get_width(), img.get_height(), num_src_chans, &png_image_size);
if (!pPNG_image_data)
return false;
success = file_utils::write_buf_to_file(pFilename, pPNG_image_data, png_image_size);
mz_free(pPNG_image_data);
}
else if (is_jpeg)
{
jpge::params params;
if (write_flags & cWriteFlagJPEGQualityLevelMask)
params.m_quality = math::clamp<uint>((write_flags & cWriteFlagJPEGQualityLevelMask) >> cWriteFlagJPEGQualityLevelShift, 1U, 100U);
params.m_two_pass_flag = (write_flags & cWriteFlagJPEGTwoPass) != 0;
params.m_no_chroma_discrim_flag = (write_flags & cWriteFlagJPEGNoChromaDiscrim) != 0;
bool load_from_file(image_u8& dest, const wchar_t* pFilename, int flags)
{
flags;
return image_utils::load_from_file_stb(pFilename, dest);
}
if (write_flags & cWriteFlagJPEGH1V1)
params.m_subsampling = jpge::H1V1;
else if (write_flags & cWriteFlagJPEGH2V1)
params.m_subsampling = jpge::H2V1;
else if (write_flags & cWriteFlagJPEGH2V2)
params.m_subsampling = jpge::H2V2;
bool save_to_grayscale_file(const wchar_t* pFilename, const image_u8& src, int component, int flags)
{
flags;
return image_utils::save_to_file_stb(pFilename, src, image_utils::cSaveGrayscale, component);
}
success = jpge::compress_image_to_jpeg_file(pFilename, img.get_width(), img.get_height(), num_src_chans, (const jpge::uint8*)pSrc_img, params);
}
bool save_to_file(const wchar_t* pFilename, const image_u8& src, int flags, bool ignore_alpha)
{
if (src.is_grayscale())
return save_to_grayscale_file(pFilename, src, cSaveLuma, flags);
else
{
success = ((ext == "bmp" ? stbi_write_bmp : stbi_write_tga)(pFilename, img.get_width(), img.get_height(), num_src_chans, pSrc_img) == CRNLIB_TRUE);
uint save_flags = 0;
if (ignore_alpha)
save_flags |= image_utils::cSaveIgnoreAlpha;
return image_utils::save_to_file_stb(pFilename, src, save_flags);
}
return success;
}
bool has_alpha(const image_u8& img)
@@ -338,7 +220,7 @@ namespace crnlib
}
}
bool is_normal_map(const image_u8& img, const char* pFilename)
bool is_normal_map(const image_u8& img, const wchar_t* pFilename)
{
float score = 0.0f;
@@ -377,13 +259,13 @@ namespace crnlib
if (pFilename)
{
dynamic_string str(pFilename);
dynamic_wstring str(pFilename);
str.tolower();
if (str.contains("normal") || str.contains("local") || str.contains("nmap"))
if (str.contains(L"normal") || str.contains(L"local") || str.contains(L"nmap"))
score += 1.0f;
if (str.contains("diffuse") || str.contains("spec") || str.contains("gloss"))
if (str.contains(L"diffuse") || str.contains(L"spec") || str.contains(L"gloss"))
score -= 1.0f;
}
@@ -830,34 +712,32 @@ namespace crnlib
if (!total_blocks)
return 0.0f;
//save_to_file_stb_or_miniz("ssim.tga", yimg, cWriteFlagGrayscale);
//save_to_file_stb(L"ssim.tga", yimg, cSaveGrayscale);
return total_ssim / total_blocks;
}
void print_ssim(const image_u8& src_img, const image_u8& dst_img)
{
src_img;
dst_img;
//double y_ssim = compute_ssim(src_img, dst_img, -1);
//console::printf("Luma MSSIM: %f, Scaled: %f", y_ssim, (y_ssim - .8f) / .2f);
double y_ssim = compute_ssim(src_img, dst_img, -1);
console::printf(L"Luma MSSIM: %f, Scaled: %f", y_ssim, (y_ssim - .8f) / .2f);
//double r_ssim = compute_ssim(src_img, dst_img, 0);
//console::printf(" R MSSIM: %f", r_ssim);
//console::printf(L" R MSSIM: %f", r_ssim);
//double g_ssim = compute_ssim(src_img, dst_img, 1);
//console::printf(" G MSSIM: %f", g_ssim);
//console::printf(L" G MSSIM: %f", g_ssim);
//double b_ssim = compute_ssim(src_img, dst_img, 2);
//console::printf(" B MSSIM: %f", b_ssim);
//console::printf(L" B MSSIM: %f", b_ssim);
}
void error_metrics::print(const char* pName) const
void error_metrics::print(const wchar_t* pName) const
{
if (mPeakSNR >= cInfinitePSNR)
console::printf("%s Error: Max: %3u, Mean: %3.3f, MSE: %3.3f, RMSE: %3.3f, PSNR: Infinite", pName, mMax, mMean, mMeanSquared, mRootMeanSquared);
console::printf(L"%s Error: Max: %3u, Mean: %3.3f, RMS: %3.3f, PSNR: Infinite", pName, mMax, mMean, mRootMeanSquared);
else
console::printf("%s Error: Max: %3u, Mean: %3.3f, MSE: %3.3f, RMSE: %3.3f, PSNR: %3.3f", pName, mMax, mMean, mMeanSquared, mRootMeanSquared, mPeakSNR);
console::printf(L"%s Error: Max: %3u, Mean: %3.3f, RMS: %3.3f, PSNR: %3.3f", pName, mMax, mMean, mRootMeanSquared, mPeakSNR);
}
bool error_metrics::compute(const image_u8& a, const image_u8& b, uint first_channel, uint num_channels, bool average_component_error)
@@ -928,35 +808,35 @@ namespace crnlib
void print_image_metrics(const image_u8& src_img, const image_u8& dst_img)
{
if ( (!src_img.get_width()) || (!dst_img.get_height()) || (src_img.get_width() != dst_img.get_width()) || (src_img.get_height() != dst_img.get_height()) )
console::printf("print_image_metrics: Image resolutions don't match exactly (%ux%u) vs. (%ux%u)", src_img.get_width(), src_img.get_height(), dst_img.get_width(), dst_img.get_height());
console::printf(L"print_image_metrics: Image resolutions don't match exactly (%ux%u) vs. (%ux%u)", src_img.get_width(), src_img.get_height(), dst_img.get_width(), dst_img.get_height());
image_utils::error_metrics error_metrics;
if (src_img.has_rgb() || dst_img.has_rgb())
{
error_metrics.compute(src_img, dst_img, 0, 3, false);
error_metrics.print("RGB Total ");
error_metrics.print(L"RGB Total ");
error_metrics.compute(src_img, dst_img, 0, 3, true);
error_metrics.print("RGB Average");
error_metrics.print(L"RGB Average");
error_metrics.compute(src_img, dst_img, 0, 0);
error_metrics.print("Luma ");
error_metrics.print(L"Luma ");
error_metrics.compute(src_img, dst_img, 0, 1);
error_metrics.print("Red ");
error_metrics.print(L"Red ");
error_metrics.compute(src_img, dst_img, 1, 1);
error_metrics.print("Green ");
error_metrics.print(L"Green ");
error_metrics.compute(src_img, dst_img, 2, 1);
error_metrics.print("Blue ");
error_metrics.print(L"Blue ");
}
if (src_img.has_alpha() || dst_img.has_alpha())
{
error_metrics.compute(src_img, dst_img, 3, 1);
error_metrics.print("Alpha ");
error_metrics.print(L"Alpha ");
}
}
@@ -1042,11 +922,6 @@ namespace crnlib
img.set_comp_flags(static_cast<pixel_format_helpers::component_flags>(pixel_format_helpers::cCompFlagRValid | pixel_format_helpers::cCompFlagGValid | pixel_format_helpers::cCompFlagBValid | pixel_format_helpers::cCompFlagGrayscale | (img.has_alpha() ? pixel_format_helpers::cCompFlagAValid : 0)));
break;
}
case cConversion_To_Y:
{
img.set_comp_flags(static_cast<pixel_format_helpers::component_flags>(img.get_comp_flags() | pixel_format_helpers::cCompFlagGrayscale));
break;
}
default:
{
CRNLIB_ASSERT(false);
@@ -1156,15 +1031,6 @@ namespace crnlib
dst.a = src.a;
break;
}
case image_utils::cConversion_To_Y:
{
uint8 y = static_cast<uint8>(src.get_luma());
dst.r = y;
dst.g = y;
dst.b = y;
dst.a = src.a;
break;
}
default:
{
CRNLIB_ASSERT(false);
@@ -1298,70 +1164,6 @@ namespace crnlib
return sqrt(var);
}
uint8* read_image_from_memory(const uint8* pImage, int nSize, int* pWidth, int* pHeight, int* pActualComps, int req_comps, const char* pFilename)
{
*pWidth = 0;
*pHeight = 0;
*pActualComps = 0;
if ((req_comps < 1) || (req_comps > 4))
return false;
mipmapped_texture tex;
buffer_stream buf_stream(pImage, nSize);
buf_stream.set_name(pFilename);
data_stream_serializer serializer(buf_stream);
if (!tex.read_from_stream(serializer))
return NULL;
if (tex.is_packed())
{
if (!tex.unpack_from_dxt(true))
return NULL;
}
image_u8 img;
image_u8* pImg = tex.get_level_image(0, 0, img);
if (!pImg)
return NULL;
*pWidth = tex.get_width();
*pHeight = tex.get_height();
if (pImg->has_alpha())
*pActualComps = 4;
else if (pImg->is_grayscale())
*pActualComps = 1;
else
*pActualComps = 3;
uint8 *pDst = NULL;
if (req_comps == 4)
{
pDst = (uint8*)malloc(tex.get_total_pixels() * sizeof(uint32));
uint8 *pSrc = (uint8*)pImg->get_ptr();
memcpy(pDst, pSrc, tex.get_total_pixels() * sizeof(uint32));
}
else
{
image_u8 luma_img;
if (req_comps == 1)
{
luma_img = *pImg;
luma_img.convert_to_grayscale();
pImg = &luma_img;
}
pixel_packer packer(req_comps, 8);
uint32 n;
pDst = image_utils::pack_image(*pImg, packer, n);
}
return pDst;
}
} // namespace image_utils
} // namespace crnlib
+40 -92
View File
@@ -2,54 +2,35 @@
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#include "crn_image.h"
#include "crn_data_stream_serializer.h"
namespace crnlib
{
enum pixel_format;
namespace image_utils
{
enum read_flags_t
bool load_from_file_stb(const wchar_t* pFilename, image_u8& img);
enum
{
cReadFlagForceSTB = 1,
cReadFlagsAllFlags = 1
cSaveIgnoreAlpha = 1,
cSaveGrayscale = 2
};
bool read_from_stream_stb(data_stream_serializer& serializer, image_u8& img);
bool read_from_stream_jpgd(data_stream_serializer& serializer, image_u8& img);
bool read_from_stream(image_u8& dest, data_stream_serializer& serializer, uint read_flags = 0);
bool read_from_file(image_u8& dest, const char* pFilename, uint read_flags = 0);
const int cSaveLuma = -1;
// Reads texture from memory, results returned stb_image.c style.
// *pActual_comps is set to 1, 3, or 4. req_comps must range from 1-4.
uint8* read_from_memory(const uint8* pImage, int nSize, int* pWidth, int* pHeight, int* pActualComps, int req_comps, const char* pFilename);
bool save_to_file_stb(const wchar_t* pFilename, const image_u8& img, uint save_flags = 0, int comp_index = cSaveLuma);
bool load_from_file(image_u8& dest, const wchar_t* pFilename, int flags = 0);
enum
{
cWriteFlagIgnoreAlpha = 0x00000001,
cWriteFlagGrayscale = 0x00000002,
cWriteFlagJPEGH1V1 = 0x00010000,
cWriteFlagJPEGH2V1 = 0x00020000,
cWriteFlagJPEGH2V2 = 0x00040000,
cWriteFlagJPEGTwoPass = 0x00080000,
cWriteFlagJPEGNoChromaDiscrim = 0x00100000,
cWriteFlagJPEGQualityLevelMask = 0xFF000000,
cWriteFlagJPEGQualityLevelShift = 24,
};
const int cLumaComponentIndex = -1;
inline uint create_jpeg_write_flags(uint base_flags, uint quality_level) { CRNLIB_ASSERT(quality_level <= 100); return base_flags | ((quality_level << cWriteFlagJPEGQualityLevelShift) & cWriteFlagJPEGQualityLevelMask); }
bool write_to_file(const char* pFilename, const image_u8& img, uint write_flags = 0, int grayscale_comp_index = cLumaComponentIndex);
bool save_to_grayscale_file(const wchar_t* pFilename, const image_u8& src, int component, int flags = 0);
bool save_to_file(const wchar_t* pFilename, const image_u8& src, int flags = 0, bool ignore_alpha = false);
bool has_alpha(const image_u8& img);
bool is_normal_map(const image_u8& img, const char* pFilename = NULL);
bool is_normal_map(const image_u8& img, const wchar_t* pFilename = NULL);
void renorm_normal_map(image_u8& img);
struct resample_params
{
resample_params() :
@@ -65,7 +46,7 @@ namespace crnlib
m_multithreaded(true)
{
}
uint m_dst_width;
uint m_dst_height;
const char* m_pFilter;
@@ -77,40 +58,40 @@ namespace crnlib
float m_source_gamma;
bool m_multithreaded;
};
bool resample_single_thread(const image_u8& src, image_u8& dst, const resample_params& params);
bool resample_multithreaded(const image_u8& src, image_u8& dst, const resample_params& params);
bool resample(const image_u8& src, image_u8& dst, const resample_params& params);
bool compute_delta(image_u8& dest, image_u8& a, image_u8& b, uint scale = 2);
class error_metrics
{
public:
error_metrics() { utils::zero_this(this); }
void print(const char* pName) const;
void print(const wchar_t* pName) const;
// If num_channels==0, luma error is computed.
// If pHist != NULL, it must point to a 256 entry array.
bool compute(const image_u8& a, const image_u8& b, uint first_channel, uint num_channels, bool average_component_error = true);
uint mMax;
double mMean;
double mMeanSquared;
double mRootMeanSquared;
double mPeakSNR;
inline bool operator== (const error_metrics& other) const
{
return mPeakSNR == other.mPeakSNR;
}
inline bool operator< (const error_metrics& other) const
{
return mPeakSNR < other.mPeakSNR;
}
inline bool operator> (const error_metrics& other) const
{
return mPeakSNR > other.mPeakSNR;
@@ -118,76 +99,43 @@ namespace crnlib
};
void print_image_metrics(const image_u8& src_img, const image_u8& dst_img);
double compute_block_ssim(uint n, const uint8* pX, const uint8* pY);
double compute_ssim(const image_u8& a, const image_u8& b, int channel_index);
void print_ssim(const image_u8& src_img, const image_u8& dst_img);
enum conversion_type
{
cConversion_Invalid = -1,
cConversion_To_CCxY,
cConversion_From_CCxY,
cConversion_To_xGxR,
cConversion_From_xGxR,
cConversion_To_xGBR,
cConversion_From_xGBR,
cConversion_To_AGBR,
cConversion_From_AGBR,
cConversion_XY_to_XYZ,
cConversion_Y_To_A,
cConversion_A_To_RGBA,
cConversion_Y_To_RGB,
cConversion_To_Y,
cConversionTotal
};
void convert_image(image_u8& img, conversion_type conv_type);
template<typename image_type>
inline uint8* pack_image(const image_type& img, const pixel_packer& packer, uint& n)
{
n = 0;
if (!packer.is_valid())
return NULL;
const uint width = img.get_width(), height = img.get_height();
uint dst_pixel_stride = packer.get_pixel_stride();
uint dst_pitch = width * dst_pixel_stride;
n = dst_pitch * height;
uint8* pImage = static_cast<uint8*>(crnlib_malloc(n));
uint8* pDst = pImage;
for (uint y = 0; y < height; y++)
{
const typename image_type::color_t* pSrc = img.get_scanline(y);
for (uint x = 0; x < width; x++)
pDst = (uint8*)packer.pack(*pSrc++, pDst);
}
return pImage;
}
image_utils::conversion_type get_conversion_type(bool cooking, pixel_format fmt);
image_utils::conversion_type get_image_conversion_type_from_crn_format(crn_format fmt);
double compute_std_dev(uint n, const color_quad_u8* pPixels, uint first_channel, uint num_channels);
uint8* read_image_from_memory(const uint8* pImage, int nSize, int* pWidth, int* pHeight, int* pActualComps, int req_comps, const char* pFilename);
} // namespace image_utils
} // namespace crnlib
}
}
-3172
View File
File diff suppressed because it is too large Load Diff
-319
View File
@@ -1,319 +0,0 @@
// jpgd.h - C++ class for JPEG decompression.
// Public domain, Rich Geldreich <richgel99@gmail.com>
#ifndef JPEG_DECODER_H
#define JPEG_DECODER_H
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#ifdef _MSC_VER
#define JPGD_NORETURN __declspec(noreturn)
#elif defined(__GNUC__)
#define JPGD_NORETURN __attribute__ ((noreturn))
#else
#define JPGD_NORETURN
#endif
namespace jpgd
{
typedef unsigned char uint8;
typedef signed short int16;
typedef unsigned short uint16;
typedef unsigned int uint;
typedef signed int int32;
// Loads a JPEG image from a memory buffer or a file.
// req_comps can be 1 (grayscale), 3 (RGB), or 4 (RGBA).
// On return, width/height will be set to the image's dimensions, and actual_comps will be set to the either 1 (grayscale) or 3 (RGB).
// Notes: For more control over where and how the source data is read, see the decompress_jpeg_image_from_stream() function below, or call the jpeg_decoder class directly.
// Requesting a 8 or 32bpp image is currently a little faster than 24bpp because the jpeg_decoder class itself currently always unpacks to either 8 or 32bpp.
unsigned char *decompress_jpeg_image_from_memory(const unsigned char *pSrc_data, int src_data_size, int *width, int *height, int *actual_comps, int req_comps);
unsigned char *decompress_jpeg_image_from_file(const char *pSrc_filename, int *width, int *height, int *actual_comps, int req_comps);
// Success/failure error codes.
enum jpgd_status
{
JPGD_SUCCESS = 0, JPGD_FAILED = -1, JPGD_DONE = 1,
JPGD_BAD_DHT_COUNTS = -256, JPGD_BAD_DHT_INDEX, JPGD_BAD_DHT_MARKER, JPGD_BAD_DQT_MARKER, JPGD_BAD_DQT_TABLE,
JPGD_BAD_PRECISION, JPGD_BAD_HEIGHT, JPGD_BAD_WIDTH, JPGD_TOO_MANY_COMPONENTS,
JPGD_BAD_SOF_LENGTH, JPGD_BAD_VARIABLE_MARKER, JPGD_BAD_DRI_LENGTH, JPGD_BAD_SOS_LENGTH,
JPGD_BAD_SOS_COMP_ID, JPGD_W_EXTRA_BYTES_BEFORE_MARKER, JPGD_NO_ARITHMITIC_SUPPORT, JPGD_UNEXPECTED_MARKER,
JPGD_NOT_JPEG, JPGD_UNSUPPORTED_MARKER, JPGD_BAD_DQT_LENGTH, JPGD_TOO_MANY_BLOCKS,
JPGD_UNDEFINED_QUANT_TABLE, JPGD_UNDEFINED_HUFF_TABLE, JPGD_NOT_SINGLE_SCAN, JPGD_UNSUPPORTED_COLORSPACE,
JPGD_UNSUPPORTED_SAMP_FACTORS, JPGD_DECODE_ERROR, JPGD_BAD_RESTART_MARKER, JPGD_ASSERTION_ERROR,
JPGD_BAD_SOS_SPECTRAL, JPGD_BAD_SOS_SUCCESSIVE, JPGD_STREAM_READ, JPGD_NOTENOUGHMEM
};
// Input stream interface.
// Derive from this class to read input data from sources other than files or memory. Set m_eof_flag to true when no more data is available.
// The decoder is rather greedy: it will keep on calling this method until its internal input buffer is full, or until the EOF flag is set.
// It the input stream contains data after the JPEG stream's EOI (end of image) marker it will probably be pulled into the internal buffer.
// Call the get_total_bytes_read() method to determine the actual size of the JPEG stream after successful decoding.
class jpeg_decoder_stream
{
public:
jpeg_decoder_stream() { }
virtual ~jpeg_decoder_stream() { }
// The read() method is called when the internal input buffer is empty.
// Parameters:
// pBuf - input buffer
// max_bytes_to_read - maximum bytes that can be written to pBuf
// pEOF_flag - set this to true if at end of stream (no more bytes remaining)
// Returns -1 on error, otherwise return the number of bytes actually written to the buffer (which may be 0).
// Notes: This method will be called in a loop until you set *pEOF_flag to true or the internal buffer is full.
virtual int read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag) = 0;
};
// stdio FILE stream class.
class jpeg_decoder_file_stream : public jpeg_decoder_stream
{
jpeg_decoder_file_stream(const jpeg_decoder_file_stream &);
jpeg_decoder_file_stream &operator =(const jpeg_decoder_file_stream &);
FILE *m_pFile;
bool m_eof_flag, m_error_flag;
public:
jpeg_decoder_file_stream();
virtual ~jpeg_decoder_file_stream();
bool open(const char *Pfilename);
void close();
virtual int read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag);
};
// Memory stream class.
class jpeg_decoder_mem_stream : public jpeg_decoder_stream
{
const uint8 *m_pSrc_data;
uint m_ofs, m_size;
public:
jpeg_decoder_mem_stream() : m_pSrc_data(NULL), m_ofs(0), m_size(0) { }
jpeg_decoder_mem_stream(const uint8 *pSrc_data, uint size) : m_pSrc_data(pSrc_data), m_ofs(0), m_size(size) { }
virtual ~jpeg_decoder_mem_stream() { }
bool open(const uint8 *pSrc_data, uint size);
void close() { m_pSrc_data = NULL; m_ofs = 0; m_size = 0; }
virtual int read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag);
};
// Loads JPEG file from a jpeg_decoder_stream.
unsigned char *decompress_jpeg_image_from_stream(jpeg_decoder_stream *pStream, int *width, int *height, int *actual_comps, int req_comps);
enum
{
JPGD_IN_BUF_SIZE = 8192, JPGD_MAX_BLOCKS_PER_MCU = 10, JPGD_MAX_HUFF_TABLES = 8, JPGD_MAX_QUANT_TABLES = 4,
JPGD_MAX_COMPONENTS = 4, JPGD_MAX_COMPS_IN_SCAN = 4, JPGD_MAX_BLOCKS_PER_ROW = 8192, JPGD_MAX_HEIGHT = 16384, JPGD_MAX_WIDTH = 16384
};
typedef int16 jpgd_quant_t;
typedef int16 jpgd_block_t;
class jpeg_decoder
{
public:
// Call get_error_code() after constructing to determine if the stream is valid or not. You may call the get_width(), get_height(), etc.
// methods after the constructor is called. You may then either destruct the object, or begin decoding the image by calling begin_decoding(), then decode() on each scanline.
jpeg_decoder(jpeg_decoder_stream *pStream);
~jpeg_decoder();
// Call this method after constructing the object to begin decompression.
// If JPGD_SUCCESS is returned you may then call decode() on each scanline.
int begin_decoding();
// Returns the next scan line.
// For grayscale images, pScan_line will point to a buffer containing 8-bit pixels (get_bytes_per_pixel() will return 1).
// Otherwise, it will always point to a buffer containing 32-bit RGBA pixels (A will always be 255, and get_bytes_per_pixel() will return 4).
// Returns JPGD_SUCCESS if a scan line has been returned.
// Returns JPGD_DONE if all scan lines have been returned.
// Returns JPGD_FAILED if an error occurred. Call get_error_code() for a more info.
int decode(const void** pScan_line, uint* pScan_line_len);
inline jpgd_status get_error_code() const { return m_error_code; }
inline int get_width() const { return m_image_x_size; }
inline int get_height() const { return m_image_y_size; }
inline int get_num_components() const { return m_comps_in_frame; }
inline int get_bytes_per_pixel() const { return m_dest_bytes_per_pixel; }
inline int get_bytes_per_scan_line() const { return m_image_x_size * get_bytes_per_pixel(); }
// Returns the total number of bytes actually consumed by the decoder (which should equal the actual size of the JPEG file).
inline int get_total_bytes_read() const { return m_total_bytes_read; }
private:
jpeg_decoder(const jpeg_decoder &);
jpeg_decoder &operator =(const jpeg_decoder &);
typedef void (*pDecode_block_func)(jpeg_decoder *, int, int, int);
struct huff_tables
{
bool ac_table;
uint look_up[256];
uint look_up2[256];
uint8 code_size[256];
uint tree[512];
};
struct coeff_buf
{
uint8 *pData;
int block_num_x, block_num_y;
int block_len_x, block_len_y;
int block_size;
};
struct mem_block
{
mem_block *m_pNext;
size_t m_used_count;
size_t m_size;
char m_data[1];
};
jmp_buf m_jmp_state;
mem_block *m_pMem_blocks;
int m_image_x_size;
int m_image_y_size;
jpeg_decoder_stream *m_pStream;
int m_progressive_flag;
uint8 m_huff_ac[JPGD_MAX_HUFF_TABLES];
uint8* m_huff_num[JPGD_MAX_HUFF_TABLES]; // pointer to number of Huffman codes per bit size
uint8* m_huff_val[JPGD_MAX_HUFF_TABLES]; // pointer to Huffman codes per bit size
jpgd_quant_t* m_quant[JPGD_MAX_QUANT_TABLES]; // pointer to quantization tables
int m_scan_type; // Gray, Yh1v1, Yh1v2, Yh2v1, Yh2v2 (CMYK111, CMYK4114 no longer supported)
int m_comps_in_frame; // # of components in frame
int m_comp_h_samp[JPGD_MAX_COMPONENTS]; // component's horizontal sampling factor
int m_comp_v_samp[JPGD_MAX_COMPONENTS]; // component's vertical sampling factor
int m_comp_quant[JPGD_MAX_COMPONENTS]; // component's quantization table selector
int m_comp_ident[JPGD_MAX_COMPONENTS]; // component's ID
int m_comp_h_blocks[JPGD_MAX_COMPONENTS];
int m_comp_v_blocks[JPGD_MAX_COMPONENTS];
int m_comps_in_scan; // # of components in scan
int m_comp_list[JPGD_MAX_COMPS_IN_SCAN]; // components in this scan
int m_comp_dc_tab[JPGD_MAX_COMPONENTS]; // component's DC Huffman coding table selector
int m_comp_ac_tab[JPGD_MAX_COMPONENTS]; // component's AC Huffman coding table selector
int m_spectral_start; // spectral selection start
int m_spectral_end; // spectral selection end
int m_successive_low; // successive approximation low
int m_successive_high; // successive approximation high
int m_max_mcu_x_size; // MCU's max. X size in pixels
int m_max_mcu_y_size; // MCU's max. Y size in pixels
int m_blocks_per_mcu;
int m_max_blocks_per_row;
int m_mcus_per_row, m_mcus_per_col;
int m_mcu_org[JPGD_MAX_BLOCKS_PER_MCU];
int m_total_lines_left; // total # lines left in image
int m_mcu_lines_left; // total # lines left in this MCU
int m_real_dest_bytes_per_scan_line;
int m_dest_bytes_per_scan_line; // rounded up
int m_dest_bytes_per_pixel; // 4 (RGB) or 1 (Y)
huff_tables* m_pHuff_tabs[JPGD_MAX_HUFF_TABLES];
coeff_buf* m_dc_coeffs[JPGD_MAX_COMPONENTS];
coeff_buf* m_ac_coeffs[JPGD_MAX_COMPONENTS];
int m_eob_run;
int m_block_y_mcu[JPGD_MAX_COMPONENTS];
uint8* m_pIn_buf_ofs;
int m_in_buf_left;
int m_tem_flag;
bool m_eof_flag;
uint8 m_in_buf_pad_start[128];
uint8 m_in_buf[JPGD_IN_BUF_SIZE + 128];
uint8 m_in_buf_pad_end[128];
int m_bits_left;
uint m_bit_buf;
int m_restart_interval;
int m_restarts_left;
int m_next_restart_num;
int m_max_mcus_per_row;
int m_max_blocks_per_mcu;
int m_expanded_blocks_per_mcu;
int m_expanded_blocks_per_row;
int m_expanded_blocks_per_component;
bool m_freq_domain_chroma_upsample;
int m_max_mcus_per_col;
uint m_last_dc_val[JPGD_MAX_COMPONENTS];
jpgd_block_t* m_pMCU_coefficients;
int m_mcu_block_max_zag[JPGD_MAX_BLOCKS_PER_MCU];
uint8* m_pSample_buf;
int m_crr[256];
int m_cbb[256];
int m_crg[256];
int m_cbg[256];
uint8* m_pScan_line_0;
uint8* m_pScan_line_1;
jpgd_status m_error_code;
bool m_ready_flag;
int m_total_bytes_read;
void free_all_blocks();
JPGD_NORETURN void stop_decoding(jpgd_status status);
void *alloc(size_t n, bool zero = false);
void word_clear(void *p, uint16 c, uint n);
void prep_in_buffer();
void read_dht_marker();
void read_dqt_marker();
void read_sof_marker();
void skip_variable_marker();
void read_dri_marker();
void read_sos_marker();
int next_marker();
int process_markers();
void locate_soi_marker();
void locate_sof_marker();
int locate_sos_marker();
void init(jpeg_decoder_stream * pStream);
void create_look_ups();
void fix_in_buffer();
void transform_mcu(int mcu_row);
void transform_mcu_expand(int mcu_row);
coeff_buf* coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y);
inline jpgd_block_t *coeff_buf_getp(coeff_buf *cb, int block_x, int block_y);
void load_next_row();
void decode_next_row();
void make_huff_table(int index, huff_tables *pH);
void check_quant_tables();
void check_huff_tables();
void calc_mcu_block_order();
int init_scan();
void init_frame();
void process_restart();
void decode_scan(pDecode_block_func decode_block_func);
void init_progressive();
void init_sequential();
void decode_start();
void decode_init(jpeg_decoder_stream * pStream);
void H2V2Convert();
void H2V1Convert();
void H1V2Convert();
void H1V1Convert();
void gray_convert();
void expanded_convert();
void find_eoi();
inline uint get_char();
inline uint get_char(bool *pPadding_flag);
inline void stuff_char(uint8 q);
inline uint8 get_octet();
inline uint get_bits(int num_bits);
inline uint get_bits_no_markers(int numbits);
inline int huff_decode(huff_tables *pH);
inline int huff_decode(huff_tables *pH, int& extrabits);
static inline uint8 clamp(int i);
static void decode_block_dc_first(jpeg_decoder *pD, int component_id, int block_x, int block_y);
static void decode_block_dc_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y);
static void decode_block_ac_first(jpeg_decoder *pD, int component_id, int block_x, int block_y);
static void decode_block_ac_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y);
};
} // namespace jpgd
#endif // JPEG_DECODER_H
-1044
View File
File diff suppressed because it is too large Load Diff
-169
View File
@@ -1,169 +0,0 @@
// jpge.h - C++ class for JPEG compression.
// Public domain, Rich Geldreich <richgel99@gmail.com>
// Alex Evans: Added RGBA support, linear memory allocator.
#ifndef JPEG_ENCODER_H
#define JPEG_ENCODER_H
namespace jpge
{
typedef unsigned char uint8;
typedef signed short int16;
typedef signed int int32;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned int uint;
// JPEG chroma subsampling factors. Y_ONLY (grayscale images) and H2V2 (color images) are the most common.
enum subsampling_t { Y_ONLY = 0, H1V1 = 1, H2V1 = 2, H2V2 = 3 };
// JPEG compression parameters structure.
struct params
{
inline params() : m_quality(85), m_subsampling(H2V2), m_no_chroma_discrim_flag(false), m_two_pass_flag(false) { }
inline bool check() const
{
if ((m_quality < 1) || (m_quality > 100)) return false;
if ((uint)m_subsampling > (uint)H2V2) return false;
return true;
}
// Quality: 1-100, higher is better. Typical values are around 50-95.
int m_quality;
// m_subsampling:
// 0 = Y (grayscale) only
// 1 = YCbCr, no subsampling (H1V1, YCbCr 1x1x1, 3 blocks per MCU)
// 2 = YCbCr, H2V1 subsampling (YCbCr 2x1x1, 4 blocks per MCU)
// 3 = YCbCr, H2V2 subsampling (YCbCr 4x1x1, 6 blocks per MCU-- very common)
subsampling_t m_subsampling;
// Disables CbCr discrimination - only intended for testing.
// If true, the Y quantization table is also used for the CbCr channels.
bool m_no_chroma_discrim_flag;
bool m_two_pass_flag;
};
// Writes JPEG image to a file.
// num_channels must be 1 (Y) or 3 (RGB), image pitch must be width*num_channels.
bool compress_image_to_jpeg_file(const char *pFilename, int width, int height, int num_channels, const uint8 *pImage_data, const params &comp_params = params());
// Writes JPEG image to memory buffer.
// On entry, buf_size is the size of the output buffer pointed at by pBuf, which should be at least ~1024 bytes.
// If return value is true, buf_size will be set to the size of the compressed data.
bool compress_image_to_jpeg_file_in_memory(void *pBuf, int &buf_size, int width, int height, int num_channels, const uint8 *pImage_data, const params &comp_params = params());
// Output stream abstract class - used by the jpeg_encoder class to write to the output stream.
// put_buf() is generally called with len==JPGE_OUT_BUF_SIZE bytes, but for headers it'll be called with smaller amounts.
class output_stream
{
public:
virtual ~output_stream() { };
virtual bool put_buf(const void* Pbuf, int len) = 0;
template<class T> inline bool put_obj(const T& obj) { return put_buf(&obj, sizeof(T)); }
};
// Lower level jpeg_encoder class - useful if more control is needed than the above helper functions.
class jpeg_encoder
{
public:
jpeg_encoder();
~jpeg_encoder();
// Initializes the compressor.
// pStream: The stream object to use for writing compressed data.
// params - Compression parameters structure, defined above.
// width, height - Image dimensions.
// channels - May be 1, or 3. 1 indicates grayscale, 3 indicates RGB source data.
// Returns false on out of memory or if a stream write fails.
bool init(output_stream *pStream, int width, int height, int src_channels, const params &comp_params = params());
const params &get_params() const { return m_params; }
// Deinitializes the compressor, freeing any allocated memory. May be called at any time.
void deinit();
uint get_total_passes() const { return m_params.m_two_pass_flag ? 2 : 1; }
inline uint get_cur_pass() { return m_pass_num; }
// Call this method with each source scanline.
// width * src_channels bytes per scanline is expected (RGB or Y format).
// You must call with NULL after all scanlines are processed to finish compression.
// Returns false on out of memory or if a stream write fails.
bool process_scanline(const void* pScanline);
private:
jpeg_encoder(const jpeg_encoder &);
jpeg_encoder &operator =(const jpeg_encoder &);
typedef int32 sample_array_t;
output_stream *m_pStream;
params m_params;
uint8 m_num_components;
uint8 m_comp_h_samp[3], m_comp_v_samp[3];
int m_image_x, m_image_y, m_image_bpp, m_image_bpl;
int m_image_x_mcu, m_image_y_mcu;
int m_image_bpl_xlt, m_image_bpl_mcu;
int m_mcus_per_row;
int m_mcu_x, m_mcu_y;
uint8 *m_mcu_lines[16];
uint8 m_mcu_y_ofs;
sample_array_t m_sample_array[64];
int16 m_coefficient_array[64];
int32 m_quantization_tables[2][64];
uint m_huff_codes[4][256];
uint8 m_huff_code_sizes[4][256];
uint8 m_huff_bits[4][17];
uint8 m_huff_val[4][256];
uint32 m_huff_count[4][256];
int m_last_dc_val[3];
enum { JPGE_OUT_BUF_SIZE = 2048 };
uint8 m_out_buf[JPGE_OUT_BUF_SIZE];
uint8 *m_pOut_buf;
uint m_out_buf_left;
uint32 m_bit_buffer;
uint m_bits_in;
uint8 m_pass_num;
bool m_all_stream_writes_succeeded;
void optimize_huffman_table(int table_num, int table_len);
void emit_byte(uint8 i);
void emit_word(uint i);
void emit_marker(int marker);
void emit_jfif_app0();
void emit_dqt();
void emit_sof();
void emit_dht(uint8 *bits, uint8 *val, int index, bool ac_flag);
void emit_dhts();
void emit_sos();
void emit_markers();
void compute_huffman_table(uint *codes, uint8 *code_sizes, uint8 *bits, uint8 *val);
void compute_quant_table(int32 *dst, int16 *src);
void adjust_quant_table(int32 *dst, int32 *src);
void first_pass_init();
bool second_pass_init();
bool jpg_open(int p_x_res, int p_y_res, int src_channels);
void load_block_8_8_grey(int x);
void load_block_8_8(int x, int y, int c);
void load_block_16_8(int x, int c);
void load_block_16_8_8(int x, int c);
void load_quantized_coefficients(int component_num);
void flush_output_buffer();
void put_bits(uint bits, uint len);
void code_coefficients_pass_one(int component_num);
void code_coefficients_pass_two(int component_num);
void code_block(int component_num);
void process_mcu_row();
bool terminate_pass_one();
bool terminate_pass_two();
bool process_end_of_image();
void load_mcu(const void* src);
void clear();
void init();
};
} // namespace jpge
#endif // JPEG_ENCODER
-920
View File
@@ -1,920 +0,0 @@
// File: crn_ktx_texture.cpp
#include "crn_core.h"
#include "crn_ktx_texture.h"
#include "crn_console.h"
// Set #if CRNLIB_KTX_PVRTEX_WORKAROUNDS to 1 to enable various workarounds for oddball KTX files written by PVRTexTool.
#define CRNLIB_KTX_PVRTEX_WORKAROUNDS 1
namespace crnlib
{
const uint8 s_ktx_file_id[12] = { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
bool is_packed_pixel_ogl_type(uint32 ogl_type)
{
switch (ogl_type)
{
case KTX_UNSIGNED_BYTE_3_3_2:
case KTX_UNSIGNED_BYTE_2_3_3_REV:
case KTX_UNSIGNED_SHORT_5_6_5:
case KTX_UNSIGNED_SHORT_5_6_5_REV:
case KTX_UNSIGNED_SHORT_4_4_4_4:
case KTX_UNSIGNED_SHORT_4_4_4_4_REV:
case KTX_UNSIGNED_SHORT_5_5_5_1:
case KTX_UNSIGNED_SHORT_1_5_5_5_REV:
case KTX_UNSIGNED_INT_8_8_8_8:
case KTX_UNSIGNED_INT_8_8_8_8_REV:
case KTX_UNSIGNED_INT_10_10_10_2:
case KTX_UNSIGNED_INT_2_10_10_10_REV:
case KTX_UNSIGNED_INT_24_8:
case KTX_UNSIGNED_INT_10F_11F_11F_REV:
case KTX_UNSIGNED_INT_5_9_9_9_REV:
return true;
}
return false;
}
uint get_ogl_type_size(uint32 ogl_type)
{
switch (ogl_type)
{
case KTX_UNSIGNED_BYTE:
case KTX_BYTE:
return 1;
case KTX_HALF_FLOAT:
case KTX_UNSIGNED_SHORT:
case KTX_SHORT:
return 2;
case KTX_FLOAT:
case KTX_UNSIGNED_INT:
case KTX_INT:
return 4;
case KTX_UNSIGNED_BYTE_3_3_2:
case KTX_UNSIGNED_BYTE_2_3_3_REV:
return 1;
case KTX_UNSIGNED_SHORT_5_6_5:
case KTX_UNSIGNED_SHORT_5_6_5_REV:
case KTX_UNSIGNED_SHORT_4_4_4_4:
case KTX_UNSIGNED_SHORT_4_4_4_4_REV:
case KTX_UNSIGNED_SHORT_5_5_5_1:
case KTX_UNSIGNED_SHORT_1_5_5_5_REV:
return 2;
case KTX_UNSIGNED_INT_8_8_8_8:
case KTX_UNSIGNED_INT_8_8_8_8_REV:
case KTX_UNSIGNED_INT_10_10_10_2:
case KTX_UNSIGNED_INT_2_10_10_10_REV:
case KTX_UNSIGNED_INT_24_8:
case KTX_UNSIGNED_INT_10F_11F_11F_REV:
case KTX_UNSIGNED_INT_5_9_9_9_REV:
return 4;
}
return 0;
}
uint32 get_ogl_base_internal_fmt(uint32 ogl_fmt)
{
switch (ogl_fmt)
{
case KTX_ETC1_RGB8_OES:
case KTX_RGB_S3TC:
case KTX_RGB4_S3TC:
case KTX_COMPRESSED_RGB_S3TC_DXT1_EXT:
case KTX_COMPRESSED_SRGB_S3TC_DXT1_EXT:
return KTX_RGB;
case KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT:
case KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
case KTX_RGBA_S3TC:
case KTX_RGBA4_S3TC:
case KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT:
case KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
case KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT:
case KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
case KTX_RGBA_DXT5_S3TC:
case KTX_RGBA4_DXT5_S3TC:
return KTX_RGBA;
case 1:
case KTX_RED:
case KTX_RED_INTEGER:
case KTX_GREEN:
case KTX_GREEN_INTEGER:
case KTX_BLUE:
case KTX_BLUE_INTEGER:
case KTX_R8:
case KTX_R8UI:
case KTX_LUMINANCE8:
case KTX_ALPHA:
case KTX_LUMINANCE:
case KTX_COMPRESSED_RED_RGTC1_EXT:
case KTX_COMPRESSED_SIGNED_RED_RGTC1_EXT:
case KTX_COMPRESSED_LUMINANCE_LATC1_EXT:
case KTX_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
return KTX_RED;
case 2:
case KTX_RG:
case KTX_RG8:
case KTX_RG_INTEGER:
case KTX_LUMINANCE_ALPHA:
case KTX_COMPRESSED_RED_GREEN_RGTC2_EXT:
case KTX_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT:
case KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
case KTX_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
return KTX_RG;
case 3:
case KTX_SRGB:
case KTX_RGB:
case KTX_RGB_INTEGER:
case KTX_BGR:
case KTX_BGR_INTEGER:
case KTX_RGB8:
case KTX_SRGB8:
return KTX_RGB;
case 4:
case KTX_RGBA:
case KTX_BGRA:
case KTX_RGBA_INTEGER:
case KTX_BGRA_INTEGER:
case KTX_SRGB_ALPHA:
case KTX_SRGB8_ALPHA8:
case KTX_RGBA8:
return KTX_RGBA;
}
return 0;
}
bool get_ogl_fmt_desc(uint32 ogl_fmt, uint32 ogl_type, uint& block_dim, uint& bytes_per_block)
{
uint ogl_type_size = get_ogl_type_size(ogl_type);
block_dim = 1;
bytes_per_block = 0;
switch (ogl_fmt)
{
case KTX_COMPRESSED_RED_RGTC1_EXT:
case KTX_COMPRESSED_SIGNED_RED_RGTC1_EXT:
case KTX_COMPRESSED_LUMINANCE_LATC1_EXT:
case KTX_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
case KTX_ETC1_RGB8_OES:
case KTX_RGB_S3TC:
case KTX_RGB4_S3TC:
case KTX_COMPRESSED_RGB_S3TC_DXT1_EXT:
case KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT:
case KTX_COMPRESSED_SRGB_S3TC_DXT1_EXT:
case KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
{
block_dim = 4;
bytes_per_block = 8;
break;
}
case KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
case KTX_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
case KTX_COMPRESSED_RED_GREEN_RGTC2_EXT:
case KTX_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT:
case KTX_RGBA_S3TC:
case KTX_RGBA4_S3TC:
case KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT:
case KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
case KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT:
case KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
case KTX_RGBA_DXT5_S3TC:
case KTX_RGBA4_DXT5_S3TC:
{
block_dim = 4;
bytes_per_block = 16;
break;
}
case 1:
case KTX_ALPHA:
case KTX_RED:
case KTX_GREEN:
case KTX_BLUE:
case KTX_RED_INTEGER:
case KTX_GREEN_INTEGER:
case KTX_BLUE_INTEGER:
case KTX_LUMINANCE:
{
bytes_per_block = ogl_type_size;
break;
}
case KTX_R8:
case KTX_R8UI:
case KTX_ALPHA8:
case KTX_LUMINANCE8:
{
bytes_per_block = 1;
break;
}
case 2:
case KTX_RG:
case KTX_RG_INTEGER:
case KTX_LUMINANCE_ALPHA:
{
bytes_per_block = 2 * ogl_type_size;
break;
}
case KTX_RG8:
case KTX_LUMINANCE8_ALPHA8:
{
bytes_per_block = 2;
break;
}
case 3:
case KTX_SRGB:
case KTX_RGB:
case KTX_BGR:
case KTX_RGB_INTEGER:
case KTX_BGR_INTEGER:
{
bytes_per_block = is_packed_pixel_ogl_type(ogl_type) ? ogl_type_size : (3 * ogl_type_size);
break;
}
case KTX_RGB8:
case KTX_SRGB8:
{
bytes_per_block = 3;
break;
}
case 4:
case KTX_RGBA:
case KTX_BGRA:
case KTX_RGBA_INTEGER:
case KTX_BGRA_INTEGER:
case KTX_SRGB_ALPHA:
{
bytes_per_block = is_packed_pixel_ogl_type(ogl_type) ? ogl_type_size : (4 * ogl_type_size);
break;
}
case KTX_SRGB8_ALPHA8:
case KTX_RGBA8:
{
bytes_per_block = 4;
break;
}
default:
return false;
}
return true;
}
bool ktx_texture::compute_pixel_info()
{
if ((!m_header.m_glType) || (!m_header.m_glFormat))
{
if ((m_header.m_glType) || (m_header.m_glFormat))
return false;
// Must be a compressed format.
if (!get_ogl_fmt_desc(m_header.m_glInternalFormat, m_header.m_glType, m_block_dim, m_bytes_per_block))
{
#if CRNLIB_KTX_PVRTEX_WORKAROUNDS
if ((!m_header.m_glInternalFormat) && (!m_header.m_glType) && (!m_header.m_glTypeSize) && (!m_header.m_glBaseInternalFormat))
{
// PVRTexTool writes bogus headers when outputting ETC1.
console::warning("ktx_texture::compute_pixel_info: Header doesn't specify any format, assuming ETC1 and hoping for the best");
m_header.m_glBaseInternalFormat = KTX_RGB;
m_header.m_glInternalFormat = KTX_ETC1_RGB8_OES;
m_header.m_glTypeSize = 1;
m_block_dim = 4;
m_bytes_per_block = 8;
return true;
}
#endif
return false;
}
if (m_block_dim == 1)
return false;
}
else
{
// Must be an uncompressed format.
if (!get_ogl_fmt_desc(m_header.m_glFormat, m_header.m_glType, m_block_dim, m_bytes_per_block))
return false;
if (m_block_dim > 1)
return false;
}
return true;
}
bool ktx_texture::read_from_stream(data_stream_serializer& serializer)
{
clear();
// Read header
if (serializer.read(&m_header, 1, sizeof(m_header)) != sizeof(ktx_header))
return false;
// Check header
if (memcmp(s_ktx_file_id, m_header.m_identifier, sizeof(m_header.m_identifier)))
return false;
if ((m_header.m_endianness != KTX_OPPOSITE_ENDIAN) && (m_header.m_endianness != KTX_ENDIAN))
return false;
m_opposite_endianness = (m_header.m_endianness == KTX_OPPOSITE_ENDIAN);
if (m_opposite_endianness)
{
m_header.endian_swap();
if ((m_header.m_glTypeSize != sizeof(uint8)) && (m_header.m_glTypeSize != sizeof(uint16)) && (m_header.m_glTypeSize != sizeof(uint32)))
return false;
}
if (!check_header())
return false;
if (!compute_pixel_info())
return false;
uint8 pad_bytes[3];
// Read the key value entries
uint num_key_value_bytes_remaining = m_header.m_bytesOfKeyValueData;
while (num_key_value_bytes_remaining)
{
if (num_key_value_bytes_remaining < sizeof(uint32))
return false;
uint32 key_value_byte_size;
if (serializer.read(&key_value_byte_size, 1, sizeof(uint32)) != sizeof(uint32))
return false;
num_key_value_bytes_remaining -= sizeof(uint32);
if (m_opposite_endianness)
key_value_byte_size = utils::swap32(key_value_byte_size);
if (key_value_byte_size > num_key_value_bytes_remaining)
return false;
uint8_vec key_value_data;
if (key_value_byte_size)
{
key_value_data.resize(key_value_byte_size);
if (serializer.read(&key_value_data[0], 1, key_value_byte_size) != key_value_byte_size)
return false;
}
m_key_values.push_back(key_value_data);
uint padding = 3 - ((key_value_byte_size + 3) % 4);
if (padding)
{
if (serializer.read(pad_bytes, 1, padding) != padding)
return false;
}
num_key_value_bytes_remaining -= key_value_byte_size;
if (num_key_value_bytes_remaining < padding)
return false;
num_key_value_bytes_remaining -= padding;
}
// Now read the mip levels
uint total_faces = get_num_mips() * get_array_size() * get_num_faces() * get_depth();
if ((!total_faces) || (total_faces > 65535))
return false;
// See Section 2.8 of KTX file format: No rounding to block sizes should be applied for block compressed textures.
// OK, I'm going to break that rule otherwise KTX can only store a subset of textures that DDS can handle for no good reason.
#if 0
const uint mip0_row_blocks = m_header.m_pixelWidth / m_block_dim;
const uint mip0_col_blocks = CRNLIB_MAX(1, m_header.m_pixelHeight) / m_block_dim;
#else
const uint mip0_row_blocks = (m_header.m_pixelWidth + m_block_dim - 1) / m_block_dim;
const uint mip0_col_blocks = (CRNLIB_MAX(1, m_header.m_pixelHeight) + m_block_dim - 1) / m_block_dim;
#endif
if ((!mip0_row_blocks) || (!mip0_col_blocks))
return false;
const uint mip0_depth = CRNLIB_MAX(1, m_header.m_pixelDepth); mip0_depth;
bool has_valid_image_size_fields = true;
bool disable_mip_and_cubemap_padding = false;
#if CRNLIB_KTX_PVRTEX_WORKAROUNDS
{
// PVRTexTool has a bogus KTX writer that doesn't write any imageSize fields. Nice.
size_t expected_bytes_remaining = 0;
for (uint mip_level = 0; mip_level < get_num_mips(); mip_level++)
{
uint mip_width, mip_height, mip_depth;
get_mip_dim(mip_level, mip_width, mip_height, mip_depth);
const uint mip_row_blocks = (mip_width + m_block_dim - 1) / m_block_dim;
const uint mip_col_blocks = (mip_height + m_block_dim - 1) / m_block_dim;
if ((!mip_row_blocks) || (!mip_col_blocks))
return false;
expected_bytes_remaining += sizeof(uint32);
if ((!m_header.m_numberOfArrayElements) && (get_num_faces() == 6))
{
for (uint face = 0; face < get_num_faces(); face++)
{
uint slice_size = mip_row_blocks * mip_col_blocks * m_bytes_per_block;
expected_bytes_remaining += slice_size;
uint num_cube_pad_bytes = 3 - ((slice_size + 3) % 4);
expected_bytes_remaining += num_cube_pad_bytes;
}
}
else
{
uint total_mip_size = 0;
for (uint array_element = 0; array_element < get_array_size(); array_element++)
{
for (uint face = 0; face < get_num_faces(); face++)
{
for (uint zslice = 0; zslice < mip_depth; zslice++)
{
uint slice_size = mip_row_blocks * mip_col_blocks * m_bytes_per_block;
total_mip_size += slice_size;
}
}
}
expected_bytes_remaining += total_mip_size;
uint num_mip_pad_bytes = 3 - ((total_mip_size + 3) % 4);
expected_bytes_remaining += num_mip_pad_bytes;
}
}
if (serializer.get_stream()->get_remaining() < expected_bytes_remaining)
{
has_valid_image_size_fields = false;
disable_mip_and_cubemap_padding = true;
console::warning("ktx_texture::read_from_stream: KTX file size is smaller than expected - trying to read anyway without imageSize fields");
}
}
#endif
for (uint mip_level = 0; mip_level < get_num_mips(); mip_level++)
{
uint mip_width, mip_height, mip_depth;
get_mip_dim(mip_level, mip_width, mip_height, mip_depth);
const uint mip_row_blocks = (mip_width + m_block_dim - 1) / m_block_dim;
const uint mip_col_blocks = (mip_height + m_block_dim - 1) / m_block_dim;
if ((!mip_row_blocks) || (!mip_col_blocks))
return false;
uint32 image_size = 0;
if (!has_valid_image_size_fields)
image_size = mip_depth * mip_row_blocks * mip_col_blocks * m_bytes_per_block * get_array_size() * get_num_faces();
else
{
if (serializer.read(&image_size, 1, sizeof(image_size)) != sizeof(image_size))
return false;
if (m_opposite_endianness)
image_size = utils::swap32(image_size);
}
if (!image_size)
return false;
uint total_mip_size = 0;
if ((!m_header.m_numberOfArrayElements) && (get_num_faces() == 6))
{
// plain non-array cubemap
for (uint face = 0; face < get_num_faces(); face++)
{
CRNLIB_ASSERT(m_image_data.size() == get_image_index(mip_level, 0, face, 0));
m_image_data.push_back(uint8_vec());
uint8_vec& image_data = m_image_data.back();
image_data.resize(image_size);
if (serializer.read(&image_data[0], 1, image_size) != image_size)
return false;
if (m_opposite_endianness)
utils::endian_swap_mem(&image_data[0], image_size, m_header.m_glTypeSize);
uint num_cube_pad_bytes = disable_mip_and_cubemap_padding ? 0 : (3 - ((image_size + 3) % 4));
if (serializer.read(pad_bytes, 1, num_cube_pad_bytes) != num_cube_pad_bytes)
return false;
total_mip_size += image_size + num_cube_pad_bytes;
}
}
else
{
// 1D, 2D, 3D (normal or array texture), or array cubemap
uint num_image_bytes_remaining = image_size;
for (uint array_element = 0; array_element < get_array_size(); array_element++)
{
for (uint face = 0; face < get_num_faces(); face++)
{
for (uint zslice = 0; zslice < mip_depth; zslice++)
{
CRNLIB_ASSERT(m_image_data.size() == get_image_index(mip_level, array_element, face, zslice));
uint slice_size = mip_row_blocks * mip_col_blocks * m_bytes_per_block;
if ((!slice_size) || (slice_size > num_image_bytes_remaining))
return false;
m_image_data.push_back(uint8_vec());
uint8_vec& image_data = m_image_data.back();
image_data.resize(slice_size);
if (serializer.read(&image_data[0], 1, slice_size) != slice_size)
return false;
if (m_opposite_endianness)
utils::endian_swap_mem(&image_data[0], slice_size, m_header.m_glTypeSize);
num_image_bytes_remaining -= slice_size;
total_mip_size += slice_size;
}
}
}
if (num_image_bytes_remaining)
return false;
}
uint num_mip_pad_bytes = disable_mip_and_cubemap_padding ? 0 : (3 - ((total_mip_size + 3) % 4));
if (serializer.read(pad_bytes, 1, num_mip_pad_bytes) != num_mip_pad_bytes)
return false;
}
return true;
}
bool ktx_texture::write_to_stream(data_stream_serializer& serializer, bool no_keyvalue_data)
{
if (!consistency_check())
{
CRNLIB_ASSERT(0);
return false;
}
memcpy(m_header.m_identifier, s_ktx_file_id, sizeof(m_header.m_identifier));
m_header.m_endianness = m_opposite_endianness ? KTX_OPPOSITE_ENDIAN : KTX_ENDIAN;
if (m_block_dim == 1)
{
m_header.m_glTypeSize = get_ogl_type_size(m_header.m_glType);
m_header.m_glBaseInternalFormat = m_header.m_glFormat;
}
else
{
m_header.m_glBaseInternalFormat = get_ogl_base_internal_fmt(m_header.m_glInternalFormat);
}
m_header.m_bytesOfKeyValueData = 0;
if (!no_keyvalue_data)
{
for (uint i = 0; i < m_key_values.size(); i++)
m_header.m_bytesOfKeyValueData += sizeof(uint32) + ((m_key_values[i].size() + 3) & ~3);
}
if (m_opposite_endianness)
m_header.endian_swap();
bool success = (serializer.write(&m_header, sizeof(m_header), 1) == 1);
if (m_opposite_endianness)
m_header.endian_swap();
if (!success)
return success;
uint total_key_value_bytes = 0;
const uint8 padding[3] = { 0, 0, 0 };
if (!no_keyvalue_data)
{
for (uint i = 0; i < m_key_values.size(); i++)
{
uint32 key_value_size = m_key_values[i].size();
if (m_opposite_endianness)
key_value_size = utils::swap32(key_value_size);
success = (serializer.write(&key_value_size, sizeof(key_value_size), 1) == 1);
total_key_value_bytes += sizeof(key_value_size);
if (m_opposite_endianness)
key_value_size = utils::swap32(key_value_size);
if (!success)
return false;
if (key_value_size)
{
if (serializer.write(&m_key_values[i][0], key_value_size, 1) != 1)
return false;
total_key_value_bytes += key_value_size;
uint num_padding = 3 - ((key_value_size + 3) % 4);
if ((num_padding) && (serializer.write(padding, num_padding, 1) != 1))
return false;
total_key_value_bytes += num_padding;
}
}
(void)total_key_value_bytes;
}
CRNLIB_ASSERT(total_key_value_bytes == m_header.m_bytesOfKeyValueData);
for (uint mip_level = 0; mip_level < get_num_mips(); mip_level++)
{
uint mip_width, mip_height, mip_depth;
get_mip_dim(mip_level, mip_width, mip_height, mip_depth);
const uint mip_row_blocks = (mip_width + m_block_dim - 1) / m_block_dim;
const uint mip_col_blocks = (mip_height + m_block_dim - 1) / m_block_dim;
if ((!mip_row_blocks) || (!mip_col_blocks))
return false;
uint32 image_size = mip_row_blocks * mip_col_blocks * m_bytes_per_block;
if ((m_header.m_numberOfArrayElements) || (get_num_faces() == 1))
image_size *= (get_array_size() * get_num_faces() * get_depth());
if (!image_size)
return false;
if (m_opposite_endianness)
image_size = utils::swap32(image_size);
success = (serializer.write(&image_size, sizeof(image_size), 1) == 1);
if (m_opposite_endianness)
image_size = utils::swap32(image_size);
if (!success)
return false;
uint total_mip_size = 0;
if ((!m_header.m_numberOfArrayElements) && (get_num_faces() == 6))
{
// plain non-array cubemap
for (uint face = 0; face < get_num_faces(); face++)
{
const uint8_vec& image_data = get_image_data(get_image_index(mip_level, 0, face, 0));
if ((!image_data.size()) || (image_data.size() != image_size))
return false;
if (m_opposite_endianness)
{
uint8_vec tmp_image_data(image_data);
utils::endian_swap_mem(&tmp_image_data[0], tmp_image_data.size(), m_header.m_glTypeSize);
if (serializer.write(&tmp_image_data[0], tmp_image_data.size(), 1) != 1)
return false;
}
else if (serializer.write(&image_data[0], image_data.size(), 1) != 1)
return false;
uint num_cube_pad_bytes = 3 - ((image_data.size() + 3) % 4);
if ((num_cube_pad_bytes) && (serializer.write(padding, num_cube_pad_bytes, 1) != 1))
return false;
total_mip_size += image_size + num_cube_pad_bytes;
}
}
else
{
// 1D, 2D, 3D (normal or array texture), or array cubemap
for (uint array_element = 0; array_element < get_array_size(); array_element++)
{
for (uint face = 0; face < get_num_faces(); face++)
{
for (uint zslice = 0; zslice < mip_depth; zslice++)
{
const uint8_vec& image_data = get_image_data(get_image_index(mip_level, array_element, face, zslice));
if (!image_data.size())
return false;
if (m_opposite_endianness)
{
uint8_vec tmp_image_data(image_data);
utils::endian_swap_mem(&tmp_image_data[0], tmp_image_data.size(), m_header.m_glTypeSize);
if (serializer.write(&tmp_image_data[0], tmp_image_data.size(), 1) != 1)
return false;
}
else if (serializer.write(&image_data[0], image_data.size(), 1) != 1)
return false;
total_mip_size += image_data.size();
}
}
}
uint num_mip_pad_bytes = 3 - ((total_mip_size + 3) % 4);
if ((num_mip_pad_bytes) && (serializer.write(padding, num_mip_pad_bytes, 1) != 1))
return false;
total_mip_size += num_mip_pad_bytes;
}
CRNLIB_ASSERT((total_mip_size & 3) == 0);
}
return true;
}
bool ktx_texture::init_2D(uint width, uint height, uint num_mips, uint32 ogl_internal_fmt, uint32 ogl_fmt, uint32 ogl_type)
{
clear();
m_header.m_pixelWidth = width;
m_header.m_pixelHeight = height;
m_header.m_numberOfMipmapLevels = num_mips;
m_header.m_glInternalFormat = ogl_internal_fmt;
m_header.m_glFormat = ogl_fmt;
m_header.m_glType = ogl_type;
m_header.m_numberOfFaces = 1;
if (!compute_pixel_info())
return false;
return true;
}
bool ktx_texture::init_2D_array(uint width, uint height, uint num_mips, uint array_size, uint32 ogl_internal_fmt, uint32 ogl_fmt, uint32 ogl_type)
{
clear();
m_header.m_pixelWidth = width;
m_header.m_pixelHeight = height;
m_header.m_numberOfMipmapLevels = num_mips;
m_header.m_numberOfArrayElements = array_size;
m_header.m_glInternalFormat = ogl_internal_fmt;
m_header.m_glFormat = ogl_fmt;
m_header.m_glType = ogl_type;
m_header.m_numberOfFaces = 1;
if (!compute_pixel_info())
return false;
return true;
}
bool ktx_texture::init_3D(uint width, uint height, uint depth, uint num_mips, uint32 ogl_internal_fmt, uint32 ogl_fmt, uint32 ogl_type)
{
clear();
m_header.m_pixelWidth = width;
m_header.m_pixelHeight = height;
m_header.m_pixelDepth = depth;
m_header.m_numberOfMipmapLevels = num_mips;
m_header.m_glInternalFormat = ogl_internal_fmt;
m_header.m_glFormat = ogl_fmt;
m_header.m_glType = ogl_type;
m_header.m_numberOfFaces = 1;
if (!compute_pixel_info())
return false;
return true;
}
bool ktx_texture::init_cubemap(uint dim, uint num_mips, uint32 ogl_internal_fmt, uint32 ogl_fmt, uint32 ogl_type)
{
clear();
m_header.m_pixelWidth = dim;
m_header.m_pixelHeight = dim;
m_header.m_numberOfMipmapLevels = num_mips;
m_header.m_glInternalFormat = ogl_internal_fmt;
m_header.m_glFormat = ogl_fmt;
m_header.m_glType = ogl_type;
m_header.m_numberOfFaces = 6;
if (!compute_pixel_info())
return false;
return true;
}
bool ktx_texture::check_header() const
{
if (((get_num_faces() != 1) && (get_num_faces() != 6)) || (!m_header.m_pixelWidth))
return false;
if ((!m_header.m_pixelHeight) && (m_header.m_pixelDepth))
return false;
if ((get_num_faces() == 6) && ((m_header.m_pixelDepth) || (!m_header.m_pixelHeight)))
return false;
if (m_header.m_numberOfMipmapLevels)
{
const uint max_mipmap_dimension = 1U << (m_header.m_numberOfMipmapLevels - 1U);
if (max_mipmap_dimension > (CRNLIB_MAX(CRNLIB_MAX(m_header.m_pixelWidth, m_header.m_pixelHeight), m_header.m_pixelDepth)))
return false;
}
return true;
}
bool ktx_texture::consistency_check() const
{
if (!check_header())
return false;
uint block_dim = 0, bytes_per_block = 0;
if ((!m_header.m_glType) || (!m_header.m_glFormat))
{
if ((m_header.m_glType) || (m_header.m_glFormat))
return false;
if (!get_ogl_fmt_desc(m_header.m_glInternalFormat, m_header.m_glType, block_dim, bytes_per_block))
return false;
if (block_dim == 1)
return false;
//if ((get_width() % block_dim) || (get_height() % block_dim))
// return false;
}
else
{
if (!get_ogl_fmt_desc(m_header.m_glFormat, m_header.m_glType, block_dim, bytes_per_block))
return false;
if (block_dim > 1)
return false;
}
if ((m_block_dim != block_dim) || (m_bytes_per_block != bytes_per_block))
return false;
if (m_image_data.size() != get_total_images())
return false;
for (uint mip_level = 0; mip_level < get_num_mips(); mip_level++)
{
uint mip_width, mip_height, mip_depth;
get_mip_dim(mip_level, mip_width, mip_height, mip_depth);
const uint mip_row_blocks = (mip_width + m_block_dim - 1) / m_block_dim;
const uint mip_col_blocks = (mip_height + m_block_dim - 1) / m_block_dim;
if ((!mip_row_blocks) || (!mip_col_blocks))
return false;
for (uint array_element = 0; array_element < get_array_size(); array_element++)
{
for (uint face = 0; face < get_num_faces(); face++)
{
for (uint zslice = 0; zslice < mip_depth; zslice++)
{
const uint8_vec& image_data = get_image_data(get_image_index(mip_level, array_element, face, zslice));
uint expected_image_size = mip_row_blocks * mip_col_blocks * m_bytes_per_block;
if (image_data.size() != expected_image_size)
return false;
}
}
}
}
return true;
}
const uint8_vec* ktx_texture::find_key(const char* pKey) const
{
const size_t n = strlen(pKey) + 1;
for (uint i = 0; i < m_key_values.size(); i++)
{
const uint8_vec& v = m_key_values[i];
if ((v.size() >= n) && (!memcmp(&v[0], pKey, n)))
return &v;
}
return NULL;
}
bool ktx_texture::get_key_value_as_string(const char* pKey, dynamic_string& str) const
{
const uint8_vec* p = find_key(pKey);
if (!p)
{
str.clear();
return false;
}
const uint ofs = (static_cast<uint>(strlen(pKey)) + 1);
const uint8* pValue = p->get_ptr() + ofs;
const uint n = p->size() - ofs;
uint i;
for (i = 0; i < n; i++)
if (!pValue[i])
break;
str.set_from_buf(pValue, i);
return true;
}
uint ktx_texture::add_key_value(const char* pKey, const void* pVal, uint val_size)
{
const uint idx = m_key_values.size();
m_key_values.resize(idx + 1);
uint8_vec& v = m_key_values.back();
v.append(reinterpret_cast<const uint8*>(pKey), static_cast<uint>(strlen(pKey)) + 1);
v.append(static_cast<const uint8*>(pVal), val_size);
return idx;
}
} // namespace crnlib
-244
View File
@@ -1,244 +0,0 @@
// File: crn_ktx_texture.h
#ifndef _KTX_TEXTURE_H_
#define _KTX_TEXTURE_H_
#ifdef _MSC_VER
#pragma once
#endif
#include "crn_data_stream_serializer.h"
#define KTX_ENDIAN 0x04030201
#define KTX_OPPOSITE_ENDIAN 0x01020304
namespace crnlib
{
extern const uint8 s_ktx_file_id[12];
struct ktx_header
{
uint8 m_identifier[12];
uint32 m_endianness;
uint32 m_glType;
uint32 m_glTypeSize;
uint32 m_glFormat;
uint32 m_glInternalFormat;
uint32 m_glBaseInternalFormat;
uint32 m_pixelWidth;
uint32 m_pixelHeight;
uint32 m_pixelDepth;
uint32 m_numberOfArrayElements;
uint32 m_numberOfFaces;
uint32 m_numberOfMipmapLevels;
uint32 m_bytesOfKeyValueData;
void clear()
{
memset(this, 0, sizeof(*this));
}
void endian_swap()
{
utils::endian_swap_mem32(&m_endianness, (sizeof(*this) - sizeof(m_identifier)) / sizeof(uint32));
}
};
typedef crnlib::vector<uint8_vec> ktx_key_value_vec;
typedef crnlib::vector<uint8_vec> ktx_image_data_vec;
// Compressed pixel data formats: ETC1, DXT1, DXT3, DXT5
enum
{
KTX_ETC1_RGB8_OES = 0x8D64, KTX_RGB_S3TC = 0x83A0, KTX_RGB4_S3TC = 0x83A1, KTX_COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0,
KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1, KTX_COMPRESSED_SRGB_S3TC_DXT1_EXT = 0x8C4C, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT = 0x8C4D,
KTX_RGBA_S3TC = 0x83A2, KTX_RGBA4_S3TC = 0x83A3, KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT = 0x8C4E,
KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT = 0x8C4F, KTX_RGBA_DXT5_S3TC = 0x83A4, KTX_RGBA4_DXT5_S3TC = 0x83A5,
KTX_COMPRESSED_RED_RGTC1_EXT = 0x8DBB, KTX_COMPRESSED_SIGNED_RED_RGTC1_EXT = 0x8DBC, KTX_COMPRESSED_RED_GREEN_RGTC2_EXT = 0x8DBD, KTX_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT = 0x8DBE,
KTX_COMPRESSED_LUMINANCE_LATC1_EXT = 0x8C70, KTX_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT = 0x8C71, KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT = 0x8C72, KTX_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT = 0x8C73
};
// Pixel formats (various internal, base, and base internal formats)
enum
{
KTX_R8 = 0x8229, KTX_R8UI = 0x8232, KTX_RGB8 = 0x8051, KTX_SRGB8 = 0x8C41, KTX_SRGB = 0x8C40, KTX_SRGB_ALPHA = 0x8C42,
KTX_SRGB8_ALPHA8 = 0x8C43, KTX_RGBA8 = 0x8058, KTX_STENCIL_INDEX = 0x1901, KTX_DEPTH_COMPONENT = 0x1902, KTX_DEPTH_STENCIL = 0x84F9, KTX_RED = 0x1903,
KTX_GREEN = 0x1904, KTX_BLUE = 0x1905, KTX_ALPHA = 0x1906, KTX_RG = 0x8227, KTX_RGB = 0x1907, KTX_RGBA = 0x1908, KTX_BGR = 0x80E0, KTX_BGRA = 0x80E1,
KTX_RED_INTEGER = 0x8D94, KTX_GREEN_INTEGER = 0x8D95, KTX_BLUE_INTEGER = 0x8D96, KTX_ALPHA_INTEGER = 0x8D97, KTX_RGB_INTEGER = 0x8D98, KTX_RGBA_INTEGER = 0x8D99,
KTX_BGR_INTEGER = 0x8D9A, KTX_BGRA_INTEGER = 0x8D9B, KTX_LUMINANCE = 0x1909, KTX_LUMINANCE_ALPHA = 0x190A, KTX_RG_INTEGER = 0x8228, KTX_RG8 = 0x822B,
KTX_ALPHA8 = 0x803C, KTX_LUMINANCE8 = 0x8040, KTX_LUMINANCE8_ALPHA8 = 0x8045
};
// Pixel data types
enum
{
KTX_UNSIGNED_BYTE = 0x1401, KTX_BYTE = 0x1400, KTX_UNSIGNED_SHORT = 0x1403, KTX_SHORT = 0x1402,
KTX_UNSIGNED_INT = 0x1405, KTX_INT = 0x1404, KTX_HALF_FLOAT = 0x140B, KTX_FLOAT = 0x1406,
KTX_UNSIGNED_BYTE_3_3_2 = 0x8032, KTX_UNSIGNED_BYTE_2_3_3_REV = 0x8362, KTX_UNSIGNED_SHORT_5_6_5 = 0x8363,
KTX_UNSIGNED_SHORT_5_6_5_REV = 0x8364, KTX_UNSIGNED_SHORT_4_4_4_4 = 0x8033, KTX_UNSIGNED_SHORT_4_4_4_4_REV = 0x8365,
KTX_UNSIGNED_SHORT_5_5_5_1 = 0x8034, KTX_UNSIGNED_SHORT_1_5_5_5_REV = 0x8366, KTX_UNSIGNED_INT_8_8_8_8 = 0x8035,
KTX_UNSIGNED_INT_8_8_8_8_REV = 0x8367, KTX_UNSIGNED_INT_10_10_10_2 = 0x8036, KTX_UNSIGNED_INT_2_10_10_10_REV = 0x8368,
KTX_UNSIGNED_INT_24_8 = 0x84FA, KTX_UNSIGNED_INT_10F_11F_11F_REV = 0x8C3B, KTX_UNSIGNED_INT_5_9_9_9_REV = 0x8C3E,
KTX_FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8DAD
};
bool is_packed_pixel_ogl_type(uint32 ogl_type);
uint get_ogl_type_size(uint32 ogl_type);
bool get_ogl_fmt_desc(uint32 ogl_fmt, uint32 ogl_type, uint& block_dim, uint& bytes_per_block);
uint get_ogl_type_size(uint32 ogl_type);
uint32 get_ogl_base_internal_fmt(uint32 ogl_fmt);
class ktx_texture
{
public:
ktx_texture()
{
clear();
}
ktx_texture(const ktx_texture& other)
{
*this = other;
}
ktx_texture& operator= (const ktx_texture& rhs)
{
if (this == &rhs)
return *this;
clear();
m_header = rhs.m_header;
m_key_values = rhs.m_key_values;
m_image_data = rhs.m_image_data;
m_block_dim = rhs.m_block_dim;
m_bytes_per_block = rhs.m_bytes_per_block;
m_opposite_endianness = rhs.m_opposite_endianness;
return *this;
}
void clear()
{
m_header.clear();
m_key_values.clear();
m_image_data.clear();
m_block_dim = 0;
m_bytes_per_block = 0;
m_opposite_endianness = false;
}
// High level methods
bool read_from_stream(data_stream_serializer& serializer);
bool write_to_stream(data_stream_serializer& serializer, bool no_keyvalue_data = false);
bool init_2D(uint width, uint height, uint num_mips, uint32 ogl_internal_fmt, uint32 ogl_fmt, uint32 ogl_type);
bool init_2D_array(uint width, uint height, uint num_mips, uint array_size, uint32 ogl_internal_fmt, uint32 ogl_fmt, uint32 ogl_type);
bool init_3D(uint width, uint height, uint depth, uint num_mips, uint32 ogl_internal_fmt, uint32 ogl_fmt, uint32 ogl_type);
bool init_cubemap(uint dim, uint num_mips, uint32 ogl_internal_fmt, uint32 ogl_fmt, uint32 ogl_type);
bool check_header() const;
bool consistency_check() const;
// General info
bool is_valid() const { return (m_header.m_pixelWidth > 0) && (m_image_data.size() > 0); }
uint get_width() const { return m_header.m_pixelWidth; }
uint get_height() const { return CRNLIB_MAX(m_header.m_pixelHeight, 1); }
uint get_depth() const { return CRNLIB_MAX(m_header.m_pixelDepth, 1); }
uint get_num_mips() const { return CRNLIB_MAX(m_header.m_numberOfMipmapLevels, 1); }
uint get_array_size() const { return CRNLIB_MAX(m_header.m_numberOfArrayElements, 1); }
uint get_num_faces() const { return m_header.m_numberOfFaces; }
uint32 get_ogl_type() const { return m_header.m_glType; }
uint32 get_ogl_fmt() const { return m_header.m_glFormat; }
uint32 get_ogl_base_fmt() const { return m_header.m_glBaseInternalFormat; }
uint32 get_ogl_internal_fmt() const { return m_header.m_glInternalFormat; }
uint get_total_images() const { return get_num_mips() * (get_depth() * get_num_faces() * get_array_size()); }
bool is_compressed() const { return m_block_dim > 1; }
bool is_uncompressed() const { return !is_compressed(); }
bool get_opposite_endianness() const { return m_opposite_endianness; }
void set_opposite_endianness(bool flag) { m_opposite_endianness = flag; }
uint32 get_block_dim() const { return m_block_dim; }
uint32 get_bytes_per_block() const { return m_bytes_per_block; }
const ktx_header& get_header() const { return m_header; }
// Key values
const ktx_key_value_vec& get_key_value_vec() const { return m_key_values; }
ktx_key_value_vec& get_key_value_vec() { return m_key_values; }
const uint8_vec* find_key(const char* pKey) const;
bool get_key_value_as_string(const char* pKey, dynamic_string& str) const;
uint add_key_value(const char* pKey, const void* pVal, uint val_size);
uint add_key_value(const char* pKey, const char* pVal) { return add_key_value(pKey, pVal, static_cast<uint>(strlen(pVal)) + 1); }
// Image data
uint get_num_images() const { return m_image_data.size(); }
const uint8_vec& get_image_data(uint image_index) const { return m_image_data[image_index]; }
uint8_vec& get_image_data(uint image_index) { return m_image_data[image_index]; }
const uint8_vec& get_image_data(uint mip_index, uint array_index, uint face_index, uint zslice_index) const { return get_image_data(get_image_index(mip_index, array_index, face_index, zslice_index)); }
uint8_vec& get_image_data(uint mip_index, uint array_index, uint face_index, uint zslice_index) { return get_image_data(get_image_index(mip_index, array_index, face_index, zslice_index)); }
const ktx_image_data_vec& get_image_data_vec() const { return m_image_data; }
ktx_image_data_vec& get_image_data_vec() { return m_image_data; }
void add_image(uint face_index, uint mip_index, const void* pImage, uint image_size)
{
const uint image_index = get_image_index(mip_index, 0, face_index, 0);
if (image_index >= m_image_data.size())
m_image_data.resize(image_index + 1);
if (image_size)
{
uint8_vec& v = m_image_data[image_index];
v.resize(image_size);
memcpy(&v[0], pImage, image_size);
}
}
uint get_image_index(uint mip_index, uint array_index, uint face_index, uint zslice_index) const
{
CRNLIB_ASSERT((mip_index < get_num_mips()) && (array_index < get_array_size()) && (face_index < get_num_faces()) && (zslice_index < get_depth()));
return zslice_index + (face_index * get_depth()) + (array_index * (get_depth() * get_num_faces())) + (mip_index * (get_depth() * get_num_faces() * get_array_size()));
}
void get_mip_dim(uint mip_index, uint& mip_width, uint& mip_height) const
{
CRNLIB_ASSERT(mip_index < get_num_mips());
mip_width = CRNLIB_MAX(get_width() >> mip_index, 1);
mip_height = CRNLIB_MAX(get_height() >> mip_index, 1);
}
void get_mip_dim(uint mip_index, uint& mip_width, uint& mip_height, uint& mip_depth) const
{
CRNLIB_ASSERT(mip_index < get_num_mips());
mip_width = CRNLIB_MAX(get_width() >> mip_index, 1);
mip_height = CRNLIB_MAX(get_height() >> mip_index, 1);
mip_depth = CRNLIB_MAX(get_depth() >> mip_index, 1);
}
private:
ktx_header m_header;
ktx_key_value_vec m_key_values;
ktx_image_data_vec m_image_data;
uint32 m_block_dim;
uint32 m_bytes_per_block;
bool m_opposite_endianness;
bool compute_pixel_info();
};
} // namespace crnlib
#endif // #ifndef _KTX_TEXTURE_H_
+34 -39
View File
@@ -4,18 +4,17 @@
#include "crn_lzma_codec.h"
#include "crn_strutils.h"
#include "crn_checksum.h"
#include "lzma_LzmaLib.h"
#include "crn_threading.h"
#include "lzma_lzmalib.h"
namespace crnlib
{
lzma_codec::lzma_codec() :
lzma_codec::lzma_codec() :
m_pCompress(LzmaCompress),
m_pUncompress(LzmaUncompress)
{
CRNLIB_ASSUME(cLZMAPropsSize == LZMA_PROPS_SIZE);
}
lzma_codec::~lzma_codec()
{
}
@@ -24,29 +23,29 @@ namespace crnlib
{
if (n > 1024U*1024U*1024U)
return false;
uint max_comp_size = n + math::maximum<uint>(128, n >> 8);
buf.resize(sizeof(header) + max_comp_size);
header* pHDR = reinterpret_cast<header*>(&buf[0]);
uint8* pComp_data = &buf[sizeof(header)];
utils::zero_object(*pHDR);
pHDR->m_uncomp_size = n;
pHDR->m_adler32 = adler32(p, n);
if (n)
{
size_t destLen = 0;
size_t outPropsSize = 0;
int status = SZ_ERROR_INPUT_EOF;
for (uint trial = 0; trial < 3; trial++)
{
destLen = max_comp_size;
outPropsSize = cLZMAPropsSize;
status = (*m_pCompress)(pComp_data, &destLen, reinterpret_cast<const unsigned char*>(p), n,
pHDR->m_lzma_props, &outPropsSize,
-1, /* 0 <= level <= 9, default = 5 */
@@ -55,87 +54,83 @@ namespace crnlib
-1, /* 0 <= lp <= 4, default = 0 */
-1, /* 0 <= pb <= 4, default = 2 */
-1, /* 5 <= fb <= 273, default = 32 */
#ifdef WIN32
(g_number_of_processors > 1) ? 2 : 1
#else
1
#endif
);
if (status != SZ_ERROR_OUTPUT_EOF)
break;
max_comp_size += ((n+1)/2);
buf.resize(sizeof(header) + max_comp_size);
pHDR = reinterpret_cast<header*>(&buf[0]);
pComp_data = &buf[sizeof(header)];
}
if (status != SZ_OK)
if (status != SZ_OK)
{
buf.clear();
return false;
}
pHDR->m_comp_size = static_cast<uint>(destLen);
buf.resize(CRNLIB_SIZEOF_U32(header) + static_cast<uint32>(destLen));
}
}
pHDR->m_sig = header::cSig;
pHDR->m_checksum = static_cast<uint8>(adler32((uint8*)pHDR + header::cChecksumSkipBytes, sizeof(header) - header::cChecksumSkipBytes));
return true;
}
bool lzma_codec::unpack(const void* p, uint n, crnlib::vector<uint8>& buf)
{
buf.resize(0);
if (n < sizeof(header))
return false;
const header& hdr = *static_cast<const header*>(p);
if (hdr.m_sig != header::cSig)
return false;
if (static_cast<uint8>(adler32((const uint8*)&hdr + header::cChecksumSkipBytes, sizeof(hdr) - header::cChecksumSkipBytes)) != hdr.m_checksum)
return false;
if (!hdr.m_uncomp_size)
return true;
if (!hdr.m_comp_size)
return false;
if (hdr.m_uncomp_size > 1024U*1024U*1024U)
return false;
if (!buf.try_resize(hdr.m_uncomp_size))
if (!buf.try_resize(hdr.m_uncomp_size))
return false;
const uint8* pComp_data = static_cast<const uint8*>(p) + sizeof(header);
size_t srcLen = n - sizeof(header);
if (srcLen < hdr.m_comp_size)
return false;
size_t destLen = hdr.m_uncomp_size;
int status = (*m_pUncompress)(&buf[0], &destLen, pComp_data, &srcLen,
hdr.m_lzma_props, cLZMAPropsSize);
hdr.m_lzma_props, cLZMAPropsSize);
if ((status != SZ_OK) || (destLen != hdr.m_uncomp_size))
{
buf.clear();
return false;
return false;
}
if (adler32(&buf[0], buf.size()) != hdr.m_adler32)
{
buf.clear();
return false;
}
return true;
}
+3 -3
View File
@@ -12,14 +12,14 @@ namespace crnlib
~lzma_codec();
// Always available, because we're statically linking in lzmalib now vs. dynamically loading the DLL.
bool is_initialized() const { return true; }
const bool is_initialized() const { return true; }
bool pack(const void* p, uint n, crnlib::vector<uint8>& buf);
bool unpack(const void* p, uint n, crnlib::vector<uint8>& buf);
private:
typedef int (CRNLIB_STDCALL *LzmaCompressFuncPtr)(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
typedef int (__stdcall *LzmaCompressFuncPtr)(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */
int level, /* 0 <= level <= 9, default = 5 */
unsigned dictSize, /* default = (1 << 24) */
@@ -30,7 +30,7 @@ namespace crnlib
int numThreads /* 1 or 2, default = 2 */
);
typedef int (CRNLIB_STDCALL *LzmaUncompressFuncPtr)(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen,
typedef int (__stdcall *LzmaUncompressFuncPtr)(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen,
const unsigned char *props, size_t propsSize);
LzmaCompressFuncPtr m_pCompress;
-15
View File
@@ -216,22 +216,7 @@ namespace crnlib
void compute_lower_pow2_dim(int& width, int& height);
void compute_upper_pow2_dim(int& width, int& height);
inline bool equal_tol(float a, float b, float t)
{
return fabs(a - b) < ((maximum(fabs(a), fabs(b)) + 1.0f) * t);
}
inline bool equal_tol(double a, double b, double t)
{
return fabs(a - b) < ((maximum(fabs(a), fabs(b)) + 1.0f) * t);
}
}
} // namespace crnlib
+11 -101
View File
@@ -1,16 +1,15 @@
// File: crn_mem.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_spinlock.h"
#include "crn_console.h"
#include "../inc/crnlib.h"
#include <malloc.h>
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif
#define CRNLIB_MEM_STATS 0
#if !CRNLIB_USE_WIN32_API
#ifndef CRNLIB_USE_WIN32_API
#define _msize malloc_usable_size
#endif
@@ -60,7 +59,7 @@ namespace crnlib
return new_total_allocated;
}
#endif // CRNLIB_MEM_STATS
static void* crnlib_default_realloc(void* p, size_t size, size_t* pActual_size, bool movable, void* pUser_data)
{
pUser_data;
@@ -72,11 +71,6 @@ namespace crnlib
p_new = ::malloc(size);
CRNLIB_ASSERT( (reinterpret_cast<ptr_bits_t>(p_new) & (CRNLIB_MIN_ALLOC_ALIGNMENT - 1)) == 0 );
if (!p_new)
{
printf("WARNING: ::malloc() of size %u failed!\n", (uint)size);
}
if (pActual_size)
*pActual_size = p_new ? ::_msize(p_new) : 0;
}
@@ -94,6 +88,7 @@ namespace crnlib
#ifdef WIN32
p_new = ::_expand(p, size);
#else
p_new = NULL;
#endif
@@ -111,10 +106,6 @@ namespace crnlib
CRNLIB_ASSERT( (reinterpret_cast<ptr_bits_t>(p_new) & (CRNLIB_MIN_ALLOC_ALIGNMENT - 1)) == 0 );
p_final_block = p_new;
}
else
{
printf("WARNING: ::realloc() of size %u failed!\n", (uint)size);
}
}
if (pActual_size)
@@ -130,96 +121,15 @@ namespace crnlib
return p ? _msize(p) : 0;
}
#if 0
static __declspec(thread) void *g_pBuf;
static __declspec(thread) size_t g_buf_size;
static __declspec(thread) size_t g_buf_ofs;
static size_t crnlib_nofree_msize(void* p, void* pUser_data)
{
pUser_data;
return p ? ((const size_t*)p)[-1] : 0;
}
static void* crnlib_nofree_realloc(void* p, size_t size, size_t* pActual_size, bool movable, void* pUser_data)
{
pUser_data;
void* p_new;
if (!p)
{
size = math::align_up_value(size, CRNLIB_MIN_ALLOC_ALIGNMENT);
size_t actual_size = sizeof(size_t)*2 + size;
size_t num_remaining = g_buf_size - g_buf_ofs;
if (num_remaining < actual_size)
{
g_buf_size = CRNLIB_MAX(actual_size, 32*1024*1024);
g_buf_ofs = 0;
g_pBuf = malloc(g_buf_size);
if (!g_pBuf)
return NULL;
}
p_new = (uint8*)g_pBuf + g_buf_ofs;
((size_t*)p_new)[1] = size;
p_new = (size_t*)p_new + 2;
g_buf_ofs += actual_size;
if (pActual_size)
*pActual_size = size;
CRNLIB_ASSERT(crnlib_nofree_msize(p_new, NULL) == size);
}
else if (!size)
{
if (pActual_size)
*pActual_size = 0;
p_new = NULL;
}
else
{
size_t cur_size = crnlib_nofree_msize(p, NULL);
p_new = p;
if (!movable)
return NULL;
if (size > cur_size)
{
p_new = crnlib_nofree_realloc(NULL, size, NULL, true, NULL);
if (!p_new)
return NULL;
memcpy(p_new, p, cur_size);
cur_size = size;
}
if (pActual_size)
*pActual_size = cur_size;
}
return p_new;
}
static crn_realloc_func g_pRealloc = crnlib_nofree_realloc;
static crn_msize_func g_pMSize = crnlib_nofree_msize;
#else
static crn_realloc_func g_pRealloc = crnlib_default_realloc;
static crn_msize_func g_pMSize = crnlib_default_msize;
#endif
static void* g_pUser_data;
void crnlib_mem_error(const char* p_msg)
{
crnlib_assert(p_msg, __FILE__, __LINE__);
}
void* crnlib_malloc(size_t size)
{
return crnlib_malloc(size, NULL);
}
void* crnlib_malloc(size_t size, size_t* pActual_size)
{
size = (size + sizeof(uint32) - 1U) & ~(sizeof(uint32) - 1U);
@@ -272,7 +182,7 @@ namespace crnlib
size_t cur_size = p ? (*g_pMSize)(p, g_pUser_data) : 0;
CRNLIB_ASSERT(!p || (cur_size >= sizeof(uint32)));
#endif
if ((size) && (size < sizeof(uint32)))
if ((size) && (size < sizeof(uint32)))
size = sizeof(uint32);
size_t actual_size = size;
@@ -343,19 +253,19 @@ namespace crnlib
return (*g_pMSize)(p, g_pUser_data);
}
void crnlib_print_mem_stats()
{
#if CRNLIB_MEM_STATS
if (console::is_initialized())
{
console::debug("crnlib_print_mem_stats:");
console::debug("Current blocks: %u, allocated: " CRNLIB_INT64_FORMAT_SPECIFIER ", max ever allocated: " CRNLIB_INT64_FORMAT_SPECIFIER, g_total_blocks, (int64)g_total_allocated, (int64)g_max_allocated);
console::debug(L"crnlib_print_mem_stats:");
console::debug(L"Current blocks: %u, allocated: %I64u, max ever allocated: %I64i", g_total_blocks, (int64)g_total_allocated, (int64)g_max_allocated);
}
else
{
printf("crnlib_print_mem_stats:\n");
printf("Current blocks: %u, allocated: " CRNLIB_INT64_FORMAT_SPECIFIER ", max ever allocated: " CRNLIB_INT64_FORMAT_SPECIFIER "\n", g_total_blocks, (int64)g_total_allocated, (int64)g_max_allocated);
printf("Current blocks: %u, allocated: %I64u, max ever allocated: %I64i\n", g_total_blocks, (int64)g_total_allocated, (int64)g_max_allocated);
}
#endif
}
+1 -25
View File
@@ -14,8 +14,7 @@ namespace crnlib
const uint32 CRNLIB_MAX_POSSIBLE_BLOCK_SIZE = 0x7FFF0000U;
#endif
void* crnlib_malloc(size_t size);
void* crnlib_malloc(size_t size, size_t* pActual_size);
void* crnlib_malloc(size_t size, size_t* pActual_size = NULL);
void* crnlib_realloc(void* p, size_t size, size_t* pActual_size = NULL, bool movable = true);
void* crnlib_calloc(size_t count, size_t size, size_t* pActual_size = NULL);
void crnlib_free(void* p);
@@ -184,26 +183,3 @@ namespace crnlib
}
} // namespace crnlib
#define CRNLIB_DEFINE_NEW_DELETE \
void* operator new (size_t size) \
{ \
void* p = crnlib::crnlib_malloc(size); \
if (!p) \
crnlib_fail("new: Out of memory!", __FILE__, __LINE__); \
return p; \
} \
void* operator new[] (size_t size) \
{ \
void* p = crnlib::crnlib_malloc(size); \
if (!p) \
crnlib_fail("new[]: Out of memory!", __FILE__, __LINE__); \
return p; \
} \
void operator delete (void* p_block) \
{ \
crnlib::crnlib_free(p_block); \
} \
void operator delete[] (void* p_block) \
{ \
crnlib::crnlib_free(p_block); \
}
-3948
View File
File diff suppressed because it is too large Load Diff
-893
View File
@@ -1,893 +0,0 @@
/* miniz.c v1.14 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing
See "unlicense" statement at the end of this file.
Rich Geldreich <richgel99@gmail.com>, last updated May 20, 2012
Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt
Most API's defined in miniz.c are optional. For example, to disable the archive related functions just define
MINIZ_NO_ARCHIVE_APIS, or to get rid of all stdio usage define MINIZ_NO_STDIO (see the list below for more macros).
* Change History
5/20/12 v1.14 - MinGW32/64 GCC 4.6.1 compiler fixes: added MZ_FORCEINLINE, #include <time.h> (thanks fermtect).
5/19/12 v1.13 - From jason@cornsyrup.org and kelwert@mtu.edu - Fix mz_crc32() so it doesn't compute the wrong CRC-32's when mz_ulong is 64-bit.
Temporarily/locally slammed in "typedef unsigned long mz_ulong" and re-ran a randomized regression test on ~500k files.
Eliminated a bunch of warnings when compiling with GCC 32-bit/64.
Ran all examples, miniz.c, and tinfl.c through MSVC 2008's /analyze (static analysis) option and fixed all warnings (except for the silly
"Use of the comma-operator in a tested expression.." analysis warning, which I purposely use to work around a MSVC compiler warning).
Created 32-bit and 64-bit Codeblocks projects/workspace. Built and tested Linux executables. The codeblocks workspace is compatible with Linux+Win32/x64.
Added miniz_tester solution/project, which is a useful little app derived from LZHAM's tester app that I use as part of the regression test.
Ran miniz.c and tinfl.c through another series of regression testing on ~500,000 files and archives.
Modified example5.c so it purposely disables a bunch of high-level functionality (MINIZ_NO_STDIO, etc.). (Thanks to corysama for the MINIZ_NO_STDIO bug report.)
Fix ftell() usage in examples so they exit with an error on files which are too large (a limitation of the examples, not miniz itself).
4/12/12 v1.12 - More comments, added low-level example5.c, fixed a couple minor level_and_flags issues in the archive API's.
level_and_flags can now be set to MZ_DEFAULT_COMPRESSION. Thanks to Bruce Dawson <bruced@valvesoftware.com> for the feedback/bug report.
5/28/11 v1.11 - Added statement from unlicense.org
5/27/11 v1.10 - Substantial compressor optimizations:
Level 1 is now ~4x faster than before. The L1 compressor's throughput now varies between 70-110MB/sec. on a
Core i7 (actual throughput varies depending on the type of data, and x64 vs. x86).
Improved baseline L2-L9 compression perf. Also, greatly improved compression perf. issues on some file types.
Refactored the compression code for better readability and maintainability.
Added level 10 compression level (L10 has slightly better ratio than level 9, but could have a potentially large
drop in throughput on some files).
5/15/11 v1.09 - Initial stable release.
* Low-level Deflate/Inflate implementation notes:
Compression: Use the "tdefl" API's. The compressor supports raw, static, and dynamic blocks, lazy or
greedy parsing, match length filtering, RLE-only, and Huffman-only streams. It performs and compresses
approximately as well as zlib.
Decompression: Use the "tinfl" API's. The entire decompressor is implemented as a single function
coroutine: see tinfl_decompress(). It supports decompression into a 32KB (or larger power of 2) wrapping buffer, or into a memory
block large enough to hold the entire file.
The low-level tdefl/tinfl API's do not make any use of dynamic memory allocation.
* zlib-style API notes:
miniz.c implements a fairly large subset of zlib. There's enough functionality present for it to be a drop-in
zlib replacement in many apps:
The z_stream struct, optional memory allocation callbacks
deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound
inflateInit/inflateInit2/inflate/inflateEnd
compress, compress2, compressBound, uncompress
CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly routines.
Supports raw deflate streams or standard zlib streams with adler-32 checking.
Limitations:
The callback API's are not implemented yet. No support for gzip headers or zlib static dictionaries.
I've tried to closely emulate zlib's various flavors of stream flushing and return status codes, but
there are no guarantees that miniz.c pulls this off perfectly.
* PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, originally written by
Alex Evans. Supports 1-4 bytes/pixel images.
* ZIP archive API notes:
The ZIP archive API's where designed with simplicity and efficiency in mind, with just enough abstraction to
get the job done with minimal fuss. There are simple API's to retrieve file information, read files from
existing archives, create new archives, append new files to existing archives, or clone archive data from
one archive to another. It supports archives located in memory or the heap, on disk (using stdio.h),
or you can specify custom file read/write callbacks.
- Archive reading: Just call this function to read a single file from a disk archive:
void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name,
size_t *pSize, mz_uint zip_flags);
For more complex cases, use the "mz_zip_reader" functions. Upon opening an archive, the entire central
directory is located and read as-is into memory, and subsequent file access only occurs when reading individual files.
- Archives file scanning: The simple way is to use this function to scan a loaded archive for a specific file:
int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
The locate operation can optionally check file comments too, which (as one example) can be used to identify
multiple versions of the same file in an archive. This function uses a simple linear search through the central
directory, so it's not very fast.
Alternately, you can iterate through all the files in an archive (using mz_zip_reader_get_num_files()) and
retrieve detailed info on each file by calling mz_zip_reader_file_stat().
- Archive creation: Use the "mz_zip_writer" functions. The ZIP writer immediately writes compressed file data
to disk and builds an exact image of the central directory in memory. The central directory image is written
all at once at the end of the archive file when the archive is finalized.
The archive writer can optionally align each file's local header and file data to any power of 2 alignment,
which can be useful when the archive will be read from optical media. Also, the writer supports placing
arbitrary data blobs at the very beginning of ZIP archives. Archives written using either feature are still
readable by any ZIP tool.
- Archive appending: The simple way to add a single file to an archive is to call this function:
mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name,
const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
The archive will be created if it doesn't already exist, otherwise it'll be appended to.
Note the appending is done in-place and is not an atomic operation, so if something goes wrong
during the operation it's possible the archive could be left without a central directory (although the local
file headers and file data will be fine, so the archive will be recoverable).
For more complex archive modification scenarios:
1. The safest way is to use a mz_zip_reader to read the existing archive, cloning only those bits you want to
preserve into a new archive using using the mz_zip_writer_add_from_zip_reader() function (which compiles the
compressed file data as-is). When you're done, delete the old archive and rename the newly written archive, and
you're done. This is safe but requires a bunch of temporary disk space or heap memory.
2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using mz_zip_writer_init_from_reader(),
append new files as needed, then finalize the archive which will write an updated central directory to the
original archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place() does.) There's a
possibility that the archive's central directory could be lost with this method if anything goes wrong, though.
- ZIP archive support limitations:
No zip64 or spanning support. Extraction functions can only handle unencrypted, stored or deflated files.
Requires streams capable of seeking.
* This is a header file library, like stb_image.c. To get only a header file, either cut and paste the
below header, or create miniz.h, #define MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it.
* Important: For best perf. be sure to customize the below macros for your target platform:
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
#define MINIZ_LITTLE_ENDIAN 1
#define MINIZ_HAS_64BIT_REGISTERS 1
*/
#pragma once
#ifndef MINIZ_HEADER_INCLUDED
#define MINIZ_HEADER_INCLUDED
#include <stdlib.h>
#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
#include <time.h>
#endif
// Defines to completely disable specific portions of miniz.c:
// If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl.
// Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O.
//#define MINIZ_NO_STDIO
// If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or
// get/set file times.
//#define MINIZ_NO_TIME
// Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's.
//#define MINIZ_NO_ARCHIVE_APIS
// Define MINIZ_NO_ARCHIVE_APIS to disable all writing related ZIP archive API's.
//#define MINIZ_NO_ARCHIVE_WRITING_APIS
// Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's.
//#define MINIZ_NO_ZLIB_APIS
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib.
//#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
// Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
// Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc
// callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user
// functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work.
//#define MINIZ_NO_MALLOC
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
// MINIZ_X86_OR_X64_CPU is only used to help set the below macros.
#define MINIZ_X86_OR_X64_CPU 1
#endif
#if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian.
#define MINIZ_LITTLE_ENDIAN 1
#endif
#if MINIZ_X86_OR_X64_CPU
// Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses.
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
#endif
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
// Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions).
#define MINIZ_HAS_64BIT_REGISTERS 1
#endif
#ifdef __cplusplus
extern "C" {
#endif
// ------------------- zlib-style API Definitions.
// For more compatibility with zlib, miniz.c uses unsigned long for some parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits!
typedef unsigned long mz_ulong;
// mz_free() internally uses the MZ_FREE() macro (which by default calls free() unless you've modified the MZ_MALLOC macro) to release a block allocated from the heap.
void mz_free(void *p);
#define MZ_ADLER32_INIT (1)
// mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL.
mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len);
#define MZ_CRC32_INIT (0)
// mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL.
mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len);
// Compression strategies.
enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 };
// Method
#define MZ_DEFLATED 8
#ifndef MINIZ_NO_ZLIB_APIS
// Heap allocation callbacks.
// Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long.
typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
typedef void (*mz_free_func)(void *opaque, void *address);
typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size);
#define MZ_VERSION "9.1.14"
#define MZ_VERNUM 0x91E0
#define MZ_VER_MAJOR 9
#define MZ_VER_MINOR 1
#define MZ_VER_REVISION 14
#define MZ_VER_SUBREVISION 0
// Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The other values are for advanced use (refer to the zlib docs).
enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 };
// Return status codes. MZ_PARAM_ERROR is non-standard.
enum { MZ_OK = 0, MZ_STREAM_END = 1, MZ_NEED_DICT = 2, MZ_ERRNO = -1, MZ_STREAM_ERROR = -2, MZ_DATA_ERROR = -3, MZ_MEM_ERROR = -4, MZ_BUF_ERROR = -5, MZ_VERSION_ERROR = -6, MZ_PARAM_ERROR = -10000 };
// Compression levels: 0-9 are the standard zlib-style levels, 10 is best possible compression (not zlib compatible, and may be very slow), MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL.
enum { MZ_NO_COMPRESSION = 0, MZ_BEST_SPEED = 1, MZ_BEST_COMPRESSION = 9, MZ_UBER_COMPRESSION = 10, MZ_DEFAULT_LEVEL = 6, MZ_DEFAULT_COMPRESSION = -1 };
// Window bits
#define MZ_DEFAULT_WINDOW_BITS 15
struct mz_internal_state;
// Compression/decompression stream struct.
typedef struct mz_stream_s
{
const unsigned char *next_in; // pointer to next byte to read
unsigned int avail_in; // number of bytes available at next_in
mz_ulong total_in; // total number of bytes consumed so far
unsigned char *next_out; // pointer to next byte to write
unsigned int avail_out; // number of bytes that can be written to next_out
mz_ulong total_out; // total number of bytes produced so far
char *msg; // error msg (unused)
struct mz_internal_state *state; // internal state, allocated by zalloc/zfree
mz_alloc_func zalloc; // optional heap allocation function (defaults to malloc)
mz_free_func zfree; // optional heap free function (defaults to free)
void *opaque; // heap alloc function user pointer
int data_type; // data_type (unused)
mz_ulong adler; // adler32 of the source or uncompressed data
mz_ulong reserved; // not used
} mz_stream;
typedef mz_stream *mz_streamp;
// Returns the version string of miniz.c.
const char *mz_version(void);
// mz_deflateInit() initializes a compressor with default options:
// Parameters:
// pStream must point to an initialized mz_stream struct.
// level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION].
// level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio.
// (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.)
// Return values:
// MZ_OK on success.
// MZ_STREAM_ERROR if the stream is bogus.
// MZ_PARAM_ERROR if the input parameters are bogus.
// MZ_MEM_ERROR on out of memory.
int mz_deflateInit(mz_streamp pStream, int level);
// mz_deflateInit2() is like mz_deflate(), except with more control:
// Additional parameters:
// method must be MZ_DEFLATED
// window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no header or footer)
// mem_level must be between [1, 9] (it's checked but ignored by miniz.c)
int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy);
// Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2().
int mz_deflateReset(mz_streamp pStream);
// mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible.
// Parameters:
// pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members.
// flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH.
// Return values:
// MZ_OK on success (when flushing, or if more input is needed but not available, and/or there's more output to be written but the output buffer is full).
// MZ_STREAM_END if all input has been consumed and all output bytes have been written. Don't call mz_deflate() on the stream anymore.
// MZ_STREAM_ERROR if the stream is bogus.
// MZ_PARAM_ERROR if one of the parameters is invalid.
// MZ_BUF_ERROR if no forward progress is possible because the input and/or output buffers are empty. (Fill up the input buffer or free up some output space and try again.)
int mz_deflate(mz_streamp pStream, int flush);
// mz_deflateEnd() deinitializes a compressor:
// Return values:
// MZ_OK on success.
// MZ_STREAM_ERROR if the stream is bogus.
int mz_deflateEnd(mz_streamp pStream);
// mz_deflateBound() returns a (very) conservative upper bound on the amount of data that could be generated by deflate(), assuming flush is set to only MZ_NO_FLUSH or MZ_FINISH.
mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len);
// Single-call compression functions mz_compress() and mz_compress2():
// Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure.
int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level);
// mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress().
mz_ulong mz_compressBound(mz_ulong source_len);
// Initializes a decompressor.
int mz_inflateInit(mz_streamp pStream);
// mz_inflateInit2() is like mz_inflateInit() with an additional option that controls the window size and whether or not the stream has been wrapped with a zlib header/footer:
// window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate).
int mz_inflateInit2(mz_streamp pStream, int window_bits);
// Decompresses the input stream to the output, consuming only as much of the input as needed, and writing as much to the output as possible.
// Parameters:
// pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members.
// flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH.
// On the first call, if flush is MZ_FINISH it's assumed the input and output buffers are both sized large enough to decompress the entire stream in a single call (this is slightly faster).
// MZ_FINISH implies that there are no more source bytes available beside what's already in the input buffer, and that the output buffer is large enough to hold the rest of the decompressed data.
// Return values:
// MZ_OK on success. Either more input is needed but not available, and/or there's more output to be written but the output buffer is full.
// MZ_STREAM_END if all needed input has been consumed and all output bytes have been written. For zlib streams, the adler-32 of the decompressed data has also been verified.
// MZ_STREAM_ERROR if the stream is bogus.
// MZ_DATA_ERROR if the deflate stream is invalid.
// MZ_PARAM_ERROR if one of the parameters is invalid.
// MZ_BUF_ERROR if no forward progress is possible because the input buffer is empty but the inflater needs more input to continue, or if the output buffer is not large enough. Call mz_inflate() again
// with more input data, or with more room in the output buffer (except when using single call decompression, described above).
int mz_inflate(mz_streamp pStream, int flush);
// Deinitializes a decompressor.
int mz_inflateEnd(mz_streamp pStream);
// Single-call decompression.
// Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure.
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
// Returns a string description of the specified error code, or NULL if the error code is invalid.
const char *mz_error(int err);
// Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used as a drop-in replacement for the subset of zlib that miniz.c supports.
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project.
#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
typedef unsigned char Byte;
typedef unsigned int uInt;
typedef mz_ulong uLong;
typedef Byte Bytef;
typedef uInt uIntf;
typedef char charf;
typedef int intf;
typedef void *voidpf;
typedef uLong uLongf;
typedef void *voidp;
typedef void *const voidpc;
#define Z_NULL 0
#define Z_NO_FLUSH MZ_NO_FLUSH
#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
#define Z_SYNC_FLUSH MZ_SYNC_FLUSH
#define Z_FULL_FLUSH MZ_FULL_FLUSH
#define Z_FINISH MZ_FINISH
#define Z_BLOCK MZ_BLOCK
#define Z_OK MZ_OK
#define Z_STREAM_END MZ_STREAM_END
#define Z_NEED_DICT MZ_NEED_DICT
#define Z_ERRNO MZ_ERRNO
#define Z_STREAM_ERROR MZ_STREAM_ERROR
#define Z_DATA_ERROR MZ_DATA_ERROR
#define Z_MEM_ERROR MZ_MEM_ERROR
#define Z_BUF_ERROR MZ_BUF_ERROR
#define Z_VERSION_ERROR MZ_VERSION_ERROR
#define Z_PARAM_ERROR MZ_PARAM_ERROR
#define Z_NO_COMPRESSION MZ_NO_COMPRESSION
#define Z_BEST_SPEED MZ_BEST_SPEED
#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
#define Z_FILTERED MZ_FILTERED
#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
#define Z_RLE MZ_RLE
#define Z_FIXED MZ_FIXED
#define Z_DEFLATED MZ_DEFLATED
#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
#define alloc_func mz_alloc_func
#define free_func mz_free_func
#define internal_state mz_internal_state
#define z_stream mz_stream
#define deflateInit mz_deflateInit
#define deflateInit2 mz_deflateInit2
#define deflateReset mz_deflateReset
#define deflate mz_deflate
#define deflateEnd mz_deflateEnd
#define deflateBound mz_deflateBound
#define compress mz_compress
#define compress2 mz_compress2
#define compressBound mz_compressBound
#define inflateInit mz_inflateInit
#define inflateInit2 mz_inflateInit2
#define inflate mz_inflate
#define inflateEnd mz_inflateEnd
#define uncompress mz_uncompress
#define crc32 mz_crc32
#define adler32 mz_adler32
#define MAX_WBITS 15
#define MAX_MEM_LEVEL 9
#define zError mz_error
#define ZLIB_VERSION MZ_VERSION
#define ZLIB_VERNUM MZ_VERNUM
#define ZLIB_VER_MAJOR MZ_VER_MAJOR
#define ZLIB_VER_MINOR MZ_VER_MINOR
#define ZLIB_VER_REVISION MZ_VER_REVISION
#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
#define zlibVersion mz_version
#define zlib_version mz_version()
#endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
#endif // MINIZ_NO_ZLIB_APIS
// ------------------- Types and macros
typedef unsigned char mz_uint8;
typedef signed short mz_int16;
typedef unsigned short mz_uint16;
typedef unsigned int mz_uint32;
typedef unsigned int mz_uint;
typedef long long mz_int64;
typedef unsigned long long mz_uint64;
typedef int mz_bool;
#define MZ_FALSE (0)
#define MZ_TRUE (1)
// Works around MSVC's spammy "warning C4127: conditional expression is constant" message.
#ifdef _MSC_VER
#define MZ_MACRO_END while (0, 0)
#else
#define MZ_MACRO_END while (0)
#endif
// ------------------- ZIP archive reading/writing
#ifndef MINIZ_NO_ARCHIVE_APIS
enum
{
MZ_ZIP_MAX_IO_BUF_SIZE = 64*1024,
MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260,
MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256
};
typedef struct
{
mz_uint32 m_file_index;
mz_uint32 m_central_dir_ofs;
mz_uint16 m_version_made_by;
mz_uint16 m_version_needed;
mz_uint16 m_bit_flag;
mz_uint16 m_method;
#ifndef MINIZ_NO_TIME
time_t m_time;
#endif
mz_uint32 m_crc32;
mz_uint64 m_comp_size;
mz_uint64 m_uncomp_size;
mz_uint16 m_internal_attr;
mz_uint32 m_external_attr;
mz_uint64 m_local_header_ofs;
mz_uint32 m_comment_size;
char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
} mz_zip_archive_file_stat;
typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n);
typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n);
struct mz_zip_internal_state_tag;
typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
typedef enum
{
MZ_ZIP_MODE_INVALID = 0,
MZ_ZIP_MODE_READING = 1,
MZ_ZIP_MODE_WRITING = 2,
MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
} mz_zip_mode;
typedef struct
{
mz_uint64 m_archive_size;
mz_uint64 m_central_directory_file_ofs;
mz_uint m_total_files;
mz_zip_mode m_zip_mode;
mz_uint m_file_offset_alignment;
mz_alloc_func m_pAlloc;
mz_free_func m_pFree;
mz_realloc_func m_pRealloc;
void *m_pAlloc_opaque;
mz_file_read_func m_pRead;
mz_file_write_func m_pWrite;
void *m_pIO_opaque;
mz_zip_internal_state *m_pState;
} mz_zip_archive;
typedef enum
{
MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800
} mz_zip_flags;
// ZIP archive reading
// Inits a ZIP archive reader.
// These functions read and validate the archive's central directory.
mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags);
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint32 flags);
#ifndef MINIZ_NO_STDIO
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags);
#endif
// Returns the total number of files in the archive.
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip);
// Returns detailed information about an archive file entry.
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat);
// Determines if an archive file entry is a directory entry.
mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index);
mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index);
// Retrieves the filename of an archive file entry.
// Returns the number of bytes written to pFilename, or if filename_buf_size is 0 this function returns the number of bytes needed to fully store the filename.
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size);
// Attempts to locates a file in the archive's central directory.
// Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH
// Returns -1 if the file cannot be found.
int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
// Extracts a archive file to a memory buffer using no memory allocation.
mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size);
mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size);
// Extracts a archive file to a memory buffer.
mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags);
mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags);
// Extracts a archive file to a dynamically allocated heap buffer.
void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags);
void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags);
// Extracts a archive file using a callback function to output the file's data.
mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags);
mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags);
#ifndef MINIZ_NO_STDIO
// Extracts a archive file to a disk file and sets its last accessed and modified times.
// This function only extracts files, not archive directory records.
mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags);
mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags);
#endif
// Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used.
mz_bool mz_zip_reader_end(mz_zip_archive *pZip);
// ZIP archive writing
#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
// Inits a ZIP archive writer.
mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size);
mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size);
#ifndef MINIZ_NO_STDIO
mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning);
#endif
// Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive.
// For archives opened using mz_zip_reader_init_file, pFilename must be the archive's filename so it can be reopened for writing. If the file can't be reopened, mz_zip_reader_end() will be called.
// For archives opened using mz_zip_reader_init_mem, the memory block must be growable using the realloc callback (which defaults to realloc unless you've overridden it).
// Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL.
// Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before
// the archive is finalized the file's central directory will be hosed.
mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename);
// Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive.
// To add a directory entry, call this method with an archive name ending in a forwardslash with empty buffer.
// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION.
mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags);
mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32);
#ifndef MINIZ_NO_STDIO
// Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive.
// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION.
mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
#endif
// Adds a file to an archive by fully cloning the data from another archive.
// This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data, and comment fields.
mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint file_index);
// Finalizes the archive by writing the central directory records followed by the end of central directory record.
// After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end().
// An archive must be manually finalized by calling this function for it to be valid.
mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip);
mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, size_t *pSize);
// Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used.
// Note for the archive to be valid, it must have been finalized before ending.
mz_bool mz_zip_writer_end(mz_zip_archive *pZip);
// Misc. high-level helper functions:
// mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive.
// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION.
mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
// Reads a single file from an archive into a heap block.
// If pComment is not NULL, only the file with the specified comment will be extracted.
// Returns NULL on failure.
void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, const char *pComment, size_t *pSize, mz_uint flags);
#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
#endif // #ifndef MINIZ_NO_ARCHIVE_APIS
// ------------------- Low-level Decompression API Definitions
// Decompression flags used by tinfl_decompress().
// TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the input is a raw deflate stream.
// TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input.
// TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB).
// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes.
enum
{
TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
TINFL_FLAG_HAS_MORE_INPUT = 2,
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
TINFL_FLAG_COMPUTE_ADLER32 = 8
};
// High level decompression functions:
// tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc().
// On entry:
// pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress.
// On return:
// Function returns a pointer to the decompressed data, or NULL on failure.
// *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data.
// The caller must call mz_free() on the returned block when it's no longer needed.
void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory.
// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success.
#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags);
// tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided callback function will be called to flush the buffer.
// Returns 1 on success or 0 on failure.
typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor;
// Max size of LZ dictionary.
#define TINFL_LZ_DICT_SIZE 32768
// Return status.
typedef enum
{
TINFL_STATUS_BAD_PARAM = -3,
TINFL_STATUS_ADLER32_MISMATCH = -2,
TINFL_STATUS_FAILED = -1,
TINFL_STATUS_DONE = 0,
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
TINFL_STATUS_HAS_MORE_OUTPUT = 2
} tinfl_status;
// Initializes the decompressor to its initial state.
#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
#define tinfl_get_adler32(r) (r)->m_check_adler32
// Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability.
// This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output.
tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags);
// Internal/private bits follow.
enum
{
TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
};
typedef struct
{
mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
} tinfl_huff_table;
#if MINIZ_HAS_64BIT_REGISTERS
#define TINFL_USE_64BIT_BITBUF 1
#endif
#if TINFL_USE_64BIT_BITBUF
typedef mz_uint64 tinfl_bit_buf_t;
#define TINFL_BITBUF_SIZE (64)
#else
typedef mz_uint32 tinfl_bit_buf_t;
#define TINFL_BITBUF_SIZE (32)
#endif
struct tinfl_decompressor_tag
{
mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES];
tinfl_bit_buf_t m_bit_buf;
size_t m_dist_from_out_buf_start;
tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
};
// ------------------- Low-level Compression API Definitions
// Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently).
#define TDEFL_LESS_MEMORY 0
// tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search):
// TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap compression), 4095=Huffman+LZ (slowest/best compression).
enum
{
TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF
};
// TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before the deflate data, and the Adler-32 of the source data at the end. Otherwise, you'll get raw deflate data.
// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers).
// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing.
// TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's initialization time to the minimum, but the output may vary from run to run given the same input (depending on the contents of memory).
// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled.
// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables.
// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks.
enum
{
TDEFL_WRITE_ZLIB_HEADER = 0x01000,
TDEFL_COMPUTE_ADLER32 = 0x02000,
TDEFL_GREEDY_PARSING_FLAG = 0x04000,
TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
TDEFL_RLE_MATCHES = 0x10000,
TDEFL_FILTER_MATCHES = 0x20000,
TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
};
// High level compression functions:
// tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc().
// On entry:
// pSrc_buf, src_buf_len: Pointer and size of source block to compress.
// flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression.
// On return:
// Function returns a pointer to the compressed data, or NULL on failure.
// *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data.
// The caller must free() the returned block when it's no longer needed.
void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
// tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory.
// Returns 0 on failure.
size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags);
// Compresses an image to a compressed PNG file in memory.
// On entry:
// pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4.
// The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory.
// On return:
// Function returns a pointer to the compressed data, or NULL on failure.
// *pLen_out will be set to the size of the PNG image file.
// The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed.
void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out);
// Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time.
typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
// tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally.
mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
enum { TDEFL_MAX_HUFF_TABLES = 3, TDEFL_MAX_HUFF_SYMBOLS_0 = 288, TDEFL_MAX_HUFF_SYMBOLS_1 = 32, TDEFL_MAX_HUFF_SYMBOLS_2 = 19, TDEFL_LZ_DICT_SIZE = 32768, TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, TDEFL_MIN_MATCH_LEN = 3, TDEFL_MAX_MATCH_LEN = 258 };
// TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes).
#if TDEFL_LESS_MEMORY
enum { TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13 ) / 10, TDEFL_MAX_HUFF_SYMBOLS = 288, TDEFL_LZ_HASH_BITS = 12, TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS };
#else
enum { TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13 ) / 10, TDEFL_MAX_HUFF_SYMBOLS = 288, TDEFL_LZ_HASH_BITS = 15, TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS };
#endif
// The low-level tdefl functions below may be used directly if the above helper functions aren't flexible enough. The low-level functions don't make any heap allocations, unlike the above helper functions.
typedef enum
{
TDEFL_STATUS_BAD_PARAM = -2,
TDEFL_STATUS_PUT_BUF_FAILED = -1,
TDEFL_STATUS_OKAY = 0,
TDEFL_STATUS_DONE = 1,
} tdefl_status;
// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums
typedef enum
{
TDEFL_NO_FLUSH = 0,
TDEFL_SYNC_FLUSH = 2,
TDEFL_FULL_FLUSH = 3,
TDEFL_FINISH = 4
} tdefl_flush;
// tdefl's compression state structure.
typedef struct
{
tdefl_put_buf_func_ptr m_pPut_buf_func;
void *m_pPut_buf_user;
mz_uint m_flags, m_max_probes[2];
int m_greedy_parsing;
mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index, m_wants_to_finish;
tdefl_status m_prev_return_status;
const void *m_pIn_buf;
void *m_pOut_buf;
size_t *m_pIn_buf_size, *m_pOut_buf_size;
tdefl_flush m_flush;
const mz_uint8 *m_pSrc;
size_t m_src_buf_left, m_out_buf_ofs;
mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
} tdefl_compressor;
// Initializes the compressor.
// There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory.
// pBut_buf_func: If NULL, output data will be supplied to the specified callback. In this case, the user should call the tdefl_compress_buffer() API for compression.
// If pBut_buf_func is NULL the user should always call the tdefl_compress() API.
// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.)
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
// Compresses a block of data, consuming as much of the specified input buffer as possible, and writing as much compressed data to the specified output buffer as possible.
tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush);
// tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr.
// tdefl_compress_buffer() always consumes the entire input buffer.
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush);
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
// Can't use tdefl_create_comp_flags_from_zip_params if MINIZ_NO_ZLIB_APIS isn't defined, because it uses some of its macros.
#ifndef MINIZ_NO_ZLIB_APIS
// Create tdefl_compress() flags given zlib-style compression parameters.
// level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files)
// window_bits may be -15 (raw deflate) or 15 (zlib)
// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
#endif // #ifndef MINIZ_NO_ZLIB_APIS
#ifdef __cplusplus
}
#endif
#endif // MINIZ_HEADER_INCLUDED
+40
View File
@@ -0,0 +1,40 @@
// File: crn_mutex.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
namespace crnlib
{
class mutex
{
mutex(const mutex&);
mutex& operator= (const mutex&);
public:
mutex(unsigned int spin_count = 0);
~mutex();
void lock();
void unlock();
void set_spin_count(unsigned int count);
private:
int m_buf[12];
#ifdef CRNLIB_BUILD_DEBUG
unsigned int m_lock_count;
#endif
};
class scoped_mutex
{
scoped_mutex(const scoped_mutex&);
scoped_mutex& operator= (const scoped_mutex&);
public:
inline scoped_mutex(mutex& m) : m_mutex(m) { m_mutex.lock(); }
inline ~scoped_mutex() { m_mutex.unlock(); }
private:
mutex& m_mutex;
};
} // namespace crnlib
+52 -15
View File
@@ -23,7 +23,6 @@ namespace crnlib
PIXEL_FMT_DXT5_xGBR,
PIXEL_FMT_DXT5_AGBR,
PIXEL_FMT_DXT1A,
PIXEL_FMT_ETC1,
PIXEL_FMT_R8G8B8,
PIXEL_FMT_L8,
PIXEL_FMT_A8,
@@ -42,7 +41,36 @@ namespace crnlib
return g_all_pixel_formats[index];
}
const char* get_pixel_format_string(pixel_format fmt)
const wchar_t* get_pixel_format_string(pixel_format fmt)
{
switch (fmt)
{
case PIXEL_FMT_INVALID: return L"INVALID";
case PIXEL_FMT_DXT1: return L"DXT1";
case PIXEL_FMT_DXT1A: return L"DXT1A";
case PIXEL_FMT_DXT2: return L"DXT2";
case PIXEL_FMT_DXT3: return L"DXT3";
case PIXEL_FMT_DXT4: return L"DXT4";
case PIXEL_FMT_DXT5: return L"DXT5";
case PIXEL_FMT_3DC: return L"3DC";
case PIXEL_FMT_DXN: return L"DXN";
case PIXEL_FMT_DXT5A: return L"DXT5A";
case PIXEL_FMT_DXT5_CCxY: return L"DXT5_CCxY";
case PIXEL_FMT_DXT5_xGxR: return L"DXT5_xGxR";
case PIXEL_FMT_DXT5_xGBR: return L"DXT5_xGBR";
case PIXEL_FMT_DXT5_AGBR: return L"DXT5_AGBR";
case PIXEL_FMT_R8G8B8: return L"R8G8B8";
case PIXEL_FMT_A8R8G8B8: return L"A8R8G8B8";
case PIXEL_FMT_A8: return L"A8";
case PIXEL_FMT_L8: return L"L8";
case PIXEL_FMT_A8L8: return L"A8L8";
default: break;
}
CRNLIB_ASSERT(false);
return L"?";
}
const char* get_pixel_format_stringa(pixel_format fmt)
{
switch (fmt)
{
@@ -60,7 +88,6 @@ namespace crnlib
case PIXEL_FMT_DXT5_xGxR: return "DXT5_xGxR";
case PIXEL_FMT_DXT5_xGBR: return "DXT5_xGBR";
case PIXEL_FMT_DXT5_AGBR: return "DXT5_AGBR";
case PIXEL_FMT_ETC1: return "ETC1";
case PIXEL_FMT_R8G8B8: return "R8G8B8";
case PIXEL_FMT_A8R8G8B8: return "A8R8G8B8";
case PIXEL_FMT_A8: return "A8";
@@ -72,7 +99,27 @@ namespace crnlib
return "?";
}
const char* get_crn_format_string(crn_format fmt)
const wchar_t* get_crn_format_string(crn_format fmt)
{
switch (fmt)
{
case cCRNFmtDXT1: return L"DXT1";
case cCRNFmtDXT3: return L"DXT3";
case cCRNFmtDXT5: return L"DXT5";
case cCRNFmtDXT5_CCxY: return L"DXT5_CCxY";
case cCRNFmtDXT5_xGBR: return L"DXT5_xGBR";
case cCRNFmtDXT5_AGBR: return L"DXT5_AGBR";
case cCRNFmtDXT5_xGxR: return L"DXT5_xGxR";
case cCRNFmtDXN_XY: return L"DXN_XY";
case cCRNFmtDXN_YX: return L"DXN_YX";
case cCRNFmtDXT5A: return L"DXT5A";
default: break;
}
CRNLIB_ASSERT(false);
return L"?";
}
const char* get_crn_format_stringa(crn_format fmt)
{
switch (fmt)
{
@@ -86,7 +133,6 @@ namespace crnlib
case cCRNFmtDXN_XY: return "DXN_XY";
case cCRNFmtDXN_YX: return "DXN_YX";
case cCRNFmtDXT5A: return "DXT5A";
case cCRNFmtETC1: return "ETC1";
default: break;
}
CRNLIB_ASSERT(false);
@@ -101,7 +147,6 @@ namespace crnlib
switch (fmt)
{
case PIXEL_FMT_DXT1:
case PIXEL_FMT_ETC1:
{
flags = cCompFlagRValid | cCompFlagGValid | cCompFlagBValid;
break;
@@ -237,9 +282,6 @@ namespace crnlib
case PIXEL_FMT_DXT5_xGxR:
fmt = cCRNFmtDXT5_xGxR;
break;
case PIXEL_FMT_ETC1:
fmt = cCRNFmtETC1;
break;
default:
{
CRNLIB_ASSERT(false);
@@ -263,12 +305,7 @@ namespace crnlib
case cCRNFmtDXN_XY: return PIXEL_FMT_DXN;
case cCRNFmtDXN_YX: return PIXEL_FMT_3DC;
case cCRNFmtDXT5A: return PIXEL_FMT_DXT5A;
case cCRNFmtETC1: return PIXEL_FMT_ETC1;
default:
{
CRNLIB_ASSERT(false);
break;
}
default: break;
}
return PIXEL_FMT_INVALID;
+5 -11
View File
@@ -12,9 +12,11 @@ namespace crnlib
uint get_num_formats();
pixel_format get_pixel_format_by_index(uint index);
const char* get_pixel_format_string(pixel_format fmt);
const wchar_t* get_pixel_format_string(pixel_format fmt);
const char* get_pixel_format_stringa(pixel_format fmt);
const char* get_crn_format_string(crn_format fmt);
const wchar_t* get_crn_format_string(crn_format fmt);
const char* get_crn_format_stringa(crn_format fmt);
inline bool is_grayscale(pixel_format fmt)
{
@@ -33,8 +35,6 @@ namespace crnlib
return (fmt == PIXEL_FMT_DXT1) || (fmt == PIXEL_FMT_DXT1A);
}
// has_alpha() should probably be called "has_opacity()" - it indicates if the format encodes opacity
// because some swizzled DXT5 formats do not encode opacity.
inline bool has_alpha(pixel_format fmt)
{
switch (fmt)
@@ -48,7 +48,6 @@ namespace crnlib
case PIXEL_FMT_A8R8G8B8:
case PIXEL_FMT_A8:
case PIXEL_FMT_A8L8:
case PIXEL_FMT_DXT5_AGBR:
return true;
default: break;
}
@@ -99,7 +98,6 @@ namespace crnlib
case PIXEL_FMT_DXT5_xGxR:
case PIXEL_FMT_DXT5_xGBR:
case PIXEL_FMT_DXT5_AGBR:
case PIXEL_FMT_ETC1:
return true;
default: break;
}
@@ -139,7 +137,6 @@ namespace crnlib
case PIXEL_FMT_DXT5_xGxR: return cDXT5;
case PIXEL_FMT_DXT5_xGBR: return cDXT5;
case PIXEL_FMT_DXT5_AGBR: return cDXT5;
case PIXEL_FMT_ETC1: return cETC1;
default: break;
}
return cDXTInvalid;
@@ -163,8 +160,6 @@ namespace crnlib
return PIXEL_FMT_3DC;
case cDXT5A:
return PIXEL_FMT_DXT5A;
case cETC1:
return PIXEL_FMT_ETC1;
default: break;
}
CRNLIB_ASSERT(false);
@@ -211,7 +206,6 @@ namespace crnlib
{
case PIXEL_FMT_DXT1: return 4;
case PIXEL_FMT_DXT1A: return 4;
case PIXEL_FMT_ETC1: return 4;
case PIXEL_FMT_DXT2: return 8;
case PIXEL_FMT_DXT3: return 8;
case PIXEL_FMT_DXT4: return 8;
@@ -241,7 +235,7 @@ namespace crnlib
case PIXEL_FMT_DXT1: return 8;
case PIXEL_FMT_DXT1A: return 8;
case PIXEL_FMT_DXT5A: return 8;
case PIXEL_FMT_ETC1: return 8;
case PIXEL_FMT_DXT2: return 16;
case PIXEL_FMT_DXT3: return 16;
case PIXEL_FMT_DXT4: return 16;
+3 -76
View File
@@ -1,73 +1,6 @@
// File: crn_platform.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif
#ifndef _MSC_VER
int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...)
{
if (!sizeOfBuffer)
return 0;
va_list args;
va_start(args, format);
int c = vsnprintf(buffer, sizeOfBuffer, format, args);
va_end(args);
buffer[sizeOfBuffer - 1] = '\0';
if (c < 0)
return sizeOfBuffer - 1;
return CRNLIB_MIN(c, (int)sizeOfBuffer - 1);
}
int vsprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, va_list args)
{
if (!sizeOfBuffer)
return 0;
int c = vsnprintf(buffer, sizeOfBuffer, format, args);
buffer[sizeOfBuffer - 1] = '\0';
if (c < 0)
return sizeOfBuffer - 1;
return CRNLIB_MIN(c, (int)sizeOfBuffer - 1);
}
char* strlwr(char* p)
{
char *q = p;
while (*q)
{
char c = *q;
*q++ = tolower(c);
}
return p;
}
char* strupr(char *p)
{
char *q = p;
while (*q)
{
char c = *q;
*q++ = toupper(c);
}
return p;
}
#endif // __GNUC__
void crnlib_debug_break(void)
{
CRNLIB_BREAKPOINT
}
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
bool crnlib_is_debugger_present(void)
@@ -75,18 +8,12 @@ bool crnlib_is_debugger_present(void)
return IsDebuggerPresent() != 0;
}
void crnlib_output_debug_string(const char* p)
void crnlib_debug_break(void)
{
OutputDebugStringA(p);
}
#else
bool crnlib_is_debugger_present(void)
{
return false;
DebugBreak();
}
void crnlib_output_debug_string(const char* p)
{
puts(p);
OutputDebugStringA(p);
}
#endif // CRNLIB_USE_WIN32_API
+13 -60
View File
@@ -2,73 +2,18 @@
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
bool crnlib_is_debugger_present(void);
void crnlib_debug_break(void);
void crnlib_output_debug_string(const char* p);
// actually in crnlib_assert.cpp
void crnlib_assert(const char* pExp, const char* pFile, unsigned line);
void crnlib_fail(const char* pExp, const char* pFile, unsigned line);
#if CRNLIB_LITTLE_ENDIAN_CPU
#ifdef CRNLIB_PLATFORM_PC
const bool c_crnlib_little_endian_platform = true;
#else
const bool c_crnlib_little_endian_platform = false;
#endif
#endif
const bool c_crnlib_big_endian_platform = !c_crnlib_little_endian_platform;
#ifdef __GNUC__
#define crn_fopen(pDstFile, f, m) *(pDstFile) = fopen64(f, m)
#define crn_fseek fseeko64
#define crn_ftell ftello64
#elif defined( _MSC_VER )
#define crn_fopen(pDstFile, f, m) fopen_s(pDstFile, f, m)
#define crn_fseek _fseeki64
#define crn_ftell _ftelli64
#else
#define crn_fopen(pDstFile, f, m) *(pDstFile) = fopen(f, m)
#define crn_fseek(s, o, w) fseek(s, static_cast<long>(o), w)
#define crn_ftell ftell
#endif
#if CRNLIB_USE_WIN32_API
#define CRNLIB_BREAKPOINT DebugBreak();
#define CRNLIB_BUILTIN_EXPECT(c, v) c
#elif defined(__GNUC__)
#define CRNLIB_BREAKPOINT asm("int $3");
#define CRNLIB_BUILTIN_EXPECT(c, v) __builtin_expect(c, v)
#else
#define CRNLIB_BREAKPOINT
#define CRNLIB_BUILTIN_EXPECT(c, v) c
#endif
#if defined(__GNUC__)
#define CRNLIB_ALIGNED(x) __attribute__((aligned(x)))
#define CRNLIB_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define CRNLIB_ALIGNED(x) __declspec(align(x))
#define CRNLIB_NOINLINE __declspec(noinline)
#else
#define CRNLIB_ALIGNED(x)
#define CRNLIB_NOINLINE
#endif
#define CRNLIB_GET_ALIGNMENT(v) ((!sizeof(v)) ? 1 : (__alignof(v) ? __alignof(v) : sizeof(uint32)))
#ifndef _MSC_VER
int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
int vsprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, va_list args);
char* strlwr(char* p);
char* strupr(char *p);
#define _stricmp strcasecmp
#define _strnicmp strncasecmp
#endif
inline bool crnlib_is_little_endian() { return c_crnlib_little_endian_platform; }
inline bool crnlib_is_big_endian() { return c_crnlib_big_endian_platform; }
inline bool crnlib_is_pc()
inline bool crnlib_is_pc()
{
#ifdef CRNLIB_PLATFORM_PC
return true;
@@ -77,7 +22,7 @@ inline bool crnlib_is_pc()
#endif
}
inline bool crnlib_is_x86()
inline bool crnlib_is_x86()
{
#ifdef CRNLIB_PLATFORM_PC_X86
return true;
@@ -86,7 +31,7 @@ inline bool crnlib_is_x86()
#endif
}
inline bool crnlib_is_x64()
inline bool crnlib_is_x64()
{
#ifdef CRNLIB_PLATFORM_PC_X64
return true;
@@ -94,3 +39,11 @@ inline bool crnlib_is_x64()
return false;
#endif
}
bool crnlib_is_debugger_present(void);
void crnlib_debug_break(void);
void crnlib_output_debug_string(const char* p);
// actually in crnlib_assert.cpp
void crnlib_assert(const char* pExp, const char* pFile, unsigned line);
void crnlib_fail(const char* pExp, const char* pFile, unsigned line);
+2 -2
View File
@@ -156,7 +156,7 @@ namespace crnlib
uint c = pCodesizes[i];
if (c)
{
CRNLIB_ASSERT(next_code[c] <= cUINT16_MAX);
CRNLIB_ASSERT(next_code[c] <= UINT16_MAX);
pCodes[i] = static_cast<uint16>(next_code[c]++);
CRNLIB_ASSERT(math::total_bits(pCodes[i]) <= pCodesizes[i]);
@@ -300,7 +300,7 @@ namespace crnlib
CRNLIB_ASSERT(t < (1U << table_bits));
CRNLIB_ASSERT(pTables->m_lookup[t] == cUINT32_MAX);
CRNLIB_ASSERT(pTables->m_lookup[t] == UINT32_MAX);
pTables->m_lookup[t] = sym_index | (codesize << 16U);
}
+13 -14
View File
@@ -61,7 +61,7 @@ namespace crnlib
CRNLIB_ASSERT(n && pBlocks);
m_main_thread_id = crn_get_current_thread_id();
m_main_thread_id = get_current_thread_id();
m_num_blocks = n;
m_pBlocks = pBlocks;
@@ -99,11 +99,6 @@ namespace crnlib
if (debugging)
debug_img.resize(num_chunks_x * cChunkPixelWidth, num_chunks_y * cChunkPixelHeight);
float adaptive_tile_color_psnr_derating = 1.5f; // was 2.4f
if ((level) && (adaptive_tile_color_psnr_derating > .25f))
{
adaptive_tile_color_psnr_derating = math::maximum(.25f, adaptive_tile_color_psnr_derating / powf(3.1f, static_cast<float>(level))); // was 3.0f
}
for (uint chunk_y = 0; chunk_y < num_chunks_y; chunk_y++)
{
for (uint chunk_x = 0; chunk_x < num_chunks_x; chunk_x++)
@@ -202,8 +197,13 @@ namespace crnlib
if (mean_squared)
peak_snr = math::clamp<double>(log10(255.0f / root_mean_squared) * 20.0f, 0.0f, 500.0f);
float adaptive_tile_color_psnr_derating = 2.4f;
//if (level)
// adaptive_tile_color_psnr_derating = math::lerp(adaptive_tile_color_psnr_derating * .5f, .3f, math::maximum((level - 1) / float(m_params.m_num_mips - 2), 1.0f));
if ((level) && (adaptive_tile_color_psnr_derating > .25f))
{
adaptive_tile_color_psnr_derating = math::maximum(.25f, adaptive_tile_color_psnr_derating / powf(3.0f, static_cast<float>(level)));
}
float color_derating = math::lerp( 0.0f, adaptive_tile_color_psnr_derating, (g_chunk_encodings[e].m_num_tiles - 1) / 3.0f );
peak_snr = peak_snr - color_derating;
@@ -306,7 +306,7 @@ namespace crnlib
#if GENERATE_DEBUG_IMAGES
if (debugging)
image_utils::write_to_file(dynamic_string(cVarArg, "debug_%u.tga", level).get_ptr(), debug_img, image_utils::cWriteFlagIgnoreAlpha);
image_utils::save_to_file_stb(dynamic_wstring(cVarArg, L"debug_%u.tga", level).get_ptr(), debug_img, image_utils::cSaveIgnoreAlpha);
#endif
} // level
@@ -440,7 +440,7 @@ namespace crnlib
if ((cluster_index & cluster_index_progress_mask) == 0)
{
if (crn_get_current_thread_id() == m_main_thread_id)
if (get_current_thread_id() == m_main_thread_id)
{
if (!update_progress(cluster_index, m_endpoint_cluster_indices.size() - 1))
return;
@@ -547,8 +547,7 @@ namespace crnlib
{
const uint block_index = indices[block_iter];
//const color_quad_u8* pSrc_pixels = &m_pBlocks[block_index].m_pixels[0][0];
const color_quad_u8* pSrc_pixels = (const color_quad_u8*)m_pBlocks[block_index].m_pixels;
const color_quad_u8* pSrc_pixels = &m_pBlocks[block_index].m_pixels[0][0];
for (uint i = 0; i < cDXTBlockSize * cDXTBlockSize; i++)
{
@@ -647,7 +646,7 @@ namespace crnlib
if ((cluster_index & 255) == 0)
{
if (crn_get_current_thread_id() == m_main_thread_id)
if (get_current_thread_id() == m_main_thread_id)
{
if (!update_progress(cluster_index, task_params.m_selector_cluster_indices.size() - 1))
return;
@@ -682,7 +681,7 @@ namespace crnlib
if (m_params.m_dxt1a_alpha_threshold > 0)
{
const color_quad_u8* pSrc_pixels = (const color_quad_u8*)m_pBlocks[block_index].m_pixels;
const color_quad_u8* pSrc_pixels = &m_pBlocks[block_index].m_pixels[0][0];
for (uint i = 0; i < cDXTBlockSize * cDXTBlockSize; i++)
{
@@ -810,7 +809,7 @@ namespace crnlib
{
CRNLIB_ASSERT(m_num_blocks);
m_main_thread_id = crn_get_current_thread_id();
m_main_thread_id = get_current_thread_id();
m_canceled = false;
m_pDst_elements = pDst_elements;
@@ -825,7 +824,7 @@ namespace crnlib
const float quality = m_params.m_quality_level / (float)qdxt1_params::cMaxQuality;
const float endpoint_quality = powf(quality, 1.8f * quality_power_mul);
const float selector_quality = powf(quality, 1.65f * quality_power_mul);
//const uint max_endpoint_clusters = math::clamp<uint>(static_cast<uint>(m_endpoint_clusterizer.get_codebook_size() * endpoint_quality), 128U, m_endpoint_clusterizer.get_codebook_size());
//const uint max_selector_clusters = math::clamp<uint>(static_cast<uint>(m_max_selector_clusters * selector_quality), 150U, m_max_selector_clusters);
const uint max_endpoint_clusters = math::clamp<uint>(static_cast<uint>(m_endpoint_clusterizer.get_codebook_size() * endpoint_quality), 96U, m_endpoint_clusterizer.get_codebook_size());
+42 -40
View File
@@ -2,6 +2,8 @@
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#include "crn_dxt.h"
#include "crn_task_pool.h"
#include "crn_spinlock.h"
#include "crn_hash_map.h"
#include "crn_clusterizer.h"
#include "crn_hash.h"
@@ -15,8 +17,8 @@ namespace crnlib
qdxt1_params()
{
clear();
}
}
void clear()
{
m_quality_level = cMaxQuality;
@@ -42,27 +44,27 @@ namespace crnlib
m_quality_level = quality_level;
m_dxt1a_alpha_threshold = pp.m_dxt1a_alpha_threshold;
}
enum { cMaxQuality = cCRNMaxQualityLevel };
uint m_quality_level;
uint m_dxt1a_alpha_threshold;
crn_dxt_quality m_dxt_quality;
bool m_perceptual;
bool m_use_alpha_blocks;
bool m_hierarchical;
struct mip_desc
{
uint m_first_block;
uint m_block_width;
uint m_block_height;
};
uint m_num_mips;
enum { cMaxMips = 128 };
mip_desc m_mip_desc[cMaxMips];
typedef bool (*progress_callback_func)(uint percentage_completed, void* pProgress_data);
progress_callback_func m_pProgress_func;
void* m_pProgress_data;
@@ -73,67 +75,67 @@ namespace crnlib
class qdxt1
{
CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(qdxt1);
public:
qdxt1(task_pool& task_pool);
~qdxt1();
void clear();
bool init(uint n, const dxt_pixel_block* pBlocks, const qdxt1_params& params);
uint get_num_blocks() const { return m_num_blocks; }
const dxt_pixel_block* get_blocks() const { return m_pBlocks; }
bool pack(dxt1_block* pDst_elements, uint elements_per_block, const qdxt1_params& params, float quality_power_mul);
private:
task_pool* m_pTask_pool;
crn_thread_id_t m_main_thread_id;
uint32 m_main_thread_id;
bool m_canceled;
uint m_progress_start;
uint m_progress_range;
uint m_num_blocks;
const dxt_pixel_block* m_pBlocks;
dxt1_block* m_pDst_elements;
uint m_elements_per_block;
qdxt1_params m_params;
uint m_max_selector_clusters;
int m_prev_percentage_complete;
typedef vec<6, float> vec6F;
typedef clusterizer<vec6F> vec6F_clusterizer;
vec6F_clusterizer m_endpoint_clusterizer;
crnlib::vector< crnlib::vector<uint> > m_endpoint_cluster_indices;
typedef vec<16, float> vec16F;
typedef threaded_clusterizer<vec16F> vec16F_clusterizer;
typedef vec16F_clusterizer::weighted_vec weighted_selector_vec;
typedef vec16F_clusterizer::weighted_vec_array weighted_selector_vec_array;
vec16F_clusterizer m_selector_clusterizer;
crnlib::vector< crnlib::vector<uint> > m_cached_selector_cluster_indices[qdxt1_params::cMaxQuality + 1];
struct cluster_id
{
cluster_id() : m_hash(0)
{
}
cluster_id(const crnlib::vector<uint>& indices)
{
set(indices);
}
void set(const crnlib::vector<uint>& indices)
{
m_cells.resize(indices.size());
@@ -143,29 +145,29 @@ namespace crnlib
std::sort(m_cells.begin(), m_cells.end());
m_hash = fast_hash(&m_cells[0], sizeof(m_cells[0]) * m_cells.size());
m_hash = fast_hash(&m_cells[0], sizeof(m_cells[0]) * m_cells.size());
}
bool operator< (const cluster_id& rhs) const
{
return m_cells < rhs.m_cells;
}
bool operator== (const cluster_id& rhs) const
{
if (m_hash != rhs.m_hash)
if (m_hash != rhs.m_hash)
return false;
return m_cells == rhs.m_cells;
}
crnlib::vector<uint32> m_cells;
size_t m_hash;
operator size_t() const { return m_hash; }
};
typedef crnlib::hash_map<cluster_id, uint> cluster_hash;
cluster_hash m_cluster_hash;
spinlock m_cluster_hash_lock;
@@ -176,10 +178,10 @@ namespace crnlib
void pack_endpoints_task(uint64 data, void* pData_ptr);
void optimize_selectors_task(uint64 data, void* pData_ptr);
bool create_selector_clusters(uint max_selector_clusters, crnlib::vector< crnlib::vector<uint> >& selector_cluster_indices);
inline dxt1_block& get_block(uint index) const { return m_pDst_elements[index * m_elements_per_block]; }
};
CRNLIB_DEFINE_BITWISE_MOVABLE(qdxt1::cluster_id);
} // namespace crnlib
+6 -7
View File
@@ -62,7 +62,7 @@ namespace crnlib
CRNLIB_ASSERT(n && pBlocks);
m_main_thread_id = crn_get_current_thread_id();
m_main_thread_id = get_current_thread_id();
m_num_blocks = n;
m_pBlocks = pBlocks;
@@ -286,7 +286,7 @@ namespace crnlib
#if QDXT5_DEBUGGING
if (debugging)
image_utils::write_to_file(dynamic_wstring(cVarArg, "debug_%u.tga", level).get_ptr(), debug_img, image_utils::cWriteFlagIgnoreAlpha);
image_utils::save_to_file_stb(dynamic_wstring(cVarArg, L"debug_%u.tga", level).get_ptr(), debug_img, image_utils::cSaveIgnoreAlpha);
#endif
} // level
@@ -419,7 +419,7 @@ namespace crnlib
if ((cluster_index & cluster_index_progress_mask) == 0)
{
if (crn_get_current_thread_id() == m_main_thread_id)
if (get_current_thread_id() == m_main_thread_id)
{
if (!update_progress(cluster_index, m_endpoint_cluster_indices.size() - 1))
return;
@@ -444,8 +444,7 @@ namespace crnlib
{
const uint block_index = cluster_indices[block_iter];
//const color_quad_u8* pSrc_pixels = &m_pBlocks[block_index].m_pixels[0][0];
const color_quad_u8* pSrc_pixels = (const color_quad_u8*)m_pBlocks[block_index].m_pixels;
const color_quad_u8* pSrc_pixels = &m_pBlocks[block_index].m_pixels[0][0];
for (uint i = 0; i < cDXTBlockSize * cDXTBlockSize; i++)
{
@@ -522,7 +521,7 @@ namespace crnlib
if ((cluster_index & 255) == 0)
{
if (crn_get_current_thread_id() == m_main_thread_id)
if (get_current_thread_id() == m_main_thread_id)
{
if (!update_progress(cluster_index, task_params.m_selector_cluster_indices.size() - 1))
return;
@@ -736,7 +735,7 @@ namespace crnlib
{
CRNLIB_ASSERT(m_num_blocks);
m_main_thread_id = crn_get_current_thread_id();
m_main_thread_id = get_current_thread_id();
m_canceled = false;
m_pDst_elements = pDst_elements;
+15 -13
View File
@@ -1,6 +1,8 @@
// File: crn_qdxt5.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#include "crn_task_pool.h"
#include "crn_spinlock.h"
#include "crn_hash_map.h"
#include "crn_clusterizer.h"
#include "crn_hash.h"
@@ -15,23 +17,23 @@ namespace crnlib
qdxt5_params()
{
clear();
}
}
void clear()
{
m_quality_level = cMaxQuality;
m_dxt_quality = cCRNDXTQualityUber;
m_pProgress_func = NULL;
m_pProgress_data = NULL;
m_num_mips = 0;
m_hierarchical = true;
utils::zero_object(m_mip_desc);
m_comp_index = 3;
m_progress_start = 0;
m_progress_range = 100;
m_use_both_block_types = true;
}
@@ -48,7 +50,7 @@ namespace crnlib
uint m_quality_level;
crn_dxt_quality m_dxt_quality;
bool m_hierarchical;
struct mip_desc
{
uint m_first_block;
@@ -65,20 +67,20 @@ namespace crnlib
void* m_pProgress_data;
uint m_progress_start;
uint m_progress_range;
uint m_comp_index;
bool m_use_both_block_types;
};
class qdxt5
{
CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(qdxt5);
public:
qdxt5(task_pool& task_pool);
~qdxt5();
void clear();
bool init(uint n, const dxt_pixel_block* pBlocks, const qdxt5_params& params);
@@ -90,7 +92,7 @@ namespace crnlib
private:
task_pool* m_pTask_pool;
crn_thread_id_t m_main_thread_id;
uint32 m_main_thread_id;
bool m_canceled;
uint m_progress_start;
@@ -144,7 +146,7 @@ namespace crnlib
std::sort(m_cells.begin(), m_cells.end());
m_hash = fast_hash(&m_cells[0], sizeof(m_cells[0]) * m_cells.size());
m_hash = fast_hash(&m_cells[0], sizeof(m_cells[0]) * m_cells.size());
}
bool operator< (const cluster_id& rhs) const
@@ -154,7 +156,7 @@ namespace crnlib
bool operator== (const cluster_id& rhs) const
{
if (m_hash != rhs.m_hash)
if (m_hash != rhs.m_hash)
return false;
return m_cells == rhs.m_cells;
-345
View File
@@ -1,345 +0,0 @@
// File: crn_radix_sort.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
namespace crnlib
{
// Returns pointer to sorted array.
template<typename T>
T* radix_sort(uint num_vals, T* pBuf0, T* pBuf1, uint key_ofs, uint key_size)
{
CRNLIB_ASSERT_OPEN_RANGE(key_ofs, 0, sizeof(T));
CRNLIB_ASSERT_CLOSED_RANGE(key_size, 1, 4);
uint hist[256 * 4];
memset(hist, 0, sizeof(hist[0]) * 256 * key_size);
#define CRNLIB_GET_KEY(p) (*(uint*)((uint8*)(p) + key_ofs))
if (key_size == 4)
{
T* p = pBuf0;
T* q = pBuf0 + num_vals;
for ( ; p != q; p++)
{
const uint key = CRNLIB_GET_KEY(p);
hist[ key & 0xFF]++;
hist[256 + ((key >> 8) & 0xFF)]++;
hist[512 + ((key >> 16) & 0xFF)]++;
hist[768 + ((key >> 24) & 0xFF)]++;
}
}
else if (key_size == 3)
{
T* p = pBuf0;
T* q = pBuf0 + num_vals;
for ( ; p != q; p++)
{
const uint key = CRNLIB_GET_KEY(p);
hist[ key & 0xFF]++;
hist[256 + ((key >> 8) & 0xFF)]++;
hist[512 + ((key >> 16) & 0xFF)]++;
}
}
else if (key_size == 2)
{
T* p = pBuf0;
T* q = pBuf0 + (num_vals >> 1) * 2;
for ( ; p != q; p += 2)
{
const uint key0 = CRNLIB_GET_KEY(p);
const uint key1 = CRNLIB_GET_KEY(p+1);
hist[ key0 & 0xFF]++;
hist[256 + ((key0 >> 8) & 0xFF)]++;
hist[ key1 & 0xFF]++;
hist[256 + ((key1 >> 8) & 0xFF)]++;
}
if (num_vals & 1)
{
const uint key = CRNLIB_GET_KEY(p);
hist[ key & 0xFF]++;
hist[256 + ((key >> 8) & 0xFF)]++;
}
}
else
{
CRNLIB_ASSERT(key_size == 1);
if (key_size != 1)
return NULL;
T* p = pBuf0;
T* q = pBuf0 + (num_vals >> 1) * 2;
for ( ; p != q; p += 2)
{
const uint key0 = CRNLIB_GET_KEY(p);
const uint key1 = CRNLIB_GET_KEY(p+1);
hist[key0 & 0xFF]++;
hist[key1 & 0xFF]++;
}
if (num_vals & 1)
{
const uint key = CRNLIB_GET_KEY(p);
hist[key & 0xFF]++;
}
}
T* pCur = pBuf0;
T* pNew = pBuf1;
for (uint pass = 0; pass < key_size; pass++)
{
const uint* pHist = &hist[pass << 8];
uint offsets[256];
uint cur_ofs = 0;
for (uint i = 0; i < 256; i += 2)
{
offsets[i] = cur_ofs;
cur_ofs += pHist[i];
offsets[i+1] = cur_ofs;
cur_ofs += pHist[i+1];
}
const uint pass_shift = pass << 3;
T* p = pCur;
T* q = pCur + (num_vals >> 1) * 2;
for ( ; p != q; p += 2)
{
uint c0 = (CRNLIB_GET_KEY(p) >> pass_shift) & 0xFF;
uint c1 = (CRNLIB_GET_KEY(p+1) >> pass_shift) & 0xFF;
if (c0 == c1)
{
uint dst_offset0 = offsets[c0];
offsets[c0] = dst_offset0 + 2;
pNew[dst_offset0] = p[0];
pNew[dst_offset0 + 1] = p[1];
}
else
{
uint dst_offset0 = offsets[c0]++;
uint dst_offset1 = offsets[c1]++;
pNew[dst_offset0] = p[0];
pNew[dst_offset1] = p[1];
}
}
if (num_vals & 1)
{
uint c = (CRNLIB_GET_KEY(p) >> pass_shift) & 0xFF;
uint dst_offset = offsets[c];
offsets[c] = dst_offset + 1;
pNew[dst_offset] = *p;
}
T* t = pCur;
pCur = pNew;
pNew = t;
}
return pCur;
}
#undef CRNLIB_GET_KEY
// Returns pointer to sorted array.
template<typename T, typename Q>
T* indirect_radix_sort(uint num_indices, T* pIndices0, T* pIndices1, const Q* pKeys, uint key_ofs, uint key_size, bool init_indices)
{
CRNLIB_ASSERT_OPEN_RANGE(key_ofs, 0, sizeof(T));
CRNLIB_ASSERT_CLOSED_RANGE(key_size, 1, 4);
if (init_indices)
{
T* p = pIndices0;
T* q = pIndices0 + (num_indices >> 1) * 2;
uint i;
for (i = 0; p != q; p += 2, i += 2)
{
p[0] = static_cast<T>(i);
p[1] = static_cast<T>(i + 1);
}
if (num_indices & 1)
*p = static_cast<T>(i);
}
uint hist[256 * 4];
memset(hist, 0, sizeof(hist[0]) * 256 * key_size);
#define CRNLIB_GET_KEY(p) (*(const uint*)((const uint8*)(pKeys + *(p)) + key_ofs))
#define CRNLIB_GET_KEY_FROM_INDEX(i) (*(const uint*)((const uint8*)(pKeys + (i)) + key_ofs))
if (key_size == 4)
{
T* p = pIndices0;
T* q = pIndices0 + num_indices;
for ( ; p != q; p++)
{
const uint key = CRNLIB_GET_KEY(p);
hist[ key & 0xFF]++;
hist[256 + ((key >> 8) & 0xFF)]++;
hist[512 + ((key >> 16) & 0xFF)]++;
hist[768 + ((key >> 24) & 0xFF)]++;
}
}
else if (key_size == 3)
{
T* p = pIndices0;
T* q = pIndices0 + num_indices;
for ( ; p != q; p++)
{
const uint key = CRNLIB_GET_KEY(p);
hist[ key & 0xFF]++;
hist[256 + ((key >> 8) & 0xFF)]++;
hist[512 + ((key >> 16) & 0xFF)]++;
}
}
else if (key_size == 2)
{
T* p = pIndices0;
T* q = pIndices0 + (num_indices >> 1) * 2;
for ( ; p != q; p += 2)
{
const uint key0 = CRNLIB_GET_KEY(p);
const uint key1 = CRNLIB_GET_KEY(p+1);
hist[ key0 & 0xFF]++;
hist[256 + ((key0 >> 8) & 0xFF)]++;
hist[ key1 & 0xFF]++;
hist[256 + ((key1 >> 8) & 0xFF)]++;
}
if (num_indices & 1)
{
const uint key = CRNLIB_GET_KEY(p);
hist[ key & 0xFF]++;
hist[256 + ((key >> 8) & 0xFF)]++;
}
}
else
{
CRNLIB_ASSERT(key_size == 1);
if (key_size != 1)
return NULL;
T* p = pIndices0;
T* q = pIndices0 + (num_indices >> 1) * 2;
for ( ; p != q; p += 2)
{
const uint key0 = CRNLIB_GET_KEY(p);
const uint key1 = CRNLIB_GET_KEY(p+1);
hist[key0 & 0xFF]++;
hist[key1 & 0xFF]++;
}
if (num_indices & 1)
{
const uint key = CRNLIB_GET_KEY(p);
hist[key & 0xFF]++;
}
}
T* pCur = pIndices0;
T* pNew = pIndices1;
for (uint pass = 0; pass < key_size; pass++)
{
const uint* pHist = &hist[pass << 8];
uint offsets[256];
uint cur_ofs = 0;
for (uint i = 0; i < 256; i += 2)
{
offsets[i] = cur_ofs;
cur_ofs += pHist[i];
offsets[i+1] = cur_ofs;
cur_ofs += pHist[i+1];
}
const uint pass_shift = pass << 3;
T* p = pCur;
T* q = pCur + (num_indices >> 1) * 2;
for ( ; p != q; p += 2)
{
uint index0 = p[0];
uint index1 = p[1];
uint c0 = (CRNLIB_GET_KEY_FROM_INDEX(index0) >> pass_shift) & 0xFF;
uint c1 = (CRNLIB_GET_KEY_FROM_INDEX(index1) >> pass_shift) & 0xFF;
if (c0 == c1)
{
uint dst_offset0 = offsets[c0];
offsets[c0] = dst_offset0 + 2;
pNew[dst_offset0] = static_cast<T>(index0);
pNew[dst_offset0 + 1] = static_cast<T>(index1);
}
else
{
uint dst_offset0 = offsets[c0]++;
uint dst_offset1 = offsets[c1]++;
pNew[dst_offset0] = static_cast<T>(index0);
pNew[dst_offset1] = static_cast<T>(index1);
}
}
if (num_indices & 1)
{
uint index = *p;
uint c = (CRNLIB_GET_KEY_FROM_INDEX(index) >> pass_shift) & 0xFF;
uint dst_offset = offsets[c];
offsets[c] = dst_offset + 1;
pNew[dst_offset] = static_cast<T>(index);
}
T* t = pCur;
pCur = pNew;
pNew = t;
}
return pCur;
}
#undef CRNLIB_GET_KEY
#undef CRNLIB_GET_KEY_FROM_INDEX
} // namespace crnlib
+1 -21
View File
@@ -174,13 +174,6 @@ namespace crnlib
return m_kiss99.next() ^ (m_ranctx.next() + m_well512.next());
}
uint64 random::urand64()
{
uint64 result = urand32();
result <<= 32ULL;
result |= urand32();
return result;
}
uint32 random::fast_urand32()
{
return m_well512.next();
@@ -211,7 +204,7 @@ namespace crnlib
return math::clamp<float>(r, l, h);
}
int random::irand(int l, int h)
{
CRNLIB_ASSERT(l < h);
@@ -236,12 +229,6 @@ namespace crnlib
return result;
}
int random::irand_inclusive(int l, int h)
{
CRNLIB_ASSERT(h < cINT32_MAX);
return irand(l, h + 1);
}
/*
ALGORITHM 712, COLLECTED ALGORITHMS FROM ACM.
THIS WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE,
@@ -330,13 +317,6 @@ namespace crnlib
return SHR3 ^ CONG;
}
uint64 fast_random::urand64()
{
uint64 result = urand32();
result <<= 32ULL;
result |= urand32();
return result;
}
int fast_random::irand(int l, int h)
{
CRNLIB_ASSERT(l < h);
-5
View File
@@ -63,7 +63,6 @@ namespace crnlib
void seed(uint32 i1, uint32 i2, uint32 i3);
uint32 urand32();
uint64 urand64();
// "Fast" variant uses no multiplies.
uint32 fast_urand32();
@@ -77,9 +76,6 @@ namespace crnlib
// Returns random between [l, h)
int irand(int l, int h);
// Returns random between [l, h]
int irand_inclusive(int l, int h);
double gaussian(double mean, double stddev);
@@ -103,7 +99,6 @@ namespace crnlib
void seed(uint32 i);
uint32 urand32();
uint64 urand64();
int irand(int l, int h);
+1 -106
View File
@@ -17,8 +17,7 @@ namespace crnlib
{
clear();
}
// up to, but not including right/bottom
inline rect(int left, int top, int right, int bottom)
{
set(left, top, right, bottom);
@@ -35,24 +34,6 @@ namespace crnlib
m_corner[0] = point;
m_corner[1].set(point[0] + 1, point[1] + 1);
}
inline bool operator== (const rect& r) const
{
return (m_corner[0] == r.m_corner[0]) && (m_corner[1] == r.m_corner[1]);
}
inline bool operator< (const rect& r) const
{
for (uint i = 0; i < 2; i++)
{
if (m_corner[i] < r.m_corner[i])
return true;
else if (!(m_corner[i] == r.m_corner[i]))
return false;
}
return false;
}
inline void clear()
{
@@ -89,96 +70,10 @@ namespace crnlib
inline bool is_empty() const { return (m_corner[1][0] <= m_corner[0][0]) || (m_corner[1][1] <= m_corner[0][1]); }
inline uint get_dimension(uint axis) const { return m_corner[1][axis] - m_corner[0][axis]; }
inline uint get_area() const { return get_dimension(0) * get_dimension(1); }
inline const vec2I& operator[] (uint i) const { CRNLIB_ASSERT(i < 2); return m_corner[i]; }
inline vec2I& operator[] (uint i) { CRNLIB_ASSERT(i < 2); return m_corner[i]; }
inline rect& translate(int x_ofs, int y_ofs)
{
m_corner[0][0] += x_ofs;
m_corner[0][1] += y_ofs;
m_corner[1][0] += x_ofs;
m_corner[1][1] += y_ofs;
return *this;
}
inline rect& init_expand()
{
m_corner[0].set(INT_MAX);
m_corner[1].set(INT_MIN);
return *this;
}
inline rect& expand(int x, int y)
{
m_corner[0][0] = math::minimum(m_corner[0][0], x);
m_corner[0][1] = math::minimum(m_corner[0][1], y);
m_corner[1][0] = math::maximum(m_corner[1][0], x + 1);
m_corner[1][1] = math::maximum(m_corner[1][1], y + 1);
return *this;
}
inline rect& expand(const rect& r)
{
m_corner[0][0] = math::minimum(m_corner[0][0], r[0][0]);
m_corner[0][1] = math::minimum(m_corner[0][1], r[0][1]);
m_corner[1][0] = math::maximum(m_corner[1][0], r[1][0]);
m_corner[1][1] = math::maximum(m_corner[1][1], r[1][1]);
return *this;
}
inline bool touches(const rect& r) const
{
for (uint i = 0; i < 2; i++)
{
if (r[1][i] <= m_corner[0][i])
return false;
else if (r[0][i] >= m_corner[1][i])
return false;
}
return true;
}
inline bool within(const rect& r) const
{
for (uint i = 0; i < 2; i++)
{
if (m_corner[0][i] < r[0][i])
return false;
else if (m_corner[1][i] > r[1][i])
return false;
}
return true;
}
inline bool intersect(const rect& r)
{
if (!touches(r))
{
clear();
return false;
}
for (uint i = 0; i < 2; i++)
{
m_corner[0][i] = math::maximum<int>(m_corner[0][i], r[0][i]);
m_corner[1][i] = math::minimum<int>(m_corner[1][i], r[1][i]);
}
return true;
}
inline bool contains(int x, int y) const
{
return (x >= m_corner[0][0]) && (x < m_corner[1][0]) &&
(y >= m_corner[0][1]) && (y < m_corner[1][1]);
}
inline bool contains(const vec2I& p) const { return contains(p[0], p[1]); }
private:
vec2I m_corner[2];
};
+1 -1
View File
@@ -132,7 +132,7 @@ namespace crnlib
if (xscale < 1.0f)
{
int total; (void)total;
int total;
/* Handle case when there are fewer destination
* samples than source samples (downsampling/minification).
File diff suppressed because it is too large Load Diff
-80
View File
@@ -1,80 +0,0 @@
// File: rg_etc1.h - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich <richgel99@gmail.com>
// Please see ZLIB license at the end of this file.
#pragma once
namespace crnlib {
namespace rg_etc1
{
// Unpacks an 8-byte ETC1 compressed block to a block of 4x4 32bpp RGBA pixels.
// Returns false if the block is invalid. Invalid blocks will still be unpacked with clamping.
// This function is thread safe, and does not dynamically allocate any memory.
// If preserve_alpha is true, the alpha channel of the destination pixels will not be overwritten. Otherwise, alpha will be set to 255.
bool unpack_etc1_block(const void *pETC1_block, unsigned int* pDst_pixels_rgba, bool preserve_alpha = false);
// Quality setting = the higher the quality, the slower.
// To pack large textures, it is highly recommended to call pack_etc1_block() in parallel, on different blocks, from multiple threads (particularly when using cHighQuality).
enum etc1_quality
{
cLowQuality,
cMediumQuality,
cHighQuality,
};
struct etc1_pack_params
{
etc1_quality m_quality;
bool m_dithering;
inline etc1_pack_params()
{
clear();
}
void clear()
{
m_quality = cHighQuality;
m_dithering = false;
}
};
// Important: pack_etc1_block_init() must be called before calling pack_etc1_block().
void pack_etc1_block_init();
// Packs a 4x4 block of 32bpp RGBA pixels to an 8-byte ETC1 block.
// 32-bit RGBA pixels must always be arranged as (R,G,B,A) (R first, A last) in memory, independent of platform endianness. A should always be 255.
// Returns squared error of result.
// This function is thread safe, and does not dynamically allocate any memory.
// pack_etc1_block() does not currently support "perceptual" colorspace metrics - it primarily optimizes for RGB RMSE.
unsigned int pack_etc1_block(void* pETC1_block, const unsigned int* pSrc_pixels_rgba, etc1_pack_params& pack_params);
} // namespace rg_etc1
} // namespace crnlib
//------------------------------------------------------------------------------
//
// rg_etc1 uses the ZLIB license:
// http://opensource.org/licenses/Zlib
//
// Copyright (c) 2012 Rich Geldreich
//
// 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.
//
//------------------------------------------------------------------------------
+25
View File
@@ -0,0 +1,25 @@
// File: crn_semaphore.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
namespace crnlib
{
class semaphore
{
CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(semaphore);
public:
semaphore(int32 initialCount = 0, int32 maximumCount = 1, const char* pName = NULL);
~semaphore();
inline void *get_handle(void) const { return m_handle; }
void release(int32 releaseCount = 1, int32 *pPreviousCount = NULL);
bool wait(uint32 milliseconds = UINT32_MAX);
private:
void *m_handle;
};
} // namespace crnlib
+38
View File
@@ -0,0 +1,38 @@
// File: crn_spinlock.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
namespace crnlib
{
// Simple non-recursive spinlock.
class spinlock
{
public:
inline spinlock() : m_flag(0) { }
void lock(uint32 max_spins = 4096, bool yielding = true, bool memoryBarrier = true);
inline void lock_no_barrier(uint32 max_spins = 4096, bool yielding = true) { lock(max_spins, yielding, false); }
void unlock();
inline void unlock_no_barrier() { m_flag = CRNLIB_FALSE; }
private:
volatile int32 m_flag;
};
class scoped_spinlock
{
scoped_spinlock(const scoped_spinlock&);
scoped_spinlock& operator= (const scoped_spinlock&);
public:
inline scoped_spinlock(spinlock& lock) : m_lock(lock) { m_lock.lock(); }
inline ~scoped_spinlock() { m_lock.unlock(); }
private:
spinlock& m_lock;
};
} // namespace crnlib
+17 -44
View File
@@ -193,23 +193,17 @@ typedef unsigned char stbi_uc;
// (you must include the appropriate extension in the filename).
// returns TRUE on success, FALSE if couldn't open file, error writing file
extern int stbi_write_bmp (char const *filename, int x, int y, int comp, const void *data);
#ifdef _MSC_VER
extern int stbi_write_bmp_w (wchar_t const *filename, int x, int y, int comp, const void *data);
#endif
extern int stbi_write_tga (char const *filename, int x, int y, int comp, const void *data);
#ifdef _MSC_VER
extern int stbi_write_tga_w (wchar_t const *filename, int x, int y, int comp, const void *data);
#endif
#endif
// PRIMARY API - works on images of any type
// load image by filename, open file, or memory buffer
#ifndef STBI_NO_STDIO
extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
#ifdef _MSC_VER
extern stbi_uc *stbi_load_w (wchar_t const *filename, int *x, int *y, int *comp, int req_comp);
#endif
extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
#endif
@@ -233,7 +227,7 @@ extern void stbi_ldr_to_hdr_scale(float scale);
// get a VERY brief reason for failure
// NOT THREADSAFE
extern const char *stbi_failure_reason (void);
extern char *stbi_failure_reason (void);
// free the loaded image -- this is just stb_free()
extern void stbi_image_free (void *retval_from_stbi_load);
@@ -424,14 +418,14 @@ typedef unsigned char validate_uint32[sizeof(uint32)==4];
//
// this is not threadsafe
static const char *failure_reason;
static char *failure_reason;
const char *stbi_failure_reason(void)
char *stbi_failure_reason(void)
{
return failure_reason;
}
static int e(const char *str)
static int e(char *str)
{
failure_reason = str;
return 0;
@@ -491,7 +485,6 @@ unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int re
return result;
}
#ifdef _MSC_VER
unsigned char *stbi_load_w(wchar_t const *filename, int *x, int *y, int *comp, int req_comp)
{
FILE *f = _wfopen(filename, L"rb");
@@ -501,7 +494,6 @@ unsigned char *stbi_load_w(wchar_t const *filename, int *x, int *y, int *comp, i
fclose(f);
return result;
}
#endif
unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
{
@@ -756,7 +748,7 @@ static void getn(stbi *s, stbi_uc *buffer, int n)
{
#ifndef STBI_NO_STDIO
if (s->img_file) {
size_t nr = fread(buffer, 1, n, s->img_file); nr;
fread(buffer, 1, n, s->img_file);
return;
}
#endif
@@ -1623,13 +1615,11 @@ typedef uint8 *(*resample_row_func)(uint8 *out, uint8 *in0, uint8 *in1,
static uint8 *resample_row_1(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
{
out, in_far, w, hs;
return in_near;
}
static uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
{
hs;
// need to generate two samples vertically for every one in input
int i;
for (i=0; i < w; ++i)
@@ -1639,7 +1629,6 @@ static uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w,
static uint8* resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
{
hs, in_far;
// need to generate two samples horizontally for every one in input
int i;
uint8 *input = in_near;
@@ -1665,7 +1654,6 @@ static uint8* resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w
static uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
{
hs;
// need to generate 2x2 samples for every one in input
int i,t0,t1;
if (w == 1) {
@@ -1687,7 +1675,6 @@ static uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w
static uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
{
in_far;
// resample with nearest-neighbor
int i,j;
for (i=0; i < w; ++i)
@@ -2408,14 +2395,10 @@ static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, u
a->out = (uint8 *) stb_malloc(x * y * out_n);
if (!a->out) return e("outofmem", "Out of memory");
if (!stbi_png_partial) {
if ((s->img_x == x) && (s->img_y == y))
{
if (s->img_x == x && s->img_y == y)
if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
}
else // interlaced:
{
if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
}
}
for (j=0; j < y; ++j) {
uint8 *cur = a->out + stride*j;
@@ -2545,7 +2528,6 @@ static int compute_transparency(png *z, uint8 tc[3], int out_n)
static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n)
{
len;
uint32 i, pixel_count = a->s.img_x * a->s.img_y;
uint8 *p, *temp_out, *orig = a->out;
@@ -2900,7 +2882,7 @@ static int shiftsigned(int v, int shift, int bits)
static stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp)
{
uint8 *out;
unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0; (void)fake_a;
unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
stbi_uc pal[256][4];
int psize=0,i,j,compress=0,width;
int bpp, flip_vertically, pad, target, offset, hsz;
@@ -3182,7 +3164,7 @@ static stbi_uc *tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)
unsigned char *tga_palette = NULL;
int i, j;
unsigned char raw_data[4];
unsigned char trans_data[4] = { 0, 0, 0, 0 };
unsigned char trans_data[4];
int RLE_count = 0;
int RLE_repeating = 0;
int read_next_pixel = 1;
@@ -3420,7 +3402,6 @@ int stbi_psd_test_file(FILE *f)
{
stbi s;
int r,n = ftell(f);
memset(&s, 0, sizeof(s));
start_file(&s, f);
r = psd_test(&s);
fseek(f,n,SEEK_SET);
@@ -3627,7 +3608,7 @@ stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int
#ifndef STBI_NO_HDR
static int hdr_test(stbi *s)
{
const char *signature = "#?RADIANCE\n";
char *signature = "#?RADIANCE\n";
int i;
for (i=0; signature[i]; ++i)
if (get8(s) != signature[i])
@@ -3647,7 +3628,6 @@ int stbi_hdr_test_file(FILE *f)
{
stbi s;
int r,n = ftell(f);
memset(&s, 0, sizeof(s));
start_file(&s, f);
r = hdr_test(&s);
fseek(f,n,SEEK_SET);
@@ -3659,8 +3639,7 @@ int stbi_hdr_test_file(FILE *f)
static char *hdr_gettoken(stbi *z, char *buffer)
{
int len=0;
//char *s = buffer;
char c = '\0';
char *s = buffer, c = '\0';
c = get8(z);
@@ -3880,18 +3859,18 @@ static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp,
fwrite(&d[comp-1], 1, 1, f);
switch (comp) {
case 1:
case 2: writef(f, (char*)"111", d[0],d[0],d[0]);
case 2: writef(f, "111", d[0],d[0],d[0]);
break;
case 4:
if (!write_alpha) {
for (k=0; k < 3; ++k)
px[k] = bg[k] + ((d[k] - bg[k]) * d[3])/255;
writef(f, (char*)"111", px[1-rgb_dir],px[1],px[1+rgb_dir]);
writef(f, "111", px[1-rgb_dir],px[1],px[1+rgb_dir]);
break;
}
/* FALLTHROUGH */
case 3:
writef(f, (char*)"111", d[1-rgb_dir],d[1],d[1+rgb_dir]);
writef(f, "111", d[1-rgb_dir],d[1],d[1+rgb_dir]);
break;
}
if (write_alpha > 0)
@@ -3915,7 +3894,6 @@ static int outfile(char const *filename, int rgb_dir, int vdir, int x, int y, in
return f != NULL;
}
#ifdef _MSC_VER
static int outfile_w(wchar_t const *filename, int rgb_dir, int vdir, int x, int y, int comp, const void *data, int alpha, int pad, char *fmt, ...)
{
FILE *f = _wfopen(filename, L"wb");
@@ -3929,43 +3907,38 @@ static int outfile_w(wchar_t const *filename, int rgb_dir, int vdir, int x, int
}
return f != NULL;
}
#endif
int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data)
{
int pad = (-x*3) & 3;
return outfile(filename,-1,-1,x,y,comp,data,0,pad,
(char*)"11 4 22 4" "4 44 22 444444",
"11 4 22 4" "4 44 22 444444",
'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header
40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header
}
#ifdef _MSC_VER
int stbi_write_bmp_w(wchar_t const *filename, int x, int y, int comp, const void *data)
{
int pad = (-x*3) & 3;
return outfile_w(filename,-1,-1,x,y,comp,data,0,pad,
(char*)"11 4 22 4" "4 44 22 444444",
"11 4 22 4" "4 44 22 444444",
'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header
40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header
}
#endif
int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
{
int has_alpha = !(comp & 1);
return outfile(filename, -1,-1, x, y, comp, data, has_alpha, 0,
(char*)"111 221 2222 11", 0,0,2, 0,0,0, 0,0,x,y, 24+8*has_alpha, 8*has_alpha);
"111 221 2222 11", 0,0,2, 0,0,0, 0,0,x,y, 24+8*has_alpha, 8*has_alpha);
}
#ifdef _MSC_VER
int stbi_write_tga_w(wchar_t const *filename, int x, int y, int comp, const void *data)
{
int has_alpha = !(comp & 1);
return outfile_w(filename, -1,-1, x, y, comp, data, has_alpha, 0,
(char*)"111 221 2222 11", 0,0,2, 0,0,0, 0,0,x,y, 24+8*has_alpha, 8*has_alpha);
"111 221 2222 11", 0,0,2, 0,0,0, 0,0,x,y, 24+8*has_alpha, 8*has_alpha);
}
#endif
// any other image formats that do interleaved rgb data?
// PNG: requires adler32,crc32 -- significant amount of code
+711 -64
View File
@@ -2,27 +2,10 @@
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_strutils.h"
#include <direct.h>
namespace crnlib
{
char* crn_strdup(const char* pStr)
{
if (!pStr)
pStr = "";
size_t l = strlen(pStr) + 1;
char *p = (char *)crnlib_malloc(l);
if (p)
memcpy(p, pStr, l);
return p;
}
int crn_stricmp(const char *p, const char *q)
{
return _stricmp(p, q);
}
char* strcpy_safe(char* pDst, uint dst_len, const char* pSrc)
{
CRNLIB_ASSERT(pDst && pSrc && dst_len);
@@ -181,6 +164,76 @@ namespace crnlib
return true;
}
bool string_to_int(const wchar_t*& pBuf, int& value)
{
value = 0;
CRNLIB_ASSERT(pBuf);
const wchar_t* p = pBuf;
while (*p && isspace(*p))
p++;
uint result = 0;
bool negative = false;
if (!iswdigit(*p))
{
if (p[0] == '-')
{
negative = true;
p++;
}
else
return false;
}
while (*p && iswdigit(*p))
{
if (result & 0xE0000000U)
return false;
const uint result8 = result << 3U;
const uint result2 = result << 1U;
if (result2 > (0xFFFFFFFFU - result8))
return false;
result = result8 + result2;
uint c = p[0] - L'0';
if (c > (0xFFFFFFFFU - result))
return false;
result += c;
p++;
}
if (negative)
{
if (result > 0x80000000U)
{
value = 0;
return false;
}
value = -static_cast<int>(result);
}
else
{
if (result > 0x7FFFFFFFU)
{
value = 0;
return false;
}
value = static_cast<int>(result);
}
pBuf = p;
return true;
}
bool string_to_int64(const char*& pBuf, int64& value)
{
value = 0;
@@ -295,6 +348,50 @@ namespace crnlib
return true;
}
bool string_to_uint(const wchar_t*& pBuf, uint& value)
{
value = 0;
CRNLIB_ASSERT(pBuf);
const wchar_t* p = pBuf;
while (*p && iswspace(*p))
p++;
uint result = 0;
if (!iswdigit(*p))
return false;
while (*p && iswdigit(*p))
{
if (result & 0xE0000000U)
return false;
const uint result8 = result << 3U;
const uint result2 = result << 1U;
if (result2 > (0xFFFFFFFFU - result8))
return false;
result = result8 + result2;
uint c = p[0] - L'0';
if (c > (0xFFFFFFFFU - result))
return false;
result += c;
p++;
}
value = result;
pBuf = p;
return true;
}
bool string_to_uint64(const char*& pBuf, uint64& value)
{
value = 0;
@@ -370,66 +467,77 @@ namespace crnlib
return false;
}
bool string_to_float(const char*& p, float& value, uint round_digit)
{
double d;
if (!string_to_double(p, d, round_digit))
{
value = 0;
return false;
}
value = static_cast<float>(d);
return true;
}
bool string_to_double(const char*& p, double& value, uint round_digit)
{
return string_to_double(p, p + 128, value, round_digit);
}
// I wrote this approx. 20 years ago in C/assembly using a limited FP emulator package, so it's a bit crude.
bool string_to_double(const char*& p, const char *pEnd, double& value, uint round_digit)
bool string_to_bool(const wchar_t* p, bool& value)
{
CRNLIB_ASSERT(p);
value = false;
if (_wcsicmp(p, L"false") == 0)
return true;
if (_wcsicmp(p, L"true") == 0)
{
value = true;
return true;
}
const wchar_t* q = p;
uint v;
if (string_to_uint(q, v))
{
if (!v)
return true;
else if (v == 1)
{
value = true;
return true;
}
}
return false;
}
bool string_to_float(const char*& p, float& value, uint round_digit)
{
CRNLIB_ASSERT(p);
value = 0;
enum { AF_BLANK = 1, AF_SIGN = 2, AF_DPOINT = 3, AF_BADCHAR = 4, AF_OVRFLOW = 5, AF_EXPONENT = 6, AF_NODIGITS = 7 };
int status = 0;
const char* buf = p;
int got_sign_flag = 0, got_dp_flag = 0, got_num_flag = 0;
int got_e_flag = 0, got_e_sign_flag = 0, e_sign = 0;
uint whole_count = 0, frac_count = 0;
int status = 0;
double whole = 0, frac = 0, scale = 1, exponent = 1;
if (round_digit > 10)
round_digit = 10;
if (p >= pEnd)
{
status = AF_NODIGITS;
goto af_exit;
}
int got_sign_flag = 0;
int got_dp_flag = 0;
int got_num_flag = 0;
int got_e_flag = 0;
int got_e_sign_flag = 0;
int e_sign = 0;
uint whole_count = 0;
uint frac_count = 0;
float whole = 0;
float frac = 0;
float scale = 1;
float exponent = 1;
while (*buf)
{
if (!isspace(*buf))
break;
if (++buf >= pEnd)
{
status = AF_NODIGITS;
goto af_exit;
}
}
p = buf;
buf++;
}
while (*buf)
{
p = buf;
if (buf >= pEnd)
break;
int i = *buf++;
switch (i)
@@ -508,7 +616,7 @@ namespace crnlib
whole_count++;
if (whole > 1e+100)
if (whole > 1e+30f)
{
status = AF_OVRFLOW;
goto af_exit;
@@ -538,10 +646,6 @@ namespace crnlib
while (*buf)
{
p = buf;
if (buf >= pEnd)
break;
int i = *buf++;
if (i == '+')
@@ -570,7 +674,7 @@ namespace crnlib
{
got_num_flag = 1;
if ((e = (e * 10) + (i - 48)) > 100)
if ((e = (e * 10) + (i - 48)) > 16)
{
status = AF_EXPONENT;
goto af_exit;
@@ -605,9 +709,552 @@ namespace crnlib
whole = -whole;
value = whole;
p = buf;
af_exit:
return (status == 0);
}
bool string_to_float(const wchar_t*& p, float& value, uint round_digit)
{
CRNLIB_ASSERT(p);
value = 0;
enum { AF_BLANK = 1, AF_SIGN = 2, AF_DPOINT = 3, AF_BADCHAR = 4, AF_OVRFLOW = 5, AF_EXPONENT = 6, AF_NODIGITS = 7 };
const wchar_t* buf = p;
int status = 0;
if (round_digit > 10)
round_digit = 10;
int got_sign_flag = 0;
int got_dp_flag = 0;
int got_num_flag = 0;
int got_e_flag = 0;
int got_e_sign_flag = 0;
int e_sign = 0;
uint whole_count = 0;
uint frac_count = 0;
float whole = 0;
float frac = 0;
float scale = 1;
float exponent = 1;
while (*buf)
{
if (!iswspace(*buf))
break;
buf++;
}
while (*buf)
{
int i = *buf++;
switch (i)
{
case L'e':
case L'E':
{
got_e_flag = 1;
goto exit_while;
}
case L'+':
{
if ((got_num_flag) || (got_sign_flag))
{
status = AF_SIGN;
goto af_exit;
}
got_sign_flag = 1;
break;
}
case L'-':
{
if ((got_num_flag) || (got_sign_flag))
{
status = AF_SIGN;
goto af_exit;
}
got_sign_flag = -1;
break;
}
case L'.':
{
if (got_dp_flag)
{
status = AF_DPOINT;
goto af_exit;
}
got_dp_flag = 1;
break;
}
default:
{
if ((i < L'0') || (i > L'9'))
goto exit_while;
else
{
i -= L'0';
got_num_flag = 1;
if (got_dp_flag)
{
if (frac_count < round_digit)
{
frac = frac * 10.0f + i;
scale = scale * 10.0f;
}
else if (frac_count == round_digit)
{
if (i >= 5) /* check for round */
frac = frac + 1.0f;
}
frac_count++;
}
else
{
whole = whole * 10.0f + i;
whole_count++;
if (whole > 1e+30f)
{
status = AF_OVRFLOW;
goto af_exit;
}
}
}
break;
}
}
}
exit_while:
if (got_e_flag)
{
if ((got_num_flag == 0) && (got_dp_flag))
{
status = AF_EXPONENT;
goto af_exit;
}
int e = 0;
e_sign = 1;
got_num_flag = 0;
got_e_sign_flag = 0;
while (*buf)
{
int i = *buf++;
if (i == L'+')
{
if ((got_num_flag) || (got_e_sign_flag))
{
status = AF_EXPONENT;
goto af_exit;
}
e_sign = 1;
got_e_sign_flag = 1;
}
else if (i == L'-')
{
if ((got_num_flag) || (got_e_sign_flag))
{
status = AF_EXPONENT;
goto af_exit;
}
e_sign = -1;
got_e_sign_flag = 1;
}
else if ((i >= L'0') && (i <= L'9'))
{
got_num_flag = 1;
if ((e = (e * 10) + (i - 48)) > 16)
{
status = AF_EXPONENT;
goto af_exit;
}
}
else
break;
}
for (int i = 1; i <= e; i++) /* compute 10^e */
exponent = exponent * 10.0f;
}
if (((whole_count + frac_count) == 0) && (got_e_flag == 0))
{
status = AF_NODIGITS;
goto af_exit;
}
if (frac)
whole = whole + (frac / scale);
if (got_e_flag)
{
if (e_sign > 0)
whole = whole * exponent;
else
whole = whole / exponent;
}
if (got_sign_flag < 0)
whole = -whole;
value = whole;
p = buf;
af_exit:
return (status == 0);
}
bool split_path(const char* p, dynamic_string* pDrive, dynamic_string* pDir, dynamic_string* pFilename, dynamic_string* pExt)
{
CRNLIB_ASSERT(p);
char drive_buf[_MAX_DRIVE];
char dir_buf[_MAX_DIR];
char fname_buf[_MAX_FNAME];
char ext_buf[_MAX_EXT];
#ifdef _MSC_VER
errno_t error = _splitpath_s(p,
pDrive ? drive_buf : NULL, pDrive ? _MAX_DRIVE : 0,
pDir ? dir_buf : NULL, pDir ? _MAX_DIR : 0,
pFilename ? fname_buf : NULL, pFilename ? _MAX_FNAME : 0,
pExt ? ext_buf : NULL, pExt ? _MAX_EXT : 0);
if (error != 0)
return false;
#else
_splitpath(p,
pDrive ? drive_buf : NULL,
pDir ? dir_buf : NULL,
pFilename ? fname_buf : NULL,
pExt ? ext_buf : NULL);
#endif
if (pDrive) *pDrive = drive_buf;
if (pDir) *pDir = dir_buf;
if (pFilename) *pFilename = fname_buf;
if (pExt) *pExt = ext_buf;
return true;
}
bool split_path(const wchar_t* p, dynamic_wstring* pDrive, dynamic_wstring* pDir, dynamic_wstring* pFilename, dynamic_wstring* pExt)
{
CRNLIB_ASSERT(p);
wchar_t drive_buf[_MAX_DRIVE];
wchar_t dir_buf[_MAX_DIR];
wchar_t fname_buf[_MAX_FNAME];
wchar_t ext_buf[_MAX_EXT];
#ifdef _MSC_VER
errno_t error = _wsplitpath_s(p,
pDrive ? drive_buf : NULL, pDrive ? _MAX_DRIVE : 0,
pDir ? dir_buf : NULL, pDir ? _MAX_DIR : 0,
pFilename ? fname_buf : NULL, pFilename ? _MAX_FNAME : 0,
pExt ? ext_buf : NULL, pExt ? _MAX_EXT : 0);
if (error != 0)
return false;
#else
_wsplitpath(p,
pDrive ? drive_buf : NULL,
pDir ? dir_buf : NULL,
pFilename ? fname_buf : NULL,
pExt ? ext_buf : NULL);
#endif
if (pDrive) *pDrive = drive_buf;
if (pDir) *pDir = dir_buf;
if (pFilename) *pFilename = fname_buf;
if (pExt) *pExt = ext_buf;
return true;
}
bool split_path(const char* p, dynamic_string& path, dynamic_string& filename)
{
dynamic_string temp_drive, temp_path, temp_ext;
if (!split_path(p, &temp_drive, &temp_path, &filename, &temp_ext))
return false;
filename += temp_ext;
combine_path(path, temp_drive.get_ptr(), temp_path.get_ptr());
return true;
}
bool split_path(const wchar_t* p, dynamic_wstring& path, dynamic_wstring& filename)
{
dynamic_wstring temp_drive, temp_path, temp_ext;
if (!split_path(p, &temp_drive, &temp_path, &filename, &temp_ext))
return false;
filename += temp_ext;
combine_path(path, temp_drive.get_ptr(), temp_path.get_ptr());
return true;
}
bool get_pathname(const char* p, dynamic_string& path)
{
dynamic_string temp_drive, temp_path;
if (!split_path(p, &temp_drive, &temp_path, NULL, NULL))
return false;
combine_path(path, temp_drive.get_ptr(), temp_path.get_ptr());
return true;
}
bool get_pathname(const wchar_t* p, dynamic_wstring& path)
{
dynamic_wstring temp_drive, temp_path;
if (!split_path(p, &temp_drive, &temp_path, NULL, NULL))
return false;
combine_path(path, temp_drive.get_ptr(), temp_path.get_ptr());
return true;
}
bool get_filename(const char* p, dynamic_string& filename)
{
dynamic_string temp_ext;
if (!split_path(p, NULL, NULL, &filename, &temp_ext))
return false;
filename += temp_ext;
return true;
}
bool get_filename(const wchar_t* p, dynamic_wstring& filename)
{
dynamic_wstring temp_ext;
if (!split_path(p, NULL, NULL, &filename, &temp_ext))
return false;
filename += temp_ext;
return true;
}
void combine_path(dynamic_string& dst, const char* pA, const char* pB)
{
dynamic_string temp;
temp = pA;
if ((!temp.is_empty()) && (pB[0] != '\\') && (pB[0] != '/'))
{
char c = temp[temp.get_len() - 1];
if ((c != '\\') && (c != '/'))
{
temp.append_char('\\');
}
}
temp += pB;
dst.swap(temp);
}
void combine_path(dynamic_wstring& dst, const wchar_t* pA, const wchar_t* pB)
{
dynamic_wstring temp;
temp = pA;
if ((!temp.is_empty()) && (pB[0] != L'\\') && (pB[0] != L'/'))
{
wchar_t c = temp[temp.get_len() - 1];
if ((c != L'\\') && (c != L'/'))
{
temp.append_char(L'\\');
}
}
temp += pB;
dst.swap(temp);
}
void combine_path(dynamic_string& dst, const char* pA, const char* pB, const char* pC)
{
combine_path(dst, pA, pB);
combine_path(dst, dst.get_ptr(), pC);
}
void combine_path(dynamic_wstring& dst, const wchar_t* pA, const wchar_t* pB, const wchar_t* pC)
{
combine_path(dst, pA, pB);
combine_path(dst, dst.get_ptr(), pC);
}
void combine_path(dynamic_wstring& dst, const wchar_t* pA, const wchar_t* pB, const wchar_t* pC, const wchar_t *pD)
{
combine_path(dst, pA, pB);
combine_path(dst, dst.get_ptr(), pC);
combine_path(dst, dst.get_ptr(), pD);
}
bool full_path(dynamic_string& path)
{
#ifndef _XBOX
char buf[CRNLIB_MAX_PATH];
char* p = _fullpath(buf, path.get_ptr(), CRNLIB_MAX_PATH);
if (!p)
return false;
path.set(buf);
#endif
return true;
}
bool full_path(dynamic_wstring& path)
{
#ifndef _XBOX
wchar_t buf[CRNLIB_MAX_PATH];
wchar_t* p = _wfullpath(buf, path.get_ptr(), CRNLIB_MAX_PATH);
if (!p)
return false;
path.set(buf);
#endif
return true;
}
bool get_extension(dynamic_string& filename)
{
int sep = filename.find_right('\\');
if (sep < 0)
sep = filename.find_right('/');
int dot = filename.find_right('.');
if (dot < sep)
{
filename.clear();
return false;
}
filename.right(dot + 1);
return true;
}
bool get_extension(dynamic_wstring& filename)
{
int sep = filename.find_right(L'\\');
if (sep < 0)
sep = filename.find_right(L'/');
int dot = filename.find_right(L'.');
if (dot < sep)
{
filename.clear();
return false;
}
filename.right(dot + 1);
return true;
}
bool remove_extension(dynamic_string& filename)
{
int sep = filename.find_right('\\');
if (sep < 0)
sep = filename.find_right('/');
int dot = filename.find_right('.');
if (dot < sep)
return false;
filename.left(dot);
return true;
}
bool remove_extension(dynamic_wstring& filename)
{
int sep = filename.find_right(L'\\');
if (sep < 0)
sep = filename.find_right(L'/');
int dot = filename.find_right(L'.');
if (dot < sep)
return false;
filename.left(dot);
return true;
}
bool create_path(const dynamic_wstring& path)
{
bool unc = false;
dynamic_wstring cur_path;
const int l = path.get_len();
int n = 0;
while (n < l)
{
const wchar_t c = path.get_ptr()[n];
const bool sep = (c == L'/') || (c == L'\\');
if ((sep) || (n == (l - 1)))
{
if ((n == (l - 1)) && (!sep))
cur_path.append_char(c);
bool valid = false;
if ((cur_path.get_len() > 3) && (cur_path.get_ptr()[1] == L':'))
valid = true;
else if (cur_path.get_len() > 2)
{
if (unc)
valid = true;
unc = true;
}
if (valid)
_wmkdir(cur_path.get_ptr());
}
cur_path.append_char(c);
n++;
}
return true;
}
void trim_trailing_seperator(dynamic_wstring& path)
{
if ( (path.get_len()) && ( (path[path.get_len() - 1] == L'\\') || (path[path.get_len() - 1] == L'/') ) )
path.truncate(path.get_len() - 1);
}
} // namespace crnlib
+43 -19
View File
@@ -2,34 +2,58 @@
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#ifdef WIN32
#define CRNLIB_PATH_SEPERATOR_CHAR '\\'
#else
#define CRNLIB_PATH_SEPERATOR_CHAR '/'
#endif
namespace crnlib
{
char* crn_strdup(const char* pStr);
int crn_stricmp(const char *p, const char *q);
char* strcpy_safe(char* pDst, uint dst_len, const char* pSrc);
bool int_to_string(int value, char* pDst, uint len);
bool uint_to_string(uint value, char* pDst, uint len);
bool string_to_int(const char*& pBuf, int& value);
bool string_to_int(const wchar_t*& pBuf, int& value);
bool string_to_uint(const char*& pBuf, uint& value);
bool string_to_uint(const wchar_t*& pBuf, uint& value);
bool string_to_int64(const char*& pBuf, int64& value);
bool string_to_uint64(const char*& pBuf, uint64& value);
bool string_to_bool(const char* p, bool& value);
bool string_to_float(const char*& p, float& value, uint round_digit = 512U);
bool string_to_double(const char*& p, double& value, uint round_digit = 512U);
bool string_to_double(const char*& p, const char *pEnd, double& value, uint round_digit = 512U);
bool string_to_bool(const wchar_t* p, bool& value);
bool string_to_float(const char*& p, float& value, uint round_digit = 10U);
bool string_to_float(const wchar_t*& p, float& value, uint round_digit = 10U);
bool split_path(const char* p, dynamic_string* pDrive, dynamic_string* pDir, dynamic_string* pFilename, dynamic_string* pExt);
bool split_path(const wchar_t* p, dynamic_wstring* pDrive, dynamic_wstring* pDir, dynamic_wstring* pFilename, dynamic_wstring* pExt);
bool split_path(const char* p, dynamic_string& path, dynamic_string& filename);
bool split_path(const wchar_t* p, dynamic_wstring& path, dynamic_wstring& filename);
bool get_pathname(const char* p, dynamic_string& path);
bool get_pathname(const wchar_t* p, dynamic_wstring& path);
bool get_filename(const char* p, dynamic_string& filename);
bool get_filename(const wchar_t* p, dynamic_wstring& filename);
void combine_path(dynamic_string& dst, const char* pA, const char* pB);
void combine_path(dynamic_wstring& dst, const wchar_t* pA, const wchar_t* pB);
void combine_path(dynamic_string& dst, const char* pA, const char* pB, const char* pC);
void combine_path(dynamic_wstring& dst, const wchar_t* pA, const wchar_t* pB, const wchar_t* pC);
void combine_path(dynamic_wstring& dst, const wchar_t* pA, const wchar_t* pB, const wchar_t* pC, const wchar_t *pD);
bool full_path(dynamic_string& path);
bool full_path(dynamic_wstring& path);
bool get_extension(dynamic_string& filename);
bool get_extension(dynamic_wstring& filename);
bool remove_extension(dynamic_string& filename);
bool remove_extension(dynamic_wstring& filename);
bool create_path(const dynamic_wstring& path);
void trim_trailing_seperator(dynamic_wstring& path);
} // namespace crnlib
+8 -8
View File
@@ -361,7 +361,7 @@ namespace crnlib
if (!max_freq)
return false;
if (max_freq <= cUINT16_MAX)
if (max_freq <= UINT16_MAX)
{
for (uint i = 0; i < total_syms; i++)
sym_freq16[i] = static_cast<uint16>(pSym_freq[i]);
@@ -381,7 +381,7 @@ namespace crnlib
if (fl < 1)
fl = 1;
CRNLIB_ASSERT(fl <= cUINT16_MAX);
CRNLIB_ASSERT(fl <= UINT16_MAX);
sym_freq16[i] = static_cast<uint16>(fl);
}
@@ -917,7 +917,7 @@ namespace crnlib
freq++;
model.m_sym_freq[sym] = static_cast<uint16>(freq);
if (freq == cUINT16_MAX)
if (freq == UINT16_MAX)
model.rescale();
if (--model.m_symbols_until_update == 0)
@@ -1426,8 +1426,8 @@ namespace crnlib
{
uint32 t = pTables->m_lookup[m_bit_buf >> (cBitBufSize - pTables->m_table_bits)];
CRNLIB_ASSERT(t != cUINT32_MAX);
sym = t & cUINT16_MAX;
CRNLIB_ASSERT(t != UINT32_MAX);
sym = t & UINT16_MAX;
len = t >> 16;
CRNLIB_ASSERT(model.m_code_sizes[sym] == len);
@@ -1462,7 +1462,7 @@ namespace crnlib
freq++;
model.m_sym_freq[sym] = static_cast<uint16>(freq);
if (freq == cUINT16_MAX)
if (freq == UINT16_MAX)
model.rescale();
if (--model.m_symbols_until_update == 0)
@@ -1614,8 +1614,8 @@ namespace crnlib
{
uint32 t = pTables->m_lookup[m_bit_buf >> (cBitBufSize - pTables->m_table_bits)];
CRNLIB_ASSERT(t != cUINT32_MAX);
sym = t & cUINT16_MAX;
CRNLIB_ASSERT(t != UINT32_MAX);
sym = t & UINT16_MAX;
len = t >> 16;
CRNLIB_ASSERT(model.m_code_sizes[sym] == len);
+243
View File
@@ -0,0 +1,243 @@
// File: crn_task_pool.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_task_pool.h"
#include <process.h>
#include "crn_winhdr.h"
namespace crnlib
{
task_pool::task_pool() :
m_num_threads(0),
m_num_outstanding_tasks(0),
m_exit_flag(false)
{
utils::zero_object(m_threads);
}
task_pool::task_pool(uint num_threads) :
m_num_threads(0),
m_num_outstanding_tasks(0),
m_exit_flag(false)
{
utils::zero_object(m_threads);
bool status = init(num_threads);
CRNLIB_VERIFY(status);
}
task_pool::~task_pool()
{
deinit();
}
bool task_pool::init(uint num_threads)
{
CRNLIB_ASSERT(num_threads <= cMaxThreads);
num_threads = math::minimum<uint>(num_threads, cMaxThreads);
deinit();
m_task_condition_var.lock();
m_num_threads = num_threads;
bool succeeded = true;
for (uint i = 0; i < num_threads; i++)
{
m_threads[i] = (HANDLE)_beginthreadex(NULL, 32768, thread_func, this, 0, NULL);
CRNLIB_ASSERT(m_threads[i] != 0);
if (!m_threads[i])
{
succeeded = false;
break;
}
}
m_task_condition_var.unlock();
if (!succeeded)
{
deinit();
return false;
}
return true;
}
void task_pool::deinit()
{
if (m_num_threads)
{
m_task_condition_var.lock();
m_exit_flag = true;
m_task_condition_var.unlock();
for (uint i = 0; i < m_num_threads; i++)
{
if (m_threads[i])
{
for ( ; ; )
{
uint32 result = WaitForSingleObject(m_threads[i], 1000);
if (result == WAIT_OBJECT_0)
break;
}
CloseHandle(m_threads[i]);
m_threads[i] = NULL;
}
}
m_num_threads = 0;
m_exit_flag = false;
}
m_tasks.clear();
m_num_outstanding_tasks = 0;
}
uint task_pool::get_num_threads() const
{
return m_num_threads;
}
void task_pool::queue_task(task_callback_func pFunc, uint64 data, void* pData_ptr)
{
CRNLIB_ASSERT(pFunc);
m_task_condition_var.lock();
task tsk;
tsk.m_callback = pFunc;
tsk.m_data = data;
tsk.m_pData_ptr = pData_ptr;
tsk.m_flags = 0;
m_tasks.push_back(tsk);
m_num_outstanding_tasks++;
m_task_condition_var.unlock();
}
// It's the object's responsibility to crnlib_delete pObj within the execute_task() method, if needed!
void task_pool::queue_task(executable_task* pObj, uint64 data, void* pData_ptr)
{
CRNLIB_ASSERT(pObj);
m_task_condition_var.lock();
task tsk;
tsk.m_pObj = pObj;
tsk.m_data = data;
tsk.m_pData_ptr = pData_ptr;
tsk.m_flags = cTaskFlagObject;
m_tasks.push_back(tsk);
m_num_outstanding_tasks++;
m_task_condition_var.unlock();
}
bool task_pool::join_condition_func(void* pCallback_data_ptr, uint64 callback_data)
{
callback_data;
task_pool* pPool = static_cast<task_pool*>(pCallback_data_ptr);
return (!pPool->m_num_outstanding_tasks) || pPool->m_exit_flag;
}
void task_pool::process_task(task& tsk)
{
if (tsk.m_flags & cTaskFlagObject)
tsk.m_pObj->execute_task(tsk.m_data, tsk.m_pData_ptr);
else
tsk.m_callback(tsk.m_data, tsk.m_pData_ptr);
m_task_condition_var.lock();
m_num_outstanding_tasks--;
m_task_condition_var.unlock();
}
void task_pool::join()
{
for ( ; ; )
{
m_task_condition_var.lock();
if (!m_tasks.empty())
{
task tsk(m_tasks.front());
m_tasks.pop_front();
m_task_condition_var.unlock();
process_task(tsk);
}
else
{
int result = m_task_condition_var.wait(join_condition_func, this);
result;
CRNLIB_ASSERT(result >= 0);
m_task_condition_var.unlock();
break;
}
}
}
bool task_pool::wait_condition_func(void* pCallback_data_ptr, uint64 callback_data)
{
callback_data;
task_pool* pPool = static_cast<task_pool*>(pCallback_data_ptr);
return (!pPool->m_tasks.empty()) || pPool->m_exit_flag;
}
unsigned __stdcall task_pool::thread_func(void* pContext)
{
//set_thread_name(GetCurrentThreadId(), "taskpoolhelper");
task_pool* pPool = static_cast<task_pool*>(pContext);
for ( ; ; )
{
pPool->m_task_condition_var.lock();
int result = pPool->m_task_condition_var.wait(wait_condition_func, pPool);
CRNLIB_ASSERT(result >= 0);
if ((result < 0) || (pPool->m_exit_flag))
{
pPool->m_task_condition_var.unlock();
break;
}
if (pPool->m_tasks.empty())
pPool->m_task_condition_var.unlock();
else
{
task tsk(pPool->m_tasks.front());
pPool->m_tasks.pop_front();
pPool->m_task_condition_var.unlock();
pPool->process_task(tsk);
}
}
_endthreadex(0);
return 0;
}
} // namespace crnlib
+140
View File
@@ -0,0 +1,140 @@
// File: crn_task_pool.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
#include "crn_condition_var.h"
#include <deque>
namespace crnlib
{
class task_pool
{
public:
task_pool();
task_pool(uint num_threads);
~task_pool();
enum { cMaxThreads = 16 };
bool init(uint num_threads);
void deinit();
uint get_num_threads() const;
// C-style task callback
typedef void (*task_callback_func)(uint64 data, void* pData_ptr);
void queue_task(task_callback_func pFunc, uint64 data = 0, void* pData_ptr = NULL);
class executable_task
{
public:
virtual void execute_task(uint64 data, void* pData_ptr) = 0;
};
// It's the caller's responsibility to crnlib_delete pObj within the execute_task() method, if needed!
void queue_task(executable_task* pObj, uint64 data = 0, void* pData_ptr = NULL);
template<typename S, typename T>
inline void queue_object_task(S* pObject, T pObject_method, uint64 data = 0, void* pData_ptr = NULL);
void join();
private:
uint m_num_threads;
uint m_num_outstanding_tasks;
void* m_threads[cMaxThreads];
bool m_exit_flag;
condition_var m_task_condition_var;
enum task_flags
{
cTaskFlagObject = 1
};
struct task
{
uint64 m_data;
void* m_pData_ptr;
union
{
task_callback_func m_callback;
executable_task* m_pObj;
};
uint m_flags;
};
std::deque<task> m_tasks;
void process_task(task& tsk);
static bool join_condition_func(void* pCallback_data_ptr, uint64 callback_data);
static bool wait_condition_func(void* pCallback_data_ptr, uint64 callback_data);
static unsigned __stdcall thread_func(void* pContext);
};
enum object_task_flags
{
cObjectTaskFlagDefault = 0,
cObjectTaskFlagDeleteAfterExecution = 1
};
template<typename T>
class object_task : public task_pool::executable_task
{
public:
object_task(uint flags = cObjectTaskFlagDefault) :
m_pObject(NULL),
m_pMethod(NULL),
m_flags(flags)
{
}
typedef void (T::*object_method_ptr)(uint64 data, void* pData_ptr);
object_task(T* pObject, object_method_ptr pMethod, uint flags = cObjectTaskFlagDefault) :
m_pObject(pObject),
m_pMethod(pMethod),
m_flags(flags)
{
CRNLIB_ASSERT(pObject && pMethod);
}
void init(T* pObject, object_method_ptr pMethod, uint flags = cObjectTaskFlagDefault)
{
CRNLIB_ASSERT(pObject && pMethod);
m_pObject = pObject;
m_pMethod = pMethod;
m_flags = flags;
}
T* get_object() const { return m_pObject; }
object_method_ptr get_method() const { return m_pMethod; }
virtual void execute_task(uint64 data, void* pData_ptr)
{
(m_pObject->*m_pMethod)(data, pData_ptr);
if (m_flags & cObjectTaskFlagDeleteAfterExecution)
crnlib_delete(this);
}
protected:
T* m_pObject;
object_method_ptr m_pMethod;
uint m_flags;
};
template<typename S, typename T>
inline void task_pool::queue_object_task(S* pObject, T pObject_method, uint64 data, void* pData_ptr)
{
queue_task(crnlib_new< object_task<S> >(pObject, pObject_method, cObjectTaskFlagDeleteAfterExecution), data, pData_ptr);
}
} // namespace crnlib
+23 -35
View File
@@ -26,7 +26,7 @@ namespace crnlib
{
if (local_params.get_flag(cCRNCompFlagPerceptual))
{
console::info("Output pixel format is swizzled or not RGB, disabling perceptual color metrics");
//console::warning(L"Output pixel format is swizzled or not RGB, disabling perceptual color metrics");
// Destination compressed pixel format is swizzled or not RGB at all, so be sure perceptual colorspace metrics are disabled.
local_params.set_flag(cCRNCompFlagPerceptual, false);
@@ -53,18 +53,6 @@ namespace crnlib
((local_params.m_file_type == cCRNFileTypeCRN) && ((local_params.m_flags & cCRNCompFlagManualPaletteSizes) != 0))
)
{
if ( (local_params.m_file_type == cCRNFileTypeCRN) ||
((local_params.m_file_type == cCRNFileTypeDDS) && (local_params.m_quality_level < cCRNMaxQualityLevel)) )
{
console::info("Compressing using quality level %i", local_params.m_quality_level);
}
if (local_params.m_format == cCRNFmtDXT3)
{
if (local_params.m_file_type == cCRNFileTypeCRN)
console::warning("CRN format doesn't support DXT3");
else if ((local_params.m_file_type == cCRNFileTypeDDS) && (local_params.m_quality_level < cCRNMaxQualityLevel))
console::warning("Clustered DDS compressor doesn't support DXT3");
}
if (!pTexture_comp->compress_pass(local_params, pActual_bitrate))
{
crnlib_delete(pTexture_comp);
@@ -107,7 +95,7 @@ namespace crnlib
{
if (params.m_flags & cCRNCompFlagDebugging)
{
console::debug("Quality level bracket: [%u, %u]", low_quality, high_quality);
console::debug(L"Quality level bracket: [%u, %u]", low_quality, high_quality);
}
int trial_quality = (low_quality + high_quality) / 2;
@@ -149,7 +137,7 @@ namespace crnlib
}
}
console::info("Compressing to quality level %u", trial_quality);
console::info(L"Compressing to quality level %u", trial_quality);
float bitrate = 0.0f;
@@ -165,7 +153,7 @@ namespace crnlib
highest_bitrate = math::maximum(highest_bitrate, bitrate);
console::info("\nTried quality level %u, bpp: %3.3f", trial_quality, bitrate);
console::info(L"\nTried quality level %u, bpp: %3.3f", trial_quality, bitrate);
if ( (best_quality_level < 0) ||
((bitrate <= local_params.m_target_bitrate) && (best_bitrate > local_params.m_target_bitrate)) ||
@@ -177,7 +165,7 @@ namespace crnlib
best_quality_level = trial_quality;
if (params.m_flags & cCRNCompFlagDebugging)
{
console::debug("Choose new best quality level");
console::debug(L"Choose new best quality level");
}
if ((best_bitrate <= local_params.m_target_bitrate) && (fabs(best_bitrate - local_params.m_target_bitrate) < .005f))
@@ -200,7 +188,7 @@ namespace crnlib
(highest_bitrate < local_params.m_target_bitrate) &&
(fabs(best_bitrate - local_params.m_target_bitrate) >= .005f))
{
console::info("Unable to achieve desired bitrate - disabling adaptive block sizes and retrying search.");
console::info(L"Unable to achieve desired bitrate - disabling adaptive block sizes and retrying search.");
local_params.m_flags &= ~cCRNCompFlagHierarchical;
@@ -226,12 +214,12 @@ namespace crnlib
if (pActual_quality_level) *pActual_quality_level = best_quality_level;
if (pActual_bitrate) *pActual_bitrate = best_bitrate;
console::printf("Selected quality level %u bpp: %f", best_quality_level, best_bitrate);
console::printf(L"Selected quality level %u bpp: %f", best_quality_level, best_bitrate);
return true;
}
static bool create_dds_tex(const crn_comp_params &params, mipmapped_texture &dds_tex)
static bool create_dds_tex(const crn_comp_params &params, dds_texture &dds_tex)
{
image_u8 images[cCRNMaxFaces][cCRNMaxLevels];
@@ -281,7 +269,7 @@ namespace crnlib
return true;
}
bool create_texture_mipmaps(mipmapped_texture &work_tex, const crn_comp_params &params, const crn_mipmap_params &mipmap_params, bool generate_mipmaps)
bool create_texture_mipmaps(dds_texture &work_tex, const crn_comp_params &params, const crn_mipmap_params &mipmap_params, bool generate_mipmaps)
{
crn_comp_params new_params(params);
@@ -322,14 +310,14 @@ namespace crnlib
{
if (work_tex.get_num_faces() > 1)
{
console::warning("Can't crop cubemap textures");
console::warning(L"Can't crop cubemap textures");
}
else
{
console::info("Cropping input texture from window (%ux%u)-(%ux%u)", window_rect.get_left(), window_rect.get_top(), window_rect.get_right(), window_rect.get_bottom());
console::info(L"Cropping input texture from window (%ux%u)-(%ux%u)", window_rect.get_left(), window_rect.get_top(), window_rect.get_right(), window_rect.get_bottom());
if (!work_tex.crop(window_rect.get_left(), window_rect.get_top(), window_rect.get_width(), window_rect.get_height()))
console::warning("Failed cropping window rect");
console::warning(L"Failed cropping window rect");
}
}
@@ -344,13 +332,13 @@ namespace crnlib
{
if (work_tex.get_num_faces() > 1)
{
console::warning("Can't crop cubemap textures");
console::warning(L"Can't crop cubemap textures");
}
else
{
new_width = math::minimum<uint>(mipmap_params.m_clamp_width, new_width);
new_height = math::minimum<uint>(mipmap_params.m_clamp_height, new_height);
console::info("Clamping input texture to %ux%u", new_width, new_height);
console::info(L"Clamping input texture to %ux%u", new_width, new_height);
work_tex.crop(0, 0, new_width, new_height);
}
}
@@ -432,13 +420,13 @@ namespace crnlib
if ((new_width != (int)work_tex.get_width()) || (new_height != (int)work_tex.get_height()))
{
console::info("Resampling input texture to %ux%u", new_width, new_height);
console::info(L"Resampling input texture to %ux%u", new_width, new_height);
const char* pFilter = crn_get_mip_filter_name(mipmap_params.m_filter);
bool srgb = mipmap_params.m_gamma_filtering != 0;
mipmapped_texture::resample_params res_params;
dds_texture::resample_params res_params;
res_params.m_pFilter = pFilter;
res_params.m_wrapping = mipmap_params.m_tiled != 0;
if (work_tex.get_num_faces())
@@ -451,7 +439,7 @@ namespace crnlib
if (!work_tex.resize(new_width, new_height, res_params))
{
console::error("Failed resizing texture!");
console::error(L"Failed resizing texture!");
return false;
}
}
@@ -462,7 +450,7 @@ namespace crnlib
const char* pFilter = crn_get_mip_filter_name(mipmap_params.m_filter);
mipmapped_texture::generate_mipmap_params gen_params;
dds_texture::generate_mipmap_params gen_params;
gen_params.m_pFilter = pFilter;
gen_params.m_wrapping = mipmap_params.m_tiled != 0;
gen_params.m_renormalize = mipmap_params.m_renormalize != 0;
@@ -473,18 +461,18 @@ namespace crnlib
gen_params.m_max_mips = mipmap_params.m_max_levels;
gen_params.m_min_mip_size = mipmap_params.m_min_mip_size;
console::info("Generating mipmaps using filter \"%s\"", pFilter);
console::info(L"Generating mipmaps using filter \"%S\"", pFilter);
timer tm;
tm.start();
if (!work_tex.generate_mipmaps(gen_params, true))
{
console::error("Failed generating mipmaps!");
console::error(L"Failed generating mipmaps!");
return false;
}
double t = tm.get_elapsed_secs();
console::info("Generated %u mipmap levels in %3.3fs", work_tex.get_num_levels() - 1, t);
console::info(L"Generated %u mipmap levels in %3.3fs", work_tex.get_num_levels() - 1, t);
}
return true;
@@ -496,10 +484,10 @@ namespace crnlib
if (pActual_bitrate) *pActual_bitrate = 0.0f;
if (pActual_quality_level) *pActual_quality_level = 0;
mipmapped_texture work_tex;
dds_texture work_tex;
if (!create_dds_tex(params, work_tex))
{
console::error("Failed creating DDS texture from crn_comp_params!");
console::error(L"Failed creating DDS texture from crn_comp_params!");
return false;
}

Some files were not shown because too many files have changed in this diff Show More