Coverage Report

Created: 2025-11-16 07:22

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