mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-15 05:19:48 +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:
@ -3,7 +3,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/src
|
||||
libwebpencode_la_SOURCES = analysis.c bit_writer.c bit_writer.h \
|
||||
config.c cost.c cost.h dsp.c dsp_sse2.c filter.c \
|
||||
frame.c iterator.c picture.c quant.c \
|
||||
syntax.c tree.c vp8enci.h webpenc.c
|
||||
syntax.c tree.c vp8enci.h webpenc.c alpha.c
|
||||
libwebpencode_la_LDFLAGS = -version-info 0:0:0 -lm
|
||||
libwebpencodeinclude_HEADERS = ../webp/encode.h ../webp/types.h
|
||||
libwebpencodeincludedir = $(includedir)/webp
|
||||
|
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
|
@ -168,6 +168,16 @@ uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {
|
||||
return bw->buf_;
|
||||
}
|
||||
|
||||
int VP8BitWriterAppend(VP8BitWriter* const bw,
|
||||
const uint8_t* data, size_t size) {
|
||||
assert(data);
|
||||
if (bw->nb_bits_ != -8) return 0; // kFlush() must have been called
|
||||
if (!BitWriterResize(bw, size)) return 0;
|
||||
memcpy(bw->buf_ + bw->pos_, data, size);
|
||||
bw->pos_ += size;
|
||||
return 1;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
|
@ -39,6 +39,8 @@ int VP8PutBit(VP8BitWriter* const bw, int bit, int prob);
|
||||
int VP8PutBitUniform(VP8BitWriter* const bw, int bit);
|
||||
void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits);
|
||||
void VP8PutSignedValue(VP8BitWriter* const bw, int value, int nb_bits);
|
||||
int VP8BitWriterAppend(VP8BitWriter* const bw,
|
||||
const uint8_t* data, size_t size);
|
||||
|
||||
// return approximate write position (in bits)
|
||||
static inline uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) {
|
||||
|
@ -41,6 +41,7 @@ int WebPConfigInitInternal(WebPConfig* const config,
|
||||
config->show_compressed = 0;
|
||||
config->preprocessing = 0;
|
||||
config->autofilter = 0;
|
||||
config->alpha_compression = 0;
|
||||
|
||||
// TODO(skal): tune.
|
||||
switch (preset) {
|
||||
@ -105,6 +106,8 @@ int WebPValidateConfig(const WebPConfig* const config) {
|
||||
return 0;
|
||||
if (config->partitions < 0 || config->partitions > 3)
|
||||
return 0;
|
||||
if (config->alpha_compression < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -46,10 +46,30 @@ int WebPPictureAlloc(WebPPicture* const picture) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int WebPPictureAddAlphaPlane(WebPPicture* const picture) {
|
||||
if (picture) {
|
||||
const int width = picture->width;
|
||||
const int height = picture->height;
|
||||
const uint64_t a_size = (uint64_t)width * height;
|
||||
// Security and validation checks
|
||||
if (width <= 0 || height <= 0 || // check param error
|
||||
a_size >= (1ULL << 40) || // check for reasonable global size
|
||||
(size_t)a_size != a_size) { // check for overflow on 32bit
|
||||
return 0;
|
||||
}
|
||||
free(picture->a); // erase previous buffer
|
||||
picture->a = (uint8_t*)malloc((size_t)a_size);
|
||||
return (picture->a != NULL);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void WebPPictureFree(WebPPicture* const picture) {
|
||||
if (picture) {
|
||||
free(picture->y);
|
||||
picture->y = picture->u = picture->v = NULL;
|
||||
free(picture->a);
|
||||
picture->a = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -198,7 +218,7 @@ static inline int rgb_to_v(int r, int g, int b) {
|
||||
|
||||
static int Import(WebPPicture* const picture,
|
||||
const uint8_t* const rgb, int rgb_stride,
|
||||
int step, int swap) {
|
||||
int step, int swap, int alpha_offset) {
|
||||
int x, y;
|
||||
const uint8_t* const r_ptr = rgb + (swap ? 2 : 0);
|
||||
const uint8_t* const g_ptr = rgb + 1;
|
||||
@ -227,6 +247,17 @@ static int Import(WebPPicture* const picture,
|
||||
RGB_TO_UV(x, y, SUM1);
|
||||
}
|
||||
}
|
||||
if (alpha_offset >= 0) {
|
||||
if (!WebPPictureAddAlphaPlane(picture)) {
|
||||
return 0;
|
||||
}
|
||||
for (y = 0; y < picture->height; ++y) {
|
||||
for (x = 0; x < picture->width; ++x) {
|
||||
picture->a[x + y * picture->width] =
|
||||
rgb[step * x + y * rgb_stride + alpha_offset];
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#undef SUM4
|
||||
@ -238,25 +269,25 @@ static int Import(WebPPicture* const picture,
|
||||
int WebPPictureImportRGB(WebPPicture* const picture,
|
||||
const uint8_t* const rgb, int rgb_stride) {
|
||||
if (!WebPPictureAlloc(picture)) return 0;
|
||||
return Import(picture, rgb, rgb_stride, 3, 0);
|
||||
return Import(picture, rgb, rgb_stride, 3, 0, -1);
|
||||
}
|
||||
|
||||
int WebPPictureImportBGR(WebPPicture* const picture,
|
||||
const uint8_t* const rgb, int rgb_stride) {
|
||||
if (!WebPPictureAlloc(picture)) return 0;
|
||||
return Import(picture, rgb, rgb_stride, 3, 1);
|
||||
return Import(picture, rgb, rgb_stride, 3, 1, -1);
|
||||
}
|
||||
|
||||
int WebPPictureImportRGBA(WebPPicture* const picture,
|
||||
const uint8_t* const rgba, int rgba_stride) {
|
||||
if (!WebPPictureAlloc(picture)) return 0;
|
||||
return Import(picture, rgba, rgba_stride, 4, 0);
|
||||
return Import(picture, rgba, rgba_stride, 4, 0, 3);
|
||||
}
|
||||
|
||||
int WebPPictureImportBGRA(WebPPicture* const picture,
|
||||
const uint8_t* const rgba, int rgba_stride) {
|
||||
if (!WebPPictureAlloc(picture)) return 0;
|
||||
return Import(picture, rgba, rgba_stride, 4, 1);
|
||||
return Import(picture, rgba, rgba_stride, 4, 1, 3);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
@ -264,7 +295,7 @@ int WebPPictureImportBGRA(WebPPicture* const picture,
|
||||
|
||||
typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int);
|
||||
|
||||
static size_t Encode(const uint8_t* rgb, int width, int height, int stride,
|
||||
static size_t Encode(const uint8_t* rgba, int width, int height, int stride,
|
||||
Importer import, float quality_factor, uint8_t** output) {
|
||||
size_t output_size = 0;
|
||||
WebPPicture pic;
|
||||
@ -286,7 +317,7 @@ static size_t Encode(const uint8_t* rgb, int width, int height, int stride,
|
||||
wrt.size = &output_size;
|
||||
InitMemoryWriter(&wrt);
|
||||
|
||||
ok = import(&pic, rgb, stride) && WebPEncode(&config, &pic);
|
||||
ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic);
|
||||
WebPPictureFree(&pic);
|
||||
if (!ok) {
|
||||
free(*output);
|
||||
|
@ -155,14 +155,46 @@ static int EmitPartitionsSize(const VP8Encoder* const enc,
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static int WriteExtensions(VP8Encoder* const enc) {
|
||||
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
||||
const int EXT_SIZE = 4;
|
||||
VP8BitWriter* const bw = &enc->bw_;
|
||||
uint8_t trailer[EXT_SIZE];
|
||||
uint32_t ext_size = 0;
|
||||
uint8_t ext_bits = 0x01;
|
||||
if (enc->alpha_data_size_) {
|
||||
if (!VP8BitWriterAppend(bw, enc->alpha_data_, enc->alpha_data_size_)) {
|
||||
return 0;
|
||||
}
|
||||
ext_size += enc->alpha_data_size_;
|
||||
ext_bits |= 0x02;
|
||||
}
|
||||
trailer[0] = (ext_size >> 16) & 0xff;
|
||||
trailer[1] = (ext_size >> 8) & 0xff;
|
||||
trailer[2] = (ext_size >> 0) & 0xff;
|
||||
trailer[EXT_SIZE - 1] = ext_bits;
|
||||
if (!VP8BitWriterAppend(bw, trailer, EXT_SIZE)) {
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static size_t GeneratePartition0(VP8Encoder* const enc) {
|
||||
VP8BitWriter* const bw = &enc->bw_;
|
||||
const int mb_size = enc->mb_w_ * enc->mb_h_;
|
||||
uint64_t pos1, pos2, pos3;
|
||||
const int need_extensions = (enc->alpha_data_size_ > 0);
|
||||
|
||||
pos1 = VP8BitWriterPos(bw);
|
||||
VP8BitWriterInit(bw, mb_size * 7 / 8); // ~7 bits per macroblock
|
||||
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
||||
VP8PutBitUniform(bw, need_extensions); // extensions
|
||||
#else
|
||||
VP8PutBitUniform(bw, 0); // colorspace
|
||||
#endif
|
||||
VP8PutBitUniform(bw, 0); // clamp type
|
||||
|
||||
PutSegmentHeader(bw, enc);
|
||||
@ -174,11 +206,17 @@ static size_t GeneratePartition0(VP8Encoder* const enc) {
|
||||
pos2 = VP8BitWriterPos(bw);
|
||||
VP8CodeIntraModes(enc);
|
||||
VP8BitWriterFinish(bw);
|
||||
|
||||
if (need_extensions && !WriteExtensions(enc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
pos3 = VP8BitWriterPos(bw);
|
||||
|
||||
if (enc->pic_->stats) {
|
||||
enc->pic_->stats->header_bytes[0] = (int)((pos2 - pos1 + 7) >> 3);
|
||||
enc->pic_->stats->header_bytes[1] = (int)((pos3 - pos2 + 7) >> 3);
|
||||
enc->pic_->stats->alpha_data_size = enc->alpha_data_size_;
|
||||
}
|
||||
return !bw->error_;
|
||||
}
|
||||
|
@ -326,6 +326,10 @@ struct VP8Encoder {
|
||||
VP8BitWriter bw_; // part0
|
||||
VP8BitWriter parts_[MAX_NUM_PARTITIONS]; // token partitions
|
||||
|
||||
// transparency blob
|
||||
uint8_t* alpha_data_;
|
||||
size_t alpha_data_size_;
|
||||
|
||||
// quantization info (one set of DC/AC dequant factor per segment)
|
||||
VP8SegmentInfo dqm_[NUM_MB_SEGMENTS];
|
||||
int base_quant_; // nominal quantizer value. Only used
|
||||
@ -414,6 +418,11 @@ void VP8SetSegmentParams(VP8Encoder* const enc, float quality);
|
||||
// Pick best modes and fills the levels. Returns true if skipped.
|
||||
int VP8Decimate(VP8EncIterator* const it, VP8ModeScore* const rd, int rd_opt);
|
||||
|
||||
// in alpha.c
|
||||
// Compress transparency information into enc->alpha_data_. Return true if ok.
|
||||
int VP8EncProcessAlpha(VP8Encoder* enc);
|
||||
void VP8EncDeleteAlpha(VP8Encoder* enc); // delete compressed data
|
||||
|
||||
// in dsp.c
|
||||
// Transforms
|
||||
// VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
|
||||
|
@ -246,7 +246,12 @@ static VP8Encoder* InitEncoder(const WebPConfig* const config,
|
||||
}
|
||||
|
||||
static void DeleteEncoder(VP8Encoder* enc) {
|
||||
free(enc);
|
||||
if (enc) {
|
||||
if (enc->alpha_data_) {
|
||||
VP8EncDeleteAlpha(enc);
|
||||
}
|
||||
free(enc);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
@ -306,6 +311,7 @@ int WebPEncode(const WebPConfig* const config, WebPPicture* const pic) {
|
||||
ok = VP8EncAnalyze(enc)
|
||||
&& VP8StatLoop(enc)
|
||||
&& VP8EncLoop(enc)
|
||||
&& VP8EncProcessAlpha(enc)
|
||||
&& VP8EncWrite(enc);
|
||||
StoreStats(enc);
|
||||
DeleteEncoder(enc);
|
||||
|
Reference in New Issue
Block a user