Image

Struct Image 

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

Represents an image with configurable settings and drawable components.

The Image struct combines configuration settings, an image buffer, and a collection of drawable components to represent a 2D canvas. It allows for easy configuration and drawing of various graphical elements on the canvas, such as circles, rectangles, lines, and text.

§Examples

use omage::{Image, Config, Components, Rgba};

// Create a new image with a specified configuration
let config = Config::new(800, 600, Rgba([255, 255, 255, 255]), Some(Rgba([0, 0, 0, 255])), "path/to/canvas/image.png", Some("path/to/font.ttf"));

let image = Image::new_with_config(config);

// Add drawable components to the image
let components = vec![
    Components::Circle(50, 50, 30, Rgba([255, 0, 0, 255])),
    Components::Rectangle(40, 60, 10, 20, Rgba([0, 255, 0, 255])),
    Components::Line(10, 10, 80, 80, Rgba([0, 0, 255, 255])),
    Components::Text(30, 40, 16, "Hello, Rust!", Rgba([255, 255, 255, 255]), Some((Rgba([0, 0, 0, 255]), 2))),
];

image.add_components(components);

§Fields

  • config: Optional configuration settings for the image canvas.
  • image_buffer: Optional image buffer containing pixel data.
  • components: Optional collection of drawable components to be rendered on the image.

§Methods

  • new: Creates a new Image instance with default settings or a specified configuration.
  • config: Adds config to Image
  • init : Initializes the Image
  • add_component: Adds a single drawable component to the image.
  • add_components: Adds a collection of drawable components to the image.
  • draw: Draws the configured image with its drawable components.

§Note

The Image struct provides a convenient way to configure and visualize graphical components on a canvas. It encapsulates the necessary logic for image creation and component rendering.

Implementations§

Source§

impl<'a> Image<'a>

Source

pub fn new() -> Self

Creates a new Image instance with default values.

Examples found in repository?
examples/anti.rs (line 10)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, None, "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 10, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
More examples
Hide additional examples
examples/circle.rs (line 10)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 300, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
examples/blend.rs (line 10)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
13    let circle2 = Components::Circle(
14        config.width / 2,
15        config.height / 2,
16        300,
17        Rgba([255, 0, 255, 120]),
18    );
19    let rectangle = Components::Rectangle(
20        100,
21        100,
22        config.width / 2 - 50,
23        config.height / 2 - 50,
24        Rgba([120, 0, 255, 19]),
25    );
26
27    image
28        .config(config)
29        .init()?
30        .add_components(vec![&circle1, &circle2, &rectangle])
31        .draw()?;
32    Ok(())
33}
examples/line.rs (line 17)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        BLACK,
12        Some(GREEN),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
20    let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
21    let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
22    let text = Components::Text(
23        WIDTH / 2 - 210,
24        HEIGHT / 2 - 250,
25        40,
26        "Xiaolin Wu's Line Algorithm",
27        BLACK,
28        Some((GREEN, 3)),
29    );
30
31    image
32        .config(config)
33        .init()?
34        .add_components(vec![&line1, &line2, &circle, &text])
35        .draw()?;
36    Ok(())
37}
examples/text.rs (line 17)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        Rgba([255, 255, 255, 0]),
12        Some(WHITE),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
20    let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
21    let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));
22
23    let text = "OMAGE";
24    let text = Components::Text(
25        config.width / 2 - 40,
26        config.height / 2 - 25,
27        50,
28        text,
29        Rgba([255, 255, 255, 255]),
30        Some((BLACK, 3)),
31    );
32
33    image
34        .config(config)
35        .init()?
36        .add_components(vec![&text, &circle1, &circle2, &circle3])
37        .draw()?;
38    Ok(())
39}
Source

pub fn config(&mut self, config: Config) -> &mut Self

Sets the configuration for the image.

