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

asked on

PNGEncoder

FileOutputStream fout = new FileOutputStream( "C:\\Java Games\\ImageDemo\\src\\Garraway_" + colour_channel + ".png" );
PngEncoder.encode( fout, seperated[ colour_channel ], false );
fout.close();

PNGEncoder? Where do i get this from?  Im using JDK 1.5 BTW.
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium 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
SOLUTION
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

trial?  is there not a free version or jar?
SOLUTION
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
The last link is inclusive source
OK ive downlaoded it but how do i use it?  all the ZIP coinatins is .java and .class files? and i cant see any methods called encode?
SOLUTION
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
SOLUTION
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
You can use ImageIO, but it depends on what the source is
What is

>>seperated[ colour_channel ]

?
ok, ive got the .java file so i take it i just add it to my src folder...
Don't you have the JAR or the class? If so, just add it to your class-path or your IDE's class-path if you use an IDE. You can add the source if you want, make sure the package-structure is correct.
>> i take it i just add it to my src folder...
Make sure the

        package xxxxxx;

stuff is adapted correctly.
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.FileOutputStream;

public class Img
{
    public static final Toolkit toolkit = Toolkit.getDefaultToolkit();
   
    public static void main( String[] args )
    {
        try
        {
            Image img = readImage( "C:\\Java Games\\ImageDemo\\src\\Garraway.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;
               
                seperated[ colour_channel ] = createImage( width, height, res );
               
                FileOutputStream fout = new FileOutputStream( "C:\\Java Games\\ImageDemo\\src\\Garraway_" + colour_channel + ".png" );
                PngEncoder.encode( fout, seperated[ colour_channel ], false );
                fout.close();
            }
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
        }
    }
   
    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;
    }
}
SOLUTION
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
SOLUTION
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
OK CEHJ, stuck:

init:
deps-jar:
Compiling 2 source files to C:\Java Games\ImageDemo\build\classes
C:\Java Games\ImageDemo\src\Img.java:45: non-static method myCreateImage() cannot be referenced from a static context
                RenderedImage rendImage = myCreateImage();
1 error
BUILD FAILED (total time: 0 seconds)

CODE:


 public class Img {
    public static final Toolkit toolkit = Toolkit.getDefaultToolkit();

    public static void main( String[] args ) {
        try {
           
            Image img = readImage( "C:\\Java Games\\ImageDemo\\src\\Garraway.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;
               
                seperated[ colour_channel ] = createImage( width, height, res );
               
                RenderedImage rendImage = myCreateImage();
               
                // Write generated image to a file
                try {
                    // Save as JPEG
                    File file = new File("C:\\Java Games\\ImageDemo\\src\\Garraway_" + colour_channel + ".jpg" );
                    ImageIO.write( rendImage, "jpg", file );
                } catch (IOException e) {
                }
               
            }
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
    }
   
    public RenderedImage myCreateImage() {
        int width = 100;
        int height = 100;
       
        // Create a buffered image in which to draw
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       
        // Create a graphics contents on the buffered image
        Graphics2D g2d = bufferedImage.createGraphics();
       
        // Draw graphics
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(Color.black);
        g2d.fillOval(0, 0, width, height);
       
        // Graphics context no longer needed so dispose it
        g2d.dispose();
       
        return bufferedImage;
    }
   
    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;
    }
}
SOLUTION
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
SOLUTION
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
Wiat im wanting to use your code... Date: 04/03/2006 02:36PM BST

found yout second typo, but not for your first...

also i get package java.imageio does not exist.

 i use jdk1.5....
SOLUTION
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
Mmmm it works creating the PNG image but they are black... no features or any other colours in them...

basically what im tryign to do is read a colour image, and create a red representation of the colour image, a green representation and a blue representation...  but my RGB representations are all coming out black...

any idea?
latest code:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.FileOutputStream;

public class Img {
    public static final Toolkit toolkit = Toolkit.getDefaultToolkit();
   
    public static void main( String[] args ) {
        try {
           
            Image img = readImage( "C:\\Java Games\\ImageDemo\\src\\Garraway.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;
               
                seperated[ colour_channel ] = createImage( width, height, res );
               
                BufferedImage jpg = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB);
                Graphics g = jpg.getGraphics();
                g.drawImage( seperated[ colour_channel ], 0, 0, null );
                FileOutputStream fout = new FileOutputStream( "C:\\Java Games\\ImageDemo\\src\\Garraway_" + colour_channel + ".jpg" );
                javax.imageio.ImageIO.write( jpg, "JPG", fout );
                g.dispose();
            }
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
    }
   
    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;
    }
}
ASKER CERTIFIED SOLUTION
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
SOLUTION
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
where would i call method?
>>where would i call method?

See my last
i still use these lines though:

Graphics g = jpg.getGraphics();
g.drawImage( seperated[ colour_channel ], 0, 0, null );

?
working!!

cheers mate
No
(Of course that 'no' came out of sequence ;-))
>>Of course that 'no' came out of sequence

eh? im lost!
can getting the red green and blue representations of the colour image be improved?
:-)

>>eh? im lost!

Don't worry ;-)

>>can getting the red green and blue representations of the colour image be improved?

I'll have a look

Seems OK

>>
final int shift = 16 - 8 * colour_channel;
               
                // 0x00ff0000 is red, 0x0000ff00 green and 0x000000ff blue
                final int mask = 0x000000ff << shift;
>>

could have been
               
// 0x00ff0000 is red, 0x0000ff00 green and 0x000000ff blue
final int mask = 0x000000ff << 8 * colour_channel;
Well i didn't post in there as it didn't seem to be quite the same question
your right - its not really the same question... in my new questions im looking for somebody to explain in detail how the red representation image is achieved from the colour image...