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 0 : 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 0 : 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 : rLHS.ScanLineBytes == rRHS.ScanLineBytes &&
88 : rLHS.ScanLineStride == rRHS.ScanLineStride &&
89 : 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->SetPixel( 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->SetPixel( 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 0 : ::BitmapEx VCL_DLLPUBLIC bitmapExFromXBitmap( const uno::Reference< rendering::XIntegerReadOnlyBitmap >& xInputBitmap )
191 : {
192 : RTL_LOGFILE_CONTEXT( aLog, "::vcl::unotools::bitmapExFromXBitmap()" );
193 :
194 0 : if( !xInputBitmap.is() )
195 0 : return ::BitmapEx();
196 :
197 : // tunnel directly for known implementation
198 : // ----------------------------------------------------------------
199 0 : VclCanvasBitmap* pImplBitmap = dynamic_cast<VclCanvasBitmap*>(xInputBitmap.get());
200 0 : if( pImplBitmap )
201 0 : return pImplBitmap->getBitmapEx();
202 :
203 : // retrieve data via UNO interface
204 : // ----------------------------------------------------------------
205 :
206 : // volatile bitmaps are a bit more complicated to read
207 : // from..
208 : uno::Reference<rendering::XVolatileBitmap> xVolatileBitmap(
209 0 : xInputBitmap, uno::UNO_QUERY);
210 :
211 : // loop a few times, until successfully read (for XVolatileBitmap)
212 0 : for( int i=0; i<10; ++i )
213 : {
214 0 : sal_Int32 nDepth=0;
215 0 : sal_Int32 nAlphaDepth=0;
216 : const rendering::IntegerBitmapLayout aLayout(
217 0 : xInputBitmap->getMemoryLayout());
218 :
219 : OSL_ENSURE(aLayout.ColorSpace.is(),
220 : "Cannot convert image without color space!");
221 0 : if( !aLayout.ColorSpace.is() )
222 0 : return ::BitmapEx();
223 :
224 0 : nDepth = aLayout.ColorSpace->getBitsPerPixel();
225 :
226 0 : if( xInputBitmap->hasAlpha() )
227 : {
228 : // determine alpha channel depth
229 : const uno::Sequence<sal_Int8> aTags(
230 0 : aLayout.ColorSpace->getComponentTags() );
231 : const uno::Sequence<sal_Int32> aDepths(
232 0 : aLayout.ColorSpace->getComponentBitCounts() );
233 0 : const sal_Int8* pStart(aTags.getConstArray());
234 0 : const sal_Size nLen(aTags.getLength());
235 0 : const sal_Int8* pEnd(pStart+nLen);
236 :
237 : const std::ptrdiff_t nAlphaIndex =
238 : std::find(pStart,pEnd,
239 0 : rendering::ColorComponentTag::ALPHA) - pStart;
240 :
241 0 : if( nAlphaIndex < sal::static_int_cast<std::ptrdiff_t>(nLen) )
242 : {
243 0 : nAlphaDepth = aLayout.ColorSpace->getComponentBitCounts()[nAlphaIndex] > 1 ? 8 : 1;
244 0 : nDepth -= nAlphaDepth;
245 0 : }
246 : }
247 :
248 0 : BitmapPalette aPalette;
249 0 : if( aLayout.Palette.is() )
250 : {
251 : uno::Reference< rendering::XColorSpace > xPaletteColorSpace(
252 0 : aLayout.Palette->getColorSpace());
253 0 : ENSURE_OR_THROW(xPaletteColorSpace.is(),
254 : "Palette without color space");
255 :
256 0 : const sal_Int32 nEntryCount( aLayout.Palette->getNumberOfEntries() );
257 0 : if( nEntryCount <= 256 )
258 : {
259 0 : if( nEntryCount <= 2 )
260 0 : nDepth = 1;
261 : else
262 0 : nDepth = 8;
263 :
264 : const sal_uInt16 nPaletteEntries(
265 : sal::static_int_cast<sal_uInt16>(
266 0 : std::min(sal_Int32(255), nEntryCount)));
267 :
268 : // copy palette entries
269 0 : aPalette.SetEntryCount(nPaletteEntries);
270 0 : uno::Reference<rendering::XBitmapPalette> xPalette( aLayout.Palette );
271 0 : uno::Reference<rendering::XColorSpace> xPalColorSpace( xPalette->getColorSpace() );
272 :
273 0 : uno::Sequence<double> aPaletteEntry;
274 0 : for( sal_uInt16 j=0; j<nPaletteEntries; ++j )
275 : {
276 0 : if( !xPalette->getIndex(aPaletteEntry,j) &&
277 : nAlphaDepth == 0 )
278 : {
279 0 : nAlphaDepth = 1;
280 : }
281 0 : uno::Sequence<rendering::RGBColor> aColors=xPalColorSpace->convertToRGB(aPaletteEntry);
282 0 : ENSURE_OR_THROW(aColors.getLength() == 1,
283 : "Palette returned more or less than one entry");
284 0 : const rendering::RGBColor& rColor=aColors[0];
285 0 : aPalette[j] = BitmapColor(toByteColor(rColor.Red),
286 0 : toByteColor(rColor.Green),
287 0 : toByteColor(rColor.Blue));
288 0 : }
289 0 : }
290 : }
291 :
292 : const ::Size aPixelSize(
293 0 : sizeFromIntegerSize2D(xInputBitmap->getSize()));
294 :
295 : // normalize bitcount
296 : nDepth =
297 : ( nDepth <= 1 ) ? 1 :
298 : ( nDepth <= 4 ) ? 4 :
299 0 : ( nDepth <= 8 ) ? 8 : 24;
300 :
301 : ::Bitmap aBitmap( aPixelSize,
302 0 : sal::static_int_cast<sal_uInt16>(nDepth),
303 0 : aLayout.Palette.is() ? &aPalette : NULL );
304 0 : ::Bitmap aAlpha;
305 0 : if( nAlphaDepth )
306 : aAlpha = ::Bitmap( aPixelSize,
307 0 : sal::static_int_cast<sal_uInt16>(nAlphaDepth),
308 : &::Bitmap::GetGreyPalette(
309 0 : sal::static_int_cast<sal_uInt16>(1L << nAlphaDepth)) );
310 :
311 : { // limit scoped access
312 0 : Bitmap::ScopedWriteAccess pWriteAccess( aBitmap );
313 : Bitmap::ScopedWriteAccess pAlphaWriteAccess( nAlphaDepth ? aAlpha.AcquireWriteAccess() : NULL,
314 0 : aAlpha );
315 :
316 0 : ENSURE_OR_THROW(pWriteAccess.get() != NULL,
317 : "Cannot get write access to bitmap");
318 :
319 0 : const sal_Int32 nWidth(aPixelSize.Width());
320 0 : const sal_Int32 nHeight(aPixelSize.Height());
321 :
322 0 : if( !readBmp(nWidth,nHeight,aLayout,xInputBitmap,
323 0 : pWriteAccess,pAlphaWriteAccess) )
324 0 : continue;
325 : } // limit scoped access
326 :
327 0 : if( nAlphaDepth )
328 : return ::BitmapEx( aBitmap,
329 0 : AlphaMask( aAlpha ) );
330 : else
331 0 : return ::BitmapEx( aBitmap );
332 0 : }
333 :
334 : // failed to read data 10 times - bail out
335 0 : return ::BitmapEx();
336 : }
337 :
338 : //---------------------------------------------------------------------------------------
339 :
340 0 : geometry::RealSize2D size2DFromSize( const Size& rSize )
341 : {
342 0 : return geometry::RealSize2D( rSize.Width(),
343 0 : rSize.Height() );
344 : }
345 :
346 0 : Size sizeFromRealSize2D( const geometry::RealSize2D& rSize )
347 : {
348 : return Size( static_cast<long>(rSize.Width + .5),
349 0 : static_cast<long>(rSize.Height + .5) );
350 : }
351 :
352 0 : ::Size sizeFromB2DSize( const ::basegfx::B2DVector& rVec )
353 : {
354 : return ::Size( FRound( rVec.getX() ),
355 0 : FRound( rVec.getY() ) );
356 : }
357 :
358 0 : ::Point pointFromB2DPoint( const ::basegfx::B2DPoint& rPoint )
359 : {
360 : return ::Point( FRound( rPoint.getX() ),
361 0 : FRound( rPoint.getY() ) );
362 : }
363 :
364 8 : ::Rectangle rectangleFromB2DRectangle( const ::basegfx::B2DRange& rRect )
365 : {
366 : return ::Rectangle( FRound( rRect.getMinX() ),
367 : FRound( rRect.getMinY() ),
368 : FRound( rRect.getMaxX() ),
369 8 : FRound( rRect.getMaxY() ) );
370 : }
371 :
372 0 : Point pointFromB2IPoint( const ::basegfx::B2IPoint& rPoint )
373 : {
374 : return ::Point( rPoint.getX(),
375 0 : rPoint.getY() );
376 : }
377 :
378 0 : Rectangle rectangleFromB2IRectangle( const ::basegfx::B2IRange& rRect )
379 : {
380 : return ::Rectangle( rRect.getMinX(),
381 : rRect.getMinY(),
382 : rRect.getMaxX(),
383 0 : rRect.getMaxY() );
384 : }
385 :
386 0 : ::basegfx::B2DVector b2DSizeFromSize( const ::Size& rSize )
387 : {
388 0 : return ::basegfx::B2DVector( rSize.Width(),
389 0 : rSize.Height() );
390 : }
391 :
392 0 : ::basegfx::B2DPoint b2DPointFromPoint( const ::Point& rPoint )
393 : {
394 0 : return ::basegfx::B2DPoint( rPoint.X(),
395 0 : rPoint.Y() );
396 : }
397 :
398 3 : ::basegfx::B2DRange b2DRectangleFromRectangle( const ::Rectangle& rRect )
399 : {
400 3 : return ::basegfx::B2DRange( rRect.Left(),
401 3 : rRect.Top(),
402 3 : rRect.Right(),
403 12 : rRect.Bottom() );
404 : }
405 :
406 0 : geometry::IntegerSize2D integerSize2DFromSize( const Size& rSize )
407 : {
408 0 : return geometry::IntegerSize2D( rSize.Width(),
409 0 : rSize.Height() );
410 : }
411 :
412 0 : Size sizeFromIntegerSize2D( const geometry::IntegerSize2D& rSize )
413 : {
414 : return Size( rSize.Width,
415 0 : rSize.Height );
416 : }
417 :
418 0 : Point pointFromIntegerPoint2D( const geometry::IntegerPoint2D& rPoint )
419 : {
420 : return Point( rPoint.X,
421 0 : rPoint.Y );
422 : }
423 :
424 0 : Rectangle rectangleFromIntegerRectangle2D( const geometry::IntegerRectangle2D& rRectangle )
425 : {
426 : return Rectangle( rRectangle.X1, rRectangle.Y1,
427 0 : rRectangle.X2, rRectangle.Y2 );
428 : }
429 :
430 : namespace
431 : {
432 0 : class StandardColorSpace : public cppu::WeakImplHelper1< com::sun::star::rendering::XColorSpace >
433 : {
434 : private:
435 : uno::Sequence< sal_Int8 > m_aComponentTags;
436 :
437 0 : virtual ::sal_Int8 SAL_CALL getType( ) throw (uno::RuntimeException)
438 : {
439 0 : return rendering::ColorSpaceType::RGB;
440 : }
441 0 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL getComponentTags( ) throw (uno::RuntimeException)
442 : {
443 0 : return m_aComponentTags;
444 : }
445 0 : virtual ::sal_Int8 SAL_CALL getRenderingIntent( ) throw (uno::RuntimeException)
446 : {
447 0 : return rendering::RenderingIntent::PERCEPTUAL;
448 : }
449 0 : virtual uno::Sequence< beans::PropertyValue > SAL_CALL getProperties( ) throw (uno::RuntimeException)
450 : {
451 0 : return uno::Sequence< beans::PropertyValue >();
452 : }
453 0 : virtual uno::Sequence< double > SAL_CALL convertColorSpace( const uno::Sequence< double >& deviceColor,
454 : const uno::Reference< rendering::XColorSpace >& targetColorSpace ) throw (lang::IllegalArgumentException,
455 : uno::RuntimeException)
456 : {
457 : // TODO(P3): if we know anything about target
458 : // colorspace, this can be greatly sped up
459 : uno::Sequence<rendering::ARGBColor> aIntermediate(
460 0 : convertToARGB(deviceColor));
461 0 : return targetColorSpace->convertFromARGB(aIntermediate);
462 : }
463 0 : virtual uno::Sequence< rendering::RGBColor > SAL_CALL convertToRGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
464 : {
465 0 : const double* pIn( deviceColor.getConstArray() );
466 0 : const sal_Size nLen( deviceColor.getLength() );
467 0 : ENSURE_ARG_OR_THROW2(nLen%4==0,
468 : "number of channels no multiple of 4",
469 : static_cast<rendering::XColorSpace*>(this), 0);
470 :
471 0 : uno::Sequence< rendering::RGBColor > aRes(nLen/4);
472 0 : rendering::RGBColor* pOut( aRes.getArray() );
473 0 : for( sal_Size i=0; i<nLen; i+=4 )
474 : {
475 0 : *pOut++ = rendering::RGBColor(pIn[0],pIn[1],pIn[2]);
476 0 : pIn += 4;
477 : }
478 0 : return aRes;
479 : }
480 0 : virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToARGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
481 : {
482 0 : const double* pIn( deviceColor.getConstArray() );
483 0 : const sal_Size nLen( deviceColor.getLength() );
484 0 : ENSURE_ARG_OR_THROW2(nLen%4==0,
485 : "number of channels no multiple of 4",
486 : static_cast<rendering::XColorSpace*>(this), 0);
487 :
488 0 : uno::Sequence< rendering::ARGBColor > aRes(nLen/4);
489 0 : rendering::ARGBColor* pOut( aRes.getArray() );
490 0 : for( sal_Size i=0; i<nLen; i+=4 )
491 : {
492 0 : *pOut++ = rendering::ARGBColor(pIn[3],pIn[0],pIn[1],pIn[2]);
493 0 : pIn += 4;
494 : }
495 0 : return aRes;
496 : }
497 0 : virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToPARGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
498 : {
499 0 : const double* pIn( deviceColor.getConstArray() );
500 0 : const sal_Size nLen( deviceColor.getLength() );
501 0 : ENSURE_ARG_OR_THROW2(nLen%4==0,
502 : "number of channels no multiple of 4",
503 : static_cast<rendering::XColorSpace*>(this), 0);
504 :
505 0 : uno::Sequence< rendering::ARGBColor > aRes(nLen/4);
506 0 : rendering::ARGBColor* pOut( aRes.getArray() );
507 0 : for( sal_Size i=0; i<nLen; i+=4 )
508 : {
509 0 : *pOut++ = rendering::ARGBColor(pIn[3],pIn[3]*pIn[0],pIn[3]*pIn[1],pIn[3]*pIn[2]);
510 0 : pIn += 4;
511 : }
512 0 : return aRes;
513 : }
514 0 : virtual uno::Sequence< double > SAL_CALL convertFromRGB( const uno::Sequence< rendering::RGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
515 : {
516 0 : const rendering::RGBColor* pIn( rgbColor.getConstArray() );
517 0 : const sal_Size nLen( rgbColor.getLength() );
518 :
519 0 : uno::Sequence< double > aRes(nLen*4);
520 0 : double* pColors=aRes.getArray();
521 0 : for( sal_Size i=0; i<nLen; ++i )
522 : {
523 0 : *pColors++ = pIn->Red;
524 0 : *pColors++ = pIn->Green;
525 0 : *pColors++ = pIn->Blue;
526 0 : *pColors++ = 1.0;
527 0 : ++pIn;
528 : }
529 0 : return aRes;
530 : }
531 0 : virtual uno::Sequence< double > SAL_CALL convertFromARGB( const uno::Sequence< rendering::ARGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
532 : {
533 0 : const rendering::ARGBColor* pIn( rgbColor.getConstArray() );
534 0 : const sal_Size nLen( rgbColor.getLength() );
535 :
536 0 : uno::Sequence< double > aRes(nLen*4);
537 0 : double* pColors=aRes.getArray();
538 0 : for( sal_Size i=0; i<nLen; ++i )
539 : {
540 0 : *pColors++ = pIn->Red;
541 0 : *pColors++ = pIn->Green;
542 0 : *pColors++ = pIn->Blue;
543 0 : *pColors++ = pIn->Alpha;
544 0 : ++pIn;
545 : }
546 0 : return aRes;
547 : }
548 0 : virtual uno::Sequence< double > SAL_CALL convertFromPARGB( const uno::Sequence< rendering::ARGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
549 : {
550 0 : const rendering::ARGBColor* pIn( rgbColor.getConstArray() );
551 0 : const sal_Size nLen( rgbColor.getLength() );
552 :
553 0 : uno::Sequence< double > aRes(nLen*4);
554 0 : double* pColors=aRes.getArray();
555 0 : for( sal_Size i=0; i<nLen; ++i )
556 : {
557 0 : *pColors++ = pIn->Red/pIn->Alpha;
558 0 : *pColors++ = pIn->Green/pIn->Alpha;
559 0 : *pColors++ = pIn->Blue/pIn->Alpha;
560 0 : *pColors++ = pIn->Alpha;
561 0 : ++pIn;
562 : }
563 0 : return aRes;
564 : }
565 :
566 : public:
567 0 : StandardColorSpace() : m_aComponentTags(4)
568 : {
569 0 : sal_Int8* pTags = m_aComponentTags.getArray();
570 0 : pTags[0] = rendering::ColorComponentTag::RGB_RED;
571 0 : pTags[1] = rendering::ColorComponentTag::RGB_GREEN;
572 0 : pTags[2] = rendering::ColorComponentTag::RGB_BLUE;
573 0 : pTags[3] = rendering::ColorComponentTag::ALPHA;
574 0 : }
575 : };
576 : }
577 :
578 0 : uno::Reference<rendering::XColorSpace> VCL_DLLPUBLIC createStandardColorSpace()
579 : {
580 0 : return new StandardColorSpace();
581 : }
582 :
583 : //---------------------------------------------------------------------------------------
584 :
585 0 : Color stdColorSpaceSequenceToColor( const uno::Sequence< double >& rColor )
586 : {
587 0 : ENSURE_ARG_OR_THROW( rColor.getLength() == 4,
588 : "color must have 4 channels" );
589 :
590 0 : Color aColor;
591 :
592 0 : aColor.SetRed ( toByteColor(rColor[0]) );
593 0 : aColor.SetGreen( toByteColor(rColor[1]) );
594 0 : aColor.SetBlue ( toByteColor(rColor[2]) );
595 : // VCL's notion of alpha is different from the rest of the world's
596 0 : aColor.SetTransparency( 255 - toByteColor(rColor[3]) );
597 :
598 0 : return aColor;
599 : }
600 :
601 0 : uno::Sequence< double > VCL_DLLPUBLIC colorToDoubleSequence(
602 : const Color& rColor,
603 : const uno::Reference< rendering::XColorSpace >& xColorSpace )
604 : {
605 0 : uno::Sequence<rendering::ARGBColor> aSeq(1);
606 0 : aSeq[0] = rendering::ARGBColor(
607 0 : 1.0-toDoubleColor(rColor.GetTransparency()),
608 0 : toDoubleColor(rColor.GetRed()),
609 0 : toDoubleColor(rColor.GetGreen()),
610 0 : toDoubleColor(rColor.GetBlue()) );
611 :
612 0 : return xColorSpace->convertFromARGB(aSeq);
613 : }
614 :
615 0 : Color VCL_DLLPUBLIC doubleSequenceToColor(
616 : const uno::Sequence< double > rColor,
617 : const uno::Reference< rendering::XColorSpace >& xColorSpace )
618 : {
619 : const rendering::ARGBColor aARGBColor(
620 0 : xColorSpace->convertToARGB(rColor)[0]);
621 :
622 0 : return Color( 255-toByteColor(aARGBColor.Alpha),
623 0 : toByteColor(aARGBColor.Red),
624 0 : toByteColor(aARGBColor.Green),
625 0 : toByteColor(aARGBColor.Blue) );
626 : }
627 :
628 : //---------------------------------------------------------------------------------------
629 :
630 : } // namespace vcltools
631 :
632 : } // namespace canvas
633 :
634 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|