WebpConverterImpl implements WebpConverter { @Override public byte[] encodeLosslessWebp(BufferedImage image) { WebpNativeLibLoader.loadNativeLibraryIfNeeded(); byte[] bgraArray = new byte[image.getWidth() * image.getHeight() * 4]; int i = 0; for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { int argb = image.getRGB(x, y); for (int b = 0; b < 4; b++) { bgraArray[i++] = (byte) argb; argb >>= 8; } } } return libwebp.WebPEncodeLosslessBGRA( bgraArray, image.getWidth(), image.getHeight(), image.getWidth() * 4); }  @Override byte[] encodeLosslessWebp(BufferedImage image);  }
@Test public void encodeLosslessWebp() throws IOException { BufferedImage inputImage = ImageIO.read(getTestDataFile("webp/drawing.png")); byte[] expectedOutputBytes = Files.readAllBytes(getTestDataFile("webp/drawing_q70.webp").toPath()); byte[] outputBytes = new WebpConverterImpl().encodeLosslessWebp(inputImage); assertThat(outputBytes).isEqualTo(expectedOutputBytes); }