bump product version to 5.0.4.1
[LibreOffice.git] / basebmp / test / bmpmasktest.cxx
blob611e18955279d7710f73d0785af9e3b238df9c90
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 * 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/range/b2ibox.hxx>
26 #include <basegfx/point/b2ipoint.hxx>
27 #include <basegfx/polygon/b2dpolygon.hxx>
28 #include <basegfx/polygon/b2dpolygontools.hxx>
29 #include <basegfx/polygon/b2dpolypolygon.hxx>
30 #include <basegfx/polygon/b2dpolypolygontools.hxx>
32 #include <basebmp/color.hxx>
33 #include <basebmp/scanlineformats.hxx>
34 #include <basebmp/bitmapdevice.hxx>
35 #include "tools.hxx"
37 using namespace ::basebmp;
39 namespace
41 class BmpMaskTest : public CppUnit::TestFixture
43 private:
44 BitmapDeviceSharedPtr mpDevice1bpp;
45 BitmapDeviceSharedPtr mpMaskBmp1bpp;
46 BitmapDeviceSharedPtr mpBmp1bpp;
47 BitmapDeviceSharedPtr mpDevice32bpp;
48 BitmapDeviceSharedPtr mpBmp32bpp;
50 void implTestBmpBasics(const BitmapDeviceSharedPtr& rDevice,
51 const BitmapDeviceSharedPtr& rBmp)
53 rDevice->clear(Color(0));
54 const Color aCol(0xFFFFFFFF);
56 const basegfx::B2IBox aSourceRect(0,0,10,10);
57 const basegfx::B2IBox aDestAll(0,0,10,10);
59 rDevice->drawMaskedBitmap(
60 rBmp,
61 mpMaskBmp1bpp,
62 aSourceRect,
63 aDestAll,
64 DrawMode::DrawMode_PAINT );
65 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 30",
66 countPixel( rDevice, aCol ) == 30);
69 void implTestBmpScaledClip(const BitmapDeviceSharedPtr& rDevice,
70 const BitmapDeviceSharedPtr& rBmp)
72 rDevice->clear(Color(0));
73 const Color aCol(0xFFFFFFFF);
75 const basegfx::B2IBox aSourceRect(0,0,10,10);
76 const basegfx::B2IBox aDestLeftTop(0,0,6,6);
78 rDevice->drawMaskedBitmap(
79 rBmp,
80 mpMaskBmp1bpp,
81 aSourceRect,
82 aDestLeftTop,
83 DrawMode_PAINT );
84 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 12",
85 countPixel( rDevice, aCol ) == 12);
88 public:
89 void setUp() SAL_OVERRIDE
91 const basegfx::B2ISize aSize(10,10);
92 mpDevice1bpp = createBitmapDevice( aSize,
93 true,
94 FORMAT_ONE_BIT_MSB_PAL,
95 basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_PAL, aSize.getX()));
96 mpDevice32bpp = createBitmapDevice( aSize,
97 true,
98 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA,
99 basebmp::getBitmapDeviceStrideForWidth(FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA, aSize.getX()));
101 mpMaskBmp1bpp = createBitmapDevice( aSize,
102 true,
103 FORMAT_ONE_BIT_MSB_GREY,
104 basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_GREY, aSize.getX()));
106 mpBmp1bpp = createBitmapDevice( aSize,
107 true,
108 FORMAT_ONE_BIT_MSB_PAL,
109 basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_PAL, aSize.getX()));
110 mpBmp32bpp = createBitmapDevice( aSize,
111 true,
112 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA,
113 basebmp::getBitmapDeviceStrideForWidth(FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA, aSize.getX()));
115 OUString aSvg( "m 0 0h5v10h5v-5h-10z" );
117 basegfx::B2DPolyPolygon aPoly;
118 basegfx::tools::importFromSvgD( aPoly, aSvg, false, NULL );
119 const Color aColWhite(0xFFFFFFFF);
120 const Color aColBlack(0);
121 mpBmp1bpp->fillPolyPolygon(
122 aPoly,
123 aColWhite,
124 DrawMode_PAINT );
125 mpBmp32bpp->fillPolyPolygon(
126 aPoly,
127 aColWhite,
128 DrawMode_PAINT );
130 aSvg = "m 0 0 h6 v10 h-6z" ;
132 aPoly.clear();
133 basegfx::tools::importFromSvgD( aPoly, aSvg, false, NULL );
134 mpMaskBmp1bpp->clear(aColWhite);
135 mpMaskBmp1bpp->fillPolyPolygon(
136 aPoly,
137 aColBlack,
138 DrawMode_PAINT );
141 void testBmpBasics()
143 // mpDevice1bpp has a black rect. 0x0 -> 6x10
144 implTestBmpBasics( mpDevice1bpp, mpBmp1bpp );
145 implTestBmpBasics( mpDevice32bpp, mpBmp32bpp );
148 void testBmpClip()
150 implTestBmpScaledClip( mpDevice1bpp, mpBmp1bpp );
151 implTestBmpScaledClip( mpDevice32bpp, mpBmp32bpp );
154 void testMasking()
156 BitmapDeviceSharedPtr xOutput;
157 BitmapDeviceSharedPtr xBitmap;
158 BitmapDeviceSharedPtr xMask;
160 { // mpMask & mpBitmap
161 const basegfx::B2ISize aSize(5, 5);
162 std::vector< basebmp::Color > aDevPal;
163 aDevPal.push_back( basebmp::Color( 0, 0, 0 ) );
164 aDevPal.push_back( basebmp::Color( 0xff, 0xff, 0xff ) );
166 basebmp::Format nFormat;
168 nFormat = FORMAT_ONE_BIT_MSB_PAL;
169 // nFormat = Format::OneBitMsbGrey; // FIXME - un-comment me to crash hard.
170 xMask = createBitmapDevice( aSize, false /* bTopDown */,
171 nFormat,
172 basebmp::getBitmapDeviceStrideForWidth( nFormat, aSize.getX()),
173 PaletteMemorySharedVector(
174 new std::vector< basebmp::Color >(aDevPal) ) );
175 // wipe to copy everything.
176 xMask->clear( basebmp::Color( 0x00, 0x00, 0x00 ) );
178 // punch out another piece not to copy
179 basegfx::B2DPolyPolygon aPoly;
180 basegfx::tools::importFromSvgD( aPoly, "m 2 2 h4 v8 h-4z",
181 false, NULL );
182 xMask->fillPolyPolygon( aPoly, basebmp::Color( 0xff, 0xff, 0xff ),
183 DrawMode::DrawMode_PAINT );
185 xBitmap = createBitmapDevice( aSize, false,
186 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRX,
187 basebmp::getBitmapDeviceStrideForWidth(
188 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRX, aSize.getX() ) );
189 xBitmap->clear(Color(0x80808080));
191 { // mpOutput & mpBitmap
192 const basegfx::B2ISize aSize(9, 9);
193 xOutput = createBitmapDevice( aSize, false,
194 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRX,
195 basebmp::getBitmapDeviceStrideForWidth(
196 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRX, aSize.getX()) );
197 xOutput->clear(Color(0xffffffff));
200 const basegfx::B2IBox aSourceRect(0,0,4,4);
201 const basegfx::B2IBox aDestAll(2,2,7,7);
203 xOutput->drawMaskedBitmap(
204 xBitmap, xMask,
205 aSourceRect, aDestAll,
206 DrawMode::DrawMode_PAINT );
208 CPPUNIT_ASSERT_MESSAGE( "output not cleared to white",
209 xOutput->getPixel( basegfx::B2IPoint( 0, 0 ) ) == Color(0xffffff) );
210 CPPUNIT_ASSERT_MESSAGE( "bitmap not drawn",
211 xOutput->getPixel( basegfx::B2IPoint( 2, 2 ) ) == Color(0x808080) );
212 CPPUNIT_ASSERT_MESSAGE( "mask not applied",
213 xOutput->getPixel( basegfx::B2IPoint( 6, 6 ) ) == Color(0xffffff) );
216 // Change the following lines only, if you add, remove or rename
217 // member functions of the current class,
218 // because these macros are need by auto register mechanism.
220 CPPUNIT_TEST_SUITE(BmpMaskTest);
221 CPPUNIT_TEST(testMasking);
222 CPPUNIT_TEST(testBmpBasics);
223 CPPUNIT_TEST(testBmpClip);
224 CPPUNIT_TEST_SUITE_END();
227 CPPUNIT_TEST_SUITE_REGISTRATION(BmpMaskTest);
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */