PiscesTileGenerator.java revision 0
0N/A/*
0N/A * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Apackage sun.java2d.pisces;
0N/A
0N/Aimport sun.java2d.pipe.AATileGenerator;
0N/A
0N/Apublic class PiscesTileGenerator implements AATileGenerator {
0N/A public static final int TILE_SIZE = 32;
0N/A
0N/A PiscesCache cache;
0N/A int x, y;
0N/A int maxalpha;
0N/A byte alphaMap[];
0N/A
0N/A public PiscesTileGenerator(PiscesCache cache, int maxalpha) {
0N/A this.cache = cache;
0N/A this.x = cache.bboxX0;
0N/A this.y = cache.bboxY0;
0N/A this.alphaMap = getAlphaMap(maxalpha);
0N/A this.maxalpha = maxalpha;
0N/A }
0N/A
0N/A static int prevMaxAlpha;
0N/A static byte prevAlphaMap[];
0N/A
0N/A public synchronized static byte[] getAlphaMap(int maxalpha) {
0N/A if (maxalpha != prevMaxAlpha) {
0N/A prevAlphaMap = new byte[maxalpha+300];
0N/A int halfmaxalpha = maxalpha>>2;
0N/A for (int i = 0; i <= maxalpha; i++) {
0N/A prevAlphaMap[i] = (byte) ((i * 255 + halfmaxalpha) / maxalpha);
0N/A }
0N/A for (int i = maxalpha; i < prevAlphaMap.length; i++) {
0N/A prevAlphaMap[i] = (byte) 255;
0N/A }
0N/A prevMaxAlpha = maxalpha;
0N/A }
0N/A return prevAlphaMap;
0N/A }
0N/A
0N/A public void getBbox(int bbox[]) {
0N/A bbox[0] = cache.bboxX0;
0N/A bbox[1] = cache.bboxY0;
0N/A bbox[2] = cache.bboxX1;
0N/A bbox[3] = cache.bboxY1;
0N/A //System.out.println("bbox["+bbox[0]+", "+bbox[1]+" => "+bbox[2]+", "+bbox[3]+"]");
0N/A }
0N/A
0N/A /**
0N/A * Gets the width of the tiles that the generator batches output into.
0N/A * @return the width of the standard alpha tile
0N/A */
0N/A public int getTileWidth() {
0N/A return TILE_SIZE;
0N/A }
0N/A
0N/A /**
0N/A * Gets the height of the tiles that the generator batches output into.
0N/A * @return the height of the standard alpha tile
0N/A */
0N/A public int getTileHeight() {
0N/A return TILE_SIZE;
0N/A }
0N/A
0N/A /**
0N/A * Gets the typical alpha value that will characterize the current
0N/A * tile.
0N/A * The answer may be 0x00 to indicate that the current tile has
0N/A * no coverage in any of its pixels, or it may be 0xff to indicate
0N/A * that the current tile is completely covered by the path, or any
0N/A * other value to indicate non-trivial coverage cases.
0N/A * @return 0x00 for no coverage, 0xff for total coverage, or any other
0N/A * value for partial coverage of the tile
0N/A */
0N/A public int getTypicalAlpha() {
0N/A if (true) return 0x80;
0N/A // Decode run-length encoded alpha mask data
0N/A // The data for row j begins at cache.rowOffsetsRLE[j]
0N/A // and is encoded as a set of 2-byte pairs (val, runLen)
0N/A // terminated by a (0, 0) pair.
0N/A
0N/A int x0 = this.x;
0N/A int x1 = x0 + TILE_SIZE;
0N/A int y0 = this.y;
0N/A int y1 = y0 + TILE_SIZE;
0N/A if (x1 > cache.bboxX1) x1 = cache.bboxX1;
0N/A if (y1 > cache.bboxY1) y1 = cache.bboxY1;
0N/A y0 -= cache.bboxY0;
0N/A y1 -= cache.bboxY0;
0N/A
0N/A int ret = -1;
0N/A for (int cy = y0; cy < y1; cy++) {
0N/A int pos = cache.rowOffsetsRLE[cy];
0N/A int cx = cache.minTouched[cy];
0N/A
0N/A if (cx > x0) {
0N/A if (ret > 0) return 0x80;
0N/A ret = 0x00;
0N/A }
0N/A while (cx < x1) {
0N/A int runLen = cache.rowAARLE[pos + 1] & 0xff;
0N/A if (runLen == 0) {
0N/A if (ret > 0) return 0x80;
0N/A ret = 0x00;
0N/A break;
0N/A }
0N/A cx += runLen;
0N/A if (cx > x0) {
0N/A int val = cache.rowAARLE[pos] & 0xff;
0N/A if (ret != val) {
0N/A if (ret < 0) {
0N/A if (val != 0x00 && val != maxalpha) return 0x80;
0N/A ret = val;
0N/A } else {
0N/A return 0x80;
0N/A }
0N/A }
0N/A }
0N/A pos += 2;
0N/A }
0N/A }
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * Skips the current tile and moves on to the next tile.
0N/A * Either this method, or the getAlpha() method should be called
0N/A * once per tile, but not both.
0N/A */
0N/A public void nextTile() {
0N/A if ((x += TILE_SIZE) >= cache.bboxX1) {
0N/A x = cache.bboxX0;
0N/A y += TILE_SIZE;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the alpha coverage values for the current tile.
0N/A * Either this method, or the nextTile() method should be called
0N/A * once per tile, but not both.
0N/A */
0N/A public void getAlpha(byte tile[], int offset, int rowstride) {
0N/A // Decode run-length encoded alpha mask data
0N/A // The data for row j begins at cache.rowOffsetsRLE[j]
0N/A // and is encoded as a set of 2-byte pairs (val, runLen)
0N/A // terminated by a (0, 0) pair.
0N/A
0N/A int x0 = this.x;
0N/A int x1 = x0 + TILE_SIZE;
0N/A int y0 = this.y;
0N/A int y1 = y0 + TILE_SIZE;
0N/A if (x1 > cache.bboxX1) x1 = cache.bboxX1;
0N/A if (y1 > cache.bboxY1) y1 = cache.bboxY1;
0N/A y0 -= cache.bboxY0;
0N/A y1 -= cache.bboxY0;
0N/A
0N/A int idx = offset;
0N/A for (int cy = y0; cy < y1; cy++) {
0N/A int pos = cache.rowOffsetsRLE[cy];
0N/A int cx = cache.minTouched[cy];
0N/A if (cx > x1) cx = x1;
0N/A
0N/A if (cx > x0) {
0N/A //System.out.println("L["+(cx-x0)+"]");
0N/A for (int i = x0; i < cx; i++) {
0N/A tile[idx++] = 0x00;
0N/A }
0N/A }
0N/A while (cx < x1) {
0N/A byte val;
0N/A int runLen = 0;
0N/A try {
0N/A val = alphaMap[cache.rowAARLE[pos] & 0xff];
0N/A runLen = cache.rowAARLE[pos + 1] & 0xff;
0N/A } catch (RuntimeException e0) {
0N/A System.out.println("maxalpha = "+maxalpha);
0N/A System.out.println("tile["+x0+", "+y0+
0N/A " => "+x1+", "+y1+"]");
0N/A System.out.println("cx = "+cx+", cy = "+cy);
0N/A System.out.println("idx = "+idx+", pos = "+pos);
0N/A System.out.println("len = "+runLen);
0N/A cache.print(System.out);
0N/A e0.printStackTrace();
0N/A System.exit(1);
0N/A return;
0N/A }
0N/A if (runLen == 0) {
0N/A break;
0N/A }
0N/A int rx0 = cx;
0N/A cx += runLen;
0N/A int rx1 = cx;
0N/A if (rx0 < x0) rx0 = x0;
0N/A if (rx1 > x1) rx1 = x1;
0N/A runLen = rx1 - rx0;
0N/A //System.out.println("M["+runLen+"]");
0N/A while (--runLen >= 0) {
0N/A try {
0N/A tile[idx++] = val;
0N/A } catch (RuntimeException e) {
0N/A System.out.println("maxalpha = "+maxalpha);
0N/A System.out.println("tile["+x0+", "+y0+
0N/A " => "+x1+", "+y1+"]");
0N/A System.out.println("cx = "+cx+", cy = "+cy);
0N/A System.out.println("idx = "+idx+", pos = "+pos);
0N/A System.out.println("rx0 = "+rx0+", rx1 = "+rx1);
0N/A System.out.println("len = "+runLen);
0N/A cache.print(System.out);
0N/A e.printStackTrace();
0N/A System.exit(1);
0N/A return;
0N/A }
0N/A }
0N/A pos += 2;
0N/A }
0N/A if (cx < x0) { cx = x0; }
0N/A while (cx < x1) {
0N/A tile[idx++] = 0x00;
0N/A cx++;
0N/A }
0N/A /*
0N/A for (int i = idx - (x1-x0); i < idx; i++) {
0N/A System.out.print(hex(tile[i], 2));
0N/A }
0N/A System.out.println();
0N/A */
0N/A idx += (rowstride - (x1-x0));
0N/A }
0N/A nextTile();
0N/A }
0N/A
0N/A static String hex(int v, int d) {
0N/A String s = Integer.toHexString(v);
0N/A while (s.length() < d) {
0N/A s = "0"+s;
0N/A }
0N/A return s.substring(0, d);
0N/A }
0N/A
0N/A /**
0N/A * Disposes this tile generator.
0N/A * No further calls will be made on this instance.
0N/A */
0N/A public void dispose() {}
0N/A}