LZW compression: Difference between revisions
Content added Content deleted
m (→version 2: changed whitespace and comments, simplified some code.) |
|||
Line 4,898: | Line 4,898: | ||
This REXX version can execute on '''ASCII''' or '''EBCDIC''' systems. |
This REXX version can execute on '''ASCII''' or '''EBCDIC''' systems. |
||
<lang rexx>/*REXX program compresses text using the LZW (Lempel─Ziv─Welch), and reconstitutes it.*/ |
<lang rexx>/*REXX program compresses text using the LZW (Lempel─Ziv─Welch), and reconstitutes it.*/ |
||
parse arg x; if x='' then , |
parse arg x; if x='' then x= , /*get an optional argument from the CL.*/ |
||
'"There is nothing permanent except change." ─── Heraclitus [540 ── 475 BCE]' |
|||
say 'original text=' x /* [↑] Not specified? Then use default*/ |
say 'original text=' x /* [↑] Not specified? Then use default*/ |
||
cypher= LZWc(x) /*compress text using the LZW algorithm*/ |
cypher= LZWc(x) /*compress text using the LZW algorithm*/ |
||
say 'reconstituted=' LZWd(cypher) /*display the reconstituted string. */ |
say 'reconstituted=' LZWd(cypher) /*display the reconstituted string. */ |
||
say ' LZW integers=' cypher /* " " LZW integers used. */ |
say ' LZW integers=' cypher /* " " LZW integers used. */ |
||
exit |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
LZWi: arg i,@.; #=256; do j=0 for #; _=d2c(j); if i then @.j=_; else @._=j; end; return |
|||
⚫ | |||
do k=1 for length(y)+1; z= w || substr(y, k, 1) |
|||
if @.z=='' then do; $= $ @.w; @.z= #; #= # + 1; w= substr(y, k, 1); end |
|||
else w= z /*#: the dictionary size.*/ |
|||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
LZWc: procedure; parse arg y,,$; call LZWi 0; w= /*LZW compress.*/ |
|||
do k=1 for length(y)+1; z= w || substr(y, k, 1) |
|||
if @.z=='' then do; $= $ @.w; @.z= #; #= # + 1; w= substr(y, k, 1); end |
|||
else w= z /*#: the dictionary size.*/ |
|||
end /*k*/; return substr($, 2) /*elide a leading blank. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
LZWd: procedure; parse arg x y; call LZWi 1; $= @.x; w= $ /*LZW decompress.*/ |
|||
do k=1 for words(y); z= word(y, k) |
|||
if @.z\=='' | @.k==" " then ?= @.z |
|||
⚫ | |||
$= $ || ? |
|||
⚫ | |||
⚫ | |||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |