pub struct Image { /* private fields */ }Implementations§
Source§impl Image
impl Image
Sourcepub fn from_color(width: u32, height: u32, color: Color) -> Self
pub fn from_color(width: u32, height: u32, color: Color) -> Self
Create a new image filled whole with color
Sourcepub fn from_data(
width: u32,
height: u32,
data: Box<[Color]>,
) -> Result<Self, String>
pub fn from_data( width: u32, height: u32, data: Box<[Color]>, ) -> Result<Self, String>
Create a new image from a boxed slice of colors
Sourcepub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, String>
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
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}Sourcepub fn resize(
&self,
w: u32,
h: u32,
resize_type: ResizeType,
) -> Result<Self, String>
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
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}Sourcepub fn roi<'a>(&'a self, x: u32, y: u32, w: u32, h: u32) -> ImageRoi<'a>
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}Sourcepub fn draw<R: Renderer>(&self, renderer: &mut R, x: i32, y: i32)
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 Renderer for Image
impl Renderer for Image
Source§fn data_mut(&mut self) -> &mut [Color]
fn data_mut(&mut self) -> &mut [Color]
Return a mutable reference to a slice of colors making up the image
Source§fn arc(&mut self, x0: i32, y0: i32, radius: i32, parts: u8, color: Color)
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)
fn circle(&mut self, x0: i32, y0: i32, radius: i32, color: Color)
Draw a circle. Negative radius will fill in the inside
fn line4points(&mut self, x0: i32, y0: i32, x: i32, y: i32, color: Color)
fn lines(&mut self, points: &[[i32; 2]], color: Color)
Source§fn draw_path_stroke(&mut self, graphicspath: GraphicsPath, color: Color)
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)
fn char(&mut self, x: i32, y: i32, c: char, color: Color)
Draw a character, using the loaded font
fn rect(&mut self, x: i32, y: i32, w: u32, h: u32, color: Color)
fn box_blur(&mut self, x: i32, y: i32, w: u32, h: u32, r: i32)
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])
fn image(&mut self, start_x: i32, start_y: i32, w: u32, h: u32, data: &[Color])
Display an image
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])
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],
)
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
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,
)
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,
)
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
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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