summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bb83aae)
raw | patch | inline | side by side (parent: bb83aae)
author | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Fri, 15 Jun 2007 06:28:47 +0000 (06:28 +0000) | ||
committer | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Fri, 15 Jun 2007 06:28:47 +0000 (06:28 +0000) |
src/rrd.h | patch | blob | history | |
src/rrd_graph.c | patch | blob | history | |
src/rrd_graph.h | patch | blob | history |
diff --git a/src/rrd.h b/src/rrd.h
index a7fe7597e0d47a1420b47e6db9ed88420eacb7f2..0403a266123c3bc0bdc34f710c16c97832452ef7 100644 (file)
--- a/src/rrd.h
+++ b/src/rrd.h
FILE *,
double *,
double *);
+
+ unsigned char *rrd_graph_in_memory(
+ int argc,
+ char **argv,
+ int *xsize,
+ int *ysize,
+ double *ymin,
+ double *ymax,
+ size_t * img_size);
+
int rrd_fetch(
int,
char **,
diff --git a/src/rrd_graph.c b/src/rrd_graph.c
index db828824e1e48a67fbd8be8875ee234d9c3f8044..2d1785f40d8102f829aa8204dd7a82bb985812c7 100644 (file)
--- a/src/rrd_graph.c
+++ b/src/rrd_graph.c
-static cairo_status_t cairo_write_func_file(
+static cairo_status_t cairo_write_func_filehandle(
void *closure,
const unsigned char *data,
unsigned int length)
return CAIRO_STATUS_SUCCESS;
}
+static cairo_status_t cairo_copy_to_buffer(
+ void *closure,
+ const unsigned char *data,
+ unsigned int length)
+{
+ image_desc_t *im = closure;
+
+ im->rendered_image =
+ realloc(im->rendered_image, im->rendered_image_size + length);
+ if (im->rendered_image == NULL) {
+ return CAIRO_STATUS_WRITE_ERROR;
+ }
+
+ memcpy(im->rendered_image + im->rendered_image_size, data, length);
+
+ im->rendered_image_size += length;
+
+ return CAIRO_STATUS_SUCCESS;
+}
/* draw that picture thing ... */
int graph_paint(
break;
case IF_PDF:
im->gridfit = 0;
- im->surface =
- cairo_pdf_surface_create(im->graphfile, im->ximg * im->zoom,
- im->yimg * im->zoom);
+ im->surface = strlen(im->graphfile)
+ ? cairo_pdf_surface_create(im->graphfile, im->ximg * im->zoom,
+ im->yimg * im->zoom)
+ : cairo_pdf_surface_create_for_stream(&cairo_copy_to_buffer, im,
+ im->ximg * im->zoom,
+ im->yimg * im->zoom);
break;
case IF_EPS:
im->gridfit = 0;
- im->surface =
- cairo_ps_surface_create(im->graphfile, im->ximg * im->zoom,
- im->yimg * im->zoom);
+ im->surface = strlen(im->graphfile)
+ ? cairo_ps_surface_create(im->graphfile, im->ximg * im->zoom,
+ im->yimg * im->zoom)
+ : cairo_ps_surface_create_for_stream(&cairo_copy_to_buffer, im,
+ im->ximg * im->zoom,
+ im->yimg * im->zoom);
break;
case IF_SVG:
im->gridfit = 0;
- im->surface =
- cairo_svg_surface_create(im->graphfile, im->ximg * im->zoom,
- im->yimg * im->zoom);
+ im->surface = strlen(im->graphfile)
+ ? cairo_svg_surface_create(im->graphfile, im->ximg * im->zoom,
+ im->yimg * im->zoom)
+ : cairo_svg_surface_create_for_stream(&cairo_copy_to_buffer, im,
+ im->ximg * im->zoom,
+ im->yimg * im->zoom);
cairo_svg_surface_restrict_to_version(im->surface,
CAIRO_SVG_VERSION_1_1);
break;
{
cairo_status_t status;
- if (strcmp(im->graphfile, "-") == 0) {
+ if (strlen(im->graphfile) == 0) {
status =
cairo_surface_write_to_png_stream(im->surface,
- &cairo_write_func_file,
+ &cairo_copy_to_buffer, im);
+ } else if (strcmp(im->graphfile, "-") == 0) {
+ status =
+ cairo_surface_write_to_png_stream(im->surface,
+ &cairo_write_func_filehandle,
(void *) stdout);
} else {
status = cairo_surface_write_to_png(im->surface, im->graphfile);
}
break;
default:
- cairo_show_page(im->cr);
+ if (strlen(im->graphfile)) {
+ cairo_show_page(im->cr);
+ } else {
+ cairo_surface_finish(im->surface);
+ }
break;
}
return 0;
/* a dummy surface so that we can measure text sizes for placements */
im.surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
im.cr = cairo_create(im.surface);
-
-
- /* not currently using this ... */
im.graphhandle = stream;
rrd_graph_options(argc, argv, &im);
return -1;
}
+ if (optind >= argc) {
+ rrd_set_error("missing filename");
+ return -1;
+ }
+
if (strlen(argv[optind]) >= MAXPATH) {
rrd_set_error("filename (including path) too long");
im_free(&im);
return -1;
}
+
strncpy(im.graphfile, argv[optind], MAXPATH - 1);
im.graphfile[MAXPATH - 1] = '\0';
return 0;
}
+/* a simplified version of the above that just creates the graph in memory
+ and returns a pointer to it. */
+
+unsigned char *rrd_graph_in_memory(
+ int argc,
+ char **argv,
+ char ***prdata,
+ int *xsize,
+ int *ysize,
+ double *ymin,
+ double *ymax,
+ size_t * img_size)
+{
+ image_desc_t im;
+
+ rrd_graph_init(&im);
+
+ /* a dummy surface so that we can measure text sizes for placements */
+ im.surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
+ im.cr = cairo_create(im.surface);
+
+ rrd_graph_options(argc, argv, &im);
+ if (rrd_test_error()) {
+ im_free(&im);
+ return NULL;
+ }
+
+ rrd_graph_script(argc, argv, &im, 1);
+ if (rrd_test_error()) {
+ im_free(&im);
+ return NULL;
+ }
+
+ /* Everything is now read and the actual work can start */
+
+ /* by not assigning a name to im.graphfile data will be written to
+ newly allocated memory on im.rendered_image ... */
+
+ (*prdata) = NULL;
+ if (graph_paint(&im, prdata) == -1) {
+ im_free(&im);
+ return NULL;
+ }
+
+ *xsize = im.ximg;
+ *ysize = im.yimg;
+ *ymin = im.minval;
+ *ymax = im.maxval;
+ *img_size = im.rendered_image_size;
+ im_free(&im);
+
+ return im.rendered_image;
+}
+
void rrd_graph_init(
image_desc_t *im)
{
im->yimg = 0;
im->xsize = 400;
im->ysize = 100;
+ im->rendered_image_size = 0;
+ im->rendered_image = NULL;
im->step = 0;
im->ylegend[0] = '\0';
im->title[0] = '\0';
im->symbol = ' ';
im->viewfactor = 1.0;
im->imgformat = IF_PNG;
+ im->graphfile[0] = '\0';
im->cr = NULL;
im->surface = NULL;
im->extra_flags = 0;
}
}
- if (optind >= argc) {
- rrd_set_error("missing filename");
- return;
- }
-
if (im->logarithmic == 1 && im->minval <= 0) {
rrd_set_error
("for a logarithmic yaxis you must specify a lower-limit > 0");
diff --git a/src/rrd_graph.h b/src/rrd_graph.h
index de968a70b44db05fad224d91f24180d3f8e4b342..00ab15ba1f1c90fe91958f9add1382c3c35826e7 100644 (file)
--- a/src/rrd_graph.h
+++ b/src/rrd_graph.h
long xorigin, yorigin; /* where is (0,0) of the graph */
long ximg, yimg; /* total size of the image */
+ size_t rendered_image_size;
double zoom;
double magfact; /* numerical magnitude */
long base; /* 1000 or 1024 depending on what we graph */
int extra_flags; /* flags for boolean options */
/* data elements */
+ unsigned char *rendered_image;
long prt_c; /* number of print elements */
long gdes_c; /* number of graphics elements */
graph_desc_t *gdes; /* points to an array of graph elements */