-
Notifications
You must be signed in to change notification settings - Fork 24
/
drawu.pas
executable file
·1437 lines (1354 loc) · 49.3 KB
/
drawu.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit drawu;
{$include opts.inc}
{$IFDEF FPC}{$mode delphi}{$ENDIF}
interface
uses
{$IFDEF DGL} dglOpenGL, {$ELSE DGL} {$IFDEF COREGL}glcorearb, {$ELSE} gl,glext, {$ENDIF} {$ENDIF DGL}
{$IFNDEF FPC} Windows, {$ENDIF} {$IFDEF COREGL} raycast_core, {$ELSE} raycast_legacy, {$ENDIF} raycast_common,
{$IFDEF USETRANSFERTEXTURE}texture_3d_unit_transfertexture, {$ELSE} {$ENDIF}
shaderu,dialogs,Classes,define_types, sysUtils; //clut, texture_3d_unit,
const
kDrawModeAppend = 0;
kDrawModeDelete = 1;
kDrawModeConstrain = 2;
type
TDraw = packed record //Next: analyze Format Header structure
dim3d: array [0..3] of int64; //unused,X,Y,Z voxels in volume space
dim2d: array [0..3] of int64; //orient,X,Y,Slice in slice space
currSlice, prevSlice, prevOrient: int64;
clickStartX, clickStartY,clickPrevX,clickPrevY: int64; //location of previous mouse buttons
colorLut: array [0..255] of TGLRGBQuad;
glslprogramId,view3dId, colorLutId: GLuint;
penColor : byte;
view3d, view2d, undo2d, modified2d: bytep0;
//start2dOXYZ, prev2dOXYZ : array [0..3] of integer; //orient,X,Y,Slice for pixel coordinates
doRedraw,doAlpha, isMouseDown, isModified, isForceCreate: boolean;
end;
var gDraw: TDraw;
function voiDescriptives: int64;
procedure StartDrawGLSL;
procedure voiUndo;
procedure voiCloseSlice;
procedure voiInterpolate(Orient: int64);
procedure voiMouseUp(autoClose, overwriteColors: boolean);
function voiMouseMove (Xfrac, Yfrac, Zfrac: single): boolean;
procedure voiMouseDown(Color, Orient: int64; Xfrac, Yfrac, Zfrac: single);
procedure voiMouseFloodFill(Color, Orient: int64; Xfrac, Yfrac, Zfrac: single);
procedure voiMorphologyFill(RGBAimg: bytep0; Color: int64; Xmm, Ymm, Zmm, Xfrac, Yfrac, Zfrac: single; dxOrigin, radiusMM: int64; drawMode: int64);
function voiOpenGLDraw: GLuint; //must be called from OpenGL
procedure voiChangeAlpha (a: byte);
procedure voiCreate(X,Y,Z: int64; ptr: byteP0);
procedure voiBinarize(Color: int64);
procedure voiClose;
function voiGetVolume: byteP0;
function voiActiveOrient: int64;
function voiIsModified: boolean;
function voiIsEmpty: boolean;
function voiActiveX: boolean;
function voiIsOpen: boolean;
procedure voiPasteSlice(Xfrac, Yfrac, Zfrac: single);
procedure voiColor (idx, r, g, b: byte);
procedure voiDefaultLUT;
//procedure voiSmooth;
procedure voiSmoothIntensity (RGBAimg: bytep0);
procedure voiSetModified(b: boolean);
implementation
uses
mainunit; //texture2raycast,checkTextureMemory
procedure voiSetModified(b: boolean);
begin
gDraw.isModified := b; //2015
end;
function voiDescriptives: int64;
var
i, nPix, nPixDraw: int64;
begin
if not voiIsOpen then begin
exit (-1);
end;
nPix := gDraw.dim3d[1] * gDraw.dim3d[2]*gDraw.dim3d[3];
if nPix < 1 then exit;
nPixDraw := 0;
for i := 0 to (nPix -1) do begin
if gDraw.view3d[i] <> 0 then
inc(nPixDraw);
end;
exit (nPixDraw);//('Volume of drawing is '+inttostr(nPixDraw)+' pixels.');
end;
function numThresh (v: singlep0; lStart,lEnd: int64; thresh: single): int64; //how many members of the array are >= thresh?
var
i: int64;
begin
result := 0;
for i := lStart to lEnd do
if v[i] >= thresh then inc(result);
end;
function maxVol (v: singlep0; lStart,lEnd: int64): single; //brightest and darkest
var
i: int64;
begin
result := v[0];
for i := lStart to lEnd do
if v[i] >= result then result := v[i];
end;
procedure meanStdInMask(mask, v: singlep0; lStart,lEnd: int64) ;
//modulate mask based on variance - in v
const
kFrac = 0.7; //0..1 what proportion of signal modulatated by intensity
var
mx: single;
mean, stdev, delta,m2: double;
i,n: int64;
begin
//1.) determine max intensity in mask, e.g. if a binary 0/1 mask this will be 1
mx := maxVol(mask,lStart,lEnd);
if mx = 0 then exit;
mx := 0.75 * mx;
//calculate stdev and mean with Welford on pass methodhttp://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
n := 0;
mean := 0.0;
m2 := 0.0;
for i := lStart to lEnd do begin
if (mask[i] >= mx) then begin
n := n + 1;
delta := v[i] - mean;
mean := mean + (delta/n);
m2 := m2 + (delta * (v[i] -mean) );
end;
end;
if (n < 2) then exit;
stdev := sqrt(m2/(n-1)); //convert to standard deviation
//GLForm1.Caption := inttostr(n)+' '+ floattostr(mean)+' '+floattostr(stdev);
for i := lStart to lEnd do begin
delta := (v[i] - mean)/stdev; //z-score
delta := abs(delta)/3; //e.g. Z= -1.5 -> 0.5
if delta > kFrac then delta := kFrac; //delta is clipped to 0..kFrac
delta := 1-delta;
//if (mask[i] > 0) then mask[i] := 243 * delta; //purely drive by intensity
mask[i] := mask[i] * (delta + (1-kFrac) );
end;
end;
procedure SmoothImg(vol: singlep0; X,Y,Z: int64);
var
vol2: singlep0;
i, nPix, dimOff: int64;
begin
if (X < 5) or (Y < 5) or (Z < 5) then exit;
nPix := X * Y * Z;
getmem(vol2, nPix * sizeof(single));
Move(vol^, vol2^,nPix * sizeof(single));//source/dest -> only for edges
for i := 2 to (nPix-1-2) do //smooth in X direction
vol[i] := vol2[i-2] + 2*vol2[i-1] + 3* vol2[i] + 2* vol2[i+1] + vol2[i+2]; //x9 of original value
dimOff := X;
for i := 2*dimOff to (nPix-1-(2*dimOff)) do //smooth in Y direction
vol2[i] := vol[i-2*dimOff] + 2*vol[i-dimOff] + 3* vol[i] + 2* vol[i+dimOff] + vol[i+2*dimOff]; //x9 of original value
dimOff := X * Y;
for i := 2*dimOff to (nPix-1-(2*dimOff)) do //smooth in Z direction
vol[i] := vol2[i-2*dimOff] + 2*vol2[i-dimOff] + 3* vol2[i] + 2* vol2[i+dimOff] + vol2[i+2*dimOff]; //x9 of original value
freemem(vol2);
end;
procedure SmoothVol(lColor: int64; intenVol: singlep0);
label
666;
var
vol: singlep0;
nPix,i, origVol,
tLo,tMid,tHi, nMid,t1stVox, tLastVox: int64;
begin
//volume dimensions must be at least 5x5x5 voxels for smooth
nPix := gDraw.dim3d[1] * gDraw.dim3d[2]*gDraw.dim3d[3];
getmem(vol, nPix * sizeof(single));
origVol := 0;
for i := 0 to (nPix-1) do
if (gDraw.view2d[i] = lColor) then begin
vol[i] := 1;
inc(origVol);
end else
vol[i] := 0;
if (origVol < 1) then goto 666;
SmoothImg(vol, gDraw.dim3d[1], gDraw.dim3d[2], gDraw.dim3d[3]);
t1stVox := -1;
tLastVox := -1;
for i := 0 to (nPix-1) do begin
vol[i] := vol[i] * (1/3); //normalize from 0..729 to 0..243
if vol[i] = 0.0 then begin
tLastVox := i;
if (t1stVox < 0) then t1stVox := i;
end;
end;
if (intenVol <> nil) then
meanStdInMask(vol, intenVol, t1stVox,tLastVox) ;
//find threshold that maintains overall volume
tLo := 1;
tMid := 121;
tHi := 243;
nMid := numThresh (vol, t1stVox,tLastVox , tMid);
while (tHi-tLo) > 3 do begin
if (nMid > origVol) then
tLo := tMid
else
tHi := tMid;
tMid := ((tHi-tLo) div 2) + tLo;
nMid := numThresh (vol, t1stVox,tLastVox , tMid);
end;
for i := 0 to (nPix-1) do
if (vol[i] >= tMid) then
gDraw.view2d[i] := lColor
else
gDraw.view2d[i] := 0;
666:
freemem(vol);
end;
const
kOrientSag = 3;
kOrientCoro = 2;
kOrientAx = 1;
kOrient3D = 0;
procedure PasteSlice2D(Orient: int64; var in2D, out3D: bytep0); //gDraw.view3d
var
volOffset, i, j, k, nPix: int64;
begin
nPix := gDraw.dim2d[1] * gDraw.dim2d[2]; //number of pixels in 2D slice
if (nPix < 1) or (Orient = kOrient3D) then exit;
if (Orient = kOrientSag) then begin//Sag
volOffset := gDraw.dim2d[3]; //sag slices are in X direction
i := 0; //sag is Y*Z
for j := 0 to (gDraw.dim3d[3]-1) do begin //read each slice (Z)
for k := 0 to (gDraw.dim3d[2]-1) do begin //read Y direction
out3D[(k*gDraw.dim3d[1]) +volOffset] := in2D[i];
i := i + 1;
end;
volOffset := volOffset + (gDraw.dim3d[1]*gDraw.dim3d[2]); //next Z slice
end;
end else if (Orient = kOrientCoro) then begin//Coro
volOffset := gDraw.dim2d[3]*gDraw.dim3d[1]; //coro slices are in Y direction
i := 0; //coro is X*Z
for j := 0 to (gDraw.dim3d[3]-1) do begin //read each slice (Z)
for k := 0 to (gDraw.dim3d[1]-1) do begin //read X direction
out3D[k+volOffset] := in2D[i];
i := i + 1;
end;
volOffset := volOffset + (gDraw.dim3d[1]*gDraw.dim3d[2]); //next Z slice
end;
end else begin //Axial
volOffset := gDraw.dim2d[3]*gDraw.dim3d[1]*gDraw.dim3d[2]; //axial slices are in Z direction, each X*Y pixels
Move(in2D^, out3D^[volOffset],nPix);//source/dest
end;
end;
(*procedure PasteSlice2D(Orient: int64; var in2D: bytep0);
var
volOffset, i, j, k, nPix: int64;
begin
nPix := gDraw.dim2d[1] * gDraw.dim2d[2]; //number of pixels in 2D slice
if (nPix < 1) or (Orient = kOrient3D) then exit;
if (Orient = kOrientSag) then begin//Sag
volOffset := gDraw.dim2d[3]; //sag slices are in X direction
i := 0; //sag is Y*Z
for j := 0 to (gDraw.dim3d[3]-1) do begin //read each slice (Z)
for k := 0 to (gDraw.dim3d[2]-1) do begin //read Y direction
gDraw.view3d[(k*gDraw.dim3d[1]) +volOffset] := in2D[i];
i := i + 1;
end;
volOffset := volOffset + (gDraw.dim3d[1]*gDraw.dim3d[2]); //next Z slice
end;
end else if (Orient = kOrientCoro) then begin//Coro
volOffset := gDraw.dim2d[3]*gDraw.dim3d[1]; //coro slices are in Y direction
i := 0; //coro is X*Z
for j := 0 to (gDraw.dim3d[3]-1) do begin //read each slice (Z)
for k := 0 to (gDraw.dim3d[1]-1) do begin //read X direction
gDraw.view3d[k+volOffset] := in2D[i];
i := i + 1;
end;
volOffset := volOffset + (gDraw.dim3d[1]*gDraw.dim3d[2]); //next Z slice
end;
end else begin //Axial
volOffset := gDraw.dim2d[3]*gDraw.dim3d[1]*gDraw.dim3d[2]; //axial slices are in Z direction, each X*Y pixels
Move(in2D^, gDraw.view3d^[volOffset],nPix);//source/dest
end;
end;*)
procedure UpdateView3d;
var
nPix: int64;
begin
if (gDraw.undo2d = nil) or (gDraw.view3d = nil) then exit;
if (gDraw.dim2d[0] = kOrient3D) then begin //3D volume
nPix := gDraw.dim3d[1]* gDraw.dim3d[2] * gDraw.dim3d[3];
if nPix < 1 then exit;
Move(gDraw.view2d^, gDraw.view3d^,nPix);//source/dest
exit;
end else
PasteSlice2D(gDraw.dim2d[0], gDraw.view2d, gDraw.view3d);
end;
procedure voiCloseSlice;
begin
if (gDraw.view2d <> nil) then freemem(gDraw.view2d);
if (gDraw.undo2d <> nil) then freemem(gDraw.undo2d);
if (gDraw.modified2d <> nil) then freemem(gDraw.modified2d);
gDraw.view2d := nil;
gDraw.undo2d := nil;
gDraw.modified2d := nil;
end;
procedure voiSmoothIntensity (RGBAimg: bytep0);
var
nPix, dark, bright, i, alpha: int64;
intenVol: singlep0;
begin
if (gDraw.view3d = nil) then exit;
if (gDraw.dim3d[1] < 5) or (gDraw.dim3d[2] < 5) or (gDraw.dim3d[3] < 5) then exit;
nPix := gDraw.dim3d[1] * gDraw.dim3d[2]*gDraw.dim3d[3];
dark := 256;
bright := 0;
for i := 0 to (nPix -1) do begin
if (gDraw.view3d[i] < dark) then dark := gDraw.view3d[i];
if (gDraw.view3d[i] > bright) then bright := gDraw.view3d[i];
end;
if (bright = dark) then exit; //no variability
voiCloseSlice; //2015
gDraw.dim2d[0] := kOrient3D; //dim[0] = slice orientation 0 = 3d volume
getmem(gDraw.view2d, nPix);
getmem(gDraw.undo2d, nPix);
Move(gDraw.view3d^, gDraw.undo2d^,nPix);//source/dest
Move(gDraw.undo2d^,gDraw.view2d^,nPix);//source/dest
if (RGBAimg <> nil) then begin
getmem(intenVol, nPix * sizeof(single));
alpha := 3; //array is red/green/blue/aplha 0=R,1=G,2=B,3=A
for i := 0 to nPix do begin
intenVol^[i] := RGBAimg[alpha];//Alpha
inc(alpha,4);
end; //for each vox
SmoothImg(intenVol, gDraw.dim3d[1], gDraw.dim3d[2], gDraw.dim3d[3]);
end else
intenVol := nil;
if (dark = 0) then dark := 1;
for i := dark to bright do
SmoothVol(i, intenVol);
if (RGBAimg <> nil) then
freemem(intenVol);
gDraw.doRedraw := true;
UpdateView3d;
gDraw.isModified := true;
end;
procedure voiColor (idx, r, g, b: byte);
begin
gDraw.doAlpha := true;
gDraw.colorLut[idx].rgbRed := r;
gDraw.colorLut[idx].rgbGreen := g;
gDraw.colorLut[idx].rgbBlue := b;
end;
function voiIsOpen: boolean;
begin
result := (gDraw.view3d <> nil);
end;
function voiIsModified: boolean;
begin
result := gDraw.isModified;
end;
function voiIsEmpty: boolean;
var
i,vx: int64;
begin
result := true;
vx := gDraw.dim3d[1] * gDraw.dim3d[2]*gDraw.dim3d[3];
if (vx < 0) or (gDraw.view3d = nil) then exit;
i := 0;
while (i < vx) and (gDraw.view3d[i] = 0) do
inc(i);
//showmessage(inttostr(i)+' '+inttostr(gDraw.view3d[i]));
if (i < vx) then
result := false;
end;
function frac2pix (frac: single; dimPix: int64): int64;
begin
//result := round(((frac- (0.5/dimPix))*dimPix))-1; //e.g. if 6 slices anything greater than 0.167 counted in slice 2
//result := round(frac * dimPix);
result := round((frac * dimPix)-0.5);
if (result < 0) then result := 0;
if (result >= dimPix) then result := dimPix - 1;
end;
procedure click2pix (var Xpix, Ypix: int64; Xfrac, Yfrac, Zfrac: single);
begin
if (gDraw.dim2d[0] = kOrientSag) then begin //Sag
Xpix := frac2pix(Yfrac, gDraw.dim2d[1]); //Sag is Y*Z
Ypix := frac2pix(Zfrac, gDraw.dim2d[2]); //Sag is Y*Z
end else if (gDraw.dim2d[0] = kOrientCoro) then begin //Coro
Xpix := frac2pix(Xfrac, gDraw.dim2d[1]); //Coro X*Z
Ypix := frac2pix(Zfrac, gDraw.dim2d[2]); //Coro is X*Z
end else begin //Axial
Xpix := frac2pix(Xfrac, gDraw.dim2d[1]); //Axial X*Y
//GLForm1.Caption := floattostr(Xfrac)+' fcx '+inttostr(gDraw.dim2d[1])+' '+inttostr(Xpix);
Ypix := frac2pix(Yfrac, gDraw.dim2d[2]); //Axial X*Y
end;
end;
function voiActiveX: boolean;
begin
if (gDraw.view3d = nil) then
result := false
else
result := true;
end;
function voiActiveOrient: int64;
begin
if (gDraw.view3d <> nil) and (gDraw.isMouseDown) then
result := gDraw.dim2d[0] //return 3D(0), Axial(1), Coronal(2) or Sagittal(3)
else
result := -1;
end;
function setDim2D(Orient: int64): int64;
begin
gDraw.dim2d[0] := Orient; //dim[0] = slice orient
if (Orient = 3) then begin
gDraw.dim2d[1] := gDraw.dim3d[2]; //Sag is Y*Z
gDraw.dim2d[2] := gDraw.dim3d[3]; //Sag is Y*Z
gDraw.dim2d[3] := gDraw.dim3d[1]; //Sag slices in X direction
//gDraw.dim2d[3] := frac2pix(Xfrac, gDraw.dim3d[1]); //sag slices select Left-Right slices
end else if (Orient = 2) then begin
gDraw.dim2d[1] := gDraw.dim3d[1]; //Coro X*Z
gDraw.dim2d[2] := gDraw.dim3d[3]; //Coro is X*Z
gDraw.dim2d[3] := gDraw.dim3d[2]; //Axial slices in Y direction
//gDraw.dim2d[3] := frac2pix(Yfrac, gDraw.dim3d[2]); //coro slices select Anterio-Posterior slices
end else begin //Axial
gDraw.dim2d[1] := gDraw.dim3d[1]; //Axial X*Y
gDraw.dim2d[2] := gDraw.dim3d[2]; //Axial X*Y
gDraw.dim2d[3] := gDraw.dim3d[3]; //Axial slices in Z direction
//gDraw.dim2d[3] := frac2pix(Zfrac, gDraw.dim3d[3]); //axial influences head-foot
end;
result := gDraw.dim2d[3];
end;
procedure CopySlice2D(Orient: int64; var out2D: bytep0);
var
volOffset, i, j, k, nPix: int64;
begin
nPix := gDraw.dim2d[1] * gDraw.dim2d[2]; //dim[3] = number of pixels
if (Orient = 3) then begin//Sag
volOffset := gDraw.dim2d[3]; //sag slices are in X direction
i := 0; //sag is Y*Z
for j := 0 to (gDraw.dim3d[3]-1) do begin //read each slice (Z)
for k := 0 to (gDraw.dim3d[2]-1) do begin //read Y direction
out2D[i] := gDraw.view3d[(k*gDraw.dim3d[1]) +volOffset];
i := i + 1;
end;
volOffset := volOffset + (gDraw.dim3d[1]*gDraw.dim3d[2]); //next Z slice
end;
end else if (Orient = 2) then begin//Coro
volOffset := gDraw.dim2d[3]*gDraw.dim3d[1]; //coro slices are in Y direction
i := 0; //coro is X*Z
for j := 0 to (gDraw.dim3d[3]-1) do begin //read each slice (Z)
for k := 0 to (gDraw.dim3d[1]-1) do begin //read X direction
out2D[i] := gDraw.view3d[k+volOffset];
i := i + 1;
end;
volOffset := volOffset + (gDraw.dim3d[1]*gDraw.dim3d[2]); //next Z slice
end;
end else begin //Axial
volOffset := gDraw.dim2d[3]*gDraw.dim3d[1]*gDraw.dim3d[2]; //axial slices are in Z direction, each X*Y pixels
Move(gDraw.view3d^[volOffset], out2D^,nPix);//source/dest
//GLForm1.caption := format('%d %d', [random(888), gDraw.dim2d[3]]);
end;
end;
procedure voiMouseDown(Color, Orient: int64; Xfrac, Yfrac, Zfrac: single);
//Orient: Ax(1), Cor(2), Sag(3)
var
nPix, nSlices, volOffset: int64;
begin
if (gDraw.view3d = nil) then exit;
if ((gDraw.dim3d[1] * gDraw.dim3d[2]*gDraw.dim3d[3]) < 1) then exit;
if (Color < 0) or (Color > 255) or (Xfrac < 0.0) or (Yfrac < 0.0) or (Zfrac < 0.0) or (Xfrac > 1.0) or (Yfrac > 1.0) or (Zfrac > 1.0) then exit;
if (Orient < 1) or (Orient > 3) then exit; //accept Axial, Sag, Coro
voiCloseSlice;
gDraw.penColor := Color;
nSlices := setDim2D(Orient);
if (Orient = 3) then
gDraw.dim2d[3] := frac2pix(Xfrac, gDraw.dim3d[1]) //sag slices select Left-Right slices
else if (Orient = 2) then
gDraw.dim2d[3] := frac2pix(Yfrac, gDraw.dim3d[2]) //coro slices select Anterio-Posterior slices
else //Axial
gDraw.dim2d[3] := frac2pix(Zfrac, gDraw.dim3d[3]); //axial influences head-foot
//store previous slice and orientation for interpolation between slices
gDraw.prevSlice := gDraw.currSlice;
gDraw.currSlice := gDraw.dim2d[3];
if gDraw.prevOrient <> Orient then //reset: e.g. we can not interpolate slices between coronal and axial orientations
gDraw.prevSlice := gDraw.currSlice;
gDraw.prevOrient := Orient;
//2D info
nPix := gDraw.dim2d[1] * gDraw.dim2d[2]; //dim[3] = number of pixels
if (nPix < 1) then exit;
//gDraw.dim2d[3] is the active slice we will be manipulating
if (gDraw.dim2d[3] < 0) or (gDraw.dim2d[3] >= nSlices) then exit;
//create slices holding initial pixel values
getmem(gDraw.view2d, nPix);
getmem(gDraw.undo2d, nPix);
getmem(gDraw.modified2d, nPix);
FillChar(gDraw.modified2d^,nPix,0); //set all to zero: nothing drawn yet
CopySlice2D(Orient, gDraw.undo2d);
Move(gDraw.undo2d^,gDraw.view2d^,nPix);//source/dest
//for i := 0 to (nPix div 4) do
// gDraw.view2d[i] := i mod 2;
//record mouse
click2pix (gDraw.clickStartX, gDraw.clickStartY, Xfrac, Yfrac, Zfrac);
gDraw.clickPrevX := gDraw.clickStartX;
gDraw.clickPrevY := gDraw.clickStartY;
gDraw.isMouseDown := true;
gDraw.isModified := true;
end;
const
kFillNewColor = 255;//255;
kIgnoreColor = 253;//253;
kFillOldColor = 0;
procedure borderPixel (x,y: int64);
var
px : int64;
begin
px := x + y* gDraw.dim2d[1];
if gDraw.modified2d[px] = kFillOldColor then
gDraw.modified2d[px] := kFillNewColor;
end;
procedure fillPixel (x,y: int64);
var
px : int64;
begin
px := x + y* gDraw.dim2d[1];
if gDraw.modified2d[px] <> kFillOldColor then exit;
gDraw.modified2d[px] := kFillNewColor;
fillPixel (x-1,y);
fillPixel (x+1,y);
fillPixel (x,y-1);
fillPixel (x,y+1);
end;
procedure fillPixelBound (x,y: int64);
//fill pixel with range checking
var
px : int64;
begin
px := x + y* gDraw.dim2d[1];
if gDraw.modified2d[px] <> kFillOldColor then exit;
gDraw.modified2d[px] := kFillNewColor;
if (x > 0) then fillPixelBound (x-1,y);
if (x < (gDraw.dim2d[1]-1)) then fillPixelBound (x+1,y);
if (y > 0) then fillPixelBound (x,y-1);
if (y < (gDraw.dim2d[2]-1)) then fillPixelBound (x,y+1);
end;
function isFillNewColor (x,y: int64): boolean;
begin
result := (gDraw.modified2d[x + y* gDraw.dim2d[1] ] = kFillNewColor);
end;
procedure setColor (x,y: int64; clr: byte);
begin
gDraw.modified2d[x + y* gDraw.dim2d[1]] := clr;
end;
procedure fillBubbles;
//from borders identifies all connected voxels of kFillOldColor and makes them kFillNewColor
var
i: int64;
begin
if (gDraw.view2d = nil) or (gDraw.modified2d = nil) then exit;
if (gDraw.dim2d[1] < 3) or (gDraw.dim2d[2] < 3) then exit;
//blank left and right sides to prevent overflow errors
for i := 0 to (gDraw.dim2d[2]-1) do begin
borderPixel(0,i);
borderPixel(gDraw.dim2d[1]-1,i);
end;
//blank top and bottom sides to prevent overflow errors
for i := 0 to (gDraw.dim2d[1]-1) do begin
borderPixel(i,0);
borderPixel(i,gDraw.dim2d[2]-1);
end;
//seed left and right edges
for i := 1 to (gDraw.dim2d[2]-2) do begin
if isFillNewColor(0,i) then fillPixel(1,i);
if isFillNewColor(gDraw.dim2d[1]-1,i) then fillPixel(gDraw.dim2d[1]-2,i);
end;
//seed top and bottom edges
for i := 1 to (gDraw.dim2d[1]-2) do begin
if isFillNewColor(i, 0) then fillPixel(i,1);
if isFillNewColor(i, gDraw.dim2d[2]-1) then fillPixel(i,gDraw.dim2d[2]-2);
end;
end;
procedure fillRegion;
var
i: int64;
begin
if (gDraw.view2d = nil) or (gDraw.modified2d = nil) then exit;
if (gDraw.dim2d[1] < 3) or (gDraw.dim2d[2] < 3) then exit;
fillBubbles;
for i := 0 to ((gDraw.dim2d[1]*gDraw.dim2d[2])-1) do
if (gDraw.modified2d[i] = kFillOldColor) then
gDraw.view2d[i] := gDraw.penColor;
end;
procedure doFloodFill(x,y: int64; newColor, oldColor: byte);
//set all voxels connected to location x,y of oldColor to have newColor
var
nPix, i: int64;
begin
nPix := gDraw.dim2d[1]*gDraw.dim2d[2];
for i := 0 to (nPix-1) do begin
if gDraw.view2d[i] = oldColor then
gDraw.modified2d[i] := kFillOldColor
else
gDraw.modified2d[i] := kIgnoreColor;
end;
fillPixelBound (x,y);
if newColor = oldColor then begin
for i := 0 to (nPix-1) do begin
if gDraw.modified2d[i] = kFillNewColor then
gDraw.modified2d[i] := kIgnoreColor
else
gDraw.modified2d[i] := kFillOldColor;
end;
fillBubbles; //any oldcolor connected to the border set to newcolor
for i := 0 to (nPix-1) do begin
if gDraw.modified2d[i] = kFillOldColor then
gDraw.modified2d[i] := kFillNewColor //fill pockets
else
gDraw.modified2d[i] := kIgnoreColor;
end;
end;
for i := 0 to (nPix-1) do
if gDraw.modified2d[i] = kFillNewColor then
gDraw.view2d[i] := newColor;
end;
procedure voiMouseFloodFill(Color, Orient: int64; Xfrac, Yfrac, Zfrac: single);
var
x,y: int64;
oldColor: byte;
begin
voiMouseDown(Color, Orient, Xfrac, Yfrac, Zfrac);
gDraw.isMouseDown := false;
if (gDraw.view2d = nil) or (gDraw.modified2d = nil) then exit;
if (gDraw.dim2d[1] < 3) or (gDraw.dim2d[2] < 3) then exit;
click2pix (x, y, Xfrac, Yfrac, Zfrac);
oldColor := gDraw.view2d[x + y* gDraw.dim2d[1]];
//GLForm1.caption :=inttostr(oldColor) +'->'+ inttostr(Color)+'xxxx'+inttostr(random(888));
doFloodFill(x,y, Color, oldColor);
gDraw.doRedraw := true;
UpdateView3d;
end;
procedure drawPixel (x,y: int64);
var
px : int64;
begin
px := x + y* gDraw.dim2d[1];
gDraw.modified2d[px] := kIgnoreColor;//1;
gDraw.view2d[px] := gDraw.penColor;
end;
procedure DrawLine (x,y, x2, y2: int64);
//http://www.edepot.com/lineb.html
var
yLonger: boolean;
i,incrementVal, shortLen, longLen, swap: int64;
multDiff: double;
begin
//drawPixel(x,y);
drawPixel(x2,y2);
yLonger:=false;
shortLen:=y2-y;
longLen:=x2-x;
if (abs(shortLen)>abs(longLen)) then begin
swap:=shortLen;
shortLen:=longLen;
longLen:=swap;
yLonger:=true;
end;
if (longLen<0) then
incrementVal:=-1
else
incrementVal:=1;
if (longLen=0.0) then
multDiff:=shortLen
else
multDiff:=shortLen/longLen;
i := 0;
if (yLonger) then begin
while (i <> longLen) do begin
drawPixel(round(x+(i*multDiff)),y+i);
i := i +incrementVal;
end;
end else begin
while (i <> longLen) do begin
drawPixel(x+i,round(y+(i*multDiff)));
i := i +incrementVal;
end;
end;
end; //DrawX
function voiMouseMove (Xfrac, Yfrac, Zfrac: single): boolean;
var
clickX, clickY: int64;
begin
result := false;
if not gDraw.isMouseDown then exit;
click2pix (clickX, clickY, Xfrac, Yfrac, Zfrac);
if (clickX = gDraw.clickPrevX) and (clickY = gDraw.clickPrevY) then exit;
DrawLine (gDraw.clickPrevX, gDraw.clickPrevY, clickX, clickY);
gDraw.clickPrevX := clickX;
gDraw.clickPrevY := clickY;
gDraw.doRedraw := true;
result := true;
end;
procedure preserveColors;
var
i: int64;
begin
for i := 0 to ((gDraw.dim2d[1]*gDraw.dim2d[2])-1) do
if (gDraw.undo2d[i] <> 0) then
gDraw.view2d[i] := gDraw.undo2d[i];
end;
procedure voiMouseUp ( autoClose, overwriteColors: boolean);
begin
if (autoClose) then begin
DrawLine (gDraw.clickPrevX, gDraw.clickPrevY, gDraw.clickStartX, gDraw.clickStartY);
fillRegion;
end;
if (not overwriteColors) and (gDraw.penColor <> 0) then
preserveColors;
gDraw.doRedraw := true;
UpdateView3d;
gDraw.isMouseDown := false;
end;
procedure voiUndo;
var
temp: bytep0;
nPix: int64;
begin
if (gDraw.view3dId = 0) or ( gDraw.view2d = nil) or ( gDraw.undo2d = nil) then exit;
if (gDraw.dim2d[0] = kOrient3D) then //3D volume
nPix := gDraw.dim3d[1]* gDraw.dim3d[2] * gDraw.dim3d[3]
else
nPix := gDraw.dim2d[1] * gDraw.dim2d[2]; //dim[3] = number of pixels
if (nPix < 1) then exit;
//swap active and undo images, so multiple calls to 'Undo' will undo/redo last drawing...
getmem(temp, nPix);
Move(gDraw.undo2d^,temp^,nPix);//source/dest
Move(gDraw.view2d^,gDraw.undo2d^,nPix);//source/dest
Move(temp^,gDraw.view2d^,nPix);//source/dest
freemem(temp);
gDraw.doRedraw := true;
UpdateView3d;
end;
procedure voiPasteSlice(Xfrac, Yfrac, Zfrac: single);
begin
if (gDraw.view3dId = 0) or ( gDraw.view2d = nil) or (gDraw.dim2d[0] = kOrient3D) then exit;
if (gDraw.dim2d[0] = kOrientSag) then begin
gDraw.dim2d[3] := frac2pix(Xfrac, gDraw.dim3d[1]); //sag slices select Left-Right slices
end else if (gDraw.dim2d[0] = kOrientCoro) then begin
gDraw.dim2d[3] := frac2pix(Yfrac, gDraw.dim3d[2]); //coro slices select Anterio-Posterior slices
end else begin //Axial
gDraw.dim2d[3] := frac2pix(Zfrac, gDraw.dim3d[3]); //axial influences head-foot
end;
gDraw.doRedraw := true;
UpdateView3d;
end;
procedure Redraw;
{$IFDEF COREGL}
const kA = GL_RED;
{$ELSE}
const kA = GL_ALPHA;
{$ENDIF}
begin
if (gDraw.view3dId = 0) or ( gDraw.view2d = nil) then exit;
glBindTexture(GL_TEXTURE_3D, gDraw.view3dId);
if (gDraw.dim2d[0] = kOrient3D) then begin //insert 3D volume
glTexSubImage3D(GL_TEXTURE_3D,0, 0, 0,0, gDraw.dim3d[1], gDraw.dim3d[2], gDraw.dim3d[3], kA, GL_UNSIGNED_BYTE,@gDraw.view2d[0]);
end else if (gDraw.dim2d[0] = kOrientSag) then begin//Sag - insert slice in X dimension
glTexSubImage3D(GL_TEXTURE_3D,0, gDraw.dim2d[3], 0,0, 1, gDraw.dim2d[1],gDraw.dim2d[2], kA, GL_UNSIGNED_BYTE,@gDraw.view2d[0]);
end else if (gDraw.dim2d[0] = kOrientCoro) then begin//Coro - insert slice in Y dimension
glTexSubImage3D(GL_TEXTURE_3D,0,0,gDraw.dim2d[3], 0,gDraw.dim2d[1],1, gDraw.dim2d[2], kA, GL_UNSIGNED_BYTE,@gDraw.view2d[0]);
end else begin//Axial - insert slice in Z dimension
glTexSubImage3D(GL_TEXTURE_3D,0,0, 0,gDraw.dim2d[3],gDraw.dim2d[1],gDraw.dim2d[2],1, kA, GL_UNSIGNED_BYTE,@gDraw.view2d[0]);
end;
end;
procedure voiChangeAlpha (a: byte);
var
i: int64;
begin
//gDraw.alpha := a;
gDraw.doAlpha := true;
gDraw.colorLut[0].rgbReserved := 0;
for i := 1 to 255 do
gDraw.colorLut[i].rgbReserved := a;
end;
function voiGetVolume: byteP0;
begin
result := gDraw.view3d;
end;
function makeRGB(r,g,b: byte): TGLRGBQuad;
begin
result.rgbRed := r;
result.rgbGreen := g;
result.rgbBlue := b;
end;
procedure voiDefaultLUT;
var
i:int64;
begin
with gDraw do begin
colorLut[0] := makeRGB(0,0,0);
colorLut[1] := makeRGB(255,0,0);//red
colorLut[2] := makeRGB(0,128,0);//green
colorLut[3] := makeRGB(0,0,255);//blue
colorLut[4] := makeRGB(255,128,0);//orange
colorLut[5] := makeRGB(128,0,255);//purple
colorLut[6] := makeRGB(0,200,200);//cyan
colorLut[7] := makeRGB(160,48,48);//brick
colorLut[8] := makeRGB(32,255,32);//lime
colorLut[9] := makeRGB(128,160,230);//lightblue
for i := 10 to 255 do
colorLut[i] := colorLut[((i-1) mod 9)+1];
end;
end;
procedure CreateColorTable;// Load image data
begin
if (gDraw.colorLutId <> 0) then glDeleteTextures(1,@gDraw.colorLutId);
glGenTextures(1, @gDraw.colorLutId);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_1D, gDraw.colorLutId);
{$IFDEF COREGL}
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
{$ELSE}
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
{$ENDIF}
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, @gDraw.colorLut[0]);
end;
procedure voiClose;
begin
if (gDraw.view3d <> nil) then freemem(gDraw.view3d);
gDraw.view3d := nil;
gDraw.isMouseDown:= false;
voiCloseSlice;
end;
procedure voiCloseGL;
begin
if (gDraw.view3dId <> 0) then glDeleteTextures(1,@gDraw.view3dId);
if (gDraw.colorLutId <> 0) then glDeleteTextures(1,@gDraw.colorLutId);
if (gDraw.glslprogramId <> 0) then glDeleteProgram(gDraw.glslprogramId);
gDraw.view3dId := 0;
gDraw.colorLutId := 0;
gDraw.glslprogramId := 0;
end;
type TSlice2D = array of single;
procedure SmoothSlice(var slice: TSlice2D; Xdim, Ydim: int64);
var
x,y, pos: int64;
sliceTemp : TSlice2D;
begin
if (Xdim < 3) or (Ydim < 3) then exit;
setlength(sliceTemp, Xdim * Ydim);
//emulate 2D Gaussian blur: since function is separable, compute as two 1D filter
//smooth in X dimension
pos := 0;
sliceTemp := Copy(slice, Low(slice), Length(slice)); //really clean, but unnecessary
for x := 0 to (Xdim -1) do begin
for y := 0 to (Ydim -1) do begin
pos := pos + 1;
if (x = 0) or (x = (Xdim-1)) then continue;
sliceTemp[pos] := (slice[pos-1] + slice[pos] + slice[pos] + slice[pos+1]) * 0.25;
end;
end;
//smooth in Y dimension
pos := 0;
for x := 0 to (Xdim -1) do begin
for y := 0 to (Ydim -1) do begin
pos := pos + 1;
if (y = 0) or (y = (Ydim-1)) then continue;
slice[pos] := (sliceTemp[pos-Ydim] + sliceTemp[pos] + sliceTemp[pos] + sliceTemp[pos+Ydim]) * 0.25;
end;
end;
sliceTemp := nil; //free
end;
function CopySlice2DSingle(Orient: int64; out out2D: TSlice2D): boolean;
var
u8_2D: bytep0;
nPix, i: int64;
begin
nPix := gDraw.dim2d[1]*gDraw.dim2d[2];
getmem(u8_2D, nPix);
setlength(out2D, nPix);
CopySlice2D(Orient,u8_2D);
result := false;
for i := 0 to (nPix -1) do
if u8_2D[i] <> u8_2D[0] then
result := true; //variability
for i := 0 to (nPix -1) do
out2D[i] := u8_2D^[i];
SmoothSlice(out2D, gDraw.dim2d[1], gDraw.dim2d[2]);
freemem(u8_2D);
end;
procedure voiInterpolateCore(orient, zLoIn, zHiIn: int64);
//interpolate voi between two 2D slices, slices indexed from 0
label
666;
var
xy, z, zLo, zHi, nSlices, nPix: int64;
FracLo, FracHi: single;
sliceLo, sliceHi: TSlice2D;
outSlice: byteP0;
begin
zLo := zLoIn;
zHi := zHiIn;
SortInt(zLo, zHi);
nSlices := setDim2D(Orient);
nPix := gDraw.dim2d[1]*gDraw.dim2d[2];
if (zLo >= zHi) or (zLo < 0) or (zHi >= nSlices) or (nPix < 9) then exit;
getmem(outSlice, nPix);
setlength(sliceLo, 0);
setLength(sliceHi, 0);
//get bottom slice
gDraw.dim2d[3] :=zLo;
if not CopySlice2DSingle(orient, sliceLo) then begin
showmessage('Error: no variability in 1st slice '+inttostr(zLo));
goto 666;
end;
//set top slice
gDraw.dim2d[3] :=zHi;
if not CopySlice2DSingle(orient, sliceHi) then begin
showmessage('Error: no variability in 2nd slice '+inttostr(zHi));
goto 666;
end;
//estimate intensity at each voxel in between
for z := (zLo+1) to (zHi -1) do begin
fracHi := (z-zLo) / (zHi - zLo);//weighting for of lower slice
fracLo := 1 - fracHi; //weighting for upper slice
for xy := 0 to (nPix-1) do begin
if ((sliceLo[xy]*fracLo) + (sliceHi[xy]*fracHi)) >= 0.375 then
outSlice^[xy] := 1
else
outSlice^[xy] := 0;
end; //each pixel in 2D slice
gDraw.dim2d[3] :=z;
PasteSlice2D(Orient, outSlice, gDraw.view2d);
end;
666:
freemem(outSlice);
setlength(sliceLo, 0);
setLength(sliceHi, 0);
end;
procedure voiInterpolateAllGaps(Orient: int64);
label
666;
var
zHi, zLo, i, z, nPix2D, nZ, nGaps: int64;
isEmpty: array of boolean;
slice2D: bytep0;
s :string;
begin
nZ := setDim2D(Orient);
nPix2D := gDraw.dim2d[1] * gDraw.dim2d[2];
getmem(slice2D,nPix2D);