Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.ArrayIndexOutOfBoundsException when getting a gif #321

Open
yellowpeter2019 opened this issue Jan 29, 2019 · 3 comments
Open

java.lang.ArrayIndexOutOfBoundsException when getting a gif #321

yellowpeter2019 opened this issue Jan 29, 2019 · 3 comments

Comments

@yellowpeter2019
Copy link

@danfickle
I tried to convert the html into an image.

When I encountered a gif on the html code, the program threw an exception like this

com.openhtmltopdf.general: INFO: java.lang.ArrayIndexOutOfBoundsException: 4096
	at com.sun.imageio.plugins.gif.GIFImageReader.read(GIFImageReader.java:984)
	at javax.imageio.ImageIO.read(ImageIO.java:1448)
	at javax.imageio.ImageIO.read(ImageIO.java:1352)
	at com.openhtmltopdf.swing.NaiveUserAgent.getImageResource(NaiveUserAgent.java:412)
	at com.openhtmltopdf.render.AbstractOutputDevice.getBackgroundImage(AbstractOutputDevice.java:204)
	at com.openhtmltopdf.render.AbstractOutputDevice.paintBackground0(AbstractOutputDevice.java:238)
	at com.openhtmltopdf.render.AbstractOutputDevice.paintBackground(AbstractOutputDevice.java:226)
	at com.openhtmltopdf.render.Box.paintBackground(Box.java:536)
	at com.openhtmltopdf.layout.Layer.paintBackgroundsAndBorders(Layer.java:322)
	at com.openhtmltopdf.layout.Layer.paint(Layer.java:416)
	at com.openhtmltopdf.layout.Layer.paintLayers(Layer.java:225)
	at com.openhtmltopdf.layout.Layer.paint(Layer.java:424)
	at com.openhtmltopdf.java2d.Java2DRenderer.writeSinglePage(Java2DRenderer.java:352)
	at com.openhtmltopdf.java2d.api.Java2DRendererBuilder.runFirstPage(Java2DRendererBuilder.java:139)

The gif:
10198151

html code:

<div class="game-icon">
    <div class="game-icon-wrapper" style="background-image: url(${gameIconUrl!''})"></div>
</div>

Java code:

Java2DRendererBuilder builder = new Java2DRendererBuilder();
builder.withHtmlContent(html, "file:///E:/tmp/");
builder.useDefaultPageSize(IMG_WIDTH / 72F, IMG_HEIGHT / 72F, Java2DRendererBuilder.PageSizeUnits.INCHES);
BufferedImagePageProcessor processor = new BufferedImagePageProcessor(BufferedImage.TYPE_INT_RGB, 1.0);

builder.toSinglePage(processor).runFirstPage();
BufferedImage image = processor.getPageImages().get(0);

Is this an issue? or am I doing something terribly wrong?
Waiting for your response.
Thx.

@syjer
Copy link
Contributor

syjer commented Jan 30, 2019

hi @yellowpeter2019 , it seems that it's a known bug in the java runtime, see the following issue https://bugs.openjdk.java.net/browse/JDK-7132728 .

I can reproduce your error by simply doing

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

public class TestImg {
    public static void main(String[] args) throws IOException {
        ImageIO.read(new File("a.gif")); //a.gif is https://user-images.githubusercontent.com/46878699/51895370-ef8cab80-23e4-11e9-9948-55d66fe18d2f.gif
    }
}

It will launch the following exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4096 out of bounds for length 4096
	at java.desktop/com.sun.imageio.plugins.gif.GIFImageReader.read(GIFImageReader.java:992)
	at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1468)
	at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1315)

(and I'm using the latest stable jdk

java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode, sharing)

)

So at the moment, I would advise to avoid animated gif as a source...

@yellowpeter2019
Copy link
Author

hi @yellowpeter2019 , it seems that it's a known bug in the java runtime, see the following issue https://bugs.openjdk.java.net/browse/JDK-7132728 .

I can reproduce your error by simply doing

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

public class TestImg {
    public static void main(String[] args) throws IOException {
        ImageIO.read(new File("a.gif")); //a.gif is https://user-images.githubusercontent.com/46878699/51895370-ef8cab80-23e4-11e9-9948-55d66fe18d2f.gif
    }
}

It will launch the following exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4096 out of bounds for length 4096
	at java.desktop/com.sun.imageio.plugins.gif.GIFImageReader.read(GIFImageReader.java:992)
	at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1468)
	at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1315)

(and I'm using the latest stable jdk

java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode, sharing)

)

So at the moment, I would advise to avoid animated gif as a source...

@syjer
Could you consider changing the gif image processing library?
For example: https://github.com/DhyanB/Open-Imaging

It seems to solve this problem.
qq 20190131093306

@danfickle
Copy link
Owner

Hi @yellowpeter2019,

Being able to provide a BufferedImage is on my internal road map, but not until after next release, otherwise it will never be released!

In the mean time you could resave the desired frame as a png to a byte stream and use that as a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants