summaryrefslogtreecommitdiff
path: root/gnu/java/awt
diff options
context:
space:
mode:
authorRoman Kennke <roman@kennke.org>2007-05-24 16:26:55 +0000
committerRoman Kennke <roman@kennke.org>2007-05-24 16:26:55 +0000
commit38c26644fa4b5b32e6a4d049c04e814df4b08dbb (patch)
tree87d56ad9aeb3341a2a090c0046b19a39fc9db19b /gnu/java/awt
parentd9de25f544b3a7e567aaf108f814ff1a41601682 (diff)
downloadclasspath-38c26644fa4b5b32e6a4d049c04e814df4b08dbb.tar.gz
2007-05-24 Roman Kennke <roman@kennke.org>
* gnu/java/awt/java2d/ScanlineCoverage.java (Iterator): New class. (Range): New class. (Coverage.covDelta): Made field package private. (Coverage.xPos): Made field package private. (iterator): New field. Stores the iterator that is reused. (ScanlineCoverage): Initialize iterator. (assertion): Removed. (isEmpty): Refined conditions. (iterate): Return Iterator instance. (next): Removed. This is done by the Iterator class now. (test): Removed. * gnu/java/awt/java2d/ScanlineConverter.java (main): Removed. * gnu/java/awt/peer/x/XGraphics2D.java (renderScanline): Adjust to new coverage iterator stuff. * gnu/java/awt/java2d/AbstractGraphics2D.java (renderScanline): Adjust to new coverage iterator stuff.
Diffstat (limited to 'gnu/java/awt')
-rw-r--r--gnu/java/awt/java2d/AbstractGraphics2D.java17
-rw-r--r--gnu/java/awt/java2d/ScanlineConverter.java10
-rw-r--r--gnu/java/awt/java2d/ScanlineCoverage.java344
-rw-r--r--gnu/java/awt/peer/x/XGraphics2D.java22
4 files changed, 220 insertions, 173 deletions
diff --git a/gnu/java/awt/java2d/AbstractGraphics2D.java b/gnu/java/awt/java2d/AbstractGraphics2D.java
index 3924698de..663ca2ee9 100644
--- a/gnu/java/awt/java2d/AbstractGraphics2D.java
+++ b/gnu/java/awt/java2d/AbstractGraphics2D.java
@@ -1561,7 +1561,6 @@ public abstract class AbstractGraphics2D
Object v = renderingHints.get(RenderingHints.KEY_ANTIALIASING);
antialias = (v == RenderingHints.VALUE_ANTIALIAS_ON);
}
-
ScanlineConverter sc = getScanlineConverter();
int resolution = 0;
if (antialias)
@@ -1739,25 +1738,21 @@ public abstract class AbstractGraphics2D
WritableRaster writeRaster = Raster.createWritableRaster(sm, db, loc);
WritableRaster alphaRaster = cm.getAlphaRaster(writeRaster);
int pixel;
- ScanlineCoverage.Coverage start = c.iterate();
- ScanlineCoverage.Coverage end = c.next();
- assert (start != null);
- assert (end != null);
- do
+ ScanlineCoverage.Iterator iter = c.iterate();
+ while (iter.hasNext())
{
- coverageAlpha = coverageAlpha + (start.getCoverageDelta() / maxCoverage);
+ ScanlineCoverage.Range range = iter.next();
+ coverageAlpha = range.getCoverage() / maxCoverage;
if (coverageAlpha < 1.0)
{
- for (int x = start.getXPos(); x < end.getXPos(); x++)
+ for (int x = range.getXPos(); x < range.getXPosEnd(); x++)
{
pixel = alphaRaster.getSample(x, y, 0);
pixel = (int) (pixel * coverageAlpha);
alphaRaster.setSample(x, y, 0, pixel);
}
}
- start = end;
- end = c.next();
- } while (end != null);
+ }
ColorModel paintColorModel = pCtx.getColorModel();
CompositeContext cCtx = composite.createContext(paintColorModel,
getColorModel(),
diff --git a/gnu/java/awt/java2d/ScanlineConverter.java b/gnu/java/awt/java2d/ScanlineConverter.java
index 71f70d3e9..13f21efcb 100644
--- a/gnu/java/awt/java2d/ScanlineConverter.java
+++ b/gnu/java/awt/java2d/ScanlineConverter.java
@@ -424,14 +424,4 @@ public final class ScanlineConverter
edgePoolLast = edgePoolLast.poolNext;
}
}
-
- /**
- * Performs some tests.
- *
- * @param args
- */
- public static void main(String[] args)
- {
- ScanlineCoverage.test();
- }
}
diff --git a/gnu/java/awt/java2d/ScanlineCoverage.java b/gnu/java/awt/java2d/ScanlineCoverage.java
index 4f5e7c070..2bed5422c 100644
--- a/gnu/java/awt/java2d/ScanlineCoverage.java
+++ b/gnu/java/awt/java2d/ScanlineCoverage.java
@@ -48,19 +48,200 @@ public final class ScanlineCoverage
{
/**
+ * Iterates over the coverage list and calculates the actual coverage
+ * ranges on a scanline.
+ */
+ public final class Iterator
+ {
+ /**
+ * This instance is reused in the iteration.
+ */
+ private Range range;
+
+ /**
+ * The pointer to the current item in the iteration.
+ */
+ private Coverage currentItem;
+
+ /**
+ * The current coverage value.
+ */
+ private int currentCoverage;
+
+ /**
+ * Creates a new CoverageIterator.
+ */
+ Iterator()
+ {
+ range = new Range();
+ }
+
+ /**
+ * Returns the next coverage range on the scanline. The returned object
+ * will always be the same object, but with different values. Keep that
+ * in mind when dealing with this object.
+ *
+ * @return the next coverage range on the scanline
+ */
+ public Range next()
+ {
+ currentCoverage += currentItem.covDelta;
+ range.setCoverage(currentCoverage);
+ range.setXPos(currentItem.xPos);
+ currentItem = currentItem.next;
+ range.setLength(currentItem.xPos - range.xPos);
+ return range;
+ }
+
+ /**
+ * Returns {@ true} when there are more coverage ranges to iterate,
+ * {@ false} otherwise.
+ *
+ * @return {@ true} when there are more coverage ranges to iterate,
+ * {@ false} otherwise
+ */
+ public boolean hasNext()
+ {
+ boolean hasNext;
+ if (currentItem == null || currentItem.next == null
+ || currentItem.next == last)
+ hasNext = false;
+ else
+ hasNext = true;
+ return hasNext;
+ }
+
+ /**
+ * Resets this iterator to the start of the list.
+ */
+ void reset()
+ {
+ currentItem = head;
+ currentCoverage = 0;
+ }
+ }
+
+ /**
+ * A data object that carries information about pixel coverage on a scanline.
+ * The data consists of a starting X position on the scanline, the
+ * length of the range in pixels and the actual coverage value.
+ยด */
+ public static final class Range
+ {
+ /**
+ * The X position on the scanline, in pixels.
+ */
+ private int xPos;
+
+ /**
+ * The length of the range, in pixels.
+ */
+ private int length;
+
+ /**
+ * The actual coverage. The relation depends on
+ * {@link ScanlineCoverage#maxCoverage}.
+ */
+ private int coverage;
+
+ /**
+ * Creates a new CoverageRange object.
+ */
+ Range()
+ {
+ // Nothing to do. The values get initialized in the corresponding
+ // setters.
+ }
+
+ /**
+ * Sets the X start position (left) on the scanline. This value is
+ * considered to be in pixels and device space.
+ *
+ * @param x the x position
+ */
+ void setXPos(int x)
+ {
+ xPos = x;
+ }
+
+ /**
+ * Returns the X start position (left) on the scanline. This value
+ * is considered to be in pixels and device space.
+ *
+ * @return the X position on the scanline
+ */
+ public int getXPos()
+ {
+ return xPos;
+ }
+
+ /**
+ * Sets the length of the pixel range. This is in pixel units.
+ *
+ * @param l the length of the range
+ */
+ void setLength(int l)
+ {
+ length = l;
+ }
+
+ /**
+ * Returns the length of the range in pixel units.
+ *
+ * @return the length of the range in pixel units
+ */
+ public int getLength()
+ {
+ return length;
+ }
+
+ /**
+ * Returns the first X position after the range.
+ *
+ * @return the first X position after the range
+ */
+ public int getXPosEnd()
+ {
+ return xPos + length;
+ }
+
+ /**
+ * Sets the coverage of the pixel range. The relation of that value
+ * depends on {@link ScanlineCoverage#maxCoverage}.
+ *
+ * @param cov the coverage value for the pixel range
+ */
+ void setCoverage(int cov)
+ {
+ coverage = cov;
+ }
+
+ /**
+ * Returns the coverage of the pixel range. The relation of this value
+ * depends on {@link ScanlineCoverage#getMaxCoverage()}.
+ *
+ * @return the coverage of the pixel range
+ */
+ public int getCoverage()
+ {
+ return coverage;
+ }
+ }
+
+ /**
* One bucket in the list.
*/
- public static final class Coverage
+ private static final class Coverage
{
/**
* The X coordinate on the scanline to which this bucket belongs.
*/
- private int xPos;
+ int xPos;
/**
* The X coverage delta.
*/
- private int covDelta;
+ int covDelta;
/**
* Implements a linked list. This points to the next element of the list.
@@ -158,6 +339,19 @@ public final class ScanlineCoverage
private int maxCoverage;
/**
+ * The iterator over the ranges of this scanline.
+ */
+ private Iterator iterator;
+
+ /**
+ * Creates a new ScanlineCoverage instance.
+ */
+ public ScanlineCoverage()
+ {
+ iterator = new Iterator();
+ }
+
+ /**
* Indicates the the next scan of the scanline begins and that the next
* request will be at the beginning of this list. This makes searching and
* sorting of this list very quick.
@@ -359,36 +553,15 @@ public final class ScanlineCoverage
}
/**
- * (Re-)Starts iterating the coverage entries by returning the first Coverage
- * item in the list. Pixels left to that entry have a zero coverage.
+ * (Re-)Starts iterating the coverage values for the scanline.
+ * Use the returned iterator to get the consecutive coverage ranges.
*
- * @return the first coverage item
+ * @return the iterator
*/
- public Coverage iterate()
+ public Iterator iterate()
{
- if (head == last)
- current = null;
- else
- current = head;
- currentPrev = null;
- return current;
- }
-
- /**
- * Returns the next coverage item in the list, according to the current
- * iteration state.
- *
- * @return the next coverage item in the list or {@ null} if the end has
- * been reached
- */
- public Coverage next()
- {
- currentPrev = current;
- if (current == null || current.next == last)
- current = null;
- else
- current = current.next;
- return current;
+ iterator.reset();
+ return iterator;
}
/**
@@ -400,115 +573,8 @@ public final class ScanlineCoverage
*/
public boolean isEmpty()
{
- return head == null || head == last;
- }
-
- /**
- * Performs some tests to check if this class is working correctly.
- * There are comments about a bunch of test points in this method, which
- * correspond to other points in this class as commented.
- */
- static void test()
- {
- System.out.println("ScanlineCoverage test 1");
- // Test testpoint 1 & 2.
- ScanlineCoverage c = new ScanlineCoverage();
- c.add(2, 3);
- c.add(3, 4);
- c.add(4, 5);
-
- Coverage cov = c.iterate();
- assertion(cov.xPos == 2);
- assertion(cov.covDelta == 3);
- cov = c.next();
- assertion(cov.xPos == 3);
- assertion(cov.covDelta == 4);
- cov = c.next();
- assertion(cov.xPos == 4);
- assertion(cov.covDelta == 5);
- assertion(c.next() == null);
-
- System.out.println("ScanlineCoverage test 2");
- // Test testpoint 3 and 4.
- c.clear();
- c.add(5, 4);
- c.add(7, 5);
- c.add(7, 10);
- cov = c.iterate();
- assertion(cov.xPos == 5);
- assertion(cov.covDelta == 4);
- cov = c.next();
- assertion(cov.xPos == 7);
- assertion(cov.covDelta == 15);
- assertion(c.next() == null);
-
- System.out.println("ScanlineCoverage test 3");
- // Test testpoint 5.
- c.rewind();
- c.add(6, 20);
- cov = c.iterate();
- assertion(cov.xPos == 5);
- assertion(cov.covDelta == 4);
- cov = c.next();
- assertion(cov.xPos == 6);
- assertion(cov.covDelta == 20);
- cov = c.next();
- assertion(cov.xPos == 7);
- assertion(cov.covDelta == 15);
- assertion(c.next() == null);
-
- System.out.println("ScanlineCoverage test 4");
- // Test testpoint 6.
- c.rewind();
- c.add(8, 21);
- cov = c.iterate();
- assertion(cov.xPos == 5);
- assertion(cov.covDelta == 4);
- cov = c.next();
- assertion(cov.xPos == 6);
- assertion(cov.covDelta == 20);
- cov = c.next();
- assertion(cov.xPos == 7);
- assertion(cov.covDelta == 15);
- cov = c.next();
- assertion(cov.xPos == 8);
- assertion(cov.covDelta == 21);
- assertion(c.next() == null);
-
- System.out.println("ScanlineCoverage test 5");
- // Test testpoint 6.
- c.rewind();
- c.add(2, 100);
- cov = c.iterate();
- assertion(cov.xPos == 2);
- assertion(cov.covDelta == 100);
- cov = c.next();
- assertion(cov.xPos == 5);
- assertion(cov.covDelta == 4);
- cov = c.next();
- assertion(cov.xPos == 6);
- assertion(cov.covDelta == 20);
- cov = c.next();
- assertion(cov.xPos == 7);
- assertion(cov.covDelta == 15);
- cov = c.next();
- assertion(cov.xPos == 8);
- assertion(cov.covDelta == 21);
- assertion(c.next() == null);
-
+ return head == null || head == last
+ || head.next == null || head.next == last;
}
- /**
- * Checks a condition and throws and AssertionError if this condition
- * fails.
- *
- * @param b the condition
- */
- private static void assertion(boolean b)
- {
- if (! b)
- {
- throw new AssertionError();
- }
- }
}
diff --git a/gnu/java/awt/peer/x/XGraphics2D.java b/gnu/java/awt/peer/x/XGraphics2D.java
index b42dec528..77f4760bf 100644
--- a/gnu/java/awt/peer/x/XGraphics2D.java
+++ b/gnu/java/awt/peer/x/XGraphics2D.java
@@ -219,20 +219,18 @@ public class XGraphics2D
public void renderScanline(int y, ScanlineCoverage c)
{
- ScanlineCoverage.Coverage start = c.iterate();
- ScanlineCoverage.Coverage end = c.next();
- assert (start != null);
- assert (end != null);
- int coverageAlpha = 0;
+ ScanlineCoverage.Iterator iter = c.iterate();
+ float coverageAlpha = 0;
int maxCoverage = c.getMaxCoverage();
Color old = getColor();
Color col = getColor();
if (col == null)
col = Color.BLACK;
- do
+ while (iter.hasNext())
{
+ ScanlineCoverage.Range range = iter.next();
// TODO: Dumb implementation for testing.
- coverageAlpha = coverageAlpha + start.getCoverageDelta();
+ coverageAlpha = range.getCoverage();
if (coverageAlpha > 0)
{
int red = col.getRed();
@@ -246,13 +244,11 @@ public class XGraphics2D
blue = 255 - (int) ((255 - blue) * alpha);
}
xgc.set_foreground(red << 16 | green << 8 | blue);
- int x0 = start.getXPos();
- int x1 = end.getXPos();
- xdrawable.fill_rectangle(xgc, x0, y, x1 - x0, 1);
+ int x0 = range.getXPos();
+ int l = range.getLength();
+ xdrawable.fill_rectangle(xgc, x0, y, l, 1);
}
- start = end;
- end = c.next();
- } while (end != null);
+ }
xgc.set_foreground(old.getRGB());
}