-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemimage.cc
73 lines (60 loc) · 1.43 KB
/
memimage.cc
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
#include <iostream>
#include <fstream>
#include "memimage.hh"
template<class T> dm::memimage<T>::memimage(const std::string& filename)
{
std::ifstream file(filename.c_str());
file >> m_xw >> m_yw;
m_data.resize(m_xw*m_yw);
if( file )
{
for(unsigned y=0; y<m_yw; ++y)
for(unsigned x=0; x<m_xw; ++x)
{
file >> operator()(x, y);
}
}
if( ! file )
{
std::cerr << "Error reading from file\n";
std::exit(1);
}
}
template<class T> void dm::memimage<T>::dump_to_file
(const std::string& filename) const
{
std::ofstream file(filename.c_str());
file << xw() << ' ' << yw() << '\n';
for(unsigned y=0; y<yw(); ++y)
{
for(unsigned x=0; x<xw(); ++x)
{
file << operator()(x, y) << ' ';
}
file << '\n';
}
}
// checked pixel access
template<class T> T& dm::memimage<T>::pixel(unsigned x, unsigned y)
{
if( x >= m_xw || y >= m_yw)
throw out_of_range_exception();
return operator() (x, y);
}
// checked const pixel access
template<class T> T dm::memimage<T>::pixel(unsigned x, unsigned y) const
{
if( x >= m_xw || y >= m_yw)
throw out_of_range_exception();
return operator() (x, y);
}
#define DM_DEFINE_TEMPL(TYPE) \
template class dm::memimage<TYPE>;
DM_DEFINE_TEMPL(short)
DM_DEFINE_TEMPL(long)
DM_DEFINE_TEMPL(float)
DM_DEFINE_TEMPL(double)
DM_DEFINE_TEMPL(unsigned char)
DM_DEFINE_TEMPL(unsigned short)
DM_DEFINE_TEMPL(unsigned long)
#undef DM_DEFINE_TEMPL