I think your ImageSetter is already in the standard library https://golang.org/pkg/image/draw/#Image It's a little bit subtle because different packages have the immutable Image and mutable Image
It might save you some work by using golang.org/x/image library which already has affine transforms. https://godoc.org/golang.org/x/image/draw#Kernel.Transform On 4 September 2017 at 08:54, Florian Florensen <f...@posteo.de> wrote: > Hello, > > I'd like to rotate, scale, ... images by moving pixels using affine > transformations. For example, the get a points x, y position after a > translating it by m, n pixels, you can just add m resp n to x and y: > > x' = x + m > y' = y + n > > > So the task is basically just to iterate over the images pixels. This was > done by directly accessing the Pix-slice > <https://golang.org/pkg/image/#Paletted>, but not every every in-memory > image has this field. > > The current solution uses reflection and is extracting the Set > function (and, as a nice side effect, a new image with the same type and > resolution as the input image): > > func getSet(im image.Image) (func(int, int, color.Color), image.Image, > error) { > set := func(x, y int, c color.Color) {} > switch tmp := im.(type) { > case *image.Alpha: > im = image.NewAlpha(tmp.Rect) > set = im.(*image.Alpha).Set > ... > default: > return nil, nil, fmt.Errorf("%T has no function Set", im) > } > return set, im, nil > } > > My favourited approach would be to use an interface which adds Set it to > image.Image. This works, but I obviously can't just throw an image.Image in. > > type ImageSetter interface { > image.Image > Set(int, int, color.Color) > } > > What would be a go-like solution for this task? > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.