-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageWriter.cpp
More file actions
98 lines (94 loc) · 2.54 KB
/
ImageWriter.cpp
File metadata and controls
98 lines (94 loc) · 2.54 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
/*
* =====================================================================================
*
* Filename: ImageWriter.cpp
*
* Description:
*
* Version: 1.0
* Created: 04/04/2011 05:10:01 PM
* Revision: none
* Compiler: gcc
*
* Author: Mathias Buhr (), [email protected]
*
* =====================================================================================
*/
#include "ImageIO.h"
#include <iostream>
#include <fstream>
using namespace ray;
using namespace ray::img;
Writer::Writer()
: _prefix("outfile"), _fmt(PPM)
{
}
void Writer::setFileFormat(const std::string& ext)
{
if (ext.compare("ppm") == 0)
_fmt = PPM;
else if (ext.compare("jpg") == 0)
_fmt = JPG;
}
bool Writer::write(const PixelBuffer &buffer, uint32_t height, uint32_t width )
{
bool ret = false;
switch (_fmt) {
case PPM:
ret = writePPM(buffer, height, width);
break;
case JPG:
ret = writeJPG(buffer, height, width);
break;
default:
std::cerr << "fileformat not implemented" << std::endl;
}
return ret;
}
bool Writer::writePPM(const PixelBuffer &buffer, uint32_t height, uint32_t width)
{
std::ofstream outfile;
outfile.open(getOutputFilePath().c_str());
if ( outfile.is_open() ) {
outfile << "P3" << std::endl;
outfile << width << " " << height << std::endl;
outfile << "255" << std::endl;
PixelBuffer::const_iterator it = buffer.begin();
uint32_t i= 0;
while (it != buffer.end()) {
math::Vec4 out = it->get() * 255.0f;
outfile << (int)out.x() << " "
<< (int)out.y() << " "
<< (int)out.z() << " ";
++i;
if (i % (width) == 0)
outfile << std::endl;
it++;
}
return true;
}
return false;
}
bool Writer::writeJPG(const PixelBuffer &buffer, uint32_t height, uint32_t width)
{
return false;
}
std::string Writer::getOutputFilePath() const
{
if (_path.length() > 0)
return _path + std::string("/") + _prefix + std::string(".") + getFileExtension();
else
return _prefix + std::string(".") + getFileExtension();
}
std::string Writer::getFileExtension() const
{
switch (_fmt) {
case PPM:
return std::string("ppm");
case JPG:
return std::string("jpg");
default:
std::cerr << "format not implemented" << std::endl;
return std::string();
}
}