Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / bitmap / BitmapSepiaFilter.cxx
blob5123bf13075c190fe2dd8150e21607ec18bcb4e9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
11 #include <sal/config.h>
13 #include <algorithm>
15 #include <vcl/bitmap.hxx>
16 #include <vcl/bitmapex.hxx>
17 #include <vcl/bitmapaccess.hxx>
18 #include <vcl/BitmapSepiaFilter.hxx>
20 #include <bitmapwriteaccess.hxx>
22 BitmapEx BitmapSepiaFilter::execute(BitmapEx const& rBitmapEx) const
24 Bitmap aBitmap(rBitmapEx.GetBitmap());
25 Bitmap::ScopedReadAccess pReadAcc(aBitmap);
26 bool bRet = false;
28 if (pReadAcc)
30 const long nSepia
31 = 10000 - 100 * std::clamp(mnSepiaPercent, sal_uInt16(0), sal_uInt16(100));
32 BitmapPalette aSepiaPal(256);
34 for (sal_uInt16 i = 0; i < 256; i++)
36 BitmapColor& rCol = aSepiaPal[i];
37 const sal_uInt8 cSepiaValue = static_cast<sal_uInt8>(nSepia * i / 10000);
39 rCol.SetRed(static_cast<sal_uInt8>(i));
40 rCol.SetGreen(cSepiaValue);
41 rCol.SetBlue(cSepiaValue);
44 Bitmap aNewBmp(aBitmap.GetSizePixel(), 8, &aSepiaPal);
45 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
47 if (pWriteAcc)
49 BitmapColor aCol(sal_uInt8(0));
50 const long nWidth = pWriteAcc->Width();
51 const long nHeight = pWriteAcc->Height();
53 if (pReadAcc->HasPalette())
55 const sal_uInt16 nPalCount = pReadAcc->GetPaletteEntryCount();
56 std::unique_ptr<sal_uInt8[]> pIndexMap(new sal_uInt8[nPalCount]);
57 for (sal_uInt16 i = 0; i < nPalCount; i++)
59 pIndexMap[i] = pReadAcc->GetPaletteColor(i).GetLuminance();
62 for (long nY = 0; nY < nHeight; nY++)
64 Scanline pScanline = pWriteAcc->GetScanline(nY);
65 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
66 for (long nX = 0; nX < nWidth; nX++)
68 aCol.SetIndex(pIndexMap[pReadAcc->GetIndexFromData(pScanlineRead, nX)]);
69 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
73 else
75 for (long nY = 0; nY < nHeight; nY++)
77 Scanline pScanline = pWriteAcc->GetScanline(nY);
78 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
79 for (long nX = 0; nX < nWidth; nX++)
81 aCol.SetIndex(pReadAcc->GetPixelFromData(pScanlineRead, nX).GetLuminance());
82 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
87 pWriteAcc.reset();
88 bRet = true;
91 pReadAcc.reset();
93 if (bRet)
95 const MapMode aMap(aBitmap.GetPrefMapMode());
96 const Size aPrefSize(aBitmap.GetPrefSize());
98 aBitmap = aNewBmp;
100 aBitmap.SetPrefMapMode(aMap);
101 aBitmap.SetPrefSize(aPrefSize);
105 if (bRet)
106 return rBitmapEx;
108 return BitmapEx();
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */