OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // This code is licensed under the same terms as WebM: | 3 // This code is licensed under the same terms as WebM: |
4 // Software License Agreement: http://www.webmproject.org/license/software/ | 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ | 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
6 // ----------------------------------------------------------------------------- | 6 // ----------------------------------------------------------------------------- |
7 // | 7 // |
8 // Misc. common utility functions | 8 // Misc. common utility functions |
9 // | 9 // |
10 // Author: Skal (pascal.massimino@gmail.com) | 10 // Authors: Skal (pascal.massimino@gmail.com) |
| 11 // Urvang (urvang@google.com) |
11 | 12 |
12 #ifndef WEBP_UTILS_UTILS_H_ | 13 #ifndef WEBP_UTILS_UTILS_H_ |
13 #define WEBP_UTILS_UTILS_H_ | 14 #define WEBP_UTILS_UTILS_H_ |
14 | 15 |
| 16 #include <assert.h> |
| 17 |
15 #include "../webp/types.h" | 18 #include "../webp/types.h" |
16 | 19 |
17 #if defined(__cplusplus) || defined(c_plusplus) | 20 #if defined(__cplusplus) || defined(c_plusplus) |
18 extern "C" { | 21 extern "C" { |
19 #endif | 22 #endif |
20 | 23 |
21 //------------------------------------------------------------------------------ | 24 //------------------------------------------------------------------------------ |
22 // Memory allocation | 25 // Memory allocation |
23 | 26 |
24 // This is the maximum memory amount that libwebp will ever try to allocate. | 27 // This is the maximum memory amount that libwebp will ever try to allocate. |
25 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40) | 28 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40) |
26 | 29 |
27 // size-checking safe malloc/calloc: verify that the requested size is not too | 30 // size-checking safe malloc/calloc: verify that the requested size is not too |
28 // large, or return NULL. You don't need to call these for constructs like | 31 // large, or return NULL. You don't need to call these for constructs like |
29 // malloc(sizeof(foo)), but only if there's picture-dependent size involved | 32 // malloc(sizeof(foo)), but only if there's picture-dependent size involved |
30 // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this | 33 // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this |
31 // safe malloc() borrows the signature from calloc(), pointing at the dangerous | 34 // safe malloc() borrows the signature from calloc(), pointing at the dangerous |
32 // underlying multiply involved. | 35 // underlying multiply involved. |
33 void* WebPSafeMalloc(uint64_t nmemb, size_t size); | 36 void* WebPSafeMalloc(uint64_t nmemb, size_t size); |
34 // Note that WebPSafeCalloc() expects the second argument type to be 'size_t' | 37 // Note that WebPSafeCalloc() expects the second argument type to be 'size_t' |
35 // in order to favor the "calloc(num_foo, sizeof(foo))" pattern. | 38 // in order to favor the "calloc(num_foo, sizeof(foo))" pattern. |
36 void* WebPSafeCalloc(uint64_t nmemb, size_t size); | 39 void* WebPSafeCalloc(uint64_t nmemb, size_t size); |
37 | 40 |
38 //------------------------------------------------------------------------------ | 41 //------------------------------------------------------------------------------ |
| 42 // Reading/writing data. |
| 43 |
| 44 // Read 16, 24 or 32 bits stored in little-endian order. |
| 45 static WEBP_INLINE int GetLE16(const uint8_t* const data) { |
| 46 return (int)(data[0] << 0) | (data[1] << 8); |
| 47 } |
| 48 |
| 49 static WEBP_INLINE int GetLE24(const uint8_t* const data) { |
| 50 return GetLE16(data) | (data[2] << 16); |
| 51 } |
| 52 |
| 53 static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) { |
| 54 return (uint32_t)GetLE16(data) | (GetLE16(data + 2) << 16); |
| 55 } |
| 56 |
| 57 // Store 16, 24 or 32 bits in little-endian order. |
| 58 static WEBP_INLINE void PutLE16(uint8_t* const data, int val) { |
| 59 assert(val < (1 << 16)); |
| 60 data[0] = (val >> 0); |
| 61 data[1] = (val >> 8); |
| 62 } |
| 63 |
| 64 static WEBP_INLINE void PutLE24(uint8_t* const data, int val) { |
| 65 assert(val < (1 << 24)); |
| 66 PutLE16(data, val & 0xffff); |
| 67 data[2] = (val >> 16); |
| 68 } |
| 69 |
| 70 static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) { |
| 71 PutLE16(data, (int)(val & 0xffff)); |
| 72 PutLE16(data + 2, (int)(val >> 16)); |
| 73 } |
| 74 |
| 75 //------------------------------------------------------------------------------ |
39 | 76 |
40 #if defined(__cplusplus) || defined(c_plusplus) | 77 #if defined(__cplusplus) || defined(c_plusplus) |
41 } // extern "C" | 78 } // extern "C" |
42 #endif | 79 #endif |
43 | 80 |
44 #endif /* WEBP_UTILS_UTILS_H_ */ | 81 #endif /* WEBP_UTILS_UTILS_H_ */ |
OLD | NEW |