Examples found in repository?
examples/anti.rs (line 14)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, None, "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 10, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
More examples
Hide additional examples
examples/circle.rs (line 14)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 300, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
examples/blend.rs (line 28)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
13    let circle2 = Components::Circle(
14        config.width / 2,
15        config.height / 2,
16        300,
17        Rgba([255, 0, 255, 120]),
18    );
19    let rectangle = Components::Rectangle(
20        100,
21        100,
22        config.width / 2 - 50,
23        config.height / 2 - 50,
24        Rgba([120, 0, 255, 19]),
25    );
26
27    image
28        .config(config)
29        .init()?
30        .add_components(vec![&circle1, &circle2, &rectangle])
31        .draw()?;
32    Ok(())
33}
examples/line.rs (line 32)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        BLACK,
12        Some(GREEN),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
20    let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
21    let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
22    let text = Components::Text(
23        WIDTH / 2 - 210,
24        HEIGHT / 2 - 250,
25        40,
26        "Xiaolin Wu's Line Algorithm",
27        BLACK,
28        Some((GREEN, 3)),
29    );
30
31    image
32        .config(config)
33        .init()?
34        .add_components(vec![&line1, &line2, &circle, &text])
35        .draw()?;
36    Ok(())
37}
examples/text.rs (line 34)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        Rgba([255, 255, 255, 0]),
12        Some(WHITE),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
20    let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
21    let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));
22
23    let text = "OMAGE";
24    let text = Components::Text(
25        config.width / 2 - 40,
26        config.height / 2 - 25,
27        50,
28        text,
29        Rgba([255, 255, 255, 255]),
30        Some((BLACK, 3)),
31    );
32
33    image
34        .config(config)
35        .init()?
36        .add_components(vec![&text, &circle1, &circle2, &circle3])
37        .draw()?;
38    Ok(())
39}
Source

pub fn init(&mut self) -> Result<&mut Self, Box<dyn Error>>

Initializes the image with the configured settings.

§Errors

Returns an error if no configuration is provided.

Examples found in repository?
examples/anti.rs (line 14)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, None, "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 10, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
More examples
Hide additional examples
examples/circle.rs (line 14)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 300, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
examples/blend.rs (line 29)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
13    let circle2 = Components::Circle(
14        config.width / 2,
15        config.height / 2,
16        300,
17        Rgba([255, 0, 255, 120]),
18    );
19    let rectangle = Components::Rectangle(
20        100,
21        100,
22        config.width / 2 - 50,
23        config.height / 2 - 50,
24        Rgba([120, 0, 255, 19]),
25    );
26
27    image
28        .config(config)
29        .init()?
30        .add_components(vec![&circle1, &circle2, &rectangle])
31        .draw()?;
32    Ok(())
33}
examples/line.rs (line 33)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        BLACK,
12        Some(GREEN),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
20    let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
21    let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
22    let text = Components::Text(
23        WIDTH / 2 - 210,
24        HEIGHT / 2 - 250,
25        40,
26        "Xiaolin Wu's Line Algorithm",
27        BLACK,
28        Some((GREEN, 3)),
29    );
30
31    image
32        .config(config)
33        .init()?
34        .add_components(vec![&line1, &line2, &circle, &text])
35        .draw()?;
36    Ok(())
37}
examples/text.rs (line 35)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        Rgba([255, 255, 255, 0]),
12        Some(WHITE),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
20    let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
21    let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));
22
23    let text = "OMAGE";
24    let text = Components::Text(
25        config.width / 2 - 40,
26        config.height / 2 - 25,
27        50,
28        text,
29        Rgba([255, 255, 255, 255]),
30        Some((BLACK, 3)),
31    );
32
33    image
34        .config(config)
35        .init()?
36        .add_components(vec![&text, &circle1, &circle2, &circle3])
37        .draw()?;
38    Ok(())
39}
Source

