2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
20 #include "../../stdafx.h"
\r
22 #include "image_transform.h"
\r
24 #include <common/utility/assert.h>
\r
26 #include <boost/algorithm/string.hpp>
\r
28 namespace caspar { namespace core {
\r
30 image_transform::image_transform()
\r
37 , deinterlace_(false)
\r
38 , blend_mode_(image_transform::blend_mode::normal)
\r
39 , alpha_mode_(image_transform::alpha_mode::normal)
\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
47 void image_transform::set_opacity(double value)
\r
49 opacity_ = std::max(value, 0.0);
\r
52 double image_transform::get_opacity() const
\r
57 void image_transform::set_gain(double value)
\r
59 gain_ = std::max(0.0, value);
\r
62 double image_transform::get_gain() const
\r
67 void image_transform::set_brightness(double value)
\r
69 brightness_ = std::max(0.0, value);
\r
72 double image_transform::get_brightness() const
\r
77 void image_transform::set_contrast(double value)
\r
79 contrast_ = std::max(0.0, value);
\r
82 double image_transform::get_contrast() const
\r
87 void image_transform::set_saturation(double value)
\r
89 saturation_ = std::max(0.0, value);
\r
92 double image_transform::get_saturation() const
\r
97 void image_transform::set_levels(const image_transform::levels& value)
\r
102 image_transform::levels image_transform::get_levels() const
\r
108 void image_transform::set_fill_translation(double x, double y)
\r
110 fill_translation_[0] = x;
\r
111 fill_translation_[1] = y;
\r
114 void image_transform::set_fill_scale(double x, double y)
\r
116 fill_scale_[0] = x;
\r
117 fill_scale_[1] = y;
\r
120 std::array<double, 2> image_transform::get_fill_translation() const
\r
122 return fill_translation_;
\r
125 std::array<double, 2> image_transform::get_fill_scale() const
\r
127 return fill_scale_;
\r
130 void image_transform::set_clip_translation(double x, double y)
\r
132 clip_translation_[0] = x;
\r
133 clip_translation_[1] = y;
\r
136 void image_transform::set_clip_scale(double x, double y)
\r
138 clip_scale_[0] = x;
\r
139 clip_scale_[1] = y;
\r
142 std::array<double, 2> image_transform::get_clip_translation() const
\r
144 return clip_translation_;
\r
147 std::array<double, 2> image_transform::get_clip_scale() const
\r
149 return clip_scale_;
\r
152 void image_transform::set_deinterlace(bool value)
\r
154 deinterlace_ = value;
\r
157 bool image_transform::get_deinterlace() const
\r
159 return deinterlace_;
\r
162 void image_transform::set_blend_mode(image_transform::blend_mode::type value)
\r
164 blend_mode_ = value;
\r
167 image_transform::blend_mode::type image_transform::get_blend_mode() const
\r
169 return blend_mode_;
\r
172 void image_transform::set_alpha_mode(image_transform::alpha_mode::type value)
\r
174 alpha_mode_ = value;
\r
177 image_transform::alpha_mode::type image_transform::get_alpha_mode() const
\r
179 return alpha_mode_;
\r
182 image_transform& image_transform::operator*=(const image_transform &other)
\r
184 opacity_ *= other.opacity_;
\r
185 blend_mode_ = std::max(blend_mode_, other.blend_mode_);
\r
186 alpha_mode_ = std::max(alpha_mode_, other.alpha_mode_);
\r
187 gain_ *= other.gain_;
\r
188 brightness_ *= other.brightness_;
\r
189 contrast_ *= other.contrast_;
\r
190 saturation_ *= other.saturation_;
\r
192 levels_.min_input = std::max(levels_.min_input, other.levels_.min_input);
\r
193 levels_.max_input = std::min(levels_.max_input, other.levels_.max_input);
\r
195 levels_.min_output = std::max(levels_.min_output, other.levels_.min_output);
\r
196 levels_.max_output = std::min(levels_.max_output, other.levels_.max_output);
\r
198 levels_.gamma *= other.levels_.gamma;
\r
200 deinterlace_ |= other.deinterlace_;
\r
201 is_key_ |= other.is_key_;
\r
202 fill_translation_[0] += other.fill_translation_[0]*fill_scale_[0];
\r
203 fill_translation_[1] += other.fill_translation_[1]*fill_scale_[1];
\r
204 fill_scale_[0] *= other.fill_scale_[0];
\r
205 fill_scale_[1] *= other.fill_scale_[1];
\r
206 clip_translation_[0] += other.clip_translation_[0]*clip_scale_[0];
\r
207 clip_translation_[1] += other.clip_translation_[1]*clip_scale_[1];
\r
208 clip_scale_[0] *= other.clip_scale_[0];
\r
209 clip_scale_[1] *= other.clip_scale_[1];
\r
213 const image_transform image_transform::operator*(const image_transform &other) const
\r
215 return image_transform(*this) *= other;
\r
218 void image_transform::set_is_key(bool value){is_key_ = value;}
\r
219 bool image_transform::get_is_key() const{return is_key_;}
\r
221 image_transform tween(double time, const image_transform& source, const image_transform& dest, double duration, const tweener_t& tweener)
\r
223 auto do_tween = [](double time, double source, double dest, double duration, const tweener_t& tweener)
\r
225 return tweener(time, source, dest-source, duration);
\r
228 image_transform result;
\r
229 result.set_blend_mode (std::max(source.get_blend_mode(), dest.get_blend_mode()));
\r
230 result.set_alpha_mode (std::max(source.get_alpha_mode(), dest.get_alpha_mode()));
\r
231 result.set_is_key (source.get_is_key() | dest.get_is_key());
\r
232 result.set_deinterlace (source.get_deinterlace() | dest.get_deinterlace());
\r
233 result.set_gain (do_tween(time, source.get_gain(), dest.get_gain(), duration, tweener));
\r
234 result.set_brightness (do_tween(time, source.get_brightness(), dest.get_brightness(), duration, tweener));
\r
235 result.set_contrast (do_tween(time, source.get_contrast(), dest.get_contrast(), duration, tweener));
\r
236 result.set_saturation (do_tween(time, source.get_saturation(), dest.get_saturation(), duration, tweener));
\r
237 result.set_opacity (do_tween(time, source.get_opacity(), dest.get_opacity(), duration, tweener));
\r
238 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
239 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
240 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
241 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
243 auto s_levels = source.get_levels();
\r
244 auto d_levels = dest.get_levels();
\r
246 d_levels.max_input = do_tween(time, s_levels.max_input, d_levels.max_input, duration, tweener);
\r
247 d_levels.min_input = do_tween(time, s_levels.min_input, d_levels.min_input, duration, tweener);
\r
249 d_levels.max_output = do_tween(time, s_levels.max_output, d_levels.max_output, duration, tweener);
\r
250 d_levels.min_output = do_tween(time, s_levels.min_output, d_levels.min_output, duration, tweener);
\r
252 d_levels.gamma = do_tween(time, s_levels.gamma, d_levels.gamma, duration, tweener);
\r
254 result.set_levels(d_levels);
\r
259 image_transform::blend_mode::type get_blend_mode(const std::wstring& str)
\r
261 if(boost::iequals(str, L"normal"))
\r
262 return image_transform::blend_mode::normal;
\r
263 else if(boost::iequals(str, L"lighten"))
\r
264 return image_transform::blend_mode::lighten;
\r
265 else if(boost::iequals(str, L"darken"))
\r
266 return image_transform::blend_mode::darken;
\r
267 else if(boost::iequals(str, L"multiply"))
\r
268 return image_transform::blend_mode::multiply;
\r
269 else if(boost::iequals(str, L"average"))
\r
270 return image_transform::blend_mode::average;
\r
271 else if(boost::iequals(str, L"add"))
\r
272 return image_transform::blend_mode::add;
\r
273 else if(boost::iequals(str, L"subtract"))
\r
274 return image_transform::blend_mode::subtract;
\r
275 else if(boost::iequals(str, L"difference"))
\r
276 return image_transform::blend_mode::difference;
\r
277 else if(boost::iequals(str, L"negation"))
\r
278 return image_transform::blend_mode::negation;
\r
279 else if(boost::iequals(str, L"exclusion"))
\r
280 return image_transform::blend_mode::exclusion;
\r
281 else if(boost::iequals(str, L"screen"))
\r
282 return image_transform::blend_mode::screen;
\r
283 else if(boost::iequals(str, L"overlay"))
\r
284 return image_transform::blend_mode::overlay;
\r
285 else if(boost::iequals(str, L"soft_light"))
\r
286 return image_transform::blend_mode::soft_light;
\r
287 else if(boost::iequals(str, L"hard_light"))
\r
288 return image_transform::blend_mode::hard_light;
\r
289 else if(boost::iequals(str, L"color_dodge"))
\r
290 return image_transform::blend_mode::color_dodge;
\r
291 else if(boost::iequals(str, L"color_burn"))
\r
292 return image_transform::blend_mode::color_burn;
\r
293 else if(boost::iequals(str, L"linear_dodge"))
\r
294 return image_transform::blend_mode::linear_dodge;
\r
295 else if(boost::iequals(str, L"linear_burn"))
\r
296 return image_transform::blend_mode::linear_burn;
\r
297 else if(boost::iequals(str, L"linear_light"))
\r
298 return image_transform::blend_mode::linear_light;
\r
299 else if(boost::iequals(str, L"vivid_light"))
\r
300 return image_transform::blend_mode::vivid_light;
\r
301 else if(boost::iequals(str, L"pin_light"))
\r
302 return image_transform::blend_mode::pin_light;
\r
303 else if(boost::iequals(str, L"hard_mix"))
\r
304 return image_transform::blend_mode::hard_mix;
\r
305 else if(boost::iequals(str, L"reflect"))
\r
306 return image_transform::blend_mode::reflect;
\r
307 else if(boost::iequals(str, L"glow"))
\r
308 return image_transform::blend_mode::glow;
\r
309 else if(boost::iequals(str, L"phoenix"))
\r
310 return image_transform::blend_mode::phoenix;
\r
311 else if(boost::iequals(str, L"contrast"))
\r
312 return image_transform::blend_mode::contrast;
\r
313 else if(boost::iequals(str, L"saturation"))
\r
314 return image_transform::blend_mode::saturation;
\r
315 else if(boost::iequals(str, L"color"))
\r
316 return image_transform::blend_mode::color;
\r
317 else if(boost::iequals(str, L"luminosity"))
\r
318 return image_transform::blend_mode::luminosity;
\r
320 return image_transform::blend_mode::normal;
\r
323 image_transform::alpha_mode::type get_alpha_mode(const std::wstring& str)
\r
325 if(boost::iequals(str, L"normal"))
\r
326 return image_transform::alpha_mode::normal;
\r
328 return image_transform::alpha_mode::normal;
\r
331 bool operator<(const image_transform& lhs, const image_transform& rhs)
\r
333 return memcmp(&lhs, &rhs, sizeof(image_transform)) < 0;
\r
336 bool operator==(const image_transform& lhs, const image_transform& rhs)
\r
338 return memcmp(&lhs, &rhs, sizeof(image_transform)) == 0;
\r
341 bool operator!=(const image_transform& lhs, const image_transform& rhs)
\r
343 return !(lhs == rhs);
\r