Image-inl.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, 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 
17 #ifndef SURGSIM_DATASTRUCTURES_IMAGE_INL_H
18 #define SURGSIM_DATASTRUCTURES_IMAGE_INL_H
19 
21 
22 namespace SurgSim
23 {
24 namespace DataStructures
25 {
26 
27 template<class T>
29  m_width(0), m_height(0), m_channels(0)
30 {
31 }
32 
33 
34 template<class T>
35 Image<T>::Image(size_t width, size_t height, size_t channels) :
36  m_width(width), m_height(height), m_channels(channels), m_data(new T[m_width * m_height * m_channels])
37 {
38 }
39 
40 template<class T>
41 Image<T>::Image(size_t width, size_t height, size_t channels, const T* const data) :
42  m_width(width), m_height(height), m_channels(channels), m_data(new T[m_width * m_height * m_channels])
43 {
44  std::copy(data, data + width * height * channels, m_data.get());
45 }
46 
47 template<class T>
48 Image<T>::Image(const Image<T>& other) :
49  m_width(other.getWidth()), m_height(other.getHeight()), m_channels(other.getNumChannels()),
50  m_data(new T[m_width * m_height * m_channels])
51 {
52  std::copy(other.m_data.get(), other.m_data.get() + m_width * m_height * m_channels, m_data.get());
53 }
54 
55 template<class T>
57 {
58  // Can use the move assignment operator to construct
59  *this = std::move(other);
60 }
61 
62 template<class T>
64 {
65  if (this != &other)
66  {
67  size_t newDataSize = other.getWidth() * other.getHeight() * other.getNumChannels();
68  size_t oldDataSize = getWidth() * getHeight() * getNumChannels();
69  if (newDataSize != oldDataSize)
70  {
71  m_data.reset(new T[newDataSize]);
72  }
73  m_width = other.getWidth();
74  m_height = other.getHeight();
75  m_channels = other.getNumChannels();
76  std::copy(other.m_data.get(), other.m_data.get() + newDataSize, m_data.get());
77  }
78  return *this;
79 }
80 
81 template<class T>
83 {
84  if (this != &other)
85  {
86  m_data = std::move(other.m_data);
87  m_width = other.getWidth();
88  m_height = other.getHeight();
89  m_channels = other.getNumChannels();
90 
91  other.m_width = 0;
92  other.m_height = 0;
93  other.m_channels = 0;
94  }
95  return *this;
96 }
97 
98 template<class T>
100 {
101 }
102 
103 template<class T>
105 {
106  SURGSIM_ASSERT(channel < m_channels) << "channel number is larger than the number of channels";
107  return ChannelType(m_data.get() + channel, m_width, m_height, Eigen::InnerStride<>(m_channels));
108 }
109 
110 template<class T>
111 size_t Image<T>::getWidth() const
112 {
113  return m_width;
114 }
115 
116 template<class T>
117 size_t Image<T>::getHeight() const
118 {
119  return m_height;
120 }
121 
122 template<class T>
123 std::array<size_t, 3> Image<T>::getSize() const
124 {
125  std::array<size_t, 3> size = {m_width, m_height, m_channels};
126  return std::move(size);
127 }
128 
129 template<class T>
131 {
132  return m_channels;
133 }
134 
135 template<class T>
137 {
138  return m_data.get();
139 }
140 
141 template<class T>
142 const T* const Image<T>::getData() const
143 {
144  return m_data.get();
145 }
146 
147 }
148 }
149 
150 #endif //SURGSIM_DATASTRUCTURES_IMAGE_INL_H
Definition: DriveElementFromInputBehavior.cpp:27
std::unique_ptr< T[]> m_data
Definition: Image.h:110
A templated Image class.
Definition: Image.h:34
size_t getHeight() const
Get the Image height.
Definition: Image-inl.h:117
size_t m_width
Definition: Image.h:107
std::array< size_t, 3 > getSize() const
Get the Image size.
Definition: Image-inl.h:123
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic >, 0, Eigen::InnerStride<> > ChannelType
Type of the channel returned by getChannel.
Definition: Image.h:91
Image< T > & operator=(const Image< T > &other)
Assignment Operator.
Definition: Image-inl.h:63
size_t m_height
Definition: Image.h:108
Image()
Default Constructor.
Definition: Image-inl.h:28
size_t m_channels
Definition: Image.h:109
The header that provides the assertion API.
size_t getWidth() const
Get the Image width.
Definition: Image-inl.h:111
virtual ~Image()
Destructor.
Definition: Image-inl.h:99
T *const getData()
Get the pointer to the data.
Definition: Image-inl.h:136
ChannelType getChannel(size_t channel)
Get the data in the channel as an eigen matrix.
Definition: Image-inl.h:104
size_t getNumChannels() const
Get the number of channels in this Image.
Definition: Image-inl.h:130