~ubuntu-branches/debian/sid/3depict/sid

« back to all changes in this revision

Viewing changes to src/colourmap.cpp

  • Committer: Bazaar Package Importer
  • Author(s): D Haley
  • Date: 2010-08-09 21:23:50 UTC
  • Revision ID: james.westby@ubuntu.com-20100809212350-cg6yumndhwi3bqws
Tags: upstream-0.0.1
ImportĀ upstreamĀ versionĀ 0.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      colourmap.cpp  - contiuum colourmap header
 
3
 *      Copyright (C) 2010, ViewerGTKQt project
 
4
 *      Modifed by D Haley 2010
 
5
 
 
6
 *      This program is free software: you can redistribute it and/or modify
 
7
 *      it under the terms of the GNU General Public License as published by
 
8
 *      the Free Software Foundation, either version 3 of the License, or
 
9
 *      (at your option) any later version.
 
10
 
 
11
 *      This program is distributed in the hope that it will be useful,
 
12
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *      GNU General Public License for more details.
 
15
 
 
16
 *      You should have received a copy of the GNU General Public License
 
17
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
 
 
21
#include <math.h>
 
22
#include <stdlib.h>
 
23
#include "colourmap.h"
 
24
#include <limits>
 
25
 
 
26
void jetColorMap(unsigned char *rgb,float value,float min,float max)
 
27
{
 
28
        float max4=(max-min)/4;
 
29
        value-=min;
 
30
        if (value==HUGE_VAL)
 
31
        {
 
32
                rgb[0]=rgb[1]=rgb[2]=255;
 
33
        }
 
34
        else if (value<0)
 
35
        {
 
36
                rgb[0]=rgb[1]=rgb[2]=0;
 
37
        }
 
38
        else if (value<max4)
 
39
        {
 
40
                unsigned char c1=144;
 
41
                rgb[0]=0;
 
42
                rgb[1]=0;
 
43
                rgb[2]=c1+(unsigned char)((255-c1)*value/max4);
 
44
        }
 
45
        else if (value<2*max4)
 
46
        {
 
47
                rgb[0]=0;
 
48
                rgb[1]=(unsigned char)(255*(value-max4)/max4);
 
49
                rgb[2]=255;
 
50
        }
 
51
        else if (value<3*max4)
 
52
        {
 
53
                rgb[0]=(unsigned char)(255*(value-2*max4)/max4);
 
54
                rgb[1]=255;
 
55
                rgb[2]=255-rgb[0];
 
56
        }
 
57
        else if (value<max)
 
58
        {
 
59
                rgb[0]=255;
 
60
                rgb[1]=(unsigned char)(255-255*(value-3*max4)/max4);
 
61
                rgb[2]=0;
 
62
        }
 
63
        else {
 
64
                rgb[0]=255;
 
65
                rgb[1]=rgb[2]=0;
 
66
        }
 
67
}
 
68
 
 
69
void hotColorMap(unsigned char *rgb,float value,float min,float max)
 
70
{
 
71
  float max3=(max-min)/3;
 
72
  value-=min;
 
73
  if(value==HUGE_VAL)
 
74
    {rgb[0]=rgb[1]=rgb[2]=255;}
 
75
  else if(value<0)
 
76
    {rgb[0]=rgb[1]=rgb[2]=0;}
 
77
  else if(value<max3)
 
78
    {rgb[0]=(unsigned char)(255*value/max3);rgb[1]=0;rgb[2]=0;}
 
79
  else if(value<2*max3)
 
80
    {rgb[0]=255;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=0;}
 
81
  else if(value<max)
 
82
    {rgb[0]=255;rgb[1]=255;rgb[2]=(unsigned char)(255*(value-2*max3)/max3);}
 
83
  else {rgb[0]=rgb[1]=rgb[2]=255;}
 
84
}
 
85
 
 
86
void coldColorMap(unsigned char *rgb,float value,float min,float max)
 
87
{
 
88
  float max3=(max-min)/3;
 
89
  value-=min;
 
90
  if(value==HUGE_VAL)
 
91
    {rgb[0]=rgb[1]=rgb[2]=255;}
 
92
  else if(value<0)
 
93
    {rgb[0]=rgb[1]=rgb[2]=0;}
 
94
  else if(value<max3)
 
95
    {rgb[0]=0;rgb[1]=0;rgb[2]=(unsigned char)(255*value/max3);}
 
96
  else if(value<2*max3)
 
97
    {rgb[0]=0;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=255;}
 
98
  else if(value<max)
 
99
    {rgb[0]=(unsigned char)(255*(value-2*max3)/max3);rgb[1]=255;rgb[2]=255;}
 
100
  else {rgb[0]=rgb[1]=rgb[2]=255;}
 
101
}
 
102
 
 
103
void blueColorMap(unsigned char *rgb,float value,float min,float max)
 
104
{
 
105
  value-=min;
 
106
  if(value==HUGE_VAL)
 
107
    {rgb[0]=rgb[1]=rgb[2]=255;}
 
108
  else if(value<0)
 
109
    {rgb[0]=rgb[1]=rgb[2]=0;}
 
110
  else if(value<max)
 
111
    {rgb[0]=0;rgb[1]=0;rgb[2]=(unsigned char)(255*value/max);}
 
112
  else {rgb[0]=rgb[1]=0;rgb[2]=255;}
 
113
}
 
114
 
 
115
void positiveColorMap(unsigned char *rgb,float value,float min,float max)
 
116
{
 
117
  value-=min;
 
118
  max-=min;
 
119
  value/=max;
 
120
 
 
121
  if(value<0){
 
122
  rgb[0]=rgb[1]=rgb[2]=0;
 
123
    return;
 
124
  }
 
125
  if(value>1){
 
126
  rgb[0]=rgb[1]=rgb[2]=255;
 
127
  return;
 
128
  }
 
129
 
 
130
  rgb[0]=192;rgb[1]=0;rgb[2]=0;
 
131
  rgb[0]+=(unsigned char)(63*value);
 
132
  rgb[1]+=(unsigned char)(255*value);
 
133
  if(value>0.5)
 
134
  rgb[2]+=(unsigned char)(255*2*(value-0.5));
 
135
}
 
136
 
 
137
void negativeColorMap(unsigned char *rgb,float value,float min,float max)
 
138
{
 
139
  value-=min;
 
140
  max-=min;
 
141
  rgb[0]=0;rgb[1]=0;rgb[2]=0;
 
142
  
 
143
  if(max>std::numeric_limits<float>::epsilon())
 
144
          value/=max;
 
145
  if(value<0) return;
 
146
  if(value>1){
 
147
  rgb[1]=rgb[2]=255;
 
148
  return;
 
149
  }
 
150
 
 
151
  rgb[1]+=(unsigned char)(255*value);
 
152
  if(value>0.5)
 
153
  rgb[2]+=(unsigned char)(255*2*(value-0.5));
 
154
 
 
155
}
 
156
 
 
157
void colorMap(unsigned char *rgb,float value,float min,float max)
 
158
{
 
159
  if(value>0) 
 
160
    positiveColorMap(rgb,value,0,max);
 
161
  else 
 
162
    negativeColorMap(rgb,value,min,0);
 
163
/*
 
164
  if(value>0) 
 
165
    hotColorMap(rgb,value,min,max);
 
166
  else 
 
167
    coldColorMap(rgb,value,min,max);
 
168
        */
 
169
}
 
170
 
 
171
void cyclicColorMap(unsigned char *rgb,float value,float min,float max)
 
172
{
 
173
  float max3=(max-min)/3;
 
174
  value-=(max-min)*(float)floor((value-min)/(max-min));
 
175
  if(value<max3)
 
176
    {rgb[0]=(unsigned char)(255-255*value/max3);rgb[1]=0;rgb[2]=255-rgb[0];}
 
177
  else if(value<2*max3)
 
178
    {rgb[0]=0;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=255-rgb[1];}
 
179
  else if(value<max)
 
180
    {rgb[0]=(unsigned char)(255*(value-2*max3)/max3);rgb[1]=255-rgb[0];rgb[2]=0;}
 
181
 
 
182
}
 
183
void randColorMap(unsigned char *rgb,float value,float min,float max)
 
184
{
 
185
  srand((int)(65000*(value-min)/(max-min)));
 
186
  rgb[0]=(unsigned char)(255*rand());
 
187
  rgb[1]=(unsigned char)(255*rand());
 
188
  rgb[2]=(unsigned char)(255*rand());
 
189
}
 
190
 
 
191
void grayColorMap(unsigned char *rgb,float value,float min,float max)
 
192
{
 
193
  max-=min;
 
194
  value-=min;
 
195
  rgb[0]=rgb[1]=rgb[2]=(unsigned char)(255*value/max);
 
196
}
 
197
 
 
198