% LUT2MAT(org,pflg)
%
% convert NIH-IMAGE compatible LUT files
% <xxx.lut>
% to ML compatible colormap files
% <xxx_lut.m>
% that can be used as
% <xxx_lut(ncols)>
%
% org : pointer to origin of LUT files
% - a folder
% - a lut_file name (including wildcards)
% - a folder/lut_file_name(*)
%
% flg : processing mode
% 1 show colormaps during conversion [def]
% 0 do NOT show colormaps during conversion
%
% note
% <xxx_lut.m> files are created in the CURRENT folder
% created:
% us 12-Nov-1992
% modified:
% us 03-Sep-2006 18:54:49
%--------------------------------------------------------------------------------
function bo=lut2mat(fnam,pflg)
if nargout
bo=[];
end
if nargin < 1
help lut2mat;
return;
end
if nargin < 2
pflg=true;
end
lext='.lut';
[fpat,frot,fext]=fileparts(fnam);
if isempty(fpat)
fpat=cd;
end
if isdir(frot)
fpat=[fpat,filesep,frot];
frot='*';
end
if isempty(fext)
fext=lext;
end
if isempty(frot)
frot='*';
end
idir=[fpat,filesep,frot,fext];
s=dir(idir);
if isempty(s)
disp(sprintf('lut2mat> no LUT files in <%s>',fnam));
return;
end
if pflg
clf;
imagesc(repmat((1:256).',1,3));
axis xy;
colorbar;
shg;
end
nl=size(s,1);
disp(sprintf('lut2mat> converting %5d <%s>',nl,idir));
for i=1:nl
flut=lower(s(i).name);
frot=strrep(flut,'.lut','');
fmat=[frot '_lut.m'];
flut=fullfile(fpat,flut);
disp(sprintf('lut2mat> converting %5d <%s>',i,fmat));
fp=fopen(flut,'rb');
if fp > 0
b=fread(fp,inf,'uint8');
fclose(fp);
b=b/255;
bl=length(b);
b=reshape(b,bl/3,3);
if pflg
colormap(b);
title([frot ': ' num2str(size(b,1))],'interpreter','none');
pause;
end
if exist(fmat,'file') == 200 % TURNED OFF BY DEFAULT!
disp(sprintf('lut2mat> error **** <%s> existing!',fmat));
else
[epat,enam,eext]=fileparts(fmat); %#ok
fp=fopen(fmat,'wt');
if fp > 0
fprintf(fp,'%% LUT file <%s>\n',flut);
fprintf(fp,'%% size %-1d\n',size(b,1));
fprintf(fp,'%% ML file <%s>\n',fmat);
fprintf(fp,'%% created <%s>\n\n',datestr(clock));
fprintf(fp,'function\th = %s(m)\n\n',enam);
fprintf(fp,'h = [\n');
fprintf(fp,'%g,%g,%g;\n',b');
fprintf(fp,'];');
end_lut(fp);
fclose(fp);
end
end
else
disp(sprintf('lut2mat> error **** <%s>',flut));
end
end
if nargout
bo=b;
end
return;
%--------------------------------------------------------------------------------
function end_lut(fp)
t={
''
''
'if nargin && ~isempty(m)'
' x=(1:size(h,1)).'';'
' k=linspace(1,x(end),m)'';'
' h=interp1(x,h,k);'
'end'
};
fprintf(fp,'%s\n',t{:});
return;
%--------------------------------------------------------------------------------