From d543a09bcfaf3d8a28541423baa65ad0ef2b72a4 Mon Sep 17 00:00:00 2001 From: Daniel Dresser Date: Sun, 22 Jan 2023 22:10:22 -0800 Subject: [PATCH] RankFilter : Use Sampler::visitPixels to improve performance --- src/GafferImage/RankFilter.cpp | 40 +++++++++++++++------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/GafferImage/RankFilter.cpp b/src/GafferImage/RankFilter.cpp index 72b97544fd1..cc72d3a8a6d 100644 --- a/src/GafferImage/RankFilter.cpp +++ b/src/GafferImage/RankFilter.cpp @@ -225,8 +225,9 @@ void RankFilter::compute( Gaffer::ValuePlug *output, const Gaffer::Context *cont vector &result = resultData->writable(); result.reserve( ImagePlug::tileSize() * ImagePlug::tileSize() ); - vector pixels( ( 1 + 2 * radius.x ) * ( 1 + 2 * radius.y ) ); - vector sortPixels( ( 1 + 2 * radius.x ) * ( 1 + 2 * radius.y ) ); + int supportSize = 1 + 2 * radius.x; + vector pixels( supportSize * supportSize ); + vector sortPixels( supportSize * supportSize ); vector::iterator resultIt = sortPixels.begin() + sortPixels.size() / 2; V2i p; @@ -237,15 +238,11 @@ void RankFilter::compute( Gaffer::ValuePlug *output, const Gaffer::Context *cont IECore::Canceller::check( context->canceller() ); // Fill array with all nearby samples - V2i o; - vector::iterator pixelsIt = pixels.begin(); - for( o.y = -radius.y; o.y <= radius.y; ++o.y ) - { - for( o.x = -radius.x; o.x <= radius.x; ++o.x ) - { - *pixelsIt++ = sampler.sample( p.x + o.x, p.y + o.y ); - } - } + V2i corner = p - radius; + sampler.visitPixels( + Imath::Box2i( p - radius, p + radius + Imath::V2i( 1 ) ), + [corner, supportSize, &pixels]( float v, int x, int y ){ pixels[ ( y - corner.y ) * supportSize + x - corner.x ] = v; } + ); switch( m_mode ) { @@ -268,7 +265,8 @@ void RankFilter::compute( Gaffer::ValuePlug *output, const Gaffer::Context *cont V2i r( INT_MAX, INT_MAX ); int closestMatch = INT_MAX; - pixelsIt = pixels.begin(); + vector::iterator pixelsIt = pixels.begin(); + V2i o; for( o.y = -radius.y; o.y <= radius.y; ++o.y ) { for( o.x = -radius.x; o.x <= radius.x; ++o.x ) @@ -398,7 +396,8 @@ IECore::ConstFloatVectorDataPtr RankFilter::computeChannelData( const std::strin return resultData; } - vector pixels( ( 1 + 2 * radius.x ) * ( 1 + 2 * radius.y ) ); + int supportSize = 1 + 2 * radius.x; + vector pixels( supportSize * supportSize ); vector::iterator resultIt = pixels.begin() + pixels.size() / 2; V2i p; @@ -408,15 +407,12 @@ IECore::ConstFloatVectorDataPtr RankFilter::computeChannelData( const std::strin { IECore::Canceller::check( context->canceller() ); - V2i o; - vector::iterator pixelsIt = pixels.begin(); - for( o.y = -radius.y; o.y <= radius.y; ++o.y ) - { - for( o.x = -radius.x; o.x <= radius.x; ++o.x ) - { - *pixelsIt++ = sampler.sample( p.x + o.x, p.y + o.y ); - } - } + V2i corner = p - radius; + sampler.visitPixels( + Imath::Box2i( p - radius, p + radius + Imath::V2i( 1 ) ), + [corner, supportSize, &pixels]( float v, int x, int y ){ pixels[ ( y - corner.y ) * supportSize + x - corner.x ] = v; } + ); + switch( m_mode ) { case MedianRank: