ConvolutionShader_buildKernel top-level property

Function ConvolutionShader_buildKernel
getter/setter pair

Implementation

Function ConvolutionShader_buildKernel = (sigma) {
  // We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.

  Function gauss = (x, sigma) {
    return Math.exp(-(x * x) / (2.0 * sigma * sigma));
  };

  var i, values, sum, halfWidth, kMaxKernelSize = 25;
  int kernelSize = (2 * Math.ceil(sigma * 3.0) + 1).toInt();

  if (kernelSize > kMaxKernelSize) kernelSize = kMaxKernelSize;
  halfWidth = (kernelSize - 1) * 0.5;

  values = new List<num>.filled(kernelSize, 0.0);
  sum = 0.0;
  for (i = 0; i < kernelSize; ++i) {
    values[i] = gauss(i - halfWidth, sigma);
    sum += values[i];
  }

  // normalize the kernel

  for (i = 0; i < kernelSize; ++i) values[i] /= sum;

  return values;
};