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  * Bilinear 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.bilinear;
37 
38 import std.math;
39 import dlib.image.image;
40 import dlib.image.color;
41 
42 /// Resize image with bilinear filter
43 SuperImage resampleBilinear(SuperImage img, in uint newWidth, in uint newHeight)
44 in
45 {
46     assert (img.data.length);
47 }
48 do
49 {
50     SuperImage res = img.createSameFormat(newWidth, newHeight);
51 
52     float xFactor = cast(float)img.width  / cast(float)newWidth;
53     float yFactor = cast(float)img.height / cast(float)newHeight;
54 
55     int floor_x, floor_y, ceil_x, ceil_y;
56     float fraction_x, fraction_y, one_minus_x, one_minus_y;
57 
58     Color4f c1, c2, c3, c4;
59     Color4f col;
60     float b1, b2;
61 
62     foreach(y; 0..res.height)
63     foreach(x; 0..res.width)
64     {
65         floor_x = cast(int)floor(x * xFactor);
66         floor_y = cast(int)floor(y * yFactor);
67 
68         ceil_x = floor_x + 1;
69         if (ceil_x >= img.width)
70             ceil_x = floor_x;
71 
72         ceil_y = floor_y + 1;
73         if (ceil_y >= img.height)
74             ceil_y = floor_y;
75 
76         fraction_x = x * xFactor - floor_x;
77         fraction_y = y * yFactor - floor_y;
78         one_minus_x = 1.0f - fraction_x;
79         one_minus_y = 1.0f - fraction_y;
80 
81         c1 = img[floor_x, floor_y];
82         c2 = img[ceil_x,  floor_y];
83         c3 = img[floor_x, ceil_y];
84         c4 = img[ceil_x,  ceil_y];
85 
86         // Red
87         b1 = one_minus_x * c1.r + fraction_x * c2.r;
88         b2 = one_minus_x * c3.r + fraction_x * c4.r;
89         col.r = one_minus_y * b1 + fraction_y * b2;
90 
91         // Green
92         b1 = one_minus_x * c1.g + fraction_x * c2.g;
93         b2 = one_minus_x * c3.g + fraction_x * c4.g;
94         col.g = one_minus_y * b1 + fraction_y * b2;
95 
96         // Blue
97         b1 = one_minus_x * c1.b + fraction_x * c2.b;
98         b2 = one_minus_x * c3.b + fraction_x * c4.b;
99         col.b = one_minus_y * b1 + fraction_y * b2;
100 
101         // Alpha
102         b1 = one_minus_x * c1.a + fraction_x * c2.a;
103         b2 = one_minus_x * c3.a + fraction_x * c4.a;
104         col.a = one_minus_y * b1 + fraction_y * b2;
105 
106         res[x, y] = col;
107     }
108 
109     return res;
110 }