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/bitmapex.hxx>
38 #include <canvasbitmap.hxx>
39 #include <vcl/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 BitmapScopedReadAccess
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_EQUAL_MESSAGE( "Original bitmap size via API not (200,200)",
100 sal_Int32(200), xBmp
->getSize().Width
);
101 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Original bitmap size via API not (200,200)",
102 sal_Int32(200), xBmp
->getSize().Height
);
104 CPPUNIT_ASSERT_EQUAL_MESSAGE( "alpha state mismatch",
105 aContainedBmpEx
.IsAlpha(), bool(xBmp
->hasAlpha()));
107 CPPUNIT_ASSERT_MESSAGE( "getScaledBitmap() failed",
108 xBmp
->getScaledBitmap( geometry::RealSize2D(500,500), false ).is());
110 rendering::IntegerBitmapLayout aLayout
;
111 uno::Sequence
<sal_Int8
> aPixelData
= xBmp
->getData(aLayout
, geometry::IntegerRectangle2D(0,0,1,1));
113 const sal_Int32
nExpectedBitsPerPixel(
114 (aContainedBmpEx
.IsAlpha() ? std::max(8,nDepth
)+8 : nDepth
) + extraBpp
);
115 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# scanlines not 1",
116 static_cast<sal_Int32
>(1), aLayout
.ScanLines
);
117 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# scanline bytes mismatch",
118 static_cast<sal_Int32
>((nExpectedBitsPerPixel
+7)/8), aLayout
.ScanLineBytes
);
119 CPPUNIT_ASSERT_MESSAGE( "# scanline stride mismatch",
120 aLayout
.ScanLineStride
== (nExpectedBitsPerPixel
+7)/8 ||
121 aLayout
.ScanLineStride
== -(nExpectedBitsPerPixel
+7)/8);
122 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# plane stride not 0",
123 static_cast<sal_Int32
>(0), aLayout
.PlaneStride
);
125 CPPUNIT_ASSERT_MESSAGE( "Color space not there",
126 aLayout
.ColorSpace
.is());
128 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Palette existence does not conform to bitmap",
129 (nDepth
<= 8), aLayout
.Palette
.is());
131 uno::Sequence
<sal_Int8
> aPixelData2
= xBmp
->getPixel( aLayout
, geometry::IntegerPoint2D(0,0) );
133 CPPUNIT_ASSERT_EQUAL_MESSAGE( "getData and getPixel did not return same amount of data",
134 aPixelData
.getLength(), aPixelData2
.getLength());
136 aPixelData
= xBmp
->getData(aLayout
, geometry::IntegerRectangle2D(0,0,200,1));
137 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# scanlines not 1 for getPixel",
138 static_cast<sal_Int32
>(1), aLayout
.ScanLines
);
139 CPPUNIT_ASSERT_EQUAL_MESSAGE( "# scanline bytes mismatch for getPixel",
140 static_cast<sal_Int32
>((200*nExpectedBitsPerPixel
+7)/8), aLayout
.ScanLineBytes
);
141 CPPUNIT_ASSERT_MESSAGE( "# scanline stride mismatch for getPixel",
142 aLayout
.ScanLineStride
== (200*nExpectedBitsPerPixel
+7)/8 ||
143 aLayout
.ScanLineStride
== -(200*nExpectedBitsPerPixel
+7)/8);
145 uno::Sequence
< rendering::RGBColor
> aRGBColors
= xBmp
->convertIntegerToRGB( aPixelData
);
146 uno::Sequence
< rendering::ARGBColor
> aARGBColors
= xBmp
->convertIntegerToARGB( aPixelData
);
148 const rendering::RGBColor
* pRGBStart ( aRGBColors
.getConstArray() );
149 const rendering::RGBColor
* pRGBEnd ( aRGBColors
.getConstArray()+aRGBColors
.getLength() );
150 const rendering::ARGBColor
* pARGBStart( aARGBColors
.getConstArray() );
151 std::pair
<const rendering::RGBColor
*,
152 const rendering::ARGBColor
*> aRes
= std::mismatch( pRGBStart
, pRGBEnd
, pARGBStart
);
153 CPPUNIT_ASSERT_EQUAL_MESSAGE( "argb and rgb colors are not equal",
154 pRGBEnd
, aRes
.first
);
156 CPPUNIT_ASSERT_MESSAGE( "rgb colors are not within [0,1] range",
157 std::none_of(pRGBStart
,pRGBEnd
,&rangeCheck
));
159 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
160 "First pixel is not white", 1.0, pRGBStart
[0].Red
, 1E-12);
161 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
162 "First pixel is not white", 1.0, pRGBStart
[0].Green
, 1E-12);
163 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
164 "First pixel is not white", 1.0, pRGBStart
[0].Blue
, 1E-12);
165 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
166 "Second pixel is not opaque", 1.0, pARGBStart
[1].Alpha
, 1E-12);
167 if( aContainedBmpEx
.IsAlpha() )
169 CPPUNIT_ASSERT_EQUAL_MESSAGE( "First pixel is not fully transparent",
170 0.0, pARGBStart
[0].Alpha
);
173 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Second pixel is not black",
174 0.0, pRGBStart
[1].Red
);
175 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Second pixel is not black",
176 0.0, pRGBStart
[1].Green
);
177 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Second pixel is not black",
178 0.0, pRGBStart
[1].Blue
);
180 if( nOriginalDepth
> 8 )
182 const Color
aCol(COL_GREEN
);
183 CPPUNIT_ASSERT_EQUAL_MESSAGE(
184 "Sixth pixel is not green (red component)",
185 vcl::unotools::toDoubleColor(aCol
.GetRed()), pRGBStart
[5].Red
);
186 CPPUNIT_ASSERT_EQUAL_MESSAGE(
187 "Sixth pixel is not green (green component)",
188 vcl::unotools::toDoubleColor(aCol
.GetGreen()), pRGBStart
[5].Green
);
189 CPPUNIT_ASSERT_EQUAL_MESSAGE(
190 "Sixth pixel is not green (blue component)",
191 vcl::unotools::toDoubleColor(aCol
.GetBlue()), pRGBStart
[5].Blue
);
193 else if( nDepth
<= 8 )
195 uno::Reference
<rendering::XBitmapPalette
> xPal
= xBmp
->getPalette();
196 CPPUNIT_ASSERT_MESSAGE( "8bit or less: missing palette",
198 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Palette incorrect entry count",
199 static_cast<sal_Int32
>(1 << nOriginalDepth
), xPal
->getNumberOfEntries());
200 uno::Sequence
<double> aIndex
;
201 CPPUNIT_ASSERT_MESSAGE( "Palette is not read-only",
202 !xPal
->setIndex(aIndex
,true,0));
203 CPPUNIT_ASSERT_MESSAGE( "Palette entry 0 is not opaque",
204 xPal
->getIndex(aIndex
,0));
205 CPPUNIT_ASSERT_MESSAGE( "Palette has no valid color space",
206 xPal
->getColorSpace().is());
209 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
210 "150th pixel is not white", 1.0, pRGBStart
[150].Red
, 1E-12);
211 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
212 "150th pixel is not white", 1.0, pRGBStart
[150].Green
, 1E-12);
213 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
214 "150th pixel is not white", 1.0, pRGBStart
[150].Blue
, 1E-12);
216 if( nOriginalDepth
<= 8 )
219 uno::Sequence
<sal_Int8
> aPixel3
, aPixel4
;
221 const Color
aCol(COL_GREEN
);
222 uno::Sequence
<rendering::ARGBColor
> aARGBColor
226 vcl::unotools::toDoubleColor(aCol
.GetRed()),
227 vcl::unotools::toDoubleColor(aCol
.GetGreen()),
228 vcl::unotools::toDoubleColor(aCol
.GetBlue())
232 uno::Sequence
<rendering::RGBColor
> aRGBColor
235 vcl::unotools::toDoubleColor(aCol
.GetRed()),
236 vcl::unotools::toDoubleColor(aCol
.GetGreen()),
237 vcl::unotools::toDoubleColor(aCol
.GetBlue())
241 aPixel3
= xBmp
->convertIntegerFromARGB( aARGBColor
);
242 aPixel4
= xBmp
->getPixel( aLayout
, geometry::IntegerPoint2D(5,0) );
243 CPPUNIT_ASSERT_MESSAGE( "Green pixel from bitmap mismatch with manually converted green pixel",
244 bool(aPixel3
== aPixel4
));
246 if( !aContainedBmpEx
.IsAlpha() )
248 aPixel3
= xBmp
->convertIntegerFromRGB( aRGBColor
);
249 CPPUNIT_ASSERT_MESSAGE( "Green pixel from bitmap mismatch with manually RGB-converted green pixel",
250 bool(aPixel3
== aPixel4
));
255 class TestBitmap
: public cppu::WeakImplHelper
< rendering::XIntegerReadOnlyBitmap
,
256 rendering::XBitmapPalette
,
257 rendering::XIntegerBitmapColorSpace
>
260 geometry::IntegerSize2D maSize
;
261 uno::Sequence
<sal_Int8
> maComponentTags
;
262 uno::Sequence
<sal_Int32
> maComponentBitCounts
;
263 rendering::IntegerBitmapLayout maLayout
;
264 const sal_Int32 mnBitsPerPixel
;
267 virtual geometry::IntegerSize2D SAL_CALL
getSize() override
{ return maSize
; }
268 virtual sal_Bool SAL_CALL
hasAlpha( ) override
{ return mnBitsPerPixel
!= 8; }
269 virtual uno::Reference
< rendering::XBitmap
> SAL_CALL
getScaledBitmap( const geometry::RealSize2D
&,
270 sal_Bool
) override
{ return this; }
272 // XIntegerReadOnlyBitmap
273 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getData( rendering::IntegerBitmapLayout
& bitmapLayout
,
274 const geometry::IntegerRectangle2D
& rect
) override
276 CPPUNIT_ASSERT_MESSAGE( "X1 out of bounds", rect
.X1
>= 0 );
277 CPPUNIT_ASSERT_MESSAGE( "Y1 out of bounds", rect
.Y1
>= 0 );
278 CPPUNIT_ASSERT_MESSAGE( "X2 out of bounds", rect
.X2
<= maSize
.Width
);
279 CPPUNIT_ASSERT_MESSAGE( "Y2 out of bounds", rect
.Y2
<= maSize
.Height
);
281 bitmapLayout
= getMemoryLayout();
283 const sal_Int32 nWidth
= rect
.X2
-rect
.X1
;
284 const sal_Int32 nHeight
= rect
.Y2
-rect
.Y1
;
285 const sal_Int32 nScanlineLen
= (nWidth
* mnBitsPerPixel
+ 7)/8;
286 uno::Sequence
<sal_Int8
> aRes( nScanlineLen
* nHeight
);
287 sal_Int8
* pOut
= aRes
.getArray();
289 bitmapLayout
.ScanLines
= nHeight
;
290 bitmapLayout
.ScanLineBytes
=
291 bitmapLayout
.ScanLineStride
= nScanlineLen
;
293 if( mnBitsPerPixel
== 8 )
295 for( sal_Int32 y
=0; y
<nHeight
; ++y
)
297 for( sal_Int32 x
=0; x
<nWidth
; ++x
)
298 pOut
[ y
*nScanlineLen
+ x
] = sal_Int8(x
);
303 for( sal_Int32 y
=0; y
<nHeight
; ++y
)
305 for( sal_Int32 x
=0; x
<nWidth
; ++x
)
307 pOut
[ y
*nScanlineLen
+ 4*x
] = sal_Int8(rect
.X1
);
308 pOut
[ y
*nScanlineLen
+ 4*x
+ 1 ] = sal_Int8(rect
.Y2
);
309 pOut
[ y
*nScanlineLen
+ 4*x
+ 2 ] = sal_Int8(x
);
310 pOut
[ y
*nScanlineLen
+ 4*x
+ 3 ] = sal_Int8(rect
.Y1
);
318 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getPixel( rendering::IntegerBitmapLayout
&,
319 const geometry::IntegerPoint2D
& ) override
321 CPPUNIT_ASSERT_MESSAGE("getPixel: method not implemented", false);
322 return uno::Sequence
< sal_Int8
>();
325 /// @throws uno::RuntimeException
326 uno::Reference
< rendering::XBitmapPalette
> getPalette( )
328 uno::Reference
< XBitmapPalette
> aRet
;
329 if( mnBitsPerPixel
== 8 )
334 virtual rendering::IntegerBitmapLayout SAL_CALL
getMemoryLayout( ) override
336 rendering::IntegerBitmapLayout
aLayout( maLayout
);
338 const sal_Int32 nScanlineLen
= (maSize
.Width
* mnBitsPerPixel
+ 7)/8;
340 aLayout
.ScanLines
= maSize
.Height
;
341 aLayout
.ScanLineBytes
=
342 aLayout
.ScanLineStride
= nScanlineLen
;
343 aLayout
.Palette
= getPalette();
344 aLayout
.ColorSpace
.set( this );
350 virtual sal_Int32 SAL_CALL
getNumberOfEntries() override
352 CPPUNIT_ASSERT_MESSAGE( "Got palette getNumberOfEntries interface call without handing out palette",
358 virtual sal_Bool SAL_CALL
getIndex( uno::Sequence
< double >& entry
,
359 ::sal_Int32 nIndex
) override
361 CPPUNIT_ASSERT_MESSAGE( "Got palette getIndex interface call without handing out palette",
363 CPPUNIT_ASSERT_MESSAGE( "getIndex: index out of range",
365 CPPUNIT_ASSERT_MESSAGE( "getIndex: index out of range",
367 entry
= colorToStdColorSpaceSequence(
368 Color(sal_uInt8(nIndex
),
370 sal_uInt8(nIndex
)) );
372 return true; // no palette transparency here.
375 virtual sal_Bool SAL_CALL
setIndex( const uno::Sequence
< double >&,
377 ::sal_Int32 nIndex
) override
379 CPPUNIT_ASSERT_MESSAGE( "Got palette setIndex interface call without handing out palette",
381 CPPUNIT_ASSERT_MESSAGE( "setIndex: index out of range",
383 CPPUNIT_ASSERT_MESSAGE( "setIndex: index out of range",
388 virtual uno::Reference
< rendering::XColorSpace
> SAL_CALL
getColorSpace( ) override
390 // this is the method from XBitmapPalette. Return palette color
392 static uno::Reference
<rendering::XColorSpace
> aColorSpace
=
393 vcl::unotools::createStandardColorSpace();
397 // XIntegerBitmapColorSpace
398 virtual ::sal_Int8 SAL_CALL
getType( ) override
400 return rendering::ColorSpaceType::RGB
;
403 virtual uno::Sequence
< sal_Int8
> SAL_CALL
getComponentTags( ) override
405 return maComponentTags
;
408 virtual ::sal_Int8 SAL_CALL
getRenderingIntent( ) override
410 return rendering::RenderingIntent::PERCEPTUAL
;
413 virtual uno::Sequence
< beans::PropertyValue
> SAL_CALL
getProperties() override
415 CPPUNIT_ASSERT_MESSAGE("getProperties: method not implemented", false );
416 return uno::Sequence
< ::beans::PropertyValue
>();
419 virtual uno::Sequence
< double > SAL_CALL
convertColorSpace( const uno::Sequence
< double >&,
420 const uno::Reference
< rendering::XColorSpace
>& ) 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 >& ) override
428 CPPUNIT_ASSERT_MESSAGE("convertToRGB: method not implemented", false);
429 return uno::Sequence
< rendering::RGBColor
>();
432 virtual uno::Sequence
< rendering::ARGBColor
> SAL_CALL
convertToARGB( const uno::Sequence
< double >& ) override
434 CPPUNIT_ASSERT_MESSAGE("convertToARGB: method not implemented", false);
435 return uno::Sequence
< rendering::ARGBColor
>();
438 virtual uno::Sequence
< rendering::ARGBColor
> SAL_CALL
convertToPARGB( const uno::Sequence
< double >& ) override
440 CPPUNIT_ASSERT_MESSAGE("convertToPARGB: method not implemented", false);
441 return uno::Sequence
< rendering::ARGBColor
>();
444 virtual uno::Sequence
< double > SAL_CALL
convertFromRGB( const uno::Sequence
< rendering::RGBColor
>& ) override
446 CPPUNIT_ASSERT_MESSAGE("convertFromRGB: method not implemented", false);
447 return uno::Sequence
< double >();
450 virtual uno::Sequence
< double > SAL_CALL
convertFromARGB( const uno::Sequence
< rendering::ARGBColor
>& ) override
452 CPPUNIT_ASSERT_MESSAGE("convertFromARGB: this method is not expected to be called!", false);
453 return uno::Sequence
< double >();
456 virtual uno::Sequence
< double > SAL_CALL
convertFromPARGB( const uno::Sequence
< rendering::ARGBColor
>& ) override
458 CPPUNIT_ASSERT_MESSAGE("convertFromPARGB: this method is not expected to be called!", false);
459 return uno::Sequence
< double >();
462 virtual ::sal_Int32 SAL_CALL
getBitsPerPixel( ) override
464 return mnBitsPerPixel
;
467 virtual uno::Sequence
< ::sal_Int32
> SAL_CALL
getComponentBitCounts( ) override
469 return maComponentBitCounts
;
472 virtual ::sal_Int8 SAL_CALL
getEndianness( ) override
474 return util::Endianness::LITTLE
;
477 virtual uno::Sequence
< double > SAL_CALL
convertFromIntegerColorSpace( const uno::Sequence
< ::sal_Int8
>& ,
478 const uno::Reference
< rendering::XColorSpace
>& ) override
480 CPPUNIT_ASSERT_MESSAGE("convertFromIntegerColorSpace: method not implemented", false);
481 return uno::Sequence
< double >();
484 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
convertToIntegerColorSpace( const uno::Sequence
< ::sal_Int8
>& ,
485 const uno::Reference
< rendering::XIntegerBitmapColorSpace
>& ) override
487 CPPUNIT_ASSERT_MESSAGE("convertToIntegerColorSpace: method not implemented", false);
488 return uno::Sequence
< sal_Int8
>();
491 virtual uno::Sequence
< rendering::RGBColor
> SAL_CALL
convertIntegerToRGB( const uno::Sequence
< ::sal_Int8
>& deviceColor
) override
493 const uno::Sequence
< rendering::ARGBColor
> aTemp( convertIntegerToARGB(deviceColor
) );
494 uno::Sequence
< rendering::RGBColor
> aRes( aTemp
.getLength() );
495 std::transform(aTemp
.begin(), aTemp
.end(), aRes
.getArray(),
496 [](const rendering::ARGBColor
& rColor
) {
497 return rendering::RGBColor(rColor
.Red
,
505 virtual uno::Sequence
< rendering::ARGBColor
> SAL_CALL
convertIntegerToARGB( const uno::Sequence
< ::sal_Int8
>& deviceColor
) override
507 const std::size_t nLen( deviceColor
.getLength() );
508 const sal_Int32
nBytesPerPixel(mnBitsPerPixel
== 8 ? 1 : 4);
509 CPPUNIT_ASSERT_EQUAL_MESSAGE("number of channels no multiple of pixel element count",
510 0, static_cast<int>(nLen
%nBytesPerPixel
));
512 uno::Sequence
< rendering::ARGBColor
> aRes( nLen
/ nBytesPerPixel
);
514 if( getPalette().is() )
516 std::transform(deviceColor
.begin(), deviceColor
.end(), aRes
.getArray(),
518 auto fColor
= vcl::unotools::toDoubleColor(nIn
);
519 return rendering::ARGBColor(1.0, fColor
, fColor
, fColor
);
524 rendering::ARGBColor
* pOut( aRes
.getArray() );
525 for( std::size_t i
=0; i
<nLen
; i
+=4 )
527 *pOut
++ = rendering::ARGBColor(
528 vcl::unotools::toDoubleColor(deviceColor
[i
+3]),
529 vcl::unotools::toDoubleColor(deviceColor
[i
+0]),
530 vcl::unotools::toDoubleColor(deviceColor
[i
+1]),
531 vcl::unotools::toDoubleColor(deviceColor
[i
+2]));
538 virtual uno::Sequence
< rendering::ARGBColor
> SAL_CALL
convertIntegerToPARGB(
539 const uno::Sequence
< ::sal_Int8
>& deviceColor
) override
541 const std::size_t nLen( deviceColor
.getLength() );
542 const sal_Int32
nBytesPerPixel(mnBitsPerPixel
== 8 ? 1 : 4);
543 CPPUNIT_ASSERT_EQUAL_MESSAGE("number of channels no multiple of pixel element count",
544 0, static_cast<int>(nLen
%nBytesPerPixel
));
546 uno::Sequence
< rendering::ARGBColor
> aRes( nLen
/ nBytesPerPixel
);
548 if( getPalette().is() )
550 std::transform(deviceColor
.begin(), deviceColor
.end(), aRes
.getArray(),
552 auto fColor
= vcl::unotools::toDoubleColor(nIn
);
553 return rendering::ARGBColor(1.0, fColor
, fColor
, fColor
);
558 rendering::ARGBColor
* pOut( aRes
.getArray() );
559 for( std::size_t i
=0; i
<nLen
; i
+=4 )
561 const double fAlpha
=vcl::unotools::toDoubleColor(deviceColor
[i
+3]);
562 *pOut
++ = rendering::ARGBColor(
564 fAlpha
*vcl::unotools::toDoubleColor(deviceColor
[i
+0]),
565 fAlpha
*vcl::unotools::toDoubleColor(deviceColor
[i
+1]),
566 fAlpha
*vcl::unotools::toDoubleColor(deviceColor
[i
+2]));
573 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
convertIntegerFromRGB(
574 const uno::Sequence
< rendering::RGBColor
>&) override
576 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromRGB: method not implemented", false);
577 return uno::Sequence
< sal_Int8
>();
580 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
convertIntegerFromARGB( const uno::Sequence
< rendering::ARGBColor
>& ) override
582 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromARGB: method not implemented", false);
583 return uno::Sequence
< sal_Int8
>();
586 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
convertIntegerFromPARGB( const uno::Sequence
< rendering::ARGBColor
>& ) override
588 CPPUNIT_ASSERT_MESSAGE("convertIntegerFromPARGB: method not implemented", false);
589 return uno::Sequence
< sal_Int8
>();
593 TestBitmap( const geometry::IntegerSize2D
& rSize
, bool bPalette
) :
596 maComponentBitCounts(),
598 mnBitsPerPixel( bPalette
? 8 : 32 )
602 maComponentTags
= { rendering::ColorComponentTag::INDEX
};
603 maComponentBitCounts
= { 8 };
607 maComponentTags
.realloc(4);
608 sal_Int8
* pTags
= maComponentTags
.getArray();
609 pTags
[0] = rendering::ColorComponentTag::RGB_BLUE
;
610 pTags
[1] = rendering::ColorComponentTag::RGB_GREEN
;
611 pTags
[2] = rendering::ColorComponentTag::RGB_RED
;
612 pTags
[3] = rendering::ColorComponentTag::ALPHA
;
614 maComponentBitCounts
.realloc(4);
615 sal_Int32
* pCounts
= maComponentBitCounts
.getArray();
622 maLayout
.ScanLines
= 0;
623 maLayout
.ScanLineBytes
= 0;
624 maLayout
.ScanLineStride
= 0;
625 maLayout
.PlaneStride
= 0;
626 maLayout
.ColorSpace
.clear();
627 maLayout
.Palette
.clear();
628 maLayout
.IsMsbFirst
= false;
632 void CanvasBitmapTest::runTest()
634 static vcl::PixelFormat ePixelFormatArray
[] =
636 vcl::PixelFormat::N8_BPP
,
637 vcl::PixelFormat::N24_BPP
640 // Testing VclCanvasBitmap wrapper
642 for (auto const pixelFormat
: ePixelFormatArray
)
644 const sal_uInt16 nDepth
= sal_uInt16(pixelFormat
);
645 Bitmap
aBitmap(Size(200,200), pixelFormat
);
646 aBitmap
.Erase(COL_WHITE
);
648 BitmapScopedWriteAccess
pAcc(aBitmap
);
651 BitmapColor
aBlack(0);
652 BitmapColor
aWhite(0);
653 if( pAcc
->HasPalette() )
655 aBlack
.SetIndex( sal::static_int_cast
<sal_Int8
>(pAcc
->GetBestPaletteIndex(BitmapColor(0,0,0))) );
656 aWhite
.SetIndex( sal::static_int_cast
<sal_Int8
>(pAcc
->GetBestPaletteIndex(BitmapColor(255,255,255))) );
663 pAcc
->SetFillColor(COL_GREEN
);
664 pAcc
->FillRect(tools::Rectangle(0,0,100,100));
665 pAcc
->SetPixel(0,0,aWhite
);
666 pAcc
->SetPixel(0,1,aBlack
);
667 pAcc
->SetPixel(0,2,aWhite
);
671 rtl::Reference
<VclCanvasBitmap
> xBmp( new VclCanvasBitmap(BitmapEx(aBitmap
)) );
673 checkCanvasBitmap( xBmp
, "single bitmap", nDepth
);
675 AlphaMask
aMask(Size(200,200));
678 BitmapScopedWriteAccess
pAcc(aMask
);
681 pAcc
->SetFillColor(COL_ALPHA_OPAQUE
);
682 pAcc
->FillRect(tools::Rectangle(0,0,100,100));
683 pAcc
->SetPixel(0,0,BitmapColor(0));
684 pAcc
->SetPixel(0,1,BitmapColor(255));
685 pAcc
->SetPixel(0,2,BitmapColor(0));
689 xBmp
.set( new VclCanvasBitmap(BitmapEx(aBitmap
,aMask
)) );
691 checkCanvasBitmap( xBmp
, "masked bitmap", nDepth
);
693 AlphaMask
aAlpha(Size(200,200));
696 BitmapScopedWriteAccess
pAcc(aAlpha
);
699 pAcc
->SetFillColor(COL_ALPHA_OPAQUE
);
700 pAcc
->FillRect(tools::Rectangle(0,0,100,100));
701 pAcc
->SetPixel(0,0,BitmapColor(0));
702 pAcc
->SetPixel(0,1,BitmapColor(255));
703 pAcc
->SetPixel(0,2,BitmapColor(0));
707 xBmp
.set( new VclCanvasBitmap(BitmapEx(aBitmap
,aAlpha
)) );
709 checkCanvasBitmap( xBmp
, "alpha bitmap", nDepth
);
712 // Testing XBitmap import
714 uno::Reference
< rendering::XIntegerReadOnlyBitmap
> xTestBmp(
715 new TestBitmap( geometry::IntegerSize2D(10,10), true ));
717 BitmapEx aBmp
= vcl::unotools::bitmapExFromXBitmap(xTestBmp
);
718 CPPUNIT_ASSERT_MESSAGE( "Palette bitmap is alpha",
720 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Bitmap does not have size (10,10)",
721 Size(10,10), aBmp
.GetSizePixel());
722 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Bitmap does not have the expected pixel format",
723 vcl::PixelFormat::N8_BPP
, aBmp
.getPixelFormat());
725 Bitmap aBitmap
= aBmp
.GetBitmap();
726 BitmapScopedReadAccess
pBmpAcc(aBitmap
);
728 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid BitmapReadAccess",
731 CPPUNIT_ASSERT_EQUAL_MESSAGE("(0,0) incorrect content",
732 BitmapColor(0), pBmpAcc
->GetPixel(0,0));
733 CPPUNIT_ASSERT_EQUAL_MESSAGE("(2,2) incorrect content",
734 BitmapColor(2), pBmpAcc
->GetPixel(2,2));
735 CPPUNIT_ASSERT_EQUAL_MESSAGE("(9,2) incorrect content",
736 BitmapColor(9), pBmpAcc
->GetPixel(2,9));
739 xTestBmp
.set( new TestBitmap( geometry::IntegerSize2D(10,10), false ));
741 aBmp
= vcl::unotools::bitmapExFromXBitmap(xTestBmp
);
742 CPPUNIT_ASSERT_MESSAGE( "Palette bitmap has no alpha",
744 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Bitmap does not have size (10,10)",
745 Size(10,10), aBmp
.GetSizePixel());
746 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Bitmap does not have the expected pixel format",
747 vcl::PixelFormat::N24_BPP
, aBmp
.getPixelFormat());
749 Bitmap aBitmap
= aBmp
.GetBitmap();
750 BitmapScopedReadAccess
pBmpAcc(aBitmap
);
751 AlphaMask aBitmapAlpha
= aBmp
.GetAlphaMask();
752 BitmapScopedReadAccess
pAlphaAcc(aBitmapAlpha
);
754 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid BitmapReadAccess",
756 CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid alpha BitmapReadAccess",
759 CPPUNIT_ASSERT_EQUAL_MESSAGE("(0,0) incorrect content",
760 BitmapColor(0,1,0), pBmpAcc
->GetPixel(0,0));
761 CPPUNIT_ASSERT_EQUAL_MESSAGE("(0,0) incorrect alpha content",
762 BitmapColor(0), pAlphaAcc
->GetPixel(0,0));
763 CPPUNIT_ASSERT_EQUAL_MESSAGE("(2,2) incorrect content",
764 BitmapColor(0,3,2), pBmpAcc
->GetPixel(2,2));
765 CPPUNIT_ASSERT_EQUAL_MESSAGE("(2,2) incorrect alpha content",
766 BitmapColor(2), pAlphaAcc
->GetPixel(2,2));
767 CPPUNIT_ASSERT_EQUAL_MESSAGE("(9,2) incorrect content",
768 BitmapColor(0,3,9), pBmpAcc
->GetPixel(2,9));
769 CPPUNIT_ASSERT_EQUAL_MESSAGE("(9,2) correct alpha content",
770 BitmapColor(2), pAlphaAcc
->GetPixel(2,9));
776 CPPUNIT_TEST_SUITE_REGISTRATION(CanvasBitmapTest
);
778 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */