Skip to content

Commit 8b58e50

Browse files
committed
fixed overflow for large normalmaps integrations
1 parent e0c8676 commit 8b58e50

5 files changed

+38
-25
lines changed

relightlab/normalsframe.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ void NormalsFrame::save() {
9595
NormalsTask *task = new NormalsTask();
9696
task->setParameters(parameters);
9797
task->output = parameters.path;
98-
task->initFromProject(qRelightApp->project());
98+
if(parameters.compute)
99+
task->initFromProject(qRelightApp->project());
99100

100101
ProcessQueue &queue = ProcessQueue::instance();
101102
queue.addTask(task);

relightlab/normalstask.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ void NormalsTask::run() {
149149
case FLAT_FOURIER:
150150
//convert radius to frequencies
151151
double sigma = width*parameters.flatPercentage/100;
152-
flattenFourierNormals(width, height, normals, 0.2, sigma);
152+
try {
153+
flattenFourierNormals(width, height, normals, 0.2, sigma);
154+
} catch(std::length_error e) {
155+
return;
156+
}
153157
break;
154158
}
155159
}
@@ -194,8 +198,16 @@ void NormalsTask::run() {
194198
vector<float> z;
195199
if(parameters.surface_integration == SURFACE_BNI)
196200
bni_integrate(callback, width, height, normals, z, parameters.bni_k);
197-
else
198-
fft_integrate(callback, width, height, normals, z);
201+
else {
202+
203+
try {
204+
fft_integrate(callback, width, height, normals, z);
205+
} catch(std::length_error e) {
206+
error = "Failed to integrate normals, length error.";
207+
status = FAILED;
208+
return;
209+
}
210+
}
199211

200212
if(z.size() == 0) {
201213
error = "Failed to integrate normals";

src/bni_normal_integration.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <Eigen/Core>
1+
#include <Eigen/Core>
22
#include <Eigen/Dense>
33
#include <Eigen/SparseCore>
44
#include <Eigen/IterativeLinearSolvers>
@@ -23,15 +23,15 @@ double sigmoid(const double x, const double k = 1.0) {
2323
return 1.0 / (1.0 + exp(-x*k));
2424
}
2525

26-
bool saveDepthMap(const QString &filename, int w, int h, std::vector<float> &z) {
26+
bool saveDepthMap(const QString &filename, size_t w, size_t h, std::vector<float> &z) {
2727
return false;
2828
}
2929

30-
bool saveNormalMap(const QString &filename, int w, int h, std::vector<float> &normals) {
30+
bool saveNormalMap(const QString &filename, size_t w, size_t h, std::vector<float> &normals) {
3131
QImage img(w, h, QImage::Format_ARGB32);
32-
for(int y = 0; y < h; y++) {
32+
for(size_t y = 0; y < h; y++) {
3333
uint8_t *line = img.scanLine(y);
34-
for(int x = 0; x < w; x++) {
34+
for(size_t x = 0; x < w; x++) {
3535
float *n = &normals[3*(x + y*w)];
3636
line[4*x + 0] = 255;
3737
line[4*x + 1] = floor(n[0]*255.0f);
@@ -43,7 +43,7 @@ bool saveNormalMap(const QString &filename, int w, int h, std::vector<float> &no
4343
return true;
4444
}
4545

46-
bool savePly(const QString &filename, int w, int h, std::vector<float> &z) {
46+
bool savePly(const QString &filename, size_t w, size_t h, std::vector<float> &z) {
4747
QFile file(filename);
4848
bool success = file.open(QFile::WriteOnly);
4949
if(!success)
@@ -63,9 +63,9 @@ bool savePly(const QString &filename, int w, int h, std::vector<float> &z) {
6363
}
6464

6565
std::vector<float> vertices(w*h*3);
66-
for(int y = 0; y < h; y++) {
67-
for(int x = 0; x < w; x++) {
68-
int pos = x + y*w;
66+
for(size_t y = 0; y < h; y++) {
67+
for(size_t x = 0; x < w; x++) {
68+
size_t pos = x + y*w;
6969
float *start = &vertices[3*pos];
7070
start[0] = x;
7171
start[1] = h - y -1;
@@ -74,9 +74,9 @@ bool savePly(const QString &filename, int w, int h, std::vector<float> &z) {
7474
}
7575
}
7676
std::vector<uint8_t> indices(13*2*(w-1)*(h-1));
77-
for(int y = 0; y < h-1; y++) {
78-
for(int x = 0; x < w-1; x++) {
79-
int pos = x + y*w;
77+
for(size_t y = 0; y < h-1; y++) {
78+
for(size_t x = 0; x < w-1; x++) {
79+
size_t pos = x + y*w;
8080
uint8_t *start = &indices[26*(x + y*(w-1))];
8181
start[0] = 3;
8282
int *face = (int *)(start + 1);
@@ -99,7 +99,7 @@ bool savePly(const QString &filename, int w, int h, std::vector<float> &z) {
9999
return true;
100100
}
101101

102-
bool saveTiff(const QString &filename, int w, int h, std::vector<float> &depthmap) {
102+
bool saveTiff(const QString &filename, size_t w, size_t h, std::vector<float> &depthmap) {
103103
float min = 1e20;
104104
float max = -1e20;
105105
for(float h: depthmap) {
@@ -140,8 +140,8 @@ bool saveTiff(const QString &filename, int w, int h, std::vector<float> &depthma
140140

141141
for(uint32_t dy = 0; dy < tileLength; dy++) {
142142
for(uint32_t dx = 0; dx < tileWidth; dx++) {
143-
uint32_t x = tx*tileWidth + dx;
144-
uint32_t y = ty*tileLength + dy;
143+
size_t x = tx*tileWidth + dx;
144+
size_t y = ty*tileLength + dy;
145145
if(x < w && y < h)
146146
data[dx + dy*tileWidth] = depthmap[x + y*w];
147147
}

src/bni_normal_integration.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ std::vector<float> bni_pyramid(std::function<bool(QString s, int n)> progressed,
2626
int max_solver_iterations = 5000,
2727
int scale = 0);
2828

29-
bool savePly(const QString &filename, int w, int h, std::vector<float> &z);
30-
bool saveTiff(const QString &filename, int w, int h, std::vector<float> &z);
31-
bool saveDepthMap(const QString &filename, int w, int h, std::vector<float> &z);
29+
bool savePly(const QString &filename, size_t w, size_t h, std::vector<float> &z);
30+
bool saveTiff(const QString &filename, size_t w, size_t h, std::vector<float> &z);
31+
bool saveDepthMap(const QString &filename, size_t w, size_t h, std::vector<float> &z);
3232

3333
#endif // BNI_NORMAL_INTEGRATION_H

src/fft_normal_integration.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void depad(int &w, int &h, std::vector<float> &heights, int padding) {
159159
swap(elev, heights);
160160
}
161161

162-
bool savePly(const QString &filename, int w, int h, std::vector<float> &z);
162+
bool savePly(const QString &filename, size_t w, size_t h, std::vector<float> &z);
163163

164164
void fft_integrate(std::function<bool(QString s, int n)> progressed,
165165
int cols, int rows, std::vector<float> &normals, std::vector<float> &heights) {
@@ -213,9 +213,9 @@ void fft_integrate(std::function<bool(QString s, int n)> progressed,
213213
heights[i * cols + j] = static_cast<float>(z(i, j));
214214
}
215215
}
216-
217-
savePly("test.ply", cols, rows, heights);
218216
depad(cols, rows, heights, padding);
217+
savePly("test.ply", cols, rows, heights);
218+
219219

220220
/*
221221
[wx, wy] = meshgrid(([1:cols]-(fix(cols/2)+1))/(cols-mod(cols,2)), ...

0 commit comments

Comments
 (0)