-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathframebuffer.cpp
176 lines (157 loc) · 5.21 KB
/
framebuffer.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "framebuffer.h"
#include "./ui_framebuffer.h"
#include "mesh.h"
#include "utils.h"
#include <QProgressBar>
#include <QTimer>
#include <tbb/tbb.h>
using namespace Mpcv;
struct TaskGroup {
tbb::task_group group;
tbb::mutex mutex;
};
inline float aces(const float v0) {
float v = 0.6f * v0;
float a = 2.51f;
float b = 0.03f;
float c = 2.43f;
float d = 0.59f;
float e = 0.14f;
return (v * (a * v + b)) / (v * (c * v + d) + e);
}
inline Color colormap(const Pvl::Vec3f& color, float exposure) {
Color result;
for (int c = 0; c < 3; ++c) {
float value = exposure * color[c];
// float compressed = 5.f * value / (5.f + value);
float compressed = aces(value);
float clamped = std::max(std::min(compressed, 1.f), 0.f);
result[c] = uint8_t(std::pow(clamped, 1.f / 2.2f) * 255.f);
}
return result;
}
void View::paintEvent(QPaintEvent*) {
QImage image;
{
tbb::mutex::scoped_lock lock(tg_->mutex);
Pvl::Vec2i dims = image_.dimension();
image = QImage(dims[0], dims[1], QImage::Format_RGB888);
image.fill(QColor(0, 0, 0));
for (int y = 0; y < dims[1]; ++y) {
for (int x = 0; x < dims[0]; ++x) {
Pvl::Vec2i pix(x, y);
Color color = colormap(image_(pix), exposure_);
image.setPixelColor(x, y, QColor(color[0], color[1], color[2]));
}
}
}
// image.save("render-" + QString::number(pass) + ".png");
QRect targetRect = rect();
QRect sourceRect = image.rect();
float targetAspect = float(targetRect.width()) / targetRect.height();
float sourceAspect = float(sourceRect.width()) / sourceRect.height();
if (targetAspect > sourceAspect) {
int newWidth = sourceRect.width() * float(targetRect.height()) / sourceRect.height();
int newX = (targetRect.width() - newWidth) / 2;
targetRect.setX(newX);
targetRect.setWidth(newWidth);
} else {
int newHeight = sourceRect.height() * float(targetRect.width()) / sourceRect.width();
int newY = (targetRect.height() - newHeight) / 2;
targetRect.setY(newY);
targetRect.setHeight(newHeight);
}
QPainter painter(this);
painter.fillRect(rect(), QColor(0, 0, 0));
painter.drawImage(targetRect, image, sourceRect);
painter.end();
}
void View::setTaskGroup(std::shared_ptr<TaskGroup> tg) {
tg_ = tg;
}
void View::setImage(Image&& image) {
{
tbb::mutex::scoped_lock lock(tg_->mutex);
image_ = std::move(image);
}
update();
}
void View::setExposure(int exposure) {
exposure_ = std::pow(2.f, float(exposure - 50.f) / 10.f);
update();
}
void View::save() {
QDir& initialDir = saveFileDialogInitialDir();
QString file = QFileDialog::getSaveFileName(this,
tr("Save render"),
initialDir.path(),
tr("PNG image (*.png);;JPEG image (*.jpg);;Targa image (*.tga)"));
if (!file.isEmpty()) {
QFileInfo info(file);
initialDir = info.dir();
if (info.suffix().isEmpty()) {
file += ".png";
}
/// \todo deduplicate
QImage image;
{
tbb::mutex::scoped_lock lock(tg_->mutex);
Pvl::Vec2i dims = image_.dimension();
image = QImage(dims[0], dims[1], QImage::Format_RGB888);
image.fill(QColor(0, 0, 0));
for (int y = 0; y < dims[1]; ++y) {
for (int x = 0; x < dims[0]; ++x) {
Pvl::Vec2i pix(x, y);
Color color = colormap(image_(pix), exposure_);
image.setPixelColor(x, y, QColor(color[0], color[1], color[2]));
}
}
}
image.save(file);
}
}
FrameBufferWidget::FrameBufferWidget(QWidget* parent)
: QMainWindow(parent)
, ui_(new Ui::FrameBuffer) {
ui_->setupUi(this);
view_ = findChild<View*>("view");
progressBar_ = findChild<QProgressBar*>("progress");
iterationBar_ = findChild<QProgressBar*>("iterations");
/*QWidget* layout = findChild<QWidget*>("horizontalLayoutWidget");
view_ = new View(layout);
view_->setGeometry(10, 10, width() - 20, height() - 20);
view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout->adjustSize();*/
tg_ = std::make_shared<TaskGroup>();
view_->setTaskGroup(tg_);
QTimer* timer = new QTimer(this);
QObject::connect(timer, &QTimer::timeout, this, [this] {
iterationBar_->setValue(passValue_);
progressBar_->setValue(progressValue_);
});
timer->start(200);
}
FrameBufferWidget::~FrameBufferWidget() {
delete ui_;
}
void FrameBufferWidget::setNumIters(int numIters) {
iterationBar_->setMaximum(numIters);
}
void FrameBufferWidget::setProgress(const int pass, const int prog) {
passValue_ = pass;
progressValue_ = prog;
}
void FrameBufferWidget::run(const std::function<void()>& func) {
tg_->group.run(func);
}
void FrameBufferWidget::on_actionSave_render_triggered() {
view_->save();
}
void FrameBufferWidget::on_horizontalSlider_valueChanged(int value) {
view_->setExposure(value);
}
void FrameBufferWidget::on_actionClose_triggered() {
cancelled_ = true;
tg_->group.wait();
close();
}