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...

LVL 16
ellandrdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.