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/.
11 #include <test/outputdevice.hxx>
12 #include <bitmapwriteaccess.hxx>
20 int deltaColor(BitmapColor aColor1
, BitmapColor aColor2
)
22 int deltaR
= std::abs(aColor1
.GetRed() - aColor2
.GetRed());
23 int deltaG
= std::abs(aColor1
.GetGreen() - aColor2
.GetGreen());
24 int deltaB
= std::abs(aColor1
.GetBlue() - aColor2
.GetBlue());
26 return std::max(std::max(deltaR
, deltaG
), deltaB
);
29 void checkValue(BitmapScopedWriteAccess
& pAccess
, int x
, int y
, Color aExpected
,
30 int& nNumberOfQuirks
, int& nNumberOfErrors
, bool bQuirkMode
, int nColorDeltaThresh
= 0)
32 const bool bColorize
= false;
33 Color aColor
= pAccess
->GetPixel(y
, x
);
34 int nColorDelta
= deltaColor(aColor
, aExpected
);
36 if (nColorDelta
<= nColorDeltaThresh
)
39 pAccess
->SetPixel(y
, x
, COL_LIGHTGREEN
);
45 pAccess
->SetPixel(y
, x
, COL_YELLOW
);
51 pAccess
->SetPixel(y
, x
, COL_LIGHTRED
);
55 TestResult
checkRect(Bitmap
& rBitmap
, int aLayerNumber
, Color aExpectedColor
)
57 BitmapScopedWriteAccess
pAccess(rBitmap
);
58 long nHeight
= pAccess
->Height();
59 long nWidth
= pAccess
->Width();
61 long firstX
= 0 + aLayerNumber
;
62 long firstY
= 0 + aLayerNumber
;
64 long lastX
= nWidth
- aLayerNumber
- 1;
65 long lastY
= nHeight
- aLayerNumber
- 1;
67 TestResult aResult
= TestResult::Passed
;
68 int nNumberOfQuirks
= 0;
69 int nNumberOfErrors
= 0;
71 // check corner quirks
72 checkValue(pAccess
, firstX
, firstY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true);
73 checkValue(pAccess
, lastX
, firstY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true);
74 checkValue(pAccess
, firstX
, lastY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true);
75 checkValue(pAccess
, lastX
, lastY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true);
77 for (long y
= firstY
+ 1; y
<= lastY
- 1; y
++)
79 checkValue(pAccess
, firstX
, y
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false);
80 checkValue(pAccess
, lastX
, y
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false);
82 for (long x
= firstX
+ 1; x
<= lastX
- 1; x
++)
84 checkValue(pAccess
, x
, firstY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false);
85 checkValue(pAccess
, x
, lastY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false);
87 if (nNumberOfQuirks
> 0)
88 aResult
= TestResult::PassedWithQuirks
;
89 if (nNumberOfErrors
> 0)
90 aResult
= TestResult::Failed
;
94 TestResult
checkHorizontalVerticalDiagonalLines(Bitmap
& rBitmap
, Color aExpectedColor
, int nColorThresh
)
96 BitmapScopedWriteAccess
pAccess(rBitmap
);
97 long nWidth
= pAccess
->Width();
98 long nHeight
= pAccess
->Height();
100 TestResult aResult
= TestResult::Passed
;
101 int nNumberOfQuirks
= 0;
102 int nNumberOfErrors
= 0;
104 // check horizontal line
107 long endX
= nWidth
- 2;
111 checkValue(pAccess
, startX
, y
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true, nColorThresh
);
112 checkValue(pAccess
, endX
, y
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true, nColorThresh
);
114 for (long x
= startX
+ 1; x
<= endX
- 1; x
++)
116 checkValue(pAccess
, x
, y
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false, nColorThresh
);
120 // check vertical line
123 long endY
= nHeight
- 2;
127 checkValue(pAccess
, x
, startY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true, nColorThresh
);
128 checkValue(pAccess
, x
, endY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true, nColorThresh
);
130 for (long y
= startY
+ 1; y
<= endY
- 1; y
++)
132 checkValue(pAccess
, x
, y
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false, nColorThresh
);
136 // check diagonal line
139 long endX
= nWidth
- 2;
142 long endY
= nHeight
- 2;
147 checkValue(pAccess
, startX
, startY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true, nColorThresh
);
148 checkValue(pAccess
, endX
, endY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true, nColorThresh
);
152 while(y
<= endY
- 1 && x
<= endX
- 1)
154 checkValue(pAccess
, x
, y
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false, nColorThresh
);
159 if (nNumberOfQuirks
> 0)
160 aResult
= TestResult::PassedWithQuirks
;
161 if (nNumberOfErrors
> 0)
162 aResult
= TestResult::Failed
;
166 TestResult
checkDiamondLine(Bitmap
& rBitmap
, int aLayerNumber
, Color aExpectedColor
)
168 BitmapScopedWriteAccess
pAccess(rBitmap
);
169 long nHeight
= pAccess
->Height();
170 long nWidth
= pAccess
->Width();
172 long midX
= nWidth
/ 2;
173 long midY
= nHeight
/ 2;
175 long firstX
= aLayerNumber
;
176 long lastX
= nWidth
- aLayerNumber
- 1;
178 long firstY
= aLayerNumber
;
179 long lastY
= nHeight
- aLayerNumber
- 1;
181 long offsetFromMid
= 0;
183 TestResult aResult
= TestResult::Passed
;
184 int nNumberOfQuirks
= 0;
185 int nNumberOfErrors
= 0;
187 checkValue(pAccess
, firstX
, midY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true);
188 checkValue(pAccess
, lastX
, midY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true);
189 checkValue(pAccess
, midX
, firstY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true);
190 checkValue(pAccess
, midX
, lastY
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, true);
193 for (long x
= firstX
+ 1; x
<= midX
- 1; x
++)
195 checkValue(pAccess
, x
, midY
- offsetFromMid
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false);
196 checkValue(pAccess
, x
, midY
+ offsetFromMid
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false);
201 offsetFromMid
= midY
- aLayerNumber
- 1;
203 for (long x
= midX
+ 1; x
<= lastX
- 1; x
++)
205 checkValue(pAccess
, x
, midY
- offsetFromMid
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false);
206 checkValue(pAccess
, x
, midY
+ offsetFromMid
, aExpectedColor
, nNumberOfQuirks
, nNumberOfErrors
, false);
211 if (nNumberOfQuirks
> 0)
212 aResult
= TestResult::PassedWithQuirks
;
213 if (nNumberOfErrors
> 0)
214 aResult
= TestResult::Failed
;
218 } // end anonymous namespace
220 const Color
OutputDeviceTestCommon::constBackgroundColor(COL_LIGHTGRAY
);
221 const Color
OutputDeviceTestCommon::constLineColor(COL_LIGHTBLUE
);
222 const Color
OutputDeviceTestCommon::constFillColor(COL_LIGHTBLUE
);
224 OutputDeviceTestCommon::OutputDeviceTestCommon()
225 : mpVirtualDevice(VclPtr
<VirtualDevice
>::Create())
228 void OutputDeviceTestCommon::initialSetup(long nWidth
, long nHeight
, Color aColor
, bool bEnableAA
)
230 maVDRectangle
= tools::Rectangle(Point(), Size (nWidth
, nHeight
));
231 mpVirtualDevice
->SetOutputSizePixel(maVDRectangle
.GetSize());
233 mpVirtualDevice
->SetAntialiasing(AntialiasingFlags::EnableB2dDraw
| AntialiasingFlags::PixelSnapHairline
);
235 mpVirtualDevice
->SetAntialiasing(AntialiasingFlags::NONE
);
236 mpVirtualDevice
->SetBackground(Wallpaper(aColor
));
237 mpVirtualDevice
->Erase();
240 TestResult
OutputDeviceTestCommon::checkLines(Bitmap
& rBitmap
)
242 return checkHorizontalVerticalDiagonalLines(rBitmap
, constLineColor
, 0);
245 TestResult
OutputDeviceTestCommon::checkAALines(Bitmap
& rBitmap
)
247 return checkHorizontalVerticalDiagonalLines(rBitmap
, constLineColor
, 30); // 30 color values threshold delta
250 TestResult
OutputDeviceTestCommon::checkRectangle(Bitmap
& aBitmap
)
252 std::vector
<Color
> aExpected
254 constBackgroundColor
, constBackgroundColor
, constLineColor
,
255 constBackgroundColor
, constBackgroundColor
, constLineColor
, constBackgroundColor
257 return checkRectangles(aBitmap
, aExpected
);
260 TestResult
OutputDeviceTestCommon::checkRectangleAA(Bitmap
& aBitmap
)
262 std::vector
<Color
> aExpected
264 constBackgroundColor
, constBackgroundColor
, constLineColor
,
265 constBackgroundColor
, constBackgroundColor
, constLineColor
, constBackgroundColor
267 return checkRectangles(aBitmap
, aExpected
);
270 TestResult
OutputDeviceTestCommon::checkFilledRectangle(Bitmap
& aBitmap
)
272 std::vector
<Color
> aExpected
274 constBackgroundColor
, constBackgroundColor
,
275 constFillColor
, constFillColor
, constFillColor
, constFillColor
, constFillColor
277 return checkRectangles(aBitmap
, aExpected
);
280 TestResult
OutputDeviceTestCommon::checkRectangles(Bitmap
& aBitmap
, std::vector
<Color
>& aExpectedColors
)
282 TestResult aReturnValue
= TestResult::Passed
;
283 for (size_t i
= 0; i
< aExpectedColors
.size(); i
++)
285 switch(checkRect(aBitmap
, i
, aExpectedColors
[i
]))
287 case TestResult::Failed
:
288 return TestResult::Failed
;
289 case TestResult::PassedWithQuirks
:
290 aReturnValue
= TestResult::PassedWithQuirks
;
300 tools::Rectangle
OutputDeviceTestCommon::alignToCenter(tools::Rectangle aRect1
, tools::Rectangle aRect2
)
302 Point
aPoint((aRect1
.GetWidth() / 2.0) - (aRect2
.GetWidth() / 2.0),
303 (aRect1
.GetHeight() / 2.0) - (aRect2
.GetHeight() / 2.0));
305 return tools::Rectangle(aPoint
, aRect2
.GetSize());
308 TestResult
OutputDeviceTestCommon::checkDiamond(Bitmap
& rBitmap
)
310 return checkDiamondLine(rBitmap
, 1, constLineColor
);
313 void OutputDeviceTestCommon::createDiamondPoints(tools::Rectangle rRect
, int nOffset
,
314 Point
& rPoint1
, Point
& rPoint2
,
315 Point
& rPoint3
, Point
& rPoint4
)
317 long midPointX
= rRect
.Left() + (rRect
.Right() - rRect
.Left()) / 2.0;
318 long midPointY
= rRect
.Top() + (rRect
.Bottom() - rRect
.Top()) / 2.0;
320 rPoint1
= Point(midPointX
, midPointY
- nOffset
);
321 rPoint2
= Point(midPointX
+ nOffset
, midPointY
);
322 rPoint3
= Point(midPointX
, midPointY
+ nOffset
);
323 rPoint4
= Point(midPointX
- nOffset
, midPointY
);
326 void OutputDeviceTestCommon::createHorizontalVerticalDiagonalLinePoints(tools::Rectangle rRect
,
327 Point
& rHorizontalLinePoint1
, Point
& rHorizontalLinePoint2
,
328 Point
& rVerticalLinePoint1
, Point
& rVerticalLinePoint2
,
329 Point
& rDiagonalLinePoint1
, Point
& rDiagonalLinePoint2
)
331 rHorizontalLinePoint1
= Point(4, 1);
332 rHorizontalLinePoint2
= Point(rRect
.Right() - 1, 1);
334 rVerticalLinePoint1
= Point(1, 4);
335 rVerticalLinePoint2
= Point(1,rRect
.Bottom() - 1);
337 rDiagonalLinePoint1
= Point(1, 1);
338 rDiagonalLinePoint2
= Point(rRect
.Right() - 1, rRect
.Bottom() - 1);
341 }} // end namespace vcl::test
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */