Skip to content

Commit

Permalink
Merge pull request #6 from umjammer/0.0.5
Browse files Browse the repository at this point in the history
0.0.5
  • Loading branch information
umjammer authored Jan 11, 2024
2 parents 98efc3b + 6811062 commit f3f9f2c
Show file tree
Hide file tree
Showing 26 changed files with 1,296 additions and 1,057 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ jobs:

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
settings-path: ${{ github.workspace }} # location for the settings.xml file

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -55,7 +63,7 @@ jobs:
- name: Build with Maven
run: |
brew install libavif
mvn -B install --file pom.xml
mvn -B package --file pom.xml -DskipTests
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
if: ${{ contains(github.event.head_commit.message, 'bump version') }}
run: grep "<version>" pom.xml | head -1 | grep -v SNAPSHOT

- name: Set up JDK 1.8
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '8'
java-version: '17'
distribution: 'temurin'
cache: maven

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[![Release](https://jitpack.io/v/umjammer/vavi-image-avif.svg)](https://jitpack.io/#umjammer/vavi-image-avif)
[![Java CI](https://github.com/umjammer/vavi-image-avif/actions/workflows/maven.yml/badge.svg)](https://github.com/umjammer/vavi-image-avif/actions/workflows/maven.yml)
[![CodeQL](https://github.com/umjammer/vavi-image-avif/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/umjammer/vavi-image-avif/actions/workflows/codeql-analysis.yml)
![Java](https://img.shields.io/badge/Java-8-b07219)
![Java](https://img.shields.io/badge/Java-17-b07219)
[![Parent](https://img.shields.io/badge/Parent-vavi--image--sandbox-pink)](https://github.com/umjammer/vavi-image-sandbox)

# vavi-image-avif

Java AVIF decoder<br/>
Java AVIF decoder and encoder<br/>
wrapped [libavif](https://github.com/AOMediaCodec/libavif) by jna<br/>
based on https://github.com/AOMediaCodec/libavif/tree/main/android_jni

Expand All @@ -15,14 +15,17 @@ based on https://github.com/AOMediaCodec/libavif/tree/main/android_jni

## Install

* install `libavif` e.g. `brew intall libavif`
* install `libavif` 1.0.3 ... e.g. `brew intall libavif`
* https://jitpack.io/#umjammer/vavi-image-avif
* add `-Djna.library.path=/usr/local/lib` for jvm args
* add `-Djna.library.path=/opt/homebrew/lib` for jvm args

## Usage

```java
// read
BufferedImage image = ImageIO.read(Paths.get("/foo/bar.avif").toFile());
// write
ImageIO.write(image, "AVIF", Paths.get("/foo/baz.avif").toFile());
```

## TODO
Expand Down
2 changes: 2 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jdk:
- openjdk17
17 changes: 8 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>vavi</groupId>
<artifactId>vavi-image-avif</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>

<name>Java AVIF Decoder</name>
<scm>
Expand Down Expand Up @@ -51,22 +51,21 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<version>3.11.0</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>UTF-8</encoding>
<release>17</release>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<version>3.1.2</version>
<configuration>
<argLine>
--add-opens java.logging/java.util.logging=ALL-UNNAMED
-Djava.util.logging.config.file=${project.build.testOutputDirectory}/logging.properties
-Djna.library.path=/usr/local/lib
-Djna.library.path=/usr/local/lib:/opt/homebrew/lib
</argLine>
<trimStackTrace>false</trimStackTrace>
</configuration>
Expand All @@ -79,7 +78,7 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.3</version>
<version>5.10.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -107,7 +106,7 @@
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.12.1</version>
<version>5.14.0</version>
</dependency>

<dependency>
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/vavi/awt/image/avif/jna/Avif.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.logging.Level;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import vavi.awt.image.jna.avif.AvifLibrary;
import vavi.awt.image.jna.avif.avifDecoder;
Expand All @@ -33,7 +34,7 @@ public class Avif {
// This is a utility class and cannot be instantiated.
private Avif() {
String version = AvifLibrary.INSTANCE.avifVersion();
if (!version.startsWith("0.11")) {
if (!version.startsWith("1.0.3")) {
Debug.println(Level.SEVERE, "wrong version: " + version);
}
}
Expand All @@ -51,7 +52,7 @@ public static Avif getInstance() {
public static boolean isAvifImage(ByteBuffer encoded, int length) {
avifROData data = new avifROData();
data.data = Native.getDirectBufferPointer(encoded);
data.size = length;
data.size.setValue(length);
return AvifLibrary.INSTANCE.avifPeekCompatibleFileType(data) == AvifLibrary.AVIF_TRUE;
}

Expand Down Expand Up @@ -87,7 +88,7 @@ private avifDecoder createDecoderAndParse(Pointer buffer, int length, int thread
// crbug.com/1198455).
decoder.strictFlags &= ~AvifLibrary.avifStrictFlag.AVIF_STRICT_PIXI_REQUIRED;

int res = AvifLibrary.INSTANCE.avifDecoderSetIOMemory(decoder, buffer, length);
int res = AvifLibrary.INSTANCE.avifDecoderSetIOMemory(decoder, buffer, new NativeLong(length));
if (res != AvifLibrary.avifResult.AVIF_RESULT_OK) {
throw new IllegalStateException("Failed to set AVIF IO to a memory reader.");
}
Expand Down Expand Up @@ -187,7 +188,7 @@ public ByteBuffer encode(BufferedImage bitmap, int quality) {
// ByteBuffer nativeBuffer = ByteBuffer.allocateDirect(bitmap.getWidth() * bitmap.getHeight() * bytes);
// rgb.pixels = Native.getDirectBufferPointer(nativeBuffer);
// rgb.rowBytes = bitmap.getWidth() * bytes;
Debug.printf(Level.FINE, StringUtil.paramString(rgb));
//Debug.printf(Level.FINE, StringUtil.paramString(rgb)); // TODO paramString doesn't work jdk16+

nativeBuffer.put(((DataBufferByte) bitmap.getRaster().getDataBuffer()).getData());

Expand Down Expand Up @@ -227,11 +228,11 @@ public ByteBuffer encode(BufferedImage bitmap, int quality) {
throw new IllegalStateException(String.format("Failed to finish encode: %s", AvifLibrary.INSTANCE.avifResultToString(finishResult)));
}

Debug.printf(Level.FINE, "Encode success: %d total bytes", avifOutput.size);
Debug.printf(Level.FINE, "Encode success: %d total bytes", avifOutput.size.longValue());

AvifLibrary.INSTANCE.avifRGBImageFreePixels(rgb);
AvifLibrary.INSTANCE.avifEncoderDestroy(encoder);

return avifOutput.data.getByteBuffer(0, avifOutput.size);
return avifOutput.data.getByteBuffer(0, avifOutput.size.longValue());
}
}
Loading

0 comments on commit f3f9f2c

Please sign in to comment.