diff --git a/src/javase/java/com/luciad/imageio/webp/WebPWriter.java b/src/javase/java/com/luciad/imageio/webp/WebPWriter.java index 9e7e837..09451df 100644 --- a/src/javase/java/com/luciad/imageio/webp/WebPWriter.java +++ b/src/javase/java/com/luciad/imageio/webp/WebPWriter.java @@ -83,13 +83,22 @@ private static byte[] encode(WebPEncoderOptions aOptions, RenderedImage aImage) throw new NullPointerException("Image may not be null"); } - boolean encodeAlpha = hasTranslucency(aImage); - if (encodeAlpha) { - byte[] rgbaData = getRGBA(aImage); - return WebP.encodeRGBA(aOptions, rgbaData, aImage.getWidth(), aImage.getHeight(), aImage.getWidth() * 4); - } else { - byte[] rgbData = getRGB(aImage); - return WebP.encodeRGB(aOptions, rgbData, aImage.getWidth(), aImage.getHeight(), aImage.getWidth() * 3); + ThreadLocal encoderThreadLocal = new ThreadLocal<>(); + try { + encoderThreadLocal.set(aOptions); + + byte[] pixels; + boolean encodeAlpha = hasTranslucency(aImage); + if (encodeAlpha) { + byte[] rgbaData = getRGBA(aImage); + pixels = WebP.encodeRGBA(aOptions, rgbaData, aImage.getWidth(), aImage.getHeight(), aImage.getWidth() * 4); + } else { + byte[] rgbData = getRGB(aImage); + pixels = WebP.encodeRGB(aOptions, rgbData, aImage.getWidth(), aImage.getHeight(), aImage.getWidth() * 3); + } + return pixels; + } finally { + encoderThreadLocal.remove(); } }