Experimenting with some sort of RLE type compression for maps. Wont implement until map is complex as may be somewhat misleadingly efficient with an unpopulated map

Raw 10000
Compressed 1981
This commit is contained in:
stevenhowes
2021-06-01 22:56:20 +01:00
parent fad15ea5f5
commit a237d6b327
2 changed files with 76 additions and 0 deletions
+6
View File
@@ -54,6 +54,12 @@ o.CTheEscape: C:h.kernel
o.CTheEscape: C:h.kernel
o.CTheEscape: h.Sound
o.CTheEscape: h.Graphics
o.CTheEscape: c.CTheEscape
o.CTheEscape: C:h.swis
o.CTheEscape: C:h.kernel
o.CTheEscape: C:h.kernel
o.CTheEscape: h.Sound
o.CTheEscape: h.Graphics
o.Sound: c.Sound
o.Sound: C:h.swis
o.Sound: C:h.kernel
+70
View File
@@ -0,0 +1,70 @@
<?php
function map_compress($data)
{
$base64 = base64_encode($data);
$newstring = "";
$array = str_split($base64);
$array[] = ".";
$consec = 1;
for($i = 0; $i < strlen($base64);$i++)
{
if(($array[$i] != $array[$i+1]) || ($consec == 255))
{
if($consec > 3)
$newstring .= ">" . chr($consec) . $array[$i];
else
$newstring .= str_repeat($array[$i],$consec);
$consec = 1;
}else{
$consec++;
}
}
return $newstring;
}
function map_decompress($data)
{
$newstring = "";
$array = str_split($data);
$inrep = 0;
$repcount = "";
for($i = 0; $i < strlen($data);$i++)
{
if(($array[$i] == ">") && ($inrep == 0))
$inrep = 1;
elseif($inrep == 1)
{
$inrep = 0;
$repcount .= $array[$i];
}
else
{
if($repcount != "")
{
$newstring .= str_repeat($array[$i],intval(ord($repcount)));
$repcount = "";
}
else
{
$newstring .= $array[$i];
}
}
}
return base64_decode($newstring);
}
$mapdata = file_get_contents("S:\\RiscOSDev\\rpcemu-win32-0.9.3-bundle-371-issue-1\\RPCEmu - 371\\hostfs\\Dev\\!TheEsc\\m2_map,ffd);
echo "In Raw " . md5($mapdata). " " . strlen($mapdata). "\n";
$compressedstring = map_compress($mapdata);
echo "Cmprssd " . md5($compressedstring). " " . strlen($compressedstring). "\n";
$decompressedstring = map_decompress($compressedstring);
echo "Out Raw " .md5($decompressedstring). " " . strlen($decompressedstring). "\n";
?>