1 //
2 // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // AstcDecompressorTestUtils.h: Utility functions for ASTC decompression tests
7
8 #include <vector>
9 #include "common/debug.h"
10
11 namespace testing
12 {
13 struct Rgba
14 {
15 uint8_t r, g, b, a;
16 bool operator==(const Rgba &o) const { return r == o.r && g == o.g && b == o.b && a == o.a; }
17 };
18 static_assert(sizeof(Rgba) == 4, "Rgba struct isn't 4 bytes");
19
20 // Creates a checkerboard image of a given size. The top left pixel will be black, and the remaining
21 // pixels will alternate between black and white.
22 // Note that both width and height must be multiples of 8
makeCheckerboard(int width,int height)23 std::vector<Rgba> makeCheckerboard(int width, int height)
24 {
25 ASSERT(width % 8 == 0 && height % 8 == 0);
26
27 const Rgba white = {0xFF, 0xFF, 0xFF, 0xFF};
28 const Rgba black = {0, 0, 0, 0xFF};
29 const Rgba colors[] = {white, black};
30
31 std::vector<Rgba> result;
32 result.reserve(width * height);
33
34 int colorIndex = 0;
35 for (int y = 0; y < height; ++y)
36 {
37 for (int x = 0; x < width; ++x)
38 {
39 result.push_back(colors[colorIndex]);
40 colorIndex ^= 1UL; // toggle the last bit, so we alternate between 0 and 1;
41 }
42 colorIndex ^= 1UL;
43 }
44 return result;
45 }
46
47 // Similar to makeCheckerboard(), but returns an ASTC-encoded image instead, with 8x8 block size.
makeAstcCheckerboard(int width,int height)48 std::vector<uint8_t> makeAstcCheckerboard(int width, int height)
49 {
50 ASSERT(width % 8 == 0 && height % 8 == 0);
51
52 // One 8x8 ASTC block with a checkerboard pattern (alternating black and white pixels)
53 const std::vector<uint8_t> block = {0x44, 0x05, 0x00, 0xfe, 0x01, 0x00, 0x00, 0x00,
54 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa};
55
56 const int numBlocks = width * height / (8 * 8);
57
58 std::vector<uint8_t> result;
59 result.reserve(numBlocks * block.size());
60 for (int i = 0; i < numBlocks; ++i)
61 {
62 result.insert(result.end(), block.begin(), block.end());
63 }
64
65 return result;
66 }
67
68 } // namespace testing