3 * dyncnn is a deep-learning algorithm for the prediction of
4 * interacting object dynamics
6 * Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of dyncnn.
11 * dyncnn is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * dyncnn is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with dyncnn. If not, see <http://www.gnu.org/licenses/>.
25 #include "canvas_cairo.h"
27 CanvasCairo::CanvasCairo(scalar_t scale, int width, int height) {
28 const int actual_width = int(width * scale);
29 const int actual_height = int(height * scale);
32 _data = new unsigned char [actual_width * actual_height * depth];
34 _image = cairo_image_surface_create_for_data(_data,
38 actual_width * depth);
40 _context_resource = cairo_create(_image);
42 cairo_scale(_context_resource, scale, scale);
44 cairo_set_source_rgb(_context_resource, 1.0, 1.0, 1.0);
45 cairo_set_line_width (_context_resource, 1.0);
47 cairo_rectangle(_context_resource, 0, 0, width, height);
49 cairo_fill(_context_resource);
52 CanvasCairo::~CanvasCairo() {
53 cairo_destroy(_context_resource);
54 cairo_surface_destroy(_image);
59 void CanvasCairo::set_line_width(scalar_t w) {
60 cairo_set_line_width (_context_resource, w);
63 void CanvasCairo::set_drawing_color(scalar_t r, scalar_t g, scalar_t b) {
64 cairo_set_source_rgb(_context_resource, r, g, b);
67 void CanvasCairo::draw_polygon(int filled, int nb, scalar_t *x, scalar_t *y) {
68 cairo_move_to(_context_resource, x[0], y[0]);
69 for(int n = 0; n < nb; n++) {
70 cairo_line_to(_context_resource, x[n], y[n]);
72 cairo_close_path(_context_resource);
75 cairo_stroke_preserve(_context_resource);
76 cairo_fill(_context_resource);
78 cairo_stroke(_context_resource);
82 static cairo_status_t write_cairo_to_file(void *closure,
83 const unsigned char *data,
84 unsigned int length) {
85 fwrite(data, 1, length, (FILE *) closure);
86 return CAIRO_STATUS_SUCCESS;
89 void CanvasCairo::write_png(FILE *file) {
90 cairo_surface_write_to_png_stream(_image, write_cairo_to_file, file);