Code archives/Algorithms/RLE compression
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
Wow, am I the first to put RLE compression on here? Pretty simple to use, 2 small functions. :) This kind of compression is really good for cleaning up files that repeat the same character over and over again alot. Using it for my hardware projects.. | |||||
Function RLE_compress(in_file$,out_file$) Local temp$ ifile = ReadFile(in_file) ofile = WriteFile(out_file) oldbyte = -1 While Not Eof(ifile) newbyte = ReadByte(ifile) If newbyte = oldbyte ; another byte! Lets add it to the list :) If rcount = 255 ; we have TOO many in the list :) WriteByte(ofile,rcount) WriteByte(ofile,newbyte) rcount = 0 ElseIf rcount = 0 And Len(temp)>0 WriteByte(ofile,0) WriteByte(ofile,Len(temp)) For i=1 To Len(temp) WriteByte(ofile,Asc(Mid(temp,i,1))) Next temp="" rcount = rcount + 1 Else rcount = rcount + 1 EndIf Else ; new byte type! Lets write off the old byte list If oldbyte>-1 If rcount>0 WriteByte(ofile,rcount) WriteByte(ofile,oldbyte) Else If Len(temp)=255 WriteByte(ofile,0) WriteByte(ofile,Len(temp)) For i=1 To Len(temp) WriteByte(ofile,Asc(Mid(temp,i,1))) Next temp = Chr(oldbyte) Else temp = temp + Chr(oldbyte) EndIf EndIf EndIf oldbyte = newbyte rcount = 0 EndIf Wend If rcount>0 WriteByte(ofile,rcount) WriteByte(ofile,oldbyte) EndIf CloseFile(ifile) CloseFile(ofile) End Function Function RLE_uncompress(in_file$,out_file$) ifile = ReadFile(in_file) ofile = WriteFile(out_file) While Not Eof(ifile) rcount = ReadByte(ifile) If rcount = 0 length = ReadByte(ifile) For i=1 To length WriteByte ofile,ReadByte(ifile) Next Else arg = ReadByte(ifile) For i=0 To rcount WriteByte ofile,arg Next EndIf Wend CloseFile(ifile) CloseFile(ofile) End Function |
Comments
| ||
excelent. Il be number two, Im no expert in file compression but i was chuffed to see this work quite nicely!: Function simplecompress(name$,outputname$) fs=WriteFile(outputname$) fs2=ReadFile(name$) bt=0 Repeat count=0 Repeat obt=bt bt=ReadByte(fs2) count=count+1 Until (bt<>obt And obt<>-1) Or count>254 Or Eof(fs2) WriteByte fs,count WriteByte fs,obt Until Eof(fs2) CloseFile fs2 CloseFile fs DeleteFile name$ End Function Function simpledecompress(name$,tempname$) fs=WriteFile(tempname$) fs2=ReadFile(name$) dn=0 Repeat count=ReadByte(fs2) Char=ReadByte(fs2) If dn>0 Then For z=1 To count:WriteByte fs,char:Next dn=1 Until Eof(fs2) CloseFile fs2 CloseFile fs End Function Problem being with mine, a file with every char different ends up twice the size.... ..and.... Anyone tell me how to put that into a code box??? |
| ||
"Cygnus" the various codes are in here: http://www.blitzbasic.com/faq/faq_entry.php?id=2 |
| ||
RLE is best used with images that have huge blocks of the same color, such as cartoon characters or illustrations. Terrible for photographs or highly detailed images. If your interested in learning more about compression, a google search brings up a lot of relavent info. Try looking at huffman compression or LZH compression, among others. If there's a specific reason why you want to use RLE, a modification will help to improve it. Not only have repeat next byte n times, but also write next n bytes as is. The format would look something like this: read countbyte. if countbyte is 0 - 127, then write next byte countbyte+1 times if countbyte is 128-255, then write the next countbyte-127 bytes as is. So 1 2 3 3 3 3 3 4 3 4 5 4 4 4 4 4 4 4 would encode as 129 1 2 4 3 131 4 3 4 5 6 4 |
| ||
Yeah, that's what I have. :) |
| ||
I think this is really Cool. I did something like this for images with Palettes of course. I tried doing stuff with 24-bit.... Didn't quite work out well. To the point. I tried it again ... 24-bit image but instead of every color attribute 0-255 i did multiples of 8 for the rgb values. It reduced the quality but it still looked ok to me. this should work. All you have to do is state your inputfilename and your outputfilename |
| ||
I did some searching, .... here are some good links on variations of RLE. http://mattmahoney.net/dc/dce.html#Section_51 http://www.fadden.com/techmisc/hdc/lesson02.htm http://michael.dipperstein.com/rle/ http://michael.dipperstein.com/rle/#download http://oldwww.rasip.fer.hr/research/compress/algorithms/fund/rl/ http://bit.ly/1Jw6NY4 (Rosetta Code) ------------------------------------------------------------------ One interesting complication is that 'Escape' bytes (or signals) may be embedded inside an RLE sequence. --Then what if the actual 'escape' character occurs (rarely) as part of the source? ------------------------------------------------------------------ read countbyte. if countbyte is 0 - 127, then write next byte countbyte+1 times if countbyte is 128-255, then write the next countbyte-127 bytes as is. Now that is clever... ------------------------------------------------------------------ PinBoard search for compression: http://bit.ly/1Jwcc1c Delicious search for compression: http://bit.ly/1JwaPQe |
| ||
Cool! |
Code Archives Forum