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 <vcl/bitmapaccess.hxx>
12 #include <basegfx/color/bcolortools.hxx>
14 #include "BitmapProcessor.hxx"
16 BitmapEx
BitmapProcessor::createLightImage(const BitmapEx
& rBitmapEx
)
18 const Size
aSize(rBitmapEx
.GetSizePixel());
20 Bitmap
aBitmap(rBitmapEx
.GetBitmap());
21 Bitmap
aDarkBitmap(aSize
, 24);
23 Bitmap::ScopedReadAccess
pRead(aBitmap
);
24 Bitmap::ScopedWriteAccess
pWrite(aDarkBitmap
);
28 for (long nY
= 0; nY
< aSize
.Height(); ++nY
)
30 for (long nX
= 0; nX
< aSize
.Width(); ++nX
)
32 BitmapColor aColor
= pRead
->HasPalette() ?
33 pRead
->GetPaletteColor(pRead
->GetPixelIndex(nY
, nX
)) :
34 pRead
->GetPixel(nY
, nX
);
35 basegfx::BColor
aBColor(Color(aColor
.Invert()).getBColor());
36 aBColor
= basegfx::tools::rgb2hsl(aBColor
);
38 double fHue
= aBColor
.getRed();
44 aBColor
= basegfx::tools::hsl2rgb(aBColor
);
45 aColor
.SetRed(((aBColor
.getRed() * 255.0) + 0.5));
46 aColor
.SetGreen(((aBColor
.getGreen() * 255.0) + 0.5));
47 aColor
.SetBlue(((aBColor
.getBlue() * 255.0) + 0.5));
49 pWrite
->SetPixel(nY
, nX
, aColor
);
56 return BitmapEx(aDarkBitmap
, rBitmapEx
.GetAlpha());
59 BitmapEx
BitmapProcessor::createDisabledImage(const BitmapEx
& rBitmapEx
)
61 const Size
aSize(rBitmapEx
.GetSizePixel());
63 //keep disable image at same depth as original where possible, otherwise
65 sal_uInt16 nBitCount
= rBitmapEx
.GetBitCount();
68 const BitmapPalette
* pPal
= nBitCount
== 8 ? &Bitmap::GetGreyPalette(256) : nullptr;
69 Bitmap
aGrey(aSize
, nBitCount
, pPal
);
71 AlphaMask
aGreyAlpha(aSize
);
73 Bitmap
aBitmap(rBitmapEx
.GetBitmap());
74 Bitmap::ScopedReadAccess
pRead(aBitmap
);
76 Bitmap::ScopedWriteAccess
pGrey(aGrey
);
77 AlphaMask::ScopedWriteAccess
pGreyAlpha(aGreyAlpha
);
79 BitmapEx aReturnBitmap
;
81 if (rBitmapEx
.IsTransparent())
83 AlphaMask
aBitmapAlpha(rBitmapEx
.GetAlpha());
84 AlphaMask::ScopedReadAccess
pReadAlpha(aBitmapAlpha
);
86 if (pRead
&& pReadAlpha
&& pGrey
&& pGreyAlpha
)
88 BitmapColor
aGreyAlphaValue(0);
90 for (long nY
= 0; nY
< aSize
.Height(); ++nY
)
92 for (long nX
= 0; nX
< aSize
.Width(); ++nX
)
94 const sal_uInt8
nLum(pRead
->GetLuminance(nY
, nX
));
95 BitmapColor
aGreyValue(nLum
, nLum
, nLum
);
96 pGrey
->SetPixel(nY
, nX
, aGreyValue
);
98 const BitmapColor
aBitmapAlphaValue(pReadAlpha
->GetPixel(nY
, nX
));
100 aGreyAlphaValue
.SetIndex(sal_uInt8(std::min(aBitmapAlphaValue
.GetIndex() + 178ul, 255ul)));
101 pGreyAlpha
->SetPixel(nY
, nX
, aGreyAlphaValue
);
106 aReturnBitmap
= BitmapEx(aGrey
, aGreyAlpha
);
110 if (pRead
&& pGrey
&& pGreyAlpha
)
112 BitmapColor
aGreyAlphaValue(0);
114 for (long nY
= 0; nY
< aSize
.Height(); ++nY
)
116 for (long nX
= 0; nX
< aSize
.Width(); ++nX
)
118 const sal_uInt8
nLum(pRead
->GetLuminance(nY
, nX
));
119 BitmapColor
aGreyValue(nLum
, nLum
, nLum
);
120 pGrey
->SetPixel(nY
, nX
, aGreyValue
);
122 aGreyAlphaValue
.SetIndex(128);
123 pGreyAlpha
->SetPixel(nY
, nX
, aGreyAlphaValue
);
127 aReturnBitmap
= BitmapEx(aGrey
);
134 return aReturnBitmap
;
137 void BitmapProcessor::colorizeImage(BitmapEx
& rBitmapEx
, Color aColor
)
139 Bitmap aBitmap
= rBitmapEx
.GetBitmap();
140 Bitmap::ScopedWriteAccess
pWriteAccess(aBitmap
);
144 BitmapColor aBitmapColor
;
145 const long nW
= pWriteAccess
->Width();
146 const long nH
= pWriteAccess
->Height();
147 std::vector
<sal_uInt8
> aMapR(256);
148 std::vector
<sal_uInt8
> aMapG(256);
149 std::vector
<sal_uInt8
> aMapB(256);
153 const sal_uInt8 cR
= aColor
.GetRed();
154 const sal_uInt8 cG
= aColor
.GetGreen();
155 const sal_uInt8 cB
= aColor
.GetBlue();
157 for (nX
= 0; nX
< 256; ++nX
)
159 aMapR
[nX
] = MinMax((nX
+ cR
) / 2, 0, 255);
160 aMapG
[nX
] = MinMax((nX
+ cG
) / 2, 0, 255);
161 aMapB
[nX
] = MinMax((nX
+ cB
) / 2, 0, 255);
164 if (pWriteAccess
->HasPalette())
166 for (sal_uInt16 i
= 0, nCount
= pWriteAccess
->GetPaletteEntryCount(); i
< nCount
; i
++)
168 const BitmapColor
& rCol
= pWriteAccess
->GetPaletteColor(i
);
169 aBitmapColor
.SetRed(aMapR
[rCol
.GetRed()]);
170 aBitmapColor
.SetGreen(aMapG
[rCol
.GetGreen()]);
171 aBitmapColor
.SetBlue(aMapB
[rCol
.GetBlue()]);
172 pWriteAccess
->SetPaletteColor(i
, aBitmapColor
);
175 else if (pWriteAccess
->GetScanlineFormat() == ScanlineFormat::N24BitTcBgr
)
177 for (nY
= 0; nY
< nH
; ++nY
)
179 Scanline pScan
= pWriteAccess
->GetScanline(nY
);
181 for (nX
= 0; nX
< nW
; ++nX
)
183 *pScan
= aMapB
[*pScan
]; pScan
++;
184 *pScan
= aMapG
[*pScan
]; pScan
++;
185 *pScan
= aMapR
[*pScan
]; pScan
++;
191 for (nY
= 0; nY
< nH
; ++nY
)
193 for (nX
= 0; nX
< nW
; ++nX
)
195 aBitmapColor
= pWriteAccess
->GetPixel(nY
, nX
);
196 aBitmapColor
.SetRed(aMapR
[aBitmapColor
.GetRed()]);
197 aBitmapColor
.SetGreen(aMapG
[aBitmapColor
.GetGreen()]);
198 aBitmapColor
.SetBlue(aMapB
[aBitmapColor
.GetBlue()]);
199 pWriteAccess
->SetPixel(nY
, nX
, aBitmapColor
);
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */