1 // Copyright 2021 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/utils/segmentation_map.h"
16
17 #include <cstdint>
18
19 #include "gtest/gtest.h"
20
21 namespace libgav1 {
22 namespace {
23
TEST(SegmentationMapTest,Clear)24 TEST(SegmentationMapTest, Clear) {
25 constexpr int32_t kRows4x4 = 60;
26 constexpr int32_t kColumns4x4 = 80;
27 SegmentationMap segmentation_map;
28 ASSERT_TRUE(segmentation_map.Allocate(kRows4x4, kColumns4x4));
29
30 segmentation_map.Clear();
31 for (int row4x4 = 0; row4x4 < kRows4x4; ++row4x4) {
32 for (int column4x4 = 0; column4x4 < kColumns4x4; ++column4x4) {
33 EXPECT_EQ(segmentation_map.segment_id(row4x4, column4x4), 0);
34 }
35 }
36 }
37
TEST(SegmentationMapTest,FillBlock)38 TEST(SegmentationMapTest, FillBlock) {
39 constexpr int32_t kRows4x4 = 60;
40 constexpr int32_t kColumns4x4 = 80;
41 SegmentationMap segmentation_map;
42 ASSERT_TRUE(segmentation_map.Allocate(kRows4x4, kColumns4x4));
43
44 // Fill the whole image with 2.
45 segmentation_map.FillBlock(0, 0, kColumns4x4, kRows4x4, 2);
46 // Fill a block with 1.
47 constexpr int kBlockWidth4x4 = 10;
48 constexpr int kBlockHeight4x4 = 20;
49 segmentation_map.FillBlock(4, 6, kBlockWidth4x4, kBlockHeight4x4, 1);
50 for (int row4x4 = 0; row4x4 < kRows4x4; ++row4x4) {
51 for (int column4x4 = 0; column4x4 < kColumns4x4; ++column4x4) {
52 if (4 <= row4x4 && row4x4 < 4 + kBlockHeight4x4 && 6 <= column4x4 &&
53 column4x4 < 6 + kBlockWidth4x4) {
54 // Inside the block.
55 EXPECT_EQ(segmentation_map.segment_id(row4x4, column4x4), 1);
56 } else {
57 // Outside the block.
58 EXPECT_EQ(segmentation_map.segment_id(row4x4, column4x4), 2);
59 }
60 }
61 }
62 }
63
TEST(SegmentationMapTest,CopyFrom)64 TEST(SegmentationMapTest, CopyFrom) {
65 constexpr int32_t kRows4x4 = 60;
66 constexpr int32_t kColumns4x4 = 80;
67 SegmentationMap segmentation_map;
68 ASSERT_TRUE(segmentation_map.Allocate(kRows4x4, kColumns4x4));
69
70 // Split the segmentation map into four blocks of equal size.
71 constexpr int kBlockWidth4x4 = 40;
72 constexpr int kBlockHeight4x4 = 30;
73 segmentation_map.FillBlock(0, 0, kBlockWidth4x4, kBlockHeight4x4, 1);
74 segmentation_map.FillBlock(0, kBlockWidth4x4, kBlockWidth4x4, kBlockHeight4x4,
75 2);
76 segmentation_map.FillBlock(kBlockHeight4x4, 0, kBlockWidth4x4,
77 kBlockHeight4x4, 3);
78 segmentation_map.FillBlock(kBlockHeight4x4, kBlockWidth4x4, kBlockWidth4x4,
79 kBlockHeight4x4, 4);
80
81 SegmentationMap segmentation_map2;
82 ASSERT_TRUE(segmentation_map2.Allocate(kRows4x4, kColumns4x4));
83 segmentation_map2.CopyFrom(segmentation_map);
84
85 for (int row4x4 = 0; row4x4 < kBlockHeight4x4; ++row4x4) {
86 for (int column4x4 = 0; column4x4 < kBlockWidth4x4; ++column4x4) {
87 EXPECT_EQ(segmentation_map.segment_id(row4x4, column4x4), 1);
88 EXPECT_EQ(segmentation_map2.segment_id(row4x4, column4x4), 1);
89 }
90 }
91 for (int row4x4 = 0; row4x4 < kBlockHeight4x4; ++row4x4) {
92 for (int column4x4 = 0; column4x4 < kBlockWidth4x4; ++column4x4) {
93 EXPECT_EQ(segmentation_map.segment_id(row4x4, kBlockWidth4x4 + column4x4),
94 2);
95 EXPECT_EQ(
96 segmentation_map2.segment_id(row4x4, kBlockWidth4x4 + column4x4), 2);
97 }
98 }
99 for (int row4x4 = 0; row4x4 < kBlockHeight4x4; ++row4x4) {
100 for (int column4x4 = 0; column4x4 < kBlockWidth4x4; ++column4x4) {
101 EXPECT_EQ(
102 segmentation_map.segment_id(kBlockHeight4x4 + row4x4, column4x4), 3);
103 EXPECT_EQ(
104 segmentation_map2.segment_id(kBlockHeight4x4 + row4x4, column4x4), 3);
105 }
106 }
107 for (int row4x4 = 0; row4x4 < kBlockHeight4x4; ++row4x4) {
108 for (int column4x4 = 0; column4x4 < kBlockWidth4x4; ++column4x4) {
109 EXPECT_EQ(segmentation_map.segment_id(kBlockHeight4x4 + row4x4,
110 kBlockWidth4x4 + column4x4),
111 4);
112 EXPECT_EQ(segmentation_map2.segment_id(kBlockHeight4x4 + row4x4,
113 kBlockWidth4x4 + column4x4),
114 4);
115 }
116 }
117 }
118
119 } // namespace
120 } // namespace libgav1
121