LCOV - code coverage report
Current view: top level - vcl/source/helper - canvastools.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 167 280 59.6 %
Date: 2015-06-13 12:38:46 Functions: 18 37 48.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11