• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

detailed description on getting RGB representations of a colour image

bascially ive got this small program that reads a colour image and creates 3 new images: a red presentation, a green representation and a blue representation.

What i plan to do next is write a few paragraphs on how this is achieved but ive already searched for some descriptions on the web and had no success.

Can anybody help?  if you think you can provide me with detailed comment lines of my code i will paste it here...

0
ellandrd
Asked:
ellandrd
  • 2
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
If the code is too long, can you upload it somewhere and post a link to it?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you don't know what the code does, you can try understanding it by walking through it and checking what each method does in the Java-doc: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/package-summary.html
0
 
ellandrdAuthor Commented:
Its not too long - 60 something lines i think...

public class Img {
    public static final Toolkit toolkit = Toolkit.getDefaultToolkit() ;
    public static void main( String[] args ) {
        try {
            Image img = readImage( "C:\\miss_piggy.jpg" ) ;
            int width = img.getWidth( null ), height = img.getHeight( null ) ;
            // gets the pixels in the default (A)RGB format
            int[] pix = grabPixels( img ) ;
            int[] res = new int[ pix.length ] ;
            // resulting images: 0=red, 1=green, 2=blue
            Image[] seperated = new Image[ 3 ] ;
            for ( int colour_channel = 0 ; colour_channel < 3 ; colour_channel++ ) {
                final int shift = 16 - 8 * colour_channel ;
                // 0x00ff0000 is red, 0x0000ff00 green and 0x000000ff blue
                final int mask = 0x000000ff << shift ;
                // iterates all pixels in (A)RGB format
                for( int idx = 0 ; idx < pix.length ; idx++ )
                    // keeps only one channel and sets the other 2 channels to zero
                    res[ idx ] = pix[ idx ] & mask ;
                BufferedImage jpg = makeBufferedImage( width, height, res) ;
                Graphics g = jpg.getGraphics() ;
                g.drawImage( seperated[ colour_channel ], 0, 0, null ) ;
                String[] colours = { "Red", "Green", "Blue" } ;
                FileOutputStream fout = new FileOutputStream( "C:\\miss_piggy_" + colours[ colour_channel ] + ".jpg" ) ;
                javax.imageio.ImageIO.write( jpg, "JPG", fout ) ;
                g.dispose() ;
            }
        } catch ( Exception ex ) {
            ex.printStackTrace() ;
        }
    }
   
    private static BufferedImage makeBufferedImage( int width, int height, int[] res ) {
        BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB ) ;
        image.setRGB( 0, 0, width, height, res, 0, width ) ;
        return image ;
    }
   
    public static int[] grabPixels( Image img ) {
        int width = img.getWidth( null ), height = img.getHeight( null ) ;
        PixelGrabber grabber = new PixelGrabber( img, 0, 0, width, height, true ) ;
        try {
            grabber.grabPixels() ;
        } catch ( InterruptedException ie ) {
            ie.printStackTrace() ;
        } return ( int[] ) grabber.getPixels() ;
    }
   
    public static Image createImage( int width, int height, int[] pix ) {
        return toolkit.createImage( new MemoryImageSource( width, height, pix, 0, width ) ) ;
    }
   
    public static Image readImage( String filename ) {
        Image img = toolkit.getImage( filename ) ;
        boolean success = false ;
        try {
            while( ( toolkit.checkImage( img, -1, -1, null ) & ( ImageObserver.ERROR | ImageObserver.ABORT ) ) == 0 ) {
                if( success = toolkit.prepareImage( img, -1, -1, null ) )
                    break ;
                synchronized( img ) {
                    img.wait( 50 ) ;
                }
            }
        } catch ( InterruptedException iex ) {
        } return success ? img : null ;
    }
}

Ellandrd
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now