forked from Neural-Systems-at-UIO/QuickNII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickNII.mxml
961 lines (863 loc) · 37.2 KB
/
QuickNII.mxml
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
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns:local="*"
xmlns:flexlib="flexlib.mdi.containers.*"
xmlns:slice="slice.*"
minWidth="1270" minHeight="500"
showStatusBar="false"
preinitialize="windowedapplication1_preinitializeHandler(event)"
applicationComplete="applicationCompleteHandler(event)"
close="windowedapplication1_closeHandler(event)">
<mx:Script>
<![CDATA[
import flash.utils.getTimer;
import flash.utils.setTimeout;
import flexlib.mdi.events.MDIWindowEvent;
import mx.binding.utils.BindingUtils;
import mx.controls.Alert;
import mx.core.IFlexDisplayObject;
import mx.events.CollectionEvent;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.events.ResizeEvent;
import mx.formatters.DateFormatter;
import mx.managers.PopUpManager;
import mx.utils.URLUtil;
[Bindable]public static var navilock:Boolean=false; //!!
public static var diz:QuickNII;
private const configsock:Socket=new Socket;
private const np:NativeProcess=new NativeProcess;
protected function applicationCompleteHandler(event:FlexEvent):void {
diz=this;
var resetItem:ContextMenuItem=new ContextMenuItem("Reset layout");
resetItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,rearrange);
mdiRoot.contextMenu.items.push(resetItem);
if(!Capabilities.supports64BitProcesses) {
log_console.text="A 64-bit Windows version is required.\n";
}
np.addEventListener(Event.STANDARD_OUTPUT_CLOSE,stdout_close);
np.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA,stdout_data);
np.addEventListener(Event.STANDARD_ERROR_CLOSE,stderr_close);
np.addEventListener(ProgressEvent.STANDARD_ERROR_DATA,stderr_data);
if(pack=="debug")
pack="C:\\QuickNII\\QuickNII-ABAMouse-v3-2017\\ABA_Mouse_CCFv3_2017_25um.cutlas";
//pack="C:\\QuickNII\\ABA_Mouse_CCFv3_dev2012_25um.cutlas";
//pack="C:\\QuickNII\\ABA_DevMouse_P28.cutlas";
const npsi:NativeProcessStartupInfo=new NativeProcessStartupInfo;
npsi.executable=File.applicationDirectory.resolvePath("Java"+File.separator+"bin"+File.separator+(Capabilities.os.indexOf("Windows")>=0?"java.exe":"java"));
npsi.arguments=new <String>["-Xmx3G","-cp",/*"Serv"*/File.applicationDirectory.resolvePath("Serv").nativePath, "Serv", File.applicationDirectory.resolvePath(pack).nativePath];
np.start(npsi);
nativeWindow.maximize();
configsock.addEventListener(ProgressEvent.SOCKET_DATA,configdata);
header.setStyle("dropShadowEnabled",false);
styleDecomp();
}
protected function stdout_close(e:Event):void {
log_panel.visible=log_panel.enabled=true;
log_panel.x=(width-log_panel.width)/2;
log_panel.y=(height-log_panel.height)/2;
log_console.text+="STDOUT closed\n";
}
private var console:String="";
private var inidone:Boolean=false;
private var cport:int;private const cstring:String="Catcher: ";
private var xport:int;private const xstring:String="XPort: ";
private var yport:int;private const ystring:String="YPort: ";
private var zport:int;private const zstring:String="ZPort: ";
private var fport:int;private const fstring:String="FPort: ";
protected function stdout_data(e:ProgressEvent):void {
// log_panel.visible=log_panel.enabled=true;
// log_panel.x=(width-log_panel.width)/2;
// log_panel.y=(height-log_panel.height)/2;
// log_console.text+="STDOUT:"+np.standardOutput.readUTFBytes(np.standardOutput.bytesAvailable)+":STDOUT\n";
console+=np.standardOutput.readUTFBytes(np.standardOutput.bytesAvailable);
if(!inidone && console.indexOf("Server ready.")>=0) {
inidone=true;
var pos1:int=console.indexOf(cstring);
var pos2:int=console.indexOf("\n",pos1);
cport=parseInt(console.substring(pos1+cstring.length,pos2));
pos1=console.indexOf(xstring);
pos2=console.indexOf("\n",pos1);
xport=parseInt(console.substring(pos1+xstring.length,pos2));
pos1=console.indexOf(ystring);
pos2=console.indexOf("\n",pos1);
yport=parseInt(console.substring(pos1+ystring.length,pos2));
pos1=console.indexOf(zstring);
pos2=console.indexOf("\n",pos1);
zport=parseInt(console.substring(pos1+zstring.length,pos2));
pos1=console.indexOf(fstring);
pos2=console.indexOf("\n",pos1);
fport=parseInt(console.substring(pos1+fstring.length,pos2));
configsock.connect("127.0.0.1",cport);
}
}
protected function stderr_close(e:Event):void {
log_panel.visible=log_panel.enabled=true;
log_panel.x=(width-log_panel.width)/2;
log_panel.y=(height-log_panel.height)/2;
log_console.text+="STDERR closed\n";
}
protected function stderr_data(e:ProgressEvent):void {
log_panel.visible=log_panel.enabled=true;
log_panel.x=(width-log_panel.width)/2;
log_panel.y=(height-log_panel.height)/2;
log_console.text+="STDERR:"+np.standardError.readUTFBytes(np.standardError.bytesAvailable)+":STDERR\n";
}
private var hdrlen:int=0;
private const xsock:Socket=new Socket;
private const ysock:Socket=new Socket;
private const zsock:Socket=new Socket;
private const fsock:Socket=new Socket;
protected function configdata(e:Event):void {
if(hdrlen==0 && configsock.bytesAvailable>=4) {
hdrlen=configsock.readInt();
}
if(hdrlen>0 && configsock.bytesAvailable>=hdrlen) {
xsock.addEventListener(Event.CONNECT,connected);
xsock.addEventListener(ProgressEvent.SOCKET_DATA,xdata);
ysock.addEventListener(Event.CONNECT,connected);
ysock.addEventListener(ProgressEvent.SOCKET_DATA,ydata);
zsock.addEventListener(Event.CONNECT,connected);
zsock.addEventListener(ProgressEvent.SOCKET_DATA,zdata);
fsock.addEventListener(Event.CONNECT,connected);
fsock.addEventListener(ProgressEvent.SOCKET_DATA,fdata);
xsock.connect("127.0.0.1",xport);
ysock.connect("127.0.0.1",yport);
zsock.connect("127.0.0.1",zport);
fsock.connect("127.0.0.1",fport);
}
}
private var conncnt:int=0;
private function connected(e:Event):void {
conncnt++;
if(conncnt==4)xmlComplete(null);
}
protected var setName:String;
private var volumes:Vector.<Volume>=new Vector.<Volume>;
[Bindable] protected var currentVolume:Volume;
[Bindable] public var xdim:int;
[Bindable] public var ydim:int;
[Bindable] public var zdim:int;
public var pixxdim:Number=1;
public var pixydim:Number=1;
public var pixzdim:Number=1;
[Bindable] public var physxdim:Number;
[Bindable] public var physydim:Number;
[Bindable] public var physzdim:Number;
public var transforms:Vector.<NamedTransformation>=new Vector.<NamedTransformation>;
[Bindable] protected var ready:Boolean=false;
protected function xmlComplete(e:Event):void {
ready=true;
mdiRoot.visible=true;
setName=configsock.readUTF();
nativeWindow.title=setName+" - QuickNII";
const abtlen:int=configsock.readInt();
const about:ByteArray=new ByteArray;
configsock.readBytes(about,0,abtlen);
about.inflate();
AboutPop.atlasText=about.readUTF();
AboutPop.main=this;
xdim=physxdim=configsock.readShort();
ydim=physydim=configsock.readShort();
zdim=physzdim=configsock.readShort();
physxdim*=pixxdim;
physydim*=pixydim;
physzdim*=pixzdim;
const vols:int=configsock.readByte();
for(var i:int=0;i<vols;i++) {
var v:Volume=new Volume(configsock);
volumes.push(v);
volumeLabels.push(v.name);
}
const trfs:int=configsock.readByte();
for(i=0;i<trfs;i++) {
transforms.push(new NamedTransformation(configsock.readUTF(),
configsock.readDouble(),configsock.readDouble(),configsock.readDouble(),configsock.readDouble(),
configsock.readDouble(),configsock.readDouble(),configsock.readDouble(),configsock.readDouble(),
configsock.readDouble(),configsock.readDouble(),configsock.readDouble(),configsock.readDouble()));
}
valbox.initTransformations(transforms);
currentVolume=volumes[0];
ctrl_volumes.selectedIndex=0;
widthstep.maximum=heightstep.maximum=Math.max(xdim,ydim,zdim)*1.2;
valbox.max.minimum=valbox.min.minimum=valbox.min.value=currentVolume.minval;
valbox.min.maximum=valbox.max.maximum=valbox.max.value=currentVolume.maxval;
cut.init(xdim,ydim,zdim);
rearrange(null);
}
internal function dispatchCut(force:Boolean=false):void {
setXSlice(cut.x,force);
setYSlice(cut.y,force);
setZSlice(cut.z,force);
xslice.DrawOverlay();
yslice.DrawOverlay();
zslice.DrawOverlay();
// updateTitles(null); //??
widthstep.value=cut.width;
heightstep.value=cut.height;
valbox.setVox(cut.x,cut.y,cut.z);
fetchSlice();
updateDecomp();
}
public function styleDecomp():void {
ctrl_rotate1.setStyle("backgroundColor", 0xE6B4B4);
ctrl_rotate2.setStyle("backgroundColor", 0xB4E6B4);
ctrl_rotate3.setStyle("backgroundColor", 0xB4B4E6);
slicewindow.titleBarOverlay.alpha=0.2;
slicewindow.titleBarOverlay.setStyle("backgroundColor", 0x0000FF);
xwindow.titleBarOverlay.alpha=0;
xwindow.titleBarOverlay.setStyle("backgroundColor", 0x000000);
ywindow.titleBarOverlay.alpha=0;
ywindow.titleBarOverlay.setStyle("backgroundColor", 0x000000);
zwindow.titleBarOverlay.alpha=0;
zwindow.titleBarOverlay.setStyle("backgroundColor", 0x000000);
if(valbox.decompcfg.selectedLabel=="Coronal") {
ind_mode.text="COR";
xwindow.titleBarOverlay.alpha=0.2;
xwindow.titleBarOverlay.setStyle("backgroundColor", 0xFF0000);
zwindow.titleBarOverlay.alpha=0.2;
zwindow.titleBarOverlay.setStyle("backgroundColor", 0x00FF00);
} else if(valbox.decompcfg.selectedLabel=="Sagittal") {
ind_mode.text="SAG";
ywindow.titleBarOverlay.alpha=0.2;
ywindow.titleBarOverlay.setStyle("backgroundColor", 0xFF0000);
zwindow.titleBarOverlay.alpha=0.2;
zwindow.titleBarOverlay.setStyle("backgroundColor", 0x00FF00);
} else if(valbox.decompcfg.selectedLabel=="Horizontal") {
ind_mode.text="HOR";
ywindow.titleBarOverlay.alpha=0.2;
ywindow.titleBarOverlay.setStyle("backgroundColor", 0xFF0000);
xwindow.titleBarOverlay.alpha=0.2;
xwindow.titleBarOverlay.setStyle("backgroundColor", 0x00FF00);
}
}
public function updateDecomp():void {
if(rot1focus||rot2focus||rot3focus)return;
const u:Vector3D=cut.u.clone();u.normalize();
const v:Vector3D=cut.v.clone();v.normalize();
const n:Vector3D=v.crossProduct(u);n.normalize();
const zerotrf:Matrix3D=new Matrix3D;
const data:Vector.<Number>=zerotrf.rawData;
if(valbox.decompcfg.selectedLabel=="Coronal") {
const c1:Number=Math.asin(Math.max(-1,Math.min(n.z,1)))*180/Math.PI;
const c2:Number=-Math.atan2(n.x,n.y)*180/Math.PI;
ctrl_rotate1.text=(c1).toFixed(2);
ctrl_rotate2.text=(c2).toFixed(2);
data[0]=1;data[1]=0;data[2]=0;
data[4]=0;data[5]=0;data[6]=-1;
SliceDescriptor.calcN(data);
zerotrf.rawData=data;
zerotrf.appendRotation(c1,Vector3D.X_AXIS);
zerotrf.appendRotation(c2,Vector3D.Z_AXIS);
} else if(valbox.decompcfg.selectedLabel=="Sagittal") {
const s1:Number=-Math.asin(Math.max(-1,Math.min(n.z,1)))*180/Math.PI;
const s2:Number=-Math.atan2(n.y,-n.x)*180/Math.PI;
ctrl_rotate1.text=(s1).toFixed(2);
ctrl_rotate2.text=(s2).toFixed(2);
data[0]=0;data[1]=1;data[2]=0;
data[4]=0;data[5]=0;data[6]=-1;
SliceDescriptor.calcN(data);
zerotrf.rawData=data;
zerotrf.appendRotation(-s1,Vector3D.Y_AXIS);
zerotrf.appendRotation(s2,Vector3D.Z_AXIS);
} else if(valbox.decompcfg.selectedLabel=="Horizontal") {
const h1:Number=-Math.asin(Math.max(-1,Math.min(1,n.x)))*180/Math.PI;
const h2:Number=-Math.atan2(n.y,n.z)*180/Math.PI;
ctrl_rotate1.text=(h1).toFixed(2);
ctrl_rotate2.text=(h2).toFixed(2);
data[0]=0;data[1]=1;data[2]=0;
data[4]=-1;data[5]=0;data[6]=0;
SliceDescriptor.calcN(data);
zerotrf.rawData=data;
zerotrf.appendRotation(h1,Vector3D.Y_AXIS);
zerotrf.appendRotation(h2,Vector3D.X_AXIS);
}
const zerou:Vector3D=zerotrf.transformVector(new Vector3D(1));
var cdt:Number=zerou.dotProduct(u);
if(cdt<-1)cdt=-1;
if(cdt>1)cdt=1;
ctrl_rotate3.text=(zerou.crossProduct(u).dotProduct(n)<0?"-":"")+new Number(Math.acos(cdt)*180/Math.PI).toFixed(2); //!!
}
public function fetchSlice():void {
/*nativeWindow.title=setName+" "+*/requestString();
}
private var fgotheader:Boolean=false;
private var fw:int;
private var fh:int;
private var ftyp:int;
private const fba:ByteArray=new ByteArray;
private function fdata(e:Event):void {
if(QS_plan.length>0) {
QS_data();
return;
}
if(!fgotheader && fsock.bytesAvailable>=9) {
ftyp=fsock.readByte();
fw=fsock.readInt();
fh=fsock.readInt();
fgotheader=true;
}
if(fgotheader &&
(
(ftyp!=3 && ftyp!=4 && fsock.bytesAvailable>=fw*fh*4)
|| (ftyp==3 && fsock.bytesAvailable>=fw*fh*5)
|| (ftyp==4 && fsock.bytesAvailable>=fw*fh*6)
)) {
if(ftyp!=3 && ftyp!=4)fsock.readBytes(fba,0,fw*fh*4);
if(ftyp==3)fsock.readBytes(fba,0,fw*fh*5);
if(ftyp==4)fsock.readBytes(fba,0,fw*fh*6);
fgotheader=false;
const Rf:Boolean=!valbox.ctrl_R.selected;
const Gf:Boolean=!valbox.ctrl_G.selected;
const Bf:Boolean=!valbox.ctrl_B.selected;
fba.position=0;
if(Rf || Gf || Bf)
for(var i:int=0;i<fw*fh;i++) {
fba.position++;
if(Rf)fba.writeByte(0);else fba.position++;
if(Gf)fba.writeByte(0);else fba.position++;
if(Bf)fba.writeByte(0);else fba.position++;
}
fba.position=0;
// var bd:BitmapData=new BitmapData(fw,fh,false);
var bd:BitmapData=new BitmapData(fw,fh,((ftyp==3)||(ftyp==4))&&valbox.outline.selected);
// var bd:BitmapData=new BitmapData(fw,fh,true);
bd.setPixels(bd.rect,fba);
SliceMix.NIISlice.source=new Bitmap(bd);
SliceMix.canvas1_resizeHandler(null);
}
}
protected function requestString():void {
fsock.writeByte(valbox.outline.selected?ctrl_volumes.selectedIndex+128:ctrl_volumes.selectedIndex);
fsock.writeFloat((valbox.min.value-currentVolume.minval)*65535/(currentVolume.maxval-currentVolume.minval));
fsock.writeFloat((valbox.max.value-currentVolume.minval)*65535/(currentVolume.maxval-currentVolume.minval));
fsock.writeDouble(cut.o.x);
fsock.writeDouble(cut.o.y);
fsock.writeDouble(cut.o.z);
fsock.writeDouble(cut.u.x);
fsock.writeDouble(cut.u.y);
fsock.writeDouble(cut.u.z);
fsock.writeDouble(cut.v.x);
fsock.writeDouble(cut.v.y);
fsock.writeDouble(cut.v.z);
fsock.flush();
// return "ox="+o.x+"&oy="+o.y+"&oz="+o.z
// +"&ux="+u.x+"&uy="+u.y+"&uz="+u.z
// +"&vx="+v.x+"&vy="+v.y+"&vz="+v.z;
}
[Bindable] internal var cut:SliceDescriptor=new SliceDescriptor(dispatchCut);
private const tba:ByteArray=new ByteArray;
private var currx:int=0;
protected function setXSlice(xcut:uint,force:Boolean):void {
const xindex:uint=xcut;
if(!force && (currx==xindex))return;
currx=xindex;
xsock.writeByte(ctrl_volumes.selectedIndex);
xsock.writeFloat((valbox.min.value-currentVolume.minval)*65535/(currentVolume.maxval-currentVolume.minval));
xsock.writeFloat((valbox.max.value-currentVolume.minval)*65535/(currentVolume.maxval-currentVolume.minval));
xsock.writeInt(xindex-1);
xsock.flush();
}
private function xdata(e:Event):void {
if(xsock.bytesAvailable>=ydim*zdim*4) {
xsock.readBytes(tba,0,ydim*zdim*4);
tba.position=0;
var bd:BitmapData=new BitmapData(ydim,zdim,false);
bd.setPixels(bd.rect,tba);
xslice.image.source=new Bitmap(bd);
}
}
private var curry:int=0;
protected function setYSlice(ycut:uint,force:Boolean):void {
const yindex:uint=ycut;
if(!force && (curry==yindex))return;
curry=yindex;
ysock.writeByte(ctrl_volumes.selectedIndex);
ysock.writeFloat((valbox.min.value-currentVolume.minval)*65535/(currentVolume.maxval-currentVolume.minval));
ysock.writeFloat((valbox.max.value-currentVolume.minval)*65535/(currentVolume.maxval-currentVolume.minval));
ysock.writeInt(yindex-1);
ysock.flush();
}
private function ydata(e:Event):void {
if(ysock.bytesAvailable>=xdim*zdim*4) {
ysock.readBytes(tba,0,xdim*zdim*4);
tba.position=0;
var bd:BitmapData=new BitmapData(xdim,zdim,false);
bd.setPixels(bd.rect,tba);
yslice.image.source=new Bitmap(bd);
}
}
private var currz:int=0;
protected function setZSlice(zcut:uint,force:Boolean):void {
const zindex:uint=zcut;
if(!force && (currz==zindex))return;
currz=zindex;
zsock.writeByte(ctrl_volumes.selectedIndex);
zsock.writeFloat((valbox.min.value-currentVolume.minval)*65535/(currentVolume.maxval-currentVolume.minval));
zsock.writeFloat((valbox.max.value-currentVolume.minval)*65535/(currentVolume.maxval-currentVolume.minval));
zsock.writeInt(zindex-1);
zsock.flush();
}
private function zdata(e:Event):void {
if(zsock.bytesAvailable>=xdim*ydim*4) {
zsock.readBytes(tba,0,xdim*ydim*4);
tba.position=0;
var bd:BitmapData=new BitmapData(xdim,ydim,false);
bd.setPixels(bd.rect,tba);
zslice.image.source=new Bitmap(bd);
}
}
public function rearrange(fake:Event):void {
if(xwindow.minimized || xwindow.maximized)xwindow.restore();
if(ywindow.minimized || ywindow.maximized)ywindow.restore();
if(zwindow.minimized || zwindow.maximized)zwindow.restore();
if(slicewindow.minimized || slicewindow.maximized)slicewindow.restore();
// if(ctrlwindow.minimized || ctrlwindow.maximized)ctrlwindow.restore();
const hdr:Number=header.height;
const horiz:Number=3+3;//xwindow.width-xslice.width;
const vert:Number=26+3;//xwindow.height-xslice.height;
const step:Number=(height-hdr-vert-vert-vert)/(physzdim+physzdim+physydim);
ywindow.width=zwindow.width=Math.max(step*physxdim+horiz,ywindow.minWidth)
ywindow.height=xwindow.height=Math.max(step*physzdim+vert,ywindow.minHeight);
xwindow.width=Math.max(step*physydim+horiz,ywindow.minWidth);
zwindow.height=Math.max(step*physydim+vert,ywindow.minHeight);
xwindow.x=width-xwindow.width;
xwindow.y=0;
ywindow.x=zwindow.x=width-ywindow.width;
ywindow.y=xwindow.height;
zwindow.y=ywindow.y+ywindow.height;
slicewindow.x=slicewindow.y=0;
slicewindow.width=Math.max(Math.min(xwindow.x,ywindow.x),slicewindow.minWidth);
if(!ctrlwindow.minimized) {
ctrlwindow.width=Math.max(Math.min(xwindow.x,ywindow.x),slicewindow.minHeight);
ctrlwindow.y=height-hdr-ctrlwindow.height;
ctrlwindow.minimize();
}
slicewindow.height=Math.max(height-hdr-slicewindow.minimizeHeight-vert/2,slicewindow.minHeight);
}
protected function SliceMix_mouseMoveHandler(event:MouseEvent):void {
const u:Number=SliceMix.NIISlice.mouseX/SliceMix.NIISlice.contentWidth;
const v:Number=SliceMix.NIISlice.mouseY/SliceMix.NIISlice.contentHeight;
const x:Number=cut.o.x+u*cut.u.x+v*cut.v.x;
const y:Number=cut.o.y+u*cut.u.y+v*cut.v.y;
const z:Number=cut.o.z+u*cut.u.z+v*cut.v.z;
slicewindow.title="Slice "+x.toFixed(1)+","+y.toFixed(1)+","+z.toFixed(1);
labelCanvas.visible=false;
if(!fgotheader && (ftyp==3 || ftyp==4) && currentVolume.colors!=null && !valbox.outline.selected) {
const bx:int=u*fw;
const by:int=v*fh;
if(bx>=0 && bx<fw && by>=0 && by<fh) {
fba.position=fw*fh*4+(bx+by*fw)*(ftyp-2);
var idx:uint=ftyp==3?fba.readUnsignedByte():fba.readUnsignedShort();
if(idx!=0) {
labelCanvas.visible=true;
if(idx!=currIdx) {
currIdx=idx;
labelCanvas.y=(height-header.height)*0.9;
labelCanvas.height=(height-header.height)*0.08;
labelText.text=currentVolume.names[idx];
labelText.y=(labelCanvas.height-labelText.height)/2;
const g:Graphics=labelCanvas.graphics;
g.clear();
g.beginFill(currentVolume.colors[idx]);
g.drawRoundRect(0,0,width,labelCanvas.height,20);
g.endFill();
}
}
}
}
}
private var currIdx:uint;
public function Store():void {
if(MgmtPanel.currentidx!=-1) {
const w:SliceData=MgmtPanel.rows.getItemAt(MgmtPanel.currentidx) as SliceData;
w.setPacked(new <Number>[cut.o.x,cut.o.y,cut.o.z,cut.u.x,cut.u.y,cut.u.z,cut.v.x,cut.v.y,cut.v.z]);
MgmtPanel.interpolate();
seriescontrols();
MgmtPanel.save(new File(MgmtPanel.baseurl+File.separator+"~~autosave~~.xml"));
}
}
public function Restore():void {
cut.load(MgmtPanel.requestString);
}
protected function volume_store():void {
currentVolume.parammin=valbox.min.value;
currentVolume.parammax=valbox.max.value;
currentVolume.gray=valbox.gray.selected;
}
protected function volume_set(v:Volume):void {
currentVolume=v;
valbox.max.minimum=valbox.min.minimum=currentVolume.minval;
valbox.min.maximum=valbox.max.maximum=currentVolume.maxval;
valbox.min.value=isNaN(currentVolume.parammin)?currentVolume.minval:currentVolume.parammin;
valbox.max.value=isNaN(currentVolume.parammax)?currentVolume.maxval:currentVolume.parammax;
valbox.gray.selected=currentVolume.gray;
dispatchCut(true);
}
protected function ctrl_volumes_changeHandler(event:ListEvent):void {
if(event!=null)
volume_store();
volume_set(volumes[ctrl_volumes.selectedIndex]);
}
protected function ctrl_rotations_changeHandler(event:Event):void {
const t1:Number=Number(ctrl_rotate1.text);
const t2:Number=Number(ctrl_rotate2.text);
const t3:Number=Number(ctrl_rotate3.text);
cut.BuildRotation(valbox.decompcfg.selectedLabel,t1,t2,t3);
cut.Dispatch();
}
private var pop:IFlexDisplayObject=null;
protected function aboutButton_clickHandler(event:MouseEvent):void {
if(pop)return;
hideCanvas.visible=hideCanvas.enabled=true;
pop=PopUpManager.createPopUp(this,AboutPop);
PopUpManager.centerPopUp(pop);
}
public function removePop():void {
hideCanvas.visible=hideCanvas.enabled=false;
PopUpManager.removePopUp(pop);
pop=null;
}
protected function hideCanvas_clickHandler(event:MouseEvent):void {
removePop();
}
private var imageFile:File=new File;
[Bindable] public var imageFileSelected:Boolean=false;
public function loadImage(event:MouseEvent):void {
imageFile.addEventListener(Event.SELECT,imageSelected);
imageFile.browseForOpen("Histological image",[new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png")]);
}
public function imageSelected(e:Event):void {
nativeWindow.title=imageFile.nativePath;
SliceMix.setSlice(imageFile.url,slicewindow);
imageFileSelected=true;
MgmtPanel.currentidx=-1;seriescontrols();
MgmtPanel.titl=imageFile.name;
}
public function setImage(filename:String,anchoring:Vector.<Number>):void {
nativeWindow.title=filename;
SliceMix.setSlice(filename,slicewindow);
MgmtPanel.requestString=anchoring;
Restore();
MgmtPanel.titl=filename;
seriescontrols();
}
private var mgmtwindow:MgmtPanel=null;
protected function pushButton_clickHandler(event:MouseEvent):void {
if((mgmtwindow==null)||(mgmtwindow.closed)) {
mgmtwindow=new MgmtPanel;
MgmtPanel.main=this;
mgmtwindow.open();
if(Screen.screens.length>1)
mgmtwindow.stage.nativeWindow.x=(Screen.screens[1] as Screen).visibleBounds.x;
mgmtwindow.maximize();
}
else mgmtwindow.activate();
}
/*private*/public var pack:String;
protected function invokeHandler(event:InvokeEvent):void {
pack=event.arguments[0];
if(pack==null) {
var fs:FileStream=new FileStream();
fs.open(File.applicationDirectory.resolvePath("pack.txt"),FileMode.READ);
pack=fs.readMultiByte(fs.bytesAvailable,"utf-8");
fs.close();
}
}
protected function windowedapplication1_preinitializeHandler(event:FlexEvent):void {
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE,invokeHandler);
}
public function refresh():void {
setXSlice(cut.x,true);
setYSlice(cut.y,true);
setZSlice(cut.z,true);
fetchSlice();
}
public function seriescontrols():void {
if(MgmtPanel.currentidx==-1)
btn_next.visible=btn_nextanch.visible=btn_prev.visible=btn_prevanch.visible=chk_anch.visible=false;
else {
btn_next.visible=btn_nextanch.visible=btn_prev.visible=btn_prevanch.visible=chk_anch.visible=true;
btn_prev.enabled=MgmtPanel.currentidx>0;
btn_next.enabled=MgmtPanel.currentidx<MgmtPanel.rows.length-1;
btn_prevanch.enabled=false;
for(var i:int=MgmtPanel.currentidx-1;i>=0;i--)
if((MgmtPanel.rows.getItemAt(i) as SliceData).hasanchoring)
btn_prevanch.enabled=true;
btn_nextanch.enabled=false;
for(i=MgmtPanel.currentidx+1;i<MgmtPanel.rows.length;i++)
if((MgmtPanel.rows.getItemAt(i) as SliceData).hasanchoring)
btn_nextanch.enabled=true;
if((MgmtPanel.rows.getItemAt(MgmtPanel.currentidx) as SliceData).hasanchoring) {
chk_anch.text="!";
chk_anch.getTextField().backgroundColor=0x80ff80;
} else {
chk_anch.text="?";
chk_anch.getTextField().backgroundColor=0xff0000;
}
chk_anch.getTextField().background=true;
}
}
public function serieshack():void { //!!
const w:SliceData=MgmtPanel.rows.getItemAt(MgmtPanel.currentidx) as SliceData;
setImage(MgmtPanel.baseurl+File.separator+w.filename,w.packed);
}
protected function btn_prevanch_clickHandler(event:MouseEvent):void {
while(!(MgmtPanel.rows.getItemAt(--MgmtPanel.currentidx) as SliceData).hasanchoring){}
serieshack();
}
protected function btn_prev_clickHandler(event:MouseEvent):void {
MgmtPanel.currentidx--;
serieshack();
}
protected function btn_next_clickHandler(event:MouseEvent):void {
MgmtPanel.currentidx++;
serieshack();
}
protected function btn_nextanch_clickHandler(event:MouseEvent):void {
while(!(MgmtPanel.rows.getItemAt(++MgmtPanel.currentidx) as SliceData).hasanchoring){}
serieshack();
}
public var rot1focus:Boolean=false;
public var rot2focus:Boolean=false;
public var rot3focus:Boolean=false;
protected function ctrl_rotate1_focusInHandler(event:FocusEvent):void {
trace("1+");
rot1focus=true;
}
protected function ctrl_rotate1_focusOutHandler(event:FocusEvent):void {
trace("1-");
rot1focus=false;
}
protected function ctrl_rotate2_focusInHandler(event:FocusEvent):void {
trace("2+");
rot2focus=true;
}
protected function ctrl_rotate2_focusOutHandler(event:FocusEvent):void {
trace("2-");
rot2focus=false;
}
protected function ctrl_rotate3_focusInHandler(event:FocusEvent):void {
trace("3+");
rot3focus=true;
}
protected function ctrl_rotate3_focusOutHandler(event:FocusEvent):void {
trace("3-");
rot3focus=false;
}
protected function windowedapplication1_closeHandler(event:Event):void {
if(mgmtwindow!=null)mgmtwindow.close();
}
private var QS_dir:File;
private var QS_plan:Vector.<QS_Item>=new Vector.<QS_Item>;
private var QS_total:int;
public function QSlices():void {
if(QS_plan.length>0)return;
QS_dir=new File(MgmtPanel.baseurl);
QS_dir.addEventListener(Event.SELECT,QS_dir_selected);
QS_dir.browseForDirectory("Select folder for generated series");
}
public function QS_dir_selected(e:Event):void {
var indexHack:Boolean=true;
for(var i:int=0;i<volumes.length;i++) {
var v:Volume=volumes[i];
if(indexHack || (v.type!=3) && (v.type!=4)) {
if((v.type==3)||(v.type==4)) {
indexHack=false;
var fs:FileStream=new FileStream;
try {
fs.open(QS_dir.resolvePath(v.name+".json"),FileMode.WRITE);
fs.writeUTFBytes('[{"index":0,"red":0,"green":0,"blue":0,"name":"empty"}');
for(var j:int=1;j<v.colors.length;j++) {
fs.writeUTFBytes(',{"index":'+j+',"red":'+((v.colors[j]>>16)&255)+',"green":'+((v.colors[j]>>8)&255)+',"blue":'+(v.colors[j]&255)+',"name":"'+v.names[j]+'"}');
}
fs.writeUTFBytes("]");
fs.close()
} catch(ex:Error){}
}
var suffix:String=v.name.replace(" ","_");
for(j=0;j<MgmtPanel.rows.length;j++) {
QS_plan.push(new QS_Item(i,v,suffix,MgmtPanel.rows.getItemAt(j) as SliceData));
}
}
}
QS_total=QS_plan.length;
MgmtPanel.pb_hack.visible=true;
QS_next();
}
private function QS_next():void {
QS_header=false;
if(QS_plan.length>0) {
MgmtPanel.pb_hack.setProgress(QS_total-QS_plan.length+1,QS_total);
QS_plan[QS_plan.length-1].request(fsock);
} else {
MgmtPanel.pb_hack.visible=false;
}
}
private var QS_ba:ByteArray=new ByteArray;
private var QS_header:Boolean;
private var QS_width:int;
private var QS_height:int;
private var QS_typ:int;
private function QS_data():void {
if(!QS_header && fsock.bytesAvailable>=9) {
QS_typ=fsock.readByte();
QS_width=fsock.readInt();
QS_height=fsock.readInt();
QS_header=true;
}
if(QS_header &&
(
(QS_typ!=3 && QS_typ!=4 && fsock.bytesAvailable>=QS_width*QS_height*4)
|| (QS_typ==3 && fsock.bytesAvailable>=QS_width*QS_height*5)
|| (QS_typ==4 && fsock.bytesAvailable>=QS_width*QS_height*6)
)) {
var item:QS_Item=QS_plan.pop();
var fs:FileStream=new FileStream;
fs.open(QS_dir.resolvePath(item.lm.filename.substr(0,item.lm.filename.lastIndexOf("."))+"-"+item.suffix+".png"),FileMode.WRITE);
fs.endian=Endian.BIG_ENDIAN;
fs.writeUnsignedInt(0x89504e47);
fs.writeUnsignedInt(0x0D0A1A0A);
QS_ba.length=0;
QS_ba.writeInt(QS_width);
QS_ba.writeInt(QS_height);
QS_ba.writeByte(8);
QS_ba.writeByte(2);
QS_ba.writeByte(0);
QS_ba.writeByte(0);
QS_ba.writeByte(0);
crcWrite(fs,"IHDR",QS_ba);
QS_ba.length=0;
for(var y:int=0;y<QS_height;y++) {
QS_ba.writeByte(0);
for(var x:int=0;x<QS_width;x++) {
fsock.readByte();
fsock.readBytes(QS_ba,QS_ba.position,3);
QS_ba.position+=3;
}
}
QS_ba.compress();
crcWrite(fs,"IDAT",QS_ba);
QS_ba.length=0;
crcWrite(fs,"IEND",QS_ba);
fs.close();
if(QS_typ==3) {
fsock.readBytes(QS_ba,0,QS_width*QS_height);
fs.open(QS_dir.resolvePath(item.lm.filename.substr(0,item.lm.filename.lastIndexOf("."))+"-"+item.suffix+".flat"),FileMode.WRITE);
fs.endian=Endian.BIG_ENDIAN;
fs.writeByte(1);
fs.writeInt(QS_width);
fs.writeInt(QS_height);
fs.writeBytes(QS_ba);
fs.close();
}
if(QS_typ==4) {
fsock.readBytes(QS_ba,0,QS_width*QS_height*2);
fs.open(QS_dir.resolvePath(item.lm.filename.substr(0,item.lm.filename.lastIndexOf("."))+"-"+item.suffix+".flat"),FileMode.WRITE);
fs.endian=Endian.BIG_ENDIAN;
fs.writeByte(2);
fs.writeInt(QS_width);
fs.writeInt(QS_height);
fs.writeBytes(QS_ba);
fs.close();
}
QS_ba.length=0;
QS_next();
}
}
private static var crcHelper:Array=null;
private static function crcWrite(fs:FileStream,type:String,ba:ByteArray):void {
if(crcHelper==null) {
crcHelper=new Array;
for(var i:uint=0;i<256;i++) {
var c:uint=i;
for(var j:uint=0;j<8;j++) {
if(c&1)c=uint(uint(0xedb88320)^uint(c>>>1));
else c=uint(c>>>1);
}
crcHelper[i]=c;
}
}
fs.writeUnsignedInt(ba.length);
fs.writeUTFBytes(type);
ba.position=0;
fs.writeBytes(ba);
ba.position=0;
c=0xFFFFFFFF;
for(i=0;i<4;i++) {
c=uint(crcHelper[(c^type.charCodeAt(i))&uint(0xFF)]^uint(c>>>8));
}
for(i=0;i<ba.length;i++) {
c=uint(crcHelper[(c^ba.readUnsignedByte())&uint(0xFF)]^uint(c>>>8));
}
c=uint(c^uint(0xFFFFFFFF));
fs.writeUnsignedInt(c);
}
]]>
</mx:Script>
<mx:ApplicationControlBar id="header" dock="true" width="100%" fillAlphas="[1.0, 1.0]" fillColors="[#909090, #909090]">
<mx:ComboBox id="ctrl_volumes" width="120" change="ctrl_volumes_changeHandler(event)"><mx:Array id="volumeLabels"/></mx:ComboBox>
<mx:Button label="?" width="30" click="aboutButton_clickHandler(event)" toolTip="About" enabled="{ready}"/>
<mx:VRule height="20"/>
<mx:Button label="Store" click="Store()"/>
<mx:Button label="Restore" click="Restore()"/>
<mx:VRule height="20"/>
<mx:Button label="Undo" click="cut.Undo()"/>
<mx:Button label="Redo" click="cut.Redo()"/>
<mx:Label text="Pan"/>
<mx:NumericStepper id="shiftstep" textAlign="right" width="52" minimum="0.05" maximum="20" value="1" stepSize="0.05" change="cut.shiftstep=shiftstep.value"/>
<mx:Label text="Rot"/>
<mx:NumericStepper id="rotstep" textAlign="right" width="41" minimum="0.05" maximum="90" value="1" stepSize="0.05" change="cut.rotstep=rotstep.value"/>
<mx:NumericStepper id="widthstep" textAlign="right" width="68" change="cut.width=widthstep.value;cut.Dispatch();"/>
<mx:Label text="x" width="10"/>
<mx:NumericStepper id="heightstep" textAlign="right" width="68" change="cut.height=heightstep.value;cut.Dispatch();"/>
<mx:VRule height="20"/>
<mx:Label id="ind_mode" fontFamily="Arial" fontSize="16" fontWeight="bold" textAlign="center"/>
<mx:TextInput id="ctrl_rotate1" textAlign="right" width="55" restrict="-.0-9" change="ctrl_rotations_changeHandler(event)" focusIn="ctrl_rotate1_focusInHandler(event)" focusOut="ctrl_rotate1_focusOutHandler(event)"/>
<mx:TextInput id="ctrl_rotate2" textAlign="right" width="55" restrict="-.0-9" change="ctrl_rotations_changeHandler(event)" focusIn="ctrl_rotate2_focusInHandler(event)" focusOut="ctrl_rotate2_focusOutHandler(event)"/>
<mx:TextInput id="ctrl_rotate3" textAlign="right" width="55" restrict="-.0-9" change="ctrl_rotations_changeHandler(event)" focusIn="ctrl_rotate3_focusInHandler(event)" focusOut="ctrl_rotate3_focusOutHandler(event)"/>
<mx:Button label="Manage data" click="pushButton_clickHandler(event)" enabled="{ready}"/>
<mx:Button id="btn_prevanch" label="|<|" visible="false" click="btn_prevanch_clickHandler(event)"/>
<mx:Button id="btn_prev" label="<" visible="false" click="btn_prev_clickHandler(event)"/>
<local:BGLabel id="chk_anch" visible="false" fontFamily="Arial" fontSize="16" fontWeight="bold" textAlign="center"/>
<mx:Button id="btn_next" label=">" visible="false" click="btn_next_clickHandler(event)"/>
<mx:Button id="btn_nextanch" label="|>|" visible="false" click="btn_nextanch_clickHandler(event)"/>
</mx:ApplicationControlBar>
<flexlib:MDICanvas id="mdiRoot" horizontalScrollPolicy="off" verticalScrollPolicy="off" visible="false"
width="100%" height="100%"
backgroundColor="#A0A0A0"
snapDistance="4" tilePadding="8">
<flexlib:MDIWindow title="Sagittal" showCloseButton="false" id="xwindow" dropShadowEnabled="false" minWidth="142" minHeight="102">
<local:Slice id="xslice" ready="{ready}" sliceDescriptor="{cut}" plane="yzx"
dataWidth="{ydim}" dataHeight="{zdim}"
physWidth="{physydim}" physHeight="{physzdim}"
leftText="labels/AnteriorR.png" rightText="labels/PosteriorR.png" topText="labels/Superior.png" bottomText="labels/Inferior.png" volume="{currentVolume}"/>
</flexlib:MDIWindow>
<flexlib:MDIWindow title="Coronal" showCloseButton="false" id="ywindow" dropShadowEnabled="false" minWidth="142" minHeight="102">
<local:Slice id="yslice" ready="{ready}" sliceDescriptor="{cut}" plane="xzy"
dataWidth="{xdim}" dataHeight="{zdim}"
physWidth="{physxdim}" physHeight="{physzdim}"
leftText="labels/Right.png" rightText="labels/Left.png" topText="labels/Superior.png" bottomText="labels/Inferior.png" volume="{currentVolume}"/>
</flexlib:MDIWindow>
<flexlib:MDIWindow title="Horizontal" showCloseButton="false" id="zwindow" dropShadowEnabled="false" minWidth="142" minHeight="102">
<local:Slice id="zslice" ready="{ready}" sliceDescriptor="{cut}" plane="xyz"
dataWidth="{xdim}" dataHeight="{ydim}"
physWidth="{physxdim}" physHeight="{physydim}"
leftText="labels/Right.png" rightText="labels/Left.png" topText="labels/Anterior.png" bottomText="labels/Posterior.png" volume="{currentVolume}"/>
</flexlib:MDIWindow>
<flexlib:MDIWindow title="Values and control" showCloseButton="false" id="ctrlwindow" dropShadowEnabled="false" height="350">
<local:ValueBox id="valbox" main="{this}" mouseOver="SliceMix.drawMidCross()" mouseOut="SliceMix.drawAnchor();"/>
</flexlib:MDIWindow>
<local:SliceWindow title="Slice" showCloseButton="false" id="slicewindow" inner="{SliceMix}" dropShadowEnabled="false" minWidth="410" minHeight="100">
<local:OneOrTwoImages id="SliceMix" mouseMove="SliceMix_mouseMoveHandler(event)" sliceDescriptor="{cut}" mouseOut="labelCanvas.visible=false;currIdx=0;" outer="{slicewindow}"/>
</local:SliceWindow>
</flexlib:MDICanvas>
<mx:Label id="indicator" text="Can not access volume" visible="false"/>
<mx:Canvas id="labelCanvas" width="100%" mouseEnabled="false" focusEnabled="false" mouseFocusEnabled="false" visible="false">
<mx:Label id="labelText" width="100%" textAlign="center" fontFamily="Verdana" fontSize="30" mouseEnabled="false" color="#0" fontWeight="bold"/>
</mx:Canvas>
<mx:Panel x="240" y="247" width="596" height="335" layout="absolute" title="ErrorLog" id="log_panel" visible="false" enabled="false">
<mx:TextArea x="0" y="0" width="576" height="295" id="log_console"/>
</mx:Panel>
<mx:Canvas id="hideCanvas" width="100%" height="100%" click="hideCanvas_clickHandler(event)" enabled="false" visible="false"/>
</mx:WindowedApplication>