pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1646922 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading: GouraudShadingContext.java PatchMeshesShadingContext.java TriangleBasedShadingContext.java
Date Sat, 20 Dec 2014 07:13:41 GMT
Author: tilman
Date: Sat Dec 20 07:13:41 2014
New Revision: 1646922

URL: http://svn.apache.org/r1646922
Log:
PDFBOX-2576: DRY refactoring of double code

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java?rev=1646922&r1=1646921&r2=1646922&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java
Sat Dec 20 07:13:41 2014
@@ -16,14 +16,11 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.shading;
 
-import java.awt.PaintContext;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Point2D;
 import java.awt.image.ColorModel;
-import java.awt.image.Raster;
-import java.awt.image.WritableRaster;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -43,7 +40,7 @@ import org.apache.pdfbox.util.Matrix;
  * @author Tilman Hausherr
  * @author Shaola Ren
  */
-abstract class GouraudShadingContext extends TriangleBasedShadingContext implements PaintContext
+abstract class GouraudShadingContext extends TriangleBasedShadingContext
 {
     private static final Log LOG = LogFactory.getLog(GouraudShadingContext.class);
 
@@ -53,14 +50,6 @@ abstract class GouraudShadingContext ext
     protected List<ShadedTriangle> triangleList;
 
     /**
-     * background values.
-     */
-    protected float[] background;
-    protected int rgbBackground;
-
-    protected Map<Point, Integer> pixelTable;
-
-    /**
      * Constructor creates an instance to be used for fill operations.
      *
      * @param shading the shading type to be used
@@ -130,14 +119,7 @@ abstract class GouraudShadingContext ext
     public void dispose()
     {
         triangleList = null;
-        outputColorModel = null;
-        shadingColorSpace = null;
-    }
-
-    @Override
-    public final ColorModel getColorModel()
-    {
-        return outputColorModel;
+        super.dispose();
     }
 
     /**
@@ -154,61 +136,8 @@ abstract class GouraudShadingContext ext
         return dstMin + (src * (dstMax - dstMin) / srcMax);
     }
 
-    @Override
-    public final Raster getRaster(int x, int y, int w, int h)
+    protected boolean emptyList()
     {
-        WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
-        int[] data = new int[w * h * 4];
-        if (!triangleList.isEmpty() || background != null)
-        {
-            for (int row = 0; row < h; row++)
-            {
-                int currentY = y + row;
-                if (bboxRect != null)
-                {
-                    if (currentY < minBBoxY || currentY > maxBBoxY)
-                    {
-                        continue;
-                    }
-                }
-                for (int col = 0; col < w; col++)
-                {
-                    int currentX = x + col;
-                    if (bboxRect != null)
-                    {
-                        if (currentX < minBBoxX || currentX > maxBBoxX)
-                        {
-                            continue;
-                        }
-                    }
-                    Point p = new Point(currentX, currentY);
-                    int value;
-                    if (pixelTable.containsKey(p))
-                    {
-                        value = pixelTable.get(p);
-                    }
-                    else
-                    {
-                        if (background != null)
-                        {
-                            value = rgbBackground;
-                        }
-                        else
-                        {
-                            continue;
-                        }
-                    }
-                    int index = (row * w + col) * 4;
-                    data[index] = value & 255;
-                    value >>= 8;
-                    data[index + 1] = value & 255;
-                    value >>= 8;
-                    data[index + 2] = value & 255;
-                    data[index + 3] = 255;
-                }
-            }
-        }
-        raster.setPixels(0, 0, w, h, data);
-        return raster;
+        return triangleList.isEmpty();
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java?rev=1646922&r1=1646921&r2=1646922&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java
Sat Dec 20 07:13:41 2014
@@ -15,14 +15,11 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.shading;
 
-import java.awt.PaintContext;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Point2D;
 import java.awt.image.ColorModel;
-import java.awt.image.Raster;
-import java.awt.image.WritableRaster;
 import java.io.EOFException;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -45,18 +42,15 @@ import org.apache.pdfbox.util.Matrix;
  *
  * @author Shaola Ren
  */
-abstract class PatchMeshesShadingContext extends TriangleBasedShadingContext implements PaintContext
+abstract class PatchMeshesShadingContext extends TriangleBasedShadingContext
 {
     private static final Log LOG = LogFactory.getLog(PatchMeshesShadingContext.class);
 
-    protected float[] background; // background values.
-    protected int rgbBackground;
     protected final PDShading patchMeshesShadingType;
 
     // the following fields are not intialized in this abstract class
     protected List<Patch> patchList; // patch list
     protected int bitsPerFlag; // bits per flag
-    protected Map<Point, Integer> pixelTable;
 
     /**
      * Constructor creates an instance to be used for fill operations.
@@ -282,71 +276,11 @@ abstract class PatchMeshesShadingContext
     public void dispose()
     {
         patchList = null;
-        outputColorModel = null;
-        shadingColorSpace = null;
+        super.dispose();
     }
 
-    @Override
-    public final ColorModel getColorModel()
+    protected boolean emptyList()
     {
-        return outputColorModel;
-    }
-
-    @Override
-    public final Raster getRaster(int x, int y, int w, int h)
-    {
-        WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
-        int[] data = new int[w * h * 4];
-        if (!patchList.isEmpty() || background != null)
-        {
-            for (int row = 0; row < h; row++)
-            {
-                int currentY = y + row;
-                if (bboxRect != null)
-                {
-                    if (currentY < minBBoxY || currentY > maxBBoxY)
-                    {
-                        continue;
-                    }
-                }
-                for (int col = 0; col < w; col++)
-                {
-                    int currentX = x + col;
-                    if (bboxRect != null)
-                    {
-                        if (currentX < minBBoxX || currentX > maxBBoxX)
-                        {
-                            continue;
-                        }
-                    }
-                    Point p = new Point(x + col, y + row);
-                    int value;
-                    if (pixelTable.containsKey(p))
-                    {
-                        value = pixelTable.get(p);
-                    }
-                    else
-                    {
-                        if (background != null)
-                        {
-                            value = rgbBackground;
-                        }
-                        else
-                        {
-                            continue;
-                        }
-                    }
-                    int index = (row * w + col) * 4;
-                    data[index] = value & 255;
-                    value >>= 8;
-                    data[index + 1] = value & 255;
-                    value >>= 8;
-                    data[index + 2] = value & 255;
-                    data[index + 3] = 255;
-                }
-            }
-        }
-        raster.setPixels(0, 0, w, h, data);
-        return raster;
-    }
+        return patchList.isEmpty();
+    }    
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java?rev=1646922&r1=1646921&r2=1646922&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java
Sat Dec 20 07:13:41 2014
@@ -15,11 +15,14 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.shading;
 
+import java.awt.PaintContext;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Point2D;
 import java.awt.image.ColorModel;
+import java.awt.image.Raster;
+import java.awt.image.WritableRaster;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
@@ -34,7 +37,7 @@ import org.apache.pdfbox.util.Matrix;
  * @author Shaola Ren
  * @author Tilman Hausherr
  */
-abstract class TriangleBasedShadingContext extends ShadingContext
+abstract class TriangleBasedShadingContext extends ShadingContext implements PaintContext
 {
     private static final Log LOG = LogFactory.getLog(TriangleBasedShadingContext.class);
 
@@ -53,8 +56,16 @@ abstract class TriangleBasedShadingConte
      */
     protected int numberOfColorComponents;
 
+    /**
+     * background values.
+     */
+    protected float[] background;
+    protected int rgbBackground;
+
     final protected boolean hasFunction;
 
+    protected Map<Point, Integer> pixelTable;
+
     public TriangleBasedShadingContext(PDShading shading, ColorModel cm,
             AffineTransform xform, Matrix ctm, Rectangle dBounds)
             throws IOException
@@ -137,4 +148,78 @@ abstract class TriangleBasedShadingConte
         return super.convertToRGB(values);
     }
 
+    // true if the relevant list is empty
+    abstract boolean emptyList();
+    
+    @Override
+    public final ColorModel getColorModel()
+    {
+        return outputColorModel;
+    }
+
+    @Override
+    public void dispose()
+    {
+        outputColorModel = null;
+        shadingColorSpace = null;
+    }
+
+    @Override
+    public final Raster getRaster(int x, int y, int w, int h)
+    {
+        WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
+        int[] data = new int[w * h * 4];
+        if (!emptyList() || background != null)
+        {
+            for (int row = 0; row < h; row++)
+            {
+                int currentY = y + row;
+                if (bboxRect != null)
+                {
+                    if (currentY < minBBoxY || currentY > maxBBoxY)
+                    {
+                        continue;
+                    }
+                }
+                for (int col = 0; col < w; col++)
+                {
+                    int currentX = x + col;
+                    if (bboxRect != null)
+                    {
+                        if (currentX < minBBoxX || currentX > maxBBoxX)
+                        {
+                            continue;
+                        }
+                    }
+                    Point p = new Point(currentX, currentY);
+                    int value;
+                    if (pixelTable.containsKey(p))
+                    {
+                        value = pixelTable.get(p);
+                    }
+                    else
+                    {
+                        if (background != null)
+                        {
+                            value = rgbBackground;
+                        }
+                        else
+                        {
+                            continue;
+                        }
+                    }
+                    int index = (row * w + col) * 4;
+                    data[index] = value & 255;
+                    value >>= 8;
+                    data[index + 1] = value & 255;
+                    value >>= 8;
+                    data[index + 2] = value & 255;
+                    data[index + 3] = 255;
+                }
+            }
+        }
+        raster.setPixels(0, 0, w, h, data);
+        return raster;
+    }
+
 }



Mime
View raw message