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.awt.Point;
023: import java.awt.Rectangle;
024:
025: import org.apache.harmony.awt.gl.image.OrdinaryWritableRaster;
026: import org.apache.harmony.awt.internal.nls.Messages;
027:
028: public class Raster {
029:
030: protected DataBuffer dataBuffer;
031:
032: protected int height;
033:
034: protected int minX;
035:
036: protected int minY;
037:
038: protected int numBands;
039:
040: protected int numDataElements;
041:
042: protected Raster parent;
043:
044: protected SampleModel sampleModel;
045:
046: protected int sampleModelTranslateX;
047:
048: protected int sampleModelTranslateY;
049:
050: protected int width;
051:
052: public static WritableRaster createBandedRaster(
053: DataBuffer dataBuffer, int w, int h, int scanlineStride,
054: int bankIndices[], int bandOffsets[], Point location) {
055:
056: if (w <= 0 || h <= 0) {
057: // awt.22E=w or h is less than or equal to zero
058: throw new RasterFormatException(Messages
059: .getString("awt.22E")); //$NON-NLS-1$
060: }
061:
062: if (location == null) {
063: location = new Point(0, 0);
064: }
065:
066: if ((long) location.x + w > Integer.MAX_VALUE
067: || (long) location.y + h > Integer.MAX_VALUE) {
068: // awt.276=location.x + w or location.y + h results in integer overflow
069: throw new RasterFormatException(Messages
070: .getString("awt.276")); //$NON-NLS-1$
071: }
072:
073: if (bankIndices == null || bandOffsets == null) {
074: // awt.277=bankIndices or bandOffsets is null
075: throw new ArrayIndexOutOfBoundsException(Messages
076: .getString("awt.277")); //$NON-NLS-1$
077: }
078:
079: if (dataBuffer == null) {
080: // awt.278=dataBuffer is null
081: throw new NullPointerException(Messages
082: .getString("awt.278")); //$NON-NLS-1$
083: }
084:
085: int dataType = dataBuffer.getDataType();
086:
087: if (dataType != DataBuffer.TYPE_BYTE
088: && dataType != DataBuffer.TYPE_USHORT
089: && dataType != DataBuffer.TYPE_INT) {
090: // awt.230=dataType is not one of the supported data types
091: throw new IllegalArgumentException(Messages
092: .getString("awt.230")); //$NON-NLS-1$
093: }
094:
095: BandedSampleModel sampleModel = new BandedSampleModel(dataType,
096: w, h, scanlineStride, bankIndices, bandOffsets);
097:
098: return new OrdinaryWritableRaster(sampleModel, dataBuffer,
099: location);
100: }
101:
102: public static WritableRaster createBandedRaster(int dataType,
103: int w, int h, int scanlineStride, int bankIndices[],
104: int bandOffsets[], Point location) {
105:
106: if (w <= 0 || h <= 0) {
107: // awt.22E=w or h is less than or equal to zero
108: throw new RasterFormatException(Messages
109: .getString("awt.22E")); //$NON-NLS-1$
110: }
111:
112: if (location == null) {
113: location = new Point(0, 0);
114: }
115:
116: if ((long) location.x + w > Integer.MAX_VALUE
117: || (long) location.y + h > Integer.MAX_VALUE) {
118: // awt.276=location.x + w or location.y + h results in integer overflow
119: throw new RasterFormatException(Messages
120: .getString("awt.276")); //$NON-NLS-1$
121: }
122:
123: if (bankIndices == null || bandOffsets == null) {
124: // awt.277=bankIndices or bandOffsets is null
125: throw new ArrayIndexOutOfBoundsException(Messages
126: .getString("awt.277")); //$NON-NLS-1$
127: }
128:
129: if (dataType != DataBuffer.TYPE_BYTE
130: && dataType != DataBuffer.TYPE_USHORT
131: && dataType != DataBuffer.TYPE_INT) {
132: // awt.230=dataType is not one of the supported data types
133: throw new IllegalArgumentException(Messages
134: .getString("awt.230")); //$NON-NLS-1$
135: }
136:
137: int maxOffset = bandOffsets[0];
138: int maxBank = bankIndices[0];
139:
140: for (int i = 0; i < bankIndices.length; i++) {
141: if (bandOffsets[i] > maxOffset) {
142: maxOffset = bandOffsets[i];
143: }
144: if (bankIndices[i] > maxBank) {
145: maxBank = bankIndices[i];
146: }
147: }
148:
149: int numBanks = maxBank + 1;
150: int dataSize = scanlineStride * (h - 1) + w + maxOffset;
151:
152: DataBuffer data = null;
153:
154: switch (dataType) {
155: case DataBuffer.TYPE_BYTE:
156: data = new DataBufferByte(dataSize, numBanks);
157: break;
158: case DataBuffer.TYPE_USHORT:
159: data = new DataBufferUShort(dataSize, numBanks);
160: break;
161: case DataBuffer.TYPE_INT:
162: data = new DataBufferInt(dataSize, numBanks);
163: break;
164: }
165: return createBandedRaster(data, w, h, scanlineStride,
166: bankIndices, bandOffsets, location);
167: }
168:
169: public static WritableRaster createBandedRaster(int dataType,
170: int w, int h, int bands, Point location) {
171:
172: if (w <= 0 || h <= 0) {
173: // awt.22E=w or h is less than or equal to zero
174: throw new RasterFormatException(Messages
175: .getString("awt.22E")); //$NON-NLS-1$
176: }
177:
178: if (location == null) {
179: location = new Point(0, 0);
180: }
181:
182: if ((long) location.x + w > Integer.MAX_VALUE
183: || (long) location.y + h > Integer.MAX_VALUE) {
184: // awt.276=location.x + w or location.y + h results in integer overflow
185: throw new RasterFormatException(Messages
186: .getString("awt.276")); //$NON-NLS-1$
187: }
188:
189: if (bands < 1) {
190: // awt.279=bands is less than 1
191: throw new ArrayIndexOutOfBoundsException(Messages
192: .getString("awt.279")); //$NON-NLS-1$
193: }
194:
195: int bandOffsets[] = new int[bands];
196: int bankIndices[] = new int[bands];
197:
198: for (int i = 0; i < bands; i++) {
199: bandOffsets[i] = 0;
200: bankIndices[i] = i;
201: }
202: return createBandedRaster(dataType, w, h, w, bankIndices,
203: bandOffsets, location);
204: }
205:
206: public static WritableRaster createInterleavedRaster(
207: DataBuffer dataBuffer, int w, int h, int scanlineStride,
208: int pixelStride, int bandOffsets[], Point location) {
209:
210: if (w <= 0 || h <= 0) {
211: // awt.22E=w or h is less than or equal to zero
212: throw new RasterFormatException(Messages
213: .getString("awt.22E")); //$NON-NLS-1$
214: }
215:
216: if (location == null) {
217: location = new Point(0, 0);
218: }
219:
220: if ((long) location.x + w > Integer.MAX_VALUE
221: || (long) location.y + h > Integer.MAX_VALUE) {
222: // awt.276=location.x + w or location.y + h results in integer overflow
223: throw new RasterFormatException(Messages
224: .getString("awt.276")); //$NON-NLS-1$
225: }
226:
227: if (dataBuffer == null) {
228: // awt.278=dataBuffer is null
229: throw new NullPointerException(Messages
230: .getString("awt.278")); //$NON-NLS-1$
231: }
232:
233: int dataType = dataBuffer.getDataType();
234: if (dataType != DataBuffer.TYPE_BYTE
235: && dataType != DataBuffer.TYPE_USHORT) {
236: // awt.230=dataType is not one of the supported data types
237: throw new IllegalArgumentException(Messages
238: .getString("awt.230")); //$NON-NLS-1$
239: }
240:
241: if (dataBuffer.getNumBanks() > 1) {
242: // awt.27A=dataBuffer has more than one bank
243: throw new RasterFormatException(Messages
244: .getString("awt.27A")); //$NON-NLS-1$
245: }
246:
247: if (bandOffsets == null) {
248: // awt.27B=bandOffsets is null
249: throw new NullPointerException(Messages
250: .getString("awt.27B")); //$NON-NLS-1$
251: }
252:
253: PixelInterleavedSampleModel sampleModel = new PixelInterleavedSampleModel(
254: dataType, w, h, pixelStride, scanlineStride,
255: bandOffsets);
256:
257: return new OrdinaryWritableRaster(sampleModel, dataBuffer,
258: location);
259: }
260:
261: public static WritableRaster createInterleavedRaster(int dataType,
262: int w, int h, int scanlineStride, int pixelStride,
263: int bandOffsets[], Point location) {
264:
265: if (w <= 0 || h <= 0) {
266: // awt.22E=w or h is less than or equal to zero
267: throw new RasterFormatException(Messages
268: .getString("awt.22E")); //$NON-NLS-1$
269: }
270:
271: if (location == null) {
272: location = new Point(0, 0);
273: }
274:
275: if ((long) location.x + w > Integer.MAX_VALUE
276: || (long) location.y + h > Integer.MAX_VALUE) {
277: // awt.276=location.x + w or location.y + h results in integer overflow
278: throw new RasterFormatException(Messages
279: .getString("awt.276")); //$NON-NLS-1$
280: }
281:
282: if (dataType != DataBuffer.TYPE_BYTE
283: && dataType != DataBuffer.TYPE_USHORT) {
284: // awt.230=dataType is not one of the supported data types
285: throw new IllegalArgumentException(Messages
286: .getString("awt.230")); //$NON-NLS-1$
287: }
288:
289: if (bandOffsets == null) {
290: // awt.27B=bandOffsets is null
291: throw new NullPointerException(Messages
292: .getString("awt.27B")); //$NON-NLS-1$
293: }
294:
295: int minOffset = bandOffsets[0];
296: for (int i = 1; i < bandOffsets.length; i++) {
297: if (bandOffsets[i] < minOffset) {
298: minOffset = bandOffsets[i];
299: }
300: }
301: int size = (h - 1) * scanlineStride + w * pixelStride
302: + minOffset;
303: DataBuffer data = null;
304:
305: switch (dataType) {
306: case DataBuffer.TYPE_BYTE:
307: data = new DataBufferByte(size);
308: break;
309: case DataBuffer.TYPE_USHORT:
310: data = new DataBufferUShort(size);
311: break;
312: }
313:
314: return createInterleavedRaster(data, w, h, scanlineStride,
315: pixelStride, bandOffsets, location);
316: }
317:
318: public static WritableRaster createInterleavedRaster(int dataType,
319: int w, int h, int bands, Point location) {
320:
321: if (w <= 0 || h <= 0) {
322: // awt.22E=w or h is less than or equal to zero
323: throw new RasterFormatException(Messages
324: .getString("awt.22E")); //$NON-NLS-1$
325: }
326:
327: if (location == null) {
328: location = new Point(0, 0);
329: }
330:
331: if ((long) location.x + w > Integer.MAX_VALUE
332: || (long) location.y + h > Integer.MAX_VALUE) {
333: // awt.276=location.x + w or location.y + h results in integer overflow
334: throw new RasterFormatException(Messages
335: .getString("awt.276")); //$NON-NLS-1$
336: }
337:
338: if (dataType != DataBuffer.TYPE_BYTE
339: && dataType != DataBuffer.TYPE_USHORT) {
340: // awt.230=dataType is not one of the supported data types
341: throw new IllegalArgumentException(Messages
342: .getString("awt.230")); //$NON-NLS-1$
343: }
344:
345: int bandOffsets[] = new int[bands];
346: for (int i = 0; i < bands; i++) {
347: bandOffsets[i] = i;
348: }
349:
350: return createInterleavedRaster(dataType, w, h, w * bands,
351: bands, bandOffsets, location);
352: }
353:
354: public static WritableRaster createPackedRaster(
355: DataBuffer dataBuffer, int w, int h, int scanlineStride,
356: int bandMasks[], Point location) {
357: if (dataBuffer == null) {
358: // awt.278=dataBuffer is null
359: throw new NullPointerException(Messages
360: .getString("awt.278")); //$NON-NLS-1$
361: }
362:
363: if (w <= 0 || h <= 0) {
364: // awt.22E=w or h is less than or equal to zero
365: throw new RasterFormatException(Messages
366: .getString("awt.22E")); //$NON-NLS-1$
367: }
368:
369: if (location == null) {
370: location = new Point(0, 0);
371: }
372:
373: if ((long) location.x + w > Integer.MAX_VALUE
374: || (long) location.y + h > Integer.MAX_VALUE) {
375: // awt.276=location.x + w or location.y + h results in integer overflow
376: throw new RasterFormatException(Messages
377: .getString("awt.276")); //$NON-NLS-1$
378: }
379:
380: if (bandMasks == null) {
381: // awt.27C=bandMasks is null
382: throw new RasterFormatException(Messages
383: .getString("awt.27C")); //$NON-NLS-1$
384: }
385:
386: if (dataBuffer.getNumBanks() > 1) {
387: // awt.27A=dataBuffer has more than one bank
388: throw new RasterFormatException(Messages
389: .getString("awt.27A")); //$NON-NLS-1$
390: }
391:
392: int dataType = dataBuffer.getDataType();
393: if (dataType != DataBuffer.TYPE_BYTE
394: && dataType != DataBuffer.TYPE_USHORT
395: && dataType != DataBuffer.TYPE_INT) {
396: // awt.230=dataType is not one of the supported data types
397: throw new IllegalArgumentException(Messages
398: .getString("awt.230")); //$NON-NLS-1$
399: }
400:
401: SinglePixelPackedSampleModel sampleModel = new SinglePixelPackedSampleModel(
402: dataType, w, h, scanlineStride, bandMasks);
403:
404: return new OrdinaryWritableRaster(sampleModel, dataBuffer,
405: location);
406: }
407:
408: public static WritableRaster createPackedRaster(
409: DataBuffer dataBuffer, int w, int h, int bitsPerPixel,
410: Point location) {
411:
412: if (w <= 0 || h <= 0) {
413: // awt.22E=w or h is less than or equal to zero
414: throw new RasterFormatException(Messages
415: .getString("awt.22E")); //$NON-NLS-1$
416: }
417:
418: if (location == null) {
419: location = new Point(0, 0);
420: }
421:
422: if ((long) location.x + w > Integer.MAX_VALUE
423: || (long) location.y + h > Integer.MAX_VALUE) {
424: // awt.276=location.x + w or location.y + h results in integer overflow
425: throw new RasterFormatException(Messages
426: .getString("awt.276")); //$NON-NLS-1$
427: }
428:
429: if (dataBuffer == null) {
430: // awt.278=dataBuffer is null
431: throw new NullPointerException(Messages
432: .getString("awt.278")); //$NON-NLS-1$
433: }
434:
435: if (dataBuffer.getNumBanks() > 1) {
436: // awt.27A=dataBuffer has more than one bank
437: throw new RasterFormatException(Messages
438: .getString("awt.27A")); //$NON-NLS-1$
439: }
440:
441: int dataType = dataBuffer.getDataType();
442: if (dataType != DataBuffer.TYPE_BYTE
443: && dataType != DataBuffer.TYPE_USHORT
444: && dataType != DataBuffer.TYPE_INT) {
445: // awt.230=dataType is not one of the supported data types
446: throw new IllegalArgumentException(Messages
447: .getString("awt.230")); //$NON-NLS-1$
448: }
449:
450: MultiPixelPackedSampleModel sampleModel = new MultiPixelPackedSampleModel(
451: dataType, w, h, bitsPerPixel);
452:
453: return new OrdinaryWritableRaster(sampleModel, dataBuffer,
454: location);
455: }
456:
457: public static WritableRaster createPackedRaster(int dataType,
458: int w, int h, int bands, int bitsPerBand, Point location) {
459:
460: if (w <= 0 || h <= 0) {
461: // awt.22E=w or h is less than or equal to zero
462: throw new RasterFormatException(Messages
463: .getString("awt.22E")); //$NON-NLS-1$
464: }
465:
466: if (location == null) {
467: location = new Point(0, 0);
468: }
469:
470: if ((long) location.x + w > Integer.MAX_VALUE
471: || (long) location.y + h > Integer.MAX_VALUE) {
472: // awt.276=location.x + w or location.y + h results in integer overflow
473: throw new RasterFormatException(Messages
474: .getString("awt.276")); //$NON-NLS-1$
475: }
476:
477: if (bands < 1 || bitsPerBand < 1) {
478: // awt.27D=bitsPerBand or bands is not greater than zero
479: throw new IllegalArgumentException(Messages
480: .getString("awt.27D")); //$NON-NLS-1$
481: }
482:
483: if (dataType != DataBuffer.TYPE_BYTE
484: && dataType != DataBuffer.TYPE_USHORT
485: && dataType != DataBuffer.TYPE_INT) {
486: // awt.230=dataType is not one of the supported data types
487: throw new IllegalArgumentException(Messages
488: .getString("awt.230")); //$NON-NLS-1$
489: }
490:
491: if (bitsPerBand * bands > DataBuffer.getDataTypeSize(dataType)) {
492: // awt.27E=The product of bitsPerBand and bands is greater than the number of bits held by dataType
493: throw new IllegalArgumentException(Messages
494: .getString("awt.27E")); //$NON-NLS-1$
495: }
496:
497: if (bands > 1) {
498:
499: int bandMasks[] = new int[bands];
500: int mask = (1 << bitsPerBand) - 1;
501:
502: for (int i = 0; i < bands; i++) {
503: bandMasks[i] = mask << (bitsPerBand * (bands - 1 - i));
504: }
505:
506: return createPackedRaster(dataType, w, h, bandMasks,
507: location);
508: }
509: DataBuffer data = null;
510: int size = ((bitsPerBand * w
511: + DataBuffer.getDataTypeSize(dataType) - 1) / DataBuffer
512: .getDataTypeSize(dataType))
513: * h;
514:
515: switch (dataType) {
516: case DataBuffer.TYPE_BYTE:
517: data = new DataBufferByte(size);
518: break;
519: case DataBuffer.TYPE_USHORT:
520: data = new DataBufferUShort(size);
521: break;
522: case DataBuffer.TYPE_INT:
523: data = new DataBufferInt(size);
524: break;
525: }
526: return createPackedRaster(data, w, h, bitsPerBand, location);
527: }
528:
529: public static WritableRaster createPackedRaster(int dataType,
530: int w, int h, int bandMasks[], Point location) {
531:
532: if (dataType != DataBuffer.TYPE_BYTE
533: && dataType != DataBuffer.TYPE_USHORT
534: && dataType != DataBuffer.TYPE_INT) {
535: // awt.230=dataType is not one of the supported data types
536: throw new IllegalArgumentException(Messages
537: .getString("awt.230")); //$NON-NLS-1$
538: }
539:
540: if (w <= 0 || h <= 0) {
541: // awt.22E=w or h is less than or equal to zero
542: throw new RasterFormatException(Messages
543: .getString("awt.22E")); //$NON-NLS-1$
544: }
545:
546: if (location == null) {
547: location = new Point(0, 0);
548: }
549:
550: if ((long) location.x + w > Integer.MAX_VALUE
551: || (long) location.y + h > Integer.MAX_VALUE) {
552: // awt.276=location.x + w or location.y + h results in integer overflow
553: throw new RasterFormatException(Messages
554: .getString("awt.276")); //$NON-NLS-1$
555: }
556:
557: if (bandMasks == null) {
558: // awt.27C=bandMasks is null
559: throw new NullPointerException(Messages
560: .getString("awt.27C")); //$NON-NLS-1$
561: }
562:
563: DataBuffer data = null;
564:
565: switch (dataType) {
566: case DataBuffer.TYPE_BYTE:
567: data = new DataBufferByte(w * h);
568: break;
569: case DataBuffer.TYPE_USHORT:
570: data = new DataBufferUShort(w * h);
571: break;
572: case DataBuffer.TYPE_INT:
573: data = new DataBufferInt(w * h);
574: break;
575: }
576:
577: return createPackedRaster(data, w, h, w, bandMasks, location);
578: }
579:
580: public static Raster createRaster(SampleModel sm, DataBuffer db,
581: Point location) {
582:
583: if (sm == null || db == null) {
584: // awt.27F=SampleModel or DataBuffer is null
585: throw new NullPointerException(Messages
586: .getString("awt.27F")); //$NON-NLS-1$
587: }
588:
589: if (location == null) {
590: location = new Point(0, 0);
591: }
592:
593: return new Raster(sm, db, location);
594: }
595:
596: public static WritableRaster createWritableRaster(SampleModel sm,
597: DataBuffer db, Point location) {
598:
599: if (sm == null || db == null) {
600: // awt.27F=SampleModel or DataBuffer is null
601: throw new NullPointerException(Messages
602: .getString("awt.27F")); //$NON-NLS-1$
603: }
604:
605: if (location == null) {
606: location = new Point(0, 0);
607: }
608:
609: return new OrdinaryWritableRaster(sm, db, location);
610: }
611:
612: public static WritableRaster createWritableRaster(SampleModel sm,
613: Point location) {
614:
615: if (sm == null) {
616: // awt.280=SampleModel is null
617: throw new NullPointerException(Messages
618: .getString("awt.280")); //$NON-NLS-1$
619: }
620:
621: if (location == null) {
622: location = new Point(0, 0);
623: }
624:
625: return createWritableRaster(sm, sm.createDataBuffer(), location);
626: }
627:
628: protected Raster(SampleModel sampleModel, DataBuffer dataBuffer,
629: Point origin) {
630:
631: this (sampleModel, dataBuffer, new Rectangle(origin.x, origin.y,
632: sampleModel.getWidth(), sampleModel.getHeight()),
633: origin, null);
634: }
635:
636: protected Raster(SampleModel sampleModel, DataBuffer dataBuffer,
637: Rectangle aRegion, Point sampleModelTranslate, Raster parent) {
638:
639: if (sampleModel == null || dataBuffer == null
640: || aRegion == null || sampleModelTranslate == null) {
641: // awt.281=sampleModel, dataBuffer, aRegion or sampleModelTranslate is null
642: throw new NullPointerException(Messages
643: .getString("awt.281")); //$NON-NLS-1$
644: }
645:
646: if (aRegion.width <= 0 || aRegion.height <= 0) {
647: // awt.282=aRegion has width or height less than or equal to zero
648: throw new RasterFormatException(Messages
649: .getString("awt.282")); //$NON-NLS-1$
650: }
651:
652: if ((long) aRegion.x + (long) aRegion.width > Integer.MAX_VALUE) {
653: // awt.283=Overflow X coordinate of Raster
654: throw new RasterFormatException(Messages
655: .getString("awt.283")); //$NON-NLS-1$
656: }
657:
658: if ((long) aRegion.y + (long) aRegion.height > Integer.MAX_VALUE) {
659: // awt.284=Overflow Y coordinate of Raster
660: throw new RasterFormatException(Messages
661: .getString("awt.284")); //$NON-NLS-1$
662: }
663:
664: validateDataBuffer(dataBuffer, aRegion.width, aRegion.height,
665: sampleModel);
666:
667: this .sampleModel = sampleModel;
668: this .dataBuffer = dataBuffer;
669: this .minX = aRegion.x;
670: this .minY = aRegion.y;
671: this .width = aRegion.width;
672: this .height = aRegion.height;
673: this .sampleModelTranslateX = sampleModelTranslate.x;
674: this .sampleModelTranslateY = sampleModelTranslate.y;
675: this .parent = parent;
676: this .numBands = sampleModel.getNumBands();
677: this .numDataElements = sampleModel.getNumDataElements();
678:
679: }
680:
681: protected Raster(SampleModel sampleModel, Point origin) {
682: this (sampleModel, sampleModel.createDataBuffer(),
683: new Rectangle(origin.x, origin.y, sampleModel
684: .getWidth(), sampleModel.getHeight()), origin,
685: null);
686: }
687:
688: public Raster createChild(int parentX, int parentY, int width,
689: int height, int childMinX, int childMinY, int bandList[]) {
690: if (width <= 0 || height <= 0) {
691: // awt.285=Width or Height of child Raster is less than or equal to zero
692: throw new RasterFormatException(Messages
693: .getString("awt.285")); //$NON-NLS-1$
694: }
695:
696: if (parentX < this .minX
697: || parentX + width > this .minX + this .width) {
698: // awt.286=parentX disposes outside Raster
699: throw new RasterFormatException(Messages
700: .getString("awt.286")); //$NON-NLS-1$
701: }
702:
703: if (parentY < this .minY
704: || parentY + height > this .minY + this .height) {
705: // awt.287=parentY disposes outside Raster
706: throw new RasterFormatException(Messages
707: .getString("awt.287")); //$NON-NLS-1$
708: }
709:
710: if ((long) parentX + width > Integer.MAX_VALUE) {
711: // awt.288=parentX + width results in integer overflow
712: throw new RasterFormatException(Messages
713: .getString("awt.288")); //$NON-NLS-1$
714: }
715:
716: if ((long) parentY + height > Integer.MAX_VALUE) {
717: // awt.289=parentY + height results in integer overflow
718: throw new RasterFormatException(Messages
719: .getString("awt.289")); //$NON-NLS-1$
720: }
721:
722: if ((long) childMinX + width > Integer.MAX_VALUE) {
723: // awt.28A=childMinX + width results in integer overflow
724: throw new RasterFormatException(Messages
725: .getString("awt.28A")); //$NON-NLS-1$
726: }
727:
728: if ((long) childMinY + height > Integer.MAX_VALUE) {
729: // awt.28B=childMinY + height results in integer overflow
730: throw new RasterFormatException(Messages
731: .getString("awt.28B")); //$NON-NLS-1$
732: }
733:
734: SampleModel childModel;
735:
736: if (bandList == null) {
737: childModel = sampleModel;
738: } else {
739: childModel = sampleModel.createSubsetSampleModel(bandList);
740: }
741:
742: int childTranslateX = childMinX - parentX;
743: int childTranslateY = childMinY - parentY;
744:
745: return new Raster(childModel, dataBuffer, new Rectangle(
746: childMinX, childMinY, width, height), new Point(
747: childTranslateX + sampleModelTranslateX,
748: childTranslateY + sampleModelTranslateY), this );
749: }
750:
751: public WritableRaster createCompatibleWritableRaster() {
752: return new OrdinaryWritableRaster(sampleModel, new Point(0, 0));
753: }
754:
755: public WritableRaster createCompatibleWritableRaster(int w, int h) {
756: if (w <= 0 || h <= 0) {
757: // awt.22E=w or h is less than or equal to zero
758: throw new RasterFormatException(Messages
759: .getString("awt.22E")); //$NON-NLS-1$
760: }
761:
762: SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
763:
764: return new OrdinaryWritableRaster(sm, new Point(0, 0));
765: }
766:
767: public WritableRaster createCompatibleWritableRaster(int x, int y,
768: int w, int h) {
769:
770: WritableRaster raster = createCompatibleWritableRaster(w, h);
771:
772: return raster.createWritableChild(0, 0, w, h, x, y, null);
773: }
774:
775: public WritableRaster createCompatibleWritableRaster(Rectangle rect) {
776: if (rect == null) {
777: // awt.28C=Rect is null
778: throw new NullPointerException(Messages
779: .getString("awt.28C")); //$NON-NLS-1$
780: }
781:
782: return createCompatibleWritableRaster(rect.x, rect.y,
783: rect.width, rect.height);
784: }
785:
786: public Raster createTranslatedChild(int childMinX, int childMinY) {
787: return createChild(minX, minY, width, height, childMinX,
788: childMinY, null);
789: }
790:
791: public Rectangle getBounds() {
792: return new Rectangle(minX, minY, width, height);
793: }
794:
795: public DataBuffer getDataBuffer() {
796: return dataBuffer;
797: }
798:
799: public Object getDataElements(int x, int y, int w, int h,
800: Object outData) {
801: return sampleModel.getDataElements(x - sampleModelTranslateX, y
802: - sampleModelTranslateY, w, h, outData, dataBuffer);
803: }
804:
805: public Object getDataElements(int x, int y, Object outData) {
806: return sampleModel.getDataElements(x - sampleModelTranslateX, y
807: - sampleModelTranslateY, outData, dataBuffer);
808: }
809:
810: public final int getHeight() {
811: return height;
812: }
813:
814: public final int getMinX() {
815: return minX;
816: }
817:
818: public final int getMinY() {
819: return minY;
820: }
821:
822: public final int getNumBands() {
823: return numBands;
824: }
825:
826: public final int getNumDataElements() {
827: return numDataElements;
828: }
829:
830: public Raster getParent() {
831: return parent;
832: }
833:
834: public double[] getPixel(int x, int y, double dArray[]) {
835: return sampleModel.getPixel(x - sampleModelTranslateX, y
836: - sampleModelTranslateY, dArray, dataBuffer);
837: }
838:
839: public float[] getPixel(int x, int y, float fArray[]) {
840: return sampleModel.getPixel(x - sampleModelTranslateX, y
841: - sampleModelTranslateY, fArray, dataBuffer);
842: }
843:
844: public int[] getPixel(int x, int y, int iArray[]) {
845: return sampleModel.getPixel(x - sampleModelTranslateX, y
846: - sampleModelTranslateY, iArray, dataBuffer);
847: }
848:
849: public double[] getPixels(int x, int y, int w, int h,
850: double dArray[]) {
851: return sampleModel.getPixels(x - sampleModelTranslateX, y
852: - sampleModelTranslateY, w, h, dArray, dataBuffer);
853: }
854:
855: public float[] getPixels(int x, int y, int w, int h, float fArray[]) {
856: return sampleModel.getPixels(x - sampleModelTranslateX, y
857: - sampleModelTranslateY, w, h, fArray, dataBuffer);
858: }
859:
860: public int[] getPixels(int x, int y, int w, int h, int iArray[]) {
861: return sampleModel.getPixels(x - sampleModelTranslateX, y
862: - sampleModelTranslateY, w, h, iArray, dataBuffer);
863: }
864:
865: public int getSample(int x, int y, int b) {
866: return sampleModel.getSample(x - sampleModelTranslateX, y
867: - sampleModelTranslateY, b, dataBuffer);
868: }
869:
870: public double getSampleDouble(int x, int y, int b) {
871: return sampleModel.getSampleDouble(x - sampleModelTranslateX, y
872: - sampleModelTranslateY, b, dataBuffer);
873: }
874:
875: public float getSampleFloat(int x, int y, int b) {
876: return sampleModel.getSampleFloat(x - sampleModelTranslateX, y
877: - sampleModelTranslateY, b, dataBuffer);
878: }
879:
880: public SampleModel getSampleModel() {
881: return sampleModel;
882: }
883:
884: public final int getSampleModelTranslateX() {
885: return sampleModelTranslateX;
886: }
887:
888: public final int getSampleModelTranslateY() {
889: return sampleModelTranslateY;
890: }
891:
892: public double[] getSamples(int x, int y, int w, int h, int b,
893: double dArray[]) {
894:
895: return sampleModel.getSamples(x - sampleModelTranslateX, y
896: - sampleModelTranslateY, w, h, b, dArray, dataBuffer);
897: }
898:
899: public float[] getSamples(int x, int y, int w, int h, int b,
900: float fArray[]) {
901:
902: return sampleModel.getSamples(x - sampleModelTranslateX, y
903: - sampleModelTranslateY, w, h, b, fArray, dataBuffer);
904: }
905:
906: public int[] getSamples(int x, int y, int w, int h, int b,
907: int iArray[]) {
908: return sampleModel.getSamples(x - sampleModelTranslateX, y
909: - sampleModelTranslateY, w, h, b, iArray, dataBuffer);
910: }
911:
912: public final int getTransferType() {
913: return sampleModel.getTransferType();
914: }
915:
916: public final int getWidth() {
917: return width;
918: }
919:
920: private static void validateDataBuffer(final DataBuffer dataBuffer,
921: final int w, final int h, final SampleModel sampleModel) {
922:
923: int size = 0;
924:
925: if (sampleModel instanceof ComponentSampleModel) {
926: ComponentSampleModel csm = (ComponentSampleModel) sampleModel;
927: int offsets[] = csm.getBandOffsets();
928: int maxOffset = offsets[0];
929: for (int i = 1; i < offsets.length; i++) {
930: if (offsets[i] > maxOffset) {
931: maxOffset = offsets[i];
932: }
933: }
934: int scanlineStride = csm.getScanlineStride();
935: int pixelStride = csm.getPixelStride();
936:
937: size = (h - 1) * scanlineStride + (w - 1) * pixelStride
938: + maxOffset + 1;
939:
940: } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
941: MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sampleModel;
942:
943: int scanlineStride = mppsm.getScanlineStride();
944: int dataBitOffset = mppsm.getDataBitOffset();
945: int dataType = dataBuffer.getDataType();
946:
947: size = scanlineStride * h;
948:
949: switch (dataType) {
950: case DataBuffer.TYPE_BYTE:
951: size += (dataBitOffset + 7) / 8;
952: break;
953: case DataBuffer.TYPE_USHORT:
954: size += (dataBitOffset + 15) / 16;
955: break;
956: case DataBuffer.TYPE_INT:
957: size += (dataBitOffset + 31) / 32;
958: break;
959: }
960: } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
961: SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
962:
963: int scanlineStride = sppsm.getScanlineStride();
964: size = (h - 1) * scanlineStride + w;
965: }
966: if (dataBuffer.getSize() < size) {
967: // awt.298=dataBuffer is too small
968: throw new RasterFormatException(Messages
969: .getString("awt.298")); //$NON-NLS-1$
970: }
971: }
972: }
|