Predictors
A predictor estimates a sample from decoded neighbours and stores the residual. Smooth tiles produce small residuals that terminal codecs can code well. The six predictors read different neighbours and decode at different speeds.
The green four rebuild whole rows. delta_n adds the row above. The others use a prefix sum suited to SIMD. med and average decode one sample at a time. Fast on one CPU core shows why.
Predictors reconstruct the original raster exactly. Predictors use wrapping unsigned arithmetic on raw bits. Data from 8 to 64 bits, including floats, round-trips bit for bit. Prediction restarts at each plane, so cube bands are predicted separately.
The examples share one 64 by 64 tile. Each recipe compresses it and checks the round trip. Python also exposes the individual nodes for custom OpenZL graphs; R and Julia use recipes.
import numpy as np
import geozl
y, x = np.mgrid[0:64, 0:64]
tile = (1000 + 3 * x + 2 * y).astype(np.int16)
library(geozl)
tile <- outer(0:63, 0:63, function(x, y) 1000 + 3 * x + 2 * y)
using GeoZL
tile = [Int16(1000 + 3 * x + 2 * y) for x in 0:63, y in 0:63]
01delta_w
delta_w stores each sample minus the one on its left. The first sample of every row stays whole, so rows never depend on each other.
Use it when data is smooth along rows but not across them, such as a product written one scan line at a time. Try planar otherwise.
| ctid | 0x72D701 |
|---|---|
| header | uint32 · row width in samples |
| widths | 8 · 16 · 32 · 64-bit elements, read as integers |
| decode | rᵢ + previous reconstructed, per row (prefix sum) |
// encode, each row on its own
r[y][0] = s[y][0];
for (x = 1; x < w; x++) r[y][x] = s[y][x] - s[y][x - 1];
// decode, a prefix sum along the row
o[y][0] = r[y][0];
for (x = 1; x < w; x++) o[y][x] = r[y][x] + o[y][x - 1];
g = geozl.graph(tile, "delta_w>zigzag>zstd")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)
node = geozl.lossless.DeltaW(tile.shape[1])
g <- geozl_graph(tile, "delta_w>zigzag>zstd", dtype = "int16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "int16", dim = dim(tile))
stopifnot(identical(back, tile))
g = GeoZL.graph(tile, "delta_w>zigzag>zstd")
frame = GeoZL.compress(tile, g)
@assert GeoZL.decompress(Int16, frame, size(tile)...) == tile
02delta_n
delta_n stores each sample minus the one directly above it. The first row of each plane stays whole, so columns never depend on each other.
Use it when structure runs north to south, or when each row is a small change on the one before, as in a band stack with one band per row. delta_n is the fastest predictor to decode. Each row needs one SIMD add with the row above and no prefix sum.
| ctid | 0x72D702 |
|---|---|
| header | uint32 · row width in samples + uint32 · plane count, when above one |
| widths | 8 · 16 · 32 · 64-bit elements, read as integers |
| decode | rᵢ + row above, per column |
// encode, the first row stays whole
for (y = 1; y < h; y++)
for (x = 0; x < w; x++) r[y][x] = s[y][x] - s[y - 1][x];
// decode, each row is one vector add
for (y = 1; y < h; y++)
for (x = 0; x < w; x++) o[y][x] = r[y][x] + o[y - 1][x];
g = geozl.graph(tile, "delta_n>zigzag>zstd")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)
node = geozl.lossless.DeltaN(tile.shape[1], planes=1)
g <- geozl_graph(tile, "delta_n>zigzag>zstd", dtype = "int16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "int16", dim = dim(tile))
stopifnot(identical(back, tile))
g = GeoZL.graph(tile, "delta_n>zigzag>zstd")
frame = GeoZL.compress(tile, g)
@assert GeoZL.decompress(Int16, frame, size(tile)...) == tile
03planar
planar predicts each sample as W + N − NW, which continues the local slope. On a perfect plane every residual away from the first row and column is zero, and on real terrain they stay small.
Try planar first. Most recipes start with it. It overshoots sharp edges, where med does better.
| ctid | 0x72D703 |
|---|---|
| header | uint32 · row width in samples + uint32 · plane count, when above one |
| widths | 8 · 16 · 32 · 64-bit elements, read as integers |
| decode | fold N−NW from the row above, then prefix-sum the row |
// a neighbour outside the plane reads as zero
W = x > 0 ? s[y][x - 1] : 0;
N = y > 0 ? s[y - 1][x] : 0;
NW = x > 0 && y > 0 ? s[y - 1][x - 1] : 0;
// encode
r[y][x] = s[y][x] - (W + N - NW);
// decode, the same neighbours read from the output o
t[x] = r[y][x] + N - NW; // the row above is already decoded
o[y][x] = t[x] + W; // a prefix sum along the row
# The recipe writes planar and Zigzag as one fused codec, planar_zigzag.
g = geozl.graph(tile, "planar>zigzag>transpose>entropy")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)
node = geozl.lossless.Planar(tile.shape[1], planes=1)
# The recipe writes planar and Zigzag as one fused codec, planar_zigzag.
g <- geozl_graph(tile, "planar>zigzag>transpose>entropy", dtype = "int16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "int16", dim = dim(tile))
stopifnot(identical(back, tile))
# The recipe writes planar and Zigzag as one fused codec, planar_zigzag.
g = GeoZL.graph(tile, "planar>zigzag>transpose>entropy")
frame = GeoZL.compress(tile, g)
@assert GeoZL.decompress(Int16, frame, size(tile)...) == tile
04med
med is the median edge detector from JPEG-LS. When NW sits across an edge, it predicts from the neighbour on the sample's own side. Otherwise it is planar.
Use it on crisp edges between flat regions, such as field boundaries and built-up areas. It decodes one sample at a time, with no prefix sum.
| ctid | 0x72D705 |
|---|---|
| header | uint32 · row width in samples + uint32 · plane count, when above one |
| widths | 8 · 16 · 32 · 64-bit elements, read as integers |
| decode | median(W, N, W+N−NW) per sample, in order |
// prediction, from W, N and NW as unsigned integers
if (NW >= max(W, N)) p = min(W, N);
else if (NW <= min(W, N)) p = max(W, N);
else p = W + N - NW;
r = s - p; // encode
o = r + p; // decode, one sample at a time
# The recipe writes med and Zigzag as one fused codec, med_zigzag.
g = geozl.graph(tile, "med>zigzag>transpose>entropy")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)
node = geozl.lossless.Med(tile.shape[1], planes=1)
# The recipe writes med and Zigzag as one fused codec, med_zigzag.
g <- geozl_graph(tile, "med>zigzag>transpose>entropy", dtype = "int16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "int16", dim = dim(tile))
stopifnot(identical(back, tile))
# The recipe writes med and Zigzag as one fused codec, med_zigzag.
g = GeoZL.graph(tile, "med>zigzag>transpose>entropy")
frame = GeoZL.compress(tile, g)
@assert GeoZL.decompress(Int16, frame, size(tile)...) == tile
05average
average predicts each sample as the mean of W and N, so the guess always lies between them. It never overshoots at an edge, but it cannot follow a slope.
Use it on textured or noisy surfaces, where neither a plane nor a clean edge fits. Like med it decodes one sample at a time.
| ctid | 0x72D706 |
|---|---|
| header | uint32 · row width in samples + uint32 · plane count, when above one |
| widths | 8 · 16 · 32 · 64-bit elements, read as integers |
| decode | floor((W + N) / 2) per sample, in order |
// floor((W + N) / 2) without overflow
p = (W >> 1) + (N >> 1) + (W & N & 1);
r = s - p; // encode
o = r + p; // decode, one sample at a time
g = geozl.graph(tile, "average>zigzag>zstd")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)
node = geozl.lossless.Average(tile.shape[1], planes=1)
g <- geozl_graph(tile, "average>zigzag>zstd", dtype = "int16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "int16", dim = dim(tile))
stopifnot(identical(back, tile))
g = GeoZL.graph(tile, "average>zigzag>zstd")
frame = GeoZL.compress(tile, g)
@assert GeoZL.decompress(Int16, frame, size(tile)...) == tile
06wp_static
wp_static predicts W plus a weighted sum of N, NW, NE and NN, with weights fitted to each tile. The encoder keeps the fitted weights only if they beat planar.
Use it when a tile has a dominant direction that is neither horizontal nor vertical. The fit costs encode time, and one set of weights covers the whole tile.
| ctid | 0x72D707 |
|---|---|
| header | uint32 width · uint8 shift · 4×int16 coeffs + uint32 · plane count, when above one |
| widths | 8 · 16 · 32 · 64-bit elements, read as integers |
| decode | P = W + ((Σ cᵢ·tapᵢ + round) >> shift), prefix sum per row |
// weights cN, cNW, cNE, cNN and shift are in the header
rnd = shift ? 1 << (shift - 1) : 0;
K = (cN * N + cNW * NW + cNE * NE + cNN * NN + rnd) >> shift;
r = s - (W + K); // encode
o = r + W + K; // decode, K is known from the rows above
g = geozl.graph(tile, "wp_static>zigzag>zstd")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)
node = geozl.lossless.WpStatic(tile.shape[1], planes=1)
g <- geozl_graph(tile, "wp_static>zigzag>zstd", dtype = "int16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "int16", dim = dim(tile))
stopifnot(identical(back, tile))
g = GeoZL.graph(tile, "wp_static>zigzag>zstd")
frame = GeoZL.compress(tile, g)
@assert GeoZL.decompress(Int16, frame, size(tile)...) == tile