https://kotlinlang.org logo
k

Kirill Grouchnikov

11/16/2020, 9:59 PM
Are there APIs for image convolutions in Compose (on
ImageBitmap
)? In general, there doesn't seem to be a way, maybe by design, to get access to the underlying pixel information for direct manipulation.
AndroidImageAsset
and
DesktopImageAsset
go through a somewhat of a contortion to expose the underlying pixel information to the
ImageBitmap
, but I don't see any way to get those pixels afterwards.
c

cb

11/16/2020, 10:09 PM
There's the new
toPixelMap()
which gives you read access to the pixels. You say manipulation, as in to mutate the pixel array values?
k

Kirill Grouchnikov

11/16/2020, 10:28 PM
Yes. This is common for libraries that provide dynamically generated image effects where you don't necessarily have an asset to begin with. One use case is to dynamically generate a texture (noise, weave, fabric, anything really), then apply a set of filters / convolutions directly to the int array, and then create an
ImageBitmap
out of the final buffer.
And image convolution is a common thing as well. On Android it can be done with RenderScript (see https://blog.stylingandroid.com/image-convolution-implementation/) but that wouldn't translate directly to Compose. In general, it's about dropping down to the underlying buffer of pixels for reading, transforming and creating a new buffer, and then creating a new image out of it. Or alternatively, rewriting the pixels in the destination so that you don't create a new image object every time.
n

Nader Jawad

11/17/2020, 12:13 AM
Right now there is no support for image convolutions in compose. Nowadays if folks want to manipulate textures they usually leverage opengl. But as @cb mentioned if you want to query pixel formation you can use the
toPixelMap
api on ImageBitmap or provide your own array to store pixel information using
readPixels
k

Kirill Grouchnikov

11/17/2020, 12:43 AM
Is it only on
ImageAsset
or also on
ImageBitmap
?
n

Nader Jawad

11/17/2020, 12:43 AM
ImageAsset
is renamed to
ImageBitmap
And there is a type alias for
ImageAsset
that points to
ImageBitmap
toPixelMap
is an extension method on
ImageBitmap
which would need an explicit import.
k

Kirill Grouchnikov

11/17/2020, 12:46 AM
Ah great, I missed the alias part. So I can use
readPixels
for two things. Get pixels from an existing image, and also do that same "remapping" trick as in
AndroidImageAsset
 and 
DesktopImageAsset
to map a dynamically created buffer of pixels to a compose image object that can be drawn on a canvas.
Well, that didn't work - see my latest post in this channel. I can create a custom implementation of
ImageAsset
, and then it's going to throw an
UnsupportedOperationException
while drawing it on a canvas
2 Views