https://kotlinlang.org logo
Title
p

Pedro Pereira

04/15/2023, 2:33 AM
Is there any way to get a subimage (indicating the position and dimension) from a png file, to display in a Composable Image?
d

Dima Avdeev

04/15/2023, 7:30 AM
Yes:
Image(
    painter = BitmapPainter(
        image = TODO("your image"),
        srcOffset = IntOffset(100, 100),
        srcSize = IntSize(200, 200)
    ),
    contentDescription = null
)
In this case full png file will be loaded
p

Pedro Pereira

04/15/2023, 10:02 AM
OK. Thanks. And can we only load the part of the image?
d

Dima Avdeev

04/15/2023, 10:05 AM
I think - it depends on image format. But I don't know how to do it with png. It is not Compose related issues. You need to read part of the file on Kotlin JVM level (or event lower with native calls). Convert part of the file to ImageBitmap. It may be hard. Or maybe it is not possible with png.
p

Pedro Pereira

04/15/2023, 10:28 AM
Thanks
e

ephemient

04/15/2023, 1:53 PM
I don't believe it is possible to decode only part of a PNG. the format puts the compressed pixels in a single compressed stream (except in the rare case of interlacing, and that doesn't help you), and the filtering means each pixel depends on the pixels that came before it
doable for JPEG though
p

Pedro Pereira

04/15/2023, 6:17 PM
OK. It makes sense. Thanks.
I'm developing a game and I have a png file with the sprites I want to display. It should be possible to load the entire image into an ImageBitmap and then extract each sprite from the image to display. Will I need to use specific JVM operations to get each part of the ImageBitmap? Or are there desktop compose functions to do this?
s

saket

04/15/2023, 7:30 PM
I don't believe it is possible to decode only part of a PNG
are you sure about this? android's
BitmapRegionDecoder
is capable of decoding regions of pngs.
e

ephemient

04/15/2023, 7:47 PM
that works by decoding the whole bitmap
s

saket

04/15/2023, 7:49 PM
do you mean by loading the whole bitmap in memory?
yes
s

saket

04/15/2023, 7:51 PM
hmmm I don't think I understand. wouldn't loading whole images in memory trigger OOMs for large images?
e

ephemient

04/15/2023, 7:51 PM
yes
JPEG is block-based so it is possible to decode only specific blocks, but PNG is not
s

saket

04/15/2023, 7:53 PM
I think I'm missing something here because that sounds incorrect considering I am able to load regions of a gigantic PNG using android's
BitmapRegionDecoder
without triggering OOMs
I'm writing a compose-first library for displaying zoomable images so I'd like to learn more about this :blob-smile:
e

ephemient

04/15/2023, 7:54 PM
try loading the whole bitmap and see if you OOM
s

saket

04/15/2023, 7:56 PM
no, I meant I'm able to load certain regions of PNGs without decoding the entire image. I was responding to this message:
I don't believe it is possible to decode only part of a PNG
e

ephemient

04/15/2023, 7:56 PM
yes. the Android PNG decoder decodes the entire image then returns a subset of it
you can follow the code yourself
s

saket

04/15/2023, 7:57 PM
does the term "decode" here mean something other than loading the bitmap in memory?
e

ephemient

04/15/2023, 7:57 PM
no
although it's all in native (off-heap) memory
s

saket

04/15/2023, 7:58 PM
I see. So that doesn't count against an app's memory?
e

ephemient

04/15/2023, 7:59 PM
it does count against an app's memory usage, but is invisible to GC