Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

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

Avatar of Mayank S
Mayank S
Flag of India image

If the code is too long, can you upload it somewhere and post a link to it?
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ellandrd

ASKER

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