Skip to content

Commit

Permalink
RankFilter : Use Sampler::visitPixels to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldresser-ie committed Jan 23, 2023
1 parent 6229788 commit d90fcba
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/GafferImage/RankFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ void RankFilter::compute( Gaffer::ValuePlug *output, const Gaffer::Context *cont
vector<V2i> &result = resultData->writable();
result.reserve( ImagePlug::tileSize() * ImagePlug::tileSize() );

vector<float> pixels( ( 1 + 2 * radius.x ) * ( 1 + 2 * radius.y ) );
vector<float> sortPixels( ( 1 + 2 * radius.x ) * ( 1 + 2 * radius.y ) );
int supportSize = 1 + 2 * radius.x;
vector<float> pixels( supportSize * supportSize );
vector<float> sortPixels( supportSize * supportSize );
vector<float>::iterator resultIt = sortPixels.begin() + sortPixels.size() / 2;

V2i p;
Expand All @@ -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<float>::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 )
{
Expand All @@ -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<float>::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 )
Expand Down Expand Up @@ -398,7 +396,8 @@ IECore::ConstFloatVectorDataPtr RankFilter::computeChannelData( const std::strin
return resultData;
}

vector<float> pixels( ( 1 + 2 * radius.x ) * ( 1 + 2 * radius.y ) );
int supportSize = 1 + 2 * radius.x;
vector<float> pixels( supportSize * supportSize );
vector<float>::iterator resultIt = pixels.begin() + pixels.size() / 2;

V2i p;
Expand All @@ -408,15 +407,12 @@ IECore::ConstFloatVectorDataPtr RankFilter::computeChannelData( const std::strin
{
IECore::Canceller::check( context->canceller() );

V2i o;
vector<float>::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:
Expand Down

0 comments on commit d90fcba

Please sign in to comment.