GithubHelp home page GithubHelp logo

amamic1803 / tinydraw-rs Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 120 KB

A small 2D drawing library in Rust

License: MIT License

Rust 100.00%
antialiasing crates-io drawing image rust-crate rust-lang shapes

tinydraw-rs's Introduction

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

Crate

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

tinydraw-rs's People

Contributors

amamic1803 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

tinydraw-rs's Issues

Small bug when drawing horizontal lines

Trying out the tinydraw crate for some simple drawing task… There I noticed that when drawing completely horizontal lines only half of the line is visible. Here is a little code example visualizing this behaviour.

fn main() {                                                                                                                                                                                                                                                                     
    let width: u32 = 1920;
    let height: u32 = 1080;
    // let h_center = width / 2;
    let v_center = height / 2;
    let background: [u8; 3] = [64, 64, 128];
    let mut image = tinydraw::ImageRGB8::new(width as usize, height as usize, background);

    let bug_color: [u8; 3] = [255, 0, 0];
    image.draw_line(
        0,
        v_center as usize,
        width as usize,
        v_center as usize,
        bug_color,
        1,
        0.5,
    );

    let okay_color: [u8; 3] = [0, 255, 0];
    image.draw_line(
        0,
        v_center as usize - 50,
        width as usize,
        v_center as usize - 51, // FIXME: should also be -50
        okay_color,
        1,
        0.5,
    );

    image.to_png("output.png").unwrap();
}

And here is how it looks like.

output

The horizontal red line stops somewhere in the middle. The green line is just for reference and "fakes" a horizontal line (sets 1px off between y1 and y2).

Panics due to overflow in usize subtraction

thread 'main' panicked at /home/nils/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tinydraw-0.1.1/src/image.rs:325:47:
attempt to subtract with overflow

I didn't print/debug the exact x1/y1/x2/y2 values yet. Very likely those are huge due to an incorrect conversion from signed to unsigned on the input side…

Update: Yep, the values became huge as assumed! To simplify debugging a little I use smaller values in i16 range (approximately +/-[0..32000]). It seems that the algorithm panics when y2 < y1 && x2 > x1.

(36,81) -> (28,43)   Okay
(28,43) -> (80,75)   Okay
(80,75) -> (88,49)   Panic!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.