bump product version to 7.2.5.1
[LibreOffice.git] / vcl / source / bitmap / BitmapPopArtFilter.cxx
blob81a3d22c76f087519ac06895e9f2e2333294f0ee
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 <bitmap/BitmapWriteAccess.hxx>
17 BitmapEx BitmapPopArtFilter::execute(BitmapEx const& rBitmapEx) const
19 Bitmap aBitmap(rBitmapEx.GetBitmap());
21 bool bRet = isPalettePixelFormat(aBitmap.getPixelFormat())
22 || aBitmap.Convert(BmpConversion::N8BitColors);
24 if (bRet)
26 bRet = false;
28 BitmapScopedWriteAccess pWriteAcc(aBitmap);
30 if (pWriteAcc)
32 const tools::Long nWidth = pWriteAcc->Width();
33 const tools::Long nHeight = pWriteAcc->Height();
34 const int nEntryCount = 1 << pWriteAcc->GetBitCount();
35 int n = 0;
36 std::vector<PopArtEntry> aPopArtTable(nEntryCount);
38 for (n = 0; n < nEntryCount; n++)
40 PopArtEntry& rEntry = aPopArtTable[n];
41 rEntry.mnIndex = static_cast<sal_uInt16>(n);
42 rEntry.mnCount = 0;
45 // get pixel count for each palette entry
46 for (tools::Long nY = 0; nY < nHeight; nY++)
48 Scanline pScanline = pWriteAcc->GetScanline(nY);
49 for (tools::Long nX = 0; nX < nWidth; nX++)
51 aPopArtTable[pWriteAcc->GetIndexFromData(pScanline, nX)].mnCount++;
55 // sort table
56 std::sort(aPopArtTable.begin(), aPopArtTable.end(),
57 [](const PopArtEntry& lhs, const PopArtEntry& rhs) {
58 return lhs.mnCount < rhs.mnCount;
59 });
61 // get last used entry
62 sal_uLong nFirstEntry;
63 sal_uLong nLastEntry = 0;
65 for (n = 0; n < nEntryCount; n++)
67 if (aPopArtTable[n].mnCount)
68 nLastEntry = n;
71 // rotate palette (one entry)
72 const BitmapColor aFirstCol(pWriteAcc->GetPaletteColor(
73 sal::static_int_cast<sal_uInt16>(aPopArtTable[0].mnIndex)));
75 for (nFirstEntry = 0; nFirstEntry < nLastEntry; nFirstEntry++)
77 pWriteAcc->SetPaletteColor(
78 sal::static_int_cast<sal_uInt16>(aPopArtTable[nFirstEntry].mnIndex),
79 pWriteAcc->GetPaletteColor(
80 sal::static_int_cast<sal_uInt16>(aPopArtTable[nFirstEntry + 1].mnIndex)));
83 pWriteAcc->SetPaletteColor(
84 sal::static_int_cast<sal_uInt16>(aPopArtTable[nLastEntry].mnIndex), aFirstCol);
86 // cleanup
87 pWriteAcc.reset();
88 bRet = true;
92 if (bRet)
93 return BitmapEx(aBitmap);
95 return BitmapEx();
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */