Skip to content

Commit 157f58d

Browse files
lisanhu38
authored andcommitted
bitmap backend implemented
1 parent aa1f79a commit 157f58d

File tree

2 files changed

+147
-9
lines changed

2 files changed

+147
-9
lines changed

Cargo.toml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,124 @@
1+
<<<<<<< HEAD
12
[workspace]
23
members = ["plotters", "plotters-backend", "plotters-bitmap", "plotters-svg"]
34
default-members = ["plotters"]
5+
=======
6+
[package]
7+
name = "plotters"
8+
version = "0.3.1"
9+
authors = ["Hao Hou <[email protected]>"]
10+
edition = "2018"
11+
license = "MIT"
12+
description = "A Rust drawing library focus on data plotting for both WASM and native applications"
13+
repository = "https://github.com/38/plotters"
14+
homepage = "https://plotters-rs.github.io/"
15+
keywords = ["WebAssembly", "Visualization", "Plotting", "Drawing"]
16+
categories = ["visualization", "wasm"]
17+
readme = "README.md"
18+
exclude = ["doc-template/*"]
19+
20+
[dependencies]
21+
num-traits = "0.2.14"
22+
chrono = { version = "0.4.19", optional = true }
23+
plotters-svg = {version = "^0.3.*", optional = true}
24+
base64 = "*"
25+
26+
[dependencies.plotters-backend]
27+
version = "^0.3"
28+
29+
[dependencies.plotters-bitmap]
30+
version = "^0.3"
31+
optional = true
32+
default_features = false
33+
34+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
35+
ttf-parser = { version = "0.12.0", optional = true }
36+
lazy_static = { version = "1.4.0", optional = true }
37+
pathfinder_geometry = { version = "0.5.1", optional = true }
38+
font-kit = { version = "0.10.0", optional = true }
39+
40+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.image]
41+
version = "0.23.14"
42+
optional = true
43+
default-features = false
44+
features = ["jpeg", "png", "bmp"]
45+
46+
[target.'cfg(target_arch = "wasm32")'.dependencies.wasm-bindgen]
47+
version = "0.2.62"
48+
49+
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
50+
version = "0.3.51"
51+
features = [
52+
"Document",
53+
"DomRect",
54+
"Element",
55+
"HtmlElement",
56+
"Node",
57+
"Window",
58+
"HtmlCanvasElement",
59+
"CanvasRenderingContext2d",
60+
]
61+
62+
[features]
63+
default = [
64+
"bitmap_backend", "bitmap_encoder", "bitmap_gif",
65+
"svg_backend",
66+
"chrono",
67+
"ttf",
68+
"image",
69+
"deprecated_items", "all_series", "all_elements",
70+
"full_palette"
71+
]
72+
all_series = ["area_series", "line_series", "point_series", "surface_series"]
73+
all_elements = ["errorbar", "candlestick", "boxplot", "histogram"]
74+
75+
# Tier 1 Backends
76+
bitmap_backend = ["plotters-bitmap", "ttf"]
77+
bitmap_encoder = ["plotters-bitmap/image_encoder"]
78+
bitmap_gif = ["plotters-bitmap/gif_backend"]
79+
svg_backend = ["plotters-svg"]
80+
81+
# Colors
82+
full_palette = []
83+
84+
# Elements
85+
errorbar = []
86+
candlestick = []
87+
boxplot = []
88+
89+
# Series
90+
histogram = []
91+
area_series = []
92+
line_series = []
93+
point_series = []
94+
surface_series = []
95+
96+
# Font implemnetation
97+
ttf = ["font-kit", "ttf-parser", "lazy_static", "pathfinder_geometry"]
98+
99+
# Misc
100+
datetime = ["chrono"]
101+
evcxr = ["svg_backend", "bitmap_backend", "image"]
102+
deprecated_items = [] # Keep some of the deprecated items for backward compatibility
103+
104+
[dev-dependencies]
105+
itertools = "0.10.0"
106+
criterion = "0.3.4"
107+
rayon = "1.5.1"
108+
serde_json = "1.0.64"
109+
serde = "1.0.126"
110+
serde_derive = "1.0.126"
111+
112+
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
113+
rand = "0.8.3"
114+
rand_distr = "0.4.0"
115+
rand_xorshift = "0.3.0"
116+
117+
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
118+
wasm-bindgen-test = "0.3.24"
119+
120+
[[bench]]
121+
name = "benchmark"
122+
harness = false
123+
path = "benches/main.rs"
124+
>>>>>>> 6bd0da5 (bitmap backend implemented)

