tinydraw

Crates.iotinydraw
lib.rstinydraw
version0.1.1
sourcesrc
created_at2023-02-16 15:17:29.215701
updated_at2023-02-16 15:19:44.774834
descriptionA small library for 2D drawing in Rust
homepage
repositoryhttps://github.com/DarkLord76865/tinydraw-rs
max_upload_size
id786839
size120,410
Antonio Mamić (amamic1803)

documentation

README

tinydraw-rs

tinydraw is a small library for 2D drawing in Rust


It's a simple crate used for drawing basic, anti-aliased shapes onto images, written in pure Rust. Support for reading and exporting images as PNG or bytes is included (dependencies).

Documentation

Available Shapes

  • line
  • rectangle
  • circle
  • ellipse

Example

use tinydraw::ImageRGB8;

fn main() { 
    let background_color: [u8; 3] = [255, 155, 0];
    let mut image: ImageRGB8 = ImageRGB8::new(640, 360, background_color);
  
    image.draw_line(0, 0, 639, 359, [255, 255, 255], 1, 1.0);
    image.draw_line(0, 359, 639, 0, [255, 255, 255], 1, 1.0);
    image.draw_rectangle(0, 0, 639, 359, [255, 255, 255], 3, 1.0);
    image.draw_ellipse(319, 179, 300, 150, [0, 0, 0], 0, 0.5);
    image.draw_circle(149, 179, 30, [255, 255, 255], 0, 1.0);
    image.draw_circle(149, 179, 20, [0, 0, 0], 0, 1.0);
    image.draw_circle(489, 179, 30, [255, 255, 255], 0, 1.0);
    image.draw_circle(489, 179, 20, [0, 0, 0], 0, 1.0);
    image.draw_ellipse(319, 90, 80, 30, [255, 255, 255], 0, 1.0);
    image.draw_ellipse(319, 90, 60, 20, [0, 0, 0], 0, 1.0);
  
    image.to_png("image.png").unwrap();
}

This code generates the following image:

image

Limitations

  • thickness above 1 doesn't work for:
    • line
    • circle
    • ellipse
  • coordinates exceeding the image bounds don't work for:
    • rectangle
    • circle
    • ellipse
  • only RGB images with bit depth of 8 are currently supported

Dependencies

bytemuck (reading, exporting bytes)

png (reading, exporting PNG)

Development

I intend to fix the limitations and perhaps add more shapes in the future. It depends on my free time and whether there will be any interest for this crate. If you encounter a bug or have any suggestions, feel free to open an issue. If you want to contribute, feel free to open a pull request.

References

Wikipedia - Xiaolin Wu's line algorithm

GeeksforGeeks - Anti-aliased Line | Xiaolin Wu’s algorithm

Stephan Brumme - Drawing Antialiased Circles and Ellipses

David Moksha - Fast, Antialiased Circles and Ellipses from Xiaolin Wu’s concepts

Commit count: 49

cargo fmt