image: Do not guess bg color for opaque images

In theory this feature made sense to see a black square
but in practice it seems mostly confusing.

Closes #186

Part-of: <https://gitlab.gnome.org/GNOME/Incubator/loupe/-/merge_requests/235>
This commit is contained in:
Sophie Herold 2023-06-20 15:34:25 +02:00 • committed by Marge Bot
parent c1422cf728
commit 0c2bdbd736

View file

@ -1798,6 +1798,7 @@ impl LpImage {
let default_color = Self::default_background_color(); let default_color = Self::default_background_color();
gio::spawn_blocking(move || { gio::spawn_blocking(move || {
let mut has_transparency = false;
let mut bytes_iter = bytes.iter(); let mut bytes_iter = bytes.iter();
// Number of transparent pixels // Number of transparent pixels
let mut completely_transparent = 0; let mut completely_transparent = 0;
@ -1810,7 +1811,10 @@ impl LpImage {
let Some(b) = bytes_iter.next() else { break 'img; }; let Some(b) = bytes_iter.next() else { break 'img; };
let Some(a) = bytes_iter.next() else { break 'img; }; let Some(a) = bytes_iter.next() else { break 'img; };
// 70% transparency if *a < 255 {
has_transparency = true;
}
if *a == 0 { if *a == 0 {
completely_transparent += 1; completely_transparent += 1;
} else { } else {
@ -1835,6 +1839,11 @@ impl LpImage {
} }
} }
if !has_transparency {
log::debug!("This image does not have transparency");
return Some(default_color);
}
let n_pixels = texture.width() * texture.height(); let n_pixels = texture.width() * texture.height();
let part_bad_contrast = if completely_transparent < n_pixels { let part_bad_contrast = if completely_transparent < n_pixels {