Ejemplo n.º 1
0
/**
 * gdk_texture_save_to_png:
 * @texture: a #GdkTexture
 * @filename: the filename to store to
 *
 * Store the given @texture to the @filename as a PNG file.
 *
 * This is a utility function intended for debugging and testing.
 * If you want more control over formats, proper error handling or
 * want to store to a #GFile or other location, you might want to
 * look into using the gdk-pixbuf library.
 *
 * Returns: %TRUE if saving succeeded, %FALSE on failure.
 **/
gboolean
gdk_texture_save_to_png (GdkTexture *texture,
                         const char *filename)
{
  cairo_surface_t *surface;
  cairo_status_t status;
  gboolean result;

  g_return_val_if_fail (GDK_IS_TEXTURE (texture), FALSE);
  g_return_val_if_fail (filename != NULL, FALSE);

  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                        gdk_texture_get_width (texture),
                                        gdk_texture_get_height (texture));
  gdk_texture_download (texture,
                        cairo_image_surface_get_data (surface),
                        cairo_image_surface_get_stride (surface));
  cairo_surface_mark_dirty (surface);

  status = cairo_surface_write_to_png (surface, filename);

  if (status != CAIRO_STATUS_SUCCESS ||
      cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
    result = FALSE;
  else
    result = TRUE;

  cairo_surface_destroy (surface);

  return result;
}
Ejemplo n.º 2
0
GtkWidget *
cb_media_image_widget_new (CbMedia *media)
{
  CbMediaImageWidget *self;

  g_return_val_if_fail (CB_IS_MEDIA (media), NULL);
  g_return_val_if_fail (!media->invalid, NULL);
  g_return_val_if_fail (media->texture != NULL, NULL);

  self = CB_MEDIA_IMAGE_WIDGET (g_object_new (CB_TYPE_MEDIA_IMAGE_WIDGET, NULL));

  if (media->type == CB_MEDIA_TYPE_GIF)
    g_warning ("Maybe remove the GIF handling support!");
    /*gtk_image_set_from_animation (GTK_IMAGE (self->image), media->animation);*/
  else
    gtk_image_set_from_paintable(GTK_IMAGE (self->image), GDK_PAINTABLE (media->texture));

  self->img_width  = gdk_texture_get_width (media->texture);
  self->img_height = gdk_texture_get_height (media->texture);

  return GTK_WIDGET (self);
}
Ejemplo n.º 3
0
/*
 * Arguments:
 *   1) .node file to compare
 *   2) .png file to compare the rendered .node file to
 */
int
main (int argc, char **argv)
{
  cairo_surface_t *reference_surface = NULL;
  cairo_surface_t *rendered_surface = NULL;
  cairo_surface_t *diff_surface = NULL;
  GdkTexture *texture;
  GskRenderer *renderer;
  GdkSurface *window;
  GskRenderNode *node;
  const char *node_file;
  const char *png_file;

  g_assert (argc == 3);

  gtk_init ();

  node_file = argv[1];
  png_file = argv[2];

  window = gdk_surface_new_toplevel (gdk_display_get_default(), 10 , 10);
  renderer = gsk_renderer_new_for_surface (window);

  g_test_message ("Node file: '%s'\n", node_file);
  g_test_message ("PNG file: '%s'\n", png_file);

  /* Load the render node from the given .node file */
  {
    GBytes *bytes;
    GError *error = NULL;
    gsize len;
    char *contents;

    if (!g_file_get_contents (node_file, &contents, &len, &error))
      {
        g_test_message ("Could not open node file: %s\n", error->message);
        g_clear_error (&error);
        g_test_fail ();
        return -1;
      }

    bytes = g_bytes_new_take (contents, len);
    node = gsk_render_node_deserialize (bytes, &error);
    g_bytes_unref (bytes);

    g_assert (node != NULL);
  }

  /* Render the .node file and download to cairo surface */
  texture = gsk_renderer_render_texture (renderer, node, NULL);
  g_assert (texture != NULL);

  rendered_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                                 gdk_texture_get_width (texture),
                                                 gdk_texture_get_height (texture));
  gdk_texture_download (texture,
                        cairo_image_surface_get_data (rendered_surface),
                        cairo_image_surface_get_stride (rendered_surface));
  cairo_surface_mark_dirty (rendered_surface);

  /* Load the given reference png file */
  reference_surface = cairo_image_surface_create_from_png (png_file);
  g_assert (reference_surface != NULL);

  /* Now compare the two */
  diff_surface = reftest_compare_surfaces (rendered_surface, reference_surface);

  save_image (rendered_surface, node_file, ".out.png");

  if (diff_surface)
    save_image (diff_surface, node_file, ".diff.png");

  g_assert (diff_surface == NULL);

  return 0;
}