[gnome-boxes] Ignore almost-black screenshots



commit bd3aa1427f2300d254ec71c8bdf7a38295e59169
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Aug 30 22:29:33 2012 +0200

    Ignore almost-black screenshots
    
    Black and almost black screenshots are almost always screensaver
    or lock screens. These just make the collection view icons
    be full of black screenshots after a while, which is not very
    useful. It is also easy to confuse with turned off boxes which
    we want to make black.
    
    To fix this we just ignore all screenshot that are too dark.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=683067

 src/machine.vala |   36 ++++++++++++++++++++++++++++++++++++
 1 files changed, 36 insertions(+), 0 deletions(-)
---
diff --git a/src/machine.vala b/src/machine.vala
index 31e6a38..6d465d8 100644
--- a/src/machine.vala
+++ b/src/machine.vala
@@ -249,6 +249,31 @@ private abstract class Boxes.Machine: Boxes.CollectionItem, Boxes.IPropertiesPro
         }
     }
 
+    /* Calculates the average energy intensity of a pixmap.
+     * Being a square of an 8bit value this is a 16bit value. */
+    private int pixbuf_energy (Gdk.Pixbuf pixbuf) {
+        unowned uint8[] pixels = pixbuf.get_pixels ();
+        int w = pixbuf.get_width ();
+        int h = pixbuf.get_height ();
+        int rowstride = pixbuf.get_rowstride ();
+        int n_channels = pixbuf.get_n_channels ();
+
+        int energy = 0;
+        int row_start = 0;
+        for (int y = 0; y < h; y++) {
+            int row_energy = 0;
+            int i = row_start;
+            for (int x = 0; x < w; x++) {
+                int avg = (pixels[i+0] + pixels[i+1] + pixels[i+2]) / 3;
+                row_energy += avg * avg;
+                i += n_channels;
+            }
+            energy += row_energy / w;
+            row_start += rowstride;
+        }
+        return energy / h;
+    }
+
     public void set_screenshot (Gdk.Pixbuf? large_screenshot, bool save) {
         if (large_screenshot != null) {
             var pw = large_screenshot.get_width ();
@@ -260,6 +285,17 @@ private abstract class Boxes.Machine: Boxes.CollectionItem, Boxes.IPropertiesPro
             var small_screenshot = new Gdk.Pixbuf (Gdk.Colorspace.RGB, large_screenshot.has_alpha, 8, w, h);
             large_screenshot.scale (small_screenshot, 0, 0, w, h, 0, 0, s, s, Gdk.InterpType.HYPER);
 
+            /* We don't accept black or almost-black screenshots as they are
+               generally just screensavers/lock screens, which are not very helpful,
+               and can easily be mistaken for turned off boxes.
+
+               The number 100 is somewhat arbitrary, picked to not allow the gnome 3
+               lock screen, nor a fullscreen white-on-black terminal with a single
+               shell prompt, but do allow the terminal with a few lines of text.
+            */
+            if (pixbuf_energy (small_screenshot) < 100)
+                return;
+
             orig_pixbuf = small_screenshot;
             pixbuf = draw_vm (small_screenshot, SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT);
             machine_actor.set_screenshot (large_screenshot); // high resolution



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]