1
1
// Copyright 2010 Google Inc. All Rights Reserved.
2
2
//
3
- // This code is licensed under the same terms as WebM:
4
- // Software License Agreement: http://www.webmproject.org/license/software/
5
- // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
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.
6
8
// -----------------------------------------------------------------------------
7
9
//
8
10
// Main decoding functions for WebP images.
14
16
15
17
#include "./types.h"
16
18
17
- #if defined( __cplusplus ) || defined( c_plusplus )
19
+ #ifdef __cplusplus
18
20
extern "C" {
19
21
#endif
20
22
21
- #define WEBP_DECODER_ABI_VERSION 0x0200 // MAJOR(8b) + MINOR(8b)
23
+ #define WEBP_DECODER_ABI_VERSION 0x0208 // MAJOR(8b) + MINOR(8b)
24
+
25
+ // Note: forward declaring enumerations is not allowed in (strict) C and C++,
26
+ // the types are left here for reference.
27
+ // typedef enum VP8StatusCode VP8StatusCode;
28
+ // typedef enum WEBP_CSP_MODE WEBP_CSP_MODE;
29
+ typedef struct WebPRGBABuffer WebPRGBABuffer ;
30
+ typedef struct WebPYUVABuffer WebPYUVABuffer ;
31
+ typedef struct WebPDecBuffer WebPDecBuffer ;
32
+ typedef struct WebPIDecoder WebPIDecoder ;
33
+ typedef struct WebPBitstreamFeatures WebPBitstreamFeatures ;
34
+ typedef struct WebPDecoderOptions WebPDecoderOptions ;
35
+ typedef struct WebPDecoderConfig WebPDecoderConfig ;
22
36
23
37
// Return the decoder's version number, packed in hexadecimal using 8bits for
24
38
// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
@@ -34,7 +48,7 @@ WEBP_EXTERN(int) WebPGetInfo(const uint8_t* data, size_t data_size,
34
48
// Decodes WebP images pointed to by 'data' and returns RGBA samples, along
35
49
// with the dimensions in *width and *height. The ordering of samples in
36
50
// memory is R, G, B, A, R, G, B, A... in scan order (endian-independent).
37
- // The returned pointer should be deleted calling free ().
51
+ // The returned pointer should be deleted calling WebPFree ().
38
52
// Returns NULL in case of error.
39
53
WEBP_EXTERN (uint8_t * ) WebPDecodeRGBA (const uint8_t * data , size_t data_size ,
40
54
int * width , int * height );
@@ -59,9 +73,9 @@ WEBP_EXTERN(uint8_t*) WebPDecodeBGR(const uint8_t* data, size_t data_size,
59
73
60
74
// Decode WebP images pointed to by 'data' to Y'UV format(*). The pointer
61
75
// returned is the Y samples buffer. Upon return, *u and *v will point to
62
- // the U and V chroma data. These U and V buffers need NOT be free()'d,
63
- // unlike the returned Y luma one. The dimension of the U and V planes
64
- // are both (*width + 1) / 2 and (*height + 1)/ 2.
76
+ // the U and V chroma data. These U and V buffers need NOT be passed to
77
+ // WebPFree(), unlike the returned Y luma one. The dimension of the U and V
78
+ // planes are both (*width + 1) / 2 and (*height + 1)/ 2.
65
79
// Upon return, the Y buffer has a stride returned as '*stride', while U and V
66
80
// have a common stride returned as '*uv_stride'.
67
81
// Return NULL in case of error.
@@ -71,6 +85,9 @@ WEBP_EXTERN(uint8_t*) WebPDecodeYUV(const uint8_t* data, size_t data_size,
71
85
uint8_t * * u , uint8_t * * v ,
72
86
int * stride , int * uv_stride );
73
87
88
+ // Releases memory returned by the WebPDecode*() functions above.
89
+ WEBP_EXTERN (void ) WebPFree (void * ptr );
90
+
74
91
// These five functions are variants of the above ones, that decode the image
75
92
// directly into a pre-allocated buffer 'output_buffer'. The maximum storage
76
93
// available in this buffer is indicated by 'output_buffer_size'. If this
@@ -118,20 +135,28 @@ WEBP_EXTERN(uint8_t*) WebPDecodeYUVInto(
118
135
// Note: the naming describes the byte-ordering of packed samples in memory.
119
136
// For instance, MODE_BGRA relates to samples ordered as B,G,R,A,B,G,R,A,...
120
137
// Non-capital names (e.g.:MODE_Argb) relates to pre-multiplied RGB channels.
121
- // RGB-565 and RGBA-4444 are also endian-agnostic and byte-oriented.
122
- typedef enum { MODE_RGB = 0 , MODE_RGBA = 1 ,
123
- MODE_BGR = 2 , MODE_BGRA = 3 ,
124
- MODE_ARGB = 4 , MODE_RGBA_4444 = 5 ,
125
- MODE_RGB_565 = 6 ,
126
- // RGB-premultiplied transparent modes (alpha value is preserved)
127
- MODE_rgbA = 7 ,
128
- MODE_bgrA = 8 ,
129
- MODE_Argb = 9 ,
130
- MODE_rgbA_4444 = 10 ,
131
- // YUV modes must come after RGB ones.
132
- MODE_YUV = 11 , MODE_YUVA = 12 , // yuv 4:2:0
133
- MODE_LAST = 13
134
- } WEBP_CSP_MODE ;
138
+ // RGBA-4444 and RGB-565 colorspaces are represented by following byte-order:
139
+ // RGBA-4444: [r3 r2 r1 r0 g3 g2 g1 g0], [b3 b2 b1 b0 a3 a2 a1 a0], ...
140
+ // RGB-565: [r4 r3 r2 r1 r0 g5 g4 g3], [g2 g1 g0 b4 b3 b2 b1 b0], ...
141
+ // In the case WEBP_SWAP_16BITS_CSP is defined, the bytes are swapped for
142
+ // these two modes:
143
+ // RGBA-4444: [b3 b2 b1 b0 a3 a2 a1 a0], [r3 r2 r1 r0 g3 g2 g1 g0], ...
144
+ // RGB-565: [g2 g1 g0 b4 b3 b2 b1 b0], [r4 r3 r2 r1 r0 g5 g4 g3], ...
145
+
146
+ typedef enum WEBP_CSP_MODE {
147
+ MODE_RGB = 0 , MODE_RGBA = 1 ,
148
+ MODE_BGR = 2 , MODE_BGRA = 3 ,
149
+ MODE_ARGB = 4 , MODE_RGBA_4444 = 5 ,
150
+ MODE_RGB_565 = 6 ,
151
+ // RGB-premultiplied transparent modes (alpha value is preserved)
152
+ MODE_rgbA = 7 ,
153
+ MODE_bgrA = 8 ,
154
+ MODE_Argb = 9 ,
155
+ MODE_rgbA_4444 = 10 ,
156
+ // YUV modes must come after RGB ones.
157
+ MODE_YUV = 11 , MODE_YUVA = 12 , // yuv 4:2:0
158
+ MODE_LAST = 13
159
+ } WEBP_CSP_MODE ;
135
160
136
161
// Some useful macros:
137
162
static WEBP_INLINE int WebPIsPremultipliedMode (WEBP_CSP_MODE mode ) {
@@ -152,24 +177,24 @@ static WEBP_INLINE int WebPIsRGBMode(WEBP_CSP_MODE mode) {
152
177
//------------------------------------------------------------------------------
153
178
// WebPDecBuffer: Generic structure for describing the output sample buffer.
154
179
155
- typedef struct { // view as RGBA
180
+ struct WebPRGBABuffer { // view as RGBA
156
181
uint8_t * rgba ; // pointer to RGBA samples
157
182
int stride ; // stride in bytes from one scanline to the next.
158
183
size_t size ; // total size of the *rgba buffer.
159
- } WebPRGBABuffer ;
184
+ };
160
185
161
- typedef struct { // view as YUVA
186
+ struct WebPYUVABuffer { // view as YUVA
162
187
uint8_t * y , * u , * v , * a ; // pointer to luma, chroma U/V, alpha samples
163
188
int y_stride ; // luma stride
164
189
int u_stride , v_stride ; // chroma strides
165
190
int a_stride ; // alpha stride
166
191
size_t y_size ; // luma plane size
167
192
size_t u_size , v_size ; // chroma planes size
168
193
size_t a_size ; // alpha-plane size
169
- } WebPYUVABuffer ;
194
+ };
170
195
171
196
// Output buffer
172
- typedef struct {
197
+ struct WebPDecBuffer {
173
198
WEBP_CSP_MODE colorspace ; // Colorspace.
174
199
int width , height ; // Dimensions.
175
200
int is_external_memory ; // If true, 'internal_memory' pointer is not used.
@@ -182,7 +207,7 @@ typedef struct {
182
207
uint8_t * private_memory ; // Internally allocated memory (only when
183
208
// is_external_memory is false). Should not be used
184
209
// externally, but accessed via the buffer union.
185
- } WebPDecBuffer ;
210
+ };
186
211
187
212
// Internal, version-checked, entry point
188
213
WEBP_EXTERN (int ) WebPInitDecBufferInternal (WebPDecBuffer * , int );
@@ -200,7 +225,7 @@ WEBP_EXTERN(void) WebPFreeDecBuffer(WebPDecBuffer* buffer);
200
225
//------------------------------------------------------------------------------
201
226
// Enumeration of the status codes
202
227
203
- typedef enum {
228
+ typedef enum VP8StatusCode {
204
229
VP8_STATUS_OK = 0 ,
205
230
VP8_STATUS_OUT_OF_MEMORY ,
206
231
VP8_STATUS_INVALID_PARAM ,
@@ -237,33 +262,45 @@ typedef enum {
237
262
// }
238
263
// WebPIDelete(idec);
239
264
240
- typedef struct WebPIDecoder WebPIDecoder ;
241
-
242
265
// Creates a new incremental decoder with the supplied buffer parameter.
243
266
// This output_buffer can be passed NULL, in which case a default output buffer
244
267
// is used (with MODE_RGB). Otherwise, an internal reference to 'output_buffer'
245
268
// is kept, which means that the lifespan of 'output_buffer' must be larger than
246
269
// that of the returned WebPIDecoder object.
270
+ // The supplied 'output_buffer' content MUST NOT be changed between calls to
271
+ // WebPIAppend() or WebPIUpdate() unless 'output_buffer.is_external_memory' is
272
+ // set to 1. In such a case, it is allowed to modify the pointers, size and
273
+ // stride of output_buffer.u.RGBA or output_buffer.u.YUVA, provided they remain
274
+ // within valid bounds.
275
+ // All other fields of WebPDecBuffer MUST remain constant between calls.
247
276
// Returns NULL if the allocation failed.
248
277
WEBP_EXTERN (WebPIDecoder * ) WebPINewDecoder (WebPDecBuffer * output_buffer );
249
278
250
279
// This function allocates and initializes an incremental-decoder object, which
251
280
// will output the RGB/A samples specified by 'csp' into a preallocated
252
281
// buffer 'output_buffer'. The size of this buffer is at least
253
282
// 'output_buffer_size' and the stride (distance in bytes between two scanlines)
254
- // is specified by 'output_stride'. Returns NULL if the allocation failed.
283
+ // is specified by 'output_stride'.
284
+ // Additionally, output_buffer can be passed NULL in which case the output
285
+ // buffer will be allocated automatically when the decoding starts. The
286
+ // colorspace 'csp' is taken into account for allocating this buffer. All other
287
+ // parameters are ignored.
288
+ // Returns NULL if the allocation failed, or if some parameters are invalid.
255
289
WEBP_EXTERN (WebPIDecoder * ) WebPINewRGB (
256
290
WEBP_CSP_MODE csp ,
257
291
uint8_t * output_buffer , size_t output_buffer_size , int output_stride );
258
292
259
293
// This function allocates and initializes an incremental-decoder object, which
260
- // will output the raw luma/chroma samples into a preallocated planes. The luma
261
- // plane is specified by its pointer 'luma', its size 'luma_size' and its stride
262
- // 'luma_stride'. Similarly, the chroma-u plane is specified by the 'u',
263
- // 'u_size' and 'u_stride' parameters, and the chroma-v plane by 'v'
264
- // and 'v_size'. And same for the alpha-plane. The 'a' pointer can be pass
265
- // NULL in case one is not interested in the transparency plane.
266
- // Returns NULL if the allocation failed.
294
+ // will output the raw luma/chroma samples into a preallocated planes if
295
+ // supplied. The luma plane is specified by its pointer 'luma', its size
296
+ // 'luma_size' and its stride 'luma_stride'. Similarly, the chroma-u plane
297
+ // is specified by the 'u', 'u_size' and 'u_stride' parameters, and the chroma-v
298
+ // plane by 'v' and 'v_size'. And same for the alpha-plane. The 'a' pointer
299
+ // can be pass NULL in case one is not interested in the transparency plane.
300
+ // Conversely, 'luma' can be passed NULL if no preallocated planes are supplied.
301
+ // In this case, the output buffer will be automatically allocated (using
302
+ // MODE_YUVA) when decoding starts. All parameters are then ignored.
303
+ // Returns NULL if the allocation failed or if a parameter is invalid.
267
304
WEBP_EXTERN (WebPIDecoder * ) WebPINewYUVA (
268
305
uint8_t * luma , size_t luma_size , int luma_stride ,
269
306
uint8_t * u , size_t u_size , int u_stride ,
@@ -344,7 +381,7 @@ WEBP_EXTERN(const WebPDecBuffer*) WebPIDecodedArea(
344
381
CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);
345
382
346
383
// C) Adjust 'config', if needed
347
- config.no_fancy = 1;
384
+ config.no_fancy_upsampling = 1;
348
385
config.output.colorspace = MODE_BGRA;
349
386
// etc.
350
387
@@ -365,28 +402,25 @@ WEBP_EXTERN(const WebPDecBuffer*) WebPIDecodedArea(
365
402
*/
366
403
367
404
// Features gathered from the bitstream
368
- typedef struct {
369
- int width ; // Width in pixels, as read from the bitstream.
370
- int height ; // Height in pixels, as read from the bitstream.
371
- int has_alpha ; // True if the bitstream contains an alpha channel.
372
-
373
- // Unused for now:
374
- int bitstream_version ; // should be 0 for now. TODO(later)
375
- int no_incremental_decoding ; // if true, using incremental decoding is not
376
- // recommended.
377
- int rotate ; // TODO(later)
378
- int uv_sampling ; // should be 0 for now. TODO(later)
379
- uint32_t pad [3 ]; // padding for later use
380
- } WebPBitstreamFeatures ;
405
+ struct WebPBitstreamFeatures {
406
+ int width ; // Width in pixels, as read from the bitstream.
407
+ int height ; // Height in pixels, as read from the bitstream.
408
+ int has_alpha ; // True if the bitstream contains an alpha channel.
409
+ int has_animation ; // True if the bitstream is an animation.
410
+ int format ; // 0 = undefined (/mixed), 1 = lossy, 2 = lossless
411
+
412
+ uint32_t pad [5 ]; // padding for later use
413
+ };
381
414
382
415
// Internal, version-checked, entry point
383
416
WEBP_EXTERN (VP8StatusCode ) WebPGetFeaturesInternal (
384
417
const uint8_t * , size_t , WebPBitstreamFeatures * , int );
385
418
386
419
// Retrieve features from the bitstream. The *features structure is filled
387
420
// with information gathered from the bitstream.
388
- // Returns false in case of error or version mismatch.
389
- // In case of error, features->bitstream_status will reflect the error code.
421
+ // Returns VP8_STATUS_OK when the features are successfully retrieved. Returns
422
+ // VP8_STATUS_NOT_ENOUGH_DATA when more data is needed to retrieve the
423
+ // features from headers. Returns error in other cases.
390
424
static WEBP_INLINE VP8StatusCode WebPGetFeatures (
391
425
const uint8_t * data , size_t data_size ,
392
426
WebPBitstreamFeatures * features ) {
@@ -395,7 +429,7 @@ static WEBP_INLINE VP8StatusCode WebPGetFeatures(
395
429
}
396
430
397
431
// Decoding options
398
- typedef struct {
432
+ struct WebPDecoderOptions {
399
433
int bypass_filtering ; // if true, skip the in-loop filtering
400
434
int no_fancy_upsampling ; // if true, use faster pointwise upsampler
401
435
int use_cropping ; // if true, cropping is applied _first_
@@ -405,19 +439,19 @@ typedef struct {
405
439
int use_scaling ; // if true, scaling is applied _afterward_
406
440
int scaled_width , scaled_height ; // final resolution
407
441
int use_threads ; // if true, use multi-threaded decoding
442
+ int dithering_strength ; // dithering strength (0=Off, 100=full)
443
+ int flip ; // flip output vertically
444
+ int alpha_dithering_strength ; // alpha dithering strength in [0..100]
408
445
409
- // Unused for now:
410
- int force_rotation ; // forced rotation (to be applied _last_)
411
- int no_enhancement ; // if true, discard enhancement layer
412
- uint32_t pad [6 ]; // padding for later use
413
- } WebPDecoderOptions ;
446
+ uint32_t pad [5 ]; // padding for later use
447
+ };
414
448
415
449
// Main object storing the configuration for advanced decoding.
416
- typedef struct {
450
+ struct WebPDecoderConfig {
417
451
WebPBitstreamFeatures input ; // Immutable bitstream features (optional)
418
452
WebPDecBuffer output ; // Output buffer (can point to external mem)
419
453
WebPDecoderOptions options ; // Decoding options
420
- } WebPDecoderConfig ;
454
+ };
421
455
422
456
// Internal, version-checked, entry point
423
457
WEBP_EXTERN (int ) WebPInitDecoderConfigInternal (WebPDecoderConfig * , int );
@@ -447,7 +481,7 @@ WEBP_EXTERN(WebPIDecoder*) WebPIDecode(const uint8_t* data, size_t data_size,
447
481
WEBP_EXTERN (VP8StatusCode ) WebPDecode (const uint8_t * data , size_t data_size ,
448
482
WebPDecoderConfig * config );
449
483
450
- #if defined( __cplusplus ) || defined( c_plusplus )
484
+ #ifdef __cplusplus
451
485
} // extern "C"
452
486
#endif
453
487
0 commit comments