Hi Denis,
I believe the bug you're running into was fixed in JDK 6. However,
there's a more efficient way to copy a subregion of a BufferedImage
without using getSubimage() (the latter method has some non-obvious
side effects, so it's best to avoid it if at all possible).
The best way is to use the variant of Graphics.drawImage() that takes
both a src and dst region:
http://download.java.net/jdk6/docs/api/java/awt/
Graphics.html#drawImage(java.awt.Image,%20int,%20int,%20int,%20int,%
20int,%20int,%20int,%20int,%20java.awt.image.ImageObserver)
So in your case it should be as easy as:
g.drawImage(img,
dstx, dsty, dstx+w, dsty+w,
srcx, srcy, srcx+w, srcy+w, null);
Looks like a good question to add to our FAQ:
http://java.sun.com/products/java-media/2D/reference/faqs/index.html
Thanks,
Chris
PS... Go Jackets.
On Aug 10, 2006, at 10:55 AM, Denis Bueno wrote:
I'm obviously confused about the right way to copy a BufferedImage
instance. I
assume there's one piece of code which I have yet to discover that
will copy (1)
a normal, full BufferedImage and (2) any sub image (obtained via
getSubimage())
of (1).
Currently I'm using the code in copyBufferedImage() below, which
generates the
following exception:
Exception in thread "main" java.lang.IllegalArgumentException:
Raster
BytePackedRaster: width = 800 height = 600 #channels 1 xOff = -20
yOff = -20 has
minX or minY not equal to zero: -20 -20
at java.awt.image.BufferedImage.<init>(BufferedImage.java:620)
at scratch.Aliasing.copyBufferedImage(Aliasing.java:73)
at scratch.Aliasing.createImage(Aliasing.java:54)
at scratch.Aliasing.<init>(Aliasing.java:20)
at scratch.Aliasing.main(Aliasing.java:83);
I tried calling WritableRaster.createWritableTranslatedChild(0, 0)
as was
suggested in a thread on JAI-interest [
http://archives.java.sun.com/cgi-bin/wa?A2=ind9907&L=jai-
interest&D=0&P=13579 ],
but, that enlarges my subimage (which should be 20x20).
I'm trying to solve a larger problem where subimages of a larger
BufferedImage
of type TYPE_BYTE_BINARY get distorted when they're painted onto
another part of
the original image. (I'm implementing a selection+translation tool
like in
Photoshop.) Eventually the code below will be a self-contained test
case of that
behavior. (At the moment I get around the problem described above
by copying the
entire BufferedImage before doing getSubimage().)
Any ideas? Thanks in advance.
-Denis
package scratch;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Aliasing extends JPanel {
private BufferedImage img = createImage();
public Aliasing() {
setPreferredSize(new Dimension(800, 600));
}
@Override
protected void paintComponent(final Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.clearRect(0, 0, getWidth(), getHeight());
g2.drawImage(img, new AffineTransformOp(new AffineTransform(),
g2.getRenderingHints()),
0, 0);
g2.setColor(Color.red);
g2.setStroke(new BasicStroke(2f));
g2.draw(new Line2D.Double(50, 0, 150, 100));
}
@Override
public void update(final Graphics g) {
paintComponent(g);
}
private BufferedImage createImage() {
BufferedImage ret = new BufferedImage(800, 600,
BufferedImage.TYPE_BYTE_BINARY);
assert (ret.getColorModel() instanceof IndexColorModel);
final Graphics2D graphics = ret.createGraphics();
graphics.setStroke(new BasicStroke(2f));
graphics.draw(new Line2D.Double(0, 0, 100, 100));
BufferedImage subimage = ret.getSubimage(20, 20, 20, 20);
subimage = copyBufferedImage(subimage);
graphics.setColor(Color.black);
graphics.fillRect(0, 0, 20, 20);
graphics.drawImage(subimage,
new AffineTransformOp(new AffineTransform
(),
graphics.getRenderingHints()),
0, 150);
// Outline the subimage.
graphics.setColor(Color.white);
graphics.drawRect(0, 150, subimage.getWidth(),
subimage.getHeight());
return ret;
}
private BufferedImage copyBufferedImage(final BufferedImage
source) {
final WritableRaster raster = source.copyData(null);
final BufferedImage copy = new BufferedImage
(source.getColorModel(),
raster,
source.isAlphaPremultiplied(), null);
return copy;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(800, 600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Aliasing aliasing = new Aliasing();
frame.add(aliasing);
frame.pack();
frame.setVisible(true);
}
}
======================================================================
=====
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".