Skip to content

Commit

Permalink
Fix several points of failure with pixel counts in the billions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom94 committed Oct 1, 2024
1 parent 2c7e709 commit 8b9e04b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
7 changes: 4 additions & 3 deletions include/tev/Box.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ struct Box {
return nanogui::max(max - min, Vector{(T)0});
}

T area() const {
using area_t = std::conditional_t<std::is_integral_v<T>, size_t, T>;
area_t area() const {
auto size = this->size();
T result = (T)1;
area_t result = (T)1;
for (uint32_t i = 0; i < N_DIMS; ++i) {
result *= size[i];
result *= (area_t)size[i];
}

return result;
Expand Down
6 changes: 3 additions & 3 deletions include/tev/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Channel {
return 0;
}

return mData[index.x() + index.y() * mSize.x()];
return mData[index.x() + index.y() * (size_t)mSize.x()];
}

float& at(size_t index) {
Expand All @@ -61,11 +61,11 @@ class Channel {
}

float& at(nanogui::Vector2i index) {
return at(index.x() + index.y() * mSize.x());
return at(index.x() + index.y() * (size_t)mSize.x());
}

float at(nanogui::Vector2i index) const {
return at(index.x() + index.y() * mSize.x());
return at(index.x() + index.y() * (size_t)mSize.x());
}

size_t numPixels() const {
Expand Down
2 changes: 1 addition & 1 deletion src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void Channel::updateTile(int x, int y, int width, int height, const vector<float

for (int posY = 0; posY < height; ++posY) {
for (int posX = 0; posX < width; ++posX) {
at({x + posX, y + posY}) = newData[posX + posY * width];
at({x + posX, y + posY}) = newData[posX + posY * (size_t)width];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ void Image::updateChannel(const string& channelName, int x, int y, int width, in

for (int posY = 0; posY < height; ++posY) {
for (int posX = 0; posX < width; ++posX) {
int tileIdx = posX + posY * width;
size_t tileIdx = posX + posY * (size_t)width;
textureData[tileIdx * 4 + i] = localChan->at({x + posX, y + posY});
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/ImageCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,18 +891,19 @@ Task<shared_ptr<CanvasStatistics>> ImageCanvas::computeCanvasStatistics(

int nChannels = result->nChannels = alphaChannel ? (int)flattened.size() - 1 : (int)flattened.size();

int pixelCount = 0;
size_t pixelCount = region.area();
for (int i = 0; i < nChannels; ++i) {
const auto& channel = flattened[i];
for (int y = region.min.y(); y < region.max.y(); ++y) {
for (int x = region.min.x(); x < region.max.x(); ++x) {
auto v = channel.at(Vector2i{x, y});
if (!isnan(v)) {
mean += v;
maximum = max(maximum, v);
minimum = min(minimum, v);
pixelCount++;
auto v = channel.at({x, y});
if (!isfinite(v)) {
continue;
}

mean += v;
maximum = max(maximum, v);
minimum = min(minimum, v);
}
}
}
Expand Down Expand Up @@ -944,17 +945,17 @@ Task<shared_ptr<CanvasStatistics>> ImageCanvas::computeCanvasStatistics(
}

auto regionSize = region.size();
auto numPixels = region.area();
size_t numPixels = region.area();
std::vector<int> indices(numPixels * nChannels);

vector<Task<void>> tasks;
for (int i = 0; i < nChannels; ++i) {
const auto& channel = flattened[i];
tasks.emplace_back(
ThreadPool::global().parallelForAsync(0, numPixels, [&, i](int j) {
ThreadPool::global().parallelForAsync((size_t)0, numPixels, [&, i](size_t j) {
int x = (j % regionSize.x()) + region.min.x();
int y = (j / regionSize.x()) + region.min.y();
indices[j + i * numPixels] = valToBin(channel.at(Vector2i{x, y}));
indices[j + i * numPixels] = valToBin(channel.at({x, y}));
}, priority)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/imageio/ExrImageLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class RawChannel {
auto data = reinterpret_cast<const T*>(mData.data());
co_await ThreadPool::global().parallelForAsync<int>(0, channel.size().y(), [&, data](int y) {
for (int x = 0; x < width; ++x) {
channel.at({x, y}) = data[x / mImfChannel.xSampling + (y / mImfChannel.ySampling) * widthSubsampled];
channel.at({x, y}) = data[x / mImfChannel.xSampling + (y / mImfChannel.ySampling) * (size_t)widthSubsampled];
}
}, priority);
}
Expand Down

0 comments on commit 8b9e04b

Please sign in to comment.