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