iFrankWu / thumbnailator

Automatically exported from code.google.com/p/thumbnailator
Other
0 stars 0 forks source link

When scaling transparent png images, the colors are being negated. #23

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Scale the attached image and convert to jpeg.
2.
3.

What is the expected output? What do you see instead?
The scaled image is expected in jpeg format.
Getting a scaled jpeg image with negative image color.

What version of the product are you using? On what operating system? Which
version of Java?
Thumbnailator 3.9
Java 1.6.21
Windows 7

Please provide any additional information below.

Original issue reported on code.google.com by rothe...@gmail.com on 22 Sep 2011 at 12:18

Attachments:

GoogleCodeExporter commented 9 years ago
Thank you for reporting the issue.

I've tried to resize the provided PNG to a JPEG, but have not been able to 
reproduce the color distortion. 

My environment is as follows:

* Thumbnailator 0.3.9
* Windows XP
* Java 1.6.0_21

The code that was used was:

  Thumbnails.of("images/pic_admin.png")
    .size(200, 200)
    .outputFormat("jpg")
    .toFile("images/Result.jpg");

Could you provide the code used to produce the JPEG with the color distortion?

Thank you!

Original comment by coobird...@gmail.com on 23 Sep 2011 at 3:27

GoogleCodeExporter commented 9 years ago
I'm not using the from file to file method, but taking a byte array and 
returning one.

InputStream inputStream = new ByteArrayInputStream(imageByteArray);

final BufferedImage bufferedImage = 
Thumbnails.of(inputStream).outputFormat("jpg").size(200, 200).asBufferedImage();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
imageIO.write(bufferedImage, "jpg", outputStream);

return outputStream.toByteArray();

Original comment by rothe...@gmail.com on 26 Sep 2011 at 8:30

GoogleCodeExporter commented 9 years ago
Thank you for the follow up.

I was able to get an image with color distortion using the following, based on 
the information I got from your code:

--------------------

  BufferedImage bufferedImage =
    Thumbnails.of(new FileInputStream("images/pic_admin.png"))
      .outputFormat("jpg")
      .size(200, 200)
      .asBufferedImage();

  ImageIO.write(bufferedImage, "jpg", new FileOutputStream("images/Result.jpg"));

--------------------

It appears that the issue here is that you're using the `ImageIO.write` method 
to write the `BufferedImage` to the `ByteArrayOutputStream`.

It turns out, the JPEG codec that is provided with Image I/O will create JPEG 
images with transparency, but many applications will not recognize the 
transparency and will display with distorted colors. (The image with the 
transparency that is provided to the codec in this case is the `BufferedImage` 
from Thumbnailator.)

(If you're interested, refer to the following link for more information: 
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4836466)

To get around this problem, Thumbnailator internally removes the transparency 
before saving the image to a JPEG, which avoids the color distortion issue.

So, this is not quite an issue with Thumbnailator, but an issue with how the 
`ImageIO` class is being used.

--------------------

The following code should create an image which will not exhibit the color 
distortion problem:

  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  Thumbnails.of(new ByteArrayInputStream(imageByteArray))
    .outputFormat("jpg")
    .size(200, 200)
    .toOutputStream(outputStream);

  return baos.toByteArray();

Please let me know if that will solve the issue.

Original comment by coobird...@gmail.com on 1 Oct 2011 at 4:57

GoogleCodeExporter commented 9 years ago
Solved the issue!
Thanks :)

Original comment by rothe...@gmail.com on 27 Oct 2011 at 11:26

GoogleCodeExporter commented 9 years ago
Good to hear it worked out. :)

I'll be closing this issue, as it doesn't require any work on Thumbnailator.

Original comment by coobird...@gmail.com on 29 Oct 2011 at 11:02