utils.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 ([email protected])
  13. // Urvang ([email protected])
  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. // size-checking safe malloc/calloc: verify that the requested size is not too
  38. // large, or return NULL. You don't need to call these for constructs like
  39. // malloc(sizeof(foo)), but only if there's picture-dependent size involved
  40. // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this
  41. // safe malloc() borrows the signature from calloc(), pointing at the dangerous
  42. // underlying multiply involved.
  43. WEBP_EXTERN(void*) WebPSafeMalloc(uint64_t nmemb, size_t size);
  44. // Note that WebPSafeCalloc() expects the second argument type to be 'size_t'
  45. // in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
  46. WEBP_EXTERN(void*) WebPSafeCalloc(uint64_t nmemb, size_t size);
  47. // Companion deallocation function to the above allocations.
  48. WEBP_EXTERN(void) WebPSafeFree(void* const ptr);
  49. //------------------------------------------------------------------------------
  50. // Alignment
  51. #define WEBP_ALIGN_CST 31
  52. #define WEBP_ALIGN(PTR) (((uintptr_t)(PTR) + WEBP_ALIGN_CST) & ~WEBP_ALIGN_CST)
  53. #if defined(WEBP_FORCE_ALIGNED)
  54. #include <string.h>
  55. // memcpy() is the safe way of moving potentially unaligned 32b memory.
  56. static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {
  57. uint32_t A;
  58. memcpy(&A, (const int*)ptr, sizeof(A));
  59. return A;
  60. }
  61. static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {
  62. memcpy(ptr, &val, sizeof(val));
  63. }
  64. #else
  65. static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE
  66. uint32_t WebPMemToUint32(const uint8_t* const ptr) {
  67. return *(const uint32_t*)ptr;
  68. }
  69. static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE
  70. void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {
  71. *(uint32_t*)ptr = val;
  72. }
  73. #endif
  74. //------------------------------------------------------------------------------
  75. // Reading/writing data.
  76. // Read 16, 24 or 32 bits stored in little-endian order.
  77. static WEBP_INLINE int GetLE16(const uint8_t* const data) {
  78. return (int)(data[0] << 0) | (data[1] << 8);
  79. }
  80. static WEBP_INLINE int GetLE24(const uint8_t* const data) {
  81. return GetLE16(data) | (data[2] << 16);
  82. }
  83. static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
  84. return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16);
  85. }
  86. // Store 16, 24 or 32 bits in little-endian order.
  87. static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
  88. assert(val < (1 << 16));
  89. data[0] = (val >> 0);
  90. data[1] = (val >> 8);
  91. }
  92. static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
  93. assert(val < (1 << 24));
  94. PutLE16(data, val & 0xffff);
  95. data[2] = (val >> 16);
  96. }
  97. static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
  98. PutLE16(data, (int)(val & 0xffff));
  99. PutLE16(data + 2, (int)(val >> 16));
  100. }
  101. // Returns (int)floor(log2(n)). n must be > 0.
  102. // use GNU builtins where available.
  103. #if defined(__GNUC__) && \
  104. ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
  105. static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
  106. return 31 ^ __builtin_clz(n);
  107. }
  108. #elif defined(_MSC_VER) && _MSC_VER > 1310 && \
  109. (defined(_M_X64) || defined(_M_IX86))
  110. #include <intrin.h>
  111. #pragma intrinsic(_BitScanReverse)
  112. static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
  113. unsigned long first_set_bit;
  114. _BitScanReverse(&first_set_bit, n);
  115. return first_set_bit;
  116. }
  117. #else
  118. static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
  119. int log = 0;
  120. uint32_t value = n;
  121. int i;
  122. for (i = 4; i >= 0; --i) {
  123. const int shift = (1 << i);
  124. const uint32_t x = value >> shift;
  125. if (x != 0) {
  126. value = x;
  127. log += shift;
  128. }
  129. }
  130. return log;
  131. }
  132. #endif
  133. //------------------------------------------------------------------------------
  134. // Pixel copying.
  135. struct WebPPicture;
  136. // Copy width x height pixels from 'src' to 'dst' honoring the strides.
  137. WEBP_EXTERN(void) WebPCopyPlane(const uint8_t* src, int src_stride,
  138. uint8_t* dst, int dst_stride,
  139. int width, int height);
  140. // Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are
  141. // assumed to be already allocated and using ARGB data.
  142. WEBP_EXTERN(void) WebPCopyPixels(const struct WebPPicture* const src,
  143. struct WebPPicture* const dst);
  144. //------------------------------------------------------------------------------
  145. // Unique colors.
  146. // Returns count of unique colors in 'pic', assuming pic->use_argb is true.
  147. // If the unique color count is more than MAX_COLOR_COUNT, returns
  148. // MAX_COLOR_COUNT+1.
  149. // If 'palette' is not NULL and number of unique colors is less than or equal to
  150. // MAX_COLOR_COUNT, also outputs the actual unique colors into 'palette'.
  151. // Note: 'palette' is assumed to be an array already allocated with at least
  152. // MAX_COLOR_COUNT elements.
  153. WEBP_EXTERN(int) WebPGetColorPalette(const struct WebPPicture* const pic,
  154. uint32_t* const palette);
  155. //------------------------------------------------------------------------------
  156. #ifdef __cplusplus
  157. } // extern "C"
  158. #endif
  159. #endif /* WEBP_UTILS_UTILS_H_ */