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 // TODO: Move layer specific stuff out of frame related classes.
\r
21 #include "../../stdafx.h"
\r
23 #include "image_transform.h"
\r
25 #include <common/utility/assert.h>
\r
27 namespace caspar { namespace core {
\r
29 image_transform::image_transform()
\r
35 , field_mode_(field_mode::progressive)
\r
37 std::fill(fill_translation_.begin(), fill_translation_.end(), 0.0);
\r
38 std::fill(fill_scale_.begin(), fill_scale_.end(), 1.0);
\r
39 std::fill(clip_translation_.begin(), clip_translation_.end(), 0.0);
\r
40 std::fill(clip_scale_.begin(), clip_scale_.end(), 1.0);
\r
43 void image_transform::set_opacity(double value)
\r
45 opacity_ = std::max(value, 0.0);
\r
48 double image_transform::get_opacity() const
\r
53 void image_transform::set_brightness(double value)
\r
55 brightness_ = std::max(0.0, value);
\r
58 double image_transform::get_brightness() const
\r
63 void image_transform::set_contrast(double value)
\r
65 contrast_ = std::max(0.0, value);
\r
68 double image_transform::get_contrast() const
\r
73 void image_transform::set_saturation(double value)
\r
75 saturation_ = std::max(0.0, value);
\r
78 double image_transform::get_saturation() const
\r
83 void image_transform::set_levels(const image_transform::levels& value)
\r
88 image_transform::levels image_transform::get_levels() const
\r
94 void image_transform::set_fill_translation(double x, double y)
\r
96 fill_translation_[0] = x;
\r
97 fill_translation_[1] = y;
\r
100 void image_transform::set_fill_scale(double x, double y)
\r
102 fill_scale_[0] = x;
\r
103 fill_scale_[1] = y;
\r
106 std::array<double, 2> image_transform::get_fill_translation() const
\r
108 return fill_translation_;
\r
111 std::array<double, 2> image_transform::get_fill_scale() const
\r
113 return fill_scale_;
\r
116 void image_transform::set_clip_translation(double x, double y)
\r
118 clip_translation_[0] = x;
\r
119 clip_translation_[1] = y;
\r
122 void image_transform::set_clip_scale(double x, double y)
\r
124 clip_scale_[0] = x;
\r
125 clip_scale_[1] = y;
\r
128 std::array<double, 2> image_transform::get_clip_translation() const
\r
130 return clip_translation_;
\r
133 std::array<double, 2> image_transform::get_clip_scale() const
\r
135 return clip_scale_;
\r
138 void image_transform::set_field_mode(field_mode::type field_mode)
\r
140 field_mode_ = field_mode;
\r
143 field_mode::type image_transform::get_field_mode() const
\r
145 return field_mode_;
\r
148 image_transform& image_transform::operator*=(const image_transform &other)
\r
150 opacity_ *= other.opacity_;
\r
151 brightness_ *= other.brightness_;
\r
152 contrast_ *= other.contrast_;
\r
153 saturation_ *= other.saturation_;
\r
155 levels_.min_input = std::max(levels_.min_input, other.levels_.min_input);
\r
156 levels_.max_input = std::min(levels_.max_input, other.levels_.max_input);
\r
158 levels_.min_output = std::max(levels_.min_output, other.levels_.min_output);
\r
159 levels_.max_output = std::min(levels_.max_output, other.levels_.max_output);
\r
161 levels_.gamma *= other.levels_.gamma;
\r
163 field_mode_ = static_cast<field_mode::type>(field_mode_ & other.field_mode_);
\r
164 is_key_ |= other.is_key_;
\r
165 fill_translation_[0] += other.fill_translation_[0]*fill_scale_[0];
\r
166 fill_translation_[1] += other.fill_translation_[1]*fill_scale_[1];
\r
167 fill_scale_[0] *= other.fill_scale_[0];
\r
168 fill_scale_[1] *= other.fill_scale_[1];
\r
169 clip_translation_[0] += other.clip_translation_[0]*clip_scale_[0];
\r
170 clip_translation_[1] += other.clip_translation_[1]*clip_scale_[1];
\r
171 clip_scale_[0] *= other.clip_scale_[0];
\r
172 clip_scale_[1] *= other.clip_scale_[1];
\r
176 const image_transform image_transform::operator*(const image_transform &other) const
\r
178 return image_transform(*this) *= other;
\r
181 void image_transform::set_is_key(bool value){is_key_ = value;}
\r
182 bool image_transform::get_is_key() const{return is_key_;}
\r
184 image_transform tween(double time, const image_transform& source, const image_transform& dest, double duration, const tweener_t& tweener)
\r
186 auto do_tween = [](double time, double source, double dest, double duration, const tweener_t& tweener)
\r
188 return tweener(time, source, dest-source, duration);
\r
191 image_transform result;
\r
192 result.set_is_key (source.get_is_key() | dest.get_is_key());
\r
193 result.set_field_mode (static_cast<field_mode::type>(source.get_field_mode() & dest.get_field_mode()));
\r
194 result.set_brightness (do_tween(time, source.get_brightness(), dest.get_brightness(), duration, tweener));
\r
195 result.set_contrast (do_tween(time, source.get_contrast(), dest.get_contrast(), duration, tweener));
\r
196 result.set_saturation (do_tween(time, source.get_saturation(), dest.get_saturation(), duration, tweener));
\r
197 result.set_opacity (do_tween(time, source.get_opacity(), dest.get_opacity(), duration, tweener));
\r
198 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
199 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
200 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
201 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
203 auto s_levels = source.get_levels();
\r
204 auto d_levels = dest.get_levels();
\r
206 d_levels.max_input = do_tween(time, s_levels.max_input, d_levels.max_input, duration, tweener);
\r
207 d_levels.min_input = do_tween(time, s_levels.min_input, d_levels.min_input, duration, tweener);
\r
209 d_levels.max_output = do_tween(time, s_levels.max_output, d_levels.max_output, duration, tweener);
\r
210 d_levels.min_output = do_tween(time, s_levels.min_output, d_levels.min_output, duration, tweener);
\r
212 d_levels.gamma = do_tween(time, s_levels.gamma, d_levels.gamma, duration, tweener);
\r
214 result.set_levels(d_levels);
\r
219 bool operator<(const image_transform& lhs, const image_transform& rhs)
\r
221 return memcmp(&lhs, &rhs, sizeof(image_transform)) < 0;
\r
224 bool operator==(const image_transform& lhs, const image_transform& rhs)
\r
226 return memcmp(&lhs, &rhs, sizeof(image_transform)) == 0;
\r
229 bool operator!=(const image_transform& lhs, const image_transform& rhs)
\r
231 return !(lhs == rhs);
\r