github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/textinput/util.go (about) 1 package main 2 3 import ( 4 "image" 5 "image/draw" 6 "os" 7 8 _ "image/jpeg" 9 _ "image/png" 10 ) 11 12 func loadImage(filepath string) (*image.RGBA, error) { 13 file, err := os.Open(filepath) 14 if err != nil { 15 return nil, err 16 } 17 defer file.Close() 18 19 m, _, err := image.Decode(file) 20 if err != nil { 21 return nil, err 22 } 23 24 if rgba, ok := m.(*image.RGBA); ok { 25 return rgba, nil 26 } 27 28 rgba := image.NewRGBA(m.Bounds()) 29 if rgba.Stride != rgba.Rect.Size().X*4 { 30 panic("unsupported stride") 31 } 32 draw.Draw(rgba, rgba.Bounds(), m, image.Point{0, 0}, draw.Src) 33 34 return rgba, nil 35 }