We help IT Professionals succeed at work.

PNGEncoder

ellandrd
ellandrd asked
on
Medium Priority
323 Views
Last Modified: 2008-02-01
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.
Comment
Watch Question

zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:

Author

Commented:
trial?  is there not a free version or jar?
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
I think there is an eopn-source one:

http://javaboutique.internet.com/PNG_Encoder/
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
The last link is inclusive source

Author

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?
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:
>> OK ive downlaoded it but how do i use it?
http://www.objectplanet.com/pngencoder/api/index.html
CERTIFIED EXPERT
Top Expert 2016

Commented:
You can use ImageIO, but it depends on what the source is
CERTIFIED EXPERT
Top Expert 2016

Commented:
What is

>>seperated[ colour_channel ]

?

Author

Commented:
ok, ive got the .java file so i take it i just add it to my src folder...
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> i take it i just add it to my src folder...
Make sure the

        package xxxxxx;

stuff is adapted correctly.

Author

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;
    }
}
CERTIFIED EXPERT
Top Expert 2016
Commented:
Here's an e.g. of using ImageIO, which requires no 3rd party software:

http://javaalmanac.com/egs/javax.imageio/Graphic2File.html
CERTIFIED EXPERT
Top Expert 2016
Commented:
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();

Author

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;
    }
}
CERTIFIED EXPERT
Top Expert 2016
Commented:
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);
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>public RenderedImage myCreateImage() {

should be

public static RenderedImage myCreateImage() {

although you shouldn't really have much code in main. Better create an instance

Author

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....
CERTIFIED EXPERT
Top Expert 2016
Commented:
Ah you found another typo ;-)

it's javax.imageio

Author

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?

Author

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;
    }
}
CERTIFIED EXPERT
Top Expert 2016
Commented:
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;
}
CERTIFIED EXPERT
Top Expert 2016
Commented:
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);

Author

Commented:
where would i call method?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>where would i call method?

See my last

Author

Commented:
i still use these lines though:

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

?

Author

Commented:
working!!

cheers mate
CERTIFIED EXPERT
Top Expert 2016

Commented:
No
CERTIFIED EXPERT
Top Expert 2016

Commented:
(Of course that 'no' came out of sequence ;-))

Author

Commented:
>>Of course that 'no' came out of sequence

eh? im lost!

Author

Commented:
can getting the red green and blue representations of the colour image be improved?
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

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

CERTIFIED EXPERT
Top Expert 2016

Commented:
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;
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
CERTIFIED EXPERT
Top Expert 2016

Commented:
Well i didn't post in there as it didn't seem to be quite the same question

Author

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...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.