-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyImage.hpp
More file actions
118 lines (102 loc) · 3.07 KB
/
MyImage.hpp
File metadata and controls
118 lines (102 loc) · 3.07 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
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
// Copyright ?Robert Spangenberg, 2014.
// See license.txt for more details
#include <string>
#include "StereoCommon.h"
#include <cstring>
#include <sstream>
uint32 readNumber(uint8* p, uint32& index)
{
uint32 start = index;
while (p[index] != ' ' && p[index] != '\n')
{
index++;
}
std::string str;
str.append((char*)p+start, index - start);
uint32 result = atoi(str.c_str());
index++;
return result;
}
template <typename T>
void readPGM(MyImage<T>& img, const char* filename)
{
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (file.is_open())
{
// get length of file:
file.seekg(0, file.end);
uint32 length = (uint32)file.tellg();
file.seekg(0, file.beg);
// read whole file
uint8* p = (uint8*)_mm_malloc(length, 16);
file.read((char*)p, length);
file.close();
// parse header
if (p[0] != 'P' || p[1] != '5') {
std::cerr << "wrong magic number, only P5 PGM files supported" << filename << std::endl;
_mm_free(p);
return;
}
uint32 index = 3;
uint32 width = readNumber(p, index);
uint32 height = readNumber(p, index);
uint32 maxValue = readNumber(p, index);
// size checks
if (sizeof(T) == 1)
{
if (maxValue != 255) {
std::cerr << "bit depth of pgm does not match image type" << filename << std::endl;
_mm_free(p);
return;
}
}
if (sizeof(T) == 2)
{
if (maxValue == 255) {
std::cerr << "bit depth of pgm does not match image type" << filename << std::endl;
_mm_free(p);
return;
}
}
// check values
if (length - index - width*height*sizeof(T) != 0)
{
std::cerr << "error in image parsing, header does not match file length" << filename << std::endl;
_mm_free(p);
return;
}
// copy values
T* data = (T*)_mm_malloc(width*height*sizeof(T), 16);
memcpy(data, p + index, width*height*sizeof(T));
img.setAttributes(width, height, data);
_mm_free(p);
}
else
{
std::cerr << "could not open file to read "<< filename << std::endl;
}
}
template <typename T>
void writePGM(MyImage<T>& img, const char* filename, bool verbose)
{
std::ofstream file(filename, std::ios::out | std::ios::binary);
if (file.is_open())
{
// write header
file << "P5\n";
std::ostringstream s;
s << img.getWidth()<<" "<<img.getHeight()<<"\n";
if (sizeof(T) == 1)
s << "255\n";
else
s << "65535\n";
file << s.str();
file.write((char*)img.getData(), img.getWidth()*img.getHeight()*sizeof(T));
file.close();
if (verbose)
{
std::cout << "write to file "<< filename << std::endl;
}
}
else std::cout << "Unable to open file "<<filename << std::endl;
}