stromx  0.8.0
Matrix.h
1 /*
2  * Copyright 2012 Matthias Fuchs
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 STROMX_RUNTIME_MATRIX_H
18 #define STROMX_RUNTIME_MATRIX_H
19 
20 #include <stdint.h>
21 #include "stromx/runtime/Data.h"
22 
23 namespace stromx
24 {
25  namespace runtime
26  {
28  class STROMX_RUNTIME_API Matrix : public Data
29  {
30  public:
31  enum ValueType
32  {
33  NONE,
34  INT_8,
35  UINT_8,
36  INT_16,
37  UINT_16,
38  INT_32,
39  UINT_32,
40  FLOAT_32,
41  FLOAT_64
42  };
43 
44  virtual ~Matrix() {}
45 
51  virtual uint8_t* buffer() = 0;
52 
54  virtual unsigned int bufferSize() const = 0;
55 
57  virtual unsigned int rows() const = 0;
58 
60  virtual unsigned int cols() const = 0;
61 
63  virtual unsigned int stride() const = 0;
64 
66  virtual ValueType valueType() const = 0;
67 
69  virtual unsigned int valueSize() const;
70 
72  virtual uint8_t* data() = 0;
73 
75  virtual const uint8_t* data() const = 0;
76 
82  virtual void initializeMatrix(const unsigned int rows,
83  const unsigned int cols,
84  const unsigned int stride,
85  uint8_t* data,
86  const ValueType valueType) = 0;
87 
89  static unsigned int valueSize(const ValueType valueType);
90 
97  template <class T>
98  T & at(const unsigned row, const unsigned col)
99  {
100  const Matrix & mat = const_cast<const Matrix &>(*this);
101 
102  return const_cast<T &>(mat.at<T>(row, col));
103  }
104 
112  template <class T>
113  const T & at(const unsigned int row, const unsigned col) const
114  {
115  if(sizeof(T) != valueSize())
116  throw WrongArgument("Size of access type does not match value size.");
117 
118  if(row >= rows())
119  throw WrongArgument("Row index out of bounds.");
120 
121  if(col >= cols())
122  throw WrongArgument("Column index out of bounds.");
123 
124  unsigned int byteOffset = row * stride() + col * valueSize();
125 
126  return *(reinterpret_cast<const T*>(data() + byteOffset));
127  }
128 
133  template <class T>
134  const T value(const unsigned int row, const unsigned int col) const
135  {
136  switch(valueType())
137  {
138  case UINT_8:
139  return static_cast<T>(at<uint8_t>(row, col));
140  case INT_8:
141  return static_cast<T>(at<int8_t>(row, col));
142  case UINT_16:
143  return static_cast<T>(at<uint16_t>(row, col));
144  case INT_16:
145  return static_cast<T>(at<int16_t>(row, col));
146  case UINT_32:
147  return static_cast<T>(at<uint32_t>(row, col));
148  case INT_32:
149  return static_cast<T>(at<int32_t>(row, col));
150  case FLOAT_32:
151  return static_cast<T>(at<float>(row, col));
152  case FLOAT_64:
153  return static_cast<T>(at<double>(row, col));
154  default:
155  throw InternalError("Unknown value type.");
156  };
157  }
158 
159  protected:
160 
162  static const runtime::VariantHandle dataVariantFromValueType(const ValueType valueType);
163  };
164 
169  STROMX_RUNTIME_API bool operator==(const Matrix & rhs, const Matrix & lhs);
170 
172  STROMX_RUNTIME_API bool operator!=(const Matrix & rhs, const Matrix & lhs);
173 
175  template <>
176  class STROMX_RUNTIME_API data_traits<Matrix>
177  {
178  public:
179  static const VariantHandle & variant();
180  };
182  }
183 }
184 
185 #endif // STROMX_RUNTIME_MATRIX_H
Abstract data object.
Definition: Data.h:53
Definition: VariantHandle.h:34
const T value(const unsigned int row, const unsigned int col) const
Definition: Matrix.h:134
const T & at(const unsigned int row, const unsigned col) const
Definition: Matrix.h:113
The stromx class library.
Definition: AdjustRgbChannels.cpp:29
An internal, unexpected error occurred.
Definition: Exception.h:80
Abstract image.
Definition: Matrix.h:28
A wrong argument was passed to a function.
Definition: Exception.h:53
T & at(const unsigned row, const unsigned col)
Definition: Matrix.h:98
bool operator!=(const runtime::DataContainer &lhs, const runtime::DataContainer &rhs)
Definition: DataContainer.cpp:46