| 1 |
#ifndef CRND_INCLUDE_CRN_DEFS_H |
| 2 |
#define CRND_INCLUDE_CRN_DEFS_H |
| 3 |
|
| 4 |
|
| 5 |
#include "crnlib.h" |
| 6 |
|
| 7 |
#define CRND_LIB_VERSION 104 |
| 8 |
#define CRND_VERSION_STRING "01.04" |
| 9 |
|
| 10 |
#ifdef _DEBUG |
| 11 |
#define CRND_BUILD_DEBUG |
| 12 |
#else |
| 13 |
#define CRND_BUILD_RELEASE |
| 14 |
#endif |
| 15 |
|
| 16 |
|
| 17 |
namespace crnd { |
| 18 |
typedef unsigned char uint8; |
| 19 |
typedef signed char int8; |
| 20 |
typedef unsigned short uint16; |
| 21 |
typedef signed short int16; |
| 22 |
typedef unsigned int uint32; |
| 23 |
typedef uint32 uint32; |
| 24 |
typedef unsigned int uint; |
| 25 |
typedef signed int int32; |
| 26 |
#ifdef __GNUC__ |
| 27 |
typedef unsigned long long uint64; |
| 28 |
typedef long long int64; |
| 29 |
#else |
| 30 |
typedef unsigned __int64 uint64; |
| 31 |
typedef signed __int64 int64; |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
const uint32 CRND_MIN_ALLOC_ALIGNMENT = sizeof(uint32) * 2U; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
typedef void* (*crnd_realloc_func)(void* p, size_t size, size_t* pActual_size, bool movable, void* pUser_data); |
| 49 |
|
| 50 |
|
| 51 |
typedef size_t (*crnd_msize_func)(void* p, void* pUser_data); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
void crnd_set_memory_callbacks(crnd_realloc_func pRealloc, crnd_msize_func pMSize, void* pUser_data); |
| 57 |
|
| 58 |
struct crn_file_info { |
| 59 |
inline crn_file_info() |
| 60 |
: m_struct_size(sizeof(crn_file_info)) {} |
| 61 |
|
| 62 |
uint32 m_struct_size; |
| 63 |
uint32 m_actual_data_size; |
| 64 |
uint32 m_header_size; |
| 65 |
uint32 m_total_palette_size; |
| 66 |
uint32 m_tables_size; |
| 67 |
uint32 m_levels; |
| 68 |
uint32 m_level_compressed_size[cCRNMaxLevels]; |
| 69 |
uint32 m_color_endpoint_palette_entries; |
| 70 |
uint32 m_color_selector_palette_entries; |
| 71 |
uint32 m_alpha_endpoint_palette_entries; |
| 72 |
uint32 m_alpha_selector_palette_entries; |
| 73 |
}; |
| 74 |
|
| 75 |
struct crn_texture_info { |
| 76 |
inline crn_texture_info() |
| 77 |
: m_struct_size(sizeof(crn_texture_info)) {} |
| 78 |
|
| 79 |
uint32 m_struct_size; |
| 80 |
uint32 m_width; |
| 81 |
uint32 m_height; |
| 82 |
uint32 m_levels; |
| 83 |
uint32 m_faces; |
| 84 |
uint32 m_bytes_per_block; |
| 85 |
uint32 m_userdata0; |
| 86 |
uint32 m_userdata1; |
| 87 |
crn_format m_format; |
| 88 |
}; |
| 89 |
|
| 90 |
struct crn_level_info { |
| 91 |
inline crn_level_info() |
| 92 |
: m_struct_size(sizeof(crn_level_info)) {} |
| 93 |
|
| 94 |
uint32 m_struct_size; |
| 95 |
uint32 m_width; |
| 96 |
uint32 m_height; |
| 97 |
uint32 m_faces; |
| 98 |
uint32 m_blocks_x; |
| 99 |
uint32 m_blocks_y; |
| 100 |
uint32 m_bytes_per_block; |
| 101 |
crn_format m_format; |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
uint32 crnd_crn_format_to_fourcc(crn_format fmt); |
| 106 |
|
| 107 |
|
| 108 |
crn_format crnd_get_fundamental_dxt_format(crn_format fmt); |
| 109 |
|
| 110 |
|
| 111 |
uint32 crnd_get_crn_format_bits_per_texel(crn_format fmt); |
| 112 |
|
| 113 |
|
| 114 |
uint32 crnd_get_bytes_per_dxt_block(crn_format fmt); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
bool crnd_validate_header(const void* pData, uint32 data_size, crn_file_info* pFile_info); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFile_info); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
bool crnd_get_texture_info(const void* pData, uint32 data_size, crn_texture_info* pTexture_info); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
bool crnd_get_level_info(const void* pData, uint32 data_size, uint32 level_index, crn_level_info* pLevel_info); |
| 135 |
|
| 136 |
|
| 137 |
typedef void* crnd_unpack_context; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
crnd_unpack_context crnd_unpack_begin(const void* pData, uint32 data_size); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
bool crnd_get_data(crnd_unpack_context pContext, const void** ppData, uint32* pData_size); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
bool crnd_unpack_level( |
| 162 |
crnd_unpack_context pContext, |
| 163 |
void** ppDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes, |
| 164 |
uint32 level_index); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
bool crnd_unpack_level_segmented( |
| 170 |
crnd_unpack_context pContext, |
| 171 |
const void* pSrc, uint32 src_size_in_bytes, |
| 172 |
void** ppDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes, |
| 173 |
uint32 level_index); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
bool crnd_unpack_end(crnd_unpack_context pContext); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
const void* crnd_get_level_data(const void* pData, uint32 data_size, uint32 level_index, uint32* pSize); |
| 187 |
|
| 188 |
|
| 189 |
uint32 crnd_get_segmented_file_size(const void* pData, uint32 data_size); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
bool crnd_create_segmented_file(const void* pData, uint32 data_size, void* pBase_data, uint base_data_size); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
namespace crnd { |
| 200 |
template <unsigned int N> |
| 201 |
struct crn_packed_uint { |
| 202 |
inline crn_packed_uint() {} |
| 203 |
|
| 204 |
inline crn_packed_uint(unsigned int val) { *this = val; } |
| 205 |
|
| 206 |
inline crn_packed_uint(const crn_packed_uint& other) { *this = other; } |
| 207 |
|
| 208 |
inline crn_packed_uint& operator=(const crn_packed_uint& rhs) { |
| 209 |
if (this != &rhs) |
| 210 |
memcpy(m_buf, rhs.m_buf, sizeof(m_buf)); |
| 211 |
return *this; |
| 212 |
} |
| 213 |
|
| 214 |
inline crn_packed_uint& operator=(unsigned int val) { |
| 215 |
|
| 216 |
|
| 217 |
val <<= (8U * (4U - N)); |
| 218 |
|
| 219 |
for (unsigned int i = 0; i < N; i++) { |
| 220 |
m_buf[i] = static_cast<unsigned char>(val >> 24U); |
| 221 |
val <<= 8U; |
| 222 |
} |
| 223 |
|
| 224 |
return *this; |
| 225 |
} |
| 226 |
|
| 227 |
inline operator unsigned int() const { |
| 228 |
switch (N) { |
| 229 |
case 1: |
| 230 |
return m_buf[0]; |
| 231 |
case 2: |
| 232 |
return (m_buf[0] << 8U) | m_buf[1]; |
| 233 |
case 3: |
| 234 |
return (m_buf[0] << 16U) | (m_buf[1] << 8U) | (m_buf[2]); |
| 235 |
default: |
| 236 |
return (m_buf[0] << 24U) | (m_buf[1] << 16U) | (m_buf[2] << 8U) | (m_buf[3]); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
unsigned char m_buf[N]; |
| 241 |
}; |
| 242 |
|
| 243 |
#pragma pack(push) |
| 244 |
#pragma pack(1) |
| 245 |
struct crn_palette { |
| 246 |
crn_packed_uint<3> m_ofs; |
| 247 |
crn_packed_uint<3> m_size; |
| 248 |
crn_packed_uint<2> m_num; |
| 249 |
}; |
| 250 |
|
| 251 |
enum crn_header_flags { |
| 252 |
|
| 253 |
cCRNHeaderFlagSegmented = 1 |
| 254 |
}; |
| 255 |
|
| 256 |
struct crn_header { |
| 257 |
enum { cCRNSigValue = ('H' << 8) | 'x' }; |
| 258 |
|
| 259 |
crn_packed_uint<2> m_sig; |
| 260 |
crn_packed_uint<2> m_header_size; |
| 261 |
crn_packed_uint<2> m_header_crc16; |
| 262 |
|
| 263 |
crn_packed_uint<4> m_data_size; |
| 264 |
crn_packed_uint<2> m_data_crc16; |
| 265 |
|
| 266 |
crn_packed_uint<2> m_width; |
| 267 |
crn_packed_uint<2> m_height; |
| 268 |
|
| 269 |
crn_packed_uint<1> m_levels; |
| 270 |
crn_packed_uint<1> m_faces; |
| 271 |
|
| 272 |
crn_packed_uint<1> m_format; |
| 273 |
crn_packed_uint<2> m_flags; |
| 274 |
|
| 275 |
crn_packed_uint<4> m_reserved; |
| 276 |
crn_packed_uint<4> m_userdata0; |
| 277 |
crn_packed_uint<4> m_userdata1; |
| 278 |
|
| 279 |
crn_palette m_color_endpoints; |
| 280 |
crn_palette m_color_selectors; |
| 281 |
|
| 282 |
crn_palette m_alpha_endpoints; |
| 283 |
crn_palette m_alpha_selectors; |
| 284 |
|
| 285 |
crn_packed_uint<2> m_tables_size; |
| 286 |
crn_packed_uint<3> m_tables_ofs; |
| 287 |
|
| 288 |
|
| 289 |
crn_packed_uint<4> m_level_ofs[1]; |
| 290 |
}; |
| 291 |
|
| 292 |
const unsigned int cCRNHeaderMinSize = 62U; |
| 293 |
|
| 294 |
#pragma pack(pop) |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
#endif |