| 1 | /* |
| 2 | * Texture compression |
| 3 | * Version: 1.0 |
| 4 | * |
| 5 | * Copyright (C) 2004 Daniel Borca All Rights Reserved. |
| 6 | * |
| 7 | * this is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * this is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Make; see the file COPYING. If not, write to |
| 19 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #ifndef INTERNAL_H_included |
| 24 | #define INTERNAL_H_included |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | |
| 28 | /*****************************************************************************\ |
| 29 | * DLL stuff |
| 30 | \*****************************************************************************/ |
| 31 | |
| 32 | #ifdef __WIN32__ |
| 33 | #define TAPI __declspec(dllexport) |
| 34 | #define TAPIENTRY /*__stdcall*/ |
| 35 | #else |
| 36 | #define TAPI |
| 37 | #define TAPIENTRY |
| 38 | #endif |
| 39 | |
| 40 | |
| 41 | /*****************************************************************************\ |
| 42 | * 64bit types on 32bit machine |
| 43 | \*****************************************************************************/ |
| 44 | |
| 45 | /* |
| 46 | * Define a 64-bit unsigned integer type and macros |
| 47 | */ |
| 48 | #if 1 |
| 49 | |
| 50 | #define Q_NATIVE 1 |
| 51 | |
| 52 | typedef uint64_t qword; |
| 53 | |
| 54 | #define Q_MOV32(a, b) a = b |
| 55 | #define Q_OR32(a, b) a |= b |
| 56 | #define Q_SHL(a, c) a <<= c |
| 57 | |
| 58 | #else |
| 59 | |
| 60 | #define Q_NATIVE 0 |
| 61 | |
| 62 | typedef struct { |
| 63 | dword lo, hi; |
| 64 | } qword; |
| 65 | |
| 66 | #define Q_MOV32(a, b) a.lo = b |
| 67 | #define Q_OR32(a, b) a.lo |= b |
| 68 | |
| 69 | #define Q_SHL(a, c) \ |
| 70 | do { \ |
| 71 | if ((c) >= 32) { \ |
| 72 | a.hi = a.lo << ((c) - 32); \ |
| 73 | a.lo = 0; \ |
| 74 | } else { \ |
| 75 | a.hi = (a.hi << (c)) | (a.lo >> (32 - (c))); \ |
| 76 | a.lo <<= (c); \ |
| 77 | } \ |
| 78 | } while (0) |
| 79 | |
| 80 | #endif |
| 81 | |
| 82 | |
| 83 | /*****************************************************************************\ |
| 84 | * Config |
| 85 | \*****************************************************************************/ |
| 86 | |
| 87 | #define RCOMP 0 |
| 88 | #define GCOMP 1 |
| 89 | #define BCOMP 2 |
| 90 | #define ACOMP 3 |
| 91 | |
| 92 | /*****************************************************************************\ |
| 93 | * Metric |
| 94 | \*****************************************************************************/ |
| 95 | |
| 96 | #define F(i) (float)1 /* can be used to obtain an oblong metric: 0.30 / 0.59 / 0.11 */ |
| 97 | #define SAFECDOT 1 /* for paranoids */ |
| 98 | |
| 99 | #define MAKEIVEC(NV, NC, IV, B, V0, V1) \ |
| 100 | do { \ |
| 101 | /* compute interpolation vector */ \ |
| 102 | float d2 = 0.0F; \ |
| 103 | float rd2; \ |
| 104 | \ |
| 105 | for (i = 0; i < NC; i++) { \ |
| 106 | IV[i] = (V1[i] - V0[i]) * F(i); \ |
| 107 | d2 += IV[i] * IV[i]; \ |
| 108 | } \ |
| 109 | rd2 = (float)NV / d2; \ |
| 110 | B = 0; \ |
| 111 | for (i = 0; i < NC; i++) { \ |
| 112 | IV[i] *= F(i); \ |
| 113 | B -= IV[i] * V0[i]; \ |
| 114 | IV[i] *= rd2; \ |
| 115 | } \ |
| 116 | B = B * rd2 + 0.5f; \ |
| 117 | } while (0) |
| 118 | |
| 119 | #define CALCCDOT(TEXEL, NV, NC, IV, B, V)\ |
| 120 | do { \ |
| 121 | float dot = 0.0F; \ |
| 122 | for (i = 0; i < NC; i++) { \ |
| 123 | dot += V[i] * IV[i]; \ |
| 124 | } \ |
| 125 | TEXEL = (int)(dot + B); \ |
| 126 | if (SAFECDOT) { \ |
| 127 | if (TEXEL < 0) { \ |
| 128 | TEXEL = 0; \ |
| 129 | } else if (TEXEL > NV) { \ |
| 130 | TEXEL = NV; \ |
| 131 | } \ |
| 132 | } \ |
| 133 | } while (0) |
| 134 | |
| 135 | |
| 136 | /*****************************************************************************\ |
| 137 | * Utility functions |
| 138 | \*****************************************************************************/ |
| 139 | |
| 140 | /** Copy a 4-element vector */ |
| 141 | #define COPY_4V( DST, SRC ) \ |
| 142 | do { \ |
| 143 | (DST)[0] = (SRC)[0]; \ |
| 144 | (DST)[1] = (SRC)[1]; \ |
| 145 | (DST)[2] = (SRC)[2]; \ |
| 146 | (DST)[3] = (SRC)[3]; \ |
| 147 | } while (0) |
| 148 | |
| 149 | /** Copy a 4-element unsigned byte vector */ |
| 150 | static inline void |
| 151 | COPY_4UBV(uint8_t dst[4], const uint8_t src[4]) |
| 152 | { |
| 153 | #if defined(__i386__) |
| 154 | *((uint32_t *) dst) = *((uint32_t *) src); |
| 155 | #else |
| 156 | /* The uint32_t cast might fail if DST or SRC are not dword-aligned (RISC) */ |
| 157 | COPY_4V(dst, src); |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | void reorder_source_3(byte *tex, dword width, dword height, int srcRowStride); |
| 162 | void *reorder_source_3_alloc(const byte *source, dword width, dword height, int srcRowStride); |
| 163 | void reorder_source_4(byte *tex, dword width, dword height, int srcRowStride); |
| 164 | void *reorder_source_4_alloc(const byte *source, dword width, dword height, int srcRowStride); |
| 165 | |
| 166 | #endif |