-
Notifications
You must be signed in to change notification settings - Fork 192
/
Image.cpp
166 lines (146 loc) · 3.79 KB
/
Image.cpp
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
// R. Crandall
// Implementation of functions in class Image
#include "Image.h"
// Default constructor. Creates empty image
template <typename T>
Image<T>::Image()
{
m_Data = 0;
m_nRows = 0;
m_nCols = 0;
}
// Constructor to create an image of a given size
template <typename T>
Image<T>::Image(unsigned int nr,
unsigned int nc)
{
m_Data = 0;
Allocate(nr, nc);
}
// Free memory allocated for this image
template <typename T>
void Image<T>::Free()
{
// Don't delete if pointer is null
if (0 != m_Data)
{
// Delete row data
for (unsigned int i = 0; i < m_nRows; ++i)
{
if (0 != m_Data[i])
{
delete[] m_Data[i];
m_Data[i] = 0;
}
}
// Delete row pointers
delete[] m_Data;
m_Data = 0;
}
m_nRows = 0;
m_nCols = 0;
}
// Set every image pixel to a certain value
template <typename T>
void Image<T>::Set(T val)
{
for (unsigned int i = 0; i < m_nRows; ++i)
{
for (unsigned int j = 0; j < m_nCols; ++j)
{
m_Data[i][j] = val;
}
}
}
// Reallocate memory for a certain size image
template <typename T>
void Image<T>::Allocate(unsigned int nr, unsigned int nc)
{
// Make sure memory isn't already allocated; if it is, do nothing
if (nr != m_nRows || nc != m_nCols)
{
// Free memory if there is any allocated
if (0 != m_Data)
{
Free();
}
m_nRows = nr;
m_nCols = nc;
// Allocate memory for row pointers
m_Data = new T*[m_nRows];
// Allocate memory for row data
for (unsigned int i = 0; i < m_nRows; ++i)
{
m_Data[i] = new T[m_nCols];
}
}
}
// Return sub-image of current image
template <typename T>
Image<T>* Image<T>::subImage(unsigned int rowStart,
unsigned int colStart,
unsigned int rowEnd,
unsigned int colEnd)
{
// Check bounds
assert(rowStart >= 0 && rowStart < m_nRows
&& rowEnd >= 0 && rowEnd < m_nRows
&& colStart >= 0 && colStart < m_nCols
&& colEnd >= 0 && colEnd < m_nCols);
Image<T>* sub = new Image<T>(rowEnd-rowStart+1,colEnd-colStart+1);
for (unsigned int i = rowStart; i <= rowEnd; ++i)
{
for (unsigned int j = colStart; j <= colEnd; ++j)
{
sub->data()[i-rowStart][j-colStart] = m_Data[i][j];
}
}
return sub;
}
// Copy data from another image of the same type
template <typename T>
void Image<T>::CopyFrom(const Image<T>* imageIn)
{
unsigned int nr = imageIn->nRows();
unsigned int nc = imageIn->nCols();
// Resize and reallocate memory if necessary
Allocate(nr, nc);
// Copy image data
for (unsigned int i = 0; i < nr; ++i)
{
for (unsigned int j = 0; j < nc; ++j)
{
m_Data[i][j] = (imageIn->data())[i][j];
}
}
}
// Copy data from an image of smaller or equal size
// into this image, starting at given row/col index
template <typename T>
void Image<T>::CopyFrom(const Image<T>* imageIn,
unsigned int rowStart,
unsigned int colStart)
{
unsigned int nr = imageIn->nRows();
unsigned int nc = imageIn->nCols();
unsigned int rowEnd = rowStart + nr - 1;
unsigned int colEnd = colStart + nc - 1;
// Check bounds
assert(rowStart >= 0 && rowStart < m_nRows
&& rowEnd >= 0 && rowEnd < m_nRows
&& colStart >= 0 && colStart < m_nCols
&& colEnd >= 0 && colStart < m_nCols);
for (unsigned int i = rowStart; i <= rowEnd; ++i)
{
for (unsigned int j = colStart; j <= colEnd; ++j)
{
m_Data[i][j] = imageIn->data()[i-rowStart][j-rowStart];
}
}
}
// Need to do this to avoid undefined reference erros for
// templated classes in GNU make... haven't found a better
// way yet
template class Image<double>;
template class Image<unsigned char>;
template class Image<unsigned int>;