Repository navigation
fix(geotiff)!: keep each image's JPEG tables in its cached tags - #769
Conversation
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]>
| // 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, | ||
| ), | ||
| ); | ||
|
|
There was a problem hiding this comment.
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]>
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). |
| }); | ||
|
|
||
| const cachedTags = await prefetchTags(primaryImage, { signal }); | ||
| const [cachedTags] = await Promise.all([ |
There was a problem hiding this comment.
I feel like this should be
| const [cachedTags] = await Promise.all([ | |
| const [cachedTags, ...cachedSubImageTags] = await Promise.all([ |
| // 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 }), |
There was a problem hiding this comment.
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]>
| /** 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. */ |
There was a problem hiding this comment.
| /** 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. */ |
There was a problem hiding this comment.
Generally because tags like jpegTables can also exist in subimage tags, in which case they override the primary image tags, right?
| } = {}, | ||
| ): Promise<Tile> { | ||
| const tileFetch = fetchCogBytes(self, x, y, { signal }); | ||
| const tileFetch = fetchCogBytes(self, tags.image, x, y, { signal }); |
There was a problem hiding this comment.
I'm not sure about adding this as a positional argument... seems more reasonable for it to be in the options object.
| /** Fetch bytes from a COG, handling whether pixel/band interleaving. */ | ||
| async function fetchCogBytes( | ||
| self: HasTiffReference, | ||
| imageTags: CachedSubImageTags, |
There was a problem hiding this comment.
yeah, I think this should be in the named options object
|
|
||
| async function fetchBandSeparateTileBytes( | ||
| self: HasTiffReference, | ||
| imageTags: CachedSubImageTags, |
| */ | ||
| async function fetchCogBytesMultiple( | ||
| self: HasTiffReference, | ||
| imageTags: CachedSubImageTags, |
There was a problem hiding this comment.
only arguments that are fully "natural" and "obvious" should be non-named
| */ | ||
| async function fetchBandSeparateTileBytesMultiple( | ||
| self: HasTiffReference, | ||
| imageTags: CachedSubImageTags, |
There was a problem hiding this comment.
ditto should be in options object
| */ | ||
| async function getTile( | ||
| image: TiffImage, | ||
| tags: CachedSubImageTags, |
| */ | ||
| async function getBytes( | ||
| image: TiffImage, | ||
| tags: CachedSubImageTags, |
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]>
> [!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]>
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 onlyjpegTables: Uint8Array | null.CachedPrimaryImageTags(wasCachedTags): extendsCachedSubImageTags, so the primary image's tables are inGeoTIFF.cachedPrimaryImageTags(wascachedTags).Overview.cachedPrimaryImageTags(wascachedTags) still holds the primary image's tags.cachedSubImageTags. In the sheets from JPEG tiles fail with "Unable to find Jpeg header" when the image's JPEG tables weren't read with its IFD #768, the full-resolution image's tables differ from the overviews'.GeoTIFFandOverviewhavecachedMaskImageTags, which isnullwhen there's no mask.fetchTileandfetchTilestake the tags of the image and of its mask as the named optionsimageTagsandmaskImageTags. They pass them down togetBytesandgetMultipleBytes, also as options.prependJpegTablesjoins the tables and a tile the same waygetJpegHeaderdid.getBytesnow forwards onlysignalto the data source, as the batched path already did, so the tags don't reach it.getJpegHeaderthrew.Breaking changes
CachedTagsis nowCachedPrimaryImageTags.GeoTIFF.cachedTagsandOverview.cachedTagsare nowcachedPrimaryImageTags.Overviewconstructor takescachedSubImageTagsandcachedMaskImageTagsaftercachedPrimaryImageTags.Tests
tests/jpeg-tables.test.ts:rio-tiler/cog_rgb_with_stats.tifwith a 256-byte first read, which leaves both images' tables lazy.prependJpegTablesjoins 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 hasgetJpegHeader, so a read that still called it would fail.getTileexactly.packages/geotiff: 192 passed, includingintegration-rasterio.test.tswith its generated fixtures.packages/deck.gl-geotiff: 94 passed.docs, which needs typedoc sidebars that a fresh worktree doesn't have.🤖 Written by Claude Code