Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / vcl / bitmapex.hxx
blob58395fe7d0e4b37f6f9143828595f92956b816c3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/bitmap.hxx>
25 #include <vcl/alpha.hxx>
26 #include <tools/color.hxx>
27 #include <basegfx/color/bcolormodifier.hxx>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <sal/types.h>
32 namespace com { namespace sun { namespace star { namespace rendering {
33 class XBitmapCanvas;
34 } } } }
36 enum class TransparentType
38 NONE,
39 Color,
40 Bitmap
43 class VCL_DLLPUBLIC BitmapEx
45 public:
47 BitmapEx();
48 BitmapEx( const OUString& rIconName );
49 BitmapEx( const BitmapEx& rBitmapEx );
50 BitmapEx( const BitmapEx& rBitmapEx, Point aSrc, Size aSize );
51 BitmapEx( const Bitmap& rBmp );
52 BitmapEx( const Bitmap& rBmp, const Bitmap& rMask );
53 BitmapEx( const Bitmap& rBmp, const AlphaMask& rAlphaMask );
54 BitmapEx( const Bitmap& rBmp, const Color& rTransparentColor );
56 BitmapEx& operator=( const BitmapEx& rBitmapEx );
57 bool operator==( const BitmapEx& rBitmapEx ) const;
58 bool operator!=( const BitmapEx& rBitmapEx ) const { return !(*this==rBitmapEx); }
59 bool operator!() const { return !aBitmap; }
61 bool IsEmpty() const;
62 void SetEmpty();
63 void Clear();
65 void Draw( OutputDevice* pOutDev,
66 const Point& rDestPt ) const;
67 void Draw( OutputDevice* pOutDev,
68 const Point& rDestPt, const Size& rDestSize ) const;
70 bool IsTransparent() const;
71 TransparentType GetTransparentType() const { return eTransparent; }
73 Bitmap GetBitmap( const Color* pTransReplaceColor = nullptr ) const;
74 /// Gives direct access to the contained bitmap.
75 const Bitmap& GetBitmapRef() const;
76 Bitmap GetMask() const;
78 bool IsAlpha() const;
79 AlphaMask GetAlpha() const;
81 const Size& GetSizePixel() const { return aBitmapSize; }
82 void SetSizePixel( const Size& rNewSize, BmpScaleFlag nScaleFlag = BmpScaleFlag::Default );
84 const Size& GetPrefSize() const { return aBitmap.GetPrefSize(); }
85 void SetPrefSize( const Size& rPrefSize ) { aBitmap.SetPrefSize( rPrefSize ); }
87 const MapMode& GetPrefMapMode() const { return aBitmap.GetPrefMapMode(); }
88 void SetPrefMapMode( const MapMode& rPrefMapMode ) { aBitmap.SetPrefMapMode( rPrefMapMode ); }
90 const Color& GetTransparentColor() const { return aTransparentColor; }
92 sal_uInt16 GetBitCount() const { return aBitmap.GetBitCount(); }
93 sal_uLong GetSizeBytes() const;
94 BitmapChecksum GetChecksum() const;
96 public:
98 /** Convert bitmap format
100 @param eConversion
101 The format this bitmap should be converted to.
103 @return true, if the conversion was completed successfully.
105 bool Convert( BmpConversion eConversion );
107 /** Reduce number of colors for the bitmap using the POPULAR algorithm
109 @param nNewColorCount
110 Maximal number of bitmap colors after the reduce operation
112 @return true, if the color reduction operation was completed successfully.
114 bool ReduceColors( sal_uInt16 nNewColorCount );
116 /** Apply a dither algorithm to the bitmap
118 This method dithers the bitmap inplace, i.e. a true color
119 bitmap is converted to a paletted bitmap, reducing the color
120 deviation by error diffusion.
122 bool Dither();
124 /** Crop the bitmap
126 @param rRectPixel
127 A rectangle specifying the crop amounts on all four sides of
128 the bitmap. If the upper left corner of the bitmap is assigned
129 (0,0), then this method cuts out the given rectangle from the
130 bitmap. Note that the rectangle is clipped to the bitmap's
131 dimension, i.e. negative left,top rectangle coordinates or
132 exceeding width or height is ignored.
134 @return true, if cropping was performed successfully. If
135 nothing had to be cropped, because e.g. the crop rectangle
136 included the bitmap, false is returned, too!
138 bool Crop( const tools::Rectangle& rRectPixel );
140 /** Expand the bitmap by pixel padding
142 @param nDX
143 Number of pixel to pad at the right border of the bitmap
145 @param nDY
146 Number of scanlines to pad at the bottom border of the bitmap
148 @param bExpandTransparent
149 Whether to expand the transparency color or not.
151 @return true, if padding was performed successfully. false is
152 not only returned when the operation failed, but also if
153 nothing had to be done, e.g. because nDX and nDY were zero.
155 bool Expand(
156 sal_uLong nDX, sal_uLong nDY,
157 bool bExpandTransparent = false );
159 /** Copy a rectangular area from another bitmap
161 @param rRectDst
162 Destination rectangle in this bitmap. This is clipped to the
163 bitmap dimensions.
165 @param rRectSrc
166 Source rectangle in pBmpSrc. This is clipped to the source
167 bitmap dimensions. Note further that no scaling takes place
168 during this copy operation, i.e. only the minimum of source
169 and destination rectangle's width and height are used.
171 @param pBmpExSrc
172 The source bitmap to copy from. If this argument is NULL, or
173 equal to the object this method is called on, copying takes
174 place within the same bitmap.
176 @return true, if the operation completed successfully. false
177 is not only returned when the operation failed, but also if
178 nothing had to be done, e.g. because one of the rectangles are
179 empty.
181 bool CopyPixel(
182 const tools::Rectangle& rRectDst,
183 const tools::Rectangle& rRectSrc,
184 const BitmapEx* pBmpExSrc );
186 /** Fill the entire bitmap with the given color
188 @param rFillColor
189 Color value to use for filling. Set the transparency part of
190 the color to fill the mask.
192 @return true, if the operation was completed successfully.
194 bool Erase( const Color& rFillColor );
196 /** Perform the Invert operation on every pixel
198 @return true, if the operation was completed successfully.
200 bool Invert();
202 /** Mirror the bitmap
204 @param nMirrorFlags
205 About which axis (horizontal, vertical, or both) to mirror
207 @return true, if the operation was completed successfully.
209 bool Mirror( BmpMirrorFlags nMirrorFlags );
211 /** Scale the bitmap
213 @param rNewSize
214 The resulting size of the scaled bitmap
216 @param nScaleFlag
217 The algorithm to be used for scaling
219 @return true, if the operation was completed successfully.
221 bool Scale(
222 const Size& rNewSize,
223 BmpScaleFlag nScaleFlag = BmpScaleFlag::Default );
225 /** Scale the bitmap
227 @param rScaleX
228 The scale factor in x direction.
230 @param rScaleY
231 The scale factor in y direction.
233 @param nScaleFlag
234 The algorithm to be used for scaling
236 @return true, if the operation was completed successfully.
238 bool Scale(
239 const double& rScaleX,
240 const double& rScaleY,
241 BmpScaleFlag nScaleFlag = BmpScaleFlag::Default );
243 /** Rotate bitmap by the specified angle
245 @param nAngle10
246 The rotation angle in tenth of a degree. The bitmap is always rotated around its center.
248 @param rFillColor
249 The color to use for filling blank areas. During rotation, the
250 bitmap is enlarged such that the whole rotation result fits
251 in. The empty spaces around that rotated original bitmap are
252 then filled with this color.
254 @return true, if the operation was completed successfully.
256 bool Rotate(
257 long nAngle10,
258 const Color& rFillColor );
260 /** Replace all pixel having the search color with the specified color
262 @param rSearchColor
263 Color specifying which pixel should be replaced
265 @param rReplaceColor
266 Color to be placed in all changed pixel
268 @return true, if the operation was completed successfully.
270 bool Replace(
271 const Color& rSearchColor,
272 const Color& rReplaceColor );
274 /** Replace all pixel having one the search colors with the corresponding replace color
276 @param pSearchColors
277 Array of colors specifying which pixel should be replaced
279 @param pReplaceColors
280 Array of colors to be placed in all changed pixel
282 @param nColorCount
283 Size of the aforementioned color arrays
285 @param pTols
286 Tolerance value. Specifies the maximal difference between
287 pSearchColor colors and the individual pixel values, such that
288 the corresponding pixel is still regarded a match.
290 @return true, if the operation was completed successfully.
292 bool Replace(
293 const Color* pSearchColors,
294 const Color* pReplaceColors,
295 sal_uLong nColorCount,
296 const sal_uLong* pTols = nullptr );
298 /** Change various global color characteristics
300 @param nLuminancePercent
301 Percent of luminance change, valid range [-100,100]. Values outside this range are clipped to the valid range.
303 @param nContrastPercent
304 Percent of contrast change, valid range [-100,100]. Values outside this range are clipped to the valid range.
306 @param nChannelRPercent
307 Percent of red channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
309 @param nChannelGPercent
310 Percent of green channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
312 @param nChannelBPercent
313 Percent of blue channel change, valid range [-100,100]. Values outside this range are clipped to the valid range.
315 @param fGamma
316 Exponent of the gamma function applied to the bitmap. The
317 value 1.0 results in no change, the valid range is
318 (0.0,10.0]. Values outside this range are regarded as 1.0.
320 @param bInvert
321 If true, invert the channel values with the logical 'not' operator
323 @param msoBrightness
324 Use the same formula for brightness as used by MSOffice.
326 @return true, if the operation was completed successfully.
328 bool Adjust(
329 short nLuminancePercent,
330 short nContrastPercent,
331 short nChannelRPercent,
332 short nChannelGPercent,
333 short nChannelBPercent,
334 double fGamma = 1.0,
335 bool bInvert = false,
336 bool msoBrightness = false );
338 /** Apply specified filter to the bitmap
340 @param eFilter
341 The filter algorithm to apply
343 @param pFilterParam
344 Various parameter for the different bitmap filter algorithms
346 @return true, if the operation was completed successfully.
348 bool Filter(
349 BmpFilter eFilter,
350 const BmpFilterParam* pFilterParam = nullptr );
352 /** Get transparency at given position
354 @param nX
355 integer X-Position in Bitmap
357 @param nY
358 integer Y-Position in Bitmap
360 @return transparency value in the range of [0 .. 255] where
361 0 is not transparent, 255 is fully transparent
363 sal_uInt8 GetTransparency(
364 sal_Int32 nX,
365 sal_Int32 nY) const;
367 /** Create transformed Bitmap
369 @param fWidth
370 The target width in pixels
372 @param fHeight
373 The target height in pixels
375 @param rTransformation
376 The back transformation for each pixel in (0 .. fWidth),(0 .. fHeight) to
377 local pixel coordinates
379 @param bSmooth
380 Defines if pixel interpolation is to be used to create the result
382 BitmapEx TransformBitmapEx(
383 double fWidth,
384 double fHeight,
385 const basegfx::B2DHomMatrix& rTransformation,
386 bool bSmooth) const;
388 /** Create transformed Bitmap
390 @param rTransformation
391 The transformation from unit coordinates to the unit range
393 @param rVisibleRange
394 The relative visible range in unit coordinates, relative to (0,0,1,1) which
395 defines the whole target area
397 @param fMaximumArea
398 A limitation for the maximum size of pixels to use for the result
400 @param bSmooth
401 Defines if pixel interpolation is to be used to create the result
403 The traget size of the result bitmap is defined by transforming the given
404 rTargetRange with the given rTransformation; the area of the result is
405 linearly scaled to not exceed the given fMaximumArea
407 @return The transformed bitmap
409 BitmapEx getTransformed(
410 const basegfx::B2DHomMatrix& rTransformation,
411 const basegfx::B2DRange& rVisibleRange,
412 double fMaximumArea,
413 bool bSmooth) const;
415 /** Create ColorStack-modified version of this BitmapEx
417 @param rBColorModifierStack
418 A ColrModifierStack which defines how each pixel has to be modified
420 BitmapEx ModifyBitmapEx( const basegfx::BColorModifierStack& rBColorModifierStack) const;
422 static BitmapEx AutoScaleBitmap( BitmapEx & aBitmap, const long aStandardSize );
424 /// populate from a canvas implementation
425 bool Create(
426 const css::uno::Reference< css::rendering::XBitmapCanvas > &xBitmapCanvas,
427 const Size &rSize );
428 public:
430 SAL_DLLPRIVATE std::shared_ptr<ImpBitmap> ImplGetBitmapImpBitmap() const { return aBitmap.ImplGetImpBitmap(); }
431 SAL_DLLPRIVATE std::shared_ptr<ImpBitmap> ImplGetMaskImpBitmap() const { return aMask.ImplGetImpBitmap(); }
434 private:
435 friend class ImpGraphic;
436 friend bool VCL_DLLPUBLIC WriteDIBBitmapEx(const BitmapEx& rSource, SvStream& rOStm);
437 void loadFromIconTheme( const OUString& rIconName );
439 Bitmap aBitmap;
440 Bitmap aMask;
441 Size aBitmapSize;
442 Color aTransparentColor;
443 TransparentType eTransparent;
444 bool bAlpha;
449 /** Create a blend frame as BitmapEx
451 @param nAlpha
452 The blend value defines how strong the frame will be blended with the
453 existing content, 255 == full coverage, 0 == no frame will be drawn
455 @param aColorTopLeft, aColorBottomRight
456 The colors defining the frame. These colors are linearly interpolated from
457 aColorTopLeft and aColorBottomRight using the width and height of the area
459 @param rSize
460 The size of the frame in pixels
462 BitmapEx VCL_DLLPUBLIC createBlendFrame(
463 const Size& rSize,
464 sal_uInt8 nAlpha,
465 Color aColorTopLeft,
466 Color aColorBottomRight);
469 /** Create a blend frame as BitmapEx
471 @param nAlpha
472 The blend value defines how strong the frame will be blended with the
473 existing content, 255 == full coverage, 0 == no frame will be drawn
475 @param aColorTopLeft, aColorBottomRight, aColorTopRight, aColorBottomLeft
476 The colors defining the frame.
478 @param rSize
479 The size of the frame in pixels
481 BitmapEx VCL_DLLPUBLIC createBlendFrame(
482 const Size& rSize,
483 sal_uInt8 nAlpha,
484 Color aColorTopLeft,
485 Color aColorTopRight,
486 Color aColorBottomRight,
487 Color aColorBottomLeft);
489 #endif // INCLUDED_VCL_BITMAPEX_HXX
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */