roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
transformation.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace roo_display {
7
8/// Geometric transformation: swap, scale, translate, rotate, clip.
10 public:
11 /// Construct a transformation with swap, scale, and translation.
14
15 /// Construct a transformation with optional clip box.
19
20 /// Identity transformation.
22
23 /// Swap x/y axes.
24 Transformation swapXY() const;
25 /// Flip across the Y axis.
26 Transformation flipX() const;
27 /// Flip across the X axis.
28 Transformation flipY() const;
29 /// Scale along both axes.
31 /// Translate by offsets.
33 /// Intersect with a clip box.
35 /// Rotate 90 degrees clockwise.
37 /// Rotate 90 degrees counter-clockwise.
39 /// Rotate 180 degrees.
41 /// Rotate clockwise by `turns` (multiples of 90 degrees).
43 /// Rotate counter-clockwise by `turns` (multiples of 90 degrees).
45
46 /// Whether x/y axes are swapped.
47 bool xy_swap() const { return xy_swap_; }
48 /// X scale factor.
49 int16_t x_scale() const { return x_scale_; }
50 /// Y scale factor.
51 int16_t y_scale() const { return y_scale_; }
52 /// X translation.
53 int16_t x_offset() const { return x_offset_; }
54 /// Y translation.
55 int16_t y_offset() const { return y_offset_; }
56 /// Whether clipping is enabled.
57 bool clipped() const { return clipped_; }
58 /// Effective clip box.
59 Box clip_box() const { return clipped_ ? clip_box_ : Box::MaximumBox(); }
60
61 /// Returns true if scale differs from 1 on any axis.
62 bool is_rescaled() const { return x_scale_ != 1 || y_scale_ != 1; }
63
64 /// Returns true if scale differs from 1 or -1 on any axis.
65 bool is_abs_rescaled() const {
66 return (x_scale_ != 1 && x_scale_ != -1) ||
67 (y_scale_ != 1 && y_scale_ != -1);
68 }
69
70 /// Returns true if translation is non-zero.
71 bool is_translated() const { return x_offset_ != 0 || y_offset_ != 0; }
72
73 /// Apply transformation to a rectangle without swapping input coordinates.
74 ///
75 /// If the transformation swaps axes, the input rectangle is assumed already
76 /// swapped.
77 void transformRectNoSwap(int16_t &x0, int16_t &y0, int16_t &x1,
78 int16_t &y1) const;
79
80 /// Apply the transformation to a box.
81 Box transformBox(Box in) const;
82
83 /// Smallest rectangle in destination coordinates enclosing `rect`.
84 Box smallestEnclosingRect(const Box &rect) const;
85
86 /// Smallest bounding rect covering the clip box in destination coordinates.
88
89 private:
90 int16_t x_scale_;
91 int16_t y_scale_;
92 int16_t x_offset_;
93 int16_t y_offset_;
94 bool xy_swap_;
95 bool clipped_;
96 Box clip_box_;
97};
98
99/// Display output wrapper that applies a `Transformation`.
101 public:
102 /// Construct a transformed output.
104 Transformation transformation)
105 : delegate_(delegate),
106 transformation_(std::move(transformation)),
107 clip_box_(transformation_.clip_box()),
108 addr_window_(),
109 x_cursor_(0),
110 y_cursor_(0) {}
111
112 void setAddress(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
113 BlendingMode mode) override;
114
115 void write(Color *color, uint32_t pixel_count) override;
116
117 void fill(Color color, uint32_t pixel_count) override;
118
119 void writePixels(BlendingMode mode, Color *color, int16_t *x, int16_t *y,
120 uint16_t pixel_count) override;
121
122 void fillPixels(BlendingMode mode, Color color, int16_t *x, int16_t *y,
123 uint16_t pixel_count) override;
124
125 void writeRects(BlendingMode mode, Color *color, int16_t *x0, int16_t *y0,
126 int16_t *x1, int16_t *y1, uint16_t pixel_count) override;
127
128 void fillRects(BlendingMode mode, Color color, int16_t *x0, int16_t *y0,
129 int16_t *x1, int16_t *y1, uint16_t count) override;
130
131 /// Return the effective clip box.
132 const Box &clip_box() const { return clip_box_; }
133
134 const ColorFormat &getColorFormat() const override {
135 return delegate_.getColorFormat();
136 }
137
138 private:
139 DisplayOutput &delegate_;
140 Transformation transformation_;
141 Box clip_box_;
142 Box addr_window_;
143 BlendingMode blending_mode_;
144 int16_t x_cursor_;
145 int16_t y_cursor_;
146};
147
148/// Drawable wrapper that applies a `Transformation`.
150 public:
151 /// Construct a transformed drawable.
153 : transformation_(std::move(transformation)), delegate_(delegate) {}
154
155 Box extents() const override {
156 return transformation_.transformBox(delegate_->extents());
157 }
158
159 Box anchorExtents() const override {
160 return transformation_.transformBox(delegate_->anchorExtents());
161 }
162
163 private:
164 void drawTo(const Surface &s) const override {
166 transformation_.translate(s.dx(), s.dy()).clip(s.clip_box());
168 Surface news(&new_output, adjusted.smallestBoundingRect(),
169 s.is_write_once(), s.bgcolor(), s.fill_mode(),
170 s.blending_mode());
171 news.drawObject(*delegate_);
172 }
173
174 private:
175 Transformation transformation_;
176 const Drawable *delegate_;
177};
178
179} // namespace roo_display
Axis-aligned integer rectangle.
Definition box.h:12
static Box MaximumBox()
Return a large sentinel box used for unbounded extents.
Definition box.h:59
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
The abstraction for drawing to a display.
Definition device.h:15
virtual const ColorFormat & getColorFormat() const =0
Return the native color format used by this device for direct drawing.
Interface for objects that can be drawn to an output device.
Definition drawable.h:229
virtual Box anchorExtents() const
Return the bounds used for alignment.
Definition drawable.h:246
virtual Box extents() const =0
Return the bounding box encompassing all pixels that need to be drawn.
Low-level handle used to draw to an underlying device.
Definition drawable.h:60
Geometric transformation: swap, scale, translate, rotate, clip.
Box smallestBoundingRect() const
Smallest bounding rect covering the clip box in destination coordinates.
Transformation translate(int16_t x_offset, int16_t y_offset) const
Translate by offsets.
Transformation scale(int16_t x_scale, int16_t y_scale) const
Scale along both axes.
int16_t y_offset() const
Y translation.
int16_t x_offset() const
X translation.
Transformation rotateUpsideDown() const
Rotate 180 degrees.
Transformation rotateCounterClockwise(int turns) const
Rotate counter-clockwise by turns (multiples of 90 degrees).
void transformRectNoSwap(int16_t &x0, int16_t &y0, int16_t &x1, int16_t &y1) const
Apply transformation to a rectangle without swapping input coordinates.
Transformation flipY() const
Flip across the X axis.
Box transformBox(Box in) const
Apply the transformation to a box.
bool clipped() const
Whether clipping is enabled.
bool is_abs_rescaled() const
Returns true if scale differs from 1 or -1 on any axis.
int16_t x_scale() const
X scale factor.
bool xy_swap() const
Whether x/y axes are swapped.
Transformation rotateRight() const
Rotate 90 degrees clockwise.
Transformation()
Identity transformation.
Box clip_box() const
Effective clip box.
Transformation swapXY() const
Swap x/y axes.
Box smallestEnclosingRect(const Box &rect) const
Smallest rectangle in destination coordinates enclosing rect.
Transformation rotateLeft() const
Rotate 90 degrees counter-clockwise.
Transformation flipX() const
Flip across the Y axis.
bool is_translated() const
Returns true if translation is non-zero.
int16_t y_scale() const
Y scale factor.
Transformation rotateClockwise(int turns) const
Rotate clockwise by turns (multiples of 90 degrees).
bool is_rescaled() const
Returns true if scale differs from 1 on any axis.
Transformation clip(Box clip_box) const
Intersect with a clip box.
Display output wrapper that applies a Transformation.
void fillRects(BlendingMode mode, Color color, int16_t *x0, int16_t *y0, int16_t *x1, int16_t *y1, uint16_t count) override
Draw the specified rectangles using the same color. Invalidates the address window.
void setAddress(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, BlendingMode mode) override
Set a rectangular window filled by subsequent calls to write().
void writeRects(BlendingMode mode, Color *color, int16_t *x0, int16_t *y0, int16_t *x1, int16_t *y1, uint16_t pixel_count) override
Draw the specified rectangles (per-rectangle colors). Invalidates the address window.
const Box & clip_box() const
Return the effective clip box.
void fill(Color color, uint32_t pixel_count) override
Write pixel_count copies of the same color into the current address window.
const ColorFormat & getColorFormat() const override
Return the native color format used by this device for direct drawing.
void fillPixels(BlendingMode mode, Color color, int16_t *x, int16_t *y, uint16_t pixel_count) override
Draw the specified pixels using the same color. Invalidates the address window.
TransformedDisplayOutput(DisplayOutput &delegate, Transformation transformation)
Construct a transformed output.
void writePixels(BlendingMode mode, Color *color, int16_t *x, int16_t *y, uint16_t pixel_count) override
Draw the specified pixels (per-pixel colors). Invalidates the address window.
void write(Color *color, uint32_t pixel_count) override
Write pixels into the current address window.
Drawable wrapper that applies a Transformation.
TransformedDrawable(Transformation transformation, const Drawable *delegate)
Construct a transformed drawable.
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Box anchorExtents() const override
Return the bounds used for alignment.
Defines 140 opaque HTML named colors.
BlendingMode
Porter-Duff style blending modes.
Definition blending.h:17