-
Notifications
You must be signed in to change notification settings - Fork 1
/
MovieSlider.asv
954 lines (824 loc) · 42.3 KB
/
MovieSlider.asv
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
%% MOVIESLIDER Embeddable panel for playback, scrolling, and contrast/color map adjustment of 3D movies.
%
% This software depends on the GUI Layout Toolbox (http://www.mathworks.com/matlabcentral/fileexchange/47982-gui-layout-toolbox).
%
% The class MovieSlider creates a panel (that can be embedded like any other uix object) that
% displays the current frame of the movie tensor provided by the user, which is assumed to be
% 3-dimensional where the first two dimensions comprise a frame and the third dimension indexes a
% particular frame in the movie. GUI and keyboard controls allow automatic playback/looping as well
% as live scrolling through frames. There is also a button to cycle through contrast settings, and
% an extended configuration panel where the user can fine-tune the playback speed, color map and
% mapping range.
%
% Example usage:
% shape = normpdf(-2.5:0.05:2.5, 0, 1);
% tensor = bsxfun(@times, bsxfun(@times, shape, shape'), reshape(shape,1,1,[]));
% MovieSlider(tensor);
classdef MovieSlider < uix.VBox
%------- Constants
properties (Constant)
DEFAULT_FPS = 30
FRAME_STEP = 10
MIN_NUMBINS = 20
CONTRAST_BINNING = 30
% CONTRAST_RANGE = 10.^(-7:-1)
% CONTRAST_RANGE = 5e-7 * (1:100:1e3).^2
% CONTRAST_RANGE = 6e-10 * (1:1e2:1e3).^3
CONTRAST_RANGE = 6e-9 * (1:10:100).^4
DIALOG_POSITION = [0.4 0.35 0.2 0.3]
DIALOG_MONITOR = -1
SELECT_COLOR = cat(3, 156, 230, 255)/255
GUI_COLOR = cat(3, 0.9400, 0.9400, 0.9400)
GUI_FONT = 11
GUI_BTNSIZE = 20
GUI_BUTTON = [100 30]
GUI_BORDER = 5
RANGE_COLOR = [255 251 222]/255
RANGE_BORDER = [255 217 0 ]/255
CONFIGURABLES = {'contrastIndex', 'pixelRange', 'playbackFPS', 'colors'}
COLORMAPS = { 'gray', 'parula', 'hot', 'cool' ...
, 'jet', 'hsv' ...
, 'spring', 'summer', 'autumn', 'winter' ...
, 'winter', 'bone', 'copper', 'pink' ...
};
CODE_PATH = parsePath(mfilename('fullpath'))
ICON_END = MovieSlider.loadIcon('end.png')
ICON_PAUSE = MovieSlider.loadIcon('pause.png')
ICON_PLAY = MovieSlider.loadIcon('play.png')
ICON_REFRESH_OFF = MovieSlider.loadIcon('refresh.png', MovieSlider.GUI_COLOR)
ICON_REFRESH_ON = MovieSlider.loadIcon('refresh.png', MovieSlider.SELECT_COLOR)
ICON_STOP = MovieSlider.loadIcon('stop.png')
ICON_BEGIN = MovieSlider.loadIcon('begin.png')
ICON_CONFIG = MovieSlider.loadIcon('config.png')
ICON_SEARCH = MovieSlider.loadIcon('search.png')
end
%------- Private data
properties (Access = protected)
figParent
cntControl
txtInfo
btnConfig
btnPlay
btnRepeat
btnBegin
btnEnd
sldFrame
hZoom
lsnScroll
tmrPlayback
syncSiblings
end
%------- Public data
properties (SetAccess = protected)
movie
binnedMovie
pixelPDF = [0 0]
pixelCDF = [0 0]
pixelValue = [0 1]
pixelDomain = [-inf inf]
pixelRange = [0 1]
colors
doRepeat = false
currentFrame = 1
contrastIndex = 1
playbackFPS = MovieSlider.DEFAULT_FPS
axsMovie
imgMovie
titleMovie = gobjects(1)
end
properties
overlay = struct()
end
%________________________________________________________________________
methods
%----- Constructor
function obj = MovieSlider(parent, varargin)
% Default arguments and parent class constructor
if nargin < 1
parent = figure;
elseif isnumeric(parent) || numel(parent) > 1 || ~ishghandle(parent)
varargin = [parent, varargin];
parent = figure;
end
[email protected]( 'Parent', parent );
% Movie frame display
obj.axsMovie = axes ( 'Parent' , uicontainer('Parent', obj) ...
, 'ActivePositionProperty' , 'Position' ...
);
obj.imgMovie = image ( 'Parent' , obj.axsMovie ...
, 'CData' , [] ...
, 'CDataMapping' , 'scaled' ...
, 'UserData' , obj.currentFrame ...
);
hold(obj.axsMovie, 'on');
% Movie info and controls
obj.cntControl = uix.HBox ( 'Parent' , obj ...
);
obj.txtInfo = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'edit' ...
, 'Enable' , 'on' ...
, 'FontSize' , MovieSlider.GUI_FONT - 3 ...
, 'Callback' , @obj.editSetFrame ...
);
obj.btnConfig = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'pushbutton' ...
, 'CData' , MovieSlider.ICON_CONFIG ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'Callback' , @obj.cycleContrast ...
, 'ButtonDownFcn' , @obj.configureDisplay ...
, 'TooltipString' , '' ...
);
obj.btnPlay = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'pushbutton' ...
, 'CData' , MovieSlider.ICON_PLAY ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'Callback' , @obj.togglePlayback ...
, 'UserData' , false ...
, 'TooltipString' , 'Play' ...
);
obj.btnRepeat = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'togglebutton' ...
, 'CData' , MovieSlider.ICON_REFRESH_OFF ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'Callback' , @obj.setRepeat ...
, 'Value' , obj.doRepeat ...
, 'TooltipString' , 'Turn on repeat' ...
);
obj.btnBegin = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'pushbutton' ...
, 'CData' , MovieSlider.ICON_BEGIN ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'Callback' , {@obj.setFrame, 1} ...
, 'TooltipString' , 'First frame' ...
);
obj.sldFrame = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'slider' ...
, 'Min' , 1 ...
, 'Max' , 1 ...
, 'Value' , 1 ...
, 'SliderStep' , [1 1] ...
, 'Enable' , 'off' ...
);
obj.btnEnd = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'pushbutton' ...
, 'CData' , MovieSlider.ICON_END ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'TooltipString' , 'Last frame' ...
);
% Configure formatting and sizes
axis( obj.axsMovie, 'image', 'ij', 'off' );
set ( obj.cntControl, 'Widths' , [6 1 1 1 1 -1 1] * MovieSlider.GUI_BTNSIZE );
set ( obj , 'Heights' , [-1 MovieSlider.GUI_BTNSIZE] );
% Listeners for frame scrolling
obj.lsnScroll = addlistener(obj.sldFrame, 'ContinuousValueChange', @obj.drawFrame);
% Callback for mouseover info display and keyboard commands
obj.figParent = findParent(parent, 'figure');
movieSliders = get(obj.figParent, 'UserData');
if isempty(movieSliders)
movieSliders = obj;
else
movieSliders(end+1) = obj;
end
set ( obj.figParent ...
, 'UserData' , movieSliders ...
, 'WindowButtonDownFcn' , @MovieSlider.startZooming ...
, 'WindowButtonMotionFcn' , @MovieSlider.updatePixelInfo ...
, 'WindowKeyPressFcn' , @MovieSlider.keyboardControl ...
);
% Playback timer
obj.tmrPlayback = timer ( 'TimerFcn' , @obj.playMovie ...
, 'ExecutionMode' ,'fixedRate' ...
, 'BusyMode' ,'drop' ...
);
% Shortcut to load a given movie at construction time
if ~isempty(varargin)
obj.show(varargin{:});
end
end
%----- Destructor
function delete(obj)
if ~isempty(obj.tmrPlayback)
stop(obj.tmrPlayback);
delete(obj.tmrPlayback);
end
[email protected](obj);
end
%----- Retrieves the current configuration as can be passed to show()
function config = configuration(obj)
config = {obj.colors, obj.pixelDomain, obj.pixelRange, obj.contrastIndex};
end
%----- Sets the currently displayed movie
function show(obj, movie, colors, pixelDomain, contrast, contrastIndex)
% Default arguments
if nargin < 3 || isempty(colors)
colors = 'default';
end
if nargin < 4 || isempty(pixelDomain)
if min(movie(:)) == 0
pixelDomain = [0, inf];
else
pixelDomain = [-inf, inf];
end
end
if nargin < 5 || isempty(contrast)
contrast = 1;
end
if nargin > 5 % overridden by contrast, but set anyway for continuity
obj.contrastIndex = contrastIndex;
end
% Movie properties
obj.movie = movie;
if size(obj.movie,3) >= MovieSlider.MIN_NUMBINS * MovieSlider.CONTRAST_BINNING
obj.binnedMovie = rebin(obj.movie, MovieSlider.CONTRAST_BINNING, 3, @mean, 'omitnan');
else
obj.binnedMovie = obj.movie;
end
if islogical(contrast) && numel(contrast) == size(movie,1)*size(movie,2)
imgMask = frameMask(size(obj.binnedMovie), contrast, 1);
[obj.pixelPDF, edges] = histcounts(obj.binnedMovie(imgMask), 'Normalization', 'prob');
if nargin > 5
contrast = contrastIndex;
else
contrast = 1;
end
else
[obj.pixelPDF, edges] = histcounts(obj.binnedMovie, 'Normalization', 'prob');
end
if numel(obj.pixelPDF) < 2
obj.pixelPDF = [0 1];
obj.pixelValue = [0 1];
else
obj.pixelValue = (edges(1:end-1) + edges(2:end)) / 2;
end
obj.pixelCDF = cumsum(obj.pixelPDF);
obj.pixelDomain = pixelDomain;
obj.colors = colors;
obj.currentFrame = 1;
% Movie frame display
set ( obj.axsMovie ...
, 'XLim' , [0.5, 0.5+max(1,size(movie,2))] ...
, 'YLim' , [0.5, 0.5+max(1,size(movie,1))] ...
);
if isempty(movie)
set ( obj.imgMovie ...
, 'CData' , 0 ...
, 'UserData' , obj.currentFrame ...
);
else
set ( obj.imgMovie ...
, 'CData' , movie(:,:,obj.currentFrame) ...
, 'UserData' , obj.currentFrame ...
);
end
set ( obj.sldFrame ...
, 'Min' , 1 ...
, 'Max' , size(movie,3) ...
, 'SliderStep' , min([1 MovieSlider.FRAME_STEP]/(size(movie,3) - 1), 1) ...
, 'Value' , obj.currentFrame ...
);
set ( obj.btnEnd ...
, 'Callback' , {@obj.setFrame, size(movie,3)} ...
);
if size(movie,3) > 1
set(obj.sldFrame, 'Enable', 'on');
else
set(obj.sldFrame, 'Enable', 'off');
end
stop(obj.tmrPlayback);
% Apply formatting
colormap( obj.axsMovie, colors );
obj.setContrast(contrast);
end
%----- Set playback rate
function setPlaybackFPS(obj, playbackFPS)
obj.playbackFPS = playbackFPS;
end
%----- Sets the current title, or removes it if an empty string is provided
function setTitle(obj, string, varargin)
if isempty(string)
delete(obj.titleMovie);
obj.titleMovie = gobjects(1);
else
obj.titleMovie = title(obj.axsMovie, string, varargin{:});
end
end
%----- Sets the current frame to the given index
function setFrame(obj, handle, event, index)
if nargin < 3
index = handle;
end
if index ~= obj.currentFrame
set(obj.sldFrame, 'Value', index);
obj.drawFrame(index);
end
end
%----- Makes a clone of this MovieSlider in a new figure
function clone(obj, handle, event)
dup = MovieSlider(figure, obj.movie, obj.colors, obj.pixelDomain, obj.contrastIndex);
for prop = {'pixelPDF', 'pixelCDF', 'pixelValue', 'pixelRange'}
dup.(prop{:}) = obj.(prop{:});
end
% dup.setTitle(get(obj.titleMovie, 'String'));
end
end
%________________________________________________________________________
methods (Access = protected)
%----- Ensure that synchronized objects exist
function checkSiblings(obj)
obj.syncSiblings(~ishghandle(obj.syncSiblings)) = [];
end
%----- Shift the current frame by the given amount
function shiftFrame(obj, shift)
if shift < 0
obj.setFrame(max(1, obj.currentFrame + shift));
else
obj.setFrame(min(size(obj.movie,3), obj.currentFrame + shift));
end
end
%----- Set contrast level
function setContrast(obj, contrast)
if numel(contrast) == 1
obj.contrastIndex = max(1, min(contrast, numel(MovieSlider.CONTRAST_RANGE)));
saturation = MovieSlider.CONTRAST_RANGE(obj.contrastIndex);
sel = [1, 1 + find(diff(obj.pixelCDF) ~= 0)];
obj.pixelRange = interp1(obj.pixelCDF(sel), obj.pixelValue(sel), [saturation, 1-saturation], 'linear', 'extrap');
sel = isfinite(obj.pixelDomain);
obj.pixelRange(sel) = obj.pixelDomain(sel);
elseif contrast(2) > contrast(1)
obj.pixelRange = contrast;
else
return;
end
set(obj.axsMovie, 'CLim', obj.pixelRange);
end
%----- Callback for user to enter the desired frame
function editSetFrame(obj, handle, event, index)
value = sscanf(get(handle, 'String'), '%d');
if isempty(value) || value < 1 || value > size(obj.movie,3)
return;
end
stop(obj.tmrPlayback);
obj.setFrame(value);
end
%----- Callback to set whether the movie replays when at the end
function setRepeat(obj, handle, event)
obj.doRepeat = get(handle, 'Value');
if obj.doRepeat
set(handle, 'CData', MovieSlider.ICON_REFRESH_ON, 'TooltipString', 'Turn on repeat');
else
set(handle, 'CData', MovieSlider.ICON_REFRESH_OFF, 'TooltipString', 'Turn on repeat');
end
obj.checkSiblings();
for iSib = 1:numel(obj.syncSiblings)
other = obj.syncSiblings(iSib);
other.doRepeat = obj.doRepeat;
if other.doRepeat
set(other.btnRepeat, 'CData', MovieSlider.ICON_REFRESH_ON);
else
set(other.btnRepeat, 'CData', MovieSlider.ICON_REFRESH_OFF);
end
end
end
%----- Callback for scrolling between frames
function drawFrame(obj, handle, event)
if isnumeric(handle)
iFrame = handle;
else
iFrame = min(size(obj.movie,3), max(1, round(get(handle, 'Value'))));
end
if iFrame ~= obj.currentFrame
set(obj.imgMovie, 'CData', obj.movie(:,:,iFrame), 'UserData', iFrame);
obj.currentFrame = iFrame;
set(obj.txtInfo, 'String', sprintf('%d/%d', iFrame, size(obj.movie,3)));
end
obj.checkSiblings();
for iSib = 1:numel(obj.syncSiblings)
other = obj.syncSiblings(iSib);
jFrame = min(size(other.movie,3), max(1, iFrame));
if jFrame ~= other.currentFrame
set(other.imgMovie, 'CData', other.movie(:,:,jFrame), 'UserData', jFrame);
other.currentFrame = jFrame;
set(other.txtInfo, 'String', sprintf('%d/%d', jFrame, size(other.movie,3)));
end
end
set(obj.figParent, 'CurrentAxes', obj.axsMovie);
drawnow;
end
%----- Callback to load the next frame in playback mode
function playMovie(obj, handle, event)
if obj.currentFrame >= size(obj.movie,3)
if obj.doRepeat
obj.currentFrame = 0;
else
obj.togglePlayback(obj.btnPlay, [], true);
return;
end
end
obj.setFrame(obj.currentFrame + 1);
end
%----- Callback to start/stop automatic playback
function togglePlayback(obj, handle, event, forceStop)
if nargin < 4
forceStop = false;
end
stop(obj.tmrPlayback);
% If already playing, stop
if forceStop || get(handle, 'UserData')
set(handle, 'CData', MovieSlider.ICON_PLAY, 'UserData', false);
% If not playing, start
else
if obj.currentFrame >= size(obj.movie,3)
obj.currentFrame = 0;
end
set(handle, 'CData', MovieSlider.ICON_STOP, 'UserData', true);
set(obj.tmrPlayback, 'Period', max(1,round(1000/obj.playbackFPS))/1000);
start(obj.tmrPlayback);
end
end
%----- Callback for cycling contrast levels
function cycleContrast(obj, handle, event)
obj.setContrast(1 + mod(obj.contrastIndex, numel(MovieSlider.CONTRAST_RANGE)));
end
%----- Callback for adjusting frame display parameters
function configureDisplay(obj, handle, event)
% Allow only one instance
persistent fig;
if ishghandle(fig)
figure(fig);
return;
end
% Store current configuration in case of user cancel
original = struct();
for field = MovieSlider.CONFIGURABLES
original.(field{:}) = obj.(field{:});
end
% Create dialog box
fig = makePositionedFigure( MovieSlider.DIALOG_POSITION ...
, MovieSlider.DIALOG_MONITOR ...
, 'OuterPosition' ...
, 'Name' , 'Movie Slider Configuration' ...
, 'ToolBar' , 'none' ...
, 'MenuBar' , 'none' ...
, 'NumberTitle' , 'off' ...
, 'CloseRequestFcn' , @restoreConfig ...
, 'WindowButtonMotionFcn' , @mouseHoverHint ...
, 'WindowButtonUpFcn' , @(h,e) set(h,'WindowButtonMotionFcn',@mouseHoverHint) ...
, 'Visible' , 'off' ...
);
% Configuration panels
cntConfig = uix.HBox('Parent', fig, 'Padding', MovieSlider.GUI_BORDER);
cntContrast = uix.VBox('Parent', cntConfig);
uix.Empty('Parent', cntConfig);
cntControls = uix.VBox('Parent', cntConfig, 'Padding', MovieSlider.GUI_BORDER, 'Spacing', MovieSlider.GUI_BORDER);
% Contrast adjustment
leeway = 0.05 * (obj.pixelValue(end) - obj.pixelValue(1));
axsRange = obj.pixelValue([1 end]) + [-1 1]*leeway;
axsPDF = axes ( 'Parent' , cntContrast ...
, 'Box' , 'on' ...
, 'Layer' , 'top' ...
, 'XLim' , axsRange ...
, 'FontSize' , MovieSlider.GUI_FONT - 1 ...
, 'Units' , 'pixels' ...
);
sldContrast = uicontrol ( 'Parent' , cntContrast ...
, 'Style' , 'slider' ...
, 'Min' , 1 ...
, 'Max' , numel(MovieSlider.CONTRAST_RANGE) ...
, 'SliderStep' , [1 5]/(numel(MovieSlider.CONTRAST_RANGE)-1) ...
, 'Value' , obj.contrastIndex ...
, 'Callback' , @slideContrast ...
);
cntManual = uix.HBox( 'Parent', cntContrast, 'Padding', MovieSlider.GUI_BORDER );
editBound(1) = uicontrol ( 'Parent' , cntManual ...
, 'Style' , 'edit' ...
, 'String' , sprintf('%.4g', obj.pixelRange(1)) ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , {@editContrast, 1} ...
);
uix.Empty ( 'Parent', cntManual );
editBound(2) = uicontrol ( 'Parent' , cntManual ...
, 'Style' , 'edit' ...
, 'String' , sprintf('%.4g', obj.pixelRange(end)) ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , {@editContrast, 2} ...
);
% Other controls
txtFPS = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'text' ...
, 'String' , 'Frames/sec:' ...
, 'FontSize' , MovieSlider.GUI_FONT ...
);
edtFPS = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'edit' ...
, 'String' , sprintf('%.4g', obj.playbackFPS) ...
, 'FontSize' , MovieSlider.GUI_FONT ...
);
edtColors = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'edit' ...
, 'String' , obj.colors ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , @setColors ...
);
mnuColors = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'listbox' ...
, 'String' , MovieSlider.COLORMAPS ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , {@setColors, edtColors} ...
, 'Min' , 0 ...
, 'Max' , 2 ...
, 'Value' , find(strcmp(MovieSlider.COLORMAPS, obj.colors)) ...
);
uix.Empty ( 'Parent', cntControls );
% Dialog box ok/cancel
btnOK = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'pushbutton' ...
, 'String' , 'OK' ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , @acceptConfig ...
);
btnCancel = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'pushbutton' ...
, 'String' , 'Cancel' ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , @restoreConfig ...
);
btnClone = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'pushbutton' ...
, 'String' , 'Clone' ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , @cloneSelf ...
);
% Size of various panels
set ( cntConfig ...
, 'Widths' , [-1, MovieSlider.GUI_BTNSIZE, MovieSlider.GUI_BUTTON(1) + 2*MovieSlider.GUI_BORDER] ...
);
set ( cntContrast ...
, 'Heights' , [-1, MovieSlider.GUI_BTNSIZE, MovieSlider.GUI_BUTTON(2)+MovieSlider.GUI_BORDER] ...
);
set ( cntManual ...
, 'Widths' , [MovieSlider.GUI_BUTTON(1), -1, MovieSlider.GUI_BUTTON(1)] ...
);
set ( cntControls ...
, 'Heights' , [0.5 1 1 -1 1 1 1 1]*MovieSlider.GUI_BUTTON(2) ...
);
% Plot pixel distribution and range
hPDF = line ( 'Parent' , axsPDF ...
, 'XData' , obj.pixelValue ...
, 'YData' , obj.pixelPDF ...
, 'LineWidth' , 1 ...
, 'Color' , [0 0 0] ...
, 'PickableParts' , 'none' ...
);
xlabel(axsPDF, 'Pixel value', 'FontSize', MovieSlider.GUI_FONT-1);
ylabel(axsPDF, 'Frequency' , 'FontSize', MovieSlider.GUI_FONT-1);
yRange = get(axsPDF, 'YLim');
yRange(1) = 0;
[rngX, rngY] = rectangleCorners(obj.pixelRange(1), 0, obj.pixelRange(end)-obj.pixelRange(1), yRange(end));
hRange = patch ( 'Parent' , axsPDF ...
, 'XData' , rngX ...
, 'YData' , rngY ...
, 'EdgeColor' , 'none' ...
, 'FaceColor' , MovieSlider.RANGE_COLOR ...
, 'PickableParts' , 'none' ...
);
hBound(1) = line ( 'Parent' , axsPDF ...
, 'XData' , [1 1]*obj.pixelRange(1) ...
, 'YData' , yRange ...
, 'LineWidth' , 2 ...
, 'Color' , MovieSlider.RANGE_BORDER ...
, 'ButtonDownFcn' , {@startDragBound, 1} ...
);
hBound(2) = line ( 'Parent' , axsPDF ...
, 'XData' , [1 1]*obj.pixelRange(end) ...
, 'YData' , yRange ...
, 'LineWidth' , 2 ...
, 'Color' , MovieSlider.RANGE_BORDER ...
, 'ButtonDownFcn' , {@startDragBound, 2} ...
);
uistack(hBound, 'bottom');
uistack(hRange, 'bottom');
set(fig, 'Visible', 'on');
% Callbacks for GUI interaction
function redrawRange(index)
if ~ishghandle(fig)
return;
end
if nargin < 1
index = 1:numel(hBound);
end
[rngX, rngY] = rectangleCorners(obj.pixelRange(1), 0, obj.pixelRange(end)-obj.pixelRange(1), yRange(end));
set(hRange, 'XData', rngX, 'YData', rngY);
for iBound = index
set(hBound(iBound), 'XData', [1 1]*obj.pixelRange(iBound));
end
end
function setColors(handle, event, hEdit)
if ~ishghandle(fig)
return;
end
value = get(handle, 'String');
if nargin > 2
indices = get(handle, 'Value');
if isempty(indices)
return;
end
value = value{indices(1)};
set(hEdit, 'String', value);
end
obj.colors = value;
colormap(obj.axsMovie, value);
end
function slideContrast(handle, event)
if ~ishghandle(fig)
return;
end
obj.setContrast(round(get(handle, 'Value')));
set(editBound(1), 'String', sprintf('%.4g', obj.pixelRange(1)));
set(editBound(2), 'String', sprintf('%.4g', obj.pixelRange(end)));
redrawRange();
drawnow;
end
function editContrast(handle, event, index)
if ~ishghandle(fig)
return;
end
obj.pixelRange(index) = str2double(get(handle, 'String'));
obj.setContrast(obj.pixelRange);
redrawRange(index);
drawnow;
end
function dragBound(handle, event, index)
persistent beingCalled;
if isempty(beingCalled)
beingCalled = true;
else
return;
end
if ~ishghandle(fig)
return;
end
mousePos = get(axsPDF, 'CurrentPoint');
range = obj.pixelRange;
range(index) = mousePos(1);
if range(2) > range(1)
set(editBound(index), 'String', sprintf('%.4g', range(index)));
editContrast(editBound(index), event, index);
end
beingCalled = [];
end
function startDragBound(handle, event, index)
if ~ishghandle(fig)
return;
end
set(fig, 'WindowButtonMotionFcn', {@dragBound, index});
end
function mouseHoverHint(handle, event)
if ~ishghandle(fig)
return;
end
mousePos = get(axsPDF, 'CurrentPoint');
axsPos = get(axsPDF, 'Position');
dPix = axsPos(3) * abs(mousePos(1) - obj.pixelRange ) / (axsRange(end) - axsRange(1));
if any(dPix < 2)
set(handle, 'Pointer', 'left');
else
set(handle, 'Pointer', 'arrow');
end
end
function acceptConfig(handle, event)
obj.contrastIndex = get(sldContrast, 'Value');
obj.pixelRange = [ str2double(get(editBound(1), 'String')) ...
, str2double(get(editBound(2), 'String')) ...
];
obj.playbackFPS = str2double(get(edtFPS, 'String'));
if ishghandle(fig)
delete(fig);
end
fig = [];
end
function restoreConfig(handle, event)
for field = MovieSlider.CONFIGURABLES
obj.(field{:}) = original.(field{:});
end
obj.setContrast(obj.pixelRange);
colormap(obj.axsMovie, obj.colors);
drawnow;
if ishghandle(fig)
delete(fig);
end
fig = [];
end
function cloneSelf(handle, event)
restoreConfig();
obj.clone();
end
end
end
%________________________________________________________________________
methods (Static)
%----- Synchronize frame display of all given movie sliders
function synchronizeFrames(sliders, doSync)
if nargin < 2 || doSync
for iMov = 1:numel(sliders)
sliders(iMov).syncSiblings = sliders([1:iMov-1, iMov+1:end]);
end
else
for iMov = 1:numel(sliders)
sliders(iMov).syncSiblings = [];
end
end
end
%----- Load GUI icon
function icon = loadIcon(iconFile, background)
if nargin < 2
background = MovieSlider.GUI_COLOR;
end
[~, ~, alpha] = imread(fullfile(MovieSlider.CODE_PATH, iconFile));
alpha = imresize(double(alpha), [1 1] * MovieSlider.GUI_BTNSIZE, 'lanczos3');
alpha = alpha .* (alpha > 0) / max(alpha(:));
icon = bsxfun(@times, 1 - alpha, background);
end
%----- Get the list of associated objects, sans deleted items
function sliders = getAssociates(handle)
sliders = get(handle, 'UserData');
sliders(~ishghandle(sliders)) = [];
set(handle, 'UserData', sliders);
end
%----- Callback for pixel info display
function updatePixelInfo(handle, event)
movieSliders = MovieSlider.getAssociates(handle);
for iMov = 1:numel(movieSliders)
if ~strcmp(get(movieSliders(iMov).tmrPlayback, 'Running'), 'off')
continue;
end
mousePos = round(get(movieSliders(iMov).axsMovie, 'CurrentPoint'));
xRange = rangemin( get(movieSliders(iMov).axsMovie, 'XLim'), [1, size(movieSliders(iMov).movie,2)] );
yRange = rangemin( get(movieSliders(iMov).axsMovie, 'YLim'), [1, size(movieSliders(iMov).movie,1)] );
if mousePos(1,2) >= yRange(1) && mousePos(1,2) <= yRange(2) ...
&& mousePos(1,1) >= xRange(1) && mousePos(1,1) <= xRange(2)
set ( movieSliders(iMov).txtInfo ...
, 'String' , sprintf ( '(%d,%d,%d)=%.4g' ...
, mousePos(1,2), mousePos(1,1), movieSliders(iMov).currentFrame ...
, movieSliders(iMov).movie(mousePos(1,2), mousePos(1,1), movieSliders(iMov).currentFrame) ...
) ...
);
return;
end
end
end
%----- Callback to start the zoom action
function started = startZooming(handle, event)
movieSliders = MovieSlider.getAssociates(handle);
started = false;
for iMov = 1:numel(movieSliders)
clickType = get(movieSliders(iMov).figParent, 'SelectionType');
if ~strcmpi(clickType, 'alt')
continue;
end
mousePos = get(movieSliders(iMov).axsMovie, 'CurrentPoint');
xRange = get(movieSliders(iMov).axsMovie, 'XLim');
yRange = get(movieSliders(iMov).axsMovie, 'YLim');
if mousePos(1,2) >= yRange(1) && mousePos(1,2) <= yRange(2) ...
&& mousePos(1,1) >= xRange(1) && mousePos(1,1) <= xRange(2)
zoomExtended(movieSliders(iMov).figParent, movieSliders(iMov).axsMovie);
started = true;
return;
end
end
end
%----- Callback for keyboard control of frame display
function keyboardControl(handle, event)
movieSliders = MovieSlider.getAssociates(handle);
if isempty(movieSliders)
return;
end
selAxes = get(handle, 'CurrentAxes');
slider = movieSliders([movieSliders.axsMovie] == selAxes);
if isempty(slider)
return;
end
switch event.Key
case 'leftarrow'
slider.shiftFrame(-1);
case 'rightarrow'
slider.shiftFrame(+1);
case 'uparrow'
slider.shiftFrame(-1);
case 'downarrow'
slider.shiftFrame(+1);
case 'pageup'
slider.shiftFrame(-MovieSlider.DEFAULT_FPS);
case 'pagedown'
slider.shiftFrame(+MovieSlider.DEFAULT_FPS);
case 'home'
slider.setFrame(1);
case 'end'
slider.setFrame(size(slider.movie,3));
case 'return'
slider.togglePlayback(slider.btnPlay, event);
case 'space'
slider.togglePlayback(slider.btnPlay, event);
end
end
end
end