forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB8dbd683Tahinli3 months ago856 commits

gen-still-fixtures.sh224 lines, 13.2 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# Still-image fixtures for ec-image's differential tests: every PNG colour
3# type and bit depth, both JPEG processes at each chroma layout, and both WebP
4# bitstreams with and without alpha.
5#
6# The corpus is generated rather than committed, so the repository stays free
7# of binaries and every machine can rebuild it: ImageMagick writes the PNG and
8# JPEG variants, ffmpeg the WebP ones.
9set -euo pipefail
10
11root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
12out="$root/fixtures/stills"
13mkdir -p "$out"
14
15need() {
16 command -v "$1" >/dev/null || { echo "missing: $1" >&2; exit 1; }
17}
18need magick
19need ffmpeg
20
21# Source pictures: one with smooth and hard content at even dimensions, one at
22# odd dimensions (chroma subsampling and Adam7 both round there), and one with
23# a real alpha channel.
24# Seeded, so the corpus is byte-identical on every machine and a differential
25# bar measured today still means the same thing tomorrow.
26magick -seed 1234 -size 320x240 plasma:fractal -blur 0x1 \
27 -fill white -stroke black -draw "rectangle 20,20 80,60" \
28 -draw "line 0,239 319,0" "$out/source.png"
29magick "$out/source.png" -resize 61x47! "$out/source-odd.png"
30magick "$out/source.png" -alpha set -channel A \
31 -fx "i/w" +channel "$out/source-alpha.png"
32
33# --- PNG: every colour type, every bit depth, both interlace methods --------
34magick "$out/source.png" -define png:color-type=2 -depth 8 "$out/rgb8.png"
35magick "$out/source-alpha.png" -define png:color-type=6 -depth 8 "$out/rgba8.png"
36magick "$out/source.png" -colorspace Gray -define png:color-type=0 -depth 8 "$out/gray8.png"
37magick "$out/source-alpha.png" -colorspace Gray -define png:color-type=4 -depth 8 "$out/graya8.png"
38magick "$out/source.png" -define png:color-type=2 -depth 16 "$out/rgb16.png"
39magick "$out/source-alpha.png" -define png:color-type=6 -depth 16 "$out/rgba16.png"
40magick "$out/source.png" -colorspace Gray -define png:color-type=0 -depth 16 "$out/gray16.png"
41magick "$out/source.png" -colors 256 -define png:color-type=3 "$out/palette8.png"
42magick "$out/source.png" -colors 16 -define png:color-type=3 -define png:bit-depth=4 "$out/palette4.png"
43magick "$out/source.png" -colors 3 -define png:color-type=3 -define png:bit-depth=2 "$out/palette2.png"
44magick "$out/source.png" -monochrome -define png:color-type=3 -define png:bit-depth=1 "$out/palette1.png"
45magick "$out/source.png" -colorspace Gray -depth 4 -define png:color-type=0 -define png:bit-depth=4 "$out/gray4.png"
46magick "$out/source.png" -colorspace Gray -depth 2 -define png:color-type=0 -define png:bit-depth=2 "$out/gray2.png"
47magick "$out/source.png" -colorspace Gray -threshold 50% -define png:color-type=0 -define png:bit-depth=1 "$out/gray1.png"
48magick "$out/source.png" -interlace PNG -define png:color-type=2 "$out/interlaced-rgb8.png"
49magick "$out/source-alpha.png" -interlace PNG -define png:color-type=6 -depth 16 "$out/interlaced-rgba16.png"
50magick "$out/source.png" -colors 64 -interlace PNG -define png:color-type=3 "$out/interlaced-palette.png"
51magick "$out/source-odd.png" -define png:color-type=2 "$out/odd-rgb8.png"
52magick "$out/source-odd.png" -interlace PNG -define png:color-type=2 "$out/odd-interlaced.png"
53# Palette with a transparent entry, and a colour-keyed truecolour image.
54magick "$out/source-alpha.png" -colors 32 PNG8:"$out/trns-palette.png"
55magick "$out/source.png" -define png:color-type=2 -transparent white "$out/trns-rgb.png" || true
56magick "$out/source.png" -colorspace Gray -depth 8 -transparent black \
57 -define png:color-type=0 "$out/trns-gray.png" || true
58
59# --- JPEG: both processes, every chroma layout, restarts, EXIF -------------
60magick "$out/source.png" -quality 90 -sampling-factor 1x1 "$out/baseline-444.jpg"
61magick "$out/source.png" -quality 90 -sampling-factor 2x1 "$out/baseline-422.jpg"
62magick "$out/source.png" -quality 90 -sampling-factor 2x2 "$out/baseline-420.jpg"
63magick "$out/source.png" -quality 50 -sampling-factor 2x2 "$out/baseline-420-q50.jpg"
64magick "$out/source.png" -quality 90 -sampling-factor 2x2 -interlace JPEG "$out/progressive-420.jpg"
65magick "$out/source.png" -quality 90 -sampling-factor 1x1 -interlace JPEG "$out/progressive-444.jpg"
66magick "$out/source.png" -colorspace Gray -quality 90 "$out/gray.jpg"
67magick "$out/source.png" -quality 90 -sampling-factor 2x2 \
68 -define jpeg:restart-interval=4 "$out/restart-420.jpg"
69magick "$out/source-odd.png" -quality 90 -sampling-factor 2x2 "$out/odd-420.jpg"
70magick "$out/source-odd.png" -quality 90 -sampling-factor 2x1 "$out/odd-422.jpg"
71
72# --- WebP: lossy and lossless, with and without alpha ----------------------
73ffmpeg -loglevel error -y -i "$out/source.png" -c:v libwebp -lossless 0 -quality 80 \
74 "$out/lossy.webp"
75ffmpeg -loglevel error -y -i "$out/source.png" -c:v libwebp -lossless 0 -quality 40 \
76 "$out/lossy-q40.webp"
77ffmpeg -loglevel error -y -i "$out/source.png" -c:v libwebp -lossless 1 "$out/lossless.webp"
78ffmpeg -loglevel error -y -i "$out/source-alpha.png" -c:v libwebp -lossless 1 \
79 "$out/lossless-alpha.webp"
80ffmpeg -loglevel error -y -i "$out/source-alpha.png" -c:v libwebp -lossless 0 -quality 80 \
81 "$out/lossy-alpha.webp"
82ffmpeg -loglevel error -y -i "$out/source-odd.png" -c:v libwebp -lossless 0 -quality 80 \
83 "$out/odd-lossy.webp"
84ffmpeg -loglevel error -y -i "$out/source-odd.png" -c:v libwebp -lossless 1 "$out/odd-lossless.webp"
85# A palette-friendly picture, so the lossless colour-indexing transform is
86# exercised rather than only the predictor one.
87magick "$out/source.png" -colors 8 "$out/source-flat.png"
88ffmpeg -loglevel error -y -i "$out/source-flat.png" -c:v libwebp -lossless 1 \
89 "$out/lossless-palette.webp"
90# An animation, which the decoder must refuse by name rather than decode.
91# ffmpeg's libwebp_anim silently writes a single still here, so ImageMagick
92# writes this one: its output really does carry ANIM/ANMF chunks.
93magick -delay 10 "$out/source.png" "$out/source-flat.png" "$out/source-odd.png" \
94 -loop 0 "$out/animated.webp"
95# A second animation whose frames carry alpha and ask for dispose-to-background:
96# the first one is opaque and disposes nothing, so it leaves the blending and
97# the disposal rectangle untested. Lossless, so the frames are exact and the
98# only thing the comparison can catch is the compositing.
99magick "$out/source-alpha.png" -resize 96x72! "$out/anim-a.png"
100magick "$out/source-alpha.png" -resize 96x72! -channel A -evaluate multiply 0.5 \
101 +channel "$out/anim-b.png"
102magick "$out/source-flat.png" -resize 48x36! -background none -extent 96x72 \
103 "$out/anim-c.png"
104magick -dispose Background -delay 6 "$out/anim-a.png" -delay 9 "$out/anim-b.png" \
105 -delay 3 "$out/anim-c.png" -loop 0 -define webp:lossless=true \
106 "$out/anim-alpha.webp"
107rm -f "$out/anim-a.png" "$out/anim-b.png" "$out/anim-c.png"
108# libwebp's own composited frames, through Pillow, as the reference for that
109# animation: the `image` crate ignores the dispose-to-background flag, so it
110# cannot be the oracle for the frame that follows a disposal.
111if python3 -c "import PIL" 2>/dev/null; then
112 python3 - "$out" <<'PILLOW'
113import sys
114from PIL import Image
115
116out = sys.argv[1]
117animation = Image.open(f"{out}/anim-alpha.webp")
118for i in range(animation.n_frames):
119 animation.seek(i)
120 animation.convert("RGBA").save(f"{out}/anim-alpha-f{i}.png")
121PILLOW
122else
123 echo "skipped anim-alpha-f*.png: python3 Pillow is not installed" >&2
124fi
125
126# --- GIF: still, interlaced, transparent, odd-size, animated ---------------
127# GIF is palette-only, so every source is quantised first; the differential bar
128# is pixel-exact, which only means anything if both decoders see the same file.
129magick "$out/source.png" -colors 256 "$out/still.gif"
130magick "$out/source.png" -colors 256 -interlace GIF "$out/interlaced.gif"
131magick "$out/source.png" -colors 64 -transparent white "$out/trns.gif"
132magick "$out/source-odd.png" -colors 256 "$out/odd.gif"
133# Three frames, each with its own delay, and a disposal method that clears the
134# frame's rectangle -- the compositing path only runs when frames differ.
135magick -delay 7 "$out/source.png" -delay 13 "$out/source-flat.png" \
136 -delay 4 "$out/source-odd.png" -dispose Background -loop 0 -colors 64 \
137 "$out/animated.gif"
138
139# --- BMP: every depth, both compressions, both row orders ------------------
140# ImageMagick writes the ordinary files; the shapes it has no switch for (16-bit
141# bitfields, top-down rows, a 32-bit alpha channel, the 12-byte OS/2 header,
142# and RLE4, which its RLE switch never chooses) are written by the helper.
143magick "$out/source.png" -resize 64x48! "$out/bmp-source.png"
144magick "$out/source-alpha.png" -resize 64x48! "$out/bmp-source-alpha.png"
145magick "$out/bmp-source.png" -monochrome -type bilevel BMP3:"$out/mono.bmp"
146magick "$out/bmp-source.png" -colors 16 -type palette -depth 4 -compress None \
147 BMP3:"$out/pal4.bmp"
148magick "$out/bmp-source.png" -colors 256 -type palette -compress None \
149 BMP3:"$out/pal8.bmp"
150magick "$out/bmp-source.png" -colors 256 -type palette -compress RLE \
151 BMP3:"$out/rle8.bmp"
152magick "$out/bmp-source.png" BMP3:"$out/rgb24.bmp"
153magick "$out/source-odd.png" -resize 37x29! BMP3:"$out/odd24.bmp"
154# A BITMAPV5HEADER, whose 124 bytes carry the colour space fields a v3 file
155# has no room for; ImageMagick writes one when asked for alpha.
156magick "$out/bmp-source-alpha.png" -define bmp:format=bmp5 BMP:"$out/v5alpha.bmp"
157magick "$out/bmp-source.png" rgba:"$out/bmp-source.rgba"
158magick "$out/bmp-source-alpha.png" rgba:"$out/bmp-source-alpha.rgba"
159magick "$out/bmp-source.png" "$out/bmp-embed.png"
160magick "$out/bmp-source.png" -quality 85 "$out/bmp-embed.jpg"
161python3 "$(dirname "$0")/lib/gen-bmp-shapes.py" "$out" "$out/bmp-source.rgba" 64 48 \
162 "$out/bmp-source-alpha.rgba" "$out/bmp-embed.png" "$out/bmp-embed.jpg"
163rm -f "$out/bmp-source.png" "$out/bmp-source-alpha.png" "$out/bmp-source.rgba" \
164 "$out/bmp-source-alpha.rgba" "$out/bmp-embed.png" "$out/bmp-embed.jpg"
165
166# --- TIFF: both byte orders, four compressions, strips and tiles -----------
167# TIFF fixes almost nothing about its layout, so the corpus is a cross-product:
168# each compression, each sample depth, both byte orders, strips and tiles, and
169# both plane arrangements. ImageMagick writes all of them.
170magick "$out/source.png" -resize 64x48! "$out/tiff-source.png"
171magick "$out/source-alpha.png" -resize 64x48! "$out/tiff-source-alpha.png"
172magick "$out/tiff-source.png" -depth 8 -compress None "$out/rgb8.tiff"
173magick "$out/tiff-source.png" -depth 8 -compress LZW "$out/rgb8-lzw.tiff"
174magick "$out/tiff-source.png" -depth 8 -compress RLE "$out/rgb8-packbits.tiff"
175magick "$out/tiff-source.png" -depth 8 -compress Zip "$out/rgb8-zip.tiff"
176# The horizontal-differencing predictor, which is what an encoder reaches for
177# when the compression is LZW or Deflate.
178magick "$out/tiff-source.png" -depth 8 -compress LZW -define tiff:predictor=2 \
179 "$out/rgb8-lzw-pred.tiff"
180magick "$out/tiff-source.png" -depth 8 -compress Zip -define tiff:predictor=2 \
181 "$out/rgb8-zip-pred.tiff"
182# Big-endian: the same picture with every field byte-swapped.
183magick "$out/tiff-source.png" -depth 8 -compress None -define tiff:endian=msb \
184 "$out/rgb8-be.tiff"
185# Tiles rather than strips, and planes rather than interleaved samples.
186magick "$out/tiff-source.png" -depth 8 -compress LZW \
187 -define tiff:tile-geometry=32x32 "$out/rgb8-tiled.tiff"
188magick "$out/tiff-source.png" -depth 8 -compress None -interlace Plane \
189 "$out/rgb8-planar.tiff"
190# Depths: bilevel, 4-bit palette, 8-bit grey, 16-bit grey and 16-bit colour.
191magick "$out/tiff-source.png" -monochrome -type bilevel -compress None \
192 "$out/bilevel.tiff"
193magick "$out/tiff-source.png" -colors 16 -type palette -depth 4 -compress None \
194 "$out/palette4.tiff"
195# The incumbent refuses a 4-bit palette outright, so ffmpeg -- which reads it --
196# writes the reference picture for that one fixture.
197ffmpeg -loglevel error -y -i "$out/palette4.tiff" "$out/palette4-golden.png"
198# -alpha off, or ImageMagick attaches an alpha channel to a grey picture that
199# has none, and a two-sample grey file is a shape the incumbent refuses.
200magick "$out/tiff-source.png" -colorspace gray -alpha off -depth 8 -compress None \
201 "$out/gray8.tiff"
202magick "$out/tiff-source.png" -colorspace gray -alpha off -depth 16 -compress None \
203 "$out/gray16.tiff"
204magick "$out/tiff-source.png" -depth 16 -compress Zip "$out/rgb16.tiff"
205# Alpha, which TIFF carries as a fourth sample plus an ExtraSamples tag: both
206# the straight and the premultiplied tagging, neither of which ImageMagick will
207# write on request.
208magick "$out/tiff-source-alpha.png" rgba:"$out/tiff-source-alpha.rgba"
209python3 "$(dirname "$0")/lib/gen-tiff-shapes.py" "$out" "$out/tiff-source-alpha.rgba" 64 48
210rm -f "$out/tiff-source.png" "$out/tiff-source-alpha.png" \
211 "$out/tiff-source-alpha.rgba"
212
213# Small versions of each format, for the fuzz sweep: ten thousand mutations
214# per format is only affordable on a picture that decodes in microseconds.
215magick "$out/source.png" -resize 32x24! "$out/tiny.png"
216magick "$out/tiny.png" -quality 80 -sampling-factor 2x2 "$out/tiny.jpg"
217ffmpeg -loglevel error -y -i "$out/tiny.png" -c:v libwebp -lossless 0 -quality 75 \
218 "$out/tiny-lossy.webp"
219ffmpeg -loglevel error -y -i "$out/tiny.png" -c:v libwebp -lossless 1 "$out/tiny-lossless.webp"
220magick "$out/tiny.png" -colors 64 "$out/tiny.gif"
221magick "$out/tiny.png" -colors 64 -type palette -compress None BMP3:"$out/tiny.bmp"
222magick "$out/tiny.png" -depth 8 -compress LZW "$out/tiny.tiff"
223
224echo "wrote $(ls "$out" | wc -l) files to $out"