roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
rasterizable.h
Go to the documentation of this file.
1#pragma once
2
8
9namespace roo_display {
10
11/// Drawable that can provide a color for any point within its extents.
12///
13/// Rasterizables can be used as overlays, backgrounds, and filters. A
14/// `Rasterizable` is also a `Streamable`. The minimum requirement is to
15/// implement `readColors()`. If your class can stream pixels or draw more
16/// efficiently than per-pixel access, override `createStream()` and/or
17/// `drawTo()`. If large areas share the same color, override `readColorRect()`
18/// to improve performance.
19class Rasterizable : public virtual Streamable {
20 public:
21 /// Read colors for the given points.
22 ///
23 /// The caller must ensure all points are within bounds.
24 virtual void readColors(const int16_t* x, const int16_t* y, uint32_t count,
25 Color* result) const = 0;
26
27 /// Read colors for points that may be out of bounds.
28 ///
29 /// Out-of-bounds points are set to `out_of_bounds_color`.
31 const int16_t* x, const int16_t* y, uint32_t count, Color* result,
32 Color out_of_bounds_color = color::Transparent) const;
33
34 /// Read colors for a rectangle.
35 ///
36 /// Returns true if all colors are identical (then only result[0] is valid).
37 /// The caller must ensure the rectangle is within bounds.
38 virtual bool readColorRect(int16_t xMin, int16_t yMin, int16_t xMax,
39 int16_t yMax, Color* result) const;
40
41 /// Default `createStream()` using `readColors()`.
42 std::unique_ptr<PixelStream> createStream() const override;
43
44 /// Default `createStream()` for a clipped box using `readColors()`.
45 std::unique_ptr<PixelStream> createStream(const Box& bounds) const override;
46
47 protected:
48 /// Default `drawTo()` using `readColors()`.
49 void drawTo(const Surface& s) const override;
50};
51
52/// Wrap a function object into a `Rasterizable`.
53///
54/// The getter must implement:
55///
56/// `Color operator()(int16_t x, int16_t y)`
57template <typename Getter>
59 public:
61 : getter_(getter), extents_(extents), transparency_(transparency) {}
62
63 Box extents() const override { return extents_; }
64
65 void readColors(const int16_t* x, const int16_t* y, uint32_t count,
66 Color* result) const override {
67 while (count-- > 0) {
68 *result++ = getter_(*x++, *y++);
69 }
70 }
71
73 return transparency_;
74 }
75
76 private:
77 Getter getter_;
78 Box extents_;
79 TransparencyMode transparency_;
80};
81
82template <typename Getter>
83/// Create a `SimpleRasterizable` from a getter function.
89
90/// "Infinite" rasterizable background by tiling a finite raster.
91template <typename Getter>
93 public:
97
99 TransparencyMode transparency, int16_t x_offset,
100 int16_t y_offset)
101 : extents_(extents),
102 getter_(getter),
103 transparency_(transparency),
104 x_offset_(x_offset),
105 y_offset_(y_offset) {}
106
107 Box extents() const override { return Box::MaximumBox(); }
108
109 void readColors(const int16_t* x, const int16_t* y, uint32_t count,
110 Color* result) const override {
111 for (uint32_t i = 0; i < count; ++i) {
112 int16_t xs = (x[i] - extents_.xMin() - x_offset_) % extents_.width() +
113 extents_.xMin();
114 int16_t ys = (y[i] - extents_.yMin() - y_offset_) % extents_.height() +
115 extents_.yMin();
116 result[i] = getter_.get(xs, ys);
117 }
118 }
119
121 return transparency_;
122 }
123
124 private:
125 Box extents_;
126 Getter getter_;
127 TransparencyMode transparency_;
128 const int16_t x_offset_, y_offset_;
129};
130
131template <typename Getter>
132/// Create a tiled rasterizable from a getter function.
138
139template <typename Raster>
140/// Create a tiled rasterizable from a raster.
143 raster->extents(), *raster, raster->color_mode().transparency());
144}
145
146template <typename Raster>
147/// Create a tiled rasterizable from a raster with offsets.
149 int16_t x_offset,
150 int16_t y_offset) {
152 raster->extents(), *raster, raster->color_mode().transparency(), x_offset,
153 y_offset);
154}
155
156} // namespace roo_display
Axis-aligned integer rectangle.
Definition box.h:12
int16_t width() const
Width in pixels (inclusive coordinates).
Definition box.h:77
int16_t xMin() const
Minimum x (inclusive).
Definition box.h:65
static Box MaximumBox()
Return a large sentinel box used for unbounded extents.
Definition box.h:59
int16_t height() const
Height in pixels (inclusive coordinates).
Definition box.h:80
int16_t yMin() const
Minimum y (inclusive).
Definition box.h:68
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
Non-owning raster view over a pixel buffer.
Definition raster.h:220
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition raster.h:256
ColorMode & color_mode()
Definition raster.h:260
Drawable that can provide a color for any point within its extents.
void readColorsMaybeOutOfBounds(const int16_t *x, const int16_t *y, uint32_t count, Color *result, Color out_of_bounds_color=color::Transparent) const
Read colors for points that may be out of bounds.
void drawTo(const Surface &s) const override
Default drawTo() using readColors().
std::unique_ptr< PixelStream > createStream() const override
Default createStream() using readColors().
virtual void readColors(const int16_t *x, const int16_t *y, uint32_t count, Color *result) const =0
Read colors for the given points.
virtual bool readColorRect(int16_t xMin, int16_t yMin, int16_t xMax, int16_t yMax, Color *result) const
Read colors for a rectangle.
Wrap a function object into a Rasterizable.
SimpleRasterizable(Box extents, Getter getter, TransparencyMode transparency)
void readColors(const int16_t *x, const int16_t *y, uint32_t count, Color *result) const override
Read colors for the given points.
TransparencyMode getTransparencyMode() const override
Return the transparency mode for pixels in this stream.
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
"Infinite" rasterizable background by tiling a finite raster.
TransparencyMode getTransparencyMode() const override
Return the transparency mode for pixels in this stream.
SimpleTiledRasterizable(const Box &extents, Getter getter, TransparencyMode transparency, int16_t x_offset, int16_t y_offset)
SimpleTiledRasterizable(const Box &extents, Getter getter, TransparencyMode transparency)
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
void readColors(const int16_t *x, const int16_t *y, uint32_t count, Color *result) const override
Read colors for the given points.
Drawable that can provide a sequential pixel stream.
Definition streamable.h:426
Low-level handle used to draw to an underlying device.
Definition drawable.h:60
Defines 140 opaque HTML named colors.
SimpleTiledRasterizable< const Raster & > MakeTiledRaster(const Raster *raster)
Create a tiled rasterizable from a raster.
SimpleTiledRasterizable< Getter > MakeTiledRasterizable(Box extents, Getter getter, TransparencyMode transparency=TransparencyMode::kFull)
Create a tiled rasterizable from a getter function.
TransparencyMode
Transparency information for a stream or color mode.
Definition blending.h:103
@ kFull
Colors may include partial transparency (alpha channel).
SimpleRasterizable< Getter > MakeRasterizable(Box extents, Getter getter, TransparencyMode transparency=TransparencyMode::kFull)
Create a SimpleRasterizable from a getter function.