Skip to content

Commit a49fc9a

Browse files
committed
fix(vm): deep copy when call copy
1 parent 5131cc6 commit a49fc9a

File tree

8 files changed

+70
-46
lines changed

8 files changed

+70
-46
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ SRC_INPUT = src/input/_AInput.cpp \
8383
src/input/Video.cpp \
8484
src/input/List.cpp \
8585
src/input/Slice.cpp \
86+
src/input/Copy.cpp \
8687

8788

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

include/input/Copy.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
** EPITECH PROJECT, 2025
3+
** video-code
4+
** File description:
5+
** Copy
6+
*/
7+
8+
#pragma once
9+
10+
#include <memory>
11+
12+
#include "input/_AInput.hpp"
13+
#include "input/_IInput.hpp"
14+
15+
class Copy : public _AInput {
16+
public:
17+
18+
Copy(std::shared_ptr<_IInput> input);
19+
~Copy() = default;
20+
21+
private:
22+
};

include/input/List.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
class List : public _AInput {
1616
public:
1717

18-
List(std::shared_ptr<_IInput> frames, int n);
19-
List(std::shared_ptr<_IInput> frames);
18+
List(std::shared_ptr<_IInput> input, int n);
2019
~List() = default;
2120

2221
private:

include/vm/LiveWindow.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ class LiveWindow {
5353
*/
5454
void goToLabel(const std::string &label);
5555

56-
/**
57-
* @ get all labels
58-
*/
59-
const std::map<std::string, std::size_t> &getLabels() const;
60-
const std::map<std::size_t, std::string> &getLabelsByVal() const;
61-
6256
/**
6357
* @ remove label
6458
*/

src/input/Copy.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
** EPITECH PROJECT, 2025
3+
** video-code
4+
** File description:
5+
** Image
6+
*/
7+
8+
#include "input/Copy.hpp"
9+
10+
#include <opencv2/imgcodecs.hpp>
11+
#include <vector>
12+
13+
#include "opencv2/core/mat.hpp"
14+
15+
static std::vector<cv::Mat> copyInput(std::shared_ptr<_IInput> input)
16+
{
17+
std::vector<cv::Mat> result{};
18+
19+
for (auto &it : input->getFrames()) {
20+
result.push_back(it.clone());
21+
}
22+
23+
return result;
24+
}
25+
26+
Copy::Copy(std::shared_ptr<_IInput> input)
27+
: _AInput(copyInput(input))
28+
{
29+
}

src/input/List.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,18 @@
1313
#include "input/_AInput.hpp"
1414
#include "opencv2/core/mat.hpp"
1515

16-
static std::vector<cv::Mat> replicateFrame(std::shared_ptr<_IInput> frames, int n)
16+
static std::vector<cv::Mat> replicateFrame(std::shared_ptr<_IInput> input, int n)
1717
{
1818
std::vector<cv::Mat> result{};
1919

2020
while (n--) {
21-
const auto& temp = frames->cgetFrames();
21+
const auto& temp = input->cgetFrames();
2222
result.insert(result.end(), temp.begin(), temp.end());
2323
}
2424
return result;
2525
}
2626

27-
List::List(std::shared_ptr<_IInput> frames, int n)
28-
: _AInput(replicateFrame(frames, n))
29-
{
30-
}
31-
32-
List::List(std::shared_ptr<_IInput> frames)
33-
: _AInput({frames->cgetFrames().begin(), frames->cgetFrames().end()})
27+
List::List(std::shared_ptr<_IInput> input, int n)
28+
: _AInput(replicateFrame(input, n))
3429
{
3530
}

src/vm/LiveWindow.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <opencv2/imgproc.hpp>
2828
#include <string>
2929

30+
#include "input/Copy.hpp"
3031
#include "input/Image.hpp"
3132
#include "input/List.hpp"
3233
#include "input/Slice.hpp"
@@ -121,16 +122,6 @@ void LiveWindow::goToLabel(const std::string &label)
121122
}
122123
}
123124

124-
const std::map<std::string, std::size_t> &LiveWindow::getLabels() const
125-
{
126-
return _labels;
127-
}
128-
129-
const std::map<std::size_t, std::string> &LiveWindow::getLabelsByVal() const
130-
{
131-
return _labelsByVal;
132-
}
133-
134125
void LiveWindow::removeLabel(const std::string &label)
135126
{
136127
_labelsByVal.erase(_labels[label]);
@@ -203,15 +194,13 @@ const std::shared_ptr<_IInput> &LiveWindow::getInput(std::string &&input)
203194

204195
void LiveWindow::firstFrame()
205196
{
206-
_index = 0;
207-
_currentLabel = _labelsByVal[0];
197+
goToLabel(_labelsByVal[0]);
208198
std::cout << std::format("Current Label set to '{}' at frame '0'.", _currentLabel) << std::endl;
209199
}
210200

211201
void LiveWindow::lastFrame()
212202
{
213-
_index = _frames.size() - 1;
214-
_currentLabel = std::prev(_labelsByVal.end())->second;
203+
goToLabel(std::prev(_labelsByVal.end())->second);
215204
std::cout << std::format("Current Label set to '{}' at frame '{}'.", _currentLabel, _index) << std::endl;
216205
}
217206

@@ -233,19 +222,20 @@ void LiveWindow::previousLabel()
233222
goToLabel(_currentLabel);
234223
std::cout << std::format("Timeline set to the start of the current label '{}', at frame '{}'.", _currentLabel, _index) << std::endl;
235224
} else {
236-
goToLabel(std::prev(_labels.find(_currentLabel))->first);
225+
goToLabel(std::prev(_labelsByVal.find(_labels[_currentLabel]))->second);
237226
std::cout << std::format("Timeline set to the previous label '{}', at frame '{}'.", _currentLabel, _index) << std::endl;
238227
}
239228
}
240229

241230
void LiveWindow::nextLabel()
242231
{
243-
auto upperBound = getLabelsByVal().upper_bound(_labels[_currentLabel]);
232+
auto next = std::next(_labelsByVal.find(_labels[_currentLabel]));
244233

245-
if (upperBound == getLabelsByVal().end()) {
246-
std::cout << std::format("The Timeline is currently at the last label, '{}'", _currentLabel) << std::endl;
234+
if (next == _labelsByVal.end()) {
235+
_index = _frames.size() - 1;
236+
std::cout << std::format("Timeline set to last index, '{}'", _index) << std::endl;
247237
} else {
248-
goToLabel(upperBound->second);
238+
goToLabel(next->second);
249239
std::cout << std::format("Timeline set to the next label '{}', at frame '{}'.", _currentLabel, _index) << std::endl;
250240
}
251241
}
@@ -356,7 +346,7 @@ std::shared_ptr<_IInput> LiveWindow::repeat(const json::array_t &args)
356346

357347
std::shared_ptr<_IInput> LiveWindow::copy(const json::array_t &args)
358348
{
359-
return std::make_unique<List>(executeInst(args[0]));
349+
return std::make_unique<Copy>(executeInst(args[0]));
360350
}
361351

362352
std::shared_ptr<_IInput> LiveWindow::concat(const json::array_t &args)

video.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@
55
# normal image
66
v1.add()
77

8-
# translated image
9-
v1.apply(translate(100, 50))
10-
v1.add()
11-
12-
# back to normal image
13-
v1.apply(translate(-100, -50))
14-
v1.add()
8+
# erase top left corner and fade the first half copy
9+
v1.apply(translate(-100, -50))[:30].copy().apply(fadeIn(ALL, -1)).add()
1510

16-
# erase some parts
17-
v1.apply(translate(-100, -50))
11+
# original
1812
v1.add()

0 commit comments

Comments
 (0)