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() SAL_OVERRIDE
150 : {
151 5 : const basegfx::B2ISize aSize(11,11);
152 10 : mpDevice1bpp = createBitmapDevice( aSize,
153 : true,
154 : Format::OneBitMsbPal,
155 5 : basebmp::getBitmapDeviceStrideForWidth(Format::OneBitMsbPal, aSize.getX()) );
156 10 : mpDevice32bpp = createBitmapDevice( aSize,
157 : true,
158 : Format::ThirtyTwoBitTcMaskBGRA,
159 10 : basebmp::getBitmapDeviceStrideForWidth(Format::ThirtyTwoBitTcMaskBGRA, aSize.getX()) );
160 5 : }
161 :
162 1 : void testCornerCases()
163 : {
164 1 : const basegfx::B2ISize aSize(1,1);
165 : BitmapDeviceSharedPtr pDevice = createBitmapDevice(
166 : aSize,
167 : true,
168 : Format::OneBitMsbPal,
169 2 : basebmp::getBitmapDeviceStrideForWidth(Format::OneBitMsbPal, aSize.getX()) );
170 :
171 2 : const basegfx::B2IPoint aPt1(0,0);
172 2 : const basegfx::B2IPoint aPt2(10,10);
173 2 : CPPUNIT_ASSERT_MESSAGE("only pixel cleared",
174 1 : pDevice->getPixelData(aPt1) == 0);
175 :
176 1 : const Color aCol(0xFFFFFFFF);
177 1 : pDevice->drawLine( aPt1, aPt2, aCol, DrawMode::Paint );
178 2 : CPPUNIT_ASSERT_MESSAGE("only pixel set",
179 1 : pDevice->getPixelData(aPt1) == 1);
180 :
181 2 : const basegfx::B2ISize aSize2(1,0);
182 2 : pDevice = createBitmapDevice(
183 : aSize2,
184 : true,
185 : Format::OneBitMsbPal,
186 1 : basebmp::getBitmapDeviceStrideForWidth(Format::OneBitMsbPal, aSize.getX()));
187 :
188 2 : CPPUNIT_ASSERT_MESSAGE("only pixel cleared",
189 1 : pDevice->getPixelData(aPt1) == 0);
190 :
191 1 : pDevice->drawLine( aPt1, aPt2, aCol, DrawMode::Paint );
192 2 : CPPUNIT_ASSERT_MESSAGE("only pixel still cleared",
193 2 : pDevice->getPixelData(aPt1) == 0);
194 1 : }
195 :
196 1 : void testBasicDiagonalLines()
197 : {
198 1 : implTestBasicDiagonalLines( mpDevice1bpp );
199 1 : implTestBasicDiagonalLines( mpDevice32bpp );
200 1 : }
201 :
202 1 : void testBasicHorizontalLines()
203 : {
204 1 : implTestBasicHorizontalLines( mpDevice1bpp );
205 1 : implTestBasicHorizontalLines( mpDevice32bpp );
206 1 : }
207 :
208 1 : void testBasicVerticalLines()
209 : {
210 1 : implTestBasicVerticalLines( mpDevice1bpp );
211 1 : implTestBasicVerticalLines( mpDevice32bpp );
212 1 : }
213 :
214 : // test pixel rounding (should always tend towards start point of
215 : // the line)
216 1 : void testTieBreaking()
217 : {
218 1 : implTestTieBreaking( mpDevice1bpp );
219 1 : implTestTieBreaking( mpDevice32bpp );
220 1 : }
221 :
222 : // Change the following lines only, if you add, remove or rename
223 : // member functions of the current class,
224 : // because these macros are need by auto register mechanism.
225 :
226 2 : CPPUNIT_TEST_SUITE(LineTest);
227 1 : CPPUNIT_TEST(testCornerCases);
228 1 : CPPUNIT_TEST(testBasicDiagonalLines);
229 1 : CPPUNIT_TEST(testBasicHorizontalLines);
230 1 : CPPUNIT_TEST(testBasicVerticalLines);
231 1 : CPPUNIT_TEST(testTieBreaking);
232 5 : CPPUNIT_TEST_SUITE_END();
233 : };
234 :
235 1 : CPPUNIT_TEST_SUITE_REGISTRATION(LineTest);
236 3 : }
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|