?
Solved

PNGEncoder

Posted on 2006-04-03
38
Medium Priority
?
286 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.
0
Comment
Question by:ellandrd
  • 15
  • 14
  • 6
  • +1
38 Comments
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 480 total points
ID: 16360536
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 480 total points
ID: 16360553
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16360555
trial?  is there not a free version or jar?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 120 total points
ID: 16360565
I think there is an eopn-source one:

http://javaboutique.internet.com/PNG_Encoder/
0
 
LVL 37

Expert Comment

by:zzynx
ID: 16360570
The last link is inclusive source
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16360578
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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 480 total points
ID: 16360579
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 480 total points
ID: 16360595
>> OK ive downlaoded it but how do i use it?
http://www.objectplanet.com/pngencoder/api/index.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16360630
You can use ImageIO, but it depends on what the source is
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16360644
What is

>>seperated[ colour_channel ]

?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16360651
ok, ive got the .java file so i take it i just add it to my src folder...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16360664
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
 
LVL 37

Expert Comment

by:zzynx
ID: 16360674
>> i take it i just add it to my src folder...
Make sure the

        package xxxxxx;

stuff is adapted correctly.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16360684
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1400 total points
ID: 16360694
Here's an e.g. of using ImageIO, which requires no 3rd party software:

http://javaalmanac.com/egs/javax.imageio/Graphic2File.html
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1400 total points
ID: 16360775
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16360801
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1400 total points
ID: 16360802
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1400 total points
ID: 16360820
>>public RenderedImage myCreateImage() {

should be

public static RenderedImage myCreateImage() {

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

Author Comment

by:ellandrd
ID: 16360849
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1400 total points
ID: 16360864
Ah you found another typo ;-)

it's javax.imageio
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16360902
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16360943
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 1400 total points
ID: 16360956
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1400 total points
ID: 16360989
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16360990
where would i call method?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16361015
>>where would i call method?

See my last
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16361022
i still use these lines though:

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

?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16361040
working!!

cheers mate
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16361041
No
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16361047
(Of course that 'no' came out of sequence ;-))
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16361081
>>Of course that 'no' came out of sequence

eh? im lost!
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16361097
can getting the red green and blue representations of the colour image be improved?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16361357
:-)

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

Expert Comment

by:CEHJ
ID: 16361639
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16361763
Well i didn't post in there as it didn't seem to be quite the same question
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16361861
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 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