Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize BitMapBackend with Generic Pixel #76

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<img src="https://codecov.io/gh/38/plotters/branch/master/graph/badge.svg" />
</a>

Plotters is drawing library designed for rendering figures, plots, and charts, in pure rust. Plotters supports various types of back-ends,
including bitmap, vector graph, piston window, GTK/Cairo and WebAssembly.
Plotters is drawing library designed for rendering figures, plots, and charts, in pure rust. Plotters supports various types of back-ends,
including bitmap, vector graph, piston window, GTK/Cairo and WebAssembly.

- A new Plotters Developer's Guide is working in progress. The preview version is available at [here](https://plotters-rs.github.io/book).
- To try Plotters with interactive Jupyter notebook, or view [here](https://plotters-rs.github.io/plotters-doc-data/evcxr-jupyter-integration.html) for the static HTML version.
Expand Down Expand Up @@ -147,7 +147,7 @@ And the following code draws a quadratic function. `src/main.rs`,
```rust
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::new("plotters-doc-data/0.png", (640, 480)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/0.png", (640, 480)).into_drawing_area();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking change, isn't it ? If this is the case, we need to at least do something on that. AFAK, BitMapImage is one of the most widely used backend. So a lot of impact will going on

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick review. There are too many breaking changes in this PR and I didn't notice that color is directly coupled with buffer manipulation.

root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("y=x^2", ("sans-serif", 50).into_font())
Expand Down Expand Up @@ -225,7 +225,7 @@ figure
*This tutorial is now working in progress and isn't complete*

Thanks to the evcxr, now we have an interactive tutorial for Plotters!
To use the interactive notebook, you must have Jupyter and evcxr installed on your computer.
To use the interactive notebook, you must have Jupyter and evcxr installed on your computer.
Follow the instruction on [this page](https://github.com/google/evcxr/tree/master/evcxr_jupyter) below to install it.

After that, you should be able to start your Jupyter server locally and load the tutorial!
Expand All @@ -249,23 +249,23 @@ But Rust is one of the best languages fits the need.
Plotting in Rust can be as easy as most of the high-level programming languages. The Rust based plotting library
can be very easy to use.

* **Fast** If you need rendering a figure with trillions of data points,
Rust is a good choice. Rust's performance allows you to combine data processing step
* **Fast** If you need rendering a figure with trillions of data points,
Rust is a good choice. Rust's performance allows you to combine data processing step
and rendering step into a single application. When plotting in high-level programming languages,
e.g. Javascript or Python, data points must be down-sampled before feeding into the plotting
program because of the performance considerations. Rust is fast enough to do the data processing and visualization
within a single program. You can also integrate the
e.g. Javascript or Python, data points must be down-sampled before feeding into the plotting
program because of the performance considerations. Rust is fast enough to do the data processing and visualization
within a single program. You can also integrate the
figure rendering code into your application handling a huge amount of data and visualize it in real-time.

* **WebAssembly Support** Rust is one of few the language with the best WASM support. Plotting in Rust could be
* **WebAssembly Support** Rust is one of few the language with the best WASM support. Plotting in Rust could be
very useful for visualization on a web page and would have a huge performance improvement comparing to Javascript.

## Plotting on HTML5 canvas with WASM Backend

Plotters currently supports backend that uses the HTML5 canvas. To use the WASM support, you can simply use
Plotters currently supports backend that uses the HTML5 canvas. To use the WASM support, you can simply use
`CanvasBackend` instead of other backend and all other API remains the same!

There's a small demo for Plotters + WASM under `examples/wasm-demo` directory of this repo.
There's a small demo for Plotters + WASM under `examples/wasm-demo` directory of this repo.
To play with the deployed version, follow this [link](https://plumberserver.com/plotters-wasm-demo/index.html).


Expand All @@ -288,7 +288,7 @@ Plotters can use different drawing back-ends, including SVG, BitMap, and even re
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a 800*600 bitmap and start drawing
let mut backend = BitMapBackend::new("plotters-doc-data/1.png", (300, 200));
let mut backend = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/1.png", (300, 200));
// And if we want SVG backend
// let backend = SVGBackend::new("output.svg", (800, 600));
backend.draw_rect((50, 50), (200, 150), &RED, true)?;
Expand All @@ -309,7 +309,7 @@ Besides that, the drawing area also allows the customized coordinate system, by
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root_drawing_area =
BitMapBackend::new("plotters-doc-data/2.png", (300, 200)).into_drawing_area();
BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/2.png", (300, 200)).into_drawing_area();
// And we can split the drawing area into 3x3 grid
let child_drawing_areas = root_drawing_area.split_evenly((3, 3));
// Then we fill the drawing area with different color
Expand All @@ -335,7 +335,7 @@ To learn more about the element system, please read the [element module document
```rust
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::new("plotters-doc-data/3.png", (300, 200)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/3.png", (300, 200)).into_drawing_area();
root.fill(&WHITE)?;
// Draw an circle on the drawing area
root.draw(&Circle::new(
Expand All @@ -352,7 +352,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
### Composable Elements

Besides the built-in elements, elements can be composed into a logic group we called composed elements.
When composing new elements, the upper-left corner is given in the target coordinate, and a new pixel-based
When composing new elements, the upper-left corner is given in the target coordinate, and a new pixel-based
coordinate which has the upper-left corner defined as `(0,0)` is used for further element composition purpose.

For example, we can have an element which includes a dot and its coordinate.
Expand All @@ -361,7 +361,7 @@ For example, we can have an element which includes a dot and its coordinate.
use plotters::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::new("plotters-doc-data/4.png", (640, 480)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/4.png", (640, 480)).into_drawing_area();

root.fill(&RGBColor(240, 200, 200))?;

Expand Down Expand Up @@ -400,7 +400,7 @@ of the chart context object.
```rust
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::new("plotters-doc-data/5.png", (640, 480)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/5.png", (640, 480)).into_drawing_area();
root.fill(&WHITE);
let root = root.margin(10, 10, 10, 10);
// After this point, we should be able to draw construct a chart context
Expand Down Expand Up @@ -475,7 +475,7 @@ This behavior can also be turned off by setting `default_features = false`.

### List of Features

This is the full list of features that is defined by `Plotters` crate. Use `default_features = false` to disable those default enabled features, and then you should be able to cherry-pick what features you want to include into `Plotters` crate.
This is the full list of features that is defined by `Plotters` crate. Use `default_features = false` to disable those default enabled features, and then you should be able to cherry-pick what features you want to include into `Plotters` crate.

| Name | Description | Additional Dependency |Default?|
|---------|--------------|--------|------------|
Expand All @@ -495,12 +495,12 @@ This is the full list of features that is defined by `Plotters` crate. Use `defa

The WASM example requires using `wasm32` target to build. Using `cargo build` is likely to use the default target
which in most of the case is any of the x86 target. Thus you need add `--target=wasm32-unknown-unknown` in the cargo
parameter list to build it.
parameter list to build it.

* How to draw text/circle/point/rectangle/... on the top of chart ?
As you may realized, Plotters is a drawing library rather than a traditional data plotting library,

As you may realized, Plotters is a drawing library rather than a traditional data plotting library,
you have the freedom to draw anything you want on the drawing area.
Use `DrawingArea::draw` to draw any element on the drawing area.
Use `DrawingArea::draw` to draw any element on the drawing area.


12 changes: 6 additions & 6 deletions benches/benches/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn draw_func_1x1_seq(c: &mut Criterion) {
let mut buffer = vec![0; (W * H * 3) as usize];
c.bench_function("parallel::draw_func_1x1_seq", |b| {
b.iter(|| {
let root = BitMapBackend::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H)).into_drawing_area();
root.fill(&WHITE).unwrap();
draw_plot(&root, 2.0);
})
Expand All @@ -48,7 +48,7 @@ fn draw_func_4x4_seq(c: &mut Criterion) {
let mut buffer = vec![0; (W * H * 3) as usize];
c.bench_function("parallel::draw_func_4x4_seq", |b| {
b.iter(|| {
let root = BitMapBackend::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let areas = root.split_evenly((4, 4));
areas.iter().for_each(|area| draw_plot(&area, 2.0));
})
Expand All @@ -59,7 +59,7 @@ fn draw_func_4x4_parallel_and_blit(c: &mut Criterion) {
let mut buffer = vec![0; (W * H * 3) as usize];
c.bench_function("parallel::draw_func_4x4_parallel_and_blit", |b| {
b.iter(|| {
let root = BitMapBackend::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let areas = root.split_evenly((4, 4));
let mut elements: Vec<_> = areas
.iter()
Expand All @@ -83,7 +83,7 @@ fn draw_func_2x1_parallel_and_blit(c: &mut Criterion) {
let mut buffer = vec![0; (W * H * 3) as usize];
c.bench_function("parallel::draw_func_2x1_parallel_and_blit", |b| {
b.iter(|| {
let root = BitMapBackend::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let areas = root.split_evenly((2, 1));
let mut elements: Vec<_> = areas
.iter()
Expand All @@ -107,7 +107,7 @@ fn draw_func_2x1_inplace_parallel(c: &mut Criterion) {
let mut buffer = vec![0u8; (W * H * 3) as usize];
c.bench_function("parallel::draw_func_2x1_inplace", |b| {
b.iter(|| {
let mut back = BitMapBackend::with_buffer(&mut buffer, (W, H));
let mut back = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I previously mentioned, this change breaks too much.

back.split(&[H / 2])
.into_par_iter()
.for_each(|b| draw_plot(&b.into_drawing_area(), 2.0));
Expand All @@ -119,7 +119,7 @@ fn draw_func_2x1_seq(c: &mut Criterion) {
let mut buffer = vec![0u8; (W * H * 3) as usize];
c.bench_function("parallel::draw_func_2x1_seq", |b| {
b.iter(|| {
let root = BitMapBackend::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H)).into_drawing_area();
root.split_evenly((2, 1))
.iter_mut()
.for_each(|area| draw_plot(area, 2.0));
Expand Down
12 changes: 6 additions & 6 deletions benches/benches/rasterizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn draw_pixel(c: &mut Criterion) {

c.bench_function("rasterizer::draw_pixel", |b| {
b.iter(|| {
let mut root = BitMapBackend::with_buffer(&mut buffer, (W, H));
let mut root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H));
for x in 0..W / 10 {
for y in 0..H / 10 {
root.draw_pixel((x as i32, y as i32), &RGBColor(255, 0, 234).to_rgba())
Expand All @@ -25,7 +25,7 @@ fn draw_line(c: &mut Criterion) {

c.bench_function("rasterizer::draw_line", |b| {
b.iter(|| {
let mut root = BitMapBackend::with_buffer(&mut buffer, (W, H));
let mut root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H));
for y in 0..10 {
root.draw_line(
(0, 0),
Expand All @@ -43,7 +43,7 @@ fn fill_background(c: &mut Criterion) {

c.bench_function("rasterizer::fill_background", |b| {
b.iter(|| {
let root = BitMapBackend::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H)).into_drawing_area();
root.fill(&WHITE).unwrap();
})
});
Expand All @@ -54,7 +54,7 @@ fn fill_circle(c: &mut Criterion) {

c.bench_function("rasterizer::fill_circle", |b| {
b.iter(|| {
let mut root = BitMapBackend::with_buffer(&mut buffer, (W, H));
let mut root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H));
root.draw_circle((W as i32 / 2, H as i32 / 2), W / 2, &WHITE.to_rgba(), true)
.unwrap();
})
Expand All @@ -66,7 +66,7 @@ fn fill_background_red(c: &mut Criterion) {

c.bench_function("rasterizer::fill_background_red", |b| {
b.iter(|| {
let root = BitMapBackend::with_buffer(&mut buffer, (W, H)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H)).into_drawing_area();
root.fill(&RED).unwrap();
})
});
Expand All @@ -87,7 +87,7 @@ fn fill_hexagon(c: &mut Criterion) {

c.bench_function("rasterizer::fill_hexagon", |b| {
b.iter(|| {
let mut root = BitMapBackend::with_buffer(&mut buffer, (W, H));
let mut root = BitMapBackend::<image::Rgb<u8>>::with_buffer(&mut buffer, (W, H));
root.fill_polygon(vert.clone(), &RED).unwrap();
})
});
Expand Down
2 changes: 1 addition & 1 deletion doc-template/examples/chart.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::new("plotters-doc-data/5.png", (640, 480)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/5.png", (640, 480)).into_drawing_area();
root.fill(&WHITE);
let root = root.margin(10, 10, 10, 10);
// After this point, we should be able to draw construct a chart context
Expand Down
2 changes: 1 addition & 1 deletion doc-template/examples/composable_elements.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use plotters::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::new("plotters-doc-data/4.png", (640, 480)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/4.png", (640, 480)).into_drawing_area();

root.fill(&RGBColor(240, 200, 200))?;

Expand Down
2 changes: 1 addition & 1 deletion doc-template/examples/drawing_area.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root_drawing_area =
BitMapBackend::new("plotters-doc-data/2.png", (300, 200)).into_drawing_area();
BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/2.png", (300, 200)).into_drawing_area();
// And we can split the drawing area into 3x3 grid
let child_drawing_areas = root_drawing_area.split_evenly((3, 3));
// Then we fill the drawing area with different color
Expand Down
2 changes: 1 addition & 1 deletion doc-template/examples/drawing_backends.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a 800*600 bitmap and start drawing
let mut backend = BitMapBackend::new("plotters-doc-data/1.png", (300, 200));
let mut backend = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/1.png", (300, 200));
// And if we want SVG backend
// let backend = SVGBackend::new("output.svg", (800, 600));
backend.draw_rect((50, 50), (200, 150), &RED, true)?;
Expand Down
2 changes: 1 addition & 1 deletion doc-template/examples/elements.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::new("plotters-doc-data/3.png", (300, 200)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/3.png", (300, 200)).into_drawing_area();
root.fill(&WHITE)?;
// Draw an circle on the drawing area
root.draw(&Circle::new(
Expand Down
2 changes: 1 addition & 1 deletion doc-template/examples/quick_start.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::new("plotters-doc-data/0.png", (640, 480)).into_drawing_area();
let root = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/0.png", (640, 480)).into_drawing_area();
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("y=x^2", ("sans-serif", 50).into_font())
Expand Down
2 changes: 1 addition & 1 deletion examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn snowflake_iter(points: &[(f64, f64)]) -> Vec<(f64, f64)> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = BitMapBackend::gif("plotters-doc-data/animation.gif", (800, 600), 1_000)?
let root = BitMapBackend::<image::Rgb<u8>>::gif("plotters-doc-data/animation.gif", (800, 600), 1_000)?
.into_drawing_area();

for i in 0..8 {
Expand Down
2 changes: 1 addition & 1 deletion examples/area-chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};

let root =
BitMapBackend::new("plotters-doc-data/area-chart.png", (1024, 768)).into_drawing_area();
BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/area-chart.png", (1024, 768)).into_drawing_area();

root.fill(&WHITE)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/blit-bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::BufReader;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let root =
BitMapBackend::new("plotters-doc-data/blit-bitmap.png", (1024, 768)).into_drawing_area();
BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/blit-bitmap.png", (1024, 768)).into_drawing_area();
root.fill(&WHITE)?;

let mut chart = ChartBuilder::on(&root)
Expand Down
2 changes: 1 addition & 1 deletion examples/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use plotters::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let root_area =
BitMapBackend::new("plotters-doc-data/sample.png", (1024, 768)).into_drawing_area();
BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/sample.png", (1024, 768)).into_drawing_area();

root_area.fill(&WHITE)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ where

fn main() -> Result<(), Box<dyn Error>> {
draw_chart(TextDrawingBackend(vec![PixelState::Empty; 5000]).into_drawing_area())?;
let b = BitMapBackend::new("plotters-doc-data/console-example.png", (1024, 768))
let b = BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/console-example.png", (1024, 768))
.into_drawing_area();
b.fill(&WHITE)?;
draw_chart(b)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/errorbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let down_sampled = down_sample(&data[..]);

let root =
BitMapBackend::new("plotters-doc-data/errorbar.png", (1024, 768)).into_drawing_area();
BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/errorbar.png", (1024, 768)).into_drawing_area();

root.fill(&WHITE)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/histogram.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root =
BitMapBackend::new("plotters-doc-data/histogram.png", (640, 480)).into_drawing_area();
BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/histogram.png", (640, 480)).into_drawing_area();

root.fill(&WHITE)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/mandelbrot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Range;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let root =
BitMapBackend::new("plotters-doc-data/mandelbrot.png", (800, 600)).into_drawing_area();
BitMapBackend::<image::Rgb<u8>>::new("plotters-doc-data/mandelbrot.png", (800, 600)).into_drawing_area();

root.fill(&WHITE)?;

Expand Down
Loading