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 : #ifndef INCLUDED_VCL_BMPACC_HXX
21 : #define INCLUDED_VCL_BMPACC_HXX
22 :
23 : #include <tools/solar.h>
24 : #include <vcl/dllapi.h>
25 : #include <vcl/salbtype.hxx>
26 : #include <vcl/bitmap.hxx>
27 :
28 :
29 : // - Access defines -
30 : #define DECL_FORMAT_GETPIXEL( Format ) \
31 : static BitmapColor GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& rMask );
32 :
33 : #define DECL_FORMAT_SETPIXEL( Format ) \
34 : static void SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask );
35 :
36 : #define DECL_FORMAT( Format ) \
37 : DECL_FORMAT_GETPIXEL( Format ) \
38 : DECL_FORMAT_SETPIXEL( Format )
39 :
40 : #define IMPL_FORMAT_GETPIXEL( Format ) \
41 : BitmapColor BitmapReadAccess::GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& rMask )
42 :
43 : #define IMPL_FORMAT_GETPIXEL_NOMASK( Format ) \
44 : BitmapColor BitmapReadAccess::GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& )
45 :
46 : #define IMPL_FORMAT_SETPIXEL( Format ) \
47 : void BitmapReadAccess::SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask )
48 :
49 : #define IMPL_FORMAT_SETPIXEL_NOMASK( Format ) \
50 : void BitmapReadAccess::SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& )
51 :
52 : #define CASE_FORMAT( Format ) \
53 : case( BMP_FORMAT##Format ): \
54 : { \
55 : mFncGetPixel = GetPixelFor##Format;\
56 : mFncSetPixel = SetPixelFor##Format;\
57 : } \
58 : break;
59 :
60 : // - Access functions -
61 : typedef BitmapColor (*FncGetPixel)( ConstScanline pScanline, long nX, const ColorMask& rMask );
62 : typedef void (*FncSetPixel)( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask );
63 :
64 :
65 : // - BitmapReadAccess -
66 : class VCL_DLLPUBLIC BitmapReadAccess
67 : {
68 : friend class BitmapWriteAccess;
69 :
70 : public:
71 : BitmapReadAccess( Bitmap& rBitmap );
72 : virtual ~BitmapReadAccess();
73 :
74 : inline bool operator!() const;
75 :
76 : inline long Width() const;
77 : inline long Height() const;
78 : inline Point TopLeft() const;
79 : inline Point BottomRight() const;
80 :
81 : inline bool IsTopDown() const;
82 : inline bool IsBottomUp() const;
83 :
84 : inline sal_uLong GetScanlineFormat() const;
85 : inline sal_uLong GetScanlineSize() const;
86 :
87 : inline sal_uInt16 GetBitCount() const;
88 : inline BitmapColor GetBestMatchingColor( const BitmapColor& rBitmapColor );
89 :
90 : inline Scanline GetBuffer() const;
91 : inline Scanline GetScanline( long nY ) const;
92 :
93 : inline bool HasPalette() const;
94 : inline const BitmapPalette& GetPalette() const;
95 : inline sal_uInt16 GetPaletteEntryCount() const;
96 : inline const BitmapColor& GetPaletteColor( sal_uInt16 nColor ) const;
97 : inline const BitmapColor& GetBestPaletteColor( const BitmapColor& rBitmapColor ) const;
98 : sal_uInt16 GetBestPaletteIndex( const BitmapColor& rBitmapColor ) const;
99 :
100 : inline bool HasColorMask() const;
101 : inline ColorMask& GetColorMask() const;
102 :
103 : inline BitmapColor GetPixelFromData( const sal_uInt8* pData, long nX ) const;
104 : inline void SetPixelOnData( sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor );
105 : inline BitmapColor GetPixel( long nY, long nX ) const;
106 : inline BitmapColor GetColor( long nY, long nX ) const;
107 : inline sal_uInt8 GetPixelIndex( long nY, long nX ) const;
108 : inline sal_uInt8 GetLuminance( long nY, long nX ) const;
109 :
110 : /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */
111 : BitmapColor GetInterpolatedColorWithFallback( double fY, double fX, const BitmapColor& rFallback ) const;
112 :
113 : /** Get the color at coordinates fY, fX; if outside, return rFallback. Automatically does the correct
114 : inside/outside checks, e.g. static_cast< sal_uInt32 >(-0.25) *is* 0, not -1 and has to be outside */
115 : BitmapColor GetColorWithFallback( double fY, double fX, const BitmapColor& rFallback ) const;
116 :
117 : private:
118 :
119 : BitmapReadAccess() {}
120 : BitmapReadAccess( const BitmapReadAccess& ) {}
121 : BitmapReadAccess& operator=( const BitmapReadAccess& ) { return *this; }
122 :
123 : protected:
124 : Bitmap maBitmap;
125 : BitmapBuffer* mpBuffer;
126 : Scanline* mpScanBuf;
127 : ColorMask maColorMask;
128 : FncGetPixel mFncGetPixel;
129 : FncSetPixel mFncSetPixel;
130 : bool mbModify;
131 :
132 :
133 : SAL_DLLPRIVATE void ImplCreate( Bitmap& rBitmap );
134 : SAL_DLLPRIVATE void ImplDestroy();
135 : SAL_DLLPRIVATE bool ImplSetAccessPointers( sal_uLong nFormat );
136 :
137 : public:
138 :
139 : SAL_DLLPRIVATE void ImplZeroInitUnusedBits();
140 : SAL_DLLPRIVATE BitmapBuffer*
141 272070 : ImplGetBitmapBuffer() const { return mpBuffer; }
142 :
143 : DECL_FORMAT( _1BIT_MSB_PAL )
144 : DECL_FORMAT( _1BIT_LSB_PAL )
145 : DECL_FORMAT( _4BIT_MSN_PAL )
146 : DECL_FORMAT( _4BIT_LSN_PAL )
147 : DECL_FORMAT( _8BIT_PAL )
148 : DECL_FORMAT( _8BIT_TC_MASK )
149 : DECL_FORMAT( _16BIT_TC_MSB_MASK )
150 : DECL_FORMAT( _16BIT_TC_LSB_MASK )
151 : DECL_FORMAT( _24BIT_TC_BGR )
152 : DECL_FORMAT( _24BIT_TC_RGB )
153 : DECL_FORMAT( _24BIT_TC_MASK )
154 : DECL_FORMAT( _32BIT_TC_ABGR )
155 : DECL_FORMAT( _32BIT_TC_ARGB )
156 : DECL_FORMAT( _32BIT_TC_BGRA )
157 : DECL_FORMAT( _32BIT_TC_RGBA )
158 : DECL_FORMAT( _32BIT_TC_MASK )
159 :
160 : protected:
161 : BitmapReadAccess( Bitmap& rBitmap, bool bModify );
162 :
163 : };
164 :
165 : // - BitmapWriteAccess -
166 : class VCL_DLLPUBLIC BitmapWriteAccess : public BitmapReadAccess
167 : {
168 : public:
169 :
170 : BitmapWriteAccess( Bitmap& rBitmap );
171 : virtual ~BitmapWriteAccess();
172 :
173 : void CopyScanline( long nY, const BitmapReadAccess& rReadAcc );
174 : void CopyScanline( long nY, ConstScanline aSrcScanline,
175 : sal_uLong nSrcScanlineFormat, sal_uLong nSrcScanlineSize );
176 :
177 : void CopyBuffer( const BitmapReadAccess& rReadAcc );
178 :
179 : inline void SetPalette( const BitmapPalette& rPalette );
180 : inline void SetPaletteEntryCount( sal_uInt16 nCount );
181 : inline void SetPaletteColor( sal_uInt16 nColor, const BitmapColor& rBitmapColor );
182 :
183 : inline void SetPixel( long nY, long nX, const BitmapColor& rBitmapColor );
184 : inline void SetPixelIndex( long nY, long nX, sal_uInt8 cIndex );
185 :
186 : void SetLineColor( const Color& rColor );
187 :
188 : void SetFillColor();
189 : void SetFillColor( const Color& rColor );
190 :
191 : void Erase( const Color& rColor );
192 :
193 : void DrawLine( const Point& rStart, const Point& rEnd );
194 :
195 : void FillRect( const Rectangle& rRect );
196 : void DrawRect( const Rectangle& rRect );
197 :
198 : private:
199 :
200 : BitmapColor* mpLineColor;
201 : BitmapColor* mpFillColor;
202 :
203 : BitmapWriteAccess() {}
204 : BitmapWriteAccess( const BitmapWriteAccess& ) : BitmapReadAccess() {}
205 : BitmapWriteAccess& operator=( const BitmapWriteAccess& ) { return *this; }
206 : };
207 :
208 : // - Inlines -
209 1168820 : inline bool BitmapReadAccess::operator!() const
210 : {
211 1168820 : return( mpBuffer == NULL );
212 : }
213 :
214 562156 : inline long BitmapReadAccess::Width() const
215 : {
216 562156 : return( mpBuffer ? mpBuffer->mnWidth : 0L );
217 : }
218 :
219 400702 : inline long BitmapReadAccess::Height() const
220 : {
221 400702 : return( mpBuffer ? mpBuffer->mnHeight : 0L );
222 : }
223 :
224 : inline Point BitmapReadAccess::TopLeft() const
225 : {
226 : return Point();
227 : }
228 :
229 : inline Point BitmapReadAccess::BottomRight() const
230 : {
231 : return Point( Width() - 1L, Height() - 1L );
232 : }
233 :
234 2148 : inline bool BitmapReadAccess::IsTopDown() const
235 : {
236 : assert(mpBuffer && "Access is not valid!");
237 2148 : return mpBuffer && ( BMP_SCANLINE_ADJUSTMENT( mpBuffer->mnFormat ) == BMP_FORMAT_TOP_DOWN );
238 : }
239 :
240 2146 : inline bool BitmapReadAccess::IsBottomUp() const
241 : {
242 2146 : return !IsTopDown();
243 : }
244 :
245 883206 : inline sal_uLong BitmapReadAccess::GetScanlineFormat() const
246 : {
247 : assert(mpBuffer && "Access is not valid!");
248 883206 : return( mpBuffer ? BMP_SCANLINE_FORMAT( mpBuffer->mnFormat ) : 0UL );
249 : }
250 :
251 735158 : inline sal_uLong BitmapReadAccess::GetScanlineSize() const
252 : {
253 : assert(mpBuffer && "Access is not valid!");
254 735158 : return( mpBuffer ? mpBuffer->mnScanlineSize : 0UL );
255 : }
256 :
257 247875 : inline sal_uInt16 BitmapReadAccess::GetBitCount() const
258 : {
259 : assert(mpBuffer && "Access is not valid!");
260 247875 : return( mpBuffer ? mpBuffer->mnBitCount : 0 );
261 : }
262 :
263 16422 : inline BitmapColor BitmapReadAccess::GetBestMatchingColor( const BitmapColor& rBitmapColor )
264 : {
265 16422 : if( HasPalette() )
266 15530 : return BitmapColor( (sal_uInt8) GetBestPaletteIndex( rBitmapColor ) );
267 : else
268 892 : return rBitmapColor;
269 : }
270 :
271 50264 : inline Scanline BitmapReadAccess::GetBuffer() const
272 : {
273 : assert(mpBuffer && "Access is not valid!");
274 50264 : return( mpBuffer ? mpBuffer->mpBits : NULL );
275 : }
276 :
277 39771367 : inline Scanline BitmapReadAccess::GetScanline( long nY ) const
278 : {
279 : assert(mpBuffer && "Access is not valid!");
280 : assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
281 39771367 : return( mpBuffer ? mpScanBuf[ nY ] : NULL );
282 : }
283 :
284 850206208 : inline bool BitmapReadAccess::HasPalette() const
285 : {
286 : assert(mpBuffer && "Access is not valid!");
287 850206208 : return( mpBuffer && !!mpBuffer->maPalette );
288 : }
289 :
290 274859 : inline const BitmapPalette& BitmapReadAccess::GetPalette() const
291 : {
292 : assert(mpBuffer && "Access is not valid!");
293 274859 : return mpBuffer->maPalette;
294 : }
295 :
296 279721 : inline sal_uInt16 BitmapReadAccess::GetPaletteEntryCount() const
297 : {
298 : assert(HasPalette() && "Bitmap has no palette!");
299 279721 : return( HasPalette() ? mpBuffer->maPalette.GetEntryCount() : 0 );
300 : }
301 :
302 373949170 : inline const BitmapColor& BitmapReadAccess::GetPaletteColor( sal_uInt16 nColor ) const
303 : {
304 : assert(mpBuffer && "Access is not valid!");
305 : assert(HasPalette() && "Bitmap has no palette!");
306 373949170 : return mpBuffer->maPalette[ nColor ];
307 : }
308 :
309 0 : inline const BitmapColor& BitmapReadAccess::GetBestPaletteColor( const BitmapColor& rBitmapColor ) const
310 : {
311 0 : return GetPaletteColor( GetBestPaletteIndex( rBitmapColor ) );
312 : }
313 :
314 : inline bool BitmapReadAccess::HasColorMask() const
315 : {
316 : assert(mpBuffer && "Access is not valid!");
317 : const sal_uLong nFormat = BMP_SCANLINE_FORMAT( mpBuffer->mnFormat );
318 :
319 : return( nFormat == BMP_FORMAT_8BIT_TC_MASK ||
320 : nFormat == BMP_FORMAT_16BIT_TC_MSB_MASK ||
321 : nFormat == BMP_FORMAT_16BIT_TC_LSB_MASK ||
322 : nFormat == BMP_FORMAT_24BIT_TC_MASK ||
323 : nFormat == BMP_FORMAT_32BIT_TC_MASK );
324 : }
325 :
326 6876 : inline ColorMask& BitmapReadAccess::GetColorMask() const
327 : {
328 : assert(mpBuffer && "Access is not valid!");
329 6876 : return mpBuffer->maColorMask;
330 : }
331 :
332 1044427507 : inline BitmapColor BitmapReadAccess::GetPixel( long nY, long nX ) const
333 : {
334 : assert(mpBuffer && "Access is not valid!");
335 : assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!");
336 : assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
337 1044427507 : return mFncGetPixel( mpScanBuf[ nY ], nX, maColorMask );
338 : }
339 :
340 356248764 : inline sal_uInt8 BitmapReadAccess::GetPixelIndex( long nY, long nX ) const
341 : {
342 356248764 : return GetPixel( nY, nX ).GetBlueOrIndex();
343 : }
344 :
345 8800 : inline BitmapColor BitmapReadAccess::GetPixelFromData( const sal_uInt8* pData, long nX ) const
346 : {
347 : assert(pData && "Access is not valid!");
348 8800 : return mFncGetPixel( pData, nX, maColorMask );
349 : }
350 :
351 16 : inline void BitmapReadAccess::SetPixelOnData( sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor )
352 : {
353 : assert(pData && "Access is not valid!");
354 16 : mFncSetPixel( pData, nX, rBitmapColor, maColorMask );
355 16 : }
356 :
357 8083652 : inline BitmapColor BitmapReadAccess::GetColor( long nY, long nX ) const
358 : {
359 8083652 : if( HasPalette() )
360 3721813 : return mpBuffer->maPalette[ GetPixelIndex( nY, nX ) ];
361 : else
362 4361839 : return GetPixel( nY, nX );
363 : }
364 :
365 2030843 : inline sal_uInt8 BitmapReadAccess::GetLuminance( long nY, long nX ) const
366 : {
367 2030843 : return GetColor( nY, nX ).GetLuminance();
368 : }
369 :
370 582 : inline void BitmapWriteAccess::SetPalette( const BitmapPalette& rPalette )
371 : {
372 : assert(mpBuffer && "Access is not valid!");
373 582 : mpBuffer->maPalette = rPalette;
374 582 : }
375 :
376 17649 : inline void BitmapWriteAccess::SetPaletteEntryCount( sal_uInt16 nCount )
377 : {
378 : assert(mpBuffer && "Access is not valid!");
379 17649 : mpBuffer->maPalette.SetEntryCount( nCount );
380 17649 : }
381 :
382 3675156 : inline void BitmapWriteAccess::SetPaletteColor( sal_uInt16 nColor, const BitmapColor& rBitmapColor )
383 : {
384 : assert(mpBuffer && "Access is not valid!");
385 : assert(HasPalette() && "Bitmap has no palette!");
386 3675156 : mpBuffer->maPalette[ nColor ] = rBitmapColor;
387 3675156 : }
388 :
389 359086577 : inline void BitmapWriteAccess::SetPixel( long nY, long nX, const BitmapColor& rBitmapColor )
390 : {
391 : assert(mpBuffer && "Access is not valid!");
392 : assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!");
393 : assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
394 359086577 : mFncSetPixel( mpScanBuf[ nY ], nX, rBitmapColor, maColorMask );
395 359086577 : }
396 :
397 82744730 : inline void BitmapWriteAccess::SetPixelIndex( long nY, long nX, sal_uInt8 cIndex )
398 : {
399 82744730 : SetPixel( nY, nX, BitmapColor( cIndex ));
400 82744730 : }
401 :
402 : #endif // INCLUDED_VCL_BMPACC_HXX
403 :
404 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|