1a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Copyright 2011 Google Inc. All Rights Reserved.
27c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora//
37c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// This code is licensed under the same terms as WebM:
47c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora//  Software License Agreement:  http://www.webmproject.org/license/software/
57c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
67c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// -----------------------------------------------------------------------------
77c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora//
87c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora//   WebP encoder: main interface
97c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora//
107c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Author: Skal (pascal.massimino@gmail.com)
117c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
127c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#ifndef WEBP_WEBP_ENCODE_H_
137c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#define WEBP_WEBP_ENCODE_H_
147c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
15466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#include "./types.h"
167c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
177c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#if defined(__cplusplus) || defined(c_plusplus)
187c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Aroraextern "C" {
197c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#endif
207c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
211e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora#define WEBP_ENCODER_ABI_VERSION 0x0201    // MAJOR(8b) + MINOR(8b)
221e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora
231e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora#if !(defined(__cplusplus) || defined(c_plusplus))
241e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef enum WebPImageHint WebPImageHint;
251e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef enum WebPEncCSP WebPEncCSP;
261e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef enum WebPPreset WebPPreset;
271e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef enum WebPEncodingError WebPEncodingError;
281e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora#endif
291e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPConfig WebPConfig;
301e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPPicture WebPPicture;   // main structure for I/O
311e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPAuxStats WebPAuxStats;
321e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPMemoryWriter WebPMemoryWriter;
337c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
347c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Return the encoder's version number, packed in hexadecimal using 8bits for
357c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
36466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPGetEncoderVersion(void);
377c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
38a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
397c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// One-stop-shop call! No questions asked:
407c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
417c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Returns the size of the compressed data (pointed to by *output), or 0 if
427c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// an error occurred. The compressed data must be released by the caller
437c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// using the call 'free(*output)'.
44a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// These functions compress using the lossy format, and the quality_factor
45a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// can go from 0 (smaller output, lower quality) to 100 (best quality,
46a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// larger output).
47466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(size_t) WebPEncodeRGB(const uint8_t* rgb,
48466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                  int width, int height, int stride,
49466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                  float quality_factor, uint8_t** output);
50466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(size_t) WebPEncodeBGR(const uint8_t* bgr,
51466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                  int width, int height, int stride,
52466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                  float quality_factor, uint8_t** output);
53466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(size_t) WebPEncodeRGBA(const uint8_t* rgba,
54466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                   int width, int height, int stride,
55466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                   float quality_factor, uint8_t** output);
56466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(size_t) WebPEncodeBGRA(const uint8_t* bgra,
57466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                   int width, int height, int stride,
58466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                   float quality_factor, uint8_t** output);
597c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
60a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// These functions are the equivalent of the above, but compressing in a
61a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// lossless manner. Files are usually larger than lossy format, but will
62a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// not suffer any compression loss.
63a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(size_t) WebPEncodeLosslessRGB(const uint8_t* rgb,
64a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          int width, int height, int stride,
65a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          uint8_t** output);
66a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(size_t) WebPEncodeLosslessBGR(const uint8_t* bgr,
67a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          int width, int height, int stride,
68a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          uint8_t** output);
69a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(size_t) WebPEncodeLosslessRGBA(const uint8_t* rgba,
70a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                           int width, int height, int stride,
71a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                           uint8_t** output);
72a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(size_t) WebPEncodeLosslessBGRA(const uint8_t* bgra,
73a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                           int width, int height, int stride,
74a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                           uint8_t** output);
75a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
76a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
777c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Coding parameters
787c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
79a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Image characteristics hint for the underlying encoder.
801e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroraenum WebPImageHint {
81a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WEBP_HINT_DEFAULT = 0,  // default preset.
82a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WEBP_HINT_PICTURE,      // digital picture, like portrait, inner shot
83a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WEBP_HINT_PHOTO,        // outdoor photograph, with natural lighting
84a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WEBP_HINT_GRAPH,        // Discrete tone image (graph, map-tile etc).
85a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WEBP_HINT_LAST
861e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora};
87a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
881e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Compression parameters.
891e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPConfig {
90a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int lossless;           // Lossless encoding (0=lossy(default), 1=lossless).
91a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  float quality;          // between 0 (smallest file) and 100 (biggest)
92a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int method;             // quality/speed trade-off (0=fast, 6=slower-better)
93a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
94a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WebPImageHint image_hint;  // Hint for image type (lossless only for now).
95a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
96a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Parameters related to lossy compression only:
97a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int target_size;        // if non-zero, set the desired target size in bytes.
98a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // Takes precedence over the 'compression' parameter.
99a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  float target_PSNR;      // if non-zero, specifies the minimal distortion to
100a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // try to achieve. Takes precedence over target_size.
101a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int segments;           // maximum number of segments to use, in [1..4]
102a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int sns_strength;       // Spatial Noise Shaping. 0=off, 100=maximum.
103a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int filter_strength;    // range: [0 = off .. 100 = strongest]
104a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int filter_sharpness;   // range: [0 = off .. 7 = least sharp]
105a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int filter_type;        // filtering type: 0 = simple, 1 = strong (only used
106a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // if filter_strength > 0 or autofilter > 0)
107a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int autofilter;         // Auto adjust filter's strength [0 = off, 1 = on]
108a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int alpha_compression;  // Algorithm for encoding the alpha plane (0 = none,
109a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // 1 = compressed with WebP lossless). Default is 1.
110a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int alpha_filtering;    // Predictive filtering method for alpha plane.
111a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          //  0: none, 1: fast, 2: best. Default if 1.
112a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int alpha_quality;      // Between 0 (smallest size) and 100 (lossless).
113a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // Default is 100.
114a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int pass;               // number of entropy-analysis passes (in [1..10]).
115a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
116a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int show_compressed;    // if true, export the compressed picture back.
117a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // In-loop filtering is not applied.
118a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int preprocessing;      // preprocessing filter (0=none, 1=segment-smooth)
119a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int partitions;         // log2(number of token partitions) in [0..3]. Default
120a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // is set to 0 for easier progressive decoding.
121a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int partition_limit;    // quality degradation allowed to fit the 512k limit
122a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // on prediction modes coding (0: no degradation,
123a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // 100: maximum possible degradation).
1241e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora  int emulate_jpeg_size;  // If true, compression parameters will be remapped
1251e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora                          // to better match the expected output size from
1261e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora                          // JPEG compression. Generally, the output size will
1271e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora                          // be similar but the degradation will be lower.
1281e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora  int thread_level;       // If non-zero, try and use multi-threaded encoding.
1291e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora  int low_memory;         // If set, reduce memory usage (but increase CPU use).
1301e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora
1311e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora  uint32_t pad[5];        // padding for later use
1321e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora};
1337c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
1347c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Enumerate some predefined settings for WebPConfig, depending on the type
1357c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// of source picture. These presets are used when calling WebPConfigPreset().
1361e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroraenum WebPPreset {
1377c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  WEBP_PRESET_DEFAULT = 0,  // default preset.
1387c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  WEBP_PRESET_PICTURE,      // digital picture, like portrait, inner shot
1397c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  WEBP_PRESET_PHOTO,        // outdoor photograph, with natural lighting
1407c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  WEBP_PRESET_DRAWING,      // hand or line drawing, with high-contrast details
1417c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  WEBP_PRESET_ICON,         // small-sized colorful images
1427c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  WEBP_PRESET_TEXT          // text-like
1431e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora};
1447c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
1457c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Internal, version-checked, entry point
146a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPConfigInitInternal(WebPConfig*, WebPPreset, float, int);
1477c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
1487c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Should always be called, to initialize a fresh WebPConfig structure before
149a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// modification. Returns false in case of version mismatch. WebPConfigInit()
150a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// must have succeeded before using the 'config' object.
151a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that the default values are lossless=0 and quality=75.
152a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPConfigInit(WebPConfig* config) {
1537c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  return WebPConfigInitInternal(config, WEBP_PRESET_DEFAULT, 75.f,
1547c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                                WEBP_ENCODER_ABI_VERSION);
1557c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora}
1567c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
1577c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// This function will initialize the configuration according to a predefined
1587c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// set of parameters (referred to by 'preset') and a given quality factor.
1597c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// This function can be called as a replacement to WebPConfigInit(). Will
160a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// return false in case of error.
161a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPConfigPreset(WebPConfig* config,
162a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                        WebPPreset preset, float quality) {
1637c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  return WebPConfigInitInternal(config, preset, quality,
1647c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                                WEBP_ENCODER_ABI_VERSION);
1657c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora}
1667c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
167a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns true if 'config' is non-NULL and all configuration parameters are
168a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// within their valid ranges.
169a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPValidateConfig(const WebPConfig* config);
1707c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
171a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
1727c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Input / Output
173b6dbce6bfeaabde2a7b581c4c6888d532d32f3acDerek Sollenberger// Structure for storing auxiliary statistics (mostly for lossy encoding).
1741e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora
1751e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPAuxStats {
1767c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  int coded_size;         // final size
177a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
178a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  float PSNR[5];          // peak-signal-to-noise ratio for Y/U/V/All/Alpha
1797c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  int block_count[3];     // number of intra4/intra16/skipped macroblocks
18088fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora  int header_bytes[2];    // approximate number of bytes spent for header
1817c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                          // and mode-partition #0
18288fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora  int residual_bytes[3][4];  // approximate number of bytes spent for
1837c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                             // DC/AC/uv coefficients for each (0..3) segments.
1847c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  int segment_size[4];    // number of macroblocks in each segments
1857c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  int segment_quant[4];   // quantizer values for each segments
1867c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  int segment_level[4];   // filtering strength for each segments [0..63]
187466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
188466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  int alpha_data_size;    // size of the transparency data
189466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  int layer_data_size;    // size of the enhancement layer data
190a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
191a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // lossless encoder statistics
192a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t lossless_features;  // bit0:predictor bit1:cross-color transform
193a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                               // bit2:subtract-green bit3:color indexing
194a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int histogram_bits;          // number of precision bits of histogram
195a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int transform_bits;          // precision bits for transform
196a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int cache_bits;              // number of bits for color cache lookup
197a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int palette_size;            // number of color in palette, if used
198a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int lossless_size;           // final lossless size
199a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
200a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t pad[4];        // padding for later use
2011e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora};
2027c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
203a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Signature for output function. Should return true if writing was successful.
2047c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// data/data_size is the segment of data to write, and 'picture' is for
2057c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// reference (and so one can make use of picture->custom_ptr).
2067c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Aroratypedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size,
207a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                  const WebPPicture* picture);
208a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
209a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// WebPMemoryWrite: a special WebPWriterFunction that writes to memory using
210a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the following WebPMemoryWriter object (to be set as a custom_ptr).
2111e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPMemoryWriter {
212a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint8_t* mem;       // final buffer (of size 'max_size', larger than 'size').
213a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  size_t   size;      // final size
214a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  size_t   max_size;  // total capacity
215a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t pad[1];    // padding for later use
2161e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora};
217a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
218a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The following must be called first before any use.
219a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(void) WebPMemoryWriterInit(WebPMemoryWriter* writer);
220a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
221a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon
222a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// completion, writer.mem and writer.size will hold the coded data.
2231e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// writer.mem must be freed using the call 'free(writer.mem)'.
224a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPMemoryWrite(const uint8_t* data, size_t data_size,
225a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                 const WebPPicture* picture);
226a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
227a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Progress hook, called from time to time to report progress. It can return
228a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// false to request an abort of the encoding process, or true otherwise if
229a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// everything is OK.
230a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroratypedef int (*WebPProgressHook)(int percent, const WebPPicture* picture);
2317c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
2321e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Color spaces.
2331e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroraenum WebPEncCSP {
234466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  // chroma sampling
235466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_YUV420 = 0,   // 4:2:0
236466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_YUV422 = 1,   // 4:2:2
237466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_YUV444 = 2,   // 4:4:4
238466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_YUV400 = 3,   // grayscale
239466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_CSP_UV_MASK = 3,   // bit-mask to get the UV sampling factors
240466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  // alpha channel variants
241466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_YUV420A = 4,
242466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_YUV422A = 5,
243466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_YUV444A = 6,
244466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_YUV400A = 7,   // grayscale + alpha
245466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WEBP_CSP_ALPHA_BIT = 4   // bit that is set if alpha is present
2461e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora};
247466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
248466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Encoding error conditions.
2491e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroraenum WebPEncodingError {
250466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8_ENC_OK = 0,
251466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8_ENC_ERROR_OUT_OF_MEMORY,            // memory error allocating objects
252466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY,  // memory error while flushing bits
253466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8_ENC_ERROR_NULL_PARAMETER,           // a pointer parameter is NULL
254466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8_ENC_ERROR_INVALID_CONFIGURATION,    // configuration is invalid
255466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8_ENC_ERROR_BAD_DIMENSION,            // picture has invalid width/height
256a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  VP8_ENC_ERROR_PARTITION0_OVERFLOW,      // partition is bigger than 512k
257a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  VP8_ENC_ERROR_PARTITION_OVERFLOW,       // partition is bigger than 16M
258466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8_ENC_ERROR_BAD_WRITE,                // error while flushing bytes
259a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  VP8_ENC_ERROR_FILE_TOO_BIG,             // file is bigger than 4G
260a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  VP8_ENC_ERROR_USER_ABORT,               // abort request by user
261a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  VP8_ENC_ERROR_LAST                      // list terminator. always last.
2621e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora};
263466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
264a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// maximum width/height allowed (inclusive), in pixels
265a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#define WEBP_MAX_DIMENSION 16383
266a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
267a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Main exchange structure (input samples, output bytes, statistics)
2687c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arorastruct WebPPicture {
269a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
270a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  //   INPUT
271a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  //////////////
272a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Main flag for encoder selecting between ARGB or YUV input.
273a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // It is recommended to use ARGB input (*argb, argb_stride) for lossless
274a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // compression, and YUV input (*y, *u, *v, etc.) for lossy compression
275a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // since these are the respective native colorspace for these formats.
276a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int use_argb;
277a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
278a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // YUV input (mostly used for input to lossy compression)
279466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPEncCSP colorspace;     // colorspace: should be YUV420 for now (=Y'CbCr).
280a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int width, height;         // dimensions (less or equal to WEBP_MAX_DIMENSION)
2817c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  uint8_t *y, *u, *v;        // pointers to luma/chroma planes.
2827c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  int y_stride, uv_stride;   // luma/chroma strides.
283a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint8_t* a;                // pointer to the alpha plane
284466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  int a_stride;              // stride of the alpha plane
285a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t pad1[2];          // padding for later use
286a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
287a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // ARGB input (mostly used for input to lossless compression)
288a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t* argb;            // Pointer to argb (32 bit) plane.
289a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int argb_stride;           // This is stride in pixels units, not bytes.
290a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t pad2[3];          // padding for later use
2917c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
292a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  //   OUTPUT
293a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  ///////////////
294a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Byte-emission hook, to store compressed bytes as they are ready.
2957c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  WebPWriterFunction writer;  // can be NULL
2967c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  void* custom_ptr;           // can be used by the writer.
2977c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
298a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // map for extra information (only for lossy compression mode)
2997c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  int extra_info_type;    // 1: intra type, 2: segment, 3: quant
3007c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                          // 4: intra-16 prediction mode,
3017c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                          // 5: chroma prediction mode,
3027c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                          // 6: bit cost, 7: distortion
3037c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  uint8_t* extra_info;    // if not NULL, points to an array of size
3047c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                          // ((width + 15) / 16) * ((height + 15) / 16) that
3057c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                          // will be filled with a macroblock map, depending
3067c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora                          // on extra_info_type.
3077c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
308a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  //   STATS AND REPORTS
309a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  ///////////////////////////
310a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Pointer to side statistics (updated only if not NULL)
3117c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  WebPAuxStats* stats;
312466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
313a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Error code for the latest error encountered during encoding
314a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WebPEncodingError error_code;
315a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
316a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // If not NULL, report progress during encoding.
317a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WebPProgressHook progress_hook;
318a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
319a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  void* user_data;        // this field is free to be set to any value and
320a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          // used during callbacks (like progress-report e.g.).
321a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
322a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t pad3[3];       // padding for later use
323a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
324a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Unused for now: original samples (for non-YUV420 modes)
325466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  uint8_t *u0, *v0;
326466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  int uv0_stride;
327466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
328a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t pad4[7];       // padding for later use
329a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
330a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // PRIVATE FIELDS
331a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  ////////////////////
332a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  void* memory_;          // row chunk of memory for yuva planes
333a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  void* memory_argb_;     // and for argb too.
334a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  void* pad5[2];          // padding for later use
3357c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora};
3367c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
3377c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Internal, version-checked, entry point
338a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureInitInternal(WebPPicture*, int);
3397c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
340a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Should always be called, to initialize the structure. Returns false in case
341a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// of version mismatch. WebPPictureInit() must have succeeded before using the
3427c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// 'picture' object.
343a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that, by default, use_argb is false and colorspace is WEBP_YUV420.
344a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPPictureInit(WebPPicture* picture) {
3457c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora  return WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION);
3467c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora}
3477c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
348a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
3497c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// WebPPicture utils
3507c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
3517c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Convenience allocation / deallocation based on picture->width/height:
352466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Allocate y/u/v buffers as per colorspace/width/height specification.
3537c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Note! This function will free the previous buffer if needed.
354a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory error.
355a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureAlloc(WebPPicture* picture);
356a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
357a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Release the memory allocated by WebPPictureAlloc() or WebPPictureImport*().
358a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that this function does _not_ free the memory used by the 'picture'
359a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// object itself.
360a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Besides memory (which is reclaimed) all other fields of 'picture' are
361a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// preserved.
362a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(void) WebPPictureFree(WebPPicture* picture);
363a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
364a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Copy the pixels of *src into *dst, using WebPPictureAlloc. Upon return,
365a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// *dst will fully own the copied pixels (this is not a view).
366a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory allocation error.
367a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst);
368a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
369a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Compute PSNR, SSIM or LSIM distortion metric between two pictures.
370a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order.
3711e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Returns false in case of error (src and ref don't have same dimension, ...)
372a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Warning: this function is rather CPU-intensive.
373a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureDistortion(
3741e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora    const WebPPicture* src, const WebPPicture* ref,
375a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    int metric_type,           // 0 = PSNR, 1 = SSIM, 2 = LSIM
376a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    float result[5]);
3777c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
3787c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// self-crops a picture to the rectangle defined by top/left/width/height.
379a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory allocation error, or if the rectangle is
3807c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// outside of the source picture.
381a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The rectangle for the view is defined by the top-left corner pixel
382a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// coordinates (left, top) as well as its width and height. This rectangle
383a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// must be fully be comprised inside the 'src' source picture. If the source
384a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// picture uses the YUV420 colorspace, the top and left coordinates will be
385a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// snapped to even values.
386a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureCrop(WebPPicture* picture,
387466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                 int left, int top, int width, int height);
388466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
389a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Extracts a view from 'src' picture into 'dst'. The rectangle for the view
390a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// is defined by the top-left corner pixel coordinates (left, top) as well
391a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// as its width and height. This rectangle must be fully be comprised inside
392a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the 'src' source picture. If the source picture uses the YUV420 colorspace,
393a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the top and left coordinates will be snapped to even values.
394a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Picture 'src' must out-live 'dst' picture. Self-extraction of view is allowed
395a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// ('src' equal to 'dst') as a mean of fast-cropping (but note that doing so,
396a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the original dimension will be lost).
397a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory allocation error or invalid parameters.
398a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureView(const WebPPicture* src,
399a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                 int left, int top, int width, int height,
400a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                 WebPPicture* dst);
401a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
402a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns true if the 'picture' is actually a view and therefore does
403a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// not own the memory for pixels.
404a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureIsView(const WebPPicture* picture);
405a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
406466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Rescale a picture to new dimension width x height.
407466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Now gamma correction is applied.
408466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Returns false in case of error (invalid parameter or insufficient memory).
409a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureRescale(WebPPicture* pic, int width, int height);
4107c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
411466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Colorspace conversion function to import RGB samples.
412466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Previous buffer will be free'd, if any.
4137c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// *rgb buffer should have a size of at least height * rgb_stride.
414a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory error.
415466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPPictureImportRGB(
416a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    WebPPicture* picture, const uint8_t* rgb, int rgb_stride);
417a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same, but for RGBA buffer.
418466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPPictureImportRGBA(
419a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    WebPPicture* picture, const uint8_t* rgba, int rgba_stride);
420a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same, but for RGBA buffer. Imports the RGB direct from the 32-bit format
421a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// input buffer ignoring the alpha channel. Avoids needing to copy the data
422a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// to a temporary 24-bit RGB buffer to import the RGB only.
423a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureImportRGBX(
424a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    WebPPicture* picture, const uint8_t* rgbx, int rgbx_stride);
425a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
426a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Variants of the above, but taking BGR(A|X) input.
427466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPPictureImportBGR(
428a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    WebPPicture* picture, const uint8_t* bgr, int bgr_stride);
429466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPPictureImportBGRA(
430a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    WebPPicture* picture, const uint8_t* bgra, int bgra_stride);
431a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureImportBGRX(
432a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    WebPPicture* picture, const uint8_t* bgrx, int bgrx_stride);
433a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
434a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Converts picture->argb data to the YUVA format specified by 'colorspace'.
435a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Upon return, picture->use_argb is set to false. The presence of real
436a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// non-opaque transparent values is detected, and 'colorspace' will be
437a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// adjusted accordingly. Note that this method is lossy.
438a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of error.
439a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureARGBToYUVA(WebPPicture* picture,
440a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                       WebPEncCSP colorspace);
441a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
442a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Converts picture->yuv to picture->argb and sets picture->use_argb to true.
443a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The input format must be YUV_420 or YUV_420A.
444a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that the use of this method is discouraged if one has access to the
445a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// raw ARGB samples, since using YUV420 is comparatively lossy. Also, the
446a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// conversion from YUV420 to ARGB incurs a small loss too.
447a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of error.
448a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureYUVAToARGB(WebPPicture* picture);
449a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
450a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Helper function: given a width x height plane of YUV(A) samples
451a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// (with stride 'stride'), clean-up the YUV samples under fully transparent
452a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// area, to help compressibility (no guarantee, though).
453a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(void) WebPCleanupTransparentArea(WebPPicture* picture);
454a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
455a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Scan the picture 'picture' for the presence of non fully opaque alpha values.
456a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns true in such case. Otherwise returns false (indicating that the
457a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// alpha plane can be ignored altogether e.g.).
458a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureHasTransparency(const WebPPicture* picture);
459a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
460a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
4617c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Main call
4627c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
46388fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora// Main encoding call, after config and picture have been initialized.
464a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 'picture' must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION),
465a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// and the 'config' object must be a valid one.
4667c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Returns false in case of error, true otherwise.
467466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// In case of error, picture->error_code is updated accordingly.
468a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 'picture' can hold the source samples in both YUV(A) or ARGB input, depending
469a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// on the value of 'picture->use_argb'. It is highly recommended to use
470a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the former for lossy encoding, and the latter for lossless encoding
471a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// (when config.lossless is true). Automatic conversion from one format to
472a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// another is provided but they both incur some loss.
473a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPEncode(const WebPConfig* config, WebPPicture* picture);
474a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
475a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
4767c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
4777c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#if defined(__cplusplus) || defined(c_plusplus)
4787c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora}    // extern "C"
4797c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#endif
4807c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora
4817c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#endif  /* WEBP_WEBP_ENCODE_H_ */
482