我在网上找到了这个,但我不知道我该怎么用啊?
该怎么引入一段数据流让它解压出来呢?解压以后的数据我怎么得到呢?
求大侠救救命,我DELPHI是赶鸭子上架啊
const // LZW encoding and decoding support
NoLZWCode = 4096;type
PByte = ^Byte;
TByteStream = array[0..MaxInt - 1] of Byte;
PByteStream = ^TByteStream; // abstract decoder class to define the base functionality of an encoder/decoder
TDecoder = class
public
procedure Decode(var Source: Pointer; Dest: Pointer; PackedSize, UnpackedSize: Integer); virtual; abstract;
procedure Encode(Source, Dest: Pointer; var FByteCounts: Cardinal); virtual; abstract;
end; // Note: We need a different LZW decoder class for GIF because the bit order is reversed compared to that
// of TIFF and the code size increment is handled slightly different.
TGIFLZW = class(TDecoder)
private
FCodeSize: Cardinal;
FCodeMask: Cardinal;
FFreeCode: Cardinal;
FOldCode: Cardinal;
FPrefix: array[0..4095] of Cardinal; // LZW prefix
FSuffix, // LZW suffix
FStack: array [0..4095] of Byte; // stack
FStackPointer: PByte;
FTarget: PByte;
FFirstChar: Byte; // buffer for decoded byte
FClearCode,
FEOICode: Word;
function DecodeLZW(Code: Cardinal): Boolean;
public
InitialCodeSize: Byte; // must be set before decoding is started!
procedure Decode(var Source: Pointer; Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; var FByteCounts: Cardinal); override;
end;implementation
function TGIFLZW.DecodeLZW(Code: Cardinal): Boolean;var
InCode: Cardinal; // buffer for passed codebegin
// handling of clear codes
if Code = FClearCode then
begin
// reset of all variables
FCodeSize := InitialCodeSize + 1;
FCodeMask := (1 shl FCodeSize) - 1;
FFreeCode := FClearCode + 2;
FOldCode := NoLZWCode;
Result := True;
Exit;
end; // check whether it is a valid, already registered code
if Code > FFreeCode then
raise Exception.Create('GIF LZW: invalid opcode.');
// handling for the first LZW code: print and keep it
if FOldCode = NoLZWCode then
begin
FFirstChar := FSuffix[Code];
FTarget^ := FFirstChar;
Inc(FTarget);
FOldCode := Code;
Result := True;
Exit;
end; // keep the passed LZW code
InCode := Code; // the first LZW code is always smaller than FFirstCode
if Code = FFreeCode then
begin
FStackPointer^ := FFirstChar;
Inc(FStackPointer);
Code := FOldCode;
end; // loop to put decoded bytes onto the stack
while Code > FClearCode do
begin
FStackPointer^ := FSuffix[Code];
Inc(FStackPointer);
Code := FPrefix[Code];
end; // place new code into code table
FFirstChar := FSuffix[Code];
FStackPointer^ := FFirstChar;
Inc(FStackPointer);
FPrefix[FFreeCode] := FOldCode;
FSuffix[FFreeCode] := FFirstChar; // increase code size if necessary
if (FFreeCode = FCodeMask) and
(FCodeSize < 12) then
begin
Inc(FCodeSize);
FCodeMask := (1 shl FCodeSize) - 1;
end;
if FFreeCode < 4095 then Inc(FFreeCode); // put decoded bytes (from the stack) into the target buffer
FOldCode := InCode;
repeat
Dec(FStackPointer);
FTarget^ := FStackPointer^;
Inc(FTarget);
until FStackPointer = @FStack; Result := True;
end;//----------------------------------------------------------------------------------------------------------------------procedure TGIFLZW.Decode(var Source: Pointer; Dest: Pointer; PackedSize, UnpackedSize: Integer);var
I: Integer;
Data, // current data
Bits, // counter for bit management
Code: Cardinal; // current code value
SourcePtr: PByte;begin
FTarget := Dest;
SourcePtr := Source; // initialize parameter
FCodeSize := InitialCodeSize + 1;
FClearCode := 1 shl InitialCodeSize;
FEOICode := FClearCode + 1;
FFreeCode := FClearCode + 2;
FOldCode := NoLZWCode;
FCodeMask := (1 shl FCodeSize) - 1; // init code table
for I := 0 to FClearCode - 1 do
begin
FPrefix[I] := NoLZWCode;
FSuffix[I] := I;
end; // initialize stack
FStackPointer := @FStack; Data := 0;
Bits := 0;
while PackedSize > 0 do
begin
// read code from bit stream
Inc(Data, SourcePtr^ shl Bits);
Inc(Bits, 8);
while Bits >= FCodeSize do
begin
// current code
Code := Data and FCodeMask;
// prepare next run
Data := Data shr FCodeSize;
Dec(Bits, FCodeSize); // EOICode -> decoding finished, check also for badly written codes and
// terminate the loop as soon as the target is filled up
if (Code = FEOICode) or
((PChar(FTarget) - PChar(Dest)) >= UnpackedSize) then Exit; if not DecodeLZW(Code) then Break;
end;
Inc(SourcePtr);
Dec(PackedSize);
end;
end;
该怎么引入一段数据流让它解压出来呢?解压以后的数据我怎么得到呢?
求大侠救救命,我DELPHI是赶鸭子上架啊
const // LZW encoding and decoding support
NoLZWCode = 4096;type
PByte = ^Byte;
TByteStream = array[0..MaxInt - 1] of Byte;
PByteStream = ^TByteStream; // abstract decoder class to define the base functionality of an encoder/decoder
TDecoder = class
public
procedure Decode(var Source: Pointer; Dest: Pointer; PackedSize, UnpackedSize: Integer); virtual; abstract;
procedure Encode(Source, Dest: Pointer; var FByteCounts: Cardinal); virtual; abstract;
end; // Note: We need a different LZW decoder class for GIF because the bit order is reversed compared to that
// of TIFF and the code size increment is handled slightly different.
TGIFLZW = class(TDecoder)
private
FCodeSize: Cardinal;
FCodeMask: Cardinal;
FFreeCode: Cardinal;
FOldCode: Cardinal;
FPrefix: array[0..4095] of Cardinal; // LZW prefix
FSuffix, // LZW suffix
FStack: array [0..4095] of Byte; // stack
FStackPointer: PByte;
FTarget: PByte;
FFirstChar: Byte; // buffer for decoded byte
FClearCode,
FEOICode: Word;
function DecodeLZW(Code: Cardinal): Boolean;
public
InitialCodeSize: Byte; // must be set before decoding is started!
procedure Decode(var Source: Pointer; Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; var FByteCounts: Cardinal); override;
end;implementation
function TGIFLZW.DecodeLZW(Code: Cardinal): Boolean;var
InCode: Cardinal; // buffer for passed codebegin
// handling of clear codes
if Code = FClearCode then
begin
// reset of all variables
FCodeSize := InitialCodeSize + 1;
FCodeMask := (1 shl FCodeSize) - 1;
FFreeCode := FClearCode + 2;
FOldCode := NoLZWCode;
Result := True;
Exit;
end; // check whether it is a valid, already registered code
if Code > FFreeCode then
raise Exception.Create('GIF LZW: invalid opcode.');
// handling for the first LZW code: print and keep it
if FOldCode = NoLZWCode then
begin
FFirstChar := FSuffix[Code];
FTarget^ := FFirstChar;
Inc(FTarget);
FOldCode := Code;
Result := True;
Exit;
end; // keep the passed LZW code
InCode := Code; // the first LZW code is always smaller than FFirstCode
if Code = FFreeCode then
begin
FStackPointer^ := FFirstChar;
Inc(FStackPointer);
Code := FOldCode;
end; // loop to put decoded bytes onto the stack
while Code > FClearCode do
begin
FStackPointer^ := FSuffix[Code];
Inc(FStackPointer);
Code := FPrefix[Code];
end; // place new code into code table
FFirstChar := FSuffix[Code];
FStackPointer^ := FFirstChar;
Inc(FStackPointer);
FPrefix[FFreeCode] := FOldCode;
FSuffix[FFreeCode] := FFirstChar; // increase code size if necessary
if (FFreeCode = FCodeMask) and
(FCodeSize < 12) then
begin
Inc(FCodeSize);
FCodeMask := (1 shl FCodeSize) - 1;
end;
if FFreeCode < 4095 then Inc(FFreeCode); // put decoded bytes (from the stack) into the target buffer
FOldCode := InCode;
repeat
Dec(FStackPointer);
FTarget^ := FStackPointer^;
Inc(FTarget);
until FStackPointer = @FStack; Result := True;
end;//----------------------------------------------------------------------------------------------------------------------procedure TGIFLZW.Decode(var Source: Pointer; Dest: Pointer; PackedSize, UnpackedSize: Integer);var
I: Integer;
Data, // current data
Bits, // counter for bit management
Code: Cardinal; // current code value
SourcePtr: PByte;begin
FTarget := Dest;
SourcePtr := Source; // initialize parameter
FCodeSize := InitialCodeSize + 1;
FClearCode := 1 shl InitialCodeSize;
FEOICode := FClearCode + 1;
FFreeCode := FClearCode + 2;
FOldCode := NoLZWCode;
FCodeMask := (1 shl FCodeSize) - 1; // init code table
for I := 0 to FClearCode - 1 do
begin
FPrefix[I] := NoLZWCode;
FSuffix[I] := I;
end; // initialize stack
FStackPointer := @FStack; Data := 0;
Bits := 0;
while PackedSize > 0 do
begin
// read code from bit stream
Inc(Data, SourcePtr^ shl Bits);
Inc(Bits, 8);
while Bits >= FCodeSize do
begin
// current code
Code := Data and FCodeMask;
// prepare next run
Data := Data shr FCodeSize;
Dec(Bits, FCodeSize); // EOICode -> decoding finished, check also for badly written codes and
// terminate the loop as soon as the target is filled up
if (Code = FEOICode) or
((PChar(FTarget) - PChar(Dest)) >= UnpackedSize) then Exit; if not DecodeLZW(Code) then Break;
end;
Inc(SourcePtr);
Dec(PackedSize);
end;
end;
解决方案 »
- 这一行总是出错,
- 在DEPHI中如何将ScriptErrorsSuppressed的属性添加到TWebBrowser上
- 如何得到操作系统里数据库所有驱动程序的名称?
- 这几天天气冷冷的.散分散分.热乎一下.
- 在数据库中如何设置字段的类型,在delphi感知控件中显示为dbcheckbox类型?
- 救急:如何用DELPHI复制引导扇区
- 如何获得指定目录下的文件列表
- 再问关于远程连接Oracle?ADO ? ODBC ? BDE ?
- delphi中加了事务处理,速度慢,数据库又被锁住的问题,请高手
- 怎样才能入ORACLE
- 使用IdFTP组件无法上载文件到Unix上的FTP站点?
- flatstyle控件的安装有问题??找不到.dcu!求救~~
procedure Encode(Source, Dest: Pointer; var FByteCounts: Cardinal); override; 看到里面的VAR参数没?返回值就在那里.
Source:pchar;//字符指针;
Dest:pchar;
或
Source:pInteger;
Dest:pInteger;
看下你的数据类型定义吧,然后开辟缓冲区存贮你的数据,用指针指向该缓冲区.具体的我没仔细读你的程序,大概思路就是这样
procedure Encode(Source, Dest: Pointer; var FByteCounts: Cardinal); override;var
Source: array[0..1000] of char;
Dest: array[0..1000] of char;
pSource: Pointer;
pDest: Pointer;
btCounts: Cardinal; pSource := (Pointer)@Source;
pDest := (Pointer)@Dest;
Decode(pSource, pDest, 100, 100); 后面的参数看你传了。。 Encode(pSouce, pDest, btCounts);just try it.
var
ms,mt : TMemoryStream;
i : Integer;
P1,P2: Pointer;
Myclass:TGIFLZW;
begin
ms:= TMemoryStream.Create;
mt:= TMemoryStream.Create;
mt.SetSize(5078);
ms.LoadFromFile('5.gif');
P1:= Pchar(ms);
P2:=PChar(mt);
MyClass := TGIFLZW.Create;
MyClass.Decode(P1,P2,8,256);
MyClass.Destroy;
ms.Free;
mt.free;
end
你的方法怎么有很多错误呢?
pSource := (Pointer)@Source; 它说这样不行?
Decode(pSource, pDest, 100, 100);它说没有Decode?
pSource := Pointer(@Source);
pDest := Pointer(@Dest);Decode(pSource, pDest, 100, 100);它说没有Decode?
你上面不是有的么你use 这个unit没
但是,我给Source赋值,我查看了运行后的Dest没有得到正确的数据啊