On 5/30/11, Ben ben.smith.li...@gmail.com wrote:
The reason I'm reading in the jpg files is so I can get their Width and
Height values. Avg. jpeg file sizes are between 35-45KB, and largest
file size is 65KB.
Ben.
A bit off-topic (as to the inner workings of FPImage), but I attached
some old code of mine to determine width/height of jpg, gif, bmp, png.
It's probably a little faster...
Bart
unit PicsLib;
{ $DEFINE DEBUG}
interface
uses Windows;
function GetBMPSize(const Fn: String; var Width, Height: dword): Boolean;
function GetGIFSize(const Fn: String; var Width, Height: dword): Boolean;
function GetJPGSize(const Fn: String; var Width, Height: dword): Boolean;
function GetPNGSize(const Fn: String; var Width, Height: dword): Boolean;
function GetImageSize(const Fn: String; var Width, Height: dword): Boolean;
implementation
uses SysUtils, Classes;
type TBitmapFileHeader = Packed Record
ID: word;
FileSize: dword;
Reserved: dword;
BitmapDataOffset: dword;
end;
TBitmapInfo= Packed Record
BitmapHeaderSize: dword;
Width: dword;
Height: dword;
Planes: word;
BitsPerPixel: word;
Compression: dword;
BitmapDataSize: dword;
XpelsPerMeter: dword;
YPelsPerMeter: dword;
ColorsUsed: dword;
ColorsImportant: dword;
end;
TGIFHeader= Packed Record
ID: array[0..5] of char;
Width, Height: Word;
end;
TPNGHeader= Packed Record
ID: array[0..7] of Char;
ChunkLength: dword;
ChunkType: array[0..3] of Char;
Width: dword;
Height: dword;
BitsPerPixel: byte;
ColorType: byte;
Compression: byte;
FilterMethod: byte;
CRC: dword;
end;
TJPGHeader= array[0..1] of Byte; //FFD8 = StartOfImage (SOI)
(*
TJFIFHeader = Packed record //APP0 Header
Len: Word;
ID: array[0..4] of Char; //JFIF#0
// Maj, Min: Byte;
// XYUnits: byte;
// XDens, YDens: word;
// ThWidth, ThHeight: byte; //Thumbnail info
end;
*)
TSOFHeader= Packed record
Len: word;
DataPrecision: byte;
Height, Width: word;
NrComponents: byte;
end;
function MotorolaToIntelDW(DW: dword): dword;
var HiWd, LoWd: word;
begin
HiWd := HiWord(DW);
LoWd := LoWord(DW);
HiWd := Swap(HiWd);
LoWd := Swap(LoWd);
Result := HiWd + (LoWd shl 16);
end;
function GetImageSize(const Fn: String; var Width, Height: dword): Boolean;
begin
if AnsiUpperCase(ExtractFileExt(Fn)) = '.BMP' then
begin
Result := GetBMPSize(Fn, Width, Height);
end
else if AnsiUpperCase(ExtractFileExt(Fn)) = '.GIF' then
begin
Result := GetGIFSize(Fn, Width, Height);
end
else if (AnsiUpperCase(ExtractFileExt(Fn)) = '.JPG')
or (AnsiUpperCase(ExtractFileExt(Fn)) = '.JPEG') then
begin
Result := GetJPGSize(Fn, Width, Height);
end
else if AnsiUpperCase(ExtractFileExt(Fn)) = '.PNG' then
begin
Result := GetPNGSize(Fn, Width, Height);
end
else
begin
Width := 0;
Height := 0;
Result := False;
end;
end;
function GetBMPSize(const Fn: String; var Width, Height: dword): Boolean;
var BitmapFileHeader: TBitmapFileHeader;
BitmapInfo: TBitmapInfo;
F: File;
bRead: Integer;
IDStr: String;
begin
Result := False;
Width := 0;
Height := 0;
Try
AssignFile(F,Fn);
FileMode := fmOpenRead or fmShareDenyWrite;
Reset(F,1);
BlockRead(F,BitmapFileHeader,SizeOf(TBitmapFileHeader),bRead);
if bRead SizeOf(TBitmapFileHeader) then Raise EInOutError.Create('');
BlockRead(F,BitmapInfo,SizeOf(TBitmapInfo),bRead);
if bRead SizeOf(TBitmapInfo) then Raise EInOutError.Create('');
CloseFile(F);
IDStr := Char(Lo(BitmapFileHeader.ID)) + Char(Hi(BitmapFileHeader.ID));
//Klopt bestandsopmaak ?
if (not (IDStr = 'BM') or (IDStr = 'BA')) or
(not (BitmapInfo.BitmapHeaderSize in [$28,$0c,$f0])) or
(not (BitmapInfo.BitsPerPixel in [1,4,8,16,24,32])) then Exit;
Width := BitmapInfo.Width;