From 1c31c9d189bfd4fb972181e3e33c2f65c54b9fc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Lehmk=C3=BChler?= Date: Mon, 16 Sep 2024 17:50:57 +0000 Subject: [PATCH] PDFBOX-5852: replace Map with a two-dimensional array git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1920718 13f79535-47bb-0310-9956-ffa450edef68 --- .../shading/GouraudShadingContext.java | 11 ++-- .../shading/PatchMeshesShadingContext.java | 11 ++-- .../shading/TriangleBasedShadingContext.java | 66 +++++++++++++------ 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java index 42ea10ed974..f5f6645b7aa 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/GouraudShadingContext.java @@ -16,15 +16,12 @@ */ package org.apache.pdfbox.pdmodel.graphics.shading; -import java.awt.Point; import java.awt.Rectangle; import java.awt.geom.AffineTransform; import java.awt.image.ColorModel; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.apache.pdfbox.util.Matrix; @@ -62,11 +59,11 @@ final void setTriangleList(List triangleList) } @Override - protected Map calcPixelTable(Rectangle deviceBounds) throws IOException + protected Integer[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException { - Map map = new HashMap<>(); - super.calcPixelTable(triangleList, map, deviceBounds); - return map; + Integer[][] array = new Integer[deviceBounds.width + 1][deviceBounds.height + 1]; + calcPixelTable(triangleList, array, deviceBounds); + return array; } @Override diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java index 6906ecd5ffb..0afde146ce1 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PatchMeshesShadingContext.java @@ -15,14 +15,11 @@ */ package org.apache.pdfbox.pdmodel.graphics.shading; -import java.awt.Point; import java.awt.Rectangle; import java.awt.geom.AffineTransform; import java.awt.image.ColorModel; import java.io.IOException; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.apache.pdfbox.util.Matrix; @@ -60,14 +57,14 @@ protected PatchMeshesShadingContext(PDMeshBasedShadingType shading, ColorModel c } @Override - protected Map calcPixelTable(Rectangle deviceBounds) throws IOException + protected Integer[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException { - Map map = new HashMap<>(); + Integer[][] array = new Integer[deviceBounds.width][deviceBounds.height]; for (Patch it : patchList) { - super.calcPixelTable(it.listOfTriangles, map, deviceBounds); + calcPixelTable(it.listOfTriangles, array, deviceBounds); } - return map; + return array; } @Override diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java index 211fbefc149..95bec442bea 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/TriangleBasedShadingContext.java @@ -23,7 +23,6 @@ import java.awt.image.WritableRaster; import java.io.IOException; import java.util.List; -import java.util.Map; import org.apache.pdfbox.util.Matrix; @@ -36,8 +35,12 @@ */ abstract class TriangleBasedShadingContext extends ShadingContext { - // map of pixels within triangles to their RGB color - private Map pixelTable; + // array of pixels within triangles to their RGB color + private Integer[][] pixelTableArray; + + // offset to be used for the array index + private int xOffset = 0; + private int yOffset = 0; /** * Constructor. @@ -59,20 +62,22 @@ abstract class TriangleBasedShadingContext extends ShadingContext */ protected final void createPixelTable(Rectangle deviceBounds) throws IOException { - pixelTable = calcPixelTable(deviceBounds); + xOffset = -deviceBounds.x; + yOffset = -deviceBounds.y; + pixelTableArray = calcPixelTableArray(deviceBounds); } /** - * Calculate every point and its color and store them in a Hash table. + * Calculate every point and its color and store them in a two-dimensional array. * - * @return a Hash table which contains all the points' positions and colors of one image + * @return an array which contains all the points' positions and colors of one image */ - abstract Map calcPixelTable(Rectangle deviceBounds) throws IOException; + abstract Integer[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException; /** * Get the points from the triangles, calculate their color and add point-color mappings. */ - protected void calcPixelTable(List triangleList, Map map, + protected void calcPixelTable(List triangleList, Integer[][] array, Rectangle deviceBounds) throws IOException { for (ShadedTriangle tri : triangleList) @@ -83,7 +88,7 @@ protected void calcPixelTable(List triangleList, Map triangleList, Map= array.length || yIndex >= array[0].length) + { + return; + } + array[xIndex][yIndex] = value; + } + + private Integer getValueFromArray(int x, int y) + { + int xIndex = x + xOffset; + int yIndex = y + yOffset; + if (xIndex < 0 || yIndex < 0 || xIndex >= pixelTableArray.length + || yIndex >= pixelTableArray[0].length) + { + return null; + } + return pixelTableArray[xIndex][yIndex]; + } + /** * Convert color to RGB color value, using function if required, then convert from the shading * color space to an RGB value, which is encoded into an integer. @@ -162,9 +191,8 @@ public final Raster getRaster(int x, int y, int w, int h) { for (int col = 0; col < w; col++) { - Point p = new IntPoint(x + col, y + row); int value; - Integer v = pixelTable.get(p); + Integer v = getValueFromArray(x + col, y + row); if (v != null) { value = v;