Coverage Report

Created: 2025-08-12 07:37

/work/include/webp/decode.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2010 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
//  Main decoding functions for WebP images.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#ifndef WEBP_WEBP_DECODE_H_
15
#define WEBP_WEBP_DECODE_H_
16
17
#include <stddef.h>
18
19
#include "./types.h"
20
21
#ifdef __cplusplus
22
extern "C" {
23
#endif
24
25
2.82k
#define WEBP_DECODER_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 VP8StatusCode VP8StatusCode;
30
// typedef enum WEBP_CSP_MODE WEBP_CSP_MODE;
31
typedef struct WebPRGBABuffer WebPRGBABuffer;
32
typedef struct WebPYUVABuffer WebPYUVABuffer;
33
typedef struct WebPDecBuffer WebPDecBuffer;
34
typedef struct WebPIDecoder WebPIDecoder;
35
typedef struct WebPBitstreamFeatures WebPBitstreamFeatures;
36
typedef struct WebPDecoderOptions WebPDecoderOptions;
37
typedef struct WebPDecoderConfig WebPDecoderConfig;
38
39
// Return the decoder's version number, packed in hexadecimal using 8bits for
40
// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
41
WEBP_EXTERN int WebPGetDecoderVersion(void);
42
43
// Retrieve basic header information: width, height.
44
// This function will also validate the header, returning true on success,
45
// false otherwise. '*width' and '*height' are only valid on successful return.
46
// Pointers 'width' and 'height' can be passed NULL if deemed irrelevant.
47
// Note: The following chunk sequences (before the raw VP8/VP8L data) are
48
// considered valid by this function:
49
// RIFF + VP8(L)
50
// RIFF + VP8X + (optional chunks) + VP8(L)
51
// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
52
// VP8(L)     <-- Not a valid WebP format: only allowed for internal purpose.
53
WEBP_NODISCARD WEBP_EXTERN int WebPGetInfo(const uint8_t* data,
54
                                           size_t data_size, int* width,
55
                                           int* height);
56
57
// Decodes WebP images pointed to by 'data' and returns RGBA samples, along
58
// with the dimensions in *width and *height. The ordering of samples in
59
// memory is R, G, B, A, R, G, B, A... in scan order (endian-independent).
60
// The returned pointer should be deleted calling WebPFree().
61
// Returns NULL in case of error.
62
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeRGBA(const uint8_t* data,
63
                                                   size_t data_size, int* width,
64
                                                   int* height);
65
66
// Same as WebPDecodeRGBA, but returning A, R, G, B, A, R, G, B... ordered data.
67
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeARGB(const uint8_t* data,
68
                                                   size_t data_size, int* width,
69
                                                   int* height);
70
71
// Same as WebPDecodeRGBA, but returning B, G, R, A, B, G, R, A... ordered data.
72
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeBGRA(const uint8_t* data,
73
                                                   size_t data_size, int* width,
74
                                                   int* height);
75
76
// Same as WebPDecodeRGBA, but returning R, G, B, R, G, B... ordered data.
77
// If the bitstream contains transparency, it is ignored.
78
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeRGB(const uint8_t* data,
79
                                                  size_t data_size, int* width,
80
                                                  int* height);
81
82
// Same as WebPDecodeRGB, but returning B, G, R, B, G, R... ordered data.
83
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeBGR(const uint8_t* data,
84
                                                  size_t data_size, int* width,
85
                                                  int* height);
86
87
// Decode WebP images pointed to by 'data' to Y'UV format(*). The pointer
88
// returned is the Y samples buffer. Upon return, *u and *v will point to
89
// the U and V chroma data. These U and V buffers need NOT be passed to
90
// WebPFree(), unlike the returned Y luma one. The dimension of the U and V
91
// planes are both (*width + 1) / 2 and (*height + 1) / 2.
92
// Upon return, the Y buffer has a stride returned as '*stride', while U and V
93
// have a common stride returned as '*uv_stride'.
94
// 'width' and 'height' may be NULL, the other pointers must not be.
95
// Returns NULL in case of error.
96
// (*) Also named Y'CbCr. See: https://en.wikipedia.org/wiki/YCbCr
97
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeYUV(const uint8_t* data,
98
                                                  size_t data_size, int* width,
99
                                                  int* height, uint8_t** u,
100
                                                  uint8_t** v, int* stride,
101
                                                  int* uv_stride);
102
103
// These five functions are variants of the above ones, that decode the image
104
// directly into a pre-allocated buffer 'output_buffer'. The maximum storage
105
// available in this buffer is indicated by 'output_buffer_size'. If this
106
// storage is not sufficient (or an error occurred), NULL is returned.
107
// Otherwise, output_buffer is returned, for convenience.
108
// The parameter 'output_stride' specifies the distance (in bytes)
109
// between scanlines. Hence, output_buffer_size is expected to be at least
110
// output_stride x picture-height.
111
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeRGBAInto(
112
    const uint8_t* data, size_t data_size, uint8_t* output_buffer,
113
    size_t output_buffer_size, int output_stride);
114
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeARGBInto(
115
    const uint8_t* data, size_t data_size, uint8_t* output_buffer,
116
    size_t output_buffer_size, int output_stride);
117
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeBGRAInto(
118
    const uint8_t* data, size_t data_size, uint8_t* output_buffer,
119
    size_t output_buffer_size, int output_stride);
120
121
// RGB and BGR variants. Here too the transparency information, if present,
122
// will be dropped and ignored.
123
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeRGBInto(const uint8_t* data,
124
                                                      size_t data_size,
125
                                                      uint8_t* output_buffer,
126
                                                      size_t output_buffer_size,
127
                                                      int output_stride);
128
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeBGRInto(const uint8_t* data,
129
                                                      size_t data_size,
130
                                                      uint8_t* output_buffer,
131
                                                      size_t output_buffer_size,
132
                                                      int output_stride);
133
134
// WebPDecodeYUVInto() is a variant of WebPDecodeYUV() that operates directly
135
// into pre-allocated luma/chroma plane buffers. This function requires the
136
// strides to be passed: one for the luma plane and one for each of the
137
// chroma ones. The size of each plane buffer is passed as 'luma_size',
138
// 'u_size' and 'v_size' respectively.
139
// Pointer to the luma plane ('*luma') is returned or NULL if an error occurred
140
// during decoding (or because some buffers were found to be too small).
141
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeYUVInto(
142
    const uint8_t* data, size_t data_size, uint8_t* luma, size_t luma_size,
143
    int luma_stride, uint8_t* u, size_t u_size, int u_stride, uint8_t* v,
144
    size_t v_size, int v_stride);
145
146
//------------------------------------------------------------------------------
147
// Output colorspaces and buffer
148
149
// Colorspaces
150
// Note: the naming describes the byte-ordering of packed samples in memory.
151
// For instance, MODE_BGRA relates to samples ordered as B,G,R,A,B,G,R,A,...
152
// Non-capital names (e.g.:MODE_Argb) relates to pre-multiplied RGB channels.
153
// RGBA-4444 and RGB-565 colorspaces are represented by following byte-order:
154
// RGBA-4444: [r3 r2 r1 r0 g3 g2 g1 g0], [b3 b2 b1 b0 a3 a2 a1 a0], ...
155
// RGB-565: [r4 r3 r2 r1 r0 g5 g4 g3], [g2 g1 g0 b4 b3 b2 b1 b0], ...
156
// In the case WEBP_SWAP_16BITS_CSP is defined, the bytes are swapped for
157
// these two modes:
158
// RGBA-4444: [b3 b2 b1 b0 a3 a2 a1 a0], [r3 r2 r1 r0 g3 g2 g1 g0], ...
159
// RGB-565: [g2 g1 g0 b4 b3 b2 b1 b0], [r4 r3 r2 r1 r0 g5 g4 g3], ...
160
161
typedef enum WEBP_CSP_MODE {
162
  MODE_RGB = 0,
163
  MODE_RGBA = 1,
164
  MODE_BGR = 2,
165
  MODE_BGRA = 3,
166
  MODE_ARGB = 4,
167
  MODE_RGBA_4444 = 5,
168
  MODE_RGB_565 = 6,
169
  // RGB-premultiplied transparent modes (alpha value is preserved)
170
  MODE_rgbA = 7,
171
  MODE_bgrA = 8,
172
  MODE_Argb = 9,
173
  MODE_rgbA_4444 = 10,
174
  // YUV modes must come after RGB ones.
175
  MODE_YUV = 11,
176
  MODE_YUVA = 12,  // yuv 4:2:0
177
  MODE_LAST = 13
178
} WEBP_CSP_MODE;
179
180
// Some useful macros:
181
0
static WEBP_INLINE int WebPIsPremultipliedMode(WEBP_CSP_MODE mode) {
182
0
  return (mode == MODE_rgbA || mode == MODE_bgrA || mode == MODE_Argb ||
183
0
          mode == MODE_rgbA_4444);
184
0
}
185
186
0
static WEBP_INLINE int WebPIsAlphaMode(WEBP_CSP_MODE mode) {
187
0
  return (mode == MODE_RGBA || mode == MODE_BGRA || mode == MODE_ARGB ||
188
0
          mode == MODE_RGBA_4444 || mode == MODE_YUVA ||
189
0
          WebPIsPremultipliedMode(mode));
190
0
}
191
192
0
static WEBP_INLINE int WebPIsRGBMode(WEBP_CSP_MODE mode) {
193
0
  return (mode < MODE_YUV);
194
0
}
195
196
//------------------------------------------------------------------------------
197
// WebPDecBuffer: Generic structure for describing the output sample buffer.
198
199
struct WebPRGBABuffer {  // view as RGBA
200
  uint8_t* rgba;         // pointer to RGBA samples
201
  int stride;            // stride in bytes from one scanline to the next.
202
  size_t size;           // total size of the *rgba buffer.
203
};
204
205
struct WebPYUVABuffer {    // view as YUVA
206
  uint8_t *y, *u, *v, *a;  // pointer to luma, chroma U/V, alpha samples
207
  int y_stride;            // luma stride
208
  int u_stride, v_stride;  // chroma strides
209
  int a_stride;            // alpha stride
210
  size_t y_size;           // luma plane size
211
  size_t u_size, v_size;   // chroma planes size
212
  size_t a_size;           // alpha-plane size
213
};
214
215
// Output buffer
216
struct WebPDecBuffer {
217
  WEBP_CSP_MODE colorspace;  // Colorspace.
218
  int width, height;         // Dimensions.
219
  int is_external_memory;    // If non-zero, 'internal_memory' pointer is not
220
                             // used. If value is '2' or more, the external
221
                             // memory is considered 'slow' and multiple
222
                             // read/write will be avoided.
223
  union {
224
    WebPRGBABuffer RGBA;
225
    WebPYUVABuffer YUVA;
226
  } u;              // Nameless union of buffer parameters.
227
  uint32_t pad[4];  // padding for later use
228
229
  uint8_t* private_memory;  // Internally allocated memory (only when
230
                            // is_external_memory is 0). Should not be used
231
                            // externally, but accessed via the buffer union.
232
};
233
234
// Internal, version-checked, entry point
235
WEBP_NODISCARD WEBP_EXTERN int WebPInitDecBufferInternal(WebPDecBuffer*, int);
236
237
// Initialize the structure as empty. Must be called before any other use.
238
// Returns false in case of version mismatch
239
0
WEBP_NODISCARD static WEBP_INLINE int WebPInitDecBuffer(WebPDecBuffer* buffer) {
240
0
  return WebPInitDecBufferInternal(buffer, WEBP_DECODER_ABI_VERSION);
241
0
}
242
243
// Free any memory associated with the buffer. Must always be called last.
244
// Note: doesn't free the 'buffer' structure itself.
245
WEBP_EXTERN void WebPFreeDecBuffer(WebPDecBuffer* buffer);
246
247
//------------------------------------------------------------------------------
248
// Enumeration of the status codes
249
250
typedef enum WEBP_NODISCARD VP8StatusCode {
251
  VP8_STATUS_OK = 0,
252
  VP8_STATUS_OUT_OF_MEMORY,
253
  VP8_STATUS_INVALID_PARAM,
254
  VP8_STATUS_BITSTREAM_ERROR,
255
  VP8_STATUS_UNSUPPORTED_FEATURE,
256
  VP8_STATUS_SUSPENDED,
257
  VP8_STATUS_USER_ABORT,
258
  VP8_STATUS_NOT_ENOUGH_DATA
259
} VP8StatusCode;
260
261
//------------------------------------------------------------------------------
262
// Incremental decoding
263
//
264
// This API allows streamlined decoding of partial data.
265
// Picture can be incrementally decoded as data become available thanks to the
266
// WebPIDecoder object. This object can be left in a SUSPENDED state if the
267
// picture is only partially decoded, pending additional input.
268
// Code example:
269
/*
270
     WebPInitDecBuffer(&output_buffer);
271
     output_buffer.colorspace = mode;
272
     ...
273
     WebPIDecoder* idec = WebPINewDecoder(&output_buffer);
274
     while (additional_data_is_available) {
275
       // ... (get additional data in some new_data[] buffer)
276
       status = WebPIAppend(idec, new_data, new_data_size);
277
       if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {
278
         break;    // an error occurred.
279
       }
280
281
       // The above call decodes the current available buffer.
282
       // Part of the image can now be refreshed by calling
283
       // WebPIDecGetRGB()/WebPIDecGetYUVA() etc.
284
     }
285
     WebPIDelete(idec);
286
*/
287
288
// Creates a new incremental decoder with the supplied buffer parameter.
289
// This output_buffer can be passed NULL, in which case a default output buffer
290
// is used (with MODE_RGB). Otherwise, an internal reference to 'output_buffer'
291
// is kept, which means that the lifespan of 'output_buffer' must be larger than
292
// that of the returned WebPIDecoder object.
293
// The supplied 'output_buffer' content MUST NOT be changed between calls to
294
// WebPIAppend() or WebPIUpdate() unless 'output_buffer.is_external_memory' is
295
// not set to 0. In such a case, it is allowed to modify the pointers, size and
296
// stride of output_buffer.u.RGBA or output_buffer.u.YUVA, provided they remain
297
// within valid bounds.
298
// All other fields of WebPDecBuffer MUST remain constant between calls.
299
// Returns NULL if the allocation failed.
300
WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPINewDecoder(
301
    WebPDecBuffer* output_buffer);
302
303
// This function allocates and initializes an incremental-decoder object, which
304
// will output the RGB/A samples specified by 'csp' into a preallocated
305
// buffer 'output_buffer'. The size of this buffer is at least
306
// 'output_buffer_size' and the stride (distance in bytes between two scanlines)
307
// is specified by 'output_stride'.
308
// Additionally, output_buffer can be passed NULL in which case the output
309
// buffer will be allocated automatically when the decoding starts. The
310
// colorspace 'csp' is taken into account for allocating this buffer. All other
311
// parameters are ignored.
312
// Returns NULL if the allocation failed, or if some parameters are invalid.
313
WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE csp,
314
                                                     uint8_t* output_buffer,
315
                                                     size_t output_buffer_size,
316
                                                     int output_stride);
317
318
// This function allocates and initializes an incremental-decoder object, which
319
// will output the raw luma/chroma samples into a preallocated planes if
320
// supplied. The luma plane is specified by its pointer 'luma', its size
321
// 'luma_size' and its stride 'luma_stride'. Similarly, the chroma-u plane
322
// is specified by the 'u', 'u_size' and 'u_stride' parameters, and the chroma-v
323
// plane by 'v' and 'v_size'. And same for the alpha-plane. The 'a' pointer
324
// can be pass NULL in case one is not interested in the transparency plane.
325
// Conversely, 'luma' can be passed NULL if no preallocated planes are supplied.
326
// In this case, the output buffer will be automatically allocated (using
327
// MODE_YUVA) when decoding starts. All parameters are then ignored.
328
// Returns NULL if the allocation failed or if a parameter is invalid.
329
WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPINewYUVA(
330
    uint8_t* luma, size_t luma_size, int luma_stride, uint8_t* u, size_t u_size,
331
    int u_stride, uint8_t* v, size_t v_size, int v_stride, uint8_t* a,
332
    size_t a_size, int a_stride);
333
334
// Deprecated version of the above, without the alpha plane.
335
// Kept for backward compatibility.
336
WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPINewYUV(
337
    uint8_t* luma, size_t luma_size, int luma_stride, uint8_t* u, size_t u_size,
338
    int u_stride, uint8_t* v, size_t v_size, int v_stride);
339
340
// Deletes the WebPIDecoder object and associated memory. Must always be called
341
// if WebPINewDecoder, WebPINewRGB or WebPINewYUV succeeded.
342
WEBP_EXTERN void WebPIDelete(WebPIDecoder* idec);
343
344
// Copies and decodes the next available data. Returns VP8_STATUS_OK when
345
// the image is successfully decoded. Returns VP8_STATUS_SUSPENDED when more
346
// data is expected. Returns error in other cases.
347
WEBP_EXTERN VP8StatusCode WebPIAppend(WebPIDecoder* idec, const uint8_t* data,
348
                                      size_t data_size);
349
350
// A variant of the above function to be used when data buffer contains
351
// partial data from the beginning. In this case data buffer is not copied
352
// to the internal memory.
353
// Note that the value of the 'data' pointer can change between calls to
354
// WebPIUpdate, for instance when the data buffer is resized to fit larger data.
355
WEBP_EXTERN VP8StatusCode WebPIUpdate(WebPIDecoder* idec, const uint8_t* data,
356
                                      size_t data_size);
357
358
// Returns the RGB/A image decoded so far. Returns NULL if output params
359
// are not initialized yet. The RGB/A output type corresponds to the colorspace
360
// specified during call to WebPINewDecoder() or WebPINewRGB().
361
// *last_y is the index of last decoded row in raster scan order. Some pointers
362
// (*last_y, *width etc.) can be NULL if corresponding information is not
363
// needed. The values in these pointers are only valid on successful (non-NULL)
364
// return.
365
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec,
366
                                                   int* last_y, int* width,
367
                                                   int* height, int* stride);
368
369
// Same as above function to get a YUVA image. Returns pointer to the luma
370
// plane or NULL in case of error. If there is no alpha information
371
// the alpha pointer '*a' will be returned NULL.
372
WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPIDecGetYUVA(const WebPIDecoder* idec,
373
                                                    int* last_y, uint8_t** u,
374
                                                    uint8_t** v, uint8_t** a,
375
                                                    int* width, int* height,
376
                                                    int* stride, int* uv_stride,
377
                                                    int* a_stride);
378
379
// Deprecated alpha-less version of WebPIDecGetYUVA(): it will ignore the
380
// alpha information (if present). Kept for backward compatibility.
381
WEBP_NODISCARD static WEBP_INLINE uint8_t* WebPIDecGetYUV(
382
    const WebPIDecoder* idec, int* last_y, uint8_t** u, uint8_t** v, int* width,
383
0
    int* height, int* stride, int* uv_stride) {
384
0
  return WebPIDecGetYUVA(idec, last_y, u, v, NULL, width, height, stride,
385
0
                         uv_stride, NULL);
386
0
}
387
388
// Generic call to retrieve information about the displayable area.
389
// If non NULL, the left/right/width/height pointers are filled with the visible
390
// rectangular area so far.
391
// Returns NULL in case the incremental decoder object is in an invalid state.
392
// Otherwise returns the pointer to the internal representation. This structure
393
// is read-only, tied to WebPIDecoder's lifespan and should not be modified.
394
WEBP_NODISCARD WEBP_EXTERN const WebPDecBuffer* WebPIDecodedArea(
395
    const WebPIDecoder* idec, int* left, int* top, int* width, int* height);
396
397
//------------------------------------------------------------------------------
398
// Advanced decoding parametrization
399
//
400
//  Code sample for using the advanced decoding API
401
/*
402
     // A) Init a configuration object
403
     WebPDecoderConfig config;
404
     CHECK(WebPInitDecoderConfig(&config));
405
406
     // B) optional: retrieve the bitstream's features.
407
     CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);
408
409
     // C) Adjust 'config', if needed
410
     config.options.no_fancy_upsampling = 1;
411
     config.output.colorspace = MODE_BGRA;
412
     // etc.
413
414
     // Note that you can also make config.output point to an externally
415
     // supplied memory buffer, provided it's big enough to store the decoded
416
     // picture. Otherwise, config.output will just be used to allocate memory
417
     // and store the decoded picture.
418
419
     // D) Decode!
420
     CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK);
421
422
     // E) Decoded image is now in config.output (and config.output.u.RGBA)
423
424
     // F) Reclaim memory allocated in config's object. It's safe to call
425
     // this function even if the memory is external and wasn't allocated
426
     // by WebPDecode().
427
     WebPFreeDecBuffer(&config.output);
428
*/
429
430
// Features gathered from the bitstream
431
struct WebPBitstreamFeatures {
432
  int width;          // Width in pixels, as read from the bitstream.
433
  int height;         // Height in pixels, as read from the bitstream.
434
  int has_alpha;      // True if the bitstream contains an alpha channel.
435
  int has_animation;  // True if the bitstream is an animation.
436
  int format;         // 0 = undefined (/mixed), 1 = lossy, 2 = lossless
437
438
  uint32_t pad[5];  // padding for later use
439
};
440
441
// Internal, version-checked, entry point
442
WEBP_EXTERN VP8StatusCode WebPGetFeaturesInternal(const uint8_t*, size_t,
443
                                                  WebPBitstreamFeatures*, int);
