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

Add compression mode option in gtTIFVolWriter

git-svn-id: https://svn.code.sf.net/p/dct/code/trunk@861 4c865b51-4357-4376-afb4-474e03ccb993
parent f1297494
No related branches found
No related tags found
No related merge requests found
function gtTIFVolWriter(vol, filename, varargin)
% GTTIFVOLWRITER Writes volume data to TIFF file without compression.
% GTTIFVOLWRITER Writes volume data to TIFF file with compression modes.
% Either single file of multiple files stack can be produced.
% The produced TIFF files contain 8/16 bit unsigned integers.
% -------------------------------------------------------------------------
......@@ -19,10 +19,14 @@ function gtTIFVolWriter(vol, filename, varargin)
% 'yrange' = <range> " Y "
% 'zrange' = <range> " Z "
%
% 'type' = <string> Type of data written in the TIFF file
% it can be 'uint8' or 'uint16'
% 'type' = <string> Force type of data written in the TIFF file
% it can be 'uint8', 'uint16' or 'logical'
% default is 'uint8'
%
% 'compress' = <string> Compression mode, by default 'none', can be
% 'packbits', 'lzm', 'deflate', 'jpeg', 'ccitt',
% 'fax3', 'fax4'
%
% 'mode' = <string> Output type which is 'single' or 'stack'
% default is a single TIFF file
% 'digits' = <int> Number of digits in stack filenames
......@@ -39,10 +43,11 @@ function gtTIFVolWriter(vol, filename, varargin)
% Version 001 15-02-2012 by YGuilhem, yoann.guilhem@esrf.fr
% Set default parameters and parse optional arguments
params.type = 'uint8';
params.type = '';
params.xrange = 'all';
params.yrange = 'all';
params.zrange = 'all';
params.compress = 'none';
params.mode = 'single';
params.filext = 'tif';
params.digits = 4;
......@@ -50,18 +55,18 @@ params.startindex = 1;
params = parse_pv_pairs(params, varargin);
% Translating range parameters
if strcmp(params.xrange,'all')
rangeX = 1:size(vol,1);
if strcmp(params.xrange, 'all')
rangeX = 1:size(vol, 1);
else
rangeX = params.xrange;
end
if strcmp(params.yrange,'all')
rangeY = 1:size(vol,2);
if strcmp(params.yrange, 'all')
rangeY = 1:size(vol, 2);
else
rangeY = params.yrange;
end
if strcmp(params.zrange,'all')
rangeZ = 1:size(vol,3);
if strcmp(params.zrange, 'all')
rangeZ = 1:size(vol, 3);
else
rangeZ = params.zrange;
end
......@@ -69,22 +74,65 @@ outVolSizeX = length(rangeX);
outVolSizeY = length(rangeY);
outVolSizeZ = length(rangeZ);
if ~isa(vol,params.type)
% Creating grayscale integer volume
grayVol = mat2gray(vol);
% Checking input/output types and decide whther casting/rescaling operations
doCast = false;
rescale = 0;
inputType = class(vol);
if ~isempty(params.type) && ~any(strcmp(params.type, {'logical', 'uint8', 'uint16'}))
Mexc = MException('TIFF:wrong_type', ['Output type ''' params.type ''' is not supported!']);
throw(Mexc);
elseif any(strcmp(inputType, {'logical', 'uint8', 'uint16'}))
if isempty(params.type)
params.type = class(vol);
elseif strcmp(params.type, inputType)
%true;
doCast = false;
elseif strcmp(params.type, 'uint8')
doCast = true;
rescale = 255;
else
doCast = true;
end
else
cast = true;
switch params.type
case 'uint8'
outVol = uint8(grayVol*255);
case 'uint16'
outVol = uint16(grayVol*65535);
otherwise
Mexc = MException('TIFF:wrong_type', ['Output type ''' params.type ''' is not supported!']);
throw(Mexc);
case 'logical'
%true;
rescale = 0;
case 'uint8'
rescale = 255;
otherwise
rescale = 65535;
end
end
% Cast and rescale if needed
if doCast
if rescale
disp(['Rescaling input volume to [0 ' str2num(rescale) '] and casting it to ' params.type ' ...']);
outVol = cast(mat2gray(vol) * rescale, params.type);
else
disp(['Casting input volume to ' params.type ' ...']);
outVol = cast(vol, params.type);
end
else
outVol = vol;
end
% Check compression mode
if isempty(params.compress)
params.compress = 'none';
elseif any(strcmp(params.compress, {'true', 'yes', 'y'}))
if islogical(outVol)
params.compress = 'ccitt';
else
params.compress = 'packbits';
end
elseif ~any(strcmp(params.compress, {'none', 'packbits', 'lzm', 'deflate', 'jpeg', 'ccitt', 'fax3', 'fax4'}))
warning(['Unkown compression mode: ' params.compress ' -> Setting it to ''none''...']);
params.compress = 'none';
end
% Guessing output directory, filename and extension
[fdir, fname, fext] = fileparts(filename);
......@@ -102,14 +150,14 @@ if strcmpi(params.mode, 'single')
% 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');
imwrite(outVol(rangeX(1):rangeX(end), rangeY(1):rangeY(end), rangeZ(i))', filename, 'tif', 'Compression', params.compress, 'writemode', 'append');
end
elseif strcmpi(params.mode, 'stack')
% Check stack starting index
if isnumeric(params.startindex)
if mod(params.startindex,1) ~= 0
if mod(params.startindex, 1) ~= 0
params.startindex = round(params.startindex);
disp(['Argument ''startindex'' convert to ' num2str(params.startindex)]);
end
......@@ -120,7 +168,7 @@ elseif strcmpi(params.mode, 'stack')
% Check stack number of digits
if isnumeric(params.digits)
if mod(params.digits,1) ~= 0
if mod(params.digits, 1) ~= 0
params.digits = round(params.digits);
disp(['Argument ''digits'' convert to ' num2str(params.digits)]);
end
......@@ -144,7 +192,7 @@ elseif strcmpi(params.mode, 'stack')
% 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');
imwrite(outVol(rangeX(1):rangeX(end), rangeY(1):rangeY(end), rangeZ(i))', outFile, 'tif', 'Compression', params.compress, 'writemode', 'overwrite');
end
end
......
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