From: <fg...@us...> - 2009-04-23 08:03:33
|
Revision: 1160 http://openutils.svn.sourceforge.net/openutils/?rev=1160&view=rev Author: fgiust Date: 2009-04-23 08:03:25 +0000 (Thu, 23 Apr 2009) Log Message: ----------- minor improvements - see changelog Modified Paths: -------------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java trunk/openutils-mgnlmedia/src/site/changes/changes.xml trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtilsTest.java Added Paths: ----------- trunk/openutils-mgnlmedia/src/test/resources/images/openmind.png 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-04-18 08:59:21 UTC (rev 1159) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2009-04-23 08:03:25 UTC (rev 1160) @@ -130,31 +130,57 @@ */ public static BufferedImage resizeImage(BufferedImage original, int x, int y) { + return resizeImage(original, x, y, x, y, null); + } + + /** + * Resize an image to x,y + * @param original original image + * @param x new width + * @param y new height + * @param canvasX canvas width + * @param canvasY canvas height + * @return resized image + */ + public static BufferedImage resizeImage(BufferedImage original, int x, int y, int canvasX, int canvasY, + Color background) + { BufferedImage resizedImage; try { - resizedImage = new BufferedImage(x, y, getType(original.getColorModel())); + resizedImage = new BufferedImage(canvasX, canvasY, getType(original.getColorModel())); } catch (NegativeArraySizeException e) { throw new RuntimeException("NegativeArraySizeException caught when resizing image to [" - + x + + canvasX + ", " - + y + + canvasY + "]"); } Graphics2D graphics2D = resizedImage.createGraphics(); + + // background color + if (background != null) + { + graphics2D.setBackground(background); + } graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + if (canvasX > x || canvasY > y) + { + graphics2D.clearRect(0, 0, canvasX, canvasY); + } if (x > original.getWidth()) { graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); - graphics2D.drawImage(original, 0, 0, x, y, null); + graphics2D.drawImage(original, (canvasX - x) / 2, (canvasY - y) / 2, x, y, background, null); } else { - graphics2D.drawImage(original.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, x, y, null); + Image scaledInstance = original.getScaledInstance(x, y, Image.SCALE_SMOOTH); + graphics2D.drawImage(scaledInstance, (canvasX - x) / 2, (canvasY - y) / 2, x, y, background, null); } return resizedImage; @@ -198,16 +224,21 @@ */ public static BufferedImage fitIn(BufferedImage original, int x, int y) { - return resizeInOut(original, x, y, false); + return resizeInOut(original, x, y, false, null); } public static BufferedImage resizeNoCrop(BufferedImage original, int x, int y) { - return resizeInOut(original, x, y, true); + return resizeInOut(original, x, y, true, null); } - private static BufferedImage resizeInOut(BufferedImage original, int x, int y, boolean external) + public static BufferedImage resizeNoCrop(BufferedImage original, int x, int y, Color background) { + return resizeInOut(original, x, y, true, background); + } + + private static BufferedImage resizeInOut(BufferedImage original, int x, int y, boolean external, Color background) + { if (original == null) { throw new IllegalArgumentException("input image is null"); @@ -223,11 +254,11 @@ double xRatio = (double) x / oX; double yRatio = (double) y / oY; - double ratio = (external ? Math.min(xRatio, yRatio) : Math.min(xRatio, yRatio)); + double ratio = Math.min(xRatio, yRatio); int newX = (int) Math.round(oX * ratio); int newY = (int) Math.round(oY * ratio); - return resizeImage(original, newX, newY); + return resizeImage(original, newX, newY, external ? x : newX, external ? y : newY, background); } /** @@ -596,12 +627,29 @@ { throw new IllegalArgumentException("input image is null"); } + if (resolution == null || resolution.length() < 1) + { + throw new IllegalArgumentException("Invalid resolution: " + resolution); + } BufferedImage img = null; + String params = null; - if (resolution.startsWith("<")) + resolution = StringUtils.lowerCase(resolution); + + if (StringUtils.contains(resolution, ";")) { - String resx = StringUtils.substringBefore(StringUtils.substringAfter(resolution, "<"), "x"); + params = StringUtils.substringAfter(resolution, ";"); + resolution = StringUtils.substringBefore(resolution, ";"); + } + + String controlChar = StringUtils.substring(resolution, 0, 1); + + if ("<".equals(controlChar) || "l".equals(controlChar)) + { + String resx = resolution.startsWith("<") ? StringUtils.substringBefore(StringUtils.substringAfter( + resolution, + "<"), "x") : StringUtils.substringBefore(StringUtils.substringAfter(resolution, "l"), "x"); String resy = StringUtils.substringAfter(resolution, "x"); img = fitIn(original, NumberUtils.toInt(resx), NumberUtils.toInt(resy)); @@ -611,7 +659,22 @@ String resx = StringUtils.substringBefore(StringUtils.substringAfter(resolution, "o"), "x"); String resy = StringUtils.substringAfter(resolution, "x"); - img = resizeNoCrop(original, NumberUtils.toInt(resx), NumberUtils.toInt(resy)); + Color background = null; + + if (StringUtils.contains(params, "background=")) + { + String colorHex = "0x" + StringUtils.trim(StringUtils.substringAfter(params, "background=")); + try + { + background = Color.decode(colorHex); + } + catch (NumberFormatException e) + { + log.error("Invalid color code: " + colorHex, e); + } + } + + img = resizeNoCrop(original, NumberUtils.toInt(resx), NumberUtils.toInt(resy), background); } else { Modified: trunk/openutils-mgnlmedia/src/site/changes/changes.xml =================================================================== --- trunk/openutils-mgnlmedia/src/site/changes/changes.xml 2009-04-18 08:59:21 UTC (rev 1159) +++ trunk/openutils-mgnlmedia/src/site/changes/changes.xml 2009-04-23 08:03:25 UTC (rev 1160) @@ -8,6 +8,11 @@ <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author> </properties> <body> + <release version="4.0-b4" date="2009-04-23" description=""> + <action type="add" dev="fgiust">Support for resize without crop to a fixed size with borders. Background color can + be specified with the syntax: "o100x100;background=FF0096"</action> + <action type="add" dev="fgiust">Alternative prefix for "fit in" resize. In previous versions you had to use < (not very handy in html), now you can use "L" (lower, case insensitive), e.g. "L100x100"</action> + </release> <release version="4.0-b3" date="2009-04-16" description=""> <action type="update" dev="fgiust">Stable release for Magnolia 4.x</action> </release> Modified: trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtilsTest.java =================================================================== --- trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtilsTest.java 2009-04-18 08:59:21 UTC (rev 1159) +++ trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtilsTest.java 2009-04-23 08:03:25 UTC (rev 1160) @@ -45,6 +45,29 @@ } @Test + public void testBorders() throws Exception + { + + NodeData cmyk = new FileNodeData("/images/openmind.png"); + BufferedImage bufferedImage = ImageUtils.createBufferedImage(cmyk); + Assert.assertNotNull(bufferedImage); + + bufferedImage = ImageUtils.getImageForResolution(bufferedImage, "O300x300;background=FF0096"); + + InputStream is = ImageUtils.getStream(bufferedImage, "png"); + + File tempFile = File.createTempFile("image", ".png"); + OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile)); + IOUtils.copy(is, os); + + IOUtils.closeQuietly(is); + IOUtils.closeQuietly(os); + + tempFile.delete(); + + } + + @Test public void testBadImageTxt() throws Exception { Added: trunk/openutils-mgnlmedia/src/test/resources/images/openmind.png =================================================================== (Binary files differ) Property changes on: trunk/openutils-mgnlmedia/src/test/resources/images/openmind.png ___________________________________________________________________ Added: svn:mime-type + image/png This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |