Skip to content

feat: Expose API to find byte range of an internal COG tile - #261

Merged
kylebarron merged 8 commits into
mainfrom
kyle/refactor-tile-byte-ranges
Mar 3, 2026
Merged

kylebarron merged 8 commits into
mainfrom
kyle/refactor-tile-byte-ranges

Conversation

@kylebarron

@kylebarron kylebarron commented Mar 2, 2026 •

Copy link
Copy Markdown
Member

Change list

  • Add TileByteRange and TilesByteRanges enums (over chunky and planar variants) that hold various byte ranges
  • Add into_fetch private methods for each of those that manage calling the underlying reader's get_byte_range / get_byte_ranges calls
  • This makes the body of fetch_tile and fetch_tiles very small.
  • We also deduplicate the verbose creation of Tile by adding CompressedBytes::into_tile

The last commit also exposed them to Python where:

  • ifd.tile_byte_range(x, y) returns either (start, end) or list[(start, end)]

We could switch to some sort of discriminated union, or, perhaps it's best to only expose to Python the single-tile byte range, not the multi-tile byte range.

We also still need to update the type hints in the pyi

@kylebarron kylebarron changed the title Kyle/refactor-tile-byte-ranges refactor: Expose byte range APIs Mar 2, 2026
@kylebarron
kylebarron requested review from maxrjones and weiji14 March 2, 2026 21:02
Comment thread src/ifd.rs

impl CompressedBytes {
fn into_tile(self, x: usize, y: usize, ifd: &ImageFileDirectory) -> Tile {
let data_type = DataType::from_tags(&ifd.sample_format, &ifd.bits_per_sample);

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.

A downside here is that after this refactor we call DataType::from_tags and PredictorInfo::from_ifd once per tile instead of just once. These are simple functions so it shouldn't be much of a difference, and it's worth it for this cleaner code. In the future we could clean it up further

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.

At least it will handle the super rare case where each ifd has a separate dtype 😆

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.

Well previously it was once per request (amortized across multiple tiles in fetch_tiles), now it's per-tile.

Comment thread src/ifd.rs

impl CompressedBytes {
fn into_tile(self, x: usize, y: usize, ifd: &ImageFileDirectory) -> Tile {
let data_type = DataType::from_tags(&ifd.sample_format, &ifd.bits_per_sample);

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.

At least it will handle the super rare case where each ifd has a separate dtype 😆

Comment thread python/src/ifd.rs Outdated
Comment on lines +486 to +492
fn tiles_byte_ranges(&self, xy: Vec<(usize, usize)>) -> PyAsyncTiffResult<PyTilesByteRanges> {
let byte_ranges = self
.ifd
.tiles_byte_ranges(&xy)
.ok_or(PyValueError::new_err("Not a tiled tiff"))?;
Ok(PyTilesByteRanges(byte_ranges))
}

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 guess a question for @maxrjones, does virtual-tiff need the merged/coalesced byte ranges from multiple tiles? Or just the (start, end) / (offset, length) for each tile? Also not a fan of the list[list[(start, end)]] nesting 🙂

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 would like to be able to batch chunk (in zarr land) requests to get the byte ranges associated with multiple tiles (in tiff land). I wasn't expecting those to be merged/coalesced.

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.

Oh sorry, I don't think there's merging/coalescing here. Just a list of the byte ranges (or a list of list of byte ranges in the case of planar config).

@maxrjones maxrjones Mar 2, 2026 •

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 would use the list of byte ranges, but could also work around its absence. I find the dynamic return type more confusing than the alternative of two functions (e.g., #257 (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.

I find the dynamic return type more confusing than the alternative of two functions

Well, here in the Python API we're emulating a Rust enum. I like the single-function approach because it doesn't require the caller to know/check in advance what interleaving the image is using. We could make the return type have two different classes, or dicts.

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.

This PR also changes the Rust side to only have a single helper function, which is much cleaner on the Rust side than having these two parallel functions. So following from that decision we just need to figure out the best Python API

Comment thread src/ifd.rs Resolved
Comment thread python/src/ifd.rs Outdated
Comment on lines +486 to +492
fn tiles_byte_ranges(&self, xy: Vec<(usize, usize)>) -> PyAsyncTiffResult<PyTilesByteRanges> {
let byte_ranges = self
.ifd
.tiles_byte_ranges(&xy)
.ok_or(PyValueError::new_err("Not a tiled tiff"))?;
Ok(PyTilesByteRanges(byte_ranges))
}

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.

Oh sorry, I don't think there's merging/coalescing here. Just a list of the byte ranges (or a list of list of byte ranges in the case of planar config).

@kylebarron
kylebarron enabled auto-merge (squash) March 3, 2026 21:22
@kylebarron
kylebarron disabled auto-merge March 3, 2026 21:22
@kylebarron kylebarron changed the title refactor: Expose byte range APIs feat: Expose API to find byte range of an internal COG tile Mar 3, 2026
@kylebarron
kylebarron enabled auto-merge (squash) March 3, 2026 21:22
@github-actions github-actions Bot added feat and removed refactor labels Mar 3, 2026
@kylebarron
kylebarron merged commit 94c8480 into main Mar 3, 2026
14 checks passed
@kylebarron
kylebarron deleted the kyle/refactor-tile-byte-ranges branch March 3, 2026 21:24
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.

3 participants