1 // Copyright 2020 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/fxge/dib/cfx_dibbase.h"
6
7 #include "core/fxcrt/fx_coordinates.h"
8 #include "core/fxge/dib/cfx_dibitmap.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 struct Input {
14 CFX_Point src_top_left;
15 CFX_Size src_size;
16 CFX_Point dest_top_left;
17 CFX_Size overlap_size;
18 };
19
20 struct Output {
21 CFX_Point src_top_left;
22 CFX_Point dest_top_left;
23 CFX_Size overlap_size;
24 };
25
RunOverlapRectTest(const CFX_DIBitmap * bitmap,const Input & input,const Output * expected_output)26 void RunOverlapRectTest(const CFX_DIBitmap* bitmap,
27 const Input& input,
28 const Output* expected_output) {
29 // Initialize in-out parameters.
30 int src_left = input.src_top_left.x;
31 int src_top = input.src_top_left.y;
32 int dest_left = input.dest_top_left.x;
33 int dest_top = input.dest_top_left.y;
34 int overlap_width = input.overlap_size.width;
35 int overlap_height = input.overlap_size.height;
36
37 bool success = bitmap->GetOverlapRect(
38 dest_left, dest_top, overlap_width, overlap_height, input.src_size.width,
39 input.src_size.height, src_left, src_top,
40 /*pClipRgn=*/nullptr);
41 if (success == !expected_output) {
42 ADD_FAILURE();
43 return;
44 }
45
46 if (expected_output) {
47 EXPECT_EQ(expected_output->src_top_left.x, src_left);
48 EXPECT_EQ(expected_output->src_top_left.y, src_top);
49 EXPECT_EQ(expected_output->dest_top_left.x, dest_left);
50 EXPECT_EQ(expected_output->dest_top_left.y, dest_top);
51 EXPECT_EQ(expected_output->overlap_size.width, overlap_width);
52 EXPECT_EQ(expected_output->overlap_size.height, overlap_height);
53 }
54 }
55
56 } // namespace
57
TEST(CFX_DIBBaseTest,GetOverlapRectTrivialOverlap)58 TEST(CFX_DIBBaseTest, GetOverlapRectTrivialOverlap) {
59 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
60 EXPECT_TRUE(bitmap->Create(400, 300, FXDIB_Format::k1bppRgb));
61
62 const Input kInput = {/*src_top_left=*/{0, 0}, /*src_size=*/{400, 300},
63 /*dest_top_left=*/{0, 0},
64 /*overlap_size=*/{400, 300}};
65 const Output kExpectedOutput = {/*src_top_left=*/{0, 0},
66 /*dest_top_left=*/{0, 0},
67 /*overlap_size=*/{400, 300}};
68 RunOverlapRectTest(bitmap.Get(), kInput, &kExpectedOutput);
69 }
70
TEST(CFX_DIBBaseTest,GetOverlapRectOverlapNoLimit)71 TEST(CFX_DIBBaseTest, GetOverlapRectOverlapNoLimit) {
72 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
73 EXPECT_TRUE(bitmap->Create(400, 300, FXDIB_Format::k1bppRgb));
74
75 const Input kInput = {/*src_top_left=*/{35, 41}, /*src_size=*/{400, 300},
76 /*dest_top_left=*/{123, 137},
77 /*overlap_size=*/{200, 100}};
78 const Output kExpectedOutput = {/*src_top_left=*/{35, 41},
79 /*dest_top_left=*/{123, 137},
80 /*overlap_size=*/{200, 100}};
81 RunOverlapRectTest(bitmap.Get(), kInput, &kExpectedOutput);
82 }
83
TEST(CFX_DIBBaseTest,GetOverlapRectOverlapLimitedBySource)84 TEST(CFX_DIBBaseTest, GetOverlapRectOverlapLimitedBySource) {
85 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
86 EXPECT_TRUE(bitmap->Create(400, 300, FXDIB_Format::k1bppRgb));
87
88 const Input kInput = {/*src_top_left=*/{141, 154}, /*src_size=*/{400, 300},
89 /*dest_top_left=*/{35, 41},
90 /*overlap_size=*/{270, 160}};
91 const Output kExpectedOutput = {/*src_top_left=*/{141, 154},
92 /*dest_top_left=*/{35, 41},
93 /*overlap_size=*/{259, 146}};
94 RunOverlapRectTest(bitmap.Get(), kInput, &kExpectedOutput);
95 }
96
TEST(CFX_DIBBaseTest,GetOverlapRectOverlapLimitedByDestination)97 TEST(CFX_DIBBaseTest, GetOverlapRectOverlapLimitedByDestination) {
98 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
99 EXPECT_TRUE(bitmap->Create(400, 300, FXDIB_Format::k1bppRgb));
100
101 const Input kInput = {/*src_top_left=*/{35, 41}, /*src_size=*/{400, 300},
102 /*dest_top_left=*/{123, 137},
103 /*overlap_size=*/{280, 170}};
104 const Output kExpectedOutput = {/*src_top_left=*/{35, 41},
105 /*dest_top_left=*/{123, 137},
106 /*overlap_size=*/{277, 163}};
107 RunOverlapRectTest(bitmap.Get(), kInput, &kExpectedOutput);
108 }
109
TEST(CFX_DIBBaseTest,GetOverlapRectBadInputs)110 TEST(CFX_DIBBaseTest, GetOverlapRectBadInputs) {
111 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
112 EXPECT_TRUE(bitmap->Create(400, 300, FXDIB_Format::k1bppRgb));
113
114 const Input kEmptyInputs[] = {
115 // Empty source rect.
116 {/*src_top_left=*/{0, 0}, /*src_size=*/{0, 0},
117 /*dest_top_left=*/{0, 0},
118 /*overlap_size=*/{400, 300}},
119 // Empty overlap size.
120 {/*src_top_left=*/{0, 0}, /*src_size=*/{400, 300},
121 /*dest_top_left=*/{0, 0},
122 /*overlap_size=*/{0, 0}},
123 // Source out of bounds on x-axis.
124 {/*src_top_left=*/{-400, 0}, /*src_size=*/{400, 300},
125 /*dest_top_left=*/{0, 0},
126 /*overlap_size=*/{400, 300}},
127 };
128 for (const Input& input : kEmptyInputs)
129 RunOverlapRectTest(bitmap.Get(), input, /*expected_output=*/nullptr);
130
131 const Input kOutOfBoundInputs[] = {
132 // Source out of bounds on x-axis.
133 {/*src_top_left=*/{400, 0}, /*src_size=*/{400, 300},
134 /*dest_top_left=*/{0, 0},
135 /*overlap_size=*/{400, 300}},
136 // Source out of bounds on y-axis.
137 {/*src_top_left=*/{0, 300}, /*src_size=*/{400, 300},
138 /*dest_top_left=*/{0, 0},
139 /*overlap_size=*/{400, 300}},
140 // Source out of bounds on y-axis.
141 {/*src_top_left=*/{0, -300}, /*src_size=*/{400, 300},
142 /*dest_top_left=*/{0, 0},
143 /*overlap_size=*/{400, 300}},
144 // Destination out of bounds on x-axis.
145 {/*src_top_left=*/{0, 0}, /*src_size=*/{400, 300},
146 /*dest_top_left=*/{-400, 0},
147 /*overlap_size=*/{400, 300}},
148 // Destination out of bounds on x-axis.
149 {/*src_top_left=*/{0, 0}, /*src_size=*/{400, 300},
150 /*dest_top_left=*/{400, 0},
151 /*overlap_size=*/{400, 300}},
152 // Destination out of bounds on y-axis.
153 {/*src_top_left=*/{0, 0}, /*src_size=*/{400, 300},
154 /*dest_top_left=*/{0, -300},
155 /*overlap_size=*/{400, 300}},
156 // Destination out of bounds on y-axis.
157 {/*src_top_left=*/{0, 0}, /*src_size=*/{400, 300},
158 /*dest_top_left=*/{0, 300},
159 /*overlap_size=*/{400, 300}},
160 };
161 for (const Input& input : kOutOfBoundInputs)
162 RunOverlapRectTest(bitmap.Get(), input, /*expected_output=*/nullptr);
163 }
164