-
Notifications
You must be signed in to change notification settings - Fork 5
/
OneOrTwoImages.mxml
361 lines (341 loc) · 12.4 KB
/
OneOrTwoImages.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
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:ui="flex.utils.ui.*" addedToStage="addedToStageHandler(event)" resize="canvas1_resizeHandler(event)" width="100%" height="100%" xmlns:s="library://ns.adobe.com/flex/spark">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.events.ResizeEvent;
import mx.events.SliderEvent;
public var sliceDescriptor:SliceDescriptor;
private var hasOverlay:Boolean;
public function setSlice(s:String,w:SliceWindow):void {
if(s!=null) {
var ldr:Loader=new Loader;
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,compl);
ldr.load(new URLRequest(s));
} else {
ImageOverlay.source=null;
bitmap=null;
}
const hadOverlay:Boolean=hasOverlay;
hasOverlay=s!=null;
w.hasOverlay=hasOverlay;
if(hasOverlay) {
if(!hadOverlay) {
AlphaSlider.value=50;
ImageOverlay.alpha=0.5;
AlphaSlider.visible=btn_hist.visible=btn_vol.visible=true;
AlphaSlider.enabled=btn_hist.enabled=btn_vol.enabled=true;
}
} else {
AlphaSlider.value=100;
ImageOverlay.alpha=1;
AlphaSlider.visible=btn_hist.visible=btn_vol.visible=false;
AlphaSlider.enabled=btn_hist.enabled=btn_vol.enabled=false;
canvas1_resizeHandler(null);
}
}
private var bitmap:BitmapData=null;
public function compl(e:Event):void {
bitmap=e.target.content.bitmapData;
doMagic();
}
public function doMagic():void {
if(bitmap==null)return;
var ctrl:int=QuickNII.diz.valbox.ctrl_S.value;
const R:Boolean=QuickNII.diz.valbox.ctrl_R0.selected;
const G:Boolean=QuickNII.diz.valbox.ctrl_G0.selected;
const B:Boolean=QuickNII.diz.valbox.ctrl_B0.selected;
var ba:ByteArray=bitmap.getPixels(bitmap.rect);
if(R && G && B && (ctrl==255))
ImageOverlay.source=new Bitmap(bitmap);
else {
if(ctrl==0) {
ba.position=0;
while(ba.bytesAvailable>0) {
ba.position++;
var tmp:uint=ba.readUnsignedByte();
if(R && (tmp>ctrl))ctrl=tmp;
tmp=ba.readUnsignedByte();
if(G && (tmp>ctrl))ctrl=tmp;
tmp=ba.readUnsignedByte();
if(B && (tmp>ctrl))ctrl=tmp;
}
QuickNII.diz.valbox.ctrl_S.value=ctrl;
}
ba.position=0;
while(ba.bytesAvailable>0) {
ba.position++;
if(R) {
tmp=ba.readUnsignedByte()*255/ctrl;
if(tmp>255)tmp=255;
ba.position--;
ba.writeByte(tmp);
}
else ba.writeByte(0);
if(G) {
tmp=ba.readUnsignedByte()*255/ctrl;
if(tmp>255)tmp=255;
ba.position--;
ba.writeByte(tmp);
}
else ba.writeByte(0);
if(B) {
tmp=ba.readUnsignedByte()*255/ctrl;
if(tmp>255)tmp=255;
ba.position--;
ba.writeByte(tmp);
}
else ba.writeByte(0);
}
ba.position=0;
var bd:BitmapData=new BitmapData(bitmap.width,bitmap.height);
bd.setPixels(bd.rect,ba);
ImageOverlay.source=new Bitmap(bd);
}
canvas1_resizeHandler(null);
}
private var slicx:Number;
private var slicy:Number;
private var slicw:Number;
private var slich:Number;
public function canvas1_resizeHandler(event:ResizeEvent):void {
AlphaSlider.y=btn_hist.height;
btn_vol.y=height-btn_vol.height;
AlphaSlider.height=btn_vol.y-AlphaSlider.y;
if(hasOverlay && ImageOverlay.source!=null) {
const indent:Number=btn_hist.width; // 31
const work:Number=width-indent;
if(ImageOverlay.source.bitmapData.width/ImageOverlay.source.bitmapData.height>width/height) {
ImageOverlay.x=slicx=indent;
ImageOverlay.width=slicw=work;
ImageOverlay.height=slich=ImageOverlay.source.bitmapData.height*work/ImageOverlay.source.bitmapData.width;
ImageOverlay.y=slicy=(height-ImageOverlay.height)/2;
} else {
ImageOverlay.y=slicy=0;
ImageOverlay.height=slich=height;
ImageOverlay.width=slicw=ImageOverlay.source.bitmapData.width*height/ImageOverlay.source.bitmapData.height;
ImageOverlay.x=slicx=indent+(work-ImageOverlay.width)/2;
}
} else {
if(sliceDescriptor.width/sliceDescriptor.height>width/height) {
slicx=0;
slicw=width;
slich=sliceDescriptor.height*width/sliceDescriptor.width;
slicy=(height-slich)/2;
} else {
slicy=0;
slich=height;
slicw=sliceDescriptor.width*height/sliceDescriptor.height;
slicx=(width-slicw)/2;
}
}
NIISlice.x=slicx;
NIISlice.y=slicy;
NIISlice.width=slicw;
NIISlice.height=slich;
}
protected function mdouble(event:MouseEvent):void {
if(!hasOverlay)return;
if(NIISlice.mouseX>=0 && NIISlice.mouseY>=0 && NIISlice.mouseX<NIISlice.width && NIISlice.mouseY<NIISlice.height) {
anchorX=mouseX;
anchorY=mouseY;
anchored=true;
const f:Number=(anchorX-ox)*sliceDescriptor.width/ow;
}
else if(anchored)anchored=false;
drawAnchor();
}
private var A_M_NONE:uint=0;
private var A_M_HORIZ:uint=2;
private var A_M_VERT:uint=4;
private var A_M_ROT:uint=8;
private var anchorMode:uint;
private var anchored:Boolean;
private var anchorX:Number;
private var anchorY:Number;
protected function canvas1_keyDownHandler(event:KeyboardEvent):void {
if(!hasOverlay)return;
if(event.keyCode==8 || event.keyCode==46 || event.keyCode==27) { // backspace-delete-esc
anchored=false;
} else if(event.keyCode==32) { // space
if(NIISlice.mouseX>=0 && NIISlice.mouseY>=0 && NIISlice.mouseX<NIISlice.width && NIISlice.mouseY<NIISlice.height) {
anchorX=mouseX;
anchorY=mouseY;
anchored=true;
const f:Number=(anchorX-ox)*sliceDescriptor.width/ow;
} else if(anchored)anchored=false; //??
} else if(event.keyCode=='H'.charCodeAt() || event.keyCode=='h'.charCodeAt()) {
btn_hist.selected=!btn_hist.selected;
histHandler(null);
} else if(event.keyCode=='V'.charCodeAt() || event.keyCode=='v'.charCodeAt()) {
btn_vol.selected=!btn_vol.selected;
volHandler(null);
} else if(event.keyCode==33) { // PageUp
if(AlphaSlider.value<90) {
AlphaSlider.value+=10;
alphaHandler(null);
}
} else if(event.keyCode==34) { // PageDown
if(AlphaSlider.value>10) {
AlphaSlider.value-=10;
alphaHandler(null);
}
}
drawAnchor();
}
public function drawAnchor():void {
const g:Graphics=MarkerCanvas.graphics;
g.clear();
if(!anchored)
return;
// no-rot
g.lineStyle(1,0xFF00);
g.moveTo(anchorX-10,anchorY);
g.lineTo(anchorX+10,anchorY);
g.moveTo(anchorX,anchorY-10);
g.lineTo(anchorX,anchorY+10);
g.lineStyle(2,0xFF00);
if((anchorMode & A_M_HORIZ)!=0) {
g.moveTo(anchorX-6,anchorY-4);
g.lineTo(anchorX-10,anchorY);
g.lineTo(anchorX-6,anchorY+4);
g.moveTo(anchorX+6,anchorY-4);
g.lineTo(anchorX+10,anchorY);
g.lineTo(anchorX+6,anchorY+4);
}
if((anchorMode & A_M_VERT)!=0) {
g.moveTo(anchorX-4,anchorY-6);
g.lineTo(anchorX,anchorY-10);
g.lineTo(anchorX+4,anchorY-6);
g.moveTo(anchorX-4,anchorY+6);
g.lineTo(anchorX,anchorY+10);
g.lineTo(anchorX+4,anchorY+6);
}
}
public function drawMidCross():void {
if(hasOverlay)return;
const g:Graphics=MarkerCanvas.graphics;
g.lineStyle(1,0xFF8080);
g.moveTo(NIISlice.x+NIISlice.width/2,0);
g.lineTo(NIISlice.x+NIISlice.width/2,height);
g.moveTo(0,NIISlice.y+NIISlice.height/2);
g.lineTo(width,NIISlice.y+NIISlice.height/2);
}
protected function mfoc(event:MouseEvent):void {
setFocus();
}
protected function addedToStageHandler(event:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,canvas1_keyDownHandler,true);
addEventListener(MouseEvent.MOUSE_OVER,mfoc);
addEventListener(MouseEvent.MOUSE_DOWN,mdown);
addEventListener(MouseEvent.MOUSE_UP,mup);
addEventListener(MouseEvent.MOUSE_OUT,mup);//??
addEventListener(MouseEvent.MOUSE_MOVE,mdrag);
addEventListener(MouseEvent.DOUBLE_CLICK,mdouble);
}
private var dragging:Boolean;
private var dragx:Number;
private var dragy:Number;
private var ox:Number;
private var oy:Number;
private var ow:Number;
private var oh:Number;
protected function mdown(event:MouseEvent):void {
if(QuickNII.navilock)return;
if(NIISlice.mouseX>=0 && NIISlice.mouseY>=0 && NIISlice.mouseX<NIISlice.width && NIISlice.mouseY<NIISlice.height) {
dragging=true;
dragx=mouseX;
dragy=mouseY;
ox=NIISlice.x;
oy=NIISlice.y;
ow=NIISlice.width;
oh=NIISlice.height;
if(anchorMode==A_M_NONE || !anchored)
drawMidCross();
}
}
protected function mup(event:MouseEvent):void {
drawAnchor();
if(!dragging)return;
dragging=false;
var ushift:Number=0;
var vshift:Number=0;
if(anchorMode==A_M_NONE || !anchored) {
ushift=(dragx-mouseX)*sliceDescriptor.width/ow;
vshift=-(dragy-mouseY)*sliceDescriptor.height/oh;
}
else if(anchored) {
if((anchorMode & A_M_HORIZ)!=0) {
const xrate:Number=slicw/NIISlice.width;
const xdist:Number=(ox+ow/2-anchorX)*sliceDescriptor.width/ow;
ushift=xdist*(xrate-1);
sliceDescriptor.width*=xrate;
outer.setDrag();
}
if((anchorMode & A_M_VERT)!=0) {
const yrate:Number=slich/NIISlice.height;
const ydist:Number=(oy+oh/2-anchorY)*sliceDescriptor.height/oh;
vshift=-ydist*(yrate-1);
sliceDescriptor.height*=yrate;
outer.setDrag();
}
}
sliceDescriptor.UVShift(ushift,vshift);
sliceDescriptor.CommitChanges();
}
protected function mdrag(event:MouseEvent):void {
if(!dragging)return;
sliceDescriptor.CommitChanges();
if(anchorMode==A_M_NONE || !anchored) {
NIISlice.x=ox+mouseX-dragx;
NIISlice.y=oy+mouseY-dragy;
}
if(anchored) {
if((anchorMode & A_M_HORIZ)!=0) {
const xrate:Number=(mouseX-anchorX)/(dragx-anchorX);
NIISlice.width=ow*xrate;
NIISlice.x=anchorX-(anchorX-ox)*xrate;
}
if((anchorMode & A_M_VERT)!=0) {
const yrate:Number=(mouseY-anchorY)/(dragy-anchorY);
NIISlice.height=oh*yrate;
NIISlice.y=anchorY-(anchorY-oy)*yrate;
}
}
}
public var outer:Object;
public function onLeft(event:MouseEvent):void{sliceDescriptor.UVStep(+1,0);sliceDescriptor.StopRedo();}
public function onRight(event:MouseEvent):void{sliceDescriptor.UVStep(-1,0);sliceDescriptor.StopRedo();}
public function onUp(event:MouseEvent):void{sliceDescriptor.UVStep(0,-1);sliceDescriptor.StopRedo();}
public function onDown(event:MouseEvent):void{sliceDescriptor.UVStep(0,+1);sliceDescriptor.StopRedo();}
public function onCCW(event:MouseEvent):void{sliceDescriptor.UVStepRot(-1);sliceDescriptor.StopRedo();}
public function onCW(event:MouseEvent):void{sliceDescriptor.UVStepRot(+1);sliceDescriptor.StopRedo();}
public function onHS(/*event:MouseEvent*/):void{anchorMode=A_M_HORIZ;drawAnchor();}
public function onVS(/*event:MouseEvent*/):void{anchorMode=A_M_VERT;drawAnchor();}
public function onDrag(/*event:MouseEvent*/):void{anchorMode=A_M_NONE;drawAnchor();}
public function alphaHandler(event:SliderEvent):void {
if(imageover)ImageOverlay.alpha=AlphaSlider.value/100;
else NIISlice.alpha=1-AlphaSlider.value/100;
btn_hist.selected=btn_vol.selected=false;
}
public function histHandler(event:Event):void {
if(imageover)ImageOverlay.alpha=btn_hist.selected?1:AlphaSlider.value/100;
else NIISlice.alpha=1-(btn_hist.selected?1:AlphaSlider.value/100);
btn_vol.selected=false;
}
public function volHandler(event:Event):void {
if(imageover)ImageOverlay.alpha=btn_vol.selected?0:AlphaSlider.value/100;
else NIISlice.alpha=1-(btn_vol.selected?0:AlphaSlider.value/100);
btn_hist.selected=false;
}
private static const imageover:Boolean=true; //!!
]]>
</mx:Script>
<mx:Image id="NIISlice" maintainAspectRatio="false"/>
<mx:Image id="ImageOverlay" complete="canvas1_resizeHandler(null)"/>
<mx:VSlider id="AlphaSlider" maximum="100" enabled="false" visible="false" liveDragging="true" change="alphaHandler(event)"/>
<mx:Canvas id="MarkerCanvas" mouseEnabled="false"/>
<mx:Button label="H" id="btn_hist" width="31" enabled="false" visible="false" toggle="true" change="histHandler(event)" toolTip="Show (histological) image only"/>
<mx:Button label="V" id="btn_vol" width="31" enabled="false" visible="false" toggle="true" change="volHandler(event)" toolTip="Show volume only"/>
</mx:Canvas>