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 <tools/solar.h>
12#include <vcl/bitmap.hxx>
13#include <vcl/bitmapex.hxx>
15
17
19{
20 Bitmap aBitmap(rBitmapEx.GetBitmap());
21
22 bool bRet = isPalettePixelFormat(aBitmap.getPixelFormat())
24
25 if (bRet)
26 {
27 bRet = false;
28
29 BitmapScopedWriteAccess pWriteAcc(aBitmap);
30
31 if (pWriteAcc)
32 {
33 const sal_Int32 nWidth = pWriteAcc->Width();
34 const sal_Int32 nHeight = pWriteAcc->Height();
35 const sal_uInt16 nEntryCount = 1 << pWriteAcc->GetBitCount();
36 sal_uInt16 n = 0;
37 std::vector<PopArtEntry> aPopArtTable(nEntryCount);
38
39 for (n = 0; n < nEntryCount; n++)
40 {
41 PopArtEntry& rEntry = aPopArtTable[n];
42 rEntry.mnIndex = n;
43 rEntry.mnCount = 0;
44 }
45
46 // get pixel count for each palette entry
47 for (sal_Int32 nY = 0; nY < nHeight; nY++)
48 {
49 Scanline pScanline = pWriteAcc->GetScanline(nY);
50 for (sal_Int32 nX = 0; nX < nWidth; nX++)
51 {
52 aPopArtTable[pWriteAcc->GetIndexFromData(pScanline, nX)].mnCount++;
53 assert(aPopArtTable[pWriteAcc->GetIndexFromData(pScanline, nX)].mnCount != 0);
54 }
55 }
56
57 // sort table
58 std::sort(aPopArtTable.begin(), aPopArtTable.end(),
59 [](const PopArtEntry& lhs, const PopArtEntry& rhs) {
60 return lhs.mnCount > rhs.mnCount;
61 });
62
63 // get last used entry
64 sal_uInt16 nFirstEntry;
65 sal_uInt16 nLastEntry = 0;
66
67 for (n = 0; n < nEntryCount; n++)
68 {
69 if (aPopArtTable[n].mnCount)
70 nLastEntry = n;
71 }
72
73 // rotate palette (one entry)
74 const BitmapColor aFirstCol(pWriteAcc->GetPaletteColor(aPopArtTable[0].mnIndex));
75
76 for (nFirstEntry = 0; nFirstEntry < nLastEntry; nFirstEntry++)
77 {
78 pWriteAcc->SetPaletteColor(
79 aPopArtTable[nFirstEntry].mnIndex,
80 pWriteAcc->GetPaletteColor(aPopArtTable[nFirstEntry + 1].mnIndex));
81 }
82
83 pWriteAcc->SetPaletteColor(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: */
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:217
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
bool Convert(BmpConversion eConversion)
Convert bitmap format.
vcl::PixelFormat getPixelFormat() const
This template handles BitmapAccess the RAII way.
std::size_t mnCount
sal_Int64 n
constexpr bool isPalettePixelFormat(PixelFormat ePixelFormat)
Is it a pixel format that forces creation of a palette.
Definition: BitmapTypes.hxx:28
sal_uInt32 mnIndex