cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gkossakow...@apache.org
Subject svn commit: r610567 - in /cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src: changes/ main/java/org/apache/cocoon/reading/imageop/ test/java/org/apache/cocoon/reading/imageop/ test/resources/org/apache/cocoon/reading/imageop/
Date Wed, 09 Jan 2008 21:12:50 GMT
Author: gkossakowski
Date: Wed Jan  9 13:12:34 2008
New Revision: 610567

URL: http://svn.apache.org/viewvc?rev=610567&view=rev
Log:
<action dev="gkossakowski" type="add" issue="COCOON-2133" due-to="Robin Wyles" due-to-email="reb@robinwyles.com">
  Addition of "allow-enlarge" parameter to ImageOp resize operation. Many thanks to Robin
Wyles for providing test-cases covering added functionality.
</action>

Modified:
    cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/changes/changes.xml
    cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/main/java/org/apache/cocoon/reading/imageop/ResizeOperation.java
    cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/java/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.java
    cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/resources/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.xtest

Modified: cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/changes/changes.xml?rev=610567&r1=610566&r2=610567&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/changes/changes.xml (original)
+++ cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/changes/changes.xml Wed Jan
 9 13:12:34 2008
@@ -29,8 +29,11 @@
             Initial creation.
           </action>
           <action dev="ltrieloff" type="add" issue="COCOON-2048">
-            Added OverlayOperation contributed by Alexander Klimetschek
-          </action>      
+            Added OverlayOperation contributed by Alexander Klimetschek.
+          </action>
+          <action dev="gkossakowski" type="add" issue="COCOON-2133" due-to="Robin Wyles"
due-to-email="reb@robinwyles.com">
+            Addition of "allow-enlarge" parameter to ImageOp resize operation. Many thanks
to Robin Wyles for providing test-cases covering added functionality.
+          </action>
         </release>
       </body>
     </document>

Modified: cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/main/java/org/apache/cocoon/reading/imageop/ResizeOperation.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/main/java/org/apache/cocoon/reading/imageop/ResizeOperation.java?rev=610567&r1=610566&r2=610567&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/main/java/org/apache/cocoon/reading/imageop/ResizeOperation.java
(original)
+++ cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/main/java/org/apache/cocoon/reading/imageop/ResizeOperation.java
Wed Jan  9 13:12:34 2008
@@ -31,6 +31,7 @@
     private int     width;
     private boolean preserveRatio;
     private boolean adjustX;
+    private boolean allowEnlarge;
 
     public void setPrefix( String prefix ) {
         this.prefix = prefix;
@@ -49,6 +50,7 @@
         }
         preserveRatio = params.getParameterAsBoolean( prefix + "preserve-ratio", false );
         adjustX = params.getParameterAsBoolean( prefix + "adjust-x", false );
+        allowEnlarge = params.getParameterAsBoolean( prefix + "allow-enlarge", true );
     }
  
     public WritableRaster apply( WritableRaster image ) {
@@ -59,17 +61,36 @@
         double width = image.getWidth();
         double xScale = this.width / width;
         double yScale = this.height / height;
-        if( preserveRatio )
+        if (allowEnlarge || (xScale <= 1 && yScale <= 1))
         {
-            if( adjustX )
-                xScale = yScale;
-            else
-                yScale = xScale;
+	        if( preserveRatio )
+	        {
+	        	if (allowEnlarge) {
+	        		if (xScale >= 1) {
+	        			yScale = xScale;
+	        		} else if (yScale >= 1) {
+	        			xScale = yScale;
+	        		}
+	        	} else {
+	        		if (xScale <= 1) {
+	        			yScale = xScale;
+	        		} else if (yScale <= 1) {
+	        			xScale = yScale;
+	        		}
+	        	}
+	            if( adjustX )
+	                xScale = yScale;
+	            else
+	                yScale = xScale;
+	        }
+	        
+	        AffineTransform scale = AffineTransform.getScaleInstance( xScale, yScale );
+	        AffineTransformOp op = new AffineTransformOp( scale, AffineTransformOp.TYPE_BILINEAR
);
+	        WritableRaster scaledRaster = op.filter( image, null );
+	        return scaledRaster;
+        } else {
+        		return image;
         }
-        AffineTransform scale = AffineTransform.getScaleInstance( xScale, yScale );
-        AffineTransformOp op = new AffineTransformOp( scale, AffineTransformOp.TYPE_BILINEAR
);
-        WritableRaster scaledRaster = op.filter( image, null );
-        return scaledRaster;
     }
 
     public String getKey() {
@@ -77,6 +98,7 @@
                + ( enabled ? "enable" : "disable" )
                + ":" + width
                + ":" + height
-               + ":" + prefix;
+               + ":" + prefix
+               + ":" + ( allowEnlarge ? "allowEnlarge" : "disallowEnlarge" );
     }
 } 

Modified: cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/java/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/java/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.java?rev=610567&r1=610566&r2=610567&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/java/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.java
(original)
+++ cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/java/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.java
Wed Jan  9 13:12:34 2008
@@ -16,6 +16,12 @@
  */
 package org.apache.cocoon.reading.imageop;
 
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+
+import javax.imageio.ImageIO;
+
+import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.cocoon.SitemapComponentTestCase;
 import org.apache.commons.io.IOUtils;
 import org.apache.excalibur.source.Source;
