summaryrefslogtreecommitdiff
path: root/histogrammer/src/main.rs
blob: 90257875b15af44cc3bead294f9541df87c3aaad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use image;

use plotters::prelude::*;

fn main() {
    let im = image::open("/misc/camera_recorder/ngc6888_o3_20201002_set2_1.png").expect("can load image");
    let im = match im {
        image::DynamicImage::ImageLuma16(ref p) => Some(p),
        _ => None,
    };
    if let Some(img) = im {
        let (w, h) = img.dimensions();
        println!("got 16-bit greyscale image: {}x{}", w, h);
        let total = w * h;
//        let mut histogram = [0u32; 65536];
//        for px in img.enumerate_pixels() {
//            histogram[px.2[0] as usize] += 1;
//        }

        let hi = img.enumerate_pixels().map(|x| ((x.2[0]) as f64, if x.2[0] == 0 { 0f64 } else { 1f64 / (total as f64) }));

 //       println!("hist: {:?}", &histogram[..]);

        fn draw_hist<I: IntoIterator<Item=(f64, f64)>>(hist_data: I, start: usize, end: usize) -> Vec<u8> {
            assert!(start <= end);

            let mut data = vec![0; 640 * 480 * 3];

            {
                let mut root = BitMapBackend::with_buffer(&mut data, (640, 480)).into_drawing_area();
                root.fill(&WHITE);

                let mut chart = ChartBuilder::on(&root)
                    .margin(5)
                    .caption("Pixel histogram", ("sans-serif", 30))
                    .set_label_area_size(LabelAreaPosition::Left, 60)
                    .set_label_area_size(LabelAreaPosition::Bottom, 60)
                    .set_label_area_size(LabelAreaPosition::Right, 60)
                    .build_cartesian_2d((start as f64)..(end as f64), 0f64..2.0).unwrap()
                    .set_secondary_coord(
                        ((start as f64)..(end as f64)).step(1.0).use_round().into_segmented(),
                        0f64..0.05f64,
                    );

                chart
                    .configure_mesh()
                    .disable_x_mesh()
                    .disable_y_mesh()
                    .y_desc("Percentage")
                    .draw().unwrap();

                chart.configure_secondary_axes().y_desc("Count").draw().unwrap();

                let actual = Histogram::vertical(chart.borrow_secondary())
                    .style(GREEN.filled())
                    .margin(3)
                    .data(hist_data);

                chart
                    .draw_secondary_series(actual).unwrap()
                    .label("Observed")
                    .legend(|(x, y)| Rectangle::new([(x, y - 5), (x + 10, y + 5)], GREEN.filled()));

                chart.configure_series_labels().draw().unwrap();
            }

            data
        }

//        let data = draw_hist(&histogram[..], 0, 6000);
        let data = draw_hist(hi, 0, 6000);

        use show_image::make_window;
        let window = make_window("image").expect("can make the window");
        let image_info = show_image::ImageInfo::rgb8(640, 480);
        window.set_image(&(data, image_info), "histogram");
        loop {
        std::thread::sleep(std::time::Duration::from_millis(10000));
        }
    } else {
        panic!("image was not 16-bit greyscale");
    }
}