Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ColorTranslator.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openflipper.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenFlipper. *
9  * *
10  * OpenFlipper is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenFlipper is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenFlipper. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 10745 $ *
38  * $Author: moebius $ *
39  * $Date: 2011-01-26 10:23:50 +0100 (Mi, 26 Jan 2011) $ *
40  * *
41 \*===========================================================================*/
42 
43 
44 
45 //=============================================================================
46 //
47 // CLASS ColorTranslator - IMPLEMENTATION
48 //
49 //=============================================================================
50 
51 
52 //== INCLUDES =================================================================
53 
54 
55 #include "ColorTranslator.hh"
56 #include <iostream>
57 
58 
59 //== NAMESPACES ===============================================================
60 
61 
62 namespace ACG {
63 
64 
65 //== IMPLEMENTATION ==========================================================
66 
67 
68 void
71 {
72  glGetIntegerv( GL_RED_BITS, &red_bits_ );
73  glGetIntegerv( GL_GREEN_BITS, &green_bits_ );
74  glGetIntegerv( GL_BLUE_BITS, &blue_bits_ );
75  glGetIntegerv( GL_ALPHA_BITS, &alpha_bits_ );
76 
77  if (red_bits_ > 8) red_bits_ = 8;
78  if (green_bits_ > 8) green_bits_ = 8;
79  if (blue_bits_ > 8) blue_bits_ = 8;
80  if (alpha_bits_ > 8) alpha_bits_ = 8;
81 
82  red_mask_ = ((1 << red_bits_) - 1);
83  green_mask_ = ((1 << green_bits_) - 1);
84  blue_mask_ = ((1 << blue_bits_) - 1);
85  alpha_mask_ = ((1 << alpha_bits_) - 1);
86 
87  red_shift_ = 8 - red_bits_;
88  green_shift_ = 8 - green_bits_;
89  blue_shift_ = 8 - blue_bits_;
90  alpha_shift_ = 8 - alpha_bits_;
91 
92  red_round_ = 1 << (red_shift_ - 1);
93  green_round_ = 1 << (green_shift_ - 1);
94  blue_round_ = 1 << (blue_shift_ - 1);
95  alpha_round_ = 1 << (alpha_shift_ - 1);
96 
97  initialized_ = true;
98 }
99 
100 
101 //-----------------------------------------------------------------------------
102 
103 
104 Vec4uc
106 index2color(unsigned int _idx) const
107 {
108  assert(initialized());
109  unsigned char r, g, b, a;
110  unsigned int idx(_idx+1);
111 
112  b = ((idx & blue_mask_) << blue_shift_) | blue_round_;
113  idx >>= blue_bits_;
114  g = ((idx & green_mask_) << green_shift_) | green_round_;
115  idx >>= green_bits_;
116  r = ((idx & red_mask_) << red_shift_) | red_round_;
117  idx >>= red_bits_;
118  a = ((idx & alpha_mask_) << alpha_shift_) | alpha_round_;
119  idx >>= alpha_bits_;
120 
121  if (!idx)
122  return Vec4uc(r, g, b, a);
123 
124  else
125  {
126  std::cerr << "Can't convert index " << _idx << " to RGBA\n";
127  return Vec4uc(0, 0, 0, 0);
128  }
129 }
130 
131 
132 //-----------------------------------------------------------------------------
133 
134 
135 int
137 color2index(Vec4uc _rgba) const
138 {
139  assert(initialized());
140  unsigned int result;
141 
142  result = _rgba[3] >> alpha_shift_;
143  result <<= red_bits_;
144  result = _rgba[0] >> red_shift_;
145  result <<= green_bits_;
146  result |= _rgba[1] >> green_shift_;
147  result <<= blue_bits_;
148  result |= _rgba[2] >> blue_shift_;
149 
150  return (result-1);
151 }
152 
153 
154 //-----------------------------------------------------------------------------
155 
156 
157 unsigned int
159 {
160  assert(initialized());
161  if (red_bits_+green_bits_+blue_bits_+alpha_bits_ == 32)
162  return 0xffffffff;
163  else
164  return (1 << (red_bits_+green_bits_+blue_bits_+alpha_bits_))-1;
165 }
166 
167 
168 //=============================================================================
169 } // namespace ACG
170 //=============================================================================
171