+#ifdef CAIRO_SUPPORT
+#include <cairo.h>
+
+static cairo_status_t write_cairo_to_file(void *closure,
+ const unsigned char *data,
+ unsigned int length) {
+ fwrite(data, 1, length, (FILE *) closure);
+ return CAIRO_STATUS_SUCCESS;
+}
+
+void generate_png(Universe *universe, scalar_t scale, FILE *file) {
+ const int depth = 4;
+ const int width = int(universe->width() * scale);
+ const int height = int(universe->height() * scale);
+
+ cairo_surface_t *image;
+ cairo_t* context_resource;
+ unsigned char *data;
+
+ data = new unsigned char [width * height * depth];
+
+ image = cairo_image_surface_create_for_data(data,
+ CAIRO_FORMAT_RGB24,
+ width,
+ height,
+ width * depth);
+
+ context_resource = cairo_create(image);
+
+ cairo_scale(context_resource, scale, scale);
+
+ cairo_set_source_rgb(context_resource, 1.0, 1.0, 1.0);
+
+ cairo_rectangle(context_resource, 0, 0,
+ universe->width(), universe->height());
+
+ cairo_fill(context_resource);
+
+ universe->draw(context_resource);
+
+ cairo_surface_write_to_png_stream(image, write_cairo_to_file, file);
+
+ cairo_destroy(context_resource);
+ cairo_surface_destroy(image);
+
+ delete[] data;
+}
+#endif
+