1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
35 #include "compressgraphicdialog.hrc"
37 using namespace com::sun::star::uno
;
38 using namespace com::sun::star::beans
;
42 CompressGraphicsDialog::CompressGraphicsDialog( Window
* pParent
, SdrGrafObj
* pGraphicObj
, SfxBindings
& rBindings
) :
43 ModalDialog ( pParent
, "CompressGraphicDialog", "svx/ui/compressgraphicdialog.ui" ),
44 m_pGraphicObj ( pGraphicObj
),
45 m_aGraphic ( pGraphicObj
->GetGraphicObject().GetGraphic() ),
46 m_aViewSize100mm ( pGraphicObj
->GetLogicRect().GetSize() ),
47 m_rBindings ( rBindings
),
48 m_dResolution ( 96.0 )
50 const SdrGrafCropItem
& rCrop
= (const SdrGrafCropItem
&) m_pGraphicObj
->GetMergedItem(SDRATTR_GRAFCROP
);
51 m_aCropRectangle
= Rectangle(rCrop
.GetLeft(), rCrop
.GetTop(), rCrop
.GetRight(), rCrop
.GetBottom());
56 CompressGraphicsDialog::CompressGraphicsDialog( Window
* pParent
, Graphic
& rGraphic
, Size rViewSize100mm
, Rectangle
& rCropRectangle
, SfxBindings
& rBindings
) :
57 ModalDialog ( pParent
, "CompressGraphicDialog", "svx/ui/compressgraphicdialog.ui" ),
58 m_pGraphicObj ( NULL
),
59 m_aGraphic ( rGraphic
),
60 m_aViewSize100mm ( rViewSize100mm
),
61 m_aCropRectangle ( rCropRectangle
),
62 m_rBindings ( rBindings
),
63 m_dResolution ( 96.0 )
68 void CompressGraphicsDialog::Initialize()
70 get(m_pLabelGraphicType
, "label-graphic-type");
71 get(m_pFixedText2
, "label-original-size");
72 get(m_pFixedText3
, "label-view-size");
73 get(m_pFixedText5
, "label-image-capacity");
74 get(m_pFixedText6
, "label-new-capacity");
75 get(m_pJpegCompRB
, "radio-jpeg");
76 get(m_pCompressionMF
, "spin-compression");
77 get(m_pLosslessRB
, "radio-lossless");
78 get(m_pQualityMF
, "spin-quality");
79 get(m_pReduceResolutionCB
, "checkbox-reduce-resolution");
80 get(m_pMFNewWidth
, "spin-new-width");
81 get(m_pMFNewHeight
, "spin-new-height");
82 get(m_pResolutionLB
, "combo-resolution");
83 get(m_pBtnCalculate
, "calculate");
84 get(m_pInterpolationCombo
, "interpolation-method-combo");
86 m_pInterpolationCombo
->SelectEntry( OUString("Lanczos") );
88 m_pMFNewWidth
->SetModifyHdl( LINK( this, CompressGraphicsDialog
, NewWidthModifiedHdl
));
89 m_pMFNewHeight
->SetModifyHdl( LINK( this, CompressGraphicsDialog
, NewHeightModifiedHdl
));
91 m_pResolutionLB
->SetModifyHdl( LINK( this, CompressGraphicsDialog
, ResolutionModifiedHdl
));
92 m_pBtnCalculate
->SetClickHdl( LINK( this, CompressGraphicsDialog
, CalculateClickHdl
) );
94 m_pLosslessRB
->SetToggleHdl( LINK( this, CompressGraphicsDialog
, ToggleCompressionRB
) );
95 m_pJpegCompRB
->SetToggleHdl( LINK( this, CompressGraphicsDialog
, ToggleCompressionRB
) );
97 m_pReduceResolutionCB
->SetToggleHdl( LINK( this, CompressGraphicsDialog
, ToggleReduceResolutionRB
) );
99 m_pJpegCompRB
->Check();
100 m_pReduceResolutionCB
->Check();
104 UpdateResolutionLB();
108 CompressGraphicsDialog::~CompressGraphicsDialog()
112 void CompressGraphicsDialog::Update()
114 GfxLinkType aLinkType
= m_aGraphic
.GetLink().GetType();
115 OUString aGraphicTypeString
;
118 case GFX_LINK_TYPE_NATIVE_GIF
:
119 aGraphicTypeString
= "Gif image";
121 case GFX_LINK_TYPE_NATIVE_JPG
:
122 aGraphicTypeString
= "Jpeg image";
124 case GFX_LINK_TYPE_NATIVE_PNG
:
125 aGraphicTypeString
= "PNG image";
127 case GFX_LINK_TYPE_NATIVE_TIF
:
128 aGraphicTypeString
= "TIFF image";
130 case GFX_LINK_TYPE_NATIVE_WMF
:
131 aGraphicTypeString
= "WMF image";
133 case GFX_LINK_TYPE_NATIVE_MET
:
134 aGraphicTypeString
= "MET image";
136 case GFX_LINK_TYPE_NATIVE_PCT
:
137 aGraphicTypeString
= "PCT image";
139 case GFX_LINK_TYPE_NATIVE_SVG
:
140 aGraphicTypeString
= "SVG image";
142 case GFX_LINK_TYPE_NATIVE_BMP
:
143 aGraphicTypeString
= "BMP image";
146 aGraphicTypeString
= "Unknown";
149 m_pLabelGraphicType
->SetText(aGraphicTypeString
);
151 const FieldUnit eFieldUnit
= m_rBindings
.GetDispatcher()->GetModule()->GetFieldUnit();
152 const LocaleDataWrapper
& rLocaleWrapper( Application::GetSettings().GetLocaleDataWrapper() );
153 sal_Unicode cSeparator
= rLocaleWrapper
.getNumDecimalSep()[0];
155 VirtualDevice
* pDummyVDev
= new VirtualDevice();
156 pDummyVDev
->EnableOutput( false );
157 pDummyVDev
->SetMapMode( m_aGraphic
.GetPrefMapMode() );
159 Size aPixelSize
= m_aGraphic
.GetSizePixel();
160 Size
aOriginalSize100mm( pDummyVDev
->PixelToLogic( m_aGraphic
.GetSizePixel(), MAP_100TH_MM
) );
162 OUString aBitmapSizeString
= SVX_RESSTR(STR_IMAGE_ORIGINAL_SIZE
);
163 OUString aWidthString
= GetUnitString( aOriginalSize100mm
.Width(), eFieldUnit
, cSeparator
);
164 OUString aHeightString
= GetUnitString( aOriginalSize100mm
.Height(), eFieldUnit
, cSeparator
);
165 aBitmapSizeString
= aBitmapSizeString
.replaceAll("$(WIDTH)", aWidthString
);
166 aBitmapSizeString
= aBitmapSizeString
.replaceAll("$(HEIGHT)", aHeightString
);
167 aBitmapSizeString
= aBitmapSizeString
.replaceAll("$(WIDTH_IN_PX)", OUString::number(aPixelSize
.Width()));
168 aBitmapSizeString
= aBitmapSizeString
.replaceAll("$(HEIGHT_IN_PX)", OUString::number(aPixelSize
.Height()));
169 m_pFixedText2
->SetText(aBitmapSizeString
);
171 int aValX
= (int) (aPixelSize
.Width() / GetViewWidthInch());
173 OUString aViewSizeString
= SVX_RESSTR(STR_IMAGE_VIEW_SIZE
);
175 aWidthString
= GetUnitString( m_aViewSize100mm
.Width(), eFieldUnit
, cSeparator
);
176 aHeightString
= GetUnitString( m_aViewSize100mm
.Height(), eFieldUnit
, cSeparator
);
177 aViewSizeString
= aViewSizeString
.replaceAll("$(WIDTH)", aWidthString
);
178 aViewSizeString
= aViewSizeString
.replaceAll("$(HEIGHT)", aHeightString
);
179 aViewSizeString
= aViewSizeString
.replaceAll("$(DPI)", OUString::number(aValX
));
180 m_pFixedText3
->SetText(aViewSizeString
);
182 SvMemoryStream aMemStream
;
183 aMemStream
.SetVersion( SOFFICE_FILEFORMAT_CURRENT
);
184 m_aGraphic
.ExportNative(aMemStream
);
185 aMemStream
.Seek( STREAM_SEEK_TO_END
);
186 sal_Int32 aNativeSize
= aMemStream
.Tell();
188 OUString aNativeSizeString
= SVX_RESSTR(STR_IMAGE_CAPACITY
);
189 aNativeSizeString
= aNativeSizeString
.replaceAll("$(CAPACITY)", OUString::number(aNativeSize
/ 1024));
190 m_pFixedText5
->SetText(aNativeSizeString
);
192 m_pFixedText6
->SetText("??");
195 void CompressGraphicsDialog::UpdateNewWidthMF()
197 int nPixelX
= (sal_Int32
)( GetViewWidthInch() * m_dResolution
);
198 m_pMFNewWidth
->SetText( OUString::number( nPixelX
));
201 void CompressGraphicsDialog::UpdateNewHeightMF()
203 int nPixelY
= (sal_Int32
)( GetViewHeightInch() * m_dResolution
);
204 m_pMFNewHeight
->SetText( OUString::number( nPixelY
));
207 void CompressGraphicsDialog::UpdateResolutionLB()
209 m_pResolutionLB
->SetText( OUString::number( (sal_Int32
) m_dResolution
) );
212 double CompressGraphicsDialog::GetViewWidthInch()
214 return (double) MetricField::ConvertValue(m_aViewSize100mm
.Width(), 2, MAP_100TH_MM
, FUNIT_INCH
) / 100.0;
217 double CompressGraphicsDialog::GetViewHeightInch()
219 return (double) MetricField::ConvertValue(m_aViewSize100mm
.Height(), 2, MAP_100TH_MM
, FUNIT_INCH
) / 100.0;
222 sal_uLong
CompressGraphicsDialog::GetSelectedInterpolationType()
224 OUString aSelectionText
= OUString( m_pInterpolationCombo
->GetSelectEntry() );
226 if( aSelectionText
== "Lanczos" ) {
227 return BMP_SCALE_LANCZOS
;
228 } else if( aSelectionText
== "Bilinear" ) {
229 return BMP_SCALE_BILINEAR
;
230 } else if( aSelectionText
== "Bicubic" ) {
231 return BMP_SCALE_BICUBIC
;
232 } else if ( aSelectionText
== "None" ) {
233 return BMP_SCALE_FAST
;
235 return BMP_SCALE_BESTQUALITY
;
238 void CompressGraphicsDialog::Compress(SvStream
& aStream
)
240 BitmapEx aBitmap
= m_aGraphic
.GetBitmapEx();
241 if ( m_pReduceResolutionCB
->IsChecked() )
243 long nPixelX
= (long)( GetViewWidthInch() * m_dResolution
);
244 long nPixelY
= (long)( GetViewHeightInch() * m_dResolution
);
246 aBitmap
.Scale( Size( nPixelX
, nPixelY
), GetSelectedInterpolationType() );
248 Graphic
aScaledGraphic( aBitmap
);
249 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
251 Sequence
< PropertyValue
> aFilterData( 3 );
252 aFilterData
[ 0 ].Name
= "Interlaced";
253 aFilterData
[ 0 ].Value
<<= (sal_Int32
) 0;
254 aFilterData
[ 1 ].Name
= "Compression";
255 aFilterData
[ 1 ].Value
<<= (sal_Int32
) m_pCompressionMF
->GetValue();
256 aFilterData
[ 2 ].Name
= "Quality";
257 aFilterData
[ 2 ].Value
<<= (sal_Int32
) m_pQualityMF
->GetValue();
259 OUString aGraphicFormatName
= m_pLosslessRB
->IsChecked() ? OUString( "png" ) : OUString( "jpg" );
261 sal_uInt16 nFilterFormat
= rFilter
.GetExportFormatNumberForShortName( aGraphicFormatName
);
262 rFilter
.ExportGraphic( aScaledGraphic
, OUString( "none" ), aStream
, nFilterFormat
, &aFilterData
);
265 IMPL_LINK_NOARG( CompressGraphicsDialog
, NewWidthModifiedHdl
)
267 m_dResolution
= m_pMFNewWidth
->GetValue() / GetViewWidthInch();
270 UpdateResolutionLB();
276 IMPL_LINK_NOARG( CompressGraphicsDialog
, NewHeightModifiedHdl
)
278 m_dResolution
= m_pMFNewHeight
->GetValue() / GetViewHeightInch();
281 UpdateResolutionLB();
287 IMPL_LINK_NOARG( CompressGraphicsDialog
, ResolutionModifiedHdl
)
289 m_dResolution
= (double) m_pResolutionLB
->GetText().toInt32();
298 IMPL_LINK_NOARG( CompressGraphicsDialog
, ToggleCompressionRB
)
300 bool choice
= m_pLosslessRB
->IsChecked();
301 m_pCompressionMF
->Enable(choice
);
302 m_pQualityMF
->Enable(!choice
);
307 IMPL_LINK_NOARG( CompressGraphicsDialog
, ToggleReduceResolutionRB
)
309 bool choice
= m_pReduceResolutionCB
->IsChecked();
310 m_pMFNewWidth
->Enable(choice
);
311 m_pMFNewHeight
->Enable(choice
);
312 m_pResolutionLB
->Enable(choice
);
313 m_pInterpolationCombo
->Enable(choice
);
318 IMPL_LINK_NOARG( CompressGraphicsDialog
, CalculateClickHdl
)
322 if ( m_dResolution
> 0.0 )
324 SvMemoryStream aMemStream
;
325 aMemStream
.SetVersion( SOFFICE_FILEFORMAT_CURRENT
);
326 Compress( aMemStream
);
327 aMemStream
.Seek( STREAM_SEEK_TO_END
);
328 aSize
= aMemStream
.Tell();
333 OUString aSizeAsString
= OUString::number(aSize
/ 1024);
335 OUString aNewSizeString
= SVX_RESSTR(STR_IMAGE_CAPACITY
);
336 aNewSizeString
= aNewSizeString
.replaceAll("$(CAPACITY)", aSizeAsString
);
337 m_pFixedText6
->SetText(aNewSizeString
);
342 Rectangle
CompressGraphicsDialog::GetScaledCropRectangle()
344 if ( m_pReduceResolutionCB
->IsChecked() )
346 long nPixelX
= (long)( GetViewWidthInch() * m_dResolution
);
347 long nPixelY
= (long)( GetViewHeightInch() * m_dResolution
);
348 Size aSize
= m_aGraphic
.GetBitmapEx().GetSizePixel();
349 double aScaleX
= nPixelX
/ (double) aSize
.Width();
350 double aScaleY
= nPixelY
/ (double) aSize
.Height();
353 m_aCropRectangle
.Left() * aScaleX
,
354 m_aCropRectangle
.Top() * aScaleY
,
355 m_aCropRectangle
.Right() * aScaleX
,
356 m_aCropRectangle
.Bottom()* aScaleY
);
360 return m_aCropRectangle
;
364 Graphic
CompressGraphicsDialog::GetCompressedGraphic()
366 if ( m_dResolution
> 0.0 )
368 SvMemoryStream aMemStream
;
369 aMemStream
.SetVersion( SOFFICE_FILEFORMAT_CURRENT
);
370 Compress( aMemStream
);
371 aMemStream
.Seek( STREAM_SEEK_TO_BEGIN
);
372 Graphic aResultGraphic
;
373 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
374 rFilter
.ImportGraphic( aResultGraphic
, OUString("import"), aMemStream
);
376 return aResultGraphic
;
381 SdrGrafObj
* CompressGraphicsDialog::GetCompressedSdrGrafObj()
383 if ( m_dResolution
> 0.0 )
385 SdrGrafObj
* pNewObject
= (SdrGrafObj
*) m_pGraphicObj
->Clone();
387 if ( m_pReduceResolutionCB
->IsChecked() )
389 Rectangle aScaledCropedRectangle
= GetScaledCropRectangle();
390 SdrGrafCropItem
aNewCrop(
391 aScaledCropedRectangle
.Left(),
392 aScaledCropedRectangle
.Top(),
393 aScaledCropedRectangle
.Right(),
394 aScaledCropedRectangle
.Bottom());
396 pNewObject
->SetMergedItem(aNewCrop
);
398 pNewObject
->SetGraphic( GetCompressedGraphic() );
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */