Add JPEG export support

This commit is contained in:
Gergely Polonkai 2014-09-17 11:22:49 +02:00
parent 7fe6a16401
commit 290d8c3f37
5 changed files with 86 additions and 0 deletions

View File

@ -11,6 +11,8 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <librsvg/rsvg.h> #include <librsvg/rsvg.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib/gi18n.h>
#include "config.h" #include "config.h"
#include "astrognome.h" #include "astrognome.h"
@ -1781,6 +1783,72 @@ ag_chart_export_svg_to_file(AgChart *chart, GFile *file, GError **err)
); );
} }
void
ag_chart_export_jpg_to_file(AgChart *chart, GFile *file, GError **err)
{
gchar *svg,
*jpg;
gsize svg_length,
jpg_length;
RsvgHandle *svg_handle;
GdkPixbuf *pixbuf;
if ((svg = ag_chart_create_svg(chart, &svg_length, TRUE, err)) == NULL) {
return;
}
if ((svg_handle = rsvg_handle_new_from_data(
(const guint8 *)svg,
svg_length,
err
)) == NULL) {
g_free(svg);
return;
}
g_free(svg);
if ((pixbuf = rsvg_handle_get_pixbuf(svg_handle)) == NULL) {
g_set_error(
err,
AG_CHART_ERROR, AG_CHART_ERROR_RENDERING_ERROR,
_("Unknown rendering error")
);
return;
}
if (!gdk_pixbuf_save_to_buffer(
pixbuf,
&jpg,
&jpg_length,
"jpeg",
err,
NULL
)) {
g_object_unref(pixbuf);
return;
}
g_object_unref(pixbuf);
g_file_replace_contents(
file,
(const gchar *)jpg,
jpg_length,
NULL,
FALSE,
G_FILE_CREATE_NONE,
NULL,
NULL,
err
);
g_free(jpg);
}
void void
ag_chart_set_note(AgChart *chart, const gchar *note) ag_chart_set_note(AgChart *chart, const gchar *note)
{ {

View File

@ -17,6 +17,7 @@ typedef enum {
AG_CHART_ERROR_NOT_IMPLEMENTED, AG_CHART_ERROR_NOT_IMPLEMENTED,
AG_CHART_ERROR_INVALID_PLAC_FILE, AG_CHART_ERROR_INVALID_PLAC_FILE,
AG_CHART_ERROR_UNSUPPORTED_PLAC_FILE, AG_CHART_ERROR_UNSUPPORTED_PLAC_FILE,
AG_CHART_ERROR_RENDERING_ERROR,
} AgChartError; } AgChartError;
#define AG_TYPE_CHART (ag_chart_get_type()) #define AG_TYPE_CHART (ag_chart_get_type())
@ -69,6 +70,10 @@ void ag_chart_export_svg_to_file(AgChart *chart,
GFile *file, GFile *file,
GError **err); GError **err);
void ag_chart_export_jpg_to_file(AgChart *chart,
GFile *file,
GError **err);
void ag_chart_set_name(AgChart *chart, void ag_chart_set_name(AgChart *chart,
const gchar *name); const gchar *name);

View File

@ -859,6 +859,7 @@ ag_window_export_image(AgWindow *window, GError **err)
_("_Save"), GTK_RESPONSE_ACCEPT, _("_Save"), GTK_RESPONSE_ACCEPT,
NULL); NULL);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fs), filter_svg); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fs), filter_svg);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fs), filter_jpg);
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(fs), filter_svg); gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(fs), filter_svg);
gtk_dialog_set_default_response(GTK_DIALOG(fs), GTK_RESPONSE_ACCEPT); gtk_dialog_set_default_response(GTK_DIALOG(fs), GTK_RESPONSE_ACCEPT);
gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(fs), FALSE); gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(fs), FALSE);
@ -887,6 +888,9 @@ ag_window_export_image(AgWindow *window, GError **err)
if (filter == filter_svg) { if (filter == filter_svg) {
extension = ".svg"; extension = ".svg";
save_func = &ag_chart_export_svg_to_file; save_func = &ag_chart_export_svg_to_file;
} else if (filter == filter_jpg) {
extension = ".jpg";
save_func = &ag_chart_export_jpg_to_file;
} else { } else {
g_warning("Unknown file type"); g_warning("Unknown file type");
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(fs), filter_svg); gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(fs), filter_svg);

View File

@ -25,6 +25,7 @@ GtkFileFilter *filter_all = NULL;
GtkFileFilter *filter_chart = NULL; GtkFileFilter *filter_chart = NULL;
GtkFileFilter *filter_hor = NULL; GtkFileFilter *filter_hor = NULL;
GtkFileFilter *filter_svg = NULL; GtkFileFilter *filter_svg = NULL;
GtkFileFilter *filter_jpg = NULL;
GtkTreeModel *country_list = NULL; GtkTreeModel *country_list = NULL;
GtkTreeModel *city_list = NULL; GtkTreeModel *city_list = NULL;
GHashTable *xinclude_positions; GHashTable *xinclude_positions;
@ -90,6 +91,13 @@ init_filters(void)
gtk_file_filter_set_name(filter_svg, _("SVG image")); gtk_file_filter_set_name(filter_svg, _("SVG image"));
gtk_file_filter_add_pattern(filter_svg, "*.svg"); gtk_file_filter_add_pattern(filter_svg, "*.svg");
g_object_ref_sink(filter_svg); g_object_ref_sink(filter_svg);
filter_jpg = gtk_file_filter_new();
gtk_file_filter_set_name(filter_jpg, _("JPEG image"));
gtk_file_filter_add_pattern(filter_jpg, "*.jpg");
gtk_file_filter_add_pattern(filter_jpg, "*.jpe");
gtk_file_filter_add_pattern(filter_jpg, "*.jpeg");
g_object_ref_sink(filter_jpg);
} }
static int static int

View File

@ -14,6 +14,7 @@ extern GtkFileFilter *filter_all;
extern GtkFileFilter *filter_chart; extern GtkFileFilter *filter_chart;
extern GtkFileFilter *filter_hor; extern GtkFileFilter *filter_hor;
extern GtkFileFilter *filter_svg; extern GtkFileFilter *filter_svg;
extern GtkFileFilter *filter_jpg;
extern GtkTreeModel *country_list; extern GtkTreeModel *country_list;
extern GtkTreeModel *city_list; extern GtkTreeModel *city_list;
extern const GswePlanet used_planets[]; extern const GswePlanet used_planets[];