Skip to content

Commit

Permalink
Merge pull request #3 from umjammer/0.0.3
Browse files Browse the repository at this point in the history
0.0.3
  • Loading branch information
umjammer authored Dec 16, 2022
2 parents ea62f54 + f90a1bf commit fe009af
Show file tree
Hide file tree
Showing 30 changed files with 1,212 additions and 1,160 deletions.
17 changes: 4 additions & 13 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,11 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2

# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD
if: ${{ github.event_name == 'pull_request' }}
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -61,10 +52,10 @@ jobs:
# and modify them (or add more) to build your code if your project
# uses a compiled language

- name: Autobuild
- name: Build with Maven
run: |
brew install libavif
mvn -B install --file pom.xml
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
27 changes: 18 additions & 9 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ jobs:
runs-on: macos-latest

steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
run: |
brew install libavif
mvn -B package --file pom.xml
- name: Checkout repository
uses: actions/checkout@v3

- name: Check w/o SNAPSHOT when "bump version"
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
uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'temurin'
cache: maven

- name: Build with Maven
run: |
brew install libavif
mvn -B package --file pom.xml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
cscope.*
tags
tmp/
local.properties
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ based on https://github.com/AOMediaCodec/libavif/tree/main/android_jni
* https://jitpack.io/#umjammer/vavi-image-avif
* add `-Djna.library.path=/usr/local/lib` for jvm args

## Usage

```java
BufferedImage image = ImageIO.read(Paths.get("/foo/bar.avif").toFile());
```
1 change: 1 addition & 0 deletions local.properties.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file=/foo/bar/baz.avif
11 changes: 6 additions & 5 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.2</version>
<version>0.0.3</version>

<name>Java AVIF Decoder</name>
<scm>
Expand All @@ -22,7 +22,8 @@

<profiles>
<profile>
<id>jnarator</id>
<!-- mvn -P jnaerator jnaerator:generate -->
<id>jnaerator</id>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -78,7 +79,7 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.8.2</version>
<version>5.9.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -96,12 +97,12 @@
<dependency>
<groupId>com.github.umjammer</groupId>
<artifactId>vavi-commons</artifactId>
<version>1.1.6</version>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>com.github.umjammer</groupId>
<artifactId>vavi-image</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
Expand Down
20 changes: 14 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 @@ -5,13 +5,15 @@

import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.util.logging.Level;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import vavi.awt.image.jna.avif.AvifLibrary;
import vavi.awt.image.jna.avif.avifDecoder;
import vavi.awt.image.jna.avif.avifRGBImage;
import vavi.awt.image.jna.avif.avifROData;
import vavi.util.Debug;


/**
Expand All @@ -24,7 +26,12 @@ public class Avif {
private static final Avif avif = new Avif();

// This is a utility class and cannot be instantiated.
private Avif() {}
private Avif() {
String version = AvifLibrary.INSTANCE.avifVersion();
if (!version.startsWith("0.11")) {
Debug.println(Level.SEVERE, "wrong version: " + version);
}
}

public static Avif getInstance() {
return avif;
Expand All @@ -36,8 +43,8 @@ public static Avif getInstance() {
* @param encoded The encoded image. buffer.position() must be 0.
* @return true if the bytes seem like an AVIF image, false otherwise.
*/
public boolean isAvifImage(ByteBuffer encoded, int length) {
final avifROData data = new avifROData();
public static boolean isAvifImage(ByteBuffer encoded, int length) {
avifROData data = new avifROData();
data.data = Native.getDirectBufferPointer(encoded);
data.size = length;
return AvifLibrary.INSTANCE.avifPeekCompatibleFileType(data) == AvifLibrary.AVIF_TRUE;
Expand All @@ -52,17 +59,18 @@ public boolean isAvifImage(ByteBuffer encoded, int length) {
*/
public BufferedImage getCompatibleImage(ByteBuffer encoded, int length) {
Pointer buffer = Native.getDirectBufferPointer(encoded);
avifDecoder decoder = createDecoderAndParse(buffer, length);
avifDecoder decoder = createDecoderAndParse(buffer, length, Runtime.getRuntime().availableProcessors());
BufferedImage image = new BufferedImage(decoder.image.width, decoder.image.height, BufferedImage.TYPE_4BYTE_ABGR);
//System.err.println("image depth: " + decoder.image.depth);
return image;
}

private avifDecoder createDecoderAndParse(final Pointer buffer, int length) {
private avifDecoder createDecoderAndParse(Pointer buffer, int length, int threads) {
avifDecoder decoder = AvifLibrary.INSTANCE.avifDecoderCreate();
if (decoder == null) {
throw new IllegalStateException("Failed to create AVIF Decoder.");
}
decoder.maxThreads = threads;
decoder.ignoreXMP = AvifLibrary.AVIF_TRUE;
decoder.ignoreExif = AvifLibrary.AVIF_TRUE;

Expand Down Expand Up @@ -95,7 +103,7 @@ private avifDecoder createDecoderAndParse(final Pointer buffer, int length) {
*/
public BufferedImage decode(ByteBuffer encoded, int length, BufferedImage bitmap) {
Pointer buffer = Native.getDirectBufferPointer(encoded);
avifDecoder decoder = createDecoderAndParse(buffer, length);
avifDecoder decoder = createDecoderAndParse(buffer, length, Runtime.getRuntime().availableProcessors());
int res = AvifLibrary.INSTANCE.avifDecoderNextImage(decoder);
if (res != AvifLibrary.avifResult.AVIF_RESULT_OK) {
throw new IllegalStateException(String.format("Failed to decode AVIF image. Status: %d", res));
Expand Down
Loading

0 comments on commit fe009af

Please sign in to comment.