@@ -32,5 +38,91 @@
         byte[] expectedBinaryArray = IOUtils.toByteArray(expectedSource.getInputStream());
         
         assertIdentical(expectedBinaryArray, read("imageop-no-effects", SitemapComponentTestCase.EMPTY_PARAMS,
inputURI));
+    }
+    
+    public void testResizeOperationAllowEnlargeTrue() throws Exception {
+        BufferedImage resizedImage = null;
+        try {
+            String inputURI = imgBase + "4x2.jpg";
+
+            Parameters params = new Parameters();
+            
+            // Both dimensions being enlarged, image should be resized in both dimensions
+            params.setParameter("resize-allow-enlarge", "true");
+            params.setParameter("resize-width", "8");
+            params.setParameter("resize-height", "4");
+            byte[] returnedBinaryArray = read("imageop-resize", params,
+                    inputURI);
+            resizedImage = ImageIO.read(new ByteArrayInputStream(
+                    returnedBinaryArray));
+            assertEquals(8, resizedImage.getWidth());
+            assertEquals(4, resizedImage.getHeight());
+            
+            // One dimension being enlarged, image should be resized in one dimension
+            params.setParameter("resize-height", "2");
+            returnedBinaryArray = read("imageop-resize", params, inputURI);
+            resizedImage = ImageIO.read(new ByteArrayInputStream(
+                    returnedBinaryArray));
+            assertEquals(8, resizedImage.getWidth());
+            assertEquals(2, resizedImage.getHeight());
+            
+            // One dimension being enlarged with preserve-ratio, image should be resized
proportionally
+            params.removeParameter("resize-height");
+            params.setParameter("resize-preserve-ratio", "true");
+            returnedBinaryArray = read("imageop-resize", params, inputURI);
+            resizedImage = ImageIO.read(new ByteArrayInputStream(
+                    returnedBinaryArray));
+            assertEquals(8, resizedImage.getWidth());
+            assertEquals(4, resizedImage.getHeight());
+            
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            if (resizedImage != null)
+                resizedImage.flush();
+        }
+    }
+
+    public void testResizeOperationAllowEnlargeFalse() throws Exception {
+        BufferedImage resizedImage = null;
+        try {
+            String inputURI = imgBase + "4x2.jpg";
+
+            Parameters params = new Parameters();
+            params.setParameter("resize-allow-enlarge", "false");
+            
+            // Both dimensions being enlarged, image should not be resized
+            params.setParameter("resize-width", "8");
+            params.setParameter("resize-height", "4");
+            byte[] returnedBinaryArray = read("imageop-resize", params,
+                    inputURI);
+            resizedImage = ImageIO.read(new ByteArrayInputStream(
+                    returnedBinaryArray));
+            assertEquals(4, resizedImage.getWidth());
+            assertEquals(2, resizedImage.getHeight());
+            
+            // One dimension being enlarged, image should not be resized
+            params.setParameter("resize-height", "2");
+            returnedBinaryArray = read("imageop-resize", params, inputURI);
+            resizedImage = ImageIO.read(new ByteArrayInputStream(
+                    returnedBinaryArray));
+            assertEquals(4, resizedImage.getWidth());
+            assertEquals(2, resizedImage.getHeight());
+            
+            // One dimension being enlarged with preserve-ratio, image should not be resized
+            params.removeParameter("resize-height");
+            params.setParameter("resize-preserve-ratio", "true");
+            returnedBinaryArray = read("imageop-resize", params, inputURI);
+            resizedImage = ImageIO.read(new ByteArrayInputStream(
+                    returnedBinaryArray));
+            assertEquals(4, resizedImage.getWidth());
+            assertEquals(2, resizedImage.getHeight());
+            
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            if (resizedImage != null)
+                resizedImage.flush();
+        }
     }
 }

Modified: cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/resources/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.xtest
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/resources/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.xtest?rev=610567&r1=610566&r2=610567&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/resources/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.xtest
(original)
+++ cocoon/trunk/blocks/cocoon-imageop/cocoon-imageop-impl/src/test/resources/org/apache/cocoon/reading/imageop/ImageOpReaderTestCase.xtest
Wed Jan  9 13:12:34 2008
@@ -26,8 +26,18 @@
  </roles>
 
  <components>
+   <!-- =============== Image Operations ==================== -->
+  <image-operations>
+    <component-instance name="resize" class="org.apache.cocoon.reading.imageop.ResizeOperation"
/>
+  </image-operations>
+  <!-- =============== Readers ==================== -->
   <readers logger="test">
    <component-instance name="imageop-no-effects" class="org.apache.cocoon.reading.imageop.ImageOpReader"/>
+    <component-instance name="imageop-resize" class="org.apache.cocoon.reading.imageop.ImageOpReader">
+      <effects>
+        <op type="resize" prefix="resize-" />
+      </effects>
+    </component-instance>
   </readers>
  </components>
 



Mime
View raw message