One class of image digital filters is described by a rectangular matrix of real coefficients called kernel convoluted in a sliding window of image pixels. Usually the kernel is square 




K

k
l




{\displaystyle K_{kl}}

, where k, l are in the range -R,-R+1,..,R-1,R. W=2R+1 is the kernel width. The filter determines the new value of a monochromatic image pixel Pij as a convolution of the image pixels in the window centered in i, j and the kernel values:







P

i
j


=


∑

k
=
−
R


R



∑

l
=
−
R


R



P

i
+
k
 
j
+
l



K

k
l





{\displaystyle P_{ij}=\displaystyle \sum _{k=-R}^{R}\sum _{l=-R}^{R}P_{i+k\ j+l}K_{kl}}




Color images are usually split into the channels which are filtered independently. A color model can be changed as well, i.e. filtration is performed not necessarily in RGB. Common kernels sizes are 3x3 and 5x5. The complexity of filtrating grows quadratically (O(n2)) with the kernel width.

Task: Write a generic convolution 3x3 kernel filter. Optionally show some end user filters that use this generic one.

(You can use, to test the functions below, these input and output solutions.)

import std.string, std.math, std.algorithm, grayscale_image;
 
struct ConvolutionFilter {
    double[][] kernel;
    double divisor, offset_;
    string name;
}
 
 
Image!Color convolve(Color)(in Image!Color im,
                            in ConvolutionFilter filter)
pure nothrow in {
    assert(im !is null);
    assert(!filter.divisor.isNaN && !filter.offset_.isNaN);
    assert(filter.divisor != 0);
    assert(filter.kernel.length > 0 && filter.kernel[0].length > 0);
    foreach (const row; filter.kernel) // Is rectangular.
        assert(row.length == filter.kernel[0].length);
    assert(filter.kernel.length % 2 == 1); // Odd sized kernel.
    assert(filter.kernel[0].length % 2 == 1);
    assert(im.ny >= filter.kernel.length);
    assert(im.nx >= filter.kernel[0].length);
} out(result) {
    assert(result !is null);
    assert(result.nx == im.nx && result.ny == im.ny);
} body {
    immutable knx2 = filter.kernel[0].length / 2;
    immutable kny2 = filter.kernel.length / 2;
    auto io = new Image!Color(im.nx, im.ny);
 
    static if (is(Color == RGB))
        alias CT = typeof(Color.r); // Component type.
    else static if (is(typeof(Color.c)))
        alias CT = typeof(Color.c);
    else
        alias CT = Color;
 
    foreach (immutable y; kny2 .. im.ny - kny2) {
        foreach (immutable x; knx2 .. im.nx - knx2) {
            static if (is(Color == RGB))
                double[3] total = 0.0;
            else
                double total = 0.0;
 
            foreach (immutable sy, const kRow; filter.kernel) {
                foreach (immutable sx, immutable k; kRow) {
                    immutable p = im[x + sx - knx2, y + sy - kny2];
                    static if (is(Color == RGB)) {
                        total[0] += p.r * k;
                        total[1] += p.g * k;
                        total[2] += p.b * k;
                    } else {
                        total += p * k;
                    }
                }
            }
 
            immutable D = filter.divisor;
            immutable O = filter.offset_ * CT.max;
            static if (is(Color == RGB)) {
                io[x, y] = Color(
                    cast(CT)min(max(total[0]/ D + O, CT.min), CT.max),
                    cast(CT)min(max(total[1]/ D + O, CT.min), CT.max),
                    cast(CT)min(max(total[2]/ D + O, CT.min), CT.max));
            } else static if (is(typeof(Color.c))) {
                io[x, y] = Color(
                    cast(CT)min(max(total / D + O, CT.min), CT.max));
            } else {
                // If Color doesn't have a 'c' field, then Color is
                // assumed to be a built-in type.
                io[x, y] =
                    cast(CT)min(max(total / D + O, CT.min), CT.max);
            }
        }
    }
 
    return io;
}
 
 
void main() {
    immutable ConvolutionFilter[] filters = [
        {[[-2.0, -1.0, 0.0],
          [-1.0,  1.0, 1.0],
          [ 0.0,  1.0, 2.0]], divisor:1.0, offset_:0.0, name:"Emboss"},
 
        {[[-1.0, -1.0, -1.0],
          [-1.0,  9.0, -1.0],
          [-1.0, -1.0, -1.0]], divisor:1.0, 0.0, "Sharpen"},
 
        {[[-1.0, -2.0, -1.0],
          [ 0.0,  0.0,  0.0],
          [ 1.0,  2.0,  1.0]], divisor:1.0, 0.5, "Sobel_emboss"},
 
        {[[1.0, 1.0, 1.0],
          [1.0, 1.0, 1.0],
          [1.0, 1.0, 1.0]], divisor:9.0, 0.0, "Box_blur"},
 
        {[[1,  4,  7,  4, 1],
          [4, 16, 26, 16, 4],
          [7, 26, 41, 26, 7],
          [4, 16, 26, 16, 4],
          [1,  4,  7,  4, 1]], divisor:273, 0.0, "Gaussian_blur"}];
 
    Image!RGB im;
    im.loadPPM6("Lenna100.ppm");
 
    foreach (immutable filter; filters)
        im.convolve(filter)
        .savePPM6(format("lenna_%s.ppm", filter.name));
 
    const img = im.rgb2grayImage();
    foreach (immutable filter; filters)
        img.convolve(filter)
        .savePGM(format("lenna_gray_%s.ppm", filter.name));
}