1 /*
2 Copyright (c) 2011-2021 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Image convolution
31  *
32  * Copyright: Timur Gafarov 2011-2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Timur Gafarov
35  */
36 module dlib.image.filters.convolution;
37 
38 import std.algorithm;
39 import dlib.image.image;
40 import dlib.image.color;
41 
42 /// Convolve an image with a kernel
43 SuperImage convolve(SuperImage img,
44                     SuperImage outp,
45                     float[] kernel,
46                     uint kw = 3,
47                     uint kh = 3,
48                     float divisor = 1.0f,
49                     float offset = 0.5f,
50                     bool normalize = true,
51                     bool useAlpha = true)
52 in
53 {
54     assert(img.data.length);
55     assert(kernel.length == kw * kh);
56 }
57 do
58 {
59     SuperImage res;
60     if (outp)
61         res = outp;
62     else
63         res = img.dup;
64 
65     float kernelSum = reduce!((a,b) => a + b)(kernel);
66 
67     foreach(y; 0..img.height)
68     foreach(x; 0..img.width)
69     {
70         float alpha = Color4f(img[x, y]).a;
71 
72         Color4f csum = Color4f(0, 0, 0);
73 
74         foreach(ky; 0..kh)
75         foreach(kx; 0..kw)
76         {
77             int iy = y + (ky - kh/2);
78             int ix = x + (kx - kw/2);
79 
80             // Extend
81             if (ix < 0) ix = 0;
82             if (ix >= img.width) ix = img.width - 1;
83             if (iy < 0) iy = 0;
84             if (iy >= img.height) iy = img.height - 1;
85 
86             // TODO:
87             // Wrap
88 
89             auto pix = Color4f(img[ix, iy]);
90             auto k = kernel[kx + ky * kw];
91 
92             csum += pix * k;
93         }
94 
95         if (normalize)
96         {
97             offset = 0.0f;
98             divisor = kernelSum;
99 
100             if (divisor == 0.0f)
101             {
102                 divisor = 1.0f;
103                 offset = 0.5f;
104             }
105 
106             if (divisor < 0.0f)
107                 offset = 1.0f;
108         }
109 
110         csum = csum / divisor + offset;
111 
112         if (!useAlpha)
113             csum.a = alpha;
114 
115         res[x,y] = csum;
116     }
117 
118     return res;
119 }
120 
121 /// ditto
122 SuperImage convolve(SuperImage img,
123                     float[] kernel,
124                     uint kw = 3,
125                     uint kh = 3,
126                     float divisor = 1.0f,
127                     float offset = 0.5f,
128                     bool normalize = true,
129                     bool useAlpha = true)
130 {
131     return convolve(img, null, kernel, kw, kh, divisor, offset, normalize, useAlpha);
132 }
133 
134 /// Various built-in convolution kernels
135 struct Kernel
136 {
137     enum float[]
138 
139     Identity =
140     [
141         0, 0, 0,
142         0, 1, 0,
143         0, 0, 0
144     ],
145 
146     BoxBlur =
147     [
148         1, 1, 1,
149         1, 1, 1,
150         1, 1, 1
151     ],
152 
153     GaussianBlur =
154     [
155         1, 2, 1,
156         2, 4, 2,
157         1, 2, 1
158     ],
159 
160     Sharpen =
161     [
162         -1, -1, -1,
163         -1, 11, -1,
164         -1, -1, -1
165     ],
166 
167     Emboss =
168     [
169        -1, -1,  0,
170        -1,  0,  1,
171         0,  1,  1,
172     ],
173 
174     EdgeEmboss =
175     [
176         -1.0f, -0.5f, -0.0f,
177         -0.5f,  1.0f,  0.5f,
178         -0.0f,  0.5f,  1.0f
179     ],
180 
181     EdgeDetect =
182     [
183         -1, -1, -1,
184         -1,  8, -1,
185         -1, -1, -1,
186     ],
187 
188     Laplace =
189     [
190         0,  1,  0,
191         1, -4,  1,
192         0,  1,  0,
193     ];
194 }