1<!-- 2 3Although you may be viewing an alternate representation, this document is 4sourced in Markdown, a light-duty markup scheme, and is optimized for the 5[kramdown](https://kramdown.gettalong.org/) transformer. 6 7See the accompanying specs_generation.md. External link targets are referenced 8at the end of this file. 9 10--> 11 12Specification for WebP Lossless Bitstream 13========================================= 14 15_Jyrki Alakuijala, Ph.D., Google, Inc., 2023-03-09_ 16 17Abstract 18-------- 19 20WebP lossless is an image format for lossless compression of ARGB images. The 21lossless format stores and restores the pixel values exactly, including the 22color values for fully transparent pixels. A universal algorithm for sequential 23data compression (LZ77), prefix coding, and a color cache are used for 24compression of the bulk data. Decoding speeds faster than PNG have been 25demonstrated, as well as 25% denser compression than can be achieved using 26today's PNG format. 27 28* TOC placeholder 29{:toc} 30 311 Introduction 32-------------- 33 34This document describes the compressed data representation of a WebP lossless 35image. It is intended as a detailed reference for the WebP lossless encoder and 36decoder implementation. 37 38In this document, we extensively use C programming language syntax to describe 39the bitstream and assume the existence of a function for reading bits, 40`ReadBits(n)`. The bytes are read in the natural order of the stream containing 41them, and bits of each byte are read in least-significant-bit-first order. When 42multiple bits are read at the same time, the integer is constructed from the 43original data in the original order. The most significant bits of the returned 44integer are also the most significant bits of the original data. Thus, the 45statement 46 47~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 48b = ReadBits(2); 49~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 50 51is equivalent with the two statements below: 52 53~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 54b = ReadBits(1); 55b |= ReadBits(1) << 1; 56~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 57 58We assume that each color component, that is, alpha, red, blue, and green, is 59represented using an 8-bit byte. We define the corresponding type as uint8. A 60whole ARGB pixel is represented by a type called uint32, which is an unsigned 61integer consisting of 32 bits. In the code showing the behavior of the 62transforms, these values are codified in the following bits: alpha in bits 6331..24, red in bits 23..16, green in bits 15..8, and blue in bits 7..0; however, 64implementations of the format are free to use another representation internally. 65 66Broadly, a WebP lossless image contains header data, transform information, and 67actual image data. Headers contain the width and height of the image. A WebP 68lossless image can go through four different types of transforms before being 69entropy encoded. The transform information in the bitstream contains the data 70required to apply the respective inverse transforms. 71 722 Nomenclature 73-------------- 74 75ARGB 76: A pixel value consisting of alpha, red, green, and blue values. 77 78ARGB image 79: A two-dimensional array containing ARGB pixels. 80 81color cache 82: A small hash-addressed array to store recently used colors to be able to 83 recall them with shorter codes. 84 85color indexing image 86: A one-dimensional image of colors that can be indexed using a small integer 87 (up to 256 within WebP lossless). 88 89color transform image 90: A two-dimensional subresolution image containing data about correlations of 91 color components. 92 93distance mapping 94: Changes LZ77 distances to have the smallest values for pixels in 95 two-dimensional proximity. 96 97entropy image 98: A two-dimensional subresolution image indicating which entropy coding should 99 be used in a respective square in the image, that is, each pixel is a meta 100 prefix code. 101 102LZ77 103: A dictionary-based sliding window compression algorithm that either emits 104 symbols or describes them as sequences of past symbols. 105 106meta prefix code 107: A small integer (up to 16 bits) that indexes an element in the meta prefix 108 table. 109 110predictor image 111: A two-dimensional subresolution image indicating which spatial predictor is 112 used for a particular square in the image. 113 114prefix code 115: A classic way to do entropy coding where a smaller number of bits are used 116 for more frequent codes. 117 118prefix coding 119: A way to entropy code larger integers, which codes a few bits of the integer 120 using an entropy code and codifies the remaining bits raw. This allows for 121 the descriptions of the entropy codes to remain relatively small even when 122 the range of symbols is large. 123 124scan-line order 125: A processing order of pixels (left to right and top to bottom), starting 126 from the left-hand-top pixel. Once a row is completed, continue from the 127 left-hand column of the next row. 128 1293 RIFF Header 130------------- 131 132The beginning of the header has the RIFF container. This consists of the 133following 21 bytes: 134 135 1. String 'RIFF'. 136 2. A little-endian, 32-bit value of the chunk length, which is the whole size 137 of the chunk controlled by the RIFF header. Normally, this equals 138 the payload size (file size minus 8 bytes: 4 bytes for the 'RIFF' 139 identifier and 4 bytes for storing the value itself). 140 3. String 'WEBP' (RIFF container name). 141 4. String 'VP8L' (FourCC for lossless-encoded image data). 142 5. A little-endian, 32-bit value of the number of bytes in the 143 lossless stream. 144 6. 1-byte signature 0x2f. 145 146The first 28 bits of the bitstream specify the width and height of the image. 147Width and height are decoded as 14-bit integers as follows: 148 149~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 150int image_width = ReadBits(14) + 1; 151int image_height = ReadBits(14) + 1; 152~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 153 154The 14-bit precision for image width and height limits the maximum size of a 155WebP lossless image to 16384✕16384 pixels. 156 157The alpha_is_used bit is a hint only, and should not impact decoding. It should 158be set to 0 when all alpha values are 255 in the picture, and 1 otherwise. 159 160~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 161int alpha_is_used = ReadBits(1); 162~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 163 164The version_number is a 3 bit code that must be set to 0. Any other value should 165be treated as an error. 166 167~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 168int version_number = ReadBits(3); 169~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 170 1714 Transforms 172------------ 173 174The transforms are reversible manipulations of the image data that can reduce 175the remaining symbolic entropy by modeling spatial and color correlations. They 176can make the final compression more dense. 177 178An image can go through four types of transforms. A 1 bit indicates the 179presence of a transform. Each transform is allowed to be used only once. The 180transforms are used only for the main-level ARGB image; the subresolution images 181(color transform image, entropy image, and predictor image) have no transforms, 182not even the 0 bit indicating the end of transforms. 183 184Typically, an encoder would use these transforms to reduce the Shannon entropy 185in the residual image. Also, the transform data can be decided based on entropy 186minimization. 187 188~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 189while (ReadBits(1)) { // Transform present. 190 // Decode transform type. 191 enum TransformType transform_type = ReadBits(2); 192 // Decode transform data. 193 ... 194} 195 196// Decode actual image data (Section 5). 197~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 198 199If a transform is present, then the next two bits specify the transform type. 200There are four types of transforms. 201 202~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 203enum TransformType { 204 PREDICTOR_TRANSFORM = 0, 205 COLOR_TRANSFORM = 1, 206 SUBTRACT_GREEN_TRANSFORM = 2, 207 COLOR_INDEXING_TRANSFORM = 3, 208}; 209~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 210 211The transform type is followed by the transform data. Transform data contains 212the information required to apply the inverse transform and depends on the 213transform type. The inverse transforms are applied in the reverse order that 214they are read from the bitstream, that is, last one first. 215 216Next, we describe the transform data for different types. 217 218### 4.1 Predictor Transform 219 220The predictor transform can be used to reduce entropy by exploiting the fact 221that neighboring pixels are often correlated. In the predictor transform, the 222current pixel value is predicted from the pixels already decoded (in scan-line 223order) and only the residual value (actual - predicted) is encoded. The green 224component of a pixel defines which of the 14 predictors is used within a 225particular block of the ARGB image. The _prediction mode_ determines the type of 226prediction to use. We divide the image into squares, and all the pixels in a 227square use the same prediction mode. 228 229The first 3 bits of prediction data define the block width and height in number 230of bits. 231 232~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 233int size_bits = ReadBits(3) + 2; 234int block_width = (1 << size_bits); 235int block_height = (1 << size_bits); 236#define DIV_ROUND_UP(num, den) (((num) + (den) - 1) / (den)) 237int transform_width = DIV_ROUND_UP(image_width, 1 << size_bits); 238~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 239 240The transform data contains the prediction mode for each block of the image. It 241is a subresolution image where the green component of a pixel defines which of 242the 14 predictors is used for all the `block_width * block_height` pixels within 243a particular block of the ARGB image. This subresolution image is encoded using 244the same techniques described in [Chapter 5](#image-data). 245 246The number of block columns, `transform_width`, is used in two-dimensional 247indexing. For a pixel (x, y), one can compute the respective filter block 248address by: 249 250~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 251int block_index = (y >> size_bits) * transform_width + 252 (x >> size_bits); 253~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 254 255There are 14 different prediction modes. In each prediction mode, the current 256pixel value is predicted from one or more neighboring pixels whose values are 257already known. 258 259We chose the neighboring pixels (TL, T, TR, and L) of the current pixel (P) as 260follows: 261 262~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 263O O O O O O O O O O O 264O O O O O O O O O O O 265O O O O TL T TR O O O O 266O O O O L P X X X X X 267X X X X X X X X X X X 268X X X X X X X X X X X 269~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 270 271where TL means top-left, T means top, TR means top-right, and L means left. At 272the time of predicting a value for P, all O, TL, T, TR and L pixels have already 273been processed, and the P pixel and all X pixels are unknown. 274 275Given the preceding neighboring pixels, the different prediction modes are 276defined as follows. 277 278| Mode | Predicted value of each channel of the current pixel | 279| ------ | ------------------------------------------------------- | 280| 0 | 0xff000000 (represents solid black color in ARGB) | 281| 1 | L | 282| 2 | T | 283| 3 | TR | 284| 4 | TL | 285| 5 | Average2(Average2(L, TR), T) | 286| 6 | Average2(L, TL) | 287| 7 | Average2(L, T) | 288| 8 | Average2(TL, T) | 289| 9 | Average2(T, TR) | 290| 10 | Average2(Average2(L, TL), Average2(T, TR)) | 291| 11 | Select(L, T, TL) | 292| 12 | ClampAddSubtractFull(L, T, TL) | 293| 13 | ClampAddSubtractHalf(Average2(L, T), TL) | 294 295 296`Average2` is defined as follows for each ARGB component: 297 298~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 299uint8 Average2(uint8 a, uint8 b) { 300 return (a + b) / 2; 301} 302~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 303 304The Select predictor is defined as follows: 305 306~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 307uint32 Select(uint32 L, uint32 T, uint32 TL) { 308 // L = left pixel, T = top pixel, TL = top-left pixel. 309 310 // ARGB component estimates for prediction. 311 int pAlpha = ALPHA(L) + ALPHA(T) - ALPHA(TL); 312 int pRed = RED(L) + RED(T) - RED(TL); 313 int pGreen = GREEN(L) + GREEN(T) - GREEN(TL); 314 int pBlue = BLUE(L) + BLUE(T) - BLUE(TL); 315 316 // Manhattan distances to estimates for left and top pixels. 317 int pL = abs(pAlpha - ALPHA(L)) + abs(pRed - RED(L)) + 318 abs(pGreen - GREEN(L)) + abs(pBlue - BLUE(L)); 319 int pT = abs(pAlpha - ALPHA(T)) + abs(pRed - RED(T)) + 320 abs(pGreen - GREEN(T)) + abs(pBlue - BLUE(T)); 321 322 // Return either left or top, the one closer to the prediction. 323 if (pL < pT) { 324 return L; 325 } else { 326 return T; 327 } 328} 329~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 330 331The functions `ClampAddSubtractFull` and `ClampAddSubtractHalf` are performed 332for each ARGB component as follows: 333 334~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 335// Clamp the input value between 0 and 255. 336int Clamp(int a) { 337 return (a < 0) ? 0 : (a > 255) ? 255 : a; 338} 339~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 340 341~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 342int ClampAddSubtractFull(int a, int b, int c) { 343 return Clamp(a + b - c); 344} 345~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 346 347~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 348int ClampAddSubtractHalf(int a, int b) { 349 return Clamp(a + (a - b) / 2); 350} 351~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 352 353There are special handling rules for some border pixels. If there is a 354prediction transform, regardless of the mode \[0..13\] for these pixels, the 355predicted value for the left-topmost pixel of the image is 0xff000000, all 356pixels on the top row are L-pixel, and all pixels on the leftmost column are 357T-pixel. 358 359Addressing the TR-pixel for pixels on the rightmost column is 360exceptional. The pixels on the rightmost column are predicted by using the modes 361\[0..13\], just like pixels not on the border, but the leftmost pixel on the 362same row as the current pixel is instead used as the TR-pixel. 363 364The final pixel value is obtained by adding each channel of the predicted value 365to the encoded residual value. 366 367~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 368void PredictorTransformOutput(uint32 residual, uint32 pred, 369 uint8* alpha, uint8* red, 370 uint8* green, uint8* blue) { 371 *alpha = ALPHA(residual) + ALPHA(pred); 372 *red = RED(residual) + RED(pred); 373 *green = GREEN(residual) + GREEN(pred); 374 *blue = BLUE(residual) + BLUE(pred); 375} 376~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 377 378### 4.2 Color Transform 379 380The goal of the color transform is to decorrelate the R, G, and B values of each 381pixel. The color transform keeps the green (G) value as it is, transforms the 382red (R) value based on the green value, and transforms the blue (B) value based 383on the green value and then on the red value. 384 385As is the case for the predictor transform, first the image is divided into 386blocks, and the same transform mode is used for all the pixels in a block. For 387each block, there are three types of color transform elements. 388 389~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 390typedef struct { 391 uint8 green_to_red; 392 uint8 green_to_blue; 393 uint8 red_to_blue; 394} ColorTransformElement; 395~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 396 397The actual color transform is done by defining a color transform delta. The 398color transform delta depends on the `ColorTransformElement`, which is the same 399for all the pixels in a particular block. The delta is subtracted during the 400color transform. The inverse color transform then is just adding those deltas. 401 402The color transform function is defined as follows: 403 404~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 405void ColorTransform(uint8 red, uint8 blue, uint8 green, 406 ColorTransformElement *trans, 407 uint8 *new_red, uint8 *new_blue) { 408 // Transformed values of red and blue components 409 int tmp_red = red; 410 int tmp_blue = blue; 411 412 // Applying the transform is just subtracting the transform deltas 413 tmp_red -= ColorTransformDelta(trans->green_to_red, green); 414 tmp_blue -= ColorTransformDelta(trans->green_to_blue, green); 415 tmp_blue -= ColorTransformDelta(trans->red_to_blue, red); 416 417 *new_red = tmp_red & 0xff; 418 *new_blue = tmp_blue & 0xff; 419} 420~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 421 422`ColorTransformDelta` is computed using a signed 8-bit integer representing a 4233.5-fixed-point number and a signed 8-bit RGB color channel (c) \[-128..127\] 424and is defined as follows: 425 426~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 427int8 ColorTransformDelta(int8 t, int8 c) { 428 return (t * c) >> 5; 429} 430~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 431 432A conversion from the 8-bit unsigned representation (uint8) to the 8-bit signed 433one (int8) is required before calling `ColorTransformDelta()`. The signed value 434should be interpreted as an 8-bit two's complement number (that is: uint8 range 435\[128..255\] is mapped to the \[-128..-1\] range of its converted int8 value). 436 437The multiplication is to be done using more precision (with at least 16-bit 438precision). The sign extension property of the shift operation does not matter 439here; only the lowest 8 bits are used from the result, and there the sign 440extension shifting and unsigned shifting are consistent with each other. 441 442Now, we describe the contents of color transform data so that decoding can apply 443the inverse color transform and recover the original red and blue values. The 444first 3 bits of the color transform data contain the width and height of the 445image block in number of bits, just like the predictor transform: 446 447~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 448int size_bits = ReadBits(3) + 2; 449int block_width = 1 << size_bits; 450int block_height = 1 << size_bits; 451~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 452 453The remaining part of the color transform data contains `ColorTransformElement` 454instances, corresponding to each block of the image. Each 455`ColorTransformElement` `'cte'` is treated as a pixel in a subresolution image 456whose alpha component is `255`, red component is `cte.red_to_blue`, green 457component is `cte.green_to_blue`, and blue component is `cte.green_to_red`. 458 459During decoding, `ColorTransformElement` instances of the blocks are decoded and 460the inverse color transform is applied on the ARGB values of the pixels. As 461mentioned earlier, that inverse color transform is just adding 462`ColorTransformElement` values to the red and blue channels. The alpha and green 463channels are left as is. 464 465~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 466void InverseTransform(uint8 red, uint8 green, uint8 blue, 467 ColorTransformElement *trans, 468 uint8 *new_red, uint8 *new_blue) { 469 // Transformed values of red and blue components 470 int tmp_red = red; 471 int tmp_blue = blue; 472 473 // Applying the inverse transform is just adding the 474 // color transform deltas 475 tmp_red += ColorTransformDelta(trans->green_to_red, green); 476 tmp_blue += ColorTransformDelta(trans->green_to_blue, green); 477 tmp_blue += 478 ColorTransformDelta(trans->red_to_blue, tmp_red & 0xff); 479 480 *new_red = tmp_red & 0xff; 481 *new_blue = tmp_blue & 0xff; 482} 483~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 484 485### 4.3 Subtract Green Transform 486 487The subtract green transform subtracts green values from red and blue values of 488each pixel. When this transform is present, the decoder needs to add the green 489value to both the red and blue values. There is no data associated with this 490transform. The decoder applies the inverse transform as follows: 491 492~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 493void AddGreenToBlueAndRed(uint8 green, uint8 *red, uint8 *blue) { 494 *red = (*red + green) & 0xff; 495 *blue = (*blue + green) & 0xff; 496} 497~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 498 499This transform is redundant, as it can be modeled using the color transform, but 500since there is no additional data here, the subtract green transform can be 501coded using fewer bits than a full-blown color transform. 502 503### 4.4 Color Indexing Transform 504 505If there are not many unique pixel values, it may be more efficient to create a 506color index array and replace the pixel values by the array's indices. The color 507indexing transform achieves this. (In the context of WebP lossless, we 508specifically do not call this a palette transform because a similar but more 509dynamic concept exists in WebP lossless encoding: color cache.) 510 511The color indexing transform checks for the number of unique ARGB values in the 512image. If that number is below a threshold (256), it creates an array of those 513ARGB values, which is then used to replace the pixel values with the 514corresponding index: the green channel of the pixels are replaced with the 515index, all alpha values are set to 255, and all red and blue values to 0. 516 517The transform data contains the color table size and the entries in the color 518table. The decoder reads the color indexing transform data as follows: 519 520~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 521// 8-bit value for the color table size 522int color_table_size = ReadBits(8) + 1; 523~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 524 525The color table is stored using the image storage format itself. The color table 526can be obtained by reading an image, without the RIFF header, image size, and 527transforms, assuming the height of 1 pixel and the width of `color_table_size`. 528The color table is always subtraction-coded to reduce image entropy. The deltas 529of palette colors contain typically much less entropy than the colors 530themselves, leading to significant savings for smaller images. In decoding, 531every final color in the color table can be obtained by adding the previous 532color component values by each ARGB component separately and storing the least 533significant 8 bits of the result. 534 535The inverse transform for the image is simply replacing the pixel values (which 536are indices to the color table) with the actual color table values. The indexing 537is done based on the green component of the ARGB color. 538 539~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 540// Inverse transform 541argb = color_table[GREEN(argb)]; 542~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 543 544If the index is equal to or larger than `color_table_size`, the argb color value 545should be set to 0x00000000 (transparent black). 546 547When the color table is small (equal to or less than 16 colors), several pixels 548are bundled into a single pixel. The pixel bundling packs several (2, 4, or 8) 549pixels into a single pixel, reducing the image width respectively. Pixel 550bundling allows for a more efficient joint distribution entropy coding of 551neighboring pixels and gives some arithmetic coding-like benefits to the 552entropy code, but it can only be used when there are 16 or fewer unique values. 553 554`color_table_size` specifies how many pixels are combined: 555 556~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 557int width_bits; 558if (color_table_size <= 2) { 559 width_bits = 3; 560} else if (color_table_size <= 4) { 561 width_bits = 2; 562} else if (color_table_size <= 16) { 563 width_bits = 1; 564} else { 565 width_bits = 0; 566} 567~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 568 569`width_bits` has a value of 0, 1, 2, or 3. A value of 0 indicates no pixel 570bundling is to be done for the image. A value of 1 indicates that two pixels are 571combined, and each pixel has a range of \[0..15\]. A value of 2 indicates that 572four pixels are combined, and each pixel has a range of \[0..3\]. A value of 3 573indicates that eight pixels are combined and each pixel has a range of \[0..1\], 574that is, a binary value. 575 576The values are packed into the green component as follows: 577 578 * `width_bits` = 1: For every x value, where x ≡ 0 (mod 2), a green 579 value at x is positioned into the 4 least significant bits of the 580 green value at x / 2, and a green value at x + 1 is positioned into the 581 4 most significant bits of the green value at x / 2. 582 * `width_bits` = 2: For every x value, where x ≡ 0 (mod 4), a green 583 value at x is positioned into the 2 least-significant bits of the 584 green value at x / 4, and green values at x + 1 to x + 3 are positioned in 585 order to the more significant bits of the green value at x / 4. 586 * `width_bits` = 3: For every x value, where x ≡ 0 (mod 8), a green 587 value at x is positioned into the least significant bit of the green 588 value at x / 8, and green values at x + 1 to x + 7 are positioned in order 589 to the more significant bits of the green value at x / 8. 590 591After reading this transform, `image_width` is subsampled by `width_bits`. This 592affects the size of subsequent transforms. The new size can be calculated using 593`DIV_ROUND_UP`, as defined [earlier](#predictor-transform). 594 595~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 596image_width = DIV_ROUND_UP(image_width, 1 << width_bits); 597~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 598 5995 Image Data 600------------ 601 602Image data is an array of pixel values in scan-line order. 603 604### 5.1 Roles of Image Data 605 606We use image data in five different roles: 607 608 1. ARGB image: Stores the actual pixels of the image. 609 1. Entropy image: Stores the meta prefix codes (see 610 ["Decoding of Meta Prefix Codes"](#decoding-of-meta-prefix-codes)). 611 1. Predictor image: Stores the metadata for the predictor transform (see 612 ["Predictor Transform"](#predictor-transform)). 613 1. Color transform image: Created by `ColorTransformElement` values 614 (defined in ["Color Transform"](#color-transform)) for different blocks of 615 the image. 616 1. Color indexing image: An array of size `color_table_size` (up to 256 ARGB 617 values) storing the metadata for the color indexing transform (see 618 ["Color Indexing Transform"](#color-indexing-transform)). 619 620### 5.2 Encoding of Image Data 621 622The encoding of image data is independent of its role. 623 624The image is first divided into a set of fixed-size blocks (typically 16x16 625blocks). Each of these blocks are modeled using their own entropy codes. Also, 626several blocks may share the same entropy codes. 627 628**Rationale:** Storing an entropy code incurs a cost. This cost can be minimized 629if statistically similar blocks share an entropy code, thereby storing that code 630only once. For example, an encoder can find similar blocks by clustering them 631using their statistical properties or by repeatedly joining a pair of randomly 632selected clusters when it reduces the overall amount of bits needed to encode 633the image. 634 635Each pixel is encoded using one of the three possible methods: 636 637 1. Prefix-coded literals: Each channel (green, red, blue, and alpha) is 638 entropy-coded independently. 639 2. LZ77 backward reference: A sequence of pixels are copied from elsewhere in 640 the image. 641 3. Color cache code: Using a short multiplicative hash code (color cache 642 index) of a recently seen color. 643 644The following subsections describe each of these in detail. 645 646#### 5.2.1 Prefix-Coded Literals 647 648The pixel is stored as prefix-coded values of green, red, blue, and alpha (in 649that order). See [Section 6.2.3](#decoding-entropy-coded-image-data) for 650details. 651 652#### 5.2.2 LZ77 Backward Reference 653 654Backward references are tuples of _length_ and _distance code_: 655 656 * Length indicates how many pixels in scan-line order are to be copied. 657 * Distance code is a number indicating the position of a previously seen 658 pixel, from which the pixels are to be copied. The exact mapping is 659 described [below](#distance-mapping). 660 661The length and distance values are stored using **LZ77 prefix coding**. 662 663LZ77 prefix coding divides large integer values into two parts: the _prefix 664code_ and the _extra bits_. The prefix code is stored using an entropy code, 665while the extra bits are stored as they are (without an entropy code). 666 667**Rationale**: This approach reduces the storage requirement for the entropy 668code. Also, large values are usually rare, so extra bits would be used for very 669few values in the image. Thus, this approach results in better compression 670overall. 671 672The following table denotes the prefix codes and extra bits used for storing 673different ranges of values. 674 675Note: The maximum backward reference length is limited to 4096. Hence, only the 676first 24 prefix codes (with the respective extra bits) are meaningful for length 677values. For distance values, however, all the 40 prefix codes are valid. 678 679| Value range | Prefix code | Extra bits | 680| --------------- | ----------- | ---------- | 681| 1 | 0 | 0 | 682| 2 | 1 | 0 | 683| 3 | 2 | 0 | 684| 4 | 3 | 0 | 685| 5..6 | 4 | 1 | 686| 7..8 | 5 | 1 | 687| 9..12 | 6 | 2 | 688| 13..16 | 7 | 2 | 689| ... | ... | ... | 690| 3072..4096 | 23 | 10 | 691| ... | ... | ... | 692| 524289..786432 | 38 | 18 | 693| 786433..1048576 | 39 | 18 | 694 695The pseudocode to obtain a (length or distance) value from the prefix code is as 696follows: 697 698~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 699if (prefix_code < 4) { 700 return prefix_code + 1; 701} 702int extra_bits = (prefix_code - 2) >> 1; 703int offset = (2 + (prefix_code & 1)) << extra_bits; 704return offset + ReadBits(extra_bits) + 1; 705~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 706 707##### Distance Mapping 708 709As noted previously, a distance code is a number indicating the position of a 710previously seen pixel, from which the pixels are to be copied. This subsection 711defines the mapping between a distance code and the position of a previous 712pixel. 713 714Distance codes larger than 120 denote the pixel distance in scan-line order, 715offset by 120. 716 717The smallest distance codes \[1..120\] are special and are reserved for a close 718neighborhood of the current pixel. This neighborhood consists of 120 pixels: 719 720 * Pixels that are 1 to 7 rows above the current pixel and are up to 8 columns 721 to the left or up to 7 columns to the right of the current pixel. \[Total 722 such pixels = `7 * (8 + 1 + 7) = 112`\]. 723 * Pixels that are in the same row as the current pixel and are up to 8 724 columns to the left of the current pixel. \[`8` such pixels\]. 725 726The mapping between distance code `distance_code` and the neighboring pixel 727offset `(xi, yi)` is as follows: 728 729~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 730(0, 1), (1, 0), (1, 1), (-1, 1), (0, 2), (2, 0), (1, 2), 731(-1, 2), (2, 1), (-2, 1), (2, 2), (-2, 2), (0, 3), (3, 0), 732(1, 3), (-1, 3), (3, 1), (-3, 1), (2, 3), (-2, 3), (3, 2), 733(-3, 2), (0, 4), (4, 0), (1, 4), (-1, 4), (4, 1), (-4, 1), 734(3, 3), (-3, 3), (2, 4), (-2, 4), (4, 2), (-4, 2), (0, 5), 735(3, 4), (-3, 4), (4, 3), (-4, 3), (5, 0), (1, 5), (-1, 5), 736(5, 1), (-5, 1), (2, 5), (-2, 5), (5, 2), (-5, 2), (4, 4), 737(-4, 4), (3, 5), (-3, 5), (5, 3), (-5, 3), (0, 6), (6, 0), 738(1, 6), (-1, 6), (6, 1), (-6, 1), (2, 6), (-2, 6), (6, 2), 739(-6, 2), (4, 5), (-4, 5), (5, 4), (-5, 4), (3, 6), (-3, 6), 740(6, 3), (-6, 3), (0, 7), (7, 0), (1, 7), (-1, 7), (5, 5), 741(-5, 5), (7, 1), (-7, 1), (4, 6), (-4, 6), (6, 4), (-6, 4), 742(2, 7), (-2, 7), (7, 2), (-7, 2), (3, 7), (-3, 7), (7, 3), 743(-7, 3), (5, 6), (-5, 6), (6, 5), (-6, 5), (8, 0), (4, 7), 744(-4, 7), (7, 4), (-7, 4), (8, 1), (8, 2), (6, 6), (-6, 6), 745(8, 3), (5, 7), (-5, 7), (7, 5), (-7, 5), (8, 4), (6, 7), 746(-6, 7), (7, 6), (-7, 6), (8, 5), (7, 7), (-7, 7), (8, 6), 747(8, 7) 748~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 749 750For example, the distance code `1` indicates an offset of `(0, 1)` for the 751neighboring pixel, that is, the pixel above the current pixel (0 pixel 752difference in the X direction and 1 pixel difference in the Y direction). 753Similarly, the distance code `3` indicates the top-left pixel. 754 755The decoder can convert a distance code `distance_code` to a scan-line order 756distance `dist` as follows: 757 758~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 759(xi, yi) = distance_map[distance_code - 1] 760dist = xi + yi * image_width 761if (dist < 1) { 762 dist = 1 763} 764~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 765 766where `distance_map` is the mapping noted above, and `image_width` is the width 767of the image in pixels. 768 769#### 5.2.3 Color Cache Coding 770{:#color-cache-code} 771 772Color cache stores a set of colors that have been recently used in the image. 773 774**Rationale:** This way, the recently used colors can sometimes be referred to 775more efficiently than emitting them using the other two methods (described in 776Sections [5.2.1](#prefix-coded-literals) and [5.2.2](#lz77-backward-reference)). 777 778Color cache codes are stored as follows. First, there is a 1-bit value that 779indicates if the color cache is used. If this bit is 0, no color cache codes 780exist, and they are not transmitted in the prefix code that decodes the green 781symbols and the length prefix codes. However, if this bit is 1, the color cache 782size is read next: 783 784~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 785int color_cache_code_bits = ReadBits(4); 786int color_cache_size = 1 << color_cache_code_bits; 787~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 788 789`color_cache_code_bits` defines the size of the color cache (`1 << 790color_cache_code_bits`). The range of allowed values for 791`color_cache_code_bits` is \[1..11\]. Compliant decoders must indicate a 792corrupted bitstream for other values. 793 794A color cache is an array of size `color_cache_size`. Each entry stores one ARGB 795color. Colors are looked up by indexing them by `(0x1e35a7bd * color) >> (32 - 796color_cache_code_bits)`. Only one lookup is done in a color cache; there is no 797conflict resolution. 798 799In the beginning of decoding or encoding of an image, all entries in all color 800cache values are set to zero. The color cache code is converted to this color at 801decoding time. The state of the color cache is maintained by inserting every 802pixel, be it produced by backward referencing or as literals, into the cache in 803the order they appear in the stream. 804 8056 Entropy Code 806-------------- 807 808### 6.1 Overview 809 810Most of the data is coded using a [canonical prefix code][canonical_huff]. 811Hence, the codes are transmitted by sending the _prefix code lengths_, as 812opposed to the actual _prefix codes_. 813 814In particular, the format uses **spatially variant prefix coding**. In other 815words, different blocks of the image can potentially use different entropy 816codes. 817 818**Rationale**: Different areas of the image may have different characteristics. 819So, allowing them to use different entropy codes provides more flexibility and 820potentially better compression. 821 822### 6.2 Details 823 824The encoded image data consists of several parts: 825 826 1. Decoding and building the prefix codes. 827 1. Meta prefix codes. 828 1. Entropy-coded image data. 829 830For any given pixel (x, y), there is a set of five prefix codes associated with 831it. These codes are (in bitstream order): 832 833 * **Prefix code #1**: Used for green channel, backward-reference length, and 834 color cache. 835 * **Prefix code #2, #3, and #4**: Used for red, blue, and alpha channels, 836 respectively. 837 * **Prefix code #5**: Used for backward-reference distance. 838 839From here on, we refer to this set as a **prefix code group**. 840 841#### 6.2.1 Decoding and Building the Prefix Codes 842 843This section describes how to read the prefix code lengths from the bitstream. 844 845The prefix code lengths can be coded in two ways. The method used is specified 846by a 1-bit value. 847 848 * If this bit is 1, it is a _simple code length code_. 849 * If this bit is 0, it is a _normal code length code_. 850 851In both cases, there can be unused code lengths that are still part of the 852stream. This may be inefficient, but it is allowed by the format. 853The described tree must be a complete binary tree. A single leaf node is 854considered a complete binary tree and can be encoded using either the simple 855code length code or the normal code length code. When coding a single leaf 856node using the _normal code length code_, all but one code length are zeros, 857and the single leaf node value is marked with the length of 1 -- even when no 858bits are consumed when that single leaf node tree is used. 859 860##### Simple Code Length Code 861 862This variant is used in the special case when only 1 or 2 prefix symbols are in 863the range \[0..255\] with code length `1`. All other prefix code lengths are 864implicitly zeros. 865 866The first bit indicates the number of symbols: 867 868~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 869int num_symbols = ReadBits(1) + 1; 870~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 871 872The following are the symbol values. 873 874This first symbol is coded using 1 or 8 bits, depending on the value of 875`is_first_8bits`. The range is \[0..1\] or \[0..255\], respectively. The second 876symbol, if present, is always assumed to be in the range \[0..255\] and coded 877using 8 bits. 878 879~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 880int is_first_8bits = ReadBits(1); 881symbol0 = ReadBits(1 + 7 * is_first_8bits); 882code_lengths[symbol0] = 1; 883if (num_symbols == 2) { 884 symbol1 = ReadBits(8); 885 code_lengths[symbol1] = 1; 886} 887~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 888 889The two symbols should be different. Duplicate symbols are allowed, but 890inefficient. 891 892**Note:** Another special case is when _all_ prefix code lengths are _zeros_ (an 893empty prefix code). For example, a prefix code for distance can be empty if 894there are no backward references. Similarly, prefix codes for alpha, red, and 895blue can be empty if all pixels within the same meta prefix code are produced 896using the color cache. However, this case doesn't need special handling, as 897empty prefix codes can be coded as those containing a single symbol `0`. 898 899##### Normal Code Length Code 900 901The code lengths of the prefix code fit in 8 bits and are read as follows. 902First, `num_code_lengths` specifies the number of code lengths. 903 904~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 905int num_code_lengths = 4 + ReadBits(4); 906~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 907 908The code lengths are themselves encoded using prefix codes; lower-level code 909lengths, `code_length_code_lengths`, first have to be read. The rest of those 910`code_length_code_lengths` (according to the order in `kCodeLengthCodeOrder`) 911are zeros. 912 913~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 914int kCodeLengthCodes = 19; 915int kCodeLengthCodeOrder[kCodeLengthCodes] = { 916 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 917}; 918int code_length_code_lengths[kCodeLengthCodes] = { 0 }; // All zeros 919for (i = 0; i < num_code_lengths; ++i) { 920 code_length_code_lengths[kCodeLengthCodeOrder[i]] = ReadBits(3); 921} 922~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 923 924Next, if `ReadBits(1) == 0`, the maximum number of different read symbols 925(`max_symbol`) for each symbol type (A, R, G, B, and distance) is set to its 926alphabet size: 927 928 * G channel: 256 + 24 + `color_cache_size` 929 * Other literals (A, R, and B): 256 930 * Distance code: 40 931 932Otherwise, it is defined as: 933 934~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 935int length_nbits = 2 + 2 * ReadBits(3); 936int max_symbol = 2 + ReadBits(length_nbits); 937~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 938 939If `max_symbol` is larger than the size of the alphabet for the symbol type, the 940bitstream is invalid. 941 942A prefix table is then built from `code_length_code_lengths` and used to read up 943to `max_symbol` code lengths. 944 945 * Code \[0..15\] indicates literal code lengths. 946 * Value 0 means no symbols have been coded. 947 * Values \[1..15\] indicate the bit length of the respective code. 948 * Code 16 repeats the previous nonzero value \[3..6\] times, that is, 949 `3 + ReadBits(2)` times. If code 16 is used before a nonzero 950 value has been emitted, a value of 8 is repeated. 951 * Code 17 emits a streak of zeros of length \[3..10\], that is, `3 + 952 ReadBits(3)` times. 953 * Code 18 emits a streak of zeros of length \[11..138\], that is, 954 `11 + ReadBits(7)` times. 955 956Once code lengths are read, a prefix code for each symbol type (A, R, G, B, and 957distance) is formed using their respective alphabet sizes. 958 959The Normal Code Length Code must code a full decision tree, that is, the sum of 960`2 ^ (-length)` for all non-zero codes must be exactly one. There is however 961one exception to this rule, the single leaf node tree, where the leaf node 962value is marked with value 1 and other values are 0s. 963 964#### 6.2.2 Decoding of Meta Prefix Codes 965 966As noted earlier, the format allows the use of different prefix codes for 967different blocks of the image. _Meta prefix codes_ are indexes identifying which 968prefix codes to use in different parts of the image. 969 970Meta prefix codes may be used _only_ when the image is being used in the 971[role](#roles-of-image-data) of an _ARGB image_. 972 973There are two possibilities for the meta prefix codes, indicated by a 1-bit 974value: 975 976 * If this bit is zero, there is only one meta prefix code used everywhere in 977 the image. No more data is stored. 978 * If this bit is one, the image uses multiple meta prefix codes. These meta 979 prefix codes are stored as an _entropy image_ (described below). 980 981The red and green components of a pixel define a 16-bit meta prefix code used in 982a particular block of the ARGB image. 983 984##### Entropy Image 985 986The entropy image defines which prefix codes are used in different parts of the 987image. 988 989The first 3 bits contain the `prefix_bits` value. The dimensions of the entropy 990image are derived from `prefix_bits`: 991 992~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 993int prefix_bits = ReadBits(3) + 2; 994int prefix_image_width = 995 DIV_ROUND_UP(image_width, 1 << prefix_bits); 996int prefix_image_height = 997 DIV_ROUND_UP(image_height, 1 << prefix_bits); 998~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 999 1000where `DIV_ROUND_UP` is as defined [earlier](#predictor-transform). 1001 1002The next bits contain an entropy image of width `prefix_image_width` and height 1003`prefix_image_height`. 1004 1005##### Interpretation of Meta Prefix Codes 1006 1007The number of prefix code groups in the ARGB image can be obtained by finding 1008the _largest meta prefix code_ from the entropy image: 1009 1010~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1011int num_prefix_groups = max(entropy image) + 1; 1012~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1013where `max(entropy image)` indicates the largest prefix code stored in the 1014entropy image. 1015 1016As each prefix code group contains five prefix codes, the total number of prefix 1017codes is: 1018 1019~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1020int num_prefix_codes = 5 * num_prefix_groups; 1021~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1022 1023Given a pixel (x, y) in the ARGB image, we can obtain the corresponding prefix 1024codes to be used as follows: 1025 1026~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1027int position = 1028 (y >> prefix_bits) * prefix_image_width + (x >> prefix_bits); 1029int meta_prefix_code = (entropy_image[position] >> 8) & 0xffff; 1030PrefixCodeGroup prefix_group = prefix_code_groups[meta_prefix_code]; 1031~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1032 1033where we have assumed the existence of `PrefixCodeGroup` structure, which 1034represents a set of five prefix codes. Also, `prefix_code_groups` is an array of 1035`PrefixCodeGroup` (of size `num_prefix_groups`). 1036 1037The decoder then uses prefix code group `prefix_group` to decode the pixel 1038(x, y), as explained in ["Decoding Entropy-Coded Image 1039Data"](#decoding-entropy-coded-image-data). 1040 1041#### 6.2.3 Decoding Entropy-Coded Image Data 1042 1043For the current position (x, y) in the image, the decoder first identifies the 1044corresponding prefix code group (as explained in the last section). Given the 1045prefix code group, the pixel is read and decoded as follows. 1046 1047Next, read the symbol S from the bitstream using prefix code #1. Note that S is 1048any integer in the range `0` to 1049`(256 + 24 + ` [`color_cache_size`](#color-cache-code)` - 1)`. 1050 1051The interpretation of S depends on its value: 1052 1053 1. If S < 256 1054 1. Use S as the green component. 1055 1. Read red from the bitstream using prefix code #2. 1056 1. Read blue from the bitstream using prefix code #3. 1057 1. Read alpha from the bitstream using prefix code #4. 1058 1. If S >= 256 & S < 256 + 24 1059 1. Use S - 256 as a length prefix code. 1060 1. Read extra bits for the length from the bitstream. 1061 1. Determine backward-reference length L from length prefix code and the 1062 extra bits read. 1063 1. Read the distance prefix code from the bitstream using prefix code #5. 1064 1. Read extra bits for the distance from the bitstream. 1065 1. Determine backward-reference distance D from the distance prefix code 1066 and the extra bits read. 1067 1. Copy L pixels (in scan-line order) from the sequence of pixels starting 1068 at the current position minus D pixels. 1069 1. If S >= 256 + 24 1070 1. Use S - (256 + 24) as the index into the color cache. 1071 1. Get ARGB color from the color cache at that index. 1072 10737 Overall Structure of the Format 1074--------------------------------- 1075 1076Below is a view into the format in Augmented Backus-Naur Form (ABNF) 1077[RFC 5234][] [RFC 7405][]. It does not cover all details. The end-of-image (EOI) 1078is only implicitly coded into the number of pixels (image_width * image_height). 1079 1080Note that `*element` means `element` can be repeated 0 or more times. `5element` 1081means `element` is repeated exactly 5 times. `%b` represents a binary value. 1082 1083#### 7.1 Basic Structure 1084 1085~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1086format = RIFF-header image-header image-stream 1087RIFF-header = %s"RIFF" 4OCTET %s"WEBPVP8L" 4OCTET 1088image-header = %x2F image-size alpha-is-used version 1089image-size = 14BIT 14BIT ; width - 1, height - 1 1090alpha-is-used = 1BIT 1091version = 3BIT ; 0 1092image-stream = optional-transform spatially-coded-image 1093~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1094 1095#### 7.2 Structure of Transforms 1096 1097~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1098optional-transform = (%b1 transform optional-transform) / %b0 1099transform = predictor-tx / color-tx / subtract-green-tx 1100transform =/ color-indexing-tx 1101 1102predictor-tx = %b00 predictor-image 1103predictor-image = 3BIT ; sub-pixel code 1104 entropy-coded-image 1105 1106color-tx = %b01 color-image 1107color-image = 3BIT ; sub-pixel code 1108 entropy-coded-image 1109 1110subtract-green-tx = %b10 1111 1112color-indexing-tx = %b11 color-indexing-image 1113color-indexing-image = 8BIT ; color count 1114 entropy-coded-image 1115~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1116 1117#### 7.3 Structure of the Image Data 1118 1119~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1120spatially-coded-image = color-cache-info meta-prefix data 1121entropy-coded-image = color-cache-info data 1122 1123color-cache-info = %b0 1124color-cache-info =/ (%b1 4BIT) ; 1 followed by color cache size 1125 1126meta-prefix = %b0 / (%b1 entropy-image) 1127 1128data = prefix-codes lz77-coded-image 1129entropy-image = 3BIT ; subsample value 1130 entropy-coded-image 1131 1132prefix-codes = prefix-code-group *prefix-codes 1133prefix-code-group = 1134 5prefix-code ; See "Interpretation of Meta Prefix Codes" to 1135 ; understand what each of these five prefix 1136 ; codes are for. 1137 1138prefix-code = simple-prefix-code / normal-prefix-code 1139simple-prefix-code = ; see "Simple Code Length Code" for details 1140normal-prefix-code = ; see "Normal Code Length Code" for details 1141 1142lz77-coded-image = 1143 *((argb-pixel / lz77-copy / color-cache-code) lz77-coded-image) 1144~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1145 1146The following is a possible example sequence: 1147 1148~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1149RIFF-header image-size %b1 subtract-green-tx 1150%b1 predictor-tx %b0 color-cache-info 1151%b0 prefix-codes lz77-coded-image 1152~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1153 1154[RFC 5234]: https://www.rfc-editor.org/rfc/rfc5234 1155[RFC 7405]: https://www.rfc-editor.org/rfc/rfc7405 1156[canonical_huff]: https://en.wikipedia.org/wiki/Canonical_Huffman_code 1157