Transparancy check
This commit is contained in:
parent
92f16b2d37
commit
d24d8fb195
29
main.go
29
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"image/png"
|
"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
|
// doSingleImage processes a single file and updateds the run stats as it goes
|
||||||
func doSingleImage(f string, c config, s stats) stats {
|
func doSingleImage(f string, c config, s stats) stats {
|
||||||
st, err := os.Stat(f)
|
st, err := os.Stat(f)
|
||||||
|
@ -143,6 +166,12 @@ func doSingleImage(f string, c config, s stats) stats {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
in.Close()
|
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
|
// Check bounds and resize if necessary
|
||||||
ores:=orig.Bounds()
|
ores:=orig.Bounds()
|
||||||
if (c.MaxWidth !=0 && ores.Dx() > int(c.MaxWidth) ) || (c.MaxHeight!=0 && ores.Dy()>int(c.MaxHeight)) {
|
if (c.MaxWidth !=0 && ores.Dx() > int(c.MaxWidth) ) || (c.MaxHeight!=0 && ores.Dy()>int(c.MaxHeight)) {
|
||||||
|
|
Loading…
Reference in New Issue