type TImageFormat = (unknown, BMP, GIF, JPG, PNG, PBMa, PGMa, PPMa, PBMb, PGMb, PPMb, XBM, XPM, SVG);
function GetImageFormat(Stream: TStream): TImageFormat;
function GetFormatExt(
const ImageFormat: TImageFormat):
string;
function LoadImageFromStream(Target: TPersistent; Stream: TStream): Boolean;
function GetImageFormat(Stream: TStream): TImageFormat;
var
Memory:
array[0..8]
of Byte;
begin
Result := unknown;
try
if Stream.Size < 10
then
Exit;
Stream.Position := 0;
Stream.
Read(Memory, 9);
case Memory[0]
of
80:
begin // P: Netpbm
case Memory[1]
of
49: Result := PBMa;
// 1: Bitmap ASCII
50: Result := PGMa;
// 2: Graymap ASCII
51: Result := PPMa;
// 3: Pixmap ASCII
52: Result := PBMb;
// 4: Bitmap binary
53: Result := PGMb;
// 5: Graymap binary
54: Result := PPMb;
// 6: Pixmap binary
end;
end;
66:
if Memory[1] = 77
then // BM: Bitmap
Result := BMP;
35:
if Memory[1] = 100
then // #define: XBM
if Memory[2] = 101
then
if Memory[3] = 102
then
if Memory[4] = 105
then
if Memory[5] = 110
then
if Memory[6] = 101
then
Result := XBM;
{33: if Memory[1] = 32 then // ! XPM2: XPM
if Memory[2] = 88 then
if Memory[3] = 80 then
if Memory[4] = 77 then
if Memory[5] = 50 then
Result := XPM; }
47:
if Memory[1] = 42
then // /* XPM */: XPM /*
if Memory[2] = 32
then
if Memory[3] = 88
then
if Memory[4] = 80
then
if Memory[5] = 77
then
if Memory[6] = 32
then
if Memory[7] = 42
then
if Memory[8] = 47
then
Result := XPM;
137:
if Memory[1] = 80
then // ‰PNG: PNG
if Memory[2] = 78
then
if Memory[3] = 71
then
if Memory[4] = 13
then
if Memory[5] = 10
then
if Memory[6] = 26
then
if Memory[7] = 10
then
Result := PNG;
71:
if Memory[1] = 73
then // GIF8...a
if Memory[2] = 70
then
if Memory[3] = 56
then
if Memory[5] = 97
then
case Memory[4]
of
55: Result := GIF;
// 7: GIF, old
57: Result := GIF;
// 9: GIF, new
end;
239:
if Memory[1] = 187
then // SVG, with BOM
if Memory[2] = 191
then
if Memory[3] = 60
then
Result := SVG;
60: Result := SVG;
// SVG, without BOM
255:
if Memory[1] = 216
then // ÿØ: JPEG
Result := JPG;
//47:
end;
except
end;
Stream.Position := 0;
end;
function GetFormatExt(
const ImageFormat: TImageFormat):
string;
begin
case ImageFormat
of
BMP: Result := '
BMP';
GIF: Result := '
GIF';
JPG: Result := '
JPG';
PNG: Result := '
PNG';
PBMa, PBMb: Result := '
PBM';
// don't care for ASCII/binary if netpbm
PGMa, PGMb: Result := '
PGM';
PPMa, PPMb: Result := '
PPM';
XBM: Result := '
XBM';
XPM: Result := '
XPM';
SVG: Result := '
SVG';
else Result := '
';
end;
end;
function LoadImageFromStream(Target: TPersistent; Stream: TStream): Boolean;
var
GraphicClass: TGraphicClass;
Graphic: TGraphic;
begin
//GraphicClass := nil;
Result := False;
case GetImageFormat(Stream)
of
BMP: GraphicClass := Graphics.TBitmap;
PNG: GraphicClass := TPNGImage;
JPG: GraphicClass := TJPEGImage;
GIF: GraphicClass := TGIFImage;
PBMa, PGMa, PPMa, PBMb, PGMb, PPMb: GraphicClass := TNetPBM;
XBM: GraphicClass := TXBitmap;
XPM: GraphicClass := TXPixmap;
SVG: GraphicClass := TSVGImage;
else raise EInvalidGraphic.CreateFmt(SInvalidImage, []);
end;
if not (GraphicClass =
nil)
then
begin
Graphic := GraphicClass.Create;
try
Graphic.LoadFromStream(Stream);
except
Graphic.Free;
raise;
end;
try
Target.Assign(Graphic);
Result := True;
finally
Graphic.Free;
end;
end;
end;