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

@ -139,10 +139,10 @@ static inline void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \
}
// All variants implemented.
UPSCALE_FUNC(UpscaleRgbLinePair, VP8YuvToRgb, 3)
UPSCALE_FUNC(UpscaleBgrLinePair, VP8YuvToBgr, 3)
UPSCALE_FUNC(UpscaleRgbaLinePair, VP8YuvToRgba, 4)
UPSCALE_FUNC(UpscaleBgraLinePair, VP8YuvToBgra, 4)
UPSCALE_FUNC(UpscaleRgbLinePair, VP8YuvToRgb, 3)
UPSCALE_FUNC(UpscaleBgrLinePair, VP8YuvToBgr, 3)
UPSCALE_FUNC(UpscaleRgbaLinePair, VP8YuvToRgb, 4)
UPSCALE_FUNC(UpscaleBgraLinePair, VP8YuvToBgr, 4)
// Main driver function.
static inline
@ -266,15 +266,42 @@ static int CustomPut(const VP8Io* io) {
} else if (p->mode == MODE_BGR) {
VP8YuvToBgr(y, u, v, dst + i * 3);
} else if (p->mode == MODE_RGBA) {
VP8YuvToRgba(y, u, v, dst + i * 4);
VP8YuvToRgb(y, u, v, dst + i * 4);
} else {
VP8YuvToBgra(y, u, v, dst + i * 4);
VP8YuvToBgr(y, u, v, dst + i * 4);
}
}
dst += p->stride;
}
}
}
// Alpha handling
if (p->mode == MODE_RGBA || p->mode == MODE_BGRA) {
int i, j;
uint8_t* dst = p->output + io->mb_y * p->stride + 3;
const uint8_t* alpha = io->a;
const int has_alpha = (alpha != NULL);
#ifdef WEBP_EXPERIMENTAL_FEATURES
if (has_alpha) {
for (j = 0; j < mb_h; ++j) {
for (i = 0; i < w; ++i) {
dst[4 * i] = alpha[i];
}
alpha += io->width;
dst += p->stride;
}
}
#endif
if (!has_alpha) { // fill-in with 0xFFs
for (j = 0; j < mb_h; ++j) {
for (i = 0; i < w; ++i) {
dst[4 * i] = 0xff;
}
dst += p->stride;
}
}
}
return 1;
}