| 1 /* pidgin |
|
| 2 * |
|
| 3 * Pidgin is the legal property of its developers, whose names are too numerous |
|
| 4 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 5 * source distribution. |
|
| 6 * |
|
| 7 * This program is free software; you can redistribute it and/or modify |
|
| 8 * it under the terms of the GNU General Public License as published by |
|
| 9 * the Free Software Foundation; either version 2 of the License, or |
|
| 10 * (at your option) any later version. |
|
| 11 * |
|
| 12 * This program is distributed in the hope that it will be useful, |
|
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 15 * GNU General Public License for more details. |
|
| 16 * |
|
| 17 * You should have received a copy of the GNU General Public License |
|
| 18 * along with this program; if not, write to the Free Software |
|
| 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
| 20 */ |
|
| 21 |
|
| 22 #include "pidgin/pidgingdkpixbuf.h" |
|
| 23 |
|
| 24 GdkPixbuf * |
|
| 25 pidgin_gdk_pixbuf_new_from_image(PurpleImage *image, GError **error) { |
|
| 26 GdkPixbufLoader *loader = NULL; |
|
| 27 GdkPixbuf *pixbuf = NULL; |
|
| 28 GBytes *data = NULL; |
|
| 29 gboolean success = FALSE; |
|
| 30 |
|
| 31 g_return_val_if_fail(PURPLE_IS_IMAGE(image), NULL); |
|
| 32 |
|
| 33 data = purple_image_get_contents(image); |
|
| 34 success = gdk_pixbuf_loader_write_bytes(loader, data, error); |
|
| 35 g_bytes_unref(data); |
|
| 36 |
|
| 37 if(success) { |
|
| 38 if(error != NULL && *error != NULL) { |
|
| 39 g_object_unref(G_OBJECT(loader)); |
|
| 40 |
|
| 41 return NULL; |
|
| 42 } |
|
| 43 |
|
| 44 if(gdk_pixbuf_loader_close(loader, error)) { |
|
| 45 pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); |
|
| 46 if(pixbuf != NULL) { |
|
| 47 pixbuf = g_object_ref(pixbuf); |
|
| 48 } |
|
| 49 } |
|
| 50 |
|
| 51 g_object_unref(G_OBJECT(loader)); |
|
| 52 } |
|
| 53 |
|
| 54 return pixbuf; |
|
| 55 } |
|
| 56 |
|
| 57 void pidgin_gdk_pixbuf_make_round(GdkPixbuf *pixbuf) { |
|
| 58 gint width, height, rowstride; |
|
| 59 guchar *pixels; |
|
| 60 |
|
| 61 if (!gdk_pixbuf_get_has_alpha(pixbuf)) { |
|
| 62 return; |
|
| 63 } |
|
| 64 |
|
| 65 width = gdk_pixbuf_get_width(pixbuf); |
|
| 66 height = gdk_pixbuf_get_height(pixbuf); |
|
| 67 rowstride = gdk_pixbuf_get_rowstride(pixbuf); |
|
| 68 pixels = gdk_pixbuf_get_pixels(pixbuf); |
|
| 69 |
|
| 70 if (width < 6 || height < 6) { |
|
| 71 return; |
|
| 72 } |
|
| 73 |
|
| 74 /* The following code will convert the alpha of the pixel data in all |
|
| 75 * corners to look something like the following diagram. |
|
| 76 * |
|
| 77 * 00 80 c0 FF FF c0 80 00 |
|
| 78 * 80 FF FF FF FF FF FF 80 |
|
| 79 * c0 FF FF FF FF FF FF c0 |
|
| 80 * FF FF FF FF FF FF FF FF |
|
| 81 * FF FF FF FF FF FF FF FF |
|
| 82 * c0 FF FF FF FF FF FF c0 |
|
| 83 * 80 FF FF FF FF FF FF 80 |
|
| 84 * 00 80 c0 FF FF c0 80 00 |
|
| 85 */ |
|
| 86 |
|
| 87 /* Top left */ |
|
| 88 pixels[3] = 0; |
|
| 89 pixels[7] = 0x80; |
|
| 90 pixels[11] = 0xC0; |
|
| 91 pixels[rowstride + 3] = 0x80; |
|
| 92 pixels[rowstride * 2 + 3] = 0xC0; |
|
| 93 |
|
| 94 /* Top right */ |
|
| 95 pixels[width * 4 - 1] = 0; |
|
| 96 pixels[width * 4 - 5] = 0x80; |
|
| 97 pixels[width * 4 - 9] = 0xC0; |
|
| 98 pixels[rowstride + (width * 4) - 1] = 0x80; |
|
| 99 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0; |
|
| 100 |
|
| 101 /* Bottom left */ |
|
| 102 pixels[(height - 1) * rowstride + 3] = 0; |
|
| 103 pixels[(height - 1) * rowstride + 7] = 0x80; |
|
| 104 pixels[(height - 1) * rowstride + 11] = 0xC0; |
|
| 105 pixels[(height - 2) * rowstride + 3] = 0x80; |
|
| 106 pixels[(height - 3) * rowstride + 3] = 0xC0; |
|
| 107 |
|
| 108 /* Bottom right */ |
|
| 109 pixels[height * rowstride - 1] = 0; |
|
| 110 pixels[(height - 1) * rowstride - 1] = 0x80; |
|
| 111 pixels[(height - 2) * rowstride - 1] = 0xC0; |
|
| 112 pixels[height * rowstride - 5] = 0x80; |
|
| 113 pixels[height * rowstride - 9] = 0xC0; |
|
| 114 } |
|
| 115 |
|
| 116 gboolean |
|
| 117 pidgin_gdk_pixbuf_is_opaque(GdkPixbuf *pixbuf) { |
|
| 118 gint height, rowstride, i; |
|
| 119 guchar *pixels; |
|
| 120 guchar *row; |
|
| 121 |
|
| 122 if (!gdk_pixbuf_get_has_alpha(pixbuf)) { |
|
| 123 return TRUE; |
|
| 124 } |
|
| 125 |
|
| 126 height = gdk_pixbuf_get_height (pixbuf); |
|
| 127 rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
|
| 128 pixels = gdk_pixbuf_get_pixels (pixbuf); |
|
| 129 |
|
| 130 /* check the top row */ |
|
| 131 row = pixels; |
|
| 132 for (i = 3; i < rowstride; i+=4) { |
|
| 133 if (row[i] < 0xfe) { |
|
| 134 return FALSE; |
|
| 135 } |
|
| 136 } |
|
| 137 |
|
| 138 /* check the left and right sides */ |
|
| 139 for (i = 1; i < height - 1; i++) { |
|
| 140 row = pixels + (i * rowstride); |
|
| 141 if (row[3] < 0xfe || row[rowstride - 1] < 0xfe) { |
|
| 142 return FALSE; |
|
| 143 } |
|
| 144 } |
|
| 145 |
|
| 146 /* check the bottom */ |
|
| 147 row = pixels + ((height - 1) * rowstride); |
|
| 148 for (i = 3; i < rowstride; i += 4) { |
|
| 149 if (row[i] < 0xfe) { |
|
| 150 return FALSE; |
|
| 151 } |
|
| 152 } |
|
| 153 |
|
| 154 return TRUE; |
|
| 155 } |
|
| 156 |
|
| 157 static GObject *pidgin_pixbuf_from_data_helper(const guchar *buf, gsize count, gboolean animated) |
|
| 158 { |
|
| 159 GObject *pixbuf; |
|
| 160 GdkPixbufLoader *loader; |
|
| 161 GError *error = NULL; |
|
| 162 |
|
| 163 loader = gdk_pixbuf_loader_new(); |
|
| 164 |
|
| 165 if (!gdk_pixbuf_loader_write(loader, buf, count, &error) || error) { |
|
| 166 purple_debug_warning("gtkutils", "gdk_pixbuf_loader_write() " |
|
| 167 "failed with size=%" G_GSIZE_FORMAT ": %s\n", count, |
|
| 168 error ? error->message : "(no error message)"); |
|
| 169 if (error) |
|
| 170 g_error_free(error); |
|
| 171 g_object_unref(G_OBJECT(loader)); |
|
| 172 return NULL; |
|
| 173 } |
|
| 174 |
|
| 175 if (!gdk_pixbuf_loader_close(loader, &error) || error) { |
|
| 176 purple_debug_warning("gtkutils", "gdk_pixbuf_loader_close() " |
|
| 177 "failed for image of size %" G_GSIZE_FORMAT ": %s\n", count, |
|
| 178 error ? error->message : "(no error message)"); |
|
| 179 if (error) |
|
| 180 g_error_free(error); |
|
| 181 g_object_unref(G_OBJECT(loader)); |
|
| 182 return NULL; |
|
| 183 } |
|
| 184 |
|
| 185 if (animated) |
|
| 186 pixbuf = G_OBJECT(gdk_pixbuf_loader_get_animation(loader)); |
|
| 187 else |
|
| 188 pixbuf = G_OBJECT(gdk_pixbuf_loader_get_pixbuf(loader)); |
|
| 189 if (!pixbuf) { |
|
| 190 purple_debug_warning("gtkutils", "%s() returned NULL for image " |
|
| 191 "of size %" G_GSIZE_FORMAT "\n", |
|
| 192 animated ? "gdk_pixbuf_loader_get_animation" |
|
| 193 : "gdk_pixbuf_loader_get_pixbuf", count); |
|
| 194 g_object_unref(G_OBJECT(loader)); |
|
| 195 return NULL; |
|
| 196 } |
|
| 197 |
|
| 198 g_object_ref(pixbuf); |
|
| 199 g_object_unref(G_OBJECT(loader)); |
|
| 200 |
|
| 201 return pixbuf; |
|
| 202 } |
|
| 203 |
|
| 204 GdkPixbuf *pidgin_pixbuf_from_data(const guchar *buf, gsize count) |
|
| 205 { |
|
| 206 return GDK_PIXBUF(pidgin_pixbuf_from_data_helper(buf, count, FALSE)); |
|
| 207 } |
|
| 208 |
|
| 209 GdkPixbufAnimation *pidgin_pixbuf_anim_from_data(const guchar *buf, gsize count) |
|
| 210 { |
|
| 211 return GDK_PIXBUF_ANIMATION(pidgin_pixbuf_from_data_helper(buf, count, TRUE)); |
|
| 212 } |
|
| 213 |
|
| 214 GdkPixbuf * |
|
| 215 pidgin_pixbuf_from_image(PurpleImage *image) |
|
| 216 { |
|
| 217 return pidgin_pixbuf_from_data(purple_image_get_data(image), |
|
| 218 purple_image_get_data_size(image)); |
|
| 219 } |
|
| 220 |
|
| 221 GdkPixbuf *pidgin_pixbuf_new_from_file(const gchar *filename) |
|
| 222 { |
|
| 223 GdkPixbuf *pixbuf; |
|
| 224 GError *error = NULL; |
|
| 225 |
|
| 226 g_return_val_if_fail(filename != NULL, NULL); |
|
| 227 g_return_val_if_fail(filename[0] != '\0', NULL); |
|
| 228 |
|
| 229 pixbuf = gdk_pixbuf_new_from_file(filename, &error); |
|
| 230 if (!pixbuf || error) { |
|
| 231 purple_debug_warning("gtkutils", "gdk_pixbuf_new_from_file() " |
|
| 232 "returned %s for file %s: %s\n", |
|
| 233 pixbuf ? "something" : "nothing", |
|
| 234 filename, |
|
| 235 error ? error->message : "(no error message)"); |
|
| 236 if (error) |
|
| 237 g_error_free(error); |
|
| 238 if (pixbuf) |
|
| 239 g_object_unref(G_OBJECT(pixbuf)); |
|
| 240 return NULL; |
|
| 241 } |
|
| 242 |
|
| 243 return pixbuf; |
|
| 244 } |
|
| 245 |
|
| 246 GdkPixbuf *pidgin_pixbuf_new_from_file_at_size(const char *filename, int width, int height) |
|
| 247 { |
|
| 248 GdkPixbuf *pixbuf; |
|
| 249 GError *error = NULL; |
|
| 250 |
|
| 251 g_return_val_if_fail(filename != NULL, NULL); |
|
| 252 g_return_val_if_fail(filename[0] != '\0', NULL); |
|
| 253 |
|
| 254 pixbuf = gdk_pixbuf_new_from_file_at_size(filename, |
|
| 255 width, height, &error); |
|
| 256 if (!pixbuf || error) { |
|
| 257 purple_debug_warning("gtkutils", "gdk_pixbuf_new_from_file_at_size() " |
|
| 258 "returned %s for file %s: %s\n", |
|
| 259 pixbuf ? "something" : "nothing", |
|
| 260 filename, |
|
| 261 error ? error->message : "(no error message)"); |
|
| 262 if (error) |
|
| 263 g_error_free(error); |
|
| 264 if (pixbuf) |
|
| 265 g_object_unref(G_OBJECT(pixbuf)); |
|
| 266 return NULL; |
|
| 267 } |
|
| 268 |
|
| 269 return pixbuf; |
|
| 270 } |
|
| 271 |
|
| 272 GdkPixbuf *pidgin_pixbuf_new_from_file_at_scale(const char *filename, int width, int height, gboolean preserve_aspect_ratio) |
|
| 273 { |
|
| 274 GdkPixbuf *pixbuf; |
|
| 275 GError *error = NULL; |
|
| 276 |
|
| 277 g_return_val_if_fail(filename != NULL, NULL); |
|
| 278 g_return_val_if_fail(filename[0] != '\0', NULL); |
|
| 279 |
|
| 280 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, |
|
| 281 width, height, preserve_aspect_ratio, &error); |
|
| 282 if (!pixbuf || error) { |
|
| 283 purple_debug_warning("gtkutils", "gdk_pixbuf_new_from_file_at_scale() " |
|
| 284 "returned %s for file %s: %s\n", |
|
| 285 pixbuf ? "something" : "nothing", |
|
| 286 filename, |
|
| 287 error ? error->message : "(no error message)"); |
|
| 288 if (error) |
|
| 289 g_error_free(error); |
|
| 290 if (pixbuf) |
|
| 291 g_object_unref(G_OBJECT(pixbuf)); |
|
| 292 return NULL; |
|
| 293 } |
|
| 294 |
|
| 295 return pixbuf; |
|
| 296 } |
|
| 297 |
|
| 298 GdkPixbuf * |
|
| 299 pidgin_pixbuf_scale_down(GdkPixbuf *src, guint max_width, guint max_height, |
|
| 300 GdkInterpType interp_type, gboolean preserve_ratio) |
|
| 301 { |
|
| 302 guint cur_w, cur_h; |
|
| 303 GdkPixbuf *dst; |
|
| 304 |
|
| 305 g_return_val_if_fail(src != NULL, NULL); |
|
| 306 |
|
| 307 if (max_width == 0 || max_height == 0) { |
|
| 308 g_object_unref(src); |
|
| 309 g_return_val_if_reached(NULL); |
|
| 310 } |
|
| 311 |
|
| 312 cur_w = gdk_pixbuf_get_width(src); |
|
| 313 cur_h = gdk_pixbuf_get_height(src); |
|
| 314 |
|
| 315 if (cur_w <= max_width && cur_h <= max_height) |
|
| 316 return src; |
|
| 317 |
|
| 318 /* cur_ratio = cur_w / cur_h |
|
| 319 * max_ratio = max_w / max_h |
|
| 320 */ |
|
| 321 |
|
| 322 if (!preserve_ratio) { |
|
| 323 cur_w = MIN(cur_w, max_width); |
|
| 324 cur_h = MIN(cur_h, max_height); |
|
| 325 } else if ((guint64)cur_w * max_height > (guint64)max_width * cur_h) { |
|
| 326 /* cur_w / cur_h > max_width / max_height */ |
|
| 327 cur_h = (guint64)max_width * cur_h / cur_w; |
|
| 328 cur_w = max_width; |
|
| 329 } else { |
|
| 330 cur_w = (guint64)max_height * cur_w / cur_h; |
|
| 331 cur_h = max_height; |
|
| 332 } |
|
| 333 |
|
| 334 if (cur_w <= 0) |
|
| 335 cur_w = 1; |
|
| 336 if (cur_h <= 0) |
|
| 337 cur_h = 1; |
|
| 338 |
|
| 339 dst = gdk_pixbuf_scale_simple(src, cur_w, cur_h, interp_type); |
|
| 340 g_object_unref(src); |
|
| 341 |
|
| 342 return dst; |
|
| 343 } |
|