]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-image.c
Include config.h. Bug #504721.
[evince.git] / libdocument / ev-image.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <config.h>
21 #include <glib/gstdio.h>
22 #include "ev-file-helpers.h"
23 #include "ev-image.h"
24
25 struct _EvImagePrivate {
26         GdkPixbuf *pixbuf;
27         gchar     *tmp_uri;
28 };
29
30 #define EV_IMAGE_GET_PRIVATE(object) \
31                 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_IMAGE, EvImagePrivate))
32
33 G_DEFINE_TYPE (EvImage, ev_image, G_TYPE_OBJECT)
34
35 static void
36 ev_image_finalize (GObject *object)
37 {
38         EvImage *image = EV_IMAGE (object);
39
40         if (image->priv->pixbuf) {
41                 g_object_unref (image->priv->pixbuf);
42                 image->priv->pixbuf = NULL;
43         }
44
45         if (image->priv->tmp_uri) {
46                 ev_tmp_filename_unlink (image->priv->tmp_uri);
47                 g_free (image->priv->tmp_uri);
48                 image->priv->tmp_uri = NULL;
49         }
50
51         (* G_OBJECT_CLASS (ev_image_parent_class)->finalize) (object);
52 }
53
54 static void
55 ev_image_class_init (EvImageClass *klass)
56 {
57         GObjectClass *g_object_class;
58
59         g_object_class = G_OBJECT_CLASS (klass);
60
61         g_type_class_add_private (g_object_class, sizeof (EvImagePrivate));
62
63         g_object_class->finalize = ev_image_finalize;
64 }
65
66 static void
67 ev_image_init (EvImage *image)
68 {
69         image->priv = EV_IMAGE_GET_PRIVATE (image);
70 }
71
72 EvImage *
73 ev_image_new_from_pixbuf (GdkPixbuf *pixbuf)
74 {
75         EvImage *image;
76
77         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
78
79         image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
80         image->priv->pixbuf = g_object_ref (pixbuf);
81
82         return image;
83 }
84
85 GdkPixbuf *
86 ev_image_get_pixbuf (EvImage *image)
87 {
88         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
89         g_return_val_if_fail (GDK_IS_PIXBUF (image->priv->pixbuf), NULL);
90
91         return image->priv->pixbuf;
92 }
93
94 const gchar *
95 ev_image_save_tmp (EvImage *image)
96 {
97         GError *error = NULL;
98         
99         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
100         g_return_val_if_fail (GDK_IS_PIXBUF (image->priv->pixbuf), NULL);
101
102         if (image->priv->tmp_uri)
103                 return image->priv->tmp_uri;
104
105         image->priv->tmp_uri = ev_tmp_filename ("image");
106         gdk_pixbuf_save (image->priv->pixbuf,
107                          image->priv->tmp_uri, "png", &error,
108                          "compression", "3", NULL);
109         if (!error)
110                 return image->priv->tmp_uri;
111
112         /* Erro saving image */
113         g_warning (error->message);
114         g_error_free (error);
115         g_free (image->priv->tmp_uri);
116         image->priv->tmp_uri = NULL;
117
118         return NULL;
119 }
120
121 const gchar *
122 ev_image_get_tmp_uri (EvImage *image)
123 {
124         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
125
126         return image->priv->tmp_uri;
127 }
128
129 /* EvImageMapping */
130 static void
131 ev_image_mapping_free_foreach (EvImageMapping *mapping)
132 {
133         g_object_unref (mapping->image);
134         g_free (mapping);
135 }
136
137 void
138 ev_image_mapping_free (GList *image_mapping)
139 {
140         if (!image_mapping)
141                 return;
142
143         g_list_foreach (image_mapping, (GFunc) ev_image_mapping_free_foreach, NULL);
144         g_list_free (image_mapping);
145 }
146
147 EvImage *
148 ev_image_mapping_find (GList   *image_mapping,
149                        gdouble  x,
150                        gdouble  y)
151 {
152         GList *list;
153
154         for (list = image_mapping; list; list = list->next) {
155                 EvImageMapping *mapping = list->data;
156
157                 if ((x >= mapping->x1) &&
158                     (y >= mapping->y1) &&
159                     (x <= mapping->x2) &&
160                     (y <= mapping->y2)) {
161                         return mapping->image;
162                 }
163         }
164
165         return NULL;
166 }
167
168