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 <dlgunit.hxx>
21 : #include <vcl/graph.hxx>
22 : #include <vcl/graphicfilter.hxx>
23 : #include <vcl/virdev.hxx>
24 : #include <vcl/svapp.hxx>
25 : #include <vcl/settings.hxx>
26 : #include <svx/svdograf.hxx>
27 : #include <svx/sdgcpitm.hxx>
28 : #include <svx/dialmgr.hxx>
29 : #include <svx/compressgraphicdialog.hxx>
30 : #include <sfx2/dispatch.hxx>
31 : #include <comphelper/processfactory.hxx>
32 : #include <com/sun/star/uno/Sequence.hxx>
33 : #include <com/sun/star/beans/XPropertySet.hpp>
34 :
35 : #include "compressgraphicdialog.hrc"
36 :
37 : using namespace com::sun::star::uno;
38 : using namespace com::sun::star::beans;
39 :
40 0 : CompressGraphicsDialog::CompressGraphicsDialog( vcl::Window* pParent, SdrGrafObj* pGraphicObj, SfxBindings& rBindings ) :
41 : ModalDialog ( pParent, "CompressGraphicDialog", "svx/ui/compressgraphicdialog.ui" ),
42 : m_pGraphicObj ( pGraphicObj ),
43 0 : m_aGraphic ( pGraphicObj->GetGraphicObject().GetGraphic() ),
44 0 : m_aViewSize100mm ( pGraphicObj->GetLogicRect().GetSize() ),
45 : m_rBindings ( rBindings ),
46 0 : m_dResolution ( 96.0 )
47 : {
48 0 : const SdrGrafCropItem& rCrop = static_cast<const SdrGrafCropItem&>( m_pGraphicObj->GetMergedItem(SDRATTR_GRAFCROP) );
49 0 : m_aCropRectangle = Rectangle(rCrop.GetLeft(), rCrop.GetTop(), rCrop.GetRight(), rCrop.GetBottom());
50 :
51 0 : Initialize();
52 0 : }
53 :
54 0 : CompressGraphicsDialog::CompressGraphicsDialog( vcl::Window* pParent, Graphic& rGraphic, Size rViewSize100mm, Rectangle& rCropRectangle, SfxBindings& rBindings ) :
55 : ModalDialog ( pParent, "CompressGraphicDialog", "svx/ui/compressgraphicdialog.ui" ),
56 : m_pGraphicObj ( NULL ),
57 : m_aGraphic ( rGraphic ),
58 : m_aViewSize100mm ( rViewSize100mm ),
59 : m_aCropRectangle ( rCropRectangle ),
60 : m_rBindings ( rBindings ),
61 0 : m_dResolution ( 96.0 )
62 : {
63 0 : Initialize();
64 0 : }
65 :
66 0 : CompressGraphicsDialog::~CompressGraphicsDialog()
67 : {
68 0 : disposeOnce();
69 0 : }
70 :
71 0 : void CompressGraphicsDialog::dispose()
72 : {
73 0 : m_pLabelGraphicType.clear();
74 0 : m_pFixedText2.clear();
75 0 : m_pFixedText3.clear();
76 0 : m_pFixedText5.clear();
77 0 : m_pFixedText6.clear();
78 0 : m_pReduceResolutionCB.clear();
79 0 : m_pMFNewWidth.clear();
80 0 : m_pMFNewHeight.clear();
81 0 : m_pResolutionLB.clear();
82 0 : m_pLosslessRB.clear();
83 0 : m_pJpegCompRB.clear();
84 0 : m_pCompressionMF.clear();
85 0 : m_pQualityMF.clear();
86 0 : m_pBtnCalculate.clear();
87 0 : m_pInterpolationCombo.clear();
88 0 : ModalDialog::dispose();
89 0 : }
90 :
91 0 : void CompressGraphicsDialog::Initialize()
92 : {
93 0 : get(m_pLabelGraphicType, "label-graphic-type");
94 0 : get(m_pFixedText2, "label-original-size");
95 0 : get(m_pFixedText3, "label-view-size");
96 0 : get(m_pFixedText5, "label-image-capacity");
97 0 : get(m_pFixedText6, "label-new-capacity");
98 0 : get(m_pJpegCompRB, "radio-jpeg");
99 0 : get(m_pCompressionMF, "spin-compression");
100 0 : get(m_pCompressionSlider, "scale-compression");
101 0 : get(m_pLosslessRB, "radio-lossless");
102 0 : get(m_pQualityMF, "spin-quality");
103 0 : get(m_pQualitySlider, "scale-quality");
104 0 : get(m_pReduceResolutionCB, "checkbox-reduce-resolution");
105 0 : get(m_pMFNewWidth, "spin-new-width");
106 0 : get(m_pMFNewHeight, "spin-new-height");
107 0 : get(m_pResolutionLB, "combo-resolution");
108 0 : get(m_pBtnCalculate, "calculate");
109 0 : get(m_pInterpolationCombo, "interpolation-method-combo");
110 :
111 0 : m_pInterpolationCombo->SelectEntry( OUString("Lanczos") );
112 :
113 0 : m_pMFNewWidth->SetModifyHdl( LINK( this, CompressGraphicsDialog, NewWidthModifiedHdl ));
114 0 : m_pMFNewHeight->SetModifyHdl( LINK( this, CompressGraphicsDialog, NewHeightModifiedHdl ));
115 :
116 0 : m_pResolutionLB->SetModifyHdl( LINK( this, CompressGraphicsDialog, ResolutionModifiedHdl ));
117 0 : m_pBtnCalculate->SetClickHdl( LINK( this, CompressGraphicsDialog, CalculateClickHdl ) );
118 :
119 0 : m_pLosslessRB->SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
120 0 : m_pJpegCompRB->SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
121 :
122 0 : m_pReduceResolutionCB->SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleReduceResolutionRB ) );
123 :
124 0 : m_pQualitySlider->SetLinkedField(m_pQualityMF);
125 0 : m_pCompressionSlider->SetLinkedField(m_pCompressionMF);
126 :
127 0 : m_pJpegCompRB->Check();
128 0 : m_pReduceResolutionCB->Check();
129 :
130 0 : UpdateNewWidthMF();
131 0 : UpdateNewHeightMF();
132 0 : UpdateResolutionLB();
133 0 : Update();
134 0 : }
135 :
136 0 : void CompressGraphicsDialog::Update()
137 : {
138 0 : GfxLinkType aLinkType = m_aGraphic.GetLink().GetType();
139 0 : OUString aGraphicTypeString;
140 0 : switch(aLinkType)
141 : {
142 : case GFX_LINK_TYPE_NATIVE_GIF:
143 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_GIF);
144 0 : break;
145 : case GFX_LINK_TYPE_NATIVE_JPG:
146 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_JPEG);
147 0 : break;
148 : case GFX_LINK_TYPE_NATIVE_PNG:
149 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_PNG);
150 0 : break;
151 : case GFX_LINK_TYPE_NATIVE_TIF:
152 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_TIFF);
153 0 : break;
154 : case GFX_LINK_TYPE_NATIVE_WMF:
155 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_WMF);
156 0 : break;
157 : case GFX_LINK_TYPE_NATIVE_MET:
158 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_MET);
159 0 : break;
160 : case GFX_LINK_TYPE_NATIVE_PCT:
161 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_PCT);
162 0 : break;
163 : case GFX_LINK_TYPE_NATIVE_SVG:
164 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_SVG);
165 0 : break;
166 : case GFX_LINK_TYPE_NATIVE_BMP:
167 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_BMP);
168 0 : break;
169 : default:
170 0 : aGraphicTypeString = SVX_RESSTR(STR_IMAGE_UNKNOWN);
171 0 : break;
172 : }
173 0 : m_pLabelGraphicType->SetText(aGraphicTypeString);
174 :
175 0 : const FieldUnit eFieldUnit = m_rBindings.GetDispatcher()->GetModule()->GetFieldUnit();
176 0 : const LocaleDataWrapper& rLocaleWrapper( Application::GetSettings().GetLocaleDataWrapper() );
177 0 : sal_Unicode cSeparator = rLocaleWrapper.getNumDecimalSep()[0];
178 :
179 0 : VclPtr<VirtualDevice> pDummyVDev = VclPtr<VirtualDevice>::Create();
180 0 : pDummyVDev->EnableOutput( false );
181 0 : pDummyVDev->SetMapMode( m_aGraphic.GetPrefMapMode() );
182 :
183 0 : Size aPixelSize = m_aGraphic.GetSizePixel();
184 0 : Size aOriginalSize100mm( pDummyVDev->PixelToLogic( m_aGraphic.GetSizePixel(), MAP_100TH_MM ) );
185 :
186 0 : OUString aBitmapSizeString = SVX_RESSTR(STR_IMAGE_ORIGINAL_SIZE);
187 0 : OUString aWidthString = GetUnitString( aOriginalSize100mm.Width(), eFieldUnit, cSeparator );
188 0 : OUString aHeightString = GetUnitString( aOriginalSize100mm.Height(), eFieldUnit, cSeparator );
189 0 : aBitmapSizeString = aBitmapSizeString.replaceAll("$(WIDTH)", aWidthString);
190 0 : aBitmapSizeString = aBitmapSizeString.replaceAll("$(HEIGHT)", aHeightString);
191 0 : aBitmapSizeString = aBitmapSizeString.replaceAll("$(WIDTH_IN_PX)", OUString::number(aPixelSize.Width()));
192 0 : aBitmapSizeString = aBitmapSizeString.replaceAll("$(HEIGHT_IN_PX)", OUString::number(aPixelSize.Height()));
193 0 : m_pFixedText2->SetText(aBitmapSizeString);
194 :
195 0 : int aValX = (int) (aPixelSize.Width() / GetViewWidthInch());
196 :
197 0 : OUString aViewSizeString = SVX_RESSTR(STR_IMAGE_VIEW_SIZE);
198 :
199 0 : aWidthString = GetUnitString( m_aViewSize100mm.Width(), eFieldUnit, cSeparator );
200 0 : aHeightString = GetUnitString( m_aViewSize100mm.Height(), eFieldUnit, cSeparator );
201 0 : aViewSizeString = aViewSizeString.replaceAll("$(WIDTH)", aWidthString);
202 0 : aViewSizeString = aViewSizeString.replaceAll("$(HEIGHT)", aHeightString);
203 0 : aViewSizeString = aViewSizeString.replaceAll("$(DPI)", OUString::number(aValX));
204 0 : m_pFixedText3->SetText(aViewSizeString);
205 :
206 0 : SvMemoryStream aMemStream;
207 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
208 0 : m_aGraphic.ExportNative(aMemStream);
209 0 : aMemStream.Seek( STREAM_SEEK_TO_END );
210 0 : sal_Int32 aNativeSize = aMemStream.Tell();
211 :
212 0 : OUString aNativeSizeString = SVX_RESSTR(STR_IMAGE_CAPACITY);
213 0 : aNativeSizeString = aNativeSizeString.replaceAll("$(CAPACITY)", OUString::number(aNativeSize / 1024));
214 0 : m_pFixedText5->SetText(aNativeSizeString);
215 :
216 0 : m_pFixedText6->SetText("??");
217 0 : }
218 :
219 0 : void CompressGraphicsDialog::UpdateNewWidthMF()
220 : {
221 0 : int nPixelX = (sal_Int32)( GetViewWidthInch() * m_dResolution );
222 0 : m_pMFNewWidth->SetText( OUString::number( nPixelX ));
223 0 : }
224 :
225 0 : void CompressGraphicsDialog::UpdateNewHeightMF()
226 : {
227 0 : int nPixelY = (sal_Int32)( GetViewHeightInch() * m_dResolution );
228 0 : m_pMFNewHeight->SetText( OUString::number( nPixelY ));
229 0 : }
230 :
231 0 : void CompressGraphicsDialog::UpdateResolutionLB()
232 : {
233 0 : m_pResolutionLB->SetText( OUString::number( (sal_Int32) m_dResolution ) );
234 0 : }
235 :
236 0 : double CompressGraphicsDialog::GetViewWidthInch()
237 : {
238 0 : return (double) MetricField::ConvertValue(m_aViewSize100mm.Width(), 2, MAP_100TH_MM, FUNIT_INCH) / 100.0;
239 : }
240 :
241 0 : double CompressGraphicsDialog::GetViewHeightInch()
242 : {
243 0 : return (double) MetricField::ConvertValue(m_aViewSize100mm.Height(), 2, MAP_100TH_MM, FUNIT_INCH) / 100.0;
244 : }
245 :
246 0 : BmpScaleFlag CompressGraphicsDialog::GetSelectedInterpolationType()
247 : {
248 0 : OUString aSelectionText = OUString( m_pInterpolationCombo->GetSelectEntry() );
249 :
250 0 : if( aSelectionText == "Lanczos" ) {
251 0 : return BmpScaleFlag::Lanczos;
252 0 : } else if( aSelectionText == "Bilinear" ) {
253 0 : return BmpScaleFlag::BiLinear;
254 0 : } else if( aSelectionText == "Bicubic" ) {
255 0 : return BmpScaleFlag::BiCubic;
256 0 : } else if ( aSelectionText == "None" ) {
257 0 : return BmpScaleFlag::Fast;
258 : }
259 0 : return BmpScaleFlag::BestQuality;
260 : }
261 :
262 0 : void CompressGraphicsDialog::Compress(SvStream& aStream)
263 : {
264 0 : BitmapEx aBitmap = m_aGraphic.GetBitmapEx();
265 0 : if ( m_pReduceResolutionCB->IsChecked() )
266 : {
267 0 : long nPixelX = (long)( GetViewWidthInch() * m_dResolution );
268 0 : long nPixelY = (long)( GetViewHeightInch() * m_dResolution );
269 :
270 0 : aBitmap.Scale( Size( nPixelX, nPixelY ), GetSelectedInterpolationType() );
271 : }
272 0 : Graphic aScaledGraphic( aBitmap );
273 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
274 :
275 0 : Sequence< PropertyValue > aFilterData( 3 );
276 0 : aFilterData[ 0 ].Name = "Interlaced";
277 0 : aFilterData[ 0 ].Value <<= (sal_Int32) 0;
278 0 : aFilterData[ 1 ].Name = "Compression";
279 0 : aFilterData[ 1 ].Value <<= (sal_Int32) m_pCompressionMF->GetValue();
280 0 : aFilterData[ 2 ].Name = "Quality";
281 0 : aFilterData[ 2 ].Value <<= (sal_Int32) m_pQualityMF->GetValue();
282 :
283 0 : OUString aGraphicFormatName = m_pLosslessRB->IsChecked() ? OUString( "png" ) : OUString( "jpg" );
284 :
285 0 : sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aGraphicFormatName );
286 0 : rFilter.ExportGraphic( aScaledGraphic, OUString( "none" ), aStream, nFilterFormat, &aFilterData );
287 0 : }
288 :
289 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, NewWidthModifiedHdl )
290 : {
291 0 : m_dResolution = m_pMFNewWidth->GetValue() / GetViewWidthInch();
292 :
293 0 : UpdateNewHeightMF();
294 0 : UpdateResolutionLB();
295 0 : Update();
296 :
297 0 : return 0L;
298 : }
299 :
300 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, NewHeightModifiedHdl )
301 : {
302 0 : m_dResolution = m_pMFNewHeight->GetValue() / GetViewHeightInch();
303 :
304 0 : UpdateNewWidthMF();
305 0 : UpdateResolutionLB();
306 0 : Update();
307 :
308 0 : return 0L;
309 : }
310 :
311 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ResolutionModifiedHdl )
312 : {
313 0 : m_dResolution = (double) m_pResolutionLB->GetText().toInt32();
314 :
315 0 : UpdateNewWidthMF();
316 0 : UpdateNewHeightMF();
317 0 : Update();
318 :
319 0 : return 0L;
320 : }
321 :
322 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ToggleCompressionRB )
323 : {
324 0 : bool choice = m_pLosslessRB->IsChecked();
325 0 : m_pCompressionMF->Enable(choice);
326 0 : m_pCompressionSlider->Enable(choice);
327 0 : m_pQualityMF->Enable(!choice);
328 0 : m_pQualitySlider->Enable(!choice);
329 0 : return 0L;
330 : }
331 :
332 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ToggleReduceResolutionRB )
333 : {
334 0 : bool choice = m_pReduceResolutionCB->IsChecked();
335 0 : m_pMFNewWidth->Enable(choice);
336 0 : m_pMFNewHeight->Enable(choice);
337 0 : m_pResolutionLB->Enable(choice);
338 0 : m_pInterpolationCombo->Enable(choice);
339 :
340 0 : return 0L;
341 : }
342 :
343 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, CalculateClickHdl )
344 : {
345 0 : sal_Int32 aSize = 0;
346 :
347 0 : if ( m_dResolution > 0.0 )
348 : {
349 0 : SvMemoryStream aMemStream;
350 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
351 0 : Compress( aMemStream );
352 0 : aMemStream.Seek( STREAM_SEEK_TO_END );
353 0 : aSize = aMemStream.Tell();
354 : }
355 :
356 0 : if ( aSize > 0 )
357 : {
358 0 : OUString aSizeAsString = OUString::number(aSize / 1024);
359 :
360 0 : OUString aNewSizeString = SVX_RESSTR(STR_IMAGE_CAPACITY);
361 0 : aNewSizeString = aNewSizeString.replaceAll("$(CAPACITY)", aSizeAsString);
362 0 : m_pFixedText6->SetText(aNewSizeString);
363 : }
364 0 : return 0L;
365 : }
366 :
367 0 : Rectangle CompressGraphicsDialog::GetScaledCropRectangle()
368 : {
369 0 : if ( m_pReduceResolutionCB->IsChecked() )
370 : {
371 0 : long nPixelX = (long)( GetViewWidthInch() * m_dResolution );
372 0 : long nPixelY = (long)( GetViewHeightInch() * m_dResolution );
373 0 : Size aSize = m_aGraphic.GetBitmapEx().GetSizePixel();
374 0 : double aScaleX = nPixelX / (double) aSize.Width();
375 0 : double aScaleY = nPixelY / (double) aSize.Height();
376 :
377 : return Rectangle(
378 0 : m_aCropRectangle.Left() * aScaleX,
379 0 : m_aCropRectangle.Top() * aScaleY,
380 0 : m_aCropRectangle.Right() * aScaleX,
381 0 : m_aCropRectangle.Bottom()* aScaleY);
382 : }
383 : else
384 : {
385 0 : return m_aCropRectangle;
386 : }
387 : }
388 :
389 0 : Graphic CompressGraphicsDialog::GetCompressedGraphic()
390 : {
391 0 : if ( m_dResolution > 0.0 )
392 : {
393 0 : SvMemoryStream aMemStream;
394 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
395 0 : Compress( aMemStream );
396 0 : aMemStream.Seek( STREAM_SEEK_TO_BEGIN );
397 0 : Graphic aResultGraphic;
398 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
399 0 : rFilter.ImportGraphic( aResultGraphic, OUString("import"), aMemStream );
400 :
401 0 : return aResultGraphic;
402 : }
403 0 : return Graphic();
404 : }
405 :
406 0 : SdrGrafObj* CompressGraphicsDialog::GetCompressedSdrGrafObj()
407 : {
408 0 : if ( m_dResolution > 0.0 )
409 : {
410 0 : SdrGrafObj* pNewObject = m_pGraphicObj->Clone();
411 :
412 0 : if ( m_pReduceResolutionCB->IsChecked() )
413 : {
414 0 : Rectangle aScaledCropedRectangle = GetScaledCropRectangle();
415 : SdrGrafCropItem aNewCrop(
416 0 : aScaledCropedRectangle.Left(),
417 0 : aScaledCropedRectangle.Top(),
418 0 : aScaledCropedRectangle.Right(),
419 0 : aScaledCropedRectangle.Bottom());
420 :
421 0 : pNewObject->SetMergedItem(aNewCrop);
422 : }
423 0 : pNewObject->SetGraphic( GetCompressedGraphic() );
424 :
425 0 : return pNewObject;
426 : }
427 0 : return NULL;
428 390 : }
429 :
430 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|