View.h
1/* Copyright (C) 2015 Marc Boris Duerner
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
7
8 As a special exception, you may use this file as part of a free
9 software library without restriction. Specifically, if other files
10 instantiate templates or use macros or inline functions from this
11 file, or you compile this file and link it with other files to
12 produce an executable, this file does not by itself cause the
13 resulting executable to be covered by the GNU General Public
14 License. This exception does not however invalidate any other
15 reasons why the executable file might be covered by the GNU Library
16 General Public License.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 MA 02110-1301 USA
27*/
28
29#ifndef PT_GFX_VIEW_H
30#define PT_GFX_VIEW_H
31
32#include <Pt/Gfx/Api.h>
33#include <Pt/Gfx/ViewBase.h>
34#include <Pt/Gfx/ImageTraits.h>
35#include <Pt/TypeTraits.h>
36#include <Pt/Types.h>
37
38#include <algorithm>
39
40namespace Pt {
41
42namespace Gfx {
43
63template <typename FormatT, typename TraitsT>
64class BasicView : public ViewBase
65{
66 public:
67 typedef FormatT Format;
68 typedef TraitsT Traits;
69
70 public:
71 explicit BasicView( const Format& format = FormatT::get() );
72
73 BasicView(Pt::uint8_t* data, Pt::ssize_t width, Pt::ssize_t height,
74 Pt::ssize_t padding, const Format& format = FormatT::get());
75
76 template <typename T>
77 explicit BasicView(T& source);
78
79 template <typename T>
80 BasicView(T& source, Int x, Int y, Int w, Int h);
81
82 BasicView& operator=(const BasicView&) = default;
83
84 ~BasicView()
85 { }
86
87 Pt::uint8_t* data()
88 { return _data; }
89
90 const Pt::uint8_t* data() const
91 { return _data; }
92
93 const Format& format() const
94 { return *_format; }
95
96 Pt::ssize_t pixelStride() const
97 { return TraitsT::pixelStride(*_format); }
98
99 Pt::ssize_t padding() const
100 { return stride() - width() * pixelStride(); }
101
102 protected:
103 void setData(Pt::uint8_t* data)
104 { _data = data; }
105
106 void setFormat(const Format& format)
107 { _format = &format; }
108
109 private:
110 Pt::uint8_t* _data;
111 const Format* _format;
112};
113
114
123template <typename FormatT, typename TraitsT>
124class BasicConstView : public ViewBase
125{
126 public:
127 typedef FormatT Format;
128 typedef TraitsT Traits;
129
130 public:
131 explicit BasicConstView( const Format& format = FormatT::get() );
132
133 BasicConstView(const Pt::uint8_t* data, Pt::ssize_t width, Pt::ssize_t height,
134 Pt::ssize_t padding, const Format& format = FormatT::get());
135
136 template <typename T>
137 explicit BasicConstView(const T& source);
138
139 template <typename T>
140 BasicConstView(const T& source, Int x, Int y, Int w, Int h);
141
142 BasicConstView& operator=(const BasicConstView&) = default;
143
144 ~BasicConstView()
145 { }
146
147 const Pt::uint8_t* data() const
148 { return _data; }
149
150 const Format& format() const
151 { return *_format; }
152
153 Pt::ssize_t pixelStride() const
154 { return TraitsT::pixelStride(*_format); }
155
156 Pt::ssize_t padding() const
157 { return stride() - width() * pixelStride(); }
158
159 protected:
160 void setData(const Pt::uint8_t* data)
161 { _data = data; }
162
163 void setFormat(const Format& format)
164 { _format = &format; }
165
166 private:
167 const Pt::uint8_t* _data;
168 const Format* _format;
169};
170
171
172template <typename T>
173BasicView<typename T::Format,
174 typename T::Traits> view(T& source)
175{
177}
178
179
180template <typename T>
181BasicConstView<typename T::Format,
182 typename T::Traits> view(const T& source)
183{
184 return BasicConstView<typename T::Format, typename T::Traits>(source);
185}
186
187
188template <typename T>
189BasicView<typename T::Format,
190 typename T::Traits> view(T& source, Int x, Int y, Int w, Int h)
191{
192 return BasicView<typename T::Format, typename T::Traits>(source, x, y, w, h);
193}
194
195
196template <typename T>
197BasicConstView<typename T::Format,
198 typename T::Traits> view(const T& source, Int x, Int y, Int w, Int h)
199{
201}
202
203
204template <typename FormatT, typename TraitsT = ImageTraits<FormatT> >
205BasicView<FormatT,
206 TraitsT> view(Pt::uint8_t* data, Pt::ssize_t width,
207 Pt::ssize_t height, Pt::ssize_t padding = 0)
208{
209 return BasicView<FormatT, TraitsT>(data, width, height, padding, FormatT::get());
210}
211
212
213template <typename FormatT, typename TraitsT = ImageTraits<FormatT> >
214BasicConstView<FormatT,
215 TraitsT> view(const Pt::uint8_t* data, Pt::ssize_t width,
216 Pt::ssize_t height, Pt::ssize_t padding = 0)
217{
218 return BasicConstView<FormatT, TraitsT>(data, width, height, padding, FormatT::get());
219}
220
221
222template <typename T>
223BasicView<typename T::Format,
224 ImageTraitsF> viewF(T& source)
225{
227}
228
229
230template <typename T>
231BasicConstView<typename T::Format,
232 ImageTraitsF> viewF(const T& source)
233{
235}
236
237
238template <typename T>
239BasicView<typename T::Format,
240 ImageTraitsF> viewF(T& source, Int x, Int y, Int w, Int h)
241{
242 return BasicView<typename T::Format, ImageTraitsF>(source, x, y, w, h);
243}
244
245
246template <typename T>
247BasicConstView<typename T::Format,
248 ImageTraitsF> viewF(const T& source, Int x, Int y, Int w, Int h)
249{
250 return BasicConstView<typename T::Format, ImageTraitsF>(source, x, y, w, h);
251}
252
253
254inline ImageViewF viewF(Pt::uint8_t* data, Pt::ssize_t width,
255 Pt::ssize_t height, Pt::ssize_t padding,
256 const ImageFormat& format)
257{
258 return ImageViewF(data, width, height, padding, format);
259}
260
261
262inline ConstImageViewF viewF(const Pt::uint8_t* data, Pt::ssize_t width,
263 Pt::ssize_t height, Pt::ssize_t padding,
264 const ImageFormat& format)
265{
266 return ConstImageViewF(data, width, height, padding, format);
267}
268
269
275template <typename From, typename P>
276void copyViewTo(const From& from, P& to)
277{
278 ConstSpan<typename From::Format,
279 typename From::Traits> fromSpan( from, 0, 0, from.width() );
280
281 for(Pt::ssize_t y = 0; y < from.height(); ++y)
282 {
283 copySpanTo(fromSpan, to);
284 fromSpan.advanceLines(1);
285 to.advanceLines(1);
286 }
287}
288
293template <typename From, typename To>
294void copyView(const From& from, To& to)
295{
296 std::size_t w = std::min(from.width(), to.width());
297 Pt::ssize_t h = std::min(from.height(), to.height());
298
299 ConstSpan<typename From::Format,
300 typename From::Traits> fromSpan( from, 0, 0, w );
301
302 Span<typename To::Format,
303 typename To::Traits> toSpan( to, 0, 0, w );
304
305 for(Pt::ssize_t y = 0; y < h; ++y)
306 {
307 copySpanTo(fromSpan, toSpan.front());
308 fromSpan.advanceLines(1);
309 toSpan.advanceLines(1);
310 }
311}
312
313} // namespace
314
315} // namespace
316
317#endif
318
319#include <Pt/Gfx/View.hpp>
Read-only image region.
Definition View.h:125
Non-owning image region.
Definition View.h:65
Runtime pixel format.
Definition ImageFormat.h:72
uint_type uint8_t
Unsigned 8-bit integer type.
Definition Api-Types.h:24
Graphics and imaging services.
Definition Api-Argb32Image.h:12
void copySpanTo(const SpanT &from, P &to)
Copies the pixels of a span to a pixel position.
Definition Span.h:403
void copyViewTo(const From &from, P &to)
Copies the pixels of a view to a pixel position.
Definition View.h:276
void copyView(const From &from, To &to)
Copies the pixels of a view to another one.
Definition View.h:294
Core module.
Definition Allocator.h:33