forked from neurolabusc/surf-ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorTable.pas
executable file
·418 lines (388 loc) · 12.8 KB
/
colorTable.pas
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
unit colorTable;
{$ifdef fpc}{$mode delphi}{$endif}
interface
uses
Classes, SysUtils, dialogs, userdir, prefs, IniFiles, define_types;
const //maximum number of control points for color schemes...
maxNodes = 32;
kPaintHideDefaultBehavior = -1;
kPaintHideDarkHideBright = 0;
kPaintHideDarkShowBright = 1;
kPaintShowDarkHideBright = 2;
kPaintShowDarkShowBright = 3;
type
TLUT = array [0..255] of TRGBA;
TLUTnodes = record
isFreeSurfer: boolean; //make items brighter or darker than range transparent
rgba: array [0..maxNodes] of TRGBA;
intensity: array [0..maxNodes] of integer;
end;
function UpdateTransferFunction (var lIndex: integer; isInvert: boolean): TLUT;//change color table
function CLUTDir: string;
function blendRGBA(c1, c2: TRGBA ): TRGBA;
function blendRGBAover(ca, cb: TRGBA ): TRGBA;
function maxRGBA(c1, c2: TRGBA ): TRGBA;
//function inten2rgb(intensity, mn, mx: single; lut: TLUT): TRGBA;
function inten2rgb(intensity, mn, mx: single; lut: TLUT; mode: integer): TRGBA; overload;
//function inten2rgb(intensity, mn, mx: single; lut: TLUT): TRGBA; overload;
function inten2rgb1(intensity, mn, mx: single; lut: TLUT): TRGBA; //use 1st not 0th LUT color (0 is transparent)
function desaturateRGBA ( rgba: TRGBA; frac: single; alpha: byte): TRGBA;
function isFreeSurferLUT(lIndex: integer): boolean;
implementation
uses mainunit;
function isFreeSurferLUT(lIndex: integer): boolean;
begin
result := (lIndex >= 15) and (lIndex <= 18);
end;
function blendRGBAover(ca, cb: TRGBA ): TRGBA;
//https://en.wikipedia.org/wiki/Alpha_compositing#Analytical_derivation_of_the_over_operator
var
aa, ab, ao: single;
begin
if cb.A = 0 then exit(ca);
if ca.A = 0 then exit(cb);
aa := ca.A / 255;
ab := cb.A / 255;
ao := 1.0 - (1.0-aa)*(1.0-ab);
result.R := round(((aa*ca.r)+(1-aa)*ab*cb.r)/ao) ;
result.G := round(((aa*ca.g)+(1-aa)*ab*cb.g)/ao) ;
result.B := round(((aa*ca.b)+(1-aa)*ab*cb.b)/ao) ;
result.A := round(ao * 255.0);
end;
function blendRGBA(c1, c2: TRGBA ): TRGBA;
var
frac1, frac2: single;
begin
result := c1;
if c2.A = 0 then exit;
result := c2;
if c1.A = 0 then exit;
frac1 := c1.A / (c1.A+c2.A);
frac2 := 1 - frac1;
result.R := round(c1.R*frac1 + c2.R*frac2) ;
result.G := round(c1.G*frac1 + c2.G*frac2);
result.B := round(c1.B*frac1 + c2.B*frac2);
if frac1 >= 0.5 then //result.a = max(c1.a, c2.a)
result.A := c1.A
else
result.A := c2.A;
end;
function maxRGBA(c1, c2: TRGBA ): TRGBA;
begin
result := c1;
if c2.A = 0 then exit;
if (c2.R > result.R) then
result.R := c2.R;
if (c2.G > result.G) then
result.G := c2.G;
if (c2.B > result.B) then
result.B := c2.B;
if (c2.A > result.A) then
result.A := c2.A;
end;
function inten2rgb(intensity, mn, mx: single; lut: TLUT; mode: integer): TRGBA; overload;
var
i: byte;
isInvert : boolean;
begin
if (mn < 0) and (mx < 0) and (mode = kPaintHideDefaultBehavior) then begin
if intensity >= mx then
exit( lut[0])
else if intensity <= mn then
exit( lut[255])
else
exit( lut[round(255* (1.0- (intensity-mn)/(mx-mn)))]);
end;
if intensity > mx then begin
i := 255;
if (mode = kPaintHideDarkHideBright) or (mode = kPaintShowDarkHideBright) then //hide bright
i := 0;
end else if intensity < mn then begin
i := 0;
if (mode = kPaintShowDarkHideBright) or (mode = kPaintShowDarkShowBright) then //hide dark
i := 1;
end else begin
i := round(255*(intensity-mn)/(mx-mn));
if (i = 0) and ((mode = kPaintHideDefaultBehavior) or (mode = kPaintShowDarkHideBright) or (mode = kPaintShowDarkShowBright)) then //hide dark
i := 1;
end;
result := lut[i];
end;
function inten2rgb(intensity, mn, mx: single; lut: TLUT): TRGBA; overload;
begin
//result := inten2rgb(intensity, mn, mx, lut, kPaintHideDefaultBehavior);
result := inten2rgb(intensity, mn, mx, lut,kPaintHideDarkHideBright);
end;
(*function inten2rgb(intensity, mn, mx: single; lut: TLUT): TRGBA;
begin
if (mn < 0) and (mx < 0) then begin
if intensity >= mx then begin
result := lut[0];
end else if intensity <= mn then
result := lut[255]
else
result := lut[round(255* (1.0- (intensity-mn)/(mx-mn)))];
end else begin
if (intensity <= mn) and (intensity <> 0) and (LUT[0].A <> 0) then
result := lut[1]
else if intensity < mn then
result := lut[0]
else if intensity = mn then begin
result := lut[0];
result.A := lut[1].A;
end else if intensity >= mx then
result := lut[255]
else
result := lut[round(255*(intensity-mn)/(mx-mn))];
end;
end; *)
function inten2rgb1(intensity, mn, mx: single; lut: TLUT): TRGBA; //use 1st not 0th LUT color (0 is transparent)
var i :integer;
begin
if (mn < 0) and (mx < 0) then begin
if intensity >= mx then
i := 0
else if intensity <= mn then
i := 255
else
i := round(255* (1.0- (intensity-mn)/(mx-mn)));
end else begin
if intensity <= mn then
i := 0
else if intensity >= mx then
i := 255
else
i := round(255*(intensity-mn)/(mx-mn));
end;
if (i < 1) then i := 1;
result := lut[i];
end;
function desaturateRGBA ( rgba: TRGBA; frac: single; alpha: byte): TRGBA;
var
y: single;
begin
//convert RGB->YUV http://en.wikipedia.org/wiki/YUV
y := 0.299 * rgba.r + 0.587 * rgba.g + 0.114 * rgba.b;
result.r := round(y * (1-frac) + rgba.r * frac);
result.g := round(y * (1-frac) + rgba.g * frac);
result.b := round(y * (1-frac) + rgba.b * frac);
result.a := alpha;
end;
function CLUTDir: string;
begin
//result := extractfilepath(paramstr(0))+'lut';
result := AppDir+'lut';
{$IFDEF UNIX}
if fileexists(result) then exit;
result := '/usr/share/surfice/lut';
if fileexists(result) then exit;
result := AppDir+'lut';
{$ENDIF}
end;
procedure setNode (r,g,b,a,i, node: integer; var lLUTnodes : TLUTnodes);
begin
lLUTnodes.rgba[node].R := r;
lLUTnodes.rgba[node].G := g;
lLUTnodes.rgba[node].B := b;
lLUTnodes.rgba[node].A := a;
lLUTnodes.intensity[node] := i;
end;
function loadCustomLUT(var lIndex: integer): TLUTnodes;
var
lFilename: string;
lIniFile: TIniFile;
numnodes, i: integer;
inten: byte;
rgba: TRGBA;
begin
setNode(0,0,0,0,0, 0, result);
setNode(255,255,255,255,255, 1, result);
result.isFreeSurfer:= false;
lFilename := CLUTdir+pathdelim+GLForm1.LayerColorDrop.Items[lIndex]+'.clut';
if not fileexists(lFilename) then begin
lIndex := 0;
exit;
end;
lIniFile := TIniFile.Create(lFilename);
IniInt(true,lIniFile, 'numnodes', numnodes);
if (numnodes < 1) or (numnodes > maxNodes) then begin
if (numnodes > maxNodes) then
showmessage(format('Too many nodes (%d, maximum %d)', [numnodes, maxNodes]));
lIniFile.Free;
lIndex := 0;
exit;
end;
for i := 0 to (numnodes-1) do begin
IniByte(true,lIniFile, 'nodeintensity'+inttostr(i),inten);
IniRGBA(true,lIniFile, 'nodergba'+inttostr(i),rgba);
setNode(rgba.r, rgba.g, rgba.b, rgba.a, inten, i, result);
end;
lIniFile.Free;
end;
function makeLUT(var lIndex: integer): TLUTnodes;
begin
//generate default grayscale color table
//result.numnodes := 2; //number of nodes implicit: final node has intensity=255
setNode(0,0,0,0,0, 0, result);
setNode(255,255,255,255,255, 1, result);
result.isFreeSurfer:= false;
case lIndex of //generate alternative color table if specified
0: exit; //default grayscale
1: begin //Red-Yellow
setNode(192,0,0,0,0, 0, result);
setNode(255,255,0,255,255, 1, result);
end;
2: begin //Blue-Green
setNode(0,0,192,0,0, 0, result);
setNode(0,255,128,255,255, 1, result);
end;
3: begin //Red
setNode(255,0,0,255,255, 1, result);
end;
4: begin //Green
setNode(0,255,0,255,255, 1, result);
end;
5: begin //Blue
setNode(0,0,255,255,255, 1, result);
end;
6: begin //Violet R+B
setNode(255,0,255,255,255, 1, result);
end;
7: begin //Yellow R+G
setNode(255,255,0,255,255, 1, result);
end;
8: begin //Cyan B+G
setNode(0,255,255,255,255, 1, result);
end;
9: begin //HotLut
//result.numnodes:=4;
setNode(3,0,0,0,0, 0, result);
setNode(255,0,0,48,95, 1, result);
setNode(255,255,0,96,191, 2, result);
setNode(255,255,255,128,255, 3, result);
end;
10: begin //bone
//result.numnodes:=3;
setNode(0,0,0,0,0, 0, result);
setNode(103,126,165,76,153, 1, result);
setNode(255,255,255,128,255, 2, result);
end;
11: begin //WinterLut
//result.numnodes:=3;
setNode(0,0,255,0,0, 0, result);
setNode(0,128,196,64,128, 1, result);
setNode(0,255,128,128,255, 2, result);
end;
12: begin //GE-Color
//result.numnodes:=5;
setNode(0,0,0,0,0, 0, result);
setNode(0,128,125,32,63, 1, result);
setNode(128,0,255,64,128, 2, result);
setNode(255,128,0,96,192, 3, result);
setNode(255,255,255,128,255, 4, result);
end;
13: begin //ACTC
//result.numnodes:=5;
setNode(0,0,0,0,0, 0, result);
setNode(0,0,136,32,64, 1, result);
setNode(24,177,0,64,128, 2, result);
setNode(248,254,0,78,156, 3, result);
setNode(255,0,0,128,255, 4, result);
end;
14: begin //X-rain
//result.numnodes:=7;
setNode(0,0,0,0,0, 0, result);
setNode(64,0,128,8,32, 1, result);
setNode(0,0,255,16,64, 2, result);
setNode(0,255,0,24,96, 3, result);
setNode(255,255,0,32,160, 4, result);
setNode(255,192,0,52,192, 5, result);
setNode(255,3,0,80,255, 6, result);
end;
15: begin //FreeSurferCurve - valleys dark
result.isFreeSurfer:= true;
setNode( 0, 0, 0, 0, 0, 0, result);
setNode( 0, 0, 0, 0,136, 1, result);
setNode( 0, 0, 0,140,155, 2, result);
setNode( 0, 0, 0,200,255, 3, result);
//setNode(255,0,0,255,0, 0, result);
//setNode(0,255,0,0,156, 1, result);
//setNode(0,0,255,255,255, 2, result);
end;
16: begin //FreeSurferCurve - curves (valleys and ridges) dark
result.isFreeSurfer:= true;
setNode(0,0,0,255,0, 0, result);
setNode(0,0,0,0,100, 1, result);
setNode(0,0,0,0,156, 2, result);
setNode(0,0,0,255,255, 3, result);
end;
17: begin //FreeSurferCurve - flat surfaces darkened
result.isFreeSurfer:= true;
setNode(0,0,0,0,0, 0, result);
setNode(0,0,0,0,100, 1, result);
setNode(0,0,0,255,128, 2, result);
setNode(0,0,0,0,156, 3, result);
setNode(0,0,0,0,255, 4, result);
end;
18: begin //FreeSurferCurve - ridges dark
result.isFreeSurfer:= true;
setNode(0,0,0,255,0, 0, result);
setNode(0,0,0,0,100,1, result);
setNode(0,0,0,0,255, 2, result);
end;
else begin
result := loadCustomLUT(lIndex); //index unknown!!!
end;
end; //case: alternative LUTs
end; //makeLUT()
function lerpRGBA (p1,p2: TRGBA; frac: single): TRGBA;
//linear interpolation
begin
result.R := round(p1.R + frac * (p2.R - p1.R));
result.G := round(p1.G + frac * (p2.G - p1.G));
result.B := round(p1.B + frac * (p2.B - p1.B));
result.A := round(p1.A + frac * (p2.A - p1.A));
end;//lerpRGBA()
function UpdateTransferFunction (var lIndex: integer; isInvert: boolean): TLUT;//change color table
var
lLUTnodes :TLUTnodes;
lInc,lNodeLo: integer;
frac, f: single;
rev: TLUT;
begin
lLUTNodes := makeLUT(lIndex);
lNodeLo := 0;
result[0] := lerpRGBA(lLUTNodes.rgba[0],lLUTNodes.rgba[0],1);
for lInc := 1 to 255 do begin
f := lInc;
if (f < 0) then f := 0;
if (f > 255) then f := 255;
//if ((lNodeLo+1) < lLUTNodes.numnodes) and ( f > lLUTNodes.intensity[lNodeLo + 1] ) then
if ( f > lLUTNodes.intensity[lNodeLo + 1] ) then
lNodeLo := lNodeLo + 1;
frac := (f-lLUTNodes.Intensity[lNodeLo])/(lLUTNodes.Intensity[lNodeLo+1]-lLUTNodes.Intensity[lNodeLo]);
if (frac < 0) then frac := 0;
if frac > 1 then frac := 1;
result[lInc] := lerpRGBA(lLUTNodes.rgba[lNodeLo],lLUTNodes.rgba[lNodeLo+1],frac);
end;
if isInvert then begin
rev := result;
for lInc := 0 to 255 do
result[lInc] := rev[255-lInc];
result[0].A := rev[0].A;
result[255].A := rev[255].A;
end;
if lLUTNodes.isFreeSurfer then begin
//for lInc := 1 to 255 do
// result[lInc].A := 128;
//result[255].R := 0;
//result[255].G := 0;
//result[255].B := 0;
//result[255].A := 255;//result[254].A;
exit; //not for freesurfer
end;
//result[0].A := 0; //see LUT[0].A <> 0
for lInc := 1 to 255 do
result[lInc].A := 255;
end;//LoadLUT()
end.