go - Using the Set() method of an image.Image or *image.RGBA -
i doing practice go image package free time summer.
package main import ( "os" "image" "image/png" "image/color" "log" "fmt" "reflect" ) func main(){ file , err := os.openfile("c:/sources/go3x3.png", os.o_rdwr, os.filemode(0777)) if err != nil { log.fatal(err) } img , err := png.decode(file) if err != nil { log.fatal(err) } img.at(0,0).rgba() fmt.println("type:", reflect.typeof(img)) m := image.newrgba(image.rect(0, 0, 640, 480)) fmt.println("type:", reflect.typeof(m)) m.set(5, 5, color.rgba{255, 0, 0, 255}) img.set(0, 0, color.rgba{136, 0, 21, 255}) }
the problem here when run img.set
commented out result
type: *image.rgba type: *image.rgba
but when it's uncommented error saying
img.set undefined (type image.image has no field or method set)
i'm assuming i'm using reflect wrong, i'm still grasping whole interface , type definitions in go.
to expand on anonymous' answer:
png.decode
may create 1 of several different underlying image types (*image.gray
, *image.rgba
, *image.paletted
, *image.nrgba
, etc). returns whatever image created image.image
interface provides read access data.
however, (most?) of actual image types returns implement set
method simple write access. way can safely test , use method via existing draw.image
interface image/draw
package. it's this:
// image/draw: // image image.image set method change single pixel. type image interface { image.image set(x, y int, c color.color) }
so like:
func drawablepngimage(r io.reader) (draw.image, error) { img, err := png.decode(r) if err != nil { return nil, err } dimg, ok := img.(draw.image) if !ok { return nil, fmt.errorf("%t not drawable image type", img) } return dimg, nil }
playground (shows example calling image.image
methods set
).
Comments
Post a Comment