15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2011 Google Inc. All Rights Reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license
4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// that can be found in the COPYING file in the root of the source
5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// tree. An additional intellectual property rights grant can be found
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// in the file PATENTS. All contributing project authors may
7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// be found in the AUTHORS file in the root of the source tree.
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// -----------------------------------------------------------------------------
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   WebP encoder: main interface
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: Skal (pascal.massimino@gmail.com)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WEBP_WEBP_ENCODE_H_
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define WEBP_WEBP_ENCODE_H_
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "./types.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(__cplusplus) || defined(c_plusplus)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define WEBP_ENCODER_ABI_VERSION 0x0201    // MAJOR(8b) + MINOR(8b)
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Note: forward declaring enumerations is not allowed in (strict) C and C++,
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// the types are left here for reference.
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// typedef enum WebPImageHint WebPImageHint;
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// typedef enum WebPEncCSP WebPEncCSP;
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// typedef enum WebPPreset WebPPreset;
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// typedef enum WebPEncodingError WebPEncodingError;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef struct WebPConfig WebPConfig;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef struct WebPPicture WebPPicture;   // main structure for I/O
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef struct WebPAuxStats WebPAuxStats;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef struct WebPMemoryWriter WebPMemoryWriter;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return the encoder's version number, packed in hexadecimal using 8bits for
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPGetEncoderVersion(void);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// One-stop-shop call! No questions asked:
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the size of the compressed data (pointed to by *output), or 0 if
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// an error occurred. The compressed data must be released by the caller
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// using the call 'free(*output)'.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These functions compress using the lossy format, and the quality_factor
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// can go from 0 (smaller output, lower quality) to 100 (best quality,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// larger output).
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(size_t) WebPEncodeRGB(const uint8_t* rgb,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  int width, int height, int stride,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  float quality_factor, uint8_t** output);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(size_t) WebPEncodeBGR(const uint8_t* bgr,
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  int width, int height, int stride,
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  float quality_factor, uint8_t** output);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(size_t) WebPEncodeRGBA(const uint8_t* rgba,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int width, int height, int stride,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   float quality_factor, uint8_t** output);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(size_t) WebPEncodeBGRA(const uint8_t* bgra,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int width, int height, int stride,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   float quality_factor, uint8_t** output);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These functions are the equivalent of the above, but compressing in a
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// lossless manner. Files are usually larger than lossy format, but will
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not suffer any compression loss.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(size_t) WebPEncodeLosslessRGB(const uint8_t* rgb,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          int width, int height, int stride,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          uint8_t** output);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(size_t) WebPEncodeLosslessBGR(const uint8_t* bgr,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          int width, int height, int stride,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          uint8_t** output);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(size_t) WebPEncodeLosslessRGBA(const uint8_t* rgba,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           int width, int height, int stride,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           uint8_t** output);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(size_t) WebPEncodeLosslessBGRA(const uint8_t* bgra,
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           int width, int height, int stride,
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           uint8_t** output);
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Coding parameters
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Image characteristics hint for the underlying encoder.
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochtypedef enum WebPImageHint {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_HINT_DEFAULT = 0,  // default preset.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_HINT_PICTURE,      // digital picture, like portrait, inner shot
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_HINT_PHOTO,        // outdoor photograph, with natural lighting
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_HINT_GRAPH,        // Discrete tone image (graph, map-tile etc).
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_HINT_LAST
88eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} WebPImageHint;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Compression parameters.
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct WebPConfig {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int lossless;           // Lossless encoding (0=lossy(default), 1=lossless).
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  float quality;          // between 0 (smallest file) and 100 (biggest)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int method;             // quality/speed trade-off (0=fast, 6=slower-better)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebPImageHint image_hint;  // Hint for image type (lossless only for now).
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Parameters related to lossy compression only:
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int target_size;        // if non-zero, set the desired target size in bytes.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // Takes precedence over the 'compression' parameter.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  float target_PSNR;      // if non-zero, specifies the minimal distortion to
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // try to achieve. Takes precedence over target_size.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int segments;           // maximum number of segments to use, in [1..4]
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int sns_strength;       // Spatial Noise Shaping. 0=off, 100=maximum.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int filter_strength;    // range: [0 = off .. 100 = strongest]
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int filter_sharpness;   // range: [0 = off .. 7 = least sharp]
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int filter_type;        // filtering type: 0 = simple, 1 = strong (only used
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // if filter_strength > 0 or autofilter > 0)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int autofilter;         // Auto adjust filter's strength [0 = off, 1 = on]
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int alpha_compression;  // Algorithm for encoding the alpha plane (0 = none,
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // 1 = compressed with WebP lossless). Default is 1.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int alpha_filtering;    // Predictive filtering method for alpha plane.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          //  0: none, 1: fast, 2: best. Default if 1.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int alpha_quality;      // Between 0 (smallest size) and 100 (lossless).
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // Default is 100.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int pass;               // number of entropy-analysis passes (in [1..10]).
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int show_compressed;    // if true, export the compressed picture back.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // In-loop filtering is not applied.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int preprocessing;      // preprocessing filter (0=none, 1=segment-smooth)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int partitions;         // log2(number of token partitions) in [0..3]. Default
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // is set to 0 for easier progressive decoding.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int partition_limit;    // quality degradation allowed to fit the 512k limit
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // on prediction modes coding (0: no degradation,
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // 100: maximum possible degradation).
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int emulate_jpeg_size;  // If true, compression parameters will be remapped
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          // to better match the expected output size from
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          // JPEG compression. Generally, the output size will
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          // be similar but the degradation will be lower.
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int thread_level;       // If non-zero, try and use multi-threaded encoding.
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int low_memory;         // If set, reduce memory usage (but increase CPU use).
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  uint32_t pad[5];        // padding for later use
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Enumerate some predefined settings for WebPConfig, depending on the type
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of source picture. These presets are used when calling WebPConfigPreset().
138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochtypedef enum WebPPreset {
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_PRESET_DEFAULT = 0,  // default preset.
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_PRESET_PICTURE,      // digital picture, like portrait, inner shot
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_PRESET_PHOTO,        // outdoor photograph, with natural lighting
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_PRESET_DRAWING,      // hand or line drawing, with high-contrast details
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_PRESET_ICON,         // small-sized colorful images
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_PRESET_TEXT          // text-like
145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} WebPPreset;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Internal, version-checked, entry point
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPConfigInitInternal(WebPConfig*, WebPPreset, float, int);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Should always be called, to initialize a fresh WebPConfig structure before
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification. Returns false in case of version mismatch. WebPConfigInit()
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// must have succeeded before using the 'config' object.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that the default values are lossless=0 and quality=75.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static WEBP_INLINE int WebPConfigInit(WebPConfig* config) {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return WebPConfigInitInternal(config, WEBP_PRESET_DEFAULT, 75.f,
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                WEBP_ENCODER_ABI_VERSION);
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function will initialize the configuration according to a predefined
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// set of parameters (referred to by 'preset') and a given quality factor.
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function can be called as a replacement to WebPConfigInit(). Will
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// return false in case of error.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static WEBP_INLINE int WebPConfigPreset(WebPConfig* config,
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        WebPPreset preset, float quality) {
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return WebPConfigInitInternal(config, preset, quality,
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                WEBP_ENCODER_ABI_VERSION);
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if 'config' is non-NULL and all configuration parameters are
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// within their valid ranges.
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPValidateConfig(const WebPConfig* config);
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Input / Output
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Structure for storing auxiliary statistics (mostly for lossy encoding).
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct WebPAuxStats {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int coded_size;         // final size
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  float PSNR[5];          // peak-signal-to-noise ratio for Y/U/V/All/Alpha
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int block_count[3];     // number of intra4/intra16/skipped macroblocks
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int header_bytes[2];    // approximate number of bytes spent for header
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // and mode-partition #0
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int residual_bytes[3][4];  // approximate number of bytes spent for
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             // DC/AC/uv coefficients for each (0..3) segments.
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int segment_size[4];    // number of macroblocks in each segments
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int segment_quant[4];   // quantizer values for each segments
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int segment_level[4];   // filtering strength for each segments [0..63]
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int alpha_data_size;    // size of the transparency data
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int layer_data_size;    // size of the enhancement layer data
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // lossless encoder statistics
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32_t lossless_features;  // bit0:predictor bit1:cross-color transform
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               // bit2:subtract-green bit3:color indexing
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int histogram_bits;          // number of precision bits of histogram
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int transform_bits;          // precision bits for transform
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int cache_bits;              // number of bits for color cache lookup
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int palette_size;            // number of color in palette, if used
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int lossless_size;           // final lossless size
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32_t pad[4];        // padding for later use
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Signature for output function. Should return true if writing was successful.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// data/data_size is the segment of data to write, and 'picture' is for
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// reference (and so one can make use of picture->custom_ptr).
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size,
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const WebPPicture* picture);
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WebPMemoryWrite: a special WebPWriterFunction that writes to memory using
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the following WebPMemoryWriter object (to be set as a custom_ptr).
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct WebPMemoryWriter {
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint8_t* mem;       // final buffer (of size 'max_size', larger than 'size').
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t   size;      // final size
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t   max_size;  // total capacity
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32_t pad[1];    // padding for later use
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The following must be called first before any use.
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(void) WebPMemoryWriterInit(WebPMemoryWriter* writer);
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// completion, writer.mem and writer.size will hold the coded data.
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// writer.mem must be freed using the call 'free(writer.mem)'.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPMemoryWrite(const uint8_t* data, size_t data_size,
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const WebPPicture* picture);
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Progress hook, called from time to time to report progress. It can return
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// false to request an abort of the encoding process, or true otherwise if
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// everything is OK.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int (*WebPProgressHook)(int percent, const WebPPicture* picture);
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Color spaces.
235eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochtypedef enum WebPEncCSP {
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // chroma sampling
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_YUV420 = 0,   // 4:2:0
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_YUV422 = 1,   // 4:2:2
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_YUV444 = 2,   // 4:4:4
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_YUV400 = 3,   // grayscale
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_CSP_UV_MASK = 3,   // bit-mask to get the UV sampling factors
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // alpha channel variants
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_YUV420A = 4,
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_YUV422A = 5,
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_YUV444A = 6,
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_YUV400A = 7,   // grayscale + alpha
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WEBP_CSP_ALPHA_BIT = 4   // bit that is set if alpha is present
248eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} WebPEncCSP;
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Encoding error conditions.
251eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochtypedef enum WebPEncodingError {
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_OK = 0,
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_OUT_OF_MEMORY,            // memory error allocating objects
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY,  // memory error while flushing bits
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_NULL_PARAMETER,           // a pointer parameter is NULL
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_INVALID_CONFIGURATION,    // configuration is invalid
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_BAD_DIMENSION,            // picture has invalid width/height
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_PARTITION0_OVERFLOW,      // partition is bigger than 512k
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_PARTITION_OVERFLOW,       // partition is bigger than 16M
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_BAD_WRITE,                // error while flushing bytes
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_FILE_TOO_BIG,             // file is bigger than 4G
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_USER_ABORT,               // abort request by user
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VP8_ENC_ERROR_LAST                      // list terminator. always last.
264eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} WebPEncodingError;
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// maximum width/height allowed (inclusive), in pixels
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define WEBP_MAX_DIMENSION 16383
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Main exchange structure (input samples, output bytes, statistics)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct WebPPicture {
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   INPUT
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //////////////
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Main flag for encoder selecting between ARGB or YUV input.
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is recommended to use ARGB input (*argb, argb_stride) for lossless
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // compression, and YUV input (*y, *u, *v, etc.) for lossy compression
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // since these are the respective native colorspace for these formats.
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int use_argb;
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // YUV input (mostly used for input to lossy compression)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebPEncCSP colorspace;     // colorspace: should be YUV420 for now (=Y'CbCr).
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int width, height;         // dimensions (less or equal to WEBP_MAX_DIMENSION)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint8_t *y, *u, *v;        // pointers to luma/chroma planes.
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int y_stride, uv_stride;   // luma/chroma strides.
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint8_t* a;                // pointer to the alpha plane
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int a_stride;              // stride of the alpha plane
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32_t pad1[2];          // padding for later use
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ARGB input (mostly used for input to lossless compression)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32_t* argb;            // Pointer to argb (32 bit) plane.
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int argb_stride;           // This is stride in pixels units, not bytes.
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32_t pad2[3];          // padding for later use
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   OUTPUT
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///////////////
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Byte-emission hook, to store compressed bytes as they are ready.
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebPWriterFunction writer;  // can be NULL
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* custom_ptr;           // can be used by the writer.
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // map for extra information (only for lossy compression mode)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int extra_info_type;    // 1: intra type, 2: segment, 3: quant
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // 4: intra-16 prediction mode,
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // 5: chroma prediction mode,
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // 6: bit cost, 7: distortion
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint8_t* extra_info;    // if not NULL, points to an array of size
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // ((width + 15) / 16) * ((height + 15) / 16) that
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // will be filled with a macroblock map, depending
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // on extra_info_type.
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   STATS AND REPORTS
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///////////////////////////
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Pointer to side statistics (updated only if not NULL)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebPAuxStats* stats;
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Error code for the latest error encountered during encoding
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebPEncodingError error_code;
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If not NULL, report progress during encoding.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebPProgressHook progress_hook;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* user_data;        // this field is free to be set to any value and
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          // used during callbacks (like progress-report e.g.).
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32_t pad3[3];       // padding for later use
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unused for now: original samples (for non-YUV420 modes)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint8_t *u0, *v0;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int uv0_stride;
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32_t pad4[7];       // padding for later use
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // PRIVATE FIELDS
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ////////////////////
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* memory_;          // row chunk of memory for yuva planes
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* memory_argb_;     // and for argb too.
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* pad5[2];          // padding for later use
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Internal, version-checked, entry point
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureInitInternal(WebPPicture*, int);
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Should always be called, to initialize the structure. Returns false in case
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of version mismatch. WebPPictureInit() must have succeeded before using the
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 'picture' object.
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that, by default, use_argb is false and colorspace is WEBP_YUV420.
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static WEBP_INLINE int WebPPictureInit(WebPPicture* picture) {
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION);
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WebPPicture utils
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Convenience allocation / deallocation based on picture->width/height:
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Allocate y/u/v buffers as per colorspace/width/height specification.
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note! This function will free the previous buffer if needed.
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of memory error.
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureAlloc(WebPPicture* picture);
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Release the memory allocated by WebPPictureAlloc() or WebPPictureImport*().
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that this function does _not_ free the memory used by the 'picture'
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// object itself.
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Besides memory (which is reclaimed) all other fields of 'picture' are
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// preserved.
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(void) WebPPictureFree(WebPPicture* picture);
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copy the pixels of *src into *dst, using WebPPictureAlloc. Upon return, *dst
366eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// will fully own the copied pixels (this is not a view). The 'dst' picture need
367eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// not be initialized as its content is overwritten.
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of memory allocation error.
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst);
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Compute PSNR, SSIM or LSIM distortion metric between two pictures.
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order.
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns false in case of error (src and ref don't have same dimension, ...)
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Warning: this function is rather CPU-intensive.
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureDistortion(
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const WebPPicture* src, const WebPPicture* ref,
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int metric_type,           // 0 = PSNR, 1 = SSIM, 2 = LSIM
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    float result[5]);
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// self-crops a picture to the rectangle defined by top/left/width/height.
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of memory allocation error, or if the rectangle is
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// outside of the source picture.
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The rectangle for the view is defined by the top-left corner pixel
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// coordinates (left, top) as well as its width and height. This rectangle
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// must be fully be comprised inside the 'src' source picture. If the source
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// picture uses the YUV420 colorspace, the top and left coordinates will be
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// snapped to even values.
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureCrop(WebPPicture* picture,
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 int left, int top, int width, int height);
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Extracts a view from 'src' picture into 'dst'. The rectangle for the view
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is defined by the top-left corner pixel coordinates (left, top) as well
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as its width and height. This rectangle must be fully be comprised inside
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the 'src' source picture. If the source picture uses the YUV420 colorspace,
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the top and left coordinates will be snapped to even values.
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Picture 'src' must out-live 'dst' picture. Self-extraction of view is allowed
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ('src' equal to 'dst') as a mean of fast-cropping (but note that doing so,
398eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// the original dimension will be lost). Picture 'dst' need not be initialized
399eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// with WebPPictureInit() if it is different from 'src', since its content will
400eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// be overwritten.
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of memory allocation error or invalid parameters.
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureView(const WebPPicture* src,
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 int left, int top, int width, int height,
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 WebPPicture* dst);
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the 'picture' is actually a view and therefore does
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not own the memory for pixels.
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureIsView(const WebPPicture* picture);
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Rescale a picture to new dimension width x height.
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Now gamma correction is applied.
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of error (invalid parameter or insufficient memory).
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureRescale(WebPPicture* pic, int width, int height);
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Colorspace conversion function to import RGB samples.
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Previous buffer will be free'd, if any.
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// *rgb buffer should have a size of at least height * rgb_stride.
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of memory error.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureImportRGB(
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebPPicture* picture, const uint8_t* rgb, int rgb_stride);
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Same, but for RGBA buffer.
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureImportRGBA(
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebPPicture* picture, const uint8_t* rgba, int rgba_stride);
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Same, but for RGBA buffer. Imports the RGB direct from the 32-bit format
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// input buffer ignoring the alpha channel. Avoids needing to copy the data
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to a temporary 24-bit RGB buffer to import the RGB only.
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureImportRGBX(
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebPPicture* picture, const uint8_t* rgbx, int rgbx_stride);
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Variants of the above, but taking BGR(A|X) input.
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureImportBGR(
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebPPicture* picture, const uint8_t* bgr, int bgr_stride);
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureImportBGRA(
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebPPicture* picture, const uint8_t* bgra, int bgra_stride);
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureImportBGRX(
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebPPicture* picture, const uint8_t* bgrx, int bgrx_stride);
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Converts picture->argb data to the YUVA format specified by 'colorspace'.
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Upon return, picture->use_argb is set to false. The presence of real
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// non-opaque transparent values is detected, and 'colorspace' will be
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// adjusted accordingly. Note that this method is lossy.
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of error.
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureARGBToYUVA(WebPPicture* picture,
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       WebPEncCSP colorspace);
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Converts picture->yuv to picture->argb and sets picture->use_argb to true.
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The input format must be YUV_420 or YUV_420A.
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that the use of this method is discouraged if one has access to the
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// raw ARGB samples, since using YUV420 is comparatively lossy. Also, the
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// conversion from YUV420 to ARGB incurs a small loss too.
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of error.
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureYUVAToARGB(WebPPicture* picture);
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper function: given a width x height plane of YUV(A) samples
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (with stride 'stride'), clean-up the YUV samples under fully transparent
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// area, to help compressibility (no guarantee, though).
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(void) WebPCleanupTransparentArea(WebPPicture* picture);
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Scan the picture 'picture' for the presence of non fully opaque alpha values.
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true in such case. Otherwise returns false (indicating that the
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// alpha plane can be ignored altogether e.g.).
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPPictureHasTransparency(const WebPPicture* picture);
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Main call
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Main encoding call, after config and picture have been initialized.
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 'picture' must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION),
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and the 'config' object must be a valid one.
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false in case of error, true otherwise.
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In case of error, picture->error_code is updated accordingly.
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 'picture' can hold the source samples in both YUV(A) or ARGB input, depending
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// on the value of 'picture->use_argb'. It is highly recommended to use
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the former for lossy encoding, and the latter for lossless encoding
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (when config.lossless is true). Automatic conversion from one format to
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// another is provided but they both incur some loss.
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBP_EXTERN(int) WebPEncode(const WebPConfig* config, WebPPicture* picture);
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(__cplusplus) || defined(c_plusplus)
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}    // extern "C"
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  /* WEBP_WEBP_ENCODE_H_ */
486