LCOV - code coverage report
Current view: top level - sd/source/ui/dlg - vectdlg.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 169 0.0 %
Date: 2012-08-25 Functions: 0 20 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : 
      30                 :            : #ifdef SD_DLLIMPLEMENTATION
      31                 :            : #undef SD_DLLIMPLEMENTATION
      32                 :            : #endif
      33                 :            : #include <vcl/vclenum.hxx>
      34                 :            : #include <vcl/wrkwin.hxx>
      35                 :            : 
      36                 :            : #include "strings.hrc"
      37                 :            : #include "sdresid.hxx"
      38                 :            : #include "DrawDocShell.hxx"
      39                 :            : #include "sdmod.hxx"
      40                 :            : #include "sdiocmpt.hxx"
      41                 :            : #include "vectdlg.hxx"
      42                 :            : #include "vectdlg.hrc"
      43                 :            : #include <vcl/bmpacc.hxx>
      44                 :            : #include <vcl/msgbox.hxx>
      45                 :            : #include <vcl/metaact.hxx>
      46                 :            : 
      47                 :            : // -----------
      48                 :            : // - Defines -
      49                 :            : // -----------
      50                 :            : 
      51                 :            : #define VECTORIZE_MAX_EXTENT 512
      52                 :            : 
      53                 :            : // ------------------
      54                 :            : // - SdVectorizeDlg -
      55                 :            : // ------------------
      56                 :            : 
      57                 :          0 : SdVectorizeDlg::SdVectorizeDlg(
      58                 :            :     Window* pParent, const Bitmap& rBmp, ::sd::DrawDocShell* pDocShell ) :
      59                 :            :         ModalDialog     ( pParent, SdResId( DLG_VECTORIZE ) ),
      60                 :            :         mpDocSh          ( pDocShell ),
      61                 :            :         aGrpSettings    ( this, SdResId( GRP_SETTINGS ) ),
      62                 :            :         aFtLayers       ( this, SdResId( FT_LAYERS ) ),
      63                 :            :         aNmLayers       ( this, SdResId( NM_LAYERS ) ),
      64                 :            :         aFtReduce       ( this, SdResId( FT_REDUCE ) ),
      65                 :            :         aMtReduce       ( this, SdResId( MT_REDUCE ) ),
      66                 :            :         aFtFillHoles    ( this, SdResId( FT_FILLHOLES ) ),
      67                 :            :         aMtFillHoles    ( this, SdResId( MT_FILLHOLES ) ),
      68                 :            :         aCbFillHoles    ( this, SdResId( CB_FILLHOLES ) ),
      69                 :            :         aFtOriginal     ( this, SdResId( FT_ORIGINAL ) ),
      70                 :            :         aBmpWin         ( this, SdResId( CTL_BMP ) ),
      71                 :            :         aFtVectorized   ( this, SdResId( FT_VECTORIZED ) ),
      72                 :            :         aMtfWin         ( this, SdResId( CTL_WMF ) ),
      73                 :            :         aGrpPrgs        ( this, SdResId( GRP_PRGS ) ),
      74                 :            :         aPrgs           ( this, SdResId( WND_PRGS ) ),
      75                 :            :         aBtnOK          ( this, SdResId( BTN_OK ) ),
      76                 :            :         aBtnCancel      ( this, SdResId( BTN_CANCEL ) ),
      77                 :            :         aBtnHelp        ( this, SdResId( BTN_HELP ) ),
      78                 :            :         aBtnPreview     ( this, SdResId( BTN_PREVIEW ) ),
      79                 :          0 :         aBmp            ( rBmp )
      80                 :            : {
      81                 :          0 :     FreeResource();
      82                 :            : 
      83                 :          0 :     aBtnPreview.SetClickHdl( LINK( this, SdVectorizeDlg, ClickPreviewHdl ) );
      84                 :          0 :     aBtnOK.SetClickHdl( LINK( this, SdVectorizeDlg, ClickOKHdl ) );
      85                 :          0 :     aNmLayers.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
      86                 :          0 :     aMtReduce.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
      87                 :          0 :     aMtFillHoles.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
      88                 :          0 :     aCbFillHoles.SetToggleHdl( LINK( this, SdVectorizeDlg, ToggleHdl ) );
      89                 :            : 
      90                 :            :     // disable 3D border
      91                 :          0 :     aBmpWin.SetBorderStyle(WINDOW_BORDER_MONO);
      92                 :          0 :     aMtfWin.SetBorderStyle(WINDOW_BORDER_MONO);
      93                 :            : 
      94                 :          0 :     LoadSettings();
      95                 :          0 :     InitPreviewBmp();
      96                 :          0 : }
      97                 :            : 
      98                 :            : // -----------------------------------------------------------------------------
      99                 :            : 
     100                 :          0 : SdVectorizeDlg::~SdVectorizeDlg()
     101                 :            : {
     102                 :          0 : }
     103                 :            : 
     104                 :            : // -----------------------------------------------------------------------------
     105                 :            : 
     106                 :          0 : Rectangle SdVectorizeDlg::GetRect( const Size& rDispSize, const Size& rBmpSize ) const
     107                 :            : {
     108                 :          0 :     Rectangle aRect;
     109                 :            : 
     110                 :          0 :     if( rBmpSize.Width() && rBmpSize.Height() && rDispSize.Width() && rDispSize.Height() )
     111                 :            :     {
     112                 :          0 :         Size         aBmpSize( rBmpSize );
     113                 :          0 :         const double fGrfWH = (double) aBmpSize.Width() / aBmpSize.Height();
     114                 :          0 :         const double fWinWH = (double) rDispSize.Width() / rDispSize.Height();
     115                 :            : 
     116                 :          0 :         if( fGrfWH < fWinWH )
     117                 :            :         {
     118                 :          0 :             aBmpSize.Width() = (long) ( rDispSize.Height() * fGrfWH );
     119                 :          0 :             aBmpSize.Height()= rDispSize.Height();
     120                 :            :         }
     121                 :            :         else
     122                 :            :         {
     123                 :          0 :             aBmpSize.Width() = rDispSize.Width();
     124                 :          0 :             aBmpSize.Height()= (long) ( rDispSize.Width() / fGrfWH);
     125                 :            :         }
     126                 :            : 
     127                 :          0 :         const Point aBmpPos( ( rDispSize.Width()  - aBmpSize.Width() ) >> 1,
     128                 :          0 :                              ( rDispSize.Height() - aBmpSize.Height() ) >> 1 );
     129                 :            : 
     130                 :          0 :         aRect = Rectangle( aBmpPos, aBmpSize );
     131                 :            :     }
     132                 :            : 
     133                 :          0 :     return aRect;
     134                 :            : }
     135                 :            : 
     136                 :            : // -----------------------------------------------------------------------------
     137                 :            : 
     138                 :          0 : void SdVectorizeDlg::InitPreviewBmp()
     139                 :            : {
     140                 :          0 :     const Rectangle aRect( GetRect( aBmpWin.GetSizePixel(), aBmp.GetSizePixel() ) );
     141                 :            : 
     142                 :          0 :     aPreviewBmp = aBmp;
     143                 :          0 :     aPreviewBmp.Scale( aRect.GetSize() );
     144                 :          0 :     aBmpWin.SetGraphic( aPreviewBmp );
     145                 :          0 : }
     146                 :            : 
     147                 :            : // -----------------------------------------------------------------------------
     148                 :            : 
     149                 :          0 : Bitmap SdVectorizeDlg::GetPreparedBitmap( Bitmap& rBmp, Fraction& rScale )
     150                 :            : {
     151                 :          0 :     Bitmap      aNew( rBmp );
     152                 :          0 :     const Size  aSizePix( aNew.GetSizePixel() );
     153                 :            : 
     154                 :          0 :     if( aSizePix.Width() > VECTORIZE_MAX_EXTENT || aSizePix.Height() > VECTORIZE_MAX_EXTENT )
     155                 :            :     {
     156                 :          0 :         const Rectangle aRect( GetRect( Size( VECTORIZE_MAX_EXTENT, VECTORIZE_MAX_EXTENT ), aSizePix ) );
     157                 :          0 :         rScale = Fraction( aSizePix.Width(), aRect.GetWidth() );
     158                 :          0 :         aNew.Scale( aRect.GetSize() );
     159                 :            :     }
     160                 :            :     else
     161                 :          0 :         rScale = Fraction( 1, 1 );
     162                 :            : 
     163                 :          0 :     aNew.ReduceColors( (sal_uInt16) aNmLayers.GetValue(), BMP_REDUCE_SIMPLE );
     164                 :            : 
     165                 :          0 :     return aNew;
     166                 :            : }
     167                 :            : 
     168                 :            : // -----------------------------------------------------------------------------
     169                 :            : 
     170                 :          0 : void SdVectorizeDlg::Calculate( Bitmap& rBmp, GDIMetaFile& rMtf )
     171                 :            : {
     172                 :          0 :     mpDocSh->SetWaitCursor( sal_True );
     173                 :          0 :     aPrgs.SetValue( 0 );
     174                 :            : 
     175                 :          0 :     Fraction    aScale;
     176                 :          0 :     Bitmap      aTmp( GetPreparedBitmap( rBmp, aScale ) );
     177                 :            : 
     178                 :          0 :     if( !!aTmp )
     179                 :            :     {
     180                 :          0 :         const Link aPrgsHdl( LINK( this, SdVectorizeDlg, ProgressHdl ) );
     181                 :          0 :         aTmp.Vectorize( rMtf, (sal_uInt8) aMtReduce.GetValue(), BMP_VECTORIZE_OUTER | BMP_VECTORIZE_REDUCE_EDGES, &aPrgsHdl );
     182                 :            : 
     183                 :          0 :         if( aCbFillHoles.IsChecked() )
     184                 :            :         {
     185                 :          0 :             GDIMetaFile         aNewMtf;
     186                 :          0 :             BitmapReadAccess*   pRAcc = aTmp.AcquireReadAccess();
     187                 :            : 
     188                 :          0 :             if( pRAcc )
     189                 :            :             {
     190                 :          0 :                 const long      nWidth = pRAcc->Width();
     191                 :          0 :                 const long      nHeight = pRAcc->Height();
     192                 :          0 :                 const long      nTileX = static_cast<long>(aMtFillHoles.GetValue());
     193                 :          0 :                 const long      nTileY = static_cast<long>(aMtFillHoles.GetValue());
     194                 :          0 :                 const long      nCountX = nWidth / nTileX;
     195                 :          0 :                 const long      nCountY = nHeight / nTileY;
     196                 :          0 :                 const long      nRestX = nWidth % nTileX;
     197                 :          0 :                 const long      nRestY = nHeight % nTileY;
     198                 :            : 
     199                 :          0 :                 MapMode aMap( rMtf.GetPrefMapMode() );
     200                 :          0 :                 aNewMtf.SetPrefSize( rMtf.GetPrefSize() );
     201                 :          0 :                 aNewMtf.SetPrefMapMode( aMap );
     202                 :            : 
     203                 :          0 :                 for( long nTY = 0; nTY < nCountY; nTY++ )
     204                 :            :                 {
     205                 :          0 :                     const long nY = nTY * nTileY;
     206                 :            : 
     207                 :          0 :                     for( long nTX = 0; nTX < nCountX; nTX++ )
     208                 :          0 :                         AddTile( pRAcc, aNewMtf, nTX * nTileX, nTY * nTileY, nTileX, nTileY );
     209                 :            : 
     210                 :          0 :                     if( nRestX )
     211                 :          0 :                         AddTile( pRAcc, aNewMtf, nCountX * nTileX, nY, nRestX, nTileY );
     212                 :            :                 }
     213                 :            : 
     214                 :          0 :                 if( nRestY )
     215                 :            :                 {
     216                 :          0 :                     const long nY = nCountY * nTileY;
     217                 :            : 
     218                 :          0 :                     for( long nTX = 0; nTX < nCountX; nTX++ )
     219                 :          0 :                         AddTile( pRAcc, aNewMtf, nTX * nTileX, nY, nTileX, nRestY );
     220                 :            : 
     221                 :          0 :                     if( nRestX )
     222                 :          0 :                         AddTile( pRAcc, aNewMtf, nCountX * nTileX, nCountY * nTileY, nRestX, nRestY );
     223                 :            :                 }
     224                 :            : 
     225                 :            : 
     226                 :          0 :                 aTmp.ReleaseAccess( pRAcc );
     227                 :            : 
     228                 :          0 :                 for( size_t n = 0, nCount = rMtf.GetActionSize(); n < nCount; n++ )
     229                 :          0 :                     aNewMtf.AddAction( rMtf.GetAction( n )->Clone() );
     230                 :            : 
     231                 :          0 :                 aMap.SetScaleX( aMap.GetScaleX() * aScale );
     232                 :          0 :                 aMap.SetScaleY( aMap.GetScaleY() * aScale );
     233                 :          0 :                 aNewMtf.SetPrefMapMode( aMap );
     234                 :          0 :                 rMtf = aNewMtf;
     235                 :          0 :             }
     236                 :            :         }
     237                 :            :     }
     238                 :            : 
     239                 :          0 :     aPrgs.SetValue( 0 );
     240                 :          0 :     mpDocSh->SetWaitCursor( sal_False );
     241                 :          0 : }
     242                 :            : 
     243                 :            : // -----------------------------------------------------------------------------
     244                 :            : 
     245                 :          0 : void SdVectorizeDlg::AddTile( BitmapReadAccess* pRAcc, GDIMetaFile& rMtf,
     246                 :            :                               long nPosX, long nPosY, long nWidth, long nHeight )
     247                 :            : {
     248                 :          0 :     sal_uLong           nSumR = 0UL, nSumG = 0UL, nSumB = 0UL;
     249                 :          0 :     const long      nRight = nPosX + nWidth - 1L;
     250                 :          0 :     const long      nBottom = nPosY + nHeight - 1L;
     251                 :          0 :     const double    fMult = 1.0 / ( nWidth * nHeight );
     252                 :            : 
     253                 :          0 :     for( long nY = nPosY; nY <= nBottom; nY++ )
     254                 :            :     {
     255                 :          0 :         for( long nX = nPosX; nX <= nRight; nX++ )
     256                 :            :         {
     257                 :          0 :             const BitmapColor aPixel( pRAcc->GetColor( nY, nX ) );
     258                 :            : 
     259                 :          0 :             nSumR += aPixel.GetRed();
     260                 :          0 :             nSumG += aPixel.GetGreen();
     261                 :          0 :             nSumB += aPixel.GetBlue();
     262                 :          0 :         }
     263                 :            :     }
     264                 :            : 
     265                 :          0 :     const Color aColor( (sal_uInt8) FRound( nSumR * fMult ),
     266                 :          0 :                         (sal_uInt8) FRound( nSumG * fMult ),
     267                 :          0 :                         (sal_uInt8) FRound( nSumB * fMult ) );
     268                 :            : 
     269                 :          0 :     Rectangle   aRect( Point( nPosX, nPosY ), Size( nWidth + 1, nHeight + 1 ) );
     270                 :          0 :     const Size& rMaxSize = rMtf.GetPrefSize();
     271                 :            : 
     272                 :          0 :     aRect = PixelToLogic( aRect, rMtf.GetPrefMapMode() );
     273                 :            : 
     274                 :          0 :     if( aRect.Right() > ( rMaxSize.Width() - 1L ) )
     275                 :          0 :         aRect.Right() = rMaxSize.Width() - 1L;
     276                 :            : 
     277                 :          0 :     if( aRect.Bottom() > ( rMaxSize.Height() - 1L ) )
     278                 :          0 :         aRect.Bottom() = rMaxSize.Height() - 1L;
     279                 :            : 
     280                 :          0 :     rMtf.AddAction( new MetaLineColorAction( aColor, sal_True ) );
     281                 :          0 :     rMtf.AddAction( new MetaFillColorAction( aColor, sal_True ) );
     282                 :          0 :     rMtf.AddAction( new MetaRectAction( aRect ) );
     283                 :          0 : }
     284                 :            : 
     285                 :            : // -----------------------------------------------------------------------------
     286                 :            : 
     287                 :          0 : IMPL_LINK( SdVectorizeDlg, ProgressHdl, void*, pData )
     288                 :            : {
     289                 :          0 :     aPrgs.SetValue( (sal_uInt16)(sal_uLong) pData );
     290                 :          0 :     return 0L;
     291                 :            : }
     292                 :            : 
     293                 :            : // -----------------------------------------------------------------------------
     294                 :            : 
     295                 :          0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickPreviewHdl)
     296                 :            : {
     297                 :          0 :     Calculate( aBmp, aMtf );
     298                 :          0 :     aMtfWin.SetGraphic( aMtf );
     299                 :          0 :     aBtnPreview.Disable();
     300                 :            : 
     301                 :          0 :     return 0L;
     302                 :            : }
     303                 :            : 
     304                 :            : // -----------------------------------------------------------------------------
     305                 :            : 
     306                 :          0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickOKHdl)
     307                 :            : {
     308                 :          0 :     if( aBtnPreview.IsEnabled() )
     309                 :          0 :         Calculate( aBmp, aMtf );
     310                 :            : 
     311                 :          0 :     SaveSettings();
     312                 :          0 :     EndDialog( RET_OK );
     313                 :            : 
     314                 :          0 :     return 0L;
     315                 :            : }
     316                 :            : 
     317                 :            : // -----------------------------------------------------------------------------
     318                 :            : 
     319                 :          0 : IMPL_LINK( SdVectorizeDlg, ToggleHdl, CheckBox*, pCb )
     320                 :            : {
     321                 :          0 :     if( pCb->IsChecked() )
     322                 :            :     {
     323                 :          0 :         aFtFillHoles.Enable();
     324                 :          0 :         aMtFillHoles.Enable();
     325                 :            :     }
     326                 :            :     else
     327                 :            :     {
     328                 :          0 :         aFtFillHoles.Disable();
     329                 :          0 :         aMtFillHoles.Disable();
     330                 :            :     }
     331                 :            : 
     332                 :          0 :     ModifyHdl( NULL );
     333                 :            : 
     334                 :          0 :     return 0L;
     335                 :            : }
     336                 :            : 
     337                 :            : // -----------------------------------------------------------------------------
     338                 :            : 
     339                 :          0 : IMPL_LINK_NOARG(SdVectorizeDlg, ModifyHdl)
     340                 :            : {
     341                 :          0 :     aBtnPreview.Enable();
     342                 :          0 :     return 0L;
     343                 :            : }
     344                 :            : 
     345                 :            : // -----------------------------------------------------------------------------
     346                 :            : 
     347                 :          0 : void SdVectorizeDlg::LoadSettings()
     348                 :            : {
     349                 :          0 :     SvStorageStreamRef  xIStm( SD_MOD()->GetOptionStream(
     350                 :            :                                rtl::OUString( SD_OPTION_VECTORIZE ) ,
     351                 :          0 :                                SD_OPTION_LOAD ) );
     352                 :            :     sal_uInt16              nLayers;
     353                 :            :     sal_uInt16              nReduce;
     354                 :            :     sal_uInt16              nFillHoles;
     355                 :            :     sal_Bool                bFillHoles;
     356                 :            : 
     357                 :          0 :     if( xIStm.Is() )
     358                 :            :     {
     359                 :          0 :         SdIOCompat aCompat( *xIStm, STREAM_READ );
     360                 :          0 :         *xIStm >> nLayers >> nReduce >> nFillHoles >> bFillHoles;
     361                 :            :     }
     362                 :            :     else
     363                 :            :     {
     364                 :          0 :         nLayers = 8;
     365                 :          0 :         nReduce = 0;
     366                 :          0 :         nFillHoles = 32;
     367                 :          0 :         bFillHoles = sal_False;
     368                 :            :     }
     369                 :            : 
     370                 :          0 :     aNmLayers.SetValue( nLayers );
     371                 :          0 :     aMtReduce.SetValue( nReduce );
     372                 :          0 :     aMtFillHoles.SetValue( nFillHoles );
     373                 :          0 :     aCbFillHoles.Check( bFillHoles );
     374                 :            : 
     375                 :          0 :     ToggleHdl( &aCbFillHoles );
     376                 :          0 : }
     377                 :            : 
     378                 :            : // -----------------------------------------------------------------------------
     379                 :            : 
     380                 :          0 : void SdVectorizeDlg::SaveSettings() const
     381                 :            : {
     382                 :          0 :     SvStorageStreamRef xOStm( SD_MOD()->GetOptionStream(
     383                 :            :                               rtl::OUString(SD_OPTION_VECTORIZE)  ,
     384                 :          0 :                               SD_OPTION_STORE ) );
     385                 :            : 
     386                 :          0 :     if( xOStm.Is() )
     387                 :            :     {
     388                 :          0 :         SdIOCompat aCompat( *xOStm, STREAM_WRITE, 1 );
     389                 :          0 :         *xOStm << (sal_uInt16) aNmLayers.GetValue() << (sal_uInt16) aMtReduce.GetValue();
     390                 :          0 :         *xOStm << (sal_uInt16) aMtFillHoles.GetValue() << aCbFillHoles.IsChecked();
     391                 :          0 :     }
     392                 :          0 : }
     393                 :            : 
     394                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10