?
Solved

detailed description on getting RGB representations of a colour image

Posted on 2006-04-03
3
Medium Priority
?
221 Views
Last Modified: 2010-03-31
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
Comment
Question by:ellandrd
  • 2
3 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16361291
If the code is too long, can you upload it somewhere and post a link to it?
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 2000 total points
ID: 16361329
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16361442
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question