/
ColormapReader.js
349 lines (303 loc) · 10.7 KB
/
ColormapReader.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
readColorMapsFromXml = function(filename, colorMapSize, onloaded)
{
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", filename, true);
xmlhttp.onload = function() {
xmlDoc = xmlhttp.responseXML;
if(xmlDoc == null)
{
alert("Error reading XML file");
return;
}
/*ar colormaps = xmlDoc.getElementsByTagName("ColorMaps");
console.log(colormaps[0]);
console.log(colormaps[0].children);*/
var colorMaps = [];
var colorMapElements = xmlDoc.getElementsByTagName('ColorMap');
for(var i = 0; i < colorMapElements.length; ++i)
{
var colorMapElement = colorMapElements[i];
var colorMapName = colorMapElement.getAttribute('name');
if(colorMapName == null)
colorMapName = "Color map " + (i + 1);
var colorMapGroup = colorMapElement.getAttribute('group');
if(colorMapGroup == null)
colorMapGroup = "";
var colorTable = new InterpolatedColorMap();
var nanColor = [0.0, 0.0, 0.0];
//console.log(colorMapElements[i].getElementsByTagName('Point').length);
//console.log(colorMapElements[i]);
var colorMapPoints = colorMapElement.getElementsByTagName('Point');
for(var j = 0; j < colorMapPoints.length; ++j)
{
var colorMapPoint = colorMapPoints[j];
var x = colorMapPoint.getAttribute('x');
var r = colorMapPoint.getAttribute('r');
var g = colorMapPoint.getAttribute('g');
var b = colorMapPoint.getAttribute('b');
var a = colorMapPoint.getAttribute('o');
if(a === null)
a = 1.0;
if(x !== null && r !== null && g !== null && b !== null &&
r >= 0.0 && r <= 1.0 && g >= 0.0 && g <= 1.0 && b >= 0.0 && b <= 1.0)
colorTable.AddColor(x, r * 255.0, g * 255.0, b * 255.0, a * 255.0);
}
var colorMapNaNs = colorMapElement.getElementsByTagName('NaN');
if(colorMapNaNs.length > 0)
{
var colorMapNaN = colorMapNaNs[0];
var r = colorMapPoint.getAttribute('r');
var g = colorMapPoint.getAttribute('g');
var b = colorMapPoint.getAttribute('b');
if(r !== null && g !== null && b !== null &&
r >= 0.0 && r <= 1.0 && g >= 0.0 && g <= 1.0 && b >= 0.0 && b <= 1.0)
nanColor = [r, g, b];
}
var colorMapSectionElements = colorMapElement.getElementsByTagName('Section');
var colorMapSections = [];
var colorMapSplitters = [
{fixed: true, pos: 0.0, left: null, right: null},
{fixed: true, pos: 1.0, left: null, right: null}
];
for(var j = 0; j < colorMapSectionElements.length; ++j)
{
var colorMapSectionElement = colorMapSectionElements[j];
var colorMapName2 = colorMapSectionElement.getAttribute('colorMapName');
var startIndex = colorMapSectionElement.getAttribute('startIndex');
var endIndex = colorMapSectionElement.getAttribute('endIndex');
var startPos = colorMapSectionElement.getAttribute('startPos');
var endPos = colorMapSectionElement.getAttribute('endPos');
var startValue = colorMapSectionElement.getAttribute('startValue');
if(startValue === null) startValue = 0.0;
var endValue = colorMapSectionElement.getAttribute('endValue');
if(endValue === null) endValue = 1.0;
var flipped = colorMapSectionElement.getAttribute('flipped');
if(flipped === null) flipped = false;
var startAlpha = colorMapSectionElement.getAttribute('startAlpha');
if(startAlpha === null) startAlpha = 1.0;
var endAlpha = colorMapSectionElement.getAttribute('endAlpha');
if(endAlpha === null) endAlpha = 1.0;
if(colorMapName2 !== null && startIndex !== null && endIndex !== null && startPos !== null && endPos !== null)
{
// Resize colorMapSplitters to include both startIndex && endIndex
for(var k = colorMapSplitters.length; k <= startIndex; ++k)
colorMapSplitters.push({});
for(var k = colorMapSplitters.length; k <= endIndex; ++k)
colorMapSplitters.push({});
// Add section
var section = {
colorMap: colorMapName2,
start: colorMapSplitters[parseInt(startIndex)],
end: colorMapSplitters[parseInt(endIndex)],
startValue: parseFloat(startValue),
endValue: parseFloat(endValue),
flipped: (flipped === 'true'),
startAlpha: startAlpha,
endAlpha: endAlpha
};
colorMapSections.push(section);
// Define start splitter
if(!('fixed' in colorMapSplitters[startIndex])) // If colorMapSplitters[startIndex] is not yet defined
{
colorMapSplitters[startIndex].fixed = false;
colorMapSplitters[startIndex].pos = parseFloat(startPos);
colorMapSplitters[startIndex].left = null;
colorMapSplitters[startIndex].right = section;
}
else
colorMapSplitters[startIndex].right = section;
// Define end splitter
if(!('fixed' in colorMapSplitters[endIndex])) // If colorMapSplitters[endIndex] is not yet defined
{
colorMapSplitters[endIndex].fixed = false;
colorMapSplitters[endIndex].pos = parseFloat(endPos);
colorMapSplitters[endIndex].left = section;
colorMapSplitters[endIndex].right = null;
}
else
colorMapSplitters[endIndex].left = section;
}
}
colorMaps[colorMapName] = {
name: colorMapName,
group: colorMapGroup,
nanclr: nanColor,
bytes: colorTable.Create(colorMapSize),
flipped: 0,
inPicker: true,
interpolatedColorMap: colorTable,
//controlPoints: colorTable.getSamples(),
//controlPointKeys: colorTable.getKeys(),
sections: colorMapSections.length <= 1 ? null : colorMapSections,
splitters: colorMapSplitters.length <= 2 ? null : colorMapSplitters
};
var colorMapDefault = colorMapElement.getAttribute('default');
if(colorMaps._default == null && colorMapDefault === "true")
colorMaps._default = colorMaps[colorMapName];
}
if(colorMaps._default == null)
for(var key in colorMaps)
{
colorMaps._default = colorMaps[key];
break;
}
onloaded(colorMaps);
};
xmlhttp.send();
}
function ColormapXmlSerializer()
{
this.header = function()
{
return '<ColorMaps>\n';
}
this.colormapStart = function(group, name)
{
return '<ColorMap space="Lab" indexedLookup="false" group="' + group + '" name="' + name + '">\n';
}
this.point = function(x, r, g, b, a)
{
return '<Point x="' + x + '" o="' + a + '" r="' + r + '" g="' + g + '" b="' + b + '"/>\n';
}
this.section = function(section, startIndex, endIndex)
{
return '<Section colorMapName="' + section.colorMap.name +
'" startIndex="' + startIndex + '" endIndex="' + endIndex +
'" startPos="' + section.start.pos + '" endPos="' + section.end.pos +
'" startValue="' + section.startValue + '" endValue="' + section.endValue +
'" flipped="' + (section.flipped ? 'true' : 'false') +
'" startAlpha="' + section.startAlpha + '" endAlpha="' + section.endAlpha +
'"/>\n'
;
}
this.colormapEnd = function()
{
return '</ColorMap>\n';
}
this.footer = function()
{
return '</ColorMaps>\n';
}
}
function ColormapXmlSerializer()
{
// Write header
var cm = '<ColorMaps>\n';
this.colormapStart = function(group, name)
{
cm += '\t<ColorMap space="Lab" indexedLookup="false" group="' + group + '" name="' + name + '">\n';
}
this.point = function(x, r, g, b, a)
{
cm += '\t\t<Point x="' + x + '" o="' + a + '" r="' + r + '" g="' + g + '" b="' + b + '"/>\n';
}
this.section = function(section, startIndex, endIndex)
{
cm += '\t\t<Section colorMapName="' + section.colorMap.name +
'" startIndex="' + startIndex + '" endIndex="' + endIndex +
'" startPos="' + section.start.pos + '" endPos="' + section.end.pos +
'" startValue="' + section.startValue + '" endValue="' + section.endValue +
'" flipped="' + (section.flipped ? 'true' : 'false') +
'" startAlpha="' + section.startAlpha + '" endAlpha="' + section.endAlpha +
'"/>\n'
;
}
this.colormapEnd = function()
{
cm += '\t</ColorMap>\n';
}
this.serialize = function()
{
// Write footer
cm += '</ColorMaps>\n';
return cm;
}
}
function ColormapJsonSerializer()
{
var colormaps = [];
var currentColormap = null;
this.colormapStart = function(group, name)
{
currentColormap = {space: "Lab", indexedLookup: false, group: group, name: name, points: [], sections: []};
}
this.point = function(x, r, g, b, a)
{
currentColormap.points.push({x: x, o: a, r: r, g: g, b: b});
}
this.section = function(section, startIndex, endIndex)
{
currentColormap.sections.push({
colorMapName: section.colorMap.name,
startIndex: startIndex,
endIndex: endIndex,
startPos: section.start.pos,
endPos: section.end.pos,
startValue: section.startValue,
endValue: section.endValue,
flipped: section.flipped ? true : false,
startAlpha: section.startAlpha,
endAlpha: section.endAlpha
});
}
this.colormapEnd = function()
{
colormaps.push(currentColormap);
currentColormap = null;
}
this.serialize = function()
{
return JSON.stringify({colormaps: colormaps});
}
}
function ColormapPngSerializer(width, height)
{
var points = [];
var colormapBytes = new Uint8Array(4 * width);
for (var i = 0; i < 4 * width; ++i)
colormapBytes[i] = 0;
this.colormapStart = function(group, name) { }
this.point = function(x, r, g, b, a)
{
points.push({x: x, o: a, r: r, g: g, b: b});
}
this.section = function(section, startIndex, endIndex)
{
var interpolatedColormap = new InterpolatedColorMap();
for (var i = points.length - 1; i >= 0; --i)
if (points[i].x >= section.start.pos && points[i].x <= section.end.pos)
{
var point = points[i];
interpolatedColormap.AddColor(point.x, point.r * 255.0, point.g * 255.0, point.b * 255.0, point.o * 255.0);
points.splice(i, 1);
}
var startPos = Math.max(0, Math.min(width - 1, Math.floor(section.start.pos * width)));
var endPos = Math.max(0, Math.min(width - 1, Math.floor(section.end.pos * width)));
var len = endPos - startPos + 1;
var bytes = interpolatedColormap.Create(len);
//console.log(startPos);
//console.log(endPos);
for (var i = 0; i < len; ++i)
if (colormapBytes[(startPos + i) * 4 + 3] === 0)
{
colormapBytes[(startPos + i) * 4 + 0] = bytes[i * 4 + 0];
colormapBytes[(startPos + i) * 4 + 1] = bytes[i * 4 + 1];
colormapBytes[(startPos + i) * 4 + 2] = bytes[i * 4 + 2];
colormapBytes[(startPos + i) * 4 + 3] = bytes[i * 4 + 3];
}
}
this.colormapEnd = function() { }
this.serialize = function()
{
// Create a 2D canvas to store the result
var tempCanvas = document.createElement('canvas');
tempCanvas.width = width;
tempCanvas.height = height;
// Copy the pixels to a 2D canvas
var imageData = tempCanvas.getContext('2d').createImageData(width, 1);
imageData.data.set(colormapBytes);
for (var y = 0; y < height; ++y)
tempCanvas.getContext('2d').putImageData(imageData, 0, y);
return tempCanvas.toDataURL();
}
}