JAI Problem with scale

D

dmcquay

I am a somewhat novice Java programmer and I am brand-new to JAI. I am
having trouble scaling an image using JAI. It works, but the image
comes out distorted. Below is the function in my code that does the
scaling. I know it is the scale function that causes the problem
because I tried skipping the scale part and just creating a RenderedOp
and then doing the JAI.create("filestore", ... and there were no
problems. You can see some of the images I scaled with this code at:

http://www.hairstylesformal.com/749.jpg
http://www.hairstylesformal.com/771.jpg

I have put a lot of work into this app and it is practically done
except for this glitch. Any help would be greatly appreciated.

Thanks,
Dustin


********
CODE
********
public File alterImage(File imgFile)
{
try{
ParameterBlock pb;
InputStream is = new FileInputStream(imgFile);
SeekableStream s = SeekableStream.wrapInputStream(is, true);
RenderedOp objImage = JAI.create("stream", s);
((OpImage)objImage.getRendering()).setTileCache(null);

// Determine image orientation
if(objImage.getWidth() > objImage.getHeight()) {
currImgIsPortrait = false;
} else {
currImgIsPortrait = true;
}

/*
* If the image is too large, scale it down.
*/
if(objImage.getWidth() > MAX_IMG_SIZE || objImage.getHeight() >
MAX_IMG_SIZE) {

float scale = MAX_IMG_SIZE / (float) (currImgIsPortrait ?
objImage.getHeight() : objImage.getWidth());

pb = new ParameterBlock();
pb.addSource(objImage);

pb.add(scale);//x scale factor
pb.add(scale);//y scale factor
pb.add(0.0F);//x translate
pb.add(0.0F);//y translate
pb.add(new InterpolationNearest());//interpolation method

objImage = JAI.create("scale", pb);

}

/*
* Save the image as a temp file
*/
File temp = File.createTempFile("mf_temp_img", ".jpg");

JPEGEncodeParam encodeParam = new JPEGEncodeParam();
encodeParam.setQuality(JPEG_QUALITY);
encodeParam.setHorizontalSubsampling(0,1);
encodeParam.setHorizontalSubsampling(1,1);
encodeParam.setHorizontalSubsampling(2,1);
encodeParam.setVerticalSubsampling(0,1);
encodeParam.setVerticalSubsampling(1,1);
encodeParam.setVerticalSubsampling(2,1);

JAI.create("filestore", objImage, temp.getAbsolutePath(), "jpeg",
encodeParam);

return temp;

} catch(Exception e) {
e.printStackTrace();
return null;
}
}
 
J

Josh Falter

I have not used the scale in JAI before, but I have used the
java.awt.Graphics2D library to operate on a BufferedImage to do the
same thing before and it was incredibly easy and successful. If you
get desperate, you might look there.
 
A

Aaron Birenboim

I am a somewhat novice Java programmer and I am brand-new to JAI. I am
having trouble scaling an image using JAI...

These images have codec problems for me (loss of synch about 2/3 down)
I don't see before's so I don't know what distortion you are talking about.

I am new to Java, but very, very old to image processing.
Does scale do proper anti-aliasing?
Are there options for scale to include anti-aliasing?
Just a wild guess.
pb.add(new InterpolationNearest());//interpolation method

This looks like a method that would alias on downsample,
replicate on upsample. Generally a bad choice if you
want the image to look "photographic"
 
D

dmcquay

I am going to look into this. It would be much better for me to just
just Graphics2D for this b/c not only is JAI not working for me, but I
have to include a pretty heavy jar to use it or expect my users to
install JAI. I just figured I needed to use JAI to do this. I didn't
even think about Graphics2D in the standard libraries. Thanks.
 
D

dmcquay

Thanks for the help. The "before" images had no loss of synch. That
loss of synch is the problem I am talking about. I just didn't know
the right term to use so I called it distortion. I guess that is the
wrong word.

The interpolation method that is specified has a couple options, but I
don't know about anti-aliasing. However, someone made a suggestion to
just use Graphics2D library instead of JAI for this and I think that is
probably the way to go. Thanks for you help.
 
D

dmcquay

I have not used the scale in JAI before, but I have used the
java.awt.Graphics2D library to operate on a BufferedImage to do the
same thing before and it was incredibly easy and successful. If you
get desperate, you might look there.

I need to read in original images in jpeg format and be able re-encode
them and save them to file OR get an input stream from them when the
scale operation is complete. Do you know if this is possible off hand?
If not I will just research.

Thanks for possibly saving me some time.
Dustin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top