mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2025-01-12 21:40:19 +00:00
Revert "shooter: Use the reftests code for taking screenshots"
This reverts commit 5c926ca6bb
.
This commit just dropped the nice shadow that we used to have
around these shots, without a justification.
This commit is contained in:
parent
a92859a447
commit
671b3181b0
@ -26,6 +26,8 @@ endif
|
|||||||
doc_shooter_DEPENDENCIES = $(DEPS)
|
doc_shooter_DEPENDENCIES = $(DEPS)
|
||||||
doc_shooter_LDADD = $(LDADDS)
|
doc_shooter_LDADD = $(LDADDS)
|
||||||
doc_shooter_SOURCES= \
|
doc_shooter_SOURCES= \
|
||||||
|
shadow.c \
|
||||||
|
shadow.h \
|
||||||
shooter.c \
|
shooter.c \
|
||||||
widgets.c \
|
widgets.c \
|
||||||
widgets.h
|
widgets.h
|
||||||
|
149
docs/tools/shadow.c
Normal file
149
docs/tools/shadow.c
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
#include "shadow.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define BLUR_RADIUS 5
|
||||||
|
#define SHADOW_OFFSET (BLUR_RADIUS * 4 / 5)
|
||||||
|
#define SHADOW_OPACITY 0.75
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int size;
|
||||||
|
double *data;
|
||||||
|
} ConvFilter;
|
||||||
|
|
||||||
|
static double
|
||||||
|
gaussian (double x, double y, double r)
|
||||||
|
{
|
||||||
|
return ((1 / (2 * M_PI * r)) *
|
||||||
|
exp ((- (x * x + y * y)) / (2 * r * r)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static ConvFilter *
|
||||||
|
create_blur_filter (int radius)
|
||||||
|
{
|
||||||
|
ConvFilter *filter;
|
||||||
|
int x, y;
|
||||||
|
double sum;
|
||||||
|
|
||||||
|
filter = g_new0 (ConvFilter, 1);
|
||||||
|
filter->size = radius * 2 + 1;
|
||||||
|
filter->data = g_new (double, filter->size * filter->size);
|
||||||
|
|
||||||
|
sum = 0.0;
|
||||||
|
|
||||||
|
for (y = 0 ; y < filter->size; y++)
|
||||||
|
{
|
||||||
|
for (x = 0 ; x < filter->size; x++)
|
||||||
|
{
|
||||||
|
sum += filter->data[y * filter->size + x] = gaussian (x - (filter->size >> 1),
|
||||||
|
y - (filter->size >> 1),
|
||||||
|
radius);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (y = 0; y < filter->size; y++)
|
||||||
|
{
|
||||||
|
for (x = 0; x < filter->size; x++)
|
||||||
|
{
|
||||||
|
filter->data[y * filter->size + x] /= sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return filter;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static GdkPixbuf *
|
||||||
|
create_shadow (GdkPixbuf *src)
|
||||||
|
{
|
||||||
|
int x, y, i, j;
|
||||||
|
int width, height;
|
||||||
|
GdkPixbuf *dest;
|
||||||
|
static ConvFilter *filter = NULL;
|
||||||
|
int src_rowstride, dest_rowstride;
|
||||||
|
int src_bpp, dest_bpp;
|
||||||
|
|
||||||
|
guchar *src_pixels, *dest_pixels;
|
||||||
|
|
||||||
|
if (!filter)
|
||||||
|
filter = create_blur_filter (BLUR_RADIUS);
|
||||||
|
|
||||||
|
width = gdk_pixbuf_get_width (src) + BLUR_RADIUS * 2 + SHADOW_OFFSET;
|
||||||
|
height = gdk_pixbuf_get_height (src) + BLUR_RADIUS * 2 + SHADOW_OFFSET;
|
||||||
|
|
||||||
|
dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
|
||||||
|
gdk_pixbuf_get_has_alpha (src),
|
||||||
|
gdk_pixbuf_get_bits_per_sample (src),
|
||||||
|
width, height);
|
||||||
|
gdk_pixbuf_fill (dest, 0);
|
||||||
|
src_pixels = gdk_pixbuf_get_pixels (src);
|
||||||
|
src_rowstride = gdk_pixbuf_get_rowstride (src);
|
||||||
|
src_bpp = gdk_pixbuf_get_has_alpha (src) ? 4 : 3;
|
||||||
|
|
||||||
|
dest_pixels = gdk_pixbuf_get_pixels (dest);
|
||||||
|
dest_rowstride = gdk_pixbuf_get_rowstride (dest);
|
||||||
|
dest_bpp = gdk_pixbuf_get_has_alpha (dest) ? 4 : 3;
|
||||||
|
|
||||||
|
for (y = 0; y < height; y++)
|
||||||
|
{
|
||||||
|
for (x = 0; x < width; x++)
|
||||||
|
{
|
||||||
|
int sumr = 0, sumg = 0, sumb = 0, suma = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < filter->size; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < filter->size; j++)
|
||||||
|
{
|
||||||
|
int src_x, src_y;
|
||||||
|
|
||||||
|
src_y = -(BLUR_RADIUS + SHADOW_OFFSET) + y - (filter->size >> 1) + i;
|
||||||
|
src_x = -(BLUR_RADIUS + SHADOW_OFFSET) + x - (filter->size >> 1) + j;
|
||||||
|
|
||||||
|
if (src_y < 0 || src_y > gdk_pixbuf_get_height (src) ||
|
||||||
|
src_x < 0 || src_x > gdk_pixbuf_get_width (src))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
sumr += src_pixels [src_y * src_rowstride +
|
||||||
|
src_x * src_bpp + 0] *
|
||||||
|
filter->data [i * filter->size + j];
|
||||||
|
sumg += src_pixels [src_y * src_rowstride +
|
||||||
|
src_x * src_bpp + 1] *
|
||||||
|
filter->data [i * filter->size + j];
|
||||||
|
|
||||||
|
sumb += src_pixels [src_y * src_rowstride +
|
||||||
|
src_x * src_bpp + 2] *
|
||||||
|
filter->data [i * filter->size + j];
|
||||||
|
|
||||||
|
if (src_bpp == 4)
|
||||||
|
suma += src_pixels [src_y * src_rowstride +
|
||||||
|
src_x * src_bpp + 3] *
|
||||||
|
filter->data [i * filter->size + j];
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dest_bpp == 4)
|
||||||
|
dest_pixels [y * dest_rowstride +
|
||||||
|
x * dest_bpp + 3] = suma * SHADOW_OPACITY;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
GdkPixbuf *
|
||||||
|
create_shadowed_pixbuf (GdkPixbuf *src)
|
||||||
|
{
|
||||||
|
GdkPixbuf *dest;
|
||||||
|
|
||||||
|
dest = create_shadow (src);
|
||||||
|
|
||||||
|
gdk_pixbuf_composite (src, dest,
|
||||||
|
BLUR_RADIUS, BLUR_RADIUS,
|
||||||
|
gdk_pixbuf_get_width (src),
|
||||||
|
gdk_pixbuf_get_height (src),
|
||||||
|
BLUR_RADIUS, BLUR_RADIUS, 1.0, 1.0,
|
||||||
|
GDK_INTERP_NEAREST, 255);
|
||||||
|
return dest;
|
||||||
|
}
|
8
docs/tools/shadow.h
Normal file
8
docs/tools/shadow.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef __SHADOW_H__
|
||||||
|
#define __SHADOW_H__
|
||||||
|
|
||||||
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
||||||
|
|
||||||
|
GdkPixbuf *create_shadowed_pixbuf (GdkPixbuf *src);
|
||||||
|
|
||||||
|
#endif /* __SHADOW_H__ */
|
@ -1,101 +1,184 @@
|
|||||||
|
#include <gdk/gdk.h>
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
|
#include <gdkx.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <X11/extensions/shape.h>
|
||||||
|
|
||||||
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <locale.h>
|
||||||
#include "widgets.h"
|
#include "widgets.h"
|
||||||
|
#include "shadow.h"
|
||||||
|
|
||||||
typedef enum {
|
#define MAXIMUM_WM_REPARENTING_DEPTH 4
|
||||||
SNAPSHOT_WINDOW,
|
#ifndef _
|
||||||
SNAPSHOT_DRAW
|
#define _(x) (x)
|
||||||
} SnapshotMode;
|
#endif
|
||||||
|
|
||||||
static gboolean
|
static Window
|
||||||
quit_when_idle (gpointer loop)
|
find_toplevel_window (Window xid)
|
||||||
{
|
{
|
||||||
g_main_loop_quit (loop);
|
Window root, parent, *children;
|
||||||
|
guint nchildren;
|
||||||
|
|
||||||
return G_SOURCE_REMOVE;
|
do
|
||||||
|
{
|
||||||
|
if (XQueryTree (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xid, &root,
|
||||||
|
&parent, &children, &nchildren) == 0)
|
||||||
|
{
|
||||||
|
g_warning ("Couldn't find window manager window");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root == parent)
|
||||||
|
return xid;
|
||||||
|
|
||||||
|
xid = parent;
|
||||||
|
}
|
||||||
|
while (TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static GdkPixbuf *
|
||||||
check_for_draw (GdkEvent *event, gpointer loop)
|
add_border_to_shot (GdkPixbuf *pixbuf)
|
||||||
{
|
{
|
||||||
if (event->type == GDK_EXPOSE)
|
GdkPixbuf *retval;
|
||||||
{
|
|
||||||
g_idle_add (quit_when_idle, loop);
|
|
||||||
gdk_event_handler_set ((GdkEventFunc) gtk_main_do_event, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
gtk_main_do_event (event);
|
retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
|
||||||
|
gdk_pixbuf_get_width (pixbuf) + 2,
|
||||||
|
gdk_pixbuf_get_height (pixbuf) + 2);
|
||||||
|
|
||||||
|
/* Fill with solid black */
|
||||||
|
gdk_pixbuf_fill (retval, 0xFF);
|
||||||
|
gdk_pixbuf_copy_area (pixbuf,
|
||||||
|
0, 0,
|
||||||
|
gdk_pixbuf_get_width (pixbuf),
|
||||||
|
gdk_pixbuf_get_height (pixbuf),
|
||||||
|
retval, 1, 1);
|
||||||
|
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static cairo_surface_t *
|
static GdkPixbuf *
|
||||||
snapshot_widget (GtkWidget *widget, SnapshotMode mode)
|
remove_shaped_area (GdkPixbuf *pixbuf,
|
||||||
|
Window window)
|
||||||
{
|
{
|
||||||
cairo_surface_t *surface;
|
GdkPixbuf *retval;
|
||||||
cairo_pattern_t *bg;
|
XRectangle *rectangles;
|
||||||
GMainLoop *loop;
|
int rectangle_count, rectangle_order;
|
||||||
cairo_t *cr;
|
int i;
|
||||||
|
|
||||||
g_assert (gtk_widget_get_realized (widget));
|
retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
|
||||||
|
gdk_pixbuf_get_width (pixbuf),
|
||||||
|
gdk_pixbuf_get_height (pixbuf));
|
||||||
|
|
||||||
|
gdk_pixbuf_fill (retval, 0);
|
||||||
|
rectangles = XShapeGetRectangles (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), window,
|
||||||
|
ShapeBounding, &rectangle_count, &rectangle_order);
|
||||||
|
|
||||||
surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
|
for (i = 0; i < rectangle_count; i++)
|
||||||
CAIRO_CONTENT_COLOR,
|
|
||||||
gtk_widget_get_allocated_width (widget),
|
|
||||||
gtk_widget_get_allocated_height (widget));
|
|
||||||
|
|
||||||
loop = g_main_loop_new (NULL, FALSE);
|
|
||||||
/* We wait until the widget is drawn for the first time.
|
|
||||||
* We can not wait for a GtkWidget::draw event, because that might not
|
|
||||||
* happen if the window is fully obscured by windowed child widgets.
|
|
||||||
* Alternatively, we could wait for an expose event on widget's window.
|
|
||||||
* Both of these are rather hairy, not sure what's best. */
|
|
||||||
gdk_event_handler_set (check_for_draw, loop, NULL);
|
|
||||||
g_main_loop_run (loop);
|
|
||||||
|
|
||||||
cr = cairo_create (surface);
|
|
||||||
|
|
||||||
switch (mode)
|
|
||||||
{
|
{
|
||||||
case SNAPSHOT_WINDOW:
|
int y, x;
|
||||||
{
|
|
||||||
GdkWindow *window = gtk_widget_get_window (widget);
|
for (y = rectangles[i].y; y < rectangles[i].y + rectangles[i].height; y++)
|
||||||
if (gdk_window_get_window_type (window) == GDK_WINDOW_TOPLEVEL ||
|
{
|
||||||
gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
|
guchar *src_pixels, *dest_pixels;
|
||||||
{
|
|
||||||
/* give the WM/server some time to sync. They need it.
|
src_pixels = gdk_pixbuf_get_pixels (pixbuf) +
|
||||||
* Also, do use popups instead of toplevls in your tests
|
y * gdk_pixbuf_get_rowstride (pixbuf) +
|
||||||
* whenever you can. */
|
rectangles[i].x * (gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3);
|
||||||
gdk_display_sync (gdk_window_get_display (window));
|
dest_pixels = gdk_pixbuf_get_pixels (retval) +
|
||||||
g_timeout_add (500, quit_when_idle, loop);
|
y * gdk_pixbuf_get_rowstride (retval) +
|
||||||
g_main_loop_run (loop);
|
rectangles[i].x * 4;
|
||||||
}
|
|
||||||
gdk_cairo_set_source_window (cr, window, 0, 0);
|
for (x = rectangles[i].x; x < rectangles[i].x + rectangles[i].width; x++)
|
||||||
cairo_paint (cr);
|
{
|
||||||
}
|
*dest_pixels++ = *src_pixels ++;
|
||||||
break;
|
*dest_pixels++ = *src_pixels ++;
|
||||||
case SNAPSHOT_DRAW:
|
*dest_pixels++ = *src_pixels ++;
|
||||||
bg = gdk_window_get_background_pattern (gtk_widget_get_window (widget));
|
*dest_pixels++ = 255;
|
||||||
if (bg)
|
|
||||||
{
|
if (gdk_pixbuf_get_has_alpha (pixbuf))
|
||||||
cairo_set_source (cr, bg);
|
src_pixels++;
|
||||||
cairo_paint (cr);
|
}
|
||||||
}
|
}
|
||||||
gtk_widget_draw (widget, cr);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
g_assert_not_reached();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cairo_destroy (cr);
|
return retval;
|
||||||
g_main_loop_unref (loop);
|
}
|
||||||
gtk_widget_destroy (widget);
|
|
||||||
|
|
||||||
return surface;
|
static GdkPixbuf *
|
||||||
|
take_window_shot (Window child,
|
||||||
|
gboolean include_decoration)
|
||||||
|
{
|
||||||
|
GdkWindow *window;
|
||||||
|
Window xid;
|
||||||
|
gint x_orig, y_orig;
|
||||||
|
gint x = 0, y = 0;
|
||||||
|
gint width, height;
|
||||||
|
|
||||||
|
GdkPixbuf *tmp, *tmp2;
|
||||||
|
GdkPixbuf *retval;
|
||||||
|
|
||||||
|
if (include_decoration)
|
||||||
|
xid = find_toplevel_window (child);
|
||||||
|
else
|
||||||
|
xid = child;
|
||||||
|
|
||||||
|
window = gdk_x11_window_foreign_new_for_display (gdk_display_get_default (), xid);
|
||||||
|
|
||||||
|
width = gdk_window_get_width (window);
|
||||||
|
height = gdk_window_get_height (window);
|
||||||
|
gdk_window_get_origin (window, &x_orig, &y_orig);
|
||||||
|
|
||||||
|
if (x_orig < 0)
|
||||||
|
{
|
||||||
|
x = - x_orig;
|
||||||
|
width = width + x_orig;
|
||||||
|
x_orig = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y_orig < 0)
|
||||||
|
{
|
||||||
|
y = - y_orig;
|
||||||
|
height = height + y_orig;
|
||||||
|
y_orig = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x_orig + width > gdk_screen_width ())
|
||||||
|
width = gdk_screen_width () - x_orig;
|
||||||
|
|
||||||
|
if (y_orig + height > gdk_screen_height ())
|
||||||
|
height = gdk_screen_height () - y_orig;
|
||||||
|
|
||||||
|
tmp = gdk_pixbuf_get_from_window (window,
|
||||||
|
x, y, width, height);
|
||||||
|
|
||||||
|
if (include_decoration)
|
||||||
|
tmp2 = remove_shaped_area (tmp, xid);
|
||||||
|
else
|
||||||
|
tmp2 = add_border_to_shot (tmp);
|
||||||
|
|
||||||
|
retval = create_shadowed_pixbuf (tmp2);
|
||||||
|
g_object_unref (tmp);
|
||||||
|
g_object_unref (tmp2);
|
||||||
|
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (int argc, char **argv)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
GList *toplevels;
|
GList *toplevels;
|
||||||
|
GdkPixbuf *screenshot = NULL;
|
||||||
GList *node;
|
GList *node;
|
||||||
|
|
||||||
/* If there's no DISPLAY, we silently error out. We don't want to break
|
/* If there's no DISPLAY, we silently error out. We don't want to break
|
||||||
@ -107,19 +190,42 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
for (node = toplevels; node; node = g_list_next (node))
|
for (node = toplevels; node; node = g_list_next (node))
|
||||||
{
|
{
|
||||||
|
GtkAllocation allocation;
|
||||||
|
GdkWindow *window;
|
||||||
WidgetInfo *info;
|
WidgetInfo *info;
|
||||||
|
XID id;
|
||||||
char *filename;
|
char *filename;
|
||||||
cairo_surface_t *surface;
|
|
||||||
|
|
||||||
info = node->data;
|
info = node->data;
|
||||||
|
|
||||||
gtk_widget_show (info->window);
|
gtk_widget_show (info->window);
|
||||||
|
|
||||||
surface = snapshot_widget (info->window,
|
window = gtk_widget_get_window (info->window);
|
||||||
info->include_decorations ? SNAPSHOT_WINDOW : SNAPSHOT_DRAW);
|
gtk_widget_get_allocation (info->window, &allocation);
|
||||||
|
|
||||||
|
gtk_widget_show_now (info->window);
|
||||||
|
gtk_widget_queue_draw_area (info->window,
|
||||||
|
allocation.x, allocation.y,
|
||||||
|
allocation.width, allocation.height);
|
||||||
|
gdk_window_process_updates (window, TRUE);
|
||||||
|
|
||||||
|
while (gtk_events_pending ())
|
||||||
|
{
|
||||||
|
gtk_main_iteration ();
|
||||||
|
}
|
||||||
|
sleep (1);
|
||||||
|
|
||||||
|
while (gtk_events_pending ())
|
||||||
|
{
|
||||||
|
gtk_main_iteration ();
|
||||||
|
}
|
||||||
|
|
||||||
|
id = gdk_x11_window_get_xid (window);
|
||||||
|
screenshot = take_window_shot (id, info->include_decorations);
|
||||||
filename = g_strdup_printf ("./%s.png", info->name);
|
filename = g_strdup_printf ("./%s.png", info->name);
|
||||||
g_assert (cairo_surface_write_to_png (surface, filename) == CAIRO_STATUS_SUCCESS);
|
gdk_pixbuf_save (screenshot, filename, "png", NULL, NULL);
|
||||||
g_free (filename);
|
g_free(filename);
|
||||||
|
gtk_widget_hide (info->window);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user