1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/BitmapMosaicFilter.hxx>
21 #include <vcl/BitmapSharpenFilter.hxx>
22 #include <vcl/BitmapEmbossGreyFilter.hxx>
23 #include <vcl/BitmapSepiaFilter.hxx>
24 #include <vcl/BitmapSmoothenFilter.hxx>
25 #include <vcl/BitmapSolarizeFilter.hxx>
26 #include <vcl/BitmapColorQuantizationFilter.hxx>
27 #include <vcl/settings.hxx>
28 #include <vcl/svapp.hxx>
29 #include <osl/diagnose.h>
30 #include <tools/helpers.hxx>
31 #include <cuigrfflt.hxx>
33 CuiGraphicPreviewWindow::CuiGraphicPreviewWindow()
34 : mpOrigGraphic(nullptr)
40 void CuiGraphicPreviewWindow::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
42 CustomWidgetController::SetDrawingArea(pDrawingArea
);
43 OutputDevice
&rDevice
= pDrawingArea
->get_ref_device();
44 maOutputSizePixel
= rDevice
.LogicToPixel(Size(81, 73), MapMode(MapUnit::MapAppFont
));
45 pDrawingArea
->set_size_request(maOutputSizePixel
.Width(), maOutputSizePixel
.Height());
48 void CuiGraphicPreviewWindow::Paint(vcl::RenderContext
& rRenderContext
, const ::tools::Rectangle
&)
50 rRenderContext
.SetBackground(Wallpaper(Application::GetSettings().GetStyleSettings().GetDialogColor()));
51 rRenderContext
.Erase();
53 const Size
aOutputSize(GetOutputSizePixel());
55 if (maPreview
.IsAnimated())
57 const Size
aGraphicSize(rRenderContext
.LogicToPixel(maPreview
.GetPrefSize(), maPreview
.GetPrefMapMode()));
58 const Point
aGraphicPosition((aOutputSize
.Width() - aGraphicSize
.Width() ) >> 1,
59 (aOutputSize
.Height() - aGraphicSize
.Height() ) >> 1);
60 maPreview
.StartAnimation(&rRenderContext
, aGraphicPosition
, aGraphicSize
);
64 const Size
aGraphicSize(maPreview
.GetSizePixel());
65 const Point
aGraphicPosition((aOutputSize
.Width() - aGraphicSize
.Width()) >> 1,
66 (aOutputSize
.Height() - aGraphicSize
.Height()) >> 1);
67 maPreview
.Draw(&rRenderContext
, aGraphicPosition
, aGraphicSize
);
71 void CuiGraphicPreviewWindow::SetPreview(const Graphic
& rGraphic
)
77 void CuiGraphicPreviewWindow::ScaleImageToFit()
82 maScaledOrig
= *mpOrigGraphic
;
84 const Size
aPreviewSize(GetOutputSizePixel());
85 Size
aGrfSize(maOrigGraphicSizePixel
);
87 if( mpOrigGraphic
->GetType() == GraphicType::Bitmap
&&
88 aPreviewSize
.Width() && aPreviewSize
.Height() &&
89 aGrfSize
.Width() && aGrfSize
.Height() )
91 const double fGrfWH
= static_cast<double>(aGrfSize
.Width()) / aGrfSize
.Height();
92 const double fPreWH
= static_cast<double>(aPreviewSize
.Width()) / aPreviewSize
.Height();
96 aGrfSize
.setWidth( static_cast<long>( aPreviewSize
.Height() * fGrfWH
) );
97 aGrfSize
.setHeight( aPreviewSize
.Height() );
101 aGrfSize
.setWidth( aPreviewSize
.Width() );
102 aGrfSize
.setHeight( static_cast<long>( aPreviewSize
.Width() / fGrfWH
) );
105 mfScaleX
= static_cast<double>(aGrfSize
.Width()) / maOrigGraphicSizePixel
.Width();
106 mfScaleY
= static_cast<double>(aGrfSize
.Height()) / maOrigGraphicSizePixel
.Height();
108 if( !mpOrigGraphic
->IsAnimated() )
110 BitmapEx
aBmpEx( mpOrigGraphic
->GetBitmapEx() );
112 if( aBmpEx
.Scale( aGrfSize
) )
113 maScaledOrig
= aBmpEx
;
117 maModifyHdl
.Call(nullptr);
120 void CuiGraphicPreviewWindow::Resize()
122 maOutputSizePixel
= GetOutputSizePixel();
126 GraphicFilterDialog::GraphicFilterDialog(weld::Window
* pParent
,
127 const OUString
& rUIXMLDescription
, const OString
& rID
,
128 const Graphic
& rGraphic
)
129 : GenericDialogController(pParent
, rUIXMLDescription
, rID
)
130 , maModifyHdl(LINK(this, GraphicFilterDialog
, ImplModifyHdl
))
131 , mxPreview(new weld::CustomWeld(*m_xBuilder
, "preview", maPreview
))
133 bIsBitmap
= rGraphic
.GetType() == GraphicType::Bitmap
;
135 maTimer
.SetInvokeHandler(LINK(this, GraphicFilterDialog
, ImplPreviewTimeoutHdl
));
136 maTimer
.SetTimeout(5);
138 maPreview
.init(&rGraphic
, maModifyHdl
);
141 IMPL_LINK_NOARG(GraphicFilterDialog
, ImplPreviewTimeoutHdl
, Timer
*, void)
144 maPreview
.SetPreview(GetFilteredGraphic(maPreview
.GetScaledOriginal(),
145 maPreview
.GetScaleX(), maPreview
.GetScaleY()));
148 IMPL_LINK_NOARG(GraphicFilterDialog
, ImplModifyHdl
, LinkParamNone
*, void)
157 GraphicFilterMosaic::GraphicFilterMosaic(weld::Window
* pParent
, const Graphic
& rGraphic
,
158 sal_uInt16 nTileWidth
, sal_uInt16 nTileHeight
, bool bEnhanceEdges
)
159 : GraphicFilterDialog(pParent
, "cui/ui/mosaicdialog.ui", "MosaicDialog", rGraphic
)
160 , mxMtrWidth(m_xBuilder
->weld_metric_spin_button("width", FieldUnit::PIXEL
))
161 , mxMtrHeight(m_xBuilder
->weld_metric_spin_button("height", FieldUnit::PIXEL
))
162 , mxCbxEdges(m_xBuilder
->weld_check_button("edges"))
164 mxMtrWidth
->set_value(nTileWidth
, FieldUnit::PIXEL
);
165 mxMtrWidth
->set_max(GetGraphicSizePixel().Width(), FieldUnit::PIXEL
);
166 mxMtrWidth
->connect_value_changed(LINK(this, GraphicFilterMosaic
, EditModifyHdl
));
168 mxMtrHeight
->set_value(nTileHeight
, FieldUnit::PIXEL
);
169 mxMtrHeight
->set_max(GetGraphicSizePixel().Height(), FieldUnit::PIXEL
);
170 mxMtrHeight
->connect_value_changed(LINK(this, GraphicFilterMosaic
, EditModifyHdl
));
172 mxCbxEdges
->set_active(bEnhanceEdges
);
173 mxCbxEdges
->connect_toggled(LINK(this, GraphicFilterMosaic
, CheckBoxModifyHdl
));
175 mxMtrWidth
->grab_focus();
178 IMPL_LINK_NOARG(GraphicFilterMosaic
, CheckBoxModifyHdl
, weld::ToggleButton
&, void)
180 GetModifyHdl().Call(nullptr);
183 IMPL_LINK_NOARG(GraphicFilterMosaic
, EditModifyHdl
, weld::MetricSpinButton
&, void)
185 GetModifyHdl().Call(nullptr);
188 Graphic
GraphicFilterMosaic::GetFilteredGraphic( const Graphic
& rGraphic
,
189 double fScaleX
, double fScaleY
)
192 long nTileWidth
= static_cast<long>(mxMtrWidth
->get_value(FieldUnit::PIXEL
));
193 long nTileHeight
= static_cast<long>(mxMtrHeight
->get_value(FieldUnit::PIXEL
));
194 const Size
aSize( std::max( FRound( nTileWidth
* fScaleX
), 1L ),
195 std::max( FRound( nTileHeight
* fScaleY
), 1L ) );
197 if( rGraphic
.IsAnimated() )
199 Animation
aAnim( rGraphic
.GetAnimation() );
201 if (BitmapFilter::Filter(aAnim
, BitmapMosaicFilter(aSize
.getWidth(), aSize
.getHeight())))
203 if( IsEnhanceEdges() )
204 (void)BitmapFilter::Filter(aAnim
, BitmapSharpenFilter());
211 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
213 if (BitmapFilter::Filter(aBmpEx
, BitmapMosaicFilter(aSize
.getWidth(), aSize
.getHeight())))
215 if( IsEnhanceEdges() )
216 BitmapFilter::Filter(aBmpEx
, BitmapSharpenFilter());
225 GraphicFilterSmooth::GraphicFilterSmooth(weld::Window
* pParent
, const Graphic
& rGraphic
, double nRadius
)
226 : GraphicFilterDialog(pParent
, "cui/ui/smoothdialog.ui", "SmoothDialog", rGraphic
)
227 , mxMtrRadius(m_xBuilder
->weld_spin_button("radius"))
229 mxMtrRadius
->set_value(nRadius
* 10);
230 mxMtrRadius
->connect_value_changed(LINK(this, GraphicFilterSmooth
, EditModifyHdl
));
231 mxMtrRadius
->grab_focus();
234 IMPL_LINK_NOARG(GraphicFilterSmooth
, EditModifyHdl
, weld::SpinButton
&, void)
236 GetModifyHdl().Call(nullptr);
239 Graphic
GraphicFilterSmooth::GetFilteredGraphic( const Graphic
& rGraphic
, double, double )
242 double nRadius
= mxMtrRadius
->get_value() / 10.0;
244 if( rGraphic
.IsAnimated() )
246 Animation
aAnim( rGraphic
.GetAnimation() );
248 if (BitmapFilter::Filter(aAnim
, BitmapSmoothenFilter(nRadius
)))
255 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
257 if (BitmapFilter::Filter(aBmpEx
, BitmapSmoothenFilter(nRadius
)))
266 GraphicFilterSolarize::GraphicFilterSolarize(weld::Window
* pParent
, const Graphic
& rGraphic
,
267 sal_uInt8 cGreyThreshold
, bool bInvert
)
268 : GraphicFilterDialog(pParent
, "cui/ui/solarizedialog.ui", "SolarizeDialog", rGraphic
)
269 , mxMtrThreshold(m_xBuilder
->weld_metric_spin_button("value", FieldUnit::PERCENT
))
270 , mxCbxInvert(m_xBuilder
->weld_check_button("invert"))
272 mxMtrThreshold
->set_value(FRound(cGreyThreshold
/ 2.55), FieldUnit::PERCENT
);
273 mxMtrThreshold
->connect_value_changed(LINK(this, GraphicFilterSolarize
, EditModifyHdl
));
275 mxCbxInvert
->set_active(bInvert
);
276 mxCbxInvert
->connect_toggled(LINK(this, GraphicFilterSolarize
, CheckBoxModifyHdl
));
279 IMPL_LINK_NOARG(GraphicFilterSolarize
, CheckBoxModifyHdl
, weld::ToggleButton
&, void)
281 GetModifyHdl().Call(nullptr);
284 IMPL_LINK_NOARG(GraphicFilterSolarize
, EditModifyHdl
, weld::MetricSpinButton
&, void)
286 GetModifyHdl().Call(nullptr);
289 Graphic
GraphicFilterSolarize::GetFilteredGraphic( const Graphic
& rGraphic
, double, double )
292 sal_uInt8 nGreyThreshold
= static_cast<sal_uInt8
>(FRound(mxMtrThreshold
->get_value(FieldUnit::PERCENT
) * 2.55));
294 if( rGraphic
.IsAnimated() )
296 Animation
aAnim( rGraphic
.GetAnimation() );
298 if (BitmapFilter::Filter(aAnim
, BitmapSolarizeFilter(nGreyThreshold
)))
308 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
310 if (BitmapFilter::Filter(aBmpEx
, BitmapSolarizeFilter(nGreyThreshold
)))
322 GraphicFilterSepia::GraphicFilterSepia(weld::Window
* pParent
, const Graphic
& rGraphic
,
323 sal_uInt16 nSepiaPercent
)
324 : GraphicFilterDialog(pParent
, "cui/ui/agingdialog.ui", "AgingDialog", rGraphic
)
325 , mxMtrSepia(m_xBuilder
->weld_metric_spin_button("value", FieldUnit::PERCENT
))
327 mxMtrSepia
->set_value(nSepiaPercent
, FieldUnit::PERCENT
);
328 mxMtrSepia
->connect_value_changed(LINK(this, GraphicFilterSepia
, EditModifyHdl
));
331 IMPL_LINK_NOARG(GraphicFilterSepia
, EditModifyHdl
, weld::MetricSpinButton
&, void)
333 GetModifyHdl().Call(nullptr);
336 Graphic
GraphicFilterSepia::GetFilteredGraphic( const Graphic
& rGraphic
, double, double )
339 sal_uInt16 nSepiaPct
= sal::static_int_cast
< sal_uInt16
>(mxMtrSepia
->get_value(FieldUnit::PERCENT
));
341 if( rGraphic
.IsAnimated() )
343 Animation
aAnim( rGraphic
.GetAnimation() );
345 if (BitmapFilter::Filter(aAnim
, BitmapSepiaFilter(nSepiaPct
)))
350 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
352 if (BitmapFilter::Filter(aBmpEx
, BitmapSepiaFilter(nSepiaPct
)))
359 GraphicFilterPoster::GraphicFilterPoster(weld::Window
* pParent
, const Graphic
& rGraphic
,
360 sal_uInt16 nPosterCount
)
361 : GraphicFilterDialog(pParent
, "cui/ui/posterdialog.ui", "PosterDialog", rGraphic
)
362 , mxNumPoster(m_xBuilder
->weld_spin_button("value"))
364 mxNumPoster
->set_range(2, rGraphic
.GetBitmapEx().GetBitCount());
365 mxNumPoster
->set_value(nPosterCount
);
366 mxNumPoster
->connect_value_changed(LINK(this, GraphicFilterPoster
, EditModifyHdl
));
369 IMPL_LINK_NOARG(GraphicFilterPoster
, EditModifyHdl
, weld::SpinButton
&, void)
371 GetModifyHdl().Call(nullptr);
374 Graphic
GraphicFilterPoster::GetFilteredGraphic( const Graphic
& rGraphic
, double, double )
377 const sal_uInt16 nPosterCount
= static_cast<sal_uInt16
>(mxNumPoster
->get_value());
379 if( rGraphic
.IsAnimated() )
381 Animation
aAnim( rGraphic
.GetAnimation() );
383 if( aAnim
.ReduceColors( nPosterCount
) )
388 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
390 if (BitmapFilter::Filter(aBmpEx
, BitmapColorQuantizationFilter(nPosterCount
)))
397 bool EmbossControl::MouseButtonDown( const MouseEvent
& rEvt
)
399 const RectPoint eOldRP
= GetActualRP();
401 SvxRectCtl::MouseButtonDown( rEvt
);
403 if( GetActualRP() != eOldRP
)
404 maModifyHdl
.Call( nullptr );
409 void EmbossControl::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
411 SvxRectCtl::SetDrawingArea(pDrawingArea
);
412 Size
aSize(pDrawingArea
->get_ref_device().LogicToPixel(Size(77, 60), MapMode(MapUnit::MapAppFont
)));
413 pDrawingArea
->set_size_request(aSize
.Width(), aSize
.Height());
416 GraphicFilterEmboss::GraphicFilterEmboss(weld::Window
* pParent
,
417 const Graphic
& rGraphic
, RectPoint eLightSource
)
418 : GraphicFilterDialog(pParent
, "cui/ui/embossdialog.ui", "EmbossDialog", rGraphic
)
419 , mxCtlLight(new weld::CustomWeld(*m_xBuilder
, "lightsource", maCtlLight
))
421 maCtlLight
.SetActualRP(eLightSource
);
422 maCtlLight
.SetModifyHdl( GetModifyHdl() );
423 maCtlLight
.GrabFocus();
426 GraphicFilterEmboss::~GraphicFilterEmboss()
430 Graphic
GraphicFilterEmboss::GetFilteredGraphic( const Graphic
& rGraphic
, double, double )
433 sal_uInt16 nAzim
, nElev
;
435 switch (maCtlLight
.GetActualRP())
437 default: OSL_FAIL("svx::GraphicFilterEmboss::GetFilteredGraphic(), unknown Reference Point!" );
439 case RectPoint::LT
: nAzim
= 4500; nElev
= 4500; break;
440 case RectPoint::MT
: nAzim
= 9000; nElev
= 4500; break;
441 case RectPoint::RT
: nAzim
= 13500; nElev
= 4500; break;
442 case RectPoint::LM
: nAzim
= 0; nElev
= 4500; break;
443 case RectPoint::MM
: nAzim
= 0; nElev
= 9000; break;
444 case RectPoint::RM
: nAzim
= 18000; nElev
= 4500; break;
445 case RectPoint::LB
: nAzim
= 31500; nElev
= 4500; break;
446 case RectPoint::MB
: nAzim
= 27000; nElev
= 4500; break;
447 case RectPoint::RB
: nAzim
= 22500; nElev
= 4500; break;
450 if( rGraphic
.IsAnimated() )
452 Animation
aAnim( rGraphic
.GetAnimation() );
454 if (BitmapFilter::Filter(aAnim
, BitmapEmbossGreyFilter(nAzim
, nElev
)))
459 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
461 if (BitmapFilter::Filter(aBmpEx
, BitmapEmbossGreyFilter(nAzim
, nElev
)))
468 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */