tusharjoshi / jmonkeyengine

Automatically exported from code.google.com/p/jmonkeyengine
0 stars 0 forks source link

Problem with AWTLoader and sub images. #651

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
When using the AWTLoader.load(BufferedImage image, boolean flip) method you are 
currently creating a buffer with a size based on the width and height of the 
given image and then attempting load the data from the underlying buffer for 
that image.  The problem occurs when using the BufferedImage.getSubImage(...) 
method due to the fact that the method keeps the underlying buffer from the 
original for the sub image and only changes the sample model for the new image 
so that you only see the sub image.  The solution that I am using currently is 
to create a deep copy of my new subimage with a new raster obtained through the 
copyData(WritableRaster) method and then creating the texture from that.  I 
believe that you could simply just change your load method (more specifically 
the extractImageData method that it calls in order to obtain the byte buffer) 
to use the raster obtained through copyData without needing to create a new 
BufferedImage as it has all excess data removed.

Here is my* deepCopy method if it helps (source is the subimage):

public static BufferedImage deepCopy(BufferedImage source) {
    ColorModel cm = source.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = source.copyData(source.getRaster().createCompatibleWritableRaster());
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}

*-The method is not exactly mine, it comes from the answer given by user1050755 
at this question:  
http://stackoverflow.com/questions/3514158/how-do-you-clone-a-bufferedimage

Original issue reported on code.google.com by Matthew....@gmail.com on 18 Mar 2015 at 3:03