Skip to content

Commit 38f0d34

Browse files
authored
Replace Gdk.pixbuf_from_surface (#2732)
Gdk.pixbuf_from_surface is provided by Gdk4 and it's deprecated there
1 parent ba69d2f commit 38f0d34

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/ScreenshotManager.vala

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ public class Gala.ScreenshotManager : Object {
506506
}
507507

508508
try {
509-
var screenshot = Gdk.pixbuf_get_from_surface (image, 0, 0, image.get_width (), image.get_height ());
509+
var screenshot = pixbuf_from_surface (image);
510510
var file = File.new_for_path (used_filename);
511511
FileIOStream stream;
512512
if (file.query_exists ()) {
@@ -525,7 +525,7 @@ public class Gala.ScreenshotManager : Object {
525525
private bool save_image_to_clipboard (Cairo.ImageSurface image, string filename, out string used_filename) {
526526
used_filename = filename;
527527

528-
var screenshot = Gdk.pixbuf_get_from_surface (image, 0, 0, image.get_width (), image.get_height ());
528+
var screenshot = pixbuf_from_surface (image);
529529
if (screenshot == null) {
530530
warning ("Could not save screenshot to clipboard: null pixbuf");
531531
return false;
@@ -634,4 +634,37 @@ public class Gala.ScreenshotManager : Object {
634634
wm.stage.queue_redraw ();
635635
yield;
636636
}
637+
638+
private static Gdk.Pixbuf pixbuf_from_surface (Cairo.ImageSurface image) {
639+
var width = image.get_width ();
640+
var height = image.get_height ();
641+
642+
var pixbuf = new Gdk.Pixbuf (RGB, true, 8, width, height);
643+
644+
unowned var src_data = image.get_data ();
645+
unowned var dest_data = pixbuf.get_pixels ();
646+
647+
for (var y = 0; y < height; y++) {
648+
var row_start = y * width * 4;
649+
650+
for (var x = 0; x < width; x++) {
651+
var pixel_start = row_start + x * 4;
652+
var alpha = src_data[pixel_start + 3];
653+
654+
if (alpha == 0) {
655+
dest_data[pixel_start + 0] = 0;
656+
dest_data[pixel_start + 1] = 0;
657+
dest_data[pixel_start + 2] = 0;
658+
} else {
659+
dest_data[pixel_start + 0] = (src_data[pixel_start + 2] * 255 + alpha / 2) / alpha;
660+
dest_data[pixel_start + 1] = (src_data[pixel_start + 1] * 255 + alpha / 2) / alpha;
661+
dest_data[pixel_start + 2] = (src_data[pixel_start + 0] * 255 + alpha / 2) / alpha;
662+
}
663+
664+
dest_data[pixel_start + 3] = alpha;
665+
}
666+
}
667+
668+
return pixbuf;
669+
}
637670
}

0 commit comments

Comments
 (0)