plotpx 0.1.7

Pixel-focused plotting engine that renders magnitude grids, heatmaps, and spectra to RGBA buffers
Documentation
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use crate::chart::{compose_with_annotations, ChartAnnotations, ChartImage};
use crate::color_scheme::{default_color_scheme_data, get_color_count};

pub struct Magnitude {
    pub width: u32,
    pub height: u32,
    pub max_magnitude: f32,
    pub min_magnitude: f32,
    pub buffer: Vec<f32>,
}

impl Magnitude {
    pub fn new(width: u32, height: u32) -> Self {
        Self {
            width,
            height,
            max_magnitude: f32::NEG_INFINITY,
            min_magnitude: f32::INFINITY,
            buffer: vec![0.0; (width * height) as usize],
        }
    }

    pub fn add_point(&mut self, x: u32, y: u32, magnitude_value: f32) {
        if x >= self.width || y >= self.height {
            return;
        }

        let idx = (y * self.width + x) as usize;
        self.buffer[idx] = magnitude_value;

        if magnitude_value > self.max_magnitude {
            self.max_magnitude = magnitude_value;
        }

        if magnitude_value < self.min_magnitude {
            self.min_magnitude = magnitude_value;
        }
    }

    pub fn shift_buffer_to_non_negative(&mut self) {
        if self.buffer.is_empty() {
            return;
        }

        if self.min_magnitude < 0.0 {
            let shift = -self.min_magnitude;
            for val in &mut self.buffer {
                *val += shift;
            }
            self.max_magnitude += shift;
            self.min_magnitude = 0.0;
        }
    }

    pub fn render(&mut self) -> Vec<u8> {
        let colors = default_color_scheme_data();
        self.render_with_colors(&colors)
    }

    pub fn render_with_colors(&mut self, colors: &[u8]) -> Vec<u8> {
        self.shift_buffer_to_non_negative();
        let saturation = if self.max_magnitude > 0.0 {
            self.max_magnitude
        } else {
            1.0
        };
        self.render_saturated(colors, saturation)
    }

    pub fn render_saturated(&self, colors: &[u8], saturation: f32) -> Vec<u8> {
        assert!(saturation > 0.0);

        let total_pixels = (self.width * self.height) as usize;
        let mut colorbuf = vec![0u8; total_pixels * 4];

        let ncolors = get_color_count(colors);
        if ncolors == 0 {
            return colorbuf;
        }

        for idx in 0..total_pixels {
            let val = self.buffer[idx];
            let normalized = (val / saturation).clamp(0.0, 1.0);

            let color_idx = ((ncolors - 1) as f32 * normalized + 0.5) as usize;
            let color_idx = color_idx.min(ncolors - 1);

            let src_start = color_idx * 4;
            let dst_start = idx * 4;
            colorbuf[dst_start..dst_start + 4].copy_from_slice(&colors[src_start..src_start + 4]);
        }

        colorbuf
    }

    pub fn render_with_annotations(
        &mut self,
        colors: &[u8],
        annotations: &ChartAnnotations,
    ) -> ChartImage {
        let data = self.render_with_colors(colors);
        compose_with_annotations(&data, self.width, self.height, annotations)
    }

    pub fn render_default_with_annotations(
        &mut self,
        annotations: &ChartAnnotations,
    ) -> ChartImage {
        let colors = default_color_scheme_data();
        self.render_with_annotations(&colors, annotations)
    }

    pub fn render_saturated_with_annotations(
        &self,
        colors: &[u8],
        saturation: f32,
        annotations: &ChartAnnotations,
    ) -> ChartImage {
        let data = self.render_saturated(colors, saturation);
        compose_with_annotations(&data, self.width, self.height, annotations)
    }

    pub fn reset(&mut self) {
        self.buffer.fill(0.0);
        self.max_magnitude = f32::NEG_INFINITY;
        self.min_magnitude = f32::INFINITY;
    }
}

pub struct MagnitudeMapped {
    pub input_width: u32,
    pub input_height: u32,
    pub image_width: u32,
    pub image_height: u32,
    pub max_magnitude: f32,
    pub min_magnitude: f32,
    pub buffer: Vec<f32>,
}

impl MagnitudeMapped {
    pub fn new(input_width: u32, input_height: u32, image_width: u32, image_height: u32) -> Self {
        Self {
            input_width,
            input_height,
            image_width,
            image_height,
            max_magnitude: f32::NEG_INFINITY,
            min_magnitude: f32::INFINITY,
            buffer: vec![0.0; (image_width * image_height) as usize],
        }
    }

