| 58 | */ |
| 59 | template<typename ScalarT> |
| 60 | struct Mat |
| 61 | { |
| 62 | private: |
| 63 | bool owns_buffer; ///< Whether the matrix owns the data buffer (and should dispose it when deleted). |
| 64 | unsigned char *buffer_; ///< Data buffer of the matrix (row major). |
| 65 | unsigned char *buffer_end_; ///< End of the buffer (just after the last element). |
| 66 | int width_; ///< Number of elements in the matrix. |
| 67 | int height_; ///< Number of rows in the matrix. |
| 68 | int x_step; ///< Number of bytes in one element. |
| 69 | int y_step; ///< Number of bytes in one row. |
| 70 | |
| 71 | /** |
| 72 | * Allocate a buffer. |
| 73 | * @param width Width of the matrix. |
| 74 | * @param height Height of the matrix. |
| 75 | * @param external_buffer If not \c null, use the provided buffer, else make a new one. |
| 76 | */ |
| 77 | void allocate(int width, int height, unsigned char *external_buffer = 0) |
| 78 | { |
| 79 | this->width_ = width; |
| 80 | this->height_ = height; |
| 81 | x_step = sizeof(ScalarT); |
| 82 | y_step = width * x_step; |
| 83 | |
| 84 | owns_buffer = external_buffer == 0; |
| 85 | |
| 86 | if(owns_buffer) |
| 87 | { |
| 88 | buffer_ = new unsigned char[y_step * height]; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | buffer_ = external_buffer; |
| 93 | } |
| 94 | buffer_end_ = buffer_ + (y_step * height); |
| 95 | } |
| 96 | |
| 97 | void deallocate() |
| 98 | { |
| 99 | if(owns_buffer && buffer_ != 0) |
| 100 | { |
| 101 | delete[] buffer_; |
| 102 | owns_buffer = false; |
| 103 | buffer_ = 0; |
| 104 | buffer_end_ = 0; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public: |
| 109 | /** Default constructor. */ |
| 110 | Mat():buffer_(0), buffer_end_(0) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Constructor with locally allocated buffer. |
| 116 | * @param height Height of the image. |
| 117 | * @param width Width of the image. |