forked from tsumpf/arrShow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrShow.m
4410 lines (3656 loc) · 176 KB
/
arrShow.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
% arrShow Image viewer.
% obj = arrShow(imageArray) displays the image in imageArray in an arrayShow GUI
% and returns an instance of the arrShow class. Most properties of the GUI
% (image contrast, cursor position, ROI ...) can also be created and controlled
% by object methods, e.g. obj.createRoi(roiPos); or obj.window.setCW([center, width]);
%
% Hint: don't call the arrShow constructor directly but use the function
% "as" instead. All arrayShow instances are hereby collected in a global
% workspace array "asObjs" which can be used e.g. for batch tasks.
%
% ------------------------------------------------------------------------
% 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 arrShow < handle
properties (Access = public)
data = []; % object containing the data and data operations
selection = []; % asSelectionClass object containing the valueChanger array
complexSelect = []; % cmplxChooser object
statistics = []; % image statistics object
cursor = []; % cursor position object
infotext = []; % info text object
window = []; % image windowing object
roi = []; % region of interest object
imageText = []; % image text object
markers = []; % pixel markers
sendGroup = []; % asSendGroupClass
UserData = []; % this is not used within this class
% and may be set and
% changed for arbitrary purpose
end
properties (Access = protected)
% debug messeges
% msg = @fprintf; % use fprintf for debugging
msg = @nop; % use nop as default
% icons
icons = []; % asIcon class
% main figure
fh = 0; % main figure handle
title = ''; % main figure title
figurePosition = []; % main figure position
userCallback = []; % callback is executed at the end of updFig,
% e.g. when the selected image or
% selected complex part has changed
linkedToWorkspaceArray = false; % is (automatically) set to true if input array is a variable in workspace
% (rather than e.g. a variable in the debugger, or the result of an operation
% as in e.g. "as(a + b)"). If linked to workspace array, this array can be reloaded
% and updated by clicking the according buttons within the arrShow main window.
workspaceArrayName = ''; % name of the input array in workspace (will be set automatically)
arrShowPath = ''; % root path of the arrShowClass
cMapStdPath = ''; % standard path for colormaps
fcmh = struct; % struct of figure context menu handle
fph = 0; % figure panel handle
bph = 0; % bottom panal handle
cph = 0 ; % control panel handle
cpcmh = struct; % control panel context menu handle
mbh = struct; % menu bar handle
tbh = struct; % tool bar handle
trph = 0; % top right panel handle
ih = []; % image handle
% (this is an array of N handles, if we display
% N images)
fp_height = 0; % the figure panel height is usually set to the constant
% variable FP_MAX_HEIGHT.
% However, for small screens it
% might have to be set to a
% smaller value
postProcFun = []; % postprocessing function handle
mouseMovementMode = 0; % behaviour on mouse movements:
% 0 : just update the cursorPositionClass
% 1 : mouse windowing mode
% 2 : dragging mode
mouseReferencePoint = []; % reference point for mouse windowing or dragging
buttonUpCbTime = uint64(0); % Workaround: if a mouse click changes the focus
% of different uiObjects, apparently the mouseUp callback is sometimes called prior the
% mouseDown callback. I guess the reason is, that the up-callback is called more or less
% directly, whereas the down-callback is called after the
% window manager has finished all its focus operations... However:
% The manual check of the callback time is a workaround for simetimes 'jammed'
% dragging operations.
mouse_wheel_zoom_factor = 1.5; % default zoom factor per mouse wheel step
processingCallback = false;
forceComplexRepresentation = true; % use phase overlay in complex mode even if imaginary part
% of the frame is zero at every point
relatives = []; % list of other arrShow objects in current environment
noRelatives = 0; % number of relatives
useGlobalArray= false; % if the "useGlobalArray" toggle
% is set to true, no individual relatives list is populated within this object and
% a global workspace array "asObjs" is used instead.
sendWdwSize = false; % send main figure window size to relatives
titleAsImageText = false; % draw the title as a text within the images
saveInfosAtImageExport = true; % if true, a description text file is created when exporting images (containing dinensions, norm, i.e.)
playAlongDim = false; % this is set to true if the play button has been pressed
framerate = 50; % Standard framerate for the play function.
% Note: the framerate setting
% currently does not consider
% the execution time of updFig and is thus not precise.
% The actual framerate can be assumed to
% be lower.
stdColormap = 'Gray(256)'; % standard colormap
phaseColormap = 'martin_phase(256)'; % standard colormap for phase display
stdCmapMightBeModified = false; % The matlab colormapeditor allows for altering the colormap
phaCmapMightBeModified = false; % even after the actual command 'colormapeditor' has already returned.
% The 'cmapMightBeModified' workaround causes
% arrayShow to retrieve the potentially modified colormap from the
% figure handle during updFig.
end
properties (Constant, GetAccess = private)
% image export presets
RESIZE_AXES_FOR_SCREENSHOTS = false;
% panel positions
TR_PANEL_POS = [5/6, 0, 1/6, 1]; % relative position and size of the complexSelector in the top Panel
STATISTICS_POS = [4/6, 0, 1/6, 1]; % relative position and size of the statistics in the top Panel
INFOTEXT_POS = [2/6, 0, 1/6, 1]; % ...
WINDOWING_POS = [3/6, 0, 1/6, 1];
% sizes
CP_HEIGHT = 2.2; % fixed height for the controlPanel (in centimeters)
BP_HEIGHT = .5; % fixed height for the bottom panel (in centimeters)
FP_MAX_HEIGHT = 18; % desired height for the figurePanel (in centimeters)
% (for small screens, the actual fp_height
% might be smaller)
% marker colors
MARKER_COL_PHA = 'white'; % default marker color for phase representations
MARKER_COL_REAL= 'yellow'; % default marker color for real valued images
end
properties (Access = private);
updFigCount = 0; % counter for updateFig calls for
% debugging and speed improvements
processingError = false; % if an error in the data is detected during
% updFig, the function can be restarted
% with new values and this flag set to
% true. The flag is a quick and dirty workaround
% to avoid endless loops if an error
% persists in a second updFig call
end
properties (Constant, GetAccess = public)
% arrShow version
VERSION = 0.35;
end
%#ok<*FPARK>
% Deactivate the warning telling me that I should use textscan instead
% of strread... I like strread. I'll change it if I feel like having
% too much time...
methods (Access = public)
function obj = arrShow(arr, varargin)
% evaluate varagin
CW = [];
userFigurePosition = [];
selectionOffset = [];
selectedImageStr = '';
pixMarkers = [];
imageTextVal = [];
initComplexSelect = [];
infoText = '';
renderUi = true;
viewMode = 'default'; % could be quiver (vector plot) as well
if nargin > 1
if length(varargin) ==1
obj.title = varargin{1};
if strcmp(obj.title,inputname(1))
obj.workspaceArrayName = inputname(1);
obj.linkedToWorkspaceArray = true;
end
end
for i = 1 : floor(length(varargin)/2)
option = varargin{i*2-1};
option_value = varargin{i*2};
switch lower(option)
case {'title','tit'}
obj.title = option_value;
case 'info'
infoText = option_value;
case {'imagetext', 'imgtxt'}
imageTextVal = option_value;
case {'windowing','window','wdw'}
CW = option_value;
case {'select','sel'}
selectedImageStr = option_value;
case {'complexselect','cplxselect','cplx','complex','cplxsel'}
initComplexSelect = option_value;
case {'colormap', 'stdcolormap'}
obj.stdColormap = option_value;
case 'phasecolormap'
obj.phaseColormap = option_value;
case {'position','pos'}
userFigurePosition = option_value;
case 'inputname'
if isempty(obj.title)
obj.title = option_value;
end
if ~isempty(option_value)
obj.workspaceArrayName = option_value;
obj.linkedToWorkspaceArray = true;
end
case {'callback','cb'}
obj.userCallback = option_value;
case 'useglobalarray'
obj.useGlobalArray = option_value;
case {'renderui','render'} % this option has been introduced
% to initialize an arrShow object without
% drawing the actual ui elements. This is
% currently used to create temporary object
% copies which are saveable in matlab >= 2014b
renderUi = option_value;
case {'offset','offs'} % offset to the asSelection class
selectionOffset = option_value;
case {'markers','marker','mark'} % pixel markers
pixMarkers = option_value;
case {'viewmode','view','mode'} % view mode can be 'default' or 'quiver' (or equivalent: 'vector')
viewMode = option_value;
otherwise
error('arrShow:varargin','unknown option [%s]!\n',option);
end;
end;
clear('option','option_value');
clear('varargin');
% If we don't explicitly delete it here,
% the varargin is stored within the
% object, wasting memory
end
% assure that all support function paths are registered
arrShow.checkPath();
% initialize the asData object (which also performs some
% initial data validity tests)
obj.data = asDataClass(arr, @obj.updFig);
si = size(obj.data.dat);
% store standard paths
obj.arrShowPath = fileparts(mfilename('fullpath'));
obj.cMapStdPath = [obj.arrShowPath, filesep, 'customColormaps'];
iconPath = [obj.arrShowPath, filesep, 'icons'];
% load icons
obj.icons = asIconClass.getInstance(iconPath);
% create main figure
fpos = obj.deriveFigurePos();
obj.fh = figure('Units','centimeters',...
'Resize','on',... % it's tempting to set this to off during initialization. However, this can cause problems with certain linux window managers
'Position',fpos,...
'KeyPressFcn',@(src,evnt)obj.keyPressCb(evnt),...
'CloseRequestFcn',@(src,evnt)obj.closeReq(src),...
'WindowButtonMotionFcn',@(src, evnt)obj.mouseMovementCb,...
'WindowButtonDownFcn',@obj.buttonDownCb,...
'WindowButtonUpFcn',@(src,evnt)obj.buttonUpCb(src),...
'WindowScrollWheelFcn',@obj.scollWheelCb,...
'MenuBar','none',...
'toolbar','none',...
'Visible','off',...
'Tag','arrShowFig',...
'IntegerHandle','on');
set(obj.fh,'UserData',obj) % link this object to main figure
% set title
if ~isempty(obj.title)
set(obj.fh,'Name',obj.title);
end
% change figure icon :-)
warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
jframe=get(obj.fh,'javaframe');
jIcon=javax.swing.ImageIcon(fullfile(iconPath,'figure.png'));
jframe.setFigureIcon(jIcon);
clear jframe jIcon
% init menu- and toolbar
obj.initMenuBar();
obj.initToolBar();
% shortcuts to some dimensions
fphe = obj.fp_height; % figure panel height
cphe = obj.CP_HEIGHT; % control panel (top panel) height
bphe = obj.BP_HEIGHT; % bottom panel height
% control panel
obj.cph = uipanel(obj.fh,'Units','centimeters',...
'Position',[0, fphe + bphe, fpos(3), cphe],...
'Interruptible','off',...
'BorderType','none',...
'BusyAction','cancel',...
'Tag','asControlPanel');
set(obj.cph,'Units','normalized');
% bottom panel
obj.bph = uipanel(obj.fh,'Units','centimeters',...
'Position',[0, 0, fpos(3), bphe],...
'Interruptible','off',...
'BusyAction','cancel' ,...
'BorderType','none',...
'Tag','asBottomPanel');
set(obj.bph,'Units','normalized');
% figure panel
obj.fph = uipanel(obj.fh,'Units','centimeters',...
'Position',[0, bphe, fpos(3), fphe],...
'Interruptible','off',...
'BorderType','beveledin',...
'BusyAction','cancel',...
'Tag','asFigurePanel');
set(obj.fph,'Units','normalized');
% image statistics object (min, max, l2 ...)
obj.statistics = asStatisticsClass(obj.cph, obj.STATISTICS_POS);
% image windowing object (the center and width slider i.e. contrast
% and brightness)
obj.window = asWindowingClass(...
obj.cph,...
obj.WINDOWING_POS,...
@obj.updFig,...
@obj.applyToRelatives,...
@()obj.getColormap('phase',true),...
obj.icons);
% info textbox object
obj.infotext = asInfoTextClass(obj.cph, obj.INFOTEXT_POS);
% top right panel
obj.trph = uipanel('visible','on','Units','normalized',...
'Position',obj.TR_PANEL_POS,'Parent',obj.cph,...
'Tag','trPanel');
% complex part selector (the dropdown menu on the top right of
% the arrayShow window)
obj.complexSelect = asCmplxChooserClass(...
obj.trph,...
@obj.updFig,...
@obj.applyToRelatives,...
obj.icons.send);
dataIsreal = isreal(obj.data.dat);
if dataIsreal
% disable the imag and phase button in the complexSelect
% object
obj.complexSelect.lockImagAndPhase;
% select real part per default
if isempty(initComplexSelect)
initComplexSelect = 'Re';
end
end
if ~isempty(initComplexSelect)
obj.complexSelect.setSelection(initComplexSelect, true);
end
% send group class
obj.sendGroup = asSendGroupClass(obj.trph);
% init the figure context menu (first entries are created
% within the asCursorPosClass)
obj.fcmh.base = uicontextmenu;
% cursor position object
obj.cursor = asCursorPosClass(...
obj.fh,...
obj.bph,...
obj.fcmh,...
obj.mbh,...
obj.icons,...
~dataIsreal,...
@obj.applyToRelatives,...
@obj.getCurrentAxesHandle,...
obj);
% valueChanger array (the +/- buttons on the top left of the
% arrayShow window)
initStrings = cell(length(si),1);
initStrings{1} = ':';
initStrings{2} = ':';
for i = 3 : length(si)
initStrings{i} = '1';
end
obj.selection = asSelectionClass(obj.cph, si,...
'figureUpdateCallback',@obj.updFig,...
'apply2allCb',@obj.applyToRelatives,...
'InitStrings',initStrings,...
'dataObject',obj.data,...
'offsets', selectionOffset,...
'sendIcon',obj.icons.send);
obj.data.linkToSelectionClassObject(obj.selection);
% init the control panel context menu and create additional
% entries in the previously created figure context menu...
obj.initContextMenus(infoText);
clear('infoText');
set(obj.fh,'HandleVisibility','off');
% ...we don't want other matlab routines to print stuff on our
% main figure
if ~isempty(selectedImageStr)
obj.selection.setValue(selectedImageStr,true,true,true);
end
% pixel markers
obj.markers = asMarkerClass(obj.selection, pixMarkers, obj.mbh.markers, @obj.applyToRelatives);
% if specific figure position is given, resize the gui
if ~isempty(userFigurePosition)
obj.setFigurePosition(userFigurePosition);
obj.fpResize(true); % manually call resize function
end
% find relatives
if ~obj.useGlobalArray
obj.refreshRelativesList();
% since this object is not yet in the cloud, add it manually to
% the list
obj.relatives = [obj.relatives,obj];
obj.noRelatives = obj.noRelatives + 1;
% (this assures that the send2all function does
% include this object, if the "includeSelf"-toggle is switched
% on)
end
% check, if we want to use a vector plot
if any(strcmp(viewMode,{'quiver','vector'}))
set(obj.mbh.quiver,'Checked','on');
end
% all gui components should be ready by now, so start
% updateFigure to find the selected array part and convert it
% to an image object in the axes region
if obj.updFigCount == 0 && renderUi
% for new figures, updFig sometimes seems to be triggered
% when setting the figure resize function...
% I haven't figured why and when exactly that happens.
% However, to speedup the start time, we don't call updFig again in
% this case.
obj.updFig;
end
% apply the initial window (center and width setting), if we
% got one as a constructor input argument
if ~isempty(CW)
obj.window.setCW(CW);
end
% select proper valueChanger object (VCO)
if length(si) > 2
obj.selection.selectVco(3);
end
% write the imagetext
if ~isempty(imageTextVal)
% deal with special case of a one-dimensional image
% text cell array and a 3 dimensional image array
if iscell(imageTextVal) && isvector(imageTextVal)
if length(size(obj.data.dat)) == 3
imageTextVal = reshape(imageTextVal,[1,1,length(imageTextVal)]);
end
end
obj.createImageText(imageTextVal);
if renderUi
obj.updFig
end
end
if ~renderUi
return;
end
% save figure position in the object property (pixel units) and activate
% figure resize function
set(obj.fh,'Visible', 'on');
drawnow; % apparently it's a good idea to draw the figure before
% activating the resize callback. Otherwise, the
% callback is sometimes triggered for no obvious
% reasons.
set(obj.fh,'Units', 'pixel',...
'ResizeFcn',@(src, evnt)obj.fpResize);
obj.figurePosition = get(obj.fh,'Position');
% put focus on complexSelector
% (this was written to put the focus away from the selection
% class at initialization of arrShow objects. As a result, key
% press calbacks can be evaluated without initial mouseclick on
% the figure window. Unfortunately, this command also seems to
% notably increases the startup time :-/ )
obj.complexSelect.focus;
end
function reloadWorkspaceArray(obj)
if obj.linkedToWorkspaceArray
% try to get the workspace array
try
WA = evalin('base',obj.workspaceArrayName);
catch err
if strcmp(err.identifier,'MATLAB:UndefinedFunction')
WA = [];
else
rethrow(err);
end
end
if isempty(WA)
fprintf('workspace variable ''%s'' seems not to be valid anymore\n',obj.workspaceArrayName);
else
obj.data.overwriteImageArray(WA);
end
else
disp('asObject is not linked to a workspace array');
end
end
function updateWorkspaceArray(obj)
if obj.linkedToWorkspaceArray
assignin('base',obj.workspaceArrayName,obj.data.dat);
end
end
function overwriteImageArray(obj, arr)
obj.data.overwriteImageArray(arr);
end
function refreshRelativesList(obj)
if obj.useGlobalArray
global asObjs %#ok<TLEV>
asObjs = arrShow.findAllObjects();
evalin('base','global asObjs');
else
obj.relatives = arrShow.findAllObjects();
obj.noRelatives = length(obj.relatives);
end
% if according options are set, send object properties to
% relatives
if obj.sendWdwSize
obj.sendFigureSize();
end
% obj.sendColormap(false);
if obj.complexSelect.sendToggleState
if obj.selection.sendToggleState
obj.applyToRelatives('complexSelect.setSelection',false,obj.complexSelect.getSelection(),true);
else
obj.applyToRelatives('complexSelect.setSelection',false,obj.complexSelect.getSelection(),false);
end
end
if obj.selection.sendToggleState
obj.selection.send;
end
if obj.window.sendAbsWindow
obj.window.sendAbsWindowToRelatives()
else
if obj.window.sendRelWindow
obj.window.sendRelWindowToRelatives();
end
end
if obj.roiExists()
if obj.roi.getSendPositionToggle();
obj.roi.callSendPositionCallback;
end
end
end
function wipeRelativesList(obj)
% obj.relatives = [];
% obj.noRelatives = 0;
% fprintf('deleted all relatives from list\n');
obj.msg('wipe call');
end
function sendColormap(obj, mapType)
% mapType can be either
% current, standard or phase
if nargin < 2
mapType = 'current';
end
obj.applyToRelatives('setColormap',false,obj.getColormap(mapType),mapType);
end
function sendAll(obj, bool)
% toggle send all (sendable) settings to the relatives
if nargin < 2
bool = true;
end
obj.selection.toggleSend(bool);
obj.window.toggleSendRelWindow(bool);
obj.window.toggleSendAbsWindow(bool);
obj.complexSelect.toggleSend2all(bool);
obj.cursor.toggleSend(bool);
% also send non toggleable options
if bool
obj.sendColormap();
obj.sendZoom();
end
end
function printCurrentImage(obj)
% this is a workaround to print an image without the uipanels
% create a help figure without menues
helpFigure = figure('MenuBar','none',...
'ToolBar','none');
colormap(obj.getColormap);
% copy current axes to the help figure
ah = obj.getCurrentAxesHandle;
helpAxes = copyobj(ah,helpFigure);
set(helpAxes,'Units','normalized','position',[0,0,1,1])
% delete cursor rectangle in helpFigure
rect = findobj(helpFigure,'type','rectangle');
delete(rect);
% print helpFigure
ph = printpreview(helpFigure);
% wait until print dialog is closed
while(ishandle(ph))
pause(0.1);
end
% ...and close the help figure
if ishandle(helpFigure)
close(helpFigure);
end
end
function batchExportDimension(obj, dim, filename, createMovie, framerate,...
screenshot, includePanels, includeCursor, scrshotPauseTime, movieType)
% export all 2D frames of dimension dim to either bitmap files
% or an avi file
% create bitmap series by default (rather than a movie file)
if nargin < 4 ||isempty(createMovie)
createMovie = false;
end
if nargin < 6 || isempty(screenshot)
screenshot = false;
end
if nargin < 7 || isempty(includePanels)
includePanels = false;
end
if nargin < 8 || isempty(includeCursor)
includeCursor = false;
end
if nargin < 9 || isempty(scrshotPauseTime)
scrshotPauseTime = 0;
end
if nargin < 10 || isempty(movieType)
movieType = 'Uncompressed AVI';
end
% use the image title as filename by default
if nargin < 3 || isempty(filename)
filename = obj.title;
if isempty(filename)
disp('Need a figure title to create a filename');
return;
end
end
% remove special characters from the filename
filename = arrShow.removeSpecialCharsFromString(filename);
% get data dimensions
dims = obj.selection.getDimensions;
noDims = length(dims);
% if no export dimension is give, open an input dialog
if nargin < 2 || isempty(dim)
dim = mydlg('Enter dimension','Enter dimension for batch export',num2str(noDims));
dim = str2double(dim);
if isnan(dim)
return
end
end
% check validity of the given export dimension
if noDims < dim
disp('invalid dimension number given');
return;
end
% if we want to create a movie file, initialize the VideoWriter
% object
if createMovie
vwObj = VideoWriter(filename, movieType);
if nargin < 5 || isempty(framerate)
framerate = mydlg('Enter framerate','Enter framerate for the movie','30');
framerate = str2double(framerate);
if isnan(framerate)
return
end
end
vwObj.FrameRate = framerate;
vwObj.open();
% check if we need to crop the frames: .avi-files require that
% the dimensions be divisible by four. Therefore, in
% exportCurrentImage, we make sure that they are. Since that
% function is called quite often and would therefore lead to
% warning spam, we issue the warning here
% instead of where we actually do the cropping.
exportSize = size(get(findobj( ...
obj.getCurrentAxesHandle(),'type','image'),'Cdata'));
rem = [mod(exportSize(1), 4) mod(exportSize(2), 4)];
if any(rem)
warning('arrShow:writeMovie',['image dimension not divisible by four, ',...
'which is required for .avi-export. Cropping image to be divisible by four...']);
end
end
% select the export dimension
obj.selection.selectVco(dim)
% store the current selection
origValue = obj.selection.getCurrentVcValue;
% loop through all frames in the export dimension
obj.selection.setCurrentVcValue(1);
for i = 1 : dims(dim);
if createMovie
obj.exportCurrentImage(vwObj,...
screenshot, includePanels, includeCursor, scrshotPauseTime);
else
obj.exportCurrentImage([filename,'_',num2str(i, '%05.5d'),'.png'],...
screenshot, includePanels, includeCursor, scrshotPauseTime);
end
obj.selection.increaseCurrentVc;
end
% reset selection
obj.selection.setCurrentVcValue(origValue);
% close videoWriter
if createMovie
vwObj.close();
end
disp('Done batchexport.');
end
function createMovie(obj, dim, framerate, movieType)
% shortcut to batchExportDimension with enabled movie export
if nargin < 4
movieType = 'Uncompressed AVI';
end
if nargin < 3
framerate = [];
end
if nargin < 2
dim = [];
end
obj.batchExportDimension(dim, [], true, framerate, ...
[],[],[],[],movieType);
end
function img = getScreenshot(obj, includePanels, includeCursor, scrshotPauseTime)
% use the matlab getframe routines to capture the current image
% with all its child-objects.
warning('arrShow:exportCurrentImage','output image might not have the exact original image''s resolution');
if nargin < 4
scrshotPauseTime = 0;
if nargin < 3
includeCursor = false;
if nargin < 2
includePanels = false;
end
end
end
ah = obj.getCurrentAxesHandle();
imh = findobj(ah,'type','image');
img = get(imh,'Cdata');
origUnits = get(ah,'units');
set(ah,'units','pixel');
origPos = get(ah,'position');
% show cursor rectangle?
if ~includeCursor
ud = get(ah,'UserData');
if ~isempty(ud) && isfield(ud,'rect') && ~isempty(ud.rect)
delete(ud.rect);
ud.rect = [];
set(ah,'UserData',ud);
end
end
if obj.RESIZE_AXES_FOR_SCREENSHOTS
si = size(img) -1;
set(ah,'position',[origPos(1:2),si(1:2)]);
end
% assure that current window is on top of all others
if scrshotPauseTime
figure(obj.fh);
drawnow;
pause(scrshotPauseTime);
end
if includePanels
img = getframe(obj.fh);
else
img = getframe(ah);
end
set(ah,'position',origPos);
set(ah,'units',origUnits);
end
function exportCurrentImage(obj, filenameOrVideoWriterObj, screenshot, includePanels, includeCursor, scrshotPauseTime)
% export data (original image or a screenshot containing
% arrayShow controls etc.) to either a bitmap file of a
% videoWriter object.
%
% filename can be either a string or a VideoWriter object.
if nargin < 6
scrshotPauseTime = 0;
if nargin < 5
includeCursor = false;
if nargin < 4
includePanels = false;
if nargin < 3
screenshot = false;
if nargin < 2
filenameOrVideoWriterObj = '';
end
end
end
end
end
if isempty(filenameOrVideoWriterObj)
% generate filename from title
filenameOrVideoWriterObj = arrShow.removeSpecialCharsFromString(obj.title);
[file,path] = uiputfile({'*.png';'*.bmp'},'Save image as', filenameOrVideoWriterObj);
if isnumeric(file)
return;
end
filenameOrVideoWriterObj = strcat(path, file);
if isempty(filenameOrVideoWriterObj)
warning('arrShow:exportCurrentImage','image export aborted, no filename given');
return;
end
if exist(filenameOrVideoWriterObj,'file');
fprintf('Fig. %d: overwriting existing file: %s\n',obj.getFigureNumber, filenameOrVideoWriterObj);
end
end
% define the method to write single frames
writeVideo = isa(filenameOrVideoWriterObj,'VideoWriter');
if writeVideo
vwObj = filenameOrVideoWriterObj;
writeFrame = @(dat, tmp1, tmp2)vwObj.writeVideo(dat);
else
writeFrame = @imwrite;
end
%img = obj.getSelectedImages();
ah = obj.getCurrentAxesHandle();
imh = findobj(ah,'type','image');
img = get(imh,'Cdata');
% .avi files need to have dimensions divisible by four
% so if we write to a movie, we should make sure that this is
% fulfilled. Therefore, we crop the image here to make sure
% that its size is divisible by four.
if writeVideo
rem = [mod(size(img, 1), 4) mod(size(img, 2), 4)];
if any(rem)
tmpstr = repmat({':'}, 1, ndims(img));
for currDim = 1:2
r = rem(currDim);
switch r
case 1
% Just leave out the last value
tmpstr{currDim} = 1:size(img,currDim)-1;
case 2
% Leave out first and last
tmpstr{currDim} = 2:size(img,currDim)-1;
case 3
% First and last two
tmpstr{currDim} = 2:size(img,currDim)-2;
end
end
% Crop image using the subscript vector
img = img(tmpstr{:});
end
end
if screenshot || includePanels
% use the matlab getframe routines to capture the current image
% with all its child-objects.
img = obj.getScreenshot(includePanels, includeCursor, scrshotPauseTime);
% write image to file
writeFrame(img.cdata,filenameOrVideoWriterObj);
else
if size(img,3) == 3
% cdata is already in RGB format, so just write it
% to file
[~, ~, extension] = fileparts(filenameOrVideoWriterObj);
if strcmp(extension, '.png')
%NaN values are rendered transparent
% get NaN positions
NaNpos = isnan(img);
% if one component is NaN, set it to transparent
NaNpos = double(~prod(NaNpos,3));
writeFrame(img,filenameOrVideoWriterObj,...
'alpha', NaNpos);
else
writeFrame(img,filenameOrVideoWriterObj);
end
else
% cdata represents intensity values while the
% visible representation is windowed and color coded
% using the colormap and CLim property of the axes object.
% In order to properly save the image we need to
% mimic the windowing of the axes object.
% get range limitations and center/width values
Clim = obj.window.getCLim();
CW = obj.window.getCW();