    pub fn map_coordinates(&self, input_x: u32, input_y: u32) -> Option<(u32, u32)> {
        if self.input_width == 0 || self.input_height == 0 {
            return None;
        }

        let scale_x = self.image_width as f32 / self.input_width as f32;
        let scale_y = self.image_height as f32 / self.input_height as f32;

        let mut image_x = (input_x as f32 * scale_x) as u32;
        let mut image_y = (input_y as f32 * scale_y) as u32;

        if image_x >= self.image_width {
            image_x = self.image_width - 1;
        }
        if image_y >= self.image_height {
            image_y = self.image_height - 1;
        }

        Some((image_x, image_y))
    }

    pub fn add_point(&mut self, input_x: u32, input_y: u32, magnitude_value: f32) {
        if self.input_width == 0 || self.input_height == 0 {
            return;
        }

        let scale_x = self.image_width as f32 / self.input_width as f32;
        let scale_y = self.image_height as f32 / self.input_height as f32;

        let start_x = (input_x as f32 * scale_x) as u32;
        let start_y = (input_y as f32 * scale_y) as u32;

        let end_x = ((input_x + 1) as f32 * scale_x) as u32;
        let end_y = ((input_y + 1) as f32 * scale_y) as u32;

        let end_x = end_x.min(self.image_width);
        let end_y = end_y.min(self.image_height);

        for img_y in start_y..end_y {
            for img_x in start_x..end_x {
                let idx = (img_y * self.image_width + img_x) as usize;
                self.buffer[idx] += magnitude_value;

                if self.buffer[idx] > self.max_magnitude {
                    self.max_magnitude = self.buffer[idx];
                }

                if self.buffer[idx] < self.min_magnitude {
                    self.min_magnitude = self.buffer[idx];
                }
            }
        }
    }

    pub fn shift_buffer_to_non_negative(&mut self) {
        if self.buffer.is_empty() {
            return;
        }

        if self.min_magnitude < 0.0 {
            let shift = -self.min_magnitude;
            for val in &mut self.buffer {
                *val += shift;
            }
            self.max_magnitude += shift;
            self.min_magnitude = 0.0;
        }
    }

    pub fn render(&mut self) -> Vec<u8> {
        let colors = default_color_scheme_data();
        self.render_with_colors(&colors)
    }

    pub fn render_with_colors(&mut self, colors: &[u8]) -> Vec<u8> {
        self.shift_buffer_to_non_negative();
        let saturation = if self.max_magnitude > 0.0 {
            self.max_magnitude
        } else {
            1.0
        };
        self.render_saturated(colors, saturation)
    }

    pub fn render_saturated(&self, colors: &[u8], saturation: f32) -> Vec<u8> {
        assert!(saturation > 0.0);

        let total_pixels = (self.image_width * self.image_height) as usize;
        let mut colorbuf = vec![0u8; total_pixels * 4];

        let ncolors = get_color_count(colors);
        if ncolors == 0 {
            return colorbuf;
        }

        for idx in 0..total_pixels {
            let val = self.buffer[idx];
            let normalized = (val / saturation).clamp(0.0, 1.0);

            let color_idx = ((ncolors - 1) as f32 * normalized + 0.5) as usize;
            let color_idx = color_idx.min(ncolors - 1);

            let src_start = color_idx * 4;
            let dst_start = idx * 4;
            colorbuf[dst_start..dst_start + 4].copy_from_slice(&colors[src_start..src_start + 4]);
        }

        colorbuf
    }

    pub fn reset(&mut self) {
        self.buffer.fill(0.0);
        self.max_magnitude = f32::NEG_INFINITY;
        self.min_magnitude = f32::INFINITY;
    }

    pub fn render_with_annotations(
        &mut self,
        colors: &[u8],
        annotations: &ChartAnnotations,
    ) -> ChartImage {
        let data = self.render_with_colors(colors);
        compose_with_annotations(&data, self.image_width, self.image_height, annotations)
    }

    pub fn render_default_with_annotations(
        &mut self,
        annotations: &ChartAnnotations,
    ) -> ChartImage {
        let colors = default_color_scheme_data();
        self.render_with_annotations(&colors, annotations)
    }

    pub fn render_saturated_with_annotations(
        &self,
        colors: &[u8],
        saturation: f32,
        annotations: &ChartAnnotations,
    ) -> ChartImage {
        let data = self.render_saturated(colors, saturation);
        compose_with_annotations(&data, self.image_width, self.image_height, annotations)
    }
}

pub struct MagnitudeMappedGrid {
    grid_size: usize,
    plot_width: u32,
    plot_height: u32,
    global_max_magnitude: f32,
    global_min_magnitude: f32,
    plots: Vec<MagnitudeMapped>,
}

impl MagnitudeMappedGrid {
    pub fn new(
        grid_size: usize,
        input_width: u32,
        input_height: u32,
        plot_width: u32,
        plot_height: u32,
    ) -> Self {
        let plot_count = grid_size * grid_size;
        let plots = (0..plot_count)
            .map(|_| MagnitudeMapped::new(input_width, input_height, plot_width, plot_height))
            .collect();

        Self {
            grid_size,
            plot_width,
            plot_height,
            global_max_magnitude: f32::NEG_INFINITY,
            global_min_magnitude: f32::INFINITY,
            plots,
        }
    }

    pub fn get_plot(&mut self, row: usize, col: usize) -> &mut MagnitudeMapped {
        assert!(row < self.grid_size && col < self.grid_size);
        &mut self.plots[row * self.grid_size + col]
    }

    pub fn set_plot(&mut self, row: usize, col: usize, plot: MagnitudeMapped) {
        assert!(row < self.grid_size && col < self.grid_size);
        self.plots[row * self.grid_size + col] = plot;
        self.update_global_extrema();
    }

    pub fn add_point(
        &mut self,
        row: usize,
        col: usize,
        input_x: u32,
        input_y: u32,
        magnitude_value: f32,
    ) {
        assert!(row < self.grid_size && col < self.grid_size);

        let plot = &mut self.plots[row * self.grid_size + col];
        plot.add_point(input_x, input_y, magnitude_value);

        self.global_max_magnitude = self.global_max_magnitude.max(plot.max_magnitude);
        self.global_min_magnitude = self.global_min_magnitude.min(plot.min_magnitude);
    }

    pub fn update_global_extrema(&mut self) {
        self.global_max_magnitude = f32::NEG_INFINITY;
        self.global_min_magnitude = f32::INFINITY;

        for plot in &self.plots {
            self.global_max_magnitude = self.global_max_magnitude.max(plot.max_magnitude);
            self.global_min_magnitude = self.global_min_magnitude.min(plot.min_magnitude);
        }
    }

    pub fn shift_all_buffers_to_non_negative(&mut self) {
        if self.global_min_magnitude < 0.0 {
            let shift = -self.global_min_magnitude;

            for plot in &mut self.plots {
                for val in &mut plot.buffer {
                    *val += shift;
                }
                plot.max_magnitude += shift;
                plot.min_magnitude = 0.0;
            }

            self.global_max_magnitude += shift;
            self.global_min_magnitude = 0.0;
        }
    }

    pub fn render(&mut self, colors: &[u8]) -> Vec<u8> {
        self.update_global_extrema();
        self.shift_all_buffers_to_non_negative();

        let total_width = self.plot_width * self.grid_size as u32;
        let total_height = self.plot_height * self.grid_size as u32;

        let mut combined_buffer = vec![0u8; (total_width * total_height * 4) as usize];

        let saturation = if self.global_max_magnitude > 0.0 {
            self.global_max_magnitude
        } else {
            1.0
        };

        for row in 0..self.grid_size {
            for col in 0..self.grid_size {
                let plot = &self.plots[row * self.grid_size + col];
                let plot_buffer = plot.render_saturated(colors, saturation);

                let start_x = col as u32 * self.plot_width;
                let start_y = row as u32 * self.plot_height;

                for y in 0..self.plot_height {
                    for x in 0..self.plot_width {
                        let src_idx = ((y * self.plot_width + x) * 4) as usize;
                        let dst_idx = (((start_y + y) * total_width + (start_x + x)) * 4) as usize;

                        combined_buffer[dst_idx..dst_idx + 4]
                            .copy_from_slice(&plot_buffer[src_idx..src_idx + 4]);
                    }
                }
            }
        }

        combined_buffer
    }

    pub fn reset(&mut self) {
        for plot in &mut self.plots {
            plot.reset();
        }
        self.global_max_magnitude = f32::NEG_INFINITY;
        self.global_min_magnitude = f32::INFINITY;
    }
}