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