| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef WEBP_UTILS_UTILS_H_ |
| 16 |
#define WEBP_UTILS_UTILS_H_ |
| 17 |
|
| 18 |
#ifdef HAVE_CONFIG_H |
| 19 |
#include "src/webp/config.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <assert.h> |
| 23 |
|
| 24 |
#include "src/webp/types.h" |
| 25 |
|
| 26 |
#ifdef __cplusplus |
| 27 |
extern "C" { |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#ifndef WEBP_MAX_ALLOCABLE_MEMORY |
| 35 |
#if SIZE_MAX > (1ULL << 34) |
| 36 |
#define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 34) |
| 37 |
#else |
| 38 |
|
| 39 |
#define WEBP_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16)) |
| 40 |
#endif |
| 41 |
#endif |
| 42 |
|
| 43 |
static WEBP_INLINE int CheckSizeOverflow(uint64_t size) { |
| 44 |
return size == (size_t)size; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
WEBP_EXTERN void* WebPSafeMalloc(uint64_t nmemb, size_t size); |
| 54 |
|
| 55 |
|
| 56 |
WEBP_EXTERN void* WebPSafeCalloc(uint64_t nmemb, size_t size); |
| 57 |
|
| 58 |
|
| 59 |
WEBP_EXTERN void WebPSafeFree(void* const ptr); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#define WEBP_ALIGN_CST 31 |
| 65 |
#define WEBP_ALIGN(PTR) (((uintptr_t)(PTR) + WEBP_ALIGN_CST) & \ |
| 66 |
~(uintptr_t)WEBP_ALIGN_CST) |
| 67 |
|
| 68 |
#include <string.h> |
| 69 |
|
| 70 |
static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) { |
| 71 |
uint32_t A; |
| 72 |
memcpy(&A, ptr, sizeof(A)); |
| 73 |
return A; |
| 74 |
} |
| 75 |
|
| 76 |
static WEBP_INLINE int32_t WebPMemToInt32(const uint8_t* const ptr) { |
| 77 |
return (int32_t)WebPMemToUint32(ptr); |
| 78 |
} |
| 79 |
|
| 80 |
static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) { |
| 81 |
memcpy(ptr, &val, sizeof(val)); |
| 82 |
} |
| 83 |
|
| 84 |
static WEBP_INLINE void WebPInt32ToMem(uint8_t* const ptr, int val) { |
| 85 |
WebPUint32ToMem(ptr, (uint32_t)val); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static WEBP_INLINE int GetLE16(const uint8_t* const data) { |
| 93 |
return (int)(data[0] << 0) | (data[1] << 8); |
| 94 |
} |
| 95 |
|
| 96 |
static WEBP_INLINE int GetLE24(const uint8_t* const data) { |
| 97 |
return GetLE16(data) | (data[2] << 16); |
| 98 |
} |
| 99 |
|
| 100 |
static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) { |
| 101 |
return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
static WEBP_INLINE void PutLE16(uint8_t* const data, int val) { |
| 106 |
assert(val < (1 << 16)); |
| 107 |
data[0] = (val >> 0) & 0xff; |
| 108 |
data[1] = (val >> 8) & 0xff; |
| 109 |
} |
| 110 |
|
| 111 |
static WEBP_INLINE void PutLE24(uint8_t* const data, int val) { |
| 112 |
assert(val < (1 << 24)); |
| 113 |
PutLE16(data, val & 0xffff); |
| 114 |
data[2] = (val >> 16) & 0xff; |
| 115 |
} |
| 116 |
|
| 117 |
static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) { |
| 118 |
PutLE16(data, (int)(val & 0xffff)); |
| 119 |
PutLE16(data + 2, (int)(val >> 16)); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
#if defined(__GNUC__) && \ |
| 124 |
((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) |
| 125 |
|
| 126 |
static WEBP_INLINE int BitsLog2Floor(uint32_t n) { |
| 127 |
return 31 ^ __builtin_clz(n); |
| 128 |
} |
| 129 |
|
| 130 |
static WEBP_INLINE int BitsCtz(uint32_t n) { return __builtin_ctz(n); } |
| 131 |
#elif defined(_MSC_VER) && _MSC_VER > 1310 && \ |
| 132 |
(defined(_M_X64) || defined(_M_IX86)) |
| 133 |
#include <intrin.h> |
| 134 |
#pragma intrinsic(_BitScanReverse) |
| 135 |
#pragma intrinsic(_BitScanForward) |
| 136 |
|
| 137 |
static WEBP_INLINE int BitsLog2Floor(uint32_t n) { |
| 138 |
unsigned long first_set_bit; |
| 139 |
_BitScanReverse(&first_set_bit, n); |
| 140 |
return first_set_bit; |
| 141 |
} |
| 142 |
static WEBP_INLINE int BitsCtz(uint32_t n) { |
| 143 |
unsigned long first_set_bit; |
| 144 |
_BitScanForward(&first_set_bit, n); |
| 145 |
return first_set_bit; |
| 146 |
} |
| 147 |
#else |
| 148 |
#define WEBP_HAVE_SLOW_CLZ_CTZ |
| 149 |
|
| 150 |
|
| 151 |
#define WEBP_NEED_LOG_TABLE_8BIT |
| 152 |
extern const uint8_t WebPLogTable8bit[256]; |
| 153 |
static WEBP_INLINE int WebPLog2FloorC(uint32_t n) { |
| 154 |
int log_value = 0; |
| 155 |
while (n >= 256) { |
| 156 |
log_value += 8; |
| 157 |
n >>= 8; |
| 158 |
} |
| 159 |
return log_value + WebPLogTable8bit[n]; |
| 160 |
} |
| 161 |
|
| 162 |
static WEBP_INLINE int BitsLog2Floor(uint32_t n) { return WebPLog2FloorC(n); } |
| 163 |
|
| 164 |
static WEBP_INLINE int BitsCtz(uint32_t n) { |
| 165 |
int i; |
| 166 |
for (i = 0; i < 32; ++i, n >>= 1) { |
| 167 |
if (n & 1) return i; |
| 168 |
} |
| 169 |
return 32; |
| 170 |
} |
| 171 |
|
| 172 |
#endif |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
struct WebPPicture; |
| 178 |
|
| 179 |
|
| 180 |
WEBP_EXTERN void WebPCopyPlane(const uint8_t* src, int src_stride, |
| 181 |
uint8_t* dst, int dst_stride, |
| 182 |
int width, int height); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
WEBP_EXTERN void WebPCopyPixels(const struct WebPPicture* const src, |
| 187 |
struct WebPPicture* const dst); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
WEBP_EXTERN int WebPGetColorPalette(const struct WebPPicture* const pic, |
| 201 |
uint32_t* const palette); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
#ifdef __cplusplus |
| 206 |
} |
| 207 |
#endif |
| 208 |
|
| 209 |
#endif |