Compare commits

...
Author SHA1 Message Date
Nigel Tao 9dbf7d0d5c Revert "lib/suitar: fix Writer block-boundary handling and directory headers" 2026-09-15 10:38:13 +10:00
Nigel Tao 5049107b90 Merge pull request #179 from rootkiller6788/fix-suitar-writer
lib/suitar: fix Writer block-boundary handling and directory headers
2026-09-15 10:35:54 +10:00
rootkiller6788 193d0354b4 lib/suitar: test chunked writes and directory entries
TestWriterChunked writes one file several times, splitting the contents
over Write calls differently each time, and checks the archive bytes come
out identical. The capped writer makes a runaway Write fail the test
instead of hanging it.

TestWriterDir writes a directory and then a file inside it, and reads
both back.
2026-09-12 23:48:25 +08:00
rootkiller6788 26ce8600a1 lib/suitar: let WriteHeader write a directory entry
WriteHeader copied h into w.header before calling w.Write to emit the
GNUTYPE_LONGNAME block holding the name. Write only accepts TypeReg and
TypeGNUSparse, so a TypeDir header failed that call and left the Writer
permanently in an error state - every later entry on the same Writer
failed too.

Set w.header after the name block has gone out instead.
2026-09-12 23:48:18 +08:00
rootkiller6788 05a8209fa7 lib/suitar: reset bIndex after flushing a full block
Write left w.bIndex at 512 once a full block had been written out, so the
next loop iteration took the "partial block" branch, copied zero bytes and
went round again forever, writing the same 512 bytes over and over. Calling
Write with sizes that aren't multiples of 512 is enough to hit this.

It also duplicated the last data block when a Write call happened to end
exactly on a block boundary, because flush then saw a non-zero bIndex and
wrote the block out a second time.
2026-09-12 23:48:01 +08:00
Nigel Tao 0f214ba59c lib/suitar: add trailing EOF blocks
Both /usr/bin/tar and the Go standard library don't seem to need this,
but some other TAR readers might expect it.
2026-09-03 08:31:06 +10:00
Nigel Tao 504f8485f4 lib/suitar: add new Go package
$ TZ=UTC tar -tvf test/data/archive.dense.suitar
-rw-r--r-- nobody/nobody     0 2020-02-05 06:10 artificial/0.bytes
-rw-r--r-- nobody/nobody   853 2020-08-12 14:04 github-tags.json
-rwxr-xr-x nobody/nobody   693 2021-05-03 06:19 hello.sh
-rw-r--r-- nobody/nobody   104 2021-05-03 06:16 non-ascii/αβ.txt
-rw-r--r-- nobody/nobody   151 2021-05-03 06:23 non-ascii/😻.txt
-rw-r--r-- nobody/nobody   208 2020-02-05 06:10 pjw-thumbnail.png
-rw-r--r-- nobody/nobody   942 2020-02-05 06:10 romeo.txt
-rw-r--r-- nobody/nobody   558 2020-02-05 06:10 romeo.txt.gz
2026-09-01 21:55:46 +10:00
Nigel Tao ce863060d6 lib/uncompng: support APNG (Animated PNG) 2026-08-25 10:17:48 +10:00
Nigel Tao 2f7e714ab0 script: add print-png-chunks.go 2026-08-12 21:10:36 +10:00
Nigel Tao e772ccd47a lib/uncompng: rearrange Encode argument order 2026-08-11 09:52:32 +10:00
Nigel Tao 1230506e7c test/data: add "VP8X with animation" test image 2026-08-10 17:40:43 +10:00
Nigel Tao b89fd2fa03 script: undo print-nia-checksums.sh comment
This was accidentally commented out by "std/vp8: blend VP8X ALPH chunk",
commit d9cf0751ca.
2026-08-10 17:38:42 +10:00
Nigel Tao aad70fdfa5 script: print-riff-chunks.go handles WEBP/ANMF 2026-08-10 16:55:56 +10:00
Nigel Tao 466cf6f15d std/webp: tweak workbuf_len determination 2026-08-10 09:23:05 +10:00
Nigel Tao c4df79d82e script: add print-riff-chunks.go 2026-08-07 16:33:25 +10:00
Nigel Tao 59176bb0c7 base: fix color_u64_argb_nonpremul for 64-bit 2026-08-07 09:09:42 +10:00
Nigel Tao 16d6267451 base: specialize swizzle_ycca for bgra_premul 2026-08-06 15:13:04 +10:00
Nigel Tao 5d5fb1d6d9 base38: add "BCP 47 as base38" example 2026-08-06 14:57:00 +10:00
Nigel Tao 0fe5509f1b base: avoid (NULL + 0) in slice functions
Fixes #178
2026-08-06 12:09:47 +10:00
Nigel Tao 7b71d6840a base: rename color_u32_at color_u32_argb_premul_at 2026-08-06 11:53:49 +10:00
Nigel Tao d9cf0751ca std/vp8: blend VP8X ALPH chunk 2026-08-06 11:05:26 +10:00
Nigel Tao 5f398130e8 std/vp8: fix QUIRK_WIDTH_AND_HEIGHT height 2026-08-01 11:09:04 +10:00
Nigel Tao e7f6c79752 std/vp8: decode VP8X ALPH chunk 2026-07-29 15:58:52 +10:00
Nigel Tao 2a97911e02 std/webp: factor out decode_vp8l.wuffs 2026-07-29 15:58:50 +10:00
Nigel Tao 9d8285b52e github/workflows: update fuzz.yml permissions 2026-07-21 15:47:31 +10:00
Nigel Tao 8e909c8ccf github/workflows: update fuzz.yml
Updates PR #174
2026-07-21 15:26:13 +10:00
Nigel Tao 6e9e2e00c2 std/vp8: accept VP8X (but ignore any ALPH chunk) 2026-07-20 21:24:35 +10:00
Nigel Tao be6327a548 test/data: add "VP8X with alpha" test image 2026-07-16 21:49:31 +10:00
Nigel Tao bd3b33770e stb-imagedumper: embiggen MAX_INCL_DIMENSION 2026-07-16 21:49:31 +10:00
Nigel Tao e3bda4efc9 std/webp: ignore alpha_is_used bit per the spec 2026-07-16 21:49:31 +10:00
Nigel Tao 8515165bee std/webp: treat this.width as constant after set 2026-07-16 21:49:31 +10:00
Nigel Tao 183beab481 std/webp: relax VP8L color transform validity
This commit, part of a series, is inspired by pull request PR #168 by
Damian Kaczmarek (with AI assistance), with some modifications.

Updates PR #168
2026-07-16 21:49:31 +10:00
Nigel Tao c2f44c84d1 testlib: apply workbuf_len when decoding images 2026-07-16 21:49:31 +10:00
Nigel Tao 0918f99bc1 Merge pull request #177 from google/dependabot/go_modules/go_modules-f0f091c0c5
build(deps): bump golang.org/x/image from 0.38.0 to 0.41.0 in the go_modules group across 1 directory
2026-07-16 21:47:04 +10:00
dependabot[bot] 30835a0132 build(deps): bump golang.org/x/image
Bumps the go_modules group with 1 update in the / directory: [golang.org/x/image](https://github.com/golang/image).


Updates `golang.org/x/image` from 0.38.0 to 0.41.0
- [Commits](https://github.com/golang/image/compare/v0.38.0...v0.41.0)

---
updated-dependencies:
- dependency-name: golang.org/x/image
  dependency-version: 0.41.0
  dependency-type: direct:production
  dependency-group: go_modules
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-07-14 21:59:41 +00:00
Nigel Tao b22c03837f release: spin in v0.3.5
This copies wuffs-v0.3.c from the v0.3 branch to the main branch. The
main branch is currently working on an unstable v0.4.
2026-07-11 20:44:46 +10:00
Nigel Tao 64ef0df550 mimiclib: #include <limits.h> and <stdint.h>
Updates #175
2026-07-11 20:18:00 +10:00
Nigel Tao eadcb09109 basic-handsum-decode: change Go import line to URL
Fixes #176
2026-07-11 18:28:44 +10:00
Nigel Tao 3fce6225c9 std/vp8: add comments about reconstruction etc 2026-07-09 17:08:58 +10:00
Nigel Tao 46ac36bd7b std/handsum: tweak scaleAndBiasChromaDown 2026-07-08 10:23:10 +10:00
Nigel Tao 0e985fe880 std/jpeg: add link to Ekrem Fidan's blog post 2026-07-07 17:07:15 +10:00
Nigel Tao 0fe290e0e4 convert-to-nia: update list of input formats 2026-07-07 11:07:52 +10:00
Nigel Tao 09b8cbb82f example/stb-imagedumper: alias - to /dev/stdin 2026-07-05 17:43:04 +10:00
Nigel Tao ee923a973c basic-handsum-decode: use memcpy unless __wasm__ 2026-07-04 16:03:34 +10:00
Nigel Tao 67591a3570 std/vp8: simplify swizzle_one_row_of_macroblocks
Prior to 381e186f "std/vp8: swizzle_ycck once, not once per MB row", we
swizzled one row of macroblocks at a time. After 381e186f, we swizzled
the whole image in one pass, but kept the option of, in the future,
amending the swizzle_ycck implementation to allow ((y_min_incl > 0) and
(ycc_upsampling == FANCY_ETC)) at the same time.

However, 381e186f didn't show a performance loss so large that it seems
worth complicating the swizzle_ycck implementation in that way. So we
just drop, in this commit, the option of that amendment.
2026-07-03 21:58:33 +10:00
Nigel Tao b2a2486096 std/vp8: use YCC_UPSAMPLING__FANCY_LIKE_LIBWEBP
Updates PR #168
2026-07-03 21:47:34 +10:00
Nigel Tao 381e186f30 std/vp8: swizzle_ycck once, not once per MB row
This prepares us to change std/vp8's YCC upsampling method from "basic"
to "fancy, like libwebp". Wuffs' current implementation of "fancy"
requires the initial Y coordinate to be zero, as fancy upsampling
special cases the top row of the image and it's simpler if the top row
(row zero) is always presented in the upsampling call.

This is not an optimization or pessimization per se, but for the record,
the performance impact is between negligible and a small improvement.

name                                               old speed      new speed      delta

wuffs_webp_lossy_decode_image_19k_8bpp/clang14     33.7MB/s ± 0%  34.1MB/s ± 0%  +1.28%  (p=0.008 n=5+5)
wuffs_webp_lossy_decode_image_40k_24bpp/clang14     106MB/s ± 0%   106MB/s ± 0%  +0.09%  (p=0.008 n=5+5)
wuffs_webp_lossy_decode_image_77k_24bpp/clang14     129MB/s ± 0%   130MB/s ± 0%  +0.54%  (p=0.008 n=5+5)
wuffs_webp_lossy_decode_image_552k_32bpp/clang14    207MB/s ± 0%   211MB/s ± 0%  +1.90%  (p=0.008 n=5+5)
wuffs_webp_lossy_decode_image_4002k_24bpp/clang14   137MB/s ± 0%   138MB/s ± 0%  +0.40%  (p=0.008 n=5+5)

wuffs_webp_lossy_decode_image_19k_8bpp/gcc12       32.9MB/s ± 0%  32.9MB/s ± 0%  +0.05%  (p=0.008 n=5+5)
wuffs_webp_lossy_decode_image_40k_24bpp/gcc12      98.4MB/s ± 0%  98.8MB/s ± 0%  +0.42%  (p=0.008 n=5+5)
wuffs_webp_lossy_decode_image_77k_24bpp/gcc12       125MB/s ± 0%   125MB/s ± 0%  +0.17%  (p=0.008 n=5+5)
wuffs_webp_lossy_decode_image_552k_32bpp/gcc12      211MB/s ± 0%   214MB/s ± 0%  +1.21%  (p=0.008 n=5+5)
wuffs_webp_lossy_decode_image_4002k_24bpp/gcc12     137MB/s ± 0%   137MB/s ± 0%    ~     (p=0.095 n=5+5)
2026-07-03 21:47:26 +10:00
Nigel Tao 85a0283cfc base: tweak swizzle_ycck ycc_upsampling parameter
This moves from a 2-valued "basic (box) or fancy (triangle)" upsampling
parameter to a 3-valued one, which can now represent the fact that
libjpeg's and libwebp's fancy (triangle) upsampling is ever-so-slightly
different (see script/script/replicate-libwebp-chroma-upsampling.go and
its comments).

This is not an optimization or pessimization per se, but for the record,
the performance impact is mixed.

name                                             old speed     new speed     delta

wuffs_jpeg_decode_19k_8bpp/clang14               317MB/s ± 0%  314MB/s ± 0%  -0.99%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_progressive/clang14  318MB/s ± 0%  328MB/s ± 0%  +3.13%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_sequential/clang14   879MB/s ± 0%  871MB/s ± 0%  -0.83%  (p=0.008 n=5+5)
wuffs_jpeg_decode_77k_24bpp/clang14              719MB/s ± 0%  713MB/s ± 0%  -0.89%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_420/clang14         779MB/s ± 0%  771MB/s ± 0%  -1.01%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_444/clang14         902MB/s ± 0%  890MB/s ± 0%  -1.32%  (p=0.008 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/clang14            784MB/s ± 0%  779MB/s ± 0%  -0.66%  (p=0.008 n=5+5)

wuffs_jpeg_decode_19k_8bpp/gcc12                 358MB/s ± 0%  362MB/s ± 0%  +0.93%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_progressive/gcc12    305MB/s ± 0%  311MB/s ± 0%  +1.91%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_sequential/gcc12     912MB/s ± 0%  918MB/s ± 0%  +0.73%  (p=0.016 n=4+5)
wuffs_jpeg_decode_77k_24bpp/gcc12                749MB/s ± 0%  763MB/s ± 0%  +1.83%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_420/gcc12           813MB/s ± 0%  824MB/s ± 0%  +1.37%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_444/gcc12           969MB/s ± 0%  984MB/s ± 0%  +1.62%  (p=0.008 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/gcc12              815MB/s ± 0%  832MB/s ± 0%  +2.06%  (p=0.008 n=5+5)
2026-07-03 21:38:28 +10:00
Nigel Tao e3b5a7d506 script: add replicate-libwebp-chroma-upsampling.go 2026-06-30 16:09:45 +10:00
Nigel Tao 58968fab9f basic-handsum-decode: remove bias_and_clamp LUT 2026-06-30 11:26:46 +10:00
Nigel Tao 5bab861b51 basic-handsum-decode: roll upsample_from to loops 2026-06-30 11:14:48 +10:00
Nigel Tao d965fe5638 basic-handsum-decode: use our own memcpy 2026-06-30 11:14:36 +10:00
Nigel Tao 0e06bcc9bc std/jpeg: add Further Reading README.md section 2026-06-29 22:05:13 +10:00
Nigel Tao 79bf624c32 std/vp8: add README.md 2026-06-29 16:09:59 +10:00
Nigel Tao ad039b5d3a snippet: add basic-handsum-decode.c 2026-06-29 14:28:48 +10:00
Nigel Tao 9c6d1dacad cmd/handsum: append '\n' to base64 output 2026-06-25 22:22:26 +10:00
81 changed files with 8716 additions and 2183 deletions

No files matched your search

+3 -1
View File
@@ -1,5 +1,7 @@
name: CIFuzz
on: [pull_request]
permissions: read-all
jobs:
Fuzzing:
runs-on: ubuntu-latest
@@ -16,7 +18,7 @@ jobs:
fuzz-seconds: 600
dry-run: false
- name: Upload Crash
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
if: failure()
with:
name: artifacts
+1
View File
@@ -203,6 +203,7 @@ func encode(w io.Writer, b64e *base64.Encoding, r io.Reader, color handsum.Color
if b64e != nil {
enc := b64e.AppendEncode(nil, buf.Bytes())
enc = append(enc, '\n')
if _, err := w.Write(enc); err != nil {
return err
}
+7
View File
@@ -70,6 +70,13 @@ code (instead of writing C/C++ code that _uses_ Wuffs' standard library).
a tab).
## 2026-07-11 version 0.3.5
- No significant changes to `wuffs-v0.3.c` per se.
- Some test files were renamed, to work better with pkg.go.dev indexing.
- Some test code had #include lines added, to fix some third party builds.
## 2024-04-19 version 0.3.4
- Have `std/png` ignore tRNS chunks for color types 4 (YA) and 6 (RGBA).
+33
View File
@@ -117,6 +117,39 @@ Some more examples:
- `0x197816_7DF74000 = "text/html/.." = "text/html"`
- `0x197816_B215E800 = "text/plai/.." = "text/plain"`
For another fits-in-53-bits example, BCP 47 language tags can use base38
3+1/2+2/1+1 encoding, `lllx/ssrr/yz`, where the l, s and r base-38 digits hold
the commonly-used language, script and region subtags. The x, y and z base-38
digits hold the rarely-used extlang, variant and extension or other subtags.
- `0x0B2084_000004C0 = "ca../..../v." = ca-valencia`
- `0x0D1688_00000000 = "en../..../.." = en`
- `0x0D32BC_00060000 = "es../..41/.." = es-419`
- `0x0E0370_000FC800 = "fr../..ca/.." = fr-CA`
- `0x138442_00000000 = "mas./..../.." = mas`
- `0x1DFF1E_7DF14800 = "yue./hthk/.." = yue-Hant-HK`
- `0x1E89E8_7DAE2000 = "zh../hs../.." = zh-Hans`
### Powers of 38
In case you want to roll your own bit-packing.
```
38 ** 1 = 38 = 0x________________26 ≈ 6 bits
38 ** 2 = 1444 = 0x_______________5A4 ≈ 11 bits
38 ** 3 = 54872 = 0x______________D658 ≈ 16 bits
38 ** 4 = 2_085136 = 0x____________1FD110 ≈ 21 bits
38 ** 5 = 79_235168 = 0x___________4B90860 ≈ 27 bits
38 ** 6 = 3010_936384 = 0x__________B3773E40 ≈ 32 bits
38 ** 7 = 114415_582592 = 0x_______1A_A3B33D80 ≈ 37 bits
38 ** 8 = 4_347792_138496 = 0x______3F4_4C9B2100 ≈ 42 bits
38 ** 9 = 165_216101_262848 = 0x_____9643_5F06E600 ≈ 48 bits
38 ** 10 = 6278_211847_988224 = 0x___164E00_1B062400 ≈ 53 bits
38 ** 11 = 238572_050223_552512 = 0x__34F9404_02E95800 ≈ 58 bits
38 ** 12 = 9_065737_908494_995456 = 0x_7DCFF898_6EA31000 ≈ 63 bits
```
### Base38 Alphabet
+3 -2
View File
@@ -76,11 +76,12 @@ Common quirks:
Package-specific quirks:
- [GIF image decoder quirks](/std/gif/decode_quirks.wuffs)
- [GIF decoder quirks](/std/gif/decode_quirks.wuffs)
- [JPEG decoder quirks](/std/jpeg/decode_quirks.wuffs)
- [JSON decoder quirks](/std/json/decode_quirks.wuffs)
- [LZMA decoder quirks](/std/lzma/decode_quirks.wuffs)
- [LZW decoder quirks](/std/lzw/decode_quirks.wuffs)
- [TH decoder quirks](/std/thumbhash/decode_quirks.wuffs)
- [THUMBHASH decoder quirks](/std/thumbhash/decode_quirks.wuffs)
- [VP8 decoder quirks](/std/vp8/decode_quirks.wuffs)
- [XZ decoder quirks](/std/xz/decode_quirks.wuffs)
- [ZLIB decoder quirks](/std/zlib/decode_quirks.wuffs)
+1
View File
@@ -3,3 +3,4 @@ Wuffs or stand-alone formats that were invented alongside Wuffs' development.
- [Naïve Image Formats: NIE, NII, NIA](/doc/spec/nie-spec.md)
- [Random Access Compression: RAC](/doc/spec/rac-spec.md)
- [Simple Uncompressed Interchange Tape Archive: SUITAR](/doc/spec/suitar-spec.md)
+220
View File
@@ -0,0 +1,220 @@
# Simple Uncompressed Interchange Tape Archive: SUITAR
Status: Draft (as of September 2026). There is no compatibility guarantee yet.
## Overview
SUITAR is a common-denominator, trivial-to-parse interchange format for "the
result of decompressing a compressed file or unpacking an archive file", such
as ZIP, RAR, 7Z, TAR, GZIP, BZIP2, XZ, ZSTD, etc.
SUITAR is to archive files (including `foobar.dat.xz` compressed files as an
archive containing 1 file) as [Farbfeld](https://tools.suckless.org/farbfeld/)
or [NIE](./nie-spec.md) is to image files: an uncompressed, "designed for Unix
pipes" format that is trivial to read or write in a few hundred lines of code,
ideally in a memory-safe programming language. It's a format for what the
Chromium web browser's "Rule of 2" security advice calls
[https://chromium.googlesource.com/chromium/src/+/master/docs/security/rule-of-2.md#normalization](Normalization).
## Subset of TAR
SUITAR is a subset of the well-known and widely-used TAR (Tape Archive, with
GNU extensions) archive file format. Every valid SUITAR file is also a valid
TAR file. These files work with popular tools like `/usr/bin/tar` and with
popular TAR-reading libraries in a variety of programming languages.
SUITAR is a subset of "pure TAR", not of "TAR wrapped in GZIP", "TAR wrapped in
BZIP2", etc. A separate compression step wrapping SUITAR is feasible, just like
wrapping TAR, but is out of scope of this document.
Like TAR, SUITAR files are a sequence of independent entries (files or
directories). Independent means that, like JSON map keys, duplicate names are
valid, although some decoders may choose to reject them.
A file's entry does not need to be preceded by explicit entries for that file's
parent directories.
### Further Restrictions
Compared to plain TAR (and refer to [the GNU TAR
manual](https://ftp.gnu.org/old-gnu/Manuals/tar-1.12/html_node/tar_123.html)),
SUITAR has further restrictions:
- Entries are either regular files (`REGTYPE`), sparse files (`GNUTYPE_SPARSE`)
or directories (`DIRTYPE`). There is no support for hard links, symlinks,
device files or other non-standard files.
- Sparse files must be completely sparse. Their content must be one contiguous
span of NUL (zero) bytes that covers the entire file.
- File and directory names must obey the "File Name Validity" rules, below.
- These names must be encoded by a `GNUTYPE_LONGNAME` header block, regardless
of whether the name's length is over or under 100 or 255 bytes.
- File size and modTime (modification time, seconds since Unix epoch) integers
must use base-256 encoding (not base-8 octal) and must be non-negative and
less than `(1 << 53)`, which is `9007_199254_740992`.
- Mode bits are either 0o644 (`rw-r--r--`) or 0o755 (`rwxr-xr-x`), encoded in
base-8 octal (not base-256).
- UID and GID are hard-coded to 65534 (as a number, equivalent to octal
0o0177776) and "nobody" (as a string).
- Any other fields are unused and must be NUL bytes.
- Padding bytes (as TAR uses 512-byte blocks) must be NUL bytes.
There is no support for various TAR variants, such as "the PAX extensions to
TAR" or "the USTAR extensions to TAR", other than what's implied by the subset
of the GNU extensions that SUITAR explicitly uses.
Encoders have no meaningful choices, bar one exception. There is only one valid
SUITAR encoding (unlike full TAR's backwards-compatible choice between base-8
or base-256 encoding of various sufficiently small numbers) for any given file
or directory entry (its combination of type, name, size, mode, modTime and
contents).
The one exception is that, if a file's contents are all NUL bytes (including
zero-sized files), an encoder can choose between a `REGTYPE` regular file (with
explicit NULs) or a `GNUTYPE_SPARSE` sparse file (with implicit NULs).
### File Name Validity
These rules apply to both file names and directory names.
- Names must be valid UTF-8.
- Names must not contain any ASCII control characters, including `'\n'` or
`'\x00'`, the "new line" or NUL bytes.
- Names must not contain the `'\x7F'` ASCII DEL byte.
- Names must be no longer than 4095 bytes, excluding a trailing NUL.
- Names must not be `""`, `"."` or `".."`.
- Names must not start or end with `"/"`, `"./"` or `"../"`.
- Names must not contain `"//"`, `"/./"` or `"/../"` as substrings.
For example, when converting from ZIP (with Japanese file names) to SUITAR, it
is the SUITAR producer's responsibility, not the SUITAR consumer's, to detect
and transform Shift-JIS encoded names to equivalent and valid UTF-8.
## File Structure
SUITAR files are a sequence of entries, followed by a 1024-byte "End Of File"
marker. The EOF marker's bytes are all NUL. Each entry occupies an integer
number of 512-byte blocks:
- 1 `GNUTYPE_LONGNAME` header block.
- 1 or more payload blocks containing the file or directory name.
- 1 `REGTYPE`, `GNUTYPE_SPARSE` or `DIRTYPE` header block.
- If `REGTYPE`, 0 or more payload blocks containing the file contents.
- If not `REGTYPE`, no further blocks.
### Header Blocks
Like all blocks, each header block is 512 bytes long. Each header block also
starts with a 12-byte magic signature (that is not valid UTF-8), identifying
SUITAR version 1. There are no other versions at this time.
The first 384 out of 512 bytes must match this template (arranged as 24 rows of
16 bytes per row, plus commentary):
@@@@@@@@@@@@.... @@@@@@@@@@@@ = "\x13sUItAR\x00\xFE\xFDv1".
................
................
................
................
................
....0000???.0177 ??? = mode.
776.0177776.$...
????????$...???? ???????? = physical size, ???????? = modTime.
??????????. ?... ?????? = checksum, ? = type.
................
................
................
................
................
................
.ustar .nobody.
................
.........nobody.
................
................
................
................
................
In this template, `@` indicates the magic signature, `.` indicates a `0x00` NUL
byte, `$` indicates a `0x80` byte and `?` indicates parts of the template that
are variable, not hard-coded.
These `?` bytes are the 3-byte mode (`"644"` or `"755"`), physical size or
modTime as an 8-byte big-endian `uint64`, 6-byte checksum (see below) or 1-byte
type, which must be one of:
- `'0'` for `REGTYPE`.
- `'5'` for `DIRTYPE`, in which case mode must be `"755"` and physical size
must be all zeroes.
- `'L'` for `GNUTYPE_LONGNAME`, in which case mode must be `"644"` and modTime
must be all zeroes.
- `'S'` for `GNUTYPE_SPARSE`, in which case physical size must be all zeroes
and offset and logical size (see below) must be the same number and, again,
within the half-open range `0 .. (1 << 53)`.
The last 128 out of 512 bytes (8 rows of 16 bytes per row) must be all NUL
bytes unless the type is `GNUTYPE_SPARSE`, in which case it must match this
template (and `?` again indicates an 8-byte big-endian `uint64`):
..$...????????$. ???????? = sparse offset.
................
................
................
................
................
...$...????????. ???????? = sparse logical size.
................
### Header Checksum
A 512-byte header block's checksum value is simply the sum of each byte (after
converting from `uint8` to `uint32`, to avoid overflow) in the block, at
offsets in the two half-open ranges `0 .. 148` and `156 .. 512`, which excludes
the 8 bytes for the 6-byte checksum itself plus another two hard-coded bytes
`"\x00\x20"`.
That checksum value is written as a 6-byte ASCII octal number in the header.
For example, `4853` (decimal) would be encoded as `"011365"` (octal).
### Payload Blocks
Each entry has one or more payload blocks, between its two header blocks,
containing the file or directory name. The name length (including a trailing
NUL byte) is the first header block's physical size value, and must be within
the half-open range `2 .. 4096`, and so the excluding-a-trailing-NUL length
must range within `1 .. 4095`. Rounding up that including-a-trailing-NUL length
to a multiple of 512 gives the number of 512-byte payload blocks that contain
the name. All padding bytes in the name's final payload block must be NUL.
For `REGTYPE` entries, the second header block's physical size value gives the
reconstructed file's size and rounding that up to a multiple of 512 gives the
number of 512-byte payload blocks that contain the file contents. Again, all
padding bytes in the contents' final payload block must be NUL.
For other entries (`DIRTYPE` or `GNUTYPE_SPARSE`), there are no further payload
blocks after the second header block.
For `GNUTYPE_SPARSE` entries, the second header block's logical size value
gives the reconstructed file's size and its contents are all NUL bytes.
# Reference Implementation
The [google/wuffs](https://github.com/google/wuffs) repository, which holds
this specification document, also holds a
[suitar](https://godoc.org/github.com/google/wuffs/lib/suitar) Go package and
some `test/data/*.suitar` example files, readable by that Go package but also
by `/usr/bin/tar`.
---
Updated on September 2026.
+4 -2
View File
@@ -132,8 +132,9 @@ In Wuffs syntax, the `base.image_decoder` methods are:
- [std/nie](/std/nie)
- [std/png](/std/png)
- [std/qoi](/std/qoi)
- [std/tga](/std/tga)
- [std/targa](/std/targa)
- [std/thumbhash](/std/thumbhash)
- [std/vp8](/std/vp8)
- [std/wbmp](/std/wbmp)
- [std/webp](/std/webp)
@@ -160,7 +161,8 @@ Examples in other repositories:
- [GIF decoder quirks](/std/gif/decode_quirks.wuffs)
- [JPEG decoder quirks](/std/jpeg/decode_quirks.wuffs)
- [TH decoder quirks](/std/thumbhash/decode_quirks.wuffs)
- [THUMBHASH decoder quirks](/std/thumbhash/decode_quirks.wuffs)
- [VP8 decoder quirks](/std/vp8/decode_quirks.wuffs)
## Related Documentation
+12 -8
View File
@@ -11,8 +11,8 @@
// ----------------
/*
convert-to-nia converts an image from stdin (e.g. in the BMP, GIF, JPEG or PNG
format) to stdout (in the NIA/NIE format).
convert-to-nia converts an image from stdin (e.g. in the BMP, GIF, JPEG, PNG,
PPM, QOI, TARGA or WEBP format) to stdout (in the NIA/NIE format).
See the "const char* g_usage" string below for details.
@@ -135,8 +135,8 @@ static const char* g_usage =
" -fail-if-unsandboxed\n"
"\n"
"convert-to-nia converts an image from stdin (e.g. in the BMP, GIF, JPEG\n"
"or PNG format) to stdout (in the NIA format, or in the NIE, hash, PPM\n"
"or PNG format if the -1, -d, -p or -u flag is given).\n"
"PNG, PPM, QOI, TARGA or WEBP format) to stdout (in the NIA format, or\n"
"in the NIE, hash, PPM or PNG format if given -1, -d, -p or -u flags).\n"
"\n"
"NIA/NIE is a trivial animated/still image file format, specified at\n"
"https://github.com/google/wuffs/blob/main/doc/spec/nie-spec.md\n"
@@ -609,8 +609,12 @@ redirect:
WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, w, h);
// Configure the work buffer.
uint64_t workbuf_len =
wuffs_base__image_decoder__workbuf_len(g_image_decoder).max_incl;
wuffs_base__range_ii_u64 r =
wuffs_base__image_decoder__workbuf_len(g_image_decoder);
if (wuffs_base__range_ii_u64__is_empty(&r)) {
return "main: unsupported image (indeterminate work buffer length)";
}
uint64_t workbuf_len = r.max_incl;
if (g_workbuf_slice.len < workbuf_len) {
return "main: image is too large (to configure work buffer)";
}
@@ -800,10 +804,10 @@ print_uncompressed_png_frame() {
uint32_t h = wuffs_base__pixel_config__height(&g_pixbuf.pixcfg);
wuffs_base__table_u8 tab = wuffs_base__pixel_buffer__plane(&g_pixbuf, 0);
return UNCOMPNG__RESULT__OK ==
uncompng__encode(&my_uncompng_write_func, NULL, tab.ptr,
uncompng__encode(&my_uncompng_write_func, NULL, pixfmt, w, h, tab.ptr,
wuffs_base__table__flattened_length(
tab.width, tab.height, tab.stride),
w, h, tab.stride, pixfmt);
tab.stride);
}
void //
+5 -1
View File
@@ -312,7 +312,11 @@ try_allocate(wuffs_gif__decoder* dec) {
return "could not allocate prev-dst buffer";
}
uint64_t workbuf_len_max_incl = wuffs_gif__decoder__workbuf_len(dec).max_incl;
wuffs_base__range_ii_u64 r = wuffs_gif__decoder__workbuf_len(dec);
if (wuffs_base__range_ii_u64__is_empty(&r)) {
return "indeterminate work buffer length";
}
uint64_t workbuf_len_max_incl = r.max_incl;
if (workbuf_len_max_incl > 0) {
g_workbuf = wuffs_base__malloc_slice_u8(malloc, workbuf_len_max_incl);
if (!g_workbuf.ptr) {
+7 -6
View File
@@ -684,7 +684,7 @@ struct {
static const char* g_usage =
"Usage: stb-imagedumper *.{jpeg,png}\n"
"\n"
"If no filenames are given then it defaults to /dev/stdin\n"
"The \"-\" filename is a special case, a short equivalent of /dev/stdin\n"
"\n"
"Flags:\n"
" -a or -ascii-art (use ASCII art instead of ANSI color codes)\n"
@@ -961,7 +961,7 @@ apply_background_color(uint32_t bg_color_premul,
// ----------------
#define MAX_INCL_DIMENSION 10000
#define MAX_INCL_DIMENSION 16384
// BYTES_PER_COLOR_OUTPUT_CHARACTER is long enough to contain
// "\x1B[38;2;255;255;255m\x1B[48;2;255;255;255mABC" plus a trailing NUL byte.
@@ -994,7 +994,11 @@ handle(const char* filename,
&src_w, &src_h, &channels_in_file,
bytes_per_pixel);
} else {
src_pixels = stbi_load(filename, //
const char* fn = filename;
if ((fn[0] == '-') && (fn[1] == '\x00')) {
fn = "/dev/stdin";
}
src_pixels = stbi_load(fn, //
&src_w, &src_h, &channels_in_file, bytes_per_pixel);
}
@@ -1201,9 +1205,6 @@ main(int argc, char** argv) {
handle(demos[c].filename, demos[c].src_len, demos[c].src_ptr,
demos[c].src_len);
}
} else if (g_flags.remaining_argc == 0) {
handle("/dev/stdin", 0, NULL, 0);
}
for (int c = 0; c < g_flags.remaining_argc; c++) {
+5 -5
View File
@@ -237,11 +237,11 @@ fuzz_swizzle_ycck(wuffs_base__io_buffer* src, uint64_t hash) {
uint32_t height_in_mcus = (3 & (hash >> 34)) + 1;
uint32_t allow_hv3 = 1 & (hash >> 36);
bool is_rgb_or_cmyk = 1 & (hash >> 37);
bool triangle_filter_for_2to1 = 1 & (hash >> 38);
uint8_t ycc_model = (1 & (hash >> 37)) ? 0x80 : 0x00;
uint8_t ycc_upsampling = 1 & (hash >> 38);
bool use_4th_component = 1 & (hash >> 39);
if (triangle_filter_for_2to1) {
if (ycc_upsampling) {
x_min_incl = 0;
y_min_incl = 0;
} else {
@@ -411,8 +411,8 @@ fuzz_swizzle_ycck(wuffs_base__io_buffer* src, uint64_t hash) {
width0, width1, width2, width3, //
h0, h1, h2, h3, //
v0, v1, v2, v3, //
is_rgb_or_cmyk, //
triangle_filter_for_2to1, //
ycc_model, //
ycc_upsampling, //
wuffs_base__make_slice_u8(scratch_buffer, sizeof(scratch_buffer)));
if (status.repr) {
return wuffs_base__status__message(&status);
+1 -1
View File
@@ -2,4 +2,4 @@ module github.com/google/wuffs
go 1.25.0
require golang.org/x/image v0.38.0
require golang.org/x/image v0.41.0
+2 -2
View File
@@ -1,2 +1,2 @@
golang.org/x/image v0.38.0 h1:5l+q+Y9JDC7mBOMjo4/aPhMDcxEptsX+Tt3GgRQRPuE=
golang.org/x/image v0.38.0/go.mod h1:/3f6vaXC+6CEanU4KJxbcUZyEePbyKbaLoDOe4ehFYY=
golang.org/x/image v0.41.0 h1:8wS72eGJMJaBxK6okTzd4WaXumUlTVlb753MlsSvTCo=
golang.org/x/image v0.41.0/go.mod h1:uIc348UZMSvS5Z65CVZ7iDPaNobNFEPeJ4kbqTOszmA=
+6 -2
View File
@@ -507,8 +507,9 @@ redirect:
}
wuffs_base__pixel_buffer pixel_buffer = alloc_pixbuf_result.pixbuf;
if (valid_background_color) {
wuffs_base__status pb_scufr_status = pixel_buffer.set_color_u32_fill_rect(
pixel_buffer.pixcfg.bounds(), background_color);
wuffs_base__status pb_scufr_status =
pixel_buffer.set_color_u32_argb_premul_fill_rect(
pixel_buffer.pixcfg.bounds(), background_color);
if (pb_scufr_status.repr != nullptr) {
return DecodeImageResult(pb_scufr_status.message());
}
@@ -517,6 +518,9 @@ redirect:
// Allocate the work buffer. Wuffs' decoders conventionally assume that this
// can be uninitialized memory.
wuffs_base__range_ii_u64 workbuf_len = image_decoder->workbuf_len();
if (workbuf_len.is_empty()) {
return DecodeImageResult(DecodeImage_UnsupportedImageFormat);
}
DecodeImageCallbacks::AllocWorkbufResult alloc_workbuf_result =
callbacks.AllocWorkbuf(workbuf_len, true);
if (!alloc_workbuf_result.error_message.empty()) {
+2
View File
@@ -162,6 +162,8 @@ class DecodeImageCallbacks {
// should be at least len_range.min_incl, but larger allocations (up to
// len_range.max_incl) may have better performance (by using more memory).
//
// The len_range argument will satisfy !len_range.is_empty().
//
// The default AllocWorkbuf implementation allocates len_range.max_incl bytes
// of either uninitialized or zeroed memory.
virtual AllocWorkbufResult //
+16 -6
View File
@@ -1466,7 +1466,7 @@ wuffs_base__make_slice_u64(uint64_t* ptr, size_t len) {
static inline wuffs_base__slice_u8 //
wuffs_base__make_slice_u8_ij(uint8_t* ptr, size_t i, size_t j) {
wuffs_base__slice_u8 ret;
ret.ptr = ptr ? (ptr + i) : NULL;
ret.ptr = i ? (ptr + i) : ptr;
ret.len = (j >= i) ? (j - i) : 0;
return ret;
}
@@ -1474,7 +1474,7 @@ wuffs_base__make_slice_u8_ij(uint8_t* ptr, size_t i, size_t j) {
static inline wuffs_base__slice_u16 //
wuffs_base__make_slice_u16_ij(uint16_t* ptr, size_t i, size_t j) {
wuffs_base__slice_u16 ret;
ret.ptr = ptr ? (ptr + i) : NULL;
ret.ptr = i ? (ptr + i) : ptr;
ret.len = (j >= i) ? (j - i) : 0;
return ret;
}
@@ -1482,7 +1482,7 @@ wuffs_base__make_slice_u16_ij(uint16_t* ptr, size_t i, size_t j) {
static inline wuffs_base__slice_u32 //
wuffs_base__make_slice_u32_ij(uint32_t* ptr, size_t i, size_t j) {
wuffs_base__slice_u32 ret;
ret.ptr = ptr ? (ptr + i) : NULL;
ret.ptr = i ? (ptr + i) : ptr;
ret.len = (j >= i) ? (j - i) : 0;
return ret;
}
@@ -1490,7 +1490,7 @@ wuffs_base__make_slice_u32_ij(uint32_t* ptr, size_t i, size_t j) {
static inline wuffs_base__slice_u64 //
wuffs_base__make_slice_u64_ij(uint64_t* ptr, size_t i, size_t j) {
wuffs_base__slice_u64 ret;
ret.ptr = ptr ? (ptr + i) : NULL;
ret.ptr = i ? (ptr + i) : ptr;
ret.len = (j >= i) ? (j - i) : 0;
return ret;
}
@@ -1631,7 +1631,12 @@ wuffs_base__slice_u8__overlaps(wuffs_base__slice_u8 s, wuffs_base__slice_u8 t) {
static inline wuffs_base__slice_u8 //
wuffs_base__slice_u8__subslice_i(wuffs_base__slice_u8 s, uint64_t i) {
if ((i <= SIZE_MAX) && (i <= s.len)) {
return wuffs_base__make_slice_u8(s.ptr + i, ((size_t)(s.len - i)));
// The "i ? (s.ptr + i) : s.ptr" expression is equivalent to the plain
// "s.ptr + i", except for the fact that in C (prior to C29), but not in
// C++, "NULL + 0" is, technically, undefined behavior (and triggers
// ubsan). See C29 N3322 and https://reviews.llvm.org/D67122
return wuffs_base__make_slice_u8(i ? (s.ptr + i) : s.ptr,
((size_t)(s.len - i)));
}
return wuffs_base__empty_slice_u8();
}
@@ -1655,7 +1660,12 @@ wuffs_base__slice_u8__subslice_ij(wuffs_base__slice_u8 s,
uint64_t i,
uint64_t j) {
if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {
return wuffs_base__make_slice_u8(s.ptr + i, ((size_t)(j - i)));
// The "i ? (s.ptr + i) : s.ptr" expression is equivalent to the plain
// "s.ptr + i", except for the fact that in C (prior to C29), but not in
// C++, "NULL + 0" is, technically, undefined behavior (and triggers
// ubsan). See C29 N3322 and https://reviews.llvm.org/D67122
return wuffs_base__make_slice_u8(i ? (s.ptr + i) : s.ptr,
((size_t)(j - i)));
}
return wuffs_base__empty_slice_u8();
}
+7 -1
View File
@@ -68,9 +68,15 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
uint8_t v2,
uint8_t v3,
uint8_t ycc_model,
bool triangle_filter_for_2to1,
uint8_t ycc_upsampling,
wuffs_base__slice_u8 scratch_buffer_2k);
// ---------------- Images (Utility)
#define wuffs_base__utility__make_pixel_blend wuffs_base__make_pixel_blend
#define wuffs_base__utility__make_pixel_format wuffs_base__make_pixel_format
static inline uint8_t //
wuffs_base__utility__demake_pixel_blend(wuffs_base__pixel_blend a) {
return a;
}
+115 -12
View File
@@ -32,6 +32,10 @@
// 0xAARRGGBB (Alpha most significant, Blue least), regardless of endianness.
typedef uint32_t wuffs_base__color_u32_argb_premul;
// wuffs_base__color_u32_argb_nonpremul is non-premultiplied version of
// wuffs_base__color_u32_argb_premul.
typedef uint32_t wuffs_base__color_u32_argb_nonpremul;
// wuffs_base__color_u32_argb_premul__is_valid returns whether c's Red, Green
// and Blue channels are all less than or equal to its Alpha channel. c uses
// premultiplied alpha, so 50% opaque 100% saturated red is 0x7F7F_0000 and a
@@ -251,6 +255,21 @@ wuffs_base__color_u64_argb_premul__as__color_u64_argb_nonpremul(
return (a << 48) | (r << 32) | (g << 16) | (b << 0);
}
static inline uint64_t //
wuffs_base__color_u64_argb_nonpremul__as__color_u64_argb_premul(
uint64_t argb_nonpremul) {
uint64_t a = 0xFFFF & (argb_nonpremul >> 48);
uint64_t r = 0xFFFF & (argb_nonpremul >> 32);
r = (r * a) / 0xFFFF;
uint64_t g = 0xFFFF & (argb_nonpremul >> 16);
g = (g * a) / 0xFFFF;
uint64_t b = 0xFFFF & (argb_nonpremul >> 0);
b = (b * a) / 0xFFFF;
return (a << 48) | (r << 32) | (g << 16) | (b << 0);
}
static inline uint8_t //
wuffs_base__color_u64_argb_nonpremul__as__color_u8_gray(
uint64_t argb_nonpremul) {
@@ -482,6 +501,11 @@ typedef uint8_t wuffs_base__pixel_blend;
#define WUFFS_BASE__PIXEL_BLEND__SRC ((wuffs_base__pixel_blend)0)
#define WUFFS_BASE__PIXEL_BLEND__SRC_OVER ((wuffs_base__pixel_blend)1)
static inline wuffs_base__pixel_blend //
wuffs_base__make_pixel_blend(uint8_t repr) {
return repr;
}
// --------
// wuffs_base__pixel_alpha_transparency is a pixel format's alpha channel
@@ -863,13 +887,21 @@ wuffs_base__pixel_subsampling::denominator_y(uint32_t plane) const {
//
// For CMY, C = (100% - R), M = (100% - G) and Y = (100% - B). Black (K), if
// present, then modulates the three RGB values.
//
// ----
//
// The 0x02 bit means the fourth channel is an alpha channel and, if set, then
// the 0x01 bit gives the blend mode (WUFFS_BASE__PIXEL_BLEND__SRC is 0,
// WUFFS_BASE__PIXEL_BLEND__SRC_OVER is 1).
// clang-format off
#define WUFFS_BASE__YCC_MODEL__BT_601_FULL_RANGE 0x00
#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE 0x01
#define WUFFS_BASE__YCC_MODEL__RGB 0x40
#define WUFFS_BASE__YCC_MODEL__CMY 0x80
#define WUFFS_BASE__YCC_MODEL__BT_601_FULL_RANGE 0x00
#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE 0x04
#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC 0x06
#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC_OVER 0x07
#define WUFFS_BASE__YCC_MODEL__RGB 0x40
#define WUFFS_BASE__YCC_MODEL__CMY 0x80
// clang-format on
@@ -1495,15 +1527,32 @@ typedef struct wuffs_base__pixel_buffer__struct {
inline wuffs_base__slice_u8 palette_or_else(wuffs_base__slice_u8 fallback);
inline wuffs_base__pixel_format pixel_format() const;
inline wuffs_base__table_u8 plane(uint32_t p);
// Deprecated: use color_u32_argb_premul_at.
inline wuffs_base__color_u32_argb_premul color_u32_at(uint32_t x,
uint32_t y) const;
inline wuffs_base__color_u32_argb_premul color_u32_argb_premul_at(
uint32_t x,
uint32_t y) const;
// Deprecated: use set_color_u32_argb_premul_at.
inline wuffs_base__status set_color_u32_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color);
inline wuffs_base__status set_color_u32_argb_premul_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color);
// Deprecated: use set_color_u32_argb_premul_fill_rect.
inline wuffs_base__status set_color_u32_fill_rect(
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color);
inline wuffs_base__status set_color_u32_argb_premul_fill_rect(
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color);
inline wuffs_base__status set_color_u32_argb_nonpremul_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color);
inline bool is_opaque();
#endif // __cplusplus
@@ -1698,24 +1747,44 @@ wuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* pb, uint32_t p) {
return ret;
}
// Deprecated: use the second name, not the first.
#define wuffs_base__pixel_buffer__color_u32_at \
wuffs_base__pixel_buffer__color_u32_argb_premul_at
// Deprecated: use the second name, not the first.
#define wuffs_base__pixel_buffer__set_color_u32_at \
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at
// Deprecated: use the second name, not the first.
#define wuffs_base__pixel_buffer__set_color_u32_fill_rect \
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect
WUFFS_BASE__MAYBE_STATIC wuffs_base__color_u32_argb_premul //
wuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y);
wuffs_base__pixel_buffer__color_u32_argb_premul_at(
const wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y);
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_at(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color);
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_fill_rect(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
wuffs_base__pixel_buffer* pb,
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color);
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_argb_nonpremul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color);
WUFFS_BASE__MAYBE_STATIC bool //
wuffs_base__pixel_buffer__is_opaque(const wuffs_base__pixel_buffer* pb);
@@ -1760,7 +1829,13 @@ wuffs_base__pixel_buffer::plane(uint32_t p) {
inline wuffs_base__color_u32_argb_premul //
wuffs_base__pixel_buffer::color_u32_at(uint32_t x, uint32_t y) const {
return wuffs_base__pixel_buffer__color_u32_at(this, x, y);
return wuffs_base__pixel_buffer__color_u32_argb_premul_at(this, x, y);
}
inline wuffs_base__color_u32_argb_premul //
wuffs_base__pixel_buffer::color_u32_argb_premul_at(uint32_t x,
uint32_t y) const {
return wuffs_base__pixel_buffer__color_u32_argb_premul_at(this, x, y);
}
inline wuffs_base__status //
@@ -1768,14 +1843,42 @@ wuffs_base__pixel_buffer::set_color_u32_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color) {
return wuffs_base__pixel_buffer__set_color_u32_at(this, x, y, color);
return wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(this, x, y,
color);
}
inline wuffs_base__status //
wuffs_base__pixel_buffer::set_color_u32_argb_premul_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color) {
return wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(this, x, y,
color);
}
inline wuffs_base__status //
wuffs_base__pixel_buffer::set_color_u32_fill_rect(
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color) {
return wuffs_base__pixel_buffer__set_color_u32_fill_rect(this, rect, color);
return wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
this, rect, color);
}
inline wuffs_base__status //
wuffs_base__pixel_buffer::set_color_u32_argb_premul_fill_rect(
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color) {
return wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
this, rect, color);
}
inline wuffs_base__status //
wuffs_base__pixel_buffer::set_color_u32_argb_nonpremul_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color) {
return wuffs_base__pixel_buffer__set_color_u32_argb_nonpremul_at(this, x, y,
color);
}
inline bool //
+382 -3
View File
@@ -49,6 +49,26 @@ wuffs_private_impl__swizzle_xxxx__y__x86_sse42(uint8_t* dst_ptr,
// --------
static inline uint32_t //
wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(
uint32_t dst_nonpremul,
uint32_t src_nonpremul);
static inline uint64_t //
wuffs_private_impl__composite_nonpremul_nonpremul_u64_axxx(
uint64_t dst_nonpremul,
uint64_t src_nonpremul);
static inline uint32_t //
wuffs_private_impl__composite_premul_nonpremul_u32_axxx(uint32_t dst_premul,
uint32_t src_nonpremul);
static inline uint64_t //
wuffs_private_impl__composite_premul_nonpremul_u64_axxx(uint64_t dst_premul,
uint64_t src_nonpremul);
// --------
static inline uint32_t //
wuffs_private_impl__color_u64__as__color_u32__swap_u32_argb_abgr(uint64_t c) {
uint32_t a = ((uint32_t)(0xFF & (c >> 56)));
@@ -156,7 +176,7 @@ wuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* pb,
// --------
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_at(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
@@ -269,6 +289,365 @@ wuffs_base__pixel_buffer__set_color_u32_at(
return wuffs_base__make_status(NULL);
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_argb_nonpremul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color) {
if (!pb) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if ((x >= pb->pixcfg.private_impl.width) ||
(y >= pb->pixcfg.private_impl.height)) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if (wuffs_base__pixel_format__is_planar(&pb->pixcfg.private_impl.pixfmt)) {
// TODO: support planar formats.
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
size_t stride = pb->private_impl.planes[0].stride;
uint8_t* row = pb->private_impl.planes[0].ptr + (stride * ((size_t)y));
switch (pb->pixcfg.private_impl.pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__BGRX:
wuffs_base__poke_u32le__no_bounds_check(
row + (4 * ((size_t)x)),
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color));
break;
// Common formats above. Rarer formats below.
case WUFFS_BASE__PIXEL_FORMAT__Y:
wuffs_base__poke_u8__no_bounds_check(
row + ((size_t)x),
wuffs_base__color_u32_argb_premul__as__color_u8_gray(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__Y_16LE:
wuffs_base__poke_u16le__no_bounds_check(
row + (2 * ((size_t)x)),
wuffs_base__color_u32_argb_premul__as__color_u16_gray(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__Y_16BE:
wuffs_base__poke_u16be__no_bounds_check(
row + (2 * ((size_t)x)),
wuffs_base__color_u32_argb_premul__as__color_u16_gray(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__YA_NONPREMUL:
wuffs_base__poke_u8__no_bounds_check(
row + (2 * ((size_t)x)),
wuffs_base__color_u32_argb_premul__as__color_u8_gray(color |
0xFF000000));
wuffs_base__poke_u8__no_bounds_check(row + (2 * ((size_t)x)) + 1,
((uint8_t)(color >> 24)));
break;
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY:
wuffs_base__poke_u8__no_bounds_check(
row + ((size_t)x),
wuffs_base__pixel_palette__closest_element(
wuffs_base__pixel_buffer__palette(pb),
pb->pixcfg.private_impl.pixfmt,
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGR_565:
wuffs_base__poke_u16le__no_bounds_check(
row + (2 * ((size_t)x)),
wuffs_base__color_u32_argb_premul__as__color_u16_rgb_565(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGR:
wuffs_base__poke_u24le__no_bounds_check(
row + (3 * ((size_t)x)),
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
wuffs_base__poke_u32le__no_bounds_check(row + (4 * ((size_t)x)), color);
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE:
wuffs_base__poke_u64le__no_bounds_check(
row + (8 * ((size_t)x)), wuffs_base__color_u32__as__color_u64(color));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL_4X16LE:
wuffs_base__poke_u64le__no_bounds_check(
row + (8 * ((size_t)x)),
wuffs_base__color_u64_argb_nonpremul__as__color_u64_argb_premul(
wuffs_base__color_u32__as__color_u64(color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:
wuffs_base__poke_u32le__no_bounds_check(
row + (4 * ((size_t)x)),
(color >> 31)
? (wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color) |
0xFF000000)
: 0);
break;
case WUFFS_BASE__PIXEL_FORMAT__RGB:
wuffs_base__poke_u24le__no_bounds_check(
row + (3 * ((size_t)x)),
wuffs_base__color_swap_u32_argb_abgr(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:
wuffs_base__poke_u32le__no_bounds_check(
row + (4 * ((size_t)x)), wuffs_base__color_swap_u32_argb_abgr(color));
break;
case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__RGBX:
wuffs_base__poke_u32le__no_bounds_check(
row + (4 * ((size_t)x)),
wuffs_base__color_swap_u32_argb_abgr(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
default:
// TODO: support more formats.
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
return wuffs_base__make_status(NULL);
}
static inline wuffs_base__status //
wuffs_base__pixel_buffer__composite_color_u32_nonpremul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color) {
if (!pb) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if ((x >= pb->pixcfg.private_impl.width) ||
(y >= pb->pixcfg.private_impl.height)) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if (wuffs_base__pixel_format__is_planar(&pb->pixcfg.private_impl.pixfmt)) {
// TODO: support planar formats.
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
size_t stride = pb->private_impl.planes[0].stride;
uint8_t* row = pb->private_impl.planes[0].ptr + (stride * ((size_t)y));
switch (pb->pixcfg.private_impl.pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRX: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr) | 0xFF000000;
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
// Common formats above. Rarer formats below.
case WUFFS_BASE__PIXEL_FORMAT__Y: {
uint8_t* ptr = row + ((size_t)x);
uint32_t dst = 0xFF000000 | (0x00010101 * ((uint32_t)(*ptr)));
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
*ptr = ((uint8_t)(dst));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__Y_16LE: {
uint8_t* ptr = row + (2 * ((size_t)x));
uint64_t dst =
0xFFFF000000000000 |
(0x000100010001 *
((uint64_t)(wuffs_base__peek_u16le__no_bounds_check(ptr))));
dst = wuffs_private_impl__composite_premul_nonpremul_u64_axxx(
dst, wuffs_base__color_u32__as__color_u64(color));
wuffs_base__poke_u16le__no_bounds_check(ptr, ((uint16_t)(dst)));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__Y_16BE: {
uint8_t* ptr = row + (2 * ((size_t)x));
uint64_t dst =
0xFFFF000000000000 |
(0x000100010001 *
((uint64_t)(wuffs_base__peek_u16be__no_bounds_check(ptr))));
dst = wuffs_private_impl__composite_premul_nonpremul_u64_axxx(
dst, wuffs_base__color_u32__as__color_u64(color));
wuffs_base__poke_u16be__no_bounds_check(ptr, ((uint16_t)(dst)));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__YA_NONPREMUL: {
uint8_t* ptr = row + (2 * ((size_t)x));
uint32_t dst =
(((uint32_t)(ptr[1])) << 24) | (((uint32_t)(ptr[0])) * 0x00010101);
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(dst,
color);
ptr[0] = wuffs_base__color_u32_argb_premul__as__color_u8_gray(dst |
0xFF000000);
ptr[1] = ((uint8_t)(dst >> 24));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: {
wuffs_base__slice_u8 palette = wuffs_base__pixel_buffer__palette(pb);
if (palette.len) {
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(
palette.ptr + (4 * ((size_t)row[x])));
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(dst,
color);
row[x] = wuffs_base__pixel_palette__closest_element(
wuffs_base__pixel_buffer__palette(pb),
pb->pixcfg.private_impl.pixfmt,
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
dst));
}
break;
}
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: {
wuffs_base__slice_u8 palette = wuffs_base__pixel_buffer__palette(pb);
if (palette.len) {
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(
palette.ptr + (4 * ((size_t)row[x])));
dst =
wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
row[x] = wuffs_base__pixel_palette__closest_element(
wuffs_base__pixel_buffer__palette(pb),
pb->pixcfg.private_impl.pixfmt, dst);
}
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGR_565: {
uint8_t* ptr = row + (2 * ((size_t)x));
uint32_t dst = wuffs_base__color_u16_rgb_565__as__color_u32_argb_premul(
wuffs_base__peek_u16le__no_bounds_check(ptr));
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
wuffs_base__poke_u16le__no_bounds_check(
ptr, wuffs_base__color_u32_argb_premul__as__color_u16_rgb_565(dst));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGR: {
uint8_t* ptr = row + (3 * ((size_t)x));
uint32_t dst = 0xFF000000 | wuffs_base__peek_u24le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
wuffs_base__poke_u24le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(dst,
color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE: {
uint8_t* ptr = row + (8 * ((size_t)x));
uint64_t dst = wuffs_base__peek_u64le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u64_axxx(
dst, wuffs_base__color_u32__as__color_u64(color));
wuffs_base__poke_u64le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL_4X16LE: {
uint8_t* ptr = row + (8 * ((size_t)x));
uint64_t dst = wuffs_base__peek_u64le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u64_axxx(
dst, wuffs_base__color_u32__as__color_u64(color));
wuffs_base__poke_u64le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
if (dst) {
dst =
wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
} else if (color >> 31) {
dst = (wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color) |
0xFF000000);
}
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__RGB: {
uint8_t* ptr = row + (3 * ((size_t)x));
uint32_t dst = 0xFF000000 | wuffs_base__peek_u24le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(
dst, wuffs_base__color_swap_u32_argb_abgr(color));
wuffs_base__poke_u24le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(
dst, wuffs_base__color_swap_u32_argb_abgr(color));
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(
dst, wuffs_base__color_swap_u32_argb_abgr(color));
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__RGBX: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr) | 0xFF000000;
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(
dst, wuffs_base__color_swap_u32_argb_abgr(color));
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
default:
// TODO: support more formats.
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
return wuffs_base__make_status(NULL);
}
// --------
static inline void //
@@ -396,7 +775,7 @@ wuffs_private_impl__pixel_buffer__set_color_u32_fill_rect__xxxxxxxx(
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_fill_rect(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
wuffs_base__pixel_buffer* pb,
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color) {
@@ -468,7 +847,7 @@ wuffs_base__pixel_buffer__set_color_u32_fill_rect(
for (y = rect.min_incl_y; y < rect.max_excl_y; y++) {
uint32_t x;
for (x = rect.min_incl_x; x < rect.max_excl_x; x++) {
wuffs_base__pixel_buffer__set_color_u32_at(pb, x, y, color);
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(pb, x, y, color);
}
}
return wuffs_base__make_status(NULL);
+13 -11
View File
@@ -445,10 +445,12 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
bool last_column,
uint8_t odd_column_rounding) {
uint8_t* dp = dst_ptr;
const uint8_t* sp_major = src_ptr_major;
const uint8_t* sp_minor = src_ptr_minor;
const uint8_t oc = odd_column_rounding;
if (first_column) {
src_len--;
@@ -456,7 +458,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
uint32_t sv = (12u * ((uint32_t)(*sp_major++))) + //
(4u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + 8u) >> 4u);
*dp++ = (uint8_t)((sv + 7u) >> 4u);
*dp++ = (uint8_t)((sv + oc) >> 4u);
return dst_ptr;
}
@@ -468,7 +470,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
if (src_len <= 0u) {
return dst_ptr;
}
@@ -489,7 +491,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
}
} else {
@@ -562,8 +564,8 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
// the u16 right value by 4. On the right (p1), shift right by 4 and then
// shift left by 8 so that, when still in the u16x16 little-endian
// interpretation, we have:
// - m1_element = (etcetera + 8) >> 4
// - p1_element = ((etcetera + 7) >> 4) << 8
// - m1_element = (etcetera + 8) >> 4
// - p1_element = ((etcetera + oc) >> 4) << 8
//
// step4_m1_lo = [0x00?? 0x00?? ... 0x00?? 0x00??]
// step4_p1_lo = [0x??00 0x??00 ... 0x??00 0x??00]
@@ -572,14 +574,14 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
__m256i step4_m1_lo = _mm256_srli_epi16(
_mm256_add_epi16(step3_m1_lo, _mm256_set1_epi16(8)), 4);
__m256i step4_p1_lo = _mm256_slli_epi16(
_mm256_srli_epi16(_mm256_add_epi16(step3_p1_lo, _mm256_set1_epi16(7)),
4),
_mm256_srli_epi16(
_mm256_add_epi16(step3_p1_lo, _mm256_set1_epi16(oc)), 4),
8);
__m256i step4_m1_hi = _mm256_srli_epi16(
_mm256_add_epi16(step3_m1_hi, _mm256_set1_epi16(8)), 4);
__m256i step4_p1_hi = _mm256_slli_epi16(
_mm256_srli_epi16(_mm256_add_epi16(step3_p1_hi, _mm256_set1_epi16(7)),
4),
_mm256_srli_epi16(
_mm256_add_epi16(step3_p1_hi, _mm256_set1_epi16(oc)), 4),
8);
// Bitwise-or two "0x00"-rich u16x16 vectors to get a u8x32 vector. Do
@@ -630,7 +632,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
}
return dst_ptr;
+214 -28
View File
@@ -45,7 +45,36 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column);
bool last_column,
uint8_t odd_column_rounding);
static const uint8_t* //
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libjpeg_x86_avx2(
uint8_t* dst_ptr,
const uint8_t* src_ptr_major,
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
return wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
dst_ptr, src_ptr_major, src_ptr_minor, src_len, h1v2_bias_ignored,
first_column, last_column, 7);
}
static const uint8_t* //
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libwebp_x86_avx2(
uint8_t* dst_ptr,
const uint8_t* src_ptr_major,
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
return wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
dst_ptr, src_ptr_major, src_ptr_minor, src_len, h1v2_bias_ignored,
first_column, last_column, 8);
}
#endif
#endif // defined(WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V3)
@@ -86,6 +115,102 @@ typedef void (*wuffs_private_impl__swizzle_ycc__convert_4_func)(
const uint8_t* up2,
const uint8_t* up3);
static void //
wuffs_private_impl__swizzle_ycca_bt601sr_src__convert_4_general(
wuffs_base__pixel_buffer* dst,
uint32_t x,
uint32_t x_end,
uint32_t y,
const uint8_t* up0,
const uint8_t* up1,
const uint8_t* up2,
const uint8_t* up3) {
for (; x < x_end; x++) {
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
color &= 0xFFFFFFu;
color |= ((uint32_t)(*up3++)) << 24;
wuffs_base__pixel_buffer__set_color_u32_argb_nonpremul_at(dst, x, y, color);
}
}
static void //
wuffs_private_impl__swizzle_ycca_bt601sr_src__convert_4_bgra_premul(
wuffs_base__pixel_buffer* dst,
uint32_t x,
uint32_t x_end,
uint32_t y,
const uint8_t* up0,
const uint8_t* up1,
const uint8_t* up2,
const uint8_t* up3) {
size_t stride = dst->private_impl.planes[0].stride;
uint8_t* row = dst->private_impl.planes[0].ptr + (stride * ((size_t)y));
for (; x < x_end; x++) {
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
color &= 0xFFFFFFu;
color |= ((uint32_t)(*up3++)) << 24;
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst_color =
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst_color);
}
}
static void //
wuffs_private_impl__swizzle_ycca_bt601sr_src_over__convert_4_general(
wuffs_base__pixel_buffer* dst,
uint32_t x,
uint32_t x_end,
uint32_t y,
const uint8_t* up0,
const uint8_t* up1,
const uint8_t* up2,
const uint8_t* up3) {
for (; x < x_end; x++) {
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
color &= 0xFFFFFFu;
color |= ((uint32_t)(*up3++)) << 24;
wuffs_base__pixel_buffer__composite_color_u32_nonpremul_at(dst, x, y,
color);
}
}
static void //
wuffs_private_impl__swizzle_ycca_bt601sr_src_over__convert_4_bgra_premul(
wuffs_base__pixel_buffer* dst,
uint32_t x,
uint32_t x_end,
uint32_t y,
const uint8_t* up0,
const uint8_t* up1,
const uint8_t* up2,
const uint8_t* up3) {
size_t stride = dst->private_impl.planes[0].stride;
uint8_t* row = dst->private_impl.planes[0].ptr + (stride * ((size_t)y));
for (; x < x_end; x++) {
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
color &= 0xFFFFFFu;
color |= ((uint32_t)(*up3++)) << 24;
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst_color = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst_color = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(
dst_color, color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst_color);
}
}
static void //
wuffs_private_impl__swizzle_cmyk__convert_4_general(
wuffs_base__pixel_buffer* dst,
@@ -107,7 +232,7 @@ wuffs_private_impl__swizzle_cmyk__convert_4_general(
r = ((r * w) + 0x7Fu) / 0xFFu;
g = ((g * w) + 0x7Fu) / 0xFFu;
b = ((b * w) + 0x7Fu) / 0xFFu;
wuffs_base__pixel_buffer__set_color_u32_at(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
dst, x, y, 0xFF000000u | (r << 16u) | (g << 8u) | (b << 0u));
}
}
@@ -134,7 +259,7 @@ wuffs_private_impl__swizzle_ycck__convert_4_general(
r = ((r * w) + 0x7Fu) / 0xFFu;
g = ((g * w) + 0x7Fu) / 0xFFu;
b = ((b * w) + 0x7Fu) / 0xFFu;
wuffs_base__pixel_buffer__set_color_u32_at(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
dst, x, y, 0xFF000000u | (r << 16u) | (g << 8u) | (b << 0u));
}
}
@@ -164,7 +289,7 @@ wuffs_private_impl__swizzle_rgb__convert_3_general(
(((uint32_t)(*up0++)) << 16u) | //
(((uint32_t)(*up1++)) << 8u) | //
(((uint32_t)(*up2++)) << 0u);
wuffs_base__pixel_buffer__set_color_u32_at(dst, x, y, color);
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(dst, x, y, color);
}
}
@@ -181,7 +306,7 @@ wuffs_private_impl__swizzle_ycc_bt601fr__convert_3_general(
uint32_t color = //
wuffs_base__color_ycc_bt601fr__as__color_u32( //
*up0++, *up1++, *up2++);
wuffs_base__pixel_buffer__set_color_u32_at(dst, x, y, color);
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(dst, x, y, color);
}
}
@@ -242,7 +367,7 @@ wuffs_private_impl__swizzle_ycc_bt601sr__convert_3_general(
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
wuffs_base__pixel_buffer__set_color_u32_at(dst, x, y, color);
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(dst, x, y, color);
}
}
@@ -404,8 +529,8 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h1v2_triangle(
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias,
bool first_column,
bool last_column) {
bool first_column_ignored,
bool last_column_ignored) {
uint8_t* dp = dst_ptr;
const uint8_t* sp_major = src_ptr_major;
const uint8_t* sp_minor = src_ptr_minor;
@@ -477,10 +602,12 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
bool last_column,
uint8_t odd_column_rounding) {
uint8_t* dp = dst_ptr;
const uint8_t* sp_major = src_ptr_major;
const uint8_t* sp_minor = src_ptr_minor;
const uint8_t oc = odd_column_rounding;
if (first_column) {
src_len--;
@@ -488,7 +615,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
uint32_t sv = (12u * ((uint32_t)(*sp_major++))) + //
(4u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + 8u) >> 4u);
*dp++ = (uint8_t)((sv + 7u) >> 4u);
*dp++ = (uint8_t)((sv + oc) >> 4u);
return dst_ptr;
}
@@ -500,7 +627,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
if (src_len <= 0u) {
return dst_ptr;
}
@@ -519,7 +646,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
}
if (last_column) {
@@ -531,12 +658,40 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
}
return dst_ptr;
}
static const uint8_t* //
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libjpeg(
uint8_t* dst_ptr,
const uint8_t* src_ptr_major,
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
return wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
dst_ptr, src_ptr_major, src_ptr_minor, src_len, h1v2_bias_ignored,
first_column, last_column, 7);
}
static const uint8_t* //
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libwebp(
uint8_t* dst_ptr,
const uint8_t* src_ptr_major,
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
return wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
dst_ptr, src_ptr_major, src_ptr_minor, src_len, h1v2_bias_ignored,
first_column, last_column, 8);
}
// wuffs_private_impl__swizzle_ycc__upsample_funcs is indexed by inv_h and then
// inv_v.
static const wuffs_private_impl__swizzle_ycc__upsample_func
@@ -1288,7 +1443,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
uint8_t v2,
uint8_t v3,
uint8_t ycc_model,
bool triangle_filter_for_2to1,
uint8_t ycc_upsampling,
wuffs_base__slice_u8 scratch_buffer_2k) {
if (!p) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
@@ -1303,7 +1458,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
(4u <= ((unsigned int)v0 - 1u)) || //
(4u <= ((unsigned int)v1 - 1u)) || //
(4u <= ((unsigned int)v2 - 1u)) || //
(triangle_filter_for_2to1 && ((x_min_incl | y_min_incl) > 0u)) ||
((ycc_upsampling != 0) && ((x_min_incl | y_min_incl) > 0u)) ||
(scratch_buffer_2k.len < 2048u)) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
@@ -1418,7 +1573,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
}
if (wuffs_base__pixel_format__is_planar(&dst->pixcfg.private_impl.pixfmt)) {
// TODO: see wuffs_base__pixel_buffer__set_color_u32_at's TODO.
// TODO: see wuffs_base__pixel_buffer__set_color_u32_argb_premul_at's TODO.
return wuffs_base__make_status(
wuffs_base__error__unsupported_pixel_swizzler_option);
}
@@ -1493,7 +1648,8 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
break;
default:
// TODO: see wuffs_base__pixel_buffer__set_color_u32_at's TODO.
// TODO: see wuffs_base__pixel_buffer__set_color_u32_argb_premul_at's
// TODO.
return wuffs_base__make_status(
wuffs_base__error__unsupported_pixel_swizzler_option);
}
@@ -1506,7 +1662,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
if (ycc_model >= WUFFS_BASE__YCC_MODEL__RGB) {
conv3func = &wuffs_private_impl__swizzle_rgb__convert_3_general;
} else if (ycc_model == WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE) {
} else if (ycc_model >= WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE) {
switch (dst->pixcfg.private_impl.pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:
@@ -1611,7 +1767,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
memcpy(&upfuncs, &wuffs_private_impl__swizzle_ycc__upsample_funcs,
sizeof upfuncs);
if (triangle_filter_for_2to1 &&
if ((ycc_upsampling != 0) &&
(wuffs_private_impl__swizzle_has_triangle_upsampler(inv_h0, inv_v0) ||
wuffs_private_impl__swizzle_has_triangle_upsampler(inv_h1, inv_v1) ||
wuffs_private_impl__swizzle_has_triangle_upsampler(inv_h2, inv_v2) ||
@@ -1621,7 +1777,10 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
upfuncs[0][1] = wuffs_private_impl__swizzle_ycc__upsample_inv_h1v2_triangle;
upfuncs[1][0] = wuffs_private_impl__swizzle_ycc__upsample_inv_h2v1_triangle;
upfuncs[1][1] = wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle;
upfuncs[1][1] =
(ycc_upsampling == 1)
? wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libjpeg
: wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libwebp;
#if defined(WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V3)
#if defined(__GNUC__) && !defined(__clang__)
@@ -1639,21 +1798,48 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
#else
if (wuffs_base__cpu_arch__have_x86_avx2()) {
upfuncs[1][1] =
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2;
(ycc_upsampling == 1)
? wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libjpeg_x86_avx2
: wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libwebp_x86_avx2;
}
#endif
#endif
}
if ((h3 != 0u) || (v3 != 0u)) {
if (ycc_model == WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE) {
return wuffs_base__make_status(
wuffs_base__error__unsupported_pixel_swizzler_option);
wuffs_private_impl__swizzle_ycc__convert_4_func conv4func = NULL;
switch (ycc_model) {
case WUFFS_BASE__YCC_MODEL__BT_601_FULL_RANGE:
conv4func = &wuffs_private_impl__swizzle_ycck__convert_4_general;
break;
case WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC:
if (dst->pixcfg.private_impl.pixfmt.repr ==
WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL) {
conv4func =
&wuffs_private_impl__swizzle_ycca_bt601sr_src__convert_4_bgra_premul;
} else {
conv4func =
&wuffs_private_impl__swizzle_ycca_bt601sr_src__convert_4_general;
}
break;
case WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC_OVER:
if (dst->pixcfg.private_impl.pixfmt.repr ==
WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL) {
conv4func =
&wuffs_private_impl__swizzle_ycca_bt601sr_src_over__convert_4_bgra_premul;
} else {
conv4func =
&wuffs_private_impl__swizzle_ycca_bt601sr_src_over__convert_4_general;
}
break;
case WUFFS_BASE__YCC_MODEL__CMY:
conv4func = &wuffs_private_impl__swizzle_cmyk__convert_4_general;
break;
default:
return wuffs_base__make_status(
wuffs_base__error__unsupported_pixel_swizzler_option);
}
wuffs_private_impl__swizzle_ycc__convert_4_func conv4func =
(ycc_model >= WUFFS_BASE__YCC_MODEL__RGB)
? &wuffs_private_impl__swizzle_cmyk__convert_4_general
: &wuffs_private_impl__swizzle_ycck__convert_4_general;
(*func4)( //
dst, x_min_incl, x_max_excl, y_min_incl, y_max_excl, //
src0.ptr, src1.ptr, src2.ptr, src3.ptr, //
+6 -1
View File
@@ -230,7 +230,12 @@ wuffs_drop_in__stb__load1( //
}
uint64_t pixbuf_len = (uint64_t)w * (uint64_t)h * (uint64_t)desired_channels;
uint64_t workbuf_len = wuffs_base__image_decoder__workbuf_len(dec).max_incl;
wuffs_base__range_ii_u64 r = wuffs_base__image_decoder__workbuf_len(dec);
if (wuffs_base__range_ii_u64__is_empty(&r)) {
wuffs_drop_in__stb__g_failure_reason = "indeterminate workbuf length";
return NULL;
}
uint64_t workbuf_len = r.max_incl;
#if SIZE_MAX < 0xFFFFFFFFFFFFFFFFull
if ((pixbuf_len > ((uint64_t)SIZE_MAX)) ||
(workbuf_len > ((uint64_t)SIZE_MAX))) {
+9 -1
View File
@@ -551,8 +551,16 @@ func (g *gen) writeExprUserDefinedCall(b *buffer, n *a.Expr, depth uint32) error
return fmt.Errorf("cannot generate user-defined method call %q for receiver type %q",
n.Str(g.tm), recv.MType().Str(g.tm))
}
qid := recvTyp.QID()
b.printf("%s%s__%s(", g.packagePrefix(qid), qid[1].Str(g.tm), method.Ident().Str(g.tm))
pkgStr := g.packagePrefix(qid)
typStr := qid[1].Str(g.tm)
methodStr := method.Ident().Str(g.tm)
if (qid[0] == t.IDBase) && qid[1].IsRangeType() && strings.HasPrefix(methodStr, "get_") {
pkgStr = "wuffs_private_impl__"
}
b.printf("%s%s__%s(", pkgStr, typStr, methodStr)
if !recvTyp.IsEtcUtilityType() {
b.writes(addr)
if err := g.writeExpr(b, recv, false, depth); err != nil {
+21 -2
View File
@@ -115,6 +115,11 @@ var Consts = [...]struct {
// ----
{t.IDU8, "0", "PIXEL_BLEND__SRC"},
{t.IDU8, "1", "PIXEL_BLEND__SRC_OVER"},
// ----
{t.IDU32, "0x02000008", "PIXEL_FORMAT__A"},
{t.IDU32, "0x20000008", "PIXEL_FORMAT__Y"},
@@ -245,9 +250,17 @@ var Consts = [...]struct {
// ----
{t.IDU32, "0x00", "YCC_MODEL__BT_601_FULL_RANGE"},
{t.IDU32, "0x01", "YCC_MODEL__BT_601_STUDIO_RANGE"},
{t.IDU32, "0x04", "YCC_MODEL__BT_601_STUDIO_RANGE"},
{t.IDU32, "0x06", "YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC"},
{t.IDU32, "0x07", "YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC_OVER"},
{t.IDU32, "0x40", "YCC_MODEL__RGB"},
{t.IDU32, "0x80", "YCC_MODEL__CMY"},
// ----
{t.IDU32, "0x00", "YCC_UPSAMPLING__BASIC"},
{t.IDU32, "0x01", "YCC_UPSAMPLING__FANCY_LIKE_LIBJPEG"},
{t.IDU32, "0x02", "YCC_UPSAMPLING__FANCY_LIKE_LIBWEBP"},
}
var Statuses = [...]string{
@@ -390,6 +403,7 @@ var funcsOther = [...]string{
// ---- utility
"utility.cpu_arch_is_32_bit() bool",
"utility.demake_pixel_blend(a: pixel_blend) u8",
"utility.empty_io_reader() io_reader",
"utility.empty_io_writer() io_writer",
"utility.empty_range_ii_u32() range_ii_u32",
@@ -402,6 +416,7 @@ var funcsOther = [...]string{
"utility.i64_divide(a: u64, b: u64[1 ..=]) u64",
"utility.make_bitvec256(e00: u64, e01: u64, e02: u64, e03: u64) bitvec256",
"utility.make_optional_u63(has_value: bool, value: u64[..= 0x7FFF_FFFF_FFFF_FFFF]) optional_u63",
"utility.make_pixel_blend(repr: u8) pixel_blend",
"utility.make_pixel_format(repr: u32) pixel_format",
"utility.make_range_ii_u32(min_incl: u32, max_incl: u32) range_ii_u32",
"utility.make_range_ie_u32(min_incl: u32, max_excl: u32) range_ie_u32",
@@ -425,21 +440,25 @@ var funcsOther = [...]string{
"range_ie_u32.get_min_incl() u32",
"range_ie_u32.get_max_excl() u32",
"range_ie_u32.intersect(r: range_ie_u32) range_ie_u32",
"range_ie_u32.is_empty() bool",
"range_ie_u32.unite(r: range_ie_u32) range_ie_u32",
"range_ii_u32.get_min_incl() u32",
"range_ii_u32.get_max_incl() u32",
"range_ii_u32.intersect(r: range_ii_u32) range_ii_u32",
"range_ii_u32.is_empty() bool",
"range_ii_u32.unite(r: range_ii_u32) range_ii_u32",
"range_ie_u64.get_min_incl() u64",
"range_ie_u64.get_max_excl() u64",
"range_ie_u64.intersect(r: range_ie_u64) range_ie_u64",
"range_ie_u64.is_empty() bool",
"range_ie_u64.unite(r: range_ie_u64) range_ie_u64",
"range_ii_u64.get_min_incl() u64",
"range_ii_u64.get_max_incl() u64",
"range_ii_u64.intersect(r: range_ii_u64) range_ii_u64",
"range_ii_u64.is_empty() bool",
"range_ii_u64.unite(r: range_ii_u64) range_ii_u64",
// ---- more_information
@@ -742,7 +761,7 @@ var funcsOther = [...]string{
"v2: u8[..= 4]," +
"v3: u8[..= 4]," +
"ycc_model: u8," +
"triangle_filter_for_2to1: bool," +
"ycc_upsampling: u8," +
"scratch_buffer_2k: slice u8) status",
// ---- arm_crc32_utility
+71
View File
@@ -0,0 +1,71 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
package handsum_test
import (
"bytes"
"encoding/base64"
"fmt"
"image/png"
"log"
"os"
"github.com/google/wuffs/lib/handsum"
)
func Example_decode() {
srcBytes, err := base64.URLEncoding.DecodeString("" +
"_tdrdX33KcrhdVU1hFi3h2mtY0qqd2dswzeX9jmJckmUxD15c1Vqk1iGdjmYoknlZDl5dmmZ" +
"xEczhliahXRDpC3JFoiItjAtQ2vJpjBsUziLiXivgimZqDq6skhwhkxIsimmragKTLa-_Hmn" +
"C5rf_acAvXR4zebYyq-Qc2SVtVnIA_mX9zVlEmn_Ul9pIzwHaFBb")
if err != nil {
log.Fatalf("base64.URLEncoding.DecodeString: %v", err)
}
img, err := handsum.Decode(bytes.NewReader(srcBytes))
if err != nil {
log.Fatalf("handsum.Decode: %v", err)
}
fmt.Printf("%T %v\n", img, img.Bounds())
// Output:
// *image.RGBA (0,0)-(16,12)
}
func Example_encode() {
f, err := os.Open("../../test/data/bricks-color.png")
if err != nil {
log.Fatalf("os.Open: %v", err)
}
defer f.Close()
srcImage, err := png.Decode(f)
if err != nil {
log.Fatalf("png.Decode: %v", err)
}
buf := &bytes.Buffer{}
err = handsum.Encode(buf, srcImage, nil)
if err != nil {
log.Fatalf("handsum.Encode: %v", err)
}
s := base64.URLEncoding.EncodeToString(buf.Bytes())
for ; len(s) > 72; s = s[72:] {
fmt.Println(s[:72])
}
fmt.Println(s)
// Output:
// _tdrdX33KcrhdVU1hFi3h2mtY0qqd2dswzeX9jmJckmUxD15c1Vqk1iGdjmYoknlZDl5dmmZ
// xEczhliahXRDpC3JFoiItjAtQ2vJpjBsUziLiXivgimZqDq6skhwhkxIsimmragKTLa-_Hmn
// C5rf_acAvXR4zebYyq-Qc2SVtVnIA_mX9zVlEmn_Ul9pIzwHaFBb
}
+66 -67
View File
@@ -47,9 +47,9 @@
// 4.59375 bits per pixel for a 16×16 pixel image (a 1:1 aspect ratio), or
// 6.125 bits per pixel for a 16×12 pixel image (a 4:3 aspect ratio).
//
// A Handsum file's starts with a 3 byte header: a 15-bit magic signature, a
// 2-bit color (Gray, RGB or RGBA), a 2-bit quality and a 5-bit aspect ratio.
// An image's longest dimension (width or height) is 16 pixels and the aspect
// Handsum files start with a 3 byte header: a 15-bit magic signature, a 2-bit
// color (Gray, RGB or RGBA), a 2-bit quality and a 5-bit aspect ratio. An
// image's longest dimension (width or height) is 16 pixels and the aspect
// ratio gives the shorter dimension.
//
// The color=RGB payload, after the header, holds a scaled 16×16 pixel YCbCr
@@ -79,10 +79,11 @@
//
// All Handsum images use the sRGB color profile and non-premultiplied alpha.
//
// The "Handsum" name was inspired by the "Thumbhash" image file format, which
// is also designed for very small thumbnails (or very compact representations
// of image placeholders). Handsum files are bigger (but better quality) than
// Thumbhash. "Handsum" also sounds like "handsome", meaning "good looking".
// The "Handsum" name was inspired by the "Thumbhash" image file format (which
// was in turn inspired by "Blurhash"), which is also designed for very small
// thumbnails (or very compact representations of image placeholders). Handsum
// files are bigger (but better quality) than Thumbhash (and Blurhash).
// "Handsum" also sounds like "handsome", meaning "good looking".
//
// Other techniques and image formats, similar to Thumbhash, can be found by
// search for "LQIP" or "Low Quality Image Placeholders".
@@ -336,7 +337,7 @@ func Encode(w io.Writer, src image.Image, options *EncodeOptions) error {
if c >= ColorRGBA {
alphasBlock := lowleveljpeg.BlockU8{}
alphasBlock.DownsampleFrom(&alphasQuadBlock)
bitOffset = encodeBlockQ3(&buf, bitOffset, &alphasBlock, QualityBest.numberOfCoefficients())
bitOffset = encodeBlockQ4(&buf, bitOffset, &alphasBlock, QualityBest.numberOfCoefficients())
}
_, err := w.Write(buf[:bitOffset/8])
@@ -374,13 +375,13 @@ func scaleSrc(src image.Image, alphasQuadBlock *lowleveljpeg.QuadBlockU8) image.
type encodeBlockFunc func(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.BlockU8, nCoeffs int) int
var encodeBlockFuncs = [4]encodeBlockFunc{
encodeBlockQ0,
encodeBlockQ1,
encodeBlockQ1,
encodeBlockQ3,
encodeBlockQ2,
encodeBlockQ2,
encodeBlockQ4,
}
func encodeBlockQ0(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.BlockU8, nCoeffs int) int {
func encodeBlockQ1(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.BlockU8, nCoeffs int) int {
f8 := src.ForwardDCT()
for _, z := range zigzag8 {
@@ -397,7 +398,7 @@ func encodeBlockQ0(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.Bloc
return bitOffset
}
func encodeBlockQ1(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.BlockU8, nCoeffs int) int {
func encodeBlockQ2(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.BlockU8, nCoeffs int) int {
for i := range 4 {
x4 := (i & 1) << 2 // The sequence 0, 4, 0, 4.
y4 := (i & 2) << 1 // The sequence 0, 0, 4, 4.
@@ -421,7 +422,7 @@ func encodeBlockQ1(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.Bloc
return bitOffset
}
func encodeBlockQ3(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.BlockU8, nCoeffs int) int {
func encodeBlockQ4(buf *[fileSizeMax]byte, bitOffset int, src *lowleveljpeg.BlockU8, nCoeffs int) int {
// We could import the lowleveldct2x2 package and call its ForwardDCT
// method (and InverseDCTFrom on decode), but it's easy to just inline it.
@@ -549,7 +550,7 @@ func Decode(r io.Reader) (image.Image, error) {
if c >= ColorRGBA {
aaBlockU8 := lowleveljpeg.BlockU8{}
bitOffset = decodeBlockQ3(aaBlockU8[:], 8, &buf, bitOffset, QualityBest.numberOfCoefficients())
bitOffset = decodeBlockQ4(aaBlockU8[:], 8, &buf, bitOffset, QualityBest.numberOfCoefficients())
aaQuadBlockU8.UpsampleFrom(&aaBlockU8)
for i, v := range aaQuadBlockU8 {
aaQuadBlockU8[i] = cubeRootLUT[v]
@@ -576,13 +577,13 @@ func decodeWidthAndHeight(buf2 byte) (w int, h int, ok bool) {
type decodeBlockFunc func(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int, nCoeffs int) int
var decodeBlockFuncs = [4]decodeBlockFunc{
decodeBlockQ0,
decodeBlockQ1,
decodeBlockQ1,
decodeBlockQ3,
decodeBlockQ2,
decodeBlockQ2,
decodeBlockQ4,
}
func decodeBlockQ0(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int, nCoeffs int) int {
func decodeBlockQ1(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int, nCoeffs int) int {
a := lowleveljpeg.BlockI16{}
for _, z := range zigzag8 {
@@ -607,7 +608,7 @@ func decodeBlockQ0(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int
return bitOffset
}
func decodeBlockQ1(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int, nCoeffs int) int {
func decodeBlockQ2(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int, nCoeffs int) int {
tmp := lowleveljpeg.BlockU8{}
for i := range 4 {
@@ -636,7 +637,7 @@ func decodeBlockQ1(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int
}
}
smoothBlockSeams8x8Q1(&tmp)
smoothBlockSeams8x8Q2(&tmp)
for i := range 8 {
di := i * stride
@@ -647,7 +648,7 @@ func decodeBlockQ1(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int
return bitOffset
}
func decodeBlockQ3(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int, nCoeffs int) int {
func decodeBlockQ4(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int, nCoeffs int) int {
tmp := lowleveljpeg.BlockU8{}
for i := range 16 {
@@ -673,7 +674,7 @@ func decodeBlockQ3(dst []byte, stride int, buf *[fileSizeMax]byte, bitOffset int
tmp[o2+9] = biasAndClamp[((+dct0-dct1-dct2+1)>>1)&1023]
}
smoothBlockSeams8x8Q3(&tmp)
smoothBlockSeams8x8Q4(&tmp)
for i := range 8 {
di := i * stride
@@ -910,7 +911,7 @@ func smoothBlockSeams16x16(b *lowleveljpeg.QuadBlockU8) {
b[0x87] = uint8(((9 * v87) + (3 * v77) + v78 + (3 * v88) + 8) / 16)
}
func smoothBlockSeams8x8Q1(b *lowleveljpeg.BlockU8) {
func smoothBlockSeams8x8Q2(b *lowleveljpeg.BlockU8) {
for _, pair := range smoothingPairs8x8 {
v0 := uint32(b[pair[0]])
v1 := uint32(b[pair[1]])
@@ -929,7 +930,7 @@ func smoothBlockSeams8x8Q1(b *lowleveljpeg.BlockU8) {
b[0o43] = uint8(((9 * v43) + (3 * v33) + v34 + (3 * v44) + 8) / 16)
}
func smoothBlockSeams8x8Q3(b *lowleveljpeg.BlockU8) {
func smoothBlockSeams8x8Q4(b *lowleveljpeg.BlockU8) {
for y := 1; y < 7; y += 2 {
for x := 1; x < 7; x += 2 {
o := (y * 8) + x
@@ -1033,58 +1034,56 @@ var smoothingPairs8x8 = [12][2]uint8{
{0o73, 0o74},
}
// scaleAndBiasChromaUp is equivalent to this [256]uint8 look-up table.
//
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04, 0x06,
// 0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24, 0x26,
// 0x28, 0x2A, 0x2C, 0x2E, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3A, 0x3C, 0x3E, 0x40, 0x42, 0x44, 0x46,
// 0x48, 0x4A, 0x4C, 0x4E, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5A, 0x5C, 0x5E, 0x60, 0x62, 0x64, 0x66,
// 0x68, 0x6A, 0x6C, 0x6E, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7A, 0x7C, 0x7E, 0x80, 0x82, 0x84, 0x86,
//
// 0x88, 0x8A, 0x8C, 0x8E, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9A, 0x9C, 0x9E, 0xA0, 0xA2, 0xA4, 0xA6,
// 0xA8, 0xAA, 0xAC, 0xAE, 0xB0, 0xB2, 0xB4, 0xB6, 0xB8, 0xBA, 0xBC, 0xBE, 0xC0, 0xC2, 0xC4, 0xC6,
// 0xC8, 0xCA, 0xCC, 0xCE, 0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6,
// 0xE8, 0xEA, 0xEC, 0xEE, 0xF0, 0xF2, 0xF4, 0xF6, 0xF8, 0xFA, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
// 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
// 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
// 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
// 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
func scaleAndBiasChromaUp(b *lowleveljpeg.BlockU8) {
for i, v := range b {
b[i] = scaleAndBiasChromaUpLUT[v]
b[i] = uint8(max(0x00, min(0xFF, ((int(v)*2)-0x78))))
}
}
// scaleAndBiasChromaDown is equivalent to this [256]uint8 look-up table.
//
// 0x3C, 0x3C, 0x3D, 0x3D, 0x3E, 0x3E, 0x3F, 0x3F, 0x40, 0x40, 0x41, 0x41, 0x42, 0x42, 0x43, 0x43,
// 0x44, 0x44, 0x45, 0x45, 0x46, 0x46, 0x47, 0x47, 0x48, 0x48, 0x49, 0x49, 0x4A, 0x4A, 0x4B, 0x4B,
// 0x4C, 0x4C, 0x4D, 0x4D, 0x4E, 0x4E, 0x4F, 0x4F, 0x50, 0x50, 0x51, 0x51, 0x52, 0x52, 0x53, 0x53,
// 0x54, 0x54, 0x55, 0x55, 0x56, 0x56, 0x57, 0x57, 0x58, 0x58, 0x59, 0x59, 0x5A, 0x5A, 0x5B, 0x5B,
// 0x5C, 0x5C, 0x5D, 0x5D, 0x5E, 0x5E, 0x5F, 0x5F, 0x60, 0x60, 0x61, 0x61, 0x62, 0x62, 0x63, 0x63,
// 0x64, 0x64, 0x65, 0x65, 0x66, 0x66, 0x67, 0x67, 0x68, 0x68, 0x69, 0x69, 0x6A, 0x6A, 0x6B, 0x6B,
// 0x6C, 0x6C, 0x6D, 0x6D, 0x6E, 0x6E, 0x6F, 0x6F, 0x70, 0x70, 0x71, 0x71, 0x72, 0x72, 0x73, 0x73,
// 0x74, 0x74, 0x75, 0x75, 0x76, 0x76, 0x77, 0x77, 0x78, 0x78, 0x79, 0x79, 0x7A, 0x7A, 0x7B, 0x7B,
//
// 0x7C, 0x7C, 0x7D, 0x7D, 0x7E, 0x7E, 0x7F, 0x7F, 0x80, 0x80, 0x81, 0x81, 0x82, 0x82, 0x83, 0x83,
// 0x84, 0x84, 0x85, 0x85, 0x86, 0x86, 0x87, 0x87, 0x88, 0x88, 0x89, 0x89, 0x8A, 0x8A, 0x8B, 0x8B,
// 0x8C, 0x8C, 0x8D, 0x8D, 0x8E, 0x8E, 0x8F, 0x8F, 0x90, 0x90, 0x91, 0x91, 0x92, 0x92, 0x93, 0x93,
// 0x94, 0x94, 0x95, 0x95, 0x96, 0x96, 0x97, 0x97, 0x98, 0x98, 0x99, 0x99, 0x9A, 0x9A, 0x9B, 0x9B,
// 0x9C, 0x9C, 0x9D, 0x9D, 0x9E, 0x9E, 0x9F, 0x9F, 0xA0, 0xA0, 0xA1, 0xA1, 0xA2, 0xA2, 0xA3, 0xA3,
// 0xA4, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0xA7, 0xA7, 0xA8, 0xA8, 0xA9, 0xA9, 0xAA, 0xAA, 0xAB, 0xAB,
// 0xAC, 0xAC, 0xAD, 0xAD, 0xAE, 0xAE, 0xAF, 0xAF, 0xB0, 0xB0, 0xB1, 0xB1, 0xB2, 0xB2, 0xB3, 0xB3,
// 0xB4, 0xB4, 0xB5, 0xB5, 0xB6, 0xB6, 0xB7, 0xB7, 0xB8, 0xB8, 0xB9, 0xB9, 0xBA, 0xBA, 0xBB, 0xBB,
func scaleAndBiasChromaDown(b *lowleveljpeg.BlockU8) {
for i, v := range b {
b[i] = scaleAndBiasChromaDownLUT[v]
b[i] = (v >> 1) + 0x3C
}
}
var scaleAndBiasChromaUpLUT = [256]uint8{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04, 0x06,
0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24, 0x26,
0x28, 0x2A, 0x2C, 0x2E, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3A, 0x3C, 0x3E, 0x40, 0x42, 0x44, 0x46,
0x48, 0x4A, 0x4C, 0x4E, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5A, 0x5C, 0x5E, 0x60, 0x62, 0x64, 0x66,
0x68, 0x6A, 0x6C, 0x6E, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7A, 0x7C, 0x7E, 0x80, 0x82, 0x84, 0x86,
0x88, 0x8A, 0x8C, 0x8E, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9A, 0x9C, 0x9E, 0xA0, 0xA2, 0xA4, 0xA6,
0xA8, 0xAA, 0xAC, 0xAE, 0xB0, 0xB2, 0xB4, 0xB6, 0xB8, 0xBA, 0xBC, 0xBE, 0xC0, 0xC2, 0xC4, 0xC6,
0xC8, 0xCA, 0xCC, 0xCE, 0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6,
0xE8, 0xEA, 0xEC, 0xEE, 0xF0, 0xF2, 0xF4, 0xF6, 0xF8, 0xFA, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
}
var scaleAndBiasChromaDownLUT = [256]uint8{
0x3C, 0x3C, 0x3D, 0x3D, 0x3E, 0x3E, 0x3F, 0x3F, 0x40, 0x40, 0x41, 0x41, 0x42, 0x42, 0x43, 0x43,
0x44, 0x44, 0x45, 0x45, 0x46, 0x46, 0x47, 0x47, 0x48, 0x48, 0x49, 0x49, 0x4A, 0x4A, 0x4B, 0x4B,
0x4C, 0x4C, 0x4D, 0x4D, 0x4E, 0x4E, 0x4F, 0x4F, 0x50, 0x50, 0x51, 0x51, 0x52, 0x52, 0x53, 0x53,
0x54, 0x54, 0x55, 0x55, 0x56, 0x56, 0x57, 0x57, 0x58, 0x58, 0x59, 0x59, 0x5A, 0x5A, 0x5B, 0x5B,
0x5C, 0x5C, 0x5D, 0x5D, 0x5E, 0x5E, 0x5F, 0x5F, 0x60, 0x60, 0x61, 0x61, 0x62, 0x62, 0x63, 0x63,
0x64, 0x64, 0x65, 0x65, 0x66, 0x66, 0x67, 0x67, 0x68, 0x68, 0x69, 0x69, 0x6A, 0x6A, 0x6B, 0x6B,
0x6C, 0x6C, 0x6D, 0x6D, 0x6E, 0x6E, 0x6F, 0x6F, 0x70, 0x70, 0x71, 0x71, 0x72, 0x72, 0x73, 0x73,
0x74, 0x74, 0x75, 0x75, 0x76, 0x76, 0x77, 0x77, 0x78, 0x78, 0x79, 0x79, 0x7A, 0x7A, 0x7B, 0x7B,
0x7C, 0x7C, 0x7D, 0x7D, 0x7E, 0x7E, 0x7F, 0x7F, 0x80, 0x80, 0x81, 0x81, 0x82, 0x82, 0x83, 0x83,
0x84, 0x84, 0x85, 0x85, 0x86, 0x86, 0x87, 0x87, 0x88, 0x88, 0x89, 0x89, 0x8A, 0x8A, 0x8B, 0x8B,
0x8C, 0x8C, 0x8D, 0x8D, 0x8E, 0x8E, 0x8F, 0x8F, 0x90, 0x90, 0x91, 0x91, 0x92, 0x92, 0x93, 0x93,
0x94, 0x94, 0x95, 0x95, 0x96, 0x96, 0x97, 0x97, 0x98, 0x98, 0x99, 0x99, 0x9A, 0x9A, 0x9B, 0x9B,
0x9C, 0x9C, 0x9D, 0x9D, 0x9E, 0x9E, 0x9F, 0x9F, 0xA0, 0xA0, 0xA1, 0xA1, 0xA2, 0xA2, 0xA3, 0xA3,
0xA4, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0xA7, 0xA7, 0xA8, 0xA8, 0xA9, 0xA9, 0xAA, 0xAA, 0xAB, 0xAB,
0xAC, 0xAC, 0xAD, 0xAD, 0xAE, 0xAE, 0xAF, 0xAF, 0xB0, 0xB0, 0xB1, 0xB1, 0xB2, 0xB2, 0xB3, 0xB3,
0xB4, 0xB4, 0xB5, 0xB5, 0xB6, 0xB6, 0xB7, 0xB7, 0xB8, 0xB8, 0xB9, 0xB9, 0xBA, 0xBA, 0xBB, 0xBB,
}
// cubeRootLUT[x] is the cube root of x, when mapping the uint8 range [0x00,
// 0xFF] to float64 range [-128/128, +127/128].
var cubeRootLUT = [256]uint8{
+87
View File
@@ -0,0 +1,87 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
package suitar_test
import (
"bytes"
"fmt"
"io"
"log"
"os"
"time"
"github.com/google/wuffs/lib/suitar"
)
func Example_minimal() {
// This mimics Example_minimal from the standard library's archive/tar.
//
// This package's Writer is a little more strict, rejecting an (implicit)
// zero-valued Header.Typeflag or Header.ModTime that archive/tar accepts.
// Header.Mode must also be 0o644 (or 0o755), not 0o600.
// Create and add some files to the archive.
var buf bytes.Buffer
tw := suitar.NewWriter(&buf)
var files = []struct {
Name, Body string
}{
{"readme.txt", "This archive contains some text files."},
{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
{"todo.txt", "Get animal handling license."},
}
for _, file := range files {
hdr := &suitar.Header{
Typeflag: suitar.TypeReg,
Name: file.Name,
Size: int64(len(file.Body)),
Mode: suitar.Mode644,
ModTime: time.Unix(0, 0),
}
if err := tw.WriteHeader(hdr); err != nil {
log.Fatal(err)
}
if _, err := tw.Write([]byte(file.Body)); err != nil {
log.Fatal(err)
}
}
if err := tw.Close(); err != nil {
log.Fatal(err)
}
// Open and iterate through the files in the archive.
tr := suitar.NewReader(&buf)
for {
hdr, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("Contents of %s:\n", hdr.Name)
if _, err := io.Copy(os.Stdout, tr); err != nil {
log.Fatal(err)
}
fmt.Println()
}
// Output:
// Contents of readme.txt:
// This archive contains some text files.
// Contents of gopher.txt:
// Gopher names:
// George
// Geoffrey
// Gonzo
// Contents of todo.txt:
// Get animal handling license.
}
+677
View File
@@ -0,0 +1,677 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// ----------------
// Package suitar implements the SUITAR archive file format.
//
// SUITAR is a subset of the TAR archive file format (including its popular
// USTAR, PAX and GNU extensions). This package's API is a subset of the
// standard library's archive/tar package.
//
// Being a subset means that this package's implementation is about 600 lines
// of Go code, compared to archive/tar being about 3000, a factor of 5×.
//
// The SUITAR specification is at
// https://github.com/google/wuffs/blob/main/doc/spec/suitar-spec.md
package suitar
import (
"errors"
"io"
"strings"
"time"
"unicode/utf8"
)
var (
errBadFileName = errors.New("suitar: bad file name")
errBadHeader = errors.New("suitar: bad header")
errBadPadding = errors.New("suitar: bad padding")
errClosed = errors.New("suitar: closed")
errHeaderSize = errors.New("suitar: inconsistent Header.Size and Write length")
errHeaderTypeflag = errors.New("suitar: inconsistent Header.Typeflag for Write")
)
// lenMagic makes headerBlockTemplate[:lenMagic] SUITAR's magic signature.
const lenMagic = 12
// headerBlockTemplate is the contents of each 512-byte header block. Each
// SUITAR archive entry (a file or directory) consists of: 1 header block (with
// typeflag typeGNULongName), the file name, 1 header block (with typeflag
// TypeReg or TypeDir or TypeGNUSparse) and the file contents.
//
// '?' bytes mean that the header block's bytes can vary at that position.
// Otherwise, the bytes are hard-coded. SUITAR is a stricter subset of TAR.
const headerBlockTemplate = "\x13sUItAR\x00\xFE\xFDv1\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x000000???\x000177" + // ??? = mode bits, 0177776 = uid("nobody").
"776\x000177776\x00\x80\x00\x00\x00" +
"????????\x80\x00\x00\x00????" + // ???????? = physical file size, ???????? = modTime.
"??????????\x00 ?\x00\x00\x00" + // ?????? = checksum, ? = typeflag.
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00ustar \x00nobody\x00" + // "ustar " mid-block magic signature, "nobody" ≈ uid.
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00nobody\x00" + // "nobody" ≈ gid.
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
// The 128-byte tail of this headerBlockTemplate only applies to
// TypeGNUSparse blocks. Otherwise, the final 128 bytes must be NUL.
"\x00\x00\x80\x00\x00\x00????????\x80\x00" + // ???????? = sparse anti-hole offset (and its length is 0).
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x80\x00\x00\x00????????\x00" + // ???????? = sparse logical file size.
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
// Valid values for Header.Typeflag. The zero value is invalid.
const (
TypeReg = '0' // Regular file (with contents).
TypeDir = '5' // Directory (with no contents).
TypeGNUSparse = 'S' // Sparse file (its contents are all NUL bytes).
typeGNULongName = 'L'
)
// Valid values for Header.Mode. The zero value is invalid.
const (
Mode644 = int64(0o644) // "rw-r--r--" mode bits, also known as permission bits.
Mode755 = int64(0o755) // "rwxr-xr-x" mode bits, also known as permission bits.
)
// IsValidHeaderName returns whether name is a valid Header.Name field value.
func IsValidHeaderName(name string) bool {
return (len(name) < 4096) &&
(name != "") &&
(name != ".") &&
(name != "..") &&
(name[0] != '/') &&
(name[len(name)-1] != '/') &&
!strings.HasPrefix(name, "./") &&
!strings.HasPrefix(name, "../") &&
!strings.Contains(name, "//") &&
!strings.Contains(name, "/./") &&
!strings.Contains(name, "/../") &&
!containsASCIIControlCharactersOrDel(name) &&
utf8.ValidString(name)
}
func containsASCIIControlCharactersOrDel(s string) bool {
for i := range len(s) {
if c := s[i]; (c < 0x20) || (c == 0x7F) {
return true
}
}
return false
}
func isAllZeroes(b []byte) bool {
for _, c := range b {
if c != 0 {
return false
}
}
return true
}
func readFullNoEOF(r io.Reader, b []byte) (int, error) {
n, err := io.ReadFull(r, b)
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return n, err
}
func u64le(b []byte) uint64 {
return (uint64(b[0]) << 56) |
(uint64(b[1]) << 48) |
(uint64(b[2]) << 40) |
(uint64(b[3]) << 32) |
(uint64(b[4]) << 24) |
(uint64(b[5]) << 16) |
(uint64(b[6]) << 8) |
(uint64(b[7]) << 0)
}
func roundUp512(n uint64) uint64 {
return (n + 511) &^ 511
}
type block [512]byte
func (b *block) calculateChecksum() uint32 {
checksum := uint32(0)
for _, c := range b[0x000:0x094] {
checksum += uint32(c)
}
// The 8 checksum bytes have value 32 when calculating the checksum itself.
checksum += 256
for _, c := range b[0x09C:0x200] {
checksum += uint32(c)
}
return checksum
}
func (b *block) setChecksum() {
checksum := b.calculateChecksum()
b[0x09B] = ' '
b[0x09A] = '\x00'
for i := range 6 {
b[0x099-i] = '0' + byte(checksum&7)
checksum >>= 3
}
}
func (b *block) isValidHeader(which int) bool {
// Compare b to the headerBlockTemplate. Blocks that aren't TypeGNUSparse
// must end in 128 NUL bytes.
end := 0x200
if typeflag := b[0x09C]; typeflag != TypeGNUSparse {
end = 0x180
}
for i := range end {
if c := headerBlockTemplate[i]; (c != '?') && (c != b[i]) {
return false
}
}
if (end == 0x180) && !isAllZeroes(b[0x180:0x200]) {
return false
}
// Check the mode bits.
if (b[0x0068] == '6') && (b[0x0069] == '4') && (b[0x006A] == '4') {
// No-op.
} else if (b[0x0068] == '7') && (b[0x0069] == '5') && (b[0x006A] == '5') && (which == 1) {
// No-op.
} else {
return false
}
// Check the checksum.
checksum := b.calculateChecksum()
for i := range 6 {
if b[0x099-i] != ('0' + byte(checksum&7)) {
return false
}
checksum >>= 3
}
return true
}
// Header is a single header in a SUITAR archive.
//
// It is a subset of tar.Header from the standard library's archive/tar
// package, but it is stricter about what field values are valid.
type Header struct {
Typeflag byte
Name string
Size int64
Mode int64
ModTime time.Time
}
// Valid returns whether h is valid to pass to a Writer. Specifically:
//
// - Typeflag must be one of three values (TypeReg, TypeDir, TypeGNUSparse).
// In particular, it cannot be zero.
// - Name must satisfy IsValidHeaderName.
// - Size must be non-negative. It must be 0 if Typeflag is TypeDir.
// - Mode must be one of two values (Mode644, Mode755). It must be Mode755 if
// Typeflag is TypeDir.
// - ModTime.Unix() must be a non-negative int64. In particular, ModTime must
// be on or after 1 January 1970 and so cannot be the zero time.Time (which
// is 1 January Year-1).
// - Size and ModTime.Unix() must also be less than (1 << 53).
//
// A Header returned (without error) by a Reader will always be valid.
func (h *Header) Valid() bool {
if h == nil {
return false
}
switch h.Typeflag {
default:
return false
case TypeDir:
if (h.Size != 0) || (h.Mode != Mode755) {
return false
}
case TypeReg, TypeGNUSparse:
if (h.Mode != Mode644) && (h.Mode != Mode755) {
return false
}
}
const maxExcl = 1 << 53
m := h.ModTime.Unix()
return (0 <= h.Size) && (h.Size < maxExcl) &&
(0 <= m) && (m < maxExcl) &&
IsValidHeaderName(h.Name)
}
// NewWriter creates a new Writer writing to w.
func NewWriter(w io.Writer) *Writer {
return &Writer{w: w}
}
// Writer provides sequential writing of a SUITAR archive.
type Writer struct {
err error
w io.Writer
header Header
remaining int64
bIndex int32
block block
nameBuf [4096]byte
}
func (w *Writer) flush() error {
if w.err != nil {
return w.err
} else if w.remaining != 0 {
w.err = errHeaderSize
return w.err
} else if w.bIndex != 0 {
clear(w.block[w.bIndex:])
if _, err := w.w.Write(w.block[:]); err != nil {
w.err = err
return w.err
}
w.bIndex = 0
}
return nil
}
// WriteHeader writes h and prepares to accept the file's contents (as w is
// also an io.Writer).
func (w *Writer) WriteHeader(h *Header) error {
if err := w.flush(); err != nil {
return err
} else if !h.Valid() {
w.err = errBadHeader
return w.err
}
w.header = *h
n := len(w.header.Name)
copy(w.nameBuf[:], w.header.Name)
w.nameBuf[n] = 0
initBlock(&w.block, typeGNULongName, int64(n+1), Mode644, 0)
if _, err := w.w.Write(w.block[:]); err != nil {
w.err = err
return w.err
}
w.remaining = int64(n + 1)
if _, err := w.Write(w.nameBuf[:n+1]); err != nil {
w.err = err
return w.err
} else if err = w.flush(); err != nil {
w.err = err
return w.err
}
initBlock(&w.block, w.header.Typeflag, w.header.Size, w.header.Mode, w.header.ModTime.Unix())
if _, err := w.w.Write(w.block[:]); err != nil {
w.err = err
return w.err
}
w.remaining = w.header.Size
if w.header.Typeflag == TypeGNUSparse {
w.remaining = 0
}
return nil
}
func initBlock(b *block, typeflag byte, size int64, mode int64, modTime int64) {
// 0o0177776 octal is 65534, which is Debian's UID/GID for "nobody".
const uidForNobody = "0177776"
clear(b[:])
copy(b[:], headerBlockTemplate[:lenMagic])
modeBits := "0000644"
if mode == Mode755 {
modeBits = "0000755"
}
copy(b[0x064:], modeBits)
copy(b[0x06C:], uidForNobody)
copy(b[0x074:], uidForNobody)
physicalSize := size
if typeflag == TypeGNUSparse {
physicalSize = 0
}
setI64(b, 0x07C, physicalSize)
setI64(b, 0x088, modTime)
b[0x09C] = typeflag
copy(b[0x101:], "ustar ")
copy(b[0x109:], "nobody")
copy(b[0x129:], "nobody")
if typeflag == TypeGNUSparse {
setI64(b, 0x182, size)
setI64(b, 0x18E, 0)
setI64(b, 0x1E3, size)
}
b.setChecksum()
}
func setI64(b *block, offset int, value int64) {
// The 0x80 uses base-256 instead of octal, allowing file sizes >= 8GiB.
b[offset] = 0x80
for i := range 8 {
b[(offset+11)-i] = byte(value)
value >>= 8
}
}
// Write satisfies io.Writer.
func (w *Writer) Write(b []byte) (int, error) {
if w.err != nil {
return 0, w.err
} else if (w.header.Typeflag != TypeReg) && (w.header.Typeflag != TypeGNUSparse) {
w.err = errHeaderTypeflag
return 0, w.err
} else if len(b) == 0 {
return 0, nil
}
tooMuch := int64(len(b)) > w.remaining
if tooMuch {
b = b[:w.remaining]
}
ret := 0
for len(b) > 0 {
if w.bIndex == 0 {
split := len(b) &^ 511
prefix, suffix := b[:split], b[split:]
if len(prefix) > 0 {
n, err := w.w.Write(prefix)
w.remaining -= int64(n)
ret += n
if err != nil {
w.err = err
break
}
}
if len(suffix) > 0 {
w.bIndex = int32(copy(w.block[:], suffix))
w.remaining -= int64(w.bIndex)
ret += int(w.bIndex)
}
b = nil
break
}
n := copy(w.block[w.bIndex:], b)
w.bIndex += int32(n)
w.remaining -= int64(n)
ret += n
b = b[n:]
if int(w.bIndex) < len(w.block) {
continue
} else if _, err := w.w.Write(w.block[:]); err != nil {
w.err = err
break
}
}
if tooMuch && (w.err == nil) {
w.err = errHeaderSize
}
return ret, w.err
}
// Close satisfies io.Closer. It closes the entire archive, not just one entry.
func (w *Writer) Close() error {
if err := w.flush(); err != nil {
return err
}
clear(w.nameBuf[:1024])
if _, err := w.w.Write(w.nameBuf[:1024]); err != nil {
w.err = err
return err
}
w.err = errClosed
return nil
}
// NewReader creates a new Reader reading from r.
func NewReader(r io.Reader) *Reader {
return &Reader{r: r}
}
// Reader provides sequential reading of a SUITAR archive.
type Reader struct {
err error
r io.Reader
remaining int64
numPadding int32
sparse bool
block block
nameBuf [4096]byte
}
// Next advances to the next entry in the SUITAR archive, preparing to read the
// file's contents (as r is also an io.Reader).
func (r *Reader) Next() (Header, error) {
if r.err != nil {
return Header{}, r.err
} else if r.remaining > 0 {
if _, err := io.Copy(io.Discard, r); err != nil {
r.err = err
return Header{}, r.err
}
}
if _, err := readFullNoEOF(r.r, r.block[:]); err != nil {
r.err = err
return Header{}, r.err
} else if r.block[0x09C] == 0 {
// SUITAR ends with 2 blocks (1024 bytes) of zeroes.
if !isAllZeroes(r.block[:]) {
r.err = errBadHeader
return Header{}, r.err
} else if _, err := readFullNoEOF(r.r, r.block[:]); err != nil {
r.err = err
return Header{}, r.err
} else if !isAllZeroes(r.block[:]) {
r.err = errBadHeader
return Header{}, r.err
}
r.err = io.EOF
return Header{}, r.err
}
nameLenInclNul, err := parseBlock0(&r.block)
if err != nil {
r.err = err
return Header{}, r.err
}
n1 := nameLenInclNul - 1
n2 := roundUp512(nameLenInclNul)
if _, err := readFullNoEOF(r.r, r.nameBuf[:n2]); err != nil {
r.err = err
return Header{}, r.err
}
name := string(r.nameBuf[:n1])
if !IsValidHeaderName(name) || !isAllZeroes(r.nameBuf[n1:n2]) {
r.err = errBadFileName
return Header{}, r.err
}
if _, err := readFullNoEOF(r.r, r.block[:0x200]); err != nil {
return Header{}, err
}
typeflag, size, mode, modTime, err := parseBlock1(&r.block)
if err != nil {
return Header{}, err
}
r.remaining = size
r.numPadding = int32(roundUp512(uint64(size)) - uint64(size))
r.sparse = typeflag == TypeGNUSparse
return Header{
Typeflag: typeflag,
Name: name,
Size: size,
Mode: mode,
ModTime: time.Unix(modTime, 0),
}, nil
}
func parseBlock0(b *block) (uint64, error) {
if !b.isValidHeader(0) {
return 0, errBadHeader
}
size := u64le(b[0x080:])
modTime := u64le(b[0x08C:])
if (size < 2) || (4097 <= size) {
return 0, errBadFileName
} else if (modTime != 0) || (b[0x09C] != 'L') {
return 0, errBadHeader
}
return size, nil
}
func parseBlock1(b *block) (byte, int64, int64, int64, error) {
if !b.isValidHeader(1) {
return 0, 0, 0, 0, errBadHeader
}
typeflag := b[0x09C]
if (typeflag != TypeReg) && (typeflag != TypeDir) && (typeflag != TypeGNUSparse) {
return 0, 0, 0, 0, errBadHeader
}
mode := Mode644
if b[0x068] == '7' {
mode = Mode755
}
size := int64(u64le(b[0x080:]))
if size < 0 {
return 0, 0, 0, 0, errBadHeader
}
modTime := int64(u64le(b[0x08C:]))
if modTime < 0 {
return 0, 0, 0, 0, errBadHeader
}
if typeflag == TypeDir {
if (size != 0) || (mode != Mode755) {
return 0, 0, 0, 0, errBadHeader
}
} else if typeflag == TypeGNUSparse {
if size != 0 {
return 0, 0, 0, 0, errBadHeader
}
size0 := int64(u64le(b[0x186:]))
size1 := int64(u64le(b[0x1E7:]))
if (size0 != size1) || (size1 < 0) {
return 0, 0, 0, 0, errBadHeader
}
size = size1
}
return typeflag, size, mode, modTime, nil
}
// Read satisfies io.Reader.
func (r *Reader) Read(b []byte) (int, error) {
if r.err != nil {
return 0, r.err
} else if r.remaining == 0 {
return 0, io.EOF
} else if len(b) == 0 {
return 0, nil
}
b = b[:int(min(r.remaining, int64(len(b))))]
if r.sparse {
n := len(b)
clear(b)
r.remaining -= int64(n)
if r.remaining == 0 {
return n, io.EOF
}
return n, nil
}
n, err := r.r.Read(b)
r.remaining -= int64(n)
if r.remaining == 0 {
if r.numPadding > 0 {
padding := r.block[:r.numPadding]
_, readFullErr := readFullNoEOF(r.r, padding)
if err == nil {
err = readFullErr
if err == nil {
for _, c := range padding {
if c != 0 {
err = errBadPadding
break
}
}
}
}
r.numPadding = 0
}
if (err == nil) || (err == io.EOF) {
return n, io.EOF
}
} else if err == io.EOF {
err = io.ErrUnexpectedEOF
}
r.err = err
return n, r.err
}
+186
View File
@@ -0,0 +1,186 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
package suitar
import (
"archive/tar"
"bytes"
"fmt"
"hash/crc32"
"io"
"os"
"testing"
)
type crcWriter uint32
func (c *crcWriter) Write(b []byte) (int, error) {
state := uint32(*c)
state = crc32.Update(state, crc32.IEEETable, b)
*c = crcWriter(state)
return len(b), nil
}
func testWriter(tt *testing.T, sparse bool) {
f, err := os.Open("../../test/data/archive.tar")
if err != nil {
tt.Fatalf("os.Open: %v", err)
}
defer f.Close()
// Convert from t (tar, using the standard library) to s (suitar, using
// this package).
buf := bytes.Buffer{}
sWriter := NewWriter(&buf)
for tReader := tar.NewReader(f); ; {
tHeader, err := tReader.Next()
if err == io.EOF {
break
} else if err != nil {
tt.Fatalf("Next: %v", err)
}
sHeader := &Header{
Typeflag: tHeader.Typeflag,
Name: tHeader.Name,
Size: tHeader.Size,
Mode: tHeader.Mode,
ModTime: tHeader.ModTime,
}
if sparse && (sHeader.Typeflag == TypeReg) {
sHeader.Typeflag = TypeGNUSparse
}
if err := sWriter.WriteHeader(sHeader); err != nil {
tt.Fatalf("WriteHeader: %v", err)
}
dstWriter := (io.Writer)(sWriter)
if sparse {
dstWriter = io.Discard
}
if _, err := io.Copy(dstWriter, tReader); err != nil {
tt.Fatalf("io.Copy: %v", err)
}
}
if err := sWriter.Close(); err != nil {
tt.Fatalf("Close: %v", err)
}
got := buf.Bytes()
wantFilename := "../../test/data/archive"
if sparse {
wantFilename += ".sparse.suitar"
} else {
wantFilename += ".dense.suitar"
}
want, err := os.ReadFile(wantFilename)
if err != nil {
tt.Fatalf("os.ReadFile: %v", err)
}
if !bytes.Equal(got, want) {
tt.Fatalf("did not recreate golden test file")
}
}
func testReader(tt *testing.T, sparse bool, ignore bool) {
filename, wantTypeflag := "../../test/data/archive.dense.suitar", " T:'0'"
if sparse {
filename, wantTypeflag = "../../test/data/archive.sparse.suitar", " T:'S'"
}
wantChecksums := []string(nil)
if ignore {
wantChecksums = []string{
"C:0x00000000",
"C:0x00000000",
"C:0x00000000",
"C:0x00000000",
"C:0x00000000",
"C:0x00000000",
"C:0x00000000",
"C:0x00000000",
}
} else if sparse {
wantChecksums = []string{
"C:0x00000000",
"C:0xB69F8E37",
"C:0x73FF3CAE",
"C:0xD71F022F",
"C:0xC446EAB8",
"C:0x5F228EB9",
"C:0x7BA7D011",
"C:0x48792A7F",
}
} else {
wantChecksums = []string{
"C:0x00000000",
"C:0xFEDD8F35",
"C:0x87EE5E05",
"C:0x703E9270",
"C:0xC37CB538",
"C:0x2B0B23B0",
"C:0xABE507EF",
"C:0x67FABE9C",
}
}
want := "" +
wantChecksums[0] + wantTypeflag + " S:0x0000 M:644 MT:0x5E3A5C50 N:artificial/0.bytes\n" +
wantChecksums[1] + wantTypeflag + " S:0x0355 M:644 MT:0x5F33F6E6 N:github-tags.json\n" +
wantChecksums[2] + wantTypeflag + " S:0x02B5 M:755 MT:0x608F960B N:hello.sh\n" +
wantChecksums[3] + wantTypeflag + " S:0x0068 M:644 MT:0x608F954D N:non-ascii/αβ.txt\n" +
wantChecksums[4] + wantTypeflag + " S:0x0097 M:644 MT:0x608F96C7 N:non-ascii/😻.txt\n" +
wantChecksums[5] + wantTypeflag + " S:0x00D0 M:644 MT:0x5E3A5C50 N:pjw-thumbnail.png\n" +
wantChecksums[6] + wantTypeflag + " S:0x03AE M:644 MT:0x5E3A5C50 N:romeo.txt\n" +
wantChecksums[7] + wantTypeflag + " S:0x022E M:644 MT:0x5E3A5C50 N:romeo.txt.gz\n" +
""
f, err := os.Open(filename)
if err != nil {
tt.Fatalf("os.Open: %v", err)
}
defer f.Close()
buf := bytes.Buffer{}
for r := NewReader(f); ; {
h, err := r.Next()
if err == io.EOF {
break
} else if err != nil {
tt.Fatalf("Next: %v", err)
}
checksum := crcWriter(0)
if !ignore {
if _, err := io.Copy(&checksum, r); err != nil {
tt.Fatalf("io.Copy: %v", err)
}
}
fmt.Fprintf(&buf, "C:0x%08X T:'%c' S:0x%04X M:%3o MT:0x%08X N:%s\n",
checksum, h.Typeflag, h.Size, h.Mode, h.ModTime.Unix(), h.Name)
}
if got := buf.String(); got != want {
tt.Fatalf("\ngot:\n%s\nwant:\n%s", got, want)
}
}
func TestWriterDense(tt *testing.T) { testWriter(tt, false) }
func TestWriterSparse(tt *testing.T) { testWriter(tt, true) }
func TestReaderDenseCheck(tt *testing.T) { testReader(tt, false, false) }
func TestReaderDenseIgnore(tt *testing.T) { testReader(tt, false, true) }
func TestReaderSparseCheck(tt *testing.T) { testReader(tt, true, false) }
func TestReaderSparseIgnore(tt *testing.T) { testReader(tt, true, true) }
+460 -89
View File
@@ -35,11 +35,11 @@
// may be better (faster) overall to skip any compression and decompression on
// either side of the communication channel.
//
// The Encoder.Encode method also makes no further memory allocations (above
// whatever its given io.Writer makes, if any). It is a lower-level API. See
// the encodeImage function in this package's test code for a higher-level API
// (with more dependencies and without the no-allocation guarantee) that takes
// an image.Image argument.
// The Encoder and AnimationEncoder methods also make no further memory
// allocations (above whatever its given io.Writer makes, if any). They are
// lower-level APIs. See the encodeImage function in this package's test code
// for a higher-level API (with more dependencies and without the no-allocation
// guarantee) that takes an image.Image argument.
package uncompng
import (
@@ -101,7 +101,7 @@ const (
)
// Encoder is an opaque type that can convert a slice of pixel data to
// PNG-formatted bytes.
// PNG-formatted bytes. For APNG (Animated PNG), see AnimationEncoder.
//
// It contains all of the buffers needed for the Encode method. Once the
// Encoder itself is allocated, Encoder.Encode makes no further allocations
@@ -155,19 +155,58 @@ type Encoder struct {
//
// After that long preamble, here's the data layout of the buf field. Each
// time a slice of buf is passed to an io.Writer, it contains exactly one
// IDAT chunk. The final time will also contain an IEND chunk after the
// IDAT chunk, unless it won't fit, in which case there will be one more
// Write call with just the 12-byte IEND chunk (and no IDAT chunk).
// IDAT chunk (or fdAT chunk, for animated PNG). The final time will also
// contain an IEND chunk after the IDAT/fdAT chunk, unless it won't fit, in
// which case there will be one more Write call with just the 12-byte IEND
// chunk (and no IDAT/fdAT chunk).
//
// For all but the first Write, the 4-byte IDAT chunk length starts at
// buf[0x0000:]. For the first Write, it will start at buf[0x0021:], since
// it is preceded by the magic signature and the IHDR chunk.
// When Encode'ing still PNGs, for all but the first Write, the 4-byte
// IDAT/fdAT chunk length starts at buf[0x0000:]. For the first Write, it
// will start at buf[0x0021:], since it is preceded by the magic signature
// and the IHDR chunk.
//
// Either way, each IDAT chunk contains exactly one DEFLATE block (an
// Either way, each IDAT/fdAT chunk contains exactly one DEFLATE block (an
// uncompressed block). The very first IDAT (in the very first Write) also
// contains the 2-byte ZLIB header. The very last IDAT also contains the
// 4-byte Adler-32 checksum.
//
// For example, here's how buf might be filled (four times) and shipped out
// to an io.Writer, when calling Encode to produce a still image.
//
// +--------------------------------------+
// | MAGIC | IHDR | IDAT | Encode
// +--------------------------------------+ :
// | IDAT | :
// +--------------------------------------+ :
// | IDAT | :
// +--------------------------------------+ :
// | IDAT | IEND | unused | :
// +--------------------------------------+
//
// The buf-packing is more complicated for an animated PNG. Reference (§).
//
// +--------------------------------------+
// | MAGIC | IHDR | acTL | unused | EncodeHeader
// +--------------------------------------+
// | fcTL | IDAT | EncodeFrame
// +--------------------------------------+ :
// | IDAT | :
// +--------------------------------------+ :
// | IDAT | unused | :
// +--------------------------------------+
// | fcTL | fdAT | EncodeFrame
// +--------------------------------------+ :
// | fdAT | :
// +--------------------------------------+ :
// | fdAT | unused | :
// +--------------------------------------+
// | fcTL | fdAT | EncodeFrame
// +--------------------------------------+ :
// | fdAT | :
// +--------------------------------------+ :
// | fdAT | IEND | unused | :
// +--------------------------------------+
//
// Each DEFLATE uncompressed block's payload spans e.buf[ei:ej], for two
// indexes ei and ej, either explicit local variables or implicit state.
// Writing pixel data to the Encoder increments ej, flushing to the
@@ -182,12 +221,47 @@ type Encoder struct {
// checksum state, as these 4 bytes won't be overwritten unless emitting
// the final IDAT chunk (containing the final DEFLATE block).
buf [65536]byte
// For still PNG images, there's 1 Encode call, which leads to:
//
// - 1 encodeImageHeader call. This sets buf to hold the magic signature,
// IHDR chunk and the start of the first IDAT chunk, but does not write
// to the io.Writer yet.
// - 1 encodeFramePayload call, with frameTypeStill. This fills in the
// rest of the first IDAT, and all of any remaining IDATs, and the final
// IEND, writing to the io.Writer one or more times (on every flush
// call, and possibly once more for the IEND chunk). Each flush is
// passed frameTypeStill.
//
// For animated PNG images, there's 1 EncodeHeader call, which leads to...
//
// - 1 encodeImageHeader call. This sets buf to hold the magic signature,
// IHDR chunk and acTL chunk, but not any part of any IDAT chunks. It
// does, however, write to the io.Writer.
//
// ...and then N EncodeFrame calls, N = numFrames, each of which leads to:
//
// - 1 encodeFrameHeader call. This sets buf to hold the fcTL chunk and
// the start of an IDAT/fdAT chunk, but does not write to the io.Writer.
// - 1 encodeFramePayload call. This fills in the rest of the first
// IDAT/fdAT, and all of any remaining IDATs/fdATs for that frame (and
// the IEND if the last frame), writing to the io.Writer via flush,
// similar to the still image case.
//
// For animated PNG images, the frameType argument, for encodeFramePayload
// and flush, will have the frameTypeBitAnimated bit on. Of the N frames,
// the frameTypeBitFirstFrame and frameTypeBitLastFrame bits are on for the
// first and last frames. If N == 1, both bits are set for the only frame.
}
type frameType byte
const (
eiFirst = 0x0030
eiLater = 0x000D
ejMax = 0xFFF8
frameTypeBitFirstFrame = frameType(1)
frameTypeBitLastFrame = frameType(2)
frameTypeBitAnimated = frameType(4)
frameTypeStill = frameTypeBitFirstFrame | frameTypeBitLastFrame
)
// Encode writes the pixel data to w. It makes no allocations above whatever
@@ -196,24 +270,50 @@ const (
// pix holds the pixel data, either 1 or 4 bytes per pixel (doubled for
// Depth16) depending on the colorType. width and height are measured in
// pixels. stride is measured in bytes. depth must be either Depth8 or Depth16.
func (e *Encoder) Encode(w io.Writer, pix []byte, width int, height int, stride int, depth Depth, colorType ColorType) error {
if (width < 0) || (height < 0) ||
((depth != Depth8) && (depth != Depth16)) ||
(colorType.pngFileFormatEncoding() == 0xFF) {
return errors.New("uncompng: invalid argument")
} else if (width > 0xFFFFFF) || (height > 0xFFFFFF) {
return errors.New("uncompng: unsupported image size")
func (e *Encoder) Encode(
w io.Writer,
depth Depth,
colorType ColorType,
width int,
height int,
pix []byte,
stride int) error {
if err := checkArguments(depth, colorType, width, height, 1, 0); err != nil {
return err
}
e.init(width, height, depth, colorType)
ej := eiFirst
ej := e.encodeImageHeader(depth, colorType, width, height, 0, 0)
return e.encodeFramePayload(
w, frameTypeStill, nil, depth, colorType, width, height, pix, stride, ej)
}
func (e *Encoder) encodeFramePayload(
w io.Writer,
fType frameType,
seqNumPtr *uint32,
depth Depth,
colorType ColorType,
width int,
height int,
pix []byte,
stride int,
ej int) (err error) {
// AdlerB=0, Adler32A=1.
e.buf[0xFFFC] = 0
e.buf[0xFFFD] = 0
e.buf[0xFFFE] = 0
e.buf[0xFFFF] = 1
const ejMax = 0xFFF8
for y := 0; y < height; y++ {
if (ej + 1) > ejMax {
if err := e.flush(w, ej, false); err != nil {
ej, err = e.flush(w, fType, seqNumPtr, ej, false)
if err != nil {
return err
}
ej = eiLater
}
e.buf[ej+0] = 0 // PNG 'none' filter.
ej += 1
@@ -225,10 +325,10 @@ func (e *Encoder) Encode(w io.Writer, pix []byte, width int, height int, stride
row = row[:1*width]
for x := 0; x < width; x++ {
if (ej + 1) > ejMax {
if err := e.flush(w, ej, false); err != nil {
ej, err = e.flush(w, fType, seqNumPtr, ej, false)
if err != nil {
return err
}
ej = eiLater
}
e.buf[ej+0] = row[0]
ej += 1
@@ -239,10 +339,10 @@ func (e *Encoder) Encode(w io.Writer, pix []byte, width int, height int, stride
row = row[:4*width]
for x := 0; x < width; x++ {
if (ej + 3) > ejMax {
if err := e.flush(w, ej, false); err != nil {
ej, err = e.flush(w, fType, seqNumPtr, ej, false)
if err != nil {
return err
}
ej = eiLater
}
e.buf[ej+0] = row[0]
e.buf[ej+1] = row[1]
@@ -255,10 +355,10 @@ func (e *Encoder) Encode(w io.Writer, pix []byte, width int, height int, stride
row = row[:4*width]
for x := 0; x < width; x++ {
if (ej + 4) > ejMax {
if err := e.flush(w, ej, false); err != nil {
ej, err = e.flush(w, fType, seqNumPtr, ej, false)
if err != nil {
return err
}
ej = eiLater
}
e.buf[ej+0] = row[0]
e.buf[ej+1] = row[1]
@@ -272,10 +372,10 @@ func (e *Encoder) Encode(w io.Writer, pix []byte, width int, height int, stride
row = row[:2*width]
for x := 0; x < width; x++ {
if (ej + 2) > ejMax {
if err := e.flush(w, ej, false); err != nil {
ej, err = e.flush(w, fType, seqNumPtr, ej, false)
if err != nil {
return err
}
ej = eiLater
}
e.buf[ej+0] = row[0]
e.buf[ej+1] = row[1]
@@ -287,10 +387,10 @@ func (e *Encoder) Encode(w io.Writer, pix []byte, width int, height int, stride
row = row[:8*width]
for x := 0; x < width; x++ {
if (ej + 6) > ejMax {
if err := e.flush(w, ej, false); err != nil {
ej, err = e.flush(w, fType, seqNumPtr, ej, false)
if err != nil {
return err
}
ej = eiLater
}
e.buf[ej+0] = row[0]
e.buf[ej+1] = row[1]
@@ -306,10 +406,10 @@ func (e *Encoder) Encode(w io.Writer, pix []byte, width int, height int, stride
row = row[:8*width]
for x := 0; x < width; x++ {
if (ej + 8) > ejMax {
if err := e.flush(w, ej, false); err != nil {
ej, err = e.flush(w, fType, seqNumPtr, ej, false)
if err != nil {
return err
}
ej = eiLater
}
e.buf[ej+0] = row[0]
e.buf[ej+1] = row[1]
@@ -325,10 +425,18 @@ func (e *Encoder) Encode(w io.Writer, pix []byte, width int, height int, stride
}
}
return e.flush(w, ej, true)
_, err = e.flush(w, fType, seqNumPtr, ej, true)
return err
}
func (e *Encoder) init(width int, height int, depth Depth, colorType ColorType) {
func (e *Encoder) encodeImageHeader(
depth Depth,
colorType ColorType,
width int,
height int,
numFrames uint32,
numPlays uint32) int {
// PNG magic signature.
e.buf[0x0000] = 0x89
e.buf[0x0001] = 'P'
@@ -370,7 +478,36 @@ func (e *Encoder) init(width int, height int, depth Depth, colorType ColorType)
e.buf[0x001F] = byte(ihdrCRC32 >> 8)
e.buf[0x0020] = byte(ihdrCRC32 >> 0)
// IDAT chunk length placeholder.
if numFrames > 0 {
// acTL chunk length.
e.buf[0x0021] = 0
e.buf[0x0022] = 0
e.buf[0x0023] = 0
e.buf[0x0024] = 0x08
// acTL chunk type.
e.buf[0x0025] = 'a'
e.buf[0x0026] = 'c'
e.buf[0x0027] = 'T'
e.buf[0x0028] = 'L'
// acTL chunk payload.
e.buf[0x0029] = byte(numFrames >> 24)
e.buf[0x002A] = byte(numFrames >> 16)
e.buf[0x002B] = byte(numFrames >> 8)
e.buf[0x002C] = byte(numFrames >> 0)
e.buf[0x002D] = byte(numPlays >> 24)
e.buf[0x002E] = byte(numPlays >> 16)
e.buf[0x002F] = byte(numPlays >> 8)
e.buf[0x0030] = byte(numPlays >> 0)
// acTL CRC-32/IEEE checksum.
actlCRC32 := crc32IEEE(e.buf[0x0025:0x0031])
e.buf[0x0031] = byte(actlCRC32 >> 24)
e.buf[0x0032] = byte(actlCRC32 >> 16)
e.buf[0x0033] = byte(actlCRC32 >> 8)
e.buf[0x0034] = byte(actlCRC32 >> 0)
return 0x0035
}
// IDAT chunk length placeholder. idatChunkStart is 0x0021.
e.buf[0x0021] = 0
e.buf[0x0022] = 0
e.buf[0x0023] = 0
@@ -389,16 +526,13 @@ func (e *Encoder) init(width int, height int, depth Depth, colorType ColorType)
e.buf[0x002D] = 0
e.buf[0x002E] = 0
e.buf[0x002F] = 0
// eiFirst is therefore 0x0030.
// AdlerB=0, Adler32A=1.
e.buf[0xFFFC] = 0
e.buf[0xFFFD] = 0
e.buf[0xFFFE] = 0
e.buf[0xFFFF] = 1
return 0x0030
}
func (e *Encoder) updateAdler32(ei int, ej int) {
func (e *Encoder) updateAdler32(
ei int,
ej int) {
b := (uint32(e.buf[0xFFFC]) << 8) | uint32(e.buf[0xFFFD])
a := (uint32(e.buf[0xFFFE]) << 8) | uint32(e.buf[0xFFFF])
for ei < ej {
@@ -419,34 +553,52 @@ func (e *Encoder) updateAdler32(ei int, ej int) {
e.buf[0xFFFF] = byte(a >> 0)
}
func (e *Encoder) flush(w io.Writer, ej int, final bool) error {
// Update the IDAT chunk length placeholder.
crc32Start, ei := 0, 0
if e.buf[0x0004] == 0x0D { // First IDAT chunk.
idatChunkLen := ej - 0x0029
if final {
idatChunkLen += 4
}
e.buf[0x0021] = byte(idatChunkLen >> 24)
e.buf[0x0022] = byte(idatChunkLen >> 16)
e.buf[0x0023] = byte(idatChunkLen >> 8)
e.buf[0x0024] = byte(idatChunkLen >> 0)
crc32Start, ei = 0x0025, eiFirst
} else { // Later IDAT chunk.
idatChunkLen := ej - 0x0008
if final {
idatChunkLen += 4
}
e.buf[0x0000] = byte(idatChunkLen >> 24)
e.buf[0x0001] = byte(idatChunkLen >> 16)
e.buf[0x0002] = byte(idatChunkLen >> 8)
e.buf[0x0003] = byte(idatChunkLen >> 0)
crc32Start, ei = 0x0004, eiLater
func (e *Encoder) flush(
w io.Writer,
fType frameType,
seqNumPtr *uint32,
ej int,
finalIDATOfFrame bool) (retEj int, retErr error) {
// Calculate where the IDAT/fdAT chunk starts, in e.buf. See (§).
idatChunkStart := 0
if e.buf[0x0007] == 0x0A {
// 0x0A is magicSignature[7]. First IDAT chunk of a still image.
idatChunkStart = 0x0021
} else if e.buf[0x0007] == 0x4C {
// 0x4C is "????fcTL"[7]. First IDAT/fdAT chunk of an animated frame.
idatChunkStart = 0x0026
} else if e.buf[0x0007] == 0x54 {
// 0x54 is "??????AT"[7]. Later IDAT/fdAT chunk.
idatChunkStart = 0x0000
} else {
return 0, errors.New("uncompng: unreachable")
}
// 13 is the 0x000D in "Write... 0x000D bytes total" below.
ei := idatChunkStart + 13
if e.buf[0x0007] != 0x54 {
// 2 more bytes for the ZLIB header (CM, CINFO, FDICT, FLEVEL).
ei += 2
}
if (fType & frameTypeBitFirstFrame) == 0 {
// fdAT chunks are longer than IDAT chunks by 4 bytes.
ei += 4
}
// Update the IDAT/fdAT chunk length placeholder.
idatChunkLen := ej - (idatChunkStart + 8)
if finalIDATOfFrame {
idatChunkLen += 4 // The Adler-32 checksum occupies 4 bytes.
}
e.buf[idatChunkStart+0] = byte(idatChunkLen >> 24)
e.buf[idatChunkStart+1] = byte(idatChunkLen >> 16)
e.buf[idatChunkStart+2] = byte(idatChunkLen >> 8)
e.buf[idatChunkStart+3] = byte(idatChunkLen >> 0)
// Update the DEFLATE uncompressed block header placeholder.
deflateBlockLen := ej - ei
e.buf[ei-5] = btou8(final)
e.buf[ei-5] = btou8(finalIDATOfFrame)
e.buf[ei-4] = 0x00 ^ byte(deflateBlockLen>>0)
e.buf[ei-3] = 0x00 ^ byte(deflateBlockLen>>8)
e.buf[ei-2] = 0xFF ^ byte(deflateBlockLen>>0)
@@ -454,7 +606,7 @@ func (e *Encoder) flush(w io.Writer, ej int, final bool) error {
// Update (and maybe write) the Adler-32 checksum.
e.updateAdler32(ei, ej)
if final {
if finalIDATOfFrame {
e.buf[ej+0] = e.buf[0xFFFC]
e.buf[ej+1] = e.buf[0xFFFD]
e.buf[ej+2] = e.buf[0xFFFE]
@@ -463,28 +615,44 @@ func (e *Encoder) flush(w io.Writer, ej int, final bool) error {
}
// Write the CRC-32/IEEE checksum.
idatCRC32 := crc32IEEE(e.buf[crc32Start:ej])
idatCRC32 := crc32IEEE(e.buf[idatChunkStart+4 : ej])
e.buf[ej+0] = byte(idatCRC32 >> 24)
e.buf[ej+1] = byte(idatCRC32 >> 16)
e.buf[ej+2] = byte(idatCRC32 >> 8)
e.buf[ej+3] = byte(idatCRC32 >> 0)
ej += 4
if !final {
if !finalIDATOfFrame {
if _, err := w.Write(e.buf[:ej]); err != nil {
return err
return 0, err
}
// Write (or reserve space for) 0x000D bytes total:
// - 4 bytes for the IDAT chunk length placeholder.
// - 4 bytes for the IDAT chunk type.
// - 4 bytes for the IDAT/fdAT chunk length placeholder.
// - 4 bytes for the IDAT/fdAT chunk type.
// - 5 bytes for the DEFLATE uncompressed block header placeholder.
//
// eiLater is therefore 0x000D.
e.buf[0x0004] = 'I'
e.buf[0x0005] = 'D'
t := fType & frameTypeBitFirstFrame
e.buf[0x0004] = "fI"[t]
e.buf[0x0005] = "dD"[t]
e.buf[0x0006] = 'A'
e.buf[0x0007] = 'T'
return nil
if (t != 0) || (seqNumPtr == nil) {
return 0x000D + 0, nil
}
seqNum := *seqNumPtr
*seqNumPtr++
e.buf[0x0008] = byte(seqNum >> 24)
e.buf[0x0009] = byte(seqNum >> 16)
e.buf[0x000A] = byte(seqNum >> 8)
e.buf[0x000B] = byte(seqNum >> 0)
return 0x000D + 4, nil
}
if (fType & frameTypeBitLastFrame) == 0 {
if _, err := w.Write(e.buf[:ej]); err != nil {
return 0, err
}
return 0, nil
}
const iendChunk = "\x00\x00\x00\x00IEND\xAE\x42\x60\x82"
@@ -495,27 +663,230 @@ func (e *Encoder) flush(w io.Writer, ej int, final bool) error {
}
if _, err := w.Write(e.buf[:ej]); err != nil {
return err
return 0, err
}
if writeSeparateIENDChunk {
copy(e.buf[:], iendChunk)
if _, err := w.Write(e.buf[:len(iendChunk)]); err != nil {
return err
return 0, err
}
}
return 0, nil
}
// AnimationEncoder is like Encoder but for APNG (Animated PNG), not still PNG.
//
// It contains all of the buffers needed for the EncodeXxx methods. Once the
// AnimationEncoder itself is allocated, AnimationEncoder.EncodeXxx makes no
// further allocations (above whatever its given io.Writer makes, if any).
//
// To create one APNG, call EncodeHeader once (with a given numFrames > 0
// argument) and then EncodeFrame numFrames times.
//
// It can be re-used (but it is not thread-safe). One AnimationEncoder can make
// multiple (1 EncodeHeader + N EncodeFrame) call sequences, potentially with
// different N values, each producing a complete, stand-alone APNG image.
type AnimationEncoder struct {
enc Encoder
depth Depth
colorType ColorType
width uint32
height uint32
seqNum uint32
numFrames uint32
curFrame uint32
}
// EncodeHeader writes the APNG header to w. It makes no allocations above
// whatever w.Write makes, if any.
//
// The arguments are similar to Encoder.Encode, as well as numFrames > 0 being
// the number of animation frames and numPlays being the number of times each
// animation frame is played (zero means to loop forever).
func (e *AnimationEncoder) EncodeHeader(
w io.Writer,
depth Depth,
colorType ColorType,
width int,
height int,
numFrames uint32,
numPlays uint32) error {
if err := checkArguments(depth, colorType, width, height, numFrames, numPlays); err != nil {
return err
}
e.depth = depth
e.colorType = colorType
e.width = uint32(width)
e.height = uint32(height)
e.seqNum = 0
e.numFrames = numFrames
e.curFrame = 0
ej := e.enc.encodeImageHeader(depth, colorType, width, height, numFrames, numPlays)
_, err := w.Write(e.enc.buf[:ej])
return err
}
// EncodeFrame writes an animation frame to w. It makes no allocations above
// whatever w.Write makes, if any.
//
// The arguments are similar to Encoder.Encode, as well as (delayNumerator /
// delayDenominator) being how many seconds to display the frame. For example,
// if delayDenominator == 1000, delayNumerator is measured in milliseconds.
func (e *AnimationEncoder) EncodeFrame(
w io.Writer,
pix []byte,
stride int,
delayNumerator uint16,
delayDenominator uint16) error {
if e.numFrames == 0 {
return errors.New("uncompng: EncodeHeader was not called")
} else if e.curFrame >= e.numFrames {
return errors.New("uncompng: too many EncodeFrame calls")
} else if e.seqNum >= 0x80000000 {
return errors.New("uncompng: too many frames")
}
fType := frameTypeBitAnimated
if e.curFrame == 0 {
fType |= frameTypeBitFirstFrame
}
e.curFrame++
if e.curFrame == e.numFrames {
fType |= frameTypeBitLastFrame
}
ej := e.encodeFrameHeader(w, delayNumerator, delayDenominator)
return e.enc.encodeFramePayload(
w, fType, &e.seqNum, e.depth, e.colorType, int(e.width), int(e.height), pix, stride, ej)
}
func (e *AnimationEncoder) encodeFrameHeader(
w io.Writer,
delayNumerator uint16,
delayDenominator uint16) int {
// fcTL chunk length.
e.enc.buf[0x0000] = 0
e.enc.buf[0x0001] = 0
e.enc.buf[0x0002] = 0
e.enc.buf[0x0003] = 0x1A
// fcTL chunk type.
e.enc.buf[0x0004] = 'f'
e.enc.buf[0x0005] = 'c'
e.enc.buf[0x0006] = 'T'
e.enc.buf[0x0007] = 'L'
// fcTL payload.
e.enc.buf[0x0008] = byte(e.seqNum >> 24)
e.enc.buf[0x0009] = byte(e.seqNum >> 16)
e.enc.buf[0x000A] = byte(e.seqNum >> 8)
e.enc.buf[0x000B] = byte(e.seqNum >> 0)
e.enc.buf[0x000C] = byte(e.width >> 24)
e.enc.buf[0x000D] = byte(e.width >> 16)
e.enc.buf[0x000E] = byte(e.width >> 8)
e.enc.buf[0x000F] = byte(e.width >> 0)
e.enc.buf[0x0010] = byte(e.height >> 24)
e.enc.buf[0x0011] = byte(e.height >> 16)
e.enc.buf[0x0012] = byte(e.height >> 8)
e.enc.buf[0x0013] = byte(e.height >> 0)
e.enc.buf[0x0014] = 0 // X offset.
e.enc.buf[0x0015] = 0
e.enc.buf[0x0016] = 0
e.enc.buf[0x0017] = 0
e.enc.buf[0x0018] = 0 // Y offset.
e.enc.buf[0x0019] = 0
e.enc.buf[0x001A] = 0
e.enc.buf[0x001B] = 0
e.enc.buf[0x001C] = byte(delayNumerator >> 8)
e.enc.buf[0x001D] = byte(delayNumerator >> 0)
e.enc.buf[0x001E] = byte(delayDenominator >> 8)
e.enc.buf[0x001F] = byte(delayDenominator >> 0)
e.enc.buf[0x0020] = 0 // APNG_DISPOSE_OP_NONE.
e.enc.buf[0x0021] = 0 // APNG_BLEND_OP_SOURCE.
// fcTL CRC-32/IEEE checksum.
fctlCRC32 := crc32IEEE(e.enc.buf[0x0004:0x0022])
e.enc.buf[0x0022] = byte(fctlCRC32 >> 24)
e.enc.buf[0x0023] = byte(fctlCRC32 >> 16)
e.enc.buf[0x0024] = byte(fctlCRC32 >> 8)
e.enc.buf[0x0025] = byte(fctlCRC32 >> 0)
useIDAT := 0
if e.seqNum == 0 {
useIDAT = 1
}
e.seqNum++
// IDAT/fdAT chunk length placeholder. idatChunkStart is 0x0026.
e.enc.buf[0x0026] = 0
e.enc.buf[0x0027] = 0
e.enc.buf[0x0028] = 0
e.enc.buf[0x0029] = 0
// IDAT/fdAT chunk type.
e.enc.buf[0x002A] = "fI"[useIDAT]
e.enc.buf[0x002B] = "dD"[useIDAT]
e.enc.buf[0x002C] = 'A'
e.enc.buf[0x002D] = 'T'
ej := 0x002E
if useIDAT == 0 {
e.enc.buf[0x002E] = byte(e.seqNum >> 24)
e.enc.buf[0x002F] = byte(e.seqNum >> 16)
e.enc.buf[0x0030] = byte(e.seqNum >> 8)
e.enc.buf[0x0031] = byte(e.seqNum >> 0)
e.seqNum++
ej = 0x0032
}
// ZLIB header: CM=8, CINFO=7, FDICT=0, FLEVEL=0. See RFC 1950.
e.enc.buf[ej+0x00] = 0x78
e.enc.buf[ej+0x01] = 0x01
// DEFLATE uncompressed block header, with 4-byte placeholder for length.
e.enc.buf[ej+0x02] = 0
e.enc.buf[ej+0x03] = 0
e.enc.buf[ej+0x04] = 0
e.enc.buf[ej+0x05] = 0
e.enc.buf[ej+0x06] = 0
return ej + 0x07
}
func checkArguments(
depth Depth,
colorType ColorType,
width int,
height int,
numFrames uint32,
numPlays uint32) error {
if ((depth != Depth8) && (depth != Depth16)) ||
(colorType.pngFileFormatEncoding() == 0xFF) ||
(width <= 0) || (height <= 0) || (numFrames <= 0) {
return errors.New("uncompng: invalid argument")
} else if (width > 0xFFFFFF) || (height > 0xFFFFFF) {
return errors.New("uncompng: unsupported image size")
}
return nil
}
func btou8(a bool) uint8 {
func btou8(
a bool) uint8 {
if a {
return 1
}
return 0
}
func crc32IEEE(b []byte) uint32 {
func crc32IEEE(
b []byte) uint32 {
hash := uint32(0xFFFF_FFFF)
for _, v := range b {
hash = crc32IEEETable[uint8(hash)^v] ^ (hash >> 8)
+306 -10
View File
@@ -12,6 +12,8 @@ package uncompng
import (
"bytes"
"compress/zlib"
"hash/crc32"
"image"
"image/draw"
"image/png"
@@ -26,39 +28,39 @@ func encodeImage(w io.Writer, src image.Image) error {
switch src := src.(type) {
case *image.Gray:
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth8, ColorTypeGray)
return e.Encode(w, Depth8, ColorTypeGray, b.Dx(), b.Dy(), src.Pix, src.Stride)
case *image.Gray16:
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth16, ColorTypeGray)
return e.Encode(w, Depth16, ColorTypeGray, b.Dx(), b.Dy(), src.Pix, src.Stride)
case *image.RGBA:
if src.Opaque() {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth8, ColorTypeRGBX)
return e.Encode(w, Depth8, ColorTypeRGBX, b.Dx(), b.Dy(), src.Pix, src.Stride)
}
case *image.RGBA64:
if src.Opaque() {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth16, ColorTypeRGBX)
return e.Encode(w, Depth16, ColorTypeRGBX, b.Dx(), b.Dy(), src.Pix, src.Stride)
}
case *image.NRGBA:
if src.Opaque() {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth8, ColorTypeRGBX)
return e.Encode(w, Depth8, ColorTypeRGBX, b.Dx(), b.Dy(), src.Pix, src.Stride)
} else {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth8, ColorTypeNRGBA)
return e.Encode(w, Depth8, ColorTypeNRGBA, b.Dx(), b.Dy(), src.Pix, src.Stride)
}
case *image.NRGBA64:
if src.Opaque() {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth16, ColorTypeRGBX)
return e.Encode(w, Depth16, ColorTypeRGBX, b.Dx(), b.Dy(), src.Pix, src.Stride)
} else {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth16, ColorTypeNRGBA)
return e.Encode(w, Depth16, ColorTypeNRGBA, b.Dx(), b.Dy(), src.Pix, src.Stride)
}
}
tmp := image.NewNRGBA(b)
draw.Draw(tmp, b, src, b.Min, draw.Src)
return e.Encode(w, tmp.Pix, b.Dx(), b.Dy(), tmp.Stride, Depth8, ColorTypeNRGBA)
return e.Encode(w, Depth8, ColorTypeNRGBA, b.Dx(), b.Dy(), tmp.Pix, tmp.Stride)
}
func getPix(m image.Image) []byte {
@@ -144,7 +146,7 @@ func TestNoAllocation(tt *testing.T) {
enc := Encoder{}
pix := make([]byte, 4*3*5)
got := testing.AllocsPerRun(100, func() {
enc.Encode(io.Discard, pix, 3, 5, 4*3, Depth8, ColorTypeNRGBA)
enc.Encode(io.Discard, Depth8, ColorTypeNRGBA, 3, 5, pix, 4*3)
})
if got != 0 {
tt.Errorf("AllocsPerRun: got %v, want 0", got)
@@ -184,3 +186,297 @@ func TestWriteSeparateIENDChunk(tt *testing.T) {
tt.Fatalf("have not seen a separate IEND chunk")
}
}
// TestAnimationEncoder recreates the "Example NIA File" animation from
// doc/spec/nie-spec.md, but in APNG format, not NIA format:
//
// This animated image is 3 pixels wide and 2 pixels high. It consists of 20
// frames, being 10 loops of 2 frames. The total animation time of a single
// loop is 3 seconds, so the 10 loops will take 30 seconds. The first frame is
// a crude approximation to the French flag (blue, white and red) and is shown
// for 1 second. The next frame is a crude approximation to the Italian flag
// (green, white and red) and is shown for (3 - 1) seconds (i.e., 2 seconds).
//
// This test just hard-codes the expected golden output (in APNG format).
// Saving the output to a file instead, and then transforming its contents via
// convert-to-nia, should recover the same bytes (in NIA format) as given in
// doc/spec/nie-spec.md.
func TestAnimationEncoderSmall(tt *testing.T) {
want := "" +
"\x89PNG\x0D\x0A\x1A\x0A" + // PNG magic signature.
"" +
"\x00\x00\x00\x0DIHDR" + // Chunk length and type.
"\x00\x00\x00\x03" + // Width.
"\x00\x00\x00\x02" + // Height.
"\x08\x02\x00\x00\x00" + // Depth and other fields.
"\x12\x16\xF1\x4D" + // Chunk checksum (CRC-32).
"" +
"\x00\x00\x00\x08acTL" + // Chunk length and type.
"\x00\x00\x00\x02" + // 2 frames.
"\x00\x00\x00\x0A" + // 10 plays.
"\x13\x58\x7A\x6E" + // Chunk checksum (CRC-32).
"" + //
"\x00\x00\x00\x1AfcTL" + // Chunk length and type.
"\x00\x00\x00\x00" + // Animation sequence number.
"\x00\x00\x00\x03" + // Width.
"\x00\x00\x00\x02" + // Height.
"\x00\x00\x00\x00" + // XOffset.
"\x00\x00\x00\x00" + // YOffset.
"\x03\xE8\x03\xE8" + // Delay (numerator = 1000, denominator = 1000).
"\x00\x00" + // DisposeOp, BlendOp.
"\x24\x35\x20\x78" + // Chunk checksum (CRC-32).
"" + //
"\x00\x00\x00\x1FIDAT" + // Chunk length and type.
"\x78\x01\x01\x14\x00\xEB\xFF" + // ZLIB, final block, 20 bytes.
"\x00" + // PNG row filter.
"\x00\x00\xFF" + // RGB pixel.
"\xFF\xFF\xFF" + // RGB pixel.
"\xFF\x00\x00" + // RGB pixel.
"\x00" + // PNG row filter.
"\x00\x00\xFF" + // RGB pixel.
"\xFF\xFF\xFF" + // RGB pixel.
"\xFF\x00\x00" + // RGB pixel.
"\x63\xB0\x09\xF7" + // ZLIB checksum (Adler32).
"\xDD\xD2\x78\x8A" + // Chunk checksum (CRC-32).
"" + //
"\x00\x00\x00\x1AfcTL" + // Chunk length and type.
"\x00\x00\x00\x01" + // Animation sequence number.
"\x00\x00\x00\x03" + // Width.
"\x00\x00\x00\x02" + // Height.
"\x00\x00\x00\x00" + // XOffset.
"\x00\x00\x00\x00" + // YOffset.
"\x07\xD0\x03\xE8" + // Delay (numerator = 2000, denominator = 1000).
"\x00\x00" + // DisposeOp, BlendOp.
"\xB5\x86\x7B\xFD" + // Chunk checksum (CRC-32).
"" + //
"\x00\x00\x00\x23fdAT" + // Chunk length and type.
"\x00\x00\x00\x02" + // Animation sequence number.
"\x78\x01\x01\x14\x00\xEB\xFF" + // ZLIB, final block, 20 bytes.
"\x00" + // PNG row filter.
"\x00\xFF\x00" + // RGB pixel.
"\xFF\xFF\xFF" + // RGB pixel.
"\xFF\x00\x00" + // RGB pixel.
"\x00" + // PNG row filter.
"\x00\xFF\x00" + // RGB pixel.
"\xFF\xFF\xFF" + // RGB pixel.
"\xFF\x00\x00" + // RGB pixel.
"\x65\xAE\x09\xF7" + // ZLIB checksum (Adler32).
"\xFA\x44\x2A\xBE" + // Chunk checksum (CRC-32).
"" + //
"\x00\x00\x00\x00IEND" + // Chunk length and type.
"\xAE\x42\x60\x82" + // Chunk checksum (CRC-32).
""
const bytesPerPixel = 4
const width = 3
const height = 2
const numFrames = 2
const numPlays = 10
buf := bytes.Buffer{}
e := AnimationEncoder{}
pixs := [numFrames][bytesPerPixel * width * height]byte{{
0x00, 0x00, 0xFF, 0xFF, // Blue.
0xFF, 0xFF, 0xFF, 0xFF, // White.
0xFF, 0x00, 0x00, 0xFF, // Red.
0x00, 0x00, 0xFF, 0xFF, // Blue.
0xFF, 0xFF, 0xFF, 0xFF, // White.
0xFF, 0x00, 0x00, 0xFF, // Red.
}, {
0x00, 0xFF, 0x00, 0xFF, // Green.
0xFF, 0xFF, 0xFF, 0xFF, // White.
0xFF, 0x00, 0x00, 0xFF, // Red.
0x00, 0xFF, 0x00, 0xFF, // Green.
0xFF, 0xFF, 0xFF, 0xFF, // White.
0xFF, 0x00, 0x00, 0xFF, // Red.
}}
delayMillis := [numFrames]uint16{1000, 2000}
if err := e.EncodeHeader(&buf, Depth8, ColorTypeRGBX, width, height, numFrames, numPlays); err != nil {
tt.Fatalf("EncodeHeader: %v", err)
}
for f := range pixs {
if err := e.EncodeFrame(&buf, pixs[f][:], bytesPerPixel*width, delayMillis[f], 1000); err != nil {
tt.Fatalf("EncodeFrame(%d): %v", f, err)
}
}
asBytes := buf.Bytes()
checkAPNGConsistency(tt, asBytes)
if got := string(asBytes); got != want {
for i := range min(len(got), len(want)) {
if got[i] != want[i] {
tt.Fatalf("compare-to-golden: at byte %d\ngot % 02X\nwant % 02X", i, got, want)
}
}
tt.Fatalf("compare-to-golden:\ngot % 02X\nwant % 02X", got, want)
}
}
func TestAnimationEncoderLarge(tt *testing.T) {
srcFile, err := os.Open("../../test/data/hibiscus.regular.png")
if err != nil {
tt.Fatalf("os.Open: %v", err)
}
defer srcFile.Close()
srcImage, err := png.Decode(srcFile)
if err != nil {
tt.Fatalf("png.Decode: %v", err)
}
srcRGBA := srcImage.(*image.RGBA)
if got, want := srcRGBA.Bounds(), image.Rect(0, 0, 312, 442); got != want {
tt.Fatalf("Bounds: got %v, want %v", got, want)
}
srcPix := srcRGBA.Pix
// Make a copy of srcPix. PNG alpha is non-premul and Go's RGBA alpha is
// premul. The fact that Go's png.Decode returned RGBA (instead of NRGBA)
// means that all of the alpha values should be 0xFF, so that converting
// srcPix (as RGBA) to srcPixAsNRGBA can just be a memcpy.
srcPixAsNRGBA := append([]byte(nil), srcPix...)
buf := bytes.Buffer{}
e := AnimationEncoder{}
const bytesPerPixel = 4
const width = 312
const height = 442
const numFrames = 5
const numPlays = 0
delayMillis := [numFrames]uint16{300, 500, 600, 400, 200}
if err := e.EncodeHeader(&buf, Depth8, ColorTypeNRGBA, width, height, numFrames, numPlays); err != nil {
tt.Fatalf("EncodeHeader: %v", err)
}
for f, delay := range delayMillis {
if err := e.EncodeFrame(&buf, srcPix, bytesPerPixel*width, delay, 1000); err != nil {
tt.Fatalf("EncodeFrame(%d): %v", f, err)
}
// Over time, set the R, G, B, A channel values to 0x80.
if f < 4 {
for i := f; i < len(srcPix); i += 4 {
srcPix[i] = 0x80
}
}
}
asBytes := buf.Bytes()
checkAPNGConsistency(tt, asBytes)
if got, want := crc32.ChecksumIEEE(asBytes), uint32(0x73B25773); got != want {
tt.Fatalf("did not get golden-output hash")
}
// png.Decode from Go's standard library doesn't recover all of the
// animation's frames, but it should recover the first frame.
//
// We also passed ColorTypeNRGBA (not ColorTypeRGBX) to EncodeHeader, so
// the decoded image is *image.NRGBA, not *image.RGBA.
dstImage, err := png.Decode(bytes.NewReader(asBytes))
if err != nil {
tt.Fatalf("png.Decode: %v", err)
}
dstNRGBA := dstImage.(*image.NRGBA)
if !bytes.Equal(dstNRGBA.Pix, srcPixAsNRGBA) {
tt.Fatal("first frame pixels: round-trip failed")
}
// Change false to true, for manual inspection in a viewer that supports
// APNG, such as a web browser.
if false {
os.WriteFile("/tmp/TestAnimationEncoderLarge.png", asBytes, 0644)
}
}
func checkAPNGConsistency(tt *testing.T, pngEncodedBytes []byte) {
if !bytes.HasPrefix(pngEncodedBytes, []byte("\x89PNG\x0D\x0A\x1A\x0A")) {
tt.Fatalf("bad PNG magic signature")
}
forEachChunk := func(f func(chunk []byte)) {
for b := pngEncodedBytes[8:]; len(b) > 0; {
if len(b) < 12 {
tt.Fatalf("bad chunk structure")
}
n := int64(u32be(b))
if (n + 12) > int64(len(b)) {
tt.Fatalf("bad chunk structure")
}
f(b[:n+12])
b = b[n+12:]
}
}
forEachChunk(func(chunk []byte) {
checksum0 := u32be(chunk[len(chunk)-4:])
checksum1 := crc32.ChecksumIEEE(chunk[4 : len(chunk)-4])
if checksum0 != checksum1 {
tt.Fatalf("bad chunk checksum")
}
})
nextSeqNum := uint32(0)
forEachChunk(func(chunk []byte) {
switch u32be(chunk[4:]) {
case 0x6663544C, 0x66644154: // "fcTL"be, "fdAT"be.
got := u32be(chunk[8:])
want := nextSeqNum
nextSeqNum++
if got != want {
tt.Fatalf("bad animation sequence number")
}
}
})
idatlikeData := [][]byte{}
forEachChunk(func(chunk []byte) {
dat := []byte(nil)
switch u32be(chunk[4:]) {
case 0x6663544C: // "fcTL"be.
idatlikeData = append(idatlikeData, nil)
case 0x66644154: // "fdAT"be.
dat = chunk[12 : len(chunk)-4]
case 0x49444154: // "IDAT"be.
dat = chunk[8 : len(chunk)-4]
}
if dat != nil {
last := len(idatlikeData) - 1
idatlikeData[last] = append(idatlikeData[last], dat...)
}
})
decompressedLen := int64(0)
for i, dat := range idatlikeData {
r, err := zlib.NewReader(bytes.NewReader(dat))
if err != nil {
tt.Fatalf("IDAT/fdAT chunk data was not ZLIB-formatted")
}
defer r.Close()
if n, err := io.Copy(io.Discard, r); err != nil {
tt.Fatalf("IDAT/fdAT chunk data was not ZLIB-formatted")
} else if i == 0 {
decompressedLen = n
} else if decompressedLen != n {
tt.Fatalf("IDAT/fdAT chunk data has inconsistent lengths")
}
}
}
func u32be(b []byte) uint32 {
return (uint32(b[0]) << 24) |
(uint32(b[1]) << 16) |
(uint32(b[2]) << 8) |
(uint32(b[3]) << 0)
}
+2811 -1157
View File
@@ -1752,7 +1752,7 @@ wuffs_base__make_slice_u64(uint64_t* ptr, size_t len) {
static inline wuffs_base__slice_u8 //
wuffs_base__make_slice_u8_ij(uint8_t* ptr, size_t i, size_t j) {
wuffs_base__slice_u8 ret;
ret.ptr = ptr ? (ptr + i) : NULL;
ret.ptr = i ? (ptr + i) : ptr;
ret.len = (j >= i) ? (j - i) : 0;
return ret;
}
@@ -1760,7 +1760,7 @@ wuffs_base__make_slice_u8_ij(uint8_t* ptr, size_t i, size_t j) {
static inline wuffs_base__slice_u16 //
wuffs_base__make_slice_u16_ij(uint16_t* ptr, size_t i, size_t j) {
wuffs_base__slice_u16 ret;
ret.ptr = ptr ? (ptr + i) : NULL;
ret.ptr = i ? (ptr + i) : ptr;
ret.len = (j >= i) ? (j - i) : 0;
return ret;
}
@@ -1768,7 +1768,7 @@ wuffs_base__make_slice_u16_ij(uint16_t* ptr, size_t i, size_t j) {
static inline wuffs_base__slice_u32 //
wuffs_base__make_slice_u32_ij(uint32_t* ptr, size_t i, size_t j) {
wuffs_base__slice_u32 ret;
ret.ptr = ptr ? (ptr + i) : NULL;
ret.ptr = i ? (ptr + i) : ptr;
ret.len = (j >= i) ? (j - i) : 0;
return ret;
}
@@ -1776,7 +1776,7 @@ wuffs_base__make_slice_u32_ij(uint32_t* ptr, size_t i, size_t j) {
static inline wuffs_base__slice_u64 //
wuffs_base__make_slice_u64_ij(uint64_t* ptr, size_t i, size_t j) {
wuffs_base__slice_u64 ret;
ret.ptr = ptr ? (ptr + i) : NULL;
ret.ptr = i ? (ptr + i) : ptr;
ret.len = (j >= i) ? (j - i) : 0;
return ret;
}
@@ -1917,7 +1917,12 @@ wuffs_base__slice_u8__overlaps(wuffs_base__slice_u8 s, wuffs_base__slice_u8 t) {
static inline wuffs_base__slice_u8 //
wuffs_base__slice_u8__subslice_i(wuffs_base__slice_u8 s, uint64_t i) {
if ((i <= SIZE_MAX) && (i <= s.len)) {
return wuffs_base__make_slice_u8(s.ptr + i, ((size_t)(s.len - i)));
// The "i ? (s.ptr + i) : s.ptr" expression is equivalent to the plain
// "s.ptr + i", except for the fact that in C (prior to C29), but not in
// C++, "NULL + 0" is, technically, undefined behavior (and triggers
// ubsan). See C29 N3322 and https://reviews.llvm.org/D67122
return wuffs_base__make_slice_u8(i ? (s.ptr + i) : s.ptr,
((size_t)(s.len - i)));
}
return wuffs_base__empty_slice_u8();
}
@@ -1941,7 +1946,12 @@ wuffs_base__slice_u8__subslice_ij(wuffs_base__slice_u8 s,
uint64_t i,
uint64_t j) {
if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {
return wuffs_base__make_slice_u8(s.ptr + i, ((size_t)(j - i)));
// The "i ? (s.ptr + i) : s.ptr" expression is equivalent to the plain
// "s.ptr + i", except for the fact that in C (prior to C29), but not in
// C++, "NULL + 0" is, technically, undefined behavior (and triggers
// ubsan). See C29 N3322 and https://reviews.llvm.org/D67122
return wuffs_base__make_slice_u8(i ? (s.ptr + i) : s.ptr,
((size_t)(j - i)));
}
return wuffs_base__empty_slice_u8();
}
@@ -4376,6 +4386,10 @@ wuffs_base__malloc_slice_u64(void* (*malloc_func)(size_t), uint64_t num_u64) {
// 0xAARRGGBB (Alpha most significant, Blue least), regardless of endianness.
typedef uint32_t wuffs_base__color_u32_argb_premul;
// wuffs_base__color_u32_argb_nonpremul is non-premultiplied version of
// wuffs_base__color_u32_argb_premul.
typedef uint32_t wuffs_base__color_u32_argb_nonpremul;
// wuffs_base__color_u32_argb_premul__is_valid returns whether c's Red, Green
// and Blue channels are all less than or equal to its Alpha channel. c uses
// premultiplied alpha, so 50% opaque 100% saturated red is 0x7F7F_0000 and a
@@ -4595,6 +4609,21 @@ wuffs_base__color_u64_argb_premul__as__color_u64_argb_nonpremul(
return (a << 48) | (r << 32) | (g << 16) | (b << 0);
}
static inline uint64_t //
wuffs_base__color_u64_argb_nonpremul__as__color_u64_argb_premul(
uint64_t argb_nonpremul) {
uint64_t a = 0xFFFF & (argb_nonpremul >> 48);
uint64_t r = 0xFFFF & (argb_nonpremul >> 32);
r = (r * a) / 0xFFFF;
uint64_t g = 0xFFFF & (argb_nonpremul >> 16);
g = (g * a) / 0xFFFF;
uint64_t b = 0xFFFF & (argb_nonpremul >> 0);
b = (b * a) / 0xFFFF;
return (a << 48) | (r << 32) | (g << 16) | (b << 0);
}
static inline uint8_t //
wuffs_base__color_u64_argb_nonpremul__as__color_u8_gray(
uint64_t argb_nonpremul) {
@@ -4826,6 +4855,11 @@ typedef uint8_t wuffs_base__pixel_blend;
#define WUFFS_BASE__PIXEL_BLEND__SRC ((wuffs_base__pixel_blend)0)
#define WUFFS_BASE__PIXEL_BLEND__SRC_OVER ((wuffs_base__pixel_blend)1)
static inline wuffs_base__pixel_blend //
wuffs_base__make_pixel_blend(uint8_t repr) {
return repr;
}
// --------
// wuffs_base__pixel_alpha_transparency is a pixel format's alpha channel
@@ -5207,13 +5241,21 @@ wuffs_base__pixel_subsampling::denominator_y(uint32_t plane) const {
//
// For CMY, C = (100% - R), M = (100% - G) and Y = (100% - B). Black (K), if
// present, then modulates the three RGB values.
//
// ----
//
// The 0x02 bit means the fourth channel is an alpha channel and, if set, then
// the 0x01 bit gives the blend mode (WUFFS_BASE__PIXEL_BLEND__SRC is 0,
// WUFFS_BASE__PIXEL_BLEND__SRC_OVER is 1).
// clang-format off
#define WUFFS_BASE__YCC_MODEL__BT_601_FULL_RANGE 0x00
#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE 0x01
#define WUFFS_BASE__YCC_MODEL__RGB 0x40
#define WUFFS_BASE__YCC_MODEL__CMY 0x80
#define WUFFS_BASE__YCC_MODEL__BT_601_FULL_RANGE 0x00
#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE 0x04
#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC 0x06
#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC_OVER 0x07
#define WUFFS_BASE__YCC_MODEL__RGB 0x40
#define WUFFS_BASE__YCC_MODEL__CMY 0x80
// clang-format on
@@ -5839,15 +5881,32 @@ typedef struct wuffs_base__pixel_buffer__struct {
inline wuffs_base__slice_u8 palette_or_else(wuffs_base__slice_u8 fallback);
inline wuffs_base__pixel_format pixel_format() const;
inline wuffs_base__table_u8 plane(uint32_t p);
// Deprecated: use color_u32_argb_premul_at.
inline wuffs_base__color_u32_argb_premul color_u32_at(uint32_t x,
uint32_t y) const;
inline wuffs_base__color_u32_argb_premul color_u32_argb_premul_at(
uint32_t x,
uint32_t y) const;
// Deprecated: use set_color_u32_argb_premul_at.
inline wuffs_base__status set_color_u32_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color);
inline wuffs_base__status set_color_u32_argb_premul_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color);
// Deprecated: use set_color_u32_argb_premul_fill_rect.
inline wuffs_base__status set_color_u32_fill_rect(
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color);
inline wuffs_base__status set_color_u32_argb_premul_fill_rect(
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color);
inline wuffs_base__status set_color_u32_argb_nonpremul_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color);
inline bool is_opaque();
#endif // __cplusplus
@@ -6042,24 +6101,44 @@ wuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* pb, uint32_t p) {
return ret;
}
// Deprecated: use the second name, not the first.
#define wuffs_base__pixel_buffer__color_u32_at \
wuffs_base__pixel_buffer__color_u32_argb_premul_at
// Deprecated: use the second name, not the first.
#define wuffs_base__pixel_buffer__set_color_u32_at \
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at
// Deprecated: use the second name, not the first.
#define wuffs_base__pixel_buffer__set_color_u32_fill_rect \
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect
WUFFS_BASE__MAYBE_STATIC wuffs_base__color_u32_argb_premul //
wuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y);
wuffs_base__pixel_buffer__color_u32_argb_premul_at(
const wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y);
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_at(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color);
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_fill_rect(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
wuffs_base__pixel_buffer* pb,
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color);
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_argb_nonpremul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color);
WUFFS_BASE__MAYBE_STATIC bool //
wuffs_base__pixel_buffer__is_opaque(const wuffs_base__pixel_buffer* pb);
@@ -6104,7 +6183,13 @@ wuffs_base__pixel_buffer::plane(uint32_t p) {
inline wuffs_base__color_u32_argb_premul //
wuffs_base__pixel_buffer::color_u32_at(uint32_t x, uint32_t y) const {
return wuffs_base__pixel_buffer__color_u32_at(this, x, y);
return wuffs_base__pixel_buffer__color_u32_argb_premul_at(this, x, y);
}
inline wuffs_base__color_u32_argb_premul //
wuffs_base__pixel_buffer::color_u32_argb_premul_at(uint32_t x,
uint32_t y) const {
return wuffs_base__pixel_buffer__color_u32_argb_premul_at(this, x, y);
}
inline wuffs_base__status //
@@ -6112,14 +6197,42 @@ wuffs_base__pixel_buffer::set_color_u32_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color) {
return wuffs_base__pixel_buffer__set_color_u32_at(this, x, y, color);
return wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(this, x, y,
color);
}
inline wuffs_base__status //
wuffs_base__pixel_buffer::set_color_u32_argb_premul_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_premul color) {
return wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(this, x, y,
color);
}
inline wuffs_base__status //
wuffs_base__pixel_buffer::set_color_u32_fill_rect(
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color) {
return wuffs_base__pixel_buffer__set_color_u32_fill_rect(this, rect, color);
return wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
this, rect, color);
}
inline wuffs_base__status //
wuffs_base__pixel_buffer::set_color_u32_argb_premul_fill_rect(
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color) {
return wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
this, rect, color);
}
inline wuffs_base__status //
wuffs_base__pixel_buffer::set_color_u32_argb_nonpremul_at(
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color) {
return wuffs_base__pixel_buffer__set_color_u32_argb_nonpremul_at(this, x, y,
color);
}
inline bool //
@@ -15457,7 +15570,9 @@ struct wuffs_thumbhash__decoder__struct {
// ---------------- Status Codes
extern const char wuffs_vp8__error__bad_quirk_source_length[];
extern const char wuffs_vp8__error__bad_quirk_width_and_height[];
extern const char wuffs_vp8__error__bad_header[];
extern const char wuffs_vp8__error__inconsistent_width_and_height[];
extern const char wuffs_vp8__error__missing_quirk_source_length[];
extern const char wuffs_vp8__error__truncated_input[];
extern const char wuffs_vp8__error__unsupported_quirk_source_length[];
@@ -15465,7 +15580,11 @@ extern const char wuffs_vp8__error__unsupported_vp8_file[];
// ---------------- Public Consts
#define WUFFS_VP8__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 553648127u
#define WUFFS_VP8__QUIRK_HAS_ALPHA_SIDE_CHANNEL 1836840960u
#define WUFFS_VP8__QUIRK_WIDTH_AND_HEIGHT 1836840961u
#define WUFFS_VP8__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 822050816u
// ---------------- Struct Declarations
@@ -15636,8 +15755,10 @@ struct wuffs_vp8__decoder__struct {
uint32_t f_mbh;
uint8_t f_call_sequence;
bool f_is_key_frame;
bool f_quirk_has_alpha_side_channel;
uint64_t f_frame_config_io_position;
uint64_t f_quirk_source_length;
uint64_t f_quirk_width_and_height;
uint32_t f_partitioned_data_length;
uint32_t f_workbuf_yuv_y_stride;
uint32_t f_workbuf_yuv_uv_stride;
@@ -16214,18 +16335,20 @@ struct wuffs_wbmp__decoder__struct {
// ---------------- Status Codes
extern const char wuffs_webp__error__bad_alph_vp8l_combination[];
extern const char wuffs_webp__error__bad_huffman_code_over_subscribed[];
extern const char wuffs_webp__error__bad_huffman_code_under_subscribed[];
extern const char wuffs_webp__error__bad_huffman_code[];
extern const char wuffs_webp__error__bad_vp8x_chunk[];
extern const char wuffs_webp__error__bad_back_reference[];
extern const char wuffs_webp__error__bad_chunk_order[];
extern const char wuffs_webp__error__bad_color_cache[];
extern const char wuffs_webp__error__bad_header[];
extern const char wuffs_webp__error__bad_transform[];
extern const char wuffs_webp__error__short_chunk[];
extern const char wuffs_webp__error__truncated_input[];
extern const char wuffs_webp__error__unsupported_number_of_huffman_groups[];
extern const char wuffs_webp__error__unsupported_transform_after_color_indexing_transform[];
extern const char wuffs_webp__error__unsupported_webp_file[];
extern const char wuffs_webp__error__unsupported_number_of_huffman_groups[];
// ---------------- Public Consts
@@ -16394,16 +16517,19 @@ struct wuffs_webp__decoder__struct {
wuffs_base__vtable vtable_for__wuffs_base__image_decoder;
wuffs_base__vtable null_vtable;
uint32_t f_pixfmt;
uint32_t f_width;
uint32_t f_height;
uint8_t f_call_sequence;
uint8_t f_code_length_code_lengths[19];
bool f_sub_chunk_has_padding;
bool f_is_vp8_lossy;
uint8_t f_variant;
uint8_t f_vp8x_header_flags;
uint8_t f_vp8x_alph_flags;
uint64_t f_frame_config_io_position;
uint32_t f_riff_chunk_length;
uint32_t f_rcl_remaining;
uint32_t f_sub_chunk_length;
uint32_t f_sub_chunk_type;
uint32_t f_bits;
uint32_t f_n_bits;
bool f_seen_transform[4];
@@ -16422,24 +16548,28 @@ struct wuffs_webp__decoder__struct {
uint32_t f_workbuf_offset_for_color_indexing;
wuffs_base__pixel_swizzler f_swizzler;
uint32_t p_decode_alph;
uint32_t p_decode_alph_limited;
uint32_t p_decode_huffman_groups;
uint32_t p_decode_huffman_tree;
uint32_t p_decode_huffman_tree_simple;
uint32_t p_decode_code_length_code_lengths;
uint32_t p_build_code_lengths;
uint32_t p_decode_pixels_slow;
uint32_t p_decode_image_config;
uint32_t p_do_decode_image_config;
uint32_t p_do_decode_image_config_limited;
uint32_t p_do_decode_image_config_limited_vp8l;
uint32_t p_decode_frame_config;
uint32_t p_do_decode_frame_config;
uint32_t p_decode_frame;
uint32_t p_do_decode_frame;
uint32_t p_do_decode_frame_vp8l;
uint32_t p_decode_transform;
uint32_t p_decode_color_cache_parameters;
uint32_t p_decode_hg_table;
uint32_t p_decode_pixels;
uint32_t p_decode_image_config;
uint32_t p_do_decode_image_config;
uint32_t p_do_decode_image_config_limited;
uint32_t p_do_decode_image_config_limited_vp8l;
uint32_t p_do_decode_image_config_limited_vp8x;
uint32_t p_decode_frame_config;
uint32_t p_do_decode_frame_config;
uint32_t p_decode_frame;
uint32_t p_do_decode_frame;
} private_impl;
struct {
@@ -16490,6 +16620,16 @@ struct wuffs_webp__decoder__struct {
uint32_t v_back_ref_dist_premap_minus_1;
uint64_t v_color_cache_p;
} s_decode_pixels_slow;
struct {
uint32_t v_width;
} s_do_decode_frame_vp8l;
struct {
uint32_t v_transform_type;
uint32_t v_tile_size_log2;
} s_decode_transform;
struct {
uint32_t v_tile_size_log2;
} s_decode_hg_table;
struct {
uint64_t scratch;
} s_do_decode_image_config;
@@ -16500,15 +16640,11 @@ struct wuffs_webp__decoder__struct {
uint64_t scratch;
} s_do_decode_image_config_limited_vp8l;
struct {
uint32_t v_width;
uint64_t scratch;
} s_do_decode_image_config_limited_vp8x;
struct {
uint64_t scratch;
} s_do_decode_frame;
struct {
uint32_t v_transform_type;
uint32_t v_tile_size_log2;
} s_decode_transform;
struct {
uint32_t v_tile_size_log2;
} s_decode_hg_table;
} private_data;
#ifdef __cplusplus
@@ -17808,6 +17944,8 @@ class DecodeImageCallbacks {
// should be at least len_range.min_incl, but larger allocations (up to
// len_range.max_incl) may have better performance (by using more memory).
//
// The len_range argument will satisfy !len_range.is_empty().
//
// The default AllocWorkbuf implementation allocates len_range.max_incl bytes
// of either uninitialized or zeroed memory.
virtual AllocWorkbufResult //
@@ -19173,13 +19311,19 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
uint8_t v2,
uint8_t v3,
uint8_t ycc_model,
bool triangle_filter_for_2to1,
uint8_t ycc_upsampling,
wuffs_base__slice_u8 scratch_buffer_2k);
// ---------------- Images (Utility)
#define wuffs_base__utility__make_pixel_blend wuffs_base__make_pixel_blend
#define wuffs_base__utility__make_pixel_format wuffs_base__make_pixel_format
static inline uint8_t //
wuffs_base__utility__demake_pixel_blend(wuffs_base__pixel_blend a) {
return a;
}
// ---------------- String Conversions
// ---------------- Unicode and UTF-8
@@ -24318,6 +24462,26 @@ wuffs_private_impl__swizzle_xxxx__y__x86_sse42(uint8_t* dst_ptr,
// --------
static inline uint32_t //
wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(
uint32_t dst_nonpremul,
uint32_t src_nonpremul);
static inline uint64_t //
wuffs_private_impl__composite_nonpremul_nonpremul_u64_axxx(
uint64_t dst_nonpremul,
uint64_t src_nonpremul);
static inline uint32_t //
wuffs_private_impl__composite_premul_nonpremul_u32_axxx(uint32_t dst_premul,
uint32_t src_nonpremul);
static inline uint64_t //
wuffs_private_impl__composite_premul_nonpremul_u64_axxx(uint64_t dst_premul,
uint64_t src_nonpremul);
// --------
static inline uint32_t //
wuffs_private_impl__color_u64__as__color_u32__swap_u32_argb_abgr(uint64_t c) {
uint32_t a = ((uint32_t)(0xFF & (c >> 56)));
@@ -24425,7 +24589,7 @@ wuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* pb,
// --------
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_at(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
@@ -24538,6 +24702,365 @@ wuffs_base__pixel_buffer__set_color_u32_at(
return wuffs_base__make_status(NULL);
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_argb_nonpremul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color) {
if (!pb) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if ((x >= pb->pixcfg.private_impl.width) ||
(y >= pb->pixcfg.private_impl.height)) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if (wuffs_base__pixel_format__is_planar(&pb->pixcfg.private_impl.pixfmt)) {
// TODO: support planar formats.
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
size_t stride = pb->private_impl.planes[0].stride;
uint8_t* row = pb->private_impl.planes[0].ptr + (stride * ((size_t)y));
switch (pb->pixcfg.private_impl.pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__BGRX:
wuffs_base__poke_u32le__no_bounds_check(
row + (4 * ((size_t)x)),
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color));
break;
// Common formats above. Rarer formats below.
case WUFFS_BASE__PIXEL_FORMAT__Y:
wuffs_base__poke_u8__no_bounds_check(
row + ((size_t)x),
wuffs_base__color_u32_argb_premul__as__color_u8_gray(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__Y_16LE:
wuffs_base__poke_u16le__no_bounds_check(
row + (2 * ((size_t)x)),
wuffs_base__color_u32_argb_premul__as__color_u16_gray(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__Y_16BE:
wuffs_base__poke_u16be__no_bounds_check(
row + (2 * ((size_t)x)),
wuffs_base__color_u32_argb_premul__as__color_u16_gray(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__YA_NONPREMUL:
wuffs_base__poke_u8__no_bounds_check(
row + (2 * ((size_t)x)),
wuffs_base__color_u32_argb_premul__as__color_u8_gray(color |
0xFF000000));
wuffs_base__poke_u8__no_bounds_check(row + (2 * ((size_t)x)) + 1,
((uint8_t)(color >> 24)));
break;
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY:
wuffs_base__poke_u8__no_bounds_check(
row + ((size_t)x),
wuffs_base__pixel_palette__closest_element(
wuffs_base__pixel_buffer__palette(pb),
pb->pixcfg.private_impl.pixfmt,
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGR_565:
wuffs_base__poke_u16le__no_bounds_check(
row + (2 * ((size_t)x)),
wuffs_base__color_u32_argb_premul__as__color_u16_rgb_565(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGR:
wuffs_base__poke_u24le__no_bounds_check(
row + (3 * ((size_t)x)),
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
wuffs_base__poke_u32le__no_bounds_check(row + (4 * ((size_t)x)), color);
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE:
wuffs_base__poke_u64le__no_bounds_check(
row + (8 * ((size_t)x)), wuffs_base__color_u32__as__color_u64(color));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL_4X16LE:
wuffs_base__poke_u64le__no_bounds_check(
row + (8 * ((size_t)x)),
wuffs_base__color_u64_argb_nonpremul__as__color_u64_argb_premul(
wuffs_base__color_u32__as__color_u64(color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:
wuffs_base__poke_u32le__no_bounds_check(
row + (4 * ((size_t)x)),
(color >> 31)
? (wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color) |
0xFF000000)
: 0);
break;
case WUFFS_BASE__PIXEL_FORMAT__RGB:
wuffs_base__poke_u24le__no_bounds_check(
row + (3 * ((size_t)x)),
wuffs_base__color_swap_u32_argb_abgr(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:
wuffs_base__poke_u32le__no_bounds_check(
row + (4 * ((size_t)x)), wuffs_base__color_swap_u32_argb_abgr(color));
break;
case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__RGBX:
wuffs_base__poke_u32le__no_bounds_check(
row + (4 * ((size_t)x)),
wuffs_base__color_swap_u32_argb_abgr(
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color)));
break;
default:
// TODO: support more formats.
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
return wuffs_base__make_status(NULL);
}
static inline wuffs_base__status //
wuffs_base__pixel_buffer__composite_color_u32_nonpremul_at(
wuffs_base__pixel_buffer* pb,
uint32_t x,
uint32_t y,
wuffs_base__color_u32_argb_nonpremul color) {
if (!pb) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if ((x >= pb->pixcfg.private_impl.width) ||
(y >= pb->pixcfg.private_impl.height)) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if (wuffs_base__pixel_format__is_planar(&pb->pixcfg.private_impl.pixfmt)) {
// TODO: support planar formats.
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
size_t stride = pb->private_impl.planes[0].stride;
uint8_t* row = pb->private_impl.planes[0].ptr + (stride * ((size_t)y));
switch (pb->pixcfg.private_impl.pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRX: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr) | 0xFF000000;
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
// Common formats above. Rarer formats below.
case WUFFS_BASE__PIXEL_FORMAT__Y: {
uint8_t* ptr = row + ((size_t)x);
uint32_t dst = 0xFF000000 | (0x00010101 * ((uint32_t)(*ptr)));
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
*ptr = ((uint8_t)(dst));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__Y_16LE: {
uint8_t* ptr = row + (2 * ((size_t)x));
uint64_t dst =
0xFFFF000000000000 |
(0x000100010001 *
((uint64_t)(wuffs_base__peek_u16le__no_bounds_check(ptr))));
dst = wuffs_private_impl__composite_premul_nonpremul_u64_axxx(
dst, wuffs_base__color_u32__as__color_u64(color));
wuffs_base__poke_u16le__no_bounds_check(ptr, ((uint16_t)(dst)));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__Y_16BE: {
uint8_t* ptr = row + (2 * ((size_t)x));
uint64_t dst =
0xFFFF000000000000 |
(0x000100010001 *
((uint64_t)(wuffs_base__peek_u16be__no_bounds_check(ptr))));
dst = wuffs_private_impl__composite_premul_nonpremul_u64_axxx(
dst, wuffs_base__color_u32__as__color_u64(color));
wuffs_base__poke_u16be__no_bounds_check(ptr, ((uint16_t)(dst)));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__YA_NONPREMUL: {
uint8_t* ptr = row + (2 * ((size_t)x));
uint32_t dst =
(((uint32_t)(ptr[1])) << 24) | (((uint32_t)(ptr[0])) * 0x00010101);
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(dst,
color);
ptr[0] = wuffs_base__color_u32_argb_premul__as__color_u8_gray(dst |
0xFF000000);
ptr[1] = ((uint8_t)(dst >> 24));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: {
wuffs_base__slice_u8 palette = wuffs_base__pixel_buffer__palette(pb);
if (palette.len) {
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(
palette.ptr + (4 * ((size_t)row[x])));
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(dst,
color);
row[x] = wuffs_base__pixel_palette__closest_element(
wuffs_base__pixel_buffer__palette(pb),
pb->pixcfg.private_impl.pixfmt,
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
dst));
}
break;
}
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: {
wuffs_base__slice_u8 palette = wuffs_base__pixel_buffer__palette(pb);
if (palette.len) {
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(
palette.ptr + (4 * ((size_t)row[x])));
dst =
wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
row[x] = wuffs_base__pixel_palette__closest_element(
wuffs_base__pixel_buffer__palette(pb),
pb->pixcfg.private_impl.pixfmt, dst);
}
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGR_565: {
uint8_t* ptr = row + (2 * ((size_t)x));
uint32_t dst = wuffs_base__color_u16_rgb_565__as__color_u32_argb_premul(
wuffs_base__peek_u16le__no_bounds_check(ptr));
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
wuffs_base__poke_u16le__no_bounds_check(
ptr, wuffs_base__color_u32_argb_premul__as__color_u16_rgb_565(dst));
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGR: {
uint8_t* ptr = row + (3 * ((size_t)x));
uint32_t dst = 0xFF000000 | wuffs_base__peek_u24le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
wuffs_base__poke_u24le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(dst,
color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE: {
uint8_t* ptr = row + (8 * ((size_t)x));
uint64_t dst = wuffs_base__peek_u64le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u64_axxx(
dst, wuffs_base__color_u32__as__color_u64(color));
wuffs_base__poke_u64le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL_4X16LE: {
uint8_t* ptr = row + (8 * ((size_t)x));
uint64_t dst = wuffs_base__peek_u64le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u64_axxx(
dst, wuffs_base__color_u32__as__color_u64(color));
wuffs_base__poke_u64le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
if (dst) {
dst =
wuffs_private_impl__composite_premul_nonpremul_u32_axxx(dst, color);
} else if (color >> 31) {
dst = (wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
color) |
0xFF000000);
}
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__RGB: {
uint8_t* ptr = row + (3 * ((size_t)x));
uint32_t dst = 0xFF000000 | wuffs_base__peek_u24le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(
dst, wuffs_base__color_swap_u32_argb_abgr(color));
wuffs_base__poke_u24le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_nonpremul_nonpremul_u32_axxx(
dst, wuffs_base__color_swap_u32_argb_abgr(color));
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(
dst, wuffs_base__color_swap_u32_argb_abgr(color));
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
case WUFFS_BASE__PIXEL_FORMAT__RGBX: {
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst = wuffs_base__peek_u32le__no_bounds_check(ptr) | 0xFF000000;
dst = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(
dst, wuffs_base__color_swap_u32_argb_abgr(color));
wuffs_base__poke_u32le__no_bounds_check(ptr, dst);
break;
}
default:
// TODO: support more formats.
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
return wuffs_base__make_status(NULL);
}
// --------
static inline void //
@@ -24665,7 +25188,7 @@ wuffs_private_impl__pixel_buffer__set_color_u32_fill_rect__xxxxxxxx(
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__status //
wuffs_base__pixel_buffer__set_color_u32_fill_rect(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
wuffs_base__pixel_buffer* pb,
wuffs_base__rect_ie_u32 rect,
wuffs_base__color_u32_argb_premul color) {
@@ -24737,7 +25260,7 @@ wuffs_base__pixel_buffer__set_color_u32_fill_rect(
for (y = rect.min_incl_y; y < rect.max_excl_y; y++) {
uint32_t x;
for (x = rect.min_incl_x; x < rect.max_excl_x; x++) {
wuffs_base__pixel_buffer__set_color_u32_at(pb, x, y, color);
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(pb, x, y, color);
}
}
return wuffs_base__make_status(NULL);
@@ -32370,7 +32893,36 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column);
bool last_column,
uint8_t odd_column_rounding);
static const uint8_t* //
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libjpeg_x86_avx2(
uint8_t* dst_ptr,
const uint8_t* src_ptr_major,
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
return wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
dst_ptr, src_ptr_major, src_ptr_minor, src_len, h1v2_bias_ignored,
first_column, last_column, 7);
}
static const uint8_t* //
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libwebp_x86_avx2(
uint8_t* dst_ptr,
const uint8_t* src_ptr_major,
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
return wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
dst_ptr, src_ptr_major, src_ptr_minor, src_len, h1v2_bias_ignored,
first_column, last_column, 8);
}
#endif
#endif // defined(WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V3)
@@ -32411,6 +32963,102 @@ typedef void (*wuffs_private_impl__swizzle_ycc__convert_4_func)(
const uint8_t* up2,
const uint8_t* up3);
static void //
wuffs_private_impl__swizzle_ycca_bt601sr_src__convert_4_general(
wuffs_base__pixel_buffer* dst,
uint32_t x,
uint32_t x_end,
uint32_t y,
const uint8_t* up0,
const uint8_t* up1,
const uint8_t* up2,
const uint8_t* up3) {
for (; x < x_end; x++) {
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
color &= 0xFFFFFFu;
color |= ((uint32_t)(*up3++)) << 24;
wuffs_base__pixel_buffer__set_color_u32_argb_nonpremul_at(dst, x, y, color);
}
}
static void //
wuffs_private_impl__swizzle_ycca_bt601sr_src__convert_4_bgra_premul(
wuffs_base__pixel_buffer* dst,
uint32_t x,
uint32_t x_end,
uint32_t y,
const uint8_t* up0,
const uint8_t* up1,
const uint8_t* up2,
const uint8_t* up3) {
size_t stride = dst->private_impl.planes[0].stride;
uint8_t* row = dst->private_impl.planes[0].ptr + (stride * ((size_t)y));
for (; x < x_end; x++) {
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
color &= 0xFFFFFFu;
color |= ((uint32_t)(*up3++)) << 24;
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst_color =
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst_color);
}
}
static void //
wuffs_private_impl__swizzle_ycca_bt601sr_src_over__convert_4_general(
wuffs_base__pixel_buffer* dst,
uint32_t x,
uint32_t x_end,
uint32_t y,
const uint8_t* up0,
const uint8_t* up1,
const uint8_t* up2,
const uint8_t* up3) {
for (; x < x_end; x++) {
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
color &= 0xFFFFFFu;
color |= ((uint32_t)(*up3++)) << 24;
wuffs_base__pixel_buffer__composite_color_u32_nonpremul_at(dst, x, y,
color);
}
}
static void //
wuffs_private_impl__swizzle_ycca_bt601sr_src_over__convert_4_bgra_premul(
wuffs_base__pixel_buffer* dst,
uint32_t x,
uint32_t x_end,
uint32_t y,
const uint8_t* up0,
const uint8_t* up1,
const uint8_t* up2,
const uint8_t* up3) {
size_t stride = dst->private_impl.planes[0].stride;
uint8_t* row = dst->private_impl.planes[0].ptr + (stride * ((size_t)y));
for (; x < x_end; x++) {
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
color &= 0xFFFFFFu;
color |= ((uint32_t)(*up3++)) << 24;
uint8_t* ptr = row + (4 * ((size_t)x));
uint32_t dst_color = wuffs_base__peek_u32le__no_bounds_check(ptr);
dst_color = wuffs_private_impl__composite_premul_nonpremul_u32_axxx(
dst_color, color);
wuffs_base__poke_u32le__no_bounds_check(ptr, dst_color);
}
}
static void //
wuffs_private_impl__swizzle_cmyk__convert_4_general(
wuffs_base__pixel_buffer* dst,
@@ -32432,7 +33080,7 @@ wuffs_private_impl__swizzle_cmyk__convert_4_general(
r = ((r * w) + 0x7Fu) / 0xFFu;
g = ((g * w) + 0x7Fu) / 0xFFu;
b = ((b * w) + 0x7Fu) / 0xFFu;
wuffs_base__pixel_buffer__set_color_u32_at(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
dst, x, y, 0xFF000000u | (r << 16u) | (g << 8u) | (b << 0u));
}
}
@@ -32459,7 +33107,7 @@ wuffs_private_impl__swizzle_ycck__convert_4_general(
r = ((r * w) + 0x7Fu) / 0xFFu;
g = ((g * w) + 0x7Fu) / 0xFFu;
b = ((b * w) + 0x7Fu) / 0xFFu;
wuffs_base__pixel_buffer__set_color_u32_at(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
dst, x, y, 0xFF000000u | (r << 16u) | (g << 8u) | (b << 0u));
}
}
@@ -32489,7 +33137,7 @@ wuffs_private_impl__swizzle_rgb__convert_3_general(
(((uint32_t)(*up0++)) << 16u) | //
(((uint32_t)(*up1++)) << 8u) | //
(((uint32_t)(*up2++)) << 0u);
wuffs_base__pixel_buffer__set_color_u32_at(dst, x, y, color);
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(dst, x, y, color);
}
}
@@ -32506,7 +33154,7 @@ wuffs_private_impl__swizzle_ycc_bt601fr__convert_3_general(
uint32_t color = //
wuffs_base__color_ycc_bt601fr__as__color_u32( //
*up0++, *up1++, *up2++);
wuffs_base__pixel_buffer__set_color_u32_at(dst, x, y, color);
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(dst, x, y, color);
}
}
@@ -32567,7 +33215,7 @@ wuffs_private_impl__swizzle_ycc_bt601sr__convert_3_general(
uint32_t color = //
wuffs_base__color_ycc_bt601sr__as__color_u32( //
*up0++, *up1++, *up2++);
wuffs_base__pixel_buffer__set_color_u32_at(dst, x, y, color);
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(dst, x, y, color);
}
}
@@ -32729,8 +33377,8 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h1v2_triangle(
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias,
bool first_column,
bool last_column) {
bool first_column_ignored,
bool last_column_ignored) {
uint8_t* dp = dst_ptr;
const uint8_t* sp_major = src_ptr_major;
const uint8_t* sp_minor = src_ptr_minor;
@@ -32802,10 +33450,12 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
bool last_column,
uint8_t odd_column_rounding) {
uint8_t* dp = dst_ptr;
const uint8_t* sp_major = src_ptr_major;
const uint8_t* sp_minor = src_ptr_minor;
const uint8_t oc = odd_column_rounding;
if (first_column) {
src_len--;
@@ -32813,7 +33463,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
uint32_t sv = (12u * ((uint32_t)(*sp_major++))) + //
(4u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + 8u) >> 4u);
*dp++ = (uint8_t)((sv + 7u) >> 4u);
*dp++ = (uint8_t)((sv + oc) >> 4u);
return dst_ptr;
}
@@ -32825,7 +33475,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
if (src_len <= 0u) {
return dst_ptr;
}
@@ -32844,7 +33494,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
}
if (last_column) {
@@ -32856,12 +33506,40 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
}
return dst_ptr;
}
static const uint8_t* //
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libjpeg(
uint8_t* dst_ptr,
const uint8_t* src_ptr_major,
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
return wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
dst_ptr, src_ptr_major, src_ptr_minor, src_len, h1v2_bias_ignored,
first_column, last_column, 7);
}
static const uint8_t* //
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libwebp(
uint8_t* dst_ptr,
const uint8_t* src_ptr_major,
const uint8_t* src_ptr_minor,
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
return wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle(
dst_ptr, src_ptr_major, src_ptr_minor, src_len, h1v2_bias_ignored,
first_column, last_column, 8);
}
// wuffs_private_impl__swizzle_ycc__upsample_funcs is indexed by inv_h and then
// inv_v.
static const wuffs_private_impl__swizzle_ycc__upsample_func
@@ -33613,7 +34291,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
uint8_t v2,
uint8_t v3,
uint8_t ycc_model,
bool triangle_filter_for_2to1,
uint8_t ycc_upsampling,
wuffs_base__slice_u8 scratch_buffer_2k) {
if (!p) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
@@ -33628,7 +34306,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
(4u <= ((unsigned int)v0 - 1u)) || //
(4u <= ((unsigned int)v1 - 1u)) || //
(4u <= ((unsigned int)v2 - 1u)) || //
(triangle_filter_for_2to1 && ((x_min_incl | y_min_incl) > 0u)) ||
((ycc_upsampling != 0) && ((x_min_incl | y_min_incl) > 0u)) ||
(scratch_buffer_2k.len < 2048u)) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
@@ -33743,7 +34421,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
}
if (wuffs_base__pixel_format__is_planar(&dst->pixcfg.private_impl.pixfmt)) {
// TODO: see wuffs_base__pixel_buffer__set_color_u32_at's TODO.
// TODO: see wuffs_base__pixel_buffer__set_color_u32_argb_premul_at's TODO.
return wuffs_base__make_status(
wuffs_base__error__unsupported_pixel_swizzler_option);
}
@@ -33818,7 +34496,8 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
break;
default:
// TODO: see wuffs_base__pixel_buffer__set_color_u32_at's TODO.
// TODO: see wuffs_base__pixel_buffer__set_color_u32_argb_premul_at's
// TODO.
return wuffs_base__make_status(
wuffs_base__error__unsupported_pixel_swizzler_option);
}
@@ -33831,7 +34510,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
if (ycc_model >= WUFFS_BASE__YCC_MODEL__RGB) {
conv3func = &wuffs_private_impl__swizzle_rgb__convert_3_general;
} else if (ycc_model == WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE) {
} else if (ycc_model >= WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE) {
switch (dst->pixcfg.private_impl.pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:
@@ -33936,7 +34615,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
memcpy(&upfuncs, &wuffs_private_impl__swizzle_ycc__upsample_funcs,
sizeof upfuncs);
if (triangle_filter_for_2to1 &&
if ((ycc_upsampling != 0) &&
(wuffs_private_impl__swizzle_has_triangle_upsampler(inv_h0, inv_v0) ||
wuffs_private_impl__swizzle_has_triangle_upsampler(inv_h1, inv_v1) ||
wuffs_private_impl__swizzle_has_triangle_upsampler(inv_h2, inv_v2) ||
@@ -33946,7 +34625,10 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
upfuncs[0][1] = wuffs_private_impl__swizzle_ycc__upsample_inv_h1v2_triangle;
upfuncs[1][0] = wuffs_private_impl__swizzle_ycc__upsample_inv_h2v1_triangle;
upfuncs[1][1] = wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle;
upfuncs[1][1] =
(ycc_upsampling == 1)
? wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libjpeg
: wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libwebp;
#if defined(WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V3)
#if defined(__GNUC__) && !defined(__clang__)
@@ -33964,21 +34646,48 @@ wuffs_base__pixel_swizzler__swizzle_ycck(
#else
if (wuffs_base__cpu_arch__have_x86_avx2()) {
upfuncs[1][1] =
wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2;
(ycc_upsampling == 1)
? wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libjpeg_x86_avx2
: wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_libwebp_x86_avx2;
}
#endif
#endif
}
if ((h3 != 0u) || (v3 != 0u)) {
if (ycc_model == WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE) {
return wuffs_base__make_status(
wuffs_base__error__unsupported_pixel_swizzler_option);
wuffs_private_impl__swizzle_ycc__convert_4_func conv4func = NULL;
switch (ycc_model) {
case WUFFS_BASE__YCC_MODEL__BT_601_FULL_RANGE:
conv4func = &wuffs_private_impl__swizzle_ycck__convert_4_general;
break;
case WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC:
if (dst->pixcfg.private_impl.pixfmt.repr ==
WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL) {
conv4func =
&wuffs_private_impl__swizzle_ycca_bt601sr_src__convert_4_bgra_premul;
} else {
conv4func =
&wuffs_private_impl__swizzle_ycca_bt601sr_src__convert_4_general;
}
break;
case WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC_OVER:
if (dst->pixcfg.private_impl.pixfmt.repr ==
WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL) {
conv4func =
&wuffs_private_impl__swizzle_ycca_bt601sr_src_over__convert_4_bgra_premul;
} else {
conv4func =
&wuffs_private_impl__swizzle_ycca_bt601sr_src_over__convert_4_general;
}
break;
case WUFFS_BASE__YCC_MODEL__CMY:
conv4func = &wuffs_private_impl__swizzle_cmyk__convert_4_general;
break;
default:
return wuffs_base__make_status(
wuffs_base__error__unsupported_pixel_swizzler_option);
}
wuffs_private_impl__swizzle_ycc__convert_4_func conv4func =
(ycc_model >= WUFFS_BASE__YCC_MODEL__RGB)
? &wuffs_private_impl__swizzle_cmyk__convert_4_general
: &wuffs_private_impl__swizzle_ycck__convert_4_general;
(*func4)( //
dst, x_min_incl, x_max_excl, y_min_incl, y_max_excl, //
src0.ptr, src1.ptr, src2.ptr, src3.ptr, //
@@ -34439,10 +35148,12 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
size_t src_len,
uint32_t h1v2_bias_ignored,
bool first_column,
bool last_column) {
bool last_column,
uint8_t odd_column_rounding) {
uint8_t* dp = dst_ptr;
const uint8_t* sp_major = src_ptr_major;
const uint8_t* sp_minor = src_ptr_minor;
const uint8_t oc = odd_column_rounding;
if (first_column) {
src_len--;
@@ -34450,7 +35161,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
uint32_t sv = (12u * ((uint32_t)(*sp_major++))) + //
(4u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + 8u) >> 4u);
*dp++ = (uint8_t)((sv + 7u) >> 4u);
*dp++ = (uint8_t)((sv + oc) >> 4u);
return dst_ptr;
}
@@ -34462,7 +35173,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
if (src_len <= 0u) {
return dst_ptr;
}
@@ -34483,7 +35194,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
}
} else {
@@ -34556,8 +35267,8 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
// the u16 right value by 4. On the right (p1), shift right by 4 and then
// shift left by 8 so that, when still in the u16x16 little-endian
// interpretation, we have:
// - m1_element = (etcetera + 8) >> 4
// - p1_element = ((etcetera + 7) >> 4) << 8
// - m1_element = (etcetera + 8) >> 4
// - p1_element = ((etcetera + oc) >> 4) << 8
//
// step4_m1_lo = [0x00?? 0x00?? ... 0x00?? 0x00??]
// step4_p1_lo = [0x??00 0x??00 ... 0x??00 0x??00]
@@ -34566,14 +35277,14 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
__m256i step4_m1_lo = _mm256_srli_epi16(
_mm256_add_epi16(step3_m1_lo, _mm256_set1_epi16(8)), 4);
__m256i step4_p1_lo = _mm256_slli_epi16(
_mm256_srli_epi16(_mm256_add_epi16(step3_p1_lo, _mm256_set1_epi16(7)),
4),
_mm256_srli_epi16(
_mm256_add_epi16(step3_p1_lo, _mm256_set1_epi16(oc)), 4),
8);
__m256i step4_m1_hi = _mm256_srli_epi16(
_mm256_add_epi16(step3_m1_hi, _mm256_set1_epi16(8)), 4);
__m256i step4_p1_hi = _mm256_slli_epi16(
_mm256_srli_epi16(_mm256_add_epi16(step3_p1_hi, _mm256_set1_epi16(7)),
4),
_mm256_srli_epi16(
_mm256_add_epi16(step3_p1_hi, _mm256_set1_epi16(oc)), 4),
8);
// Bitwise-or two "0x00"-rich u16x16 vectors to get a u8x32 vector. Do
@@ -34624,7 +35335,7 @@ wuffs_private_impl__swizzle_ycc__upsample_inv_h2v2_triangle_x86_avx2(
uint32_t sv = (9u * ((uint32_t)(*sp_major++))) + //
(3u * ((uint32_t)(*sp_minor++)));
*dp++ = (uint8_t)((sv + (3u * sv_major_m1) + (sv_minor_m1) + 8u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + 7u) >> 4u);
*dp++ = (uint8_t)((sv + (3u * sv_major_p1) + (sv_minor_p1) + oc) >> 4u);
}
return dst_ptr;
@@ -50631,42 +51342,6 @@ WUFFS_HANDSUM__ZIGZAG[2][15] WUFFS_BASE__POTENTIALLY_UNUSED = {
},
};
static const uint8_t
WUFFS_HANDSUM__SCALE_AND_BIAS_CHROMA_DOWN[256] WUFFS_BASE__POTENTIALLY_UNUSED = {
60u, 60u, 61u, 61u, 62u, 62u, 63u, 63u,
64u, 64u, 65u, 65u, 66u, 66u, 67u, 67u,
68u, 68u, 69u, 69u, 70u, 70u, 71u, 71u,
72u, 72u, 73u, 73u, 74u, 74u, 75u, 75u,
76u, 76u, 77u, 77u, 78u, 78u, 79u, 79u,
80u, 80u, 81u, 81u, 82u, 82u, 83u, 83u,
84u, 84u, 85u, 85u, 86u, 86u, 87u, 87u,
88u, 88u, 89u, 89u, 90u, 90u, 91u, 91u,
92u, 92u, 93u, 93u, 94u, 94u, 95u, 95u,
96u, 96u, 97u, 97u, 98u, 98u, 99u, 99u,
100u, 100u, 101u, 101u, 102u, 102u, 103u, 103u,
104u, 104u, 105u, 105u, 106u, 106u, 107u, 107u,
108u, 108u, 109u, 109u, 110u, 110u, 111u, 111u,
112u, 112u, 113u, 113u, 114u, 114u, 115u, 115u,
116u, 116u, 117u, 117u, 118u, 118u, 119u, 119u,
120u, 120u, 121u, 121u, 122u, 122u, 123u, 123u,
124u, 124u, 125u, 125u, 126u, 126u, 127u, 127u,
128u, 128u, 129u, 129u, 130u, 130u, 131u, 131u,
132u, 132u, 133u, 133u, 134u, 134u, 135u, 135u,
136u, 136u, 137u, 137u, 138u, 138u, 139u, 139u,
140u, 140u, 141u, 141u, 142u, 142u, 143u, 143u,
144u, 144u, 145u, 145u, 146u, 146u, 147u, 147u,
148u, 148u, 149u, 149u, 150u, 150u, 151u, 151u,
152u, 152u, 153u, 153u, 154u, 154u, 155u, 155u,
156u, 156u, 157u, 157u, 158u, 158u, 159u, 159u,
160u, 160u, 161u, 161u, 162u, 162u, 163u, 163u,
164u, 164u, 165u, 165u, 166u, 166u, 167u, 167u,
168u, 168u, 169u, 169u, 170u, 170u, 171u, 171u,
172u, 172u, 173u, 173u, 174u, 174u, 175u, 175u,
176u, 176u, 177u, 177u, 178u, 178u, 179u, 179u,
180u, 180u, 181u, 181u, 182u, 182u, 183u, 183u,
184u, 184u, 185u, 185u, 186u, 186u, 187u, 187u,
};
static const uint8_t
WUFFS_HANDSUM__CUBE_ROOT[256] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 0u, 1u, 1u, 1u, 2u, 2u, 2u,
@@ -52175,8 +52850,8 @@ wuffs_handsum__decoder__scale_and_bias_chroma_down(
while (v_y < 8u) {
v_x = 0u;
while (v_x < 8u) {
self->private_data.f_buffers[1u][v_y][((4u * v_x) + 1u)] = WUFFS_HANDSUM__SCALE_AND_BIAS_CHROMA_DOWN[self->private_data.f_buffers[1u][v_y][((4u * v_x) + 1u)]];
self->private_data.f_buffers[1u][v_y][((4u * v_x) + 2u)] = WUFFS_HANDSUM__SCALE_AND_BIAS_CHROMA_DOWN[self->private_data.f_buffers[1u][v_y][((4u * v_x) + 2u)]];
self->private_data.f_buffers[1u][v_y][((4u * v_x) + 1u)] = ((uint8_t)(60u + ((uint8_t)(self->private_data.f_buffers[1u][v_y][((4u * v_x) + 1u)] >> 1u))));
self->private_data.f_buffers[1u][v_y][((4u * v_x) + 2u)] = ((uint8_t)(60u + ((uint8_t)(self->private_data.f_buffers[1u][v_y][((4u * v_x) + 2u)] >> 1u))));
v_x += 1u;
}
v_y += 1u;
@@ -57760,6 +58435,7 @@ wuffs_jpeg__decoder__swizzle_colorful(
uint32_t v_height1 = 0;
uint32_t v_height2 = 0;
uint32_t v_height3 = 0;
uint8_t v_ycc_upsampling = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
if (self->private_impl.f_swizzle_immediately) {
@@ -57821,6 +58497,10 @@ wuffs_jpeg__decoder__swizzle_colorful(
v_height3 = self->private_impl.f_components_workbuf_heights[3u];
}
}
v_ycc_upsampling = ((uint8_t)(0u));
if ( ! self->private_impl.f_use_lower_quality) {
v_ycc_upsampling = ((uint8_t)(1u));
}
v_status = wuffs_base__pixel_swizzler__swizzle_ycck(&self->private_impl.f_swizzler,
a_dst,
wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)),
@@ -57853,7 +58533,7 @@ wuffs_jpeg__decoder__swizzle_colorful(
self->private_impl.f_components_v[2u],
self->private_impl.f_components_v[3u],
self->private_impl.f_ycc_model,
! self->private_impl.f_use_lower_quality,
v_ycc_upsampling,
wuffs_base__make_slice_u8(self->private_data.f_swizzle_ycck_scratch_buffer_2k, 2048));
return wuffs_private_impl__status__ensure_not_a_suspension(v_status);
}
@@ -80898,7 +81578,9 @@ wuffs_thumbhash__decoder__workbuf_len(
// ---------------- Status Codes Implementations
const char wuffs_vp8__error__bad_quirk_source_length[] = "#vp8: bad QUIRK_SOURCE_LENGTH";
const char wuffs_vp8__error__bad_quirk_width_and_height[] = "#vp8: bad QUIRK_WIDTH_AND_HEIGHT";
const char wuffs_vp8__error__bad_header[] = "#vp8: bad header";
const char wuffs_vp8__error__inconsistent_width_and_height[] = "#vp8: inconsistent width and height";
const char wuffs_vp8__error__missing_quirk_source_length[] = "#vp8: missing QUIRK_SOURCE_LENGTH";
const char wuffs_vp8__error__truncated_input[] = "#vp8: truncated input";
const char wuffs_vp8__error__unsupported_quirk_source_length[] = "#vp8: unsupported QUIRK_SOURCE_LENGTH";
@@ -81793,6 +82475,8 @@ WUFFS_VP8__ZIGZAG[16] WUFFS_BASE__POTENTIALLY_UNUSED = {
9u, 12u, 13u, 10u, 7u, 11u, 14u, 15u,
};
#define WUFFS_VP8__QUIRKS_BASE 1836840960u
// ---------------- Private Initializer Prototypes
// ---------------- Private Function Prototypes
@@ -81838,7 +82522,7 @@ wuffs_vp8__decoder__initialize_mb_coeffs(
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_vp8__decoder__decode_block_coefficients(
wuffs_vp8__decoder__decode_subblock_coefficients(
wuffs_vp8__decoder* self,
wuffs_base__slice_u8 a_workbuf,
uint32_t a_mby,
@@ -81994,7 +82678,6 @@ WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_vp8__decoder__decode_macroblocks(
wuffs_vp8__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf);
WUFFS_BASE__GENERATED_C_CODE
@@ -82043,11 +82726,11 @@ wuffs_vp8__decoder__copy_from_yuv_cache(
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_vp8__decoder__swizzle_one_row_of_macroblocks(
wuffs_vp8__decoder__swizzle(
wuffs_vp8__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf,
uint32_t a_mby);
wuffs_base__pixel_blend a_blend,
wuffs_base__slice_u8 a_workbuf);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
@@ -82101,6 +82784,11 @@ wuffs_vp8__decoder__do_decode_image_config(
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_vp8__decoder__calculate_mbw_mbh(
wuffs_vp8__decoder* self);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_vp8__decoder__do_decode_frame_config(
@@ -82342,7 +83030,7 @@ wuffs_vp8__decoder__decode_coefficients(
self->private_data.f_mb_states_left &= 4286578687u;
v_tnz = (1u & (self->private_data.f_mb_states_top[a_mbx] >> 23u));
self->private_data.f_mb_states_top[a_mbx] &= 4286578687u;
v_lnz = wuffs_vp8__decoder__decode_block_coefficients(self,
v_lnz = wuffs_vp8__decoder__decode_subblock_coefficients(self,
a_workbuf,
a_mby,
a_seg,
@@ -82363,7 +83051,7 @@ wuffs_vp8__decoder__decode_coefficients(
v_tnz = (1u & (self->private_data.f_mb_states_top[a_mbx] >> (24u + v_bx)));
self->private_data.f_mb_states_top[a_mbx] &= (4294967295u ^ (((uint32_t)(1u)) << (24u + v_bx)));
v_b = ((4u * v_by) + v_bx);
v_lnz = wuffs_vp8__decoder__decode_block_coefficients(self,
v_lnz = wuffs_vp8__decoder__decode_subblock_coefficients(self,
a_workbuf,
a_mby,
a_seg,
@@ -82396,7 +83084,7 @@ wuffs_vp8__decoder__decode_coefficients(
(2u * v_by) +
v_bx +
16u);
v_lnz = wuffs_vp8__decoder__decode_block_coefficients(self,
v_lnz = wuffs_vp8__decoder__decode_subblock_coefficients(self,
a_workbuf,
a_mby,
a_seg,
@@ -82444,11 +83132,11 @@ wuffs_vp8__decoder__initialize_mb_coeffs(
return wuffs_base__make_empty_struct();
}
// -------- func vp8.decoder.decode_block_coefficients
// -------- func vp8.decoder.decode_subblock_coefficients
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_vp8__decoder__decode_block_coefficients(
wuffs_vp8__decoder__decode_subblock_coefficients(
wuffs_vp8__decoder* self,
wuffs_base__slice_u8 a_workbuf,
uint32_t a_mby,
@@ -83894,11 +84582,9 @@ WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_vp8__decoder__decode_macroblocks(
wuffs_vp8__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf) {
uint32_t v_mby = 0;
uint32_t v_mbx = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
v_mbx = 0u;
while (v_mbx < self->private_impl.f_mbw) {
@@ -83920,24 +84606,15 @@ wuffs_vp8__decoder__decode_macroblocks(
} else {
wuffs_vp8__decoder__filter_normal(self, a_workbuf, (v_mby - 1u));
}
v_status = wuffs_vp8__decoder__swizzle_one_row_of_macroblocks(self, a_dst, a_workbuf, (v_mby - 1u));
if ( ! wuffs_base__status__is_ok(&v_status)) {
return wuffs_private_impl__status__ensure_not_a_suspension(v_status);
}
}
v_mby += 1u;
}
if (self->private_impl.f_mbh > 0u) {
v_mby = (self->private_impl.f_mbh - 1u);
if (self->private_impl.f_filt_level <= 0u) {
} else if (self->private_impl.f_filt_simple) {
wuffs_vp8__decoder__filter_simple(self, a_workbuf, v_mby);
wuffs_vp8__decoder__filter_simple(self, a_workbuf, (self->private_impl.f_mbh - 1u));
} else {
wuffs_vp8__decoder__filter_normal(self, a_workbuf, v_mby);
}
v_status = wuffs_vp8__decoder__swizzle_one_row_of_macroblocks(self, a_dst, a_workbuf, v_mby);
if ( ! wuffs_base__status__is_ok(&v_status)) {
return wuffs_private_impl__status__ensure_not_a_suspension(v_status);
wuffs_vp8__decoder__filter_normal(self, a_workbuf, (self->private_impl.f_mbh - 1u));
}
}
return wuffs_base__make_status(NULL);
@@ -84196,76 +84873,80 @@ wuffs_vp8__decoder__copy_from_yuv_cache(
return wuffs_base__make_empty_struct();
}
// -------- func vp8.decoder.swizzle_one_row_of_macroblocks
// -------- func vp8.decoder.swizzle
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_vp8__decoder__swizzle_one_row_of_macroblocks(
wuffs_vp8__decoder__swizzle(
wuffs_vp8__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf,
uint32_t a_mby) {
uint32_t v_y_min_incl = 0;
uint32_t v_y_max_excl = 0;
uint64_t v_o = 0;
wuffs_base__slice_u8 v_src0 = {0};
wuffs_base__slice_u8 v_src1 = {0};
wuffs_base__slice_u8 v_src2 = {0};
wuffs_base__pixel_blend a_blend,
wuffs_base__slice_u8 a_workbuf) {
uint64_t v_wh = 0;
wuffs_base__slice_u8 v_prefix = {0};
wuffs_base__slice_u8 v_suffix = {0};
uint32_t v_has_alpha = 0;
uint32_t v_ycc_model = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
if ((self->private_impl.f_workbuf_yuv_y_end > self->private_impl.f_workbuf_yuv_u_end) || (self->private_impl.f_workbuf_yuv_u_end > self->private_impl.f_workbuf_yuv_v_end) || (self->private_impl.f_workbuf_yuv_v_end > ((uint64_t)(a_workbuf.len)))) {
if (self->private_impl.f_quirk_has_alpha_side_channel) {
if (self->private_impl.f_quirk_width_and_height == 0u) {
return wuffs_base__make_status(wuffs_vp8__error__inconsistent_width_and_height);
}
v_wh = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height)));
if (v_wh > ((uint64_t)(a_workbuf.len))) {
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
}
v_prefix = wuffs_base__slice_u8__subslice_j(a_workbuf, v_wh);
v_suffix = wuffs_base__slice_u8__subslice_i(a_workbuf, v_wh);
v_has_alpha = 1u;
v_ycc_model = (6u | (1u & ((uint32_t)(wuffs_base__utility__demake_pixel_blend(a_blend)))));
} else {
v_prefix = wuffs_base__utility__empty_slice_u8();
v_suffix = a_workbuf;
v_has_alpha = 0u;
v_ycc_model = 4u;
}
if ((self->private_impl.f_workbuf_yuv_y_end > self->private_impl.f_workbuf_yuv_u_end) || (self->private_impl.f_workbuf_yuv_u_end > self->private_impl.f_workbuf_yuv_v_end) || (self->private_impl.f_workbuf_yuv_v_end > ((uint64_t)(v_suffix.len)))) {
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
}
v_y_min_incl = wuffs_base__u32__sat_sub((a_mby * 16u), 6u);
v_y_max_excl = ((a_mby * 16u) + 10u);
if ((a_mby + 1u) >= self->private_impl.f_mbh) {
v_y_max_excl = self->private_impl.f_height;
}
v_o = ((uint64_t)((v_y_min_incl * self->private_impl.f_workbuf_yuv_y_stride)));
if (v_o < self->private_impl.f_workbuf_yuv_y_end) {
v_src0 = wuffs_base__slice_u8__subslice_ij(a_workbuf, v_o, self->private_impl.f_workbuf_yuv_y_end);
}
v_o = (((uint64_t)(((v_y_min_incl / 2u) * self->private_impl.f_workbuf_yuv_uv_stride))) + self->private_impl.f_workbuf_yuv_y_end);
if (v_o < self->private_impl.f_workbuf_yuv_u_end) {
v_src1 = wuffs_base__slice_u8__subslice_ij(a_workbuf, v_o, self->private_impl.f_workbuf_yuv_u_end);
}
v_o = (((uint64_t)(((v_y_min_incl / 2u) * self->private_impl.f_workbuf_yuv_uv_stride))) + self->private_impl.f_workbuf_yuv_u_end);
if (v_o < self->private_impl.f_workbuf_yuv_v_end) {
v_src2 = wuffs_base__slice_u8__subslice_ij(a_workbuf, v_o, self->private_impl.f_workbuf_yuv_v_end);
}
v_status = wuffs_base__pixel_swizzler__swizzle_ycck(&self->private_impl.f_swizzler,
a_dst,
wuffs_base__pixel_buffer__palette(a_dst),
0u,
self->private_impl.f_width,
v_y_min_incl,
v_y_max_excl,
v_src0,
v_src1,
v_src2,
wuffs_base__utility__empty_slice_u8(),
0u,
self->private_impl.f_height,
wuffs_base__slice_u8__subslice_j(v_suffix, self->private_impl.f_workbuf_yuv_y_end),
wuffs_base__slice_u8__subslice_ij(v_suffix,
self->private_impl.f_workbuf_yuv_y_end,
self->private_impl.f_workbuf_yuv_u_end),
wuffs_base__slice_u8__subslice_ij(v_suffix,
self->private_impl.f_workbuf_yuv_u_end,
self->private_impl.f_workbuf_yuv_v_end),
v_prefix,
(self->private_impl.f_mbw * 16u),
(self->private_impl.f_mbw * 8u),
(self->private_impl.f_mbw * 8u),
0u,
wuffs_base__u32__sat_sub((self->private_impl.f_mbh * 16u), v_y_min_incl),
wuffs_base__u32__sat_sub((self->private_impl.f_mbh * 8u), (v_y_min_incl / 2u)),
wuffs_base__u32__sat_sub((self->private_impl.f_mbh * 8u), (v_y_min_incl / 2u)),
0u,
(self->private_impl.f_width * v_has_alpha),
(self->private_impl.f_mbh * 16u),
(self->private_impl.f_mbh * 8u),
(self->private_impl.f_mbh * 8u),
(self->private_impl.f_height * v_has_alpha),
(self->private_impl.f_mbw * 16u),
(self->private_impl.f_mbw * 8u),
(self->private_impl.f_mbw * 8u),
0u,
(self->private_impl.f_width * v_has_alpha),
2u,
1u,
1u,
0u,
((uint8_t)((2u * v_has_alpha))),
2u,
1u,
1u,
0u,
((uint8_t)(1u)),
false,
((uint8_t)((2u * v_has_alpha))),
((uint8_t)(v_ycc_model)),
((uint8_t)(2u)),
wuffs_base__make_slice_u8(self->private_data.f_swizzle_ycck_scratch_buffer_2k, 2048));
return wuffs_private_impl__status__ensure_not_a_suspension(v_status);
}
@@ -85184,6 +85865,12 @@ wuffs_vp8__decoder__get_quirk(
if (a_key == 3u) {
return self->private_impl.f_quirk_source_length;
} else if (a_key == 1836840960u) {
if (self->private_impl.f_quirk_has_alpha_side_channel) {
return 1u;
}
} else if (a_key == 1836840961u) {
return self->private_impl.f_quirk_width_and_height;
}
return 0u;
}
@@ -85206,8 +85893,41 @@ wuffs_vp8__decoder__set_quirk(
: wuffs_base__error__initialize_not_called);
}
uint32_t v_w = 0;
uint32_t v_h = 0;
if (a_key == 3u) {
self->private_impl.f_quirk_source_length = a_value;
if ((a_value & 1u) == 0u) {
if (a_value != 0u) {
return wuffs_base__make_status(wuffs_vp8__error__bad_quirk_source_length);
}
self->private_impl.f_quirk_source_length = 0u;
self->private_impl.f_partitioned_data_length = 0u;
} else if ((a_value >> 1u) < 10u) {
return wuffs_base__make_status(wuffs_vp8__error__bad_quirk_source_length);
} else if ((a_value >> 1u) > 150994945u) {
return wuffs_base__make_status(wuffs_vp8__error__unsupported_quirk_source_length);
} else {
self->private_impl.f_quirk_source_length = a_value;
self->private_impl.f_partitioned_data_length = (((uint32_t)((a_value >> 1u))) - 10u);
}
return wuffs_base__make_status(NULL);
} else if (a_key == 1836840960u) {
self->private_impl.f_quirk_has_alpha_side_channel = (a_value != 0u);
return wuffs_base__make_status(NULL);
} else if (a_key == 1836840961u) {
v_w = ((uint32_t)(a_value));
v_h = ((uint32_t)((a_value >> 32u)));
if ((v_w < 1u) ||
(16383u < v_w) ||
(v_h < 1u) ||
(16383u < v_h)) {
return wuffs_base__make_status(wuffs_vp8__error__bad_quirk_width_and_height);
}
self->private_impl.f_quirk_width_and_height = a_value;
self->private_impl.f_width = v_w;
self->private_impl.f_height = v_h;
wuffs_vp8__decoder__calculate_mbw_mbh(self);
return wuffs_base__make_status(NULL);
}
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
@@ -85310,17 +86030,10 @@ wuffs_vp8__decoder__do_decode_image_config(
status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
goto exit;
}
if ((self->private_impl.f_quirk_source_length & 1u) == 0u) {
if (self->private_impl.f_quirk_source_length == 0u) {
status = wuffs_base__make_status(wuffs_vp8__error__missing_quirk_source_length);
goto exit;
} else if ((self->private_impl.f_quirk_source_length >> 1u) < 10u) {
status = wuffs_base__make_status(wuffs_vp8__error__bad_quirk_source_length);
goto exit;
} else if ((self->private_impl.f_quirk_source_length >> 1u) > 150994945u) {
status = wuffs_base__make_status(wuffs_vp8__error__unsupported_quirk_source_length);
goto exit;
}
self->private_impl.f_partitioned_data_length = (((uint32_t)((self->private_impl.f_quirk_source_length >> 1u))) - 10u);
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_0;
@@ -85424,13 +86137,14 @@ wuffs_vp8__decoder__do_decode_image_config(
}
self->private_impl.f_width = (16383u & v_c32);
self->private_impl.f_height = (16383u & (v_c32 >> 16u));
self->private_impl.f_mbw = ((self->private_impl.f_width + 15u) / 16u);
self->private_impl.f_mbh = ((self->private_impl.f_height + 15u) / 16u);
self->private_impl.f_workbuf_yuv_y_stride = (self->private_impl.f_mbw * 16u);
self->private_impl.f_workbuf_yuv_uv_stride = (self->private_impl.f_mbw * 8u);
self->private_impl.f_workbuf_yuv_y_end = (((uint64_t)(self->private_impl.f_workbuf_yuv_y_stride)) * ((uint64_t)((self->private_impl.f_mbh * 16u))));
self->private_impl.f_workbuf_yuv_u_end = (self->private_impl.f_workbuf_yuv_y_end + (((uint64_t)(self->private_impl.f_workbuf_yuv_uv_stride)) * ((uint64_t)((self->private_impl.f_mbh * 8u)))));
self->private_impl.f_workbuf_yuv_v_end = (self->private_impl.f_workbuf_yuv_u_end + (((uint64_t)(self->private_impl.f_workbuf_yuv_uv_stride)) * ((uint64_t)((self->private_impl.f_mbh * 8u)))));
if ((self->private_impl.f_width == 0u) || (self->private_impl.f_height == 0u)) {
status = wuffs_base__make_status(wuffs_vp8__error__bad_header);
goto exit;
} else if ((self->private_impl.f_quirk_width_and_height != 0u) && (self->private_impl.f_quirk_width_and_height != (((uint64_t)(self->private_impl.f_width)) | (((uint64_t)(self->private_impl.f_height)) << 32u)))) {
status = wuffs_base__make_status(wuffs_vp8__error__inconsistent_width_and_height);
goto exit;
}
wuffs_vp8__decoder__calculate_mbw_mbh(self);
self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)));
if (a_dst != NULL) {
wuffs_base__image_config__set(
@@ -85463,6 +86177,22 @@ wuffs_vp8__decoder__do_decode_image_config(
return status;
}
// -------- func vp8.decoder.calculate_mbw_mbh
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_vp8__decoder__calculate_mbw_mbh(
wuffs_vp8__decoder* self) {
self->private_impl.f_mbw = ((self->private_impl.f_width + 15u) / 16u);
self->private_impl.f_mbh = ((self->private_impl.f_height + 15u) / 16u);
self->private_impl.f_workbuf_yuv_y_stride = (self->private_impl.f_mbw * 16u);
self->private_impl.f_workbuf_yuv_uv_stride = (self->private_impl.f_mbw * 8u);
self->private_impl.f_workbuf_yuv_y_end = (((uint64_t)(self->private_impl.f_workbuf_yuv_y_stride)) * ((uint64_t)((self->private_impl.f_mbh * 16u))));
self->private_impl.f_workbuf_yuv_u_end = (self->private_impl.f_workbuf_yuv_y_end + (((uint64_t)(self->private_impl.f_workbuf_yuv_uv_stride)) * ((uint64_t)((self->private_impl.f_mbh * 8u)))));
self->private_impl.f_workbuf_yuv_v_end = (self->private_impl.f_workbuf_yuv_u_end + (((uint64_t)(self->private_impl.f_workbuf_yuv_uv_stride)) * ((uint64_t)((self->private_impl.f_mbh * 8u)))));
return wuffs_base__make_empty_struct();
}
// -------- func vp8.decoder.decode_frame_config
WUFFS_BASE__GENERATED_C_CODE
@@ -85648,37 +86378,65 @@ wuffs_vp8__decoder__decode_frame(
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
uint64_t v_wh = 0;
uint64_t v_wb_len = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
wuffs_base__slice_u8 v_trimmed = {0};
uint32_t coro_susp_point = self->private_impl.p_decode_frame;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
v_trimmed = a_workbuf;
if (self->private_impl.f_quirk_has_alpha_side_channel) {
if (self->private_impl.f_quirk_width_and_height == 0u) {
status = wuffs_base__make_status(wuffs_vp8__error__inconsistent_width_and_height);
goto exit;
}
v_wh = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height)));
if (v_wh > ((uint64_t)(a_workbuf.len))) {
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
goto exit;
}
v_trimmed = wuffs_base__slice_u8__subslice_i(a_workbuf, v_wh);
}
v_wb_len = (self->private_impl.f_workbuf_yuv_v_end + ((uint64_t)(self->private_impl.f_partitioned_data_length)) + 8u);
if (v_wb_len > ((uint64_t)(a_workbuf.len))) {
if (v_wb_len > ((uint64_t)(v_trimmed.len))) {
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
goto exit;
} else {
a_workbuf = wuffs_base__slice_u8__subslice_j(a_workbuf, v_wb_len);
v_trimmed = wuffs_base__slice_u8__subslice_j(v_trimmed, v_wb_len);
}
{
wuffs_base__status t_0 = wuffs_vp8__decoder__do_decode_frame(self,
a_dst,
a_src,
a_blend,
a_workbuf,
v_trimmed,
a_opts);
v_status = t_0;
}
if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) {
if (wuffs_base__status__is_ok(&v_status)) {
break;
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) {
status = wuffs_base__make_status(wuffs_vp8__error__truncated_input);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
v_status = wuffs_vp8__decoder__swizzle(self, a_dst, a_blend, a_workbuf);
if ( ! wuffs_base__status__is_ok(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
}
ok:
self->private_impl.p_decode_frame = 0;
@@ -85780,7 +86538,7 @@ wuffs_vp8__decoder__do_decode_frame(
}
goto ok;
}
v_status = wuffs_vp8__decoder__decode_macroblocks(self, a_dst, a_workbuf);
v_status = wuffs_vp8__decoder__decode_macroblocks(self, a_workbuf);
if ( ! wuffs_base__status__is_ok(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
@@ -85981,10 +86739,17 @@ wuffs_vp8__decoder__workbuf_len(
return wuffs_base__utility__empty_range_ii_u64();
}
uint64_t v_wh = 0;
uint64_t v_wb_len = 0;
if (self->private_impl.f_quirk_source_length == 0u) {
return wuffs_base__utility__make_range_ii_u64(1u, 0u);
}
if (self->private_impl.f_quirk_has_alpha_side_channel) {
v_wh = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height)));
}
v_wb_len = (self->private_impl.f_workbuf_yuv_v_end + ((uint64_t)(self->private_impl.f_partitioned_data_length)) + 8u);
return wuffs_base__utility__make_range_ii_u64(v_wb_len, v_wb_len);
return wuffs_base__utility__make_range_ii_u64((v_wh + v_wb_len), (v_wh + v_wb_len));
}
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__VP8)
@@ -86915,18 +87680,20 @@ wuffs_wbmp__decoder__workbuf_len(
// ---------------- Status Codes Implementations
const char wuffs_webp__error__bad_alph_vp8l_combination[] = "#webp: bad ALPH+VP8L combination";
const char wuffs_webp__error__bad_huffman_code_over_subscribed[] = "#webp: bad Huffman code (over-subscribed)";
const char wuffs_webp__error__bad_huffman_code_under_subscribed[] = "#webp: bad Huffman code (under-subscribed)";
const char wuffs_webp__error__bad_huffman_code[] = "#webp: bad Huffman code";
const char wuffs_webp__error__bad_vp8x_chunk[] = "#webp: bad VP8X chunk";
const char wuffs_webp__error__bad_back_reference[] = "#webp: bad back-reference";
const char wuffs_webp__error__bad_chunk_order[] = "#webp: bad chunk order";
const char wuffs_webp__error__bad_color_cache[] = "#webp: bad color cache";
const char wuffs_webp__error__bad_header[] = "#webp: bad header";
const char wuffs_webp__error__bad_transform[] = "#webp: bad transform";
const char wuffs_webp__error__short_chunk[] = "#webp: short chunk";
const char wuffs_webp__error__truncated_input[] = "#webp: truncated input";
const char wuffs_webp__error__unsupported_number_of_huffman_groups[] = "#webp: unsupported number of Huffman groups";
const char wuffs_webp__error__unsupported_transform_after_color_indexing_transform[] = "#webp: unsupported transform after color indexing transform";
const char wuffs_webp__error__unsupported_webp_file[] = "#webp: unsupported WebP file";
const char wuffs_webp__error__unsupported_number_of_huffman_groups[] = "#webp: unsupported number of Huffman groups";
const char wuffs_webp__error__internal_error_inconsistent_huffman_code[] = "#webp: internal error: inconsistent Huffman code";
const char wuffs_webp__error__internal_error_inconsistent_dst_buffer[] = "#webp: internal error: inconsistent dst buffer";
const char wuffs_webp__error__internal_error_inconsistent_n_bits[] = "#webp: internal error: inconsistent n_bits";
@@ -86978,6 +87745,44 @@ WUFFS_WEBP__DISTANCE_MAP[120] WUFFS_BASE__POTENTIALLY_UNUSED = {
// ---------------- Private Function Prototypes
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__decode_alph(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__decode_alph_limited(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__compact_alph(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__filter_alph_horizontal(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__filter_alph_vertical(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__filter_alph_gradient(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__decode_huffman_groups(
@@ -87041,6 +87846,7 @@ static wuffs_base__empty_struct
wuffs_webp__decoder__apply_transform_predictor(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_pix,
uint32_t a_width,
wuffs_base__slice_u8 a_tile_data);
WUFFS_BASE__GENERATED_C_CODE
@@ -87071,6 +87877,7 @@ static wuffs_base__empty_struct
wuffs_webp__decoder__apply_transform_cross_color(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_pix,
uint32_t a_width,
wuffs_base__slice_u8 a_tile_data);
WUFFS_BASE__GENERATED_C_CODE
@@ -87083,38 +87890,12 @@ WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__apply_transform_color_indexing(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_pix);
wuffs_base__slice_u8 a_pix,
uint32_t a_width);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited_vp8l(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_frame_config(
wuffs_webp__decoder* self,
wuffs_base__frame_config* a_dst,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_frame(
wuffs_webp__decoder__do_decode_frame_vp8l(
wuffs_webp__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__io_buffer* a_src,
@@ -87162,6 +87943,49 @@ wuffs_webp__decoder__swizzle(
wuffs_base__slice_u8 a_src,
wuffs_base__pixel_blend a_blend);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited_vp8l(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited_vp8x(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_frame_config(
wuffs_webp__decoder* self,
wuffs_base__frame_config* a_dst,
wuffs_base__io_buffer* a_src);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_frame(
wuffs_webp__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__pixel_blend a_blend,
wuffs_base__slice_u8 a_workbuf,
wuffs_base__decode_frame_options* a_opts);
// ---------------- VTables
const wuffs_base__image_decoder__func_ptrs
@@ -87278,6 +88102,375 @@ sizeof__wuffs_webp__decoder(void) {
// ---------------- Function Implementations
// -------- func webp.decoder.decode_alph
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__decode_alph(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint64_t v_r_mark = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_decode_alph;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
{
const bool o_0_closed_a_src = a_src->meta.closed;
const uint8_t* o_0_io2_a_src = io2_a_src;
wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src,
((uint64_t)(self->private_impl.f_sub_chunk_length)));
if (a_src) {
size_t n = ((size_t)(io2_a_src - a_src->data.ptr));
a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n);
a_src->meta.wi = n;
}
v_r_mark = ((uint64_t)(iop_a_src - io0_a_src));
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_0 = wuffs_webp__decoder__decode_alph_limited(self, a_src, a_workbuf);
v_status = t_0;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_sub_chunk_length, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src))))));
io2_a_src = o_0_io2_a_src;
if (a_src) {
a_src->meta.closed = o_0_closed_a_src;
a_src->meta.wi = ((size_t)(io2_a_src - a_src->data.ptr));
}
}
if (wuffs_base__status__is_ok(&v_status)) {
break;
} else if ( ! wuffs_base__status__is_suspension(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_sub_chunk_length == 0u)) {
status = wuffs_base__make_status(wuffs_webp__error__short_chunk);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
ok:
self->private_impl.p_decode_alph = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_alph = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.decode_alph_limited
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__decode_alph_limited(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint64_t v_n = 0;
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_decode_alph_limited;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint8_t t_0 = *iop_a_src++;
self->private_impl.f_vp8x_alph_flags = t_0;
}
if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 3u)) == 0u) {
wuffs_private_impl__io_reader__limited_copy_u32_to_slice(
&iop_a_src, io2_a_src,(self->private_impl.f_width * self->private_impl.f_height), a_workbuf);
} else if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 3u)) == 1u) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
status = wuffs_webp__decoder__do_decode_frame_vp8l(self,
NULL,
a_src,
wuffs_base__utility__make_pixel_blend(0u),
a_workbuf,
NULL);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
v_n = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height * 4u)));
if (v_n <= ((uint64_t)(a_workbuf.len))) {
wuffs_webp__decoder__compact_alph(self, wuffs_base__slice_u8__subslice_j(a_workbuf, v_n));
}
} else {
status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk);
goto exit;
}
self->private_impl.f_vp8x_alph_flags |= 2u;
v_n = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height)));
if (v_n <= ((uint64_t)(a_workbuf.len))) {
if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 12u)) == 4u) {
wuffs_webp__decoder__filter_alph_horizontal(self, wuffs_base__slice_u8__subslice_j(a_workbuf, v_n));
} else if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 12u)) == 8u) {
wuffs_webp__decoder__filter_alph_vertical(self, wuffs_base__slice_u8__subslice_j(a_workbuf, v_n));
} else if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 12u)) == 12u) {
wuffs_webp__decoder__filter_alph_gradient(self, wuffs_base__slice_u8__subslice_j(a_workbuf, v_n));
}
}
goto ok;
ok:
self->private_impl.p_decode_alph_limited = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_alph_limited = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.compact_alph
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__compact_alph(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_s) {
uint64_t v_i = 0;
uint64_t v_j = 0;
if (((uint64_t)(a_s.len)) > 1073741824u) {
return wuffs_base__make_empty_struct();
}
v_i = 0u;
v_j = 1u;
while ((v_i < ((uint64_t)(a_s.len))) && (v_j < ((uint64_t)(a_s.len)))) {
a_s.ptr[v_i] = a_s.ptr[v_j];
v_i += 1u;
v_j += 4u;
}
return wuffs_base__make_empty_struct();
}
// -------- func webp.decoder.filter_alph_horizontal
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__filter_alph_horizontal(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_s) {
uint32_t v_y = 0;
uint32_t v_x = 0;
uint64_t v_i = 0;
uint8_t v_pred = 0;
v_y = 0u;
while (v_y < self->private_impl.f_height) {
v_pred = 0u;
if (v_y > 0u) {
v_i = ((uint64_t)(((v_y - 1u) * self->private_impl.f_width)));
if (v_i >= ((uint64_t)(a_s.len))) {
break;
}
v_pred = a_s.ptr[v_i];
}
v_x = 0u;
while (v_x < self->private_impl.f_width) {
v_i = ((uint64_t)(((v_y * self->private_impl.f_width) + v_x)));
if (v_i >= ((uint64_t)(a_s.len))) {
break;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
a_s.ptr[v_i] += v_pred;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
v_pred = a_s.ptr[v_i];
v_x += 1u;
}
v_y += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func webp.decoder.filter_alph_vertical
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__filter_alph_vertical(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_s) {
uint32_t v_y = 0;
uint32_t v_x = 0;
uint64_t v_i = 0;
uint8_t v_pred = 0;
v_x = 0u;
while (v_x < self->private_impl.f_width) {
v_pred = 0u;
if (v_x > 0u) {
v_i = ((uint64_t)((v_x - 1u)));
if (v_i >= ((uint64_t)(a_s.len))) {
break;
}
v_pred = a_s.ptr[v_i];
}
v_y = 0u;
while (v_y < self->private_impl.f_height) {
v_i = ((uint64_t)(((v_y * self->private_impl.f_width) + v_x)));
if (v_i >= ((uint64_t)(a_s.len))) {
break;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
a_s.ptr[v_i] += v_pred;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
v_pred = a_s.ptr[v_i];
v_y += 1u;
}
v_x += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func webp.decoder.filter_alph_gradient
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__filter_alph_gradient(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_s) {
uint32_t v_y = 0;
uint32_t v_x = 0;
uint64_t v_i = 0;
uint8_t v_pred = 0;
uint32_t v_grad = 0;
uint8_t v_l = 0;
uint8_t v_t = 0;
uint8_t v_tl = 0;
v_x = 0u;
while (v_x < self->private_impl.f_width) {
v_i = ((uint64_t)(v_x));
if (v_i >= ((uint64_t)(a_s.len))) {
break;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
a_s.ptr[v_i] += v_pred;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
v_pred = a_s.ptr[v_i];
v_x += 1u;
}
v_y = 1u;
while (v_y < self->private_impl.f_height) {
v_i = ((uint64_t)(((v_y - 1u) * self->private_impl.f_width)));
if (v_i >= ((uint64_t)(a_s.len))) {
break;
}
v_t = a_s.ptr[v_i];
v_tl = v_t;
v_l = v_t;
v_x = 0u;
while (v_x < self->private_impl.f_width) {
v_i = ((uint64_t)((((v_y - 1u) * self->private_impl.f_width) + v_x)));
if (v_i >= ((uint64_t)(a_s.len))) {
break;
}
v_t = a_s.ptr[v_i];
v_i = ((uint64_t)(((v_y * self->private_impl.f_width) + v_x)));
if (v_i >= ((uint64_t)(a_s.len))) {
break;
}
v_grad = ((uint32_t)(((uint32_t)(((uint32_t)(v_l)) + ((uint32_t)(v_t)))) - ((uint32_t)(v_tl))));
if (v_grad > 2147483647u) {
v_grad = 0u;
} else if (v_grad > 255u) {
v_grad = 255u;
}
v_l = ((uint8_t)(a_s.ptr[v_i] + ((uint8_t)(v_grad))));
a_s.ptr[v_i] = v_l;
v_tl = v_t;
v_x += 1u;
}
v_y += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func webp.decoder.decode_huffman_groups
WUFFS_BASE__GENERATED_C_CODE
@@ -88462,6 +89655,7 @@ static wuffs_base__empty_struct
wuffs_webp__decoder__apply_transform_predictor(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_pix,
uint32_t a_width,
wuffs_base__slice_u8 a_tile_data) {
uint64_t v_w4 = 0;
wuffs_base__slice_u8 v_prev_row = {0};
@@ -88489,10 +89683,10 @@ wuffs_webp__decoder__apply_transform_predictor(
uint32_t v_sum_l = 0;
uint32_t v_sum_t = 0;
if ((self->private_impl.f_width <= 0u) || (self->private_impl.f_height <= 0u)) {
if ((a_width <= 0u) || (self->private_impl.f_height <= 0u)) {
return wuffs_base__make_empty_struct();
}
v_w4 = ((uint64_t)((self->private_impl.f_width * 4u)));
v_w4 = ((uint64_t)((a_width * 4u)));
v_curr_row = wuffs_base__utility__empty_slice_u8();
if (v_w4 <= ((uint64_t)(a_pix.len))) {
v_curr_row = wuffs_base__slice_u8__subslice_j(a_pix, v_w4);
@@ -88522,7 +89716,7 @@ wuffs_webp__decoder__apply_transform_predictor(
v_curr_row = wuffs_base__slice_u8__subslice_i(v_curr_row, 4u);
}
v_tile_size_log2 = ((uint32_t)(self->private_impl.f_transform_tile_size_log2[0u]));
v_tiles_per_row = ((self->private_impl.f_width + ((((uint32_t)(1u)) << v_tile_size_log2) - 1u)) >> v_tile_size_log2);
v_tiles_per_row = ((a_width + ((((uint32_t)(1u)) << v_tile_size_log2) - 1u)) >> v_tile_size_log2);
v_mask = ((((uint32_t)(1u)) << v_tile_size_log2) - 1u);
v_y = 1u;
while (v_y < self->private_impl.f_height) {
@@ -88554,7 +89748,7 @@ wuffs_webp__decoder__apply_transform_predictor(
#endif
}
v_x = 1u;
while (v_x < self->private_impl.f_width) {
while (v_x < a_width) {
if (((v_x & v_mask) == 0u) && (((uint64_t)(v_tile_data.len)) >= 4u)) {
v_mode = ((uint8_t)(v_tile_data.ptr[1u] & 15u));
v_tile_data = wuffs_base__slice_u8__subslice_i(v_tile_data, 4u);
@@ -88850,6 +90044,7 @@ static wuffs_base__empty_struct
wuffs_webp__decoder__apply_transform_cross_color(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_pix,
uint32_t a_width,
wuffs_base__slice_u8 a_tile_data) {
uint32_t v_tile_size_log2 = 0;
uint32_t v_tiles_per_row = 0;
@@ -88866,7 +90061,7 @@ wuffs_webp__decoder__apply_transform_cross_color(
uint8_t v_r = 0;
v_tile_size_log2 = ((uint32_t)(self->private_impl.f_transform_tile_size_log2[1u]));
v_tiles_per_row = ((self->private_impl.f_width + ((((uint32_t)(1u)) << v_tile_size_log2) - 1u)) >> v_tile_size_log2);
v_tiles_per_row = ((a_width + ((((uint32_t)(1u)) << v_tile_size_log2) - 1u)) >> v_tile_size_log2);
v_mask = ((((uint32_t)(1u)) << v_tile_size_log2) - 1u);
v_y = 0u;
while (v_y < self->private_impl.f_height) {
@@ -88876,7 +90071,7 @@ wuffs_webp__decoder__apply_transform_cross_color(
v_tile_data = wuffs_base__slice_u8__subslice_i(a_tile_data, v_t);
}
v_x = 0u;
while (v_x < self->private_impl.f_width) {
while (v_x < a_width) {
if (((v_x & v_mask) == 0u) && (((uint64_t)(v_tile_data.len)) >= 4u)) {
v_g2r = wuffs_base__utility__sign_extend_convert_u8_u32(v_tile_data.ptr[0u]);
v_g2b = wuffs_base__utility__sign_extend_convert_u8_u32(v_tile_data.ptr[1u]);
@@ -88947,7 +90142,8 @@ WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_webp__decoder__apply_transform_color_indexing(
wuffs_webp__decoder* self,
wuffs_base__slice_u8 a_pix) {
wuffs_base__slice_u8 a_pix,
uint32_t a_width) {
uint32_t v_tile_size_log2 = 0;
uint32_t v_bits_per_pixel = 0;
uint32_t v_x_mask = 0;
@@ -88994,8 +90190,8 @@ wuffs_webp__decoder__apply_transform_color_indexing(
v_src_index = ((uint64_t)((self->private_impl.f_workbuf_offset_for_color_indexing + 1u)));
v_y = 0u;
while (v_y < self->private_impl.f_height) {
v_di = ((uint64_t)((4u * v_y * self->private_impl.f_width)));
v_dj = ((uint64_t)((4u * (v_y + 1u) * self->private_impl.f_width)));
v_di = ((uint64_t)((4u * v_y * a_width)));
v_dj = ((uint64_t)((4u * (v_y + 1u) * a_width)));
if ((v_di > v_dj) || (v_dj > ((uint64_t)(a_pix.len)))) {
break;
}
@@ -89024,866 +90220,11 @@ wuffs_webp__decoder__apply_transform_color_indexing(
return wuffs_base__make_empty_struct();
}
// -------- func webp.decoder.get_quirk
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_webp__decoder__get_quirk(
const wuffs_webp__decoder* self,
uint32_t a_key) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return 0u;
}
// -------- func webp.decoder.set_quirk
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_webp__decoder__set_quirk(
wuffs_webp__decoder* self,
uint32_t a_key,
uint64_t a_value) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
// -------- func webp.decoder.decode_image_config
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_webp__decoder__decode_image_config(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (!a_src) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 1)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t coro_susp_point = self->private_impl.p_decode_image_config;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
{
wuffs_base__status t_0 = wuffs_webp__decoder__do_decode_image_config(self, a_dst, a_src);
v_status = t_0;
}
if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) {
status = wuffs_base__make_status(wuffs_webp__error__truncated_input);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
ok:
self->private_impl.p_decode_image_config = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_image_config = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 1 : 0;
goto exit;
exit:
if (wuffs_base__status__is_error(&status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
}
// -------- func webp.decoder.do_decode_image_config
// -------- func webp.decoder.do_decode_frame_vp8l
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_c32 = 0;
uint64_t v_r_mark = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_image_config;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence != 0u) {
status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config.scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_0;
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch));
break;
}
num_bits_0 += 8u;
*scratch |= ((uint64_t)(num_bits_0)) << 56;
}
}
v_c32 = t_0;
}
if (v_c32 != 1179011410u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config.scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8u;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
self->private_impl.f_riff_chunk_length = t_1;
}
if ((self->private_impl.f_riff_chunk_length & 1u) != 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
while (true) {
{
const bool o_0_closed_a_src = a_src->meta.closed;
const uint8_t* o_0_io2_a_src = io2_a_src;
wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src,
((uint64_t)(self->private_impl.f_riff_chunk_length)));
if (a_src) {
size_t n = ((size_t)(io2_a_src - a_src->data.ptr));
a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n);
a_src->meta.wi = n;
}
v_r_mark = ((uint64_t)(iop_a_src - io0_a_src));
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_2 = wuffs_webp__decoder__do_decode_image_config_limited(self, a_dst, a_src);
v_status = t_2;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_riff_chunk_length, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src))))));
io2_a_src = o_0_io2_a_src;
if (a_src) {
a_src->meta.closed = o_0_closed_a_src;
a_src->meta.wi = ((size_t)(io2_a_src - a_src->data.ptr));
}
}
if (wuffs_base__status__is_ok(&v_status)) {
break;
} else if ( ! wuffs_base__status__is_suspension(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_riff_chunk_length == 0u)) {
status = wuffs_base__make_status(wuffs_webp__error__short_chunk);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(5);
}
self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)));
if ( ! self->private_impl.f_is_vp8_lossy && (a_dst != NULL)) {
wuffs_base__image_config__set(
a_dst,
self->private_impl.f_pixfmt,
0u,
self->private_impl.f_width,
self->private_impl.f_height,
self->private_impl.f_frame_config_io_position,
false);
}
self->private_impl.f_call_sequence = 32u;
ok:
self->private_impl.p_do_decode_image_config = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_image_config = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.do_decode_image_config_limited
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_c32 = 0;
uint64_t v_r_mark = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_image_config_limited;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited.scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_0;
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch));
break;
}
num_bits_0 += 8u;
*scratch |= ((uint64_t)(num_bits_0)) << 56;
}
}
v_c32 = t_0;
}
if (v_c32 != 1346520407u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited.scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8u;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
v_c32 = t_1;
}
if (v_c32 == 540561494u) {
self->private_impl.f_is_vp8_lossy = true;
} else if (v_c32 == 1278758998u) {
} else if (v_c32 == 1480085590u) {
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
goto exit;
} else {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
uint32_t t_2;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_2 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited.scratch;
uint32_t num_bits_2 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_2;
if (num_bits_2 == 24) {
t_2 = ((uint32_t)(*scratch));
break;
}
num_bits_2 += 8u;
*scratch |= ((uint64_t)(num_bits_2)) << 56;
}
}
self->private_impl.f_sub_chunk_length = t_2;
}
if (self->private_impl.f_sub_chunk_length < 4u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
self->private_impl.f_sub_chunk_has_padding = ((self->private_impl.f_sub_chunk_length & 1u) != 0u);
if (self->private_impl.f_is_vp8_lossy) {
wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(self->private_impl.f_sub_chunk_length)) << 1u) | 1u));
}
while (true) {
{
const bool o_0_closed_a_src = a_src->meta.closed;
const uint8_t* o_0_io2_a_src = io2_a_src;
wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src,
((uint64_t)(self->private_impl.f_sub_chunk_length)));
if (a_src) {
size_t n = ((size_t)(io2_a_src - a_src->data.ptr));
a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n);
a_src->meta.wi = n;
}
v_r_mark = ((uint64_t)(iop_a_src - io0_a_src));
if (self->private_impl.f_is_vp8_lossy) {
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_3 = wuffs_vp8__decoder__decode_image_config(&self->private_data.f_vp8, a_dst, a_src);
v_status = t_3;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
} else {
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_4 = wuffs_webp__decoder__do_decode_image_config_limited_vp8l(self, a_src);
v_status = t_4;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
}
wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_sub_chunk_length, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src))))));
io2_a_src = o_0_io2_a_src;
if (a_src) {
a_src->meta.closed = o_0_closed_a_src;
a_src->meta.wi = ((size_t)(io2_a_src - a_src->data.ptr));
}
}
if (wuffs_base__status__is_ok(&v_status)) {
break;
} else if ( ! wuffs_base__status__is_suspension(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_sub_chunk_length == 0u)) {
status = wuffs_base__make_status(wuffs_webp__error__short_chunk);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(7);
}
ok:
self->private_impl.p_do_decode_image_config_limited = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_image_config_limited = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.do_decode_image_config_limited_vp8l
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited_vp8l(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_c32 = 0;
uint32_t v_transform_size = 0;
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_image_config_limited_vp8l;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint32_t t_0 = *iop_a_src++;
v_c32 = t_0;
}
if (v_c32 != 47u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited_vp8l.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited_vp8l.scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8u;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
v_c32 = t_1;
}
self->private_impl.f_width = ((v_c32 & 16383u) + 1u);
v_c32 >>= 14u;
self->private_impl.f_height = ((v_c32 & 16383u) + 1u);
v_c32 >>= 14u;
self->private_impl.f_pixfmt = 2415954056u;
if ((v_c32 & 1u) != 0u) {
self->private_impl.f_pixfmt = 2164295816u;
}
v_c32 >>= 1u;
if (v_c32 != 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
v_transform_size = (4u * ((self->private_impl.f_width + 3u) >> 2u) * ((self->private_impl.f_height + 3u) >> 2u));
self->private_impl.f_workbuf_offset_for_transform[0u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + 0u);
self->private_impl.f_workbuf_offset_for_transform[1u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + v_transform_size);
self->private_impl.f_workbuf_offset_for_transform[2u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (2u * v_transform_size));
self->private_impl.f_workbuf_offset_for_transform[3u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (3u * v_transform_size));
goto ok;
ok:
self->private_impl.p_do_decode_image_config_limited_vp8l = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_image_config_limited_vp8l = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.decode_frame_config
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_webp__decoder__decode_frame_config(
wuffs_webp__decoder* self,
wuffs_base__frame_config* a_dst,
wuffs_base__io_buffer* a_src) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (!a_src) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 2)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t coro_susp_point = self->private_impl.p_decode_frame_config;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
if (self->private_impl.f_is_vp8_lossy) {
{
wuffs_base__status t_0 = wuffs_vp8__decoder__decode_frame_config(&self->private_data.f_vp8, a_dst, a_src);
v_status = t_0;
}
} else {
{
wuffs_base__status t_1 = wuffs_webp__decoder__do_decode_frame_config(self, a_dst, a_src);
v_status = t_1;
}
}
if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) {
status = wuffs_base__make_status(wuffs_webp__error__truncated_input);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
ok:
self->private_impl.p_decode_frame_config = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_frame_config = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 2 : 0;
goto exit;
exit:
if (wuffs_base__status__is_error(&status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
}
// -------- func webp.decoder.do_decode_frame_config
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_frame_config(
wuffs_webp__decoder* self,
wuffs_base__frame_config* a_dst,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__pixel_format v_pixfmt = {0};
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_frame_config;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence == 32u) {
} else if (self->private_impl.f_call_sequence < 32u) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_webp__decoder__do_decode_image_config(self, NULL, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
} else if (self->private_impl.f_call_sequence == 40u) {
if (self->private_impl.f_frame_config_io_position != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) {
status = wuffs_base__make_status(wuffs_base__error__bad_restart);
goto exit;
}
} else if (self->private_impl.f_call_sequence == 64u) {
self->private_impl.f_call_sequence = 96u;
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
} else {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
}
if (a_dst != NULL) {
v_pixfmt = wuffs_base__utility__make_pixel_format(self->private_impl.f_pixfmt);
wuffs_base__frame_config__set(
a_dst,
wuffs_base__utility__make_rect_ie_u32(
0u,
0u,
self->private_impl.f_width,
self->private_impl.f_height),
((wuffs_base__flicks)(0u)),
0u,
self->private_impl.f_frame_config_io_position,
0u,
false,
false,
wuffs_base__pixel_format__default_background_color(&v_pixfmt));
}
self->private_impl.f_call_sequence = 64u;
ok:
self->private_impl.p_do_decode_frame_config = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_frame_config = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.decode_frame
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_webp__decoder__decode_frame(
wuffs_webp__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__pixel_blend a_blend,
wuffs_base__slice_u8 a_workbuf,
wuffs_base__decode_frame_options* a_opts) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (!a_dst || !a_src) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 3)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t coro_susp_point = self->private_impl.p_decode_frame;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
if (self->private_impl.f_is_vp8_lossy) {
{
wuffs_base__status t_0 = wuffs_vp8__decoder__decode_frame(&self->private_data.f_vp8,
a_dst,
a_src,
a_blend,
a_workbuf,
a_opts);
v_status = t_0;
}
} else {
{
wuffs_base__status t_1 = wuffs_webp__decoder__do_decode_frame(self,
a_dst,
a_src,
a_blend,
a_workbuf,
a_opts);
v_status = t_1;
}
}
if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) {
status = wuffs_base__make_status(wuffs_webp__error__truncated_input);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
ok:
self->private_impl.p_decode_frame = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_frame = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 3 : 0;
goto exit;
exit:
if (wuffs_base__status__is_error(&status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
}
// -------- func webp.decoder.do_decode_frame
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_frame(
wuffs_webp__decoder__do_decode_frame_vp8l(
wuffs_webp__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__io_buffer* a_src,
@@ -89915,30 +90256,13 @@ wuffs_webp__decoder__do_decode_frame(
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_frame;
uint32_t coro_susp_point = self->private_impl.p_do_decode_frame_vp8l;
if (coro_susp_point) {
v_width = self->private_data.s_do_decode_frame.v_width;
v_width = self->private_data.s_do_decode_frame_vp8l.v_width;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence == 64u) {
} else if (self->private_impl.f_call_sequence < 64u) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_webp__decoder__do_decode_frame_config(self, NULL, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
} else {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
}
self->private_impl.f_seen_transform[0u] = false;
self->private_impl.f_seen_transform[1u] = false;
self->private_impl.f_seen_transform[2u] = false;
@@ -89947,7 +90271,7 @@ wuffs_webp__decoder__do_decode_frame(
while (true) {
if (self->private_impl.f_n_bits < 1u) {
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
@@ -89967,7 +90291,7 @@ wuffs_webp__decoder__do_decode_frame(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
status = wuffs_webp__decoder__decode_transform(self, a_src, a_workbuf);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -89983,7 +90307,7 @@ wuffs_webp__decoder__do_decode_frame(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
status = wuffs_webp__decoder__decode_color_cache_parameters(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -89995,7 +90319,7 @@ wuffs_webp__decoder__do_decode_frame(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
status = wuffs_webp__decoder__decode_hg_table(self, a_src, v_width, a_workbuf);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -90007,7 +90331,7 @@ wuffs_webp__decoder__do_decode_frame(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
status = wuffs_webp__decoder__decode_huffman_groups(self, a_src, self->private_impl.f_overall_n_huffman_groups);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -90016,10 +90340,7 @@ wuffs_webp__decoder__do_decode_frame(
goto suspend;
}
while (true) {
if ((((uint64_t)(self->private_impl.f_workbuf_offset_for_color_indexing)) > ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u]))) ||
(((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u])) > ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[1u]))) ||
(((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[1u])) > ((uint64_t)(a_workbuf.len))) ||
(((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u])) > ((uint64_t)(a_workbuf.len)))) {
if ((((uint64_t)(self->private_impl.f_workbuf_offset_for_color_indexing)) > ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u]))) || (((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u])) > ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[1u]))) || (((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[1u])) > ((uint64_t)(a_workbuf.len)))) {
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
goto exit;
}
@@ -90049,13 +90370,18 @@ wuffs_webp__decoder__do_decode_frame(
break;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(7);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(6);
}
if (((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u])) > ((uint64_t)(a_workbuf.len))) {
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
goto exit;
}
v_pix = wuffs_base__slice_u8__subslice_j(a_workbuf, ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u])));
v_width = self->private_impl.f_width;
if (self->private_impl.f_seen_transform[3u] && (((uint64_t)(self->private_impl.f_workbuf_offset_for_color_indexing)) <= ((uint64_t)(v_pix.len)))) {
v_pix = wuffs_base__slice_u8__subslice_i(v_pix, ((uint64_t)(self->private_impl.f_workbuf_offset_for_color_indexing)));
v_width = self->private_impl.f_color_indexing_width;
}
v_which = self->private_impl.f_n_transforms;
while (v_which > 0u) {
v_which -= 1u;
@@ -90069,14 +90395,15 @@ wuffs_webp__decoder__do_decode_frame(
}
}
if (v_transform_type == 0u) {
wuffs_webp__decoder__apply_transform_predictor(self, v_pix, v_tile_data);
wuffs_webp__decoder__apply_transform_predictor(self, v_pix, v_width, v_tile_data);
} else if (v_transform_type == 1u) {
wuffs_webp__decoder__apply_transform_cross_color(self, v_pix, v_tile_data);
wuffs_webp__decoder__apply_transform_cross_color(self, v_pix, v_width, v_tile_data);
} else if (v_transform_type == 2u) {
wuffs_webp__decoder__apply_transform_subtract_green(self, v_pix);
} else {
wuffs_webp__decoder__apply_transform_color_indexing(self, v_pix);
} else if (((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u])) <= ((uint64_t)(a_workbuf.len))) {
v_pix = wuffs_base__slice_u8__subslice_j(a_workbuf, ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[0u])));
v_width = self->private_impl.f_width;
wuffs_webp__decoder__apply_transform_color_indexing(self, v_pix, v_width);
}
}
v_status = wuffs_webp__decoder__swizzle(self, a_dst, v_pix, a_blend);
@@ -90093,14 +90420,14 @@ wuffs_webp__decoder__do_decode_frame(
self->private_impl.f_call_sequence = 96u;
ok:
self->private_impl.p_do_decode_frame = 0;
self->private_impl.p_do_decode_frame_vp8l = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_frame = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_data.s_do_decode_frame.v_width = v_width;
self->private_impl.p_do_decode_frame_vp8l = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_data.s_do_decode_frame_vp8l.v_width = v_width;
goto exit;
exit:
@@ -90125,6 +90452,7 @@ wuffs_webp__decoder__decode_transform(
uint8_t v_c8 = 0;
uint32_t v_transform_type = 0;
uint32_t v_tile_size_log2 = 0;
uint32_t v_width = 0;
wuffs_base__slice_u8 v_p = {0};
const uint8_t* iop_a_src = NULL;
@@ -90169,9 +90497,6 @@ wuffs_webp__decoder__decode_transform(
if (self->private_impl.f_seen_transform[v_transform_type] || (self->private_impl.f_n_transforms >= 4u)) {
status = wuffs_base__make_status(wuffs_webp__error__bad_transform);
goto exit;
} else if (self->private_impl.f_seen_transform[3u]) {
status = wuffs_base__make_status(wuffs_webp__error__unsupported_transform_after_color_indexing_transform);
goto exit;
}
self->private_impl.f_seen_transform[v_transform_type] = true;
self->private_impl.f_transform_type[self->private_impl.f_n_transforms] = ((uint8_t)(v_transform_type));
@@ -90221,6 +90546,10 @@ wuffs_webp__decoder__decode_transform(
goto suspend;
}
while (true) {
v_width = self->private_impl.f_width;
if (self->private_impl.f_seen_transform[3u]) {
v_width = self->private_impl.f_color_indexing_width;
}
if ((((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[(v_transform_type + 1u)])) > ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[(v_transform_type + 2u)]))) || (((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[(v_transform_type + 2u)])) > ((uint64_t)(a_workbuf.len)))) {
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
goto exit;
@@ -90234,7 +90563,7 @@ wuffs_webp__decoder__decode_transform(
((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[(v_transform_type + 1u)])),
((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[(v_transform_type + 2u)]))),
a_src,
((self->private_impl.f_width + ((((uint32_t)(1u)) << v_tile_size_log2) - 1u)) >> v_tile_size_log2),
((v_width + ((((uint32_t)(1u)) << v_tile_size_log2) - 1u)) >> v_tile_size_log2),
((self->private_impl.f_height + ((((uint32_t)(1u)) << v_tile_size_log2) - 1u)) >> v_tile_size_log2),
wuffs_base__utility__empty_slice_u8(),
0u);
@@ -90720,10 +91049,13 @@ wuffs_webp__decoder__swizzle(
wuffs_base__slice_u8 v_dst = {0};
uint32_t v_y = 0;
if (a_dst == NULL) {
return wuffs_base__make_status(NULL);
}
v_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler,
wuffs_base__pixel_buffer__pixel_format(a_dst),
wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_palette, 1024)),
wuffs_base__utility__make_pixel_format(self->private_impl.f_pixfmt),
wuffs_base__utility__make_pixel_format(2164295816u),
wuffs_base__utility__empty_slice_u8(),
a_blend);
if ( ! wuffs_base__status__is_ok(&v_status)) {
@@ -90751,6 +91083,1308 @@ wuffs_webp__decoder__swizzle(
return wuffs_base__make_status(NULL);
}
// -------- func webp.decoder.get_quirk
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_webp__decoder__get_quirk(
const wuffs_webp__decoder* self,
uint32_t a_key) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return 0u;
}
// -------- func webp.decoder.set_quirk
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_webp__decoder__set_quirk(
wuffs_webp__decoder* self,
uint32_t a_key,
uint64_t a_value) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
// -------- func webp.decoder.decode_image_config
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_webp__decoder__decode_image_config(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (!a_src) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 1)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t coro_susp_point = self->private_impl.p_decode_image_config;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
{
wuffs_base__status t_0 = wuffs_webp__decoder__do_decode_image_config(self, a_dst, a_src);
v_status = t_0;
}
if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) {
status = wuffs_base__make_status(wuffs_webp__error__truncated_input);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
ok:
self->private_impl.p_decode_image_config = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_image_config = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 1 : 0;
goto exit;
exit:
if (wuffs_base__status__is_error(&status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
}
// -------- func webp.decoder.do_decode_image_config
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_c32 = 0;
uint64_t v_r_mark = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_image_config;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence != 0u) {
status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config.scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_0;
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch));
break;
}
num_bits_0 += 8u;
*scratch |= ((uint64_t)(num_bits_0)) << 56;
}
}
v_c32 = t_0;
}
if (v_c32 != 1179011410u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config.scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8u;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
self->private_impl.f_riff_chunk_length = t_1;
}
if ((self->private_impl.f_riff_chunk_length & 1u) != 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
self->private_impl.f_rcl_remaining = self->private_impl.f_riff_chunk_length;
while (true) {
{
const bool o_0_closed_a_src = a_src->meta.closed;
const uint8_t* o_0_io2_a_src = io2_a_src;
wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src,
((uint64_t)(self->private_impl.f_rcl_remaining)));
if (a_src) {
size_t n = ((size_t)(io2_a_src - a_src->data.ptr));
a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n);
a_src->meta.wi = n;
}
v_r_mark = ((uint64_t)(iop_a_src - io0_a_src));
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_2 = wuffs_webp__decoder__do_decode_image_config_limited(self, a_dst, a_src);
v_status = t_2;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_rcl_remaining, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src))))));
io2_a_src = o_0_io2_a_src;
if (a_src) {
a_src->meta.closed = o_0_closed_a_src;
a_src->meta.wi = ((size_t)(io2_a_src - a_src->data.ptr));
}
}
if (wuffs_base__status__is_ok(&v_status)) {
break;
} else if ( ! wuffs_base__status__is_suspension(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_rcl_remaining == 0u)) {
status = wuffs_base__make_status(wuffs_webp__error__short_chunk);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(5);
}
self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)));
if ((self->private_impl.f_variant != 1u) && (a_dst != NULL)) {
wuffs_base__image_config__set(
a_dst,
2164295816u,
0u,
self->private_impl.f_width,
self->private_impl.f_height,
self->private_impl.f_frame_config_io_position,
false);
}
self->private_impl.f_call_sequence = 32u;
ok:
self->private_impl.p_do_decode_image_config = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_image_config = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.do_decode_image_config_limited
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited(
wuffs_webp__decoder* self,
wuffs_base__image_config* a_dst,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_c32 = 0;
uint64_t v_r_mark = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_image_config_limited;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited.scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_0;
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch));
break;
}
num_bits_0 += 8u;
*scratch |= ((uint64_t)(num_bits_0)) << 56;
}
}
v_c32 = t_0;
}
if (v_c32 != 1346520407u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited.scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8u;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
v_c32 = t_1;
}
if (v_c32 == 540561494u) {
self->private_impl.f_variant = 1u;
} else if (v_c32 == 1278758998u) {
self->private_impl.f_variant = 2u;
} else if (v_c32 == 1480085590u) {
self->private_impl.f_variant = 4u;
} else {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
uint32_t t_2;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_2 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited.scratch;
uint32_t num_bits_2 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_2;
if (num_bits_2 == 24) {
t_2 = ((uint32_t)(*scratch));
break;
}
num_bits_2 += 8u;
*scratch |= ((uint64_t)(num_bits_2)) << 56;
}
}
self->private_impl.f_sub_chunk_length = t_2;
}
self->private_impl.f_sub_chunk_has_padding = ((self->private_impl.f_sub_chunk_length & 1u) != 0u);
if (self->private_impl.f_variant == 1u) {
wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(self->private_impl.f_sub_chunk_length)) << 1u) | 1u));
} else if (self->private_impl.f_variant == 2u) {
} else if (self->private_impl.f_sub_chunk_length != 10u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
while (true) {
{
const bool o_0_closed_a_src = a_src->meta.closed;
const uint8_t* o_0_io2_a_src = io2_a_src;
wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src,
((uint64_t)(self->private_impl.f_sub_chunk_length)));
if (a_src) {
size_t n = ((size_t)(io2_a_src - a_src->data.ptr));
a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n);
a_src->meta.wi = n;
}
v_r_mark = ((uint64_t)(iop_a_src - io0_a_src));
if (self->private_impl.f_variant == 1u) {
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_3 = wuffs_vp8__decoder__decode_image_config(&self->private_data.f_vp8, a_dst, a_src);
v_status = t_3;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
} else if (self->private_impl.f_variant == 2u) {
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_4 = wuffs_webp__decoder__do_decode_image_config_limited_vp8l(self, a_src);
v_status = t_4;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
} else {
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_5 = wuffs_webp__decoder__do_decode_image_config_limited_vp8x(self, a_src);
v_status = t_5;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
}
wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_sub_chunk_length, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src))))));
io2_a_src = o_0_io2_a_src;
if (a_src) {
a_src->meta.closed = o_0_closed_a_src;
a_src->meta.wi = ((size_t)(io2_a_src - a_src->data.ptr));
}
}
if (wuffs_base__status__is_ok(&v_status)) {
break;
} else if ( ! wuffs_base__status__is_suspension(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_sub_chunk_length == 0u)) {
status = wuffs_base__make_status(wuffs_webp__error__short_chunk);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(7);
}
ok:
self->private_impl.p_do_decode_image_config_limited = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_image_config_limited = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.do_decode_image_config_limited_vp8l
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited_vp8l(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_c32 = 0;
uint32_t v_transform_size = 0;
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_image_config_limited_vp8l;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint32_t t_0 = *iop_a_src++;
v_c32 = t_0;
}
if (v_c32 != 47u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited_vp8l.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited_vp8l.scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8u;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
v_c32 = t_1;
}
self->private_impl.f_width = ((v_c32 & 16383u) + 1u);
v_c32 >>= 14u;
self->private_impl.f_height = ((v_c32 & 16383u) + 1u);
v_c32 >>= 14u;
v_c32 >>= 1u;
if (v_c32 != 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
goto exit;
}
v_transform_size = (4u * ((self->private_impl.f_width + 3u) >> 2u) * ((self->private_impl.f_height + 3u) >> 2u));
self->private_impl.f_workbuf_offset_for_transform[0u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + 0u);
self->private_impl.f_workbuf_offset_for_transform[1u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + v_transform_size);
self->private_impl.f_workbuf_offset_for_transform[2u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (2u * v_transform_size));
self->private_impl.f_workbuf_offset_for_transform[3u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (3u * v_transform_size));
goto ok;
ok:
self->private_impl.p_do_decode_image_config_limited_vp8l = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_image_config_limited_vp8l = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.do_decode_image_config_limited_vp8x
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_image_config_limited_vp8x(
wuffs_webp__decoder* self,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_c32 = 0;
uint32_t v_transform_size = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_image_config_limited_vp8x;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_image_config_limited_vp8x.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited_vp8x.scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_0;
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch));
break;
}
num_bits_0 += 8u;
*scratch |= ((uint64_t)(num_bits_0)) << 56;
}
}
v_c32 = t_0;
}
if ((v_c32 & 4294967233u) != 0u) {
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
goto exit;
}
self->private_impl.f_vp8x_header_flags = ((uint8_t)((v_c32 | 1u)));
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 3)) {
t_1 = ((uint32_t)(wuffs_base__peek_u24le__no_bounds_check(iop_a_src)));
iop_a_src += 3;
} else {
self->private_data.s_do_decode_image_config_limited_vp8x.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited_vp8x.scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 16) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8u;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
v_c32 = t_1;
}
if (v_c32 >= 16383u) {
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
goto exit;
}
self->private_impl.f_width = ((v_c32 & 16383u) + 1u);
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
uint32_t t_2;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 3)) {
t_2 = ((uint32_t)(wuffs_base__peek_u24le__no_bounds_check(iop_a_src)));
iop_a_src += 3;
} else {
self->private_data.s_do_decode_image_config_limited_vp8x.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_image_config_limited_vp8x.scratch;
uint32_t num_bits_2 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_2;
if (num_bits_2 == 16) {
t_2 = ((uint32_t)(*scratch));
break;
}
num_bits_2 += 8u;
*scratch |= ((uint64_t)(num_bits_2)) << 56;
}
}
v_c32 = t_2;
}
if (v_c32 >= 16383u) {
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
goto exit;
}
self->private_impl.f_height = ((v_c32 & 16383u) + 1u);
v_transform_size = (4u * ((self->private_impl.f_width + 3u) >> 2u) * ((self->private_impl.f_height + 3u) >> 2u));
self->private_impl.f_workbuf_offset_for_transform[0u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + 0u);
self->private_impl.f_workbuf_offset_for_transform[1u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + v_transform_size);
self->private_impl.f_workbuf_offset_for_transform[2u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (2u * v_transform_size));
self->private_impl.f_workbuf_offset_for_transform[3u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (3u * v_transform_size));
v_status = wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(self->private_impl.f_riff_chunk_length)) << 1u) | 1u));
if ( ! wuffs_base__status__is_ok(&v_status)) {
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
goto exit;
}
v_status = wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 1836840961u, (((uint64_t)(self->private_impl.f_width)) | (((uint64_t)(self->private_impl.f_height)) << 32u)));
if ( ! wuffs_base__status__is_ok(&v_status)) {
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
goto exit;
}
goto ok;
ok:
self->private_impl.p_do_decode_image_config_limited_vp8x = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_image_config_limited_vp8x = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.decode_frame_config
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_webp__decoder__decode_frame_config(
wuffs_webp__decoder* self,
wuffs_base__frame_config* a_dst,
wuffs_base__io_buffer* a_src) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (!a_src) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 2)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t coro_susp_point = self->private_impl.p_decode_frame_config;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
if (self->private_impl.f_variant == 1u) {
{
wuffs_base__status t_0 = wuffs_vp8__decoder__decode_frame_config(&self->private_data.f_vp8, a_dst, a_src);
v_status = t_0;
}
} else {
{
wuffs_base__status t_1 = wuffs_webp__decoder__do_decode_frame_config(self, a_dst, a_src);
v_status = t_1;
}
}
if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) {
status = wuffs_base__make_status(wuffs_webp__error__truncated_input);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
ok:
self->private_impl.p_decode_frame_config = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_frame_config = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 2 : 0;
goto exit;
exit:
if (wuffs_base__status__is_error(&status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
}
// -------- func webp.decoder.do_decode_frame_config
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_frame_config(
wuffs_webp__decoder* self,
wuffs_base__frame_config* a_dst,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_frame_config;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence == 32u) {
} else if (self->private_impl.f_call_sequence < 32u) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_webp__decoder__do_decode_image_config(self, NULL, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
} else if (self->private_impl.f_call_sequence == 40u) {
if (self->private_impl.f_frame_config_io_position != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) {
status = wuffs_base__make_status(wuffs_base__error__bad_restart);
goto exit;
}
} else if (self->private_impl.f_call_sequence == 64u) {
self->private_impl.f_call_sequence = 96u;
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
} else {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
}
if ((self->private_impl.f_variant != 1u) && (a_dst != NULL)) {
wuffs_base__frame_config__set(
a_dst,
wuffs_base__utility__make_rect_ie_u32(
0u,
0u,
self->private_impl.f_width,
self->private_impl.f_height),
((wuffs_base__flicks)(0u)),
0u,
self->private_impl.f_frame_config_io_position,
0u,
false,
false,
0u);
}
self->private_impl.f_call_sequence = 64u;
ok:
self->private_impl.p_do_decode_frame_config = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_frame_config = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.decode_frame
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_webp__decoder__decode_frame(
wuffs_webp__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__pixel_blend a_blend,
wuffs_base__slice_u8 a_workbuf,
wuffs_base__decode_frame_options* a_opts) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (!a_dst || !a_src) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 3)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t coro_susp_point = self->private_impl.p_decode_frame;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
{
wuffs_base__status t_0 = wuffs_webp__decoder__do_decode_frame(self,
a_dst,
a_src,
a_blend,
a_workbuf,
a_opts);
v_status = t_0;
}
if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) {
status = wuffs_base__make_status(wuffs_webp__error__truncated_input);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
ok:
self->private_impl.p_decode_frame = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_frame = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 3 : 0;
goto exit;
exit:
if (wuffs_base__status__is_error(&status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
}
// -------- func webp.decoder.do_decode_frame
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__status
wuffs_webp__decoder__do_decode_frame(
wuffs_webp__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__pixel_blend a_blend,
wuffs_base__slice_u8 a_workbuf,
wuffs_base__decode_frame_options* a_opts) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint64_t v_r_mark = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_do_decode_frame;
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence == 64u) {
} else if (self->private_impl.f_call_sequence < 64u) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_webp__decoder__do_decode_frame_config(self, NULL, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
} else {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
}
while (((uint8_t)(self->private_impl.f_variant & 3u)) == 0u) {
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_frame.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_frame.scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_0;
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch));
break;
}
num_bits_0 += 8u;
*scratch |= ((uint64_t)(num_bits_0)) << 56;
}
}
self->private_impl.f_sub_chunk_type = t_0;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_do_decode_frame.scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_frame.scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8u;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
self->private_impl.f_sub_chunk_length = t_1;
}
self->private_impl.f_sub_chunk_has_padding = ((self->private_impl.f_sub_chunk_length & 1u) != 0u);
if (self->private_impl.f_sub_chunk_length > 2147483647u) {
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
goto exit;
}
if (self->private_impl.f_sub_chunk_type == 1346585417u) {
if (self->private_impl.f_variant > 4u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_chunk_order);
goto exit;
} else if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 32u)) == 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk);
goto exit;
}
self->private_impl.f_variant = 8u;
} else if (self->private_impl.f_sub_chunk_type == 1296649793u) {
if (self->private_impl.f_variant > 8u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_chunk_order);
goto exit;
} else if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 2u)) == 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk);
goto exit;
}
self->private_impl.f_variant = 16u;
} else if (self->private_impl.f_sub_chunk_type == 1179471425u) {
if (self->private_impl.f_variant > 16u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_chunk_order);
goto exit;
} else if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 2u)) == 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk);
goto exit;
}
self->private_impl.f_variant = 32u;
} else if (self->private_impl.f_sub_chunk_type == 1213221953u) {
if (self->private_impl.f_variant > 32u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_chunk_order);
goto exit;
} else if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 16u)) == 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk);
goto exit;
}
self->private_impl.f_variant = 64u;
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
status = wuffs_webp__decoder__decode_alph(self, a_src, a_workbuf);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 1836840960u, 1u);
} else if (self->private_impl.f_sub_chunk_type == 540561494u) {
wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(self->private_impl.f_sub_chunk_length)) << 1u) | 1u));
self->private_impl.f_variant = 129u;
break;
} else if (self->private_impl.f_sub_chunk_type == 1278758998u) {
if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 16u)) != 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_alph_vp8l_combination);
goto exit;
}
self->private_impl.f_variant = 130u;
break;
} else if (self->private_impl.f_sub_chunk_type == 542133592u) {
if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 4u)) == 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk);
goto exit;
}
} else if (self->private_impl.f_sub_chunk_type == 1179211845u) {
if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 8u)) == 0u) {
status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk);
goto exit;
}
}
self->private_data.s_do_decode_frame.scratch = self->private_impl.f_sub_chunk_length;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
if (self->private_data.s_do_decode_frame.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_do_decode_frame.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
iop_a_src = io2_a_src;
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
iop_a_src += self->private_data.s_do_decode_frame.scratch;
if (self->private_impl.f_sub_chunk_has_padding) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
iop_a_src++;
}
}
while (true) {
{
const bool o_0_closed_a_src = a_src->meta.closed;
const uint8_t* o_0_io2_a_src = io2_a_src;
wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src,
((uint64_t)(self->private_impl.f_sub_chunk_length)));
if (a_src) {
size_t n = ((size_t)(io2_a_src - a_src->data.ptr));
a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n);
a_src->meta.wi = n;
}
v_r_mark = ((uint64_t)(iop_a_src - io0_a_src));
if (((uint8_t)(self->private_impl.f_variant & 1u)) != 0u) {
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_2 = wuffs_vp8__decoder__decode_frame(&self->private_data.f_vp8,
a_dst,
a_src,
a_blend,
a_workbuf,
a_opts);
v_status = t_2;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
} else {
{
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
wuffs_base__status t_3 = wuffs_webp__decoder__do_decode_frame_vp8l(self,
a_dst,
a_src,
a_blend,
a_workbuf,
a_opts);
v_status = t_3;
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
}
}
wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_sub_chunk_length, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src))))));
io2_a_src = o_0_io2_a_src;
if (a_src) {
a_src->meta.closed = o_0_closed_a_src;
a_src->meta.wi = ((size_t)(io2_a_src - a_src->data.ptr));
}
}
if (wuffs_base__status__is_ok(&v_status)) {
break;
} else if ( ! wuffs_base__status__is_suspension(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_sub_chunk_length == 0u)) {
status = wuffs_base__make_status(wuffs_webp__error__short_chunk);
goto exit;
}
status = v_status;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9);
}
ok:
self->private_impl.p_do_decode_frame = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_do_decode_frame = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func webp.decoder.frame_dirty_rect
WUFFS_BASE__GENERATED_C_CODE
@@ -90765,7 +92399,7 @@ wuffs_webp__decoder__frame_dirty_rect(
return wuffs_base__utility__empty_rect_ie_u32();
}
if (self->private_impl.f_is_vp8_lossy) {
if (self->private_impl.f_variant == 1u) {
return wuffs_vp8__decoder__frame_dirty_rect(&self->private_data.f_vp8);
}
return wuffs_base__utility__make_rect_ie_u32(
@@ -90806,7 +92440,7 @@ wuffs_webp__decoder__num_decoded_frame_configs(
return 0;
}
if (self->private_impl.f_is_vp8_lossy) {
if (self->private_impl.f_variant == 1u) {
return wuffs_vp8__decoder__num_decoded_frame_configs(&self->private_data.f_vp8);
}
if (self->private_impl.f_call_sequence > 32u) {
@@ -90829,7 +92463,7 @@ wuffs_webp__decoder__num_decoded_frames(
return 0;
}
if (self->private_impl.f_is_vp8_lossy) {
if (self->private_impl.f_variant == 1u) {
return wuffs_vp8__decoder__num_decoded_frames(&self->private_data.f_vp8);
}
if (self->private_impl.f_call_sequence > 64u) {
@@ -90858,7 +92492,7 @@ wuffs_webp__decoder__restart_frame(
wuffs_base__status v_status = wuffs_base__make_status(NULL);
if (self->private_impl.f_is_vp8_lossy) {
if (self->private_impl.f_variant == 1u) {
v_status = wuffs_vp8__decoder__restart_frame(&self->private_data.f_vp8, a_index, a_io_position);
return wuffs_private_impl__status__ensure_not_a_suspension(v_status);
}
@@ -90940,10 +92574,21 @@ wuffs_webp__decoder__workbuf_len(
return wuffs_base__utility__empty_range_ii_u64();
}
if (self->private_impl.f_is_vp8_lossy) {
wuffs_base__range_ii_u64 v_r = {0};
wuffs_base__range_ii_u64 v_s = {0};
if (self->private_impl.f_variant == 1u) {
return wuffs_vp8__decoder__workbuf_len(&self->private_data.f_vp8);
}
return wuffs_base__utility__make_range_ii_u64(((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[3u])), ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[3u])));
v_r = wuffs_base__utility__make_range_ii_u64(((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[3u])), ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[3u])));
if (self->private_impl.f_variant != 2u) {
v_s = wuffs_vp8__decoder__workbuf_len(&self->private_data.f_vp8);
if (wuffs_base__range_ii_u64__is_empty(&v_s)) {
return v_s;
}
v_r = wuffs_base__utility__make_range_ii_u64(wuffs_base__u64__max(wuffs_private_impl__range_ii_u64__get_min_incl(&v_s), wuffs_private_impl__range_ii_u64__get_min_incl(&v_r)), wuffs_base__u64__max(wuffs_private_impl__range_ii_u64__get_max_incl(&v_s), wuffs_private_impl__range_ii_u64__get_max_incl(&v_r)));
}
return v_r;
}
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__WEBP)
@@ -95498,8 +97143,9 @@ redirect:
}
wuffs_base__pixel_buffer pixel_buffer = alloc_pixbuf_result.pixbuf;
if (valid_background_color) {
wuffs_base__status pb_scufr_status = pixel_buffer.set_color_u32_fill_rect(
pixel_buffer.pixcfg.bounds(), background_color);
wuffs_base__status pb_scufr_status =
pixel_buffer.set_color_u32_argb_premul_fill_rect(
pixel_buffer.pixcfg.bounds(), background_color);
if (pb_scufr_status.repr != nullptr) {
return DecodeImageResult(pb_scufr_status.message());
}
@@ -95508,6 +97154,9 @@ redirect:
// Allocate the work buffer. Wuffs' decoders conventionally assume that this
// can be uninitialized memory.
wuffs_base__range_ii_u64 workbuf_len = image_decoder->workbuf_len();
if (workbuf_len.is_empty()) {
return DecodeImageResult(DecodeImage_UnsupportedImageFormat);
}
DecodeImageCallbacks::AllocWorkbufResult alloc_workbuf_result =
callbacks.AllocWorkbuf(workbuf_len, true);
if (!alloc_workbuf_result.error_message.empty()) {
@@ -96448,7 +98097,12 @@ wuffs_drop_in__stb__load1( //
}
uint64_t pixbuf_len = (uint64_t)w * (uint64_t)h * (uint64_t)desired_channels;
uint64_t workbuf_len = wuffs_base__image_decoder__workbuf_len(dec).max_incl;
wuffs_base__range_ii_u64 r = wuffs_base__image_decoder__workbuf_len(dec);
if (wuffs_base__range_ii_u64__is_empty(&r)) {
wuffs_drop_in__stb__g_failure_reason = "indeterminate workbuf length";
return NULL;
}
uint64_t workbuf_len = r.max_incl;
#if SIZE_MAX < 0xFFFFFFFFFFFFFFFFull
if ((pixbuf_len > ((uint64_t)SIZE_MAX)) ||
(workbuf_len > ((uint64_t)SIZE_MAX))) {
+6 -6
View File
@@ -85,15 +85,15 @@ extern "C" {
// each major.minor branch, the commit count should increase monotonically.
//
// WUFFS_VERSION was overridden by "wuffs gen -version" based on revision
// a138188d5742c0469de983878a430bdbe7e50e77 committed on 2024-04-19.
#define WUFFS_VERSION 0x000030004
// 40445d992b97be816cd09b0460c55e6a160389f6 committed on 2026-07-11.
#define WUFFS_VERSION 0x000030005
#define WUFFS_VERSION_MAJOR 0
#define WUFFS_VERSION_MINOR 3
#define WUFFS_VERSION_PATCH 4
#define WUFFS_VERSION_PATCH 5
#define WUFFS_VERSION_PRE_RELEASE_LABEL ""
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 3401
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 20240419
#define WUFFS_VERSION_STRING "0.3.4+3401.20240419"
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 3404
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 20260711
#define WUFFS_VERSION_STRING "0.3.5+3404.20260711"
// ---------------- Configuration
+29 -12
View File
@@ -60,18 +60,35 @@ func main1() error {
}
} else if len(arg) == 12 {
// Assume that arg is of the form "aaaa/bbbb/cc".
arg0 := arg[0:4]
arg1 := arg[5:9]
arg2 := ".." + arg[10:12]
c0, ok0 := base38.Encode(arg0)
c1, ok1 := base38.Encode(arg1)
c2, ok2 := base38.Encode(arg2)
if ok0 && ok1 && ok2 {
c := (uint64(c0) << 32) | (uint64(c1) << 11) | uint64(c2)
code := fmt.Sprintf("0x%014X", c)
fmt.Printf("%s\t%16d\t%s\n", underscore8(code), c, arg)
continue
if (arg[4] == '/') && (arg[7] == '/') {
// Assume that arg is of the form "aaaa/bb/cccc".
arg0 := arg[0:4]
arg1 := ".." + arg[5:7]
arg2 := arg[8:12]
c0, ok0 := base38.Encode(arg0)
c1, ok1 := base38.Encode(arg1)
c2, ok2 := base38.Encode(arg2)
if ok0 && ok1 && ok2 {
c := (uint64(c0) << 32) | (uint64(c1) << 21) | uint64(c2)
code := fmt.Sprintf("0x%014X", c)
fmt.Printf("%s\t%16d\t%s\n", underscore8(code), c, arg)
continue
}
} else if (arg[4] == '/') && (arg[9] == '/') {
// Assume that arg is of the form "aaaa/bbbb/cc".
arg0 := arg[0:4]
arg1 := arg[5:9]
arg2 := ".." + arg[10:12]
c0, ok0 := base38.Encode(arg0)
c1, ok1 := base38.Encode(arg1)
c2, ok2 := base38.Encode(arg2)
if ok0 && ok1 && ok2 {
c := (uint64(c0) << 32) | (uint64(c1) << 11) | uint64(c2)
code := fmt.Sprintf("0x%014X", c)
fmt.Printf("%s\t%16d\t%s\n", underscore8(code), c, arg)
continue
}
}
} else if len(arg) == 14 {
+105
View File
@@ -0,0 +1,105 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// ----------------
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define BASIC_HANDSUM_DECODE_CONFIG__STATIC_FUNCTIONS
#define BASIC_HANDSUM_DECODE_IMPLEMENTATION
#include "../snippet/basic-handsum-decode.c"
// ----
// Hard-code a color=3, quality-4 Handsum image file, known in this repo as
// test/data/mona-lisa.21x32.c3q4.handsum
const uint8_t g_src_ptr__mona_lisa_21_32_c3q4_handsum[] = {
0xfe, 0xd7, 0x7a, 0x87, 0x76, 0x68, 0x87, 0x75, 0x89, 0x77, 0x8c, 0xb7,
0xc7, 0x9b, 0x78, 0x64, 0x5a, 0x98, 0xb4, 0x79, 0xe6, 0x75, 0x57, 0x8a,
0x84, 0x88, 0x03, 0x86, 0x7a, 0x68, 0x87, 0x76, 0x68, 0xf4, 0x55, 0xe2,
0x68, 0x9a, 0x89, 0xf4, 0x3b, 0x94, 0x75, 0x6a, 0xb8, 0xf8, 0x32, 0x68,
0x55, 0x6a, 0x78, 0x55, 0x48, 0xac, 0x53, 0x6b, 0xf6, 0x93, 0x3b, 0x78,
0x83, 0x25, 0x99, 0x92, 0x29, 0xb6, 0x66, 0x6a, 0x9a, 0x81, 0x19, 0x97,
0x92, 0x2c, 0xb9, 0xd5, 0x3f, 0x88, 0x82, 0x48, 0xa6, 0x91, 0x19, 0x98,
0x71, 0x38, 0x96, 0xa3, 0x26, 0x79, 0x81, 0x29, 0x97, 0x91, 0x1a, 0x98,
0x81, 0x19, 0x97, 0x95, 0x58, 0xbb, 0x86, 0x57, 0x98, 0x96, 0x5a, 0x9e,
0x26, 0x6c, 0x99, 0x87, 0x76, 0x4a, 0x78, 0x84, 0x6a, 0xa9, 0x67, 0x08,
0x78, 0x94, 0x88, 0x88, 0x97, 0x45, 0xa8, 0x86, 0x78, 0x89, 0xa5, 0x85,
0xba, 0x86, 0x58, 0x8a, 0x9a, 0xa8, 0x89, 0x9a, 0xa7, 0x79, 0xa9, 0xc8,
0x99, 0x8b, 0x87,
};
const size_t g_src_len__mona_lisa_21_32_c3q4_handsum = 147;
// ----
int //
main(int argc, char** argv) {
basic_handsum_decode__pixel_buffer pixbuf;
int result = basic_handsum_decode__decode(
&pixbuf, g_src_ptr__mona_lisa_21_32_c3q4_handsum,
g_src_len__mona_lisa_21_32_c3q4_handsum);
if (result != 0) {
fprintf(stderr, "basic_handsum_decode__decode failed, result code: %d\n",
result);
return 1;
} else if ((pixbuf.width < 1) || (pixbuf.width > 16) || //
(pixbuf.height < 1) || (pixbuf.height > 16)) {
fprintf(stderr,
"basic_handsum_decode__decode produced invalid dimensions\n");
return 1;
}
// Print pixbuf to stdout in the NIE image format (doc/spec/nie-spec.md).
//
// For test/data/mona-lisa.21x32.c3q4.handsum, the sha256sum of the output
// should be 9749f54780a135710a50014fa827a04128e614903c48467fe8cf3e0b4ebeff62.
//
// This can be compared to Wuffs' full-featured (not just the c3q4 that
// snippet/basic-handsum-decode.c supports) Handsum decoder implementation:
//
// clang-format off
//
// ./build-example.sh example/convert-to-nia
// ./gen/bin/example-convert-to-nia -1 test/data/mona-lisa.21x32.c3q4.handsum | sha256sum
//
// clang-format on
//
// The example/imageviewer and example/stb-imagedumper programs can also view
// NIE files directly.
uint8_t out[1040];
out[0x0] = 0x6E;
out[0x1] = 0xC3;
out[0x2] = 0xAF;
out[0x3] = 0x45;
out[0x4] = 0xFF;
out[0x5] = 0x62;
out[0x6] = 0x6E;
out[0x7] = 0x34;
out[0x8] = pixbuf.width;
out[0x9] = 0x00;
out[0xA] = 0x00;
out[0xB] = 0x00;
out[0xC] = pixbuf.height;
out[0xD] = 0x00;
out[0xE] = 0x00;
out[0xF] = 0x00;
size_t n = 4 * pixbuf.width * pixbuf.height;
memcpy(&out[16], &pixbuf.bgra_pixels[0], n);
if (write(1, out, 16 + n) < 0) {
return 1;
}
return 0;
}
+109
View File
@@ -0,0 +1,109 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//go:build ignore
// +build ignore
package main
// print-png-chunks.go prints a PNG's chunks' position and type.
//
// Usage: go run print-png-chunks.go foo.png
import (
"flag"
"fmt"
"hash/crc32"
"os"
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
return fmt.Errorf("usage: progname filename.png")
}
src, err := os.ReadFile(args[0])
if err != nil {
return err
}
pos := 0
if len(src) < 8 {
return posError(pos, len(src))
} else if len(src) > 0x7FFF_FFFF {
return fmt.Errorf("PNG file is too large")
}
pos = 8
for pos < len(src) {
if pos > (len(src) - 8) {
return posError(pos, len(src))
}
pos0 := pos
n := int64(u32be(src[pos : pos+4]))
name := clean(src[pos+4 : pos+8])
pos += 8
if int64(len(src)-pos) < n {
return posError(pos, len(src))
}
checksumHave := crc32.ChecksumIEEE(src[pos-4 : pos+int(n)])
pos += int(n)
if int64(len(src)-pos) < 4 {
return posError(pos, len(src))
}
checksumWant := u32be(src[pos : pos+4])
pos += 4
badChecksum := ""
if checksumHave != checksumWant {
badChecksum = " (bad crc32)"
}
fmt.Printf("pos: 0x%08X %10d +8, len: 0x%08X %10d +4, %s%s\n",
pos0, pos0, n, n, name, badChecksum)
}
fmt.Printf("pos: 0x%08X %10d ~~~~\n",
pos, pos)
return nil
}
func posError(pos int, lenSrc int) error {
return fmt.Errorf("bad PNG, pos: 0x%08X %10d, len(src): 0x%08X %10d",
pos, pos, lenSrc, lenSrc)
}
func clean(b []byte) string {
a := [4]byte{}
for i, c := range b[:4] {
if (c < '!') || ('~' < c) {
c = '.'
}
a[i] = c
}
return string(a[:4])
}
func u32be(b []byte) uint32 {
return (uint32(b[0]) << 24) |
(uint32(b[1]) << 16) |
(uint32(b[2]) << 8) |
(uint32(b[3]) << 0)
}
+125
View File
@@ -0,0 +1,125 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//go:build ignore
// +build ignore
package main
// print-riff-chunks.go prints a RIFF's chunks' position and type.
//
// ANI, AVI, WAV and WEBP files are all RIFF files.
//
// Usage: go run print-riff-chunks.go foo.webp
import (
"flag"
"fmt"
"os"
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
return fmt.Errorf("usage: progname filename.riff")
}
src, err := os.ReadFile(args[0])
if err != nil {
return err
}
pos := 0
if len(src) < 12 {
return posError(pos, len(src))
} else if len(src) > 0x7FFF_FFFF {
return fmt.Errorf("RIFF file is too large")
}
chunkLen := int64(u32le(src[pos+4 : pos+8]))
if int64(len(src)-8) < chunkLen {
return posError(0, len(src))
}
isWEBP = string(src[8:12]) == "WEBP"
fmt.Printf("pos: 0x%08X %10d +8, len: 0x%08X %10d, dep: %d, %s\n",
0, 0, chunkLen, chunkLen, 0, clean(src[8:12]))
pos, err = do(src, 1, 12)
if err != nil {
return err
}
fmt.Printf("pos: 0x%08X %10d ~~~~\n",
pos, pos)
return nil
}
func do(src []byte, depth int, pos int) (int, error) {
for pos < len(src) {
if pos > (len(src) - 8) {
return 0, posError(pos, len(src))
}
n := int64(u32le(src[pos+4 : pos+8]))
isWEBPANMF := isWEBP && (depth == 1) && (string(src[pos:pos+4]) == "ANMF")
fmt.Printf("pos: 0x%08X %10d +8, len: 0x%08X %10d, dep: %d, %s\n",
pos, pos, n, n, depth, clean(src[pos:pos+4]))
pos += 8
if (n & 1) != 0 {
n++
}
if int64(len(src)-pos) < n {
return 0, posError(pos, len(src))
} else if isWEBPANMF && (n > 16) {
pos1, err := do(src[:int64(pos)+n], depth+1, pos+16)
if err != nil {
return 0, err
}
pos = pos1
} else {
pos += int(n)
}
}
return pos, nil
}
var isWEBP bool
func posError(pos int, lenSrc int) error {
return fmt.Errorf("bad RIFF, pos: 0x%08X %10d, len(src): 0x%08X %10d",
pos, pos, lenSrc, lenSrc)
}
func clean(b []byte) string {
a := [4]byte{}
for i, c := range b[:4] {
if (c < '!') || ('~' < c) {
c = '.'
}
a[i] = c
}
return string(a[:4])
}
func u32le(b []byte) uint32 {
return (uint32(b[0]) << 0) |
(uint32(b[1]) << 8) |
(uint32(b[2]) << 16) |
(uint32(b[3]) << 24)
}
@@ -0,0 +1,283 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//go:build ignore
// +build ignore
package main
// replicate-libwebp-chroma-upsampling.go tests a Go port of libwebp's "fancy"
// 4:2:0 chroma upsampling algorithm. This is a 9:3:3:1 triangle filter (the
// 2-Dimensional analogue of the 1-Dimensional 3:1 triangle filter).
//
// libwebp's "fancy" upsampling is very similar to (but not exactly the same
// as) libjpeg's "fancy" upsampling. Both are 9:3:3:1 and the weights sum to
// 16, but they use a different integer arithmetic rounding algorithm:
//
// - libwebp uses truncate((weighted_average + 8) / 16), everywhere.
// - libjpeg uses truncate((weighted_average + H) / 16), where H is 8
// (rounding up) for even columns and 7 (rounding down) for odd columns.
//
// libwebp's plain C implementation is the src/dsp/upsampling.c UPSAMPLE_FUNC
// macro. While it doesn't affect rounding, the macro also computes Cb and Cr
// simultaneously using SWAR (SIMD Within A Register), packing u16x2 into a
// u32, which this Go re-implementation does not replicate. libwebp also has
// other SIMD-optimized implementations.
//
// libjpeg's plain C implementation is the jdsample.c h2v2_fancy_upsample
// function. libjpeg also has other SIMD-optimized implementations.
//
// Usage: "go run replicate-libwebp-chroma-upsampling.go" should print nothing,
// but it would print a diff if the hard-coded tests fail.
import (
"bytes"
"errors"
"fmt"
"os"
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
{
dst := make([]byte, len(dstSamplesMona))
upsampleImage(dst, 21, 32, srcSamplesMona[:], 16, 16)
compare(dst, dstSamplesMona[:], 21, "test/data/mona-lisa.21x32.q90.lossy.webp")
}
{
dst := make([]byte, len(dstSamplesPeac))
upsampleImage(dst, 24, 19, srcSamplesPeac[:], 16, 16)
compare(dst, dstSamplesPeac[:], 24, "test/data/peacock.24x19.lossy.webp")
}
if failed {
return errors.New("Failed.")
}
return nil
}
func upsampleImage(dst []byte, dstw int, dsth int, src []byte, srcw int, srch int) {
if (dstw <= 0) || (dsth <= 0) || (srcw <= 0) || (srch <= 0) {
return
}
// The dsty sequence is 0, 1, 3, 5, 7, 9, ...
for dsty := 0; dsty < dsth; {
dsty0 := dsty + 0
dsty1 := dsty + 1
srcy0 := dsty >> 1
srcy1 := srcy0 + 1
if dsty == 0 { // First row.
dsty1 = -1
srcy1 = 0
dsty = 1
} else if (dsty + 1) >= dsth { // Last row, when dsth is even.
dsty1 = -1
srcy1 = srcy0
dsty += 1
} else { // All other rows.
dsty += 2
}
dst0 := dst[(dsty0+0)*dstw : (dsty0+1)*dstw]
dst1 := []byte(nil)
if dsty1 >= 0 {
dst1 = dst[(dsty1+0)*dstw : (dsty1+1)*dstw]
}
src0 := src[(srcy0+0)*srcw : (srcy0+1)*srcw]
src1 := src[(srcy1+0)*srcw : (srcy1+1)*srcw]
upsampleStrip(dst0, dst1, src0, src1)
}
}
func upsampleStrip(dst0 []byte, dst1 []byte, src0 []byte, src1 []byte) {
if (len(dst0) <= 0) ||
((len(dst0) != len(dst1)) && (len(dst1) != 0)) ||
((len(src0) != len(src1)) && (len(src1) != 0)) {
return
}
// t=top, l=left, c=current.
tl := uint32(src0[0])
cl := uint32(src1[0])
{
dst0[0] = uint8(((3 * tl) + cl + 2) >> 2)
}
if len(dst1) > 0 {
dst1[0] = uint8(((3 * cl) + tl + 2) >> 2)
}
for x, lastPixelPair := 1, (len(dst0)-1)>>1; x <= lastPixelPair; x++ {
tc := uint32(src0[x])
cc := uint32(src1[x])
avg := tl + tc + cl + cc + 8
diag12 := (avg + (2 * (tc + cl))) >> 3
diag03 := (avg + (2 * (tl + cc))) >> 3
{
dst0[(2*x)-1] = uint8((diag12 + tl) >> 1)
dst0[(2*x)-0] = uint8((diag03 + tc) >> 1)
}
if len(dst1) > 0 {
dst1[(2*x)-1] = uint8((diag03 + cl) >> 1)
dst1[(2*x)-0] = uint8((diag12 + cc) >> 1)
}
tl = tc
cl = cc
}
if (len(dst0) & 1) == 0 {
n := len(dst0) - 1
{
dst0[n] = uint8(((3 * tl) + cl + 2) >> 2)
}
if len(dst1) > 0 {
dst1[n] = uint8(((3 * cl) + tl + 2) >> 2)
}
}
}
func compare(have []byte, want []byte, width int, name string) {
if len(have) != len(want) {
panic("invalid compare")
} else if bytes.Equal(have, want) {
return
}
failed = true
fmt.Fprintf(os.Stderr, "Wrong output for %s\n", name)
for i := range have {
fmt.Fprintf(os.Stderr, " %02X", have[i]-want[i])
if (i % width) == (width - 1) {
fmt.Fprintln(os.Stderr)
}
}
}
var failed = false
// srcSamplesMona are the Cb (Chroma blue) samples, 16x16 in four macroblocks,
// from test/data/mona-lisa.21x32.q90.lossy.webp (whose dimensions are 21x32).
var srcSamplesMona = [16 * 16]uint8{
0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6D, 0x69, 0x6B, 0x70, 0x6D, 0x72, 0x6E, 0x73, 0x73, 0x73, 0x73,
0x69, 0x69, 0x69, 0x69, 0x67, 0x6A, 0x70, 0x6D, 0x6C, 0x68, 0x6D, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A,
0x69, 0x69, 0x69, 0x69, 0x5F, 0x66, 0x7A, 0x70, 0x6B, 0x67, 0x6C, 0x69, 0x69, 0x69, 0x69, 0x69,
0x6E, 0x6E, 0x6E, 0x6E, 0x58, 0x63, 0x81, 0x72, 0x6D, 0x6A, 0x6F, 0x6B, 0x70, 0x70, 0x70, 0x70,
0x79, 0x73, 0x73, 0x79, 0x5C, 0x5B, 0x81, 0x77, 0x79, 0x72, 0x76, 0x76, 0x75, 0x75, 0x75, 0x75,
0x7C, 0x77, 0x77, 0x7C, 0x66, 0x6A, 0x85, 0x75, 0x7D, 0x75, 0x7A, 0x79, 0x79, 0x79, 0x79, 0x79,
0x79, 0x74, 0x74, 0x79, 0x69, 0x5E, 0x71, 0x7A, 0x6F, 0x6C, 0x75, 0x79, 0x77, 0x77, 0x77, 0x77,
0x72, 0x6C, 0x6C, 0x72, 0x4F, 0x51, 0x65, 0x71, 0x76, 0x6C, 0x6E, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C,
0x72, 0x6E, 0x7E, 0x6C, 0x57, 0x5C, 0x6F, 0x84, 0x87, 0x7A, 0x73, 0x77, 0x76, 0x76, 0x76, 0x76,
0x74, 0x73, 0x84, 0x7E, 0x6E, 0x7C, 0x7F, 0x74, 0x87, 0x7D, 0x78, 0x7B, 0x7D, 0x7D, 0x7D, 0x7D,
0x78, 0x77, 0x80, 0x85, 0x80, 0x81, 0x84, 0x89, 0x86, 0x83, 0x80, 0x7F, 0x7C, 0x7C, 0x7C, 0x7C,
0x7D, 0x77, 0x75, 0x7E, 0x88, 0x85, 0x85, 0x87, 0x86, 0x86, 0x85, 0x83, 0x83, 0x83, 0x83, 0x83,
0x89, 0x88, 0x75, 0x5A, 0x62, 0x7B, 0x85, 0x7A, 0x8A, 0x89, 0x87, 0x86, 0x86, 0x86, 0x86, 0x86,
0x8A, 0x88, 0x79, 0x66, 0x63, 0x6B, 0x73, 0x76, 0x87, 0x85, 0x83, 0x82, 0x80, 0x80, 0x80, 0x80,
0x8C, 0x88, 0x7F, 0x77, 0x81, 0x81, 0x86, 0x8C, 0x87, 0x85, 0x83, 0x82, 0x87, 0x87, 0x87, 0x87,
0x8D, 0x88, 0x84, 0x83, 0x83, 0x88, 0x8A, 0x88, 0x8A, 0x89, 0x87, 0x86, 0x89, 0x89, 0x89, 0x89,
}
var dstSamplesMona = [21 * 32]uint8{
0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6D, 0x6C, 0x6A, 0x6A, 0x6B, 0x6C, 0x6F, 0x6F, 0x6E, 0x6E, 0x71,
0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6C, 0x6C, 0x6C, 0x6C, 0x6B, 0x6B, 0x6B, 0x6C, 0x6E, 0x6E, 0x6D, 0x6D, 0x70,
0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x69, 0x69, 0x6A, 0x6C, 0x6D, 0x6E, 0x6D, 0x6D, 0x6D, 0x6C, 0x6A, 0x6B, 0x6D,
0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x68, 0x66, 0x66, 0x68, 0x6B, 0x70, 0x71, 0x6F, 0x6D, 0x6C, 0x6B, 0x69, 0x69, 0x6C,
0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x67, 0x63, 0x63, 0x66, 0x6B, 0x73, 0x75, 0x71, 0x6E, 0x6C, 0x6A, 0x68, 0x69, 0x6B,
0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x67, 0x61, 0x5F, 0x63, 0x6B, 0x76, 0x79, 0x73, 0x6F, 0x6D, 0x6B, 0x69, 0x69, 0x6C,
0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x68, 0x5F, 0x5C, 0x61, 0x6B, 0x78, 0x7C, 0x75, 0x70, 0x6E, 0x6C, 0x6A, 0x6B, 0x6D,
0x71, 0x70, 0x70, 0x6F, 0x6F, 0x70, 0x70, 0x6B, 0x5F, 0x5B, 0x5F, 0x69, 0x79, 0x7E, 0x77, 0x72, 0x71, 0x6F, 0x6D, 0x6D, 0x70,
0x76, 0x75, 0x73, 0x72, 0x72, 0x73, 0x75, 0x6F, 0x62, 0x5C, 0x5D, 0x66, 0x78, 0x7E, 0x79, 0x76, 0x76, 0x75, 0x72, 0x71, 0x73,
0x7A, 0x78, 0x75, 0x74, 0x74, 0x75, 0x78, 0x73, 0x65, 0x5F, 0x5F, 0x68, 0x79, 0x7F, 0x79, 0x77, 0x79, 0x78, 0x75, 0x74, 0x76,
0x7B, 0x7A, 0x77, 0x76, 0x76, 0x77, 0x7A, 0x75, 0x69, 0x64, 0x66, 0x6E, 0x7D, 0x80, 0x79, 0x77, 0x7A, 0x7A, 0x76, 0x75, 0x78,
0x7B, 0x7A, 0x78, 0x76, 0x76, 0x78, 0x7A, 0x76, 0x6C, 0x67, 0x67, 0x6D, 0x7A, 0x7E, 0x79, 0x77, 0x79, 0x78, 0x74, 0x74, 0x77,
0x7A, 0x79, 0x76, 0x75, 0x75, 0x76, 0x79, 0x75, 0x6D, 0x66, 0x63, 0x66, 0x71, 0x77, 0x78, 0x77, 0x74, 0x71, 0x6F, 0x70, 0x74,
0x77, 0x76, 0x73, 0x72, 0x72, 0x73, 0x76, 0x72, 0x68, 0x61, 0x5D, 0x60, 0x69, 0x70, 0x75, 0x76, 0x73, 0x70, 0x6D, 0x6E, 0x71,
0x74, 0x72, 0x6F, 0x6E, 0x6E, 0x6F, 0x72, 0x6C, 0x5D, 0x55, 0x55, 0x59, 0x63, 0x6B, 0x70, 0x74, 0x74, 0x72, 0x6E, 0x6D, 0x6F,
0x72, 0x71, 0x6E, 0x6E, 0x70, 0x71, 0x71, 0x69, 0x59, 0x52, 0x53, 0x59, 0x63, 0x6B, 0x72, 0x77, 0x79, 0x78, 0x72, 0x6F, 0x6F,
0x72, 0x71, 0x6F, 0x71, 0x77, 0x77, 0x71, 0x67, 0x5B, 0x56, 0x58, 0x5E, 0x68, 0x71, 0x7B, 0x80, 0x82, 0x80, 0x7A, 0x75, 0x73,
0x73, 0x72, 0x70, 0x73, 0x7B, 0x7C, 0x74, 0x6C, 0x62, 0x5F, 0x62, 0x68, 0x6F, 0x76, 0x7D, 0x82, 0x85, 0x84, 0x7E, 0x79, 0x76,
0x74, 0x73, 0x72, 0x76, 0x7E, 0x80, 0x7C, 0x75, 0x6D, 0x6B, 0x71, 0x76, 0x79, 0x7A, 0x79, 0x7C, 0x83, 0x84, 0x7F, 0x7B, 0x78,
0x75, 0x75, 0x74, 0x78, 0x7F, 0x82, 0x81, 0x7C, 0x76, 0x75, 0x7B, 0x7E, 0x80, 0x7F, 0x7B, 0x7D, 0x83, 0x85, 0x81, 0x7D, 0x7B,
0x77, 0x77, 0x76, 0x79, 0x7E, 0x82, 0x83, 0x81, 0x7D, 0x7D, 0x7F, 0x81, 0x82, 0x83, 0x84, 0x84, 0x86, 0x85, 0x83, 0x81, 0x7F,
0x79, 0x79, 0x78, 0x79, 0x7C, 0x7F, 0x82, 0x83, 0x82, 0x82, 0x82, 0x83, 0x84, 0x85, 0x87, 0x88, 0x87, 0x85, 0x84, 0x83, 0x82,
0x7C, 0x7B, 0x78, 0x77, 0x78, 0x7A, 0x7E, 0x81, 0x84, 0x86, 0x85, 0x84, 0x85, 0x85, 0x87, 0x87, 0x86, 0x86, 0x85, 0x85, 0x84,
0x80, 0x7F, 0x7C, 0x7A, 0x77, 0x75, 0x75, 0x77, 0x7C, 0x80, 0x82, 0x83, 0x84, 0x85, 0x84, 0x85, 0x86, 0x87, 0x87, 0x86, 0x86,
0x86, 0x85, 0x84, 0x80, 0x79, 0x71, 0x68, 0x65, 0x69, 0x70, 0x79, 0x7F, 0x83, 0x83, 0x7F, 0x80, 0x86, 0x89, 0x88, 0x88, 0x87,
0x89, 0x89, 0x88, 0x84, 0x7B, 0x70, 0x63, 0x5E, 0x61, 0x67, 0x72, 0x79, 0x7E, 0x7F, 0x7B, 0x7D, 0x85, 0x89, 0x88, 0x88, 0x87,
0x8A, 0x89, 0x88, 0x84, 0x7C, 0x73, 0x68, 0x63, 0x63, 0x66, 0x6C, 0x71, 0x75, 0x77, 0x77, 0x7B, 0x84, 0x87, 0x86, 0x86, 0x85,
0x8B, 0x8A, 0x89, 0x85, 0x7E, 0x76, 0x6E, 0x6A, 0x6A, 0x6C, 0x6F, 0x72, 0x76, 0x79, 0x7B, 0x7E, 0x84, 0x87, 0x86, 0x85, 0x84,
0x8C, 0x8B, 0x89, 0x85, 0x80, 0x7B, 0x75, 0x74, 0x78, 0x7A, 0x7B, 0x7D, 0x80, 0x83, 0x85, 0x87, 0x87, 0x87, 0x86, 0x85, 0x84,
0x8C, 0x8B, 0x89, 0x86, 0x82, 0x7F, 0x7C, 0x7C, 0x80, 0x82, 0x82, 0x84, 0x86, 0x88, 0x8A, 0x8A, 0x89, 0x87, 0x86, 0x86, 0x85,
0x8D, 0x8C, 0x89, 0x87, 0x84, 0x82, 0x81, 0x81, 0x82, 0x83, 0x85, 0x87, 0x88, 0x89, 0x89, 0x89, 0x89, 0x89, 0x88, 0x88, 0x87,
0x8D, 0x8C, 0x89, 0x87, 0x85, 0x84, 0x83, 0x83, 0x83, 0x84, 0x87, 0x89, 0x8A, 0x8A, 0x89, 0x89, 0x8A, 0x8A, 0x89, 0x89, 0x88,
}
// srcSamplesPeac are the Cb (Chroma blue) samples, 16x16 in four macroblocks,
// from test/data/peacock.24x19.lossy.webp (whose dimensions are 24x19).
var srcSamplesPeac = [16 * 16]uint8{
0x65, 0x65, 0x65, 0x63, 0x63, 0x6C, 0x72, 0x71, 0x66, 0x69, 0x73, 0x76, 0x6E, 0x6E, 0x6E, 0x6E,
0x65, 0x65, 0x65, 0x65, 0x5F, 0x6B, 0x72, 0x6E, 0x69, 0x70, 0x6D, 0x73, 0x6E, 0x6E, 0x6E, 0x6E,
0x67, 0x67, 0x67, 0x67, 0x6B, 0x96, 0x9C, 0x7B, 0x6D, 0x79, 0x63, 0x6F, 0x6E, 0x6E, 0x6E, 0x6E,
0x68, 0x68, 0x68, 0x68, 0x80, 0x7E, 0x84, 0x8F, 0x72, 0x80, 0x5C, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D,
0x6A, 0x6A, 0x6A, 0x6A, 0x63, 0x6B, 0x7F, 0x95, 0x75, 0x74, 0x6F, 0x6A, 0x69, 0x69, 0x69, 0x69,
0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x99, 0x85, 0x74, 0x70, 0x6B, 0x67, 0x65, 0x65, 0x65, 0x65,
0x6C, 0x6C, 0x6C, 0x6C, 0x6D, 0x81, 0xA8, 0x76, 0x6F, 0x6B, 0x66, 0x62, 0x60, 0x60, 0x60, 0x60,
0x6C, 0x6C, 0x6C, 0x6C, 0x68, 0xA0, 0xA4, 0x72, 0x6B, 0x67, 0x62, 0x5F, 0x5C, 0x5C, 0x5C, 0x5C,
0x6B, 0x6B, 0x6B, 0x6B, 0x70, 0xA5, 0xA3, 0x89, 0x64, 0x68, 0x67, 0x60, 0x60, 0x60, 0x60, 0x60,
0x6B, 0x6B, 0x6B, 0x6B, 0x7E, 0xAD, 0xAB, 0x97, 0x64, 0x68, 0x67, 0x60, 0x60, 0x60, 0x60, 0x60,
0x6A, 0x6A, 0x6A, 0x6A, 0x87, 0xAD, 0xAB, 0xA0, 0x64, 0x68, 0x67, 0x60, 0x60, 0x60, 0x60, 0x60,
0x69, 0x69, 0x69, 0x69, 0x85, 0xA5, 0xA3, 0x9E, 0x64, 0x68, 0x67, 0x60, 0x60, 0x60, 0x60, 0x60,
0x69, 0x69, 0x69, 0x69, 0x7C, 0xAA, 0xAC, 0xA0, 0x64, 0x68, 0x67, 0x60, 0x60, 0x60, 0x60, 0x60,
0x68, 0x68, 0x68, 0x68, 0x7C, 0xAA, 0xAC, 0xA0, 0x64, 0x68, 0x67, 0x60, 0x60, 0x60, 0x60, 0x60,
0x68, 0x68, 0x68, 0x68, 0x7C, 0xAA, 0xAC, 0xA0, 0x64, 0x68, 0x67, 0x60, 0x60, 0x60, 0x60, 0x60,
0x68, 0x68, 0x68, 0x68, 0x7C, 0xAA, 0xAC, 0xA0, 0x64, 0x68, 0x67, 0x60, 0x60, 0x60, 0x60, 0x60,
}
var dstSamplesPeac = [24 * 19]uint8{
0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x64, 0x63, 0x63, 0x65, 0x6A, 0x6E, 0x71, 0x72, 0x71, 0x6E, 0x69, 0x67, 0x68, 0x6C, 0x71, 0x74, 0x75, 0x76,
0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x64, 0x63, 0x62, 0x64, 0x69, 0x6D, 0x70, 0x72, 0x71, 0x6E, 0x69, 0x68, 0x6A, 0x6C, 0x70, 0x72, 0x74, 0x75,
0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x63, 0x61, 0x63, 0x68, 0x6D, 0x70, 0x71, 0x70, 0x6D, 0x6A, 0x6A, 0x6D, 0x6E, 0x6E, 0x70, 0x72, 0x74,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x65, 0x63, 0x67, 0x71, 0x77, 0x7B, 0x7A, 0x74, 0x6F, 0x6C, 0x6C, 0x70, 0x70, 0x6C, 0x6C, 0x70, 0x72,
0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x68, 0x71, 0x82, 0x8D, 0x90, 0x8B, 0x7E, 0x75, 0x6F, 0x6F, 0x74, 0x72, 0x6A, 0x68, 0x6D, 0x70,
0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x6A, 0x6E, 0x78, 0x88, 0x92, 0x95, 0x91, 0x86, 0x7C, 0x73, 0x71, 0x78, 0x74, 0x68, 0x65, 0x6B, 0x6F,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x6D, 0x76, 0x7D, 0x82, 0x86, 0x89, 0x8A, 0x8A, 0x84, 0x77, 0x74, 0x7B, 0x76, 0x66, 0x62, 0x6A, 0x6E,
0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x6D, 0x75, 0x79, 0x79, 0x7C, 0x80, 0x86, 0x8D, 0x89, 0x7A, 0x75, 0x7A, 0x76, 0x68, 0x64, 0x69, 0x6C,
0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6C, 0x6E, 0x74, 0x7C, 0x85, 0x8F, 0x8C, 0x7C, 0x75, 0x76, 0x74, 0x6D, 0x6A, 0x6B, 0x6B,
0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x69, 0x66, 0x67, 0x6A, 0x72, 0x7F, 0x88, 0x8E, 0x8A, 0x7C, 0x74, 0x73, 0x72, 0x6F, 0x6D, 0x6A, 0x69,
0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6A, 0x69, 0x6A, 0x6B, 0x75, 0x89, 0x90, 0x8B, 0x84, 0x79, 0x73, 0x72, 0x70, 0x6D, 0x6B, 0x69, 0x68,
0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6D, 0x6F, 0x7C, 0x92, 0x96, 0x88, 0x7E, 0x76, 0x72, 0x70, 0x6E, 0x6B, 0x69, 0x67, 0x66,
0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x70, 0x78, 0x86, 0x9A, 0x9A, 0x84, 0x77, 0x73, 0x6F, 0x6D, 0x6B, 0x69, 0x66, 0x64, 0x63,
0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x73, 0x82, 0x90, 0x9F, 0x9B, 0x82, 0x73, 0x70, 0x6D, 0x6B, 0x69, 0x66, 0x64, 0x62, 0x61,
0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6B, 0x6A, 0x75, 0x8D, 0x9B, 0xA2, 0x99, 0x80, 0x71, 0x6E, 0x6B, 0x69, 0x67, 0x64, 0x62, 0x61, 0x60,
0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6B, 0x6A, 0x78, 0x93, 0xA2, 0xA3, 0x99, 0x83, 0x74, 0x6D, 0x69, 0x68, 0x66, 0x64, 0x62, 0x60, 0x5F,
0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6C, 0x6D, 0x7B, 0x96, 0xA4, 0xA3, 0x9B, 0x8B, 0x7C, 0x6D, 0x66, 0x67, 0x67, 0x66, 0x64, 0x61, 0x60,
0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6D, 0x71, 0x80, 0x9A, 0xA7, 0xA6, 0x9F, 0x93, 0x82, 0x6E, 0x65, 0x67, 0x68, 0x67, 0x65, 0x62, 0x60,
0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6F, 0x77, 0x87, 0x9F, 0xAB, 0xAA, 0xA4, 0x99, 0x88, 0x70, 0x65, 0x67, 0x68, 0x67, 0x65, 0x62, 0x60,
}
+486
View File
@@ -0,0 +1,486 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// ----------------
// This is a small, self-contained, single-file C library to decode basic
// Handsum image files. Basic means that it only supports Handsum's default
// configuration, c3q4: color=3 (RGB, no Alpha) and quality=4 (best).
//
// It is a C port of some (decoding only and c3q4 only) of this Go library:
// https://pkg.go.dev/github.com/google/wuffs/lib/handsum
//
// To decode other configurations, like c1q1 (Gray, worst quality) or c4q2
// (RGBA, medium-low quality), use the github.com/google/wuffs Go or
// C-transpiled-from-memory-safe-Wuffs implementations.
//
// To use this file as a "foo.c"-like implementation, instead of a "foo.h"-like
// header, #define BASIC_HANDSUM_DECODE_IMPLEMENTATION before #include'ing or
// compiling it.
//
// As an option, you may also #define
// BASIC_HANDSUM_DECODE_CONFIG__STATIC_FUNCTIONS to make these functions have
// static storage. This can help the compiler ignore or discard unused code,
// which can produce faster compiles and smaller binaries.
#ifndef BASIC_HANDSUM_DECODE_INCLUDE_GUARD
#define BASIC_HANDSUM_DECODE_INCLUDE_GUARD
#if defined(BASIC_HANDSUM_DECODE_CONFIG__STATIC_FUNCTIONS)
#define BASIC_HANDSUM_DECODE__MAYBE_STATIC static
#else
#define BASIC_HANDSUM_DECODE__MAYBE_STATIC
#endif
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef struct basic_handsum_decode__pixel_buffer_type {
uint32_t width; // Measured in pixels, not bytes.
uint32_t height; // Measured in pixels, not bytes.
uint8_t bgra_pixels[1024];
} basic_handsum_decode__pixel_buffer;
// BASIC_HANDSUM_DECODE__RESULT__ETC can be returned by
// basic_handsum_decode__decode. Zero means OK. Non-zero means an error.
#define BASIC_HANDSUM_DECODE__RESULT__OK 0
#define BASIC_HANDSUM_DECODE__RESULT__DST_IS_NULL 1
#define BASIC_HANDSUM_DECODE__RESULT__SRC_IS_TOO_SHORT 2
#define BASIC_HANDSUM_DECODE__RESULT__SRC_IS_NOT_HANDSUM_C3Q4 3
// Decodes the source bytes into the destination pixel buffer, producing 4
// bytes per pixel (in B, G, R, A order). The bgra_pixels' row stride will be
// (4 * width) bytes.
//
// It only supports c3q4 Handsum files: color=3 (so alpha is always 0xFF) and
// quality=4 (best). Every valid c3q4 Handsum file is exactly 147 bytes long.
//
// On success, it returns zero and dst's width and height will range between 1
// and 16, inclusive.
//
// On failure, it returns non-zero and, if dst is non-NULL, its width and
// height will be set to zero.
//
// This function is thread-safe.
BASIC_HANDSUM_DECODE__MAYBE_STATIC int //
basic_handsum_decode__decode(basic_handsum_decode__pixel_buffer* dst,
const uint8_t* src_ptr,
const size_t src_len);
// --------
#ifdef BASIC_HANDSUM_DECODE_IMPLEMENTATION
#ifdef __wasm__
static void //
basic_handsum_decode__memcpy(uint8_t* restrict dst,
const uint8_t* restrict src,
size_t n) {
while (n--) {
*dst++ = *src++;
}
}
#else
#include <string.h>
#define basic_handsum_decode__memcpy memcpy
#endif
static void //
basic_handsum_decode__smooth_block_seams_16x16(uint8_t* b) {
static const uint8_t smoothing_pairs_16x16[28][2] = {
{0x07, 0x08}, {0x17, 0x18}, {0x27, 0x28}, {0x37, 0x38},
{0x47, 0x48}, {0x57, 0x58}, {0x67, 0x68},
{0x70, 0x80}, {0x71, 0x81}, {0x72, 0x82}, {0x73, 0x83},
{0x74, 0x84}, {0x75, 0x85}, {0x76, 0x86},
{0x79, 0x89}, {0x7A, 0x8A}, {0x7B, 0x8B}, {0x7C, 0x8C},
{0x7D, 0x8D}, {0x7E, 0x8E}, {0x7F, 0x8F},
{0x97, 0x98}, {0xA7, 0xA8}, {0xB7, 0xB8}, {0xC7, 0xC8},
{0xD7, 0xD8}, {0xE7, 0xE8}, {0xF7, 0xF8},
};
for (int i = 0; i < 28; i++) {
uint8_t p0 = smoothing_pairs_16x16[i][0];
uint8_t p1 = smoothing_pairs_16x16[i][1];
uint32_t v0 = b[p0];
uint32_t v1 = b[p1];
b[p0] = ((3 * v0) + v1 + 2) / 4;
b[p1] = ((3 * v1) + v0 + 2) / 4;
}
uint32_t v77 = b[0x77];
uint32_t v78 = b[0x78];
uint32_t v88 = b[0x88];
uint32_t v87 = b[0x87];
b[0x77] = ((9 * v77) + (3 * v78) + v88 + (3 * v87) + 8) / 16;
b[0x78] = ((9 * v78) + (3 * v88) + v87 + (3 * v77) + 8) / 16;
b[0x88] = ((9 * v88) + (3 * v87) + v77 + (3 * v78) + 8) / 16;
b[0x87] = ((9 * v87) + (3 * v77) + v78 + (3 * v88) + 8) / 16;
}
static void //
basic_handsum_decode__smooth_block_seams_8x8_q4(uint8_t* b) {
for (int y = 1; y < 7; y += 2) {
for (int x = 1; x < 7; x += 2) {
int o = (y * 8) + x;
uint32_t v0 = b[o + 0];
uint32_t v1 = b[o + 1];
uint32_t v9 = b[o + 9];
uint32_t v8 = b[o + 8];
b[o + 0] = ((9 * v0) + (3 * v1) + v9 + (3 * v8) + 8) / 16;
b[o + 1] = ((9 * v1) + (3 * v9) + v8 + (3 * v0) + 8) / 16;
b[o + 9] = ((9 * v9) + (3 * v8) + v0 + (3 * v1) + 8) / 16;
b[o + 8] = ((9 * v8) + (3 * v0) + v1 + (3 * v9) + 8) / 16;
}
{
uint32_t v0 = b[000 + y];
uint32_t v1 = b[001 + y];
b[000 + y] = ((3 * v0) + v1 + 2) / 4;
b[001 + y] = ((3 * v1) + v0 + 2) / 4;
}
{
uint32_t v0 = b[070 + y];
uint32_t v1 = b[071 + y];
b[070 + y] = ((3 * v0) + v1 + 2) / 4;
b[071 + y] = ((3 * v1) + v0 + 2) / 4;
}
int y8 = y * 8;
{
uint32_t v0 = b[000 + y8];
uint32_t v1 = b[010 + y8];
b[000 + y8] = ((3 * v0) + v1 + 2) / 4;
b[010 + y8] = ((3 * v1) + v0 + 2) / 4;
}
{
uint32_t v0 = b[007 + y8];
uint32_t v1 = b[017 + y8];
b[007 + y8] = ((3 * v0) + v1 + 2) / 4;
b[017 + y8] = ((3 * v1) + v0 + 2) / 4;
}
}
}
// Return (x + 0x80), if in range, otherwise clamped to 0x00 or 0xFF.
static uint8_t //
basic_handsum_decode__bias_and_clamp(int x) {
x += 0x80;
return ((x >> 8) == 0) ? x : ~(x >> 31);
}
static int //
basic_handsum_decode__decode_block_q4(uint8_t* dst_ptr,
size_t dst_stride,
const uint8_t* src_ptr,
size_t bit_offset) {
uint8_t tmp[64];
for (int i = 0; i < 16; i++) {
int nibble0 = (src_ptr[bit_offset >> 3] >> (bit_offset & 4)) & 15;
int dct0 = (nibble0 * 0x22) - 256;
bit_offset += 4;
int nibble1 = (src_ptr[bit_offset >> 3] >> (bit_offset & 4)) & 15;
int dct1 = (nibble1 * 0x10) - 128;
bit_offset += 4;
int nibble2 = (src_ptr[bit_offset >> 3] >> (bit_offset & 4)) & 15;
int dct2 = (nibble2 * 0x10) - 128;
bit_offset += 4;
int x2 = 2 * (i & 3);
int y2 = 2 * (i >> 2);
int o2 = (y2 * 8) + x2;
tmp[o2 + 0] =
basic_handsum_decode__bias_and_clamp((+dct0 + dct1 + dct2 + 1) >> 1);
tmp[o2 + 1] =
basic_handsum_decode__bias_and_clamp((+dct0 - dct1 + dct2 + 1) >> 1);
tmp[o2 + 8] =
basic_handsum_decode__bias_and_clamp((+dct0 + dct1 - dct2 + 1) >> 1);
tmp[o2 + 9] =
basic_handsum_decode__bias_and_clamp((+dct0 - dct1 - dct2 + 1) >> 1);
}
basic_handsum_decode__smooth_block_seams_8x8_q4(&tmp[0]);
for (size_t i = 0; i < 8; i++) {
size_t di = i * dst_stride;
size_t ti = i * 8;
basic_handsum_decode__memcpy(&dst_ptr[di], &tmp[ti], 8);
}
return bit_offset;
}
static void //
basic_handsum_decode__scale_and_bias_chroma_down(uint8_t* b) {
for (int i = 0; i < 64; i++) {
b[i] = (b[i] >> 1) + 0x3C;
}
}
static void //
basic_handsum_decode__upsample_from(uint8_t* restrict dst,
const uint8_t* restrict src) {
// dst is 16x16, src is 8x8.
for (int y = 0; y < 16; y++) {
// sy is: 0, +8, -8, +8, -8, ..., +8, -8, 0.
int sy = ((y == 0) || (y == 15)) ? 0 : ((y & 1) * 16) - 8;
for (int x = 0; x < 16; x++) {
// sx is: 0, +1, -1, +1, -1, ..., +1, -1, 0.
int sx = ((x == 0) || (x == 15)) ? 0 : ((x & 1) * 2) - 1;
int s0 = ((y >> 1) * 8) + (x >> 1);
int v00 = src[s0];
int v01 = src[s0 + sx];
int v10 = src[s0 + sy];
int v11 = src[s0 + sx + sy];
int d0 = (y * 16) + x;
dst[d0] = ((9 * v00) + (3 * v01) + (3 * v10) + v11 + 8) / 16;
}
}
}
static void //
basic_handsum_decode__scale_horizontal(
basic_handsum_decode__pixel_buffer* pixbuf) {
uint8_t dst[1024];
uint32_t w = pixbuf->width;
const uint8_t* src = pixbuf->bgra_pixels;
for (int y = 0; y < 16; y++) {
int dstx = 0;
uint32_t acc0 = 0;
uint32_t acc1 = 0;
uint32_t acc2 = 0;
uint32_t acc3 = 0;
uint32_t remainder = 16;
for (int srcx = 0; srcx < 16; srcx++) {
int si = (64 * y) + (4 * srcx);
uint32_t s0 = src[si + 0];
uint32_t s1 = src[si + 1];
uint32_t s2 = src[si + 2];
uint32_t s3 = src[si + 3];
if (remainder > w) {
remainder -= w;
acc0 += w * s0;
acc1 += w * s1;
acc2 += w * s2;
acc3 += w * s3;
} else {
acc0 += remainder * s0;
acc1 += remainder * s1;
acc2 += remainder * s2;
acc3 += remainder * s3;
int di = (4 * w * y) + (4 * dstx);
dst[di + 0] = (acc0 + 8) / 16;
dst[di + 1] = (acc1 + 8) / 16;
dst[di + 2] = (acc2 + 8) / 16;
dst[di + 3] = (acc3 + 8) / 16;
dstx++;
uint32_t partial = w - remainder;
acc0 = partial * s0;
acc1 = partial * s1;
acc2 = partial * s2;
acc3 = partial * s3;
remainder = 16 - partial;
}
}
}
for (int y = 0; y < 16; y++) {
basic_handsum_decode__memcpy(&pixbuf->bgra_pixels[4 * w * y],
&dst[4 * w * y], 4 * w);
}
}
static void //
basic_handsum_decode__scale_vertical(
basic_handsum_decode__pixel_buffer* pixbuf) {
uint8_t dst[1024];
uint32_t h = pixbuf->height;
const uint8_t* src = pixbuf->bgra_pixels;
for (int x = 0; x < 16; x++) {
int dsty = 0;
uint32_t acc0 = 0;
uint32_t acc1 = 0;
uint32_t acc2 = 0;
uint32_t acc3 = 0;
uint32_t remainder = 16;
for (int srcy = 0; srcy < 16; srcy++) {
int si = (64 * srcy) + (4 * x);
uint32_t s0 = src[si + 0];
uint32_t s1 = src[si + 1];
uint32_t s2 = src[si + 2];
uint32_t s3 = src[si + 3];
if (remainder > h) {
remainder -= h;
acc0 += h * s0;
acc1 += h * s1;
acc2 += h * s2;
acc3 += h * s3;
} else {
acc0 += remainder * s0;
acc1 += remainder * s1;
acc2 += remainder * s2;
acc3 += remainder * s3;
int di = (64 * dsty) + (4 * x);
dst[di + 0] = (acc0 + 8) / 16;
dst[di + 1] = (acc1 + 8) / 16;
dst[di + 2] = (acc2 + 8) / 16;
dst[di + 3] = (acc3 + 8) / 16;
dsty++;
uint32_t partial = h - remainder;
acc0 = partial * s0;
acc1 = partial * s1;
acc2 = partial * s2;
acc3 = partial * s3;
remainder = 16 - partial;
}
}
}
basic_handsum_decode__memcpy(&pixbuf->bgra_pixels[0], &dst[0], 64 * h);
}
static void //
basic_handsum_decode__convert_ycbcr_bgra(
basic_handsum_decode__pixel_buffer* dst,
const uint8_t* restrict yy,
const uint8_t* restrict cb,
const uint8_t* restrict cr) {
for (int i = 0; i < 256; i++) {
int32_t yy1 = (int32_t)(yy[i]) * 0x10101;
int32_t cb1 = (int32_t)(cb[i]) - 128;
int32_t cr1 = (int32_t)(cr[i]) - 128;
int32_t r = yy1 + (91881 * cr1);
if (((uint32_t)(r)&0xFF000000u) == 0) {
r >>= 16;
} else {
r = -1 ^ (r >> 31);
}
int32_t g = yy1 - (22554 * cb1) - (46802 * cr1);
if (((uint32_t)(g)&0xFF000000u) == 0) {
g >>= 16;
} else {
g = -1 ^ (g >> 31);
}
int32_t b = yy1 + (116130 * cb1);
if (((uint32_t)(b)&0xFF000000u) == 0) {
b >>= 16;
} else {
b = -1 ^ (b >> 31);
}
dst->bgra_pixels[(4 * i) + 0] = b;
dst->bgra_pixels[(4 * i) + 1] = g;
dst->bgra_pixels[(4 * i) + 2] = r;
dst->bgra_pixels[(4 * i) + 3] = 0xFF;
}
}
BASIC_HANDSUM_DECODE__MAYBE_STATIC int //
basic_handsum_decode__decode(basic_handsum_decode__pixel_buffer* dst,
const uint8_t* src_ptr,
const size_t src_len) {
if (dst == NULL) {
return BASIC_HANDSUM_DECODE__RESULT__DST_IS_NULL;
}
dst->width = 0;
dst->height = 0;
if (src_len < 147) {
return BASIC_HANDSUM_DECODE__RESULT__SRC_IS_TOO_SHORT;
} else if ((src_ptr[0] != 0xFE) || //
(src_ptr[1] != 0xD7) || //
((src_ptr[2] & 0xE0) != 0x60) || //
((src_ptr[2] & 0x1F) == 0x1F)) {
return BASIC_HANDSUM_DECODE__RESULT__SRC_IS_NOT_HANDSUM_C3Q4;
}
if ((src_ptr[2] & 0x10) == 0x00) { // Landscape.
dst->width = 16;
dst->height = 1 + (src_ptr[2] & 0x0F);
} else { // Portrait.
dst->width = 1 + (src_ptr[2] & 0x0F);
dst->height = 16;
}
uint8_t luma_quad_block[256];
uint8_t tmp[64];
uint8_t cb_quad_block[256];
uint8_t cr_quad_block[256];
size_t bit_offset = 24;
bit_offset = basic_handsum_decode__decode_block_q4( //
&luma_quad_block[0x00], 16, src_ptr, bit_offset);
bit_offset = basic_handsum_decode__decode_block_q4( //
&luma_quad_block[0x08], 16, src_ptr, bit_offset);
bit_offset = basic_handsum_decode__decode_block_q4( //
&luma_quad_block[0x80], 16, src_ptr, bit_offset);
bit_offset = basic_handsum_decode__decode_block_q4( //
&luma_quad_block[0x88], 16, src_ptr, bit_offset);
basic_handsum_decode__smooth_block_seams_16x16(&luma_quad_block[0]);
bit_offset = basic_handsum_decode__decode_block_q4( //
&tmp[0], 8, src_ptr, bit_offset);
basic_handsum_decode__scale_and_bias_chroma_down(&tmp[0]);
basic_handsum_decode__upsample_from(&cb_quad_block[0], &tmp[0]);
bit_offset = basic_handsum_decode__decode_block_q4( //
&tmp[0], 8, src_ptr, bit_offset);
basic_handsum_decode__scale_and_bias_chroma_down(&tmp[0]);
basic_handsum_decode__upsample_from(&cr_quad_block[0], &tmp[0]);
basic_handsum_decode__convert_ycbcr_bgra(
dst, &luma_quad_block[0], &cb_quad_block[0], &cr_quad_block[0]);
if (dst->width < 16) {
basic_handsum_decode__scale_horizontal(dst);
} else if (dst->height < 16) {
basic_handsum_decode__scale_vertical(dst);
}
return BASIC_HANDSUM_DECODE__RESULT__OK;
}
#endif // BASIC_HANDSUM_DECODE_IMPLEMENTATION
#endif // BASIC_HANDSUM_DECODE_INCLUDE_GUARD
+8 -8
View File
@@ -94,12 +94,12 @@ uncompng__encode(int (*write_func)(void* context,
const uint8_t* data_ptr,
size_t data_len),
void* context,
const uint8_t* pixel_ptr,
size_t pixel_len,
uint32_t pixel_format,
uint32_t width,
uint32_t height,
size_t stride,
uint32_t pixel_format);
const uint8_t* pixel_ptr,
size_t pixel_len,
size_t stride);
// --------
@@ -581,12 +581,12 @@ uncompng__encode(int (*write_func)(void* context,
const uint8_t* data_ptr,
size_t data_len),
void* context,
const uint8_t* pixel_ptr,
size_t pixel_len,
uint32_t pixel_format,
uint32_t width,
uint32_t height,
size_t stride,
uint32_t pixel_format) {
const uint8_t* pixel_ptr,
size_t pixel_len,
size_t stride) {
if (!write_func) {
return UNCOMPNG__RESULT__INVALID_ARGUMENT;
}
+4 -26
View File
@@ -771,8 +771,8 @@ pri func decoder.smooth_seams_8x8!(which: base.u32[..= 1], y_offset: base.u32[..
this.buffers[args.which][args.y_offset + 4][args.x_offset + 12] = w43 as base.u8
}
// scale_and_bias_chroma_down applies a look-up table transform to the 8×8
// Chroma samples in this.buffers[1].
// scale_and_bias_chroma_down transforms the 8×8 Chroma samples in
// this.buffers[1].
pri func decoder.scale_and_bias_chroma_down!() {
var y : base.u32
var x : base.u32
@@ -783,10 +783,8 @@ pri func decoder.scale_and_bias_chroma_down!() {
while x < 8,
inv y < 8,
{
this.buffers[1][y][(4 * x) + 1] = SCALE_AND_BIAS_CHROMA_DOWN[
this.buffers[1][y][(4 * x) + 1]]
this.buffers[1][y][(4 * x) + 2] = SCALE_AND_BIAS_CHROMA_DOWN[
this.buffers[1][y][(4 * x) + 2]]
this.buffers[1][y][(4 * x) + 1] = 0x3C + (this.buffers[1][y][(4 * x) + 1] >> 1)
this.buffers[1][y][(4 * x) + 2] = 0x3C + (this.buffers[1][y][(4 * x) + 2] >> 1)
x += 1
}
@@ -1199,26 +1197,6 @@ pri const ZIGZAG : roarray[2] roarray[15] base.u8[..= 24] = [[
0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 0, 0, 0, 0, 0,
]]
pri const SCALE_AND_BIAS_CHROMA_DOWN : roarray[256] base.u8 = [
0x3C, 0x3C, 0x3D, 0x3D, 0x3E, 0x3E, 0x3F, 0x3F, 0x40, 0x40, 0x41, 0x41, 0x42, 0x42, 0x43, 0x43,
0x44, 0x44, 0x45, 0x45, 0x46, 0x46, 0x47, 0x47, 0x48, 0x48, 0x49, 0x49, 0x4A, 0x4A, 0x4B, 0x4B,
0x4C, 0x4C, 0x4D, 0x4D, 0x4E, 0x4E, 0x4F, 0x4F, 0x50, 0x50, 0x51, 0x51, 0x52, 0x52, 0x53, 0x53,
0x54, 0x54, 0x55, 0x55, 0x56, 0x56, 0x57, 0x57, 0x58, 0x58, 0x59, 0x59, 0x5A, 0x5A, 0x5B, 0x5B,
0x5C, 0x5C, 0x5D, 0x5D, 0x5E, 0x5E, 0x5F, 0x5F, 0x60, 0x60, 0x61, 0x61, 0x62, 0x62, 0x63, 0x63,
0x64, 0x64, 0x65, 0x65, 0x66, 0x66, 0x67, 0x67, 0x68, 0x68, 0x69, 0x69, 0x6A, 0x6A, 0x6B, 0x6B,
0x6C, 0x6C, 0x6D, 0x6D, 0x6E, 0x6E, 0x6F, 0x6F, 0x70, 0x70, 0x71, 0x71, 0x72, 0x72, 0x73, 0x73,
0x74, 0x74, 0x75, 0x75, 0x76, 0x76, 0x77, 0x77, 0x78, 0x78, 0x79, 0x79, 0x7A, 0x7A, 0x7B, 0x7B,
0x7C, 0x7C, 0x7D, 0x7D, 0x7E, 0x7E, 0x7F, 0x7F, 0x80, 0x80, 0x81, 0x81, 0x82, 0x82, 0x83, 0x83,
0x84, 0x84, 0x85, 0x85, 0x86, 0x86, 0x87, 0x87, 0x88, 0x88, 0x89, 0x89, 0x8A, 0x8A, 0x8B, 0x8B,
0x8C, 0x8C, 0x8D, 0x8D, 0x8E, 0x8E, 0x8F, 0x8F, 0x90, 0x90, 0x91, 0x91, 0x92, 0x92, 0x93, 0x93,
0x94, 0x94, 0x95, 0x95, 0x96, 0x96, 0x97, 0x97, 0x98, 0x98, 0x99, 0x99, 0x9A, 0x9A, 0x9B, 0x9B,
0x9C, 0x9C, 0x9D, 0x9D, 0x9E, 0x9E, 0x9F, 0x9F, 0xA0, 0xA0, 0xA1, 0xA1, 0xA2, 0xA2, 0xA3, 0xA3,
0xA4, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0xA7, 0xA7, 0xA8, 0xA8, 0xA9, 0xA9, 0xAA, 0xAA, 0xAB, 0xAB,
0xAC, 0xAC, 0xAD, 0xAD, 0xAE, 0xAE, 0xAF, 0xAF, 0xB0, 0xB0, 0xB1, 0xB1, 0xB2, 0xB2, 0xB3, 0xB3,
0xB4, 0xB4, 0xB5, 0xB5, 0xB6, 0xB6, 0xB7, 0xB7, 0xB8, 0xB8, 0xB9, 0xB9, 0xBA, 0xBA, 0xBB, 0xBB,
]
pri const CUBE_ROOT : roarray[256] base.u8 = [
0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x05, 0x05,
0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x08, 0x08, 0x09, 0x09, 0x09, 0x0A, 0x0A, 0x0B, 0x0B, 0x0B,
+32
View File
@@ -7,3 +7,35 @@ compression format for opaque, still images.
The ["Making Software - Image
Compression"](https://www.makingsoftware.com/chapters/image-compression) book
chapter also has a good overview of a number of image formats, including JPEG.
# Further Reading
Web pages:
- [DCT and DFT for 1D and 2D Signals](https://ekrem-7-fidan.medium.com/dct-and-dft-for-1d-and-2d-signals-discrete-cosine-transform-and-discrete-fourier-transform-59feb837441d) by Ekrem Fidan.
- [How JPEG works](https://cgjennings.ca/articles/jpeg-compression/) by Christopher G. Jennings.
- [How JPG Works](https://www.freecodecamp.org/news/how-jpg-works-a4dbd2316f35/) by Colt McAnlis.
- [JPEG compression](https://www.sophielwang.com/blog/jpeg) by Sophie Wang.
- [JPEG Series, Part I: Visualizing the Inverse Discrete Cosine Transform](https://alexdowad.github.io/visualizing-the-idct/) by Alex Dowad.
- [JPEG Series, Part II: Huffman Coding](https://alexdowad.github.io/huffman-coding/) by Alex Dowad.
- [Not so fast, Mr. Fourier!](https://lcamtuf.substack.com/p/not-so-fast-mr-fourier) by Michał Zalewski.
- [Understanding and Decoding a JPEG Image using Python](https://yasoob.me/posts/understanding-and-writing-jpeg-decoder-in-python/) by Yasoob Khalid.
- [Unraveling the JPEG](https://parametric.press/issue-01/unraveling-the-jpeg/) by Omar Shehata.
- [Wikipedia's JPEG Encoding page](https://en.wikipedia.org/wiki/JPEG#Encoding).
YouTube videos:
- [JPEG DCT, Discrete Cosine Transform (JPEG Pt2)- Computerphile](https://www.youtube.com/watch?v=Q2aEzeMDHMA).
- [The Unreasonable Effectiveness of JPEG: A Signal Processing Approach](https://www.youtube.com/watch?v=0me3guauqOU).
Books:
You may be able to find these books at your local university's science library.
- [Compressed Image File Formats: JPEG, PNG, GIF, XBM, BMP](https://amazon.com/dp/0201604434), also known as the white book.
- [JPEG: Still Image Data Compression Standard](https://amazon.com/dp/0442012721), also known as the pink book.
Link collections:
- [https://pinboard.in/u:thakis/t:jpeg/](https://pinboard.in/u:thakis/t:jpeg/)
+9 -2
View File
@@ -2072,7 +2072,9 @@ pri func decoder.swizzle_colorful!(dst: ptr base.pixel_buffer, workbuf: slice ba
var height1 : base.u32[..= 0x1_0008]
var height2 : base.u32[..= 0x1_0008]
var height3 : base.u32[..= 0x1_0008]
var status : base.status
var ycc_upsampling : base.u8
var status : base.status
if this.swizzle_immediately {
i = this.swizzle_immediately_c_offsets[0] as base.u64
@@ -2137,6 +2139,11 @@ pri func decoder.swizzle_colorful!(dst: ptr base.pixel_buffer, workbuf: slice ba
}
}
ycc_upsampling = base.YCC_UPSAMPLING__BASIC as base.u8
if not this.use_lower_quality {
ycc_upsampling = base.YCC_UPSAMPLING__FANCY_LIKE_LIBJPEG as base.u8
}
status = this.swizzler.swizzle_ycck!(
dst: args.dst,
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
@@ -2169,7 +2176,7 @@ pri func decoder.swizzle_colorful!(dst: ptr base.pixel_buffer, workbuf: slice ba
v2: this.components_v[2],
v3: this.components_v[3],
ycc_model: this.ycc_model,
triangle_filter_for_2to1: not this.use_lower_quality,
ycc_upsampling: ycc_upsampling,
scratch_buffer_2k: this.swizzle_ycck_scratch_buffer_2k[..])
return status
}
+8
View File
@@ -0,0 +1,8 @@
# VP8
VP8 is a compressed video data format, described in [RFC
6386](https://www.ietf.org/rfc/rfc6386.txt).
The WebM movie (moving image) format consists of muliple VP8 frames, both key
(I-Frames) and non-key (B-Frames, P-Frames, etc). Separate from WebM, the WebP
still image format essentially wraps one VP8 key frame.
+4
View File
@@ -8,6 +8,10 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// RFC 6386 Section 7. Boolean Entropy Decoder.
pri func decoder.read_bit!(workbuf: roslice base.u8, part: base.u32[..= 8], prob: base.u32[..= 0xFF]) base.u32[..= 1] {
var p : base.u32[..= 8]
var c8 : base.u8
+12 -4
View File
@@ -8,6 +8,14 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// RFC 6386 Section 13. DCT Coefficient Decoding.
//
// and
//
// RFC 6386 Section 13.2. Coding of Individual Coefficient Values.
pri func decoder.decode_coefficients!(
workbuf: slice base.u8,
mbx: base.u32[..= 0x3FF],
@@ -36,7 +44,7 @@ pri func decoder.decode_coefficients!(
tnz = 1 & (this.mb_states_top[args.mbx] >> 23)
this.mb_states_top[args.mbx] &= 0xFF7F_FFFF
lnz = this.decode_block_coefficients!(
lnz = this.decode_subblock_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
@@ -67,7 +75,7 @@ pri func decoder.decode_coefficients!(
b = (4 * by) + bx
lnz = this.decode_block_coefficients!(
lnz = this.decode_subblock_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
@@ -110,7 +118,7 @@ pri func decoder.decode_coefficients!(
this.mb_states_top[args.mbx] &= 0xFFFF_FFFF ^ ((1 as base.u32) << (28 + bc + bx))
b = (2 * bc) + (2 * by) + bx + 16
lnz = this.decode_block_coefficients!(
lnz = this.decode_subblock_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
@@ -158,7 +166,7 @@ pri func decoder.initialize_mb_coeffs!() {
}
}
pri func decoder.decode_block_coefficients!(
pri func decoder.decode_subblock_coefficients!(
workbuf: slice base.u8,
mby: base.u32[..= 0x3FF],
seg: base.u32[..= 3],
+13 -1
View File
@@ -8,6 +8,10 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// A simplified and optimized version of decoder.inverse_dct_full when the AC
// coefficients are all zero.
pri func decoder.inverse_dct_dc_only!(
cachex: base.u32[..= 28],
cachey: base.u32[..= 22],
@@ -25,6 +29,9 @@ pri func decoder.inverse_dct_dc_only!(
}
}
// RFC 6386 Section 14.4. Implementation of the DCT Inversion.
//
// It reads from mb_coeffs and yuv_cache and writes to yuv_cache.
pri func decoder.inverse_dct_full!(
cachex: base.u32[..= 28],
cachey: base.u32[..= 22],
@@ -119,8 +126,10 @@ pri func decoder.inverse_dct_full!(
var q32 : base.u32
var q33 : base.u32
// 85627 is approximately 65536 * cos(pi/8) * sqrt(2).
// 35468 is approximately 65536 * sin(pi/8) * sqrt(2).
//
// 85627 is approximately 65536 * cos(pi/8) * sqrt(2). Compared to the RFC
// implementation 85627 is also (20091 + 65536).
// --------
@@ -299,6 +308,9 @@ pri func decoder.inverse_dct_full!(
this.yuv_cache[args.cachey + 3][args.cachex + 3] as base.u32) ~mod+ q33)]
}
// RFC 6386 Section 14.3. Implementation of the WHT Inversion.
//
// It reads from and writes to mb_coeffs.
pri func decoder.inverse_wht!() {
var a00 : base.u32
var a01 : base.u32
+57 -69
View File
@@ -8,10 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status {
var mby : base.u32
var mbx : base.u32
var status : base.status
pri func decoder.decode_macroblocks!(workbuf: slice base.u8) base.status {
var mby : base.u32
var mbx : base.u32
mbx = 0
while mbx < this.mbw {
@@ -28,6 +27,8 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice
this.mb_states_left = 0
// Decode this row, using this.mb_coeffs and this.yuv_cache before
// ultimately writing to args.workbuf.
mbx = 0
while mbx < this.mbw,
inv mby < 0x400,
@@ -39,6 +40,7 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice
mbx += 1
}
// Filter the previous row, already in args.workbuf.
if mby > 0 {
if this.filt_level <= 0 {
// No-op.
@@ -47,30 +49,21 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice
} else {
this.filter_normal!(workbuf: args.workbuf, mby: mby - 1)
}
status = this.swizzle_one_row_of_macroblocks!(dst: args.dst, workbuf: args.workbuf, mby: mby - 1)
if not status.is_ok() {
return status
}
}
mby += 1
}
// Filter the final row, already in args.workbuf.
if this.mbh > 0 {
mby = this.mbh - 1
if this.filt_level <= 0 {
// No-op.
} else if this.filt_simple {
this.filter_simple!(workbuf: args.workbuf, mby: mby)
this.filter_simple!(workbuf: args.workbuf, mby: this.mbh - 1)
} else {
this.filter_normal!(workbuf: args.workbuf, mby: mby)
this.filter_normal!(workbuf: args.workbuf, mby: this.mbh - 1)
}
status = this.swizzle_one_row_of_macroblocks!(dst: args.dst, workbuf: args.workbuf, mby: mby)
if not status.is_ok() {
return status
}
}
return ok
@@ -85,6 +78,7 @@ pri func decoder.decode_one_macroblock!(workbuf: slice base.u8, mbx: base.u32[..
var luma_mode : base.u32[..= 4]
var chroma_mode : base.u32[..= 3]
// RFC 6386 Section 10. Segment-Based Feature Adjustments.
seg = 0
if this.seg_update_map {
v1 = this.read_bit!(workbuf: args.workbuf, part: 0, prob: this.seg_probs[0] as base.u32)
@@ -347,85 +341,79 @@ pri func decoder.copy_from_yuv_cache!(
}
}
pri func decoder.swizzle_one_row_of_macroblocks!(
pri func decoder.swizzle!(
dst: ptr base.pixel_buffer,
workbuf: slice base.u8,
mby: base.u32[..= 0x3FF]) base.status {
var y_min_incl : base.u32[..= 0x3FFF]
var y_max_excl : base.u32[..= 0x3FFF]
blend: base.pixel_blend,
workbuf: slice base.u8) base.status {
var wh : base.u64[..= 0xFFF_8001]
var prefix : slice base.u8
var suffix : slice base.u8
var has_alpha : base.u32[..= 1]
var ycc_model : base.u32[..= 0xFF]
var status : base.status
var o : base.u64
var src0 : slice base.u8
var src1 : slice base.u8
var src2 : slice base.u8
if this.quirk_has_alpha_side_channel {
if this.quirk_width_and_height == 0 {
return "#inconsistent width and height"
}
wh = (this.width * this.height) as base.u64
if wh > args.workbuf.length() {
return base."#bad workbuf length"
}
prefix = args.workbuf[.. wh]
suffix = args.workbuf[wh ..]
has_alpha = 1
ycc_model = base.YCC_MODEL__BT_601_STUDIO_RANGE__ALPHA__SRC |
(0x01 & (this.util.demake_pixel_blend(a: args.blend) as base.u32))
var status : base.status
} else {
prefix = this.util.empty_slice_u8()
suffix = args.workbuf
has_alpha = 0
ycc_model = base.YCC_MODEL__BT_601_STUDIO_RANGE
}
if (this.workbuf_yuv_y_end > this.workbuf_yuv_u_end) or
(this.workbuf_yuv_u_end > this.workbuf_yuv_v_end) or
(this.workbuf_yuv_v_end > args.workbuf.length()) {
(this.workbuf_yuv_v_end > suffix.length()) {
return base."#bad workbuf length"
}
assert this.workbuf_yuv_u_end <= args.workbuf.length() via "a <= b: a <= c; c <= b"(c: this.workbuf_yuv_v_end)
assert this.workbuf_yuv_y_end <= args.workbuf.length() via "a <= b: a <= c; c <= b"(c: this.workbuf_yuv_u_end)
// There are 16 rows of pixels per macroblock. However, the bottom 6 rows
// of pixels (the bottom 3 rows of samples, at 4:2:0 chroma subsampling)
// can be modified by the the macroblock below's loop filtering (which
// happens after decoder.swizzle_one_row_of_macroblocks returns), unless
// we're swizzling the bottom-most row of macroblocks.
y_min_incl = (args.mby * 16) ~sat- 6
y_max_excl = (args.mby * 16) + 10
if (args.mby + 1) >= this.mbh {
y_max_excl = this.height
}
o = (y_min_incl * this.workbuf_yuv_y_stride) as base.u64
if o < this.workbuf_yuv_y_end {
src0 = args.workbuf[o .. this.workbuf_yuv_y_end]
}
o = (((y_min_incl / 2) * this.workbuf_yuv_uv_stride) as base.u64) + this.workbuf_yuv_y_end
if o < this.workbuf_yuv_u_end {
src1 = args.workbuf[o .. this.workbuf_yuv_u_end]
}
o = (((y_min_incl / 2) * this.workbuf_yuv_uv_stride) as base.u64) + this.workbuf_yuv_u_end
if o < this.workbuf_yuv_v_end {
src2 = args.workbuf[o .. this.workbuf_yuv_v_end]
}
assert this.workbuf_yuv_u_end <= suffix.length() via "a <= b: a <= c; c <= b"(c: this.workbuf_yuv_v_end)
assert this.workbuf_yuv_y_end <= suffix.length() via "a <= b: a <= c; c <= b"(c: this.workbuf_yuv_u_end)
status = this.swizzler.swizzle_ycck!(
dst: args.dst,
dst_palette: args.dst.palette(),
x_min_incl: 0,
x_max_excl: this.width,
y_min_incl: y_min_incl,
y_max_excl: y_max_excl,
src0: src0,
src1: src1,
src2: src2,
src3: this.util.empty_slice_u8(),
y_min_incl: 0,
y_max_excl: this.height,
src0: suffix[.. this.workbuf_yuv_y_end],
src1: suffix[this.workbuf_yuv_y_end .. this.workbuf_yuv_u_end],
src2: suffix[this.workbuf_yuv_u_end .. this.workbuf_yuv_v_end],
src3: prefix,
width0: this.mbw * 16,
width1: this.mbw * 8,
width2: this.mbw * 8,
width3: 0,
height0: (this.mbh * 16) ~sat- y_min_incl,
height1: (this.mbh * 8) ~sat- (y_min_incl / 2),
height2: (this.mbh * 8) ~sat- (y_min_incl / 2),
height3: 0,
width3: this.width * has_alpha,
height0: this.mbh * 16,
height1: this.mbh * 8,
height2: this.mbh * 8,
height3: this.height * has_alpha,
stride0: this.mbw * 16,
stride1: this.mbw * 8,
stride2: this.mbw * 8,
stride3: 0,
stride3: this.width * has_alpha,
h0: 2,
h1: 1,
h2: 1,
h3: 0,
h3: (2 * has_alpha) as base.u8,
v0: 2,
v1: 1,
v2: 1,
v3: 0,
ycc_model: base.YCC_MODEL__BT_601_STUDIO_RANGE as base.u8,
triangle_filter_for_2to1: false,
v3: (2 * has_alpha) as base.u8,
ycc_model: ycc_model as base.u8,
ycc_upsampling: base.YCC_UPSAMPLING__FANCY_LIKE_LIBWEBP as base.u8,
scratch_buffer_2k: this.swizzle_ycck_scratch_buffer_2k[..])
return status
}
+7 -1
View File
@@ -10,7 +10,13 @@
// --------
// Predicts a 4×4 luma block, the Xs below, based on samples top and left.
// RFC 6386 Section 12. Intraframe Prediction.
//
// These methods read from and write to yuv_cache.
// --------
// Predicts a 4×4 luma subblock, the Xs below, based on samples top and left.
//
// a b c d e f g h i
// p X X X X
+43
View File
@@ -0,0 +1,43 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// Quirks are discussed in (/doc/note/quirks.md).
//
// The base38 encoding of "vp8." is 0x1B_5EFE. Left shifting by 10 gives
// 0x6D7B_F800.
pri const QUIRKS_BASE : base.u32 = 0x6D7B_F800
// --------
// When this quirk is enabled, the workbuf starts with (width × height) alpha
// samples, decoded out-of-band (from the VP8 decoder's perspective, they are
// typically decoded in-band from the surrounding WebP decoder's perspective).
pub const QUIRK_HAS_ALPHA_SIDE_CHANNEL : base.u32 = 0x6D7B_F800 | 0x00
// Gives the image width and height out-of-band, in the u64 quirk value. The
// low 32 bits are the width and the high 32 bits are the height. Both
// dimensions should range within 0x0001 ..= 0x3FFF.
//
// A simple WebP file could contain one chunk, a VP8 chunk, that gives the
// image width and height. A complex WebP file could contain three chunks:
// VP8X, ALPH and VP8. The image dimensions are given in the VP8X chunk and are
// then repeated in the VP8 chunk.
//
// After a webp.decoder decode_image_config call, the caller might next want to
// query the workbuf length, to know how much memory to dynamically allocate,
// especially before attempting to decode the ALPH chunk. The outer
// webp.decoder workbuf_len method can delegate to the inner vp8.decoder
// workbuf_len method, but the latter needs to know the image dimensions.
//
// This quirk mechanism lets the outer webp decoder tell the inner vp8 decoder
// those dimensions, after parsing the VP8X chunk but before ALPH or VP8.
pub const QUIRK_WIDTH_AND_HEIGHT : base.u32 = 0x6D7B_F800 | 0x01
+68
View File
@@ -8,6 +8,66 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// Reconstructing YUV samples involves adding predictions (based on previous
// YUV samples, in macroblocks one column left or one column above) to
// residuals (decoded from the bitstream, dequantized and IDCT'ed).
//
// There are 16×16 = 256 luma and 2×8×8 = 128 chroma samples per macroblock.
// The decoder.yuv_cache field is a bit larger than that, being an array[26]
// array[32] base.u8. Here's the layout.
//
// 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
// . . . . . . . a b b b b b b b b b b b b b b b b c c c c . . . . 00
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 01
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 02
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 03
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y c c c c . . . . 04
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 05
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 06
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 07
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y c c c c . . . . 08
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 09
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0A
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0B
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y c c c c . . . . 0C
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0D
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0E
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0F
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 10
// . . . . . . . e f f f f f f f f . . . . . . . g h h h h h h h h 11
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 12
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 13
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 14
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 15
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 16
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 17
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 18
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 19
//
// Y, U and V are this macroblock's luma and chroma values.
//
// The Y predictions (per 4×4 subblock) depend on the row above's Y values
// (either abc or dYc) and the column left's Y values (ad or bY).
//
// The U predictions (per 8×8 block) depend on the row above's U values (ef)
// and the column left's U values (i).
//
// The V predictions (per 8×8 block) depend on the row above's V values (gh)
// and the column left's V values (j).
//
// For top-row (mby == 0) macroblocks, abcefgh is initialized to 0x7F. For
// other macroblocks, these are copied from the bottom-most row of the
// macroblock above (before filtering, for I-Frames), from the workbuf.
//
// The c values are then duplicated from row 0x00 to rows 0x04, 0x08 and 0x0C.
//
// For left-column (mbx == 0) macroblocks, adeigj are initialized to 0x81 (but
// aeg is 0x7F if mby is also 0). For other macroblocks, these are copied from
// the right-most column of the macroblock to the left (before filtering, for
// I-Frames), from the yuv_cache to itself, shifted by 8 or 16 columns.
pri func decoder.reconstruct!(
workbuf: slice base.u8,
mbx: base.u32[..= 0x3FF],
@@ -120,7 +180,10 @@ pri func decoder.prepare_yuv_cache!(
var offset : base.u64[..= 0x17FE_FFF8]
var o : base.u64
// For abcdefghij comments, see the layout at the top of this file.
if args.mbx <= 0 {
// Initialize adeigj to 0x81.
j = 0x00
while j < 0x11 {
this.yuv_cache[j][0x07] = 0x81
@@ -133,6 +196,7 @@ pri func decoder.prepare_yuv_cache!(
}
} else {
// Copy from the previous (left) macroblock, in yuv_cache, to adeigj.
j = 0x00
while j < 0x11 {
this.yuv_cache[j][0x07] = this.yuv_cache[j][0x17]
@@ -146,6 +210,7 @@ pri func decoder.prepare_yuv_cache!(
}
if args.mby <= 0 {
// Initialize abcefgh to 0x7F.
i = 0x00
while i < 0x20 {
this.yuv_cache[0x00][i] = 0x7F
@@ -158,6 +223,8 @@ pri func decoder.prepare_yuv_cache!(
}
} else {
// Copy from the previous (above) macroblock, in args.workbuf, to bcfh.
offset = 0 +
(((((args.mby * 16) - 1) * this.workbuf_yuv_y_stride) + (args.mbx * 16)) as base.u64)
i = 0x00
@@ -220,6 +287,7 @@ pri func decoder.prepare_yuv_cache!(
}
}
// Duplicate the c values.
j = 0x04
while j < 0x10 {
this.yuv_cache[j][0x18] = this.yuv_cache[0][0x18]
+148 -31
View File
@@ -9,13 +9,15 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
pub status "#bad QUIRK_SOURCE_LENGTH"
pub status "#bad QUIRK_WIDTH_AND_HEIGHT"
pub status "#bad header"
pub status "#inconsistent width and height"
pub status "#missing QUIRK_SOURCE_LENGTH"
pub status "#truncated input"
pub status "#unsupported QUIRK_SOURCE_LENGTH"
pub status "#unsupported VP8 file"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x20FF_FFFF
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x30FF_8000
pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0x3FFF],
@@ -31,15 +33,24 @@ pub struct decoder? implements base.image_decoder(
is_key_frame : base.bool,
quirk_has_alpha_side_channel : base.bool,
frame_config_io_position : base.u64,
// This uses the same representation as an wuffs_base__optional_u63:
// ((value_as_u63 << 1) | has_value_as_u1).
quirk_source_length : base.u64,
quirk_width_and_height : base.u64,
// Just under 144 MiB, for 9 partitions each up to 0xFF_FFFF bytes.
partitioned_data_length : base.u32[..= 0x08FF_FFF7],
// args.workbuf strides and offsets.
//
// The foobar_end offsets are measured after any "prefix trimmed work
// buffer" adjustment (if QUIRK_HAS_ALPHA_SIDE_CHANNEL is enabled).
//
// The workbuf holds the YUV buffers (after decoding from the source
// bytes but before converting YUV to RGBX) and then also a copy of
// most of the source bytes themselves - the "at least 2, up to 9"
@@ -119,6 +130,7 @@ pub struct decoder? implements base.image_decoder(
// hold in the 0x100 bit) and prob_skip_false (in the low 8 bits).
prob_skip : base.u32[..= 0x1FF],
// 4×4 luma subblock prediction modes.
mb_subblock_modes : array[16] base.u8[..= 9],
// Two u32 values whose bits are packed like this:
@@ -132,6 +144,13 @@ pub struct decoder? implements base.image_decoder(
mb_dc_nz : base.u32,
mb_ac_nz : base.u32,
// The workspace for the reconstruction-in-progress macroblock,
// containing the predicted and then fully decoded (prediction plus
// post-IDCT residuals) YUV samples to copy out to the workbuf. Its
// edges also hold samples from previous (left and top) macroblocks
// that are used in calculating this macroblock's predictions.
//
// See the comments near decoder.reconstruct for the layout.
yuv_cache : array[26] array[32] base.u8,
swizzler : base.pixel_swizzler,
@@ -142,6 +161,20 @@ pub struct decoder? implements base.image_decoder(
// 1056 is (4 planes, 8 bands, 3 contexts, 11 tokens) flattened.
coeff_probs : array[1056] base.u8,
// Macroblock coefficients. Per this.decode_one_macroblock:
//
// this.decode_coefficients decodes these macroblock coefficients from
// the bitstream and dequantizes them, writing them to this.mb_coeffs.
// At this point, these are pre-IDCT residuals.
//
// this.reconstruct calculates the predictions (writing them to
// this.yuv_cache, not this.mb_coeffs) and then applies the inverse WHT
// (if applicable) and the inverse DCT to the pre-IDCT residuals, by
// calling this.inverse_dct_etc.
//
// this.inverse_dct_etc reads the pre-IDCT residuals from
// this.mb_coeffs and writes fully decoded samples (predictions plus
// post-IDCT residuals) to this.yuv_cache.
mb_coeffs : array[25] array[16] base.u16,
// Macroblock states are a u32 whose bits are packed like this:
@@ -181,15 +214,54 @@ pub struct decoder? implements base.image_decoder(
pub func decoder.get_quirk(key: base.u32) base.u64 {
if args.key == base.QUIRK_SOURCE_LENGTH {
return this.quirk_source_length
} else if args.key == QUIRK_HAS_ALPHA_SIDE_CHANNEL {
if this.quirk_has_alpha_side_channel {
return 1
}
} else if args.key == QUIRK_WIDTH_AND_HEIGHT {
return this.quirk_width_and_height
}
return 0
}
pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
var w : base.u32
var h : base.u32
if args.key == base.QUIRK_SOURCE_LENGTH {
this.quirk_source_length = args.value
if (args.value & 1) == 0 {
if args.value <> 0 {
return "#bad QUIRK_SOURCE_LENGTH"
}
this.quirk_source_length = 0
this.partitioned_data_length = 0
} else if (args.value >> 1) < 10 {
return "#bad QUIRK_SOURCE_LENGTH"
} else if (args.value >> 1) > (0x08FF_FFF7 + 10) {
return "#unsupported QUIRK_SOURCE_LENGTH"
} else {
this.quirk_source_length = args.value
this.partitioned_data_length = ((args.value >> 1) as base.u32) - 10
}
return ok
} else if args.key == QUIRK_HAS_ALPHA_SIDE_CHANNEL {
this.quirk_has_alpha_side_channel = args.value <> 0
return ok
} else if args.key == QUIRK_WIDTH_AND_HEIGHT {
w = (args.value & 0xFFFF_FFFF) as base.u32
h = (args.value >> 32) as base.u32
if (w < 1) or (0x3FFF < w) or (h < 1) or (0x3FFF < h) {
return "#bad QUIRK_WIDTH_AND_HEIGHT"
}
this.quirk_width_and_height = args.value
this.width = w
this.height = h
this.calculate_mbw_mbh!()
return ok
}
return base."#unsupported option"
}
@@ -212,14 +284,9 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
return base."#bad call sequence"
}
if (this.quirk_source_length & 1) == 0 {
if this.quirk_source_length == 0 {
return "#missing QUIRK_SOURCE_LENGTH"
} else if (this.quirk_source_length >> 1) < 10 {
return "#bad QUIRK_SOURCE_LENGTH"
} else if (this.quirk_source_length >> 1) > (0x08FF_FFF7 + 10) {
return "#unsupported QUIRK_SOURCE_LENGTH"
}
this.partitioned_data_length = ((this.quirk_source_length >> 1) as base.u32) - 10
// RFC 6386 Section 9. Frame Header, up to and including 9.1. Uncompressed
// Data Chunk. decode_header_partition handles the rest of section 9.
@@ -243,17 +310,13 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
c32 = args.src.read_u32le?()
this.width = 0x3FFF & (c32 >> 0)
this.height = 0x3FFF & (c32 >> 16)
this.mbw = (this.width + 15) / 16
this.mbh = (this.height + 15) / 16
this.workbuf_yuv_y_stride = this.mbw * 16
this.workbuf_yuv_uv_stride = this.mbw * 8
this.workbuf_yuv_y_end =
((this.workbuf_yuv_y_stride as base.u64) * ((this.mbh * 16) as base.u64))
this.workbuf_yuv_u_end = this.workbuf_yuv_y_end +
((this.workbuf_yuv_uv_stride as base.u64) * ((this.mbh * 8) as base.u64))
this.workbuf_yuv_v_end = this.workbuf_yuv_u_end +
((this.workbuf_yuv_uv_stride as base.u64) * ((this.mbh * 8) as base.u64))
if (this.width == 0) or (this.height == 0) {
return "#bad header"
} else if (this.quirk_width_and_height <> 0) and
(this.quirk_width_and_height <> ((this.width as base.u64) | ((this.height as base.u64) << 32))) {
return "#inconsistent width and height"
}
this.calculate_mbw_mbh!()
this.frame_config_io_position = args.src.position()
@@ -270,6 +333,20 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
this.call_sequence = 0x20
}
pri func decoder.calculate_mbw_mbh!() {
this.mbw = (this.width + 15) / 16
this.mbh = (this.height + 15) / 16
this.workbuf_yuv_y_stride = this.mbw * 16
this.workbuf_yuv_uv_stride = this.mbw * 8
this.workbuf_yuv_y_end =
((this.workbuf_yuv_y_stride as base.u64) * ((this.mbh * 16) as base.u64))
this.workbuf_yuv_u_end = this.workbuf_yuv_y_end +
((this.workbuf_yuv_uv_stride as base.u64) * ((this.mbh * 8) as base.u64))
this.workbuf_yuv_v_end = this.workbuf_yuv_u_end +
((this.workbuf_yuv_uv_stride as base.u64) * ((this.mbh * 8) as base.u64))
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var status : base.status
@@ -317,23 +394,52 @@ pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.
}
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var wh : base.u64[..= 0xFFF_8001]
var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE]
var status : base.status
// The args.workbuf, with a "prefix trimmed work buffer" adjustment, if
// QUIRK_HAS_ALPHA_SIDE_CHANNEL is enabled, in which case the first (width
// × height) bytes of args.workbuf hold alpha samples that are e.g. part of
// WebP, not part of VP8 per se.
//
// The prefix isn't used when decoding VP8 to YCbCr, but the final swizzle
// to the destination pixel buffer should combine YCbCr and Alpha.
var trimmed : slice base.u8
while true {
wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8
if wb_len > args.workbuf.length() {
return base."#bad workbuf length"
} else {
args.workbuf = args.workbuf[.. wb_len]
trimmed = args.workbuf
if this.quirk_has_alpha_side_channel {
if this.quirk_width_and_height == 0 {
return "#inconsistent width and height"
}
wh = (this.width * this.height) as base.u64
if wh > args.workbuf.length() {
return base."#bad workbuf length"
}
trimmed = args.workbuf[wh ..]
}
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
if (status == base."$short read") and args.src.is_closed() {
wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8
if wb_len > trimmed.length() {
return base."#bad workbuf length"
} else {
trimmed = trimmed[.. wb_len]
}
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: trimmed, opts: args.opts)
if status.is_ok() {
break
} else if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
}
status = this.swizzle!(dst: args.dst, blend: args.blend, workbuf: args.workbuf)
if not status.is_ok() {
return status
}
}
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
@@ -380,7 +486,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
return status
}
status = this.decode_macroblocks!(dst: args.dst, workbuf: args.workbuf)
status = this.decode_macroblocks!(workbuf: args.workbuf)
if not status.is_ok() {
return status
}
@@ -426,7 +532,7 @@ pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.sta
}
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
// No-op. VP8 doesn't support metadata (but WEBP does).
// No-op. VP8 doesn't support metadata (but WebP does).
}
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
@@ -434,10 +540,21 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
}
pub func decoder.workbuf_len() base.range_ii_u64 {
var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE]
var wh : base.u64[..= 0xFFF_8001]
var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE - 0xFFF_8001]
if this.quirk_source_length == 0 {
// (min_incl > max_incl) means an empty range_ii_u64. The workbuf
// length isn't known yet, as we don't know partitioned_data_length.
return this.util.make_range_ii_u64(min_incl: 1, max_incl: 0)
}
if this.quirk_has_alpha_side_channel {
wh = (this.width * this.height) as base.u64
}
wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8
return this.util.make_range_ii_u64(
min_incl: wb_len,
max_incl: wb_len)
min_incl: wh + wb_len,
max_incl: wh + wb_len)
}
+6 -3
View File
@@ -1,12 +1,15 @@
# WebP
WebP is a still image format, described in [RFC
6386](https://www.ietf.org/rfc/rfc6386.txt).
WebP is a still image format, described in the [WebP Container
Specification](https://developers.google.com/speed/webp/docs/riff_container).
It is a container for [VP8 Lossy](https://www.ietf.org/rfc/rfc6386.txt) or [VP8
Lossless](https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification).
The ["Making Software - Image
Compression"](https://www.makingsoftware.com/chapters/image-compression) book
chapter also has a good overview of a number of image formats, including WebP.
There are two WebP variants: lossy (based on VP8) and lossless. Wuffs' VP8
There are two WebP variants: lossy (based on VP8) and lossless (based on what's
essentially an unrelated design, other than "WebP" branding). Wuffs' VP8
implementation is in a separate package, as the WebM moving image format also
uses VP8. There's no Wuffs WebM package yet but there might be in the future.
+242
View File
@@ -0,0 +1,242 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.decode_alph?(src: base.io_reader, workbuf: slice base.u8) {
var r_mark : base.u64
var status : base.status
while true {
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
r_mark = args.src.mark()
status =? this.decode_alph_limited?(src: args.src, workbuf: args.workbuf)
this.sub_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if status.is_ok() {
break
} else if not status.is_suspension() {
return status
} else if (status == base."$short read") and (this.sub_chunk_length == 0) {
return "#short chunk"
}
yield? status
}
}
pri func decoder.decode_alph_limited?(src: base.io_reader, workbuf: slice base.u8) {
var n : base.u64
this.vp8x_alph_flags = args.src.read_u8?()
if (this.vp8x_alph_flags & 3) == 0 {
args.src.limited_copy_u32_to_slice!(up_to: this.width * this.height, s: args.workbuf)
} else if (this.vp8x_alph_flags & 3) == 1 {
this.do_decode_frame_vp8l?(
dst: nullptr,
src: args.src,
blend: this.util.make_pixel_blend(repr: base.PIXEL_BLEND__SRC),
workbuf: args.workbuf,
opts: nullptr)
n = (this.width * this.height * 4) as base.u64
if n <= args.workbuf.length() {
this.compact_alph!(s: args.workbuf[.. n])
}
} else {
return "#bad VP8X chunk"
}
this.vp8x_alph_flags |= 2
n = (this.width * this.height) as base.u64
if n <= args.workbuf.length() {
if (this.vp8x_alph_flags & 0x0C) == 0x04 {
this.filter_alph_horizontal!(s: args.workbuf[.. n])
} else if (this.vp8x_alph_flags & 0x0C) == 0x08 {
this.filter_alph_vertical!(s: args.workbuf[.. n])
} else if (this.vp8x_alph_flags & 0x0C) == 0x0C {
this.filter_alph_gradient!(s: args.workbuf[.. n])
}
}
}
pri func decoder.compact_alph!(s: slice base.u8) {
var i : base.u64[..= 0x4000_0004]
var j : base.u64[..= 0x4000_0004]
if args.s.length() > 0x4000_0000 {
return nothing
}
i = 0
j = 1
while (i < args.s.length()) and (j < args.s.length()),
inv args.s.length() <= 0x4000_0000,
{
assert i <= 0x4000_0000 via "a <= b: a <= c; c <= b"(c: args.s.length())
assert j <= 0x4000_0000 via "a <= b: a <= c; c <= b"(c: args.s.length())
args.s[i] = args.s[j]
i += 1
j += 4
}
}
// libwebp calls this HorizontalUnfilter_C.
pri func decoder.filter_alph_horizontal!(s: slice base.u8) {
var y : base.u32
var x : base.u32
var i : base.u64
var pred : base.u8
y = 0
while y < this.height {
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
pred = 0
if y > 0 {
i = ((y - 1) * this.width) as base.u64
if i >= args.s.length() {
break
}
pred = args.s[i]
}
x = 0
while x < this.width,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
i = ((y * this.width) + x) as base.u64
if i >= args.s.length() {
break
}
args.s[i] ~mod+= pred
pred = args.s[i]
x += 1
}
y += 1
}
}
// libwebp calls this VerticalUnfilter_C.
pri func decoder.filter_alph_vertical!(s: slice base.u8) {
var y : base.u32
var x : base.u32
var i : base.u64
var pred : base.u8
x = 0
while x < this.width {
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
pred = 0
if x > 0 {
i = (x - 1) as base.u64
if i >= args.s.length() {
break
}
pred = args.s[i]
}
y = 0
while y < this.height,
inv x < 0x4000,
{
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
i = ((y * this.width) + x) as base.u64
if i >= args.s.length() {
break
}
args.s[i] ~mod+= pred
pred = args.s[i]
y += 1
}
x += 1
}
}
// libwebp calls this GradientUnfilter_C.
pri func decoder.filter_alph_gradient!(s: slice base.u8) {
var y : base.u32
var x : base.u32
var i : base.u64
var pred : base.u8
var grad : base.u32
var l : base.u8 // Left sample.
var t : base.u8 // Top sample.
var tl : base.u8 // Top-Left sample.
// First row.
x = 0
while x < this.width {
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
i = x as base.u64
if i >= args.s.length() {
break
}
args.s[i] ~mod+= pred
pred = args.s[i]
x += 1
}
// Other rows.
y = 1
while y < this.height,
inv y >= 1,
{
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
i = (((y - 1) * this.width) + 0) as base.u64
if i >= args.s.length() {
break
}
t = args.s[i]
tl = t
l = t
x = 0
while x < this.width,
inv y >= 1,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
i = (((y - 1) * this.width) + x) as base.u64
if i >= args.s.length() {
break
}
t = args.s[i]
i = ((y * this.width) + x) as base.u64
if i >= args.s.length() {
break
}
grad = ((l as base.u32) ~mod+ (t as base.u32)) ~mod- (tl as base.u32)
if grad > 0x7FFF_FFFF {
grad = 0x00
} else if grad > 0xFF {
grad = 0xFF
}
l = args.s[i] ~mod+ ((grad & 0xFF) as base.u8)
args.s[i] = l
tl = t
x += 1
}
y += 1
}
}
+13 -13
View File
@@ -8,7 +8,7 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.apply_transform_predictor!(pix: slice base.u8, tile_data: roslice base.u8) {
pri func decoder.apply_transform_predictor!(pix: slice base.u8, width: base.u32[..= 0x4000], tile_data: roslice base.u8) {
var w4 : base.u64[..= 0x1_0000]
var prev_row : roslice base.u8
var curr_row : slice base.u8
@@ -37,11 +37,11 @@ pri func decoder.apply_transform_predictor!(pix: slice base.u8, tile_data: rosli
var sum_l : base.u32
var sum_t : base.u32
if (this.width <= 0) or (this.height <= 0) {
if (args.width <= 0) or (this.height <= 0) {
return nothing
}
w4 = (this.width * 4) as base.u64
w4 = (args.width * 4) as base.u64
curr_row = this.util.empty_slice_u8()
if w4 <= args.pix.length() {
curr_row = args.pix[.. w4]
@@ -62,7 +62,7 @@ pri func decoder.apply_transform_predictor!(pix: slice base.u8, tile_data: rosli
}
tile_size_log2 = this.transform_tile_size_log2[0] as base.u32
tiles_per_row = (this.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2
tiles_per_row = (args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2
mask = ((1 as base.u32) << tile_size_log2) - 1
y = 1
@@ -94,10 +94,10 @@ pri func decoder.apply_transform_predictor!(pix: slice base.u8, tile_data: rosli
}
x = 1
while x < this.width,
while x < args.width,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
assert x < 0x4000 via "a < b: a < c; c <= b"(c: args.width)
if ((x & mask) == 0) and (tile_data.length() >= 4) {
mode = tile_data[1] & 0x0F
@@ -283,7 +283,7 @@ pri func decoder.mode13(l: base.u8, t: base.u8, tl: base.u8) base.u8 {
return 0
}
pri func decoder.apply_transform_cross_color!(pix: slice base.u8, tile_data: roslice base.u8) {
pri func decoder.apply_transform_cross_color!(pix: slice base.u8, width: base.u32[..= 0x4000], tile_data: roslice base.u8) {
var tile_size_log2 : base.u32[..= 9]
var tiles_per_row : base.u32[..= 16895]
var mask : base.u32
@@ -301,7 +301,7 @@ pri func decoder.apply_transform_cross_color!(pix: slice base.u8, tile_data: ros
var r : base.u8
tile_size_log2 = this.transform_tile_size_log2[1] as base.u32
tiles_per_row = (this.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2
tiles_per_row = (args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2
mask = ((1 as base.u32) << tile_size_log2) - 1
y = 0
@@ -315,10 +315,10 @@ pri func decoder.apply_transform_cross_color!(pix: slice base.u8, tile_data: ros
}
x = 0
while x < this.width,
while x < args.width,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
assert x < 0x4000 via "a < b: a < c; c <= b"(c: args.width)
if ((x & mask) == 0) and (tile_data.length() >= 4) {
g2r = this.util.sign_extend_convert_u8_u32(a: tile_data[0])
@@ -357,7 +357,7 @@ pri func decoder.apply_transform_subtract_green!(pix: slice base.u8) {
}
}
pri func decoder.apply_transform_color_indexing!(pix: slice base.u8) {
pri func decoder.apply_transform_color_indexing!(pix: slice base.u8, width: base.u32[..= 0x4000]) {
var tile_size_log2 : base.u32[..= 9]
var bits_per_pixel : base.u32[..= 8]
var x_mask : base.u32
@@ -404,8 +404,8 @@ pri func decoder.apply_transform_color_indexing!(pix: slice base.u8) {
while y < this.height {
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
di = (4 * (y + 0) * this.width) as base.u64
dj = (4 * (y + 1) * this.width) as base.u64
di = (4 * (y + 0) * args.width) as base.u64
dj = (4 * (y + 1) * args.width) as base.u64
if (di > dj) or (dj > args.pix.length()) {
break
}
+484
View File
@@ -0,0 +1,484 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.do_decode_frame_vp8l?(dst: nptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var c8 : base.u8
var has_more : base.u32[..= 1]
var width : base.u32[..= 0x4000]
var dst : slice base.u8
var tile_data : roslice base.u8
var status : base.status
var pix : slice base.u8
var which : base.u32[..= 4]
var transform_type : base.u32[..= 3]
var ti : base.u64
var tj : base.u64
this.seen_transform[0] = false
this.seen_transform[1] = false
this.seen_transform[2] = false
this.seen_transform[3] = false
this.n_transforms = 0
while true {
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
has_more = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
if has_more == 0 {
break
}
this.decode_transform?(src: args.src, workbuf: args.workbuf)
}
width = this.width
if this.seen_transform[3] {
width = this.color_indexing_width
}
this.decode_color_cache_parameters?(src: args.src)
this.overall_color_cache_bits = this.color_cache_bits
this.decode_hg_table?(src: args.src, width: width, workbuf: args.workbuf)
this.color_cache_bits = this.overall_color_cache_bits
this.decode_huffman_groups?(src: args.src, n_huffman_groups: this.overall_n_huffman_groups)
while true {
if ((this.workbuf_offset_for_color_indexing as base.u64) > (this.workbuf_offset_for_transform[0] as base.u64)) or
((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
assert (this.workbuf_offset_for_transform[0] as base.u64) <= args.workbuf.length() via "a <= b: a <= c; c <= b"(c: this.workbuf_offset_for_transform[1] as base.u64)
dst = args.workbuf[
(this.workbuf_offset_for_color_indexing as base.u64) ..
(this.workbuf_offset_for_transform[0] as base.u64)]
tile_data = args.workbuf[
(this.workbuf_offset_for_transform[0] as base.u64) ..
(this.workbuf_offset_for_transform[1] as base.u64)]
status =? this.decode_pixels?(
dst: dst,
src: args.src,
width: width,
height: this.height,
tile_data: tile_data,
tile_size_log2: this.overall_tile_size_log2)
if status.is_ok() {
break
}
yield? status
}
if (this.workbuf_offset_for_transform[0] as base.u64) > args.workbuf.length() {
return base."#bad workbuf length"
}
pix = args.workbuf[.. (this.workbuf_offset_for_transform[0] as base.u64)]
width = this.width
if this.seen_transform[3] and
((this.workbuf_offset_for_color_indexing as base.u64) <= pix.length()) {
pix = pix[this.workbuf_offset_for_color_indexing as base.u64 ..]
width = this.color_indexing_width
}
which = this.n_transforms
while which > 0 {
which -= 1
transform_type = this.transform_type[which] as base.u32
tile_data = this.util.empty_slice_u8()
if transform_type < 2 {
ti = this.workbuf_offset_for_transform[transform_type + 1] as base.u64
tj = this.workbuf_offset_for_transform[transform_type + 2] as base.u64
if (ti <= tj) and (tj <= args.workbuf.length()) {
tile_data = args.workbuf[ti .. tj]
}
}
if transform_type == 0 {
this.apply_transform_predictor!(pix: pix, width: width, tile_data: tile_data)
} else if transform_type == 1 {
this.apply_transform_cross_color!(pix: pix, width: width, tile_data: tile_data)
} else if transform_type == 2 {
this.apply_transform_subtract_green!(pix: pix)
} else if (this.workbuf_offset_for_transform[0] as base.u64) <= args.workbuf.length() {
pix = args.workbuf[.. (this.workbuf_offset_for_transform[0] as base.u64)]
width = this.width
this.apply_transform_color_indexing!(pix: pix, width: width)
}
}
status = this.swizzle!(
dst: args.dst,
src: pix,
blend: args.blend)
if not status.is_ok() {
return status
}
this.call_sequence = 0x60
}
pri func decoder.decode_transform?(src: base.io_reader, workbuf: slice base.u8) {
var status : base.status
var c8 : base.u8
var transform_type : base.u32[..= 3]
var tile_size_log2 : base.u32[..= 9]
var width : base.u32[..= 0x4000]
var p : slice base.u8
if this.n_bits < 2 {
c8 = args.src.read_u8?()
if this.n_bits >= 2 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 2
}
transform_type = this.bits & 3
this.bits >>= 2
this.n_bits -= 2
if this.seen_transform[transform_type] or (this.n_transforms >= 4) {
return "#bad transform"
}
this.seen_transform[transform_type] = true
this.transform_type[this.n_transforms] = transform_type as base.u8
this.n_transforms += 1
if transform_type < 2 { // Predictor, Cross Color transform.
if this.n_bits < 3 {
c8 = args.src.read_u8?()
if this.n_bits >= 3 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 3
}
tile_size_log2 = (this.bits & 7) + 2
this.transform_tile_size_log2[transform_type] = tile_size_log2 as base.u8
this.bits >>= 3
this.n_bits -= 3
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
while true,
inv transform_type < 2,
inv tile_size_log2 >= 2,
{
width = this.width
if this.seen_transform[3] {
width = this.color_indexing_width
}
if ((this.workbuf_offset_for_transform[transform_type + 1] as base.u64) > (this.workbuf_offset_for_transform[transform_type + 2] as base.u64)) or
((this.workbuf_offset_for_transform[transform_type + 2] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
status =? this.decode_pixels?(
dst: args.workbuf[
(this.workbuf_offset_for_transform[transform_type + 1] as base.u64) ..
(this.workbuf_offset_for_transform[transform_type + 2] as base.u64)],
src: args.src,
width: (width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
height: (this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
if status.is_ok() {
break
}
yield? status
}
} else if transform_type == 2 { // Subtract Green transform.
// No-op.
} else { // Color Indexing transform.
if this.n_bits < 8 {
c8 = args.src.read_u8?()
if this.n_bits >= 8 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 8
}
this.color_indexing_palette_size = (this.bits & 0xFF) + 1
this.bits >>= 8
this.n_bits -= 8
if this.color_indexing_palette_size <= 2 {
this.color_indexing_width = (this.width + 7) / 8
this.transform_tile_size_log2[3] = 3
} else if this.color_indexing_palette_size <= 4 {
this.color_indexing_width = (this.width + 3) / 4
this.transform_tile_size_log2[3] = 2
} else if this.color_indexing_palette_size <= 16 {
this.color_indexing_width = (this.width + 1) / 2
this.transform_tile_size_log2[3] = 1
} else {
this.color_indexing_width = this.width
this.transform_tile_size_log2[3] = 0
}
if this.width >= this.color_indexing_width {
this.workbuf_offset_for_color_indexing = 4 * (this.width - this.color_indexing_width) * this.height
}
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
this.decode_pixels?(
dst: this.palette[.. 4 * this.color_indexing_palette_size],
src: args.src,
width: this.color_indexing_palette_size,
height: 1,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
this.palette[4 * this.color_indexing_palette_size .. 1024].bulk_memset!(byte_value: 0)
p = this.palette[.. 4 * this.color_indexing_palette_size]
while p.length() >= 8 {
p[4] ~mod+= p[0]
p[5] ~mod+= p[1]
p[6] ~mod+= p[2]
p[7] ~mod+= p[3]
p = p[4 ..]
}
}
}
pri func decoder.decode_color_cache_parameters?(src: base.io_reader) {
var c8 : base.u8
var use_color_cache : base.u32[..= 1]
var color_cache_bits : base.u32[..= 15]
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
use_color_cache = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
this.color_cache_bits = 0
if use_color_cache <> 0 {
if this.n_bits < 4 {
c8 = args.src.read_u8?()
if this.n_bits >= 4 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 4
}
color_cache_bits = this.bits & 15
this.bits >>= 4
this.n_bits -= 4
if (color_cache_bits < 1) or (11 < color_cache_bits) {
return "#bad color cache"
}
this.color_cache_bits = color_cache_bits
}
}
pri func decoder.decode_hg_table?(src: base.io_reader, width: base.u32[..= 0x4000], workbuf: slice base.u8) {
var status : base.status
var c8 : base.u8
var use_hg_table : base.u32[..= 1]
var tile_size_log2 : base.u32[..= 9]
var hg_pixels : slice base.u8
var n : base.u64
var p : roslice base.u8
var hg_plus_1 : base.u32[..= 256]
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
use_hg_table = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
if use_hg_table == 0 {
this.overall_n_huffman_groups = 1
this.overall_tile_size_log2 = 0
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
hg_pixels = args.workbuf[
this.workbuf_offset_for_transform[0] as base.u64 ..
this.workbuf_offset_for_transform[1] as base.u64]
if hg_pixels.length() >= 4 {
hg_pixels[0] = 0x00
hg_pixels[1] = 0x00
hg_pixels[2] = 0x00
hg_pixels[3] = 0x00
}
return ok
}
if this.n_bits < 3 {
c8 = args.src.read_u8?()
if this.n_bits >= 3 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 3
}
tile_size_log2 = (this.bits & 7) + 2
this.bits >>= 3
this.n_bits -= 3
this.overall_tile_size_log2 = tile_size_log2
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
while true,
inv tile_size_log2 >= 2,
{
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
status =? this.decode_pixels?(
dst: args.workbuf[
(this.workbuf_offset_for_transform[0] as base.u64) ..
(this.workbuf_offset_for_transform[1] as base.u64)],
src: args.src,
width: (args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
height: (this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
if status.is_ok() {
break
}
yield? status
}
this.overall_n_huffman_groups = 1
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
hg_pixels = args.workbuf[
this.workbuf_offset_for_transform[0] as base.u64 ..
this.workbuf_offset_for_transform[1] as base.u64]
n = (((args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2) *
((this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2) *
4) as base.u64
if n > hg_pixels.length() {
return base."#bad workbuf length"
}
p = hg_pixels[.. n]
while p.length() >= 4 {
if p[2] <> 0 {
return "#unsupported number of Huffman groups"
}
hg_plus_1 = (p[1] as base.u32) + 1
if this.overall_n_huffman_groups < hg_plus_1 {
this.overall_n_huffman_groups = hg_plus_1
}
p = p[4 ..]
}
}
pri func decoder.decode_pixels?(dst: slice base.u8, src: base.io_reader, width: base.u32[..= 0x4000], height: base.u32[..= 0x4000], tile_data: roslice base.u8, tile_size_log2: base.u32[..= 9]) {
var i : base.u32
var n : base.u32[..= 2048]
i = 0
n = (1 as base.u32) << this.color_cache_bits
while i < n {
assert i < 2048 via "a < b: a < c; c <= b"(c: n)
this.color_cache[i] = 0
i += 1
}
this.decode_pixels_slow?(
dst: args.dst,
src: args.src,
width: args.width,
height: args.height,
tile_data: args.tile_data,
tile_size_log2: args.tile_size_log2)
}
pri func decoder.swizzle!(dst: nptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status {
var status : base.status
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u32[..= 32]
var dst_bytes_per_row : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
var src_bytes_per_row : base.u64
var dst : slice base.u8
var y : base.u32
if args.dst == nullptr {
return ok
}
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette_or_else(fallback: this.palette[..]),
src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRA_NONPREMUL),
src_palette: this.util.empty_slice_u8(),
blend: args.blend)
if not status.is_ok() {
return status
}
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = dst_bits_per_pixel / 8
dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64
dst_palette = args.dst.palette_or_else(fallback: this.palette[..])
tab = args.dst.plane(p: 0)
src_bytes_per_row = (this.width * 4) as base.u64
while src_bytes_per_row <= args.src.length() {
dst = tab.row_u32(y: y)
if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row]
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: dst_palette,
src: args.src[.. src_bytes_per_row])
args.src = args.src[src_bytes_per_row ..]
y ~mod+= 1
}
return ok
}
+255 -481
View File
@@ -10,18 +10,20 @@
use "std/vp8"
pub status "#bad ALPH+VP8L combination"
pub status "#bad Huffman code (over-subscribed)"
pub status "#bad Huffman code (under-subscribed)"
pub status "#bad Huffman code"
pub status "#bad VP8X chunk"
pub status "#bad back-reference"
pub status "#bad chunk order"
pub status "#bad color cache"
pub status "#bad header"
pub status "#bad transform"
pub status "#short chunk"
pub status "#truncated input"
pub status "#unsupported number of Huffman groups"
pub status "#unsupported transform after color indexing transform"
pub status "#unsupported WebP file"
pub status "#unsupported number of Huffman groups"
pri status "#internal error: inconsistent Huffman code"
pri status "#internal error: inconsistent dst buffer"
@@ -32,7 +34,6 @@ pri status "#internal error: inconsistent n_bits"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x4C00_0000
pub struct decoder? implements base.image_decoder(
pixfmt : base.u32,
width : base.u32[..= 0x4000],
height : base.u32[..= 0x4000],
@@ -44,12 +45,49 @@ pub struct decoder? implements base.image_decoder(
sub_chunk_has_padding : base.bool,
is_vp8_lossy : base.bool,
// 0x01 = VP8 lossy
// 0x02 = VP8L lossless
// 0x04 = VP8X and seen VP8X chunks
// 0x08 = VP8X and seen VP8X, ICCP chunks
// 0x10 = VP8X and seen VP8X, ICCP†, ANIM chunks
// 0x20 = VP8X and seen VP8X, ICCP†, ANIM†, ANMF chunks
// 0x40 = VP8X and seen VP8X, ICCP†, ANIM†, ANMF†, ALPH chunks
// 0x81 = VP8X and seen VP8X, ICCP†, ANIM†, ANMF†, ALPH†, VP8 chunks
// 0x82 = VP8X and seen VP8X, ICCP†, ANIM†, ANMF†, ALPH†, VP8L chunks
//
// Chunks marked with a † are optional.
variant : base.u8,
// The "Rsv|I|L|E|X|A|R" bits from
// https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
//
// We also re-purpose the otherwise-unused 0x01 bit to mean VP8X-ness,
// so that (vp8x_header_flags <> 0) is an easy check for VP8X versus
// not-VP8X. Similarly, ((vp8x_header_flags & 0x11) == 0x01) is an easy
// check for "VP8X without an ALPH chunk".
//
// See also libwebp's src/webp/mux_types.h:
//
// ANIMATION_FLAG = 0x00000002,
// XMP_FLAG = 0x00000004,
// EXIF_FLAG = 0x00000008,
// ALPHA_FLAG = 0x00000010,
// ICCP_FLAG = 0x00000020,
vp8x_header_flags : base.u8,
// The "Rsv| P | F | C" bits from
// https://developers.google.com/speed/webp/docs/riff_container#alpha
//
// We also re-purpose the otherwise-unused 0x02 bit to mean that we
// have seen the ALPH chunk.
vp8x_alph_flags : base.u8,
frame_config_io_position : base.u64,
riff_chunk_length : base.u32,
rcl_remaining : base.u32,
sub_chunk_length : base.u32,
sub_chunk_type : base.u32,
bits : base.u32,
n_bits : base.u32[..= 31],
@@ -180,12 +218,13 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
if (this.riff_chunk_length & 1) <> 0 {
return "#bad header"
}
this.rcl_remaining = this.riff_chunk_length
while true {
io_limit (io: args.src, limit: this.riff_chunk_length as base.u64) {
io_limit (io: args.src, limit: this.rcl_remaining as base.u64) {
r_mark = args.src.mark()
status =? this.do_decode_image_config_limited?(dst: args.dst, src: args.src)
this.riff_chunk_length ~sat-=
this.rcl_remaining ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
@@ -193,7 +232,7 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
break
} else if not status.is_suspension() {
return status
} else if (status == base."$short read") and (this.riff_chunk_length == 0) {
} else if (status == base."$short read") and (this.rcl_remaining == 0) {
return "#short chunk"
}
yield? status
@@ -201,9 +240,9 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
this.frame_config_io_position = args.src.position()
if (not this.is_vp8_lossy) and (args.dst <> nullptr) {
if (this.variant <> 0x01) and (args.dst <> nullptr) {
args.dst.set!(
pixfmt: this.pixfmt,
pixfmt: base.PIXEL_FORMAT__BGRA_NONPREMUL,
pixsub: 0,
width: this.width,
height: this.height,
@@ -226,35 +265,37 @@ pri func decoder.do_decode_image_config_limited?(dst: nptr base.image_config, sr
c32 = args.src.read_u32le?()
if c32 == 'VP8 'le {
this.is_vp8_lossy = true
this.variant = 0x01
} else if c32 == 'VP8L'le {
// No-op.
this.variant = 0x02
} else if c32 == 'VP8X'le {
return "#unsupported WebP file"
this.variant = 0x04
} else {
return "#bad header"
}
this.sub_chunk_length = args.src.read_u32le?()
if this.sub_chunk_length < 4 {
return "#bad header"
}
this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0
if this.is_vp8_lossy {
if this.variant == 0x01 {
this.vp8.set_quirk!(
key: base.QUIRK_SOURCE_LENGTH,
value: ((this.sub_chunk_length as base.u64) << 1) | 1)
} else if this.variant == 0x02 {
// No-op.
} else if this.sub_chunk_length <> 10 {
return "#bad header"
}
while true {
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
r_mark = args.src.mark()
if this.is_vp8_lossy {
if this.variant == 0x01 {
status =? this.vp8.decode_image_config?(dst: args.dst, src: args.src)
} else {
} else if this.variant == 0x02 {
status =? this.do_decode_image_config_limited_vp8l?(src: args.src)
} else {
status =? this.do_decode_image_config_limited_vp8x?(src: args.src)
}
this.sub_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
@@ -284,10 +325,10 @@ pri func decoder.do_decode_image_config_limited_vp8l?(src: base.io_reader) {
c32 >>= 14
this.height = (c32 & 0x3FFF) + 1
c32 >>= 14
this.pixfmt = base.PIXEL_FORMAT__BGRX
if (c32 & 1) <> 0 {
this.pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
}
// In theory, (c32 & 1) would let us discriminate between
// base.PIXEL_FORMAT__BGRX and base.PIXEL_FORMAT__BGRA_NONPREMUL, but the
// VP8 Lossless spec says that this bit "is a hint only, and *should not
// impact decoding* [emphasis added]".
c32 >>= 1
if c32 <> 0 {
return "#bad header"
@@ -300,11 +341,90 @@ pri func decoder.do_decode_image_config_limited_vp8l?(src: base.io_reader) {
this.workbuf_offset_for_transform[3] = (4 * this.width * this.height) + (3 * transform_size)
}
pri func decoder.do_decode_image_config_limited_vp8x?(src: base.io_reader) {
var c32 : base.u32
var transform_size : base.u32[..= 0x400_0000]
var status : base.status
c32 = args.src.read_u32le?()
if (c32 & 0xFFFF_FFC1) <> 0 {
return "#unsupported WebP file"
}
this.vp8x_header_flags = ((c32 | 0x01) & 0xFF) as base.u8
c32 = args.src.read_u24le_as_u32?()
if c32 >= 0x3FFF {
return "#unsupported WebP file"
}
this.width = (c32 & 0x3FFF) + 1
c32 = args.src.read_u24le_as_u32?()
if c32 >= 0x3FFF {
return "#unsupported WebP file"
}
this.height = (c32 & 0x3FFF) + 1
// It's valid to call this.workbuf_len after this.decode_image_config
// returns, for the caller to know how much dynamically-sized memory
// (called workbuf memory) is needed to decode the image file.
//
// Unfortunately, having only seen the VP8X chunk, we don't know yet
// whether it's ultimately a VP8 or VP8L image, but we wouldn't know until
// after we've processed the ALPH chunk (if present), and processing the
// ALPH chunk requires workbuf memory (as the amount of memory depends on
// this.width and this.height).
//
// Wuffs' "decode an image" API assumes that the caller, not callee, is
// responsible for dynamically allocating memory. But also, it only assumes
// sequential access (not random access) to the io_reader.
//
// So, here, we have to cater for the worst case and set aside workbuf
// memory just like plain VP8L, even if it might end up being unused, for
// example: VP8X+VP8 instead of VP8X+VP8L or VP8X+ALPH+VP8.
transform_size = 4 * ((this.width + 3) >> 2) * ((this.height + 3) >> 2)
this.workbuf_offset_for_transform[0] = (4 * this.width * this.height) + (0 * transform_size)
this.workbuf_offset_for_transform[1] = (4 * this.width * this.height) + (1 * transform_size)
this.workbuf_offset_for_transform[2] = (4 * this.width * this.height) + (2 * transform_size)
this.workbuf_offset_for_transform[3] = (4 * this.width * this.height) + (3 * transform_size)
// The RIFF chunk length (which we have seen) is an upper bound for the VP8
// sub-chunk length (which we haven't seen yet). Set this now so that
// this.vp8.workbuf_len will return something definitive (non-empty).
//
// TODO: for animated WebP images, this upper bound essentially allocates
// O(numberOfAnimationFrames) memory. Ideally, it'd be O(1) instead.
//
// This might need tweaking the semantics of the wuffs_base__image_decoder
// API (which applies to multiple image formats, not just WebP). The API is
// that you can call workbuf_len as soon as decode_image_config returns.
// workbuf_len returns a range and any value within that range should be a
// valid length for the "dynamically allocated, not O(1) in terms of width
// and height" workbuf memory passed to decode_frame.
//
// We could relax this so that decode_frame could return a "$short workbuf"
// suspension, if the workbuf length is unknown at the time of the first
// decode_frame call, and the caller is expected to essentially realloc the
// workbuf, keeping any existing contents when growing it.
status = this.vp8.set_quirk!(
key: base.QUIRK_SOURCE_LENGTH,
value: ((this.riff_chunk_length as base.u64) << 1) | 1)
if not status.is_ok() {
return "#unsupported WebP file"
}
status = this.vp8.set_quirk!(
key: vp8.QUIRK_WIDTH_AND_HEIGHT,
value: ((this.width as base.u64) | ((this.height as base.u64) << 32)))
if not status.is_ok() {
return "#unsupported WebP file"
}
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var status : base.status
while true {
if this.is_vp8_lossy {
if this.variant == 0x01 {
status =? this.vp8.decode_frame_config?(dst: args.dst, src: args.src)
} else {
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
@@ -317,8 +437,6 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
}
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var pixfmt : base.pixel_format
if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
@@ -334,8 +452,7 @@ pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.
return base."@end of data"
}
if args.dst <> nullptr {
pixfmt = this.util.make_pixel_format(repr: this.pixfmt)
if (this.variant <> 0x01) and (args.dst <> nullptr) {
args.dst.set!(bounds: this.util.make_rect_ie_u32(
min_incl_x: 0,
min_incl_y: 0,
@@ -347,7 +464,7 @@ pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.
disposal: 0,
opaque_within_bounds: false,
overwrite_instead_of_blend: false,
background_color: pixfmt.default_background_color())
background_color: 0x0000_0000)
}
this.call_sequence = 0x40
@@ -357,11 +474,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
var status : base.status
while true {
if this.is_vp8_lossy {
status =? this.vp8.decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
} else {
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
}
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
@@ -370,17 +483,8 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
}
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var c8 : base.u8
var has_more : base.u32[..= 1]
var width : base.u32[..= 0x4000]
var dst : slice base.u8
var tile_data : roslice base.u8
var status : base.status
var pix : slice base.u8
var which : base.u32[..= 4]
var transform_type : base.u32[..= 3]
var ti : base.u64
var tj : base.u64
var r_mark : base.u64
var status : base.status
if this.call_sequence == 0x40 {
// No-op.
@@ -390,454 +494,106 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
return base."@end of data"
}
this.seen_transform[0] = false
this.seen_transform[1] = false
this.seen_transform[2] = false
this.seen_transform[3] = false
this.n_transforms = 0
while true {
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
while (this.variant & 0x03) == 0 {
this.sub_chunk_type = args.src.read_u32le?()
this.sub_chunk_length = args.src.read_u32le?()
this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0
if this.sub_chunk_length > 0x7FFF_FFFF {
return "#unsupported WebP file"
}
has_more = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
if has_more == 0 {
if this.sub_chunk_type == 'ICCP'le {
if this.variant > 0x04 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x20) == 0 {
return "#bad VP8X chunk"
}
this.variant = 0x08
} else if this.sub_chunk_type == 'ANIM'le {
if this.variant > 0x08 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x02) == 0 {
return "#bad VP8X chunk"
}
this.variant = 0x10
} else if this.sub_chunk_type == 'ANMF'le {
if this.variant > 0x10 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x02) == 0 {
return "#bad VP8X chunk"
}
this.variant = 0x20
} else if this.sub_chunk_type == 'ALPH'le {
if this.variant > 0x20 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x10) == 0 {
return "#bad VP8X chunk"
}
this.variant = 0x40
this.decode_alph?(src: args.src, workbuf: args.workbuf)
this.vp8.set_quirk!(
key: vp8.QUIRK_HAS_ALPHA_SIDE_CHANNEL,
value: 1)
} else if this.sub_chunk_type == 'VP8 'le {
this.vp8.set_quirk!(
key: base.QUIRK_SOURCE_LENGTH,
value: ((this.sub_chunk_length as base.u64) << 1) | 1)
this.variant = 0x81
break
} else if this.sub_chunk_type == 'VP8L'le {
if (this.vp8x_header_flags & 0x10) <> 0 {
return "#bad ALPH+VP8L combination"
}
this.variant = 0x82
break
} else if this.sub_chunk_type == 'XMP 'le {
if (this.vp8x_header_flags & 0x04) == 0 {
return "#bad VP8X chunk"
}
} else if this.sub_chunk_type == 'EXIF'le {
if (this.vp8x_header_flags & 0x08) == 0 {
return "#bad VP8X chunk"
}
}
this.decode_transform?(src: args.src, workbuf: args.workbuf)
}
width = this.width
if this.seen_transform[3] {
width = this.color_indexing_width
args.src.skip_u32?(n: this.sub_chunk_length)
if this.sub_chunk_has_padding {
args.src.skip_u32?(n: 1)
}
}
this.decode_color_cache_parameters?(src: args.src)
this.overall_color_cache_bits = this.color_cache_bits
this.decode_hg_table?(src: args.src, width: width, workbuf: args.workbuf)
this.color_cache_bits = this.overall_color_cache_bits
this.decode_huffman_groups?(src: args.src, n_huffman_groups: this.overall_n_huffman_groups)
while true {
if ((this.workbuf_offset_for_color_indexing as base.u64) > (this.workbuf_offset_for_transform[0] as base.u64)) or
((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) or
((this.workbuf_offset_for_transform[0] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
r_mark = args.src.mark()
if (this.variant & 0x01) <> 0 {
status =? this.vp8.decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
} else {
status =? this.do_decode_frame_vp8l?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
}
this.sub_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
dst = args.workbuf[
(this.workbuf_offset_for_color_indexing as base.u64) ..
(this.workbuf_offset_for_transform[0] as base.u64)]
tile_data = args.workbuf[
(this.workbuf_offset_for_transform[0] as base.u64) ..
(this.workbuf_offset_for_transform[1] as base.u64)]
status =? this.decode_pixels?(
dst: dst,
src: args.src,
width: width,
height: this.height,
tile_data: tile_data,
tile_size_log2: this.overall_tile_size_log2)
if status.is_ok() {
break
} else if not status.is_suspension() {
return status
} else if (status == base."$short read") and (this.sub_chunk_length == 0) {
return "#short chunk"
}
yield? status
}
if (this.workbuf_offset_for_transform[0] as base.u64) > args.workbuf.length() {
return base."#bad workbuf length"
}
pix = args.workbuf[.. (this.workbuf_offset_for_transform[0] as base.u64)]
which = this.n_transforms
while which > 0 {
which -= 1
transform_type = this.transform_type[which] as base.u32
tile_data = this.util.empty_slice_u8()
if transform_type < 2 {
ti = this.workbuf_offset_for_transform[transform_type + 1] as base.u64
tj = this.workbuf_offset_for_transform[transform_type + 2] as base.u64
if (ti <= tj) and (tj <= args.workbuf.length()) {
tile_data = args.workbuf[ti .. tj]
}
}
if transform_type == 0 {
this.apply_transform_predictor!(pix: pix, tile_data: tile_data)
} else if transform_type == 1 {
this.apply_transform_cross_color!(pix: pix, tile_data: tile_data)
} else if transform_type == 2 {
this.apply_transform_subtract_green!(pix: pix)
} else {
this.apply_transform_color_indexing!(pix: pix)
width = this.width
}
}
status = this.swizzle!(
dst: args.dst,
src: pix,
blend: args.blend)
if not status.is_ok() {
return status
}
this.call_sequence = 0x60
}
pri func decoder.decode_transform?(src: base.io_reader, workbuf: slice base.u8) {
var status : base.status
var c8 : base.u8
var transform_type : base.u32[..= 3]
var tile_size_log2 : base.u32[..= 9]
var p : slice base.u8
if this.n_bits < 2 {
c8 = args.src.read_u8?()
if this.n_bits >= 2 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 2
}
transform_type = this.bits & 3
this.bits >>= 2
this.n_bits -= 2
if this.seen_transform[transform_type] or (this.n_transforms >= 4) {
return "#bad transform"
} else if this.seen_transform[3] {
return "#unsupported transform after color indexing transform"
}
this.seen_transform[transform_type] = true
this.transform_type[this.n_transforms] = transform_type as base.u8
this.n_transforms += 1
if transform_type < 2 { // Predictor, Cross Color transform.
if this.n_bits < 3 {
c8 = args.src.read_u8?()
if this.n_bits >= 3 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 3
}
tile_size_log2 = (this.bits & 7) + 2
this.transform_tile_size_log2[transform_type] = tile_size_log2 as base.u8
this.bits >>= 3
this.n_bits -= 3
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
while true,
inv transform_type < 2,
inv tile_size_log2 >= 2,
{
if ((this.workbuf_offset_for_transform[transform_type + 1] as base.u64) > (this.workbuf_offset_for_transform[transform_type + 2] as base.u64)) or
((this.workbuf_offset_for_transform[transform_type + 2] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
status =? this.decode_pixels?(
dst: args.workbuf[
(this.workbuf_offset_for_transform[transform_type + 1] as base.u64) ..
(this.workbuf_offset_for_transform[transform_type + 2] as base.u64)],
src: args.src,
width: (this.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
height: (this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
if status.is_ok() {
break
}
yield? status
}
} else if transform_type == 2 { // Subtract Green transform.
// No-op.
} else { // Color Indexing transform.
if this.n_bits < 8 {
c8 = args.src.read_u8?()
if this.n_bits >= 8 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 8
}
this.color_indexing_palette_size = (this.bits & 0xFF) + 1
this.bits >>= 8
this.n_bits -= 8
if this.color_indexing_palette_size <= 2 {
this.color_indexing_width = (this.width + 7) / 8
this.transform_tile_size_log2[3] = 3
} else if this.color_indexing_palette_size <= 4 {
this.color_indexing_width = (this.width + 3) / 4
this.transform_tile_size_log2[3] = 2
} else if this.color_indexing_palette_size <= 16 {
this.color_indexing_width = (this.width + 1) / 2
this.transform_tile_size_log2[3] = 1
} else {
this.color_indexing_width = this.width
this.transform_tile_size_log2[3] = 0
}
if this.width >= this.color_indexing_width {
this.workbuf_offset_for_color_indexing = 4 * (this.width - this.color_indexing_width) * this.height
}
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
this.decode_pixels?(
dst: this.palette[.. 4 * this.color_indexing_palette_size],
src: args.src,
width: this.color_indexing_palette_size,
height: 1,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
this.palette[4 * this.color_indexing_palette_size .. 1024].bulk_memset!(byte_value: 0)
p = this.palette[.. 4 * this.color_indexing_palette_size]
while p.length() >= 8 {
p[4] ~mod+= p[0]
p[5] ~mod+= p[1]
p[6] ~mod+= p[2]
p[7] ~mod+= p[3]
p = p[4 ..]
}
}
}
pri func decoder.decode_color_cache_parameters?(src: base.io_reader) {
var c8 : base.u8
var use_color_cache : base.u32[..= 1]
var color_cache_bits : base.u32[..= 15]
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
use_color_cache = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
this.color_cache_bits = 0
if use_color_cache <> 0 {
if this.n_bits < 4 {
c8 = args.src.read_u8?()
if this.n_bits >= 4 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 4
}
color_cache_bits = this.bits & 15
this.bits >>= 4
this.n_bits -= 4
if (color_cache_bits < 1) or (11 < color_cache_bits) {
return "#bad color cache"
}
this.color_cache_bits = color_cache_bits
}
}
pri func decoder.decode_hg_table?(src: base.io_reader, width: base.u32[..= 0x4000], workbuf: slice base.u8) {
var status : base.status
var c8 : base.u8
var use_hg_table : base.u32[..= 1]
var tile_size_log2 : base.u32[..= 9]
var hg_pixels : slice base.u8
var n : base.u64
var p : roslice base.u8
var hg_plus_1 : base.u32[..= 256]
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
use_hg_table = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
if use_hg_table == 0 {
this.overall_n_huffman_groups = 1
this.overall_tile_size_log2 = 0
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
hg_pixels = args.workbuf[
this.workbuf_offset_for_transform[0] as base.u64 ..
this.workbuf_offset_for_transform[1] as base.u64]
if hg_pixels.length() >= 4 {
hg_pixels[0] = 0x00
hg_pixels[1] = 0x00
hg_pixels[2] = 0x00
hg_pixels[3] = 0x00
}
return ok
}
if this.n_bits < 3 {
c8 = args.src.read_u8?()
if this.n_bits >= 3 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 3
}
tile_size_log2 = (this.bits & 7) + 2
this.bits >>= 3
this.n_bits -= 3
this.overall_tile_size_log2 = tile_size_log2
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
while true,
inv tile_size_log2 >= 2,
{
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
status =? this.decode_pixels?(
dst: args.workbuf[
(this.workbuf_offset_for_transform[0] as base.u64) ..
(this.workbuf_offset_for_transform[1] as base.u64)],
src: args.src,
width: (args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
height: (this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
if status.is_ok() {
break
}
yield? status
}
this.overall_n_huffman_groups = 1
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
hg_pixels = args.workbuf[
this.workbuf_offset_for_transform[0] as base.u64 ..
this.workbuf_offset_for_transform[1] as base.u64]
n = (((args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2) *
((this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2) *
4) as base.u64
if n > hg_pixels.length() {
return base."#bad workbuf length"
}
p = hg_pixels[.. n]
while p.length() >= 4 {
if p[2] <> 0 {
return "#unsupported number of Huffman groups"
}
hg_plus_1 = (p[1] as base.u32) + 1
if this.overall_n_huffman_groups < hg_plus_1 {
this.overall_n_huffman_groups = hg_plus_1
}
p = p[4 ..]
}
}
pri func decoder.decode_pixels?(dst: slice base.u8, src: base.io_reader, width: base.u32[..= 0x4000], height: base.u32[..= 0x4000], tile_data: roslice base.u8, tile_size_log2: base.u32[..= 9]) {
var i : base.u32
var n : base.u32[..= 2048]
i = 0
n = (1 as base.u32) << this.color_cache_bits
while i < n {
assert i < 2048 via "a < b: a < c; c <= b"(c: n)
this.color_cache[i] = 0
i += 1
}
this.decode_pixels_slow?(
dst: args.dst,
src: args.src,
width: args.width,
height: args.height,
tile_data: args.tile_data,
tile_size_log2: args.tile_size_log2)
}
pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status {
var status : base.status
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u32[..= 32]
var dst_bytes_per_row : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
var src_bytes_per_row : base.u64
var dst : slice base.u8
var y : base.u32
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette_or_else(fallback: this.palette[..]),
src_pixfmt: this.util.make_pixel_format(repr: this.pixfmt),
src_palette: this.util.empty_slice_u8(),
blend: args.blend)
if not status.is_ok() {
return status
}
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = dst_bits_per_pixel / 8
dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64
dst_palette = args.dst.palette_or_else(fallback: this.palette[..])
tab = args.dst.plane(p: 0)
src_bytes_per_row = (this.width * 4) as base.u64
while src_bytes_per_row <= args.src.length() {
dst = tab.row_u32(y: y)
if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row]
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: dst_palette,
src: args.src[.. src_bytes_per_row])
args.src = args.src[src_bytes_per_row ..]
y ~mod+= 1
}
return ok
}
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
if this.is_vp8_lossy {
if this.variant == 0x01 {
return this.vp8.frame_dirty_rect()
}
return this.util.make_rect_ie_u32(
@@ -852,7 +608,7 @@ pub func decoder.num_animation_loops() base.u32 {
}
pub func decoder.num_decoded_frame_configs() base.u64 {
if this.is_vp8_lossy {
if this.variant == 0x01 {
return this.vp8.num_decoded_frame_configs()
}
if this.call_sequence > 0x20 {
@@ -862,7 +618,7 @@ pub func decoder.num_decoded_frame_configs() base.u64 {
}
pub func decoder.num_decoded_frames() base.u64 {
if this.is_vp8_lossy {
if this.variant == 0x01 {
return this.vp8.num_decoded_frames()
}
if this.call_sequence > 0x40 {
@@ -874,7 +630,7 @@ pub func decoder.num_decoded_frames() base.u64 {
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
var status : base.status
if this.is_vp8_lossy {
if this.variant == 0x01 {
status = this.vp8.restart_frame!(index: args.index, io_position: args.io_position)
return status
}
@@ -897,12 +653,30 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
}
pub func decoder.workbuf_len() base.range_ii_u64 {
if this.is_vp8_lossy {
var r : base.range_ii_u64
var s : base.range_ii_u64
if this.variant == 0x01 {
return this.vp8.workbuf_len()
}
return this.util.make_range_ii_u64(
r = this.util.make_range_ii_u64(
min_incl: this.workbuf_offset_for_transform[3] as base.u64,
max_incl: this.workbuf_offset_for_transform[3] as base.u64)
if this.variant <> 0x02 {
s = this.vp8.workbuf_len()
if s.is_empty() {
// Empty means that the workbuf_len range isn't known yet.
return s
}
r = this.util.make_range_ii_u64(
min_incl: s.get_min_incl().max(no_less_than: r.get_min_incl()),
max_incl: s.get_max_incl().max(no_less_than: r.get_max_incl()))
}
return r
}
pri const DISTANCE_MAP : roarray[120] base.u8 = [
+27 -3
View File
@@ -30,6 +30,7 @@ OK. d9f8441c test/3pdata/blinksuite/blue-10.png
OK. 267741e0 test/3pdata/blinksuite/blue-100.png
OK. 89449c01 test/3pdata/blinksuite/blue-wheel-srgb-color-profile.jpg
OK. 8fce70a9 test/3pdata/blinksuite/blue-wheel-srgb-color-profile.png
OK. 426a5623 test/3pdata/blinksuite/blue-wheel-srgb-color-profile.webp
OK. 2d7bad9f test/3pdata/blinksuite/border-image-srgb-color-profile.png
OK. 9ca73f21 test/3pdata/blinksuite/border-image.png
OK. 277600f9 test/3pdata/blinksuite/boston.gif
@@ -42,6 +43,8 @@ OK. 5ec78d7c test/3pdata/blinksuite/color-checker-adobe-color-profile.png
OK. b3389858 test/3pdata/blinksuite/color-checker-srgb-color-profile.png
OK. 10721531 test/3pdata/blinksuite/count-down-color-test.gif
OK. 1abc4dc6 test/3pdata/blinksuite/count-down-color-test.png
BAD d663b0e6 test/3pdata/blinksuite/count-down-color-test.webp
OK. 18fbe8ca test/3pdata/blinksuite/crbug.364830.webp
OK. 24ab1efd test/3pdata/blinksuite/crbug779261.gif
OK. ae034abe test/3pdata/blinksuite/crbug807324.png
OK. 4f50fdb0 test/3pdata/blinksuite/crbug827754.png
@@ -66,6 +69,7 @@ OK. d97ce4e8 test/3pdata/blinksuite/cs-uma-ycbcr-other.jpg
OK. a2a940bc test/3pdata/blinksuite/cs-uma-ycck.jpg
OK. b6000be6 test/3pdata/blinksuite/dice.png
BAD 1023c20a test/3pdata/blinksuite/empty-frame.png
OK. 4a495d9f test/3pdata/blinksuite/flip.webp
OK. eff36075 test/3pdata/blinksuite/flowchart.jpg
OK. 20ff1df8 test/3pdata/blinksuite/full2loop.gif
OK. dc814e29 test/3pdata/blinksuite/gif-loop-count.gif
@@ -89,9 +93,14 @@ OK. 95641cb9 test/3pdata/blinksuite/icc-v2-gbr-420-height-not-whole-mcu.jpg
OK. 93f21c2a test/3pdata/blinksuite/icc-v2-gbr-420-width-not-whole-mcu.jpg
OK. f37cea44 test/3pdata/blinksuite/icc-v2-gbr-422-whole-mcus.jpg
OK. 1de5a906 test/3pdata/blinksuite/icc-v2-gbr.jpg
BAD 7c80a001 test/3pdata/blinksuite/invalid-animated-webp.webp
BAD 1e37d000 test/3pdata/blinksuite/invalid-animated-webp2.webp
BAD 97b2b706 test/3pdata/blinksuite/invalid-animated-webp3.webp
BAD 145962c5 test/3pdata/blinksuite/invalid-animated-webp4.webp
OK. 2ba1fc10 test/3pdata/blinksuite/invalid_vp8_vp8x.webp
OK. 0da5f37e test/3pdata/blinksuite/jpeg-height-exif-orientation.jpg
OK. 4409dca6 test/3pdata/blinksuite/large-gif-checkerboard.gif
OK. 8f798e8b test/3pdata/blinksuite/large.webp
OK. d67648b6 test/3pdata/blinksuite/large.webp
BAD 2f310e57 test/3pdata/blinksuite/missing-eoi.jpg
OK. 72bbe2b6 test/3pdata/blinksuite/motion-jpeg-single-frame.jpg
OK. 0268284a test/3pdata/blinksuite/mu.png
@@ -170,10 +179,25 @@ OK. 9a7e5ec7 test/3pdata/blinksuite/sprite_alternate_fragments_same.png
OK. 1b1f5f96 test/3pdata/blinksuite/stripes-large.png
OK. 6b85cdb9 test/3pdata/blinksuite/stripes-small.png
OK. 16c2c0fe test/3pdata/blinksuite/test-load.jpg
OK. 631f6a02 test/3pdata/blinksuite/test.webp
OK. 6fbda804 test/3pdata/blinksuite/test.webp
OK. eb16e76c test/3pdata/blinksuite/test2.webp
OK. 19d3a207 test/3pdata/blinksuite/test3.webp
OK. 56224156 test/3pdata/blinksuite/truncated.webp
BAD 1dfa35c6 test/3pdata/blinksuite/truncated2.webp
OK. 603ddc49 test/3pdata/blinksuite/webp-color-no-profile-lossy.webp
BAD b16eb1a2 test/3pdata/blinksuite/webp-animated-icc-xmp.webp
BAD 45030792 test/3pdata/blinksuite/webp-animated-large.webp
BAD 4519c83d test/3pdata/blinksuite/webp-animated-no-blend.webp
BAD 4519c83d test/3pdata/blinksuite/webp-animated-opaque.webp
BAD d26b2bd9 test/3pdata/blinksuite/webp-animated-semitransparent1.webp
BAD 73e0b22f test/3pdata/blinksuite/webp-animated-semitransparent2.webp
BAD d26b2bd9 test/3pdata/blinksuite/webp-animated-semitransparent3.webp
BAD 0dba213a test/3pdata/blinksuite/webp-animated-semitransparent4.webp
BAD ae7f1fcf test/3pdata/blinksuite/webp-animated.webp
OK. eb191e45 test/3pdata/blinksuite/webp-color-no-profile-lossy.webp
OK. 60116c2c test/3pdata/blinksuite/webp-color-profile-crash.webp
BAD f234aa8a test/3pdata/blinksuite/webp-color-profile-lossless.webp
OK. db8c197f test/3pdata/blinksuite/webp-color-profile-lossy-alpha.webp
OK. 684e3635 test/3pdata/blinksuite/webp-color-profile-lossy.webp
BAD ed67a9a5 test/3pdata/blinksuite/wrong-block-length.gif
OK. 577c5c4c test/3pdata/blinksuite/ycbcr-420-fast-int-progressive.jpg
OK. bf4cd8e2 test/3pdata/blinksuite/ycbcr-420-float-progressive.jpg
+95 -77
View File
@@ -1,9 +1,23 @@
# Generated by script/print-nia-checksums.sh
OK. ca4c0038 test/3pdata/webpsuite/bad_palette_index.webp
OK. 3557c3f5 test/3pdata/webpsuite/bryce.webp
OK. a07d001c test/3pdata/webpsuite/bug3.webp
OK. a92322d0 test/3pdata/webpsuite/alpha_color_cache.webp
OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_0_method_0.webp
OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_0_method_1.webp
OK. d955c04f test/3pdata/webpsuite/alpha_filter_1.webp
OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_1_method_0.webp
OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_1_method_1.webp
OK. d955c04f test/3pdata/webpsuite/alpha_filter_2.webp
OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_2_method_0.webp
OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_2_method_1.webp
OK. d955c04f test/3pdata/webpsuite/alpha_filter_3.webp
OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_3_method_0.webp
OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_3_method_1.webp
OK. d955c04f test/3pdata/webpsuite/alpha_no_compression.webp
OK. 0ecfd841 test/3pdata/webpsuite/bad_palette_index.webp
OK. 79f9ca4a test/3pdata/webpsuite/big_endian_bug_393.webp
OK. 3dd065ab test/3pdata/webpsuite/bryce.webp
OK. 7794c8b2 test/3pdata/webpsuite/bug3.webp
OK. 6fbda804 test/3pdata/webpsuite/color_cache_bits_11.webp
BAD d338aed8 test/3pdata/webpsuite/dual_transform.webp
OK. 0fecb95b test/3pdata/webpsuite/dual_transform.webp
OK. f79224e7 test/3pdata/webpsuite/grid.bmp
OK. 33b7a1f3 test/3pdata/webpsuite/grid.pgm
OK. f79224e7 test/3pdata/webpsuite/grid.png
@@ -20,106 +34,110 @@ OK. 289af407 test/3pdata/webpsuite/lossless_color_transform.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_0.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_1.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_10.webp
BAD df11692d test/3pdata/webpsuite/lossless_vec_1_11.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_11.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_12.webp
BAD df11692d test/3pdata/webpsuite/lossless_vec_1_13.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_13.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_14.webp
BAD df11692d test/3pdata/webpsuite/lossless_vec_1_15.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_15.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_2.webp
BAD df11692d test/3pdata/webpsuite/lossless_vec_1_3.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_3.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_4.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_5.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_6.webp
BAD df11692d test/3pdata/webpsuite/lossless_vec_1_7.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_7.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_8.webp
BAD df11692d test/3pdata/webpsuite/lossless_vec_1_9.webp
OK. f79224e7 test/3pdata/webpsuite/lossless_vec_1_9.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_0.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_1.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_10.webp
BAD 79781cfb test/3pdata/webpsuite/lossless_vec_2_11.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_11.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_12.webp
BAD 79781cfb test/3pdata/webpsuite/lossless_vec_2_13.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_13.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_14.webp
BAD 79781cfb test/3pdata/webpsuite/lossless_vec_2_15.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_15.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_2.webp
BAD 79781cfb test/3pdata/webpsuite/lossless_vec_2_3.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_3.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_4.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_5.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_6.webp
BAD 79781cfb test/3pdata/webpsuite/lossless_vec_2_7.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_7.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_8.webp
BAD 79781cfb test/3pdata/webpsuite/lossless_vec_2_9.webp
OK. 3a23e407 test/3pdata/webpsuite/lossy_extreme_probabilities.webp
OK. f6f31bd1 test/3pdata/webpsuite/lossy_q0_f100.webp
OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_9.webp
OK. a62056d3 test/3pdata/webpsuite/lossy_alpha1.webp
OK. 7d50b493 test/3pdata/webpsuite/lossy_alpha2.webp
OK. 3a84e899 test/3pdata/webpsuite/lossy_alpha3.webp
OK. a9d10d15 test/3pdata/webpsuite/lossy_alpha4.webp
OK. ac62b1e6 test/3pdata/webpsuite/lossy_extreme_probabilities.webp
OK. 84ff1fbf test/3pdata/webpsuite/lossy_q0_f100.webp
OK. 6957defa test/3pdata/webpsuite/near_lossless_75.webp
OK. 7c80a001 test/3pdata/webpsuite/one_color_no_palette.webp
OK. 6706e719 test/3pdata/webpsuite/peak.bmp
OK. 315bbd77 test/3pdata/webpsuite/peak.pgm
OK. 6706e719 test/3pdata/webpsuite/peak.png
OK. 6706e719 test/3pdata/webpsuite/peak.ppm
OK. e58f2013 test/3pdata/webpsuite/segment01.webp
OK. 931e5bb2 test/3pdata/webpsuite/segment02.webp
OK. c42afa6d test/3pdata/webpsuite/segment03.webp
OK. 6d2a2c1f test/3pdata/webpsuite/segment01.webp
OK. b7705924 test/3pdata/webpsuite/segment02.webp
OK. 98115d46 test/3pdata/webpsuite/segment03.webp
OK. 9fba8052 test/3pdata/webpsuite/small_13x1.webp
OK. f08db259 test/3pdata/webpsuite/small_1x1.webp
OK. ae403f6e test/3pdata/webpsuite/small_1x13.webp
OK. 70c37cb6 test/3pdata/webpsuite/small_31x13.webp
OK. 80c1ff42 test/3pdata/webpsuite/test-nostrong.webp
OK. 631f6a02 test/3pdata/webpsuite/test.webp
OK. fc06d690 test/3pdata/webpsuite/small_31x13.webp
OK. 706a6c13 test/3pdata/webpsuite/test-nostrong.webp
OK. 6fbda804 test/3pdata/webpsuite/test.webp
OK. b1e2e316 test/3pdata/webpsuite/very_short.webp
OK. ff7d9585 test/3pdata/webpsuite/vp80-00-comprehensive-001.webp
OK. 684b846a test/3pdata/webpsuite/vp80-00-comprehensive-002.webp
OK. d3127a10 test/3pdata/webpsuite/vp80-00-comprehensive-003.webp
OK. ff7d9585 test/3pdata/webpsuite/vp80-00-comprehensive-004.webp
OK. c5600ecc test/3pdata/webpsuite/vp80-00-comprehensive-005.webp
OK. 3836d469 test/3pdata/webpsuite/vp80-00-comprehensive-006.webp
OK. 04f7f4c9 test/3pdata/webpsuite/vp80-00-comprehensive-007.webp
OK. e06af858 test/3pdata/webpsuite/vp80-00-comprehensive-008.webp
OK. 9458b100 test/3pdata/webpsuite/vp80-00-comprehensive-009.webp
OK. 954647a5 test/3pdata/webpsuite/vp80-00-comprehensive-010.webp
OK. ff7d9585 test/3pdata/webpsuite/vp80-00-comprehensive-011.webp
OK. 6732c101 test/3pdata/webpsuite/vp80-00-comprehensive-012.webp
OK. df21b5c8 test/3pdata/webpsuite/vp80-00-comprehensive-013.webp
OK. de3154f1 test/3pdata/webpsuite/vp80-00-comprehensive-014.webp
OK. 6291f264 test/3pdata/webpsuite/vp80-00-comprehensive-015.webp
OK. 37bf6310 test/3pdata/webpsuite/vp80-00-comprehensive-001.webp
OK. d87c0135 test/3pdata/webpsuite/vp80-00-comprehensive-002.webp
OK. 53f2a4c8 test/3pdata/webpsuite/vp80-00-comprehensive-003.webp
OK. 37bf6310 test/3pdata/webpsuite/vp80-00-comprehensive-004.webp
OK. cc470087 test/3pdata/webpsuite/vp80-00-comprehensive-005.webp
OK. 22f2bc85 test/3pdata/webpsuite/vp80-00-comprehensive-006.webp
OK. 861d5c90 test/3pdata/webpsuite/vp80-00-comprehensive-007.webp
OK. ec6e76ae test/3pdata/webpsuite/vp80-00-comprehensive-008.webp
OK. 24899b34 test/3pdata/webpsuite/vp80-00-comprehensive-009.webp
OK. b265598f test/3pdata/webpsuite/vp80-00-comprehensive-010.webp
OK. 37bf6310 test/3pdata/webpsuite/vp80-00-comprehensive-011.webp
OK. dafaf33d test/3pdata/webpsuite/vp80-00-comprehensive-012.webp
OK. ca91f611 test/3pdata/webpsuite/vp80-00-comprehensive-013.webp
OK. 11382a93 test/3pdata/webpsuite/vp80-00-comprehensive-014.webp
OK. 85b7887a test/3pdata/webpsuite/vp80-00-comprehensive-015.webp
OK. 3346ef2b test/3pdata/webpsuite/vp80-00-comprehensive-016.webp
OK. 3346ef2b test/3pdata/webpsuite/vp80-00-comprehensive-017.webp
OK. 34a0adbb test/3pdata/webpsuite/vp80-01-intra-1400.webp
OK. 2cda6f62 test/3pdata/webpsuite/vp80-01-intra-1411.webp
OK. a321721b test/3pdata/webpsuite/vp80-01-intra-1416.webp
OK. 754a4ff8 test/3pdata/webpsuite/vp80-01-intra-1417.webp
OK. 34a0adbb test/3pdata/webpsuite/vp80-02-inter-1402.webp
OK. 2cda6f62 test/3pdata/webpsuite/vp80-02-inter-1412.webp
OK. 0bb72bca test/3pdata/webpsuite/vp80-02-inter-1418.webp
OK. 93739a20 test/3pdata/webpsuite/vp80-02-inter-1424.webp
OK. 34a0adbb test/3pdata/webpsuite/vp80-03-segmentation-1401.webp
OK. 34a0adbb test/3pdata/webpsuite/vp80-03-segmentation-1403.webp
OK. aaea276b test/3pdata/webpsuite/vp80-03-segmentation-1407.webp
OK. aaea276b test/3pdata/webpsuite/vp80-03-segmentation-1408.webp
OK. aaea276b test/3pdata/webpsuite/vp80-03-segmentation-1409.webp
OK. aaea276b test/3pdata/webpsuite/vp80-03-segmentation-1410.webp
OK. 2cda6f62 test/3pdata/webpsuite/vp80-03-segmentation-1413.webp
OK. 65fb3f68 test/3pdata/webpsuite/vp80-03-segmentation-1414.webp
OK. 65fb3f68 test/3pdata/webpsuite/vp80-03-segmentation-1415.webp
OK. 3819b4d7 test/3pdata/webpsuite/vp80-03-segmentation-1425.webp
OK. 981e1d2e test/3pdata/webpsuite/vp80-03-segmentation-1426.webp
OK. 57780055 test/3pdata/webpsuite/vp80-03-segmentation-1427.webp
OK. caf44365 test/3pdata/webpsuite/vp80-03-segmentation-1432.webp
OK. 17d6af96 test/3pdata/webpsuite/vp80-03-segmentation-1435.webp
OK. 792cc4dc test/3pdata/webpsuite/vp80-03-segmentation-1436.webp
OK. a0c109d5 test/3pdata/webpsuite/vp80-03-segmentation-1437.webp
OK. 88f3ed22 test/3pdata/webpsuite/vp80-03-segmentation-1441.webp
OK. ad7b1ced test/3pdata/webpsuite/vp80-03-segmentation-1442.webp
OK. 34a0adbb test/3pdata/webpsuite/vp80-04-partitions-1404.webp
OK. 34a0adbb test/3pdata/webpsuite/vp80-04-partitions-1405.webp
OK. 34a0adbb test/3pdata/webpsuite/vp80-04-partitions-1406.webp
OK. deb943fc test/3pdata/webpsuite/vp80-05-sharpness-1428.webp
OK. eaffae2b test/3pdata/webpsuite/vp80-05-sharpness-1429.webp
OK. 4442b90a test/3pdata/webpsuite/vp80-01-intra-1400.webp
OK. 20ba1cc0 test/3pdata/webpsuite/vp80-01-intra-1411.webp
OK. 6dafc49a test/3pdata/webpsuite/vp80-01-intra-1416.webp
OK. bfd3e3e3 test/3pdata/webpsuite/vp80-01-intra-1417.webp
OK. 4442b90a test/3pdata/webpsuite/vp80-02-inter-1402.webp
OK. 20ba1cc0 test/3pdata/webpsuite/vp80-02-inter-1412.webp
OK. 18734e4a test/3pdata/webpsuite/vp80-02-inter-1418.webp
OK. de941f26 test/3pdata/webpsuite/vp80-02-inter-1424.webp
OK. 4442b90a test/3pdata/webpsuite/vp80-03-segmentation-1401.webp
OK. 4442b90a test/3pdata/webpsuite/vp80-03-segmentation-1403.webp
OK. 29b4a1ef test/3pdata/webpsuite/vp80-03-segmentation-1407.webp
OK. 29b4a1ef test/3pdata/webpsuite/vp80-03-segmentation-1408.webp
OK. 29b4a1ef test/3pdata/webpsuite/vp80-03-segmentation-1409.webp
OK. 29b4a1ef test/3pdata/webpsuite/vp80-03-segmentation-1410.webp
OK. 20ba1cc0 test/3pdata/webpsuite/vp80-03-segmentation-1413.webp
OK. 8463f4cd test/3pdata/webpsuite/vp80-03-segmentation-1414.webp
OK. 8463f4cd test/3pdata/webpsuite/vp80-03-segmentation-1415.webp
OK. 002fde0c test/3pdata/webpsuite/vp80-03-segmentation-1425.webp
OK. 57573719 test/3pdata/webpsuite/vp80-03-segmentation-1426.webp
OK. 13241231 test/3pdata/webpsuite/vp80-03-segmentation-1427.webp
OK. dab26697 test/3pdata/webpsuite/vp80-03-segmentation-1432.webp
OK. c3ef4215 test/3pdata/webpsuite/vp80-03-segmentation-1435.webp
OK. c5120084 test/3pdata/webpsuite/vp80-03-segmentation-1436.webp
OK. bcc51ad6 test/3pdata/webpsuite/vp80-03-segmentation-1437.webp
OK. f1372752 test/3pdata/webpsuite/vp80-03-segmentation-1441.webp
OK. cf14a320 test/3pdata/webpsuite/vp80-03-segmentation-1442.webp
OK. 4442b90a test/3pdata/webpsuite/vp80-04-partitions-1404.webp
OK. 4442b90a test/3pdata/webpsuite/vp80-04-partitions-1405.webp
OK. 4442b90a test/3pdata/webpsuite/vp80-04-partitions-1406.webp
OK. d6068a35 test/3pdata/webpsuite/vp80-05-sharpness-1428.webp
OK. 84c592b7 test/3pdata/webpsuite/vp80-05-sharpness-1429.webp
OK. 88ca32c7 test/3pdata/webpsuite/vp80-05-sharpness-1430.webp
OK. 386ff4b2 test/3pdata/webpsuite/vp80-05-sharpness-1431.webp
OK. 792cc4dc test/3pdata/webpsuite/vp80-05-sharpness-1433.webp
OK. 8220ff9f test/3pdata/webpsuite/vp80-05-sharpness-1434.webp
OK. 2b2a6b87 test/3pdata/webpsuite/vp80-05-sharpness-1438.webp
OK. 7342903f test/3pdata/webpsuite/vp80-05-sharpness-1439.webp
OK. 792cc4dc test/3pdata/webpsuite/vp80-05-sharpness-1440.webp
OK. a5c713d9 test/3pdata/webpsuite/vp80-05-sharpness-1443.webp
OK. bfa52a04 test/3pdata/webpsuite/vp80-05-sharpness-1431.webp
OK. c5120084 test/3pdata/webpsuite/vp80-05-sharpness-1433.webp
OK. 5941d626 test/3pdata/webpsuite/vp80-05-sharpness-1434.webp
OK. 2e9ca716 test/3pdata/webpsuite/vp80-05-sharpness-1438.webp
OK. 85784028 test/3pdata/webpsuite/vp80-05-sharpness-1439.webp
OK. c5120084 test/3pdata/webpsuite/vp80-05-sharpness-1440.webp
OK. 5e488473 test/3pdata/webpsuite/vp80-05-sharpness-1443.webp
+3
View File
@@ -8,6 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
#include <limits.h>
#include <stdint.h>
#include "bzlib.h"
const char* //
+3
View File
@@ -19,6 +19,9 @@
// #define WUFFS_MIMICLIB_USE_LIBDEFLATE_INSTEAD_OF_ZLIB 1
// #define WUFFS_MIMICLIB_USE_MINIZ_INSTEAD_OF_ZLIB 1
#include <limits.h>
#include <stdint.h>
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_ZLIB
#if defined(WUFFS_MIMICLIB_USE_LIBDEFLATE_INSTEAD_OF_ZLIB)
#include "libdeflate.h"
+3
View File
@@ -8,6 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
#include <limits.h>
#include <stdint.h>
#include "gif_lib.h"
int //
+3
View File
@@ -18,6 +18,9 @@
//
// #define WUFFS_MIMICLIB_USE_STB_IMAGE_INSTEAD_OF_LIBJPEG 1
#include <limits.h>
#include <stdint.h>
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBJPEG
#if defined(WUFFS_MIMICLIB_USE_STB_IMAGE_INSTEAD_OF_LIBJPEG)
+3
View File
@@ -8,6 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
#include <limits.h>
#include <stdint.h>
#include "lzma.h"
const char* //
+3
View File
@@ -8,6 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
#include <limits.h>
#include <stdint.h>
#include "openssl/evp.h"
const char* //
+3
View File
@@ -20,6 +20,9 @@
// #define WUFFS_MIMICLIB_USE_LODEPNG_INSTEAD_OF_LIBPNG 1
// #define WUFFS_MIMICLIB_USE_STB_IMAGE_INSTEAD_OF_LIBPNG 1
#include <limits.h>
#include <stdint.h>
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG
#if defined(WUFFS_MIMICLIB_USE_LIBSPNG_INSTEAD_OF_LIBPNG)
+3
View File
@@ -8,6 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
#include <limits.h>
#include <stdint.h>
// We #include a foo.cpp file, not a foo.h file, as stb_image is a "single file
// C library".
//
+3 -1
View File
@@ -8,6 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
#include <limits.h>
#include <stdint.h>
#include "webp/decode.h"
static void //
@@ -74,7 +77,6 @@ mimic_webp_decode(uint64_t* n_bytes_out,
return "mimic_webp_decode: unsupported pixfmt";
}
config.options.no_fancy_upsampling = 1; // TODO: remove for best mimicry.
config.output.colorspace = MODE_BGRA;
config.output.width = w;
config.output.height = h;
+3
View File
@@ -8,6 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
#include <limits.h>
#include <stdint.h>
#include "xxhash.h"
uint32_t global_mimiclib_xxhash32_unused_u32;
+5 -1
View File
@@ -126,7 +126,11 @@ test_wuffs_jpeg_decode_lower_quality() {
&dec, WUFFS_BASE__QUIRK_QUALITY,
WUFFS_BASE__QUIRK_QUALITY__VALUE__LOWER_QUALITY);
}
uint64_t m = wuffs_jpeg__decoder__workbuf_len(&dec).min_incl;
wuffs_base__range_ii_u64 r = wuffs_jpeg__decoder__workbuf_len(&dec);
if (wuffs_base__range_ii_u64__is_empty(&r)) {
RETURN_FAIL("q=%d: indeterminate workbuf length", q);
}
uint64_t m = r.min_incl;
if ((q == 0) && (m == 0)) {
RETURN_FAIL("q=%d: have %" PRIu64 ", want non-zero", q, m);
} else if ((q != 0) && (m != 0)) {
+11 -10
View File
@@ -261,9 +261,9 @@ test_wuffs_pixel_buffer_fill_rect() {
// Reset to transparent black (or its closest approximation).
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
CHECK_STATUS(
"set_color_u32_at",
wuffs_base__pixel_buffer__set_color_u32_at(&dst_pixbuf, x, y, 0));
CHECK_STATUS("set_color_u32_argb_premul_at",
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
&dst_pixbuf, x, y, 0));
}
}
@@ -275,9 +275,10 @@ test_wuffs_pixel_buffer_fill_rect() {
1 + (height / 2))
: wuffs_base__make_rect_ie_u32(width / 2, 0,
1 + (width / 2), height);
CHECK_STATUS("set_color_u32_fill_rect",
wuffs_base__pixel_buffer__set_color_u32_fill_rect(
&dst_pixbuf, rect, dsts[d].color));
CHECK_STATUS(
"set_color_u32_argb_premul_fill_rect",
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
&dst_pixbuf, rect, dsts[d].color));
// Check the middle dst pixel.
wuffs_base__color_u32_argb_premul want_dst_pixel = dsts[d].color;
@@ -447,8 +448,8 @@ test_wuffs_pixel_swizzler_swizzle() {
}
// Set and check the middle src pixel.
CHECK_STATUS("set_color_u32_at",
wuffs_base__pixel_buffer__set_color_u32_at(
CHECK_STATUS("set_color_u32_argb_premul_at",
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
&src_pixbuf, width / 2, height / 2, srcs[s].color));
wuffs_base__color_u32_argb_premul have_src_pixel =
wuffs_base__pixel_buffer__color_u32_at(&src_pixbuf, width / 2,
@@ -483,8 +484,8 @@ test_wuffs_pixel_swizzler_swizzle() {
for (size_t b = 0; b < WUFFS_TESTLIB_ARRAY_SIZE(blends); b++) {
// Set the middle dst pixel.
CHECK_STATUS("set_color_u32_at",
wuffs_base__pixel_buffer__set_color_u32_at(
CHECK_STATUS("set_color_u32_argb_premul_at",
wuffs_base__pixel_buffer__set_color_u32_argb_premul_at(
&dst_pixbuf, width / 2, height / 2, dsts[d].color));
// Swizzle.
+204 -2
View File
@@ -114,6 +114,20 @@ test_wuffs_webp_decode_interface_lossy() {
"test/data/bricks-color.lossy.webp", 0, SIZE_MAX, 160, 120, 0xFF032665);
}
const char* //
test_wuffs_webp_decode_interface_vp8x_alpha_lossy() {
CHECK_FOCUS(__func__);
wuffs_webp__decoder* dec = &g_webp_decoder;
CHECK_STATUS("initialize",
wuffs_webp__decoder__initialize(
dec, sizeof *dec, WUFFS_VERSION,
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED));
return do_test__wuffs_base__image_decoder(
wuffs_webp__decoder__upcast_as__wuffs_base__image_decoder(dec),
"test/data/hippopotamus.masked-with-muybridge.lossy.webp", 0, SIZE_MAX,
36, 28, 0x7D787878);
}
const char* //
test_wuffs_webp_decode_many_small_reads() {
CHECK_FOCUS(__func__);
@@ -159,10 +173,14 @@ test_wuffs_webp_decode_many_small_reads() {
wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){});
CHECK_STATUS("set_from_slice", wuffs_base__pixel_buffer__set_from_slice(
&pb, &ic.pixcfg, g_pixel_slice_u8));
wuffs_base__pixel_buffer__set_color_u32_fill_rect(
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
&pb, wuffs_base__make_rect_ie_u32(0, 0, w, h), 0xFF234567);
uint64_t m = wuffs_webp__decoder__workbuf_len(dec).min_incl;
wuffs_base__range_ii_u64 r = wuffs_webp__decoder__workbuf_len(dec);
if (wuffs_base__range_ii_u64__is_empty(&r)) {
RETURN_FAIL("workbuf_len: indeterminate workbuf length");
}
uint64_t m = r.min_incl;
wuffs_base__slice_u8 workbuf = g_work_slice_u8;
if (workbuf.len > m) {
workbuf.len = m;
@@ -207,6 +225,188 @@ test_wuffs_webp_decode_many_small_reads() {
return NULL;
}
const char* //
test_wuffs_webp_decode_filter_alph() {
CHECK_FOCUS(__func__);
const int width = 41;
const int height = 6;
// src is arbitrary bytes, a subset of a noise texture.
const uint8_t src[1][246] = {
{
0x3C, 0xD4, 0xB6, 0x77, 0x64, 0xAB, 0xEF, 0x03, 0x7D, 0x2F, 0xC0,
0x24, 0x79, 0x3E, 0x87, 0x9A, 0x36, 0x7F, 0xBC, 0xF0, 0x42, 0xAF,
0x2D, 0x63, 0x22, 0xC3, 0x4E, 0x33, 0xE9, 0x67, 0x9C, 0x01, 0xA9,
0x23, 0x50, 0xDD, 0x36, 0x26, 0x4B, 0x94, 0xDC,
0x14, 0xFB, 0x47, 0x5F, 0xF2, 0x13, 0x53, 0xE7, 0xD7, 0x28, 0x8A,
0x07, 0xC6, 0x54, 0xE5, 0xB5, 0x0E, 0x78, 0xAE, 0x14, 0x84, 0xC0,
0x20, 0x7D, 0x95, 0xEA, 0x06, 0x8A, 0xFA, 0xC3, 0x70, 0x5C, 0xA6,
0x88, 0xCC, 0x09, 0xBA, 0xA6, 0x68, 0x1E, 0xB3,
0x5A, 0xA5, 0x68, 0xFD, 0x18, 0x90, 0x70, 0x3A, 0xF0, 0x5F, 0xCA,
0x44, 0x2B, 0x58, 0x12, 0xBD, 0x43, 0x61, 0xB0, 0x9D, 0x0E, 0x3F,
0xEC, 0xB6, 0x36, 0x6E, 0x94, 0xDE, 0x2F, 0xCE, 0x77, 0x92, 0x11,
0xE2, 0x37, 0x81, 0xA0, 0x46, 0xCD, 0xDF, 0xA3,
0x05, 0x97, 0xDC, 0x73, 0xE4, 0x35, 0x6D, 0xF2, 0xC7, 0x1D, 0x56,
0x7B, 0xCE, 0x2A, 0x17, 0x54, 0xE9, 0x21, 0x7E, 0x4E, 0xEB, 0x3B,
0xF9, 0x47, 0xCA, 0x75, 0xB9, 0xD9, 0x0B, 0x5A, 0x27, 0x8A, 0x4D,
0xF5, 0x1F, 0xB7, 0x8E, 0xD0, 0xA4, 0x28, 0x81,
0xD9, 0x32, 0xE4, 0xA3, 0x8F, 0x65, 0xD8, 0x9B, 0x42, 0xB2, 0x04,
0x8B, 0x19, 0xBE, 0x9B, 0x00, 0x51, 0x22, 0x31, 0xF6, 0x7C, 0xBF,
0x1A, 0x6C, 0xB0, 0x3C, 0x63, 0x0A, 0xF9, 0x53, 0x15, 0x93, 0x67,
0xB8, 0x4B, 0x01, 0xF4, 0x82, 0xBE, 0x11, 0xFD,
0x61, 0xC4, 0xA4, 0x57, 0x6D, 0x2C, 0xDD, 0xED, 0xB1, 0x92, 0x63,
0xA8, 0xE2, 0x35, 0xC8, 0x7F, 0xD5, 0x9D, 0x49, 0x85, 0xAE, 0xE2,
0x41, 0x0C, 0xFE, 0x76, 0xC5, 0x20, 0x45, 0xAC, 0x30, 0xCB, 0x73,
0x24, 0xEF, 0xDA, 0x80, 0xAB, 0x5E, 0x89, 0x6E,
},
};
const uint8_t wants[3][246] = {
{
0x3C, 0x10, 0xC6, 0x3D, 0xA1, 0x4C, 0x3B, 0x3E, 0xBB, 0xEA, 0xAA,
0xCE, 0x47, 0x85, 0x0C, 0xA6, 0xDC, 0x5B, 0x17, 0x07, 0x49, 0xF8,
0x25, 0x88, 0xAA, 0x6D, 0xBB, 0xEE, 0xD7, 0x3E, 0xDA, 0xDB, 0x84,
0xA7, 0xF7, 0xD4, 0x0A, 0x30, 0x7B, 0x0F, 0xEB,
0x50, 0x4B, 0x92, 0xF1, 0xE3, 0xF6, 0x49, 0x30, 0x07, 0x2F, 0xB9,
0xC0, 0x86, 0xDA, 0xBF, 0x74, 0x82, 0xFA, 0xA8, 0xBC, 0x40, 0x00,
0x20, 0x9D, 0x32, 0x1C, 0x22, 0xAC, 0xA6, 0x69, 0xD9, 0x35, 0xDB,
0x63, 0x2F, 0x38, 0xF2, 0x98, 0x00, 0x1E, 0xD1,
0xAA, 0x4F, 0xB7, 0xB4, 0xCC, 0x5C, 0xCC, 0x06, 0xF6, 0x55, 0x1F,
0x63, 0x8E, 0xE6, 0xF8, 0xB5, 0xF8, 0x59, 0x09, 0xA6, 0xB4, 0xF3,
0xDF, 0x95, 0xCB, 0x39, 0xCD, 0xAB, 0xDA, 0xA8, 0x1F, 0xB1, 0xC2,
0xA4, 0xDB, 0x5C, 0xFC, 0x42, 0x0F, 0xEE, 0x91,
0xAF, 0x46, 0x22, 0x95, 0x79, 0xAE, 0x1B, 0x0D, 0xD4, 0xF1, 0x47,
0xC2, 0x90, 0xBA, 0xD1, 0x25, 0x0E, 0x2F, 0xAD, 0xFB, 0xE6, 0x21,
0x1A, 0x61, 0x2B, 0xA0, 0x59, 0x32, 0x3D, 0x97, 0xBE, 0x48, 0x95,
0x8A, 0xA9, 0x60, 0xEE, 0xBE, 0x62, 0x8A, 0x0B,
0x88, 0xBA, 0x9E, 0x41, 0xD0, 0x35, 0x0D, 0xA8, 0xEA, 0x9C, 0xA0,
0x2B, 0x44, 0x02, 0x9D, 0x9D, 0xEE, 0x10, 0x41, 0x37, 0xB3, 0x72,
0x8C, 0xF8, 0xA8, 0xE4, 0x47, 0x51, 0x4A, 0x9D, 0xB2, 0x45, 0xAC,
0x64, 0xAF, 0xB0, 0xA4, 0x26, 0xE4, 0xF5, 0xF2,
0xE9, 0xAD, 0x51, 0xA8, 0x15, 0x41, 0x1E, 0x0B, 0xBC, 0x4E, 0xB1,
0x59, 0x3B, 0x70, 0x38, 0xB7, 0x8C, 0x29, 0x72, 0xF7, 0xA5, 0x87,
0xC8, 0xD4, 0xD2, 0x48, 0x0D, 0x2D, 0x72, 0x1E, 0x4E, 0x19, 0x8C,
0xB0, 0x9F, 0x79, 0xF9, 0xA4, 0x02, 0x8B, 0xF9,
},
{
0x3C, 0x10, 0xC6, 0x3D, 0xA1, 0x4C, 0x3B, 0x3E, 0xBB, 0xEA, 0xAA,
0xCE, 0x47, 0x85, 0x0C, 0xA6, 0xDC, 0x5B, 0x17, 0x07, 0x49, 0xF8,
0x25, 0x88, 0xAA, 0x6D, 0xBB, 0xEE, 0xD7, 0x3E, 0xDA, 0xDB, 0x84,
0xA7, 0xF7, 0xD4, 0x0A, 0x30, 0x7B, 0x0F, 0xEB,
0x50, 0x0B, 0x0D, 0x9C, 0x93, 0x5F, 0x8E, 0x25, 0x92, 0x12, 0x34,
0xD5, 0x0D, 0xD9, 0xF1, 0x5B, 0xEA, 0xD3, 0xC5, 0x1B, 0xCD, 0xB8,
0x45, 0x05, 0x3F, 0x57, 0xC1, 0x78, 0xD1, 0x01, 0x4A, 0x37, 0x2A,
0x2F, 0xC3, 0xDD, 0xC4, 0xD6, 0xE3, 0x2D, 0x9E,
0xAA, 0xB0, 0x75, 0x99, 0xAB, 0xEF, 0xFE, 0x5F, 0x82, 0x71, 0xFE,
0x19, 0x38, 0x31, 0x03, 0x18, 0x2D, 0x34, 0x75, 0xB8, 0xDB, 0xF7,
0x31, 0xBB, 0x75, 0xC5, 0x55, 0x56, 0x00, 0xCF, 0xC1, 0xC9, 0x3B,
0x11, 0xFA, 0x5E, 0x64, 0x1C, 0xB0, 0x0C, 0x41,
0xAF, 0x47, 0x51, 0x0C, 0x8F, 0x24, 0x6B, 0x51, 0x49, 0x8E, 0x54,
0x94, 0x06, 0x5B, 0x1A, 0x6C, 0x16, 0x55, 0xF3, 0x06, 0xC6, 0x32,
0x2A, 0x02, 0x3F, 0x3A, 0x0E, 0x2F, 0x0B, 0x29, 0xE8, 0x53, 0x88,
0x06, 0x19, 0x15, 0xF2, 0xEC, 0x54, 0x34, 0xC2,
0x88, 0x79, 0x35, 0xAF, 0x1E, 0x89, 0x43, 0xEC, 0x8B, 0x40, 0x58,
0x1F, 0x1F, 0x19, 0xB5, 0x6C, 0x67, 0x77, 0x24, 0xFC, 0x42, 0xF1,
0x44, 0x6E, 0xEF, 0x76, 0x71, 0x39, 0x04, 0x7C, 0xFD, 0xE6, 0xEF,
0xBE, 0x64, 0x16, 0xE6, 0x6E, 0x12, 0x45, 0xBF,
0xE9, 0x3D, 0xD9, 0x06, 0x8B, 0xB5, 0x20, 0xD9, 0x3C, 0xD2, 0xBB,
0xC7, 0x01, 0x4E, 0x7D, 0xEB, 0x3C, 0x14, 0x6D, 0x81, 0xF0, 0xD3,
0x85, 0x7A, 0xED, 0xEC, 0x36, 0x59, 0x49, 0x28, 0x2D, 0xB1, 0x62,
0xE2, 0x53, 0xF0, 0x66, 0x19, 0x70, 0xCE, 0x2D,
},
{
0x3C, 0x10, 0xC6, 0x3D, 0xA1, 0x4C, 0x3B, 0x3E, 0xBB, 0xEA, 0xAA,
0xCE, 0x47, 0x85, 0x0C, 0xA6, 0xDC, 0x5B, 0x17, 0x07, 0x49, 0xF8,
0x25, 0x88, 0xAA, 0x6D, 0xBB, 0xEE, 0xD7, 0x3E, 0xDA, 0xDB, 0x84,
0xA7, 0xF7, 0xD4, 0x0A, 0x30, 0x7B, 0x0F, 0xEB,
0x50, 0x1F, 0x1C, 0x5F, 0xB5, 0x73, 0xB5, 0x9F, 0xD6, 0x27, 0x8A,
0xB5, 0xF4, 0x53, 0xE5, 0xB4, 0xF8, 0xEF, 0x59, 0x5D, 0x23, 0x92,
0x20, 0x00, 0xB7, 0x64, 0xB8, 0x75, 0x58, 0xC3, 0x6F, 0xCC, 0x1B,
0xC6, 0xCB, 0xB1, 0xBA, 0x86, 0x39, 0x1E, 0xAD,
0xAA, 0x1E, 0x83, 0xC3, 0x17, 0x90, 0x42, 0x66, 0x8D, 0x5F, 0x8C,
0xFB, 0x2A, 0x58, 0xFC, 0x88, 0x0F, 0x67, 0xB0, 0x51, 0x25, 0xD3,
0x4D, 0xE3, 0x35, 0x6E, 0x56, 0xF1, 0x03, 0x3C, 0x77, 0x66, 0x11,
0x9E, 0xDA, 0x41, 0xEA, 0xFC, 0x7C, 0x40, 0x72,
0xAF, 0xBA, 0xDB, 0x72, 0xE4, 0x34, 0x6D, 0x83, 0x71, 0x60, 0xE3,
0x7A, 0xCE, 0x26, 0xE1, 0xC1, 0x31, 0xAA, 0x71, 0x60, 0x1F, 0x08,
0xF9, 0x46, 0xCA, 0x74, 0x15, 0x89, 0x0B, 0x9E, 0x00, 0x8A, 0x82,
0xF4, 0x1E, 0xB7, 0x8D, 0x6F, 0xA4, 0x90, 0x43,
0x88, 0xC5, 0xCA, 0x04, 0x05, 0x65, 0x76, 0x27, 0x57, 0xF8, 0x03,
0x8B, 0xF8, 0x0E, 0x64, 0x44, 0x51, 0xEC, 0xE4, 0xC9, 0x04, 0xBF,
0x19, 0x6C, 0xA0, 0x86, 0x8A, 0x08, 0xF9, 0x52, 0x15, 0x32, 0x91,
0xB7, 0x4B, 0xE5, 0xAF, 0x13, 0x06, 0x11, 0xFD,
0xE9, 0xC3, 0x6C, 0x57, 0xC5, 0x2B, 0x19, 0xED, 0xB0, 0x91, 0x63,
0x93, 0xE1, 0x35, 0x53, 0xB2, 0x94, 0x9C, 0xDD, 0x47, 0xAE, 0xE1,
0x7C, 0xDB, 0xFD, 0x59, 0x22, 0x20, 0x44, 0xAC, 0x9F, 0x87, 0x59,
0xA3, 0x26, 0x9A, 0xE4, 0xF3, 0x44, 0xD8, 0x6D,
},
};
wuffs_webp__decoder dec = {0};
dec.private_impl.f_width = width;
dec.private_impl.f_height = height;
for (int filter = 0; filter < 3; filter++) {
uint8_t buf[246];
memcpy(&buf[0], &src[0][0], 246);
wuffs_base__slice_u8 s = wuffs_base__make_slice_u8(&buf[0], 246);
switch (filter) {
case 0:
wuffs_webp__decoder__filter_alph_horizontal(&dec, s);
break;
case 1:
wuffs_webp__decoder__filter_alph_vertical(&dec, s);
break;
case 2:
wuffs_webp__decoder__filter_alph_gradient(&dec, s);
break;
}
if (0 == memcmp(&buf[0], &wants[filter][0], 246)) {
continue;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int i = (y * width) + x;
int have = buf[i];
int want = wants[filter][i];
if (have != want) {
RETURN_FAIL("filter=%d, y=%d, x=%d: have 0x%02X, want 0x%02X", filter,
y, x, have, want);
}
}
}
return "unreachable";
}
return NULL;
}
// ---------------- Mimic Tests
#ifdef WUFFS_MIMIC
@@ -518,8 +718,10 @@ bench_mimic_webp_lossy_decode_image_4002k_24bpp() {
proc g_tests[] = {
test_wuffs_webp_decode_filter_alph,
test_wuffs_webp_decode_interface_lossless,
test_wuffs_webp_decode_interface_lossy,
test_wuffs_webp_decode_interface_vp8x_alpha_lossy,
test_wuffs_webp_decode_many_small_reads,
#ifdef WUFFS_MIMIC
+15 -4
View File
@@ -1237,6 +1237,17 @@ do_test__wuffs_base__image_decoder(
CHECK_STATUS("decode_image_config",
wuffs_base__image_decoder__decode_image_config(b, &ic, &src));
wuffs_base__slice_u8 workbuf = g_work_slice_u8;
wuffs_base__range_ii_u64 workbuf_len =
wuffs_base__image_decoder__workbuf_len(b);
if (workbuf_len.min_incl > workbuf_len.max_incl) {
return "indeterminate workbuf_len";
}
if (workbuf_len.max_incl > workbuf.len) {
return "workbuf_len is too large";
}
workbuf.len = workbuf_len.max_incl;
uint32_t have_width = wuffs_base__pixel_config__width(&ic.pixcfg);
if (have_width != want_width) {
RETURN_FAIL("width: have %" PRIu32 ", want %" PRIu32, have_width,
@@ -1254,9 +1265,9 @@ do_test__wuffs_base__image_decoder(
wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){});
CHECK_STATUS("set_from_slice", wuffs_base__pixel_buffer__set_from_slice(
&pb, &ic.pixcfg, g_pixel_slice_u8));
CHECK_STATUS("decode_frame", wuffs_base__image_decoder__decode_frame(
b, &pb, &src, WUFFS_BASE__PIXEL_BLEND__SRC,
g_work_slice_u8, NULL));
CHECK_STATUS("decode_frame",
wuffs_base__image_decoder__decode_frame(
b, &pb, &src, WUFFS_BASE__PIXEL_BLEND__SRC, workbuf, NULL));
uint64_t n = wuffs_base__pixel_config__pixbuf_len(&ic.pixcfg);
if (n < 4) {
@@ -1297,7 +1308,7 @@ do_test__wuffs_base__io_transformer(wuffs_base__io_transformer* b,
wuffs_base__range_ii_u64 workbuf_len =
wuffs_base__io_transformer__workbuf_len(b);
if (workbuf_len.min_incl > workbuf_len.max_incl) {
return "inconsistent workbuf_len";
return "indeterminate workbuf_len";
}
if (workbuf_len.max_incl > IO_BUFFER_ARRAY_SIZE) {
return "workbuf_len is too large";
Binary file not shown.

After

Width:  |  Height:  |  Size: 836 B

Binary file not shown.
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

+14 -10
View File
@@ -17,6 +17,7 @@ OK. 14a5b4b0 test/data/animated-red-blue.000003.nie
OK. 76e40058 test/data/animated-red-blue.apng
OK. 76e40058 test/data/animated-red-blue.gif
OK. 76e40058 test/data/animated-red-blue.nia
BAD c4a44cef test/data/animated-red-blue.webp
OK. f9a10dac test/data/artificial-gif/background-color.gif
OK. 36e7d9c7 test/data/artificial-gif/empty-palette.gif
OK. db4c30eb test/data/artificial-gif/frame-out-of-bounds.gif
@@ -65,7 +66,7 @@ OK. 41e6110e test/data/bricks-color.etc1s.pkm
OK. 5670f263 test/data/bricks-color.etc2.pkm
OK. 72a1f9cc test/data/bricks-color.jpeg
OK. 076cb375 test/data/bricks-color.lossless.webp
OK. 3466b80b test/data/bricks-color.lossy.webp
OK. 25a84d5d test/data/bricks-color.lossy.webp
OK. 076cb375 test/data/bricks-color.png
OK. 076cb375 test/data/bricks-color.qoi
OK. 076cb375 test/data/bricks-color.tga
@@ -102,26 +103,26 @@ OK. 030f5a48 test/data/harvesters.bmp
OK. c18b3d5a test/data/harvesters.gif
OK. f217df74 test/data/harvesters.jpeg
OK. 030f5a48 test/data/harvesters.lossless.webp
OK. 60bc9e08 test/data/harvesters.lossy.webp
OK. 9fcd525c test/data/harvesters.lossy.webp
OK. 030f5a48 test/data/harvesters.png
OK. e776c90f test/data/hat.bmp
OK. 6dcba6a4 test/data/hat.gif
OK. 2298f3ca test/data/hat.jpeg
OK. e776c90f test/data/hat.lossless.webp
OK. be7f0169 test/data/hat.lossy.webp
OK. 291ba102 test/data/hat.lossy.webp
OK. e776c90f test/data/hat.png
OK. d30bfe5d test/data/hat.wbmp
OK. 33a44f22 test/data/hibiscus.primitive.bmp
OK. 25e212b3 test/data/hibiscus.primitive.gif
OK. 9624fa44 test/data/hibiscus.primitive.jpeg
OK. 33a44f22 test/data/hibiscus.primitive.lossless.webp
OK. 09bbaab0 test/data/hibiscus.primitive.lossy.webp
OK. 05ebd4c2 test/data/hibiscus.primitive.lossy.webp
OK. 33a44f22 test/data/hibiscus.primitive.png
OK. 60040742 test/data/hibiscus.regular.bmp
OK. b727da8b test/data/hibiscus.regular.gif
OK. 41e39405 test/data/hibiscus.regular.jpeg
OK. 60040742 test/data/hibiscus.regular.lossless.webp
OK. 6bbe1ed2 test/data/hibiscus.regular.lossy.webp
OK. 6f151b3e test/data/hibiscus.regular.lossy.webp
OK. 60040742 test/data/hibiscus.regular.png
OK. dcbb225a test/data/hippopotamus.bmp
OK. ed4b78fc test/data/hippopotamus.interlaced.gif
@@ -130,10 +131,11 @@ BAD c3c4bd65 test/data/hippopotamus.interlaced.truncated.gif
BAD 3feec847 test/data/hippopotamus.interlaced.truncated.png
OK. 96bdbbb3 test/data/hippopotamus.jpeg
OK. dcbb225a test/data/hippopotamus.lossless.webp
OK. 66ccbffa test/data/hippopotamus.lossy.webp
OK. 56cf6ae1 test/data/hippopotamus.lossy.webp
OK. 2535637e test/data/hippopotamus.masked-with-muybridge.etc2.bgra-binary.pkm
OK. 720180ee test/data/hippopotamus.masked-with-muybridge.etc2.bgra-nonpremul.pkm
OK. d3bbed27 test/data/hippopotamus.masked-with-muybridge.gif
OK. 1d8cd8b2 test/data/hippopotamus.masked-with-muybridge.lossy.webp
OK. 7e6acf01 test/data/hippopotamus.masked-with-muybridge.png
OK. dcbb225a test/data/hippopotamus.nie
OK. 57d9a4dd test/data/hippopotamus.pgm
@@ -146,17 +148,19 @@ OK. 39d18fce test/data/mona-lisa.21x32.c3q4.handsum
OK. f6bb81e2 test/data/mona-lisa.21x32.etc2.pkm
OK. 012ce4cf test/data/mona-lisa.21x32.lossless.webp
OK. 012ce4cf test/data/mona-lisa.21x32.png
OK. 457ca659 test/data/mona-lisa.21x32.q0.lossy.webp
OK. 4503912c test/data/mona-lisa.21x32.q25.lossy.webp
OK. c2c97129 test/data/mona-lisa.21x32.q0.lossy.webp
OK. c9d3bcd2 test/data/mona-lisa.21x32.q25.lossy.webp
OK. 0a22e689 test/data/mona-lisa.21x32.q50.jpeg
OK. 8ae1312b test/data/mona-lisa.21x32.q50.lossy.webp
OK. 49f54fd4 test/data/mona-lisa.21x32.q50.lossy.webp
OK. 059ecb80 test/data/mona-lisa.21x32.q90.jpeg
OK. 567c8999 test/data/mona-lisa.21x32.q90.lossy.webp
OK. 8396e4f6 test/data/mona-lisa.21x32.q90.lossy.webp
OK. 012ce4cf test/data/mona-lisa.21x32.qoi
OK. 12fc69a5 test/data/mona-lisa.21x32.th
OK. 2dccfe56 test/data/muybridge-frame-000.wbmp
OK. db2733f5 test/data/muybridge.apng
OK. db2733f5 test/data/muybridge.gif
OK. 773c8dda test/data/peacock.24x19.lossy.webp
OK. ad130b74 test/data/peacock.24x19.png
OK. d42a13cd test/data/peacock.default.jpeg
OK. c8a50351 test/data/peacock.default.truncated.decoded-by-djpeg.png
OK. 91b6bebb test/data/peacock.default.truncated.decoded-by-wuffs.png