summaryrefslogtreecommitdiff
path: root/vcl/source/bitmap/BitmapSimpleColorQuantizationFilter.cxx
blob: 4284db563ac318e50b1a368929e9feb69ffb44c9 (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
/* -*- 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/BitmapSimpleColorQuantizationFilter.hxx>

#include <bitmapwriteaccess.hxx>
#include <bitmap/Octree.hxx>

BitmapEx BitmapSimpleColorQuantizationFilter::execute(BitmapEx const& aBitmapEx) const
{
    Bitmap aBitmap = aBitmapEx.GetBitmap();

    bool bRet = false;

    if (aBitmap.GetColorCount() <= sal_Int64(mnNewColorCount))
    {
        bRet = true;
    }
    else
    {
        Bitmap aNewBmp;
        Bitmap::ScopedReadAccess pRAcc(aBitmap);
        const sal_uInt16 nColorCount = std::min(mnNewColorCount, sal_uInt16(256));
        sal_uInt16 nBitCount = 0;

        if (nColorCount <= 2)
            nBitCount = 1;
        else if (nColorCount <= 16)
            nBitCount = 4;
        else
            nBitCount = 8;

        if (pRAcc)
        {
            Octree aOct(*pRAcc, nColorCount);
            const BitmapPalette& rPal = aOct.GetPalette();

            aNewBmp = Bitmap(aBitmap.GetSizePixel(), nBitCount, &rPal);
            BitmapScopedWriteAccess pWAcc(aNewBmp);

            if (pWAcc)
            {
                const tools::Long nWidth = pRAcc->Width();
                const tools::Long nHeight = pRAcc->Height();

                if (pRAcc->HasPalette())
                {
                    for (tools::Long nY = 0; nY < nHeight; nY++)
                    {
                        Scanline pScanline = pWAcc->GetScanline(nY);
                        Scanline pScanlineRead = pRAcc->GetScanline(nY);
                        for (tools::Long nX = 0; nX < nWidth; nX++)
                        {
                            auto c = pRAcc->GetPaletteColor(
                                pRAcc->GetIndexFromData(pScanlineRead, nX));
                            pWAcc->SetPixelOnData(
                                pScanline, nX,
                                BitmapColor(static_cast<sal_uInt8>(aOct.GetBestPaletteIndex(c))));
                        }
                    }
                }
                else
                {
                    for (tools::Long nY = 0; nY < nHeight; nY++)
                    {
                        Scanline pScanline = pWAcc->GetScanline(nY);
                        Scanline pScanlineRead = pRAcc->GetScanline(nY);
                        for (tools::Long nX = 0; nX < nWidth; nX++)
                        {
                            auto c = pRAcc->GetPixelFromData(pScanlineRead, nX);
                            pWAcc->SetPixelOnData(
                                pScanline, nX,
                                BitmapColor(static_cast<sal_uInt8>(aOct.GetBestPaletteIndex(c))));
                        }
                    }
                }

                pWAcc.reset();
                bRet = true;
            }

            pRAcc.reset();
        }

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

            aBitmap = aNewBmp;

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

    if (bRet)
        return BitmapEx(aBitmap);

    return BitmapEx();
}

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