summaryrefslogtreecommitdiff
path: root/vcl/source/bitmap/BitmapBasicMorphologyFilter.cxx
blob: 8bc038b486f9ed4a28bda3084f802190c6116c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include <sal/config.h>

#include <comphelper/threadpool.hxx>
#include <sal/log.hxx>
#include <vcl/BitmapBasicMorphologyFilter.hxx>

#include <bitmap/BitmapWriteAccess.hxx>

#include <algorithm>

/* TODO: Use round kernel instead of square one.
   This would make the result more natural, e.g. not making rounded square out of circle.
 */

namespace
{
struct FilterSharedData
{
    BitmapReadAccess* mpReadAccess;
    BitmapWriteAccess* mpWriteAccess;
    tools::Long mnRadius;
    sal_uInt8 mnOutsideVal;
    Color maOutsideColor;

    FilterSharedData(Bitmap::ScopedReadAccess& rReadAccess, BitmapScopedWriteAccess& rWriteAccess,
                     tools::Long nRadius, sal_uInt8 nOutsideVal)
        : mpReadAccess(rReadAccess.get())
        , mpWriteAccess(rWriteAccess.get())
        , mnRadius(nRadius)
        , mnOutsideVal(nOutsideVal)
        , maOutsideColor(ColorTransparency, nOutsideVal, nOutsideVal, nOutsideVal, nOutsideVal)
    {
    }
};

// Black is foreground, white is background

struct ErodeOp
{
    static sal_uInt8 apply(sal_uInt8 v1, sal_uInt8 v2) { return std::max(v1, v2); }
    static constexpr sal_uInt8 initVal = 0;
};

struct DilateOp
{
    static sal_uInt8 apply(sal_uInt8 v1, sal_uInt8 v2) { return std::min(v1, v2); }
    static constexpr sal_uInt8 initVal = SAL_MAX_UINT8;
};

// 8 bit per channel case

template <typename MorphologyOp, int nComponentWidth> struct Value
{
    static constexpr int nWidthBytes = nComponentWidth / 8;
    static_assert(nWidthBytes * 8 == nComponentWidth);

    sal_uInt8 aResult[nWidthBytes];

    // If we are at the start or at the end of the line, consider outside value
    Value(FilterSharedData const& rShared, bool bLookOutside)
    {
        std::fill_n(aResult, nWidthBytes,
                    bLookOutside ? rShared.mnOutsideVal : MorphologyOp::initVal);
    }

    void apply(const BitmapReadAccess* pReadAccess, tools::Long x, tools::Long y,
               sal_uInt8* pHint = nullptr)
    {
        sal_uInt8* pSource = (pHint ? pHint : pReadAccess->GetScanline(y)) + nWidthBytes * x;
        std::transform(pSource, pSource + nWidthBytes, aResult, aResult, MorphologyOp::apply);
    }

    void copy(const BitmapWriteAccess* pWriteAccess, tools::Long x, tools::Long y,
              sal_uInt8* pHint = nullptr)
    {
        sal_uInt8* pDest = (pHint ? pHint : pWriteAccess->GetScanline(y)) + nWidthBytes * x;
        std::copy_n(aResult, nWidthBytes, pDest);
    }
};

// Partial specializations for nComponentWidth == 0, using access' GetColor/SetPixel

template <typename MorphologyOp> struct Value<MorphologyOp, 0>
{
    static constexpr Color initColor{ ColorTransparency, MorphologyOp::initVal,
                                      MorphologyOp::initVal, MorphologyOp::initVal,
                                      MorphologyOp::initVal };

    Color aResult;

    // If we are at the start or at the end of the line, consider outside value
    Value(FilterSharedData const& rShared, bool bLookOutside)
        : aResult(bLookOutside ? rShared.maOutsideColor : initColor)
    {
    }

    void apply(const BitmapReadAccess* pReadAccess, tools::Long x, tools::Long y,
               sal_uInt8* /*pHint*/ = nullptr)
    {
        const auto& rSource = pReadAccess->GetColor(y, x);
        aResult = Color(ColorAlpha, MorphologyOp::apply(rSource.GetAlpha(), aResult.GetAlpha()),
                        MorphologyOp::apply(rSource.GetRed(), aResult.GetRed()),
                        MorphologyOp::apply(rSource.GetGreen(), aResult.GetGreen()),
                        MorphologyOp::apply(rSource.GetBlue(), aResult.GetBlue()));
    }

    void copy(BitmapWriteAccess* pWriteAccess, tools::Long x, tools::Long y,
              sal_uInt8* /*pHint*/ = nullptr)
    {
        pWriteAccess->SetPixel(y, x, aResult);
    }
};

bool GetMinMax(tools::Long nCenter, tools::Long nRadius, tools::Long nMaxLimit, tools::Long& nMin,
               tools::Long& nMax)
{
    nMin = nCenter - nRadius;
    nMax = nCenter + nRadius;
    bool bLookOutside = false;
    if (nMin < 0)
    {
        bLookOutside = true;
        nMin = 0;
    }
    if (nMax > nMaxLimit)
    {
        bLookOutside = true;
        nMax = nMaxLimit;
    }
    return bLookOutside;
}

template <typename MorphologyOp, int nComponentWidth> struct pass
{
    static void Horizontal(FilterSharedData const& rShared, const tools::Long nStart,
                           const tools::Long nEnd)
    {
        BitmapReadAccess* pReadAccess = rShared.mpReadAccess;
        BitmapWriteAccess* pWriteAccess = rShared.mpWriteAccess;

        const tools::Long nLastIndex = pReadAccess->Width() - 1;

        for (tools::Long y = nStart; y <= nEnd; y++)
        {
            // Optimization
            sal_uInt8* const pSourceHint = pReadAccess->GetScanline(y);
            sal_uInt8* const pDestHint = pWriteAccess->GetScanline(y);
            for (tools::Long x = 0; x <= nLastIndex; x++)
            {
                // This processes [nRadius * 2 + 1] pixels of source per resulting pixel
                // TODO: try to optimize this to not process same pixels repeatedly
                tools::Long iMin, iMax;
                const bool bLookOutside = GetMinMax(x, rShared.mnRadius, nLastIndex, iMin, iMax);
                Value<MorphologyOp, nComponentWidth> aResult(rShared, bLookOutside);
                for (tools::Long i = iMin; i <= iMax; ++i)
                    aResult.apply(pReadAccess, i, y, pSourceHint);

                aResult.copy(pWriteAccess, x, y, pDestHint);
            }
        }
    }

    static void Vertical(FilterSharedData const& rShared, const tools::Long nStart,
                         const tools::Long nEnd)
    {
        BitmapReadAccess* pReadAccess = rShared.mpReadAccess;
        BitmapWriteAccess* pWriteAccess = rShared.mpWriteAccess;

        const tools::Long nLastIndex = pReadAccess->Height() - 1;

        for (tools::Long x = nStart; x <= nEnd; x++)
        {
            for (tools::Long y = 0; y <= nLastIndex; y++)
            {
                // This processes [nRadius * 2 + 1] pixels of source per resulting pixel
                // TODO: try to optimize this to not process same pixels repeatedly
                tools::Long iMin, iMax;
                const bool bLookOutside = GetMinMax(y, rShared.mnRadius, nLastIndex, iMin, iMax);
                Value<MorphologyOp, nComponentWidth> aResult(rShared, bLookOutside);
                for (tools::Long i = iMin; i <= iMax; ++i)
                    aResult.apply(pReadAccess, x, i);

                aResult.copy(pWriteAccess, x, y);
            }
        }
    }
};

typedef void (*passFn)(FilterSharedData const& rShared, tools::Long nStart, tools::Long nEnd);

class FilterTask : public comphelper::ThreadTask
{
    passFn mpFunction;
    FilterSharedData& mrShared;
    tools::Long mnStart;
    tools::Long mnEnd;

public:
    explicit FilterTask(const std::shared_ptr<comphelper::ThreadTaskTag>& pTag, passFn pFunction,
                        FilterSharedData& rShared, tools::Long nStart, tools::Long nEnd)
        : comphelper::ThreadTask(pTag)
        , mpFunction(pFunction)
        , mrShared(rShared)
        , mnStart(nStart)
        , mnEnd(nEnd)
    {
    }

    virtual void doWork() override { mpFunction(mrShared, mnStart, mnEnd); }
};

constexpr tools::Long nThreadStrip = 16;

template <typename MorphologyOp, int nComponentWidth>
void runFilter(Bitmap& rBitmap, const tools::Long nRadius, const bool bParallel,
               bool bUseValueOutside, sal_uInt8 nValueOutside)
{
    using myPass = pass<MorphologyOp, nComponentWidth>;
    const sal_uInt8 nOutsideVal = bUseValueOutside ? nValueOutside : MorphologyOp::initVal;
    if (bParallel)
    {
        try
        {
            comphelper::ThreadPool& rShared = comphelper::ThreadPool::getSharedOptimalPool();
            auto pTag = comphelper::ThreadPool::createThreadTaskTag();

            {
                Bitmap::ScopedReadAccess pReadAccess(rBitmap);
                BitmapScopedWriteAccess pWriteAccess(rBitmap);
                FilterSharedData aSharedData(pReadAccess, pWriteAccess, nRadius, nOutsideVal);

                const tools::Long nLastIndex = pReadAccess->Height() - 1;
                tools::Long nStripStart = 0;
                for (; nStripStart < nLastIndex - nThreadStrip; nStripStart += nThreadStrip)
                {
                    tools::Long nStripEnd = nStripStart + nThreadStrip - 1;
                    auto pTask(std::make_unique<FilterTask>(pTag, myPass::Horizontal, aSharedData,
                                                            nStripStart, nStripEnd));
                    rShared.pushTask(std::move(pTask));
                }
                // Do the last (or the only) strip in main thread without threading overhead
                myPass::Horizontal(aSharedData, nStripStart, nLastIndex);
                rShared.waitUntilDone(pTag);
            }
            {
                Bitmap::ScopedReadAccess pReadAccess(rBitmap);
                BitmapScopedWriteAccess pWriteAccess(rBitmap);
                FilterSharedData aSharedData(pReadAccess, pWriteAccess, nRadius, nOutsideVal);

                const tools::Long nLastIndex = pReadAccess->Width() - 1;
                tools::Long nStripStart = 0;
                for (; nStripStart < nLastIndex - nThreadStrip; nStripStart += nThreadStrip)
                {
                    tools::Long nStripEnd = nStripStart + nThreadStrip - 1;
                    auto pTask(std::make_unique<FilterTask>(pTag, myPass::Vertical, aSharedData,
                                                            nStripStart, nStripEnd));
                    rShared.pushTask(std::move(pTask));
                }
                // Do the last (or the only) strip in main thread without threading overhead
                myPass::Vertical(aSharedData, nStripStart, nLastIndex);
                rShared.waitUntilDone(pTag);
            }
        }
        catch (...)
        {
            SAL_WARN("vcl.gdi", "threaded bitmap blurring failed");
        }
    }
    else
    {
        {
            Bitmap::ScopedReadAccess pReadAccess(rBitmap);
            BitmapScopedWriteAccess pWriteAccess(rBitmap);
            FilterSharedData aSharedData(pReadAccess, pWriteAccess, nRadius, nOutsideVal);
            tools::Long nFirstIndex = 0;
            tools::Long nLastIndex = pReadAccess->Height() - 1;
            myPass::Horizontal(aSharedData, nFirstIndex, nLastIndex);
        }
        {
            Bitmap::ScopedReadAccess pReadAccess(rBitmap);
            BitmapScopedWriteAccess pWriteAccess(rBitmap);
            FilterSharedData aSharedData(pReadAccess, pWriteAccess, nRadius, nOutsideVal);
            tools::Long nFirstIndex = 0;
            tools::Long nLastIndex = pReadAccess->Width() - 1;
            myPass::Vertical(aSharedData, nFirstIndex, nLastIndex);
        }
    }
}

template <int nComponentWidth>
void runFilter(Bitmap& rBitmap, BasicMorphologyOp op, sal_Int32 nRadius, bool bUseValueOutside,
               sal_uInt8 nValueOutside)
{
    const bool bParallel = true;

    if (op == BasicMorphologyOp::erode)
        runFilter<ErodeOp, nComponentWidth>(rBitmap, nRadius, bParallel, bUseValueOutside,
                                            nValueOutside);
    else if (op == BasicMorphologyOp::dilate)
        runFilter<DilateOp, nComponentWidth>(rBitmap, nRadius, bParallel, bUseValueOutside,
                                             nValueOutside);
}

} // end anonymous namespace

BitmapBasicMorphologyFilter::BitmapBasicMorphologyFilter(BasicMorphologyOp op, sal_Int32 nRadius)
    : m_eOp(op)
    , m_nRadius(nRadius)
{
}

BitmapBasicMorphologyFilter::BitmapBasicMorphologyFilter(BasicMorphologyOp op, sal_Int32 nRadius,
                                                         sal_uInt8 nValueOutside)
    : m_eOp(op)
    , m_nRadius(nRadius)
    , m_nValueOutside(nValueOutside)
    , m_bUseValueOutside(true)
{
}

BitmapBasicMorphologyFilter::~BitmapBasicMorphologyFilter() = default;

BitmapEx BitmapBasicMorphologyFilter::execute(BitmapEx const& rBitmapEx) const
{
    Bitmap result = filter(rBitmapEx.GetBitmap());
    return BitmapEx(result, rBitmapEx.GetMask());
}

Bitmap BitmapBasicMorphologyFilter::filter(Bitmap const& rBitmap) const
{
    Bitmap bitmapCopy(rBitmap);
    ScanlineFormat nScanlineFormat;
    {
        Bitmap::ScopedReadAccess pReadAccess(bitmapCopy);
        nScanlineFormat = pReadAccess->GetScanlineFormat();
    }

    switch (nScanlineFormat)
    {
        case ScanlineFormat::N24BitTcRgb:
        case ScanlineFormat::N24BitTcBgr:
            runFilter<24>(bitmapCopy, m_eOp, m_nRadius, m_bUseValueOutside, m_nValueOutside);
            break;
        case ScanlineFormat::N32BitTcMask:
        case ScanlineFormat::N32BitTcBgra:
            runFilter<32>(bitmapCopy, m_eOp, m_nRadius, m_bUseValueOutside, m_nValueOutside);
            break;
        case ScanlineFormat::N8BitPal:
            runFilter<8>(bitmapCopy, m_eOp, m_nRadius, m_bUseValueOutside, m_nValueOutside);
            break;
        // TODO: handle 1-bit images
        default:
            // Use access' GetColor/SetPixel fallback
            runFilter<0>(bitmapCopy, m_eOp, m_nRadius, m_bUseValueOutside, m_nValueOutside);
            break;
    }

    return bitmapCopy;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */