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 #ifndef INCLUDED_VCL_BMPACC_HXX
21 #define INCLUDED_VCL_BMPACC_HXX
23 #include <tools/solar.h>
24 #include <vcl/dllapi.h>
25 #include <vcl/salbtype.hxx>
26 #include <vcl/bitmap.hxx>
28 // --------------------
30 // --------------------
32 #define DECL_FORMAT_GETPIXEL( Format ) \
33 static BitmapColor GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& rMask );
35 #define DECL_FORMAT_SETPIXEL( Format ) \
36 static void SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask );
38 #define DECL_FORMAT( Format ) \
39 DECL_FORMAT_GETPIXEL( Format ) \
40 DECL_FORMAT_SETPIXEL( Format )
42 #define IMPL_FORMAT_GETPIXEL( Format ) \
43 BitmapColor BitmapReadAccess::GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& rMask )
45 #define IMPL_FORMAT_GETPIXEL_NOMASK( Format ) \
46 BitmapColor BitmapReadAccess::GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& )
48 #define IMPL_FORMAT_SETPIXEL( Format ) \
49 void BitmapReadAccess::SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask )
51 #define IMPL_FORMAT_SETPIXEL_NOMASK( Format ) \
52 void BitmapReadAccess::SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& )
54 #define CASE_FORMAT( Format ) \
55 case( BMP_FORMAT##Format ): \
57 mFncGetPixel = GetPixelFor##Format;\
58 mFncSetPixel = SetPixelFor##Format;\
63 // --------------------
64 // - Access functions -
65 // --------------------
67 typedef BitmapColor (*FncGetPixel
)( ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
68 typedef void (*FncSetPixel
)( Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
70 // --------------------
71 // - BitmapReadAccess -
72 // --------------------
74 class VCL_DLLPUBLIC BitmapReadAccess
76 friend class BitmapWriteAccess
;
81 BitmapReadAccess( const BitmapReadAccess
& ) {}
82 BitmapReadAccess
& operator=( const BitmapReadAccess
& ) { return *this; }
86 BitmapBuffer
* mpBuffer
;
88 ColorMask maColorMask
;
89 FncGetPixel mFncGetPixel
;
90 FncSetPixel mFncSetPixel
;
94 SAL_DLLPRIVATE
void ImplCreate( Bitmap
& rBitmap
);
95 SAL_DLLPRIVATE
void ImplDestroy();
96 SAL_DLLPRIVATE sal_Bool
ImplSetAccessPointers( sal_uLong nFormat
);
100 SAL_DLLPRIVATE
void ImplZeroInitUnusedBits();
101 SAL_DLLPRIVATE BitmapBuffer
* ImplGetBitmapBuffer() const { return mpBuffer
; }
103 DECL_FORMAT( _1BIT_MSB_PAL
)
104 DECL_FORMAT( _1BIT_LSB_PAL
)
105 DECL_FORMAT( _4BIT_MSN_PAL
)
106 DECL_FORMAT( _4BIT_LSN_PAL
)
107 DECL_FORMAT( _8BIT_PAL
)
108 DECL_FORMAT( _8BIT_TC_MASK
)
109 DECL_FORMAT( _16BIT_TC_MSB_MASK
)
110 DECL_FORMAT( _16BIT_TC_LSB_MASK
)
111 DECL_FORMAT( _24BIT_TC_BGR
)
112 DECL_FORMAT( _24BIT_TC_RGB
)
113 DECL_FORMAT( _24BIT_TC_MASK
)
114 DECL_FORMAT( _32BIT_TC_ABGR
)
115 DECL_FORMAT( _32BIT_TC_ARGB
)
116 DECL_FORMAT( _32BIT_TC_BGRA
)
117 DECL_FORMAT( _32BIT_TC_RGBA
)
118 DECL_FORMAT( _32BIT_TC_MASK
)
120 BitmapReadAccess( Bitmap
& rBitmap
, sal_Bool bModify
);
123 BitmapReadAccess( Bitmap
& rBitmap
);
124 virtual ~BitmapReadAccess();
126 inline sal_Bool
operator!() const;
128 inline long Width() const;
129 inline long Height() const;
130 inline Point
TopLeft() const;
131 inline Point
BottomRight() const;
133 inline sal_Bool
IsTopDown() const;
134 inline sal_Bool
IsBottomUp() const;
136 inline sal_uLong
GetScanlineFormat() const;
137 inline sal_uLong
GetScanlineSize() const;
139 inline sal_uInt16
GetBitCount() const;
140 inline BitmapColor
GetBestMatchingColor( const BitmapColor
& rBitmapColor
);
142 inline Scanline
GetBuffer() const;
143 inline Scanline
GetScanline( long nY
) const;
145 inline sal_Bool
HasPalette() const;
146 inline const BitmapPalette
& GetPalette() const;
147 inline sal_uInt16
GetPaletteEntryCount() const;
148 inline const BitmapColor
& GetPaletteColor( sal_uInt16 nColor
) const;
149 inline const BitmapColor
& GetBestPaletteColor( const BitmapColor
& rBitmapColor
) const;
150 sal_uInt16
GetBestPaletteIndex( const BitmapColor
& rBitmapColor
) const;
152 inline sal_Bool
HasColorMask() const;
153 inline ColorMask
& GetColorMask() const;
155 inline BitmapColor
GetPixelFromData( const sal_uInt8
* pData
, long nX
) const;
156 inline void SetPixelOnData( sal_uInt8
* pData
, long nX
, const BitmapColor
& rBitmapColor
);
157 inline BitmapColor
GetPixel( long nY
, long nX
) const;
158 inline BitmapColor
GetColor( long nY
, long nX
) const;
159 inline sal_uInt8
GetPixelIndex( long nY
, long nX
) const;
160 inline sal_uInt8
GetLuminance( long nY
, long nX
) const;
162 /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */
163 BitmapColor
GetInterpolatedColorWithFallback( double fY
, double fX
, const BitmapColor
& rFallback
) const;
165 /** Get the color at coordinates fY, fX; if outside, return rFallback. Automatically does the correct
166 inside/outside checks, e.g. static_cast< sal_uInt32 >(-0.25) *is* 0, not -1 and has to be outside */
167 BitmapColor
GetColorWithFallback( double fY
, double fX
, const BitmapColor
& rFallback
) const;
170 // ---------------------
171 // - BitmapWriteAccess -
172 // ---------------------
174 class VCL_DLLPUBLIC BitmapWriteAccess
: public BitmapReadAccess
178 BitmapWriteAccess( Bitmap
& rBitmap
);
179 virtual ~BitmapWriteAccess();
181 void CopyScanline( long nY
, const BitmapReadAccess
& rReadAcc
);
182 void CopyScanline( long nY
, ConstScanline aSrcScanline
,
183 sal_uLong nSrcScanlineFormat
, sal_uLong nSrcScanlineSize
);
185 void CopyBuffer( const BitmapReadAccess
& rReadAcc
);
187 inline void SetPalette( const BitmapPalette
& rPalette
);
188 inline void SetPaletteEntryCount( sal_uInt16 nCount
);
189 inline void SetPaletteColor( sal_uInt16 nColor
, const BitmapColor
& rBitmapColor
);
191 inline void SetPixel( long nY
, long nX
, const BitmapColor
& rBitmapColor
);
192 inline void SetPixelIndex( long nY
, long nX
, sal_uInt8 cIndex
);
194 void SetLineColor( const Color
& rColor
);
196 void SetFillColor( const Color
& rColor
);
198 void Erase( const Color
& rColor
);
200 void DrawLine( const Point
& rStart
, const Point
& rEnd
);
202 void FillRect( const Rectangle
& rRect
);
203 void DrawRect( const Rectangle
& rRect
);
207 BitmapColor
* mpLineColor
;
208 BitmapColor
* mpFillColor
;
210 BitmapWriteAccess() {}
211 BitmapWriteAccess( const BitmapWriteAccess
& ) : BitmapReadAccess() {}
212 BitmapWriteAccess
& operator=( const BitmapWriteAccess
& ) { return *this; }
219 inline sal_Bool
BitmapReadAccess::operator!() const
221 return( mpBuffer
== NULL
);
224 // ------------------------------------------------------------------
226 inline long BitmapReadAccess::Width() const
228 return( mpBuffer
? mpBuffer
->mnWidth
: 0L );
231 // ------------------------------------------------------------------
233 inline long BitmapReadAccess::Height() const
235 return( mpBuffer
? mpBuffer
->mnHeight
: 0L );
238 // ------------------------------------------------------------------
240 inline Point
BitmapReadAccess::TopLeft() const
245 // ------------------------------------------------------------------
247 inline Point
BitmapReadAccess::BottomRight() const
249 return Point( Width() - 1L, Height() - 1L );
252 // ------------------------------------------------------------------
254 inline sal_Bool
BitmapReadAccess::IsTopDown() const
256 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
257 return( mpBuffer
? sal::static_int_cast
<sal_Bool
>( BMP_SCANLINE_ADJUSTMENT( mpBuffer
->mnFormat
) == BMP_FORMAT_TOP_DOWN
) : sal_False
);
260 // ------------------------------------------------------------------
262 inline sal_Bool
BitmapReadAccess::IsBottomUp() const
267 // ------------------------------------------------------------------
269 inline sal_uLong
BitmapReadAccess::GetScanlineFormat() const
271 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
272 return( mpBuffer
? BMP_SCANLINE_FORMAT( mpBuffer
->mnFormat
) : 0UL );
275 // ------------------------------------------------------------------
277 inline sal_uLong
BitmapReadAccess::GetScanlineSize() const
279 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
280 return( mpBuffer
? mpBuffer
->mnScanlineSize
: 0UL );
283 // ------------------------------------------------------------------
285 inline sal_uInt16
BitmapReadAccess::GetBitCount() const
287 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
288 return( mpBuffer
? mpBuffer
->mnBitCount
: 0 );
291 // ------------------------------------------------------------------
293 inline BitmapColor
BitmapReadAccess::GetBestMatchingColor( const BitmapColor
& rBitmapColor
)
296 return BitmapColor( (sal_uInt8
) GetBestPaletteIndex( rBitmapColor
) );
301 // ------------------------------------------------------------------
303 inline Scanline
BitmapReadAccess::GetBuffer() const
305 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
306 return( mpBuffer
? mpBuffer
->mpBits
: NULL
);
309 // ------------------------------------------------------------------
311 inline Scanline
BitmapReadAccess::GetScanline( long nY
) const
313 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
314 DBG_ASSERT( nY
< mpBuffer
->mnHeight
, "y-coordinate out of range!" );
315 return( mpBuffer
? mpScanBuf
[ nY
] : NULL
);
318 // ------------------------------------------------------------------
320 inline sal_Bool
BitmapReadAccess::HasPalette() const
322 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
323 return( mpBuffer
&& !!mpBuffer
->maPalette
);
326 // ------------------------------------------------------------------
328 inline const BitmapPalette
& BitmapReadAccess::GetPalette() const
330 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
331 return mpBuffer
->maPalette
;
334 // ------------------------------------------------------------------
336 inline sal_uInt16
BitmapReadAccess::GetPaletteEntryCount() const
338 DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
339 return( HasPalette() ? mpBuffer
->maPalette
.GetEntryCount() : 0 );
342 // ------------------------------------------------------------------
344 inline const BitmapColor
& BitmapReadAccess::GetPaletteColor( sal_uInt16 nColor
) const
346 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
347 DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
348 return mpBuffer
->maPalette
[ nColor
];
351 // ------------------------------------------------------------------
353 inline const BitmapColor
& BitmapReadAccess::GetBestPaletteColor( const BitmapColor
& rBitmapColor
) const
355 return GetPaletteColor( GetBestPaletteIndex( rBitmapColor
) );
358 // ------------------------------------------------------------------
360 inline sal_Bool
BitmapReadAccess::HasColorMask() const
362 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
363 const sal_uLong nFormat
= BMP_SCANLINE_FORMAT( mpBuffer
->mnFormat
);
365 return( nFormat
== BMP_FORMAT_8BIT_TC_MASK
||
366 nFormat
== BMP_FORMAT_16BIT_TC_MSB_MASK
||
367 nFormat
== BMP_FORMAT_16BIT_TC_LSB_MASK
||
368 nFormat
== BMP_FORMAT_24BIT_TC_MASK
||
369 nFormat
== BMP_FORMAT_32BIT_TC_MASK
);
372 // ------------------------------------------------------------------
374 inline ColorMask
& BitmapReadAccess::GetColorMask() const
376 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
377 return mpBuffer
->maColorMask
;
380 // ------------------------------------------------------------------
382 inline BitmapColor
BitmapReadAccess::GetPixel( long nY
, long nX
) const
384 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
385 DBG_ASSERT( nX
< mpBuffer
->mnWidth
, "x-coordinate out of range!" );
386 DBG_ASSERT( nY
< mpBuffer
->mnHeight
, "y-coordinate out of range!" );
387 return mFncGetPixel( mpScanBuf
[ nY
], nX
, maColorMask
);
390 inline sal_uInt8
BitmapReadAccess::GetPixelIndex( long nY
, long nX
) const
392 return GetPixel( nY
, nX
).GetBlueOrIndex();
395 // ------------------------------------------------------------------
397 inline BitmapColor
BitmapReadAccess::GetPixelFromData( const sal_uInt8
* pData
, long nX
) const
399 DBG_ASSERT( pData
, "Access is not valid!" );
400 return mFncGetPixel( pData
, nX
, maColorMask
);
403 // ------------------------------------------------------------------
405 inline void BitmapReadAccess::SetPixelOnData( sal_uInt8
* pData
, long nX
, const BitmapColor
& rBitmapColor
)
407 DBG_ASSERT( pData
, "Access is not valid!" );
408 mFncSetPixel( pData
, nX
, rBitmapColor
, maColorMask
);
411 // ------------------------------------------------------------------
413 inline BitmapColor
BitmapReadAccess::GetColor( long nY
, long nX
) const
416 return mpBuffer
->maPalette
[ GetPixelIndex( nY
, nX
) ];
418 return GetPixel( nY
, nX
);
421 // ------------------------------------------------------------------
423 inline sal_uInt8
BitmapReadAccess::GetLuminance( long nY
, long nX
) const
425 return GetColor( nY
, nX
).GetLuminance();
428 // ------------------------------------------------------------------
430 inline void BitmapWriteAccess::SetPalette( const BitmapPalette
& rPalette
)
432 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
433 mpBuffer
->maPalette
= rPalette
;
436 // ------------------------------------------------------------------
438 inline void BitmapWriteAccess::SetPaletteEntryCount( sal_uInt16 nCount
)
440 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
441 mpBuffer
->maPalette
.SetEntryCount( nCount
);
444 // ------------------------------------------------------------------
446 inline void BitmapWriteAccess::SetPaletteColor( sal_uInt16 nColor
, const BitmapColor
& rBitmapColor
)
448 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
449 DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
450 mpBuffer
->maPalette
[ nColor
] = rBitmapColor
;
453 // ------------------------------------------------------------------
455 inline void BitmapWriteAccess::SetPixel( long nY
, long nX
, const BitmapColor
& rBitmapColor
)
457 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
458 DBG_ASSERT( nX
< mpBuffer
->mnWidth
, "x-coordinate out of range!" );
459 DBG_ASSERT( nY
< mpBuffer
->mnHeight
, "y-coordinate out of range!" );
460 mFncSetPixel( mpScanBuf
[ nY
], nX
, rBitmapColor
, maColorMask
);
463 inline void BitmapWriteAccess::SetPixelIndex( long nY
, long nX
, sal_uInt8 cIndex
)
465 SetPixel( nY
, nX
, BitmapColor( cIndex
));
468 // ------------------------------------------------------------------
470 #endif // INCLUDED_VCL_BMPACC_HXX
472 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */