📄 another lzw compression algorithm.mht
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject:
Date: Mon, 30 Jun 2008 15:14:07 +0800
MIME-Version: 1.0
Content-Type: text/html;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.mathworks.com/matlabcentral/files/14741/norm2lzw.m
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.3354" name=3DGENERATOR></HEAD>
<BODY><PRE>function [output,table] =3D norm2lzw(vector)
%NORM2LZW LZW Data Compression (encoder)
% For vectors, NORM2LZW(X) is the compressed vector of X using the LZW =
algorithm.
% [...,T] =3D NORM2LZW(X) returns also the table that the algorithm =
produces.
%
% For matrices, X(:) is used as input.
%
% Input must be of uint8 type, while the output is a uint16.
% Table is a cell array, each element containig the corresponding =
code.
%
% This is an implementation of the algorithm presented in the article
% http://www.dogma.net/markn/articles/lzw/lzw.htm
%
% See also LZW2NORM
% $Author: Giuseppe Ridino' $
% $Revision: 1.0 $ $Date: 10-May-2004 14:16:08 $
%
% Revision:
% Change the code table structure to improve the performance.=20
% date: 22-Apr-2007
% by: Haiyong Xu (haiyeong@gmail.com)
% =20
% How it encodes:
%
% STRING =3D get input character
% WHILE there are still input characters DO
% CHARACTER =3D get input character
% IF STRING+CHARACTER is in the string table then
% STRING =3D STRING+character
% ELSE
% output the code for STRING
% add STRING+CHARACTER to the string table
% STRING =3D CHARACTER
% END of IF
% END of WHILE
% output the code for STRING
% ensure to handle uint8 input vector
if ~isa(vector,'uint8'),
error('input argument must be a uint8 vector')
end
% vector as double row
vector =3D double(vector(:)');
table =3D double(0:255)';
global APPENDING;
APPENDING =3D -1;
% initialize output
output =3D vector;
% main loop
outputindex =3D 1;
startindex =3D 1;
for index=3D2:length(vector),
if mod(index, 1000) =3D=3D 0
index
size(table)
end
element =3D vector(index);
substr =3D vector(startindex:(index-1));
code =3D getcodefor([substr element],table);
if isempty(code),
% add it to the table
output(outputindex) =3D getcodefor(substr,table);
[table,code] =3D addcode(table,[substr element]);
outputindex =3D outputindex+1;
startindex =3D index;
else,
% go on looping
end
end
substr =3D vector(startindex:index);
output(outputindex) =3D getcodefor(substr,table);
% remove not used positions
output((outputindex+1):end) =3D [];
%%
function code =3D getcodefor(substr,table)
global APPENDING;
code =3D double([]);
if length(substr)=3D=3D1,
code =3D substr;
else
x =3D size(substr, 2);
[m, y] =3D size(table);
if x < y
substr =3D [substr, APPENDING * ones(1, y - x)];
elseif x > y
return;
end
=20
% find which row in codetable equals substr
x =3D table - repmat(substr, [m, 1]);
index =3D find(sum(abs(x), 2) =3D=3D 0);
if numel(index) =3D=3D 1
code =3D index - 1;
end
end
%%
function [table,code] =3D addcode(table,substr)
global APPENDING;
x =3D size(substr, 2);
[code, y] =3D size(table);
if x > y
table =3D [table, repmat(APPENDING * ones(1, x - y), [code, 1])];
elseif x < y
substr =3D [substr, APPENDING * ones(1, y - x)];
end
table =3D [table; substr];
</PRE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -