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
36 , mode_(video_mode::invalid)
\r
38 , deinterlace_(false)
\r
39 , blend_mode_(image_transform::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_mode(video_mode::type mode)
\r
157 video_mode::type image_transform::get_mode() const
\r
162 void image_transform::set_deinterlace(bool value)
\r
164 deinterlace_ = value;
\r
167 bool image_transform::get_deinterlace() const
\r
169 return deinterlace_;
\r
172 void image_transform::set_blend_mode(image_transform::blend_mode value)
\r
174 blend_mode_ = value;
\r
177 image_transform::blend_mode image_transform::get_blend_mode() const
\r
179 return blend_mode_;
\r
182 image_transform& image_transform::operator*=(const image_transform &other)
\r
184 opacity_ *= other.opacity_;
\r
186 if(other.mode_ != video_mode::invalid)
\r
187 mode_ = other.mode_;
\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
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
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
201 levels_.gamma *= other.levels_.gamma;
\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
216 const image_transform image_transform::operator*(const image_transform &other) const
\r
218 return image_transform(*this) *= other;
\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
224 image_transform tween(double time, const image_transform& source, const image_transform& dest, double duration, const tweener_t& tweener)
\r
226 auto do_tween = [](double time, double source, double dest, double duration, const tweener_t& tweener)
\r
228 return tweener(time, source, dest-source, duration);
\r
231 CASPAR_ASSERT(source.get_mode() == dest.get_mode() || source.get_mode() == video_mode::invalid || dest.get_mode() == video_mode::invalid);
\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
248 auto s_levels = source.get_levels();
\r
249 auto d_levels = dest.get_levels();
\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
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
257 d_levels.gamma = do_tween(time, s_levels.gamma, d_levels.gamma, duration, tweener);
\r
259 result.set_levels(d_levels);
\r
264 image_transform::blend_mode get_blend_mode(const std::wstring& str)
\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
325 return image_transform::normal;
\r