update dev300-m58
[ooovba.git] / vcl / source / gdi / bmpacc3.cxx
blob70dc50fd9889b7089718c6cc56f8ecf1cbb72aa0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bmpacc3.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
33 #include <vcl/salbtype.hxx>
34 #include <vcl/bitmap.hxx>
35 #include <tools/poly.hxx>
36 #include <vcl/region.hxx>
37 #include <vcl/bmpacc.hxx>
38 #include <vcl/bmpfast.hxx>
40 // ---------------------
41 // - BitmapWriteAccess -
42 // ---------------------
44 void BitmapWriteAccess::SetLineColor()
46 delete mpLineColor;
47 mpLineColor = NULL;
50 // ------------------------------------------------------------------
52 void BitmapWriteAccess::SetLineColor( const Color& rColor )
54 delete mpLineColor;
56 if( rColor.GetTransparency() == 255 )
57 mpLineColor = NULL;
58 else
59 mpLineColor = ( HasPalette() ? new BitmapColor( (BYTE) GetBestPaletteIndex( rColor ) ) : new BitmapColor( rColor ) );
62 // ------------------------------------------------------------------
64 Color BitmapWriteAccess::GetLineColor() const
66 Color aRet;
68 if( mpLineColor )
69 aRet = (const Color&) *mpLineColor;
70 else
71 aRet.SetTransparency( 255 );
73 return aRet;
76 // ------------------------------------------------------------------
78 void BitmapWriteAccess::SetFillColor()
80 delete mpFillColor;
81 mpFillColor = NULL;
84 // ------------------------------------------------------------------
86 void BitmapWriteAccess::SetFillColor( const Color& rColor )
88 delete mpFillColor;
90 if( rColor.GetTransparency() == 255 )
91 mpFillColor = NULL;
92 else
93 mpFillColor = ( HasPalette() ? new BitmapColor( (BYTE) GetBestPaletteIndex( rColor ) ) : new BitmapColor( rColor ) );
96 // ------------------------------------------------------------------
98 Color BitmapWriteAccess::GetFillColor() const
100 Color aRet;
102 if( mpFillColor )
103 aRet = (const Color&) *mpFillColor;
104 else
105 aRet.SetTransparency( 255 );
107 return aRet;
110 // ------------------------------------------------------------------
112 void BitmapWriteAccess::Erase( const Color& rColor )
114 // convert the color format from RGB to palette index if needed
115 // TODO: provide and use Erase( BitmapColor& method)
116 BitmapColor aColor = rColor;
117 if( HasPalette() )
118 aColor = BitmapColor( (BYTE)GetBestPaletteIndex( rColor) );
119 // try fast bitmap method first
120 if( ImplFastEraseBitmap( *mpBuffer, aColor ) )
121 return;
123 // use the canonical method to clear the bitmap
124 BitmapColor* pOldFillColor = mpFillColor ? new BitmapColor( *mpFillColor ) : NULL;
125 const Point aPoint;
126 const Rectangle aRect( aPoint, maBitmap.GetSizePixel() );
128 SetFillColor( rColor );
129 FillRect( aRect );
130 delete mpFillColor;
131 mpFillColor = pOldFillColor;
134 // ------------------------------------------------------------------
136 void BitmapWriteAccess::DrawLine( const Point& rStart, const Point& rEnd )
138 if( mpLineColor )
140 const BitmapColor& rLineColor = *mpLineColor;
141 long nX, nY;
143 if ( rStart.X() == rEnd.X() )
145 // vertikale Line
146 const long nEndY = rEnd.Y();
148 nX = rStart.X();
149 nY = rStart.Y();
151 if ( nEndY > nY )
153 for (; nY <= nEndY; nY++ )
154 SetPixel( nY, nX, rLineColor );
156 else
158 for (; nY >= nEndY; nY-- )
159 SetPixel( nY, nX, rLineColor );
162 else if ( rStart.Y() == rEnd.Y() )
164 // horizontale Line
165 const long nEndX = rEnd.X();
167 nX = rStart.X();
168 nY = rStart.Y();
170 if ( nEndX > nX )
172 for (; nX <= nEndX; nX++ )
173 SetPixel( nY, nX, rLineColor );
175 else
177 for (; nX >= nEndX; nX-- )
178 SetPixel( nY, nX, rLineColor );
181 else
183 const long nDX = labs( rEnd.X() - rStart.X() );
184 const long nDY = labs( rEnd.Y() - rStart.Y() );
185 long nX1;
186 long nY1;
187 long nX2;
188 long nY2;
190 if ( nDX >= nDY )
192 if ( rStart.X() < rEnd.X() )
194 nX1 = rStart.X();
195 nY1 = rStart.Y();
196 nX2 = rEnd.X();
197 nY2 = rEnd.Y();
199 else
201 nX1 = rEnd.X();
202 nY1 = rEnd.Y();
203 nX2 = rStart.X();
204 nY2 = rStart.Y();
207 const long nDYX = ( nDY - nDX ) << 1;
208 const long nDY2 = nDY << 1;
209 long nD = nDY2 - nDX;
210 BOOL bPos = nY1 < nY2;
212 for ( nX = nX1, nY = nY1; nX <= nX2; nX++ )
214 SetPixel( nY, nX, rLineColor );
216 if ( nD < 0 )
217 nD += nDY2;
218 else
220 nD += nDYX;
222 if ( bPos )
223 nY++;
224 else
225 nY--;
229 else
231 if ( rStart.Y() < rEnd.Y() )
233 nX1 = rStart.X();
234 nY1 = rStart.Y();
235 nX2 = rEnd.X();
236 nY2 = rEnd.Y();
238 else
240 nX1 = rEnd.X();
241 nY1 = rEnd.Y();
242 nX2 = rStart.X();
243 nY2 = rStart.Y();
246 const long nDYX = ( nDX - nDY ) << 1;
247 const long nDY2 = nDX << 1;
248 long nD = nDY2 - nDY;
249 BOOL bPos = nX1 < nX2;
251 for ( nX = nX1, nY = nY1; nY <= nY2; nY++ )
253 SetPixel( nY, nX, rLineColor );
255 if ( nD < 0 )
256 nD += nDY2;
257 else
259 nD += nDYX;
261 if ( bPos )
262 nX++;
263 else
264 nX--;
272 // ------------------------------------------------------------------
274 void BitmapWriteAccess::FillRect( const Rectangle& rRect )
276 if( mpFillColor )
278 const BitmapColor& rFillColor = *mpFillColor;
279 Point aPoint;
280 Rectangle aRect( aPoint, maBitmap.GetSizePixel() );
282 aRect.Intersection( rRect );
284 if( !aRect.IsEmpty() )
286 const long nStartX = rRect.Left();
287 const long nStartY = rRect.Top();
288 const long nEndX = rRect.Right();
289 const long nEndY = rRect.Bottom();
291 for( long nY = nStartY; nY <= nEndY; nY++ )
292 for( long nX = nStartX; nX <= nEndX; nX++ )
293 SetPixel( nY, nX, rFillColor );
298 // ------------------------------------------------------------------
300 void BitmapWriteAccess::DrawRect( const Rectangle& rRect )
302 if( mpFillColor )
303 FillRect( rRect );
305 if( mpLineColor && ( !mpFillColor || ( *mpFillColor != *mpLineColor ) ) )
307 DrawLine( rRect.TopLeft(), rRect.TopRight() );
308 DrawLine( rRect.TopRight(), rRect.BottomRight() );
309 DrawLine( rRect.BottomRight(), rRect.BottomLeft() );
310 DrawLine( rRect.BottomLeft(), rRect.TopLeft() );
314 // ------------------------------------------------------------------
316 void BitmapWriteAccess::FillPolygon( const Polygon& rPoly )
318 const USHORT nSize = rPoly.GetSize();
320 if( nSize && mpFillColor )
322 const BitmapColor& rFillColor = *mpFillColor;
323 Region aRegion( rPoly );
324 Rectangle aRect;
326 aRegion.Intersect( Rectangle( Point(), Size( Width(), Height() ) ) );
328 if( !aRegion.IsEmpty() )
330 RegionHandle aRegHandle( aRegion.BeginEnumRects() );
332 while( aRegion.GetNextEnumRect( aRegHandle, aRect ) )
333 for( long nY = aRect.Top(), nEndY = aRect.Bottom(); nY <= nEndY; nY++ )
334 for( long nX = aRect.Left(), nEndX = aRect.Right(); nX <= nEndX; nX++ )
335 SetPixel( nY, nX, rFillColor );
337 aRegion.EndEnumRects( aRegHandle );
342 // ------------------------------------------------------------------
344 void BitmapWriteAccess::DrawPolygon( const Polygon& rPoly )
346 if( mpFillColor )
347 FillPolygon( rPoly );
349 if( mpLineColor && ( !mpFillColor || ( *mpFillColor != *mpLineColor ) ) )
351 const USHORT nSize = rPoly.GetSize();
353 for( USHORT i = 0, nSize1 = nSize - 1; i < nSize1; i++ )
354 DrawLine( rPoly[ i ], rPoly[ i + 1 ] );
356 if( rPoly[ nSize - 1 ] != rPoly[ 0 ] )
357 DrawLine( rPoly[ nSize - 1 ], rPoly[ 0 ] );
361 // ------------------------------------------------------------------
363 void BitmapWriteAccess::FillPolyPolygon( const PolyPolygon& rPolyPoly )
365 const USHORT nCount = rPolyPoly.Count();
367 if( nCount && mpFillColor )
369 const BitmapColor& rFillColor = *mpFillColor;
370 Region aRegion( rPolyPoly );
371 Rectangle aRect;
373 aRegion.Intersect( Rectangle( Point(), Size( Width(), Height() ) ) );
375 if( !aRegion.IsEmpty() )
377 RegionHandle aRegHandle( aRegion.BeginEnumRects() );
379 while( aRegion.GetNextEnumRect( aRegHandle, aRect ) )
380 for( long nY = aRect.Top(), nEndY = aRect.Bottom(); nY <= nEndY; nY++ )
381 for( long nX = aRect.Left(), nEndX = aRect.Right(); nX <= nEndX; nX++ )
382 SetPixel( nY, nX, rFillColor );
384 aRegion.EndEnumRects( aRegHandle );
389 // ------------------------------------------------------------------
391 void BitmapWriteAccess::DrawPolyPolygon( const PolyPolygon& rPolyPoly )
393 if( mpFillColor )
394 FillPolyPolygon( rPolyPoly );
396 if( mpLineColor && ( !mpFillColor || ( *mpFillColor != *mpLineColor ) ) )
398 for( USHORT n = 0, nCount = rPolyPoly.Count(); n < nCount; )
400 const Polygon& rPoly = rPolyPoly[ n++ ];
401 const USHORT nSize = rPoly.GetSize();
403 if( nSize )
405 for( USHORT i = 0, nSize1 = nSize - 1; i < nSize1; i++ )
406 DrawLine( rPoly[ i ], rPoly[ i + 1 ] );
408 if( rPoly[ nSize - 1 ] != rPoly[ 0 ] )
409 DrawLine( rPoly[ nSize - 1 ], rPoly[ 0 ] );