Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / source / gdi / bmpacc3.cxx
blobf8aeb5c99bf94e33a4e1726386dd1263a1483d0b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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)
33 mpLineColor.reset();
35 else
37 if (HasPalette())
39 mpLineColor.reset(new BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rColor))));
41 else
43 mpLineColor.reset(new BitmapColor(rColor));
48 void BitmapWriteAccess::SetFillColor()
50 mpFillColor.reset();
53 void BitmapWriteAccess::SetFillColor( const Color& rColor )
55 if (rColor.GetTransparency() == 255)
57 mpFillColor.reset();
59 else
61 if (HasPalette())
63 mpFillColor.reset(new BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rColor))));
65 else
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;
77 if (HasPalette())
79 aColor = BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rColor)));
81 // try fast bitmap method first
82 if (ImplFastEraseBitmap(*mpBuffer, aColor))
83 return;
85 // use the canonical method to clear the bitmap
86 BitmapColor* pOldFillColor = mpFillColor ? new BitmapColor(*mpFillColor) : nullptr;
87 const Point aPoint;
88 const tools::Rectangle aRect(aPoint, maBitmap.GetSizePixel());
90 SetFillColor(rColor);
91 FillRect(aRect);
93 mpFillColor.reset(pOldFillColor);
96 void BitmapWriteAccess::DrawLine( const Point& rStart, const Point& rEnd )
98 if (mpLineColor)
100 const BitmapColor& rLineColor = *mpLineColor.get();
101 long nX, nY;
103 if (rStart.X() == rEnd.X())
105 // Vertical Line
106 const long nEndY = rEnd.Y();
108 nX = rStart.X();
109 nY = rStart.Y();
111 if (nEndY > nY)
113 for (; nY <= nEndY; nY++ )
114 SetPixel( nY, nX, rLineColor );
116 else
118 for (; nY >= nEndY; nY-- )
119 SetPixel( nY, nX, rLineColor );
122 else if (rStart.Y() == rEnd.Y())
124 // Horizontal Line
125 const long nEndX = rEnd.X();
127 nX = rStart.X();
128 nY = rStart.Y();
130 if (nEndX > nX)
132 for (; nX <= nEndX; nX++)
133 SetPixel(nY, nX, rLineColor);
135 else
137 for (; nX >= nEndX; nX--)
138 SetPixel(nY, nX, rLineColor);
141 else
143 const long nDX = labs( rEnd.X() - rStart.X() );
144 const long nDY = labs( rEnd.Y() - rStart.Y() );
145 long nX1;
146 long nY1;
147 long nX2;
148 long nY2;
150 if (nDX >= nDY)
152 if (rStart.X() < rEnd.X())
154 nX1 = rStart.X();
155 nY1 = rStart.Y();
156 nX2 = rEnd.X();
157 nY2 = rEnd.Y();
159 else
161 nX1 = rEnd.X();
162 nY1 = rEnd.Y();
163 nX2 = rStart.X();
164 nY2 = rStart.Y();
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);
176 if (nD < 0)
177 nD += nDY2;
178 else
180 nD += nDYX;
182 if (bPos)
183 nY++;
184 else
185 nY--;
189 else
191 if (rStart.Y() < rEnd.Y())
193 nX1 = rStart.X();
194 nY1 = rStart.Y();
195 nX2 = rEnd.X();
196 nY2 = rEnd.Y();
198 else
200 nX1 = rEnd.X();
201 nY1 = rEnd.Y();
202 nX2 = rStart.X();
203 nY2 = rStart.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);
215 if (nD < 0)
216 nD += nDY2;
217 else
219 nD += nDYX;
221 if (bPos)
222 nX++;
223 else
224 nX--;
232 void BitmapWriteAccess::FillRect( const tools::Rectangle& rRect )
234 if (mpFillColor)
236 const BitmapColor& rFillColor = *mpFillColor.get();
237 Point aPoint;
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 )
262 if (mpFillColor)
263 FillRect(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: */