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 : // bootstrap stuff
21 : #include <test/bootstrapfixture.hxx>
22 :
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>
30 :
31 : #include <cppuhelper/compbase3.hxx>
32 : #include <tools/diagnose_ex.h>
33 : #include <rtl/ref.hxx>
34 :
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"
42 :
43 : #include "canvasbitmap.hxx"
44 :
45 : using namespace ::com::sun::star;
46 : using namespace ::vcl::unotools;
47 :
48 : namespace com { namespace sun { namespace star { namespace rendering
49 : {
50 :
51 3000 : bool operator==( const RGBColor& rLHS, const ARGBColor& rRHS )
52 : {
53 3000 : return rLHS.Red == rRHS.Red && rLHS.Green == rRHS.Green && rLHS.Blue == rRHS.Blue;
54 : }
55 :
56 : } } } }
57 :
58 : namespace
59 : {
60 :
61 2 : class CanvasBitmapTest : public test::BootstrapFixture
62 : {
63 : public:
64 1 : CanvasBitmapTest() : BootstrapFixture(true, false) {}
65 :
66 : void runTest();
67 :
68 2 : CPPUNIT_TEST_SUITE(CanvasBitmapTest);
69 1 : CPPUNIT_TEST(runTest);
70 2 : CPPUNIT_TEST_SUITE_END();
71 : };
72 :
73 3000 : bool rangeCheck( const rendering::RGBColor& rColor )
74 : {
75 9000 : return rColor.Red < 0.0 || rColor.Red > 1.0 ||
76 9000 : rColor.Green < 0.0 || rColor.Green > 1.0 ||
77 9000 : rColor.Blue < 0.0 || rColor.Blue > 1.0;
78 : }
79 :
80 15 : void checkCanvasBitmap( const rtl::Reference<VclCanvasBitmap>& xBmp,
81 : const char* msg,
82 : int nOriginalDepth )
83 : {
84 : OSL_TRACE("-------------------------");
85 : OSL_TRACE("Testing %s, with depth %d", msg, nOriginalDepth);
86 :
87 15 : BitmapEx aContainedBmpEx( xBmp->getBitmapEx() );
88 30 : Bitmap aContainedBmp( aContainedBmpEx.GetBitmap() );
89 15 : int nDepth = nOriginalDepth;
90 :
91 : {
92 15 : Bitmap::ScopedReadAccess pAcc( aContainedBmp );
93 15 : nDepth = pAcc->GetBitCount();
94 : }
95 :
96 30 : CPPUNIT_ASSERT_MESSAGE( "Original bitmap size not (200,200)",
97 15 : aContainedBmp.GetSizePixel() == Size(200,200));
98 :
99 30 : CPPUNIT_ASSERT_MESSAGE( "Original bitmap size via API not (200,200)",
100 15 : xBmp->getSize().Width == 200 && xBmp->getSize().Height == 200);
101 :
102 30 : CPPUNIT_ASSERT_MESSAGE( "alpha state mismatch",
103 15 : bool(xBmp->hasAlpha()) == aContainedBmpEx.IsTransparent());
104 :
105 30 : CPPUNIT_ASSERT_MESSAGE( "getScaledBitmap() failed",
106 15 : xBmp->getScaledBitmap( geometry::RealSize2D(500,500), false ).is());
107 :
108 30 : rendering::IntegerBitmapLayout aLayout;
109 30 : uno::Sequence<sal_Int8> aPixelData = xBmp->getData(aLayout, geometry::IntegerRectangle2D(0,0,1,1));
110 :
111 : const sal_Int32 nExpectedBitsPerPixel(
112 15 : aContainedBmpEx.IsTransparent() ? std::max(8,nDepth)+8 : nDepth);
113 30 : CPPUNIT_ASSERT_MESSAGE( "# scanlines not 1",
114 15 : aLayout.ScanLines == 1);
115 30 : CPPUNIT_ASSERT_MESSAGE( "# scanline bytes mismatch",
116 15 : aLayout.ScanLineBytes == (nExpectedBitsPerPixel+7)/8);
117 30 : CPPUNIT_ASSERT_MESSAGE( "# scanline stride mismatch",
118 : aLayout.ScanLineStride == (nExpectedBitsPerPixel+7)/8 ||
119 15 : aLayout.ScanLineStride == -(nExpectedBitsPerPixel+7)/8);
120 30 : CPPUNIT_ASSERT_MESSAGE( "# plane stride not 0",
121 15 : aLayout.PlaneStride == 0);
122 :
123 30 : CPPUNIT_ASSERT_MESSAGE( "Color space not there",
124 15 : aLayout.ColorSpace.is());
125 :
126 30 : CPPUNIT_ASSERT_MESSAGE( "Palette existance does not conform to bitmap",
127 15 : aLayout.Palette.is() == (nDepth <= 8));
128 :
129 30 : uno::Sequence<sal_Int8> aPixelData2 = xBmp->getPixel( aLayout, geometry::IntegerPoint2D(0,0) );
130 :
131 30 : CPPUNIT_ASSERT_MESSAGE( "getData and getPixel did not return same amount of data",
132 15 : aPixelData2.getLength() == aPixelData.getLength());
133 :
134 15 : aPixelData = xBmp->getData(aLayout, geometry::IntegerRectangle2D(0,0,200,1));
135 30 : CPPUNIT_ASSERT_MESSAGE( "# scanlines not 1 for getPixel",
136 15 : aLayout.ScanLines == 1);
137 30 : CPPUNIT_ASSERT_MESSAGE( "# scanline bytes mismatch for getPixel",
138 15 : aLayout.ScanLineBytes == (200*nExpectedBitsPerPixel+7)/8);
139 30 : CPPUNIT_ASSERT_MESSAGE( "# scanline stride mismatch for getPixel",
140 : aLayout.ScanLineStride == (200*nExpectedBitsPerPixel+7)/8 ||
141 15 : aLayout.ScanLineStride == -(200*nExpectedBitsPerPixel+7)/8);
142 :
143 30 : uno::Sequence< rendering::RGBColor > aRGBColors = xBmp->convertIntegerToRGB( aPixelData );
144 30 : uno::Sequence< rendering::ARGBColor > aARGBColors = xBmp->convertIntegerToARGB( aPixelData );
145 :
146 15 : const rendering::RGBColor* pRGBStart ( aRGBColors.getConstArray() );
147 15 : const rendering::RGBColor* pRGBEnd ( aRGBColors.getConstArray()+aRGBColors.getLength() );
148 15 : const rendering::ARGBColor* pARGBStart( aARGBColors.getConstArray() );
149 : std::pair<const rendering::RGBColor*,
150 15 : const rendering::ARGBColor*> aRes = std::mismatch( pRGBStart, pRGBEnd, pARGBStart );
151 30 : CPPUNIT_ASSERT_MESSAGE( "argb and rgb colors are not equal",
152 15 : aRes.first == pRGBEnd);
153 :
154 30 : CPPUNIT_ASSERT_MESSAGE( "rgb colors are not within [0,1] range",
155 15 : std::find_if(pRGBStart,pRGBEnd,&rangeCheck) == pRGBEnd);
156 :
157 30 : CPPUNIT_ASSERT_MESSAGE( "First pixel is not white",
158 15 : pRGBStart[0].Red == 1.0 && pRGBStart[0].Green == 1.0 && pRGBStart[0].Blue == 1.0);
159 30 : CPPUNIT_ASSERT_MESSAGE( "Second pixel is not opaque",
160 15 : pARGBStart[1].Alpha == 1.0);
161 15 : if( aContainedBmpEx.IsTransparent() )
162 : {
163 20 : CPPUNIT_ASSERT_MESSAGE( "First pixel is not fully transparent",
164 10 : pARGBStart[0].Alpha == 0.0);
165 : }
166 :
167 30 : CPPUNIT_ASSERT_MESSAGE( "Second pixel is not black",
168 15 : pRGBStart[1].Red == 0.0 && pRGBStart[1].Green == 0.0 && pRGBStart[1].Blue == 0.0);
169 :
170 15 : if( nOriginalDepth > 8 )
171 : {
172 6 : const Color aCol(COL_GREEN);
173 12 : CPPUNIT_ASSERT_EQUAL_MESSAGE(
174 : "Sixth pixel is not green (red component)",
175 6 : vcl::unotools::toDoubleColor(aCol.GetRed()), pRGBStart[5].Red);
176 12 : CPPUNIT_ASSERT_EQUAL_MESSAGE(
177 : "Sixth pixel is not green (green component)",
178 6 : vcl::unotools::toDoubleColor(aCol.GetGreen()), pRGBStart[5].Green);
179 12 : CPPUNIT_ASSERT_EQUAL_MESSAGE(
180 : "Sixth pixel is not green (blue component)",
181 6 : vcl::unotools::toDoubleColor(aCol.GetBlue()), pRGBStart[5].Blue);
182 : }
183 9 : else if( nDepth <= 8 )
184 : {
185 7 : uno::Reference<rendering::XBitmapPalette> xPal = xBmp->getPalette();
186 14 : CPPUNIT_ASSERT_MESSAGE( "8bit or less: missing palette",
187 7 : xPal.is());
188 14 : CPPUNIT_ASSERT_MESSAGE( "Palette incorrect entry count",
189 7 : xPal->getNumberOfEntries() == 1L << nOriginalDepth);
190 14 : uno::Sequence<double> aIndex;
191 14 : CPPUNIT_ASSERT_MESSAGE( "Palette is not read-only",
192 7 : !xPal->setIndex(aIndex,true,0));
193 14 : CPPUNIT_ASSERT_MESSAGE( "Palette entry 0 is not opaque",
194 7 : xPal->getIndex(aIndex,0));
195 14 : CPPUNIT_ASSERT_MESSAGE( "Palette has no valid color space",
196 14 : xPal->getColorSpace().is());
197 : }
198 :
199 30 : CPPUNIT_ASSERT_MESSAGE( "150th pixel is not white",
200 : pRGBStart[150].Red == 1.0 &&
201 : pRGBStart[150].Green == 1.0 &&
202 15 : pRGBStart[150].Blue == 1.0);
203 :
204 15 : if( nOriginalDepth > 8 )
205 : {
206 6 : uno::Sequence<rendering::ARGBColor> aARGBColor(1);
207 12 : uno::Sequence<rendering::RGBColor> aRGBColor(1);
208 12 : uno::Sequence<sal_Int8> aPixel3, aPixel4;
209 :
210 6 : const Color aCol(COL_GREEN);
211 6 : aARGBColor[0].Red = vcl::unotools::toDoubleColor(aCol.GetRed());
212 6 : aARGBColor[0].Green = vcl::unotools::toDoubleColor(aCol.GetGreen());
213 6 : aARGBColor[0].Blue = vcl::unotools::toDoubleColor(aCol.GetBlue());
214 6 : aARGBColor[0].Alpha = 1.0;
215 :
216 6 : aRGBColor[0].Red = vcl::unotools::toDoubleColor(aCol.GetRed());
217 6 : aRGBColor[0].Green = vcl::unotools::toDoubleColor(aCol.GetGreen());
218 6 : aRGBColor[0].Blue = vcl::unotools::toDoubleColor(aCol.GetBlue());
219 :
220 6 : aPixel3 = xBmp->convertIntegerFromARGB( aARGBColor );
221 6 : aPixel4 = xBmp->getPixel( aLayout, geometry::IntegerPoint2D(5,0) );
222 12 : CPPUNIT_ASSERT_MESSAGE( "Green pixel from bitmap mismatch with manually converted green pixel",
223 6 : aPixel3 == aPixel4);
224 :
225 6 : if( !aContainedBmpEx.IsTransparent() )
226 : {
227 2 : aPixel3 = xBmp->convertIntegerFromRGB( aRGBColor );
228 4 : CPPUNIT_ASSERT_MESSAGE( "Green pixel from bitmap mismatch with manually RGB-converted green pixel",
229 2 : aPixel3 == aPixel4);
230 6 : }
231 15 : }
232 15 : }
233 :
234 4 : class TestBitmap : public cppu::WeakImplHelper3< rendering::XIntegerReadOnlyBitmap,
235 : rendering::XBitmapPalette,
236 : rendering::XIntegerBitmapColorSpace >
237 : {
238 : private:
239 : geometry::IntegerSize2D maSize;
240 : uno::Sequence<sal_Int8> maComponentTags;
241 : uno::Sequence<sal_Int32> maComponentBitCounts;
242 : rendering::IntegerBitmapLayout maLayout;
243 : const sal_Int32 mnBitsPerPixel;
244 :
245 : // XBitmap
246 2 : virtual geometry::IntegerSize2D SAL_CALL getSize() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return maSize; }
247 2 : virtual sal_Bool SAL_CALL hasAlpha( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return mnBitsPerPixel != 8; }
248 0 : virtual uno::Reference< rendering::XBitmap > SAL_CALL getScaledBitmap( const geometry::RealSize2D&,
249 0 : sal_Bool ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return this; }
250 :
251 : // XIntegerReadOnlyBitmap
252 20 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL getData( rendering::IntegerBitmapLayout& bitmapLayout,
253 : const geometry::IntegerRectangle2D& rect )
254 : throw (lang::IndexOutOfBoundsException,
255 : rendering::VolatileContentDestroyedException,
256 : uno::RuntimeException,
257 : std::exception) SAL_OVERRIDE
258 : {
259 20 : CPPUNIT_ASSERT_MESSAGE( "X1 out of bounds", rect.X1 >= 0 );
260 20 : CPPUNIT_ASSERT_MESSAGE( "Y1 out of bounds", rect.Y1 >= 0 );
261 20 : CPPUNIT_ASSERT_MESSAGE( "X2 out of bounds", rect.X2 <= maSize.Width );
262 20 : CPPUNIT_ASSERT_MESSAGE( "Y2 out of bounds", rect.Y2 <= maSize.Height );
263 :
264 20 : bitmapLayout = getMemoryLayout();
265 :
266 20 : const sal_Int32 nWidth = rect.X2-rect.X1;
267 20 : const sal_Int32 nHeight = rect.Y2-rect.Y1;
268 20 : const sal_Int32 nScanlineLen = (nWidth * mnBitsPerPixel + 7)/8;
269 20 : uno::Sequence<sal_Int8> aRes( nScanlineLen * nHeight );
270 20 : sal_Int8* pOut = aRes.getArray();
271 :
272 20 : bitmapLayout.ScanLines = nHeight;
273 : bitmapLayout.ScanLineBytes =
274 20 : bitmapLayout.ScanLineStride= nScanlineLen;
275 :
276 20 : if( mnBitsPerPixel == 8 )
277 : {
278 20 : for( sal_Int32 y=0; y<nHeight; ++y )
279 : {
280 110 : for( sal_Int32 x=0; x<nWidth; ++x )
281 100 : pOut[ y*nScanlineLen + x ] = sal_Int8(x);
282 : }
283 : }
284 : else
285 : {
286 20 : for( sal_Int32 y=0; y<nHeight; ++y )
287 : {
288 110 : for( sal_Int32 x=0; x<nWidth; ++x )
289 : {
290 100 : pOut[ y*nScanlineLen + 4*x ] = sal_Int8(rect.X1);
291 100 : pOut[ y*nScanlineLen + 4*x + 1 ] = sal_Int8(rect.Y2);
292 100 : pOut[ y*nScanlineLen + 4*x + 2 ] = sal_Int8(x);
293 100 : pOut[ y*nScanlineLen + 4*x + 3 ] = sal_Int8(rect.Y1);
294 : }
295 : }
296 : }
297 :
298 20 : return aRes;
299 : }
300 :
301 0 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL getPixel( rendering::IntegerBitmapLayout&,
302 : const geometry::IntegerPoint2D& )
303 : throw (lang::IndexOutOfBoundsException,
304 : rendering::VolatileContentDestroyedException,
305 : uno::RuntimeException,
306 : std::exception) SAL_OVERRIDE
307 : {
308 0 : CPPUNIT_ASSERT_MESSAGE("getPixel: method not implemented", false);
309 0 : return uno::Sequence< sal_Int8 >();
310 : }
311 :
312 298 : virtual uno::Reference< rendering::XBitmapPalette > SAL_CALL getPalette( ) throw (uno::RuntimeException)
313 : {
314 298 : uno::Reference< XBitmapPalette > aRet;
315 298 : if( mnBitsPerPixel == 8 )
316 277 : aRet.set(this);
317 298 : return aRet;
318 : }
319 :
320 22 : virtual rendering::IntegerBitmapLayout SAL_CALL getMemoryLayout( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
321 : {
322 22 : rendering::IntegerBitmapLayout aLayout( maLayout );
323 :
324 22 : const sal_Int32 nScanlineLen = (maSize.Width * mnBitsPerPixel + 7)/8;
325 :
326 22 : aLayout.ScanLines = maSize.Height;
327 : aLayout.ScanLineBytes =
328 22 : aLayout.ScanLineStride= nScanlineLen;
329 22 : aLayout.Palette = getPalette();
330 22 : aLayout.ColorSpace.set( this );
331 :
332 22 : return aLayout;
333 : }
334 :
335 : // XBitmapPalette
336 1 : virtual sal_Int32 SAL_CALL getNumberOfEntries() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
337 : {
338 2 : CPPUNIT_ASSERT_MESSAGE( "Got palette getNumberOfEntries interface call without handing out palette",
339 1 : getPalette().is() );
340 :
341 1 : return 255;
342 : }
343 :
344 255 : virtual sal_Bool SAL_CALL getIndex( uno::Sequence< double >& entry,
345 : ::sal_Int32 nIndex )
346 : throw (lang::IndexOutOfBoundsException,
347 : uno::RuntimeException,
348 : std::exception) SAL_OVERRIDE
349 : {
350 510 : CPPUNIT_ASSERT_MESSAGE( "Got palette getIndex interface call without handing out palette",
351 255 : getPalette().is() );
352 510 : CPPUNIT_ASSERT_MESSAGE( "getIndex: index out of range",
353 255 : nIndex >= 0 && nIndex < 256 );
354 510 : entry = colorToStdColorSpaceSequence(
355 : Color(sal_uInt8(nIndex),
356 : sal_uInt8(nIndex),
357 255 : sal_uInt8(nIndex)) );
358 :
359 255 : return true; // no palette transparency here.
360 : }
361 :
362 0 : virtual sal_Bool SAL_CALL setIndex( const uno::Sequence< double >&,
363 : sal_Bool,
364 : ::sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException,
365 : lang::IllegalArgumentException,
366 : uno::RuntimeException,
367 : std::exception) SAL_OVERRIDE
368 : {
369 0 : CPPUNIT_ASSERT_MESSAGE( "Got palette setIndex interface call without handing out palette",
370 0 : getPalette().is());
371 0 : CPPUNIT_ASSERT_MESSAGE( "setIndex: index out of range",
372 0 : nIndex >= 0 && nIndex < 256);
373 0 : return false;
374 : }
375 :
376 : struct PaletteColorSpaceHolder: public rtl::StaticWithInit<uno::Reference<rendering::XColorSpace>,
377 : PaletteColorSpaceHolder>
378 : {
379 1 : uno::Reference<rendering::XColorSpace> operator()()
380 : {
381 1 : return vcl::unotools::createStandardColorSpace();
382 : }
383 : };
384 :
385 2 : virtual uno::Reference< rendering::XColorSpace > SAL_CALL getColorSpace( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
386 : {
387 : // this is the method from XBitmapPalette. Return palette color
388 : // space here
389 2 : return PaletteColorSpaceHolder::get();
390 : }
391 :
392 : // XIntegerBitmapColorSpace
393 0 : virtual ::sal_Int8 SAL_CALL getType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
394 : {
395 0 : return rendering::ColorSpaceType::RGB;
396 : }
397 :
398 1 : virtual uno::Sequence< sal_Int8 > SAL_CALL getComponentTags( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
399 : {
400 1 : return maComponentTags;
401 : }
402 :
403 0 : virtual ::sal_Int8 SAL_CALL getRenderingIntent( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
404 : {
405 0 : return rendering::RenderingIntent::PERCEPTUAL;
406 : }
407 :
408 0 : virtual uno::Sequence< beans::PropertyValue > SAL_CALL getProperties()
409 : throw (uno::RuntimeException,
410 : std::exception) SAL_OVERRIDE
411 : {
412 0 : CPPUNIT_ASSERT_MESSAGE("getProperties: method not implemented", false );
413 0 : return uno::Sequence< ::beans::PropertyValue >();
414 : }
415 :
416 0 : virtual uno::Sequence< double > SAL_CALL convertColorSpace( const uno::Sequence< double >&,
417 : const uno::Reference< rendering::XColorSpace >& )
418 : throw (uno::RuntimeException,
419 : std::exception) SAL_OVERRIDE
420 : {
421 0 : CPPUNIT_ASSERT_MESSAGE("convertColorSpace: method not implemented", false);
422 0 : return uno::Sequence< double >();
423 : }
424 :
425 0 : virtual uno::Sequence< rendering::RGBColor > SAL_CALL convertToRGB( const uno::Sequence< double >& )
426 : throw (lang::IllegalArgumentException,
427 : uno::RuntimeException,
428 : std::exception) SAL_OVERRIDE
429 : {
430 0 : CPPUNIT_ASSERT_MESSAGE("convertToRGB: method not implemented", false);
431 0 : return uno::Sequence< rendering::RGBColor >();
432 : }
433 :
434 0 : virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToARGB( const uno::Sequence< double >& )
435 : throw (lang::IllegalArgumentException,
436 : uno::RuntimeException,
437 : std::exception) SAL_OVERRIDE
438 : {
439 0 : CPPUNIT_ASSERT_MESSAGE("convertToARGB: method not implemented", false);
440 0 : return uno::Sequence< rendering::ARGBColor >();
441 : }
442 :
443 0 : virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToPARGB( const uno::Sequence< double >& )
444 : throw (lang::IllegalArgumentException,
445 : uno::RuntimeException,
446 : std::exception) SAL_OVERRIDE
447 : {
448 0 : CPPUNIT_ASSERT_MESSAGE("convertToPARGB: method not implemented", false);
449 0 : return uno::Sequence< rendering::ARGBColor >();
450 : }
451 :
452 0 : virtual uno::Sequence< double > SAL_CALL convertFromRGB( const uno::Sequence< rendering::RGBColor >& )
453 : throw (lang::IllegalArgumentException,
454 : uno::RuntimeException,
455 : std::exception) SAL_OVERRIDE
456 : {
457 0 : CPPUNIT_ASSERT_MESSAGE("convertFromRGB: method not implemented", false);
458 0 : return uno::Sequence< double >();
459 : }
460 :
461 0 : virtual uno::Sequence< double > SAL_CALL convertFromARGB( const uno::Sequence< rendering::ARGBColor >& )
462 : throw (lang::IllegalArgumentException,
463 : uno::RuntimeException,
464 : std::exception) SAL_OVERRIDE
465 : {
466 0 : CPPUNIT_ASSERT_MESSAGE("convertFromARGB: this method is not expected to be called!", false);
467 0 : return uno::Sequence< double >();
468 : }
469 :
470 0 : virtual uno::Sequence< double > SAL_CALL convertFromPARGB( const uno::Sequence< rendering::ARGBColor >& )
471 : throw (lang::IllegalArgumentException,
472 : uno::RuntimeException,
473 : std::exception) SAL_OVERRIDE
474 : {
475 0 : CPPUNIT_ASSERT_MESSAGE("convertFromPARGB: this method is not expected to be called!", false);
476 0 : return uno::Sequence< double >();
477 : }
478 :
479 2 : virtual ::sal_Int32 SAL_CALL getBitsPerPixel( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
480 : {
481 2 : return mnBitsPerPixel;
482 : }
483 :
484 1 : virtual uno::Sequence< ::sal_Int32 > SAL_CALL getComponentBitCounts( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
485 : {
486 1 : return maComponentBitCounts;
487 : }
488 :
489 0 : virtual ::sal_Int8 SAL_CALL getEndianness( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
490 : {
491 0 : return util::Endianness::LITTLE;
492 : }
493 :
494 0 : virtual uno::Sequence< double > SAL_CALL convertFromIntegerColorSpace( const uno::Sequence< ::sal_Int8 >& ,
495 : const uno::Reference< rendering::XColorSpace >& )
496 : throw (lang::IllegalArgumentException,
497 : uno::RuntimeException,
498 : std::exception) SAL_OVERRIDE
499 : {
500 0 : CPPUNIT_ASSERT_MESSAGE("convertFromIntegerColorSpace: method not implemented", false);
501 0 : return uno::Sequence< double >();
502 : }
503 :
504 0 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertToIntegerColorSpace( const uno::Sequence< ::sal_Int8 >& ,
505 : const uno::Reference< rendering::XIntegerBitmapColorSpace >& )
506 : throw (lang::IllegalArgumentException,
507 : uno::RuntimeException,
508 : std::exception) SAL_OVERRIDE
509 : {
510 0 : CPPUNIT_ASSERT_MESSAGE("convertToIntegerColorSpace: method not implemented", false);
511 0 : return uno::Sequence< sal_Int8 >();
512 : }
513 :
514 10 : virtual uno::Sequence< rendering::RGBColor > SAL_CALL convertIntegerToRGB( const uno::Sequence< ::sal_Int8 >& deviceColor ) throw (lang::IllegalArgumentException,
515 : uno::RuntimeException, std::exception) SAL_OVERRIDE
516 : {
517 10 : const uno::Sequence< rendering::ARGBColor > aTemp( convertIntegerToARGB(deviceColor) );
518 10 : const sal_Size nLen(aTemp.getLength());
519 10 : uno::Sequence< rendering::RGBColor > aRes( nLen );
520 10 : rendering::RGBColor* pOut = aRes.getArray();
521 110 : for( sal_Size i=0; i<nLen; ++i )
522 : {
523 100 : *pOut++ = rendering::RGBColor(aTemp[i].Red,
524 100 : aTemp[i].Green,
525 300 : aTemp[i].Blue);
526 : }
527 :
528 10 : return aRes;
529 : }
530 :
531 20 : virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertIntegerToARGB( const uno::Sequence< ::sal_Int8 >& deviceColor ) throw (lang::IllegalArgumentException,
532 : uno::RuntimeException, std::exception) SAL_OVERRIDE
533 : {
534 20 : const sal_Size nLen( deviceColor.getLength() );
535 20 : const sal_Int32 nBytesPerPixel(mnBitsPerPixel == 8 ? 1 : 4);
536 40 : CPPUNIT_ASSERT_MESSAGE("number of channels no multiple of pixel element count",
537 20 : nLen%nBytesPerPixel==0);
538 :
539 20 : uno::Sequence< rendering::ARGBColor > aRes( nLen / nBytesPerPixel );
540 20 : rendering::ARGBColor* pOut( aRes.getArray() );
541 :
542 20 : if( getPalette().is() )
543 : {
544 110 : for( sal_Size i=0; i<nLen; ++i )
545 : {
546 : *pOut++ = rendering::ARGBColor(
547 : 1.0,
548 100 : vcl::unotools::toDoubleColor(deviceColor[i]),
549 100 : vcl::unotools::toDoubleColor(deviceColor[i]),
550 300 : vcl::unotools::toDoubleColor(deviceColor[i]));
551 : }
552 : }
553 : else
554 : {
555 110 : for( sal_Size i=0; i<nLen; i+=4 )
556 : {
557 : *pOut++ = rendering::ARGBColor(
558 100 : vcl::unotools::toDoubleColor(deviceColor[i+3]),
559 100 : vcl::unotools::toDoubleColor(deviceColor[i+0]),
560 100 : vcl::unotools::toDoubleColor(deviceColor[i+1]),
561 400 : vcl::unotools::toDoubleColor(deviceColor[i+2]));
562 : }
563 : }
564 :
565 20 : return aRes;
566 : }
567 :
568 0 : virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertIntegerToPARGB(
569 : const uno::Sequence< ::sal_Int8 >& deviceColor)
570 : throw (lang::IllegalArgumentException,
571 : uno::RuntimeException,
572 : std::exception) SAL_OVERRIDE
573 : {
574 0 : const sal_Size nLen( deviceColor.getLength() );
575 0 : const sal_Int32 nBytesPerPixel(mnBitsPerPixel == 8 ? 1 : 4);
576 0 : CPPUNIT_ASSERT_MESSAGE("number of channels no multiple of pixel element count",
577 0 : nLen%nBytesPerPixel==0);
578 :
579 0 : uno::Sequence< rendering::ARGBColor > aRes( nLen / nBytesPerPixel );
580 0 : rendering::ARGBColor* pOut( aRes.getArray() );
581 :
582 0 : if( getPalette().is() )
583 : {
584 0 : for( sal_Size i=0; i<nLen; ++i )
585 : {
586 : *pOut++ = rendering::ARGBColor(
587 : 1.0,
588 0 : vcl::unotools::toDoubleColor(deviceColor[i]),
589 0 : vcl::unotools::toDoubleColor(deviceColor[i]),
590 0 : vcl::unotools::toDoubleColor(deviceColor[i]));
591 : }
592 : }
593 : else
594 : {
595 0 : for( sal_Size i=0; i<nLen; i+=4 )
596 : {
597 0 : const double fAlpha=vcl::unotools::toDoubleColor(deviceColor[i+3]);
598 : *pOut++ = rendering::ARGBColor(
599 : fAlpha,
600 0 : fAlpha*vcl::unotools::toDoubleColor(deviceColor[i+0]),
601 0 : fAlpha*vcl::unotools::toDoubleColor(deviceColor[i+1]),
602 0 : fAlpha*vcl::unotools::toDoubleColor(deviceColor[i+2]));
603 : }
604 : }
605 :
606 0 : return aRes;
607 : }
608 :
609 0 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromRGB(
610 : const uno::Sequence< rendering::RGBColor >&)
611 : throw (lang::IllegalArgumentException,
612 : uno::RuntimeException,
613 : std::exception) SAL_OVERRIDE
614 : {
615 0 : CPPUNIT_ASSERT_MESSAGE("convertIntegerFromRGB: method not implemented", false);
616 0 : return uno::Sequence< sal_Int8 >();
617 : }
618 :
619 0 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromARGB( const uno::Sequence< rendering::ARGBColor >& ) throw (lang::IllegalArgumentException,
620 : uno::RuntimeException,
621 : std::exception) SAL_OVERRIDE
622 : {
623 0 : CPPUNIT_ASSERT_MESSAGE("convertIntegerFromARGB: method not implemented", false);
624 0 : return uno::Sequence< sal_Int8 >();
625 : }
626 :
627 0 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromPARGB( const uno::Sequence< rendering::ARGBColor >& )
628 : throw (lang::IllegalArgumentException,
629 : uno::RuntimeException,
630 : std::exception) SAL_OVERRIDE
631 : {
632 0 : CPPUNIT_ASSERT_MESSAGE("convertIntegerFromPARGB: method not implemented", false);
633 0 : return uno::Sequence< sal_Int8 >();
634 : }
635 :
636 : public:
637 2 : TestBitmap( const geometry::IntegerSize2D& rSize, bool bPalette ) :
638 : maSize(rSize),
639 : maComponentTags(),
640 : maComponentBitCounts(),
641 : maLayout(),
642 2 : mnBitsPerPixel( bPalette ? 8 : 32 )
643 : {
644 2 : if( bPalette )
645 : {
646 1 : maComponentTags.realloc(1);
647 1 : maComponentTags[0] = rendering::ColorComponentTag::INDEX;
648 :
649 1 : maComponentBitCounts.realloc(1);
650 1 : maComponentBitCounts[0] = 8;
651 : }
652 : else
653 : {
654 1 : maComponentTags.realloc(4);
655 1 : sal_Int8* pTags = maComponentTags.getArray();
656 1 : pTags[0] = rendering::ColorComponentTag::RGB_BLUE;
657 1 : pTags[1] = rendering::ColorComponentTag::RGB_GREEN;
658 1 : pTags[2] = rendering::ColorComponentTag::RGB_RED;
659 1 : pTags[3] = rendering::ColorComponentTag::ALPHA;
660 :
661 1 : maComponentBitCounts.realloc(4);
662 1 : sal_Int32* pCounts = maComponentBitCounts.getArray();
663 1 : pCounts[0] = 8;
664 1 : pCounts[1] = 8;
665 1 : pCounts[2] = 8;
666 1 : pCounts[3] = 8;
667 : }
668 :
669 2 : maLayout.ScanLines = 0;
670 2 : maLayout.ScanLineBytes = 0;
671 2 : maLayout.ScanLineStride = 0;
672 2 : maLayout.PlaneStride = 0;
673 2 : maLayout.ColorSpace.clear();
674 2 : maLayout.Palette.clear();
675 2 : maLayout.IsMsbFirst = false;
676 2 : }
677 : };
678 :
679 1 : void CanvasBitmapTest::runTest()
680 : {
681 : static const sal_Int8 lcl_depths[]={1,4,8,16,24};
682 :
683 : // Testing VclCanvasBitmap wrapper
684 :
685 6 : for( unsigned int i=0; i<SAL_N_ELEMENTS(lcl_depths); ++i )
686 : {
687 5 : const sal_Int8 nDepth( lcl_depths[i] );
688 5 : Bitmap aBitmap(Size(200,200),nDepth);
689 5 : aBitmap.Erase(COL_WHITE);
690 : {
691 5 : Bitmap::ScopedWriteAccess pAcc(aBitmap);
692 5 : if( pAcc.get() )
693 : {
694 5 : BitmapColor aBlack(0);
695 10 : BitmapColor aWhite(0);
696 5 : if( pAcc->HasPalette() )
697 : {
698 3 : aBlack.SetIndex( sal::static_int_cast<sal_Int8>(pAcc->GetBestPaletteIndex(BitmapColor(0,0,0))) );
699 3 : aWhite.SetIndex( sal::static_int_cast<sal_Int8>(pAcc->GetBestPaletteIndex(BitmapColor(255,255,255))) );
700 : }
701 : else
702 : {
703 2 : aBlack = Color(COL_BLACK);
704 2 : aWhite = Color(COL_WHITE);
705 : }
706 5 : pAcc->SetFillColor(COL_GREEN);
707 5 : pAcc->FillRect(Rectangle(0,0,100,100));
708 5 : pAcc->SetPixel(0,0,aWhite);
709 5 : pAcc->SetPixel(0,1,aBlack);
710 10 : pAcc->SetPixel(0,2,aWhite);
711 5 : }
712 : }
713 :
714 10 : rtl::Reference<VclCanvasBitmap> xBmp( new VclCanvasBitmap(aBitmap) );
715 :
716 5 : checkCanvasBitmap( xBmp, "single bitmap", nDepth );
717 :
718 10 : Bitmap aMask(Size(200,200),1);
719 5 : aMask.Erase(COL_WHITE);
720 : {
721 5 : Bitmap::ScopedWriteAccess pAcc(aMask);
722 5 : if( pAcc.get() )
723 : {
724 5 : pAcc->SetFillColor(COL_BLACK);
725 5 : pAcc->FillRect(Rectangle(0,0,100,100));
726 5 : pAcc->SetPixel(0,0,BitmapColor(1));
727 5 : pAcc->SetPixel(0,1,BitmapColor(0));
728 5 : pAcc->SetPixel(0,2,BitmapColor(1));
729 5 : }
730 : }
731 :
732 5 : xBmp.set( new VclCanvasBitmap(BitmapEx(aBitmap,aMask)) );
733 :
734 5 : checkCanvasBitmap( xBmp, "masked bitmap", nDepth );
735 :
736 10 : AlphaMask aAlpha(Size(200,200));
737 5 : aAlpha.Erase(255);
738 : {
739 5 : BitmapWriteAccess* pAcc = aAlpha.AcquireWriteAccess();
740 5 : if( pAcc )
741 : {
742 5 : pAcc->SetFillColor(COL_BLACK);
743 5 : pAcc->FillRect(Rectangle(0,0,100,100));
744 5 : pAcc->SetPixel(0,0,BitmapColor(255));
745 5 : pAcc->SetPixel(0,1,BitmapColor(0));
746 5 : pAcc->SetPixel(0,2,BitmapColor(255));
747 5 : aAlpha.ReleaseAccess(pAcc);
748 : }
749 : }
750 :
751 5 : xBmp.set( new VclCanvasBitmap(BitmapEx(aBitmap,aAlpha)) );
752 :
753 5 : checkCanvasBitmap( xBmp, "alpha bitmap", nDepth );
754 5 : }
755 :
756 : // Testing XBitmap import
757 :
758 : uno::Reference< rendering::XIntegerReadOnlyBitmap > xTestBmp(
759 1 : new TestBitmap( geometry::IntegerSize2D(10,10), true ));
760 :
761 2 : BitmapEx aBmp = vcl::unotools::bitmapExFromXBitmap(xTestBmp);
762 2 : CPPUNIT_ASSERT_MESSAGE( "Palette bitmap is transparent",
763 1 : !aBmp.IsTransparent());
764 2 : CPPUNIT_ASSERT_MESSAGE( "Bitmap does not have size (10,10)",
765 1 : aBmp.GetSizePixel() == Size(10,10));
766 2 : CPPUNIT_ASSERT_MESSAGE( "Bitmap does not have bitcount of 8",
767 1 : aBmp.GetBitCount() == 8);
768 : {
769 1 : BitmapReadAccess* pBmpAcc = aBmp.GetBitmap().AcquireReadAccess();
770 :
771 2 : CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid BitmapReadAccess",
772 1 : pBmpAcc );
773 :
774 2 : CPPUNIT_ASSERT_MESSAGE("(0,0) incorrect content",
775 1 : pBmpAcc->GetPixel(0,0) == BitmapColor(0));
776 2 : CPPUNIT_ASSERT_MESSAGE("(2,2) incorrect content",
777 1 : pBmpAcc->GetPixel(2,2) == BitmapColor(2));
778 2 : CPPUNIT_ASSERT_MESSAGE("(9,2) incorrect content",
779 1 : pBmpAcc->GetPixel(2,9) == BitmapColor(9));
780 :
781 1 : aBmp.GetBitmap().ReleaseAccess(pBmpAcc);
782 : }
783 :
784 1 : xTestBmp.set( new TestBitmap( geometry::IntegerSize2D(10,10), false ));
785 :
786 1 : aBmp = vcl::unotools::bitmapExFromXBitmap(xTestBmp);
787 2 : CPPUNIT_ASSERT_MESSAGE( "Palette bitmap is not transparent",
788 1 : aBmp.IsTransparent());
789 2 : CPPUNIT_ASSERT_MESSAGE( "Palette bitmap has no alpha",
790 1 : aBmp.IsAlpha());
791 2 : CPPUNIT_ASSERT_MESSAGE( "Bitmap does not have size (10,10)",
792 1 : aBmp.GetSizePixel() == Size(10,10));
793 2 : CPPUNIT_ASSERT_MESSAGE( "Bitmap has bitcount of 24",
794 1 : aBmp.GetBitCount() == 24);
795 : {
796 1 : BitmapReadAccess* pBmpAcc = aBmp.GetBitmap().AcquireReadAccess();
797 1 : BitmapReadAccess* pAlphaAcc = aBmp.GetAlpha().AcquireReadAccess();
798 :
799 2 : CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid BitmapReadAccess",
800 1 : pBmpAcc);
801 2 : CPPUNIT_ASSERT_MESSAGE( "Bitmap has invalid alpha BitmapReadAccess",
802 1 : pAlphaAcc);
803 :
804 2 : CPPUNIT_ASSERT_MESSAGE("(0,0) incorrect content",
805 1 : pBmpAcc->GetPixel(0,0) == BitmapColor(0,1,0));
806 2 : CPPUNIT_ASSERT_MESSAGE("(0,0) incorrect alpha content",
807 1 : pAlphaAcc->GetPixel(0,0) == BitmapColor(255));
808 2 : CPPUNIT_ASSERT_MESSAGE("(2,2) incorrect content",
809 1 : pBmpAcc->GetPixel(2,2) == BitmapColor(0,3,2));
810 2 : CPPUNIT_ASSERT_MESSAGE("(2,2) incorrect alpha content",
811 1 : pAlphaAcc->GetPixel(2,2) == BitmapColor(253));
812 2 : CPPUNIT_ASSERT_MESSAGE("(9,2) incorrect content",
813 1 : pBmpAcc->GetPixel(2,9) == BitmapColor(0,3,9));
814 2 : CPPUNIT_ASSERT_MESSAGE("(9,2) correct alpha content",
815 1 : pAlphaAcc->GetPixel(2,9) == BitmapColor(253));
816 :
817 1 : aBmp.GetAlpha().ReleaseAccess(pAlphaAcc);
818 1 : aBmp.GetBitmap().ReleaseAccess(pBmpAcc);
819 1 : }
820 1 : }
821 :
822 : } // namespace
823 :
824 3 : CPPUNIT_TEST_SUITE_REGISTRATION(CanvasBitmapTest);
825 :
826 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|