Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / gnu / java / awt / java2d / PixelCoverage.java
blobc83ad1fff8f4870797fc85c6c42010e8e535b60d
1 package gnu.java.awt.java2d;
3 /**
4 * Stores and handles the pixel converage for a scanline. The pixel coverage
5 * is stored as sorted list of buckets, each of which holds information about
6 * the coverage for the X and Y axis. This is utilized to compute the actual
7 * coverage for each pixel on the scanline and finding chunks of pixels with
8 * equal coverage.
9 */
10 final class PixelCoverage
13 /**
14 * One bucket in the list.
16 private static final class Bucket
18 /**
19 * The X coordinate on the scanline to which this bucket belongs.
21 int xPos;
23 /**
24 * The X coverage.
26 int xCov;
28 /**
29 * The Y coverage.
31 int yCov;
33 /**
34 * Implements a linked list. This points to the next element of the list.
36 Bucket next;
38 /**
39 * Implements a linked list. This points to the previous element of the
40 * list.
42 Bucket prev;
45 /**
46 * The head of the sorted list of buckets.
48 private Bucket head;
50 /**
51 * The current bucket. We make use of the fact that the scanline converter
52 * always scans the scanline (and thus this list) from left to right to
53 * quickly find buckets or insertion points.
55 private Bucket current;
57 /**
58 * The bucket after the last valid bucket. Unused buckets are not thrown
59 * away and garbage collected. Instead, we keep them at the tail of the list
60 * and reuse them when necessary.
62 private Bucket last;
64 /**
65 * Indicates the the next scan of the scanline begins and that the next
66 * request will be at the beginning of this list. This makes searching and
67 * sorting of this list very quick.
69 void rewind()
71 current = head;
74 /**
75 * Clears the list. This does not throw away the old buckets but only
76 * resets the end-pointer of the list to the first element. All buckets are
77 * then unused and are reused when the list is filled again.
79 void clear()
81 last = head;
84 /**
85 * This adds the specified x and y coverage to the pixel at the specified
86 * X position.
88 * @param x the X position
89 * @param xc the x coverage
90 * @param yc the y coverage
92 void add(int x, int xc, int yc)
94 Bucket bucket = findOrInsert(x);
95 bucket.xCov += xc;
96 bucket.yCov += yc;
99 /**
100 * Finds the bucket in the list with the specified X coordinate.
101 * If no such bucket is found, then a new one is fetched (either a cached
102 * bucket from the end of the list or a newly allocated one) inserted at the
103 * correct position and returned.
105 * @param x the X coordinate
107 * @return a bucket to hold the coverage data
109 private Bucket findOrInsert(int x)
111 // First search for a matching bucket.
112 if (head == null)
114 // Special case: the list is still empty.
115 head = new Bucket();
116 current = head;
117 return head;
120 // This performs a linear search, starting from the current bucket.
121 // This is reasonably efficient because access to this list is always done
122 // in a linear fashion and we are not more then 1 or 2 buckets away from
123 // the one we're looking for.
124 Bucket match = current;
125 while (match != null && match.xPos != x)
130 return match;