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 // CLASS ColorTranslator - IMPLEMENTATION
45 //
46 //=============================================================================
47 
48 
49 //== INCLUDES =================================================================
50 
51 
52 #include "ColorTranslator.hh"
53 #include <ACG/GL/GLState.hh>
54 #include <iostream>
55 #include <limits>
56 
57 
58 //== NAMESPACES ===============================================================
59 
60 
61 namespace ACG {
62 
63 
64 //== IMPLEMENTATION ==========================================================
65 
66 
67 void
70 {
71  if(_state->compatibilityProfile())
72  {
73  glGetIntegerv( GL_RED_BITS, &red_bits_ );
74  glGetIntegerv( GL_GREEN_BITS, &green_bits_ );
75  glGetIntegerv( GL_BLUE_BITS, &blue_bits_ );
76  glGetIntegerv( GL_ALPHA_BITS, &alpha_bits_ );
77  }
78  else
79  { //only up to 8 bits are supported as of now, so hardcode them for core profiles
80  red_bits_ = 8;
81  green_bits_ = 8;
82  blue_bits_ = 8;
83  alpha_bits_ = 8;
84  }
85  // We currently only support up to 8 bits per channel (
86  if (red_bits_ > 8) red_bits_ = 8;
87  if (green_bits_ > 8) green_bits_ = 8;
88  if (blue_bits_ > 8) blue_bits_ = 8;
89  if (alpha_bits_ > 8) alpha_bits_ = 8;
90 
91  // Compute the mask to extract the component
92  red_mask_ = ((1 << red_bits_) - 1);
93  green_mask_ = ((1 << green_bits_) - 1);
94  blue_mask_ = ((1 << blue_bits_) - 1);
95  alpha_mask_ = ((1 << alpha_bits_) - 1);
96 
97  // Shift required to move the component to the lowest bits
98  red_shift_ = 8 - red_bits_;
99  green_shift_ = 8 - green_bits_;
100  blue_shift_ = 8 - blue_bits_;
101  alpha_shift_ = 8 - alpha_bits_;
102 
103 
104  red_round_ = 1 << (red_shift_ - 1);
105  green_round_ = 1 << (green_shift_ - 1);
106  blue_round_ = 1 << (blue_shift_ - 1);
107  alpha_round_ = 1 << (alpha_shift_ - 1);
108 
109  initialized_ = true;
110 }
111 
112 
113 //-----------------------------------------------------------------------------
114 
115 
116 Vec4uc
118 index2color(const size_t _idx) const
119 {
120  assert(initialized());
121  unsigned char r, g, b, a;
122 
123  // Make sure that the number fits
124  if ( _idx > std::numeric_limits<unsigned int>::max() ) {
125  std::cerr << "Can't convert index " << _idx << " to RGBA. Number too large for unsigned int \n";
126  return Vec4uc(0, 0, 0, 0);
127  }
128 
129  unsigned int idx = ( static_cast<unsigned int>(_idx) + 1);
130 
131  b = ((idx & blue_mask_) << blue_shift_) | blue_round_;
132  idx >>= blue_bits_;
133  g = ((idx & green_mask_) << green_shift_) | green_round_;
134  idx >>= green_bits_;
135  r = ((idx & red_mask_) << red_shift_) | red_round_;
136  idx >>= red_bits_;
137  a = ((idx & alpha_mask_) << alpha_shift_) | alpha_round_;
138  idx >>= alpha_bits_;
139 
140  if (!idx)
141  return Vec4uc(r, g, b, a);
142 
143  else
144  {
145  std::cerr << "Can't convert index " << _idx << " to RGBA\n";
146  return Vec4uc(0, 0, 0, 0);
147  }
148 }
149 
150 
151 //-----------------------------------------------------------------------------
152 
153 
154 size_t
156 color2index(const Vec4uc _rgba) const
157 {
158 
159 
160  assert(initialized());
161 
162  // Work internally with#include <iostream> unsigned int for now
163  unsigned int result;
164 
165  // Combine the single unsigned chars according to masks
166  result = _rgba[3] >> alpha_shift_;
167  result <<= red_bits_;
168  result = _rgba[0] >> red_shift_;
169  result <<= green_bits_;
170  result |= _rgba[1] >> green_shift_;
171  result <<= blue_bits_;
172  result |= _rgba[2] >> blue_shift_;
173 
174  // Return size_t Here
175  return ( static_cast<size_t>(result-1) );
176 }
177 
178 
179 //-----------------------------------------------------------------------------
180 
181 
182 size_t
184 {
185  assert(initialized());
186  if (red_bits_+green_bits_+blue_bits_+alpha_bits_ == 32)
187  return 0xffffffff;
188  else
189  return (1 << (red_bits_+green_bits_+blue_bits_+alpha_bits_))-1;
190 }
191 
192 
193 //=============================================================================
194 } // namespace ACG
195 //=============================================================================
196 
Namespace providing different geometric functions concerning angles.
VectorT< unsigned char, 4 > Vec4uc
Definition: VectorT.hh:128
void initialize(ACG::GLState *)
Vec4uc index2color(const size_t _idx) const
index -> color (one buffer)
size_t color2index(const Vec4uc _rgba) const
color -> index (one buffer)
size_t max_index() const
returns maximal convertible index
bool initialized() const
has it been initialized?