-
Notifications
You must be signed in to change notification settings - Fork 261
/
KeyFrameBBox.h
200 lines (157 loc) · 6.07 KB
/
KeyFrameBBox.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* @file
* @brief Header file for the IKeyframe class
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/
/* LICENSE
*
* Copyright (c) 2008-2019 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPENSHOT_BBOXKEYFRAME_H
#define OPENSHOT_BBOXKEYFRAME_H
#include <iostream>
#include <iomanip>
#include <cmath>
#include <assert.h>
#include <vector>
#include "Exceptions.h"
#include "Fraction.h"
#include "Coordinate.h"
#include "Point.h"
#include "Json.h"
#include "IKeyFrame.h"
#include "KeyFrame.h"
namespace openshot {
/**
* @brief A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
*
* Keyframes are used to animate and interpolate values of properties over time. For example, a single property
* can use a Keyframe instead of a constant value. Assume you want to slide an image (from left to right) over
* a video. You can create a Keyframe which will adjust the X value of the image over 100 frames (or however many
* frames the animation needs to last) from the value of 0 to 640.
*
* \endcode
*/
struct BBox{
float x1 = -1;
float y1 = -1;
float width = -1;
float height = -1;
// Constructors
BBox(){
return;
}
BBox(float _x1, float _y1, float _width, float _height){
//frame_num = _frame_num;
x1 = _x1;
y1 = _y1;
width = _width;
height = _height;
}
std::string Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value JsonValue() const {
// Create root json object
Json::Value root;
root["x1"] = x1;
root["y1"] = y1;
root["height"] = height;
root["width"] = width;
return root;
}
// Load JSON string into this object
void SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void SetJsonValue(const Json::Value root) {
// Set data from Json (if key is found)
if (!root["x1"].isNull())
x1 = root["x1"].asDouble();
if (!root["y1"].isNull())
y1 = root["y1"].asDouble();
if (!root["height"].isNull())
height = root["height"].asDouble();
if (!root["width"].isNull())
width = root["width"].asDouble();
}
};
class KeyFrameBBox {
private:
bool visible;
Fraction BaseFps;
double TimeScale;
std::map<double, BBox> BoxVec;
public:
//Keyframe delta_x;
//Keyframe delta_y;
//Keyframe scale_x;
//Keyframe scale_y;
//Keyframe rotation;
KeyFrameBBox();
//void AddDisplacement(int64_t _frame_num, double _delta_x, double _delta_y);
//void AddScale(int64_t _frame_num, double _delta_x, double _delta_y);
void AddBox(int64_t _frame_num, float _x1, float _y1, float _width, float _height);
//void AddRotation(int64_t _frame_num, double rot);
void SetBaseFPS(Fraction fps);
Fraction GetBaseFPS();
void ScalePoints(double scale);
bool Contains(int64_t frame_number);
//double GetDelta(int64_t index) const ;
int64_t GetLength() const;
/// Remove a points by frame_number
void RemovePoint(int64_t frame_number);
//void RemoveDelta(int64_t frame_number);
//void RemoveScale(int64_t frame_number);
//void RemoveRotation(int64_t frame_number);
BBox GetValue(int64_t frame_number) const
{
return const_cast<KeyFrameBBox*>(this)->GetValue(frame_number);
}
BBox GetValue(int64_t frame_number);
/// Print collection of points
//void PrintParams();
double FrameNToTime(int64_t frame_number, double time_scale);
BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target);
/// Get and Set JSON methods
std::string Json(); ///< Generate JSON string of this object
Json::Value JsonValue(); ///< Generate Json::Value for this object
void SetJson(const std::string value); ///< Load JSON string into this object
void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
void clear(); //clear all fields
};
}
#endif