I need to get a pixel array in the form of []byte
to be passed to the texImage2D
method of a Contex from the /mobile/gl package.
It needs a pixel array where rgba values of each pixel is appended in the order of pixels left to right, top to bottom. Currently I have an image loaded from a file.
a, err := asset.Open("key.jpeg")
if err != nil {
log.Fatal(err)
}
defer a.Close()
img, _, err := image.Decode(a)
if err != nil {
log.Fatal(err)
}
I am looking for something like img.Pixels()
In my test,
Pix
method shows ~4 times faster thanAt
, but still takes too long...Here is my test script, not sure about the
height
/width
order, but this works for me:Run
This is what I ended up doing. I am using image/draw package's Draw function to refill an
image.RGBA
instanceNow
rgba.Pix
contains the array I want and can be used in theTexImage2D
method.Alternately
Image instances contains an
At
method that returns a Color. So it is possible to loop through each pixel and and collect colors. But converting returned rgba values from theColor
might be complex. Quoting documentation:You can simply use
img.At(x, y).RGBA()
to get the RBGA values for a pixel, you just need to divide them by 257 to get the 8 bit representation. I'd recommend building your own bi-dimensional array of pixels. Here's a possible implementation, modify it as needed: