1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <cppunit/TestAssert.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
24 #include <basegfx/vector/b2isize.hxx>
25 #include <basegfx/point/b2ipoint.hxx>
27 #include <basebmp/color.hxx>
28 #include <basebmp/scanlineformats.hxx>
29 #include <basebmp/bitmapdevice.hxx>
32 using namespace ::basebmp
;
36 class LineTest
: public CppUnit::TestFixture
39 BitmapDeviceSharedPtr mpDevice1bpp
;
40 BitmapDeviceSharedPtr mpDevice32bpp
;
42 void implTestBasicDiagonalLines(const BitmapDeviceSharedPtr
& rDevice
)
44 rDevice
->clear(Color(0));
46 const basegfx::B2IPoint
aPt1(1,1);
47 const basegfx::B2IPoint
aPt2(9,9);
48 const Color
aCol(0xFFFFFFFF);
49 rDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_PAINT
);
50 CPPUNIT_ASSERT_MESSAGE("first pixel set",
51 rDevice
->getPixel(aPt1
) == aCol
);
52 CPPUNIT_ASSERT_MESSAGE("last pixel set",
53 rDevice
->getPixel(aPt2
) == aCol
);
54 const basegfx::B2IPoint
aPt3(0,0);
55 CPPUNIT_ASSERT_MESSAGE("topmost pixel not set",
56 rDevice
->getPixel(aPt3
) != aCol
);
57 const basegfx::B2IPoint
aPt4(10,10);
58 CPPUNIT_ASSERT_MESSAGE("bottommost pixel not set",
59 rDevice
->getPixel(aPt4
) != aCol
);
61 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 9",
62 countPixel( rDevice
, aCol
) == 9);
64 rDevice
->drawLine( aPt2
, aPt1
, aCol
, DrawMode_PAINT
);
66 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
67 "reversed paint is not 9",
68 countPixel( rDevice
, aCol
) == 9);
71 void implTestBasicHorizontalLines(const BitmapDeviceSharedPtr
& rDevice
)
73 rDevice
->clear(Color(0));
75 const basegfx::B2IPoint
aPt1(10,10);
76 const basegfx::B2IPoint
aPt2(0,10);
77 const Color
aCol(0xFFFFFFFF);
78 rDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_PAINT
);
79 CPPUNIT_ASSERT_MESSAGE("first pixel set",
80 rDevice
->getPixel(aPt1
) == aCol
);
81 CPPUNIT_ASSERT_MESSAGE("last pixel set",
82 rDevice
->getPixel(aPt2
) == aCol
);
83 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 11",
84 countPixel( rDevice
, aCol
) == 11);
86 rDevice
->clear(Color(0));
87 rDevice
->drawLine( aPt2
, aPt1
, aCol
, DrawMode_PAINT
);
88 CPPUNIT_ASSERT_MESSAGE("first pixel set",
89 rDevice
->getPixel(aPt1
) == aCol
);
90 CPPUNIT_ASSERT_MESSAGE("last pixel set",
91 rDevice
->getPixel(aPt2
) == aCol
);
92 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 11",
93 countPixel( rDevice
, aCol
) == 11);
96 void implTestBasicVerticalLines(const BitmapDeviceSharedPtr
& rDevice
)
98 rDevice
->clear(Color(0));
100 const basegfx::B2IPoint
aPt1(1,1);
101 const basegfx::B2IPoint
aPt2(1,9);
102 const Color
aCol(0xFFFFFFFF);
103 rDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_PAINT
);
104 CPPUNIT_ASSERT_MESSAGE("first pixel set",
105 rDevice
->getPixel(aPt1
) == aCol
);
106 CPPUNIT_ASSERT_MESSAGE("last pixel set",
107 rDevice
->getPixel(aPt2
) == aCol
);
108 const basegfx::B2IPoint
aPt3(0,0);
109 CPPUNIT_ASSERT_MESSAGE("topmost pixel not set",
110 rDevice
->getPixel(aPt3
) != aCol
);
111 const basegfx::B2IPoint
aPt4(0,10);
112 CPPUNIT_ASSERT_MESSAGE("bottommost pixel not set",
113 rDevice
->getPixel(aPt4
) != aCol
);
115 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 9",
116 countPixel( rDevice
, aCol
) == 9);
119 // test pixel rounding (should always tend towards start point of
121 void implTestTieBreaking(const BitmapDeviceSharedPtr
& rDevice
)
123 rDevice
->clear(Color(0));
125 const basegfx::B2IPoint
aPt1(1,1);
126 const basegfx::B2IPoint
aPt2(3,2);
127 const Color
aCol(0xFFFFFFFF);
128 rDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_PAINT
);
129 CPPUNIT_ASSERT_MESSAGE("first pixel set",
130 rDevice
->getPixel(aPt1
) == aCol
);
131 CPPUNIT_ASSERT_MESSAGE("second pixel set",
132 rDevice
->getPixel(basegfx::B2IPoint(2,1)) == aCol
);
133 CPPUNIT_ASSERT_MESSAGE("last pixel set",
134 rDevice
->getPixel(aPt2
) == aCol
);
135 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
136 "reversed paint is not 3",
137 countPixel( rDevice
, aCol
) == 3);
139 rDevice
->drawLine( aPt2
, aPt1
, aCol
, DrawMode_PAINT
);
140 CPPUNIT_ASSERT_MESSAGE("alternate second pixel set",
141 rDevice
->getPixel(basegfx::B2IPoint(2,2)) == aCol
);
143 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel after "
144 "reversed paint is not 4",
145 countPixel( rDevice
, aCol
) == 4);
149 void setUp() SAL_OVERRIDE
151 const basegfx::B2ISize
aSize(11,11);
152 mpDevice1bpp
= createBitmapDevice( aSize
,
154 FORMAT_ONE_BIT_MSB_PAL
,
155 basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_PAL
, aSize
.getX()) );
156 mpDevice32bpp
= createBitmapDevice( aSize
,
158 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA
,
159 basebmp::getBitmapDeviceStrideForWidth(FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA
, aSize
.getX()) );
162 void testCornerCases()
164 const basegfx::B2ISize
aSize(1,1);
165 BitmapDeviceSharedPtr pDevice
= createBitmapDevice(
168 FORMAT_ONE_BIT_MSB_PAL
,
169 basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_PAL
, aSize
.getX()) );
171 const basegfx::B2IPoint
aPt1(0,0);
172 const basegfx::B2IPoint
aPt2(10,10);
173 CPPUNIT_ASSERT_MESSAGE("only pixel cleared",
174 pDevice
->getPixelData(aPt1
) == 0);
176 const Color
aCol(0xFFFFFFFF);
177 pDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_PAINT
);
178 CPPUNIT_ASSERT_MESSAGE("only pixel set",
179 pDevice
->getPixelData(aPt1
) == 1);
181 const basegfx::B2ISize
aSize2(1,0);
182 pDevice
= createBitmapDevice(
185 FORMAT_ONE_BIT_MSB_PAL
,
186 basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_PAL
, aSize
.getX()));
188 CPPUNIT_ASSERT_MESSAGE("only pixel cleared",
189 pDevice
->getPixelData(aPt1
) == 0);
191 pDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_PAINT
);
192 CPPUNIT_ASSERT_MESSAGE("only pixel still cleared",
193 pDevice
->getPixelData(aPt1
) == 0);
196 void testBasicDiagonalLines()
198 implTestBasicDiagonalLines( mpDevice1bpp
);
199 implTestBasicDiagonalLines( mpDevice32bpp
);
202 void testBasicHorizontalLines()
204 implTestBasicHorizontalLines( mpDevice1bpp
);
205 implTestBasicHorizontalLines( mpDevice32bpp
);
208 void testBasicVerticalLines()
210 implTestBasicVerticalLines( mpDevice1bpp
);
211 implTestBasicVerticalLines( mpDevice32bpp
);
214 // test pixel rounding (should always tend towards start point of
216 void testTieBreaking()
218 implTestTieBreaking( mpDevice1bpp
);
219 implTestTieBreaking( mpDevice32bpp
);
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.
226 CPPUNIT_TEST_SUITE(LineTest
);
227 CPPUNIT_TEST(testCornerCases
);
228 CPPUNIT_TEST(testBasicDiagonalLines
);
229 CPPUNIT_TEST(testBasicHorizontalLines
);
230 CPPUNIT_TEST(testBasicVerticalLines
);
231 CPPUNIT_TEST(testTieBreaking
);
232 CPPUNIT_TEST_SUITE_END();
235 CPPUNIT_TEST_SUITE_REGISTRATION(LineTest
);
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */