Skip to content

fix(geotiff)!: keep each image's JPEG tables in its cached tags - #769

Merged
kylebarron merged 4 commits into
mainfrom
kyle/preload-jpeg-tables
Oct 22, 2026
Merged

kylebarron merged 4 commits into
mainfrom
kyle/preload-jpeg-tables

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Note

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

Closes #768

Tile reads got each image's JPEG tables from cogeotiff's tag state, through TiffImage.getJpegHeader. That fails when the JpegTables tag wasn't in the IFD's first read, because nothing fetched it (see #768). Now the tables are read when the GeoTIFF opens and kept as data in the cached tags, and tile reads use those.

Change

  • CachedSubImageTags (new): the tags that can differ between a file's images. For now that's only jpegTables: Uint8Array | null.
  • CachedPrimaryImageTags (was CachedTags): extends CachedSubImageTags, so the primary image's tables are in GeoTIFF.cachedPrimaryImageTags (was cachedTags).
  • Overviews:
  • Masks: GeoTIFF and Overview have cachedMaskImageTags, which is null when there's no mask.
  • Reads:
    • fetchTile and fetchTiles take the tags of the image and of its mask as the named options imageTags and maskImageTags. They pass them down to getBytes and getMultipleBytes, also as options.
    • A new prependJpegTables joins the tables and a tile the same way getJpegHeader did.
    • getBytes now forwards only signal to the data source, as the batched path already did, so the tags don't reach it.
  • JPEG images without a JpegTables tag: their tiles now pass through unchanged, since each one is then a complete JPEG. getJpegHeader threw.
  • Cost: nothing when the tag was in the first read or the image has no tables. When the tag is lazy, one small header read per image at open.

Breaking changes

  • CachedTags is now CachedPrimaryImageTags.
  • GeoTIFF.cachedTags and Overview.cachedTags are now cachedPrimaryImageTags.
  • The Overview constructor takes cachedSubImageTags and cachedMaskImageTags after cachedPrimaryImageTags.

Tests

  • tests/jpeg-tables.test.ts:
    • Opens rio-tiler/cog_rgb_with_stats.tif with a 256-byte first read, which leaves both images' tables lazy.
    • The fixture's two images share their tables, so the test changes the overview's in memory first.
    • Checks that each image's tables are stored, and that a tile read from each image starts with that image's own tables.
    • Checks that prependJpegTables joins a tile with the tables, and passes a tile through when there are none.
  • tests/get-multiple-bytes.test.ts: passes the tables explicitly. The mock image no longer has getJpegHeader, so a read that still called it would fail.
  • Real data: read tile (0, 0) from every level of the 16 USGS sheets near Delta, CO from JPEG tiles fail with "Unable to find Jpeg header" when the image's JPEG tables weren't read with its IFD #768.
    • Every read succeeds.
    • The joined bytes match cogeotiff's own getTile exactly.
    • All 128 tiles decode with Pillow.
  • packages/geotiff: 192 passed, including integration-rasterio.test.ts with its generated fixtures.
  • packages/deck.gl-geotiff: 94 passed.
  • Typecheck: passes for every package except docs, which needs typedoc sidebars that a fresh worktree doesn't have.
  • Biome: clean.

🤖 Written by Claude Code

A JPEG tile read prepends its image's JPEG tables through cogeotiff's
getJpegHeader, which throws "Unable to find Jpeg header" when the
JpegTables tag is lazy. cogeotiff reads a tag with its IFD only when the
tag's bytes were in that read, and nothing fetched it afterwards:
prefetchTags skips it, and runs for the primary image only. So any JPEG
image whose tables sat past that read, often an overview, failed every
tile read.

Fetch JpegTables for every JPEG-compressed image in GeoTIFF.fromTiff.

Closes #768

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the fix label Oct 21, 2026
Comment thread packages/geotiff/src/geotiff.ts Outdated
Comment on lines +209 to +221
// Every JPEG tile read prepends its image's JPEG tables, and
// `getJpegHeader` throws if they aren't loaded. cogeotiff only reads a tag
// with its IFD when the tag's bytes were in that first read; otherwise the
// tag stays lazy until fetched. So load the tables for each JPEG image
// now.
await Promise.all(
images.map((image) =>
image.value(TiffTag.Compression) === Compression.Jpeg
? image.fetch(TiffTag.JpegTables, { signal })
: null,
),
);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@kylebarron

Copy link
Copy Markdown
Member Author

GeoTIFF.fromTiff now fetches the JpegTables tag (347) for every JPEG-compressed image, the primary and each overview, right after image.init():

there's a separate jpeg table for each image but not each tile? Is that right? How much data is this?

…ageTags

Move the JpegTables fetch out of GeoTIFF.fromTiff into ifd.ts, next to
the other tag prefetching. prefetchTags loads the primary image's
tables, and a new prefetchSubImageTags loads what each sub-image (an
overview or a mask) needs for its own tile reads, which for now is only
its JPEG tables. fromTiff runs it for every non-primary image alongside
prefetchTags.

The tables aren't returned in CachedTags. Overviews share the primary
image's CachedTags, but JPEG tables belong to each image and can differ
between them: in the USGS sheet from #768, the full-resolution image's
tables differ from its overviews'.

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

Copy link
Copy Markdown
Member Author

GeoTIFF.fromTiff now fetches the JpegTables tag (347) for every JPEG-compressed image, the primary and each overview, right after image.init():

there's a separate jpeg table for each image but not each tile? Is that right? How much data is this?

From claude:

Confirmed: in North Delta, the full-resolution image uses different JPEG quantization tables than the overviews (same 574-byte length but different SHA1 hashes).

Comment thread packages/geotiff/src/geotiff.ts Outdated
});

const cachedTags = await prefetchTags(primaryImage, { signal });
const [cachedTags] = await Promise.all([

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I feel like this should be

Suggested change
const [cachedTags] = await Promise.all([
const [cachedTags, ...cachedSubImageTags] = await Promise.all([

Comment thread packages/geotiff/src/ifd.ts Outdated
// Loaded but not returned. Each image has its own JPEG tables, which can
// differ between a file's images, and tile reads take them from the image
// itself. See `prefetchSubImageTags`.
image.fetch(TiffTag.JpegTables, { signal }),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why not return it in cachedTags? It'll still be nullable, but we can assure that it's non-nullable for any jpeg-compressed image

Tile reads took each image's JPEG tables from cogeotiff's tag state through
`TiffImage.getJpegHeader`, which works only after something has fetched the
tag. Store the tables as data when opening, and pass them to the reads:

- `CachedTags` is now `CachedPrimaryImageTags`, which extends the new
  `CachedSubImageTags` (`jpegTables`). `GeoTIFF.cachedTags` and
  `Overview.cachedTags` are now `cachedPrimaryImageTags`.
- Each Overview keeps its own `cachedSubImageTags`, and GeoTIFF and Overview
  keep `cachedMaskImageTags` for their masks.
- `prependJpegTables` joins an image's tables and a tile, as `getJpegHeader`
  did. An image without tables now passes its tiles through unchanged,
  rather than throwing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kylebarron kylebarron changed the title fix(geotiff): load JPEG tables for every JPEG image at open fix(geotiff)!: keep each image's JPEG tables in its cached tags Oct 21, 2026
Comment thread packages/geotiff/src/fetch.ts Outdated
/** Protocol for objects that hold a TIFF reference and can request tiles. */
interface HasTiffReference extends HasTransform {
readonly cachedTags: CachedTags;
/** Tags of the file's primary image, which apply to every overview too. */

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Suggested change
/** Tags of the file's primary image, which apply to every overview too. */
/** Tags of the file's primary image, which generally apply to every overview too. */

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Generally because tags like jpegTables can also exist in subimage tags, in which case they override the primary image tags, right?

Comment thread packages/geotiff/src/fetch.ts Outdated
} = {},
): Promise<Tile> {
const tileFetch = fetchCogBytes(self, x, y, { signal });
const tileFetch = fetchCogBytes(self, tags.image, x, y, { signal });

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure about adding this as a positional argument... seems more reasonable for it to be in the options object.

Comment thread packages/geotiff/src/fetch.ts Outdated
/** Fetch bytes from a COG, handling whether pixel/band interleaving. */
async function fetchCogBytes(
self: HasTiffReference,
imageTags: CachedSubImageTags,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah, I think this should be in the named options object

Comment thread packages/geotiff/src/fetch.ts Outdated

async function fetchBandSeparateTileBytes(
self: HasTiffReference,
imageTags: CachedSubImageTags,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ditto

Comment thread packages/geotiff/src/fetch.ts Outdated
*/
async function fetchCogBytesMultiple(
self: HasTiffReference,
imageTags: CachedSubImageTags,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ditto

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

only arguments that are fully "natural" and "obvious" should be non-named

Comment thread packages/geotiff/src/fetch.ts Outdated
*/
async function fetchBandSeparateTileBytesMultiple(
self: HasTiffReference,
imageTags: CachedSubImageTags,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ditto should be in options object

Comment thread packages/geotiff/src/fetch.ts Outdated
*/
async function getTile(
image: TiffImage,
tags: CachedSubImageTags,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ditto

Comment thread packages/geotiff/src/fetch.ts Outdated
*/
async function getBytes(
image: TiffImage,
tags: CachedSubImageTags,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ditto

Only arguments whose meaning is obvious should be positional. Move the
`CachedSubImageTags` for the image and its mask into the options objects of
`fetchTile`, `fetchTiles`, and the readers they call, as `imageTags` and
`maskImageTags`.

`getBytes` now forwards only `signal` to the data source, as the batched
path already does, so the tags don't reach it.

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

@kylebarron kylebarron left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM; thanks

@kylebarron
kylebarron merged commit faf1607 into main Oct 22, 2026
4 checks passed
@kylebarron
kylebarron deleted the kyle/preload-jpeg-tables branch October 22, 2026 03:21
kylebarron added a commit that referenced this pull request Oct 22, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Bumps `version` to `0.9.0-beta.3` in the root `package.json` and in
every `packages/*/package.json`, ready for a third beta release. The
publish workflow sees `beta` in the version and publishes to npm under
the `beta` dist-tag, so `latest` stays on 0.8.1.

`CHANGELOG.md` gets a `v0.9.0-beta.3` section listing the 16 PRs merged
since v0.9.0-beta.2. Two of them change the API in ways that break
existing code:

- #769 renames `CachedTags` to `CachedPrimaryImageTags` in
`@developmentseed/geotiff`, and `cachedTags` to `cachedPrimaryImageTags`
on `GeoTIFF` and `Overview`.
- #760 isn't marked breaking, but `PROJECTION_REGISTRY` in
`@developmentseed/proj` now holds `Promise<ProjectionDefinition>`
values, so code that sets entries directly should call
`registerProjection` instead. `@developmentseed/proj` also depends on
proj4 now.

Once this is merged, tag the merge commit `v0.9.0-beta.3` and push the
tag.

🤖 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JPEG tiles fail with "Unable to find Jpeg header" when the image's JPEG tables weren't read with its IFD

1 participant