Skip to content

Glavo/jwebp

Repository files navigation

JWebP - Pure Java WebP Decoder

codecov javadoc

A dependency-free, pure Java WebP decoder library that supports lossless and lossy compressed WebP images, as well as animated WebP.

This project was ported with Codex assistance from image-rs/image-webp.

We have ported test cases from image-rs and libwebp to verify its correctness, and it has been used in Hello Minecraft! Launcher.

Features

  • Pure Java implementation with no native dependencies.
  • Only depends on the java.base module, no dependency on other modules.
  • Supports lossy and lossless compressed WebP images.
  • Supports animated WebP.
  • Supports image scaling during reading.
  • Raw ICC, EXIF, and XMP metadata extraction
  • Provides optional JavaFX and Swing helper functionality for easily converting WebP images to JavaFX and Swing images.

Requirements

  • Java 17 or newer

Download

Gradle:

dependencies {
    implementation("org.glavo:webp:0.2.0")
}

Maven:

<dependency>
    <groupId>org.glavo</groupId>
    <artifactId>webp</artifactId>
    <version>0.2.0</version>
</dependency>

Basic Usage

Decode a whole image at once:

WebPImage image = WebPImage.read(Path.of("sample.webp"));
System.out.println(image.getWidth() + "x" + image.getHeight());
System.out.println("frames = " + image.getFrames().size());
System.out.println("pixels = " + image.getFirstFrame().getArgbPixels());

Stream frames from an animated WebP:

try (InputStream input = Files.newInputStream(Path.of("/animated.webp"));
     WebPImageReader reader = WebPImageReader.open(input)) {
    while (true) {
        WebPFrame frame = reader.readNextFrame();
        if (frame == null) {
            break;
        }
        System.out.println("duration = " + frame.getDurationMillis());
    }
}

JavaFX Integration

JWebP's core part only depends on the java.base module, which can work normally on the Android platform.

However, JWebP also provides optional components for JavaFX, located in the org.glavo.webp.javafx package, which can easily convert WebPImage to JavaFX Image:

// Create a JavaFX image from a WebPImage.
// If it is an animated WebP, it will automatically play the animation.
// You can control its behavior by passing the autoplay parameter.
javafx.scene.image.Image _ = new WebPFXImage(WebPImage.read(...));

// Create a JavaFX image from a WebPFrame.
javafx.scene.image.Image _ = new WebPFXImage(WebPFrame.read(...).getFirstFrame());

Swing Integration

JWebP provides an optional Swing integration component located in the org.glavo.webp.swing package, which can easily convert WebPImage to Swing BufferedImage:

// Create a Swing image from a WebPImage.
BufferedImage _ = WebPSwingUtils.fromWebPImage(WebPImage.read(...));

// Create a Swing image from a WebPFrame.
BufferedImage _ = WebPSwingUtils.fromWebPImage(WebPImage.read(...).getFirstFrame());

Currently, it only supports creating static BufferedImage, does not support animation and ImageIO.

Testing

Run all tests:

./gradlew test

The test suite includes:

  • project-local decoder regression tests
  • tests ported from image-rs
  • tests ported from libwebp
  • tests backed by the downloaded libwebp-test-data corpus

About

A dependency-free, pure Java WebP decoder library

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages