~ubuntu-branches/ubuntu/trusty/3depict/trusty-proposed

« back to all changes in this revision

Viewing changes to src/common/colourmap.cpp

  • Committer: Package Import Robot
  • Author(s): D Haley
  • Date: 2013-05-17 00:52:39 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20130517005239-7bl4mnhkvrhc2ba6
Tags: 0.0.13-1
Upload to unstable 

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 2013
 
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
#include "common/translation.h"
 
27
 
 
28
void jetColorMap(unsigned char *rgb,float value,float min,float max)
 
29
{
 
30
        float max4=(max-min)/4;
 
31
        value-=min;
 
32
        if (value==HUGE_VAL)
 
33
        {
 
34
                rgb[0]=rgb[1]=rgb[2]=255;
 
35
        }
 
36
        else if (value<0)
 
37
        {
 
38
                rgb[0]=rgb[1]=rgb[2]=0;
 
39
        }
 
40
        else if (value<max4)
 
41
        {
 
42
                unsigned char c1=144;
 
43
                rgb[0]=0;
 
44
                rgb[1]=0;
 
45
                rgb[2]=c1+(unsigned char)((255-c1)*value/max4);
 
46
        }
 
47
        else if (value<2*max4)
 
48
        {
 
49
                rgb[0]=0;
 
50
                rgb[1]=(unsigned char)(255*(value-max4)/max4);
 
51
                rgb[2]=255;
 
52
        }
 
53
        else if (value<3*max4)
 
54
        {
 
55
                rgb[0]=(unsigned char)(255*(value-2*max4)/max4);
 
56
                rgb[1]=255;
 
57
                rgb[2]=255-rgb[0];
 
58
        }
 
59
        else if (value<max)
 
60
        {
 
61
                rgb[0]=255;
 
62
                rgb[1]=(unsigned char)(255-255*(value-3*max4)/max4);
 
63
                rgb[2]=0;
 
64
        }
 
65
        else {
 
66
                rgb[0]=255;
 
67
                rgb[1]=rgb[2]=0;
 
68
        }
 
69
}
 
70
 
 
71
void hotColorMap(unsigned char *rgb,float value,float min,float max)
 
72
{
 
73
  float max3=(max-min)/3;
 
74
  value-=min;
 
75
  if(value==HUGE_VAL)
 
76
    {rgb[0]=rgb[1]=rgb[2]=255;}
 
77
  else if(value<0)
 
78
    {rgb[0]=rgb[1]=rgb[2]=0;}
 
79
  else if(value<max3)
 
80
    {rgb[0]=(unsigned char)(255*value/max3);rgb[1]=0;rgb[2]=0;}
 
81
  else if(value<2*max3)
 
82
    {rgb[0]=255;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=0;}
 
83
  else if(value<max)
 
84
    {rgb[0]=255;rgb[1]=255;rgb[2]=(unsigned char)(255*(value-2*max3)/max3);}
 
85
  else {rgb[0]=rgb[1]=rgb[2]=255;}
 
86
}
 
87
 
 
88
void coldColorMap(unsigned char *rgb,float value,float min,float max)
 
89
{
 
90
  float max3=(max-min)/3;
 
91
  value-=min;
 
92
  if(value==HUGE_VAL)
 
93
    {rgb[0]=rgb[1]=rgb[2]=255;}
 
94
  else if(value<0)
 
95
    {rgb[0]=rgb[1]=rgb[2]=0;}
 
96
  else if(value<max3)
 
97
    {rgb[0]=0;rgb[1]=0;rgb[2]=(unsigned char)(255*value/max3);}
 
98
  else if(value<2*max3)
 
99
    {rgb[0]=0;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=255;}
 
100
  else if(value<max)
 
101
    {rgb[0]=(unsigned char)(255*(value-2*max3)/max3);rgb[1]=255;rgb[2]=255;}
 
102
  else {rgb[0]=rgb[1]=rgb[2]=255;}
 
103
}
 
104
 
 
105
void blueColorMap(unsigned char *rgb,float value,float min,float max)
 
106
{
 
107
  value-=min;
 
108
  if(value==HUGE_VAL)
 
109
    {rgb[0]=rgb[1]=rgb[2]=255;}
 
110
  else if(value<0)
 
111
    {rgb[0]=rgb[1]=rgb[2]=0;}
 
112
  else if(value<max)
 
113
    {rgb[0]=0;rgb[1]=0;rgb[2]=(unsigned char)(255*value/max);}
 
114
  else {rgb[0]=rgb[1]=0;rgb[2]=255;}
 
115
}
 
116
 
 
117
void positiveColorMap(unsigned char *rgb,float value,float min,float max)
 
118
{
 
119
  value-=min;
 
120
  max-=min;
 
121
  value/=max;
 
122
 
 
123
  if(value<0){
 
124
  rgb[0]=rgb[1]=rgb[2]=0;
 
125
    return;
 
126
  }
 
127
  if(value>1){
 
128
  rgb[0]=rgb[1]=rgb[2]=255;
 
129
  return;
 
130
  }
 
131
 
 
132
  rgb[0]=192;rgb[1]=0;rgb[2]=0;
 
133
  rgb[0]+=(unsigned char)(63*value);
 
134
  rgb[1]+=(unsigned char)(255*value);
 
135
  if(value>0.5)
 
136
  rgb[2]+=(unsigned char)(255*2*(value-0.5));
 
137
}
 
138
 
 
139
void negativeColorMap(unsigned char *rgb,float value,float min,float max)
 
140
{
 
141
  value-=min;
 
142
  max-=min;
 
143
  rgb[0]=0;rgb[1]=0;rgb[2]=0;
 
144
  
 
145
  if(max>std::numeric_limits<float>::epsilon())
 
146
          value/=max;
 
147
  if(value<0) return;
 
148
  if(value>1){
 
149
  rgb[1]=rgb[2]=255;
 
150
  return;
 
151
  }
 
152
 
 
153
  rgb[1]+=(unsigned char)(255*value);
 
154
  if(value>0.5)
 
155
  rgb[2]+=(unsigned char)(255*2*(value-0.5));
 
156
 
 
157
}
 
158
 
 
159
void colorMap(unsigned char *rgb,float value,float min,float max)
 
160
{
 
161
  if(value>0) 
 
162
    positiveColorMap(rgb,value,0,max);
 
163
  else 
 
164
    negativeColorMap(rgb,value,min,0);
 
165
}
 
166
 
 
167
void cyclicColorMap(unsigned char *rgb,float value,float min,float max)
 
168
{
 
169
  float max3=(max-min)/3;
 
170
  value-=(max-min)*(float)floor((value-min)/(max-min));
 
171
  if(value<max3)
 
172
    {rgb[0]=(unsigned char)(255-255*value/max3);rgb[1]=0;rgb[2]=255-rgb[0];}
 
173
  else if(value<2*max3)
 
174
    {rgb[0]=0;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=255-rgb[1];}
 
175
  else if(value<max)
 
176
    {rgb[0]=(unsigned char)(255*(value-2*max3)/max3);rgb[1]=255-rgb[0];rgb[2]=0;}
 
177
 
 
178
}
 
179
void randColorMap(unsigned char *rgb,float value,float min,float max)
 
180
{
 
181
  srand((int)(65000*(value-min)/(max-min)));
 
182
  rgb[0]=(unsigned char)(255*rand());
 
183
  rgb[1]=(unsigned char)(255*rand());
 
184
  rgb[2]=(unsigned char)(255*rand());
 
185
}
 
186
 
 
187
void grayColorMap(unsigned char *rgb,float value,float min,float max)
 
188
{
 
189
  max-=min;
 
190
  value-=min;
 
191
  rgb[0]=rgb[1]=rgb[2]=(unsigned char)(255*value/max);
 
192
}
 
193
 
 
194
void colourMapWrap(unsigned int mapID,unsigned char *rgb, float v, float min, float max)
 
195
{
 
196
        //Colour functions assume  positive value, so remap
 
197
        v= v-min;
 
198
        max-=min;
 
199
        min=0;
 
200
        //Select the desired colour map
 
201
        switch(mapID)
 
202
        {
 
203
                case  0:
 
204
                        jetColorMap(rgb, v, min, max);
 
205
                        break;
 
206
                case  1:
 
207
                        hotColorMap(rgb, v, min, max);
 
208
                        break;
 
209
                case  2:
 
210
                        coldColorMap(rgb, v, min, max);
 
211
                        break;
 
212
                case  3:
 
213
                         grayColorMap(rgb, v, min, max);
 
214
                        break;
 
215
                case  4:
 
216
                        cyclicColorMap(rgb, v, min, max);
 
217
                        break;
 
218
                case  5:
 
219
                        colorMap(rgb, v, min, max);
 
220
                        break;
 
221
                case  6:
 
222
                        blueColorMap(rgb, v, min, max);
 
223
                        break;
 
224
                case  7:
 
225
                         randColorMap(rgb, v, min, max);
 
226
                        break;
 
227
        }
 
228
 
 
229
}
 
230
 
 
231
std::string getColourMapName(unsigned int mapID)
 
232
{
 
233
 
 
234
        const char *mapNames[] = { NTRANS("Jet"),
 
235
                                NTRANS("Hot"),
 
236
                                NTRANS("Cold"),
 
237
                                NTRANS("Grey"),
 
238
                                NTRANS("Cyclic"),
 
239
                                NTRANS("General"),
 
240
                                NTRANS("Blue"),
 
241
                                NTRANS("Pseudo-Random")};
 
242
 
 
243
        return TRANS(mapNames[mapID]);
 
244
}
 
245
 
 
246