-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapChannel.cpp
More file actions
100 lines (86 loc) · 3.35 KB
/
swapChannel.cpp
File metadata and controls
100 lines (86 loc) · 3.35 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
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
#include "videoFunction.h"
#include "multithread.h"
using namespace std;
void swapChannel(
Video &video,
const string &optimisationFlag,
const int &channel1,
const int &channel2) {
if (optimisationFlag == "-S") {
multiThread(
video, [&](int64_t startFrame, int64_t endFrame)
{ swapChannelSpeed(ref(video), startFrame, endFrame,
cref(channel1), cref(channel2)); });
} else if (optimisationFlag == "-M") {
swapChannelMemory(ref(video), cref(channel1), cref(channel2));
} else {
swapChannelVanilla(ref(video), cref(channel1), cref(channel2));
}
}
void swapChannelSpeed(
Video &video,
const int64_t startFrame,
const int64_t endFrame,
const int channel1,
const int channel2) {
for (int64_t frameIndex = startFrame; frameIndex < endFrame; ++frameIndex) {
Frame ¤tFrame = video.frames[frameIndex];
for (int64_t yPosition = 0; yPosition < video.height; ++yPosition) {
for (int64_t xPosition = 0; xPosition < video.width; ++xPosition) {
unsigned char pixel1 = video.getPixelValue(
currentFrame, xPosition, yPosition, channel1);
int64_t pixel2Position = video.getPixelPosition(
currentFrame, xPosition, yPosition, channel2);
unsigned char &pixel2Ref = currentFrame.pixels[pixel2Position];
// Swap the pixel between the 2 channel
video.writePixel(
pixel2Ref, currentFrame, xPosition, yPosition, channel1);
pixel2Ref = pixel1;
}
}
}
}
void swapChannelMemory(
Video &video,
const int channel1,
const int channel2) {
for (int64_t frameIndex = 0; frameIndex < video.numFrames; frameIndex++) {
Frame currentFrame = readFrameFromFile(video, frameIndex);
for (int64_t yPosition = 0; yPosition < video.height; yPosition++) {
for (int64_t xPosition = 0; xPosition < video.width; xPosition++) {
// Alway need to create a copy otherwise they overwrite eachother
unsigned char pixel1 = video.getPixelValue(
currentFrame, xPosition, yPosition, channel1);
int64_t pixel2Position = video.getPixelPosition(
currentFrame, xPosition, yPosition, channel2);
unsigned char &pixel2Ref = currentFrame.pixels[pixel2Position];
video.writePixel(
pixel2Ref, currentFrame, xPosition, yPosition, channel1);
pixel2Ref = pixel1;
}
}
writeFrameToFile(video, currentFrame);
}
}
void swapChannelVanilla(
Video &video,
const int channel1,
const int channel2) {
for (int64_t frameIndex = 0; frameIndex < video.numFrames; ++frameIndex) {
Frame ¤tFrame = video.frames[frameIndex];
for (int64_t yPosition = 0; yPosition < video.height; ++yPosition) {
for (int64_t xPosition = 0; xPosition < video.width; ++xPosition) {
// Alway need to create a copy otherwise they overwrite eachother
unsigned char pixel1 = video.getPixelValue(
currentFrame, xPosition, yPosition, channel1);
int64_t pixel2Position = video.getPixelPosition(
currentFrame, xPosition, yPosition, channel2);
unsigned char &pixel2Ref = currentFrame.pixels[pixel2Position];
// Swap the pixel between the 2 channel
video.writePixel(
pixel2Ref, currentFrame, xPosition, yPosition, channel1);
pixel2Ref = pixel1;
}
}
}
}