001: /*
002: * $RCSfile: BorderExtenderZero.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:57:04 $
010: * $State: Exp $
011: */
012: package javax.media.jai;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.DataBuffer;
016: import java.awt.image.WritableRaster;
017:
018: /**
019: * A subclass of <code>BorderExtender</code> that implements
020: * border extension by filling all pixels outside of the image
021: * bounds with zeros. For example, the image:
022: *
023: * <p><center>
024: * <table border=1>
025: * <tr align=center><td>A</td><td>B</td><td>C</td> </tr>
026: * <tr align=center><td>D</td><td>E</td><td>F</td> </tr>
027: * <tr align=center><td>G</td><td>H</td><td>I</td> </tr>
028: * </table></center>
029:
030: *
031: * <br>if extended by adding two extra rows to the top and bottom and
032: * two extra columns on the left and right sides, would become:
033: *
034: * <p><center>
035: * <table border=1>
036: * <tr align=center>
037: * <td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td> </tr>
038: * <td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td> </tr>
039: * <td>0</td><td>0</td><td>A</td><td>B</td><td>C</td><td>0</td><td>0</td> </tr>
040: * <td>0</td><td>0</td><td>D</td><td>E</td><td>F</td><td>0</td><td>0</td> </tr>
041: * <td>0</td><td>0</td><td>G</td><td>H</td><td>I</td><td>0</td><td>0</td> </tr>
042: * <td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td> </tr>
043: * <td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td> </tr>
044: * </table></center>
045: *
046: * @see BorderExtender
047: */
048: public final class BorderExtenderZero extends BorderExtender {
049:
050: BorderExtenderZero() {
051: }
052:
053: /**
054: * Fills in the portions of a given <code>Raster</code> that lie
055: * outside the bounds of a given <code>PlanarImage</code> with
056: * zeros.
057: *
058: * <p> The portion of <code>raster</code> that lies within
059: * <code>im.getBounds()</code> is not altered.
060: *
061: * @param raster The <code>WritableRaster</code> the border area of
062: * which is to be filled with zero.
063: * @param im The <code>PlanarImage</code> which determines the
064: * portion of the <code>WritableRaster</code> <i>not</i>
065: * to be filled.
066: *
067: * @throws <code>IllegalArgumentException</code> if either parameter is
068: * <code>null</code>.
069: */
070: public final void extend(WritableRaster raster, PlanarImage im) {
071:
072: if (raster == null || im == null) {
073: throw new IllegalArgumentException(JaiI18N
074: .getString("Generic0"));
075: }
076:
077: int width = raster.getWidth();
078: int height = raster.getHeight();
079: int numBands = raster.getNumBands();
080:
081: int minX = raster.getMinX();
082: int maxX = minX + width;
083: int minY = raster.getMinY();
084: int maxY = minY + height;
085:
086: int validMinX = Math.max(im.getMinX(), minX);
087: int validMaxX = Math.min(im.getMaxX(), maxX);
088: int validMinY = Math.max(im.getMinY(), minY);
089: int validMaxY = Math.min(im.getMaxY(), maxY);
090:
091: int row;
092: switch (raster.getSampleModel().getDataType()) {
093: case DataBuffer.TYPE_BYTE:
094: case DataBuffer.TYPE_SHORT:
095: case DataBuffer.TYPE_USHORT:
096: case DataBuffer.TYPE_INT:
097: int[] iData = new int[width * numBands];
098: if (validMinX > validMaxX || validMinY > validMaxY) {
099: // Raster does not intersect image.
100: for (row = minY; row < maxY; row++) {
101: raster.setPixels(minX, row, width, 1, iData);
102: }
103: } else {
104: for (row = minY; row < validMinY; row++) {
105: raster.setPixels(minX, row, width, 1, iData);
106: }
107: for (row = validMinY; row < validMaxY; row++) {
108: if (minX < validMinX) {
109: raster.setPixels(minX, row, validMinX - minX,
110: 1, iData);
111: }
112: if (validMaxX < maxX) {
113: raster.setPixels(validMaxX, row, maxX
114: - validMaxX, 1, iData);
115: }
116: }
117: for (row = validMaxY; row < maxY; row++) {
118: raster.setPixels(minX, row, width, 1, iData);
119: }
120: }
121: break;
122:
123: case DataBuffer.TYPE_FLOAT:
124: float[] fData = new float[width * numBands];
125: if (validMinX > validMaxX || validMinY > validMaxY) {
126: // Raster does not intersect image.
127: for (row = minY; row < maxY; row++) {
128: raster.setPixels(minX, row, width, 1, fData);
129: }
130: } else {
131: for (row = minY; row < validMinY; row++) {
132: raster.setPixels(minX, row, width, 1, fData);
133: }
134: for (row = validMinY; row < validMaxY; row++) {
135: if (minX < validMinX) {
136: raster.setPixels(minX, row, validMinX - minX,
137: 1, fData);
138: }
139: if (validMaxX < maxX) {
140: raster.setPixels(validMaxX, row, maxX
141: - validMaxX, 1, fData);
142: }
143: }
144: for (row = validMaxY; row < maxY; row++) {
145: raster.setPixels(minX, row, width, 1, fData);
146: }
147: }
148: break;
149:
150: case DataBuffer.TYPE_DOUBLE:
151: double[] dData = new double[width * numBands];
152: if (validMinX > validMaxX || validMinY > validMaxY) {
153: // Raster does not intersect image.
154: for (row = minY; row < maxY; row++) {
155: raster.setPixels(minX, row, width, 1, dData);
156: }
157: } else {
158: for (row = minY; row < validMinY; row++) {
159: raster.setPixels(minX, row, width, 1, dData);
160: }
161: for (row = validMinY; row < validMaxY; row++) {
162: if (minX < validMinX) {
163: raster.setPixels(minX, row, validMinX - minX,
164: 1, dData);
165: }
166: if (validMaxX < maxX) {
167: raster.setPixels(validMaxX, row, maxX
168: - validMaxX, 1, dData);
169: }
170: }
171: for (row = validMaxY; row < maxY; row++) {
172: raster.setPixels(minX, row, width, 1, dData);
173: }
174: }
175: break;
176: }
177: }
178: }
|