]> git.sesse.net Git - casparcg/blob - core/frame/frame_transform.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / frame / frame_transform.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../stdafx.h"\r
23 \r
24 #include "frame_transform.h"\r
25 \r
26 #include <boost/range/algorithm/equal.hpp>\r
27 #include <boost/range/algorithm/fill.hpp>\r
28 \r
29 namespace caspar { namespace core {\r
30                 \r
31 frame_transform::frame_transform() \r
32         : volume(1.0)\r
33         , opacity(1.0)\r
34         , brightness(1.0)\r
35         , contrast(1.0)\r
36         , saturation(1.0)\r
37         , field_mode(field_mode::progressive)\r
38         , is_key(false)\r
39         , is_mix(false)\r
40 {\r
41         boost::range::fill(fill_translation, 0.0);\r
42         boost::range::fill(fill_scale, 1.0);\r
43         boost::range::fill(clip_translation, 0.0);\r
44         boost::range::fill(clip_scale, 1.0);\r
45 }\r
46 \r
47 frame_transform& frame_transform::operator*=(const frame_transform &other)\r
48 {\r
49         volume                                  *= other.volume;\r
50         opacity                                 *= other.opacity;       \r
51         brightness                              *= other.brightness;\r
52         contrast                                *= other.contrast;\r
53         saturation                              *= other.saturation;\r
54         fill_translation[0]             += other.fill_translation[0]*fill_scale[0];\r
55         fill_translation[1]             += other.fill_translation[1]*fill_scale[1];\r
56         fill_scale[0]                   *= other.fill_scale[0];\r
57         fill_scale[1]                   *= other.fill_scale[1];\r
58         clip_translation[0]             += other.clip_translation[0]*clip_scale[0];\r
59         clip_translation[1]             += other.clip_translation[1]*clip_scale[1];\r
60         clip_scale[0]                   *= other.clip_scale[0];\r
61         clip_scale[1]                   *= other.clip_scale[1];\r
62         levels.min_input                 = std::max(levels.min_input,  other.levels.min_input);\r
63         levels.max_input                 = std::min(levels.max_input,  other.levels.max_input); \r
64         levels.min_output                = std::max(levels.min_output, other.levels.min_output);\r
65         levels.max_output                = std::min(levels.max_output, other.levels.max_output);\r
66         levels.gamma                    *= other.levels.gamma;\r
67         field_mode                               = static_cast<core::field_mode>(field_mode & other.field_mode);\r
68         is_key                                  |= other.is_key;\r
69         is_mix                                  |= other.is_mix;\r
70         return *this;\r
71 }\r
72 \r
73 frame_transform frame_transform::operator*(const frame_transform &other) const\r
74 {\r
75         return frame_transform(*this) *= other;\r
76 }\r
77 \r
78 frame_transform frame_transform::tween(double time, const frame_transform& source, const frame_transform& dest, double duration, const tweener& tween)\r
79 {       \r
80         auto do_tween = [](double time, double source, double dest, double duration, const tweener& tween)\r
81         {\r
82                 return tween(time, source, dest-source, duration);\r
83         };\r
84         \r
85         frame_transform result; \r
86         result.volume                           = do_tween(time, source.volume,                                 dest.volume,                            duration, tween);\r
87         result.brightness                       = do_tween(time, source.brightness,                             dest.brightness,                        duration, tween);\r
88         result.contrast                         = do_tween(time, source.contrast,                               dest.contrast,                          duration, tween);\r
89         result.saturation                       = do_tween(time, source.saturation,                             dest.saturation,                        duration, tween);\r
90         result.opacity                          = do_tween(time, source.opacity,                                dest.opacity,                           duration, tween);       \r
91         result.fill_translation[0]      = do_tween(time, source.fill_translation[0],    dest.fill_translation[0],       duration, tween), \r
92         result.fill_translation[1]      = do_tween(time, source.fill_translation[1],    dest.fill_translation[1],       duration, tween);               \r
93         result.fill_scale[0]            = do_tween(time, source.fill_scale[0],                  dest.fill_scale[0],                     duration, tween), \r
94         result.fill_scale[1]            = do_tween(time, source.fill_scale[1],                  dest.fill_scale[1],                     duration, tween);       \r
95         result.clip_translation[0]      = do_tween(time, source.clip_translation[0],    dest.clip_translation[0],       duration, tween), \r
96         result.clip_translation[1]      = do_tween(time, source.clip_translation[1],    dest.clip_translation[1],       duration, tween);               \r
97         result.clip_scale[0]            = do_tween(time, source.clip_scale[0],                  dest.clip_scale[0],                     duration, tween), \r
98         result.clip_scale[1]            = do_tween(time, source.clip_scale[1],                  dest.clip_scale[1],                     duration, tween);\r
99         result.levels.max_input         = do_tween(time, source.levels.max_input,               dest.levels.max_input,          duration, tween);\r
100         result.levels.min_input         = do_tween(time, source.levels.min_input,               dest.levels.min_input,          duration, tween);       \r
101         result.levels.max_output        = do_tween(time, source.levels.max_output,              dest.levels.max_output,         duration, tween);\r
102         result.levels.min_output        = do_tween(time, source.levels.min_output,              dest.levels.min_output,         duration, tween);\r
103         result.levels.gamma                     = do_tween(time, source.levels.gamma,                   dest.levels.gamma,                      duration, tween);\r
104         result.field_mode                       = source.field_mode & dest.field_mode;\r
105         result.is_key                           = source.is_key | dest.is_key;\r
106         result.is_mix                           = source.is_mix | dest.is_mix;\r
107         \r
108         return result;\r
109 }\r
110 \r
111 bool operator==(const frame_transform& lhs, const frame_transform& rhs)\r
112 {\r
113         auto eq = [](double lhs, double rhs)\r
114         {\r
115                 return std::abs(lhs - rhs) < 5e-8;\r
116         };\r
117 \r
118         return \r
119                 eq(lhs.volume, rhs.volume) &&\r
120                 eq(lhs.opacity, rhs.opacity) &&\r
121                 eq(lhs.contrast, rhs.contrast) &&\r
122                 eq(lhs.brightness, rhs.brightness) &&\r
123                 eq(lhs.saturation, rhs.saturation) &&\r
124                 boost::range::equal(lhs.fill_translation, rhs.fill_translation, eq) &&\r
125                 boost::range::equal(lhs.fill_scale, rhs.fill_scale, eq) &&\r
126                 boost::range::equal(lhs.clip_translation, rhs.clip_translation, eq) &&\r
127                 boost::range::equal(lhs.clip_scale, rhs.clip_scale, eq) &&\r
128                 lhs.field_mode == rhs.field_mode &&\r
129                 lhs.is_key == rhs.is_key &&\r
130                 lhs.is_mix == rhs.is_mix;\r
131 }\r
132 \r
133 bool operator!=(const frame_transform& lhs, const frame_transform& rhs)\r
134 {\r
135         return !(lhs == rhs);\r
136 }\r
137 \r
138 bool operator<(const frame_transform& lhs, const frame_transform& rhs)\r
139 {\r
140         return memcmp(&lhs, &rhs, sizeof(frame_transform)) < 0;\r
141 }\r
142 \r
143 bool operator>(const frame_transform& lhs, const frame_transform& rhs)\r
144 {\r
145         return memcmp(&lhs, &rhs, sizeof(frame_transform)) > 0;\r
146 }\r
147 \r
148 }}