GLPixelCanvasColorTable.h

Go to the documentation of this file.
00001 //# GLPixelCanvasColorTable.h: color table provision for X11 devices
00002 //# Copyright (C) 2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 
00028 #ifndef TRIALDISPLAY_GLPIXELCANVASCOLORTABLE_H
00029 #define TRIALDISPLAY_GLPIXELCANVASCOLORTABLE_H
00030 
00031 #include <casa/aips.h>
00032 #include <graphics/X11/X11Util.h>
00033 
00034 #include <graphics/X11/X_enter.h>
00035 #include <X11/Xlib.h>
00036 #include <X11/Xutil.h>
00037 #include <graphics/X11/X_exit.h>
00038 
00039 #include <casa/Arrays/Vector.h>
00040 #include <display/Display/DisplayEnums.h>
00041 #include <display/Display/PixelCanvasColorTable.h>
00042 
00043 
00044 namespace casa { //# NAMESPACE CASA - BEGIN
00045 
00046 // <summary>
00047 // Implementation of PixelCanvasColorTable for OpenGL.
00048 // </summary>
00049 // <synopsis>
00050 // GLPixelCanvasColorTable is an X11PixelCanvasColorTable that has been
00051 // modified to work within the constraints of the GLPixelCanvas. (e.g. Not
00052 // assuming the default visual).
00053 //
00054 // Colormap values are written to a virtual colortable (an internal array).
00055 // For Indexed (PseudoColor) windows, the values are sent to the hardware
00056 // colormap. For RGB (TrueColor) windows, the virtual colortable is used
00057 // to emulate a hardware lookup table.
00058 // </synopsis>
00059 //
00060 // <prerequisite>
00061 // <li> <linkto class="PixelCanvasColorTable">PixelCanvasColorTable</linkto>
00062 // <li> How to get the X Screen pointer.
00063 // </prerequisite>
00064 //
00065 // <etymology>
00066 // </etymology>
00067 //
00068 // <thrown>
00069 //    AipsError
00070 // </thrown>
00071 // <use visibility=export>
00072 
00073 // Colormap entry for the virtual colormap.
00074         class GLVColorTableEntry {
00075         public:
00076                 GLVColorTableEntry();
00077 
00078                 void operator=(const GLVColorTableEntry &);
00079 
00080                 // Store/get the values.
00081                 void put(const float red, const float green, const float blue) {
00082                         red_ = red;
00083                         green_ = green;
00084                         blue_ = blue;
00085                 }
00086 
00087                 void get(float &red, float &green, float &blue)const {
00088                         red = red_;
00089                         green = green_;
00090                         blue = blue_;
00091                 }
00092 
00093                 uInt    getIndex()const {
00094                         return index_;
00095                 }
00096                 void    setIndex(const uInt i) {
00097                         index_ = i;
00098                 }
00099 
00100                 uLong getPixel()const {
00101                         return pixel_;
00102                 }
00103                 void setPixel(const unsigned long p) {
00104                         pixel_ = p;
00105                 }
00106 
00107                 friend ostream &operator<<(ostream &s, const GLVColorTableEntry &x);
00108 
00109         private:
00110                 uInt    index_;                 // Index into map for this entry.
00111                 uLong   pixel_;                 // Real index or color value.
00112                 float   red_, green_, blue_;    // Color component values. (0..1).
00113         };
00114 
00115         class GLPixelCanvasColorTable : public PixelCanvasColorTable {
00116         public:
00117 
00118                 // <group>
00119                 GLPixelCanvasColorTable();
00120 
00121                 ~GLPixelCanvasColorTable();
00122 
00123                 GLPixelCanvasColorTable(::XDisplay *dpy,
00124                                         Display::ColorModel mapType = Display::RGB,
00125                                         Float percent=90.0,
00126                                         XVisualInfo *visInfo=NULL);
00127                 // </group>
00128 
00129                 // allocate cells for colormaps
00130                 Bool allocCells(uInt nCells);
00131 
00132                 // deallocate cells for colormaps
00133                 Bool deallocCells();
00134 
00135                 // Allocate the best color cube given the map
00136                 Bool allocColorCube();
00137                 // Allocate a color cube of a specific size
00138                 Bool allocColorCube(uInt n1, uInt n2, uInt n3);
00139                 // Allocate a color cube within the ranges of sizes
00140                 Bool allocColorCubeMinMax(uInt n1min, uInt n2min, uInt n3min,
00141                                           uInt n1max, uInt n2max, uInt n3max);
00142                 // Copy color cube info from the mapRef
00143                 void copyColorCube(const GLPixelCanvasColorTable & mapRef);
00144                 // Fill a color cube with an RGB spectrum
00145                 void fillColorCubeRGB();
00146                 enum FILLMODE {FILLRGB, FILLRBG, FILLGRB, FILLGBR, FILLBRG, FILLBGR};
00147                 static void colorFillRGB(
00148                     Vector<Float> &r, Vector<Float> &g, Vector<Float> &b,
00149                     uInt nr, uInt ng, uInt nb,
00150                     FILLMODE mode = FILLRGB);
00151 
00152                 static void colorFillRGB(
00153                     Vector<Float> &r, Vector<Float> &g, Vector<Float> &b,
00154                     uInt ncolors, FILLMODE mode= FILLRGB);
00155                 // Fill a color cube with an HSV spectrum
00156                 void fillColorCubeHSV();
00157 
00158                 // Merge separate channel data into an output image.
00159                 // This function maps floating values between 0 and 1
00160                 // into a output image suitable for PixelCanvas::drawImage().
00161                 // <group>
00162                 void mapToColor3(Array<uLong> & out,
00163                                  const Array<Float> & chan1in,
00164                                  const Array<Float> & chan2in,
00165                                  const Array<Float> & chan3in);
00166                 void mapToColor3(Array<uLong> & out,
00167                                  const Array<Double> & chan1in,
00168                                  const Array<Double> & chan2in,
00169                                  const Array<Double> & chan3in);
00170                 // </group>
00171 
00172                 // This one maps values between 0 and the integer
00173                 // maximum value for each channel into a single
00174                 // output image suitable for PixelCanvas::drawImage().
00175                 // <group>
00176                 void mapToColor3(Array<uLong> & out,
00177                                  const Array<uShort> & chan1in,
00178                                  const Array<uShort> & chan2in,
00179                                  const Array<uShort> & chan3in);
00180                 void mapToColor3(Array<uLong> & out,
00181                                  const Array<uInt> & chan1in,
00182                                  const Array<uInt> & chan2in,
00183                                  const Array<uInt> & chan3in);
00184                 // </group>
00185 
00186                 // (Multichannel Color)
00187                 // Transform arrays from the passed color model into
00188                 // the colormodel of the XPCCT.
00189                 // Does nothing if colorModel is Display::Index.
00190                 // It is assumed that input arrays are in the range of [0,1]
00191                 Bool colorSpaceMap(Display::ColorModel,
00192                                    const Array<Float> & chan1in,
00193                                    const Array<Float> & chan2in,
00194                                    const Array<Float> & chan3in,
00195                                    Array<Float> & chan1out,
00196                                    Array<Float> & chan2out,
00197                                    Array<Float> & chan3out);
00198 
00199                 // map [0,N-1] into colorpixels, where N is the current colormap size
00200                 // The values are returned as unsigned integers in their respective
00201                 // array.
00202                 // <note role="warning">uChar type may not have enough bits
00203                 // to hold the pixel index on some high-end graphics systems </note>
00204                 // <note role="warning">uShort type may not have enough bits
00205                 // to hold the pixel index on some high-end graphics systems </note>
00206                 // <group>
00207                 void mapToColor(const Colormap * map, Array<uChar> & outArray,
00208                                 const Array<uChar> & inArray, Bool rangeCheck = True) const;
00209                 void mapToColor(const Colormap * map, Array<uShort> & outArray,
00210                                 const Array<uShort> & inArray, Bool rangeCheck = True) const;
00211                 void mapToColor(const Colormap * map, Array<uInt> & outArray,
00212                                 const Array<uInt> & inArray, Bool rangeCheck = True) const;
00213                 void mapToColor(const Colormap * map, Array<uLong> & outArray,
00214                                 const Array<uLong> & inArray, Bool rangeCheck = True) const;
00215                 // </group>
00216 
00217                 // same as above except the matrix is operated on in place.  Only unsigned
00218                 // values make sense here.
00219                 // <group>
00220                 void mapToColor(const Colormap * map, Array<uChar> & inOutArray,
00221                                 Bool rangeCheck = True) const;
00222                 void mapToColor(const Colormap * map, Array<uShort> & inOutArray,
00223                                 Bool rangeCheck = True) const;
00224                 void mapToColor(const Colormap * map, Array<uInt> & inOutArray,
00225                                 Bool rangeCheck = True) const;
00226                 void mapToColor(const Colormap * map, Array<uLong> & inOutArray,
00227                                 Bool rangeCheck = True) const;
00228                 // </group>
00229 
00230                 // print details of class to ostream
00231                 friend ostream & operator << (ostream & os, const GLPixelCanvasColorTable & pcc);
00232 
00233                 // Is the hardware colormap resizeable?  ie. is it write-only?
00234                 virtual Bool staticSize() {
00235                         return (readOnly_ && decomposedIndex_);
00236                 }
00237 
00238                 // resize the map if allowed.  Returns True if resize was accepted
00239                 // <group>
00240                 Bool resize(uInt newSize);
00241                 Bool resize(uInt nReds, uInt nGreens, uInt nBlues);
00242                 // </group>
00243 
00244                 // Install colors into the color table. Offset is zero-based. Colors
00245                 // are installed into the PixelCanvasColorTable until the Arrays run out
00246                 // or until the end of the colortable is reached.  This only has an
00247                 // effect if the ColorModel is Index.  Values are clamped to [0.0,1.0].
00248                 Bool installRGBColors(const Vector<Float> & r, const Vector<Float> & g,
00249                                       const Vector<Float> & b, uInt offset = 0);
00250 
00251                 // Return the total number of RW colors currently in use.
00252                 uInt nColors() const;
00253 
00254                 // Return the number of colors per component in the map.  Throws
00255                 // an exception if this is not an HSV or RGB ColorTable.
00256                 virtual void nColors(uInt &n1, uInt &n2, uInt &n3) const;
00257 
00258                 // Return the depth in bits of the colors
00259                 uInt depth() const;
00260 
00261                 // Return the number of colors that are still unallocated
00262                 uInt nSpareColors() const;
00263 
00264                 // Return pointer to display that is being used
00265                 ::XDisplay * display() const;
00266                 // Return pointer to screen that is being used
00267                 Screen * screen() const;
00268                 // Return pointer to visual that is being used
00269                 Visual * visual() const;
00270                 // Return pointer to visual info that is being used
00271                 XVisualInfo *visualInfo() const {
00272                         return visualInfo_;
00273                 }
00274                 // Return XID of X "virtual colormap" being used
00275                 XColormap xcmap() const;
00276 
00277                 // Return True if the table is in colorIndex mode
00278                 Bool indexMode() const {
00279                         return (colorModel_ == Display::Index);
00280                 }
00281                 // Return True if the table is in RGB mode
00282                 Bool rgbMode() const {
00283                         return (colorModel_ == Display::RGB);
00284                 }
00285                 // Return True if the table is in HSV mode
00286                 Bool hsvMode() const {
00287                         return (colorModel_ == Display::HSV);
00288                 }
00289 
00290                 // Return True if the colortable can be resized.
00291                 Bool rigid() const {
00292                         return rigid_;
00293                 }
00294 
00295                 // Return the color model for multichannel color
00296                 Display::ColorModel colorModel() const {
00297                         return colorModel_;
00298                 }
00299                 Bool readOnly()const {
00300                         return readOnly_;
00301                 }
00302                 Bool decomposedIndex()const {
00303                         return decomposedIndex_;
00304                 }
00305                 // Return the number of currently unallocated cells that can be allocated RW.
00306                 uInt QueryColorsAvailable(const Bool contig)const;
00307                 virtual uInt QueryHWColorsAvailable(const Bool contig)const;
00308                 // Convert a virtual index to a physical pixel.
00309                 Bool virtualToPhysical(const unsigned long vindex,
00310                                        unsigned long &pindex)const;
00311                 // Store an RGB value at index. For RGV visuals, only the virtual colormap
00312                 // is updated.
00313                 void storeColor(const uInt index,
00314                                 const float r, const float g, const float b);
00315                 // Convert a pixel to color components. If decomposed index (eg TC),
00316                 // the pixel contains the color information. Otherwise, (eg. PseudoColor),
00317                 // The information is looked up in the virtual colormap.
00318                 void pixelToComponents(const uLong pixel, Float &r, Float &g, Float &b);
00319                 // Return a GL capable visual that supports the colormodel or NULL.
00320                 static XVisualInfo *getVisualInfo(::XDisplay *dpy,
00321                                                   const Display::ColorModel colormodel);
00322                 // Returns the color values for the index. (Inverse of installRGBColors()).
00323                 void indexToRGB(const uInt index, float &r, float &g, float &b);
00324         private:
00325 
00326                 // Return the log power 2 of n and return True if n is
00327                 // a power of two.  Otherwise return false.
00328                 Bool isPow2(uInt n, uInt & log2n);
00329 
00330                 // (Multi-Channel)
00331                 void setupColorCube(uLong n1, uLong n2, uLong n3,
00332                                     uLong n1m, uLong n2m, uLong n3m);
00333                 // (Multi-Channel)
00334                 void setupStandardMapping(const XStandardColormap * mapInfo);
00335 
00336                 Bool initVisual(XVisualInfo *vi=NULL);
00337                 // A pointer to the XDisplay
00338                 ::XDisplay * display_;
00339                 // A pointer the the X Screen
00340                 Screen * screen_;
00341                 // A pointer to the X Visual
00342                 Visual * visual_;
00343                 XVisualInfo *visualInfo_;
00344                 // A pointer to the XColormap (X Hardware colormap)
00345                 XColormap xcmap_;
00346 
00347                 // (Valid Always) number of bits of depth
00348                 uInt depth_;
00349                 // (Valid Always) number of total colors available for RW.
00350                 uInt nColors_;
00351                 // (Valid only when implementation uses a PseudoColor or a
00352                 // StaticColor visual).  Table of color indices available.
00353                 uLong * colors_;        // converts pixel index into vcmap index. Always valid.
00354                 uShort vcmapLength_;    // Length of virtual colormap.
00355                 // (& HW map if it exists)
00356                 GLVColorTableEntry *vcmap_;
00357                 // (Valid Always)
00358                 // True if the table may not be resized, such as when a
00359                 // standard XColormap is used.  Generally this is set to True
00360                 // unless the visual is PseudoColor/StaticColor and the color
00361                 // model is Display::Index.
00362                 Bool rigid_;
00364                 // True if the colormap is read only.
00365                 Bool    readOnly_;
00366                 // True if TrueColor or DirectColor.
00367                 Bool  decomposedIndex_;
00368                 void checkVisual();
00369                 // Write an RGB value to hardware colormap at physical index.
00370                 // (Called by storeColor()).
00371                 virtual void storeHWColor(const uLong pindex,
00372                                           const float r, const float g, const float b);
00373                 // Shift counts, masks, and max values used to build pixels for
00374                 // decomposed index colormaps.
00375                 unsigned short red_shift_, green_shift_, blue_shift_;
00376                 unsigned short red_max_, green_max_, blue_max_;
00377                 unsigned long red_mask_, green_mask_, blue_mask_;
00378                 // Convert from integer HSV components to RGB pixel components.
00379                 void HSV2RGB(const uLong H, const uLong S, const uLong V,
00380                              uLong &R, uLong &G, uLong &B);
00381                 uInt HSV2Index(float h, float s, float v);
00382 
00384                 // (Valid only for multi-channel color modes (RGB, HSV, etc))
00385                 // If true, then the color cube's sides are powers of two,
00386                 // making indexing possible with shift/add using the values of
00387                 // <nBits1_, nBits2_, nBits3_>.  If not true, indexing
00388                 // using <n1_,n2_,n3_> and multiplication is required.
00389                 Bool pow2Mapping_;
00390 
00391                 // (Valid Always)
00392                 // The colormodel that this GLPixelCanvasColorTable has been
00393                 // configured as.
00394                 Display::ColorModel colorModel_;
00395 
00396                 // (Valid only for multi-channel color modes (RGB, HSV, etc))
00397                 // Represents the first cell used for the color cube.
00398                 // baseColor_ is zero for PseudoColor/StaticColor implementations
00399                 // because they use a table.
00400                 uLong baseColor_;
00401 
00402                 // (Valid only for multi-channel color modes (RGB, HSV, etc))
00403                 // Specifies the color resolution for each side of the
00404                 // color cube.
00405                 // index = n1Mult_*R + n2Mult_*G + n3Mult_*B for RGB in
00406                 // the range of <[0,n1_-1],[0,n2_-1],[0,n3_-1]>
00407                 // <group>
00408                 uInt n1_;
00409                 uInt n2_;
00410                 uInt n3_;
00411 
00412                 uInt n1Mult_;
00413                 uInt n2Mult_;
00414                 uInt n3Mult_;
00415                 // </group>
00416 
00417                 // (Valid only for multi-channel color modes (RGB, HSV, etc))
00418                 // and when pow2Mapping is true.
00419                 // index = (R << n1Shift_) | (G << n2Shift_) | (B << n3Shift_)
00420                 // for RGB the range of <[0,n1_-1],[0,n2_-1],[0,n3_-1]>
00421                 // <group>
00422                 uInt n1Shift_;
00423                 uInt n2Shift_;
00424                 uInt n3Shift_;
00425                 // </group>
00426         };
00427 
00428 
00429 } //# NAMESPACE CASA - END
00430 #ifndef AIPS_NO_TEMPLATE_SRC
00431 #include <display/Display/GLPCColTblTemplates.tcc>
00432 #endif
00433 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1