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 : #include <cppuhelper/compbase1.hxx>
21 :
22 : #include <com/sun/star/geometry/RealSize2D.hpp>
23 : #include <com/sun/star/geometry/RealPoint2D.hpp>
24 : #include <com/sun/star/geometry/RealRectangle2D.hpp>
25 : #include <com/sun/star/geometry/IntegerSize2D.hpp>
26 : #include <com/sun/star/geometry/IntegerPoint2D.hpp>
27 : #include <com/sun/star/geometry/IntegerRectangle2D.hpp>
28 : #include <com/sun/star/geometry/RealBezierSegment2D.hpp>
29 :
30 : #include <com/sun/star/rendering/ColorSpaceType.hpp>
31 : #include <com/sun/star/rendering/RenderingIntent.hpp>
32 : #include <com/sun/star/rendering/XGraphicDevice.hpp>
33 : #include <com/sun/star/rendering/XBitmap.hpp>
34 : #include <com/sun/star/rendering/XPolyPolygon2D.hpp>
35 : #include <com/sun/star/rendering/IntegerBitmapLayout.hpp>
36 : #include <com/sun/star/rendering/XIntegerBitmap.hpp>
37 : #include <com/sun/star/rendering/ColorComponentTag.hpp>
38 :
39 : #include <basegfx/matrix/b2dhommatrix.hxx>
40 : #include <basegfx/vector/b2dsize.hxx>
41 : #include <basegfx/point/b2dpoint.hxx>
42 : #include <basegfx/range/b2drectangle.hxx>
43 : #include <basegfx/vector/b2isize.hxx>
44 : #include <basegfx/point/b2ipoint.hxx>
45 : #include <basegfx/range/b2irectangle.hxx>
46 :
47 : // #i79917#
48 : #include <basegfx/polygon/b2dpolygon.hxx>
49 : #include <basegfx/tools/canvastools.hxx>
50 : #include <basegfx/polygon/b2dpolypolygon.hxx>
51 :
52 : #include <tools/poly.hxx>
53 : #include <tools/diagnose_ex.h>
54 : #include <rtl/uuid.h>
55 :
56 : #include <vcl/salbtype.hxx>
57 : #include <vcl/bmpacc.hxx>
58 : #include <vcl/bitmapex.hxx>
59 :
60 : #include <canvasbitmap.hxx>
61 : #include <vcl/canvastools.hxx>
62 : #include <boost/unordered_map.hpp>
63 :
64 : using namespace ::com::sun::star;
65 :
66 : namespace vcl
67 : {
68 : namespace unotools
69 : {
70 0 : uno::Reference< rendering::XBitmap > xBitmapFromBitmapEx( const uno::Reference< rendering::XGraphicDevice >& /*xGraphicDevice*/,
71 : const ::BitmapEx& inputBitmap )
72 : {
73 : SAL_INFO( "vcl.helper", "::vcl::unotools::xBitmapFromBitmapEx()" );
74 :
75 0 : return new vcl::unotools::VclCanvasBitmap( inputBitmap );
76 : }
77 :
78 : namespace
79 : {
80 0 : inline bool operator==( const rendering::IntegerBitmapLayout& rLHS,
81 : const rendering::IntegerBitmapLayout& rRHS )
82 : {
83 : return
84 0 : rLHS.ScanLineBytes == rRHS.ScanLineBytes &&
85 0 : rLHS.ScanLineStride == rRHS.ScanLineStride &&
86 0 : rLHS.PlaneStride == rRHS.PlaneStride &&
87 0 : rLHS.ColorSpace == rRHS.ColorSpace &&
88 0 : rLHS.Palette == rRHS.Palette &&
89 0 : rLHS.IsMsbFirst == rRHS.IsMsbFirst;
90 : }
91 :
92 0 : bool readBmp( sal_Int32 nWidth,
93 : sal_Int32 nHeight,
94 : const rendering::IntegerBitmapLayout& rLayout,
95 : const uno::Reference< rendering::XIntegerReadOnlyBitmap >& xInputBitmap,
96 : Bitmap::ScopedWriteAccess& rWriteAcc,
97 : Bitmap::ScopedWriteAccess& rAlphaAcc )
98 : {
99 0 : rendering::IntegerBitmapLayout aCurrLayout;
100 0 : geometry::IntegerRectangle2D aRect;
101 0 : uno::Sequence<sal_Int8> aPixelData;
102 0 : uno::Sequence<rendering::RGBColor> aRGBColors;
103 0 : uno::Sequence<rendering::ARGBColor> aARGBColors;
104 :
105 0 : for( aRect.Y1=0; aRect.Y1<nHeight; ++aRect.Y1 )
106 : {
107 0 : aRect.X1 = 0; aRect.X2 = nWidth; aRect.Y2 = aRect.Y1+1;
108 : try
109 : {
110 0 : aPixelData = xInputBitmap->getData(aCurrLayout,aRect);
111 : }
112 0 : catch( rendering::VolatileContentDestroyedException& )
113 : {
114 : // re-read bmp from the start
115 0 : return false;
116 : }
117 0 : if( !(aCurrLayout == rLayout) )
118 0 : return false; // re-read bmp from the start
119 :
120 0 : if( rAlphaAcc.get() )
121 : {
122 : // read ARGB color
123 0 : aARGBColors = rLayout.ColorSpace->convertIntegerToARGB(aPixelData);
124 :
125 0 : if( rWriteAcc->HasPalette() )
126 : {
127 0 : for( sal_Int32 x=0; x<nWidth; ++x )
128 : {
129 0 : const rendering::ARGBColor& rColor=aARGBColors[x];
130 : rWriteAcc->SetPixelIndex( aRect.Y1, x,
131 0 : (sal_uInt8) rWriteAcc->GetBestPaletteIndex(
132 0 : BitmapColor( toByteColor(rColor.Red),
133 0 : toByteColor(rColor.Green),
134 0 : toByteColor(rColor.Blue))) );
135 : rAlphaAcc->SetPixel( aRect.Y1, x,
136 0 : BitmapColor( 255 - toByteColor(rColor.Alpha) ));
137 : }
138 : }
139 : else
140 : {
141 0 : for( sal_Int32 x=0; x<nWidth; ++x )
142 : {
143 0 : const rendering::ARGBColor& rColor=aARGBColors[x];
144 : rWriteAcc->SetPixel( aRect.Y1, x,
145 0 : BitmapColor( toByteColor(rColor.Red),
146 0 : toByteColor(rColor.Green),
147 0 : toByteColor(rColor.Blue) ));
148 : rAlphaAcc->SetPixel( aRect.Y1, x,
149 0 : BitmapColor( 255 - toByteColor(rColor.Alpha) ));
150 : }
151 : }
152 : }
153 : else
154 : {
155 : // read RGB color
156 0 : aRGBColors = rLayout.ColorSpace->convertIntegerToRGB(aPixelData);
157 0 : if( rWriteAcc->HasPalette() )
158 : {
159 0 : for( sal_Int32 x=0; x<nWidth; ++x )
160 : {
161 0 : const rendering::RGBColor& rColor=aRGBColors[x];
162 : rWriteAcc->SetPixelIndex( aRect.Y1, x,
163 0 : (sal_uInt8) rWriteAcc->GetBestPaletteIndex(
164 0 : BitmapColor( toByteColor(rColor.Red),
165 0 : toByteColor(rColor.Green),
166 0 : toByteColor(rColor.Blue))) );
167 : }
168 : }
169 : else
170 : {
171 0 : for( sal_Int32 x=0; x<nWidth; ++x )
172 : {
173 0 : const rendering::RGBColor& rColor=aRGBColors[x];
174 : rWriteAcc->SetPixel( aRect.Y1, x,
175 0 : BitmapColor( toByteColor(rColor.Red),
176 0 : toByteColor(rColor.Green),
177 0 : toByteColor(rColor.Blue) ));
178 : }
179 : }
180 : }
181 : }
182 :
183 0 : return true;
184 : }
185 : }
186 :
187 0 : ::BitmapEx VCL_DLLPUBLIC bitmapExFromXBitmap( const uno::Reference< rendering::XIntegerReadOnlyBitmap >& xInputBitmap )
188 : {
189 : SAL_INFO( "vcl.helper", "::vcl::unotools::bitmapExFromXBitmap()" );
190 :
191 0 : if( !xInputBitmap.is() )
192 0 : return ::BitmapEx();
193 :
194 : // tunnel directly for known implementation
195 0 : VclCanvasBitmap* pImplBitmap = dynamic_cast<VclCanvasBitmap*>(xInputBitmap.get());
196 0 : if( pImplBitmap )
197 0 : return pImplBitmap->getBitmapEx();
198 :
199 : // retrieve data via UNO interface
200 :
201 : // volatile bitmaps are a bit more complicated to read
202 : // from..
203 : uno::Reference<rendering::XVolatileBitmap> xVolatileBitmap(
204 0 : xInputBitmap, uno::UNO_QUERY);
205 :
206 : // loop a few times, until successfully read (for XVolatileBitmap)
207 0 : for( int i=0; i<10; ++i )
208 : {
209 0 : sal_Int32 nDepth=0;
210 0 : sal_Int32 nAlphaDepth=0;
211 : const rendering::IntegerBitmapLayout aLayout(
212 0 : xInputBitmap->getMemoryLayout());
213 :
214 : OSL_ENSURE(aLayout.ColorSpace.is(),
215 : "Cannot convert image without color space!");
216 0 : if( !aLayout.ColorSpace.is() )
217 0 : return ::BitmapEx();
218 :
219 0 : nDepth = aLayout.ColorSpace->getBitsPerPixel();
220 :
221 0 : if( xInputBitmap->hasAlpha() )
222 : {
223 : // determine alpha channel depth
224 : const uno::Sequence<sal_Int8> aTags(
225 0 : aLayout.ColorSpace->getComponentTags() );
226 0 : const sal_Int8* pStart(aTags.getConstArray());
227 0 : const sal_Size nLen(aTags.getLength());
228 0 : const sal_Int8* pEnd(pStart+nLen);
229 :
230 : const std::ptrdiff_t nAlphaIndex =
231 : std::find(pStart,pEnd,
232 0 : rendering::ColorComponentTag::ALPHA) - pStart;
233 :
234 0 : if( nAlphaIndex < sal::static_int_cast<std::ptrdiff_t>(nLen) )
235 : {
236 0 : nAlphaDepth = aLayout.ColorSpace->getComponentBitCounts()[nAlphaIndex] > 1 ? 8 : 1;
237 0 : nDepth -= nAlphaDepth;
238 0 : }
239 : }
240 :
241 0 : BitmapPalette aPalette;
242 0 : if( aLayout.Palette.is() )
243 : {
244 : uno::Reference< rendering::XColorSpace > xPaletteColorSpace(
245 0 : aLayout.Palette->getColorSpace());
246 0 : ENSURE_OR_THROW(xPaletteColorSpace.is(),
247 : "Palette without color space");
248 :
249 0 : const sal_Int32 nEntryCount( aLayout.Palette->getNumberOfEntries() );
250 0 : if( nEntryCount <= 256 )
251 : {
252 0 : if( nEntryCount <= 2 )
253 0 : nDepth = 1;
254 : else
255 0 : nDepth = 8;
256 :
257 : const sal_uInt16 nPaletteEntries(
258 : sal::static_int_cast<sal_uInt16>(
259 0 : std::min(sal_Int32(255), nEntryCount)));
260 :
261 : // copy palette entries
262 0 : aPalette.SetEntryCount(nPaletteEntries);
263 0 : uno::Reference<rendering::XBitmapPalette> xPalette( aLayout.Palette );
264 0 : uno::Reference<rendering::XColorSpace> xPalColorSpace( xPalette->getColorSpace() );
265 :
266 0 : uno::Sequence<double> aPaletteEntry;
267 0 : for( sal_uInt16 j=0; j<nPaletteEntries; ++j )
268 : {
269 0 : if( !xPalette->getIndex(aPaletteEntry,j) &&
270 : nAlphaDepth == 0 )
271 : {
272 0 : nAlphaDepth = 1;
273 : }
274 0 : uno::Sequence<rendering::RGBColor> aColors=xPalColorSpace->convertToRGB(aPaletteEntry);
275 0 : ENSURE_OR_THROW(aColors.getLength() == 1,
276 : "Palette returned more or less than one entry");
277 0 : const rendering::RGBColor& rColor=aColors[0];
278 0 : aPalette[j] = BitmapColor(toByteColor(rColor.Red),
279 0 : toByteColor(rColor.Green),
280 0 : toByteColor(rColor.Blue));
281 0 : }
282 0 : }
283 : }
284 :
285 : const ::Size aPixelSize(
286 0 : sizeFromIntegerSize2D(xInputBitmap->getSize()));
287 :
288 : // normalize bitcount
289 : nDepth =
290 : ( nDepth <= 1 ) ? 1 :
291 : ( nDepth <= 4 ) ? 4 :
292 0 : ( nDepth <= 8 ) ? 8 : 24;
293 :
294 : ::Bitmap aBitmap( aPixelSize,
295 0 : sal::static_int_cast<sal_uInt16>(nDepth),
296 0 : aLayout.Palette.is() ? &aPalette : NULL );
297 0 : ::Bitmap aAlpha;
298 0 : if( nAlphaDepth )
299 0 : aAlpha = ::Bitmap( aPixelSize,
300 0 : sal::static_int_cast<sal_uInt16>(nAlphaDepth),
301 : &::Bitmap::GetGreyPalette(
302 0 : sal::static_int_cast<sal_uInt16>(1L << nAlphaDepth)) );
303 :
304 : { // limit scoped access
305 0 : Bitmap::ScopedWriteAccess pWriteAccess( aBitmap );
306 : Bitmap::ScopedWriteAccess pAlphaWriteAccess( nAlphaDepth ? aAlpha.AcquireWriteAccess() : NULL,
307 0 : aAlpha );
308 :
309 0 : ENSURE_OR_THROW(pWriteAccess.get() != NULL,
310 : "Cannot get write access to bitmap");
311 :
312 0 : const sal_Int32 nWidth(aPixelSize.Width());
313 0 : const sal_Int32 nHeight(aPixelSize.Height());
314 :
315 0 : if( !readBmp(nWidth,nHeight,aLayout,xInputBitmap,
316 0 : pWriteAccess,pAlphaWriteAccess) )
317 0 : continue;
318 : } // limit scoped access
319 :
320 0 : if( nAlphaDepth )
321 : return ::BitmapEx( aBitmap,
322 0 : AlphaMask( aAlpha ) );
323 : else
324 0 : return ::BitmapEx( aBitmap );
325 0 : }
326 :
327 : // failed to read data 10 times - bail out
328 0 : return ::BitmapEx();
329 : }
330 :
331 0 : geometry::RealSize2D size2DFromSize( const Size& rSize )
332 : {
333 0 : return geometry::RealSize2D( rSize.Width(),
334 0 : rSize.Height() );
335 : }
336 :
337 0 : Size sizeFromRealSize2D( const geometry::RealSize2D& rSize )
338 : {
339 0 : return Size( static_cast<long>(rSize.Width + .5),
340 0 : static_cast<long>(rSize.Height + .5) );
341 : }
342 :
343 0 : ::Size sizeFromB2DSize( const ::basegfx::B2DVector& rVec )
344 : {
345 : return ::Size( FRound( rVec.getX() ),
346 0 : FRound( rVec.getY() ) );
347 : }
348 :
349 0 : ::Point pointFromB2DPoint( const ::basegfx::B2DPoint& rPoint )
350 : {
351 : return ::Point( FRound( rPoint.getX() ),
352 0 : FRound( rPoint.getY() ) );
353 : }
354 :
355 0 : ::Rectangle rectangleFromB2DRectangle( const ::basegfx::B2DRange& rRect )
356 : {
357 : return ::Rectangle( FRound( rRect.getMinX() ),
358 : FRound( rRect.getMinY() ),
359 : FRound( rRect.getMaxX() ),
360 0 : FRound( rRect.getMaxY() ) );
361 : }
362 :
363 0 : Point pointFromB2IPoint( const ::basegfx::B2IPoint& rPoint )
364 : {
365 : return ::Point( rPoint.getX(),
366 0 : rPoint.getY() );
367 : }
368 :
369 0 : Rectangle rectangleFromB2IRectangle( const ::basegfx::B2IRange& rRect )
370 : {
371 : return ::Rectangle( rRect.getMinX(),
372 : rRect.getMinY(),
373 : rRect.getMaxX(),
374 0 : rRect.getMaxY() );
375 : }
376 :
377 0 : ::basegfx::B2DVector b2DSizeFromSize( const ::Size& rSize )
378 : {
379 0 : return ::basegfx::B2DVector( rSize.Width(),
380 0 : rSize.Height() );
381 : }
382 :
383 0 : ::basegfx::B2DPoint b2DPointFromPoint( const ::Point& rPoint )
384 : {
385 0 : return ::basegfx::B2DPoint( rPoint.X(),
386 0 : rPoint.Y() );
387 : }
388 :
389 0 : ::basegfx::B2DRange b2DRectangleFromRectangle( const ::Rectangle& rRect )
390 : {
391 0 : return ::basegfx::B2DRange( rRect.Left(),
392 0 : rRect.Top(),
393 0 : rRect.Right(),
394 0 : rRect.Bottom() );
395 : }
396 :
397 0 : geometry::IntegerSize2D integerSize2DFromSize( const Size& rSize )
398 : {
399 0 : return geometry::IntegerSize2D( rSize.Width(),
400 0 : rSize.Height() );
401 : }
402 :
403 0 : Size sizeFromIntegerSize2D( const geometry::IntegerSize2D& rSize )
404 : {
405 : return Size( rSize.Width,
406 0 : rSize.Height );
407 : }
408 :
409 0 : Point pointFromIntegerPoint2D( const geometry::IntegerPoint2D& rPoint )
410 : {
411 : return Point( rPoint.X,
412 0 : rPoint.Y );
413 : }
414 :
415 0 : Rectangle rectangleFromIntegerRectangle2D( const geometry::IntegerRectangle2D& rRectangle )
416 : {
417 : return Rectangle( rRectangle.X1, rRectangle.Y1,
418 0 : rRectangle.X2, rRectangle.Y2 );
419 : }
420 :
421 : namespace
422 : {
423 0 : class StandardColorSpace : public cppu::WeakImplHelper1< com::sun::star::rendering::XColorSpace >
424 : {
425 : private:
426 : uno::Sequence< sal_Int8 > m_aComponentTags;
427 :
428 0 : virtual ::sal_Int8 SAL_CALL getType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
429 : {
430 0 : return rendering::ColorSpaceType::RGB;
431 : }
432 0 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL getComponentTags( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
433 : {
434 0 : return m_aComponentTags;
435 : }
436 0 : virtual ::sal_Int8 SAL_CALL getRenderingIntent( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
437 : {
438 0 : return rendering::RenderingIntent::PERCEPTUAL;
439 : }
440 0 : virtual uno::Sequence< beans::PropertyValue > SAL_CALL getProperties( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
441 : {
442 0 : return uno::Sequence< beans::PropertyValue >();
443 : }
444 0 : virtual uno::Sequence< double > SAL_CALL convertColorSpace( const uno::Sequence< double >& deviceColor,
445 : const uno::Reference< rendering::XColorSpace >& targetColorSpace ) throw (lang::IllegalArgumentException,
446 : uno::RuntimeException, std::exception) SAL_OVERRIDE
447 : {
448 : // TODO(P3): if we know anything about target
449 : // colorspace, this can be greatly sped up
450 : uno::Sequence<rendering::ARGBColor> aIntermediate(
451 0 : convertToARGB(deviceColor));
452 0 : return targetColorSpace->convertFromARGB(aIntermediate);
453 : }
454 0 : virtual uno::Sequence< rendering::RGBColor > SAL_CALL convertToRGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) SAL_OVERRIDE
455 : {
456 0 : const double* pIn( deviceColor.getConstArray() );
457 0 : const sal_Size nLen( deviceColor.getLength() );
458 0 : ENSURE_ARG_OR_THROW2(nLen%4==0,
459 : "number of channels no multiple of 4",
460 : static_cast<rendering::XColorSpace*>(this), 0);
461 :
462 0 : uno::Sequence< rendering::RGBColor > aRes(nLen/4);
463 0 : rendering::RGBColor* pOut( aRes.getArray() );
464 0 : for( sal_Size i=0; i<nLen; i+=4 )
465 : {
466 0 : *pOut++ = rendering::RGBColor(pIn[0],pIn[1],pIn[2]);
467 0 : pIn += 4;
468 : }
469 0 : return aRes;
470 : }
471 0 : virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToARGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) SAL_OVERRIDE
472 : {
473 0 : const double* pIn( deviceColor.getConstArray() );
474 0 : const sal_Size nLen( deviceColor.getLength() );
475 0 : ENSURE_ARG_OR_THROW2(nLen%4==0,
476 : "number of channels no multiple of 4",
477 : static_cast<rendering::XColorSpace*>(this), 0);
478 :
479 0 : uno::Sequence< rendering::ARGBColor > aRes(nLen/4);
480 0 : rendering::ARGBColor* pOut( aRes.getArray() );
481 0 : for( sal_Size i=0; i<nLen; i+=4 )
482 : {
483 0 : *pOut++ = rendering::ARGBColor(pIn[3],pIn[0],pIn[1],pIn[2]);
484 0 : pIn += 4;
485 : }
486 0 : return aRes;
487 : }
488 0 : virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToPARGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) SAL_OVERRIDE
489 : {
490 0 : const double* pIn( deviceColor.getConstArray() );
491 0 : const sal_Size nLen( deviceColor.getLength() );
492 0 : ENSURE_ARG_OR_THROW2(nLen%4==0,
493 : "number of channels no multiple of 4",
494 : static_cast<rendering::XColorSpace*>(this), 0);
495 :
496 0 : uno::Sequence< rendering::ARGBColor > aRes(nLen/4);
497 0 : rendering::ARGBColor* pOut( aRes.getArray() );
498 0 : for( sal_Size i=0; i<nLen; i+=4 )
499 : {
500 0 : *pOut++ = rendering::ARGBColor(pIn[3],pIn[3]*pIn[0],pIn[3]*pIn[1],pIn[3]*pIn[2]);
501 0 : pIn += 4;
502 : }
503 0 : return aRes;
504 : }
505 0 : virtual uno::Sequence< double > SAL_CALL convertFromRGB( const uno::Sequence< rendering::RGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) SAL_OVERRIDE
506 : {
507 0 : const rendering::RGBColor* pIn( rgbColor.getConstArray() );
508 0 : const sal_Size nLen( rgbColor.getLength() );
509 :
510 0 : uno::Sequence< double > aRes(nLen*4);
511 0 : double* pColors=aRes.getArray();
512 0 : for( sal_Size i=0; i<nLen; ++i )
513 : {
514 0 : *pColors++ = pIn->Red;
515 0 : *pColors++ = pIn->Green;
516 0 : *pColors++ = pIn->Blue;
517 0 : *pColors++ = 1.0;
518 0 : ++pIn;
519 : }
520 0 : return aRes;
521 : }
522 0 : virtual uno::Sequence< double > SAL_CALL convertFromARGB( const uno::Sequence< rendering::ARGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) SAL_OVERRIDE
523 : {
524 0 : const rendering::ARGBColor* pIn( rgbColor.getConstArray() );
525 0 : const sal_Size nLen( rgbColor.getLength() );
526 :
527 0 : uno::Sequence< double > aRes(nLen*4);
528 0 : double* pColors=aRes.getArray();
529 0 : for( sal_Size i=0; i<nLen; ++i )
530 : {
531 0 : *pColors++ = pIn->Red;
532 0 : *pColors++ = pIn->Green;
533 0 : *pColors++ = pIn->Blue;
534 0 : *pColors++ = pIn->Alpha;
535 0 : ++pIn;
536 : }
537 0 : return aRes;
538 : }
539 0 : virtual uno::Sequence< double > SAL_CALL convertFromPARGB( const uno::Sequence< rendering::ARGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) SAL_OVERRIDE
540 : {
541 0 : const rendering::ARGBColor* pIn( rgbColor.getConstArray() );
542 0 : const sal_Size nLen( rgbColor.getLength() );
543 :
544 0 : uno::Sequence< double > aRes(nLen*4);
545 0 : double* pColors=aRes.getArray();
546 0 : for( sal_Size i=0; i<nLen; ++i )
547 : {
548 0 : *pColors++ = pIn->Red/pIn->Alpha;
549 0 : *pColors++ = pIn->Green/pIn->Alpha;
550 0 : *pColors++ = pIn->Blue/pIn->Alpha;
551 0 : *pColors++ = pIn->Alpha;
552 0 : ++pIn;
553 : }
554 0 : return aRes;
555 : }
556 :
557 : public:
558 0 : StandardColorSpace() : m_aComponentTags(4)
559 : {
560 0 : sal_Int8* pTags = m_aComponentTags.getArray();
561 0 : pTags[0] = rendering::ColorComponentTag::RGB_RED;
562 0 : pTags[1] = rendering::ColorComponentTag::RGB_GREEN;
563 0 : pTags[2] = rendering::ColorComponentTag::RGB_BLUE;
564 0 : pTags[3] = rendering::ColorComponentTag::ALPHA;
565 0 : }
566 : };
567 : }
568 :
569 0 : uno::Reference<rendering::XColorSpace> VCL_DLLPUBLIC createStandardColorSpace()
570 : {
571 0 : return new StandardColorSpace();
572 : }
573 :
574 0 : uno::Sequence< double > colorToStdColorSpaceSequence( const Color& rColor )
575 : {
576 0 : uno::Sequence< double > aRet(4);
577 0 : double* pRet = aRet.getArray();
578 :
579 0 : pRet[0] = toDoubleColor(rColor.GetRed());
580 0 : pRet[1] = toDoubleColor(rColor.GetGreen());
581 0 : pRet[2] = toDoubleColor(rColor.GetBlue());
582 :
583 : // VCL's notion of alpha is different from the rest of the world's
584 0 : pRet[3] = 1.0 - toDoubleColor(rColor.GetTransparency());
585 :
586 0 : return aRet;
587 : }
588 :
589 0 : Color stdColorSpaceSequenceToColor( const uno::Sequence< double >& rColor )
590 : {
591 0 : ENSURE_ARG_OR_THROW( rColor.getLength() == 4,
592 : "color must have 4 channels" );
593 :
594 0 : Color aColor;
595 :
596 0 : aColor.SetRed ( toByteColor(rColor[0]) );
597 0 : aColor.SetGreen( toByteColor(rColor[1]) );
598 0 : aColor.SetBlue ( toByteColor(rColor[2]) );
599 : // VCL's notion of alpha is different from the rest of the world's
600 0 : aColor.SetTransparency( 255 - toByteColor(rColor[3]) );
601 :
602 0 : return aColor;
603 : }
604 :
605 0 : uno::Sequence< double > VCL_DLLPUBLIC colorToDoubleSequence(
606 : const Color& rColor,
607 : const uno::Reference< rendering::XColorSpace >& xColorSpace )
608 : {
609 0 : uno::Sequence<rendering::ARGBColor> aSeq(1);
610 0 : aSeq[0] = rendering::ARGBColor(
611 0 : 1.0-toDoubleColor(rColor.GetTransparency()),
612 0 : toDoubleColor(rColor.GetRed()),
613 0 : toDoubleColor(rColor.GetGreen()),
614 0 : toDoubleColor(rColor.GetBlue()) );
615 :
616 0 : return xColorSpace->convertFromARGB(aSeq);
617 : }
618 :
619 0 : Color VCL_DLLPUBLIC doubleSequenceToColor(
620 : const uno::Sequence< double > rColor,
621 : const uno::Reference< rendering::XColorSpace >& xColorSpace )
622 : {
623 : const rendering::ARGBColor aARGBColor(
624 0 : xColorSpace->convertToARGB(rColor)[0]);
625 :
626 0 : return Color( 255-toByteColor(aARGBColor.Alpha),
627 0 : toByteColor(aARGBColor.Red),
628 0 : toByteColor(aARGBColor.Green),
629 0 : toByteColor(aARGBColor.Blue) );
630 : }
631 :
632 : } // namespace vcltools
633 :
634 : } // namespace canvas
635 :
636 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|