summaryrefslogtreecommitdiffstats
path: root/tksao/colorbar/lut.C
blob: 3b511106578a78174074315da1b4db3af0207b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (C) 1999-200
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include "lut.h"
#include "colorbar.h"

// LUT Parser Stuf
#undef yyFlexLexer
#define yyFlexLexer rgbFlexLexer
#include <FlexLexer.h>

void* rgblval;
rgbFlexLexer* rgblexx;
extern int rgbparse(LUTColorMap*, rgbFlexLexer*);

int rgblex(void* vval, rgbFlexLexer* ll)
{
  rgblval = vval;
  rgblexx = ll;
  return ll ? ll->yylex() : 0;
}

void rgberror(LUTColorMap* cm, rgbFlexLexer* ll, const char* m) {}

// RGBColor
istream& operator>>(istream& str, RGBColor& cc)
{
  str >> cc.red_ >> cc.green_ >> cc.blue_;
  return str;
}

ostream& operator<<(ostream& str, RGBColor& cc)
{
  str.setf(ios::fixed, ios::floatfield);
  str << setw(8) << setprecision(6) 
      << cc.red_ << " " << cc.green_ << " " << cc.blue_ << endl;
  return str;
}

// LUTColorMap

LUTColorMap::LUTColorMap(Colorbar* p) : ColorMapInfo(p)
{
}

void LUTColorMap::newRGBColor(float r, float g, float b)
{
  colors.append(new RGBColor(r,g,b));
}

int LUTColorMap::load()
{
  ifstream str(fileName);
  if (!str)
    return 0;
  
  rgbFlexLexer* ll = new rgbFlexLexer(&str);
  rgbparse(this, ll);
  delete ll;

  if (colors.isEmpty())
    return 0;  // bailout
  else
    return 1;  // we found at least one RGBColor
}

int LUTColorMap::load(const char* var)
{
  const char* ccmd = Tcl_GetVar(parent_->getInterp(), var,
				TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG);
  if (!ccmd)
    return 0;

  // only make command string as long as needed
  // or the rest will be processed as garbage
  int len = strlen(ccmd)+2;
  char* buf = new char[len];
  memcpy(buf, ccmd, len);

  // add terminator to make parser happy
  buf[len-2] = '\n';
  buf[len-1] = '\0';

  string x(buf);
  istringstream istr(x);

  rgbFlexLexer* ll = new rgbFlexLexer(&istr);
  rgbparse(this, ll);
  delete ll;
  delete [] buf;

  if (colors.isEmpty())
    return 0;  // bailout
  else
    return 1;  // we found at least one RGBColor
}

void LUTColorMap::save(const char* fn)
{
  ofstream fstr(fn);
  if (!fstr)
    return;
  fstr << *this;
}

unsigned char LUTColorMap::getRedChar(int ii, int count)
{
  int size = colors.count();
  int index = (int)((ii*size/count) + .5);
  if (index>=0 && index<size)
    return (unsigned char)(colors[index]->red() * UCHAR_MAX);
  else
    return 0;
}

unsigned char LUTColorMap::getGreenChar(int ii, int count)
{
  int size = colors.count();
  int index = (int)((ii*size/count) + .5);
  if (index>=0 && index<size)
    return (unsigned char)(colors[index]->green() * UCHAR_MAX);
  else
    return 0;
}

unsigned char LUTColorMap::getBlueChar(int ii, int count)
{
  int size = colors.count();
  int index = (int)((ii*size/count) + .5);
  if (index>=0 && index<size)
    return (unsigned char)(colors[index]->blue() * UCHAR_MAX);
  else
    return 0;
}

unsigned short LUTColorMap::getRedShrt(int ii, int count)
{
  int size = colors.count();
  int index = (int)((ii*size/count) + .5);
  if (index >=0 && index < size)
    return (unsigned short)(colors[index]->red() * USHRT_MAX);
  else
    return 0;
}

unsigned short LUTColorMap::getGreenShrt(int ii, int count)
{
  int size = colors.count();
  int index = (int)((ii*size/count) + .5);
  if (index>=0 && index<size)
    return (unsigned short)(colors[index]->green() * USHRT_MAX);
  else
    return 0;
}

unsigned short LUTColorMap::getBlueShrt(int ii, int count)
{
  int size = colors.count();
  int index = (int)((ii*size/count) + .5);
  if (index>=0 && index<size)
    return (unsigned short)(colors[index]->blue() * USHRT_MAX);
  else
    return 0;
}

ostream& operator<<(ostream& str, LUTColorMap& cc)
{
  cc.colors.head();
  do
    str << *cc.colors.current();
  while (cc.colors.next());
  return str;
}