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/.
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 .
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/implbase.hxx>
32 #include <rtl/ref.hxx>
33 #include <sal/log.hxx>
35 #include <vcl/canvastools.hxx>
36 #include <vcl/bitmapaccess.hxx>
37 #include <vcl/bitmapex.hxx>
39 #include <canvasbitmap.hxx>
41 #include <bitmapwriteaccess.hxx>
43 using namespace ::com::sun::star
;
44 using namespace vcl::unotools
;
46 namespace com::sun::star::rendering
49 static bool operator==( const RGBColor
& rLHS
, const ARGBColor
& rRHS
)
51 return rLHS
.Red
== rRHS
.Red
&& rLHS
.Green
== rRHS
.Green
&& rLHS
.Blue
== rRHS
.Blue
;
59 class CanvasBitmapTest
: public test::BootstrapFixture
62 CanvasBitmapTest() : BootstrapFixture(true, false) {}
66 CPPUNIT_TEST_SUITE(CanvasBitmapTest
);
67 CPPUNIT_TEST(runTest
);
68 CPPUNIT_TEST_SUITE_END();
71 bool rangeCheck( const rendering::RGBColor
& rColor
)
73 return rColor
.Red
< 0.0 || rColor
.Red
> 1.0 ||
74 rColor
.Green
< 0.0 || rColor
.Green
> 1.0 ||
75 rColor
.Blue
< 0.0 || rColor
.Blue
> 1.0;
78 void checkCanvasBitmap( const rtl::Reference
<VclCanvasBitmap
>& xBmp
,
82 SAL_INFO("vcl", "Testing " << msg
<< ", with depth " << nOriginalDepth
);
84 BitmapEx
aContainedBmpEx( xBmp
->getBitmapEx() );
85 Bitmap
aContainedBmp( aContainedBmpEx
.GetBitmap() );
86 int nDepth
= nOriginalDepth
;
90 Bitmap::ScopedReadAccess
pAcc( aContainedBmp
);
91 nDepth
= pAcc
->GetBitCount();
92 if( pAcc
->GetScanlineFormat() == ScanlineFormat::N32BitTcMask
)
93 extraBpp
= 8; // the format has 8 unused bits
96 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Original bitmap size not (200,200)",
97 Size(200,200), aContainedBmp
.GetSizePixel());
99 CPPUNIT_ASSERT_MESSAGE( "Original bitmap size via API not (200,200)",
100 xBmp
->getSize().Width
== 200 && xBmp
->getSize().Height
== 200);
102 CPPUNIT_ASSERT_EQUAL_MESSAGE( "alpha state mismatch",
103 aContainedBmpEx
.IsTransparent(), bool(xBmp
->hasAlpha()));
105 CPPUNIT_ASSERT_MESSAGE( "getScaledBitmap() failed",
106 xBmp
->getScaledBitmap( geometry::RealSize2D(500,500), false ).is());
108 rendering::IntegerBitmapLayout aLayout
;
109 uno::Sequence
<sal_Int8
> aPixelData
= xBmp
->getData(aLayout
, geometry::IntegerRectangle2D(0,0,1,1));
111 const sal_Int32
nExpectedBitsPerPixel(
112 (aContainedBmpEx
.IsTransparent() ? std::max(8,nDepth
)+8 : nDepth
) + extraBpp
);
113 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# scanlines not 1",
114 static_cast<sal_Int32
>(1), aLayout
.ScanLines
);
115 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# scanline bytes mismatch",
116 static_cast<sal_Int32
>((nExpectedBitsPerPixel
+7)/8), aLayout
.ScanLineBytes
);
117 CPPUNIT_ASSERT_MESSAGE( "# scanline stride mismatch",
118 aLayout
.ScanLineStride
== (nExpectedBitsPerPixel
+7)/8 ||
119 aLayout
.ScanLineStride
== -(nExpectedBitsPerPixel
+7)/8);
120 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# plane stride not 0",
121 static_cast<sal_Int32
>(0), aLayout
.PlaneStride
);
123 CPPUNIT_ASSERT_MESSAGE( "Color space not there",
124 aLayout
.ColorSpace
.is());
126 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Palette existence does not conform to bitmap",
127 (nDepth
<= 8), aLayout
.Palette
.is());
129 uno::Sequence
<sal_Int8
> aPixelData2
= xBmp
->getPixel( aLayout
, geometry::IntegerPoint2D(0,0) );
131 CPPUNIT_ASSERT_EQUAL_MESSAGE( "getData and getPixel did not return same amount of data",
132 aPixelData
.getLength(), aPixelData2
.getLength());
134 aPixelData
= xBmp
->getData(aLayout
, geometry::IntegerRectangle2D(0,0,200,1));
135 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# scanlines not 1 for getPixel",
136 static_cast<sal_Int32
>(1), aLayout
.ScanLines
);
137 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# scanline bytes mismatch for getPixel",
138 static_cast<sal_Int32
>((200*nExpectedBitsPerPixel
+7)/8), aLayout
.ScanLineBytes
);
139 CPPUNIT_ASSERT_MESSAGE( "# scanline stride mismatch for getPixel",
140 aLayout
.ScanLineStride
== (200*nExpectedBitsPerPixel
+7)/8 ||
141 aLayout
.ScanLineStride
== -(200*nExpectedBitsPerPixel
+7)/8);
143 uno::Sequence
< rendering::RGBColor
> aRGBColors
= xBmp
->convertIntegerToRGB( aPixelData
);
144 uno::Sequence
< rendering::ARGBColor
> aARGBColors
= xBmp
->convertIntegerToARGB( aPixelData
);
146 const rendering::RGBColor
* pRGBStart ( aRGBColors
.getConstArray() );
147 const rendering::RGBColor
* pRGBEnd ( aRGBColors
.getConstArray()+aRGBColors
.getLength() );
148 const rendering::ARGBColor
* pARGBStart( aARGBColors
.getConstArray() );
149 std::pair
<const rendering::RGBColor
*,
150 const rendering::ARGBColor
*> aRes
= std::mismatch( pRGBStart
, pRGBEnd
, pARGBStart
);
151 CPPUNIT_ASSERT_EQUAL_MESSAGE( "argb and rgb colors are not equal",
152 pRGBEnd
, aRes
.first
);
154 CPPUNIT_ASSERT_MESSAGE( "rgb colors are not within [0,1] range",
155 std::none_of(pRGBStart
,pRGBEnd
,&rangeCheck
));
157 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
158 "First pixel is not white", 1.0, pRGBStart
[0].Red
, 1E-12);
159 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
160 "First pixel is not white", 1.0, pRGBStart
[0].Green
, 1E-12);
161 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
162 "First pixel is not white", 1.0, pRGBStart
[0].Blue
, 1E-12);
163 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
164 "Second pixel is not opaque", 1.0, pARGBStart
[1].Alpha
, 1E-12);
165 if( aContainedBmpEx
.IsTransparent() )
167 CPPUNIT_ASSERT_EQUAL_MESSAGE( "First pixel is not fully transparent",
168 0.0, pARGBStart
[0].Alpha
);
171 CPPUNIT_ASSERT_MESSAGE( "Second pixel is not black",
172 pRGBStart
[1].Red
== 0.0 && pRGBStart
[1].Green
== 0.0 && pRGBStart
[1].Blue
== 0.0);
174 if( nOriginalDepth
> 8 )
176 const Color
aCol(COL_GREEN
);
177 CPPUNIT_ASSERT_EQUAL_MESSAGE(
178 "Sixth pixel is not green (red component)",
179 vcl::unotools::toDoubleColor(aCol
.GetRed()), pRGBStart
[5].Red
);
180 CPPUNIT_ASSERT_EQUAL_MESSAGE(
181 "Sixth pixel is not green (green component)",
182 vcl::unotools::toDoubleColor(aCol
.GetGreen()), pRGBStart
[5].Green
);
183 CPPUNIT_ASSERT_EQUAL_MESSAGE(
184 "Sixth pixel is not green (blue component)",
185 vcl::unotools::toDoubleColor(aCol
.GetBlue()), pRGBStart
[5].Blue
);
187 else if( nDepth
<= 8 )
189 uno::Reference
<rendering::XBitmapPalette
> xPal
= xBmp
->getPalette();
190 CPPUNIT_ASSERT_MESSAGE( "8bit or less: missing palette",
192 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Palette incorrect entry count",
193 static_cast<sal_Int32
>(1 << nOriginalDepth
), xPal
->getNumberOfEntries());
194 uno::Sequence
<double> aIndex
;
195 CPPUNIT_ASSERT_MESSAGE( "Palette is not read-only",
196 !xPal
->setIndex(aIndex
,true,0));
197 CPPUNIT_ASSERT_MESSAGE( "Palette entry 0 is not opaque",
198 xPal
->getIndex(aIndex
,0));
199 CPPUNIT_ASSERT_MESSAGE( "Palette has no valid color space",
200 xPal
->getColorSpace().is());
203 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
204 "150th pixel is not white", 1.0, pRGBStart
[150].Red
, 1E-12);
205 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
206 "150th pixel is not white", 1.0, pRGBStart
[150].Green
, 1E-12);
207 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
208 "150th pixel is not white", 1.0, pRGBStart
[150].Blue
, 1E-12);
210 if( nOriginalDepth
<= 8 )
213 uno::Sequence
<rendering::ARGBColor
> aARGBColor(1);
214 uno::Sequence
<rendering::RGBColor
> aRGBColor(1);
215 uno::Sequence
<sal_Int8
> aPixel3
, aPixel4
;
217 const Color
aCol(COL_GREEN
);
218 aARGBColor
[0].Red
= vcl::unotools::toDoubleColor(aCol
.GetRed());
219 aARGBColor
[0].Green
= vcl::unotools::toDoubleColor(aCol
.GetGreen());
220 aARGBColor
[0].Blue
= vcl::unotools::toDoubleColor(aCol
.GetBlue());
221 aARGBColor
[0].Alpha
= 1.0;
223 aRGBColor
[0].Red
= vcl::unotools::toDoubleColor(aCol
.GetRed());
224 aRGBColor
[0].Green
= vcl::unotools::toDoubleColor(aCol
.GetGreen());
225 aRGBColor
[0].Blue
= vcl::unotools::toDoubleColor(aCol
.GetBlue());
227 aPixel3
= xBmp
->convertIntegerFromARGB( aARGBColor
);
228 aPixel4
= xBmp
->getPixel( aLayout
, geometry::IntegerPoint2D(5,0) );
229 CPPUNIT_ASSERT_MESSAGE( "Green pixel from bitmap mismatch with manually converted green pixel",
230 bool(aPixel3
== aPixel4
));
232 if( !aContainedBmpEx
.IsTransparent() )
234 aPixel3
= xBmp
->convertIntegerFromRGB( aRGBColor
);
235 CPPUNIT_ASSERT_MESSAGE( "Green pixel from bitmap mismatch with manually RGB-converted green pixel",
236 bool(aPixel3
== aPixel4
));
241 class TestBitmap
: public cppu::WeakImplHelper
< rendering::XIntegerReadOnlyBitmap
,
242 rendering::XBitmapPalette
,
243 rendering::XIntegerBitmapColorSpace
>
246 geometry::IntegerSize2D maSize
;
247 uno::Sequence
<sal_Int8
> maComponentTags
;
248 uno::Sequence
<sal_Int32
> maComponentBitCounts
;
249 rendering::IntegerBitmapLayout maLayout
;
250 const sal_Int32 mnBitsPerPixel
;
253 virtual geometry::IntegerSize2D SAL_CALL
getSize() override
{ return maSize
; }
254 virtual sal_Bool SAL_CALL
hasAlpha( ) override
{ return mnBitsPerPixel
!= 8; }
255 virtual uno::Reference
< rendering::XBitmap
> SAL_CALL
getScaledBitmap( const geometry::RealSize2D
&,
256 sal_Bool
) override
{ return this; }
258 // XIntegerReadOnlyBitmap
259 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getData( rendering::IntegerBitmapLayout
& bitmapLayout
,
260 const geometry::IntegerRectangle2D
& rect
) override
262 CPPUNIT_ASSERT_MESSAGE( "X1 out of bounds", rect
.X1
>= 0 );
263 CPPUNIT_ASSERT_MESSAGE( "Y1 out of bounds", rect
.Y1
>= 0 );
264 CPPUNIT_ASSERT_MESSAGE( "X2 out of bounds", rect
.X2
<= maSize
.Width
);
265 CPPUNIT_ASSERT_MESSAGE( "Y2 out of bounds", rect
.Y2
<= maSize
.Height
);
267 bitmapLayout
= getMemoryLayout();
269 const sal_Int32 nWidth
= rect
.X2
-rect
.X1
;
270 const sal_Int32 nHeight
= rect
.Y2
-rect
.Y1
;
271 const sal_Int32 nScanlineLen
= (nWidth
* mnBitsPerPixel
+ 7)/8;
272 uno::Sequence
<sal_Int8
> aRes( nScanlineLen
* nHeight
);
273 sal_Int8
* pOut
= aRes
.getArray();
275 bitmapLayout
.ScanLines
= nHeight
;
276 bitmapLayout
.ScanLineBytes
=
277 bitmapLayout
.ScanLineStride
= nScanlineLen
;
279 if( mnBitsPerPixel
== 8 )
281 for( sal_Int32 y
=0; y
<nHeight
; ++y
)
283 for( sal_Int32 x
=0; x
<nWidth
; ++x
)
284 pOut
[ y
*nScanlineLen
+ x
] = sal_Int8(x
);
289 for( sal_Int32 y
=0; y
<nHeight
; ++y
)
291 for( sal_Int32 x
=0; x
<nWidth
; ++x
)
293 pOut
[ y
*nScanlineLen
+ 4*x
] = sal_Int8(rect
.X1
);
294 pOut
[ y
*nScanlineLen
+ 4*x
+ 1 ] = sal_Int8(rect
.Y2
);
295 pOut
[ y
*nScanlineLen
+ 4*x
+ 2 ] = sal_Int8(x
);
296 pOut
[ y
*nScanlineLen
+ 4*x
+ 3 ] = sal_Int8(rect
.Y1
);
304 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getPixel( rendering::IntegerBitmapLayout
&,
305 const geometry::IntegerPoint2D
& ) override
307 CPPUNIT_ASSERT_MESSAGE("getPixel: method not implemented", false);
308 return uno::Sequence
< sal_Int8
>();
311 /// @throws uno::RuntimeException
312 uno::Reference
< rendering::XBitmapPalette
> getPalette( )
314 uno::Reference
< XBitmapPalette
> aRet
;
315 if( mnBitsPerPixel
== 8 )
320 virtual rendering::IntegerBitmapLayout SAL_CALL
getMemoryLayout( ) override
322 rendering::IntegerBitmapLayout
aLayout( maLayout
);
324 const sal_Int32 nScanlineLen
= (maSize
.Width
* mnBitsPerPixel
+ 7)/8;
326 aLayout
.ScanLines
= maSize
.Height
;
327 aLayout
.ScanLineBytes
=
328 aLayout
.ScanLineStride
= nScanlineLen
;
329 aLayout
.Palette
= getPalette();
330 aLayout
.ColorSpace
.set( this );
336 virtual sal_Int32 SAL_CALL
getNumberOfEntries() override
338 CPPUNIT_ASSERT_MESSAGE( "Got palette getNumberOfEntries interface call without handing out palette",
344 virtual sal_Bool SAL_CALL
getIndex( uno::Sequence
< double >& entry
,
345 ::sal_Int32 nIndex
) override
347 CPPUNIT_ASSERT_MESSAGE( "Got palette getIndex interface call without handing out palette",
349 CPPUNIT_ASSERT_MESSAGE( "getIndex: index out of range",
350 nIndex
>= 0 && nIndex
< 256 );
351 entry
= colorToStdColorSpaceSequence(
352 Color(sal_uInt8(nIndex
),
354 sal_uInt8(nIndex
)) );
356 return true; // no palette transparency here.
359 virtual sal_Bool SAL_CALL
setIndex( const uno::Sequence
< double >&,
361 ::sal_Int32 nIndex
) override
363 CPPUNIT_ASSERT_MESSAGE( "Got palette setIndex interface call without handing out palette",
365 CPPUNIT_ASSERT_MESSAGE( "setIndex: index out of range",
366 nIndex
>= 0 && nIndex
< 256);
370 struct PaletteColorSpaceHolder
: public rtl::StaticWithInit
<uno::Reference
<rendering::XColorSpace
>,
371 PaletteColorSpaceHolder
>
373 uno::Reference
<rendering::XColorSpace
> operator()()
375 return vcl::unotools::createStandardColorSpace();
379 virtual uno::Reference
< rendering::XColorSpace
> SAL_CALL
getColorSpace( ) override
381 // this is the method from XBitmapPalette. Return palette color
383 return PaletteColorSpaceHolder::get();
386 // XIntegerBitmapColorSpace
387 virtual ::sal_Int8 SAL_CALL
getType( ) override
389 return rendering::ColorSpaceType::RGB
;
392 virtual uno::Sequence
< sal_Int8
> SAL_CALL
getComponentTags( ) override
394 return maComponentTags
;
397 virtual ::sal_Int8 SAL_CALL
getRenderingIntent( ) override
399 return rendering::RenderingIntent::PERCEPTUAL
;
402 virtual uno::Sequence
< beans::PropertyValue
> SAL_CALL
getProperties() override
404 CPPUNIT_ASSERT_MESSAGE("getProperties: method not implemented", false );
405 return uno::Sequence
< ::beans::PropertyValue
>();
408 virtual uno::Sequence
< double > SAL_CALL
convertColorSpace( const uno::Sequence
< double >&,
409 const uno::Reference
< rendering::XColorSpace
>& ) override
411 CPPUNIT_ASSERT_MESSAGE("convertColorSpace: method not implemented", false);
412 return uno::Sequence
< double >();
415 virtual uno::Sequence
< rendering::RGBColor
> SAL_CALL
convertToRGB( const uno::Sequence
< double >& ) override
417 CPPUNIT_ASSERT_MESSAGE("convertToRGB: method not implemented", false);
418 return uno::Sequence
< rendering::RGBColor
>();
421 virtual uno::Sequence
< rendering::ARGBColor
> SAL_CALL
convertToARGB( const uno::Sequence
< double >& ) override
423 CPPUNIT_ASSERT_MESSAGE("convertToARGB: method not implemented", false);
424 return uno::Sequence
< rendering::ARGBColor
>();
427 virtual uno::Sequence
< rendering::ARGBColor
> SAL_CALL
convertToPARGB( const uno::Sequence
< double >& ) override
429 CPPUNIT_ASSERT_MESSAGE("convertToPARGB: method not implemented", false);
430 return uno::Sequence
< rendering::ARGBColor
>();
433 virtual uno::Sequence
< double > SAL_CALL
convertFromRGB( const uno::Sequence
< rendering::RGBColor
>& ) override
435 CPPUNIT_ASSERT_MESSAGE("convertFromRGB: method not implemented", false);
436 return uno::Sequence
< double >();
439 virtual uno::Sequence
< double > SAL_CALL
convertFromARGB( const uno::Sequence
< rendering::ARGBColor
>& ) override
441 CPPUNIT_ASSERT_MESSAGE("convertFromARGB: this method is not expected to be called!", false);
442 return uno::Sequence
< double >();
445 virtual uno::Sequence
< double > SAL_CALL
convertFromPARGB( const uno::Sequence
< rendering::ARGBColor
>& ) override
447 CPPUNIT_ASSERT_MESSAGE("convertFromPARGB: this method is not expected to be called!", false);
448 return uno::Sequence
< double >();
451 virtual ::sal_Int32 SAL_CALL
getBitsPerPixel( ) override
453 return mnBitsPerPixel
;
456 virtual uno::Sequence
< ::sal_Int32
> SAL_CALL
getComponentBitCounts( ) override
458 return maComponentBitCounts
;
461 virtual ::sal_Int8 SAL_CALL
getEndianness( ) override
463 return util::Endianness::LITTLE
;
466 virtual uno::Sequence
< double > SAL_CALL
convertFromIntegerColorSpace( const uno::Sequence
< ::sal_Int8
>& ,
467 const uno::Reference
< rendering::XColorSpace
>& ) override
469 CPPUNIT_ASSERT_MESSAGE("convertFromIntegerColorSpace: method not implemented", false);
470 return uno::Sequence
< double >();
473 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
convertToIntegerColorSpace( const uno::Sequence
< ::sal_Int8
>& ,
474 const uno::Reference
< rendering::XIntegerBitmapColorSpace
>& ) override
476 CPPUNIT_ASSERT_MESSAGE("convertToIntegerColorSpace: method not implemented", false);
477 return uno::Sequence
< sal_Int8
>();
480 virtual uno::Sequence
< rendering::RGBColor
> SAL_CALL
convertIntegerToRGB( const uno::Sequence
< ::sal_Int8
>& deviceColor
) override
482 const uno::Sequence
< rendering::ARGBColor
> aTemp( convertIntegerToARGB(deviceColor
) );
483 uno::Sequence
< rendering::RGBColor
> aRes( aTemp
.getLength() );
484 std::transform(aTemp
.begin(), aTemp
.end(), aRes
.begin(),
485 [](const rendering::ARGBColor
& rColor
) {
486 return rendering::RGBColor(rColor
.Red
,
494 virtual uno::Sequence
< rendering::ARGBColor
> SAL_CALL
convertIntegerToARGB( const uno::Sequence
< ::sal_Int8
>& deviceColor
) override
496 const std::size_t nLen( deviceColor
.getLength() );
497 const sal_Int32
nBytesPerPixel(mnBitsPerPixel
== 8 ? 1 : 4);
498 CPPUNIT_ASSERT_EQUAL_MESSAGE("number of channels no multiple of pixel element count",
499 0, static_cast<int>(nLen
%nBytesPerPixel
));
501 uno::Sequence
< rendering::ARGBColor
> aRes( nLen
/ nBytesPerPixel
);
503 if( getPalette().is() )
505 std::transform(deviceColor
.begin(), deviceColor
.end(), aRes
.begin(),
507 auto fColor
= vcl::unotools::toDoubleColor(nIn
);
508 return rendering::ARGBColor(1.0, fColor
, fColor
, fColor
);
513 rendering::ARGBColor
* pOut( aRes
.getArray() );
514 for( std::size_t i
=0; i
<nLen
; i
+=4 )
516 *pOut
++ = rendering::ARGBColor(
517 vcl::unotools::toDoubleColor(deviceColor
[i
+3]),
518 vcl::unotools::toDoubleColor(deviceColor
[i
+0]),
519 vcl::unotools::toDoubleColor(deviceColor
[i
+1]),
520 vcl::unotools::toDoubleColor(deviceColor
[i
+2]));
527 virtual uno::Sequence
< rendering::ARGBColor
> SAL_CALL
convertIntegerToPARGB(
528 const uno::Sequence
< ::sal_Int8
>& deviceColor
) override
530 const std::size_t nLen( deviceColor
.getLength() );
531 const sal_Int32
nBytesPerPixel(mnBitsPerPixel
== 8 ? 1 : 4);
532 CPPUNIT_ASSERT_EQUAL_MESSAGE("number of channels no multiple of pixel element count",
533 0, static_cast<int>(nLen
%nBytesPerPixel
));
535 uno::Sequence
< rendering::ARGBColor
> aRes( nLen
/ nBytesPerPixel
);
537 if( getPalette().is() )
539 std::transform(deviceColor
.begin(), deviceColor
.end(), aRes
.begin(),
541 auto fColor
= vcl::unotools::toDoubleColor(nIn
);
542 return rendering::ARGBColor(1.0, fColor
, fColor
, fColor
);
547 rendering::ARGBColor
* pOut( aRes
.getArray() );
548 for( std::size_t i
=0; i
<nLen
; i
+=4 )
550 const double fAlpha
=vcl::unotools::toDoubleColor(deviceColor
[i
+3]);
551 *pOut
++ = rendering::ARGBColor(
553 fAlpha
*vcl::unotools::toDoubleColor(deviceColor
[i
+0]),
554 fAlpha
*vcl::unotools::toDoubleColor(deviceColor
[i
+1]),
555 fAlpha
*vcl::unotools::toDoubleColor(deviceColor
[i
+2]));
562 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
convertIntegerFromRGB(
563 const uno::Sequence
< rendering::RGBColor
>&) override
565 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromRGB: method not implemented", false);
566 return uno::Sequence
< sal_Int8
>();
569 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
convertIntegerFromARGB( const uno::Sequence
< rendering::ARGBColor
>& ) override
571 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromARGB: method not implemented", false);
572 return uno::Sequence
< sal_Int8
>();
575 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
convertIntegerFromPARGB( const uno::Sequence
< rendering::ARGBColor
>& ) override
577 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromPARGB: method not implemented", false);
578 return uno::Sequence
< sal_Int8
>();
582 TestBitmap( const geometry::IntegerSize2D
& rSize
, bool bPalette
) :
585 maComponentBitCounts(),
587 mnBitsPerPixel( bPalette
? 8 : 32 )
591 maComponentTags
.realloc(1);
592 maComponentTags
[0] = rendering::ColorComponentTag::INDEX
;
594 maComponentBitCounts
.realloc(1);
595 maComponentBitCounts
[0] = 8;
599 maComponentTags
.realloc(4);
600 sal_Int8
* pTags
= maComponentTags
.getArray();
601 pTags
[0] = rendering::ColorComponentTag::RGB_BLUE
;
602 pTags
[1] = rendering::ColorComponentTag::RGB_GREEN
;
603 pTags
[2] = rendering::ColorComponentTag::RGB_RED
;
604 pTags
[3] = rendering::ColorComponentTag::ALPHA
;
606 maComponentBitCounts
.realloc(4);
607 sal_Int32
* pCounts
= maComponentBitCounts
.getArray();
614 maLayout
.ScanLines
= 0;
615 maLayout
.ScanLineBytes
= 0;
616 maLayout
.ScanLineStride
= 0;
617 maLayout
.PlaneStride
= 0;
618 maLayout
.ColorSpace
.clear();
619 maLayout
.Palette
.clear();
620 maLayout
.IsMsbFirst
= false;
624 void CanvasBitmapTest::runTest()
626 static const sal_Int8 lcl_depths
[]={1,4,8,24};
628 // Testing VclCanvasBitmap wrapper
630 for( size_t i
=0; i
<SAL_N_ELEMENTS(lcl_depths
); ++i
)
632 const sal_Int8
nDepth( lcl_depths
[i
] );
633 Bitmap
aBitmap(Size(200,200),nDepth
);
634 aBitmap
.Erase(COL_WHITE
);
636 BitmapScopedWriteAccess
pAcc(aBitmap
);
639 BitmapColor
aBlack(0);
640 BitmapColor
aWhite(0);
641 if( pAcc
->HasPalette() )
643 aBlack
.SetIndex( sal::static_int_cast
<sal_Int8
>(pAcc
->GetBestPaletteIndex(BitmapColor(0,0,0))) );
644 aWhite
.SetIndex( sal::static_int_cast
<sal_Int8
>(pAcc
->GetBestPaletteIndex(BitmapColor(255,255,255))) );
651 pAcc
->SetFillColor(COL_GREEN
);
652 pAcc
->FillRect(tools::Rectangle(0,0,100,100));
653 pAcc
->SetPixel(0,0,aWhite
);
654 pAcc
->SetPixel(0,1,aBlack
);
655 pAcc
->SetPixel(0,2,aWhite
);
659 rtl::Reference
<VclCanvasBitmap
> xBmp( new VclCanvasBitmap(BitmapEx(aBitmap
)) );
661 checkCanvasBitmap( xBmp
, "single bitmap", nDepth
);
663 Bitmap
aMask(Size(200,200),1);
664 aMask
.Erase(COL_WHITE
);
666 BitmapScopedWriteAccess
pAcc(aMask
);
669 pAcc
->SetFillColor(COL_BLACK
);
670 pAcc
->FillRect(tools::Rectangle(0,0,100,100));
671 pAcc
->SetPixel(0,0,BitmapColor(1));
672 pAcc
->SetPixel(0,1,BitmapColor(0));
673 pAcc
->SetPixel(0,2,BitmapColor(1));
677 xBmp
.set( new VclCanvasBitmap(BitmapEx(aBitmap
,aMask
)) );
679 checkCanvasBitmap( xBmp
, "masked bitmap", nDepth
);
681 AlphaMask
aAlpha(Size(200,200));
684 BitmapWriteAccess
* pAcc
= aAlpha
.AcquireWriteAccess();
687 pAcc
->SetFillColor(COL_BLACK
);
688 pAcc
->FillRect(tools::Rectangle(0,0,100,100));
689 pAcc
->SetPixel(0,0,BitmapColor(255));
690 pAcc
->SetPixel(0,1,BitmapColor(0));
691 pAcc
->SetPixel(0,2,BitmapColor(255));
692 aAlpha
.ReleaseAccess(pAcc
);
696 xBmp
.set( new VclCanvasBitmap(BitmapEx(aBitmap
,aAlpha
)) );
698 checkCanvasBitmap( xBmp
, "alpha bitmap", nDepth
);
701 // Testing XBitmap import
703 uno::Reference
< rendering::XIntegerReadOnlyBitmap
> xTestBmp(
704 new TestBitmap( geometry::IntegerSize2D(10,10), true ));
706 BitmapEx aBmp
= vcl::unotools::bitmapExFromXBitmap(xTestBmp
);
707 CPPUNIT_ASSERT_MESSAGE( "Palette bitmap is transparent",
708 !aBmp
.IsTransparent());
709 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Bitmap does not have size (10,10)",
710 Size(10,10), aBmp
.GetSizePixel());
711 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Bitmap does not have bitcount of 8",
712 static_cast<sal_uInt16
>(8), aBmp
.GetBitCount());
714 Bitmap aBitmap
= aBmp
.GetBitmap();
715 BitmapReadAccess
* pBmpAcc
= aBitmap
.AcquireReadAccess();
717 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid BitmapReadAccess",
720 CPPUNIT_ASSERT_EQUAL_MESSAGE("(0,0) incorrect content",
721 BitmapColor(0), pBmpAcc
->GetPixel(0,0));
722 CPPUNIT_ASSERT_EQUAL_MESSAGE("(2,2) incorrect content",
723 BitmapColor(2), pBmpAcc
->GetPixel(2,2));
724 CPPUNIT_ASSERT_EQUAL_MESSAGE("(9,2) incorrect content",
725 BitmapColor(9), pBmpAcc
->GetPixel(2,9));
727 Bitmap::ReleaseAccess(pBmpAcc
);
730 xTestBmp
.set( new TestBitmap( geometry::IntegerSize2D(10,10), false ));
732 aBmp
= vcl::unotools::bitmapExFromXBitmap(xTestBmp
);
733 CPPUNIT_ASSERT_MESSAGE( "Palette bitmap is not transparent",
734 aBmp
.IsTransparent());
735 CPPUNIT_ASSERT_MESSAGE( "Palette bitmap has no alpha",
737 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Bitmap does not have size (10,10)",
738 Size(10,10), aBmp
.GetSizePixel());
739 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Bitmap has bitcount of 24",
740 static_cast<sal_uInt16
>(24), aBmp
.GetBitCount());
742 Bitmap aBitmap
= aBmp
.GetBitmap();
743 BitmapReadAccess
* pBmpAcc
= aBitmap
.AcquireReadAccess();
744 BitmapReadAccess
* pAlphaAcc
= aBmp
.GetAlpha().AcquireReadAccess();
746 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid BitmapReadAccess",
748 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid alpha BitmapReadAccess",
751 CPPUNIT_ASSERT_EQUAL_MESSAGE("(0,0) incorrect content",
752 BitmapColor(0,1,0), pBmpAcc
->GetPixel(0,0));
753 CPPUNIT_ASSERT_EQUAL_MESSAGE("(0,0) incorrect alpha content",
754 BitmapColor(255), pAlphaAcc
->GetPixel(0,0));
755 CPPUNIT_ASSERT_EQUAL_MESSAGE("(2,2) incorrect content",
756 BitmapColor(0,3,2), pBmpAcc
->GetPixel(2,2));
757 CPPUNIT_ASSERT_EQUAL_MESSAGE("(2,2) incorrect alpha content",
758 BitmapColor(253), pAlphaAcc
->GetPixel(2,2));
759 CPPUNIT_ASSERT_EQUAL_MESSAGE("(9,2) incorrect content",
760 BitmapColor(0,3,9), pBmpAcc
->GetPixel(2,9));
761 CPPUNIT_ASSERT_EQUAL_MESSAGE("(9,2) correct alpha content",
762 BitmapColor(253), pAlphaAcc
->GetPixel(2,9));
764 aBmp
.GetAlpha().ReleaseAccess(pAlphaAcc
);
765 Bitmap::ReleaseAccess(pBmpAcc
);
771 CPPUNIT_TEST_SUITE_REGISTRATION(CanvasBitmapTest
);
773 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */