Skip to content

Commit 0544530

Browse files
authored
Merge pull request #3444 from mkitti/develop_matlab_bfGetPlaneAtZCT
MATLAB: Add function bfGetPlaneAtZCT.m and improve range checking for bfGetPlane
2 parents 02b3305 + 5e2382e commit 0544530

File tree

3 files changed

+122
-13
lines changed

3 files changed

+122
-13
lines changed

components/formats-gpl/matlab/bfGetPlane.m

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,20 @@
4949
ip.parse(r);
5050

5151
% Plane check
52-
isValidPlane = @(x) isscalar(x) && ismember(x, 1 : r.getImageCount());
52+
isValidPlane = @(p) bfTestInRange(p,'iPlane',r.getImageCount());
5353
% Optional tile arguments check
54-
isValidX = @(x) isscalar(x) && ismember(x, 1 : r.getSizeX());
55-
isValidY = @(x) isscalar(x) && ismember(x, 1 : r.getSizeY());
54+
isValidX = @(x) bfTestInRange(x,'x',r.getSizeX());
55+
isValidY = @(y) bfTestInRange(y,'y',r.getSizeY());
56+
isValidWidth = @(w) bfTestInRange(w,'width',r.getSizeX()-varargin{2}+1);
57+
isValidHeight = @(h) bfTestInRange(h,'height',r.getSizeY()-varargin{3}+1);
58+
5659
ip.addRequired('iPlane', isValidPlane);
5760
ip.addOptional('x', 1, isValidX);
5861
ip.addOptional('y', 1, isValidY);
59-
ip.addOptional('width', r.getSizeX(), isValidX);
60-
ip.addOptional('height', r.getSizeY(), isValidY);
62+
ip.addOptional('width', r.getSizeX(), isValidWidth);
63+
ip.addOptional('height', r.getSizeY(), isValidHeight);
6164
ip.parse(r, varargin{:});
6265

63-
% Additional check for tile size
64-
assert(ip.Results.x - 1 + ip.Results.width <= r.getSizeX(),...
65-
'MATLAB:InputParser:ArgumentFailedValidation',...
66-
'Invalid tile size');
67-
assert(ip.Results.y - 1 + ip.Results.height <= r.getSizeY(),...
68-
'MATLAB:InputParser:ArgumentFailedValidation',...
69-
'Invalid tile size');
70-
7166
% Get pixel type
7267
pixelType = r.getPixelType();
7368
bpp = javaMethod('getBytesPerPixel', 'loci.formats.FormatTools', pixelType);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function [I,idx] = bfGetPlaneAtZCT( r, z, c, t, varargin )
2+
%bfGetPlaneAtCZT Obtains an image plane for the Z, C, T
3+
%
4+
% I = bfGetPlaneAtZCT(r, z, c, t) returns a specified plane from the input
5+
% format reader. The indices specifying the plane to retrieve should be
6+
% contained between 1 and the number of planes for each dimesnion.
7+
%
8+
% I = bfGetPlaneAtZCT(r, z, c, t, ...) does as above but passes extra
9+
% arguments to bfGetPlane for tiling, etc.
10+
%
11+
% Examples
12+
%
13+
% r = bfGetReader('example.tif');
14+
% I = bfGetPlaneAtZCT(r, 1, 1, 1) % First plane of the series
15+
% I = bfGetPlaneAtZCT(r,r.getSizeZ(),r.getSizeC(),r.getSizeT()) % Last plane of the series
16+
% I = bfGetPlaneAtZCT(r, 1, 1, 1, 1, 1, 1, 20, 20) % 20x20 tile originated at (0, 0)
17+
%
18+
% See also: BFGETREADER, BFGETPLANE, loci.formats.IFormatReader.getIndex
19+
20+
% OME Bio-Formats package for reading and converting biological file formats.
21+
%
22+
% Copyright (C) 2012 - 2017 Open Microscopy Environment:
23+
% - Board of Regents of the University of Wisconsin-Madison
24+
% - Glencoe Software, Inc.
25+
% - University of Dundee
26+
%
27+
% This program is free software: you can redistribute it and/or modify
28+
% it under the terms of the GNU General Public License as
29+
% published by the Free Software Foundation, either version 2 of the
30+
% License, or (at your option) any later version.
31+
%
32+
% This program is distributed in the hope that it will be useful,
33+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
34+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35+
% GNU General Public License for more details.
36+
%
37+
% You should have received a copy of the GNU General Public License along
38+
% with this program; if not, write to the Free Software Foundation, Inc.,
39+
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
40+
41+
% Input check
42+
ip = inputParser;
43+
isValidReader = @(x) isa(x, 'loci.formats.IFormatReader') && ...
44+
~isempty(x.getCurrentFile());
45+
ip.addRequired('r', isValidReader);
46+
ip.parse(r);
47+
48+
ip = inputParser;
49+
% No validation because we already checked
50+
% Kept for positional errors
51+
ip.addRequired('r');
52+
53+
% Check ZCT coordinates are within range
54+
ip.addOptional('z',1,@(x) bfTestInRange(x,'z',r.getSizeZ()));
55+
ip.addOptional('c',1,@(x) bfTestInRange(x,'c',r.getSizeC()));
56+
ip.addOptional('t',1,@(x) bfTestInRange(x,'t',r.getSizeT()));
57+
58+
ip.parse(r, z, c, t);
59+
60+
javaIndex = r.getIndex(z-1, c-1, t-1);
61+
I = bfGetPlane(r, javaIndex+1);
62+
63+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function test = bfTestInRange(x,name,maxValue)
2+
%bfTestInRange A validation function that tests if the argument
3+
% is scalar integer between 1 and maxValue
4+
%
5+
% This should be faster than ismember(x, 1:maxValue) while also producing
6+
% more readable errors.
7+
8+
% OME Bio-Formats package for reading and converting biological file formats.
9+
%
10+
% Copyright (C) 2012 - 2017 Open Microscopy Environment:
11+
% - Board of Regents of the University of Wisconsin-Madison
12+
% - Glencoe Software, Inc.
13+
% - University of Dundee
14+
%
15+
% This program is free software: you can redistribute it and/or modify
16+
% it under the terms of the GNU General Public License as
17+
% published by the Free Software Foundation, either version 2 of the
18+
% License, or (at your option) any later version.
19+
%
20+
% This program is distributed in the hope that it will be useful,
21+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
% GNU General Public License for more details.
24+
%
25+
% You should have received a copy of the GNU General Public License along
26+
% with this program; if not, write to the Free Software Foundation, Inc.,
27+
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28+
29+
30+
% Check to see if x is a single value
31+
test = isscalar(x);
32+
if(~test)
33+
error('bfTestInRange:notScalar', ...
34+
[name ' value, [' num2str(x) '], is not scalar']);
35+
end
36+
37+
% Check to see if x is a whole number
38+
test = mod(x,1) == 0;
39+
if(~test)
40+
error('bfTestInRange:notAnInteger', ...
41+
[name ' value, ' num2str(x) ', is not an integer']);
42+
end
43+
44+
% Check to see if x is between 1 and maxValue
45+
test = x >= 1 && x <= maxValue;
46+
if(~test)
47+
error('bfTestInRange:notInSequence', ...
48+
[name ' value, ' num2str(x) ', is not between 1 and ', ...
49+
num2str(maxValue)]);
50+
end
51+
end

0 commit comments

Comments
 (0)