Reputation:
I'm trying to write a CGI program that will output a PNG image to stdout. I can already do this from an image file (PNG or otherwise), but now I'm using Cairo to dynamically generate some image, then output it to the browser.
The problem I'm facing is this: the way Cairo writes a surface to a PNG is using one of two functions. The first is Surface::write_to_png(string filename). This doesn't work for me, since I'm not writing to a file, but to stdout. The second is Surface::write_to_png_stream( something-or-other write_func), as described here. I do not understand how this works, or even if this is what I want. Is there a better way to accomplish this, and if not, how do I use this abysmal function?
Thanks
Upvotes: 2
Views: 2503
Reputation:
For those who need the answer to this question(if you exist), I've figured it out:
Kerrek actually gets most of the credit here, but I thought I would post my results, and what ended up working. Here's the write function:
Cairo::ErrorStatus write_stdout(const unsigned char* data, unsigned int length)
{
return std::cout.write((char*)data,length)?CAIRO_STATUS_SUCCESS:CAIRO_STATUS_WRITE_ERROR;
}
Now, I don't know whether this will return CAIRO_STATUS_WRITE_ERROR on error, since I'm not sure what the return value of write
is. However, this code does work.
To call it, I used:
surface->write_to_png_stream(&write_stdout);
surface
was defined as such:
Cairo::RefPtr<Cairo::ImageSurface> surface =
Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, WIDTH, HEIGHT);
Basically, it's a normal surface. Anyways, thanks to Kerrek again, for answering, and I hope that helps someone.
Upvotes: 0
Reputation: 476950
As it says in the documentation, write a function to handle the writing:
#include <cstdio> // for stdout
Cairo::ErrorStatus my_write_func(unsigned char* data, unsigned int length)
{
return length == std::fwrite(data, length, stdout) ? CAIRO_STATUS_SUCCESS : CAIRO_STATUS_WRITE_ERROR;
}
Usage:
my_surface.write_to_png_stream(my_write_func);
Upvotes: 1