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 _SV_BMPACC_HXX
21 #define _SV_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;
163 // ---------------------
164 // - BitmapWriteAccess -
165 // ---------------------
167 class VCL_DLLPUBLIC BitmapWriteAccess
: public BitmapReadAccess
171 BitmapWriteAccess( Bitmap
& rBitmap
);
172 virtual ~BitmapWriteAccess();
174 void CopyScanline( long nY
, const BitmapReadAccess
& rReadAcc
);
175 void CopyScanline( long nY
, ConstScanline aSrcScanline
,
176 sal_uLong nSrcScanlineFormat
, sal_uLong nSrcScanlineSize
);
178 void CopyBuffer( const BitmapReadAccess
& rReadAcc
);
180 inline void SetPalette( const BitmapPalette
& rPalette
);
181 inline void SetPaletteEntryCount( sal_uInt16 nCount
);
182 inline void SetPaletteColor( sal_uInt16 nColor
, const BitmapColor
& rBitmapColor
);
184 inline void SetPixel( long nY
, long nX
, const BitmapColor
& rBitmapColor
);
185 inline void SetPixelIndex( long nY
, long nX
, sal_uInt8 cIndex
);
187 void SetLineColor( const Color
& rColor
);
189 void SetFillColor( const Color
& rColor
);
191 void Erase( const Color
& rColor
);
193 void DrawLine( const Point
& rStart
, const Point
& rEnd
);
195 void FillRect( const Rectangle
& rRect
);
196 void DrawRect( const Rectangle
& rRect
);
200 BitmapColor
* mpLineColor
;
201 BitmapColor
* mpFillColor
;
203 BitmapWriteAccess() {}
204 BitmapWriteAccess( const BitmapWriteAccess
& ) : BitmapReadAccess() {}
205 BitmapWriteAccess
& operator=( const BitmapWriteAccess
& ) { return *this; }
212 inline sal_Bool
BitmapReadAccess::operator!() const
214 return( mpBuffer
== NULL
);
217 // ------------------------------------------------------------------
219 inline long BitmapReadAccess::Width() const
221 return( mpBuffer
? mpBuffer
->mnWidth
: 0L );
224 // ------------------------------------------------------------------
226 inline long BitmapReadAccess::Height() const
228 return( mpBuffer
? mpBuffer
->mnHeight
: 0L );
231 // ------------------------------------------------------------------
233 inline Point
BitmapReadAccess::TopLeft() const
238 // ------------------------------------------------------------------
240 inline Point
BitmapReadAccess::BottomRight() const
242 return Point( Width() - 1L, Height() - 1L );
245 // ------------------------------------------------------------------
247 inline sal_Bool
BitmapReadAccess::IsTopDown() const
249 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
250 return( mpBuffer
? sal::static_int_cast
<sal_Bool
>( BMP_SCANLINE_ADJUSTMENT( mpBuffer
->mnFormat
) == BMP_FORMAT_TOP_DOWN
) : sal_False
);
253 // ------------------------------------------------------------------
255 inline sal_Bool
BitmapReadAccess::IsBottomUp() const
260 // ------------------------------------------------------------------
262 inline sal_uLong
BitmapReadAccess::GetScanlineFormat() const
264 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
265 return( mpBuffer
? BMP_SCANLINE_FORMAT( mpBuffer
->mnFormat
) : 0UL );
268 // ------------------------------------------------------------------
270 inline sal_uLong
BitmapReadAccess::GetScanlineSize() const
272 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
273 return( mpBuffer
? mpBuffer
->mnScanlineSize
: 0UL );
276 // ------------------------------------------------------------------
278 inline sal_uInt16
BitmapReadAccess::GetBitCount() const
280 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
281 return( mpBuffer
? mpBuffer
->mnBitCount
: 0 );
284 // ------------------------------------------------------------------
286 inline BitmapColor
BitmapReadAccess::GetBestMatchingColor( const BitmapColor
& rBitmapColor
)
289 return BitmapColor( (sal_uInt8
) GetBestPaletteIndex( rBitmapColor
) );
294 // ------------------------------------------------------------------
296 inline Scanline
BitmapReadAccess::GetBuffer() const
298 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
299 return( mpBuffer
? mpBuffer
->mpBits
: NULL
);
302 // ------------------------------------------------------------------
304 inline Scanline
BitmapReadAccess::GetScanline( long nY
) const
306 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
307 DBG_ASSERT( nY
< mpBuffer
->mnHeight
, "y-coordinate out of range!" );
308 return( mpBuffer
? mpScanBuf
[ nY
] : NULL
);
311 // ------------------------------------------------------------------
313 inline sal_Bool
BitmapReadAccess::HasPalette() const
315 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
316 return( mpBuffer
&& !!mpBuffer
->maPalette
);
319 // ------------------------------------------------------------------
321 inline const BitmapPalette
& BitmapReadAccess::GetPalette() const
323 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
324 return mpBuffer
->maPalette
;
327 // ------------------------------------------------------------------
329 inline sal_uInt16
BitmapReadAccess::GetPaletteEntryCount() const
331 DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
332 return( HasPalette() ? mpBuffer
->maPalette
.GetEntryCount() : 0 );
335 // ------------------------------------------------------------------
337 inline const BitmapColor
& BitmapReadAccess::GetPaletteColor( sal_uInt16 nColor
) const
339 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
340 DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
341 return mpBuffer
->maPalette
[ nColor
];
344 // ------------------------------------------------------------------
346 inline const BitmapColor
& BitmapReadAccess::GetBestPaletteColor( const BitmapColor
& rBitmapColor
) const
348 return GetPaletteColor( GetBestPaletteIndex( rBitmapColor
) );
351 // ------------------------------------------------------------------
353 inline sal_Bool
BitmapReadAccess::HasColorMask() const
355 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
356 const sal_uLong nFormat
= BMP_SCANLINE_FORMAT( mpBuffer
->mnFormat
);
358 return( nFormat
== BMP_FORMAT_8BIT_TC_MASK
||
359 nFormat
== BMP_FORMAT_16BIT_TC_MSB_MASK
||
360 nFormat
== BMP_FORMAT_16BIT_TC_LSB_MASK
||
361 nFormat
== BMP_FORMAT_24BIT_TC_MASK
||
362 nFormat
== BMP_FORMAT_32BIT_TC_MASK
);
365 // ------------------------------------------------------------------
367 inline ColorMask
& BitmapReadAccess::GetColorMask() const
369 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
370 return mpBuffer
->maColorMask
;
373 // ------------------------------------------------------------------
375 inline BitmapColor
BitmapReadAccess::GetPixel( long nY
, long nX
) const
377 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
378 DBG_ASSERT( nX
< mpBuffer
->mnWidth
, "x-coordinate out of range!" );
379 DBG_ASSERT( nY
< mpBuffer
->mnHeight
, "y-coordinate out of range!" );
380 return mFncGetPixel( mpScanBuf
[ nY
], nX
, maColorMask
);
383 inline sal_uInt8
BitmapReadAccess::GetPixelIndex( long nY
, long nX
) const
385 return GetPixel( nY
, nX
).GetBlueOrIndex();
388 // ------------------------------------------------------------------
390 inline BitmapColor
BitmapReadAccess::GetPixelFromData( const sal_uInt8
* pData
, long nX
) const
392 DBG_ASSERT( pData
, "Access is not valid!" );
393 return mFncGetPixel( pData
, nX
, maColorMask
);
396 // ------------------------------------------------------------------
398 inline void BitmapReadAccess::SetPixelOnData( sal_uInt8
* pData
, long nX
, const BitmapColor
& rBitmapColor
)
400 DBG_ASSERT( pData
, "Access is not valid!" );
401 mFncSetPixel( pData
, nX
, rBitmapColor
, maColorMask
);
404 // ------------------------------------------------------------------
406 inline BitmapColor
BitmapReadAccess::GetColor( long nY
, long nX
) const
409 return mpBuffer
->maPalette
[ GetPixelIndex( nY
, nX
) ];
411 return GetPixel( nY
, nX
);
414 // ------------------------------------------------------------------
416 inline sal_uInt8
BitmapReadAccess::GetLuminance( long nY
, long nX
) const
418 return GetColor( nY
, nX
).GetLuminance();
421 // ------------------------------------------------------------------
423 inline void BitmapWriteAccess::SetPalette( const BitmapPalette
& rPalette
)
425 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
426 mpBuffer
->maPalette
= rPalette
;
429 // ------------------------------------------------------------------
431 inline void BitmapWriteAccess::SetPaletteEntryCount( sal_uInt16 nCount
)
433 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
434 mpBuffer
->maPalette
.SetEntryCount( nCount
);
437 // ------------------------------------------------------------------
439 inline void BitmapWriteAccess::SetPaletteColor( sal_uInt16 nColor
, const BitmapColor
& rBitmapColor
)
441 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
442 DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
443 mpBuffer
->maPalette
[ nColor
] = rBitmapColor
;
446 // ------------------------------------------------------------------
448 inline void BitmapWriteAccess::SetPixel( long nY
, long nX
, const BitmapColor
& rBitmapColor
)
450 DBG_ASSERT( mpBuffer
, "Access is not valid!" );
451 DBG_ASSERT( nX
< mpBuffer
->mnWidth
, "x-coordinate out of range!" );
452 DBG_ASSERT( nY
< mpBuffer
->mnHeight
, "y-coordinate out of range!" );
453 mFncSetPixel( mpScanBuf
[ nY
], nX
, rBitmapColor
, maColorMask
);
456 inline void BitmapWriteAccess::SetPixelIndex( long nY
, long nX
, sal_uInt8 cIndex
)
458 SetPixel( nY
, nX
, BitmapColor( cIndex
));
461 // ------------------------------------------------------------------
463 #endif // _SV_BMPACC_HXX
465 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */