Skip to content

Commit

Permalink
Fix gamma processing out of image bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 12, 2020
1 parent b2f397d commit 3180ec4
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,28 @@ public BokehBlurProcessor(Configuration configuration, BokehBlurProcessor defini
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source)
{
var sourceRectangle = Rectangle.Intersect(this.SourceRectangle, source.Bounds());

// Preliminary gamma highlight pass
var gammaOperation = new ApplyGammaExposureRowOperation(this.SourceRectangle, source.PixelBuffer, this.Configuration, this.gamma);
var gammaOperation = new ApplyGammaExposureRowOperation(sourceRectangle, source.PixelBuffer, this.Configuration, this.gamma);
ParallelRowIterator.IterateRows<ApplyGammaExposureRowOperation, Vector4>(
this.Configuration,
this.SourceRectangle,
sourceRectangle,
in gammaOperation);

// Create a 0-filled buffer to use to store the result of the component convolutions
using Buffer2D<Vector4> processingBuffer = this.Configuration.MemoryAllocator.Allocate2D<Vector4>(source.Size(), AllocationOptions.Clean);

// Perform the 1D convolutions on all the kernel components and accumulate the results
this.OnFrameApplyCore(source, this.SourceRectangle, this.Configuration, processingBuffer);
this.OnFrameApplyCore(source, sourceRectangle, this.Configuration, processingBuffer);

float inverseGamma = 1 / this.gamma;

// Apply the inverse gamma exposure pass, and write the final pixel data
var operation = new ApplyInverseGammaExposureRowOperation(this.SourceRectangle, source.PixelBuffer, processingBuffer, this.Configuration, inverseGamma);
var operation = new ApplyInverseGammaExposureRowOperation(sourceRectangle, source.PixelBuffer, processingBuffer, this.Configuration, inverseGamma);
ParallelRowIterator.IterateRows(
this.Configuration,
this.SourceRectangle,
sourceRectangle,
in operation);
}

Expand All @@ -116,8 +118,6 @@ private void OnFrameApplyCore(
// Allocate the buffer with the intermediate convolution results
using Buffer2D<ComplexVector4> firstPassBuffer = configuration.MemoryAllocator.Allocate2D<ComplexVector4>(source.Size());

var interest = Rectangle.Intersect(sourceRectangle, source.Bounds());

// Unlike in the standard 2 pass convolution processor, we use a rectangle of 1x the interest width
// to speedup the actual convolution, by applying bulk pixel conversion and clamping calculation.
// The second half of the buffer will just target the temporary buffer of complex pixel values.
Expand All @@ -128,8 +128,8 @@ private void OnFrameApplyCore(
using var mapX = new KernelSamplingMap(configuration.MemoryAllocator);
using var mapY = new KernelSamplingMap(configuration.MemoryAllocator);

mapX.BuildSamplingOffsetMap(1, this.kernelSize, interest);
mapY.BuildSamplingOffsetMap(this.kernelSize, 1, interest);
mapX.BuildSamplingOffsetMap(1, this.kernelSize, sourceRectangle);
mapY.BuildSamplingOffsetMap(this.kernelSize, 1, sourceRectangle);

ref Complex64[] baseRef = ref MemoryMarshal.GetReference(this.kernels.AsSpan());
ref Vector4 paramsRef = ref MemoryMarshal.GetReference(this.kernelParameters.AsSpan());
Expand All @@ -143,7 +143,7 @@ private void OnFrameApplyCore(

// Horizontal convolution
var horizontalOperation = new FirstPassConvolutionRowOperation(
interest,
sourceRectangle,
firstPassBuffer,
source.PixelBuffer,
mapX,
Expand All @@ -152,12 +152,12 @@ private void OnFrameApplyCore(

ParallelRowIterator.IterateRows<FirstPassConvolutionRowOperation, Vector4>(
configuration,
interest,
sourceRectangle,
in horizontalOperation);

// Vertical 1D convolutions to accumulate the partial results on the target buffer
var verticalOperation = new BokehBlurProcessor.SecondPassConvolutionRowOperation(
interest,
sourceRectangle,
processingBuffer,
firstPassBuffer,
mapY,
Expand All @@ -167,7 +167,7 @@ private void OnFrameApplyCore(

ParallelRowIterator.IterateRows(
configuration,
interest,
sourceRectangle,
in verticalOperation);
}
}
Expand Down

0 comments on commit 3180ec4

Please sign in to comment.