File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes getting problem in creating an image from int array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "getting problem in creating an image from int array" Watch "getting problem in creating an image from int array" New topic

getting problem in creating an image from int array

Aayush Patel

Joined: Apr 26, 2001
Posts: 21
I had created an int array from an image from the following code..
public void createImage(Image serverlateImage)
{int iw=0;
int ih=0;
int i=0;
try{ iw=serverlateImage.getWidth(null);
pixels=new int[iw*ih];
PixelGrabber pg=new PixelGrabber(serverlateImage,0,0,iw,ih,pixels,0,iw);
}catch(InterruptedException ie){}
now if i had created perfect int array then i m creating an image from this array...and code is following..
PrintFrame(int []pixels)
{System.out.println("size of array at printframe "+pixels.length);

Dimension d=getSize();
int w=(int)(d.getWidth());
int h=(int)(d.getHeight());
int al = 0,
rd = 0,
gr = 0,
bl = 0,
avg = 0;
int grayMap[] = new int[w*h];
// im=createImage(pixels,w,h);
// if(im!=null)
{for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
al = (pixels [(i*w)+j] & 0xff000000)>>24;
rd = (pixels [(i*w)+j] & 0x00ff0000)>>16;
gr = (pixels [(i*w)+j] & 0x0000ff00)>>8;
bl = (pixels [(i*w)+j] & 0x000000ff);
avg = (rd+gr+bl)/3;
grayMap [(i*w)+j] = ((al <<24) |(avg <<16)|(avg<<8)|avg);
im=createImage (grayMap,w,h) ;
public static Image createImage(int [] pixels, int width, int height)
MemoryImageSource imageSource = new MemoryImageSource(width,height,pixels,0,width);
return Toolkit.getDefaultToolkit().createImage(imageSource);
now can somebody tell me whats the problem with this code...y i am not able to get image....thnx in advance
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
I understand that you are trying to do a simple image processing functionality-grayscaling of RGB images.
Here is a similar code:

In your code, you should have included the Mediatracker while loading the image.Remember image loading takes some time.So you must ensure that the execution of the program waits until the image is fully loaded. m.waitForID9..) ensures this. Otherwise you get NullPointerException for the image.
Secondly in your method printFrame(int[] pixels) you took the width and height of the image to be created from:
Dimension d=getSize();
but assuming that you are writing the code inside a class extending FRame/Applet/Panel etc, getSize() will give the size of that component and this size has nothing to do with the image.What you need is the size of the original image from which you obtained the pixels. So in the imageToIntArray(...) method put the width and height of the image into member variables, so that you can get these values in the convert(..) method.
hope this helps.
For more image manipulation algorithms,codes and concepts like image processing theory,image filters, brightness/contrast, histograms, noise reduction etc you can visit my website: and go to the projects page.Or directly you can access:
hope this helps

Author of JPhotoBrush Pro (
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
I my reply above, I wrote 'image' variable as 'im' in the code:

Please correct it to:

wood burning stoves
subject: getting problem in creating an image from int array
It's not a secret anymore!