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
/* -*- 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 <vcl/bitmap.hxx>
#include <vcl/bitmapex.hxx>
#include <vcl/bitmapaccess.hxx>
#include <vcl/BitmapMosaicFilter.hxx>

#include <bitmapwriteaccess.hxx>

BitmapEx BitmapMosaicFilter::execute(BitmapEx const& rBitmapEx) const
{
    Bitmap aBitmap(rBitmapEx.GetBitmap());

    bool bRet = false;

    if (mnTileWidth > 1 || mnTileHeight > 1)
    {
        std::unique_ptr<Bitmap> pNewBmp;
        BitmapReadAccess* pReadAcc;
        BitmapWriteAccess* pWriteAcc;

        if (aBitmap.GetBitCount() > 8)
        {
            pReadAcc = pWriteAcc = aBitmap.AcquireWriteAccess();
        }
        else
        {
            pNewBmp.reset(new Bitmap(aBitmap.GetSizePixel(), 24));
            pReadAcc = aBitmap.AcquireReadAccess();
            pWriteAcc = pNewBmp->AcquireWriteAccess();
        }

        bool bConditionsMet = false;
        long nWidth(0);
        long nHeight(0);
        if (pReadAcc && pWriteAcc)
        {
            nWidth = pReadAcc->Width();
            nHeight = pReadAcc->Height();
            bConditionsMet = (nWidth > 0 && nHeight > 0);
        }

        if (bConditionsMet)
        {
            BitmapColor aCol;
            long nX, nY, nX1, nX2, nY1, nY2, nSumR, nSumG, nSumB;
            double fArea_1;

            nY1 = 0;
            nY2 = mnTileHeight - 1;

            if (nY2 >= nHeight)
                nY2 = nHeight - 1;

            do
            {
                nX1 = 0;
                nX2 = mnTileWidth - 1;

                if (nX2 >= nWidth)
                    nX2 = nWidth - 1;

                fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));

                if (!pNewBmp)
                {
                    do
                    {
                        for (nY = nY1, nSumR = nSumG = nSumB = 0; nY <= nY2; nY++)
                        {
                            Scanline pScanlineRead = pReadAcc->GetScanline(nY);
                            for (nX = nX1; nX <= nX2; nX++)
                            {
                                aCol = pReadAcc->GetPixelFromData(pScanlineRead, nX);
                                nSumR += aCol.GetRed();
                                nSumG += aCol.GetGreen();
                                nSumB += aCol.GetBlue();
                            }
                        }

                        aCol.SetRed(static_cast<sal_uInt8>(nSumR * fArea_1));
                        aCol.SetGreen(static_cast<sal_uInt8>(nSumG * fArea_1));
                        aCol.SetBlue(static_cast<sal_uInt8>(nSumB * fArea_1));

                        for (nY = nY1; nY <= nY2; nY++)
                        {
                            Scanline pScanline = pWriteAcc->GetScanline(nY);
                            for (nX = nX1; nX <= nX2; nX++)
                                pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
                        }

                        nX1 += mnTileWidth;
                        nX2 += mnTileWidth;

                        if (nX2 >= nWidth)
                        {
                            nX2 = nWidth - 1;
                            fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));
                        }
                    } while (nX1 < nWidth);
                }
                else
                {
                    do
                    {
                        for (nY = nY1, nSumR = nSumG = nSumB = 0; nY <= nY2; nY++)
                        {
                            Scanline pScanlineRead = pReadAcc->GetScanline(nY);
                            for (nX = nX1; nX <= nX2; nX++)
                            {
                                const BitmapColor& rCol = pReadAcc->GetPaletteColor(
                                    pReadAcc->GetIndexFromData(pScanlineRead, nX));
                                nSumR += rCol.GetRed();
                                nSumG += rCol.GetGreen();
                                nSumB += rCol.GetBlue();
                            }
                        }

                        aCol.SetRed(static_cast<sal_uInt8>(nSumR * fArea_1));
                        aCol.SetGreen(static_cast<sal_uInt8>(nSumG * fArea_1));
                        aCol.SetBlue(static_cast<sal_uInt8>(nSumB * fArea_1));

                        for (nY = nY1; nY <= nY2; nY++)
                        {
                            Scanline pScanline = pWriteAcc->GetScanline(nY);
                            for (nX = nX1; nX <= nX2; nX++)
                                pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
                        }

                        nX1 += mnTileWidth;
                        nX2 += mnTileWidth;

                        if (nX2 >= nWidth)
                        {
                            nX2 = nWidth - 1;
                            fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));
                        }
                    } while (nX1 < nWidth);
                }

                nY1 += mnTileHeight;
                nY2 += mnTileHeight;

                if (nY2 >= nHeight)
                    nY2 = nHeight - 1;

            } while (nY1 < nHeight);

            bRet = true;
        }

        Bitmap::ReleaseAccess(pReadAcc);

        if (pNewBmp)
        {
            Bitmap::ReleaseAccess(pWriteAcc);

            if (bRet)
            {
                const MapMode aMap(aBitmap.GetPrefMapMode());
                const Size aPrefSize(aBitmap.GetPrefSize());

                aBitmap = *pNewBmp;

                aBitmap.SetPrefMapMode(aMap);
                aBitmap.SetPrefSize(aPrefSize);
            }
        }
    }

    if (bRet)
        return BitmapEx(aBitmap);

    return BitmapEx();
}

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