1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
);
28 BitmapScopedWriteAccess
pWriteAcc(aBitmap
);
32 const tools::Long nWidth
= pWriteAcc
->Width();
33 const tools::Long nHeight
= pWriteAcc
->Height();
34 const int nEntryCount
= 1 << pWriteAcc
->GetBitCount();
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
);
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
++;
56 std::sort(aPopArtTable
.begin(), aPopArtTable
.end(),
57 [](const PopArtEntry
& lhs
, const PopArtEntry
& rhs
) {
58 return lhs
.mnCount
< rhs
.mnCount
;
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
)
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
);
93 return BitmapEx(aBitmap
);
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */