Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <tools/poly.hxx>
22 :
23 : #include <vcl/salbtype.hxx>
24 : #include <vcl/bitmap.hxx>
25 : #include <vcl/region.hxx>
26 : #include <vcl/bmpacc.hxx>
27 :
28 : #include <bmpfast.hxx>
29 :
30 12600 : void BitmapWriteAccess::SetLineColor( const Color& rColor )
31 : {
32 12600 : delete mpLineColor;
33 :
34 12600 : if( rColor.GetTransparency() == 255 )
35 0 : mpLineColor = NULL;
36 : else
37 12600 : mpLineColor = ( HasPalette() ? new BitmapColor( (sal_uInt8) GetBestPaletteIndex( rColor ) ) : new BitmapColor( rColor ) );
38 12600 : }
39 :
40 5430 : void BitmapWriteAccess::SetFillColor( const Color& rColor )
41 : {
42 5430 : delete mpFillColor;
43 :
44 5430 : if( rColor.GetTransparency() == 255 )
45 0 : mpFillColor = NULL;
46 : else
47 5430 : mpFillColor = ( HasPalette() ? new BitmapColor( (sal_uInt8) GetBestPaletteIndex( rColor ) ) : new BitmapColor( rColor ) );
48 5430 : }
49 :
50 765 : void BitmapWriteAccess::Erase( const Color& rColor )
51 : {
52 : // convert the color format from RGB to palette index if needed
53 : // TODO: provide and use Erase( BitmapColor& method)
54 765 : BitmapColor aColor = rColor;
55 765 : if( HasPalette() )
56 541 : aColor = BitmapColor( (sal_uInt8)GetBestPaletteIndex( rColor) );
57 : // try fast bitmap method first
58 765 : if( ImplFastEraseBitmap( *mpBuffer, aColor ) )
59 1500 : return;
60 :
61 : // use the canonical method to clear the bitmap
62 30 : BitmapColor* pOldFillColor = mpFillColor ? new BitmapColor( *mpFillColor ) : NULL;
63 30 : const Point aPoint;
64 30 : const Rectangle aRect( aPoint, maBitmap.GetSizePixel() );
65 :
66 30 : SetFillColor( rColor );
67 30 : FillRect( aRect );
68 30 : delete mpFillColor;
69 30 : mpFillColor = pOldFillColor;
70 : }
71 :
72 11000 : void BitmapWriteAccess::DrawLine( const Point& rStart, const Point& rEnd )
73 : {
74 11000 : if( mpLineColor )
75 : {
76 11000 : const BitmapColor& rLineColor = *mpLineColor;
77 : long nX, nY;
78 :
79 11000 : if ( rStart.X() == rEnd.X() )
80 : {
81 : // Vertical Line
82 4400 : const long nEndY = rEnd.Y();
83 :
84 4400 : nX = rStart.X();
85 4400 : nY = rStart.Y();
86 :
87 4400 : if ( nEndY > nY )
88 : {
89 35200 : for (; nY <= nEndY; nY++ )
90 30800 : SetPixel( nY, nX, rLineColor );
91 : }
92 : else
93 : {
94 0 : for (; nY >= nEndY; nY-- )
95 0 : SetPixel( nY, nX, rLineColor );
96 : }
97 : }
98 6600 : else if ( rStart.Y() == rEnd.Y() )
99 : {
100 : // Horizontal Line
101 6600 : const long nEndX = rEnd.X();
102 :
103 6600 : nX = rStart.X();
104 6600 : nY = rStart.Y();
105 :
106 6600 : if ( nEndX > nX )
107 : {
108 77000 : for (; nX <= nEndX; nX++ )
109 70400 : SetPixel( nY, nX, rLineColor );
110 : }
111 : else
112 : {
113 0 : for (; nX >= nEndX; nX-- )
114 0 : SetPixel( nY, nX, rLineColor );
115 : }
116 : }
117 : else
118 : {
119 0 : const long nDX = labs( rEnd.X() - rStart.X() );
120 0 : const long nDY = labs( rEnd.Y() - rStart.Y() );
121 : long nX1;
122 : long nY1;
123 : long nX2;
124 : long nY2;
125 :
126 0 : if ( nDX >= nDY )
127 : {
128 0 : if ( rStart.X() < rEnd.X() )
129 : {
130 0 : nX1 = rStart.X();
131 0 : nY1 = rStart.Y();
132 0 : nX2 = rEnd.X();
133 0 : nY2 = rEnd.Y();
134 : }
135 : else
136 : {
137 0 : nX1 = rEnd.X();
138 0 : nY1 = rEnd.Y();
139 0 : nX2 = rStart.X();
140 0 : nY2 = rStart.Y();
141 : }
142 :
143 0 : const long nDYX = ( nDY - nDX ) << 1;
144 0 : const long nDY2 = nDY << 1;
145 0 : long nD = nDY2 - nDX;
146 0 : bool bPos = nY1 < nY2;
147 :
148 0 : for ( nX = nX1, nY = nY1; nX <= nX2; nX++ )
149 : {
150 0 : SetPixel( nY, nX, rLineColor );
151 :
152 0 : if ( nD < 0 )
153 0 : nD += nDY2;
154 : else
155 : {
156 0 : nD += nDYX;
157 :
158 0 : if ( bPos )
159 0 : nY++;
160 : else
161 0 : nY--;
162 : }
163 : }
164 : }
165 : else
166 : {
167 0 : if ( rStart.Y() < rEnd.Y() )
168 : {
169 0 : nX1 = rStart.X();
170 0 : nY1 = rStart.Y();
171 0 : nX2 = rEnd.X();
172 0 : nY2 = rEnd.Y();
173 : }
174 : else
175 : {
176 0 : nX1 = rEnd.X();
177 0 : nY1 = rEnd.Y();
178 0 : nX2 = rStart.X();
179 0 : nY2 = rStart.Y();
180 : }
181 :
182 0 : const long nDYX = ( nDX - nDY ) << 1;
183 0 : const long nDY2 = nDX << 1;
184 0 : long nD = nDY2 - nDY;
185 0 : bool bPos = nX1 < nX2;
186 :
187 0 : for ( nX = nX1, nY = nY1; nY <= nY2; nY++ )
188 : {
189 0 : SetPixel( nY, nX, rLineColor );
190 :
191 0 : if ( nD < 0 )
192 0 : nD += nDY2;
193 : else
194 : {
195 0 : nD += nDYX;
196 :
197 0 : if ( bPos )
198 0 : nX++;
199 : else
200 0 : nX--;
201 : }
202 : }
203 : }
204 : }
205 : }
206 11000 : }
207 :
208 5430 : void BitmapWriteAccess::FillRect( const Rectangle& rRect )
209 : {
210 5430 : if( mpFillColor )
211 : {
212 5430 : const BitmapColor& rFillColor = *mpFillColor;
213 5430 : Point aPoint;
214 5430 : Rectangle aRect( aPoint, maBitmap.GetSizePixel() );
215 :
216 5430 : aRect.Intersection( rRect );
217 :
218 5430 : if( !aRect.IsEmpty() )
219 : {
220 5430 : const long nStartX = rRect.Left();
221 5430 : const long nStartY = rRect.Top();
222 5430 : const long nEndX = rRect.Right();
223 5430 : const long nEndY = rRect.Bottom();
224 :
225 33930 : for( long nY = nStartY; nY <= nEndY; nY++ )
226 421100 : for( long nX = nStartX; nX <= nEndX; nX++ )
227 392600 : SetPixel( nY, nX, rFillColor );
228 : }
229 : }
230 5430 : }
231 :
232 3200 : void BitmapWriteAccess::DrawRect( const Rectangle& rRect )
233 : {
234 3200 : if( mpFillColor )
235 3200 : FillRect( rRect );
236 :
237 3200 : if( mpLineColor && ( !mpFillColor || ( *mpFillColor != *mpLineColor ) ) )
238 : {
239 0 : DrawLine( rRect.TopLeft(), rRect.TopRight() );
240 0 : DrawLine( rRect.TopRight(), rRect.BottomRight() );
241 0 : DrawLine( rRect.BottomRight(), rRect.BottomLeft() );
242 0 : DrawLine( rRect.BottomLeft(), rRect.TopLeft() );
243 : }
244 3200 : }
245 :
246 : // ------------------------------------------------------------------
247 :
248 0 : void BitmapWriteAccess::FillPolygon( const Polygon& rPoly )
249 : {
250 0 : const sal_uInt16 nSize = rPoly.GetSize();
251 :
252 0 : if( nSize && mpFillColor )
253 : {
254 0 : const BitmapColor& rFillColor = *mpFillColor;
255 0 : Region aRegion( rPoly );
256 : // Rectangle aRect;
257 :
258 0 : aRegion.Intersect( Rectangle( Point(), Size( Width(), Height() ) ) );
259 :
260 0 : if( !aRegion.IsEmpty() )
261 : {
262 0 : RectangleVector aRectangles;
263 0 : aRegion.GetRegionRectangles(aRectangles);
264 :
265 0 : for(RectangleVector::const_iterator aRectIter(aRectangles.begin()); aRectIter != aRectangles.end(); ++aRectIter)
266 : {
267 0 : for(long nY = aRectIter->Top(), nEndY = aRectIter->Bottom(); nY <= nEndY; nY++)
268 : {
269 0 : for(long nX = aRectIter->Left(), nEndX = aRectIter->Right(); nX <= nEndX; nX++)
270 : {
271 0 : SetPixel(nY, nX, rFillColor);
272 : }
273 : }
274 0 : }
275 :
276 : //RegionHandle aRegHandle( aRegion.BeginEnumRects() );
277 : //
278 : //while( aRegion.GetEnumRects( aRegHandle, aRect ) )
279 : // for( long nY = aRect.Top(), nEndY = aRect.Bottom(); nY <= nEndY; nY++ )
280 : // for( long nX = aRect.Left(), nEndX = aRect.Right(); nX <= nEndX; nX++ )
281 : // SetPixel( nY, nX, rFillColor );
282 : //
283 : //aRegion.EndEnumRects( aRegHandle );
284 0 : }
285 : }
286 0 : }
287 :
288 : // ------------------------------------------------------------------
289 :
290 0 : void BitmapWriteAccess::DrawPolygon( const Polygon& rPoly )
291 : {
292 0 : if( mpFillColor )
293 0 : FillPolygon( rPoly );
294 :
295 0 : if( mpLineColor && ( !mpFillColor || ( *mpFillColor != *mpLineColor ) ) )
296 : {
297 0 : const sal_uInt16 nSize = rPoly.GetSize();
298 :
299 0 : for( sal_uInt16 i = 0, nSize1 = nSize - 1; i < nSize1; i++ )
300 0 : DrawLine( rPoly[ i ], rPoly[ i + 1 ] );
301 :
302 0 : if( rPoly[ nSize - 1 ] != rPoly[ 0 ] )
303 0 : DrawLine( rPoly[ nSize - 1 ], rPoly[ 0 ] );
304 : }
305 0 : }
306 :
307 : // ------------------------------------------------------------------
308 :
309 0 : void BitmapWriteAccess::FillPolyPolygon( const PolyPolygon& rPolyPoly )
310 : {
311 0 : const sal_uInt16 nCount = rPolyPoly.Count();
312 :
313 0 : if( nCount && mpFillColor )
314 : {
315 0 : const BitmapColor& rFillColor = *mpFillColor;
316 0 : Region aRegion( rPolyPoly );
317 : //Rectangle aRect;
318 :
319 0 : aRegion.Intersect( Rectangle( Point(), Size( Width(), Height() ) ) );
320 :
321 0 : if( !aRegion.IsEmpty() )
322 : {
323 0 : RectangleVector aRectangles;
324 0 : aRegion.GetRegionRectangles(aRectangles);
325 :
326 0 : for(RectangleVector::const_iterator aRectIter(aRectangles.begin()); aRectIter != aRectangles.end(); ++aRectIter)
327 : {
328 0 : for(long nY = aRectIter->Top(), nEndY = aRectIter->Bottom(); nY <= nEndY; nY++)
329 : {
330 0 : for(long nX = aRectIter->Left(), nEndX = aRectIter->Right(); nX <= nEndX; nX++)
331 : {
332 0 : SetPixel(nY, nX, rFillColor);
333 : }
334 : }
335 0 : }
336 :
337 : //RegionHandle aRegHandle( aRegion.BeginEnumRects() );
338 : //
339 : //while( aRegion.GetEnumRects( aRegHandle, aRect ) )
340 : // for( long nY = aRect.Top(), nEndY = aRect.Bottom(); nY <= nEndY; nY++ )
341 : // for( long nX = aRect.Left(), nEndX = aRect.Right(); nX <= nEndX; nX++ )
342 : // SetPixel( nY, nX, rFillColor );
343 : //
344 : //aRegion.EndEnumRects( aRegHandle );
345 0 : }
346 : }
347 0 : }
348 :
349 : // ------------------------------------------------------------------
350 :
351 0 : void BitmapWriteAccess::DrawPolyPolygon( const PolyPolygon& rPolyPoly )
352 : {
353 0 : if( mpFillColor )
354 0 : FillPolyPolygon( rPolyPoly );
355 :
356 0 : if( mpLineColor && ( !mpFillColor || ( *mpFillColor != *mpLineColor ) ) )
357 : {
358 0 : for( sal_uInt16 n = 0, nCount = rPolyPoly.Count(); n < nCount; )
359 : {
360 0 : const Polygon& rPoly = rPolyPoly[ n++ ];
361 0 : const sal_uInt16 nSize = rPoly.GetSize();
362 :
363 0 : if( nSize )
364 : {
365 0 : for( sal_uInt16 i = 0, nSize1 = nSize - 1; i < nSize1; i++ )
366 0 : DrawLine( rPoly[ i ], rPoly[ i + 1 ] );
367 :
368 0 : if( rPoly[ nSize - 1 ] != rPoly[ 0 ] )
369 0 : DrawLine( rPoly[ nSize - 1 ], rPoly[ 0 ] );
370 : }
371 : }
372 : }
373 465 : }
374 :
375 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|