LCOV - code coverage report
Current view: top level - basebmp/test - polytest.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 166 166 100.0 %
Date: 2015-06-13 12:38:46 Functions: 26 26 100.0 %
Legend: Lines: hit not hit

          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/range/b2ibox.hxx>
      26             : #include <basegfx/point/b2ipoint.hxx>
      27             : #include <basegfx/matrix/b2dhommatrix.hxx>
      28             : #include <basegfx/polygon/b2dpolygon.hxx>
      29             : #include <basegfx/polygon/b2dpolygontools.hxx>
      30             : #include <basegfx/polygon/b2dpolypolygon.hxx>
      31             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      32             : 
      33             : #include <basebmp/color.hxx>
      34             : #include <basebmp/scanlineformats.hxx>
      35             : #include <basebmp/bitmapdevice.hxx>
      36             : #include "tools.hxx"
      37             : 
      38             : using namespace ::basebmp;
      39             : 
      40             : namespace
      41             : {
      42          21 : class PolyTest : public CppUnit::TestFixture
      43             : {
      44             : private:
      45             :     BitmapDeviceSharedPtr mpDevice1bpp;
      46             :     BitmapDeviceSharedPtr mpDevice32bpp;
      47             : 
      48           2 :     void implTestEmpty(const BitmapDeviceSharedPtr& rDevice)
      49             :     {
      50           2 :         const Color aCol(0xFFFFFFFF);
      51           2 :         const Color aBgCol(0);
      52           2 :         rDevice->clear(aBgCol);
      53           2 :         basegfx::B2DPolyPolygon aPoly;
      54             : 
      55             :         basegfx::tools::importFromSvgD(
      56             :             aPoly,
      57           2 :             OUString( "M2 2 l7 7 z" ), false, NULL );
      58             :         rDevice->fillPolyPolygon(
      59             :             aPoly,
      60             :             aCol,
      61           2 :             DrawMode::Paint );
      62           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0",
      63           2 :                                countPixel( rDevice, aCol ) == 0);
      64             : 
      65             : 
      66             : 
      67           2 :         rDevice->clear(aBgCol);
      68           2 :         aPoly.clear();
      69             :         basegfx::tools::importFromSvgD(
      70             :             aPoly,
      71           2 :             OUString( "M7 2 l-6 6 z" ), false, NULL );
      72             :         rDevice->fillPolyPolygon(
      73             :             aPoly,
      74             :             aCol,
      75           2 :             DrawMode::Paint );
      76           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0(b)",
      77           4 :                                countPixel( rDevice, aCol ) == 0);
      78           2 :     }
      79             : 
      80           2 :     void implTestHairline(const BitmapDeviceSharedPtr& rDevice)
      81             :     {
      82           2 :         const Color aCol(0xFFFFFFFF);
      83           2 :         const Color aBgCol(0);
      84           2 :         rDevice->clear(aBgCol);
      85           2 :         basegfx::B2DPolyPolygon aPoly;
      86             : 
      87             :         basegfx::tools::importFromSvgD(
      88             :             aPoly,
      89           2 :             OUString( "M2 2 h1 l7 7 h-1 z" ), false, NULL );
      90             :         rDevice->fillPolyPolygon(
      91             :             aPoly,
      92             :             aCol,
      93           2 :             DrawMode::Paint );
      94           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
      95           2 :                                countPixel( rDevice, aCol ) == 7);
      96             : 
      97             : 
      98             : 
      99           2 :         rDevice->clear(aBgCol);
     100           2 :         aPoly.clear();
     101             :         basegfx::tools::importFromSvgD(
     102             :             aPoly,
     103           2 :             OUString( "M7 2 h-1 l-6 6 h1 z" ), false, NULL );
     104             :         rDevice->fillPolyPolygon(
     105             :             aPoly,
     106             :             aCol,
     107           2 :             DrawMode::Paint );
     108           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 6",
     109           2 :                                countPixel( rDevice, aCol ) == 6);
     110             : 
     111             : 
     112             : 
     113           2 :         rDevice->clear(aBgCol);
     114           2 :         aPoly.clear();
     115             :         basegfx::tools::importFromSvgD(
     116             :             aPoly,
     117           2 :             OUString( "M0 0 l7 7 h-1 l-5-7 z" ), false, NULL );
     118             :         rDevice->fillPolyPolygon(
     119             :             aPoly,
     120             :             aCol,
     121           2 :             DrawMode::Paint );
     122           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 3",
     123           4 :                                countPixel( rDevice, aCol ) == 3);
     124           2 :     }
     125             : 
     126           2 :     void implTestPolyPoly(const BitmapDeviceSharedPtr& rDevice)
     127             :     {
     128           2 :         const Color aCol(0xFFFFFFFF);
     129           2 :         const Color aBgCol(0);
     130           2 :         rDevice->clear(aBgCol);
     131           2 :         basegfx::B2DPolyPolygon aPoly;
     132             : 
     133             :         basegfx::tools::importFromSvgD( aPoly,
     134             :                                         OUString( "M0 0 h7 v7 h-7 z M2 2 v3 h3 v-3 z" ),
     135           2 :                                         false, NULL );
     136             : 
     137             :         rDevice->fillPolyPolygon(
     138             :             aPoly,
     139             :             aCol,
     140           2 :             DrawMode::Paint );
     141           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 40",
     142           4 :                                countPixel( rDevice, aCol ) == 40);
     143           2 :     }
     144             : 
     145           2 :     void implTestPolyPolyClip(const BitmapDeviceSharedPtr& rDevice)
     146             :     {
     147           2 :         const Color aCol(0xFFFFFFFF);
     148           2 :         const Color aBgCol(0);
     149           2 :         rDevice->clear(aBgCol);
     150           2 :         basegfx::B2DPolyPolygon aPoly;
     151             : 
     152             :         basegfx::tools::importFromSvgD( aPoly,
     153             :                                         OUString( "M0 0 h7 v7 h-7 z M2 2 v3 h3 v-3 z" ),
     154           2 :                                         false, NULL );
     155           4 :         basegfx::B2DHomMatrix aMat;
     156           2 :         aMat.translate(-3,-3);
     157           2 :         aMat.rotate( 1.7 );
     158           2 :         aMat.translate(6,5);
     159           2 :         aPoly.transform(aMat);
     160             : 
     161             :         rDevice->fillPolyPolygon(
     162             :             aPoly,
     163             :             aCol,
     164           2 :             DrawMode::Paint );
     165             : 
     166           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 39",
     167           2 :                                countPixel( rDevice, aCol ) == 39);
     168             : 
     169             :         BitmapDeviceSharedPtr pClippedDevice(
     170             :             subsetBitmapDevice( rDevice,
     171           4 :                                 basegfx::B2IBox(3,3,5,8) ));
     172           4 :         CPPUNIT_ASSERT_MESSAGE("size of subsetted device is not (2,5)",
     173           2 :                                pClippedDevice->getSize() == basegfx::B2IVector(2,5));
     174             : 
     175           2 :         rDevice->clear(aBgCol);
     176             :         pClippedDevice->fillPolyPolygon(
     177             :             aPoly,
     178             :             aCol,
     179           2 :             DrawMode::Paint );
     180           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
     181           4 :                                countPixel( rDevice, aCol ) == 7);
     182           2 :     }
     183             : 
     184           2 :     void implTestLineDrawClip(const BitmapDeviceSharedPtr& rDevice)
     185             :     {
     186           2 :         const Color aCol(0xFFFFFFFF);
     187           2 :         const Color aBgCol(0);
     188           2 :         rDevice->clear(aBgCol);
     189             : 
     190             :         // create rectangular subset, such that we can 'see' extra
     191             :         // pixel outside
     192             :         BitmapDeviceSharedPtr pClippedDevice=(
     193             :             subsetBitmapDevice( rDevice,
     194           2 :                                 basegfx::B2IBox(3,3,5,9) ));
     195             : 
     196             :         // trigger "alternate bresenham" case in
     197             :         // clippedlinerenderer.hxx, first point not clipped
     198           4 :         const basegfx::B2IPoint aPt1(3,3);
     199           4 :         const basegfx::B2IPoint aPt2(4,2);
     200           2 :         pClippedDevice->drawLine( aPt1, aPt2, aCol, DrawMode::Paint );
     201             : 
     202           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 1",
     203           2 :                                countPixel( rDevice, aCol ) == 1);
     204             : 
     205             :         // trigger "alternate bresenham" case in
     206             :         // clippedlinerenderer.hxx, both start and endpoint clipped
     207           4 :         const basegfx::B2IPoint aPt3(0,4);
     208           2 :         pClippedDevice->drawLine( aPt3, aPt2, aCol, DrawMode::XOR );
     209             : 
     210           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0",
     211           2 :                                countPixel( rDevice, aCol ) == 0);
     212             : 
     213             :         // trigger "standard bresenham" case in
     214             :         // clippedlinerenderer.hxx, first point not clipped
     215           4 :         const basegfx::B2IPoint aPt4(6,2);
     216           2 :         pClippedDevice->drawLine( aPt1, aPt4, aCol, DrawMode::Paint );
     217             : 
     218           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 2",
     219           2 :                                countPixel( rDevice, aCol ) == 2);
     220             : 
     221             :         // trigger "clipCode1 & aMinFlag/bMinFlag" cases in
     222             :         // clippedlinerenderer.hxx (note1: needs forcing end point to
     223             :         // be clipped as well, otherwise optimization kicks in. note2:
     224             :         // needs forcing end point to clip on two edges, not only on
     225             :         // one, otherwise swap kicks in)
     226           4 :         const basegfx::B2IPoint aPt5(1,1);
     227           4 :         const basegfx::B2IPoint aPt6(6,10);
     228           2 :         pClippedDevice->drawLine( aPt5, aPt6, aCol, DrawMode::XOR );
     229             : 
     230           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 6",
     231           2 :                                countPixel( rDevice, aCol ) == 6);
     232             : 
     233             :         // trigger "clipCode1 & (aMinFlag|aMaxFlag)" case in
     234             :         // clippedlinerenderer.hxx that was not taken for the test
     235             :         // above
     236           2 :         pClippedDevice->drawLine( aPt3, aPt6, aCol, DrawMode::XOR );
     237             : 
     238           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 8",
     239           4 :                                countPixel( rDevice, aCol ) == 8);
     240             : 
     241           2 :     }
     242             : 
     243           2 :     void implTestPolyDrawClip(const BitmapDeviceSharedPtr& rDevice)
     244             :     {
     245           2 :         const Color aCol(0xFFFFFFFF);
     246           2 :         const Color aBgCol(0);
     247           2 :         rDevice->clear(aBgCol);
     248           2 :         basegfx::B2DPolyPolygon aPoly;
     249             : 
     250             :         // test all corner-touching lines of our clip rect. note that
     251             :         // *all* of the four two-pixel lines in that polygon do *not*
     252             :         // generate a single pixel, due to the rasterization effect.
     253             :         basegfx::tools::importFromSvgD( aPoly,
     254             :                                         OUString( "M2 3 l1 -1 M4 2 l1 1 M2 8 l1 1 M5 8 l-1 1 M2 5 h4 M3 0 v10" ),
     255           2 :                                         false, NULL );
     256             :         BitmapDeviceSharedPtr pClippedDevice=(
     257             :             subsetBitmapDevice( rDevice,
     258           4 :                                 basegfx::B2IBox(3,3,5,9) ));
     259             : 
     260          14 :         for( unsigned int i=0; i<aPoly.count(); ++i )
     261             :             pClippedDevice->drawPolygon(
     262             :                 aPoly.getB2DPolygon(i),
     263             :                 aCol,
     264          12 :                 DrawMode::Paint );
     265             : 
     266           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
     267           4 :                                countPixel( rDevice, aCol ) == 7);
     268           2 :     }
     269             : 
     270           2 :     void implTestPolyPolyCrissCross(const BitmapDeviceSharedPtr& rDevice)
     271             :     {
     272           2 :         const Color aCol(0xFFFFFFFF);
     273           2 :         const Color aBgCol(0);
     274           2 :         rDevice->clear(aBgCol);
     275           2 :         basegfx::B2DPolyPolygon aPoly;
     276             : 
     277             :         basegfx::tools::importFromSvgD( aPoly,
     278             :                                         OUString( "M0 0 v2 l10 2 v-2 z"
     279             :                                             "M10 6 v-2 l-10 2 v2 z"
     280             :                                             "M1 0 h1 v10 h-1 z"
     281             :                                             "M4 0 h1 v10 h-1 z"
     282             :                                             "M8 0 h1 v10 h-1 z" ),
     283           2 :                                         false, NULL );
     284             :         rDevice->fillPolyPolygon(
     285             :             aPoly,
     286             :             aCol,
     287           2 :             DrawMode::Paint );
     288           4 :         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 46",
     289           4 :                                countPixel( rDevice, aCol ) == 46);
     290           2 :     }
     291             : 
     292             : 
     293             : public:
     294           7 :     void setUp() SAL_OVERRIDE
     295             :     {
     296           7 :         const basegfx::B2ISize aSize(10,10);
     297          14 :         mpDevice1bpp = createBitmapDevice( aSize,
     298             :                                            true,
     299             :                                            Format::OneBitMsbPal,
     300           7 :                                            basebmp::getBitmapDeviceStrideForWidth(Format::OneBitMsbPal, aSize.getX()));
     301          14 :         mpDevice32bpp = createBitmapDevice( aSize,
     302             :                                             true,
     303             :                                             Format::ThirtyTwoBitTcMaskBGRA,
     304          14 :                                             basebmp::getBitmapDeviceStrideForWidth(Format::ThirtyTwoBitTcMaskBGRA, aSize.getX()));
     305           7 :     }
     306             : 
     307           1 :     void testEmpty()
     308             :     {
     309           1 :         implTestEmpty( mpDevice1bpp );
     310           1 :         implTestEmpty( mpDevice32bpp );
     311           1 :     }
     312             : 
     313           1 :     void testHairline()
     314             :     {
     315           1 :         implTestHairline( mpDevice1bpp );
     316           1 :         implTestHairline( mpDevice32bpp );
     317           1 :     }
     318             : 
     319           1 :     void testPolyPoly()
     320             :     {
     321           1 :         implTestPolyPoly( mpDevice1bpp );
     322           1 :         implTestPolyPoly( mpDevice32bpp );
     323           1 :     }
     324             : 
     325           1 :     void testPolyPolyClip()
     326             :     {
     327           1 :         implTestPolyPolyClip(mpDevice1bpp);
     328           1 :         implTestPolyPolyClip(mpDevice32bpp);
     329           1 :     }
     330             : 
     331           1 :     void testLineDrawClip()
     332             :     {
     333           1 :         implTestLineDrawClip(mpDevice1bpp);
     334           1 :         implTestLineDrawClip(mpDevice32bpp);
     335           1 :     }
     336             : 
     337           1 :     void testPolyDrawClip()
     338             :     {
     339           1 :         implTestPolyDrawClip(mpDevice1bpp);
     340           1 :         implTestPolyDrawClip(mpDevice32bpp);
     341           1 :     }
     342             : 
     343           1 :     void testPolyPolyCrissCross()
     344             :     {
     345           1 :         implTestPolyPolyCrissCross(mpDevice1bpp);
     346           1 :         implTestPolyPolyCrissCross(mpDevice32bpp);
     347           1 :     }
     348             : 
     349             :     // Change the following lines only, if you add, remove or rename
     350             :     // member functions of the current class,
     351             :     // because these macros are need by auto register mechanism.
     352             : 
     353           2 :     CPPUNIT_TEST_SUITE(PolyTest);
     354           1 :     CPPUNIT_TEST(testEmpty);
     355           1 :     CPPUNIT_TEST(testHairline);
     356           1 :     CPPUNIT_TEST(testPolyPoly);
     357           1 :     CPPUNIT_TEST(testPolyPolyClip);
     358           1 :     CPPUNIT_TEST(testLineDrawClip);
     359           1 :     CPPUNIT_TEST(testPolyDrawClip);
     360           1 :     CPPUNIT_TEST(testPolyPolyCrissCross);
     361           5 :     CPPUNIT_TEST_SUITE_END();
     362             : };
     363             : 
     364           1 : CPPUNIT_TEST_SUITE_REGISTRATION(PolyTest);
     365           3 : }
     366             : 
     367             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11