BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Get size of an image
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
QS Computing
Guest





PostPosted: Thu Jan 27, 2005 11:35 am    Post subject: Get size of an image Reply with quote



I'm looking for some code that will allow me to get the pixel
dimensions of an image in JPG, PNG or GIF format in Delphi 7 Personal.
Obviously I can load a JPG into a TImage with AutoSize=true and check
the size of the TImage afterwards, but that won't work with PNG or
GIF. Any ideas?

Thanks,
- QS Computing.
Back to top
Maarten Wiltink
Guest





PostPosted: Thu Jan 27, 2005 2:08 pm    Post subject: Re: Get size of an image Reply with quote



"QS Computing" <qscomputing (AT) gmail (DOT) com> wrote


Quote:
I'm looking for some code that will allow me to get the pixel
dimensions of an image in JPG, PNG or GIF format in Delphi 7 Personal.
Obviously I can load a JPG into a TImage with AutoSize=true and check
the size of the TImage afterwards, but that won't work with PNG or
GIF. Any ideas?

Loading the entire image to know its size is overkill. Just find out
the file format, load the first sixteen bytes or so, and get the ones
that are the size. It's not as easy, but it saves rather a lot on
bloat if you're not otherwise loading the entire image.

Groetjes,
Maarten Wiltink



Back to top
Dodgy
Guest





PostPosted: Thu Jan 27, 2005 3:07 pm    Post subject: Re: Get size of an image Reply with quote



On 27 Jan 2005 03:35:46 -0800, [email]qscomputing (AT) gmail (DOT) com[/email] (QS Computing)
waffled on about something:

Quote:
I'm looking for some code that will allow me to get the pixel
dimensions of an image in JPG, PNG or GIF format in Delphi 7 Personal.
Obviously I can load a JPG into a TImage with AutoSize=true and check
the size of the TImage afterwards, but that won't work with PNG or
GIF. Any ideas?

Thanks,
- QS Computing.

Do as Maarten suggests. The file format specs can be found via
www.wotsit.org

Basically a GIF will always have GIF as the first 3 chars of the file.
Followed by the version number of the GIF format. This doesn't matter
though, as the width/height are always in the same place IIRC.
The width is at offset 6,7 (low byte high byte)
The height is at offset 8,9 (low/high)

The jpeg mehod is a little trickier.

Add efg to your delphi bookmarks.

http://www.efg2.com/Lab/Library/Delphi/Graphics/FileFormatsAndConversion.htm

D0d6y.
--
MUSHROOMS ARE THE OPIATE OF THE MOOSES

Back to top
qscomputing@gmail.com
Guest





PostPosted: Thu Jan 27, 2005 6:50 pm    Post subject: Re: Get size of an image Reply with quote

@Dodgy: OK, so how do I read the binary data in a file like this?

Back to top
Maarten Wiltink
Guest





PostPosted: Thu Jan 27, 2005 8:59 pm    Post subject: Re: Get size of an image Reply with quote

<qscomputing (AT) gmail (DOT) com> wrote


Quote:
@Dodgy: OK, so how do I read the binary data in a file like this?

Do you mind terribly if I butt in?

Using a TFileStream, you can Seek to the right place and ReadBuffer
straight into a variable.

Using an untyped file, BlockRead those sixteen bytes into a buffer
and Move bytes into the same variable.

Using a typed file (for which you need to declare a record type),
you can read those same sixteen bytes, or however many, into a
record type variable and have the interesting bits (bytes) available
as fields in it.

Groetjes,
Maarten Wiltink



Back to top
QS Computing
Guest





PostPosted: Fri Jan 28, 2005 11:30 am    Post subject: Re: Get size of an image Reply with quote

Quote:
Do you mind terribly if I butt in?

<g> Not at all!

I looked at the page on efg and found a sample program that reads
header information from JPEGs. I think I can adapt this just to read
the info I'm looking for and to read the width and height from a GIF.
Now the problem I have is with PNG. I looked on wotsit.org and couldn't
find anything useful?

Any suggestions?

TIA,
- QS Computing.


Back to top
Maarten Wiltink
Guest





PostPosted: Fri Jan 28, 2005 6:48 pm    Post subject: Re: Get size of an image Reply with quote

"QS Computing" <qscomputing (AT) gmail (DOT) com> wrote


Quote:
Do you mind terribly if I butt in?

g> Not at all!

I looked at the page on efg and found a sample program that reads
header information from JPEGs. I think I can adapt this just to read
the info I'm looking for and to read the width and height from a GIF.
Now the problem I have is with PNG. I looked on wotsit.org and
couldn't find anything useful?

http://www.libpng.org/pub/png/spec/1.2/PNG-Contents.html

Groetjes,
Maarten Wiltink




Back to top
QS Computing
Guest





PostPosted: Fri Jan 28, 2005 10:45 pm    Post subject: Re: Get size of an image Reply with quote

OK, I've tried out some code for JPEGs that works fine. Then, using the
info from Wotsit's Format, I've tried to edit the code to read from a
GIF, but it isn't working. I think I'm just reading in the wrong place,
but I'm not sure. (code below) Any suggestions?

TIA,
- QS Computing.

Code is as follows (trimmed):

inferface
....
type
ByteRA = array [1..1] of byte;
Bytep = ^ByteRA;
....
implementation

function GetFileSize(AFileName: string): integer;
var FileStream: TFileStream;
begin
try
FileStream:=TFileStream.Create(AFileName, fmOPENREAD +
fmSHAREDENYNONE);
Result:=FileStream.Size;
FileStream.Free();
except
Result:=(-1);
end;

end;

function ReadWord(const lRawRA: byteP; lRawPos: integer): word;
var
lbtL1, lbtL2: byte;
begin
lbtL1 := lRawRA[lRawPos];
Inc(lRawPos);
lbtL2 := lRawRA[lRawPos];
Result := (256 * lbtL1 + lbtL2)
end;

procedure GetJPEGDimensions(AFileName: string; var Width, Height,
FileSize: integer);
var Size: integer;
RawRA: byteP;
InFile: file;
begin
Size:=GetFileSize(AFileName);
try
AssignFile(InFile, AFileName);
Reset(InFile, 1);
GetMem(RawRA, 170);
BlockRead(InFile, RawRA^, 170);
CloseFile(InFile);
Height:=ReadWord(RawRA, 164);
Width:=ReadWord(RawRA, 166);
FileSize:=Size;
FreeMem(RawRA);
except
FileSize:=(-1);
Width:=(-1);
Height:=(-1);
end;
end;

procedure GetGIFDimensions(AFileName: string; var Width, Height,
FileSize: integer);
var Size: integer;
RawRA: byteP;
InFile: file;
begin
Size:=GetFileSize(AFileName);
try
AssignFile(InFile, AFileName);
Reset(InFile, 1);
GetMem(RawRA, 20);
BlockRead(InFile, RawRA^, 20);
CloseFile(InFile);
Width:=ReadWord(RawRA, 6);
Height:=ReadWord(RawRA, Cool;
FileSize:=Size;
FreeMem(RawRA);
except
FileSize:=(-1);
Width:=(-1);
Height:=(-1);
end;
end;

Back to top
J French
Guest





PostPosted: Sat Jan 29, 2005 8:02 am    Post subject: Re: Get size of an image Reply with quote

On 28 Jan 2005 14:45:10 -0800, "QS Computing" <qscomputing (AT) gmail (DOT) com>
wrote:

Quote:
OK, I've tried out some code for JPEGs that works fine. Then, using the
info from Wotsit's Format, I've tried to edit the code to read from a
GIF, but it isn't working. I think I'm just reading in the wrong place,
but I'm not sure. (code below) Any suggestions?

TIA,
- QS Computing.

Code is as follows (trimmed):
Ouch


Why don't you simply read the header of the file into a Record
directly from the TFileStream ?



Back to top
QS Computing
Guest





PostPosted: Sat Jan 29, 2005 9:47 am    Post subject: Re: Get size of an image Reply with quote

J French wrote on Saturday 29 Jan 2005 08:02:
Quote:
Why don't you simply read the header of the file into a Record
directly from the TFileStream ?
Could you tell me how to do this please, or point me to some useful info on

the web.

Sorry if I seem like a newbie here - I haven't programmed anything like this
before!

--
QS Computing
http://www.qscomputing.plus.com
[email]postmaster (AT) qscomputing (DOT) plus.com[/email]

Back to top
J French
Guest





PostPosted: Sat Jan 29, 2005 11:08 am    Post subject: Re: Get size of an image Reply with quote

On Sat, 29 Jan 2005 09:47:18 +0000, QS Computing
<postmaster (AT) qscomputing (DOT) plus.com> wrote:

Quote:
J French wrote on Saturday 29 Jan 2005 08:02:
Why don't you simply read the header of the file into a Record
directly from the TFileStream ?
Could you tell me how to do this please, or point me to some useful info on
the web.

I could see that one coming !

Here you go, I've not bothered with error handling or checking that it
really is a GIF file

Type TGifHeader = Packed Record
Sig :Array[0 ..2] Of Char;
Ver :Array[0 ..2] Of Char;
PixW :WORD;
PixH :WORD;
End;

Function GifStats( Const FileSpec :String ;
Var PixW, PixH, FileSize :Integer ):Boolean;
Var
FS :TFileStream;
HeaderRec :TGifHeader;
Begin
Result := False;
If Not FileExists( FileSpec ) Then Exit;
//
FS := TFileStream.Create( FileSpec, fmOpenRead Or fmShareDenyNone );
FileSize := FS.Size;
FS.Read( HeaderRec, SizeOf( HeaderRec ) );
FS.Free;

// See what we have read
ShowMessage( HeaderRec.Sig + #13 + HeaderRec.Ver );

PixW := HeaderRec.PixW;
PixH := HeaderRec.PixH;
Result := True;

End;

procedure TForm1.Button1Click(Sender: TObject);
Var
W, H, FileSize :Integer;
begin
If Not GifStats( 'c:downloadlogo.gif', W, H, FileSize ) Then
Begin
ShowMessage( 'Check file name' );
Exit; // Function Failed
End;
//
ShowMessage( IntToStr(W) + ' ' + IntToStr(H) +
#13 + IntToStr(FileSize) );
end;

======================================
Ok, getting the file size like that is Ok, but it is a PITA to open a
file if one just wants to get its size
- also it is asking for problems if the file is locked

Here is how I do it :-

Function FileLen( Fle:String ):Integer;
var
L9 : Integer;
Handle: THandle;
FindData: TWin32FindData;
begin
Result := -1;
{ Check for '*' and '?' }
For L9 := 1 To Length( Fle ) Do
If ( Fle[L9] = '*' ) Or ( Fle[L9] = '?' ) Then
Exit;

Handle := FindFirstFile(PChar(Fle), FindData);
if Handle <> INVALID_HANDLE_VALUE then
begin
Windows.FindClose(Handle);
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0
then
begin
Result := FindData.nFileSizeLow ; // Note - Up to 2 Gb
Exit;
end;
end;

End;




Back to top
QS Computing
Guest





PostPosted: Mon Jan 31, 2005 11:24 am    Post subject: Re: Get size of an image Reply with quote

That's great thanks!

Any ideas how to do something similar for PNG?

Back to top
J French
Guest





PostPosted: Mon Jan 31, 2005 2:17 pm    Post subject: Re: Get size of an image Reply with quote

On 31 Jan 2005 03:24:45 -0800, "QS Computing" <qscomputing (AT) gmail (DOT) com>
wrote:

Quote:
That's great thanks!

Any ideas how to do something similar for PNG?

You go off and find me a website with the PNG format
- and I'll run up another example

I trust you have implemented the method I posted for BMP and JPG

The real point is that you need to forget about all the old native
Pascal file access stuff
- Wurth simply got it wrong
- the BCD/Unix (?) people got it right with Streams

Those were nicked by MSDOS in (IRRC) MSDOS 3.0 (?? 2.1 ??) when they
augmented File Control Blocks with something a lot less block headed.

It all goes back to the very old days when programmers were acutely
aware of the physical layout of the disk drives.

Learn about TStream - and you'll be impressed by how well the concept
has been implemented in Delphi


Back to top
Maarten Wiltink
Guest





PostPosted: Mon Jan 31, 2005 2:30 pm    Post subject: Re: Get size of an image Reply with quote

"J French" <erewhon (AT) nowhere (DOT) uk> wrote

Quote:
On 31 Jan 2005 03:24:45 -0800, "QS Computing" wrote:

Any ideas how to do something similar for PNG?

You go off and find me a website with the PNG format
- and I'll run up another example

I posted a link to one. PNG is fairly easy at this level; an
eight-byte file header followed by several chunks. The first
chunk is always IHDR and starts with a four-byte header ("IHDR")
and image size as two signed 32-bit integers. (All this IIRC.)


[...]
Quote:
The real point is that you need to forget about all the old native
Pascal file access stuff
- Wurth simply got it wrong

Who is this Wurth character? Wirth's design for Pascal I/O wasn't
all that bad if you see it as a platform-independent abstraction of
punch card I/O. Which it was.

Groetjes,
Maarten Wiltink



Back to top
Marco van de Voort
Guest





PostPosted: Mon Jan 31, 2005 2:56 pm    Post subject: Re: Get size of an image Reply with quote

On 2005-01-31, J French <erewhon (AT) nowhere (DOT) uk> wrote:
Quote:
On 31 Jan 2005 03:24:45 -0800, "QS Computing" wrote:

That's great thanks!

Any ideas how to do something similar for PNG?

You go off and find me a website with the PNG format
- and I'll run up another example

No need, see the fpimage collection:
http://www.freepascal.org/cgi-bin/viewcvs.cgi/fpc/fcl/image/

Free (LGPL), used enough to be sure that it works.

Quote:
The real point is that you need to forget about all the old native
Pascal file access stuff
- Wurth simply got it wrong

Assuming you mean Wirth. What exactly did he get wrong.

Quote:
- the BCD/Unix (?) people got it right with Streams

BCD=BSD I assume. BSD doesn't have streams, and SysV OS level streams are
different from application streams.

Quote:
Those were nicked by MSDOS in (IRRC) MSDOS 3.0 (?? 2.1 ??) when they
augmented File Control Blocks with something a lot less block headed.

POSIX read/write are exactly the same in functionality as blockread/write.
The only artefact is the default size of an untyped file. So what are you
actually trying to say?

Quote:
Learn about TStream - and you'll be impressed by how well the concept
has been implemented in Delphi

Dog slow for large amounts of data, unless you buffer, and then the result is
the same as blockread/write (what TStream calls internally probably anyway)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.