1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org>
5 * Evince is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Evince is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20 #include <glib/gstdio.h>
21 #include "ev-file-helpers.h"
24 struct _EvImagePrivate {
29 #define EV_IMAGE_GET_PRIVATE(object) \
30 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_IMAGE, EvImagePrivate))
32 G_DEFINE_TYPE (EvImage, ev_image, G_TYPE_OBJECT)
35 ev_image_finalize (GObject *object)
37 EvImage *image = EV_IMAGE (object);
39 if (image->priv->pixbuf) {
40 g_object_unref (image->priv->pixbuf);
41 image->priv->pixbuf = NULL;
44 if (image->priv->tmp_uri) {
45 ev_tmp_filename_unlink (image->priv->tmp_uri);
46 g_free (image->priv->tmp_uri);
47 image->priv->tmp_uri = NULL;
50 (* G_OBJECT_CLASS (ev_image_parent_class)->finalize) (object);
54 ev_image_class_init (EvImageClass *klass)
56 GObjectClass *g_object_class;
58 g_object_class = G_OBJECT_CLASS (klass);
60 g_type_class_add_private (g_object_class, sizeof (EvImagePrivate));
62 g_object_class->finalize = ev_image_finalize;
66 ev_image_init (EvImage *image)
68 image->priv = EV_IMAGE_GET_PRIVATE (image);
72 ev_image_new_from_pixbuf (GdkPixbuf *pixbuf)
76 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
78 image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
79 image->priv->pixbuf = g_object_ref (pixbuf);
85 ev_image_get_pixbuf (EvImage *image)
87 g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
88 g_return_val_if_fail (GDK_IS_PIXBUF (image->priv->pixbuf), NULL);
90 return image->priv->pixbuf;
94 ev_image_save_tmp (EvImage *image)
98 g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
99 g_return_val_if_fail (GDK_IS_PIXBUF (image->priv->pixbuf), NULL);
101 if (image->priv->tmp_uri)
102 return image->priv->tmp_uri;
104 image->priv->tmp_uri = ev_tmp_filename ("image");
105 gdk_pixbuf_save (image->priv->pixbuf,
106 image->priv->tmp_uri, "png", &error,
107 "compression", "3", NULL);
109 return image->priv->tmp_uri;
111 /* Erro saving image */
112 g_warning (error->message);
113 g_error_free (error);
114 g_free (image->priv->tmp_uri);
115 image->priv->tmp_uri = NULL;
121 ev_image_get_tmp_uri (EvImage *image)
123 g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
125 return image->priv->tmp_uri;
130 ev_image_mapping_free_foreach (EvImageMapping *mapping)
132 g_object_unref (mapping->image);
137 ev_image_mapping_free (GList *image_mapping)
142 g_list_foreach (image_mapping, (GFunc) ev_image_mapping_free_foreach, NULL);
143 g_list_free (image_mapping);
147 ev_image_mapping_find (GList *image_mapping,
153 for (list = image_mapping; list; list = list->next) {
154 EvImageMapping *mapping = list->data;
156 if ((x >= mapping->x1) &&
157 (y >= mapping->y1) &&
158 (x <= mapping->x2) &&
159 (y <= mapping->y2)) {
160 return mapping->image;