├── bin
├── crunch.exe
├── crunch.pdb
└── crunch_x64.exe
├── bin_osx
└── crunch
├── bin_linux
└── crunch
├── bin_mingw
└── crunch.exe
├── crnlib
├── crn_zeng.cpp
├── crn_decomp.cpp
├── lzma_7zVersion.h
├── crn_threading.h
├── lzma_MyVersion.h
├── crn_winhdr.h
├── crn_checksum.h
├── crn_core.cpp
├── crn_zeng.h
├── crn_value.cpp
├── crn_colorized_console.h
├── crn_huffman_codes.h
├── crn_resample_filters.h
├── lzma_7zCrc.h
├── osx_pthread_spinlock_shim.h
├── lzma_Alloc.h
├── lzma_7zBuf.cpp
├── lzma_7zBuf.h
├── lzma_Bcj2.h
├── crn_ryg_dxt.hpp
├── crn_hash.h
├── lzma_7zCrc.cpp
├── crn_dxt_fast.h
├── lzma_7zBuf2.cpp
├── crn_vec_interval.h
├── crn_strutils.h
├── crn_dxt_hc_common.h
├── crn_dxt_hc_common.cpp
├── crn_texture_comp.h
├── crn_dds_comp.h
├── crn_ray.h
├── lzma_7zFile.h
├── lzma_LzmaLib.cpp
├── crn_assert.cpp
├── crn_dxt_endpoint_refiner.h
├── crn_dxt5a.h
├── crn_texture_file_types.h
├── crn_checksum.cpp
├── crn_find_files.h
├── crn_utils.cpp
├── crn_platform.cpp
├── crn_file_utils.h
├── crn_helpers.h
├── lzma_BraIA64.cpp
├── lzma_Bra.h
├── crn_hash.cpp
├── crn_timer.h
├── crn_lzma_codec.h
├── lzma_LzHash.h
├── crn_assert.h
├── crn_types.h
├── lzma_CpuArch.h
├── lzma_Threads.h
├── crn_arealist.h
├── crn_math.cpp
├── crn_threaded_resampler.h
├── crn_packed_uint.h
├── Makefile
├── lzma_Bra86.cpp
├── crn_vector.cpp
├── lzma_LzFindMt.h
├── crn_rand.h
├── crn_texture_file_types.cpp
├── crn_platform.h
├── lzma_LzmaEnc.h
├── crn_data_stream.h
├── crn_data_stream.cpp
├── lzma_Alloc.cpp
├── crn_rg_etc1.h
├── lzham_timer.h
├── crn_command_line_params.h
├── crn_colorized_console.cpp
├── lzma_LzFind.h
├── lzma_Threads.cpp
├── lzma_Bra.cpp
├── lzham_timer.cpp
├── lzma_Bcj2.cpp
├── crn_timer.cpp
├── crn_intersect.h
├── crn_prefix_coding.h
├── crn_texture_conversion.h
├── crn_console.h
├── crn_hash_map.cpp
├── crn_lzma_codec.cpp
├── crn_buffer_stream.h
├── lzma_7zStream.cpp
└── crn_vector2d.h
├── crn.workspace
├── crn_linux.workspace
├── crunch
├── corpus_gen.h
├── corpus_test.h
├── crunch.cbp
└── crunch_linux.cbp
├── license.txt
├── example2
├── timer.h
└── timer.cpp
├── crn.2008.sln
└── crn_examples.2008.sln
/bin/crunch.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BKcore/crunch-osx/HEAD/bin/crunch.exe
--------------------------------------------------------------------------------
/bin/crunch.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BKcore/crunch-osx/HEAD/bin/crunch.pdb
--------------------------------------------------------------------------------
/bin_osx/crunch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BKcore/crunch-osx/HEAD/bin_osx/crunch
--------------------------------------------------------------------------------
/bin_linux/crunch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BKcore/crunch-osx/HEAD/bin_linux/crunch
--------------------------------------------------------------------------------
/bin/crunch_x64.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BKcore/crunch-osx/HEAD/bin/crunch_x64.exe
--------------------------------------------------------------------------------
/bin_mingw/crunch.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BKcore/crunch-osx/HEAD/bin_mingw/crunch.exe
--------------------------------------------------------------------------------
/crnlib/crn_zeng.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BKcore/crunch-osx/HEAD/crnlib/crn_zeng.cpp
--------------------------------------------------------------------------------
/crnlib/crn_decomp.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_decomp.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 |
5 | // Include the single-file header library with no defines, which brings in the full CRN decompressor.
6 | #include "../inc/crn_decomp.h"
7 |
--------------------------------------------------------------------------------
/crnlib/lzma_7zVersion.h:
--------------------------------------------------------------------------------
1 | #define MY_VER_MAJOR 4
2 | #define MY_VER_MINOR 63
3 | #define MY_VER_BUILD 0
4 | #define MY_VERSION "4.63"
5 | #define MY_DATE "2008-12-31"
6 | #define MY_COPYRIGHT ": Igor Pavlov : Public domain"
7 | #define MY_VERSION_COPYRIGHT_DATE MY_VERSION " " MY_COPYRIGHT " : " MY_DATE
8 |
--------------------------------------------------------------------------------
/crnlib/crn_threading.h:
--------------------------------------------------------------------------------
1 | // File: crn_threading.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 |
4 | #if CRNLIB_USE_WIN32_API
5 | #include "crn_threading_win32.h"
6 | #elif CRNLIB_USE_PTHREADS_API
7 | #include "crn_threading_pthreads.h"
8 | #else
9 | #include "crn_threading_null.h"
10 | #endif
11 |
--------------------------------------------------------------------------------
/crnlib/lzma_MyVersion.h:
--------------------------------------------------------------------------------
1 | #define MY_VER_MAJOR 4
2 | #define MY_VER_MINOR 63
3 | #define MY_VER_BUILD 0
4 | #define MY_VERSION "4.63"
5 | #define MY_7ZIP_VERSION "7-Zip 4.63"
6 | #define MY_DATE "2008-12-31"
7 | #define MY_COPYRIGHT "Copyright (c) 1999-2008 Igor Pavlov"
8 | #define MY_VERSION_COPYRIGHT_DATE MY_VERSION " " MY_COPYRIGHT " " MY_DATE
9 |
--------------------------------------------------------------------------------
/crn.workspace:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/crnlib/crn_winhdr.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #ifndef WIN32
4 | #error Should not get here
5 | #endif
6 |
7 | #ifndef _WIN32_WINNT
8 | #define _WIN32_WINNT 0x500
9 | #endif
10 |
11 | #ifndef WIN32_LEAN_AND_MEAN
12 | #define WIN32_LEAN_AND_MEAN
13 | #endif
14 |
15 | #ifndef NOMINMAX
16 | #define NOMINMAX
17 | #endif
18 |
19 | #include "windows.h"
20 |
--------------------------------------------------------------------------------
/crn_linux.workspace:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/crnlib/crn_checksum.h:
--------------------------------------------------------------------------------
1 | // File: crn_checksum.h
2 | #pragma once
3 |
4 | namespace crnlib
5 | {
6 | const uint cInitAdler32 = 1U;
7 | uint adler32(const void* pBuf, size_t buflen, uint adler32 = cInitAdler32);
8 |
9 | // crc16() intended for small buffers - doesn't use an acceleration table.
10 | const uint cInitCRC16 = 0;
11 | uint16 crc16(const void* pBuf, size_t len, uint16 crc = cInitCRC16);
12 |
13 | } // namespace crnlib
14 |
--------------------------------------------------------------------------------
/crnlib/crn_core.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_core.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 |
5 | #if CRNLIB_USE_WIN32_API
6 | #include "crn_winhdr.h"
7 | #endif
8 |
9 | namespace crnlib
10 | {
11 | const char *g_copyright_str = "Copyright (c) 2010-2012 Rich Geldreich and Tenacious Software LLC";
12 | const char *g_sig_str = "C8cfRlaorj0wLtnMSxrBJxTC85rho2L9hUZKHcBL";
13 |
14 | } // namespace crnlib
15 |
--------------------------------------------------------------------------------
/crnlib/crn_zeng.h:
--------------------------------------------------------------------------------
1 | // File: crn_zeng.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 |
4 | namespace crnlib
5 | {
6 | typedef float (*zeng_similarity_func)(uint index_a, uint index_b, void* pContext);
7 |
8 | void create_zeng_reorder_table(uint n, uint num_indices, const uint* pIndices, crnlib::vector& remap_table, zeng_similarity_func pFunc, void* pContext, float similarity_func_weight);
9 |
10 | } // namespace crnlib
11 |
--------------------------------------------------------------------------------
/crunch/corpus_gen.h:
--------------------------------------------------------------------------------
1 | // File: corpus_gen.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_command_line_params.h"
5 | #include "crn_image.h"
6 |
7 | namespace crnlib
8 | {
9 | class corpus_gen
10 | {
11 | public:
12 | corpus_gen();
13 |
14 | bool generate(const char* pCmd_line);
15 |
16 | private:
17 | void sort_blocks(image_u8& img);
18 | };
19 |
20 | } // namespace crnlib
21 |
--------------------------------------------------------------------------------
/crnlib/crn_value.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_value.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 | #include "crn_value.h"
5 |
6 | namespace crnlib
7 | {
8 | const char* gValueDataTypeStrings[cDTTotal + 1] =
9 | {
10 | "invalid",
11 | "string",
12 | "bool",
13 | "int",
14 | "uint",
15 | "float",
16 | "vec3f",
17 | "vec3i",
18 |
19 | NULL,
20 | };
21 |
22 | } // namespace crnlib
23 |
--------------------------------------------------------------------------------
/crnlib/crn_colorized_console.h:
--------------------------------------------------------------------------------
1 | // File: crn_colorized_console.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_console.h"
5 |
6 | namespace crnlib
7 | {
8 | class colorized_console
9 | {
10 | public:
11 | static void init();
12 | static void deinit();
13 | static void tick();
14 |
15 | private:
16 | static bool console_output_func(eConsoleMessageType type, const char* pMsg, void* pData);
17 | };
18 |
19 | } // namespace crnlib
20 |
--------------------------------------------------------------------------------
/crnlib/crn_huffman_codes.h:
--------------------------------------------------------------------------------
1 | // File: crn_huffman_codes.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | const uint cHuffmanMaxSupportedSyms = 8192;
8 |
9 | void* create_generate_huffman_codes_tables();
10 | void free_generate_huffman_codes_tables(void* p);
11 |
12 | bool generate_huffman_codes(void* pContext, uint num_syms, const uint16* pFreq, uint8* pCodesizes, uint& max_code_size, uint& total_freq_ret);
13 |
14 | } // namespace crnlib
15 |
--------------------------------------------------------------------------------
/crnlib/crn_resample_filters.h:
--------------------------------------------------------------------------------
1 | // File: crn_resample_filters.h
2 | // RG: This is public domain code, originally derived from Graphics Gems 3, see: http://code.google.com/p/imageresampler/
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | typedef float (*resample_filter_func)(float t);
8 |
9 | struct resample_filter
10 | {
11 | char name[32];
12 | resample_filter_func func;
13 | float support;
14 | };
15 |
16 | extern const resample_filter g_resample_filters[];
17 | extern const int g_num_resample_filters;
18 |
19 | int find_resample_filter(const char* pName);
20 |
21 | } // namespace crnlib
22 |
--------------------------------------------------------------------------------
/crnlib/lzma_7zCrc.h:
--------------------------------------------------------------------------------
1 | /* 7zCrc.h -- CRC32 calculation
2 | 2008-03-13
3 | Igor Pavlov
4 | Public domain */
5 |
6 | #ifndef __7Z_CRC_H
7 | #define __7Z_CRC_H
8 |
9 | #include
10 |
11 | #include "lzma_Types.h"
12 |
13 | namespace crnlib {
14 |
15 | extern UInt32 g_CrcTable[];
16 |
17 | void MY_FAST_CALL CrcGenerateTable(void);
18 |
19 | #define CRC_INIT_VAL 0xFFFFFFFF
20 | #define CRC_GET_DIGEST(crc) ((crc) ^ 0xFFFFFFFF)
21 | #define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
22 |
23 | UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void *data, size_t size);
24 | UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size);
25 |
26 | }
27 |
28 | #endif
29 |
--------------------------------------------------------------------------------
/crnlib/osx_pthread_spinlock_shim.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | typedef OSSpinLock pthread_spinlock_t;
6 |
7 | static inline int pthread_spin_init(pthread_spinlock_t *lock, int pshared) {
8 | OSSpinLockUnlock(lock);
9 | return 0;
10 | }
11 |
12 | static inline int pthread_spin_destroy(pthread_spinlock_t *lock) {
13 | return 0;
14 | }
15 |
16 | static inline int pthread_spin_lock(pthread_spinlock_t *lock) {
17 | OSSpinLockLock(lock);
18 | return 0;
19 | }
20 |
21 | static inline int pthread_spin_trylock(pthread_spinlock_t *lock) {
22 | if (OSSpinLockTry(lock)) {
23 | return 0;
24 | }
25 | return EBUSY;
26 | }
27 |
28 | static inline int pthread_spin_unlock(pthread_spinlock_t *lock) {
29 | OSSpinLockUnlock(lock);
30 | return 0;
31 | }
--------------------------------------------------------------------------------
/crnlib/lzma_Alloc.h:
--------------------------------------------------------------------------------
1 | /* Alloc.h -- Memory allocation functions
2 | 2008-03-13
3 | Igor Pavlov
4 | Public domain */
5 |
6 | #ifndef __COMMON_ALLOC_H
7 | #define __COMMON_ALLOC_H
8 |
9 | #include
10 |
11 | namespace crnlib {
12 |
13 | void *MyAlloc(size_t size);
14 | void MyFree(void *address);
15 |
16 | #ifdef _WIN32
17 |
18 | void SetLargePageSize();
19 |
20 | void *MidAlloc(size_t size);
21 | void MidFree(void *address);
22 | void *BigAlloc(size_t size);
23 | void BigFree(void *address);
24 |
25 | #else
26 |
27 | #define MidAlloc(size) MyAlloc(size)
28 | #define MidFree(address) MyFree(address)
29 | #define BigAlloc(size) MyAlloc(size)
30 | #define BigFree(address) MyFree(address)
31 |
32 | #endif
33 |
34 | }
35 |
36 | #endif
37 |
--------------------------------------------------------------------------------
/crnlib/lzma_7zBuf.cpp:
--------------------------------------------------------------------------------
1 | /* 7zBuf.c -- Byte Buffer
2 | 2008-03-28
3 | Igor Pavlov
4 | Public domain */
5 | #include "crn_core.h"
6 |
7 | #include "lzma_7zBuf.h"
8 |
9 | namespace crnlib {
10 |
11 | void Buf_Init(CBuf *p)
12 | {
13 | p->data = 0;
14 | p->size = 0;
15 | }
16 |
17 | int Buf_Create(CBuf *p, size_t size, ISzAlloc *alloc)
18 | {
19 | p->size = 0;
20 | if (size == 0)
21 | {
22 | p->data = 0;
23 | return 1;
24 | }
25 | p->data = (Byte *)alloc->Alloc(alloc, size);
26 | if (p->data != 0)
27 | {
28 | p->size = size;
29 | return 1;
30 | }
31 | return 0;
32 | }
33 |
34 | void Buf_Free(CBuf *p, ISzAlloc *alloc)
35 | {
36 | alloc->Free(alloc, p->data);
37 | p->data = 0;
38 | p->size = 0;
39 | }
40 |
41 | }
--------------------------------------------------------------------------------
/crnlib/lzma_7zBuf.h:
--------------------------------------------------------------------------------
1 | /* 7zBuf.h -- Byte Buffer
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 |
4 | #ifndef __7Z_BUF_H
5 | #define __7Z_BUF_H
6 |
7 | #include "lzma_Types.h"
8 |
9 | namespace crnlib {
10 |
11 | typedef struct
12 | {
13 | Byte *data;
14 | size_t size;
15 | } CBuf;
16 |
17 | void Buf_Init(CBuf *p);
18 | int Buf_Create(CBuf *p, size_t size, ISzAlloc *alloc);
19 | void Buf_Free(CBuf *p, ISzAlloc *alloc);
20 |
21 | typedef struct
22 | {
23 | Byte *data;
24 | size_t size;
25 | size_t pos;
26 | } CDynBuf;
27 |
28 | void DynBuf_Construct(CDynBuf *p);
29 | void DynBuf_SeekToBeg(CDynBuf *p);
30 | int DynBuf_Write(CDynBuf *p, const Byte *buf, size_t size, ISzAlloc *alloc);
31 | void DynBuf_Free(CDynBuf *p, ISzAlloc *alloc);
32 |
33 | }
34 |
35 | #endif
36 |
--------------------------------------------------------------------------------
/crnlib/lzma_Bcj2.h:
--------------------------------------------------------------------------------
1 | /* Bcj2.h -- Converter for x86 code (BCJ2)
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 |
4 | #ifndef __BCJ2_H
5 | #define __BCJ2_H
6 |
7 | #include "lzma_Types.h"
8 |
9 | namespace crnlib {
10 |
11 | /*
12 | Conditions:
13 | outSize <= FullOutputSize,
14 | where FullOutputSize is full size of output stream of x86_2 filter.
15 |
16 | If buf0 overlaps outBuf, there are two required conditions:
17 | 1) (buf0 >= outBuf)
18 | 2) (buf0 + size0 >= outBuf + FullOutputSize).
19 |
20 | Returns:
21 | SZ_OK
22 | SZ_ERROR_DATA - Data error
23 | */
24 |
25 | int Bcj2_Decode(
26 | const Byte *buf0, SizeT size0,
27 | const Byte *buf1, SizeT size1,
28 | const Byte *buf2, SizeT size2,
29 | const Byte *buf3, SizeT size3,
30 | Byte *outBuf, SizeT outSize);
31 |
32 | }
33 |
34 | #endif
35 |
--------------------------------------------------------------------------------
/crnlib/crn_ryg_dxt.hpp:
--------------------------------------------------------------------------------
1 | // File: ryg_dxt.hpp
2 | #pragma once
3 |
4 | #include "crn_ryg_types.hpp"
5 |
6 | namespace ryg_dxt
7 | {
8 | extern sU8 Expand5[32];
9 | extern sU8 Expand6[64];
10 | extern sU8 OMatch5[256][2];
11 | extern sU8 OMatch6[256][2];
12 | extern sU8 OMatch5_3[256][2];
13 | extern sU8 OMatch6_3[256][2];
14 | extern sU8 QuantRBTab[256+16];
15 | extern sU8 QuantGTab[256+16];
16 |
17 | // initialize DXT codec. only needs to be called once.
18 | void sInitDXT();
19 |
20 | // input: a 4x4 pixel block, A8R8G8B8. you need to handle boundary cases
21 | // yourself.
22 | // alpha=sTRUE => use DXT5 (else use DXT1)
23 | // quality: 0=fastest (no dither), 1=medium (dither)
24 | void sCompressDXTBlock(sU8 *dest,const sU32 *src,sBool alpha,sInt quality);
25 |
26 | void sCompressDXT5ABlock(sU8 *dest,const sU32 *src,sInt quality);
27 |
28 | } // namespace ryg_dxt
29 |
30 |
--------------------------------------------------------------------------------
/crnlib/crn_hash.h:
--------------------------------------------------------------------------------
1 | // File: crn_hash.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | uint32 fast_hash (const void* p, int len);
8 |
9 | // 4-byte integer hash, full avalanche
10 | inline uint32 bitmix32c(uint32 a)
11 | {
12 | a = (a+0x7ed55d16) + (a<<12);
13 | a = (a^0xc761c23c) ^ (a>>19);
14 | a = (a+0x165667b1) + (a<<5);
15 | a = (a+0xd3a2646c) ^ (a<<9);
16 | a = (a+0xfd7046c5) + (a<<3);
17 | a = (a^0xb55a4f09) ^ (a>>16);
18 | return a;
19 | }
20 |
21 | // 4-byte integer hash, full avalanche, no constants
22 | inline uint32 bitmix32(uint32 a)
23 | {
24 | a -= (a<<6);
25 | a ^= (a>>17);
26 | a -= (a<<9);
27 | a ^= (a<<4);
28 | a -= (a<<3);
29 | a ^= (a<<10);
30 | a ^= (a>>15);
31 | return a;
32 | }
33 |
34 | } // namespace crnlib
35 |
--------------------------------------------------------------------------------
/crnlib/lzma_7zCrc.cpp:
--------------------------------------------------------------------------------
1 | /* 7zCrc.c -- CRC32 calculation
2 | 2008-08-05
3 | Igor Pavlov
4 | Public domain */
5 | #include "crn_core.h"
6 |
7 | #include "lzma_7zCrc.h"
8 |
9 | namespace crnlib {
10 |
11 | #define kCrcPoly 0xEDB88320
12 | UInt32 g_CrcTable[256];
13 |
14 | void MY_FAST_CALL CrcGenerateTable(void)
15 | {
16 | UInt32 i;
17 | for (i = 0; i < 256; i++)
18 | {
19 | UInt32 r = i;
20 | int j;
21 | for (j = 0; j < 8; j++)
22 | r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
23 | g_CrcTable[i] = r;
24 | }
25 | }
26 |
27 | UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size)
28 | {
29 | const Byte *p = (const Byte *)data;
30 | for (; size > 0 ; size--, p++)
31 | v = CRC_UPDATE_BYTE(v, *p);
32 | return v;
33 | }
34 |
35 | UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size)
36 | {
37 | return CrcUpdate(CRC_INIT_VAL, data, size) ^ 0xFFFFFFFF;
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/crnlib/crn_dxt_fast.h:
--------------------------------------------------------------------------------
1 | // File: crn_dxt_fast.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_color.h"
5 | #include "crn_dxt.h"
6 |
7 | namespace crnlib
8 | {
9 | namespace dxt_fast
10 | {
11 | void compress_color_block(uint n, const color_quad_u8* block, uint& low16, uint& high16, uint8* pSelectors, bool refine = false);
12 | void compress_color_block(dxt1_block* pDXT1_block, const color_quad_u8* pBlock, bool refine = false);
13 |
14 | void compress_alpha_block(uint n, const color_quad_u8* block, uint& low8, uint& high8, uint8* pSelectors, uint comp_index);
15 | void compress_alpha_block(dxt5_block* pDXT5_block, const color_quad_u8* pBlock, uint comp_index);
16 |
17 | void find_representative_colors(uint n, const color_quad_u8* pBlock, color_quad_u8& lo, color_quad_u8& hi);
18 |
19 | } // namespace dxt_fast
20 |
21 | } // namespace crnlib
22 |
--------------------------------------------------------------------------------
/crunch/corpus_test.h:
--------------------------------------------------------------------------------
1 | // File: corpus_test.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_command_line_params.h"
5 | #include "crn_image_utils.h"
6 |
7 | namespace crnlib
8 | {
9 | class corpus_tester
10 | {
11 | public:
12 | corpus_tester();
13 |
14 | bool test(const char* pCmd_line);
15 |
16 | private:
17 | void print_comparative_metric_stats(const command_line_params& params, const crnlib::vector& stats1, const crnlib::vector& stats2, uint num_blocks_x, uint num_blocks_y);
18 | void print_metric_stats(const crnlib::vector& stats, uint num_blocks_x, uint num_blocks_y);
19 |
20 | image_u8 m_bad_block_img;
21 | uint m_next_bad_block_index;
22 | uint m_total_bad_block_files;
23 |
24 | void flush_bad_blocks();
25 | void add_bad_block(image_u8& block);
26 | };
27 |
28 | } // namespace crnlib
29 |
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | crunch/crnlib uses the ZLIB license:
2 | http://opensource.org/licenses/Zlib
3 |
4 | Copyright (c) 2010-2012 Rich Geldreich and Tenacious Software LLC
5 |
6 | This software is provided 'as-is', without any express or implied
7 | warranty. In no event will the authors be held liable for any damages
8 | arising from the use of this software.
9 |
10 | Permission is granted to anyone to use this software for any purpose,
11 | including commercial applications, and to alter it and redistribute it
12 | freely, subject to the following restrictions:
13 |
14 | 1. The origin of this software must not be misrepresented; you must not
15 | claim that you wrote the original software. If you use this software
16 | in a product, an acknowledgment in the product documentation would be
17 | appreciated but is not required.
18 |
19 | 2. Altered source versions must be plainly marked as such, and must not be
20 | misrepresented as being the original software.
21 |
22 | 3. This notice may not be removed or altered from any source distribution.
23 |
--------------------------------------------------------------------------------
/crnlib/lzma_7zBuf2.cpp:
--------------------------------------------------------------------------------
1 | /* 7zBuf2.c -- Byte Buffer
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 |
4 | #include "crn_core.h"
5 | #include
6 | #include "lzma_7zBuf.h"
7 |
8 | namespace crnlib {
9 |
10 | void DynBuf_Construct(CDynBuf *p)
11 | {
12 | p->data = 0;
13 | p->size = 0;
14 | p->pos = 0;
15 | }
16 |
17 | void DynBuf_SeekToBeg(CDynBuf *p)
18 | {
19 | p->pos = 0;
20 | }
21 |
22 | int DynBuf_Write(CDynBuf *p, const Byte *buf, size_t size, ISzAlloc *alloc)
23 | {
24 | if (size > p->size - p->pos)
25 | {
26 | size_t newSize = p->pos + size;
27 | Byte *data;
28 | newSize += newSize / 4;
29 | data = (Byte *)alloc->Alloc(alloc, newSize);
30 | if (data == 0)
31 | return 0;
32 | p->size = newSize;
33 | memcpy(data, p->data, p->pos);
34 | alloc->Free(alloc, p->data);
35 | p->data = data;
36 | }
37 | memcpy(p->data + p->pos, buf, size);
38 | p->pos += size;
39 | return 1;
40 | }
41 |
42 | void DynBuf_Free(CDynBuf *p, ISzAlloc *alloc)
43 | {
44 | alloc->Free(alloc, p->data);
45 | p->data = 0;
46 | p->size = 0;
47 | p->pos = 0;
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/crnlib/crn_vec_interval.h:
--------------------------------------------------------------------------------
1 | // File: crn_vec_interval.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_vec.h"
5 |
6 | namespace crnlib
7 | {
8 | template
9 | class vec_interval
10 | {
11 | public:
12 | enum { N = T::num_elements };
13 | typedef typename T::scalar_type scalar_type;
14 |
15 | inline vec_interval(const T& v) { m_bounds[0] = v; m_bounds[1] = v; }
16 | inline vec_interval(const T& low, const T& high) { m_bounds[0] = low; m_bounds[1] = high; }
17 |
18 | inline void clear() { m_bounds[0].clear(); m_bounds[1].clear(); }
19 |
20 | inline const T& operator[] (uint i) const { CRNLIB_ASSERT(i < 2); return m_bounds[i]; }
21 | inline T& operator[] (uint i) { CRNLIB_ASSERT(i < 2); return m_bounds[i]; }
22 |
23 | private:
24 | T m_bounds[2];
25 | };
26 |
27 | typedef vec_interval vec_interval1F;
28 | typedef vec_interval vec_interval2F;
29 | typedef vec_interval vec_interval3F;
30 | typedef vec_interval vec_interval4F;
31 |
32 | typedef vec_interval2F aabb2F;
33 | typedef vec_interval3F aabb3F;
34 |
35 | } // namespace crnlib
36 |
--------------------------------------------------------------------------------
/crnlib/crn_strutils.h:
--------------------------------------------------------------------------------
1 | // File: crn_strutils.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | #ifdef WIN32
6 | #define CRNLIB_PATH_SEPERATOR_CHAR '\\'
7 | #else
8 | #define CRNLIB_PATH_SEPERATOR_CHAR '/'
9 | #endif
10 |
11 | namespace crnlib
12 | {
13 | char* crn_strdup(const char* pStr);
14 | int crn_stricmp(const char *p, const char *q);
15 |
16 | char* strcpy_safe(char* pDst, uint dst_len, const char* pSrc);
17 |
18 | bool int_to_string(int value, char* pDst, uint len);
19 | bool uint_to_string(uint value, char* pDst, uint len);
20 |
21 | bool string_to_int(const char*& pBuf, int& value);
22 |
23 | bool string_to_uint(const char*& pBuf, uint& value);
24 |
25 | bool string_to_int64(const char*& pBuf, int64& value);
26 | bool string_to_uint64(const char*& pBuf, uint64& value);
27 |
28 | bool string_to_bool(const char* p, bool& value);
29 |
30 | bool string_to_float(const char*& p, float& value, uint round_digit = 512U);
31 |
32 | bool string_to_double(const char*& p, double& value, uint round_digit = 512U);
33 | bool string_to_double(const char*& p, const char *pEnd, double& value, uint round_digit = 512U);
34 |
35 | } // namespace crnlib
36 |
--------------------------------------------------------------------------------
/example2/timer.h:
--------------------------------------------------------------------------------
1 | // File: timer.h
2 | // A simple high-precision, platform independent timer class.
3 | #pragma once
4 |
5 | typedef unsigned long long timer_ticks;
6 |
7 | class timer
8 | {
9 | public:
10 | timer();
11 | timer(timer_ticks start_ticks);
12 |
13 | void start();
14 | void start(timer_ticks start_ticks);
15 |
16 | void stop();
17 |
18 | double get_elapsed_secs() const;
19 | inline double get_elapsed_ms() const { return get_elapsed_secs() * 1000.0f; }
20 | timer_ticks get_elapsed_us() const;
21 |
22 | static void init();
23 | static inline timer_ticks get_ticks_per_sec() { return g_freq; }
24 | static timer_ticks get_init_ticks();
25 | static timer_ticks get_ticks();
26 | static double ticks_to_secs(timer_ticks ticks);
27 | static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; }
28 | static inline double get_secs() { return ticks_to_secs(get_ticks()); }
29 | static inline double get_ms() { return ticks_to_ms(get_ticks()); }
30 |
31 | private:
32 | static timer_ticks g_init_ticks;
33 | static timer_ticks g_freq;
34 | static double g_inv_freq;
35 |
36 | timer_ticks m_start_time;
37 | timer_ticks m_stop_time;
38 |
39 | bool m_started : 1;
40 | bool m_stopped : 1;
41 | };
42 |
--------------------------------------------------------------------------------
/crnlib/crn_dxt_hc_common.h:
--------------------------------------------------------------------------------
1 | // File: crn_dxt_hc_common.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | struct chunk_tile_desc
8 | {
9 | // These values are in pixels, and always a multiple of cBlockPixelWidth/cBlockPixelHeight.
10 | uint m_x_ofs;
11 | uint m_y_ofs;
12 | uint m_width;
13 | uint m_height;
14 | uint m_layout_index;
15 | };
16 |
17 | struct chunk_encoding_desc
18 | {
19 | uint m_num_tiles;
20 | chunk_tile_desc m_tiles[4];
21 | };
22 |
23 | const uint cChunkPixelWidth = 8;
24 | const uint cChunkPixelHeight = 8;
25 | const uint cChunkBlockWidth = 2;
26 | const uint cChunkBlockHeight = 2;
27 |
28 | const uint cChunkMaxTiles = 4;
29 |
30 | const uint cBlockPixelWidthShift = 2;
31 | const uint cBlockPixelHeightShift = 2;
32 |
33 | const uint cBlockPixelWidth = 4;
34 | const uint cBlockPixelHeight = 4;
35 |
36 | const uint cNumChunkEncodings = 8;
37 | extern chunk_encoding_desc g_chunk_encodings[cNumChunkEncodings];
38 |
39 | const uint cNumChunkTileLayouts = 9;
40 | const uint cFirst4x4ChunkTileLayout = 5;
41 | extern chunk_tile_desc g_chunk_tile_layouts[cNumChunkTileLayouts];
42 |
43 | } // namespace crnlib
44 |
--------------------------------------------------------------------------------
/crnlib/crn_dxt_hc_common.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_dxt_hc_common.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 | #include "crn_dxt_hc_common.h"
5 |
6 | namespace crnlib
7 | {
8 | chunk_encoding_desc g_chunk_encodings[cNumChunkEncodings] =
9 | {
10 | { 1, { { 0, 0, 8, 8, 0 } } },
11 |
12 | { 2, { { 0, 0, 8, 4, 1 }, { 0, 4, 8, 4, 2 } } },
13 | { 2, { { 0, 0, 4, 8, 3 }, { 4, 0, 4, 8, 4 } } },
14 |
15 | { 3, { { 0, 0, 8, 4, 1 }, { 0, 4, 4, 4, 7 }, { 4, 4, 4, 4, 8 } } },
16 | { 3, { { 0, 4, 8, 4, 2 }, { 0, 0, 4, 4, 5 }, { 4, 0, 4, 4, 6 } } },
17 |
18 | { 3, { { 0, 0, 4, 8, 3 }, { 4, 0, 4, 4, 6 }, { 4, 4, 4, 4, 8 } } },
19 | { 3, { { 4, 0, 4, 8, 4 }, { 0, 0, 4, 4, 5 }, { 0, 4, 4, 4, 7 } } },
20 |
21 | { 4, { { 0, 0, 4, 4, 5 }, { 4, 0, 4, 4, 6 }, { 0, 4, 4, 4, 7 }, { 4, 4, 4, 4, 8 } } }
22 | };
23 |
24 | chunk_tile_desc g_chunk_tile_layouts[cNumChunkTileLayouts] =
25 | {
26 | // 2x2
27 | { 0, 0, 8, 8, 0 },
28 |
29 | // 2x1
30 | { 0, 0, 8, 4, 1 },
31 | { 0, 4, 8, 4, 2 },
32 |
33 | // 1x2
34 | { 0, 0, 4, 8, 3 },
35 | { 4, 0, 4, 8, 4 },
36 |
37 | // 1x1
38 | { 0, 0, 4, 4, 5 },
39 | { 4, 0, 4, 4, 6 },
40 | { 0, 4, 4, 4, 7 },
41 | { 4, 4, 4, 4, 8 }
42 | };
43 |
44 | } // namespace crnlib
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/crnlib/crn_texture_comp.h:
--------------------------------------------------------------------------------
1 | // File: crn_texture_comp.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | #include "../inc/crnlib.h"
6 |
7 | namespace crnlib
8 | {
9 | class mipmapped_texture;
10 |
11 | class itexture_comp
12 | {
13 | CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(itexture_comp);
14 |
15 | public:
16 | itexture_comp() { }
17 | virtual ~itexture_comp() { }
18 |
19 | virtual const char *get_ext() const = 0;
20 |
21 | virtual bool compress_init(const crn_comp_params& params) = 0;
22 | virtual bool compress_pass(const crn_comp_params& params, float *pEffective_bitrate) = 0;
23 | virtual void compress_deinit() = 0;
24 |
25 | virtual const crnlib::vector& get_comp_data() const = 0;
26 | virtual crnlib::vector& get_comp_data() = 0;
27 | };
28 |
29 | bool create_compressed_texture(const crn_comp_params ¶ms, crnlib::vector &comp_data, uint32 *pActual_quality_level, float *pActual_bitrate);
30 | bool create_texture_mipmaps(mipmapped_texture &work_tex, const crn_comp_params ¶ms, const crn_mipmap_params &mipmap_params, bool generate_mipmaps);
31 | bool create_compressed_texture(const crn_comp_params ¶ms, const crn_mipmap_params &mipmap_params, crnlib::vector &comp_data, uint32 *pActual_quality_level, float *pActual_bitrate);
32 |
33 | } // namespace crnlib
34 |
--------------------------------------------------------------------------------
/crnlib/crn_dds_comp.h:
--------------------------------------------------------------------------------
1 | // File: crn_comp.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_comp.h"
5 | #include "crn_mipmapped_texture.h"
6 | #include "crn_texture_comp.h"
7 |
8 | namespace crnlib
9 | {
10 | class dds_comp : public itexture_comp
11 | {
12 | CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(dds_comp);
13 |
14 | public:
15 | dds_comp();
16 | virtual ~dds_comp();
17 |
18 | virtual const char *get_ext() const { return "DDS"; }
19 |
20 | virtual bool compress_init(const crn_comp_params& params);
21 | virtual bool compress_pass(const crn_comp_params& params, float *pEffective_bitrate);
22 | virtual void compress_deinit();
23 |
24 | virtual const crnlib::vector& get_comp_data() const { return m_comp_data; }
25 | virtual crnlib::vector& get_comp_data() { return m_comp_data; }
26 |
27 | private:
28 | mipmapped_texture m_src_tex;
29 | mipmapped_texture m_packed_tex;
30 |
31 | crnlib::vector m_comp_data;
32 |
33 | const crn_comp_params* m_pParams;
34 |
35 | pixel_format m_pixel_fmt;
36 | dxt_image::pack_params m_pack_params;
37 |
38 | task_pool m_task_pool;
39 | qdxt1_params m_q1_params;
40 | qdxt5_params m_q5_params;
41 | mipmapped_texture::qdxt_state *m_pQDXT_state;
42 |
43 | void clear();
44 | bool create_dds_tex(mipmapped_texture &dds_tex);
45 | bool convert_to_dxt(const crn_comp_params& params);
46 | };
47 |
48 | } // namespace crnlib
49 |
--------------------------------------------------------------------------------
/crnlib/crn_ray.h:
--------------------------------------------------------------------------------
1 | // File: crn_ray.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_vec.h"
5 |
6 | namespace crnlib
7 | {
8 | template
9 | class ray
10 | {
11 | public:
12 | typedef vector_type vector_t;
13 | typedef typename vector_type::scalar_type scalar_type;
14 |
15 | inline ray() { }
16 | inline ray(eClear) { clear(); }
17 | inline ray(const vector_type& origin, const vector_type& direction) : m_origin(origin), m_direction(direction) { }
18 |
19 | inline void clear()
20 | {
21 | m_origin.clear();
22 | m_direction.clear();
23 | }
24 |
25 | inline const vector_type& get_origin(void) const { return m_origin; }
26 | inline void set_origin(const vector_type& origin) { m_origin = origin; }
27 |
28 | inline const vector_type& get_direction(void) const { return m_direction; }
29 | inline void set_direction(const vector_type& direction) { m_direction = direction; }
30 |
31 | inline scalar_type set_endpoints(const vector_type& start, const vector_type& end, const vector_type& def)
32 | {
33 | m_origin = start;
34 |
35 | m_direction = end - start;
36 | return m_direction.normalize(&def);
37 | }
38 |
39 | inline vector_type eval(scalar_type t) const
40 | {
41 | return m_origin + m_direction * t;
42 | }
43 |
44 | private:
45 | vector_type m_origin;
46 | vector_type m_direction;
47 | };
48 |
49 | typedef ray ray2F;
50 | typedef ray ray3F;
51 |
52 | } // namespace crnlib
53 |
--------------------------------------------------------------------------------
/crnlib/lzma_7zFile.h:
--------------------------------------------------------------------------------
1 | /* 7zFile.h -- File IO
2 | 2008-11-22 : Igor Pavlov : Public domain */
3 |
4 | #ifndef __7Z_FILE_H
5 | #define __7Z_FILE_H
6 |
7 | #ifdef _WIN32
8 | #define USE_WINDOWS_FILE
9 | #endif
10 |
11 | #ifdef USE_WINDOWS_FILE
12 | #include
13 | #else
14 | #include
15 | #endif
16 |
17 | #include "lzma_Types.h"
18 |
19 | namespace crnlib {
20 |
21 |
22 | /* ---------- File ---------- */
23 |
24 | typedef struct
25 | {
26 | #ifdef USE_WINDOWS_FILE
27 | HANDLE handle;
28 | #else
29 | FILE *file;
30 | #endif
31 | } CSzFile;
32 |
33 | void File_Construct(CSzFile *p);
34 | WRes InFile_Open(CSzFile *p, const char *name);
35 | WRes OutFile_Open(CSzFile *p, const char *name);
36 | WRes File_Close(CSzFile *p);
37 |
38 | /* reads max(*size, remain file's size) bytes */
39 | WRes File_Read(CSzFile *p, void *data, size_t *size);
40 |
41 | /* writes *size bytes */
42 | WRes File_Write(CSzFile *p, const void *data, size_t *size);
43 |
44 | WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin);
45 | WRes File_GetLength(CSzFile *p, UInt64 *length);
46 |
47 |
48 | /* ---------- FileInStream ---------- */
49 |
50 | typedef struct
51 | {
52 | ISeqInStream s;
53 | CSzFile file;
54 | } CFileSeqInStream;
55 |
56 | void FileSeqInStream_CreateVTable(CFileSeqInStream *p);
57 |
58 |
59 | typedef struct
60 | {
61 | ISeekInStream s;
62 | CSzFile file;
63 | } CFileInStream;
64 |
65 | void FileInStream_CreateVTable(CFileInStream *p);
66 |
67 |
68 | typedef struct
69 | {
70 | ISeqOutStream s;
71 | CSzFile file;
72 | } CFileOutStream;
73 |
74 | void FileOutStream_CreateVTable(CFileOutStream *p);
75 |
76 | }
77 |
78 | #endif
79 |
--------------------------------------------------------------------------------
/crnlib/lzma_LzmaLib.cpp:
--------------------------------------------------------------------------------
1 | /* LzmaLib.c -- LZMA library wrapper
2 | 2008-08-05
3 | Igor Pavlov
4 | Public domain */
5 | #include "crn_core.h"
6 | #include "lzma_LzmaEnc.h"
7 | #include "lzma_LzmaDec.h"
8 | #include "lzma_Alloc.h"
9 | #include "lzma_LzmaLib.h"
10 |
11 | namespace crnlib {
12 |
13 | static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); }
14 | static void SzFree(void *p, void *address) { p = p; MyFree(address); }
15 | static ISzAlloc g_Alloc = { SzAlloc, SzFree };
16 |
17 | MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
18 | unsigned char *outProps, size_t *outPropsSize,
19 | int level, /* 0 <= level <= 9, default = 5 */
20 | unsigned dictSize, /* use (1 << N) or (3 << N). 4 KB < dictSize <= 128 MB */
21 | int lc, /* 0 <= lc <= 8, default = 3 */
22 | int lp, /* 0 <= lp <= 4, default = 0 */
23 | int pb, /* 0 <= pb <= 4, default = 2 */
24 | int fb, /* 5 <= fb <= 273, default = 32 */
25 | int numThreads /* 1 or 2, default = 2 */
26 | )
27 | {
28 | CLzmaEncProps props;
29 | LzmaEncProps_Init(&props);
30 | props.level = level;
31 | props.dictSize = dictSize;
32 | props.lc = lc;
33 | props.lp = lp;
34 | props.pb = pb;
35 | props.fb = fb;
36 | props.numThreads = numThreads;
37 |
38 | return LzmaEncode(dest, destLen, src, srcLen, &props, outProps, outPropsSize, 0,
39 | NULL, &g_Alloc, &g_Alloc);
40 | }
41 |
42 |
43 | MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen,
44 | const unsigned char *props, size_t propsSize)
45 | {
46 | ELzmaStatus status;
47 | return LzmaDecode(dest, destLen, src, srcLen, props, (unsigned)propsSize, LZMA_FINISH_ANY, &status, &g_Alloc);
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/crnlib/crn_assert.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_assert.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 | #if CRNLIB_USE_WIN32_API
5 | #include "crn_winhdr.h"
6 | #endif
7 |
8 | static bool g_fail_exceptions;
9 | static bool g_exit_on_failure = true;
10 |
11 | void crnlib_enable_fail_exceptions(bool enabled)
12 | {
13 | g_fail_exceptions = enabled;
14 | }
15 |
16 | void crnlib_assert(const char* pExp, const char* pFile, unsigned line)
17 | {
18 | char buf[512];
19 |
20 | sprintf_s(buf, sizeof(buf), "%s(%u): Assertion failed: \"%s\"\n", pFile, line, pExp);
21 |
22 | crnlib_output_debug_string(buf);
23 |
24 | fputs(buf, stderr);
25 |
26 | if (crnlib_is_debugger_present())
27 | crnlib_debug_break();
28 | }
29 |
30 | void crnlib_fail(const char* pExp, const char* pFile, unsigned line)
31 | {
32 | char buf[512];
33 |
34 | sprintf_s(buf, sizeof(buf), "%s(%u): Failure: \"%s\"\n", pFile, line, pExp);
35 |
36 | crnlib_output_debug_string(buf);
37 |
38 | fputs(buf, stderr);
39 |
40 | if (crnlib_is_debugger_present())
41 | crnlib_debug_break();
42 |
43 | #if CRNLIB_USE_WIN32_API
44 | if (g_fail_exceptions)
45 | RaiseException(CRNLIB_FAIL_EXCEPTION_CODE, 0, 0, NULL);
46 | else
47 | #endif
48 | if (g_exit_on_failure)
49 | exit(EXIT_FAILURE);
50 | }
51 |
52 | void trace(const char* pFmt, va_list args)
53 | {
54 | if (crnlib_is_debugger_present())
55 | {
56 | char buf[512];
57 | vsprintf_s(buf, sizeof(buf), pFmt, args);
58 |
59 | crnlib_output_debug_string(buf);
60 | }
61 | };
62 |
63 | void trace(const char* pFmt, ...)
64 | {
65 | va_list args;
66 | va_start(args, pFmt);
67 | trace(pFmt, args);
68 | va_end(args);
69 | };
70 |
--------------------------------------------------------------------------------
/crnlib/crn_dxt_endpoint_refiner.h:
--------------------------------------------------------------------------------
1 | // File: crn_dxt_endpoint_refiner.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_dxt.h"
5 |
6 | namespace crnlib
7 | {
8 | // TODO: Experimental/Not fully implemented
9 | class dxt_endpoint_refiner
10 | {
11 | public:
12 | dxt_endpoint_refiner();
13 |
14 | struct params
15 | {
16 | params() :
17 | m_block_index(0),
18 | m_pPixels(NULL),
19 | m_num_pixels(0),
20 | m_pSelectors(NULL),
21 | m_alpha_comp_index(0),
22 | m_error_to_beat(cUINT64_MAX),
23 | m_dxt1_selectors(true),
24 | m_perceptual(true),
25 | m_highest_quality(true)
26 | {
27 | }
28 |
29 | uint m_block_index;
30 |
31 | const color_quad_u8* m_pPixels;
32 | uint m_num_pixels;
33 |
34 | const uint8* m_pSelectors;
35 |
36 | uint m_alpha_comp_index;
37 |
38 | uint64 m_error_to_beat;
39 |
40 | bool m_dxt1_selectors;
41 | bool m_perceptual;
42 | bool m_highest_quality;
43 | };
44 |
45 | struct results
46 | {
47 | uint16 m_low_color;
48 | uint16 m_high_color;
49 | uint64 m_error;
50 | };
51 |
52 | bool refine(const params& p, results& r);
53 |
54 | private:
55 | const params* m_pParams;
56 | results* m_pResults;
57 |
58 | void optimize_dxt1(vec3F low_color, vec3F high_color);
59 | void optimize_dxt5(vec3F low_color, vec3F high_color);
60 | };
61 |
62 | } // namespace crnlib
63 |
--------------------------------------------------------------------------------
/crnlib/crn_dxt5a.h:
--------------------------------------------------------------------------------
1 | // File: crn_dxt5a.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_dxt.h"
5 |
6 | namespace crnlib
7 | {
8 | class dxt5_endpoint_optimizer
9 | {
10 | public:
11 | dxt5_endpoint_optimizer();
12 |
13 | struct params
14 | {
15 | params() :
16 | m_block_index(0),
17 | m_pPixels(NULL),
18 | m_num_pixels(0),
19 | m_comp_index(3),
20 | m_quality(cCRNDXTQualityUber),
21 | m_use_both_block_types(true)
22 | {
23 | }
24 |
25 | uint m_block_index;
26 |
27 | const color_quad_u8* m_pPixels;
28 | uint m_num_pixels;
29 | uint m_comp_index;
30 |
31 | crn_dxt_quality m_quality;
32 |
33 | bool m_use_both_block_types;
34 | };
35 |
36 | struct results
37 | {
38 | uint8* m_pSelectors;
39 |
40 | uint64 m_error;
41 |
42 | uint8 m_first_endpoint;
43 | uint8 m_second_endpoint;
44 |
45 | uint8 m_block_type; // 1 if 6-alpha, otherwise 8-alpha
46 | };
47 |
48 | bool compute(const params& p, results& r);
49 |
50 | private:
51 | const params* m_pParams;
52 | results* m_pResults;
53 |
54 | crnlib::vector m_unique_values;
55 | crnlib::vector m_unique_value_weights;
56 |
57 | crnlib::vector m_trial_selectors;
58 | crnlib::vector m_best_selectors;
59 | int m_unique_value_map[256];
60 |
61 | sparse_bit_array m_flags;
62 |
63 | void evaluate_solution(uint low_endpoint, uint high_endpoint);
64 | };
65 |
66 | } // namespace crnlib
67 |
--------------------------------------------------------------------------------
/crnlib/crn_texture_file_types.h:
--------------------------------------------------------------------------------
1 | // File: crn_texture_file_types.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "../inc/crnlib.h"
5 | #include "crn_vec.h"
6 | #include "crn_pixel_format.h"
7 |
8 | namespace crnlib
9 | {
10 | struct texture_file_types
11 | {
12 | enum format
13 | {
14 | cFormatInvalid = -1,
15 |
16 | cFormatDDS,
17 | cFormatCRN,
18 | cFormatKTX,
19 |
20 | cNumMipmappedFileFormats,
21 |
22 | cFormatTGA = cNumMipmappedFileFormats,
23 | cFormatPNG,
24 | cFormatJPG,
25 | cFormatJPEG,
26 | cFormatBMP,
27 | cFormatGIF,
28 | cFormatTIF,
29 | cFormatTIFF,
30 | cFormatPPM,
31 | cFormatPGM,
32 | cFormatPSD,
33 | cFormatJP2,
34 |
35 | cNumRegularFileFormats,
36 |
37 | cNumImageFileFormats = cNumRegularFileFormats - cNumMipmappedFileFormats,
38 |
39 | // Not really a file format
40 | cFormatClipboard = cNumRegularFileFormats,
41 | cFormatDragDrop,
42 |
43 | cNumFileFormats
44 | };
45 |
46 | static const char* get_extension(format fmt);
47 |
48 | static format determine_file_format(const char* pFilename);
49 |
50 | static bool supports_mipmaps(format fmt);
51 | static bool supports_alpha(format fmt);
52 | };
53 |
54 | enum texture_type
55 | {
56 | cTextureTypeUnknown = 0,
57 | cTextureTypeRegularMap,
58 | cTextureTypeNormalMap,
59 | cTextureTypeVerticalCrossCubemap,
60 | cTextureTypeCubemap,
61 |
62 | cNumTextureTypes
63 | };
64 |
65 | const char* get_texture_type_desc(texture_type t);
66 |
67 | } // namespace crnlib
68 |
69 |
--------------------------------------------------------------------------------
/crnlib/crn_checksum.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_checksum.cpp
2 | #include "crn_core.h"
3 |
4 | namespace crnlib
5 | {
6 | // From the public domain stb.h header.
7 | uint adler32(const void* pBuf, size_t buflen, uint adler32)
8 | {
9 | const uint8* buffer = static_cast(pBuf);
10 |
11 | const unsigned long ADLER_MOD = 65521;
12 | unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
13 | size_t blocklen;
14 | unsigned long i;
15 |
16 | blocklen = buflen % 5552;
17 | while (buflen) {
18 | for (i=0; i + 7 < blocklen; i += 8) {
19 | s1 += buffer[0], s2 += s1;
20 | s1 += buffer[1], s2 += s1;
21 | s1 += buffer[2], s2 += s1;
22 | s1 += buffer[3], s2 += s1;
23 | s1 += buffer[4], s2 += s1;
24 | s1 += buffer[5], s2 += s1;
25 | s1 += buffer[6], s2 += s1;
26 | s1 += buffer[7], s2 += s1;
27 |
28 | buffer += 8;
29 | }
30 |
31 | for (; i < blocklen; ++i)
32 | s1 += *buffer++, s2 += s1;
33 |
34 | s1 %= ADLER_MOD, s2 %= ADLER_MOD;
35 | buflen -= blocklen;
36 | blocklen = 5552;
37 | }
38 | return (s2 << 16) + s1;
39 | }
40 |
41 | uint16 crc16(const void* pBuf, size_t len, uint16 crc)
42 | {
43 | crc = ~crc;
44 |
45 | const uint8* p = reinterpret_cast(pBuf);
46 | while (len)
47 | {
48 | const uint16 q = *p++ ^ (crc >> 8);
49 | crc <<= 8U;
50 | uint16 r = (q >> 4) ^ q;
51 | crc ^= r;
52 | r <<= 5U;
53 | crc ^= r;
54 | r <<= 7U;
55 | crc ^= r;
56 | len--;
57 | }
58 |
59 | return static_cast(~crc);
60 | }
61 |
62 | } // namespace crnlib
63 |
64 |
--------------------------------------------------------------------------------
/crunch/crunch.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/crnlib/crn_find_files.h:
--------------------------------------------------------------------------------
1 | // File: crn_win32_find_files.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | class find_files
8 | {
9 | public:
10 | struct file_desc
11 | {
12 | inline file_desc() : m_is_dir(false) { }
13 |
14 | dynamic_string m_fullname;
15 | dynamic_string m_base;
16 | dynamic_string m_rel;
17 | dynamic_string m_name;
18 | bool m_is_dir;
19 |
20 | inline bool operator== (const file_desc& other) const { return m_fullname == other.m_fullname; }
21 | inline bool operator< (const file_desc& other) const { return m_fullname < other.m_fullname; }
22 |
23 | inline operator size_t() const { return static_cast(m_fullname); }
24 | };
25 |
26 | typedef crnlib::vector file_desc_vec;
27 |
28 | inline find_files()
29 | {
30 | m_last_error = 0; // S_OK;
31 | }
32 |
33 | enum flags
34 | {
35 | cFlagRecursive = 1,
36 | cFlagAllowDirs = 2,
37 | cFlagAllowFiles = 4,
38 | cFlagAllowHidden = 8
39 | };
40 |
41 | bool find(const char* pBasepath, const char* pFilespec, uint flags = cFlagAllowFiles);
42 |
43 | bool find(const char* pSpec, uint flags = cFlagAllowFiles);
44 |
45 | // An HRESULT under Win32. FIXME: Abstract this better?
46 | inline int64 get_last_error() const { return m_last_error; }
47 |
48 | const file_desc_vec& get_files() const { return m_files; }
49 |
50 | private:
51 | file_desc_vec m_files;
52 |
53 | // A HRESULT under Win32
54 | int64 m_last_error;
55 |
56 | bool find_internal(const char* pBasepath, const char* pRelpath, const char* pFilespec, uint flags, int level);
57 |
58 | }; // class find_files
59 |
60 | } // namespace crnlib
61 |
--------------------------------------------------------------------------------
/crnlib/crn_utils.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_utils.cpp
2 | #include "crn_core.h"
3 | #include "crn_utils.h"
4 |
5 | namespace crnlib
6 | {
7 | namespace utils
8 | {
9 | void endian_switch_words(uint16* p, uint num)
10 | {
11 | uint16* p_end = p + num;
12 | while (p != p_end)
13 | {
14 | uint16 k = *p;
15 | *p++ = swap16(k);
16 | }
17 | }
18 |
19 | void endian_switch_dwords(uint32* p, uint num)
20 | {
21 | uint32* p_end = p + num;
22 | while (p != p_end)
23 | {
24 | uint32 k = *p;
25 | *p++ = swap32(k);
26 | }
27 | }
28 |
29 | void copy_words(uint16* pDst, const uint16* pSrc, uint num, bool endian_switch)
30 | {
31 | if (!endian_switch)
32 | memcpy(pDst, pSrc, num << 1U);
33 | else
34 | {
35 | uint16* pDst_end = pDst + num;
36 | while (pDst != pDst_end)
37 | *pDst++ = swap16(*pSrc++);
38 | }
39 | }
40 |
41 | void copy_dwords(uint32* pDst, const uint32* pSrc, uint num, bool endian_switch)
42 | {
43 | if (!endian_switch)
44 | memcpy(pDst, pSrc, num << 2U);
45 | else
46 | {
47 | uint32* pDst_end = pDst + num;
48 | while (pDst != pDst_end)
49 | *pDst++ = swap32(*pSrc++);
50 | }
51 | }
52 |
53 | uint compute_max_mips(uint width, uint height)
54 | {
55 | if ((width | height) == 0)
56 | return 0;
57 |
58 | uint num_mips = 1;
59 |
60 | while ((width > 1U) || (height > 1U))
61 | {
62 | width >>= 1U;
63 | height >>= 1U;
64 | num_mips++;
65 | }
66 |
67 | return num_mips;
68 | }
69 |
70 | } // namespace utils
71 |
72 | } // namespace crnlib
73 |
--------------------------------------------------------------------------------
/crnlib/crn_platform.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_platform.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 |
5 | #if CRNLIB_USE_WIN32_API
6 | #include "crn_winhdr.h"
7 | #endif
8 | #ifndef _MSC_VER
9 | int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...)
10 | {
11 | if (!sizeOfBuffer)
12 | return 0;
13 |
14 | va_list args;
15 | va_start(args, format);
16 | int c = vsnprintf(buffer, sizeOfBuffer, format, args);
17 | va_end(args);
18 |
19 | buffer[sizeOfBuffer - 1] = '\0';
20 |
21 | if (c < 0)
22 | return sizeOfBuffer - 1;
23 |
24 | return CRNLIB_MIN(c, (int)sizeOfBuffer - 1);
25 | }
26 |
27 | int vsprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, va_list args)
28 | {
29 | if (!sizeOfBuffer)
30 | return 0;
31 |
32 | int c = vsnprintf(buffer, sizeOfBuffer, format, args);
33 |
34 | buffer[sizeOfBuffer - 1] = '\0';
35 |
36 | if (c < 0)
37 | return sizeOfBuffer - 1;
38 |
39 | return CRNLIB_MIN(c, (int)sizeOfBuffer - 1);
40 | }
41 |
42 | char* strlwr(char* p)
43 | {
44 | char *q = p;
45 | while (*q)
46 | {
47 | char c = *q;
48 | *q++ = tolower(c);
49 | }
50 | return p;
51 | }
52 |
53 | char* strupr(char *p)
54 | {
55 | char *q = p;
56 | while (*q)
57 | {
58 | char c = *q;
59 | *q++ = toupper(c);
60 | }
61 | return p;
62 | }
63 | #endif // __GNUC__
64 |
65 | void crnlib_debug_break(void)
66 | {
67 | CRNLIB_BREAKPOINT
68 | }
69 |
70 | #if CRNLIB_USE_WIN32_API
71 | #include "crn_winhdr.h"
72 |
73 | bool crnlib_is_debugger_present(void)
74 | {
75 | return IsDebuggerPresent() != 0;
76 | }
77 |
78 | void crnlib_output_debug_string(const char* p)
79 | {
80 | OutputDebugStringA(p);
81 | }
82 | #else
83 | bool crnlib_is_debugger_present(void)
84 | {
85 | return false;
86 | }
87 |
88 | void crnlib_output_debug_string(const char* p)
89 | {
90 | puts(p);
91 | }
92 | #endif // CRNLIB_USE_WIN32_API
93 |
--------------------------------------------------------------------------------
/crnlib/crn_file_utils.h:
--------------------------------------------------------------------------------
1 | // File: crn_file_utils.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | struct file_utils
8 | {
9 | // Returns true if pSrcFilename is older than pDstFilename
10 | static bool is_read_only(const char* pFilename);
11 | static bool disable_read_only(const char* pFilename);
12 | static bool is_older_than(const char *pSrcFilename, const char* pDstFilename);
13 | static bool does_file_exist(const char* pFilename);
14 | static bool does_dir_exist(const char* pDir);
15 | static bool get_file_size(const char* pFilename, uint64& file_size);
16 | static bool get_file_size(const char* pFilename, uint32& file_size);
17 |
18 | static bool is_path_separator(char c);
19 | static bool is_path_or_drive_separator(char c);
20 | static bool is_drive_separator(char c);
21 |
22 | static bool split_path(const char* p, dynamic_string* pDrive, dynamic_string* pDir, dynamic_string* pFilename, dynamic_string* pExt);
23 | static bool split_path(const char* p, dynamic_string& path, dynamic_string& filename);
24 |
25 | static bool get_pathname(const char* p, dynamic_string& path);
26 | static bool get_filename(const char* p, dynamic_string& filename);
27 |
28 | static void combine_path(dynamic_string& dst, const char* pA, const char* pB);
29 | static void combine_path(dynamic_string& dst, const char* pA, const char* pB, const char* pC);
30 |
31 | static bool full_path(dynamic_string& path);
32 | static bool get_extension(dynamic_string& filename);
33 | static bool remove_extension(dynamic_string& filename);
34 | static bool create_path(const dynamic_string& path);
35 | static void trim_trailing_seperator(dynamic_string& path);
36 |
37 | static int wildcmp(const char* pWild, const char* pString);
38 |
39 | static bool write_buf_to_file(const char* pPath, const void* pData, size_t data_size);
40 |
41 | }; // struct file_utils
42 |
43 | } // namespace crnlib
44 |
--------------------------------------------------------------------------------
/crnlib/crn_helpers.h:
--------------------------------------------------------------------------------
1 | // File: crn_helpers.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | #define CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(c) c(const c&); c& operator= (const c&);
6 | #define CRNLIB_NO_HEAP_ALLOC() private: static void* operator new(size_t); static void* operator new[](size_t);
7 |
8 | namespace crnlib
9 | {
10 | namespace helpers
11 | {
12 | template struct rel_ops
13 | {
14 | friend bool operator!=(const T& x, const T& y) { return (!(x == y)); }
15 | friend bool operator> (const T& x, const T& y) { return (y < x); }
16 | friend bool operator<=(const T& x, const T& y) { return (!(y < x)); }
17 | friend bool operator>=(const T& x, const T& y) { return (!(x < y)); }
18 | };
19 |
20 | template
21 | inline T* construct(T* p)
22 | {
23 | return new (static_cast(p)) T;
24 | }
25 |
26 | template
27 | inline T* construct(T* p, const U& init)
28 | {
29 | return new (static_cast(p)) T(init);
30 | }
31 |
32 | template
33 | inline void construct_array(T* p, uint n)
34 | {
35 | T* q = p + n;
36 | for ( ; p != q; ++p)
37 | new (static_cast(p)) T;
38 | }
39 |
40 | template
41 | inline void construct_array(T* p, uint n, const U& init)
42 | {
43 | T* q = p + n;
44 | for ( ; p != q; ++p)
45 | new (static_cast(p)) T(init);
46 | }
47 |
48 | template
49 | inline void destruct(T* p)
50 | {
51 | p;
52 | p->~T();
53 | }
54 |
55 | template inline void destruct_array(T* p, uint n)
56 | {
57 | T* q = p + n;
58 | for ( ; p != q; ++p)
59 | p->~T();
60 | }
61 |
62 | } // namespace helpers
63 |
64 | } // namespace crnlib
65 |
--------------------------------------------------------------------------------
/crnlib/lzma_BraIA64.cpp:
--------------------------------------------------------------------------------
1 | /* BraIA64.c -- Converter for IA-64 code
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 | #include "crn_core.h"
4 | #include "lzma_Bra.h"
5 |
6 | namespace crnlib {
7 |
8 | static const Byte kBranchTable[32] =
9 | {
10 | 0, 0, 0, 0, 0, 0, 0, 0,
11 | 0, 0, 0, 0, 0, 0, 0, 0,
12 | 4, 4, 6, 6, 0, 0, 7, 7,
13 | 4, 4, 0, 0, 4, 4, 0, 0
14 | };
15 |
16 | SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
17 | {
18 | SizeT i;
19 | if (size < 16)
20 | return 0;
21 | size -= 16;
22 | for (i = 0; i <= size; i += 16)
23 | {
24 | UInt32 instrTemplate = data[i] & 0x1F;
25 | UInt32 mask = kBranchTable[instrTemplate];
26 | UInt32 bitPos = 5;
27 | int slot;
28 | for (slot = 0; slot < 3; slot++, bitPos += 41)
29 | {
30 | UInt32 bytePos, bitRes;
31 | UInt64 instruction, instNorm;
32 | int j;
33 | if (((mask >> slot) & 1) == 0)
34 | continue;
35 | bytePos = (bitPos >> 3);
36 | bitRes = bitPos & 0x7;
37 | instruction = 0;
38 | for (j = 0; j < 6; j++)
39 | instruction += (UInt64)data[i + j + bytePos] << (8 * j);
40 |
41 | instNorm = instruction >> bitRes;
42 | if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0)
43 | {
44 | UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF);
45 | UInt32 dest;
46 | src |= ((UInt32)(instNorm >> 36) & 1) << 20;
47 |
48 | src <<= 4;
49 |
50 | if (encoding)
51 | dest = ip + (UInt32)i + src;
52 | else
53 | dest = src - (ip + (UInt32)i);
54 |
55 | dest >>= 4;
56 |
57 | instNorm &= ~((UInt64)(0x8FFFFF) << 13);
58 | instNorm |= ((UInt64)(dest & 0xFFFFF) << 13);
59 | instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20));
60 |
61 | instruction &= (1 << bitRes) - 1;
62 | instruction |= (instNorm << bitRes);
63 | for (j = 0; j < 6; j++)
64 | data[i + j + bytePos] = (Byte)(instruction >> (8 * j));
65 | }
66 | }
67 | }
68 | return i;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/crnlib/lzma_Bra.h:
--------------------------------------------------------------------------------
1 | /* Bra.h -- Branch converters for executables
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 |
4 | #ifndef __BRA_H
5 | #define __BRA_H
6 |
7 | #include "lzma_Types.h"
8 |
9 | namespace crnlib {
10 |
11 | /*
12 | These functions convert relative addresses to absolute addresses
13 | in CALL instructions to increase the compression ratio.
14 |
15 | In:
16 | data - data buffer
17 | size - size of data
18 | ip - current virtual Instruction Pinter (IP) value
19 | state - state variable for x86 converter
20 | encoding - 0 (for decoding), 1 (for encoding)
21 |
22 | Out:
23 | state - state variable for x86 converter
24 |
25 | Returns:
26 | The number of processed bytes. If you call these functions with multiple calls,
27 | you must start next call with first byte after block of processed bytes.
28 |
29 | Type Endian Alignment LookAhead
30 |
31 | x86 little 1 4
32 | ARMT little 2 2
33 | ARM little 4 0
34 | PPC big 4 0
35 | SPARC big 4 0
36 | IA64 little 16 0
37 |
38 | size must be >= Alignment + LookAhead, if it's not last block.
39 | If (size < Alignment + LookAhead), converter returns 0.
40 |
41 | Example:
42 |
43 | UInt32 ip = 0;
44 | for ()
45 | {
46 | ; size must be >= Alignment + LookAhead, if it's not last block
47 | SizeT processed = Convert(data, size, ip, 1);
48 | data += processed;
49 | size -= processed;
50 | ip += processed;
51 | }
52 | */
53 |
54 | #define x86_Convert_Init(state) { state = 0; }
55 | SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding);
56 | SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
57 | SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
58 | SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
59 | SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
60 | SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
61 |
62 | }
63 |
64 | #endif
65 |
--------------------------------------------------------------------------------
/crnlib/crn_hash.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_hash.cpp
2 | // See Paul Hsieh's page at: http://www.azillionmonkeys.com/qed/hash.html
3 | // Also see http://www.concentric.net/~Ttwang/tech/inthash.htm,
4 | // http://burtleburtle.net/bob/hash/integer.html
5 | #include "crn_core.h"
6 |
7 | #undef get16bits
8 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
9 | || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
10 | #define get16bits(d) (*((const uint16 *) (d)))
11 | #endif
12 |
13 | #if !defined (get16bits)
14 | #define get16bits(d) ((((uint32)(((const uint8 *)(d))[1])) << 8)\
15 | +(uint32)(((const uint8 *)(d))[0]) )
16 | #endif
17 |
18 | namespace crnlib
19 | {
20 | uint32 fast_hash (const void* p, int len)
21 | {
22 | const char * data = static_cast(p);
23 |
24 | uint32 hash = len, tmp;
25 | int rem;
26 |
27 | if (len <= 0 || data == NULL) return 0;
28 |
29 | rem = len & 3;
30 | len >>= 2;
31 |
32 | /* Main loop */
33 | for (;len > 0; len--) {
34 | hash += get16bits (data);
35 | tmp = (get16bits (data+2) << 11) ^ hash;
36 | hash = (hash << 16) ^ tmp;
37 | data += 2*sizeof (uint16);
38 | hash += hash >> 11;
39 | }
40 |
41 | /* Handle end cases */
42 | switch (rem) {
43 | case 3: hash += get16bits (data);
44 | hash ^= hash << 16;
45 | hash ^= data[sizeof (uint16)] << 18;
46 | hash += hash >> 11;
47 | break;
48 | case 2: hash += get16bits (data);
49 | hash ^= hash << 11;
50 | hash += hash >> 17;
51 | break;
52 | case 1: hash += *data;
53 | hash ^= hash << 10;
54 | hash += hash >> 1;
55 | }
56 |
57 | /* Force "avalanching" of final 127 bits */
58 | hash ^= hash << 3;
59 | hash += hash >> 5;
60 | hash ^= hash << 4;
61 | hash += hash >> 17;
62 | hash ^= hash << 25;
63 | hash += hash >> 6;
64 |
65 | return hash;
66 | }
67 |
68 | } // namespace crnlib
69 |
--------------------------------------------------------------------------------
/crnlib/crn_timer.h:
--------------------------------------------------------------------------------
1 | // File: crn_win32_timer.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | typedef unsigned long long timer_ticks;
8 |
9 | class timer
10 | {
11 | public:
12 | timer();
13 | timer(timer_ticks start_ticks);
14 |
15 | void start();
16 | void start(timer_ticks start_ticks);
17 |
18 | void stop();
19 |
20 | double get_elapsed_secs() const;
21 | inline double get_elapsed_ms() const { return get_elapsed_secs() * 1000.0f; }
22 | timer_ticks get_elapsed_us() const;
23 |
24 | static void init();
25 | static inline timer_ticks get_ticks_per_sec() { return g_freq; }
26 | static timer_ticks get_init_ticks();
27 | static timer_ticks get_ticks();
28 | static double ticks_to_secs(timer_ticks ticks);
29 | static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; }
30 | static inline double get_secs() { return ticks_to_secs(get_ticks()); }
31 | static inline double get_ms() { return ticks_to_ms(get_ticks()); }
32 |
33 | private:
34 | static timer_ticks g_init_ticks;
35 | static timer_ticks g_freq;
36 | static double g_inv_freq;
37 |
38 | timer_ticks m_start_time;
39 | timer_ticks m_stop_time;
40 |
41 | bool m_started : 1;
42 | bool m_stopped : 1;
43 | };
44 |
45 | // Prints object's lifetime to stdout
46 | class timed_scope
47 | {
48 | const char* m_pName;
49 | timer m_tm;
50 |
51 | public:
52 | inline timed_scope(char* pName = "timed_scope") : m_pName(pName) { m_tm.start(); }
53 |
54 | inline double get_elapsed_secs() const { return m_tm.get_elapsed_secs(); }
55 | inline double get_elapsed_ms() const { return m_tm.get_elapsed_ms(); }
56 |
57 | const timer &get_timer() const { return m_tm; }
58 | timer &get_timer() { return m_tm; }
59 |
60 | inline ~timed_scope() { double secs = m_tm.get_elapsed_secs(); printf("%s: %f secs, %f ms\n", m_pName, secs, secs * 1000.0f); }
61 | };
62 |
63 | } // namespace crnlib
64 |
--------------------------------------------------------------------------------
/crnlib/crn_lzma_codec.h:
--------------------------------------------------------------------------------
1 | // File: crn_lzma_codec.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_packed_uint.h"
5 |
6 | namespace crnlib
7 | {
8 | class lzma_codec
9 | {
10 | public:
11 | lzma_codec();
12 | ~lzma_codec();
13 |
14 | // Always available, because we're statically linking in lzmalib now vs. dynamically loading the DLL.
15 | bool is_initialized() const { return true; }
16 |
17 | bool pack(const void* p, uint n, crnlib::vector& buf);
18 |
19 | bool unpack(const void* p, uint n, crnlib::vector& buf);
20 |
21 | private:
22 | typedef int (CRNLIB_STDCALL *LzmaCompressFuncPtr)(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
23 | unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */
24 | int level, /* 0 <= level <= 9, default = 5 */
25 | unsigned dictSize, /* default = (1 << 24) */
26 | int lc, /* 0 <= lc <= 8, default = 3 */
27 | int lp, /* 0 <= lp <= 4, default = 0 */
28 | int pb, /* 0 <= pb <= 4, default = 2 */
29 | int fb, /* 5 <= fb <= 273, default = 32 */
30 | int numThreads /* 1 or 2, default = 2 */
31 | );
32 |
33 | typedef int (CRNLIB_STDCALL *LzmaUncompressFuncPtr)(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen,
34 | const unsigned char *props, size_t propsSize);
35 |
36 | LzmaCompressFuncPtr m_pCompress;
37 | LzmaUncompressFuncPtr m_pUncompress;
38 |
39 | enum { cLZMAPropsSize = 5 };
40 |
41 | #pragma pack(push)
42 | #pragma pack(1)
43 | struct header
44 | {
45 | enum { cSig = 'L' | ('0' << 8), cChecksumSkipBytes = 3 };
46 | packed_uint<2> m_sig;
47 | uint8 m_checksum;
48 |
49 | uint8 m_lzma_props[cLZMAPropsSize];
50 |
51 | packed_uint<4> m_comp_size;
52 | packed_uint<4> m_uncomp_size;
53 |
54 | packed_uint<4> m_adler32;
55 | };
56 | #pragma pack(pop)
57 |
58 | };
59 |
60 | } // namespace crnlib
61 |
--------------------------------------------------------------------------------
/crnlib/lzma_LzHash.h:
--------------------------------------------------------------------------------
1 | /* LzHash.h -- HASH functions for LZ algorithms
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 |
4 | #ifndef __LZHASH_H
5 | #define __LZHASH_H
6 |
7 | #define kHash2Size (1 << 10)
8 | #define kHash3Size (1 << 16)
9 | #define kHash4Size (1 << 20)
10 |
11 | #define kFix3HashSize (kHash2Size)
12 | #define kFix4HashSize (kHash2Size + kHash3Size)
13 | #define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
14 |
15 | #define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
16 |
17 | #define HASH3_CALC { \
18 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
19 | hash2Value = temp & (kHash2Size - 1); \
20 | hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; }
21 |
22 | #define HASH4_CALC { \
23 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
24 | hash2Value = temp & (kHash2Size - 1); \
25 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
26 | hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; }
27 |
28 | #define HASH5_CALC { \
29 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
30 | hash2Value = temp & (kHash2Size - 1); \
31 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
32 | hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \
33 | hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \
34 | hash4Value &= (kHash4Size - 1); }
35 |
36 | /* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */
37 | #define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF;
38 |
39 |
40 | #define MT_HASH2_CALC \
41 | hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
42 |
43 | #define MT_HASH3_CALC { \
44 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
45 | hash2Value = temp & (kHash2Size - 1); \
46 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
47 |
48 | #define MT_HASH4_CALC { \
49 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
50 | hash2Value = temp & (kHash2Size - 1); \
51 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
52 | hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); }
53 |
54 | #endif
55 |
--------------------------------------------------------------------------------
/crnlib/crn_assert.h:
--------------------------------------------------------------------------------
1 | // File: crn_assert.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | const unsigned int CRNLIB_FAIL_EXCEPTION_CODE = 256U;
6 | void crnlib_enable_fail_exceptions(bool enabled);
7 |
8 | void crnlib_assert(const char* pExp, const char* pFile, unsigned line);
9 | void crnlib_fail(const char* pExp, const char* pFile, unsigned line);
10 |
11 | #ifdef NDEBUG
12 | #define CRNLIB_ASSERT(x) ((void)0)
13 | #undef CRNLIB_ASSERTS_ENABLED
14 | #else
15 | #define CRNLIB_ASSERT(_exp) (void)( (!!(_exp)) || (crnlib_assert(#_exp, __FILE__, __LINE__), 0) )
16 | #define CRNLIB_ASSERTS_ENABLED
17 | #endif
18 |
19 | #define CRNLIB_VERIFY(_exp) (void)( (!!(_exp)) || (crnlib_assert(#_exp, __FILE__, __LINE__), 0) )
20 |
21 | #define CRNLIB_FAIL(msg) do { crnlib_fail(#msg, __FILE__, __LINE__); } while(0)
22 |
23 | #define CRNLIB_ASSERT_OPEN_RANGE(x, l, h) CRNLIB_ASSERT((x >= l) && (x < h))
24 | #define CRNLIB_ASSERT_CLOSED_RANGE(x, l, h) CRNLIB_ASSERT((x >= l) && (x <= h))
25 |
26 | void trace(const char* pFmt, va_list args);
27 | void trace(const char* pFmt, ...);
28 |
29 | // Borrowed from boost libraries.
30 | template struct crnlib_assume_failure;
31 | template <> struct crnlib_assume_failure { enum { blah = 1 }; };
32 | template struct crnlib_assume_try { };
33 |
34 | #define CRNLIB_JOINER_FINAL(a, b) a##b
35 | #define CRNLIB_JOINER(a, b) CRNLIB_JOINER_FINAL(a, b)
36 | #define CRNLIB_JOIN(a, b) CRNLIB_JOINER(a, b)
37 | #define CRNLIB_ASSUME(p) typedef crnlib_assume_try < sizeof(crnlib_assume_failure< (bool)(p) > ) > CRNLIB_JOIN(crnlib_assume_typedef, __COUNTER__)
38 |
39 | #ifdef NDEBUG
40 | template inline T crnlib_assert_range(T i, T m)
41 | {
42 | m;
43 | return i;
44 | }
45 | template inline T crnlib_assert_range_incl(T i, T m)
46 | {
47 | m;
48 | return i;
49 | }
50 | #else
51 | template inline T crnlib_assert_range(T i, T m)
52 | {
53 | CRNLIB_ASSERT((i >= 0) && (i < m));
54 | return i;
55 | }
56 | template inline T crnlib_assert_range_incl(T i, T m)
57 | {
58 | CRNLIB_ASSERT((i >= 0) && (i <= m));
59 | return i;
60 | }
61 | #endif
62 |
--------------------------------------------------------------------------------
/crnlib/crn_types.h:
--------------------------------------------------------------------------------
1 | // File: crn_types.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | typedef unsigned char uint8;
8 | typedef signed char int8;
9 | typedef unsigned short uint16;
10 | typedef signed short int16;
11 | typedef unsigned int uint32;
12 | typedef uint32 uint;
13 | typedef signed int int32;
14 |
15 | #ifdef __APPLE__
16 | typedef unsigned long uint64;
17 | typedef long int64;
18 | #elif defined __GNUC__
19 | typedef unsigned long long uint64;
20 | typedef long long int64;
21 | #else
22 | typedef unsigned __int64 uint64;
23 | typedef signed __int64 int64;
24 | #endif
25 |
26 | const uint8 cUINT8_MIN = 0;
27 | const uint8 cUINT8_MAX = 0xFFU;
28 | const uint16 cUINT16_MIN = 0;
29 | const uint16 cUINT16_MAX = 0xFFFFU;
30 | const uint32 cUINT32_MIN = 0;
31 | const uint32 cUINT32_MAX = 0xFFFFFFFFU;
32 | const uint64 cUINT64_MIN = 0;
33 | const uint64 cUINT64_MAX = 0xFFFFFFFFFFFFFFFFULL; //0xFFFFFFFFFFFFFFFFui64;
34 |
35 | const int8 cINT8_MIN = -128;
36 | const int8 cINT8_MAX = 127;
37 | const int16 cINT16_MIN = -32768;
38 | const int16 cINT16_MAX = 32767;
39 | const int32 cINT32_MIN = (-2147483647 - 1);
40 | const int32 cINT32_MAX = 2147483647;
41 | const int64 cINT64_MIN = (int64)0x8000000000000000ULL; //(-9223372036854775807i64 - 1);
42 | const int64 cINT64_MAX = (int64)0x7FFFFFFFFFFFFFFFULL; // 9223372036854775807i64;
43 |
44 | #if CRNLIB_64BIT_POINTERS
45 | typedef uint64 uint_ptr;
46 | typedef uint64 uint32_ptr;
47 | typedef int64 signed_size_t;
48 | typedef uint64 ptr_bits_t;
49 | #else
50 | typedef unsigned int uint_ptr;
51 | typedef unsigned int uint32_ptr;
52 | typedef signed int signed_size_t;
53 | typedef uint32 ptr_bits_t;
54 | #endif
55 |
56 | enum eVarArg { cVarArg };
57 | enum eClear { cClear };
58 | enum eNoClamp { cNoClamp };
59 | enum { cInvalidIndex = -1 };
60 |
61 | const uint cIntBits = 32;
62 |
63 | struct empty_type { };
64 |
65 | } // namespace crnlib
66 |
--------------------------------------------------------------------------------
/crnlib/lzma_CpuArch.h:
--------------------------------------------------------------------------------
1 | /* CpuArch.h
2 | 2008-08-05
3 | Igor Pavlov
4 | Public domain */
5 |
6 | #ifndef __CPUARCH_H
7 | #define __CPUARCH_H
8 |
9 | /*
10 | LITTLE_ENDIAN_UNALIGN means:
11 | 1) CPU is LITTLE_ENDIAN
12 | 2) it's allowed to make unaligned memory accesses
13 | if LITTLE_ENDIAN_UNALIGN is not defined, it means that we don't know
14 | about these properties of platform.
15 | */
16 |
17 | #if defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(__x86_64__)
18 | #define LITTLE_ENDIAN_UNALIGN
19 | #endif
20 |
21 | #ifdef LITTLE_ENDIAN_UNALIGN
22 |
23 | #define GetUi16(p) (*(const UInt16 *)(p))
24 | #define GetUi32(p) (*(const UInt32 *)(p))
25 | #define GetUi64(p) (*(const UInt64 *)(p))
26 | #define SetUi32(p, d) *(UInt32 *)(p) = (d);
27 |
28 | #else
29 |
30 | #define GetUi16(p) (((const Byte *)(p))[0] | ((UInt16)((const Byte *)(p))[1] << 8))
31 |
32 | #define GetUi32(p) ( \
33 | ((const Byte *)(p))[0] | \
34 | ((UInt32)((const Byte *)(p))[1] << 8) | \
35 | ((UInt32)((const Byte *)(p))[2] << 16) | \
36 | ((UInt32)((const Byte *)(p))[3] << 24))
37 |
38 | #define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32))
39 |
40 | #define SetUi32(p, d) { UInt32 _x_ = (d); \
41 | ((Byte *)(p))[0] = (Byte)_x_; \
42 | ((Byte *)(p))[1] = (Byte)(_x_ >> 8); \
43 | ((Byte *)(p))[2] = (Byte)(_x_ >> 16); \
44 | ((Byte *)(p))[3] = (Byte)(_x_ >> 24); }
45 |
46 | #endif
47 |
48 | #if defined(LITTLE_ENDIAN_UNALIGN) && defined(_WIN64) && (_MSC_VER >= 1300)
49 |
50 | #pragma intrinsic(_byteswap_ulong)
51 | #pragma intrinsic(_byteswap_uint64)
52 | #define GetBe32(p) _byteswap_ulong(*(const UInt32 *)(const Byte *)(p))
53 | #define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const Byte *)(p))
54 |
55 | #else
56 |
57 | #define GetBe32(p) ( \
58 | ((UInt32)((const Byte *)(p))[0] << 24) | \
59 | ((UInt32)((const Byte *)(p))[1] << 16) | \
60 | ((UInt32)((const Byte *)(p))[2] << 8) | \
61 | ((const Byte *)(p))[3] )
62 |
63 | #define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4))
64 |
65 | #endif
66 |
67 | #define GetBe16(p) (((UInt16)((const Byte *)(p))[0] << 8) | ((const Byte *)(p))[1])
68 |
69 | #endif
70 |
--------------------------------------------------------------------------------
/crnlib/lzma_Threads.h:
--------------------------------------------------------------------------------
1 | /* Threads.h -- multithreading library
2 | 2008-11-22 : Igor Pavlov : Public domain */
3 |
4 | #ifndef __7Z_THRESDS_H
5 | #define __7Z_THRESDS_H
6 |
7 | #include "lzma_Types.h"
8 |
9 | namespace crnlib {
10 |
11 | typedef struct _CThread
12 | {
13 | HANDLE handle;
14 | } CThread;
15 |
16 | #define Thread_Construct(thread) (thread)->handle = NULL
17 | #define Thread_WasCreated(thread) ((thread)->handle != NULL)
18 |
19 | typedef unsigned THREAD_FUNC_RET_TYPE;
20 | #define THREAD_FUNC_CALL_TYPE MY_STD_CALL
21 | #define THREAD_FUNC_DECL THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE
22 |
23 | WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), LPVOID parameter);
24 | WRes Thread_Wait(CThread *thread);
25 | WRes Thread_Close(CThread *thread);
26 |
27 | typedef struct _CEvent
28 | {
29 | HANDLE handle;
30 | } CEvent;
31 |
32 | typedef CEvent CAutoResetEvent;
33 | typedef CEvent CManualResetEvent;
34 |
35 | #define Event_Construct(event) (event)->handle = NULL
36 | #define Event_IsCreated(event) ((event)->handle != NULL)
37 |
38 | WRes ManualResetEvent_Create(CManualResetEvent *event, int initialSignaled);
39 | WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *event);
40 | WRes AutoResetEvent_Create(CAutoResetEvent *event, int initialSignaled);
41 | WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *event);
42 | WRes Event_Set(CEvent *event);
43 | WRes Event_Reset(CEvent *event);
44 | WRes Event_Wait(CEvent *event);
45 | WRes Event_Close(CEvent *event);
46 |
47 |
48 | typedef struct _CSemaphore
49 | {
50 | HANDLE handle;
51 | } CSemaphore;
52 |
53 | #define Semaphore_Construct(p) (p)->handle = NULL
54 |
55 | WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount);
56 | WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num);
57 | WRes Semaphore_Release1(CSemaphore *p);
58 | WRes Semaphore_Wait(CSemaphore *p);
59 | WRes Semaphore_Close(CSemaphore *p);
60 |
61 |
62 | typedef CRITICAL_SECTION CCriticalSection;
63 |
64 | WRes CriticalSection_Init(CCriticalSection *p);
65 | #define CriticalSection_Delete(p) DeleteCriticalSection(p)
66 | #define CriticalSection_Enter(p) EnterCriticalSection(p)
67 | #define CriticalSection_Leave(p) LeaveCriticalSection(p)
68 |
69 | }
70 |
71 | #endif
72 |
73 |
--------------------------------------------------------------------------------
/crnlib/crn_arealist.h:
--------------------------------------------------------------------------------
1 | // File: crn_arealist.h - 2D shape algebra
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | struct Area
8 | {
9 | struct Area *Pprev, *Pnext;
10 |
11 | int x1, y1, x2, y2;
12 |
13 | uint get_width() const { return x2 - x1 + 1; }
14 | uint get_height() const { return y2 - y1 + 1; }
15 | uint get_area() const { return get_width() * get_height(); }
16 | };
17 |
18 | typedef Area * Area_Ptr;
19 |
20 | struct Area_List
21 | {
22 | int total_areas;
23 | int next_free;
24 |
25 | Area *Phead, *Ptail, *Pfree;
26 | };
27 |
28 | typedef Area_List * Area_List_Ptr;
29 |
30 | Area_List * Area_List_init(int max_areas);
31 | void Area_List_deinit(Area_List* Pobj_base);
32 |
33 | void Area_List_print(Area_List *Plist);
34 |
35 | Area_List * Area_List_dup_new(Area_List *Plist,
36 | int x_ofs, int y_ofs);
37 |
38 | uint Area_List_get_num(Area_List* Plist);
39 |
40 | // src and dst area lists must have the same number of total areas.
41 | void Area_List_dup(Area_List *Psrc_list,
42 | Area_List *Pdst_list,
43 | int x_ofs, int y_ofs);
44 |
45 | void Area_List_copy(Area_List *Psrc_list,
46 | Area_List *Pdst_list,
47 | int x_ofs, int y_ofs);
48 |
49 | void Area_List_clear(Area_List *Plist);
50 |
51 | void Area_List_set(Area_List *Plist,
52 | int x1, int y1, int x2, int y2);
53 |
54 | // logical: x and (not y)
55 | void Area_List_remove(Area_List *Plist,
56 | int x1, int y1, int x2, int y2);
57 |
58 | // logical: x or y
59 | void Area_List_insert(Area_List *Plist,
60 | int x1, int y1, int x2, int y2,
61 | bool combine);
62 |
63 | // logical: x and y
64 | void Area_List_intersect_area(Area_List *Plist,
65 | int x1, int y1, int x2, int y2);
66 |
67 | // logical: x and y
68 | void Area_List_intersect_Area_List(Area_List *Pouter_list,
69 | Area_List *Pinner_list,
70 | Area_List *Pdst_list);
71 |
72 | Area_List_Ptr Area_List_create_optimal(Area_List_Ptr Plist);
73 |
74 | } // namespace crnlib
75 |
--------------------------------------------------------------------------------
/crnlib/crn_math.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_math.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 |
5 | namespace crnlib
6 | {
7 | namespace math
8 | {
9 | uint g_bitmasks[32] =
10 | {
11 | 1U << 0U, 1U << 1U, 1U << 2U, 1U << 3U,
12 | 1U << 4U, 1U << 5U, 1U << 6U, 1U << 7U,
13 | 1U << 8U, 1U << 9U, 1U << 10U, 1U << 11U,
14 | 1U << 12U, 1U << 13U, 1U << 14U, 1U << 15U,
15 | 1U << 16U, 1U << 17U, 1U << 18U, 1U << 19U,
16 | 1U << 20U, 1U << 21U, 1U << 22U, 1U << 23U,
17 | 1U << 24U, 1U << 25U, 1U << 26U, 1U << 27U,
18 | 1U << 28U, 1U << 29U, 1U << 30U, 1U << 31U
19 | };
20 |
21 | double compute_entropy(const uint8* p, uint n)
22 | {
23 | uint hist[256];
24 | utils::zero_object(hist);
25 |
26 | for (uint i = 0; i < n; i++)
27 | hist[*p++]++;
28 |
29 | double entropy = 0.0f;
30 |
31 | const double invln2 = 1.0f/log(2.0f);
32 | for (uint i = 0; i < 256; i++)
33 | {
34 | if (!hist[i])
35 | continue;
36 |
37 | double prob = static_cast(hist[i]) / n;
38 | entropy += (-log(prob) * invln2) * hist[i];
39 | }
40 |
41 | return entropy;
42 | }
43 |
44 | void compute_lower_pow2_dim(int& width, int& height)
45 | {
46 | const int tex_width = width;
47 | const int tex_height = height;
48 |
49 | width = 1;
50 | for ( ; ; )
51 | {
52 | if ((width * 2) > tex_width)
53 | break;
54 | width *= 2;
55 | }
56 |
57 | height = 1;
58 | for ( ; ; )
59 | {
60 | if ((height * 2) > tex_height)
61 | break;
62 | height *= 2;
63 | }
64 | }
65 |
66 | void compute_upper_pow2_dim(int& width, int& height)
67 | {
68 | if (!math::is_power_of_2((uint32)width))
69 | width = math::next_pow2((uint32)width);
70 |
71 | if (!math::is_power_of_2((uint32)height))
72 | height = math::next_pow2((uint32)height);
73 | }
74 |
75 | } // namespace math
76 | } // namespace crnlib
77 |
--------------------------------------------------------------------------------
/crnlib/crn_threaded_resampler.h:
--------------------------------------------------------------------------------
1 | // File: crn_threaded_resampler.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_resampler.h"
5 | #include "crn_vec.h"
6 |
7 | namespace crnlib
8 | {
9 | class task_pool;
10 | class threaded_resampler
11 | {
12 | CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(threaded_resampler);
13 |
14 | public:
15 | threaded_resampler(task_pool& tp);
16 | ~threaded_resampler();
17 |
18 | enum pixel_format
19 | {
20 | cPF_Y_F32,
21 | cPF_RGBX_F32,
22 | cPF_RGBA_F32,
23 |
24 | cPF_Total
25 | };
26 |
27 | struct params
28 | {
29 | params()
30 | {
31 | clear();
32 | }
33 |
34 | void clear()
35 | {
36 | utils::zero_object(*this);
37 |
38 | m_boundary_op = Resampler::BOUNDARY_CLAMP;
39 | m_sample_low = 0.0f;
40 | m_sample_high = 255.0f;
41 | m_Pfilter_name = CRNLIB_RESAMPLER_DEFAULT_FILTER;
42 | m_filter_x_scale = 1.0f;
43 | m_filter_y_scale = 1.0f;
44 | }
45 |
46 | pixel_format m_fmt;
47 |
48 | const void* m_pSrc_pixels;
49 | uint m_src_width;
50 | uint m_src_height;
51 | uint m_src_pitch;
52 |
53 | void* m_pDst_pixels;
54 | uint m_dst_width;
55 | uint m_dst_height;
56 | uint m_dst_pitch;
57 |
58 | Resampler::Boundary_Op m_boundary_op;
59 |
60 | float m_sample_low;
61 | float m_sample_high;
62 |
63 | const char* m_Pfilter_name;
64 | float m_filter_x_scale;
65 | float m_filter_y_scale;
66 | };
67 |
68 | bool resample(const params& p);
69 |
70 | private:
71 | task_pool* m_pTask_pool;
72 |
73 | const params* m_pParams;
74 |
75 | Resampler::Contrib_List* m_pX_contribs;
76 | Resampler::Contrib_List* m_pY_contribs;
77 | uint m_bytes_per_pixel;
78 |
79 | crnlib::vector m_tmp_img;
80 |
81 | void free_contrib_lists();
82 |
83 | void resample_x_task(uint64 data, void* pData_ptr);
84 | void resample_y_task(uint64 data, void* pData_ptr);
85 | };
86 |
87 | } // namespace crnlib
88 |
--------------------------------------------------------------------------------
/crnlib/crn_packed_uint.h:
--------------------------------------------------------------------------------
1 | // File: crn_packed_uint
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | template
8 | struct packed_uint
9 | {
10 | inline packed_uint() { }
11 |
12 | inline packed_uint(unsigned int val) { *this = val; }
13 |
14 | inline packed_uint(const packed_uint& other) { *this = other; }
15 |
16 | inline packed_uint& operator= (const packed_uint& rhs)
17 | {
18 | if (this != &rhs)
19 | memcpy(m_buf, rhs.m_buf, sizeof(m_buf));
20 | return *this;
21 | }
22 |
23 | inline packed_uint& operator= (unsigned int val)
24 | {
25 | #ifdef CRNLIB_BUILD_DEBUG
26 | if (N == 1)
27 | {
28 | CRNLIB_ASSERT(val <= 0xFFU);
29 | }
30 | else if (N == 2)
31 | {
32 | CRNLIB_ASSERT(val <= 0xFFFFU);
33 | }
34 | else if (N == 3)
35 | {
36 | CRNLIB_ASSERT(val <= 0xFFFFFFU);
37 | }
38 | #endif
39 |
40 | val <<= (8U * (4U - N));
41 |
42 | for (unsigned int i = 0; i < N; i++)
43 | {
44 | m_buf[i] = static_cast(val >> 24U);
45 | val <<= 8U;
46 | }
47 |
48 | return *this;
49 | }
50 |
51 | inline operator unsigned int() const
52 | {
53 | switch (N)
54 | {
55 | case 1: return m_buf[0];
56 | case 2: return (m_buf[0] << 8U) | m_buf[1];
57 | case 3: return (m_buf[0] << 16U) | (m_buf[1] << 8U) | (m_buf[2]);
58 | default: return (m_buf[0] << 24U) | (m_buf[1] << 16U) | (m_buf[2] << 8U) | (m_buf[3]);
59 | }
60 | }
61 |
62 | unsigned char m_buf[N];
63 | };
64 | template
65 | class packed_value
66 | {
67 | public:
68 | packed_value() { }
69 | packed_value(T val) { *this = val; }
70 |
71 | inline operator T() const
72 | {
73 | T result = 0;
74 | for (int i = sizeof(T) - 1; i >= 0; i--)
75 | result = static_cast((result << 8) | m_bytes[i]);
76 | return result;
77 | }
78 | packed_value& operator= (T val)
79 | {
80 | for (int i = 0; i < sizeof(T); i++)
81 | {
82 | m_bytes[i] = static_cast(val);
83 | val >>= 8;
84 | }
85 | return *this;
86 | }
87 | private:
88 | uint8 m_bytes[sizeof(T)];
89 | };
90 | } // namespace crnlib
91 |
92 |
--------------------------------------------------------------------------------
/crnlib/Makefile:
--------------------------------------------------------------------------------
1 | COMPILE_OPTIONS = -O3 -fomit-frame-pointer -ffast-math -fno-math-errno -g -fno-strict-aliasing -Wall -Wno-unused-value -Wno-unused -march=core2
2 | LINKER_OPTIONS = -lpthread -g
3 |
4 | OBJECTS = \
5 | crn_arealist.o \
6 | crn_assert.o \
7 | crn_checksum.o \
8 | crn_colorized_console.o \
9 | crn_command_line_params.o \
10 | crn_comp.o \
11 | crn_console.o \
12 | crn_core.o \
13 | crn_data_stream.o \
14 | crn_mipmapped_texture.o \
15 | crn_decomp.o \
16 | crn_dxt1.o \
17 | crn_dxt5a.o \
18 | crn_dxt.o \
19 | crn_dxt_endpoint_refiner.o \
20 | crn_dxt_fast.o \
21 | crn_dxt_hc_common.o \
22 | crn_dxt_hc.o \
23 | crn_dxt_image.o \
24 | crn_dynamic_string.o \
25 | crn_file_utils.o \
26 | crn_find_files.o \
27 | crn_hash.o \
28 | crn_hash_map.o \
29 | crn_huffman_codes.o \
30 | crn_image_utils.o \
31 | crnlib.o \
32 | crn_math.o \
33 | crn_mem.o \
34 | crn_pixel_format.o \
35 | crn_platform.o \
36 | crn_prefix_coding.o \
37 | crn_qdxt1.o \
38 | crn_qdxt5.o \
39 | crn_rand.o \
40 | crn_resample_filters.o \
41 | crn_resampler.o \
42 | crn_ryg_dxt.o \
43 | crn_sparse_bit_array.o \
44 | crn_stb_image.o \
45 | crn_strutils.o \
46 | crn_symbol_codec.o \
47 | crn_texture_file_types.o \
48 | crn_threaded_resampler.o \
49 | crn_threading_pthreads.o \
50 | crn_timer.o \
51 | crn_utils.o \
52 | crn_value.o \
53 | crn_vector.o \
54 | crn_zeng.o \
55 | crn_texture_comp.o \
56 | crn_texture_conversion.o \
57 | crn_dds_comp.o \
58 | crn_lzma_codec.o \
59 | crn_ktx_texture.o \
60 | crn_etc.o \
61 | crn_rg_etc1.o \
62 | crn_miniz.o \
63 | crn_jpge.o \
64 | crn_jpgd.o \
65 | lzma_7zBuf2.o \
66 | lzma_7zBuf.o \
67 | lzma_7zCrc.o \
68 | lzma_7zFile.o \
69 | lzma_7zStream.o \
70 | lzma_Alloc.o \
71 | lzma_Bcj2.o \
72 | lzma_Bra86.o \
73 | lzma_Bra.o \
74 | lzma_BraIA64.o \
75 | lzma_LzFind.o \
76 | lzma_LzmaDec.o \
77 | lzma_LzmaEnc.o \
78 | lzma_LzmaLib.o
79 |
80 | all: crunch
81 |
82 | %.o: %.cpp
83 | $(CXX) $< -o $@ -c $(COMPILE_OPTIONS)
84 |
85 | crunch.o: ../crunch/crunch.cpp
86 | $(CXX) $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
87 |
88 | corpus_gen.o: ../crunch/corpus_gen.cpp
89 | $(CXX) $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
90 |
91 | corpus_test.o: ../crunch/corpus_test.cpp
92 | $(CXX) $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
93 |
94 | crunch: $(OBJECTS) crunch.o corpus_gen.o corpus_test.o
95 | $(CXX) $(OBJECTS) crunch.o corpus_gen.o corpus_test.o -o crunch $(LINKER_OPTIONS)
96 |
97 | clean:
98 | rm -f *.o
99 | rm -f ../crunch/*.o
100 |
--------------------------------------------------------------------------------
/crnlib/lzma_Bra86.cpp:
--------------------------------------------------------------------------------
1 | /* Bra86.c -- Converter for x86 code (BCJ)
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 | #include "crn_core.h"
4 | #include "lzma_Bra.h"
5 |
6 | namespace crnlib {
7 |
8 | #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
9 |
10 | const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
11 | const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
12 |
13 | SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
14 | {
15 | SizeT bufferPos = 0, prevPosT;
16 | UInt32 prevMask = *state & 0x7;
17 | if (size < 5)
18 | return 0;
19 | ip += 5;
20 | prevPosT = (SizeT)0 - 1;
21 |
22 | for (;;)
23 | {
24 | Byte *p = data + bufferPos;
25 | Byte *limit = data + size - 4;
26 | for (; p < limit; p++)
27 | if ((*p & 0xFE) == 0xE8)
28 | break;
29 | bufferPos = (SizeT)(p - data);
30 | if (p >= limit)
31 | break;
32 | prevPosT = bufferPos - prevPosT;
33 | if (prevPosT > 3)
34 | prevMask = 0;
35 | else
36 | {
37 | prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
38 | if (prevMask != 0)
39 | {
40 | Byte b = p[4 - kMaskToBitNumber[prevMask]];
41 | if (!kMaskToAllowedStatus[prevMask] || Test86MSByte(b))
42 | {
43 | prevPosT = bufferPos;
44 | prevMask = ((prevMask << 1) & 0x7) | 1;
45 | bufferPos++;
46 | continue;
47 | }
48 | }
49 | }
50 | prevPosT = bufferPos;
51 |
52 | if (Test86MSByte(p[4]))
53 | {
54 | UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
55 | UInt32 dest;
56 | for (;;)
57 | {
58 | Byte b;
59 | int index;
60 | if (encoding)
61 | dest = (ip + (UInt32)bufferPos) + src;
62 | else
63 | dest = src - (ip + (UInt32)bufferPos);
64 | if (prevMask == 0)
65 | break;
66 | index = kMaskToBitNumber[prevMask] * 8;
67 | b = (Byte)(dest >> (24 - index));
68 | if (!Test86MSByte(b))
69 | break;
70 | src = dest ^ ((1 << (32 - index)) - 1);
71 | }
72 | p[4] = (Byte)(~(((dest >> 24) & 1) - 1));
73 | p[3] = (Byte)(dest >> 16);
74 | p[2] = (Byte)(dest >> 8);
75 | p[1] = (Byte)dest;
76 | bufferPos += 5;
77 | }
78 | else
79 | {
80 | prevMask = ((prevMask << 1) & 0x7) | 1;
81 | bufferPos++;
82 | }
83 | }
84 | prevPosT = bufferPos - prevPosT;
85 | *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7));
86 | return bufferPos;
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/crnlib/crn_vector.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_vector.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 | #include "crn_vector.h"
5 | #include "crn_rand.h"
6 |
7 | #include "crn_color.h"
8 | #include "crn_vec.h"
9 |
10 | namespace crnlib
11 | {
12 | bool elemental_vector::increase_capacity(uint min_new_capacity, bool grow_hint, uint element_size, object_mover pMover, bool nofail)
13 | {
14 | CRNLIB_ASSERT(m_size <= m_capacity);
15 | #ifdef CRNLIB_64BIT_POINTERS
16 | CRNLIB_ASSERT(min_new_capacity < (0x400000000ULL / element_size));
17 | #else
18 | CRNLIB_ASSERT(min_new_capacity < (0x7FFF0000U / element_size));
19 | #endif
20 |
21 | if (m_capacity >= min_new_capacity)
22 | return true;
23 |
24 | size_t new_capacity = min_new_capacity;
25 | if ((grow_hint) && (!math::is_power_of_2(new_capacity)))
26 | new_capacity = math::next_pow2(new_capacity);
27 |
28 | CRNLIB_ASSERT(new_capacity && (new_capacity > m_capacity));
29 |
30 | const size_t desired_size = element_size * new_capacity;
31 | size_t actual_size;
32 | if (!pMover)
33 | {
34 | void* new_p = crnlib_realloc(m_p, desired_size, &actual_size, true);
35 | if (!new_p)
36 | {
37 | if (nofail)
38 | return false;
39 |
40 | char buf[256];
41 | #ifdef _MSC_VER
42 | sprintf_s(buf, sizeof(buf), "vector: crnlib_realloc() failed allocating %u bytes", (uint)desired_size);
43 | #else
44 | sprintf(buf, "vector: crnlib_realloc() failed allocating %u bytes", (uint)desired_size);
45 | #endif
46 | CRNLIB_FAIL(buf);
47 | }
48 | m_p = new_p;
49 | }
50 | else
51 | {
52 | void* new_p = crnlib_malloc(desired_size, &actual_size);
53 | if (!new_p)
54 | {
55 | if (nofail)
56 | return false;
57 |
58 | char buf[256];
59 | #ifdef _MSC_VER
60 | sprintf_s(buf, sizeof(buf), "vector: crnlib_malloc() failed allocating %u bytes", (uint)desired_size);
61 | #else
62 | sprintf(buf, "vector: crnlib_malloc() failed allocating %u bytes", (uint)desired_size);
63 | #endif
64 | CRNLIB_FAIL(buf);
65 | }
66 |
67 | (*pMover)(new_p, m_p, m_size);
68 |
69 | if (m_p)
70 | crnlib_free(m_p);
71 |
72 | m_p = new_p;
73 | }
74 |
75 | if (actual_size > desired_size)
76 | m_capacity = static_cast(actual_size / element_size);
77 | else
78 | m_capacity = static_cast(new_capacity);
79 |
80 | return true;
81 | }
82 |
83 | } // namespace crnlib
84 |
--------------------------------------------------------------------------------
/crunch/crunch_linux.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/crnlib/lzma_LzFindMt.h:
--------------------------------------------------------------------------------
1 | /* LzFindMt.h -- multithreaded Match finder for LZ algorithms
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 |
4 | #ifndef __LZFINDMT_H
5 | #define __LZFINDMT_H
6 |
7 | #include "lzma_Threads.h"
8 | #include "lzma_LzFind.h"
9 |
10 | namespace crnlib {
11 |
12 | #define kMtHashBlockSize (1 << 13)
13 | #define kMtHashNumBlocks (1 << 3)
14 | #define kMtHashNumBlocksMask (kMtHashNumBlocks - 1)
15 |
16 | #define kMtBtBlockSize (1 << 14)
17 | #define kMtBtNumBlocks (1 << 6)
18 | #define kMtBtNumBlocksMask (kMtBtNumBlocks - 1)
19 |
20 | typedef struct _CMtSync
21 | {
22 | Bool wasCreated;
23 | Bool needStart;
24 | Bool exit;
25 | Bool stopWriting;
26 |
27 | CThread thread;
28 | CAutoResetEvent canStart;
29 | CAutoResetEvent wasStarted;
30 | CAutoResetEvent wasStopped;
31 | CSemaphore freeSemaphore;
32 | CSemaphore filledSemaphore;
33 | Bool csWasInitialized;
34 | Bool csWasEntered;
35 | CCriticalSection cs;
36 | UInt32 numProcessedBlocks;
37 | } CMtSync;
38 |
39 | typedef UInt32 * (*Mf_Mix_Matches)(void *p, UInt32 matchMinPos, UInt32 *distances);
40 |
41 | /* kMtCacheLineDummy must be >= size_of_CPU_cache_line */
42 | #define kMtCacheLineDummy 128
43 |
44 | typedef void (*Mf_GetHeads)(const Byte *buffer, UInt32 pos,
45 | UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc);
46 |
47 | typedef struct _CMatchFinderMt
48 | {
49 | /* LZ */
50 | const Byte *pointerToCurPos;
51 | UInt32 *btBuf;
52 | UInt32 btBufPos;
53 | UInt32 btBufPosLimit;
54 | UInt32 lzPos;
55 | UInt32 btNumAvailBytes;
56 |
57 | UInt32 *hash;
58 | UInt32 fixedHashSize;
59 | UInt32 historySize;
60 | const UInt32 *crc;
61 |
62 | Mf_Mix_Matches MixMatchesFunc;
63 |
64 | /* LZ + BT */
65 | CMtSync btSync;
66 | Byte btDummy[kMtCacheLineDummy];
67 |
68 | /* BT */
69 | UInt32 *hashBuf;
70 | UInt32 hashBufPos;
71 | UInt32 hashBufPosLimit;
72 | UInt32 hashNumAvail;
73 |
74 | CLzRef *son;
75 | UInt32 matchMaxLen;
76 | UInt32 numHashBytes;
77 | UInt32 pos;
78 | Byte *buffer;
79 | UInt32 cyclicBufferPos;
80 | UInt32 cyclicBufferSize; /* it must be historySize + 1 */
81 | UInt32 cutValue;
82 |
83 | /* BT + Hash */
84 | CMtSync hashSync;
85 | /* Byte hashDummy[kMtCacheLineDummy]; */
86 |
87 | /* Hash */
88 | Mf_GetHeads GetHeadsFunc;
89 | CMatchFinder *MatchFinder;
90 | } CMatchFinderMt;
91 |
92 | void MatchFinderMt_Construct(CMatchFinderMt *p);
93 | void MatchFinderMt_Destruct(CMatchFinderMt *p, ISzAlloc *alloc);
94 | SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddBufferBefore,
95 | UInt32 matchMaxLen, UInt32 keepAddBufferAfter, ISzAlloc *alloc);
96 | void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder *vTable);
97 | void MatchFinderMt_ReleaseStream(CMatchFinderMt *p);
98 |
99 | }
100 |
101 | #endif
102 |
--------------------------------------------------------------------------------
/crnlib/crn_rand.h:
--------------------------------------------------------------------------------
1 | // File: crn_rand.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | class kiss99
8 | {
9 | public:
10 | kiss99();
11 |
12 | void seed(uint32 i, uint32 j, uint32 k);
13 |
14 | inline uint32 next();
15 |
16 | private:
17 | uint32 x;
18 | uint32 y;
19 | uint32 z;
20 | uint32 c;
21 | };
22 |
23 | class well512
24 | {
25 | public:
26 | well512();
27 |
28 | enum { cStateSize = 16 };
29 | void seed(uint32 seed[cStateSize]);
30 | void seed(uint32 seed);
31 | void seed(uint32 seed1, uint32 seed2, uint32 seed3);
32 |
33 | inline uint32 next();
34 |
35 | private:
36 | uint32 m_state[cStateSize];
37 | uint32 m_index;
38 | };
39 |
40 | class ranctx
41 | {
42 | public:
43 | ranctx() { seed(0xDE149737); }
44 |
45 | void seed(uint32 seed);
46 |
47 | inline uint32 next();
48 |
49 | private:
50 | uint32 a;
51 | uint32 b;
52 | uint32 c;
53 | uint32 d;
54 | };
55 |
56 | class random
57 | {
58 | public:
59 | random();
60 | random(uint32 i);
61 |
62 | void seed(uint32 i);
63 | void seed(uint32 i1, uint32 i2, uint32 i3);
64 |
65 | uint32 urand32();
66 | uint64 urand64();
67 |
68 | // "Fast" variant uses no multiplies.
69 | uint32 fast_urand32();
70 |
71 | uint32 bit();
72 |
73 | // Returns random between [0, 1)
74 | double drand(double l, double h);
75 |
76 | float frand(float l, float h);
77 |
78 | // Returns random between [l, h)
79 | int irand(int l, int h);
80 |
81 | // Returns random between [l, h]
82 | int irand_inclusive(int l, int h);
83 |
84 | double gaussian(double mean, double stddev);
85 |
86 | void test();
87 |
88 | private:
89 | ranctx m_ranctx;
90 | kiss99 m_kiss99;
91 | well512 m_well512;
92 | };
93 |
94 | // Simpler, minimal state PRNG
95 | class fast_random
96 | {
97 | public:
98 | fast_random();
99 | fast_random(uint32 i);
100 | fast_random(const fast_random& other);
101 | fast_random& operator=(const fast_random& other);
102 |
103 | void seed(uint32 i);
104 |
105 | uint32 urand32();
106 | uint64 urand64();
107 |
108 | int irand(int l, int h);
109 |
110 | double drand(double l, double h);
111 |
112 | float frand(float l, float h);
113 |
114 | private:
115 | uint32 jsr;
116 | uint32 jcong;
117 | };
118 |
119 | } // namespace crnlib
120 |
--------------------------------------------------------------------------------
/crnlib/crn_texture_file_types.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_texture_file_types.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 | #include "crn_texture_file_types.h"
5 | #include "crn_file_utils.h"
6 |
7 | namespace crnlib
8 | {
9 | const char* texture_file_types::get_extension(format fmt)
10 | {
11 | CRNLIB_ASSERT(fmt < cNumFileFormats);
12 | if (fmt >= cNumFileFormats)
13 | return NULL;
14 |
15 | static const char* extensions[cNumFileFormats] =
16 | {
17 | "dds",
18 | "crn",
19 | "ktx",
20 |
21 | "tga",
22 | "png",
23 | "jpg",
24 | "jpeg",
25 | "bmp",
26 | "gif",
27 | "tif",
28 | "tiff",
29 | "ppm",
30 | "pgm",
31 | "psd",
32 | "jp2",
33 |
34 | "",
35 | ""
36 | };
37 | return extensions[fmt];
38 | }
39 |
40 | texture_file_types::format texture_file_types::determine_file_format(const char* pFilename)
41 | {
42 | dynamic_string ext;
43 | if (!file_utils::split_path(pFilename, NULL, NULL, NULL, &ext))
44 | return cFormatInvalid;
45 |
46 | if (ext.is_empty())
47 | return cFormatInvalid;
48 |
49 | if (ext[0] == '.')
50 | ext.right(1);
51 |
52 | for (uint i = 0; i < cNumFileFormats; i++)
53 | if (ext == get_extension(static_cast(i)))
54 | return static_cast(i);
55 |
56 | return cFormatInvalid;
57 | }
58 |
59 | bool texture_file_types::supports_mipmaps(format fmt)
60 | {
61 | switch (fmt)
62 | {
63 | case cFormatCRN:
64 | case cFormatDDS:
65 | case cFormatKTX:
66 | return true;
67 | default: break;
68 | }
69 |
70 | return false;
71 | }
72 |
73 | bool texture_file_types::supports_alpha(format fmt)
74 | {
75 | switch (fmt)
76 | {
77 | case cFormatJPG:
78 | case cFormatJPEG:
79 | case cFormatGIF:
80 | case cFormatJP2:
81 | return false;
82 | default: break;
83 | }
84 |
85 | return true;
86 | }
87 |
88 | const char* get_texture_type_desc(texture_type t)
89 | {
90 | switch (t)
91 | {
92 | case cTextureTypeUnknown: return "Unknown";
93 | case cTextureTypeRegularMap: return "2D map";
94 | case cTextureTypeNormalMap: return "Normal map";
95 | case cTextureTypeVerticalCrossCubemap: return "Vertical Cross Cubemap";
96 | case cTextureTypeCubemap: return "Cubemap";
97 | default: break;
98 | }
99 |
100 | CRNLIB_ASSERT(false);
101 |
102 | return "?";
103 | }
104 |
105 | } // namespace crnlib
106 |
--------------------------------------------------------------------------------
/crnlib/crn_platform.h:
--------------------------------------------------------------------------------
1 | // File: crn_platform.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | bool crnlib_is_debugger_present(void);
6 | void crnlib_debug_break(void);
7 | void crnlib_output_debug_string(const char* p);
8 |
9 | // actually in crnlib_assert.cpp
10 | void crnlib_assert(const char* pExp, const char* pFile, unsigned line);
11 | void crnlib_fail(const char* pExp, const char* pFile, unsigned line);
12 |
13 | #if CRNLIB_LITTLE_ENDIAN_CPU
14 | const bool c_crnlib_little_endian_platform = true;
15 | #else
16 | const bool c_crnlib_little_endian_platform = false;
17 | #endif
18 |
19 | const bool c_crnlib_big_endian_platform = !c_crnlib_little_endian_platform;
20 |
21 | #if defined(__GNUC__) && !defined(__APPLE__)
22 | #define crn_fopen(pDstFile, f, m) *(pDstFile) = fopen64(f, m)
23 | #define crn_fseek fseeko64
24 | #define crn_ftell ftello64
25 | #elif defined( _MSC_VER )
26 | #define crn_fopen(pDstFile, f, m) fopen_s(pDstFile, f, m)
27 | #define crn_fseek _fseeki64
28 | #define crn_ftell _ftelli64
29 | #else
30 | #define crn_fopen(pDstFile, f, m) *(pDstFile) = fopen(f, m)
31 | #define crn_fseek(s, o, w) fseek(s, static_cast(o), w)
32 | #define crn_ftell ftell
33 | #endif
34 |
35 | #if CRNLIB_USE_WIN32_API
36 | #define CRNLIB_BREAKPOINT DebugBreak();
37 | #define CRNLIB_BUILTIN_EXPECT(c, v) c
38 | #elif defined(__GNUC__)
39 | #define CRNLIB_BREAKPOINT asm("int $3");
40 | #define CRNLIB_BUILTIN_EXPECT(c, v) __builtin_expect(c, v)
41 | #else
42 | #define CRNLIB_BREAKPOINT
43 | #define CRNLIB_BUILTIN_EXPECT(c, v) c
44 | #endif
45 |
46 | #if defined(__GNUC__)
47 | #define CRNLIB_ALIGNED(x) __attribute__((aligned(x)))
48 | #define CRNLIB_NOINLINE __attribute__((noinline))
49 | #elif defined(_MSC_VER)
50 | #define CRNLIB_ALIGNED(x) __declspec(align(x))
51 | #define CRNLIB_NOINLINE __declspec(noinline)
52 | #else
53 | #define CRNLIB_ALIGNED(x)
54 | #define CRNLIB_NOINLINE
55 | #endif
56 |
57 | #define CRNLIB_GET_ALIGNMENT(v) ((!sizeof(v)) ? 1 : (__alignof(v) ? __alignof(v) : sizeof(uint32)))
58 |
59 | #ifndef _MSC_VER
60 | int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
61 | int vsprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, va_list args);
62 | char* strlwr(char* p);
63 | char* strupr(char *p);
64 | #define _stricmp strcasecmp
65 | #define _strnicmp strncasecmp
66 | #endif
67 |
68 | inline bool crnlib_is_little_endian() { return c_crnlib_little_endian_platform; }
69 | inline bool crnlib_is_big_endian() { return c_crnlib_big_endian_platform; }
70 |
71 | inline bool crnlib_is_pc()
72 | {
73 | #ifdef CRNLIB_PLATFORM_PC
74 | return true;
75 | #else
76 | return false;
77 | #endif
78 | }
79 |
80 | inline bool crnlib_is_x86()
81 | {
82 | #ifdef CRNLIB_PLATFORM_PC_X86
83 | return true;
84 | #else
85 | return false;
86 | #endif
87 | }
88 |
89 | inline bool crnlib_is_x64()
90 | {
91 | #ifdef CRNLIB_PLATFORM_PC_X64
92 | return true;
93 | #else
94 | return false;
95 | #endif
96 | }
97 |
--------------------------------------------------------------------------------
/crnlib/lzma_LzmaEnc.h:
--------------------------------------------------------------------------------
1 | /* LzmaEnc.h -- LZMA Encoder
2 | 2008-10-04 : Igor Pavlov : Public domain */
3 |
4 | #ifndef __LZMAENC_H
5 | #define __LZMAENC_H
6 |
7 | #include "lzma_Types.h"
8 |
9 | namespace crnlib {
10 |
11 | #define LZMA_PROPS_SIZE 5
12 |
13 | typedef struct _CLzmaEncProps
14 | {
15 | int level; /* 0 <= level <= 9 */
16 | UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
17 | (1 << 12) <= dictSize <= (1 << 30) for 64-bit version
18 | default = (1 << 24) */
19 | int lc; /* 0 <= lc <= 8, default = 3 */
20 | int lp; /* 0 <= lp <= 4, default = 0 */
21 | int pb; /* 0 <= pb <= 4, default = 2 */
22 | int algo; /* 0 - fast, 1 - normal, default = 1 */
23 | int fb; /* 5 <= fb <= 273, default = 32 */
24 | int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
25 | int numHashBytes; /* 2, 3 or 4, default = 4 */
26 | UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
27 | unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
28 | int numThreads; /* 1 or 2, default = 2 */
29 | } CLzmaEncProps;
30 |
31 | void LzmaEncProps_Init(CLzmaEncProps *p);
32 | void LzmaEncProps_Normalize(CLzmaEncProps *p);
33 | UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2);
34 |
35 |
36 | /* ---------- CLzmaEncHandle Interface ---------- */
37 |
38 | /* LzmaEnc_* functions can return the following exit codes:
39 | Returns:
40 | SZ_OK - OK
41 | SZ_ERROR_MEM - Memory allocation error
42 | SZ_ERROR_PARAM - Incorrect paramater in props
43 | SZ_ERROR_WRITE - Write callback error.
44 | SZ_ERROR_PROGRESS - some break from progress callback
45 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
46 | */
47 |
48 | typedef void * CLzmaEncHandle;
49 |
50 | CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc);
51 | void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig);
52 | SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props);
53 | SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
54 | SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream,
55 | ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
56 | SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
57 | int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
58 |
59 | /* ---------- One Call Interface ---------- */
60 |
61 | /* LzmaEncode
62 | Return code:
63 | SZ_OK - OK
64 | SZ_ERROR_MEM - Memory allocation error
65 | SZ_ERROR_PARAM - Incorrect paramater
66 | SZ_ERROR_OUTPUT_EOF - output buffer overflow
67 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
68 | */
69 |
70 | SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
71 | const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
72 | ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
73 |
74 | }
75 |
76 | #endif
77 |
--------------------------------------------------------------------------------
/crnlib/crn_data_stream.h:
--------------------------------------------------------------------------------
1 | // File: crn_data_stream.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 |
5 | namespace crnlib
6 | {
7 | enum data_stream_attribs
8 | {
9 | cDataStreamReadable = 1,
10 | cDataStreamWritable = 2,
11 | cDataStreamSeekable = 4
12 | };
13 |
14 | const int64 DATA_STREAM_SIZE_UNKNOWN = cINT64_MAX;
15 | const int64 DATA_STREAM_SIZE_INFINITE = cUINT64_MAX;
16 |
17 | class data_stream
18 | {
19 | data_stream(const data_stream&);
20 | data_stream& operator= (const data_stream&);
21 |
22 | public:
23 | data_stream();
24 | data_stream(const char* pName, uint attribs);
25 |
26 | virtual ~data_stream() { }
27 |
28 | virtual data_stream *get_parent() { return NULL; }
29 |
30 | virtual bool close() { m_opened = false; m_error = false; m_got_cr = false; return true; }
31 |
32 | typedef uint16 attribs_t;
33 | inline attribs_t get_attribs() const { return m_attribs; }
34 |
35 | inline bool is_opened() const { return m_opened; }
36 |
37 | inline bool is_readable() const { return utils::is_bit_set(m_attribs, cDataStreamReadable); }
38 | inline bool is_writable() const { return utils::is_bit_set(m_attribs, cDataStreamWritable); }
39 | inline bool is_seekable() const { return utils::is_bit_set(m_attribs, cDataStreamSeekable); }
40 |
41 | inline bool get_error() const { return m_error; }
42 |
43 | inline const dynamic_string& get_name() const { return m_name; }
44 | inline void set_name(const char* pName) { m_name.set(pName); }
45 |
46 | virtual uint read(void* pBuf, uint len) = 0;
47 | virtual uint64 skip(uint64 len);
48 |
49 | virtual uint write(const void* pBuf, uint len) = 0;
50 | virtual bool flush() = 0;
51 |
52 | virtual bool is_size_known() const { return true; }
53 |
54 | // Returns DATA_STREAM_SIZE_UNKNOWN if size hasn't been determined yet, or DATA_STREAM_SIZE_INFINITE for infinite streams.
55 | virtual uint64 get_size() = 0;
56 | virtual uint64 get_remaining() = 0;
57 |
58 | virtual uint64 get_ofs() = 0;
59 | virtual bool seek(int64 ofs, bool relative) = 0;
60 |
61 | virtual const void* get_ptr() const { return NULL; }
62 |
63 | inline int read_byte() { uint8 c; if (read(&c, 1) != 1) return -1; return c; }
64 | inline bool write_byte(uint8 c) { return write(&c, 1) == 1; }
65 |
66 | bool read_line(dynamic_string& str);
67 | bool printf(const char* p, ...);
68 | bool write_line(const dynamic_string& str);
69 | bool write_bom() { uint16 bom = 0xFEFF; return write(&bom, sizeof(bom)) == sizeof(bom); }
70 |
71 | bool read_array(vector& buf);
72 | bool write_array(const vector& buf);
73 |
74 | protected:
75 | dynamic_string m_name;
76 |
77 | attribs_t m_attribs;
78 | bool m_opened : 1;
79 | bool m_error : 1;
80 | bool m_got_cr : 1;
81 |
82 | inline void set_error() { m_error = true; }
83 | inline void clear_error() { m_error = false; }
84 |
85 | inline void post_seek() { m_got_cr = false; }
86 | };
87 |
88 | } // namespace crnlib
89 |
90 |
--------------------------------------------------------------------------------
/crn.2008.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 10.00
3 | # Visual Studio 2008
4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "crunch", "crunch\crunch.2008.vcproj", "{8F645BA1-B996-49EB-859B-970A671DE05D}"
5 | ProjectSection(ProjectDependencies) = postProject
6 | {CF2E70E8-7133-4D96-92C7-68BB406C0664} = {CF2E70E8-7133-4D96-92C7-68BB406C0664}
7 | EndProjectSection
8 | EndProject
9 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "crnlib", "crnlib\crnlib.2008.vcproj", "{CF2E70E8-7133-4D96-92C7-68BB406C0664}"
10 | EndProject
11 | Global
12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
13 | Debug_DLL|Win32 = Debug_DLL|Win32
14 | Debug_DLL|x64 = Debug_DLL|x64
15 | Debug|Win32 = Debug|Win32
16 | Debug|x64 = Debug|x64
17 | Release_DLL|Win32 = Release_DLL|Win32
18 | Release_DLL|x64 = Release_DLL|x64
19 | Release|Win32 = Release|Win32
20 | Release|x64 = Release|x64
21 | EndGlobalSection
22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
23 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug_DLL|Win32.ActiveCfg = Debug|Win32
24 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug_DLL|x64.ActiveCfg = Debug|x64
25 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug|Win32.ActiveCfg = Debug|Win32
26 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug|Win32.Build.0 = Debug|Win32
27 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug|x64.ActiveCfg = Debug|x64
28 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug|x64.Build.0 = Debug|x64
29 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release_DLL|Win32.ActiveCfg = Release|Win32
30 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release_DLL|x64.ActiveCfg = Release|x64
31 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release|Win32.ActiveCfg = Release|Win32
32 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release|Win32.Build.0 = Release|Win32
33 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release|x64.ActiveCfg = Release|x64
34 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release|x64.Build.0 = Release|x64
35 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug_DLL|Win32.ActiveCfg = Debug_DLL|Win32
36 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug_DLL|Win32.Build.0 = Debug_DLL|Win32
37 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug_DLL|x64.ActiveCfg = Debug_DLL|x64
38 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug_DLL|x64.Build.0 = Debug_DLL|x64
39 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug|Win32.ActiveCfg = Debug|Win32
40 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug|Win32.Build.0 = Debug|Win32
41 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug|x64.ActiveCfg = Debug|x64
42 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug|x64.Build.0 = Debug|x64
43 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release_DLL|Win32.ActiveCfg = Release_DLL|Win32
44 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release_DLL|Win32.Build.0 = Release_DLL|Win32
45 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release_DLL|x64.ActiveCfg = Release_DLL|x64
46 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release_DLL|x64.Build.0 = Release_DLL|x64
47 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release|Win32.ActiveCfg = Release|Win32
48 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release|Win32.Build.0 = Release|Win32
49 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release|x64.ActiveCfg = Release|x64
50 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release|x64.Build.0 = Release|x64
51 | EndGlobalSection
52 | GlobalSection(SolutionProperties) = preSolution
53 | HideSolutionNode = FALSE
54 | EndGlobalSection
55 | EndGlobal
56 |
--------------------------------------------------------------------------------
/crnlib/crn_data_stream.cpp:
--------------------------------------------------------------------------------
1 | // File: crn_data_stream.cpp
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #include "crn_core.h"
4 | #include "crn_data_stream.h"
5 |
6 | namespace crnlib
7 | {
8 | data_stream::data_stream() :
9 | m_attribs(0),
10 | m_opened(false), m_error(false), m_got_cr(false)
11 | {
12 | }
13 |
14 | data_stream::data_stream(const char* pName, uint attribs) :
15 | m_name(pName),
16 | m_attribs(static_cast(attribs)),
17 | m_opened(false), m_error(false), m_got_cr(false)
18 | {
19 | }
20 |
21 | uint64 data_stream::skip(uint64 len)
22 | {
23 | uint64 total_bytes_read = 0;
24 |
25 | const uint cBufSize = 1024;
26 | uint8 buf[cBufSize];
27 |
28 | while (len)
29 | {
30 | const uint64 bytes_to_read = math::minimum(sizeof(buf), len);
31 | const uint64 bytes_read = read(buf, static_cast(bytes_to_read));
32 | total_bytes_read += bytes_read;
33 |
34 | if (bytes_read != bytes_to_read)
35 | break;
36 |
37 | len -= bytes_read;
38 | }
39 |
40 | return total_bytes_read;
41 | }
42 |
43 | bool data_stream::read_line(dynamic_string& str)
44 | {
45 | str.empty();
46 |
47 | for ( ; ; )
48 | {
49 | const int c = read_byte();
50 |
51 | const bool prev_got_cr = m_got_cr;
52 | m_got_cr = false;
53 |
54 | if (c < 0)
55 | {
56 | if (!str.is_empty())
57 | break;
58 |
59 | return false;
60 | }
61 | else if ((26 == c) || (!c))
62 | continue;
63 | else if (13 == c)
64 | {
65 | m_got_cr = true;
66 | break;
67 | }
68 | else if (10 == c)
69 | {
70 | if (prev_got_cr)
71 | continue;
72 |
73 | break;
74 | }
75 |
76 | str.append_char(static_cast(c));
77 | }
78 |
79 | return true;
80 | }
81 |
82 | bool data_stream::printf(const char* p, ...)
83 | {
84 | va_list args;
85 |
86 | va_start(args, p);
87 | dynamic_string buf;
88 | buf.format_args(p, args);
89 | va_end(args);
90 |
91 | return write(buf.get_ptr(), buf.get_len() * sizeof(char)) == buf.get_len() * sizeof(char);
92 | }
93 |
94 | bool data_stream::write_line(const dynamic_string& str)
95 | {
96 | if (!str.is_empty())
97 | return write(str.get_ptr(), str.get_len()) == str.get_len();
98 |
99 | return true;
100 | }
101 |
102 | bool data_stream::read_array(vector& buf)
103 | {
104 | if (buf.size() < get_remaining())
105 | {
106 | if (get_remaining() > 1024U*1024U*1024U)
107 | return false;
108 |
109 | buf.resize((uint)get_remaining());
110 | }
111 |
112 | if (!get_remaining())
113 | {
114 | buf.resize(0);
115 | return true;
116 | }
117 |
118 | return read(&buf[0], buf.size()) == buf.size();
119 | }
120 |
121 | bool data_stream::write_array(const vector& buf)
122 | {
123 | if (!buf.empty())
124 | return write(&buf[0], buf.size()) == buf.size();
125 | return true;
126 | }
127 |
128 | } // namespace crnlib
129 |
--------------------------------------------------------------------------------
/crnlib/lzma_Alloc.cpp:
--------------------------------------------------------------------------------
1 | /* Alloc.c -- Memory allocation functions
2 | 2008-09-24
3 | Igor Pavlov
4 | Public domain */
5 | #include "crn_core.h"
6 | #ifdef _WIN32
7 | #include
8 | #endif
9 | #include
10 |
11 | #include "lzma_Alloc.h"
12 |
13 | namespace crnlib {
14 |
15 | /* #define _SZ_ALLOC_DEBUG */
16 |
17 | /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
18 | #ifdef _SZ_ALLOC_DEBUG
19 | #include
20 | int g_allocCount = 0;
21 | int g_allocCountMid = 0;
22 | int g_allocCountBig = 0;
23 | #endif
24 |
25 | void *MyAlloc(size_t size)
26 | {
27 | if (size == 0)
28 | return 0;
29 | #ifdef _SZ_ALLOC_DEBUG
30 | {
31 | void *p = crnlib::crnlib_malloc(size);
32 | fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p);
33 | return p;
34 | }
35 | #else
36 | return crnlib::crnlib_malloc(size);
37 | #endif
38 | }
39 |
40 | void MyFree(void *address)
41 | {
42 | #ifdef _SZ_ALLOC_DEBUG
43 | if (address != 0)
44 | fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address);
45 | #endif
46 | crnlib::crnlib_free(address);
47 | }
48 |
49 | #ifdef _WIN32
50 |
51 | void *MidAlloc(size_t size)
52 | {
53 | if (size == 0)
54 | return 0;
55 | #ifdef _SZ_ALLOC_DEBUG
56 | fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
57 | #endif
58 | return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
59 | }
60 |
61 | void MidFree(void *address)
62 | {
63 | #ifdef _SZ_ALLOC_DEBUG
64 | if (address != 0)
65 | fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
66 | #endif
67 | if (address == 0)
68 | return;
69 | VirtualFree(address, 0, MEM_RELEASE);
70 | }
71 |
72 | #ifndef MEM_LARGE_PAGES
73 | #undef _7ZIP_LARGE_PAGES
74 | #endif
75 |
76 | #ifdef _7ZIP_LARGE_PAGES
77 | SIZE_T g_LargePageSize = 0;
78 | typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
79 | #endif
80 |
81 | void SetLargePageSize()
82 | {
83 | #ifdef _7ZIP_LARGE_PAGES
84 | SIZE_T size = 0;
85 | GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
86 | GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
87 | if (largePageMinimum == 0)
88 | return;
89 | size = largePageMinimum();
90 | if (size == 0 || (size & (size - 1)) != 0)
91 | return;
92 | g_LargePageSize = size;
93 | #endif
94 | }
95 |
96 |
97 | void *BigAlloc(size_t size)
98 | {
99 | if (size == 0)
100 | return 0;
101 | #ifdef _SZ_ALLOC_DEBUG
102 | fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
103 | #endif
104 |
105 | #ifdef _7ZIP_LARGE_PAGES
106 | if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
107 | {
108 | void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
109 | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
110 | if (res != 0)
111 | return res;
112 | }
113 | #endif
114 | return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
115 | }
116 |
117 | void BigFree(void *address)
118 | {
119 | #ifdef _SZ_ALLOC_DEBUG
120 | if (address != 0)
121 | fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
122 | #endif
123 |
124 | if (address == 0)
125 | return;
126 | VirtualFree(address, 0, MEM_RELEASE);
127 | }
128 |
129 | #endif
130 |
131 | }
132 |
--------------------------------------------------------------------------------
/crnlib/crn_rg_etc1.h:
--------------------------------------------------------------------------------
1 | // File: rg_etc1.h - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich
2 | // Please see ZLIB license at the end of this file.
3 | #pragma once
4 |
5 | namespace crnlib {
6 |
7 | namespace rg_etc1
8 | {
9 | // Unpacks an 8-byte ETC1 compressed block to a block of 4x4 32bpp RGBA pixels.
10 | // Returns false if the block is invalid. Invalid blocks will still be unpacked with clamping.
11 | // This function is thread safe, and does not dynamically allocate any memory.
12 | // If preserve_alpha is true, the alpha channel of the destination pixels will not be overwritten. Otherwise, alpha will be set to 255.
13 | bool unpack_etc1_block(const void *pETC1_block, unsigned int* pDst_pixels_rgba, bool preserve_alpha = false);
14 |
15 | // Quality setting = the higher the quality, the slower.
16 | // To pack large textures, it is highly recommended to call pack_etc1_block() in parallel, on different blocks, from multiple threads (particularly when using cHighQuality).
17 | enum etc1_quality
18 | {
19 | cLowQuality,
20 | cMediumQuality,
21 | cHighQuality,
22 | };
23 |
24 | struct etc1_pack_params
25 | {
26 | etc1_quality m_quality;
27 | bool m_dithering;
28 |
29 | inline etc1_pack_params()
30 | {
31 | clear();
32 | }
33 |
34 | void clear()
35 | {
36 | m_quality = cHighQuality;
37 | m_dithering = false;
38 | }
39 | };
40 |
41 | // Important: pack_etc1_block_init() must be called before calling pack_etc1_block().
42 | void pack_etc1_block_init();
43 |
44 | // Packs a 4x4 block of 32bpp RGBA pixels to an 8-byte ETC1 block.
45 | // 32-bit RGBA pixels must always be arranged as (R,G,B,A) (R first, A last) in memory, independent of platform endianness. A should always be 255.
46 | // Returns squared error of result.
47 | // This function is thread safe, and does not dynamically allocate any memory.
48 | // pack_etc1_block() does not currently support "perceptual" colorspace metrics - it primarily optimizes for RGB RMSE.
49 | unsigned int pack_etc1_block(void* pETC1_block, const unsigned int* pSrc_pixels_rgba, etc1_pack_params& pack_params);
50 |
51 | } // namespace rg_etc1
52 |
53 | } // namespace crnlib
54 |
55 | //------------------------------------------------------------------------------
56 | //
57 | // rg_etc1 uses the ZLIB license:
58 | // http://opensource.org/licenses/Zlib
59 | //
60 | // Copyright (c) 2012 Rich Geldreich
61 | //
62 | // This software is provided 'as-is', without any express or implied
63 | // warranty. In no event will the authors be held liable for any damages
64 | // arising from the use of this software.
65 | //
66 | // Permission is granted to anyone to use this software for any purpose,
67 | // including commercial applications, and to alter it and redistribute it
68 | // freely, subject to the following restrictions:
69 | //
70 | // 1. The origin of this software must not be misrepresented; you must not
71 | // claim that you wrote the original software. If you use this software
72 | // in a product, an acknowledgment in the product documentation would be
73 | // appreciated but is not required.
74 | //
75 | // 2. Altered source versions must be plainly marked as such, and must not be
76 | // misrepresented as being the original software.
77 | //
78 | // 3. This notice may not be removed or altered from any source distribution.
79 | //
80 | //------------------------------------------------------------------------------
81 |
--------------------------------------------------------------------------------
/crnlib/lzham_timer.h:
--------------------------------------------------------------------------------
1 | // File: lzham_timer.h
2 | // See Copyright Notice and license at the end of include/lzham.h
3 | #pragma once
4 |
5 | namespace lzham
6 | {
7 | typedef unsigned long long timer_ticks;
8 |
9 | class lzham_timer
10 | {
11 | public:
12 | lzham_timer();
13 | lzham_timer(timer_ticks start_ticks);
14 |
15 | void start();
16 | void start(timer_ticks start_ticks);
17 |
18 | void stop();
19 |
20 | double get_elapsed_secs() const;
21 | inline double get_elapsed_ms() const { return get_elapsed_secs() * 1000.0f; }
22 | timer_ticks get_elapsed_us() const;
23 |
24 | static void init();
25 | static inline timer_ticks get_ticks_per_sec() { return g_freq; }
26 | static timer_ticks get_init_ticks();
27 | static timer_ticks get_ticks();
28 | static double ticks_to_secs(timer_ticks ticks);
29 | static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; }
30 | static inline double get_secs() { return ticks_to_secs(get_ticks()); }
31 | static inline double get_ms() { return ticks_to_ms(get_ticks()); }
32 |
33 | private:
34 | static timer_ticks g_init_ticks;
35 | static timer_ticks g_freq;
36 | static double g_inv_freq;
37 |
38 | timer_ticks m_start_time;
39 | timer_ticks m_stop_time;
40 |
41 | bool m_started : 1;
42 | bool m_stopped : 1;
43 | };
44 |
45 | enum var_args_t { cVarArgs };
46 |
47 | #if LZHAM_PERF_SECTIONS
48 | class scoped_perf_section
49 | {
50 | public:
51 | inline scoped_perf_section() :
52 | m_start_ticks(lzham_timer::get_ticks())
53 | {
54 | m_name[0] = '?';
55 | m_name[1] = '\0';
56 | }
57 |
58 | inline scoped_perf_section(const char *pName) :
59 | m_start_ticks(lzham_timer::get_ticks())
60 | {
61 | strcpy_s(m_name, pName);
62 |
63 | lzham_buffered_printf("Thread: 0x%08X, BEGIN Time: %3.3fms, Section: %s\n", GetCurrentThreadId(), lzham_timer::ticks_to_ms(m_start_ticks), m_name);
64 | }
65 |
66 | inline scoped_perf_section(var_args_t, const char *pName, ...) :
67 | m_start_ticks(lzham_timer::get_ticks())
68 | {
69 | va_list args;
70 | va_start(args, pName);
71 | vsprintf_s(m_name, sizeof(m_name), pName, args);
72 | va_end(args);
73 |
74 | lzham_buffered_printf("Thread: 0x%08X, BEGIN Time: %3.3fms, Section: %s\n", GetCurrentThreadId(), lzham_timer::ticks_to_ms(m_start_ticks), m_name);
75 | }
76 |
77 | inline ~scoped_perf_section()
78 | {
79 | double end_ms = lzham_timer::get_ms();
80 | double start_ms = lzham_timer::ticks_to_ms(m_start_ticks);
81 |
82 | lzham_buffered_printf("Thread: 0x%08X, END Time: %3.3fms, Total: %3.3fms, Section: %s\n", GetCurrentThreadId(), end_ms, end_ms - start_ms, m_name);
83 | }
84 |
85 | private:
86 | char m_name[64];
87 | timer_ticks m_start_ticks;
88 | };
89 | #else
90 | class scoped_perf_section
91 | {
92 | public:
93 | inline scoped_perf_section() { }
94 | inline scoped_perf_section(const char *pName) { (void)pName; }
95 | inline scoped_perf_section(var_args_t, const char *pName, ...) { (void)pName; }
96 | };
97 | #endif // LZHAM_PERF_SECTIONS
98 |
99 | } // namespace lzham
100 |
--------------------------------------------------------------------------------
/example2/timer.cpp:
--------------------------------------------------------------------------------
1 | // File: timer.cpp
2 | // A simple high-precision, platform independent timer class.
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | #include "timer.h"
9 |
10 | #if defined(WIN32)
11 | #include
12 | #elif defined(_XBOX)
13 | #include
14 | #endif
15 |
16 | unsigned long long timer::g_init_ticks;
17 | unsigned long long timer::g_freq;
18 | double timer::g_inv_freq;
19 |
20 | #if defined(WIN32) || defined(_XBOX)
21 | inline void query_counter(timer_ticks *pTicks)
22 | {
23 | QueryPerformanceCounter(reinterpret_cast(pTicks));
24 | }
25 | inline void query_counter_frequency(timer_ticks *pTicks)
26 | {
27 | QueryPerformanceFrequency(reinterpret_cast(pTicks));
28 | }
29 | #elif defined(__GNUC__)
30 | #include
31 | inline void query_counter(timer_ticks *pTicks)
32 | {
33 | struct timeval cur_time;
34 | gettimeofday(&cur_time, NULL);
35 | *pTicks = static_cast(cur_time.tv_sec)*1000000ULL + static_cast(cur_time.tv_usec);
36 | }
37 | inline void query_counter_frequency(timer_ticks *pTicks)
38 | {
39 | *pTicks = 1000000;
40 | }
41 | #endif
42 |
43 | timer::timer() :
44 | m_start_time(0),
45 | m_stop_time(0),
46 | m_started(false),
47 | m_stopped(false)
48 | {
49 | if (!g_inv_freq)
50 | init();
51 | }
52 |
53 | timer::timer(timer_ticks start_ticks)
54 | {
55 | if (!g_inv_freq)
56 | init();
57 |
58 | m_start_time = start_ticks;
59 |
60 | m_started = true;
61 | m_stopped = false;
62 | }
63 |
64 | void timer::start(timer_ticks start_ticks)
65 | {
66 | m_start_time = start_ticks;
67 |
68 | m_started = true;
69 | m_stopped = false;
70 | }
71 |
72 | void timer::start()
73 | {
74 | query_counter(&m_start_time);
75 |
76 | m_started = true;
77 | m_stopped = false;
78 | }
79 |
80 | void timer::stop()
81 | {
82 | assert(m_started);
83 |
84 | query_counter(&m_stop_time);
85 |
86 | m_stopped = true;
87 | }
88 |
89 | double timer::get_elapsed_secs() const
90 | {
91 | assert(m_started);
92 | if (!m_started)
93 | return 0;
94 |
95 | timer_ticks stop_time = m_stop_time;
96 | if (!m_stopped)
97 | query_counter(&stop_time);
98 |
99 | timer_ticks delta = stop_time - m_start_time;
100 | return delta * g_inv_freq;
101 | }
102 |
103 | timer_ticks timer::get_elapsed_us() const
104 | {
105 | assert(m_started);
106 | if (!m_started)
107 | return 0;
108 |
109 | timer_ticks stop_time = m_stop_time;
110 | if (!m_stopped)
111 | query_counter(&stop_time);
112 |
113 | timer_ticks delta = stop_time - m_start_time;
114 | return (delta * 1000000ULL + (g_freq >> 1U)) / g_freq;
115 | }
116 |
117 | void timer::init()
118 | {
119 | if (!g_inv_freq)
120 | {
121 | query_counter_frequency(&g_freq);
122 | g_inv_freq = 1.0f / g_freq;
123 |
124 | query_counter(&g_init_ticks);
125 | }
126 | }
127 |
128 | timer_ticks timer::get_init_ticks()
129 | {
130 | if (!g_inv_freq)
131 | init();
132 |
133 | return g_init_ticks;
134 | }
135 |
136 | timer_ticks timer::get_ticks()
137 | {
138 | if (!g_inv_freq)
139 | init();
140 |
141 | timer_ticks ticks;
142 | query_counter(&ticks);
143 | return ticks - g_init_ticks;
144 | }
145 |
146 | double timer::ticks_to_secs(timer_ticks ticks)
147 | {
148 | if (!g_inv_freq)
149 | init();
150 |
151 | return ticks * g_inv_freq;
152 | }
153 |
154 |
--------------------------------------------------------------------------------
/crnlib/crn_command_line_params.h:
--------------------------------------------------------------------------------
1 | // File: crn_command_line_params.h
2 | // See Copyright Notice and license at the end of inc/crnlib.h
3 | #pragma once
4 | #include "crn_value.h"
5 | #include