summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2017-11-14 16:57:09 +0000
committerRichard Hughes <richard@hughsie.com>2017-11-14 16:57:09 +0000
commit562772dd8fd7d78b773477ad381552e2e6efcdf0 (patch)
treebebe7d088ab5c4b3535ec0fd6cc82bf11a291ae0
parent473f3ab19f91e13014d6cefdbf57209337a6d407 (diff)
downloadappstream-glib-562772dd8fd7d78b773477ad381552e2e6efcdf0.tar.gz
Use pngquant to make the application icons take up less space
Fixes https://github.com/hughsie/appstream-glib/issues/203
-rw-r--r--contrib/libappstream-glib.spec.in1
-rw-r--r--libappstream-builder/asb-app.c5
-rw-r--r--libappstream-builder/asb-utils.c34
-rw-r--r--libappstream-builder/asb-utils.h2
4 files changed, 42 insertions, 0 deletions
diff --git a/contrib/libappstream-glib.spec.in b/contrib/libappstream-glib.spec.in
index ccfb006..419a38a 100644
--- a/contrib/libappstream-glib.spec.in
+++ b/contrib/libappstream-glib.spec.in
@@ -69,6 +69,7 @@ GLib headers and libraries for appstream-glib.
%package builder
Summary: Library and command line tools for building AppStream metadata
Requires: %{name}%{?_isa} = %{version}-%{release}
+Requires: pngquant
%description builder
This library and command line tool is used for building AppStream metadata
diff --git a/libappstream-builder/asb-app.c b/libappstream-builder/asb-app.c
index fa42160..b44f091 100644
--- a/libappstream-builder/asb-app.c
+++ b/libappstream-builder/asb-app.c
@@ -33,6 +33,7 @@
#include <appstream-glib.h>
#include "asb-app.h"
+#include "asb-utils.h"
typedef struct
{
@@ -188,6 +189,10 @@ asb_app_save_resources (AsbApp *app, AsbAppSaveFlags save_flags, GError **error)
if (!gdk_pixbuf_save (pixbuf, filename, "png", error, NULL))
return FALSE;
+ /* optimize the icon */
+ if (!asb_utils_optimize_png (filename, error))
+ return FALSE;
+
/* set new AppStream compatible icon name */
asb_package_log (priv->pkg,
ASB_PACKAGE_LOG_LEVEL_DEBUG,
diff --git a/libappstream-builder/asb-utils.c b/libappstream-builder/asb-utils.c
index f1b48e9..503c379 100644
--- a/libappstream-builder/asb-utils.c
+++ b/libappstream-builder/asb-utils.c
@@ -281,6 +281,40 @@ asb_utils_explode_file (struct archive_entry *entry, const gchar *dir)
}
/**
+ * asb_utils_optimize_png:
+ * @filename: package filename
+ * @error: A #GError or %NULL
+ *
+ * Optimises a PNG if pngquant is installed on the system.
+ *
+ * Returns: %TRUE for success, %FALSE otherwise
+ **/
+gboolean
+asb_utils_optimize_png (const gchar *filename, GError **error)
+{
+ g_autofree gchar *standard_error = NULL;
+ gint exit_status = 0;
+ const gchar *argv[] = { "/usr/bin/pngquant", "--skip-if-larger",
+ "--strip", "--ext", ".png",
+ "--force", "--speed", "1", filename, NULL };
+ if (!g_file_test (argv[0], G_FILE_TEST_IS_EXECUTABLE))
+ return TRUE;
+ if (!g_spawn_sync (NULL, (gchar **) argv, NULL, G_SPAWN_DEFAULT,
+ NULL, NULL, NULL, &standard_error, &exit_status, error))
+ return FALSE;
+ if (exit_status != 0) {
+ g_autofree gchar *argv_str = g_strjoinv (" ", (gchar **) argv);
+ g_set_error (error,
+ AS_APP_ERROR,
+ AS_APP_ERROR_FAILED,
+ "failed to run %s: %s",
+ argv_str, standard_error);
+ return FALSE;
+ }
+ return TRUE;
+}
+
+/**
* asb_utils_explode:
* @filename: package filename
* @dir: directory to decompress into
diff --git a/libappstream-builder/asb-utils.h b/libappstream-builder/asb-utils.h
index 266f311..31922b6 100644
--- a/libappstream-builder/asb-utils.h
+++ b/libappstream-builder/asb-utils.h
@@ -42,6 +42,8 @@ gboolean asb_utils_explode (const gchar *filename,
const gchar *dir,
GPtrArray *glob,
GError **error);
+gboolean asb_utils_optimize_png (const gchar *filename,
+ GError **error);
gchar *asb_utils_get_cache_id_for_filename (const gchar *filename);
gchar *asb_utils_get_builder_id (void);