-2

I'm trying to copy in three new BufferedImage the same BufferedImage content, this is my code:

    ColorModel cm = image.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = image.copyData(null);
    BufferedImage[] images = {
        new BufferedImage(cm, raster, isAlphaPremultiplied, null),
        new BufferedImage(cm, raster, isAlphaPremultiplied, null),
        new BufferedImage(cm, raster, isAlphaPremultiplied, null)
    };

Even if I'm editing these images in distinct ways, the result is the same. I'm sure it's all ok because the code work properly if I have only one copy, but not more than one.

How can I manage something like this?

FaMa
  • 1
  • there is some sharing of memory - how can you manage what?? I dont see hell of a lot of code so no answer – gpasch Feb 14 '18 at 14:33
  • I can't post hundreds lines of code. There is no need of more code. I got the solution by myself, thank you anyway. – FaMa Feb 14 '18 at 14:38
  • Possible duplicate: https://stackoverflow.com/questions/3514158/how-do-you-clone-a-bufferedimage – Harald K Feb 15 '18 at 08:00

1 Answers1

0

I got it. Even if I the BufferedImages are different, ColorModel and WritableRaster are only reference to the same objects.

If someone has my same problem try something like this:

    private BufferedImage copyImage() {
        ColorModel cm = image.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = image.copyData(null);
        return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
    }

    public BufferedImage[] copyUpdatedImage() {
        BufferedImage[] images = {
            copyImage(),
            copyImage(),
            copyImage()
        };
    }
FaMa
  • 1
  • Concerning the copyImage method, there are faster solutions: https://stackoverflow.com/a/66569366/5093841 – clic Mar 11 '21 at 07:48