444
445
// Retrieve features from the bitstream. The *features structure is filled
446
// with information gathered from the bitstream.
447
// Returns VP8_STATUS_OK when the features are successfully retrieved. Returns
448
// VP8_STATUS_NOT_ENOUGH_DATA when more data is needed to retrieve the
449
// features from headers. Returns error in other cases.
450
// Note: The following chunk sequences (before the raw VP8/VP8L data) are
451
// considered valid by this function:
452
// RIFF + VP8(L)
453
// RIFF + VP8X + (optional chunks) + VP8(L)
454
// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
455
// VP8(L)     <-- Not a valid WebP format: only allowed for internal purpose.
456
static WEBP_INLINE VP8StatusCode WebPGetFeatures(
457
2.13k
    const uint8_t* data, size_t data_size, WebPBitstreamFeatures* features) {
458
2.13k
  return WebPGetFeaturesInternal(data, data_size, features,
459
2.13k
                                 WEBP_DECODER_ABI_VERSION);
460
2.13k
}
461
462
// Decoding options
463
struct WebPDecoderOptions {
464
  int bypass_filtering;             // if true, skip the in-loop filtering
465
  int no_fancy_upsampling;          // if true, use faster pointwise upsampler
466
  int use_cropping;                 // if true, cropping is applied _first_
467
  int crop_left, crop_top;          // top-left position for cropping.
468
                                    // Will be snapped to even values.
469
  int crop_width, crop_height;      // dimension of the cropping area
470
  int use_scaling;                  // if true, scaling is applied _afterward_
471
  int scaled_width, scaled_height;  // final resolution. if one is 0, it is
472
                                    // guessed from the other one to keep the
473
                                    // original ratio.
474
  int use_threads;                  // if true, use multi-threaded decoding
475
  int dithering_strength;           // dithering strength (0=Off, 100=full)
476
  int flip;                         // if true, flip output vertically
477
  int alpha_dithering_strength;     // alpha dithering strength in [0..100]
478
479
  uint32_t pad[5];  // padding for later use
480
};
481
482
// Main object storing the configuration for advanced decoding.
483
struct WebPDecoderConfig {
484
  WebPBitstreamFeatures input;  // Immutable bitstream features (optional)
485
  WebPDecBuffer output;         // Output buffer (can point to external mem)
486
  WebPDecoderOptions options;   // Decoding options
487
};
488
489
// Internal, version-checked, entry point
490
WEBP_NODISCARD WEBP_EXTERN int WebPInitDecoderConfigInternal(WebPDecoderConfig*,
491
                                                             int);
492
493
// Initialize the configuration as empty. This function must always be
494
// called first, unless WebPGetFeatures() is to be called.
495
// Returns false in case of mismatched version.
496
WEBP_NODISCARD static WEBP_INLINE int WebPInitDecoderConfig(
497
689
    WebPDecoderConfig* config) {
498
689
  return WebPInitDecoderConfigInternal(config, WEBP_DECODER_ABI_VERSION);
499
689
}
500
501
// Returns true if 'config' is non-NULL and all configuration parameters are
502
// within their valid ranges.
503
WEBP_NODISCARD WEBP_EXTERN int WebPValidateDecoderConfig(
504
    const WebPDecoderConfig* config);
505
506
// Instantiate a new incremental decoder object with the requested
507
// configuration. The bitstream can be passed using 'data' and 'data_size'
508
// parameter, in which case the features will be parsed and stored into
509
// config->input. Otherwise, 'data' can be NULL and no parsing will occur.
510
// Note that 'config' can be NULL too, in which case a default configuration
511
// is used. If 'config' is not NULL, it must outlive the WebPIDecoder object
512
// as some references to its fields will be used. No internal copy of 'config'
513
// is made.
514
// The return WebPIDecoder object must always be deleted calling WebPIDelete().
515
// Returns NULL in case of error (and config->status will then reflect
516
// the error condition, if available).
517
WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPIDecode(const uint8_t* data,
518
                                                     size_t data_size,
519
                                                     WebPDecoderConfig* config);
520
521
// Non-incremental version. This version decodes the full data at once, taking
522
// 'config' into account. Returns decoding status (which should be VP8_STATUS_OK
523
// if the decoding was successful). Note that 'config' cannot be NULL.
524
WEBP_EXTERN VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
525
                                     WebPDecoderConfig* config);
526
527
#ifdef __cplusplus
528
}  // extern "C"
529
#endif
530
531
#endif  // WEBP_WEBP_DECODE_H_