Skip to content

Commit

Permalink
PDFBOX-5852: replace Integer with int, add some minor optimizations
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1920894 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lehmi committed Sep 25, 2024
1 parent dc5e372 commit a1b9c99
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.awt.image.ColorModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.pdfbox.util.Matrix;
Expand Down Expand Up @@ -59,9 +60,14 @@ final void setTriangleList(List<ShadedTriangle> triangleList)
}

@Override
protected Integer[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException
protected int[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException
{
Integer[][] array = new Integer[deviceBounds.width + 1][deviceBounds.height + 1];
int[][] array = new int[deviceBounds.width + 1][deviceBounds.height + 1];
int initialValue = getBackground() != null ? getRgbBackground() : -1;
for (int i = 0; i < deviceBounds.width + 1; i++)
{
Arrays.fill(array[i], initialValue);
}
calcPixelTable(triangleList, array, deviceBounds);
return array;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.awt.geom.AffineTransform;
import java.awt.image.ColorModel;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.pdfbox.util.Matrix;
Expand Down Expand Up @@ -57,9 +58,14 @@ protected PatchMeshesShadingContext(PDMeshBasedShadingType shading, ColorModel c
}

@Override
protected Integer[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException
protected int[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException
{
Integer[][] array = new Integer[deviceBounds.width][deviceBounds.height];
int[][] array = new int[deviceBounds.width][deviceBounds.height];
int initialValue = getBackground() != null ? getRgbBackground() : -1;
for (int i = 0; i < deviceBounds.width + 1; i++)
{
Arrays.fill(array[i], initialValue);
}
for (Patch it : patchList)
{
calcPixelTable(it.listOfTriangles, array, deviceBounds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
abstract class TriangleBasedShadingContext extends ShadingContext
{
// array of pixels within triangles to their RGB color
private Integer[][] pixelTableArray;
private int[][] pixelTableArray;

// offset to be used for the array index
private int xOffset = 0;
Expand Down Expand Up @@ -72,24 +72,20 @@ protected final void createPixelTable(Rectangle deviceBounds) throws IOException
*
* @return an array which contains all the points' positions and colors of one image
*/
abstract Integer[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException;
abstract int[][] calcPixelTableArray(Rectangle deviceBounds) throws IOException;

/**
* Get the points from the triangles, calculate their color and add point-color mappings.
*/
protected void calcPixelTable(List<ShadedTriangle> triangleList, Integer[][] array,
protected void calcPixelTable(List<ShadedTriangle> triangleList, int[][] array,
Rectangle deviceBounds) throws IOException
{
for (ShadedTriangle tri : triangleList)
{
int degree = tri.getDeg();
if (degree == 2)
{
Line line = tri.getLine();
for (Point p : line.linePoints)
{
addValueToArray(p, evalFunctionAndConvertToRGB(line.calcColor(p)), array);
}
addLinePoints(tri.getLine(), array);
}
else
{
Expand All @@ -111,7 +107,6 @@ protected void calcPixelTable(List<ShadedTriangle> triangleList, Integer[][] arr
}
}
}

// "fatten" triangle by drawing the borders with Bresenham's line algorithm
// Inspiration: Raph Levien in http://bugs.ghostscript.com/show_bug.cgi?id=219588
Point p0 = new Point((int) Math.round(tri.corner[0].getX()),
Expand All @@ -120,26 +115,22 @@ protected void calcPixelTable(List<ShadedTriangle> triangleList, Integer[][] arr
(int) Math.round(tri.corner[1].getY()));
Point p2 = new Point((int) Math.round(tri.corner[2].getX()),
(int) Math.round(tri.corner[2].getY()));
Line l1 = new Line(p0, p1, tri.color[0], tri.color[1]);
Line l2 = new Line(p1, p2, tri.color[1], tri.color[2]);
Line l3 = new Line(p2, p0, tri.color[2], tri.color[0]);
for (Point p : l1.linePoints)
{
addValueToArray(p, evalFunctionAndConvertToRGB(l1.calcColor(p)), array);
}
for (Point p : l2.linePoints)
{
addValueToArray(p, evalFunctionAndConvertToRGB(l2.calcColor(p)), array);
}
for (Point p : l3.linePoints)
{
addValueToArray(p, evalFunctionAndConvertToRGB(l3.calcColor(p)), array);
}
addLinePoints(new Line(p0, p1, tri.color[0], tri.color[1]), array);
addLinePoints(new Line(p1, p2, tri.color[1], tri.color[2]), array);
addLinePoints(new Line(p2, p0, tri.color[2], tri.color[0]), array);
}
}
}

private void addValueToArray(Point p, int value, Integer[][] array)
private void addLinePoints(Line line, int[][] array) throws IOException
{
for (Point p : line.linePoints)
{
addValueToArray(p, evalFunctionAndConvertToRGB(line.calcColor(p)), array);
}
}

private void addValueToArray(Point p, int value, int[][] array)
{
int xIndex = p.x + xOffset;
int yIndex = p.y + yOffset;
Expand All @@ -150,14 +141,14 @@ private void addValueToArray(Point p, int value, Integer[][] array)
array[xIndex][yIndex] = value;
}

private Integer getValueFromArray(int x, int y)
private int 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 -1;
}
return pixelTableArray[xIndex][yIndex];
}
Expand Down Expand Up @@ -191,27 +182,17 @@ public final Raster getRaster(int x, int y, int w, int h)
{
for (int col = 0; col < w; col++)
{
int value;
Integer v = getValueFromArray(x + col, y + row);
if (v != null)
int value = getValueFromArray(x + col, y + row);
if (value >= 0)
{
value = v;
}
else
{
if (getBackground() == null)
{
continue;
}
value = getRgbBackground();
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;
}
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;
}
}
}
Expand Down

0 comments on commit a1b9c99

Please sign in to comment.