-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvertChannel.cpp
More file actions
79 lines (68 loc) · 2.74 KB
/
invertChannel.cpp
File metadata and controls
79 lines (68 loc) · 2.74 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
#include "videoFunction.h"
#include "multithread.h"
using namespace std;
void InvertChannel(
Video &video,
const string &optimisationFlag,
const int &channel) {
if (optimisationFlag == "-S") {
multiThread(
video, [&](int64_t startFrame, int64_t endFrame)
{ InvertChannelSpeed(
ref(video), startFrame, endFrame, cref(channel)); });
} else if (optimisationFlag == "-M") {
InvertChannelMemory(video, channel);
} else {
InvertChannelVanilla(ref(video), cref(channel));
}
}
void InvertChannelSpeed(
Video &video,
int64_t startFrame,
int64_t endFrame,
const int &channel) {
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 pixel = video.getPixelValue(
currentFrame, xPosition, yPosition, channel);
// Invert the pixel if assuming pixel value range from 0 - 255
int invertedPixel = 255 - pixel;
video.writePixel(
invertedPixel, currentFrame, xPosition, yPosition, channel);
}
}
}
}
void InvertChannelMemory(Video &video, const int &channel) {
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++) {
int64_t pixelPosition = video.getPixelPosition(
currentFrame, xPosition, yPosition, channel);
unsigned char &pixelRef = currentFrame.pixels[pixelPosition];
// Invert the pixel if assuming pixel value range from 0 - 255
int invertedPixel = 255 - static_cast<int>(pixelRef);
pixelRef = static_cast<unsigned char>(invertedPixel);
}
}
writeFrameToFile(video, currentFrame);
}
}
void InvertChannelVanilla(Video &video, const int &channel) {
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++) {
int64_t pixelPosition = video.getPixelPosition(
currentFrame, xPosition, yPosition, channel);
unsigned char &pixelRef = currentFrame.pixels[pixelPosition];
// Invert the pixel if assuming pixel value range from 0 - 255
int invertedPixel = 255 - static_cast<int>(pixelRef);
pixelRef = static_cast<unsigned char>(invertedPixel);
}
}
}
}