Skip to content

Commit

Permalink
Merge branch 'image-rs:main' into exif-rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnatsel authored Sep 9, 2024
2 parents 873c410 + 4afe957 commit 644b68e
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ jobs:
run: |
echo "#![deny(exported_private_dependencies)]" | cat - src/lib.rs > src/lib.rs.0
mv src/lib.rs.0 src/lib.rs
echo 'cargo-features = ["public-dependency"]' | cat - Cargo.toml > Cargo.toml.0
echo 'cargo-features = ["public-dependency"]' | cat - Cargo.toml | sed 's/rayon = { ver/rayon = { public = true, ver/' > Cargo.toml.0
mv Cargo.toml.0 Cargo.toml
cargo check
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ image-webp = { version = "0.1.0", optional = true }
mp4parse = { version = "0.17.0", optional = true }
png = { version = "0.17.6", optional = true }
qoi = { version = "0.4", optional = true }
ravif = { version = "0.11.2", default-features = false, optional = true }
ravif = { version = "0.11.3", default-features = false, optional = true }
rayon = { version = "1.7.0", optional = true }
rgb = { version = "0.8.25", optional = true }
rgb = { version = "0.8.48", default-features = false, optional = true }
tiff = { version = "0.9.0", optional = true }
zune-core = { version = "0.4.12", default-features = false, optional = true }
zune-jpeg = { version = "0.4.13", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ mod test {
let val = a.pixels_mut().next().unwrap();
*val = Rgb([42, 0, 0]);
}
assert_eq!(a.data[0], 42)
assert_eq!(a.data[0], 42);
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions src/codecs/jpeg/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ impl<R: BufRead + Seek> ImageDecoder for JpegDecoder<R> {
Ok(decoder.icc_profile())
}

fn exif_metadata(&mut self) -> ImageResult<Option<Vec<u8>>> {
let mut decoder = zune_jpeg::JpegDecoder::new(&self.input);
decoder.decode_headers().map_err(ImageError::from_jpeg)?;
Ok(decoder.exif().cloned())
}

fn read_image(self, buf: &mut [u8]) -> ImageResult<()> {
let advertised_len = self.total_bytes();
let actual_len = buf.len() as u64;
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/jpeg/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,9 @@ impl<W: Write> JpegEncoder<W> {

let mut tables = vec![STD_LUMA_QTABLE, STD_CHROMA_QTABLE];
tables.iter_mut().for_each(|t| {
t.iter_mut().for_each(|v| {
for v in t.iter_mut() {
*v = clamp((u32::from(*v) * scale + 50) / 100, 1, u32::from(u8::MAX)) as u8;
});
}
});

JpegEncoder {
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/openexr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<R: BufRead + Seek> ImageDecoder for OpenExrDecoder<R> {
assert!(
!has_invalid_size_or_overflowed,
"byte buffer not large enough for the specified dimensions and f32 pixels"
)
);
}

let result = read()
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/pnm/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,9 @@ impl<R: Read> PnmDecoder<R> {
let factor = target_sample_max as f32 / current_sample_max as f32;

if S::sample_size() == 1 {
buf.iter_mut().for_each(|v| {
for v in buf.iter_mut() {
*v = (f32::from(*v) * factor).round() as u8;
});
}
} else if S::sample_size() == 2 {
for chunk in buf.chunks_exact_mut(2) {
let v = NativeEndian::read_u16(chunk);
Expand Down
6 changes: 6 additions & 0 deletions src/codecs/webp/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl<R: BufRead + Seek> ImageDecoder for WebPDecoder<R> {
.icc_profile()
.map_err(ImageError::from_webp_decode)
}

fn exif_metadata(&mut self) -> ImageResult<Option<Vec<u8>>> {
self.inner
.exif_metadata()
.map_err(ImageError::from_webp_decode)
}
}

impl<'a, R: 'a + Read + Seek> AnimationDecoder<'a> for WebPDecoder<R> {
Expand Down
17 changes: 17 additions & 0 deletions src/codecs/webp/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ use crate::error::{EncodingError, UnsupportedError, UnsupportedErrorKind};
use crate::{ExtendedColorType, ImageEncoder, ImageError, ImageFormat, ImageResult};

/// WebP Encoder.
///
/// ### Limitations
///
/// Right now only **lossless** encoding is supported.
///
/// If you need **lossy** encoding, you'll have to use `libwebp`.
/// Example code for encoding a [`DynamicImage`](crate::DynamicImage) with `libwebp`
/// via the [`webp`](https://docs.rs/webp/latest/webp/) crate can be found
/// [here](https://github.com/jaredforth/webp/blob/main/examples/convert.rs).
///
/// ### Compression ratio
///
/// This encoder reaches compression ratios higher than PNG at a fraction of the encoding time.
/// However, it does not reach the full potential of lossless WebP for reducing file size.
///
/// If you need an even higher compression ratio at the cost of much slower encoding,
/// please encode the image with `libwebp` as outlined above.
pub struct WebPEncoder<W> {
inner: image_webp::WebPEncoder<W>,
}
Expand Down
5 changes: 2 additions & 3 deletions src/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,8 @@ impl SampleLayout {
Some(size) => size,
};

match max_dim.checked_len() {
None => return true,
Some(_) => (), // Only want to know this didn't overflow.
if max_dim.checked_len().is_none() {
return true;
};

// Each higher dimension must walk over all of one lower dimension.
Expand Down
13 changes: 12 additions & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,14 @@ pub trait ImageDecoder {
Ok(None)
}

/// Returns the raw [Exif](https://en.wikipedia.org/wiki/Exif) chunk, if it is present.
/// A third-party crate such as [`kamadak-exif`](https://docs.rs/kamadak-exif/) is required to actually parse it.
///
/// For formats that don't support embedded profiles this function should always return `Ok(None)`.
fn exif_metadata(&mut self) -> ImageResult<Option<Vec<u8>>> {
Ok(None)
}

/// Returns the total number of bytes in the decoded image.
///
/// This is the size of the buffer that must be passed to `read_image` or
Expand Down Expand Up @@ -710,6 +718,9 @@ impl<T: ?Sized + ImageDecoder> ImageDecoder for Box<T> {
fn icc_profile(&mut self) -> ImageResult<Option<Vec<u8>>> {
(**self).icc_profile()
}
fn exif_metadata(&mut self) -> ImageResult<Option<Vec<u8>>> {
(**self).exif_metadata()
}
fn total_bytes(&self) -> u64 {
(**self).total_bytes()
}
Expand Down Expand Up @@ -1799,7 +1810,7 @@ mod tests {

#[test]
fn all() {
let all_formats: HashSet<ImageFormat> = HashSet::from_iter(ImageFormat::all());
let all_formats: HashSet<ImageFormat> = ImageFormat::all().collect();
assert!(all_formats.contains(&ImageFormat::Avif));
assert!(all_formats.contains(&ImageFormat::Gif));
assert!(all_formats.contains(&ImageFormat::Bmp));
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This crate provides native rust implementations of image encoding and decoding as well as some
//! basic image manipulation functions. Additional documentation can currently also be found in the
//! [README.md file which is most easily viewed on
//! github](https://github.com/image-rs/image/blob/master/README.md).
//! github](https://github.com/image-rs/image/blob/main/README.md).
//!
//! There are two core problems for which this library provides solutions: a unified interface for image
//! encodings and simple generic buffers for their content. It's possible to use either feature
Expand Down
2 changes: 1 addition & 1 deletion tests/reference_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ fn check_hdr_references() {
.rev()
{
if let Normal(name) = *c {
ref_path.push(name)
ref_path.push(name);
} else {
panic!()
}
Expand Down

0 comments on commit 644b68e

Please sign in to comment.