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 , blend_mode_(image_transform::blend_mode::normal)
\r
39 std::fill(fill_translation_.begin(), fill_translation_.end(), 0.0);
\r
40 std::fill(fill_scale_.begin(), fill_scale_.end(), 1.0);
\r
41 std::fill(clip_translation_.begin(), clip_translation_.end(), 0.0);
\r
42 std::fill(clip_scale_.begin(), clip_scale_.end(), 1.0);
\r
45 void image_transform::set_opacity(double value)
\r
47 opacity_ = std::max(value, 0.0);
\r
50 double image_transform::get_opacity() const
\r
55 void image_transform::set_gain(double value)
\r
57 gain_ = std::max(0.0, value);
\r
60 double image_transform::get_gain() const
\r
65 void image_transform::set_brightness(double value)
\r
67 brightness_ = std::max(0.0, value);
\r
70 double image_transform::get_brightness() const
\r
75 void image_transform::set_contrast(double value)
\r
77 contrast_ = std::max(0.0, value);
\r
80 double image_transform::get_contrast() const
\r
85 void image_transform::set_saturation(double value)
\r
87 saturation_ = std::max(0.0, value);
\r
90 double image_transform::get_saturation() const
\r
95 void image_transform::set_levels(const image_transform::levels& value)
\r
100 image_transform::levels image_transform::get_levels() const
\r
106 void image_transform::set_fill_translation(double x, double y)
\r
108 fill_translation_[0] = x;
\r
109 fill_translation_[1] = y;
\r
112 void image_transform::set_fill_scale(double x, double y)
\r
114 fill_scale_[0] = x;
\r
115 fill_scale_[1] = y;
\r
118 std::array<double, 2> image_transform::get_fill_translation() const
\r
120 return fill_translation_;
\r
123 std::array<double, 2> image_transform::get_fill_scale() const
\r
125 return fill_scale_;
\r
128 void image_transform::set_clip_translation(double x, double y)
\r
130 clip_translation_[0] = x;
\r
131 clip_translation_[1] = y;
\r
134 void image_transform::set_clip_scale(double x, double y)
\r
136 clip_scale_[0] = x;
\r
137 clip_scale_[1] = y;
\r
140 std::array<double, 2> image_transform::get_clip_translation() const
\r
142 return clip_translation_;
\r
145 std::array<double, 2> image_transform::get_clip_scale() const
\r
147 return clip_scale_;
\r
150 void image_transform::set_blend_mode(image_transform::blend_mode::type value)
\r
152 blend_mode_ = value;
\r
155 image_transform::blend_mode::type image_transform::get_blend_mode() const
\r
157 return blend_mode_;
\r
160 image_transform& image_transform::operator*=(const image_transform &other)
\r
162 opacity_ *= other.opacity_;
\r
163 blend_mode_ = std::max(blend_mode_, other.blend_mode_);
\r
164 gain_ *= other.gain_;
\r
165 brightness_ *= other.brightness_;
\r
166 contrast_ *= other.contrast_;
\r
167 saturation_ *= other.saturation_;
\r
169 levels_.min_input = std::max(levels_.min_input, other.levels_.min_input);
\r
170 levels_.max_input = std::min(levels_.max_input, other.levels_.max_input);
\r
172 levels_.min_output = std::max(levels_.min_output, other.levels_.min_output);
\r
173 levels_.max_output = std::min(levels_.max_output, other.levels_.max_output);
\r
175 levels_.gamma *= other.levels_.gamma;
\r
177 is_key_ |= other.is_key_;
\r
178 fill_translation_[0] += other.fill_translation_[0]*fill_scale_[0];
\r
179 fill_translation_[1] += other.fill_translation_[1]*fill_scale_[1];
\r
180 fill_scale_[0] *= other.fill_scale_[0];
\r
181 fill_scale_[1] *= other.fill_scale_[1];
\r
182 clip_translation_[0] += other.clip_translation_[0]*clip_scale_[0];
\r
183 clip_translation_[1] += other.clip_translation_[1]*clip_scale_[1];
\r
184 clip_scale_[0] *= other.clip_scale_[0];
\r
185 clip_scale_[1] *= other.clip_scale_[1];
\r
189 const image_transform image_transform::operator*(const image_transform &other) const
\r
191 return image_transform(*this) *= other;
\r
194 void image_transform::set_is_key(bool value){is_key_ = value;}
\r
195 bool image_transform::get_is_key() const{return is_key_;}
\r
197 image_transform tween(double time, const image_transform& source, const image_transform& dest, double duration, const tweener_t& tweener)
\r
199 auto do_tween = [](double time, double source, double dest, double duration, const tweener_t& tweener)
\r
201 return tweener(time, source, dest-source, duration);
\r
204 image_transform result;
\r
205 result.set_blend_mode (std::max(source.get_blend_mode(), dest.get_blend_mode()));
\r
206 result.set_is_key (source.get_is_key() | dest.get_is_key());
\r
207 result.set_gain (do_tween(time, source.get_gain(), dest.get_gain(), duration, tweener));
\r
208 result.set_brightness (do_tween(time, source.get_brightness(), dest.get_brightness(), duration, tweener));
\r
209 result.set_contrast (do_tween(time, source.get_contrast(), dest.get_contrast(), duration, tweener));
\r
210 result.set_saturation (do_tween(time, source.get_saturation(), dest.get_saturation(), duration, tweener));
\r
211 result.set_opacity (do_tween(time, source.get_opacity(), dest.get_opacity(), duration, tweener));
\r
212 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
213 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
214 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
215 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
217 auto s_levels = source.get_levels();
\r
218 auto d_levels = dest.get_levels();
\r
220 d_levels.max_input = do_tween(time, s_levels.max_input, d_levels.max_input, duration, tweener);
\r
221 d_levels.min_input = do_tween(time, s_levels.min_input, d_levels.min_input, duration, tweener);
\r
223 d_levels.max_output = do_tween(time, s_levels.max_output, d_levels.max_output, duration, tweener);
\r
224 d_levels.min_output = do_tween(time, s_levels.min_output, d_levels.min_output, duration, tweener);
\r
226 d_levels.gamma = do_tween(time, s_levels.gamma, d_levels.gamma, duration, tweener);
\r
228 result.set_levels(d_levels);
\r
233 image_transform::blend_mode::type get_blend_mode(const std::wstring& str)
\r
235 if(boost::iequals(str, L"normal"))
\r
236 return image_transform::blend_mode::normal;
\r
237 else if(boost::iequals(str, L"lighten"))
\r
238 return image_transform::blend_mode::lighten;
\r
239 else if(boost::iequals(str, L"darken"))
\r
240 return image_transform::blend_mode::darken;
\r
241 else if(boost::iequals(str, L"multiply"))
\r
242 return image_transform::blend_mode::multiply;
\r
243 else if(boost::iequals(str, L"average"))
\r
244 return image_transform::blend_mode::average;
\r
245 else if(boost::iequals(str, L"add"))
\r
246 return image_transform::blend_mode::add;
\r
247 else if(boost::iequals(str, L"subtract"))
\r
248 return image_transform::blend_mode::subtract;
\r
249 else if(boost::iequals(str, L"difference"))
\r
250 return image_transform::blend_mode::difference;
\r
251 else if(boost::iequals(str, L"negation"))
\r
252 return image_transform::blend_mode::negation;
\r
253 else if(boost::iequals(str, L"exclusion"))
\r
254 return image_transform::blend_mode::exclusion;
\r
255 else if(boost::iequals(str, L"screen"))
\r
256 return image_transform::blend_mode::screen;
\r
257 else if(boost::iequals(str, L"overlay"))
\r
258 return image_transform::blend_mode::overlay;
\r
259 else if(boost::iequals(str, L"soft_light"))
\r
260 return image_transform::blend_mode::soft_light;
\r
261 else if(boost::iequals(str, L"hard_light"))
\r
262 return image_transform::blend_mode::hard_light;
\r
263 else if(boost::iequals(str, L"color_dodge"))
\r
264 return image_transform::blend_mode::color_dodge;
\r
265 else if(boost::iequals(str, L"color_burn"))
\r
266 return image_transform::blend_mode::color_burn;
\r
267 else if(boost::iequals(str, L"linear_dodge"))
\r
268 return image_transform::blend_mode::linear_dodge;
\r
269 else if(boost::iequals(str, L"linear_burn"))
\r
270 return image_transform::blend_mode::linear_burn;
\r
271 else if(boost::iequals(str, L"linear_light"))
\r
272 return image_transform::blend_mode::linear_light;
\r
273 else if(boost::iequals(str, L"vivid_light"))
\r
274 return image_transform::blend_mode::vivid_light;
\r
275 else if(boost::iequals(str, L"pin_light"))
\r
276 return image_transform::blend_mode::pin_light;
\r
277 else if(boost::iequals(str, L"hard_mix"))
\r
278 return image_transform::blend_mode::hard_mix;
\r
279 else if(boost::iequals(str, L"reflect"))
\r
280 return image_transform::blend_mode::reflect;
\r
281 else if(boost::iequals(str, L"glow"))
\r
282 return image_transform::blend_mode::glow;
\r
283 else if(boost::iequals(str, L"phoenix"))
\r
284 return image_transform::blend_mode::phoenix;
\r
285 else if(boost::iequals(str, L"contrast"))
\r
286 return image_transform::blend_mode::contrast;
\r
287 else if(boost::iequals(str, L"saturation"))
\r
288 return image_transform::blend_mode::saturation;
\r
289 else if(boost::iequals(str, L"color"))
\r
290 return image_transform::blend_mode::color;
\r
291 else if(boost::iequals(str, L"luminosity"))
\r
292 return image_transform::blend_mode::luminosity;
\r
294 return image_transform::blend_mode::normal;
\r
297 bool operator<(const image_transform& lhs, const image_transform& rhs)
\r
299 return memcmp(&lhs, &rhs, sizeof(image_transform)) < 0;
\r
302 bool operator==(const image_transform& lhs, const image_transform& rhs)
\r
304 return memcmp(&lhs, &rhs, sizeof(image_transform)) == 0;
\r
307 bool operator!=(const image_transform& lhs, const image_transform& rhs)
\r
309 return !(lhs == rhs);
\r