]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/commitdiff
example.c: use GdkPixbuf instead of Cairo for SVG -> PNG conversion.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 4 Aug 2023 01:58:15 +0000 (21:58 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 4 Aug 2023 01:58:15 +0000 (21:58 -0400)
example.c

index fa1e00010ee015f18d03aacf3db25dff182beffe..3dab2bace2093f70bc9d1a0625418ae45c810a8d 100644 (file)
--- a/example.c
+++ b/example.c
@@ -5,6 +5,8 @@
 #include <unistd.h> /* read */
 
 #include <cairo.h>
+#include <gdk/gdk.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
 #include <svgtiny.h>
 
 /* Convenient typedefs for libsvgtiny */
@@ -153,7 +155,6 @@ diagram_t* svgtiny_diagram_from_path(char* path, int width, int height) {
   return diagram;
 }
 
-
 /*
  * @brief Create a cairo context from a libsvgtiny diagram.
  *
@@ -242,18 +243,15 @@ cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
   return cr;
 }
 
-
 int main(int argc, char** argv) {
-  int exit_code = 0;
-
   char* svgpath;
   char* pngpath;
   int pngwidth = 1024, pngheight = 1024;
 
   diagram_t* diagram;
-  cairo_surface_t *surface;
   cairo_t *cr = 0;
-  cairo_status_t crs;
+
+  GdkPixbuf* pb;
 
   /* Parse arguments, and maybe print usage */
   if (argc < 3) {
@@ -276,14 +274,16 @@ int main(int argc, char** argv) {
     return 1;
   }
 
-  crs = cairo_surface_write_to_png(cairo_get_target(cr), pngpath);
-  if (crs != CAIRO_STATUS_SUCCESS) {
-    fprintf(stderr,
-           "cairo_surface_write_to_png failed: %s\n",
-           cairo_status_to_string(crs));
-    svgtiny_free(diagram);
-    cairo_destroy(cr);
-    return 1;
+  pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
+                                  0,
+                                  0,
+                                  pngwidth,
+                                  pngheight);
+
+
+  if (pb) {
+    gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
+    g_object_unref(pb);
   }
 
   return 0;