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 int nEntryCount = 1 << pWriteAcc->GetBitCount();
36 int 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 = static_cast<sal_uInt16>(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 }
54 }
55
56 // sort table
57 std::sort(aPopArtTable.begin(), aPopArtTable.end(),
58 [](const PopArtEntry& lhs, const PopArtEntry& rhs) {
59 return lhs.mnCount < rhs.mnCount;
60 });
61
62 // get last used entry
63 sal_uLong nFirstEntry;
64 sal_uLong nLastEntry = 0;
65
66 for (n = 0; n < nEntryCount; n++)
67 {
68 if (aPopArtTable[n].mnCount)
69 nLastEntry = n;
70 }
71
72 // rotate palette (one entry)
73 const BitmapColor aFirstCol(pWriteAcc->GetPaletteColor(
74 sal::static_int_cast<sal_uInt16>(aPopArtTable[0].mnIndex)));
75
76 for (nFirstEntry = 0; nFirstEntry < nLastEntry; nFirstEntry++)
77 {
78 pWriteAcc->SetPaletteColor(
79 sal::static_int_cast<sal_uInt16>(aPopArtTable[nFirstEntry].mnIndex),
80 pWriteAcc->GetPaletteColor(
81 sal::static_int_cast<sal_uInt16>(aPopArtTable[nFirstEntry + 1].mnIndex)));
82 }
83
84 pWriteAcc->SetPaletteColor(
85 sal::static_int_cast<sal_uInt16>(aPopArtTable[nLastEntry].mnIndex), aFirstCol);
86
87 // cleanup
88 pWriteAcc.reset();
89 bRet = true;
90 }
91 }
92
93 if (bRet)
94 return BitmapEx(aBitmap);
95
96 return BitmapEx();
97}
98
99/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
int mnIndex
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:203
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:29
sal_uIntPtr sal_uLong