1.11
image.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_IMAGE_H
7#define EGT_IMAGE_H
8
14#include <egt/detail/meta.h>
15#include <egt/geometry.h>
16#include <egt/painter.h>
17#include <egt/serialize.h>
18#include <egt/surface.h>
19#include <map>
20#include <string>
21
22namespace egt
23{
24inline namespace v1
25{
26
27class SvgImage;
28
37class EGT_API Image
38{
39public:
40
51 // cppcheck-suppress noExplicitConstructor
52 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
53 Image(const std::string& uri = {}, float scale = 1.0);
54
62 Image(const std::string& uri, float hscale, float vscale);
63
70 Image(const unsigned char* data, size_t len);
71
80 void load(const std::string& uri, float hscale = 1.0, float vscale = 1.0, bool approx = false);
81
90 // cppcheck-suppress noExplicitConstructor
91 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
92 Image(std::shared_ptr<Surface> surface);
93
94 Image(Surface&& surface);
95
109 void scale(float hscale, float vscale, bool approximate = false);
110
116 inline void scale(float scale, bool approximate = false)
117 {
118 this->scale(scale, scale, approximate);
119 }
120
126 void resize(const Size& size)
127 {
128 if (!m_orig_size.empty() && this->size() != size)
129 {
130 float hs = static_cast<float>(size.width()) / static_cast<float>(m_orig_size.width());
131 float vs = static_cast<float>(size.height()) / static_cast<float>(m_orig_size.height());
132 scale(hs, vs);
133 }
134 }
135
139 EGT_NODISCARD float hscale() const { return m_hscale; }
140
144 EGT_NODISCARD float vscale() const { return m_vscale; }
145
149 EGT_NODISCARD Size size() const;
150
151 EGT_NODISCARD DefaultDim width() const
152 {
153 return size().width();
154 }
155
156 EGT_NODISCARD DefaultDim height() const
157 {
158 return size().height();
159 }
160
164 EGT_NODISCARD bool empty() const;
165
169 EGT_NODISCARD const std::shared_ptr<Surface>& surface() const
170 {
171 return m_surface;
172 }
173
178 EGT_NODISCARD Size size_orig() const { return m_orig_size; }
179
184 EGT_NODISCARD Rect align(const Rect& bounding, const AlignFlags& align);
185
192 void keep_image_ratio(bool enable)
193 {
194 m_keep_image_ratio = enable;
195 }
196
200 bool keep_image_ratio() const
201 {
202 return m_keep_image_ratio;
203 }
204
205 EGT_NODISCARD std::string uri() const
206 {
207 return m_uri;
208 }
209
210 void uri(const std::string& uri) { load(uri, m_hscale, m_vscale); }
211
212 void reset_uri() { uri({}); }
213
214 Image crop(const Rect& rect) const;
215
219 void serialize(const std::string& name, Serializer& serializer) const;
220
224 void deserialize(const std::string& name, const std::string& value,
225 const Serializer::Attributes& attrs);
226
227protected:
229
231 std::string m_uri;
232
234 float m_hscale{1.0};
235
237 float m_vscale{1.0};
238
240 std::shared_ptr<Surface> m_surface;
241
244
246 bool m_keep_image_ratio{true};
247
248private:
256 Image(Surface&& surface, const std::string& uri)
257 : Image(std::move(surface))
258 {
259 m_uri = uri;
260 }
261
262 friend class SvgImage;
263};
264
265static_assert(detail::rule_of_5<Image>(), "must fulfill rule of 5");
266
267}
268}
269
270#endif
Alignment flags.
Definition widgetflags.h:380
Raster image resource used for drawing or displaying.
Definition image.h:38
Image(const std::string &uri={}, float scale=1.0)
Construct a raster image from a URI with an optional scale.
EGT_NODISCARD float hscale() const
Get the horizontal scale value.
Definition image.h:139
void handle_surface_changed()
EGT_NODISCARD DefaultDim width() const
Definition image.h:151
EGT_NODISCARD Size size_orig() const
Get the original size of the image before any Image::resize() or Image::scale() calls.
Definition image.h:178
void deserialize(const std::string &name, const std::string &value, const Serializer::Attributes &attrs)
Deserialized property.
void keep_image_ratio(bool enable)
Enable/disable ratio preservation while scaling the image.
Definition image.h:192
bool keep_image_ratio() const
Get the keep image ratio state.
Definition image.h:200
void serialize(const std::string &name, Serializer &serializer) const
Serialize to the specified serializer.
EGT_NODISCARD float vscale() const
Get the vertical scale value.
Definition image.h:144
EGT_NODISCARD const std::shared_ptr< Surface > & surface() const
Get a reference to the internal image surface.
Definition image.h:169
std::shared_ptr< Surface > m_surface
Shared surface pointer.
Definition image.h:240
Image(std::shared_ptr< Surface > surface)
EGT_NODISCARD std::string uri() const
Definition image.h:205
std::string m_uri
If a URI was used, the URI.
Definition image.h:231
void uri(const std::string &uri)
Definition image.h:210
void resize(const Size &size)
Resize the image to the specified absolute size.
Definition image.h:126
EGT_NODISCARD Size size() const
Get the absolute size of the image.
void scale(float scale, bool approximate=false)
Scale the image.
Definition image.h:116
void scale(float hscale, float vscale, bool approximate=false)
Scale the image.
Image(const std::string &uri, float hscale, float vscale)
Construct a raster image from a URI with an optional scale.
EGT_NODISCARD DefaultDim height() const
Definition image.h:156
EGT_NODISCARD bool empty() const
Returns true if no internal surface is set.
void load(const std::string &uri, float hscale=1.0, float vscale=1.0, bool approx=false)
Load a new source image with an optional scale.
Size m_orig_size
Original image size.
Definition image.h:243
Image crop(const Rect &rect) const
EGT_NODISCARD Rect align(const Rect &bounding, const AlignFlags &align)
Get the position and size of the image once aligned inside the bounding area.
Image(const unsigned char *data, size_t len)
Construct a raster image from memory.
void reset_uri()
Definition image.h:212
Image(Surface &&surface)
Abstract base serializer class.
Definition serialize.h:34
std::list< std::pair< std::string, std::string > > Attributes
Attributes array type.
Definition serialize.h:45
Definition surface.h:34
An SVG image.
Definition svgimage.h:57
int DefaultDim
Define the default dimension type used for geometry.
Definition geometry.h:34
EGT framework namespace.
Definition animation.h:24