// OSL sample implementation of gaussian blur for Octane Render shader GaussianBlur( color in = 0 [[string label = "Input texture"]], int iterations = 5 [[int min = 0, int max = MAX_ITERATIONS, string label = "Step count", string description = "Number of steps"]], float stepSize = 0.001 [[float min = 0.00001, float max = 0.01, string label = "Step size", string description = "UV distance between steps"]], output color out = 0) { if (iterations == 0) { // just return the input with the right UV mapping out = _evaluateDelayed(in, u, v); } else { float kernel[MAX_KERNEL_SIZE];
// initialize the kernel float normFactor = 0; for (int i = 0; i <= iterations; ++i) { // use normal probability density function with sigma = 7 float n = 0.05699175 * exp(-0.01020408 * i * i); normFactor += i == 0 ? n : 2*n; kernel[i] = n; }
// average the neighbours color c = 0; for (int i = -iterations; i <= iterations; ++i) { float n = kernel[iterations - abs(i)]; for (int j = -iterations; j <= iterations; ++j) { float f = n * kernel[iterations - abs(j)]; c += f * _evaluateDelayed(in, u + i * stepSize, v + j * stepSize); } }
// calculate the final value out = c/(normFactor*normFactor); } }