github.com/aclements/go-misc@v0.0.0-20240129233631-2f6ede80790c/srgb/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "image" 7 "image/png" 8 "log" 9 "os" 10 11 "golang.org/x/image/draw" 12 ) 13 14 func main() { 15 var err error 16 17 flag.Usage = func() { 18 fmt.Fprintf(os.Stderr, "Usage: %s input output\n", os.Args[0]) 19 } 20 flag.Parse() 21 if flag.NArg() != 2 { 22 flag.Usage() 23 os.Exit(2) 24 } 25 26 // Read input file. 27 f, err := os.Open(os.Args[1]) 28 if err != nil { 29 log.Fatal(err) 30 } 31 defer f.Close() 32 src, _, err := image.Decode(f) 33 if err != nil { 34 log.Fatal(err) 35 } 36 37 // Scale down by a factor of 2. 38 sb := src.Bounds() 39 dst := image.NewRGBA(image.Rect(0, 0, sb.Dx()/2, sb.Dy()/2)) 40 draw.BiLinear.Scale(dst, dst.Bounds(), src, sb, draw.Over, nil) 41 42 // Write output file. 43 if f, err = os.Create(os.Args[2]); err != nil { 44 log.Fatal(err) 45 } 46 defer f.Close() 47 if err := png.Encode(f, dst); err != nil { 48 log.Fatal(err) 49 } 50 }