]> git.sesse.net Git - casparcg/blob - core/producer/frame/image_transform.cpp
a51c6f00e5bfe831a6b8d104066cd9132a7baa83
[casparcg] / core / producer / frame / image_transform.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "image_transform.h"\r
23 \r
24 #include <common/utility/assert.h>\r
25 \r
26 #include <boost/algorithm/string.hpp>\r
27 \r
28 namespace caspar { namespace core {\r
29                 \r
30 image_transform::image_transform() \r
31         : opacity_(1.0)\r
32         , gain_(1.0)\r
33         , brightness_(1.0)\r
34         , contrast_(1.0)\r
35         , saturation_(1.0)\r
36         , mode_(video_mode::invalid)\r
37         , is_key_(false)\r
38         , deinterlace_(false)\r
39         , blend_mode_(image_transform::normal)\r
40 {\r
41         std::fill(fill_translation_.begin(), fill_translation_.end(), 0.0);\r
42         std::fill(fill_scale_.begin(), fill_scale_.end(), 1.0);\r
43         std::fill(clip_translation_.begin(), clip_translation_.end(), 0.0);\r
44         std::fill(clip_scale_.begin(), clip_scale_.end(), 1.0);\r
45 }\r
46 \r
47 void image_transform::set_opacity(double value)\r
48 {\r
49         opacity_ = std::max(value, 0.0);\r
50 }\r
51 \r
52 double image_transform::get_opacity() const\r
53 {\r
54         return opacity_;\r
55 }\r
56 \r
57 void image_transform::set_gain(double value)\r
58 {\r
59         gain_ = std::max(0.0, value);\r
60 }\r
61 \r
62 double image_transform::get_gain() const\r
63 {\r
64         return gain_;\r
65 }\r
66 \r
67 void image_transform::set_brightness(double value)\r
68 {\r
69         brightness_ = std::max(0.0, value);\r
70 }\r
71 \r
72 double image_transform::get_brightness() const\r
73 {\r
74         return brightness_;\r
75 }\r
76 \r
77 void image_transform::set_contrast(double value)\r
78 {\r
79         contrast_ = std::max(0.0, value);\r
80 }\r
81 \r
82 double image_transform::get_contrast() const\r
83 {\r
84         return contrast_;\r
85 }\r
86 \r
87 void image_transform::set_saturation(double value)\r
88 {\r
89         saturation_ = std::max(0.0, value);\r
90 }\r
91 \r
92 double image_transform::get_saturation() const\r
93 {\r
94         return saturation_;\r
95 }\r
96 \r
97 void image_transform::set_levels(const image_transform::levels& value)\r
98 {\r
99         levels_ = value;\r
100 }\r
101 \r
102 image_transform::levels image_transform::get_levels() const\r
103 {\r
104         return levels_;\r
105 }\r
106 \r
107 \r
108 void image_transform::set_fill_translation(double x, double y)\r
109 {\r
110         fill_translation_[0] = x;\r
111         fill_translation_[1] = y;\r
112 }\r
113 \r
114 void image_transform::set_fill_scale(double x, double y)\r
115 {\r
116         fill_scale_[0] = x;\r
117         fill_scale_[1] = y;     \r
118 }\r
119 \r
120 std::array<double, 2> image_transform::get_fill_translation() const\r
121 {\r
122         return fill_translation_;\r
123 }\r
124 \r
125 std::array<double, 2> image_transform::get_fill_scale() const\r
126 {\r
127         return fill_scale_;\r
128 }\r
129 \r
130 void image_transform::set_clip_translation(double x, double y)\r
131 {\r
132         clip_translation_[0] = x;\r
133         clip_translation_[1] = y;\r
134 }\r
135 \r
136 void image_transform::set_clip_scale(double x, double y)\r
137 {\r
138         clip_scale_[0] = x;\r
139         clip_scale_[1] = y;     \r
140 }\r
141 \r
142 std::array<double, 2> image_transform::get_clip_translation() const\r
143 {\r
144         return clip_translation_;\r
145 }\r
146 \r
147 std::array<double, 2> image_transform::get_clip_scale() const\r
148 {\r
149         return clip_scale_;\r
150 }\r
151 \r
152 void image_transform::set_mode(video_mode::type mode)\r
153 {\r
154         mode_ = mode;\r
155 }\r
156 \r
157 video_mode::type image_transform::get_mode() const\r
158 {\r
159         return mode_;\r
160 }\r
161 \r
162 void image_transform::set_deinterlace(bool value)\r
163 {\r
164         deinterlace_ = value;\r
165 }\r
166 \r
167 bool image_transform::get_deinterlace() const\r
168 {\r
169         return deinterlace_;\r
170 }\r
171 \r
172 void image_transform::set_blend_mode(image_transform::blend_mode value)\r
173 {\r
174         blend_mode_ = value;\r
175 }\r
176 \r
177 image_transform::blend_mode image_transform::get_blend_mode() const\r
178 {\r
179         return blend_mode_;\r
180 }\r
181 \r
182 image_transform& image_transform::operator*=(const image_transform &other)\r
183 {\r
184         opacity_                                *= other.opacity_;\r
185         \r
186         if(other.mode_ != video_mode::invalid)\r
187                 mode_ = other.mode_;\r
188 \r
189         blend_mode_                              = std::max(blend_mode_, other.blend_mode_);\r
190         gain_                                   *= other.gain_;\r
191         brightness_                             *= other.brightness_;\r
192         contrast_                               *= other.contrast_;\r
193         saturation_                             *= other.saturation_;\r
194 \r
195         levels_.min_input               = std::max(levels_.min_input, other.levels_.min_input);\r
196         levels_.max_input               = std::min(levels_.max_input, other.levels_.max_input);\r
197         \r
198         levels_.min_output              = std::max(levels_.min_output, other.levels_.min_output);\r
199         levels_.max_output              = std::min(levels_.max_output, other.levels_.max_output);\r
200 \r
201         levels_.gamma                   *= other.levels_.gamma;\r
202 \r
203         deinterlace_                    |= other.deinterlace_;\r
204         is_key_                                 |= other.is_key_;\r
205         fill_translation_[0]    += other.fill_translation_[0]*fill_scale_[0];\r
206         fill_translation_[1]    += other.fill_translation_[1]*fill_scale_[1];\r
207         fill_scale_[0]                  *= other.fill_scale_[0];\r
208         fill_scale_[1]                  *= other.fill_scale_[1];\r
209         clip_translation_[0]    += other.clip_translation_[0]*clip_scale_[0];\r
210         clip_translation_[1]    += other.clip_translation_[1]*clip_scale_[1];\r
211         clip_scale_[0]                  *= other.clip_scale_[0];\r
212         clip_scale_[1]                  *= other.clip_scale_[1];\r
213         return *this;\r
214 }\r
215 \r
216 const image_transform image_transform::operator*(const image_transform &other) const\r
217 {\r
218         return image_transform(*this) *= other;\r
219 }\r
220 \r
221 void image_transform::set_is_key(bool value){is_key_ = value;}\r
222 bool image_transform::get_is_key() const{return is_key_;}\r
223 \r
224 image_transform tween(double time, const image_transform& source, const image_transform& dest, double duration, const tweener_t& tweener)\r
225 {       \r
226         auto do_tween = [](double time, double source, double dest, double duration, const tweener_t& tweener)\r
227         {\r
228                 return tweener(time, source, dest-source, duration);\r
229         };\r
230 \r
231         CASPAR_ASSERT(source.get_mode() == dest.get_mode() || source.get_mode() == video_mode::invalid || dest.get_mode() == video_mode::invalid);\r
232 \r
233         image_transform result; \r
234         result.set_mode                         (dest.get_mode() != video_mode::invalid ? dest.get_mode() : source.get_mode());\r
235         result.set_blend_mode           (std::max(source.get_blend_mode(), dest.get_blend_mode()));\r
236         result.set_is_key                       (source.get_is_key() | dest.get_is_key());\r
237         result.set_deinterlace          (source.get_deinterlace() | dest.get_deinterlace());\r
238         result.set_gain                         (do_tween(time, source.get_gain(), dest.get_gain(), duration, tweener));\r
239         result.set_brightness           (do_tween(time, source.get_brightness(), dest.get_brightness(), duration, tweener));\r
240         result.set_contrast                     (do_tween(time, source.get_contrast(), dest.get_contrast(), duration, tweener));\r
241         result.set_saturation           (do_tween(time, source.get_saturation(), dest.get_saturation(), duration, tweener));\r
242         result.set_opacity                      (do_tween(time, source.get_opacity(), dest.get_opacity(), duration, tweener));\r
243         result.set_fill_translation     (do_tween(time, source.get_fill_translation()[0], dest.get_fill_translation()[0], duration, tweener), do_tween(time, source.get_fill_translation()[1], dest.get_fill_translation()[1], duration, tweener));\r
244         result.set_fill_scale           (do_tween(time, source.get_fill_scale()[0], dest.get_fill_scale()[0], duration, tweener), do_tween(time, source.get_fill_scale()[1], dest.get_fill_scale()[1], duration, tweener));\r
245         result.set_clip_translation     (do_tween(time, source.get_clip_translation()[0], dest.get_clip_translation()[0], duration, tweener), do_tween(time, source.get_clip_translation()[1], dest.get_clip_translation()[1], duration, tweener));\r
246         result.set_clip_scale           (do_tween(time, source.get_clip_scale()[0], dest.get_clip_scale()[0], duration, tweener), do_tween(time, source.get_clip_scale()[1], dest.get_clip_scale()[1], duration, tweener));\r
247         \r
248         auto s_levels = source.get_levels();\r
249         auto d_levels = dest.get_levels();\r
250 \r
251         d_levels.max_input = do_tween(time, s_levels.max_input, d_levels.max_input, duration, tweener);\r
252         d_levels.min_input = do_tween(time, s_levels.min_input, d_levels.min_input, duration, tweener);\r
253         \r
254         d_levels.max_output = do_tween(time, s_levels.max_output, d_levels.max_output, duration, tweener);\r
255         d_levels.min_output = do_tween(time, s_levels.min_output, d_levels.min_output, duration, tweener);\r
256 \r
257         d_levels.gamma = do_tween(time, s_levels.gamma, d_levels.gamma, duration, tweener);\r
258 \r
259         result.set_levels(d_levels);\r
260 \r
261         return result;\r
262 }\r
263 \r
264 image_transform::blend_mode get_blend_mode(const std::wstring& str)\r
265 {\r
266         if(boost::iequals(str, L"normal"))\r
267                 return image_transform::normal;\r
268         else if(boost::iequals(str, L"lighten"))\r
269                 return image_transform::lighten;\r
270         else if(boost::iequals(str, L"darken"))\r
271                 return image_transform::darken;\r
272         else if(boost::iequals(str, L"multiply"))\r
273                 return image_transform::multiply;\r
274         else if(boost::iequals(str, L"average"))\r
275                 return image_transform::average;\r
276         else if(boost::iequals(str, L"add"))\r
277                 return image_transform::add;\r
278         else if(boost::iequals(str, L"subtract"))\r
279                 return image_transform::subtract;\r
280         else if(boost::iequals(str, L"difference"))\r
281                 return image_transform::difference;\r
282         else if(boost::iequals(str, L"negation"))\r
283                 return image_transform::negation;\r
284         else if(boost::iequals(str, L"exclusion"))\r
285                 return image_transform::exclusion;\r
286         else if(boost::iequals(str, L"screen"))\r
287                 return image_transform::screen;\r
288         else if(boost::iequals(str, L"overlay"))\r
289                 return image_transform::overlay;\r
290         else if(boost::iequals(str, L"soft_light"))\r
291                 return image_transform::soft_light;\r
292         else if(boost::iequals(str, L"hard_light"))\r
293                 return image_transform::hard_light;\r
294         else if(boost::iequals(str, L"color_dodge"))\r
295                 return image_transform::color_dodge;\r
296         else if(boost::iequals(str, L"color_burn"))\r
297                 return image_transform::color_burn;\r
298         else if(boost::iequals(str, L"linear_dodge"))\r
299                 return image_transform::linear_dodge;\r
300         else if(boost::iequals(str, L"linear_burn"))\r
301                 return image_transform::linear_burn;\r
302         else if(boost::iequals(str, L"linear_light"))\r
303                 return image_transform::linear_light;\r
304         else if(boost::iequals(str, L"vivid_light"))\r
305                 return image_transform::vivid_light;\r
306         else if(boost::iequals(str, L"pin_light"))\r
307                 return image_transform::pin_light;\r
308         else if(boost::iequals(str, L"hard_mix"))\r
309                 return image_transform::hard_mix;\r
310         else if(boost::iequals(str, L"reflect"))\r
311                 return image_transform::reflect;\r
312         else if(boost::iequals(str, L"glow"))\r
313                 return image_transform::glow;\r
314         else if(boost::iequals(str, L"phoenix"))\r
315                 return image_transform::phoenix;\r
316         else if(boost::iequals(str, L"contrast"))\r
317                 return image_transform::contrast;\r
318         else if(boost::iequals(str, L"saturation"))\r
319                 return image_transform::saturation;\r
320         else if(boost::iequals(str, L"color"))\r
321                 return image_transform::color;\r
322         else if(boost::iequals(str, L"luminosity"))\r
323                 return image_transform::luminosity;\r
324                 \r
325         return image_transform::normal;\r
326 }\r
327 \r
328 }}