Skip to content

Commit

Permalink
Provide scale method of JpegDecoder.
Browse files Browse the repository at this point in the history
Add a `scale(&mut self, requested_width, requested_height)` to
`JpegDecoder`, exposing same functionality in `jpeg_decoder::Decoder`.

Question: As it is allowed for this function to do nothing, maybe it
should be added to trait `ImageDecoder`?  If would be possibe to add a
meaningfull implementation to any "progressive" format, such as png
and gif, and it's ok to do nothing for other formats.
  • Loading branch information
kaj committed Oct 9, 2020
1 parent 6bee185 commit df6e2ca
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/jpeg/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ impl<R: Read> JpegDecoder<R> {
metadata,
})
}

/// Configure the decoder to scale the image during decoding.
///
/// This efficiently scales the image by the smallest supported
/// scale factor that produces an image larger than or equal to
/// the requested size in at least one axis. The currently
/// implemented scale factors are 1/8, 1/4, 1/2 and 1.
///
/// To generate a thumbnail of an exact size, pass the desired
/// size and then scale to the final size using a traditional
/// resampling algorithm.
///
/// The size of the image to be loaded, with the scale factor
/// applied, is returned.
pub fn scale(
&mut self,
requested_width: u16,
requested_height: u16
) -> ImageResult<(u16, u16)> {
let result = self.decoder.scale(requested_width, requested_height)
.map_err(ImageError::from_jpeg)?;

self.metadata.width = result.0;
self.metadata.height = result.1;

Ok(result)
}
}

/// Wrapper struct around a `Cursor<Vec<u8>>`
Expand Down

0 comments on commit df6e2ca

Please sign in to comment.