QtColorTranslator.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6743 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  CLASS QtColorTranslator - IMPLEMENTATION
00048 //
00049 //=============================================================================
00050 
00051 
00052 //== INCLUDES =================================================================
00053 
00054 #include "QtColorTranslator.hh"
00055 #include <iostream>
00056 #include <assert.h>
00057 
00058 
00059 //== NAMESPACES ===============================================================
00060 
00061 namespace ACG {
00062 
00063 
00064 //== IMPLEMENTATION ========================================================== 
00065 
00066 
00067 void
00068 QtColorTranslator::initialize()
00069 {
00070   glGetIntegerv( GL_RED_BITS,   &redBits_ );
00071   glGetIntegerv( GL_GREEN_BITS, &greenBits_ );
00072   glGetIntegerv( GL_BLUE_BITS,  &blueBits_ );    
00073 
00074   if (redBits_ > 8)   redBits_ = 8;
00075   if (greenBits_ > 8) greenBits_ = 8;
00076   if (blueBits_ > 8)  blueBits_ = 8;
00077 
00078   redMask_    = ((1 << redBits_)   - 1);
00079   greenMask_  = ((1 << greenBits_) - 1);
00080   blueMask_   = ((1 << blueBits_)  - 1);    
00081   redShift_   = 8 - redBits_;
00082   greenShift_ = 8 - greenBits_;
00083   blueShift_  = 8 - blueBits_;    
00084   redRound_   = 1 << (redShift_   - 1);
00085   greenRound_ = 1 << (greenShift_ - 1);
00086   blueRound_  = 1 << (blueShift_  - 1);
00087 
00088   initialized_ = true;
00089 
00090   if (redBits_   > 8) std::cerr << "error: more than 8 red bits\n";
00091   if (greenBits_ > 8) std::cerr << "error: more than 8 green bits\n";
00092   if (blueBits_  > 8) std::cerr << "error: more than 8 blue bits\n";
00093 }
00094 
00095   
00096 //-----------------------------------------------------------------------------
00097 
00098 
00099 bool
00100 QtColorTranslator::index2color(unsigned int _idx, QRgb& _col) const
00101 {
00102   assert(initialized());
00103   unsigned char  r, g, b;
00104   unsigned int idx(_idx+1);
00105   
00106   b = ((idx & blueMask_)  << blueShift_)  | blueRound_;  
00107   idx >>= blueBits_;
00108   g = ((idx & greenMask_) << greenShift_) | greenRound_;  
00109   idx >>= greenBits_;
00110   r = ((idx & redMask_)   << redShift_)   | redRound_;  
00111   idx >>= redBits_;
00112   
00113   if (!idx)
00114   {
00115     _col = qRgb(r, g, b);
00116     return true;
00117   }
00118   else {
00119     std::cerr << "Can't convert index " << _idx << " to RGB\n";
00120     _col = qRgb(0,0,0);
00121     return false;
00122   }
00123 }
00124 
00125   
00126 //-----------------------------------------------------------------------------
00127 
00128 
00129 bool
00130 QtColorTranslator::index2color(unsigned int _idx, QRgb& _fc, QRgb& _bc) const
00131 {
00132   assert(initialized());
00133   unsigned char  r, g, b;
00134   unsigned int idx(_idx+1);
00135   
00136   b = ((idx & blueMask_)  << blueShift_)  | blueRound_;  
00137   idx >>= blueBits_;
00138   g = ((idx & greenMask_) << greenShift_) | greenRound_;  
00139   idx >>= greenBits_;
00140   r = ((idx & redMask_)   << redShift_)   | redRound_;  
00141   idx >>= redBits_;
00142   
00143   if (!idx)
00144   {
00145     _bc = qRgb(r, g, b);
00146     _fc = qRgb(0,0,0);
00147     return true;
00148   }
00149   else
00150   {
00151     _bc = qRgb(r, g, b);
00152 
00153     b = ((idx & blueMask_)  << blueShift_)  | blueRound_;  
00154     idx >>= blueBits_;
00155     g = ((idx & greenMask_) << greenShift_) | greenRound_;  
00156     idx >>= greenBits_;
00157     r = ((idx & redMask_)   << redShift_)   | redRound_;  
00158     idx >>= redBits_;
00159 
00160     if (!idx)
00161     {
00162       _fc = qRgb(r,g,b);
00163       return true;
00164     }
00165     else
00166     {
00167       std::cerr << "Can't convert index " << _idx << " to RGB\n";
00168       _bc = qRgb(0,0,0);
00169       _fc = qRgb(0,0,0);
00170       return false;
00171     }
00172   }
00173 }
00174 
00175   
00176 //-----------------------------------------------------------------------------
00177 
00178 
00179 int
00180 QtColorTranslator::color2index(QRgb _c) const
00181 {
00182   assert(initialized());
00183   unsigned int result;
00184 
00185   result =   qRed(_c) >> redShift_;
00186   result <<= greenBits_;
00187   result |=  qGreen(_c) >> greenShift_;
00188   result <<= blueBits_;
00189   result |=  qBlue(_c) >> blueShift_;    
00190 
00191   return (result-1);
00192 }
00193 
00194 
00195 //-----------------------------------------------------------------------------
00196 
00197 
00198 int
00199 QtColorTranslator::color2index(QRgb _fc, QRgb _bc) const
00200 {
00201   assert(initialized());
00202   unsigned int result;
00203 
00204   result =   qRed(_fc) >> redShift_;
00205   result <<= greenBits_;
00206   result |=  qGreen(_fc) >> greenShift_;
00207   result <<= blueBits_;
00208   result |=  qBlue(_fc) >> blueShift_;    
00209 
00210   result <<= redBits_;
00211   result |=  qRed(_bc) >> redShift_;
00212   result <<= greenBits_;
00213   result |=  qGreen(_bc) >> greenShift_;
00214   result <<= blueBits_;
00215   result |=  qBlue(_bc) >> blueShift_;
00216   
00217   return (result-1);
00218 }
00219 
00220 
00221 //-----------------------------------------------------------------------------
00222 
00223 
00224 unsigned int
00225 QtColorTranslator::maxIndex() const
00226 {
00227   assert(initialized());
00228   unsigned int result(~0);
00229   result >>= 32 - redBits_ - greenBits_ - blueBits_;
00230   return (result-1);
00231 }
00232 
00233 
00234 //=============================================================================
00235 } // namespace ACG
00236 //=============================================================================
00237 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .