Update.
authorFrancois Fleuret <francois@fleuret.org>
Sun, 11 Sep 2016 13:51:02 +0000 (15:51 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Sun, 11 Sep 2016 13:51:02 +0000 (15:51 +0200)
canvas.cc [new file with mode: 0644]
canvas.h [new file with mode: 0644]
canvas_cairo.cc [new file with mode: 0644]
canvas_cairo.h [new file with mode: 0644]
generate.cc

diff --git a/canvas.cc b/canvas.cc
new file mode 100644 (file)
index 0000000..d536158
--- /dev/null
+++ b/canvas.cc
@@ -0,0 +1,11 @@
+
+////////////////////////////////////////////////////////////////////
+// START_IP_HEADER                                                //
+//                                                                //
+// Written by Francois Fleuret                                    //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports //
+//                                                                //
+// END_IP_HEADER                                                  //
+////////////////////////////////////////////////////////////////////
+
+#include "canvas.h"
diff --git a/canvas.h b/canvas.h
new file mode 100644 (file)
index 0000000..a12dc74
--- /dev/null
+++ b/canvas.h
@@ -0,0 +1,22 @@
+
+////////////////////////////////////////////////////////////////////
+// START_IP_HEADER                                                //
+//                                                                //
+// Written by Francois Fleuret                                    //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports //
+//                                                                //
+// END_IP_HEADER                                                  //
+////////////////////////////////////////////////////////////////////
+
+#ifndef CANVAS_H
+#define CANVAS_H
+
+#include "misc.h"
+
+class Canvas {
+public:
+  virtual void set_drawing_color(scalar_t r, scalar_t g, scalar_t b) = 0;
+  virtual void draw_polygon(int filled, int nb, scalar_t *x, scalar_t *y) = 0;
+};
+
+#endif
diff --git a/canvas_cairo.cc b/canvas_cairo.cc
new file mode 100644 (file)
index 0000000..f4c4acc
--- /dev/null
@@ -0,0 +1,73 @@
+
+////////////////////////////////////////////////////////////////////
+// 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);
+}
diff --git a/canvas_cairo.h b/canvas_cairo.h
new file mode 100644 (file)
index 0000000..ab1d6ba
--- /dev/null
@@ -0,0 +1,33 @@
+
+////////////////////////////////////////////////////////////////////
+// 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
index c338a1e..6a8ee69 100644 (file)
@@ -92,7 +92,7 @@ int main(int argc, char **argv) {
           scalar_t object_center_x = delta + (world_width - 2 * delta) * drand48();
           scalar_t object_center_y = delta + (world_height - 2 * delta) * drand48();
           scalar_t red, green, blue;
-          red = drand48() * 0.8;
+          red = 0.75;
           green = red;
           blue = red;
           delete pol;