Image

Struct Image 

Source
pub struct Image { /* private fields */ }

Implementations§

Source§

impl Image

Source

pub fn new(width: u32, height: u32) -> Self

Create a new image

Source

pub fn from_color(width: u32, height: u32, color: Color) -> Self

Create a new image filled whole with color

Source

pub fn from_data( width: u32, height: u32, data: Box<[Color]>, ) -> Result<Self, String>

Create a new image from a boxed slice of colors

Source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, String>

Load an image from file path. Supports BMP and PNG

Examples found in repository?
examples/viewer.rs (line 60)
54fn main() {
55    let path = match env::args().nth(1) {
56        Some(arg) => arg,
57        None => "/ui/background.png".to_string(),
58    };
59
60    match Image::from_path(&path) {
61        Ok(image) => {
62            let (display_width, display_height) = orbclient::get_display_size().expect("viewer: failed to get display size");
63
64            let (width, height, scale) = find_scale(&image, display_width * 4/5, display_height * 4/5);
65
66            let mut window = Window::new_flags(
67                -1, -1, max(320, width), max(240, height),
68                &format!("{} - {:.1}% - Viewer", path, scale * 100.0),
69                &[WindowFlag::Resizable]
70            ).unwrap();
71
72            let mut scaled_image = image.clone();
73            let mut resize = Some((window.width(), window.height()));
74            loop {
75                if let Some((w, h)) = resize.take() {
76                    let (width, height, scale) = find_scale(&image, w, h);
77
78                    if width == scaled_image.width() && height == scaled_image.height() {
79                        // Do not resize scaled image
80                    } else if width == image.width() && height == image.height() {
81                        scaled_image = image.clone();
82                    } else {
83                        scaled_image = image.resize(width, height, orbimage::ResizeType::Lanczos3).unwrap();
84                    }
85
86                    window.set_title(&format!("{} - {:.1}% - Viewer", path, scale * 100.0));
87
88                    draw_image(&mut window, &scaled_image);
89                }
90
91                for event in window.events() {
92                    match event.to_option() {
93                        EventOption::Resize(resize_event) => {
94                            resize = Some((resize_event.width, resize_event.height));
95                        },
96                        EventOption::Quit(_) => return,
97                        _ => ()
98                    }
99                }
100            }
101        },
102        Err(err) => {
103            let msg = format!("{}", err);
104
105            let mut window = Window::new(-1, -1, max(320, msg.len() as u32 * 8), 32,
106                                         &format!("{} - Viewer", path)).unwrap();
107
108            window.set(Color::rgb(255, 255, 255));
109
110            let mut x = 0;
111            for c in msg.chars() {
112                window.char(x, 0, c, Color::rgb(0, 0, 0));
113                x += 8;
114            }
115
116            window.sync();
117
118            loop {
119                for event in window.events() {
120                    if let EventOption::Quit(_) = event.to_option() {
121                        return;
122                    }
123                }
124            }
125        }
126    }
127}
More examples
Hide additional examples
examples/background.rs (line 83)
73fn main() {
74    let mut args = env::args().skip(1);
75
76    let path = match args.next() {
77        Some(arg) => arg,
78        None => "/ui/background.png".to_string(),
79    };
80
81    let mode = BackgroundMode::from_str(&args.next().unwrap_or(String::new()));
82
83    match Image::from_path(&path) {
84        Ok(image) => {
85            let (display_width, display_height) = orbclient::get_display_size().expect("background: failed to get display size");
86
87            let mut window = Window::new_flags(
88                0, 0, display_width, display_height, &format!("{} - Background", path),
89                &[WindowFlag::Back, WindowFlag::Borderless, WindowFlag::Unclosable]
90            ).unwrap();
91
92            let mut scaled_image = image.clone();
93            let mut resize = Some((display_width, display_height));
94            loop {
95                if let Some((w, h)) = resize.take() {
96                    let (width, height) = find_scale(&image, mode, w, h);
97
98                    if width == scaled_image.width() && height == scaled_image.height() {
99                        // Do not resize scaled image
100                    } else if width == image.width() && height == image.height() {
101                        scaled_image = image.clone();
102                    } else {
103                        scaled_image = image.resize(width, height, orbimage::ResizeType::Lanczos3).unwrap();
104                    }
105
106                    let (crop_x, crop_w) = if width > w  {
107                        ((width - w)/2, w)
108                    } else {
109                        (0, width)
110                    };
111
112                    let (crop_y, crop_h) = if height > h {
113                        ((height - h)/2, h)
114                    } else {
115                        (0, height)
116                    };
117
118                    window.set(Color::rgb(0, 0, 0));
119
120                    let x = (w as i32 - crop_w as i32)/2;
121                    let y = (h as i32 - crop_h as i32)/2;
122                    scaled_image.roi(
123                        crop_x, crop_y,
124                        crop_w, crop_h,
125                    ).draw(
126                        &mut window,
127                        x, y
128                    );
129
130                    window.sync();
131                }
132
133                for event in window.events() {
134                    match event.to_option() {
135                        EventOption::Resize(resize_event) => {
136                            resize = Some((resize_event.width, resize_event.height));
137                        },
138                        EventOption::Screen(screen_event) => {
139                            window.set_size(screen_event.width, screen_event.height);
140                            resize = Some((screen_event.width, screen_event.height));
141                        },
142                        EventOption::Quit(_) => return,
143                        _ => ()
144                    }
145                }
146            }
147        },
148        Err(err) => {
149            println!("background: error loading {}: {}", path, err);
150        }
151    }
152}
Source

