LCOV - code coverage report
Current view: top level - sd/source/ui/dlg - vectdlg.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 197 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 23 0.0 %
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 <vcl/vclenum.hxx>
      21             : #include <vcl/wrkwin.hxx>
      22             : 
      23             : #include "strings.hrc"
      24             : #include "sdresid.hxx"
      25             : #include "DrawDocShell.hxx"
      26             : #include "sdmod.hxx"
      27             : #include "sdiocmpt.hxx"
      28             : #include "vectdlg.hxx"
      29             : #include <vcl/bmpacc.hxx>
      30             : #include <vcl/msgbox.hxx>
      31             : #include <vcl/metaact.hxx>
      32             : 
      33             : #define VECTORIZE_MAX_EXTENT 512
      34             : 
      35           0 : SdVectorizeDlg::SdVectorizeDlg(vcl::Window* pParent, const Bitmap& rBmp, ::sd::DrawDocShell* pDocShell)
      36             :     : ModalDialog(pParent, "VectorizeDialog", "modules/sdraw/ui/vectorize.ui")
      37             :     , mpDocSh(pDocShell)
      38           0 :     , aBmp(rBmp)
      39             : {
      40           0 :     get(m_pNmLayers, "colors");
      41           0 :     get(m_pMtReduce, "points");
      42           0 :     get(m_pCbFillHoles, "fillholes");
      43           0 :     get(m_pFtFillHoles, "tilesft");
      44           0 :     get(m_pMtFillHoles, "tiles");
      45           0 :     get(m_pBmpWin, "source");
      46           0 :     get(m_pMtfWin, "vectorized");
      47             : 
      48           0 :     Size aSize(LogicToPixel(Size(92, 100), MAP_APPFONT));
      49           0 :     m_pBmpWin->set_width_request(aSize.Width());
      50           0 :     m_pMtfWin->set_width_request(aSize.Width());
      51           0 :     m_pBmpWin->set_height_request(aSize.Height());
      52           0 :     m_pMtfWin->set_height_request(aSize.Height());
      53             : 
      54           0 :     get(m_pPrgs, "progressbar");
      55           0 :     get(m_pBtnPreview, "preview");
      56           0 :     get(m_pBtnOK, "ok");
      57             : 
      58           0 :     m_pBtnPreview->SetClickHdl( LINK( this, SdVectorizeDlg, ClickPreviewHdl ) );
      59           0 :     m_pBtnOK->SetClickHdl( LINK( this, SdVectorizeDlg, ClickOKHdl ) );
      60           0 :     m_pNmLayers->SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
      61           0 :     m_pMtReduce->SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
      62           0 :     m_pMtFillHoles->SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
      63           0 :     m_pCbFillHoles->SetToggleHdl( LINK( this, SdVectorizeDlg, ToggleHdl ) );
      64             : 
      65             :     // disable 3D border
      66           0 :     m_pBmpWin->SetBorderStyle(WindowBorderStyle::MONO);
      67           0 :     m_pMtfWin->SetBorderStyle(WindowBorderStyle::MONO);
      68             : 
      69           0 :     LoadSettings();
      70           0 :     InitPreviewBmp();
      71           0 : }
      72             : 
      73           0 : SdVectorizeDlg::~SdVectorizeDlg()
      74             : {
      75           0 :     disposeOnce();
      76           0 : }
      77             : 
      78           0 : void SdVectorizeDlg::dispose()
      79             : {
      80           0 :     m_pNmLayers.clear();
      81           0 :     m_pMtReduce.clear();
      82           0 :     m_pFtFillHoles.clear();
      83           0 :     m_pMtFillHoles.clear();
      84           0 :     m_pCbFillHoles.clear();
      85           0 :     m_pBmpWin.clear();
      86           0 :     m_pMtfWin.clear();
      87           0 :     m_pPrgs.clear();
      88           0 :     m_pBtnOK.clear();
      89           0 :     m_pBtnPreview.clear();
      90           0 :     ModalDialog::dispose();
      91           0 : }
      92             : 
      93           0 : Rectangle SdVectorizeDlg::GetRect( const Size& rDispSize, const Size& rBmpSize )
      94             : {
      95           0 :     Rectangle aRect;
      96             : 
      97           0 :     if( rBmpSize.Width() && rBmpSize.Height() && rDispSize.Width() && rDispSize.Height() )
      98             :     {
      99           0 :         Size         aBmpSize( rBmpSize );
     100           0 :         const double fGrfWH = (double) aBmpSize.Width() / aBmpSize.Height();
     101           0 :         const double fWinWH = (double) rDispSize.Width() / rDispSize.Height();
     102             : 
     103           0 :         if( fGrfWH < fWinWH )
     104             :         {
     105           0 :             aBmpSize.Width() = (long) ( rDispSize.Height() * fGrfWH );
     106           0 :             aBmpSize.Height()= rDispSize.Height();
     107             :         }
     108             :         else
     109             :         {
     110           0 :             aBmpSize.Width() = rDispSize.Width();
     111           0 :             aBmpSize.Height()= (long) ( rDispSize.Width() / fGrfWH);
     112             :         }
     113             : 
     114           0 :         const Point aBmpPos( ( rDispSize.Width()  - aBmpSize.Width() ) >> 1,
     115           0 :                              ( rDispSize.Height() - aBmpSize.Height() ) >> 1 );
     116             : 
     117           0 :         aRect = Rectangle( aBmpPos, aBmpSize );
     118             :     }
     119             : 
     120           0 :     return aRect;
     121             : }
     122             : 
     123           0 : void SdVectorizeDlg::InitPreviewBmp()
     124             : {
     125           0 :     const Rectangle aRect( GetRect( m_pBmpWin->GetSizePixel(), aBmp.GetSizePixel() ) );
     126             : 
     127           0 :     aPreviewBmp = aBmp;
     128           0 :     aPreviewBmp.Scale( aRect.GetSize() );
     129           0 :     m_pBmpWin->SetGraphic( aPreviewBmp );
     130           0 : }
     131             : 
     132           0 : Bitmap SdVectorizeDlg::GetPreparedBitmap( Bitmap& rBmp, Fraction& rScale )
     133             : {
     134           0 :     Bitmap      aNew( rBmp );
     135           0 :     const Size  aSizePix( aNew.GetSizePixel() );
     136             : 
     137           0 :     if( aSizePix.Width() > VECTORIZE_MAX_EXTENT || aSizePix.Height() > VECTORIZE_MAX_EXTENT )
     138             :     {
     139           0 :         const Rectangle aRect( GetRect( Size( VECTORIZE_MAX_EXTENT, VECTORIZE_MAX_EXTENT ), aSizePix ) );
     140           0 :         rScale = Fraction( aSizePix.Width(), aRect.GetWidth() );
     141           0 :         aNew.Scale( aRect.GetSize() );
     142             :     }
     143             :     else
     144           0 :         rScale = Fraction( 1, 1 );
     145             : 
     146           0 :     aNew.ReduceColors( (sal_uInt16) m_pNmLayers->GetValue(), BMP_REDUCE_SIMPLE );
     147             : 
     148           0 :     return aNew;
     149             : }
     150             : 
     151           0 : void SdVectorizeDlg::Calculate( Bitmap& rBmp, GDIMetaFile& rMtf )
     152             : {
     153           0 :     mpDocSh->SetWaitCursor( true );
     154           0 :     m_pPrgs->SetValue( 0 );
     155             : 
     156           0 :     Fraction    aScale;
     157           0 :     Bitmap      aTmp( GetPreparedBitmap( rBmp, aScale ) );
     158             : 
     159           0 :     if( !!aTmp )
     160             :     {
     161           0 :         const Link<> aPrgsHdl( LINK( this, SdVectorizeDlg, ProgressHdl ) );
     162           0 :         aTmp.Vectorize( rMtf, (sal_uInt8) m_pMtReduce->GetValue(), BmpVectorizeFlags::Outer | BmpVectorizeFlags::ReduceEdges, &aPrgsHdl );
     163             : 
     164           0 :         if( m_pCbFillHoles->IsChecked() )
     165             :         {
     166           0 :             GDIMetaFile         aNewMtf;
     167           0 :             BitmapReadAccess*   pRAcc = aTmp.AcquireReadAccess();
     168             : 
     169           0 :             if( pRAcc )
     170             :             {
     171           0 :                 const long      nWidth = pRAcc->Width();
     172           0 :                 const long      nHeight = pRAcc->Height();
     173           0 :                 const long      nTileX = static_cast<long>(m_pMtFillHoles->GetValue());
     174           0 :                 const long      nTileY = static_cast<long>(m_pMtFillHoles->GetValue());
     175           0 :                 const long      nCountX = nWidth / nTileX;
     176           0 :                 const long      nCountY = nHeight / nTileY;
     177           0 :                 const long      nRestX = nWidth % nTileX;
     178           0 :                 const long      nRestY = nHeight % nTileY;
     179             : 
     180           0 :                 MapMode aMap( rMtf.GetPrefMapMode() );
     181           0 :                 aNewMtf.SetPrefSize( rMtf.GetPrefSize() );
     182           0 :                 aNewMtf.SetPrefMapMode( aMap );
     183             : 
     184           0 :                 for( long nTY = 0; nTY < nCountY; nTY++ )
     185             :                 {
     186           0 :                     const long nY = nTY * nTileY;
     187             : 
     188           0 :                     for( long nTX = 0; nTX < nCountX; nTX++ )
     189           0 :                         AddTile( pRAcc, aNewMtf, nTX * nTileX, nTY * nTileY, nTileX, nTileY );
     190             : 
     191           0 :                     if( nRestX )
     192           0 :                         AddTile( pRAcc, aNewMtf, nCountX * nTileX, nY, nRestX, nTileY );
     193             :                 }
     194             : 
     195           0 :                 if( nRestY )
     196             :                 {
     197           0 :                     const long nY = nCountY * nTileY;
     198             : 
     199           0 :                     for( long nTX = 0; nTX < nCountX; nTX++ )
     200           0 :                         AddTile( pRAcc, aNewMtf, nTX * nTileX, nY, nTileX, nRestY );
     201             : 
     202           0 :                     if( nRestX )
     203           0 :                         AddTile( pRAcc, aNewMtf, nCountX * nTileX, nCountY * nTileY, nRestX, nRestY );
     204             :                 }
     205             : 
     206           0 :                 Bitmap::ReleaseAccess( pRAcc );
     207             : 
     208           0 :                 for( size_t n = 0, nCount = rMtf.GetActionSize(); n < nCount; n++ )
     209           0 :                     aNewMtf.AddAction( rMtf.GetAction( n )->Clone() );
     210             : 
     211           0 :                 aMap.SetScaleX( aMap.GetScaleX() * aScale );
     212           0 :                 aMap.SetScaleY( aMap.GetScaleY() * aScale );
     213           0 :                 aNewMtf.SetPrefMapMode( aMap );
     214           0 :                 rMtf = aNewMtf;
     215           0 :             }
     216             :         }
     217             :     }
     218             : 
     219           0 :     m_pPrgs->SetValue( 0 );
     220           0 :     mpDocSh->SetWaitCursor( false );
     221           0 : }
     222             : 
     223           0 : void SdVectorizeDlg::AddTile( BitmapReadAccess* pRAcc, GDIMetaFile& rMtf,
     224             :                               long nPosX, long nPosY, long nWidth, long nHeight )
     225             : {
     226           0 :     sal_uLong           nSumR = 0UL, nSumG = 0UL, nSumB = 0UL;
     227           0 :     const long      nRight = nPosX + nWidth - 1L;
     228           0 :     const long      nBottom = nPosY + nHeight - 1L;
     229           0 :     const double    fMult = 1.0 / ( nWidth * nHeight );
     230             : 
     231           0 :     for( long nY = nPosY; nY <= nBottom; nY++ )
     232             :     {
     233           0 :         for( long nX = nPosX; nX <= nRight; nX++ )
     234             :         {
     235           0 :             const BitmapColor aPixel( pRAcc->GetColor( nY, nX ) );
     236             : 
     237           0 :             nSumR += aPixel.GetRed();
     238           0 :             nSumG += aPixel.GetGreen();
     239           0 :             nSumB += aPixel.GetBlue();
     240           0 :         }
     241             :     }
     242             : 
     243           0 :     const Color aColor( (sal_uInt8) FRound( nSumR * fMult ),
     244           0 :                         (sal_uInt8) FRound( nSumG * fMult ),
     245           0 :                         (sal_uInt8) FRound( nSumB * fMult ) );
     246             : 
     247           0 :     Rectangle   aRect( Point( nPosX, nPosY ), Size( nWidth + 1, nHeight + 1 ) );
     248           0 :     const Size& rMaxSize = rMtf.GetPrefSize();
     249             : 
     250           0 :     aRect = PixelToLogic( aRect, rMtf.GetPrefMapMode() );
     251             : 
     252           0 :     if( aRect.Right() > ( rMaxSize.Width() - 1L ) )
     253           0 :         aRect.Right() = rMaxSize.Width() - 1L;
     254             : 
     255           0 :     if( aRect.Bottom() > ( rMaxSize.Height() - 1L ) )
     256           0 :         aRect.Bottom() = rMaxSize.Height() - 1L;
     257             : 
     258           0 :     rMtf.AddAction( new MetaLineColorAction( aColor, true ) );
     259           0 :     rMtf.AddAction( new MetaFillColorAction( aColor, true ) );
     260           0 :     rMtf.AddAction( new MetaRectAction( aRect ) );
     261           0 : }
     262             : 
     263           0 : IMPL_LINK( SdVectorizeDlg, ProgressHdl, void*, pData )
     264             : {
     265           0 :     m_pPrgs->SetValue( (sal_uInt16)reinterpret_cast<sal_uLong>(pData) );
     266           0 :     return 0L;
     267             : }
     268             : 
     269           0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickPreviewHdl)
     270             : {
     271           0 :     Calculate( aBmp, aMtf );
     272           0 :     m_pMtfWin->SetGraphic( aMtf );
     273           0 :     m_pBtnPreview->Disable();
     274             : 
     275           0 :     return 0L;
     276             : }
     277             : 
     278           0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickOKHdl)
     279             : {
     280           0 :     if( m_pBtnPreview->IsEnabled() )
     281           0 :         Calculate( aBmp, aMtf );
     282             : 
     283           0 :     SaveSettings();
     284           0 :     EndDialog( RET_OK );
     285             : 
     286           0 :     return 0L;
     287             : }
     288             : 
     289           0 : IMPL_LINK( SdVectorizeDlg, ToggleHdl, CheckBox*, pCb )
     290             : {
     291           0 :     if( pCb->IsChecked() )
     292             :     {
     293           0 :         m_pFtFillHoles->Enable();
     294           0 :         m_pMtFillHoles->Enable();
     295             :     }
     296             :     else
     297             :     {
     298           0 :         m_pFtFillHoles->Disable();
     299           0 :         m_pMtFillHoles->Disable();
     300             :     }
     301             : 
     302           0 :     ModifyHdl( NULL );
     303             : 
     304           0 :     return 0L;
     305             : }
     306             : 
     307           0 : IMPL_LINK_NOARG(SdVectorizeDlg, ModifyHdl)
     308             : {
     309           0 :     m_pBtnPreview->Enable();
     310           0 :     return 0L;
     311             : }
     312             : 
     313           0 : void SdVectorizeDlg::LoadSettings()
     314             : {
     315           0 :     tools::SvRef<SotStorageStream>  xIStm( SD_MOD()->GetOptionStream(
     316             :                                OUString( SD_OPTION_VECTORIZE ) ,
     317           0 :                                SD_OPTION_LOAD ) );
     318             :     sal_uInt16              nLayers;
     319             :     sal_uInt16              nReduce;
     320             :     sal_uInt16              nFillHoles;
     321             :     bool                bFillHoles;
     322             : 
     323           0 :     if( xIStm.Is() )
     324             :     {
     325           0 :         SdIOCompat aCompat( *xIStm, StreamMode::READ );
     326           0 :         xIStm->ReadUInt16( nLayers ).ReadUInt16( nReduce ).ReadUInt16( nFillHoles ).ReadCharAsBool( bFillHoles );
     327             :     }
     328             :     else
     329             :     {
     330           0 :         nLayers = 8;
     331           0 :         nReduce = 0;
     332           0 :         nFillHoles = 32;
     333           0 :         bFillHoles = false;
     334             :     }
     335             : 
     336           0 :     m_pNmLayers->SetValue( nLayers );
     337           0 :     m_pMtReduce->SetValue( nReduce );
     338           0 :     m_pMtFillHoles->SetValue( nFillHoles );
     339           0 :     m_pCbFillHoles->Check( bFillHoles );
     340             : 
     341           0 :     ToggleHdl(m_pCbFillHoles);
     342           0 : }
     343             : 
     344           0 : void SdVectorizeDlg::SaveSettings() const
     345             : {
     346           0 :     tools::SvRef<SotStorageStream> xOStm( SD_MOD()->GetOptionStream(
     347             :                               OUString(SD_OPTION_VECTORIZE)  ,
     348           0 :                               SD_OPTION_STORE ) );
     349             : 
     350           0 :     if( xOStm.Is() )
     351             :     {
     352           0 :         SdIOCompat aCompat( *xOStm, StreamMode::WRITE, 1 );
     353           0 :         xOStm->WriteUInt16( m_pNmLayers->GetValue() ).WriteUInt16( m_pMtReduce->GetValue() );
     354           0 :         xOStm->WriteUInt16( m_pMtFillHoles->GetValue() ).WriteBool( m_pCbFillHoles->IsChecked() );
     355           0 :     }
     356           0 : }
     357             : 
     358             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11