The SDL Component Suite is an industry leading collection of components supporting scientific and engineering computing. Please visit the SDL Web site for more information....



Encode


Unit:SDL_dstruct
Class: TRLEncoder
Declaration: function Encode (var InBuf: array of byte; NumBytes: integer): boolean; {Pascal}
bool __fastcall Encode(Byte * InBuf, const int InBuf_Size, int NumBytes); {C++}

The method Encode accepts NumBytes input data which are stored in the array variable Inbuf and encodes it into a run length limited data stream. If the result exceeds the maximum buffer length, a TRUE value is returned. In this case the encoder buffer should be emptied by calling the method GetResult.

The user has to make sure that the NumBytes does not exceed BufLeng, otherwise the resulting code may be corrupted.

Hint: The declaration of Encode in C++ slightly differs from the Pascal declaration (note the extra parameter InBuf_Size which specifies the highest index of the InBuf array):
bool __fastcall Encode(Byte * InBuf, const int InBuf_Size, int NumBytes);

Example: Below is a sample code which reads data from a file and stores the RL encoded stream in another file (RLE1 is an instance of TRLEncoder):

const
  BLeng = 1024;

var
  IFile     : file;
  OFile     : file;
  count     : integer;
  Buffer    : array [1..BLeng] of byte;

begin
OpenDialog1.FileName := '*.bmp';
if OPenDialog1.Execute then
  begin
  assignFile (IFile, OpenDialog1.Filename);
  reset (IFile, 1);
  assignFile (OFile, StripExtension(OpenDialog1.Filename)+'.RLE');
  rewrite (OFile, 1);
  RLE1.Reset;
  RLE1.BufLeng := BLeng;
  while not eof(IFile) do
    begin
    Blockread (IFile, Buffer, BLeng, count);
    if RLE1.Encode (Buffer, count) then
      begin
      RLE1.GetResult (Buffer);
      BlockWrite (OFile, Buffer, BLeng);
      end;
    end;
  count := RLE1.Finish (Buffer);
  BlockWrite (OFile, Buffer, count);
  closeFile (OFile);
  closeFile (IFile);
  end;
end;



Last Update: 2012-Oct-20