1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bmpacc3.cxx,v $
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()
50 // ------------------------------------------------------------------
52 void BitmapWriteAccess::SetLineColor( const Color
& rColor
)
56 if( rColor
.GetTransparency() == 255 )
59 mpLineColor
= ( HasPalette() ? new BitmapColor( (BYTE
) GetBestPaletteIndex( rColor
) ) : new BitmapColor( rColor
) );
62 // ------------------------------------------------------------------
64 Color
BitmapWriteAccess::GetLineColor() const
69 aRet
= (const Color
&) *mpLineColor
;
71 aRet
.SetTransparency( 255 );
76 // ------------------------------------------------------------------
78 void BitmapWriteAccess::SetFillColor()
84 // ------------------------------------------------------------------
86 void BitmapWriteAccess::SetFillColor( const Color
& rColor
)
90 if( rColor
.GetTransparency() == 255 )
93 mpFillColor
= ( HasPalette() ? new BitmapColor( (BYTE
) GetBestPaletteIndex( rColor
) ) : new BitmapColor( rColor
) );
96 // ------------------------------------------------------------------
98 Color
BitmapWriteAccess::GetFillColor() const
103 aRet
= (const Color
&) *mpFillColor
;
105 aRet
.SetTransparency( 255 );
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
;
118 aColor
= BitmapColor( (BYTE
)GetBestPaletteIndex( rColor
) );
119 // try fast bitmap method first
120 if( ImplFastEraseBitmap( *mpBuffer
, aColor
) )
123 // use the canonical method to clear the bitmap
124 BitmapColor
* pOldFillColor
= mpFillColor
? new BitmapColor( *mpFillColor
) : NULL
;
126 const Rectangle
aRect( aPoint
, maBitmap
.GetSizePixel() );
128 SetFillColor( rColor
);
131 mpFillColor
= pOldFillColor
;
134 // ------------------------------------------------------------------
136 void BitmapWriteAccess::DrawLine( const Point
& rStart
, const Point
& rEnd
)
140 const BitmapColor
& rLineColor
= *mpLineColor
;
143 if ( rStart
.X() == rEnd
.X() )
146 const long nEndY
= rEnd
.Y();
153 for (; nY
<= nEndY
; nY
++ )
154 SetPixel( nY
, nX
, rLineColor
);
158 for (; nY
>= nEndY
; nY
-- )
159 SetPixel( nY
, nX
, rLineColor
);
162 else if ( rStart
.Y() == rEnd
.Y() )
165 const long nEndX
= rEnd
.X();
172 for (; nX
<= nEndX
; nX
++ )
173 SetPixel( nY
, nX
, rLineColor
);
177 for (; nX
>= nEndX
; nX
-- )
178 SetPixel( nY
, nX
, rLineColor
);
183 const long nDX
= labs( rEnd
.X() - rStart
.X() );
184 const long nDY
= labs( rEnd
.Y() - rStart
.Y() );
192 if ( rStart
.X() < rEnd
.X() )
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
);
231 if ( rStart
.Y() < rEnd
.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
);
272 // ------------------------------------------------------------------
274 void BitmapWriteAccess::FillRect( const Rectangle
& rRect
)
278 const BitmapColor
& rFillColor
= *mpFillColor
;
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
)
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
);
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
)
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
);
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
)
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();
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 ] );