cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jheym...@apache.org
Subject svn commit: r279650 - /cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java
Date Thu, 08 Sep 2005 21:49:14 GMT
Author: jheymans
Date: Thu Sep  8 14:49:10 2005
New Revision: 279650

URL: http://svn.apache.org/viewcvs?rev=279650&view=rev
Log:
Fix bug #36326, thanks to Eric Caron <ericATplauditdesignDOTcom>

Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java?rev=279650&r1=279649&r2=279650&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java Thu Sep
 8 14:49:10 2005
@@ -31,12 +31,14 @@
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.environment.SourceResolver;
+import org.apache.cocoon.reading.ResourceReader;
 import org.apache.commons.lang.SystemUtils;
 import org.xml.sax.SAXException;
 
 import com.sun.image.codec.jpeg.ImageFormatException;
 import com.sun.image.codec.jpeg.JPEGCodec;
 import com.sun.image.codec.jpeg.JPEGDecodeParam;
+import com.sun.image.codec.jpeg.JPEGEncodeParam;
 import com.sun.image.codec.jpeg.JPEGImageDecoder;
 import com.sun.image.codec.jpeg.JPEGImageEncoder;
 
@@ -52,13 +54,13 @@
  *     <dd> This parameter is optional. When specified, it determines the
  *          width of the binary image.
  *          If no height parameter is specified, the aspect ratio
- *          of the image is kept.
+ *          of the image is kept. The parameter may be expressed as an int or a percentage.
  *     </dd>
  *     <dt>&lt;height&gt;</dt>
  *     <dd> This parameter is optional. When specified, it determines the
  *          height of the binary image.
  *          If no width parameter is specified, the aspect ratio
- *          of the image is kept.
+ *          of the image is kept. The parameter may be expressed as an int or a percentage.
  *     </dd>
  *     <dt>&lt;scale(Red|Green|Blue)&gt;</dt>
  *     <dd>This parameter is optional. When specified it will cause the
@@ -82,11 +84,17 @@
  *         images will be reduced in size, but not enlarged. The default is
  *         "<code>true</code>".
  *     </dd>
+ *     <dt>&lt;quality&gt;</dt>
+ *     <dd>This parameter is optional. By default, the quality uses the
+ *         default for the JVM. If it is specified, the proper JPEG quality
+ *         compression is used. The range is 0.0 to 1.0, if specified.
+ *     </dd>
  *   </dl>
  *
  * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
  * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
  * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
+ * @author <a href="mailto:eric@plauditdesign.com">Eric Caron</a>
  * @version CVS $Id$
  */
 final public class ImageReader extends ResourceReader {
@@ -101,9 +109,11 @@
     private int height;
     private float[] scaleColor = new float[3];
     private float[] offsetColor = new float[3];
+    private float[] quality = new float[1];
 
     private boolean enlarge;
     private boolean fitUniform;
+    private boolean usePercent;
     private RescaleOp colorFilter;
     private ColorConvertOp grayscaleFilter;
 
@@ -111,8 +121,9 @@
     public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
     throws ProcessingException, SAXException, IOException {
 
-        width = par.getParameterAsInteger("width", 0);
-        height = par.getParameterAsInteger("height", 0);
+        char lastChar;
+        String tmpWidth = par.getParameter("width", "0");
+        String tmpHeight = par.getParameter("height", "0");
 
         scaleColor[0] = par.getParameterAsFloat("scaleRed", -1.0f);
         scaleColor[1] = par.getParameterAsFloat("scaleGreen", -1.0f);
@@ -120,6 +131,7 @@
         offsetColor[0] = par.getParameterAsFloat("offsetRed", 0.0f);
         offsetColor[1] = par.getParameterAsFloat("offsetGreen", 0.0f);
         offsetColor[2] = par.getParameterAsFloat("offsetBlue", 0.0f);
+        quality[0] = par.getParameterAsFloat("quality", 0.9f);
 
         boolean filterColor = false;
         for (int i = 0; i < 3; ++i) {
@@ -137,6 +149,23 @@
             this.colorFilter = new RescaleOp(scaleColor, offsetColor, null);
         }
 
+        usePercent = false;
+        lastChar = tmpWidth.charAt(tmpWidth.length() - 1);
+        if (lastChar == '%') {
+            usePercent = true;
+            width = Integer.parseInt(tmpWidth.substring(0, tmpWidth.length() - 1));
+        } else {
+            width = Integer.parseInt(tmpWidth);
+        }
+
+        lastChar = tmpHeight.charAt(tmpHeight.length() - 1);
+        if(lastChar == '%') {
+            usePercent = true;
+            height = Integer.parseInt(tmpHeight.substring(0, tmpHeight.length() - 1));
+        } else {
+            height = Integer.parseInt(tmpHeight);
+        }
+		
         if (par.getParameterAsBoolean("grayscale", GRAYSCALE_DEFAULT)) {
             this.grayscaleFilter = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),
null);
         }
@@ -160,7 +189,7 @@
      * @return True if image transform is specified
      */
     private boolean hasTransform() {
-        return width > 0 || height > 0 || null != colorFilter || null != grayscaleFilter;
+        return width > 0 || height > 0 || null != colorFilter || null != grayscaleFilter
|| (quality[0] != 0.9f);
     }
 
     /**
@@ -258,6 +287,15 @@
                     double ow = decodeParam.getWidth();
                     double oh = decodeParam.getHeight();
 
+                    if (usePercent == true) {
+                        if (width > 0) {
+                            width = Math.round((int)(ow * width) / 100);
+                        }
+                        if (height > 0) {
+                            height = Math.round((int)(oh * height) / 100);
+                        }
+                    }
+
                     AffineTransformOp filter = new AffineTransformOp(getTransform(ow, oh,
width, height), AffineTransformOp.TYPE_BILINEAR);
                     WritableRaster scaledRaster = filter.createCompatibleDestRaster(currentImage.getRaster());
 
@@ -277,10 +315,16 @@
                 // JVM Bug handling
                 if (JVMBugFixed) {
                     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
+                    JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(currentImage);
+                    p.setQuality(quality[0], true);
+                    encoder.setJPEGEncodeParam(p);
                     encoder.encode(currentImage);
                 } else { 
                     ByteArrayOutputStream bstream = new ByteArrayOutputStream();
                     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bstream);
+                    JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(currentImage);
+                    p.setQuality(quality[0], true);
+                    encoder.setJPEGEncodeParam(p);
                     encoder.encode(currentImage);
                     out.write(bstream.toByteArray());
                 }
@@ -321,6 +365,7 @@
                 + ":" + this.offsetColor[0]
                 + ":" + this.offsetColor[1]
                 + ":" + this.offsetColor[2]
+                + ":" + this.quality[0]
                 + ":" + ((null == this.grayscaleFilter) ? "color" : "grayscale")
                 + ":" + super.getKey();
     }



Mime
View raw message