Is there any way to get a subimage (indicating the...
# compose-desktop
p
Is there any way to get a subimage (indicating the position and dimension) from a png file, to display in a Composable Image?
d
Yes:
Copy code
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
OK. Thanks. And can we only load the part of the image?
d
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
Thanks
e
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
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
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
that works by decoding the whole bitmap
s
do you mean by loading the whole bitmap in memory?
yes
s
hmmm I don't think I understand. wouldn't loading whole images in memory trigger OOMs for large images?
e
yes
JPEG is block-based so it is possible to decode only specific blocks, but PNG is not
s
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
try loading the whole bitmap and see if you OOM
s
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
yes. the Android PNG decoder decodes the entire image then returns a subset of it
you can follow the code yourself
s
does the term "decode" here mean something other than loading the bitmap in memory?
e
no
although it's all in native (off-heap) memory
s
I see. So that doesn't count against an app's memory?
e
it does count against an app's memory usage, but is invisible to GC