1*b2055c35SXin Li // Copyright 2012 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // Misc. common utility functions
11*b2055c35SXin Li //
12*b2055c35SXin Li // Authors: Skal ([email protected])
13*b2055c35SXin Li // Urvang ([email protected])
14*b2055c35SXin Li
15*b2055c35SXin Li #ifndef WEBP_UTILS_UTILS_H_
16*b2055c35SXin Li #define WEBP_UTILS_UTILS_H_
17*b2055c35SXin Li
18*b2055c35SXin Li #ifdef HAVE_CONFIG_H
19*b2055c35SXin Li #include "src/webp/config.h"
20*b2055c35SXin Li #endif
21*b2055c35SXin Li
22*b2055c35SXin Li #include <assert.h>
23*b2055c35SXin Li
24*b2055c35SXin Li #include "src/webp/types.h"
25*b2055c35SXin Li
26*b2055c35SXin Li #ifdef __cplusplus
27*b2055c35SXin Li extern "C" {
28*b2055c35SXin Li #endif
29*b2055c35SXin Li
30*b2055c35SXin Li //------------------------------------------------------------------------------
31*b2055c35SXin Li // Memory allocation
32*b2055c35SXin Li
33*b2055c35SXin Li // This is the maximum memory amount that libwebp will ever try to allocate.
34*b2055c35SXin Li #ifndef WEBP_MAX_ALLOCABLE_MEMORY
35*b2055c35SXin Li #if SIZE_MAX > (1ULL << 34)
36*b2055c35SXin Li #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 34)
37*b2055c35SXin Li #else
38*b2055c35SXin Li // For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.
39*b2055c35SXin Li #define WEBP_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))
40*b2055c35SXin Li #endif
41*b2055c35SXin Li #endif // WEBP_MAX_ALLOCABLE_MEMORY
42*b2055c35SXin Li
CheckSizeOverflow(uint64_t size)43*b2055c35SXin Li static WEBP_INLINE int CheckSizeOverflow(uint64_t size) {
44*b2055c35SXin Li return size == (size_t)size;
45*b2055c35SXin Li }
46*b2055c35SXin Li
47*b2055c35SXin Li // size-checking safe malloc/calloc: verify that the requested size is not too
48*b2055c35SXin Li // large, or return NULL. You don't need to call these for constructs like
49*b2055c35SXin Li // malloc(sizeof(foo)), but only if there's picture-dependent size involved
50*b2055c35SXin Li // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this
51*b2055c35SXin Li // safe malloc() borrows the signature from calloc(), pointing at the dangerous
52*b2055c35SXin Li // underlying multiply involved.
53*b2055c35SXin Li WEBP_EXTERN void* WebPSafeMalloc(uint64_t nmemb, size_t size);
54*b2055c35SXin Li // Note that WebPSafeCalloc() expects the second argument type to be 'size_t'
55*b2055c35SXin Li // in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
56*b2055c35SXin Li WEBP_EXTERN void* WebPSafeCalloc(uint64_t nmemb, size_t size);
57*b2055c35SXin Li
58*b2055c35SXin Li // Companion deallocation function to the above allocations.
59*b2055c35SXin Li WEBP_EXTERN void WebPSafeFree(void* const ptr);
60*b2055c35SXin Li
61*b2055c35SXin Li //------------------------------------------------------------------------------
62*b2055c35SXin Li // Alignment
63*b2055c35SXin Li
64*b2055c35SXin Li #define WEBP_ALIGN_CST 31
65*b2055c35SXin Li #define WEBP_ALIGN(PTR) (((uintptr_t)(PTR) + WEBP_ALIGN_CST) & \
66*b2055c35SXin Li ~(uintptr_t)WEBP_ALIGN_CST)
67*b2055c35SXin Li
68*b2055c35SXin Li #include <string.h>
69*b2055c35SXin Li // memcpy() is the safe way of moving potentially unaligned 32b memory.
WebPMemToUint32(const uint8_t * const ptr)70*b2055c35SXin Li static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {
71*b2055c35SXin Li uint32_t A;
72*b2055c35SXin Li memcpy(&A, ptr, sizeof(A));
73*b2055c35SXin Li return A;
74*b2055c35SXin Li }
75*b2055c35SXin Li
WebPMemToInt32(const uint8_t * const ptr)76*b2055c35SXin Li static WEBP_INLINE int32_t WebPMemToInt32(const uint8_t* const ptr) {
77*b2055c35SXin Li return (int32_t)WebPMemToUint32(ptr);
78*b2055c35SXin Li }
79*b2055c35SXin Li
WebPUint32ToMem(uint8_t * const ptr,uint32_t val)80*b2055c35SXin Li static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {
81*b2055c35SXin Li memcpy(ptr, &val, sizeof(val));
82*b2055c35SXin Li }
83*b2055c35SXin Li
WebPInt32ToMem(uint8_t * const ptr,int val)84*b2055c35SXin Li static WEBP_INLINE void WebPInt32ToMem(uint8_t* const ptr, int val) {
85*b2055c35SXin Li WebPUint32ToMem(ptr, (uint32_t)val);
86*b2055c35SXin Li }
87*b2055c35SXin Li
88*b2055c35SXin Li //------------------------------------------------------------------------------
89*b2055c35SXin Li // Reading/writing data.
90*b2055c35SXin Li
91*b2055c35SXin Li // Read 16, 24 or 32 bits stored in little-endian order.
GetLE16(const uint8_t * const data)92*b2055c35SXin Li static WEBP_INLINE int GetLE16(const uint8_t* const data) {
93*b2055c35SXin Li return (int)(data[0] << 0) | (data[1] << 8);
94*b2055c35SXin Li }
95*b2055c35SXin Li
GetLE24(const uint8_t * const data)96*b2055c35SXin Li static WEBP_INLINE int GetLE24(const uint8_t* const data) {
97*b2055c35SXin Li return GetLE16(data) | (data[2] << 16);
98*b2055c35SXin Li }
99*b2055c35SXin Li
GetLE32(const uint8_t * const data)100*b2055c35SXin Li static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
101*b2055c35SXin Li return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16);
102*b2055c35SXin Li }
103*b2055c35SXin Li
104*b2055c35SXin Li // Store 16, 24 or 32 bits in little-endian order.
PutLE16(uint8_t * const data,int val)105*b2055c35SXin Li static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
106*b2055c35SXin Li assert(val < (1 << 16));
107*b2055c35SXin Li data[0] = (val >> 0) & 0xff;
108*b2055c35SXin Li data[1] = (val >> 8) & 0xff;
109*b2055c35SXin Li }
110*b2055c35SXin Li
PutLE24(uint8_t * const data,int val)111*b2055c35SXin Li static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
112*b2055c35SXin Li assert(val < (1 << 24));
113*b2055c35SXin Li PutLE16(data, val & 0xffff);
114*b2055c35SXin Li data[2] = (val >> 16) & 0xff;
115*b2055c35SXin Li }
116*b2055c35SXin Li
PutLE32(uint8_t * const data,uint32_t val)117*b2055c35SXin Li static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
118*b2055c35SXin Li PutLE16(data, (int)(val & 0xffff));
119*b2055c35SXin Li PutLE16(data + 2, (int)(val >> 16));
120*b2055c35SXin Li }
121*b2055c35SXin Li
122*b2055c35SXin Li // use GNU builtins where available.
123*b2055c35SXin Li #if defined(__GNUC__) && \
124*b2055c35SXin Li ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
125*b2055c35SXin Li // Returns (int)floor(log2(n)). n must be > 0.
BitsLog2Floor(uint32_t n)126*b2055c35SXin Li static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
127*b2055c35SXin Li return 31 ^ __builtin_clz(n);
128*b2055c35SXin Li }
129*b2055c35SXin Li // counts the number of trailing zero
BitsCtz(uint32_t n)130*b2055c35SXin Li static WEBP_INLINE int BitsCtz(uint32_t n) { return __builtin_ctz(n); }
131*b2055c35SXin Li #elif defined(_MSC_VER) && _MSC_VER > 1310 && \
132*b2055c35SXin Li (defined(_M_X64) || defined(_M_IX86))
133*b2055c35SXin Li #include <intrin.h>
134*b2055c35SXin Li #pragma intrinsic(_BitScanReverse)
135*b2055c35SXin Li #pragma intrinsic(_BitScanForward)
136*b2055c35SXin Li
BitsLog2Floor(uint32_t n)137*b2055c35SXin Li static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
138*b2055c35SXin Li unsigned long first_set_bit; // NOLINT (runtime/int)
139*b2055c35SXin Li _BitScanReverse(&first_set_bit, n);
140*b2055c35SXin Li return first_set_bit;
141*b2055c35SXin Li }
BitsCtz(uint32_t n)142*b2055c35SXin Li static WEBP_INLINE int BitsCtz(uint32_t n) {
143*b2055c35SXin Li unsigned long first_set_bit; // NOLINT (runtime/int)
144*b2055c35SXin Li _BitScanForward(&first_set_bit, n);
145*b2055c35SXin Li return first_set_bit;
146*b2055c35SXin Li }
147*b2055c35SXin Li #else // default: use the (slow) C-version.
148*b2055c35SXin Li #define WEBP_HAVE_SLOW_CLZ_CTZ // signal that the Clz/Ctz function are slow
149*b2055c35SXin Li // Returns 31 ^ clz(n) = log2(n). This is the default C-implementation, either
150*b2055c35SXin Li // based on table or not. Can be used as fallback if clz() is not available.
151*b2055c35SXin Li #define WEBP_NEED_LOG_TABLE_8BIT
152*b2055c35SXin Li extern const uint8_t WebPLogTable8bit[256];
WebPLog2FloorC(uint32_t n)153*b2055c35SXin Li static WEBP_INLINE int WebPLog2FloorC(uint32_t n) {
154*b2055c35SXin Li int log_value = 0;
155*b2055c35SXin Li while (n >= 256) {
156*b2055c35SXin Li log_value += 8;
157*b2055c35SXin Li n >>= 8;
158*b2055c35SXin Li }
159*b2055c35SXin Li return log_value + WebPLogTable8bit[n];
160*b2055c35SXin Li }
161*b2055c35SXin Li
BitsLog2Floor(uint32_t n)162*b2055c35SXin Li static WEBP_INLINE int BitsLog2Floor(uint32_t n) { return WebPLog2FloorC(n); }
163*b2055c35SXin Li
BitsCtz(uint32_t n)164*b2055c35SXin Li static WEBP_INLINE int BitsCtz(uint32_t n) {
165*b2055c35SXin Li int i;
166*b2055c35SXin Li for (i = 0; i < 32; ++i, n >>= 1) {
167*b2055c35SXin Li if (n & 1) return i;
168*b2055c35SXin Li }
169*b2055c35SXin Li return 32;
170*b2055c35SXin Li }
171*b2055c35SXin Li
172*b2055c35SXin Li #endif
173*b2055c35SXin Li
174*b2055c35SXin Li //------------------------------------------------------------------------------
175*b2055c35SXin Li // Pixel copying.
176*b2055c35SXin Li
177*b2055c35SXin Li struct WebPPicture;
178*b2055c35SXin Li
179*b2055c35SXin Li // Copy width x height pixels from 'src' to 'dst' honoring the strides.
180*b2055c35SXin Li WEBP_EXTERN void WebPCopyPlane(const uint8_t* src, int src_stride,
181*b2055c35SXin Li uint8_t* dst, int dst_stride,
182*b2055c35SXin Li int width, int height);
183*b2055c35SXin Li
184*b2055c35SXin Li // Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are
185*b2055c35SXin Li // assumed to be already allocated and using ARGB data.
186*b2055c35SXin Li WEBP_EXTERN void WebPCopyPixels(const struct WebPPicture* const src,
187*b2055c35SXin Li struct WebPPicture* const dst);
188*b2055c35SXin Li
189*b2055c35SXin Li //------------------------------------------------------------------------------
190*b2055c35SXin Li // Unique colors.
191*b2055c35SXin Li
192*b2055c35SXin Li // Returns count of unique colors in 'pic', assuming pic->use_argb is true.
193*b2055c35SXin Li // If the unique color count is more than MAX_PALETTE_SIZE, returns
194*b2055c35SXin Li // MAX_PALETTE_SIZE+1.
195*b2055c35SXin Li // If 'palette' is not NULL and number of unique colors is less than or equal to
196*b2055c35SXin Li // MAX_PALETTE_SIZE, also outputs the actual unique colors into 'palette'.
197*b2055c35SXin Li // Note: 'palette' is assumed to be an array already allocated with at least
198*b2055c35SXin Li // MAX_PALETTE_SIZE elements.
199*b2055c35SXin Li // TODO(vrabaud) remove whenever we can break the ABI.
200*b2055c35SXin Li WEBP_EXTERN int WebPGetColorPalette(const struct WebPPicture* const pic,
201*b2055c35SXin Li uint32_t* const palette);
202*b2055c35SXin Li
203*b2055c35SXin Li //------------------------------------------------------------------------------
204*b2055c35SXin Li
205*b2055c35SXin Li #ifdef __cplusplus
206*b2055c35SXin Li } // extern "C"
207*b2055c35SXin Li #endif
208*b2055c35SXin Li
209*b2055c35SXin Li #endif // WEBP_UTILS_UTILS_H_
210