Skip to content

Commit

Permalink
Merge pull request BradLarson#30 from Hyun-je/master
Browse files Browse the repository at this point in the history
Add Median Filter
  • Loading branch information
BradLarson authored Jan 29, 2019
2 parents 70e1092 + e1dcd83 commit bcbea79
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
12 changes: 12 additions & 0 deletions framework/GPUImage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@
7B72DB292103CEB900A91156 /* AddBlend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B72DB272103CEB900A91156 /* AddBlend.swift */; };
7B72DB2B2103CED000A91156 /* AddBlend.metal in Sources */ = {isa = PBXBuildFile; fileRef = 7B72DB2A2103CED000A91156 /* AddBlend.metal */; };
7B72DB2C2103CED000A91156 /* AddBlend.metal in Sources */ = {isa = PBXBuildFile; fileRef = 7B72DB2A2103CED000A91156 /* AddBlend.metal */; };
96F24FEF22007D6D0042E78D /* MedianFilter.metal in Sources */ = {isa = PBXBuildFile; fileRef = 96F24FED22007D6C0042E78D /* MedianFilter.metal */; };
96F24FF022007D6D0042E78D /* MedianFilter.metal in Sources */ = {isa = PBXBuildFile; fileRef = 96F24FED22007D6C0042E78D /* MedianFilter.metal */; };
96F24FF122007D6D0042E78D /* MedianFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96F24FEE22007D6C0042E78D /* MedianFilter.swift */; };
96F24FF222007D6D0042E78D /* MedianFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96F24FEE22007D6C0042E78D /* MedianFilter.swift */; };
BC67D91820F815B60046D2C4 /* RenderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79E30E7320F11DD9005FA13C /* RenderView.swift */; };
BC7BA27420F2BD1E006B5F4B /* ShaderUniformSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC7BA27320F2BD1E006B5F4B /* ShaderUniformSettings.swift */; };
BC7BA27720F2C269006B5F4B /* BrightnessAdjustment.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC7BA27620F2C269006B5F4B /* BrightnessAdjustment.swift */; };
Expand Down Expand Up @@ -461,6 +465,8 @@
7B25C0EB2103D5E9000EC621 /* ColorBlend.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; name = ColorBlend.metal; path = Source/Operations/ColorBlend.metal; sourceTree = "<group>"; };
7B72DB272103CEB900A91156 /* AddBlend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AddBlend.swift; path = Source/Operations/AddBlend.swift; sourceTree = "<group>"; };
7B72DB2A2103CED000A91156 /* AddBlend.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; name = AddBlend.metal; path = Source/Operations/AddBlend.metal; sourceTree = "<group>"; };
96F24FED22007D6C0042E78D /* MedianFilter.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; name = MedianFilter.metal; path = Source/Operations/MedianFilter.metal; sourceTree = "<group>"; };
96F24FEE22007D6C0042E78D /* MedianFilter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MedianFilter.swift; path = Source/Operations/MedianFilter.swift; sourceTree = "<group>"; };
BC7BA27320F2BD1E006B5F4B /* ShaderUniformSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ShaderUniformSettings.swift; path = Source/ShaderUniformSettings.swift; sourceTree = "<group>"; };
BC7BA27620F2C269006B5F4B /* BrightnessAdjustment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = BrightnessAdjustment.swift; path = Source/Operations/BrightnessAdjustment.swift; sourceTree = "<group>"; };
BC7BA27820F2C2CF006B5F4B /* BrightnessAdjustment.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; name = BrightnessAdjustment.metal; path = Source/Operations/BrightnessAdjustment.metal; sourceTree = "<group>"; };
Expand Down Expand Up @@ -646,6 +652,8 @@
795ECAC221EF961F000EF927 /* LocalBinaryPattern.metal */,
795ECAC521EF97F8000EF927 /* ColorLocalBinaryPattern.swift */,
795ECAC821EF9806000EF927 /* ColorLocalBinaryPattern.metal */,
96F24FED22007D6C0042E78D /* MedianFilter.metal */,
96F24FEE22007D6C0042E78D /* MedianFilter.swift */,
);
name = "Image processing";
sourceTree = "<group>";
Expand Down Expand Up @@ -924,6 +932,7 @@
79CB6E0821092E330042F87B /* LuminosityBlend.swift in Sources */,
7957438D20FE790E001EAE0A /* LuminanceRangeReduction.swift in Sources */,
79A81C882100E1B700A3B43A /* LevelsAdjustment.swift in Sources */,
96F24FF122007D6D0042E78D /* MedianFilter.swift in Sources */,
795ECABD21EF920B000EF927 /* Laplacian.metal in Sources */,
79DD50C7213450CB004EF308 /* SwirlDistortion.metal in Sources */,
79E30E6620F10F72005FA13C /* BasicOperation.swift in Sources */,
Expand Down Expand Up @@ -1013,6 +1022,7 @@
79CB6DD22108B3F00042F87B /* ColorDodgeBlend.swift in Sources */,
79EB1B6E2125C7670049135E /* LookupFilter.metal in Sources */,
795ECA7D21E9095A000EF927 /* ZoomBlur.metal in Sources */,
96F24FEF22007D6D0042E78D /* MedianFilter.metal in Sources */,
BC7BA28F20F5A275006B5F4B /* PictureInput.swift in Sources */,
79CB6DDE2108C9160042F87B /* DifferenceBlend.swift in Sources */,
BC9294A421051346006C8E93 /* ColorMatrixFilter.metal in Sources */,
Expand Down Expand Up @@ -1083,6 +1093,7 @@
79CB6E0921092E330042F87B /* LuminosityBlend.swift in Sources */,
7957438E20FE790E001EAE0A /* LuminanceRangeReduction.swift in Sources */,
79A81C892100E1B700A3B43A /* LevelsAdjustment.swift in Sources */,
96F24FF222007D6D0042E78D /* MedianFilter.swift in Sources */,
795ECABE21EF920B000EF927 /* Laplacian.metal in Sources */,
79DD50C8213450CB004EF308 /* SwirlDistortion.metal in Sources */,
793D932620F66303008A7A6E /* Luminance.metal in Sources */,
Expand Down Expand Up @@ -1172,6 +1183,7 @@
79CB6DD32108B3F00042F87B /* ColorDodgeBlend.swift in Sources */,
79EB1B6F2125C7670049135E /* LookupFilter.metal in Sources */,
795ECA7E21E9095A000EF927 /* ZoomBlur.metal in Sources */,
96F24FF022007D6D0042E78D /* MedianFilter.metal in Sources */,
795B05D920F270FF00D1E759 /* MetalRendering.swift in Sources */,
79CB6DDF2108C9160042F87B /* DifferenceBlend.swift in Sources */,
BC9294A521051347006C8E93 /* ColorMatrixFilter.metal in Sources */,
Expand Down
109 changes: 109 additions & 0 deletions framework/Source/Operations/MedianFilter.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <metal_stdlib>
#include "TexelSamplingTypes.h"

using namespace metal;

#define s2(a, b) temp = a; a = min(a, b); b = max(temp, b);
#define mn3(a, b, c) s2(a, b); s2(a, c);
#define mx3(a, b, c) s2(b, c); s2(a, c);

#define mnmx3(a, b, c) mx3(a, b, c); s2(a, b); // 3 exchanges
#define mnmx4(a, b, c, d) s2(a, b); s2(c, d); s2(a, c); s2(b, d); // 4 exchanges
#define mnmx5(a, b, c, d, e) s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e); // 6 exchanges
#define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges



fragment half4 medianFilter(NearbyTexelVertexIO fragmentInput [[stage_in]],
texture2d<half> inputTexture [[texture(0)]]) {

constexpr sampler quadSampler(coord::pixel);

half3 v[6];

v[0] = inputTexture.sample(quadSampler, fragmentInput.bottomLeftTextureCoordinate).rgb;
v[1] = inputTexture.sample(quadSampler, fragmentInput.topRightTextureCoordinate).rgb;
v[2] = inputTexture.sample(quadSampler, fragmentInput.topLeftTextureCoordinate).rgb;
v[3] = inputTexture.sample(quadSampler, fragmentInput.bottomRightTextureCoordinate).rgb;
v[4] = inputTexture.sample(quadSampler, fragmentInput.leftTextureCoordinate).rgb;
v[5] = inputTexture.sample(quadSampler, fragmentInput.rightTextureCoordinate).rgb;


half3 temp;

mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]);

v[5] = inputTexture.sample(quadSampler, fragmentInput.bottomTextureCoordinate).rgb;

mnmx5(v[1], v[2], v[3], v[4], v[5]);

v[5] = inputTexture.sample(quadSampler, fragmentInput.topTextureCoordinate).rgb;

mnmx4(v[2], v[3], v[4], v[5]);

v[5] = inputTexture.sample(quadSampler, fragmentInput.textureCoordinate).rgb;

mnmx3(v[3], v[4], v[5]);

return half4(v[4], 1.0);

}


/*
varying vec2 textureCoordinate;
varying vec2 leftTextureCoordinate;
varying vec2 rightTextureCoordinate;
varying vec2 topTextureCoordinate;
varying vec2 topLeftTextureCoordinate;
varying vec2 topRightTextureCoordinate;
varying vec2 bottomTextureCoordinate;
varying vec2 bottomLeftTextureCoordinate;
varying vec2 bottomRightTextureCoordinate;
uniform sampler2D inputImageTexture;
#define s2(a, b) temp = a; a = min(a, b); b = max(temp, b);
#define mn3(a, b, c) s2(a, b); s2(a, c);
#define mx3(a, b, c) s2(b, c); s2(a, c);
#define mnmx3(a, b, c) mx3(a, b, c); s2(a, b); // 3 exchanges
#define mnmx4(a, b, c, d) s2(a, b); s2(c, d); s2(a, c); s2(b, d); // 4 exchanges
#define mnmx5(a, b, c, d, e) s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e); // 6 exchanges
#define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges
void main()
{
vec3 v[6];
v[0] = texture2D(inputImageTexture, bottomLeftTextureCoordinate).rgb;
v[1] = texture2D(inputImageTexture, topRightTextureCoordinate).rgb;
v[2] = texture2D(inputImageTexture, topLeftTextureCoordinate).rgb;
v[3] = texture2D(inputImageTexture, bottomRightTextureCoordinate).rgb;
v[4] = texture2D(inputImageTexture, leftTextureCoordinate).rgb;
v[5] = texture2D(inputImageTexture, rightTextureCoordinate).rgb;
// v[6] = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;
// v[7] = texture2D(inputImageTexture, topTextureCoordinate).rgb;
vec3 temp;
mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]);
v[5] = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;
mnmx5(v[1], v[2], v[3], v[4], v[5]);
v[5] = texture2D(inputImageTexture, topTextureCoordinate).rgb;
mnmx4(v[2], v[3], v[4], v[5]);
v[5] = texture2D(inputImageTexture, textureCoordinate).rgb;
mnmx3(v[3], v[4], v[5]);
gl_FragColor = vec4(v[4], 1.0);
}
*/
5 changes: 5 additions & 0 deletions framework/Source/Operations/MedianFilter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class MedianFilter: TextureSamplingOperation {
public init() {
super.init(fragmentFunctionName:"medianFilter")
}
}

0 comments on commit bcbea79

Please sign in to comment.