| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#ifndef CRND_INCLUDE_CRND_H |
| 13 |
#define CRND_INCLUDE_CRND_H |
| 14 |
|
| 15 |
|
| 16 |
#include "crn_defs.h" |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdint.h> |
| 21 |
#ifdef WIN32 |
| 22 |
#include <memory.h> |
| 23 |
#elif defined(__FreeBSD__) |
| 24 |
|
| 25 |
#include <malloc_np.h> |
| 26 |
#elif defined(__APPLE__) |
| 27 |
#include <malloc/malloc.h> |
| 28 |
#else |
| 29 |
#include <malloc.h> |
| 30 |
#endif |
| 31 |
#include <stdarg.h> |
| 32 |
#include <new> |
| 33 |
|
| 34 |
#define CRND_RESTRICT __restrict |
| 35 |
|
| 36 |
#ifdef _MSC_VER |
| 37 |
#pragma warning(disable : 4127) // warning C4127: conditional expression is constant |
| 38 |
#endif |
| 39 |
|
| 40 |
#ifdef CRND_DEVEL |
| 41 |
#ifndef _WIN32_WINNT |
| 42 |
#define _WIN32_WINNT 0x500 |
| 43 |
#endif |
| 44 |
#ifndef WIN32_LEAN_AND_MEAN |
| 45 |
#define WIN32_LEAN_AND_MEAN |
| 46 |
#endif |
| 47 |
#ifndef |
| 48 |
#define NOMINMAX |
| 49 |
#endif |
| 50 |
#include "windows.h" |
| 51 |
#endif |
| 52 |
|
| 53 |
|
| 54 |
namespace crnd { |
| 55 |
const crn_uint8 cUINT8_MIN = 0; |
| 56 |
const crn_uint8 cUINT8_MAX = 0xFFU; |
| 57 |
const uint16 cUINT16_MIN = 0; |
| 58 |
const uint16 cUINT16_MAX = 0xFFFFU; |
| 59 |
const uint32 cUINT32_MIN = 0; |
| 60 |
const uint32 cUINT32_MAX = 0xFFFFFFFFU; |
| 61 |
|
| 62 |
const int8 cINT8_MIN = -128; |
| 63 |
const int8 cINT8_MAX = 127; |
| 64 |
const int16 cINT16_MIN = -32768; |
| 65 |
const int16 cINT16_MAX = 32767; |
| 66 |
const int32 cINT32_MIN = (-2147483647 - 1); |
| 67 |
const int32 cINT32_MAX = 2147483647; |
| 68 |
|
| 69 |
enum eClear { cClear }; |
| 70 |
|
| 71 |
const uint32 cIntBits = 32U; |
| 72 |
|
| 73 |
template <typename T> |
| 74 |
struct int_traits { |
| 75 |
enum { cMin = crnd::cINT32_MIN, |
| 76 |
cMax = crnd::cINT32_MAX, |
| 77 |
cSigned = true }; |
| 78 |
}; |
| 79 |
|
| 80 |
template <> |
| 81 |
struct int_traits<int8> { |
| 82 |
enum { cMin = crnd::cINT8_MIN, |
| 83 |
cMax = crnd::cINT8_MAX, |
| 84 |
cSigned = true }; |
| 85 |
}; |
| 86 |
template <> |
| 87 |
struct int_traits<int16> { |
| 88 |
enum { cMin = crnd::cINT16_MIN, |
| 89 |
cMax = crnd::cINT16_MAX, |
| 90 |
cSigned = true }; |
| 91 |
}; |
| 92 |
template <> |
| 93 |
struct int_traits<int32> { |
| 94 |
enum { cMin = crnd::cINT32_MIN, |
| 95 |
cMax = crnd::cINT32_MAX, |
| 96 |
cSigned = true }; |
| 97 |
}; |
| 98 |
|
| 99 |
template <> |
| 100 |
struct int_traits<uint8> { |
| 101 |
enum { cMin = 0, |
| 102 |
cMax = crnd::cUINT8_MAX, |
| 103 |
cSigned = false }; |
| 104 |
}; |
| 105 |
template <> |
| 106 |
struct int_traits<uint16> { |
| 107 |
enum { cMin = 0, |
| 108 |
cMax = crnd::cUINT16_MAX, |
| 109 |
cSigned = false }; |
| 110 |
}; |
| 111 |
template <> |
| 112 |
struct int_traits<uint32> { |
| 113 |
enum { cMin = 0, |
| 114 |
cMax = crnd::cUINT32_MAX, |
| 115 |
cSigned = false }; |
| 116 |
}; |
| 117 |
|
| 118 |
struct empty_type {}; |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
namespace crnd { |
| 124 |
|
| 125 |
bool crnd_is_debugger_present(); |
| 126 |
void crnd_debug_break(); |
| 127 |
void crnd_output_debug_string(const char* p); |
| 128 |
|
| 129 |
|
| 130 |
void crnd_assert(const char* pExp, const char* pFile, unsigned line); |
| 131 |
void crnd_fail(const char* pExp, const char* pFile, unsigned line); |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
namespace crnd { |
| 137 |
void crnd_assert(const char* pExp, const char* pFile, unsigned line); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
#ifndef CRND_ASSERT |
| 142 |
#ifdef NDEBUG |
| 143 |
#define CRND_ASSERT(x) ((void)0) |
| 144 |
#else |
| 145 |
#define CRND_ASSERT(_exp) (void)((!!(_exp)) || (crnd::crnd_assert(#_exp, __FILE__, __LINE__), 0)) |
| 146 |
#endif |
| 147 |
#endif |
| 148 |
|
| 149 |
void crnd_trace(const char* pFmt, va_list args); |
| 150 |
void crnd_trace(const char* pFmt, ...); |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
namespace crnd { |
| 156 |
namespace helpers { |
| 157 |
template <typename T> |
| 158 |
struct rel_ops { |
| 159 |
friend bool operator!=(const T& x, const T& y) { return (!(x == y)); } |
| 160 |
friend bool operator>(const T& x, const T& y) { return (y < x); } |
| 161 |
friend bool operator<=(const T& x, const T& y) { return (!(y < x)); } |
| 162 |
friend bool operator>=(const T& x, const T& y) { return (!(x < y)); } |
| 163 |
}; |
| 164 |
|
| 165 |
template <typename T> |
| 166 |
inline T* construct(T* p) { |
| 167 |
return new (static_cast<void*>(p)) T; |
| 168 |
} |
| 169 |
|
| 170 |
template <typename T, typename U> |
| 171 |
inline T* construct(T* p, const U& init) { |
| 172 |
return new (static_cast<void*>(p)) T(init); |
| 173 |
} |
| 174 |
|
| 175 |
template <typename T> |
| 176 |
void construct_array(T* p, uint32 n) { |
| 177 |
T* q = p + n; |
| 178 |
for (; p != q; ++p) |
| 179 |
new (static_cast<void*>(p)) T; |
| 180 |
} |
| 181 |
|
| 182 |
template <typename T, typename U> |
| 183 |
void construct_array(T* p, uint32 n, const U& init) { |
| 184 |
T* q = p + n; |
| 185 |
for (; p != q; ++p) |
| 186 |
new (static_cast<void*>(p)) T(init); |
| 187 |
} |
| 188 |
|
| 189 |
template <typename T> |
| 190 |
inline void destruct(T* p) { |
| 191 |
p->~T(); |
| 192 |
} |
| 193 |
|
| 194 |
template <typename T> |
| 195 |
inline void destruct_array(T* p, uint32 n) { |
| 196 |
T* q = p + n; |
| 197 |
for (; p != q; ++p) |
| 198 |
p->~T(); |
| 199 |
} |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
namespace crnd { |
| 207 |
template <typename T> |
| 208 |
struct scalar_type { |
| 209 |
enum { cFlag = false }; |
| 210 |
static inline void construct(T* p) { helpers::construct(p); } |
| 211 |
static inline void construct(T* p, const T& init) { helpers::construct(p, init); } |
| 212 |
static inline void construct_array(T* p, uint32 n) { helpers::construct_array(p, n); } |
| 213 |
static inline void destruct(T* p) { helpers::destruct(p); } |
| 214 |
static inline void destruct_array(T* p, uint32 n) { helpers::destruct_array(p, n); } |
| 215 |
}; |
| 216 |
|
| 217 |
template <typename T> |
| 218 |
struct scalar_type<T*> { |
| 219 |
enum { cFlag = true }; |
| 220 |
static inline void construct(T** p) { memset(p, 0, sizeof(T*)); } |
| 221 |
static inline void construct(T** p, T* init) { *p = init; } |
| 222 |
static inline void construct_array(T** p, uint32 n) { memset(p, 0, sizeof(T*) * n); } |
| 223 |
static inline void destruct(T**) {} |
| 224 |
static inline void destruct_array(T**, uint32) {} |
| 225 |
}; |
| 226 |
|
| 227 |
#define CRND_DEFINE_BUILT_IN_TYPE(X) \ |
| 228 |
template <> \ |
| 229 |
struct scalar_type<X> { \ |
| 230 |
enum { cFlag = true }; \ |
| 231 |
static inline void construct(X* p) { memset(p, 0, sizeof(X)); } \ |
| 232 |
static inline void construct(X* p, const X& init) { memcpy(p, &init, sizeof(X)); } \ |
| 233 |
static inline void construct_array(X* p, uint32 n) { memset(p, 0, sizeof(X) * n); } \ |
| 234 |
static inline void destruct(X*) {} \ |
| 235 |
static inline void destruct_array(X*, uint32) {} \ |
| 236 |
}; |
| 237 |
|
| 238 |
CRND_DEFINE_BUILT_IN_TYPE(bool) |
| 239 |
CRND_DEFINE_BUILT_IN_TYPE(char) |
| 240 |
CRND_DEFINE_BUILT_IN_TYPE(unsigned char) |
| 241 |
CRND_DEFINE_BUILT_IN_TYPE(short) |
| 242 |
CRND_DEFINE_BUILT_IN_TYPE(unsigned short) |
| 243 |
CRND_DEFINE_BUILT_IN_TYPE(int) |
| 244 |
CRND_DEFINE_BUILT_IN_TYPE(unsigned int) |
| 245 |
CRND_DEFINE_BUILT_IN_TYPE(long) |
| 246 |
CRND_DEFINE_BUILT_IN_TYPE(unsigned long) |
| 247 |
CRND_DEFINE_BUILT_IN_TYPE(int64) |
| 248 |
CRND_DEFINE_BUILT_IN_TYPE(uint64) |
| 249 |
CRND_DEFINE_BUILT_IN_TYPE(float) |
| 250 |
CRND_DEFINE_BUILT_IN_TYPE(double) |
| 251 |
CRND_DEFINE_BUILT_IN_TYPE(long double) |
| 252 |
|
| 253 |
#undef CRND_DEFINE_BUILT_IN_TYPE |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
template <typename T> |
| 258 |
struct bitwise_movable { |
| 259 |
enum { cFlag = false }; |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
#define CRND_DEFINE_BITWISE_MOVABLE(Q) \ |
| 264 |
template <> \ |
| 265 |
struct bitwise_movable<Q> { \ |
| 266 |
enum { cFlag = true }; \ |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
typedef char (&yes_t)[1]; |
| 274 |
typedef char (&no_t)[2]; |
| 275 |
|
| 276 |
template <class U> |
| 277 |
yes_t class_test(int U::*); |
| 278 |
template <class U> |
| 279 |
no_t class_test(...); |
| 280 |
|
| 281 |
template <class T> |
| 282 |
struct is_class { |
| 283 |
enum { value = (sizeof(class_test<T>(0)) == sizeof(yes_t)) }; |
| 284 |
}; |
| 285 |
|
| 286 |
template <typename T> |
| 287 |
struct is_pointer { |
| 288 |
enum { value = false }; |
| 289 |
}; |
| 290 |
|
| 291 |
template <typename T> |
| 292 |
struct is_pointer<T*> { |
| 293 |
enum { value = true }; |
| 294 |
}; |
| 295 |
|
| 296 |
#define CRND_IS_POD(T) __is_pod(T) |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
namespace crnd { |
| 302 |
void* crnd_malloc(size_t size, size_t* pActual_size = NULL); |
| 303 |
void* crnd_realloc(void* p, size_t size, size_t* pActual_size = NULL, bool movable = true); |
| 304 |
void crnd_free(void* p); |
| 305 |
size_t crnd_msize(void* p); |
| 306 |
|
| 307 |
template <typename T> |
| 308 |
inline T* crnd_new() { |
| 309 |
T* p = static_cast<T*>(crnd_malloc(sizeof(T))); |
| 310 |
if (!p) |
| 311 |
return NULL; |
| 312 |
|
| 313 |
return helpers::construct(p); |
| 314 |
} |
| 315 |
|
| 316 |
template <typename T> |
| 317 |
inline T* crnd_new(const T& init) { |
| 318 |
T* p = static_cast<T*>(crnd_malloc(sizeof(T))); |
| 319 |
if (!p) |
| 320 |
return NULL; |
| 321 |
|
| 322 |
return helpers::construct(p, init); |
| 323 |
} |
| 324 |
|
| 325 |
template <typename T> |
| 326 |
inline T* crnd_new_array(uint32 num) { |
| 327 |
if (!num) |
| 328 |
num = 1; |
| 329 |
|
| 330 |
uint8* q = static_cast<uint8*>(crnd_malloc(CRND_MIN_ALLOC_ALIGNMENT + sizeof(T) * num)); |
| 331 |
if (!q) |
| 332 |
return NULL; |
| 333 |
|
| 334 |
T* p = reinterpret_cast<T*>(q + CRND_MIN_ALLOC_ALIGNMENT); |
| 335 |
|
| 336 |
reinterpret_cast<uint32*>(p)[-1] = num; |
| 337 |
reinterpret_cast<uint32*>(p)[-2] = ~num; |
| 338 |
|
| 339 |
helpers::construct_array(p, num); |
| 340 |
return p; |
| 341 |
} |
| 342 |
|
| 343 |
template <typename T> |
| 344 |
inline void crnd_delete(T* p) { |
| 345 |
if (p) { |
| 346 |
helpers::destruct(p); |
| 347 |
crnd_free(p); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
template <typename T> |
| 352 |
inline void crnd_delete_array(T* p) { |
| 353 |
if (p) { |
| 354 |
const uint32 num = reinterpret_cast<uint32*>(p)[-1]; |
| 355 |
CRND_ASSERT(num && (num == ~reinterpret_cast<uint32*>(p)[-2])); |
| 356 |
|
| 357 |
helpers::destruct_array(p, num); |
| 358 |
|
| 359 |
crnd_free(reinterpret_cast<uint8*>(p) - CRND_MIN_ALLOC_ALIGNMENT); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
namespace crnd { |
| 367 |
namespace math { |
| 368 |
const float cNearlyInfinite = 1.0e+37f; |
| 369 |
|
| 370 |
const float cDegToRad = 0.01745329252f; |
| 371 |
const float cRadToDeg = 57.29577951f; |
| 372 |
|
| 373 |
extern uint32 g_bitmasks[32]; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
template <typename T> |
| 379 |
inline T minimum(T a, T b) { |
| 380 |
return (a < b) ? a : b; |
| 381 |
} |
| 382 |
|
| 383 |
template <typename T> |
| 384 |
inline T minimum(T a, T b, T c) { |
| 385 |
return minimum(minimum(a, b), c); |
| 386 |
} |
| 387 |
|
| 388 |
template <typename T> |
| 389 |
inline T maximum(T a, T b) { |
| 390 |
return (a > b) ? a : b; |
| 391 |
} |
| 392 |
|
| 393 |
template <typename T> |
| 394 |
inline T maximum(T a, T b, T c) { |
| 395 |
return maximum(maximum(a, b), c); |
| 396 |
} |
| 397 |
|
| 398 |
template <typename T> |
| 399 |
inline T clamp(T value, T low, T high) { |
| 400 |
return (value < low) ? low : ((value > high) ? high : value); |
| 401 |
} |
| 402 |
|
| 403 |
template <typename T> |
| 404 |
inline T square(T value) { |
| 405 |
return value * value; |
| 406 |
} |
| 407 |
|
| 408 |
inline bool is_power_of_2(uint32 x) { |
| 409 |
return x && ((x & (x - 1U)) == 0U); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
inline int next_pow2(uint32 val) { |
| 414 |
val--; |
| 415 |
val |= val >> 16; |
| 416 |
val |= val >> 8; |
| 417 |
val |= val >> 4; |
| 418 |
val |= val >> 2; |
| 419 |
val |= val >> 1; |
| 420 |
return val + 1; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
inline uint32 total_bits(uint32 v) { |
| 425 |
uint32 l = 0; |
| 426 |
while (v > 0U) { |
| 427 |
v >>= 1; |
| 428 |
l++; |
| 429 |
} |
| 430 |
return l; |
| 431 |
} |
| 432 |
|
| 433 |
inline uint floor_log2i(uint v) { |
| 434 |
uint l = 0; |
| 435 |
while (v > 1U) { |
| 436 |
v >>= 1; |
| 437 |
l++; |
| 438 |
} |
| 439 |
return l; |
| 440 |
} |
| 441 |
|
| 442 |
inline uint ceil_log2i(uint v) { |
| 443 |
uint l = floor_log2i(v); |
| 444 |
if ((l != cIntBits) && (v > (1U << l))) |
| 445 |
l++; |
| 446 |
return l; |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
namespace crnd { |
| 453 |
namespace utils { |
| 454 |
template <typename T> |
| 455 |
inline void zero_object(T& obj) { |
| 456 |
memset(&obj, 0, sizeof(obj)); |
| 457 |
} |
| 458 |
|
| 459 |
template <typename T> |
| 460 |
inline void zero_this(T* pObj) { |
| 461 |
memset(pObj, 0, sizeof(*pObj)); |
| 462 |
} |
| 463 |
|
| 464 |
template <typename T> |
| 465 |
inline void swap(T& left, T& right) { |
| 466 |
T temp(left); |
| 467 |
left = right; |
| 468 |
right = temp; |
| 469 |
} |
| 470 |
|
| 471 |
inline void invert_buf(void* pBuf, uint32 size) { |
| 472 |
uint8* p = static_cast<uint8*>(pBuf); |
| 473 |
|
| 474 |
const uint32 half_size = size >> 1; |
| 475 |
for (uint32 i = 0; i < half_size; i++) |
| 476 |
swap(p[i], p[size - 1U - i]); |
| 477 |
} |
| 478 |
|
| 479 |
static inline uint16 swap16(uint16 x) { |
| 480 |
return static_cast<uint16>((x << 8) | (x >> 8)); |
| 481 |
} |
| 482 |
static inline uint32 swap32(uint32 x) { |
| 483 |
return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24)); |
| 484 |
} |
| 485 |
|
| 486 |
uint32 compute_max_mips(uint32 width, uint32 height); |
| 487 |
|
| 488 |
} |
| 489 |
|
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
namespace crnd { |
| 494 |
struct elemental_vector { |
| 495 |
void* m_p; |
| 496 |
uint32 m_size; |
| 497 |
uint32 m_capacity; |
| 498 |
|
| 499 |
typedef void (*object_mover)(void* pDst, void* pSrc, uint32 num); |
| 500 |
|
| 501 |
bool increase_capacity(uint32 min_new_capacity, bool grow_hint, uint32 element_size, object_mover pRelocate); |
| 502 |
}; |
| 503 |
|
| 504 |
#ifdef _MSC_VER |
| 505 |
#pragma warning(push) |
| 506 |
#pragma warning(disable : 4127) // warning C4127: conditional expression is constant |
| 507 |
#endif |
| 508 |
|
| 509 |
template <typename T> |
| 510 |
class vector : public helpers::rel_ops<vector<T> > { |
| 511 |
public: |
| 512 |
typedef T* iterator; |
| 513 |
typedef const T* const_iterator; |
| 514 |
typedef T value_type; |
| 515 |
typedef T& reference; |
| 516 |
typedef const T& const_reference; |
| 517 |
typedef T* pointer; |
| 518 |
typedef const T* const_pointer; |
| 519 |
|
| 520 |
inline vector() |
| 521 |
: m_p(NULL), |
| 522 |
m_size(0), |
| 523 |
m_capacity(0), |
| 524 |
m_alloc_failed(false) { |
| 525 |
} |
| 526 |
|
| 527 |
inline vector(const vector& other) |
| 528 |
: m_p(NULL), |
| 529 |
m_size(0), |
| 530 |
m_capacity(0), |
| 531 |
m_alloc_failed(false) { |
| 532 |
*this = other; |
| 533 |
} |
| 534 |
|
| 535 |
inline vector(uint32 size) |
| 536 |
: m_p(NULL), |
| 537 |
m_size(0), |
| 538 |
m_capacity(0), |
| 539 |
m_alloc_failed(false) { |
| 540 |
resize(size); |
| 541 |
} |
| 542 |
|
| 543 |
inline ~vector() { |
| 544 |
clear(); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
inline bool get_alloc_failed() const { return m_alloc_failed; } |
| 549 |
inline void clear_alloc_failed() { m_alloc_failed = false; } |
| 550 |
|
| 551 |
inline bool assign(const vector& other) { |
| 552 |
if (this == &other) |
| 553 |
return true; |
| 554 |
|
| 555 |
if (m_capacity == other.m_size) |
| 556 |
resize(0); |
| 557 |
else { |
| 558 |
clear(); |
| 559 |
|
| 560 |
if (!increase_capacity(other.m_size, false)) |
| 561 |
return false; |
| 562 |
} |
| 563 |
|
| 564 |
if (scalar_type<T>::cFlag) |
| 565 |
memcpy(m_p, other.m_p, other.m_size * sizeof(T)); |
| 566 |
else { |
| 567 |
T* pDst = m_p; |
| 568 |
const T* pSrc = other.m_p; |
| 569 |
for (uint32 i = other.m_size; i > 0; i--) |
| 570 |
helpers::construct(pDst++, *pSrc++); |
| 571 |
} |
| 572 |
|
| 573 |
m_size = other.m_size; |
| 574 |
|
| 575 |
return true; |
| 576 |
} |
| 577 |
|
| 578 |
inline vector& operator=(const vector& other) { |
| 579 |
assign(other); |
| 580 |
return *this; |
| 581 |
} |
| 582 |
|
| 583 |
inline const T* begin() const { return m_p; } |
| 584 |
T* begin() { return m_p; } |
| 585 |
|
| 586 |
inline const T* end() const { return m_p + m_size; } |
| 587 |
T* end() { return m_p + m_size; } |
| 588 |
|
| 589 |
inline bool empty() const { return !m_size; } |
| 590 |
inline uint32 size() const { return m_size; } |
| 591 |
inline uint32 capacity() const { return m_capacity; } |
| 592 |
|
| 593 |
inline const T& operator[](uint32 i) const { |
| 594 |
CRND_ASSERT(i < m_size); |
| 595 |
return m_p[i]; |
| 596 |
} |
| 597 |
inline T& operator[](uint32 i) { |
| 598 |
CRND_ASSERT(i < m_size); |
| 599 |
return m_p[i]; |
| 600 |
} |
| 601 |
|
| 602 |
inline const T& front() const { |
| 603 |
CRND_ASSERT(m_size); |
| 604 |
return m_p[0]; |
| 605 |
} |
| 606 |
inline T& front() { |
| 607 |
CRND_ASSERT(m_size); |
| 608 |
return m_p[0]; |
| 609 |
} |
| 610 |
|
| 611 |
inline const T& back() const { |
| 612 |
CRND_ASSERT(m_size); |
| 613 |
return m_p[m_size - 1]; |
| 614 |
} |
| 615 |
inline T& back() { |
| 616 |
CRND_ASSERT(m_size); |
| 617 |
return m_p[m_size - 1]; |
| 618 |
} |
| 619 |
|
| 620 |
inline void clear() { |
| 621 |
if (m_p) { |
| 622 |
scalar_type<T>::destruct_array(m_p, m_size); |
| 623 |
crnd_free(m_p); |
| 624 |
m_p = NULL; |
| 625 |
m_size = 0; |
| 626 |
m_capacity = 0; |
| 627 |
} |
| 628 |
|
| 629 |
m_alloc_failed = false; |
| 630 |
} |
| 631 |
|
| 632 |
inline bool reserve(uint32 new_capacity) { |
| 633 |
if (!increase_capacity(new_capacity, false)) |
| 634 |
return false; |
| 635 |
|
| 636 |
return true; |
| 637 |
} |
| 638 |
|
| 639 |
inline bool resize(uint32 new_size) { |
| 640 |
if (m_size != new_size) { |
| 641 |
if (new_size < m_size) |
| 642 |
scalar_type<T>::destruct_array(m_p + new_size, m_size - new_size); |
| 643 |
else { |
| 644 |
if (new_size > m_capacity) { |
| 645 |
if (!increase_capacity(new_size, new_size == (m_size + 1))) |
| 646 |
return false; |
| 647 |
} |
| 648 |
|
| 649 |
scalar_type<T>::construct_array(m_p + m_size, new_size - m_size); |
| 650 |
} |
| 651 |
|
| 652 |
m_size = new_size; |
| 653 |
} |
| 654 |
|
| 655 |
return true; |
| 656 |
} |
| 657 |
|
| 658 |
inline bool push_back(const T& obj) { |
| 659 |
CRND_ASSERT(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size))); |
| 660 |
|
| 661 |
if (m_size >= m_capacity) { |
| 662 |
if (!increase_capacity(m_size + 1, true)) |
| 663 |
return false; |
| 664 |
} |
| 665 |
|
| 666 |
scalar_type<T>::construct(m_p + m_size, obj); |
| 667 |
m_size++; |
| 668 |
|
| 669 |
return true; |
| 670 |
} |
| 671 |
|
| 672 |
inline void pop_back() { |
| 673 |
CRND_ASSERT(m_size); |
| 674 |
|
| 675 |
if (m_size) { |
| 676 |
m_size--; |
| 677 |
scalar_type<T>::destruct(&m_p[m_size]); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
inline void insert(uint32 index, const T* p, uint32 n) { |
| 682 |
CRND_ASSERT(index <= m_size); |
| 683 |
if (!n) |
| 684 |
return; |
| 685 |
|
| 686 |
const uint32 orig_size = m_size; |
| 687 |
resize(m_size + n); |
| 688 |
|
| 689 |
const T* pSrc = m_p + orig_size - 1; |
| 690 |
T* pDst = const_cast<T*>(pSrc) + n; |
| 691 |
|
| 692 |
const uint32 num_to_move = orig_size - index; |
| 693 |
|
| 694 |
for (uint32 i = 0; i < num_to_move; i++) { |
| 695 |
CRND_ASSERT((pDst - m_p) < (int)m_size); |
| 696 |
*pDst-- = *pSrc--; |
| 697 |
} |
| 698 |
|
| 699 |
pSrc = p; |
| 700 |
pDst = m_p + index; |
| 701 |
|
| 702 |
for (uint32 i = 0; i < n; i++) { |
| 703 |
CRND_ASSERT((pDst - m_p) < (int)m_size); |
| 704 |
*pDst++ = *p++; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
inline void erase(uint32 start, uint32 n) { |
| 709 |
CRND_ASSERT((start + n) <= m_size); |
| 710 |
|
| 711 |
if (!n) |
| 712 |
return; |
| 713 |
|
| 714 |
const uint32 num_to_move = m_size - (start + n); |
| 715 |
|
| 716 |
T* pDst = m_p + start; |
| 717 |
T* pDst_end = pDst + num_to_move; |
| 718 |
const T* pSrc = m_p + start + n; |
| 719 |
|
| 720 |
while (pDst != pDst_end) |
| 721 |
*pDst++ = *pSrc++; |
| 722 |
|
| 723 |
scalar_type<T>::destruct_array(pDst_end, n); |
| 724 |
|
| 725 |
m_size -= n; |
| 726 |
} |
| 727 |
|
| 728 |
inline void erase(uint32 index) { |
| 729 |
erase(index, 1); |
| 730 |
} |
| 731 |
|
| 732 |
inline void erase(T* p) { |
| 733 |
CRND_ASSERT((p >= m_p) && (p < (m_p + m_size))); |
| 734 |
erase(p - m_p); |
| 735 |
} |
| 736 |
|
| 737 |
inline bool operator==(const vector& rhs) const { |
| 738 |
if (m_size != rhs.m_size) |
| 739 |
return false; |
| 740 |
else if (m_size) { |
| 741 |
if (scalar_type<T>::cFlag) |
| 742 |
return memcmp(m_p, rhs.m_p, sizeof(T) * m_size) == 0; |
| 743 |
else { |
| 744 |
const T* pSrc = m_p; |
| 745 |
const T* pDst = rhs.m_p; |
| 746 |
for (uint32 i = m_size; i; i--) |
| 747 |
if (!(*pSrc++ == *pDst++)) |
| 748 |
return false; |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
return true; |
| 753 |
} |
| 754 |
|
| 755 |
inline bool operator<(const vector& rhs) const { |
| 756 |
const uint32 min_size = math::minimum(m_size, rhs.m_size); |
| 757 |
|
| 758 |
const T* pSrc = m_p; |
| 759 |
const T* pSrc_end = m_p + min_size; |
| 760 |
const T* pDst = rhs.m_p; |
| 761 |
|
| 762 |
while ((pSrc < pSrc_end) && (*pSrc == *pDst)) { |
| 763 |
pSrc++; |
| 764 |
pDst++; |
| 765 |
} |
| 766 |
|
| 767 |
if (pSrc < pSrc_end) |
| 768 |
return *pSrc < *pDst; |
| 769 |
|
| 770 |
return m_size < rhs.m_size; |
| 771 |
} |
| 772 |
|
| 773 |
void swap(vector& other) { |
| 774 |
utils::swap(m_p, other.m_p); |
| 775 |
utils::swap(m_size, other.m_size); |
| 776 |
utils::swap(m_capacity, other.m_capacity); |
| 777 |
} |
| 778 |
|
| 779 |
private: |
| 780 |
T* m_p; |
| 781 |
uint32 m_size; |
| 782 |
uint32 m_capacity; |
| 783 |
bool m_alloc_failed; |
| 784 |
|
| 785 |
template <typename Q> |
| 786 |
struct is_vector { |
| 787 |
enum { cFlag = false }; |
| 788 |
}; |
| 789 |
template <typename Q> |
| 790 |
struct is_vector<vector<Q> > { |
| 791 |
enum { cFlag = true }; |
| 792 |
}; |
| 793 |
|
| 794 |
static void object_mover(void* pDst_void, void* pSrc_void, uint32 num) { |
| 795 |
T* pSrc = static_cast<T*>(pSrc_void); |
| 796 |
T* const pSrc_end = pSrc + num; |
| 797 |
T* pDst = static_cast<T*>(pDst_void); |
| 798 |
|
| 799 |
while (pSrc != pSrc_end) { |
| 800 |
helpers::construct<T>(pDst, *pSrc); |
| 801 |
pSrc->~T(); |
| 802 |
pSrc++; |
| 803 |
pDst++; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
inline bool increase_capacity(uint32 min_new_capacity, bool grow_hint) { |
| 808 |
if (!reinterpret_cast<elemental_vector*>(this)->increase_capacity( |
| 809 |
min_new_capacity, grow_hint, sizeof(T), |
| 810 |
((scalar_type<T>::cFlag) || (is_vector<T>::cFlag) || (bitwise_movable<T>::cFlag) || CRND_IS_POD(T)) ? NULL : object_mover)) { |
| 811 |
m_alloc_failed = true; |
| 812 |
return false; |
| 813 |
} |
| 814 |
return true; |
| 815 |
} |
| 816 |
}; |
| 817 |
|
| 818 |
#ifdef _MSC_VER |
| 819 |
#pragma warning(pop) |
| 820 |
#endif |
| 821 |
|
| 822 |
extern void vector_test(); |
| 823 |
|
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
namespace crnd { |
| 828 |
const crn_header* crnd_get_header(const void* pData, uint32 data_size); |
| 829 |
|
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
namespace crnd { |
| 834 |
|
| 835 |
const uint16 cInitCRC16 = 0; |
| 836 |
uint16 crc16(const void* pBuf, uint32 len, uint16 crc = cInitCRC16); |
| 837 |
|
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
namespace crnd { |
| 842 |
template <typename component_type> |
| 843 |
struct color_quad_component_traits { |
| 844 |
enum { |
| 845 |
cSigned = false, |
| 846 |
cFloat = false, |
| 847 |
cMin = cUINT8_MIN, |
| 848 |
cMax = cUINT8_MAX |
| 849 |
}; |
| 850 |
}; |
| 851 |
|
| 852 |
template <> |
| 853 |
struct color_quad_component_traits<int16> { |
| 854 |
enum { |
| 855 |
cSigned = true, |
| 856 |
cFloat = false, |
| 857 |
cMin = cINT16_MIN, |
| 858 |
cMax = cINT16_MAX |
| 859 |
}; |
| 860 |
}; |
| 861 |
|
| 862 |
template <> |
| 863 |
struct color_quad_component_traits<uint16> { |
| 864 |
enum { |
| 865 |
cSigned = false, |
| 866 |
cFloat = false, |
| 867 |
cMin = cUINT16_MIN, |
| 868 |
cMax = cUINT16_MAX |
| 869 |
}; |
| 870 |
}; |
| 871 |
|
| 872 |
template <> |
| 873 |
struct color_quad_component_traits<int32> { |
| 874 |
enum { |
| 875 |
cSigned = true, |
| 876 |
cFloat = false, |
| 877 |
cMin = cINT32_MIN, |
| 878 |
cMax = cINT32_MAX |
| 879 |
}; |
| 880 |
}; |
| 881 |
|
| 882 |
template <> |
| 883 |
struct color_quad_component_traits<uint32> { |
| 884 |
enum { |
| 885 |
cSigned = false, |
| 886 |
cFloat = false, |
| 887 |
cMin = cUINT32_MIN, |
| 888 |
cMax = cUINT32_MAX |
| 889 |
}; |
| 890 |
}; |
| 891 |
|
| 892 |
template <> |
| 893 |
struct color_quad_component_traits<float> { |
| 894 |
enum { |
| 895 |
cSigned = false, |
| 896 |
cFloat = true, |
| 897 |
cMin = cINT32_MIN, |
| 898 |
cMax = cINT32_MAX |
| 899 |
}; |
| 900 |
}; |
| 901 |
|
| 902 |
template <> |
| 903 |
struct color_quad_component_traits<double> { |
| 904 |
enum { |
| 905 |
cSigned = false, |
| 906 |
cFloat = true, |
| 907 |
cMin = cINT32_MIN, |
| 908 |
cMax = cINT32_MAX |
| 909 |
}; |
| 910 |
}; |
| 911 |
|
| 912 |
#ifdef _MSC_VER |
| 913 |
#pragma warning(push) |
| 914 |
#pragma warning(disable : 4201) // warning C4201: nonstandard extension used : nameless struct/union |
| 915 |
#pragma warning(disable : 4127) // warning C4127: conditional expression is constant |
| 916 |
#endif |
| 917 |
|
| 918 |
template <typename component_type, typename parameter_type> |
| 919 |
class color_quad : public helpers::rel_ops<color_quad<component_type, parameter_type> > { |
| 920 |
static parameter_type clamp(parameter_type v) { |
| 921 |
if (component_traits::cFloat) |
| 922 |
return v; |
| 923 |
else { |
| 924 |
if (v < component_traits::cMin) |
| 925 |
return component_traits::cMin; |
| 926 |
else if (v > component_traits::cMax) |
| 927 |
return component_traits::cMax; |
| 928 |
return v; |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
public: |
| 933 |
typedef component_type component_t; |
| 934 |
typedef parameter_type parameter_t; |
| 935 |
typedef color_quad_component_traits<component_type> component_traits; |
| 936 |
|
| 937 |
enum { cNumComps = 4 }; |
| 938 |
|
| 939 |
union { |
| 940 |
struct |
| 941 |
{ |
| 942 |
component_type r; |
| 943 |
component_type g; |
| 944 |
component_type b; |
| 945 |
component_type a; |
| 946 |
}; |
| 947 |
|
| 948 |
component_type c[cNumComps]; |
| 949 |
}; |
| 950 |
|
| 951 |
inline color_quad() { |
| 952 |
} |
| 953 |
|
| 954 |
inline color_quad(eClear) |
| 955 |
: r(0), g(0), b(0), a(0) { |
| 956 |
} |
| 957 |
|
| 958 |
inline color_quad(const color_quad& other) |
| 959 |
: r(other.r), g(other.g), b(other.b), a(other.a) { |
| 960 |
} |
| 961 |
|
| 962 |
inline color_quad(parameter_type y, parameter_type alpha = component_traits::cMax) { |
| 963 |
set(y, alpha); |
| 964 |
} |
| 965 |
|
| 966 |
inline color_quad(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha = component_traits::cMax) { |
| 967 |
set(red, green, blue, alpha); |
| 968 |
} |
| 969 |
|
| 970 |
template <typename other_component_type, typename other_parameter_type> |
| 971 |
inline color_quad(const color_quad<other_component_type, other_parameter_type>& other) |
| 972 |
: r(clamp(other.r)), g(clamp(other.g)), b(clamp(other.b)), a(clamp(other.a)) { |
| 973 |
} |
| 974 |
|
| 975 |
inline void clear() { |
| 976 |
r = 0; |
| 977 |
g = 0; |
| 978 |
b = 0; |
| 979 |
a = 0; |
| 980 |
} |
| 981 |
|
| 982 |
inline color_quad& operator=(const color_quad& other) { |
| 983 |
r = other.r; |
| 984 |
g = other.g; |
| 985 |
b = other.b; |
| 986 |
a = other.a; |
| 987 |
return *this; |
| 988 |
} |
| 989 |
|
| 990 |
template <typename other_component_type, typename other_parameter_type> |
| 991 |
inline color_quad& operator=(const color_quad<other_component_type, other_parameter_type>& other) { |
| 992 |
r = clamp(other.r); |
| 993 |
g = clamp(other.g); |
| 994 |
b = clamp(other.b); |
| 995 |
a = clamp(other.a); |
| 996 |
return *this; |
| 997 |
} |
| 998 |
|
| 999 |
inline color_quad& set(parameter_type y, parameter_type alpha = component_traits::cMax) { |
| 1000 |
y = clamp(y); |
| 1001 |
r = static_cast<component_type>(y); |
| 1002 |
g = static_cast<component_type>(y); |
| 1003 |
b = static_cast<component_type>(y); |
| 1004 |
a = static_cast<component_type>(alpha); |
| 1005 |
return *this; |
| 1006 |
} |
| 1007 |
|
| 1008 |
inline color_quad& set(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha = component_traits::cMax) { |
| 1009 |
r = static_cast<component_type>(clamp(red)); |
| 1010 |
g = static_cast<component_type>(clamp(green)); |
| 1011 |
b = static_cast<component_type>(clamp(blue)); |
| 1012 |
a = static_cast<component_type>(clamp(alpha)); |
| 1013 |
return *this; |
| 1014 |
} |
| 1015 |
|
| 1016 |
inline color_quad& set_noclamp_rgba(parameter_type red, parameter_type green, parameter_type blue, parameter_type alpha) { |
| 1017 |
r = static_cast<component_type>(red); |
| 1018 |
g = static_cast<component_type>(green); |
| 1019 |
b = static_cast<component_type>(blue); |
| 1020 |
a = static_cast<component_type>(alpha); |
| 1021 |
return *this; |
| 1022 |
} |
| 1023 |
|
| 1024 |
inline color_quad& set_noclamp_rgb(parameter_type red, parameter_type green, parameter_type blue) { |
| 1025 |
r = static_cast<component_type>(red); |
| 1026 |
g = static_cast<component_type>(green); |
| 1027 |
b = static_cast<component_type>(blue); |
| 1028 |
return *this; |
| 1029 |
} |
| 1030 |
|
| 1031 |
static inline parameter_type get_min_comp() { return component_traits::cMin; } |
| 1032 |
static inline parameter_type get_max_comp() { return component_traits::cMax; } |
| 1033 |
static inline bool get_comps_are_signed() { return component_traits::cSigned; } |
| 1034 |
|
| 1035 |
inline component_type operator[](uint32 i) const { |
| 1036 |
CRND_ASSERT(i < cNumComps); |
| 1037 |
return c[i]; |
| 1038 |
} |
| 1039 |
inline component_type& operator[](uint32 i) { |
| 1040 |
CRND_ASSERT(i < cNumComps); |
| 1041 |
return c[i]; |
| 1042 |
} |
| 1043 |
|
| 1044 |
inline color_quad& set_component(uint32 i, parameter_type f) { |
| 1045 |
CRND_ASSERT(i < cNumComps); |
| 1046 |
|
| 1047 |
c[i] = static_cast<component_type>(clamp(f)); |
| 1048 |
|
| 1049 |
return *this; |
| 1050 |
} |
| 1051 |
|
| 1052 |
inline color_quad& clamp(const color_quad& l, const color_quad& h) { |
| 1053 |
for (uint32 i = 0; i < cNumComps; i++) |
| 1054 |
c[i] = static_cast<component_type>(math::clamp<parameter_type>(c[i], l[i], h[i])); |
| 1055 |
return *this; |
| 1056 |
} |
| 1057 |
|
| 1058 |
inline color_quad& clamp(parameter_type l, parameter_type h) { |
| 1059 |
for (uint32 i = 0; i < cNumComps; i++) |
| 1060 |
c[i] = static_cast<component_type>(math::clamp<parameter_type>(c[i], l, h)); |
| 1061 |
return *this; |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
inline parameter_type get_luma() const { |
| 1066 |
return static_cast<parameter_type>((19595U * r + 38470U * g + 7471U * b + 32768) >> 16U); |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
inline parameter_type get_luma_rec709() const { |
| 1071 |
return static_cast<parameter_type>((13938U * r + 46869U * g + 4729U * b + 32768U) >> 16U); |
| 1072 |
} |
| 1073 |
|
| 1074 |
inline uint32 squared_distance(const color_quad& c, bool alpha = true) const { |
| 1075 |
return math::square(r - c.r) + math::square(g - c.g) + math::square(b - c.b) + (alpha ? math::square(a - c.a) : 0); |
| 1076 |
} |
| 1077 |
|
| 1078 |
inline bool rgb_equals(const color_quad& rhs) const { |
| 1079 |
return (r == rhs.r) && (g == rhs.g) && (b == rhs.b); |
| 1080 |
} |
| 1081 |
|
| 1082 |
inline bool operator==(const color_quad& rhs) const { |
| 1083 |
return (r == rhs.r) && (g == rhs.g) && (b == rhs.b) && (a == rhs.a); |
| 1084 |
} |
| 1085 |
|
| 1086 |
inline bool operator<(const color_quad& rhs) const { |
| 1087 |
for (uint32 i = 0; i < cNumComps; i++) { |
| 1088 |
if (c[i] < rhs.c[i]) |
| 1089 |
return true; |
| 1090 |
else if (!(c[i] == rhs.c[i])) |
| 1091 |
return false; |
| 1092 |
} |
| 1093 |
return false; |
| 1094 |
} |
| 1095 |
|
| 1096 |
inline color_quad& operator+=(const color_quad& other) { |
| 1097 |
for (uint32 i = 0; i < 4; i++) |
| 1098 |
c[i] = static_cast<component_type>(clamp(c[i] + other.c[i])); |
| 1099 |
return *this; |
| 1100 |
} |
| 1101 |
|
| 1102 |
inline color_quad& operator-=(const color_quad& other) { |
| 1103 |
for (uint32 i = 0; i < 4; i++) |
| 1104 |
c[i] = static_cast<component_type>(clamp(c[i] - other.c[i])); |
| 1105 |
return *this; |
| 1106 |
} |
| 1107 |
|
| 1108 |
inline color_quad& operator*=(parameter_type v) { |
| 1109 |
for (uint32 i = 0; i < 4; i++) |
| 1110 |
c[i] = static_cast<component_type>(clamp(c[i] * v)); |
| 1111 |
return *this; |
| 1112 |
} |
| 1113 |
|
| 1114 |
inline color_quad& operator/=(parameter_type v) { |
| 1115 |
for (uint32 i = 0; i < 4; i++) |
| 1116 |
c[i] = static_cast<component_type>(c[i] / v); |
| 1117 |
return *this; |
| 1118 |
} |
| 1119 |
|
| 1120 |
inline color_quad get_swizzled(uint32 x, uint32 y, uint32 z, uint32 w) const { |
| 1121 |
CRND_ASSERT((x | y | z | w) < 4); |
| 1122 |
return color_quad(c[x], c[y], c[z], c[w]); |
| 1123 |
} |
| 1124 |
|
| 1125 |
inline friend color_quad operator+(const color_quad& lhs, const color_quad& rhs) { |
| 1126 |
color_quad result(lhs); |
| 1127 |
result += rhs; |
| 1128 |
return result; |
| 1129 |
} |
| 1130 |
|
| 1131 |
inline friend color_quad operator-(const color_quad& lhs, const color_quad& rhs) { |
| 1132 |
color_quad result(lhs); |
| 1133 |
result -= rhs; |
| 1134 |
return result; |
| 1135 |
} |
| 1136 |
|
| 1137 |
inline friend color_quad operator*(const color_quad& lhs, parameter_type v) { |
| 1138 |
color_quad result(lhs); |
| 1139 |
result *= v; |
| 1140 |
return result; |
| 1141 |
} |
| 1142 |
|
| 1143 |
friend inline color_quad operator/(const color_quad& lhs, parameter_type v) { |
| 1144 |
color_quad result(lhs); |
| 1145 |
result /= v; |
| 1146 |
return result; |
| 1147 |
} |
| 1148 |
|
| 1149 |
friend inline color_quad operator*(parameter_type v, const color_quad& rhs) { |
| 1150 |
color_quad result(rhs); |
| 1151 |
result *= v; |
| 1152 |
return result; |
| 1153 |
} |
| 1154 |
|
| 1155 |
inline uint32 get_min_component_index(bool alpha = true) const { |
| 1156 |
uint32 index = 0; |
| 1157 |
uint32 limit = alpha ? cNumComps : (cNumComps - 1); |
| 1158 |
for (uint32 i = 1; i < limit; i++) |
| 1159 |
if (c[i] < c[index]) |
| 1160 |
index = i; |
| 1161 |
return index; |
| 1162 |
} |
| 1163 |
|
| 1164 |
inline uint32 get_max_component_index(bool alpha = true) const { |
| 1165 |
uint32 index = 0; |
| 1166 |
uint32 limit = alpha ? cNumComps : (cNumComps - 1); |
| 1167 |
for (uint32 i = 1; i < limit; i++) |
| 1168 |
if (c[i] > c[index]) |
| 1169 |
index = i; |
| 1170 |
return index; |
| 1171 |
} |
| 1172 |
|
| 1173 |
inline void get_float4(float* pDst) { |
| 1174 |
for (uint32 i = 0; i < 4; i++) |
| 1175 |
pDst[i] = ((*this)[i] - component_traits::cMin) / float(component_traits::cMax - component_traits::cMin); |
| 1176 |
} |
| 1177 |
|
| 1178 |
inline void get_float3(float* pDst) { |
| 1179 |
for (uint32 i = 0; i < 3; i++) |
| 1180 |
pDst[i] = ((*this)[i] - component_traits::cMin) / float(component_traits::cMax - component_traits::cMin); |
| 1181 |
} |
| 1182 |
|
| 1183 |
static inline color_quad make_black() { |
| 1184 |
return color_quad(0, 0, 0, component_traits::cMax); |
| 1185 |
} |
| 1186 |
|
| 1187 |
static inline color_quad make_white() { |
| 1188 |
return color_quad(component_traits::cMax, component_traits::cMax, component_traits::cMax, component_traits::cMax); |
| 1189 |
} |
| 1190 |
}; |
| 1191 |
|
| 1192 |
#ifdef _MSC_VER |
| 1193 |
#pragma warning(pop) |
| 1194 |
#endif |
| 1195 |
|
| 1196 |
template <typename c, typename q> |
| 1197 |
struct scalar_type<color_quad<c, q> > { |
| 1198 |
enum { cFlag = true }; |
| 1199 |
static inline void construct(color_quad<c, q>*) {} |
| 1200 |
static inline void construct(color_quad<c, q>* p, const color_quad<c, q>& init) { memcpy(p, &init, sizeof(color_quad<c, q>)); } |
| 1201 |
static inline void construct_array(color_quad<c, q>* p, uint32 n) { p, n; } |
| 1202 |
static inline void destruct(color_quad<c, q>* p) { p; } |
| 1203 |
static inline void destruct_array(color_quad<c, q>* p, uint32 n) { p, n; } |
| 1204 |
}; |
| 1205 |
|
| 1206 |
typedef color_quad<uint8, int> color_quad_u8; |
| 1207 |
typedef color_quad<int16, int> color_quad_i16; |
| 1208 |
typedef color_quad<uint16, int> color_quad_u16; |
| 1209 |
typedef color_quad<int32, int> color_quad_i32; |
| 1210 |
typedef color_quad<uint32, uint32> color_quad_u32; |
| 1211 |
typedef color_quad<float, float> color_quad_f; |
| 1212 |
typedef color_quad<double, double> color_quad_d; |
| 1213 |
|
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
namespace crnd { |
| 1218 |
enum dxt_format { |
| 1219 |
cDXTInvalid = -1, |
| 1220 |
|
| 1221 |
|
| 1222 |
cDXT1, |
| 1223 |
cDXT1A, |
| 1224 |
|
| 1225 |
cDXT3, |
| 1226 |
cDXT5, |
| 1227 |
cDXT5A, |
| 1228 |
|
| 1229 |
cDXN_XY, |
| 1230 |
cDXN_YX |
| 1231 |
}; |
| 1232 |
|
| 1233 |
enum dxt_constants { |
| 1234 |
cDXTBlockShift = 2U, |
| 1235 |
cDXTBlockSize = 1U << cDXTBlockShift, |
| 1236 |
|
| 1237 |
cDXT1BytesPerBlock = 8U, |
| 1238 |
cDXT5NBytesPerBlock = 16U, |
| 1239 |
|
| 1240 |
cDXT1SelectorBits = 2U, |
| 1241 |
cDXT1SelectorValues = 1U << cDXT1SelectorBits, |
| 1242 |
cDXT1SelectorMask = cDXT1SelectorValues - 1U, |
| 1243 |
|
| 1244 |
cDXT5SelectorBits = 3U, |
| 1245 |
cDXT5SelectorValues = 1U << cDXT5SelectorBits, |
| 1246 |
cDXT5SelectorMask = cDXT5SelectorValues - 1U |
| 1247 |
}; |
| 1248 |
|
| 1249 |
const float cDXT1MaxLinearValue = 3.0f; |
| 1250 |
const float cDXT1InvMaxLinearValue = 1.0f / 3.0f; |
| 1251 |
|
| 1252 |
const float cDXT5MaxLinearValue = 7.0f; |
| 1253 |
const float cDXT5InvMaxLinearValue = 1.0f / 7.0f; |
| 1254 |
|
| 1255 |
|
| 1256 |
extern const uint8 g_dxt1_to_linear[cDXT1SelectorValues]; |
| 1257 |
|
| 1258 |
|
| 1259 |
extern const uint8 g_dxt5_to_linear[cDXT5SelectorValues]; |
| 1260 |
|
| 1261 |
|
| 1262 |
extern const uint8 g_dxt1_from_linear[cDXT1SelectorValues]; |
| 1263 |
|
| 1264 |
|
| 1265 |
extern const uint8 g_dxt5_from_linear[cDXT5SelectorValues]; |
| 1266 |
|
| 1267 |
extern const uint8 g_six_alpha_invert_table[cDXT5SelectorValues]; |
| 1268 |
extern const uint8 g_eight_alpha_invert_table[cDXT5SelectorValues]; |
| 1269 |
|
| 1270 |
struct dxt1_block { |
| 1271 |
uint8 m_low_color[2]; |
| 1272 |
uint8 m_high_color[2]; |
| 1273 |
|
| 1274 |
enum { cNumSelectorBytes = 4 }; |
| 1275 |
uint8 m_selectors[cNumSelectorBytes]; |
| 1276 |
|
| 1277 |
inline void clear() { |
| 1278 |
utils::zero_this(this); |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
inline uint32 get_low_color() const { |
| 1283 |
return m_low_color[0] | (m_low_color[1] << 8U); |
| 1284 |
} |
| 1285 |
|
| 1286 |
inline uint32 get_high_color() const { |
| 1287 |
return m_high_color[0] | (m_high_color[1] << 8U); |
| 1288 |
} |
| 1289 |
|
| 1290 |
inline void set_low_color(uint16 c) { |
| 1291 |
m_low_color[0] = static_cast<uint8>(c & 0xFF); |
| 1292 |
m_low_color[1] = static_cast<uint8>((c >> 8) & 0xFF); |
| 1293 |
} |
| 1294 |
|
| 1295 |
inline void set_high_color(uint16 c) { |
| 1296 |
m_high_color[0] = static_cast<uint8>(c & 0xFF); |
| 1297 |
m_high_color[1] = static_cast<uint8>((c >> 8) & 0xFF); |
| 1298 |
} |
| 1299 |
|
| 1300 |
inline uint32 get_selector(uint32 x, uint32 y) const { |
| 1301 |
CRND_ASSERT((x < 4U) && (y < 4U)); |
| 1302 |
return (m_selectors[y] >> (x * cDXT1SelectorBits)) & cDXT1SelectorMask; |
| 1303 |
} |
| 1304 |
|
| 1305 |
inline void set_selector(uint32 x, uint32 y, uint32 val) { |
| 1306 |
CRND_ASSERT((x < 4U) && (y < 4U) && (val < 4U)); |
| 1307 |
|
| 1308 |
m_selectors[y] &= (~(cDXT1SelectorMask << (x * cDXT1SelectorBits))); |
| 1309 |
m_selectors[y] |= (val << (x * cDXT1SelectorBits)); |
| 1310 |
} |
| 1311 |
|
| 1312 |
static uint16 pack_color(const color_quad_u8& color, bool scaled, uint32 bias = 127U); |
| 1313 |
static uint16 pack_color(uint32 r, uint32 g, uint32 b, bool scaled, uint32 bias = 127U); |
| 1314 |
|
| 1315 |
static color_quad_u8 unpack_color(uint16 packed_color, bool scaled, uint32 alpha = 255U); |
| 1316 |
static void unpack_color(uint32& r, uint32& g, uint32& b, uint16 packed_color, bool scaled); |
| 1317 |
|
| 1318 |
static uint32 get_block_colors3(color_quad_u8* pDst, uint16 color0, uint16 color1); |
| 1319 |
static uint32 get_block_colors4(color_quad_u8* pDst, uint16 color0, uint16 color1); |
| 1320 |
|
| 1321 |
static uint32 get_block_colors(color_quad_u8* pDst, uint16 color0, uint16 color1); |
| 1322 |
|
| 1323 |
static color_quad_u8 unpack_endpoint(uint32 endpoints, uint32 index, bool scaled, uint32 alpha = 255U); |
| 1324 |
static uint32 pack_endpoints(uint32 lo, uint32 hi); |
| 1325 |
}; |
| 1326 |
|
| 1327 |
CRND_DEFINE_BITWISE_MOVABLE(dxt1_block); |
| 1328 |
|
| 1329 |
struct dxt3_block { |
| 1330 |
enum { cNumAlphaBytes = 8 }; |
| 1331 |
uint8 m_alpha[cNumAlphaBytes]; |
| 1332 |
|
| 1333 |
void set_alpha(uint32 x, uint32 y, uint32 value, bool scaled); |
| 1334 |
uint32 get_alpha(uint32 x, uint32 y, bool scaled) const; |
| 1335 |
}; |
| 1336 |
|
| 1337 |
CRND_DEFINE_BITWISE_MOVABLE(dxt3_block); |
| 1338 |
|
| 1339 |
struct dxt5_block { |
| 1340 |
uint8 m_endpoints[2]; |
| 1341 |
|
| 1342 |
enum { cNumSelectorBytes = 6 }; |
| 1343 |
uint8 m_selectors[cNumSelectorBytes]; |
| 1344 |
|
| 1345 |
inline void clear() { |
| 1346 |
utils::zero_this(this); |
| 1347 |
} |
| 1348 |
|
| 1349 |
inline uint32 get_low_alpha() const { |
| 1350 |
return m_endpoints[0]; |
| 1351 |
} |
| 1352 |
|
| 1353 |
inline uint32 get_high_alpha() const { |
| 1354 |
return m_endpoints[1]; |
| 1355 |
} |
| 1356 |
|
| 1357 |
inline void set_low_alpha(uint32 i) { |
| 1358 |
CRND_ASSERT(i <= cUINT8_MAX); |
| 1359 |
m_endpoints[0] = static_cast<uint8>(i); |
| 1360 |
} |
| 1361 |
|
| 1362 |
inline void set_high_alpha(uint32 i) { |
| 1363 |
CRND_ASSERT(i <= cUINT8_MAX); |
| 1364 |
m_endpoints[1] = static_cast<uint8>(i); |
| 1365 |
} |
| 1366 |
|
| 1367 |
uint32 get_endpoints_as_word() const { return m_endpoints[0] | (m_endpoints[1] << 8); } |
| 1368 |
|
| 1369 |
uint32 get_selectors_as_word(uint32 index) { |
| 1370 |
CRND_ASSERT(index < 3); |
| 1371 |
return m_selectors[index * 2] | (m_selectors[index * 2 + 1] << 8); |
| 1372 |
} |
| 1373 |
|
| 1374 |
inline uint32 get_selector(uint32 x, uint32 y) const { |
| 1375 |
CRND_ASSERT((x < 4U) && (y < 4U)); |
| 1376 |
|
| 1377 |
uint32 selector_index = (y * 4) + x; |
| 1378 |
uint32 bit_index = selector_index * cDXT5SelectorBits; |
| 1379 |
|
| 1380 |
uint32 byte_index = bit_index >> 3; |
| 1381 |
uint32 bit_ofs = bit_index & 7; |
| 1382 |
|
| 1383 |
uint32 v = m_selectors[byte_index]; |
| 1384 |
if (byte_index < (cNumSelectorBytes - 1)) |
| 1385 |
v |= (m_selectors[byte_index + 1] << 8); |
| 1386 |
|
| 1387 |
return (v >> bit_ofs) & 7; |
| 1388 |
} |
| 1389 |
|
| 1390 |
inline void set_selector(uint32 x, uint32 y, uint32 val) { |
| 1391 |
CRND_ASSERT((x < 4U) && (y < 4U) && (val < 8U)); |
| 1392 |
|
| 1393 |
uint32 selector_index = (y * 4) + x; |
| 1394 |
uint32 bit_index = selector_index * cDXT5SelectorBits; |
| 1395 |
|
| 1396 |
uint32 byte_index = bit_index >> 3; |
| 1397 |
uint32 bit_ofs = bit_index & 7; |
| 1398 |
|
| 1399 |
uint32 v = m_selectors[byte_index]; |
| 1400 |
if (byte_index < (cNumSelectorBytes - 1)) |
| 1401 |
v |= (m_selectors[byte_index + 1] << 8); |
| 1402 |
|
| 1403 |
v &= (~(7 << bit_ofs)); |
| 1404 |
v |= (val << bit_ofs); |
| 1405 |
|
| 1406 |
m_selectors[byte_index] = static_cast<uint8>(v); |
| 1407 |
if (byte_index < (cNumSelectorBytes - 1)) |
| 1408 |
m_selectors[byte_index + 1] = static_cast<uint8>(v >> 8); |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
static uint32 get_block_values6(color_quad_u8* pDst, uint32 l, uint32 h); |
| 1413 |
static uint32 get_block_values8(color_quad_u8* pDst, uint32 l, uint32 h); |
| 1414 |
static uint32 get_block_values(color_quad_u8* pDst, uint32 l, uint32 h); |
| 1415 |
|
| 1416 |
static uint32 get_block_values6(uint32* pDst, uint32 l, uint32 h); |
| 1417 |
static uint32 get_block_values8(uint32* pDst, uint32 l, uint32 h); |
| 1418 |
|
| 1419 |
static uint32 get_block_values(uint32* pDst, uint32 l, uint32 h); |
| 1420 |
|
| 1421 |
static uint32 unpack_endpoint(uint32 packed, uint32 index); |
| 1422 |
static uint32 pack_endpoints(uint32 lo, uint32 hi); |
| 1423 |
}; |
| 1424 |
|
| 1425 |
CRND_DEFINE_BITWISE_MOVABLE(dxt5_block); |
| 1426 |
|
| 1427 |
} |
| 1428 |
|
| 1429 |
|
| 1430 |
#ifdef _XBOX |
| 1431 |
#define CRND_PREFIX_CODING_USE_FIXED_TABLE_SIZE 1 |
| 1432 |
#else |
| 1433 |
#define CRND_PREFIX_CODING_USE_FIXED_TABLE_SIZE 0 |
| 1434 |
#endif |
| 1435 |
|
| 1436 |
namespace crnd { |
| 1437 |
namespace prefix_coding { |
| 1438 |
const uint32 cMaxExpectedCodeSize = 16; |
| 1439 |
const uint32 cMaxSupportedSyms = 8192; |
| 1440 |
const uint32 cMaxTableBits = 11; |
| 1441 |
|
| 1442 |
class decoder_tables { |
| 1443 |
public: |
| 1444 |
inline decoder_tables() |
| 1445 |
: m_cur_lookup_size(0), m_lookup(NULL), m_cur_sorted_symbol_order_size(0), m_sorted_symbol_order(NULL) { |
| 1446 |
} |
| 1447 |
|
| 1448 |
inline decoder_tables(const decoder_tables& other) |
| 1449 |
: m_cur_lookup_size(0), m_lookup(NULL), m_cur_sorted_symbol_order_size(0), m_sorted_symbol_order(NULL) { |
| 1450 |
*this = other; |
| 1451 |
} |
| 1452 |
|
| 1453 |
decoder_tables& operator=(const decoder_tables& other) { |
| 1454 |
if (this == &other) |
| 1455 |
return *this; |
| 1456 |
|
| 1457 |
clear(); |
| 1458 |
|
| 1459 |
memcpy((void*) this, &other, sizeof(*this)); |
| 1460 |
|
| 1461 |
if (other.m_lookup) { |
| 1462 |
m_lookup = crnd_new_array<uint32>(m_cur_lookup_size); |
| 1463 |
if (m_lookup) |
| 1464 |
memcpy(m_lookup, other.m_lookup, sizeof(m_lookup[0]) * m_cur_lookup_size); |
| 1465 |
} |
| 1466 |
|
| 1467 |
if (other.m_sorted_symbol_order) { |
| 1468 |
m_sorted_symbol_order = crnd_new_array<uint16>(m_cur_sorted_symbol_order_size); |
| 1469 |
if (m_sorted_symbol_order) |
| 1470 |
memcpy(m_sorted_symbol_order, other.m_sorted_symbol_order, sizeof(m_sorted_symbol_order[0]) * m_cur_sorted_symbol_order_size); |
| 1471 |
} |
| 1472 |
|
| 1473 |
return *this; |
| 1474 |
} |
| 1475 |
|
| 1476 |
inline void clear() { |
| 1477 |
if (m_lookup) { |
| 1478 |
crnd_delete_array(m_lookup); |
| 1479 |
m_lookup = 0; |
| 1480 |
m_cur_lookup_size = 0; |
| 1481 |
} |
| 1482 |
|
| 1483 |
if (m_sorted_symbol_order) { |
| 1484 |
crnd_delete_array(m_sorted_symbol_order); |
| 1485 |
m_sorted_symbol_order = NULL; |
| 1486 |
m_cur_sorted_symbol_order_size = 0; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
inline ~decoder_tables() { |
| 1491 |
if (m_lookup) |
| 1492 |
crnd_delete_array(m_lookup); |
| 1493 |
|
| 1494 |
if (m_sorted_symbol_order) |
| 1495 |
crnd_delete_array(m_sorted_symbol_order); |
| 1496 |
} |
| 1497 |
|
| 1498 |
bool init(uint32 num_syms, const uint8* pCodesizes, uint32 table_bits); |
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
uint32 m_num_syms; |
| 1503 |
uint32 m_total_used_syms; |
| 1504 |
uint32 m_table_bits; |
| 1505 |
uint32 m_table_shift; |
| 1506 |
uint32 m_table_max_code; |
| 1507 |
uint32 m_decode_start_code_size; |
| 1508 |
|
| 1509 |
uint8 m_min_code_size; |
| 1510 |
uint8 m_max_code_size; |
| 1511 |
|
| 1512 |
uint32 m_max_codes[cMaxExpectedCodeSize + 1]; |
| 1513 |
int32 m_val_ptrs[cMaxExpectedCodeSize + 1]; |
| 1514 |
|
| 1515 |
uint32 m_cur_lookup_size; |
| 1516 |
uint32* m_lookup; |
| 1517 |
|
| 1518 |
uint32 m_cur_sorted_symbol_order_size; |
| 1519 |
uint16* m_sorted_symbol_order; |
| 1520 |
|
| 1521 |
inline uint32 get_unshifted_max_code(uint32 len) const { |
| 1522 |
CRND_ASSERT((len >= 1) && (len <= cMaxExpectedCodeSize)); |
| 1523 |
uint32 k = m_max_codes[len - 1]; |
| 1524 |
if (!k) |
| 1525 |
return crnd::cUINT32_MAX; |
| 1526 |
return (k - 1) >> (16 - len); |
| 1527 |
} |
| 1528 |
}; |
| 1529 |
|
| 1530 |
} |
| 1531 |
|
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
namespace crnd { |
| 1536 |
class static_huffman_data_model { |
| 1537 |
public: |
| 1538 |
static_huffman_data_model(); |
| 1539 |
static_huffman_data_model(const static_huffman_data_model& other); |
| 1540 |
~static_huffman_data_model(); |
| 1541 |
|
| 1542 |
static_huffman_data_model& operator=(const static_huffman_data_model& rhs); |
| 1543 |
|
| 1544 |
bool init(uint32 total_syms, const uint8* pCode_sizes, uint32 code_size_limit); |
| 1545 |
void clear(); |
| 1546 |
|
| 1547 |
inline bool is_valid() const { return m_pDecode_tables != NULL; } |
| 1548 |
|
| 1549 |
inline uint32 get_total_syms() const { return m_total_syms; } |
| 1550 |
|
| 1551 |
inline uint32 get_code_size(uint32 sym) const { return m_code_sizes[sym]; } |
| 1552 |
|
| 1553 |
inline const uint8* get_code_sizes() const { return m_code_sizes.empty() ? NULL : &m_code_sizes[0]; } |
| 1554 |
|
| 1555 |
public: |
| 1556 |
uint32 m_total_syms; |
| 1557 |
crnd::vector<uint8> m_code_sizes; |
| 1558 |
prefix_coding::decoder_tables* m_pDecode_tables; |
| 1559 |
|
| 1560 |
private: |
| 1561 |
bool prepare_decoder_tables(); |
| 1562 |
uint compute_decoder_table_bits() const; |
| 1563 |
|
| 1564 |
friend class symbol_codec; |
| 1565 |
}; |
| 1566 |
|
| 1567 |
class symbol_codec { |
| 1568 |
public: |
| 1569 |
symbol_codec(); |
| 1570 |
|
| 1571 |
bool start_decoding(const uint8* pBuf, uint32 buf_size); |
| 1572 |
bool decode_receive_static_data_model(static_huffman_data_model& model); |
| 1573 |
|
| 1574 |
uint32 decode_bits(uint32 num_bits); |
| 1575 |
uint32 decode(const static_huffman_data_model& model); |
| 1576 |
|
| 1577 |
uint64 stop_decoding(); |
| 1578 |
|
| 1579 |
public: |
| 1580 |
const uint8* m_pDecode_buf; |
| 1581 |
const uint8* m_pDecode_buf_next; |
| 1582 |
const uint8* m_pDecode_buf_end; |
| 1583 |
uint32 m_decode_buf_size; |
| 1584 |
|
| 1585 |
typedef uint32 bit_buf_type; |
| 1586 |
enum { cBitBufSize = 32U }; |
| 1587 |
bit_buf_type m_bit_buf; |
| 1588 |
|
| 1589 |
int m_bit_count; |
| 1590 |
|
| 1591 |
private: |
| 1592 |
void get_bits_init(); |
| 1593 |
uint32 get_bits(uint32 num_bits); |
| 1594 |
}; |
| 1595 |
|
| 1596 |
} |
| 1597 |
|
| 1598 |
namespace crnd { |
| 1599 |
void crnd_assert(const char* pExp, const char* pFile, unsigned line) { |
| 1600 |
char buf[512]; |
| 1601 |
|
| 1602 |
#if defined(_WIN32) |
| 1603 |
sprintf_s(buf, sizeof(buf), "%s(%u): Assertion failure: \"%s\"\n", pFile, line, pExp); |
| 1604 |
#else |
| 1605 |
snprintf(buf, sizeof(buf), "%s(%u): Assertion failure: \"%s\"\n", pFile, line, pExp); |
| 1606 |
#endif |
| 1607 |
|
| 1608 |
crnd_output_debug_string(buf); |
| 1609 |
|
| 1610 |
puts(buf); |
| 1611 |
|
| 1612 |
if (crnd_is_debugger_present()) |
| 1613 |
crnd_debug_break(); |
| 1614 |
} |
| 1615 |
|
| 1616 |
void crnd_trace(const char* pFmt, va_list args) { |
| 1617 |
if (crnd_is_debugger_present()) { |
| 1618 |
char buf[512]; |
| 1619 |
#if defined(_WIN32) |
| 1620 |
vsprintf_s(buf, sizeof(buf), pFmt, args); |
| 1621 |
#else |
| 1622 |
vsnprintf(buf, sizeof(buf), pFmt, args); |
| 1623 |
#endif |
| 1624 |
|
| 1625 |
crnd_output_debug_string(buf); |
| 1626 |
} |
| 1627 |
}; |
| 1628 |
|
| 1629 |
void crnd_trace(const char* pFmt, ...) { |
| 1630 |
va_list args; |
| 1631 |
va_start(args, pFmt); |
| 1632 |
crnd_trace(pFmt, args); |
| 1633 |
va_end(args); |
| 1634 |
}; |
| 1635 |
|
| 1636 |
} |
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
namespace crnd { |
| 1641 |
uint16 crc16(const void* pBuf, uint32 len, uint16 crc) { |
| 1642 |
crc = ~crc; |
| 1643 |
|
| 1644 |
const uint8* p = reinterpret_cast<const uint8*>(pBuf); |
| 1645 |
while (len) { |
| 1646 |
const uint16 q = *p++ ^ (crc >> 8U); |
| 1647 |
crc <<= 8U; |
| 1648 |
|
| 1649 |
uint16 r = (q >> 4U) ^ q; |
| 1650 |
crc ^= r; |
| 1651 |
r <<= 5U; |
| 1652 |
crc ^= r; |
| 1653 |
r <<= 7U; |
| 1654 |
crc ^= r; |
| 1655 |
|
| 1656 |
len--; |
| 1657 |
} |
| 1658 |
|
| 1659 |
return static_cast<uint16>(~crc); |
| 1660 |
} |
| 1661 |
|
| 1662 |
} |
| 1663 |
|
| 1664 |
|
| 1665 |
namespace crnd { |
| 1666 |
bool elemental_vector::increase_capacity(uint32 min_new_capacity, bool grow_hint, uint32 element_size, object_mover pMover) { |
| 1667 |
CRND_ASSERT(m_size <= m_capacity); |
| 1668 |
CRND_ASSERT(min_new_capacity < (0x7FFF0000U / element_size)); |
| 1669 |
|
| 1670 |
if (m_capacity >= min_new_capacity) |
| 1671 |
return true; |
| 1672 |
|
| 1673 |
uint32 new_capacity = min_new_capacity; |
| 1674 |
if ((grow_hint) && (!math::is_power_of_2(new_capacity))) |
| 1675 |
new_capacity = math::next_pow2(new_capacity); |
| 1676 |
|
| 1677 |
CRND_ASSERT(new_capacity && (new_capacity > m_capacity)); |
| 1678 |
|
| 1679 |
const uint32 desired_size = element_size * new_capacity; |
| 1680 |
size_t actual_size; |
| 1681 |
if (!pMover) { |
| 1682 |
void* new_p = crnd_realloc(m_p, desired_size, &actual_size, true); |
| 1683 |
if (!new_p) |
| 1684 |
return false; |
| 1685 |
m_p = new_p; |
| 1686 |
} else { |
| 1687 |
void* new_p = crnd_malloc(desired_size, &actual_size); |
| 1688 |
if (!new_p) |
| 1689 |
return false; |
| 1690 |
|
| 1691 |
(*pMover)(new_p, m_p, m_size); |
| 1692 |
|
| 1693 |
if (m_p) |
| 1694 |
crnd_free(m_p); |
| 1695 |
|
| 1696 |
m_p = new_p; |
| 1697 |
} |
| 1698 |
|
| 1699 |
if (actual_size > desired_size) |
| 1700 |
m_capacity = static_cast<uint32>(actual_size / element_size); |
| 1701 |
else |
| 1702 |
m_capacity = new_capacity; |
| 1703 |
|
| 1704 |
return true; |
| 1705 |
} |
| 1706 |
|
| 1707 |
} |
| 1708 |
|
| 1709 |
|
| 1710 |
namespace crnd { |
| 1711 |
namespace utils { |
| 1712 |
uint32 compute_max_mips(uint32 width, uint32 height) { |
| 1713 |
if ((width | height) == 0) |
| 1714 |
return 0; |
| 1715 |
|
| 1716 |
uint32 num_mips = 1; |
| 1717 |
|
| 1718 |
while ((width > 1U) || (height > 1U)) { |
| 1719 |
width >>= 1U; |
| 1720 |
height >>= 1U; |
| 1721 |
num_mips++; |
| 1722 |
} |
| 1723 |
|
| 1724 |
return num_mips; |
| 1725 |
} |
| 1726 |
|
| 1727 |
} |
| 1728 |
|
| 1729 |
} |
| 1730 |
|
| 1731 |
|
| 1732 |
namespace crnd { |
| 1733 |
namespace prefix_coding { |
| 1734 |
bool decoder_tables::init(uint32 num_syms, const uint8* pCodesizes, uint32 table_bits) { |
| 1735 |
uint32 min_codes[cMaxExpectedCodeSize]; |
| 1736 |
if ((!num_syms) || (table_bits > cMaxTableBits)) |
| 1737 |
return false; |
| 1738 |
|
| 1739 |
m_num_syms = num_syms; |
| 1740 |
|
| 1741 |
uint32 num_codes[cMaxExpectedCodeSize + 1]; |
| 1742 |
utils::zero_object(num_codes); |
| 1743 |
|
| 1744 |
for (uint32 i = 0; i < num_syms; i++) { |
| 1745 |
uint32 c = pCodesizes[i]; |
| 1746 |
if (c) |
| 1747 |
num_codes[c]++; |
| 1748 |
} |
| 1749 |
|
| 1750 |
uint32 sorted_positions[cMaxExpectedCodeSize + 1]; |
| 1751 |
|
| 1752 |
uint32 cur_code = 0; |
| 1753 |
|
| 1754 |
uint32 total_used_syms = 0; |
| 1755 |
uint32 max_code_size = 0; |
| 1756 |
uint32 min_code_size = cUINT32_MAX; |
| 1757 |
for (uint32 i = 1; i <= cMaxExpectedCodeSize; i++) { |
| 1758 |
const uint32 n = num_codes[i]; |
| 1759 |
|
| 1760 |
if (!n) |
| 1761 |
m_max_codes[i - 1] = 0; |
| 1762 |
else { |
| 1763 |
min_code_size = math::minimum(min_code_size, i); |
| 1764 |
max_code_size = math::maximum(max_code_size, i); |
| 1765 |
|
| 1766 |
min_codes[i - 1] = cur_code; |
| 1767 |
|
| 1768 |
m_max_codes[i - 1] = cur_code + n - 1; |
| 1769 |
m_max_codes[i - 1] = 1 + ((m_max_codes[i - 1] << (16 - i)) | ((1 << (16 - i)) - 1)); |
| 1770 |
|
| 1771 |
m_val_ptrs[i - 1] = total_used_syms; |
| 1772 |
|
| 1773 |
sorted_positions[i] = total_used_syms; |
| 1774 |
|
| 1775 |
cur_code += n; |
| 1776 |
total_used_syms += n; |
| 1777 |
} |
| 1778 |
|
| 1779 |
cur_code <<= 1; |
| 1780 |
} |
| 1781 |
|
| 1782 |
m_total_used_syms = total_used_syms; |
| 1783 |
|
| 1784 |
if (total_used_syms > m_cur_sorted_symbol_order_size) { |
| 1785 |
m_cur_sorted_symbol_order_size = total_used_syms; |
| 1786 |
|
| 1787 |
if (!math::is_power_of_2(total_used_syms)) |
| 1788 |
m_cur_sorted_symbol_order_size = math::minimum<uint32>(num_syms, math::next_pow2(total_used_syms)); |
| 1789 |
|
| 1790 |
if (m_sorted_symbol_order) |
| 1791 |
crnd_delete_array(m_sorted_symbol_order); |
| 1792 |
|
| 1793 |
m_sorted_symbol_order = crnd_new_array<uint16>(m_cur_sorted_symbol_order_size); |
| 1794 |
if (!m_sorted_symbol_order) |
| 1795 |
return false; |
| 1796 |
} |
| 1797 |
|
| 1798 |
m_min_code_size = static_cast<uint8>(min_code_size); |
| 1799 |
m_max_code_size = static_cast<uint8>(max_code_size); |
| 1800 |
|
| 1801 |
for (uint32 i = 0; i < num_syms; i++) { |
| 1802 |
uint32 c = pCodesizes[i]; |
| 1803 |
if (c) { |
| 1804 |
CRND_ASSERT(num_codes[c]); |
| 1805 |
|
| 1806 |
uint32 sorted_pos = sorted_positions[c]++; |
| 1807 |
|
| 1808 |
CRND_ASSERT(sorted_pos < total_used_syms); |
| 1809 |
|
| 1810 |
m_sorted_symbol_order[sorted_pos] = static_cast<uint16>(i); |
| 1811 |
} |
| 1812 |
} |
| 1813 |
|
| 1814 |
if (table_bits <= m_min_code_size) |
| 1815 |
table_bits = 0; |
| 1816 |
m_table_bits = table_bits; |
| 1817 |
|
| 1818 |
if (table_bits) { |
| 1819 |
uint32 table_size = 1 << table_bits; |
| 1820 |
if (table_size > m_cur_lookup_size) { |
| 1821 |
m_cur_lookup_size = table_size; |
| 1822 |
|
| 1823 |
if (m_lookup) |
| 1824 |
crnd_delete_array(m_lookup); |
| 1825 |
|
| 1826 |
m_lookup = crnd_new_array<uint32>(table_size); |
| 1827 |
if (!m_lookup) |
| 1828 |
return false; |
| 1829 |
} |
| 1830 |
|
| 1831 |
memset(m_lookup, 0xFF, (uint)sizeof(m_lookup[0]) * (1UL << table_bits)); |
| 1832 |
|
| 1833 |
for (uint32 codesize = 1; codesize <= table_bits; codesize++) { |
| 1834 |
if (!num_codes[codesize]) |
| 1835 |
continue; |
| 1836 |
|
| 1837 |
const uint32 fillsize = table_bits - codesize; |
| 1838 |
const uint32 fillnum = 1 << fillsize; |
| 1839 |
|
| 1840 |
const uint32 min_code = min_codes[codesize - 1]; |
| 1841 |
const uint32 max_code = get_unshifted_max_code(codesize); |
| 1842 |
const uint32 val_ptr = m_val_ptrs[codesize - 1]; |
| 1843 |
|
| 1844 |
for (uint32 code = min_code; code <= max_code; code++) { |
| 1845 |
const uint32 sym_index = m_sorted_symbol_order[val_ptr + code - min_code]; |
| 1846 |
CRND_ASSERT(pCodesizes[sym_index] == codesize); |
| 1847 |
|
| 1848 |
for (uint32 j = 0; j < fillnum; j++) { |
| 1849 |
const uint32 t = j + (code << fillsize); |
| 1850 |
|
| 1851 |
CRND_ASSERT(t < (1U << table_bits)); |
| 1852 |
|
| 1853 |
CRND_ASSERT(m_lookup[t] == cUINT32_MAX); |
| 1854 |
|
| 1855 |
m_lookup[t] = sym_index | (codesize << 16U); |
| 1856 |
} |
| 1857 |
} |
| 1858 |
} |
| 1859 |
} |
| 1860 |
|
| 1861 |
for (uint32 i = 0; i < cMaxExpectedCodeSize; i++) |
| 1862 |
m_val_ptrs[i] -= min_codes[i]; |
| 1863 |
|
| 1864 |
m_table_max_code = 0; |
| 1865 |
m_decode_start_code_size = m_min_code_size; |
| 1866 |
|
| 1867 |
if (table_bits) { |
| 1868 |
uint32 i; |
| 1869 |
for (i = table_bits; i >= 1; i--) { |
| 1870 |
if (num_codes[i]) { |
| 1871 |
m_table_max_code = m_max_codes[i - 1]; |
| 1872 |
break; |
| 1873 |
} |
| 1874 |
} |
| 1875 |
if (i >= 1) { |
| 1876 |
m_decode_start_code_size = table_bits + 1; |
| 1877 |
for (uint32 j = table_bits + 1; j <= max_code_size; j++) { |
| 1878 |
if (num_codes[j]) { |
| 1879 |
m_decode_start_code_size = j; |
| 1880 |
break; |
| 1881 |
} |
| 1882 |
} |
| 1883 |
} |
| 1884 |
} |
| 1885 |
|
| 1886 |
|
| 1887 |
m_max_codes[cMaxExpectedCodeSize] = cUINT32_MAX; |
| 1888 |
m_val_ptrs[cMaxExpectedCodeSize] = 0xFFFFF; |
| 1889 |
|
| 1890 |
m_table_shift = 32 - m_table_bits; |
| 1891 |
return true; |
| 1892 |
} |
| 1893 |
|
| 1894 |
} |
| 1895 |
|
| 1896 |
} |
| 1897 |
|
| 1898 |
|
| 1899 |
namespace crnd { |
| 1900 |
bool crnd_is_debugger_present() { |
| 1901 |
#ifdef CRND_DEVEL |
| 1902 |
return IsDebuggerPresent() != 0; |
| 1903 |
#else |
| 1904 |
return false; |
| 1905 |
#endif |
| 1906 |
} |
| 1907 |
|
| 1908 |
void crnd_debug_break() { |
| 1909 |
#ifdef CRND_DEVEL |
| 1910 |
DebugBreak(); |
| 1911 |
#endif |
| 1912 |
} |
| 1913 |
|
| 1914 |
void crnd_output_debug_string(const char* p) { |
| 1915 |
(void)p; |
| 1916 |
#ifdef CRND_DEVEL |
| 1917 |
OutputDebugStringA(p); |
| 1918 |
#endif |
| 1919 |
} |
| 1920 |
|
| 1921 |
} |
| 1922 |
|
| 1923 |
|
| 1924 |
namespace crnd { |
| 1925 |
const uint32 MAX_POSSIBLE_BLOCK_SIZE = 0x7FFF0000U; |
| 1926 |
|
| 1927 |
static void* crnd_default_realloc(void* p, size_t size, size_t* pActual_size, bool movable, void*) { |
| 1928 |
void* p_new; |
| 1929 |
|
| 1930 |
if (!p) { |
| 1931 |
#if defined(__FreeBSD__) |
| 1932 |
|
| 1933 |
p_new = ::aligned_alloc(CRNLIB_MIN_ALLOC_ALIGNMENT, size); |
| 1934 |
#else |
| 1935 |
p_new = ::malloc(size); |
| 1936 |
#endif |
| 1937 |
|
| 1938 |
if (pActual_size) { |
| 1939 |
#ifdef WIN32 |
| 1940 |
*pActual_size = p_new ? ::_msize(p_new) : 0; |
| 1941 |
#elif defined(__APPLE__) |
| 1942 |
*pActual_size = p_new ? ::malloc_size(p_new) : 0; |
| 1943 |
#else |
| 1944 |
*pActual_size = p_new ? malloc_usable_size(p_new) : 0; |
| 1945 |
#endif |
| 1946 |
} |
| 1947 |
} else if (!size) { |
| 1948 |
::free(p); |
| 1949 |
p_new = NULL; |
| 1950 |
|
| 1951 |
if (pActual_size) |
| 1952 |
*pActual_size = 0; |
| 1953 |
} else { |
| 1954 |
void* p_final_block = p; |
| 1955 |
#ifdef WIN32 |
| 1956 |
p_new = ::_expand(p, size); |
| 1957 |
#else |
| 1958 |
p_new = NULL; |
| 1959 |
#endif |
| 1960 |
|
| 1961 |
if (p_new) |
| 1962 |
p_final_block = p_new; |
| 1963 |
else if (movable) { |
| 1964 |
p_new = ::realloc(p, size); |
| 1965 |
|
| 1966 |
if (p_new) |
| 1967 |
p_final_block = p_new; |
| 1968 |
} |
| 1969 |
|
| 1970 |
if (pActual_size) { |
| 1971 |
#ifdef WIN32 |
| 1972 |
*pActual_size = ::_msize(p_final_block); |
| 1973 |
#elif defined(__APPLE__) |
| 1974 |
*pActual_size = ::malloc_size(p_final_block); |
| 1975 |
#else |
| 1976 |
*pActual_size = ::malloc_usable_size(p_final_block); |
| 1977 |
#endif |
| 1978 |
} |
| 1979 |
} |
| 1980 |
|
| 1981 |
return p_new; |
| 1982 |
} |
| 1983 |
|
| 1984 |
static size_t crnd_default_msize(void* p, void* pUser_data) { |
| 1985 |
(void)pUser_data; |
| 1986 |
#ifdef WIN32 |
| 1987 |
return p ? _msize(p) : 0; |
| 1988 |
#elif defined(__APPLE__) |
| 1989 |
return p ? malloc_size(p) : 0; |
| 1990 |
#else |
| 1991 |
return p ? malloc_usable_size(p) : 0; |
| 1992 |
#endif |
| 1993 |
} |
| 1994 |
|
| 1995 |
static crnd_realloc_func g_pRealloc = crnd_default_realloc; |
| 1996 |
static crnd_msize_func g_pMSize = crnd_default_msize; |
| 1997 |
static void* g_pUser_data; |
| 1998 |
|
| 1999 |
void crnd_set_memory_callbacks(crnd_realloc_func pRealloc, crnd_msize_func pMSize, void* pUser_data) { |
| 2000 |
if ((!pRealloc) || (!pMSize)) { |
| 2001 |
g_pRealloc = crnd_default_realloc; |
| 2002 |
g_pMSize = crnd_default_msize; |
| 2003 |
g_pUser_data = NULL; |
| 2004 |
} else { |
| 2005 |
g_pRealloc = pRealloc; |
| 2006 |
g_pMSize = pMSize; |
| 2007 |
g_pUser_data = pUser_data; |
| 2008 |
} |
| 2009 |
} |
| 2010 |
|
| 2011 |
static inline void crnd_mem_error(const char* p_msg) { |
| 2012 |
crnd_assert(p_msg, __FILE__, __LINE__); |
| 2013 |
} |
| 2014 |
|
| 2015 |
void* crnd_malloc(size_t size, size_t* pActual_size) { |
| 2016 |
size = (size + sizeof(uint32) - 1U) & ~(sizeof(uint32) - 1U); |
| 2017 |
if (!size) |
| 2018 |
size = sizeof(uint32); |
| 2019 |
|
| 2020 |
if (size > MAX_POSSIBLE_BLOCK_SIZE) { |
| 2021 |
crnd_mem_error("crnd_malloc: size too big"); |
| 2022 |
return NULL; |
| 2023 |
} |
| 2024 |
|
| 2025 |
size_t actual_size = size; |
| 2026 |
uint8* p_new = static_cast<uint8*>((*g_pRealloc)(NULL, size, &actual_size, true, g_pUser_data)); |
| 2027 |
|
| 2028 |
if (pActual_size) |
| 2029 |
*pActual_size = actual_size; |
| 2030 |
|
| 2031 |
if ((!p_new) || (actual_size < size)) { |
| 2032 |
crnd_mem_error("crnd_malloc: out of memory"); |
| 2033 |
return NULL; |
| 2034 |
} |
| 2035 |
|
| 2036 |
CRND_ASSERT(((uint32) reinterpret_cast<uintptr_t>(p_new) & (CRND_MIN_ALLOC_ALIGNMENT - 1)) == 0); |
| 2037 |
|
| 2038 |
return p_new; |
| 2039 |
} |
| 2040 |
|
| 2041 |
void* crnd_realloc(void* p, size_t size, size_t* pActual_size, bool movable) { |
| 2042 |
if ((uint32) reinterpret_cast<uintptr_t>(p) & (CRND_MIN_ALLOC_ALIGNMENT - 1)) { |
| 2043 |
crnd_mem_error("crnd_realloc: bad ptr"); |
| 2044 |
return NULL; |
| 2045 |
} |
| 2046 |
|
| 2047 |
if (size > MAX_POSSIBLE_BLOCK_SIZE) { |
| 2048 |
crnd_mem_error("crnd_malloc: size too big"); |
| 2049 |
return NULL; |
| 2050 |
} |
| 2051 |
|
| 2052 |
size_t actual_size = size; |
| 2053 |
void* p_new = (*g_pRealloc)(p, size, &actual_size, movable, g_pUser_data); |
| 2054 |
|
| 2055 |
if (pActual_size) |
| 2056 |
*pActual_size = actual_size; |
| 2057 |
|
| 2058 |
CRND_ASSERT(((uint32) reinterpret_cast<uintptr_t>(p_new) & (CRND_MIN_ALLOC_ALIGNMENT - 1)) == 0); |
| 2059 |
|
| 2060 |
return p_new; |
| 2061 |
} |
| 2062 |
|
| 2063 |
void crnd_free(void* p) { |
| 2064 |
if (!p) |
| 2065 |
return; |
| 2066 |
|
| 2067 |
if ((uint32) reinterpret_cast<uintptr_t>(p) & (CRND_MIN_ALLOC_ALIGNMENT - 1)) { |
| 2068 |
crnd_mem_error("crnd_free: bad ptr"); |
| 2069 |
return; |
| 2070 |
} |
| 2071 |
|
| 2072 |
(*g_pRealloc)(p, 0, NULL, true, g_pUser_data); |
| 2073 |
} |
| 2074 |
|
| 2075 |
size_t crnd_msize(void* p) { |
| 2076 |
if (!p) |
| 2077 |
return 0; |
| 2078 |
|
| 2079 |
if ((uint32) reinterpret_cast<uintptr_t>(p) & (CRND_MIN_ALLOC_ALIGNMENT - 1)) { |
| 2080 |
crnd_mem_error("crnd_msize: bad ptr"); |
| 2081 |
return 0; |
| 2082 |
} |
| 2083 |
|
| 2084 |
return (*g_pMSize)(p, g_pUser_data); |
| 2085 |
} |
| 2086 |
|
| 2087 |
} |
| 2088 |
|
| 2089 |
|
| 2090 |
namespace crnd { |
| 2091 |
namespace math { |
| 2092 |
uint32 g_bitmasks[32] = |
| 2093 |
{ |
| 2094 |
1U << 0U, 1U << 1U, 1U << 2U, 1U << 3U, |
| 2095 |
1U << 4U, 1U << 5U, 1U << 6U, 1U << 7U, |
| 2096 |
1U << 8U, 1U << 9U, 1U << 10U, 1U << 11U, |
| 2097 |
1U << 12U, 1U << 13U, 1U << 14U, 1U << 15U, |
| 2098 |
1U << 16U, 1U << 17U, 1U << 18U, 1U << 19U, |
| 2099 |
1U << 20U, 1U << 21U, 1U << 22U, 1U << 23U, |
| 2100 |
1U << 24U, 1U << 25U, 1U << 26U, 1U << 27U, |
| 2101 |
1U << 28U, 1U << 29U, 1U << 30U, 1U << 31U}; |
| 2102 |
|
| 2103 |
} |
| 2104 |
} |
| 2105 |
|
| 2106 |
|
| 2107 |
namespace crnd { |
| 2108 |
#define CRND_FOURCC(a, b, c, d) ((a) | ((b) << 8U) | ((c) << 16U) | ((d) << 24U)) |
| 2109 |
|
| 2110 |
uint32 crnd_crn_format_to_fourcc(crn_format fmt) { |
| 2111 |
switch (fmt) { |
| 2112 |
case cCRNFmtDXT1: |
| 2113 |
return CRND_FOURCC('D', 'X', 'T', '1'); |
| 2114 |
case cCRNFmtDXT3: |
| 2115 |
return CRND_FOURCC('D', 'X', 'T', '3'); |
| 2116 |
case cCRNFmtDXT5: |
| 2117 |
return CRND_FOURCC('D', 'X', 'T', '5'); |
| 2118 |
case cCRNFmtDXN_XY: |
| 2119 |
return CRND_FOURCC('A', '2', 'X', 'Y'); |
| 2120 |
case cCRNFmtDXN_YX: |
| 2121 |
return CRND_FOURCC('A', 'T', 'I', '2'); |
| 2122 |
case cCRNFmtDXT5A: |
| 2123 |
return CRND_FOURCC('A', 'T', 'I', '1'); |
| 2124 |
case cCRNFmtDXT5_CCxY: |
| 2125 |
return CRND_FOURCC('C', 'C', 'x', 'Y'); |
| 2126 |
case cCRNFmtDXT5_xGxR: |
| 2127 |
return CRND_FOURCC('x', 'G', 'x', 'R'); |
| 2128 |
case cCRNFmtDXT5_xGBR: |
| 2129 |
return CRND_FOURCC('x', 'G', 'B', 'R'); |
| 2130 |
case cCRNFmtDXT5_AGBR: |
| 2131 |
return CRND_FOURCC('A', 'G', 'B', 'R'); |
| 2132 |
case cCRNFmtETC1: |
| 2133 |
return CRND_FOURCC('E', 'T', 'C', '1'); |
| 2134 |
case cCRNFmtETC2: |
| 2135 |
return CRND_FOURCC('E', 'T', 'C', '2'); |
| 2136 |
case cCRNFmtETC2A: |
| 2137 |
return CRND_FOURCC('E', 'T', '2', 'A'); |
| 2138 |
case cCRNFmtETC1S: |
| 2139 |
return CRND_FOURCC('E', 'T', '1', 'S'); |
| 2140 |
case cCRNFmtETC2AS: |
| 2141 |
return CRND_FOURCC('E', '2', 'A', 'S'); |
| 2142 |
default: |
| 2143 |
break; |
| 2144 |
} |
| 2145 |
CRND_ASSERT(false); |
| 2146 |
return 0; |
| 2147 |
} |
| 2148 |
|
| 2149 |
crn_format crnd_get_fundamental_dxt_format(crn_format fmt) { |
| 2150 |
switch (fmt) { |
| 2151 |
case cCRNFmtDXT5_CCxY: |
| 2152 |
case cCRNFmtDXT5_xGxR: |
| 2153 |
case cCRNFmtDXT5_xGBR: |
| 2154 |
case cCRNFmtDXT5_AGBR: |
| 2155 |
return cCRNFmtDXT5; |
| 2156 |
default: |
| 2157 |
break; |
| 2158 |
} |
| 2159 |
return fmt; |
| 2160 |
} |
| 2161 |
|
| 2162 |
uint32 crnd_get_crn_format_bits_per_texel(crn_format fmt) { |
| 2163 |
switch (fmt) { |
| 2164 |
case cCRNFmtDXT1: |
| 2165 |
case cCRNFmtDXT5A: |
| 2166 |
case cCRNFmtETC1: |
| 2167 |
case cCRNFmtETC2: |
| 2168 |
case cCRNFmtETC1S: |
| 2169 |
return 4; |
| 2170 |
case cCRNFmtDXT3: |
| 2171 |
case cCRNFmtDXT5: |
| 2172 |
case cCRNFmtDXN_XY: |
| 2173 |
case cCRNFmtDXN_YX: |
| 2174 |
case cCRNFmtDXT5_CCxY: |
| 2175 |
case cCRNFmtDXT5_xGxR: |
| 2176 |
case cCRNFmtDXT5_xGBR: |
| 2177 |
case cCRNFmtDXT5_AGBR: |
| 2178 |
case cCRNFmtETC2A: |
| 2179 |
case cCRNFmtETC2AS: |
| 2180 |
return 8; |
| 2181 |
default: |
| 2182 |
break; |
| 2183 |
} |
| 2184 |
CRND_ASSERT(false); |
| 2185 |
return 0; |
| 2186 |
} |
| 2187 |
|
| 2188 |
uint32 crnd_get_bytes_per_dxt_block(crn_format fmt) { |
| 2189 |
return (crnd_get_crn_format_bits_per_texel(fmt) << 4) >> 3; |
| 2190 |
} |
| 2191 |
|
| 2192 |
|
| 2193 |
const crn_header* crnd_get_header(const void* pData, uint32 data_size) { |
| 2194 |
if ((!pData) || (data_size < sizeof(crn_header))) |
| 2195 |
return NULL; |
| 2196 |
|
| 2197 |
const crn_header& file_header = *static_cast<const crn_header*>(pData); |
| 2198 |
if (file_header.m_sig != crn_header::cCRNSigValue) |
| 2199 |
return NULL; |
| 2200 |
|
| 2201 |
if ((file_header.m_header_size < sizeof(crn_header)) || (data_size < file_header.m_data_size)) |
| 2202 |
return NULL; |
| 2203 |
|
| 2204 |
return &file_header; |
| 2205 |
} |
| 2206 |
|
| 2207 |
bool crnd_validate_header(const void* pData, uint32 data_size, crn_file_info* pFile_info) { |
| 2208 |
if (pFile_info) { |
| 2209 |
if (pFile_info->m_struct_size != sizeof(crn_file_info)) |
| 2210 |
return false; |
| 2211 |
|
| 2212 |
memset(&pFile_info->m_struct_size + 1, 0, sizeof(crn_file_info) - sizeof(pFile_info->m_struct_size)); |
| 2213 |
} |
| 2214 |
|
| 2215 |
if ((!pData) || (data_size < cCRNHeaderMinSize)) |
| 2216 |
return false; |
| 2217 |
|
| 2218 |
const crn_header* pHeader = crnd_get_header(pData, data_size); |
| 2219 |
if (!pHeader) |
| 2220 |
return false; |
| 2221 |
|
| 2222 |
const uint32 header_crc = crc16(&pHeader->m_data_size, (uint32)(pHeader->m_header_size - ((const uint8*)&pHeader->m_data_size - (const uint8*)pHeader))); |
| 2223 |
if (header_crc != pHeader->m_header_crc16) |
| 2224 |
return false; |
| 2225 |
|
| 2226 |
if (pHeader->m_data_size < pHeader->m_header_size) |
| 2227 |
return false; |
| 2228 |
if ((pHeader->m_faces != 1) && (pHeader->m_faces != 6)) |
| 2229 |
return false; |
| 2230 |
if ((pHeader->m_width < 1) || (pHeader->m_width > cCRNMaxLevelResolution)) |
| 2231 |
return false; |
| 2232 |
if ((pHeader->m_height < 1) || (pHeader->m_height > cCRNMaxLevelResolution)) |
| 2233 |
return false; |
| 2234 |
if ((pHeader->m_levels < 1) || (pHeader->m_levels > utils::compute_max_mips(pHeader->m_width, pHeader->m_height))) |
| 2235 |
return false; |
| 2236 |
if (((int)pHeader->m_format < cCRNFmtDXT1) || ((int)pHeader->m_format >= cCRNFmtTotal)) |
| 2237 |
return false; |
| 2238 |
|
| 2239 |
if (pFile_info) { |
| 2240 |
pFile_info->m_actual_data_size = pHeader->m_data_size; |
| 2241 |
pFile_info->m_header_size = pHeader->m_header_size; |
| 2242 |
pFile_info->m_total_palette_size = pHeader->m_color_endpoints.m_size + pHeader->m_color_selectors.m_size + pHeader->m_alpha_endpoints.m_size + pHeader->m_alpha_selectors.m_size; |
| 2243 |
pFile_info->m_tables_size = pHeader->m_tables_size; |
| 2244 |
|
| 2245 |
pFile_info->m_levels = pHeader->m_levels; |
| 2246 |
|
| 2247 |
for (uint32 i = 0; i < pHeader->m_levels; i++) { |
| 2248 |
uint32 next_ofs = pHeader->m_data_size; |
| 2249 |
|
| 2250 |
|
| 2251 |
if ((i + 1) < pHeader->m_levels) |
| 2252 |
next_ofs = pHeader->m_level_ofs[i + 1]; |
| 2253 |
|
| 2254 |
pFile_info->m_level_compressed_size[i] = next_ofs - pHeader->m_level_ofs[i]; |
| 2255 |
} |
| 2256 |
|
| 2257 |
pFile_info->m_color_endpoint_palette_entries = pHeader->m_color_endpoints.m_num; |
| 2258 |
pFile_info->m_color_selector_palette_entries = pHeader->m_color_selectors.m_num; |
| 2259 |
; |
| 2260 |
pFile_info->m_alpha_endpoint_palette_entries = pHeader->m_alpha_endpoints.m_num; |
| 2261 |
; |
| 2262 |
pFile_info->m_alpha_selector_palette_entries = pHeader->m_alpha_selectors.m_num; |
| 2263 |
; |
| 2264 |
} |
| 2265 |
|
| 2266 |
return true; |
| 2267 |
} |
| 2268 |
|
| 2269 |
bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFile_info) { |
| 2270 |
if (!crnd_validate_header(pData, data_size, pFile_info)) |
| 2271 |
return false; |
| 2272 |
|
| 2273 |
|
| 2274 |
const crn_header* pHeader = static_cast<const crn_header*>(pData); |
| 2275 |
|
| 2276 |
if (pHeader->m_data_size > data_size) |
| 2277 |
return false; |
| 2278 |
|
| 2279 |
const uint32 data_crc = crc16((const uint8*)pData + pHeader->m_header_size, pHeader->m_data_size - pHeader->m_header_size); |
| 2280 |
return data_crc == pHeader->m_data_crc16; |
| 2281 |
} |
| 2282 |
|
| 2283 |
bool crnd_get_texture_info(const void* pData, uint32 data_size, crn_texture_info* pInfo) { |
| 2284 |
if ((!pData) || (data_size < sizeof(crn_header)) || (!pInfo)) |
| 2285 |
return false; |
| 2286 |
|
| 2287 |
if (pInfo->m_struct_size != sizeof(crn_texture_info)) |
| 2288 |
return false; |
| 2289 |
|
| 2290 |
const crn_header* pHeader = crnd_get_header(pData, data_size); |
| 2291 |
if (!pHeader) |
| 2292 |
return false; |
| 2293 |
|
| 2294 |
pInfo->m_width = pHeader->m_width; |
| 2295 |
pInfo->m_height = pHeader->m_height; |
| 2296 |
pInfo->m_levels = pHeader->m_levels; |
| 2297 |
pInfo->m_faces = pHeader->m_faces; |
| 2298 |
pInfo->m_format = static_cast<crn_format>((uint32)pHeader->m_format); |
| 2299 |
pInfo->m_bytes_per_block = pHeader->m_format == cCRNFmtDXT1 || pHeader->m_format == cCRNFmtDXT5A || pHeader->m_format == cCRNFmtETC1 || pHeader->m_format == cCRNFmtETC2 || pHeader->m_format == cCRNFmtETC1S ? 8 : 16; |
| 2300 |
pInfo->m_userdata0 = pHeader->m_userdata0; |
| 2301 |
pInfo->m_userdata1 = pHeader->m_userdata1; |
| 2302 |
|
| 2303 |
return true; |
| 2304 |
} |
| 2305 |
|
| 2306 |
bool crnd_get_level_info(const void* pData, uint32 data_size, uint32 level_index, crn_level_info* pLevel_info) { |
| 2307 |
if ((!pData) || (data_size < cCRNHeaderMinSize) || (!pLevel_info)) |
| 2308 |
return false; |
| 2309 |
|
| 2310 |
if (pLevel_info->m_struct_size != sizeof(crn_level_info)) |
| 2311 |
return false; |
| 2312 |
|
| 2313 |
const crn_header* pHeader = crnd_get_header(pData, data_size); |
| 2314 |
if (!pHeader) |
| 2315 |
return false; |
| 2316 |
|
| 2317 |
if (level_index >= pHeader->m_levels) |
| 2318 |
return false; |
| 2319 |
|
| 2320 |
uint32 width = math::maximum<uint32>(1U, pHeader->m_width >> level_index); |
| 2321 |
uint32 height = math::maximum<uint32>(1U, pHeader->m_height >> level_index); |
| 2322 |
|
| 2323 |
pLevel_info->m_width = width; |
| 2324 |
pLevel_info->m_height = height; |
| 2325 |
pLevel_info->m_faces = pHeader->m_faces; |
| 2326 |
pLevel_info->m_blocks_x = (width + 3) >> 2; |
| 2327 |
pLevel_info->m_blocks_y = (height + 3) >> 2; |
| 2328 |
pLevel_info->m_bytes_per_block = ((pHeader->m_format == cCRNFmtDXT1) || (pHeader->m_format == cCRNFmtDXT5A)) ? 8 : 16; |
| 2329 |
pLevel_info->m_format = static_cast<crn_format>((uint32)pHeader->m_format); |
| 2330 |
|
| 2331 |
return true; |
| 2332 |
} |
| 2333 |
|
| 2334 |
const void* crnd_get_level_data(const void* pData, uint32 data_size, uint32 level_index, uint32* pSize) { |
| 2335 |
if (pSize) |
| 2336 |
*pSize = 0; |
| 2337 |
|
| 2338 |
if ((!pData) || (data_size < cCRNHeaderMinSize)) |
| 2339 |
return NULL; |
| 2340 |
|
| 2341 |
const crn_header* pHeader = crnd_get_header(pData, data_size); |
| 2342 |
if (!pHeader) |
| 2343 |
return NULL; |
| 2344 |
|
| 2345 |
if (level_index >= pHeader->m_levels) |
| 2346 |
return NULL; |
| 2347 |
|
| 2348 |
uint32 cur_level_ofs = pHeader->m_level_ofs[level_index]; |
| 2349 |
|
| 2350 |
if (pSize) { |
| 2351 |
uint32 next_level_ofs = data_size; |
| 2352 |
if ((level_index + 1) < (pHeader->m_levels)) |
| 2353 |
next_level_ofs = pHeader->m_level_ofs[level_index + 1]; |
| 2354 |
|
| 2355 |
*pSize = next_level_ofs - cur_level_ofs; |
| 2356 |
} |
| 2357 |
|
| 2358 |
return static_cast<const uint8*>(pData) + cur_level_ofs; |
| 2359 |
} |
| 2360 |
|
| 2361 |
uint32 crnd_get_segmented_file_size(const void* pData, uint32 data_size) { |
| 2362 |
if ((!pData) || (data_size < cCRNHeaderMinSize)) |
| 2363 |
return false; |
| 2364 |
|
| 2365 |
const crn_header* pHeader = crnd_get_header(pData, data_size); |
| 2366 |
if (!pHeader) |
| 2367 |
return false; |
| 2368 |
|
| 2369 |
uint32 size = pHeader->m_header_size; |
| 2370 |
|
| 2371 |
size = math::maximum(size, pHeader->m_color_endpoints.m_ofs + pHeader->m_color_endpoints.m_size); |
| 2372 |
size = math::maximum(size, pHeader->m_color_selectors.m_ofs + pHeader->m_color_selectors.m_size); |
| 2373 |
size = math::maximum(size, pHeader->m_alpha_endpoints.m_ofs + pHeader->m_alpha_endpoints.m_size); |
| 2374 |
size = math::maximum(size, pHeader->m_alpha_selectors.m_ofs + pHeader->m_alpha_selectors.m_size); |
| 2375 |
size = math::maximum(size, pHeader->m_tables_ofs + pHeader->m_tables_size); |
| 2376 |
|
| 2377 |
return size; |
| 2378 |
} |
| 2379 |
|
| 2380 |
bool crnd_create_segmented_file(const void* pData, uint32 data_size, void* pBase_data, uint base_data_size) { |
| 2381 |
if ((!pData) || (data_size < cCRNHeaderMinSize)) |
| 2382 |
return false; |
| 2383 |
|
| 2384 |
const crn_header* pHeader = crnd_get_header(pData, data_size); |
| 2385 |
if (!pHeader) |
| 2386 |
return false; |
| 2387 |
|
| 2388 |
if (pHeader->m_flags & cCRNHeaderFlagSegmented) |
| 2389 |
return false; |
| 2390 |
|
| 2391 |
const uint actual_base_data_size = crnd_get_segmented_file_size(pData, data_size); |
| 2392 |
if (base_data_size < actual_base_data_size) |
| 2393 |
return false; |
| 2394 |
|
| 2395 |
memcpy(pBase_data, pData, actual_base_data_size); |
| 2396 |
|
| 2397 |
crn_header& new_header = *static_cast<crn_header*>(pBase_data); |
| 2398 |
new_header.m_flags = new_header.m_flags | cCRNHeaderFlagSegmented; |
| 2399 |
new_header.m_data_size = actual_base_data_size; |
| 2400 |
|
| 2401 |
new_header.m_data_crc16 = crc16((const uint8*)pBase_data + new_header.m_header_size, new_header.m_data_size - new_header.m_header_size); |
| 2402 |
|
| 2403 |
new_header.m_header_crc16 = crc16(&new_header.m_data_size, new_header.m_header_size - (uint32)((const uint8*)&new_header.m_data_size - (const uint8*)&new_header)); |
| 2404 |
|
| 2405 |
CRND_ASSERT(crnd_validate_file(&new_header, actual_base_data_size, NULL)); |
| 2406 |
|
| 2407 |
return true; |
| 2408 |
} |
| 2409 |
|
| 2410 |
} |
| 2411 |
|
| 2412 |
|
| 2413 |
namespace crnd { |
| 2414 |
static_huffman_data_model::static_huffman_data_model() |
| 2415 |
: m_total_syms(0), |
| 2416 |
m_pDecode_tables(NULL) { |
| 2417 |
} |
| 2418 |
|
| 2419 |
static_huffman_data_model::static_huffman_data_model(const static_huffman_data_model& other) |
| 2420 |
: m_total_syms(0), |
| 2421 |
m_pDecode_tables(NULL) { |
| 2422 |
*this = other; |
| 2423 |
} |
| 2424 |
|
| 2425 |
static_huffman_data_model::~static_huffman_data_model() { |
| 2426 |
if (m_pDecode_tables) |
| 2427 |
crnd_delete(m_pDecode_tables); |
| 2428 |
} |
| 2429 |
|
| 2430 |
static_huffman_data_model& static_huffman_data_model::operator=(const static_huffman_data_model& rhs) { |
| 2431 |
if (this == &rhs) |
| 2432 |
return *this; |
| 2433 |
|
| 2434 |
m_total_syms = rhs.m_total_syms; |
| 2435 |
m_code_sizes = rhs.m_code_sizes; |
| 2436 |
if (m_code_sizes.get_alloc_failed()) { |
| 2437 |
clear(); |
| 2438 |
return *this; |
| 2439 |
} |
| 2440 |
|
| 2441 |
if (rhs.m_pDecode_tables) { |
| 2442 |
if (m_pDecode_tables) |
| 2443 |
*m_pDecode_tables = *rhs.m_pDecode_tables; |
| 2444 |
else |
| 2445 |
m_pDecode_tables = crnd_new<prefix_coding::decoder_tables>(*rhs.m_pDecode_tables); |
| 2446 |
} else { |
| 2447 |
crnd_delete(m_pDecode_tables); |
| 2448 |
m_pDecode_tables = NULL; |
| 2449 |
} |
| 2450 |
|
| 2451 |
return *this; |
| 2452 |
} |
| 2453 |
|
| 2454 |
void static_huffman_data_model::clear() { |
| 2455 |
m_total_syms = 0; |
| 2456 |
m_code_sizes.clear(); |
| 2457 |
if (m_pDecode_tables) { |
| 2458 |
crnd_delete(m_pDecode_tables); |
| 2459 |
m_pDecode_tables = NULL; |
| 2460 |
} |
| 2461 |
} |
| 2462 |
|
| 2463 |
bool static_huffman_data_model::init(uint32 total_syms, const uint8* pCode_sizes, uint32 code_size_limit) { |
| 2464 |
CRND_ASSERT((total_syms >= 1) && (total_syms <= prefix_coding::cMaxSupportedSyms) && (code_size_limit >= 1)); |
| 2465 |
|
| 2466 |
code_size_limit = math::minimum(code_size_limit, prefix_coding::cMaxExpectedCodeSize); |
| 2467 |
|
| 2468 |
if (!m_code_sizes.resize(total_syms)) |
| 2469 |
return false; |
| 2470 |
|
| 2471 |
uint32 min_code_size = cUINT32_MAX; |
| 2472 |
uint32 max_code_size = 0; |
| 2473 |
|
| 2474 |
for (uint32 i = 0; i < total_syms; i++) { |
| 2475 |
uint32 s = pCode_sizes[i]; |
| 2476 |
m_code_sizes[i] = static_cast<uint8>(s); |
| 2477 |
min_code_size = math::minimum(min_code_size, s); |
| 2478 |
max_code_size = math::maximum(max_code_size, s); |
| 2479 |
} |
| 2480 |
|
| 2481 |
if ((max_code_size < 1) || (max_code_size > 32) || (min_code_size > code_size_limit)) |
| 2482 |
return false; |
| 2483 |
|
| 2484 |
if (max_code_size > code_size_limit) |
| 2485 |
return false; |
| 2486 |
|
| 2487 |
if (!m_pDecode_tables) |
| 2488 |
m_pDecode_tables = crnd_new<prefix_coding::decoder_tables>(); |
| 2489 |
|
| 2490 |
if (!m_pDecode_tables->init(m_total_syms, &m_code_sizes[0], compute_decoder_table_bits())) |
| 2491 |
return false; |
| 2492 |
|
| 2493 |
return true; |
| 2494 |
} |
| 2495 |
|
| 2496 |
bool static_huffman_data_model::prepare_decoder_tables() { |
| 2497 |
uint32 total_syms = m_code_sizes.size(); |
| 2498 |
|
| 2499 |
CRND_ASSERT((total_syms >= 1) && (total_syms <= prefix_coding::cMaxSupportedSyms)); |
| 2500 |
|
| 2501 |
m_total_syms = total_syms; |
| 2502 |
|
| 2503 |
if (!m_pDecode_tables) |
| 2504 |
m_pDecode_tables = crnd_new<prefix_coding::decoder_tables>(); |
| 2505 |
|
| 2506 |
return m_pDecode_tables->init(m_total_syms, &m_code_sizes[0], compute_decoder_table_bits()); |
| 2507 |
} |
| 2508 |
|
| 2509 |
uint static_huffman_data_model::compute_decoder_table_bits() const { |
| 2510 |
#if CRND_PREFIX_CODING_USE_FIXED_TABLE_SIZE |
| 2511 |
return prefix_coding::cMaxTableBits; |
| 2512 |
#else |
| 2513 |
uint32 decoder_table_bits = 0; |
| 2514 |
if (m_total_syms > 16) |
| 2515 |
decoder_table_bits = static_cast<uint8>(math::minimum(1 + math::ceil_log2i(m_total_syms), prefix_coding::cMaxTableBits)); |
| 2516 |
return decoder_table_bits; |
| 2517 |
#endif |
| 2518 |
} |
| 2519 |
|
| 2520 |
symbol_codec::symbol_codec() |
| 2521 |
: m_pDecode_buf(NULL), |
| 2522 |
m_pDecode_buf_next(NULL), |
| 2523 |
m_pDecode_buf_end(NULL), |
| 2524 |
m_decode_buf_size(0), |
| 2525 |
m_bit_buf(0), |
| 2526 |
m_bit_count(0) { |
| 2527 |
} |
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
const uint32 cMaxCodelengthCodes = 21; |
| 2532 |
|
| 2533 |
const uint32 cSmallZeroRunCode = 17; |
| 2534 |
const uint32 cLargeZeroRunCode = 18; |
| 2535 |
const uint32 cSmallRepeatCode = 19; |
| 2536 |
const uint32 cLargeRepeatCode = 20; |
| 2537 |
|
| 2538 |
const uint32 cMinSmallZeroRunSize = 3; |
| 2539 |
const uint32 cMaxSmallZeroRunSize = 10; |
| 2540 |
const uint32 cMinLargeZeroRunSize = 11; |
| 2541 |
const uint32 cMaxLargeZeroRunSize = 138; |
| 2542 |
|
| 2543 |
const uint32 cSmallMinNonZeroRunSize = 3; |
| 2544 |
const uint32 cSmallMaxNonZeroRunSize = 6; |
| 2545 |
const uint32 cLargeMinNonZeroRunSize = 7; |
| 2546 |
const uint32 cLargeMaxNonZeroRunSize = 70; |
| 2547 |
|
| 2548 |
const uint32 cSmallZeroRunExtraBits = 3; |
| 2549 |
const uint32 cLargeZeroRunExtraBits = 7; |
| 2550 |
const uint32 cSmallNonZeroRunExtraBits = 2; |
| 2551 |
const uint32 cLargeNonZeroRunExtraBits = 6; |
| 2552 |
|
| 2553 |
static const uint8 g_most_probable_codelength_codes[] = |
| 2554 |
{ |
| 2555 |
cSmallZeroRunCode, cLargeZeroRunCode, |
| 2556 |
cSmallRepeatCode, cLargeRepeatCode, |
| 2557 |
|
| 2558 |
0, 8, |
| 2559 |
7, 9, |
| 2560 |
6, 10, |
| 2561 |
5, 11, |
| 2562 |
4, 12, |
| 2563 |
3, 13, |
| 2564 |
2, 14, |
| 2565 |
1, 15, |
| 2566 |
16}; |
| 2567 |
const uint32 cNumMostProbableCodelengthCodes = sizeof(g_most_probable_codelength_codes) / sizeof(g_most_probable_codelength_codes[0]); |
| 2568 |
|
| 2569 |
bool symbol_codec::decode_receive_static_data_model(static_huffman_data_model& model) { |
| 2570 |
const uint32 total_used_syms = decode_bits(math::total_bits(prefix_coding::cMaxSupportedSyms)); |
| 2571 |
|
| 2572 |
if (!total_used_syms) { |
| 2573 |
model.clear(); |
| 2574 |
return true; |
| 2575 |
} |
| 2576 |
|
| 2577 |
if (!model.m_code_sizes.resize(total_used_syms)) |
| 2578 |
return false; |
| 2579 |
|
| 2580 |
memset(&model.m_code_sizes[0], 0, sizeof(model.m_code_sizes[0]) * total_used_syms); |
| 2581 |
|
| 2582 |
const uint32 num_codelength_codes_to_send = decode_bits(5); |
| 2583 |
if ((num_codelength_codes_to_send < 1) || (num_codelength_codes_to_send > cMaxCodelengthCodes)) |
| 2584 |
return false; |
| 2585 |
|
| 2586 |
static_huffman_data_model dm; |
| 2587 |
if (!dm.m_code_sizes.resize(cMaxCodelengthCodes)) |
| 2588 |
return false; |
| 2589 |
|
| 2590 |
for (uint32 i = 0; i < num_codelength_codes_to_send; i++) |
| 2591 |
dm.m_code_sizes[g_most_probable_codelength_codes[i]] = static_cast<uint8>(decode_bits(3)); |
| 2592 |
|
| 2593 |
if (!dm.prepare_decoder_tables()) |
| 2594 |
return false; |
| 2595 |
|
| 2596 |
uint32 ofs = 0; |
| 2597 |
while (ofs < total_used_syms) { |
| 2598 |
const uint32 num_remaining = total_used_syms - ofs; |
| 2599 |
|
| 2600 |
uint32 code = decode(dm); |
| 2601 |
if (code <= 16) |
| 2602 |
model.m_code_sizes[ofs++] = static_cast<uint8>(code); |
| 2603 |
else if (code == cSmallZeroRunCode) { |
| 2604 |
uint32 len = decode_bits(cSmallZeroRunExtraBits) + cMinSmallZeroRunSize; |
| 2605 |
if (len > num_remaining) |
| 2606 |
return false; |
| 2607 |
ofs += len; |
| 2608 |
} else if (code == cLargeZeroRunCode) { |
| 2609 |
uint32 len = decode_bits(cLargeZeroRunExtraBits) + cMinLargeZeroRunSize; |
| 2610 |
if (len > num_remaining) |
| 2611 |
return false; |
| 2612 |
ofs += len; |
| 2613 |
} else if ((code == cSmallRepeatCode) || (code == cLargeRepeatCode)) { |
| 2614 |
uint32 len; |
| 2615 |
if (code == cSmallRepeatCode) |
| 2616 |
len = decode_bits(cSmallNonZeroRunExtraBits) + cSmallMinNonZeroRunSize; |
| 2617 |
else |
| 2618 |
len = decode_bits(cLargeNonZeroRunExtraBits) + cLargeMinNonZeroRunSize; |
| 2619 |
|
| 2620 |
if ((!ofs) || (len > num_remaining)) |
| 2621 |
return false; |
| 2622 |
const uint32 prev = model.m_code_sizes[ofs - 1]; |
| 2623 |
if (!prev) |
| 2624 |
return false; |
| 2625 |
const uint32 end = ofs + len; |
| 2626 |
while (ofs < end) |
| 2627 |
model.m_code_sizes[ofs++] = static_cast<uint8>(prev); |
| 2628 |
} else { |
| 2629 |
CRND_ASSERT(0); |
| 2630 |
return false; |
| 2631 |
} |
| 2632 |
} |
| 2633 |
|
| 2634 |
if (ofs != total_used_syms) |
| 2635 |
return false; |
| 2636 |
|
| 2637 |
return model.prepare_decoder_tables(); |
| 2638 |
} |
| 2639 |
|
| 2640 |
bool symbol_codec::start_decoding(const uint8* pBuf, uint32 buf_size) { |
| 2641 |
if (!buf_size) |
| 2642 |
return false; |
| 2643 |
|
| 2644 |
m_pDecode_buf = pBuf; |
| 2645 |
m_pDecode_buf_next = pBuf; |
| 2646 |
m_decode_buf_size = buf_size; |
| 2647 |
m_pDecode_buf_end = pBuf + buf_size; |
| 2648 |
|
| 2649 |
get_bits_init(); |
| 2650 |
|
| 2651 |
return true; |
| 2652 |
} |
| 2653 |
|
| 2654 |
void symbol_codec::get_bits_init() { |
| 2655 |
m_bit_buf = 0; |
| 2656 |
m_bit_count = 0; |
| 2657 |
} |
| 2658 |
|
| 2659 |
uint32 symbol_codec::decode_bits(uint32 num_bits) { |
| 2660 |
if (!num_bits) |
| 2661 |
return 0; |
| 2662 |
|
| 2663 |
if (num_bits > 16) { |
| 2664 |
uint32 a = get_bits(num_bits - 16); |
| 2665 |
uint32 b = get_bits(16); |
| 2666 |
|
| 2667 |
return (a << 16) | b; |
| 2668 |
} else |
| 2669 |
return get_bits(num_bits); |
| 2670 |
} |
| 2671 |
|
| 2672 |
uint32 symbol_codec::get_bits(uint32 num_bits) { |
| 2673 |
CRND_ASSERT(num_bits <= 32U); |
| 2674 |
|
| 2675 |
while (m_bit_count < (int)num_bits) { |
| 2676 |
bit_buf_type c = 0; |
| 2677 |
if (m_pDecode_buf_next != m_pDecode_buf_end) |
| 2678 |
c = *m_pDecode_buf_next++; |
| 2679 |
|
| 2680 |
m_bit_count += 8; |
| 2681 |
CRND_ASSERT(m_bit_count <= cBitBufSize); |
| 2682 |
|
| 2683 |
m_bit_buf |= (c << (cBitBufSize - m_bit_count)); |
| 2684 |
} |
| 2685 |
|
| 2686 |
uint32 result = static_cast<uint32>(m_bit_buf >> (cBitBufSize - num_bits)); |
| 2687 |
|
| 2688 |
m_bit_buf <<= num_bits; |
| 2689 |
m_bit_count -= num_bits; |
| 2690 |
|
| 2691 |
return result; |
| 2692 |
} |
| 2693 |
|
| 2694 |
uint32 symbol_codec::decode(const static_huffman_data_model& model) { |
| 2695 |
const prefix_coding::decoder_tables* pTables = model.m_pDecode_tables; |
| 2696 |
|
| 2697 |
if (m_bit_count < 24) { |
| 2698 |
if (m_bit_count < 16) { |
| 2699 |
uint32 c0 = 0, c1 = 0; |
| 2700 |
const uint8* p = m_pDecode_buf_next; |
| 2701 |
if (p < m_pDecode_buf_end) |
| 2702 |
c0 = *p++; |
| 2703 |
if (p < m_pDecode_buf_end) |
| 2704 |
c1 = *p++; |
| 2705 |
m_pDecode_buf_next = p; |
| 2706 |
m_bit_count += 16; |
| 2707 |
uint32 c = (c0 << 8) | c1; |
| 2708 |
m_bit_buf |= (c << (32 - m_bit_count)); |
| 2709 |
} else { |
| 2710 |
uint32 c = (m_pDecode_buf_next < m_pDecode_buf_end) ? *m_pDecode_buf_next++ : 0; |
| 2711 |
m_bit_count += 8; |
| 2712 |
m_bit_buf |= (c << (32 - m_bit_count)); |
| 2713 |
} |
| 2714 |
} |
| 2715 |
|
| 2716 |
uint32 k = (m_bit_buf >> 16) + 1; |
| 2717 |
uint32 sym, len; |
| 2718 |
|
| 2719 |
if (k <= pTables->m_table_max_code) { |
| 2720 |
uint32 t = pTables->m_lookup[m_bit_buf >> (32 - pTables->m_table_bits)]; |
| 2721 |
|
| 2722 |
CRND_ASSERT(t != cUINT32_MAX); |
| 2723 |
sym = t & cUINT16_MAX; |
| 2724 |
len = t >> 16; |
| 2725 |
|
| 2726 |
CRND_ASSERT(model.m_code_sizes[sym] == len); |
| 2727 |
} else { |
| 2728 |
len = pTables->m_decode_start_code_size; |
| 2729 |
|
| 2730 |
for (;;) { |
| 2731 |
if (k <= pTables->m_max_codes[len - 1]) |
| 2732 |
break; |
| 2733 |
len++; |
| 2734 |
} |
| 2735 |
|
| 2736 |
int val_ptr = pTables->m_val_ptrs[len - 1] + (m_bit_buf >> (32 - len)); |
| 2737 |
|
| 2738 |
if (((uint32)val_ptr >= model.m_total_syms)) { |
| 2739 |
|
| 2740 |
CRND_ASSERT(0); |
| 2741 |
return 0; |
| 2742 |
} |
| 2743 |
|
| 2744 |
sym = pTables->m_sorted_symbol_order[val_ptr]; |
| 2745 |
} |
| 2746 |
|
| 2747 |
m_bit_buf <<= len; |
| 2748 |
m_bit_count -= len; |
| 2749 |
|
| 2750 |
return sym; |
| 2751 |
} |
| 2752 |
|
| 2753 |
uint64 symbol_codec::stop_decoding() { |
| 2754 |
return static_cast<uint64>(m_pDecode_buf_next - m_pDecode_buf); |
| 2755 |
} |
| 2756 |
|
| 2757 |
} |
| 2758 |
|
| 2759 |
|
| 2760 |
namespace crnd { |
| 2761 |
const uint8 g_dxt1_to_linear[cDXT1SelectorValues] = {0U, 3U, 1U, 2U}; |
| 2762 |
const uint8 g_dxt1_from_linear[cDXT1SelectorValues] = {0U, 2U, 3U, 1U}; |
| 2763 |
const uint8 g_etc1_from_linear[cDXT1SelectorValues] = {3U, 2U, 0U, 1U}; |
| 2764 |
|
| 2765 |
const uint8 g_dxt5_to_linear[cDXT5SelectorValues] = {0U, 7U, 1U, 2U, 3U, 4U, 5U, 6U}; |
| 2766 |
const uint8 g_dxt5_from_linear[cDXT5SelectorValues] = {0U, 2U, 3U, 4U, 5U, 6U, 7U, 1U}; |
| 2767 |
|
| 2768 |
const uint8 g_six_alpha_invert_table[cDXT5SelectorValues] = {1, 0, 5, 4, 3, 2, 6, 7}; |
| 2769 |
const uint8 g_eight_alpha_invert_table[cDXT5SelectorValues] = {1, 0, 7, 6, 5, 4, 3, 2}; |
| 2770 |
|
| 2771 |
uint16 dxt1_block::pack_color(const color_quad_u8& color, bool scaled, uint32 bias) { |
| 2772 |
uint32 r = color.r; |
| 2773 |
uint32 g = color.g; |
| 2774 |
uint32 b = color.b; |
| 2775 |
|
| 2776 |
if (scaled) { |
| 2777 |
r = (r * 31U + bias) / 255U; |
| 2778 |
g = (g * 63U + bias) / 255U; |
| 2779 |
b = (b * 31U + bias) / 255U; |
| 2780 |
} |
| 2781 |
|
| 2782 |
r = math::minimum(r, 31U); |
| 2783 |
g = math::minimum(g, 63U); |
| 2784 |
b = math::minimum(b, 31U); |
| 2785 |
|
| 2786 |
return static_cast<uint16>(b | (g << 5U) | (r << 11U)); |
| 2787 |
} |
| 2788 |
|
| 2789 |
uint16 dxt1_block::pack_color(uint32 r, uint32 g, uint32 b, bool scaled, uint32 bias) { |
| 2790 |
return pack_color(color_quad_u8(r, g, b, 0), scaled, bias); |
| 2791 |
} |
| 2792 |
|
| 2793 |
color_quad_u8 dxt1_block::unpack_color(uint16 packed_color, bool scaled, uint32 alpha) { |
| 2794 |
uint32 b = packed_color & 31U; |
| 2795 |
uint32 g = (packed_color >> 5U) & 63U; |
| 2796 |
uint32 r = (packed_color >> 11U) & 31U; |
| 2797 |
|
| 2798 |
if (scaled) { |
| 2799 |
b = (b << 3U) | (b >> 2U); |
| 2800 |
g = (g << 2U) | (g >> 4U); |
| 2801 |
r = (r << 3U) | (r >> 2U); |
| 2802 |
} |
| 2803 |
|
| 2804 |
return color_quad_u8(r, g, b, alpha); |
| 2805 |
} |
| 2806 |
|
| 2807 |
void dxt1_block::unpack_color(uint32& r, uint32& g, uint32& b, uint16 packed_color, bool scaled) { |
| 2808 |
color_quad_u8 c(unpack_color(packed_color, scaled, 0)); |
| 2809 |
r = c.r; |
| 2810 |
g = c.g; |
| 2811 |
b = c.b; |
| 2812 |
} |
| 2813 |
|
| 2814 |
uint32 dxt1_block::get_block_colors3(color_quad_u8* pDst, uint16 color0, uint16 color1) { |
| 2815 |
color_quad_u8 c0(unpack_color(color0, true)); |
| 2816 |
color_quad_u8 c1(unpack_color(color1, true)); |
| 2817 |
|
| 2818 |
pDst[0] = c0; |
| 2819 |
pDst[1] = c1; |
| 2820 |
pDst[2].set((c0.r + c1.r) >> 1U, (c0.g + c1.g) >> 1U, (c0.b + c1.b) >> 1U, 255U); |
| 2821 |
pDst[3].set(0, 0, 0, 0); |
| 2822 |
|
| 2823 |
return 3; |
| 2824 |
} |
| 2825 |
|
| 2826 |
uint32 dxt1_block::get_block_colors4(color_quad_u8* pDst, uint16 color0, uint16 color1) { |
| 2827 |
color_quad_u8 c0(unpack_color(color0, true)); |
| 2828 |
color_quad_u8 c1(unpack_color(color1, true)); |
| 2829 |
|
| 2830 |
pDst[0] = c0; |
| 2831 |
pDst[1] = c1; |
| 2832 |
|
| 2833 |
|
| 2834 |
|
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
pDst[2].set((c0.r * 2 + c1.r) / 3, (c0.g * 2 + c1.g) / 3, (c0.b * 2 + c1.b) / 3, 255U); |
| 2839 |
pDst[3].set((c1.r * 2 + c0.r) / 3, (c1.g * 2 + c0.g) / 3, (c1.b * 2 + c0.b) / 3, 255U); |
| 2840 |
|
| 2841 |
return 4; |
| 2842 |
} |
| 2843 |
|
| 2844 |
uint32 dxt1_block::get_block_colors(color_quad_u8* pDst, uint16 color0, uint16 color1) { |
| 2845 |
if (color0 > color1) |
| 2846 |
return get_block_colors4(pDst, color0, color1); |
| 2847 |
else |
| 2848 |
return get_block_colors3(pDst, color0, color1); |
| 2849 |
} |
| 2850 |
|
| 2851 |
color_quad_u8 dxt1_block::unpack_endpoint(uint32 endpoints, uint32 index, bool scaled, uint32 alpha) { |
| 2852 |
CRND_ASSERT(index < 2); |
| 2853 |
return unpack_color(static_cast<uint16>((endpoints >> (index * 16U)) & 0xFFFFU), scaled, alpha); |
| 2854 |
} |
| 2855 |
|
| 2856 |
uint32 dxt1_block::pack_endpoints(uint32 lo, uint32 hi) { |
| 2857 |
CRND_ASSERT((lo <= 0xFFFFU) && (hi <= 0xFFFFU)); |
| 2858 |
return lo | (hi << 16U); |
| 2859 |
} |
| 2860 |
|
| 2861 |
void dxt3_block::set_alpha(uint32 x, uint32 y, uint32 value, bool scaled) { |
| 2862 |
CRND_ASSERT((x < cDXTBlockSize) && (y < cDXTBlockSize)); |
| 2863 |
|
| 2864 |
if (scaled) { |
| 2865 |
CRND_ASSERT(value <= 0xFF); |
| 2866 |
value = (value * 15U + 128U) / 255U; |
| 2867 |
} else { |
| 2868 |
CRND_ASSERT(value <= 0xF); |
| 2869 |
} |
| 2870 |
|
| 2871 |
uint32 ofs = (y << 1U) + (x >> 1U); |
| 2872 |
uint32 c = m_alpha[ofs]; |
| 2873 |
|
| 2874 |
c &= ~(0xF << ((x & 1U) << 2U)); |
| 2875 |
c |= (value << ((x & 1U) << 2U)); |
| 2876 |
|
| 2877 |
m_alpha[ofs] = static_cast<uint8>(c); |
| 2878 |
} |
| 2879 |
|
| 2880 |
uint32 dxt3_block::get_alpha(uint32 x, uint32 y, bool scaled) const { |
| 2881 |
CRND_ASSERT((x < cDXTBlockSize) && (y < cDXTBlockSize)); |
| 2882 |
|
| 2883 |
uint32 value = m_alpha[(y << 1U) + (x >> 1U)]; |
| 2884 |
if (x & 1) |
| 2885 |
value >>= 4; |
| 2886 |
value &= 0xF; |
| 2887 |
|
| 2888 |
if (scaled) |
| 2889 |
value = (value << 4U) | value; |
| 2890 |
|
| 2891 |
return value; |
| 2892 |
} |
| 2893 |
|
| 2894 |
uint32 dxt5_block::get_block_values6(color_quad_u8* pDst, uint32 l, uint32 h) { |
| 2895 |
pDst[0].a = static_cast<uint8>(l); |
| 2896 |
pDst[1].a = static_cast<uint8>(h); |
| 2897 |
pDst[2].a = static_cast<uint8>((l * 4 + h) / 5); |
| 2898 |
pDst[3].a = static_cast<uint8>((l * 3 + h * 2) / 5); |
| 2899 |
pDst[4].a = static_cast<uint8>((l * 2 + h * 3) / 5); |
| 2900 |
pDst[5].a = static_cast<uint8>((l + h * 4) / 5); |
| 2901 |
pDst[6].a = 0; |
| 2902 |
pDst[7].a = 255; |
| 2903 |
return 6; |
| 2904 |
} |
| 2905 |
|
| 2906 |
uint32 dxt5_block::get_block_values8(color_quad_u8* pDst, uint32 l, uint32 h) { |
| 2907 |
pDst[0].a = static_cast<uint8>(l); |
| 2908 |
pDst[1].a = static_cast<uint8>(h); |
| 2909 |
pDst[2].a = static_cast<uint8>((l * 6 + h) / 7); |
| 2910 |
pDst[3].a = static_cast<uint8>((l * 5 + h * 2) / 7); |
| 2911 |
pDst[4].a = static_cast<uint8>((l * 4 + h * 3) / 7); |
| 2912 |
pDst[5].a = static_cast<uint8>((l * 3 + h * 4) / 7); |
| 2913 |
pDst[6].a = static_cast<uint8>((l * 2 + h * 5) / 7); |
| 2914 |
pDst[7].a = static_cast<uint8>((l + h * 6) / 7); |
| 2915 |
return 8; |
| 2916 |
} |
| 2917 |
|
| 2918 |
uint32 dxt5_block::get_block_values(color_quad_u8* pDst, uint32 l, uint32 h) { |
| 2919 |
if (l > h) |
| 2920 |
return get_block_values8(pDst, l, h); |
| 2921 |
else |
| 2922 |
return get_block_values6(pDst, l, h); |
| 2923 |
} |
| 2924 |
|
| 2925 |
uint32 dxt5_block::get_block_values6(uint32* pDst, uint32 l, uint32 h) { |
| 2926 |
pDst[0] = l; |
| 2927 |
pDst[1] = h; |
| 2928 |
pDst[2] = (l * 4 + h) / 5; |
| 2929 |
pDst[3] = (l * 3 + h * 2) / 5; |
| 2930 |
pDst[4] = (l * 2 + h * 3) / 5; |
| 2931 |
pDst[5] = (l + h * 4) / 5; |
| 2932 |
pDst[6] = 0; |
| 2933 |
pDst[7] = 255; |
| 2934 |
return 6; |
| 2935 |
} |
| 2936 |
|
| 2937 |
uint32 dxt5_block::get_block_values8(uint32* pDst, uint32 l, uint32 h) { |
| 2938 |
pDst[0] = l; |
| 2939 |
pDst[1] = h; |
| 2940 |
pDst[2] = (l * 6 + h) / 7; |
| 2941 |
pDst[3] = (l * 5 + h * 2) / 7; |
| 2942 |
pDst[4] = (l * 4 + h * 3) / 7; |
| 2943 |
pDst[5] = (l * 3 + h * 4) / 7; |
| 2944 |
pDst[6] = (l * 2 + h * 5) / 7; |
| 2945 |
pDst[7] = (l + h * 6) / 7; |
| 2946 |
return 8; |
| 2947 |
} |
| 2948 |
|
| 2949 |
uint32 dxt5_block::unpack_endpoint(uint32 packed, uint32 index) { |
| 2950 |
CRND_ASSERT(index < 2); |
| 2951 |
return (packed >> (8 * index)) & 0xFF; |
| 2952 |
} |
| 2953 |
|
| 2954 |
uint32 dxt5_block::pack_endpoints(uint32 lo, uint32 hi) { |
| 2955 |
CRND_ASSERT((lo <= 0xFF) && (hi <= 0xFF)); |
| 2956 |
return lo | (hi << 8U); |
| 2957 |
} |
| 2958 |
|
| 2959 |
uint32 dxt5_block::get_block_values(uint32* pDst, uint32 l, uint32 h) { |
| 2960 |
if (l > h) |
| 2961 |
return get_block_values8(pDst, l, h); |
| 2962 |
else |
| 2963 |
return get_block_values6(pDst, l, h); |
| 2964 |
} |
| 2965 |
|
| 2966 |
} |
| 2967 |
|
| 2968 |
|
| 2969 |
|
| 2970 |
namespace crnd { |
| 2971 |
|
| 2972 |
class crn_unpacker { |
| 2973 |
public: |
| 2974 |
inline crn_unpacker() |
| 2975 |
: m_magic(cMagicValue), |
| 2976 |
m_pData(NULL), |
| 2977 |
m_data_size(0), |
| 2978 |
m_pHeader(NULL) { |
| 2979 |
} |
| 2980 |
|
| 2981 |
inline ~crn_unpacker() { |
| 2982 |
m_magic = 0; |
| 2983 |
} |
| 2984 |
|
| 2985 |
inline bool is_valid() const { return m_magic == cMagicValue; } |
| 2986 |
|
| 2987 |
bool init(const void* pData, uint32 data_size) { |
| 2988 |
m_pHeader = crnd_get_header(pData, data_size); |
| 2989 |
if (!m_pHeader) |
| 2990 |
return false; |
| 2991 |
|
| 2992 |
m_pData = static_cast<const uint8*>(pData); |
| 2993 |
m_data_size = data_size; |
| 2994 |
|
| 2995 |
if (!init_tables()) |
| 2996 |
return false; |
| 2997 |
|
| 2998 |
if (!decode_palettes()) |
| 2999 |
return false; |
| 3000 |
|
| 3001 |
return true; |
| 3002 |
} |
| 3003 |
|
| 3004 |
bool unpack_level( |
| 3005 |
void** pDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes, |
| 3006 |
uint32 level_index) { |
| 3007 |
uint32 cur_level_ofs = m_pHeader->m_level_ofs[level_index]; |
| 3008 |
|
| 3009 |
uint32 next_level_ofs = m_data_size; |
| 3010 |
if ((level_index + 1) < (m_pHeader->m_levels)) |
| 3011 |
next_level_ofs = m_pHeader->m_level_ofs[level_index + 1]; |
| 3012 |
|
| 3013 |
CRND_ASSERT(next_level_ofs > cur_level_ofs); |
| 3014 |
|
| 3015 |
return unpack_level(m_pData + cur_level_ofs, next_level_ofs - cur_level_ofs, pDst, dst_size_in_bytes, row_pitch_in_bytes, level_index); |
| 3016 |
} |
| 3017 |
|
| 3018 |
bool unpack_level( |
| 3019 |
const void* pSrc, uint32 src_size_in_bytes, |
| 3020 |
void** pDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes, |
| 3021 |
uint32 level_index) { |
| 3022 |
|
| 3023 |
#ifdef CRND_BUILD_DEBUG |
| 3024 |
for (uint32 f = 0; f < m_pHeader->m_faces; f++) |
| 3025 |
if (!pDst[f]) |
| 3026 |
return false; |
| 3027 |
#endif |
| 3028 |
|
| 3029 |
const uint32 width = math::maximum(m_pHeader->m_width >> level_index, 1U); |
| 3030 |
const uint32 height = math::maximum(m_pHeader->m_height >> level_index, 1U); |
| 3031 |
const uint32 blocks_x = (width + 3U) >> 2U; |
| 3032 |
const uint32 blocks_y = (height + 3U) >> 2U; |
| 3033 |
const uint32 block_size = m_pHeader->m_format == cCRNFmtDXT1 || m_pHeader->m_format == cCRNFmtDXT5A || m_pHeader->m_format == cCRNFmtETC1 || m_pHeader->m_format == cCRNFmtETC2 || m_pHeader->m_format == cCRNFmtETC1S ? 8 : 16; |
| 3034 |
|
| 3035 |
uint32 minimal_row_pitch = block_size * blocks_x; |
| 3036 |
if (!row_pitch_in_bytes) |
| 3037 |
row_pitch_in_bytes = minimal_row_pitch; |
| 3038 |
else if ((row_pitch_in_bytes < minimal_row_pitch) || (row_pitch_in_bytes & 3)) |
| 3039 |
return false; |
| 3040 |
if (dst_size_in_bytes < row_pitch_in_bytes * blocks_y) |
| 3041 |
return false; |
| 3042 |
|
| 3043 |
if (!m_codec.start_decoding(static_cast<const crnd::uint8*>(pSrc), src_size_in_bytes)) |
| 3044 |
return false; |
| 3045 |
|
| 3046 |
bool status = false; |
| 3047 |
switch (m_pHeader->m_format) { |
| 3048 |
case cCRNFmtDXT1: |
| 3049 |
case cCRNFmtETC1S: |
| 3050 |
status = unpack_dxt1((uint8**)pDst, row_pitch_in_bytes, blocks_x, blocks_y); |
| 3051 |
break; |
| 3052 |
case cCRNFmtDXT5: |
| 3053 |
case cCRNFmtDXT5_CCxY: |
| 3054 |
case cCRNFmtDXT5_xGBR: |
| 3055 |
case cCRNFmtDXT5_AGBR: |
| 3056 |
case cCRNFmtDXT5_xGxR: |
| 3057 |
case cCRNFmtETC2AS: |
| 3058 |
status = unpack_dxt5((uint8**)pDst, row_pitch_in_bytes, blocks_x, blocks_y); |
| 3059 |
break; |
| 3060 |
case cCRNFmtDXT5A: |
| 3061 |
status = unpack_dxt5a((uint8**)pDst, row_pitch_in_bytes, blocks_x, blocks_y); |
| 3062 |
break; |
| 3063 |
case cCRNFmtDXN_XY: |
| 3064 |
case cCRNFmtDXN_YX: |
| 3065 |
status = unpack_dxn((uint8**)pDst, row_pitch_in_bytes, blocks_x, blocks_y); |
| 3066 |
break; |
| 3067 |
case cCRNFmtETC1: |
| 3068 |
status = unpack_etc1((uint8**)pDst, row_pitch_in_bytes, blocks_x, blocks_y); |
| 3069 |
break; |
| 3070 |
case cCRNFmtETC2: |
| 3071 |
status = unpack_etc1((uint8**)pDst, row_pitch_in_bytes, blocks_x, blocks_y); |
| 3072 |
break; |
| 3073 |
case cCRNFmtETC2A: |
| 3074 |
status = unpack_etc2a((uint8**)pDst, row_pitch_in_bytes, blocks_x, blocks_y); |
| 3075 |
break; |
| 3076 |
default: |
| 3077 |
return false; |
| 3078 |
} |
| 3079 |
if (!status) |
| 3080 |
return false; |
| 3081 |
|
| 3082 |
m_codec.stop_decoding(); |
| 3083 |
return true; |
| 3084 |
} |
| 3085 |
|
| 3086 |
inline const void* get_data() const { return m_pData; } |
| 3087 |
inline uint32 get_data_size() const { return m_data_size; } |
| 3088 |
|
| 3089 |
private: |
| 3090 |
enum { cMagicValue = 0x1EF9CABD }; |
| 3091 |
uint32 m_magic; |
| 3092 |
|
| 3093 |
const uint8* m_pData; |
| 3094 |
uint32 m_data_size; |
| 3095 |
const crn_header* m_pHeader; |
| 3096 |
|
| 3097 |
symbol_codec m_codec; |
| 3098 |
|
| 3099 |
static_huffman_data_model m_reference_encoding_dm; |
| 3100 |
static_huffman_data_model m_endpoint_delta_dm[2]; |
| 3101 |
static_huffman_data_model m_selector_delta_dm[2]; |
| 3102 |
|
| 3103 |
crnd::vector<uint32> m_color_endpoints; |
| 3104 |
crnd::vector<uint32> m_color_selectors; |
| 3105 |
|
| 3106 |
crnd::vector<uint16> m_alpha_endpoints; |
| 3107 |
crnd::vector<uint16> m_alpha_selectors; |
| 3108 |
|
| 3109 |
struct block_buffer_element { |
| 3110 |
uint16 endpoint_reference; |
| 3111 |
uint16 color_endpoint_index; |
| 3112 |
uint16 alpha0_endpoint_index; |
| 3113 |
uint16 alpha1_endpoint_index; |
| 3114 |
}; |
| 3115 |
crnd::vector<block_buffer_element> m_block_buffer; |
| 3116 |
|
| 3117 |
bool init_tables() { |
| 3118 |
if (!m_codec.start_decoding(m_pData + m_pHeader->m_tables_ofs, m_pHeader->m_tables_size)) |
| 3119 |
return false; |
| 3120 |
|
| 3121 |
if (!m_codec.decode_receive_static_data_model(m_reference_encoding_dm)) |
| 3122 |
return false; |
| 3123 |
|
| 3124 |
if ((!m_pHeader->m_color_endpoints.m_num) && (!m_pHeader->m_alpha_endpoints.m_num)) |
| 3125 |
return false; |
| 3126 |
|
| 3127 |
if (m_pHeader->m_color_endpoints.m_num) { |
| 3128 |
if (!m_codec.decode_receive_static_data_model(m_endpoint_delta_dm[0])) |
| 3129 |
return false; |
| 3130 |
if (!m_codec.decode_receive_static_data_model(m_selector_delta_dm[0])) |
| 3131 |
return false; |
| 3132 |
} |
| 3133 |
|
| 3134 |
if (m_pHeader->m_alpha_endpoints.m_num) { |
| 3135 |
if (!m_codec.decode_receive_static_data_model(m_endpoint_delta_dm[1])) |
| 3136 |
return false; |
| 3137 |
if (!m_codec.decode_receive_static_data_model(m_selector_delta_dm[1])) |
| 3138 |
return false; |
| 3139 |
} |
| 3140 |
|
| 3141 |
m_codec.stop_decoding(); |
| 3142 |
|
| 3143 |
return true; |
| 3144 |
} |
| 3145 |
|
| 3146 |
bool decode_palettes() { |
| 3147 |
if (m_pHeader->m_color_endpoints.m_num) { |
| 3148 |
if (!decode_color_endpoints()) |
| 3149 |
return false; |
| 3150 |
if (!decode_color_selectors()) |
| 3151 |
return false; |
| 3152 |
} |
| 3153 |
|
| 3154 |
if (m_pHeader->m_alpha_endpoints.m_num) { |
| 3155 |
if (!decode_alpha_endpoints()) |
| 3156 |
return false; |
| 3157 |
if (!(m_pHeader->m_format == cCRNFmtETC2AS ? decode_alpha_selectors_etcs() : m_pHeader->m_format == cCRNFmtETC2A ? decode_alpha_selectors_etc() : decode_alpha_selectors())) |
| 3158 |
return false; |
| 3159 |
} |
| 3160 |
|
| 3161 |
return true; |
| 3162 |
} |
| 3163 |
|
| 3164 |
bool decode_color_endpoints() { |
| 3165 |
const uint32 num_color_endpoints = m_pHeader->m_color_endpoints.m_num; |
| 3166 |
const bool has_etc_color_blocks = m_pHeader->m_format == cCRNFmtETC1 || m_pHeader->m_format == cCRNFmtETC2 || m_pHeader->m_format == cCRNFmtETC2A || m_pHeader->m_format == cCRNFmtETC1S || m_pHeader->m_format == cCRNFmtETC2AS; |
| 3167 |
const bool has_subblocks = m_pHeader->m_format == cCRNFmtETC1 || m_pHeader->m_format == cCRNFmtETC2 || m_pHeader->m_format == cCRNFmtETC2A; |
| 3168 |
|
| 3169 |
if (!m_color_endpoints.resize(num_color_endpoints)) |
| 3170 |
return false; |
| 3171 |
|
| 3172 |
if (!m_codec.start_decoding(m_pData + m_pHeader->m_color_endpoints.m_ofs, m_pHeader->m_color_endpoints.m_size)) |
| 3173 |
return false; |
| 3174 |
|
| 3175 |
static_huffman_data_model dm[2]; |
| 3176 |
for (uint32 i = 0; i < (has_etc_color_blocks ? 1U : 2U); i++) |
| 3177 |
if (!m_codec.decode_receive_static_data_model(dm[i])) |
| 3178 |
return false; |
| 3179 |
|
| 3180 |
uint32 a = 0, b = 0, c = 0; |
| 3181 |
uint32 d = 0, e = 0, f = 0; |
| 3182 |
|
| 3183 |
uint32* CRND_RESTRICT pDst = &m_color_endpoints[0]; |
| 3184 |
|
| 3185 |
for (uint32 i = 0; i < num_color_endpoints; i++) { |
| 3186 |
if (has_etc_color_blocks) { |
| 3187 |
for (b = 0; b < 32; b += 8) |
| 3188 |
a += m_codec.decode(dm[0]) << b; |
| 3189 |
a &= 0x1F1F1F1F; |
| 3190 |
*pDst++ = has_subblocks ? a : (a & 0x07000000) << 5 | (a & 0x07000000) << 2 | 0x02000000 | (a & 0x001F1F1F) << 3; |
| 3191 |
} else { |
| 3192 |
a = (a + m_codec.decode(dm[0])) & 31; |
| 3193 |
b = (b + m_codec.decode(dm[1])) & 63; |
| 3194 |
c = (c + m_codec.decode(dm[0])) & 31; |
| 3195 |
d = (d + m_codec.decode(dm[0])) & 31; |
| 3196 |
e = (e + m_codec.decode(dm[1])) & 63; |
| 3197 |
f = (f + m_codec.decode(dm[0])) & 31; |
| 3198 |
*pDst++ = c | (b << 5U) | (a << 11U) | (f << 16U) | (e << 21U) | (d << 27U); |
| 3199 |
} |
| 3200 |
} |
| 3201 |
|
| 3202 |
m_codec.stop_decoding(); |
| 3203 |
|
| 3204 |
return true; |
| 3205 |
} |
| 3206 |
|
| 3207 |
bool decode_color_selectors() { |
| 3208 |
const bool has_etc_color_blocks = m_pHeader->m_format == cCRNFmtETC1 || m_pHeader->m_format == cCRNFmtETC2 || m_pHeader->m_format == cCRNFmtETC2A || m_pHeader->m_format == cCRNFmtETC1S || m_pHeader->m_format == cCRNFmtETC2AS; |
| 3209 |
const bool has_subblocks = m_pHeader->m_format == cCRNFmtETC1 || m_pHeader->m_format == cCRNFmtETC2 || m_pHeader->m_format == cCRNFmtETC2A; |
| 3210 |
m_codec.start_decoding(m_pData + m_pHeader->m_color_selectors.m_ofs, m_pHeader->m_color_selectors.m_size); |
| 3211 |
static_huffman_data_model dm; |
| 3212 |
m_codec.decode_receive_static_data_model(dm); |
| 3213 |
m_color_selectors.resize(m_pHeader->m_color_selectors.m_num << (has_subblocks ? 1 : 0)); |
| 3214 |
for (uint32 s = 0, i = 0; i < m_pHeader->m_color_selectors.m_num; i++) { |
| 3215 |
for (uint32 j = 0; j < 32; j += 4) |
| 3216 |
s ^= m_codec.decode(dm) << j; |
| 3217 |
if (has_etc_color_blocks) { |
| 3218 |
for (uint32 selector = (~s & 0xAAAAAAAA) | (~(s ^ s >> 1) & 0x55555555), t = 8, h = 0; h < 4; h++, t -= 15) { |
| 3219 |
for (uint32 w = 0; w < 4; w++, t += 4) { |
| 3220 |
if (has_subblocks) { |
| 3221 |
uint32 s0 = selector >> (w << 3 | h << 1); |
| 3222 |
m_color_selectors[i << 1] |= ((s0 >> 1 & 1) | (s0 & 1) << 16) << (t & 15); |
| 3223 |
} |
| 3224 |
uint32 s1 = selector >> (h << 3 | w << 1); |
| 3225 |
m_color_selectors[has_subblocks ? i << 1 | 1 : i] |= ((s1 >> 1 & 1) | (s1 & 1) << 16) << (t & 15); |
| 3226 |
} |
| 3227 |
} |
| 3228 |
} else { |
| 3229 |
m_color_selectors[i] = ((s ^ s << 1) & 0xAAAAAAAA) | (s >> 1 & 0x55555555); |
| 3230 |
} |
| 3231 |
} |
| 3232 |
m_codec.stop_decoding(); |
| 3233 |
return true; |
| 3234 |
} |
| 3235 |
|
| 3236 |
bool decode_alpha_endpoints() { |
| 3237 |
const uint32 num_alpha_endpoints = m_pHeader->m_alpha_endpoints.m_num; |
| 3238 |
|
| 3239 |
if (!m_codec.start_decoding(m_pData + m_pHeader->m_alpha_endpoints.m_ofs, m_pHeader->m_alpha_endpoints.m_size)) |
| 3240 |
return false; |
| 3241 |
|
| 3242 |
static_huffman_data_model dm; |
| 3243 |
if (!m_codec.decode_receive_static_data_model(dm)) |
| 3244 |
return false; |
| 3245 |
|
| 3246 |
if (!m_alpha_endpoints.resize(num_alpha_endpoints)) |
| 3247 |
return false; |
| 3248 |
|
| 3249 |
uint16* CRND_RESTRICT pDst = &m_alpha_endpoints[0]; |
| 3250 |
uint32 a = 0, b = 0; |
| 3251 |
|
| 3252 |
for (uint32 i = 0; i < num_alpha_endpoints; i++) { |
| 3253 |
a = (a + m_codec.decode(dm)) & 255; |
| 3254 |
b = (b + m_codec.decode(dm)) & 255; |
| 3255 |
*pDst++ = (uint16)(a | (b << 8)); |
| 3256 |
} |
| 3257 |
|
| 3258 |
m_codec.stop_decoding(); |
| 3259 |
|
| 3260 |
return true; |
| 3261 |
} |
| 3262 |
|
| 3263 |
bool decode_alpha_selectors() { |
| 3264 |
m_codec.start_decoding(m_pData + m_pHeader->m_alpha_selectors.m_ofs, m_pHeader->m_alpha_selectors.m_size); |
| 3265 |
static_huffman_data_model dm; |
| 3266 |
m_codec.decode_receive_static_data_model(dm); |
| 3267 |
m_alpha_selectors.resize(m_pHeader->m_alpha_selectors.m_num * 3); |
| 3268 |
uint8 dxt5_from_linear[64]; |
| 3269 |
for (uint32 i = 0; i < 64; i++) |
| 3270 |
dxt5_from_linear[i] = g_dxt5_from_linear[i & 7] | g_dxt5_from_linear[i >> 3] << 3; |
| 3271 |
for (uint32 s0_linear = 0, s1_linear = 0, i = 0; i < m_alpha_selectors.size();) { |
| 3272 |
uint32 s0 = 0, s1 = 0; |
| 3273 |
for (uint32 j = 0; j < 24; s0 |= dxt5_from_linear[s0_linear >> j & 0x3F] << j, j += 6) |
| 3274 |
s0_linear ^= m_codec.decode(dm) << j; |
| 3275 |
for (uint32 j = 0; j < 24; s1 |= dxt5_from_linear[s1_linear >> j & 0x3F] << j, j += 6) |
| 3276 |
s1_linear ^= m_codec.decode(dm) << j; |
| 3277 |
m_alpha_selectors[i++] = s0; |
| 3278 |
m_alpha_selectors[i++] = s0 >> 16 | s1 << 8; |
| 3279 |
m_alpha_selectors[i++] = s1 >> 8; |
| 3280 |
} |
| 3281 |
m_codec.stop_decoding(); |
| 3282 |
return true; |
| 3283 |
} |
| 3284 |
|
| 3285 |
bool decode_alpha_selectors_etc() { |
| 3286 |
m_codec.start_decoding(m_pData + m_pHeader->m_alpha_selectors.m_ofs, m_pHeader->m_alpha_selectors.m_size); |
| 3287 |
static_huffman_data_model dm; |
| 3288 |
m_codec.decode_receive_static_data_model(dm); |
| 3289 |
m_alpha_selectors.resize(m_pHeader->m_alpha_selectors.m_num * 6); |
| 3290 |
uint8 s_linear[8] = {}; |
| 3291 |
uint8* data = (uint8*)m_alpha_selectors.begin(); |
| 3292 |
for (uint i = 0; i < m_alpha_selectors.size(); i += 6, data += 12) { |
| 3293 |
for (uint s_group = 0, p = 0; p < 16; p++) { |
| 3294 |
s_group = p & 1 ? s_group >> 3 : s_linear[p >> 1] ^= m_codec.decode(dm); |
| 3295 |
uint8 s = s_group & 7; |
| 3296 |
if (s <= 3) |
| 3297 |
s = 3 - s; |
| 3298 |
uint8 d = 3 * (p + 1); |
| 3299 |
uint8 byte_offset = d >> 3; |
| 3300 |
uint8 bit_offset = d & 7; |
| 3301 |
data[byte_offset] |= s << (8 - bit_offset); |
| 3302 |
if (bit_offset < 3) |
| 3303 |
data[byte_offset - 1] |= s >> bit_offset; |
| 3304 |
d += 9 * ((p & 3) - (p >> 2)); |
| 3305 |
byte_offset = d >> 3; |
| 3306 |
bit_offset = d & 7; |
| 3307 |
data[byte_offset + 6] |= s << (8 - bit_offset); |
| 3308 |
if (bit_offset < 3) |
| 3309 |
data[byte_offset + 5] |= s >> bit_offset; |
| 3310 |
} |
| 3311 |
} |
| 3312 |
m_codec.stop_decoding(); |
| 3313 |
return true; |
| 3314 |
} |
| 3315 |
|
| 3316 |
bool decode_alpha_selectors_etcs() { |
| 3317 |
m_codec.start_decoding(m_pData + m_pHeader->m_alpha_selectors.m_ofs, m_pHeader->m_alpha_selectors.m_size); |
| 3318 |
static_huffman_data_model dm; |
| 3319 |
m_codec.decode_receive_static_data_model(dm); |
| 3320 |
m_alpha_selectors.resize(m_pHeader->m_alpha_selectors.m_num * 3); |
| 3321 |
uint8 s_linear[8] = {}; |
| 3322 |
uint8* data = (uint8*)m_alpha_selectors.begin(); |
| 3323 |
for (uint i = 0; i < (m_alpha_selectors.size() << 1); i += 6) { |
| 3324 |
for (uint s_group = 0, p = 0; p < 16; p++) { |
| 3325 |
s_group = p & 1 ? s_group >> 3 : s_linear[p >> 1] ^= m_codec.decode(dm); |
| 3326 |
uint8 s = s_group & 7; |
| 3327 |
if (s <= 3) |
| 3328 |
s = 3 - s; |
| 3329 |
uint8 d = 3 * (p + 1) + 9 * ((p & 3) - (p >> 2)); |
| 3330 |
uint8 byte_offset = d >> 3; |
| 3331 |
uint8 bit_offset = d & 7; |
| 3332 |
data[i + byte_offset] |= s << (8 - bit_offset); |
| 3333 |
if (bit_offset < 3) |
| 3334 |
data[i + byte_offset - 1] |= s >> bit_offset; |
| 3335 |
} |
| 3336 |
} |
| 3337 |
m_codec.stop_decoding(); |
| 3338 |
return true; |
| 3339 |
} |
| 3340 |
|
| 3341 |
static inline uint32 tiled_offset_2d_outer(uint32 y, uint32 AlignedWidth, uint32 LogBpp) { |
| 3342 |
uint32 Macro = ((y >> 5) * (AlignedWidth >> 5)) << (LogBpp + 7); |
| 3343 |
uint32 Micro = ((y & 6) << 2) << LogBpp; |
| 3344 |
|
| 3345 |
return Macro + |
| 3346 |
((Micro & ~15) << 1) + |
| 3347 |
(Micro & 15) + |
| 3348 |
((y & 8) << (3 + LogBpp)) + ((y & 1) << 4); |
| 3349 |
} |
| 3350 |
|
| 3351 |
static inline uint32 tiled_offset_2d_inner(uint32 x, uint32 y, uint32 LogBpp, uint32 BaseOffset) { |
| 3352 |
uint32 Macro = (x >> 5) << (LogBpp + 7); |
| 3353 |
uint32 Micro = (x & 7) << LogBpp; |
| 3354 |
uint32 Offset = BaseOffset + Macro + ((Micro & ~15) << 1) + (Micro & 15); |
| 3355 |
|
| 3356 |
return ((Offset & ~511) << 3) + ((Offset & 448) << 2) + (Offset & 63) + |
| 3357 |
((y & 16) << 7) + |
| 3358 |
(((((y & 8) >> 2) + (x >> 3)) & 3) << 6); |
| 3359 |
} |
| 3360 |
|
| 3361 |
static inline void limit(uint& x, uint n) { |
| 3362 |
int v = x - n; |
| 3363 |
int msk = (v >> 31); |
| 3364 |
x = (x & msk) | (v & ~msk); |
| 3365 |
} |
| 3366 |
|
| 3367 |
bool unpack_dxt1(uint8** pDst, uint32 output_pitch_in_bytes, uint32 output_width, uint32 output_height) { |
| 3368 |
const uint32 num_color_endpoints = m_color_endpoints.size(); |
| 3369 |
const uint32 width = (output_width + 1) & ~1; |
| 3370 |
const uint32 height = (output_height + 1) & ~1; |
| 3371 |
const int32 delta_pitch_in_dwords = (output_pitch_in_bytes >> 2) - (width << 1); |
| 3372 |
|
| 3373 |
if (m_block_buffer.size() < width) |
| 3374 |
m_block_buffer.resize(width); |
| 3375 |
|
| 3376 |
uint32 color_endpoint_index = 0; |
| 3377 |
uint8 reference_group = 0; |
| 3378 |
|
| 3379 |
for (uint32 f = 0; f < m_pHeader->m_faces; f++) { |
| 3380 |
uint32* pData = (uint32*)pDst[f]; |
| 3381 |
for (uint32 y = 0; y < height; y++, pData += delta_pitch_in_dwords) { |
| 3382 |
bool visible = y < output_height; |
| 3383 |
for (uint32 x = 0; x < width; x++, pData += 2) { |
| 3384 |
visible = visible && x < output_width; |
| 3385 |
if (!(y & 1) && !(x & 1)) |
| 3386 |
reference_group = m_codec.decode(m_reference_encoding_dm); |
| 3387 |
block_buffer_element &buffer = m_block_buffer[x]; |
| 3388 |
uint8 endpoint_reference; |
| 3389 |
if (y & 1) { |
| 3390 |
endpoint_reference = buffer.endpoint_reference; |
| 3391 |
} else { |
| 3392 |
endpoint_reference = reference_group & 3; |
| 3393 |
reference_group >>= 2; |
| 3394 |
buffer.endpoint_reference = reference_group & 3; |
| 3395 |
reference_group >>= 2; |
| 3396 |
} |
| 3397 |
if (!endpoint_reference) { |
| 3398 |
color_endpoint_index += m_codec.decode(m_endpoint_delta_dm[0]); |
| 3399 |
if (color_endpoint_index >= num_color_endpoints) |
| 3400 |
color_endpoint_index -= num_color_endpoints; |
| 3401 |
buffer.color_endpoint_index = color_endpoint_index; |
| 3402 |
} else if (endpoint_reference == 1) { |
| 3403 |
buffer.color_endpoint_index = color_endpoint_index; |
| 3404 |
} else { |
| 3405 |
color_endpoint_index = buffer.color_endpoint_index; |
| 3406 |
} |
| 3407 |
uint32 color_selector_index = m_codec.decode(m_selector_delta_dm[0]); |
| 3408 |
if (visible) { |
| 3409 |
pData[0] = m_color_endpoints[color_endpoint_index]; |
| 3410 |
pData[1] = m_color_selectors[color_selector_index]; |
| 3411 |
} |
| 3412 |
} |
| 3413 |
} |
| 3414 |
} |
| 3415 |
return true; |
| 3416 |
} |
| 3417 |
|
| 3418 |
bool unpack_dxt5(uint8** pDst, uint32 row_pitch_in_bytes, uint32 output_width, uint32 output_height) { |
| 3419 |
const uint32 num_color_endpoints = m_color_endpoints.size(); |
| 3420 |
const uint32 num_alpha_endpoints = m_alpha_endpoints.size(); |
| 3421 |
const uint32 width = (output_width + 1) & ~1; |
| 3422 |
const uint32 height = (output_height + 1) & ~1; |
| 3423 |
const int32 delta_pitch_in_dwords = (row_pitch_in_bytes >> 2) - (width << 2); |
| 3424 |
|
| 3425 |
if (m_block_buffer.size() < width) |
| 3426 |
m_block_buffer.resize(width); |
| 3427 |
|
| 3428 |
uint32 color_endpoint_index = 0; |
| 3429 |
uint32 alpha0_endpoint_index = 0; |
| 3430 |
uint8 reference_group = 0; |
| 3431 |
|
| 3432 |
for (uint32 f = 0; f < m_pHeader->m_faces; f++) { |
| 3433 |
uint32* pData = (uint32*)pDst[f]; |
| 3434 |
for (uint32 y = 0; y < height; y++, pData += delta_pitch_in_dwords) { |
| 3435 |
bool visible = y < output_height; |
| 3436 |
for (uint32 x = 0; x < width; x++, pData += 4) { |
| 3437 |
visible = visible && x < output_width; |
| 3438 |
if (!(y & 1) && !(x & 1)) |
| 3439 |
reference_group = m_codec.decode(m_reference_encoding_dm); |
| 3440 |
block_buffer_element &buffer = m_block_buffer[x]; |
| 3441 |
uint8 endpoint_reference; |
| 3442 |
if (y & 1) { |
| 3443 |
endpoint_reference = buffer.endpoint_reference; |
| 3444 |
} else { |
| 3445 |
endpoint_reference = reference_group & 3; |
| 3446 |
reference_group >>= 2; |
| 3447 |
buffer.endpoint_reference = reference_group & 3; |
| 3448 |
reference_group >>= 2; |
| 3449 |
} |
| 3450 |
if (!endpoint_reference) { |
| 3451 |
color_endpoint_index += m_codec.decode(m_endpoint_delta_dm[0]); |
| 3452 |
if (color_endpoint_index >= num_color_endpoints) |
| 3453 |
color_endpoint_index -= num_color_endpoints; |
| 3454 |
buffer.color_endpoint_index = color_endpoint_index; |
| 3455 |
alpha0_endpoint_index += m_codec.decode(m_endpoint_delta_dm[1]); |
| 3456 |
if (alpha0_endpoint_index >= num_alpha_endpoints) |
| 3457 |
alpha0_endpoint_index -= num_alpha_endpoints; |
| 3458 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index; |
| 3459 |
} else if (endpoint_reference == 1) { |
| 3460 |
buffer.color_endpoint_index = color_endpoint_index; |
| 3461 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index; |
| 3462 |
} else { |
| 3463 |
color_endpoint_index = buffer.color_endpoint_index; |
| 3464 |
alpha0_endpoint_index = buffer.alpha0_endpoint_index; |
| 3465 |
} |
| 3466 |
uint32 color_selector_index = m_codec.decode(m_selector_delta_dm[0]); |
| 3467 |
uint32 alpha0_selector_index = m_codec.decode(m_selector_delta_dm[1]); |
| 3468 |
if (visible) { |
| 3469 |
const uint16* pAlpha0_selectors = &m_alpha_selectors[alpha0_selector_index * 3]; |
| 3470 |
pData[0] = m_alpha_endpoints[alpha0_endpoint_index] | (pAlpha0_selectors[0] << 16); |
| 3471 |
pData[1] = pAlpha0_selectors[1] | (pAlpha0_selectors[2] << 16); |
| 3472 |
pData[2] = m_color_endpoints[color_endpoint_index]; |
| 3473 |
pData[3] = m_color_selectors[color_selector_index]; |
| 3474 |
} |
| 3475 |
} |
| 3476 |
} |
| 3477 |
} |
| 3478 |
return true; |
| 3479 |
} |
| 3480 |
|
| 3481 |
bool unpack_dxn(uint8** pDst, uint32 row_pitch_in_bytes, uint32 output_width, uint32 output_height) { |
| 3482 |
const uint32 num_alpha_endpoints = m_alpha_endpoints.size(); |
| 3483 |
const uint32 width = (output_width + 1) & ~1; |
| 3484 |
const uint32 height = (output_height + 1) & ~1; |
| 3485 |
const int32 delta_pitch_in_dwords = (row_pitch_in_bytes >> 2) - (width << 2); |
| 3486 |
|
| 3487 |
if (m_block_buffer.size() < width) |
| 3488 |
m_block_buffer.resize(width); |
| 3489 |
|
| 3490 |
uint32 alpha0_endpoint_index = 0; |
| 3491 |
uint32 alpha1_endpoint_index = 0; |
| 3492 |
uint8 reference_group = 0; |
| 3493 |
|
| 3494 |
for (uint32 f = 0; f < m_pHeader->m_faces; f++) { |
| 3495 |
uint32* pData = (uint32*)pDst[f]; |
| 3496 |
for (uint32 y = 0; y < height; y++, pData += delta_pitch_in_dwords) { |
| 3497 |
bool visible = y < output_height; |
| 3498 |
for (uint32 x = 0; x < width; x++, pData += 4) { |
| 3499 |
visible = visible && x < output_width; |
| 3500 |
if (!(y & 1) && !(x & 1)) |
| 3501 |
reference_group = m_codec.decode(m_reference_encoding_dm); |
| 3502 |
block_buffer_element &buffer = m_block_buffer[x]; |
| 3503 |
uint8 endpoint_reference; |
| 3504 |
if (y & 1) { |
| 3505 |
endpoint_reference = buffer.endpoint_reference; |
| 3506 |
} else { |
| 3507 |
endpoint_reference = reference_group & 3; |
| 3508 |
reference_group >>= 2; |
| 3509 |
buffer.endpoint_reference = reference_group & 3; |
| 3510 |
reference_group >>= 2; |
| 3511 |
} |
| 3512 |
if (!endpoint_reference) { |
| 3513 |
alpha0_endpoint_index += m_codec.decode(m_endpoint_delta_dm[1]); |
| 3514 |
if (alpha0_endpoint_index >= num_alpha_endpoints) |
| 3515 |
alpha0_endpoint_index -= num_alpha_endpoints; |
| 3516 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index; |
| 3517 |
alpha1_endpoint_index += m_codec.decode(m_endpoint_delta_dm[1]); |
| 3518 |
if (alpha1_endpoint_index >= num_alpha_endpoints) |
| 3519 |
alpha1_endpoint_index -= num_alpha_endpoints; |
| 3520 |
buffer.alpha1_endpoint_index = alpha1_endpoint_index; |
| 3521 |
} else if (endpoint_reference == 1) { |
| 3522 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index; |
| 3523 |
buffer.alpha1_endpoint_index = alpha1_endpoint_index; |
| 3524 |
} else { |
| 3525 |
alpha0_endpoint_index = buffer.alpha0_endpoint_index; |
| 3526 |
alpha1_endpoint_index = buffer.alpha1_endpoint_index; |
| 3527 |
} |
| 3528 |
uint32 alpha0_selector_index = m_codec.decode(m_selector_delta_dm[1]); |
| 3529 |
uint32 alpha1_selector_index = m_codec.decode(m_selector_delta_dm[1]); |
| 3530 |
if (visible) { |
| 3531 |
const uint16* pAlpha0_selectors = &m_alpha_selectors[alpha0_selector_index * 3]; |
| 3532 |
const uint16* pAlpha1_selectors = &m_alpha_selectors[alpha1_selector_index * 3]; |
| 3533 |
pData[0] = m_alpha_endpoints[alpha0_endpoint_index] | (pAlpha0_selectors[0] << 16); |
| 3534 |
pData[1] = pAlpha0_selectors[1] | (pAlpha0_selectors[2] << 16); |
| 3535 |
pData[2] = m_alpha_endpoints[alpha1_endpoint_index] | (pAlpha1_selectors[0] << 16); |
| 3536 |
pData[3] = pAlpha1_selectors[1] | (pAlpha1_selectors[2] << 16); |
| 3537 |
} |
| 3538 |
} |
| 3539 |
} |
| 3540 |
} |
| 3541 |
return true; |
| 3542 |
} |
| 3543 |
|
| 3544 |
bool unpack_dxt5a(uint8** pDst, uint32 row_pitch_in_bytes, uint32 output_width, uint32 output_height) { |
| 3545 |
const uint32 num_alpha_endpoints = m_alpha_endpoints.size(); |
| 3546 |
const uint32 width = (output_width + 1) & ~1; |
| 3547 |
const uint32 height = (output_height + 1) & ~1; |
| 3548 |
const int32 delta_pitch_in_dwords = (row_pitch_in_bytes >> 2) - (width << 1); |
| 3549 |
|
| 3550 |
if (m_block_buffer.size() < width) |
| 3551 |
m_block_buffer.resize(width); |
| 3552 |
|
| 3553 |
uint32 alpha0_endpoint_index = 0; |
| 3554 |
uint8 reference_group = 0; |
| 3555 |
|
| 3556 |
for (uint32 f = 0; f < m_pHeader->m_faces; f++) { |
| 3557 |
uint32* pData = (uint32*)pDst[f]; |
| 3558 |
for (uint32 y = 0; y < height; y++, pData += delta_pitch_in_dwords) { |
| 3559 |
bool visible = y < output_height; |
| 3560 |
for (uint32 x = 0; x < width; x++, pData += 2) { |
| 3561 |
visible = visible && x < output_width; |
| 3562 |
if (!(y & 1) && !(x & 1)) |
| 3563 |
reference_group = m_codec.decode(m_reference_encoding_dm); |
| 3564 |
block_buffer_element &buffer = m_block_buffer[x]; |
| 3565 |
uint8 endpoint_reference; |
| 3566 |
if (y & 1) { |
| 3567 |
endpoint_reference = buffer.endpoint_reference; |
| 3568 |
} else { |
| 3569 |
endpoint_reference = reference_group & 3; |
| 3570 |
reference_group >>= 2; |
| 3571 |
buffer.endpoint_reference = reference_group & 3; |
| 3572 |
reference_group >>= 2; |
| 3573 |
} |
| 3574 |
if (!endpoint_reference) { |
| 3575 |
alpha0_endpoint_index += m_codec.decode(m_endpoint_delta_dm[1]); |
| 3576 |
if (alpha0_endpoint_index >= num_alpha_endpoints) |
| 3577 |
alpha0_endpoint_index -= num_alpha_endpoints; |
| 3578 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index; |
| 3579 |
} else if (endpoint_reference == 1) { |
| 3580 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index; |
| 3581 |
} else { |
| 3582 |
alpha0_endpoint_index = buffer.alpha0_endpoint_index; |
| 3583 |
} |
| 3584 |
uint32 alpha0_selector_index = m_codec.decode(m_selector_delta_dm[1]); |
| 3585 |
if (visible) { |
| 3586 |
const uint16* pAlpha0_selectors = &m_alpha_selectors[alpha0_selector_index * 3]; |
| 3587 |
pData[0] = m_alpha_endpoints[alpha0_endpoint_index] | (pAlpha0_selectors[0] << 16); |
| 3588 |
pData[1] = pAlpha0_selectors[1] | (pAlpha0_selectors[2] << 16); |
| 3589 |
} |
| 3590 |
} |
| 3591 |
} |
| 3592 |
} |
| 3593 |
return true; |
| 3594 |
} |
| 3595 |
|
| 3596 |
bool unpack_etc1(uint8** pDst, uint32 output_pitch_in_bytes, uint32 output_width, uint32 output_height) { |
| 3597 |
const uint32 num_color_endpoints = m_color_endpoints.size(); |
| 3598 |
const uint32 width = (output_width + 1) & ~1; |
| 3599 |
const uint32 height = (output_height + 1) & ~1; |
| 3600 |
const int32 delta_pitch_in_dwords = (output_pitch_in_bytes >> 2) - (width << 1); |
| 3601 |
|
| 3602 |
if (m_block_buffer.size() < width << 1) |
| 3603 |
m_block_buffer.resize(width << 1); |
| 3604 |
|
| 3605 |
uint32 color_endpoint_index = 0, diagonal_color_endpoint_index = 0; |
| 3606 |
uint8 reference_group = 0; |
| 3607 |
|
| 3608 |
for (uint32 f = 0; f < m_pHeader->m_faces; f++) { |
| 3609 |
uint32* pData = (uint32*)pDst[f]; |
| 3610 |
for (uint32 y = 0; y < height; y++, pData += delta_pitch_in_dwords) { |
| 3611 |
bool visible = y < output_height; |
| 3612 |
for (uint32 x = 0; x < width; x++, pData += 2) { |
| 3613 |
visible = visible && x < output_width; |
| 3614 |
block_buffer_element &buffer = m_block_buffer[x << 1]; |
| 3615 |
uint8 endpoint_reference, block_endpoint[4], e0[4], e1[4]; |
| 3616 |
if (y & 1) { |
| 3617 |
endpoint_reference = buffer.endpoint_reference; |
| 3618 |
} else { |
| 3619 |
reference_group = m_codec.decode(m_reference_encoding_dm); |
| 3620 |
endpoint_reference = (reference_group & 3) | (reference_group >> 2 & 12); |
| 3621 |
buffer.endpoint_reference = (reference_group >> 2 & 3) | (reference_group >> 4 & 12); |
| 3622 |
} |
| 3623 |
if (!(endpoint_reference & 3)) { |
| 3624 |
color_endpoint_index += m_codec.decode(m_endpoint_delta_dm[0]); |
| 3625 |
if (color_endpoint_index >= num_color_endpoints) |
| 3626 |
color_endpoint_index -= num_color_endpoints; |
| 3627 |
buffer.color_endpoint_index = color_endpoint_index; |
| 3628 |
} else if ((endpoint_reference & 3) == 1) { |
| 3629 |
buffer.color_endpoint_index = color_endpoint_index; |
| 3630 |
} else if ((endpoint_reference & 3) == 3) { |
| 3631 |
buffer.color_endpoint_index = color_endpoint_index = diagonal_color_endpoint_index; |
| 3632 |
} else { |
| 3633 |
color_endpoint_index = buffer.color_endpoint_index; |
| 3634 |
} |
| 3635 |
endpoint_reference >>= 2; |
| 3636 |
*(uint32*)&e0 = m_color_endpoints[color_endpoint_index]; |
| 3637 |
uint32 selector_index = m_codec.decode(m_selector_delta_dm[0]); |
| 3638 |
if (endpoint_reference) { |
| 3639 |
color_endpoint_index += m_codec.decode(m_endpoint_delta_dm[0]); |
| 3640 |
if (color_endpoint_index >= num_color_endpoints) |
| 3641 |
color_endpoint_index -= num_color_endpoints; |
| 3642 |
} |
| 3643 |
diagonal_color_endpoint_index = m_block_buffer[x << 1 | 1].color_endpoint_index; |
| 3644 |
m_block_buffer[x << 1 | 1].color_endpoint_index = color_endpoint_index; |
| 3645 |
*(uint32*)&e1 = m_color_endpoints[color_endpoint_index]; |
| 3646 |
if (visible) { |
| 3647 |
uint32 flip = endpoint_reference >> 1 ^ 1, diff = 1; |
| 3648 |
for (uint c = 0; diff && c < 3; c++) |
| 3649 |
diff = e0[c] + 3 >= e1[c] && e1[c] + 4 >= e0[c] ? diff : 0; |
| 3650 |
for (uint c = 0; c < 3; c++) |
| 3651 |
block_endpoint[c] = diff ? e0[c] << 3 | ((e1[c] - e0[c]) & 7) : (e0[c] << 3 & 0xF0) | e1[c] >> 1; |
| 3652 |
block_endpoint[3] = e0[3] << 5 | e1[3] << 2 | diff << 1 | flip; |
| 3653 |
pData[0] = *(uint32*)&block_endpoint; |
| 3654 |
pData[1] = m_color_selectors[selector_index << 1 | flip]; |
| 3655 |
} |
| 3656 |
} |
| 3657 |
} |
| 3658 |
} |
| 3659 |
return true; |
| 3660 |
} |
| 3661 |
|
| 3662 |
bool unpack_etc2a(uint8** pDst, uint32 output_pitch_in_bytes, uint32 output_width, uint32 output_height) { |
| 3663 |
const uint32 num_color_endpoints = m_color_endpoints.size(); |
| 3664 |
const uint32 num_alpha_endpoints = m_alpha_endpoints.size(); |
| 3665 |
const uint32 width = (output_width + 1) & ~1; |
| 3666 |
const uint32 height = (output_height + 1) & ~1; |
| 3667 |
const int32 delta_pitch_in_dwords = (output_pitch_in_bytes >> 2) - (width << 2); |
| 3668 |
|
| 3669 |
if (m_block_buffer.size() < width << 1) |
| 3670 |
m_block_buffer.resize(width << 1); |
| 3671 |
|
| 3672 |
uint32 color_endpoint_index = 0, diagonal_color_endpoint_index = 0, alpha0_endpoint_index = 0, diagonal_alpha0_endpoint_index = 0; |
| 3673 |
uint8 reference_group = 0; |
| 3674 |
|
| 3675 |
for (uint32 f = 0; f < m_pHeader->m_faces; f++) { |
| 3676 |
uint32* pData = (uint32*)pDst[f]; |
| 3677 |
for (uint32 y = 0; y < height; y++, pData += delta_pitch_in_dwords) { |
| 3678 |
bool visible = y < output_height; |
| 3679 |
for (uint32 x = 0; x < width; x++, pData += 4) { |
| 3680 |
visible = visible && x < output_width; |
| 3681 |
block_buffer_element &buffer = m_block_buffer[x << 1]; |
| 3682 |
uint8 endpoint_reference, block_endpoint[4], e0[4], e1[4]; |
| 3683 |
if (y & 1) { |
| 3684 |
endpoint_reference = buffer.endpoint_reference; |
| 3685 |
} else { |
| 3686 |
reference_group = m_codec.decode(m_reference_encoding_dm); |
| 3687 |
endpoint_reference = (reference_group & 3) | (reference_group >> 2 & 12); |
| 3688 |
buffer.endpoint_reference = (reference_group >> 2 & 3) | (reference_group >> 4 & 12); |
| 3689 |
} |
| 3690 |
if (!(endpoint_reference & 3)) { |
| 3691 |
color_endpoint_index += m_codec.decode(m_endpoint_delta_dm[0]); |
| 3692 |
if (color_endpoint_index >= num_color_endpoints) |
| 3693 |
color_endpoint_index -= num_color_endpoints; |
| 3694 |
alpha0_endpoint_index += m_codec.decode(m_endpoint_delta_dm[1]); |
| 3695 |
if (alpha0_endpoint_index >= num_alpha_endpoints) |
| 3696 |
alpha0_endpoint_index -= num_alpha_endpoints; |
| 3697 |
buffer.color_endpoint_index = color_endpoint_index; |
| 3698 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index; |
| 3699 |
} else if ((endpoint_reference & 3) == 1) { |
| 3700 |
buffer.color_endpoint_index = color_endpoint_index; |
| 3701 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index; |
| 3702 |
} else if ((endpoint_reference & 3) == 3) { |
| 3703 |
buffer.color_endpoint_index = color_endpoint_index = diagonal_color_endpoint_index; |
| 3704 |
buffer.alpha0_endpoint_index = alpha0_endpoint_index = diagonal_alpha0_endpoint_index; |
| 3705 |
} else { |
| 3706 |
color_endpoint_index = buffer.color_endpoint_index; |
| 3707 |
alpha0_endpoint_index = buffer.alpha0_endpoint_index; |
| 3708 |
} |
| 3709 |
endpoint_reference >>= 2; |
| 3710 |
*(uint32*)&e0 = m_color_endpoints[color_endpoint_index]; |
| 3711 |
uint32 color_selector_index = m_codec.decode(m_selector_delta_dm[0]); |
| 3712 |
uint32 alpha0_selector_index = m_codec.decode(m_selector_delta_dm[1]); |
| 3713 |
if (endpoint_reference) { |
| 3714 |
color_endpoint_index += m_codec.decode(m_endpoint_delta_dm[0]); |
| 3715 |
if (color_endpoint_index >= num_color_endpoints) |
| 3716 |
color_endpoint_index -= num_color_endpoints; |
| 3717 |
} |
| 3718 |
*(uint32*)&e1 = m_color_endpoints[color_endpoint_index]; |
| 3719 |
diagonal_color_endpoint_index = m_block_buffer[x << 1 | 1].color_endpoint_index; |
| 3720 |
diagonal_alpha0_endpoint_index = m_block_buffer[x << 1 | 1].alpha0_endpoint_index; |
| 3721 |
m_block_buffer[x << 1 | 1].color_endpoint_index = color_endpoint_index; |
| 3722 |
m_block_buffer[x << 1 | 1].alpha0_endpoint_index = alpha0_endpoint_index; |
| 3723 |
if (visible) { |
| 3724 |
uint32 flip = endpoint_reference >> 1 ^ 1, diff = 1; |
| 3725 |
for (uint c = 0; diff && c < 3; c++) |
| 3726 |
diff = e0[c] + 3 >= e1[c] && e1[c] + 4 >= e0[c] ? diff : 0; |
| 3727 |
for (uint c = 0; c < 3; c++) |
| 3728 |
block_endpoint[c] = diff ? e0[c] << 3 | ((e1[c] - e0[c]) & 7) : (e0[c] << 3 & 0xF0) | e1[c] >> 1; |
| 3729 |
block_endpoint[3] = e0[3] << 5 | e1[3] << 2 | diff << 1 | flip; |
| 3730 |
const uint16* pAlpha0_selectors = &m_alpha_selectors[alpha0_selector_index * 6 + (flip ? 3 : 0)]; |
| 3731 |
pData[0] = m_alpha_endpoints[alpha0_endpoint_index] | pAlpha0_selectors[0] << 16; |
| 3732 |
pData[1] = pAlpha0_selectors[1] | pAlpha0_selectors[2] << 16; |
| 3733 |
pData[2] = *(uint32*)&block_endpoint; |
| 3734 |
pData[3] = m_color_selectors[color_selector_index << 1 | flip]; |
| 3735 |
} |
| 3736 |
} |
| 3737 |
} |
| 3738 |
} |
| 3739 |
return true; |
| 3740 |
} |
| 3741 |
|
| 3742 |
}; |
| 3743 |
|
| 3744 |
crnd_unpack_context crnd_unpack_begin(const void* pData, uint32 data_size) { |
| 3745 |
if ((!pData) || (data_size < cCRNHeaderMinSize)) |
| 3746 |
return NULL; |
| 3747 |
|
| 3748 |
crn_unpacker* p = crnd_new<crn_unpacker>(); |
| 3749 |
if (!p) |
| 3750 |
return NULL; |
| 3751 |
|
| 3752 |
if (!p->init(pData, data_size)) { |
| 3753 |
crnd_delete(p); |
| 3754 |
return NULL; |
| 3755 |
} |
| 3756 |
|
| 3757 |
return p; |
| 3758 |
} |
| 3759 |
|
| 3760 |
bool crnd_get_data(crnd_unpack_context pContext, const void** ppData, uint32* pData_size) { |
| 3761 |
if (!pContext) |
| 3762 |
return false; |
| 3763 |
|
| 3764 |
crn_unpacker* pUnpacker = static_cast<crn_unpacker*>(pContext); |
| 3765 |
|
| 3766 |
if (!pUnpacker->is_valid()) |
| 3767 |
return false; |
| 3768 |
|
| 3769 |
if (ppData) |
| 3770 |
*ppData = pUnpacker->get_data(); |
| 3771 |
|
| 3772 |
if (pData_size) |
| 3773 |
*pData_size = pUnpacker->get_data_size(); |
| 3774 |
|
| 3775 |
return true; |
| 3776 |
} |
| 3777 |
|
| 3778 |
bool crnd_unpack_level( |
| 3779 |
crnd_unpack_context pContext, |
| 3780 |
void** pDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes, |
| 3781 |
uint32 level_index) { |
| 3782 |
if ((!pContext) || (!pDst) || (dst_size_in_bytes < 8U) || (level_index >= cCRNMaxLevels)) |
| 3783 |
return false; |
| 3784 |
|
| 3785 |
crn_unpacker* pUnpacker = static_cast<crn_unpacker*>(pContext); |
| 3786 |
|
| 3787 |
if (!pUnpacker->is_valid()) |
| 3788 |
return false; |
| 3789 |
|
| 3790 |
return pUnpacker->unpack_level(pDst, dst_size_in_bytes, row_pitch_in_bytes, level_index); |
| 3791 |
} |
| 3792 |
|
| 3793 |
bool crnd_unpack_level_segmented( |
| 3794 |
crnd_unpack_context pContext, |
| 3795 |
const void* pSrc, uint32 src_size_in_bytes, |
| 3796 |
void** pDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes, |
| 3797 |
uint32 level_index) { |
| 3798 |
if ((!pContext) || (!pSrc) || (!pDst) || (dst_size_in_bytes < 8U) || (level_index >= cCRNMaxLevels)) |
| 3799 |
return false; |
| 3800 |
|
| 3801 |
crn_unpacker* pUnpacker = static_cast<crn_unpacker*>(pContext); |
| 3802 |
|
| 3803 |
if (!pUnpacker->is_valid()) |
| 3804 |
return false; |
| 3805 |
|
| 3806 |
return pUnpacker->unpack_level(pSrc, src_size_in_bytes, pDst, dst_size_in_bytes, row_pitch_in_bytes, level_index); |
| 3807 |
} |
| 3808 |
|
| 3809 |
bool crnd_unpack_end(crnd_unpack_context pContext) { |
| 3810 |
if (!pContext) |
| 3811 |
return false; |
| 3812 |
|
| 3813 |
crn_unpacker* pUnpacker = static_cast<crn_unpacker*>(pContext); |
| 3814 |
|
| 3815 |
if (!pUnpacker->is_valid()) |
| 3816 |
return false; |
| 3817 |
|
| 3818 |
crnd_delete(pUnpacker); |
| 3819 |
|
| 3820 |
return true; |
| 3821 |
} |
| 3822 |
|
| 3823 |
} |
| 3824 |
|
| 3825 |
#endif |
| 3826 |
|
| 3827 |
|
| 3828 |
|
| 3829 |
|
| 3830 |
|
| 3831 |
|
| 3832 |
|
| 3833 |
|
| 3834 |
|
| 3835 |
|
| 3836 |
|
| 3837 |
|
| 3838 |
|
| 3839 |
|
| 3840 |
|
| 3841 |
|
| 3842 |
|
| 3843 |
|
| 3844 |
|
| 3845 |
|
| 3846 |
|
| 3847 |
|
| 3848 |
|
| 3849 |
|
| 3850 |
|
| 3851 |
|
| 3852 |
|