pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1672207 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image: PDImage.java PDImageXObject.java
Date Wed, 08 Apr 2015 22:36:16 GMT
Author: jahewson
Date: Wed Apr  8 22:36:16 2015
New Revision: 1672207

URL: http://svn.apache.org/r1672207
Log:
PDFBOX-2750: Perform high-quality upscaling of image masks

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImage.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImage.java?rev=1672207&r1=1672206&r2=1672207&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImage.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImage.java
Wed Apr  8 22:36:16 2015
@@ -34,6 +34,7 @@ public interface PDImage extends COSObje
 {
     /**
      * Returns the content of this image as an AWT buffered image with an (A)RGB color space.
+     * The size of the returned image is the larger of the size of the image itself or its
mask. 
      * @return content of this image as a buffered image.
      * @throws IOException
      */

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java?rev=1672207&r1=1672206&r2=1672207&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
Wed Apr  8 22:36:16 2015
@@ -16,6 +16,7 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.image;
 
+import java.awt.RenderingHints;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSName;
@@ -244,19 +245,20 @@ public final class PDImageXObject extend
         int width = image.getWidth();
         int height = image.getHeight();
 
-        // compose to ARGB
-        BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
-
-        // scale mask to fit image
-        if (mask.getWidth() != width || mask.getHeight() != height)
+        // scale mask to fit image, or image to fit mask, whichever is larger
+        if (mask.getWidth() < width || mask.getHeight() < height)
         {
-            BufferedImage mask2 = new BufferedImage(width, height, mask.getType());
-            Graphics2D g = mask2.createGraphics();
-            g.drawImage(mask, 0, 0, width, height, 0, 0, mask.getWidth(), mask.getHeight(),
null);
-            g.dispose();
-            mask = mask2;
+            mask = scaleImage(mask, width, height);
+        }
+        else if (mask.getWidth() > width || mask.getHeight() > height)
+        {
+            width = mask.getWidth();
+            height = mask.getHeight();
+            image = scaleImage(image, width, height);
         }
 
+        // compose to ARGB
+        BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
         WritableRaster src = image.getRaster();
         WritableRaster dest = masked.getRaster();
         WritableRaster alpha = mask.getRaster();
@@ -292,6 +294,22 @@ public final class PDImageXObject extend
     }
 
     /**
+     * High-quality image scaling.
+     */
+    private BufferedImage scaleImage(BufferedImage image, int width, int height)
+    {
+        BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+        Graphics2D g = image2.createGraphics();
+        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+                           RenderingHints.VALUE_INTERPOLATION_BICUBIC);
+        g.setRenderingHint(RenderingHints.KEY_RENDERING,
+                           RenderingHints.VALUE_RENDER_QUALITY);
+        g.drawImage(image, 0, 0, width, height, 0, 0, image.getWidth(), image.getHeight(),
null);
+        g.dispose();
+        return image2;
+    }
+
+    /**
      * Returns the Mask Image XObject associated with this image, or null if there is none.
      * @return Mask Image XObject
      */



Mime
View raw message