commit 4f191e74da22f0c98ff91fa2bdd890ca858440ef
parent 2729af259a99d7a65dd21904dc6fc8cb4d376d18
Author: aabacchus <ben@bvnf.space>
Date: Sun, 3 Oct 2021 01:00:41 +0100
kplot: add -w -h width/height flags
Diffstat:
M | kplot.c | | | 32 | ++++++++++++++++++++++++++------ |
1 file changed, 26 insertions(+), 6 deletions(-)
diff --git a/kplot.c b/kplot.c
@@ -10,25 +10,29 @@
void
usage(char *argv0) {
- fprintf(stderr, "usage: %s [-HTo] [file.csv]\n", argv0);
+ fprintf(stderr, "usage: %s [-HTwoh] [file.csv]\n", argv0);
fprintf(stderr, " -H treat first line as header\n");
fprintf(stderr, " -T format output format (options \"png\", \"pdf\")"
" (default: \"pdf\")\n");
fprintf(stderr, " -o outfile output filename\n");
+ fprintf(stderr, " -w width\n");
+ fprintf(stderr, " -h height\n");
}
int
main(int argc, char **argv) {
- int ch, flag_H, flag_o, output_type;
+ int ch, flag_H, flag_o, output_type, width, height, w, h;
char *outfile;
flag_H = flag_o = 0;
+ width = 600;
+ height = 400;
enum {
OUT_PNG,
OUT_PDF
};
output_type = OUT_PDF;
outfile = "out.pdf";
- while ((ch = getopt(argc, argv, "HT:o:")) != -1) {
+ while ((ch = getopt(argc, argv, "HT:o:w:h:")) != -1) {
switch (ch) {
case 'H':
flag_H = 1;
@@ -48,6 +52,22 @@ main(int argc, char **argv) {
flag_o = 1;
outfile = optarg;
break;
+ case 'h':
+ h = atoi(optarg);
+ if (h <= 0) {
+ fprintf(stderr, "bad height \"%s\"\n", optarg);
+ return 1;
+ }
+ height = h;
+ break;
+ case 'w':
+ w = atoi(optarg);
+ if (w <= 0) {
+ fprintf(stderr, "bad width \"%s\"\n", optarg);
+ return 1;
+ }
+ width = w;
+ break;
case '?':
usage(*argv);
return 1;
@@ -101,12 +121,12 @@ main(int argc, char **argv) {
kplot_attach_data(p, d1, KPLOT_POINTS, NULL);
kdata_destroy(d1);
if (output_type == OUT_PNG)
- surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 600, 400);
+ surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
else
- surf = cairo_pdf_surface_create(outfile, 600, 400);
+ surf = cairo_pdf_surface_create(outfile, width, height);
cr = cairo_create(surf);
cairo_surface_destroy(surf);
- kplot_draw(p, 600.0, 400.0, cr);
+ kplot_draw(p, (double) width, (double) height, cr);
if (output_type == OUT_PNG)
cairo_surface_write_to_png(cairo_get_target(cr), outfile);
cairo_destroy(cr);