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