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 java.util.Arrays;
023:
024: import org.apache.harmony.awt.internal.nls.Messages;
025:
026: public class ComponentSampleModel extends SampleModel {
027:
028: protected int bandOffsets[];
029:
030: protected int bankIndices[];
031:
032: protected int numBands;
033:
034: protected int numBanks;
035:
036: protected int scanlineStride;
037:
038: protected int pixelStride;
039:
040: public ComponentSampleModel(int dataType, int w, int h,
041: int pixelStride, int scanlineStride, int bankIndices[],
042: int bandOffsets[]) {
043:
044: super (dataType, w, h, bandOffsets.length);
045:
046: if (pixelStride < 0) {
047: // awt.24B=Pixel stride must be >= 0
048: throw new IllegalArgumentException(Messages
049: .getString("awt.24B")); //$NON-NLS-1$
050: }
051:
052: if (scanlineStride < 0) {
053: // awt.24C=Scanline stride must be >= 0
054: throw new IllegalArgumentException(Messages
055: .getString("awt.24C")); //$NON-NLS-1$
056: }
057:
058: if (bankIndices.length != bandOffsets.length) {
059: // awt.24D=Bank Indices length must be equal Bank Offsets length
060: throw new IllegalArgumentException(Messages
061: .getString("awt.24D")); //$NON-NLS-1$
062: }
063:
064: this .pixelStride = pixelStride;
065: this .scanlineStride = scanlineStride;
066: this .bandOffsets = bandOffsets.clone();
067: this .bankIndices = bankIndices.clone();
068: this .numBands = bandOffsets.length;
069:
070: int maxBank = 0;
071: for (int i = 0; i < bankIndices.length; i++) {
072: if (bankIndices[i] < 0) {
073: // awt.24E=Index of {0} bank must be >= 0
074: throw new IllegalArgumentException(Messages.getString(
075: "awt.24E", i)); //$NON-NLS-1$
076: }
077: if (bankIndices[i] > maxBank) {
078: maxBank = bankIndices[i];
079: }
080: }
081: this .numBanks = maxBank + 1;
082:
083: }
084:
085: public ComponentSampleModel(int dataType, int w, int h,
086: int pixelStride, int scanlineStride, int bandOffsets[]) {
087:
088: super (dataType, w, h, bandOffsets.length);
089: if (pixelStride < 0) {
090: // awt.24B=Pixel stride must be >= 0
091: throw new IllegalArgumentException(Messages
092: .getString("awt.24B")); //$NON-NLS-1$
093: }
094:
095: if (scanlineStride < 0) {
096: // awt.24C=Scanline stride must be >= 0
097: throw new IllegalArgumentException(Messages
098: .getString("awt.24C")); //$NON-NLS-1$
099: }
100:
101: this .pixelStride = pixelStride;
102: this .scanlineStride = scanlineStride;
103: this .bandOffsets = bandOffsets.clone();
104: this .numBands = bandOffsets.length;
105: this .numBanks = 1;
106:
107: this .bankIndices = new int[numBands];
108: for (int i = 0; i < numBands; i++) {
109: bankIndices[i] = 0;
110: }
111: }
112:
113: @Override
114: public Object getDataElements(int x, int y, Object obj,
115: DataBuffer data) {
116: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
117: // awt.63=Coordinates are not in bounds
118: throw new ArrayIndexOutOfBoundsException(Messages
119: .getString("awt.63")); //$NON-NLS-1$
120: }
121: switch (dataType) {
122: case DataBuffer.TYPE_BYTE:
123: byte bdata[];
124: if (obj == null) {
125: bdata = new byte[numBands];
126: } else {
127: bdata = (byte[]) obj;
128: }
129:
130: for (int i = 0; i < numBands; i++) {
131: bdata[i] = (byte) getSample(x, y, i, data);
132: }
133:
134: obj = bdata;
135: break;
136:
137: case DataBuffer.TYPE_SHORT:
138: case DataBuffer.TYPE_USHORT:
139: short sdata[];
140: if (obj == null) {
141: sdata = new short[numBands];
142: } else {
143: sdata = (short[]) obj;
144: }
145:
146: for (int i = 0; i < numBands; i++) {
147: sdata[i] = (short) getSample(x, y, i, data);
148: }
149:
150: obj = sdata;
151: break;
152:
153: case DataBuffer.TYPE_INT:
154: int idata[];
155: if (obj == null) {
156: idata = new int[numBands];
157: } else {
158: idata = (int[]) obj;
159: }
160:
161: for (int i = 0; i < numBands; i++) {
162: idata[i] = getSample(x, y, i, data);
163: }
164:
165: obj = idata;
166: break;
167:
168: case DataBuffer.TYPE_FLOAT:
169: float fdata[];
170: if (obj == null) {
171: fdata = new float[numBands];
172: } else {
173: fdata = (float[]) obj;
174: }
175:
176: for (int i = 0; i < numBands; i++) {
177: fdata[i] = getSampleFloat(x, y, i, data);
178: }
179:
180: obj = fdata;
181: break;
182:
183: case DataBuffer.TYPE_DOUBLE:
184: double ddata[];
185: if (obj == null) {
186: ddata = new double[numBands];
187: } else {
188: ddata = (double[]) obj;
189: }
190:
191: for (int i = 0; i < numBands; i++) {
192: ddata[i] = getSampleDouble(x, y, i, data);
193: }
194:
195: obj = ddata;
196: break;
197: }
198:
199: return obj;
200: }
201:
202: @Override
203: public void setDataElements(int x, int y, Object obj,
204: DataBuffer data) {
205: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
206: // awt.63=Coordinates are not in bounds
207: throw new ArrayIndexOutOfBoundsException(Messages
208: .getString("awt.63")); //$NON-NLS-1$
209: }
210: switch (dataType) {
211: case DataBuffer.TYPE_BYTE:
212: byte barr[] = (byte[]) obj;
213: for (int i = 0; i < numBands; i++) {
214: setSample(x, y, i, barr[i] & 0xff, data);
215: }
216: break;
217:
218: case DataBuffer.TYPE_SHORT:
219: case DataBuffer.TYPE_USHORT:
220: short sarr[] = (short[]) obj;
221: for (int i = 0; i < numBands; i++) {
222: setSample(x, y, i, sarr[i] & 0xffff, data);
223: }
224: break;
225:
226: case DataBuffer.TYPE_INT:
227: int iarr[] = (int[]) obj;
228: for (int i = 0; i < numBands; i++) {
229: setSample(x, y, i, iarr[i], data);
230: }
231: break;
232:
233: case DataBuffer.TYPE_FLOAT:
234: float farr[] = (float[]) obj;
235: for (int i = 0; i < numBands; i++) {
236: setSample(x, y, i, farr[i], data);
237: }
238: break;
239:
240: case DataBuffer.TYPE_DOUBLE:
241: double darr[] = (double[]) obj;
242: for (int i = 0; i < numBands; i++) {
243: setSample(x, y, i, darr[i], data);
244: }
245: break;
246: }
247: }
248:
249: @Override
250: public boolean equals(Object o) {
251: if ((o == null) || !(o instanceof ComponentSampleModel)) {
252: return false;
253: }
254: ComponentSampleModel model = (ComponentSampleModel) o;
255: return this .width == model.width && this .height == model.height
256: && this .numBands == model.numBands
257: && this .dataType == model.dataType
258: && Arrays.equals(this .bandOffsets, model.bandOffsets)
259: && Arrays.equals(this .bankIndices, model.bankIndices)
260: && this .numBands == model.numBands
261: && this .numBanks == model.numBanks
262: && this .scanlineStride == model.scanlineStride
263: && this .pixelStride == model.pixelStride;
264: }
265:
266: @Override
267: public SampleModel createSubsetSampleModel(int bands[]) {
268: if (bands.length > this .numBands) {
269: // awt.64=The number of the bands in the subset is greater than the number of bands in the sample model
270: throw new RasterFormatException(Messages
271: .getString("awt.64")); //$NON-NLS-1$
272: }
273:
274: int indices[] = new int[bands.length];
275: int offsets[] = new int[bands.length];
276:
277: for (int i = 0; i < bands.length; i++) {
278: indices[i] = bankIndices[bands[i]];
279: offsets[i] = bandOffsets[bands[i]];
280: }
281:
282: return new ComponentSampleModel(dataType, width, height,
283: pixelStride, scanlineStride, indices, offsets);
284:
285: }
286:
287: @Override
288: public SampleModel createCompatibleSampleModel(int w, int h) {
289: return new ComponentSampleModel(dataType, w, h, pixelStride,
290: pixelStride * w, bankIndices, bandOffsets);
291: }
292:
293: @Override
294: public int[] getPixel(int x, int y, int iArray[], DataBuffer data) {
295: int pixel[];
296:
297: if (iArray == null) {
298: pixel = new int[numBands];
299: } else {
300: pixel = iArray;
301: }
302:
303: for (int i = 0; i < numBands; i++) {
304: pixel[i] = getSample(x, y, i, data);
305: }
306:
307: return pixel;
308: }
309:
310: @Override
311: public void setPixel(int x, int y, int iArray[], DataBuffer data) {
312: for (int i = 0; i < numBands; i++) {
313: setSample(x, y, i, iArray[i], data);
314: }
315: }
316:
317: @Override
318: public int getSample(int x, int y, int b, DataBuffer data) {
319: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
320: // awt.63=Coordinates are not in bounds
321: throw new ArrayIndexOutOfBoundsException(Messages
322: .getString("awt.63")); //$NON-NLS-1$
323: }
324:
325: return data.getElem(bankIndices[b], y * scanlineStride + x
326: * pixelStride + bandOffsets[b]);
327: }
328:
329: @Override
330: public float getSampleFloat(int x, int y, int b, DataBuffer data) {
331: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
332: // awt.63=Coordinates are not in bounds
333: throw new ArrayIndexOutOfBoundsException(Messages
334: .getString("awt.63")); //$NON-NLS-1$
335: }
336:
337: return data.getElemFloat(bankIndices[b], y * scanlineStride + x
338: * pixelStride + bandOffsets[b]);
339: }
340:
341: @Override
342: public double getSampleDouble(int x, int y, int b, DataBuffer data) {
343: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
344: // awt.63=Coordinates are not in bounds
345: throw new ArrayIndexOutOfBoundsException(Messages
346: .getString("awt.63")); //$NON-NLS-1$
347: }
348:
349: return data.getElemDouble(bankIndices[b], y * scanlineStride
350: + x * pixelStride + bandOffsets[b]);
351: }
352:
353: @Override
354: public int[] getPixels(int x, int y, int w, int h, int iArray[],
355: DataBuffer data) {
356: if (x < 0 || y < 0 || x > this .width || x + w > this .width
357: || y > this .height || y + h > this .height) {
358: // awt.63=Coordinates are not in bounds
359: throw new ArrayIndexOutOfBoundsException(Messages
360: .getString("awt.63")); //$NON-NLS-1$
361: }
362: int pixels[] = null;
363: int idx = 0;
364:
365: if (iArray == null) {
366: pixels = new int[w * h * numBands];
367: } else {
368: pixels = iArray;
369: }
370:
371: for (int i = y; i < y + h; i++) {
372: for (int j = x; j < x + w; j++) {
373: for (int n = 0; n < numBands; n++) {
374: pixels[idx++] = getSample(j, i, n, data);
375: }
376: }
377: }
378:
379: return pixels;
380: }
381:
382: @Override
383: public void setPixels(int x, int y, int w, int h, int iArray[],
384: DataBuffer data) {
385: if (x < 0 || y < 0 || x + w > this .width || y + h > this .height) {
386: // awt.63=Coordinates are not in bounds
387: throw new ArrayIndexOutOfBoundsException(Messages
388: .getString("awt.63")); //$NON-NLS-1$
389: }
390: int idx = 0;
391: for (int i = y; i < y + h; i++) {
392: for (int j = x; j < x + w; j++) {
393: for (int n = 0; n < numBands; n++) {
394: setSample(j, i, n, iArray[idx++], data);
395: }
396: }
397: }
398: }
399:
400: @Override
401: public void setSample(int x, int y, int b, int s, DataBuffer data) {
402: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
403: // awt.63=Coordinates are not in bounds
404: throw new ArrayIndexOutOfBoundsException(Messages
405: .getString("awt.63")); //$NON-NLS-1$
406: }
407:
408: data.setElem(bankIndices[b], y * scanlineStride + x
409: * pixelStride + bandOffsets[b], s);
410: }
411:
412: @Override
413: public int[] getSamples(int x, int y, int w, int h, int b,
414: int iArray[], DataBuffer data) {
415: if (x < 0 || y < 0 || x + w > this .width || y + h > this .height) {
416: // awt.63=Coordinates are not in bounds
417: throw new ArrayIndexOutOfBoundsException(Messages
418: .getString("awt.63")); //$NON-NLS-1$
419: }
420: int samples[];
421: int idx = 0;
422:
423: if (iArray == null) {
424: samples = new int[w * h];
425: } else {
426: samples = iArray;
427: }
428:
429: if (data == null) {
430: // awt.295=data is null
431: throw new NullPointerException(Messages
432: .getString("awt.295")); //$NON-NLS-1$
433: }
434:
435: for (int i = y; i < y + h; i++) {
436: for (int j = x; j < x + w; j++) {
437: samples[idx++] = getSample(j, i, b, data);
438: }
439: }
440:
441: return samples;
442: }
443:
444: @Override
445: public void setSamples(int x, int y, int w, int h, int b,
446: int iArray[], DataBuffer data) {
447: if (x < 0 || y < 0 || x + w > this .width || y + h > this .height) {
448: // awt.63=Coordinates are not in bounds
449: throw new ArrayIndexOutOfBoundsException(Messages
450: .getString("awt.63")); //$NON-NLS-1$
451: }
452: int idx = 0;
453: for (int i = y; i < y + h; i++) {
454: for (int j = x; j < x + w; j++) {
455: setSample(j, i, b, iArray[idx++], data);
456: }
457: }
458: }
459:
460: @Override
461: public void setSample(int x, int y, int b, float s, DataBuffer data) {
462: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
463: // awt.63=Coordinates are not in bounds
464: throw new ArrayIndexOutOfBoundsException(Messages
465: .getString("awt.63")); //$NON-NLS-1$
466: }
467:
468: data.setElemFloat(bankIndices[b], y * scanlineStride + x
469: * pixelStride + bandOffsets[b], s);
470: }
471:
472: @Override
473: public void setSample(int x, int y, int b, double s, DataBuffer data) {
474: if (x < 0 || y < 0 || x >= this .width || y >= this .height) {
475: // awt.63=Coordinates are not in bounds
476: throw new ArrayIndexOutOfBoundsException(Messages
477: .getString("awt.63")); //$NON-NLS-1$
478: }
479:
480: data.setElemDouble(bankIndices[b], y * scanlineStride + x
481: * pixelStride + bandOffsets[b], s);
482: }
483:
484: @Override
485: public DataBuffer createDataBuffer() {
486: DataBuffer data = null;
487:
488: int maxOffset = bandOffsets[0];
489: for (int i = 1; i < bandOffsets.length; i++) {
490: if (bandOffsets[i] > maxOffset) {
491: maxOffset = bandOffsets[i];
492: }
493: }
494: int size = (height - 1) * scanlineStride + (width - 1)
495: * pixelStride + maxOffset + 1;
496:
497: switch (dataType) {
498: case DataBuffer.TYPE_BYTE:
499: data = new DataBufferByte(size, numBanks);
500: break;
501: case DataBuffer.TYPE_SHORT:
502: data = new DataBufferShort(size, numBanks);
503: break;
504: case DataBuffer.TYPE_USHORT:
505: data = new DataBufferUShort(size, numBanks);
506: break;
507: case DataBuffer.TYPE_INT:
508: data = new DataBufferInt(size, numBanks);
509: break;
510: case DataBuffer.TYPE_FLOAT:
511: data = new DataBufferFloat(size, numBanks);
512: break;
513: case DataBuffer.TYPE_DOUBLE:
514: data = new DataBufferDouble(size, numBanks);
515: break;
516: }
517:
518: return data;
519:
520: }
521:
522: public int getOffset(int x, int y, int b) {
523: return y * scanlineStride + x * pixelStride + bandOffsets[b];
524: }
525:
526: public int getOffset(int x, int y) {
527: return y * scanlineStride + x * pixelStride + bandOffsets[0];
528: }
529:
530: @Override
531: public final int getSampleSize(int band) {
532: return DataBuffer.getDataTypeSize(dataType);
533: }
534:
535: @Override
536: public final int[] getSampleSize() {
537: int sampleSizes[] = new int[numBands];
538: int size = DataBuffer.getDataTypeSize(dataType);
539:
540: for (int i = 0; i < numBands; i++) {
541: sampleSizes[i] = size;
542: }
543: return sampleSizes;
544: }
545:
546: public final int[] getBankIndices() {
547: return bankIndices.clone();
548: }
549:
550: public final int[] getBandOffsets() {
551: return bandOffsets.clone();
552: }
553:
554: @Override
555: public int hashCode() {
556: int hash = 0;
557: int tmp = 0;
558:
559: hash = width;
560: tmp = hash >>> 24;
561: hash <<= 8;
562: hash |= tmp;
563: hash ^= height;
564: tmp = hash >>> 24;
565: hash <<= 8;
566: hash |= tmp;
567: hash ^= numBands;
568: tmp = hash >>> 24;
569: hash <<= 8;
570: hash |= tmp;
571: hash ^= dataType;
572: tmp = hash >>> 24;
573: hash <<= 8;
574: hash |= tmp;
575: for (int element : bandOffsets) {
576: hash ^= element;
577: tmp = hash >>> 24;
578: hash <<= 8;
579: hash |= tmp;
580: }
581: for (int element : bankIndices) {
582: hash ^= element;
583: tmp = hash >>> 24;
584: hash <<= 8;
585: hash |= tmp;
586: }
587: hash ^= pixelStride;
588: tmp = hash >>> 24;
589: hash <<= 8;
590: hash |= tmp;
591:
592: hash ^= scanlineStride;
593: return hash;
594: }
595:
596: public final int getScanlineStride() {
597: return scanlineStride;
598: }
599:
600: public final int getPixelStride() {
601: return pixelStride;
602: }
603:
604: @Override
605: public final int getNumDataElements() {
606: return numBands;
607: }
608:
609: }
|