-- RLE-Compression for 24-bit RGB pixel values (3-Byte-triples)
-- loosely based on RLE8 compression in windows bitmaps (BMPs),
-- see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_6x0u.asp
--------------------------------------
--
--------------------------------------
on RLE24_encode (str)
out = ""
cnt = str.length/3
repeat with i = 1 to cnt
c = get_tripel_enc(str, i)
num = 1
repeat while true
if i+1>cnt or num>254 then exit repeat
if get_tripel_enc(str, i+1)<>c then exit repeat
i = i + 1
num=num+1
end repeat
if num>1 OR i=cnt then
put numtochar(num) after out
put c after out
else
if i>cnt-3 then
put numtochar(1) after out
put get_tripel_enc(str, i) after out
else if get_tripel_enc(str, i+1)<>get_tripel_enc(str, i+2) AND get_tripel_enc(str, i+2)<>get_tripel_enc(str, i+3) then -- escape to absolute mode
put numtochar(0) after out
put numtochar(0) after out --> change later
cntPos = out.length
num = 0
repeat while true
if i+1>cnt or num>254 then
put get_tripel_enc(str, i) after out
i=i+1
num=num+1
exit repeat
end if
t = get_tripel_enc(str, i)
if t=get_tripel_enc(str, i+1) then exit repeat
put t after out
i=i+1
num=num+1
end repeat
put numtochar(num) into char cntPos of out
i=i-1
else -- don't escape to absolute mode
put numtochar(1) after out
put get_tripel_enc(str, i) after out
end if
end if
end repeat
return out
end
--------------------------------------
--
--------------------------------------
on RLE24_decode (str)
out = ""
cnt = str.length
repeat with i = 1 to cnt
o = chartonum(str.char[i])
case (o) of
0: -- ESCAPE
i=i+1
-- case (chartonum(str.char[i])) of
-- 0: -- put "NEW LINE"
-- 1: -- put "END OF FILE"
-- 2: --put "DELTA"
-- i=i+1
-- --out.add( "delta-x:" && l[i] )
-- i=i+1
-- --out.add( "delta-y:" && l[i] )
--
-- otherwise: -- put "ABSOLUTE MODE"
num = chartonum(str.char[i])
repeat with j = 1 to num
put get_tripel_dec(str, i) after out
i=i+3
end repeat
-- end case
otherwise:
c = get_tripel_dec(str, i)
i=i+3
repeat with j=1 to o
put c after out
end repeat
end case
end repeat
return out
end
--------------------------------------
-- utilities
--------------------------------------
on get_tripel_enc (str, i)
pos = (i-1)*3+1
return str.char[pos]&str.char[pos+1]&str.char[pos+2]
end
on get_tripel_dec (str, pos)
return str.char[pos+1]&str.char[pos+2]&str.char[pos+3]
end