tags:

views:

452

answers:

2

I have an int array containing gray scale values from 0-254, i also have the x and y size of the image. It is an easy thing to create an pgm image, but i want to display it in a jsp, so i need somehow to convert it to a jpeg or png image. If you suggest jai, than please tell me at which classes to look, or how to actually do it in jai. Thanks a lot, in advance.

+2  A: 

Maybe skip the PGM entirely?

int[] myImage = getGreyscaleIntArray();

BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = im.getRaster();
for(int h=0;h<height;h++)
{
    for(int w=0;w<width;w++)
    {
        raster.setSample(w,h,0, myImage[h * width + w]); 
    }
}

ByteArrayOutputStream myJpg = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(im, "jpg", myJpg);

uses the JAI ImageIO api, specifically the ImageIO utility class

WriteableRaster sample from the Java Image Processing cookbook

Stobor
This was the first thing a thought about. But it doesnt work. Try reading a pgm file. ImageIO.read() will always return null if it's pgm format.
Red33mer
thanks, thats it
Red33mer
A: 

ImageMagick works well for converting images and Jmagick provides an interface to call directly from java programs.

G_A
I would have to install ImageMagick. I don't want to do that, since i wouldn't be able to deploy the app on any server running tomcat.
Red33mer