VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gxcolourmap.cpp
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 // gxcolourmap.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gxdef.h"
23 #include "gxcolourmap.h"
24 #include "gxdisplay.h"
25 #include "gxvisual.h"
26 #include "gxwindow.h"
27 #include "gxerror.h"
28 #include "grcolour16.h"
29 #include <sstream>
30 
31 GX::ColourMap::ColourMap( Display & display , bool greyscale256 ) :
32  m_display(display) ,
33  m_size(greyscale256?256U:16U)
34 {
35  m_map.reserve( m_size ) ;
36  GX::Window window( display , 1 , 1 ) ;
37  Visual default_visual( display ) ;
38  create( display , window.x() , default_visual.x() , greyscale256 ) ;
39 }
40 
41 GX::ColourMap::ColourMap( Display & display , GX::Window & window , ::Visual * visual ) :
42  m_display(display)
43 {
44  create( display , window.x() , visual , false ) ;
45 }
46 
47 void GX::ColourMap::create( Display & display , ::Window window , ::Visual * visual , bool greyscale256 )
48 {
49  m_x = XCreateColormap( display.x() , window , visual , AllocNone ) ;
50  if( greyscale256 )
51  setGrey256() ;
52  else
53  setColour16() ;
54 }
55 
56 unsigned int GX::ColourMap::size() const
57 {
58  return m_size ;
59 }
60 
61 void GX::ColourMap::setGrey256()
62 {
63  int n = 0 ;
64  for( int i = 0 ; i < 256 ; i++ , n += 257 )
65  {
66  XColor xcolour ;
67  xcolour.pixel = 0 ;
68  xcolour.flags = 0 ;
69  xcolour.red = n ;
70  xcolour.green = n ;
71  xcolour.blue = n ;
72  Status rc = XAllocColor( m_display.x() , m_x , &xcolour ) ;
73  if( rc == 0 ) throw Error( "XAllocColor" ) ;
74  m_map.push_back(xcolour.pixel) ;
75  }
76 }
77 
78 void GX::ColourMap::setColour16()
79 {
80  for( unsigned i = 0U ; i < 16U ; i++ )
81  addColour16( Gr::colour16::r(i) , Gr::colour16::g(i) , Gr::colour16::b(i) ) ;
82 }
83 
84 void GX::ColourMap::addColour16( int r , int g , int b )
85 {
86  XColor xcolour ;
87  xcolour.pixel = 0 ;
88  xcolour.flags = 0 ;
89  xcolour.red = 32761 * r ;
90  xcolour.green = 32761 * g ;
91  xcolour.blue = 32761 * b ;
92  Status rc = XAllocColor( m_display.x() , m_x , &xcolour ) ;
93  if( rc == 0 ) throw Error( "XAllocColor" ) ;
94  m_map.push_back(xcolour.pixel) ;
95 }
96 
98 {
99  try
100  {
101  XFreeColormap( m_display.x() , m_x ) ;
102  }
103  catch(...)
104  {
105  }
106 }
107 
108 ::Colormap GX::ColourMap::x()
109 {
110  return m_x ;
111 }
112 
113 int GX::ColourMap::find( unsigned long p ) const
114 {
115  int n = static_cast<int>(size()) ;
116  for( int i = 0 ; i < n ; i++ )
117  {
118  if( get(i) == p )
119  return i ;
120  }
121  std::ostringstream ss ;
122  ss << p << " not in" ;
123  for( int i = 0U ; i < n ; i++ )
124  {
125  ss << " " << get(i) ;
126  }
127  throw Error( "GX::ColourMap::find: invalid pixel value: " + ss.str() ) ;
128  return 0 ;
129 }
130 
131 /// \file gxcolourmap.cpp
int find(unsigned long) const
Does a reverse lookup to return the index value for the given pixel value.
A window class that is-a GX::Drawable and a GX::EventHandler.
Definition: gxwindow.h:47
::Display * x()
Returns the X object.
Definition: gxdisplay.cpp:53
An Xlib Display wrapper.
Definition: gxdisplay.h:38
An exception class for GX classes.
Definition: gxerror.h:37
::Window x()
Returns the X object.
Definition: gxwindow.cpp:149
~ColourMap()
Destructor.
Definition: gxcolourmap.cpp:97
An Xlib XVisual wrapper.
Definition: gxvisual.h:38
unsigned int size() const
Returns 16 for colours or 256 for greyscale.
Definition: gxcolourmap.cpp:56
::Visual * x()
Returns the X object.
Definition: gxvisual.cpp:61
ColourMap(Display &, bool greyscale256=false)
Constructor for a colourmap for the default visual providing 16 colours or 256 grey levels...
Definition: gxcolourmap.cpp:31
::Colormap x()
Returns the X colourmap id. See XCreateColormap(3).