--- /dev/null
+
+////////////////////////////////////////////////////////////////////
+// START_IP_HEADER //
+// //
+// Written by Francois Fleuret //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports //
+// //
+// END_IP_HEADER //
+////////////////////////////////////////////////////////////////////
+
+#include "canvas_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;
+}
+
+CanvasCairo::CanvasCairo(scalar_t scale, int width, int height) {
+ const int actual_width = int(width * scale);
+ const int actual_height = int(height * scale);
+ const int depth = 4;
+
+ _data = new unsigned char [actual_width * actual_height * depth];
+
+ _image = cairo_image_surface_create_for_data(_data,
+ CAIRO_FORMAT_RGB24,
+ actual_width,
+ actual_height,
+ actual_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_set_source_rgb(_context_resource, 0.0, 0.0, 0.0);
+
+ cairo_rectangle(_context_resource, 0, 0, width, height);
+
+ cairo_fill(_context_resource);
+}
+
+CanvasCairo::~CanvasCairo() {
+ cairo_destroy(_context_resource);
+ cairo_surface_destroy(_image);
+ delete[] _data;
+}
+
+void CanvasCairo::set_drawing_color(scalar_t r, scalar_t g, scalar_t b) {
+ cairo_set_source_rgb(_context_resource, r, g, b);
+}
+
+void CanvasCairo::draw_polygon(int filled, int nb, scalar_t *x, scalar_t *y) {
+ cairo_set_line_width(_context_resource, 1.0);
+ cairo_move_to(_context_resource, x[0], y[0]);
+ for(int n = 0; n < nb; n++) {
+ cairo_line_to(_context_resource, x[n], y[n]);
+ }
+ cairo_close_path(_context_resource);
+
+ if(filled) {
+ cairo_stroke_preserve(_context_resource);
+ cairo_fill(_context_resource);
+ } else {
+ cairo_stroke(_context_resource);
+ }
+}
+
+void CanvasCairo::write_png(FILE *file) {
+ cairo_surface_write_to_png_stream(_image, write_cairo_to_file, file);
+}
--- /dev/null
+
+////////////////////////////////////////////////////////////////////
+// START_IP_HEADER //
+// //
+// Written by Francois Fleuret //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports //
+// //
+// END_IP_HEADER //
+////////////////////////////////////////////////////////////////////
+
+#ifndef CANVAS_CAIRO_H
+#define CANVAS_CAIRO_H
+
+#include "canvas.h"
+
+#include <stdio.h>
+#include <cairo.h>
+
+class CanvasCairo : public Canvas {
+ unsigned char *_data;
+ cairo_surface_t *_image;
+ cairo_t *_context_resource;
+
+public:
+ CanvasCairo(scalar_t scale, int width, int height);
+
+ ~CanvasCairo();
+ virtual void set_drawing_color(scalar_t r, scalar_t g, scalar_t b);
+ virtual void draw_polygon(int filled, int nb, scalar_t *x, scalar_t *y);
+ virtual void write_png(FILE *file);
+};
+
+#endif