Skip to content

Commit 22740f2

Browse files
lisanhu38
authored andcommitted
attempt to use blit_bitmap to convert bitmap to svg
1 parent f74505b commit 22740f2

File tree

1 file changed

+6
-34
lines changed

1 file changed

+6
-34
lines changed

plotters/src/evcxr.rs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use crate::coord::Shift;
22
use crate::drawing::{DrawingArea, IntoDrawingArea};
3-
use image::{png::PngEncoder, ImageBuffer, ImageError, Pixel, Rgb, RgbImage};
4-
use plotters_bitmap::BitMapBackend;
53
use plotters_svg::SVGBackend;
64
use plotters_backend::DrawingBackend;
7-
use std::ops::Deref;
5+
use plotters_bitmap::BitMapBackend;
86

97
/// The wrapper for the generated SVG
108
pub struct SVGWrapper(String, String);
@@ -73,20 +71,7 @@ impl std::fmt::Debug for BitMapWrapper {
7371
}
7472
}
7573

76-
// #[cfg(feature = "evcxr_bitmap_figure")]
77-
#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
78-
fn encode_png<P, Container>(img: &ImageBuffer<P, Container>) -> Result<Vec<u8>, ImageError>
79-
where
80-
P: Pixel<Subpixel = u8> + 'static,
81-
Container: Deref<Target = [P::Subpixel]>,
82-
{
83-
let mut buf = Vec::new();
84-
let encoder = PngEncoder::new(&mut buf);
85-
encoder.encode(img, img.width(), img.height(), P::COLOR_TYPE)?;
86-
Ok(buf)
87-
}
8874

89-
// #[cfg(feature = "evcxr_bitmap_figure")]
9075
/// Start drawing an evcxr figure
9176
#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
9277
pub fn evcxr_bitmap_figure<
@@ -100,24 +85,11 @@ pub fn evcxr_bitmap_figure<
10085
buf.resize((size.0 as usize) * (size.1 as usize) * pixel_size, 0);
10186
let root = BitMapBackend::with_buffer(&mut buf, size).into_drawing_area();
10287
draw(root).expect("Drawing failure");
103-
let img = RgbImage::from_raw(size.0, size.1, buf).unwrap();
10488
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())
89+
{
90+
let mut svg_root = SVGBackend::with_string(&mut buffer, size);
91+
svg_root.blit_bitmap((0, 0), size, &buf).expect("Failure converting to SVG");
92+
}
93+
SVGWrapper(buffer.clone(), "".to_string())
11194
}
11295

113-
// #[cfg(feature = "evcxr_bitmap_figure")]
114-
// pub fn evcxr_animation<
115-
// Draw: FnOnce(DrawingArea<SVGBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
116-
// >(
117-
// drawing_area: &DrawingArea<SVGBackend, Shift>,
118-
// draws: Draw,
119-
// frames: usize,
120-
// interval: usize,
121-
// ) -> SVGWrapper {
122-
// todo!();
123-
// }

0 commit comments

Comments
 (0)