pub fn add_component(&mut self, component: &'a Component) -> &mut Self

Adds a single component to the image.

Examples found in repository?
examples/anti.rs (line 14)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, None, "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 10, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
More examples
Hide additional examples
examples/circle.rs (line 14)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 300, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
Source

pub fn add_components(&mut self, components: Vec<&'a Component>) -> &mut Self

Adds multiple components to the image.

Examples found in repository?
examples/blend.rs (line 30)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
13    let circle2 = Components::Circle(
14        config.width / 2,
15        config.height / 2,
16        300,
17        Rgba([255, 0, 255, 120]),
18    );
19    let rectangle = Components::Rectangle(
20        100,
21        100,
22        config.width / 2 - 50,
23        config.height / 2 - 50,
24        Rgba([120, 0, 255, 19]),
25    );
26
27    image
28        .config(config)
29        .init()?
30        .add_components(vec![&circle1, &circle2, &rectangle])
31        .draw()?;
32    Ok(())
33}
More examples
Hide additional examples
examples/line.rs (line 34)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        BLACK,
12        Some(GREEN),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
20    let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
21    let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
22    let text = Components::Text(
23        WIDTH / 2 - 210,
24        HEIGHT / 2 - 250,
25        40,
26        "Xiaolin Wu's Line Algorithm",
27        BLACK,
28        Some((GREEN, 3)),
29    );
30
31    image
32        .config(config)
33        .init()?
34        .add_components(vec![&line1, &line2, &circle, &text])
35        .draw()?;
36    Ok(())
37}
examples/text.rs (line 36)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        Rgba([255, 255, 255, 0]),
12        Some(WHITE),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
20    let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
21    let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));
22
23    let text = "OMAGE";
24    let text = Components::Text(
25        config.width / 2 - 40,
26        config.height / 2 - 25,
27        50,
28        text,
29        Rgba([255, 255, 255, 255]),
30        Some((BLACK, 3)),
31    );
32
33    image
34        .config(config)
35        .init()?
36        .add_components(vec![&text, &circle1, &circle2, &circle3])
37        .draw()?;
38    Ok(())
39}
Source

pub fn draw(&self) -> Result<(), Box<dyn Error>>

Draws all the components on the image and saves it to the specified path.

§Errors

Returns an error if there is no configuration, no components, or an issue occurs during drawing or saving.

Examples found in repository?
examples/anti.rs (line 14)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, None, "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 10, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
More examples
Hide additional examples
examples/circle.rs (line 14)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle = Components::Circle(config.width / 2, config.height / 2, 300, RED);
13
14    image.config(config).init()?.add_component(&circle).draw()?;
15    Ok(())
16}
examples/blend.rs (line 31)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10    let mut image = Image::new();
11
12    let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
13    let circle2 = Components::Circle(
14        config.width / 2,
15        config.height / 2,
16        300,
17        Rgba([255, 0, 255, 120]),
18    );
19    let rectangle = Components::Rectangle(
20        100,
21        100,
22        config.width / 2 - 50,
23        config.height / 2 - 50,
24        Rgba([120, 0, 255, 19]),
25    );
26
27    image
28        .config(config)
29        .init()?
30        .add_components(vec![&circle1, &circle2, &rectangle])
31        .draw()?;
32    Ok(())
33}
examples/line.rs (line 35)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        BLACK,
12        Some(GREEN),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
20    let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
21    let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
22    let text = Components::Text(
23        WIDTH / 2 - 210,
24        HEIGHT / 2 - 250,
25        40,
26        "Xiaolin Wu's Line Algorithm",
27        BLACK,
28        Some((GREEN, 3)),
29    );
30
31    image
32        .config(config)
33        .init()?
34        .add_components(vec![&line1, &line2, &circle, &text])
35        .draw()?;
36    Ok(())
37}
examples/text.rs (line 37)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let config = Config::new(
9        WIDTH,
10        HEIGHT,
11        Rgba([255, 255, 255, 0]),
12        Some(WHITE),
13        "output.png",
14        Some("./fonts/Roboto-Medium.ttf"),
15    );
16
17    let mut image = Image::new();
18
19    let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
20    let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
21    let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));
22
23    let text = "OMAGE";
24    let text = Components::Text(
25        config.width / 2 - 40,
26        config.height / 2 - 25,
27        50,
28        text,
29        Rgba([255, 255, 255, 255]),
30        Some((BLACK, 3)),
31    );
32
33    image
34        .config(config)
35        .init()?
36        .add_components(vec![&text, &circle1, &circle2, &circle3])
37        .draw()?;
38    Ok(())
39}

Auto Trait Implementations§

§

impl<'a> Freeze for Image<'a>

§

impl<'a> RefUnwindSafe for Image<'a>

§

impl<'a> Send for Image<'a>

§

impl<'a> Sync for Image<'a>

§

impl<'a> Unpin for Image<'a>

§

impl<'a> UnwindSafe for Image<'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> 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, 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.