-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.hpp
More file actions
67 lines (57 loc) · 1.33 KB
/
image.hpp
File metadata and controls
67 lines (57 loc) · 1.33 KB
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
#include <stddef.h>
#include <stdio.h>
#include <jpeglib.h>
#include <iostream>
// Image class
template<class Pix>
class Image {
public:
Image(int height = 0, int width = 0)
: _width(width), _height(height) {
allocate_array();
}
Image(const Image<Pix>& other)
: _width(other._width), _height(other._height) {
allocate_array();
for (int col = 0; col < _width; col++) {
for (int row = 0; row < _height; row++) {
_array[row*_width + col] = other._array[row*_width + col];
}
}
}
Image& operator=(const Image& other) {
dealloc_array();
_width = other._width;
_height = other._height;
allocate_array();
for (int col = 0; col < _width; col++) {
for (int row = 0; row < _height; row++) {
_array[row*_width + col] = other._array[row*_width + col];
}
}
return *this;
}
Pix* operator[](const size_t i) {
return _array+(_width*i);
}
~Image() {
dealloc_array();
};
int width() const { return _width; }
int height() const { return _height; }
protected:
void dealloc_array() {
if (_array)
delete[] _array;
_array = NULL;
}
void allocate_array() {
//Check for NULL
if (_width* _height > 0)
_array = new Pix[_width*_height];
else
_array = NULL;
}
int _width, _height;
Pix *_array;
};