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 <svx/svdograf.hxx>
26 #include <svx/sdgcpitm.hxx>
27 #include <svx/dialmgr.hxx>
28 #include <svx/compressgraphicdialog.hxx>
29 #include <sfx2/dispatch.hxx>
30 #include <comphelper/processfactory.hxx>
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include "compressgraphicdialog.hrc"
36 using namespace com::sun::star::uno
;
37 using namespace com::sun::star::beans
;
41 CompressGraphicsDialog::CompressGraphicsDialog( Window
* pParent
, SdrGrafObj
* pGraphicObj
, SfxBindings
& rBindings
) :
42 ModalDialog ( pParent
, "CompressGraphicDialog", "svx/ui/compressgraphicdialog.ui" ),
43 m_pGraphicObj ( pGraphicObj
),
44 m_aGraphic ( pGraphicObj
->GetGraphicObject().GetGraphic() ),
45 m_aViewSize100mm ( pGraphicObj
->GetLogicRect().GetSize() ),
46 m_rBindings ( rBindings
),
47 m_dResolution ( 96.0 )
49 const SdrGrafCropItem
& rCrop
= (const SdrGrafCropItem
&) m_pGraphicObj
->GetMergedItem(SDRATTR_GRAFCROP
);
50 m_aCropRectangle
= Rectangle(rCrop
.GetLeft(), rCrop
.GetTop(), rCrop
.GetRight(), rCrop
.GetBottom());
55 CompressGraphicsDialog::CompressGraphicsDialog( Window
* pParent
, Graphic
& rGraphic
, Size rViewSize100mm
, Rectangle
& rCropRectangle
, SfxBindings
& rBindings
) :
56 ModalDialog ( pParent
, "CompressGraphicDialog", "svx/ui/compressgraphicdialog.ui" ),
57 m_pGraphicObj ( NULL
),
58 m_aGraphic ( rGraphic
),
59 m_aViewSize100mm ( rViewSize100mm
),
60 m_aCropRectangle ( rCropRectangle
),
61 m_rBindings ( rBindings
),
62 m_dResolution ( 96.0 )
67 void CompressGraphicsDialog::Initialize()
69 get(m_pLabelGraphicType
, "label-graphic-type");
70 get(m_pFixedText2
, "label-original-size");
71 get(m_pFixedText3
, "label-view-size");
72 get(m_pFixedText5
, "label-image-capacity");
73 get(m_pFixedText6
, "label-new-capacity");
74 get(m_pJpegCompRB
, "radio-jpeg");
75 get(m_pCompressionMF
, "spin-compression");
76 get(m_pLosslessRB
, "radio-lossless");
77 get(m_pQualityMF
, "spin-quality");
78 get(m_pReduceResolutionCB
, "checkbox-reduce-resolution");
79 get(m_pMFNewWidth
, "spin-new-width");
80 get(m_pMFNewHeight
, "spin-new-height");
81 get(m_pResolutionLB
, "combo-resolution");
82 get(m_pBtnCalculate
, "calculate");
83 get(m_pInterpolationCombo
, "interpolation-method-combo");
85 m_pInterpolationCombo
->SelectEntry( OUString("Lanczos") );
87 m_pMFNewWidth
->SetModifyHdl( LINK( this, CompressGraphicsDialog
, NewWidthModifiedHdl
));
88 m_pMFNewHeight
->SetModifyHdl( LINK( this, CompressGraphicsDialog
, NewHeightModifiedHdl
));
90 m_pResolutionLB
->SetModifyHdl( LINK( this, CompressGraphicsDialog
, ResolutionModifiedHdl
));
91 m_pBtnCalculate
->SetClickHdl( LINK( this, CompressGraphicsDialog
, CalculateClickHdl
) );
93 m_pLosslessRB
->SetToggleHdl( LINK( this, CompressGraphicsDialog
, ToggleCompressionRB
) );
94 m_pJpegCompRB
->SetToggleHdl( LINK( this, CompressGraphicsDialog
, ToggleCompressionRB
) );
96 m_pReduceResolutionCB
->SetToggleHdl( LINK( this, CompressGraphicsDialog
, ToggleReduceResolutionRB
) );
98 m_pJpegCompRB
->Check();
99 m_pReduceResolutionCB
->Check();
103 UpdateResolutionLB();
107 CompressGraphicsDialog::~CompressGraphicsDialog()
111 void CompressGraphicsDialog::Update()
113 GfxLinkType aLinkType
= m_aGraphic
.GetLink().GetType();
114 OUString aGraphicTypeString
;
117 case GFX_LINK_TYPE_NATIVE_GIF
:
118 aGraphicTypeString
= "Gif image";
120 case GFX_LINK_TYPE_NATIVE_JPG
:
121 aGraphicTypeString
= "Jpeg image";
123 case GFX_LINK_TYPE_NATIVE_PNG
:
124 aGraphicTypeString
= "PNG image";
126 case GFX_LINK_TYPE_NATIVE_TIF
:
127 aGraphicTypeString
= "TIFF image";
129 case GFX_LINK_TYPE_NATIVE_WMF
:
130 aGraphicTypeString
= "WMF image";
132 case GFX_LINK_TYPE_NATIVE_MET
:
133 aGraphicTypeString
= "MET image";
135 case GFX_LINK_TYPE_NATIVE_PCT
:
136 aGraphicTypeString
= "PCT image";
138 case GFX_LINK_TYPE_NATIVE_SVG
:
139 aGraphicTypeString
= "SVG image";
142 aGraphicTypeString
= "Unknown";
145 m_pLabelGraphicType
->SetText(aGraphicTypeString
);
147 const FieldUnit eFieldUnit
= m_rBindings
.GetDispatcher()->GetModule()->GetFieldUnit();
148 const LocaleDataWrapper
& rLocaleWrapper( Application::GetSettings().GetLocaleDataWrapper() );
149 sal_Unicode cSeparator
= rLocaleWrapper
.getNumDecimalSep()[0];
151 VirtualDevice
* pDummyVDev
= new VirtualDevice();
152 pDummyVDev
->EnableOutput( false );
153 pDummyVDev
->SetMapMode( m_aGraphic
.GetPrefMapMode() );
155 Size aPixelSize
= m_aGraphic
.GetSizePixel();
156 Size
aOriginalSize100mm( pDummyVDev
->PixelToLogic( m_aGraphic
.GetSizePixel(), MAP_100TH_MM
) );
158 String aBitmapSizeString
= String(SVX_RES(STR_IMAGE_ORIGINAL_SIZE
));
159 OUString aWidthString
= GetUnitString( aOriginalSize100mm
.Width(), eFieldUnit
, cSeparator
);
160 OUString aHeightString
= GetUnitString( aOriginalSize100mm
.Height(), eFieldUnit
, cSeparator
);
161 aBitmapSizeString
.SearchAndReplaceAllAscii( "$(WIDTH)", aWidthString
);
162 aBitmapSizeString
.SearchAndReplaceAllAscii( "$(HEIGHT)", aHeightString
);
163 aBitmapSizeString
.SearchAndReplaceAllAscii( "$(WIDTH_IN_PX)", OUString::valueOf(aPixelSize
.Width()) );
164 aBitmapSizeString
.SearchAndReplaceAllAscii( "$(HEIGHT_IN_PX)", OUString::valueOf(aPixelSize
.Height()) );
165 m_pFixedText2
->SetText(aBitmapSizeString
);
167 int aValX
= (int) (aPixelSize
.Width() / GetViewWidthInch());
169 String aViewSizeString
= String(SVX_RES(STR_IMAGE_VIEW_SIZE
));
171 aWidthString
= GetUnitString( m_aViewSize100mm
.Width(), eFieldUnit
, cSeparator
);
172 aHeightString
= GetUnitString( m_aViewSize100mm
.Height(), eFieldUnit
, cSeparator
);
173 aViewSizeString
.SearchAndReplaceAllAscii( "$(WIDTH)", aWidthString
);
174 aViewSizeString
.SearchAndReplaceAllAscii( "$(HEIGHT)", aHeightString
);
175 aViewSizeString
.SearchAndReplaceAllAscii( "$(DPI)", OUString::number( aValX
) );
176 m_pFixedText3
->SetText(aViewSizeString
);
178 SvMemoryStream aMemStream
;
179 aMemStream
.SetVersion( SOFFICE_FILEFORMAT_CURRENT
);
180 m_aGraphic
.ExportNative(aMemStream
);
181 aMemStream
.Seek( STREAM_SEEK_TO_END
);
182 sal_Int32 aNativeSize
= aMemStream
.Tell();
184 String aNativeSizeString
= String(SVX_RES(STR_IMAGE_CAPACITY
));
185 aNativeSizeString
.SearchAndReplaceAllAscii( "$(CAPACITY)", OUString::valueOf(aNativeSize
/ 1024) );
186 m_pFixedText5
->SetText(aNativeSizeString
);
188 m_pFixedText6
->SetText(String("??"));
191 void CompressGraphicsDialog::UpdateNewWidthMF()
193 int nPixelX
= (sal_Int32
)( GetViewWidthInch() * m_dResolution
);
194 m_pMFNewWidth
->SetText( OUString::number( nPixelX
));
197 void CompressGraphicsDialog::UpdateNewHeightMF()
199 int nPixelY
= (sal_Int32
)( GetViewHeightInch() * m_dResolution
);
200 m_pMFNewHeight
->SetText( OUString::number( nPixelY
));
203 void CompressGraphicsDialog::UpdateResolutionLB()
205 m_pResolutionLB
->SetText( OUString::valueOf( (sal_Int32
) m_dResolution
) );
208 double CompressGraphicsDialog::GetViewWidthInch()
210 return (double) MetricField::ConvertValue(m_aViewSize100mm
.Width(), 2, MAP_100TH_MM
, FUNIT_INCH
) / 100.0;
213 double CompressGraphicsDialog::GetViewHeightInch()
215 return (double) MetricField::ConvertValue(m_aViewSize100mm
.Height(), 2, MAP_100TH_MM
, FUNIT_INCH
) / 100.0;
218 sal_uLong
CompressGraphicsDialog::GetSelectedInterpolationType()
220 OUString aSelectionText
= OUString( m_pInterpolationCombo
->GetSelectEntry() );
222 if( aSelectionText
== "Lanczos" ) {
223 return BMP_SCALE_LANCZOS
;
224 } else if( aSelectionText
== "Bilinear" ) {
225 return BMP_SCALE_BILINEAR
;
226 } else if( aSelectionText
== "Bicubic" ) {
227 return BMP_SCALE_BICUBIC
;
228 } else if ( aSelectionText
== "None" ) {
229 return BMP_SCALE_FAST
;
231 return BMP_SCALE_BESTQUALITY
;
234 void CompressGraphicsDialog::Compress(SvStream
& aStream
)
236 BitmapEx aBitmap
= m_aGraphic
.GetBitmapEx();
237 if ( m_pReduceResolutionCB
->IsChecked() )
239 long nPixelX
= (long)( GetViewWidthInch() * m_dResolution
);
240 long nPixelY
= (long)( GetViewHeightInch() * m_dResolution
);
242 aBitmap
.Scale( Size( nPixelX
, nPixelY
), GetSelectedInterpolationType() );
244 Graphic
aScaledGraphic( aBitmap
);
245 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
247 Sequence
< PropertyValue
> aFilterData( 3 );
248 aFilterData
[ 0 ].Name
= "Interlaced";
249 aFilterData
[ 0 ].Value
<<= (sal_Int32
) 0;
250 aFilterData
[ 1 ].Name
= "Compression";
251 aFilterData
[ 1 ].Value
<<= (sal_Int32
) m_pCompressionMF
->GetValue();
252 aFilterData
[ 2 ].Name
= "Quality";
253 aFilterData
[ 2 ].Value
<<= (sal_Int32
) m_pQualityMF
->GetValue();
255 String aGraphicFormatName
= m_pLosslessRB
->IsChecked() ? String( "png" ) : String( "jpg" );
257 sal_uInt16 nFilterFormat
= rFilter
.GetExportFormatNumberForShortName( aGraphicFormatName
);
258 rFilter
.ExportGraphic( aScaledGraphic
, OUString( "none" ), aStream
, nFilterFormat
, &aFilterData
);
261 IMPL_LINK_NOARG( CompressGraphicsDialog
, NewWidthModifiedHdl
)
263 m_dResolution
= m_pMFNewWidth
->GetValue() / GetViewWidthInch();
266 UpdateResolutionLB();
272 IMPL_LINK_NOARG( CompressGraphicsDialog
, NewHeightModifiedHdl
)
274 m_dResolution
= m_pMFNewHeight
->GetValue() / GetViewHeightInch();
277 UpdateResolutionLB();
283 IMPL_LINK_NOARG( CompressGraphicsDialog
, ResolutionModifiedHdl
)
285 m_dResolution
= (double) m_pResolutionLB
->GetText().toInt32();
294 IMPL_LINK_NOARG( CompressGraphicsDialog
, ToggleCompressionRB
)
296 bool choice
= m_pLosslessRB
->IsChecked();
297 m_pCompressionMF
->Enable(choice
);
298 m_pQualityMF
->Enable(!choice
);
303 IMPL_LINK_NOARG( CompressGraphicsDialog
, ToggleReduceResolutionRB
)
305 bool choice
= m_pReduceResolutionCB
->IsChecked();
306 m_pMFNewWidth
->Enable(choice
);
307 m_pMFNewHeight
->Enable(choice
);
308 m_pResolutionLB
->Enable(choice
);
309 m_pInterpolationCombo
->Enable(choice
);
314 IMPL_LINK_NOARG( CompressGraphicsDialog
, CalculateClickHdl
)
318 if ( m_dResolution
> 0.0 )
320 SvMemoryStream aMemStream
;
321 aMemStream
.SetVersion( SOFFICE_FILEFORMAT_CURRENT
);
322 Compress( aMemStream
);
323 aMemStream
.Seek( STREAM_SEEK_TO_END
);
324 aSize
= aMemStream
.Tell();
329 OUString aSizeAsString
= OUString::valueOf(aSize
/ 1024);
331 String aNewSizeString
= String(SVX_RES(STR_IMAGE_CAPACITY
));
332 aNewSizeString
.SearchAndReplaceAllAscii( "$(CAPACITY)", aSizeAsString
);
333 m_pFixedText6
->SetText(aNewSizeString
);
338 Rectangle
CompressGraphicsDialog::GetScaledCropRectangle()
340 if ( m_pReduceResolutionCB
->IsChecked() )
342 long nPixelX
= (long)( GetViewWidthInch() * m_dResolution
);
343 long nPixelY
= (long)( GetViewHeightInch() * m_dResolution
);
344 Size aSize
= m_aGraphic
.GetBitmapEx().GetSizePixel();
345 double aScaleX
= nPixelX
/ (double) aSize
.Width();
346 double aScaleY
= nPixelY
/ (double) aSize
.Height();
349 m_aCropRectangle
.Left() * aScaleX
,
350 m_aCropRectangle
.Top() * aScaleY
,
351 m_aCropRectangle
.Right() * aScaleX
,
352 m_aCropRectangle
.Bottom()* aScaleY
);
356 return m_aCropRectangle
;
360 Graphic
CompressGraphicsDialog::GetCompressedGraphic()
362 if ( m_dResolution
> 0.0 )
364 SvMemoryStream aMemStream
;
365 aMemStream
.SetVersion( SOFFICE_FILEFORMAT_CURRENT
);
366 Compress( aMemStream
);
367 aMemStream
.Seek( STREAM_SEEK_TO_BEGIN
);
368 Graphic aResultGraphic
;
369 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
370 rFilter
.ImportGraphic( aResultGraphic
, OUString("import"), aMemStream
);
372 return aResultGraphic
;
377 SdrGrafObj
* CompressGraphicsDialog::GetCompressedSdrGrafObj()
379 if ( m_dResolution
> 0.0 )
381 SdrGrafObj
* pNewObject
= (SdrGrafObj
*) m_pGraphicObj
->Clone();
383 if ( m_pReduceResolutionCB
->IsChecked() )
385 Rectangle aScaledCropedRectangle
= GetScaledCropRectangle();
386 SdrGrafCropItem
aNewCrop(
387 aScaledCropedRectangle
.Left(),
388 aScaledCropedRectangle
.Top(),
389 aScaledCropedRectangle
.Right(),
390 aScaledCropedRectangle
.Bottom());
392 pNewObject
->SetMergedItem(aNewCrop
);
394 pNewObject
->SetGraphic( GetCompressedGraphic() );
401 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */