Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "cppunit/TestAssert.h"
21 : : #include "cppunit/TestFixture.h"
22 : : #include "cppunit/extensions/HelperMacros.h"
23 : :
24 : : #include <basegfx/vector/b2isize.hxx>
25 : : #include <basegfx/point/b2ipoint.hxx>
26 : :
27 : : #include <basebmp/color.hxx>
28 : : #include <basebmp/scanlineformats.hxx>
29 : : #include <basebmp/bitmapdevice.hxx>
30 : : #include "tools.hxx"
31 : :
32 : : using namespace ::basebmp;
33 : :
34 : : namespace
35 : : {
36 [ + - ][ + - ]: 75 : class LineTest : public CppUnit::TestFixture
[ - + ][ + - ]
[ + - ]
37 : : {
38 : : private:
39 : : BitmapDeviceSharedPtr mpDevice1bpp;
40 : : BitmapDeviceSharedPtr mpDevice32bpp;
41 : :
42 : 10 : void implTestBasicDiagonalLines(const BitmapDeviceSharedPtr& rDevice)
43 : : {
44 [ + - ]: 10 : rDevice->clear(Color(0));
45 : :
46 : 10 : const basegfx::B2IPoint aPt1(1,1);
47 : 10 : const basegfx::B2IPoint aPt2(9,9);
48 : 10 : const Color aCol(0xFFFFFFFF);
49 [ + - ]: 10 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
50 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
51 [ + - ]: 10 : rDevice->getPixel(aPt1) == aCol);
52 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
53 [ + - ]: 10 : rDevice->getPixel(aPt2) == aCol);
54 : 10 : const basegfx::B2IPoint aPt3(0,0);
55 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("topmost pixel not set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
56 [ + - ]: 10 : rDevice->getPixel(aPt3) != aCol);
57 : 10 : const basegfx::B2IPoint aPt4(10,10);
58 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("bottommost pixel not set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
59 [ + - ]: 10 : rDevice->getPixel(aPt4) != aCol);
60 : :
61 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 9",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
62 [ + - ]: 10 : countPixel( rDevice, aCol ) == 9);
63 : :
64 [ + - ]: 10 : rDevice->drawLine( aPt2, aPt1, aCol, DrawMode_PAINT );
65 : :
66 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
67 : : "reversed paint is not 9",
68 [ + - ]: 20 : countPixel( rDevice, aCol ) == 9);
69 : 10 : }
70 : :
71 : 10 : void implTestBasicHorizontalLines(const BitmapDeviceSharedPtr& rDevice)
72 : : {
73 [ + - ]: 10 : rDevice->clear(Color(0));
74 : :
75 : 10 : const basegfx::B2IPoint aPt1(10,10);
76 : 10 : const basegfx::B2IPoint aPt2(0,10);
77 : 10 : const Color aCol(0xFFFFFFFF);
78 [ + - ]: 10 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
79 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
80 [ + - ]: 10 : rDevice->getPixel(aPt1) == aCol);
81 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
82 [ + - ]: 10 : rDevice->getPixel(aPt2) == aCol);
83 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 11",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
84 [ + - ]: 10 : countPixel( rDevice, aCol ) == 11);
85 : :
86 [ + - ]: 10 : rDevice->clear(Color(0));
87 [ + - ]: 10 : rDevice->drawLine( aPt2, aPt1, aCol, DrawMode_PAINT );
88 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
89 [ + - ]: 10 : rDevice->getPixel(aPt1) == aCol);
90 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
91 [ + - ]: 10 : rDevice->getPixel(aPt2) == aCol);
92 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 11",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
93 [ + - ]: 20 : countPixel( rDevice, aCol ) == 11);
94 : 10 : }
95 : :
96 : 10 : void implTestBasicVerticalLines(const BitmapDeviceSharedPtr& rDevice)
97 : : {
98 [ + - ]: 10 : rDevice->clear(Color(0));
99 : :
100 : 10 : const basegfx::B2IPoint aPt1(1,1);
101 : 10 : const basegfx::B2IPoint aPt2(1,9);
102 : 10 : const Color aCol(0xFFFFFFFF);
103 [ + - ]: 10 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
104 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
105 [ + - ]: 10 : rDevice->getPixel(aPt1) == aCol);
106 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
107 [ + - ]: 10 : rDevice->getPixel(aPt2) == aCol);
108 : 10 : const basegfx::B2IPoint aPt3(0,0);
109 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("topmost pixel not set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
110 [ + - ]: 10 : rDevice->getPixel(aPt3) != aCol);
111 : 10 : const basegfx::B2IPoint aPt4(0,10);
112 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("bottommost pixel not set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
113 [ + - ]: 10 : rDevice->getPixel(aPt4) != aCol);
114 : :
115 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 9",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
116 [ + - ]: 20 : countPixel( rDevice, aCol ) == 9);
117 : 10 : }
118 : :
119 : : // test pixel rounding (should always tend towards start point of
120 : : // the line)
121 : 10 : void implTestTieBreaking(const BitmapDeviceSharedPtr& rDevice)
122 : : {
123 [ + - ]: 10 : rDevice->clear(Color(0));
124 : :
125 : 10 : const basegfx::B2IPoint aPt1(1,1);
126 : 10 : const basegfx::B2IPoint aPt2(3,2);
127 : 10 : const Color aCol(0xFFFFFFFF);
128 [ + - ]: 10 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
129 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
130 [ + - ]: 10 : rDevice->getPixel(aPt1) == aCol);
131 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("second pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
132 [ + - ]: 10 : rDevice->getPixel(basegfx::B2IPoint(2,1)) == aCol);
133 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
134 [ + - ]: 10 : rDevice->getPixel(aPt2) == aCol);
135 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
136 : : "reversed paint is not 3",
137 [ + - ]: 10 : countPixel( rDevice, aCol ) == 3);
138 : :
139 [ + - ]: 10 : rDevice->drawLine( aPt2, aPt1, aCol, DrawMode_PAINT );
140 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("alternate second pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
141 [ + - ]: 10 : rDevice->getPixel(basegfx::B2IPoint(2,2)) == aCol);
142 : :
143 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
144 : : "reversed paint is not 4",
145 [ + - ]: 20 : countPixel( rDevice, aCol ) == 4);
146 : 10 : }
147 : :
148 : : public:
149 : 25 : void setUp()
150 : : {
151 : 25 : const basegfx::B2ISize aSize(11,11);
152 : : mpDevice1bpp = createBitmapDevice( aSize,
153 : : true,
154 [ + - ][ + - ]: 25 : Format::ONE_BIT_MSB_PAL );
[ + - ]
155 : : mpDevice32bpp = createBitmapDevice( aSize,
156 : : true,
157 [ + - ][ + - ]: 25 : Format::THIRTYTWO_BIT_TC_MASK );
[ + - ]
158 : 25 : }
159 : :
160 : 5 : void testCornerCases()
161 : : {
162 : 5 : const basegfx::B2ISize aSize(1,1);
163 : : BitmapDeviceSharedPtr pDevice = createBitmapDevice(
164 : : aSize,
165 : : true,
166 [ + - ]: 5 : Format::ONE_BIT_MSB_PAL );
167 : :
168 : 5 : const basegfx::B2IPoint aPt1(0,0);
169 : 5 : const basegfx::B2IPoint aPt2(10,10);
170 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE("only pixel cleared",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
171 [ + - ]: 5 : pDevice->getPixelData(aPt1) == 0);
172 : :
173 : 5 : const Color aCol(0xFFFFFFFF);
174 [ + - ]: 5 : pDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
175 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE("only pixel set",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
176 [ + - ]: 5 : pDevice->getPixelData(aPt1) == 1);
177 : :
178 : 5 : const basegfx::B2ISize aSize2(1,0);
179 : : pDevice = createBitmapDevice(
180 : : aSize2,
181 : : true,
182 [ + - ][ + - ]: 5 : Format::ONE_BIT_MSB_PAL );
[ + - ]
183 : :
184 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE("only pixel cleared",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
185 [ + - ]: 5 : pDevice->getPixelData(aPt1) == 0);
186 : :
187 [ + - ]: 5 : pDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
188 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE("only pixel still cleared",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
189 [ + - + - ]: 10 : pDevice->getPixelData(aPt1) == 0);
190 : 5 : }
191 : :
192 : 5 : void testBasicDiagonalLines()
193 : : {
194 : 5 : implTestBasicDiagonalLines( mpDevice1bpp );
195 : 5 : implTestBasicDiagonalLines( mpDevice32bpp );
196 : 5 : }
197 : :
198 : 5 : void testBasicHorizontalLines()
199 : : {
200 : 5 : implTestBasicHorizontalLines( mpDevice1bpp );
201 : 5 : implTestBasicHorizontalLines( mpDevice32bpp );
202 : 5 : }
203 : :
204 : 5 : void testBasicVerticalLines()
205 : : {
206 : 5 : implTestBasicVerticalLines( mpDevice1bpp );
207 : 5 : implTestBasicVerticalLines( mpDevice32bpp );
208 : 5 : }
209 : :
210 : : // test pixel rounding (should always tend towards start point of
211 : : // the line)
212 : 5 : void testTieBreaking()
213 : : {
214 : 5 : implTestTieBreaking( mpDevice1bpp );
215 : 5 : implTestTieBreaking( mpDevice32bpp );
216 : 5 : }
217 : :
218 : : // Change the following lines only, if you add, remove or rename
219 : : // member functions of the current class,
220 : : // because these macros are need by auto register mechanism.
221 : :
222 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(LineTest);
[ + - ][ + - ]
[ # # ]
223 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testCornerCases);
[ + - ][ + - ]
[ + - ][ + - ]
224 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testBasicDiagonalLines);
[ + - ][ + - ]
[ + - ][ + - ]
225 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testBasicHorizontalLines);
[ + - ][ + - ]
[ + - ][ + - ]
226 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testBasicVerticalLines);
[ + - ][ + - ]
[ + - ][ + - ]
227 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testTieBreaking);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
228 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
229 : : };
230 : :
231 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(LineTest);
232 [ + - ][ + - ]: 15 : }
233 : :
234 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|