Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-image.c
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include <config.h>
21 
22 #include <glib/gstdio.h>
23 #include <unistd.h>
24 
25 #include "ev-document-misc.h"
26 #include "ev-file-helpers.h"
27 #include "ev-image.h"
28 
30  gint page;
31  gint id;
32  GdkPixbuf *pixbuf;
33  gchar *tmp_uri;
34 };
35 
36 #define EV_IMAGE_GET_PRIVATE(object) \
37  (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_IMAGE, EvImagePrivate))
38 
39 G_DEFINE_TYPE (EvImage, ev_image, G_TYPE_OBJECT)
40 
41 static void
42 ev_image_finalize (GObject *object)
43 {
44  EvImage *image = EV_IMAGE (object);
45 
46  if (image->priv->pixbuf) {
47  g_object_unref (image->priv->pixbuf);
48  image->priv->pixbuf = NULL;
49  }
50 
51  if (image->priv->tmp_uri) {
52  gchar *filename;
53 
54  filename = g_filename_from_uri (image->priv->tmp_uri, NULL, NULL);
55  ev_tmp_filename_unlink (filename);
56  g_free (filename);
57  g_free (image->priv->tmp_uri);
58  image->priv->tmp_uri = NULL;
59  }
60 
61  (* G_OBJECT_CLASS (ev_image_parent_class)->finalize) (object);
62 }
63 
64 static void
66 {
67  GObjectClass *g_object_class;
68 
69  g_object_class = G_OBJECT_CLASS (klass);
70 
71  g_type_class_add_private (g_object_class, sizeof (EvImagePrivate));
72 
73  g_object_class->finalize = ev_image_finalize;
74 }
75 
76 static void
78 {
79  image->priv = EV_IMAGE_GET_PRIVATE (image);
80 }
81 
82 EvImage *
83 ev_image_new (gint page,
84  gint img_id)
85 {
86  EvImage *image;
87 
88  image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
89  image->priv->page = page;
90  image->priv->id = img_id;
91 
92  return image;
93 }
94 
95 EvImage *
96 ev_image_new_from_pixbuf (GdkPixbuf *pixbuf)
97 {
98  EvImage *image;
99 
100  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
101 
102  image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
103  image->priv->pixbuf = g_object_ref (pixbuf);
104 
105  return image;
106 }
107 
108 gint
110 {
111  g_return_val_if_fail (EV_IS_IMAGE (image), -1);
112 
113  return image->priv->page;
114 }
115 
116 gint
118 {
119  g_return_val_if_fail (EV_IS_IMAGE (image), -1);
120 
121  return image->priv->id;
122 }
123 
130 GdkPixbuf *
132 {
133  g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
134  g_return_val_if_fail (GDK_IS_PIXBUF (image->priv->pixbuf), NULL);
135 
136  return image->priv->pixbuf;
137 }
138 
139 const gchar *
141  GdkPixbuf *pixbuf)
142 {
143  GError *error = NULL;
144  gchar *filename = NULL;
145  int fd;
146 
147  g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
148  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
149 
150  if (image->priv->tmp_uri)
151  return image->priv->tmp_uri;
152 
153  if ((fd = ev_mkstemp ("image.XXXXXX.png", &filename, &error)) == -1)
154  goto had_error;
155 
156  gdk_pixbuf_save (pixbuf, filename,
157  "png", &error,
158  "compression", "3", NULL);
159  close (fd);
160 
161  if (!error) {
162  image->priv->tmp_uri = g_filename_to_uri (filename, NULL, &error);
163  if (image->priv->tmp_uri == NULL)
164  goto had_error;
165 
166  g_free (filename);
167 
168  return image->priv->tmp_uri;
169  }
170 
171  had_error:
172 
173  /* Erro saving image */
174  g_warning ("Error saving image: %s", error->message);
175  g_error_free (error);
176  g_free (filename);
177 
178  return NULL;
179 }
180 
181 const gchar *
183 {
184  g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
185 
186  return image->priv->tmp_uri;
187 }