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

Transparent background with incorrect file type #1083

Open
thomaskiesl opened this issue Jan 23, 2025 · 3 comments
Open

Transparent background with incorrect file type #1083

thomaskiesl opened this issue Jan 23, 2025 · 3 comments

Comments

@thomaskiesl
Copy link

thomaskiesl commented Jan 23, 2025

Describe the bug
If png files/images with alpha channels (transparent background) are renamed from .png to .jpg the following exception comes up. Changing the file type to .png it was processed as expected.

javax.imageio.IIOException: Bogus input colorspace
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1035)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:385)
    at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:175)
    at java.desktop/javax.imageio.ImageWriter.write(ImageWriter.java:613)
    at java.desktop/javax.imageio.ImageIO.doWrite(ImageIO.java:1632)
    at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1558)

It happens on production, because someone tried to upload such a file. Maybe there is an easy way to fix this.

Version information
1The version of the TwelveMonkeys ImageIO library in use.
3.12.0

@haraldk
Copy link
Owner

haraldk commented Jan 27, 2025

Hi Thomas,

If png files/images with alpha channels (transparent background) are renamed from .png to .jpg the following exception comes up

I'm afraid I don't understand this issue. Changing the file extension (renaming) files should not matter, as ImageIO detects file type by looking at the file contents, rather than the file name.

Can you provide a JUnit test case or a minimal program with a main method, along with sample files, that exposes the problem?

PS: I think newer versions of Java don't support reading/writing JPEG with alpha channel, while older versions did. But I don't know the version of Java you use. There's a reason why the issue template asks for this information. 😉

@thomaskiesl
Copy link
Author

Thanks for the information. My application is running on Java 17 (Open JDK).

These are the two images (one with jpg and one with png):

Image
Image

I reiceive the error with the following code - calling ImageIO.write()

ByteArrayInputStream imageInputStream = new ByteArrayInputStream(content);
BufferedImage image = ImageIO.read(imageInputStream);
File file = new File(path, fileName);
ImageIO.write(image, fileType, file);

@haraldk
Copy link
Owner

haraldk commented Jan 30, 2025

Okay, I checked the files, and they are identical (apart from the file name). They are both the same indexed PNG with transparency.

My guess is that you are trying to convert the image to JPEG, based on fileType. The code snippet does not explain how this is derived, but I guess it's based on the file name. In theory, this should either "work" (for older JDKs, creating a JPEG file most other software will not display correctly) or ImageIO.write should return false (for recent JDKs not supporting JPEG with alpha channel).

Now, the fun part... When you invoke ImageIO.write, the method will try to obtain a suitable writer based on the image's type and the format given. The image type (pixel format, color model etc) is derived from the image using ImageTypeSpecifier.createFromRenderedImage(image), but unfortunately this method has a bug for images with index color model, in that it does not retain the actual color model. So the fact that it contains alpha is lost... The writer is SPI then believes it can encode the image, but when it's handed the actual image data, it contains alpha and fails with the exception you see.

Unfortunately, I don't see a way to fix this as it is the JDK alone that causes this.

PS: Using ImageiO.read/ImageIO.write in combination is a very inefficient way of copying image files, but I assume you have reasons to do it this way. 😀

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

No branches or pull requests

2 participants