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 bool bRet = false;
26
27 if (pReadAcc)
28 {
29 const sal_Int32 nSepia
30 = 10000 - 100 * std::clamp(mnSepiaPercent, sal_uInt16(0), sal_uInt16(100));
31 BitmapPalette aSepiaPal(256);
32
33 for (sal_uInt16 i = 0; i < 256; i++)
34 {
35 BitmapColor& rCol = aSepiaPal[i];
36 const sal_uInt8 cSepiaValue = static_cast<sal_uInt8>(nSepia * i / 10000);
37
38 rCol.SetRed(static_cast<sal_uInt8>(i));
39 rCol.SetGreen(cSepiaValue);
40 rCol.SetBlue(cSepiaValue);
41 }
42
43 Bitmap aNewBmp(aBitmap.GetSizePixel(), vcl::PixelFormat::N8_BPP, &aSepiaPal);
44 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
45
46 if (pWriteAcc)
47 {
48 BitmapColor aCol(sal_uInt8(0));
49 const sal_Int32 nWidth = pWriteAcc->Width();
50 const sal_Int32 nHeight = pWriteAcc->Height();
51
52 if (pReadAcc->HasPalette())
53 {
54 const sal_uInt16 nPalCount = pReadAcc->GetPaletteEntryCount();
55 std::unique_ptr<sal_uInt8[]> pIndexMap(new sal_uInt8[nPalCount]);
56 for (sal_uInt16 i = 0; i < nPalCount; i++)
57 {
58 pIndexMap[i] = pReadAcc->GetPaletteColor(i).GetLuminance();
59 }
60
61 for (sal_Int32 nY = 0; nY < nHeight; nY++)
62 {
63 Scanline pScanline = pWriteAcc->GetScanline(nY);
64 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
65 for (sal_Int32 nX = 0; nX < nWidth; nX++)
66 {
67 aCol.SetIndex(pIndexMap[pReadAcc->GetIndexFromData(pScanlineRead, nX)]);
68 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
69 }
70 }
71 }
72 else
73 {
74 for (sal_Int32 nY = 0; nY < nHeight; nY++)
75 {
76 Scanline pScanline = pWriteAcc->GetScanline(nY);
77 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
78 for (sal_Int32 nX = 0; nX < nWidth; nX++)
79 {
80 aCol.SetIndex(pReadAcc->GetPixelFromData(pScanlineRead, nX).GetLuminance());
81 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
82 }
83 }
84 }
85
86 pWriteAcc.reset();
87 bRet = true;
88 }
89
90 pReadAcc.reset();
91
92 if (bRet)
93 {
94 const MapMode aMap(aBitmap.GetPrefMapMode());
95 const Size aPrefSize(aBitmap.GetPrefSize());
96
97 aBitmap = aNewBmp;
98
99 aBitmap.SetPrefMapMode(aMap);
100 aBitmap.SetPrefSize(aPrefSize);
101 }
102 }
103
104 if (bRet)
105 return BitmapEx(aBitmap);
106
107 return BitmapEx();
108}
109
110/* 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:203
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