Transparancy check

This commit is contained in:
Your Name 2022-11-20 16:13:40 -05:00
parent 92f16b2d37
commit d24d8fb195
1 changed files with 29 additions and 0 deletions

29
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
@ -112,6 +113,28 @@ func main() {
}
// Check image for transparency
func Opaque(im image.Image) bool {
// Check if image has Opaque() method:
if oim, ok := im.(interface {
Opaque() bool
}); ok {
return oim.Opaque() // It does, call it and return its result!
}
// No Opaque() method, we need to loop through all pixels and check manually:
rect := im.Bounds()
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
if _, _, _, a := im.At(x, y).RGBA(); a != 0xffff {
return false // Found a non-opaque pixel: image is non-opaque
}
}
}
return true // All pixels are opaque, so is the image
}
// doSingleImage processes a single file and updateds the run stats as it goes
func doSingleImage(f string, c config, s stats) stats {
st, err := os.Stat(f)
@ -143,6 +166,12 @@ func doSingleImage(f string, c config, s stats) stats {
}
}
in.Close()
if !Opaque(orig) {
if !c.Quiet {
fmt.Printf("Skipping %s as it has transparancy", f)
}
return s
}
// Check bounds and resize if necessary
ores:=orig.Bounds()
if (c.MaxWidth !=0 && ores.Dx() > int(c.MaxWidth) ) || (c.MaxHeight!=0 && ores.Dy()>int(c.MaxHeight)) {