Home > Back-end >  RLE algorithm encryption image is saved as a hexadecimal code array, implementation approach
RLE algorithm encryption image is saved as a hexadecimal code array, implementation approach

Time:10-03

GraphicCompression myself beginners Delphi, through the data access components can carry on the transformation to the image, the younger brother to implement the thinking of Rle encryption image is first read the original image data stream and then call EncodeRLE () is encrypted, TargetPtr is encrypted data, do not know whether it is feasible to
The following code:
The function CountDiffPixels (P: PByte; BPP: Byte; Count: Integer) : Integer;

//counts pixels in buffer until two identical adjacent ones found

Var
N: Integer;
Pixel,
NextPixel: Cardinal;

The begin
N:=0;
NextPixel:=0;//shut up the compiler
If the Count=1 then the Result:=Count
The else
The begin
Pixel:=GetPixel (P, BPP);
While the Count & gt; 1 do
The begin
Inc (P, BPP);
NextPixel:=GetPixel (P, BPP);
If NextPixel=Pixel then Break;
Pixel:=NextPixel;
Inc (N);
Dec (Count);
end;
If NextPixel=Pixel then Result: N=
The else Result:=N + 1;
end;
end;

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The function CountSamePixels (P: PByte; BPP: Byte; Count: Integer) : Integer;

Var
Pixel,
NextPixel: Cardinal;

The begin
Result:=1;
Pixel:=GetPixel (P, BPP);
Dec (Count);
While the Count & gt; 0 do
The begin
Inc (P, BPP);
NextPixel:=GetPixel (P, BPP);
If NextPixel & lt;> Pixel then Break;
Inc (Result);
Dec (Count);
end;
end;

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The function EncodeRLE (const Source and Target: Pointer; Count: Integer; BitLength: Byte) : Integer;
Var
I: Integer;
DiffCount,//pixel count until two identical
SameCount: Integer;//number of identical adjacent pixels
SourcePtr,
TargetPtr: PByte;
The begin
Result:=0;
SourcePtr:=Source;
TargetPtr:=Target;
While the Count & gt; 0 do begin
//create a raw packet
DiffCount:=CountDiffPixels (SourcePtr bitLength, Count);
SameCount:=CountSamePixels (SourcePtr bitLength, Count);
If DiffCount & gt; Then 128
DiffCount:=128;
If SameCount & gt; Then 128
SameCount:=128;
If DiffCount & gt; 0 then the begin
TargetPtr ^ :=DiffCount - 1;
Inc (TargetPtr);
Dec (Count, DiffCount);
Inc (Result, (DiffCount * bitLength) + 1);
While DiffCount & gt; 0 do begin
For I:=0 to do bitLength - 1
The begin
TargetPtr ^ : SourcePtr=^.
Inc (SourcePtr);
Inc (TargetPtr);
end;
Dec (DiffCount);
end;
end;
If SameCount & gt; 1 then the begin
//create a RLE packet
TargetPtr ^ :=(SameCount - 1) or $80;
Inc (TargetPtr);
Dec (Count, SameCount);
Inc (Result, bitLength + 1);
Inc (SourcePtr, (SameCount - 1) * bitLength);
For I:=0 to do bitLength - 1
The begin
TargetPtr ^ : SourcePtr=^.
Inc (SourcePtr);
Inc (TargetPtr);
end;
end;
end;
end;
  • Related