Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / vcl / backendtest / outputdevice / common.cxx
blob9f69a28fabd3dc475ffa7a017473cbcf0e84d302
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 */
11 #include <test/outputdevice.hxx>
12 #include <bitmapwriteaccess.hxx>
14 namespace vcl {
15 namespace test {
17 namespace
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).GetColor();
34 int nColorDelta = deltaColor(aColor, aExpected);
36 if (nColorDelta <= nColorDeltaThresh)
38 if (bColorize)
39 pAccess->SetPixel(y, x, COL_LIGHTGREEN);
41 else if (bQuirkMode)
43 nNumberOfQuirks++;
44 if (bColorize)
45 pAccess->SetPixel(y, x, COL_YELLOW);
47 else
49 nNumberOfErrors++;
50 if (bColorize)
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;
91 return aResult;
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
106 long startX = 4;
107 long endX = nWidth - 2;
109 long y = 1;
111 checkValue(pAccess, startX, y, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, true, nColorThresh);
112 checkValue(pAccess, endX, y, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, true, nColorThresh);
114 for (int x = startX + 1; x <= endX - 1; x++)
116 checkValue(pAccess, x, y, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, false, nColorThresh);
120 // check vertical line
122 long startY = 4;
123 long endY = nHeight - 2;
125 long x = 1;
127 checkValue(pAccess, x, startY, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, true, nColorThresh);
128 checkValue(pAccess, x, endY, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, true, nColorThresh);
130 for (int y = startY + 1; y <= endY - 1; y++)
132 checkValue(pAccess, x, y, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, false, nColorThresh);
136 // check diagonal line
138 long startX = 1;
139 long endX = nWidth - 2;
141 long startY = 1;
142 long endY = nHeight - 2;
144 long x = startX;
145 long y = startY;
147 checkValue(pAccess, startX, startY, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, true, nColorThresh);
148 checkValue(pAccess, endX, endY, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, true, nColorThresh);
150 x++; y++;
152 while(y <= endY - 1 && x <= endX - 1)
154 checkValue(pAccess, x, y, aExpectedColor, nNumberOfQuirks, nNumberOfErrors, false, nColorThresh);
155 x++; y++;
159 if (nNumberOfQuirks > 0)
160 aResult = TestResult::PassedWithQuirks;
161 if (nNumberOfErrors > 0)
162 aResult = TestResult::Failed;
163 return aResult;
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);
192 offsetFromMid = 1;
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);
198 offsetFromMid++;
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);
208 offsetFromMid--;
211 if (nNumberOfQuirks > 0)
212 aResult = TestResult::PassedWithQuirks;
213 if (nNumberOfErrors > 0)
214 aResult = TestResult::Failed;
215 return aResult;
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)
230 maVDRectangle = tools::Rectangle(Point(), Size (nWidth, nHeight));
231 mpVirtualDevice->SetOutputSizePixel(maVDRectangle.GetSize());
232 mpVirtualDevice->SetBackground(Wallpaper(aColor));
233 mpVirtualDevice->Erase();
236 TestResult OutputDeviceTestCommon::checkLines(Bitmap& rBitmap)
238 return checkHorizontalVerticalDiagonalLines(rBitmap, constLineColor, 0);
241 TestResult OutputDeviceTestCommon::checkAALines(Bitmap& rBitmap)
243 return checkHorizontalVerticalDiagonalLines(rBitmap, constLineColor, 30); // 30 color values threshold delta
246 TestResult OutputDeviceTestCommon::checkRectangle(Bitmap& aBitmap)
248 std::vector<Color> aExpected
250 constBackgroundColor, constBackgroundColor, constLineColor,
251 constBackgroundColor, constBackgroundColor, constLineColor, constBackgroundColor
253 return checkRectangles(aBitmap, aExpected);
256 TestResult OutputDeviceTestCommon::checkFilledRectangle(Bitmap& aBitmap)
258 std::vector<Color> aExpected
260 constBackgroundColor, constBackgroundColor,
261 constFillColor, constFillColor, constFillColor, constFillColor, constFillColor
263 return checkRectangles(aBitmap, aExpected);
266 TestResult OutputDeviceTestCommon::checkRectangles(Bitmap& aBitmap, std::vector<Color>& aExpectedColors)
268 TestResult aReturnValue = TestResult::Passed;
269 for (size_t i = 0; i < aExpectedColors.size(); i++)
271 switch(checkRect(aBitmap, i, aExpectedColors[i]))
273 case TestResult::Failed:
274 return TestResult::Failed;
275 case TestResult::PassedWithQuirks:
276 aReturnValue = TestResult::PassedWithQuirks;
277 break;
278 default:
279 break;
283 return aReturnValue;
286 tools::Rectangle OutputDeviceTestCommon::alignToCenter(tools::Rectangle aRect1, tools::Rectangle aRect2)
288 Point aPoint((aRect1.GetWidth() / 2.0) - (aRect2.GetWidth() / 2.0),
289 (aRect1.GetHeight() / 2.0) - (aRect2.GetHeight() / 2.0));
291 return tools::Rectangle(aPoint, aRect2.GetSize());
294 TestResult OutputDeviceTestCommon::checkDiamond(Bitmap& rBitmap)
296 return checkDiamondLine(rBitmap, 1, constLineColor);
299 void OutputDeviceTestCommon::createDiamondPoints(tools::Rectangle rRect, int nOffset,
300 Point& rPoint1, Point& rPoint2,
301 Point& rPoint3, Point& rPoint4)
303 long midPointX = rRect.Left() + (rRect.Right() - rRect.Left()) / 2.0;
304 long midPointY = rRect.Top() + (rRect.Bottom() - rRect.Top()) / 2.0;
306 rPoint1 = Point(midPointX , midPointY - nOffset);
307 rPoint2 = Point(midPointX + nOffset, midPointY );
308 rPoint3 = Point(midPointX , midPointY + nOffset);
309 rPoint4 = Point(midPointX - nOffset, midPointY );
312 void OutputDeviceTestCommon::createHorizontalVerticalDiagonalLinePoints(tools::Rectangle rRect,
313 Point& rHorizontalLinePoint1, Point& rHorizontalLinePoint2,
314 Point& rVerticalLinePoint1, Point& rVerticalLinePoint2,
315 Point& rDiagonalLinePoint1, Point& rDiagonalLinePoint2)
317 rHorizontalLinePoint1 = Point(4, 1);
318 rHorizontalLinePoint2 = Point(rRect.Right() - 1, 1);
320 rVerticalLinePoint1 = Point(1, 4);
321 rVerticalLinePoint2 = Point(1,rRect.Bottom() - 1);
323 rDiagonalLinePoint1 = Point(1, 1);
324 rDiagonalLinePoint2 = Point(rRect.Right() - 1, rRect.Bottom() - 1);
327 }} // end namespace vcl::test
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */