From 9c1ed304375d5515e5a4a20bbdb0784d5b4de4b1 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 25 May 2022 20:27:22 +0200 Subject: Changed license to LGPL-3.0, to accomodate the cairo_jpeg license. Added support for writing jpeg files. --- genclockimg.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'genclockimg.c') diff --git a/genclockimg.c b/genclockimg.c index e85a4ee..cb303d4 100644 --- a/genclockimg.c +++ b/genclockimg.c @@ -2,15 +2,17 @@ genclockimg generates a clock face PNG image */ + #include #include #include #include #include #include - #include +#include "cairo_jpg.h" + int main (int argc, const char **argv) { @@ -21,6 +23,8 @@ int main (int argc, const char **argv) char img_filename_default[] = "out.png\0"; char *timestr; + int use_jpeg; + /* timestamp represeting what time to draw */ time_t clock_timestamp; struct tm *clock_localtime; @@ -39,6 +43,7 @@ int main (int argc, const char **argv) {"hours", '\0', POPT_ARG_INT, &h, 0, "hour to display", NULL }, {"minutes", '\0', POPT_ARG_INT, &m, 0, "minutes to display", NULL }, {"time", 't', POPT_ARG_STRING, ×tr, 0, "time to display as HH:MM", NULL}, + {"jpg", 'j', POPT_ARG_NONE, &use_jpeg, 0, "Write a JPEG file", NULL}, POPT_AUTOHELP {NULL, '\0', 0, NULL, 0, NULL, NULL} }; @@ -46,9 +51,9 @@ int main (int argc, const char **argv) poptContext options_context; /* default filename is out.png */ - img_filename = NULL; - + img_filename = NULL; timestr = NULL; + use_jpeg = 0; /* default image size is 128x128 */ img_width = 128; @@ -64,12 +69,18 @@ int main (int argc, const char **argv) options_context = poptGetContext(NULL, argc, argv, options_table, 0); int result; - while ((result = poptGetNextOpt(options_context)) > 0 ) { + while ((result = poptGetNextOpt(options_context)) > 0 ) { } if (result < -1) { - /* an error occurred during option processing */ - fprintf(stderr, "%s: %s\n", poptBadOption(options_context, POPT_BADOPTION_NOALIAS), poptStrerror(result)); - exit(1); + /* an error occurred during option processing */ + fprintf(stderr, "%s: %s\n", poptBadOption(options_context, POPT_BADOPTION_NOALIAS),poptStrerror(result)); + exit(1); + } + + if (use_jpeg) { + img_filename_default[4] = 'j'; + img_filename_default[5] = 'p'; + img_filename_default[6] = 'g'; } if (img_filename == NULL) { @@ -92,7 +103,7 @@ int main (int argc, const char **argv) context = cairo_create(surface); /* draw image */ - cairo_set_source_rgba(context, 0.0, 0.0, 0.0, 0.0); + cairo_set_source_rgba(context, 0.0, 0.0, 0.0, 1.0); cairo_rectangle(context, 0, 0, img_width, img_height); cairo_fill(context); @@ -107,18 +118,20 @@ int main (int argc, const char **argv) } float angle = 2.0f * M_PI / 12.0f; - /* draw clock face */ + /* draw clock background */ + /* cairo_set_source_rgba (context, 1.0, 1.0, 1.0, 1.0); cairo_arc(context, x1, y1, r-4, 0, 2 * M_PI); cairo_fill(context); - - cairo_set_source_rgba (context, 0.0, 0.0, 0.0, 1.0); + */ + /* draw clock face */ + cairo_set_source_rgba (context, 1.0, 1.0, 1.0, 1.0); cairo_set_line_width (context, 1); cairo_arc(context, x1, y1, r-4, 0, 2 * M_PI); cairo_stroke(context); /* draw markers */ - cairo_set_source_rgba (context, 0.0, 0.0, 0.0, 1.0); + cairo_set_source_rgba (context, 1.0, 1.0, 1.0, 1.0); cairo_set_line_width (context, 2); for (int h = 0; h < 12; h++) { cairo_move_to(context, x1 + (r - 10) * cosf(angle * h), y1 + (r - 10) * sinf(angle * h)); @@ -140,7 +153,13 @@ int main (int argc, const char **argv) /* write image file */ - if (cairo_surface_write_to_png(surface, img_filename) != CAIRO_STATUS_SUCCESS) { + cairo_status_t status = CAIRO_STATUS_SUCCESS; + if (use_jpeg) { + status = cairo_image_surface_write_to_jpeg(surface, img_filename, 100); + } else { + status = cairo_surface_write_to_png(surface, img_filename); + } + if (status != CAIRO_STATUS_SUCCESS) { fprintf(stderr, "Error saving %s\n", img_filename); } -- cgit v1.2.3