File: pps-image.c

package info (click to toggle)
papers 49.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,140 kB
  • sloc: ansic: 37,721; sh: 197; xml: 127; makefile: 112
file content (182 lines) | stat: -rw-r--r-- 3,585 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// SPDX-License-Identifier: GPL-2.0-or-later
/* this file is part of papers, a gnome document viewer
 *
 *  Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org>
 */

#include <config.h>

#include <glib/gstdio.h>
#include <unistd.h>

#include "pps-document-misc.h"
#include "pps-file-helpers.h"
#include "pps-image.h"

struct _PpsImagePrivate {
	gint page;
	gint id;
	GdkPixbuf *pixbuf;
	gchar *tmp_uri;
};

typedef struct _PpsImagePrivate PpsImagePrivate;

#define GET_PRIVATE(o) pps_image_get_instance_private (o);

G_DEFINE_TYPE_WITH_PRIVATE (PpsImage, pps_image, G_TYPE_OBJECT)

static void
pps_image_finalize (GObject *object)
{
	PpsImage *image = PPS_IMAGE (object);
	PpsImagePrivate *priv = GET_PRIVATE (image);

	g_clear_object (&priv->pixbuf);

	if (priv->tmp_uri) {
		gchar *filename;

		filename = g_filename_from_uri (priv->tmp_uri, NULL, NULL);
		pps_tmp_filename_unlink (filename);
		g_free (filename);
		g_clear_pointer (&priv->tmp_uri, g_free);
	}

	(*G_OBJECT_CLASS (pps_image_parent_class)->finalize) (object);
}

static void
pps_image_class_init (PpsImageClass *klass)
{
	GObjectClass *g_object_class;

	g_object_class = G_OBJECT_CLASS (klass);

	g_object_class->finalize = pps_image_finalize;
}

static void
pps_image_init (PpsImage *image)
{
}

PpsImage *
pps_image_new (gint page,
               gint img_id)
{
	PpsImage *image;
	PpsImagePrivate *priv;

	image = PPS_IMAGE (g_object_new (PPS_TYPE_IMAGE, NULL));

	priv = GET_PRIVATE (image);
	priv->page = page;
	priv->id = img_id;

	return image;
}

PpsImage *
pps_image_new_from_pixbuf (GdkPixbuf *pixbuf)
{
	PpsImage *image;
	PpsImagePrivate *priv;

	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

	image = PPS_IMAGE (g_object_new (PPS_TYPE_IMAGE, NULL));
	priv = GET_PRIVATE (image);
	priv->pixbuf = g_object_ref (pixbuf);

	return image;
}

gint
pps_image_get_page (PpsImage *image)
{
	g_return_val_if_fail (PPS_IS_IMAGE (image), -1);
	PpsImagePrivate *priv = GET_PRIVATE (image);

	return priv->page;
}

gint
pps_image_get_id (PpsImage *image)
{
	g_return_val_if_fail (PPS_IS_IMAGE (image), -1);
	PpsImagePrivate *priv = GET_PRIVATE (image);

	return priv->id;
}

/**
 * pps_image_get_pixbuf:
 * @image: an #PpsImage
 *
 * Returns: (transfer none): a #GdkPixbuf
 */
GdkPixbuf *
pps_image_get_pixbuf (PpsImage *image)
{
	PpsImagePrivate *priv;
	g_return_val_if_fail (PPS_IS_IMAGE (image), NULL);
	priv = GET_PRIVATE (image);
	g_return_val_if_fail (GDK_IS_PIXBUF (priv->pixbuf), NULL);

	return priv->pixbuf;
}

const gchar *
pps_image_save_tmp (PpsImage *image,
                    GdkPixbuf *pixbuf)
{
	GError *error = NULL;
	gchar *filename = NULL;
	PpsImagePrivate *priv;
	int fd;

	g_return_val_if_fail (PPS_IS_IMAGE (image), NULL);
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

	priv = GET_PRIVATE (image);

	if (priv->tmp_uri)
		return priv->tmp_uri;

	if ((fd = pps_mkstemp ("image.XXXXXX.png", &filename, &error)) == -1)
		goto had_error;

	gdk_pixbuf_save (pixbuf, filename,
	                 "png", &error,
	                 "compression", "3", NULL);
	close (fd);

	if (!error) {
		priv->tmp_uri = g_filename_to_uri (filename, NULL, &error);
		if (priv->tmp_uri == NULL)
			goto had_error;

		g_free (filename);

		return priv->tmp_uri;
	}

had_error:

	/* Erro saving image */
	g_warning ("Error saving image: %s", error->message);
	g_error_free (error);
	g_free (filename);

	return NULL;
}

const gchar *
pps_image_get_tmp_uri (PpsImage *image)
{
	g_return_val_if_fail (PPS_IS_IMAGE (image), NULL);
	PpsImagePrivate *priv = GET_PRIVATE (image);

	return priv->tmp_uri;
}