static void test_serialize (void) { GError *error = NULL; GdkPixbuf *pixbuf; GdkPixbuf *pixbuf2; GVariant *data; GIcon *icon; GInputStream *stream; pixbuf = gdk_pixbuf_new_from_file (g_test_get_filename (G_TEST_DIST, "test-image.png", NULL), &error); g_assert_no_error (error); g_assert (pixbuf != NULL); /* turn it into a GVariant */ data = g_icon_serialize (G_ICON (pixbuf)); /* back to a GIcon, but this will be a GBytesIcon, not GdkPixbuf */ icon = g_icon_deserialize (data); g_assert (G_IS_BYTES_ICON (icon)); /* but since that is a GLoadableIcon, we can load it again */ stream = g_loadable_icon_load (G_LOADABLE_ICON (icon), 0, NULL, NULL, &error); g_assert_no_error (error); pixbuf2 = gdk_pixbuf_new_from_stream (stream, NULL, &error); g_assert_no_error (error); /* make sure that the pixels are the same. * our _serialize() uses png, so this should be perfect. */ { guchar *pixels_a, *pixels_b; guint len_a, len_b; pixels_a = gdk_pixbuf_get_pixels_with_length (pixbuf, &len_a); pixels_b = gdk_pixbuf_get_pixels_with_length (pixbuf2, &len_b); g_assert (len_a == len_b); g_assert (memcmp (pixels_a, pixels_b, len_a) == 0); } g_object_unref (pixbuf2); g_object_unref (pixbuf); g_object_unref (stream); g_variant_unref (data); }
/** * as_image_set_pixbuf: * @image: a #AsImage instance. * @pixbuf: the #GdkPixbuf, or %NULL * * Sets the image pixbuf. * * Since: 0.1.6 **/ void as_image_set_pixbuf (AsImage *image, GdkPixbuf *pixbuf) { AsImagePrivate *priv = GET_PRIVATE (image); guchar *data; guint len; g_set_object (&priv->pixbuf, pixbuf); if (pixbuf == NULL) return; if (priv->md5 == NULL) { data = gdk_pixbuf_get_pixels_with_length (pixbuf, &len); priv->md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5, data, len); } priv->width = gdk_pixbuf_get_width (pixbuf); priv->height = gdk_pixbuf_get_height (pixbuf); }
/** * as_image_set_pixbuf: * @image: a #AsImage instance. * @pixbuf: the #GdkPixbuf, or %NULL * * Sets the image pixbuf. * * Since: 0.1.6 **/ void as_image_set_pixbuf (AsImage *image, GdkPixbuf *pixbuf) { AsImagePrivate *priv = GET_PRIVATE (image); guchar *data; guint len; g_set_object (&priv->pixbuf, pixbuf); if (pixbuf == NULL) return; if (priv->md5 == NULL) { g_autofree gchar *md5_tmp = NULL; data = gdk_pixbuf_get_pixels_with_length (pixbuf, &len); md5_tmp = g_compute_checksum_for_data (G_CHECKSUM_MD5, data, len); as_ref_string_assign_safe (&priv->md5, md5_tmp); } priv->width = (guint) gdk_pixbuf_get_width (pixbuf); priv->height = (guint) gdk_pixbuf_get_height (pixbuf); }
/** * gd_create_variant_from_pixbuf: * @pixbuf: * * Returns: (transfer full): */ GVariant * gd_create_variant_from_pixbuf (GdkPixbuf *pixbuf) { GVariant *variant; guchar *data; guint length; data = gdk_pixbuf_get_pixels_with_length (pixbuf, &length); variant = g_variant_new ("(iiibii@ay)", gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), gdk_pixbuf_get_rowstride (pixbuf), gdk_pixbuf_get_has_alpha (pixbuf), gdk_pixbuf_get_bits_per_sample (pixbuf), gdk_pixbuf_get_n_channels (pixbuf), g_variant_new_from_data (G_VARIANT_TYPE_BYTESTRING, data, length, TRUE, (GDestroyNotify)g_object_unref, g_object_ref (pixbuf))); return g_variant_ref_sink (variant); }
char* pixbuf_to_canvas_data(GdkPixbuf* pixbuf) { guchar* buf = NULL; int size = 0; int width = gdk_pixbuf_get_width(pixbuf); int height = gdk_pixbuf_get_height(pixbuf); int stride = gdk_pixbuf_get_rowstride(pixbuf); int pix_bit = stride / width; int offset = 0; buf = gdk_pixbuf_get_pixels_with_length(pixbuf, (guint*)&size); g_assert(buf != NULL); GString* string = g_string_sized_new(height * stride + 10); g_string_append_c(string, '['); if (pix_bit == 4) { for (int i=0; i<height; i++) { for (int j=0; j<width; j++) { offset = i * stride + j*4; g_string_append_printf(string, "%d,%d,%d,%d,", buf[offset], buf[offset+1], buf[offset+2], buf[offset+3]); } } } else if (pix_bit == 3) { for (int i=0; i<height; i++) { for (int j=0; j<width; j++) { offset = i * stride + j*3; g_string_append_printf(string, "%d,%d,%d,255,", buf[offset], buf[offset+1], buf[offset+2]); } } } g_string_overwrite(string, string->len-1, "]"); return g_string_free(string, FALSE); }
/** * gdk_pixbuf_get_pixels: * @pixbuf: A pixbuf. * * Queries a pointer to the pixel data of a pixbuf. * * Return value: (array): A pointer to the pixbuf's pixel data. * Please see the section on [image data][image-data] for information * about how the pixel data is stored in memory. * * This function will cause an implicit copy of the pixbuf data if the * pixbuf was created from read-only data. **/ guchar * gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf) { return gdk_pixbuf_get_pixels_with_length (pixbuf, NULL); }