mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-13 14:34:33 +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:
98
src/enc/alpha.c
Normal file
98
src/enc/alpha.c
Normal file
@ -0,0 +1,98 @@
|
||||
// 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 compression.
|
||||
//
|
||||
// Author: Skal (pascal.massimino@gmail.com)
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "vp8enci.h"
|
||||
|
||||
#include "zlib.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static int CompressAlpha(const uint8_t* data, size_t data_size,
|
||||
uint8_t** output, size_t* output_size,
|
||||
int algo) {
|
||||
int ret = Z_OK;
|
||||
z_stream strm;
|
||||
const int CHUNK_SIZE = 8192;
|
||||
*output = NULL;
|
||||
*output_size = 0;
|
||||
memset(&strm, 0, sizeof(strm));
|
||||
if (deflateInit(&strm, algo ? Z_BEST_SPEED : Z_BEST_COMPRESSION) != Z_OK) {
|
||||
return 0;
|
||||
}
|
||||
strm.next_in = (unsigned char*)data;
|
||||
strm.avail_in = data_size;
|
||||
do {
|
||||
size_t size_out;
|
||||
unsigned char chunk[CHUNK_SIZE];
|
||||
strm.next_out = chunk;
|
||||
strm.avail_out = CHUNK_SIZE;
|
||||
ret = deflate(&strm, Z_FINISH);
|
||||
if (ret == Z_STREAM_ERROR) {
|
||||
break;
|
||||
}
|
||||
size_out = CHUNK_SIZE - strm.avail_out;
|
||||
if (size_out) {
|
||||
size_t new_size = *output_size + size_out;
|
||||
uint8_t* new_output = realloc(*output, new_size);
|
||||
if (new_output == NULL) {
|
||||
ret = Z_MEM_ERROR;
|
||||
break;
|
||||
}
|
||||
memcpy(new_output + *output_size, chunk, size_out);
|
||||
*output_size = new_size;
|
||||
*output = new_output;
|
||||
}
|
||||
} while (ret != Z_STREAM_END || strm.avail_out == 0);
|
||||
|
||||
deflateEnd(&strm);
|
||||
if (ret != Z_STREAM_END) {
|
||||
free(*output);
|
||||
output_size = 0;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif /* WEBP_EXPERIMENTAL_FEATURES */
|
||||
|
||||
int VP8EncProcessAlpha(VP8Encoder* enc) {
|
||||
const WebPPicture* pic_ = enc->pic_;
|
||||
enc->alpha_data_ = NULL;
|
||||
enc->alpha_data_size_ = 0;
|
||||
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
||||
if (pic_->a == NULL) {
|
||||
return 1;
|
||||
}
|
||||
if (!CompressAlpha(pic_->a, pic_->width * pic_->height,
|
||||
&enc->alpha_data_, &enc->alpha_data_size_,
|
||||
enc->config_->alpha_compression)) {
|
||||
return 0;
|
||||
}
|
||||
#endif /* WEBP_EXPERIMENTAL_FEATURES */
|
||||
return 1;
|
||||
}
|
||||
|
||||
void VP8EncDeleteAlpha(VP8Encoder* enc) {
|
||||
free(enc->alpha_data_);
|
||||
enc->alpha_data_ = NULL;
|
||||
enc->alpha_data_size_ = 0;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
Reference in New Issue
Block a user