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  * Lanczos resampling
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.resampling.lanczos;
37 
38 import std.math;
39 import dlib.image.image;
40 import dlib.image.color;
41 
42 T lanczos(T) (T x, int filterSize)
43 {
44     if (x <= -filterSize || x >= filterSize)
45         return 0.0; // Outside of the window
46     if (x > -T.epsilon && x < T.epsilon)
47         return 1.0; // Special case the discontinuity at the origin
48 
49     auto sinc = (T x) => sin(PI * x) / (PI * x);
50     return sinc(x) * sinc(x / filterSize);
51 }
52 
53 /// Resize image with Lanczos filter
54 SuperImage resampleLanczos(SuperImage img, in uint newWidth, in uint newHeight)
55 in
56 {
57     assert (img.data.length);
58 }
59 do
60 {
61     SuperImage res = img.createSameFormat(newWidth, newHeight);
62 
63     float xFactor = cast(float)img.width  / cast(float)newWidth;
64     float yFactor = cast(float)img.height / cast(float)newHeight;
65 
66     foreach(x; 0..res.width)
67     {
68         float ox = x * xFactor - 0.5f;
69         int ox1 = cast(int)ox;
70         float dx = ox - ox1;
71 
72         foreach(y; 0..res.height)
73         {
74             float oy = y * yFactor - 0.5f;
75             int oy1 = cast(int)oy;
76             float dy = oy - oy1;
77 
78             Color4f colSum = Color4f(0, 0, 0);
79             float kSum;
80 
81             foreach(kx; -3..4)
82             {
83                 int ix = ox1 + kx;
84 
85                 if (ix < 0) ix = 0;
86                 if (ix >= img.width) ix = img.width - 1;
87 
88                 foreach(ky; -3..4)
89                 {
90                     int iy = oy1 + ky;
91 
92                     if (iy < 0) iy = 0;
93                     if (iy >= img.height) iy = img.height - 1;
94 
95                     auto col = img[ix, iy];
96 
97                     float k1 = lanczos((cast(float)ky - dy), 3);
98                     float k2 = k1 * lanczos((cast(float)kx - dx), 3);
99 
100                     kSum += k2;
101 
102                     colSum += col * k2;
103                 }
104             }
105 
106             if (kSum > 0.0f)
107                 colSum /= kSum;
108 
109             //colSum.clamp(0.0f, 1.0f);
110             res[x, y] = colSum;
111         }
112     }
113 
114     return res;
115 }