本人初學Delphi,通過查閱資料GraphicCompression組件能夠對圖片進行轉換,小弟實作Rle加密圖片的思路是先讀取原圖片的資料流然后呼叫EncodeRLE()進行加密,TargetPtr即為加密后資料,不知是否可行
下邊代碼:
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;
begin
N := 0;
NextPixel := 0; // shut up compiler
if Count = 1 then Result := Count
else
begin
Pixel := GetPixel(P, BPP);
while Count > 1 do
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
else Result := N + 1;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function CountSamePixels(P: PByte; BPP: Byte; Count: Integer): Integer;
var
Pixel,
NextPixel: Cardinal;
begin
Result := 1;
Pixel := GetPixel(P, BPP);
Dec(Count);
while Count > 0 do
begin
Inc(P, BPP);
NextPixel := GetPixel(P, BPP);
if NextPixel <> Pixel then Break;
Inc(Result);
Dec(Count);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function EncodeRLE(const Source, 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;
begin
Result := 0;
SourcePtr := Source;
TargetPtr := Target;
while Count > 0 do begin
// create a raw packet
DiffCount := CountDiffPixels(SourcePtr, bitLength, Count);
SameCount := CountSamePixels(SourcePtr, bitLength, Count);
if DiffCount > 128 then
DiffCount := 128;
if SameCount > 128 then
SameCount := 128;
if DiffCount > 0 then begin
TargetPtr^ := DiffCount - 1;
Inc(TargetPtr);
Dec(Count, DiffCount);
Inc(Result, (DiffCount * bitLength) + 1);
while DiffCount > 0 do begin
for i := 0 to bitLength - 1 do
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
end;
Dec(DiffCount);
end;
end;
if SameCount > 1 then 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 bitLength - 1 do
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
end;
end;
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/110184.html
