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  int nRet;
58 
59  if (lhs.mnCount < rhs.mnCount)
60  {
61  nRet = 1;
62  }
63  else if (lhs.mnCount == rhs.mnCount)
64  {
65  nRet = 0;
66  }
67  else
68  {
69  nRet = -1;
70  }
71 
72  return nRet;
73  });
74 
75  // get last used entry
76  sal_uLong nFirstEntry;
77  sal_uLong nLastEntry = 0;
78 
79  for (n = 0; n < nEntryCount; n++)
80  {
81  if (aPopArtTable[n].mnCount)
82  nLastEntry = n;
83  }
84 
85  // rotate palette (one entry)
86  const BitmapColor aFirstCol(pWriteAcc->GetPaletteColor(
87  sal::static_int_cast<sal_uInt16>(aPopArtTable[0].mnIndex)));
88 
89  for (nFirstEntry = 0; nFirstEntry < nLastEntry; nFirstEntry++)
90  {
91  pWriteAcc->SetPaletteColor(
92  sal::static_int_cast<sal_uInt16>(aPopArtTable[nFirstEntry].mnIndex),
93  pWriteAcc->GetPaletteColor(
94  sal::static_int_cast<sal_uInt16>(aPopArtTable[nFirstEntry + 1].mnIndex)));
95  }
96 
97  pWriteAcc->SetPaletteColor(
98  sal::static_int_cast<sal_uInt16>(aPopArtTable[nLastEntry].mnIndex), aFirstCol);
99 
100  // cleanup
101  pWriteAcc.reset();
102  bRet = true;
103  }
104  }
105 
106  if (bRet)
107  return BitmapEx(aBitmap);
108 
109  return BitmapEx();
110 }
111 
112 /* 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