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>
31 
32 #include <o3tl/typed_flags_set.hxx>
33 
34 #include <memory>
35 
36 #define GAMMA( _def_cVal, _def_InvGamma ) (static_cast<sal_uInt8>(MinMax(FRound(pow( _def_cVal/255.0,_def_InvGamma)*255.0),0,255)))
37 
38 class Color;
39 
40 template <typename Arg, typename Ret> class Link;
41 
42 enum class BmpMirrorFlags
43 {
44  NONE = 0x00,
45  Horizontal = 0x01,
46  Vertical = 0x02,
47 };
48 
49 namespace o3tl
50 {
51  template<> struct typed_flags<BmpMirrorFlags> : is_typed_flags<BmpMirrorFlags, 0x03> {};
52 }
53 
54 enum class BmpScaleFlag
55 {
56 // Try to preferably use these.
57  Default,
58  Fast,
60 // Specific algorithms, use only if you really need to (mainly used for tests)
62  Interpolate, // fast, integer bilinear
63  Lanczos,
64  BiCubic,
65  BiLinear,
66  Super // bilinear interpolation when supersampling and averaging when subsampling under certain scale
67 };
68 
69 #define BMP_COL_TRANS Color( 252, 3, 251 )
70 
71 enum class BmpConversion
72 {
73  NNONE,
75  N8BitGreys,
77  N24Bit,
78  N32Bit,
79  N8BitTrans,
80  N8BitNoConversion // make 8bit without color conversion (e.g. take the red channel)
81 };
82 
83 enum class BmpCombine
84 {
85  Or, And
86 };
87 
88 class BitmapInfoAccess;
89 class BitmapReadAccess;
90 class BitmapWriteAccess;
91 class BitmapPalette;
92 class GDIMetaFile;
93 class AlphaMask;
94 class OutputDevice;
95 class SalBitmap;
96 
98 {
99  #if defined(_WIN32)
100  void* pDIB; // device independent byte buffer
101  #elif defined( MACOSX ) || defined( IOS )
102  // Nothing needed, apparently
103  #else
104  void* aPixmap;
105  #endif
106  int mnWidth;
107  int mnHeight;
108 };
109 
111 {
112 public:
113 
114  Bitmap();
115  Bitmap( const Bitmap& rBitmap );
116  Bitmap( const Size& rSizePixel, vcl::PixelFormat ePixelFormat, const BitmapPalette* pPal = nullptr );
117  explicit Bitmap( std::shared_ptr<SalBitmap> const & xSalBitmap );
118  virtual ~Bitmap();
119 
120  Bitmap& operator=( const Bitmap& rBitmap );
121  Bitmap& operator=( Bitmap&& rBitmap ) noexcept;
122  bool operator==( const Bitmap& rBitmap ) const;
123  bool operator!=( const Bitmap& rBitmap ) const { return !operator==(rBitmap); }
124 
125  inline bool IsEmpty() const;
126  void SetEmpty();
127 
128  inline const MapMode& GetPrefMapMode() const;
129  inline void SetPrefMapMode( const MapMode& rMapMode );
130 
131  inline const Size& GetPrefSize() const;
132  inline void SetPrefSize( const Size& rSize );
133 
134  Size GetSizePixel() const;
135 
136  vcl::PixelFormat getPixelFormat() const;
137  inline sal_uLong GetSizeBytes() const;
138  bool HasGreyPalette8Bit() const;
139  bool HasGreyPaletteAny() const;
147  bool GetSystemData( BitmapSystemData& rData ) const;
148 
149  BitmapChecksum GetChecksum() const;
150 
151  Bitmap CreateDisplayBitmap( OutputDevice* pDisplay ) const;
152 
153  static const BitmapPalette&
154  GetGreyPalette( int nEntries );
155 
156 public:
157 
165  bool Convert( BmpConversion eConversion );
166 
174  bool Dither();
175 
190  bool Crop( const tools::Rectangle& rRectPixel );
191 
207  bool Expand(
208  sal_uLong nDX, sal_uLong nDY,
209  const Color* pInitColor = nullptr );
210 
233  bool CopyPixel(
234  const tools::Rectangle& rRectDst,
235  const tools::Rectangle& rRectSrc,
236  const Bitmap* pBmpSrc = nullptr );
237 
238  bool CopyPixel_AlphaOptimized(
239  const tools::Rectangle& rRectDst,
240  const tools::Rectangle& rRectSrc,
241  const Bitmap* pBmpSrc );
242 
253  bool CombineSimple(
254  const Bitmap& rMask,
255  BmpCombine eCombine );
256 
273  bool Blend(
274  const AlphaMask& rAlpha,
275  const Color& rBackgroundColor );
276 
284  bool Erase( const Color& rFillColor );
285 
290  bool Invert();
291 
299  bool Mirror( BmpMirrorFlags nMirrorFlags );
300 
311  bool Scale( const Size& rNewSize, BmpScaleFlag nScaleFlag = BmpScaleFlag::Default );
312 
326  bool Scale( const double& rScaleX, const double& rScaleY, BmpScaleFlag nScaleFlag = BmpScaleFlag::Default );
327 
335  static bool HasFastScale();
336 
337  // Adapt the BitCount of rNew to BitCount of total, including grey or color palette
338  // Can be used to create alpha/mask bitmaps after their processing in 24bit
339  void AdaptBitCount(Bitmap& rNew) const;
340 
354  bool Rotate( Degree10 nAngle10, const Color& rFillColor );
355 
372  Bitmap CreateMask( const Color& rTransColor, sal_uInt8 nTol = 0 ) const;
373 
385  vcl::Region CreateRegion( const Color& rColor, const tools::Rectangle& rRect ) const;
386 
397  bool Replace( const Bitmap& rMask, const Color& rReplaceColor );
398 
409  bool Replace( const AlphaMask& rAlpha, const Color& rMergeColor );
410 
426  bool Replace( const Color& rSearchColor, const Color& rReplaceColor, sal_uInt8 nTol = 0 );
427 
446  bool Replace(
447  const Color* pSearchColors,
448  const Color* rReplaceColors,
449  size_t nColorCount,
450  sal_uInt8 const * pTols );
451 
467  void Vectorize(
468  GDIMetaFile& rMtf,
469  sal_uInt8 cReduce,
470  const Link<tools::Long,void>* pProgress );
471 
502  bool Adjust(
503  short nLuminancePercent,
504  short nContrastPercent = 0,
505  short nChannelRPercent = 0,
506  short nChannelGPercent = 0,
507  short nChannelBPercent = 0,
508  double fGamma = 1.0,
509  bool bInvert = false,
510  bool msoBrightness = false );
511 
512 public:
519  SAL_DLLPRIVATE void ReassignWithSize(const Bitmap& rBitmap);
520 
521  SAL_DLLPRIVATE void ImplMakeUnique();
522  const std::shared_ptr<SalBitmap>& ImplGetSalBitmap() const { return mxSalBmp; }
523  SAL_DLLPRIVATE void ImplSetSalBitmap( const std::shared_ptr<SalBitmap>& xImpBmp );
524 
525  SAL_DLLPRIVATE bool ImplMakeGreyscales();
526 
527 public:
528 
529  BitmapInfoAccess* AcquireInfoAccess();
530  BitmapReadAccess* AcquireReadAccess();
531  BitmapWriteAccess* AcquireWriteAccess();
532  static void ReleaseAccess( BitmapInfoAccess* pAccess );
533 
536 
537 private:
538  SAL_DLLPRIVATE bool ImplConvertUp(vcl::PixelFormat ePixelFormat, Color const* pExtColor = nullptr);
539  SAL_DLLPRIVATE bool ImplConvertDown8BPP(Color const* pExtColor = nullptr);
540 
541 private:
542  std::shared_ptr<SalBitmap> mxSalBmp;
545 };
546 
547 inline bool Bitmap::IsEmpty() const
548 {
549  return( mxSalBmp == nullptr );
550 }
551 
552 inline const MapMode& Bitmap::GetPrefMapMode() const
553 {
554  return maPrefMapMode;
555 }
556 
557 inline void Bitmap::SetPrefMapMode( const MapMode& rMapMode )
558 {
559  maPrefMapMode = rMapMode;
560 }
561 
562 inline const Size& Bitmap::GetPrefSize() const
563 {
564  return maPrefSize;
565 }
566 
567 inline void Bitmap::SetPrefSize( const Size& rSize )
568 {
569  maPrefSize = rSize;
570 }
571 
573 {
574  const auto aSizePixel = GetSizePixel();
575  const sal_uInt64 aBitCount = vcl::pixelFormatBitCount(getPixelFormat());
576  sal_uInt64 aSizeInBytes = (aSizePixel.Width() * aSizePixel.Height() * aBitCount) / 8;
577  return sal_uLong(aSizeInBytes);
578 }
579 
580 #endif // INCLUDED_VCL_BITMAP_HXX
581 
582 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PixelFormat
Pixel format of the bitmap in bits per pixel.
Definition: BitmapTypes.hxx:19
sal_uInt64 BitmapChecksum
Definition: checksum.hxx:30
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
const std::shared_ptr< SalBitmap > & ImplGetSalBitmap() const
Definition: bitmap.hxx:522
sal_uIntPtr sal_uLong
Size maPrefSize
Definition: bitmap.hxx:544
Size GetSizePixel() const
const Size & GetPrefSize() const
Definition: bitmap.hxx:562
bool operator!=(const Bitmap &rBitmap) const
Definition: bitmap.hxx:123
Point Rotate(const Point &rPoint, Degree10 nOrientation, const Point &rOrigin)
vcl::ScopedBitmapAccess< BitmapReadAccess, Bitmap,&Bitmap::AcquireReadAccess > ScopedReadAccess
Definition: bitmap.hxx:534
bool operator==(const ItalicMatrix &a, const ItalicMatrix &b)
Definition: vclenum.hxx:204
const MapMode & GetPrefMapMode() const
Definition: bitmap.hxx:552
void SetPrefMapMode(const MapMode &rMapMode)
Definition: bitmap.hxx:557
void * aPixmap
Definition: bitmap.hxx:104
vcl::PixelFormat getPixelFormat() const
sal_uLong GetSizeBytes() const
Definition: bitmap.hxx:572
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:303
BmpMirrorFlags
Definition: bitmap.hxx:42
void SetPrefSize(const Size &rSize)
Definition: bitmap.hxx:567
BmpScaleFlag
Definition: bitmap.hxx:54
BmpConversion
Definition: bitmap.hxx:71
#define SAL_WARN_UNUSED
unsigned char sal_uInt8
bool IsEmpty() const
Definition: bitmap.hxx:547
constexpr sal_uInt16 pixelFormatBitCount(PixelFormat ePixelFormat)
Definition: BitmapTypes.hxx:35
MapMode maPrefMapMode
Definition: bitmap.hxx:543
std::shared_ptr< SalBitmap > mxSalBmp
Definition: bitmap.hxx:542
BmpCombine
Definition: bitmap.hxx:83
vcl::ScopedBitmapAccess< BitmapInfoAccess, Bitmap,&Bitmap::AcquireInfoAccess > ScopedInfoAccess
Definition: bitmap.hxx:535