The lossy VP8 bitstream has no alpha channel, so an image with transparency stores its
alpha in an ALPH chunk just before the VP8 chunk. It should not
appear beside a VP8L bitstream, which carries its own alpha.
Layout
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | bits | flags | See below. |
| 1 | chunk size − 1 | bytes | alpha data | Depends on the compression method. |
| Spec bits | Mask | Name | Values |
|---|---|---|---|
| 0–1 | C0 | Rsv | Must be 0. The specification says readers ignore them; libwebp refuses an ALPH chunk where they are not 0. |
| 2–3 | 30 | P, pre-processing | 0 none; 1 level reduction. Information only: a decoder may use it for dithering or smoothing but need not. libwebp refuses the values 2 and 3. |
| 4–5 | 0C | F, filtering | 0 none; 1 horizontal; 2 vertical; 3 gradient. |
| 6–7 | 03 | C, compression | 0 raw; 1 WebP lossless. |
Compression methods
| Value | Name | Data |
|---|---|---|
| 0 | Raw | width × height bytes in scan order. |
| 1 | WebP lossless | A VP8L image stream with no header and implicit dimensions. Alpha is read from the green channel of the decoded ARGB values. |
Decoding the plane
- The plane is as wide and tall as the picture it belongs to: the VP8 frame, which must match the canvas of the VP8X chunk (or the ANMF frame). The chunk does not repeat the size.
- Get one byte for each pixel: the raw bytes (the chunk must hold exactly width × height of them), or the green channel of the decoded lossless image. The lossless stream is the VP8L image-stream without its signature, size and version: it begins with the transform flags, and its size is taken from the picture. Its other channels are ignored.
- Undo the filter, row by row (below).
- The result is the alpha of each pixel. The pre-processing bits change nothing about decoding: a reader may smooth the quantised levels the encoder left, and need not.
A plane that cannot be decoded (too short, a bad lossless stream, a compression method that is not 0 or 1) makes the whole image fail in libwebp. The viewer shows the colours with no alpha and reports the problem.
Raw alpha
With compression 0 the bytes after the header are the alpha values in scan order, filtered as the filtering field says. Bytes past width × height are ignored.
Lossless alpha
With compression 1 the bytes after the header are a VP8L image-stream of the picture’s size. Typical streams use the colour indexing transform (an alpha plane often has few distinct values) with a colour cache and prefix codes for the green channel only, since red, blue and alpha are constant.
Filters
A is the pixel to the left, B the pixel above and C the pixel above and to the left. Each decoded alpha is the predictor plus the stored value, modulo 256.
| Value | Name | Predictor |
|---|---|---|
| 0 | None | 0 |
| 1 | Horizontal | A |
| 2 | Vertical | B |
| 3 | Gradient | clip(A + B − C), where clip limits the value to 0 to 255 |
| Position | Filter | Predictor used |
|---|---|---|
| Top-left pixel | Any | 0 |
| Left column | Horizontal, gradient | The pixel above |
| Top row | Vertical, gradient | The pixel to the left |