blob: b3f05b1ffcf8dfbefe529bb7c3fe98fb240409b6 [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Vikas Arora7c970a02011-06-16 15:56:45 +05302//
Vikas Arora0406ce12013-08-09 15:57:12 -07003// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
Vikas Arora7c970a02011-06-16 15:56:45 +05308// -----------------------------------------------------------------------------
9//
10// WebP encoder: main interface
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#ifndef WEBP_WEBP_ENCODE_H_
15#define WEBP_WEBP_ENCODE_H_
16
Vikas Arora46672792011-07-13 16:37:55 +053017#include "./types.h"
Vikas Arora7c970a02011-06-16 15:56:45 +053018
Vikas Arora8b720222014-01-02 16:48:02 -080019#ifdef __cplusplus
Vikas Arora7c970a02011-06-16 15:56:45 +053020extern "C" {
21#endif
22
Vikas Arora8b720222014-01-02 16:48:02 -080023#define WEBP_ENCODER_ABI_VERSION 0x0202 // MAJOR(8b) + MINOR(8b)
Vikas Arora1e7bf882013-03-13 16:43:18 -070024
Vikas Arora0406ce12013-08-09 15:57:12 -070025// Note: forward declaring enumerations is not allowed in (strict) C and C++,
26// the types are left here for reference.
27// typedef enum WebPImageHint WebPImageHint;
28// typedef enum WebPEncCSP WebPEncCSP;
29// typedef enum WebPPreset WebPPreset;
30// typedef enum WebPEncodingError WebPEncodingError;
Vikas Arora1e7bf882013-03-13 16:43:18 -070031typedef struct WebPConfig WebPConfig;
32typedef struct WebPPicture WebPPicture; // main structure for I/O
33typedef struct WebPAuxStats WebPAuxStats;
34typedef struct WebPMemoryWriter WebPMemoryWriter;
Vikas Arora7c970a02011-06-16 15:56:45 +053035
36// Return the encoder's version number, packed in hexadecimal using 8bits for
37// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
Vikas Arora46672792011-07-13 16:37:55 +053038WEBP_EXTERN(int) WebPGetEncoderVersion(void);
Vikas Arora7c970a02011-06-16 15:56:45 +053039
Vikas Aroraa2415722012-08-09 16:18:58 -070040//------------------------------------------------------------------------------
Vikas Arora7c970a02011-06-16 15:56:45 +053041// One-stop-shop call! No questions asked:
42
43// Returns the size of the compressed data (pointed to by *output), or 0 if
44// an error occurred. The compressed data must be released by the caller
45// using the call 'free(*output)'.
Vikas Aroraa2415722012-08-09 16:18:58 -070046// These functions compress using the lossy format, and the quality_factor
47// can go from 0 (smaller output, lower quality) to 100 (best quality,
48// larger output).
Vikas Arora46672792011-07-13 16:37:55 +053049WEBP_EXTERN(size_t) WebPEncodeRGB(const uint8_t* rgb,
50 int width, int height, int stride,
51 float quality_factor, uint8_t** output);
52WEBP_EXTERN(size_t) WebPEncodeBGR(const uint8_t* bgr,
53 int width, int height, int stride,
54 float quality_factor, uint8_t** output);
55WEBP_EXTERN(size_t) WebPEncodeRGBA(const uint8_t* rgba,
56 int width, int height, int stride,
57 float quality_factor, uint8_t** output);
58WEBP_EXTERN(size_t) WebPEncodeBGRA(const uint8_t* bgra,
59 int width, int height, int stride,
60 float quality_factor, uint8_t** output);
Vikas Arora7c970a02011-06-16 15:56:45 +053061
Vikas Aroraa2415722012-08-09 16:18:58 -070062// These functions are the equivalent of the above, but compressing in a
63// lossless manner. Files are usually larger than lossy format, but will
64// not suffer any compression loss.
65WEBP_EXTERN(size_t) WebPEncodeLosslessRGB(const uint8_t* rgb,
66 int width, int height, int stride,
67 uint8_t** output);
68WEBP_EXTERN(size_t) WebPEncodeLosslessBGR(const uint8_t* bgr,
69 int width, int height, int stride,
70 uint8_t** output);
71WEBP_EXTERN(size_t) WebPEncodeLosslessRGBA(const uint8_t* rgba,
72 int width, int height, int stride,
73 uint8_t** output);
74WEBP_EXTERN(size_t) WebPEncodeLosslessBGRA(const uint8_t* bgra,
75 int width, int height, int stride,
76 uint8_t** output);
77
78//------------------------------------------------------------------------------
Vikas Arora7c970a02011-06-16 15:56:45 +053079// Coding parameters
80
Vikas Aroraa2415722012-08-09 16:18:58 -070081// Image characteristics hint for the underlying encoder.
Vikas Arora0406ce12013-08-09 15:57:12 -070082typedef enum WebPImageHint {
Vikas Aroraa2415722012-08-09 16:18:58 -070083 WEBP_HINT_DEFAULT = 0, // default preset.
84 WEBP_HINT_PICTURE, // digital picture, like portrait, inner shot
85 WEBP_HINT_PHOTO, // outdoor photograph, with natural lighting
86 WEBP_HINT_GRAPH, // Discrete tone image (graph, map-tile etc).
87 WEBP_HINT_LAST
Vikas Arora0406ce12013-08-09 15:57:12 -070088} WebPImageHint;
Vikas Arora7c970a02011-06-16 15:56:45 +053089
Vikas Arora1e7bf882013-03-13 16:43:18 -070090// Compression parameters.
91struct WebPConfig {
Vikas Aroraa2415722012-08-09 16:18:58 -070092 int lossless; // Lossless encoding (0=lossy(default), 1=lossless).
93 float quality; // between 0 (smallest file) and 100 (biggest)
94 int method; // quality/speed trade-off (0=fast, 6=slower-better)
95
96 WebPImageHint image_hint; // Hint for image type (lossless only for now).
97
98 // Parameters related to lossy compression only:
99 int target_size; // if non-zero, set the desired target size in bytes.
100 // Takes precedence over the 'compression' parameter.
101 float target_PSNR; // if non-zero, specifies the minimal distortion to
102 // try to achieve. Takes precedence over target_size.
103 int segments; // maximum number of segments to use, in [1..4]
104 int sns_strength; // Spatial Noise Shaping. 0=off, 100=maximum.
105 int filter_strength; // range: [0 = off .. 100 = strongest]
106 int filter_sharpness; // range: [0 = off .. 7 = least sharp]
107 int filter_type; // filtering type: 0 = simple, 1 = strong (only used
108 // if filter_strength > 0 or autofilter > 0)
109 int autofilter; // Auto adjust filter's strength [0 = off, 1 = on]
110 int alpha_compression; // Algorithm for encoding the alpha plane (0 = none,
111 // 1 = compressed with WebP lossless). Default is 1.
112 int alpha_filtering; // Predictive filtering method for alpha plane.
113 // 0: none, 1: fast, 2: best. Default if 1.
114 int alpha_quality; // Between 0 (smallest size) and 100 (lossless).
115 // Default is 100.
116 int pass; // number of entropy-analysis passes (in [1..10]).
117
118 int show_compressed; // if true, export the compressed picture back.
119 // In-loop filtering is not applied.
Vikas Arora8b720222014-01-02 16:48:02 -0800120 int preprocessing; // preprocessing filter:
121 // 0=none, 1=segment-smooth, 2=pseudo-random dithering
Vikas Aroraa2415722012-08-09 16:18:58 -0700122 int partitions; // log2(number of token partitions) in [0..3]. Default
123 // is set to 0 for easier progressive decoding.
124 int partition_limit; // quality degradation allowed to fit the 512k limit
125 // on prediction modes coding (0: no degradation,
126 // 100: maximum possible degradation).
Vikas Arora1e7bf882013-03-13 16:43:18 -0700127 int emulate_jpeg_size; // If true, compression parameters will be remapped
128 // to better match the expected output size from
129 // JPEG compression. Generally, the output size will
130 // be similar but the degradation will be lower.
131 int thread_level; // If non-zero, try and use multi-threaded encoding.
132 int low_memory; // If set, reduce memory usage (but increase CPU use).
Vikas Aroraa2415722012-08-09 16:18:58 -0700133
Vikas Arora1e7bf882013-03-13 16:43:18 -0700134 uint32_t pad[5]; // padding for later use
135};
Vikas Arora7c970a02011-06-16 15:56:45 +0530136
137// Enumerate some predefined settings for WebPConfig, depending on the type
138// of source picture. These presets are used when calling WebPConfigPreset().
Vikas Arora0406ce12013-08-09 15:57:12 -0700139typedef enum WebPPreset {
Vikas Arora7c970a02011-06-16 15:56:45 +0530140 WEBP_PRESET_DEFAULT = 0, // default preset.
141 WEBP_PRESET_PICTURE, // digital picture, like portrait, inner shot
142 WEBP_PRESET_PHOTO, // outdoor photograph, with natural lighting
143 WEBP_PRESET_DRAWING, // hand or line drawing, with high-contrast details
144 WEBP_PRESET_ICON, // small-sized colorful images
145 WEBP_PRESET_TEXT // text-like
Vikas Arora0406ce12013-08-09 15:57:12 -0700146} WebPPreset;
Vikas Arora7c970a02011-06-16 15:56:45 +0530147
148// Internal, version-checked, entry point
Vikas Aroraa2415722012-08-09 16:18:58 -0700149WEBP_EXTERN(int) WebPConfigInitInternal(WebPConfig*, WebPPreset, float, int);
Vikas Arora7c970a02011-06-16 15:56:45 +0530150
151// Should always be called, to initialize a fresh WebPConfig structure before
Vikas Aroraa2415722012-08-09 16:18:58 -0700152// modification. Returns false in case of version mismatch. WebPConfigInit()
153// must have succeeded before using the 'config' object.
154// Note that the default values are lossless=0 and quality=75.
155static WEBP_INLINE int WebPConfigInit(WebPConfig* config) {
Vikas Arora7c970a02011-06-16 15:56:45 +0530156 return WebPConfigInitInternal(config, WEBP_PRESET_DEFAULT, 75.f,
157 WEBP_ENCODER_ABI_VERSION);
158}
159
160// This function will initialize the configuration according to a predefined
161// set of parameters (referred to by 'preset') and a given quality factor.
162// This function can be called as a replacement to WebPConfigInit(). Will
Vikas Aroraa2415722012-08-09 16:18:58 -0700163// return false in case of error.
164static WEBP_INLINE int WebPConfigPreset(WebPConfig* config,
165 WebPPreset preset, float quality) {
Vikas Arora7c970a02011-06-16 15:56:45 +0530166 return WebPConfigInitInternal(config, preset, quality,
167 WEBP_ENCODER_ABI_VERSION);
168}
169
Vikas Arora33f74da2014-07-25 13:53:32 -0700170#if WEBP_ENCODER_ABI_VERSION > 0x0202
171// Activate the lossless compression mode with the desired efficiency level
172// between 0 (fastest, lowest compression) and 9 (slower, best compression).
173// A good default level is '6', providing a fair tradeoff between compression
174// speed and final compressed size.
175// This function will overwrite several fields from config: 'method', 'quality'
176// and 'lossless'. Returns false in case of parameter error.
177WEBP_EXTERN(int) WebPConfigLosslessPreset(WebPConfig* config, int level);
178#endif
179
Vikas Aroraa2415722012-08-09 16:18:58 -0700180// Returns true if 'config' is non-NULL and all configuration parameters are
181// within their valid ranges.
182WEBP_EXTERN(int) WebPValidateConfig(const WebPConfig* config);
Vikas Arora7c970a02011-06-16 15:56:45 +0530183
Vikas Aroraa2415722012-08-09 16:18:58 -0700184//------------------------------------------------------------------------------
Vikas Arora7c970a02011-06-16 15:56:45 +0530185// Input / Output
Derek Sollenbergerb6dbce62013-02-07 18:38:54 +0000186// Structure for storing auxiliary statistics (mostly for lossy encoding).
Vikas Arora1e7bf882013-03-13 16:43:18 -0700187
188struct WebPAuxStats {
Vikas Arora7c970a02011-06-16 15:56:45 +0530189 int coded_size; // final size
Vikas Aroraa2415722012-08-09 16:18:58 -0700190
191 float PSNR[5]; // peak-signal-to-noise ratio for Y/U/V/All/Alpha
Vikas Arora7c970a02011-06-16 15:56:45 +0530192 int block_count[3]; // number of intra4/intra16/skipped macroblocks
Vikas Arora88fe2b82011-07-22 14:49:55 +0530193 int header_bytes[2]; // approximate number of bytes spent for header
Vikas Arora7c970a02011-06-16 15:56:45 +0530194 // and mode-partition #0
Vikas Arora88fe2b82011-07-22 14:49:55 +0530195 int residual_bytes[3][4]; // approximate number of bytes spent for
Vikas Arora7c970a02011-06-16 15:56:45 +0530196 // DC/AC/uv coefficients for each (0..3) segments.
197 int segment_size[4]; // number of macroblocks in each segments
198 int segment_quant[4]; // quantizer values for each segments
199 int segment_level[4]; // filtering strength for each segments [0..63]
Vikas Arora46672792011-07-13 16:37:55 +0530200
201 int alpha_data_size; // size of the transparency data
202 int layer_data_size; // size of the enhancement layer data
Vikas Aroraa2415722012-08-09 16:18:58 -0700203
204 // lossless encoder statistics
205 uint32_t lossless_features; // bit0:predictor bit1:cross-color transform
206 // bit2:subtract-green bit3:color indexing
207 int histogram_bits; // number of precision bits of histogram
208 int transform_bits; // precision bits for transform
209 int cache_bits; // number of bits for color cache lookup
210 int palette_size; // number of color in palette, if used
211 int lossless_size; // final lossless size
212
213 uint32_t pad[4]; // padding for later use
Vikas Arora1e7bf882013-03-13 16:43:18 -0700214};
Vikas Arora7c970a02011-06-16 15:56:45 +0530215
Vikas Aroraa2415722012-08-09 16:18:58 -0700216// Signature for output function. Should return true if writing was successful.
Vikas Arora7c970a02011-06-16 15:56:45 +0530217// data/data_size is the segment of data to write, and 'picture' is for
218// reference (and so one can make use of picture->custom_ptr).
219typedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size,
Vikas Aroraa2415722012-08-09 16:18:58 -0700220 const WebPPicture* picture);
221
222// WebPMemoryWrite: a special WebPWriterFunction that writes to memory using
223// the following WebPMemoryWriter object (to be set as a custom_ptr).
Vikas Arora1e7bf882013-03-13 16:43:18 -0700224struct WebPMemoryWriter {
Vikas Aroraa2415722012-08-09 16:18:58 -0700225 uint8_t* mem; // final buffer (of size 'max_size', larger than 'size').
226 size_t size; // final size
227 size_t max_size; // total capacity
228 uint32_t pad[1]; // padding for later use
Vikas Arora1e7bf882013-03-13 16:43:18 -0700229};
Vikas Aroraa2415722012-08-09 16:18:58 -0700230
231// The following must be called first before any use.
232WEBP_EXTERN(void) WebPMemoryWriterInit(WebPMemoryWriter* writer);
233
Vikas Arora8c098652015-01-28 14:08:58 -0800234#if WEBP_ENCODER_ABI_VERSION > 0x0203
Vikas Arora33f74da2014-07-25 13:53:32 -0700235// The following must be called to deallocate writer->mem memory. The 'writer'
236// object itself is not deallocated.
237WEBP_EXTERN(void) WebPMemoryWriterClear(WebPMemoryWriter* writer);
238#endif
Vikas Aroraa2415722012-08-09 16:18:58 -0700239// The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon
240// completion, writer.mem and writer.size will hold the coded data.
Vikas Arora8c098652015-01-28 14:08:58 -0800241#if WEBP_ENCODER_ABI_VERSION > 0x0203
Vikas Arora33f74da2014-07-25 13:53:32 -0700242// writer.mem must be freed by calling WebPMemoryWriterClear.
243#else
244// writer.mem must be freed by calling 'free(writer.mem)'.
245#endif
Vikas Aroraa2415722012-08-09 16:18:58 -0700246WEBP_EXTERN(int) WebPMemoryWrite(const uint8_t* data, size_t data_size,
247 const WebPPicture* picture);
248
249// Progress hook, called from time to time to report progress. It can return
250// false to request an abort of the encoding process, or true otherwise if
251// everything is OK.
252typedef int (*WebPProgressHook)(int percent, const WebPPicture* picture);
Vikas Arora7c970a02011-06-16 15:56:45 +0530253
Vikas Arora1e7bf882013-03-13 16:43:18 -0700254// Color spaces.
Vikas Arora0406ce12013-08-09 15:57:12 -0700255typedef enum WebPEncCSP {
Vikas Arora46672792011-07-13 16:37:55 +0530256 // chroma sampling
Vikas Arora33f74da2014-07-25 13:53:32 -0700257 WEBP_YUV420 = 0, // 4:2:0
258 WEBP_YUV420A = 4, // alpha channel variant
259 WEBP_CSP_UV_MASK = 3, // bit-mask to get the UV sampling factors
Vikas Arora46672792011-07-13 16:37:55 +0530260 WEBP_CSP_ALPHA_BIT = 4 // bit that is set if alpha is present
Vikas Arora0406ce12013-08-09 15:57:12 -0700261} WebPEncCSP;
Vikas Arora46672792011-07-13 16:37:55 +0530262
263// Encoding error conditions.
Vikas Arora0406ce12013-08-09 15:57:12 -0700264typedef enum WebPEncodingError {
Vikas Arora46672792011-07-13 16:37:55 +0530265 VP8_ENC_OK = 0,
266 VP8_ENC_ERROR_OUT_OF_MEMORY, // memory error allocating objects
267 VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY, // memory error while flushing bits
268 VP8_ENC_ERROR_NULL_PARAMETER, // a pointer parameter is NULL
269 VP8_ENC_ERROR_INVALID_CONFIGURATION, // configuration is invalid
270 VP8_ENC_ERROR_BAD_DIMENSION, // picture has invalid width/height
Vikas Aroraa2415722012-08-09 16:18:58 -0700271 VP8_ENC_ERROR_PARTITION0_OVERFLOW, // partition is bigger than 512k
272 VP8_ENC_ERROR_PARTITION_OVERFLOW, // partition is bigger than 16M
Vikas Arora46672792011-07-13 16:37:55 +0530273 VP8_ENC_ERROR_BAD_WRITE, // error while flushing bytes
Vikas Aroraa2415722012-08-09 16:18:58 -0700274 VP8_ENC_ERROR_FILE_TOO_BIG, // file is bigger than 4G
275 VP8_ENC_ERROR_USER_ABORT, // abort request by user
276 VP8_ENC_ERROR_LAST // list terminator. always last.
Vikas Arora0406ce12013-08-09 15:57:12 -0700277} WebPEncodingError;
Vikas Arora46672792011-07-13 16:37:55 +0530278
Vikas Aroraa2415722012-08-09 16:18:58 -0700279// maximum width/height allowed (inclusive), in pixels
280#define WEBP_MAX_DIMENSION 16383
281
282// Main exchange structure (input samples, output bytes, statistics)
Vikas Arora7c970a02011-06-16 15:56:45 +0530283struct WebPPicture {
Vikas Aroraa2415722012-08-09 16:18:58 -0700284 // INPUT
285 //////////////
286 // Main flag for encoder selecting between ARGB or YUV input.
287 // It is recommended to use ARGB input (*argb, argb_stride) for lossless
288 // compression, and YUV input (*y, *u, *v, etc.) for lossy compression
289 // since these are the respective native colorspace for these formats.
290 int use_argb;
291
292 // YUV input (mostly used for input to lossy compression)
Vikas Arora46672792011-07-13 16:37:55 +0530293 WebPEncCSP colorspace; // colorspace: should be YUV420 for now (=Y'CbCr).
Vikas Aroraa2415722012-08-09 16:18:58 -0700294 int width, height; // dimensions (less or equal to WEBP_MAX_DIMENSION)
Vikas Arora7c970a02011-06-16 15:56:45 +0530295 uint8_t *y, *u, *v; // pointers to luma/chroma planes.
296 int y_stride, uv_stride; // luma/chroma strides.
Vikas Aroraa2415722012-08-09 16:18:58 -0700297 uint8_t* a; // pointer to the alpha plane
Vikas Arora46672792011-07-13 16:37:55 +0530298 int a_stride; // stride of the alpha plane
Vikas Aroraa2415722012-08-09 16:18:58 -0700299 uint32_t pad1[2]; // padding for later use
Vikas Arora7c970a02011-06-16 15:56:45 +0530300
Vikas Aroraa2415722012-08-09 16:18:58 -0700301 // ARGB input (mostly used for input to lossless compression)
302 uint32_t* argb; // Pointer to argb (32 bit) plane.
303 int argb_stride; // This is stride in pixels units, not bytes.
304 uint32_t pad2[3]; // padding for later use
305
306 // OUTPUT
307 ///////////////
308 // Byte-emission hook, to store compressed bytes as they are ready.
Vikas Arora7c970a02011-06-16 15:56:45 +0530309 WebPWriterFunction writer; // can be NULL
310 void* custom_ptr; // can be used by the writer.
311
Vikas Aroraa2415722012-08-09 16:18:58 -0700312 // map for extra information (only for lossy compression mode)
Vikas Arora7c970a02011-06-16 15:56:45 +0530313 int extra_info_type; // 1: intra type, 2: segment, 3: quant
314 // 4: intra-16 prediction mode,
315 // 5: chroma prediction mode,
316 // 6: bit cost, 7: distortion
317 uint8_t* extra_info; // if not NULL, points to an array of size
318 // ((width + 15) / 16) * ((height + 15) / 16) that
319 // will be filled with a macroblock map, depending
320 // on extra_info_type.
321
Vikas Aroraa2415722012-08-09 16:18:58 -0700322 // STATS AND REPORTS
323 ///////////////////////////
324 // Pointer to side statistics (updated only if not NULL)
Vikas Arora7c970a02011-06-16 15:56:45 +0530325 WebPAuxStats* stats;
Vikas Arora46672792011-07-13 16:37:55 +0530326
Vikas Aroraa2415722012-08-09 16:18:58 -0700327 // Error code for the latest error encountered during encoding
328 WebPEncodingError error_code;
329
330 // If not NULL, report progress during encoding.
331 WebPProgressHook progress_hook;
332
333 void* user_data; // this field is free to be set to any value and
334 // used during callbacks (like progress-report e.g.).
335
336 uint32_t pad3[3]; // padding for later use
337
Vikas Arora33f74da2014-07-25 13:53:32 -0700338 // Unused for now
339 uint8_t *pad4, *pad5;
340 uint32_t pad6[8]; // padding for later use
Vikas Aroraa2415722012-08-09 16:18:58 -0700341
342 // PRIVATE FIELDS
343 ////////////////////
344 void* memory_; // row chunk of memory for yuva planes
345 void* memory_argb_; // and for argb too.
Vikas Arora33f74da2014-07-25 13:53:32 -0700346 void* pad7[2]; // padding for later use
Vikas Arora7c970a02011-06-16 15:56:45 +0530347};
348
349// Internal, version-checked, entry point
Vikas Aroraa2415722012-08-09 16:18:58 -0700350WEBP_EXTERN(int) WebPPictureInitInternal(WebPPicture*, int);
Vikas Arora7c970a02011-06-16 15:56:45 +0530351
Vikas Aroraa2415722012-08-09 16:18:58 -0700352// Should always be called, to initialize the structure. Returns false in case
353// of version mismatch. WebPPictureInit() must have succeeded before using the
Vikas Arora7c970a02011-06-16 15:56:45 +0530354// 'picture' object.
Vikas Aroraa2415722012-08-09 16:18:58 -0700355// Note that, by default, use_argb is false and colorspace is WEBP_YUV420.
356static WEBP_INLINE int WebPPictureInit(WebPPicture* picture) {
Vikas Arora7c970a02011-06-16 15:56:45 +0530357 return WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION);
358}
359
Vikas Aroraa2415722012-08-09 16:18:58 -0700360//------------------------------------------------------------------------------
Vikas Arora7c970a02011-06-16 15:56:45 +0530361// WebPPicture utils
362
363// Convenience allocation / deallocation based on picture->width/height:
Vikas Arora46672792011-07-13 16:37:55 +0530364// Allocate y/u/v buffers as per colorspace/width/height specification.
Vikas Arora7c970a02011-06-16 15:56:45 +0530365// Note! This function will free the previous buffer if needed.
Vikas Aroraa2415722012-08-09 16:18:58 -0700366// Returns false in case of memory error.
367WEBP_EXTERN(int) WebPPictureAlloc(WebPPicture* picture);
Vikas Arora7c970a02011-06-16 15:56:45 +0530368
Vikas Aroraa2415722012-08-09 16:18:58 -0700369// Release the memory allocated by WebPPictureAlloc() or WebPPictureImport*().
370// Note that this function does _not_ free the memory used by the 'picture'
371// object itself.
372// Besides memory (which is reclaimed) all other fields of 'picture' are
373// preserved.
374WEBP_EXTERN(void) WebPPictureFree(WebPPicture* picture);
Vikas Arora7c970a02011-06-16 15:56:45 +0530375
Vikas Arora0406ce12013-08-09 15:57:12 -0700376// Copy the pixels of *src into *dst, using WebPPictureAlloc. Upon return, *dst
377// will fully own the copied pixels (this is not a view). The 'dst' picture need
378// not be initialized as its content is overwritten.
Vikas Aroraa2415722012-08-09 16:18:58 -0700379// Returns false in case of memory allocation error.
380WEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst);
381
382// Compute PSNR, SSIM or LSIM distortion metric between two pictures.
383// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order.
Vikas Arora1e7bf882013-03-13 16:43:18 -0700384// Returns false in case of error (src and ref don't have same dimension, ...)
Vikas Aroraa2415722012-08-09 16:18:58 -0700385// Warning: this function is rather CPU-intensive.
386WEBP_EXTERN(int) WebPPictureDistortion(
Vikas Arora1e7bf882013-03-13 16:43:18 -0700387 const WebPPicture* src, const WebPPicture* ref,
Vikas Aroraa2415722012-08-09 16:18:58 -0700388 int metric_type, // 0 = PSNR, 1 = SSIM, 2 = LSIM
389 float result[5]);
Vikas Arora7c970a02011-06-16 15:56:45 +0530390
391// self-crops a picture to the rectangle defined by top/left/width/height.
Vikas Aroraa2415722012-08-09 16:18:58 -0700392// Returns false in case of memory allocation error, or if the rectangle is
Vikas Arora7c970a02011-06-16 15:56:45 +0530393// outside of the source picture.
Vikas Aroraa2415722012-08-09 16:18:58 -0700394// The rectangle for the view is defined by the top-left corner pixel
395// coordinates (left, top) as well as its width and height. This rectangle
396// must be fully be comprised inside the 'src' source picture. If the source
397// picture uses the YUV420 colorspace, the top and left coordinates will be
398// snapped to even values.
399WEBP_EXTERN(int) WebPPictureCrop(WebPPicture* picture,
Vikas Arora46672792011-07-13 16:37:55 +0530400 int left, int top, int width, int height);
Vikas Arora7c970a02011-06-16 15:56:45 +0530401
Vikas Aroraa2415722012-08-09 16:18:58 -0700402// Extracts a view from 'src' picture into 'dst'. The rectangle for the view
403// is defined by the top-left corner pixel coordinates (left, top) as well
404// as its width and height. This rectangle must be fully be comprised inside
405// the 'src' source picture. If the source picture uses the YUV420 colorspace,
406// the top and left coordinates will be snapped to even values.
407// Picture 'src' must out-live 'dst' picture. Self-extraction of view is allowed
408// ('src' equal to 'dst') as a mean of fast-cropping (but note that doing so,
Vikas Arora0406ce12013-08-09 15:57:12 -0700409// the original dimension will be lost). Picture 'dst' need not be initialized
410// with WebPPictureInit() if it is different from 'src', since its content will
411// be overwritten.
Vikas Aroraa2415722012-08-09 16:18:58 -0700412// Returns false in case of memory allocation error or invalid parameters.
413WEBP_EXTERN(int) WebPPictureView(const WebPPicture* src,
414 int left, int top, int width, int height,
415 WebPPicture* dst);
416
417// Returns true if the 'picture' is actually a view and therefore does
418// not own the memory for pixels.
419WEBP_EXTERN(int) WebPPictureIsView(const WebPPicture* picture);
420
Vikas Arora46672792011-07-13 16:37:55 +0530421// Rescale a picture to new dimension width x height.
James Zern9e80ee92015-03-17 18:54:21 -0700422// If either 'width' or 'height' (but not both) is 0 the corresponding
423// dimension will be calculated preserving the aspect ratio.
424// No gamma correction is applied.
Vikas Arora46672792011-07-13 16:37:55 +0530425// Returns false in case of error (invalid parameter or insufficient memory).
Vikas Aroraa2415722012-08-09 16:18:58 -0700426WEBP_EXTERN(int) WebPPictureRescale(WebPPicture* pic, int width, int height);
Vikas Arora46672792011-07-13 16:37:55 +0530427
428// Colorspace conversion function to import RGB samples.
429// Previous buffer will be free'd, if any.
Vikas Arora7c970a02011-06-16 15:56:45 +0530430// *rgb buffer should have a size of at least height * rgb_stride.
Vikas Aroraa2415722012-08-09 16:18:58 -0700431// Returns false in case of memory error.
Vikas Arora46672792011-07-13 16:37:55 +0530432WEBP_EXTERN(int) WebPPictureImportRGB(
Vikas Aroraa2415722012-08-09 16:18:58 -0700433 WebPPicture* picture, const uint8_t* rgb, int rgb_stride);
434// Same, but for RGBA buffer.
Vikas Arora46672792011-07-13 16:37:55 +0530435WEBP_EXTERN(int) WebPPictureImportRGBA(
Vikas Aroraa2415722012-08-09 16:18:58 -0700436 WebPPicture* picture, const uint8_t* rgba, int rgba_stride);
437// Same, but for RGBA buffer. Imports the RGB direct from the 32-bit format
438// input buffer ignoring the alpha channel. Avoids needing to copy the data
439// to a temporary 24-bit RGB buffer to import the RGB only.
440WEBP_EXTERN(int) WebPPictureImportRGBX(
441 WebPPicture* picture, const uint8_t* rgbx, int rgbx_stride);
Vikas Arora7c970a02011-06-16 15:56:45 +0530442
Vikas Aroraa2415722012-08-09 16:18:58 -0700443// Variants of the above, but taking BGR(A|X) input.
Vikas Arora46672792011-07-13 16:37:55 +0530444WEBP_EXTERN(int) WebPPictureImportBGR(
Vikas Aroraa2415722012-08-09 16:18:58 -0700445 WebPPicture* picture, const uint8_t* bgr, int bgr_stride);
Vikas Arora46672792011-07-13 16:37:55 +0530446WEBP_EXTERN(int) WebPPictureImportBGRA(
Vikas Aroraa2415722012-08-09 16:18:58 -0700447 WebPPicture* picture, const uint8_t* bgra, int bgra_stride);
448WEBP_EXTERN(int) WebPPictureImportBGRX(
449 WebPPicture* picture, const uint8_t* bgrx, int bgrx_stride);
Vikas Arora7c970a02011-06-16 15:56:45 +0530450
Vikas Arora8c098652015-01-28 14:08:58 -0800451// Converts picture->argb data to the YUV420A format. The 'colorspace'
452// parameter is deprecated and should be equal to WEBP_YUV420.
Vikas Aroraa2415722012-08-09 16:18:58 -0700453// Upon return, picture->use_argb is set to false. The presence of real
454// non-opaque transparent values is detected, and 'colorspace' will be
455// adjusted accordingly. Note that this method is lossy.
456// Returns false in case of error.
457WEBP_EXTERN(int) WebPPictureARGBToYUVA(WebPPicture* picture,
Vikas Arora8c098652015-01-28 14:08:58 -0800458 WebPEncCSP /*colorspace = WEBP_YUV420*/);
Vikas Aroraa2415722012-08-09 16:18:58 -0700459
Vikas Arora8b720222014-01-02 16:48:02 -0800460// Same as WebPPictureARGBToYUVA(), but the conversion is done using
461// pseudo-random dithering with a strength 'dithering' between
462// 0.0 (no dithering) and 1.0 (maximum dithering). This is useful
463// for photographic picture.
464WEBP_EXTERN(int) WebPPictureARGBToYUVADithered(
465 WebPPicture* picture, WebPEncCSP colorspace, float dithering);
466
Vikas Arora8c098652015-01-28 14:08:58 -0800467#if WEBP_ENCODER_ABI_VERSION > 0x0204
468// Performs 'smart' RGBA->YUVA420 downsampling and colorspace conversion.
469// Downsampling is handled with extra care in case of color clipping. This
470// method is roughly 2x slower than WebPPictureARGBToYUVA() but produces better
471// YUV representation.
472// Returns false in case of error.
473WEBP_EXTERN(int) WebPPictureSmartARGBToYUVA(WebPPicture* picture);
474#endif
475
Vikas Aroraa2415722012-08-09 16:18:58 -0700476// Converts picture->yuv to picture->argb and sets picture->use_argb to true.
477// The input format must be YUV_420 or YUV_420A.
478// Note that the use of this method is discouraged if one has access to the
479// raw ARGB samples, since using YUV420 is comparatively lossy. Also, the
480// conversion from YUV420 to ARGB incurs a small loss too.
481// Returns false in case of error.
482WEBP_EXTERN(int) WebPPictureYUVAToARGB(WebPPicture* picture);
483
Vikas Arora33f74da2014-07-25 13:53:32 -0700484// Helper function: given a width x height plane of RGBA or YUV(A) samples
485// clean-up the YUV or RGB samples under fully transparent area, to help
486// compressibility (no guarantee, though).
Vikas Aroraa2415722012-08-09 16:18:58 -0700487WEBP_EXTERN(void) WebPCleanupTransparentArea(WebPPicture* picture);
488
489// Scan the picture 'picture' for the presence of non fully opaque alpha values.
490// Returns true in such case. Otherwise returns false (indicating that the
491// alpha plane can be ignored altogether e.g.).
492WEBP_EXTERN(int) WebPPictureHasTransparency(const WebPPicture* picture);
493
Vikas Arora8b720222014-01-02 16:48:02 -0800494// Remove the transparency information (if present) by blending the color with
495// the background color 'background_rgb' (specified as 24bit RGB triplet).
496// After this call, all alpha values are reset to 0xff.
497WEBP_EXTERN(void) WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb);
498
Vikas Aroraa2415722012-08-09 16:18:58 -0700499//------------------------------------------------------------------------------
Vikas Arora7c970a02011-06-16 15:56:45 +0530500// Main call
501
Vikas Arora88fe2b82011-07-22 14:49:55 +0530502// Main encoding call, after config and picture have been initialized.
Vikas Aroraa2415722012-08-09 16:18:58 -0700503// 'picture' must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION),
504// and the 'config' object must be a valid one.
Vikas Arora7c970a02011-06-16 15:56:45 +0530505// Returns false in case of error, true otherwise.
Vikas Arora46672792011-07-13 16:37:55 +0530506// In case of error, picture->error_code is updated accordingly.
Vikas Aroraa2415722012-08-09 16:18:58 -0700507// 'picture' can hold the source samples in both YUV(A) or ARGB input, depending
508// on the value of 'picture->use_argb'. It is highly recommended to use
509// the former for lossy encoding, and the latter for lossless encoding
510// (when config.lossless is true). Automatic conversion from one format to
511// another is provided but they both incur some loss.
512WEBP_EXTERN(int) WebPEncode(const WebPConfig* config, WebPPicture* picture);
Vikas Arora7c970a02011-06-16 15:56:45 +0530513
Vikas Aroraa2415722012-08-09 16:18:58 -0700514//------------------------------------------------------------------------------
Vikas Arora7c970a02011-06-16 15:56:45 +0530515
Vikas Arora8b720222014-01-02 16:48:02 -0800516#ifdef __cplusplus
Vikas Arora7c970a02011-06-16 15:56:45 +0530517} // extern "C"
518#endif
519
520#endif /* WEBP_WEBP_ENCODE_H_ */