utils.h 6.7 KB

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