添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

While converting tiff files to JPEG, we occasionally (rarely) will run into the following error:

Note: The same file will convert properly to PNG format.

javax.imageio.IIOException: Invalid argument to native writeImage
	at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
	at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1067)
	at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:363)
	at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:173)
	at javax.imageio.ImageWriter.write(ImageWriter.java:615)
	at javax.imageio.ImageIO.doWrite(ImageIO.java:1622)
	at javax.imageio.ImageIO.write(ImageIO.java:1588)

I'm attaching a sample maven project which has sample files and test cases. You should see a failure in the test case com.acme.ConvertTiffToJpegTest#convertProblemTiff

Version Info:

        <dependency>
            <groupId>com.twelvemonkeys.imageio</groupId>
            <artifactId>imageio-tiff</artifactId>
            <version>3.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.twelvemonkeys.imageio</groupId>
            <artifactId>imageio-jpeg</artifactId>
            <version>3.8.2</version>
        </dependency>
openjdk version "1.8.0_332"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_332-b09)
OpenJDK 64-Bit Server VM (Temurin)(build 25.332-b09, mixed mode)

Your problem.tif contains an alpha channel (for some reason, it is still fully opaque). I believe the OpenJDK JPEG plugin (that we delegate the actual writing to) does not support writing RGB + A JPEGs (this support has also been removed in later Oracle JDKs). You could try the same code, using an Oracle JDK 1.8.x for comparison. I believe the provider's canEncodeImage should also return false for the image in the case it's not supported.

JPEG is not a great format for storing alpha (because it's lossy), and tool support isn't very widespread. Most software will misinterpret such images as CMYK/YCCK JPEGs. So I believe your best bet would be to either remove the alpha, or write using a different format like PNG or WebP.

Of course, if you really want to, I think you still can write JPEGs with alpha channel, it's just inconvenient... The "fix" is to write using the Raster as input, and add enough metadata for the reader to recognize this as RGB + A (or YCbCr + A). I could probably add support for this to the TwelveMonkeys library at some point too, feel free to suggest such a feature and/or submit a PR! 😀

Yeah, I'm not sure why this image has an alpha channel. The image was created as part of a test dataset long ago where I used an app to convert a PDF to Tiff. I just happened to notice that it failed like some of the other live data we saw.

Regarding the ImageIO.write result, I tried Open JDK 17 and it does return false instead of failing so it's definitely something with OpenJDK 8.

For our purposes, I'm fine with just using PNG if the alpha channel is present. We only use JPEG if there is color in the image because the performance was much faster.

Thanks for the reply!

I think I'll close this issue for now, if you are okay with that explanation. I definitively think this is an OpenJDK 8 issue. It should behave like JDK 17, and just silently return false.

PS: The JDK PNG write support has been notoriously slow, as it used hardcoded maximum zip/deflate compression... It's possible to get better performance by using Java 9 or later, or JDK9 PNG Writer Backport.

Yeah, let's go ahead and close it. I don't think there's much reason to spend a lot of work debugging JDK8 issues at this point. We're working on getting this project moving forward but it's been tough due to some dependencies.

Thanks for the help!