ellandrd
asked on
PNGEncoder
FileOutputStream fout = new FileOutputStream( "C:\\Java Games\\ImageDemo\\src\\Gar raway_" + 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.
PngEncoder.encode( fout, seperated[ colour_channel ], false );
fout.close();
PNGEncoder? Where do i get this from? Im using JDK 1.5 BTW.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The last link is inclusive source
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use ImageIO, but it depends on what the source is
What is
>>seperated[ colour_channel ]
?
>>seperated[ colour_channel ]
?
ASKER
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.
Make sure the
package xxxxxx;
stuff is adapted correctly.
ASKER
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserv er;
import java.awt.image.MemoryImage Source;
import java.awt.image.PixelGrabbe r;
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\\Gar raway.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\\Gar raway_" + 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;
}
}
import java.awt.Toolkit;
import java.awt.image.ImageObserv
import java.awt.image.MemoryImage
import java.awt.image.PixelGrabbe
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\\Gar
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\\Gar
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK CEHJ, stuck:
init:
deps-jar:
Compiling 2 source files to C:\Java Games\ImageDemo\build\clas ses
C:\Java Games\ImageDemo\src\Img.ja va: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\\Gar raway.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\\Gar raway_" + 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.createGraphi cs();
// 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;
}
}
init:
deps-jar:
Compiling 2 source files to C:\Java Games\ImageDemo\build\clas
C:\Java Games\ImageDemo\src\Img.ja
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\\Gar
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\\Gar
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.createGraphi
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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....
found yout second typo, but not for your first...
also i get package java.imageio does not exist.
i use jdk1.5....
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER
latest code:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedIma ge;
import java.awt.image.ImageObserv er;
import java.awt.image.MemoryImage Source;
import java.awt.image.PixelGrabbe r;
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\\Gar raway.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\\Gar raway_" + colour_channel + ".jpg" );
javax.imageio.ImageIO.writ e( 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;
}
}
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedIma
import java.awt.image.ImageObserv
import java.awt.image.MemoryImage
import java.awt.image.PixelGrabbe
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\\Gar
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\\Gar
javax.imageio.ImageIO.writ
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
where would i call method?
>>where would i call method?
See my last
See my last
ASKER
i still use these lines though:
Graphics g = jpg.getGraphics();
g.drawImage( seperated[ colour_channel ], 0, 0, null );
?
Graphics g = jpg.getGraphics();
g.drawImage( seperated[ colour_channel ], 0, 0, null );
?
ASKER
working!!
cheers mate
cheers mate
No
(Of course that 'no' came out of sequence ;-))
ASKER
>>Of course that 'no' came out of sequence
eh? im lost!
eh? im lost!
ASKER
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
>>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;
>>
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
ASKER
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...
ASKER