From: <mol...@us...> - 2009-10-14 14:19:43
|
Revision: 1486 http://openutils.svn.sourceforge.net/openutils/?rev=1486&view=rev Author: molaschi Date: 2009-10-14 14:19:37 +0000 (Wed, 14 Oct 2009) Log Message: ----------- MEDIA-26 fix to make round corners antialiased Modified Paths: -------------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2009-10-14 14:18:03 UTC (rev 1485) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2009-10-14 14:19:37 UTC (rev 1486) @@ -32,13 +32,13 @@ import info.magnolia.cms.util.ExclusiveWrite; import info.magnolia.context.MgnlContext; +import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Point; import java.awt.RenderingHints; import java.awt.Transparency; -import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.io.BufferedOutputStream; @@ -293,36 +293,70 @@ public static BufferedImage addRoundedCorners(BufferedImage original, Color backgroundColor, int radius) { - BufferedImage resizedImage; + int originalImageType = getType(original.getColorModel()); + int roundedCornersImageType = BufferedImage.TYPE_4BYTE_ABGR; + + if (originalImageType != BufferedImage.TYPE_4BYTE_ABGR) + { + if (originalImageType != BufferedImage.TYPE_4BYTE_ABGR_PRE) + { + // the image has not alpha channel so fill background + if (backgroundColor == null) + { + backgroundColor = Color.WHITE; + } + } + else + { + roundedCornersImageType = BufferedImage.TYPE_4BYTE_ABGR_PRE; + } + } + + // use a 4 byte image type to create antialiased rounded corners + BufferedImage roundedImage; try { - resizedImage = new BufferedImage(original.getWidth(), original.getHeight(), getType(original - .getColorModel())); + roundedImage = new BufferedImage(original.getWidth(), original.getHeight(), roundedCornersImageType); } catch (NegativeArraySizeException e) { - throw new RuntimeException("NegativeArraySizeException caught when resizing image]"); + throw new RuntimeException("NegativeArraySizeException caught when adding rounded corners"); } - Graphics2D graphics2D = resizedImage.createGraphics(); - graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + Graphics2D roundedGraphics2D = roundedImage.createGraphics(); + roundedGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + roundedGraphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); - if (original.getColorModel().getTransparency() == Transparency.OPAQUE && backgroundColor == null) - { - backgroundColor = Color.WHITE; - } + roundedGraphics2D.setColor(Color.WHITE); + roundedGraphics2D.fillRoundRect(0, 0, original.getWidth(), original.getHeight(), radius, radius); + roundedGraphics2D.setComposite(AlphaComposite.SrcIn); + roundedGraphics2D.drawImage(original, 0, 0, original.getWidth(), original.getHeight(), null); + if (backgroundColor != null) { - graphics2D.setBackground(backgroundColor); + + BufferedImage destImage; + try + { + destImage = new BufferedImage(original.getWidth(), original.getHeight(), originalImageType); + } + catch (NegativeArraySizeException e) + { + throw new RuntimeException("NegativeArraySizeException caught when resizing image]"); + } + // draw new image + Graphics2D destImageGraphics2D = destImage.createGraphics(); + destImageGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + destImageGraphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + + destImageGraphics2D.setBackground(backgroundColor); + destImageGraphics2D.clearRect(0, 0, original.getWidth(), original.getHeight()); + // destImageGraphics2D.setComposite(AlphaComposite.DstIn); + destImageGraphics2D.drawImage(roundedImage, 0, 0, original.getWidth(), original.getHeight(), null); + return destImage; } - graphics2D.clearRect(0, 0, original.getWidth(), original.getHeight()); - graphics2D - .setClip(new RoundRectangle2D.Double(0, 0, original.getWidth(), original.getHeight(), radius, radius)); - graphics2D.drawImage(original, 0, 0, original.getWidth(), original.getHeight(), null); - - return resizedImage; + return roundedImage; } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |