forked from IENT/YUView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversionDifferenceRGB.cpp
More file actions
227 lines (189 loc) · 8.78 KB
/
Copy pathConversionDifferenceRGB.cpp
File metadata and controls
227 lines (189 loc) · 8.78 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* This file is part of YUView - The YUV player with advanced analytics toolset
* <https://github.com/IENT/YUView>
* Copyright (C) 2015 Institut für Nachrichtentechnik, RWTH Aachen University, GERMANY
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*
* You must obey the GNU General Public License in all respects for all
* of the code used other than OpenSSL. If you modify file(s) with this
* exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do
* so, delete this exception statement from your version. If you delete
* this exception statement from all source files in the program, then
* also delete it here.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ConversionDifferenceRGB.h"
#include <common/FunctionsGui.h>
#include <cstdint>
#include "ConversionFunctions.h"
#include "video/PixelFormat.h"
#include "video/rgb/ConversionRGB.h"
#include "video/rgb/PixelFormatRGB.h"
// Restrict is basically a promise to the compiler that for the scope of the pointer, the target of
// the pointer will only be accessed through that pointer (and pointers copied from it).
#if __STDC__ != 1
#define restrict __restrict /* use implementation __ format */
#else
#ifndef __STDC_VERSION__
#define restrict __restrict /* use implementation __ format */
#else
#if __STDC_VERSION__ < 199901L
#define restrict __restrict /* use implementation __ format */
#else
#/* all ok */
#endif
#endif
#endif
namespace video::rgb
{
namespace
{
using RenderValue = std::tuple<unsigned char, unsigned char, unsigned char>;
RenderValue convertDeltaToRenderValue(const rgba_t &delta,
const bool markDifference,
const int amplificationFactor)
{
if (markDifference)
{
const auto r = (delta.r == 0) ? 0 : 255;
const auto g = (delta.g == 0) ? 0 : 255;
const auto b = (delta.b == 0) ? 0 : 255;
return {r, g, b};
}
else
{
const auto r = static_cast<unsigned char>(
functions::clip(128 + static_cast<int64_t>(delta.r) * amplificationFactor, 0LL, 255LL));
const auto g = static_cast<unsigned char>(
functions::clip(128 + static_cast<int64_t>(delta.g) * amplificationFactor, 0LL, 255LL));
const auto b = static_cast<unsigned char>(
functions::clip(128 + static_cast<int64_t>(delta.b) * amplificationFactor, 0LL, 255LL));
return {r, g, b};
}
}
std::pair<QImage, MSE>
calculateDifferencePredefinedPixelFormat(const InputFrameParameters &frame1,
const InputFrameParameters &frame2,
const PredefinedPixelFormat predefinedPixelFormat,
const Endianness endianness,
const int amplificationFactor,
const bool markDifference)
{
if (predefinedPixelFormat != PredefinedPixelFormat::RGB565)
return {};
const auto frameSize = Size(std::min(frame1.frameSize.width, frame2.frameSize.width),
std::min(frame1.frameSize.height, frame2.frameSize.height));
auto outputImage =
QImage(QSize(frameSize.width, frameSize.height), functionsGui::platformImageFormat(false));
unsigned char *restrict dst = outputImage.bits();
SSE sse;
auto rawData1 = reinterpret_cast<const unsigned char *>(frame1.rawDataItem.data());
auto rawData2 = reinterpret_cast<const unsigned char *>(frame2.rawDataItem.data());
for (unsigned i = 0; i < frameSize.width * frameSize.height; ++i)
{
const auto rgb1 = extractRGB565Value(rawData1, endianness);
const auto rgb2 = extractRGB565Value(rawData2, endianness);
const auto delta = rgb1 - rgb2;
sse.addSample(delta);
std::tie(dst[2], dst[1], dst[0]) =
convertDeltaToRenderValue(delta, markDifference, amplificationFactor);
dst[3] = 255;
rawData1 += 2;
rawData2 += 2;
dst += 4;
}
return {outputImage, sse.getMSE()};
}
template <typename T>
rgba_t getRGBAndConvertEndianness(const DataPointers<T> dataPointers, const Endianness endianness)
{
constexpr auto bitDepth =
(std::is_same_v<T, uint8_t> ? 8 : (std::is_same_v<T, uint16_t> ? 16 : 32));
auto r = *dataPointers.r;
auto g = *dataPointers.g;
auto b = *dataPointers.b;
if (endianness == Endianness::Big)
{
r = swapBytesEndianness<bitDepth>(r);
g = swapBytesEndianness<bitDepth>(g);
b = swapBytesEndianness<bitDepth>(b);
}
return rgba_t({.r = static_cast<int>(r), .g = static_cast<int>(g), .b = static_cast<int>(b)});
}
template <typename T>
std::pair<QImage, MSE> calculateDifferenceAndMSE(const InputFrameParameters &frame1,
const InputFrameParameters &frame2,
const PixelFormatRGB &pixelFormat,
const int amplificationFactor,
const bool markDifference)
{
static_assert(std::is_same_v<T, uint8_t> || std::is_same_v<T, uint16_t> ||
std::is_same_v<T, uint32_t>);
auto dataPointers1 =
calculatePointersToStartOfComponents<T>(frame1.rawDataItem, frame1.frameSize, pixelFormat);
auto dataPointers2 =
calculatePointersToStartOfComponents<T>(frame2.rawDataItem, frame2.frameSize, pixelFormat);
const auto frameSize = Size(std::min(frame1.frameSize.width, frame2.frameSize.width),
std::min(frame1.frameSize.height, frame2.frameSize.height));
auto outputImage = QImage(QSize(frameSize.width, frameSize.height),
functionsGui::platformImageFormat(pixelFormat.hasAlpha()));
SSE sse;
unsigned char *restrict dst = outputImage.bits();
const auto offsetToNextValue =
(pixelFormat.getDataLayout() == DataLayout::Planar ? 1 : pixelFormat.getNrChannels());
for (unsigned i = 0; i < frameSize.width * frameSize.height; ++i)
{
const auto rgb1 = getRGBAndConvertEndianness(dataPointers1, pixelFormat.getEndianness());
const auto rgb2 = getRGBAndConvertEndianness(dataPointers2, pixelFormat.getEndianness());
const auto delta = rgb1 - rgb2;
sse.addSample(delta);
std::tie(dst[2], dst[1], dst[0]) =
convertDeltaToRenderValue(delta, markDifference, amplificationFactor);
dst[3] = 255;
dataPointers1 += offsetToNextValue;
dataPointers2 += offsetToNextValue;
dst += 4;
}
return {outputImage, sse.getMSE()};
}
} // namespace
std::pair<QImage, MSE> calculateDifferenceAndMSE(const InputFrameParameters &frame1,
const InputFrameParameters &frame2,
const PixelFormatRGB &pixelFormat,
const int amplificationFactor,
const bool markDifference)
{
if (pixelFormat.getPredefinedPixelFormat())
return calculateDifferencePredefinedPixelFormat(frame1,
frame2,
*pixelFormat.getPredefinedPixelFormat(),
pixelFormat.getEndianness(),
amplificationFactor,
markDifference);
if (pixelFormat.getBitsPerComponent() == 8)
return calculateDifferenceAndMSE<uint8_t>(
frame1, frame2, pixelFormat, amplificationFactor, markDifference);
if (pixelFormat.getBitsPerComponent() <= 16)
return calculateDifferenceAndMSE<uint16_t>(
frame1, frame2, pixelFormat, amplificationFactor, markDifference);
return calculateDifferenceAndMSE<uint32_t>(
frame1, frame2, pixelFormat, amplificationFactor, markDifference);
}
} // namespace video::rgb