title: Rewriting an Android OpenGL filter to Metal (for CIFilter)
body: <p>There are dozens of image filters written for the Android version of our app in GLSL (ES). As of iOS 12, OpenGL is deprecated, and CIFilter kernels have to be written in Metal.</p>
  
  <p>I had some previous background in OpenGL, however writing CIFilter kernels in Metal is new to me.</p>
  
  <p>Here is one of the filters. Could you help me in translating it to Metal as a CIFilter kernel? That would provide a good example for me so I could translate others.</p>
  
  <pre class="lang-c prettyprint-override"><code>#extension GL_OES_EGL_image_external : require
  precision mediump float;
  varying vec2 vTextureCoord;
  uniform samplerExternalOES sTexture;
  uniform float texelWidth;
  uniform float texelHeight;
  uniform float intensivity;
  void main() {
      float SIZE = 1.25 + (intensivity / 100.0)*2.0;
      vec4 color;
      float min = 1.0;
      float max = 0.0;
      float val = 0.0;
      for (float x = -SIZE; x &lt; SIZE; x++) {
          for (float y = -SIZE; y &lt; SIZE; y++) {
              color = texture2D(sTexture, vTextureCoord + vec2(x * texelWidth, y * texelHeight));
              val = (color.r + color.g + color.b) / 3.;
              if (val &gt; max) { max = val; } else if (val &lt; min) { min = val; }
          }
      }
      float range = 5. * (max - min);
      gl_FragColor = vec4(pow(1. - range, SIZE * 1.5));
      gl_FragColor = vec4((gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3. &gt; 0.75 ? vec3(1.) : gl_FragColor.rgb, 1.);
  }
  </code></pre>