fast image operations
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -29,6 +29,7 @@ mod affine; pub mod builder; mod drawing; mod overlay; +pub mod scale; pub use overlay::{Overlay, OverlayAt}; /// like assert!(), but causes undefined behaviour at runtime when the condition is not met. @@ -361,6 +362,29 @@ macro_rules! save { }; } +impl<const CHANNELS: usize> Image<Vec<u8>, CHANNELS> { + #[cfg(feature = "save")] + /// Open a PNG image + pub fn open(f: impl AsRef<std::path::Path>) -> Self { + let p = std::fs::File::open(f).unwrap(); + let r = std::io::BufReader::new(p); + let dec = png::Decoder::new(r); + let mut reader = dec.read_info().unwrap(); + let mut buf = vec![0; reader.output_buffer_size()]; + let info = reader.next_frame(&mut buf).unwrap(); + use png::ColorType::*; + match info.color_type { + Indexed | Grayscale => { + assert_eq!(CHANNELS, 1, "indexed | grayscale requires one channel") + } + Rgb => assert_eq!(CHANNELS, 3, "rgb requires three channels"), + Rgba => assert_eq!(CHANNELS, 4, "rgba requires four channels"), + GrayscaleAlpha => assert_eq!(CHANNELS, 2, "ya requires two channels"), + } + Self::build(info.width, info.height).buf(buf) + } +} + save!(3 == Rgb("RGB")); save!(4 == Rgba("RGBA")); save!(2 == GrayscaleAlpha("YA")); |