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 :
31 :
32 : #define DECL_FORMAT_GETPIXEL( Format ) \
33 : static BitmapColor GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& rMask );
34 :
35 : #define DECL_FORMAT_SETPIXEL( Format ) \
36 : static void SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask );
37 :
38 : #define DECL_FORMAT( Format ) \
39 : DECL_FORMAT_GETPIXEL( Format ) \
40 : DECL_FORMAT_SETPIXEL( Format )
41 :
42 : #define IMPL_FORMAT_GETPIXEL( Format ) \
43 : BitmapColor BitmapReadAccess::GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& rMask )
44 :
45 : #define IMPL_FORMAT_GETPIXEL_NOMASK( Format ) \
46 : BitmapColor BitmapReadAccess::GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& )
47 :
48 : #define IMPL_FORMAT_SETPIXEL( Format ) \
49 : void BitmapReadAccess::SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask )
50 :
51 : #define IMPL_FORMAT_SETPIXEL_NOMASK( Format ) \
52 : void BitmapReadAccess::SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& )
53 :
54 : #define CASE_FORMAT( Format ) \
55 : case( BMP_FORMAT##Format ): \
56 : { \
57 : mFncGetPixel = GetPixelFor##Format;\
58 : mFncSetPixel = SetPixelFor##Format;\
59 : } \
60 : break;
61 :
62 :
63 :
64 : // - Access functions -
65 :
66 :
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 );
69 :
70 :
71 : // - BitmapReadAccess -
72 :
73 :
74 : class VCL_DLLPUBLIC BitmapReadAccess
75 : {
76 : friend class BitmapWriteAccess;
77 :
78 : private:
79 :
80 : BitmapReadAccess() {}
81 : BitmapReadAccess( const BitmapReadAccess& ) {}
82 : BitmapReadAccess& operator=( const BitmapReadAccess& ) { return *this; }
83 :
84 : protected:
85 : Bitmap maBitmap;
86 : BitmapBuffer* mpBuffer;
87 : Scanline* mpScanBuf;
88 : ColorMask maColorMask;
89 : FncGetPixel mFncGetPixel;
90 : FncSetPixel mFncSetPixel;
91 : bool mbModify;
92 :
93 :
94 : SAL_DLLPRIVATE void ImplCreate( Bitmap& rBitmap );
95 : SAL_DLLPRIVATE void ImplDestroy();
96 : SAL_DLLPRIVATE bool ImplSetAccessPointers( sal_uLong nFormat );
97 :
98 : public:
99 :
100 : SAL_DLLPRIVATE void ImplZeroInitUnusedBits();
101 0 : SAL_DLLPRIVATE BitmapBuffer* ImplGetBitmapBuffer() const { return mpBuffer; }
102 :
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 )
119 : protected:
120 : BitmapReadAccess( Bitmap& rBitmap, bool bModify );
121 :
122 : public:
123 : BitmapReadAccess( Bitmap& rBitmap );
124 : virtual ~BitmapReadAccess();
125 :
126 : inline bool operator!() const;
127 :
128 : inline long Width() const;
129 : inline long Height() const;
130 : inline Point TopLeft() const;
131 : inline Point BottomRight() const;
132 :
133 : inline bool IsTopDown() const;
134 : inline bool IsBottomUp() const;
135 :
136 : inline sal_uLong GetScanlineFormat() const;
137 : inline sal_uLong GetScanlineSize() const;
138 :
139 : inline sal_uInt16 GetBitCount() const;
140 : inline BitmapColor GetBestMatchingColor( const BitmapColor& rBitmapColor );
141 :
142 : inline Scanline GetBuffer() const;
143 : inline Scanline GetScanline( long nY ) const;
144 :
145 : inline 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;
151 :
152 : inline bool HasColorMask() const;
153 : inline ColorMask& GetColorMask() const;
154 :
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;
161 :
162 : /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */
163 : BitmapColor GetInterpolatedColorWithFallback( double fY, double fX, const BitmapColor& rFallback ) const;
164 :
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;
168 : };
169 :
170 :
171 : // - BitmapWriteAccess -
172 :
173 :
174 : class VCL_DLLPUBLIC BitmapWriteAccess : public BitmapReadAccess
175 : {
176 : public:
177 :
178 : BitmapWriteAccess( Bitmap& rBitmap );
179 : virtual ~BitmapWriteAccess();
180 :
181 : void CopyScanline( long nY, const BitmapReadAccess& rReadAcc );
182 : void CopyScanline( long nY, ConstScanline aSrcScanline,
183 : sal_uLong nSrcScanlineFormat, sal_uLong nSrcScanlineSize );
184 :
185 : void CopyBuffer( const BitmapReadAccess& rReadAcc );
186 :
187 : inline void SetPalette( const BitmapPalette& rPalette );
188 : inline void SetPaletteEntryCount( sal_uInt16 nCount );
189 : inline void SetPaletteColor( sal_uInt16 nColor, const BitmapColor& rBitmapColor );
190 :
191 : inline void SetPixel( long nY, long nX, const BitmapColor& rBitmapColor );
192 : inline void SetPixelIndex( long nY, long nX, sal_uInt8 cIndex );
193 :
194 : void SetLineColor( const Color& rColor );
195 :
196 : void SetFillColor( const Color& rColor );
197 :
198 : void Erase( const Color& rColor );
199 :
200 : void DrawLine( const Point& rStart, const Point& rEnd );
201 :
202 : void FillRect( const Rectangle& rRect );
203 : void DrawRect( const Rectangle& rRect );
204 :
205 : private:
206 :
207 : BitmapColor* mpLineColor;
208 : BitmapColor* mpFillColor;
209 :
210 : BitmapWriteAccess() {}
211 : BitmapWriteAccess( const BitmapWriteAccess& ) : BitmapReadAccess() {}
212 : BitmapWriteAccess& operator=( const BitmapWriteAccess& ) { return *this; }
213 : };
214 :
215 :
216 : // - Inlines -
217 :
218 :
219 0 : inline bool BitmapReadAccess::operator!() const
220 : {
221 0 : return( mpBuffer == NULL );
222 : }
223 :
224 :
225 :
226 0 : inline long BitmapReadAccess::Width() const
227 : {
228 0 : return( mpBuffer ? mpBuffer->mnWidth : 0L );
229 : }
230 :
231 :
232 :
233 0 : inline long BitmapReadAccess::Height() const
234 : {
235 0 : return( mpBuffer ? mpBuffer->mnHeight : 0L );
236 : }
237 :
238 :
239 :
240 : inline Point BitmapReadAccess::TopLeft() const
241 : {
242 : return Point();
243 : }
244 :
245 :
246 :
247 : inline Point BitmapReadAccess::BottomRight() const
248 : {
249 : return Point( Width() - 1L, Height() - 1L );
250 : }
251 :
252 :
253 :
254 0 : inline bool BitmapReadAccess::IsTopDown() const
255 : {
256 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
257 0 : return( mpBuffer ? sal::static_int_cast<bool>( BMP_SCANLINE_ADJUSTMENT( mpBuffer->mnFormat ) == BMP_FORMAT_TOP_DOWN ) : false );
258 : }
259 :
260 :
261 :
262 0 : inline bool BitmapReadAccess::IsBottomUp() const
263 : {
264 0 : return !IsTopDown();
265 : }
266 :
267 :
268 :
269 0 : inline sal_uLong BitmapReadAccess::GetScanlineFormat() const
270 : {
271 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
272 0 : return( mpBuffer ? BMP_SCANLINE_FORMAT( mpBuffer->mnFormat ) : 0UL );
273 : }
274 :
275 :
276 :
277 0 : inline sal_uLong BitmapReadAccess::GetScanlineSize() const
278 : {
279 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
280 0 : return( mpBuffer ? mpBuffer->mnScanlineSize : 0UL );
281 : }
282 :
283 :
284 :
285 0 : inline sal_uInt16 BitmapReadAccess::GetBitCount() const
286 : {
287 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
288 0 : return( mpBuffer ? mpBuffer->mnBitCount : 0 );
289 : }
290 :
291 :
292 :
293 0 : inline BitmapColor BitmapReadAccess::GetBestMatchingColor( const BitmapColor& rBitmapColor )
294 : {
295 0 : if( HasPalette() )
296 0 : return BitmapColor( (sal_uInt8) GetBestPaletteIndex( rBitmapColor ) );
297 : else
298 0 : return rBitmapColor;
299 : }
300 :
301 :
302 :
303 0 : inline Scanline BitmapReadAccess::GetBuffer() const
304 : {
305 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
306 0 : return( mpBuffer ? mpBuffer->mpBits : NULL );
307 : }
308 :
309 :
310 :
311 0 : inline Scanline BitmapReadAccess::GetScanline( long nY ) const
312 : {
313 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
314 : DBG_ASSERT( nY < mpBuffer->mnHeight, "y-coordinate out of range!" );
315 0 : return( mpBuffer ? mpScanBuf[ nY ] : NULL );
316 : }
317 :
318 :
319 :
320 0 : inline bool BitmapReadAccess::HasPalette() const
321 : {
322 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
323 0 : return( mpBuffer && !!mpBuffer->maPalette );
324 : }
325 :
326 :
327 :
328 0 : inline const BitmapPalette& BitmapReadAccess::GetPalette() const
329 : {
330 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
331 0 : return mpBuffer->maPalette;
332 : }
333 :
334 :
335 :
336 0 : inline sal_uInt16 BitmapReadAccess::GetPaletteEntryCount() const
337 : {
338 : DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
339 0 : return( HasPalette() ? mpBuffer->maPalette.GetEntryCount() : 0 );
340 : }
341 :
342 :
343 :
344 0 : inline const BitmapColor& BitmapReadAccess::GetPaletteColor( sal_uInt16 nColor ) const
345 : {
346 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
347 : DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
348 0 : return mpBuffer->maPalette[ nColor ];
349 : }
350 :
351 :
352 :
353 0 : inline const BitmapColor& BitmapReadAccess::GetBestPaletteColor( const BitmapColor& rBitmapColor ) const
354 : {
355 0 : return GetPaletteColor( GetBestPaletteIndex( rBitmapColor ) );
356 : }
357 :
358 :
359 :
360 : inline bool BitmapReadAccess::HasColorMask() const
361 : {
362 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
363 : const sal_uLong nFormat = BMP_SCANLINE_FORMAT( mpBuffer->mnFormat );
364 :
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 );
370 : }
371 :
372 :
373 :
374 0 : inline ColorMask& BitmapReadAccess::GetColorMask() const
375 : {
376 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
377 0 : return mpBuffer->maColorMask;
378 : }
379 :
380 :
381 :
382 0 : inline BitmapColor BitmapReadAccess::GetPixel( long nY, long nX ) const
383 : {
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 0 : return mFncGetPixel( mpScanBuf[ nY ], nX, maColorMask );
388 : }
389 :
390 0 : inline sal_uInt8 BitmapReadAccess::GetPixelIndex( long nY, long nX ) const
391 : {
392 0 : return GetPixel( nY, nX ).GetBlueOrIndex();
393 : }
394 :
395 :
396 :
397 0 : inline BitmapColor BitmapReadAccess::GetPixelFromData( const sal_uInt8* pData, long nX ) const
398 : {
399 : DBG_ASSERT( pData, "Access is not valid!" );
400 0 : return mFncGetPixel( pData, nX, maColorMask );
401 : }
402 :
403 :
404 :
405 0 : inline void BitmapReadAccess::SetPixelOnData( sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor )
406 : {
407 : DBG_ASSERT( pData, "Access is not valid!" );
408 0 : mFncSetPixel( pData, nX, rBitmapColor, maColorMask );
409 0 : }
410 :
411 :
412 :
413 0 : inline BitmapColor BitmapReadAccess::GetColor( long nY, long nX ) const
414 : {
415 0 : if( HasPalette() )
416 0 : return mpBuffer->maPalette[ GetPixelIndex( nY, nX ) ];
417 : else
418 0 : return GetPixel( nY, nX );
419 : }
420 :
421 :
422 :
423 0 : inline sal_uInt8 BitmapReadAccess::GetLuminance( long nY, long nX ) const
424 : {
425 0 : return GetColor( nY, nX ).GetLuminance();
426 : }
427 :
428 :
429 :
430 0 : inline void BitmapWriteAccess::SetPalette( const BitmapPalette& rPalette )
431 : {
432 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
433 0 : mpBuffer->maPalette = rPalette;
434 0 : }
435 :
436 :
437 :
438 0 : inline void BitmapWriteAccess::SetPaletteEntryCount( sal_uInt16 nCount )
439 : {
440 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
441 0 : mpBuffer->maPalette.SetEntryCount( nCount );
442 0 : }
443 :
444 :
445 :
446 0 : inline void BitmapWriteAccess::SetPaletteColor( sal_uInt16 nColor, const BitmapColor& rBitmapColor )
447 : {
448 : DBG_ASSERT( mpBuffer, "Access is not valid!" );
449 : DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
450 0 : mpBuffer->maPalette[ nColor ] = rBitmapColor;
451 0 : }
452 :
453 :
454 :
455 0 : inline void BitmapWriteAccess::SetPixel( long nY, long nX, const BitmapColor& rBitmapColor )
456 : {
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 0 : mFncSetPixel( mpScanBuf[ nY ], nX, rBitmapColor, maColorMask );
461 0 : }
462 :
463 0 : inline void BitmapWriteAccess::SetPixelIndex( long nY, long nX, sal_uInt8 cIndex )
464 : {
465 0 : SetPixel( nY, nX, BitmapColor( cIndex ));
466 0 : }
467 :
468 :
469 :
470 : #endif // INCLUDED_VCL_BMPACC_HXX
471 :
472 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|