Repository navigation
fix(geotiff): decode big endian tiles in the platform's byte order - #687
Conversation
7992696 to
7142d95
Compare
|
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? |
|
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.
7142d95 to
2596766
Compare
|
The big endian fixtures are only in
Big endian without a predictor is fine in geotiff.js. rasterio (GDAL) decodes both fixtures correctly, and this PR matches it. |
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]>
|
I pushed a couple commits to
|
| // 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. |
There was a problem hiding this comment.
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
| const needsByteSwap = | ||
| !littleEndian && | ||
| bitsPerSample > 8 && | ||
| predictor !== Predictor.FloatingPoint; |
There was a problem hiding this comment.
Also it's perhaps a little messy that we're fixing byte endianness in two places...
There was a problem hiding this comment.
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
IIandMMfiles. Undoing that has to write the bytes back into memory in some order, anddecodeRowFloatingPointwrites 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]>
> [!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]>
Closes #686.
Big endian (
MM) TIFFs decoded to byte-swapped pixel values:toTypedArrayviews the decoded bytes in the platform's byte order, andDecoderMetadatadidn't carry the file's.Changes
DecoderMetadatagains an optionallittleEndian(defaulttrue, so existing callers ofdecode()are unaffected).fetch.tssets it fromimage.tiff.isLittleEndianfor both data and mask tiles.decode()byte-swaps samples wider than 8 bits from big endian files, beforeapplyPredictor, since horizontal differencing (predictor 2) has to accumulate native-order integers.decodeRowFloatingPointalready reassembles them in platform order.Tests
Three unit tests in
decode.test.tsbuild big endian tiles from known values:IIandMM, which must agreeThe first two fail on main; the third fails if the swap is applied to predictor 3.
pnpm typecheckandbiome checkare clean.The second commit bumps
fixtures/geotiff-test-datato pick up its big endian fixtures (developmentseed/geotiff-test-data#73, #77) and adds both tointegration-rasterio.test.ts:uint16_1band_lzw_block128_predictor2_big_endian: failstile pixel data matcheson 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-npyas in CI, the integration suite passes 19/19 here and fails only the uint16 big endian case against main'sdecode.ts/fetch.ts.