Skip to content

Commit

Permalink
Add image downsample method (#2159)
Browse files Browse the repository at this point in the history
This changelist adds an Image::applyBoxDownsample method, allowing images to be downsampled by an integer factor using a box filter.
  • Loading branch information
jstone-lucasfilm authored Dec 29, 2024
1 parent 59c5439 commit 8ba7530
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions source/MaterialXRender/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,35 @@ ImagePtr Image::applyGaussianBlur()
return blurImage2;
}

ImagePtr Image::applyBoxDownsample(unsigned int factor)
{
factor = std::max(factor, (unsigned int) 2);

ImagePtr sampleImage = Image::create(getWidth() / factor, getHeight() / factor, getChannelCount(), getBaseType());
sampleImage->createResourceBuffer();

for (int y = 0; y < (int) sampleImage->getHeight(); y++)
{
for (int x = 0; x < (int) sampleImage->getWidth(); x++)
{
Color4 sampleColor;
for (int dy = 0; dy < (int) factor; dy++)
{
int sy = std::min(std::max(y * (int) factor + dy, 0), (int) getHeight() - 1);
for (int dx = 0; dx < (int) factor; dx++)
{
int sx = std::min(std::max(x * (int) factor + dx, 0), (int) getWidth() - 1);
sampleColor += getTexelColor(sx, sy);
}
}
sampleColor /= (float) (factor * factor);
sampleImage->setTexelColor(x, y, sampleColor);
}
}

return sampleImage;
}

ImagePair Image::splitByLuminance(float luminance)
{
ImagePtr underflowImage = Image::create(getWidth(), getHeight(), getChannelCount(), getBaseType());
Expand Down
4 changes: 4 additions & 0 deletions source/MaterialXRender/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class MX_RENDER_API Image
/// Apply a 7x7 Gaussian blur to this image, returning a new blurred image.
ImagePtr applyGaussianBlur();

/// Downsample this image by an integer factor using a box filter,
/// returning the new reduced image.
ImagePtr applyBoxDownsample(unsigned int factor);

/// Split this image by the given luminance threshold, returning the
/// resulting underflow and overflow images.
ImagePair splitByLuminance(float luminance);
Expand Down
1 change: 1 addition & 0 deletions source/PyMaterialX/PyMaterialXRender/PyImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void bindPyImage(py::module& mod)
.def("copy", &mx::Image::copy)
.def("applyBoxBlur", &mx::Image::applyBoxBlur)
.def("applyGaussianBlur", &mx::Image::applyGaussianBlur)
.def("applyBoxDownsample", &mx::Image::applyBoxDownsample)
.def("splitByLuminance", &mx::Image::splitByLuminance)
.def("setResourceBuffer", &mx::Image::setResourceBuffer)
.def("getResourceBuffer", &mx::Image::getResourceBuffer)
Expand Down

0 comments on commit 8ba7530

Please sign in to comment.