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 "vectdlg.hrc"
30 : #include <vcl/bmpacc.hxx>
31 : #include <vcl/msgbox.hxx>
32 : #include <vcl/metaact.hxx>
33 :
34 : #define VECTORIZE_MAX_EXTENT 512
35 :
36 0 : SdVectorizeDlg::SdVectorizeDlg(
37 : Window* pParent, const Bitmap& rBmp, ::sd::DrawDocShell* pDocShell ) :
38 : ModalDialog ( pParent, SdResId( DLG_VECTORIZE ) ),
39 : mpDocSh ( pDocShell ),
40 : aGrpSettings ( this, SdResId( GRP_SETTINGS ) ),
41 : aFtLayers ( this, SdResId( FT_LAYERS ) ),
42 : aNmLayers ( this, SdResId( NM_LAYERS ) ),
43 : aFtReduce ( this, SdResId( FT_REDUCE ) ),
44 : aMtReduce ( this, SdResId( MT_REDUCE ) ),
45 : aFtFillHoles ( this, SdResId( FT_FILLHOLES ) ),
46 : aMtFillHoles ( this, SdResId( MT_FILLHOLES ) ),
47 : aCbFillHoles ( this, SdResId( CB_FILLHOLES ) ),
48 : aFtOriginal ( this, SdResId( FT_ORIGINAL ) ),
49 : aBmpWin ( this, SdResId( CTL_BMP ) ),
50 : aFtVectorized ( this, SdResId( FT_VECTORIZED ) ),
51 : aMtfWin ( this, SdResId( CTL_WMF ) ),
52 : aGrpPrgs ( this, SdResId( GRP_PRGS ) ),
53 : aPrgs ( this, SdResId( WND_PRGS ) ),
54 : aBtnOK ( this, SdResId( BTN_OK ) ),
55 : aBtnCancel ( this, SdResId( BTN_CANCEL ) ),
56 : aBtnHelp ( this, SdResId( BTN_HELP ) ),
57 : aBtnPreview ( this, SdResId( BTN_PREVIEW ) ),
58 0 : aBmp ( rBmp )
59 : {
60 0 : FreeResource();
61 :
62 0 : aBtnPreview.SetClickHdl( LINK( this, SdVectorizeDlg, ClickPreviewHdl ) );
63 0 : aBtnOK.SetClickHdl( LINK( this, SdVectorizeDlg, ClickOKHdl ) );
64 0 : aNmLayers.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
65 0 : aMtReduce.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
66 0 : aMtFillHoles.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
67 0 : aCbFillHoles.SetToggleHdl( LINK( this, SdVectorizeDlg, ToggleHdl ) );
68 :
69 : // disable 3D border
70 0 : aBmpWin.SetBorderStyle(WINDOW_BORDER_MONO);
71 0 : aMtfWin.SetBorderStyle(WINDOW_BORDER_MONO);
72 :
73 0 : LoadSettings();
74 0 : InitPreviewBmp();
75 0 : }
76 :
77 0 : SdVectorizeDlg::~SdVectorizeDlg()
78 : {
79 0 : }
80 :
81 0 : Rectangle SdVectorizeDlg::GetRect( const Size& rDispSize, const Size& rBmpSize ) const
82 : {
83 0 : Rectangle aRect;
84 :
85 0 : if( rBmpSize.Width() && rBmpSize.Height() && rDispSize.Width() && rDispSize.Height() )
86 : {
87 0 : Size aBmpSize( rBmpSize );
88 0 : const double fGrfWH = (double) aBmpSize.Width() / aBmpSize.Height();
89 0 : const double fWinWH = (double) rDispSize.Width() / rDispSize.Height();
90 :
91 0 : if( fGrfWH < fWinWH )
92 : {
93 0 : aBmpSize.Width() = (long) ( rDispSize.Height() * fGrfWH );
94 0 : aBmpSize.Height()= rDispSize.Height();
95 : }
96 : else
97 : {
98 0 : aBmpSize.Width() = rDispSize.Width();
99 0 : aBmpSize.Height()= (long) ( rDispSize.Width() / fGrfWH);
100 : }
101 :
102 0 : const Point aBmpPos( ( rDispSize.Width() - aBmpSize.Width() ) >> 1,
103 0 : ( rDispSize.Height() - aBmpSize.Height() ) >> 1 );
104 :
105 0 : aRect = Rectangle( aBmpPos, aBmpSize );
106 : }
107 :
108 0 : return aRect;
109 : }
110 :
111 0 : void SdVectorizeDlg::InitPreviewBmp()
112 : {
113 0 : const Rectangle aRect( GetRect( aBmpWin.GetSizePixel(), aBmp.GetSizePixel() ) );
114 :
115 0 : aPreviewBmp = aBmp;
116 0 : aPreviewBmp.Scale( aRect.GetSize() );
117 0 : aBmpWin.SetGraphic( aPreviewBmp );
118 0 : }
119 :
120 0 : Bitmap SdVectorizeDlg::GetPreparedBitmap( Bitmap& rBmp, Fraction& rScale )
121 : {
122 0 : Bitmap aNew( rBmp );
123 0 : const Size aSizePix( aNew.GetSizePixel() );
124 :
125 0 : if( aSizePix.Width() > VECTORIZE_MAX_EXTENT || aSizePix.Height() > VECTORIZE_MAX_EXTENT )
126 : {
127 0 : const Rectangle aRect( GetRect( Size( VECTORIZE_MAX_EXTENT, VECTORIZE_MAX_EXTENT ), aSizePix ) );
128 0 : rScale = Fraction( aSizePix.Width(), aRect.GetWidth() );
129 0 : aNew.Scale( aRect.GetSize() );
130 : }
131 : else
132 0 : rScale = Fraction( 1, 1 );
133 :
134 0 : aNew.ReduceColors( (sal_uInt16) aNmLayers.GetValue(), BMP_REDUCE_SIMPLE );
135 :
136 0 : return aNew;
137 : }
138 :
139 0 : void SdVectorizeDlg::Calculate( Bitmap& rBmp, GDIMetaFile& rMtf )
140 : {
141 0 : mpDocSh->SetWaitCursor( sal_True );
142 0 : aPrgs.SetValue( 0 );
143 :
144 0 : Fraction aScale;
145 0 : Bitmap aTmp( GetPreparedBitmap( rBmp, aScale ) );
146 :
147 0 : if( !!aTmp )
148 : {
149 0 : const Link aPrgsHdl( LINK( this, SdVectorizeDlg, ProgressHdl ) );
150 0 : aTmp.Vectorize( rMtf, (sal_uInt8) aMtReduce.GetValue(), BMP_VECTORIZE_OUTER | BMP_VECTORIZE_REDUCE_EDGES, &aPrgsHdl );
151 :
152 0 : if( aCbFillHoles.IsChecked() )
153 : {
154 0 : GDIMetaFile aNewMtf;
155 0 : BitmapReadAccess* pRAcc = aTmp.AcquireReadAccess();
156 :
157 0 : if( pRAcc )
158 : {
159 0 : const long nWidth = pRAcc->Width();
160 0 : const long nHeight = pRAcc->Height();
161 0 : const long nTileX = static_cast<long>(aMtFillHoles.GetValue());
162 0 : const long nTileY = static_cast<long>(aMtFillHoles.GetValue());
163 0 : const long nCountX = nWidth / nTileX;
164 0 : const long nCountY = nHeight / nTileY;
165 0 : const long nRestX = nWidth % nTileX;
166 0 : const long nRestY = nHeight % nTileY;
167 :
168 0 : MapMode aMap( rMtf.GetPrefMapMode() );
169 0 : aNewMtf.SetPrefSize( rMtf.GetPrefSize() );
170 0 : aNewMtf.SetPrefMapMode( aMap );
171 :
172 0 : for( long nTY = 0; nTY < nCountY; nTY++ )
173 : {
174 0 : const long nY = nTY * nTileY;
175 :
176 0 : for( long nTX = 0; nTX < nCountX; nTX++ )
177 0 : AddTile( pRAcc, aNewMtf, nTX * nTileX, nTY * nTileY, nTileX, nTileY );
178 :
179 0 : if( nRestX )
180 0 : AddTile( pRAcc, aNewMtf, nCountX * nTileX, nY, nRestX, nTileY );
181 : }
182 :
183 0 : if( nRestY )
184 : {
185 0 : const long nY = nCountY * nTileY;
186 :
187 0 : for( long nTX = 0; nTX < nCountX; nTX++ )
188 0 : AddTile( pRAcc, aNewMtf, nTX * nTileX, nY, nTileX, nRestY );
189 :
190 0 : if( nRestX )
191 0 : AddTile( pRAcc, aNewMtf, nCountX * nTileX, nCountY * nTileY, nRestX, nRestY );
192 : }
193 :
194 :
195 0 : aTmp.ReleaseAccess( pRAcc );
196 :
197 0 : for( size_t n = 0, nCount = rMtf.GetActionSize(); n < nCount; n++ )
198 0 : aNewMtf.AddAction( rMtf.GetAction( n )->Clone() );
199 :
200 0 : aMap.SetScaleX( aMap.GetScaleX() * aScale );
201 0 : aMap.SetScaleY( aMap.GetScaleY() * aScale );
202 0 : aNewMtf.SetPrefMapMode( aMap );
203 0 : rMtf = aNewMtf;
204 0 : }
205 : }
206 : }
207 :
208 0 : aPrgs.SetValue( 0 );
209 0 : mpDocSh->SetWaitCursor( sal_False );
210 0 : }
211 :
212 0 : void SdVectorizeDlg::AddTile( BitmapReadAccess* pRAcc, GDIMetaFile& rMtf,
213 : long nPosX, long nPosY, long nWidth, long nHeight )
214 : {
215 0 : sal_uLong nSumR = 0UL, nSumG = 0UL, nSumB = 0UL;
216 0 : const long nRight = nPosX + nWidth - 1L;
217 0 : const long nBottom = nPosY + nHeight - 1L;
218 0 : const double fMult = 1.0 / ( nWidth * nHeight );
219 :
220 0 : for( long nY = nPosY; nY <= nBottom; nY++ )
221 : {
222 0 : for( long nX = nPosX; nX <= nRight; nX++ )
223 : {
224 0 : const BitmapColor aPixel( pRAcc->GetColor( nY, nX ) );
225 :
226 0 : nSumR += aPixel.GetRed();
227 0 : nSumG += aPixel.GetGreen();
228 0 : nSumB += aPixel.GetBlue();
229 0 : }
230 : }
231 :
232 0 : const Color aColor( (sal_uInt8) FRound( nSumR * fMult ),
233 0 : (sal_uInt8) FRound( nSumG * fMult ),
234 0 : (sal_uInt8) FRound( nSumB * fMult ) );
235 :
236 0 : Rectangle aRect( Point( nPosX, nPosY ), Size( nWidth + 1, nHeight + 1 ) );
237 0 : const Size& rMaxSize = rMtf.GetPrefSize();
238 :
239 0 : aRect = PixelToLogic( aRect, rMtf.GetPrefMapMode() );
240 :
241 0 : if( aRect.Right() > ( rMaxSize.Width() - 1L ) )
242 0 : aRect.Right() = rMaxSize.Width() - 1L;
243 :
244 0 : if( aRect.Bottom() > ( rMaxSize.Height() - 1L ) )
245 0 : aRect.Bottom() = rMaxSize.Height() - 1L;
246 :
247 0 : rMtf.AddAction( new MetaLineColorAction( aColor, sal_True ) );
248 0 : rMtf.AddAction( new MetaFillColorAction( aColor, sal_True ) );
249 0 : rMtf.AddAction( new MetaRectAction( aRect ) );
250 0 : }
251 :
252 0 : IMPL_LINK( SdVectorizeDlg, ProgressHdl, void*, pData )
253 : {
254 0 : aPrgs.SetValue( (sal_uInt16)(sal_uLong) pData );
255 0 : return 0L;
256 : }
257 :
258 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickPreviewHdl)
259 : {
260 0 : Calculate( aBmp, aMtf );
261 0 : aMtfWin.SetGraphic( aMtf );
262 0 : aBtnPreview.Disable();
263 :
264 0 : return 0L;
265 : }
266 :
267 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickOKHdl)
268 : {
269 0 : if( aBtnPreview.IsEnabled() )
270 0 : Calculate( aBmp, aMtf );
271 :
272 0 : SaveSettings();
273 0 : EndDialog( RET_OK );
274 :
275 0 : return 0L;
276 : }
277 :
278 0 : IMPL_LINK( SdVectorizeDlg, ToggleHdl, CheckBox*, pCb )
279 : {
280 0 : if( pCb->IsChecked() )
281 : {
282 0 : aFtFillHoles.Enable();
283 0 : aMtFillHoles.Enable();
284 : }
285 : else
286 : {
287 0 : aFtFillHoles.Disable();
288 0 : aMtFillHoles.Disable();
289 : }
290 :
291 0 : ModifyHdl( NULL );
292 :
293 0 : return 0L;
294 : }
295 :
296 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ModifyHdl)
297 : {
298 0 : aBtnPreview.Enable();
299 0 : return 0L;
300 : }
301 :
302 0 : void SdVectorizeDlg::LoadSettings()
303 : {
304 0 : SvStorageStreamRef xIStm( SD_MOD()->GetOptionStream(
305 : OUString( SD_OPTION_VECTORIZE ) ,
306 0 : SD_OPTION_LOAD ) );
307 : sal_uInt16 nLayers;
308 : sal_uInt16 nReduce;
309 : sal_uInt16 nFillHoles;
310 : sal_Bool bFillHoles;
311 :
312 0 : if( xIStm.Is() )
313 : {
314 0 : SdIOCompat aCompat( *xIStm, STREAM_READ );
315 0 : *xIStm >> nLayers >> nReduce >> nFillHoles >> bFillHoles;
316 : }
317 : else
318 : {
319 0 : nLayers = 8;
320 0 : nReduce = 0;
321 0 : nFillHoles = 32;
322 0 : bFillHoles = sal_False;
323 : }
324 :
325 0 : aNmLayers.SetValue( nLayers );
326 0 : aMtReduce.SetValue( nReduce );
327 0 : aMtFillHoles.SetValue( nFillHoles );
328 0 : aCbFillHoles.Check( bFillHoles );
329 :
330 0 : ToggleHdl( &aCbFillHoles );
331 0 : }
332 :
333 0 : void SdVectorizeDlg::SaveSettings() const
334 : {
335 0 : SvStorageStreamRef xOStm( SD_MOD()->GetOptionStream(
336 : OUString(SD_OPTION_VECTORIZE) ,
337 0 : SD_OPTION_STORE ) );
338 :
339 0 : if( xOStm.Is() )
340 : {
341 0 : SdIOCompat aCompat( *xOStm, STREAM_WRITE, 1 );
342 0 : *xOStm << (sal_uInt16) aNmLayers.GetValue() << (sal_uInt16) aMtReduce.GetValue();
343 0 : *xOStm << (sal_uInt16) aMtFillHoles.GetValue() << aCbFillHoles.IsChecked();
344 0 : }
345 0 : }
346 :
347 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|