Question

I have a byteArray that represents a PNG, but I want to convert this to webP. I have the following code. I make use of com.sksamuel.scrimage.Image in my code and load the System library webp_jni.

val image = Image(content) //content of PNG of type Array[Byte]
extension match {
    case "webp" => Ok(
        libwebp.WebPEncodeRGB(
            image.write,
            image.width,
            image.height,
            image.width * 3,
            80F)
        ).as("image/webp")
    case _ => Ok(image.write).as("image/png")
}

If I request the png image it all works fine, but when requesting the webP image, then I get the following.

#  SIGSEGV (0xb) at pc=0x00007fc008287377, pid=14883, tid=140461610465024
#
# JRE version: Java(TM) SE Runtime Environment (8.0_05-b13) (build 1.8.0_05-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.5-b02 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libwebp.so.5+0x33377]  WebPPictureAlloc+0x407

I tried other values for the stride input image.width, image.width rounded to multiples of 4, image.width rounded to multiples of 3. In this case it doesn't crash, but the image is very noisy and looks nothing like the original.

Was it helpful?

Solution

I switched my project to use the webp-imageio library and was able to implement it in the following way.

val image = Image(content) //content of PNG of type Array[Byte]
extension match {
    case "webp" => {
        val out = new ByteArrayOutputStream()
        ImageIO.write(resizedImage.awt, "webp", out)
        Ok(out.toByteArray).as("image/webp")
    case _ => Ok(image.write).as("image/png")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top