1 ## Copyright (C) 2007 P. Cloetens
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ## function im=edfread(filename,varargin)
19 ## reads an image in edf format into the matrix im
20 ## uses pmedf_read by Petr Mikulik if only filename is given
21 ## if both header and image are needed, use [hd,im]=pmedf_read(filename)
22 ## does not transpose the image, 1st dimension remains 1st dimension of original file
25 ## argument 1: filename
26 ## argument 2 (optional): vector of adjacent rows to be read
27 ## argument 3 (optional): vector of adjacent colums to be read
28 ## argument 4 (optional): vector of layers to be read
31 ## im = edfread('dark.edf');
32 ## reads complete image
33 ## im = edfread('dark.edf',10:200,30:50);
34 ## reads subimage corresponding to row (Dim_1) 10 until 200 and column (Dim_2) 30 until 50
35 ## im = edfread('dark.edf',10:200,30:50,[1 4 5]);
36 ## reads layers 1, 4 and 5 of a 3-dimensional image (see Dim_3 and W. Ludwig)
38 ## See also: edfwrite, pmedf_read, pmedf_write
40 ## Author: P. Cloetens <cloetens@esrf.fr>
42 ## 2007-03-03 P. Cloetens <cloetens@esrf.fr>
45 function im=edfread(filename,varargin)
51 usepm = isoctave&exist('pmedf_read');
54 columns = varargin{2};
57 columns = varargin{2};
62 [hd,im] = pmedf_read(filename);
64 fid=fopen(filename,'r');
68 disp(sprintf('!!! Error opening file %s !!!',filename))
70 hd=readedfheader(fid);
72 byteorder=findheader(hd,'ByteOrder','string');
73 fidpos=ftell(fid); % store present position in file
75 if strcmp(byteorder,'HighByteFirst')
80 fid=fopen(filename,'r',byteorder); % re-open with good format
81 fseek(fid,fidpos,0); % re-position at end of header
83 xsize=findheader(hd,'Dim_1','integer');
84 ysize=findheader(hd,'Dim_2','integer');
85 zsize=findheader(hd,'Dim_3','integer');
90 datatype=findheader(hd,'DataType','string');
98 case {'UnsignedInteger','UnsignedLong'}
101 case {'Float','FloatValue','FLOATVALUE','Real'}
110 if isempty(who('rows'))
113 if isempty(who('columns'))
116 if isempty(who('layers'))
121 fseek(fid,nbytes*(rows(1)-1+(columns(1)-1)*xsize),0);
122 im=fread(fid,[length(rows),length(columns)],sprintf('%d*%s',length(rows),datatype),nbytes*(xsize-length(rows)));
126 fseek(fid,1024+nbytes*(rows(1)-1+(columns(1)-1)*xsize+xsize*ysize*(i-1)),-1);
127 im(:,:,j)=fread(fid,[length(rows),length(columns)],sprintf('%d*%s',length(rows),datatype),nbytes*(xsize-length(rows)));