Skip to content

Commit

Permalink
Change the JPEG encoder to work on a GenericImageView instead of a by…
Browse files Browse the repository at this point in the history
…te slice

This change allows users to encode jpeg images from other sources
than memory, thus finally allowing the encoding of images that
do not fit in memory.

This also allows users to make the encoder work in a streaming fashion.
(see image-rs#1219)

This also finally allows encoding images from other pixel formats than
8-bit RGB. The conversion to 8bit YCbCr (used in JPEG) is done on the fly
during encoding.
  • Loading branch information
lovasoa committed May 24, 2020
1 parent 4431cd4 commit 2b6953a
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 113 deletions.
15 changes: 13 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use num_traits::{NumCast, ToPrimitive, Zero};
use std::ops::{Index, IndexMut};

use num_traits::{NumCast, ToPrimitive, Zero};

use crate::traits::{Pixel, Primitive};

/// An enumeration over supported color types and bit depths
Expand Down Expand Up @@ -57,6 +58,16 @@ impl ColorType {
}
}

/// Returns false if the color scheme is grayscale, true otherwise.
pub fn has_color(self) -> bool {
use ColorType::*;
match self {
L8 | L16 | La8 | La16 => false,
Rgb8 | Bgr8 | Rgb16 | Rgba8 | Bgra8 | Rgba16 => true,
__NonExhaustive(marker) => match marker._private {},
}
}

/// Returns the number of bits contained in a pixel of `ColorType` ```c``` (which will always be
/// a multiple of 8).
pub fn bits_per_pixel(self) -> u16 {
Expand Down Expand Up @@ -1126,7 +1137,7 @@ impl<T: Primitive> Invert for Bgr<T> {

#[cfg(test)]
mod tests {
use super::{Luma, LumaA, Pixel, Rgb, Rgba, Bgr, Bgra};
use super::{Bgr, Bgra, Luma, LumaA, Pixel, Rgb, Rgba};

#[test]
fn test_apply_with_alpha_rgba() {
Expand Down
5 changes: 2 additions & 3 deletions src/dynimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,8 @@ impl DynamicImage {
}
#[cfg(feature = "jpeg")]
image::ImageOutputFormat::Jpeg(quality) => {
let mut j = jpeg::JPEGEncoder::new_with_quality(w, quality);

j.encode(&bytes, width, height, color)?;
let j = jpeg::JPEGEncoder::new_with_quality(w, quality);
j.write_image(&bytes, width, height, color)?;
Ok(())
}

Expand Down
Loading

0 comments on commit 2b6953a

Please sign in to comment.