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 <vcl/bitmap.hxx>
22 #include <bmpfast.hxx>
23 #include <bitmapwriteaccess.hxx>
25 void BitmapWriteAccess::SetLineColor( const Color
& rColor
)
27 if (rColor
.GetTransparency() == 255)
35 mpLineColor
= BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
)));
39 mpLineColor
= BitmapColor(rColor
);
44 void BitmapWriteAccess::SetFillColor()
49 void BitmapWriteAccess::SetFillColor( const Color
& rColor
)
51 if (rColor
.GetTransparency() == 255)
59 mpFillColor
= BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
)));
63 mpFillColor
= BitmapColor(rColor
);
68 void BitmapWriteAccess::Erase( const Color
& rColor
)
70 // convert the color format from RGB to palette index if needed
71 // TODO: provide and use Erase( BitmapColor& method)
72 BitmapColor aColor
= rColor
;
75 aColor
= BitmapColor(static_cast<sal_uInt8
>(GetBestPaletteIndex(rColor
)));
78 // try fast bitmap method first
79 if (ImplFastEraseBitmap(*mpBuffer
, aColor
))
82 tools::Rectangle
aRect(Point(), maBitmap
.GetSizePixel());
85 // clear the bitmap by filling the first line pixel by pixel,
86 // then dup the first line over each other line
87 Scanline pFirstScanline
= GetScanline(0);
88 const long nEndX
= aRect
.Right();
89 for (long nX
= 0; nX
<= nEndX
; ++nX
)
90 SetPixelOnData(pFirstScanline
, nX
, rColor
);
91 const auto nScanlineSize
= GetScanlineSize();
92 const long nEndY
= aRect
.Bottom();
93 for (long nY
= 1; nY
<= nEndY
; nY
++)
95 Scanline pDestScanline
= GetScanline(nY
);
96 memcpy(pDestScanline
, pFirstScanline
, nScanlineSize
);
100 void BitmapWriteAccess::DrawLine( const Point
& rStart
, const Point
& rEnd
)
104 const BitmapColor
& rLineColor
= *mpLineColor
;
107 if (rStart
.X() == rEnd
.X())
110 const long nEndY
= rEnd
.Y();
117 for (; nY
<= nEndY
; nY
++ )
118 SetPixel( nY
, nX
, rLineColor
);
122 for (; nY
>= nEndY
; nY
-- )
123 SetPixel( nY
, nX
, rLineColor
);
126 else if (rStart
.Y() == rEnd
.Y())
129 const long nEndX
= rEnd
.X();
136 for (; nX
<= nEndX
; nX
++)
137 SetPixel(nY
, nX
, rLineColor
);
141 for (; nX
>= nEndX
; nX
--)
142 SetPixel(nY
, nX
, rLineColor
);
147 const long nDX
= labs( rEnd
.X() - rStart
.X() );
148 const long nDY
= labs( rEnd
.Y() - rStart
.Y() );
156 if (rStart
.X() < rEnd
.X())
171 const long nDYX
= (nDY
- nDX
) << 1;
172 const long nDY2
= nDY
<< 1;
173 long nD
= nDY2
- nDX
;
174 bool bPos
= nY1
< nY2
;
176 for (nX
= nX1
, nY
= nY1
; nX
<= nX2
; nX
++)
178 SetPixel(nY
, nX
, rLineColor
);
195 if (rStart
.Y() < rEnd
.Y())
210 const long nDYX
= (nDX
- nDY
) << 1;
211 const long nDY2
= nDX
<< 1;
212 long nD
= nDY2
- nDY
;
213 bool bPos
= nX1
< nX2
;
215 for (nX
= nX1
, nY
= nY1
; nY
<= nY2
; nY
++)
217 SetPixel(nY
, nX
, rLineColor
);
236 void BitmapWriteAccess::FillRect( const tools::Rectangle
& rRect
)
240 const BitmapColor
& rFillColor
= *mpFillColor
;
241 tools::Rectangle
aRect(Point(), maBitmap
.GetSizePixel());
243 aRect
.Intersection(rRect
);
245 if (!aRect
.IsEmpty())
247 const long nStartX
= rRect
.Left();
248 const long nStartY
= rRect
.Top();
249 const long nEndX
= rRect
.Right();
250 const long nEndY
= rRect
.Bottom();
252 for (long nY
= nStartY
; nY
<= nEndY
; nY
++)
254 Scanline pScanline
= GetScanline( nY
);
255 for (long nX
= nStartX
; nX
<= nEndX
; nX
++)
257 SetPixelOnData(pScanline
, nX
, rFillColor
);
264 void BitmapWriteAccess::DrawRect( const tools::Rectangle
& rRect
)
269 if (mpLineColor
&& (!mpFillColor
|| ( *mpFillColor
!= *mpLineColor
)))
271 DrawLine(rRect
.TopLeft(), rRect
.TopRight());
272 DrawLine(rRect
.TopRight(), rRect
.BottomRight());
273 DrawLine(rRect
.BottomRight(), rRect
.BottomLeft());
274 DrawLine(rRect
.BottomLeft(), rRect
.TopLeft());
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */