Initial commit
[dyncnn.git] / canvas_cairo.cc
1
2 /*
3  *  dyncnn is a deep-learning algorithm for the prediction of
4  *  interacting object dynamics
5  *
6  *  Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of dyncnn.
10  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 #include "canvas_cairo.h"
26
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);
30   const int depth = 4;
31
32   _data = new unsigned char [actual_width * actual_height * depth];
33
34   _image = cairo_image_surface_create_for_data(_data,
35                                                CAIRO_FORMAT_RGB24,
36                                                actual_width,
37                                                actual_height,
38                                                actual_width * depth);
39
40   _context_resource = cairo_create(_image);
41
42   cairo_scale(_context_resource, scale, scale);
43
44   cairo_set_source_rgb(_context_resource, 1.0, 1.0, 1.0);
45   cairo_set_line_width (_context_resource, 1.0);
46
47   cairo_rectangle(_context_resource, 0, 0, width, height);
48
49   cairo_fill(_context_resource);
50 }
51
52 CanvasCairo::~CanvasCairo() {
53   cairo_destroy(_context_resource);
54   cairo_surface_destroy(_image);
55   delete[] _data;
56 }
57
58
59 void CanvasCairo::set_line_width(scalar_t w) {
60   cairo_set_line_width (_context_resource, w);
61 }
62
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);
65 }
66
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]);
71   }
72   cairo_close_path(_context_resource);
73
74   if(filled) {
75     cairo_stroke_preserve(_context_resource);
76     cairo_fill(_context_resource);
77   } else {
78     cairo_stroke(_context_resource);
79   }
80 }
81
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;
87 }
88
89 void CanvasCairo::write_png(FILE *file) {
90   cairo_surface_write_to_png_stream(_image, write_cairo_to_file, file);
91 }