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_BITMAPEX_HXX
21 #define INCLUDED_VCL_BITMAPEX_HXX
23 #include <vcl/dllapi.h>
24 #include <vcl/alpha.hxx>
25 #include <vcl/Scanline.hxx>
26 #include <tools/color.hxx>
27 #include <tools/degree.hxx>
29 #include <sal/types.h>
31 namespace com::sun::star::rendering
{
34 namespace com::sun::star::uno
{ template <class interface_type
> class Reference
; }
35 namespace basegfx
{ class BColorModifierStack
; }
37 class SAL_WARN_UNUSED VCL_DLLPUBLIC BitmapEx
42 explicit BitmapEx( const OUString
& rIconName
);
43 BitmapEx( const BitmapEx
& rBitmapEx
);
44 BitmapEx( const BitmapEx
& rBitmapEx
, Point aSrc
, Size aSize
);
45 BitmapEx(Size aSize
, vcl::PixelFormat ePixelFormat
);
46 explicit BitmapEx( const Bitmap
& rBmp
);
47 BitmapEx( const Bitmap
& rBmp
, const Bitmap
& rMask
);
48 BitmapEx( const Bitmap
& rBmp
, const AlphaMask
& rAlphaMask
);
49 BitmapEx( const Bitmap
& rBmp
, const Color
& rTransparentColor
);
51 BitmapEx
& operator=( const BitmapEx
& rBitmapEx
);
52 BitmapEx
& operator=( const Bitmap
& rBitmap
) { return operator=(BitmapEx(rBitmap
)); }
53 bool operator==( const BitmapEx
& rBitmapEx
) const;
54 bool operator!=( const BitmapEx
& rBitmapEx
) const { return !(*this==rBitmapEx
); }
61 void Draw( OutputDevice
* pOutDev
,
62 const Point
& rDestPt
) const;
63 void Draw( OutputDevice
* pOutDev
,
64 const Point
& rDestPt
, const Size
& rDestSize
) const;
66 Bitmap
GetBitmap( Color aTransparentReplaceColor
) const;
67 /// Gives direct access to the contained bitmap.
68 const Bitmap
& GetBitmap() const;
71 const AlphaMask
& GetAlphaMask() const { return maAlphaMask
; }
73 const Size
& GetSizePixel() const { return maBitmapSize
; }
74 void SetSizePixel(const Size
& rNewSize
);
76 const Size
& GetPrefSize() const { return maBitmap
.GetPrefSize(); }
77 void SetPrefSize( const Size
& rPrefSize
) { maBitmap
.SetPrefSize( rPrefSize
); }
79 const MapMode
& GetPrefMapMode() const { return maBitmap
.GetPrefMapMode(); }
80 void SetPrefMapMode( const MapMode
& rPrefMapMode
) { maBitmap
.SetPrefMapMode( rPrefMapMode
); }
82 vcl::PixelFormat
getPixelFormat() const
84 return maBitmap
.getPixelFormat();
87 sal_Int64
GetSizeBytes() const;
88 BitmapChecksum
GetChecksum() const;
90 /** Convert bitmap format
93 The format this bitmap should be converted to.
95 @return true, if the conversion was completed successfully.
97 bool Convert( BmpConversion eConversion
);
102 A rectangle specifying the crop amounts on all four sides of
103 the bitmap. If the upper left corner of the bitmap is assigned
104 (0,0), then this method cuts out the given rectangle from the
105 bitmap. Note that the rectangle is clipped to the bitmap's
106 dimension, i.e. negative left,top rectangle coordinates or
107 exceeding width or height is ignored.
109 @return true, if cropping was performed successfully. If
110 nothing had to be cropped, because e.g. the crop rectangle
111 included the bitmap, false is returned, too!
113 bool Crop( const tools::Rectangle
& rRectPixel
);
115 /** Expand the bitmap by pixel padding
118 Number of pixel to pad at the right border of the bitmap
121 Number of scanlines to pad at the bottom border of the bitmap
123 @param bExpandTransparent
124 Whether to expand the transparency color or not.
127 sal_Int32 nDX
, sal_Int32 nDY
,
128 bool bExpandTransparent
= false );
130 /** Copy a rectangular area from another bitmap
133 Destination rectangle in this bitmap. This is clipped to the
137 Source rectangle in pBmpSrc. This is clipped to the source
138 bitmap dimensions. Note further that no scaling takes place
139 during this copy operation, i.e. only the minimum of source
140 and destination rectangle's width and height are used.
143 The source bitmap to copy from. If this argument is NULL, or
144 equal to the object this method is called on, copying takes
145 place within the same bitmap.
147 @return true, if the operation completed successfully. false
148 is not only returned when the operation failed, but also if
149 nothing had to be done, e.g. because one of the rectangles are
153 const tools::Rectangle
& rRectDst
,
154 const tools::Rectangle
& rRectSrc
,
155 const BitmapEx
* pBmpExSrc
);
157 /** Fill the entire bitmap with the given color
160 Color value to use for filling. Set the transparency part of
161 the color to fill the mask.
163 @return true, if the operation was completed successfully.
165 bool Erase( const Color
& rFillColor
);
167 /** Perform the Invert operation on every pixel
169 @return true, if the operation was completed successfully.
173 /** Mirror the bitmap
176 About which axis (horizontal, vertical, or both) to mirror
178 @return true, if the operation was completed successfully.
180 bool Mirror( BmpMirrorFlags nMirrorFlags
);
185 The resulting size of the scaled bitmap
188 The algorithm to be used for scaling
190 @return true, if the operation was completed successfully.
193 const Size
& rNewSize
,
194 BmpScaleFlag nScaleFlag
= BmpScaleFlag::Default
);
199 The scale factor in x direction.
202 The scale factor in y direction.
205 The algorithm to be used for scaling
207 @return true, if the operation was completed successfully.
210 const double& rScaleX
,
211 const double& rScaleY
,
212 BmpScaleFlag nScaleFlag
= BmpScaleFlag::Default
);
214 /** Rotate bitmap by the specified angle
217 The rotation angle in tenth of a degree. The bitmap is always rotated around its center.
220 The color to use for filling blank areas. During rotation, the
221 bitmap is enlarged such that the whole rotation result fits
222 in. The empty spaces around that rotated original bitmap are
223 then filled with this color.
225 @return true, if the operation was completed successfully.
229 const Color
& rFillColor
);
231 /** Replace all pixel having the search color with the specified color
234 Color specifying which pixel should be replaced
237 Color to be placed in all changed pixel
240 const Color
& rSearchColor
,
241 const Color
& rReplaceColor
);
243 /** Replace all pixel having the search color with the specified color
246 Color specifying which pixel should be replaced
249 Color to be placed in all changed pixel
252 Tolerance value. Specifies the maximal difference between
253 rSearchColor and the individual pixel values, such that the
254 corresponding pixel is still regarded a match.
257 const Color
& rSearchColor
,
258 const Color
& rReplaceColor
,
259 sal_uInt8 nTolerance
);
261 /** Replace all pixel having one the search colors with the corresponding replace color
264 Array of colors specifying which pixel should be replaced
266 @param pReplaceColors
267 Array of colors to be placed in all changed pixel
270 Size of the aforementioned color arrays
273 Tolerance value. Specifies the maximal difference between
274 pSearchColor colors and the individual pixel values, such that
275 the corresponding pixel is still regarded a match.
278 const Color
* pSearchColors
,
279 const Color
* pReplaceColors
,
280 size_t nColorCount
);
282 /** Replace all pixel having one the search colors with the corresponding replace color
285 Array of colors specifying which pixel should be replaced
287 @param rReplaceColors
288 Array of colors to be placed in all changed pixel
291 Size of the aforementioned color arrays
294 Tolerance value. Specifies the maximal difference between
295 pSearchColor colors and the individual pixel values, such that
296 the corresponding pixel is still regarded a match.
298 @return true, if the operation was completed successfully.
301 const Color
* pSearchColors
,
302 const Color
* pReplaceColors
,
304 sal_uInt8
const * pTols
);
306 /** Replace transparency with given color.
308 void ReplaceTransparency( const Color
& rColor
);
310 /** Get contours in image */
311 tools::Polygon
GetContour( bool bContourEdgeDetect
, const tools::Rectangle
* pWorkRect
);
313 /** Change various global color characteristics
315 @param nLuminancePercent
316 Percent of luminance change, valid range [-100,100]. Values outside this range are clipped to the valid range.
318 @param nContrastPercent
319 Percent of contrast change, valid range [-100,100]. Values outside this range are clipped to the valid range.
321 @param nChannelRPercent
322 Percent of red channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
324 @param nChannelGPercent
325 Percent of green channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
327 @param nChannelBPercent
328 Percent of blue channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
331 Exponent of the gamma function applied to the bitmap. The
332 value 1.0 results in no change, the valid range is
333 (0.0,10.0]. Values outside this range are regarded as 1.0.
336 If true, invert the channel values with the logical 'not' operator
339 Use the same formula for brightness as used by MSOffice.
341 @return true, if the operation was completed successfully.
344 short nLuminancePercent
,
345 short nContrastPercent
,
346 short nChannelRPercent
,
347 short nChannelGPercent
,
348 short nChannelBPercent
,
350 bool bInvert
= false,
351 bool msoBrightness
= false );
353 /** Get alpha at given position
356 integer X-Position in Bitmap
359 integer Y-Position in Bitmap
361 @return alpha value in the range of [0 .. 255] where
362 0 is fully transparent, 255 is not transparent
368 /** Get pixel color (including alpha) at given position
371 integer X-Position in Bitmap
374 integer Y-Position in Bitmap
376 ::Color
GetPixelColor(
380 /** Create transformed Bitmap
383 The target width in pixels
386 The target height in pixels
388 @param rTransformation
389 The back transformation for each pixel in (0 .. fWidth),(0 .. fHeight) to
390 local pixel coordinates
393 BitmapEx
TransformBitmapEx(
396 const basegfx::B2DHomMatrix
& rTransformation
) const;
398 /** Create transformed Bitmap
400 @param rTransformation
401 The transformation from unit coordinates to the unit range
404 The relative visible range in unit coordinates, relative to (0,0,1,1) which
405 defines the whole target area
408 A limitation for the maximum size of pixels to use for the result
410 The target size of the result bitmap is defined by transforming the given
411 rTargetRange with the given rTransformation; the area of the result is
412 linearly scaled to not exceed the given fMaximumArea
414 @return The transformed bitmap
417 BitmapEx
getTransformed(
418 const basegfx::B2DHomMatrix
& rTransformation
,
419 const basegfx::B2DRange
& rVisibleRange
,
420 double fMaximumArea
) const;
422 /** Create ColorStack-modified version of this BitmapEx
424 @param rBColorModifierStack
425 A ColrModifierStack which defines how each pixel has to be modified
428 BitmapEx
ModifyBitmapEx( const basegfx::BColorModifierStack
& rBColorModifierStack
) const;
431 static BitmapEx
AutoScaleBitmap( BitmapEx
const & aBitmap
, const tools::Long aStandardSize
);
433 /// populate from a canvas implementation
435 const css::uno::Reference
< css::rendering::XBitmapCanvas
> &xBitmapCanvas
,
438 void ChangeColorAlpha( sal_uInt8 cIndexFrom
, sal_Int8 nAlphaTo
);
440 void AdjustTransparency( sal_uInt8 cTrans
);
442 void CombineMaskOr(Color maskColor
, sal_uInt8 nTol
);
445 * Retrieves the color model data we need for the XImageConsumer stuff.
447 void GetColorModel(css::uno::Sequence
< sal_Int32
>& rRGBPalette
,
448 sal_uInt32
& rnRedMask
, sal_uInt32
& rnGreenMask
, sal_uInt32
& rnBlueMask
, sal_uInt32
& rnAlphaMask
, sal_uInt32
& rnTransparencyIndex
,
449 sal_uInt32
& rnWidth
, sal_uInt32
& rnHeight
, sal_uInt8
& rnBitCount
);
451 SAL_DLLPRIVATE
std::shared_ptr
<SalBitmap
> const & ImplGetBitmapSalBitmap() const { return maBitmap
.ImplGetSalBitmap(); }
453 /// Dumps the pixels as PNG in bitmap.png.
454 void DumpAsPng(const char* pFileName
= nullptr) const;
457 friend class ImpGraphic
;
458 friend class OutputDevice
;
459 friend bool VCL_DLLPUBLIC
WriteDIBBitmapEx(const BitmapEx
& rSource
, SvStream
& rOStm
);
460 friend bool VCL_DLLPUBLIC
ReadRawDIB(BitmapEx
& rTarget
, const unsigned char* pBuf
,
461 const ScanlineFormat nFormat
,
465 void loadFromIconTheme( const OUString
& rIconName
);
468 AlphaMask maAlphaMask
;
473 /** Create a blend frame as BitmapEx
476 The blend value defines how strong the frame will be blended with the
477 existing content, 255 == full coverage, 0 == no frame will be drawn
479 @param aColorTopLeft, aColorBottomRight
480 The colors defining the frame. These colors are linearly interpolated from
481 aColorTopLeft and aColorBottomRight using the width and height of the area
484 The size of the frame in pixels
486 BitmapEx VCL_DLLPUBLIC
createBlendFrame(
490 Color aColorBottomRight
);
493 /** Create a blend frame as BitmapEx
496 The blend value defines how strong the frame will be blended with the
497 existing content, 255 == full coverage, 0 == no frame will be drawn
499 @param aColorTopLeft, aColorBottomRight, aColorTopRight, aColorBottomLeft
500 The colors defining the frame.
503 The size of the frame in pixels
505 BitmapEx
createBlendFrame(
509 Color aColorTopRight
,
510 Color aColorBottomRight
,
511 Color aColorBottomLeft
);
513 #endif // INCLUDED_VCL_BITMAPEX_HXX
515 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */