LZW算法简介
字符串和编码的对应关系是在压缩过程中动态生成的,并且隐含在压缩数据中,解压的时候根据表来进行恢复,算是一种无损压缩.
根据 Lempel-Ziv-Welch Encoding ,简称 LZW 的压缩算法,用任何一种语言来实现它.
LZW压缩算法[1]的基本概念:LZW压缩有三个重要的对象:数据流(CharStream)、编码流(CodeStream)和编译表(String Table)。在编码时,数据流是输入对象(文本文件的据序列),编码流就是输出对象(经过压缩运算的编码数据);在解码时,编码流则是输入对象,数据流 是输出对象;而编译表是在编码和解码时都须要用借助的对象。字符(Character):最基础的数据元素,在文本文件中就是一个字节,在光栅数据中就是 一个像素的颜色在指定的颜色列表中的索引值;字符串(String):由几个连续的字符组成; 前缀(Prefix):也是一个字符串,不过通常用在另一个字符的前面,而且它的长度可以为0;根(Root):一个长度的字符串;编码(Code):一 个数字,按照固定长度(编码长度)从编码流中取出,编译表的映射值;图案:一个字符串,按不定长度从数据流中读出,映射到编译表条目.
LZW压缩算法的基本原理:提取原始文本文件数据中的不同字符,基于这些字符创建一个编译表,然后用编译表中的字符的索引来替代原始文本文件数据中 的相应字符,减少原始数据大小。看起来和调色板图象的实现原理差不多,但是应该注意到的是,我们这里的编译表不是事先创建好的,而是根据原始文件数据动态 创建的,解码时还要从已编码的数据中还原出原来的编译表.
原版:
<?php
/**
* @link http://code.google.com/p/php-lzw/
* @author Jakub Vrana, http://php.vrana.cz/
* @copyright 2009 Jakub Vrana
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/** LZW compression
* @param string data to compress
* @return string binary data
*/
function lzw_compress($string) {
// compression
$dictionary = array_flip(range("\0", "\xFF"));
$word = "";
$codes = array();
for ($i=0; $i <= strlen($string); $i++) {
$x = $string[$i];
if (strlen($x) && isset($dictionary[$word . $x])) {
$word .= $x;
} elseif ($i) {
$codes[] = $dictionary[$word];
$dictionary[$word . $x] = count($dictionary);
$word = $x;
}
}
// convert codes to binary string
$dictionary_count = 256;
$bits = 8; // ceil(log($dictionary_count, 2))
$return = "";
$rest = 0;
$rest_length = 0;
foreach ($codes as $code) {
$rest = ($rest << $bits) + $code;
$rest_length += $bits;
$dictionary_count++;
if ($dictionary_count > (1 << $bits)) {
$bits++;
}
while ($rest_length > 7) {
$rest_length -= 8;
$return .= chr($rest >> $rest_length);
$rest &= (1 << $rest_length) - 1;
}
}
return $return . ($rest_length ? chr($rest << (8 - $rest_length)) : "");
}
/** LZW decompression
* @param string compressed binary data
* @return string original data
*/
function lzw_decompress($binary) {
// convert binary string to codes
$dictionary_count = 256;
$bits = 8; // ceil(log($dictionary_count, 2))
$codes = array();
$rest = 0;
$rest_length = 0;
for ($i=0; $i < strlen($binary); $i++) {
$rest = ($rest << 8) + ord($binary[$i]);
$rest_length += 8;
if ($rest_length >= $bits) {
$rest_length -= $bits;
$codes[] = $rest >> $rest_length;
$rest &= (1 << $rest_length) - 1;
$dictionary_count++;
if ($dictionary_count > (1 << $bits)) {
$bits++;
}
}
}
// decompression
$dictionary = range("\0", "\xFF");
$return = "";
foreach ($codes as $i => $code) {
$element = $dictionary[$code];
if (!isset($element)) {
$element = $word . $word[0];
}
$return .= $element;
if ($i) {
$dictionary[] = $word . $element[0];
}
$word = $element;
}
return $return;
}
$data = "";
$compressed = lzw_compress($data);
var_dump($data === lzw_decompress($compressed));
优化版:
<?php
function lzw_compress($string) {
// compression
$dict = array_flip(range("\\0", "\\xFF"));
$dict_size = 256;
$word = $string[0];
$dict_count = 256;
$bits = 8;
$bits_max = 256;
$return = "";
$rest = 0;
$rest_length = 0;
for ($i = 1, $j = strlen($string); $i < $j; $i++) {
$x = $string[$i];
$y = $word . $x;
if (isset($dict[$y])) {
$word .= $x;
} else {
$rest = ($rest << $bits) + $dict[$word];
$rest_length += $bits;
$dict_count++;
if ($dict_count > $bits_max) {
$bits_max = 1 << ++$bits;
}
while ($rest_length > 7) {
$rest_length -= 8;
$return .= chr($rest >> $rest_length);
$rest &= (1 << $rest_length) - 1;
}
$dict[$y] = $dict_size++;
$word = $x;
}
}
$rest = ($rest << $bits) + $dict[$word];
$rest_length += $bits;
$dict_count++;
if ($dict_count > $bits_max) {
$bits_max = 1 << ++$bits;
}
while ($rest_length > 0) {
if($rest_length>7){
$rest_length -= 8;
$return .= chr($rest >> $rest_length);
$rest &= (1 << $rest_length) - 1;
}else{
$return .= chr($rest << (8 - $rest_length));
$rest_length = 0;
}
}
return $return;
}
/** LZW decompression
* @param string compressed binary data
* @return string original data
*/
function lzw_decompress($binary) {
// convert binary string to codes
$rest = 0;
$rest_length = 0;
$out_count = 257;
$bits = 9;
$bits_max = 512;
// decompression
$dict = range("\\0", "\\xFF");
$w = $binary[0];
$return = $w;
for ($i = 1, $j = strlen($binary); $i < $j; $i++) {
$rest = ($rest << 8) + ord($binary[$i]);
$rest_length += 8;
if ($rest_length >= $bits) {
$rest_length -= $bits;
// decompression
$e = $dict[$rest >> $rest_length];
if (!isset($e)) {
$e = $w . $w[0];
}
$return .= $e;
$dict[] = $w . $e[0];
$w = $e;
//--decompression
$rest &= (1 << $rest_length) - 1;
if (++$out_count > $bits_max) {
$bits_max = 1 << ++$bits;
}
}
}
return $return;
}
?>