svn commit: r811707 - in /ofbiz/trunk: applications/product/src/org/ofbiz/product/image/ScaleImage.java applications/product/webapp/catalog/product/EditProductContent.ftl framework/common/src/org/ofbiz/common/image/ImageTransform.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r811707 - in /ofbiz/trunk: applications/product/src/org/ofbiz/product/image/ScaleImage.java applications/product/webapp/catalog/product/EditProductContent.ftl framework/common/src/org/ofbiz/common/image/ImageTransform.java

jleroux@apache.org
Author: jleroux
Date: Sat Sep  5 20:29:43 2009
New Revision: 811707

URL: http://svn.apache.org/viewvc?rev=811707&view=rev
Log:
A patch from Eric DE MAULDE "Image scaling quality" (https://issues.apache.org/jira/browse/OFBIZ-2552) - OFBIZ-2552

Modified:
    ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java
    ofbiz/trunk/applications/product/webapp/catalog/product/EditProductContent.ftl
    ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java?rev=811707&r1=811706&r2=811707&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java Sat Sep  5 20:29:43 2009
@@ -18,11 +18,9 @@
  *******************************************************************************/
 package org.ofbiz.product.image;
 
-import java.awt.RenderingHints;
-import java.awt.geom.AffineTransform;
-import java.awt.image.AffineTransformOp;
 import java.awt.image.BufferedImage;
 import java.awt.image.ImagingOpException;
+import java.awt.image.RenderedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
@@ -79,8 +77,7 @@
         int index;
         Map<String, Map<String, String>> imgPropertyMap = FastMap.newInstance();
         BufferedImage bufImg, bufNewImg;
-        double imgHeight, imgWidth, scaleFactor;
-        AffineTransformOp op;
+        double imgHeight, imgWidth;
         Map<String, String> imgUrlMap = FastMap.newInstance();
         Map<String, Object> resultXMLMap = FastMap.newInstance();
         Map<String, Object> resultBufImgMap = FastMap.newInstance();
@@ -161,35 +158,6 @@
 
                 if (resultScaleImgMap.containsKey("responseMessage") && resultScaleImgMap.get("responseMessage").equals("success")) {
                     bufNewImg = (BufferedImage) resultScaleImgMap.get("bufferedImage");
-                    Double scaleFactorDb = (Double) resultScaleImgMap.get("scaleFactor");
-                    scaleFactor = scaleFactorDb.doubleValue();
-
-                    // define Interpolation
-                    Map<RenderingHints.Key, Object> rhMap = FastMap.newInstance();
-                        rhMap.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
-                        rhMap.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-                        rhMap.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
-                        rhMap.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
-                        rhMap.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
-                        rhMap.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
-                        rhMap.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
-                        rhMap.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
-                        rhMap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
-                    RenderingHints rh = new RenderingHints(rhMap);
-
-                    /* IMAGE TRANFORMATION */
-                    AffineTransform tx = new AffineTransform();
-                    tx.scale(scaleFactor, scaleFactor);
-
-
-                    try {
-                        op = new AffineTransformOp(tx, rh);
-                    } catch (ImagingOpException e) {
-                        String errMsg = UtilProperties.getMessage(resource, "ScaleImage.transform_is_non_invertible", locale)  + e.toString();
-                        Debug.logError(errMsg, module);
-                        result.put("errorMessage", errMsg);
-                        return result;
-                    }
 
                     // write the New Scaled Image
                     String newFileLocation = null;
@@ -216,7 +184,7 @@
 
                     // write new image
                     try {
-                        ImageIO.write(op.filter(bufImg, bufNewImg), imgExtension, new File(imageServerPath + "/" + newFilePathPrefix + filenameToUse));
+                        ImageIO.write((RenderedImage) bufNewImg, imgExtension, new File(imageServerPath + "/" + newFilePathPrefix + filenameToUse));
                     } catch (IllegalArgumentException e) {
                         String errMsg = UtilProperties.getMessage(resource, "ScaleImage.one_parameter_is_null", locale) + e.toString();
                         Debug.logError(errMsg, module);

Modified: ofbiz/trunk/applications/product/webapp/catalog/product/EditProductContent.ftl
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/webapp/catalog/product/EditProductContent.ftl?rev=811707&r1=811706&r2=811707&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/webapp/catalog/product/EditProductContent.ftl (original)
+++ ofbiz/trunk/applications/product/webapp/catalog/product/EditProductContent.ftl Sat Sep  5 20:29:43 2009
@@ -160,7 +160,6 @@
         <td>&nbsp;</td>
         <td width="80%" colspan="4" valign="top">
         <input type="text" name="originalImageUrl" value="${(product.originalImageUrl)?default('')}" size="60" maxlength="255">
-        <br/><span class="tooltip">${uiLabelMap.ProductOriginalImageMessage} : {ofbiz.home}/applications/product/config/ImageProperties.xml&quot;</span>
         <#if productId?has_content>
             <div>
             <span>${uiLabelMap.ProductInsertDefaultImageUrl}: </span>
@@ -201,5 +200,6 @@
                 </td>
             </tr>
         </table>
+        <span class="tooltip">${uiLabelMap.ProductOriginalImageMessage} : {ofbiz.home}/applications/product/config/ImageProperties.xml&quot;</span>
     </form>
-</#if>
\ No newline at end of file
+</#if>

Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java?rev=811707&r1=811706&r2=811707&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java (original)
+++ ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java Sat Sep  5 20:29:43 2009
@@ -18,6 +18,8 @@
  *******************************************************************************/
 package org.ofbiz.common.image;
 
+import java.awt.Graphics2D;
+import java.awt.Image;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.FileInputStream;
@@ -27,6 +29,7 @@
 import java.util.Map;
 
 import javax.imageio.ImageIO;
+import javax.swing.ImageIcon;
 import javax.xml.parsers.ParserConfigurationException;
 
 import javolution.util.FastMap;
@@ -167,7 +170,10 @@
             bufImgType = bufImg.getType();
         }
 
-        bufNewImg = new BufferedImage((int) (imgWidth * scaleFactor), (int) (imgHeight * scaleFactor), bufImgType);
+        // scale original image with new size
+        Image newImg = bufImg.getScaledInstance((int) (imgWidth * scaleFactor), (int) (imgHeight * scaleFactor), Image.SCALE_SMOOTH);
+        
+        bufNewImg = ImageTransform.toBufferedImage(newImg, bufImgType);
 
         result.put("responseMessage", "success");
         result.put("bufferedImage", bufNewImg);
@@ -245,4 +251,38 @@
         return result;
 
     }
+    
+    /**
+     * toBufferedImage
+     * <p>
+     * Transform from an Image instance to a BufferedImage instance
+     *
+     * @param image             Source image
+     * @return BufferedImage
+     */
+    public static BufferedImage toBufferedImage(Image image) {
+        return ImageTransform.toBufferedImage(image, BufferedImage.TYPE_INT_ARGB_PRE);
+    }
+    
+    public static BufferedImage toBufferedImage(Image image, int bufImgType) {
+        /** Check if the image isn't already a BufferedImage instance */
+        if( image instanceof BufferedImage ) {
+                return( (BufferedImage)image );
+        } else {
+                /** Full image loading */
+                image = new ImageIcon(image).getImage();
+                
+                /** new BufferedImage creation */
+                BufferedImage bufferedImage = new BufferedImage(
+                            image.getWidth(null),
+                            image.getHeight(null),
+                            bufImgType);
+                
+                Graphics2D g = bufferedImage.createGraphics();
+                g.drawImage(image,0,0,null);
+                g.dispose();
+                
+                return( bufferedImage );
+        }
+    }
 }