001: /*
002: * $RCSfile: RandomIterCSM.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:42 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.iterator;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.ComponentSampleModel;
016: import java.awt.image.DataBuffer;
017: import java.awt.image.Raster;
018: import java.awt.image.RenderedImage;
019: import java.awt.image.SampleModel;
020: import javax.media.jai.PlanarImage;
021: import javax.media.jai.iterator.RandomIter;
022:
023: /**
024: * @since EA2
025: */
026: public abstract class RandomIterCSM extends RandomIterFallback {
027:
028: protected ComponentSampleModel sampleModel;
029: protected int pixelStride;
030: protected int scanlineStride;
031: protected int[] bandOffsets;
032: protected int numBands;
033:
034: public RandomIterCSM(RenderedImage im, Rectangle bounds) {
035: super (im, bounds);
036: this .sampleModel = (ComponentSampleModel) im.getSampleModel();
037: this .numBands = sampleModel.getNumBands();
038: this .pixelStride = sampleModel.getPixelStride();
039: this .scanlineStride = sampleModel.getScanlineStride();
040: }
041:
042: protected void dataBufferChanged() {
043: }
044:
045: /**
046: * Sets dataBuffer to the correct buffer for the pixel
047: * (x, y) = (xLocal + boundsRect.x, yLocal + boundsRect.y).
048: *
049: * @param xLocal the X coordinate in the local coordinate system.
050: * @param yLocal the Y coordinate in the local coordinate system.
051: */
052: protected final void makeCurrent(int xLocal, int yLocal) {
053: int xIDNew = xTiles[xLocal];
054: int yIDNew = yTiles[yLocal];
055:
056: if ((xIDNew != xID) || (yIDNew != yID) || (dataBuffer == null)) {
057: xID = xIDNew;
058: yID = yIDNew;
059: Raster tile = im.getTile(xID, yID);
060:
061: this .dataBuffer = tile.getDataBuffer();
062: dataBufferChanged();
063:
064: this .bandOffsets = dataBuffer.getOffsets();
065: }
066: }
067:
068: public float getSampleFloat(int x, int y, int b) {
069: return (float) getSample(x, y, b);
070: }
071:
072: public double getSampleDouble(int x, int y, int b) {
073: return (double) getSample(x, y, b);
074: }
075:
076: public int[] getPixel(int x, int y, int[] iArray) {
077: if (iArray == null) {
078: iArray = new int[numBands];
079: }
080: for (int b = 0; b < numBands; b++) {
081: iArray[b] = getSample(x, y, b);
082: }
083: return iArray;
084: }
085:
086: public float[] getPixel(int x, int y, float[] fArray) {
087: if (fArray == null) {
088: fArray = new float[numBands];
089: }
090: for (int b = 0; b < numBands; b++) {
091: fArray[b] = getSampleFloat(x, y, b);
092: }
093: return fArray;
094: }
095:
096: public double[] getPixel(int x, int y, double[] dArray) {
097: if (dArray == null) {
098: dArray = new double[numBands];
099: }
100: for (int b = 0; b < numBands; b++) {
101: dArray[b] = getSampleDouble(x, y, b);
102: }
103: return dArray;
104: }
105: }
|