sejda-pdf / webp-imageio

Java ImageIO WebP support
Apache License 2.0
198 stars 74 forks source link

java.lang.ArrayIndexOutOfBoundsException: 1 #12

Open Ali-RS opened 3 years ago

Ali-RS commented 3 years ago

Hi

When trying to convert an emissive map (glow map) from jpg to webp using ImageIO and this plugin, I am getting this error:


SEVERE: Uncaught exception thrown in Thread[main,5,main]
java.lang.ArrayIndexOutOfBoundsException: 1
    at com.luciad.imageio.webp.WebPWriter.extractComponentRGBByte(WebPWriter.java:216)
    at com.luciad.imageio.webp.WebPWriter.getRGB(WebPWriter.java:129)
    at com.luciad.imageio.webp.WebPWriter.encode(WebPWriter.java:99)
    at com.luciad.imageio.webp.WebPWriter.write(WebPWriter.java:72)
    at app.Main.convertTexturesToWebP(Main.java:706)

this is the texture:

Seeker_emissive

When I am converting it to webp with Gimp it converts Ok.

fleminra commented 3 years ago

What's the type of the output image? I notice that I get that error when I try to write an image of type TYPE_BYTE_GRAY.

fleminra commented 3 years ago

What's the type of the output image? I notice that I get that error when I try to write an image of type TYPE_BYTE_GRAY.

And upon further investigation, it appears that lossless variant of the WebP format supports only ARGB. Someone correct me if I'm wrong. So this is a limitation of WebP, not a bug in webp-imageio. Although maybe webp-imageio should check the image type when encoding to WebP, and throw an IllegalArgumentException for types it doesn't support.

Ali-RS commented 3 years ago

Hi @fleminra

I am using the default settings. Can you please let me know how I can specify it and what output type should I use for the glow map?

This the code snippet I am using to export the image

                    // Obtain image
                    try {
                        BufferedImage image = ImageIO.read(new File(convert.getSourceRoot(), key.getName()));

                        // Obtain a WebP ImageWriter instance
                        ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();

                        // Configure encoding parameters
                        WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());

                        writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT);

                        // writeParam.setCompressionMode(WebPWriteParam.MODE_EXPLICIT);
                        // writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSY_COMPRESSION]);
                        // writeParam.setCompressionQuality(0.9f);

                        // Configure the output on the ImageWriter
                        writer.setOutput(new FileImageOutputStream(outputFile));

                        // Encode
                        writer.write(null, new IIOImage(image, null, null), writeParam);
                    } catch (Exception e) {
                        log.error("Error converting texture to WebP. Using original texture instead.", e);
                        convertedTextures.remove(outputFile.getPath());
                        continue;
                    }