-
Notifications
You must be signed in to change notification settings - Fork 21
/
image.hpp
151 lines (130 loc) · 3.9 KB
/
image.hpp
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
//Copyright (C) 2011 Pierre Moulon
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef LIBS_IMAGE_IMAGE_H_
#define LIBS_IMAGE_IMAGE_H_
#include <iostream>
#include <cstring>
//using namespace std;
//-- Container for a 2D image
//-- This class ensure that the image have a width and a height
//-- and a 2D array of T data.
//-
//-- Data is saved in row major format
//-- Pixel access is done with operator(y,x)
// [2/3/2011 pierre MOULON]
//---------------------------
template <typename T>
class Image
{
public:
typedef T Tpixel; //-- Store the pixel data type
//------------------------------
//-- Image construction method
Image(){ _data = NULL; _width = _height = 0; }
Image(size_t width, size_t height, const T val = T())
{
_height = height;
_width = width;
_data = new T[_width * _height];
memset(_data, val, sizeof(T) * _width * _height);
}
Image(size_t width, size_t height, const T * imdata){
_height = height;
_width = width;
_data = new T[_width * _height];
memcpy( _data, imdata, _width * _height * sizeof(T) );
}
Image(const Image<T> & I){
_data = NULL; _width = _height = 0;
(*this) = I;
}
Image& operator=(const Image<T> & I) {
if( this != &I)
{
_height = I._height;
_width = I._width;
if(_data)
delete [] _data;
_data = new T[_width * _height];
memcpy( _data, I._data, _width * _height * sizeof(T) );
}
return (*this);
}
virtual ~Image(){
if (_data)
delete [] _data;
_width = _height = 0;
}
//-- Image construction method
//------------------------------
//------------------------------
//-- accessors/getters methods
/// Retrieve the width of the image
size_t Width() const { return _width; }
/// Retrieve the height of the image
size_t Height() const { return _height; }
/// Return the depth in byte of the pixel (unsigned char will return 1)
size_t Depth() const { return sizeof(Tpixel);}
/// image memory access
T * data() const {return _data;}
/// random pixel access
inline const T & operator()(size_t y, size_t x) const {return _data[ _width * y + x];}
/// random pixel access
inline T & operator()(size_t y, size_t x) {return _data[ _width * y + x];}
inline bool operator==(const Image<T> & img) const
{
if(_width == img._width &&
_height == img._height)
return (0 == memcmp(_data, img._data, Width()*Height()*sizeof(T)));
else
return false;
}
//-- accessors/getters methods
//------------------------------
/// Tell if a point is inside the image.
bool Contains(size_t y, size_t x) const {
return x < _width && y < _height;
}
//-- Utils
//
void Resize(size_t width, size_t height){
_height = height;
_width = width;
if(_data)
delete [] _data;
_data = new T[_width * _height];
}
void fill(const T & val)
{
memset(_data, val, sizeof(T) * _width * _height);
}
friend std::ostream & operator<<(std::ostream & os, const Image<T> & im) {
os << im.Width() << " " << im.Height();
for (size_t j = 0; j < im.Height(); ++j)
{
for (size_t i = 0; i < im.Width(); ++i)
os << im(j,i) << " ";
os << std::endl;
}
return os;
}
protected :
//-- Internal data :
// None
T * _data;
size_t _width;
size_t _height;
};
#endif // LIBS_IMAGE_IMAGE_H_