Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/dialog/grfflt.cxx
Line
Count
Source
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 <vcl/animate/Animation.hxx>
21
#include <vcl/bitmap/BitmapSharpenFilter.hxx>
22
#include <vcl/bitmap/BitmapMedianFilter.hxx>
23
#include <vcl/bitmap/BitmapSobelGreyFilter.hxx>
24
#include <vcl/bitmap/BitmapPopArtFilter.hxx>
25
#include <vcl/GraphicObject.hxx>
26
#include <vcl/vclenum.hxx>
27
28
#include <sfx2/viewfrm.hxx>
29
#include <sfx2/viewsh.hxx>
30
#include <sfx2/objsh.hxx>
31
#include <sfx2/request.hxx>
32
33
#include <osl/diagnose.h>
34
#include <svx/grfflt.hxx>
35
#include <svx/svxids.hrc>
36
#include <svx/svxdlg.hxx>
37
38
39
static void handleGraphicFilterDialog(const VclPtr<AbstractGraphicFilterDialog>& pDlg,
40
        const Graphic& aInputGraphic,
41
        const std::function<void(const Graphic&)>& f);
42
43
void SvxGraphicFilter::ExecuteGrfFilterSlot( SfxRequest const & rReq,
44
                        const GraphicObject& rInputObject,
45
                        const std::function<void(const Graphic&)>& f)
46
0
{
47
0
    const Graphic& aInputGraphic = rInputObject.GetGraphic();
48
49
0
    if( aInputGraphic.GetType() != GraphicType::Bitmap )
50
0
        return;
51
52
0
    SfxViewFrame*   pViewFrame = SfxViewFrame::Current();
53
0
    SfxObjectShell* pShell = pViewFrame ? pViewFrame->GetObjectShell() : nullptr;
54
0
    weld::Window*   pFrameWeld = (pViewFrame && pViewFrame->GetViewShell()) ? pViewFrame->GetViewShell()->GetFrameWeld() : nullptr;
55
56
0
    switch( rReq.GetSlot() )
57
0
    {
58
0
        case SID_GRFFILTER_INVERT:
59
0
        {
60
0
            Graphic aOutputGraphic;
61
62
0
            if( pShell )
63
0
                pShell->SetWaitCursor( true );
64
65
0
            if( aInputGraphic.IsAnimated() )
66
0
            {
67
0
                Animation aAnimation( aInputGraphic.GetAnimation() );
68
69
0
                if( aAnimation.Invert() )
70
0
                    aOutputGraphic = aAnimation;
71
0
            }
72
0
            else
73
0
            {
74
0
                Bitmap aBmp( aInputGraphic.GetBitmap() );
75
76
0
                if( aBmp.Invert() )
77
0
                    aOutputGraphic = aBmp;
78
0
            }
79
80
0
            if( pShell )
81
0
                pShell->SetWaitCursor( false );
82
83
0
            if( aOutputGraphic.GetType() != GraphicType::NONE )
84
0
                f(aOutputGraphic);
85
0
        }
86
0
        break;
87
88
0
        case SID_GRFFILTER_SMOOTH:
89
0
        {
90
0
            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
91
0
            handleGraphicFilterDialog(pFact->CreateGraphicFilterSmooth(pFrameWeld, aInputGraphic, 0.7), aInputGraphic, f);
92
0
        }
93
0
        break;
94
95
0
        case SID_GRFFILTER_SHARPEN:
96
0
        {
97
0
            Graphic aOutputGraphic;
98
99
0
            if( pShell )
100
0
                pShell->SetWaitCursor( true );
101
102
0
            if( aInputGraphic.IsAnimated() )
103
0
            {
104
0
                Animation aAnimation( aInputGraphic.GetAnimation() );
105
106
0
                if (BitmapFilter::Filter(aAnimation, BitmapSharpenFilter()))
107
0
                    aOutputGraphic = aAnimation;
108
0
            }
109
0
            else
110
0
            {
111
0
                Bitmap aBmp( aInputGraphic.GetBitmap() );
112
113
0
                if (BitmapFilter::Filter(aBmp, BitmapSharpenFilter()))
114
0
                    aOutputGraphic = aBmp;
115
0
            }
116
117
0
            if( pShell )
118
0
                pShell->SetWaitCursor( false );
119
120
0
            if( aOutputGraphic.GetType() != GraphicType::NONE )
121
0
                f(aOutputGraphic);
122
0
        }
123
0
        break;
124
125
0
        case SID_GRFFILTER_REMOVENOISE:
126
0
        {
127
0
            Graphic aOutputGraphic;
128
129
0
            if( pShell )
130
0
                pShell->SetWaitCursor( true );
131
132
0
            if( aInputGraphic.IsAnimated() )
133
0
            {
134
0
                Animation aAnimation( aInputGraphic.GetAnimation() );
135
136
0
                if (BitmapFilter::Filter(aAnimation, BitmapMedianFilter()))
137
0
                    aOutputGraphic = aAnimation;
138
0
            }
139
0
            else
140
0
            {
141
0
                Bitmap aBmp( aInputGraphic.GetBitmap() );
142
143
0
                if (BitmapFilter::Filter(aBmp, BitmapMedianFilter()))
144
0
                    aOutputGraphic = aBmp;
145
0
            }
146
147
0
            if( pShell )
148
0
                pShell->SetWaitCursor( false );
149
150
0
            if( aOutputGraphic.GetType() != GraphicType::NONE )
151
0
                f(aOutputGraphic);
152
0
        }
153
0
        break;
154
155
0
        case SID_GRFFILTER_SOBEL:
156
0
        {
157
0
            Graphic aOutputGraphic;
158
159
0
            if( pShell )
160
0
                pShell->SetWaitCursor( true );
161
162
0
            if( aInputGraphic.IsAnimated() )
163
0
            {
164
0
                Animation aAnimation( aInputGraphic.GetAnimation() );
165
166
0
                if (BitmapFilter::Filter(aAnimation, BitmapSobelGreyFilter()))
167
0
                    aOutputGraphic = aAnimation;
168
0
            }
169
0
            else
170
0
            {
171
0
                Bitmap aBmp( aInputGraphic.GetBitmap() );
172
173
0
                if (BitmapFilter::Filter(aBmp, BitmapSobelGreyFilter()))
174
0
                    aOutputGraphic = aBmp;
175
0
            }
176
177
0
            if( pShell )
178
0
                pShell->SetWaitCursor( false );
179
180
0
            if( aOutputGraphic.GetType() != GraphicType::NONE )
181
0
                f(aOutputGraphic);
182
0
        }
183
0
        break;
184
185
0
        case SID_GRFFILTER_MOSAIC:
186
0
        {
187
0
            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
188
0
            handleGraphicFilterDialog(pFact->CreateGraphicFilterMosaic(pFrameWeld, aInputGraphic), aInputGraphic, f);
189
0
        }
190
0
        break;
191
192
0
        case SID_GRFFILTER_EMBOSS:
193
0
        {
194
0
            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
195
0
            handleGraphicFilterDialog(pFact->CreateGraphicFilterEmboss(pFrameWeld, aInputGraphic), aInputGraphic, f);
196
0
        }
197
0
        break;
198
199
0
        case SID_GRFFILTER_POSTER:
200
0
        {
201
0
            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
202
0
            handleGraphicFilterDialog(pFact->CreateGraphicFilterPoster(pFrameWeld, aInputGraphic), aInputGraphic, f);
203
0
        }
204
0
        break;
205
206
0
        case SID_GRFFILTER_POPART:
207
0
        {
208
0
            Graphic aOutputGraphic;
209
210
0
            if( pShell )
211
0
                pShell->SetWaitCursor( true );
212
213
0
            if( aInputGraphic.IsAnimated() )
214
0
            {
215
0
                Animation aAnimation( aInputGraphic.GetAnimation() );
216
217
0
                if (BitmapFilter::Filter(aAnimation, BitmapPopArtFilter()))
218
0
                    aOutputGraphic = aAnimation;
219
0
            }
220
0
            else
221
0
            {
222
0
                Bitmap aBmp( aInputGraphic.GetBitmap() );
223
224
0
                if (BitmapFilter::Filter(aBmp, BitmapPopArtFilter()))
225
0
                    aOutputGraphic = aBmp;
226
0
            }
227
228
0
            if( pShell )
229
0
                pShell->SetWaitCursor( false );
230
231
0
            if( aOutputGraphic.GetType() != GraphicType::NONE )
232
0
                f(aOutputGraphic);
233
0
        }
234
0
        break;
235
236
0
        case SID_GRFFILTER_SEPIA:
237
0
        {
238
0
            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
239
0
            handleGraphicFilterDialog(pFact->CreateGraphicFilterSepia(pFrameWeld, aInputGraphic), aInputGraphic, f);
240
0
        }
241
0
        break;
242
243
0
        case SID_GRFFILTER_SOLARIZE:
244
0
        {
245
0
            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
246
0
            handleGraphicFilterDialog(pFact->CreateGraphicFilterSolarize(pFrameWeld, aInputGraphic), aInputGraphic, f);
247
0
        }
248
0
        break;
249
250
0
        case SID_GRFFILTER :
251
0
        {
252
            // do nothing; no error
253
0
            return;
254
0
        }
255
256
0
        default:
257
0
        {
258
0
            OSL_FAIL( "SvxGraphicFilter: selected filter slot not yet implemented" );
259
0
            return;
260
0
        }
261
0
    }
262
0
}
263
264
static void handleGraphicFilterDialog(const VclPtr<AbstractGraphicFilterDialog>& pDlg,
265
        const Graphic& aInputGraphic,
266
        const std::function<void(const Graphic&)>& f)
