Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

JAI Problem with scale

Thread view: 
dmcquay@gmail.com - 26 Jan 2006 19:26 GMT
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;
            }
        }
Josh Falter - 27 Jan 2006 14:20 GMT
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.
dmcquay@gmail.com - 27 Jan 2006 16:20 GMT
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.
dmcquay@gmail.com - 27 Jan 2006 16:28 GMT
> 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
Aaron Birenboim - 27 Jan 2006 15:41 GMT
> I am a somewhat novice Java programmer and I am brand-new to JAI.  I am
> having trouble scaling an image using JAI...

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

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"
dmcquay@gmail.com - 27 Jan 2006 16:23 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.