Skip to content

fix(data): turn a JPEG GeoTIFF's YCbCr into colours when it is read in place - #3544

Merged
Mavrick-F merged 1 commit into
opengeos:mainfrom
Mavrick-F:fix/ycbcr-in-place-2761
Dec 1, 2026
Merged

Mavrick-F merged 1 commit into
opengeos:mainfrom
Mavrick-F:fix/ycbcr-in-place-2761

Conversation

@Mavrick-F

@Mavrick-F Mavrick-F commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Summary

A JPEG-compressed GeoTIFF usually stores its colours as YCbCr. The whole-file reader turns them into red, green and blue (#692), but reading a tile at a time didn't. A large JPEG orthophoto read in place, from this computer or a web address, drew in wrong colours, and Identify gave luma and chroma as its values. v0.9.0 reads a large local file in place, so it's reached there without doing anything special.

  • Each level is converted by its own header, where the chunk is decoded, so windows, overviews, strips and map tiles all get it.
  • The conversion comes before no-data is matched, as in the whole-file reader.
  • A YCbCr JPEG with its bands stored apart can't be converted one band at a time, so it isn't read in place.

Not in this PR: Identify still gives the overview's pixel for a layer read in place (a later step of #2761). A stretch or range a project saved for the wrong colours isn't changed.

Surfaces: the decoder is shared by the desktop, browser (web addresses), command line, Python and phone. No command, parameter or saved format changes, and no new memory: the bands are converted in place.

Testing

  • ycbcr_jpeg_is_shown_as_rgb_read_in_place: two levels, and no-data equal to a decoded colour and to a stored luma. It fails without the conversion (81 for 200), and with the conversion moved after no-data.
  • A 20000 × 20000 YCbCr JPEG file against GDAL's values, 20 random points: the largest difference in any band was 108 before and 2 after (JPEG decoding isn't exact).
  • Full CI passed on this commit in the fork.

UI evidence

A synthetic 20000 × 20000 JPEG GeoTIFF with red, green, blue, yellow, white and black boxes, opened from this computer:

Before (v0.9.0) After
Wrong colours The file's colours

Related issue

Part of #2761.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed color rendering for JPEG-compressed YCbCr imagery in cloud-optimized GeoTIFFs, including images with multiple resolution levels.
    • No-data values are now evaluated against the converted colors, helping prevent valid pixels from being incorrectly treated as missing.

…n place

The whole-file reader turned JPEG-compressed YCbCr bands into red, green and
blue (opengeos#692); reading a tile at a time kept them as they are, so an
orthophoto read in place, or from its web address, drew in wrong colours and
Identify gave luma and chroma as its values. Each level is now converted by
its own header, before no-data is matched. Part of opengeos#2761.
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

COG decoding now identifies JPEG-compressed YCbCr images and converts decoded tiles to RGB using each level’s header. The changes also update COG color classification and add a test for RGB output and no-data matching.

Changes: JPEG YCbCr COG decoding

Layer / File(s) Summary
JPEG YCbCr detection and classification
crates/data/src/raster/geotiff.rs, crates/data/src/raster/cog.rs
A shared helper identifies JPEG-compressed YCbCr images. COG classification also checks planar configuration, and read_image uses the helper.
COG tile conversion and validation
crates/data/src/raster/cog.rs, crates/data/src/raster/geotiff.rs
COG tile decoding checks the selected level’s header and converts matching output to RGB. A test checks decoded colors and no-data matching against converted values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • opengeos/RustGIS#694: Adds YCbCr-to-RGB conversion to whole-image, overview, and threaded-chunk GeoTIFF read paths. This PR extends the conversion to in-place COG tile decoding.

Suggested reviewers: giswqs, ninhhaidang

Merge Risk: Minimal · up to 56514

JPEG YCbCr GeoTIFFs read in place now convert to RGB, and the supplied evidence shows no outstanding merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: JPEG GeoTIFF YCbCr values are converted to colours when the file is read in place. The raw summary shows the change in cog.rs and geotiff.rs adds YCbCr-to-RGB conversi…
Description check Passed The description follows the template's main sections: Summary, Testing, UI evidence, and Related issue. The Summary explains the change and reason. Testing lists a new test, a GDAL comparison, and CI …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: JPEG GeoTIFF YCbCr values are converted to colours when the file is read in place. The raw summary shows the change in cog.rs and geotiff.rs adds YCbCr-to-RGB conversion for tile-at-a-time decoding, which matches the title. The title is concise, specific, and follows the conventional type(scope) prefix.

Full details: Description check

Explanation

The description follows the template's main sections: Summary, Testing, UI evidence, and Related issue. The Summary explains the change and reason. Testing lists a new test, a GDAL comparison, and CI status. UI evidence includes before and after screenshots for a visible colour change, which matches the template's rule for UI-visible changes. Related issue states 'Part of #2761'. The template's UI-section text says 'No visible UI change' applies only when nothing visible changes, and this PR does change visible colours, so the screenshots are the correct choice. The description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks each level’s sign,
Then turns YCbCr into RGB shine.
The tiles reveal their colors true,
No-data checks use the converted hue.
The burrow cheers as pixels glow!

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: f8b4d24a-ba1c-4d88-992e-77d35711a3f7
Commits

Reviewing files that changed from the base of the PR and between 5799813 and 5651419.

Files selected for processing: 2

  • crates/data/src/raster/cog.rs
  • crates/data/src/raster/geotiff.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Comment on lines +369 to +371
ColorType::YCbCr(_)
if super::geotiff::is_ycbcr_jpeg(&mut dec)
&& dec.find_tag_unsigned::<u16>(Tag::PlanarConfiguration).ok().flatten() != Some(2) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new PlanarConfiguration != 2 guard changes what a planar YCbCr JPEG does: before, it was read in place (and drawn with wrong colours, since each chunk holds one band and ycbcr_to_rgb skips a single band). Now it falls through to NotTiled. That fix is right, but no test covers the planar branch, so nothing checks that a planar YCbCr JPEG is read whole and converted, rather than read in place. A small case in geotiff.rs's tests (planar, compression 7, YCbCr photometric) asserting Parse::NotTiled in cog::layout, and the whole-file read shown in colour, would pin it.

Category: quality

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.

Agreed, a hand-built planar file would pin the guard. Left for the release quality pass.

ColorType::YCbCr(_) if dec.find_tag_unsigned::<u16>(Tag::Compression).ok().flatten() == Some(7) => {
// A chunk's three bands are turned into colours together, so they are read together.
ColorType::YCbCr(_)
if super::geotiff::is_ycbcr_jpeg(&mut dec)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consequence to check: a large planar YCbCr JPEG GeoTIFF (for example, a big orthophoto written with separate bands) used to open in place, so a window or tile read stayed cheap. With this guard it is read whole, so its memory and time now scale with the whole file, and the in-place path that the PR's title describes doesn't apply to it. The PR says it "isn't read in place" but doesn't say what that costs or how big a file it covers. Please give its size as a multiple of the layer (AGENTS.md, Size for real data), and confirm that the whole read is refused past memory::planned with a notice, not left to run out of memory.

Confidence: medium-low (I did not trace the whole-read limit for this path).

Category: performance

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.

Checked. The whole-file read is bounded: past max_samples() it reads an overview, or every k-th pixel where there is none, and says so, so memory doesn't grow with the file. Such a file should be rare: gdal_translate refuses to write one (PHOTOMETRIC=YCBCR requires INTERLEAVE=PIXEL). No change.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code review

Two commits' worth of change, 2 files, ~55 lines added. The core fix looks right: the YCbCr conversion now runs per level in decode_tile_counted, before no-data matching, which matches the whole-file reader. I found no crash, silent-loss, or wasm issues. The points below are about test coverage and the cost of one behaviour change.

Bugs

None found. Checked: the conversion order against no-data, read_image still computing ycbcr from the chosen image, ct still used after the change, and ycbcr_to_rgb's clamping. Confidence: high.

Security

None found.

Performance

  • Planar YCbCr JPEGs now read whole instead of in place (cog.rs:370). A large planar YCbCr JPEG GeoTIFF used to open in place (with wrong colours), and now it is read whole. The PR doesn't say how big a file this affects, and I didn't trace whether the whole read is refused past memory::planned with a notice. Confidence: medium-low.

Quality

  • No test for the planar exclusion (cog.rs:369-371). The new PlanarConfiguration != 2 branch has no test in either direction. Confidence: high that the gap exists.
  • Nit: is_ycbcr_jpeg runs per tile in decode_tile_counted and re-reads the colour type and Compression tag each time. It could be a bool on Layout. Negligible at current tile sizes. Confidence: high.

AGENTS.md

None found. The change adds no command, asset, wasm branch, or stored data. The "Size for real data" point is covered in the performance item above.

Twins checked

  • In-place reads: decode_tile_counted is the only in-place chunk decoder, and both callers in crates/tiles/src/cog.rs (1356, 2160) go through it, so both get the fix.
  • Whole-file reads: read_image converts after the whole, decimated and windowed paths, so they are covered.
  • Layout detection: read_layout and read_image both use is_ycbcr_jpeg on the same image.
  • No #[cfg(target_arch = "wasm32")] branches are touched.

Prior reviews

No earlier threads on this PR, so no concerns were left out as already raised.

Change size

Compared with main (merge base def6c67 to PR head 5651419).

Measure Value
Files changed 2 (0 added, 2 modified, 0 removed, 0 renamed)
Size before 376.4 KB
Size after 379.2 KB
Total size change +2.8 KB
Path Before After Change
crates/data/src/raster/geotiff.rs 300.5 KB 302.9 KB +2.4 KB
crates/data/src/raster/cog.rs 76.0 KB 76.3 KB +380 B

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3544/
Web app https://preview.rustgis.app/pr-3544/app/
Commit 5651419

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

@Mavrick-F
Mavrick-F added this pull request to the merge queue Dec 1, 2026
Merged via the queue into opengeos:main with commit 42ac627 Dec 1, 2026
28 checks passed
@Mavrick-F
Mavrick-F deleted the fix/ycbcr-in-place-2761 branch December 1, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant