001: /*
002: * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.image;
027:
028: import java.awt.image.Raster;
029: import java.awt.image.WritableRaster;
030: import java.awt.image.RasterFormatException;
031: import java.awt.image.SampleModel;
032: import java.awt.image.ComponentSampleModel;
033: import java.awt.image.PixelInterleavedSampleModel;
034: import java.awt.image.SinglePixelPackedSampleModel;
035: import java.awt.image.DataBuffer;
036: import java.awt.image.DataBufferUShort;
037: import java.awt.Rectangle;
038: import java.awt.Point;
039:
040: /**
041: * This class defines a Raster with pixels consisting of one or more 16-bit
042: * data elements stored in close proximity to each other in a short integer
043: * array. The bit precision per data element is that
044: * of the data type (that is, the bit precision for this Raster is 16).
045: * There is only one pixel stride and one scanline stride for all
046: * bands. This type of Raster can be used with a
047: * ComponentColorModel if there are multiple bands, or a
048: * IndexColorModel if there is only one band.
049: * <p>
050: * For example, 5-6-5 RGB image data can be represented by a
051: * ShortInterleavedRaster using a SinglePixelPackedSampleModel and
052: * a ComponentColorModel.
053: *
054: *
055: * @version 10 Feb 1997
056: */
057: public class ShortInterleavedRaster extends ShortComponentRaster {
058:
059: /** A cached copy of minX + width for use in bounds checks. */
060: private int maxX;
061:
062: /** A cached copy of minY + height for use in bounds checks. */
063: private int maxY;
064:
065: /**
066: * Constructs a ShortInterleavedRaster with the given SampleModel.
067: * The Raster's upper left corner is origin and it is the same
068: * size as the SampleModel. A DataBuffer large enough to describe the
069: * Raster is automatically created. SampleModel must be of type
070: * PixelInterleavedSampleModel or SinglePixelPackedSampleModel.
071: * @param sampleModel The SampleModel that specifies the layout.
072: * @param origin The Point that specified the origin.
073: */
074: public ShortInterleavedRaster(SampleModel sampleModel, Point origin) {
075: this (sampleModel, sampleModel.createDataBuffer(),
076: new Rectangle(origin.x, origin.y, sampleModel
077: .getWidth(), sampleModel.getHeight()), origin,
078: null);
079: }
080:
081: /**
082: * Constructs a ShortInterleavedRaster with the given SampleModel
083: * and DataBuffer. The Raster's upper left corner is origin and
084: * it is the same sizes the SampleModel. The DataBuffer is not
085: * initialized and must be a DataBufferUShort compatible with SampleModel.
086: * SampleModel must be of type PixelInterleavedSampleModel or
087: * SinglePixelPackedSampleModel.
088: * @param sampleModel The SampleModel that specifies the layout.
089: * @param dataBuffer The DataBufferUShort that contains the image data.
090: * @param origin The Point that specifies the origin.
091: */
092: public ShortInterleavedRaster(SampleModel sampleModel,
093: DataBuffer dataBuffer, Point origin) {
094: this (sampleModel, dataBuffer, new Rectangle(origin.x, origin.y,
095: sampleModel.getWidth(), sampleModel.getHeight()),
096: origin, null);
097: }
098:
099: /**
100: * Constructs a ShortInterleavedRaster with the given SampleModel,
101: * DataBuffer, and parent. DataBuffer must be a DataBufferUShort and
102: * SampleModel must be of type PixelInterleavedSampleModel or
103: * SinglePixelPackedSampleModel. When translated into the base Raster's
104: * coordinate system, aRegion must be contained by the base Raster.
105: * Origin is the coodinate in the new Raster's coordinate system of
106: * the origin of the base Raster. (The base Raster is the Raster's
107: * ancestor which has no parent.)
108: *
109: * Note that this constructor should generally be called by other
110: * constructors or create methods, it should not be used directly.
111: * @param sampleModel The SampleModel that specifies the layout.
112: * @param dataBuffer The DataBufferUShort that contains the image data.
113: * @param aRegion The Rectangle that specifies the image area.
114: * @param origin The Point that specifies the origin.
115: * @param parent The parent (if any) of this raster.
116: */
117: public ShortInterleavedRaster(SampleModel sampleModel,
118: DataBuffer dataBuffer, Rectangle aRegion, Point origin,
119: ShortInterleavedRaster parent) {
120:
121: super (sampleModel, dataBuffer, aRegion, origin, parent);
122: this .maxX = minX + width;
123: this .maxY = minY + height;
124:
125: if (!(dataBuffer instanceof DataBufferUShort)) {
126: throw new RasterFormatException(
127: "ShortInterleavedRasters must "
128: + "have ushort DataBuffers");
129: }
130:
131: DataBufferUShort dbus = (DataBufferUShort) dataBuffer;
132: this .data = stealData(dbus, 0);
133:
134: // REMIND: need case for interleaved ComponentSampleModel
135: if ((sampleModel instanceof PixelInterleavedSampleModel)
136: || (sampleModel instanceof ComponentSampleModel && sampleModel
137: .getNumBands() == 1)) {
138: ComponentSampleModel csm = (ComponentSampleModel) sampleModel;
139:
140: this .scanlineStride = csm.getScanlineStride();
141: this .pixelStride = csm.getPixelStride();
142: this .dataOffsets = csm.getBandOffsets();
143: int xOffset = aRegion.x - origin.x;
144: int yOffset = aRegion.y - origin.y;
145: for (int i = 0; i < getNumDataElements(); i++) {
146: dataOffsets[i] += xOffset * pixelStride + yOffset
147: * scanlineStride;
148: }
149: } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
150: SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
151: this .scanlineStride = sppsm.getScanlineStride();
152: this .pixelStride = 1;
153: this .dataOffsets = new int[1];
154: this .dataOffsets[0] = dbus.getOffset();
155: int xOffset = aRegion.x - origin.x;
156: int yOffset = aRegion.y - origin.y;
157: dataOffsets[0] += xOffset + yOffset * scanlineStride;
158: } else {
159: throw new RasterFormatException(
160: "ShortInterleavedRasters must "
161: + "have PixelInterleavedSampleModel, SinglePixelPackedSampleModel"
162: + " or 1 band ComponentSampleModel. Sample model is "
163: + sampleModel);
164: }
165: this .bandOffset = this .dataOffsets[0];
166: verify(false);
167: }
168:
169: /**
170: * Returns a copy of the data offsets array. For each band the data offset
171: * is the index into the band's data array, of the first sample of the
172: * band.
173: */
174: public int[] getDataOffsets() {
175: return (int[]) dataOffsets.clone();
176: }
177:
178: /**
179: * Returns the data offset for the specified band. The data offset
180: * is the index into the data array in which the first sample
181: * of the first scanline is stored.
182: * @param band The band whose offset is returned.
183: */
184: public int getDataOffset(int band) {
185: return dataOffsets[band];
186: }
187:
188: /**
189: * Returns the scanline stride -- the number of data array elements between
190: * a given sample and the same sample in the same column of the next row.
191: */
192: public int getScanlineStride() {
193: return scanlineStride;
194: }
195:
196: /**
197: * Returns pixel stride -- the number of data array elements between two
198: * samples for the same band on the same scanline.
199: */
200: public int getPixelStride() {
201: return pixelStride;
202: }
203:
204: /**
205: * Returns a reference to the data array.
206: */
207: public short[] getDataStorage() {
208: return data;
209: }
210:
211: /**
212: * Returns the data elements for all bands at the specified
213: * location.
214: * An ArrayIndexOutOfBounds exception will be thrown at runtime
215: * if the pixel coordinate is out of bounds.
216: * A ClassCastException will be thrown if the input object is non null
217: * and references anything other than an array of transferType.
218: * @param x The X coordinate of the pixel location.
219: * @param y The Y coordinate of the pixel location.
220: * @param outData An object reference to an array of type defined by
221: * getTransferType() and length getNumDataElements().
222: * If null an array of appropriate type and size will be
223: * allocated.
224: * @return An object reference to an array of type defined by
225: * getTransferType() with the request pixel data.
226: */
227: public Object getDataElements(int x, int y, Object obj) {
228: if ((x < this .minX) || (y < this .minY) || (x >= this .maxX)
229: || (y >= this .maxY)) {
230: throw new ArrayIndexOutOfBoundsException(
231: "Coordinate out of bounds!");
232: }
233: short outData[];
234: if (obj == null) {
235: outData = new short[numDataElements];
236: } else {
237: outData = (short[]) obj;
238: }
239: int off = (y - minY) * scanlineStride + (x - minX)
240: * pixelStride;
241:
242: for (int band = 0; band < numDataElements; band++) {
243: outData[band] = data[dataOffsets[band] + off];
244: }
245:
246: return outData;
247: }
248:
249: /**
250: * Returns an array of data elements from the specified rectangular
251: * region.
252: * An ArrayIndexOutOfBounds exception will be thrown at runtime
253: * if the pixel coordinates are out of bounds.
254: * A ClassCastException will be thrown if the input object is non null
255: * and references anything other than an array of transferType.
256: * <pre>
257: * short[] bandData = (short[])Raster.getDataElements(x, y, w, h, null);
258: * int numDataElements = Raster.getBands();
259: * short[] pixel = new short[numDataElements];
260: * // To find the data element at location (x2, y2)
261: * System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
262: * pixel, 0, numDataElements);
263: * </pre>
264: * @param x The X coordinate of the upper left pixel location.
265: * @param y The Y coordinate of the upper left pixel location.
266: * @param width Width of the pixel rectangle.
267: * @param height Height of the pixel rectangle.
268: * @param outData An object reference to an array of type defined by
269: * getTransferType() and length w*h*getNumDataElements().
270: * If null an array of appropriate type and size will be
271: * allocated.
272: * @return An object reference to an array of type defined by
273: * getTransferType() with the request pixel data.
274: */
275: public Object getDataElements(int x, int y, int w, int h, Object obj) {
276: if ((x < this .minX) || (y < this .minY) || (x + w > this .maxX)
277: || (y + h > this .maxY)) {
278: throw new ArrayIndexOutOfBoundsException(
279: "Coordinate out of bounds!");
280: }
281: short outData[];
282: if (obj == null) {
283: outData = new short[w * h * numDataElements];
284: } else {
285: outData = (short[]) obj;
286: }
287: int yoff = (y - minY) * scanlineStride + (x - minX)
288: * pixelStride;
289:
290: int xoff;
291: int off = 0;
292: int xstart;
293: int ystart;
294:
295: for (ystart = 0; ystart < h; ystart++, yoff += scanlineStride) {
296: xoff = yoff;
297: for (xstart = 0; xstart < w; xstart++, xoff += pixelStride) {
298: for (int c = 0; c < numDataElements; c++) {
299: outData[off++] = data[dataOffsets[c] + xoff];
300: }
301: }
302: }
303:
304: return outData;
305: }
306:
307: /**
308: * Returns a short integer array of data elements from the
309: * specified rectangular region.
310: * An ArrayIndexOutOfBounds exception will be thrown at runtime
311: * if the pixel coordinates are out of bounds.
312: * <pre>
313: * short[] bandData = Raster.getShortData(x, y, w, h, null);
314: * // To find the data element at location (x2, y2)
315: * short dataElenent = bandData[((y2-y)*w + (x2-x))];
316: * </pre>
317: * @param x The X coordinate of the upper left pixel location.
318: * @param y The Y coordinate of the upper left pixel location.
319: * @param width Width of the sample rectangle.
320: * @param height Height of the sample rectangle.
321: * @param band The band to return.
322: * @param outData If non-null, data elements for all bands
323: * at the specified location are returned in this array.
324: * @return Data array with data elements for all bands.
325: */
326: public short[] getShortData(int x, int y, int w, int h, int band,
327: short[] outData) {
328: // Bounds check for 'band' will be performed automatically
329: if ((x < this .minX) || (y < this .minY) || (x + w > this .maxX)
330: || (y + h > this .maxY)) {
331: throw new ArrayIndexOutOfBoundsException(
332: "Coordinate out of bounds!");
333: }
334: if (outData == null) {
335: outData = new short[numDataElements * w * h];
336: }
337: int yoff = (y - minY) * scanlineStride + (x - minX)
338: * pixelStride + dataOffsets[band];
339: int xoff;
340: int off = 0;
341: int xstart;
342: int ystart;
343:
344: if (pixelStride == 1) {
345: if (scanlineStride == w) {
346: System.arraycopy(data, yoff, outData, 0, w * h);
347: } else {
348: for (ystart = 0; ystart < h; ystart++, yoff += scanlineStride) {
349: System.arraycopy(data, yoff, outData, off, w);
350: off += w;
351: }
352: }
353: } else {
354: for (ystart = 0; ystart < h; ystart++, yoff += scanlineStride) {
355: xoff = yoff;
356: for (xstart = 0; xstart < w; xstart++, xoff += pixelStride) {
357: outData[off++] = data[xoff];
358: }
359: }
360: }
361:
362: return outData;
363: }
364:
365: /**
366: * Returns a short integer array of data elements from the
367: * specified rectangular region.
368: * An ArrayIndexOutOfBounds exception will be thrown at runtime
369: * if the pixel coordinates are out of bounds.
370: * <pre>
371: * short[] bandData = Raster.getShortData(x, y, w, h, null);
372: * int numDataElements = Raster.getNumBands();
373: * short[] pixel = new short[numDataElements];
374: * // To find the data element at location (x2, y2)
375: * System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
376: * pixel, 0, numDataElements);
377: * </pre>
378: * @param x The X coordinate of the upper left pixel location.
379: * @param y The Y coordinate of the upper left pixel location.
380: * @param width Width of the pixel rectangle.
381: * @param height Height of the pixel rectangle.
382: * @param outData If non-null, data elements for all bands
383: * at the specified location are returned in this array.
384: * @return Data array with data elements for all bands.
385: */
386: public short[] getShortData(int x, int y, int w, int h,
387: short[] outData) {
388: if ((x < this .minX) || (y < this .minY) || (x + w > this .maxX)
389: || (y + h > this .maxY)) {
390: throw new ArrayIndexOutOfBoundsException(
391: "Coordinate out of bounds!");
392: }
393: if (outData == null) {
394: outData = new short[numDataElements * w * h];
395: }
396: int yoff = (y - minY) * scanlineStride + (x - minX)
397: * pixelStride;
398: int xoff;
399: int off = 0;
400: int xstart;
401: int ystart;
402:
403: for (ystart = 0; ystart < h; ystart++, yoff += scanlineStride) {
404: xoff = yoff;
405: for (xstart = 0; xstart < w; xstart++, xoff += pixelStride) {
406: for (int c = 0; c < numDataElements; c++) {
407: outData[off++] = data[dataOffsets[c] + xoff];
408: }
409: }
410: }
411:
412: return outData;
413: }
414:
415: /**
416: * Stores the data elements for all bands at the specified location.
417: * An ArrayIndexOutOfBounds exception will be thrown at runtime
418: * if the pixel coordinate is out of bounds.
419: * A ClassCastException will be thrown if the input object is non null
420: * and references anything other than an array of transferType.
421: * @param x The X coordinate of the pixel location.
422: * @param y The Y coordinate of the pixel location.
423: * @param inData An object reference to an array of type defined by
424: * getTransferType() and length getNumDataElements()
425: * containing the pixel data to place at x,y.
426: */
427: public void setDataElements(int x, int y, Object obj) {
428: if ((x < this .minX) || (y < this .minY) || (x >= this .maxX)
429: || (y >= this .maxY)) {
430: throw new ArrayIndexOutOfBoundsException(
431: "Coordinate out of bounds!");
432: }
433: short inData[] = (short[]) obj;
434: int off = (y - minY) * scanlineStride + (x - minX)
435: * pixelStride;
436: for (int i = 0; i < numDataElements; i++) {
437: data[dataOffsets[i] + off] = (short) inData[i];
438: }
439: markDirty();
440: }
441:
442: /**
443: * Stores the Raster data at the specified location.
444: * An ArrayIndexOutOfBounds exception will be thrown at runtime
445: * if the pixel coordinates are out of bounds.
446: * @param x The X coordinate of the pixel location.
447: * @param y The Y coordinate of the pixel location.
448: * @param inRaster Raster of data to place at x,y location.
449: */
450: public void setDataElements(int x, int y, Raster inRaster) {
451: int dstOffX = x + inRaster.getMinX();
452: int dstOffY = y + inRaster.getMinY();
453: int width = inRaster.getWidth();
454: int height = inRaster.getHeight();
455: if ((dstOffX < this .minX) || (dstOffY < this .minY)
456: || (dstOffX + width > this .maxX)
457: || (dstOffY + height > this .maxY)) {
458: throw new ArrayIndexOutOfBoundsException(
459: "Coordinate out of bounds!");
460: }
461:
462: setDataElements(dstOffX, dstOffY, width, height, inRaster);
463: }
464:
465: /**
466: * Stores the Raster data at the specified location.
467: * @param dstX The absolute X coordinate of the destination pixel
468: * that will receive a copy of the upper-left pixel of the
469: * inRaster
470: * @param dstY The absolute Y coordinate of the destination pixel
471: * that will receive a copy of the upper-left pixel of the
472: * inRaster
473: * @param width The number of pixels to store horizontally
474: * @param height The number of pixels to store vertically
475: * @param inRaster Raster of data to place at x,y location.
476: */
477: private void setDataElements(int dstX, int dstY, int width,
478: int height, Raster inRaster) {
479: // Assume bounds checking has been performed previously
480: if (width <= 0 || height <= 0) {
481: return;
482: }
483:
484: // Write inRaster (minX, minY) to (dstX, dstY)
485:
486: int srcOffX = inRaster.getMinX();
487: int srcOffY = inRaster.getMinY();
488: Object tdata = null;
489:
490: // REMIND: Do something faster!
491: // if (inRaster instanceof ShortInterleavedRaster) {
492: // }
493:
494: for (int startY = 0; startY < height; startY++) {
495: // Grab one scanline at a time
496: tdata = inRaster.getDataElements(srcOffX, srcOffY + startY,
497: width, 1, tdata);
498: setDataElements(dstX, dstY + startY, width, 1, tdata);
499: }
500: }
501:
502: /**
503: * Stores an array of data elements into the specified rectangular
504: * region.
505: * An ArrayIndexOutOfBounds exception will be thrown at runtime
506: * if the pixel coordinates are out of bounds.
507: * A ClassCastException will be thrown if the input object is non null
508: * and references anything other than an array of transferType.
509: * The data elements in the
510: * data array are assumed to be packed. That is, a data element
511: * for the nth band at location (x2, y2) would be found at:
512: * <pre>
513: * inData[((y2-y)*w + (x2-x))*numDataElements + n]
514: * </pre>
515: * @param x The X coordinate of the upper left pixel location.
516: * @param y The Y coordinate of the upper left pixel location.
517: * @param w Width of the pixel rectangle.
518: * @param h Height of the pixel rectangle.
519: * @param inData An object reference to an array of type defined by
520: * getTransferType() and length w*h*getNumDataElements()
521: * containing the pixel data to place between x,y and
522: * x+h, y+h.
523: */
524: public void setDataElements(int x, int y, int w, int h, Object obj) {
525: if ((x < this .minX) || (y < this .minY) || (x + w > this .maxX)
526: || (y + h > this .maxY)) {
527: throw new ArrayIndexOutOfBoundsException(
528: "Coordinate out of bounds!");
529: }
530: short inData[] = (short[]) obj;
531: int yoff = (y - minY) * scanlineStride + (x - minX)
532: * pixelStride;
533: int xoff;
534: int off = 0;
535: int xstart;
536: int ystart;
537:
538: for (ystart = 0; ystart < h; ystart++, yoff += scanlineStride) {
539: xoff = yoff;
540: for (xstart = 0; xstart < w; xstart++, xoff += pixelStride) {
541: for (int c = 0; c < numDataElements; c++) {
542: data[dataOffsets[c] + xoff] = (short) inData[off++];
543: }
544: }
545: }
546:
547: markDirty();
548: }
549:
550: /**
551: * Stores a short integer array of data elements into the
552: * specified rectangular region.
553: * An ArrayIndexOutOfBounds exception will be thrown at runtime
554: * if the pixel coordinates are out of bounds.
555: * The data elements in the
556: * data array are assumed to be packed. That is, a data element
557: * at location (x2, y2) would be found at:
558: * <pre>
559: * inData[((y2-y)*w + (x2-x))]
560: * </pre>
561: * @param x The X coordinate of the upper left pixel location.
562: * @param y The Y coordinate of the upper left pixel location.
563: * @param w Width of the pixel rectangle.
564: * @param h Height of the pixel rectangle.
565: * @param band The band to set.
566: * @param inData The data elements to be stored.
567: */
568: public void putShortData(int x, int y, int w, int h, int band,
569: short[] inData) {
570: // Bounds check for 'band' will be performed automatically
571: if ((x < this .minX) || (y < this .minY) || (x + w > this .maxX)
572: || (y + h > this .maxY)) {
573: throw new ArrayIndexOutOfBoundsException(
574: "Coordinate out of bounds!");
575: }
576: int yoff = (y - minY) * scanlineStride + (x - minX)
577: * pixelStride + dataOffsets[band];
578: int xoff;
579: int off = 0;
580: int xstart;
581: int ystart;
582:
583: if (pixelStride == 1) {
584: if (scanlineStride == w) {
585: System.arraycopy(inData, 0, data, yoff, w * h);
586: } else {
587: for (ystart = 0; ystart < h; ystart++, yoff += scanlineStride) {
588: System.arraycopy(inData, off, data, yoff, w);
589: off += w;
590: }
591: }
592: } else {
593: for (ystart = 0; ystart < h; ystart++, yoff += scanlineStride) {
594: xoff = yoff;
595: for (xstart = 0; xstart < w; xstart++, xoff += pixelStride) {
596: data[xoff] = inData[off++];
597: }
598: }
599: }
600:
601: markDirty();
602: }
603:
604: /**
605: * Stores a short integer array of data elements into the
606: * specified rectangular region.
607: * An ArrayIndexOutOfBounds exception will be thrown at runtime
608: * if the pixel coordinates are out of bounds.
609: * The data elements in the
610: * data array are assumed to be packed. That is, a data element
611: * for the nth band at location (x2, y2) would be found at:
612: * <pre>
613: * inData[((y2-y)*w + (x2-x))*numDataElements + n]
614: * </pre>
615: * @param x The X coordinate of the upper left pixel location.
616: * @param y The Y coordinate of the upper left pixel location.
617: * @param w Width of the pixel rectangle.
618: * @param h Height of the pixel rectangle.
619: * @param inData The data elements to be stored.
620: */
621: public void putShortData(int x, int y, int w, int h, short[] inData) {
622: if ((x < this .minX) || (y < this .minY) || (x + w > this .maxX)
623: || (y + h > this .maxY)) {
624: throw new ArrayIndexOutOfBoundsException(
625: "Coordinate out of bounds!");
626: }
627: int yoff = (y - minY) * scanlineStride + (x - minX)
628: * pixelStride;
629: int xoff;
630: int off = 0;
631: int xstart;
632: int ystart;
633:
634: for (ystart = 0; ystart < h; ystart++, yoff += scanlineStride) {
635: xoff = yoff;
636: for (xstart = 0; xstart < w; xstart++, xoff += pixelStride) {
637: for (int c = 0; c < numDataElements; c++) {
638: data[dataOffsets[c] + xoff] = inData[off++];
639: }
640: }
641: }
642:
643: markDirty();
644: }
645:
646: /**
647: * Creates a subraster given a region of the raster. The x and y
648: * coordinates specify the horizontal and vertical offsets
649: * from the upper-left corner of this raster to the upper-left corner
650: * of the subraster. A subset of the bands of the parent Raster may
651: * be specified. If this is null, then all the bands are present in the
652: * subRaster. A translation to the subRaster may also be specified.
653: * Note that the subraster will reference the same
654: * band objects as the parent raster, but using different offsets.
655: * @param x X offset.
656: * @param y Y offset.
657: * @param width Width (in pixels) of the subraster.
658: * @param height Height (in pixels) of the subraster.
659: * @param x0 Translated X origin of the subraster.
660: * @param y0 Translated Y origin of the subraster.
661: * @param bandList Array of band indices.
662: * @exception RasterFormatException
663: * if the specified bounding box is outside of the parent raster.
664: */
665: public Raster createChild(int x, int y, int width, int height,
666: int x0, int y0, int[] bandList) {
667: WritableRaster newRaster = createWritableChild(x, y, width,
668: height, x0, y0, bandList);
669: return (Raster) newRaster;
670: }
671:
672: /**
673: * Creates a Writable subRaster given a region of the Raster. The x and y
674: * coordinates specify the horizontal and vertical offsets
675: * from the upper-left corner of this Raster to the upper-left corner
676: * of the subRaster. A subset of the bands of the parent Raster may
677: * be specified. If this is null, then all the bands are present in the
678: * subRaster. A translation to the subRaster may also be specified.
679: * Note that the subRaster will reference the same
680: * DataBuffers as the parent Raster, but using different offsets.
681: * @param x X offset.
682: * @param y Y offset.
683: * @param width Width (in pixels) of the subraster.
684: * @param height Height (in pixels) of the subraster.
685: * @param x0 Translated X origin of the subraster.
686: * @param y0 Translated Y origin of the subraster.
687: * @param bandList Array of band indices.
688: * @exception RasterFormatException
689: * if the specified bounding box is outside of the parent Raster.
690: */
691: public WritableRaster createWritableChild(int x, int y, int width,
692: int height, int x0, int y0, int[] bandList) {
693: if (x < this .minX) {
694: throw new RasterFormatException("x lies outside the raster");
695: }
696: if (y < this .minY) {
697: throw new RasterFormatException("y lies outside the raster");
698: }
699: if ((x + width < x) || (x + width > this .minX + this .width)) {
700: throw new RasterFormatException(
701: "(x + width) is outside of Raster");
702: }
703: if ((y + height < y) || (y + height > this .minY + this .height)) {
704: throw new RasterFormatException(
705: "(y + height) is outside of Raster");
706: }
707:
708: SampleModel sm;
709:
710: if (bandList != null)
711: sm = sampleModel.createSubsetSampleModel(bandList);
712: else
713: sm = sampleModel;
714:
715: int deltaX = x0 - x;
716: int deltaY = y0 - y;
717:
718: return new ShortInterleavedRaster(sm, dataBuffer,
719: new Rectangle(x0, y0, width, height), new Point(
720: sampleModelTranslateX + deltaX,
721: sampleModelTranslateY + deltaY), this );
722: }
723:
724: /**
725: * Creates a Raster with the same layout but using a different
726: * width and height, and with new zeroed data arrays.
727: */
728: public WritableRaster createCompatibleWritableRaster(int w, int h) {
729: if (w <= 0 || h <= 0) {
730: throw new RasterFormatException("negative "
731: + ((w <= 0) ? "width" : "height"));
732: }
733:
734: SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
735:
736: return new ShortInterleavedRaster(sm, new Point(0, 0));
737: }
738:
739: /**
740: * Creates a Raster with the same layout and the same
741: * width and height, and with new zeroed data arrays. If
742: * the Raster is a subRaster, this will call
743: * createCompatibleRaster(width, height).
744: */
745: public WritableRaster createCompatibleWritableRaster() {
746: return createCompatibleWritableRaster(width, height);
747: }
748:
749: /**
750: * Verify that the layout parameters are consistent with
751: * the data. If strictCheck
752: * is false, this method will check for ArrayIndexOutOfBounds conditions. If
753: * strictCheck is true, this method will check for additional error
754: * conditions such as line wraparound (width of a line greater than
755: * the scanline stride).
756: * @return String Error string, if the layout is incompatible with
757: * the data. Otherwise returns null.
758: */
759: private void verify(boolean strictCheck) {
760: int maxSize = 0;
761: int size;
762:
763: for (int i = 0; i < numDataElements; i++) {
764: size = (height - 1) * scanlineStride + (width - 1)
765: * pixelStride + dataOffsets[i];
766: if (size > maxSize) {
767: maxSize = size;
768: }
769: }
770: if (data.length < maxSize) {
771: throw new RasterFormatException(
772: "Data array too small (should be " + maxSize + " )");
773: }
774: }
775:
776: public String toString() {
777: return new String("ShortInterleavedRaster: width = " + width
778: + " height = " + height + " #numDataElements "
779: + numDataElements);
780: // +" xOff = "+xOffset+" yOff = "+yOffset);
781: }
782:
783: }
|