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