Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / bitmap / BitmapPopArtFilter.cxx
bloba335f04fb824140a4356b667efba888762bc0f49
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 */
11 #include <vcl/bitmap.hxx>
12 #include <vcl/bitmapex.hxx>
13 #include <vcl/BitmapPopArtFilter.hxx>
15 #include <bitmapwriteaccess.hxx>
17 BitmapEx BitmapPopArtFilter::execute(BitmapEx const& rBitmapEx) const
19 Bitmap aBitmap(rBitmapEx.GetBitmap());
21 bool bRet = (aBitmap.GetBitCount() <= 8) || aBitmap.Convert(BmpConversion::N8BitColors);
23 if (bRet)
25 bRet = false;
27 BitmapScopedWriteAccess pWriteAcc(aBitmap);
29 if (pWriteAcc)
31 const long nWidth = pWriteAcc->Width();
32 const long nHeight = pWriteAcc->Height();
33 const int nEntryCount = 1 << pWriteAcc->GetBitCount();
34 int n = 0;
35 std::vector<PopArtEntry> aPopArtTable(nEntryCount);
37 for (n = 0; n < nEntryCount; n++)
39 PopArtEntry& rEntry = aPopArtTable[n];
40 rEntry.mnIndex = static_cast<sal_uInt16>(n);
41 rEntry.mnCount = 0;
44 // get pixel count for each palette entry
45 for (long nY = 0; nY < nHeight; nY++)
47 Scanline pScanline = pWriteAcc->GetScanline(nY);
48 for (long nX = 0; nX < nWidth; nX++)
50 aPopArtTable[pWriteAcc->GetIndexFromData(pScanline, nX)].mnCount++;
54 // sort table
55 std::sort(aPopArtTable.begin(), aPopArtTable.end(),
56 [](const PopArtEntry& lhs, const PopArtEntry& rhs) {
57 return lhs.mnCount < rhs.mnCount;
58 });
60 // get last used entry
61 sal_uLong nFirstEntry;
62 sal_uLong nLastEntry = 0;
64 for (n = 0; n < nEntryCount; n++)
66 if (aPopArtTable[n].mnCount)
67 nLastEntry = n;
70 // rotate palette (one entry)
71 const BitmapColor aFirstCol(pWriteAcc->GetPaletteColor(
72 sal::static_int_cast<sal_uInt16>(aPopArtTable[0].mnIndex)));
74 for (nFirstEntry = 0; nFirstEntry < nLastEntry; nFirstEntry++)
76 pWriteAcc->SetPaletteColor(
77 sal::static_int_cast<sal_uInt16>(aPopArtTable[nFirstEntry].mnIndex),
78 pWriteAcc->GetPaletteColor(
79 sal::static_int_cast<sal_uInt16>(aPopArtTable[nFirstEntry + 1].mnIndex)));
82 pWriteAcc->SetPaletteColor(
83 sal::static_int_cast<sal_uInt16>(aPopArtTable[nLastEntry].mnIndex), aFirstCol);
85 // cleanup
86 pWriteAcc.reset();
87 bRet = true;
91 if (bRet)
92 return BitmapEx(aBitmap);
94 return BitmapEx();
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */