-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcropscreenshot.m
426 lines (378 loc) · 11.4 KB
/
cropscreenshot.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
function varargout = cropscreenshot(varargin)
% MATLAB code for cropscreenshot.fig
% disp(varargin{1})
macro_helper(varargin{:});
if nargin < 1 || isempty(varargin{1})
varargin{1} = 'init';
end
[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
%------------
function init %#ok<DEFNU>
%------------
%Initialize the GUI
global DATA
gui = mygui('cropscreenshot.fig');
DATA.GUI.CropScreenshot = gui;
gui.handles.screenshotimgh = image([],'Parent',gui.handles.screenshotaxes);
axis(gui.handles.screenshotaxes,'image','off');
gui.handles.screenshotaxes.Toolbar.Visible = 'off';
gui.handles.cropbox = line(gui.handles.screenshotaxes,nan, nan,'Color','y','ButtonDownFcn', ...
'cropscreenshot(''cropbox_Buttondown'')', 'LineWidth', 0.75);
gui.handles.framebox = line(gui.handles.screenshotaxes,nan, nan,'Color','k','ButtonDownFcn', ...
'cropscreenshot(''cropbox_Buttondown'')', 'LineWidth', 2);
gui.handles.resizeu = line(gui.handles.screenshotaxes,nan, nan,'Color','y','LineStyle','None','Marker','o','ButtonDownFcn', ...
'cropscreenshot(''cropbox_Buttondown'')','LineWidth', 0.75);
gui.handles.resizel = line(gui.handles.screenshotaxes,nan, nan,'Color','y','LineStyle','None','Marker','o','ButtonDownFcn', ...
'cropscreenshot(''cropbox_Buttondown'')','LineWidth', 0.75);
selectedbtn = gui.handles.selectcropregioncuibuttongroup.SelectedObject;
switch selectedbtn.Tag
case 'imagepanelradiobutton'
imagepanelradiobutton_Callback
case 'userselectionradiobutton'
setimage;
resetcropboundaries;
updatecropbox;
end
%-------------------------------------
function imagepanelradiobutton_Callback
%---------------------------------
global DATA
gui = DATA.GUI.CropScreenshot;
setimage;
gui.handles.resetpushbutton.Enable = 'off';
%-------------------------------------
function userselectionradiobutton_Callback
%---------------------------------
global DATA
gui = DATA.GUI.CropScreenshot;
try
gui.handles.resetpushbutton.Enable = 'on';
setimage;
updatecropbox;
catch me
mydispexception(me)
end
%-------------------------------------
function resetcropboundaries
%-------------------------------------
global DATA
gui = DATA.GUI.CropScreenshot;
[sizeX, sizeY,~] = size(gui.handles.screenshotimgh.CData);
gui.startx = 1;
gui.starty = 1;
gui.endx = sizeY;
gui.endy = sizeX;
%-------------------------------------
function resetframe
%-------------------------------------
global DATA
gui = DATA.GUI.CropScreenshot;
[sizeX, sizeY,~] = size(gui.handles.screenshotimgh.CData);
selectedbtn = gui.handles.selectcropregioncuibuttongroup.SelectedObject;
switch selectedbtn.Tag
case 'imagepanelradiobutton'
offset = 2;
case 'userselectionradiobutton'
offset = 5;
end
startx = 1-offset;
starty = 1-offset;
endx = sizeY+offset;
endy = sizeX+offset;
boxX = [startx,endx,endx,startx,startx];
boxY = [starty,starty,endy,endy,starty];
gui.handles.framebox.XData = boxX;
gui.handles.framebox.YData = boxY;
%-------------------------------------
function cropbox_Buttondown %#ok<DEFNU> called as a string from init
%---------------------------------
global DATA
gui = DATA.GUI.CropScreenshot;
[y,x] = mygetcurrentpoint(gui.handles.screenshotaxes);
%distance to startx/y
dstart = norm([gui.startx,gui.starty]-[y,x]);
dstop = norm([gui.endx,gui.endy]-[y,x]);
if dstop <= 25 && dstop < dstart
%scale box lower point
pointtoscale = 'scalel';
elseif dstart <= 25
%scale box upper point
pointtoscale = 'scaleu';
else
return
end
gui.fig.WindowButtonMotionFcn = sprintf('cropscreenshot(''cropbox_Motion'',''%s'')',pointtoscale);
gui.fig.WindowButtonUpFcn = 'cropscreenshot(''cropbox_Buttonup'')';
%-------------------------------------
function cropbox_Motion(type) %#ok<DEFNU> as string in cropbox_Buttondown
%---------------------------------
%motion function to move cropping box points.
global DATA
gui = DATA.GUI.CropScreenshot;
[x,y] = mygetcurrentpoint(gui.handles.screenshotaxes);
[sizeX, sizeY,~] = size(gui.handles.screenshotimgh.CData);
switch type
case 'scaleu'
startx = min(sizeY,max(1,x));
starty = min(sizeX,max(1,y));
endx = gui.endx;
endy = gui.endy;
case 'scalel'
startx = gui.startx;
starty = gui.starty;
endx = min(sizeY,max(1,x));
endy = min(sizeX,max(1,y));
end
%update params
gui.startx = startx;
gui.starty = starty;
gui.endx = endx;
gui.endy = endy;
updatecropbox
%-------------------------------------
function cropbox_Buttonup %#ok<DEFNU> as string in cropbox_Buttondown
%---------------------------------
%motion function for mid slice axes. Move box points.
global DATA
gui = DATA.GUI.CropScreenshot;
gui.fig.WindowButtonMotionFcn = '';
gui.fig.WindowButtonUpFcn = '';
%-------------------------------------
function updatecropbox
%---------------------------------
%updates cropbox
global DATA
gui = DATA.GUI.CropScreenshot;
boxX = [gui.startx,gui.endx,gui.endx,gui.startx,gui.startx];
boxY = [gui.starty,gui.starty,gui.endy,gui.endy,gui.starty];
gui.handles.cropbox.XData = boxX;
gui.handles.cropbox.YData = boxY;
gui.handles.resizeu.XData = gui.startx;
gui.handles.resizeu.YData = gui.starty;
gui.handles.resizel.XData = gui.endx;
gui.handles.resizel.YData = gui.endy;
%-------------------------------------
function reset_Callback %#ok<DEFNU> button callback
%---------------------------------
resetcropboundaries;
userselectionradiobutton_Callback;
%-------------------------------------
function close_Callback
%---------------------------------
global DATA
try
DATA.GUI.CropScreenshot = close(DATA.GUI.CropScreenshot);
catch me
mydispexception(me)%#ok<CTCH>
DATA.GUI.CropScreenshot =[];
delete(gcbf);
end
%-------------------------------------
function save_Callback %#ok<DEFNU> called from gui
%---------------------------------
[fname, extindex] = setfilename;
im = getimage;
successful = writeimage(im,fname,extindex);
if successful
close_Callback;
end
%-------------------------------------
function [fname, extindex] = setfilename
%-------------------------------------
global DATA SET
gui = DATA.GUI.CropScreenshot;
selectedbtn = gui.handles.savefileuibuttongroup.SelectedObject;
switch selectedbtn.Tag
case 'fileradiobutton'
[filename, pathname,extindex] = myuiputfile(...
{ '*.png','PNG image (*.png)';...
'*.jpg','JPEG image (*.jpg)';...
'*.bmp','BMP image (*.bmp)';...
'*.tif','TIFF image (*.tif)'},...
dprintf('Save file as'),'screenshot');
case 'pacsradiobutton'
filename = char(myinputdlg({'Enter File Name'},'File name',1,{'screenshot'}));
isnotok = 1;
while isnotok
if isempty(filename)
return;
elseif ~isvarname(filename)
myfailed(dprintf('File name %s is not valid.\nFile name can contain letters, digits, and underscores',filename),DATA.GUI.Segment);
isnotok = 1;
filename = char(myinputdlg({'Enter File Name'},'File name',1,{'screenshot'}));
elseif isvarname(filename)
break;
end
end
pathname = getpreferencespath;
extindex = 5;
case 'reportradiobutton'
name = removeforbiddenchars(SET(1).PatientInfo.Name);
if isempty(name)
name = 'Hidden';
end
pathname = fullfile(DATA.Pref.Pacs.ReportsheetPath, ...
name,'Screenshots');
if ~exist(pathname,'dir')
sucess = mkdir(pathname);
if ~sucess
myfailed('Could not create screenshot directory. Aborted');
return
end
end
reportdir = dir(fullfile(pathname,'screenshot*.png'));
nbrs = zeros(1,numel(reportdir));
for i = 1:numel(reportdir)
nbrs(i) = sscanf(reportdir(i).name(12:15),'%f');
end
newnbr = min(setdiff(1:9999,nbrs));
filename = sprintf('screenshot_%04.0f.png',newnbr);
extindex = 1;
if ~isfield(DATA.Pref.Pacs, 'ScreenshotPath')
DATA.Pref.Pacs.ScreenshotPath = pathname;
elseif ~strcmp(DATA.Pref.Pacs.ScreenshotPath, pathname)
DATA.Pref.Pacs.ScreenshotPath = pathname;
end
case 0
filename = 0;
end
fname = fullfile(pathname,filename);
%Add extension if necessary
[~,~,ext] = fileparts(fname);
if isempty(ext)
switch extindex
case 1
fname = [fname '.png'];
case 2
fname = [fname '.jpg'];
case 3
fname = [fname '.bmp'];
case 4
fname = [fname '.tif'];
case 5
fname = [fname '.dcm'];
end
end
%-------------------------------------
function successful = writeimage(im,fname,extindex)
%-------------------------------------
global DATA
successful = false;
switch extindex
case 1
try
imwrite(im,fname,'png','bitdepth',8,'software','Segment',...
'creationtime',datestr(now));
disp('Export successful');
successful = true;
catch me
mydispexception(me);
disp('Export failed');
end
case 2
try
imwrite(im,fname,'jpg','quality',100);
disp('Export successful');
successful = true;
catch me
mydispexception(me);
disp('Export failed');
end
case 3
try
imwrite(im,fname,'bmp');
successful = true;
disp('Export successful');
catch me
mydispexception(me)
disp('Export failed');
end
case 4
try
imwrite(im,fname,'tif');
successful = true;
disp('Export successful');
catch me
mydispexception(me);
disp('Export failed');
end
case 5
makeimagedicom(im,fname,DATA.ViewPanels(DATA.CurrentPanel));
try
successful = pacsaccess('savetopacs_helper',{fname},getpreferencespath);
catch me
successful = 0;
mydispexception(me);
myfailed(me.message);
end
try
status = 1;
delete(fname)
catch me
mydispexception(me)
status = 0;
end
if successful
stri = dprintf('Save to PACS success');
else
stri = dprintf('Save to PACS failed');
end
if status == 1
stri = [stri,dprintf(', succesfully removed report from computer.')];
else
stri = [stri,dprintf(', failed to remove report from computer.')];
end
mymsgbox(stri)
end
%---------------------------------
function img = getimage
%---------------------------------
global DATA
gui = DATA.GUI.CropScreenshot;
img = [];
selectedbtn = gui.handles.selectcropregioncuibuttongroup.SelectedObject;
switch selectedbtn.Tag
case 'imagepanelradiobutton'
try
img = gui.handles.screenshotimgh.CData;
catch me
mydispexception(me);
end
case 'userselectionradiobutton'
[sizeX, sizeY,~] = size(gui.handles.screenshotimgh.CData);
gui.startx = min(sizeY,max(1,round(gui.startx)));
gui.starty = min(sizeX,max(1,round(gui.starty)));
gui.endx = min(sizeY,max(1,round(gui.endx)));
gui.endy = min(sizeX,max(1,round(gui.endy)));
try
img = gui.handles.screenshotimgh.CData(gui.starty:gui.endy,gui.startx:gui.endx,:);
catch me
mydispexception(me);
end
end
%---------------------------------
function setimage
%---------------------------------
global DATA
gui = DATA.GUI.CropScreenshot;
selectedbtn = gui.handles.selectcropregioncuibuttongroup.SelectedObject;
try
switch selectedbtn.Tag
case 'imagepanelradiobutton'
h = DATA.Handles.boxaxes;
imgframe = mygetframe(h);
boxvis = 'off';
case 'userselectionradiobutton'
% get whole figure of segment as a frame
sgui = DATA.GUI.Segment;
imgframe = mygetframe(sgui.fig);
imgframe.colormap = colormap(sgui.fig);
boxvis = 'on';
end
img = frame2im(imgframe);
gui.handles.screenshotimgh.CData = img;
set([gui.handles.cropbox,gui.handles.resizeu,gui.handles.resizel],'Visible',boxvis);
resetframe;
catch me
mydispexception(me);
end