-
-
Notifications
You must be signed in to change notification settings - Fork 887
Bokeh blur implementation #842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JimBobSquarePants
merged 85 commits into
SixLabors:master
from
Sergio0694:feature_bokeh-blur-impl
Aug 6, 2019
Merged
Changes from 60 commits
Commits
Show all changes
85 commits
Select commit
Hold shift + click to select a range
034472e
Added base BokehBlurProcessor class, and kernel parameters
Sergio0694 cdd004d
Added method to calculate the kernel parameters
Sergio0694 0bcd244
Switched to float, added method to create the 1D kernels
Sergio0694 3dbf5e5
Added complex kernels normalization
Sergio0694 c21d35b
Added BokehBlurExtensions class
Sergio0694 e0c60cc
Added the Complex64 struct type
Sergio0694 b42b9f7
Switched to Complex64 in the BokehBlurProcessor
Sergio0694 177a40c
Added caching system for the bokeh processor parameters
Sergio0694 0716ced
Added WeightedSum method to the Complex64 type
Sergio0694 47721ce
Added IEquatable<T> interface to the Complex64 type
Sergio0694 8468a2f
New complex types added
Sergio0694 2e7fb74
Added method to reshape a DenseMatrix<T> with no copies
Sergio0694 bd8c2e3
Added bokeh convolution first pass (WIP)
Sergio0694 ceebb7b
Added second bokeh convolution pass (WIP)
Sergio0694 732d229
Added image sum pass to the bokeh processor (WIP)
Sergio0694 5fe13ea
Minor bug fixes (WIP)
Sergio0694 df8c786
Switched to Vector4 processing in the bokeh computation
Sergio0694 9bd1c87
Minor tweaks
Sergio0694 15d0a98
Added Unit test for the bokeh kernel components
Sergio0694 6fc9c50
Minor performance improvements
Sergio0694 5985904
Minor code refactoring, added gamma parameter (WIP)
Sergio0694 3b7ad24
Removed unused temp buffers in the bokeh processing
Sergio0694 c654965
Gamma highlight processing implemented
Sergio0694 a261f93
Speed optimizations, fixed partials computations in target rectangle
Sergio0694 b36a907
Increased epsilon value in the unit tests
Sergio0694 5b8579b
Fixed for alpha transparency blur
Sergio0694 bf090ad
Fixed a bug when only blurring a target rectangle
Sergio0694 34308fe
Added bokeh blur image tests (WIP)
Sergio0694 c2486eb
Added IXunitSerializable interface to the test info class
Sergio0694 1fe7a43
culture independent parsing in BokehBlurTest.cs
antonfirsov fb4efe5
Performance optimizations in the bokeh processor
Sergio0694 9711d5f
Reduced number of memory allocations, fixed bug with multiple components
Sergio0694 697766e
Initialization and other speed improvements
Sergio0694 bd348a3
More initialization speed improvements
Sergio0694 ee5bbc3
Replaced LINQ with manual loop
Sergio0694 a64636e
Added BokehBlur overload to just specify the target bounds
Sergio0694 e18066e
Speed optimizations to the bokeh 1D convolutions
Sergio0694 1122563
More speed optimizations to the bokeh processor
Sergio0694 afcf78a
Fixed code style and Complex64.ToString method
Sergio0694 b56da13
Fixed processing buffer initialization
Sergio0694 32d062d
Minor performance improvements
Sergio0694 6e3e541
FIxed issue when applying bokeh blur to specific bounds
Sergio0694 39ce26c
Minor speed optimizaations
Sergio0694 4f77a38
Minor code refactoring
Sergio0694 93b6a82
Fixed convolution upper bound in second 1D pass
Sergio0694 d0b0b2b
improve BokehBlurTest coverage
antonfirsov 3772e89
use Gray8 instead of Alpha8
antonfirsov 2ed623a
Adjusted guard position in bokeh processor constructor
Sergio0694 c05d39a
Added BokehBlurParameters struct
Sergio0694 d511f06
Added BokehBlurKernelData struct
Sergio0694 73ee135
Minor code refactoring
Sergio0694 de72bfd
Fixed API change build errors
Sergio0694 93e8c1a
Bug fixes with the pixel premultiplication steps
Sergio0694 2e89d3d
Removed unwanted unpremultiplication pass
Sergio0694 7ec6905
Removed unused using directives
Sergio0694 2c5c85d
Fixed missing using directives in conditional branches
Sergio0694 343da66
Update from latest upstream master
JimBobSquarePants 9304db2
Merge branch 'master' into feature_bokeh-blur-impl
JimBobSquarePants 32d9077
Update Block8x8F.Generated.cs
JimBobSquarePants 512586e
Update GenericBlock8x8.Generated.cs
JimBobSquarePants 0e6259d
Manual checking for files with LF (see gitter)
Sergio0694 f36564c
Removed unused using directive
Sergio0694 9491262
Added IEquatable<ComplexVector4> interface
Sergio0694 9edf46b
Added IEquatable<BokehBlurParameters> interface
Sergio0694 db58163
Moved bokeh blur parameters types
Sergio0694 24cc75a
Added reference to original source code
Sergio0694 0a77f69
Complex convolution methods moved to another class
Sergio0694 04984cd
Switched to MathF in the bokeh blur processor
Sergio0694 a70518b
Switched to Vector4.Clamp
Sergio0694 8aec5e0
Added Buffer2D<T>.Slice API
Sergio0694 3f24cee
Added BokehBlurExecutionMode enum
Sergio0694 06dfb02
Added new bokeh blur processor constructors
Sergio0694 f1a0c37
Added new bokeh blur extension overloads with execution mode
Sergio0694 ebd64f6
Code refactoring in preparation for the execution mode switch
Sergio0694 5fc559a
Implemented execution mode switch in the bokeh processor
Sergio0694 7e7f163
Merge pull request #7 from Sergio0694/feature_bokeh-blur-impl-switch
Sergio0694 9aa75cc
Moved BokehBlurExecutionMode struct
Sergio0694 0869492
Removed unused using directives
Sergio0694 f663adf
Minor code refactoring
Sergio0694 8e9986e
More minor code refactoring
Sergio0694 5d28f6e
Update External
JimBobSquarePants c94948c
Fix undisposed buffers
JimBobSquarePants 11fadaf
Bokeh blur processor cache switched to concurrent dictionary
Sergio0694 871ae09
Minor code refactoring
Sergio0694 efe5322
Merge branch 'master' into feature_bokeh-blur-impl
antonfirsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (c) Six Labors and contributors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System.Numerics; | ||
|
|
||
| namespace SixLabors.ImageSharp.Primitives | ||
| { | ||
| /// <summary> | ||
| /// A <see langword="struct"/> that contains data about a set of bokeh blur kernels | ||
| /// </summary> | ||
| internal readonly struct BokehBlurKernelData | ||
| { | ||
| /// <summary> | ||
| /// The kernel parameters to use for the current set of complex kernels | ||
| /// </summary> | ||
| public readonly Vector4[] Parameters; | ||
|
|
||
| /// <summary> | ||
| /// The scaling factor for the kernel values | ||
| /// </summary> | ||
| public readonly float Scale; | ||
|
|
||
| /// <summary> | ||
| /// The kernel components to apply the bokeh blur effect | ||
| /// </summary> | ||
| public readonly Complex64[][] Kernels; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BokehBlurKernelData"/> struct. | ||
| /// </summary> | ||
| /// <param name="parameters">The kernel parameters</param> | ||
| /// <param name="scale">The kernel scale factor</param> | ||
| /// <param name="kernels">The complex kernel components</param> | ||
| public BokehBlurKernelData(Vector4[] parameters, float scale, Complex64[][] kernels) | ||
| { | ||
| this.Parameters = parameters; | ||
| this.Scale = scale; | ||
| this.Kernels = kernels; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) Six Labors and contributors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| namespace SixLabors.ImageSharp.Primitives | ||
| { | ||
| /// <summary> | ||
| /// A <see langword="struct"/> that contains parameters to apply a bokeh blur filter | ||
| /// </summary> | ||
| internal readonly struct BokehBlurParameters | ||
Sergio0694 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// The size of the convolution kernel to use when applying the bokeh blur | ||
| /// </summary> | ||
| public readonly int Radius; | ||
|
|
||
| /// <summary> | ||
| /// The number of complex components to use to approximate the bokeh kernel | ||
| /// </summary> | ||
| public readonly int Components; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BokehBlurParameters"/> struct. | ||
| /// </summary> | ||
| /// <param name="radius">The size of the kernel</param> | ||
| /// <param name="components">The number of kernel components</param> | ||
| public BokehBlurParameters(int radius, int components) | ||
| { | ||
| this.Radius = radius; | ||
| this.Components = components; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright (c) Six Labors and contributors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace SixLabors.ImageSharp.Primitives | ||
| { | ||
| /// <summary> | ||
| /// Represents a complex number, where the real and imaginary parts are stored as <see cref="float"/> values. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is a more efficient version of the <see cref="Complex64"/> type. | ||
| /// </remarks> | ||
| internal readonly struct Complex64 : IEquatable<Complex64> | ||
| { | ||
| /// <summary> | ||
| /// The real part of the complex number | ||
| /// </summary> | ||
| public readonly float Real; | ||
|
|
||
| /// <summary> | ||
| /// The imaginary part of the complex number | ||
| /// </summary> | ||
| public readonly float Imaginary; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Complex64"/> struct. | ||
| /// </summary> | ||
| /// <param name="real">The real part in the complex number.</param> | ||
| /// <param name="imaginary">The imaginary part in the complex number.</param> | ||
| public Complex64(float real, float imaginary) | ||
| { | ||
| this.Real = real; | ||
| this.Imaginary = imaginary; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs the multiplication operation between a <see cref="Complex64"/> intance and a <see cref="float"/> scalar. | ||
| /// </summary> | ||
| /// <param name="value">The <see cref="Complex64"/> value to multiply.</param> | ||
| /// <param name="scalar">The <see cref="float"/> scalar to use to multiply the <see cref="Complex64"/> value.</param> | ||
| /// <returns>The <see cref="Complex64"/> result</returns> | ||
| [MethodImpl(InliningOptions.ShortMethod)] | ||
| public static Complex64 operator *(Complex64 value, float scalar) => new Complex64(value.Real * scalar, value.Imaginary * scalar); | ||
|
|
||
| /// <summary> | ||
| /// Performs the multiplication operation between a <see cref="Complex64"/> intance and a <see cref="Vector4"/>. | ||
| /// </summary> | ||
| /// <param name="value">The <see cref="Complex64"/> value to multiply.</param> | ||
| /// <param name="vector">The <see cref="Vector4"/> instance to use to multiply the <see cref="Complex64"/> value.</param> | ||
| /// <returns>The <see cref="Complex64"/> result</returns> | ||
| [MethodImpl(InliningOptions.ShortMethod)] | ||
| public static ComplexVector4 operator *(Complex64 value, Vector4 vector) | ||
| { | ||
| return new ComplexVector4 { Real = vector * value.Real, Imaginary = vector * value.Imaginary }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs the multiplication operation between a <see cref="Complex64"/> intance and a <see cref="ComplexVector4"/>. | ||
| /// </summary> | ||
| /// <param name="value">The <see cref="Complex64"/> value to multiply.</param> | ||
| /// <param name="vector">The <see cref="ComplexVector4"/> instance to use to multiply the <see cref="Complex64"/> value.</param> | ||
| /// <returns>The <see cref="Complex64"/> result</returns> | ||
| [MethodImpl(InliningOptions.ShortMethod)] | ||
| public static ComplexVector4 operator *(Complex64 value, ComplexVector4 vector) | ||
| { | ||
| Vector4 real = (value.Real * vector.Real) - (value.Imaginary * vector.Imaginary); | ||
| Vector4 imaginary = (value.Real * vector.Imaginary) + (value.Imaginary * vector.Real); | ||
| return new ComplexVector4 { Real = real, Imaginary = imaginary }; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Equals(Complex64 other) | ||
| { | ||
| return this.Real.Equals(other.Real) && this.Imaginary.Equals(other.Imaginary); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override bool Equals(object obj) => obj is Complex64 other && this.Equals(other); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override int GetHashCode() | ||
| { | ||
| unchecked | ||
| { | ||
| return (this.Real.GetHashCode() * 397) ^ this.Imaginary.GetHashCode(); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string ToString() => $"{this.Real}{(this.Imaginary >= 0 ? "+" : string.Empty)}{this.Imaginary}j"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright (c) Six Labors and contributors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace SixLabors.ImageSharp.Primitives | ||
| { | ||
| /// <summary> | ||
| /// A vector with 4 values of type <see cref="Complex64"/>. | ||
| /// </summary> | ||
| internal struct ComplexVector4 | ||
Sergio0694 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// The real part of the complex vector | ||
| /// </summary> | ||
| public Vector4 Real; | ||
|
|
||
| /// <summary> | ||
| /// The imaginary part of the complex number | ||
| /// </summary> | ||
| public Vector4 Imaginary; | ||
|
|
||
| /// <summary> | ||
| /// Sums the values in the input <see cref="ComplexVector4"/> to the current instance | ||
| /// </summary> | ||
| /// <param name="value">The input <see cref="ComplexVector4"/> to sum</param> | ||
| [MethodImpl(InliningOptions.ShortMethod)] | ||
| public void Sum(in ComplexVector4 value) | ||
| { | ||
| this.Real += value.Real; | ||
| this.Imaginary += value.Imaginary; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs a weighted sum on the current instance according to the given parameters | ||
| /// </summary> | ||
| /// <param name="a">The 'a' parameter, for the real component</param> | ||
| /// <param name="b">The 'b' parameter, for the imaginary component</param> | ||
| /// <returns>The resulting <see cref="Vector4"/> value</returns> | ||
| [MethodImpl(InliningOptions.ShortMethod)] | ||
| public Vector4 WeightedSum(float a, float b) => (this.Real * a) + (this.Imaginary * b); | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
src/ImageSharp/Processing/Extensions/BokehBlurExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright (c) Six Labors and contributors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using SixLabors.ImageSharp.Processing.Processors.Convolution; | ||
| using SixLabors.Primitives; | ||
|
|
||
| namespace SixLabors.ImageSharp.Processing | ||
| { | ||
| /// <summary> | ||
| /// Adds bokeh blurring extensions to the <see cref="Image{TPixel}"/> type. | ||
| /// </summary> | ||
| public static class BokehBlurExtensions | ||
| { | ||
| /// <summary> | ||
| /// Applies a bokeh blur to the image. | ||
| /// </summary> | ||
| /// <param name="source">The image this method extends.</param> | ||
| /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns> | ||
| public static IImageProcessingContext BokehBlur(this IImageProcessingContext source) | ||
| => source.ApplyProcessor(new BokehBlurProcessor()); | ||
|
|
||
| /// <summary> | ||
| /// Applies a bokeh blur to the image. | ||
| /// </summary> | ||
| /// <param name="source">The image this method extends.</param> | ||
| /// <param name="radius">The 'radius' value representing the size of the area to sample.</param> | ||
| /// <param name="components">The 'components' value representing the number of kernels to use to approximate the bokeh effect.</param> | ||
| /// <param name="gamma">The gamma highlight factor to use to emphasize bright spots in the source image</param> | ||
| /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns> | ||
| public static IImageProcessingContext BokehBlur(this IImageProcessingContext source, int radius, int components, float gamma) | ||
| => source.ApplyProcessor(new BokehBlurProcessor(radius, components, gamma)); | ||
|
|
||
| /// <summary> | ||
| /// Applies a bokeh blur to the image. | ||
| /// </summary> | ||
| /// <param name="source">The image this method extends.</param> | ||
| /// <param name="rectangle"> | ||
| /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter. | ||
| /// </param> | ||
| /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns> | ||
| public static IImageProcessingContext BokehBlur(this IImageProcessingContext source, Rectangle rectangle) | ||
| => source.ApplyProcessor(new BokehBlurProcessor(), rectangle); | ||
|
|
||
| /// <summary> | ||
| /// Applies a bokeh blur to the image. | ||
| /// </summary> | ||
| /// <param name="source">The image this method extends.</param> | ||
| /// <param name="radius">The 'radius' value representing the size of the area to sample.</param> | ||
| /// <param name="components">The 'components' value representing the number of kernels to use to approximate the bokeh effect.</param> | ||
| /// <param name="gamma">The gamma highlight factor to use to emphasize bright spots in the source image</param> | ||
| /// <param name="rectangle"> | ||
| /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter. | ||
| /// </param> | ||
| /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns> | ||
| public static IImageProcessingContext BokehBlur(this IImageProcessingContext source, int radius, int components, float gamma, Rectangle rectangle) | ||
| => source.ApplyProcessor(new BokehBlurProcessor(radius, components, gamma), rectangle); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.