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