]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-image.c
Do not render images when rendering the page but on demand. It reduces the
[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-document-misc.h"
23 #include "ev-file-helpers.h"
24 #include "ev-image.h"
25
26 struct _EvImagePrivate {
27         gint       page;
28         gint       id;
29         GdkPixbuf *pixbuf;
30         gchar     *tmp_uri;
31 };
32
33 #define EV_IMAGE_GET_PRIVATE(object) \
34                 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_IMAGE, EvImagePrivate))
35
36 G_DEFINE_TYPE (EvImage, ev_image, G_TYPE_OBJECT)
37
38 static void
39 ev_image_finalize (GObject *object)
40 {
41         EvImage *image = EV_IMAGE (object);
42
43         if (image->priv->pixbuf) {
44                 g_object_unref (image->priv->pixbuf);
45                 image->priv->pixbuf = NULL;
46         }
47
48         if (image->priv->tmp_uri) {
49                 ev_tmp_filename_unlink (image->priv->tmp_uri);
50                 g_free (image->priv->tmp_uri);
51                 image->priv->tmp_uri = NULL;
52         }
53
54         (* G_OBJECT_CLASS (ev_image_parent_class)->finalize) (object);
55 }
56
57 static void
58 ev_image_class_init (EvImageClass *klass)
59 {
60         GObjectClass *g_object_class;
61
62         g_object_class = G_OBJECT_CLASS (klass);
63
64         g_type_class_add_private (g_object_class, sizeof (EvImagePrivate));
65
66         g_object_class->finalize = ev_image_finalize;
67 }
68
69 static void
70 ev_image_init (EvImage *image)
71 {
72         image->priv = EV_IMAGE_GET_PRIVATE (image);
73 }
74
75 EvImage *
76 ev_image_new (gint page,
77               gint img_id)
78 {
79         EvImage *image;
80
81         image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
82         image->priv->page = page;
83         image->priv->id = img_id;
84
85         return image;
86 }
87
88 EvImage *
89 ev_image_new_from_pixbuf (GdkPixbuf *pixbuf)
90 {
91         EvImage *image;
92
93         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
94
95         image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
96         image->priv->pixbuf = g_object_ref (pixbuf);
97
98         return image;
99 }
100
101 gint
102 ev_image_get_page (EvImage *image)
103 {
104         g_return_val_if_fail (EV_IS_IMAGE (image), -1);
105
106         return image->priv->page;
107 }
108
109 gint
110 ev_image_get_id (EvImage *image)
111 {
112         g_return_val_if_fail (EV_IS_IMAGE (image), -1);
113
114         return image->priv->id;
115 }
116
117 GdkPixbuf *
118 ev_image_get_pixbuf (EvImage *image)
119 {
120         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
121         g_return_val_if_fail (GDK_IS_PIXBUF (image->priv->pixbuf), NULL);
122
123         return image->priv->pixbuf;
124 }
125
126 const gchar *
127 ev_image_save_tmp (EvImage   *image,
128                    GdkPixbuf *pixbuf)
129 {
130         GError *error = NULL;
131         
132         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
133         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
134
135         if (image->priv->tmp_uri)
136                 return image->priv->tmp_uri;
137
138         image->priv->tmp_uri = ev_tmp_filename ("image");
139         gdk_pixbuf_save (pixbuf, image->priv->tmp_uri,
140                          "png", &error,
141                          "compression", "3", NULL);
142         if (!error)
143                 return image->priv->tmp_uri;
144
145         /* Erro saving image */
146         g_warning (error->message);
147         g_error_free (error);
148         g_free (image->priv->tmp_uri);
149         image->priv->tmp_uri = NULL;
150
151         return NULL;
152 }
153
154 const gchar *
155 ev_image_get_tmp_uri (EvImage *image)
156 {
157         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
158
159         return image->priv->tmp_uri;
160 }
161
162 /* EvImageMapping */
163 static void
164 ev_image_mapping_free_foreach (EvImageMapping *mapping)
165 {
166         g_object_unref (mapping->image);
167         g_free (mapping);
168 }
169
170 void
171 ev_image_mapping_free (GList *image_mapping)
172 {
173         if (!image_mapping)
174                 return;
175
176         g_list_foreach (image_mapping, (GFunc) ev_image_mapping_free_foreach, NULL);
177         g_list_free (image_mapping);
178 }
179
180 EvImage *
181 ev_image_mapping_find (GList   *image_mapping,
182                        gdouble  x,
183                        gdouble  y)
184 {
185         GList *list;
186
187         for (list = image_mapping; list; list = list->next) {
188                 EvImageMapping *mapping = list->data;
189
190                 if ((x >= mapping->x1) &&
191                     (y >= mapping->y1) &&
192                     (x <= mapping->x2) &&
193                     (y <= mapping->y2)) {
194                         return mapping->image;
195                 }
196         }
197
198         return NULL;
199 }
200
201