LibreOffice Module svx (master)  1
compressgraphicdialog.cxx
Go to the documentation of this file.
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/fieldvalues.hxx>
22 #include <vcl/graph.hxx>
23 #include <vcl/graphicfilter.hxx>
24 #include <vcl/virdev.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/settings.hxx>
27 #include <vcl/weld.hxx>
28 #include <svx/strings.hrc>
29 #include <svx/svdograf.hxx>
30 #include <svx/sdgcpitm.hxx>
31 #include <svx/dialmgr.hxx>
32 #include <svx/graphichelper.hxx>
34 #include <sfx2/dispatch.hxx>
35 #include <sfx2/module.hxx>
36 #include <comphelper/fileformat.h>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include <tools/stream.hxx>
41 
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::beans;
44 
46  GenericDialogController( pParent, "svx/ui/compressgraphicdialog.ui", "CompressGraphicDialog" ),
47  m_xGraphicObj ( pGraphicObj ),
48  m_aGraphic ( pGraphicObj->GetGraphicObject().GetGraphic() ),
49  m_aViewSize100mm ( pGraphicObj->GetLogicRect().GetSize() ),
50  m_rBindings ( rBindings ),
51  m_dResolution ( 300 )
52 {
53  const SdrGrafCropItem& rCrop = m_xGraphicObj->GetMergedItem(SDRATTR_GRAFCROP);
54  m_aCropRectangle = tools::Rectangle(rCrop.GetLeft(), rCrop.GetTop(), rCrop.GetRight(), rCrop.GetBottom());
55 
56  Initialize();
57 }
58 
59 CompressGraphicsDialog::CompressGraphicsDialog( weld::Window* pParent, Graphic const & rGraphic, Size rViewSize100mm, tools::Rectangle const & rCropRectangle, SfxBindings& rBindings ) :
60  GenericDialogController( pParent, "svx/ui/compressgraphicdialog.ui", "CompressGraphicDialog" ),
61  m_xGraphicObj ( nullptr ),
62  m_aGraphic ( rGraphic ),
63  m_aViewSize100mm ( rViewSize100mm ),
64  m_aCropRectangle ( rCropRectangle ),
65  m_rBindings ( rBindings ),
66  m_dResolution ( 300 )
67 {
68  Initialize();
69 }
70 
72 {
73 }
74 
76 {
77  m_xLabelGraphicType = m_xBuilder->weld_label("label-graphic-type");
78  m_xFixedText2 = m_xBuilder->weld_label("label-original-size");
79  m_xFixedText3 = m_xBuilder->weld_label("label-view-size");
80  m_xFixedText5 = m_xBuilder->weld_label("label-image-capacity");
81  m_xFixedText6 = m_xBuilder->weld_label("label-new-capacity");
82  m_xJpegCompRB = m_xBuilder->weld_radio_button("radio-jpeg");
83  m_xCompressionMF = m_xBuilder->weld_spin_button("spin-compression");
84  m_xCompressionSlider = m_xBuilder->weld_scale("scale-compression");
85  m_xLosslessRB = m_xBuilder->weld_radio_button("radio-lossless");
86  m_xQualityMF = m_xBuilder->weld_spin_button("spin-quality");
87  m_xQualitySlider = m_xBuilder->weld_scale("scale-quality");
88  m_xReduceResolutionCB = m_xBuilder->weld_check_button("checkbox-reduce-resolution");
89  m_xMFNewWidth = m_xBuilder->weld_spin_button("spin-new-width");
90  m_xMFNewHeight = m_xBuilder->weld_spin_button("spin-new-height");
91  m_xResolutionLB = m_xBuilder->weld_combo_box("combo-resolution");
92  m_xBtnCalculate = m_xBuilder->weld_button("calculate");
93  m_xInterpolationCombo = m_xBuilder->weld_combo_box("interpolation-method-combo");
94 
95  m_xInterpolationCombo->set_active_text("Lanczos");
96 
97  m_xInterpolationCombo->connect_changed(LINK(this, CompressGraphicsDialog, NewInterpolationModifiedHdl));
98 
99  m_xMFNewWidth->connect_changed( LINK( this, CompressGraphicsDialog, NewWidthModifiedHdl ));
100  m_xMFNewHeight->connect_changed( LINK( this, CompressGraphicsDialog, NewHeightModifiedHdl ));
101 
102  m_xResolutionLB->connect_changed( LINK( this, CompressGraphicsDialog, ResolutionModifiedHdl ));
103  m_xBtnCalculate->connect_clicked( LINK( this, CompressGraphicsDialog, CalculateClickHdl ) );
104 
105  m_xLosslessRB->connect_toggled( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
106  m_xJpegCompRB->connect_toggled( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
107 
108  m_xReduceResolutionCB->connect_toggled( LINK( this, CompressGraphicsDialog, ToggleReduceResolutionRB ) );
109 
110  m_xQualitySlider->connect_value_changed( LINK( this, CompressGraphicsDialog, SlideHdl ));
111  m_xCompressionSlider->connect_value_changed( LINK( this, CompressGraphicsDialog, SlideHdl ));
112  m_xQualityMF->connect_changed( LINK( this, CompressGraphicsDialog, NewQualityModifiedHdl ));
113  m_xCompressionMF->connect_changed( LINK( this, CompressGraphicsDialog, NewCompressionModifiedHdl ));
114 
115  m_xJpegCompRB->set_active(true);
116  m_xReduceResolutionCB->set_active(true);
117 
121  Update();
122 }
123 
125 {
126  auto pGfxLink = m_aGraphic.GetSharedGfxLink();
127 
128  m_xLabelGraphicType->set_label(GraphicHelper::GetImageType(m_aGraphic));
129 
130  const FieldUnit eFieldUnit = m_rBindings.GetDispatcher()->GetModule()->GetFieldUnit();
132  sal_Unicode cSeparator = rLocaleWrapper.getNumDecimalSep()[0];
133 
135  pDummyVDev->EnableOutput( false );
136  pDummyVDev->SetMapMode( m_aGraphic.GetPrefMapMode() );
137 
138  Size aPixelSize = m_aGraphic.GetSizePixel();
139  Size aOriginalSize100mm(pDummyVDev->PixelToLogic(m_aGraphic.GetSizePixel(), MapMode(MapUnit::Map100thMM)));
140 
141  OUString aBitmapSizeString = SvxResId(STR_IMAGE_ORIGINAL_SIZE);
142  OUString aWidthString = GetUnitString( aOriginalSize100mm.Width(), eFieldUnit, cSeparator );
143  OUString aHeightString = GetUnitString( aOriginalSize100mm.Height(), eFieldUnit, cSeparator );
144  aBitmapSizeString = aBitmapSizeString.replaceAll("$(WIDTH)", aWidthString);
145  aBitmapSizeString = aBitmapSizeString.replaceAll("$(HEIGHT)", aHeightString);
146  aBitmapSizeString = aBitmapSizeString.replaceAll("$(WIDTH_IN_PX)", OUString::number(aPixelSize.Width()));
147  aBitmapSizeString = aBitmapSizeString.replaceAll("$(HEIGHT_IN_PX)", OUString::number(aPixelSize.Height()));
148  m_xFixedText2->set_label(aBitmapSizeString);
149 
150  int aValX = static_cast<int>(aPixelSize.Width() / GetViewWidthInch());
151 
152  OUString aViewSizeString = SvxResId(STR_IMAGE_VIEW_SIZE);
153 
154  aWidthString = GetUnitString( m_aViewSize100mm.Width(), eFieldUnit, cSeparator );
155  aHeightString = GetUnitString( m_aViewSize100mm.Height(), eFieldUnit, cSeparator );
156  aViewSizeString = aViewSizeString.replaceAll("$(WIDTH)", aWidthString);
157  aViewSizeString = aViewSizeString.replaceAll("$(HEIGHT)", aHeightString);
158  aViewSizeString = aViewSizeString.replaceAll("$(DPI)", OUString::number(aValX));
159  m_xFixedText3->set_label(aViewSizeString);
160 
161  m_aNativeSize = pGfxLink ? pGfxLink->GetDataSize() : 0;
162 
163  OUString aNativeSizeString = SvxResId(STR_IMAGE_CAPACITY);
164  aNativeSizeString = aNativeSizeString.replaceAll("$(CAPACITY)", OUString::number( m_aNativeSize / 1024 ));
165  m_xFixedText5->set_label(aNativeSizeString);
166 
167  m_xFixedText6->set_label("??");
168 }
169 
171 {
172  int nPixelX = static_cast<sal_Int32>( GetViewWidthInch() * m_dResolution );
173  m_xMFNewWidth->set_value(nPixelX);
174 }
175 
177 {
178  int nPixelY = static_cast<sal_Int32>( GetViewHeightInch() * m_dResolution );
179  m_xMFNewHeight->set_value(nPixelY);
180 }
181 
183 {
184  m_xResolutionLB->set_entry_text( OUString::number( static_cast<sal_Int32>(m_dResolution) ) );
185 }
186 
188 {
189  return static_cast<double>(vcl::ConvertValue(m_aViewSize100mm.Width(), 2, MapUnit::Map100thMM, FieldUnit::INCH)) / 100.0;
190 }
191 
193 {
194  return static_cast<double>(vcl::ConvertValue(m_aViewSize100mm.Height(), 2, MapUnit::Map100thMM, FieldUnit::INCH)) / 100.0;
195 }
196 
198 {
199  OUString aSelectionText = m_xInterpolationCombo->get_active_text();
200 
201  if( aSelectionText == "Lanczos" ) {
202  return BmpScaleFlag::Lanczos;
203  } else if( aSelectionText == "Bilinear" ) {
204  return BmpScaleFlag::BiLinear;
205  } else if( aSelectionText == "Bicubic" ) {
206  return BmpScaleFlag::BiCubic;
207  } else if ( aSelectionText == "None" ) {
208  return BmpScaleFlag::Fast;
209  }
210  return BmpScaleFlag::BestQuality;
211 }
212 
214 {
215  BitmapEx aBitmap = m_aGraphic.GetBitmapEx();
216  if ( m_xReduceResolutionCB->get_active() )
217  {
218  tools::Long nPixelX = static_cast<tools::Long>( GetViewWidthInch() * m_dResolution );
219  tools::Long nPixelY = static_cast<tools::Long>( GetViewHeightInch() * m_dResolution );
220 
221  aBitmap.Scale( Size( nPixelX, nPixelY ), GetSelectedInterpolationType() );
222  }
223  Graphic aScaledGraphic( aBitmap );
225 
226  Sequence< PropertyValue > aFilterData{
227  comphelper::makePropertyValue("Interlaced", sal_Int32(0)),
228  comphelper::makePropertyValue("Compression", static_cast<sal_Int32>(m_xCompressionMF->get_value())),
229  comphelper::makePropertyValue("Quality", static_cast<sal_Int32>(m_xQualityMF->get_value()))
230  };
231 
232  OUString aGraphicFormatName = m_xLosslessRB->get_active() ? OUString( "png" ) : OUString( "jpg" );
233 
234  sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aGraphicFormatName );
235  rFilter.ExportGraphic( aScaledGraphic, "none", aStream, nFilterFormat, &aFilterData );
236 }
237 
238 IMPL_LINK_NOARG( CompressGraphicsDialog, NewWidthModifiedHdl, weld::Entry&, void )
239 {
240  m_dResolution = m_xMFNewWidth->get_value() / GetViewWidthInch();
241 
242  UpdateNewHeightMF();
243  UpdateResolutionLB();
244  Update();
245 }
246 
247 IMPL_LINK( CompressGraphicsDialog, SlideHdl, weld::Scale&, rScale, void )
248 {
249  if (&rScale == m_xQualitySlider.get())
250  m_xQualityMF->set_value(m_xQualitySlider->get_value());
251  else
252  m_xCompressionMF->set_value(m_xCompressionSlider->get_value());
253  Update();
254 }
255 
256 IMPL_LINK_NOARG( CompressGraphicsDialog, NewInterpolationModifiedHdl, weld::ComboBox&, void )
257 {
258  Update();
259 }
260 
261 IMPL_LINK_NOARG( CompressGraphicsDialog, NewQualityModifiedHdl, weld::Entry&, void )
262 {
263  m_xQualitySlider->set_value(m_xQualityMF->get_value());
264  Update();
265 }
266 
267 IMPL_LINK_NOARG( CompressGraphicsDialog, NewCompressionModifiedHdl, weld::Entry&, void )
268 {
269  m_xCompressionSlider->set_value(m_xCompressionMF->get_value());
270  Update();
271 }
272 
273 IMPL_LINK_NOARG( CompressGraphicsDialog, NewHeightModifiedHdl, weld::Entry&, void )
274 {
275  m_dResolution = m_xMFNewHeight->get_value() / GetViewHeightInch();
276 
277  UpdateNewWidthMF();
278  UpdateResolutionLB();
279  Update();
280 }
281 
282 IMPL_LINK_NOARG( CompressGraphicsDialog, ResolutionModifiedHdl, weld::ComboBox&, void )
283 {
284  m_dResolution = static_cast<double>(m_xResolutionLB->get_active_text().toInt32());
285 
286  UpdateNewWidthMF();
287  UpdateNewHeightMF();
288  Update();
289 }
290 
292 {
293  bool choice = m_xLosslessRB->get_active();
294  m_xCompressionMF->set_sensitive(choice);
295  m_xCompressionSlider->set_sensitive(choice);
296  m_xQualityMF->set_sensitive(!choice);
297  m_xQualitySlider->set_sensitive(!choice);
298  Update();
299 }
300 
301 IMPL_LINK_NOARG( CompressGraphicsDialog, ToggleReduceResolutionRB, weld::Toggleable&, void )
302 {
303  bool choice = m_xReduceResolutionCB->get_active();
304  m_xMFNewWidth->set_sensitive(choice);
305  m_xMFNewHeight->set_sensitive(choice);
306  m_xResolutionLB->set_sensitive(choice);
307  m_xInterpolationCombo->set_sensitive(choice);
308  Update();
309 }
310 
312 {
313  sal_Int32 aSize = 0;
314 
315  if ( m_dResolution > 0.0 )
316  {
317  SvMemoryStream aMemStream;
319  Compress( aMemStream );
320  aSize = aMemStream.TellEnd();
321  }
322 
323  if ( aSize > 0 )
324  {
325  OUString aSizeAsString = OUString::number(aSize / 1024);
326 
327  OUString aReductionSizeAsString = OUString::number( m_aNativeSize > 0 ? (m_aNativeSize - aSize) * 100 / m_aNativeSize : 0 );
328 
329  OUString aNewSizeString = SvxResId(STR_IMAGE_CAPACITY_WITH_REDUCTION);
330  aNewSizeString = aNewSizeString.replaceAll("$(CAPACITY)", aSizeAsString);
331  aNewSizeString = aNewSizeString.replaceAll("$(REDUCTION)", aReductionSizeAsString);
332  m_xFixedText6->set_label(aNewSizeString);
333  }
334 }
335 
337 {
338  if ( m_xReduceResolutionCB->get_active() )
339  {
340  tools::Long nPixelX = static_cast<tools::Long>( GetViewWidthInch() * m_dResolution );
341  tools::Long nPixelY = static_cast<tools::Long>( GetViewHeightInch() * m_dResolution );
343  double aScaleX = nPixelX / static_cast<double>(aSize.Width());
344  double aScaleY = nPixelY / static_cast<double>(aSize.Height());
345 
346  return tools::Rectangle(
347  m_aCropRectangle.Left() * aScaleX,
348  m_aCropRectangle.Top() * aScaleY,
349  m_aCropRectangle.Right() * aScaleX,
350  m_aCropRectangle.Bottom()* aScaleY);
351  }
352  else
353  {
354  return m_aCropRectangle;
355  }
356 }
357 
359 {
360  if ( m_dResolution > 0.0 )
361  {
362  SvMemoryStream aMemStream;
364  Compress( aMemStream );
365  aMemStream.Seek( STREAM_SEEK_TO_BEGIN );
366  Graphic aResultGraphic;
368  rFilter.ImportGraphic( aResultGraphic, OUString("import"), aMemStream );
369 
370  return aResultGraphic;
371  }
372  return Graphic();
373 }
374 
376 {
377  if ( m_dResolution > 0.0 )
378  {
380 
381  if ( m_xReduceResolutionCB->get_active() )
382  {
383  tools::Rectangle aScaledCropedRectangle = GetScaledCropRectangle();
384  SdrGrafCropItem aNewCrop(
385  aScaledCropedRectangle.Left(),
386  aScaledCropedRectangle.Top(),
387  aScaledCropedRectangle.Right(),
388  aScaledCropedRectangle.Bottom());
389 
390  pNewObject->SetMergedItem(aNewCrop);
391  }
392  pNewObject->SetGraphic( GetCompressedGraphic() );
393 
394  return pNewObject;
395  }
396  return nullptr;
397 }
398 
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Label > m_xFixedText5
FieldUnit
ErrCode ImportGraphic(Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat=GRFILTER_FORMAT_DONTKNOW, sal_uInt16 *pDeterminedFormat=nullptr, GraphicFilterImportFlags nImportFlags=GraphicFilterImportFlags::NONE)
std::unique_ptr< weld::Builder > m_xBuilder
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
constexpr tools::Long Left() const
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
long Long
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
static const AllSettings & GetSettings()
std::unique_ptr< weld::SpinButton > m_xMFNewWidth
#define SOFFICE_FILEFORMAT_CURRENT
std::unique_ptr< weld::RadioButton > m_xLosslessRB
sal_uInt64 Seek(sal_uInt64 nPos)
virtual ~CompressGraphicsDialog() override
std::unique_ptr< weld::RadioButton > m_xJpegCompRB
const std::shared_ptr< GfxLink > & GetSharedGfxLink() const
virtual sal_uInt64 TellEnd() override
IMPL_LINK_NOARG(CompressGraphicsDialog, NewWidthModifiedHdl, weld::Entry &, void)
constexpr tools::Long Width() const
std::unique_ptr< weld::Scale > m_xQualitySlider
SfxModule * GetModule() const
sal_uInt16 sal_Unicode
std::unique_ptr< weld::SpinButton > m_xQualityMF
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
void SetGraphic(const Graphic &rGrf)
Definition: svdograf.cxx:364
std::unique_ptr< weld::Button > m_xBtnCalculate
std::unique_ptr< weld::ComboBox > m_xInterpolationCombo
void Compress(SvStream &aStream)
sal_uInt16 GetExportFormatNumberForShortName(std::u16string_view rShortName)
BmpScaleFlag
CompressGraphicsDialog(weld::Window *pParent, SdrGrafObj *pGraphicObj, SfxBindings &rBindings)
std::unique_ptr< weld::Label > m_xLabelGraphicType
const OUString & getNumDecimalSep() const
OUString GetUnitString(tools::Long nVal_100, FieldUnit eFieldUnit, sal_Unicode cSep)
Definition: dlgunit.hxx:27
FieldUnit GetFieldUnit() const
tools::Rectangle GetScaledCropRectangle() const
#define STREAM_SEEK_TO_BEGIN
constexpr tools::Long Right() const
SdrModel & getSdrModelFromSdrObject() const
Definition: svdobj.cxx:279
constexpr tools::Long Top() const
const LocaleDataWrapper & GetLocaleDataWrapper(LanguageType nLang)
void SetMergedItem(const SfxPoolItem &rItem)
Definition: svdobj.cxx:1990
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
const SfxPoolItem & GetMergedItem(const sal_uInt16 nWhich) const
Definition: svdobj.cxx:2015
void SetVersion(sal_Int32 n)
This class represents an embedded or linked bitmap graphic object.
Definition: svdograf.hxx:67
std::unique_ptr< weld::Label > m_xFixedText3
constexpr TypedWhichId< SdrGrafCropItem > SDRATTR_GRAFCROP(SDRATTR_GRAF_FIRST+9)
ErrCode ExportGraphic(const Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr)
std::unique_ptr< weld::Label > m_xFixedText6
constexpr tools::Long Bottom() const
IMPL_LINK(CompressGraphicsDialog, SlideHdl, weld::Scale &, rScale, void)
std::unique_ptr< weld::CheckButton > m_xReduceResolutionCB
MapMode GetPrefMapMode() const
constexpr tools::Long Height() const
virtual SdrGrafObj * CloneSdrObject(SdrModel &rTargetModel) const override
Definition: svdograf.cxx:773
BmpScaleFlag GetSelectedInterpolationType() const
std::unique_ptr< weld::Scale > m_xCompressionSlider
sal_Int64 ConvertValue(sal_Int64 nValue, sal_Int64 mnBaseValue, sal_uInt16 nDecDigits, FieldUnit eInUnit, FieldUnit eOutUnit)
std::unique_ptr< weld::Label > m_xFixedText2
SfxDispatcher * GetDispatcher() const
static GraphicFilter & GetGraphicFilter()
std::unique_ptr< weld::SpinButton > m_xCompressionMF
const Size & GetSizePixel() const
std::unique_ptr< weld::ComboBox > m_xResolutionLB
std::unique_ptr< weld::SpinButton > m_xMFNewHeight