diff --git a/.travis.yml b/.travis.yml index f46d0d17ac..3053bceeb9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,25 @@ os: env: global: - secure: M2MCRtyP5P/Xf2TSqrbz8cs41TQY04mK/5Fi6qgr77OKLNZlDclKiFY8BmQ6f1JhVccoE5gMIFpfPoVUu8wZ0Pe7/X4IyO4vxWawVQfE6f0NYErD9yqiE1KEi/RGKPOQfL5HFUK7ifnXvLwsAMh1ix9XMgaBZfZLQ8KhkxNRXwI= + matrix: + - FEATURES='' + - FEATURES='gif' + - FEATURES='jpeg' + - FEATURES='png' + - FEATURES='ppm' + - FEATURES='tga' + - FEATURES='tiff' + - FEATURES='webp' script: - - cargo build -v - - cargo test -v - - cargo doc -v + - if [ -z "$FEATURES" ]; then + cargo build -v; + cargo test -v; + cargo doc -v; + else + cargo build -v --no-default-features --features "$FEATURES"; + cargo test -v --no-default-features --features "$FEATURES"; + cargo doc -v; + fi after_success: | [ $TRAVIS_BRANCH = master ] && [ $TRAVIS_PULL_REQUEST = false ] && diff --git a/Cargo.toml b/Cargo.toml index 51312f5e15..ae7c075c2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,3 +26,14 @@ version = "0.1.15" [dev-dependencies.glob] version = "0.2.3" + +[features] +default = ["gif", "jpeg", "png", "ppm", "tga", "tiff", "webp"] + +gif = [] +jpeg = [] +png = [] +ppm = [] +tga = [] +tiff = [] +webp = [] diff --git a/src/dynimage.rs b/src/dynimage.rs index 24c85745f8..e1d1b771d6 100644 --- a/src/dynimage.rs +++ b/src/dynimage.rs @@ -2,12 +2,19 @@ use std::old_io; use std::iter; use std::ascii::OwnedAsciiExt; +#[cfg(feature = "ppm")] use ppm; +#[cfg(feature = "gif")] use gif; +#[cfg(feature = "webp")] use webp; +#[cfg(feature = "jpeg")] use jpeg; +#[cfg(feature = "png")] use png; +#[cfg(feature = "tiff")] use tiff; +#[cfg(feature = "tga")] use tga; use color; @@ -336,13 +343,14 @@ impl DynamicImage { let color = self.color(); let r = match format { + #[cfg(feature = "png")] image::ImageFormat::PNG => { let mut p = png::PNGEncoder::new(w); try!(p.encode(&bytes, width, height, color)); Ok(()) } - + #[cfg(feature = "ppm")] image::ImageFormat::PPM => { let mut p = ppm::PPMEncoder::new(w); @@ -350,6 +358,7 @@ impl DynamicImage { Ok(()) } + #[cfg(feature = "jpeg")] image::ImageFormat::JPEG => { let mut j = jpeg::JPEGEncoder::new(w); @@ -357,6 +366,7 @@ impl DynamicImage { Ok(()) } + #[cfg(feature = "gif")] image::ImageFormat::GIF => { let mut g = gif::GIFEncoder::new( self.to_rgba(), None, gif::ColorMode::Indexed(0xFF) @@ -536,9 +546,12 @@ pub fn save_buffer(path: &Path, buf: &[u8], width: u32, height: u32, color: colo .map_or("".to_string(), | s | s.to_string().into_ascii_lowercase()); match &*ext { + #[cfg(feature = "jpeg")] "jpg" | "jpeg" => jpeg::JPEGEncoder::new(fout).encode(buf, width, height, color), + #[cfg(feature = "png")] "png" => png::PNGEncoder::new(fout).encode(buf, width, height, color), + #[cfg(feature = "ppm")] "ppm" => ppm::PPMEncoder::new(fout).encode(buf, width, height, color), format => Err(old_io::IoError { kind: old_io::InvalidInput, @@ -554,11 +567,17 @@ pub fn save_buffer(path: &Path, buf: &[u8], width: u32, height: u32, color: colo /// Create a new image from a Reader pub fn load(r: R, format: ImageFormat) -> ImageResult { match format { + #[cfg(feature = "png")] image::ImageFormat::PNG => decoder_to_image(png::PNGDecoder::new(old_io::BufferedReader::new(r))), + #[cfg(feature = "gif")] image::ImageFormat::GIF => decoder_to_image(gif::GIFDecoder::new(old_io::BufferedReader::new(r))), + #[cfg(feature = "jpeg")] image::ImageFormat::JPEG => decoder_to_image(jpeg::JPEGDecoder::new(old_io::BufferedReader::new(r))), + #[cfg(feature = "webp")] image::ImageFormat::WEBP => decoder_to_image(webp::WebpDecoder::new(old_io::BufferedReader::new(r))), + #[cfg(feature = "tiff")] image::ImageFormat::TIFF => decoder_to_image(try!(tiff::TIFFDecoder::new(r))), + #[cfg(feature = "tga")] image::ImageFormat::TGA => decoder_to_image(tga::TGADecoder::new(r)), _ => Err(image::ImageError::UnsupportedError(format!("A decoder for {:?} is not available.", format))), } diff --git a/src/imageops/sample.rs b/src/imageops/sample.rs index 1156738925..2714469f0a 100644 --- a/src/imageops/sample.rs +++ b/src/imageops/sample.rs @@ -475,6 +475,7 @@ mod tests { use super::{resize, FilterType}; #[bench] + #[cfg(feature = "png")] fn bench_resize(b: &mut test::Bencher) { let img = ::open(&Path::new("./examples/fractal.png")).unwrap(); b.iter(|| { diff --git a/src/lib.rs b/src/lib.rs index 29ba03854f..0d1a822842 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,12 +109,19 @@ pub mod math; pub mod imageops; // Image Codecs +#[cfg(feature = "webp")] pub mod webp; +#[cfg(feature = "ppm")] pub mod ppm; +#[cfg(feature = "png")] pub mod png; +#[cfg(feature = "jpeg")] pub mod jpeg; +#[cfg(feature = "gif")] pub mod gif; +#[cfg(feature = "tiff")] pub mod tiff; +#[cfg(feature = "tga")] pub mod tga; diff --git a/tests/tga.rs b/tests/tga.rs old mode 100644 new mode 100755 index 002d5d0b2c..18f92cacbd --- a/tests/tga.rs +++ b/tests/tga.rs @@ -1,4 +1,5 @@ #![feature(old_io, old_path)] +#![cfg(feature = "tga")] extern crate image;