Skip to content

Commit f6ad980

Browse files
committed
rework: zoom
Iterable now uses get() to access the underlying input instead of ->, improves clarity.
1 parent e460c76 commit f6ad980

File tree

14 files changed

+58
-125
lines changed

14 files changed

+58
-125
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ SRC_TSF_POS = src/transformation/position/translate.cpp \
104104

105105

106106
SRC_TSF_COLOR = src/transformation/color/fade.cpp \
107+
src/transformation/color/grayscale.cpp \
107108

108109

109110
SRC_TSF_OTHER = src/transformation/other/overlay.cpp \
110-
src/transformation/other/repeat.cpp \
111+
src/transformation/other/repeat.cpp \
112+
src/transformation/other/zoom.cpp \
111113

112114

113115
OBJ = $(SRC:.cpp=.o)

include/input/Iterable.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class IterableInput
4444
_endIndex = endTime.get<float>() * framerate;
4545
}
4646

47-
_size = _endIndex - _beginIndex;
47+
_nbFrames = _endIndex - _beginIndex;
4848

4949
_beginIterator = input->begin() + _beginIndex;
5050
_endIterator = input->begin() + _endIndex;
@@ -56,17 +56,16 @@ class IterableInput
5656

5757
std::vector<Frame>::iterator end() { return _endIterator; }
5858

59-
IInput* operator->() { return _input.get(); }
59+
IInput* get() { return _input.get(); }
6060

61-
size_t nbFrames() { return _size; };
61+
size_t _nbFrames;
6262

6363
private:
6464

6565
std::shared_ptr<IInput> _input;
6666

6767
size_t _beginIndex;
6868
size_t _endIndex;
69-
size_t _size;
7069

7170
///< Different begin and start than the size of the frames of the input
7271
std::vector<Frame>::iterator _beginIterator;

include/vm/VideoCode.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ class VideoCode
6060
void reloadSourceFile(); ///< Update the timeline if any changes occured in the source file
6161
void goToFirstFrame();
6262
void goToLastFrame();
63-
void goToPreviousLabel();
64-
void goToNextLabel();
63+
void backward3frame();
64+
void forward3frame();
6565

6666
///< Execute the instructions in the stack
6767
void executeStack();
6868

6969
private:
7070

7171
///< TODO: frame rate
72-
const int _framerate{24};
72+
const size_t _framerate{24};
7373

7474
///< Window size
7575
const int _width;
@@ -105,8 +105,8 @@ class VideoCode
105105
{Qt::Key_R, bindCmd(reloadSourceFile)},
106106
{Qt::Key_Down, bindCmd(goToFirstFrame)},
107107
{Qt::Key_Up, bindCmd(goToLastFrame)},
108-
{Qt::Key_Left, bindCmd(goToPreviousLabel)},
109-
{Qt::Key_Right, bindCmd(goToNextLabel)},
108+
{Qt::Key_Left, bindCmd(backward3frame)},
109+
{Qt::Key_Right, bindCmd(forward3frame)},
110110
};
111111
// clang-format on
112112

requirements.txt

Whitespace-only changes.

src/transformation/color/fade.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void transformation::fade(IterableInput input, const json::object_t& args)
1515
const bool affectTransparentPixel = args.at("affectTransparentPixel");
1616
const float startOpacity = args.at("startOpacity");
1717
const float endOpacity = args.at("endOpacity");
18-
const int nbFrames = input.nbFrames();
18+
const size_t nbFrames = input._nbFrames;
1919

2020
int frameIndex = 0;
2121
for (auto& [frame, _] : input) {
@@ -39,6 +39,7 @@ void transformation::fade(IterableInput input, const json::object_t& args)
3939
}
4040
else if (side == "right") {
4141
if (cols > 1) {
42+
///< FIX: maybe error. try with fadeIn from Left then fadeOut from Right
4243
alpha *= 1 - static_cast<float>(cols - 1 - x) / (cols - 1) * (1 - static_cast<float>(frameIndex) / (nbFrames - 1));
4344
}
4445
}

src/transformation/color/grayscale.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99

1010
#include "transformation/transformation.hpp"
1111

12-
void transformation::grayscale(std::shared_ptr<IInput> input, [[maybe_unused]] Register &reg, [[maybe_unused]] const json::object_t &args)
12+
void transformation::grayscale(IterableInput input, [[maybe_unused]] const json::object_t &args)
1313
{
14-
for (auto it = input->begin(); it != input->end(); ++it)
15-
{
16-
cv::Mat &frame = it->_mat;
17-
for (int y = 0; y < frame.rows; y++)
18-
{
19-
for (int x = 0; x < frame.cols; x++)
20-
{
14+
for (auto &[frame, _] : input) {
15+
for (int y = 0; y < frame.rows; y++) {
16+
for (int x = 0; x < frame.cols; x++) {
2117
cv::Vec4b &pixel = frame.at<cv::Vec4b>(y, x);
2218
uchar gray_value = static_cast<uchar>(0.299 * pixel[2] + 0.587 * pixel[1] + 0.114 * pixel[0]);
2319
pixel = cv::Vec4b(gray_value, gray_value, gray_value, pixel[3]);

src/transformation/other/repeat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
void transformation::repeat(IterableInput input, const json::object_t& args)
1212
{
13-
input->repeat(args.at("n"));
13+
input.get()->repeat(args.at("n"));
1414
}

src/transformation/other/zoom.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,23 @@
55
** zoom
66
*/
77

8-
#include <memory>
9-
10-
#include "input/IInput.hpp"
118
#include "transformation/transformation.hpp"
129

13-
void transformation::zoom(std::shared_ptr<IInput> input, [[maybe_unused]] Register &reg, const json::object_t &args)
10+
void transformation::zoom(IterableInput input, const json::object_t &args)
1411
{
15-
const float zoomFactor = args.at("zoomFactor");
16-
const std::pair<float, float> zoomCenter = args.at("zoomCenter");
17-
const bool staticZoom = args.at("mode") == "static";
18-
const float startZoomFactor = args.at("zoomstart");
19-
const float endZoomFactor = args.at("zoomend");
20-
const int nbFrames = input->size();
21-
int i = 0;
12+
const int x = args.at("x").is_number_integer() ? args.at("x").get<int>() : args.at("x").get<float>() * input.get()->begin()->_mat.cols;
13+
const int y = args.at("y").is_number_integer() ? args.at("y").get<int>() : args.at("y").get<float>() * input.get()->begin()->_mat.rows;
14+
const cv::Point2f center(x, y);
2215

23-
for (auto &[frame, _] : *input)
24-
{
25-
float currentZoomFactor;
16+
const float startFactor = args.at("factor")[0];
17+
const float endFactor = args.at("factor")[1];
2618

27-
if (staticZoom)
28-
currentZoomFactor = zoomFactor;
29-
else
30-
currentZoomFactor = startZoomFactor + (endZoomFactor - startZoomFactor) * (static_cast<float>(i) / (nbFrames - 1));
19+
const float zoomIncr = (endFactor - startFactor) / (input._nbFrames - 1);
20+
float zoomAcc = startFactor - zoomIncr;
3121

32-
cv::Mat zoomedFrame;
33-
cv::Point2f center(frame.cols * zoomCenter.first, frame.rows * zoomCenter.second);
34-
cv::Mat zoomMatrix = cv::getRotationMatrix2D(center, 0, currentZoomFactor);
35-
cv::warpAffine(frame, zoomedFrame, zoomMatrix, frame.size());
36-
frame = zoomedFrame;
37-
i++;
22+
for (auto &[frame, _] : input) {
23+
zoomAcc += zoomIncr;
24+
cv::Mat zoomMatrix = cv::getRotationMatrix2D(center, 0, zoomAcc);
25+
cv::warpAffine(frame, frame, zoomMatrix, frame.size());
3826
}
3927
}

src/transformation/position/move.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ void transformation::move(IterableInput input, const json::object_t& args)
1111
{
1212
const int dstX = args.at("x");
1313
const int dstY = args.at("y");
14-
const int srcX = input->begin()->_meta.x;
15-
const int srcY = input->begin()->_meta.y;
16-
const int nbFrames = input->size();
14+
const int srcX = input.get()->begin()->_meta.x;
15+
const int srcY = input.get()->begin()->_meta.y;
16+
const int nbFrames = input._nbFrames;
1717

1818
if (nbFrames == 0) {
1919
return;

src/vm/VideoCode.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ void VideoCode::executeStack()
103103
}
104104
else if (i["action"] == "Apply") {
105105
///< {"action": 'Apply', "input": 0, "transformation": 'overlay', args: {"fg": 1}}
106-
VC_LOG_DEBUG("transformation: " << i["transformation"] << ": " << i["input"]);
107-
108-
i["args"]["framerate"] = _framerate;
109-
110106
transformation::map.at(i["transformation"])(IterableInput(_register[i["input"]], i["startTime"], i["endTime"], _framerate), i["args"]);
111107
}
112108
else if (i["action"] == "Wait") {
@@ -239,14 +235,6 @@ static void overlayKeptInput(cv::Mat &background, const Frame &frame)
239235
const cv::Vec4b &bgPixel = background.at<cv::Vec4b>(y + dst.y, x + dst.x);
240236
const cv::Vec4b &ovPixel = overlay.at<cv::Vec4b>(y + src.y, x + src.x);
241237

242-
// if (ovPixel[0] == 0 && ovPixel[1] == 0 && ovPixel[1] == 0 && ovPixel[3] != 0)
243-
// {
244-
// // std::cout << "x:" << x << std::endl;
245-
// // std::cout << "y:" << y << std::endl;
246-
// // std::cout << "alpha:" << (int)ovPixel[3] << std::endl;
247-
// continue;
248-
// }
249-
250238
const float alphaBg = bgPixel[3] / 255.0f;
251239
const float alphaOv = ovPixel[3] / 255.0f;
252240

@@ -300,28 +288,20 @@ void VideoCode::goToLastFrame()
300288
std::cout << std::format("Current Label set to '{}' at frame '{}'.", _currentLabel, _index) << std::endl;
301289
}
302290

303-
void VideoCode::goToPreviousLabel()
291+
void VideoCode::backward3frame()
304292
{
305-
if (_labels[_currentLabel] == 0) {
306-
goToLabel(_currentLabel);
307-
std::cout << std::format("Timeline set to the start of the current label '{}', at frame '{}'.", _currentLabel, _index) << std::endl;
293+
if (_index < 3 * _framerate) {
294+
_index = 0;
308295
}
309296
else {
310-
goToLabel(std::prev(_labelsByVal.find(_labels[_currentLabel]))->second);
311-
std::cout << std::format("Timeline set to the previous label '{}', at frame '{}'.", _currentLabel, _index) << std::endl;
297+
_index -= 3 * _framerate;
312298
}
313299
}
314300

315-
void VideoCode::goToNextLabel()
301+
void VideoCode::forward3frame()
316302
{
317-
auto next = std::next(_labelsByVal.find(_labels[_currentLabel]));
318-
319-
if (next == _labelsByVal.end()) {
303+
_index += 5 * _framerate;
304+
if (_index > _frames.size()) {
320305
_index = _frames.size() - 1;
321-
std::cout << std::format("Timeline set to last index, '{}'", _index) << std::endl;
322-
}
323-
else {
324-
goToLabel(next->second);
325-
std::cout << std::format("Timeline set to the next label '{}', at frame '{}'.", _currentLabel, _index) << std::endl;
326306
}
327307
}

0 commit comments

Comments
 (0)