LCOV - code coverage report
Current view: top level - vcl/source/gdi - gfxlink.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 155 192 80.7 %
Date: 2015-06-13 12:38:46 Functions: 20 21 95.2 %
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 <osl/file.h>
      21             : #include <tools/vcompat.hxx>
      22             : #include <tools/debug.hxx>
      23             : #include <unotools/ucbstreamhelper.hxx>
      24             : #include <unotools/tempfile.hxx>
      25             : #include <ucbhelper/content.hxx>
      26             : #include <vcl/graph.hxx>
      27             : #include <vcl/gfxlink.hxx>
      28             : #include <vcl/cvtgrf.hxx>
      29             : #include <com/sun/star/ucb/CommandAbortedException.hpp>
      30             : #include <memory>
      31             : 
      32       29789 : GfxLink::GfxLink() :
      33             :     meType      ( GFX_LINK_TYPE_NONE ),
      34             :     mpBuf       ( NULL ),
      35             :     mpSwap      ( NULL ),
      36             :     mnBufSize   ( 0 ),
      37             :     mnUserId    ( 0UL ),
      38       29789 :     mpImpData   ( new ImpGfxLink )
      39             : {
      40       29789 : }
      41             : 
      42       11985 : GfxLink::GfxLink( const GfxLink& rGfxLink ) :
      43       11985 :     mpImpData( new ImpGfxLink )
      44             : {
      45       11985 :     ImplCopy( rGfxLink );
      46       11985 : }
      47             : 
      48        2100 : GfxLink::GfxLink( sal_uInt8* pBuf, sal_uInt32 nSize, GfxLinkType nType, bool bOwns ) :
      49        2100 :     mpImpData( new ImpGfxLink )
      50             : {
      51             :     DBG_ASSERT( (pBuf != NULL && nSize) || (!bOwns && nSize == 0),
      52             :                 "GfxLink::GfxLink(): empty/NULL buffer given" );
      53             : 
      54        2100 :     meType = nType;
      55        2100 :     mnBufSize = nSize;
      56        2100 :     mpSwap = NULL;
      57        2100 :     mnUserId = 0UL;
      58             : 
      59        2100 :     if( bOwns )
      60        2100 :         mpBuf = new ImpBuffer( pBuf );
      61           0 :     else if( nSize )
      62             :     {
      63           0 :         mpBuf = new ImpBuffer( nSize );
      64           0 :         memcpy( mpBuf->mpBuffer, pBuf, nSize );
      65             :     }
      66             :     else
      67           0 :         mpBuf = NULL;
      68        2100 : }
      69             : 
      70       43714 : GfxLink::~GfxLink()
      71             : {
      72       43714 :     if( mpBuf && !( --mpBuf->mnRefCount ) )
      73        2395 :         delete mpBuf;
      74             : 
      75       43714 :     if( mpSwap && !( --mpSwap->mnRefCount ) )
      76        2047 :         delete mpSwap;
      77             : 
      78       43714 :     delete mpImpData;
      79       43714 : }
      80             : 
      81       15470 : GfxLink& GfxLink::operator=( const GfxLink& rGfxLink )
      82             : {
      83       15470 :     if( &rGfxLink != this )
      84             :     {
      85       15470 :         if ( mpBuf && !( --mpBuf->mnRefCount ) )
      86           0 :             delete mpBuf;
      87             : 
      88       15470 :         if( mpSwap && !( --mpSwap->mnRefCount ) )
      89          38 :             delete mpSwap;
      90             : 
      91       15470 :         ImplCopy( rGfxLink );
      92             :     }
      93             : 
      94       15470 :     return *this;
      95             : }
      96             : 
      97           0 : bool GfxLink::IsEqual( const GfxLink& rGfxLink ) const
      98             : {
      99           0 :     bool bIsEqual = false;
     100             : 
     101           0 :     if ( ( mnBufSize == rGfxLink.mnBufSize ) && ( meType == rGfxLink.meType ) )
     102             :     {
     103           0 :         const sal_uInt8* pSource = GetData();
     104           0 :         const sal_uInt8* pDest = rGfxLink.GetData();
     105           0 :         sal_uInt32 nSourceSize = GetDataSize();
     106           0 :         sal_uInt32 nDestSize = rGfxLink.GetDataSize();
     107           0 :         if ( pSource && pDest && ( nSourceSize == nDestSize ) )
     108             :         {
     109           0 :             bIsEqual = memcmp( pSource, pDest, nSourceSize ) == 0;
     110             :         }
     111           0 :         else if ( ( pSource == 0 ) && ( pDest == 0 ) )
     112           0 :             bIsEqual = true;
     113             :     }
     114           0 :     return bIsEqual;
     115             : }
     116             : 
     117       27455 : void GfxLink::ImplCopy( const GfxLink& rGfxLink )
     118             : {
     119       27455 :     mnBufSize = rGfxLink.mnBufSize;
     120       27455 :     meType = rGfxLink.meType;
     121       27455 :     mpBuf = rGfxLink.mpBuf;
     122       27455 :     mpSwap = rGfxLink.mpSwap;
     123       27455 :     mnUserId = rGfxLink.mnUserId;
     124       27455 :     *mpImpData = *rGfxLink.mpImpData;
     125             : 
     126       27455 :     if( mpBuf )
     127        2208 :         mpBuf->mnRefCount++;
     128             : 
     129       27455 :     if( mpSwap )
     130       11007 :         mpSwap->mnRefCount++;
     131       27455 : }
     132             : 
     133             : 
     134        8375 : bool GfxLink::IsNative() const
     135             : {
     136        8375 :     return( meType >= GFX_LINK_FIRST_NATIVE_ID && meType <= GFX_LINK_LAST_NATIVE_ID );
     137             : }
     138             : 
     139             : 
     140         529 : const sal_uInt8* GfxLink::GetData() const
     141             : {
     142         529 :     if( IsSwappedOut() )
     143         295 :         const_cast<GfxLink*>(this)->SwapIn();
     144             : 
     145         529 :     return( mpBuf ? mpBuf->mpBuffer : NULL );
     146             : }
     147             : 
     148             : 
     149         292 : void GfxLink::SetPrefSize( const Size& rPrefSize )
     150             : {
     151         292 :     mpImpData->maPrefSize = rPrefSize;
     152         292 :     mpImpData->mbPrefSizeValid = true;
     153         292 : }
     154             : 
     155             : 
     156             : 
     157         292 : void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
     158             : {
     159         292 :     mpImpData->maPrefMapMode = rPrefMapMode;
     160         292 :     mpImpData->mbPrefMapModeValid = true;
     161         292 : }
     162             : 
     163             : 
     164         108 : bool GfxLink::LoadNative( Graphic& rGraphic )
     165             : {
     166         108 :     bool bRet = false;
     167             : 
     168         108 :     if( IsNative() && mnBufSize )
     169             :     {
     170         108 :         const sal_uInt8* pData = GetData();
     171             : 
     172         108 :         if( pData )
     173             :         {
     174         108 :             SvMemoryStream    aMemStm;
     175             :             ConvertDataFormat nCvtType;
     176             : 
     177         108 :             aMemStm.SetBuffer( const_cast<sal_uInt8*>(pData), mnBufSize, false, mnBufSize );
     178             : 
     179         108 :             switch( meType )
     180             :             {
     181           0 :                 case( GFX_LINK_TYPE_NATIVE_GIF ): nCvtType = ConvertDataFormat::GIF; break;
     182             : 
     183             :                 // #i15508# added BMP type for better exports (reload when swapped - checked, works)
     184           0 :                 case( GFX_LINK_TYPE_NATIVE_BMP ): nCvtType = ConvertDataFormat::BMP; break;
     185             : 
     186         107 :                 case( GFX_LINK_TYPE_NATIVE_JPG ): nCvtType = ConvertDataFormat::JPG; break;
     187           0 :                 case( GFX_LINK_TYPE_NATIVE_PNG ): nCvtType = ConvertDataFormat::PNG; break;
     188           0 :                 case( GFX_LINK_TYPE_NATIVE_TIF ): nCvtType = ConvertDataFormat::TIF; break;
     189           0 :                 case( GFX_LINK_TYPE_NATIVE_WMF ): nCvtType = ConvertDataFormat::WMF; break;
     190           0 :                 case( GFX_LINK_TYPE_NATIVE_MET ): nCvtType = ConvertDataFormat::MET; break;
     191           0 :                 case( GFX_LINK_TYPE_NATIVE_PCT ): nCvtType = ConvertDataFormat::PCT; break;
     192           1 :                 case( GFX_LINK_TYPE_NATIVE_SVG ): nCvtType = ConvertDataFormat::SVG; break;
     193             : 
     194           0 :                 default: nCvtType = ConvertDataFormat::Unknown; break;
     195             :             }
     196             : 
     197         108 :             if( nCvtType != ConvertDataFormat::Unknown && ( GraphicConverter::Import( aMemStm, rGraphic, nCvtType ) == ERRCODE_NONE ) )
     198         108 :                 bRet = true;
     199             :         }
     200             :     }
     201             : 
     202         108 :     return bRet;
     203             : }
     204             : 
     205        7948 : void GfxLink::SwapOut()
     206             : {
     207        7948 :     if( !IsSwappedOut() && mpBuf )
     208             :     {
     209        2098 :         mpSwap = new ImpSwap( mpBuf->mpBuffer, mnBufSize );
     210             : 
     211        2098 :         if( !mpSwap->IsSwapped() )
     212             :         {
     213           9 :             delete mpSwap;
     214           9 :             mpSwap = NULL;
     215             :         }
     216             :         else
     217             :         {
     218        2089 :             if( !( --mpBuf->mnRefCount ) )
     219           0 :                 delete mpBuf;
     220             : 
     221        2089 :             mpBuf = NULL;
     222             :         }
     223             :     }
     224        7948 : }
     225             : 
     226         295 : void GfxLink::SwapIn()
     227             : {
     228         295 :     if( IsSwappedOut() )
     229             :     {
     230         295 :         mpBuf = new ImpBuffer( mpSwap->GetData() );
     231             : 
     232         295 :         if( !( --mpSwap->mnRefCount ) )
     233           0 :             delete mpSwap;
     234             : 
     235         295 :         mpSwap = NULL;
     236             :     }
     237         295 : }
     238             : 
     239          21 : bool GfxLink::ExportNative( SvStream& rOStream ) const
     240             : {
     241          21 :     if( GetDataSize() )
     242             :     {
     243          21 :         if( IsSwappedOut() )
     244          21 :             mpSwap->WriteTo( rOStream );
     245           0 :         else if( GetData() )
     246           0 :             rOStream.Write( GetData(), GetDataSize() );
     247             :     }
     248             : 
     249          21 :     return ( rOStream.GetError() == ERRCODE_NONE );
     250             : }
     251             : 
     252         184 : SvStream& WriteGfxLink( SvStream& rOStream, const GfxLink& rGfxLink )
     253             : {
     254         184 :     VersionCompat* pCompat = new VersionCompat( rOStream, StreamMode::WRITE, 2 );
     255             : 
     256             :     // Version 1
     257         184 :     rOStream.WriteUInt16( rGfxLink.GetType() ).WriteUInt32( rGfxLink.GetDataSize() ).WriteUInt32( rGfxLink.GetUserId() );
     258             : 
     259             :     // Version 2
     260         184 :     WritePair( rOStream, rGfxLink.GetPrefSize() );
     261         184 :     WriteMapMode( rOStream, rGfxLink.GetPrefMapMode() );
     262             : 
     263         184 :     delete pCompat;
     264             : 
     265         184 :     if( rGfxLink.GetDataSize() )
     266             :     {
     267         184 :         if( rGfxLink.IsSwappedOut() )
     268         184 :             rGfxLink.mpSwap->WriteTo( rOStream );
     269           0 :         else if( rGfxLink.GetData() )
     270           0 :             rOStream.Write( rGfxLink.GetData(), rGfxLink.GetDataSize() );
     271             :     }
     272             : 
     273         184 :     return rOStream;
     274             : }
     275             : 
     276         108 : SvStream& ReadGfxLink( SvStream& rIStream, GfxLink& rGfxLink)
     277             : {
     278         108 :     Size            aSize;
     279         108 :     MapMode         aMapMode;
     280             :     sal_uInt32      nSize;
     281             :     sal_uInt32      nUserId;
     282             :     sal_uInt16          nType;
     283             :     sal_uInt8*          pBuf;
     284         108 :     bool            bMapAndSizeValid( false );
     285         108 :     VersionCompat*  pCompat = new VersionCompat( rIStream, StreamMode::READ );
     286             : 
     287             :     // Version 1
     288         108 :     rIStream.ReadUInt16( nType ).ReadUInt32( nSize ).ReadUInt32( nUserId );
     289             : 
     290         108 :     if( pCompat->GetVersion() >= 2 )
     291             :     {
     292         108 :         ReadPair( rIStream, aSize );
     293         108 :         ReadMapMode( rIStream, aMapMode );
     294         108 :         bMapAndSizeValid = true;
     295             :     }
     296             : 
     297         108 :     delete pCompat;
     298             : 
     299         108 :     pBuf = new sal_uInt8[ nSize ];
     300         108 :     rIStream.Read( pBuf, nSize );
     301             : 
     302         108 :     rGfxLink = GfxLink( pBuf, nSize, (GfxLinkType) nType, true );
     303         108 :     rGfxLink.SetUserId( nUserId );
     304             : 
     305         108 :     if( bMapAndSizeValid )
     306             :     {
     307         108 :         rGfxLink.SetPrefSize( aSize );
     308         108 :         rGfxLink.SetPrefMapMode( aMapMode );
     309             :     }
     310             : 
     311         108 :     return rIStream;
     312             : }
     313             : 
     314        2098 : ImpSwap::ImpSwap( sal_uInt8* pData, sal_uLong nDataSize ) :
     315             :             mnDataSize( nDataSize ),
     316        2098 :             mnRefCount( 1UL )
     317             : {
     318        2098 :     if( pData && mnDataSize )
     319             :     {
     320        2089 :         ::utl::TempFile aTempFile;
     321             : 
     322        2089 :         maURL = aTempFile.GetURL();
     323        2089 :         if( !maURL.isEmpty() )
     324             :         {
     325        2089 :             std::unique_ptr<SvStream> xOStm(::utl::UcbStreamHelper::CreateStream( maURL, STREAM_READWRITE | StreamMode::SHARE_DENYWRITE ));
     326        2089 :             if( xOStm )
     327             :             {
     328        2089 :                 xOStm->Write( pData, mnDataSize );
     329        2089 :                 bool bError = ( ERRCODE_NONE != xOStm->GetError() );
     330        2089 :                 xOStm.reset();
     331             : 
     332        2089 :                 if( bError )
     333             :                 {
     334           0 :                     osl_removeFile( maURL.pData );
     335           0 :                     maURL.clear();
     336             :                 }
     337        2089 :             }
     338        2089 :         }
     339             :     }
     340        2098 : }
     341             : 
     342        4188 : ImpSwap::~ImpSwap()
     343             : {
     344        2094 :     if( IsSwapped() )
     345        2085 :         osl_removeFile( maURL.pData );
     346        2094 : }
     347             : 
     348         500 : sal_uInt8* ImpSwap::GetData() const
     349             : {
     350             :     sal_uInt8* pData;
     351             : 
     352         500 :     if( IsSwapped() )
     353             :     {
     354         500 :         std::unique_ptr<SvStream> xIStm(::utl::UcbStreamHelper::CreateStream( maURL, STREAM_READWRITE ));
     355         500 :         if( xIStm )
     356             :         {
     357         500 :             pData = new sal_uInt8[ mnDataSize ];
     358         500 :             xIStm->Read( pData, mnDataSize );
     359         500 :             bool bError = ( ERRCODE_NONE != xIStm->GetError() );
     360         500 :             sal_Size nActReadSize = xIStm->Tell();
     361         500 :             if (nActReadSize != mnDataSize)
     362             :             {
     363           0 :                 bError = true;
     364             :             }
     365         500 :             xIStm.reset();
     366             : 
     367         500 :             if( bError )
     368           0 :                 delete[] pData, pData = NULL;
     369             :         }
     370             :         else
     371           0 :             pData = NULL;
     372             :     }
     373             :     else
     374           0 :         pData = NULL;
     375             : 
     376         500 :     return pData;
     377             : }
     378             : 
     379         205 : void ImpSwap::WriteTo( SvStream& rOStm ) const
     380             : {
     381         205 :     sal_uInt8* pData = GetData();
     382             : 
     383         205 :     if( pData )
     384             :     {
     385         205 :         rOStm.Write( pData, mnDataSize );
     386         205 :         delete[] pData;
     387             :     }
     388         205 : }
     389             : 
     390             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11