Hi,

I've written a piece of code that uses Java 2D to (jdk 1.5, Windows (XP)) to 
"split" an image into two other images. The code works, but is slow and it 
feels clumsy. I'm writing in the hope one of you clever people is willing to 
help me speed up this code.

The original image is a .png image that I read from disk (~100K) into a 
BufferedImage. The image is mostly transparent, with some (black) outlines. The 
image also contains pixels with one particular (defined) colour. This colour 
can be different from image to image, but is limited and defined within one 
image.

What the code does is process this original image into two new images:
1. a BufferedImage (bodyImage) that is the same as the original image, but 
where all pixels with that defined colour (paintRGB) are turned transparent (by 
setting it to rgb '0'),
2. a BufferedImage (colourImage) that is transparent expect for the pixels with 
that defined colour (paintRGB).

At the moment reading the original image into a BufferedImage takes about 3.5 
seconds. Processing the image into the two others takes another 4 seconds.

I read up and played with BufferedImageOps, but haven't managed to find one (or 
use one) that does that what I want:
ImageFilter doesn't work on BufferedImage.
LookupOp doesn't allow me to make one single colour transparent (as far as I 
can tell).

There is the code snippet:

[code]
public void read(File imageFile, int paintRGB) throws IOException {             
  BufferedImage imageOnDisk = ImageIO.read(imageFile);                          
  Graphics2D g = imageOnDisk.createGraphics();                                  
  g.setComposite(AlphaComposite.Src);                                           
  g.drawImage(imageOnDisk, 0, 0, null);                                         
  g.dispose();                                                                  
                                                                                
  int width = imageOnDisk.getWidth();                                           
  int height = imageOnDisk.getHeight();                                         
                                                                                
  BufferedImage bodyImage = new BufferedImage(width, height,                    
      BufferedImage.TYPE_INT_ARGB);                                             
  BufferedImage colourImage = new BufferedImage(width, height,                  
      BufferedImage.TYPE_INT_ARGB);                                             
                                                                                
  for (int x = 0; x < width; x++) {                                             
    for (int y = 0; y < height; y++) {                                          
      int rgb = imageOnDisk.getRGB(x, y);                                       
      if (rgb == paintRGB) {                                                    
        bodyImage.setRGB(x, y, 0);                                              
        colourImage.setRGB(x, y, rgb);                                          
      } else {                                                                  
        bodyImage.setRGB(x, y, rgb);                                            
        colourImage.setRGB(x, y, 0);                                            
      }                                                                         
    }                                                                           
  }                                                                             
}                              
[/code]

Thanks, Birgit Arkesteijn
[Message sent by forum member 'birgita' (birgita)]

http://forums.java.net/jive/thread.jspa?messageID=341950

===========================================================================
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
lists...@java.sun.com and include in the body of the message "help".

Reply via email to