Debian Codegraph @

src/bitmap/test.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 use super::*;
2
3 #[test]
4 fn test_bitmap_backend() {
5 use plotters::prelude::*;
6
7 let mut buffer = vec![0; 10 * 10 * 3];
8
9 {
10 let back = BitMapBackend::with_buffer(&mut buffer, (10, 10));
11
12 let area = back.into_drawing_area();
13 area.fill(&WHITE).unwrap();
14 area.draw(&PathElement::new(vec![(0, 0), (10, 10)], RED.filled()))
15 .unwrap();
16 area.present().unwrap();
17 }
18
19 for i in 0..10 {
20 assert_eq!(buffer[i * 33], 255);
21 assert_eq!(buffer[i * 33 + 1], 0);
22 assert_eq!(buffer[i * 33 + 2], 0);
23 buffer[i * 33] = 255;
24 buffer[i * 33 + 1] = 255;
25 buffer[i * 33 + 2] = 255;
26 }
27
28 assert!(buffer.into_iter().all(|x| x == 255));
29 }
30
31 #[cfg(test)]
32 #[test]
33 fn test_bitmap_backend_fill_half() {
34 use plotters::prelude::*;
35 let mut buffer = vec![0; 10 * 10 * 3];
36
37 {
38 let back = BitMapBackend::with_buffer(&mut buffer, (10, 10));
39
40 let area = back.into_drawing_area();
41 area.draw(&Rectangle::new([(0, 0), (5, 10)], RED.filled()))
42 .unwrap();
43 area.present().unwrap();
44 }
45 for x in 0..10 {
46 for y in 0..10 {
47 assert_eq!(
48 buffer[(y * 10 + x) as usize * 3 + 0],
49 if x < 5 { 255 } else { 0 }
50 );
51 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], 0);
52 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], 0);
53 }
54 }
55
56 let mut buffer = vec![0; 10 * 10 * 3];
57
58 {
59 let back = BitMapBackend::with_buffer(&mut buffer, (10, 10));
60
61 let area = back.into_drawing_area();
62 area.draw(&Rectangle::new([(0, 0), (10, 5)], RED.filled()))
63 .unwrap();
64 area.present().unwrap();
65 }
66 for x in 0..10 {
67 for y in 0..10 {
68 assert_eq!(
69 buffer[(y * 10 + x) as usize * 3 + 0],
70 if y < 5 { 255 } else { 0 }
71 );
72 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], 0);
73 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], 0);
74 }
75 }
76 }
77
78 #[cfg(test)]
79 #[test]
80 fn test_bitmap_backend_blend() {
81 use plotters::prelude::*;
82 let mut buffer = vec![255; 10 * 10 * 3];
83
84 {
85 let back = BitMapBackend::with_buffer(&mut buffer, (10, 10));
86
87 let area = back.into_drawing_area();
88 area.draw(&Rectangle::new(
89 [(0, 0), (5, 10)],
90 RGBColor(0, 100, 200).mix(0.2).filled(),
91 ))
92 .unwrap();
93 area.present().unwrap();
94 }
95
96 for x in 0..10 {
97 for y in 0..10 {
98 let (r, g, b) = if x < 5 {
99 (205, 225, 245)
100 } else {
101 (255, 255, 255)
102 };
103 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 0], r);
104 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], g);
105 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], b);
106 }
107 }
108 }
109
110 #[cfg(test)]
111 #[test]
112 fn test_bitmap_backend_split_and_fill() {
113 use plotters::prelude::*;
114 let mut buffer = vec![255; 10 * 10 * 3];
115
116 {
117 let mut back = BitMapBackend::with_buffer(&mut buffer, (10, 10));
118
119 for (sub_backend, color) in back.split(&[5]).into_iter().zip([&RED, &GREEN].iter()) {
120 sub_backend.into_drawing_area().fill(*color).unwrap();
121 }
122 }
123
124 for x in 0..10 {
125 for y in 0..10 {
126 let (r, g, b) = if y < 5 { (255, 0, 0) } else { (0, 255, 0) };
127 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 0], r);
128 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], g);
129 assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], b);
130 }
131 }
132 }
133
134 #[cfg(test)]
135 #[test]
136 fn test_draw_rect_out_of_range() {
137 use plotters::prelude::*;
138 let mut buffer = vec![0; 1099 * 1000 * 3];
139
140 {
141 let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000));
142
143 back.draw_line((1100, 0), (1100, 999), &RED.to_rgba())
144 .unwrap();
145 back.draw_line((0, 1100), (999, 1100), &RED.to_rgba())
146 .unwrap();
147 back.draw_rect((1100, 0), (1100, 999), &RED.to_rgba(), true)
148 .unwrap();
149 }
150
151 for x in 0..1000 {
152 for y in 0..1000 {
153 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 0], 0);
154 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 1], 0);
155 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 2], 0);
156 }
157 }
158 }
159
160 #[cfg(test)]
161 #[test]
162 fn test_draw_rect_exclude_bottom_right() {
163 use plotters::prelude::*;
164 let mut buffer = vec![0; 100 * 100 * 3];
165
166 {
167 let mut back = BitMapBackend::with_buffer(&mut buffer, (100, 100));
168
169 back.draw_rect((0, 0), (0, 0), &RED.to_rgba(), true)
170 .unwrap();
171 }
172
173 assert_eq!(&buffer[0..3], &[0, 0, 0]);
174 }
175
176 #[cfg(test)]
177 #[test]
178 fn test_draw_line_out_of_range() {
179 use plotters::prelude::*;
180 let mut buffer = vec![0; 1000 * 1000 * 3];
181
182 {
183 let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000));
184
185 back.draw_line((-1000, -1000), (2000, 2000), &WHITE.to_rgba())
186 .unwrap();
187
188 back.draw_line((999, -1000), (999, 2000), &WHITE.to_rgba())
189 .unwrap();
190 }
191
192 for x in 0..1000 {
193 for y in 0..1000 {
194 let expected_value = if x == y || x == 999 { 255 } else { 0 };
195 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 0], expected_value);
196 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 1], expected_value);
197 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 2], expected_value);
198 }
199 }
200 }
201
202 #[cfg(test)]
203 #[test]
204 fn test_bitmap_blend_large() {
205 use plotters::prelude::*;
206 let mut buffer = vec![0; 1000 * 1000 * 3];
207
208 for fill_color in [RED, GREEN, BLUE].iter() {
209 buffer.iter_mut().for_each(|x| *x = 0);
210
211 {
212 let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000));
213
214 back.draw_rect((0, 0), (1000, 1000), &WHITE.mix(0.1), true)
215 .unwrap(); // should be (24, 24, 24)
216 back.draw_rect((0, 0), (100, 100), &fill_color.mix(0.5), true)
217 .unwrap(); // should be (139, 24, 24)
218 }
219
220 for x in 0..1000 {
221 for y in 0..1000 {
222 let expected_value = if x < 100 && y < 100 {
223 let (r, g, b) = fill_color.to_rgba().rgb();
224 (
225 if r > 0 { 139 } else { 12 },
226 if g > 0 { 139 } else { 12 },
227 if b > 0 { 139 } else { 12 },
228 )
229 } else {
230 (24, 24, 24)
231 };
232 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 0], expected_value.0);
233 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 1], expected_value.1);
234 assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 2], expected_value.2);
235 }
236 }
237 }
238 }
239
240 #[cfg(test)]
241 #[test]
242 fn test_bitmap_bgrx_pixel_format() {
243 use crate::{bitmap_pixel::BGRXPixel, BitMapBackend};
244 use plotters::prelude::{Color, RGBColor, BLACK, BLUE, GREEN, RED, WHITE, YELLOW};
245 let mut rgb_buffer = vec![0; 1000 * 1000 * 3];
246 let mut bgrx_buffer = vec![0; 1000 * 1000 * 4];
247
248 {
249 let mut rgb_back = BitMapBackend::with_buffer(&mut rgb_buffer, (1000, 1000));
250 let mut bgrx_back =
251 BitMapBackend::<BGRXPixel>::with_buffer_and_format(&mut bgrx_buffer, (1000, 1000))
252 .unwrap();
253
254 rgb_back
255 .draw_rect((0, 0), (1000, 1000), &BLACK, true)
256 .unwrap();
257 bgrx_back
258 .draw_rect((0, 0), (1000, 1000), &BLACK, true)
259 .unwrap();
260
261 rgb_back
262 .draw_rect(
263 (0, 0),
264 (1000, 1000),
265 &RGBColor(0xaa, 0xbb, 0xcc).mix(0.85),
266 true,
267 )
268 .unwrap();
269 bgrx_back
270 .draw_rect(
271 (0, 0),
272 (1000, 1000),
273 &RGBColor(0xaa, 0xbb, 0xcc).mix(0.85),
274 true,
275 )
276 .unwrap();
277
278 rgb_back
279 .draw_rect((0, 0), (1000, 1000), &RED.mix(0.85), true)
280 .unwrap();
281 bgrx_back
282 .draw_rect((0, 0), (1000, 1000), &RED.mix(0.85), true)
283 .unwrap();
284
285 rgb_back.draw_circle((300, 300), 100, &GREEN, true).unwrap();
286 bgrx_back
287 .draw_circle((300, 300), 100, &GREEN, true)
288 .unwrap();
289
290 rgb_back.draw_rect((10, 10), (50, 50), &BLUE, true).unwrap();
291 bgrx_back
292 .draw_rect((10, 10), (50, 50), &BLUE, true)
293 .unwrap();
294
295 rgb_back
296 .draw_rect((10, 10), (50, 50), &WHITE, true)
297 .unwrap();
298 bgrx_back
299 .draw_rect((10, 10), (50, 50), &WHITE, true)
300 .unwrap();
301
302 rgb_back
303 .draw_rect((10, 10), (15, 50), &YELLOW, true)
304 .unwrap();
305 bgrx_back
306 .draw_rect((10, 10), (15, 50), &YELLOW, true)
307 .unwrap();
308 }
309
310 for x in 0..1000 {
311 for y in 0..1000 {
312 assert!(
313 (rgb_buffer[y * 3000 + x * 3 + 0] as i32
314 - bgrx_buffer[y * 4000 + x * 4 + 2] as i32)
315 .abs()
316 <= 1
317 );
318 assert!(
319 (rgb_buffer[y * 3000 + x * 3 + 1] as i32
320 - bgrx_buffer[y * 4000 + x * 4 + 1] as i32)
321 .abs()
322 <= 1
323 );
324 assert!(
325 (rgb_buffer[y * 3000 + x * 3 + 2] as i32
326 - bgrx_buffer[y * 4000 + x * 4 + 0] as i32)
327 .abs()
328 <= 1
329 );
330 }
331 }
332 }
333 #[cfg(test)]
334 #[test]
335 fn test_draw_simple_lines() {
336 use plotters::prelude::*;
337 let mut buffer = vec![0; 1000 * 1000 * 3];
338
339 {
340 let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000));
341 back.draw_line((500, 0), (500, 1000), &WHITE.filled().stroke_width(5))
342 .unwrap();
343 }
344
345 let nz_count = buffer.into_iter().filter(|x| *x != 0).count();
346
347 assert_eq!(nz_count, 6 * 1000 * 3);
348 }
349
350 #[cfg(test)]
351 #[test]
352 fn test_bitmap_blit() {
353 let src_bitmap: Vec<u8> = (0..100)
354 .map(|y| (0..300).map(move |x| ((x * y) % 253) as u8))
355 .flatten()
356 .collect();
357
358 use plotters::prelude::*;
359 let mut buffer = vec![0; 1000 * 1000 * 3];
360
361 {
362 let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000));
363 back.blit_bitmap((500, 500), (100, 100), &src_bitmap[..])
364 .unwrap();
365 }
366
367 for y in 0..1000 {
368 for x in 0..1000 {
369 if x >= 500 && x < 600 && y >= 500 && y < 600 {
370 let lx = x - 500;
371 let ly = y - 500;
372 assert_eq!(buffer[y * 3000 + x * 3 + 0] as usize, (ly * lx * 3) % 253);
373 assert_eq!(
374 buffer[y * 3000 + x * 3 + 1] as usize,
375 (ly * (lx * 3 + 1)) % 253
376 );
377 assert_eq!(
378 buffer[y * 3000 + x * 3 + 2] as usize,
379 (ly * (lx * 3 + 2)) % 253
380 );
381 } else {
382 assert_eq!(buffer[y * 3000 + x * 3 + 0], 0);
383 assert_eq!(buffer[y * 3000 + x * 3 + 1], 0);
384 assert_eq!(buffer[y * 3000 + x * 3 + 2], 0);
385 }
386 }
387 }
388 }
389
390 #[cfg(all(not(target_arch = "wasm32"), feature = "image"))]
391 #[cfg(test)]
392 mod test {
393 use crate::BitMapBackend;
394 use image::{ImageBuffer, Rgb};
395 use plotters::prelude::*;
396 use plotters::style::text_anchor::{HPos, Pos, VPos};
397 use std::fs;
398 use std::path::Path;
399
400 static DST_DIR: &str = "target/test/bitmap";
401
402 fn checked_save_file(name: &str, content: &[u8], w: u32, h: u32) {
403 /*
404 Please use the PNG file to manually verify the results.
405 */
406 assert!(content.iter().any(|x| *x != 0));
407 fs::create_dir_all(DST_DIR).unwrap();
408 let file_name = format!("{}.png", name);
409 let file_path = Path::new(DST_DIR).join(file_name);
410 println!("{:?} created", file_path);
411 let img = ImageBuffer::<Rgb<u8>, &[u8]>::from_raw(w, h, content).unwrap();
412 img.save(&file_path).unwrap();
413 }
414
415 fn draw_mesh_with_custom_ticks(tick_size: i32, test_name: &str) {
416 let (width, height) = (500, 500);
417 let mut buffer = vec![0; (width * height * 3) as usize];
418 {
419 let root = BitMapBackend::with_buffer(&mut buffer, (width, height)).into_drawing_area();
420 root.fill(&WHITE).unwrap();
421
422 let mut chart = ChartBuilder::on(&root)
423 .caption("This is a test", ("sans-serif", 20))
424 .set_all_label_area_size(40)
425 .build_cartesian_2d(0..10, 0..10)
426 .unwrap();
427
428 chart
429 .configure_mesh()
430 .set_all_tick_mark_size(tick_size)
431 .draw()
432 .unwrap();
433 }
434 checked_save_file(test_name, &buffer, width, height);
435 }
436
437 #[test]
438 fn test_draw_mesh_no_ticks() {
439 draw_mesh_with_custom_ticks(0, "test_draw_mesh_no_ticks");
440 }
441
442 #[test]
443 fn test_draw_mesh_negative_ticks() {
444 draw_mesh_with_custom_ticks(-10, "test_draw_mesh_negative_ticks");
445 }
446
447 #[test]
448 fn test_text_draw() {
449 let (width, height) = (1500, 800);
450 let mut buffer = vec![0; (width * height * 3) as usize];
451 {
452 let root = BitMapBackend::with_buffer(&mut buffer, (width, height)).into_drawing_area();
453 root.fill(&WHITE).unwrap();
454 let root = root
455 .titled("Image Title", ("sans-serif", 60).into_font())
456 .unwrap();
457
458 let mut chart = ChartBuilder::on(&root)
459 .caption("All anchor point positions", ("sans-serif", 20))
460 .set_all_label_area_size(40)
461 .build_cartesian_2d(0..100, 0..50)
462 .unwrap();
463
464 chart
465 .configure_mesh()
466 .disable_x_mesh()
467 .disable_y_mesh()
468 .x_desc("X Axis")
469 .y_desc("Y Axis")
470 .draw()
471 .unwrap();
472
473 let ((x1, y1), (x2, y2), (x3, y3)) = ((-30, 30), (0, -30), (30, 30));
474
475 for (dy, trans) in [
476 FontTransform::None,
477 FontTransform::Rotate90,
478 FontTransform::Rotate180,
479 FontTransform::Rotate270,
480 ]
481 .iter()
482 .enumerate()
483 {
484 for (dx1, h_pos) in [HPos::Left, HPos::Right, HPos::Center].iter().enumerate() {
485 for (dx2, v_pos) in [VPos::Top, VPos::Center, VPos::Bottom].iter().enumerate() {
486 let x = 150_i32 + (dx1 as i32 * 3 + dx2 as i32) * 150;
487 let y = 120 + dy as i32 * 150;
488 let draw = |x, y, text| {
489 root.draw(&Circle::new((x, y), 3, &BLACK.mix(0.5))).unwrap();
490 let style = TextStyle::from(("sans-serif", 20).into_font())
491 .pos(Pos::new(*h_pos, *v_pos))
492 .transform(trans.clone());
493 root.draw_text(text, &style, (x, y)).unwrap();
494 };
495 draw(x + x1, y + y1, "dood");
496 draw(x + x2, y + y2, "dog");
497 draw(x + x3, y + y3, "goog");
498 }
499 }
500 }
501 }
502 checked_save_file("test_text_draw", &buffer, width, height);
503 }
504
505 #[test]
506 fn test_text_clipping() {
507 let (width, height) = (500_i32, 500_i32);
508 let mut buffer = vec![0; (width * height * 3) as usize];
509 {
510 let root = BitMapBackend::with_buffer(&mut buffer, (width as u32, height as u32))
511 .into_drawing_area();
512 root.fill(&WHITE).unwrap();
513
514 let style = TextStyle::from(("sans-serif", 20).into_font())
515 .pos(Pos::new(HPos::Center, VPos::Center));
516 root.draw_text("TOP LEFT", &style, (0, 0)).unwrap();
517 root.draw_text("TOP CENTER", &style, (width / 2, 0))
518 .unwrap();
519 root.draw_text("TOP RIGHT", &style, (width, 0)).unwrap();
520
521 root.draw_text("MIDDLE LEFT", &style, (0, height / 2))
522 .unwrap();
523 root.draw_text("MIDDLE RIGHT", &style, (width, height / 2))
524 .unwrap();
525
526 root.draw_text("BOTTOM LEFT", &style, (0, height)).unwrap();
527 root.draw_text("BOTTOM CENTER", &style, (width / 2, height))
528 .unwrap();
529 root.draw_text("BOTTOM RIGHT", &style, (width, height))
530 .unwrap();
531 }
532 checked_save_file("test_text_clipping", &buffer, width as u32, height as u32);
533 }
534
535 #[test]
536 fn test_series_labels() {
537 let (width, height) = (500, 500);
538 let mut buffer = vec![0; (width * height * 3) as usize];
539 {
540 let root = BitMapBackend::with_buffer(&mut buffer, (width, height)).into_drawing_area();
541 root.fill(&WHITE).unwrap();
542
543 let mut chart = ChartBuilder::on(&root)
544 .caption("All series label positions", ("sans-serif", 20))
545 .set_all_label_area_size(40)
546 .build_cartesian_2d(0..50, 0..50)
547 .unwrap();
548
549 chart
550 .configure_mesh()
551 .disable_x_mesh()
552 .disable_y_mesh()
553 .draw()
554 .unwrap();
555
556 chart
557 .draw_series(std::iter::once(Circle::new((5, 15), 5, &RED)))
558 .expect("Drawing error")
559 .label("Series 1")
560 .legend(|(x, y)| Circle::new((x, y), 3, RED.filled()));
561
562 chart
563 .draw_series(std::iter::once(Circle::new((5, 15), 10, &BLUE)))
564 .expect("Drawing error")
565 .label("Series 2")
566 .legend(|(x, y)| Circle::new((x, y), 3, BLUE.filled()));
567
568 for pos in vec![
569 SeriesLabelPosition::UpperLeft,
570 SeriesLabelPosition::MiddleLeft,
571 SeriesLabelPosition::LowerLeft,
572 SeriesLabelPosition::UpperMiddle,
573 SeriesLabelPosition::MiddleMiddle,
574 SeriesLabelPosition::LowerMiddle,
575 SeriesLabelPosition::UpperRight,
576 SeriesLabelPosition::MiddleRight,
577 SeriesLabelPosition::LowerRight,
578 SeriesLabelPosition::Coordinate(70, 70),
579 ]
580 .into_iter()
581 {
582 chart
583 .configure_series_labels()
584 .border_style(&BLACK.mix(0.5))
585 .position(pos)
586 .draw()
587 .expect("Drawing error");
588 }
589 }
590 checked_save_file("test_series_labels", &buffer, width, height);
591 }
592
593 #[test]
594 fn test_draw_pixel_alphas() {
595 let (width, height) = (100_i32, 100_i32);
596 let mut buffer = vec![0; (width * height * 3) as usize];
597 {
598 let root = BitMapBackend::with_buffer(&mut buffer, (width as u32, height as u32))
599 .into_drawing_area();
600 root.fill(&WHITE).unwrap();
601 for i in -20..20 {
602 let alpha = i as f64 * 0.1;
603 root.draw_pixel((50 + i, 50 + i), &BLACK.mix(alpha))
604 .unwrap();
605 }
606 }
607 checked_save_file(
608 "test_draw_pixel_alphas",
609 &buffer,
610 width as u32,
611 height as u32,
612 );
613 }
614 }