?? pngimage.pas
字號(hào):
procedure AssignTo(Dest: TPersistent);{$IFDEF UseDelphi}override;{$ENDIF}
{Assigns from a windows bitmap handle}
procedure AssignHandle(Handle: HBitmap; Transparent: Boolean;
TransparentColor: ColorRef);
{Draws the image into a canvas}
procedure Draw(ACanvas: TCanvas; const Rect: TRect);
{$IFDEF UseDelphi}override;{$ENDIF}
{Width and height properties}
property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
{Returns if the image is interlaced}
property InterlaceMethod: TInterlaceMethod read fInterlaceMethod
write fInterlaceMethod;
{Filters to test to encode}
property Filters: TFilters read fFilters write fFilters;
{Maximum size for IDAT chunks, default and minimum is 65536}
property MaxIdatSize: Cardinal read fMaxIdatSize write SetMaxIdatSize;
{Property to return if the image is empty or not}
property Empty: Boolean read GetEmpty;
{Compression level}
property CompressionLevel: TCompressionLevel read fCompressionLevel
write fCompressionLevel;
{Access to the chunk list}
property Chunks: TPngList read fChunkList;
{Object being created and destroyed}
constructor Create; {$IFDEF UseDelphi}override;{$ENDIF}
destructor Destroy; override;
{$IFNDEF UseDelphi}procedure LoadFromFile(const Filename: String);{$ENDIF}
{$IFNDEF UseDelphi}procedure SaveToFile(const Filename: String);{$ENDIF}
procedure LoadFromStream(Stream: TStream); {$IFDEF UseDelphi}override;{$ENDIF}
procedure SaveToStream(Stream: TStream); {$IFDEF UseDelphi}override;{$ENDIF}
{Loading the image from resources}
procedure LoadFromResourceName(Instance: HInst; const Name: String);
procedure LoadFromResourceID(Instance: HInst; ResID: Integer);
end;
{Chunk name object}
TChunkName = Array[0..3] of Char;
{Global chunk object}
TChunk = class
private
{Contains data}
fData: Pointer;
fDataSize: Cardinal;
{Stores owner}
fOwner: TPngObject;
{Stores the chunk name}
fName: TChunkName;
{Returns pointer to the TChunkIHDR}
function GetHeader: TChunkIHDR;
{Used with property index}
function GetIndex: Integer;
{Should return chunk class/name}
class function GetName: String; virtual;
{Returns the chunk name}
function GetChunkName: String;
public
{Returns index from list}
property Index: Integer read GetIndex;
{Returns pointer to the TChunkIHDR}
property Header: TChunkIHDR read GetHeader;
{Resize the data}
procedure ResizeData(const NewSize: Cardinal);
{Returns data and size}
property Data: Pointer read fData;
property DataSize: Cardinal read fDataSize;
{Assigns from another TChunk}
procedure Assign(Source: TChunk); virtual;
{Returns owner}
property Owner: TPngObject read fOwner;
{Being destroyed/created}
constructor Create(Owner: TPngObject); virtual;
destructor Destroy; override;
{Returns chunk class/name}
property Name: String read GetChunkName;
{Loads the chunk from a stream}
function LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
Size: Integer): Boolean; virtual;
{Saves the chunk to a stream}
function SaveToStream(Stream: TStream): Boolean; virtual;
end;
{Chunk classes}
TChunkIEND = class(TChunk); {End chunk}
{IHDR data}
pIHDRData = ^TIHDRData;
TIHDRData = packed record
Width, Height: Cardinal;
BitDepth,
ColorType,
CompressionMethod,
FilterMethod,
InterlaceMethod: Byte;
end;
{Information header chunk}
TChunkIHDR = class(TChunk)
private
{Current image}
ImageHandle: HBitmap;
ImageDC: HDC;
{Output windows bitmap}
HasPalette: Boolean;
BitmapInfo: TMaxBitmapInfo;
BytesPerRow: Integer;
{Stores the image bytes}
ImageData: pointer;
ImageAlpha: Pointer;
{Contains all the ihdr data}
IHDRData: TIHDRData;
{Resizes the image data to fill the color type, bit depth, }
{width and height parameters}
procedure PrepareImageData;
{Release allocated ImageData memory}
procedure FreeImageData;
public
{Properties}
property Width: Cardinal read IHDRData.Width write IHDRData.Width;
property Height: Cardinal read IHDRData.Height write IHDRData.Height;
property BitDepth: Byte read IHDRData.BitDepth write IHDRData.BitDepth;
property ColorType: Byte read IHDRData.ColorType write IHDRData.ColorType;
property CompressionMethod: Byte read IHDRData.CompressionMethod
write IHDRData.CompressionMethod;
property FilterMethod: Byte read IHDRData.FilterMethod
write IHDRData.FilterMethod;
property InterlaceMethod: Byte read IHDRData.InterlaceMethod
write IHDRData.InterlaceMethod;
{Loads the chunk from a stream}
function LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
Size: Integer): Boolean; override;
{Saves the chunk to a stream}
function SaveToStream(Stream: TStream): Boolean; override;
{Destructor/constructor}
constructor Create(Owner: TPngObject); override;
destructor Destroy; override;
{Assigns from another TChunk}
procedure Assign(Source: TChunk); override;
end;
{Gamma chunk}
TChunkgAMA = class(TChunk)
private
{Returns/sets the value for the gamma chunk}
function GetValue: Cardinal;
procedure SetValue(const Value: Cardinal);
public
{Returns/sets gamma value}
property Gamma: Cardinal read GetValue write SetValue;
{Loading the chunk from a stream}
function LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
Size: Integer): Boolean; override;
{Being created}
constructor Create(Owner: TPngObject); override;
{Assigns from another TChunk}
procedure Assign(Source: TChunk); override;
end;
{ZLIB Decompression extra information}
TZStreamRec2 = packed record
{From ZLIB}
ZLIB: TZStreamRec;
{Additional info}
Data: Pointer;
fStream : TStream;
end;
{Palette chunk}
TChunkPLTE = class(TChunk)
private
{Number of items in the palette}
fCount: Integer;
{Contains the palette handle}
function GetPaletteItem(Index: Byte): TRGBQuad;
public
{Returns the color for each item in the palette}
property Item[Index: Byte]: TRGBQuad read GetPaletteItem;
{Returns the number of items in the palette}
property Count: Integer read fCount;
{Loads the chunk from a stream}
function LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
Size: Integer): Boolean; override;
{Saves the chunk to a stream}
function SaveToStream(Stream: TStream): Boolean; override;
{Assigns from another TChunk}
procedure Assign(Source: TChunk); override;
end;
{Transparency information}
TChunktRNS = class(TChunk)
private
fBitTransparency: Boolean;
function GetTransparentColor: ColorRef;
{Returns the transparent color}
procedure SetTransparentColor(const Value: ColorRef);
public
{Palette values for transparency}
PaletteValues: Array[Byte] of Byte;
{Returns if it uses bit transparency}
property BitTransparency: Boolean read fBitTransparency;
{Returns the transparent color}
property TransparentColor: ColorRef read GetTransparentColor write
SetTransparentColor;
{Loads/saves the chunk from/to a stream}
function LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
Size: Integer): Boolean; override;
function SaveToStream(Stream: TStream): Boolean; override;
{Assigns from another TChunk}
procedure Assign(Source: TChunk); override;
end;
{Actual image information}
TChunkIDAT = class(TChunk)
private
{Holds another pointer to the TChunkIHDR}
Header: TChunkIHDR;
{Stores temporary image width and height}
ImageWidth, ImageHeight: Integer;
{Size in bytes of each line and offset}
Row_Bytes, Offset : Cardinal;
{Contains data for the lines}
Encode_Buffer: Array[0..5] of pByteArray;
Row_Buffer: Array[Boolean] of pByteArray;
{Variable to invert the Row_Buffer used}
RowUsed: Boolean;
{Ending position for the current IDAT chunk}
EndPos: Integer;
{Filter the current line}
procedure FilterRow;
{Filter to encode and returns the best filter}
function FilterToEncode: Byte;
{Reads ZLIB compressed data}
function IDATZlibRead(var ZLIBStream: TZStreamRec2; Buffer: Pointer;
Count: Integer; var EndPos: Integer; var crcfile: Cardinal): Integer;
{Compress and writes IDAT data}
procedure IDATZlibWrite(var ZLIBStream: TZStreamRec2; Buffer: Pointer;
const Length: Cardinal);
procedure FinishIDATZlib(var ZLIBStream: TZStreamRec2);
{Prepares the palette}
procedure PreparePalette;
protected
{Decode interlaced image}
procedure DecodeInterlacedAdam7(Stream: TStream;
var ZLIBStream: TZStreamRec2; const Size: Integer; var crcfile: Cardinal);
{Decode non interlaced imaged}
procedure DecodeNonInterlaced(Stream: TStream;
var ZLIBStream: TZStreamRec2; const Size: Integer;
var crcfile: Cardinal);
protected
{Encode non interlaced images}
procedure EncodeNonInterlaced(Stream: TStream;
var ZLIBStream: TZStreamRec2);
{Encode interlaced images}
procedure EncodeInterlacedAdam7(Stream: TStream;
var ZLIBStream: TZStreamRec2);
protected
{Memory copy methods to decode}
procedure CopyNonInterlacedRGB8(Src, Dest, Trans: pChar);
procedure CopyNonInterlacedRGB16(Src, Dest, Trans: pChar);
procedure CopyNonInterlacedPalette148(Src, Dest, Trans: pChar);
procedure CopyNonInterlacedPalette2(Src, Dest, Trans: pChar);
procedure CopyNonInterlacedGray2(Src, Dest, Trans: pChar);
procedure CopyNonInterlacedGrayscale16(Src, Dest, Trans: pChar);
procedure CopyNonInterlacedRGBAlpha8(Src, Dest, Trans: pChar);
procedure CopyNonInterlacedRGBAlpha16(Src, Dest, Trans: pChar);
procedure CopyNonInterlacedGrayscaleAlpha8(Src, Dest, Trans: PChar);
procedure CopyNonInterlacedGrayscaleAlpha16(Src, Dest, Trans: PChar);
procedure CopyInterlacedRGB8(const Pass: Byte; Src, Dest, Trans: pChar);
procedure CopyInterlacedRGB16(const Pass: Byte; Src, Dest, Trans: pChar);
procedure CopyInterlacedPalette148(const Pass: Byte; Src,Dest,Trans: pChar);
procedure CopyInterlacedPalette2(const Pass: Byte; Src, Dest, Trans: pChar);
procedure CopyInterlacedGray2(const Pass: Byte; Src, Dest, Trans: pChar);
procedure CopyInterlacedGrayscale16(const Pass: Byte;Src,Dest,Trans: pChar);
procedure CopyInterlacedRGBAlpha8(const Pass: Byte; Src,Dest,Trans: pChar);
procedure CopyInterlacedRGBAlpha16(const Pass: Byte; Src,Dest,Trans: pChar);
procedure CopyInterlacedGrayscaleAlpha8(const Pass: Byte;
Src, Dest, Trans: pChar);
procedure CopyInterlacedGrayscaleAlpha16(const Pass: Byte;
Src, Dest, Trans: pChar);
protected
{Memory copy methods to encode}
procedure EncodeNonInterlacedRGB8(Src, Dest, Trans: pChar);
procedure EncodeNonInterlacedRGB16(Src, Dest, Trans: pChar);
procedure EncodeNonInterlacedGrayscale16(Src, Dest, Trans: pChar);
procedure EncodeNonInterlacedPalette148(Src, Dest, Trans: pChar);
procedure EncodeNonInterlacedRGBAlpha8(Src, Dest, Trans: pChar);
procedure EncodeNonInterlacedRGBAlpha16(Src, Dest, Trans: pChar);
procedure EncodeNonInterlacedGrayscaleAlpha8(Src, Dest, Trans: pChar);
procedure EncodeNonInterlacedGrayscaleAlpha16(Src, Dest, Trans: pChar);
procedure EncodeInterlacedRGB8(const Pass: Byte; Src, Dest, Trans: pChar);
procedure EncodeInterlacedRGB16(const Pass: Byte; Src, Dest, Trans: pChar);
procedure EncodeInterlacedPalette148(const Pass:Byte; Src,Dest,Trans:pChar);
procedure EncodeInterlacedGrayscale16(const Pass:Byte;Src,Dest,Trans:pChar);
procedure EncodeInterlacedRGBAlpha8(const Pass: Byte;Src,Dest,Trans: pChar);
procedure EncodeInterlacedRGBAlpha16(const Pass:Byte; Src,Dest,Trans:pChar);
procedure EncodeInterlacedGrayscaleAlpha8(const Pass: Byte; Src, Dest,
Trans: pChar);
procedure EncodeInterlacedGrayscaleAlpha16(const Pass: Byte; Src, Dest,
Trans: pChar);
public
{Loads the chunk from a stream}
function LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
Size: Integer): Boolean; override;
{Saves the chunk to a stream}
function SaveToStream(Stream: TStream): Boolean; override;
end;
{Image last modification chunk}
TChunktIME = class(TChunk)
private
{Holds the variables}
fYear: Word;
fMonth, fDay, fHour, fMinute, fSecond: Byte;
public
{Returns/sets variables}
property Year: Word read fYear write fYear;
property Month: Byte read fMonth write fMonth;
property Day: Byte read fDay write fDay;
property Hour: Byte read fHour write fHour;
property Minute: Byte read fMinute write fMinute;
property Second: Byte read fSecond write fSecond;
{Loads the chunk from a stream}
function LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
Size: Integer): Boolean; override;
{Saves the chunk to a stream}
function SaveToStream(Stream: TStream): Boolean; override;
end;
{Textual data}
TChunktEXt = class(TChunk)
private
fKeyword, fText: String;
public
{Keyword and text}
property Keyword: String read fKeyword write fKeyword;
property Text: String read fText write fText;
{Loads the chunk from a stream}
function LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
Size: Integer): Boolean; override;
{Saves the chunk to a stream}
function SaveToStream(Stream: TStream): Boolean; override;
{Assigns from another TChunk}
procedure Assign(Source: TChunk); override;
end;
{Here we test if it's c++ builder or delphi version 3 or less}
{$IFDEF VER110}{$DEFINE DelphiBuilder3Less}{$ENDIF}
{$IFDEF VER100}{$DEFINE DelphiBuilder3Less}{$ENDIF}
{$IFDEF VER93}{$DEFINE DelphiBuilder3Less}{$ENDIF}
{$IFDEF VER90}{$DEFINE DelphiBuilder3Less}{$ENDIF}
{$IFDEF VER80}{$DEFINE DelphiBuilder3Less}{$ENDIF}
{Registers a new chunk class}
procedure RegisterChunk(ChunkClass: TChunkClass);
{Calculates crc}
function update_crc(crc: {$IFNDEF DelphiBuilder3Less}Cardinal{$ELSE}Integer
{$ENDIF}; buf: pByteArray; len: Integer): Cardinal;
{Invert bytes using assembly}
function ByteSwap(const a: integer): integer;
implementation
var
ChunkClasses: TPngPointerList;
{Table of CRCs of all 8-bit messages}
crc_table: Array[0..255] of Cardinal;
{Flag: has the table been computed? Initially false}
crc_table_computed: Boolean;
{Draw transparent image using transparent color}
procedure DrawTransparentBitmap(dc: HDC; srcBits: Pointer;
var srcHeader: TBitmapInfoHeader;
srcBitmapInfo: pBitmapInfo; Rect: TRect; cTransparentColor: COLORREF);
var
cColor: COLORREF;
bmAndBack, bmAndObject, bmAndMem: HBITMAP;
bmBackOld, bmObjectOld, bmMemOld: HBITMAP;
hdcMem, hdcBack, hdcObject, hdcTemp: HDC;
ptSize, orgSize: TPOINT;
OldBitmap, DrawBitmap: HBITMAP;
begin
hdcTemp := CreateCompatibleDC(dc);
// Select the bitmap
DrawBitmap := CreateDIBitmap(dc, srcHeader, CBM_INIT, srcBits, srcBitmapInfo^,
DIB_RGB_COLORS);
OldBitmap := SelectObject(hdcTemp, DrawBitmap);
// Sizes
OrgSize.x := abs(srcHeader.biWidth);
OrgSize.y := abs(srcHeader.biHeight);
ptSize.x := Rect.Right - Rect.Left; // Get width of bitmap
ptSize.y := Rect.Bottom - Rect.Top; // Get height of bitmap
// Create some DCs to hold temporary data.
hdcBack := CreateCompatibleDC(dc);
hdcObject := CreateCompatibleDC(dc);
hdcMem := CreateCompatibleDC(dc);
// Create a bitmap for each DC. DCs are required for a number of
// GDI functions.
// Monochrome DCs
bmAndBack := CreateBitmap(ptSize.x, ptSize.y, 1, 1, nil);
bmAndObject := CreateBitmap(ptSize.x, ptSize.y, 1, 1, nil);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -