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 |
11 // | 11 // |
12 // Authors: Skal (pascal.massimino@gmail.com) | 12 // Authors: Skal (pascal.massimino@gmail.com) |
13 // Urvang (urvang@google.com) | 13 // Urvang (urvang@google.com) |
14 | 14 |
15 #ifndef WEBP_UTILS_UTILS_H_ | 15 #ifndef WEBP_UTILS_UTILS_H_ |
16 #define WEBP_UTILS_UTILS_H_ | 16 #define WEBP_UTILS_UTILS_H_ |
17 | 17 |
| 18 #ifdef HAVE_CONFIG_H |
| 19 #include "../webp/config.h" |
| 20 #endif |
| 21 |
18 #include <assert.h> | 22 #include <assert.h> |
19 | 23 |
20 #include "../webp/types.h" | 24 #include "../webp/types.h" |
21 | 25 |
22 #ifdef __cplusplus | 26 #ifdef __cplusplus |
23 extern "C" { | 27 extern "C" { |
24 #endif | 28 #endif |
25 | 29 |
26 //------------------------------------------------------------------------------ | 30 //------------------------------------------------------------------------------ |
27 // Memory allocation | 31 // Memory allocation |
28 | 32 |
29 // This is the maximum memory amount that libwebp will ever try to allocate. | 33 // This is the maximum memory amount that libwebp will ever try to allocate. |
30 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40) | 34 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40) |
31 | 35 |
32 // size-checking safe malloc/calloc: verify that the requested size is not too | 36 // size-checking safe malloc/calloc: verify that the requested size is not too |
33 // large, or return NULL. You don't need to call these for constructs like | 37 // large, or return NULL. You don't need to call these for constructs like |
34 // malloc(sizeof(foo)), but only if there's picture-dependent size involved | 38 // malloc(sizeof(foo)), but only if there's picture-dependent size involved |
35 // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this | 39 // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this |
36 // safe malloc() borrows the signature from calloc(), pointing at the dangerous | 40 // safe malloc() borrows the signature from calloc(), pointing at the dangerous |
37 // underlying multiply involved. | 41 // underlying multiply involved. |
38 WEBP_EXTERN(void*) WebPSafeMalloc(uint64_t nmemb, size_t size); | 42 WEBP_EXTERN(void*) WebPSafeMalloc(uint64_t nmemb, size_t size); |
39 // Note that WebPSafeCalloc() expects the second argument type to be 'size_t' | 43 // Note that WebPSafeCalloc() expects the second argument type to be 'size_t' |
40 // in order to favor the "calloc(num_foo, sizeof(foo))" pattern. | 44 // in order to favor the "calloc(num_foo, sizeof(foo))" pattern. |
41 WEBP_EXTERN(void*) WebPSafeCalloc(uint64_t nmemb, size_t size); | 45 WEBP_EXTERN(void*) WebPSafeCalloc(uint64_t nmemb, size_t size); |
42 | 46 |
43 // Companion deallocation function to the above allocations. | 47 // Companion deallocation function to the above allocations. |
44 WEBP_EXTERN(void) WebPSafeFree(void* const ptr); | 48 WEBP_EXTERN(void) WebPSafeFree(void* const ptr); |
45 | 49 |
46 //------------------------------------------------------------------------------ | 50 //------------------------------------------------------------------------------ |
| 51 // Alignment |
| 52 |
| 53 #define WEBP_ALIGN_CST 31 |
| 54 #define WEBP_ALIGN(PTR) ((uintptr_t)((PTR) + WEBP_ALIGN_CST) & ~WEBP_ALIGN_CST) |
| 55 |
| 56 #if defined(WEBP_FORCE_ALIGNED) |
| 57 #include <string.h> |
| 58 // memcpy() is the safe way of moving potentially unaligned 32b memory. |
| 59 static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) { |
| 60 uint32_t A; |
| 61 memcpy(&A, (const int*)ptr, sizeof(A)); |
| 62 return A; |
| 63 } |
| 64 static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) { |
| 65 memcpy(ptr, &val, sizeof(val)); |
| 66 } |
| 67 #else |
| 68 static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) { |
| 69 return *(const uint32_t*)ptr; |
| 70 } |
| 71 static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) { |
| 72 *(uint32_t*)ptr = val; |
| 73 } |
| 74 #endif |
| 75 |
| 76 //------------------------------------------------------------------------------ |
47 // Reading/writing data. | 77 // Reading/writing data. |
48 | 78 |
49 // Read 16, 24 or 32 bits stored in little-endian order. | 79 // Read 16, 24 or 32 bits stored in little-endian order. |
50 static WEBP_INLINE int GetLE16(const uint8_t* const data) { | 80 static WEBP_INLINE int GetLE16(const uint8_t* const data) { |
51 return (int)(data[0] << 0) | (data[1] << 8); | 81 return (int)(data[0] << 0) | (data[1] << 8); |
52 } | 82 } |
53 | 83 |
54 static WEBP_INLINE int GetLE24(const uint8_t* const data) { | 84 static WEBP_INLINE int GetLE24(const uint8_t* const data) { |
55 return GetLE16(data) | (data[2] << 16); | 85 return GetLE16(data) | (data[2] << 16); |
56 } | 86 } |
57 | 87 |
58 static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) { | 88 static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) { |
59 return (uint32_t)GetLE16(data) | (GetLE16(data + 2) << 16); | 89 return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16); |
60 } | 90 } |
61 | 91 |
62 // Store 16, 24 or 32 bits in little-endian order. | 92 // Store 16, 24 or 32 bits in little-endian order. |
63 static WEBP_INLINE void PutLE16(uint8_t* const data, int val) { | 93 static WEBP_INLINE void PutLE16(uint8_t* const data, int val) { |
64 assert(val < (1 << 16)); | 94 assert(val < (1 << 16)); |
65 data[0] = (val >> 0); | 95 data[0] = (val >> 0); |
66 data[1] = (val >> 8); | 96 data[1] = (val >> 8); |
67 } | 97 } |
68 | 98 |
69 static WEBP_INLINE void PutLE24(uint8_t* const data, int val) { | 99 static WEBP_INLINE void PutLE24(uint8_t* const data, int val) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 if (x != 0) { | 136 if (x != 0) { |
107 value = x; | 137 value = x; |
108 log += shift; | 138 log += shift; |
109 } | 139 } |
110 } | 140 } |
111 return log; | 141 return log; |
112 } | 142 } |
113 #endif | 143 #endif |
114 | 144 |
115 //------------------------------------------------------------------------------ | 145 //------------------------------------------------------------------------------ |
| 146 // Pixel copying. |
| 147 |
| 148 struct WebPPicture; |
| 149 |
| 150 // Copy width x height pixels from 'src' to 'dst' honoring the strides. |
| 151 WEBP_EXTERN(void) WebPCopyPlane(const uint8_t* src, int src_stride, |
| 152 uint8_t* dst, int dst_stride, |
| 153 int width, int height); |
| 154 |
| 155 // Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are |
| 156 // assumed to be already allocated and using ARGB data. |
| 157 WEBP_EXTERN(void) WebPCopyPixels(const struct WebPPicture* const src, |
| 158 struct WebPPicture* const dst); |
| 159 |
| 160 //------------------------------------------------------------------------------ |
116 | 161 |
117 #ifdef __cplusplus | 162 #ifdef __cplusplus |
118 } // extern "C" | 163 } // extern "C" |
119 #endif | 164 #endif |
120 | 165 |
121 #endif /* WEBP_UTILS_UTILS_H_ */ | 166 #endif /* WEBP_UTILS_UTILS_H_ */ |
OLD | NEW |