Skip to content
Snippets Groups Projects
Commit 967cd952 authored by Yoann Guilhem's avatar Yoann Guilhem Committed by Nicola Vigano
Browse files

Add the options to output a stack of multiple TIFF files

git-svn-id: https://svn.code.sf.net/p/dct/code/trunk@666 4c865b51-4357-4376-afb4-474e03ccb993
parent a57c8b38
No related branches found
No related tags found
No related merge requests found
function gtTIFVolWriter(vol, filename, varargin) function gtTIFVolWriter(vol, filename, varargin)
% GTTIFVOLWRITER Writes volume data to TIFF file without compression. % GTTIFVOLWRITER Writes volume data to TIFF file without compression.
% Either single file of multiple files stack can be produced.
% The produced TIFF files contain 8/16 bit unsigned integers. % The produced TIFF files contain 8/16 bit unsigned integers.
% ------------------------------------------------------------------------- % -------------------------------------------------------------------------
% gtTIFVolWriter(vol, filename, varargin) % gtTIFVolWriter(vol, filename, varargin)
% %
% INPUT: % INPUT:
% vol = <int> 3D volume data % vol = <int> 3D volume data
% <float> % <float>
% filename = <string> path to TIFF file to write % filename = <string> path to TIFF file to write
% the path may be relative or absolute % The path may be relative or absolute.
% if extension is missing, '.tif' will be append % If extension is missing, '.tif' will be append.
% In the case of stack output, this will be the
% stack filename prefix to which the slice number
% and the '.tif' extension will be append.
% %
% OPTIONAL INPUT (varargin as a list of pairs, see parse_pv_pairs.m): % OPTIONAL INPUT (varargin as a list of pairs, see parse_pv_pairs.m):
% type = <string> type of data written in the TIFF file % xrange = <range> index range of output in X direction
% it can be 'uint8' or 'uint16' % yrange = <range> index range of output in Y direction
% by default it is 'uint8' % zrange = <range> index range of output in Z direction
% xrange = <range> index range of output in X direction %
% yrange = <range> index range of output in Y direction % type = <string> type of data written in the TIFF file
% zrange = <range> index range of output in Z direction % it can be 'uint8' or 'uint16'
% by default it is 'uint8'
%
% output = <string> output type which is either 'single' or 'stack'
% default output type is a single TIFF file
% digits = <int> number of digits in stack filenames
% startindex = <int>
% %
% TODO : % TODO :
% - RGB data output % - RGB data output
% %
% Version 002 19-07-2012 by YGuilhem
% Add the options to output the volume as a stack of multiple TIFF
% files.
%
% Version 001 15-02-2012 by YGuilhem % Version 001 15-02-2012 by YGuilhem
% Set default parameters and parse optional arguments % Set default parameters and parse optional arguments
...@@ -29,19 +43,11 @@ params.type = 'uint8'; ...@@ -29,19 +43,11 @@ params.type = 'uint8';
params.xrange = 'all'; params.xrange = 'all';
params.yrange = 'all'; params.yrange = 'all';
params.zrange = 'all'; params.zrange = 'all';
params.output = 'single';
params.digits = 4;
params.startindex = 1;
params = parse_pv_pairs(params, varargin); params = parse_pv_pairs(params, varargin);
% Setting output filename
[~, ~, fext] = fileparts(filename);
if ~length(fext)
filename = [filename '.tif'];
end
% Check existence of output file to delete it before writing
if exist(filename,'file')
delete(filename);
end
% Translating range parameters % Translating range parameters
if strcmp(params.xrange,'all') if strcmp(params.xrange,'all')
rangeX = 1:size(vol,1); rangeX = 1:size(vol,1);
...@@ -71,16 +77,75 @@ if ~isa(vol,params.type) ...@@ -71,16 +77,75 @@ if ~isa(vol,params.type)
case 'uint16' case 'uint16'
outVol = uint16(grayVol*65535); outVol = uint16(grayVol*65535);
otherwise otherwise
Mexc = MException('TIFF:wrong_type', ['Out ouput type ''' type ''' is not supported!']); Mexc = MException('TIFF:wrong_type', ['Output type ''' type ''' is not supported!']);
throw(Mexc); throw(Mexc);
end end
else else
outVol = vol; outVol = vol;
end end
% Writing volume in TIFF file % Guessing output directory, filename and extension
for i=1:length(rangeZ) [fdir, fname, fext] = fileparts(filename);
imwrite(outVol(rangeX(1):rangeX(end), rangeY(1):rangeY(end),rangeZ(i))', filename, 'tif', 'Compression', 'none', 'writemode', 'append');
if strcmpi(params.output, 'single')
% Setting output filename
if ~length(fext)
filename = fullfile(fdir, [fname '.tif']);
end
% Check existence of output file to delete it before writing
if exist(filename, 'file')
delete(filename);
end
% Writing volume in TIFF file
for i=1:length(rangeZ)
imwrite(outVol(rangeX(1):rangeX(end), rangeY(1):rangeY(end),rangeZ(i))', filename, 'tif', 'Compression', 'none', 'writemode', 'append');
end
elseif strcmpi(params.output, 'stack')
% Check stack starting index
if isnumeric(params.startindex)
if mod(params.startindex,1) ~= 0
params.startindex = round(params.startindex);
disp(['Argument ''startindex'' convert to ' num2str(params.startindex)]);
end
else
Mexc = MException('INPUT:wrong_type', ['The argument ''startindex'' should be an integer!']);
throw(Mexc);
end
% Check stack number of digits
if isnumeric(params.digits)
if mod(params.digits,1) ~= 0
params.digits = round(params.digits);
disp(['Argument ''digits'' convert to ' num2str(params.digits)]);
end
else
Mexc = MException('INPUT:wrong_type', ['The argument ''digits'' should be an integer!']);
throw(Mexc);
end
% Create directory
if and(length(fdir), ~isdir(fdir))
mkdir(fdir);
end
% Output file output format
fprefix = fullfile(fdir, fname);
filenameFormat = sprintf('%s%%0%dd.tif', fprefix, params.digits);
% Set output index offset
offset = params.startindex - 1;
% Writing volume in TIFF file
for i=1:length(rangeZ)
outFile = sprintf(filenameFormat, i+offset);
imwrite(outVol(rangeX(1):rangeX(end), rangeY(1):rangeY(end),rangeZ(i))', outFile, 'tif', 'Compression', 'none');
end
end end
end % end of function end % end of function
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment