Re: [GM-core] please help about fileSize() function

Bob Friesenhahn <[email protected]> Fri, 4 Mar 2005 17:43:59 -0600 (CST)
Newsgroups gmane.comp.video.graphicsmagick.help,gmane.comp.video.graphicsmagick.core,gmane.comp.video.graphicsmagick.apis
Message-ID <[email protected]>
On Thu, 3 Mar 2005, alessandro bonvicini wrote:

> HI all,
> first of all I would like to thanks all of you for the really wonderfull work 
> done in graphics magicks.
> I am new to the library but I am very impressed from the features.
>
> I need your help because I don't understnd the behavior of the fileSize() 
> function.

I believe that fileSize() will only produce a correct result based on 
the last read() or ping() operation.  It does not return the size of 
the image just written.

> In my program I need to know the dimension of the file in which the image 
> will be written BEFORE the call to
> image.write, for streaming purpose.
> How can  I do this? Is it possibile?

The problem is that the file format encoder has responsibility for 
encoding the file.  When the file format is compressed, it is 
impossible to know the final file size without performing the 
encoding.  For uncompressed files, it is often possible to accurately 
estimate the final file size.  Even for uncompressed files, there may 
be associated metadata which takes up extra space, or there may be 
thresholds to encoding options (e.g. the number of bits used) which 
cause some images to take more space per pixel than others.

> And why the fileSize() returned in the end is different from what it is 
> written on disk?

It is apparently some random value at that point.

If you absolutely need to know the file size prior to streaming, then 
you can write to a temporary file, or write the image to an in-memory 
Blob, and then stream the temporary file or Blob to the client.  If 
the image is not too large, writing to an in-memory Blob is usually 
fastest.

Bob
======================================
Bob Friesenhahn
[email protected], http://www.simplesystems.org/users/bfriesen/
GraphicsMagick Maintainer,    http://www.GraphicsMagick.org/


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click