1.10
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 <cairo.h>
15#include <egt/detail/meta.h>
16#include <egt/geometry.h>
17#include <egt/painter.h>
18#include <egt/serialize.h>
19#include <map>
20#include <string>
21
22namespace egt
23{
24inline namespace v1
25{
26
27class SvgImage;
28
38class EGT_API Image
39{
40public:
41
52 // cppcheck-suppress noExplicitConstructor
53 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
54 Image(const std::string& uri = {}, float scale = 1.0);
55
63 Image(const std::string& uri, float hscale, float vscale);
64
71 Image(const unsigned char* data, size_t len);
72
81 void load(const std::string& uri, float hscale = 1.0, float vscale = 1.0, bool approx = false);
82
91 // cppcheck-suppress noExplicitConstructor
92 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
94
101 // cppcheck-suppress noExplicitConstructor
102 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
103 Image(cairo_surface_t* surface);
104
118 void scale(float hscale, float vscale, bool approximate = false);
119
125 inline void scale(float scale, bool approximate = false)
126 {
127 this->scale(scale, scale, approximate);
128 }
129
135 void resize(const Size& size)
136 {
137 if (this->size() != size)
138 {
139 float hs = static_cast<float>(size.width()) / static_cast<float>(m_orig_size.width());
140 float vs = static_cast<float>(size.height()) / static_cast<float>(m_orig_size.height());
141 scale(hs, vs);
142 }
143 }
144
148 EGT_NODISCARD float hscale() const { return m_hscale; }
149
153 EGT_NODISCARD float vscale() const { return m_vscale; }
154
158 EGT_NODISCARD Size size() const
159 {
160 if (empty())
161 return {};
162
163 return {cairo_image_surface_get_width(surface().get()),
164 cairo_image_surface_get_height(surface().get())};
165 }
166
167 EGT_NODISCARD DefaultDim width() const
168 {
169 return size().width();
170 }
171
172 EGT_NODISCARD DefaultDim height() const
173 {
174 return size().height();
175 }
176
180 EGT_NODISCARD bool empty() const
181 {
182 return !surface();
183 }
184
188 EGT_NODISCARD shared_cairo_surface_t surface() const
189 {
190 if (m_surface_local.get())
191 return m_surface_local;
192 return m_surface;
193 }
194
196 EGT_NODISCARD cairo_pattern_t* pattern() const
197 {
198 if (!m_pattern)
199 m_pattern.reset(cairo_pattern_create_for_surface(surface().get()),
200 cairo_pattern_destroy);
201 assert(m_pattern.get());
202 return m_pattern.get();
203 }
204
209 EGT_NODISCARD Size size_orig() const { return m_orig_size; }
210
215 EGT_NODISCARD Rect align(const Rect& bounding, const AlignFlags& align);
216
223 void keep_image_ratio(bool enable)
224 {
225 m_keep_image_ratio = enable;
226 }
227
231 bool keep_image_ratio() const
232 {
233 return m_keep_image_ratio;
234 }
235
244 void copy();
245
246 EGT_NODISCARD std::string uri() const
247 {
248 return m_uri;
249 }
250
251 void uri(const std::string& uri) { load(uri, m_hscale, m_vscale); }
252
253 void reset_uri() { uri({}); }
254
255 Image crop(const RectF& rect);
256
257 Image crop(const Rect& rect)
258 {
259 return crop(RectF(rect.x(), rect.y(), rect.width(), rect.height()));
260 }
261
265 void serialize(const std::string& name, Serializer& serializer) const;
266
270 void deserialize(const std::string& name, const std::string& value,
271 const Serializer::Attributes& attrs);
272
273protected:
275
277 std::string m_uri;
278
280 float m_hscale{1.0};
281
283 float m_vscale{1.0};
284
287
290
293
295 bool m_keep_image_ratio{true};
296
299
300private:
308 Image(shared_cairo_surface_t surface, const std::string& uri)
309 : Image(surface)
310 {
311 m_uri = uri;
312 }
313
314 friend class SvgImage;
315};
316
317static_assert(detail::rule_of_5<Image>(), "must fulfill rule of 5");
318
319}
320}
321
322#endif
Alignment flags.
Definition widgetflags.h:379
Raster image resource used for drawing or displaying.
Definition image.h:39
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:148
void handle_surface_changed()
EGT_NODISCARD DefaultDim width() const
Definition image.h:167
EGT_NODISCARD Size size_orig() const
Get the original size of the image before any Image::resize() or Image::scale() calls.
Definition image.h:209
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:223
bool keep_image_ratio() const
Get the keep image ratio state.
Definition image.h:231
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:153
Image crop(const Rect &rect)
Definition image.h:257
Image(shared_cairo_surface_t surface)
EGT_NODISCARD std::string uri() const
Definition image.h:246
std::string m_uri
If a URI was used, the URI.
Definition image.h:277
EGT_NODISCARD cairo_pattern_t * pattern() const
Get internal pattern representation.
Definition image.h:196
shared_cairo_surface_t m_surface_local
Local surface pointer.
Definition image.h:289
void uri(const std::string &uri)
Definition image.h:251
void resize(const Size &size)
Resize the image to the specified absolute size.
Definition image.h:135
EGT_NODISCARD Size size() const
Get the absolute size of the image.
Definition image.h:158
void scale(float scale, bool approximate=false)
Scale the image.
Definition image.h:125
void copy()
This function must be called any time the surface is going to be modified.
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:172
EGT_NODISCARD shared_cairo_surface_t surface() const
Get a reference to the internal image surface.
Definition image.h:188
EGT_NODISCARD bool empty() const
Returns true if no internal surface is set.
Definition image.h:180
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.
Image crop(const RectF &rect)
shared_cairo_pattern_t m_pattern
Internal pattern representation.
Definition image.h:298
Size m_orig_size
Original image size.
Definition image.h:292
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.
shared_cairo_surface_t m_surface
Shared surface pointer.
Definition image.h:286
Image(cairo_surface_t *surface)
void reset_uri()
Definition image.h:253
EGT_NODISCARD constexpr Dim width() const noexcept
Get the width value.
Definition geometry.h:913
EGT_NODISCARD constexpr Dim x() const noexcept
Get the x value.
Definition geometry.h:903
EGT_NODISCARD constexpr Dim height() const noexcept
Get the height value.
Definition geometry.h:915
EGT_NODISCARD constexpr Dim y() const noexcept
Get the y value.
Definition geometry.h:905
Abstract base serializer class.
Definition serialize.h:34
std::list< std::pair< std::string, std::string > > Attributes
Attributes array type.
Definition serialize.h:45
An SVG image.
Definition svgimage.h:57
std::shared_ptr< cairo_surface_t > shared_cairo_surface_t
Shared pointer for a cairo surface.
Definition types.h:29
std::list< T > get(const BitFields< T > &fields)
std::shared_ptr< cairo_pattern_t > shared_cairo_pattern_t
Shared pointer for a cairo pattern.
Definition types.h:41
int DefaultDim
Define the default dimension type used for geometry.
Definition geometry.h:34
EGT framework namespace.
Definition animation.h:24