stromx  0.8.0
MatrixWrapper.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_MATRIXWRAPPER_H
18 #define STROMX_RUNTIME_MATRIXWRAPPER_H
19 
20 #include "stromx/runtime/Matrix.h"
21 
22 namespace stromx
23 {
24  namespace runtime
25  {
27  class STROMX_RUNTIME_API MatrixWrapper : public Matrix
28  {
29  public:
33  static void save(const std::string& filename, const runtime::Matrix & matrix);
34 
39  MatrixWrapper(const unsigned int bufferSize, uint8_t* const buffer);
40 
45  MatrixWrapper();
46 
47  // Implementation of stromx::runtime::Matrix
48  virtual const VariantHandle & variant() const { return m_variant; }
49  virtual uint8_t* buffer() { return m_buffer; }
50  virtual unsigned int bufferSize() const { return m_bufferSize; }
51  virtual unsigned int rows() const { return m_rows; }
52  virtual unsigned int cols() const { return m_cols; }
53  virtual unsigned int stride() const { return m_stride; }
54  virtual ValueType valueType() const { return m_valueType; }
55  virtual uint8_t* data() { return m_data; }
56  virtual const uint8_t* data() const { return m_data; }
57  virtual void initializeMatrix(const unsigned int rows,
58  const unsigned int cols,
59  const unsigned int stride,
60  uint8_t* data,
61  const ValueType valueType);
62  virtual void serialize(OutputProvider & output) const;
63  virtual void deserialize(InputProvider & input, const Version & version);
64 
66  void resize(const unsigned int rows, const unsigned int cols, const ValueType valueType);
67 
73  void resize(const unsigned int size);
74 
79  void open(const std::string& filename);
80 
84  void save(const std::string& filename) const;
85 
86  protected:
95  void setBuffer(uint8_t* const buffer, const unsigned int bufferSize);
96 
104  virtual void allocate(const unsigned int rows, const unsigned int cols, const runtime::Matrix::ValueType valueType) = 0;
105 
106 
107 
108  private:
109  static const char NUMPY_MAGIC_BYTE = char(0x93);
110 
111  static void doSerialize(std::ostream & out, const runtime::Matrix & matrix);
112  static void doDeserialize(std::istream & in, MatrixWrapper & matrix);
113  static bool isLittleEndian();
114  static char npyTypeSymbol(const runtime::Matrix::ValueType valueType);
115  static Matrix::ValueType valueTypeFromNpyHeader(const char valueType, const int wordSize);
116 
117  void validate(const unsigned int rows,
118  const unsigned int cols,
119  const unsigned int stride,
120  const uint8_t* data,
121  const ValueType valueType) const;
122 
123  unsigned int m_rows;
124  unsigned int m_cols;
125  unsigned int m_stride;
126  unsigned int m_bufferSize;
127  ValueType m_valueType;
128  uint8_t* m_data;
129  uint8_t* m_buffer;
130  VariantHandle m_variant;
131  };
132  }
133 }
134 
135 #endif // STROMX_RUNTIME_MATRIXWRAPPER_H
A version of an operator or data type.
Definition: Version.h:46
Definition: VariantHandle.h:34
Provides functions to serialize data to strings and files.
Definition: OutputProvider.h:27
Provides functions to deserialize data from strings and files.
Definition: InputProvider.h:27
virtual unsigned int cols() const
Definition: MatrixWrapper.h:52
virtual uint8_t * buffer()
Definition: MatrixWrapper.h:49
virtual ValueType valueType() const
Definition: MatrixWrapper.h:54
virtual const uint8_t * data() const
Definition: MatrixWrapper.h:56
Version version()
Returns the version of the stromx runtime library.
Definition: Runtime.cpp:54
The stromx class library.
Definition: AdjustRgbChannels.cpp:29
Abstract image.
Definition: Matrix.h:28
virtual unsigned int stride() const
Definition: MatrixWrapper.h:53
Concrete matrix without memory management.
Definition: MatrixWrapper.h:27
virtual unsigned int bufferSize() const
Definition: MatrixWrapper.h:50
virtual uint8_t * data()
Definition: MatrixWrapper.h:55
virtual const VariantHandle & variant() const
Definition: MatrixWrapper.h:48
virtual unsigned int rows() const
Definition: MatrixWrapper.h:51