001: /*
002: * $RCSfile: RectIterCSM.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.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.RectIter;
022:
023: /**
024: */
025: public abstract class RectIterCSM extends RectIterFallback {
026:
027: protected int[] bankIndices;
028: protected int scanlineStride;
029: protected int pixelStride;
030: protected int[] bandOffsets;
031: protected int[] DBOffsets;
032:
033: protected int offset;
034: protected int bandOffset;
035:
036: public RectIterCSM(RenderedImage im, Rectangle bounds) {
037: super (im, bounds);
038:
039: ComponentSampleModel csm = (ComponentSampleModel) sampleModel;
040:
041: this .scanlineStride = csm.getScanlineStride();
042: this .pixelStride = csm.getPixelStride();
043: this .bankIndices = csm.getBankIndices();
044: int[] bo = csm.getBandOffsets();
045:
046: this .bandOffsets = new int[numBands + 1];
047: for (int i = 0; i < numBands; i++) {
048: bandOffsets[i] = bo[i];
049: }
050: bandOffsets[numBands] = 0;
051:
052: this .DBOffsets = new int[numBands];
053:
054: this .offset = (y - sampleModelTranslateY) * scanlineStride
055: + (x - sampleModelTranslateX) * pixelStride;
056: this .bandOffset = bandOffsets[0];
057: }
058:
059: protected void dataBufferChanged() {
060: }
061:
062: protected void adjustBandOffsets() {
063: int[] newDBOffsets = dataBuffer.getOffsets();
064: for (int i = 0; i < numBands; i++) {
065: int bankNum = bankIndices[i];
066: bandOffsets[i] += newDBOffsets[bankNum]
067: - DBOffsets[bankNum];
068: }
069: this .DBOffsets = newDBOffsets;
070: }
071:
072: protected void setDataBuffer() {
073: Raster tile = im.getTile(tileX, tileY);
074: this .dataBuffer = tile.getDataBuffer();
075: dataBufferChanged();
076:
077: int newSampleModelTranslateX = tile.getSampleModelTranslateX();
078: int newSampleModelTranslateY = tile.getSampleModelTranslateY();
079:
080: int deltaX = sampleModelTranslateX - newSampleModelTranslateX;
081: int deltaY = sampleModelTranslateY - newSampleModelTranslateY;
082:
083: offset += deltaY * scanlineStride + deltaX * pixelStride;
084:
085: this .sampleModelTranslateX = newSampleModelTranslateX;
086: this .sampleModelTranslateY = newSampleModelTranslateY;
087: }
088:
089: public void startLines() {
090: offset += (bounds.y - y) * scanlineStride;
091: y = bounds.y;
092:
093: tileY = startTileY;
094: setTileYBounds();
095: setDataBuffer();
096: }
097:
098: public void nextLine() {
099: ++y;
100: offset += scanlineStride;
101: }
102:
103: public void jumpLines(int num) {
104: int jumpY = y + num;
105: if (jumpY < bounds.y || jumpY > lastY) {
106: // Jumped outside the image.
107: throw new IndexOutOfBoundsException(JaiI18N
108: .getString("RectIterFallback1"));
109: }
110:
111: y = jumpY;
112: offset += num * scanlineStride;
113:
114: if (y < prevYBoundary || y > nextYBoundary) {
115: this .tileY = PlanarImage.YToTileY(y, tileGridYOffset,
116: tileHeight);
117: setTileYBounds();
118: setDataBuffer();
119: }
120: }
121:
122: public void startPixels() {
123: offset += (bounds.x - x) * pixelStride;
124: x = bounds.x;
125:
126: tileX = startTileX;
127: setTileXBounds();
128: setDataBuffer();
129: }
130:
131: public void nextPixel() {
132: ++x;
133: offset += pixelStride;
134: }
135:
136: public void jumpPixels(int num) {
137: int jumpX = x + num;
138: if (jumpX < bounds.x || jumpX > lastX) {
139: // Jumped outside the image.
140: throw new IndexOutOfBoundsException(JaiI18N
141: .getString("RectIterFallback0"));
142: }
143:
144: x = jumpX;
145: offset += num * pixelStride;
146:
147: if (x < prevXBoundary || x > nextXBoundary) {
148: this .tileX = PlanarImage.XToTileX(x, tileGridXOffset,
149: tileWidth);
150:
151: setTileXBounds();
152: setDataBuffer();
153: }
154: }
155:
156: public void startBands() {
157: b = 0;
158: bandOffset = bandOffsets[0];
159: }
160:
161: public void nextBand() {
162: ++b;
163: bandOffset = bandOffsets[b];
164: }
165: }
|