-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagewriter.h
59 lines (48 loc) · 1.25 KB
/
imagewriter.h
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
#ifndef IMAGEWRITER_H
#define IMAGEWRITER_H
#include <string>
#include <iostream>
#include <fstream>
#include "image.h"
#include "type.h"
using namespace std;
class ImageWriter
{
public:
template <class T> bool write(Image<T> &img, Type t, string name)
{
bool success = false;
switch (t) {
case PGM:
success = write(img, name, ".pgm", "P2");
break;
case PPM:
success = write(img, name, ".ppm", "P3");
default:
break;
}
return success;
}
private:
template <class T> bool write(Image<T> &img, string name, string ext, string ver)
{
bool success = false;
std::ofstream out;
string file = name + ext;
out.open(file.c_str());
int height = img.getHeight();
int width = img.getWidth();
out << ver << '\n';
out << width << ' ' << height << '\n';
out << img.getMax() << '\n';
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width - 1; ++j)
out << img.getPixel(i, j) << ' ';
out << img.getPixel(i, width - 1) << '\n';
}
out.close();
success = true;
return success;
}
};
#endif // IMAGEWRITER_H