Closed Thread Icon

Preserved Topic: Java image resizing: antialiasing trouble (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=13018" title="Pages that link to Preserved Topic: Java image resizing:  antialiasing trouble (Page 1 of 1)" rel="nofollow" >Preserved Topic: Java image resizing:  antialiasing trouble <span class="small">(Page 1 of 1)</span>\

 
Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-16-2003 21:26

My current issue is as simple as they come: I can read in a JPEG image, and resize it, and write it back out, but the final image is jaggy even though I've set rendering hints. Here's some of the relevant code...

code:
// used Graphics2D to read in an Image object startingImage (ImageIO.read gave me odd colors)

// code to figure out a scaling factor ([b]double[/b] scale)

// imageHeight and imageWidth are the original image sizes

BufferedImage bufferedImage = new BufferedImage( /* appropriate code */ );

Graphics2D bufferedImageContext = bufferedImage.createGraphics();

RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

AffineTransform affineTransform = AffineTransform.getScaleInstance(scale, scale);
bufferedImageContext.setRenderingHints(hints);
bufferedImageContext.drawImage(startingImage, affineTransform, null);

// write image to file
ImageIO.write(bufferedImage, "JPEG", new File("new_" + fileName);



edit: Ahem, and my actual problem... the code works fine (although there's more to it than just this sample), it's just that the image isn't antialiased. Jaggy resizing is bad! Any suggestions? Should I delve into the Java Advanced Imaging API?

edit: I'm about to try the AreaAveragingScaleFilter... part of the problem with Java is that it can be hard even to find out about things that should be helpful. Too much API, too little time.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

[This message has been edited by Perfect Thunder (edited 12-16-2003).]

[This message has been edited by Perfect Thunder (edited 12-16-2003).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 12-17-2003 09:43

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 12-17-2003 10:23

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-17-2003 14:24

Thanks for the response, InI! After I posted this, I came across the bilinear interpolation rendering hint you suggested, and the BufferedImageOp superclass (of which AffineTransformOp is my baby). I made a post on Sun's Java forums showing my current code.

I'm starting to worry that, as you suggested, it's just a platform-specific thing. You'd think that Windows 2000 with JDK 1.4.2 would support most things, but the bicubic interpolation rendering hint doesn't appear to be working.

Any further suggestions (shy of writing my own renderer)? I could do that -- I'm being paid and everything here -- but I'd rather use some 3rd-party library if it comes to that. I'm positive someone else will already have done the math, for a fairly common function like this.

I guess my next step is to try out Java Advanced Imaging. I was just hoping to avoid that level of complexity.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 12-17-2003 15:52

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-17-2003 16:13

I'll ask on cfxweb, thanks for the invitation... and I guess it wouldn't be too terrible to write my own renderer, since speed and efficiency aren't huge issues in this case. But if someone has working code already, I'll try to get that first.

My real original image is 1.4MB, so here's a medium-sized version (188kb JPEG), scaled down in Photoshop.

Here's the result of my AffineTransformOp (even with bicubic interpolation turned on everywhere it's available): jaggy output image (57kb JPEG)

In particular, notice the edge of the rooftop in the background.

Edit: Was there a recent rebuild of the cfxweb forums? Doesn't seem to be much history -- I was going to start with a search for similar problems.

Edit: Hey Alan! Let me draw your attention to a fantastic new technique known as reading the whole damn page!

Edit: Well, I solved my problem... turns out that the venerable Image.getScaledInstance(image, Image.SCALE_SMOOTH) did the trick almost perfectly. Slightly lower-quality transform than Photoshop, but what do I expect?

Now I just have to convert from RGB to CMYK! ColorModels, ColorMaps, and ColorSpaces are hurting my brain... I'm trying to figure out how to use them. I have a perfectly good algorithm, I just don't have the actual code implementation clear!

Cell 1250 :: alanmacdougall.com :: Illustrator tips

[This message has been edited by Perfect Thunder (edited 12-17-2003).]

« BackwardsOnwards »

Show Forum Drop Down Menu