Hi Michael,
I think you are getting confused by some under-documented APIs that
never had a useful purpose for developers and, in consequence, that have
some undiscovered bugs in them.
coerceData is not the way to make a premultiplied image. Even worse, it
can have dangerous effects on an existing image. It is used internally
to munge raster data in the constructor that takes a raw raster and
colormodel, but it is not meant to be used in any other cases, or its
behavior in other cases is questionable at best, and buggy as you've
discovered at worst.
If you want a premultiplied raster, then use the TYPE_INT_ARGB_PRE image
type, don't use coerceData.
The dangerous thing that coerceData is doing is that it is replacing the
ColorModel in the image with a new one that doesn't match its declared
type. When we go to render into/out of it we end up believing its type
and we do the wrong thing.
Unfortunately, I'm not sure how to deal with that bug in the coerceData
method now - a BufferedImage is not supposed to ever change its type,
but this method can cause it to change its nature. I believe the right
thing for us to do is to gut the method and deprecate it and move the
functionality of what it currently does into the custom constructor.
In any case, simply create your images using TYPE_INT_ARGB (for
non-premultiplied) and TYPE_INT_ARGB_PRE (for premultiplied) and you
should be good to go...
...jim
[EMAIL PROTECTED] wrote:
When copying an image using Graphics2D.drawImage(),I'm suprised to find that
the isAlphaPremultiplied() property of the two images is not taken into
account. I believe the pixels are copied directly without respect to color
model.
A little example is below. I would expect the normalized (sRGB) pixel for each image to
be the same, but instead the "real" pixel value is the same and the
interpretation and normalized data differ.
Is this a bug or am I misunderstanding how these things should work?
Thanks much,
michael
[code]
public static void main(String[] args) {
BufferedImage premultiplied = new BufferedImage(10, 10,
BufferedImage.TYPE_INT_ARGB);
BufferedImage notpremultiplied = new BufferedImage(10, 10,
BufferedImage.TYPE_INT_ARGB);
premultiplied.coerceData(true);
notpremultiplied.coerceData(false);
Graphics2D preG = premultiplied.createGraphics();
preG.setColor(new Color(0, 40, 0, 120));
preG.fillRect(0, 0, 10, 10);
preG.dispose();
// prints:
// real: pixel:2013276160 120,0,85,0
// normalized: pixel:2013287680 120,0,85,0
printOriginPixel(premultiplied);
Graphics2D notG = notpremultiplied.createGraphics();
notG.drawImage(premultiplied, 0, 0, 10, 10, null);
notG.dispose();
// prints:
// real: pixel:2013276160 120,0,40,0
// normalized: pixel:2013276160 120,0,40,0
printOriginPixel(notpremultiplied);
}
public static void printOriginPixel(BufferedImage image) {
int defaultColorSpacePixel = image.getRGB(0, 0);
int realPixel = ((int[]) image.getRaster().getDataElements(0,
0, null))[0];
System.out.println("real: pixel:"+ realPixel + " "
+ image.getColorModel().getAlpha(realPixel)
+ ","
+ image.getColorModel().getRed(realPixel)
+ ","
+ image.getColorModel().getGreen(realPixel)
+ ","
+ image.getColorModel().getBlue(realPixel));
System.out.println("normalized: pixel:" + defaultColorSpacePixel + "
"
+
ColorModel.getRGBdefault().getAlpha(defaultColorSpacePixel)
+ ","
+
ColorModel.getRGBdefault().getRed(defaultColorSpacePixel)
+ ","
+
ColorModel.getRGBdefault().getGreen(defaultColorSpacePixel)
+ ","
+
ColorModel.getRGBdefault().getBlue(defaultColorSpacePixel));
}
[/code]
[Message sent by forum member 'kazoobrewer' (kazoobrewer)]
http://forums.java.net/jive/thread.jspa?messageID=293378
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".