diff --git a/main.go b/main.go index ff7c731..ac43b0e 100644 --- a/main.go +++ b/main.go @@ -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)) {