MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
image_base.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef MVE_IMAGE_BASE_HEADER
11#define MVE_IMAGE_BASE_HEADER
12
13#include <cstdint>
14#include <memory>
15#include <vector>
16
17#include "util/strings.h"
18#include "mve/defines.h"
19
21
28{
30 /* Unsigned integer types. */
31 IMAGE_TYPE_UINT8, // uint8_t, unsigned char
32 IMAGE_TYPE_UINT16, // uint16_t
33 IMAGE_TYPE_UINT32, // uint32_t, unsigned int
34 IMAGE_TYPE_UINT64, // uint64_t
35 /* Signed integer types. */
36 IMAGE_TYPE_SINT8, // int8_t, char, signed char
38 IMAGE_TYPE_SINT32, // int32_t, int
40 /* Floating point types. */
42 IMAGE_TYPE_DOUBLE // double
43};
44
45/* ---------------------------------------------------------------- */
46
53{
54public:
55 typedef std::shared_ptr<ImageBase> Ptr;
56 typedef std::shared_ptr<ImageBase const> ConstPtr;
57
58public:
60 ImageBase (void) = default;
61 virtual ~ImageBase (void) = default;
62
64 virtual ImageBase::Ptr duplicate_base (void) const;
65
67 int64_t width (void) const;
69 int64_t height (void) const;
71 int64_t channels (void) const;
72
74 bool valid (void) const;
75
80 bool reinterpret (int64_t new_w, int64_t new_h, int64_t new_c);
81
83 virtual std::size_t get_byte_size (void) const;
85 virtual char const* get_byte_pointer (void) const;
87 virtual char* get_byte_pointer (void);
89 virtual ImageType get_type (void) const;
91 virtual char const* get_type_string (void) const;
92
94 static ImageType get_type_for_string (std::string const& type_string);
95
96protected:
97 int64_t w = 0;
98 int64_t h = 0;
99 int64_t c = 0;
100};
101
102/* ---------------------------------------------------------------- */
103
110template <typename T>
112{
113public:
114 typedef T ValueType;
115 typedef std::shared_ptr<TypedImageBase<T> > Ptr;
116 typedef std::shared_ptr<TypedImageBase<T> const> ConstPtr;
117 typedef std::vector<T> ImageData;
118
119public:
121 TypedImageBase (void) = default;
122
124 TypedImageBase (TypedImageBase<T> const& other);
125
126 virtual ~TypedImageBase (void) = default;
127
129 virtual ImageBase::Ptr duplicate_base (void) const;
130
132 void allocate (int64_t width, int64_t height, int64_t chans);
133
141 void resize (int64_t width, int64_t height, int64_t chans);
142
144 virtual void clear (void);
145
147 void fill (T const& value);
148
150 void swap (TypedImageBase<T>& other);
151
153 virtual ImageType get_type (void) const;
155 char const* get_type_string (void) const;
156
158 ImageData const& get_data (void) const;
160 ImageData& get_data (void);
161
163 T const* get_data_pointer (void) const;
165 T* get_data_pointer (void);
166
168 T* begin (void);
170 T const* begin (void) const;
172 T* end (void);
174 T const* end (void) const;
175
177 int64_t get_pixel_amount (void) const;
179 int64_t get_value_amount (void) const;
180
182 std::size_t get_byte_size (void) const;
184 char const* get_byte_pointer (void) const;
186 char* get_byte_pointer (void);
187
188protected:
190};
191
192/* ================================================================ */
193
194inline ImageBase::Ptr
195ImageBase::duplicate_base (void) const
196{
197 return ImageBase::Ptr(new ImageBase(*this));
198}
199
200inline int64_t
201ImageBase::width (void) const
202{
203 return this->w;
204}
205
206inline int64_t
207ImageBase::height (void) const
208{
209 return this->h;
210}
211
212inline int64_t
213ImageBase::channels (void) const
214{
215 return this->c;
216}
217
218inline bool
219ImageBase::valid (void) const
220{
221 return this->w > 0 && this->h > 0 && this->c > 0;
222}
223
224inline bool
225ImageBase::reinterpret (int64_t new_w, int64_t new_h, int64_t new_c)
226{
227 if (new_w * new_h * new_c != this->w * this->h * this->c)
228 return false;
229
230 this->w = new_w;
231 this->h = new_h;
232 this->c = new_c;
233 return true;
234}
235
236inline std::size_t
237ImageBase::get_byte_size (void) const
238{
239 return 0;
240}
241
242inline char const*
243ImageBase::get_byte_pointer (void) const
244{
245 return nullptr;
246}
247
248inline char*
249ImageBase::get_byte_pointer (void)
250{
251 return nullptr;
252}
253
254inline ImageType
255ImageBase::get_type (void) const
256{
257 return IMAGE_TYPE_UNKNOWN;
258}
259
260inline char const*
261ImageBase::get_type_string (void) const
262{
263 return "unknown";
264}
265
266inline ImageType
267ImageBase::get_type_for_string (std::string const& type_string)
268{
269 if (type_string == "sint8")
270 return IMAGE_TYPE_SINT8;
271 else if (type_string == "sint16")
272 return IMAGE_TYPE_SINT16;
273 else if (type_string == "sint32")
274 return IMAGE_TYPE_SINT32;
275 else if (type_string == "sint64")
276 return IMAGE_TYPE_SINT64;
277 else if (type_string == "uint8")
278 return IMAGE_TYPE_UINT8;
279 else if (type_string == "uint16")
280 return IMAGE_TYPE_UINT16;
281 else if (type_string == "uint32")
282 return IMAGE_TYPE_UINT32;
283 else if (type_string == "uint64")
284 return IMAGE_TYPE_UINT64;
285 else if (type_string == "float")
286 return IMAGE_TYPE_FLOAT;
287 else if (type_string == "double")
288 return IMAGE_TYPE_DOUBLE;
289
290 return IMAGE_TYPE_UNKNOWN;
291}
292
293/* ---------------------------------------------------------------- */
294
295template <typename T>
296inline
298 : ImageBase(other), data(other.data)
299{
300}
301
302template <typename T>
303inline ImageBase::Ptr
305{
306 return ImageBase::Ptr(new TypedImageBase<T>(*this));
307}
308
309template <typename T>
310inline ImageType
312{
313 return IMAGE_TYPE_UNKNOWN;
314}
315
316template <>
317inline ImageType
319{
320 return IMAGE_TYPE_SINT8;
321}
322
323template <>
324inline ImageType
326{
327 return IMAGE_TYPE_SINT16;
328}
329
330template <>
331inline ImageType
333{
334 return IMAGE_TYPE_SINT32;
335}
336
337template <>
338inline ImageType
340{
341 return IMAGE_TYPE_SINT64;
342}
343
344template <>
345inline ImageType
347{
348 return IMAGE_TYPE_UINT8;
349}
350
351template <>
352inline ImageType
354{
355 return IMAGE_TYPE_UINT16;
356}
357
358template <>
359inline ImageType
361{
362 return IMAGE_TYPE_UINT32;
363}
364
365template <>
366inline ImageType
368{
369 return IMAGE_TYPE_UINT64;
370}
371
372template <>
373inline ImageType
375{
376 return IMAGE_TYPE_FLOAT;
377}
378
379template <>
380inline ImageType
382{
383 return IMAGE_TYPE_DOUBLE;
384}
385
386template <typename T>
387inline char const*
389{
390 return util::string::for_type<T>();
391}
392
393template <typename T>
394inline void
395TypedImageBase<T>::allocate (int64_t width, int64_t height, int64_t chans)
396{
397 this->clear();
398 this->resize(width, height, chans);
399}
400
401template <typename T>
402inline void
403TypedImageBase<T>::resize (int64_t width, int64_t height, int64_t chans)
404{
405 this->w = width;
406 this->h = height;
407 this->c = chans;
408 this->data.resize(width * height * chans);
409}
410
411template <typename T>
412inline void
414{
415 this->w = 0;
416 this->h = 0;
417 this->c = 0;
418 this->data.clear();
419}
420
421template <typename T>
422inline void
424{
425 std::fill(this->data.begin(), this->data.end(), value);
426}
427
428template <typename T>
429inline void
431{
432 std::swap(this->w, other.w);
433 std::swap(this->h, other.h);
434 std::swap(this->c, other.c);
435 std::swap(this->data, other.data);
436}
437
438template <typename T>
439inline typename TypedImageBase<T>::ImageData&
441{
442 return this->data;
443}
444
445template <typename T>
446inline typename TypedImageBase<T>::ImageData const&
448{
449 return this->data;
450}
451
452template <typename T>
453inline T const*
455{
456 if (this->data.empty())
457 return nullptr;
458 return &this->data[0];
459}
460
461template <typename T>
462inline T*
464{
465 if (this->data.empty())
466 return nullptr;
467 return &this->data[0];
468}
469
470template <typename T>
471inline T*
473{
474 return this->data.empty() ? nullptr : &this->data[0];
475}
476
477template <typename T>
478inline T const*
480{
481 return this->data.empty() ? nullptr : &this->data[0];
482}
483
484template <typename T>
485inline T*
487{
488 return this->data.empty() ? nullptr : this->begin() + this->data.size();
489}
490
491template <typename T>
492inline T const*
494{
495 return this->data.empty() ? nullptr : this->begin() + this->data.size();
496}
497
498template <typename T>
499inline int64_t
501{
502 return this->w * this->h;
503}
504
505template <typename T>
506inline int64_t
508{
509 return this->get_pixel_amount() * this->c;
510}
511
512template <typename T>
513inline std::size_t
515{
516 return this->data.size() * sizeof(T);
517}
518
519template <typename T>
520inline char const*
522{
523 return reinterpret_cast<char const*>(this->get_data_pointer());
524}
525
526template <typename T>
527inline char*
529{
530 return reinterpret_cast<char*>(this->get_data_pointer());
531}
532
534
535/* ---------------------------------------------------------------- */
536
538
540template <class T>
541inline void
546
548
549#endif /* MVE_IMAGE_BASE_HEADER */
Base class for images without type information.
Definition image_base.h:53
virtual ~ImageBase(void)=default
std::shared_ptr< ImageBase const > ConstPtr
Definition image_base.h:56
std::shared_ptr< ImageBase > Ptr
Definition image_base.h:55
ImageBase(void)=default
Initializes members with 0.
Base class for images of arbitrary type.
Definition image_base.h:112
void swap(TypedImageBase< T > &other)
Swaps the contents of the images.
Definition image_base.h:430
ImageData const & get_data(void) const
Returns the data vector for the image.
Definition image_base.h:447
int64_t get_pixel_amount(void) const
Returns the amount of pixels in the image (w * h).
Definition image_base.h:500
int64_t get_value_amount(void) const
Returns the amount of values in the image (w * h * c).
Definition image_base.h:507
std::size_t get_byte_size(void) const
Returns the size of the image in bytes (w * h * c * BPV).
Definition image_base.h:514
void fill(T const &value)
Fills the data with a constant value.
Definition image_base.h:423
void resize(int64_t width, int64_t height, int64_t chans)
Resizes the underlying image data vector.
Definition image_base.h:403
virtual ImageType get_type(void) const
Value type information by template specialization.
Definition image_base.h:311
T * begin(void)
Returns data pointer to beginning.
Definition image_base.h:472
virtual void clear(void)
Clears the image data from memory.
Definition image_base.h:413
std::shared_ptr< TypedImageBase< T > const > ConstPtr
Definition image_base.h:116
T * end(void)
Returns data pointer to end.
Definition image_base.h:486
char const * get_type_string(void) const
Returns a string representation of the image data type.
Definition image_base.h:388
std::shared_ptr< TypedImageBase< T > > Ptr
Definition image_base.h:115
T const * get_data_pointer(void) const
Returns the data pointer.
Definition image_base.h:454
std::vector< T > ImageData
Definition image_base.h:117
virtual ImageBase::Ptr duplicate_base(void) const
Duplicates the image.
Definition image_base.h:304
char const * get_byte_pointer(void) const
Returns the char pointer to the data.
Definition image_base.h:521
void allocate(int64_t width, int64_t height, int64_t chans)
Allocates new image space, clearing previous content.
Definition image_base.h:395
virtual ~TypedImageBase(void)=default
TypedImageBase(void)=default
Default constructor creates an empty image.
#define MVE_NAMESPACE_BEGIN
Definition defines.h:13
#define STD_NAMESPACE_END
Definition defines.h:24
#define MVE_NAMESPACE_END
Definition defines.h:14
#define STD_NAMESPACE_BEGIN
Definition defines.h:23
ImageType
Identifiers for image types.
Definition image_base.h:28
@ IMAGE_TYPE_UINT64
Definition image_base.h:34
@ IMAGE_TYPE_UINT32
Definition image_base.h:33
@ IMAGE_TYPE_DOUBLE
Definition image_base.h:42
@ IMAGE_TYPE_UINT16
Definition image_base.h:32
@ IMAGE_TYPE_UINT8
Definition image_base.h:31
@ IMAGE_TYPE_SINT16
Definition image_base.h:37
@ IMAGE_TYPE_SINT32
Definition image_base.h:38
@ IMAGE_TYPE_UNKNOWN
Definition image_base.h:29
@ IMAGE_TYPE_SINT8
Definition image_base.h:36
@ IMAGE_TYPE_SINT64
Definition image_base.h:39
@ IMAGE_TYPE_FLOAT
Definition image_base.h:41
void swap(mve::Image< T > &a, mve::Image< T > &b)
Specialization of std::swap for efficient image swapping.
Definition image.h:478