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