Developer Documentation
ColorTranslator.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS ColorTranslator - IMPLEMENTATION
55 //
56 //=============================================================================
57 
58 
59 //== INCLUDES =================================================================
60 
61 
62 #include "ColorTranslator.hh"
63 #include <iostream>
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 
69 namespace ACG {
70 
71 
72 //== IMPLEMENTATION ==========================================================
73 
74 
75 void
78 {
79  glGetIntegerv( GL_RED_BITS, &red_bits_ );
80  glGetIntegerv( GL_GREEN_BITS, &green_bits_ );
81  glGetIntegerv( GL_BLUE_BITS, &blue_bits_ );
82  glGetIntegerv( GL_ALPHA_BITS, &alpha_bits_ );
83 
84  if (red_bits_ > 8) red_bits_ = 8;
85  if (green_bits_ > 8) green_bits_ = 8;
86  if (blue_bits_ > 8) blue_bits_ = 8;
87  if (alpha_bits_ > 8) alpha_bits_ = 8;
88 
89  red_mask_ = ((1 << red_bits_) - 1);
90  green_mask_ = ((1 << green_bits_) - 1);
91  blue_mask_ = ((1 << blue_bits_) - 1);
92  alpha_mask_ = ((1 << alpha_bits_) - 1);
93 
94  red_shift_ = 8 - red_bits_;
95  green_shift_ = 8 - green_bits_;
96  blue_shift_ = 8 - blue_bits_;
97  alpha_shift_ = 8 - alpha_bits_;
98 
99  red_round_ = 1 << (red_shift_ - 1);
100  green_round_ = 1 << (green_shift_ - 1);
101  blue_round_ = 1 << (blue_shift_ - 1);
102  alpha_round_ = 1 << (alpha_shift_ - 1);
103 
104  initialized_ = true;
105 }
106 
107 
108 //-----------------------------------------------------------------------------
109 
110 
111 Vec4uc
113 index2color(unsigned int _idx) const
114 {
115  assert(initialized());
116  unsigned char r, g, b, a;
117  unsigned int idx(_idx+1);
118 
119  b = ((idx & blue_mask_) << blue_shift_) | blue_round_;
120  idx >>= blue_bits_;
121  g = ((idx & green_mask_) << green_shift_) | green_round_;
122  idx >>= green_bits_;
123  r = ((idx & red_mask_) << red_shift_) | red_round_;
124  idx >>= red_bits_;
125  a = ((idx & alpha_mask_) << alpha_shift_) | alpha_round_;
126  idx >>= alpha_bits_;
127 
128  if (!idx)
129  return Vec4uc(r, g, b, a);
130 
131  else
132  {
133  std::cerr << "Can't convert index " << _idx << " to RGBA\n";
134  return Vec4uc(0, 0, 0, 0);
135  }
136 }
137 
138 
139 //-----------------------------------------------------------------------------
140 
141 
142 int
144 color2index(Vec4uc _rgba) const
145 {
146  assert(initialized());
147  unsigned int result;
148 
149  result = _rgba[3] >> alpha_shift_;
150  result <<= red_bits_;
151  result = _rgba[0] >> red_shift_;
152  result <<= green_bits_;
153  result |= _rgba[1] >> green_shift_;
154  result <<= blue_bits_;
155  result |= _rgba[2] >> blue_shift_;
156 
157  return (result-1);
158 }
159 
160 
161 //-----------------------------------------------------------------------------
162 
163 
164 unsigned int
166 {
167  assert(initialized());
168  if (red_bits_+green_bits_+blue_bits_+alpha_bits_ == 32)
169  return 0xffffffff;
170  else
171  return (1 << (red_bits_+green_bits_+blue_bits_+alpha_bits_))-1;
172 }
173 
174 
175 //=============================================================================
176 } // namespace ACG
177 //=============================================================================
178 
bool initialized() const
has it been initialized?
void initialize()
init (takes current GL context)
VectorT< unsigned char, 4 > Vec4uc
Definition: VectorT.hh:134
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
Vec4uc index2color(unsigned int _idx) const
index -> color (one buffer)
unsigned int max_index() const
returns maximal convertable index
int color2index(Vec4uc _rgba) const
color -> index (one buffer)