001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package java.awt.image;
021:
022: import org.apache.harmony.awt.internal.nls.Messages;
023:
024: public final class BandedSampleModel extends ComponentSampleModel {
025:
026: private static int[] createIndices(int numBands) {
027: int indices[] = new int[numBands];
028: for (int i = 0; i < numBands; i++) {
029: indices[i] = i;
030: }
031: return indices;
032: }
033:
034: private static int[] createOffsets(int numBands) {
035: int offsets[] = new int[numBands];
036: for (int i = 0; i < numBands; i++) {
037: offsets[i] = 0;
038: }
039: return offsets;
040: }
041:
042: public BandedSampleModel(int dataType, int w, int h, int numBands) {
043: this (dataType, w, h, w, BandedSampleModel
044: .createIndices(numBands), BandedSampleModel
045: .createOffsets(numBands));
046: }
047:
048: public BandedSampleModel(int dataType, int w, int h,
049: int scanlineStride, int bankIndices[], int bandOffsets[]) {
050: super (dataType, w, h, 1, scanlineStride, bankIndices,
051: bandOffsets);
052: }
053:
054: @Override
055: public SampleModel createCompatibleSampleModel(int w, int h) {
056: return new BandedSampleModel(dataType, w, h, w, bankIndices,
057: bandOffsets);
058: }
059:
060: @Override
061: public DataBuffer createDataBuffer() {
062: DataBuffer data = null;
063: int size = scanlineStride * height;
064:
065: switch (dataType) {
066: case DataBuffer.TYPE_BYTE:
067: data = new DataBufferByte(size, numBanks);
068: break;
069: case DataBuffer.TYPE_SHORT:
070: case DataBuffer.TYPE_USHORT:
071: data = new DataBufferShort(size, numBanks);
072: break;
073: case DataBuffer.TYPE_INT:
074: data = new DataBufferInt(size, numBanks);
075: break;
076: case DataBuffer.TYPE_FLOAT:
077: data = new DataBufferFloat(size, numBanks);
078: break;
079: case DataBuffer.TYPE_DOUBLE:
080: data = new DataBufferDouble(size, numBanks);
081: break;
082: }
083:
084: return data;
085:
086: }
087:
088: @Override
089: public SampleModel createSubsetSampleModel(int[] bands) {
090: if (bands.length > numBands) {
091: // awt.64=The number of the bands in the subset is greater than the number of bands in the sample model
092: throw new RasterFormatException(Messages
093: .getString("awt.64")); //$NON-NLS-1$
094: }
095:
096: int indices[] = new int[bands.length];
097: int offsets[] = new int[bands.length];
098:
099: for (int i = 0; i < bands.length; i++) {
100: indices[i] = bankIndices[bands[i]];
101: offsets[i] = bandOffsets[bands[i]];
102: }
103:
104: return new BandedSampleModel(dataType, width, height,
105: scanlineStride, indices, offsets);
106: }
107:
108: @Override
109: public Object getDataElements(int x, int y, Object obj,
110: DataBuffer data) {
111: switch (dataType) {
112: case DataBuffer.TYPE_BYTE: {
113: byte bdata[];
114:
115: if (obj == null) {
116: bdata = new byte[numBands];
117: } else {
118: bdata = (byte[]) obj;
119: }
120:
121: for (int i = 0; i < numBands; i++) {
122: bdata[i] = (byte) getSample(x, y, i, data);
123: }
124:
125: obj = bdata;
126: break;
127: }
128: case DataBuffer.TYPE_SHORT:
129: case DataBuffer.TYPE_USHORT: {
130: short sdata[];
131:
132: if (obj == null) {
133: sdata = new short[numBands];
134: } else {
135: sdata = (short[]) obj;
136: }
137:
138: for (int i = 0; i < numBands; i++) {
139: sdata[i] = (short) getSample(x, y, i, data);
140: }
141:
142: obj = sdata;
143: break;
144: }
145: case DataBuffer.TYPE_INT: {
146: int idata[];
147:
148: if (obj == null) {
149: idata = new int[numBands];
150: } else {
151: idata = (int[]) obj;
152: }
153:
154: for (int i = 0; i < numBands; i++) {
155: idata[i] = getSample(x, y, i, data);
156: }
157:
158: obj = idata;
159: break;
160: }
161: case DataBuffer.TYPE_FLOAT: {
162: float fdata[];
163:
164: if (obj == null) {
165: fdata = new float[numBands];
166: } else {
167: fdata = (float[]) obj;
168: }
169:
170: for (int i = 0; i < numBands; i++) {
171: fdata[i] = getSampleFloat(x, y, i, data);
172: }
173:
174: obj = fdata;
175: break;
176: }
177: case DataBuffer.TYPE_DOUBLE: {
178: double ddata[];
179:
180: if (obj == null) {
181: ddata = new double[numBands];
182: } else {
183: ddata = (double[]) obj;
184: }
185:
186: for (int i = 0; i < numBands; i++) {
187: ddata[i] = getSampleDouble(x, y, i, data);
188: }
189:
190: obj = ddata;
191: break;
192: }
193: }
194:
195: return obj;
196: }
197:
198: @Override
199: public int[] getPixel(int x, int y, int iArray[], DataBuffer data) {
200: int pixel[];
201: if (iArray == null) {
202: pixel = new int[numBands];
203: } else {
204: pixel = iArray;
205: }
206:
207: for (int i = 0; i < numBands; i++) {
208: pixel[i] = getSample(x, y, i, data);
209: }
210:
211: return pixel;
212: }
213:
214: @Override
215: public int getSample(int x, int y, int b, DataBuffer data) {
216: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
217: // awt.63=Coordinates are not in bounds
218: throw new ArrayIndexOutOfBoundsException(Messages
219: .getString("awt.63")); //$NON-NLS-1$
220: }
221:
222: return data.getElem(bankIndices[b], y * scanlineStride + x
223: + bandOffsets[b]);
224: }
225:
226: @Override
227: public double getSampleDouble(int x, int y, int b, DataBuffer data) {
228: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
229: // awt.63=Coordinates are not in bounds
230: throw new ArrayIndexOutOfBoundsException(Messages
231: .getString("awt.63")); //$NON-NLS-1$
232: }
233:
234: return data.getElemDouble(bankIndices[b], y * scanlineStride
235: + x + bandOffsets[b]);
236: }
237:
238: @Override
239: public float getSampleFloat(int x, int y, int b, DataBuffer data) {
240: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
241: // awt.63=Coordinates are not in bounds
242: throw new ArrayIndexOutOfBoundsException(Messages
243: .getString("awt.63")); //$NON-NLS-1$
244: }
245:
246: return data.getElemFloat(bankIndices[b], y * scanlineStride + x
247: + bandOffsets[b]);
248: }
249:
250: @Override
251: public int[] getSamples(int x, int y, int w, int h, int b,
252: int iArray[], DataBuffer data) {
253: int samples[];
254: int idx = 0;
255:
256: if (iArray == null) {
257: samples = new int[w * h];
258: } else {
259: samples = iArray;
260: }
261:
262: for (int i = y; i < y + h; i++) {
263: for (int j = x; j < x + w; j++) {
264: samples[idx++] = getSample(j, i, b, data);
265: }
266: }
267:
268: return samples;
269: }
270:
271: @Override
272: public int hashCode() {
273: int hash = super .hashCode();
274: int tmp = hash >>> 8;
275: hash <<= 8;
276: hash |= tmp;
277:
278: return hash ^ 0x55;
279: }
280:
281: @Override
282: public void setDataElements(int x, int y, Object obj,
283: DataBuffer data) {
284: switch (dataType) {
285: case DataBuffer.TYPE_BYTE:
286: byte bdata[] = (byte[]) obj;
287: for (int i = 0; i < numBands; i++) {
288: setSample(x, y, i, bdata[i] & 0xff, data);
289: }
290: break;
291:
292: case DataBuffer.TYPE_SHORT:
293: case DataBuffer.TYPE_USHORT:
294: short sdata[] = (short[]) obj;
295: for (int i = 0; i < numBands; i++) {
296: setSample(x, y, i, sdata[i] & 0xffff, data);
297: }
298: break;
299:
300: case DataBuffer.TYPE_INT:
301: int idata[] = (int[]) obj;
302: for (int i = 0; i < numBands; i++) {
303: setSample(x, y, i, idata[i], data);
304: }
305: break;
306:
307: case DataBuffer.TYPE_FLOAT:
308: float fdata[] = (float[]) obj;
309: for (int i = 0; i < numBands; i++) {
310: setSample(x, y, i, fdata[i], data);
311: }
312: break;
313:
314: case DataBuffer.TYPE_DOUBLE:
315: double ddata[] = (double[]) obj;
316: for (int i = 0; i < numBands; i++) {
317: setSample(x, y, i, ddata[i], data);
318: }
319: break;
320: }
321: }
322:
323: @Override
324: public void setPixel(int x, int y, int iArray[], DataBuffer data) {
325: for (int i = 0; i < numBands; i++) {
326: setSample(x, y, i, iArray[i], data);
327: }
328: }
329:
330: @Override
331: public void setPixels(int x, int y, int w, int h, int iArray[],
332: DataBuffer data) {
333: int idx = 0;
334:
335: for (int i = y; i < y + h; i++) {
336: for (int j = x; j < x + w; j++) {
337: for (int n = 0; n < numBands; n++) {
338: setSample(j, i, n, iArray[idx++], data);
339: }
340: }
341: }
342: }
343:
344: @Override
345: public void setSample(int x, int y, int b, double s, DataBuffer data) {
346: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
347: // awt.63=Coordinates are not in bounds
348: throw new ArrayIndexOutOfBoundsException(Messages
349: .getString("awt.63")); //$NON-NLS-1$
350: }
351:
352: data.setElemDouble(bankIndices[b], y * scanlineStride + x
353: + bandOffsets[b], s);
354: }
355:
356: @Override
357: public void setSample(int x, int y, int b, float s, DataBuffer data) {
358: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
359: // awt.63=Coordinates are not in bounds
360: throw new ArrayIndexOutOfBoundsException(Messages
361: .getString("awt.63")); //$NON-NLS-1$
362: }
363:
364: data.setElemFloat(bankIndices[b], y * scanlineStride + x
365: + bandOffsets[b], s);
366: }
367:
368: @Override
369: public void setSample(int x, int y, int b, int s, DataBuffer data) {
370: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
371: // awt.63=Coordinates are not in bounds
372: throw new ArrayIndexOutOfBoundsException(Messages
373: .getString("awt.63")); //$NON-NLS-1$
374: }
375:
376: data.setElem(bankIndices[b], y * scanlineStride + x
377: + bandOffsets[b], s);
378: }
379:
380: @Override
381: public void setSamples(int x, int y, int w, int h, int b,
382: int iArray[], DataBuffer data) {
383: int idx = 0;
384:
385: for (int i = y; i < y + h; i++) {
386: for (int j = x; j < x + w; j++) {
387: setSample(j, i, b, iArray[idx++], data);
388: }
389: }
390:
391: }
392:
393: }
|