📄 hdfpeek.m
字号:
function [tagreflist, namelist, info]=hdfpeek( filename, tagref )
%HDFPEEK List object tag/ref pairs in HDF file.
% Note: HDFPEEK has been grandfathered. Use IMFINFO, IMREAD,
% and IMWRITE instead.
%
% [TAGREF,NAME,INFO]=HDFPEEK( 'filename' ) scans an HDF file
% and returns a list of tag/reference no. pairs for image
% groups in the file. TAGREF is an Nx2 matrix where N is the
% number of images found, and each row contains the tag and
% reference no. for an image in the file. NAME returns a
% matrix of strings containing the group names corresponding to
% the tags of the images, and INFO contains a corresponding
% list of scalars denoting the image depth (8 for binary,
% intensity, and indexed images, 24 for RGB images, -1 for
% unknown).
%
% [TAGREF,NAME,INFO]=HDFPEEK( 'filename', [GROUPTAG GROUPREF] )
% returns a list of data objects in the specified Raster Image
% Group, using the same output format as given above, except
% INFO will be a single scalar instead of a list of scalars.
%
% See also IMFINFO, IMREAD, IMWRITE.
% Author: J.M. Winograd 7-93
% Revised: Steven L. Eddins, May 1996
% Copyright 1993-1998 The MathWorks, Inc. All Rights Reserved.
% $Revision: 5.5 $ $Date: 1997/11/24 15:35:04 $
error( nargchk( 1, 2, nargin ) );
if (~isstr(filename))
error( 'FILENAME must be a string.' );
end
% Find full filename
fid = fopen(filename);
if fid == -1, error(['File ',filename,' not found.']); end
fname = fopen(fid); fclose(fid);
if nargin == 1
[tagreflist, namelist, info] = TagSearch( fname );
else
if (tagref(1,1) ~= 306)
error('Unsupported or invalid group tag');
end
[tagreflist, namelist, info] = RIGSearch( fname, tagref(1,2) );
end
%%%
%%% TagSearch --- find and return info about RIG tags in file
%%%
function [tagreflist, namelist, info] = TagSearch(fname)
DFTAG_RIG = 306;
DFTAG_ID = 300;
DFREF_WILDCARD = 0;
% restart HDF system
hdf('DFR8', 'restart');
hdf('DF24', 'restart');
file_id = hdf('H', 'open', fname, 'read', 0);
if (file_id == -1)
error(sprintf('Could not open %s', fname));
end
% Find number of objects in file with the Raster Image Group tag
matchCount = hdf('H', 'number', file_id, DFTAG_RIG);
tagreflist = zeros(matchCount, 2);
namelist = repmat(' ', matchCount, 30);
info = zeros(matchCount, 1);
access_id = hdf('H', 'startread', file_id, DFTAG_RIG, DFREF_WILDCARD);
status = access_id;
for k = 1:matchCount
if (status == -1)
hdf('H', 'endaccess', access_id);
hdf('H', 'close', file_id)
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
[junk, junk, ref, numbytes, junk, junk, junk, junk, status] = ...
hdf('H', 'inquire', access_id);
if (status == -1)
hdf('H', 'endaccess', access_id);
hdf('H', 'close', file_id)
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
tagreflist(k,:) = [DFTAG_RIG ref];
nameString = hdf('HD', 'gettagsname', DFTAG_RIG);
nameString = nameString(1:min(end, 30));
namelist(k,1:length(nameString)) = nameString;
memberList = hdf('H', 'read', access_id, numbytes);
if (isempty(memberList))
hdf('H', 'endaccess', access_id);
hdf('H', 'close', file_id)
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
memberList = Uint16Decode(memberList);
memberTags = memberList(1:2:end);
memberRefs = memberList(2:2:end);
idx = find(memberTags == DFTAG_ID);
if (isempty(idx))
warning('Missing DFTAG_ID in Raster Image Group');
info(k) = -1;
break;
else
idx = idx(1); % There should be only 1, but we'll be cautious
end
% Find the number of components, which is stored in the DFTAG_ID record.
access_id2 = hdf('H', 'startread', file_id, DFTAG_ID, memberRefs(idx));
if (access_id2 == -1)
hdf('H', 'endaccess', access_id);
hdf('H', 'close', file_id)
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
memberList = hdf('H', 'read', access_id2, 14);
if (isempty(memberList))
hdf('H', 'endaccess', access_id);
hdf('H', 'endaccess', access_id2);
hdf('H', 'close', file_id)
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
hdf('H', 'endaccess', access_id2);
numComponents = Uint16Decode(memberList(13:14));
if (numComponents == 1)
info(k) = 8;
elseif (numComponents == 3)
info(k) = 24;
else
info(k) = -1;
end
if (k < matchCount)
status = hdf('H', 'nextread', access_id, DFTAG_RIG, ...
DFREF_WILDCARD, 'current');
end
end
hdf('H', 'endaccess', access_id);
hdf('H', 'close', file_id);
%%%
%%% RIGSearch --- return list of tags contained in Raster Image Group
%%%
function [tagrefs,names,info] = RIGSearch(fname, ref)
% HDF constants
DFTAG_RIG = 306;
DFTAG_ID = 300;
% Restart HDF system
hdf('DFR8', 'restart');
hdf('DF24', 'restart');
file_id = hdf('H', 'open', fname, 'read', 0);
if (file_id == -1)
error(sprintf('Could not open %s', fname));
end
access_id = hdf('H', 'startread', file_id, DFTAG_RIG, ref);
if (access_id == -1)
hdf('H', 'close', file_id);
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
bytes = hdf('H', 'read', access_id, 0);
if (isempty(bytes))
hdf('H', 'endaccess', access_id);
hdf('H', 'close', file_id);
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
hdf('H', 'endaccess', access_id);
shorts = Uint16Decode(bytes);
memberTags = shorts(1:2:end);
memberRefs = shorts(2:2:end);
tagrefs = [memberTags memberRefs];
names = '';
for k = 1:length(memberTags)
names = str2mat(names, hdf('HD', 'gettagsname', memberTags(k)));
end
names(1,:) = [];
idx = find(memberTags == DFTAG_ID);
if (isempty(idx))
warning('Missing DFTAG_ID in Raster Image Group');
info = -1;
break;
else
idx = idx(1); % There should be only 1, but we'll be cautious
end
% Find the number of components, which is stored in the DFTAG_ID record.
access_id = hdf('H', 'startread', file_id, DFTAG_ID, memberRefs(idx));
if (access_id == -1)
hdf('H', 'close', file_id)
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
memberList = hdf('H', 'read', access_id, 14);
if (isempty(memberList))
hdf('H', 'endaccess', access_id);
hdf('H', 'close', file_id)
error(hdf('HE', 'string', hdf('HE', 'value', 1)));
end
hdf('H', 'endaccess', access_id);
numComponents = Uint16Decode(memberList(13:14));
if (numComponents == 1)
info = 8;
elseif (numComponents == 3)
info = 24;
else
info = -1;
end
hdf('H', 'close', file_id);
%%%
%%% Uint16Decode --- turn uint8 input into uint16 integers
%%%
function out = Uint16Decode(in)
msb = double(in(1:2:end));
lsb = double(in(2:2:end));
out = bitshift(msb,8) + lsb;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -