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.
LVL 16
ellandrdAsked:
Who is Participating?
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.

zzynxSoftware engineerCommented:
0
zzynxSoftware engineerCommented:
0
ellandrdAuthor Commented:
trial?  is there not a free version or jar?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Mayank SAssociate Director - Product EngineeringCommented:
I think there is an eopn-source one:

http://javaboutique.internet.com/PNG_Encoder/
0
zzynxSoftware engineerCommented:
The last link is inclusive source
0
ellandrdAuthor Commented:
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?
0
zzynxSoftware engineerCommented:
0
zzynxSoftware engineerCommented:
>> OK ive downlaoded it but how do i use it?
http://www.objectplanet.com/pngencoder/api/index.html
0
CEHJCommented:
You can use ImageIO, but it depends on what the source is
0
CEHJCommented:
What is

>>seperated[ colour_channel ]

?
0
ellandrdAuthor Commented:
ok, ive got the .java file so i take it i just add it to my src folder...
0
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
zzynxSoftware engineerCommented:
>> i take it i just add it to my src folder...
Make sure the

        package xxxxxx;

stuff is adapted correctly.
0
ellandrdAuthor Commented:
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;
    }
}
0
CEHJCommented:
Here's an e.g. of using ImageIO, which requires no 3rd party software:

http://javaalmanac.com/egs/javax.imageio/Graphic2File.html
0
CEHJCommented:
Try the following:


seperated[ colour_channel ] = createImage( width, height, res );
java.awt.image.BufferedImage png = new java.awt.image.BufferedImage( width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = png.getGraphics();
g.drawImage(seperated[ colour_channel ], 0, 0, null);
FileOutputStream fout = new FileOutputStream( "C:/Java Games/ImageDemo/src/Garraway_" + colour_channel + ".png" );
java.imageio.ImageIO.write(png, "PNG" fout);
g.dispose();
0
ellandrdAuthor Commented:
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;
    }
}
0
CEHJCommented:
Two typos

>>BufferedImage.TYPE_INT_RGB);
=

java.awt.image.BufferedImage.TYPE_INT_RGB);

>>java.imageio.ImageIO.write(png, "PNG" fout);

=

java.imageio.ImageIO.write(png, "PNG", fout);
0
CEHJCommented:
>>public RenderedImage myCreateImage() {

should be

public static RenderedImage myCreateImage() {

although you shouldn't really have much code in main. Better create an instance
0
ellandrdAuthor Commented:
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....
0
CEHJCommented:
Ah you found another typo ;-)

it's javax.imageio
0
ellandrdAuthor Commented:
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?
0
ellandrdAuthor Commented:
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;
    }
}
0
CEHJCommented:
Yes, i have. I forgot to do something, but this is much less kludgy:


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;
}
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
CEHJCommented:
That means you can change

>>
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 );
>>

to



BufferedImage jpg = makeBufferedImage( width, height, res);
0
ellandrdAuthor Commented:
where would i call method?
0
CEHJCommented:
>>where would i call method?

See my last
0
ellandrdAuthor Commented:
i still use these lines though:

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

?
0
ellandrdAuthor Commented:
working!!

cheers mate
0
CEHJCommented:
No
0
CEHJCommented:
(Of course that 'no' came out of sequence ;-))
0
ellandrdAuthor Commented:
>>Of course that 'no' came out of sequence

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

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

0
CEHJCommented:
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;
0
Mayank SAssociate Director - Product EngineeringCommented:
0
CEHJCommented:
Well i didn't post in there as it didn't seem to be quite the same question
0
ellandrdAuthor Commented:
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...
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.