78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
// alphaprobe:打印一张图若干采样点的 RGBA,并统计 alpha 分布。
|
|
//
|
|
// 用来诊断「缩放后图标看起来全透明」这类问题 ——
|
|
// 光看缩略图分不清是图像真透明,还是预览器把透明当白底。
|
|
//
|
|
// 用法:go run ./tools/alphaprobe <img.png> [更多.png...]
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/png"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
fmt.Fprintln(os.Stderr, "用法: go run ./tools/alphaprobe <img>...")
|
|
os.Exit(1)
|
|
}
|
|
for _, p := range os.Args[1:] {
|
|
probe(p)
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
func probe(path string) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
fmt.Printf("%s: %v\n", path, err)
|
|
return
|
|
}
|
|
defer f.Close()
|
|
|
|
im, err := png.Decode(f)
|
|
if err != nil {
|
|
fmt.Printf("%s: 解不开: %v\n", path, err)
|
|
return
|
|
}
|
|
b := im.Bounds()
|
|
fmt.Printf("%s bounds=%v\n", path, b)
|
|
|
|
pts := []struct {
|
|
name string
|
|
x, y int
|
|
}{
|
|
{"左上角", b.Min.X, b.Min.Y},
|
|
{"中心", b.Min.X + b.Dx()/2, b.Min.Y + b.Dy()/2},
|
|
{"上边中", b.Min.X + b.Dx()/2, b.Min.Y},
|
|
{"左边中", b.Min.X, b.Min.Y + b.Dy()/2},
|
|
{"右下角", b.Max.X - 1, b.Max.Y - 1},
|
|
}
|
|
for _, p := range pts {
|
|
r, g, bl, a := im.At(p.x, p.y).RGBA()
|
|
fmt.Printf(" %-8s (%3d,%3d) R=%3d G=%3d B=%3d A=%3d\n",
|
|
p.name, p.x, p.y, r>>8, g>>8, bl>>8, a>>8)
|
|
}
|
|
|
|
var opaque, semi, clear int
|
|
for y := b.Min.Y; y < b.Max.Y; y++ {
|
|
for x := b.Min.X; x < b.Max.X; x++ {
|
|
if _, _, _, a := im.At(x, y).RGBA(); a>>8 > 240 {
|
|
opaque++
|
|
} else if a>>8 > 10 {
|
|
semi++
|
|
} else {
|
|
clear++
|
|
}
|
|
}
|
|
}
|
|
total := b.Dx() * b.Dy()
|
|
fmt.Printf(" alpha: 不透明 %d (%.1f%%) · 半透明 %d (%.1f%%) · 透明 %d (%.1f%%)\n",
|
|
opaque, 100*float64(opaque)/float64(total),
|
|
semi, 100*float64(semi)/float64(total),
|
|
clear, 100*float64(clear)/float64(total))
|
|
_ = image.Rect
|
|
}
|