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

Bugfixes and cleaning for TIFF IO functions

git-svn-id: https://svn.code.sf.net/p/dct/code/trunk@944 4c865b51-4357-4376-afb4-474e03ccb993
parent 25b9f7eb
No related branches found
No related tags found
No related merge requests found
......@@ -59,8 +59,8 @@ if strcmp(params.mode, 'single')
elseif strcmp(params.mode, 'stack')
params.singlemode = false;
else
Mexc = MException('TIFF:wrong_input_mode', ['Unkown input mode: ''' params.mode '''']);
throw(Mexc);
gtError('gtTIFVolReader:wrong_input_mode', ...
['Unkown input mode: ''' params.mode '''']);
end
if ~exist('filename', 'var')
......@@ -68,8 +68,8 @@ if ~exist('filename', 'var')
[fname, fpath] = uigetfile('*', 'Select a .tif volume file');
filename = fullfile(fpath, fname);
else
Mexc = MException('TIFF:missing_input_filename', 'The name of the input file is missing!');
throw(Mexc);
gtError('gtTIFVolReader:missing_input_filename', ...
'The name of the input file is missing!');
end
end
......@@ -85,20 +85,20 @@ if params.singlemode
fullFilenameFormat = fullfile(fpath, filenameFormat);
% Check existence of file
if ~exist(fullFilenameFormat)
Mexc = MException('TIFF:wrong_file_name', ['File ''' fullFilenameFormat ''' does not exist!']);
throw(Mexc);
if ~exist(fullFilenameFormat, 'file')
gtError('gtTIFVolReader:wrong_file_name', ...
['File ''' fullFilenameFormat ''' does not exist!']);
end
else
filenameFormat = sprintf('%s%%0%dd.%s', fname, params.digits, params.filext);
fullFilenameFormat = fullfile(fpath, filenameFormat);
% Check existence of input stack files
for i=params.startindex:params.endindex
sfilename = sprintf(filenameFormat, i);
if ~exist(sfilename)
Mexc = MException('TIFF:wrong_file_name', ['File ''' sfilename ''' does not exist!']);
throw(Mexc);
for idx=params.startindex:params.endindex
sfilename = sprintf(filenameFormat, idx);
if ~exist(sfilename, 'file')
gtError('gtTIFVolReader:wrong_file_name', ...
['File ''' sfilename ''' does not exist!']);
end
end
end
......@@ -108,11 +108,18 @@ infoFilename = sprintf(fullFilenameFormat, params.startindex);
try
tifInfo = imfinfo(infoFilename);
catch Mexc
Mexc1 = MException('TIFF:wrong_input', ['Unable to read file ''' infoFilename '''']);
Mexc1 = MException('gtTIFVolReader:wrong_input', ...
['Unable to read file ''' infoFilename '''']);
Mexc1 = addCause(Mexc1, Mexc);
throw(Mexc1);
end
% Cannot read RGB TIFF files right now
if length(tifInfo(1).BitsPerSample) > 1
gtError('gtTIFVolReader:unsupported_input', ...
'Cannot handle RGB TIFF files yet!');
end
fullFilename = tifInfo(1).Filename;
if params.singlemode
fileIndex = ones(1, numel(tifInfo));
......@@ -125,7 +132,7 @@ tifSizeY = tifInfo(1).Height;
tifSizeZ = length(fileIndex);
% Update file parts and full file format
[fpath, fname, fext] = fileparts(fullFilename);
[fpath, fname, ~] = fileparts(fullFilename);
fullFilenameFormat = fullfile(fpath, filenameFormat);
% Check each dimension range
......@@ -189,14 +196,15 @@ info.BitDepth = tifInfo(1).BitDepth;
info.RowsPerStrip = tifInfo(1).RowsPerStrip;
% Set slice container
if ~length(ftype)
if isempty(ftype)
vol = zeros(volSizeX, volSizeY, volSizeZ);
tmp_slice = zeros(volSizeX, volSizeY);
elseif strcmp(ftype, 'logical')
vol = false(volSizeX, volSizeY, volSizeZ);
slice = false(volSizeX, volSizeY);
tmp_slice = false(volSizeX, volSizeY);
else
vol = zeros(volSizeX, volSizeY, volSizeZ, ftype);
slice = zeros(volSizeX, volSizeY, ftype);
tmp_slice = zeros(volSizeX, volSizeY, ftype);
end
% If new reading method and PhotometricInterpretation set to WhiteIsZero
......@@ -212,50 +220,50 @@ if params.newMethod
inputFilename = sprintf(fullFilenameFormat);
if params.singlemode
fid = tifflib('open', inputFilename, 'r');
for i=1:volSizeZ
for idx=1:volSizeZ
if fullRangeXY
vol(:, :, i) = sfReadTIFSlice(fid, slice, rangeZ(i), info.RowsPerStrip, ftype);
vol(:, :, idx) = sfReadTIFSlice(fid, tmp_slice, rangeZ(idx), info.RowsPerStrip);
else
vol(:, :, i) = sfReadTIFSliceROI(fid, slice, rangeZ(i), info.RowsPerStrip, ftype);
vol(:, :, idx) = sfReadTIFSliceROI(fid, tmp_slice, rangeZ(idx), info.RowsPerStrip);
end
end
tifflib('close', fid);
else
for i=1:volSizeZ
inputFilename = sprintf(fullFilenameFormat, fileIndex(i));
for idx=1:volSizeZ
inputFilename = sprintf(fullFilenameFormat, fileIndex(idx));
fid = tifflib('open', inputFilename, 'r');
if fullRangeXY
vol(:, :, i) = sfReadTIFSlice(fid, slice, 1, info.RowsPerStrip, ftype);
vol(:, :, idx) = sfReadTIFSlice(fid, tmp_slice, 1, info.RowsPerStrip);
else
vol(:, :, i) = sfReadTIFSliceROI(fid, slice, 1, info.RowsPerStrip, ftype);
vol(:, :, idx) = sfReadTIFSliceROI(fid, tmp_slice, 1, info.RowsPerStrip);
end
tifflib('close', fid);
end
end
else
for i=1:volSizeZ
inputFilename = sprintf(fullFilenameFormat, fileIndex(i));
for idx=1:volSizeZ
inputFilename = sprintf(fullFilenameFormat, fileIndex(idx));
if fullRangeXY
vol(:, :, i) = imread(inputFilename, rangeZ(i), 'Info', tifInfo)';
vol(:, :, idx) = imread(inputFilename, rangeZ(idx), 'Info', tifInfo)';
else
vol(:, :, i) = imread(inputFilename, rangeZ(i), 'PixelRegion', {[rangeY(1) rangeY(end)], [rangeX(1) rangeX(end)]})';
vol(:, :, idx) = imread(inputFilename, rangeZ(idx), 'PixelRegion', {[rangeY(1) rangeY(end)], [rangeX(1) rangeX(end)]})';
% This second method should be faster but in fact...
%vol(:, :, i) = imread(inputFilename, rangeZ(i), 'Info', info, 'PixelRegion', {[rangeY(1) rangeY(end)], [rangeX(1) rangeX(end)]})';
end
end
end
function slice = sfReadTIFSlice(fid, slice, sliceIndex, rowPerStrip, ftype)
function x = sfReadTIFSlice(fid, x, sliceIndex, rowPerStrip)
tifflib('setDirectory', fid, sliceIndex);
rowPerStrip = min(rowPerStrip, volSizeY);
for irow = 1:rowPerStrip:volSizeY
indY = irow:min(volSizeY, irow+rowPerStrip-1);
stripNum = tifflib('computeStrip', fid, irow);
slice(:, indY) = tifflib('readEncodedStrip', fid, stripNum)';
x(:, indY) = tifflib('readEncodedStrip', fid, stripNum)';
end
end
function slice = sfReadTIFSliceROI(fid, slice, sliceIndex, rowPerStrip, ftype)
function x = sfReadTIFSliceROI(fid, x, sliceIndex, rowPerStrip)
tifflib('setDirectory', fid, sliceIndex);
iy = 1;
while iy <= volSizeY
......@@ -268,7 +276,7 @@ function slice = sfReadTIFSliceROI(fid, slice, sliceIndex, rowPerStrip, ftype)
stripIndicesY = mod(rowStart, rowPerStrip):(mod(rowStart, rowPerStrip) + nRows);
volIndicesY = iy:(iy + nRows);
slice(:, volIndicesY) = tifStrip(rangeX, stripIndicesY);
x(:, volIndicesY) = tifStrip(rangeX, stripIndicesY);
iy = iy + nRows + 1;
end
end
......
......@@ -85,15 +85,14 @@ else
end
outVolSizeX = length(rangeX);
outVolSizeY = length(rangeY);
outVolSizeZ = length(rangeZ);
% Checking input/output types and decide whther casting/rescaling operations
% Checking input/output types and decide whether casting/rescaling operations
doCast = false;
rescale = 0;
inputType = class(vol);
if ~isempty(params.type) && ~any(strcmp(params.type, {'logical', 'uint8', 'uint16'}))
gtError('gtTIFVolWriter:wrong_type', ['Output type ''' params.type ''' is not supported!']);
throw(Mexc);
gtError('gtTIFVolWriter:wrong_output_type', ...
['Output type ''' params.type ''' is not supported!']);
elseif any(strcmp(inputType, {'logical', 'uint8', 'uint16'}))
if isempty(params.type)
params.type = class(vol);
......@@ -120,7 +119,8 @@ end
% Cast and rescale if needed
if doCast
if rescale
disp(['Rescaling input volume to [0 ' num2str(rescale) '] and casting it to ' params.type ' ...']);
disp(['Rescaling input volume to [0 ' num2str(rescale) ...
'] and casting it to ' params.type ' ...']);
outVol = cast(mat2gray(vol) * rescale, params.type);
else
disp(['Casting input volume to ' params.type ' ...']);
......@@ -157,11 +157,15 @@ elseif (islogical(params.compress) && params.compress)
end
elseif any(strcmp(params.compress, {'ccitt', 'fax3', 'fax4'}))
if ~islogical(outVol)
gtError('gtTIFVolWriter:wrong_compression', ...
['Compression mode ''' params.compress ''' is only available for logical volumes!']);
gtError('gtTIFVolWriter:wrong_compression_mode', ...
['Compression mode ''' params.compress ...
''' is only available for logical volumes!']);
end
elseif ~any(strcmp(params.compress, {'none','packbits', 'deflate', 'lzw', 'ccitt', 'fax3', 'fax4'}))
warning(['Unkown compression mode: ' params.compress ' -> Setting it to ''none''...']);
elseif ~any(strcmp(params.compress, {'none', 'packbits', 'deflate', 'lzw', ...
'ccitt', 'fax3', 'fax4'}))
warning('gtTIFVolWriter:unknown_compression_mode', ...
['Unknown compression mode: ' params.compress ...
' -> Setting it to ''none''...']);
params.compress = 'none';
end
......@@ -173,6 +177,7 @@ case 'deflate' , tifTag.Compression = Tiff.Compression.Deflate; % Slow but effi
case 'ccitt' , tifTag.Compression = Tiff.Compression.CCITTRLE;
case 'fax3' , tifTag.Compression = Tiff.Compression.CCITTFax3;
case 'fax4' , tifTag.Compression = Tiff.Compression.CCITTFax4;
otherwise , tifTag.Compression = Tiff.Compression.None;
end
% Set TIFF file tags
......@@ -183,13 +188,19 @@ tifTag.ImageLength = outVolSizeY;
tifTag.Software = 'matlabDCT';
% Guessing output directory, filename and extension
[fdir, fname, fext] = fileparts(filename);
[fpath, fname, fext] = fileparts(filename);
% Trick to handle ~ path in unix
if fpath(1)=='~' && isunix()
fpath(1) = '';
fpath = fullfile(getenv('HOME'), fpath);
end
if strcmpi(params.mode, 'single')
% Setting output filename
if ~length(fext)
filename = fullfile(fdir, [fname '.' params.filext]);
if isempty(fext)
filename = fullfile(fpath, [fname '.' params.filext]);
else
filename = fullfile(fpath, [fname '.' fext]);
end
% Check existence of output file to delete it before writing
......@@ -209,8 +220,10 @@ if strcmpi(params.mode, 'single')
else
% Old method
for iz=1:length(rangeZ)
imwrite(outVol(rangeX(1):rangeX(end), rangeY(1):rangeY(end), rangeZ(iz))', ...
filename, 'tif', 'Compression', params.compress, 'writemode', 'append');
imwrite(outVol(rangeX(1):rangeX(end), rangeY(1):rangeY(end), ...
rangeZ(iz))', ...
filename, 'tif', 'Compression', params.compress, ...
'writemode', 'append');
end
end
......@@ -220,11 +233,12 @@ elseif strcmpi(params.mode, 'stack')
if isnumeric(params.startindex)
if mod(params.startindex, 1) ~= 0
params.startindex = round(params.startindex);
disp(['Argument ''startindex'' convert to ' num2str(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);
gtError('gtTIFVolWriter:wrong_argument_type', ...
'The argument ''startindex'' should be an integer!');
end
% Check stack number of digits
......@@ -234,18 +248,18 @@ elseif strcmpi(params.mode, 'stack')
disp(['Argument ''digits'' convert to ' num2str(params.digits)]);
end
else
Mexc = MException('INPUT:wrong_type', ['The argument ''digits'' should be an integer!']);
throw(Mexc);
gtError('gtTIFVolWriter:wrong_argument_type', ...
'The argument ''digits'' should be an integer!');
end
% Create directory
if and(length(fdir), ~isdir(fdir))
mkdir(fdir);
if ~isempty(fpath) && ~exist(fpath, 'dir')
mkdir(fpath);
end
% Output file output format
fprefix = fullfile(fdir, fname);
filenameFormat = sprintf('%s%%0%dd.%s', fprefix, params.digits, params.filext);
fprefix = fullfile(fpath, fname);
filenameFMT = sprintf('%s%%0%dd.%s', fprefix, params.digits, params.filext);
% Set output index offset
offset = params.startindex - 1;
......@@ -253,7 +267,7 @@ elseif strcmpi(params.mode, 'stack')
% Writing volume in TIFF stack files
if params.newMethod
for iz=1:length(rangeZ)
outFile = sprintf(filenameFormat, iz+offset);
outFile = sprintf(filenameFMT, iz+offset);
tif = Tiff(outFile, 'w');
tif.setTag(tifTag);
tif.write(outVol(rangeX, rangeY, rangeZ(iz))');
......@@ -262,7 +276,7 @@ elseif strcmpi(params.mode, 'stack')
else
% Old method
for iz=1:length(rangeZ)
outFile = sprintf(filenameFormat, iz+offset);
outFile = sprintf(filenameFMT, iz+offset);
imwrite(outVol(rangeX, rangeY, rangeZ(iz))', outFile, 'tif', ...
'Compression', params.compress, 'writemode', 'overwrite');
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