ColorTranslator.cc 6.06 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
34

Jan Möbius's avatar
Jan Möbius committed
35 36
/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
38 39 40 41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74



//=============================================================================
//
//  CLASS ColorTranslator - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================


#include "ColorTranslator.hh"
#include <iostream>


//== NAMESPACES ===============================================================


namespace ACG {


//== IMPLEMENTATION ========================================================== 


void
ColorTranslator::
initialize()
{
  glGetIntegerv( GL_RED_BITS,   &red_bits_   );
  glGetIntegerv( GL_GREEN_BITS, &green_bits_ );
  glGetIntegerv( GL_BLUE_BITS,  &blue_bits_  );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
75
  glGetIntegerv( GL_ALPHA_BITS,  &alpha_bits_ );
Jan Möbius's avatar
 
Jan Möbius committed
76 77 78 79

  if (red_bits_   > 8)  red_bits_   = 8;
  if (green_bits_ > 8)  green_bits_ = 8;
  if (blue_bits_  > 8)  blue_bits_  = 8;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
80
  if (alpha_bits_ > 8)  alpha_bits_ = 8;
Jan Möbius's avatar
 
Jan Möbius committed
81 82 83

  red_mask_    = ((1 << red_bits_)   - 1);
  green_mask_  = ((1 << green_bits_) - 1);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
84 85
  blue_mask_   = ((1 << blue_bits_)  - 1);
  alpha_mask_  = ((1 << alpha_bits_) - 1);
Jan Möbius's avatar
 
Jan Möbius committed
86 87 88

  red_shift_   = 8 - red_bits_;
  green_shift_ = 8 - green_bits_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
89 90
  blue_shift_  = 8 - blue_bits_;
  alpha_shift_ = 8 - alpha_bits_;
Jan Möbius's avatar
 
Jan Möbius committed
91 92 93 94

  red_round_   = 1 << (red_shift_   - 1);
  green_round_ = 1 << (green_shift_ - 1);
  blue_round_  = 1 << (blue_shift_  - 1);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
95
  alpha_round_ = 1 << (alpha_shift_ - 1);
Jan Möbius's avatar
 
Jan Möbius committed
96 97 98 99 100 101 102 103

  initialized_ = true;
}

  
//-----------------------------------------------------------------------------


Jan Möbius's avatar
Dennis:  
Jan Möbius committed
104
Vec4uc
Jan Möbius's avatar
 
Jan Möbius committed
105 106 107 108
ColorTranslator::
index2color(unsigned int _idx) const 
{
  assert(initialized());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
109
  unsigned char  r, g, b, a;
Jan Möbius's avatar
 
Jan Möbius committed
110 111 112 113 114 115 116 117
  unsigned int   idx(_idx+1);
  
  b = ((idx & blue_mask_)  << blue_shift_)  | blue_round_;  
  idx >>= blue_bits_;
  g = ((idx & green_mask_) << green_shift_) | green_round_;  
  idx >>= green_bits_;
  r = ((idx & red_mask_)   << red_shift_)   | red_round_;  
  idx >>= red_bits_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
118 119 120
  a = ((idx & alpha_mask_) << alpha_shift_) | alpha_round_;
  idx >>= alpha_bits_;

Jan Möbius's avatar
 
Jan Möbius committed
121
  if (!idx) 
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
122
    return  Vec4uc(r, g, b, a);
Jan Möbius's avatar
 
Jan Möbius committed
123 124 125

  else 
  {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
126 127
    std::cerr << "Can't convert index " << _idx << " to RGBA\n";
    return Vec4uc(0, 0, 0, 0);
Jan Möbius's avatar
 
Jan Möbius committed
128 129 130 131 132 133 134 135 136
  }
}

  
//-----------------------------------------------------------------------------


int
ColorTranslator::
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
137
color2index(Vec4uc _rgba) const
Jan Möbius's avatar
 
Jan Möbius committed
138 139 140 141
{
  assert(initialized());
  unsigned int result;

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
142 143 144
  result =   _rgba[3] >> alpha_shift_;
  result <<= red_bits_;
  result =   _rgba[0] >> red_shift_;
Jan Möbius's avatar
 
Jan Möbius committed
145
  result <<= green_bits_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
146
  result |=  _rgba[1] >> green_shift_;
Jan Möbius's avatar
 
Jan Möbius committed
147
  result <<= blue_bits_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
148
  result |=  _rgba[2] >> blue_shift_;    
Jan Möbius's avatar
 
Jan Möbius committed
149 150 151 152 153 154 155 156 157 158 159 160

  return (result-1);
}


//-----------------------------------------------------------------------------


unsigned int
ColorTranslator::max_index() const 
{
  assert(initialized());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
161 162 163 164
  if (red_bits_+green_bits_+blue_bits_+alpha_bits_ == 32)
    return 0xffffffff;
  else
    return (1 << (red_bits_+green_bits_+blue_bits_+alpha_bits_))-1;
Jan Möbius's avatar
 
Jan Möbius committed
165 166 167 168 169 170 171
}


//=============================================================================
} // namespace ACG
//=============================================================================