Skip to content

Commit b6ac1cd

Browse files
committed
Replace Gdk.pixbuf_from_surface
1 parent c0ced5d commit b6ac1cd

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/ScreenshotManager.vala

Lines changed: 37 additions & 3 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;
@@ -629,7 +629,7 @@ public class Gala.ScreenshotManager : Object {
629629
cr.set_source_surface (cursor_image, coords.x - image_rect.x, coords.y - image_rect.y);
630630
cr.paint ();
631631

632-
return (Cairo.ImageSurface)cr.get_target ();
632+
return (Cairo.ImageSurface) cr.get_target ();
633633
}
634634

635635
private async void wait_stage_repaint () {
@@ -642,4 +642,38 @@ public class Gala.ScreenshotManager : Object {
642642
wm.stage.queue_redraw ();
643643
yield;
644644
}
645+
646+
private static Gdk.Pixbuf pixbuf_from_surface (Cairo.ImageSurface image) {
647+
var width = image.get_width ();
648+
var height = image.get_height ();
649+
650+
var pixbuf = new Gdk.Pixbuf (RGB, true, 8, width, height);
651+
652+
unowned var src_data = image.get_data ();
653+
unowned var dest_data = pixbuf.get_pixels ();
654+
655+
for (var y = 0; y < height; y++) {
656+
var row_start = y * width * 4;
657+
658+
for (var x = 0; x < width; x++) {
659+
var pixel_start = row_start + x * 4;
660+
var alpha = src_data[pixel_start + 3];
661+
662+
if (alpha == 0) {
663+
dest_data[pixel_start + 0] = 0;
664+
dest_data[pixel_start + 1] = 0;
665+
dest_data[pixel_start + 2] = 0;
666+
} else {
667+
dest_data[pixel_start + 0] = (src_data[pixel_start + 2] * 255 + alpha / 2) / alpha;
668+
dest_data[pixel_start + 1] = (src_data[pixel_start + 1] * 255 + alpha / 2) / alpha;
669+
dest_data[pixel_start + 2] = (src_data[pixel_start + 0] * 255 + alpha / 2) / alpha;
670+
}
671+
672+
dest_data[pixel_start + 3] = alpha;
673+
674+
}
675+
}
676+
677+
return pixbuf;
678+
}
645679
}

0 commit comments

Comments
 (0)