-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix_color.h
211 lines (170 loc) · 5.22 KB
/
matrix_color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef _MATRIX_COLOR_H_
#define _MATRIX_COLOR_H_
#include <cstdio>
#include <memory>
#include <typeinfo>
#include <exception>
#include "error.h"
using namespace std;
namespace Renzoku {
/**
* Matrix with column-major internal storage. The third dimension stores color.
*/
template <class T>
class MatrixColor {
public:
MatrixColor();
MatrixColor(const MatrixColor<T> &m);
/**
* Defautl constructor of type T is called.
* In case of int, float, double: what is the default value of int()?
*/
MatrixColor(int rows, int cols);
MatrixColor(int rows, int cols, T val);
~MatrixColor();
public:
int get_rows() const;
int get_cols() const;
int get_colors() const;
public:
// The array operator [] only accepts a single argument so it cannot be used to accept row and column index.
// To be consistent, we use operator() to access elements of the matrix.
T operator()(int i) const;
T& operator()(int i);
T operator()(int i, int j, int k) const;
T& operator()(int i, int j, int k);
public:
void load(const char *file);
/**
* RGB matrix format:
*
* <width>(space)<height>\n
* binary data with RGB of pixel 1, RGB of pixel 2, in column major order.
*/
void save(const char *file);
public:
/**
* Modify the storage of a matrix by setting a new number of rows.
*/
void resize(int new_rows);
/**
* Resize the matrix to new rows/columns.
*
* The internal memory is reallocated if necessary. Old matrix data becomes invalid.
*/
void resize(int new_rows, int new_cols);
protected:
/**
* Ensure only compatible types are used with the matrix.
*/
static bool check_typeid() throw();
protected:
T *data;
int max_rows, max_cols;
int rows, cols, colors;
};
template <class T>
bool MatrixColor<T>::check_typeid() throw() {
if (typeid(T) == typeid(char) || typeid(T) == typeid(int) || typeid(T) == typeid(long) || typeid(T) == typeid(long long) ||
typeid(T) == typeid(float) || typeid(T) == typeid(double) ||
typeid(T) == typeid(unsigned int) || typeid(T) == typeid(unsigned char) ||
typeid(T) == typeid(unsigned long) || typeid(T) == typeid(unsigned long long)
) return true;
throw TypeException("Matrix type not supported.");
return false;
}
template <class T>
MatrixColor<T>::MatrixColor() : data(NULL), rows(0), cols(0), colors(3), max_rows(0), max_cols(0) {
}
template <class T>
MatrixColor<T>::MatrixColor(int rows, int cols) : rows(rows), cols(cols), colors(3), max_rows(rows), max_cols(cols) {
data = new T[max_rows * max_cols * colors];
if (!data) error_alloc(__FILE__, __LINE__);
for (int i = 0; i < rows * cols * colors; ++i)
data[i] = T();
}
template <class T>
MatrixColor<T>::MatrixColor(int rows, int cols, T val) : rows(rows), cols(cols), colors(3), max_rows(rows), max_cols(cols) {
data = new T[max_rows * max_cols * colors];
if (!data) error_alloc(__FILE__, __LINE__);
for (int i = 0; i < rows * cols * colors; ++i)
data[i] = val;
}
template <class T>
MatrixColor<T>::MatrixColor(const MatrixColor<T> &m) : rows(m.rows), cols(m.cols), colors(m.colors), max_rows(m.max_rows), max_cols(m.max_cols) {
data = new T[max_rows * max_cols * colors];
if (!data) error_alloc(__FILE__, __LINE__);
memcpy(data, m.data, sizeof(T) * rows * cols * colors);
}
template <class T>
MatrixColor<T>::~MatrixColor() {
if (data)
delete [] data;
}
template <class T>
int MatrixColor<T>::get_rows() const { return rows; }
template <class T>
int MatrixColor<T>::get_cols() const { return cols; }
template <class T>
int MatrixColor<T>::get_colors() const { return colors; }
template <class T>
inline T MatrixColor<T>::operator()(int i) const {
return data[i];
}
template <class T>
inline T& MatrixColor<T>::operator()(int i) {
return data[i];
}
template <class T>
inline T MatrixColor<T>::operator()(int i, int j, int k) const {
return data[k * cols * rows + j * rows + i];
}
template <class T>
inline T& MatrixColor<T>::operator()(int i, int j, int k) {
return data[k * cols * rows + j * rows + i];
}
template <class T>
void MatrixColor<T>::save(const char *file) {
check_typeid();
FILE *f = fopen(file, "wb");
if (f == NULL)
throw FileException("Cannot open file.");
fprintf(f, "%d %d\n", rows, cols);
fwrite(data, sizeof(T), rows * cols * colors, f);
fclose(f);
}
template <class T>
void MatrixColor<T>::load(const char *file) {
check_typeid();
FILE *f = fopen(file, "rb");
if (f == NULL)
throw FileException("Cannot open file.");
fscanf(f, "%d %d\n", &rows, &cols);
max_rows = rows;
max_cols = cols;
colors = 3;
data = new T[rows * cols * colors];
fread(data, sizeof(T), rows * cols, f);
fclose(f);
}
template <class T>
void MatrixColor<T>::resize(int new_rows) {
resize(new_rows, cols);
}
template <class T>
void MatrixColor<T>::resize(int new_rows, int new_cols) {
if (new_rows * new_cols <= max_rows * max_cols) {
rows = new_rows;
cols = new_cols;
} else {
// reallocate
rows = new_rows;
cols = new_cols;
max_rows = new_rows;
max_cols = new_cols;
if (data) delete [] data;
data = new T[max_rows * max_cols * colors];
}
}
} // end namespace
#endif