This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilm.cc
57 lines (50 loc) · 1.37 KB
/
film.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
// Copyright (c) 2016 Kai Luo. All rights reserved.
#include <OpenEXR/ImfRgba.h>
#include <OpenEXR/ImfRgbaFile.h>
#include "color.h"
#include "film.h"
#include "kl/error.h"
namespace zLi {
Film::Film(const Film &f) {
data_.resize(f.width_);
for (int i = 0; i < f.width_; ++i) {
for (int j = 0; j < f.height_; ++j) {
data_[i].push_back(std::make_unique<Spectrum>(*f.data_[i][j]));
}
}
}
void Film::Set(int i, int j, Spectrum &&s) {
if (i >= 0 && i < width_ && j >= 0 && j < height_) {
data_[i][j] = std::make_unique<Spectrum>(std::move(s));
}
}
// wont check size of pixels
void Film::FillRgba(Imf::Rgba *pixels) {
for (int i = 0; i < width_; ++i) {
for (int j = 0; j < height_; ++j) {
if (data_[i][j]) {
RGBColor rgb(data_[i][j]->ToRGB());
int k = i * height_ + j;
pixels[k].r = rgb.r;
pixels[k].g = rgb.g;
pixels[k].b = rgb.b;
pixels[k].a = 0;
}
}
}
}
kl::Result<void> Film::WriteEXR(const std::string &exr) {
Imf::Rgba *pixels = new Imf::Rgba[width_ * height_];
FillRgba(pixels);
try {
Imf::RgbaOutputFile file(exr.c_str(), width_, height_, Imf::WRITE_RGBA);
file.setFrameBuffer(pixels, height_, 1);
file.writePixels(height_);
} catch (const std::exception &e) {
delete[] pixels;
return kl::Err(e.what());
}
delete[] pixels;
return kl::Ok();
}
} // namespace zLi