static GdkPixbuf* get_pixbuf (void) { GdkPixbuf *screenshot; GdkPixbuf *magnified; #if 0 g_print ("Size %d x %d\n", last_grab_width, last_grab_height); #endif screenshot = gdk_pixbuf_get_from_window(gdk_get_default_root_window(), last_grab_x, last_grab_y, last_grab_width, last_grab_height); if (screenshot == NULL) { g_printerr ("Screenshot failed\n"); exit (1); } magnified = gdk_pixbuf_scale_simple (screenshot, last_grab_width * width_factor, last_grab_height * height_factor, interp_mode); g_object_unref (G_OBJECT (screenshot)); return magnified; }
/*! * Take a screenshot of the graphics and convert it to an image. */ GR_Image * GR_UnixCairoGraphics::genImageFromRectangle(const UT_Rect &rec) { UT_sint32 idx = _tduX(rec.left); UT_sint32 idy = _tduY(rec.top); UT_sint32 idw = _tduR(rec.width); UT_sint32 idh = _tduR(rec.height); UT_return_val_if_fail (idw > 0 && idh > 0 && idx >= 0, NULL); cairo_surface_flush ( cairo_get_target(m_cr)); #if !GTK_CHECK_VERSION(3,0,0) GdkColormap* cmp = gdk_colormap_get_system(); GdkPixbuf * pix = gdk_pixbuf_get_from_drawable(NULL, _getWindow(), cmp, idx, idy, 0, 0, idw, idh); #else GdkPixbuf * pix = gdk_pixbuf_get_from_window(getWindow(), idx, idy, idw, idh); #endif UT_return_val_if_fail(pix, NULL); GR_UnixImage * pImg = new GR_UnixImage("ScreenShot"); pImg->setData(pix); pImg->setDisplaySize(idw,idh); return pImg; }
int grab_window(int src_x, int src_y, int width, int height) { GdkWindow *window, *root; #ifndef GTK3 window = gdk_window_foreign_new(GDK_ROOT_WINDOW()); root = gdk_window_foreign_new(GDK_ROOT_WINDOW()); #else GdkDisplay *gdpy = gdk_display_get_default(); window = root = gdk_x11_window_foreign_new_for_display(gdpy, GDK_ROOT_WINDOW()); #endif GdkPixbuf *screenshot; GError *error = NULL; if (src_x + width > gdk_screen_width ()) width = gdk_screen_width () - src_x; if (src_y + height > gdk_screen_height ()) height = gdk_screen_height () - src_y; time_t now; time(&now); char path[MAXPATHLEN]; snprintf(path, sizeof(path), "%s%i%s", "./screen-shot-window", now, ".png"); printf("%s\n", path); #ifndef GTK3 screenshot = gdk_pixbuf_get_from_drawable (NULL, root, NULL, src_x, src_y, 0, 0, width, height); #else screenshot = gdk_pixbuf_get_from_window(root, src_x, src_y, width, height); #endif printf("----------------%d\n", gdk_pixbuf_get_rowstride(screenshot)); gdk_pixbuf_save (screenshot, path, "png", &error, "tEXt::CREATOR", "gnome-panel-screenshot", NULL); return 0; }
int main (int argc, char **argv) { GdkWindow *root; GdkPixbuf *pixbuf; gboolean ret; gint retval = 1; GError *error = NULL; gtk_init (&argc, &argv); root = gdk_get_default_root_window (); pixbuf = gdk_pixbuf_get_from_window (root, 0, 0, 150, 160); /* PASS */ g_debug ("try to save PNG with a profile"); ret = save_image_png ("icc-profile.png", pixbuf, &error); if (!ret) { g_warning ("FAILED: did not save image: %s", error->message); g_error_free (error); goto out; } /* PASS */ g_debug ("try to save TIFF with a profile"); ret = save_image_tiff ("icc-profile.tiff", pixbuf, &error); if (!ret) { g_warning ("FAILED: did not save image: %s", error->message); g_error_free (error); goto out; } /* PASS */ g_debug ("try to load PNG and get color attributes"); ret = save_image_verify ("icc-profile.png", &error); if (!ret) { g_warning ("FAILED: did not load image: %s", error->message); g_error_free (error); goto out; } /* PASS */ g_debug ("try to load TIFF and get color attributes"); ret = save_image_verify ("icc-profile.tiff", &error); if (!ret) { g_warning ("FAILED: did not load image: %s", error->message); g_error_free (error); goto out; } /* success */ retval = 0; g_debug ("ALL OKAY!"); out: return retval; }
int grab_whole_screen() { GdkWindow *window, *root; #ifndef GTK3 window = gdk_window_foreign_new(GDK_ROOT_WINDOW()); root = gdk_window_foreign_new(GDK_ROOT_WINDOW()); #else GdkDisplay *gdpy = gdk_display_get_default(); window = root = gdk_x11_window_foreign_new_for_display(gdpy, GDK_ROOT_WINDOW()); #endif GdkPixbuf *screenshot; gint x_real_orig, y_real_orig; gint x_orig, y_orig; gint real_width, real_height; gint width, height; GError *error = NULL; #ifndef GTK3 gdk_drawable_get_size(window, &real_width, &real_height); #else real_width = gdk_window_get_width(window); real_height = gdk_window_get_height(window); #endif gdk_window_get_origin(window, &x_real_orig, &y_real_orig); x_orig = x_real_orig; y_orig = y_real_orig; width = real_width; height = real_height; if (x_orig < 0) { width = width + x_orig; x_orig = 0; } if (y_orig < 0) { 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; char path[MAXPATHLEN]; time_t now; time(&now); snprintf(path, sizeof(path), "%s%i%s", "./screen-shot-whole-screen-", now, ".png"); #ifndef GTK3 screenshot = gdk_pixbuf_get_from_drawable (NULL, root, NULL, x_orig, y_orig, 0, 0, width, height); #else screenshot = gdk_pixbuf_get_from_window(root, x_orig, y_orig, width, height); #endif gdk_pixbuf_save (screenshot, path, "png", &error, "tEXt::CREATOR", "gnome-panel-screenshot", NULL); return 0; }
static void get_rgba_at_cursor (GstyleEyedropper *self, GdkScreen *screen, GdkDevice *device, gint x, gint y, GdkRGBA *rgba) { GdkWindow *window; GdkPixbuf *pixbuf; guchar *pixels; g_assert (GSTYLE_IS_EYEDROPPER (self)); g_assert (GDK_IS_SCREEN (screen)); g_assert (GDK_IS_DEVICE (device)); window = gdk_screen_get_root_window (screen); pixbuf = gdk_pixbuf_get_from_window (window, x, y, 1, 1); if (!pixbuf) { window = gdk_device_get_window_at_position (device, &x, &y); if (!window) return; pixbuf = gdk_pixbuf_get_from_window (window, x, y, 1, 1); if (!pixbuf) return; } g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB); g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8); pixels = gdk_pixbuf_get_pixels (pixbuf); rgba->red = pixels[0] / 255.0; rgba->green = pixels[1] / 255.0; rgba->blue = pixels[2] /255.0; rgba->alpha = 1.0; g_object_unref (pixbuf); }
static void gstyle_eyedropper_draw_zoom_area (GstyleEyedropper *self, gint cursor_x, gint cursor_y) { GdkWindow *window; GdkPixbuf *root_pixbuf; gint dst_width; gint dst_height; gint src_width; gint src_height; gint start_x; gint start_y; g_assert (GSTYLE_IS_EYEDROPPER (self)); src_width = ceil (ZOOM_AREA_WIDTH / self->zoom_factor); if (src_width % 2 == 0) src_width += 1; src_height = ceil (ZOOM_AREA_HEIGHT / self->zoom_factor); if (src_height % 2 == 0) src_height += 1; dst_width = src_width * ceil (self->zoom_factor); dst_height = src_height * ceil (self->zoom_factor); self->pixbuf_offset_x = (dst_width - ZOOM_AREA_WIDTH) / 2; self->pixbuf_offset_y = (dst_height - ZOOM_AREA_HEIGHT) / 2; start_x = MAX (cursor_x - src_width / 2, 0); if (start_x + src_width > self->screen_width) start_x = self->screen_width - src_width; start_y = MAX (cursor_y - src_height / 2, 0); if (start_y + src_height > self->screen_height) start_y = self->screen_height - src_height; window = gdk_screen_get_root_window (self->screen); root_pixbuf = gdk_pixbuf_get_from_window (window, start_x, start_y, src_width, src_height); self->offset_x = (cursor_x - start_x + 0.5) * ceil (self->zoom_factor) - self->pixbuf_offset_x; self->offset_y = (cursor_y - start_y + 0.5) * ceil (self->zoom_factor) - self->pixbuf_offset_y; g_clear_object (&self->pixbuf); self->pixbuf = gdk_pixbuf_scale_simple (root_pixbuf, dst_width, dst_height, GDK_INTERP_NEAREST); g_object_unref (root_pixbuf); gtk_widget_queue_draw (self->zoom_area); }
/* Grab the screenshoot and put it in the image buffer. */ GdkPixbuf * grab_screenshot () { gint height = gdk_screen_height (); gint width = gdk_screen_width (); GdkWindow *root_window = gdk_get_default_root_window (); return gdk_pixbuf_get_from_window (root_window, 0, 0, width, height); }
/* Get the background pixbuf. */ static GdkPixbuf * get_background_pixbuf(void) { /* Get the root window pixmap. */ GdkScreen * screen = gdk_screen_get_default(); #ifdef ENABLE_GTK3 GdkPixbuf * pixbuf = gdk_pixbuf_get_from_window( gdk_get_default_root_window(), 0, 0, gdk_screen_get_width(screen), /* Width */ gdk_screen_get_height(screen)); /* Height */ #else GdkPixbuf * pixbuf = gdk_pixbuf_get_from_drawable( NULL, /* Allocate a new pixbuf */ gdk_get_default_root_window(), /* The drawable */ NULL, /* Its colormap */ 0, 0, 0, 0, /* Coordinates */ gdk_screen_get_width(screen), /* Width */ gdk_screen_get_height(screen)); /* Height */ #endif /* Make the background darker. */ if (pixbuf != NULL) { unsigned char * pixels = gdk_pixbuf_get_pixels(pixbuf); int width = gdk_pixbuf_get_width(pixbuf); int height = gdk_pixbuf_get_height(pixbuf); int pixel_stride = ((gdk_pixbuf_get_has_alpha(pixbuf)) ? 4 : 3); int row_stride = gdk_pixbuf_get_rowstride(pixbuf); int y; for (y = 0; y < height; y += 1) { unsigned char * p = pixels; int x; for (x = 0; x < width; x += 1) { p[0] = p[0] / 2; p[1] = p[1] / 2; p[2] = p[2] / 2; p += pixel_stride; } pixels += row_stride; } } return pixbuf; }
/** * Transfers image data from a GdkWindow and converts it to an RGB(A) * representation inside a GdkPixbuf. In other words, copies image data * from a server-side drawable to a client-side RGB(A) buffer. * This allows to efficiently read individual pixels on the client side. * * Create a new GdkPixbuf containing a copy of src scaled to dest_width x dest_height . * Leaves src unaffected. interp_type should be GDK_INTERP_NEAREST * if you want maximum speed (but when scaling down GDK_INTERP_NEAREST is usually unusably ugly). * The default interp_type should be GDK_INTERP_BILINEAR which offers reasonable quality and speed. * * @param DockItem* item * @return GdkPixbuf* * */ GdkPixbuf* Preview::getScaledPixbuf(DockItem* item) { GdkWindow *wm_window = gdk_x11_window_foreign_new_for_display( gdk_display_get_default(), item->m_xid); GdkRectangle boundingbox; gdk_window_get_frame_extents(wm_window, &boundingbox); GdkPixbuf *pb = gdk_pixbuf_get_from_window(wm_window, 0, 0, boundingbox.width - 10, boundingbox.height - 30); if (pb == NULL) { return nullptr; } int height = m_previewHeight - DEF_PREVIEW_SCALE_HEIGHT_OFFSET; int width = m_previewWidth - DEF_PREVIEW_SCALE_WIDTH_OFFSET; int scale_heght = height; int windowheight = gdk_window_get_height(wm_window); int heightHalf = (height / 2); if (windowheight < 300) scale_heght = heightHalf; if (windowheight < 200) scale_heght = heightHalf - 20; if (windowheight < 100) scale_heght = heightHalf - 40; if (windowheight < 50) scale_heght = heightHalf - 60; if (scale_heght < 10) scale_heght = height; // offers reasonable quality and speed. GdkPixbuf* scaledpb = gdk_pixbuf_scale_simple(pb, width, scale_heght, GDK_INTERP_BILINEAR); g_object_unref(pb); return scaledpb; }
int main(int argc, char** argv) { gdk_init(&argc, &argv); #if defined(HAVE_LIBXSS) && defined(MOZ_WIDGET_GTK) int event_base, error_base; Bool have_xscreensaver = XScreenSaverQueryExtension(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), &event_base, &error_base); if (!have_xscreensaver) { fprintf(stderr, "No XScreenSaver extension on display\n"); } else { XScreenSaverInfo* info = XScreenSaverAllocInfo(); if (!info) { fprintf(stderr, "%s: Out of memory\n", argv[0]); return 1; } XScreenSaverQueryInfo(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), GDK_ROOT_WINDOW(), info); const char* state; const char* til_or_since = nullptr; switch (info->state) { case ScreenSaverOff: state = "Off"; til_or_since = "XScreenSaver will activate after another %lu seconds idle time\n"; break; case ScreenSaverOn: state = "On"; if (info->til_or_since) { til_or_since = "XScreenSaver idle timer activated %lu seconds ago\n"; } else { til_or_since = "XScreenSaver idle activation is disabled\n"; } break; case ScreenSaverDisabled: state = "Disabled"; break; default: state = "unknown"; } const char* kind; switch (info->kind) { case ScreenSaverBlanked: kind = "Blanked"; break; case ScreenSaverInternal: state = "Internal"; break; case ScreenSaverExternal: state = "External"; break; default: state = "unknown"; } fprintf(stderr, "XScreenSaver state: %s\n", state); if (til_or_since) { fprintf(stderr, "XScreenSaver kind: %s\n", kind); fprintf(stderr, til_or_since, info->til_or_since / 1000); } fprintf(stderr, "User input has been idle for %lu seconds\n", info->idle / 1000); XFree(info); } #endif GdkPixbuf* screenshot = nullptr; GdkWindow* window = gdk_get_default_root_window(); #if (MOZ_WIDGET_GTK == 2) screenshot = gdk_pixbuf_get_from_drawable(nullptr, window, nullptr, 0, 0, 0, 0, gdk_screen_width(), gdk_screen_height()); #else screenshot = gdk_pixbuf_get_from_window(window, 0, 0, gdk_window_get_width(window), gdk_window_get_height(window)); #endif if (!screenshot) { fprintf(stderr, "%s: failed to create screenshot GdkPixbuf\n", argv[0]); return 1; } GError* error = nullptr; if (argc > 1) { gdk_pixbuf_save(screenshot, argv[1], "png", &error, nullptr); } else { gdk_pixbuf_save_to_callback(screenshot, save_to_stdout, nullptr, "png", &error, nullptr); } if (error) { fprintf(stderr, "%s: failed to write screenshot as png: %s\n", argv[0], error->message); return error->code; } return 0; }
static GdkPixbuf * screenshot_fallback_get_pixbuf (GdkRectangle *rectangle) { GdkWindow *root, *wm_window = NULL; GdkPixbuf *screenshot; GdkRectangle real_coords, screenshot_coords; Window wm; GtkBorder frame_offset = { 0, 0, 0, 0 }; GdkWindow *window; window = screenshot_fallback_find_current_window (); screenshot_fallback_get_window_rect_coords (window, screenshot_config->include_border, &real_coords, &screenshot_coords); wm = find_wm_window (window); if (wm != None) { GdkRectangle wm_real_coords; wm_window = gdk_x11_window_foreign_new_for_display (gdk_window_get_display (window), wm); screenshot_fallback_get_window_rect_coords (wm_window, FALSE, &wm_real_coords, NULL); frame_offset.left = (gdouble) (real_coords.x - wm_real_coords.x); frame_offset.top = (gdouble) (real_coords.y - wm_real_coords.y); frame_offset.right = (gdouble) (wm_real_coords.width - real_coords.width - frame_offset.left); frame_offset.bottom = (gdouble) (wm_real_coords.height - real_coords.height - frame_offset.top); } if (rectangle) { screenshot_coords.x = rectangle->x - screenshot_coords.x; screenshot_coords.y = rectangle->y - screenshot_coords.y; screenshot_coords.width = rectangle->width; screenshot_coords.height = rectangle->height; } root = gdk_get_default_root_window (); screenshot = gdk_pixbuf_get_from_window (root, screenshot_coords.x, screenshot_coords.y, screenshot_coords.width, screenshot_coords.height); if (!screenshot_config->take_window_shot && !screenshot_config->take_area_shot) mask_monitors (screenshot, root); #ifdef HAVE_X11_EXTENSIONS_SHAPE_H if (screenshot_config->include_border && (wm != None)) { XRectangle *rectangles; GdkPixbuf *tmp; int rectangle_count, rectangle_order, i; /* we must use XShape to avoid showing what's under the rounder corners * of the WM decoration. */ rectangles = XShapeGetRectangles (GDK_DISPLAY_XDISPLAY (gdk_display_get_default()), wm, ShapeBounding, &rectangle_count, &rectangle_order); if (rectangles && rectangle_count > 0) { gboolean has_alpha = gdk_pixbuf_get_has_alpha (screenshot); tmp = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, screenshot_coords.width, screenshot_coords.height); gdk_pixbuf_fill (tmp, 0); for (i = 0; i < rectangle_count; i++) { gint rec_x, rec_y; gint rec_width, rec_height; gint y; /* If we're using invisible borders, the ShapeBounding might not * have the same size as the frame extents, as it would include the * areas for the invisible borders themselves. * In that case, trim every rectangle we get by the offset between the * WM window size and the frame extents. */ rec_x = rectangles[i].x; rec_y = rectangles[i].y; rec_width = rectangles[i].width - (frame_offset.left + frame_offset.right); rec_height = rectangles[i].height - (frame_offset.top + frame_offset.bottom); if (real_coords.x < 0) { rec_x += real_coords.x; rec_x = MAX(rec_x, 0); rec_width += real_coords.x; } if (real_coords.y < 0) { rec_y += real_coords.y; rec_y = MAX(rec_y, 0); rec_height += real_coords.y; } if (screenshot_coords.x + rec_x + rec_width > gdk_screen_width ()) rec_width = gdk_screen_width () - screenshot_coords.x - rec_x; if (screenshot_coords.y + rec_y + rec_height > gdk_screen_height ()) rec_height = gdk_screen_height () - screenshot_coords.y - rec_y; for (y = rec_y; y < rec_y + rec_height; y++) { guchar *src_pixels, *dest_pixels; gint x; src_pixels = gdk_pixbuf_get_pixels (screenshot) + y * gdk_pixbuf_get_rowstride(screenshot) + rec_x * (has_alpha ? 4 : 3); dest_pixels = gdk_pixbuf_get_pixels (tmp) + y * gdk_pixbuf_get_rowstride (tmp) + rec_x * 4; for (x = 0; x < rec_width; x++) { *dest_pixels++ = *src_pixels++; *dest_pixels++ = *src_pixels++; *dest_pixels++ = *src_pixels++; if (has_alpha) *dest_pixels++ = *src_pixels++; else *dest_pixels++ = 255; } } } g_object_unref (screenshot); screenshot = tmp; XFree (rectangles); } } #endif /* HAVE_X11_EXTENSIONS_SHAPE_H */ /* if we have a selected area, there were by definition no cursor in the * screenshot */ if (screenshot_config->include_pointer && !rectangle) { GdkCursor *cursor; GdkPixbuf *cursor_pixbuf; cursor = gdk_cursor_new_for_display (gdk_display_get_default (), GDK_LEFT_PTR); cursor_pixbuf = gdk_cursor_get_image (cursor); if (cursor_pixbuf != NULL) { GdkDeviceManager *manager; GdkDevice *device; GdkRectangle rect; gint cx, cy, xhot, yhot; manager = gdk_display_get_device_manager (gdk_display_get_default ()); device = gdk_device_manager_get_client_pointer (manager); if (wm_window != NULL) gdk_window_get_device_position (wm_window, device, &cx, &cy, NULL); else gdk_window_get_device_position (window, device, &cx, &cy, NULL); sscanf (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"), "%d", &xhot); sscanf (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"), "%d", &yhot); /* in rect we have the cursor window coordinates */ rect.x = cx + real_coords.x; rect.y = cy + real_coords.y; rect.width = gdk_pixbuf_get_width (cursor_pixbuf); rect.height = gdk_pixbuf_get_height (cursor_pixbuf); /* see if the pointer is inside the window */ if (gdk_rectangle_intersect (&real_coords, &rect, &rect)) { gint cursor_x, cursor_y; cursor_x = cx - xhot - frame_offset.left; cursor_y = cy - yhot - frame_offset.top; gdk_pixbuf_composite (cursor_pixbuf, screenshot, cursor_x, cursor_y, rect.width, rect.height, cursor_x, cursor_y, 1.0, 1.0, GDK_INTERP_BILINEAR, 255); } g_object_unref (cursor_pixbuf); g_object_unref (cursor); } } screenshot_fallback_fire_flash (window, rectangle); return screenshot; }