views:

654

answers:

4

What is the best way of rotating a bufferedimage about its center where the gradient is 1 degree?

I know there is AffineTransform, but that causes shearing and weird stretching or black spots that should be blank.

edit The sizes of images I am dealing with are icon sizes, so typically 24x24 up to 48x48 pixels

+3  A: 

The quality of rotation for such a small angle will vary greatly with the size of the image. How big is your image?

[After the OP edited the question to indicate the size of the image]

IMO the image is too small for any meaningful rotation other than in multiples of 90 degrees (assuming its a square). I am afraid this needs to be done manually by a graphic designer to get the best possible quality.

SDX2000
generally between 24x24 pixels to 48x48 pixels, it is a very small image
yx
yeah, anything this small even a 1 degree change is never 1 degree. its either 0 or 45, isn't it? a pixel either moves or it doesn't.
John Gardner
Huh? Of course it's possible to rotate a small image, it just means that some pixels will move while some won't. Consider a line going from (0,0) to (639,0) compared to one going from (0,0) to (639,1), and so on.
unwind
639? I hope those are not pixels! If they are then I think you missed the point.
SDX2000
+2  A: 

Have you tried setting the anti-aliasing of your graphics context?

g2d.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON
);
jedierikb
A: 

Here are some links which explain how you might want to proceed when applying transforms to icons.

Java 2D Trickery: Antialiased Image Transforms http://weblogs.java.net/blog/campbell/archive/2007/03/java_2d_tricker_1.html

Sub pixel sampling of Raster or DataBuffer in BufferedImage. http://forums.java.net/jive/thread.jspa?messageID=204921&tstart=0

jedierikb
+1  A: 

As a quick and dirty fix, have you considered the following method:

  • scale up the image by a factor of eg 8, by drawing it onto a new bufferedimage
  • rotate the image, by drawing it transformed by an affinetransform
  • scale it back down again, by drawing it onto yet another new bufferedimage

Any low-level artefacts should vanish during the scale-down. This isn't perhaps the fastest option, but it may well do what you want with a minimum of fuss - and more complex solutions may likely boil down to doing the same thing behind the scenes.

Zarkonnen