I am trying to implement a painting tool with layer support. For each layer I create a WritableRaster. To display the canvas, the layers have to be merged. What's the most efficient way to do this? I could use a bufferedImage, set the raster and draw each layer to the Graphics context with a alpha composite. But isn't it better/faster to merge it on data level and then draw a single image?
Edit: I did some profiling. This is what my current approach looks like:
//tmp.getRaster().setRect(layer.getRaster()); // VERY slow
tmp.getRaster().setDataElements(0, 0, layer.getRaster()); //slow
g2.drawImage(tmp, 0, 0, scaledDim.width, scaledDim.height, null);
I used BufferedImages instead of WritableRasters for each layer before and there was no delay noticeable.
My next step would be to cache top and bottom layers, so there would be only 3 images to be drawn. That would probably solve the problem.
maybe you can use JAI: