VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gxcanvas.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gxcanvas.h
19 ///
20 
21 #ifndef GX_CANVAS_H
22 #define GX_CANVAS_H
23 
24 #include "gdef.h"
25 #include "gxdef.h"
26 #include "gxcolourmap.h"
27 #include "gxwindow.h"
28 #include "gximage.h"
29 #include "gxpixmap.h"
30 #include "gxcontext.h"
31 #include "grcolour.h"
32 #include <string>
33 
34 namespace GX
35 {
36  class Canvas ;
37  class CanvasImp ;
38  class Window ;
39 }
40 
41 /// \class GX::Canvas
42 /// A drawing surface that is embedded in a window.
43 ///
44 /// Supports server-side and client-side implementations chosen at run-time.
45 /// Client-side uses a local image that is transferred to the X-server by the
46 /// blit() method; server-side uses a pixmap that lives on the X-server and
47 /// blit() is a copy performed within the X-server. Client-side should be
48 /// preferred for efficient reading of pixels and/or for dense images;
49 /// server-side might be better for sparse images.
50 ///
51 /// Optionally uses a palette for ancient display hardware; either 16-value
52 /// bios colours, or 256 greyscale shades. If using a palette then all
53 /// colours passed to point(), line() etc. should be from colour().
54 ///
55 /// The point (0,0) is in the bottom left hand corner and (dx()-1,dy()-1) is
56 /// the top right.
57 ///
58 /// A stereo mode can be set so that colours are modified to red or green,
59 /// with (if client-side) yellow pixels on intersection.
60 ///
62 {
63 public:
64  explicit Canvas( GX::Window & , int dx = 0 , int dy = 0 , int colours = 0 , bool server_side = false ) ;
65  ///< Constructor. By default the size of the canvas is the size
66  ///< of the window. The colours parameter can be zero, 16 or 256.
67 
68  ~Canvas() ;
69  ///< Destructor.
70 
71  int dx() const ;
72  ///< Returns the canvas width in pixels.
73 
74  int dy() const ;
75  ///< Returns the canvas height in pixels.
76 
77  std::pair<unsigned int,unsigned int> aspect() const ;
78  ///< Returns the canvas aspect ratio as a fraction (normally about 0.7).
79 
80  int colours() const ;
81  ///< Returns the number of colours, as passed in to the ctor.
82 
83  Gr::Colour colour( unsigned int n ) const ;
84  ///< Returns a colour from the palette.
85  ///< Precondition: n < colours
86 
87  Gr::Colour black() const ;
88  ///< Returns black.
89 
90  Gr::Colour white() const ;
91  ///< Returns white.
92 
93  void text( const std::string & s , int x , int y , Gr::Colour c ) ;
94  ///< Draws a single-line text string at the given position.
95 
96  void point( int x , int y , Gr::Colour c ) ;
97  ///< Draws a single pixel.
98 
99  bool fastable() const ;
100  ///< Returns true if fastpoint() can be used rather than point().
101 
102  void fastpoint( int x , int y , Gr::Colour c ) ;
103  ///< Draws a pixel quickly, assuming a client-side image and no stereo.
104  ///< Precondition: fastable()
105 
106  void line( int x0 , int y0 , int x1 , int y1 , Gr::Colour c ) ;
107  ///< Draws a line.
108 
109  void lineAcross( int y , int x_first , int x_last , const Gr::Colour & c ) ;
110  ///< Draws a horizontal line.
111 
112  void lineDown( int x , int y_first , int y_last , const Gr::Colour & c ) ;
113  ///< Draws a vertical line.
114 
115  Gr::Colour readPoint( int x , int y ) const ;
116  ///< Reads a pixel. This method might be very slow and inefficient for
117  ///< a server-side canvas. If using a palette then the palette index
118  ///< will be in the red component.
119 
120  void clear( bool white = false ) ;
121  ///< Clears the canvas to black (or white).
122 
123  void blit() ;
124  ///< Blits the canvas contents to the window.
125 
126  enum Stereo { left , right , normal } ;
127  void stereo( Stereo mode = normal ) ;
128  ///< Sets the left/right stereo mode for subsequent line() and
129  ///< point() drawing operations.
130 
131 private:
132  Canvas( const Canvas & ) ;
133  void operator=( const Canvas & ) ;
134  unsigned long pixel( const Gr::Colour & c_in ) const ;
135  unsigned long pixel( const Gr::Colour & c_in , Stereo stereo_mode ) const ;
136 
137 private:
138  GX::Window & m_window ;
139  int m_dx ;
140  int m_dy ;
141  int m_colours ; // 0=>rgb, 16=>biospalette, 256=>greyscale
142  bool m_client_side ;
143  unique_ptr<GX::ColourMap> m_colour_map ;
144  unsigned int m_aspect_top ;
145  unsigned int m_aspect_bottom ;
146  GX::Pixmap m_pixmap ;
147  GX::Image m_image ;
148  GX::Context m_context ;
149  Canvas::Stereo m_stereo_mode ;
150 } ;
151 
152 inline
153 void GX::Canvas::fastpoint( int x , int y , Gr::Colour c )
154 {
155  m_image.drawFastPoint( x , m_dy-1-y , c.cc() ) ;
156 }
157 
158 #endif
Gr::Colour colour(unsigned int n) const
Returns a colour from the palette.
Definition: gxcanvas.cpp:84
A window class that is-a GX::Drawable and a GX::EventHandler.
Definition: gxwindow.h:47
A class for xclient-side images that are drawn locally and then blitted to the xserver.
Definition: gximage.h:41
void fastpoint(int x, int y, Gr::Colour c)
Draws a pixel quickly, assuming a client-side image and no stereo.
Definition: gxcanvas.h:153
void point(int x, int y, Gr::Colour c)
Draws a single pixel.
Definition: gxcanvas.cpp:161
void lineDown(int x, int y_first, int y_last, const Gr::Colour &c)
Draws a vertical line.
Definition: gxcanvas.cpp:197
Canvas(GX::Window &, int dx=0, int dy=0, int colours=0, bool server_side=false)
Constructor.
Definition: gxcanvas.cpp:36
void clear(bool white=false)
Clears the canvas to black (or white).
Definition: gxcanvas.cpp:234
void blit()
Blits the canvas contents to the window.
Definition: gxcanvas.cpp:248
void lineAcross(int y, int x_first, int x_last, const Gr::Colour &c)
Draws a horizontal line.
Definition: gxcanvas.cpp:189
A simple rgb colour structure.
Definition: grcolour.h:36
std::pair< unsigned int, unsigned int > aspect() const
Returns the canvas aspect ratio as a fraction (normally about 0.7).
Definition: gxcanvas.cpp:79
~Canvas()
Destructor.
Definition: gxcanvas.cpp:60
int colours() const
Returns the number of colours, as passed in to the ctor.
Definition: gxcanvas.cpp:74
A pixmap class for xserver-side images.
Definition: gxpixmap.h:41
bool fastable() const
Returns true if fastpoint() can be used rather than point().
Definition: gxcanvas.cpp:153
Gr::Colour white() const
Returns white.
Definition: gxcanvas.cpp:95
int dx() const
Returns the canvas width in pixels.
Definition: gxcanvas.cpp:64
Gr::Colour readPoint(int x, int y) const
Reads a pixel.
Definition: gxcanvas.cpp:205
An Xlib GC wrapper.
Definition: gxcontext.h:37
void line(int x0, int y0, int x1, int y1, Gr::Colour c)
Draws a line.
Definition: gxcanvas.cpp:175
void drawFastPoint(int x, int y, unsigned long p)
Draws a point fast.
Definition: gximage.h:127
void text(const std::string &s, int x, int y, Gr::Colour c)
Draws a single-line text string at the given position.
Definition: gxcanvas.cpp:117
A drawing surface that is embedded in a window.
Definition: gxcanvas.h:61
cc_t cc() const
Returns a combined-component value that incorporates the r(), g() and b() values. ...
Definition: grcolour.h:106
int dy() const
Returns the canvas height in pixels.
Definition: gxcanvas.cpp:69
Gr::Colour black() const
Returns black.
Definition: gxcanvas.cpp:90
void stereo(Stereo mode=normal)
Sets the left/right stereo mode for subsequent line() and point() drawing operations.
Definition: gxcanvas.cpp:256