PCL
Bitmap.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/Bitmap.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Bitmap_h
53 #define __PCL_Bitmap_h
54 
56 
57 #ifndef __PCL_BUILDING_PIXINSIGHT_APPLICATION
58 
59 #include <pcl/Defs.h>
60 
61 #include <pcl/Color.h>
63 #include <pcl/Rectangle.h>
64 #include <pcl/String.h>
65 #include <pcl/UIObject.h>
66 
67 #endif // __PCL_BUILDING_PIXINSIGHT_APPLICATION
68 
69 namespace pcl
70 {
71 
72 // ----------------------------------------------------------------------------
73 
95 namespace BitmapFormat
96 {
97  enum value_type
98  {
99  ARGB32, // AARRGGBB
100  RGB32, // ffRRGGBB
101  ARGB32_Premultiplied, // AARRGGBB with premultiplied RGB
102 
103  // ### TODO: Core version 1.8.x: Implement 16-bit bitmap formats.
104 // RGBA64, // RRRRGGGGBBBBAAAA
105 // RGBA64, // RRRRGGGGBBBBffff
106 // RGBA64_Premultiplied, // RRRRGGGGBBBBAAAA with premultiplied RGB
107 
108  NumberOfBitmapFormats,
109 
110  Invalid = -1
111  };
112 }
113 
114 // ----------------------------------------------------------------------------
115 
135 namespace SVGRenderOption
136 {
137  enum mask_type
138  {
139  IgnoreAspectRatio = 0x00000000, // Render freely to fill the specified rectangular area entirely, ignoring aspect ratio.
140  PreserveAspectRatio = 0x00000001, // Render within the specified rectangular area, preserving the original aspect ratio of the SVG drawing.
141  Fast = 0x00000000, // Render with a fast, thread-safe implementation that supports most of the SVG 1.1 specification.
142  HighQuality = 0x00000010, // Render with a high-quality, thread-unsafe implementation supporting the entire SVG 1.1 specification.
143  Default = PreserveAspectRatio|Fast
144  };
145 };
146 
152 using SVGRenderOptions = Flags<SVGRenderOption::mask_type>;
153 
154 // ----------------------------------------------------------------------------
155 
156 #ifndef __PCL_BUILDING_PIXINSIGHT_APPLICATION
157 
158 class PCL_CLASS ImageVariant;
159 
160 // ----------------------------------------------------------------------------
161 
203 class PCL_CLASS Bitmap : public UIObject
204 {
205 public:
206 
211  using pixel_format = BitmapFormat::value_type;
212 
218  using display_channel = DisplayChannel::value_type;
219 
225  using mask_mode = MaskMode::value_type;
226 
231 
239  Bitmap( int width, int height, pixel_format fmt = BitmapFormat::ARGB32 );
240 
251  Bitmap( const char** xpm );
252 
268  Bitmap( const String& filePath );
269 
298  Bitmap( const char* filePath );
299 
304  Bitmap( const IsoString& filePath );
305 
342  Bitmap( const void* data, size_type size, const char* format = "SVG", uint32 flags = 0 );
343 
352  Bitmap( const Bitmap& bmp, const pcl::Rect& r );
353 
366  Bitmap( const Bitmap& bmp, int x0, int y0, int x1, int y1 );
367 
372  Bitmap( const Bitmap& bmp ) : UIObject( bmp )
373  {
374  }
375 
379  Bitmap( Bitmap&& x ) : UIObject( std::move( x ) )
380  {
381  }
382 
392  ~Bitmap() override
393  {
394  }
395 
403  Bitmap& operator =( const Bitmap& bmp )
404  {
405  Assign( bmp );
406  return *this;
407  }
408 
412  Bitmap& operator =( Bitmap&& x )
413  {
414  Transfer( x );
415  return *this;
416  }
417 
422  static Bitmap& Null();
423 
430  pixel_format PixelFormat() const;
431 
439  void SetPixelFormat( pixel_format fmt );
440 
504  static Bitmap Render( const ImageVariant& image,
505  int zoom = 1, display_channel displayChannel = DisplayChannel::RGBK, bool transparency = true,
506  const ImageVariant* mask = nullptr, mask_mode maskMode = MaskMode::Default, bool maskInverted = false,
507  const uint8** LUT = nullptr, bool fastDownsample = true, bool (*callback)() = nullptr );
508 
512  void GetDimensions( int& width, int& height ) const;
513 
517  int Width() const
518  {
519  int w, dum; GetDimensions( w, dum ); return w;
520  }
521 
525  int Height() const
526  {
527  int dum, h; GetDimensions( dum, h ); return h;
528  }
529 
537  {
538  int w, h; GetDimensions( w, h ); return pcl::Rect( w, h );
539  }
540 
545  bool IsEmpty() const;
546 
553  operator bool() const
554  {
555  return !IsEmpty();
556  }
557 
570  RGBA Pixel( int x, int y ) const;
571 
581  RGBA Pixel( const pcl::Point& p ) const
582  {
583  return Pixel( p.x, p.y );
584  }
585 
598  void SetPixel( int x, int y, RGBA v );
599 
613  void SetPixel( const pcl::Point& p, RGBA v )
614  {
615  SetPixel( p.x, p.y, v );
616  }
617 
627  const RGBA* ScanLine( int y ) const;
628 
639  RGBA* ScanLine( int y );
640 
651  Bitmap Mirrored() const;
652 
660 
668 
684  Bitmap Scaled( double sx, double sy, bool precise = true ) const;
685 
697  Bitmap Scaled( double sxy, bool precise = true ) const;
698 
712  Bitmap ScaledToWidth( int width, bool precise = true ) const;
713 
727  Bitmap ScaledToHeight( int height, bool precise = true ) const;
728 
743  Bitmap ScaledToSize( int width, int height, bool precise = true ) const;
744 
756  Bitmap Rotated( double angle, bool precise = true ) const;
757 
792  void Load( const String& filePath );
793 
825  void Save( const String& filePath, int quality = -1 ) const;
826 
845  void Load( const void* data, size_type size, const char* format = "SVG", uint32 flags = 0 );
846 
859  Bitmap Subimage( int x0, int y0, int x1, int y1 ) const;
860 
869  Bitmap Subimage( const pcl::Rect& r ) const
870  {
871  return Subimage( r.x0, r.y0, r.x1, r.y1 );
872  }
873 
896  void Copy( const pcl::Point& p, const Bitmap& src, const pcl::Rect& r );
897 
917  void Copy( const pcl::Point& p, const Bitmap& src )
918  {
919  Copy( p, src, src.Bounds() );
920  }
921 
938  void Copy( const Bitmap& src )
939  {
940  Copy( pcl::Point( 0 ), src );
941  }
942 
951  void Fill( const pcl::Rect& rect, RGBA fillWith );
952 
961  void Fill( RGBA fillWith )
962  {
963  Fill( Bounds(), fillWith );
964  }
965 
976  void Or( const pcl::Rect& rect, RGBA orWith );
977 
989  void Or( RGBA orWith )
990  {
991  Or( Bounds(), orWith );
992  }
993 
1014  void Or( const pcl::Point& p, const Bitmap& src, const pcl::Rect& r );
1015 
1037  void Or( const pcl::Point& p, const Bitmap& src )
1038  {
1039  Or( p, src, src.Bounds() );
1040  }
1041 
1051  void Or( const Bitmap& src )
1052  {
1053  Or( pcl::Point( 0 ), src );
1054  }
1055 
1066  void And( const pcl::Rect& rect, RGBA andWith );
1067 
1079  void And( RGBA andWith )
1080  {
1081  And( Bounds(), andWith );
1082  }
1083 
1104  void And( const pcl::Point& p, const Bitmap& src, const pcl::Rect& r );
1105 
1127  void And( const pcl::Point& p, const Bitmap& src )
1128  {
1129  And( p, src, src.Bounds() );
1130  }
1131 
1141  void And( const Bitmap& src )
1142  {
1143  And( pcl::Point( 0 ), src );
1144  }
1145 
1156  void Xor( const pcl::Rect& rect, RGBA xorWith );
1157 
1169  void Xor( RGBA xorWith )
1170  {
1171  Xor( Bounds(), xorWith );
1172  }
1173 
1194  void Xor( const pcl::Point& p, const Bitmap& src, const pcl::Rect& r );
1195 
1217  void Xor( const pcl::Point& p, const Bitmap& src )
1218  {
1219  Xor( p, src, src.Bounds() );
1220  }
1221 
1231  void Xor( const Bitmap& src )
1232  {
1233  Xor( pcl::Point( 0 ), src );
1234  }
1235 
1251  void XorRect( const pcl::Rect& rect, RGBA xorWith );
1252 
1266  void XorRect( RGBA xorWith )
1267  {
1268  XorRect( Bounds(), xorWith );
1269  }
1270 
1283  void Invert( const pcl::Rect& rect )
1284  {
1285  Xor( rect, 0x00ffffff );
1286  }
1287 
1298  void Invert()
1299  {
1300  Invert( Bounds() );
1301  }
1302 
1314  void InvertRect( const pcl::Rect& rect )
1315  {
1316  XorRect( rect, 0x00ffffff );
1317  }
1318 
1329  void InvertRect()
1330  {
1331  InvertRect( Bounds() );
1332  }
1333 
1348  void ReplaceColor( const pcl::Rect& rect, RGBA replaceThis, RGBA replaceWith );
1349 
1363  void ReplaceColor( RGBA replaceThis, RGBA replaceWith )
1364  {
1365  ReplaceColor( Bounds(), replaceThis, replaceWith );
1366  }
1367 
1377  void SetAlpha( const pcl::Rect& rect, uint8 newAlpha );
1378 
1391  void SetAlpha( uint8 newAlpha )
1392  {
1393  SetAlpha( Bounds(), newAlpha );
1394  }
1395 
1404  double PhysicalPixelRatio() const;
1405 
1424  void SetPhysicalPixelRatio( double ratio );
1425 
1471  static Bitmap FromSVGFile( const String& filePath, int width = 0, int height = 0,
1472  SVGRenderOptions options = SVGRenderOption::Default );
1473 
1492  static Bitmap FromSVG( const IsoString& svgSource, int width = 0, int height = 0,
1493  SVGRenderOptions options = SVGRenderOption::Default );
1494 
1495 private:
1496 
1497  Bitmap( void* h ) : UIObject( h )
1498  {
1499  }
1500 
1501  void* CloneHandle() const override;
1502 
1503  friend class GraphicsContextBase;
1504  friend class Graphics;
1505  friend class VectorGraphics;
1506  friend class Action;
1507  friend class Brush;
1508  friend class Cursor;
1509  friend class Button;
1510  friend class PushButton;
1511  friend class ToolButton;
1512  friend class BitmapBox;
1513  friend class TabBox;
1514  friend class ComboBox;
1515  friend class TreeBox;
1516  friend class ImageView;
1517  friend class ImageWindow;
1518  friend class Process;
1519  friend class FileFormat;
1520 };
1521 
1522 // ----------------------------------------------------------------------------
1523 
1524 #endif // __PCL_BUILDING_PIXINSIGHT_APPLICATION
1525 
1526 } // pcl
1527 
1528 #endif // __PCL_Bitmap_h
1529 
1530 // ----------------------------------------------------------------------------
1531 // EOF pcl/Bitmap.h - Released 2024-06-18T15:48:54Z
Client-side interface to a PixInsight module-defined action object.
Definition: Action.h:174
Client-side interface to a PixInsight BitmapBox control.
Definition: BitmapBox.h:76
Client-side interface to a PixInsight Bitmap object.
Definition: Bitmap.h:204
pcl::Rect Bounds() const
Definition: Bitmap.h:536
~Bitmap() override
Definition: Bitmap.h:392
void Load(const void *data, size_type size, const char *format="SVG", uint32 flags=0)
void GetDimensions(int &width, int &height) const
Bitmap Subimage(const pcl::Rect &r) const
Definition: Bitmap.h:869
static Bitmap FromSVG(const IsoString &svgSource, int width=0, int height=0, SVGRenderOptions options=SVGRenderOption::Default)
pixel_format PixelFormat() const
int Height() const
Definition: Bitmap.h:525
Bitmap(const char *filePath)
Bitmap(int width, int height, pixel_format fmt=BitmapFormat::ARGB32)
Bitmap(const void *data, size_type size, const char *format="SVG", uint32 flags=0)
Bitmap(const IsoString &filePath)
Bitmap(Bitmap &&x)
Definition: Bitmap.h:379
bool IsEmpty() const
Bitmap Subimage(int x0, int y0, int x1, int y1) const
static Bitmap & Null()
Bitmap(const Bitmap &bmp, const pcl::Rect &r)
void SetPixelFormat(pixel_format fmt)
static Bitmap FromSVGFile(const String &filePath, int width=0, int height=0, SVGRenderOptions options=SVGRenderOption::Default)
Bitmap(const char **xpm)
static Bitmap Render(const ImageVariant &image, int zoom=1, display_channel displayChannel=DisplayChannel::RGBK, bool transparency=true, const ImageVariant *mask=nullptr, mask_mode maskMode=MaskMode::Default, bool maskInverted=false, const uint8 **LUT=nullptr, bool fastDownsample=true, bool(*callback)()=nullptr)
Bitmap(const String &filePath)
Bitmap(const Bitmap &bmp, int x0, int y0, int x1, int y1)
Bitmap(const Bitmap &bmp)
Definition: Bitmap.h:372
int Width() const
Definition: Bitmap.h:517
Client-side interface to a PixInsight Brush object.
Definition: Brush.h:158
Abstract base class for all client-side interfaces to PixInsight button controls.
Definition: Button.h:100
Client-side interface to a PixInsight ComboBox control.
Definition: ComboBox.h:77
Client-side interface to a PixInsight Cursor object.
Definition: Cursor.h:168
High-level interface to an installed image file format.
Definition: FileFormat.h:98
A type-safe collection of enumerated flags.
Definition: Flags.h:85
A generic point in the two-dimensional space.
Definition: Point.h:100
component x
Abscissa (horizontal, or X-axis coordinate).
Definition: Point.h:111
component y
Ordinate (vertical, or Y-axis coordinate).
Definition: Point.h:112
A generic rectangle in the two-dimensional space.
Definition: Rectangle.h:314
component x1
Horizontal coordinate of the lower right corner.
Definition: Rectangle.h:334
component y1
Vertical coordinate of the lower right corner.
Definition: Rectangle.h:335
component y0
Vertical coordinate of the upper left corner.
Definition: Rectangle.h:333
component x0
Horizontal coordinate of the upper left corner.
Definition: Rectangle.h:332
Base class of client-side interface classes to PixInsight graphics contexts
Definition: Graphics.h:208
Client-side interface to a PixInsight graphics context object.
Definition: Graphics.h:646
Acts like a union for all types of images in PCL, with optional class-wide ownership of transported i...
Definition: ImageVariant.h:322
Client-side interface to a PixInsight ImageView control.
Definition: ImageView.h:80
High-level interface to an image window object in the PixInsight core application.
Definition: ImageWindow.h:287
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5425
High-level interface to an installed process.
Definition: Process.h:99
Client-side interface to a PixInsight PushButton control.
Definition: PushButton.h:75
A collection of rendering options for raster image representation of SVG documents.
Definition: Bitmap.h:145
Unicode (UTF-16) string.
Definition: String.h:8113
Client-side interface to a PixInsight TabBox control.
Definition: TabBox.h:97
Client-side interface to a PixInsight ToolButton control.
Definition: ToolButton.h:75
Client-side interface to a PixInsight TreeBox control.
Definition: TreeBox.h:81
Root base class for all user interface objects.
Definition: UIObject.h:95
Client-side interface to a PixInsight vector graphics context.
Definition: Graphics.h:1463
Bitmap ScaledToHeight(int height, bool precise=true) const
Bitmap ScaledToSize(int width, int height, bool precise=true) const
Bitmap ScaledToWidth(int width, bool precise=true) const
Bitmap Rotated(double angle, bool precise=true) const
Bitmap Scaled(double sxy, bool precise=true) const
Bitmap Scaled(double sx, double sy, bool precise=true) const
void InvertRect(const pcl::Rect &rect)
Definition: Bitmap.h:1314
void And(RGBA andWith)
Definition: Bitmap.h:1079
void And(const pcl::Point &p, const Bitmap &src, const pcl::Rect &r)
void And(const pcl::Point &p, const Bitmap &src)
Definition: Bitmap.h:1127
void Xor(const pcl::Point &p, const Bitmap &src)
Definition: Bitmap.h:1217
void Or(RGBA orWith)
Definition: Bitmap.h:989
void And(const Bitmap &src)
Definition: Bitmap.h:1141
void Xor(const pcl::Rect &rect, RGBA xorWith)
void Or(const pcl::Point &p, const Bitmap &src, const pcl::Rect &r)
void Copy(const Bitmap &src)
Definition: Bitmap.h:938
void XorRect(const pcl::Rect &rect, RGBA xorWith)
void Or(const Bitmap &src)
Definition: Bitmap.h:1051
void InvertRect()
Definition: Bitmap.h:1329
void Xor(const pcl::Point &p, const Bitmap &src, const pcl::Rect &r)
void Copy(const pcl::Point &p, const Bitmap &src, const pcl::Rect &r)
void Invert()
Definition: Bitmap.h:1298
void Copy(const pcl::Point &p, const Bitmap &src)
Definition: Bitmap.h:917
void Invert(const pcl::Rect &rect)
Definition: Bitmap.h:1283
void Xor(RGBA xorWith)
Definition: Bitmap.h:1169
void Fill(RGBA fillWith)
Definition: Bitmap.h:961
void Fill(const pcl::Rect &rect, RGBA fillWith)
void Xor(const Bitmap &src)
Definition: Bitmap.h:1231
void XorRect(RGBA xorWith)
Definition: Bitmap.h:1266
void Or(const pcl::Rect &rect, RGBA orWith)
void And(const pcl::Rect &rect, RGBA andWith)
void Or(const pcl::Point &p, const Bitmap &src)
Definition: Bitmap.h:1037
void ReplaceColor(const pcl::Rect &rect, RGBA replaceThis, RGBA replaceWith)
void SetAlpha(const pcl::Rect &rect, uint8 newAlpha)
void SetAlpha(uint8 newAlpha)
Definition: Bitmap.h:1391
void ReplaceColor(RGBA replaceThis, RGBA replaceWith)
Definition: Bitmap.h:1363
void Load(const String &filePath)
void Save(const String &filePath, int quality=-1) const
RGBA Pixel(const pcl::Point &p) const
Definition: Bitmap.h:581
RGBA * ScanLine(int y)
RGBA Pixel(int x, int y) const
void SetPixel(int x, int y, RGBA v)
const RGBA * ScanLine(int y) const
void SetPixel(const pcl::Point &p, RGBA v)
Definition: Bitmap.h:613
Bitmap MirroredHorizontally() const
Bitmap MirroredVertically() const
Bitmap Mirrored() const
unsigned char uint8
Definition: Defs.h:642
unsigned int uint32
Definition: Defs.h:666
void SetAlpha(RGBA &rgba, uint8 a)
Definition: Color.h:180
uint32 RGBA
Definition: Color.h:92
size_t size_type
Definition: Defs.h:609
double PhysicalPixelRatio() const
void SetPhysicalPixelRatio(double ratio)
PCL root namespace.
Definition: AbstractImage.h:77