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
This commit is contained in:
Pascal Massimino
2011-04-25 16:58:04 -07:00
parent cfbf88a6c4
commit 2ab4b72f53
27 changed files with 477 additions and 59 deletions

View File

@ -178,11 +178,10 @@ VP8StatusCode WebPIAppend(WebPIDecoder* const idec, const uint8_t* data,
VP8StatusCode WebPIUpdate(WebPIDecoder* const idec, const uint8_t* data,
uint32_t data_size);
// Returns the r/g/b/(a) image decoded so far. Returns NULL if output params
// are not initialized yet. The r/g/b/(a) output type corresponds to the mode
// specified in WebPINew()/WebPINewRGB(). *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.
// 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.
uint8_t* WebPIDecGetRGB(const WebPIDecoder* const idec, int *last_y,
int* width, int* height, int* stride);

View File

@ -18,7 +18,7 @@
extern "C" {
#endif
#define WEBP_DECODER_ABI_VERSION 0x0001
#define WEBP_DECODER_ABI_VERSION 0x0002
//-----------------------------------------------------------------------------
// Lower-level API
@ -80,6 +80,9 @@ struct VP8Io {
// of more visible blocking. Note that output will also be non-compliant
// with the VP8 specifications.
int bypass_filtering;
// pointer to the alpha data (if present) corresponding to the rows
const uint8_t* a;
};
// Internal, version-checked, entry point

View File

@ -20,7 +20,7 @@
extern "C" {
#endif
#define WEBP_ENCODER_ABI_VERSION 0x0001
#define WEBP_ENCODER_ABI_VERSION 0x0002
// Return the encoder's version number, packed in hexadecimal using 8bits for
// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
@ -32,7 +32,6 @@ int WebPGetEncoderVersion(void);
// Returns the size of the compressed data (pointed to by *output), or 0 if
// an error occurred. The compressed data must be released by the caller
// using the call 'free(*output)'.
// Currently, alpha values are discarded.
size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride,
float quality_factor, uint8_t** output);
size_t WebPEncodeBGR(const uint8_t* bgr, int width, int height, int stride,
@ -66,6 +65,7 @@ typedef struct {
int preprocessing; // preprocessing filter (0=none, 1=segment-smooth)
int partitions; // log2(number of token partitions) in [0..3]
// Default is set to 0 for easier progressive decoding.
int alpha_compression; // Algorithm for optimizing the alpha plane (0 = none)
} WebPConfig;
// Enumerate some predefined settings for WebPConfig, depending on the type
@ -120,6 +120,8 @@ typedef struct {
int segment_size[4]; // number of macroblocks in each segments
int segment_quant[4]; // quantizer values for each segments
int segment_level[4]; // filtering strength for each segments [0..63]
int alpha_data_size; // size of the transparency data
} WebPAuxStats;
// Signature for output function. Should return 1 if writing was successful.
@ -134,7 +136,7 @@ struct WebPPicture {
int width, height; // dimensions.
uint8_t *y, *u, *v; // pointers to luma/chroma planes.
int y_stride, uv_stride; // luma/chroma strides.
uint8_t *a; // pointer to the alpha plane (unused for now).
uint8_t *a; // pointer to the width x height alpha plane
// output
WebPWriterFunction writer; // can be NULL
@ -173,6 +175,10 @@ static inline int WebPPictureInit(WebPPicture* const picture) {
// Returns 0 in case of memory error.
int WebPPictureAlloc(WebPPicture* const picture);
// This function will add storage for a transparency plane to a picture, using
// its width and depth.
int WebPPictureAddAlphaPlane(WebPPicture* const picture);
// Release memory allocated by WebPPictureAlloc() or WebPPictureImport*()
// Note that this function does _not_ free the memory pointed to by 'picture'.
void WebPPictureFree(WebPPicture* const picture);
@ -187,16 +193,17 @@ int WebPPictureCopy(const WebPPicture* const src, WebPPicture* const dst);
int WebPPictureCrop(WebPPicture* const picture,
int left, int top, int width, int height);
// Colorspace conversion function. Previous buffer will be free'd, if any.
// Colorspace conversion function to import RGB samples.
// Previous buffer will be free'd, if any.
// *rgb buffer should have a size of at least height * rgb_stride.
// Returns 0 in case of memory error.
int WebPPictureImportRGB(WebPPicture* const picture,
const uint8_t* const rgb, int rgb_stride);
// Same, but for RGBA buffer. Alpha information is ignored.
// Same, but for RGBA buffer
int WebPPictureImportRGBA(WebPPicture* const picture,
const uint8_t* const rgba, int rgba_stride);
// Variant of the above, but taking BGR input:
// Variant of the above, but taking BGR(A) input:
int WebPPictureImportBGR(WebPPicture* const picture,
const uint8_t* const bgr, int bgr_stride);
int WebPPictureImportBGRA(WebPPicture* const picture,

View File

@ -12,6 +12,10 @@
#ifndef WEBP_WEBP_TYPES_H_
#define WEBP_WEBP_TYPES_H_
// This is for experimentation only! Bitstreams generated will surely
// be invalid, non-decodable ones! USE WITH CARE!
// #define WEBP_EXPERIMENTAL_FEATURES // activate alpha support, yuv444, etc.
#ifndef _MSC_VER
#include <inttypes.h>
#ifdef ANSI