2010-09-30 15:34:38 +02:00
|
|
|
// Copyright 2010 Google Inc.
|
|
|
|
//
|
|
|
|
// This code is licensed under the same terms as WebM:
|
|
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Main decoding functions for WEBP images.
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
2011-02-01 07:00:33 +01:00
|
|
|
#ifndef WEBP_WEBP_DECODE_H_
|
|
|
|
#define WEBP_WEBP_DECODE_H_
|
2010-09-30 15:34:38 +02:00
|
|
|
|
2011-01-06 16:25:43 +01:00
|
|
|
#include "webp/types.h"
|
2010-09-30 15:34:38 +02:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-03-25 00:17:10 +01:00
|
|
|
// Return the decoder's version number, packed in hexadecimal using 8bits for
|
|
|
|
// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
|
2011-03-25 23:04:11 +01:00
|
|
|
int WebPGetDecoderVersion(void);
|
2011-03-25 00:17:10 +01:00
|
|
|
|
2010-09-30 15:34:38 +02:00
|
|
|
// Retrieve basic header information: width, height.
|
|
|
|
// This function will also validate the header and return 0 in
|
|
|
|
// case of formatting error.
|
|
|
|
// Pointers *width/*height can be passed NULL if deemed irrelevant.
|
|
|
|
int WebPGetInfo(const uint8_t* data, uint32_t data_size,
|
|
|
|
int *width, int *height);
|
|
|
|
|
|
|
|
// Decodes WEBP images pointed to by *data and returns RGB samples, along
|
|
|
|
// with the dimensions in *width and *height.
|
|
|
|
// The returned pointer should be deleted calling free().
|
|
|
|
// Returns NULL in case of error.
|
|
|
|
uint8_t* WebPDecodeRGB(const uint8_t* data, uint32_t data_size,
|
|
|
|
int *width, int *height);
|
|
|
|
|
|
|
|
// Same as WebPDecodeRGB, but returning RGBA data.
|
|
|
|
uint8_t* WebPDecodeRGBA(const uint8_t* data, uint32_t data_size,
|
|
|
|
int *width, int *height);
|
|
|
|
|
|
|
|
// This variant decode to BGR instead of RGB.
|
|
|
|
uint8_t* WebPDecodeBGR(const uint8_t* data, uint32_t data_size,
|
|
|
|
int *width, int *height);
|
|
|
|
// This variant decodes to BGRA instead of RGBA.
|
|
|
|
uint8_t* WebPDecodeBGRA(const uint8_t* data, uint32_t data_size,
|
|
|
|
int *width, int *height);
|
|
|
|
|
|
|
|
// Decode WEBP images stored in *data in Y'UV format(*). The pointer returned is
|
|
|
|
// the Y samples buffer. Upon return, *u and *v will point to the U and V
|
|
|
|
// chroma data. These U and V buffers need NOT be free()'d, unlike the returned
|
|
|
|
// Y luma one. The dimension of the U and V planes are both (*width + 1) / 2
|
|
|
|
// and (*height + 1)/ 2.
|
|
|
|
// Upon return, the Y buffer has a stride returned as '*stride', while U and V
|
|
|
|
// have a common stride returned as '*uv_stride'.
|
|
|
|
// Return NULL in case of error.
|
|
|
|
// (*) Also named Y'CbCr. See: http://en.wikipedia.org/wiki/YCbCr
|
|
|
|
uint8_t* WebPDecodeYUV(const uint8_t* data, uint32_t data_size,
|
|
|
|
int *width, int *height, uint8_t** u, uint8_t** v,
|
|
|
|
int *stride, int* uv_stride);
|
|
|
|
|
|
|
|
// These three functions are variants of the above ones, that decode the image
|
|
|
|
// directly into a pre-allocated buffer 'output_buffer'. The maximum storage
|
|
|
|
// available in this buffer is indicated by 'output_buffer_size'. If this
|
|
|
|
// storage is not sufficient (or an error occurred), NULL is returned.
|
|
|
|
// Otherwise, output_buffer is returned, for convenience.
|
|
|
|
// The parameter 'output_stride' specifies the distance (in bytes)
|
|
|
|
// between scanlines. Hence, output_buffer_size is expected to be at least
|
|
|
|
// output_stride x picture-height.
|
|
|
|
uint8_t* WebPDecodeRGBInto(const uint8_t* data, uint32_t data_size,
|
|
|
|
uint8_t* output_buffer, int output_buffer_size,
|
|
|
|
int output_stride);
|
|
|
|
uint8_t* WebPDecodeRGBAInto(const uint8_t* data, uint32_t data_size,
|
|
|
|
uint8_t* output_buffer, int output_buffer_size,
|
|
|
|
int output_stride);
|
|
|
|
// BGR variants
|
|
|
|
uint8_t* WebPDecodeBGRInto(const uint8_t* data, uint32_t data_size,
|
|
|
|
uint8_t* output_buffer, int output_buffer_size,
|
|
|
|
int output_stride);
|
|
|
|
uint8_t* WebPDecodeBGRAInto(const uint8_t* data, uint32_t data_size,
|
|
|
|
uint8_t* output_buffer, int output_buffer_size,
|
|
|
|
int output_stride);
|
|
|
|
|
|
|
|
// WebPDecodeYUVInto() is a variant of WebPDecodeYUV() that operates directly
|
|
|
|
// into pre-allocated luma/chroma plane buffers. This function requires the
|
|
|
|
// strides to be passed: one for the luma plane and one for each of the
|
|
|
|
// chroma ones. The size of each plane buffer is passed as 'luma_size',
|
|
|
|
// 'u_size' and 'v_size' respectively.
|
|
|
|
// Pointer to the luma plane ('*luma') is returned or NULL if an error occurred
|
|
|
|
// during decoding (or because some buffers were found to be too small).
|
|
|
|
uint8_t* WebPDecodeYUVInto(const uint8_t* data, uint32_t data_size,
|
|
|
|
uint8_t* luma, int luma_size, int luma_stride,
|
|
|
|
uint8_t* u, int u_size, int u_stride,
|
|
|
|
uint8_t* v, int v_size, int v_stride);
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-03-24 02:03:53 +01:00
|
|
|
// Output colorspaces
|
|
|
|
typedef enum { MODE_RGB = 0, MODE_RGBA = 1,
|
|
|
|
MODE_BGR = 2, MODE_BGRA = 3,
|
|
|
|
MODE_YUV = 4 } WEBP_CSP_MODE;
|
|
|
|
|
|
|
|
// Enumeration of the status codes
|
|
|
|
typedef enum {
|
|
|
|
VP8_STATUS_OK = 0,
|
|
|
|
VP8_STATUS_OUT_OF_MEMORY,
|
|
|
|
VP8_STATUS_INVALID_PARAM,
|
|
|
|
VP8_STATUS_BITSTREAM_ERROR,
|
|
|
|
VP8_STATUS_UNSUPPORTED_FEATURE,
|
|
|
|
VP8_STATUS_SUSPENDED,
|
|
|
|
VP8_STATUS_USER_ABORT,
|
|
|
|
VP8_STATUS_NOT_ENOUGH_DATA
|
|
|
|
} VP8StatusCode;
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Incremental decoding
|
|
|
|
//
|
|
|
|
// This API allows streamlined decoding of partial data.
|
|
|
|
// Picture can be incrementally decoded as data become available thanks to the
|
|
|
|
// WebPIDecoder object. This object can be left in a SUSPENDED state if the
|
|
|
|
// picture is only partially decoded, pending additional input.
|
|
|
|
// Code example:
|
|
|
|
//
|
|
|
|
// WebPIDecoder* const idec = WebPINew(mode);
|
|
|
|
// while (has_more_data) {
|
|
|
|
// // ... (get additional data)
|
|
|
|
// status = WebPIAppend(idec, new_data, new_data_size);
|
|
|
|
// if (status != VP8_STATUS_SUSPENDED ||
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // The above call decodes the current available buffer.
|
|
|
|
// // Part of the image can now be refreshed by calling to
|
|
|
|
// // WebPIDecGetRGB()/WebPIDecGetYUV() etc.
|
|
|
|
// }
|
|
|
|
// WebPIDelete(idec);
|
|
|
|
|
|
|
|
typedef struct WebPIDecoder WebPIDecoder;
|
|
|
|
|
|
|
|
// Creates a WebPIDecoder object. Returns NULL in case of failure.
|
|
|
|
WebPIDecoder* WebPINew(WEBP_CSP_MODE mode);
|
|
|
|
|
|
|
|
// This function allocates and initializes an incremental-decoder object, which
|
|
|
|
// will output the r/g/b(/a) samples specified by 'mode' into a preallocated
|
|
|
|
// buffer 'output_buffer'. The size of this buffer is at least
|
|
|
|
// 'output_buffer_size' and the stride (distance in bytes between two scanlines)
|
|
|
|
// is specified by 'output_stride'. Returns NULL if the allocation failed.
|
|
|
|
WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE mode, uint8_t* output_buffer,
|
|
|
|
int output_buffer_size, int output_stride);
|
|
|
|
|
|
|
|
// This function allocates and initializes an incremental-decoder object, which
|
|
|
|
// will output the raw luma/chroma samples into a preallocated planes. The luma
|
|
|
|
// plane is specified by its pointer 'luma', its size 'luma_size' and its stride
|
|
|
|
// 'luma_stride'. Similarly, the chroma-u plane is specified by the 'u',
|
|
|
|
// 'u_size' and 'u_stride' parameters, and the chroma-v plane by 'v', 'v_size'
|
|
|
|
// and 'v_size'.
|
|
|
|
// Returns NULL if the allocation failed.
|
|
|
|
WebPIDecoder* WebPINewYUV(uint8_t* luma, int luma_size, int luma_stride,
|
|
|
|
uint8_t* u, int u_size, int u_stride,
|
|
|
|
uint8_t* v, int v_size, int v_stride);
|
|
|
|
|
|
|
|
// Deletes the WebpBuffer object and associated memory. Must always be called
|
|
|
|
// if WebPINew, WebPINewRGB or WebPINewYUV succeeded.
|
|
|
|
void WebPIDelete(WebPIDecoder* const idec);
|
|
|
|
|
|
|
|
// Copies and decodes the next available data. Returns VP8_STATUS_OK when
|
|
|
|
// the image is successfully decoded. Returns VP8_STATUS_SUSPENDED when more
|
|
|
|
// data is expected. Returns error in other cases.
|
|
|
|
VP8StatusCode WebPIAppend(WebPIDecoder* const idec, const uint8_t* data,
|
|
|
|
uint32_t data_size);
|
|
|
|
|
|
|
|
// A variant of the above function to be used when data buffer contains
|
|
|
|
// partial data from the beginning. In this case data buffer is not copied
|
|
|
|
// to the internal memory.
|
2011-03-25 23:04:11 +01:00
|
|
|
// Note that the value of the 'data' pointer can change between calls to
|
|
|
|
// WebPIUpdate, for instance when the data buffer is resized to fit larger data.
|
2011-03-24 02:03:53 +01:00
|
|
|
VP8StatusCode WebPIUpdate(WebPIDecoder* const idec, const uint8_t* data,
|
|
|
|
uint32_t data_size);
|
|
|
|
|
EXPERIMENTAL: add support for alpha channel
This is a (minor) bitstream change: if the 'color_space' bit is set to '1'
(which is normally an undefined/invalid behaviour), we add extra data at the
end of partition #0 (so-called 'extensions')
Namely, we add the size of the extension data as 3 bytes (little-endian),
followed by a set of bits telling which extensions we're incorporating.
The data then _preceeds_ this trailing tags.
This is all experimental, and you'll need to have
'#define WEBP_EXPERIMENTAL_FEATURES' in webp/types.h to enable this code
(at your own risk! :))
Still, this hack produces almost-valid WebP file for decoders that don't
check this color_space bit. In particular, previous 'dwebp' (and for instance
Chrome) will recognize this files and decode them, but without the alpha
of course. Other decoder will just see random extra stuff at the end of
partition #0.
To experiment with the alpha-channel, you need to compile on Unix platform
and use PNGs for input/output.
If 'alpha.png' is a source with alpha channel, then you can try (on Unix):
cwebp alpha.png -o alpha.webp
dwebp alpha.webp -o test.png
cwebp now has a '-noalpha' flag to ignore any alpha information from the
source, if present.
More hacking and experimenting welcome!
Change-Id: I3c7b1fd8411c9e7a9f77690e898479ad85c52f3e
2011-04-26 01:58:04 +02:00
|
|
|
// Returns the RGB image decoded so far. Returns NULL if output params are not
|
|
|
|
// initialized yet. *last_y is the index of last decoded row in raster scan
|
|
|
|
// order. Some pointers (*last_y, *width etc.) can be NULL if corresponding
|
|
|
|
// information is not needed.
|
2011-03-24 02:03:53 +01:00
|
|
|
uint8_t* WebPIDecGetRGB(const WebPIDecoder* const idec, int *last_y,
|
|
|
|
int* width, int* height, int* stride);
|
|
|
|
|
|
|
|
// Same as above function to get YUV image. Returns pointer to the luma plane
|
|
|
|
// or NULL in case of error.
|
|
|
|
uint8_t* WebPIDecGetYUV(const WebPIDecoder* const idec, int* last_y,
|
|
|
|
uint8_t** u, uint8_t** v,
|
|
|
|
int* width, int* height, int* stride, int* uv_stride);
|
|
|
|
|
|
|
|
|
2010-09-30 15:34:38 +02:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2011-02-16 23:33:16 +01:00
|
|
|
#endif /* WEBP_WEBP_DECODE_H_ */
|