Online
Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.079 Beiträge
Delphi 12 Athens
|
AW: wie art der daten in blob erkennen?
31. Aug 2023, 20:09
Zitat:
Naja, lesbar ist aber anders. Und ob es dabei wirklich auf die paar Millisekunden ankommt, die diese "Verbesserung" bringt, wage ich mal zu bezweifeln.
Wie oft kommt es denn vor, dass diese Prüfung 2 Millionen Mal pro Sekunde ausführen muß, um es nötig zu haben, das noch zu optimieren?
Delphi-Quellcode:
{$SCOPEDENUMS ON}
type TImageFormat = (Unknown, BMP, GIF87a, TIFF, JPEG, PNG, ICO);
{$POINTERMATH ON}
function GetBytesFormat(const AData: TBytes): TImageFormat;
var
Signature: LongWord;
begin
Result := TImageFormat.Unknown;
if Length(AData) >= 2 then
if PWord(AData)^ = $4D42 then
Result := TImageFormat.BMP;
if Length(AData) >= 4 then
case PLongWord(AData)^ of
$DBFFD8FF, $01004649, $EEFFD8FF, $00006669, $E0FFD8FF:
Result := TImageFormat.JPEG;
$E0FFD8FF:
if (Length(AData) >= 8) and ((PLongWord(AData) + 1)^ = $464A1000) then
Result := TImageFormat.JPEG;
$E1FFD8FF:
if (Length(AData) >= 6) and ((PWord(AData) + 2)^ = $7845) then
Result := TImageFormat.JPEG;
$474E5089:
if (Length(AData) >= 8) and ((PLongWord(AData) + 1)^ = $0A1A0A0D) then
Result := TImageFormat.PNG;
$38464947:
if Length(AData) >= 6 then
case (PWord(AData) + 2)^ of
$6137, $6139:
Result := TImageFormat.GIF87a;
end;
$00010000:
Result := TImageFormat.ICO;
$002A4949, $2A004D4D:
Result := TImageFormat.TIFF;
end;
end;
Lesbarer wird es jedenfalls, wenn man Daten und Code noch etwas trennt.
Code:
type
{$SCOPEDENUMS ON}
TImageFormat = (Unknown, BMP, GIF87a, TIFF, JPEG, PNG, ICO);
{$SCOPEDENUMS OFF}
TImageSignature = array[-2..7] of Byte;
const
cImageSignatures: array[0..13] of TImageSignature = (
(Ord(TImageFormat.JPEG), 4, $FF, $D8, $FF, $ DB, 0, 0, 0, 0),
(Ord(TImageFormat.JPEG), 8, $FF, $D8, $FF, $E0, $00, $10, $4A, $46),
(Ord(TImageFormat.JPEG), 4, $49, $46, $00, $01, 0, 0, 0, 0),
(Ord(TImageFormat.JPEG), 4, $FF, $D8, $FF, $EE, 0, 0, 0, 0),
(Ord(TImageFormat.JPEG), 6, $FF, $D8, $FF, $E1, $45, $78, 0, 0),
(Ord(TImageFormat.JPEG), 4, $69, $66, $00, $00, 0, 0, 0, 0),
(Ord(TImageFormat.JPEG), 4, $FF, $D8, $FF, $E0, 0, 0, 0, 0),
(Ord(TImageFormat.PNG), 8, $89, $50, $4E, $47, $0D, $0A, $1A, $0A),
(Ord(TImageFormat.GIF87a), 6, $47, $49, $46, $38, $37, $61, 0, 0),
(Ord(TImageFormat.GIF87a), 6, $47, $49, $46, $38, $39, $61, 0, 0),
(Ord(TImageFormat.BMP), 2, $42, $4D, 0, 0, 0, 0, 0, 0),
(Ord(TImageFormat.ICO), 4, $00, $00, $01, $00, 0, 0, 0, 0),
(Ord(TImageFormat.TIFF), 4, $49, $49, $2A, $00, 0, 0, 0, 0),
(Ord(TImageFormat.TIFF), 4, $4D, $4D, $00, $2A, 0, 0, 0, 0)
);
function GetBytesFormat(const Data: TBytes): TImageFormat;
begin
for var Signature: TImageSignature in cImageSignatures do
if (Length(Data) >= Signature[-1]) and CompareMem(@Data[0], @Signature[0], Signature[-1]) then
Exit(TImageFormat(Signature[-2]));
Result := TImageFormat.Unknown;
end;
Delphi-Quellcode:
{$SCOPEDENUMS ON}
type
TImageFormat = (Unknown, BMP, GIF87a, TIFF, JPEG, PNG, ICO);
{$SCOPEDENUMS OFF}
var
cImageSignatures: array of array of Byte = [
[Ord(TImageFormat.JPEG), $FF, $D8, $FF, $ DB],
[Ord(TImageFormat.JPEG), $FF, $D8, $FF, $E0, $00, $10, $4A, $46],
[Ord(TImageFormat.JPEG), $49, $46, $00, $01],
[Ord(TImageFormat.JPEG), $FF, $D8, $FF, $EE],
[Ord(TImageFormat.JPEG), $FF, $D8, $FF, $E1, $45, $78],
[Ord(TImageFormat.JPEG), $69, $66, $00, $00],
[Ord(TImageFormat.JPEG), $FF, $D8, $FF, $E0],
[Ord(TImageFormat.PNG), $89, $50, $4E, $47, $0D, $0A, $1A, $0A],
[Ord(TImageFormat.GIF87a), $47, $49, $46, $38, $37, $61],
[Ord(TImageFormat.GIF87a), $47, $49, $46, $38, $39, $61],
[Ord(TImageFormat.BMP), $42, $4D],
[Ord(TImageFormat.ICO), $00, $00, $01, $00],
[Ord(TImageFormat.TIFF), $49, $49, $2A, $00],
[Ord(TImageFormat.TIFF), $4D, $4D, $00, $2A]
];
function GetBytesFormat( const Data: TBytes): TImageFormat;
begin
for var Signature in cImageSignatures do
if (Length(Data) >= Length(Signature)-1) and CompareMem(@Data[0], @Signature[1], Length(Signature)-1) then
Exit(TImageFormat(Signature[0]));
Result := TImageFormat.Unknown;
end;
Neuste Erkenntnis:
Seit Pos einen dritten Parameter hat,
wird PoSex im Delphi viel seltener praktiziert.
Geändert von himitsu (31. Aug 2023 um 20:26 Uhr)
|