opensurgsim
Image-inl.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_IMAGE_INL_H
17 #define SURGSIM_DATASTRUCTURES_IMAGE_INL_H
18 
19 
20 namespace SurgSim
21 {
22 namespace DataStructures
23 {
24 
25 template<class T>
27 {
28  ImageBase<T>::setSize(0, 0, 0);
29 }
30 
31 template<class T>
32 Image<T>::Image(size_t width, size_t height, size_t channels) :
33  m_data(new T[width * height * channels])
34 {
35  ImageBase<T>::setSize(width, height, channels);
36 }
37 
38 template<class T>
39 Image<T>::Image(size_t width, size_t height, size_t channels, const T* const data) :
40  m_data(new T[width * height * channels])
41 {
42  ImageBase<T>::setSize(width, height, channels);
43  std::copy(data, data + width * height * channels, m_data.get());
44 }
45 
46 template<class T>
47 template<class D>
48 Image<T>::Image(size_t width, size_t height, size_t channels, const D* const data) :
49  m_data(new T[width * height * channels])
50 {
51  ImageBase<T>::setSize(width, height, channels);
52  Eigen::Map<const Eigen::Matrix<D, Eigen::Dynamic, 1>> theirData(data, width * height * channels);
53  Eigen::Map<typename ImageBase<T>::VectorType, Eigen::Unaligned> myData(m_data.get(), width * height * channels);
54  myData = theirData.template cast<T>();
55 }
56 
57 template<class T>
59 {
60  ImageBase<T>::setSize(other.getWidth(), other.getHeight(), other.getNumChannels());
61  size_t size = other.getWidth() * other.getHeight() * other.getNumChannels();
62  m_data = std::unique_ptr<T[]>(new T[size]);
63  std::copy(other.m_data.get(), other.m_data.get() + size, m_data.get());
64 }
65 
66 template<class T>
68 {
69  // Can use the move assignment operator to construct
70  *this = std::move(other);
71 }
72 
73 template<class T>
75 {
76  if (this != &other)
77  {
78  size_t newDataSize = other.getWidth() * other.getHeight() * other.getNumChannels();
80  if (newDataSize != oldDataSize)
81  {
82  m_data.reset(new T[newDataSize]);
83  }
84  ImageBase<T>::setSize(other.getWidth(), other.getHeight(), other.getNumChannels());
85  std::copy(other.m_data.get(), other.m_data.get() + newDataSize, m_data.get());
86  }
87  return *this;
88 }
89 
90 template<class T>
92 {
93  if (this != &other)
94  {
95  m_data = std::move(other.m_data);
96  ImageBase<T>::setSize(other.getWidth(), other.getHeight(), other.getNumChannels());
97 
98  other.setSize(0, 0, 0);
99  }
100  return *this;
101 }
102 
103 template<class T>
105 {
106 }
107 
108 template<class T>
110 {
111  return m_data.get();
112 }
113 
114 template<class T>
115 const T* const Image<T>::getData() const
116 {
117  return m_data.get();
118 }
119 
120 };
121 };
122 
123 #endif //SURGSIM_DATASTRUCTURES_IMAGE_INL_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
A templated Image class.
Definition: Image.h:33
size_t getHeight() const
Get the Image height.
Definition: ImageBase-inl.h:109
Image< T > & operator=(const Image< T > &other)
Assignment Operator.
Definition: Image-inl.h:74
Image()
Default Constructor.
Definition: Image-inl.h:26
T *const getData() override
Get the pointer to the data.
Definition: Image-inl.h:109
void setSize(size_t width, size_t height, size_t channels)
Set the Image size.
Definition: ImageBase-inl.h:128
size_t getWidth() const
Get the Image width.
Definition: ImageBase-inl.h:103
virtual ~Image()
Destructor.
Definition: Image-inl.h:104
size_t getNumChannels() const
Get the number of channels in this Image.
Definition: ImageBase-inl.h:122