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 <bitmapwriteaccess.hxx>
17 BitmapEx
BitmapPopArtFilter::execute(BitmapEx
const& rBitmapEx
) const
19 Bitmap
aBitmap(rBitmapEx
.GetBitmap());
21 bool bRet
= (aBitmap
.GetBitCount() <= 8) || aBitmap
.Convert(BmpConversion::N8BitColors
);
27 BitmapScopedWriteAccess
pWriteAcc(aBitmap
);
31 const long nWidth
= pWriteAcc
->Width();
32 const long nHeight
= pWriteAcc
->Height();
33 const int nEntryCount
= 1 << pWriteAcc
->GetBitCount();
35 std::vector
<PopArtEntry
> aPopArtTable(nEntryCount
);
37 for (n
= 0; n
< nEntryCount
; n
++)
39 PopArtEntry
& rEntry
= aPopArtTable
[n
];
40 rEntry
.mnIndex
= static_cast<sal_uInt16
>(n
);
44 // get pixel count for each palette entry
45 for (long nY
= 0; nY
< nHeight
; nY
++)
47 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
48 for (long nX
= 0; nX
< nWidth
; nX
++)
50 aPopArtTable
[pWriteAcc
->GetIndexFromData(pScanline
, nX
)].mnCount
++;
55 std::sort(aPopArtTable
.begin(), aPopArtTable
.end(),
56 [](const PopArtEntry
& lhs
, const PopArtEntry
& rhs
) {
57 return lhs
.mnCount
< rhs
.mnCount
;
60 // get last used entry
61 sal_uLong nFirstEntry
;
62 sal_uLong nLastEntry
= 0;
64 for (n
= 0; n
< nEntryCount
; n
++)
66 if (aPopArtTable
[n
].mnCount
)
70 // rotate palette (one entry)
71 const BitmapColor
aFirstCol(pWriteAcc
->GetPaletteColor(
72 sal::static_int_cast
<sal_uInt16
>(aPopArtTable
[0].mnIndex
)));
74 for (nFirstEntry
= 0; nFirstEntry
< nLastEntry
; nFirstEntry
++)
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
)));
82 pWriteAcc
->SetPaletteColor(
83 sal::static_int_cast
<sal_uInt16
>(aPopArtTable
[nLastEntry
].mnIndex
), aFirstCol
);
92 return BitmapEx(aBitmap
);
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */