Julia
- Package
GeoZLfor Julia 1.10 or newer, sources inbindings/julia.
The Julia package has the graph, compress, decompress and profile calls of the Python package and writes the same frames. It works on Julia's own integer and float arrays without conversion.
Install
using Pkg
Pkg.Registry.add(url = "https://github.com/asterisk-labs/AsteriskRegistry")
Pkg.add("GeoZL")
The package calls libgeozl directly. It loads the library named by GEOZL_LIB_PATH, then the one make lib builds in a checkout, then the artifact a release publishes for Linux x86-64 and macOS arm64, downloaded on first use.
using GeoZL
tile = [UInt16(1000 + 3 * (x - 1) + 2 * (y - 1)) for x in 1:256, y in 1:256]
rows = GeoZL.profile(tile)
g = GeoZL.graph(tile, rows[1].graph)
frame = GeoZL.compress(tile, g)
back = GeoZL.decompress(UInt16, frame, size(tile)...)
back == tile # true
Functions
The module exports only GeoZLError, so call the functions as GeoZL.graph and so on. Bad arguments throw ArgumentError. So does a request the library refuses as invalid, with ZL error code 21 or 30, such as an unknown recipe or a lossy tile outside its graph's domain. Any other refusal, such as a corrupt frame, throws GeoZLError. A library message starts with the call, as in GeoZL.compress failed (method = "planar>zigzag>pfor"), and ends with the ZL error code.
profile
GeoZL.profile(x; prior="planar", width=nothing, planes=nothing,
error=nothing, reps=5, nodata=nothing, verify=false) -> Profile
Compresses and decompresses x with every recipe prior allows and ranks them by ratio. Recipes that do not apply to the input are left out. When every recipe fails, it throws the first failure as graph would, with the text GeoZL.profile failed and the reason.
| prior | One predictor plus the id path, "planar", "med", "delta_w", "delta_n", "average", "wp_static" or "delta_1d". nothing tries every predictor and "none" only id. |
|---|---|
| reps | Round trips per recipe, an integer from 1 to 2147483647. The fastest is kept. |
| verify | true to check checksums while timing decode. |
| others | As in graph. |
Returns a Profile, best ratio first. It prints its settings as Python does, separated by ·.
rows = GeoZL.profile(tile; prior = nothing, reps = 1)
rows[1].graph # "med>zigzag>zstd"
graph
GeoZL.graph(x, method; width=nothing, planes=nothing, error=nothing,
nodata=nothing) -> Graph
Builds a reusable graph for one recipe. The graph fixes the element type and the geometry of every tile compressed with it.
| x | An array of UInt8 to UInt64, Int8 to Int64, Float16, Float32 or Float64, possibly with missing. A lossless graph also takes any other primitive element of 1, 2, 4 or 8 bytes but a pointer, such as Bool or Char, and a Complex of one. A struct is refused, since it may hold padding. For a lossy graph it fixes the domain later tiles are checked against. |
|---|---|
| method | A recipe as profile spells it, such as "planar>zigzag>pfor", without a NUL character. Python's graph lists them all. |
| width | Row length in samples, an integer from 1 to 4294967295. Defaults to size(x, 1) and is required for a vector. |
| planes | Stacked images predicted independently, an integer from 1 to 4294967295. Defaults to the last dimension of an array with three or more, otherwise 1. |
| error | nothing or 0 for lossless, a number for an absolute bound, "1%" for a relative bound, or a full LINEAR, LOG or SQRT recipe, as in Python. A float is written as Python writes it, so 2.0 gives "LINEAR:MAX_ERROR=2.0" and 1e20 gives "LINEAR:MAX_ERROR=1e+20". A lossy graph needs one of the eleven Sample types. |
| nodata | A sentinel, one real number or NaN but not a Bool. missing values in x are stored as it. NaN holes in a float raster are found without it. An integer raster cannot use NaN, a Float16 raster takes only NaN, and other elements than the eleven Sample types take none. |
Returns a Graph{T} for the element type T of x.
g = GeoZL.graph(tile, "planar>zigzag>pfor"; error = 2)
g.error # "LINEAR:MAX_ERROR=2"
g.dtype, eltype(g) # ("uint16", UInt16)
compress
GeoZL.compress(x, graph::Graph; coeffs=nothing) -> Vector{UInt8}
GeoZL.compress(x; graph::Graph, coeffs=nothing) -> Vector{UInt8}
Compresses one tile with a graph, given by position or by name, and returns the frame. The element type must be the graph's. The shape is not checked, but a row predictor refuses a tile longer than one row that is not whole rows of the graph's width with ArgumentError. A lossy tile outside the domain the graph was built from throws ArgumentError. coeffs, a vector or tuple of integer vectors, is stored in the frame header and read back with coeffs.
frame = GeoZL.compress(tile, g; coeffs = ([1, 2, 3], [4]))
plain = GeoZL.compress(tile; graph = g)
decompress
GeoZL.decompress(frame; verify=true, max_output_size=nothing) -> Vector{UInt8}
GeoZL.decompress(T, frame, dims...; nodata=nothing, verify=true,
max_output_size=nothing) -> Array{T}
Decodes a frame. Without T the result is the raw native bytes. With it, the frame decodes in place into an Array{T} shaped dims, which may also be given as a tuple. T is any element type graph takes, and a frame its size does not divide throws ArgumentError. A frame that declares more output than Julia can allocate, or decodes to fewer bytes than it declares, throws GeoZLError.
| nodata | Samples equal to this value become missing, and the result is an Array{Union{Missing,T}}. It is checked as in graph, so a Float16 frame takes only NaN. |
|---|---|
| verify | true or false, whether to check the frame checksums. |
| max_output_size | A finite number of at least 0. A frame that declares more output bytes than this throws ArgumentError. Set it for untrusted frames. |
back = GeoZL.decompress(UInt16, frame, size(tile))
coeffs
GeoZL.coeffs(frame) -> Union{Nothing,Vector{Vector{Int32}}}
Returns the coefficient vectors stored by compress, or nothing when the frame has none. Only the frame header is read.
@assert GeoZL.coeffs(frame) == [Int32[1, 2, 3], Int32[4]]
Types
| Graph | What graph returns, a Graph{T} for samples of type T, which eltype(g) returns. Its fields are method, width, planes, dtype, the name of T such as "uint16", itemsize, its bytes per sample, error and nodata. It prints as GeoZL.Graph(planar>zigzag>pfor, uint16, row width 256, 1 plane, lossless). It holds native state and is not thread safe, so build one per task. |
|---|---|
| Profile | What profile returns, a vector of named tuples with the fields graph, bytes, ratio, encode_mbps, decode_mbps and shannon_pct. It also keeps dims, dtype, the element type's name, raw_bytes, width, planes, prior, reps and error. |
| Sample | The union of the eleven element types lossy graphs and sentinels take. |
| GeoZLError | GeoZLError(code, message), thrown when the library refuses a tile or a frame other than as an invalid request. code is the ZL error code as an Int, or nothing when the refusal has none, and message carries the library's text. |
Rasters
The fastest-varying dimension is the row. An array shaped (x, y) or (x, y, band), as Rasters.jl holds a raster, gives the same frame as Python does for (y, x) or (band, y, x). Views and transposes are copied to dense samples first.
missing values are stored as nodata, or as NaN in a float raster. An integer raster with missing values needs a sentinel. A Float16 raster takes no sentinel, though NaN still works.
What it covers
The Julia package covers profile, graph, compress, decompress and coeffs. It has no OpenZL nodes like geozl.lossless and geozl.lossy, no fit_noise and no GPU decode. To share one SQRT curve across products, fit it in Python with fit_noise and pass the recipe it writes.