Aspose.Slides for Java 18.12: `renderToGraphics` from BufferedImage Size

When doing

BufferedImage img = new BufferedImage(1000, 500, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D slideImg = slideBufferImg.createGraphics();

slide.renderToGraphics(notesOptions, slideImg, 1.0f);

using Java 11, we get:

java.lang.IllegalArgumentException: Dimensions (width=2147483647 height=2147483647) are too large

	at java.desktop/java.awt.image.SampleModel.<init>(SampleModel.java:130)
	at java.desktop/java.awt.image.ComponentSampleModel.<init>(ComponentSampleModel.java:140)
	at java.desktop/java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSampleModel.java:87)
	at java.desktop/java.awt.image.PixelInterleavedSampleModel.createCompatibleSampleModel(PixelInterleavedSampleModel.java:144)
	at java.desktop/sun.awt.image.ByteInterleavedRaster.createCompatibleWritableRaster(ByteInterleavedRaster.java:1274)
	at java.desktop/sun.awt.image.BufferedImageGraphicsConfig.createCompatibleImage(BufferedImageGraphicsConfig.java:122)
	at com.aspose.slides.internal.ey.break.do(Unknown Source)
	at com.aspose.slides.Slide.renderToGraphics(Unknown Source)

The problem seems to be that Aspose.Slides is calling

Rectangle bounds = slideImg.getDeviceConfiguration().getBounds();
slideImg.getDeviceConfiguration().createCompatibleImage(bounds.width, bounds.height);

but in Java 11 (or really 9b89 or later), the bounds of a buffered image are Int.MAXIMUM_VALUE as per this bug report:

https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8072682

Aspose should be using the actual image width and height, not the device configuration bounds.

@rocketraman,

I have observed the issue shared by you and request you to please share the sample presentation and working sample code reproducing the issue. We will be able to investigate the issue and log in our issue tracking system on provision of requested information.

Thanks @mudassir.fayyaz. Here is a repro:

File: Sample.zip (33.5 KB)

and code:

import com.aspose.slides.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class TestPptImage {
  public static void main(String[] args) throws Exception {
    License l = new License();
    l.setLicense(TestPptImage.class.getResourceAsStream("/Aspose.Total.lic"));

    try(FileInputStream input = new FileInputStream("/home/raman/tmp/Sample.pptx")) {
      Presentation p = new Presentation(input);
      ISlide s = p.getSlides().get_Item(0);

      double width = 1280;
      double height = p.getSlideSize().getSize().getHeight() / p.getSlideSize().getSize().getWidth() * width;

      System.out.println("Width=" + width);
      System.out.println("Height=" + height);

      BufferedImage slideBufferImg = new BufferedImage((int)width, (int)height, BufferedImage.TYPE_3BYTE_BGR);
      Graphics2D slideGraphics = slideBufferImg.createGraphics();

      NotesCommentsLayoutingOptions options = new NotesCommentsLayoutingOptions();
      options.setNotesPosition(NotesPositions.None);

      s.renderToGraphics(options, slideGraphics, 1.0f);
    }
  }
}

When run with JDK11, the output of this is:

Width=1280.0
Height=720.0
Exception in thread "main" java.lang.IllegalArgumentException: Dimensions (width=2147483647 height=2147483647) are too large
	at java.desktop/java.awt.image.SampleModel.<init>(SampleModel.java:130)
	at java.desktop/java.awt.image.ComponentSampleModel.<init>(ComponentSampleModel.java:140)
	at java.desktop/java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSampleModel.java:87)
	at java.desktop/java.awt.image.PixelInterleavedSampleModel.createCompatibleSampleModel(PixelInterleavedSampleModel.java:144)
	at java.desktop/sun.awt.image.ByteInterleavedRaster.createCompatibleWritableRaster(ByteInterleavedRaster.java:1274)
	at java.desktop/sun.awt.image.BufferedImageGraphicsConfig.createCompatibleImage(BufferedImageGraphicsConfig.java:122)
	at com.aspose.slides.internal.ey.break.do(Unknown Source)
	at com.aspose.slides.Slide.renderToGraphics(Unknown Source)
	at TestPptImage.main(TestPptImage.java:28)

@rocketraman,

I have worked with the sample code and presentation file shared by you and unable to observe any issue. However an investigation ticket with ID SLIDESJAVA-37544 has been created in our issue tracking system to further investigate issue on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

@Adnan.Ahmad Did you run the code with JDK 11?

@rocketraman,

I have worked with this issue again using Java 11 and have been able to observe issue. However it is working fine with Java 8. I have created issue to further investigate and resolve this problem. I request for your patience and we will share good news with you soon.

Thank you! Yes, the issue does not happen on JDK8, because JDK8 behavior is actually faulty, and a bug was fixed in JDK9b89 which fixes that behavior. Aspose is actually relying on the faulty behavior. See this bug report for an explanation:

https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8072682

Specifically, this comment:

If you can theoretically (with enough memory/heap) create an image with MAXINT, MAXINT bounds, then that is what should be reported [ed: referring to graphics.getDeviceConfiguration().getBounds() on a Graphics2D object from a BufferedImage] in all cases, not the bounds of a (random) image. If we make that change are we likely to break anyone ? I suppose since the current values are random its not likely anyone can be relying on them anyway but I wouldn’t be too surprised if (say) some code was allocating some backing corresponding to the largest bounds seen so far. I expect if we change it to MAXINT we’ll find those cases quite quickly !

as well as the workaround:

CUSTOMER SUBMITTED WORKAROUND : Do not use graphics.getDeviceConfiguration to get the image bounds get it directly from image.getWidth() and image.getHeight()

@rocketraman,

Thank you very much for sharing such information. We will get back to you with good news with soon.

@Adnan.Ahmad Just curious about why the status of the issue changed to Blocked?

@rocketraman,

I have verified from our issue tracking system and regret to share that after initial investigation of issue the issue has blocked owing to internal limitation of API which will be addressed first. We will share the good news with you as soon as the issue will be fixed.

1 Like

The issue linked to this topic SLIDESJAVA-37544 shows as “Closed” now, and indeed with 23.11 it works correctly on JDK 11.

BUT, unfortunately on JDK 17, this issue has regressed again. I’m guessing whatever the fix was is checking specifically for version 11 rather than checking if the version is > 1.8.

Same reproduction as above, but use JDK 17.

@rocketraman

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESJAVA-39352

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

2 Likes

@rocketraman

For now, you can use below workaround:

--add-opens java.desktop/sun.java2d=ALL-UNNAMED --add-opens java.desktop/sun.awt.image=ALL-UNNAMED

Moreover, we will also be resolving it in future version and let you know once it is fixed.

1 Like

Thank you, that workaround works.