vips-ffm icon indicating copy to clipboard operation
vips-ffm copied to clipboard

Saving image to format determined by file extension

Open JohannesBeranek opened this issue 1 year ago • 4 comments

vips-ffm provides VImage::writeToTarget which, as far as I understood from libvips documentation itself, should allow to save an image without knowing what the target format will be, and have the library automatically determine it based on the format option (which should actually be the filename suffix, so include the leading ".", according to my testing).

How would one get a byte array out of a VTarget?

vTarget.getUnsafeStructAddress().asByteBuffer()

does not seem to give a byte buffer of the image or it's missing a NUL terminator or has some other issue.

JohannesBeranek avatar Sep 27 '24 15:09 JohannesBeranek

Also relevant: vips_target_steal ()

The data is NOT automatically null-terminated. [vips_target_putc()](https://www.libvips.org/API/current/VipsTargetCustom.html#vips-target-putc) a '\0' before calling this to get a null-terminated string.

You can't call this after [vips_target_end()](https://www.libvips.org/API/current/VipsTargetCustom.html#vips-target-end), since that moves the data to a blob, and we can't steal from that in case the pointer has been be shared.

The second paragraph implies to me that we should have a VTarget.end() method or the like to get a VBlob ?

JohannesBeranek avatar Sep 27 '24 16:09 JohannesBeranek

Had a look at how other bindings do this - they seem to just expose the underlying blob for access, with a warning about only using it with memory-backed targets, so I've done the same and added VTarget.getBlob to get the VBlob in 0.5.12 🙂

Sample for that: https://github.com/lopcode/vips-ffm/blob/main/sample/src/main/kotlin/vipsffm/VTargetMemorySample.kt#L10

lopcode avatar Sep 27 '24 17:09 lopcode

Seems to work so far. Let me do a little bit more testing, then I'll be happy to close this if that's alright with you?

JohannesBeranek avatar Oct 01 '24 12:10 JohannesBeranek

Yep sounds good to me 👍

lopcode avatar Oct 01 '24 12:10 lopcode

Checking in to see if I can close this issue now, @JohannesBeranek?

lopcode avatar Oct 24 '24 01:10 lopcode

Sorry for the late response, but looks good now, thanks!

JohannesBeranek avatar Nov 04 '24 16:11 JohannesBeranek