Skip to content

fix(geotiff): decode big endian tiles in the platform's byte order - #687

Merged
kylebarron merged 5 commits into
developmentseed:mainfrom
james-willis:jw/big-endian-decode
Oct 13, 2026
Merged

kylebarron merged 5 commits into
developmentseed:mainfrom
james-willis:jw/big-endian-decode

Conversation

@james-willis

@james-willis james-willis commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes #686.

Big endian (MM) TIFFs decoded to byte-swapped pixel values: toTypedArray views the decoded bytes in the platform's byte order, and DecoderMetadata didn't carry the file's.

Changes

  • DecoderMetadata gains an optional littleEndian (default true, so existing callers of decode() are unaffected). fetch.ts sets it from image.tiff.isLittleEndian for both data and mask tiles.
  • decode() byte-swaps samples wider than 8 bits from big endian files, before applyPredictor, since horizontal differencing (predictor 2) has to accumulate native-order integers.
  • Predictor 3 is excluded: its byte planes are most significant byte first in every file (TIFF Technical Note 3), and decodeRowFloatingPoint already reassembles them in platform order.
  • The swap writes to a new buffer, because the uncompressed decoder returns the caller's bytes, which may be shared with the block cache.
  • Codecs that return typed pixels (LERC, JPEG, WebP) don't go through this path and are unchanged.

Tests

Three unit tests in decode.test.ts build big endian tiles from known values:

  • float32, no predictor (also asserts the input buffer is untouched)
  • uint16, predictor 2
  • float32, predictor 3, decoded as both II and MM, which must agree

The first two fail on main; the third fails if the swap is applied to predictor 3. pnpm typecheck and biome check are clean.

The second commit bumps fixtures/geotiff-test-data to pick up its big endian fixtures (developmentseed/geotiff-test-data#73, #77) and adds both to integration-rasterio.test.ts:

  • uint16_1band_lzw_block128_predictor2_big_endian: fails tile pixel data matches on main, passes here.
  • float32_1band_deflate_block64_predictor3_big_endian: passes either way, and guards against swapping predictor 3 byte planes.

With pixi run generate-npy as in CI, the integration suite passes 19/19 here and fails only the uint16 big endian case against main's decode.ts / fetch.ts.

@github-actions github-actions Bot added the fix label Oct 1, 2026
@james-willis
james-willis marked this pull request as ready for review October 1, 2026 18:18
@kylebarron

Copy link
Copy Markdown
Member

Thanks!

Could you make a PR to add a big-endian TIFF to https://github.com/developmentseed/geotiff-test-data so that we have a known TIFF to test against?

@kylebarron

Copy link
Copy Markdown
Member

Thanks for the associated PRs into geotiff-test-data; now we can update the submodule here and test against those files

Bump geotiff-test-data to pick up its big endian fixtures and add them to
the rasterio integration test. On main the uint16 predictor 2 fixture
fails tile pixel data matches; the predictor 3 one passes either way,
which guards against swapping its byte planes.
@james-willis

Copy link
Copy Markdown
Contributor Author

The big endian fixtures are only in integration-rasterio.test.ts, not integration.test.ts, because geotiff.js (3.0.5) mis-decodes big endian files that use a predictor, so it can't serve as the reference for them:

  • Predictor 2: applyPredictor accumulates over a Uint16Array view (platform byte order) before samples are read with the file's byte order, so it adds byte-swapped values. For uint16_1band_lzw_block128_predictor2_big_endian, the second pixel is stored as FA 00 then FC 18 (64000, then −1000); summed as little endian and read back big endian that gives 63001 instead of 63000, and the error grows along the row (62002, …).
  • Predictor 3: decodeRowFloatingPoint reassembles each float in platform order, which is right, but geotiff.js then reads it with the file's big endian byte order, so every value comes out byte-swapped.

Big endian without a predictor is fine in geotiff.js. rasterio (GDAL) decodes both fixtures correctly, and this PR matches it.

kylebarron and others added 2 commits October 13, 2026 10:54
Every TIFF header declares its byte order, and both places that build
DecoderMetadata in fetch.ts already set it. Defaulting a missing value to
little endian meant a caller that forgot the field would silently decode
big endian files byte-swapped, the bug this branch fixes. Requiring it
turns that into a type error. Custom decoders also always receive a real
boolean.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Deflate, LZW and ZSTD decoders return a fresh buffer, so swap it in place
instead of copying it into a new one. This halves the swap time for a
512x512 tile and skips a 0.5-2 MiB allocation per tile. Only the
uncompressed decoder, which returns the caller's own bytes, still gets a
copy; canSwapBytesInPlace documents why.

Also register two fixtures that the geotiff-test-data bump added: the
little endian twin of the big endian predictor 3 fixture, and the only
predictor 2 fixture with more than one band.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kylebarron

Copy link
Copy Markdown
Member

I pushed a couple commits to

  • be cleaner in the type signature. We always know whether littleEndian is true or not; it should never be undefined
  • Swap bytes in place for performance when possible

Comment thread packages/geotiff/src/decode.ts Outdated
Comment on lines +122 to +124
// Predictor 3 byte planes are most significant byte first in every file,
// and applyPredictor reassembles them in platform order, so only the other
// layouts carry the file's byte order.

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.

Does this comment read as "if needsByteSwap is true, then Predictor 3 byte planes are most significant byte first in every file" ? I feel like perhaps the wording could be better

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.

wording updated in 11a4fa1

const needsByteSwap =
!littleEndian &&
bitsPerSample > 8 &&
predictor !== Predictor.FloatingPoint;

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.

Also it's perhaps a little messy that we're fixing byte endianness in two places...

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.

Note

This comment was written by Claude (Claude Code), not by @kylebarron.

This isn't really two places doing the same job. They handle two different storage layouts, and only one of them stores bytes in the file's byte order.

  • No predictor, or predictor 2: the decoded bytes are whole samples, each in the file's byte order. Predictor 2 adds neighboring samples together as integers, so they have to be in native order first. That's why the swap runs before applyPredictor.
  • Predictor 3: the file's byte order never comes into it. The encoder splits each value into byte planes by significance (every most significant byte in the row, then the next, down to the least significant), so the stored layout is identical in II and MM files. Undoing that has to write the bytes back into memory in some order, and decodeRowFloatingPoint writes the order the typed array needs. There's no file-order step in between to swap.

libtiff splits it the same way in tif_predict.c: predictor 2 on a byte-swapped file swaps before accumulating (swabHorAcc16/32/64), while predictor 3 turns the normal post-decode swap off:

The data should not be swapped outside of the floating point predictor, the accumulation routine should return bytes in the native order.

The one rough edge is coupling: decode.ts excludes predictor 3 because of how applyPredictor behaves, and only the comment above needsByteSwap records that. Moving the swap into applyPredictor wouldn't remove either code path, and that function would then also need to handle the no-predictor case, so I'd leave it as is.

🤖 Written by Claude Code

State the general rule first and predictor 3 as the exception, so the
comment no longer reads as if predictor 3 were the reason for the swap.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@kylebarron kylebarron 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.

Thanks!

@kylebarron
kylebarron merged commit 41c2d33 into developmentseed:main Oct 13, 2026
3 checks passed
kylebarron added a commit that referenced this pull request Oct 16, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Closes #704.

LERC tiles from big endian (`MM`) files decoded byte-swapped. libtiff
swaps samples into the file's byte order before passing them to any
codec, LERC included, so the LERC data in a big endian file holds
byte-swapped values. #687 only swapped the output of codecs that return
raw bytes.

### Changes

- `decode()` now also byte-swaps the typed pixels a codec returns when
the file is big endian and samples are wider than 8 bits. In practice
that means LERC, LERC_DEFLATE and LERC_ZSTD. JPEG and WebP output is
8-bit, so it's untouched.
- The swap happens in place. The `lerc` package copies decoded pixels
out of WASM memory into a new buffer, so nothing else references it.
- `swapBytesInPlace` now takes a `Uint8Array`, because LERC bands are
`subarray` views into one shared buffer.

### Tests

- New unit test in `decode.test.ts`: decoding the existing
`float32_1band_lerc_block32` tile as big endian must give exactly the
little endian result with each sample's bytes reversed. It fails on
`main`.
- The second commit bumps `fixtures/geotiff-test-data` to pick up its
new big endian LERC fixtures (developmentseed/geotiff-test-data#81) and
adds both to `integration-rasterio.test.ts`:
`uint16_1band_lerc_block64_big_endian` and
`float32_1band_lerc_block64_big_endian`. Both fail `tile pixel data
matches` on `main` and pass here. The bump also brings in
geotiff-test-data #78–#80, which no test here uses.
- All package tests pass, including `geotiff` (166/166) with the
rasterio integration suite, and `pnpm typecheck` and `biome check` are
clean.

🤖 Written by [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Big endian TIFFs decode to wrong pixel values (toTypedArray assumes platform byte order)

2 participants