Home > vbmeg > external > bioelectromagnetism > avw_img_write.m

avw_img_write

PURPOSE ^

avw_img_write - write Analyze image files (*.img)

SYNOPSIS ^

function avw_img_write(avw, fileprefix, machine)

DESCRIPTION ^

 avw_img_write - write Analyze image files (*.img)
 
 avw_img_write(avw, fileprefix, [machine])
 
 avw.img    - a 3D matrix of image data (double precision).
 avw.hdr    - a struct with image data parameters.  If
              not empty, this function calls avw_hdr_write.
 
 fileprefix - a string, the filename without the .img
              extension. If empty, may use avw.fileprefix
 
 (Modified by M. Sato)
 Do not change orientation of image data written in the file
 
 machine - a string, see machineformat in fread for details.
           The default here is 'ieee-le'.
 
 Tip: to change the data type, set avw.hdr.dime.datatype to:
 
     1    Binary             (  1 bit  per voxel)
     2    Unsigned character (  8 bits per voxel)
     4    Signed short       ( 16 bits per voxel)
     8    Signed integer     ( 32 bits per voxel)
    16    Floating point     ( 32 bits per voxel)
    32    Complex, 2 floats  ( 64 bits per voxel), not supported
    64    Double precision   ( 64 bits per voxel)
   128    Red-Green-Blue     (128 bits per voxel), not supported
 
 See also: avw_write, avw_hdr_write,
           avw_read, avw_hdr_read, avw_img_read, avw_view

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function avw_img_write(avw, fileprefix, machine)
0002 
0003 % avw_img_write - write Analyze image files (*.img)
0004 %
0005 % avw_img_write(avw, fileprefix, [machine])
0006 %
0007 % avw.img    - a 3D matrix of image data (double precision).
0008 % avw.hdr    - a struct with image data parameters.  If
0009 %              not empty, this function calls avw_hdr_write.
0010 %
0011 % fileprefix - a string, the filename without the .img
0012 %              extension. If empty, may use avw.fileprefix
0013 %
0014 % (Modified by M. Sato)
0015 % Do not change orientation of image data written in the file
0016 %
0017 % machine - a string, see machineformat in fread for details.
0018 %           The default here is 'ieee-le'.
0019 %
0020 % Tip: to change the data type, set avw.hdr.dime.datatype to:
0021 %
0022 %     1    Binary             (  1 bit  per voxel)
0023 %     2    Unsigned character (  8 bits per voxel)
0024 %     4    Signed short       ( 16 bits per voxel)
0025 %     8    Signed integer     ( 32 bits per voxel)
0026 %    16    Floating point     ( 32 bits per voxel)
0027 %    32    Complex, 2 floats  ( 64 bits per voxel), not supported
0028 %    64    Double precision   ( 64 bits per voxel)
0029 %   128    Red-Green-Blue     (128 bits per voxel), not supported
0030 %
0031 % See also: avw_write, avw_hdr_write,
0032 %           avw_read, avw_hdr_read, avw_img_read, avw_view
0033 %
0034 
0035 % $Revision: 1332 $ $Date:: 2011-02-24 15:57:20 +0900#$
0036 
0037 % Licence:  GNU GPL, no express or implied warranties
0038 % History:  05/2002, Darren.Weber@flinders.edu.au
0039 %                    The Analyze format is copyright
0040 %                    (c) Copyright, 1986-1995
0041 %                    Biomedical Imaging Resource, Mayo Foundation
0042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0043 
0044 
0045 %------------------------------------------------------------------------
0046 % Check inputs
0047 
0048 if ~exist('avw','var'),
0049     doc avw_img_write;
0050     msg = sprintf('\n...no input avw.\n');
0051     error(msg);
0052 elseif isempty(avw),
0053     msg = sprintf('\n...empty input avw.\n');
0054     error(msg);
0055 elseif ~isfield(avw,'img'),
0056     msg = sprintf('\n...empty input avw.img\n');
0057     error(msg);
0058 end
0059 
0060 if ~exist('fileprefix','var'),
0061     if isfield(avw,'fileprefix'),
0062         if ~isempty(avw.fileprefix),
0063             fileprefix = avw.fileprefix;
0064         else
0065             fileprefix = '';
0066         end
0067     end
0068 end
0069 if isempty(fileprefix),
0070     doc avw_img_write;
0071     fprintf('\n...no input fileprefix - see help avw_img_write\n');
0072     return;
0073 end
0074 
0075 if ~exist('IMGorient','var'), IMGorient = ''; end
0076 if ~exist('machine','var'), machine = 'ieee-le'; end
0077 
0078 if findstr('.hdr',fileprefix),
0079 %    fprintf('AVW_IMG_WRITE: Removing .hdr extension from ''%s''\n',fileprefix);
0080     fileprefix = strrep(fileprefix,'.hdr','');
0081 end
0082 if findstr('.img',fileprefix),
0083 %    fprintf('AVW_IMG_WRITE: Removing .img extension from ''%s''\n',fileprefix);
0084     fileprefix = strrep(fileprefix,'.img','');
0085 end
0086 
0087 
0088 
0089 %------------------------------------------------------------------------
0090 % MAIN
0091 
0092 % Modified by M. Sato
0093 %version = '[$Revision: 1332 $]';
0094 %fprintf('\nAVW_IMG_WRITE [v%s]\n',version(12:16));  tic;
0095 
0096 fid = fopen(sprintf('%s.img',fileprefix),'w',machine);
0097 if fid < 0,
0098     msg = sprintf('Cannot open file %s.img\n',fileprefix);
0099     error(msg);
0100 else
0101     avw = write_image(fid,avw,fileprefix,IMGorient,machine);
0102 end
0103 
0104 %t=toc; fprintf('...done (%5.2f sec).\n\n',t);
0105 
0106 % MUST write header after the image, to ensure any
0107 % orientation changes during image write are saved
0108 % in the header
0109 avw_hdr_write(avw,fileprefix,machine);
0110 
0111 return
0112 
0113 
0114 
0115 
0116 %-----------------------------------------------------------------------------------
0117 
0118 function avw = write_image(fid,avw,fileprefix,IMGorient,machine)
0119 
0120 % short int bitpix;    /* Number of bits per pixel; 1, 8, 16, 32, or 64. */
0121 % short int datatype   /* Datatype for this image set */
0122 % /*Acceptable values for datatype are*/
0123 % #define DT_NONE             0
0124 % #define DT_UNKNOWN          0    /*Unknown data type*/
0125 % #define DT_BINARY           1    /*Binary             ( 1 bit per voxel)*/
0126 % #define DT_UNSIGNED_CHAR    2    /*Unsigned character ( 8 bits per voxel)*/
0127 % #define DT_SIGNED_SHORT     4    /*Signed short       (16 bits per voxel)*/
0128 % #define DT_SIGNED_INT       8    /*Signed integer     (32 bits per voxel)*/
0129 % #define DT_FLOAT           16    /*Floating point     (32 bits per voxel)*/
0130 % #define DT_COMPLEX         32    /*Complex,2 floats   (64 bits per voxel)/*
0131 % #define DT_DOUBLE          64    /*Double precision   (64 bits per voxel)*/
0132 % #define DT_RGB            128    /*A Red-Green-Blue datatype*/
0133 % #define DT_ALL            255    /*Undocumented*/
0134 
0135 switch double(avw.hdr.dime.datatype),
0136 case   1,
0137     avw.hdr.dime.bitpix = int16( 1); precision = 'bit1';
0138 case   2,
0139     avw.hdr.dime.bitpix = int16( 8); precision = 'uchar';
0140 case   4,
0141     avw.hdr.dime.bitpix = int16(16); precision = 'int16';
0142 case   8,
0143     avw.hdr.dime.bitpix = int16(32); precision = 'int32';
0144 case  16,
0145     avw.hdr.dime.bitpix = int16(32); precision = 'single';
0146 case  32,
0147     error('...complex datatype not yet supported.\n');
0148 case  64,
0149     avw.hdr.dime.bitpix = int16(64); precision = 'double';
0150 case 128,
0151     error('...RGB datatype not yet supported.\n');
0152 otherwise
0153     fprintf('...unknown datatype, using type 16 (32 bit floats).\n');
0154     avw.hdr.dime.datatype = int16(16);
0155     avw.hdr.dime.bitpix = int16(32); precision = 'single';
0156 end
0157 
0158 
0159 % write the .img file, depending on the .img orientation
0160 fprintf('...writing %s precision Analyze image (%s).\n',precision,machine);
0161 
0162 fseek(fid,0,'bof');
0163 
0164 % Modified by M. Sato
0165 
0166 fprintf('...writing axial unflipped\n');
0167     
0168 %    avw.hdr.hist.orient = uint8(0);
0169 %
0170 %    SliceDim = double(avw.hdr.dime.dim(4)); % z
0171 %    RowDim   = double(avw.hdr.dime.dim(3)); % y
0172 %    PixelDim = double(avw.hdr.dime.dim(2)); % x
0173 %    SliceSz  = double(avw.hdr.dime.pixdim(4));
0174 %    RowSz    = double(avw.hdr.dime.pixdim(3));
0175 %    PixelSz  = double(avw.hdr.dime.pixdim(2));
0176     
0177 % Modified by M. Sato
0178 
0179 fwrite(fid,avw.img,precision);
0180 
0181 %    x = 1:PixelDim;
0182 %    for z = 1:SliceDim,
0183 %        for y = 1:RowDim,
0184 %            fwrite(fid,avw.img(x,y,z),precision);
0185 %        end
0186 %    end
0187 
0188 fclose(fid);
0189 
0190 % Update the header
0191 %avw.hdr.dime.dim(2:4) = int16([PixelDim,RowDim,SliceDim]);
0192 %avw.hdr.dime.pixdim(2:4) = single([PixelSz,RowSz,SliceSz]);
0193 
0194 return

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005