GeoZL

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 six predictors by the neighbours they read. delta_w reads W, delta_n reads N, planar W + N - NW, med the median of W, N and the planar guess, average the mean of W and N, wp_static W plus fitted weights over N, NW, NE and NN. delta_w, delta_n, planar and wp_static decode a whole row at once. Med and average decode one sample at a time.
The black cell is the sample being predicted. Green rebuild a whole row at once, yellow one sample at a time.

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.

delta_w rewrites each value as a difference from its west neighbour. The first value of the row is a seed
each row starts from one whole value and then stores only steps

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.

ctid0x72D701
headeruint32 · row width in samples
widths8 · 16 · 32 · 64-bit elements, read as integers
decoderᵢ + 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

Source in core/src/delta_w

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.

delta_n rewrites each value as a difference from its north neighbour. The first row is a seed and columns are independent
the first row is kept whole and every column runs on its own

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.

ctid0x72D702
headeruint32 · row width in samples
+ uint32 · plane count, when above one
widths8 · 16 · 32 · 64-bit elements, read as integers
decoderᵢ + 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

Source in core/src/delta_n

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.

planar predicts a pixel from the plane through its west, north and north-west neighbours. P equals W plus N minus NW
W + N − NW continues the local slope, so any plane predicts with zero residual

Try planar first. Most recipes start with it. It overshoots sharp edges, where med does better.

ctid0x72D703
headeruint32 · row width in samples
+ uint32 · plane count, when above one
widths8 · 16 · 32 · 64-bit elements, read as integers
decodefold 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

Source in core/src/planar

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.

med is the median edge detector. Because NW sits above max of W and N, it predicts the minimum, clamping to the near side of the edge
NW above both neighbours points to an edge, so med takes the lower one

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.

ctid0x72D705
headeruint32 · row width in samples
+ uint32 · plane count, when above one
widths8 · 16 · 32 · 64-bit elements, read as integers
decodemedian(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

Source in core/src/med

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.

average predicts each pixel from the floor average of its west and north neighbours
the guess always lies between W and N

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.

ctid0x72D706
headeruint32 · row width in samples
+ uint32 · plane count, when above one
widths8 · 16 · 32 · 64-bit elements, read as integers
decodefloor((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

Source in core/src/average

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.

wp_static predicts a pixel as W plus a weighted kernel over N, NW, NE and NN, using fixed weights carried in the header
W plus a kernel over the rows above, with weights carried in the header

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.

ctid0x72D707
headeruint32 width · uint8 shift · 4×int16 coeffs
+ uint32 · plane count, when above one
widths8 · 16 · 32 · 64-bit elements, read as integers
decodeP = 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

Source in core/src/wp_static