forked from BiomedNMR/arrShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asWindowingClass.m
1207 lines (1008 loc) · 46.2 KB
/
asWindowingClass.m
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
% Copyright (C) 2009-2013 Biomedizinische NMR Forschungs GmbH
% http://www.biomednmr.mpg.de
% Author: Tilman Johannes Sumpf <[email protected]>
%
% Distributed under the Boost Software License, Version 1.0.
% (See accompanying file LICENSE_1_0.txt or copy at
% http://www.boost.org/LICENSE_1_0.txt)
classdef asWindowingClass < handle
properties (GetAccess = private, SetAccess = private)
% handle
ph = []; % panel handle
ih = []; % image handle
ah = []; % axes handle
cntSliderH = []; % slider handle
widthSliderH = []; %
filterButtonH = []; % button handle
sendAbsButtonH = []; %
sendRelButtonH = []; %
keepAbsButtonH = []; %
cmh = []; % context menu handle
cntTextH = []; % static-text handle for "C"
widthTextH = []; % ...and "W"
CWTextH = []; % ...and "C/W"
% callbacks
updFigCb = []; %
apply2allCb = []; % send to all relatives callback
getPhaseColormapCb = [];
% data type
isComplex = false;
complexRef = [];
% center and width
usePhaseCW = false; % individual windowing settings are stored and
% restored for phase and for
% magnitude/real/imag views.
magniCW = [0,1]; % these values are set to the current slider
phaseCW = [0,360];% values (depending on the usePhaseCW state)
% data ranges
magniMin = -0.5;
magniMax = 0.5;
phaseMin = -180;
phaseMax = 180;
% toggle states
keepRelCW = false;
keepAbsCW = false;
% context menu handles
keepRelCWctxmH = []; % handle to context menu entry for keepRelCw
keepAbsCWctxmH = [];
cntLimits = [0,0];
widthLimits = [0,0];
rangeCalcMethod = 1;% derive data range from:
% 0 = manual 1 = min/max 2 = percentile
% (calculate image range from percentile rather
% than just from min and max helps to have a
% reasonable windowing range for noisy data)
percentile = 98;
isInitialized = false;
isEnabled = true;
end
properties (Constant, Access = private)
% minimum width value (if the calculated width falls under that
% value, the windowing object is automatically disabled)
MIN_VALID_WIDTH = realmin;
% initial relative windowing
INITIAL_REL_CW = [.5, 1];
% background colors for keepAbsoluteCW and keepRelativeCW
BG_COLOR_REL = get(0,'defaultuicontrolbackgroundcolor');
BG_COLOR_ABS = [205/255;0;0]; % dark red
end
%#ok<*FPARK>
% Deactivate the warning telling me that I should use textscan instead
% of strread... I like textscan. I'll change it if I feel like having
% too much time...
properties (GetAccess = public, SetAccess = private)
sendAbsWindow = false;
sendRelWindow = false;
end
methods (Access = public)
function obj = asWindowingClass(...
parentPanelHandle,...
panelPosition,...
updFigCb,...
apply2allCb,...
getPhaseColormapCb,...
icons)
obj.updFigCb = updFigCb;
obj.apply2allCb = apply2allCb;
obj.getPhaseColormapCb = getPhaseColormapCb;
% create parent panel
obj.ph = uipanel('visible','on','Units','normalized',...
'Position',panelPosition,'Parent',parentPanelHandle,...
'Tag','asWindowingPanel');
% create filter button
htmlToolTip = ['<html><b>Filter:</b><br><table>',...
'<tr><td><u>Normal click</u></td><td>: Enable filter (use percentile)</td></tr>',...
'<tr><td><u>Ctrl+click:</u></td>: Set percentile</td></tr>',...
'<tr><td><u>Shift+click:</u></td>: Manual mode</td></tr></table>',...
'<br><p style="width:180px; text-align:left"><i>',...
'Description:<br>Without ''filter'', the slider range for the windowing is calculated ',...
'from the min. and max. of the image. This can be disadvantageous for images ',...
'with strong outliers. Filter mode uses percentile instead.',...
'</i></p></html>'];
obj.filterButtonH = uicontrol('Style','togglebutton',...
'Parent', obj.ph,...
'Units','normalized',...
'Position',[.4,.75,.15,.25],...
'SelectionHighlight','off',...
'tooltip',htmlToolTip,...
'Callback',@(src,evnt)filtButtonCb(obj),...
'Value',0,...
'CData',icons.filter);
function filtButtonCb(obj)
fh = get(get(obj.ph,'Parent'),'Parent');
modifiers = get(fh,'currentModifier');
val = get(obj.filterButtonH,'Value');
if isempty(modifiers)
switch(val)
case 1
% set range calc method to percentile
obj.setRangeCalcMethod(2);
case 0
% set range calc method to minMax
obj.setRangeCalcMethod(1);
end
else
if ismember('control',modifiers)
set(obj.filterButtonH,'Value',~val); % invert toggle state
obj.setPercentile(); % alter the percentile
else
if ismember('shift',modifiers)
set(obj.filterButtonH,'Value',~val); % invert toggle state
obj.overwriteSliderLimits();
end
end
end
end
% create send button
obj.sendAbsButtonH = uicontrol('Style','togglebutton',...
'Parent', obj.ph,...
'Units','normalized',...
'Position',[.85,.75,.15,.25],...
'SelectionHighlight','off',...
'tooltip','Send absolute window to relatives',...
'Callback',@(src,evnt)obj.toggleSendAbsWindow,...
'String','A',...
'fontweight','bold',...
'ForegroundColor', obj.BG_COLOR_ABS,...
'CData',icons.send);
obj.sendRelButtonH = uicontrol('Style','togglebutton',...
'Parent', obj.ph,...
'Units','normalized',...
'Position',[.7,.75,.15,.25],...
'SelectionHighlight','off',...
'tooltip','Send relative window to relatives',...
'Callback',@(src,evnt)obj.toggleSendRelWindow,...
'String','R',...
'fontweight','bold',...
'ForegroundColor','blue',...
'CData',icons.send);
% create button for the window behaviour
htmlToolTip = ['<html><b>Window behavior when changing frames:</b><br><table>',...
'<tr><td><font color="red"><b>A</font></b></td><td>: Keep the absolute windowing</td></tr>',...
'<tr><td><b>R</b></td>: Keep the relative windowing</td></tr></table>',...
'<br><p style="width:200px; text-align:justify"><i>',...
'Description: The absolute windowing determines how an image value is mapped to a pixel color. ',...
'If kept constant, a value of e.g. 1 will always be white. The relative windowing ',...
'consideres the actual data range. If kept constant, the absolute windowing will be ',...
'recalculated for every frame such that white always corresponds to e.g. 90% of the data maximum.',...
'</i></p></html>'];
obj.keepAbsButtonH = uicontrol('Style','pushbutton',...
'Parent', obj.ph,...
'Units','normalized',...
'Position',[.55,.75,.15,.25],...
'SelectionHighlight','off',...
'tooltip',htmlToolTip,...
'Callback',@(src,evnt)absButtonCb(obj),...
'String','A');
function absButtonCb(obj)
switch(get(obj.keepAbsButtonH,'String'))
case {'A','-'}
% toggle to keepRelativeWindow
obj.setKeepRelCW(true);
case 'R'
% toggle to keepAbsWindow
obj.setKeepAbsCW(true);
end
% note: disabling both options ('-') has been removed since
% the behavior can also be achieved by simply keeping the
% relative initial windowing.
end
% create center and width slider
hintTxt = ['<br><br><i>Hints:<br>- Use center mouse button + mouse movement for convenient windowing<br>',...
'- Use double click to reset windowing</i>'];
obj.cntSliderH = uicontrol('Style','slider',...
'Parent', obj.ph,...
'Units','normalized',...
'Tag','cntSlider',...
'tooltip',['<html>Center (inverse brightness)',hintTxt,'</html>'],...
'Position',[0,.5,1,.24],...
'Callback',@(src,evnt)obj.sliderCb());
obj.widthSliderH = uicontrol('Style','slider',...
'Parent', obj.ph,...
'Units','normalized',...
'Position',[0,.25,1,.24],...
'tooltip',['<html>Width (inverse contrast)',hintTxt,'</html>'],...
'Callback',@(src,evnt)obj.sliderCb());
% create static annotation text
obj.cntTextH = uicontrol('Style','Text',...
'Parent', obj.ph,...
'String','C/W',...
'FontSize',8,...
'Units','normalized',...
'tooltip',['<html>Center / width of the window<br>(inverse brightness / inverse contrast)',hintTxt,'</html>'],...
'HorizontalAlignment','left',...
'Position',[0,.74,.4,.23]);
% create dynamic text for CW values
obj.CWTextH = uicontrol('Style','Text',...
'Parent', obj.ph,...
'String','',...
'Units','normalized',...
'FontSize',8,...
'tooltip',['<html>Center / width of the window<br>(inverse brightness / inverse contrast)',hintTxt,'</html>'],...
'HorizontalAlignment','center',...
'Position',[0.01,0,.98,.2]);
% create context menu
obj.cmh.base = uicontextmenu;
uimenu(obj.cmh.base,'Label','Input center and width',...
'callback',@(src,evnt)obj.setCW);
uimenu(obj.cmh.base,'Label','Input min and max',...
'callback',@(src,evnt)obj.setCLim);
uimenu(obj.cmh.base,'Label','Activate immediate slider update',...
'callback',@(src,evnt)obj.activateImmediateUpdate);
uimenu(obj.cmh.base,'Label','Reset windowing (double click)',...
'callback',@(src,evnt)obj.resetWindowing);
% ---
obj.cmh.keepRelCW = uimenu(obj.cmh.base,'Label','Keep relativ windowing',...
'callback',@(src,evnt)obj.setKeepRelCW,...
'Checked','off', 'Separator','on');
obj.cmh.keepAbsCW = uimenu(obj.cmh.base,'Label','Keep absolute windowing',...
'callback',@(src,evnt)obj.setKeepAbsCW,...
'Checked','off');
% ---
uimenu(obj.cmh.base,'Label','Copy absolute windowing (Strg + c)',...
'callback',@(src,evnt)obj.copyAbsWindow,...
'Separator','on');
uimenu(obj.cmh.base,'Label','Paste absolute windowing (Strg + v)',...
'callback',@(src,evnt)obj.pasteAbsWindow);
uimenu(obj.cmh.base,'Label','Load absolute windowing from txt (Strg + o)',...
'callback',@(src,evnt)obj.loadAbsWindow);
% ---
obj.cmh.sendAbsWindow = uimenu(obj.cmh.base,'Label','Send absolute windowing to all relatives' ,...
'callback',@(src,evnt)obj.toggleSendAbsWindow,...
'Separator','on');
obj.cmh.sendRelWindow = uimenu(obj.cmh.base,'Label','Send relative windowing to all relatives' ,...
'callback',@(src,evnt)obj.toggleSendRelWindow);
% ---
obj.cmh.rangeCalcMethod = uimenu(obj.cmh.base,'Label','Derive data range from...',...
'separator','on');
% ->
obj.cmh.rangeCalcManu = uimenu(obj.cmh.rangeCalcMethod,'Label','Manual',...
'callback',@(src,evnt)obj.setRangeCalcMethod(0));
obj.cmh.rangeCalcMiMa = uimenu(obj.cmh.rangeCalcMethod,'Label','Min/Max',...
'callback',@(src,evnt)obj.setRangeCalcMethod(1));
obj.cmh.rangeCalcPerc = uimenu(obj.cmh.rangeCalcMethod,...
'Label',[num2str(obj.percentile),'% Percentile'],...
'callback',@(src,evnt)obj.setRangeCalcMethod(2),...
'checked','on'); % check percentile by default
obj.cmh.percentile = uimenu(obj.cmh.rangeCalcMethod,...
'Label','Change percentile...',...
'callback',@(src,evnt)obj.setPercentile(),...
'separator','on');
% <-
% assign context menu to panel, slider, and text
set(obj.ph,'uicontextmenu',obj.cmh.base);
set(obj.cntSliderH,'uicontextmenu',obj.cmh.base);
set(obj.widthSliderH,'uicontextmenu',obj.cmh.base);
set(obj.cntTextH,'uicontextmenu',obj.cmh.base);
set(obj.widthTextH,'uicontextmenu',obj.cmh.base);
set(obj.CWTextH,'uicontextmenu',obj.cmh.base);
end
function toggleUsePhaseCW(obj,bool)
% the windowing object stores two different window value sets
% (center and width). One for phase - and one for 'non phase'
% views...
if obj.isEnabled
if nargin < 2
obj.usePhaseCW = ~obj.usePhaseCW;
else
obj.usePhaseCW = bool;
end
end
end
function linkToImage(obj,ihandle)
% This is called whenever the image in the arrayShow gui
% changes (e.g. due to a different selection string or a
% different complex part)
% If the image handle differs from the previous one, the slider
% limits and values are updated according to the new image
% dynamic range.
if ihandle == obj.ih
return; % (nothing needs to be done)
end
obj.ih = ihandle;
obj.ah = get(ihandle,'Parent');
% if the ihandle is not actually an image, e.g. because quiver
% has been used to illustrate the image array, diable the
% windowing functionality
if ~strcmp(get(ihandle,'Type'),'image')
obj.disable();
return;
end
% if we reached this point, we are dealing with a new image
% which might have a different dynamic range than the previous
% one...
% get reference image data
refImage = get(obj.ih,'CData');
if size(refImage,3) == 3
% assume that we are dealing with an rgb array, made from a
% complex image. So get complex image from the axes
% UserData
obj.isComplex = true;
ud = get(obj.ah,'UserData');
obj.complexRef = ud.cplxImg;
else
obj.isComplex = false;
end
% Update the data range properties (imageMin, max, etc)
% according to the choosen rangeCalcMethod
obj.updateDataRange()
if ~obj.isInitialized
obj.updateCWtext();
obj.setKeepRelCW(true);
obj.isInitialized = true;
end
end
function setRangeCalcMethod(obj, method)
% setRangeCalcMethod(obj, method)
% Sets how the windowing range is calculated
% method can be either a nunber or a string:
% 0 : 'manual'
% 1 : 'min/max' (default)
% 2 : 'percentile'
switch method
case {0,'manual'} % manual
set(obj.cmh.rangeCalcManu,'checked','on');
set(obj.cmh.rangeCalcMiMa,'checked','off');
set(obj.cmh.rangeCalcPerc,'checked','off');
set(obj.filterButtonH,'Value',0);
obj.overwriteSliderLimits();
obj.rangeCalcMethod = 0;
return;
case {1,'min/max'} % min/max
set(obj.cmh.rangeCalcManu,'checked','off');
set(obj.cmh.rangeCalcMiMa,'checked','on');
set(obj.cmh.rangeCalcPerc,'checked','off');
set(obj.filterButtonH,'Value',0);
obj.rangeCalcMethod = 1;
case {2,'percentile'} % percentile
set(obj.cmh.rangeCalcManu,'checked','off');
set(obj.cmh.rangeCalcMiMa,'checked','off');
set(obj.cmh.rangeCalcPerc,'checked','on');
set(obj.filterButtonH,'Value',1);
obj.rangeCalcMethod = 2;
otherwise
error('asWindowingClass:invalidMethod','Invalid range calc method %d',obj.rangeCalcMethod);
end
obj.updateDataRange();
end
function setPercentile(obj, per)
if nargin < 2
per = obj.percentile;
perStr = mydlg('Enter percentile','Enter percentile',num2str(per));
if ~isempty(perStr)
try
per = str2double(perStr);
catch err
if strcmp(err.identifier,'MATLAB:dataread:TroubleReading')
fprintf('incorrect limits format\n');
else
rethrow(err);
end
end
else
return
end
end
if per == obj.percentile
return;
end
obj.percentile = per;
set(obj.cmh.rangeCalcPerc,'Label',[num2str(obj.percentile),'% Percentile']);
if obj.rangeCalcMethod == 2 % percentile
obj.updateDataRange();
end
end
function overwriteSliderLimits(obj, mi, ma)
% if not given as an argument, get min and max from input
% dialog
if nargin < 3
[mi, ma] = obj.getMinMaxWidth();
limStr = inputdlg({'min','max'},'Enter min and max values',1,{num2str(mi),num2str(ma)});
if ~isempty(limStr)
try
mi = str2double(limStr{1});
ma = str2double(limStr{2});
catch err
if strcmp(err.identifier,'MATLAB:dataread:TroubleReading')
fprintf('incorrect limits format\n');
else
rethrow(err);
end
end
else
fprintf('incorrect limits format\n');
return
end
end
% set the range calc method to 'manual'
obj.rangeCalcMethod = 0;
% derive width
wi = ma - mi;
% assure that the width is not too close to zero
if wi < obj.MIN_VALID_WIDTH
warning('asWindowingClass:invalidValue','Minimal width is limited to %e',obj.MIN_VALID_WIDTH);
ma = ma + obj.MIN_VALID_WIDTH;
end
% get the previous relative CW, in case we want to keep that
prevRelCW = obj.getRelCW();
% store the new data properties
obj.setMinMaxWidth(mi, ma);
% update slider limits
obj.updateSliderLimits(false, prevRelCW);
end
function setCLim(obj, CLim, adaptLimits, apply2relatives)
if obj.isEnabled
% default parameters
if nargin < 4
apply2relatives = obj.sendAbsWindow || obj.sendRelWindow;
if nargin < 3
adaptLimits = true;
end
end
% if not given as an argument, get CLim from input dialog
if nargin < 2
CLim = obj.getCLim;
CLimStr = mydlg('Enter window limits','Enter window limits',[num2str(CLim(1)),' , ',num2str(CLim(2))]);
if ~isempty(CLimStr)
try
[CLim(1), CLim(2)] = strread(CLimStr,'%f%f','delimiter',','); %#ok<*FPARK>
catch err
if strcmp(err.identifier,'MATLAB:dataread:TroubleReading')
fprintf('incorrect CLim format\n');
else
rethrow(err);
end
end
end
end
% derive absolute width and center values
width = CLim(2) - CLim(1);
center = CLim(1) + width/2;
obj.setCW([center, width], adaptLimits, apply2relatives);
end
end
function CW = getCW(obj)
% get center and width
if obj.usePhaseCW
CW = obj.phaseCW;
else
CW = obj.magniCW;
end
end
function relCW = getRelCW(obj)
if obj.isEnabled
CW = obj.getCW;
[mi, ~, wi] = obj.getMinMaxWidth;
% get center and width setting, relative to image width
relCW(1) = double((CW(1) - mi )/ wi);
relCW(2) = double(CW(2) / wi);
else
relCW = obj.INITIAL_REL_CW;
end
end
function activateImmediateUpdate(obj)
% dirty way of creating a valueAdjustment- java callback
% (actually i'd love this to be the default behaviour. However,
% it seems to leave orphant java classes in the matlab sessions
% which can become a problem when trying to get rid of old
% arrayShow instances)
hJScrollBar = findjobj('depth',19,'nomenu','Class','Slider');
hJScrollBar(1).AdjustmentValueChangedCallback = @(src, evnt)obj.sliderCb;
hJScrollBar(2).AdjustmentValueChangedCallback = @(src, evnt)obj.sliderCb; %#ok<NASGU> it is used :)
clear('hJScrollBar');
end
function setCW(obj, CW, adaptLimits, apply2relatives)
if obj.isEnabled
if nargin < 4
apply2relatives = obj.sendAbsWindow || obj.sendRelWindow;
if nargin < 3
adaptLimits = true;
end
end
% if not given as an argument, get CW from input dialog
if nargin < 2
CW = obj.getCW;
CWStr = mydlg('Enter window center and width','Enter window center and width',[num2str(CW(1)),' , ',num2str(CW(2))]);
if ~isempty(CWStr)
try
[CW(1), CW(2)] = strread(CWStr,'%f%f','delimiter',',');
catch err
if strcmp(err.identifier,'MATLAB:dataread:TroubleReading')
fprintf('incorrect CLim format\n');
else
rethrow(err);
end
end
end
end
% set center and width
center = CW(1);
width = CW(2);
if adaptLimits
% adapt slider limits
if center < get(obj.cntSliderH,'Min')
set(obj.cntSliderH,'Min',center);
else
if center > get(obj.cntSliderH,'Max')
set(obj.cntSliderH,'Max',center);
end
end
if width < get(obj.widthSliderH,'Min')
set(obj.widthSliderH,'Min',width);
else
if width > get(obj.widthSliderH,'Max')
set(obj.widthSliderH,'Max',width);
end
end
else
% make sure, that values are within valid range
center = asWindowingClass.limit(center,get(obj.cntSliderH,'Min'),get(obj.cntSliderH,'Max'));
width = asWindowingClass.limit(width ,get(obj.widthSliderH,'Min'),get(obj.widthSliderH,'Max'));
end
% set to slider
set(obj.cntSliderH,'Value',center);
set(obj.widthSliderH,'Value',width);
obj.backupSliderValues();
% derive axes limits
CLim(1) = center - width/2;
CLim(2) = CLim(1) + width;
if obj.isComplex
rgbImg = complex2rgb(obj.complexRef,256,CLim, obj.getPhaseColormapCb());
set(obj.ih,'CData',rgbImg);
else
% last check, if the width is too small to neglect rounding
% errors
if CLim(1) - CLim(2) == 0
obj.enable(false);
return;
end
% set limits to axes
set(obj.ah,'CLim',CLim);
end
% update CW text
obj.updateCWtext();
if apply2relatives
if obj.sendAbsWindow
obj.apply2allCb('window.setCW',false, CW, adaptLimits,false);
else if obj.sendRelWindow
relCW = obj.getRelCW;
obj.apply2allCb('window.setRelCW', false, relCW, adaptLimits, false);
end
end
end
end
end
function setRelCW(obj, relCW, adaptLimits, apply2relatives)
if obj.isEnabled
if nargin < 4
apply2relatives = obj.sendRelWindow || obj.sendAbsWindow;
if nargin < 3
adaptLimits = true;
end
end
[mi, ~, wi] = obj.getMinMaxWidth;
CW = relCW * wi;
CW(1) = CW(1) + mi;
obj.setCW(CW, adaptLimits, apply2relatives);
obj.updateCWtext();
end
end
function copyAbsWindow(obj)
clipboard('copy',num2str(obj.getCW));
fprintf('Copied center and width to clipboard\n');
end
function pasteAbsWindow(obj)
pastedCW = str2num(clipboard('paste')); %#ok<ST2NM> the data is not expected to be a scalar
if ~isempty(pastedCW)
if ~any(size(pastedCW) ~= [1,2])
obj.setCW(pastedCW);
obj.updFigCb();
return
end
end
fprintf('No valid center/width information in clipboard\n');
end
function loadAbsWindow(obj, file)
if nargin < 2
[fname, fpath] = uigetfile('*.txt');
file = [fpath, fname];
end
if isempty(file) || isnumeric(file)
return
else
% read file
fid = fopen(file,'r');
str = fread(fid,'char=>char');
fclose(fid);
% find CW strung
strIdentifier = 'center/width = [';
pos = strfind(str',strIdentifier);
if isempty(pos)
fprintf('No valid center/width information in clipboard\n');
return;
else
pos = pos + length(strIdentifier);
[C, W] = strread(str(pos:end),'%f %f',1);
obj.setCW([C,W]);
end
end
end
function CLim = getCLim(obj)
% get current absolute windowing
CLim = get(obj.ah,'CLim');
end
function ah = getAxesHandle(obj)
ah = obj.ah;
end
function ih = getImageHandle(obj)
ih = obj.ih;
end
function width = getDataWidth(obj)
[~,~,width] = obj.getMinMaxWidth();
end
function resetWindowing(obj, apply2relatives)
if nargin < 2
apply2relatives = obj.sendAbsWindow || obj.sendRelWindow;
end
if obj.isEnabled
[mi, ma, wi] = obj.getMinMaxWidth;
CLim = [mi,ma];
if obj.isComplex
rgbImg = complex2rgb(obj.complexRef,256,CLim, obj.getPhaseColormapCb());
set(obj.ih,'CData',rgbImg);
else
set(obj.ah,'CLim',CLim);
end
set(obj.cntSliderH,'Value',mi + wi/2);
set(obj.widthSliderH,'Value',wi);
obj.backupSliderValues();
obj.updateCWtext();
if apply2relatives
obj.apply2allCb('window.resetWindowing',false,false);
end
end
end
function setKeepRelCW(obj,bool)
if nargin < 2
obj.keepRelCW = ~obj.keepRelCW;
else
obj.keepRelCW = bool;
end
switch obj.keepRelCW
case true
set(obj.cmh.keepRelCW,'Checked','on');
set(obj.keepAbsButtonH,'String','R','BackgroundColor',obj.BG_COLOR_REL);
set(obj.cmh.keepAbsCW,'Checked','off'); %disable concurring option
obj.keepAbsCW = false;
case false
set(obj.cmh.keepRelCW,'Checked','off');
if(~obj.keepAbsCW)
% if neither option is set, draw a '-' in
% pushbutton
set(obj.keepAbsButtonH,'String','-',...
'BackgroundColor',get(0,'defaultuicontrolbackgroundcolor'));
end
end
end
function setKeepAbsCW(obj,bool)
if nargin < 2
obj.keepAbsCW = ~obj.keepAbsCW;
else
obj.keepAbsCW = bool;
end
switch obj.keepAbsCW
case true
set(obj.cmh.keepAbsCW,'Checked','on');
set(obj.keepAbsButtonH,'String','A','BackgroundColor',obj.BG_COLOR_ABS);
set(obj.cmh.keepRelCW,'Checked','off'); %disable concurring option
obj.keepRelCW = false;
case false
set(obj.cmh.keepAbsCW,'Checked','off');
if(~obj.keepRelCW)
% if neither option is set, draw a '-' in
% pushbutton
set(obj.keepAbsButtonH,'String','-',...
'BackgroundColor',get(0,'defaultuicontrolbackgroundcolor'));
end
end
end
function disable(obj)
% shortcut to obj.enable(false) for backward compatibility
obj.enable(false);
end
function enable(obj, state)
if nargin < 2
state = 'on';
end
if ischar(state)
% assume state to be an "on" or "off" string
stateStr = state;
else
% assume integer or boolean state and create according "on"
% or "off" string
stateStr = arrShow.boolToOnOff(state);
end
set(obj.cntSliderH,'Enable', stateStr);
set(obj.widthSliderH,'Enable', stateStr);
set(obj.cntTextH,'Enable', stateStr);
set(obj.widthTextH,'Enable', stateStr);
set(obj.CWTextH,'Enable', stateStr);
set(obj.sendAbsButtonH,'Enable', stateStr);
set(obj.sendRelButtonH,'Enable', stateStr);
set(obj.keepAbsButtonH,'Enable', stateStr);
obj.isEnabled = arrShow.onOffToBool(stateStr);
end
function bool = getIsEnabled(obj)
bool = obj.isEnabled;
end
% ----
function sendAbsWindowToRelatives(obj)
obj.apply2allCb('window.setCW',false,obj.getCW(),true,false);
end
function sendRelWindowToRelatives(obj)
relCW = obj.getRelCW();
obj.apply2allCb('window.setRelCW',false,relCW,true,false);
end
function toggleSendAbsWindow(obj,bool)
if nargin > 1
set(obj.cmh.sendAbsWindow,'Checked',arrShow.boolToOnOff(~bool));
end
switch get(obj.cmh.sendAbsWindow,'Checked')
case 'off'
obj.sendAbsWindow = true;
set(obj.cmh.sendAbsWindow,'Checked','on');
set(obj.sendAbsButtonH,'value',1);
% assure concurring option is disabled
set(obj.cmh.sendRelWindow,'Checked','off');
set(obj.sendRelButtonH,'value',0);
obj.sendRelWindow = false;
obj.sendAbsWindowToRelatives();
case 'on'
obj.sendAbsWindow = false;
set(obj.cmh.sendAbsWindow,'Checked','off');
set(obj.sendAbsButtonH,'value',0);
end
end
function toggleSendRelWindow(obj,bool)
if nargin > 1
set(obj.cmh.sendRelWindow,'Checked',arrShow.boolToOnOff(~bool));
end
switch get(obj.cmh.sendRelWindow,'Checked')
case 'off'
obj.sendRelWindow = true;
set(obj.cmh.sendRelWindow,'Checked','on');
set(obj.sendRelButtonH,'value',1);
% assure concurring option is disabled
set(obj.cmh.sendAbsWindow,'Checked','off');
obj.sendAbsWindow = false;
set(obj.sendAbsButtonH,'value',0);
obj.sendRelWindowToRelatives();
case 'on'
obj.sendRelWindow = false;
set(obj.cmh.sendRelWindow,'Checked','off');
set(obj.sendRelButtonH,'value',0);
end
end
% ----
end
methods (Access = private)
function updateDataRange(obj)
% Update the data range properties (imageMin, max, etc)
% according to the choosen rangeCalcMethod
% get the previous relative CW, in case we want to keep that
prevRelCW = obj.getRelCW();
if obj.rangeCalcMethod > 0
% get reference image data
refImage = get(obj.ih,'CData');
if obj.isComplex
refImage = abs(obj.complexRef);
end
switch obj.rangeCalcMethod
case 1 % min / max
mi = min(min(refImage));
ma = max(max(refImage));
case 2 % percentile
mi = -asWindowingClass.vecPerc(-refImage(:),obj.percentile);
ma = asWindowingClass.vecPerc(refImage(:),obj.percentile);
otherwise
error('asWindowingClass:invalidMethod','Invalid range calc method %d',obj.rangeCalcMethod);
end
% store the data range (min and max values) in the object
% properties
obj.setMinMaxWidth(mi, ma);
end
% update the slider limits
obj.updateSliderLimits(true, prevRelCW);
end
function [mi, ma, wi] = getMinMaxWidth(obj)
if obj.usePhaseCW
mi = obj.phaseMin;
ma = obj.phaseMax;
else
mi = obj.magniMin;
ma = obj.magniMax;
end
wi = ma - mi;
end
function setMinMaxWidth(obj, mi, ma)
if obj.usePhaseCW
obj.phaseMax = ma;
obj.phaseMin = mi;
else
obj.magniMax = ma;
obj.magniMin = mi;
end
end