Skip to main content

ImageRef

Struct ImageRef 

Source
pub struct ImageRef<'a> { /* private fields */ }
Expand description

A structure to borrow an existing image in software memory.

Implementations§

Source§

impl<'a> ImageRef<'a>

Source

pub fn from_data(w: u32, h: u32, data: &'a mut [Color]) -> Self

Examples found in repository?
examples/image_bench.rs (line 63)
22fn main() {
23    //let (width, height) = orbclient::get_display_size().unwrap();
24
25    let mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27    window.set(Color::rgb(255, 255, 255));
28
29    //create image data : a green square
30    let data = vec![Color::rgba(100, 200, 10, 3); 412500];
31    let mut data2 = vec![Color::rgba(200, 100, 10, 3); 412500];
32    let mut data3 = vec![Color::rgba(10, 100, 100, 3); 412500];
33    let mut data4 = vec![Color::rgba(10, 100, 200, 3); 800 * 400];
34
35    //draw image benchmarking
36    println!("Benchmarking implementations to draw an image on window:");
37
38    time!("image_legacy", {
39        for _i in 0..TIMES {
40            window.image_legacy(15, 15, 750, 550, &data[..]);
41        }
42    });
43
44    time!("image_over", {
45        for _i in 0..TIMES {
46            window.image_over(50, &data4[..]);
47        }
48    });
49
50    time!("image_fast", {
51        for _i in 0..TIMES {
52            window.image_fast(20, 20, 750, 550, &data2[..]);
53        }
54    });
55
56    time!("image_opaque", {
57        for _i in 0..TIMES {
58            window.image_opaque(30, 30, 750, 550, &data3[..]);
59        }
60    });
61
62    time!("image_roi_mut_blend", {
63        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
64        for _i in 0..TIMES {
65            ImageRef::from_renderer(&mut window)
66                .roi_mut(&Rect::new(40, 40, 750, 550))
67                .blend(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
68        }
69    });
70
71    time!("image_roi_mut_blit_mask", {
72        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
73        for _i in 0..TIMES {
74            ImageRef::from_renderer(&mut window)
75                .roi_mut(&Rect::new(40, 40, 750, 550))
76                .blit_mask(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
77        }
78    });
79
80    time!("image_roi_mut_blit", {
81        let data3_roi = ImageRef::from_data(750, 550, &mut data3[..]);
82        for _i in 0..TIMES {
83            ImageRef::from_renderer(&mut window)
84                .roi_mut(&Rect::new(50, 50, 750, 550))
85                .blit(&data3_roi.roi(&Rect::new(0, 0, 750, 550)));
86        }
87    });
88
89    time!("image_roi_mut_blit_over", {
90        let data4_roi = ImageRef::from_data(800, 400, &mut data4[..]);
91        for _i in 0..TIMES {
92            ImageRef::from_renderer(&mut window)
93                // .roi_mut(&Rect::new(10, 120, 790, 400)) // to test blit_over does not trigger
94                .roi_mut(&Rect::new(0, 120, 800, 400))
95                .blit(&data4_roi.roi(&Rect::new(0, 0, 800, 400)));
96        }
97    });
98
99    println!("------------------------------------------------");
100
101    window.sync();
102
103    'events: loop {
104        for event in window.events() {
105            match event.to_option() {
106                EventOption::Quit(_quit_event) => break 'events,
107                EventOption::Mouse(evt) => println!(
108                    "At position {:?} pixel color is : {:?}",
109                    (evt.x, evt.y),
110                    window.getpixel(evt.x, evt.y)
111                ),
112                event_option => println!("{:?}", event_option),
113            }
114        }
115    }
116}
Source

pub fn from_renderer(renderer: &'a mut impl Renderer) -> Self

Examples found in repository?
examples/image_bench.rs (line 65)
22fn main() {
23    //let (width, height) = orbclient::get_display_size().unwrap();
24
25    let mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27    window.set(Color::rgb(255, 255, 255));
28
29    //create image data : a green square
30    let data = vec![Color::rgba(100, 200, 10, 3); 412500];
31    let mut data2 = vec![Color::rgba(200, 100, 10, 3); 412500];
32    let mut data3 = vec![Color::rgba(10, 100, 100, 3); 412500];
33    let mut data4 = vec![Color::rgba(10, 100, 200, 3); 800 * 400];
34
35    //draw image benchmarking
36    println!("Benchmarking implementations to draw an image on window:");
37
38    time!("image_legacy", {
39        for _i in 0..TIMES {
40            window.image_legacy(15, 15, 750, 550, &data[..]);
41        }
42    });
43
44    time!("image_over", {
45        for _i in 0..TIMES {
46            window.image_over(50, &data4[..]);
47        }
48    });
49
50    time!("image_fast", {
51        for _i in 0..TIMES {
52            window.image_fast(20, 20, 750, 550, &data2[..]);
53        }
54    });
55
56    time!("image_opaque", {
57        for _i in 0..TIMES {
58            window.image_opaque(30, 30, 750, 550, &data3[..]);
59        }
60    });
61
62    time!("image_roi_mut_blend", {
63        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
64        for _i in 0..TIMES {
65            ImageRef::from_renderer(&mut window)
66                .roi_mut(&Rect::new(40, 40, 750, 550))
67                .blend(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
68        }
69    });
70
71    time!("image_roi_mut_blit_mask", {
72        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
73        for _i in 0..TIMES {
74            ImageRef::from_renderer(&mut window)
75                .roi_mut(&Rect::new(40, 40, 750, 550))
76                .blit_mask(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
77        }
78    });
79
80    time!("image_roi_mut_blit", {
81        let data3_roi = ImageRef::from_data(750, 550, &mut data3[..]);
82        for _i in 0..TIMES {
83            ImageRef::from_renderer(&mut window)
84                .roi_mut(&Rect::new(50, 50, 750, 550))
85                .blit(&data3_roi.roi(&Rect::new(0, 0, 750, 550)));
86        }
87    });
88
89    time!("image_roi_mut_blit_over", {
90        let data4_roi = ImageRef::from_data(800, 400, &mut data4[..]);
91        for _i in 0..TIMES {
92            ImageRef::from_renderer(&mut window)
93                // .roi_mut(&Rect::new(10, 120, 790, 400)) // to test blit_over does not trigger
94                .roi_mut(&Rect::new(0, 120, 800, 400))
95                .blit(&data4_roi.roi(&Rect::new(0, 0, 800, 400)));
96        }
97    });
98
99    println!("------------------------------------------------");
100
101    window.sync();
102
103    'events: loop {
104        for event in window.events() {
105            match event.to_option() {
106                EventOption::Quit(_quit_event) => break 'events,
107                EventOption::Mouse(evt) => println!(
108                    "At position {:?} pixel color is : {:?}",
109                    (evt.x, evt.y),
110                    window.getpixel(evt.x, evt.y)
111                ),
112                event_option => println!("{:?}", event_option),
113            }
114        }
115    }
116}
Source

pub fn roi(&self, rect: &Rect) -> ImageRoi<'_>

Read a specified rect of the image

Examples found in repository?
examples/image_bench.rs (line 67)
22fn main() {
23    //let (width, height) = orbclient::get_display_size().unwrap();
24
25    let mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27    window.set(Color::rgb(255, 255, 255));
28
29    //create image data : a green square
30    let data = vec![Color::rgba(100, 200, 10, 3); 412500];
31    let mut data2 = vec![Color::rgba(200, 100, 10, 3); 412500];
32    let mut data3 = vec![Color::rgba(10, 100, 100, 3); 412500];
33    let mut data4 = vec![Color::rgba(10, 100, 200, 3); 800 * 400];
34
35    //draw image benchmarking
36    println!("Benchmarking implementations to draw an image on window:");
37
38    time!("image_legacy", {
39        for _i in 0..TIMES {
40            window.image_legacy(15, 15, 750, 550, &data[..]);
41        }
42    });
43
44    time!("image_over", {
45        for _i in 0..TIMES {
46            window.image_over(50, &data4[..]);
47        }
48    });
49
50    time!("image_fast", {
51        for _i in 0..TIMES {
52            window.image_fast(20, 20, 750, 550, &data2[..]);
53        }
54    });
55
56    time!("image_opaque", {
57        for _i in 0..TIMES {
58            window.image_opaque(30, 30, 750, 550, &data3[..]);
59        }
60    });
61
62    time!("image_roi_mut_blend", {
63        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
64        for _i in 0..TIMES {
65            ImageRef::from_renderer(&mut window)
66                .roi_mut(&Rect::new(40, 40, 750, 550))
67                .blend(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
68        }
69    });
70
71    time!("image_roi_mut_blit_mask", {
72        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
73        for _i in 0..TIMES {
74            ImageRef::from_renderer(&mut window)
75                .roi_mut(&Rect::new(40, 40, 750, 550))
76                .blit_mask(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
77        }
78    });
79
80    time!("image_roi_mut_blit", {
81        let data3_roi = ImageRef::from_data(750, 550, &mut data3[..]);
82        for _i in 0..TIMES {
83            ImageRef::from_renderer(&mut window)
84                .roi_mut(&Rect::new(50, 50, 750, 550))
85                .blit(&data3_roi.roi(&Rect::new(0, 0, 750, 550)));
86        }
87    });
88
89    time!("image_roi_mut_blit_over", {
90        let data4_roi = ImageRef::from_data(800, 400, &mut data4[..]);
91        for _i in 0..TIMES {
92            ImageRef::from_renderer(&mut window)
93                // .roi_mut(&Rect::new(10, 120, 790, 400)) // to test blit_over does not trigger
94                .roi_mut(&Rect::new(0, 120, 800, 400))
95                .blit(&data4_roi.roi(&Rect::new(0, 0, 800, 400)));
96        }
97    });
98
99    println!("------------------------------------------------");
100
101    window.sync();
102
103    'events: loop {
104        for event in window.events() {
105            match event.to_option() {
106                EventOption::Quit(_quit_event) => break 'events,
107                EventOption::Mouse(evt) => println!(
108                    "At position {:?} pixel color is : {:?}",
109                    (evt.x, evt.y),
110                    window.getpixel(evt.x, evt.y)
111                ),
112                event_option => println!("{:?}", event_option),
113            }
114        }
115    }
116}
Source

pub fn roi_mut(&mut self, rect: &Rect) -> ImageRoiMut<'_>

Read or write a specified rect of the image

Examples found in repository?
examples/image_bench.rs (line 66)
22fn main() {
23    //let (width, height) = orbclient::get_display_size().unwrap();
24
25    let mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27    window.set(Color::rgb(255, 255, 255));
28
29    //create image data : a green square
30    let data = vec![Color::rgba(100, 200, 10, 3); 412500];
31    let mut data2 = vec![Color::rgba(200, 100, 10, 3); 412500];
32    let mut data3 = vec![Color::rgba(10, 100, 100, 3); 412500];
33    let mut data4 = vec![Color::rgba(10, 100, 200, 3); 800 * 400];
34
35    //draw image benchmarking
36    println!("Benchmarking implementations to draw an image on window:");
37
38    time!("image_legacy", {
39        for _i in 0..TIMES {
40            window.image_legacy(15, 15, 750, 550, &data[..]);
41        }
42    });
43
44    time!("image_over", {
45        for _i in 0..TIMES {
46            window.image_over(50, &data4[..]);
47        }
48    });
49
50    time!("image_fast", {
51        for _i in 0..TIMES {
52            window.image_fast(20, 20, 750, 550, &data2[..]);
53        }
54    });
55
56    time!("image_opaque", {
57        for _i in 0..TIMES {
58            window.image_opaque(30, 30, 750, 550, &data3[..]);
59        }
60    });
61
62    time!("image_roi_mut_blend", {
63        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
64        for _i in 0..TIMES {
65            ImageRef::from_renderer(&mut window)
66                .roi_mut(&Rect::new(40, 40, 750, 550))
67                .blend(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
68        }
69    });
70
71    time!("image_roi_mut_blit_mask", {
72        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
73        for _i in 0..TIMES {
74            ImageRef::from_renderer(&mut window)
75                .roi_mut(&Rect::new(40, 40, 750, 550))
76                .blit_mask(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
77        }
78    });
79
80    time!("image_roi_mut_blit", {
81        let data3_roi = ImageRef::from_data(750, 550, &mut data3[..]);
82        for _i in 0..TIMES {
83            ImageRef::from_renderer(&mut window)
84                .roi_mut(&Rect::new(50, 50, 750, 550))
85                .blit(&data3_roi.roi(&Rect::new(0, 0, 750, 550)));
86        }
87    });
88
89    time!("image_roi_mut_blit_over", {
90        let data4_roi = ImageRef::from_data(800, 400, &mut data4[..]);
91        for _i in 0..TIMES {
92            ImageRef::from_renderer(&mut window)
93                // .roi_mut(&Rect::new(10, 120, 790, 400)) // to test blit_over does not trigger
94                .roi_mut(&Rect::new(0, 120, 800, 400))
95                .blit(&data4_roi.roi(&Rect::new(0, 0, 800, 400)));
96        }
97    });
98
99    println!("------------------------------------------------");
100
101    window.sync();
102
103    'events: loop {
104        for event in window.events() {
105            match event.to_option() {
106                EventOption::Quit(_quit_event) => break 'events,
107                EventOption::Mouse(evt) => println!(
108                    "At position {:?} pixel color is : {:?}",
109                    (evt.x, evt.y),
110                    window.getpixel(evt.x, evt.y)
111                ),
112                event_option => println!("{:?}", event_option),
113            }
114        }
115    }
116}
Source

pub fn into_roi_mut(self, rect: Rect) -> ImageRoiMut<'a>

Convert into a ImageRoiMut with a specified rect of the image

Source

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

Draw the whole image on a renderer.

Trait Implementations§

Source§

impl<'a> Renderer for ImageRef<'a>

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 mode(&self) -> &Cell<Mode>

Set/get drawing mode
Source§

fn sync(&mut self) -> bool

Update the hardware buffer
Source§

fn update(&mut self) -> bool

Update the software buffer
Source§

fn update_rects(&mut self, _rects: &[(i32, i32, u32, u32)]) -> bool

Update the specified software buffer region (x, y, w, h)
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<'a> !Freeze for ImageRef<'a>

§

impl<'a> !RefUnwindSafe for ImageRef<'a>

§

impl<'a> !Sync for ImageRef<'a>

§

impl<'a> !UnwindSafe for ImageRef<'a>

§

impl<'a> Send for ImageRef<'a>

§

impl<'a> Unpin for ImageRef<'a>

§

impl<'a> UnsafeUnpin for ImageRef<'a>

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> 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, 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.