roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
compactor.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace roo_display {
6
7// Helper class that can turn a streak of pixel writes that happen to be on a
8// horizontal or vertical direction into a single write or fill request to the
9// underlying device, minimizing address window changes.
10//
11// To use the compactor, call it from writePixels and fillPixels, and give it
12// the following functor:
13// void write(int16_t offset, int16_t x, int16_t y,
14// Compactor::WriteDirection direction, int16_t count);
15// };
16//
17// When called, the functor should write or fill the specified count of pixels,
18// starting at x, y, and in the specified direction. The offset argument
19// indicates the absolute pixel index in the input array. It can be used to
20// determine colors for writing / filling. See addr_window_device.h for an
21// example.
22class Compactor {
23 public:
25 template <typename Writer>
27 Writer write = Writer()) {
28 if (pixel_count == 0) return;
29
30 uint16_t i = 0;
31 uint16_t count = 0;
32 uint16_t x = xs[0];
33 uint16_t y = ys[0];
36
37 while (i < pixel_count) {
38 int start = i;
39 ++i;
40 if (i == pixel_count) {
41 write(start, x, y, RIGHT, 1);
42 return;
43 }
44 x_start = x;
45 y_start = y;
46 x = xs[i];
47 y = ys[i];
48 count = 1;
49 if (y == y_start && x == x_start + 1) {
50 // Horizontal streak.
51 do {
52 ++count;
53 ++i;
54 if (i == pixel_count) break;
55 x = xs[i];
56 y = ys[i];
57 } while (y == y_start && x == x_start + count);
58 write(start, x_start, y_start, RIGHT, count);
59 } else if (x == x_start && y == y_start + 1) {
60 // Vertical streak.
61 do {
62 ++count;
63 ++i;
64 if (i == pixel_count) break;
65 x = xs[i];
66 y = ys[i];
67 } while (x == x_start && y == y_start + count);
68 write(start, x_start, y_start, DOWN, count);
69 } else if (y == y_start && x == x_start - 1) {
70 // Inverse horizontal streak.
71 do {
72 ++count;
73 ++i;
74 if (i == pixel_count) break;
75 x = xs[i];
76 y = ys[i];
77 } while (y == y_start && x == x_start - count);
78 write(start, x_start, y_start, LEFT, count);
79 } else if (x == x_start && y == y_start - 1) {
80 // Vertical streak.
81 do {
82 ++count;
83 ++i;
84 if (i == pixel_count) break;
85 x = xs[i];
86 y = ys[i];
87 } while (x == x_start && y == y_start - count);
88 write(start, x_start, y_start, UP, count);
89 } else {
90 write(start, x_start, y_start, RIGHT, 1);
91 }
92 }
93 }
94
96};
97
98} // namespace roo_display
void drawPixels(int16_t *xs, int16_t *ys, uint16_t pixel_count, Writer write=Writer())
Definition compactor.h:26
Defines 140 opaque HTML named colors.