| OLD | NEW |
| 1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
| 4 // that can be found in the COPYING file in the root of the source | 4 // that can be found in the COPYING file in the root of the source |
| 5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
| 6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
| 7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
| 8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
| 9 // | 9 // |
| 10 // Misc. common utility functions | 10 // Misc. common utility functions |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 // Companion deallocation function to the above allocations. | 56 // Companion deallocation function to the above allocations. |
| 57 WEBP_EXTERN(void) WebPSafeFree(void* const ptr); | 57 WEBP_EXTERN(void) WebPSafeFree(void* const ptr); |
| 58 | 58 |
| 59 //------------------------------------------------------------------------------ | 59 //------------------------------------------------------------------------------ |
| 60 // Alignment | 60 // Alignment |
| 61 | 61 |
| 62 #define WEBP_ALIGN_CST 31 | 62 #define WEBP_ALIGN_CST 31 |
| 63 #define WEBP_ALIGN(PTR) (((uintptr_t)(PTR) + WEBP_ALIGN_CST) & ~WEBP_ALIGN_CST) | 63 #define WEBP_ALIGN(PTR) (((uintptr_t)(PTR) + WEBP_ALIGN_CST) & ~WEBP_ALIGN_CST) |
| 64 | 64 |
| 65 #if defined(WEBP_FORCE_ALIGNED) | |
| 66 #include <string.h> | 65 #include <string.h> |
| 67 // memcpy() is the safe way of moving potentially unaligned 32b memory. | 66 // memcpy() is the safe way of moving potentially unaligned 32b memory. |
| 68 static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) { | 67 static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) { |
| 69 uint32_t A; | 68 uint32_t A; |
| 70 memcpy(&A, (const int*)ptr, sizeof(A)); | 69 memcpy(&A, (const int*)ptr, sizeof(A)); |
| 71 return A; | 70 return A; |
| 72 } | 71 } |
| 73 static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) { | 72 static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) { |
| 74 memcpy(ptr, &val, sizeof(val)); | 73 memcpy(ptr, &val, sizeof(val)); |
| 75 } | 74 } |
| 76 #else | |
| 77 static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE | |
| 78 uint32_t WebPMemToUint32(const uint8_t* const ptr) { | |
| 79 return *(const uint32_t*)ptr; | |
| 80 } | |
| 81 static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE | |
| 82 void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) { | |
| 83 *(uint32_t*)ptr = val; | |
| 84 } | |
| 85 #endif | |
| 86 | 75 |
| 87 //------------------------------------------------------------------------------ | 76 //------------------------------------------------------------------------------ |
| 88 // Reading/writing data. | 77 // Reading/writing data. |
| 89 | 78 |
| 90 // Read 16, 24 or 32 bits stored in little-endian order. | 79 // Read 16, 24 or 32 bits stored in little-endian order. |
| 91 static WEBP_INLINE int GetLE16(const uint8_t* const data) { | 80 static WEBP_INLINE int GetLE16(const uint8_t* const data) { |
| 92 return (int)(data[0] << 0) | (data[1] << 8); | 81 return (int)(data[0] << 0) | (data[1] << 8); |
| 93 } | 82 } |
| 94 | 83 |
| 95 static WEBP_INLINE int GetLE24(const uint8_t* const data) { | 84 static WEBP_INLINE int GetLE24(const uint8_t* const data) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 111 assert(val < (1 << 24)); | 100 assert(val < (1 << 24)); |
| 112 PutLE16(data, val & 0xffff); | 101 PutLE16(data, val & 0xffff); |
| 113 data[2] = (val >> 16); | 102 data[2] = (val >> 16); |
| 114 } | 103 } |
| 115 | 104 |
| 116 static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) { | 105 static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) { |
| 117 PutLE16(data, (int)(val & 0xffff)); | 106 PutLE16(data, (int)(val & 0xffff)); |
| 118 PutLE16(data + 2, (int)(val >> 16)); | 107 PutLE16(data + 2, (int)(val >> 16)); |
| 119 } | 108 } |
| 120 | 109 |
| 110 // Returns 31 ^ clz(n) = log2(n). This is the default C-implementation, either |
| 111 // based on table or not. Can be used as fallback if clz() is not available. |
| 112 #define WEBP_NEED_LOG_TABLE_8BIT |
| 113 extern const uint8_t WebPLogTable8bit[256]; |
| 114 static WEBP_INLINE int WebPLog2FloorC(uint32_t n) { |
| 115 int log = 0; |
| 116 while (n >= 256) { |
| 117 log += 8; |
| 118 n >>= 8; |
| 119 } |
| 120 return log + WebPLogTable8bit[n]; |
| 121 } |
| 122 |
| 121 // Returns (int)floor(log2(n)). n must be > 0. | 123 // Returns (int)floor(log2(n)). n must be > 0. |
| 122 // use GNU builtins where available. | 124 // use GNU builtins where available. |
| 123 #if defined(__GNUC__) && \ | 125 #if defined(__GNUC__) && \ |
| 124 ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) | 126 ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) |
| 125 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | 127 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { |
| 126 return 31 ^ __builtin_clz(n); | 128 return 31 ^ __builtin_clz(n); |
| 127 } | 129 } |
| 128 #elif defined(_MSC_VER) && _MSC_VER > 1310 && \ | 130 #elif defined(_MSC_VER) && _MSC_VER > 1310 && \ |
| 129 (defined(_M_X64) || defined(_M_IX86)) | 131 (defined(_M_X64) || defined(_M_IX86)) |
| 130 #include <intrin.h> | 132 #include <intrin.h> |
| 131 #pragma intrinsic(_BitScanReverse) | 133 #pragma intrinsic(_BitScanReverse) |
| 132 | 134 |
| 133 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | 135 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { |
| 134 unsigned long first_set_bit; | 136 unsigned long first_set_bit; |
| 135 _BitScanReverse(&first_set_bit, n); | 137 _BitScanReverse(&first_set_bit, n); |
| 136 return first_set_bit; | 138 return first_set_bit; |
| 137 } | 139 } |
| 138 #else | 140 #else // default: use the C-version. |
| 139 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | 141 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { return WebPLog2FloorC(n); } |
| 140 int log = 0; | |
| 141 uint32_t value = n; | |
| 142 int i; | |
| 143 | |
| 144 for (i = 4; i >= 0; --i) { | |
| 145 const int shift = (1 << i); | |
| 146 const uint32_t x = value >> shift; | |
| 147 if (x != 0) { | |
| 148 value = x; | |
| 149 log += shift; | |
| 150 } | |
| 151 } | |
| 152 return log; | |
| 153 } | |
| 154 #endif | 142 #endif |
| 155 | 143 |
| 156 //------------------------------------------------------------------------------ | 144 //------------------------------------------------------------------------------ |
| 157 // Pixel copying. | 145 // Pixel copying. |
| 158 | 146 |
| 159 struct WebPPicture; | 147 struct WebPPicture; |
| 160 | 148 |
| 161 // Copy width x height pixels from 'src' to 'dst' honoring the strides. | 149 // Copy width x height pixels from 'src' to 'dst' honoring the strides. |
| 162 WEBP_EXTERN(void) WebPCopyPlane(const uint8_t* src, int src_stride, | 150 WEBP_EXTERN(void) WebPCopyPlane(const uint8_t* src, int src_stride, |
| 163 uint8_t* dst, int dst_stride, | 151 uint8_t* dst, int dst_stride, |
| 164 int width, int height); | 152 int width, int height); |
| 165 | 153 |
| 166 // Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are | 154 // Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are |
| 167 // assumed to be already allocated and using ARGB data. | 155 // assumed to be already allocated and using ARGB data. |
| 168 WEBP_EXTERN(void) WebPCopyPixels(const struct WebPPicture* const src, | 156 WEBP_EXTERN(void) WebPCopyPixels(const struct WebPPicture* const src, |
| 169 struct WebPPicture* const dst); | 157 struct WebPPicture* const dst); |
| 170 | 158 |
| 171 //------------------------------------------------------------------------------ | 159 //------------------------------------------------------------------------------ |
| 172 // Unique colors. | 160 // Unique colors. |
| 173 | 161 |
| 174 // Returns count of unique colors in 'pic', assuming pic->use_argb is true. | 162 // Returns count of unique colors in 'pic', assuming pic->use_argb is true. |
| 175 // If the unique color count is more than MAX_COLOR_COUNT, returns | 163 // If the unique color count is more than MAX_PALETTE_SIZE, returns |
| 176 // MAX_COLOR_COUNT+1. | 164 // MAX_PALETTE_SIZE+1. |
| 177 // If 'palette' is not NULL and number of unique colors is less than or equal to | 165 // If 'palette' is not NULL and number of unique colors is less than or equal to |
| 178 // MAX_COLOR_COUNT, also outputs the actual unique colors into 'palette'. | 166 // MAX_PALETTE_SIZE, also outputs the actual unique colors into 'palette'. |
| 179 // Note: 'palette' is assumed to be an array already allocated with at least | 167 // Note: 'palette' is assumed to be an array already allocated with at least |
| 180 // MAX_COLOR_COUNT elements. | 168 // MAX_PALETTE_SIZE elements. |
| 181 WEBP_EXTERN(int) WebPGetColorPalette(const struct WebPPicture* const pic, | 169 WEBP_EXTERN(int) WebPGetColorPalette(const struct WebPPicture* const pic, |
| 182 uint32_t* const palette); | 170 uint32_t* const palette); |
| 183 | 171 |
| 184 //------------------------------------------------------------------------------ | 172 //------------------------------------------------------------------------------ |
| 185 | 173 |
| 186 #ifdef __cplusplus | 174 #ifdef __cplusplus |
| 187 } // extern "C" | 175 } // extern "C" |
| 188 #endif | 176 #endif |
| 189 | 177 |
| 190 #endif /* WEBP_UTILS_UTILS_H_ */ | 178 #endif /* WEBP_UTILS_UTILS_H_ */ |
| OLD | NEW |