-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathparserLUT.js
More file actions
154 lines (112 loc) · 3.66 KB
/
Copy pathparserLUT.js
File metadata and controls
154 lines (112 loc) · 3.66 KB
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
/*
*
* xxxxxxx xxxxxxx
* x:::::x x:::::x
* x:::::x x:::::x
* x:::::xx:::::x
* x::::::::::x
* x::::::::x
* x::::::::x
* x::::::::::x
* x:::::xx:::::x
* x:::::x x:::::x
* x:::::x x:::::x
* THE xxxxxxx xxxxxxx TOOLKIT
*
* http://www.goXTK.com
*
* Copyright (c) 2012 The X Toolkit Developers <dev@goXTK.com>
*
* The X Toolkit (XTK) is licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* "Free software" is a matter of liberty, not price.
* "Free" as in "free speech", not as in "free beer".
* - Richard M. Stallman
*
*
*/
// provides
goog.provide('X.parserLUT');
// requires
goog.require('X.event');
goog.require('X.parser');
goog.require('X.triplets');
/**
* Create a parser for color maps (look-up tables).
*
* @constructor
* @extends X.parser
*/
X.parserLUT = function() {
//
// call the standard constructor of X.base
goog.base(this);
//
// class attributes
/**
* @inheritDoc
* @const
*/
this._classname = 'parserLUT';
};
// inherit from X.parser
goog.inherits(X.parserLUT, X.parser);
/**
* @inheritDoc
*/
X.parserLUT.prototype.parse = function(container, object, data, flag) {
X.TIMER(this._classname + '.parse');
var colortable = container;
this._data = data;
var _bytes = this.scan('uchar', data.byteLength);
var _length = _bytes.length;
var _rangeStart = 0;
var i;
for (i = 0; i < _length; i++) {
if (_bytes[i] == 10) {
// the current byte is a line break
// grab a line
var line = this.parseChars(_bytes, _rangeStart, i);
_rangeStart = i + 1;
// now we have the line
// trim the line
line = line.replace(/^\s+|\s+$/g, '');
// ignore comments
if (line[0] == '#') {
continue;
}
// split each line
var lineFields = /** @type {Array} */ (line.split(' '));
// filter out multiple blanks
lineFields = lineFields.filter(function(v) {
return v != '';
});
// check if we have 6 values
if (lineFields.length != 6) {
// ignore this line
continue;
}
// here, we have a valid array containing
// labelValue, labelName, r, g, b, a
// convert r, g, b, a to the range 0..1 and don't forget to make it a
// number
lineFields[2] = parseInt(lineFields[2], 10) / 255; // r
lineFields[3] = parseInt(lineFields[3], 10) / 255; // g
lineFields[4] = parseInt(lineFields[4], 10) / 255; // b
lineFields[5] = parseInt(lineFields[5], 10) / 255; // a
// .. push it
colortable.add(parseInt(lineFields[0], 10), lineFields[1], lineFields[2],
lineFields[3], lineFields[4], lineFields[5], 10);
}
}
X.TIMERSTOP(this._classname + '.parse');
// the object should be set up here, so let's fire a modified event
var modifiedEvent = new X.event.ModifiedEvent();
modifiedEvent._object = object;
modifiedEvent._container = container;
this.dispatchEvent(modifiedEvent);
};
// export symbols (required for advanced compilation)
goog.exportSymbol('X.parserLUT', X.parserLUT);
goog.exportSymbol('X.parserLUT.prototype.parse', X.parserLUT.prototype.parse);