plotters/src/evcxr.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::coord::Shift;
22
use crate::drawing::{DrawingArea, IntoDrawingArea};
33
use base64;
4+
use image::{png::PngEncoder, ImageBuffer, ImageError, Pixel, RgbImage, Rgb};
5+
use std::ops::Deref;
46
use plotters_svg::SVGBackend;
57
use plotters_bitmap::BitMapBackend;
68

@@ -43,9 +45,9 @@ pub fn evcxr_figure<
4345
SVGWrapper(buffer, "".to_string())
4446
}
4547

46-
pub struct BitmapWrapper(String, String);
48+
pub struct BitMapWrapper(String, String);
4749

48-
impl BitmapWrapper {
50+
impl BitMapWrapper {
4951
pub fn evcxr_display(&self) {
5052
println!("{:?}", self);
5153
}
@@ -56,7 +58,7 @@ impl BitmapWrapper {
5658
}
5759
}
5860

59-
impl std::fmt::Debug for BitmapWrapper {
61+
impl std::fmt::Debug for BitMapWrapper {
6062
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
6163
let svg = self.0.as_str();
6264
write!(
@@ -67,19 +69,34 @@ impl std::fmt::Debug for BitmapWrapper {
6769
}
6870
}
6971

72+
fn encode_png<P, Container>(img: &ImageBuffer<P, Container>) -> Result<Vec<u8>, ImageError>
73+
where
74+
P: Pixel<Subpixel = u8> + 'static,
75+
Container: Deref<Target = [P::Subpixel]>,
76+
{
77+
let mut buf = Vec::new();
78+
let encoder = PngEncoder::new(&mut buf);
79+
encoder.encode(img, img.width(), img.height(), P::COLOR_TYPE)?;
80+
Ok(buf)
81+
}
82+
7083
/// Start drawing an evcxr figure
7184
pub fn evcxr_bitmap_figure<
7285
Draw: FnOnce(DrawingArea<BitMapBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
7386
>(
7487
size: (u32, u32),
7588
draw: Draw,
76-
) -> SVGWrapper {
77-
let pixel_size = plotters_bitmap::bitmap_pixel::RGBPixel::PIXEL_SIZE;
78-
let mut buf = [u8; (size.0 as usize) * (size.1 as usize) * pixel_size];
79-
let root = BitMapBackend::with_buffer(&buf, size).into_drawing_area();
80-
let buffer = base64::encode(&buf);
89+
) -> BitMapWrapper {
90+
let pixel_size = 3;
91+
let mut buf = Vec::new();
92+
buf.resize((size.0 as usize) * (size.1 as usize) * pixel_size, 0);
93+
let root = BitMapBackend::with_buffer(&mut buf, size).into_drawing_area();
8194
draw(root).expect("Drawing failure");
82-
BitmapWrapper(buffer, "".to_string())
95+
let img = RgbImage::from_raw(size.0, size.1, buf).unwrap();
96+
let enc_buf = encode_png(&img).unwrap();
97+
let buffer = base64::encode(&enc_buf);
98+
BitMapWrapper(buffer, "".to_string())
99+
// todo!()
83100
}
84101

85102
pub fn evcxr_animation<

0 commit comments

Comments
 (0)