1 /* Copyright (C) 2016 Red Hat, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "config.h"
18
19 #include "gdkpixbufutilsprivate.h"
20
21 static GdkPixbuf *
load_from_stream(GdkPixbufLoader * loader,GInputStream * stream,GCancellable * cancellable,GError ** error)22 load_from_stream (GdkPixbufLoader *loader,
23 GInputStream *stream,
24 GCancellable *cancellable,
25 GError **error)
26 {
27 GdkPixbuf *pixbuf;
28 gssize n_read;
29 guchar buffer[65536];
30 gboolean res;
31
32 res = TRUE;
33 while (1)
34 {
35 n_read = g_input_stream_read (stream, buffer, sizeof (buffer), cancellable, error);
36 if (n_read < 0)
37 {
38 res = FALSE;
39 error = NULL; /* Ignore further errors */
40 break;
41 }
42
43 if (n_read == 0)
44 break;
45
46 if (!gdk_pixbuf_loader_write (loader, buffer, n_read, error))
47 {
48 res = FALSE;
49 error = NULL;
50 break;
51 }
52 }
53
54 if (!gdk_pixbuf_loader_close (loader, error))
55 {
56 res = FALSE;
57 error = NULL;
58 }
59
60 pixbuf = NULL;
61
62 if (res)
63 {
64 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
65 if (pixbuf)
66 g_object_ref (pixbuf);
67 }
68
69 return pixbuf;
70 }
71
72 static void
size_prepared_cb(GdkPixbufLoader * loader,gint width,gint height,gpointer data)73 size_prepared_cb (GdkPixbufLoader *loader,
74 gint width,
75 gint height,
76 gpointer data)
77 {
78 gdouble *scale = data;
79
80 width = MAX (*scale * width, 1);
81 height = MAX (*scale * height, 1);
82
83 gdk_pixbuf_loader_set_size (loader, width, height);
84 }
85
86 /* Like gdk_pixbuf_new_from_stream_at_scale, but
87 * load the image at its original size times the
88 * given scale.
89 */
90 GdkPixbuf *
_gdk_pixbuf_new_from_stream_scaled(GInputStream * stream,gdouble scale,GCancellable * cancellable,GError ** error)91 _gdk_pixbuf_new_from_stream_scaled (GInputStream *stream,
92 gdouble scale,
93 GCancellable *cancellable,
94 GError **error)
95 {
96 GdkPixbufLoader *loader;
97 GdkPixbuf *pixbuf;
98
99 loader = gdk_pixbuf_loader_new ();
100
101 g_signal_connect (loader, "size-prepared",
102 G_CALLBACK (size_prepared_cb), &scale);
103
104 pixbuf = load_from_stream (loader, stream, cancellable, error);
105
106 g_object_unref (loader);
107
108 return pixbuf;
109 }
110
111 /* Like gdk_pixbuf_new_from_resource_at_scale, but
112 * load the image at its original size times the
113 * given scale.
114 */
115 GdkPixbuf *
_gdk_pixbuf_new_from_resource_scaled(const gchar * resource_path,gdouble scale,GError ** error)116 _gdk_pixbuf_new_from_resource_scaled (const gchar *resource_path,
117 gdouble scale,
118 GError **error)
119 {
120 GInputStream *stream;
121 GdkPixbuf *pixbuf;
122
123 stream = g_resources_open_stream (resource_path, 0, error);
124 if (stream == NULL)
125 return NULL;
126
127 pixbuf = _gdk_pixbuf_new_from_stream_scaled (stream, scale, NULL, error);
128 g_object_unref (stream);
129
130 return pixbuf;
131 }
132
133