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 <sal/config.h>
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
);
30 const tools::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
);
49 BitmapColor
aCol(sal_uInt8(0));
50 const tools::Long nWidth
= pWriteAcc
->Width();
51 const tools::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 (tools::Long nY
= 0; nY
< nHeight
; nY
++)
64 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
65 Scanline pScanlineRead
= pReadAcc
->GetScanline(nY
);
66 for (tools::Long nX
= 0; nX
< nWidth
; nX
++)
68 aCol
.SetIndex(pIndexMap
[pReadAcc
->GetIndexFromData(pScanlineRead
, nX
)]);
69 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aCol
);
75 for (tools::Long nY
= 0; nY
< nHeight
; nY
++)
77 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
78 Scanline pScanlineRead
= pReadAcc
->GetScanline(nY
);
79 for (tools::Long nX
= 0; nX
< nWidth
; nX
++)
81 aCol
.SetIndex(pReadAcc
->GetPixelFromData(pScanlineRead
, nX
).GetLuminance());
82 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aCol
);
95 const MapMode
aMap(aBitmap
.GetPrefMapMode());
96 const Size
aPrefSize(aBitmap
.GetPrefSize());
100 aBitmap
.SetPrefMapMode(aMap
);
101 aBitmap
.SetPrefSize(aPrefSize
);
106 return BitmapEx(aBitmap
);
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */