Skip to content

fix: default SamplesPerPixel to 1 when tag is absent#319

Open
TomNicholas wants to merge 1 commit into
developmentseed:mainfrom
TomNicholas:fix/samples-per-pixel-default
Open

fix: default SamplesPerPixel to 1 when tag is absent#319
TomNicholas wants to merge 1 commit into
developmentseed:mainfrom
TomNicholas:fix/samples-per-pixel-default

Conversation

@TomNicholas

@TomNicholas TomNicholas commented Jul 18, 2026

Copy link
Copy Markdown

Fixes #318.

Problem

Parsing a TIFF that omits the SamplesPerPixel tag (277) panicked with samples_per_pixel not found, rather than applying the TIFF 6.0 spec default of 1. Single-channel grayscale TIFFs commonly omit the tag — e.g. the PerkinElmer/Phenix microscopy images in the Cell Painting Gallery. Via virtual_tiff/pyo3-async-runtimes the .expect() surfaces as an uncatchable Rust panic on the tokio worker.

Change

src/ifd.rs: default SamplesPerPixel to 1 when the tag is absent, per the spec:

let samples_per_pixel = samples_per_pixel.unwrap_or(1);

The PlanarConfiguration logic immediately below already handles samples_per_pixel == 1, so this is self-contained.

Test

Adds fixtures/other/grayscale_no_samplesperpixel.tif — a hand-built 114-byte 2x2 uint8 grayscale TIFF that omits tag 277 — and a regression test asserting it parses with samples_per_pixel() == 1. The test panics with samples_per_pixel not found without the fix and passes with it.

@ds-release-bot ds-release-bot Bot added the fix label Jul 18, 2026
The TIFF 6.0 specification defines SamplesPerPixel (tag 277) with a
default value of 1. Some single-channel grayscale TIFFs omit the tag
entirely (e.g. PerkinElmer/Phenix microscopy images in the Cell
Painting Gallery). Previously IFD parsing panicked with
'samples_per_pixel not found' on such files; now it falls back to the
spec default of 1.
@TomNicholas
TomNicholas force-pushed the fix/samples-per-pixel-default branch from 54a9195 to 747c08c Compare July 18, 2026 23:58
Comment thread fixtures/other/readme.md
defines with a default of 1. It is a regression fixture for parsing files that
omit the tag (e.g. PerkinElmer/Phenix microscopy images), which previously
panicked with "samples_per_pixel not found". It was generated by hand-writing
the TIFF header, IFD and pixel bytes with Python's `struct` module.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we:

  1. Put this image fixture in https://github.com/developmentseed/geotiff-test-data instead of here
  2. Make the image creation reproducible. You say you hand-edited the TIFF header, and it would be nice to have a record on how that was done. You can see in the existing rasterio_generated directory how there's a Python script that creates the file.

Comment thread src/ifd.rs
Comment on lines +375 to +377
// SamplesPerPixel (tag 277) defaults to 1 when the tag is absent.
// TIFF 6.0 spec, "SamplesPerPixel ... Default = 1": https://download.osgeo.org/libtiff/doc/TIFF6.pdf
// Many single-channel grayscale TIFFs (e.g. PerkinElmer/Phenix microscopy) omit it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is verbose; just say

Suggested change
// SamplesPerPixel (tag 277) defaults to 1 when the tag is absent.
// TIFF 6.0 spec, "SamplesPerPixel ... Default = 1": https://download.osgeo.org/libtiff/doc/TIFF6.pdf
// Many single-channel grayscale TIFFs (e.g. PerkinElmer/Phenix microscopy) omit it.
// Samples per pixel defaults to 1
// https://web.archive.org/web/20240329145232/https://www.awaresystems.be/imaging/tiff/tifftags/samplesperpixel.html

@TomNicholas

Copy link
Copy Markdown
Author

Sorry yes I can make those changes.

For context @ianhi and others are very interested in using VirtualiZarr on a bunch of bioscience data, lots of which is in weird TIFF variants. So this is not GeoTIFF data, and expect to see lots more edges case like this found soon. (I assume that is still within scope for this project?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic on TIFFs that omit the SamplesPerPixel tag (spec default is 1)

2 participants