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 <tools/poly.hxx>
22 #include <vcl/salbtype.hxx>
23 #include <vcl/bitmap.hxx>
24 #include <vcl/region.hxx>
25 #include <vcl/bitmapaccess.hxx>
27 #include <bmpfast.hxx>
29 void BitmapWriteAccess::SetLineColor( const Color
& rColor
)
31 if (rColor
.GetTransparency() == 255)
39 mpLineColor
.reset(new BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
))));
43 mpLineColor
.reset(new BitmapColor(rColor
));
48 void BitmapWriteAccess::SetFillColor()
53 void BitmapWriteAccess::SetFillColor( const Color
& rColor
)
55 if (rColor
.GetTransparency() == 255)
63 mpFillColor
.reset(new BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
))));
67 mpFillColor
.reset(new BitmapColor(rColor
));
72 void BitmapWriteAccess::Erase( const Color
& rColor
)
74 // convert the color format from RGB to palette index if needed
75 // TODO: provide and use Erase( BitmapColor& method)
76 BitmapColor aColor
= rColor
;
79 aColor
= BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
)));
81 // try fast bitmap method first
82 if (ImplFastEraseBitmap(*mpBuffer
, aColor
))
85 // use the canonical method to clear the bitmap
86 BitmapColor
* pOldFillColor
= mpFillColor
? new BitmapColor(*mpFillColor
) : nullptr;
88 const tools::Rectangle
aRect(aPoint
, maBitmap
.GetSizePixel());
93 mpFillColor
.reset(pOldFillColor
);
96 void BitmapWriteAccess::DrawLine( const Point
& rStart
, const Point
& rEnd
)
100 const BitmapColor
& rLineColor
= *mpLineColor
.get();
103 if (rStart
.X() == rEnd
.X())
106 const long nEndY
= rEnd
.Y();
113 for (; nY
<= nEndY
; nY
++ )
114 SetPixel( nY
, nX
, rLineColor
);
118 for (; nY
>= nEndY
; nY
-- )
119 SetPixel( nY
, nX
, rLineColor
);
122 else if (rStart
.Y() == rEnd
.Y())
125 const long nEndX
= rEnd
.X();
132 for (; nX
<= nEndX
; nX
++)
133 SetPixel(nY
, nX
, rLineColor
);
137 for (; nX
>= nEndX
; nX
--)
138 SetPixel(nY
, nX
, rLineColor
);
143 const long nDX
= labs( rEnd
.X() - rStart
.X() );
144 const long nDY
= labs( rEnd
.Y() - rStart
.Y() );
152 if (rStart
.X() < rEnd
.X())
167 const long nDYX
= (nDY
- nDX
) << 1;
168 const long nDY2
= nDY
<< 1;
169 long nD
= nDY2
- nDX
;
170 bool bPos
= nY1
< nY2
;
172 for (nX
= nX1
, nY
= nY1
; nX
<= nX2
; nX
++)
174 SetPixel(nY
, nX
, rLineColor
);
191 if (rStart
.Y() < rEnd
.Y())
206 const long nDYX
= (nDX
- nDY
) << 1;
207 const long nDY2
= nDX
<< 1;
208 long nD
= nDY2
- nDY
;
209 bool bPos
= nX1
< nX2
;
211 for (nX
= nX1
, nY
= nY1
; nY
<= nY2
; nY
++)
213 SetPixel(nY
, nX
, rLineColor
);
232 void BitmapWriteAccess::FillRect( const tools::Rectangle
& rRect
)
236 const BitmapColor
& rFillColor
= *mpFillColor
.get();
238 tools::Rectangle
aRect(aPoint
, maBitmap
.GetSizePixel());
240 aRect
.Intersection(rRect
);
242 if (!aRect
.IsEmpty())
244 const long nStartX
= rRect
.Left();
245 const long nStartY
= rRect
.Top();
246 const long nEndX
= rRect
.Right();
247 const long nEndY
= rRect
.Bottom();
249 for (long nY
= nStartY
; nY
<= nEndY
; nY
++)
251 for (long nX
= nStartX
; nX
<= nEndX
; nX
++)
253 SetPixel(nY
, nX
, rFillColor
);
260 void BitmapWriteAccess::DrawRect( const tools::Rectangle
& rRect
)
265 if (mpLineColor
&& (!mpFillColor
|| ( *mpFillColor
.get() != *mpLineColor
.get())))
267 DrawLine(rRect
.TopLeft(), rRect
.TopRight());
268 DrawLine(rRect
.TopRight(), rRect
.BottomRight());
269 DrawLine(rRect
.BottomRight(), rRect
.BottomLeft());
270 DrawLine(rRect
.BottomLeft(), rRect
.TopLeft());
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */