Files
codex/codex-rs/tui/src/image_decode.rs
T
Dmitriy Kovalenko 0598a96e93 Prefer native clipboard images on macOS and honor image orientation (#60455)
## Why

The macOS clipboard reader requests TIFF even when PNG or JPEG data is available, causing clipboard owners to generate a large TIFF just for a paste. Pasted image files also need their orientation metadata applied before encoding attachments.

## What changed

- Read native macOS pasteboard representations before falling back to `arboard`, preferring PNG within each item while respecting item order and JPEG/TIFF ordering.
- Keep copied image files ahead of clipboard previews and apply orientation metadata when decoding files and native PNG/JPEG data.
- Share orientation-aware decoding with transcript thumbnails while preserving allocation limits.

## Testing

Add tests for pasteboard item ordering, all EXIF orientations in PNG and JPEG images, and rejection of images exceeding the allocation budget.

GitOrigin-RevId: 088f37a3205e49c805a09a84bbbb00b74dd6cb78
2026-11-09 21:30:05 +00:00

32 lines
917 B
Rust

//! Decode image pixels in their display orientation within the caller's allocation limits.
use image::DynamicImage;
use image::ImageDecoder;
use image::ImageReader;
use image::ImageResult;
use image::Limits;
use std::io::BufRead;
use std::io::Seek;
pub(crate) fn decode_oriented(
mut reader: ImageReader<impl BufRead + Seek>,
mut limits: Limits,
) -> ImageResult<DynamicImage> {
reader.limits(limits.clone());
let mut decoder = reader.into_decoder()?;
// Match ImageReader::decode's allocation check before allocating the pixel buffer.
limits.reserve(decoder.total_bytes())?;
decoder.set_limits(limits)?;
let orientation = decoder.orientation().ok();
let mut image = DynamicImage::from_decoder(decoder)?;
if let Some(orientation) = orientation {
image.apply_orientation(orientation);
}
Ok(image)
}
#[cfg(test)]
#[path = "image_decode_tests.rs"]
mod tests;