1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//! imaged is an image processing and storage library for working with a wide range of image types
//! For more information see [imaged](https://github.com/zshipko/imaged)
//!
//! ## Getting started
//!
//! ```rust
//! use imaged::*;
//!
//! fn run() -> Result<(), Error> {
//!     // Read image
//!     let image = Image::read_default("test.jpg")?;
//!
//!     // Convert colorspace and type
//!     let a = image.convert(Color::HSV, Type::F(32))?;
//!
//!     // Save image
//!     a.write("out.tiff")?;
//!
//!     Ok(())
//! }
//!

use std::ffi::c_void;

/// Raw imaged bindings
pub mod sys;

#[cfg(feature = "halide")]
mod halide_wrapper;

#[cfg(feature = "halide")]
pub use halide_runtime as halide;

#[cfg(feature = "viewer")]
pub mod viewer;

mod db;
mod error;
mod image;
mod iter;
mod pixel;

pub use db::*;
pub use error::Error;
pub use image::*;
pub use iter::*;
pub use pixel::*;

/// Global configuration flags
pub mod conf {
    use crate::*;

    /// Use autobrightness in libraw
    pub fn use_raw_auto_brightness(b: bool) {
        unsafe { sys::imageRAWUseAutoBrightness(b) }
    }

    /// Use camera white balance in libraw
    pub fn use_raw_camera_white_balance(b: bool) {
        unsafe { sys::imageRAWUseCameraWhiteBalance(b) }
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn it_works() {
        let db = DB::open("./testing").unwrap();
        let image = Image::new(Meta::new(800, 600, Color::RGB, Type::F(32))).unwrap();
        db.set("testing", &image).unwrap();
        {
            let mut iter = db.iter().unwrap();
            let (k, i) = iter.next().unwrap();
            assert!(k == "testing");
            assert!(i.meta().width() == 800, i.meta().height() == 600);
        }

        db.remove("testing").unwrap();

        {
            let mut iter = db.iter().unwrap();
            assert!(iter.next().is_none());
        }

        db.destroy().unwrap();
    }

    #[test]
    fn each_pixel() {
        let mut image = Image::new(Meta::new(800, 600, Color::RGB, Type::F(32))).unwrap();
        let mut image2 = Image::new(Meta::new(800, 600, Color::RGB, Type::F(32))).unwrap();

        assert!(image.shape() == (800, 600, 3));
        assert!(image2.shape() == (800, 600, 3));

        image
            .each_pixel(None, |x, y, px| {
                let data = px.data_mut();
                data[0] = 1.0;
                data[2] = 0.5;
                data[3] = 0.25;
                image2.set_pixel(x, y, px);
                Ok(true)
            })
            .unwrap();

        for y in 0..600 {
            for x in 0..800 {
                assert!(image.at::<f32>(x, y).unwrap() == image2.at::<f32>(x, y).unwrap());
            }
        }
    }

    #[test]
    fn each_pixel2() {
        let mut image = Image::new(Meta::new(800, 600, Color::RGB, Type::F(32))).unwrap();
        let mut image2 = Image::new(Meta::new(800, 600, Color::RGB, Type::F(32))).unwrap();

        assert!(image.shape() == (800, 600, 3));
        assert!(image2.shape() == (800, 600, 3));

        image
            .each_pixel(None, |x, y, px| {
                let data = px.data_mut();
                data[0] = 1.0;
                data[2] = 0.5;
                data[3] = 0.25;
                image2.set_pixel(x, y, px);
                Ok(true)
            })
            .unwrap();

        for y in 0..600 {
            for x in 0..800 {
                assert!(image.at::<f32>(x, y).unwrap() == image2.at::<f32>(x, y).unwrap());
            }
        }
    }
}