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 : }
76 :
77 0 : Rectangle SdVectorizeDlg::GetRect( const Size& rDispSize, const Size& rBmpSize ) const
78 : {
79 0 : Rectangle aRect;
80 :
81 0 : if( rBmpSize.Width() && rBmpSize.Height() && rDispSize.Width() && rDispSize.Height() )
82 : {
83 0 : Size aBmpSize( rBmpSize );
84 0 : const double fGrfWH = (double) aBmpSize.Width() / aBmpSize.Height();
85 0 : const double fWinWH = (double) rDispSize.Width() / rDispSize.Height();
86 :
87 0 : if( fGrfWH < fWinWH )
88 : {
89 0 : aBmpSize.Width() = (long) ( rDispSize.Height() * fGrfWH );
90 0 : aBmpSize.Height()= rDispSize.Height();
91 : }
92 : else
93 : {
94 0 : aBmpSize.Width() = rDispSize.Width();
95 0 : aBmpSize.Height()= (long) ( rDispSize.Width() / fGrfWH);
96 : }
97 :
98 0 : const Point aBmpPos( ( rDispSize.Width() - aBmpSize.Width() ) >> 1,
99 0 : ( rDispSize.Height() - aBmpSize.Height() ) >> 1 );
100 :
101 0 : aRect = Rectangle( aBmpPos, aBmpSize );
102 : }
103 :
104 0 : return aRect;
105 : }
106 :
107 0 : void SdVectorizeDlg::InitPreviewBmp()
108 : {
109 0 : const Rectangle aRect( GetRect( m_pBmpWin->GetSizePixel(), aBmp.GetSizePixel() ) );
110 :
111 0 : aPreviewBmp = aBmp;
112 0 : aPreviewBmp.Scale( aRect.GetSize() );
113 0 : m_pBmpWin->SetGraphic( aPreviewBmp );
114 0 : }
115 :
116 0 : Bitmap SdVectorizeDlg::GetPreparedBitmap( Bitmap& rBmp, Fraction& rScale )
117 : {
118 0 : Bitmap aNew( rBmp );
119 0 : const Size aSizePix( aNew.GetSizePixel() );
120 :
121 0 : if( aSizePix.Width() > VECTORIZE_MAX_EXTENT || aSizePix.Height() > VECTORIZE_MAX_EXTENT )
122 : {
123 0 : const Rectangle aRect( GetRect( Size( VECTORIZE_MAX_EXTENT, VECTORIZE_MAX_EXTENT ), aSizePix ) );
124 0 : rScale = Fraction( aSizePix.Width(), aRect.GetWidth() );
125 0 : aNew.Scale( aRect.GetSize() );
126 : }
127 : else
128 0 : rScale = Fraction( 1, 1 );
129 :
130 0 : aNew.ReduceColors( (sal_uInt16) m_pNmLayers->GetValue(), BMP_REDUCE_SIMPLE );
131 :
132 0 : return aNew;
133 : }
134 :
135 0 : void SdVectorizeDlg::Calculate( Bitmap& rBmp, GDIMetaFile& rMtf )
136 : {
137 0 : mpDocSh->SetWaitCursor( true );
138 0 : m_pPrgs->SetValue( 0 );
139 :
140 0 : Fraction aScale;
141 0 : Bitmap aTmp( GetPreparedBitmap( rBmp, aScale ) );
142 :
143 0 : if( !!aTmp )
144 : {
145 0 : const Link aPrgsHdl( LINK( this, SdVectorizeDlg, ProgressHdl ) );
146 0 : aTmp.Vectorize( rMtf, (sal_uInt8) m_pMtReduce->GetValue(), BMP_VECTORIZE_OUTER | BMP_VECTORIZE_REDUCE_EDGES, &aPrgsHdl );
147 :
148 0 : if( m_pCbFillHoles->IsChecked() )
149 : {
150 0 : GDIMetaFile aNewMtf;
151 0 : BitmapReadAccess* pRAcc = aTmp.AcquireReadAccess();
152 :
153 0 : if( pRAcc )
154 : {
155 0 : const long nWidth = pRAcc->Width();
156 0 : const long nHeight = pRAcc->Height();
157 0 : const long nTileX = static_cast<long>(m_pMtFillHoles->GetValue());
158 0 : const long nTileY = static_cast<long>(m_pMtFillHoles->GetValue());
159 0 : const long nCountX = nWidth / nTileX;
160 0 : const long nCountY = nHeight / nTileY;
161 0 : const long nRestX = nWidth % nTileX;
162 0 : const long nRestY = nHeight % nTileY;
163 :
164 0 : MapMode aMap( rMtf.GetPrefMapMode() );
165 0 : aNewMtf.SetPrefSize( rMtf.GetPrefSize() );
166 0 : aNewMtf.SetPrefMapMode( aMap );
167 :
168 0 : for( long nTY = 0; nTY < nCountY; nTY++ )
169 : {
170 0 : const long nY = nTY * nTileY;
171 :
172 0 : for( long nTX = 0; nTX < nCountX; nTX++ )
173 0 : AddTile( pRAcc, aNewMtf, nTX * nTileX, nTY * nTileY, nTileX, nTileY );
174 :
175 0 : if( nRestX )
176 0 : AddTile( pRAcc, aNewMtf, nCountX * nTileX, nY, nRestX, nTileY );
177 : }
178 :
179 0 : if( nRestY )
180 : {
181 0 : const long nY = nCountY * nTileY;
182 :
183 0 : for( long nTX = 0; nTX < nCountX; nTX++ )
184 0 : AddTile( pRAcc, aNewMtf, nTX * nTileX, nY, nTileX, nRestY );
185 :
186 0 : if( nRestX )
187 0 : AddTile( pRAcc, aNewMtf, nCountX * nTileX, nCountY * nTileY, nRestX, nRestY );
188 : }
189 :
190 0 : aTmp.ReleaseAccess( pRAcc );
191 :
192 0 : for( size_t n = 0, nCount = rMtf.GetActionSize(); n < nCount; n++ )
193 0 : aNewMtf.AddAction( rMtf.GetAction( n )->Clone() );
194 :
195 0 : aMap.SetScaleX( aMap.GetScaleX() * aScale );
196 0 : aMap.SetScaleY( aMap.GetScaleY() * aScale );
197 0 : aNewMtf.SetPrefMapMode( aMap );
198 0 : rMtf = aNewMtf;
199 0 : }
200 : }
201 : }
202 :
203 0 : m_pPrgs->SetValue( 0 );
204 0 : mpDocSh->SetWaitCursor( false );
205 0 : }
206 :
207 0 : void SdVectorizeDlg::AddTile( BitmapReadAccess* pRAcc, GDIMetaFile& rMtf,
208 : long nPosX, long nPosY, long nWidth, long nHeight )
209 : {
210 0 : sal_uLong nSumR = 0UL, nSumG = 0UL, nSumB = 0UL;
211 0 : const long nRight = nPosX + nWidth - 1L;
212 0 : const long nBottom = nPosY + nHeight - 1L;
213 0 : const double fMult = 1.0 / ( nWidth * nHeight );
214 :
215 0 : for( long nY = nPosY; nY <= nBottom; nY++ )
216 : {
217 0 : for( long nX = nPosX; nX <= nRight; nX++ )
218 : {
219 0 : const BitmapColor aPixel( pRAcc->GetColor( nY, nX ) );
220 :
221 0 : nSumR += aPixel.GetRed();
222 0 : nSumG += aPixel.GetGreen();
223 0 : nSumB += aPixel.GetBlue();
224 0 : }
225 : }
226 :
227 0 : const Color aColor( (sal_uInt8) FRound( nSumR * fMult ),
228 0 : (sal_uInt8) FRound( nSumG * fMult ),
229 0 : (sal_uInt8) FRound( nSumB * fMult ) );
230 :
231 0 : Rectangle aRect( Point( nPosX, nPosY ), Size( nWidth + 1, nHeight + 1 ) );
232 0 : const Size& rMaxSize = rMtf.GetPrefSize();
233 :
234 0 : aRect = PixelToLogic( aRect, rMtf.GetPrefMapMode() );
235 :
236 0 : if( aRect.Right() > ( rMaxSize.Width() - 1L ) )
237 0 : aRect.Right() = rMaxSize.Width() - 1L;
238 :
239 0 : if( aRect.Bottom() > ( rMaxSize.Height() - 1L ) )
240 0 : aRect.Bottom() = rMaxSize.Height() - 1L;
241 :
242 0 : rMtf.AddAction( new MetaLineColorAction( aColor, true ) );
243 0 : rMtf.AddAction( new MetaFillColorAction( aColor, true ) );
244 0 : rMtf.AddAction( new MetaRectAction( aRect ) );
245 0 : }
246 :
247 0 : IMPL_LINK( SdVectorizeDlg, ProgressHdl, void*, pData )
248 : {
249 0 : m_pPrgs->SetValue( (sal_uInt16)(sal_uLong) pData );
250 0 : return 0L;
251 : }
252 :
253 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickPreviewHdl)
254 : {
255 0 : Calculate( aBmp, aMtf );
256 0 : m_pMtfWin->SetGraphic( aMtf );
257 0 : m_pBtnPreview->Disable();
258 :
259 0 : return 0L;
260 : }
261 :
262 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickOKHdl)
263 : {
264 0 : if( m_pBtnPreview->IsEnabled() )
265 0 : Calculate( aBmp, aMtf );
266 :
267 0 : SaveSettings();
268 0 : EndDialog( RET_OK );
269 :
270 0 : return 0L;
271 : }
272 :
273 0 : IMPL_LINK( SdVectorizeDlg, ToggleHdl, CheckBox*, pCb )
274 : {
275 0 : if( pCb->IsChecked() )
276 : {
277 0 : m_pFtFillHoles->Enable();
278 0 : m_pMtFillHoles->Enable();
279 : }
280 : else
281 : {
282 0 : m_pFtFillHoles->Disable();
283 0 : m_pMtFillHoles->Disable();
284 : }
285 :
286 0 : ModifyHdl( NULL );
287 :
288 0 : return 0L;
289 : }
290 :
291 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ModifyHdl)
292 : {
293 0 : m_pBtnPreview->Enable();
294 0 : return 0L;
295 : }
296 :
297 0 : void SdVectorizeDlg::LoadSettings()
298 : {
299 0 : SvStorageStreamRef xIStm( SD_MOD()->GetOptionStream(
300 : OUString( SD_OPTION_VECTORIZE ) ,
301 0 : SD_OPTION_LOAD ) );
302 : sal_uInt16 nLayers;
303 : sal_uInt16 nReduce;
304 : sal_uInt16 nFillHoles;
305 : bool bFillHoles;
306 :
307 0 : if( xIStm.Is() )
308 : {
309 0 : SdIOCompat aCompat( *xIStm, STREAM_READ );
310 0 : xIStm->ReadUInt16( nLayers ).ReadUInt16( nReduce ).ReadUInt16( nFillHoles ).ReadCharAsBool( bFillHoles );
311 : }
312 : else
313 : {
314 0 : nLayers = 8;
315 0 : nReduce = 0;
316 0 : nFillHoles = 32;
317 0 : bFillHoles = false;
318 : }
319 :
320 0 : m_pNmLayers->SetValue( nLayers );
321 0 : m_pMtReduce->SetValue( nReduce );
322 0 : m_pMtFillHoles->SetValue( nFillHoles );
323 0 : m_pCbFillHoles->Check( bFillHoles );
324 :
325 0 : ToggleHdl(m_pCbFillHoles);
326 0 : }
327 :
328 0 : void SdVectorizeDlg::SaveSettings() const
329 : {
330 0 : SvStorageStreamRef xOStm( SD_MOD()->GetOptionStream(
331 : OUString(SD_OPTION_VECTORIZE) ,
332 0 : SD_OPTION_STORE ) );
333 :
334 0 : if( xOStm.Is() )
335 : {
336 0 : SdIOCompat aCompat( *xOStm, STREAM_WRITE, 1 );
337 0 : xOStm->WriteUInt16( m_pNmLayers->GetValue() ).WriteUInt16( m_pMtReduce->GetValue() );
338 0 : xOStm->WriteUInt16( m_pMtFillHoles->GetValue() ).WriteUChar( m_pCbFillHoles->IsChecked() );
339 0 : }
340 0 : }
341 :
342 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|