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 15 : class LineTest : public CppUnit::TestFixture
37 : {
38 : private:
39 : BitmapDeviceSharedPtr mpDevice1bpp;
40 : BitmapDeviceSharedPtr mpDevice32bpp;
41 :
42 2 : void implTestBasicDiagonalLines(const BitmapDeviceSharedPtr& rDevice)
43 : {
44 2 : rDevice->clear(Color(0));
45 :
46 2 : const basegfx::B2IPoint aPt1(1,1);
47 4 : const basegfx::B2IPoint aPt2(9,9);
48 2 : const Color aCol(0xFFFFFFFF);
49 2 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
50 4 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
51 2 : rDevice->getPixel(aPt1) == aCol);
52 4 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
53 2 : rDevice->getPixel(aPt2) == aCol);
54 4 : const basegfx::B2IPoint aPt3(0,0);
55 4 : CPPUNIT_ASSERT_MESSAGE("topmost pixel not set",
56 2 : rDevice->getPixel(aPt3) != aCol);
57 4 : const basegfx::B2IPoint aPt4(10,10);
58 4 : CPPUNIT_ASSERT_MESSAGE("bottommost pixel not set",
59 2 : rDevice->getPixel(aPt4) != aCol);
60 :
61 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 9",
62 2 : countPixel( rDevice, aCol ) == 9);
63 :
64 2 : rDevice->drawLine( aPt2, aPt1, aCol, DrawMode_PAINT );
65 :
66 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
67 : "reversed paint is not 9",
68 4 : countPixel( rDevice, aCol ) == 9);
69 2 : }
70 :
71 2 : void implTestBasicHorizontalLines(const BitmapDeviceSharedPtr& rDevice)
72 : {
73 2 : rDevice->clear(Color(0));
74 :
75 2 : const basegfx::B2IPoint aPt1(10,10);
76 4 : const basegfx::B2IPoint aPt2(0,10);
77 2 : const Color aCol(0xFFFFFFFF);
78 2 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
79 4 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
80 2 : rDevice->getPixel(aPt1) == aCol);
81 4 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
82 2 : rDevice->getPixel(aPt2) == aCol);
83 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 11",
84 2 : countPixel( rDevice, aCol ) == 11);
85 :
86 2 : rDevice->clear(Color(0));
87 2 : rDevice->drawLine( aPt2, aPt1, aCol, DrawMode_PAINT );
88 4 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
89 2 : rDevice->getPixel(aPt1) == aCol);
90 4 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
91 2 : rDevice->getPixel(aPt2) == aCol);
92 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 11",
93 4 : countPixel( rDevice, aCol ) == 11);
94 2 : }
95 :
96 2 : void implTestBasicVerticalLines(const BitmapDeviceSharedPtr& rDevice)
97 : {
98 2 : rDevice->clear(Color(0));
99 :
100 2 : const basegfx::B2IPoint aPt1(1,1);
101 4 : const basegfx::B2IPoint aPt2(1,9);
102 2 : const Color aCol(0xFFFFFFFF);
103 2 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
104 4 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
105 2 : rDevice->getPixel(aPt1) == aCol);
106 4 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
107 2 : rDevice->getPixel(aPt2) == aCol);
108 4 : const basegfx::B2IPoint aPt3(0,0);
109 4 : CPPUNIT_ASSERT_MESSAGE("topmost pixel not set",
110 2 : rDevice->getPixel(aPt3) != aCol);
111 4 : const basegfx::B2IPoint aPt4(0,10);
112 4 : CPPUNIT_ASSERT_MESSAGE("bottommost pixel not set",
113 2 : rDevice->getPixel(aPt4) != aCol);
114 :
115 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 9",
116 4 : countPixel( rDevice, aCol ) == 9);
117 2 : }
118 :
119 : // test pixel rounding (should always tend towards start point of
120 : // the line)
121 2 : void implTestTieBreaking(const BitmapDeviceSharedPtr& rDevice)
122 : {
123 2 : rDevice->clear(Color(0));
124 :
125 2 : const basegfx::B2IPoint aPt1(1,1);
126 4 : const basegfx::B2IPoint aPt2(3,2);
127 2 : const Color aCol(0xFFFFFFFF);
128 2 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
129 4 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
130 2 : rDevice->getPixel(aPt1) == aCol);
131 4 : CPPUNIT_ASSERT_MESSAGE("second pixel set",
132 2 : rDevice->getPixel(basegfx::B2IPoint(2,1)) == aCol);
133 4 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
134 2 : rDevice->getPixel(aPt2) == aCol);
135 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
136 : "reversed paint is not 3",
137 2 : countPixel( rDevice, aCol ) == 3);
138 :
139 2 : rDevice->drawLine( aPt2, aPt1, aCol, DrawMode_PAINT );
140 4 : CPPUNIT_ASSERT_MESSAGE("alternate second pixel set",
141 2 : rDevice->getPixel(basegfx::B2IPoint(2,2)) == aCol);
142 :
143 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
144 : "reversed paint is not 4",
145 4 : countPixel( rDevice, aCol ) == 4);
146 2 : }
147 :
148 : public:
149 5 : void setUp()
150 : {
151 5 : const basegfx::B2ISize aSize(11,11);
152 10 : mpDevice1bpp = createBitmapDevice( aSize,
153 : true,
154 5 : Format::ONE_BIT_MSB_PAL );
155 10 : mpDevice32bpp = createBitmapDevice( aSize,
156 : true,
157 10 : Format::THIRTYTWO_BIT_TC_MASK );
158 5 : }
159 :
160 1 : void testCornerCases()
161 : {
162 1 : const basegfx::B2ISize aSize(1,1);
163 : BitmapDeviceSharedPtr pDevice = createBitmapDevice(
164 : aSize,
165 : true,
166 2 : Format::ONE_BIT_MSB_PAL );
167 :
168 2 : const basegfx::B2IPoint aPt1(0,0);
169 2 : const basegfx::B2IPoint aPt2(10,10);
170 2 : CPPUNIT_ASSERT_MESSAGE("only pixel cleared",
171 1 : pDevice->getPixelData(aPt1) == 0);
172 :
173 1 : const Color aCol(0xFFFFFFFF);
174 1 : pDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
175 2 : CPPUNIT_ASSERT_MESSAGE("only pixel set",
176 1 : pDevice->getPixelData(aPt1) == 1);
177 :
178 2 : const basegfx::B2ISize aSize2(1,0);
179 2 : pDevice = createBitmapDevice(
180 : aSize2,
181 : true,
182 1 : Format::ONE_BIT_MSB_PAL );
183 :
184 2 : CPPUNIT_ASSERT_MESSAGE("only pixel cleared",
185 1 : pDevice->getPixelData(aPt1) == 0);
186 :
187 1 : pDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
188 2 : CPPUNIT_ASSERT_MESSAGE("only pixel still cleared",
189 2 : pDevice->getPixelData(aPt1) == 0);
190 1 : }
191 :
192 1 : void testBasicDiagonalLines()
193 : {
194 1 : implTestBasicDiagonalLines( mpDevice1bpp );
195 1 : implTestBasicDiagonalLines( mpDevice32bpp );
196 1 : }
197 :
198 1 : void testBasicHorizontalLines()
199 : {
200 1 : implTestBasicHorizontalLines( mpDevice1bpp );
201 1 : implTestBasicHorizontalLines( mpDevice32bpp );
202 1 : }
203 :
204 1 : void testBasicVerticalLines()
205 : {
206 1 : implTestBasicVerticalLines( mpDevice1bpp );
207 1 : implTestBasicVerticalLines( mpDevice32bpp );
208 1 : }
209 :
210 : // test pixel rounding (should always tend towards start point of
211 : // the line)
212 1 : void testTieBreaking()
213 : {
214 1 : implTestTieBreaking( mpDevice1bpp );
215 1 : implTestTieBreaking( mpDevice32bpp );
216 1 : }
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 2 : CPPUNIT_TEST_SUITE(LineTest);
223 1 : CPPUNIT_TEST(testCornerCases);
224 1 : CPPUNIT_TEST(testBasicDiagonalLines);
225 1 : CPPUNIT_TEST(testBasicHorizontalLines);
226 1 : CPPUNIT_TEST(testBasicVerticalLines);
227 1 : CPPUNIT_TEST(testTieBreaking);
228 2 : CPPUNIT_TEST_SUITE_END();
229 : };
230 :
231 1 : CPPUNIT_TEST_SUITE_REGISTRATION(LineTest);
232 3 : }
233 :
234 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|