Skip to content

Commit f74505b

Browse files
lisanhu38
authored andcommitted
merging stash edits
1 parent e13f2a2 commit f74505b

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

plotters/src/evcxr.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::coord::Shift;
22
use crate::drawing::{DrawingArea, IntoDrawingArea};
3-
use base64;
43
use image::{png::PngEncoder, ImageBuffer, ImageError, Pixel, Rgb, RgbImage};
54
use plotters_bitmap::BitMapBackend;
65
use plotters_svg::SVGBackend;
6+
use plotters_backend::DrawingBackend;
77
use std::ops::Deref;
88

99
/// The wrapper for the generated SVG
@@ -45,10 +45,10 @@ pub fn evcxr_figure<
4545
SVGWrapper(buffer, "".to_string())
4646
}
4747

48-
#[cfg(feature = "evcxr_bitmap_figure")]
48+
#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
4949
pub struct BitMapWrapper(String, String);
5050

51-
#[cfg(feature = "evcxr_bitmap_figure")]
51+
#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
5252
impl BitMapWrapper {
5353
pub fn evcxr_display(&self) {
5454
println!("{:?}", self);
@@ -60,7 +60,8 @@ impl BitMapWrapper {
6060
}
6161
}
6262

63-
#[cfg(feature = "evcxr_bitmap_figure")]
63+
// #[cfg(feature = "evcxr_bitmap_figure")]
64+
#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
6465
impl std::fmt::Debug for BitMapWrapper {
6566
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
6667
let enc = self.0.as_str();
@@ -72,7 +73,8 @@ impl std::fmt::Debug for BitMapWrapper {
7273
}
7374
}
7475

75-
#[cfg(feature = "evcxr_bitmap_figure")]
76+
// #[cfg(feature = "evcxr_bitmap_figure")]
77+
#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
7678
fn encode_png<P, Container>(img: &ImageBuffer<P, Container>) -> Result<Vec<u8>, ImageError>
7779
where
7880
P: Pixel<Subpixel = u8> + 'static,
@@ -84,23 +86,28 @@ where
8486
Ok(buf)
8587
}
8688

87-
#[cfg(feature = "evcxr_bitmap_figure")]
89+
// #[cfg(feature = "evcxr_bitmap_figure")]
8890
/// Start drawing an evcxr figure
91+
#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
8992
pub fn evcxr_bitmap_figure<
9093
Draw: FnOnce(DrawingArea<BitMapBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
9194
>(
9295
size: (u32, u32),
9396
draw: Draw,
94-
) -> BitMapWrapper {
97+
) -> SVGWrapper {
9598
let pixel_size = 3;
9699
let mut buf = Vec::new();
97100
buf.resize((size.0 as usize) * (size.1 as usize) * pixel_size, 0);
98101
let root = BitMapBackend::with_buffer(&mut buf, size).into_drawing_area();
99102
draw(root).expect("Drawing failure");
100103
let img = RgbImage::from_raw(size.0, size.1, buf).unwrap();
101-
let enc_buf = encode_png(&img).unwrap();
102-
let buffer = base64::encode(&enc_buf);
103-
BitMapWrapper(buffer, "".to_string())
104+
let mut buffer = "".to_string();
105+
let svg_root = SVGBackend::with_string(&mut buffer, size).into_drawing_area();
106+
svg_root.blit_bitmap((0, 0), size, buf).expect("Failure converting to SVG");
107+
// draw(svg_root).expect("Failure converting bitmap to SVG");
108+
// let enc_buf = encode_png(&img).unwrap();
109+
// let buffer = base64::encode(&enc_buf);
110+
SVGWrapper(buffer, "".to_string())
104111
}
105112

106113
// #[cfg(feature = "evcxr_bitmap_figure")]

0 commit comments

Comments
 (0)