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/virdev.hxx>
23 : #include <vcl/svapp.hxx>
24 : #include <svx/svdograf.hxx>
25 : #include <svx/sdgcpitm.hxx>
26 : #include <svx/dialmgr.hxx>
27 : #include <svx/compressgraphicdialog.hxx>
28 : #include <svtools/filter.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 : using namespace com::sun::star::uno;
35 : using namespace com::sun::star::beans;
36 :
37 : #include <stdio.h>
38 :
39 0 : CompressGraphicsDialog::CompressGraphicsDialog( Window* pParent, SdrGrafObj* pGraphicObj, SfxBindings& rBindings ) :
40 : ModalDialog ( pParent, "CompressGraphicDialog", "svx/ui/compressgraphicdialog.ui" ),
41 : m_pGraphicObj ( pGraphicObj ),
42 0 : m_aGraphic ( pGraphicObj->GetGraphicObject().GetGraphic() ),
43 0 : m_aViewSize100mm ( pGraphicObj->GetLogicRect().GetSize() ),
44 : m_aCropRectangle ( Rectangle() ),
45 : m_rBindings ( rBindings ),
46 0 : m_dResolution ( 96.0 )
47 : {
48 0 : Initialize();
49 0 : }
50 :
51 0 : void CompressGraphicsDialog::Initialize()
52 : {
53 0 : get(m_pFixedText2, "label-original-size");
54 0 : get(m_pFixedText3, "label-view-size");
55 0 : get(m_pFixedText5, "label-image-capacity");
56 0 : get(m_pFixedText6, "label-new-capacity");
57 0 : get(m_pJpegCompRB, "radio-jpeg");
58 0 : get(m_pCompressionMF, "spin-compression");
59 0 : get(m_pLosslessRB, "radio-lossless");
60 0 : get(m_pQualityMF, "spin-quality");
61 0 : get(m_pReduceResolutionCB, "checkbox-reduce-resolution");
62 0 : get(m_pMFNewWidth, "spin-new-width");
63 0 : get(m_pMFNewHeight, "spin-new-height");
64 0 : get(m_pResolutionLB, "combo-resolution");
65 0 : get(m_pBtnCalculate, "calculate");
66 0 : get(m_pInterpolationCombo, "interpolation-method-combo");
67 :
68 0 : m_pInterpolationCombo->SelectEntry( OUString("Lanczos") );
69 :
70 0 : m_pMFNewWidth->SetModifyHdl( LINK( this, CompressGraphicsDialog, NewWidthModifiedHdl ));
71 0 : m_pMFNewHeight->SetModifyHdl( LINK( this, CompressGraphicsDialog, NewHeightModifiedHdl ));
72 :
73 0 : m_pResolutionLB->SetModifyHdl( LINK( this, CompressGraphicsDialog, ResolutionModifiedHdl ));
74 0 : m_pBtnCalculate->SetClickHdl( LINK( this, CompressGraphicsDialog, CalculateClickHdl ) );
75 :
76 0 : m_pLosslessRB->SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
77 0 : m_pJpegCompRB->SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
78 :
79 0 : m_pReduceResolutionCB->SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleReduceResolutionRB ) );
80 :
81 0 : m_pJpegCompRB->Check();
82 0 : m_pReduceResolutionCB->Check();
83 :
84 0 : UpdateNewWidthMF();
85 0 : UpdateNewHeightMF();
86 0 : UpdateResolutionLB();
87 0 : Update();
88 0 : }
89 :
90 0 : CompressGraphicsDialog::~CompressGraphicsDialog()
91 : {
92 0 : }
93 :
94 0 : void CompressGraphicsDialog::Update()
95 : {
96 0 : const FieldUnit eFieldUnit = m_rBindings.GetDispatcher()->GetModule()->GetFieldUnit();
97 0 : const LocaleDataWrapper& rLocaleWrapper( Application::GetSettings().GetLocaleDataWrapper() );
98 0 : sal_Unicode cSeparator = rLocaleWrapper.getNumDecimalSep()[0];
99 :
100 0 : VirtualDevice* pDummyVDev = new VirtualDevice();
101 0 : pDummyVDev->EnableOutput( false );
102 0 : pDummyVDev->SetMapMode( m_aGraphic.GetPrefMapMode() );
103 :
104 0 : Size aPixelSize = m_aGraphic.GetSizePixel();
105 0 : Size aOriginalSize100mm( pDummyVDev->PixelToLogic( m_aGraphic.GetSizePixel(), MAP_100TH_MM ) );
106 :
107 0 : String aBitmapSizeString;
108 0 : aBitmapSizeString += GetUnitString( aOriginalSize100mm.Width(), eFieldUnit, cSeparator );
109 0 : aBitmapSizeString += String( " x " ) ;
110 0 : aBitmapSizeString += GetUnitString( aOriginalSize100mm.Height(), eFieldUnit, cSeparator );
111 0 : aBitmapSizeString += String( " ( " ) ;
112 0 : aBitmapSizeString += UniString::CreateFromInt32(aPixelSize.Width());
113 0 : aBitmapSizeString += String( " x " ) ;
114 0 : aBitmapSizeString += UniString::CreateFromInt32(aPixelSize.Height());
115 0 : aBitmapSizeString += String( " px )" ) ;
116 0 : m_pFixedText2->SetText(aBitmapSizeString);
117 :
118 0 : String aViewSizeString;
119 :
120 0 : int aValX = (int) (aPixelSize.Width() / GetViewWidthInch());
121 :
122 0 : aViewSizeString += GetUnitString( m_aViewSize100mm.Width(), eFieldUnit, cSeparator );
123 0 : aViewSizeString += String( " x " ) ;
124 0 : aViewSizeString += GetUnitString( m_aViewSize100mm.Height(), eFieldUnit, cSeparator );
125 0 : aViewSizeString += ( " at " ) ;
126 0 : aViewSizeString += UniString::CreateFromInt32(aValX);
127 0 : aViewSizeString += ( " DPI" ) ;
128 0 : m_pFixedText3->SetText(aViewSizeString);
129 :
130 0 : SvMemoryStream aMemStream;
131 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
132 0 : m_aGraphic.ExportNative(aMemStream);
133 0 : aMemStream.Seek( STREAM_SEEK_TO_END );
134 0 : sal_Int32 aNativeSize = aMemStream.Tell();
135 :
136 0 : String aNativeSizeString;
137 0 : aNativeSizeString += UniString::CreateFromInt32(aNativeSize / 1024);
138 0 : aNativeSizeString += String( " kiB" ) ;
139 :
140 0 : m_pFixedText5->SetText(aNativeSizeString);
141 0 : m_pFixedText6->SetText(String("??"));
142 0 : }
143 :
144 0 : void CompressGraphicsDialog::UpdateNewWidthMF()
145 : {
146 0 : int nPixelX = (sal_Int32)( GetViewWidthInch() * m_dResolution );
147 0 : m_pMFNewWidth->SetText( UniString::CreateFromInt32( nPixelX ) );
148 0 : }
149 :
150 0 : void CompressGraphicsDialog::UpdateNewHeightMF()
151 : {
152 0 : int nPixelY = (sal_Int32)( GetViewHeightInch() * m_dResolution );
153 0 : m_pMFNewHeight->SetText( UniString::CreateFromInt32( nPixelY ) );
154 0 : }
155 :
156 0 : void CompressGraphicsDialog::UpdateResolutionLB()
157 : {
158 0 : m_pResolutionLB->SetText( UniString::CreateFromInt32( (sal_Int32) m_dResolution ) );
159 0 : }
160 :
161 0 : double CompressGraphicsDialog::GetViewWidthInch()
162 : {
163 0 : return (double) MetricField::ConvertValue(m_aViewSize100mm.Width(), 2, MAP_100TH_MM, FUNIT_INCH) / 100.0;
164 : }
165 :
166 0 : double CompressGraphicsDialog::GetViewHeightInch()
167 : {
168 0 : return (double) MetricField::ConvertValue(m_aViewSize100mm.Height(), 2, MAP_100TH_MM, FUNIT_INCH) / 100.0;
169 : }
170 :
171 0 : sal_uLong CompressGraphicsDialog::GetSelectedInterpolationType()
172 : {
173 0 : OUString aSelectionText = OUString( m_pInterpolationCombo->GetSelectEntry() );
174 :
175 0 : if( aSelectionText == "Lanczos" ) {
176 0 : return BMP_SCALE_LANCZOS;
177 0 : } else if( aSelectionText == "Bilinear" ) {
178 0 : return BMP_SCALE_BILINEAR;
179 0 : } else if( aSelectionText == "Bicubic" ) {
180 0 : return BMP_SCALE_BICUBIC;
181 0 : } else if ( aSelectionText == "None" ) {
182 0 : return BMP_SCALE_FAST;
183 : }
184 0 : return BMP_SCALE_BEST;
185 : }
186 :
187 0 : void CompressGraphicsDialog::Compress(SvStream& aStream)
188 : {
189 0 : BitmapEx bitmap = m_aGraphic.GetBitmapEx();
190 0 : if ( m_pReduceResolutionCB->IsChecked() )
191 : {
192 0 : long nPixelX = (long)( GetViewWidthInch() * m_dResolution );
193 0 : long nPixelY = (long)( GetViewHeightInch() * m_dResolution );
194 :
195 0 : bitmap.Scale( Size( nPixelX, nPixelY ), GetSelectedInterpolationType() );
196 : }
197 0 : Graphic aScaledGraphic = Graphic( bitmap );
198 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
199 :
200 0 : Sequence< PropertyValue > aFilterData( 3 );
201 0 : aFilterData[ 0 ].Name = "Interlaced";
202 0 : aFilterData[ 0 ].Value <<= (sal_Int32) 0;
203 0 : aFilterData[ 1 ].Name = "Compression";
204 0 : aFilterData[ 1 ].Value <<= (sal_Int32) m_pCompressionMF->GetValue();
205 0 : aFilterData[ 2 ].Name = "Quality";
206 0 : aFilterData[ 2 ].Value <<= (sal_Int32) m_pQualityMF->GetValue();
207 :
208 0 : String aGraphicFormatName = m_pLosslessRB->IsChecked() ? String( "png" ) : String( "jpg" );
209 :
210 0 : sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aGraphicFormatName );
211 0 : rFilter.ExportGraphic( aScaledGraphic, String( "test" ), aStream, nFilterFormat, &aFilterData );
212 0 : }
213 :
214 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, NewWidthModifiedHdl )
215 : {
216 0 : m_dResolution = m_pMFNewWidth->GetValue() / GetViewWidthInch();
217 :
218 0 : UpdateNewHeightMF();
219 0 : UpdateResolutionLB();
220 0 : Update();
221 :
222 0 : return 0L;
223 : }
224 :
225 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, NewHeightModifiedHdl )
226 : {
227 0 : m_dResolution = m_pMFNewHeight->GetValue() / GetViewHeightInch();
228 :
229 0 : UpdateNewWidthMF();
230 0 : UpdateResolutionLB();
231 0 : Update();
232 :
233 0 : return 0L;
234 : }
235 :
236 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ResolutionModifiedHdl )
237 : {
238 0 : m_dResolution = (double) m_pResolutionLB->GetText().ToInt32();
239 :
240 0 : UpdateNewWidthMF();
241 0 : UpdateNewHeightMF();
242 0 : Update();
243 :
244 0 : return 0L;
245 : }
246 :
247 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ToggleCompressionRB )
248 : {
249 0 : bool choice = m_pLosslessRB->IsChecked();
250 0 : m_pCompressionMF->Enable(choice);
251 0 : m_pQualityMF->Enable(!choice);
252 :
253 0 : return 0L;
254 : }
255 :
256 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ToggleReduceResolutionRB )
257 : {
258 0 : bool choice = m_pReduceResolutionCB->IsChecked();
259 0 : m_pMFNewWidth->Enable(choice);
260 0 : m_pMFNewHeight->Enable(choice);
261 0 : m_pResolutionLB->Enable(choice);
262 0 : m_pInterpolationCombo->Enable(choice);
263 :
264 0 : return 0L;
265 : }
266 :
267 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, CalculateClickHdl )
268 : {
269 0 : sal_Int32 aSize = 0;
270 :
271 0 : if ( m_dResolution > 0 )
272 : {
273 0 : SvMemoryStream aMemStream;
274 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
275 0 : Compress( aMemStream );
276 0 : aMemStream.Seek( STREAM_SEEK_TO_END );
277 0 : aSize = aMemStream.Tell();
278 : }
279 :
280 0 : if ( aSize > 0 )
281 : {
282 0 : String aNewSizeString;
283 0 : aNewSizeString += UniString::CreateFromInt32(aSize / 1024);
284 0 : aNewSizeString += String( " kiB" ) ;
285 0 : m_pFixedText6->SetText(aNewSizeString);
286 : }
287 0 : return 0L;
288 : }
289 :
290 0 : SdrGrafObj* CompressGraphicsDialog::GetCompressedSdrGrafObj()
291 : {
292 0 : if ( m_dResolution > 0 )
293 : {
294 0 : SdrGrafObj* pNewObject = (SdrGrafObj*) m_pGraphicObj->Clone();
295 :
296 0 : if ( m_pReduceResolutionCB->IsChecked() )
297 : {
298 0 : const SdrGrafCropItem& rCrop = (const SdrGrafCropItem&) m_pGraphicObj->GetMergedItem(SDRATTR_GRAFCROP);
299 0 : long nPixelX = (long)( GetViewWidthInch() * m_dResolution );
300 0 : long nPixelY = (long)( GetViewHeightInch() * m_dResolution );
301 0 : Size size = m_aGraphic.GetBitmapEx().GetSizePixel();
302 0 : double aScaleX = nPixelX / (double) size.Width();
303 0 : double aScaleY = nPixelY / (double) size.Height();
304 :
305 : SdrGrafCropItem aNewCrop(
306 0 : rCrop.GetLeft() * aScaleX,
307 0 : rCrop.GetTop() * aScaleY,
308 0 : rCrop.GetRight() * aScaleX,
309 0 : rCrop.GetBottom()* aScaleY);
310 0 : pNewObject->SetMergedItem(aNewCrop);
311 : }
312 :
313 0 : SvMemoryStream aMemStream;
314 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
315 0 : Compress( aMemStream );
316 0 : aMemStream.Seek( STREAM_SEEK_TO_BEGIN );
317 0 : Graphic aResultGraphic;
318 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
319 0 : rFilter.ImportGraphic( aResultGraphic, String("import"), aMemStream );
320 :
321 0 : pNewObject->SetGraphic( aResultGraphic );
322 :
323 0 : return pNewObject;
324 : }
325 0 : return NULL;
326 : }
327 :
328 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|