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".

Reply via email to