Branch data 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 <svx/dialogs.hrc>
21 : : #include <svx/dialmgr.hxx>
22 : : #include <vcl/graph.hxx>
23 : : #include <compressgraphicdialog.hrc>
24 : : #include <svx/compressgraphicdialog.hxx>
25 : : #include <dlgunit.hxx>
26 : : #include <vcl/virdev.hxx>
27 : : #include <sfx2/dispatch.hxx>
28 : : #include <vcl/svapp.hxx>
29 : : #include <comphelper/processfactory.hxx>
30 : : #include <svtools/filter.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 : 0 : CompressGraphicsDialog::CompressGraphicsDialog( Window* pParent, const Graphic& rGraphic, const Size& rViewSize100mm, SfxBindings& rBindings ) :
38 : 0 : ModalDialog ( pParent, SVX_RES( RID_SVXDLG_COMPRESSGRAPHICS ) ),
39 [ # # ]: 0 : m_aImageDetailsFL ( this, SVX_RES( FL_IMAGE_DETAILS ) ),
40 [ # # ]: 0 : m_aFixedText2X ( this, SVX_RES( FT_CG_2_X ) ),
41 [ # # ]: 0 : m_aFixedText2 ( this, SVX_RES( FT_CG_2 ) ),
42 [ # # ]: 0 : m_aFixedText3X ( this, SVX_RES( FT_CG_3_X ) ),
43 [ # # ]: 0 : m_aFixedText3 ( this, SVX_RES( FT_CG_3 ) ),
44 [ # # ]: 0 : m_aFixedText5X ( this, SVX_RES( FT_CG_5_X ) ),
45 [ # # ]: 0 : m_aFixedText5 ( this, SVX_RES( FT_CG_5 ) ),
46 [ # # ]: 0 : m_aFixedText6X ( this, SVX_RES( FT_CG_6_X ) ),
47 [ # # ]: 0 : m_aFixedText6 ( this, SVX_RES( FT_CG_6 ) ),
48 [ # # ]: 0 : m_aSettingsFL ( this, SVX_RES( FL_SETTINGS ) ),
49 [ # # ]: 0 : m_aReduceResolutionCB ( this, SVX_RES( CB_REDUCE_IMAGE_RESOLUTION ) ),
50 [ # # ]: 0 : m_aNewWidthFT ( this, SVX_RES( FT_NEW_WIDTH ) ),
51 [ # # ]: 0 : m_aMFNewWidth ( this, SVX_RES( MF_NEW_WIDTH ) ),
52 [ # # ]: 0 : m_aNewHeightFT ( this, SVX_RES( FT_NEW_HEIGHT ) ),
53 [ # # ]: 0 : m_aMFNewHeight ( this, SVX_RES( MF_NEW_HEIGHT ) ),
54 [ # # ]: 0 : m_aResolutionFT ( this, SVX_RES( FT_RESOLUTION ) ),
55 [ # # ]: 0 : m_aResolutionLB ( this, SVX_RES( LB_RESOLUTION ) ),
56 [ # # ]: 0 : m_aFixedTextDPI ( this, SVX_RES( FT_DPI ) ),
57 [ # # ]: 0 : m_aLosslessRB ( this, SVX_RES( RB_LOSSLESS_COMPRESSION ) ),
58 [ # # ]: 0 : m_aJpegCompRB ( this, SVX_RES( RB_JPEG_COMPRESSION ) ),
59 [ # # ]: 0 : m_aCompressionFT ( this, SVX_RES( FT_COMPRESSION ) ),
60 [ # # ]: 0 : m_aCompressionMF ( this, SVX_RES( MF_COMPRESSION_FACTOR ) ),
61 [ # # ]: 0 : m_aQualityFT ( this, SVX_RES( FT_QUALITY ) ),
62 [ # # ]: 0 : m_aQualityMF ( this, SVX_RES( MF_QUALITY_FACTOR ) ),
63 [ # # ]: 0 : m_aCropCB ( this, SVX_RES( CB_CROP ) ),
64 [ # # ]: 0 : m_aBtnOK ( this, SVX_RES( BUTTON_CG_OK ) ),
65 [ # # ]: 0 : m_aBtnCancel ( this, SVX_RES( BUTTON_CG_CANCEL ) ),
66 [ # # ]: 0 : m_aBtnHelp ( this, SVX_RES( BUTTON_CG_HELP ) ),
67 [ # # ]: 0 : m_aBtnCalculate ( this, SVX_RES( BUTTON_CG_CALCULATE ) ),
68 : : m_aGraphic ( rGraphic ),
69 : : m_aViewSize100mm ( rViewSize100mm ),
70 [ # # ][ # # ]: 0 : m_rBindings ( rBindings )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
71 : : {
72 [ # # ]: 0 : FreeResource();
73 : :
74 [ # # ][ # # ]: 0 : SetStyle(GetStyle() | WB_CENTER | WB_VCENTER);
75 : :
76 [ # # ]: 0 : m_aQualityMF.SetValue( 90L );
77 [ # # ]: 0 : m_aCompressionMF.SetValue( 9L );
78 : :
79 [ # # ]: 0 : m_aMFNewWidth.SetModifyHdl( LINK( this, CompressGraphicsDialog, NewWidthModifiedHdl ));
80 [ # # ]: 0 : m_aMFNewHeight.SetModifyHdl( LINK( this, CompressGraphicsDialog, NewHeightModifiedHdl ));
81 : :
82 : 0 : m_dResolution = 96.0;
83 : :
84 [ # # ]: 0 : m_aResolutionLB.SetModifyHdl( LINK( this, CompressGraphicsDialog, ResolutionModifiedHdl ));
85 [ # # ]: 0 : m_aBtnCalculate.SetClickHdl( LINK( this, CompressGraphicsDialog, CalculateClickHdl ) );
86 : :
87 [ # # ]: 0 : m_aLosslessRB.SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
88 [ # # ]: 0 : m_aJpegCompRB.SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
89 : :
90 [ # # ]: 0 : m_aReduceResolutionCB.SetToggleHdl( LINK( this, CompressGraphicsDialog, ToggleReduceResolutionRB ) );
91 : :
92 : :
93 [ # # ]: 0 : m_aLosslessRB.Check();
94 [ # # ]: 0 : m_aReduceResolutionCB.Check();
95 : :
96 [ # # ]: 0 : UpdateNewWidthMF();
97 [ # # ]: 0 : UpdateNewHeightMF();
98 [ # # ]: 0 : UpdateResolutionLB();
99 [ # # ]: 0 : Update();
100 : 0 : }
101 : :
102 [ # # ][ # # ]: 0 : CompressGraphicsDialog::~CompressGraphicsDialog()
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
103 : : {
104 [ # # ]: 0 : }
105 : :
106 : 0 : void CompressGraphicsDialog::Update()
107 : : {
108 [ # # ][ # # ]: 0 : const FieldUnit eFieldUnit = m_rBindings.GetDispatcher()->GetModule()->GetFieldUnit();
109 [ # # ][ # # ]: 0 : LocaleDataWrapper aLocaleWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
[ # # ][ # # ]
110 [ # # ]: 0 : sal_Unicode cSep = aLocaleWrapper.getNumDecimalSep()[0];
111 : :
112 [ # # ][ # # ]: 0 : VirtualDevice* pDummyVDev = new VirtualDevice();
113 [ # # ]: 0 : pDummyVDev->EnableOutput( false );
114 [ # # ][ # # ]: 0 : pDummyVDev->SetMapMode( m_aGraphic.GetPrefMapMode() );
[ # # ]
115 : :
116 [ # # ]: 0 : Size aPixelSize = m_aGraphic.GetSizePixel();
117 [ # # ][ # # ]: 0 : Size aOriginalSize100mm( pDummyVDev->PixelToLogic( m_aGraphic.GetSizePixel(), MAP_100TH_MM ) );
[ # # ][ # # ]
118 : :
119 [ # # ]: 0 : String aBitmapSizeString;
120 [ # # ][ # # ]: 0 : aBitmapSizeString += GetUnitString( aOriginalSize100mm.Width(), eFieldUnit, cSep );
[ # # ]
121 [ # # ][ # # ]: 0 : aBitmapSizeString += String( " x " ) ;
[ # # ]
122 [ # # ][ # # ]: 0 : aBitmapSizeString += GetUnitString( aOriginalSize100mm.Height(), eFieldUnit, cSep );
[ # # ]
123 [ # # ][ # # ]: 0 : aBitmapSizeString += String( " ( " ) ;
[ # # ]
124 [ # # ][ # # ]: 0 : aBitmapSizeString += UniString::CreateFromInt32(aPixelSize.Width());
[ # # ]
125 [ # # ][ # # ]: 0 : aBitmapSizeString += String( " x " ) ;
[ # # ]
126 [ # # ][ # # ]: 0 : aBitmapSizeString += UniString::CreateFromInt32(aPixelSize.Height());
[ # # ]
127 [ # # ][ # # ]: 0 : aBitmapSizeString += String( " px )" ) ;
[ # # ]
128 [ # # ]: 0 : m_aFixedText2.SetText(aBitmapSizeString);
129 : :
130 [ # # ]: 0 : String aViewSizeString;
131 : :
132 [ # # ]: 0 : int aValX = (int) (aPixelSize.Width() / GetViewWidthInch());
133 : :
134 [ # # ][ # # ]: 0 : aViewSizeString += GetUnitString( m_aViewSize100mm.Width(), eFieldUnit, cSep );
[ # # ]
135 [ # # ][ # # ]: 0 : aViewSizeString += String( " x " ) ;
[ # # ]
136 [ # # ][ # # ]: 0 : aViewSizeString += GetUnitString( m_aViewSize100mm.Height(), eFieldUnit, cSep );
[ # # ]
137 [ # # ]: 0 : aViewSizeString += ( " at " ) ;
138 [ # # ][ # # ]: 0 : aViewSizeString += UniString::CreateFromInt32(aValX);
[ # # ]
139 [ # # ]: 0 : aViewSizeString += ( " DPI" ) ;
140 [ # # ]: 0 : m_aFixedText3.SetText(aViewSizeString);
141 : :
142 [ # # ]: 0 : SvMemoryStream aMemStream;
143 : 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
144 [ # # ]: 0 : m_aGraphic.ExportNative(aMemStream);
145 [ # # ]: 0 : aMemStream.Seek( STREAM_SEEK_TO_END );
146 : 0 : sal_Int32 aNativeSize = aMemStream.Tell();
147 : :
148 [ # # ]: 0 : String aNativeSizeString;
149 [ # # ][ # # ]: 0 : aNativeSizeString += UniString::CreateFromInt32(aNativeSize / 1024);
[ # # ]
150 [ # # ][ # # ]: 0 : aNativeSizeString += String( " kiB" ) ;
[ # # ]
151 : :
152 [ # # ]: 0 : m_aFixedText5.SetText(aNativeSizeString);
153 [ # # ][ # # ]: 0 : m_aFixedText6.SetText(String("??"));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
154 : 0 : }
155 : :
156 : 0 : void CompressGraphicsDialog::UpdateNewWidthMF()
157 : : {
158 : 0 : int nPixelX = (sal_Int32)( GetViewWidthInch() * m_dResolution );
159 [ # # ]: 0 : m_aMFNewWidth.SetText( UniString::CreateFromInt32( nPixelX ) );
160 : 0 : }
161 : :
162 : 0 : void CompressGraphicsDialog::UpdateNewHeightMF()
163 : : {
164 : 0 : int nPixelY = (sal_Int32)( GetViewHeightInch() * m_dResolution );
165 [ # # ]: 0 : m_aMFNewHeight.SetText( UniString::CreateFromInt32( nPixelY ) );
166 : 0 : }
167 : :
168 : 0 : void CompressGraphicsDialog::UpdateResolutionLB()
169 : : {
170 [ # # ]: 0 : m_aResolutionLB.SetText( UniString::CreateFromInt32( (sal_Int32) m_dResolution ) );
171 : 0 : }
172 : :
173 : 0 : double CompressGraphicsDialog::GetViewWidthInch()
174 : : {
175 : 0 : return (double) MetricField::ConvertValue(m_aViewSize100mm.Width(), 2, MAP_100TH_MM, FUNIT_INCH) / 100.0;
176 : : }
177 : :
178 : 0 : double CompressGraphicsDialog::GetViewHeightInch()
179 : : {
180 : 0 : return (double) MetricField::ConvertValue(m_aViewSize100mm.Height(), 2, MAP_100TH_MM, FUNIT_INCH) / 100.0;
181 : : }
182 : :
183 : 0 : void CompressGraphicsDialog::Compress(SvStream& aStream)
184 : : {
185 [ # # ]: 0 : long nPixelX = (long)( GetViewWidthInch() * m_dResolution );
186 [ # # ]: 0 : long nPixelY = (long)( GetViewHeightInch() * m_dResolution );
187 : :
188 [ # # ]: 0 : BitmapEx bitmap = m_aGraphic.GetBitmapEx();
189 [ # # ][ # # ]: 0 : if ( m_aReduceResolutionCB.IsChecked() )
190 : : {
191 [ # # ]: 0 : bitmap.Scale( Size( nPixelX, nPixelY ), BMP_SCALE_BEST );
192 : : }
193 [ # # ]: 0 : Graphic aScaledGraphic = Graphic( bitmap );
194 [ # # ]: 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
195 : :
196 [ # # ]: 0 : Sequence< PropertyValue > aFilterData( 3 );
197 [ # # ]: 0 : aFilterData[ 0 ].Name = "Interlaced";
198 [ # # ][ # # ]: 0 : aFilterData[ 0 ].Value <<= (sal_Int32) 0;
199 [ # # ]: 0 : aFilterData[ 1 ].Name = "Compression";
200 [ # # ][ # # ]: 0 : aFilterData[ 1 ].Value <<= (sal_Int32) m_aCompressionMF.GetValue();
[ # # ]
201 [ # # ]: 0 : aFilterData[ 2 ].Name = "Quality";
202 [ # # ][ # # ]: 0 : aFilterData[ 2 ].Value <<= (sal_Int32) m_aQualityMF.GetValue();
[ # # ]
203 : :
204 [ # # ][ # # ]: 0 : String aGraphicFormatName = m_aLosslessRB.IsChecked() ? String( "png" ) : String( "jpg" );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # #
# # # # #
# ]
205 : :
206 [ # # ]: 0 : sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aGraphicFormatName );
207 [ # # ][ # # ]: 0 : rFilter.ExportGraphic( aScaledGraphic, String( "test" ), aStream, nFilterFormat, &aFilterData );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
208 : 0 : }
209 : :
210 : 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, NewWidthModifiedHdl )
211 : : {
212 : 0 : m_dResolution = m_aMFNewWidth.GetValue() / GetViewWidthInch();
213 : :
214 : 0 : UpdateNewHeightMF();
215 : 0 : UpdateResolutionLB();
216 : 0 : Update();
217 : :
218 : 0 : return 0L;
219 : : }
220 : :
221 : 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, NewHeightModifiedHdl )
222 : : {
223 : 0 : m_dResolution = m_aMFNewHeight.GetValue() / GetViewHeightInch();
224 : :
225 : 0 : UpdateNewWidthMF();
226 : 0 : UpdateResolutionLB();
227 : 0 : Update();
228 : :
229 : 0 : return 0L;
230 : : }
231 : :
232 : 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ResolutionModifiedHdl )
233 : : {
234 [ # # ]: 0 : m_dResolution = (double) m_aResolutionLB.GetText().ToInt32();
235 : :
236 : 0 : UpdateNewWidthMF();
237 : 0 : UpdateNewHeightMF();
238 : 0 : Update();
239 : :
240 : 0 : return 0L;
241 : : }
242 : :
243 : 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ToggleCompressionRB )
244 : : {
245 : 0 : bool choice = m_aLosslessRB.IsChecked();
246 : 0 : m_aCompressionMF.Enable(choice);
247 : 0 : m_aQualityMF.Enable(!choice);
248 : :
249 : 0 : return 0L;
250 : : }
251 : :
252 : 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, ToggleReduceResolutionRB )
253 : : {
254 : 0 : bool choice = m_aReduceResolutionCB.IsChecked();
255 : 0 : m_aMFNewWidth.Enable(choice);
256 : 0 : m_aMFNewHeight.Enable(choice);
257 : 0 : m_aResolutionLB.Enable(choice);
258 : :
259 : 0 : return 0L;
260 : : }
261 : :
262 : 0 : IMPL_LINK_NOARG( CompressGraphicsDialog, CalculateClickHdl )
263 : : {
264 : 0 : sal_Int32 aSize = 0;
265 : :
266 [ # # ]: 0 : if ( m_dResolution > 0 )
267 : : {
268 [ # # ]: 0 : SvMemoryStream aMemStream;
269 : 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
270 [ # # ]: 0 : Compress( aMemStream );
271 [ # # ]: 0 : aMemStream.Seek( STREAM_SEEK_TO_END );
272 [ # # ]: 0 : aSize = aMemStream.Tell();
273 : : }
274 : :
275 [ # # ]: 0 : if ( aSize > 0 )
276 : : {
277 [ # # ]: 0 : String aNewSizeString;
278 [ # # ][ # # ]: 0 : aNewSizeString += UniString::CreateFromInt32(aSize / 1024);
[ # # ]
279 [ # # ][ # # ]: 0 : aNewSizeString += String( " kiB" ) ;
[ # # ]
280 [ # # ][ # # ]: 0 : m_aFixedText6.SetText(aNewSizeString);
281 : : }
282 : 0 : return 0L;
283 : : }
284 : :
285 : 0 : Graphic CompressGraphicsDialog::GetCompressedGraphic()
286 : : {
287 [ # # ]: 0 : if ( m_dResolution > 0 )
288 : : {
289 [ # # ]: 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
290 [ # # ]: 0 : Graphic aResultGraphic = Graphic ();
291 [ # # ]: 0 : SvMemoryStream aMemStream;
292 : 0 : aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
293 [ # # ]: 0 : Compress( aMemStream );
294 [ # # ]: 0 : aMemStream.Seek( STREAM_SEEK_TO_BEGIN );
295 [ # # ][ # # ]: 0 : rFilter.ImportGraphic( aResultGraphic, String("test"), aMemStream );
[ # # ]
296 [ # # ][ # # ]: 0 : return aResultGraphic;
[ # # ]
297 : : }
298 : 0 : return m_aGraphic;
299 : : }
300 : :
301 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|