001: /*
002: * $RCSfile: RandomIterFallback.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:55:43 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.iterator;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.DataBuffer;
016: import java.awt.image.Raster;
017: import java.awt.image.RenderedImage;
018: import java.awt.image.SampleModel;
019: import javax.media.jai.PlanarImage;
020: import javax.media.jai.iterator.RandomIter;
021:
022: /**
023: * @since EA2
024: */
025: public class RandomIterFallback implements RandomIter {
026:
027: protected RenderedImage im;
028: protected Rectangle boundsRect;
029:
030: protected SampleModel sampleModel;
031:
032: protected int xID;
033: protected int yID;
034: protected int sampleModelTranslateX;
035: protected int sampleModelTranslateY;
036: protected DataBuffer dataBuffer = null;
037:
038: protected int boundsX;
039: protected int boundsY;
040:
041: protected int[] xTiles;
042: protected int[] yTiles;
043:
044: public RandomIterFallback(RenderedImage im, Rectangle bounds) {
045: this .im = im;
046:
047: Rectangle imBounds = new Rectangle(im.getMinX(), im.getMinY(),
048: im.getWidth(), im.getHeight());
049: this .boundsRect = imBounds.intersection(bounds);
050: this .sampleModel = im.getSampleModel();
051:
052: int x = boundsRect.x;
053: int y = boundsRect.y;
054: int width = boundsRect.width;
055: int height = boundsRect.height;
056:
057: this .boundsX = boundsRect.x;
058: this .boundsY = boundsRect.y;
059: this .xTiles = new int[width];
060: this .yTiles = new int[height];
061:
062: int tileWidth = im.getTileWidth();
063: int tileGridXOffset = im.getTileGridXOffset();
064: int minTileX = PlanarImage.XToTileX(x, tileGridXOffset,
065: tileWidth);
066: int offsetX = x
067: - PlanarImage.tileXToX(minTileX, tileGridXOffset,
068: tileWidth);
069: int tileX = minTileX;
070:
071: for (int i = 0; i < width; i++) {
072: xTiles[i] = tileX;
073: ++offsetX;
074: if (offsetX == tileWidth) {
075: ++tileX;
076: offsetX = 0;
077: }
078: }
079:
080: int tileHeight = im.getTileHeight();
081: int tileGridYOffset = im.getTileGridYOffset();
082: int minTileY = PlanarImage.YToTileY(y, tileGridYOffset,
083: tileHeight);
084: int offsetY = y
085: - PlanarImage.tileYToY(minTileY, tileGridYOffset,
086: tileHeight);
087: int tileY = minTileY;
088:
089: for (int i = 0; i < height; i++) {
090: yTiles[i] = tileY;
091: ++offsetY;
092: if (offsetY == tileHeight) {
093: ++tileY;
094: offsetY = 0;
095: }
096: }
097: }
098:
099: /**
100: * Sets dataBuffer to the correct buffer for the pixel
101: * (x, y) = (xLocal + boundsRect.x, yLocal + boundsRect.y).
102: *
103: * @param xLocal the X coordinate in the local coordinate system.
104: * @param yLocal the Y coordinate in the local coordinate system.
105: */
106: private void makeCurrent(int xLocal, int yLocal) {
107: int xIDNew = xTiles[xLocal];
108: int yIDNew = yTiles[yLocal];
109:
110: if ((xIDNew != xID) || (yIDNew != yID) || (dataBuffer == null)) {
111: xID = xIDNew;
112: yID = yIDNew;
113: Raster tile = im.getTile(xID, yID);
114:
115: this .dataBuffer = tile.getDataBuffer();
116: this .sampleModelTranslateX = tile
117: .getSampleModelTranslateX();
118: this .sampleModelTranslateY = tile
119: .getSampleModelTranslateY();
120: }
121: }
122:
123: public int getSample(int x, int y, int b) {
124: makeCurrent(x - boundsX, y - boundsY);
125: return sampleModel.getSample(x - sampleModelTranslateX, y
126: - sampleModelTranslateY, b, dataBuffer);
127: }
128:
129: public float getSampleFloat(int x, int y, int b) {
130: makeCurrent(x - boundsX, y - boundsY);
131: return sampleModel.getSampleFloat(x - sampleModelTranslateX, y
132: - sampleModelTranslateY, b, dataBuffer);
133: }
134:
135: public double getSampleDouble(int x, int y, int b) {
136: makeCurrent(x - boundsX, y - boundsY);
137: return sampleModel.getSampleDouble(x - sampleModelTranslateX, y
138: - sampleModelTranslateY, b, dataBuffer);
139: }
140:
141: public int[] getPixel(int x, int y, int[] iArray) {
142: makeCurrent(x - boundsX, y - boundsY);
143: return sampleModel.getPixel(x - sampleModelTranslateX, y
144: - sampleModelTranslateY, iArray, dataBuffer);
145: }
146:
147: public float[] getPixel(int x, int y, float[] fArray) {
148: makeCurrent(x - boundsX, y - boundsY);
149: return sampleModel.getPixel(x - sampleModelTranslateX, y
150: - sampleModelTranslateY, fArray, dataBuffer);
151: }
152:
153: public double[] getPixel(int x, int y, double[] dArray) {
154: makeCurrent(x - boundsX, y - boundsY);
155: return sampleModel.getPixel(x - sampleModelTranslateX, y
156: - sampleModelTranslateY, dArray, dataBuffer);
157: }
158:
159: public void done() {
160: xTiles = null;
161: yTiles = null;
162: dataBuffer = null;
163: }
164: }
|