roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
nibble_rect.h
Go to the documentation of this file.
1#pragma once
2
3#include <inttypes.h>
4
8#include "roo_io/memory/fill.h"
9
10namespace roo_display {
11namespace internal {
12
13// Rectangular nibble (half-byte) area.
15 public:
17 : buffer_(buffer), width_bytes_(width_bytes), height_(height) {}
18
19 roo::byte* buffer() { return buffer_; }
20 const roo::byte* buffer() const { return buffer_; }
21 int16_t width_bytes() const { return width_bytes_; }
22 int16_t width() const { return width_bytes_ * 2; }
23 int16_t height() const { return height_; }
24
25 uint8_t get(int16_t x, int16_t y) const {
26 return (uint8_t)(buffer_[y * width_bytes_ + x / 2] >> ((1 - (x % 2)) * 4)) &
27 0xF;
28 }
29
30 void set(int16_t x, int16_t y, uint8_t value) {
31 roo::byte& b = buffer_[x / 2 + y * width_bytes()];
32 if (x % 2 == 0) {
33 b &= roo::byte{0x0F};
34 b |= (roo::byte)(value << 4);
35 } else {
36 b &= roo::byte{0xF0};
37 b |= (roo::byte)value;
38 }
39 }
40
41 // Fills the specified rectangle of the mask with the specified bit value.
42 void fillRect(const Box& rect, uint8_t value) {
43 if (rect.xMin() == 0 && rect.xMax() == width() - 1) {
44 roo_io::NibbleFill(buffer_, rect.yMin() * width(),
45 rect.height() * width(), (roo::byte)value);
46 } else {
47 int16_t w = rect.width();
48 uint32_t offset = rect.xMin() + rect.yMin() * width();
49 for (int16_t i = rect.height(); i > 0; --i) {
50 roo_io::NibbleFill(buffer_, offset, w, (roo::byte)value);
51 offset += width();
52 }
53 }
54 }
55
56 private:
57 roo::byte* buffer_;
58 int16_t width_bytes_;
59 int16_t height_;
60};
61
62// Iterates over bits in a specified rectangular sub-window of a nibble rect.
64 public:
66 int16_t x1, int16_t y1)
67 : ptr_(rect->buffer() + (y0 * rect->width_bytes() + x0 / 2)),
68 nibble_idx_(x0 % 2),
69 x_(0),
70 width_(x0 == 0 && x1 == rect->width() - 1
71 ? rect->width() * rect->height()
72 : x1 - x0 + 1),
73 width_skip_(rect->width() - (x1 - x0 + 1)) {}
74
76 if (x_ >= width_) {
77 ptr_ += (width_skip_ + nibble_idx_) / 2;
78 nibble_idx_ = (width_skip_ + nibble_idx_) % 2;
79 x_ = 0;
80 }
81 uint8_t result = (uint8_t)(*ptr_ >> ((1 - nibble_idx_) * 4)) & 0xF;
82 ++x_;
83 ++nibble_idx_;
84 ptr_ += (nibble_idx_ >= 2);
85 nibble_idx_ &= 1;
86 return result;
87 }
88
89 private:
90 const roo::byte* ptr_;
91 uint8_t nibble_idx_;
92 int16_t x_;
93 const int16_t width_;
94 const int16_t width_skip_;
95};
96
97// Adapter to use writer as filler, for a single rectangle.
101 void fillRect(int16_t x0, int16_t y0, int16_t x1, int16_t y1) const {
102 writer.writeRect(x0, y0, x1, y1, color);
103 }
104};
105
106} // namespace internal
107} // namespace roo_display
Axis-aligned integer rectangle.
Definition box.h:12
void writeRect(int16_t x0, int16_t y0, int16_t x1, int16_t y1, Color color)
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
NibbleRectWindowIterator(const NibbleRect *rect, int16_t x0, int16_t y0, int16_t x1, int16_t y1)
Definition nibble_rect.h:65
void fillRect(const Box &rect, uint8_t value)
Definition nibble_rect.h:42
const roo::byte * buffer() const
Definition nibble_rect.h:20
NibbleRect(roo::byte *buffer, int16_t width_bytes, int16_t height)
Definition nibble_rect.h:16
uint8_t get(int16_t x, int16_t y) const
Definition nibble_rect.h:25
void set(int16_t x, int16_t y, uint8_t value)
Definition nibble_rect.h:30
Defines 140 opaque HTML named colors.
void fillRect(int16_t x0, int16_t y0, int16_t x1, int16_t y1) const