Skip to content

fix: write ExtraSamples Tiff tag when needed - #3614

Open
dsamaey wants to merge 6 commits into
locationtech:masterfrom
dsamaey:ExtraBands-support-for-tiff
Open

fix: write ExtraSamples Tiff tag when needed#3614
dsamaey wants to merge 6 commits into
locationtech:masterfrom
dsamaey:ExtraBands-support-for-tiff

Conversation

@dsamaey

@dsamaey dsamaey commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Overview

Adds the missing ExtraSamples Tiff tag when writing GeoTIFF files. Without this ExtraSamples tag the files are actually corrupt (but most tools are lenient enough to just give a warning and guess the correct values).

Checklist

  • ./CHANGELOG.md updated, if necessary. Link to the issue if closed, otherwise the PR.
  • Unit tests added for bug-fix or new feature

Notes

Caveat : when writing RGB files with 4 bands, it is assumed that the 4th band is the alpha channel (RGBA).

Closes #3613

@pomadchin pomadchin added the bug label Jun 23, 2026
@pomadchin
pomadchin self-requested a review July 11, 2026 21:48
@pomadchin

Copy link
Copy Markdown
Member

Thanks for the PR and sorry for the delay; will review shortly!

@pomadchin pomadchin left a comment

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.

LGTM! A solid improvement; I left a couple of comments; out of main gotchas

  • ExtraSamplesTag is SHORTs
  • We don't preserve the tag not on round trips (read + write with on actions in between), it might be a good idea to make it a part of the GeoTiffOptions

Let me know what you think / or I can help once have a bit of time!

if (geoTiff.options.colorSpace == ColorSpace.RGB && geoTiff.options.rgbChannels > 3) {
bytes(0) = 2 // next band acts as alpha channel for RGB
}
fieldValues += TiffTagFieldValue(ExtraSamplesTag, BytesFieldType, extraBands, bytes)

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.

I'm reading https://www.itu.int/itudoc/itu-t/com16/tiff-fx/docs/tiff6.pdf and it should be ShortsFieldType!

if (extraBands > 0) {
val bytes = new Array[Byte](extraBands)
if (geoTiff.options.colorSpace == ColorSpace.RGB && geoTiff.options.rgbChannels > 3) {
bytes(0) = 2 // next band acts as alpha channel for RGB

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.

  • 0 - unspecified data
  • 1 - associated alpha
  • 2 - unassociated alpha


addToPurge(path)
val tiffTags = TiffTags.read(path)
val samples = tiffTags.nonBasicTags.extraSamples

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.

our tiff does not preserve those 🤔 we could put it into options on read?

addToPurge(path)
val tiffTags = TiffTags.read(path)
val samples = tiffTags.nonBasicTags.extraSamples
samples.get should equal(Array(0))

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.

^ "multi-tag.tif has 2 set for the extra samples tag


override def afterAll() = purge
override def afterAll() = {
println("done :)")

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.

a leftover? 👀

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.

Tiff tag ExtraSamples missing

2 participants