mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-15 21:39:59 +02:00
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:
@ -305,6 +305,10 @@ int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
|
||||
return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
|
||||
"bad partition length");
|
||||
}
|
||||
|
||||
dec->alpha_data_ = NULL;
|
||||
dec->alpha_data_size_ = 0;
|
||||
|
||||
br = &dec->br_;
|
||||
VP8InitBitReader(br, buf, buf + frm_hdr->partition_length_);
|
||||
buf += frm_hdr->partition_length_;
|
||||
@ -323,6 +327,7 @@ int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
|
||||
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
|
||||
"cannot parse filter header");
|
||||
}
|
||||
|
||||
status = ParsePartitions(dec, buf, buf_size);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return VP8SetError(dec, status, "cannot parse partitions");
|
||||
@ -368,6 +373,32 @@ int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
|
||||
|
||||
VP8ParseProba(br, dec);
|
||||
|
||||
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
||||
// Extensions
|
||||
if (dec->pic_hdr_.colorspace_) {
|
||||
const uint32_t EXT_SIZE = 4;
|
||||
uint32_t ext_size;
|
||||
uint8_t ext_bits;
|
||||
const uint8_t* ext_bytes_end = buf - EXT_SIZE;
|
||||
if (frm_hdr->partition_length_ <= EXT_SIZE) {
|
||||
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
|
||||
"RIFF: Inconsistent extra information.");
|
||||
}
|
||||
ext_size = (ext_bytes_end[0] << 16) | (ext_bytes_end[1] << 8)
|
||||
| (ext_bytes_end[2]);
|
||||
ext_bits = ext_bytes_end[3];
|
||||
ext_bytes_end -= ext_size;
|
||||
if (!(ext_bits & 0x01) || (ext_size + EXT_SIZE > frm_hdr->partition_length_)) {
|
||||
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
|
||||
"RIFF: Inconsistent extra information.");
|
||||
}
|
||||
if (!!(ext_bits & 0x02)) { // has alpha data
|
||||
dec->alpha_data_size_ = ext_size;
|
||||
dec->alpha_data_ = ext_bytes_end;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// sanitized state
|
||||
dec->ready_ = 1;
|
||||
return 1;
|
||||
|
Reference in New Issue
Block a user