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_BITMAP_HXX
21 #define INCLUDED_VCL_BITMAP_HXX
23 #include <tools/solar.h>
24 #include <tools/degree.hxx>
25 #include <vcl/checksum.hxx>
26 #include <vcl/dllapi.h>
27 #include <vcl/mapmod.hxx>
28 #include <vcl/region.hxx>
29 #include <vcl/scopedbitmapaccess.hxx>
30 #include <o3tl/typed_flags_set.hxx>
35 template <typename Arg
, typename Ret
> class Link
;
37 enum class BmpMirrorFlags
46 template<> struct typed_flags
<BmpMirrorFlags
> : is_typed_flags
<BmpMirrorFlags
, 0x03> {};
49 enum class BmpScaleFlag
51 // Try to preferably use these.
55 // Specific algorithms, use only if you really need to (mainly used for tests)
57 Interpolate
, // fast, integer bilinear
61 Super
// bilinear interpolation when supersampling and averaging when subsampling under certain scale
64 #define BMP_COL_TRANS Color( 252, 3, 251 )
66 enum class BmpConversion
77 N8BitNoConversion
// make 8bit without color conversion (e.g. take the red channel)
85 class BitmapInfoAccess
;
86 class BitmapReadAccess
;
87 class BitmapWriteAccess
;
94 struct BitmapSystemData
97 void* pDIB
; // device independent byte buffer
98 #elif defined( MACOSX ) || defined( IOS )
99 // Nothing needed, apparently
107 class SAL_WARN_UNUSED VCL_DLLPUBLIC Bitmap
112 Bitmap( const Bitmap
& rBitmap
);
113 Bitmap( const Size
& rSizePixel
, sal_uInt16 nBitCount
, const BitmapPalette
* pPal
= nullptr );
114 explicit Bitmap( std::shared_ptr
<SalBitmap
> const & xSalBitmap
);
117 Bitmap
& operator=( const Bitmap
& rBitmap
);
118 Bitmap
& operator=( Bitmap
&& rBitmap
) noexcept
;
119 inline bool operator!() const;
120 bool operator==( const Bitmap
& rBitmap
) const;
121 bool operator!=( const Bitmap
& rBitmap
) const { return !operator==(rBitmap
); }
123 inline bool IsEmpty() const;
126 inline const MapMode
& GetPrefMapMode() const;
127 inline void SetPrefMapMode( const MapMode
& rMapMode
);
129 inline const Size
& GetPrefSize() const;
130 inline void SetPrefSize( const Size
& rSize
);
132 Size
GetSizePixel() const;
134 sal_uInt16
GetBitCount() const;
135 inline sal_Int64
GetColorCount() const;
136 inline sal_uLong
GetSizeBytes() const;
137 bool HasGreyPalette8Bit() const;
138 bool HasGreyPaletteAny() const;
139 /** get system dependent bitmap data
142 The system dependent BitmapSystemData structure to be filled
144 @return true if the bitmap has a valid system object (e.g. not empty)
146 bool GetSystemData( BitmapSystemData
& rData
) const;
148 BitmapChecksum
GetChecksum() const;
150 Bitmap
CreateDisplayBitmap( OutputDevice
* pDisplay
) const;
152 static const BitmapPalette
&
153 GetGreyPalette( int nEntries
);
157 /** Convert bitmap format
160 The format this bitmap should be converted to.
162 @return true the conversion was completed successfully.
164 bool Convert( BmpConversion eConversion
);
166 /** Apply a Floyd dither algorithm to the bitmap
168 This method dithers the bitmap inplace, i.e. a true color
169 bitmap is converted to a paletted bitmap, reducing the color
170 deviation by error diffusion.
178 A rectangle specifying the crop amounts on all four sides of
179 the bitmap. If the upper left corner of the bitmap is assigned
180 (0,0), then this method cuts out the given rectangle from the
181 bitmap. Note that the rectangle is clipped to the bitmap's
182 dimension, i.e. negative left,top rectangle coordinates or
183 exceeding width or height is ignored.
185 @return true cropping was performed successfully. If
186 nothing had to be cropped, because e.g. the crop rectangle
187 included the bitmap, false is returned, too!
189 bool Crop( const tools::Rectangle
& rRectPixel
);
191 /** Expand the bitmap by pixel padding
194 Number of pixel to pad at the right border of the bitmap
197 Number of scanlines to pad at the bottom border of the bitmap
200 Color to use for padded pixel
202 @return true, if padding was performed successfully. false is
203 not only returned when the operation failed, but also if
204 nothing had to be done, e.g. because nDX and nDY were zero.
207 sal_uLong nDX
, sal_uLong nDY
,
208 const Color
* pInitColor
= nullptr );
210 /** Copy a rectangular area from another bitmap
213 Destination rectangle in this bitmap. This is clipped to the
217 Source rectangle in pBmpSrc. This is clipped to the source
218 bitmap dimensions. Note further that no scaling takes place
219 during this copy operation, i.e. only the minimum of source
220 and destination rectangle's width and height are used.
223 The source bitmap to copy from. If this argument is NULL, or
224 equal to the object this method is called on, copying takes
225 place within the same bitmap.
227 @return true, if the operation completed successfully. false
228 is not only returned when the operation failed, but also if
229 nothing had to be done, e.g. because one of the rectangles are
233 const tools::Rectangle
& rRectDst
,
234 const tools::Rectangle
& rRectSrc
,
235 const Bitmap
* pBmpSrc
= nullptr );
237 bool CopyPixel_AlphaOptimized(
238 const tools::Rectangle
& rRectDst
,
239 const tools::Rectangle
& rRectSrc
,
240 const Bitmap
* pBmpSrc
);
242 /** Perform boolean operations with another bitmap
245 The mask bitmap in the selected combine operation
248 The combine operation to perform on the bitmap
250 @return true, if the operation was completed successfully.
254 BmpCombine eCombine
);
256 /** Alpha-blend the given bitmap against a specified uniform
259 @attention This method might convert paletted bitmaps to
260 truecolor, to be able to represent every necessary color. Note
261 that during alpha blending, lots of colors not originally
262 included in the bitmap can be generated.
265 Alpha mask to blend with
267 @param rBackgroundColor
268 Background color to use for every pixel during alpha blending
270 @return true, if blending was successful, false otherwise
273 const AlphaMask
& rAlpha
,
274 const Color
& rBackgroundColor
);
276 /** Fill the entire bitmap with the given color
279 Color value to use for filling
281 @return true, if the operation was completed successfully.
283 bool Erase( const Color
& rFillColor
);
285 /** Perform the Invert operation on every pixel
287 @return true, if the operation was completed successfully.
291 /** Mirror the bitmap
294 About which axis (horizontal, vertical, or both) to mirror
296 @return true, if the operation was completed successfully.
298 bool Mirror( BmpMirrorFlags nMirrorFlags
);
303 The resulting size of the scaled bitmap
306 The algorithm to be used for scaling
308 @return true, if the operation was completed successfully.
310 bool Scale( const Size
& rNewSize
, BmpScaleFlag nScaleFlag
= BmpScaleFlag::Default
);
315 The scale factor in x direction.
318 The scale factor in y direction.
321 Method of scaling - it is recommended that either BmpScaleFlag::Default or BmpScaleFlag::BestQuality be used.
323 @return true, if the operation was completed successfully.
325 bool Scale( const double& rScaleX
, const double& rScaleY
, BmpScaleFlag nScaleFlag
= BmpScaleFlag::Default
);
328 Returns true if bitmap scaling is considered to be fast.
330 Currently this returns true if OpenGL is used for scaling, otherwise false (CPU scaling is slower).
334 static bool HasFastScale();
336 // Adapt the BitCount of rNew to BitCount of total, including grey or color palette
337 // Can be used to create alpha/mask bitmaps after their processing in 24bit
338 void AdaptBitCount(Bitmap
& rNew
) const;
340 /** Rotate bitmap by the specified angle
343 The rotation angle in tenth of a degree. The bitmap is always rotated around its center.
346 The color to use for filling blank areas. During rotation, the
347 bitmap is enlarged such that the whole rotation result fits
348 in. The empty spaces around that rotated original bitmap are
349 then filled with this color.
351 @return true, if the operation was completed successfully.
353 bool Rotate( Degree10 nAngle10
, const Color
& rFillColor
);
355 /** Create on-off mask from bitmap
357 This method creates a bitmask from the bitmap, where every
358 pixel that equals rTransColor is set transparent, the rest
362 Color value where the bitmask should be transparent
365 Tolerance value. Specifies the maximal difference between
366 rTransColor and the individual pixel values, such that the
367 corresponding pixel is still regarded as transparent.
369 @return the resulting bitmask.
371 Bitmap
CreateMask( const Color
& rTransColor
, sal_uInt8 nTol
= 0 ) const;
373 /** Create region of similar colors in a given rectangle
376 All pixel which have this color are included in the calculated region
379 The rectangle within which matching pixel are looked for. This
380 rectangle is always clipped to the bitmap dimensions.
382 @return the generated region.
384 vcl::Region
CreateRegion( const Color
& rColor
, const tools::Rectangle
& rRect
) const;
386 /** Replace all pixel where the given mask is on with the specified color
389 Mask specifying which pixel should be replaced
392 Color to be placed in all changed pixel
394 @return true, if the operation was completed successfully.
396 bool Replace( const Bitmap
& rMask
, const Color
& rReplaceColor
);
398 /** Merge bitmap with given background color according to specified alpha mask
401 Alpha mask specifying the amount of background color to merge in
404 Background color to be used for merging
406 @return true, if the operation was completed successfully.
408 bool Replace( const AlphaMask
& rAlpha
, const Color
& rMergeColor
);
410 /** Replace all pixel having the search color with the specified color
413 Color specifying which pixel should be replaced
416 Color to be placed in all changed pixel
419 Tolerance value. Specifies the maximal difference between
420 rSearchColor and the individual pixel values, such that the
421 corresponding pixel is still regarded a match.
423 @return true, if the operation was completed successfully.
425 bool Replace( const Color
& rSearchColor
, const Color
& rReplaceColor
, sal_uInt8 nTol
= 0 );
427 /** Replace all pixel having one the search colors with the corresponding replace color
430 Array of colors specifying which pixel should be replaced
432 @param rReplaceColors
433 Array of colors to be placed in all changed pixel
436 Size of the aforementioned color arrays
439 Tolerance value. Specifies the maximal difference between
440 pSearchColor colors and the individual pixel values, such that
441 the corresponding pixel is still regarded a match.
443 @return true, if the operation was completed successfully.
446 const Color
* pSearchColors
,
447 const Color
* rReplaceColors
,
448 sal_uLong nColorCount
,
449 sal_uInt8
const * pTols
);
451 /** Convert the bitmap to a meta file
453 This works by putting continuous areas of the same color into
454 polygons painted in this color, by tracing the area's bounding
458 The resulting meta file
461 If non-null, minimal size of bound rects for individual polygons. Smaller ones are ignored.
464 A callback for showing the progress of the vectorization
469 const Link
<tools::Long
,void>* pProgress
);
471 /** Change various global color characteristics
473 @param nLuminancePercent
474 Percent of luminance change, valid range [-100,100]. Values outside this range are clipped to the valid range.
476 @param nContrastPercent
477 Percent of contrast change, valid range [-100,100]. Values outside this range are clipped to the valid range.
479 @param nChannelRPercent
480 Percent of red channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
482 @param nChannelGPercent
483 Percent of green channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
485 @param nChannelBPercent
486 Percent of blue channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
489 Exponent of the gamma function applied to the bitmap. The
490 value 1.0 results in no change, the valid range is
491 (0.0,10.0]. Values outside this range are regarded as 1.0.
494 If true, invert the channel values with the logical 'not' operator
497 Use the same formula for brightness as used by MSOffice.
499 @return true, if the operation was completed successfully.
502 short nLuminancePercent
,
503 short nContrastPercent
= 0,
504 short nChannelRPercent
= 0,
505 short nChannelGPercent
= 0,
506 short nChannelBPercent
= 0,
508 bool bInvert
= false,
509 bool msoBrightness
= false );
512 /** ReassignWithSize and recalculate bitmap.
514 ReassignWithSizes the bitmap, and recalculates the bitmap size based on the new bitmap.
516 @param rBitmap Bitmap to reassign and use for size calculation
518 SAL_DLLPRIVATE
void ReassignWithSize(const Bitmap
& rBitmap
);
520 SAL_DLLPRIVATE
void ImplMakeUnique();
521 const std::shared_ptr
<SalBitmap
>& ImplGetSalBitmap() const { return mxSalBmp
; }
522 SAL_DLLPRIVATE
void ImplSetSalBitmap( const std::shared_ptr
<SalBitmap
>& xImpBmp
);
524 SAL_DLLPRIVATE
bool ImplMakeGreyscales( sal_uInt16 nGreyscales
);
528 BitmapInfoAccess
* AcquireInfoAccess();
529 BitmapReadAccess
* AcquireReadAccess();
530 BitmapWriteAccess
* AcquireWriteAccess();
531 static void ReleaseAccess( BitmapInfoAccess
* pAccess
);
533 typedef vcl::ScopedBitmapAccess
<BitmapReadAccess
, Bitmap
, &Bitmap::AcquireReadAccess
> ScopedReadAccess
;
534 typedef vcl::ScopedBitmapAccess
<BitmapInfoAccess
, Bitmap
, &Bitmap::AcquireInfoAccess
> ScopedInfoAccess
;
537 SAL_DLLPRIVATE
bool ImplConvertUp(sal_uInt16 nBitCount
, Color
const* pExtColor
= nullptr);
538 SAL_DLLPRIVATE
bool ImplConvertDown(sal_uInt16 nBitCount
, Color
const* pExtColor
= nullptr);
541 std::shared_ptr
<SalBitmap
> mxSalBmp
;
542 MapMode maPrefMapMode
;
547 inline bool Bitmap::operator!() const
549 return( mxSalBmp
== nullptr );
552 inline bool Bitmap::IsEmpty() const
554 return( mxSalBmp
== nullptr );
557 inline const MapMode
& Bitmap::GetPrefMapMode() const
559 return maPrefMapMode
;
562 inline void Bitmap::SetPrefMapMode( const MapMode
& rMapMode
)
564 maPrefMapMode
= rMapMode
;
567 inline const Size
& Bitmap::GetPrefSize() const
572 inline void Bitmap::SetPrefSize( const Size
& rSize
)
577 inline sal_Int64
Bitmap::GetColorCount() const
579 return sal_Int64(1) << sal_Int64(GetBitCount());
582 inline sal_uLong
Bitmap::GetSizeBytes() const
584 const Size
aSizePix( GetSizePixel() );
585 return( ( static_cast<sal_uLong
>(aSizePix
.Width()) * aSizePix
.Height() * GetBitCount() ) >> 3 );
588 #endif // INCLUDED_VCL_BITMAP_HXX
590 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */