LibreOffice Module vcl (master) 1
BitmapSepiaFilter.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 <sal/config.h>
12
13#include <vcl/bitmap.hxx>
14#include <vcl/bitmapex.hxx>
16
18
19#include <algorithm>
20
22{
23 Bitmap aBitmap(rBitmapEx.GetBitmap());
24 Bitmap::ScopedReadAccess pReadAcc(aBitmap);
25 if (!pReadAcc)
26 return BitmapEx();
27
28 const sal_Int32 nSepia
29 = 10000 - 100 * std::clamp(mnSepiaPercent, sal_uInt16(0), sal_uInt16(100));
30 BitmapPalette aSepiaPal(256);
31
32 for (sal_uInt16 i = 0; i < 256; i++)
33 {
34 BitmapColor& rCol = aSepiaPal[i];
35 const sal_uInt8 cSepiaValue = static_cast<sal_uInt8>(nSepia * i / 10000);
36
37 rCol.SetRed(static_cast<sal_uInt8>(i));
38 rCol.SetGreen(cSepiaValue);
39 rCol.SetBlue(cSepiaValue);
40 }
41
42 Bitmap aNewBmp(aBitmap.GetSizePixel(), vcl::PixelFormat::N8_BPP, &aSepiaPal);
43 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
44 if (!pWriteAcc)
45 return BitmapEx();
46
47 BitmapColor aCol(sal_uInt8(0));
48 const sal_Int32 nWidth = pWriteAcc->Width();
49 const sal_Int32 nHeight = pWriteAcc->Height();
50
51 if (pReadAcc->HasPalette())
52 {
53 const sal_uInt16 nPalCount = pReadAcc->GetPaletteEntryCount();
54 std::unique_ptr<sal_uInt8[]> pIndexMap(new sal_uInt8[nPalCount]);
55 for (sal_uInt16 i = 0; i < nPalCount; i++)
56 {
57 pIndexMap[i] = pReadAcc->GetPaletteColor(i).GetLuminance();
58 }
59
60 for (sal_Int32 nY = 0; nY < nHeight; nY++)
61 {
62 Scanline pScanline = pWriteAcc->GetScanline(nY);
63 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
64 for (sal_Int32 nX = 0; nX < nWidth; nX++)
65 {
66 aCol.SetIndex(pIndexMap[pReadAcc->GetIndexFromData(pScanlineRead, nX)]);
67 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
68 }
69 }
70 }
71 else
72 {
73 for (sal_Int32 nY = 0; nY < nHeight; nY++)
74 {
75 Scanline pScanline = pWriteAcc->GetScanline(nY);
76 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
77 for (sal_Int32 nX = 0; nX < nWidth; nX++)
78 {
79 aCol.SetIndex(pReadAcc->GetPixelFromData(pScanlineRead, nX).GetLuminance());
80 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
81 }
82 }
83 }
84
85 pWriteAcc.reset();
86 pReadAcc.reset();
87
88 const MapMode aMap(aBitmap.GetPrefMapMode());
89 const Size aPrefSize(aBitmap.GetPrefSize());
90
91 aBitmap = aNewBmp;
92
93 aBitmap.SetPrefMapMode(aMap);
94 aBitmap.SetPrefSize(aPrefSize);
95
96 return BitmapEx(aBitmap);
97}
98
99/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
void SetIndex(sal_uInt8 cIndex)
Definition: BitmapColor.hxx:75
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:217
bool HasPalette() const
sal_uInt16 GetPaletteEntryCount() 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
sal_uInt8 GetLuminance() const
void SetGreen(sal_uInt8 nGreen)
void SetRed(sal_uInt8 nRed)
void SetBlue(sal_uInt8 nBlue)
int i
HashMap_OWString_Interface aMap
unsigned char sal_uInt8