๐Ÿ“ฆ sharkdp / binocle

๐Ÿ“„ binocle.rs ยท 137 lines
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137use anyhow::Result;

use crate::buffer::Buffer;
use crate::datatype::Datatype;
use crate::options::{BackingOption, CliOptions};
use crate::settings::{GuiDatatype, PixelStyle, Settings, WIDTH};
use crate::style::{
    Abgr, Bgr, Category, ColorGradient, Colorful, DatatypeStyle, Entropy, Grayscale, Rgb, Rgba,
    Style,
};
use crate::view::View;

pub struct Binocle {
    pub settings: Settings,
    buffer: Buffer,
}

impl Binocle {
    pub fn new(options: CliOptions) -> Result<Self> {
        let buffer = match options.backing {
            BackingOption::File => Buffer::from_file(options.filename),
            BackingOption::Mmap => Buffer::from_mmap(options.filename),
        }?;

        let buffer_length = buffer.len();
        let settings = Settings {
            buffer_length: buffer_length as isize,
            ..Default::default()
        };

        Ok(Self { buffer, settings })
    }

    pub fn update_hex_view(&mut self) {
        if !self.settings.hex_view_visible {
            return;
        }

        let mut hex_view = String::new();
        let mut hex_ascii = String::new();

        let view = View::new(
            self.buffer.data(),
            self.settings.offset + self.settings.offset_fine,
            1,
        );

        let width = (self.settings.width * self.settings.stride).min(36);
        let height = 24;

        for i in 0..(width * height) {
            if i > 0 && i % width == 0 {
                hex_view.push('\n');
                hex_ascii.push('\n');
            } else if i > 0 && (i % width) % 8 == 0 {
                hex_view.push(' ');
            }

            if let Some(byte) = view.byte_at(i) {
                hex_view.push_str(&format!("{:02x} ", byte));

                if byte.is_ascii_graphic() || (byte as char) == ' ' {
                    hex_ascii.push(byte as char);
                } else {
                    hex_ascii.push('ยท');
                }
            } else {
                hex_view.push_str("  ");
                hex_ascii.push(' ');
            }
        }
        self.settings.hex_view = hex_view;
        self.settings.hex_ascii = hex_ascii;
    }

    pub fn draw(&self, frame: &mut [u8]) {
        let settings = &self.settings;

        let view = View::new(
            self.buffer.data(),
            settings.offset + settings.offset_fine,
            settings.stride,
        );

        let mut style: Box<dyn Style> = match settings.pixel_style {
            PixelStyle::Colorful => Box::new(Colorful {}),
            PixelStyle::Grayscale => Box::new(Grayscale {}),
            PixelStyle::Category => Box::new(Category {}),
            PixelStyle::GradientMagma => Box::new(ColorGradient::new(colorgrad::magma())),
            PixelStyle::GradientPlasma => Box::new(ColorGradient::new(colorgrad::plasma())),
            PixelStyle::GradientViridis => Box::new(ColorGradient::new(colorgrad::viridis())),
            PixelStyle::GradientRainbow => Box::new(ColorGradient::new(colorgrad::rainbow())),
            PixelStyle::GradientTurbo => Box::new(ColorGradient::new(colorgrad::turbo())),
            PixelStyle::GradientCubehelix => {
                Box::new(ColorGradient::new(colorgrad::cubehelix_default()))
            }
            PixelStyle::Rgba => Box::new(Rgba {}),
            PixelStyle::Abgr => Box::new(Abgr {}),
            PixelStyle::Rgb => Box::new(Rgb {}),
            PixelStyle::Bgr => Box::new(Bgr {}),
            PixelStyle::Entropy => Box::new(Entropy::with_window_size(32)),
            PixelStyle::Datatype => Box::new(DatatypeStyle::new(
                match (
                    &settings.datatype_settings.datatype,
                    settings.datatype_settings.signedness,
                ) {
                    (GuiDatatype::Integer8, signedness) => Datatype::Integer8(signedness),
                    (GuiDatatype::Integer16, signedness) => Datatype::Integer16(signedness),
                    (GuiDatatype::Integer32, signedness) => Datatype::Integer32(signedness),
                    (GuiDatatype::Integer64, signedness) => Datatype::Integer64(signedness),
                    (GuiDatatype::Float32, _) => Datatype::Float32,
                    (GuiDatatype::Float64, _) => Datatype::Float64,
                },
                settings.datatype_settings.endianness,
                settings.value_range,
            )),
        };
        style.init(&view);

        for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
            let zoom_factor = settings.zoom_factor();
            let x = ((i as isize) % WIDTH as isize) / zoom_factor;
            let y = ((i as isize) / WIDTH as isize) / zoom_factor;

            let color = if x >= settings.width {
                [0, 0, 0, 0]
            } else {
                let view_index = y * settings.width + x;

                style.color_at_index(&view, view_index)
            };

            pixel.copy_from_slice(&color);
        }
    }
}