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
|
|
|
// Copyright 2011 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/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Alpha-plane decompression.
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "vp8i.h"
|
|
|
|
|
|
|
|
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
|
|
|
|
|
|
|
#include "zlib.h"
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-08-25 23:22:32 +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
2011-04-26 01:58:04 +02:00
|
|
|
|
|
|
|
const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
|
|
|
|
int row, int num_rows) {
|
|
|
|
uint8_t* output = dec->alpha_plane_;
|
|
|
|
const int stride = dec->pic_hdr_.width_;
|
|
|
|
if (row < 0 || row + num_rows > dec->pic_hdr_.height_) {
|
|
|
|
return NULL; // sanity check
|
|
|
|
}
|
|
|
|
if (row == 0) {
|
|
|
|
// TODO(skal): for now, we just decompress everything during the first call.
|
|
|
|
// Later, we'll decode progressively, but we need to store the
|
|
|
|
// z_stream state.
|
|
|
|
const uint8_t* data = dec->alpha_data_;
|
|
|
|
size_t data_size = dec->alpha_data_size_;
|
|
|
|
const size_t output_size = stride * dec->pic_hdr_.height_;
|
|
|
|
int ret = Z_OK;
|
|
|
|
z_stream strm;
|
|
|
|
|
|
|
|
memset(&strm, 0, sizeof(strm));
|
|
|
|
if (inflateInit(&strm) != Z_OK) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strm.avail_in = data_size;
|
|
|
|
strm.next_in = (unsigned char*)data;
|
|
|
|
do {
|
|
|
|
strm.avail_out = output_size;
|
|
|
|
strm.next_out = output;
|
|
|
|
ret = inflate(&strm, Z_NO_FLUSH);
|
|
|
|
if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (strm.avail_out == 0);
|
|
|
|
|
|
|
|
inflateEnd(&strm);
|
|
|
|
if (ret != Z_STREAM_END) {
|
|
|
|
return NULL; // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output + row * stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // WEBP_EXPERIMENTAL_FEATURES
|