[cairo] How to get PNG Stream Size before Writing the stream

Matthias Hilbig mhilbig at gmail.com
Sat Jul 17 01:53:48 PDT 2010


Tony Rich schrieb:
> I am using the cairo_surface_write_to_png_stream() function to
> write a PNG stream to an allocated buffer - usually successfully.
>  
> The result will be an exact number of bytes, literally representing
> the generated PNG image in memory, as if it is a file. I can then
> write this stream of bytes to a database or elsewhere.
>  
> Problem is, the custom write function does not know the actual
> size of the buffer to allocate in advance, which of course would
> be the size of the PNG stream AFTER it has been created from
> the current cairo surface and context.
>  
> As a result, if I don't allocate enough space for the final image
> stream, the write function returns an error.
>  
> i.e, I have to guess a sufficiently large buffer size - which does not
> lend itself to a flexible program.
>  
> So how do we find the number of bytes that an image surface will
> contain, BEFORE writing it to a PNG stream??
>  
> This would round off a spectacular vector drawing API.
>  
> Thanks in advance if anyone can help.
>  

It may be possible if you use libpng functions directly. But I think the
easiest way would be to use a dynamic array (like std::vector in C++) to
save the bytes.



More information about the cairo mailing list