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