ColorTranslator.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 ColorTranslator - IMPLEMENTATION
00048 //
00049 //=============================================================================
00050 
00051 
00052 //== INCLUDES =================================================================
00053 
00054 
00055 #include "ColorTranslator.hh"
00056 #include <iostream>
00057 
00058 
00059 //== NAMESPACES ===============================================================
00060 
00061 
00062 namespace ACG {
00063 
00064 
00065 //== IMPLEMENTATION ========================================================== 
00066 
00067 
00068 void
00069 ColorTranslator::
00070 initialize()
00071 {
00072   glGetIntegerv( GL_RED_BITS,   &red_bits_   );
00073   glGetIntegerv( GL_GREEN_BITS, &green_bits_ );
00074   glGetIntegerv( GL_BLUE_BITS,  &blue_bits_  );
00075   glGetIntegerv( GL_ALPHA_BITS,  &alpha_bits_ );
00076 
00077   if (red_bits_   > 8)  red_bits_   = 8;
00078   if (green_bits_ > 8)  green_bits_ = 8;
00079   if (blue_bits_  > 8)  blue_bits_  = 8;
00080   if (alpha_bits_ > 8)  alpha_bits_ = 8;
00081 
00082   red_mask_    = ((1 << red_bits_)   - 1);
00083   green_mask_  = ((1 << green_bits_) - 1);
00084   blue_mask_   = ((1 << blue_bits_)  - 1);
00085   alpha_mask_  = ((1 << alpha_bits_) - 1);
00086 
00087   red_shift_   = 8 - red_bits_;
00088   green_shift_ = 8 - green_bits_;
00089   blue_shift_  = 8 - blue_bits_;
00090   alpha_shift_ = 8 - alpha_bits_;
00091 
00092   red_round_   = 1 << (red_shift_   - 1);
00093   green_round_ = 1 << (green_shift_ - 1);
00094   blue_round_  = 1 << (blue_shift_  - 1);
00095   alpha_round_ = 1 << (alpha_shift_ - 1);
00096 
00097   initialized_ = true;
00098 }
00099 
00100   
00101 //-----------------------------------------------------------------------------
00102 
00103 
00104 Vec4uc
00105 ColorTranslator::
00106 index2color(unsigned int _idx) const 
00107 {
00108   assert(initialized());
00109   unsigned char  r, g, b, a;
00110   unsigned int   idx(_idx+1);
00111   
00112   b = ((idx & blue_mask_)  << blue_shift_)  | blue_round_;  
00113   idx >>= blue_bits_;
00114   g = ((idx & green_mask_) << green_shift_) | green_round_;  
00115   idx >>= green_bits_;
00116   r = ((idx & red_mask_)   << red_shift_)   | red_round_;  
00117   idx >>= red_bits_;
00118   a = ((idx & alpha_mask_) << alpha_shift_) | alpha_round_;
00119   idx >>= alpha_bits_;
00120 
00121   if (!idx) 
00122     return  Vec4uc(r, g, b, a);
00123 
00124   else 
00125   {
00126     std::cerr << "Can't convert index " << _idx << " to RGBA\n";
00127     return Vec4uc(0, 0, 0, 0);
00128   }
00129 }
00130 
00131   
00132 //-----------------------------------------------------------------------------
00133 
00134 
00135 int
00136 ColorTranslator::
00137 color2index(Vec4uc _rgba) const
00138 {
00139   assert(initialized());
00140   unsigned int result;
00141 
00142   result =   _rgba[3] >> alpha_shift_;
00143   result <<= red_bits_;
00144   result =   _rgba[0] >> red_shift_;
00145   result <<= green_bits_;
00146   result |=  _rgba[1] >> green_shift_;
00147   result <<= blue_bits_;
00148   result |=  _rgba[2] >> blue_shift_;    
00149 
00150   return (result-1);
00151 }
00152 
00153 
00154 //-----------------------------------------------------------------------------
00155 
00156 
00157 unsigned int
00158 ColorTranslator::max_index() const 
00159 {
00160   assert(initialized());
00161   if (red_bits_+green_bits_+blue_bits_+alpha_bits_ == 32)
00162     return 0xffffffff;
00163   else
00164     return (1 << (red_bits_+green_bits_+blue_bits_+alpha_bits_))-1;
00165 }
00166 
00167 
00168 //=============================================================================
00169 } // namespace ACG
00170 //=============================================================================
00171 

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