pub fn default() -> Self

Create a new empty image

Source

pub fn resize( &self, w: u32, h: u32, resize_type: ResizeType, ) -> Result<Self, String>

Examples found in repository?
examples/viewer.rs (line 83)
54fn main() {
55    let path = match env::args().nth(1) {
56        Some(arg) => arg,
57        None => "/ui/background.png".to_string(),
58    };
59
60    match Image::from_path(&path) {
61        Ok(image) => {
62            let (display_width, display_height) = orbclient::get_display_size().expect("viewer: failed to get display size");
63
64            let (width, height, scale) = find_scale(&image, display_width * 4/5, display_height * 4/5);
65
66            let mut window = Window::new_flags(
67                -1, -1, max(320, width), max(240, height),
68                &format!("{} - {:.1}% - Viewer", path, scale * 100.0),
69                &[WindowFlag::Resizable]
70            ).unwrap();
71
72            let mut scaled_image = image.clone();
73            let mut resize = Some((window.width(), window.height()));
74            loop {
75                if let Some((w, h)) = resize.take() {
76                    let (width, height, scale) = find_scale(&image, w, h);
77
78                    if width == scaled_image.width() && height == scaled_image.height() {
79                        // Do not resize scaled image
80                    } else if width == image.width() && height == image.height() {
81                        scaled_image = image.clone();
82                    } else {
83                        scaled_image = image.resize(width, height, orbimage::ResizeType::Lanczos3).unwrap();
84                    }
85
86                    window.set_title(&format!("{} - {:.1}% - Viewer", path, scale * 100.0));
87
88                    draw_image(&mut window, &scaled_image);
89                }
90
91                for event in window.events() {
92                    match event.to_option() {
93                        EventOption::Resize(resize_event) => {
94                            resize = Some((resize_event.width, resize_event.height));
95                        },
96                        EventOption::Quit(_) => return,
97                        _ => ()
98                    }
99                }
100            }
101        },
102        Err(err) => {
103            let msg = format!("{}", err);
104
105            let mut window = Window::new(-1, -1, max(320, msg.len() as u32 * 8), 32,
106                                         &format!("{} - Viewer", path)).unwrap();
107
108            window.set(Color::rgb(255, 255, 255));
109
110            let mut x = 0;
111            for c in msg.chars() {
112                window.char(x, 0, c, Color::rgb(0, 0, 0));
113                x += 8;
114            }
115
116            window.sync();
117
118            loop {
119                for event in window.events() {
120                    if let EventOption::Quit(_) = event.to_option() {
121                        return;
122                    }
123                }
124            }
125        }
126    }
127}
More examples
Hide additional examples
examples/background.rs (line 103)
73fn main() {
74    let mut args = env::args().skip(1);
75
76    let path = match args.next() {
77        Some(arg) => arg,
78        None => "/ui/background.png".to_string(),
79    };
80
81    let mode = BackgroundMode::from_str(&args.next().unwrap_or(String::new()));
82
83    match Image::from_path(&path) {
84        Ok(image) => {
85            let (display_width, display_height) = orbclient::get_display_size().expect("background: failed to get display size");
86
87            let mut window = Window::new_flags(
88                0, 0, display_width, display_height, &format!("{} - Background", path),
89                &[WindowFlag::Back, WindowFlag::Borderless, WindowFlag::Unclosable]
90            ).unwrap();
91
92            let mut scaled_image = image.clone();
93            let mut resize = Some((display_width, display_height));
94            loop {
95                if let Some((w, h)) = resize.take() {
96                    let (width, height) = find_scale(&image, mode, w, h);
97
98                    if width == scaled_image.width() && height == scaled_image.height() {
99                        // Do not resize scaled image
100                    } else if width == image.width() && height == image.height() {
101                        scaled_image = image.clone();
102                    } else {
103                        scaled_image = image.resize(width, height, orbimage::ResizeType::Lanczos3).unwrap();
104                    }
105
106                    let (crop_x, crop_w) = if width > w  {
107                        ((width - w)/2, w)
108                    } else {
109                        (0, width)
110                    };
111
112                    let (crop_y, crop_h) = if height > h {
113                        ((height - h)/2, h)
114                    } else {
115                        (0, height)
116                    };
117
118                    window.set(Color::rgb(0, 0, 0));
119
120                    let x = (w as i32 - crop_w as i32)/2;
121                    let y = (h as i32 - crop_h as i32)/2;
122                    scaled_image.roi(
123                        crop_x, crop_y,
124                        crop_w, crop_h,
125                    ).draw(
126                        &mut window,
127                        x, y
128                    );
129
130                    window.sync();
131                }
132
133                for event in window.events() {
134                    match event.to_option() {
135                        EventOption::Resize(resize_event) => {
136                            resize = Some((resize_event.width, resize_event.height));
137                        },
138                        EventOption::Screen(screen_event) => {
139                            window.set_size(screen_event.width, screen_event.height);
140                            resize = Some((screen_event.width, screen_event.height));
141                        },
142                        EventOption::Quit(_) => return,
143                        _ => ()
144                    }
145                }
146            }
147        },
148        Err(err) => {
149            println!("background: error loading {}: {}", path, err);
150        }
151    }
152}
Source

pub fn roi<'a>(&'a self, x: u32, y: u32, w: u32, h: u32) -> ImageRoi<'a>

Get a piece of the image

Examples found in repository?
examples/background.rs (lines 122-125)
73fn main() {
74    let mut args = env::args().skip(1);
75
76    let path = match args.next() {
77        Some(arg) => arg,
78        None => "/ui/background.png".to_string(),
79    };
80
81    let mode = BackgroundMode::from_str(&args.next().unwrap_or(String::new()));
82
83    match Image::from_path(&path) {
84        Ok(image) => {
85            let (display_width, display_height) = orbclient::get_display_size().expect("background: failed to get display size");
86
87            let mut window = Window::new_flags(
88                0, 0, display_width, display_height, &format!("{} - Background", path),
89                &[WindowFlag::Back, WindowFlag::Borderless, WindowFlag::Unclosable]
90            ).unwrap();
91
92            let mut scaled_image = image.clone();
93            let mut resize = Some((display_width, display_height));
94            loop {
95                if let Some((w, h)) = resize.take() {
96                    let (width, height) = find_scale(&image, mode, w, h);
97
98                    if width == scaled_image.width() && height == scaled_image.height() {
99                        // Do not resize scaled image
100                    } else if width == image.width() && height == image.height() {
101                        scaled_image = image.clone();
102                    } else {
103                        scaled_image = image.resize(width, height, orbimage::ResizeType::Lanczos3).unwrap();
104                    }
105
106                    let (crop_x, crop_w) = if width > w  {
107                        ((width - w)/2, w)
108                    } else {
109                        (0, width)
110                    };
111
112                    let (crop_y, crop_h) = if height > h {
113                        ((height - h)/2, h)
114                    } else {
115                        (0, height)
116                    };
117
118                    window.set(Color::rgb(0, 0, 0));
119
120                    let x = (w as i32 - crop_w as i32)/2;
121                    let y = (h as i32 - crop_h as i32)/2;
122                    scaled_image.roi(
123                        crop_x, crop_y,
124                        crop_w, crop_h,
125                    ).draw(
126                        &mut window,
127                        x, y
128                    );
129
130                    window.sync();
131                }
132
133                for event in window.events() {
134                    match event.to_option() {
135                        EventOption::Resize(resize_event) => {
136                            resize = Some((resize_event.width, resize_event.height));
137                        },
138                        EventOption::Screen(screen_event) => {
139                            window.set_size(screen_event.width, screen_event.height);
140                            resize = Some((screen_event.width, screen_event.height));
141                        },
142                        EventOption::Quit(_) => return,
143                        _ => ()
144                    }
145                }
146            }
147        },
148        Err(err) => {
149            println!("background: error loading {}: {}", path, err);
150        }
151    }
152}
Source

pub fn into_data(self) -> Box<[Color]>

Return a boxed slice of colors making up the image

Source

pub fn draw<R: Renderer>(&self, renderer: &mut R, x: i32, y: i32)

Draw the image on a window

Examples found in repository?
examples/viewer.rs (line 50)
31fn draw_image(window: &mut Window, image: &Image) {
32    window.set(Color::rgb(0, 0, 0));
33    /*
34    let box_size = 4;
35    for box_y in 0..window.height()/box_size {
36        for box_x in 0..window.width()/box_size {
37            let color = if box_x % 2 == box_y % 2 {
38                Color::rgb(102, 102, 102)
39            }else{
40                Color::rgb(53, 53, 53)
41            };
42
43            window.rect((box_x * box_size) as i32, (box_y * box_size) as i32, box_size, box_size, color);
44        }
45    }
46    */
47
48    let x = (window.width() - image.width())/2;
49    let y = (window.height() - image.height())/2;
50    image.draw(window, x as i32, y as i32);
51    window.sync();
52}

Trait Implementations§

Source§

impl Clone for Image

Source§

fn clone(&self) -> Image

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Renderer for Image

Source§

fn width(&self) -> u32

Get the width of the image in pixels

Source§

fn height(&self) -> u32

Get the height of the image in pixels

Source§

fn data(&self) -> &[Color]

Return a reference to a slice of colors making up the image

Source§

fn data_mut(&mut self) -> &mut [Color]

Return a mutable reference to a slice of colors making up the image

Source§

fn sync(&mut self) -> bool

Flip the buffer
Source§

fn mode(&self) -> &Cell<Mode>

Set/get drawing mode
Source§

fn pixel(&mut self, x: i32, y: i32, color: Color)

Draw a pixel
Source§

fn arc(&mut self, x0: i32, y0: i32, radius: i32, parts: u8, color: Color)

Draw a piece of an arc. Negative radius will fill in the inside
Source§

fn circle(&mut self, x0: i32, y0: i32, radius: i32, color: Color)

Draw a circle. Negative radius will fill in the inside
Source§

fn line4points(&mut self, x0: i32, y0: i32, x: i32, y: i32, color: Color)

Source§

fn line(&mut self, argx1: i32, argy1: i32, argx2: i32, argy2: i32, color: Color)

Draw a line
Source§

fn lines(&mut self, points: &[[i32; 2]], color: Color)

Source§

fn draw_path_stroke(&mut self, graphicspath: GraphicsPath, color: Color)

Draw a path (GraphicsPath)
Source§

fn char(&mut self, x: i32, y: i32, c: char, color: Color)

Draw a character, using the loaded font
Source§

fn set(&mut self, color: Color)

Set entire window to a color
Source§

fn clear(&mut self)

Sets the whole window to black
Source§

fn rect(&mut self, x: i32, y: i32, w: u32, h: u32, color: Color)

Source§

fn box_blur(&mut self, x: i32, y: i32, w: u32, h: u32, r: i32)

Source§

fn box_shadow( &mut self, x: i32, y: i32, w: u32, h: u32, offset_x: i32, offset_y: i32, r: i32, color: Color, )

Source§

fn image(&mut self, start_x: i32, start_y: i32, w: u32, h: u32, data: &[Color])

Display an image
Source§

fn image_legacy( &mut self, start_x: i32, start_y: i32, w: u32, h: u32, data: &[Color], )

Source§

fn image_over(&mut self, start: i32, image_data: &[Color])

Display an image overwriting a portion of window starting at given line : very quick!!
Source§

fn image_opaque( &mut self, start_x: i32, start_y: i32, w: u32, h: u32, image_data: &[Color], )

Display an image using non transparent method
Source§

fn image_fast( &mut self, start_x: i32, start_y: i32, w: u32, h: u32, image_data: &[Color], )

Source§

fn linear_gradient( &mut self, rect_x: i32, rect_y: i32, rect_width: u32, rect_height: u32, start_x: i32, start_y: i32, end_x: i32, end_y: i32, start_color: Color, end_color: Color, )

Draw a linear gradient in a rectangular region
Source§

fn rounded_rect( &mut self, x: i32, y: i32, w: u32, h: u32, radius: u32, filled: bool, color: Color, )

Draw a rect with rounded corners
Source§

fn wu_line(&mut self, x0: i32, y0: i32, x1: i32, y1: i32, color: Color)

Draws antialiased line
Source§

fn wu_circle(&mut self, x0: i32, y0: i32, radius: i32, color: Color)

Draws antialiased circle
Source§

fn getpixel(&self, x: i32, y: i32) -> Color

Gets pixel color at x,y position

Auto Trait Implementations§

§

impl !Freeze for Image

§

impl !RefUnwindSafe for Image

§

impl Send for Image

§

impl !Sync for Image

§

impl Unpin for Image

§

impl UnwindSafe for Image

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> SetParameter for T

Source§

fn set<T>(&mut self, value: T) -> <T as Parameter<Self>>::Result
where T: Parameter<Self>,

Sets value as a parameter of self.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.