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  * Per-pixel image arithmetics
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.arithmetics;
37 
38 import dlib.image.image;
39 import dlib.image.color;
40 
41 /// Add two images
42 SuperImage add(SuperImage a, SuperImage b, SuperImage outp, float t = 1.0f)
43 in
44 {
45     assert(a.width == b.width);
46     assert(a.height == b.height);
47 }
48 do
49 {
50     SuperImage img;
51     if (outp)
52         img = outp;
53     else
54         img = a.dup;
55 
56     foreach(y; 0..img.height)
57     foreach(x; 0..img.width)
58     {
59         Color4f acol = Color4f(a[x, y]);
60         Color4f bcol = Color4f(b[x, y]);
61         Color4f col  = acol + (bcol * t);
62         col.a = acol.a;
63         img[x, y] = col;
64     }
65 
66     return img;
67 }
68 
69 /// ditto
70 SuperImage add(SuperImage a, SuperImage b, float t = 1.0f)
71 {
72     return add(a, b, null, t);
73 }
74 
75 /// Subtract image b from image a
76 SuperImage subtract(SuperImage a, SuperImage b, SuperImage outp, float t = 1.0f)
77 in
78 {
79     assert(a.width == b.width);
80     assert(a.height == b.height);
81 }
82 do
83 {
84     SuperImage img;
85     if (outp)
86         img = outp;
87     else
88         img = a.dup;
89 
90     foreach(y; 0..img.height)
91     foreach(x; 0..img.width)
92     {
93         Color4f acol = Color4f(a[x, y]);
94         Color4f bcol = Color4f(b[x, y]);
95         Color4f col  = acol - (bcol * t);
96         col.a = acol.a;
97         img[x, y] = col;
98     }
99 
100     return img;
101 }
102 
103 /// ditto
104 SuperImage subtract(SuperImage a, SuperImage b, float t = 1.0f)
105 {
106     return subtract(a, b, null, t);
107 }
108 
109 /// Multiply two images
110 SuperImage multiply(SuperImage a, SuperImage b, SuperImage outp, float t = 1.0f)
111 in
112 {
113     assert(a.width == b.width);
114     assert(a.height == b.height);
115 }
116 do
117 {
118     SuperImage img;
119     if (outp)
120         img = outp;
121     else
122         img = a.dup;
123 
124     foreach(y; 0..img.height)
125     foreach(x; 0..img.width)
126     {
127         Color4f acol = Color4f(a[x, y]);
128         Color4f bcol = Color4f(b[x, y]);
129         Color4f col  = acol * (bcol * t);
130         col.a = acol.a;
131         img[x, y] = col;
132     }
133 
134     return img;
135 }
136 
137 /// ditto
138 SuperImage multiply(SuperImage a, SuperImage b, float t = 1.0f)
139 {
140     return multiply(a, b, null, t);
141 }
142 
143 /// Divide two images
144 SuperImage divide(SuperImage a, SuperImage b, SuperImage outp, float t = 1.0f)
145 in
146 {
147     assert(a.width == b.width);
148     assert(a.height == b.height);
149 }
150 do
151 {
152     SuperImage img;
153     if (outp)
154         img = outp;
155     else
156         img = a.dup;
157 
158     foreach(y; 0..img.height)
159     foreach(x; 0..img.width)
160     {
161         Color4f acol = Color4f(a[x, y]);
162         Color4f bcol = Color4f(b[x, y]);
163         Color4f col  = acol / (bcol * t);
164         col.a = acol.a;
165         img[x, y] = col;
166     }
167 
168     return img;
169 }
170 
171 /// ditto
172 SuperImage divide(SuperImage a, SuperImage b, float t = 1.0f)
173 {
174     return divide(a, b, null, t);
175 }
176 
177 /// Invert image
178 SuperImage invert(SuperImage a, SuperImage outp)
179 {
180     SuperImage img;
181     if (outp)
182         img = outp;
183     else
184         img = a.dup;
185 
186     foreach(y; 0..img.height)
187     foreach(x; 0..img.width)
188     {
189         img[x, y] = a[x, y].inverse;
190     }
191 
192     return img;
193 }
194 
195 /// ditto
196 SuperImage invert(SuperImage a)
197 {
198     return invert(a, null);
199 }