Blame src/webp/encode.h

Packit 9c6abc
// Copyright 2011 Google Inc. All Rights Reserved.
Packit 9c6abc
//
Packit 9c6abc
// Use of this source code is governed by a BSD-style license
Packit 9c6abc
// that can be found in the COPYING file in the root of the source
Packit 9c6abc
// tree. An additional intellectual property rights grant can be found
Packit 9c6abc
// in the file PATENTS. All contributing project authors may
Packit 9c6abc
// be found in the AUTHORS file in the root of the source tree.
Packit 9c6abc
// -----------------------------------------------------------------------------
Packit 9c6abc
//
Packit 9c6abc
//   WebP encoder: main interface
Packit 9c6abc
//
Packit 9c6abc
// Author: Skal (pascal.massimino@gmail.com)
Packit 9c6abc
Packit 9c6abc
#ifndef WEBP_WEBP_ENCODE_H_
Packit 9c6abc
#define WEBP_WEBP_ENCODE_H_
Packit 9c6abc
Packit 9c6abc
#include "./types.h"
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
extern "C" {
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
#define WEBP_ENCODER_ABI_VERSION 0x020e    // MAJOR(8b) + MINOR(8b)
Packit 9c6abc
Packit 9c6abc
// Note: forward declaring enumerations is not allowed in (strict) C and C++,
Packit 9c6abc
// the types are left here for reference.
Packit 9c6abc
// typedef enum WebPImageHint WebPImageHint;
Packit 9c6abc
// typedef enum WebPEncCSP WebPEncCSP;
Packit 9c6abc
// typedef enum WebPPreset WebPPreset;
Packit 9c6abc
// typedef enum WebPEncodingError WebPEncodingError;
Packit 9c6abc
typedef struct WebPConfig WebPConfig;
Packit 9c6abc
typedef struct WebPPicture WebPPicture;   // main structure for I/O
Packit 9c6abc
typedef struct WebPAuxStats WebPAuxStats;
Packit 9c6abc
typedef struct WebPMemoryWriter WebPMemoryWriter;
Packit 9c6abc
Packit 9c6abc
// Return the encoder's version number, packed in hexadecimal using 8bits for
Packit 9c6abc
// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
Packit 9c6abc
WEBP_EXTERN int WebPGetEncoderVersion(void);
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// One-stop-shop call! No questions asked:
Packit 9c6abc
Packit 9c6abc
// Returns the size of the compressed data (pointed to by *output), or 0 if
Packit 9c6abc
// an error occurred. The compressed data must be released by the caller
Packit 9c6abc
// using the call 'WebPFree(*output)'.
Packit 9c6abc
// These functions compress using the lossy format, and the quality_factor
Packit 9c6abc
// can go from 0 (smaller output, lower quality) to 100 (best quality,
Packit 9c6abc
// larger output).
Packit 9c6abc
WEBP_EXTERN size_t WebPEncodeRGB(const uint8_t* rgb,
Packit 9c6abc
                                 int width, int height, int stride,
Packit 9c6abc
                                 float quality_factor, uint8_t** output);
Packit 9c6abc
WEBP_EXTERN size_t WebPEncodeBGR(const uint8_t* bgr,
Packit 9c6abc
                                 int width, int height, int stride,
Packit 9c6abc
                                 float quality_factor, uint8_t** output);
Packit 9c6abc
WEBP_EXTERN size_t WebPEncodeRGBA(const uint8_t* rgba,
Packit 9c6abc
                                  int width, int height, int stride,
Packit 9c6abc
                                  float quality_factor, uint8_t** output);
Packit 9c6abc
WEBP_EXTERN size_t WebPEncodeBGRA(const uint8_t* bgra,
Packit 9c6abc
                                  int width, int height, int stride,
Packit 9c6abc
                                  float quality_factor, uint8_t** output);
Packit 9c6abc
Packit 9c6abc
// These functions are the equivalent of the above, but compressing in a
Packit 9c6abc
// lossless manner. Files are usually larger than lossy format, but will
Packit 9c6abc
// not suffer any compression loss.
Packit 9c6abc
WEBP_EXTERN size_t WebPEncodeLosslessRGB(const uint8_t* rgb,
Packit 9c6abc
                                         int width, int height, int stride,
Packit 9c6abc
                                         uint8_t** output);
Packit 9c6abc
WEBP_EXTERN size_t WebPEncodeLosslessBGR(const uint8_t* bgr,
Packit 9c6abc
                                         int width, int height, int stride,
Packit 9c6abc
                                         uint8_t** output);
Packit 9c6abc
WEBP_EXTERN size_t WebPEncodeLosslessRGBA(const uint8_t* rgba,
Packit 9c6abc
                                          int width, int height, int stride,
Packit 9c6abc
                                          uint8_t** output);
Packit 9c6abc
WEBP_EXTERN size_t WebPEncodeLosslessBGRA(const uint8_t* bgra,
Packit 9c6abc
                                          int width, int height, int stride,
Packit 9c6abc
                                          uint8_t** output);
Packit 9c6abc
Packit 9c6abc
// Releases memory returned by the WebPEncode*() functions above.
Packit 9c6abc
WEBP_EXTERN void WebPFree(void* ptr);
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Coding parameters
Packit 9c6abc
Packit 9c6abc
// Image characteristics hint for the underlying encoder.
Packit 9c6abc
typedef enum WebPImageHint {
Packit 9c6abc
  WEBP_HINT_DEFAULT = 0,  // default preset.
Packit 9c6abc
  WEBP_HINT_PICTURE,      // digital picture, like portrait, inner shot
Packit 9c6abc
  WEBP_HINT_PHOTO,        // outdoor photograph, with natural lighting
Packit 9c6abc
  WEBP_HINT_GRAPH,        // Discrete tone image (graph, map-tile etc).
Packit 9c6abc
  WEBP_HINT_LAST
Packit 9c6abc
} WebPImageHint;
Packit 9c6abc
Packit 9c6abc
// Compression parameters.
Packit 9c6abc
struct WebPConfig {
Packit 9c6abc
  int lossless;           // Lossless encoding (0=lossy(default), 1=lossless).
Packit 9c6abc
  float quality;          // between 0 and 100. For lossy, 0 gives the smallest
Packit 9c6abc
                          // size and 100 the largest. For lossless, this
Packit 9c6abc
                          // parameter is the amount of effort put into the
Packit 9c6abc
                          // compression: 0 is the fastest but gives larger
Packit 9c6abc
                          // files compared to the slowest, but best, 100.
Packit 9c6abc
  int method;             // quality/speed trade-off (0=fast, 6=slower-better)
Packit 9c6abc
Packit 9c6abc
  WebPImageHint image_hint;  // Hint for image type (lossless only for now).
Packit 9c6abc
Packit 9c6abc
  int target_size;        // if non-zero, set the desired target size in bytes.
Packit 9c6abc
                          // Takes precedence over the 'compression' parameter.
Packit 9c6abc
  float target_PSNR;      // if non-zero, specifies the minimal distortion to
Packit 9c6abc
                          // try to achieve. Takes precedence over target_size.
Packit 9c6abc
  int segments;           // maximum number of segments to use, in [1..4]
Packit 9c6abc
  int sns_strength;       // Spatial Noise Shaping. 0=off, 100=maximum.
Packit 9c6abc
  int filter_strength;    // range: [0 = off .. 100 = strongest]
Packit 9c6abc
  int filter_sharpness;   // range: [0 = off .. 7 = least sharp]
Packit 9c6abc
  int filter_type;        // filtering type: 0 = simple, 1 = strong (only used
Packit 9c6abc
                          // if filter_strength > 0 or autofilter > 0)
Packit 9c6abc
  int autofilter;         // Auto adjust filter's strength [0 = off, 1 = on]
Packit 9c6abc
  int alpha_compression;  // Algorithm for encoding the alpha plane (0 = none,
Packit 9c6abc
                          // 1 = compressed with WebP lossless). Default is 1.
Packit 9c6abc
  int alpha_filtering;    // Predictive filtering method for alpha plane.
Packit 9c6abc
                          //  0: none, 1: fast, 2: best. Default if 1.
Packit 9c6abc
  int alpha_quality;      // Between 0 (smallest size) and 100 (lossless).
Packit 9c6abc
                          // Default is 100.
Packit 9c6abc
  int pass;               // number of entropy-analysis passes (in [1..10]).
Packit 9c6abc
Packit 9c6abc
  int show_compressed;    // if true, export the compressed picture back.
Packit 9c6abc
                          // In-loop filtering is not applied.
Packit 9c6abc
  int preprocessing;      // preprocessing filter:
Packit 9c6abc
                          // 0=none, 1=segment-smooth, 2=pseudo-random dithering
Packit 9c6abc
  int partitions;         // log2(number of token partitions) in [0..3]. Default
Packit 9c6abc
                          // is set to 0 for easier progressive decoding.
Packit 9c6abc
  int partition_limit;    // quality degradation allowed to fit the 512k limit
Packit 9c6abc
                          // on prediction modes coding (0: no degradation,
Packit 9c6abc
                          // 100: maximum possible degradation).
Packit 9c6abc
  int emulate_jpeg_size;  // If true, compression parameters will be remapped
Packit 9c6abc
                          // to better match the expected output size from
Packit 9c6abc
                          // JPEG compression. Generally, the output size will
Packit 9c6abc
                          // be similar but the degradation will be lower.
Packit 9c6abc
  int thread_level;       // If non-zero, try and use multi-threaded encoding.
Packit 9c6abc
  int low_memory;         // If set, reduce memory usage (but increase CPU use).
Packit 9c6abc
Packit 9c6abc
  int near_lossless;      // Near lossless encoding [0 = max loss .. 100 = off
Packit 9c6abc
                          // (default)].
Packit 9c6abc
  int exact;              // if non-zero, preserve the exact RGB values under
Packit 9c6abc
                          // transparent area. Otherwise, discard this invisible
Packit 9c6abc
                          // RGB information for better compression. The default
Packit 9c6abc
                          // value is 0.
Packit 9c6abc
Packit 9c6abc
  int use_delta_palette;  // reserved for future lossless feature
Packit 9c6abc
  int use_sharp_yuv;      // if needed, use sharp (and slow) RGB->YUV conversion
Packit 9c6abc
Packit 9c6abc
  uint32_t pad[2];        // padding for later use
Packit 9c6abc
};
Packit 9c6abc
Packit 9c6abc
// Enumerate some predefined settings for WebPConfig, depending on the type
Packit 9c6abc
// of source picture. These presets are used when calling WebPConfigPreset().
Packit 9c6abc
typedef enum WebPPreset {
Packit 9c6abc
  WEBP_PRESET_DEFAULT = 0,  // default preset.
Packit 9c6abc
  WEBP_PRESET_PICTURE,      // digital picture, like portrait, inner shot
Packit 9c6abc
  WEBP_PRESET_PHOTO,        // outdoor photograph, with natural lighting
Packit 9c6abc
  WEBP_PRESET_DRAWING,      // hand or line drawing, with high-contrast details
Packit 9c6abc
  WEBP_PRESET_ICON,         // small-sized colorful images
Packit 9c6abc
  WEBP_PRESET_TEXT          // text-like
Packit 9c6abc
} WebPPreset;
Packit 9c6abc
Packit 9c6abc
// Internal, version-checked, entry point
Packit 9c6abc
WEBP_EXTERN int WebPConfigInitInternal(WebPConfig*, WebPPreset, float, int);
Packit 9c6abc
Packit 9c6abc
// Should always be called, to initialize a fresh WebPConfig structure before
Packit 9c6abc
// modification. Returns false in case of version mismatch. WebPConfigInit()
Packit 9c6abc
// must have succeeded before using the 'config' object.
Packit 9c6abc
// Note that the default values are lossless=0 and quality=75.
Packit 9c6abc
static WEBP_INLINE int WebPConfigInit(WebPConfig* config) {
Packit 9c6abc
  return WebPConfigInitInternal(config, WEBP_PRESET_DEFAULT, 75.f,
Packit 9c6abc
                                WEBP_ENCODER_ABI_VERSION);
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
// This function will initialize the configuration according to a predefined
Packit 9c6abc
// set of parameters (referred to by 'preset') and a given quality factor.
Packit 9c6abc
// This function can be called as a replacement to WebPConfigInit(). Will
Packit 9c6abc
// return false in case of error.
Packit 9c6abc
static WEBP_INLINE int WebPConfigPreset(WebPConfig* config,
Packit 9c6abc
                                        WebPPreset preset, float quality) {
Packit 9c6abc
  return WebPConfigInitInternal(config, preset, quality,
Packit 9c6abc
                                WEBP_ENCODER_ABI_VERSION);
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
// Activate the lossless compression mode with the desired efficiency level
Packit 9c6abc
// between 0 (fastest, lowest compression) and 9 (slower, best compression).
Packit 9c6abc
// A good default level is '6', providing a fair tradeoff between compression
Packit 9c6abc
// speed and final compressed size.
Packit 9c6abc
// This function will overwrite several fields from config: 'method', 'quality'
Packit 9c6abc
// and 'lossless'. Returns false in case of parameter error.
Packit 9c6abc
WEBP_EXTERN int WebPConfigLosslessPreset(WebPConfig* config, int level);
Packit 9c6abc
Packit 9c6abc
// Returns true if 'config' is non-NULL and all configuration parameters are
Packit 9c6abc
// within their valid ranges.
Packit 9c6abc
WEBP_EXTERN int WebPValidateConfig(const WebPConfig* config);
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Input / Output
Packit 9c6abc
// Structure for storing auxiliary statistics.
Packit 9c6abc
Packit 9c6abc
struct WebPAuxStats {
Packit 9c6abc
  int coded_size;         // final size
Packit 9c6abc
Packit 9c6abc
  float PSNR[5];          // peak-signal-to-noise ratio for Y/U/V/All/Alpha
Packit 9c6abc
  int block_count[3];     // number of intra4/intra16/skipped macroblocks
Packit 9c6abc
  int header_bytes[2];    // approximate number of bytes spent for header
Packit 9c6abc
                          // and mode-partition #0
Packit 9c6abc
  int residual_bytes[3][4];  // approximate number of bytes spent for
Packit 9c6abc
                             // DC/AC/uv coefficients for each (0..3) segments.
Packit 9c6abc
  int segment_size[4];    // number of macroblocks in each segments
Packit 9c6abc
  int segment_quant[4];   // quantizer values for each segments
Packit 9c6abc
  int segment_level[4];   // filtering strength for each segments [0..63]
Packit 9c6abc
Packit 9c6abc
  int alpha_data_size;    // size of the transparency data
Packit 9c6abc
  int layer_data_size;    // size of the enhancement layer data
Packit 9c6abc
Packit 9c6abc
  // lossless encoder statistics
Packit 9c6abc
  uint32_t lossless_features;  // bit0:predictor bit1:cross-color transform
Packit 9c6abc
                               // bit2:subtract-green bit3:color indexing
Packit 9c6abc
  int histogram_bits;          // number of precision bits of histogram
Packit 9c6abc
  int transform_bits;          // precision bits for transform
Packit 9c6abc
  int cache_bits;              // number of bits for color cache lookup
Packit 9c6abc
  int palette_size;            // number of color in palette, if used
Packit 9c6abc
  int lossless_size;           // final lossless size
Packit 9c6abc
  int lossless_hdr_size;       // lossless header (transform, huffman etc) size
Packit 9c6abc
  int lossless_data_size;      // lossless image data size
Packit 9c6abc
Packit 9c6abc
  uint32_t pad[2];        // padding for later use
Packit 9c6abc
};
Packit 9c6abc
Packit 9c6abc
// Signature for output function. Should return true if writing was successful.
Packit 9c6abc
// data/data_size is the segment of data to write, and 'picture' is for
Packit 9c6abc
// reference (and so one can make use of picture->custom_ptr).
Packit 9c6abc
typedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size,
Packit 9c6abc
                                  const WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// WebPMemoryWrite: a special WebPWriterFunction that writes to memory using
Packit 9c6abc
// the following WebPMemoryWriter object (to be set as a custom_ptr).
Packit 9c6abc
struct WebPMemoryWriter {
Packit 9c6abc
  uint8_t* mem;       // final buffer (of size 'max_size', larger than 'size').
Packit 9c6abc
  size_t   size;      // final size
Packit 9c6abc
  size_t   max_size;  // total capacity
Packit 9c6abc
  uint32_t pad[1];    // padding for later use
Packit 9c6abc
};
Packit 9c6abc
Packit 9c6abc
// The following must be called first before any use.
Packit 9c6abc
WEBP_EXTERN void WebPMemoryWriterInit(WebPMemoryWriter* writer);
Packit 9c6abc
Packit 9c6abc
// The following must be called to deallocate writer->mem memory. The 'writer'
Packit 9c6abc
// object itself is not deallocated.
Packit 9c6abc
WEBP_EXTERN void WebPMemoryWriterClear(WebPMemoryWriter* writer);
Packit 9c6abc
// The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon
Packit 9c6abc
// completion, writer.mem and writer.size will hold the coded data.
Packit 9c6abc
// writer.mem must be freed by calling WebPMemoryWriterClear.
Packit 9c6abc
WEBP_EXTERN int WebPMemoryWrite(const uint8_t* data, size_t data_size,
Packit 9c6abc
                                const WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Progress hook, called from time to time to report progress. It can return
Packit 9c6abc
// false to request an abort of the encoding process, or true otherwise if
Packit 9c6abc
// everything is OK.
Packit 9c6abc
typedef int (*WebPProgressHook)(int percent, const WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Color spaces.
Packit 9c6abc
typedef enum WebPEncCSP {
Packit 9c6abc
  // chroma sampling
Packit 9c6abc
  WEBP_YUV420  = 0,        // 4:2:0
Packit 9c6abc
  WEBP_YUV420A = 4,        // alpha channel variant
Packit 9c6abc
  WEBP_CSP_UV_MASK = 3,    // bit-mask to get the UV sampling factors
Packit 9c6abc
  WEBP_CSP_ALPHA_BIT = 4   // bit that is set if alpha is present
Packit 9c6abc
} WebPEncCSP;
Packit 9c6abc
Packit 9c6abc
// Encoding error conditions.
Packit 9c6abc
typedef enum WebPEncodingError {
Packit 9c6abc
  VP8_ENC_OK = 0,
Packit 9c6abc
  VP8_ENC_ERROR_OUT_OF_MEMORY,            // memory error allocating objects
Packit 9c6abc
  VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY,  // memory error while flushing bits
Packit 9c6abc
  VP8_ENC_ERROR_NULL_PARAMETER,           // a pointer parameter is NULL
Packit 9c6abc
  VP8_ENC_ERROR_INVALID_CONFIGURATION,    // configuration is invalid
Packit 9c6abc
  VP8_ENC_ERROR_BAD_DIMENSION,            // picture has invalid width/height
Packit 9c6abc
  VP8_ENC_ERROR_PARTITION0_OVERFLOW,      // partition is bigger than 512k
Packit 9c6abc
  VP8_ENC_ERROR_PARTITION_OVERFLOW,       // partition is bigger than 16M
Packit 9c6abc
  VP8_ENC_ERROR_BAD_WRITE,                // error while flushing bytes
Packit 9c6abc
  VP8_ENC_ERROR_FILE_TOO_BIG,             // file is bigger than 4G
Packit 9c6abc
  VP8_ENC_ERROR_USER_ABORT,               // abort request by user
Packit 9c6abc
  VP8_ENC_ERROR_LAST                      // list terminator. always last.
Packit 9c6abc
} WebPEncodingError;
Packit 9c6abc
Packit 9c6abc
// maximum width/height allowed (inclusive), in pixels
Packit 9c6abc
#define WEBP_MAX_DIMENSION 16383
Packit 9c6abc
Packit 9c6abc
// Main exchange structure (input samples, output bytes, statistics)
Packit 9c6abc
struct WebPPicture {
Packit 9c6abc
  //   INPUT
Packit 9c6abc
  //////////////
Packit 9c6abc
  // Main flag for encoder selecting between ARGB or YUV input.
Packit 9c6abc
  // It is recommended to use ARGB input (*argb, argb_stride) for lossless
Packit 9c6abc
  // compression, and YUV input (*y, *u, *v, etc.) for lossy compression
Packit 9c6abc
  // since these are the respective native colorspace for these formats.
Packit 9c6abc
  int use_argb;
Packit 9c6abc
Packit 9c6abc
  // YUV input (mostly used for input to lossy compression)
Packit 9c6abc
  WebPEncCSP colorspace;     // colorspace: should be YUV420 for now (=Y'CbCr).
Packit 9c6abc
  int width, height;         // dimensions (less or equal to WEBP_MAX_DIMENSION)
Packit 9c6abc
  uint8_t *y, *u, *v;        // pointers to luma/chroma planes.
Packit 9c6abc
  int y_stride, uv_stride;   // luma/chroma strides.
Packit 9c6abc
  uint8_t* a;                // pointer to the alpha plane
Packit 9c6abc
  int a_stride;              // stride of the alpha plane
Packit 9c6abc
  uint32_t pad1[2];          // padding for later use
Packit 9c6abc
Packit 9c6abc
  // ARGB input (mostly used for input to lossless compression)
Packit 9c6abc
  uint32_t* argb;            // Pointer to argb (32 bit) plane.
Packit 9c6abc
  int argb_stride;           // This is stride in pixels units, not bytes.
Packit 9c6abc
  uint32_t pad2[3];          // padding for later use
Packit 9c6abc
Packit 9c6abc
  //   OUTPUT
Packit 9c6abc
  ///////////////
Packit 9c6abc
  // Byte-emission hook, to store compressed bytes as they are ready.
Packit 9c6abc
  WebPWriterFunction writer;  // can be NULL
Packit 9c6abc
  void* custom_ptr;           // can be used by the writer.
Packit 9c6abc
Packit 9c6abc
  // map for extra information (only for lossy compression mode)
Packit 9c6abc
  int extra_info_type;    // 1: intra type, 2: segment, 3: quant
Packit 9c6abc
                          // 4: intra-16 prediction mode,
Packit 9c6abc
                          // 5: chroma prediction mode,
Packit 9c6abc
                          // 6: bit cost, 7: distortion
Packit 9c6abc
  uint8_t* extra_info;    // if not NULL, points to an array of size
Packit 9c6abc
                          // ((width + 15) / 16) * ((height + 15) / 16) that
Packit 9c6abc
                          // will be filled with a macroblock map, depending
Packit 9c6abc
                          // on extra_info_type.
Packit 9c6abc
Packit 9c6abc
  //   STATS AND REPORTS
Packit 9c6abc
  ///////////////////////////
Packit 9c6abc
  // Pointer to side statistics (updated only if not NULL)
Packit 9c6abc
  WebPAuxStats* stats;
Packit 9c6abc
Packit 9c6abc
  // Error code for the latest error encountered during encoding
Packit 9c6abc
  WebPEncodingError error_code;
Packit 9c6abc
Packit 9c6abc
  // If not NULL, report progress during encoding.
Packit 9c6abc
  WebPProgressHook progress_hook;
Packit 9c6abc
Packit 9c6abc
  void* user_data;        // this field is free to be set to any value and
Packit 9c6abc
                          // used during callbacks (like progress-report e.g.).
Packit 9c6abc
Packit 9c6abc
  uint32_t pad3[3];       // padding for later use
Packit 9c6abc
Packit 9c6abc
  // Unused for now
Packit 9c6abc
  uint8_t *pad4, *pad5;
Packit 9c6abc
  uint32_t pad6[8];       // padding for later use
Packit 9c6abc
Packit 9c6abc
  // PRIVATE FIELDS
Packit 9c6abc
  ////////////////////
Packit 9c6abc
  void* memory_;          // row chunk of memory for yuva planes
Packit 9c6abc
  void* memory_argb_;     // and for argb too.
Packit 9c6abc
  void* pad7[2];          // padding for later use
Packit 9c6abc
};
Packit 9c6abc
Packit 9c6abc
// Internal, version-checked, entry point
Packit 9c6abc
WEBP_EXTERN int WebPPictureInitInternal(WebPPicture*, int);
Packit 9c6abc
Packit 9c6abc
// Should always be called, to initialize the structure. Returns false in case
Packit 9c6abc
// of version mismatch. WebPPictureInit() must have succeeded before using the
Packit 9c6abc
// 'picture' object.
Packit 9c6abc
// Note that, by default, use_argb is false and colorspace is WEBP_YUV420.
Packit 9c6abc
static WEBP_INLINE int WebPPictureInit(WebPPicture* picture) {
Packit 9c6abc
  return WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION);
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// WebPPicture utils
Packit 9c6abc
Packit 9c6abc
// Convenience allocation / deallocation based on picture->width/height:
Packit 9c6abc
// Allocate y/u/v buffers as per colorspace/width/height specification.
Packit 9c6abc
// Note! This function will free the previous buffer if needed.
Packit 9c6abc
// Returns false in case of memory error.
Packit 9c6abc
WEBP_EXTERN int WebPPictureAlloc(WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Release the memory allocated by WebPPictureAlloc() or WebPPictureImport*().
Packit 9c6abc
// Note that this function does _not_ free the memory used by the 'picture'
Packit 9c6abc
// object itself.
Packit 9c6abc
// Besides memory (which is reclaimed) all other fields of 'picture' are
Packit 9c6abc
// preserved.
Packit 9c6abc
WEBP_EXTERN void WebPPictureFree(WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Copy the pixels of *src into *dst, using WebPPictureAlloc. Upon return, *dst
Packit 9c6abc
// will fully own the copied pixels (this is not a view). The 'dst' picture need
Packit 9c6abc
// not be initialized as its content is overwritten.
Packit 9c6abc
// Returns false in case of memory allocation error.
Packit 9c6abc
WEBP_EXTERN int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst);
Packit 9c6abc
Packit 9c6abc
// Compute the single distortion for packed planes of samples.
Packit 9c6abc
// 'src' will be compared to 'ref', and the raw distortion stored into
Packit 9c6abc
// '*distortion'. The refined metric (log(MSE), log(1 - ssim),...' will be
Packit 9c6abc
// stored in '*result'.
Packit 9c6abc
// 'x_step' is the horizontal stride (in bytes) between samples.
Packit 9c6abc
// 'src/ref_stride' is the byte distance between rows.
Packit 9c6abc
// Returns false in case of error (bad parameter, memory allocation error, ...).
Packit 9c6abc
WEBP_EXTERN int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,
Packit 9c6abc
                                    const uint8_t* ref, size_t ref_stride,
Packit 9c6abc
                                    int width, int height,
Packit 9c6abc
                                    size_t x_step,
Packit 9c6abc
                                    int type,   // 0 = PSNR, 1 = SSIM, 2 = LSIM
Packit 9c6abc
                                    float* distortion, float* result);
Packit 9c6abc
Packit 9c6abc
// Compute PSNR, SSIM or LSIM distortion metric between two pictures. Results
Packit 9c6abc
// are in dB, stored in result[] in the B/G/R/A/All order. The distortion is
Packit 9c6abc
// always performed using ARGB samples. Hence if the input is YUV(A), the
Packit 9c6abc
// picture will be internally converted to ARGB (just for the measurement).
Packit 9c6abc
// Warning: this function is rather CPU-intensive.
Packit 9c6abc
WEBP_EXTERN int WebPPictureDistortion(
Packit 9c6abc
    const WebPPicture* src, const WebPPicture* ref,
Packit 9c6abc
    int metric_type,           // 0 = PSNR, 1 = SSIM, 2 = LSIM
Packit 9c6abc
    float result[5]);
Packit 9c6abc
Packit 9c6abc
// self-crops a picture to the rectangle defined by top/left/width/height.
Packit 9c6abc
// Returns false in case of memory allocation error, or if the rectangle is
Packit 9c6abc
// outside of the source picture.
Packit 9c6abc
// The rectangle for the view is defined by the top-left corner pixel
Packit 9c6abc
// coordinates (left, top) as well as its width and height. This rectangle
Packit 9c6abc
// must be fully be comprised inside the 'src' source picture. If the source
Packit 9c6abc
// picture uses the YUV420 colorspace, the top and left coordinates will be
Packit 9c6abc
// snapped to even values.
Packit 9c6abc
WEBP_EXTERN int WebPPictureCrop(WebPPicture* picture,
Packit 9c6abc
                                int left, int top, int width, int height);
Packit 9c6abc
Packit 9c6abc
// Extracts a view from 'src' picture into 'dst'. The rectangle for the view
Packit 9c6abc
// is defined by the top-left corner pixel coordinates (left, top) as well
Packit 9c6abc
// as its width and height. This rectangle must be fully be comprised inside
Packit 9c6abc
// the 'src' source picture. If the source picture uses the YUV420 colorspace,
Packit 9c6abc
// the top and left coordinates will be snapped to even values.
Packit 9c6abc
// Picture 'src' must out-live 'dst' picture. Self-extraction of view is allowed
Packit 9c6abc
// ('src' equal to 'dst') as a mean of fast-cropping (but note that doing so,
Packit 9c6abc
// the original dimension will be lost). Picture 'dst' need not be initialized
Packit 9c6abc
// with WebPPictureInit() if it is different from 'src', since its content will
Packit 9c6abc
// be overwritten.
Packit 9c6abc
// Returns false in case of memory allocation error or invalid parameters.
Packit 9c6abc
WEBP_EXTERN int WebPPictureView(const WebPPicture* src,
Packit 9c6abc
                                int left, int top, int width, int height,
Packit 9c6abc
                                WebPPicture* dst);
Packit 9c6abc
Packit 9c6abc
// Returns true if the 'picture' is actually a view and therefore does
Packit 9c6abc
// not own the memory for pixels.
Packit 9c6abc
WEBP_EXTERN int WebPPictureIsView(const WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Rescale a picture to new dimension width x height.
Packit 9c6abc
// If either 'width' or 'height' (but not both) is 0 the corresponding
Packit 9c6abc
// dimension will be calculated preserving the aspect ratio.
Packit 9c6abc
// No gamma correction is applied.
Packit 9c6abc
// Returns false in case of error (invalid parameter or insufficient memory).
Packit 9c6abc
WEBP_EXTERN int WebPPictureRescale(WebPPicture* pic, int width, int height);
Packit 9c6abc
Packit 9c6abc
// Colorspace conversion function to import RGB samples.
Packit 9c6abc
// Previous buffer will be free'd, if any.
Packit 9c6abc
// *rgb buffer should have a size of at least height * rgb_stride.
Packit 9c6abc
// Returns false in case of memory error.
Packit 9c6abc
WEBP_EXTERN int WebPPictureImportRGB(
Packit 9c6abc
    WebPPicture* picture, const uint8_t* rgb, int rgb_stride);
Packit 9c6abc
// Same, but for RGBA buffer.
Packit 9c6abc
WEBP_EXTERN int WebPPictureImportRGBA(
Packit 9c6abc
    WebPPicture* picture, const uint8_t* rgba, int rgba_stride);
Packit 9c6abc
// Same, but for RGBA buffer. Imports the RGB direct from the 32-bit format
Packit 9c6abc
// input buffer ignoring the alpha channel. Avoids needing to copy the data
Packit 9c6abc
// to a temporary 24-bit RGB buffer to import the RGB only.
Packit 9c6abc
WEBP_EXTERN int WebPPictureImportRGBX(
Packit 9c6abc
    WebPPicture* picture, const uint8_t* rgbx, int rgbx_stride);
Packit 9c6abc
Packit 9c6abc
// Variants of the above, but taking BGR(A|X) input.
Packit 9c6abc
WEBP_EXTERN int WebPPictureImportBGR(
Packit 9c6abc
    WebPPicture* picture, const uint8_t* bgr, int bgr_stride);
Packit 9c6abc
WEBP_EXTERN int WebPPictureImportBGRA(
Packit 9c6abc
    WebPPicture* picture, const uint8_t* bgra, int bgra_stride);
Packit 9c6abc
WEBP_EXTERN int WebPPictureImportBGRX(
Packit 9c6abc
    WebPPicture* picture, const uint8_t* bgrx, int bgrx_stride);
Packit 9c6abc
Packit 9c6abc
// Converts picture->argb data to the YUV420A format. The 'colorspace'
Packit 9c6abc
// parameter is deprecated and should be equal to WEBP_YUV420.
Packit 9c6abc
// Upon return, picture->use_argb is set to false. The presence of real
Packit 9c6abc
// non-opaque transparent values is detected, and 'colorspace' will be
Packit 9c6abc
// adjusted accordingly. Note that this method is lossy.
Packit 9c6abc
// Returns false in case of error.
Packit 9c6abc
WEBP_EXTERN int WebPPictureARGBToYUVA(WebPPicture* picture,
Packit 9c6abc
                                      WebPEncCSP /*colorspace = WEBP_YUV420*/);
Packit 9c6abc
Packit 9c6abc
// Same as WebPPictureARGBToYUVA(), but the conversion is done using
Packit 9c6abc
// pseudo-random dithering with a strength 'dithering' between
Packit 9c6abc
// 0.0 (no dithering) and 1.0 (maximum dithering). This is useful
Packit 9c6abc
// for photographic picture.
Packit 9c6abc
WEBP_EXTERN int WebPPictureARGBToYUVADithered(
Packit 9c6abc
    WebPPicture* picture, WebPEncCSP colorspace, float dithering);
Packit 9c6abc
Packit 9c6abc
// Performs 'sharp' RGBA->YUVA420 downsampling and colorspace conversion.
Packit 9c6abc
// Downsampling is handled with extra care in case of color clipping. This
Packit 9c6abc
// method is roughly 2x slower than WebPPictureARGBToYUVA() but produces better
Packit 9c6abc
// and sharper YUV representation.
Packit 9c6abc
// Returns false in case of error.
Packit 9c6abc
WEBP_EXTERN int WebPPictureSharpARGBToYUVA(WebPPicture* picture);
Packit 9c6abc
// kept for backward compatibility:
Packit 9c6abc
WEBP_EXTERN int WebPPictureSmartARGBToYUVA(WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Converts picture->yuv to picture->argb and sets picture->use_argb to true.
Packit 9c6abc
// The input format must be YUV_420 or YUV_420A. The conversion from YUV420 to
Packit 9c6abc
// ARGB incurs a small loss too.
Packit 9c6abc
// Note that the use of this colorspace is discouraged if one has access to the
Packit 9c6abc
// raw ARGB samples, since using YUV420 is comparatively lossy.
Packit 9c6abc
// Returns false in case of error.
Packit 9c6abc
WEBP_EXTERN int WebPPictureYUVAToARGB(WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Helper function: given a width x height plane of RGBA or YUV(A) samples
Packit 9c6abc
// clean-up or smoothen the YUV or RGB samples under fully transparent area,
Packit 9c6abc
// to help compressibility (no guarantee, though).
Packit 9c6abc
WEBP_EXTERN void WebPCleanupTransparentArea(WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Scan the picture 'picture' for the presence of non fully opaque alpha values.
Packit 9c6abc
// Returns true in such case. Otherwise returns false (indicating that the
Packit 9c6abc
// alpha plane can be ignored altogether e.g.).
Packit 9c6abc
WEBP_EXTERN int WebPPictureHasTransparency(const WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
// Remove the transparency information (if present) by blending the color with
Packit 9c6abc
// the background color 'background_rgb' (specified as 24bit RGB triplet).
Packit 9c6abc
// After this call, all alpha values are reset to 0xff.
Packit 9c6abc
WEBP_EXTERN void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb);
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Main call
Packit 9c6abc
Packit 9c6abc
// Main encoding call, after config and picture have been initialized.
Packit 9c6abc
// 'picture' must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION),
Packit 9c6abc
// and the 'config' object must be a valid one.
Packit 9c6abc
// Returns false in case of error, true otherwise.
Packit 9c6abc
// In case of error, picture->error_code is updated accordingly.
Packit 9c6abc
// 'picture' can hold the source samples in both YUV(A) or ARGB input, depending
Packit 9c6abc
// on the value of 'picture->use_argb'. It is highly recommended to use
Packit 9c6abc
// the former for lossy encoding, and the latter for lossless encoding
Packit 9c6abc
// (when config.lossless is true). Automatic conversion from one format to
Packit 9c6abc
// another is provided but they both incur some loss.
Packit 9c6abc
WEBP_EXTERN int WebPEncode(const WebPConfig* config, WebPPicture* picture);
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
}    // extern "C"
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
#endif  /* WEBP_WEBP_ENCODE_H_ */