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(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(WINDOW_BORDER_MONO);
67 0 : m_pMtfWin->SetBorderStyle(WINDOW_BORDER_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 :
191 0 : aTmp.ReleaseAccess( pRAcc );
192 :
193 0 : for( size_t n = 0, nCount = rMtf.GetActionSize(); n < nCount; n++ )
194 0 : aNewMtf.AddAction( rMtf.GetAction( n )->Clone() );
195 :
196 0 : aMap.SetScaleX( aMap.GetScaleX() * aScale );
197 0 : aMap.SetScaleY( aMap.GetScaleY() * aScale );
198 0 : aNewMtf.SetPrefMapMode( aMap );
199 0 : rMtf = aNewMtf;
200 0 : }
201 : }
202 : }
203 :
204 0 : m_pPrgs->SetValue( 0 );
205 0 : mpDocSh->SetWaitCursor( false );
206 0 : }
207 :
208 0 : void SdVectorizeDlg::AddTile( BitmapReadAccess* pRAcc, GDIMetaFile& rMtf,
209 : long nPosX, long nPosY, long nWidth, long nHeight )
210 : {
211 0 : sal_uLong nSumR = 0UL, nSumG = 0UL, nSumB = 0UL;
212 0 : const long nRight = nPosX + nWidth - 1L;
213 0 : const long nBottom = nPosY + nHeight - 1L;
214 0 : const double fMult = 1.0 / ( nWidth * nHeight );
215 :
216 0 : for( long nY = nPosY; nY <= nBottom; nY++ )
217 : {
218 0 : for( long nX = nPosX; nX <= nRight; nX++ )
219 : {
220 0 : const BitmapColor aPixel( pRAcc->GetColor( nY, nX ) );
221 :
222 0 : nSumR += aPixel.GetRed();
223 0 : nSumG += aPixel.GetGreen();
224 0 : nSumB += aPixel.GetBlue();
225 0 : }
226 : }
227 :
228 0 : const Color aColor( (sal_uInt8) FRound( nSumR * fMult ),
229 0 : (sal_uInt8) FRound( nSumG * fMult ),
230 0 : (sal_uInt8) FRound( nSumB * fMult ) );
231 :
232 0 : Rectangle aRect( Point( nPosX, nPosY ), Size( nWidth + 1, nHeight + 1 ) );
233 0 : const Size& rMaxSize = rMtf.GetPrefSize();
234 :
235 0 : aRect = PixelToLogic( aRect, rMtf.GetPrefMapMode() );
236 :
237 0 : if( aRect.Right() > ( rMaxSize.Width() - 1L ) )
238 0 : aRect.Right() = rMaxSize.Width() - 1L;
239 :
240 0 : if( aRect.Bottom() > ( rMaxSize.Height() - 1L ) )
241 0 : aRect.Bottom() = rMaxSize.Height() - 1L;
242 :
243 0 : rMtf.AddAction( new MetaLineColorAction( aColor, true ) );
244 0 : rMtf.AddAction( new MetaFillColorAction( aColor, true ) );
245 0 : rMtf.AddAction( new MetaRectAction( aRect ) );
246 0 : }
247 :
248 0 : IMPL_LINK( SdVectorizeDlg, ProgressHdl, void*, pData )
249 : {
250 0 : m_pPrgs->SetValue( (sal_uInt16)(sal_uLong) pData );
251 0 : return 0L;
252 : }
253 :
254 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickPreviewHdl)
255 : {
256 0 : Calculate( aBmp, aMtf );
257 0 : m_pMtfWin->SetGraphic( aMtf );
258 0 : m_pBtnPreview->Disable();
259 :
260 0 : return 0L;
261 : }
262 :
263 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ClickOKHdl)
264 : {
265 0 : if( m_pBtnPreview->IsEnabled() )
266 0 : Calculate( aBmp, aMtf );
267 :
268 0 : SaveSettings();
269 0 : EndDialog( RET_OK );
270 :
271 0 : return 0L;
272 : }
273 :
274 0 : IMPL_LINK( SdVectorizeDlg, ToggleHdl, CheckBox*, pCb )
275 : {
276 0 : if( pCb->IsChecked() )
277 : {
278 0 : m_pFtFillHoles->Enable();
279 0 : m_pMtFillHoles->Enable();
280 : }
281 : else
282 : {
283 0 : m_pFtFillHoles->Disable();
284 0 : m_pMtFillHoles->Disable();
285 : }
286 :
287 0 : ModifyHdl( NULL );
288 :
289 0 : return 0L;
290 : }
291 :
292 0 : IMPL_LINK_NOARG(SdVectorizeDlg, ModifyHdl)
293 : {
294 0 : m_pBtnPreview->Enable();
295 0 : return 0L;
296 : }
297 :
298 0 : void SdVectorizeDlg::LoadSettings()
299 : {
300 0 : SvStorageStreamRef xIStm( SD_MOD()->GetOptionStream(
301 : OUString( SD_OPTION_VECTORIZE ) ,
302 0 : SD_OPTION_LOAD ) );
303 : sal_uInt16 nLayers;
304 : sal_uInt16 nReduce;
305 : sal_uInt16 nFillHoles;
306 : sal_Bool bFillHoles;
307 :
308 0 : if( xIStm.Is() )
309 : {
310 0 : SdIOCompat aCompat( *xIStm, STREAM_READ );
311 0 : xIStm->ReadUInt16( nLayers ).ReadUInt16( nReduce ).ReadUInt16( nFillHoles ).ReadUChar( bFillHoles );
312 : }
313 : else
314 : {
315 0 : nLayers = 8;
316 0 : nReduce = 0;
317 0 : nFillHoles = 32;
318 0 : bFillHoles = sal_False;
319 : }
320 :
321 0 : m_pNmLayers->SetValue( nLayers );
322 0 : m_pMtReduce->SetValue( nReduce );
323 0 : m_pMtFillHoles->SetValue( nFillHoles );
324 0 : m_pCbFillHoles->Check( bFillHoles );
325 :
326 0 : ToggleHdl(m_pCbFillHoles);
327 0 : }
328 :
329 0 : void SdVectorizeDlg::SaveSettings() const
330 : {
331 0 : SvStorageStreamRef xOStm( SD_MOD()->GetOptionStream(
332 : OUString(SD_OPTION_VECTORIZE) ,
333 0 : SD_OPTION_STORE ) );
334 :
335 0 : if( xOStm.Is() )
336 : {
337 0 : SdIOCompat aCompat( *xOStm, STREAM_WRITE, 1 );
338 0 : xOStm->WriteUInt16( (sal_uInt16) m_pNmLayers->GetValue() ).WriteUInt16( (sal_uInt16) m_pMtReduce->GetValue() );
339 0 : xOStm->WriteUInt16( (sal_uInt16) m_pMtFillHoles->GetValue() ).WriteUChar( m_pCbFillHoles->IsChecked() );
340 0 : }
341 0 : }
342 :
343 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|