1.12
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
52 // cppcheck-suppress noExplicitConstructor
53 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
54 Image(const std::string& uri = {}, float scale = 1.0, bool is_cached = false);
55
64 Image(const std::string& uri, float hscale, float vscale, bool is_cached = false);
65
72 Image(const unsigned char* data, size_t len);
73
83 void load(const std::string& uri, float hscale = 1.0, float vscale = 1.0, bool approx = false, bool is_cached = false);
84
93 // cppcheck-suppress noExplicitConstructor
94 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
95 Image(std::shared_ptr<Surface> surface);
96
97 Image(Surface&& surface);
98
113 void scale(float hscale, float vscale, bool approximate = false, bool is_cached = false);
114
120 inline void scale(float scale, bool approximate = false, bool is_cached = false)
121 {
122 this->scale(scale, scale, approximate, is_cached);
123 }
124
130 void resize(const Size& size, bool is_cached = false)
131 {
132 if (!m_orig_size.empty() && this->size() != size)
133 {
134 float hs = static_cast<float>(size.width()) / static_cast<float>(m_orig_size.width());
135 float vs = static_cast<float>(size.height()) / static_cast<float>(m_orig_size.height());
136 scale(hs, vs, false, is_cached);
137 }
138 }
139
143 EGT_NODISCARD float hscale() const { return m_hscale; }
144
148 EGT_NODISCARD float vscale() const { return m_vscale; }
149
153 EGT_NODISCARD Size size() const;
154
155 EGT_NODISCARD DefaultDim width() const
156 {
157 return size().width();
158 }
159
160 EGT_NODISCARD DefaultDim height() const
161 {
162 return size().height();
163 }
164
168 EGT_NODISCARD bool empty() const;
169
173 EGT_NODISCARD const std::shared_ptr<Surface>& surface() const
174 {
175 return m_surface;
176 }
177
182 EGT_NODISCARD Size size_orig() const { return m_orig_size; }
183
188 EGT_NODISCARD Rect align(const Rect& bounding, const AlignFlags& align);
189
196 void keep_image_ratio(bool enable)
197 {
198 m_keep_image_ratio = enable;
199 }
200
204 bool keep_image_ratio() const
205 {
206 return m_keep_image_ratio;
207 }
208
209 EGT_NODISCARD std::string uri() const
210 {
211 return m_uri;
212 }
213
214 void uri(const std::string& uri, bool is_cached = false) { load(uri, m_hscale, m_vscale, false, is_cached); }
215
216 void reset_uri() { uri({}); }
217
218 Image crop(const Rect& rect) const;
219
223 void serialize(const std::string& name, Serializer& serializer) const;
224
228 void deserialize(const std::string& name, const std::string& value,
229 const Serializer::Attributes& attrs);
230
231protected:
233
235 std::string m_uri;
236
238 float m_hscale{1.0};
239
241 float m_vscale{1.0};
242
244 std::shared_ptr<Surface> m_surface;
245
248
250 bool m_keep_image_ratio{true};
251
252private:
260 Image(Surface&& surface, const std::string& uri)
261 : Image(std::move(surface))
262 {
263 m_uri = uri;
264 }
265
266 friend class SvgImage;
267};
268
269static_assert(detail::rule_of_5<Image>(), "must fulfill rule of 5");
270
271}
272}
273
274#endif
Alignment flags.
Definition widgetflags.h:380
Raster image resource used for drawing or displaying.
Definition image.h:38
EGT_NODISCARD float hscale() const
Get the horizontal scale value.
Definition image.h:143
void handle_surface_changed()
Image(const std::string &uri={}, float scale=1.0, bool is_cached=false)
Construct a raster image from a URI with an optional scale.
void load(const std::string &uri, float hscale=1.0, float vscale=1.0, bool approx=false, bool is_cached=false)
Load a new source image with an optional scale.
EGT_NODISCARD DefaultDim width() const
Definition image.h:155
EGT_NODISCARD Size size_orig() const
Get the original size of the image before any Image::resize() or Image::scale() calls.
Definition image.h:182
void deserialize(const std::string &name, const std::string &value, const Serializer::Attributes &attrs)
Deserialized property.
void resize(const Size &size, bool is_cached=false)
Resize the image to the specified absolute size.
Definition image.h:130
void keep_image_ratio(bool enable)
Enable/disable ratio preservation while scaling the image.
Definition image.h:196
void scale(float hscale, float vscale, bool approximate=false, bool is_cached=false)
Scale the image.
bool keep_image_ratio() const
Get the keep image ratio state.
Definition image.h:204
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:148
EGT_NODISCARD const std::shared_ptr< Surface > & surface() const
Get a reference to the internal image surface.
Definition image.h:173
std::shared_ptr< Surface > m_surface
Shared surface pointer.
Definition image.h:244
Image(std::shared_ptr< Surface > surface)
EGT_NODISCARD std::string uri() const
Definition image.h:209
std::string m_uri
If a URI was used, the URI.
Definition image.h:235
void uri(const std::string &uri, bool is_cached=false)
Definition image.h:214
Image(const std::string &uri, float hscale, float vscale, bool is_cached=false)
Construct a raster image from a URI with an optional scale.
EGT_NODISCARD Size size() const
Get the absolute size of the image.
void scale(float scale, bool approximate=false, bool is_cached=false)
Scale the image.
Definition image.h:120
EGT_NODISCARD DefaultDim height() const
Definition image.h:160
EGT_NODISCARD bool empty() const
Returns true if no internal surface is set.
Size m_orig_size
Original image size.
Definition image.h:247
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:216
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