Skip to content

Commit

Permalink
Merge pull request #2 from umjammer/0.0.2
Browse files Browse the repository at this point in the history
0.0.2
  • Loading branch information
umjammer authored Sep 10, 2022
2 parents 3ab79ba + 4c3080d commit ea62f54
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion 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-SNAPSHOT</version>
<version>0.0.2</version>

<name>Java AVIF Decoder</name>
<scm>
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/vavi/imageio/avif/AvifImageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package vavi.imageio.avif;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -77,14 +78,17 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
InputStream stream = new WrappedImageInputStream((ImageInputStream) input);

try {
ByteBuffer bb = ByteBuffer.allocateDirect(Integer.MAX_VALUE);
int l = 0;
while (l < bb.capacity()) {
int r = Channels.newChannel(stream).read(bb);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[8192];
while (true) {
int r = stream.read(b, 0, b.length);
if (r < 0) break;
l += r;
baos.write(b, 0, r);
}
int l = baos.size();
Debug.println(Level.FINE, "size: " + l);
ByteBuffer bb = ByteBuffer.allocateDirect(l);
bb.put(baos.toByteArray(), 0, l);

Avif avif = Avif.getInstance();

Expand Down
19 changes: 12 additions & 7 deletions src/main/java/vavi/imageio/avif/AvifImageReaderSpi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package vavi.imageio.avif;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -87,15 +88,19 @@ public boolean canDecodeInput(Object obj) throws IOException {
Debug.println(Level.FINE, "input: " + obj);
if (obj instanceof ImageInputStream) {
InputStream stream = new BufferedInputStream(new WrappedImageInputStream((ImageInputStream) obj));
stream.mark(stream.available()); // TODO available is integer.max
// we currently accept heif only
ByteBuffer bb = ByteBuffer.allocateDirect(stream.available()); // TODO available is integer.max
int l = 0;
while (l < bb.capacity()) {
int r = Channels.newChannel(stream).read(bb);
Debug.println(Level.FINE, "stream.available(): " + stream.available());
stream.mark(stream.available());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[8192];
while (true) {
int r = stream.read(b, 0, b.length);
if (r < 0) break;
l += r;
baos.write(b, 0, r);
}
int l = baos.size();
Debug.println(Level.FINE, "size: " + l);
ByteBuffer bb = ByteBuffer.allocateDirect(l);
bb.put(baos.toByteArray(), 0, l);
stream.reset();
Avif avif = Avif.getInstance();
return avif.isAvifImage(bb, l);
Expand Down

0 comments on commit ea62f54

Please sign in to comment.