Skip to content

photometric='miniswhite' silently inverts pixel values on round-trip #1836

@brendancol

Description

@brendancol

Describe the bug

`to_geotiff(..., photometric='miniswhite')` writes the TIFF Photometric tag (value 0) but does not invert pixel values. The reader unconditionally inverts `photometric == 0` single-band data at `_reader.py:2334` (`_apply_photometric_miniswhite`) and applies it during reads at `_reader.py:2524`. The two paths are asymmetric, so a round-trip silently corrupts pixel values.

Reproduce

```python
import numpy as np, xarray as xr
from xrspatial.geotiff import open_geotiff, to_geotiff

a = np.array([[0, 255]], dtype=np.uint8)
da = xr.DataArray(a, dims=('y', 'x'),
coords={'y': [0.0], 'x': [0.0, 1.0]},
attrs={'res': (1.0, 1.0)})
to_geotiff(da, 'out.tif', photometric='miniswhite')
r = open_geotiff('out.tif')
print(r.values) # [[255 0]] -- inverted
```

For analytical rasters this is silent data corruption.

Expected behavior

Either:

  • (a) The writer inverts pixels symmetrically with the reader so the round-trip is the identity, or
  • (b) `photometric='miniswhite'` is rejected for analytical data with a clear error.

Preferred: (a). Mirror the reader inversion (`_apply_photometric_miniswhite`):

  • unsigned: `np.iinfo(dtype).max - arr`
  • float: `-arr`
  • signed/other: pass through

Additional context

Tag set on write: `_writer.py:94` (`_PHOTOMETRIC_NAME_MAP`).
Reader inversion: `_reader.py:2334` (`_apply_photometric_miniswhite`), applied at `_reader.py:2524`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions