LibreOffice Module vcl (master)  1
bitmap.hxx
Go to the documentation of this file.
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_BITMAP_HXX
21 #define INCLUDED_VCL_BITMAP_HXX
22 
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>
30 
31 #include <o3tl/typed_flags_set.hxx>
32 
33 #include <memory>
34 
35 #define GAMMA( _def_cVal, _def_InvGamma ) (static_cast<sal_uInt8>(MinMax(FRound(pow( _def_cVal/255.0,_def_InvGamma)*255.0),0,255)))
36 
37 class Color;
38 
39 template <typename Arg, typename Ret> class Link;
40 
41 enum class BmpMirrorFlags
42 {
43  NONE = 0x00,
44  Horizontal = 0x01,
45  Vertical = 0x02,
46 };
47 
48 namespace o3tl
49 {
50  template<> struct typed_flags<BmpMirrorFlags> : is_typed_flags<BmpMirrorFlags, 0x03> {};
51 }
52 
53 enum class BmpScaleFlag
54 {
55 // Try to preferably use these.
56  Default,
57  Fast,
59 // Specific algorithms, use only if you really need to (mainly used for tests)
61  Interpolate, // fast, integer bilinear
62  Lanczos,
63  BiCubic,
64  BiLinear,
65  Super // bilinear interpolation when supersampling and averaging when subsampling under certain scale
66 };
67 
68 #define BMP_COL_TRANS Color( 252, 3, 251 )
69 
70 enum class BmpConversion
71 {
72  NNONE,
74  N4BitGreys,
76  N8BitGreys,
78  N24Bit,
79  N32Bit,
80  N8BitTrans,
81  N8BitNoConversion // make 8bit without color conversion (e.g. take the red channel)
82 };
83 
84 enum class BmpCombine
85 {
86  Or, And
87 };
88 
89 class BitmapInfoAccess;
90 class BitmapReadAccess;
91 class BitmapWriteAccess;
92 class BitmapPalette;
93 class GDIMetaFile;
94 class AlphaMask;
95 class OutputDevice;
96 class SalBitmap;
97 
99 {
100  #if defined(_WIN32)
101  void* pDIB; // device independent byte buffer
102  #elif defined( MACOSX ) || defined( IOS )
103  // Nothing needed, apparently
104  #else
105  void* aPixmap;
106  #endif
107  int mnWidth;
108  int mnHeight;
109 };
110 
112 {
113 public:
114 
115  Bitmap();
116  Bitmap( const Bitmap& rBitmap );
117  Bitmap( const Size& rSizePixel, sal_uInt16 nBitCount, const BitmapPalette* pPal = nullptr );
118  explicit Bitmap( std::shared_ptr<SalBitmap> const & xSalBitmap );
119  virtual ~Bitmap();
120 
121  Bitmap& operator=( const Bitmap& rBitmap );
122  Bitmap& operator=( Bitmap&& rBitmap ) noexcept;
123  inline bool operator!() const;
124  bool operator==( const Bitmap& rBitmap ) const;
125  bool operator!=( const Bitmap& rBitmap ) const { return !operator==(rBitmap); }
126 
127  inline bool IsEmpty() const;
128  void SetEmpty();
129 
130  inline const MapMode& GetPrefMapMode() const;
131  inline void SetPrefMapMode( const MapMode& rMapMode );
132 
133  inline const Size& GetPrefSize() const;
134  inline void SetPrefSize( const Size& rSize );
135 
136  Size GetSizePixel() const;
137 
138  sal_uInt16 GetBitCount() const;
139  inline sal_Int64 GetColorCount() const;
140  inline sal_uLong GetSizeBytes() const;
141  bool HasGreyPalette8Bit() const;
142  bool HasGreyPaletteAny() const;
150  bool GetSystemData( BitmapSystemData& rData ) const;
151 
152  BitmapChecksum GetChecksum() const;
153 
154  Bitmap CreateDisplayBitmap( OutputDevice* pDisplay ) const;
155 
156  static const BitmapPalette&
157  GetGreyPalette( int nEntries );
158 
159 public:
160 
168  bool Convert( BmpConversion eConversion );
169 
177  bool Dither();
178 
193  bool Crop( const tools::Rectangle& rRectPixel );
194 
210  bool Expand(
211  sal_uLong nDX, sal_uLong nDY,
212  const Color* pInitColor = nullptr );
213 
236  bool CopyPixel(
237  const tools::Rectangle& rRectDst,
238  const tools::Rectangle& rRectSrc,
239  const Bitmap* pBmpSrc = nullptr );
240 
241  bool CopyPixel_AlphaOptimized(
242  const tools::Rectangle& rRectDst,
243  const tools::Rectangle& rRectSrc,
244  const Bitmap* pBmpSrc );
245 
256  bool CombineSimple(
257  const Bitmap& rMask,
258  BmpCombine eCombine );
259 
276  bool Blend(
277  const AlphaMask& rAlpha,
278  const Color& rBackgroundColor );
279 
287  bool Erase( const Color& rFillColor );
288 
293  bool Invert();
294 
302  bool Mirror( BmpMirrorFlags nMirrorFlags );
303 
314  bool Scale( const Size& rNewSize, BmpScaleFlag nScaleFlag = BmpScaleFlag::Default );
315 
329  bool Scale( const double& rScaleX, const double& rScaleY, BmpScaleFlag nScaleFlag = BmpScaleFlag::Default );
330 
338  static bool HasFastScale();
339 
340  // Adapt the BitCount of rNew to BitCount of total, including grey or color palette
341  // Can be used to create alpha/mask bitmaps after their processing in 24bit
342  void AdaptBitCount(Bitmap& rNew) const;
343 
357  bool Rotate( Degree10 nAngle10, const Color& rFillColor );
358 
375  Bitmap CreateMask( const Color& rTransColor, sal_uInt8 nTol = 0 ) const;
376 
388  vcl::Region CreateRegion( const Color& rColor, const tools::Rectangle& rRect ) const;
389 
400  bool Replace( const Bitmap& rMask, const Color& rReplaceColor );
401 
412  bool Replace( const AlphaMask& rAlpha, const Color& rMergeColor );
413 
429  bool Replace( const Color& rSearchColor, const Color& rReplaceColor, sal_uInt8 nTol = 0 );
430 
449  bool Replace(
450  const Color* pSearchColors,
451  const Color* rReplaceColors,
452  size_t nColorCount,
453  sal_uInt8 const * pTols );
454 
470  void Vectorize(
471  GDIMetaFile& rMtf,
472  sal_uInt8 cReduce,
473  const Link<tools::Long,void>* pProgress );
474 
505  bool Adjust(
506  short nLuminancePercent,
507  short nContrastPercent = 0,
508  short nChannelRPercent = 0,
509  short nChannelGPercent = 0,
510  short nChannelBPercent = 0,
511  double fGamma = 1.0,
512  bool bInvert = false,
513  bool msoBrightness = false );
514 
515 public:
522  SAL_DLLPRIVATE void ReassignWithSize(const Bitmap& rBitmap);
523 
524  SAL_DLLPRIVATE void ImplMakeUnique();
525  const std::shared_ptr<SalBitmap>& ImplGetSalBitmap() const { return mxSalBmp; }
526  SAL_DLLPRIVATE void ImplSetSalBitmap( const std::shared_ptr<SalBitmap>& xImpBmp );
527 
528  SAL_DLLPRIVATE bool ImplMakeGreyscales( sal_uInt16 nGreyscales );
529 
530 public:
531 
532  BitmapInfoAccess* AcquireInfoAccess();
533  BitmapReadAccess* AcquireReadAccess();
534  BitmapWriteAccess* AcquireWriteAccess();
535  static void ReleaseAccess( BitmapInfoAccess* pAccess );
536 
539 
540 private:
541  SAL_DLLPRIVATE bool ImplConvertUp(sal_uInt16 nBitCount, Color const* pExtColor = nullptr);
542  SAL_DLLPRIVATE bool ImplConvertDown(sal_uInt16 nBitCount, Color const* pExtColor = nullptr);
543 
544 private:
545  std::shared_ptr<SalBitmap> mxSalBmp;
548 
549 };
550 
551 inline bool Bitmap::operator!() const
552 {
553  return( mxSalBmp == nullptr );
554 }
555 
556 inline bool Bitmap::IsEmpty() const
557 {
558  return( mxSalBmp == nullptr );
559 }
560 
561 inline const MapMode& Bitmap::GetPrefMapMode() const
562 {
563  return maPrefMapMode;
564 }
565 
566 inline void Bitmap::SetPrefMapMode( const MapMode& rMapMode )
567 {
568  maPrefMapMode = rMapMode;
569 }
570 
571 inline const Size& Bitmap::GetPrefSize() const
572 {
573  return maPrefSize;
574 }
575 
576 inline void Bitmap::SetPrefSize( const Size& rSize )
577 {
578  maPrefSize = rSize;
579 }
580 
581 inline sal_Int64 Bitmap::GetColorCount() const
582 {
583  return sal_Int64(1) << sal_Int64(GetBitCount());
584 }
585 
587 {
588  const Size aSizePix( GetSizePixel() );
589  return( ( static_cast<sal_uLong>(aSizePix.Width()) * aSizePix.Height() * GetBitCount() ) >> 3 );
590 }
591 
592 #endif // INCLUDED_VCL_BITMAP_HXX
593 
594 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt64 BitmapChecksum
Definition: checksum.hxx:30
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
const std::shared_ptr< SalBitmap > & ImplGetSalBitmap() const
Definition: bitmap.hxx:525
sal_Int64 GetColorCount() const
Definition: bitmap.hxx:581
sal_uIntPtr sal_uLong
Size maPrefSize
Definition: bitmap.hxx:547
Size GetSizePixel() const
const Size & GetPrefSize() const
Definition: bitmap.hxx:571
bool operator!=(const Bitmap &rBitmap) const
Definition: bitmap.hxx:125
Point Rotate(const Point &rPoint, Degree10 nOrientation, const Point &rOrigin)
vcl::ScopedBitmapAccess< BitmapReadAccess, Bitmap,&Bitmap::AcquireReadAccess > ScopedReadAccess
Definition: bitmap.hxx:537
bool operator==(const ItalicMatrix &a, const ItalicMatrix &b)
Definition: vclenum.hxx:206
const MapMode & GetPrefMapMode() const
Definition: bitmap.hxx:561
void SetPrefMapMode(const MapMode &rMapMode)
Definition: bitmap.hxx:566
void * aPixmap
Definition: bitmap.hxx:105
sal_uLong GetSizeBytes() const
Definition: bitmap.hxx:586
tools::Long Width() const
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:303
bool operator!() const
Definition: bitmap.hxx:551
BmpMirrorFlags
Definition: bitmap.hxx:41
void SetPrefSize(const Size &rSize)
Definition: bitmap.hxx:576
BmpScaleFlag
Definition: bitmap.hxx:53
BmpConversion
Definition: bitmap.hxx:70
short nBitCount
#define SAL_WARN_UNUSED
unsigned char sal_uInt8
tools::Long Height() const
bool IsEmpty() const
Definition: bitmap.hxx:556
sal_uInt16 GetBitCount() const
MapMode maPrefMapMode
Definition: bitmap.hxx:546
std::shared_ptr< SalBitmap > mxSalBmp
Definition: bitmap.hxx:545
BmpCombine
Definition: bitmap.hxx:84
vcl::ScopedBitmapAccess< BitmapInfoAccess, Bitmap,&Bitmap::AcquireInfoAccess > ScopedInfoAccess
Definition: bitmap.hxx:538