Path: blob/master/3rdparty/libwebp/src/utils/utils.h
16358 views
// Copyright 2012 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Misc. common utility functions10//11// Authors: Skal ([email protected])12// Urvang ([email protected])1314#ifndef WEBP_UTILS_UTILS_H_15#define WEBP_UTILS_UTILS_H_1617#ifdef HAVE_CONFIG_H18#include "src/webp/config.h"19#endif2021#include <assert.h>22#include <limits.h>2324#include "src/dsp/dsp.h"25#include "src/webp/types.h"2627#ifdef __cplusplus28extern "C" {29#endif3031//------------------------------------------------------------------------------32// Memory allocation3334// This is the maximum memory amount that libwebp will ever try to allocate.35#ifndef WEBP_MAX_ALLOCABLE_MEMORY36#if SIZE_MAX > (1ULL << 34)37#define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 34)38#else39// For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.40#define WEBP_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))41#endif42#endif // WEBP_MAX_ALLOCABLE_MEMORY4344// size-checking safe malloc/calloc: verify that the requested size is not too45// large, or return NULL. You don't need to call these for constructs like46// malloc(sizeof(foo)), but only if there's picture-dependent size involved47// somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this48// safe malloc() borrows the signature from calloc(), pointing at the dangerous49// underlying multiply involved.50WEBP_EXTERN void* WebPSafeMalloc(uint64_t nmemb, size_t size);51// Note that WebPSafeCalloc() expects the second argument type to be 'size_t'52// in order to favor the "calloc(num_foo, sizeof(foo))" pattern.53WEBP_EXTERN void* WebPSafeCalloc(uint64_t nmemb, size_t size);5455// Companion deallocation function to the above allocations.56WEBP_EXTERN void WebPSafeFree(void* const ptr);5758//------------------------------------------------------------------------------59// Alignment6061#define WEBP_ALIGN_CST 3162#define WEBP_ALIGN(PTR) (((uintptr_t)(PTR) + WEBP_ALIGN_CST) & ~WEBP_ALIGN_CST)6364#include <string.h>65// memcpy() is the safe way of moving potentially unaligned 32b memory.66static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {67uint32_t A;68memcpy(&A, ptr, sizeof(A));69return A;70}71static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {72memcpy(ptr, &val, sizeof(val));73}7475//------------------------------------------------------------------------------76// Reading/writing data.7778// Read 16, 24 or 32 bits stored in little-endian order.79static WEBP_INLINE int GetLE16(const uint8_t* const data) {80return (int)(data[0] << 0) | (data[1] << 8);81}8283static WEBP_INLINE int GetLE24(const uint8_t* const data) {84return GetLE16(data) | (data[2] << 16);85}8687static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {88return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16);89}9091// Store 16, 24 or 32 bits in little-endian order.92static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {93assert(val < (1 << 16));94data[0] = (val >> 0);95data[1] = (val >> 8);96}9798static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {99assert(val < (1 << 24));100PutLE16(data, val & 0xffff);101data[2] = (val >> 16);102}103104static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {105PutLE16(data, (int)(val & 0xffff));106PutLE16(data + 2, (int)(val >> 16));107}108109// Returns 31 ^ clz(n) = log2(n). This is the default C-implementation, either110// based on table or not. Can be used as fallback if clz() is not available.111#define WEBP_NEED_LOG_TABLE_8BIT112extern const uint8_t WebPLogTable8bit[256];113static WEBP_INLINE int WebPLog2FloorC(uint32_t n) {114int log_value = 0;115while (n >= 256) {116log_value += 8;117n >>= 8;118}119return log_value + WebPLogTable8bit[n];120}121122// Returns (int)floor(log2(n)). n must be > 0.123// use GNU builtins where available.124#if defined(__GNUC__) && \125((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)126static WEBP_INLINE int BitsLog2Floor(uint32_t n) {127return 31 ^ __builtin_clz(n);128}129#elif defined(_MSC_VER) && _MSC_VER > 1310 && \130(defined(_M_X64) || defined(_M_IX86))131#include <intrin.h>132#pragma intrinsic(_BitScanReverse)133134static WEBP_INLINE int BitsLog2Floor(uint32_t n) {135unsigned long first_set_bit;136_BitScanReverse(&first_set_bit, n);137return first_set_bit;138}139#else // default: use the C-version.140static WEBP_INLINE int BitsLog2Floor(uint32_t n) { return WebPLog2FloorC(n); }141#endif142143//------------------------------------------------------------------------------144// Pixel copying.145146struct WebPPicture;147148// Copy width x height pixels from 'src' to 'dst' honoring the strides.149WEBP_EXTERN void WebPCopyPlane(const uint8_t* src, int src_stride,150uint8_t* dst, int dst_stride,151int width, int height);152153// Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are154// assumed to be already allocated and using ARGB data.155WEBP_EXTERN void WebPCopyPixels(const struct WebPPicture* const src,156struct WebPPicture* const dst);157158//------------------------------------------------------------------------------159// Unique colors.160161// Returns count of unique colors in 'pic', assuming pic->use_argb is true.162// If the unique color count is more than MAX_PALETTE_SIZE, returns163// MAX_PALETTE_SIZE+1.164// If 'palette' is not NULL and number of unique colors is less than or equal to165// MAX_PALETTE_SIZE, also outputs the actual unique colors into 'palette'.166// Note: 'palette' is assumed to be an array already allocated with at least167// MAX_PALETTE_SIZE elements.168WEBP_EXTERN int WebPGetColorPalette(const struct WebPPicture* const pic,169uint32_t* const palette);170171//------------------------------------------------------------------------------172173#ifdef __cplusplus174} // extern "C"175#endif176177#endif /* WEBP_UTILS_UTILS_H_ */178179180