Should get-pixels
always return an int array like its description says?
Creating a new image and getting its pixels works as I'd expect, i.e. the number of pixels equals the width times the height of the image.
testing.core=> (def block (new-image 10 10))
#'testing.core/block
testing.core=> (def blockpixels (get-pixels block))
#'testing.core/blockpixels
testing.core=> (alength blockpixels)
100
So far, so good. However, when I read a PNG image, things are not that straightforward. For this test I created a 10 px * 10 px image that consists of red pixels only.
testing.core=> (def redbox (load-image-resource "../resources/red.png"))
#'testing.core/redbox
testing.core=> (.getWidth redbox)
10
testing.core=> (.getHeight redbox)
10
testing.core=> (def redpixels (get-pixels redbox))
#'testing.core/redpixels
testing.core=> (alength redpixels)
300
testing.core=> (vec redpixels)
[-1 0 0 -1 0 0 ... -1 0 0]
It seems get-pixels
returns a byte array that has separate RGB values for each pixel. And them being bytes, the value 255 actually becomes -1.
As the function uses Java's getDataElements
from the Raster
class, I checked its docs:
Returns the pixel data for the specified rectangle of pixels in a primitive array of type TransferType. For image data supported by the Java 2D API, this will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE.
But I guess that's exactly why there's the ^ints
in get-pixels
. It just doesn't seem to do the trick. Or am I possibly doing something stupid here?
(I'm using Clojure 1.6.0 and Imagez 0.4.1)