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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 #include <tools/debug.hxx>
24 #include <vcl/BitmapWriteAccess.hxx>
25 #include <bitmap/bmpfast.hxx>
27 BitmapWriteAccess::BitmapWriteAccess(Bitmap
& rBitmap
)
28 : BitmapReadAccess(rBitmap
, BitmapAccessMode::Write
)
32 BitmapWriteAccess::BitmapWriteAccess(AlphaMask
& rBitmap
)
33 : BitmapReadAccess(rBitmap
, BitmapAccessMode::Write
)
37 BitmapWriteAccess::~BitmapWriteAccess() {}
39 void BitmapWriteAccess::CopyScanline(tools::Long nY
, const BitmapReadAccess
& rReadAcc
)
41 assert(nY
>= 0 && nY
< mpBuffer
->mnHeight
&& "y-coordinate in destination out of range!");
42 SAL_WARN_IF(nY
>= rReadAcc
.Height(), "vcl", "y-coordinate in source out of range!");
43 SAL_WARN_IF((!HasPalette() || !rReadAcc
.HasPalette())
44 && (HasPalette() || rReadAcc
.HasPalette()),
45 "vcl", "No copying possible between palette bitmap and TC bitmap!");
47 if ((GetScanlineFormat() == rReadAcc
.GetScanlineFormat())
48 && (GetScanlineSize() >= rReadAcc
.GetScanlineSize()))
50 memcpy(GetScanline(nY
), rReadAcc
.GetScanline(nY
), rReadAcc
.GetScanlineSize());
54 tools::Long nWidth
= std::min(mpBuffer
->mnWidth
, rReadAcc
.Width());
55 if (!ImplFastCopyScanline(nY
, *ImplGetBitmapBuffer(), *rReadAcc
.ImplGetBitmapBuffer()))
57 Scanline pScanline
= GetScanline(nY
);
58 Scanline pScanlineRead
= rReadAcc
.GetScanline(nY
);
59 for (tools::Long nX
= 0; nX
< nWidth
; nX
++)
60 SetPixelOnData(pScanline
, nX
, rReadAcc
.GetPixelFromData(pScanlineRead
, nX
));
65 void BitmapWriteAccess::CopyScanline(tools::Long nY
, ConstScanline aSrcScanline
,
66 ScanlineFormat nSrcScanlineFormat
, sal_uInt32 nSrcScanlineSize
)
68 const ScanlineFormat eFormat
= nSrcScanlineFormat
;
70 assert(nY
>= 0 && nY
< mpBuffer
->mnHeight
&& "y-coordinate in destination out of range!");
71 DBG_ASSERT((HasPalette() && eFormat
<= ScanlineFormat::N8BitPal
)
72 || (!HasPalette() && eFormat
> ScanlineFormat::N8BitPal
),
73 "No copying possible between palette and non palette scanlines!");
75 const sal_uInt32 nCount
= std::min(GetScanlineSize(), nSrcScanlineSize
);
80 if (GetScanlineFormat() == eFormat
)
81 memcpy(GetScanline(nY
), aSrcScanline
, nCount
);
84 if (ImplFastCopyScanline(nY
, *ImplGetBitmapBuffer(), aSrcScanline
, nSrcScanlineFormat
,
88 DBG_ASSERT(eFormat
!= ScanlineFormat::N32BitTcMask
,
89 "No support for pixel formats with color masks yet!");
90 FncGetPixel pFncGetPixel
;
93 case ScanlineFormat::N1BitMsbPal
:
94 pFncGetPixel
= GetPixelForN1BitMsbPal
;
96 case ScanlineFormat::N8BitPal
:
97 pFncGetPixel
= GetPixelForN8BitPal
;
99 case ScanlineFormat::N24BitTcBgr
:
100 pFncGetPixel
= GetPixelForN24BitTcBgr
;
102 case ScanlineFormat::N24BitTcRgb
:
103 pFncGetPixel
= GetPixelForN24BitTcRgb
;
105 case ScanlineFormat::N32BitTcAbgr
:
106 if (Bitmap32IsPreMultipled())
107 pFncGetPixel
= GetPixelForN32BitTcAbgr
;
109 pFncGetPixel
= GetPixelForN32BitTcXbgr
;
111 case ScanlineFormat::N32BitTcArgb
:
112 if (Bitmap32IsPreMultipled())
113 pFncGetPixel
= GetPixelForN32BitTcArgb
;
115 pFncGetPixel
= GetPixelForN32BitTcXrgb
;
117 case ScanlineFormat::N32BitTcBgra
:
118 if (Bitmap32IsPreMultipled())
119 pFncGetPixel
= GetPixelForN32BitTcBgra
;
121 pFncGetPixel
= GetPixelForN32BitTcBgrx
;
123 case ScanlineFormat::N32BitTcRgba
:
124 if (Bitmap32IsPreMultipled())
125 pFncGetPixel
= GetPixelForN32BitTcRgba
;
127 pFncGetPixel
= GetPixelForN32BitTcRgbx
;
129 case ScanlineFormat::N32BitTcMask
:
130 pFncGetPixel
= GetPixelForN32BitTcMask
;
135 pFncGetPixel
= nullptr;
141 const ColorMask aDummyMask
;
142 Scanline pScanline
= GetScanline(nY
);
143 for (tools::Long nX
= 0, nWidth
= mpBuffer
->mnWidth
; nX
< nWidth
; ++nX
)
144 SetPixelOnData(pScanline
, nX
, pFncGetPixel(aSrcScanline
, nX
, aDummyMask
));
149 void BitmapWriteAccess::SetLineColor(const Color
& rColor
)
151 if (rColor
.GetAlpha() == 0)
159 mpLineColor
= BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
)));
163 mpLineColor
= BitmapColor(rColor
);
168 void BitmapWriteAccess::SetFillColor() { mpFillColor
.reset(); }
170 void BitmapWriteAccess::SetFillColor(const Color
& rColor
)
172 if (rColor
.GetAlpha() == 0)
180 mpFillColor
= BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
)));
184 mpFillColor
= BitmapColor(rColor
);
189 void BitmapWriteAccess::Erase(const Color
& rColor
)
191 // convert the color format from RGB to palette index if needed
192 // TODO: provide and use Erase( BitmapColor& method)
193 BitmapColor aColor
= rColor
;
196 aColor
= BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
)));
199 // try fast bitmap method first
200 if (ImplFastEraseBitmap(*mpBuffer
, aColor
))
203 tools::Rectangle
aRect(Point(), maBitmap
.GetSizePixel());
206 // clear the bitmap by filling the first line pixel by pixel,
207 // then dup the first line over each other line
208 Scanline pFirstScanline
= GetScanline(0);
209 const tools::Long nEndX
= aRect
.Right();
210 for (tools::Long nX
= 0; nX
<= nEndX
; ++nX
)
211 SetPixelOnData(pFirstScanline
, nX
, rColor
);
212 const auto nScanlineSize
= GetScanlineSize();
213 const tools::Long nEndY
= aRect
.Bottom();
214 for (tools::Long nY
= 1; nY
<= nEndY
; nY
++)
216 Scanline pDestScanline
= GetScanline(nY
);
217 memcpy(pDestScanline
, pFirstScanline
, nScanlineSize
);
221 void BitmapWriteAccess::DrawLine(const Point
& rStart
, const Point
& rEnd
)
226 const BitmapColor
& rLineColor
= *mpLineColor
;
229 if (rStart
.X() == rEnd
.X())
232 const tools::Long nEndY
= rEnd
.Y();
239 for (; nY
<= nEndY
; nY
++)
240 SetPixel(nY
, nX
, rLineColor
);
244 for (; nY
>= nEndY
; nY
--)
245 SetPixel(nY
, nX
, rLineColor
);
248 else if (rStart
.Y() == rEnd
.Y())
251 const tools::Long nEndX
= rEnd
.X();
258 for (; nX
<= nEndX
; nX
++)
259 SetPixel(nY
, nX
, rLineColor
);
263 for (; nX
>= nEndX
; nX
--)
264 SetPixel(nY
, nX
, rLineColor
);
269 const tools::Long nDX
= std::abs(rEnd
.X() - rStart
.X());
270 const tools::Long nDY
= std::abs(rEnd
.Y() - rStart
.Y());
278 if (rStart
.X() < rEnd
.X())
293 const tools::Long nDYX
= (nDY
- nDX
) << 1;
294 const tools::Long nDY2
= nDY
<< 1;
295 tools::Long nD
= nDY2
- nDX
;
296 bool bPos
= nY1
< nY2
;
298 for (nX
= nX1
, nY
= nY1
; nX
<= nX2
; nX
++)
300 SetPixel(nY
, nX
, rLineColor
);
317 if (rStart
.Y() < rEnd
.Y())
332 const tools::Long nDYX
= (nDX
- nDY
) << 1;
333 const tools::Long nDY2
= nDX
<< 1;
334 tools::Long nD
= nDY2
- nDY
;
335 bool bPos
= nX1
< nX2
;
337 for (nX
= nX1
, nY
= nY1
; nY
<= nY2
; nY
++)
339 SetPixel(nY
, nX
, rLineColor
);
357 void BitmapWriteAccess::FillRect(const tools::Rectangle
& rRect
)
362 const BitmapColor
& rFillColor
= *mpFillColor
;
363 tools::Rectangle
aRect(Point(), maBitmap
.GetSizePixel());
365 aRect
.Intersection(rRect
);
370 const tools::Long nStartX
= rRect
.Left();
371 const tools::Long nStartY
= rRect
.Top();
372 const tools::Long nEndX
= rRect
.Right();
373 const tools::Long nEndY
= rRect
.Bottom();
375 for (tools::Long nY
= nStartY
; nY
<= nEndY
; nY
++)
377 Scanline pScanline
= GetScanline(nY
);
378 for (tools::Long nX
= nStartX
; nX
<= nEndX
; nX
++)
380 SetPixelOnData(pScanline
, nX
, rFillColor
);
385 void BitmapWriteAccess::DrawRect(const tools::Rectangle
& rRect
)
390 if (mpLineColor
&& (!mpFillColor
|| (*mpFillColor
!= *mpLineColor
)))
392 DrawLine(rRect
.TopLeft(), rRect
.TopRight());
393 DrawLine(rRect
.TopRight(), rRect
.BottomRight());
394 DrawLine(rRect
.BottomRight(), rRect
.BottomLeft());
395 DrawLine(rRect
.BottomLeft(), rRect
.TopLeft());
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */