jpayne@69
|
1 // Copyright 2011 Google Inc. All Rights Reserved.
|
jpayne@69
|
2 //
|
jpayne@69
|
3 // Use of this source code is governed by a BSD-style license
|
jpayne@69
|
4 // that can be found in the COPYING file in the root of the source
|
jpayne@69
|
5 // tree. An additional intellectual property rights grant can be found
|
jpayne@69
|
6 // in the file PATENTS. All contributing project authors may
|
jpayne@69
|
7 // be found in the AUTHORS file in the root of the source tree.
|
jpayne@69
|
8 // -----------------------------------------------------------------------------
|
jpayne@69
|
9 //
|
jpayne@69
|
10 // WebP encoder: main interface
|
jpayne@69
|
11 //
|
jpayne@69
|
12 // Author: Skal (pascal.massimino@gmail.com)
|
jpayne@69
|
13
|
jpayne@69
|
14 #ifndef WEBP_WEBP_ENCODE_H_
|
jpayne@69
|
15 #define WEBP_WEBP_ENCODE_H_
|
jpayne@69
|
16
|
jpayne@69
|
17 #include "./types.h"
|
jpayne@69
|
18
|
jpayne@69
|
19 #ifdef __cplusplus
|
jpayne@69
|
20 extern "C" {
|
jpayne@69
|
21 #endif
|
jpayne@69
|
22
|
jpayne@69
|
23 #define WEBP_ENCODER_ABI_VERSION 0x0210 // MAJOR(8b) + MINOR(8b)
|
jpayne@69
|
24
|
jpayne@69
|
25 // Note: forward declaring enumerations is not allowed in (strict) C and C++,
|
jpayne@69
|
26 // the types are left here for reference.
|
jpayne@69
|
27 // typedef enum WebPImageHint WebPImageHint;
|
jpayne@69
|
28 // typedef enum WebPEncCSP WebPEncCSP;
|
jpayne@69
|
29 // typedef enum WebPPreset WebPPreset;
|
jpayne@69
|
30 // typedef enum WebPEncodingError WebPEncodingError;
|
jpayne@69
|
31 typedef struct WebPConfig WebPConfig;
|
jpayne@69
|
32 typedef struct WebPPicture WebPPicture; // main structure for I/O
|
jpayne@69
|
33 typedef struct WebPAuxStats WebPAuxStats;
|
jpayne@69
|
34 typedef struct WebPMemoryWriter WebPMemoryWriter;
|
jpayne@69
|
35
|
jpayne@69
|
36 // Return the encoder's version number, packed in hexadecimal using 8bits for
|
jpayne@69
|
37 // each of major/minor/revision. E.g: v2.5.7 is 0x020507.
|
jpayne@69
|
38 WEBP_EXTERN int WebPGetEncoderVersion(void);
|
jpayne@69
|
39
|
jpayne@69
|
40 //------------------------------------------------------------------------------
|
jpayne@69
|
41 // One-stop-shop call! No questions asked:
|
jpayne@69
|
42
|
jpayne@69
|
43 // Returns the size of the compressed data (pointed to by *output), or 0 if
|
jpayne@69
|
44 // an error occurred. The compressed data must be released by the caller
|
jpayne@69
|
45 // using the call 'WebPFree(*output)'.
|
jpayne@69
|
46 // These functions compress using the lossy format, and the quality_factor
|
jpayne@69
|
47 // can go from 0 (smaller output, lower quality) to 100 (best quality,
|
jpayne@69
|
48 // larger output).
|
jpayne@69
|
49 WEBP_EXTERN size_t WebPEncodeRGB(const uint8_t* rgb,
|
jpayne@69
|
50 int width, int height, int stride,
|
jpayne@69
|
51 float quality_factor, uint8_t** output);
|
jpayne@69
|
52 WEBP_EXTERN size_t WebPEncodeBGR(const uint8_t* bgr,
|
jpayne@69
|
53 int width, int height, int stride,
|
jpayne@69
|
54 float quality_factor, uint8_t** output);
|
jpayne@69
|
55 WEBP_EXTERN size_t WebPEncodeRGBA(const uint8_t* rgba,
|
jpayne@69
|
56 int width, int height, int stride,
|
jpayne@69
|
57 float quality_factor, uint8_t** output);
|
jpayne@69
|
58 WEBP_EXTERN size_t WebPEncodeBGRA(const uint8_t* bgra,
|
jpayne@69
|
59 int width, int height, int stride,
|
jpayne@69
|
60 float quality_factor, uint8_t** output);
|
jpayne@69
|
61
|
jpayne@69
|
62 // These functions are the equivalent of the above, but compressing in a
|
jpayne@69
|
63 // lossless manner. Files are usually larger than lossy format, but will
|
jpayne@69
|
64 // not suffer any compression loss.
|
jpayne@69
|
65 // Note these functions, like the lossy versions, use the library's default
|
jpayne@69
|
66 // settings. For lossless this means 'exact' is disabled. RGB values in
|
jpayne@69
|
67 // transparent areas will be modified to improve compression. To avoid this,
|
jpayne@69
|
68 // use WebPEncode() and set WebPConfig::exact to 1.
|
jpayne@69
|
69 WEBP_EXTERN size_t WebPEncodeLosslessRGB(const uint8_t* rgb,
|
jpayne@69
|
70 int width, int height, int stride,
|
jpayne@69
|
71 uint8_t** output);
|
jpayne@69
|
72 WEBP_EXTERN size_t WebPEncodeLosslessBGR(const uint8_t* bgr,
|
jpayne@69
|
73 int width, int height, int stride,
|
jpayne@69
|
74 uint8_t** output);
|
jpayne@69
|
75 WEBP_EXTERN size_t WebPEncodeLosslessRGBA(const uint8_t* rgba,
|
jpayne@69
|
76 int width, int height, int stride,
|
jpayne@69
|
77 uint8_t** output);
|
jpayne@69
|
78 WEBP_EXTERN size_t WebPEncodeLosslessBGRA(const uint8_t* bgra,
|
jpayne@69
|
79 int width, int height, int stride,
|
jpayne@69
|
80 uint8_t** output);
|
jpayne@69
|
81
|
jpayne@69
|
82 //------------------------------------------------------------------------------
|
jpayne@69
|
83 // Coding parameters
|
jpayne@69
|
84
|
jpayne@69
|
85 // Image characteristics hint for the underlying encoder.
|
jpayne@69
|
86 typedef enum WebPImageHint {
|
jpayne@69
|
87 WEBP_HINT_DEFAULT = 0, // default preset.
|
jpayne@69
|
88 WEBP_HINT_PICTURE, // digital picture, like portrait, inner shot
|
jpayne@69
|
89 WEBP_HINT_PHOTO, // outdoor photograph, with natural lighting
|
jpayne@69
|
90 WEBP_HINT_GRAPH, // Discrete tone image (graph, map-tile etc).
|
jpayne@69
|
91 WEBP_HINT_LAST
|
jpayne@69
|
92 } WebPImageHint;
|
jpayne@69
|
93
|
jpayne@69
|
94 // Compression parameters.
|
jpayne@69
|
95 struct WebPConfig {
|
jpayne@69
|
96 int lossless; // Lossless encoding (0=lossy(default), 1=lossless).
|
jpayne@69
|
97 float quality; // between 0 and 100. For lossy, 0 gives the smallest
|
jpayne@69
|
98 // size and 100 the largest. For lossless, this
|
jpayne@69
|
99 // parameter is the amount of effort put into the
|
jpayne@69
|
100 // compression: 0 is the fastest but gives larger
|
jpayne@69
|
101 // files compared to the slowest, but best, 100.
|
jpayne@69
|
102 int method; // quality/speed trade-off (0=fast, 6=slower-better)
|
jpayne@69
|
103
|
jpayne@69
|
104 WebPImageHint image_hint; // Hint for image type (lossless only for now).
|
jpayne@69
|
105
|
jpayne@69
|
106 int target_size; // if non-zero, set the desired target size in bytes.
|
jpayne@69
|
107 // Takes precedence over the 'compression' parameter.
|
jpayne@69
|
108 float target_PSNR; // if non-zero, specifies the minimal distortion to
|
jpayne@69
|
109 // try to achieve. Takes precedence over target_size.
|
jpayne@69
|
110 int segments; // maximum number of segments to use, in [1..4]
|
jpayne@69
|
111 int sns_strength; // Spatial Noise Shaping. 0=off, 100=maximum.
|
jpayne@69
|
112 int filter_strength; // range: [0 = off .. 100 = strongest]
|
jpayne@69
|
113 int filter_sharpness; // range: [0 = off .. 7 = least sharp]
|
jpayne@69
|
114 int filter_type; // filtering type: 0 = simple, 1 = strong (only used
|
jpayne@69
|
115 // if filter_strength > 0 or autofilter > 0)
|
jpayne@69
|
116 int autofilter; // Auto adjust filter's strength [0 = off, 1 = on]
|
jpayne@69
|
117 int alpha_compression; // Algorithm for encoding the alpha plane (0 = none,
|
jpayne@69
|
118 // 1 = compressed with WebP lossless). Default is 1.
|
jpayne@69
|
119 int alpha_filtering; // Predictive filtering method for alpha plane.
|
jpayne@69
|
120 // 0: none, 1: fast, 2: best. Default if 1.
|
jpayne@69
|
121 int alpha_quality; // Between 0 (smallest size) and 100 (lossless).
|
jpayne@69
|
122 // Default is 100.
|
jpayne@69
|
123 int pass; // number of entropy-analysis passes (in [1..10]).
|
jpayne@69
|
124
|
jpayne@69
|
125 int show_compressed; // if true, export the compressed picture back.
|
jpayne@69
|
126 // In-loop filtering is not applied.
|
jpayne@69
|
127 int preprocessing; // preprocessing filter:
|
jpayne@69
|
128 // 0=none, 1=segment-smooth, 2=pseudo-random dithering
|
jpayne@69
|
129 int partitions; // log2(number of token partitions) in [0..3]. Default
|
jpayne@69
|
130 // is set to 0 for easier progressive decoding.
|
jpayne@69
|
131 int partition_limit; // quality degradation allowed to fit the 512k limit
|
jpayne@69
|
132 // on prediction modes coding (0: no degradation,
|
jpayne@69
|
133 // 100: maximum possible degradation).
|
jpayne@69
|
134 int emulate_jpeg_size; // If true, compression parameters will be remapped
|
jpayne@69
|
135 // to better match the expected output size from
|
jpayne@69
|
136 // JPEG compression. Generally, the output size will
|
jpayne@69
|
137 // be similar but the degradation will be lower.
|
jpayne@69
|
138 int thread_level; // If non-zero, try and use multi-threaded encoding.
|
jpayne@69
|
139 int low_memory; // If set, reduce memory usage (but increase CPU use).
|
jpayne@69
|
140
|
jpayne@69
|
141 int near_lossless; // Near lossless encoding [0 = max loss .. 100 = off
|
jpayne@69
|
142 // (default)].
|
jpayne@69
|
143 int exact; // if non-zero, preserve the exact RGB values under
|
jpayne@69
|
144 // transparent area. Otherwise, discard this invisible
|
jpayne@69
|
145 // RGB information for better compression. The default
|
jpayne@69
|
146 // value is 0.
|
jpayne@69
|
147
|
jpayne@69
|
148 int use_delta_palette; // reserved
|
jpayne@69
|
149 int use_sharp_yuv; // if needed, use sharp (and slow) RGB->YUV conversion
|
jpayne@69
|
150
|
jpayne@69
|
151 int qmin; // minimum permissible quality factor
|
jpayne@69
|
152 int qmax; // maximum permissible quality factor
|
jpayne@69
|
153 };
|
jpayne@69
|
154
|
jpayne@69
|
155 // Enumerate some predefined settings for WebPConfig, depending on the type
|
jpayne@69
|
156 // of source picture. These presets are used when calling WebPConfigPreset().
|
jpayne@69
|
157 typedef enum WebPPreset {
|
jpayne@69
|
158 WEBP_PRESET_DEFAULT = 0, // default preset.
|
jpayne@69
|
159 WEBP_PRESET_PICTURE, // digital picture, like portrait, inner shot
|
jpayne@69
|
160 WEBP_PRESET_PHOTO, // outdoor photograph, with natural lighting
|
jpayne@69
|
161 WEBP_PRESET_DRAWING, // hand or line drawing, with high-contrast details
|
jpayne@69
|
162 WEBP_PRESET_ICON, // small-sized colorful images
|
jpayne@69
|
163 WEBP_PRESET_TEXT // text-like
|
jpayne@69
|
164 } WebPPreset;
|
jpayne@69
|
165
|
jpayne@69
|
166 // Internal, version-checked, entry point
|
jpayne@69
|
167 WEBP_NODISCARD WEBP_EXTERN int WebPConfigInitInternal(WebPConfig*, WebPPreset,
|
jpayne@69
|
168 float, int);
|
jpayne@69
|
169
|
jpayne@69
|
170 // Should always be called, to initialize a fresh WebPConfig structure before
|
jpayne@69
|
171 // modification. Returns false in case of version mismatch. WebPConfigInit()
|
jpayne@69
|
172 // must have succeeded before using the 'config' object.
|
jpayne@69
|
173 // Note that the default values are lossless=0 and quality=75.
|
jpayne@69
|
174 WEBP_NODISCARD static WEBP_INLINE int WebPConfigInit(WebPConfig* config) {
|
jpayne@69
|
175 return WebPConfigInitInternal(config, WEBP_PRESET_DEFAULT, 75.f,
|
jpayne@69
|
176 WEBP_ENCODER_ABI_VERSION);
|
jpayne@69
|
177 }
|
jpayne@69
|
178
|
jpayne@69
|
179 // This function will initialize the configuration according to a predefined
|
jpayne@69
|
180 // set of parameters (referred to by 'preset') and a given quality factor.
|
jpayne@69
|
181 // This function can be called as a replacement to WebPConfigInit(). Will
|
jpayne@69
|
182 // return false in case of error.
|
jpayne@69
|
183 WEBP_NODISCARD static WEBP_INLINE int WebPConfigPreset(WebPConfig* config,
|
jpayne@69
|
184 WebPPreset preset,
|
jpayne@69
|
185 float quality) {
|
jpayne@69
|
186 return WebPConfigInitInternal(config, preset, quality,
|
jpayne@69
|
187 WEBP_ENCODER_ABI_VERSION);
|
jpayne@69
|
188 }
|
jpayne@69
|
189
|
jpayne@69
|
190 // Activate the lossless compression mode with the desired efficiency level
|
jpayne@69
|
191 // between 0 (fastest, lowest compression) and 9 (slower, best compression).
|
jpayne@69
|
192 // A good default level is '6', providing a fair tradeoff between compression
|
jpayne@69
|
193 // speed and final compressed size.
|
jpayne@69
|
194 // This function will overwrite several fields from config: 'method', 'quality'
|
jpayne@69
|
195 // and 'lossless'. Returns false in case of parameter error.
|
jpayne@69
|
196 WEBP_NODISCARD WEBP_EXTERN int WebPConfigLosslessPreset(WebPConfig* config,
|
jpayne@69
|
197 int level);
|
jpayne@69
|
198
|
jpayne@69
|
199 // Returns true if 'config' is non-NULL and all configuration parameters are
|
jpayne@69
|
200 // within their valid ranges.
|
jpayne@69
|
201 WEBP_NODISCARD WEBP_EXTERN int WebPValidateConfig(const WebPConfig* config);
|
jpayne@69
|
202
|
jpayne@69
|
203 //------------------------------------------------------------------------------
|
jpayne@69
|
204 // Input / Output
|
jpayne@69
|
205 // Structure for storing auxiliary statistics.
|
jpayne@69
|
206
|
jpayne@69
|
207 struct WebPAuxStats {
|
jpayne@69
|
208 int coded_size; // final size
|
jpayne@69
|
209
|
jpayne@69
|
210 float PSNR[5]; // peak-signal-to-noise ratio for Y/U/V/All/Alpha
|
jpayne@69
|
211 int block_count[3]; // number of intra4/intra16/skipped macroblocks
|
jpayne@69
|
212 int header_bytes[2]; // approximate number of bytes spent for header
|
jpayne@69
|
213 // and mode-partition #0
|
jpayne@69
|
214 int residual_bytes[3][4]; // approximate number of bytes spent for
|
jpayne@69
|
215 // DC/AC/uv coefficients for each (0..3) segments.
|
jpayne@69
|
216 int segment_size[4]; // number of macroblocks in each segments
|
jpayne@69
|
217 int segment_quant[4]; // quantizer values for each segments
|
jpayne@69
|
218 int segment_level[4]; // filtering strength for each segments [0..63]
|
jpayne@69
|
219
|
jpayne@69
|
220 int alpha_data_size; // size of the transparency data
|
jpayne@69
|
221 int layer_data_size; // size of the enhancement layer data
|
jpayne@69
|
222
|
jpayne@69
|
223 // lossless encoder statistics
|
jpayne@69
|
224 uint32_t lossless_features; // bit0:predictor bit1:cross-color transform
|
jpayne@69
|
225 // bit2:subtract-green bit3:color indexing
|
jpayne@69
|
226 int histogram_bits; // number of precision bits of histogram
|
jpayne@69
|
227 int transform_bits; // precision bits for predictor transform
|
jpayne@69
|
228 int cache_bits; // number of bits for color cache lookup
|
jpayne@69
|
229 int palette_size; // number of color in palette, if used
|
jpayne@69
|
230 int lossless_size; // final lossless size
|
jpayne@69
|
231 int lossless_hdr_size; // lossless header (transform, huffman etc) size
|
jpayne@69
|
232 int lossless_data_size; // lossless image data size
|
jpayne@69
|
233 int cross_color_transform_bits; // precision bits for cross-color transform
|
jpayne@69
|
234
|
jpayne@69
|
235 uint32_t pad[1]; // padding for later use
|
jpayne@69
|
236 };
|
jpayne@69
|
237
|
jpayne@69
|
238 // Signature for output function. Should return true if writing was successful.
|
jpayne@69
|
239 // data/data_size is the segment of data to write, and 'picture' is for
|
jpayne@69
|
240 // reference (and so one can make use of picture->custom_ptr).
|
jpayne@69
|
241 typedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size,
|
jpayne@69
|
242 const WebPPicture* picture);
|
jpayne@69
|
243
|
jpayne@69
|
244 // WebPMemoryWrite: a special WebPWriterFunction that writes to memory using
|
jpayne@69
|
245 // the following WebPMemoryWriter object (to be set as a custom_ptr).
|
jpayne@69
|
246 struct WebPMemoryWriter {
|
jpayne@69
|
247 uint8_t* mem; // final buffer (of size 'max_size', larger than 'size').
|
jpayne@69
|
248 size_t size; // final size
|
jpayne@69
|
249 size_t max_size; // total capacity
|
jpayne@69
|
250 uint32_t pad[1]; // padding for later use
|
jpayne@69
|
251 };
|
jpayne@69
|
252
|
jpayne@69
|
253 // The following must be called first before any use.
|
jpayne@69
|
254 WEBP_EXTERN void WebPMemoryWriterInit(WebPMemoryWriter* writer);
|
jpayne@69
|
255
|
jpayne@69
|
256 // The following must be called to deallocate writer->mem memory. The 'writer'
|
jpayne@69
|
257 // object itself is not deallocated.
|
jpayne@69
|
258 WEBP_EXTERN void WebPMemoryWriterClear(WebPMemoryWriter* writer);
|
jpayne@69
|
259 // The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon
|
jpayne@69
|
260 // completion, writer.mem and writer.size will hold the coded data.
|
jpayne@69
|
261 // writer.mem must be freed by calling WebPMemoryWriterClear.
|
jpayne@69
|
262 WEBP_NODISCARD WEBP_EXTERN int WebPMemoryWrite(
|
jpayne@69
|
263 const uint8_t* data, size_t data_size, const WebPPicture* picture);
|
jpayne@69
|
264
|
jpayne@69
|
265 // Progress hook, called from time to time to report progress. It can return
|
jpayne@69
|
266 // false to request an abort of the encoding process, or true otherwise if
|
jpayne@69
|
267 // everything is OK.
|
jpayne@69
|
268 typedef int (*WebPProgressHook)(int percent, const WebPPicture* picture);
|
jpayne@69
|
269
|
jpayne@69
|
270 // Color spaces.
|
jpayne@69
|
271 typedef enum WebPEncCSP {
|
jpayne@69
|
272 // chroma sampling
|
jpayne@69
|
273 WEBP_YUV420 = 0, // 4:2:0
|
jpayne@69
|
274 WEBP_YUV420A = 4, // alpha channel variant
|
jpayne@69
|
275 WEBP_CSP_UV_MASK = 3, // bit-mask to get the UV sampling factors
|
jpayne@69
|
276 WEBP_CSP_ALPHA_BIT = 4 // bit that is set if alpha is present
|
jpayne@69
|
277 } WebPEncCSP;
|
jpayne@69
|
278
|
jpayne@69
|
279 // Encoding error conditions.
|
jpayne@69
|
280 typedef enum WebPEncodingError {
|
jpayne@69
|
281 VP8_ENC_OK = 0,
|
jpayne@69
|
282 VP8_ENC_ERROR_OUT_OF_MEMORY, // memory error allocating objects
|
jpayne@69
|
283 VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY, // memory error while flushing bits
|
jpayne@69
|
284 VP8_ENC_ERROR_NULL_PARAMETER, // a pointer parameter is NULL
|
jpayne@69
|
285 VP8_ENC_ERROR_INVALID_CONFIGURATION, // configuration is invalid
|
jpayne@69
|
286 VP8_ENC_ERROR_BAD_DIMENSION, // picture has invalid width/height
|
jpayne@69
|
287 VP8_ENC_ERROR_PARTITION0_OVERFLOW, // partition is bigger than 512k
|
jpayne@69
|
288 VP8_ENC_ERROR_PARTITION_OVERFLOW, // partition is bigger than 16M
|
jpayne@69
|
289 VP8_ENC_ERROR_BAD_WRITE, // error while flushing bytes
|
jpayne@69
|
290 VP8_ENC_ERROR_FILE_TOO_BIG, // file is bigger than 4G
|
jpayne@69
|
291 VP8_ENC_ERROR_USER_ABORT, // abort request by user
|
jpayne@69
|
292 VP8_ENC_ERROR_LAST // list terminator. always last.
|
jpayne@69
|
293 } WebPEncodingError;
|
jpayne@69
|
294
|
jpayne@69
|
295 // maximum width/height allowed (inclusive), in pixels
|
jpayne@69
|
296 #define WEBP_MAX_DIMENSION 16383
|
jpayne@69
|
297
|
jpayne@69
|
298 // Main exchange structure (input samples, output bytes, statistics)
|
jpayne@69
|
299 //
|
jpayne@69
|
300 // Once WebPPictureInit() has been called, it's ok to make all the INPUT fields
|
jpayne@69
|
301 // (use_argb, y/u/v, argb, ...) point to user-owned data, even if
|
jpayne@69
|
302 // WebPPictureAlloc() has been called. Depending on the value use_argb,
|
jpayne@69
|
303 // it's guaranteed that either *argb or *y/*u/*v content will be kept untouched.
|
jpayne@69
|
304 struct WebPPicture {
|
jpayne@69
|
305 // INPUT
|
jpayne@69
|
306 //////////////
|
jpayne@69
|
307 // Main flag for encoder selecting between ARGB or YUV input.
|
jpayne@69
|
308 // It is recommended to use ARGB input (*argb, argb_stride) for lossless
|
jpayne@69
|
309 // compression, and YUV input (*y, *u, *v, etc.) for lossy compression
|
jpayne@69
|
310 // since these are the respective native colorspace for these formats.
|
jpayne@69
|
311 int use_argb;
|
jpayne@69
|
312
|
jpayne@69
|
313 // YUV input (mostly used for input to lossy compression)
|
jpayne@69
|
314 WebPEncCSP colorspace; // colorspace: should be YUV420 for now (=Y'CbCr).
|
jpayne@69
|
315 int width, height; // dimensions (less or equal to WEBP_MAX_DIMENSION)
|
jpayne@69
|
316 uint8_t* y, *u, *v; // pointers to luma/chroma planes.
|
jpayne@69
|
317 int y_stride, uv_stride; // luma/chroma strides.
|
jpayne@69
|
318 uint8_t* a; // pointer to the alpha plane
|
jpayne@69
|
319 int a_stride; // stride of the alpha plane
|
jpayne@69
|
320 uint32_t pad1[2]; // padding for later use
|
jpayne@69
|
321
|
jpayne@69
|
322 // ARGB input (mostly used for input to lossless compression)
|
jpayne@69
|
323 uint32_t* argb; // Pointer to argb (32 bit) plane.
|
jpayne@69
|
324 int argb_stride; // This is stride in pixels units, not bytes.
|
jpayne@69
|
325 uint32_t pad2[3]; // padding for later use
|
jpayne@69
|
326
|
jpayne@69
|
327 // OUTPUT
|
jpayne@69
|
328 ///////////////
|
jpayne@69
|
329 // Byte-emission hook, to store compressed bytes as they are ready.
|
jpayne@69
|
330 WebPWriterFunction writer; // can be NULL
|
jpayne@69
|
331 void* custom_ptr; // can be used by the writer.
|
jpayne@69
|
332
|
jpayne@69
|
333 // map for extra information (only for lossy compression mode)
|
jpayne@69
|
334 int extra_info_type; // 1: intra type, 2: segment, 3: quant
|
jpayne@69
|
335 // 4: intra-16 prediction mode,
|
jpayne@69
|
336 // 5: chroma prediction mode,
|
jpayne@69
|
337 // 6: bit cost, 7: distortion
|
jpayne@69
|
338 uint8_t* extra_info; // if not NULL, points to an array of size
|
jpayne@69
|
339 // ((width + 15) / 16) * ((height + 15) / 16) that
|
jpayne@69
|
340 // will be filled with a macroblock map, depending
|
jpayne@69
|
341 // on extra_info_type.
|
jpayne@69
|
342
|
jpayne@69
|
343 // STATS AND REPORTS
|
jpayne@69
|
344 ///////////////////////////
|
jpayne@69
|
345 // Pointer to side statistics (updated only if not NULL)
|
jpayne@69
|
346 WebPAuxStats* stats;
|
jpayne@69
|
347
|
jpayne@69
|
348 // Error code for the latest error encountered during encoding
|
jpayne@69
|
349 WebPEncodingError error_code;
|
jpayne@69
|
350
|
jpayne@69
|
351 // If not NULL, report progress during encoding.
|
jpayne@69
|
352 WebPProgressHook progress_hook;
|
jpayne@69
|
353
|
jpayne@69
|
354 void* user_data; // this field is free to be set to any value and
|
jpayne@69
|
355 // used during callbacks (like progress-report e.g.).
|
jpayne@69
|
356
|
jpayne@69
|
357 uint32_t pad3[3]; // padding for later use
|
jpayne@69
|
358
|
jpayne@69
|
359 // Unused for now
|
jpayne@69
|
360 uint8_t* pad4, *pad5;
|
jpayne@69
|
361 uint32_t pad6[8]; // padding for later use
|
jpayne@69
|
362
|
jpayne@69
|
363 // PRIVATE FIELDS
|
jpayne@69
|
364 ////////////////////
|
jpayne@69
|
365 void* memory_; // row chunk of memory for yuva planes
|
jpayne@69
|
366 void* memory_argb_; // and for argb too.
|
jpayne@69
|
367 void* pad7[2]; // padding for later use
|
jpayne@69
|
368 };
|
jpayne@69
|
369
|
jpayne@69
|
370 // Internal, version-checked, entry point
|
jpayne@69
|
371 WEBP_NODISCARD WEBP_EXTERN int WebPPictureInitInternal(WebPPicture*, int);
|
jpayne@69
|
372
|
jpayne@69
|
373 // Should always be called, to initialize the structure. Returns false in case
|
jpayne@69
|
374 // of version mismatch. WebPPictureInit() must have succeeded before using the
|
jpayne@69
|
375 // 'picture' object.
|
jpayne@69
|
376 // Note that, by default, use_argb is false and colorspace is WEBP_YUV420.
|
jpayne@69
|
377 WEBP_NODISCARD static WEBP_INLINE int WebPPictureInit(WebPPicture* picture) {
|
jpayne@69
|
378 return WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION);
|
jpayne@69
|
379 }
|
jpayne@69
|
380
|
jpayne@69
|
381 //------------------------------------------------------------------------------
|
jpayne@69
|
382 // WebPPicture utils
|
jpayne@69
|
383
|
jpayne@69
|
384 // Convenience allocation / deallocation based on picture->width/height:
|
jpayne@69
|
385 // Allocate y/u/v buffers as per colorspace/width/height specification.
|
jpayne@69
|
386 // Note! This function will free the previous buffer if needed.
|
jpayne@69
|
387 // Returns false in case of memory error.
|
jpayne@69
|
388 WEBP_NODISCARD WEBP_EXTERN int WebPPictureAlloc(WebPPicture* picture);
|
jpayne@69
|
389
|
jpayne@69
|
390 // Release the memory allocated by WebPPictureAlloc() or WebPPictureImport*().
|
jpayne@69
|
391 // Note that this function does _not_ free the memory used by the 'picture'
|
jpayne@69
|
392 // object itself.
|
jpayne@69
|
393 // Besides memory (which is reclaimed) all other fields of 'picture' are
|
jpayne@69
|
394 // preserved.
|
jpayne@69
|
395 WEBP_EXTERN void WebPPictureFree(WebPPicture* picture);
|
jpayne@69
|
396
|
jpayne@69
|
397 // Copy the pixels of *src into *dst, using WebPPictureAlloc. Upon return, *dst
|
jpayne@69
|
398 // will fully own the copied pixels (this is not a view). The 'dst' picture need
|
jpayne@69
|
399 // not be initialized as its content is overwritten.
|
jpayne@69
|
400 // Returns false in case of memory allocation error.
|
jpayne@69
|
401 WEBP_NODISCARD WEBP_EXTERN int WebPPictureCopy(const WebPPicture* src,
|
jpayne@69
|
402 WebPPicture* dst);
|
jpayne@69
|
403
|
jpayne@69
|
404 // Compute the single distortion for packed planes of samples.
|
jpayne@69
|
405 // 'src' will be compared to 'ref', and the raw distortion stored into
|
jpayne@69
|
406 // '*distortion'. The refined metric (log(MSE), log(1 - ssim),...' will be
|
jpayne@69
|
407 // stored in '*result'.
|
jpayne@69
|
408 // 'x_step' is the horizontal stride (in bytes) between samples.
|
jpayne@69
|
409 // 'src/ref_stride' is the byte distance between rows.
|
jpayne@69
|
410 // Returns false in case of error (bad parameter, memory allocation error, ...).
|
jpayne@69
|
411 WEBP_NODISCARD WEBP_EXTERN int WebPPlaneDistortion(
|
jpayne@69
|
412 const uint8_t* src, size_t src_stride,
|
jpayne@69
|
413 const uint8_t* ref, size_t ref_stride, int width, int height, size_t x_step,
|
jpayne@69
|
414 int type, // 0 = PSNR, 1 = SSIM, 2 = LSIM
|
jpayne@69
|
415 float* distortion, float* result);
|
jpayne@69
|
416
|
jpayne@69
|
417 // Compute PSNR, SSIM or LSIM distortion metric between two pictures. Results
|
jpayne@69
|
418 // are in dB, stored in result[] in the B/G/R/A/All order. The distortion is
|
jpayne@69
|
419 // always performed using ARGB samples. Hence if the input is YUV(A), the
|
jpayne@69
|
420 // picture will be internally converted to ARGB (just for the measurement).
|
jpayne@69
|
421 // Warning: this function is rather CPU-intensive.
|
jpayne@69
|
422 WEBP_NODISCARD WEBP_EXTERN int WebPPictureDistortion(
|
jpayne@69
|
423 const WebPPicture* src, const WebPPicture* ref,
|
jpayne@69
|
424 int metric_type, // 0 = PSNR, 1 = SSIM, 2 = LSIM
|
jpayne@69
|
425 float result[5]);
|
jpayne@69
|
426
|
jpayne@69
|
427 // self-crops a picture to the rectangle defined by top/left/width/height.
|
jpayne@69
|
428 // Returns false in case of memory allocation error, or if the rectangle is
|
jpayne@69
|
429 // outside of the source picture.
|
jpayne@69
|
430 // The rectangle for the view is defined by the top-left corner pixel
|
jpayne@69
|
431 // coordinates (left, top) as well as its width and height. This rectangle
|
jpayne@69
|
432 // must be fully be comprised inside the 'src' source picture. If the source
|
jpayne@69
|
433 // picture uses the YUV420 colorspace, the top and left coordinates will be
|
jpayne@69
|
434 // snapped to even values.
|
jpayne@69
|
435 WEBP_NODISCARD WEBP_EXTERN int WebPPictureCrop(
|
jpayne@69
|
436 WebPPicture* picture, int left, int top, int width, int height);
|
jpayne@69
|
437
|
jpayne@69
|
438 // Extracts a view from 'src' picture into 'dst'. The rectangle for the view
|
jpayne@69
|
439 // is defined by the top-left corner pixel coordinates (left, top) as well
|
jpayne@69
|
440 // as its width and height. This rectangle must be fully be comprised inside
|
jpayne@69
|
441 // the 'src' source picture. If the source picture uses the YUV420 colorspace,
|
jpayne@69
|
442 // the top and left coordinates will be snapped to even values.
|
jpayne@69
|
443 // Picture 'src' must out-live 'dst' picture. Self-extraction of view is allowed
|
jpayne@69
|
444 // ('src' equal to 'dst') as a mean of fast-cropping (but note that doing so,
|
jpayne@69
|
445 // the original dimension will be lost). Picture 'dst' need not be initialized
|
jpayne@69
|
446 // with WebPPictureInit() if it is different from 'src', since its content will
|
jpayne@69
|
447 // be overwritten.
|
jpayne@69
|
448 // Returns false in case of invalid parameters.
|
jpayne@69
|
449 WEBP_NODISCARD WEBP_EXTERN int WebPPictureView(
|
jpayne@69
|
450 const WebPPicture* src, int left, int top, int width, int height,
|
jpayne@69
|
451 WebPPicture* dst);
|
jpayne@69
|
452
|
jpayne@69
|
453 // Returns true if the 'picture' is actually a view and therefore does
|
jpayne@69
|
454 // not own the memory for pixels.
|
jpayne@69
|
455 WEBP_EXTERN int WebPPictureIsView(const WebPPicture* picture);
|
jpayne@69
|
456
|
jpayne@69
|
457 // Rescale a picture to new dimension width x height.
|
jpayne@69
|
458 // If either 'width' or 'height' (but not both) is 0 the corresponding
|
jpayne@69
|
459 // dimension will be calculated preserving the aspect ratio.
|
jpayne@69
|
460 // No gamma correction is applied.
|
jpayne@69
|
461 // Returns false in case of error (invalid parameter or insufficient memory).
|
jpayne@69
|
462 WEBP_NODISCARD WEBP_EXTERN int WebPPictureRescale(WebPPicture* picture,
|
jpayne@69
|
463 int width, int height);
|
jpayne@69
|
464
|
jpayne@69
|
465 // Colorspace conversion function to import RGB samples.
|
jpayne@69
|
466 // Previous buffer will be free'd, if any.
|
jpayne@69
|
467 // *rgb buffer should have a size of at least height * rgb_stride.
|
jpayne@69
|
468 // Returns false in case of memory error.
|
jpayne@69
|
469 WEBP_NODISCARD WEBP_EXTERN int WebPPictureImportRGB(
|
jpayne@69
|
470 WebPPicture* picture, const uint8_t* rgb, int rgb_stride);
|
jpayne@69
|
471 // Same, but for RGBA buffer.
|
jpayne@69
|
472 WEBP_NODISCARD WEBP_EXTERN int WebPPictureImportRGBA(
|
jpayne@69
|
473 WebPPicture* picture, const uint8_t* rgba, int rgba_stride);
|
jpayne@69
|
474 // Same, but for RGBA buffer. Imports the RGB direct from the 32-bit format
|
jpayne@69
|
475 // input buffer ignoring the alpha channel. Avoids needing to copy the data
|
jpayne@69
|
476 // to a temporary 24-bit RGB buffer to import the RGB only.
|
jpayne@69
|
477 WEBP_NODISCARD WEBP_EXTERN int WebPPictureImportRGBX(
|
jpayne@69
|
478 WebPPicture* picture, const uint8_t* rgbx, int rgbx_stride);
|
jpayne@69
|
479
|
jpayne@69
|
480 // Variants of the above, but taking BGR(A|X) input.
|
jpayne@69
|
481 WEBP_NODISCARD WEBP_EXTERN int WebPPictureImportBGR(
|
jpayne@69
|
482 WebPPicture* picture, const uint8_t* bgr, int bgr_stride);
|
jpayne@69
|
483 WEBP_NODISCARD WEBP_EXTERN int WebPPictureImportBGRA(
|
jpayne@69
|
484 WebPPicture* picture, const uint8_t* bgra, int bgra_stride);
|
jpayne@69
|
485 WEBP_NODISCARD WEBP_EXTERN int WebPPictureImportBGRX(
|
jpayne@69
|
486 WebPPicture* picture, const uint8_t* bgrx, int bgrx_stride);
|
jpayne@69
|
487
|
jpayne@69
|
488 // Converts picture->argb data to the YUV420A format. The 'colorspace'
|
jpayne@69
|
489 // parameter is deprecated and should be equal to WEBP_YUV420.
|
jpayne@69
|
490 // Upon return, picture->use_argb is set to false. The presence of real
|
jpayne@69
|
491 // non-opaque transparent values is detected, and 'colorspace' will be
|
jpayne@69
|
492 // adjusted accordingly. Note that this method is lossy.
|
jpayne@69
|
493 // Returns false in case of error.
|
jpayne@69
|
494 WEBP_NODISCARD WEBP_EXTERN int WebPPictureARGBToYUVA(
|
jpayne@69
|
495 WebPPicture* picture, WebPEncCSP /*colorspace = WEBP_YUV420*/);
|
jpayne@69
|
496
|
jpayne@69
|
497 // Same as WebPPictureARGBToYUVA(), but the conversion is done using
|
jpayne@69
|
498 // pseudo-random dithering with a strength 'dithering' between
|
jpayne@69
|
499 // 0.0 (no dithering) and 1.0 (maximum dithering). This is useful
|
jpayne@69
|
500 // for photographic picture.
|
jpayne@69
|
501 WEBP_NODISCARD WEBP_EXTERN int WebPPictureARGBToYUVADithered(
|
jpayne@69
|
502 WebPPicture* picture, WebPEncCSP colorspace, float dithering);
|
jpayne@69
|
503
|
jpayne@69
|
504 // Performs 'sharp' RGBA->YUVA420 downsampling and colorspace conversion
|
jpayne@69
|
505 // Downsampling is handled with extra care in case of color clipping. This
|
jpayne@69
|
506 // method is roughly 2x slower than WebPPictureARGBToYUVA() but produces better
|
jpayne@69
|
507 // and sharper YUV representation.
|
jpayne@69
|
508 // Returns false in case of error.
|
jpayne@69
|
509 WEBP_NODISCARD WEBP_EXTERN int WebPPictureSharpARGBToYUVA(WebPPicture* picture);
|
jpayne@69
|
510 // kept for backward compatibility:
|
jpayne@69
|
511 WEBP_NODISCARD WEBP_EXTERN int WebPPictureSmartARGBToYUVA(WebPPicture* picture);
|
jpayne@69
|
512
|
jpayne@69
|
513 // Converts picture->yuv to picture->argb and sets picture->use_argb to true.
|
jpayne@69
|
514 // The input format must be YUV_420 or YUV_420A. The conversion from YUV420 to
|
jpayne@69
|
515 // ARGB incurs a small loss too.
|
jpayne@69
|
516 // Note that the use of this colorspace is discouraged if one has access to the
|
jpayne@69
|
517 // raw ARGB samples, since using YUV420 is comparatively lossy.
|
jpayne@69
|
518 // Returns false in case of error.
|
jpayne@69
|
519 WEBP_NODISCARD WEBP_EXTERN int WebPPictureYUVAToARGB(WebPPicture* picture);
|
jpayne@69
|
520
|
jpayne@69
|
521 // Helper function: given a width x height plane of RGBA or YUV(A) samples
|
jpayne@69
|
522 // clean-up or smoothen the YUV or RGB samples under fully transparent area,
|
jpayne@69
|
523 // to help compressibility (no guarantee, though).
|
jpayne@69
|
524 WEBP_EXTERN void WebPCleanupTransparentArea(WebPPicture* picture);
|
jpayne@69
|
525
|
jpayne@69
|
526 // Scan the picture 'picture' for the presence of non fully opaque alpha values.
|
jpayne@69
|
527 // Returns true in such case. Otherwise returns false (indicating that the
|
jpayne@69
|
528 // alpha plane can be ignored altogether e.g.).
|
jpayne@69
|
529 WEBP_EXTERN int WebPPictureHasTransparency(const WebPPicture* picture);
|
jpayne@69
|
530
|
jpayne@69
|
531 // Remove the transparency information (if present) by blending the color with
|
jpayne@69
|
532 // the background color 'background_rgb' (specified as 24bit RGB triplet).
|
jpayne@69
|
533 // After this call, all alpha values are reset to 0xff.
|
jpayne@69
|
534 WEBP_EXTERN void WebPBlendAlpha(WebPPicture* picture, uint32_t background_rgb);
|
jpayne@69
|
535
|
jpayne@69
|
536 //------------------------------------------------------------------------------
|
jpayne@69
|
537 // Main call
|
jpayne@69
|
538
|
jpayne@69
|
539 // Main encoding call, after config and picture have been initialized.
|
jpayne@69
|
540 // 'picture' must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION),
|
jpayne@69
|
541 // and the 'config' object must be a valid one.
|
jpayne@69
|
542 // Returns false in case of error, true otherwise.
|
jpayne@69
|
543 // In case of error, picture->error_code is updated accordingly.
|
jpayne@69
|
544 // 'picture' can hold the source samples in both YUV(A) or ARGB input, depending
|
jpayne@69
|
545 // on the value of 'picture->use_argb'. It is highly recommended to use
|
jpayne@69
|
546 // the former for lossy encoding, and the latter for lossless encoding
|
jpayne@69
|
547 // (when config.lossless is true). Automatic conversion from one format to
|
jpayne@69
|
548 // another is provided but they both incur some loss.
|
jpayne@69
|
549 WEBP_NODISCARD WEBP_EXTERN int WebPEncode(const WebPConfig* config,
|
jpayne@69
|
550 WebPPicture* picture);
|
jpayne@69
|
551
|
jpayne@69
|
552 //------------------------------------------------------------------------------
|
jpayne@69
|
553
|
jpayne@69
|
554 #ifdef __cplusplus
|
jpayne@69
|
555 } // extern "C"
|
jpayne@69
|
556 #endif
|
jpayne@69
|
557
|
jpayne@69
|
558 #endif // WEBP_WEBP_ENCODE_H_
|