Class Origami::Filter::RunLength
In: sources/parser/filters.rb
Parent: Object

Class representing a Filter used to encode and decode data using RLE compression algorithm.

Methods

decode   encode  

Included Modules

Filter

Public Instance methods

Decodes data using RLE decompression method.

stream:The data to decode.

[Source]

     # File sources/parser/filters.rb, line 726
726:       def decode(stream)
727:         
728:         i = 0
729:         result = ""
730:         
731:         if not stream.include?(EOD) then raise InvalidRunLengthData, "No end marker" end
732:         
733:         until stream[i] == EOD do
734:         
735:           length = stream[i]
736:           if length < EOD
737:             result << stream[i + 1, length + 1]
738:             i = i + length + 2
739:           else
740:             result << stream[i + 1,1] * (257 - length)
741:             i = i + 2
742:           end
743:           
744:         end
745:         
746:         result
747:       end

Encodes data using RLE compression method.

stream:The data to encode.

[Source]

     # File sources/parser/filters.rb, line 678
678:       def encode(stream)
679: 
680:         result = ""
681:         i = 0
682: 
683:         while i < stream.size
684:           
685:           #
686:           # How many identical bytes coming?
687:           #
688:           length = 1
689:           while i+1 < stream.size and length < EOD and stream[i] == stream[i+1]
690:             length = length + 1
691:             i = i + 1
692:           end
693: 
694:           #
695:           # If more than 1, then compress them.
696:           #
697:           if length > 1
698:             result << (257 - length).chr << stream[i,1]
699: 
700:           #
701:           # Otherwise how many different bytes to copy ?
702:           #
703:           else
704:             j = i
705:             while j+1 < stream.size and (j - i + 1) < EOD and stream[j] != stream[j+1]
706:               j = j + 1
707:             end
708: 
709:             length = j - i
710:             result << length.chr << stream[i, length+1]
711: 
712:             i = j
713:           end
714: 
715:           i = i + 1
716:         end
717: 
718:         result << EOD
719:       end

[Validate]