LibreOffice Module vcl (master)  1
BitmapPopArtFilter.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  */
10 
11 #include <vcl/bitmap.hxx>
12 #include <vcl/bitmapex.hxx>
14 
15 #include <bitmapwriteaccess.hxx>
16 
18 {
19  Bitmap aBitmap(rBitmapEx.GetBitmap());
20 
21  bool bRet = (aBitmap.GetBitCount() <= 8) || aBitmap.Convert(BmpConversion::N8BitColors);
22 
23  if (bRet)
24  {
25  bRet = false;
26 
27  BitmapScopedWriteAccess pWriteAcc(aBitmap);
28 
29  if (pWriteAcc)
30  {
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);
36 
37  for (n = 0; n < nEntryCount; n++)
38  {
39  PopArtEntry& rEntry = aPopArtTable[n];
40  rEntry.mnIndex = static_cast<sal_uInt16>(n);
41  rEntry.mnCount = 0;
42  }
43 
44  // get pixel count for each palette entry
45  for (long nY = 0; nY < nHeight; nY++)
46  {
47  Scanline pScanline = pWriteAcc->GetScanline(nY);
48  for (long nX = 0; nX < nWidth; nX++)
49  {
50  aPopArtTable[pWriteAcc->GetIndexFromData(pScanline, nX)].mnCount++;
51  }
52  }
53 
54  // sort table
55  std::sort(aPopArtTable.begin(), aPopArtTable.end(),
56  [](const PopArtEntry& lhs, const PopArtEntry& rhs) {
57  return lhs.mnCount < rhs.mnCount;
58  });
59 
60  // get last used entry
61  sal_uLong nFirstEntry;
62  sal_uLong nLastEntry = 0;
63 
64  for (n = 0; n < nEntryCount; n++)
65  {
66  if (aPopArtTable[n].mnCount)
67  nLastEntry = n;
68  }
69 
70  // rotate palette (one entry)
71  const BitmapColor aFirstCol(pWriteAcc->GetPaletteColor(
72  sal::static_int_cast<sal_uInt16>(aPopArtTable[0].mnIndex)));
73 
74  for (nFirstEntry = 0; nFirstEntry < nLastEntry; nFirstEntry++)
75  {
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)));
80  }
81 
82  pWriteAcc->SetPaletteColor(
83  sal::static_int_cast<sal_uInt16>(aPopArtTable[nLastEntry].mnIndex), aFirstCol);
84 
85  // cleanup
86  pWriteAcc.reset();
87  bRet = true;
88  }
89  }
90 
91  if (bRet)
92  return BitmapEx(aBitmap);
93 
94  return BitmapEx();
95 }
96 
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Scanline GetScanline(long nY) const
sal_uIntPtr sal_uLong
sal_uInt16 GetBitCount() const
sal_uInt32 mnIndex
long Width() const
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
std::size_t mnCount
long Height() const
bool Convert(BmpConversion eConversion)
Convert bitmap format.
Definition: bitmap3.cxx:226
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
void SetPaletteColor(sal_uInt16 nColor, const BitmapColor &rBitmapColor)
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, long nX) const