Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / qa / cppunit / canvasbitmaptest.cxx
blob07f2a51c85ef590a3fa23a3a2f7a2a1f4556cdc9
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 // bootstrap stuff
21 #include <test/bootstrapfixture.hxx>
23 #include <com/sun/star/util/Endianness.hpp>
24 #include <com/sun/star/rendering/ColorComponentTag.hpp>
25 #include <com/sun/star/rendering/ColorSpaceType.hpp>
26 #include <com/sun/star/rendering/RenderingIntent.hpp>
27 #include <com/sun/star/rendering/XIntegerReadOnlyBitmap.hpp>
28 #include <com/sun/star/rendering/XIntegerBitmapColorSpace.hpp>
29 #include <com/sun/star/rendering/XBitmapPalette.hpp>
31 #include <cppuhelper/compbase3.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <rtl/ref.hxx>
35 #include "vcl/svapp.hxx"
36 #include "vcl/canvastools.hxx"
37 #include "vcl/dialog.hxx"
38 #include "vcl/outdev.hxx"
39 #include "vcl/bmpacc.hxx"
40 #include "vcl/virdev.hxx"
41 #include "vcl/bitmapex.hxx"
43 #include "canvasbitmap.hxx"
44 #include <algorithm>
46 using namespace ::com::sun::star;
47 using namespace vcl::unotools;
49 namespace com { namespace sun { namespace star { namespace rendering
52 bool operator==( const RGBColor& rLHS, const ARGBColor& rRHS )
54 return rLHS.Red == rRHS.Red && rLHS.Green == rRHS.Green && rLHS.Blue == rRHS.Blue;
57 } } } }
59 namespace
62 class CanvasBitmapTest : public test::BootstrapFixture
64 public:
65 CanvasBitmapTest() : BootstrapFixture(true, false) {}
67 void runTest();
69 CPPUNIT_TEST_SUITE(CanvasBitmapTest);
70 CPPUNIT_TEST(runTest);
71 CPPUNIT_TEST_SUITE_END();
74 bool rangeCheck( const rendering::RGBColor& rColor )
76 return rColor.Red < 0.0 || rColor.Red > 1.0 ||
77 rColor.Green < 0.0 || rColor.Green > 1.0 ||
78 rColor.Blue < 0.0 || rColor.Blue > 1.0;
81 void checkCanvasBitmap( const rtl::Reference<VclCanvasBitmap>& xBmp,
82 const char* msg,
83 int nOriginalDepth )
85 OSL_TRACE("-------------------------");
86 OSL_TRACE("Testing %s, with depth %d", msg, nOriginalDepth);
88 BitmapEx aContainedBmpEx( xBmp->getBitmapEx() );
89 Bitmap aContainedBmp( aContainedBmpEx.GetBitmap() );
90 int nDepth = nOriginalDepth;
93 Bitmap::ScopedReadAccess pAcc( aContainedBmp );
94 nDepth = pAcc->GetBitCount();
97 CPPUNIT_ASSERT_MESSAGE( "Original bitmap size not (200,200)",
98 aContainedBmp.GetSizePixel() == Size(200,200));
100 CPPUNIT_ASSERT_MESSAGE( "Original bitmap size via API not (200,200)",
101 xBmp->getSize().Width == 200 && xBmp->getSize().Height == 200);
103 CPPUNIT_ASSERT_MESSAGE( "alpha state mismatch",
104 bool(xBmp->hasAlpha()) == aContainedBmpEx.IsTransparent());
106 CPPUNIT_ASSERT_MESSAGE( "getScaledBitmap() failed",
107 xBmp->getScaledBitmap( geometry::RealSize2D(500,500), false ).is());
109 rendering::IntegerBitmapLayout aLayout;
110 uno::Sequence<sal_Int8> aPixelData = xBmp->getData(aLayout, geometry::IntegerRectangle2D(0,0,1,1));
112 const sal_Int32 nExpectedBitsPerPixel(
113 aContainedBmpEx.IsTransparent() ? std::max(8,nDepth)+8 : nDepth);
114 CPPUNIT_ASSERT_MESSAGE( "# scanlines not 1",
115 aLayout.ScanLines == 1);
116 CPPUNIT_ASSERT_MESSAGE( "# scanline bytes mismatch",
117 aLayout.ScanLineBytes == (nExpectedBitsPerPixel+7)/8);
118 CPPUNIT_ASSERT_MESSAGE( "# scanline stride mismatch",
119 aLayout.ScanLineStride == (nExpectedBitsPerPixel+7)/8 ||
120 aLayout.ScanLineStride == -(nExpectedBitsPerPixel+7)/8);
121 CPPUNIT_ASSERT_MESSAGE( "# plane stride not 0",
122 aLayout.PlaneStride == 0);
124 CPPUNIT_ASSERT_MESSAGE( "Color space not there",
125 aLayout.ColorSpace.is());
127 CPPUNIT_ASSERT_MESSAGE( "Palette existence does not conform to bitmap",
128 aLayout.Palette.is() == (nDepth <= 8));
130 uno::Sequence<sal_Int8> aPixelData2 = xBmp->getPixel( aLayout, geometry::IntegerPoint2D(0,0) );
132 CPPUNIT_ASSERT_MESSAGE( "getData and getPixel did not return same amount of data",
133 aPixelData2.getLength() == aPixelData.getLength());
135 aPixelData = xBmp->getData(aLayout, geometry::IntegerRectangle2D(0,0,200,1));
136 CPPUNIT_ASSERT_MESSAGE( "# scanlines not 1 for getPixel",
137 aLayout.ScanLines == 1);
138 CPPUNIT_ASSERT_MESSAGE( "# scanline bytes mismatch for getPixel",
139 aLayout.ScanLineBytes == (200*nExpectedBitsPerPixel+7)/8);
140 CPPUNIT_ASSERT_MESSAGE( "# scanline stride mismatch for getPixel",
141 aLayout.ScanLineStride == (200*nExpectedBitsPerPixel+7)/8 ||
142 aLayout.ScanLineStride == -(200*nExpectedBitsPerPixel+7)/8);
144 uno::Sequence< rendering::RGBColor > aRGBColors = xBmp->convertIntegerToRGB( aPixelData );
145 uno::Sequence< rendering::ARGBColor > aARGBColors = xBmp->convertIntegerToARGB( aPixelData );
147 const rendering::RGBColor* pRGBStart ( aRGBColors.getConstArray() );
148 const rendering::RGBColor* pRGBEnd ( aRGBColors.getConstArray()+aRGBColors.getLength() );
149 const rendering::ARGBColor* pARGBStart( aARGBColors.getConstArray() );
150 std::pair<const rendering::RGBColor*,
151 const rendering::ARGBColor*> aRes = std::mismatch( pRGBStart, pRGBEnd, pARGBStart );
152 CPPUNIT_ASSERT_MESSAGE( "argb and rgb colors are not equal",
153 aRes.first == pRGBEnd);
155 CPPUNIT_ASSERT_MESSAGE( "rgb colors are not within [0,1] range",
156 std::none_of(pRGBStart,pRGBEnd,&rangeCheck));
158 CPPUNIT_ASSERT_MESSAGE( "First pixel is not white",
159 pRGBStart[0].Red == 1.0 && pRGBStart[0].Green == 1.0 && pRGBStart[0].Blue == 1.0);
160 CPPUNIT_ASSERT_MESSAGE( "Second pixel is not opaque",
161 pARGBStart[1].Alpha == 1.0);
162 if( aContainedBmpEx.IsTransparent() )
164 CPPUNIT_ASSERT_MESSAGE( "First pixel is not fully transparent",
165 pARGBStart[0].Alpha == 0.0);
168 CPPUNIT_ASSERT_MESSAGE( "Second pixel is not black",
169 pRGBStart[1].Red == 0.0 && pRGBStart[1].Green == 0.0 && pRGBStart[1].Blue == 0.0);
171 if( nOriginalDepth > 8 )
173 const Color aCol(COL_GREEN);
174 CPPUNIT_ASSERT_EQUAL_MESSAGE(
175 "Sixth pixel is not green (red component)",
176 vcl::unotools::toDoubleColor(aCol.GetRed()), pRGBStart[5].Red);
177 CPPUNIT_ASSERT_EQUAL_MESSAGE(
178 "Sixth pixel is not green (green component)",
179 vcl::unotools::toDoubleColor(aCol.GetGreen()), pRGBStart[5].Green);
180 CPPUNIT_ASSERT_EQUAL_MESSAGE(
181 "Sixth pixel is not green (blue component)",
182 vcl::unotools::toDoubleColor(aCol.GetBlue()), pRGBStart[5].Blue);
184 else if( nDepth <= 8 )
186 uno::Reference<rendering::XBitmapPalette> xPal = xBmp->getPalette();
187 CPPUNIT_ASSERT_MESSAGE( "8bit or less: missing palette",
188 xPal.is());
189 CPPUNIT_ASSERT_MESSAGE( "Palette incorrect entry count",
190 xPal->getNumberOfEntries() == 1L << nOriginalDepth);
191 uno::Sequence<double> aIndex;
192 CPPUNIT_ASSERT_MESSAGE( "Palette is not read-only",
193 !xPal->setIndex(aIndex,true,0));
194 CPPUNIT_ASSERT_MESSAGE( "Palette entry 0 is not opaque",
195 xPal->getIndex(aIndex,0));
196 CPPUNIT_ASSERT_MESSAGE( "Palette has no valid color space",
197 xPal->getColorSpace().is());
200 CPPUNIT_ASSERT_MESSAGE( "150th pixel is not white",
201 pRGBStart[150].Red == 1.0 &&
202 pRGBStart[150].Green == 1.0 &&
203 pRGBStart[150].Blue == 1.0);
205 if( nOriginalDepth > 8 )
207 uno::Sequence<rendering::ARGBColor> aARGBColor(1);
208 uno::Sequence<rendering::RGBColor> aRGBColor(1);
209 uno::Sequence<sal_Int8> aPixel3, aPixel4;
211 const Color aCol(COL_GREEN);
212 aARGBColor[0].Red = vcl::unotools::toDoubleColor(aCol.GetRed());
213 aARGBColor[0].Green = vcl::unotools::toDoubleColor(aCol.GetGreen());
214 aARGBColor[0].Blue = vcl::unotools::toDoubleColor(aCol.GetBlue());
215 aARGBColor[0].Alpha = 1.0;
217 aRGBColor[0].Red = vcl::unotools::toDoubleColor(aCol.GetRed());
218 aRGBColor[0].Green = vcl::unotools::toDoubleColor(aCol.GetGreen());
219 aRGBColor[0].Blue = vcl::unotools::toDoubleColor(aCol.GetBlue());
221 aPixel3 = xBmp->convertIntegerFromARGB( aARGBColor );
222 aPixel4 = xBmp->getPixel( aLayout, geometry::IntegerPoint2D(5,0) );
223 CPPUNIT_ASSERT_MESSAGE( "Green pixel from bitmap mismatch with manually converted green pixel",
224 aPixel3 == aPixel4);
226 if( !aContainedBmpEx.IsTransparent() )
228 aPixel3 = xBmp->convertIntegerFromRGB( aRGBColor );
229 CPPUNIT_ASSERT_MESSAGE( "Green pixel from bitmap mismatch with manually RGB-converted green pixel",
230 aPixel3 == aPixel4);
235 class TestBitmap : public cppu::WeakImplHelper3< rendering::XIntegerReadOnlyBitmap,
236 rendering::XBitmapPalette,
237 rendering::XIntegerBitmapColorSpace >
239 private:
240 geometry::IntegerSize2D maSize;
241 uno::Sequence<sal_Int8> maComponentTags;
242 uno::Sequence<sal_Int32> maComponentBitCounts;
243 rendering::IntegerBitmapLayout maLayout;
244 const sal_Int32 mnBitsPerPixel;
246 // XBitmap
247 virtual geometry::IntegerSize2D SAL_CALL getSize() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return maSize; }
248 virtual sal_Bool SAL_CALL hasAlpha( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return mnBitsPerPixel != 8; }
249 virtual uno::Reference< rendering::XBitmap > SAL_CALL getScaledBitmap( const geometry::RealSize2D&,
250 sal_Bool ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return this; }
252 // XIntegerReadOnlyBitmap
253 virtual uno::Sequence< ::sal_Int8 > SAL_CALL getData( rendering::IntegerBitmapLayout& bitmapLayout,
254 const geometry::IntegerRectangle2D& rect )
255 throw (lang::IndexOutOfBoundsException,
256 rendering::VolatileContentDestroyedException,
257 uno::RuntimeException,
258 std::exception) SAL_OVERRIDE
260 CPPUNIT_ASSERT_MESSAGE( "X1 out of bounds", rect.X1 >= 0 );
261 CPPUNIT_ASSERT_MESSAGE( "Y1 out of bounds", rect.Y1 >= 0 );
262 CPPUNIT_ASSERT_MESSAGE( "X2 out of bounds", rect.X2 <= maSize.Width );
263 CPPUNIT_ASSERT_MESSAGE( "Y2 out of bounds", rect.Y2 <= maSize.Height );
265 bitmapLayout = getMemoryLayout();
267 const sal_Int32 nWidth = rect.X2-rect.X1;
268 const sal_Int32 nHeight = rect.Y2-rect.Y1;
269 const sal_Int32 nScanlineLen = (nWidth * mnBitsPerPixel + 7)/8;
270 uno::Sequence<sal_Int8> aRes( nScanlineLen * nHeight );
271 sal_Int8* pOut = aRes.getArray();
273 bitmapLayout.ScanLines = nHeight;
274 bitmapLayout.ScanLineBytes =
275 bitmapLayout.ScanLineStride= nScanlineLen;
277 if( mnBitsPerPixel == 8 )
279 for( sal_Int32 y=0; y<nHeight; ++y )
281 for( sal_Int32 x=0; x<nWidth; ++x )
282 pOut[ y*nScanlineLen + x ] = sal_Int8(x);
285 else
287 for( sal_Int32 y=0; y<nHeight; ++y )
289 for( sal_Int32 x=0; x<nWidth; ++x )
291 pOut[ y*nScanlineLen + 4*x ] = sal_Int8(rect.X1);
292 pOut[ y*nScanlineLen + 4*x + 1 ] = sal_Int8(rect.Y2);
293 pOut[ y*nScanlineLen + 4*x + 2 ] = sal_Int8(x);
294 pOut[ y*nScanlineLen + 4*x + 3 ] = sal_Int8(rect.Y1);
299 return aRes;
302 virtual uno::Sequence< ::sal_Int8 > SAL_CALL getPixel( rendering::IntegerBitmapLayout&,
303 const geometry::IntegerPoint2D& )
304 throw (lang::IndexOutOfBoundsException,
305 rendering::VolatileContentDestroyedException,
306 uno::RuntimeException,
307 std::exception) SAL_OVERRIDE
309 CPPUNIT_ASSERT_MESSAGE("getPixel: method not implemented", false);
310 return uno::Sequence< sal_Int8 >();
313 uno::Reference< rendering::XBitmapPalette > SAL_CALL getPalette( ) throw (uno::RuntimeException)
315 uno::Reference< XBitmapPalette > aRet;
316 if( mnBitsPerPixel == 8 )
317 aRet.set(this);
318 return aRet;
321 virtual rendering::IntegerBitmapLayout SAL_CALL getMemoryLayout( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
323 rendering::IntegerBitmapLayout aLayout( maLayout );
325 const sal_Int32 nScanlineLen = (maSize.Width * mnBitsPerPixel + 7)/8;
327 aLayout.ScanLines = maSize.Height;
328 aLayout.ScanLineBytes =
329 aLayout.ScanLineStride= nScanlineLen;
330 aLayout.Palette = getPalette();
331 aLayout.ColorSpace.set( this );
333 return aLayout;
336 // XBitmapPalette
337 virtual sal_Int32 SAL_CALL getNumberOfEntries() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
339 CPPUNIT_ASSERT_MESSAGE( "Got palette getNumberOfEntries interface call without handing out palette",
340 getPalette().is() );
342 return 255;
345 virtual sal_Bool SAL_CALL getIndex( uno::Sequence< double >& entry,
346 ::sal_Int32 nIndex )
347 throw (lang::IndexOutOfBoundsException,
348 uno::RuntimeException,
349 std::exception) SAL_OVERRIDE
351 CPPUNIT_ASSERT_MESSAGE( "Got palette getIndex interface call without handing out palette",
352 getPalette().is() );
353 CPPUNIT_ASSERT_MESSAGE( "getIndex: index out of range",
354 nIndex >= 0 && nIndex < 256 );
355 entry = colorToStdColorSpaceSequence(
356 Color(sal_uInt8(nIndex),
357 sal_uInt8(nIndex),
358 sal_uInt8(nIndex)) );
360 return true; // no palette transparency here.
363 virtual sal_Bool SAL_CALL setIndex( const uno::Sequence< double >&,
364 sal_Bool,
365 ::sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException,
366 lang::IllegalArgumentException,
367 uno::RuntimeException,
368 std::exception) SAL_OVERRIDE
370 CPPUNIT_ASSERT_MESSAGE( "Got palette setIndex interface call without handing out palette",
371 getPalette().is());
372 CPPUNIT_ASSERT_MESSAGE( "setIndex: index out of range",
373 nIndex >= 0 && nIndex < 256);
374 return false;
377 struct PaletteColorSpaceHolder: public rtl::StaticWithInit<uno::Reference<rendering::XColorSpace>,
378 PaletteColorSpaceHolder>
380 uno::Reference<rendering::XColorSpace> operator()()
382 return vcl::unotools::createStandardColorSpace();
386 virtual uno::Reference< rendering::XColorSpace > SAL_CALL getColorSpace( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
388 // this is the method from XBitmapPalette. Return palette color
389 // space here
390 return PaletteColorSpaceHolder::get();
393 // XIntegerBitmapColorSpace
394 virtual ::sal_Int8 SAL_CALL getType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
396 return rendering::ColorSpaceType::RGB;
399 virtual uno::Sequence< sal_Int8 > SAL_CALL getComponentTags( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
401 return maComponentTags;
404 virtual ::sal_Int8 SAL_CALL getRenderingIntent( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
406 return rendering::RenderingIntent::PERCEPTUAL;
409 virtual uno::Sequence< beans::PropertyValue > SAL_CALL getProperties()
410 throw (uno::RuntimeException,
411 std::exception) SAL_OVERRIDE
413 CPPUNIT_ASSERT_MESSAGE("getProperties: method not implemented", false );
414 return uno::Sequence< ::beans::PropertyValue >();
417 virtual uno::Sequence< double > SAL_CALL convertColorSpace( const uno::Sequence< double >&,
418 const uno::Reference< rendering::XColorSpace >& )
419 throw (uno::RuntimeException,
420 std::exception) SAL_OVERRIDE
422 CPPUNIT_ASSERT_MESSAGE("convertColorSpace: method not implemented", false);
423 return uno::Sequence< double >();
426 virtual uno::Sequence< rendering::RGBColor > SAL_CALL convertToRGB( const uno::Sequence< double >& )
427 throw (lang::IllegalArgumentException,
428 uno::RuntimeException,
429 std::exception) SAL_OVERRIDE
431 CPPUNIT_ASSERT_MESSAGE("convertToRGB: method not implemented", false);
432 return uno::Sequence< rendering::RGBColor >();
435 virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToARGB( const uno::Sequence< double >& )
436 throw (lang::IllegalArgumentException,
437 uno::RuntimeException,
438 std::exception) SAL_OVERRIDE
440 CPPUNIT_ASSERT_MESSAGE("convertToARGB: method not implemented", false);
441 return uno::Sequence< rendering::ARGBColor >();
444 virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToPARGB( const uno::Sequence< double >& )
445 throw (lang::IllegalArgumentException,
446 uno::RuntimeException,
447 std::exception) SAL_OVERRIDE
449 CPPUNIT_ASSERT_MESSAGE("convertToPARGB: method not implemented", false);
450 return uno::Sequence< rendering::ARGBColor >();
453 virtual uno::Sequence< double > SAL_CALL convertFromRGB( const uno::Sequence< rendering::RGBColor >& )
454 throw (lang::IllegalArgumentException,
455 uno::RuntimeException,
456 std::exception) SAL_OVERRIDE
458 CPPUNIT_ASSERT_MESSAGE("convertFromRGB: method not implemented", false);
459 return uno::Sequence< double >();
462 virtual uno::Sequence< double > SAL_CALL convertFromARGB( const uno::Sequence< rendering::ARGBColor >& )
463 throw (lang::IllegalArgumentException,
464 uno::RuntimeException,
465 std::exception) SAL_OVERRIDE
467 CPPUNIT_ASSERT_MESSAGE("convertFromARGB: this method is not expected to be called!", false);
468 return uno::Sequence< double >();
471 virtual uno::Sequence< double > SAL_CALL convertFromPARGB( const uno::Sequence< rendering::ARGBColor >& )
472 throw (lang::IllegalArgumentException,
473 uno::RuntimeException,
474 std::exception) SAL_OVERRIDE
476 CPPUNIT_ASSERT_MESSAGE("convertFromPARGB: this method is not expected to be called!", false);
477 return uno::Sequence< double >();
480 virtual ::sal_Int32 SAL_CALL getBitsPerPixel( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
482 return mnBitsPerPixel;
485 virtual uno::Sequence< ::sal_Int32 > SAL_CALL getComponentBitCounts( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
487 return maComponentBitCounts;
490 virtual ::sal_Int8 SAL_CALL getEndianness( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
492 return util::Endianness::LITTLE;
495 virtual uno::Sequence< double > SAL_CALL convertFromIntegerColorSpace( const uno::Sequence< ::sal_Int8 >& ,
496 const uno::Reference< rendering::XColorSpace >& )
497 throw (lang::IllegalArgumentException,
498 uno::RuntimeException,
499 std::exception) SAL_OVERRIDE
501 CPPUNIT_ASSERT_MESSAGE("convertFromIntegerColorSpace: method not implemented", false);
502 return uno::Sequence< double >();
505 virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertToIntegerColorSpace( const uno::Sequence< ::sal_Int8 >& ,
506 const uno::Reference< rendering::XIntegerBitmapColorSpace >& )
507 throw (lang::IllegalArgumentException,
508 uno::RuntimeException,
509 std::exception) SAL_OVERRIDE
511 CPPUNIT_ASSERT_MESSAGE("convertToIntegerColorSpace: method not implemented", false);
512 return uno::Sequence< sal_Int8 >();
515 virtual uno::Sequence< rendering::RGBColor > SAL_CALL convertIntegerToRGB( const uno::Sequence< ::sal_Int8 >& deviceColor ) throw (lang::IllegalArgumentException,
516 uno::RuntimeException, std::exception) SAL_OVERRIDE
518 const uno::Sequence< rendering::ARGBColor > aTemp( convertIntegerToARGB(deviceColor) );
519 const sal_Size nLen(aTemp.getLength());
520 uno::Sequence< rendering::RGBColor > aRes( nLen );
521 rendering::RGBColor* pOut = aRes.getArray();
522 for( sal_Size i=0; i<nLen; ++i )
524 *pOut++ = rendering::RGBColor(aTemp[i].Red,
525 aTemp[i].Green,
526 aTemp[i].Blue);
529 return aRes;
532 virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertIntegerToARGB( const uno::Sequence< ::sal_Int8 >& deviceColor ) throw (lang::IllegalArgumentException,
533 uno::RuntimeException, std::exception) SAL_OVERRIDE
535 const sal_Size nLen( deviceColor.getLength() );
536 const sal_Int32 nBytesPerPixel(mnBitsPerPixel == 8 ? 1 : 4);
537 CPPUNIT_ASSERT_MESSAGE("number of channels no multiple of pixel element count",
538 nLen%nBytesPerPixel==0);
540 uno::Sequence< rendering::ARGBColor > aRes( nLen / nBytesPerPixel );
541 rendering::ARGBColor* pOut( aRes.getArray() );
543 if( getPalette().is() )
545 for( sal_Size i=0; i<nLen; ++i )
547 *pOut++ = rendering::ARGBColor(
548 1.0,
549 vcl::unotools::toDoubleColor(deviceColor[i]),
550 vcl::unotools::toDoubleColor(deviceColor[i]),
551 vcl::unotools::toDoubleColor(deviceColor[i]));
554 else
556 for( sal_Size i=0; i<nLen; i+=4 )
558 *pOut++ = rendering::ARGBColor(
559 vcl::unotools::toDoubleColor(deviceColor[i+3]),
560 vcl::unotools::toDoubleColor(deviceColor[i+0]),
561 vcl::unotools::toDoubleColor(deviceColor[i+1]),
562 vcl::unotools::toDoubleColor(deviceColor[i+2]));
566 return aRes;
569 virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertIntegerToPARGB(
570 const uno::Sequence< ::sal_Int8 >& deviceColor)
571 throw (lang::IllegalArgumentException,
572 uno::RuntimeException,
573 std::exception) SAL_OVERRIDE
575 const sal_Size nLen( deviceColor.getLength() );
576 const sal_Int32 nBytesPerPixel(mnBitsPerPixel == 8 ? 1 : 4);
577 CPPUNIT_ASSERT_MESSAGE("number of channels no multiple of pixel element count",
578 nLen%nBytesPerPixel==0);
580 uno::Sequence< rendering::ARGBColor > aRes( nLen / nBytesPerPixel );
581 rendering::ARGBColor* pOut( aRes.getArray() );
583 if( getPalette().is() )
585 for( sal_Size i=0; i<nLen; ++i )
587 *pOut++ = rendering::ARGBColor(
588 1.0,
589 vcl::unotools::toDoubleColor(deviceColor[i]),
590 vcl::unotools::toDoubleColor(deviceColor[i]),
591 vcl::unotools::toDoubleColor(deviceColor[i]));
594 else
596 for( sal_Size i=0; i<nLen; i+=4 )
598 const double fAlpha=vcl::unotools::toDoubleColor(deviceColor[i+3]);
599 *pOut++ = rendering::ARGBColor(
600 fAlpha,
601 fAlpha*vcl::unotools::toDoubleColor(deviceColor[i+0]),
602 fAlpha*vcl::unotools::toDoubleColor(deviceColor[i+1]),
603 fAlpha*vcl::unotools::toDoubleColor(deviceColor[i+2]));
607 return aRes;
610 virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromRGB(
611 const uno::Sequence< rendering::RGBColor >&)
612 throw (lang::IllegalArgumentException,
613 uno::RuntimeException,
614 std::exception) SAL_OVERRIDE
616 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromRGB: method not implemented", false);
617 return uno::Sequence< sal_Int8 >();
620 virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromARGB( const uno::Sequence< rendering::ARGBColor >& ) throw (lang::IllegalArgumentException,
621 uno::RuntimeException,
622 std::exception) SAL_OVERRIDE
624 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromARGB: method not implemented", false);
625 return uno::Sequence< sal_Int8 >();
628 virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromPARGB( const uno::Sequence< rendering::ARGBColor >& )
629 throw (lang::IllegalArgumentException,
630 uno::RuntimeException,
631 std::exception) SAL_OVERRIDE
633 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromPARGB: method not implemented", false);
634 return uno::Sequence< sal_Int8 >();
637 public:
638 TestBitmap( const geometry::IntegerSize2D& rSize, bool bPalette ) :
639 maSize(rSize),
640 maComponentTags(),
641 maComponentBitCounts(),
642 maLayout(),
643 mnBitsPerPixel( bPalette ? 8 : 32 )
645 if( bPalette )
647 maComponentTags.realloc(1);
648 maComponentTags[0] = rendering::ColorComponentTag::INDEX;
650 maComponentBitCounts.realloc(1);
651 maComponentBitCounts[0] = 8;
653 else
655 maComponentTags.realloc(4);
656 sal_Int8* pTags = maComponentTags.getArray();
657 pTags[0] = rendering::ColorComponentTag::RGB_BLUE;
658 pTags[1] = rendering::ColorComponentTag::RGB_GREEN;
659 pTags[2] = rendering::ColorComponentTag::RGB_RED;
660 pTags[3] = rendering::ColorComponentTag::ALPHA;
662 maComponentBitCounts.realloc(4);
663 sal_Int32* pCounts = maComponentBitCounts.getArray();
664 pCounts[0] = 8;
665 pCounts[1] = 8;
666 pCounts[2] = 8;
667 pCounts[3] = 8;
670 maLayout.ScanLines = 0;
671 maLayout.ScanLineBytes = 0;
672 maLayout.ScanLineStride = 0;
673 maLayout.PlaneStride = 0;
674 maLayout.ColorSpace.clear();
675 maLayout.Palette.clear();
676 maLayout.IsMsbFirst = false;
680 void CanvasBitmapTest::runTest()
682 static const sal_Int8 lcl_depths[]={1,4,8,16,24};
684 // Testing VclCanvasBitmap wrapper
686 for( unsigned int i=0; i<SAL_N_ELEMENTS(lcl_depths); ++i )
688 const sal_Int8 nDepth( lcl_depths[i] );
689 Bitmap aBitmap(Size(200,200),nDepth);
690 aBitmap.Erase(COL_WHITE);
692 Bitmap::ScopedWriteAccess pAcc(aBitmap);
693 if( pAcc.get() )
695 BitmapColor aBlack(0);
696 BitmapColor aWhite(0);
697 if( pAcc->HasPalette() )
699 aBlack.SetIndex( sal::static_int_cast<sal_Int8>(pAcc->GetBestPaletteIndex(BitmapColor(0,0,0))) );
700 aWhite.SetIndex( sal::static_int_cast<sal_Int8>(pAcc->GetBestPaletteIndex(BitmapColor(255,255,255))) );
702 else
704 aBlack = Color(COL_BLACK);
705 aWhite = Color(COL_WHITE);
707 pAcc->SetFillColor(COL_GREEN);
708 pAcc->FillRect(Rectangle(0,0,100,100));
709 pAcc->SetPixel(0,0,aWhite);
710 pAcc->SetPixel(0,1,aBlack);
711 pAcc->SetPixel(0,2,aWhite);
715 rtl::Reference<VclCanvasBitmap> xBmp( new VclCanvasBitmap(aBitmap) );
717 checkCanvasBitmap( xBmp, "single bitmap", nDepth );
719 Bitmap aMask(Size(200,200),1);
720 aMask.Erase(COL_WHITE);
722 Bitmap::ScopedWriteAccess pAcc(aMask);
723 if( pAcc.get() )
725 pAcc->SetFillColor(COL_BLACK);
726 pAcc->FillRect(Rectangle(0,0,100,100));
727 pAcc->SetPixel(0,0,BitmapColor(1));
728 pAcc->SetPixel(0,1,BitmapColor(0));
729 pAcc->SetPixel(0,2,BitmapColor(1));
733 xBmp.set( new VclCanvasBitmap(BitmapEx(aBitmap,aMask)) );
735 checkCanvasBitmap( xBmp, "masked bitmap", nDepth );
737 AlphaMask aAlpha(Size(200,200));
738 aAlpha.Erase(255);
740 BitmapWriteAccess* pAcc = aAlpha.AcquireWriteAccess();
741 if( pAcc )
743 pAcc->SetFillColor(COL_BLACK);
744 pAcc->FillRect(Rectangle(0,0,100,100));
745 pAcc->SetPixel(0,0,BitmapColor(255));
746 pAcc->SetPixel(0,1,BitmapColor(0));
747 pAcc->SetPixel(0,2,BitmapColor(255));
748 aAlpha.ReleaseAccess(pAcc);
752 xBmp.set( new VclCanvasBitmap(BitmapEx(aBitmap,aAlpha)) );
754 checkCanvasBitmap( xBmp, "alpha bitmap", nDepth );
757 // Testing XBitmap import
759 uno::Reference< rendering::XIntegerReadOnlyBitmap > xTestBmp(
760 new TestBitmap( geometry::IntegerSize2D(10,10), true ));
762 BitmapEx aBmp = vcl::unotools::bitmapExFromXBitmap(xTestBmp);
763 CPPUNIT_ASSERT_MESSAGE( "Palette bitmap is transparent",
764 !aBmp.IsTransparent());
765 CPPUNIT_ASSERT_MESSAGE( "Bitmap does not have size (10,10)",
766 aBmp.GetSizePixel() == Size(10,10));
767 CPPUNIT_ASSERT_MESSAGE( "Bitmap does not have bitcount of 8",
768 aBmp.GetBitCount() == 8);
770 BitmapReadAccess* pBmpAcc = aBmp.GetBitmap().AcquireReadAccess();
772 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid BitmapReadAccess",
773 pBmpAcc );
775 CPPUNIT_ASSERT_MESSAGE("(0,0) incorrect content",
776 pBmpAcc->GetPixel(0,0) == BitmapColor(0));
777 CPPUNIT_ASSERT_MESSAGE("(2,2) incorrect content",
778 pBmpAcc->GetPixel(2,2) == BitmapColor(2));
779 CPPUNIT_ASSERT_MESSAGE("(9,2) incorrect content",
780 pBmpAcc->GetPixel(2,9) == BitmapColor(9));
782 Bitmap::ReleaseAccess(pBmpAcc);
785 xTestBmp.set( new TestBitmap( geometry::IntegerSize2D(10,10), false ));
787 aBmp = vcl::unotools::bitmapExFromXBitmap(xTestBmp);
788 CPPUNIT_ASSERT_MESSAGE( "Palette bitmap is not transparent",
789 aBmp.IsTransparent());
790 CPPUNIT_ASSERT_MESSAGE( "Palette bitmap has no alpha",
791 aBmp.IsAlpha());
792 CPPUNIT_ASSERT_MESSAGE( "Bitmap does not have size (10,10)",
793 aBmp.GetSizePixel() == Size(10,10));
794 CPPUNIT_ASSERT_MESSAGE( "Bitmap has bitcount of 24",
795 aBmp.GetBitCount() == 24);
797 BitmapReadAccess* pBmpAcc = aBmp.GetBitmap().AcquireReadAccess();
798 BitmapReadAccess* pAlphaAcc = aBmp.GetAlpha().AcquireReadAccess();
800 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid BitmapReadAccess",
801 pBmpAcc);
802 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid alpha BitmapReadAccess",
803 pAlphaAcc);
805 CPPUNIT_ASSERT_MESSAGE("(0,0) incorrect content",
806 pBmpAcc->GetPixel(0,0) == BitmapColor(0,1,0));
807 CPPUNIT_ASSERT_MESSAGE("(0,0) incorrect alpha content",
808 pAlphaAcc->GetPixel(0,0) == BitmapColor(255));
809 CPPUNIT_ASSERT_MESSAGE("(2,2) incorrect content",
810 pBmpAcc->GetPixel(2,2) == BitmapColor(0,3,2));
811 CPPUNIT_ASSERT_MESSAGE("(2,2) incorrect alpha content",
812 pAlphaAcc->GetPixel(2,2) == BitmapColor(253));
813 CPPUNIT_ASSERT_MESSAGE("(9,2) incorrect content",
814 pBmpAcc->GetPixel(2,9) == BitmapColor(0,3,9));
815 CPPUNIT_ASSERT_MESSAGE("(9,2) correct alpha content",
816 pAlphaAcc->GetPixel(2,9) == BitmapColor(253));
818 aBmp.GetAlpha().ReleaseAccess(pAlphaAcc);
819 Bitmap::ReleaseAccess(pBmpAcc);
823 } // namespace
825 CPPUNIT_TEST_SUITE_REGISTRATION(CanvasBitmapTest);
827 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */