Repository navigation
Expand file tree
/
Copy pathImage.cpp
More file actions
356 lines (308 loc) · 12.7 KB
/
Copy pathImage.cpp
File metadata and controls
356 lines (308 loc) · 12.7 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Image.h"
#include <fstream>
#include <ostream>
namespace gfxstream {
// Loads:
// rgbaPixels[0] = R for x:0 y:0
// rgbaPixels[1] = G for x:0 y:0
// rgbaPixels[2] = B for x:0 y:0
// rgbaPixels[3] = A for x:0 y:0
gfxstream::expected<RGBAImage, std::string> LoadRGBAFromBitmapFile(
const std::string& filename) {
std::ifstream bitmap(filename, std::ofstream::in | std::ofstream::binary);
if (!bitmap.is_open()) {
return gfxstream::unexpected("Failed to open " + filename);
}
std::vector<char> bitmapBytes((std::istreambuf_iterator<char>(bitmap)),
std::istreambuf_iterator<char>());
if (bitmapBytes[0] != 0x42) {
return gfxstream::unexpected("Failed to open " + filename + ": invalid bitmap file?");
}
if (bitmapBytes[1] != 0x4D) {
return gfxstream::unexpected("Failed to open " + filename + ": invalid bitmap file?");
}
auto ReadUint16AtByte = [&](const uint32_t offset) {
return *reinterpret_cast<uint16_t*>(&bitmapBytes[offset]);
};
auto ReadUint32AtByte = [&](const uint32_t offset) {
return *reinterpret_cast<uint32_t*>(&bitmapBytes[offset]);
};
uint32_t w = ReadUint32AtByte(18);
uint32_t h = ReadUint32AtByte(22);
uint32_t planes = ReadUint16AtByte(26);
if (planes != 1) {
return gfxstream::unexpected("Failed to open " + filename + ": unhandled number of planes.");
}
uint32_t bpp = ReadUint16AtByte(28);
if (bpp != 32) {
return gfxstream::unexpected("Failed to open " + filename + ": unhandled bpp.");
}
uint32_t rChannelMask = ReadUint32AtByte(54);
uint32_t gChannelMask = ReadUint32AtByte(58);
uint32_t bChannelMask = ReadUint32AtByte(62);
uint32_t aChannelMask = ReadUint32AtByte(66);
RGBAImage out;
out.width = w;
out.height = h;
out.pixels.reserve(w * h * 4);
uint32_t bitmapHeadersSize = ReadUint32AtByte(10);
uint32_t bitmapPixelsOffset = bitmapHeadersSize;
auto GetChannel = [](uint32_t pixel, uint32_t channelMask) {
if (channelMask == 0) {
return static_cast<uint8_t>(0xFF);
} else if (channelMask == 0x000000FF) {
return static_cast<uint8_t>((pixel & channelMask) >> 0);
} else if (channelMask == 0x0000FF00) {
return static_cast<uint8_t>((pixel & channelMask) >> 8);
} else if (channelMask == 0x00FF0000) {
return static_cast<uint8_t>((pixel & channelMask) >> 16);
} else if (channelMask == 0xFF000000) {
return static_cast<uint8_t>((pixel & channelMask) >> 24);
} else {
return static_cast<uint8_t>(0);
}
};
for (uint32_t y = 0; y < h; y++) {
uint32_t flippedY = h - y - 1;
for (uint32_t x = 0; x < w; x++) {
uint32_t pixelOffset = (flippedY * w * 4) + (x * 4);
uint32_t pixel = ReadUint32AtByte(bitmapPixelsOffset + pixelOffset);
uint8_t r = GetChannel(pixel, rChannelMask);
uint8_t g = GetChannel(pixel, gChannelMask);
uint8_t b = GetChannel(pixel, bChannelMask);
uint8_t a = GetChannel(pixel, aChannelMask);
out.pixels.push_back(r);
out.pixels.push_back(g);
out.pixels.push_back(b);
out.pixels.push_back(a);
}
}
return out;
}
// Assumes:
// rgbaPixels[0] = R for x:0 y:0
// rgbaPixels[1] = G for x:0 y:0
// rgbaPixels[2] = B for x:0 y:0
// rgbaPixels[3] = A for x:0 y:0
gfxstream::expected<Ok, std::string> SaveRGBAToBitmapFile(
uint32_t w,
uint32_t h,
const uint8_t* rgbaPixels,
const std::string& filename) {
std::ofstream bitmap(filename, std::ofstream::out | std::ofstream::binary);
if (!bitmap.is_open()) {
return gfxstream::unexpected("Failed to save " + filename + ": failed to open.");
}
static constexpr const uint32_t kBytesPerPixel = 4;
uint32_t bitmapPixelsSize = w * h * kBytesPerPixel;
uint32_t bitmapHeaderSize = 14;
uint32_t bitmapDibHeaderSize = 108;
uint32_t bitmapHeadersSize = bitmapHeaderSize + bitmapDibHeaderSize;
uint32_t bitmapFileSize = bitmapHeadersSize + bitmapPixelsSize;
auto WriteAsBytes = [&](const auto& value) {
bitmap.write(reinterpret_cast<const char*>(&value), sizeof(value));
};
auto WriteCharAsBytes = [&](const char value) { WriteAsBytes(value); };
auto WriteUint16AsBytes = [&](const uint16_t value) { WriteAsBytes(value); };
auto WriteUint32AsBytes = [&](const uint32_t value) { WriteAsBytes(value); };
WriteCharAsBytes(0x42); // "B"
WriteCharAsBytes(0x4D); // "M"
WriteUint32AsBytes(bitmapFileSize);
WriteCharAsBytes(0); // reserved 1
WriteCharAsBytes(0); // reserved 1
WriteCharAsBytes(0); // reserved 2
WriteCharAsBytes(0); // reserved 2
WriteUint32AsBytes(bitmapHeadersSize); // offset to actual pixel data
WriteUint32AsBytes(bitmapDibHeaderSize);
WriteUint32AsBytes(w);
WriteUint32AsBytes(h);
WriteUint16AsBytes(1); // number of planes
WriteUint16AsBytes(32); // bits per pixel
WriteUint32AsBytes(0x03); // compression/format
WriteUint32AsBytes(bitmapPixelsSize); // image size
WriteUint32AsBytes(0); // horizontal print reset
WriteUint32AsBytes(0); // vertical print reset
WriteUint32AsBytes(0); // num_palette_colors
WriteUint32AsBytes(0); // num_important_colors
WriteUint32AsBytes(0x000000FF); // red channel mask
WriteUint32AsBytes(0x0000FF00); // green channel mask
WriteUint32AsBytes(0x00FF0000); // blue channel mask
WriteUint32AsBytes(0xFF000000); // alpha channel mask
WriteUint32AsBytes(0x206e6957); // "win"
for (uint32_t i = 0; i < 36; i++) {
WriteCharAsBytes(0);
} // cie color space
WriteUint32AsBytes(0); // "win"
WriteUint32AsBytes(0); // "win"
WriteUint32AsBytes(0); // "win"
uint32_t stribeBytes = w * 4;
for (uint32_t currentY = 0; currentY < h; currentY++) {
uint32_t flippedY = h - currentY - 1;
const uint8_t* currentPixel = rgbaPixels + (stribeBytes * flippedY);
for (uint32_t currentX = 0; currentX < w; currentX++) {
WriteAsBytes(*currentPixel);
++currentPixel;
WriteAsBytes(*currentPixel);
++currentPixel;
WriteAsBytes(*currentPixel);
++currentPixel;
WriteAsBytes(*currentPixel);
++currentPixel;
}
}
bitmap.close();
return Ok{};
}
gfxstream::expected<YUV420Image, std::string> LoadYUV420FromBitmapFile(
const std::string& filename) {
auto rgbaImage = GFXSTREAM_EXPECT(LoadRGBAFromBitmapFile(filename));
return ConvertRGBA8888ToYUV420(rgbaImage);
}
RGBAImage FillWithColor(uint32_t width,
uint32_t height,
uint8_t red,
uint8_t green,
uint8_t blue,
uint8_t alpha) {
RGBAImage out;
out.width = width;
out.height = height;
out.pixels.reserve(width * height * 4);
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
out.pixels.push_back(red);
out.pixels.push_back(green);
out.pixels.push_back(blue);
out.pixels.push_back(alpha);
}
}
return out;
}
namespace {
uint8_t Clamp(int x) {
if (x > 255) {
return 255;
}
if (x < 0) {
return 0;
}
return static_cast<uint8_t>(x);
}
// BT.601 with "Studio Swing" / narrow range.
void ConvertRGBA8888PixelToYUV(const uint8_t r,
const uint8_t g,
const uint8_t b,
uint8_t* out_y,
uint8_t* out_u,
uint8_t* out_v) {
const int r_int = static_cast<int>(r);
const int g_int = static_cast<int>(g);
const int b_int = static_cast<int>(b);
*out_y = Clamp(((( 66 * r_int) + (129 * g_int) + (25 * b_int) + 128) >> 8) + 16);
*out_u = Clamp((((-38 * r_int) - (74 * g_int) + (112 * b_int) + 128) >> 8) + 128);
*out_v = Clamp((((112 * r_int) - (94 * g_int) - (18 * b_int) + 128) >> 8) + 128);
}
} // namespace
YUV420Image ConvertRGBA8888ToYUV420(const RGBAImage& rgbaImage) {
const uint32_t w = rgbaImage.width;
const uint32_t h = rgbaImage.height;
YUV420Image yuvImage;
yuvImage.width = w;
yuvImage.height = h;
yuvImage.y.reserve(w * h);
yuvImage.u.reserve((w / 2) * (h / 2));
yuvImage.v.reserve((w / 2) * (h / 2));
const auto* input = rgbaImage.pixels.data();
for (uint32_t y = 0; y < h; y++) {
for (uint32_t x = 0; x < w; x++) {
const uint8_t r = *input;
++input;
const uint8_t g = *input;
++input;
const uint8_t b = *input;
++input;
// const uint8_t a = *input;
++input;
uint8_t pixelY;
uint8_t pixelU;
uint8_t pixelV;
ConvertRGBA8888PixelToYUV(r, g, b, &pixelY, &pixelU, &pixelV);
yuvImage.y.push_back(pixelY);
if ((x % 2 == 0) && (y % 2 == 0)) {
yuvImage.u.push_back(pixelU);
yuvImage.v.push_back(pixelV);
}
}
}
return yuvImage;
}
namespace {
bool PixelsAreSimilar(uint32_t pixel1, uint32_t pixel2) {
const uint8_t* pixel1_rgba = reinterpret_cast<const uint8_t*>(&pixel1);
const uint8_t* pixel2_rgba = reinterpret_cast<const uint8_t*>(&pixel2);
constexpr const uint32_t kDefaultTolerance = 2;
for (uint32_t channel = 0; channel < 4; channel++) {
const uint8_t pixel1_channel = pixel1_rgba[channel];
const uint8_t pixel2_channel = pixel2_rgba[channel];
if ((std::max(pixel1_channel, pixel2_channel) -
std::min(pixel1_channel, pixel2_channel)) > kDefaultTolerance) {
return false;
}
}
return true;
}
} // namespace
gfxstream::expected<Ok, std::vector<PixelDiff>> CompareImages(
const RGBAImage& expected,
const RGBAImage& actual) {
const uint32_t width = expected.width;
const uint32_t height = expected.height;
constexpr const uint32_t kMaxReportedIncorrectPixels = 10;
const auto* expectedPixels = reinterpret_cast<const uint32_t*>(expected.pixels.data());
const auto* actualPixels =reinterpret_cast<const uint32_t*>(actual.pixels.data());
std::vector<PixelDiff> pixelDiffs;
for (uint32_t y = 0; y < width; y++) {
for (uint32_t x = 0; x < height; x++) {
const uint32_t expectedPixel = expectedPixels[y * height + x];
const uint32_t actualPixel = actualPixels[y * height + x];
if (!PixelsAreSimilar(expectedPixel, actualPixel)) {
if (pixelDiffs.size() < kMaxReportedIncorrectPixels) {
const uint8_t* expectedPixelRgba = reinterpret_cast<const uint8_t*>(&expectedPixel);
const uint8_t* actualPixelRgba = reinterpret_cast<const uint8_t*>(&actualPixel);
pixelDiffs.push_back(PixelDiff{
.x = x,
.y = y,
.expectedR = expectedPixelRgba[0],
.expectedG = expectedPixelRgba[1],
.expectedB = expectedPixelRgba[2],
.expectedA = expectedPixelRgba[3],
.actualR = actualPixelRgba[0],
.actualG = actualPixelRgba[1],
.actualB = actualPixelRgba[2],
.actualA = actualPixelRgba[3],
});
}
}
}
}
if (!pixelDiffs.empty()) {
return gfxstream::unexpected(std::move(pixelDiffs));
}
return Ok{};
}
} // namespace gfxstream