Skip to main content

[JAVA2D] Saving ARGB BufferedImages as JPEG with ImageIO

2 replies [Last post]
Anonymous

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chris Campbell

On Mar 23, 2004, at 11:07 AM, J�rg Lehni wrote:
> I do not agree.
>

Okay. Like I said, please send me (off the list) a small testcase that
reproduces the problem. Code snippets are not enough; we prefer to see
a full testcase that we can compile easily and run.

> If you're interested I can send you on of those images for analysis.
>

Yes, that would be very helpful, in addition to the testcase.

Thanks,
Chris

> Best
>
> J�rg
>
> Am 23.03.2004 um 19:40 schrieb Chris Campbell:
>
>> Hi J�rg,
>>
>> This sounds a lot like 4836466, which we closed as "not a bug". See
>> the report for more details:
>> http://developer.java.sun.com/developer/bugParade/bugs/4836466.html
>>
>> Basically, it appears that many native apps have poor support for
>> JPEG images with an alpha channel. If you can read back one of these
>> images using IIO (ImageIO.read()) and everything displays properly,
>> I'd say your issue is related to 4836466. If not, send me a small
>> testcase containing your sample code (off the list) and I can take a
>> look. Maybe there's a separate issue lurking out there...
>>
>> Thanks,
>> Chris
>>
>> On Mar 22, 2004, at 12:39 PM, J�rg Lehni wrote:
>>> Thanks Jim!
>>>
>>> That's how my latest code looks like, i simplified it a lot:
>>>
>>> param.setSourceBands(new int[] {0, 1, 2});
>>> param.setDestinationType(
>>>
>>> ImageTypeSpecifier.createPacked(ColorSpace.getInstance(ColorSpace.CS_
>>> sRGB),
>>> 0x00ff0000,
>>> 0x0000ff00,
>>> 0x000000ff,
>>> 0x0,
>>> DataBuffer.TYPE_INT,
>>> false
>>> )
>>> );
>>>
>>> But it still produces the same problems. The resulting jpeg files
>>> are strange, don't display correctly in some viewers and get much
>>> bigger in size than necessary.
>>>
>>> If I use the workaround of copying the TYPE_INT_ARGB BufferedImage
>>> into a TYPE_INT_RGB BufferedImage and then generate the JPEG by just
>>> using the default settings, everything works fine.
>>>
>>> It looks to me as if there is a bug in the JPEG codec.
>>>
>>> What do you think?
>>>
>>> J�rg
>>>
>>> Am 17.03.2004 um 01:55 schrieb Jim Graham:
>>>
>>>> An image of TYPE_INT_ARGB uses a PackedColorModel with a Packed
>>>> Raster with a TYPE_INT DataBuffer. It is incompatible with the
>>>> Byte-based ComponentColorModel (and SampleModel) that you are
>>>> creating. I'm not sure how these two pieces of data would
>>>> interact in this case...
>>>>
>>>> ...jim
>>>>
>>>> --On 03/15/04 22:40:12 +0100 J�rg Lehni wrote:
>>>>> if (bi.getType() == BufferedImage.TYPE_INT_ARGB) {
>>>>> // create a new ColorModel with OPAQUE transparency and no
>>>>> alpha channel.
>>>>> ColorModel cm = new
>>>>> ComponentColorModel(bi.getColorModel().getColorSpace(), false,
>>>>> false,
>>>>> Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
>>>>
>>>> ====================================================================
>>>> =======
>>>> To unsubscribe, send email to listserv@java.sun.com and include in
>>>> the body
>>>> of the message "signoff JAVA2D-INTEREST". For general help, send
>>>> email to
>>>> listserv@java.sun.com and include in the body of the message "help".
>>>>
>>>
>>> =====================================================================
>>> ======
>>> To unsubscribe, send email to listserv@java.sun.com and include in
>>> the body
>>> of the message "signoff JAVA2D-INTEREST". For general help, send
>>> email to
>>> listserv@java.sun.com and include in the body of the message "help".
>>
>> ======================================================================
>> =====
>> To unsubscribe, send email to listserv@java.sun.com and include in
>> the body
>> of the message "signoff JAVA2D-INTEREST". For general help, send
>> email to
>> listserv@java.sun.com and include in the body of the message "help".
>>
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

J�rg Lehni

Ok, here's the file and a testcase. The app loads an image from the
internet, draws it into a ARGB BufferedImage and saves this as a new
Jpeg, trying to to get the codec to ignore tha alpha channel.

J�rg

import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.color.*;
import javax.imageio.*;
import javax.imageio.stream.*;

public class JpegBug {
public static void main(String[] args) throws Exception {
// first load an image, create an ARGB BufferedImage and draw the
image into it:
Image image = Toolkit.getDefaultToolkit().createImage(new
URL("http://www.tu-darmstadt.de/hrz/hhlr/links/test.jpg"));
MediaTracker mt = new MediaTracker(new Container());
mt.addImage(image, 0);
try {
mt.waitForID(0);
} catch (InterruptedException e){
return;
}
BufferedImage buffered = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
buffered.createGraphics().drawImage(image, 0, 0, null);
// new generate a jpeg from this image:
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
if (iter.hasNext()) writer = (ImageWriter)iter.next();
if (writer != null) {
ImageOutputStream ios = null;
try {
// Prepare output file
File file = new File("output.jpeg");
if (file.exists()) file.delete();
ios = ImageIO.createImageOutputStream(file);
writer.setOutput(ios);
// Set some parameters
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.7f);

// set the source bands so that the codedc ignores the alpha band:
param.setSourceBands(new int[] {0, 1, 2});
// specify the correct desination type without alpha:
param.setDestinationType(

ImageTypeSpecifier.createPacked(ColorSpace.getInstance(ColorSpace.CS_sRG
B),
0x00ff0000,
0x0000ff00,
0x000000ff,
0x0,
DataBuffer.TYPE_INT,
false
)
/*
ImageTypeSpecifier.createInterleaved(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {2, 1, 0},
DataBuffer.TYPE_BYTE,
false,
false
)
*/
);

// Write the image
writer.write(null, new IIOImage(buffered, null, null), param);

// Cleanup
ios.flush();
} finally {
if (ios != null) ios.close();
writer.dispose();
}
}
}
}

Am 23.03.2004 um 20:20 schrieb Chris Campbell:

> On Mar 23, 2004, at 11:07 AM, J�rg Lehni wrote:
>> I do not agree.
>>
>
> Okay. Like I said, please send me (off the list) a small testcase
> that reproduces the problem. Code snippets are not enough; we prefer
> to see a full testcase that we can compile easily and run.
>
>> If you're interested I can send you on of those images for analysis.
>>
>
> Yes, that would be very helpful, in addition to the testcase.
>
> Thanks,
> Chris
>
>
>> Best
>>
>> J�rg
>>
>> Am 23.03.2004 um 19:40 schrieb Chris Campbell:
>>
>>> Hi J�rg,
>>>
>>> This sounds a lot like 4836466, which we closed as "not a bug". See
>>> the report for more details:
>>> http://developer.java.sun.com/developer/bugParade/bugs/4836466.html
>>>
>>> Basically, it appears that many native apps have poor support for
>>> JPEG images with an alpha channel. If you can read back one of
>>> these images using IIO (ImageIO.read()) and everything displays
>>> properly, I'd say your issue is related to 4836466. If not, send me
>>> a small testcase containing your sample code (off the list) and I
>>> can take a look. Maybe there's a separate issue lurking out
>>> there...
>>>
>>> Thanks,
>>> Chris
>>>
>>> On Mar 22, 2004, at 12:39 PM, J�rg Lehni wrote:
>>>> Thanks Jim!
>>>>
>>>> That's how my latest code looks like, i simplified it a lot:
>>>>
>>>> param.setSourceBands(new int[] {0, 1, 2});
>>>> param.setDestinationType(
>>>>
>>>> ImageTypeSpecifier.createPacked(ColorSpace.getInstance(ColorSpace.CS
>>>> _sRGB),
>>>> 0x00ff0000,
>>>> 0x0000ff00,
>>>> 0x000000ff,
>>>> 0x0,
>>>> DataBuffer.TYPE_INT,
>>>> false
>>>> )
>>>> );
>>>>
>>>> But it still produces the same problems. The resulting jpeg files
>>>> are strange, don't display correctly in some viewers and get much
>>>> bigger in size than necessary.
>>>>
>>>> If I use the workaround of copying the TYPE_INT_ARGB BufferedImage
>>>> into a TYPE_INT_RGB BufferedImage and then generate the JPEG by
>>>> just using the default settings, everything works fine.
>>>>
>>>> It looks to me as if there is a bug in the JPEG codec.
>>>>
>>>> What do you think?
>>>>
>>>> J�rg
>>>>
>>>> Am 17.03.2004 um 01:55 schrieb Jim Graham:
>>>>
>>>>> An image of TYPE_INT_ARGB uses a PackedColorModel with a Packed
>>>>> Raster with a TYPE_INT DataBuffer. It is incompatible with the
>>>>> Byte-based ComponentColorModel (and SampleModel) that you are
>>>>> creating. I'm not sure how these two pieces of data would
>>>>> interact in this case...
>>>>>
>>>>> ...jim
>>>>>
>>>>> --On 03/15/04 22:40:12 +0100 J�rg Lehni wrote:
>>>>>> if (bi.getType() == BufferedImage.TYPE_INT_ARGB) {
>>>>>> // create a new ColorModel with OPAQUE transparency and no
>>>>>> alpha channel.
>>>>>> ColorModel cm = new
>>>>>> ComponentColorModel(bi.getColorModel().getColorSpace(), false,
>>>>>> false,
>>>>>> Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
>>>>>
>>>>> ===================================================================
>>>>> ========
>>>>> To unsubscribe, send email to listserv@java.sun.com and include in
>>>>> the body
>>>>> of the message "signoff JAVA2D-INTEREST". For general help, send
>>>>> email to
>>>>> listserv@java.sun.com and include in the body of the message
>>>>> "help".
>>>>>
>>>>
>>>> ====================================================================
>>>> =======
>>>> To unsubscribe, send email to listserv@java.sun.com and include in
>>>> the body
>>>> of the message "signoff JAVA2D-INTEREST". For general help, send
>>>> email to
>>>> listserv@java.sun.com and include in the body of the message "help".
>>>
>>> =====================================================================
>>> ======
>>> To unsubscribe, send email to listserv@java.sun.com and include in
>>> the body
>>> of the message "signoff JAVA2D-INTEREST". For general help, send
>>> email to
>>> listserv@java.sun.com and include in the body of the message "help".
>>>
>>
>
> =======================================================================
> ====
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body
> of the message "signoff JAVA2D-INTEREST". For general help, send
> email to
> listserv@java.sun.com and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".