libheif icon indicating copy to clipboard operation
libheif copied to clipboard

How to encode multiple images using go package

Open ggoy96 opened this issue 3 years ago • 4 comments

There is very less documentation available for the go package. It might be sufficient for seasoned programmers but for someone new like me it's difficult to navigate. I am trying to encode multiple images but couldn't find a way to do that. In the readme of the repository, it's written that you can encode multiple images but I am not sure if its available for Golang too or not.

So far, I am only able to encode a single image using EncodeFromImage function. I will appreciate any help towards the right direction.

ggoy96 avatar Jul 02 '21 12:07 ggoy96

If you look at EncodeFromImage, it's just a short wrapper around heif_context_encode_image. EncodeFromImage will always create a new context, but it would be quite simple to extend the function to only create a new context if an existing one isn't passed in. Every time you hit heif_context_encode_image with the same context, it will add the additional image into the encoded file, with the first one being the primary. As the readme says, the Go API is in flux and open to contributions.

silverbacknet avatar Jul 03 '21 11:07 silverbacknet

Thanks @silverbacknet for the explanation. That's what I thought initially that EncodeFromImage should have a argument that can accept the context but there is none. The signature looks like

func EncodeFromImage(img image.Image, compression Compression, quality int, lossless LosslessMode, logging LoggingLevel) (*Context, error)

It only seems to accept an Image and there is no way as per my understanding to pass a context. Maybe I am missing something here.

ggoy96 avatar Jul 04 '21 16:07 ggoy96

Right, and since Go cannot do overloading, that means you'd have to change the API or add a new API. I don't know what @farindk prefers. The signature would then be something like,

func EncodeFromImageAdditional(img image.Image, compression Compression, quality int, lossless LosslessMode, logging LoggingLevel, ctx *Context) (error)

Go's not a language I really know, but I'm installing a toolchain just to sanity check that would work OK with a few tweaks.

silverbacknet avatar Jul 04 '21 22:07 silverbacknet

Right, I guess I have to pick up C at last 😄 and try to create a wrapper that can accept a Context. Thanks!

ggoy96 avatar Jul 05 '21 14:07 ggoy96