LibreOffice Module vcl (master) 1
BitmapSimpleColorQuantizationFilter.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
16#include <bitmap/Octree.hxx>
17
19{
20 Bitmap aBitmap = aBitmapEx.GetBitmap();
21
22 if (vcl::numberOfColors(aBitmap.getPixelFormat()) > sal_Int64(mnNewColorCount))
23 return BitmapEx(aBitmap);
24
25 Bitmap aNewBmp;
26 Bitmap::ScopedReadAccess pRAcc(aBitmap);
27 if (!pRAcc)
28 return BitmapEx();
29
30 const sal_uInt16 nColorCount = std::min(mnNewColorCount, sal_uInt16(256));
31 auto ePixelFormat = vcl::PixelFormat::N8_BPP;
32
33 Octree aOct(*pRAcc, nColorCount);
34 const BitmapPalette& rPal = aOct.GetPalette();
35
36 aNewBmp = Bitmap(aBitmap.GetSizePixel(), ePixelFormat, &rPal);
37 BitmapScopedWriteAccess pWAcc(aNewBmp);
38 if (!pWAcc)
39 return BitmapEx();
40
41 const sal_Int32 nWidth = pRAcc->Width();
42 const sal_Int32 nHeight = pRAcc->Height();
43
44 if (pRAcc->HasPalette())
45 {
46 for (sal_Int32 nY = 0; nY < nHeight; nY++)
47 {
48 Scanline pScanline = pWAcc->GetScanline(nY);
49 Scanline pScanlineRead = pRAcc->GetScanline(nY);
50 for (sal_Int32 nX = 0; nX < nWidth; nX++)
51 {
52 auto c = pRAcc->GetPaletteColor(pRAcc->GetIndexFromData(pScanlineRead, nX));
53 pWAcc->SetPixelOnData(
54 pScanline, nX,
55 BitmapColor(static_cast<sal_uInt8>(aOct.GetBestPaletteIndex(c))));
56 }
57 }
58 }
59 else
60 {
61 for (sal_Int32 nY = 0; nY < nHeight; nY++)
62 {
63 Scanline pScanline = pWAcc->GetScanline(nY);
64 Scanline pScanlineRead = pRAcc->GetScanline(nY);
65 for (sal_Int32 nX = 0; nX < nWidth; nX++)
66 {
67 auto c = pRAcc->GetPixelFromData(pScanlineRead, nX);
68 pWAcc->SetPixelOnData(
69 pScanline, nX,
70 BitmapColor(static_cast<sal_uInt8>(aOct.GetBestPaletteIndex(c))));
71 }
72 }
73 }
74
75 pWAcc.reset();
76 pRAcc.reset();
77
78 const MapMode aMap(aBitmap.GetPrefMapMode());
79 const Size aSize(aBitmap.GetPrefSize());
80
81 aBitmap = aNewBmp;
82
83 aBitmap.SetPrefMapMode(aMap);
84 aBitmap.SetPrefSize(aSize);
85
86 return BitmapEx(aBitmap);
87}
88
89/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:217
tools::Long Height() const
tools::Long Width() const
bool HasPalette() const
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
BitmapColor GetPixelFromData(const sal_uInt8 *pData, tools::Long nX) const
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, tools::Long nX) const
Scanline GetScanline(tools::Long nY) const
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
void SetPrefMapMode(const MapMode &rMapMode)
const MapMode & GetPrefMapMode() const
Size GetSizePixel() const
void SetPrefSize(const Size &rSize)
const Size & GetPrefSize() const
vcl::PixelFormat getPixelFormat() const
sal_uInt16 GetBestPaletteIndex(const BitmapColor &rColor)
Definition: Octree.cxx:205
const BitmapPalette & GetPalette()
Definition: Octree.cxx:197
constexpr sal_Int64 numberOfColors(PixelFormat ePixelFormat)
Definition: BitmapTypes.hxx:39
HashMap_OWString_Interface aMap
unsigned char sal_uInt8