|
| 1 | +"""Coverage for ``write_geotiff_gpu`` compression modes. |
| 2 | +
|
| 3 | +The GPU writer documents four ``compression=`` modes: ``'zstd'`` |
| 4 | +(default, "fastest on GPU"), ``'deflate'``, ``'jpeg'`` (nvJPEG with |
| 5 | +Pillow fallback), and ``'none'``. The existing test suite exercises |
| 6 | +only ``'none'`` and ``'deflate'`` with direct round-trip assertions. |
| 7 | +
|
| 8 | +* ``'zstd'`` is the default and is hit implicitly by tests that omit |
| 9 | + ``compression=``, but no test asserts pixel fidelity for the zstd |
| 10 | + path. A regression in the nvCOMP zstd encoder (or in the writer's |
| 11 | + zstd codec-tag wiring) would not surface against the implicit |
| 12 | + callers because they only assert metadata-level properties. |
| 13 | +
|
| 14 | +* ``'jpeg'`` routes to ``_nvjpeg_batch_encode`` with a CPU Pillow |
| 15 | + fallback. Neither code path is exercised through ``write_geotiff_gpu`` |
| 16 | + anywhere else in the suite. ``to_geotiff(compression='jpeg')`` |
| 17 | + rejects the CPU path with the JPEGTables interop error, so the only |
| 18 | + way to reach the GPU JPEG encoder via the public API is through |
| 19 | + ``write_geotiff_gpu``. |
| 20 | +
|
| 21 | +This module closes the Cat 4 HIGH parameter-coverage gap by pinning a |
| 22 | +round-trip test for each documented mode (zstd, deflate, jpeg, none) |
| 23 | +plus a parametrised TIFF compression-tag check that the file header |
| 24 | +advertises the right codec. |
| 25 | +""" |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +import importlib.util |
| 29 | + |
| 30 | +import numpy as np |
| 31 | +import pytest |
| 32 | +import xarray as xr |
| 33 | + |
| 34 | +from xrspatial.geotiff import ( |
| 35 | + open_geotiff, |
| 36 | + write_geotiff_gpu, |
| 37 | +) |
| 38 | +from xrspatial.geotiff._header import parse_header, parse_ifd |
| 39 | + |
| 40 | + |
| 41 | +def _gpu_available() -> bool: |
| 42 | + if importlib.util.find_spec("cupy") is None: |
| 43 | + return False |
| 44 | + try: |
| 45 | + import cupy |
| 46 | + return bool(cupy.cuda.is_available()) |
| 47 | + except Exception: |
| 48 | + return False |
| 49 | + |
| 50 | + |
| 51 | +_HAS_GPU = _gpu_available() |
| 52 | +_gpu_only = pytest.mark.skipif(not _HAS_GPU, reason="cupy + CUDA required") |
| 53 | + |
| 54 | + |
| 55 | +# Compression-tag IDs from the TIFF specification, mirroring the table |
| 56 | +# in ``_writer._compression_tag``. Pinned here so an accidental change |
| 57 | +# to the codec-tag wiring is caught. |
| 58 | +_TIFF_COMPRESSION_TAG = 259 |
| 59 | +_COMPRESSION_TAGS = { |
| 60 | + 'none': 1, |
| 61 | + 'deflate': 8, |
| 62 | + 'jpeg': 7, |
| 63 | + 'zstd': 50000, |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +def _read_compression_tag(path: str) -> int: |
| 68 | + """Return the TIFF Compression (tag 259) value from *path*.""" |
| 69 | + with open(path, 'rb') as f: |
| 70 | + data = f.read() |
| 71 | + hdr = parse_header(data) |
| 72 | + ifd = parse_ifd(data, hdr.first_ifd_offset, hdr) |
| 73 | + entry = ifd.entries[_TIFF_COMPRESSION_TAG] |
| 74 | + val = entry.value |
| 75 | + # value is either an int scalar or a 1-tuple depending on count; |
| 76 | + # the TIFF spec allows count=1 to be inlined. |
| 77 | + if isinstance(val, (tuple, list)): |
| 78 | + return int(val[0]) |
| 79 | + return int(val) |
| 80 | + |
| 81 | + |
| 82 | +def _make_int_da(h=64, w=64, dtype=np.int32): |
| 83 | + """Build a deterministic CuPy-backed DataArray for lossless codecs.""" |
| 84 | + import cupy |
| 85 | + arr = (np.arange(h * w, dtype=np.int64) % 1000).astype(dtype).reshape(h, w) |
| 86 | + return xr.DataArray( |
| 87 | + cupy.asarray(arr), |
| 88 | + dims=('y', 'x'), |
| 89 | + coords={'y': np.arange(h), 'x': np.arange(w)}, |
| 90 | + ), arr |
| 91 | + |
| 92 | + |
| 93 | +def _make_rgb_uint8_da(h=64, w=64, seed=0): |
| 94 | + """Build a CuPy-backed uint8 3-band DataArray for JPEG.""" |
| 95 | + import cupy |
| 96 | + rng = np.random.default_rng(seed) |
| 97 | + arr = rng.integers(0, 256, size=(h, w, 3), dtype=np.uint8) |
| 98 | + return xr.DataArray( |
| 99 | + cupy.asarray(arr), |
| 100 | + dims=('y', 'x', 'band'), |
| 101 | + coords={'y': np.arange(h), 'x': np.arange(w), 'band': [1, 2, 3]}, |
| 102 | + ), arr |
| 103 | + |
| 104 | + |
| 105 | +# --------------------------------------------------------------------------- |
| 106 | +# Cat 4 HIGH: zstd is the documented default, never round-tripped explicitly |
| 107 | +# --------------------------------------------------------------------------- |
| 108 | + |
| 109 | +@_gpu_only |
| 110 | +def test_write_geotiff_gpu_zstd_roundtrip(tmp_path): |
| 111 | + """Default ``compression='zstd'`` round-trips pixel-exact. |
| 112 | +
|
| 113 | + The GPU writer advertises zstd as the fastest GPU codec and uses it |
| 114 | + as the default. nvCOMP zstd is lossless, so the read-back must |
| 115 | + equal the input bit-for-bit. |
| 116 | + """ |
| 117 | + da, arr = _make_int_da() |
| 118 | + path = str(tmp_path / "zstd_roundtrip.tif") |
| 119 | + |
| 120 | + write_geotiff_gpu(da, path, compression='zstd') |
| 121 | + |
| 122 | + out = open_geotiff(path) |
| 123 | + np.testing.assert_array_equal(out.values, arr) |
| 124 | + assert out.dtype == arr.dtype |
| 125 | + |
| 126 | + |
| 127 | +@_gpu_only |
| 128 | +def test_write_geotiff_gpu_zstd_default_matches_explicit(tmp_path): |
| 129 | + """Omitting ``compression=`` defaults to zstd; bytes must match an |
| 130 | + explicit ``compression='zstd'`` call. |
| 131 | +
|
| 132 | + Pins the default so a silent change to the default codec (eg. to |
| 133 | + 'deflate') would fail this test. |
| 134 | + """ |
| 135 | + da, _ = _make_int_da() |
| 136 | + default_path = str(tmp_path / "default.tif") |
| 137 | + explicit_path = str(tmp_path / "explicit_zstd.tif") |
| 138 | + |
| 139 | + write_geotiff_gpu(da, default_path) |
| 140 | + write_geotiff_gpu(da, explicit_path, compression='zstd') |
| 141 | + |
| 142 | + # Both files must advertise zstd in the TIFF header. |
| 143 | + assert _read_compression_tag(default_path) == _COMPRESSION_TAGS['zstd'] |
| 144 | + assert _read_compression_tag(explicit_path) == _COMPRESSION_TAGS['zstd'] |
| 145 | + |
| 146 | + |
| 147 | +# --------------------------------------------------------------------------- |
| 148 | +# Cat 4 HIGH: jpeg is documented but never round-tripped |
| 149 | +# --------------------------------------------------------------------------- |
| 150 | + |
| 151 | +@_gpu_only |
| 152 | +def test_write_geotiff_gpu_jpeg_rgb_roundtrip(tmp_path): |
| 153 | + """``compression='jpeg'`` round-trips a 3-band uint8 RGB raster. |
| 154 | +
|
| 155 | + JPEG is lossy so we tolerate a moderate per-pixel error budget but |
| 156 | + require the mean error to stay within typical JPEG quality bounds |
| 157 | + (well under 50 for default-quality 8-bit). |
| 158 | + """ |
| 159 | + da, arr = _make_rgb_uint8_da() |
| 160 | + path = str(tmp_path / "jpeg_rgb.tif") |
| 161 | + |
| 162 | + write_geotiff_gpu(da, path, compression='jpeg') |
| 163 | + |
| 164 | + out = open_geotiff(path) |
| 165 | + assert out.shape == arr.shape |
| 166 | + assert out.dtype == arr.dtype |
| 167 | + diff = np.abs(out.values.astype(np.int32) - arr.astype(np.int32)) |
| 168 | + # Random uint8 is the worst case for JPEG; we just want to catch a |
| 169 | + # codec that emits all-zero or all-255 output rather than measure |
| 170 | + # quality. Mean-abs-diff below 50 is comfortable for default quality. |
| 171 | + assert diff.mean() < 50, ( |
| 172 | + f"JPEG round-trip mean diff {diff.mean()} suggests encoder/decoder break" |
| 173 | + ) |
| 174 | + |
| 175 | + |
| 176 | +@_gpu_only |
| 177 | +def test_write_geotiff_gpu_jpeg_uint8_single_band_roundtrip(tmp_path): |
| 178 | + """``compression='jpeg'`` round-trips a 1-band uint8 (greyscale) |
| 179 | + raster. |
| 180 | +
|
| 181 | + Single-band JPEG exercises a different nvJPEG path (luminance-only |
| 182 | + vs. RGB) and the Pillow fallback's monochrome branch. |
| 183 | + """ |
| 184 | + import cupy |
| 185 | + rng = np.random.default_rng(0) |
| 186 | + arr = rng.integers(0, 256, size=(64, 64), dtype=np.uint8) |
| 187 | + da = xr.DataArray( |
| 188 | + cupy.asarray(arr), |
| 189 | + dims=('y', 'x'), |
| 190 | + coords={'y': np.arange(64), 'x': np.arange(64)}, |
| 191 | + ) |
| 192 | + path = str(tmp_path / "jpeg_mono.tif") |
| 193 | + |
| 194 | + write_geotiff_gpu(da, path, compression='jpeg') |
| 195 | + |
| 196 | + out = open_geotiff(path) |
| 197 | + assert out.shape == arr.shape |
| 198 | + assert out.dtype == arr.dtype |
| 199 | + diff = np.abs(out.values.astype(np.int32) - arr.astype(np.int32)) |
| 200 | + assert diff.mean() < 50 |
| 201 | + |
| 202 | + |
| 203 | +# --------------------------------------------------------------------------- |
| 204 | +# Cat 4 MEDIUM: compression-tag header check across all documented modes |
| 205 | +# --------------------------------------------------------------------------- |
| 206 | + |
| 207 | +@_gpu_only |
| 208 | +@pytest.mark.parametrize("compression", ['none', 'deflate', 'zstd']) |
| 209 | +def test_write_geotiff_gpu_compression_tag(tmp_path, compression): |
| 210 | + """The TIFF Compression tag in the output matches the requested |
| 211 | + codec. |
| 212 | +
|
| 213 | + A regression that wired the writer to a different codec tag would |
| 214 | + produce files that decode correctly through the internal reader |
| 215 | + (it inspects the same wired tag) but break interop with GDAL / |
| 216 | + rasterio / libtiff. |
| 217 | + """ |
| 218 | + da, _ = _make_int_da() |
| 219 | + path = str(tmp_path / f"compression_tag_{compression}.tif") |
| 220 | + |
| 221 | + write_geotiff_gpu(da, path, compression=compression) |
| 222 | + |
| 223 | + assert _read_compression_tag(path) == _COMPRESSION_TAGS[compression] |
| 224 | + |
| 225 | + |
| 226 | +@_gpu_only |
| 227 | +def test_write_geotiff_gpu_jpeg_compression_tag(tmp_path): |
| 228 | + """The JPEG compression tag (7) is written for uint8 RGB input.""" |
| 229 | + da, _ = _make_rgb_uint8_da() |
| 230 | + path = str(tmp_path / "jpeg_tag.tif") |
| 231 | + |
| 232 | + write_geotiff_gpu(da, path, compression='jpeg') |
| 233 | + |
| 234 | + assert _read_compression_tag(path) == _COMPRESSION_TAGS['jpeg'] |
| 235 | + |
| 236 | + |
| 237 | +# --------------------------------------------------------------------------- |
| 238 | +# Cat 4 MEDIUM: explicit deflate round-trip (already covered indirectly |
| 239 | +# but no test in the suite asserts pixel equality on the GPU writer |
| 240 | +# deflate path with a non-COG/non-overview layout). |
| 241 | +# --------------------------------------------------------------------------- |
| 242 | + |
| 243 | +@_gpu_only |
| 244 | +def test_write_geotiff_gpu_deflate_roundtrip(tmp_path): |
| 245 | + """``compression='deflate'`` round-trips pixel-exact for the plain |
| 246 | + (non-COG) GPU writer path. |
| 247 | +
|
| 248 | + The existing deflate coverage on the GPU writer runs through the |
| 249 | + COG path or through NaN-sentinel scenarios. This test pins the |
| 250 | + plain tiled-deflate layout against a deterministic integer raster. |
| 251 | + """ |
| 252 | + da, arr = _make_int_da() |
| 253 | + path = str(tmp_path / "deflate_plain.tif") |
| 254 | + |
| 255 | + write_geotiff_gpu(da, path, compression='deflate') |
| 256 | + |
| 257 | + out = open_geotiff(path) |
| 258 | + np.testing.assert_array_equal(out.values, arr) |
| 259 | + assert _read_compression_tag(path) == _COMPRESSION_TAGS['deflate'] |
| 260 | + |
| 261 | + |
| 262 | +# --------------------------------------------------------------------------- |
| 263 | +# Cat 4 MEDIUM: none / uncompressed round-trip |
| 264 | +# --------------------------------------------------------------------------- |
| 265 | + |
| 266 | +@_gpu_only |
| 267 | +def test_write_geotiff_gpu_none_roundtrip(tmp_path): |
| 268 | + """``compression='none'`` round-trips pixel-exact. |
| 269 | +
|
| 270 | + The GPU writer still chunks the image into tile buffers even when |
| 271 | + no codec is applied; this test pins that the no-codec assembly |
| 272 | + path emits a valid, readable file. |
| 273 | + """ |
| 274 | + da, arr = _make_int_da() |
| 275 | + path = str(tmp_path / "none_plain.tif") |
| 276 | + |
| 277 | + write_geotiff_gpu(da, path, compression='none') |
| 278 | + |
| 279 | + out = open_geotiff(path) |
| 280 | + np.testing.assert_array_equal(out.values, arr) |
| 281 | + assert _read_compression_tag(path) == _COMPRESSION_TAGS['none'] |
| 282 | + |
| 283 | + |
| 284 | +# --------------------------------------------------------------------------- |
| 285 | +# Cross-codec parity: pixel-exact for lossless codecs |
| 286 | +# --------------------------------------------------------------------------- |
| 287 | + |
| 288 | +@_gpu_only |
| 289 | +def test_write_geotiff_gpu_lossless_codecs_agree(tmp_path): |
| 290 | + """zstd / deflate / none must produce pixel-identical read-backs. |
| 291 | +
|
| 292 | + The codecs are lossless, so for the same input the decoded |
| 293 | + pixel arrays must match exactly. Catches regressions where a codec |
| 294 | + path silently corrupts data (eg. wrong predictor wiring). |
| 295 | + """ |
| 296 | + da, arr = _make_int_da() |
| 297 | + paths = { |
| 298 | + codec: str(tmp_path / f"parity_{codec}.tif") |
| 299 | + for codec in ('none', 'deflate', 'zstd') |
| 300 | + } |
| 301 | + for codec, path in paths.items(): |
| 302 | + write_geotiff_gpu(da, path, compression=codec) |
| 303 | + |
| 304 | + reads = {codec: open_geotiff(path).values for codec, path in paths.items()} |
| 305 | + |
| 306 | + np.testing.assert_array_equal(reads['none'], arr) |
| 307 | + np.testing.assert_array_equal(reads['deflate'], reads['none']) |
| 308 | + np.testing.assert_array_equal(reads['zstd'], reads['none']) |
0 commit comments