267
0
{
268
0
    pDlg->StartExecuteAsync(
269
0
        [pDlg, aInputGraphic, f] (sal_Int32 nResult)->void
270
0
        {
271
0
            if (nResult == RET_OK)
272
0
                f(pDlg->GetFilteredGraphic(aInputGraphic, 1.0, 1.0));
273
0
            pDlg->disposeOnce();
274
0
        }
275
0
    );
276
0
}
277
278
void SvxGraphicFilter::DisableGraphicFilterSlots( SfxItemSet& rSet )
279
0
{
280
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER ) )
281
0
        rSet.DisableItem( SID_GRFFILTER );
282
283
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_INVERT ) )
284
0
        rSet.DisableItem( SID_GRFFILTER_INVERT );
285
286
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_SMOOTH ) )
287
0
        rSet.DisableItem( SID_GRFFILTER_SMOOTH );
288
289
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_SHARPEN ) )
290
0
        rSet.DisableItem( SID_GRFFILTER_SHARPEN );
291
292
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_REMOVENOISE ) )
293
0
        rSet.DisableItem( SID_GRFFILTER_REMOVENOISE );
294
295
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_SOBEL ) )
296
0
        rSet.DisableItem( SID_GRFFILTER_SOBEL );
297
298
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_MOSAIC ) )
299
0
        rSet.DisableItem( SID_GRFFILTER_MOSAIC );
300
301
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_EMBOSS ) )
302
0
        rSet.DisableItem( SID_GRFFILTER_EMBOSS );
303
304
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_POSTER ) )
305
0
        rSet.DisableItem( SID_GRFFILTER_POSTER );
306
307
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_POPART ) )
308
0
        rSet.DisableItem( SID_GRFFILTER_POPART );
309
310
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_SEPIA ) )
311
0
        rSet.DisableItem( SID_GRFFILTER_SEPIA );
312
313
0
    if( SfxItemState::DEFAULT <= rSet.GetItemState( SID_GRFFILTER_SOLARIZE ) )
314
0
        rSet.DisableItem( SID_GRFFILTER_SOLARIZE );
315
0
};
316
317
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */