-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPngWriter.cpp
More file actions
59 lines (49 loc) · 1.87 KB
/
Copy pathPngWriter.cpp
File metadata and controls
59 lines (49 loc) · 1.87 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
#include "PngWriter.h"
#include <cstdio>
#include <iostream>
namespace RayTracer {
void PngWriter::writePng(const Picture& picture, const std::string& path)
{
const auto png = fopen(path.c_str(), "wb");
if (!png)
{
std::cout << "Could not open " << path << "!" << std::endl;
exit(1);
}
auto pngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!pngStruct)
{
std::cout << "Could not create PNG write struct!" << std::endl;
exit(1);
}
auto pngInfo = png_create_info_struct(pngStruct);
if (!pngInfo)
{
png_destroy_write_struct(&pngStruct, nullptr);
std::cout << "Could not create PNG info struct!" << std::endl;
exit(1);
}
try
{
png_init_io(pngStruct, png);
png_set_IHDR(pngStruct, pngInfo, picture.getResolutionX(), picture.getResolutionY(), 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(pngStruct, pngInfo);
auto pictureData = picture.getData();
auto offsetVector = std::vector<png_byte *>();
offsetVector.reserve(picture.getResolutionY());
for (auto i = 0ul; i < pictureData.size(); i += picture.getResolutionX() * 3)
offsetVector.emplace_back(&pictureData[i]);
png_write_image(pngStruct, offsetVector.data());
png_write_end(pngStruct, pngInfo);
}
catch (std::exception& e)
{
png_destroy_write_struct(&pngStruct, &pngInfo);
fclose(png);
std::cout << "Could not write PNG!" << std::endl;
exit(1);
}
png_destroy_write_struct(&pngStruct, &pngInfo);
fclose(png);
}
} // RayTracer