-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfade.cpp
More file actions
62 lines (55 loc) · 2.32 KB
/
fade.cpp
File metadata and controls
62 lines (55 loc) · 2.32 KB
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
/*
** EPITECH PROJECT, 2025
** video-code
** File description:
** fade
*/
#include <algorithm>
#include <opencv2/opencv.hpp>
#include "transformation/transformation.hpp"
void transformation::fade(IterableInput input, const json::object_t& args)
{
const json::array_t& sides = args.at("sides");
const bool affectTransparentPixel = args.at("affectTransparentPixel");
const float startOpacity = args.at("startOpacity");
const float endOpacity = args.at("endOpacity");
const int nbFrames = input.nbFrames();
int frameIndex = 0;
for (auto& [frame, _] : input) {
int cols = frame.cols;
int rows = frame.rows;
float currentOpacity = startOpacity + (endOpacity - startOpacity) * (static_cast<float>(frameIndex) / (nbFrames - 1));
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
if (frame.at<cv::Vec4b>(y, x)[3] == 0 && affectTransparentPixel == false) {
continue;
}
float alpha = currentOpacity;
for (const auto& side : sides) {
if (side == "left") {
if (cols > 1) {
alpha *= 1 - static_cast<float>(x) / (cols - 1) * (1 - static_cast<float>(frameIndex) / (nbFrames - 1));
}
}
else if (side == "right") {
if (cols > 1) {
alpha *= 1 - static_cast<float>(cols - 1 - x) / (cols - 1) * (1 - static_cast<float>(frameIndex) / (nbFrames - 1));
}
}
else if (side == "up") {
if (rows > 1) {
alpha *= 1 - static_cast<float>(y) / (rows - 1) * (1 - static_cast<float>(frameIndex) / (nbFrames - 1));
}
}
else if (side == "down") {
if (rows > 1) {
alpha *= 1 - static_cast<float>(rows - 1 - y) / (rows - 1) * (1 - static_cast<float>(frameIndex) / (nbFrames - 1));
}
}
}
frame.at<cv::Vec4b>(y, x)[3] = static_cast<unsigned char>(std::clamp<float>(alpha, 0.0f, 255.0f));
}
}
frameIndex++;
}
}