KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > BandedSampleModel


1 /*
2  * @(#)BandedSampleModel.java 1.35 04/01/06
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /* ****************************************************************
9  ******************************************************************
10  ******************************************************************
11  *** COPYRIGHT (c) Eastman Kodak Company, 1997
12  *** As an unpublished work pursuant to Title 17 of the United
13  *** States Code. All rights reserved.
14  ******************************************************************
15  ******************************************************************
16  ******************************************************************/

17
18 package java.awt.image;
19
20 /**
21  * This class represents image data which is stored in a band interleaved
22  * fashion and for
23  * which each sample of a pixel occupies one data element of the DataBuffer.
24  * It subclasses ComponentSampleModel but provides a more efficent
25  * implementation for accessing band interleaved image data than is provided
26  * by ComponentSampleModel. This class should typically be used when working
27  * with images which store sample data for each band in a different bank of the
28  * DataBuffer. Accessor methods are provided so that image data can be
29  * manipulated directly. Pixel stride is the number of
30  * data array elements between two samples for the same band on the same
31  * scanline. The pixel stride for a BandedSampleModel is one.
32  * Scanline stride is the number of data array elements between
33  * a given sample and the corresponding sample in the same column of the next
34  * scanline. Band offsets denote the number
35  * of data array elements from the first data array element of the bank
36  * of the DataBuffer holding each band to the first sample of the band.
37  * The bands are numbered from 0 to N-1.
38  * Bank indices denote the correspondence between a bank of the data buffer
39  * and a band of image data. This class supports
40  * {@link DataBuffer#TYPE_BYTE TYPE_BYTE},
41  * {@link DataBuffer#TYPE_USHORT TYPE_USHORT},
42  * {@link DataBuffer#TYPE_SHORT TYPE_SHORT},
43  * {@link DataBuffer#TYPE_INT TYPE_INT},
44  * {@link DataBuffer#TYPE_FLOAT TYPE_FLOAT}, and
45  * {@link DataBuffer#TYPE_DOUBLE TYPE_DOUBLE} datatypes
46  */

47
48
49 public final class BandedSampleModel extends ComponentSampleModel JavaDoc
50 {
51
52     /**
53      * Constructs a BandedSampleModel with the specified parameters.
54      * The pixel stride will be one data element. The scanline stride
55      * will be the same as the width. Each band will be stored in
56      * a separate bank and all band offsets will be zero.
57      * @param dataType The data type for storing samples.
58      * @param w The width (in pixels) of the region of
59      * image data described.
60      * @param h The height (in pixels) of the region of image
61      * data described.
62      * @param numBands The number of bands for the image data.
63      * @throws IllegalArgumentException if <code>dataType</code> is not
64      * one of the supported data types
65      */

66     public BandedSampleModel(int dataType, int w, int h, int numBands) {
67     super(dataType, w, h, 1, w,
68               BandedSampleModel.createIndicesArray(numBands),
69               BandedSampleModel.createOffsetArray(numBands));
70     }
71
72     /**
73      * Constructs a BandedSampleModel with the specified parameters.
74      * The number of bands will be inferred from the lengths of the
75      * bandOffsets bankIndices arrays, which must be equal. The pixel
76      * stride will be one data element.
77      * @param dataType The data type for storing samples.
78      * @param w The width (in pixels) of the region of
79      * image data described.
80      * @param h The height (in pixels) of the region of
81      * image data described.
82      * @param scanlineStride The line stride of the of the image data.
83      * @param bankIndices The bank index for each band.
84      * @param bandOffsets The band offset for each band.
85      * @throws IllegalArgumentException if <code>dataType</code> is not
86      * one of the supported data types
87      */

88     public BandedSampleModel(int dataType,
89                  int w, int h,
90                  int scanlineStride,
91                  int bankIndices[],
92                              int bandOffsets[]) {
93
94     super(dataType, w, h, 1,scanlineStride, bankIndices, bandOffsets);
95     }
96
97     /**
98      * Creates a new BandedSampleModel with the specified
99      * width and height. The new BandedSampleModel will have the same
100      * number of bands, storage data type, and bank indices
101      * as this BandedSampleModel. The band offsets will be compressed
102      * such that the offset between bands will be w*pixelStride and
103      * the minimum of all of the band offsets is zero.
104      * @param w the width of the resulting <code>BandedSampleModel</code>
105      * @param h the height of the resulting <code>BandedSampleModel</code>
106      * @return a new <code>BandedSampleModel</code> with the specified
107      * width and height.
108      * @throws IllegalArgumentException if <code>w</code> or
109      * <code>h</code> equals either
110      * <code>Integer.MAX_VALUE</code> or
111      * <code>Integer.MIN_VALUE</code>
112      * @throws IllegalArgumentException if <code>dataType</code> is not
113      * one of the supported data types
114      */

115     public SampleModel JavaDoc createCompatibleSampleModel(int w, int h) {
116         int[] bandOffs;
117
118         if (numBanks == 1) {
119             bandOffs = orderBands(bandOffsets, w*h);
120         }
121         else {
122             bandOffs = new int[bandOffsets.length];
123         }
124
125         SampleModel JavaDoc sampleModel =
126             new BandedSampleModel JavaDoc(dataType, w, h, w, bankIndices, bandOffs);
127     return sampleModel;
128     }
129
130     /**
131      * Creates a new BandedSampleModel with a subset of the bands of this
132      * BandedSampleModel. The new BandedSampleModel can be
133      * used with any DataBuffer that the existing BandedSampleModel
134      * can be used with. The new BandedSampleModel/DataBuffer
135      * combination will represent an image with a subset of the bands
136      * of the original BandedSampleModel/DataBuffer combination.
137      * @throws RasterFormatException if the number of bands is greater than
138      * the number of banks in this sample model.
139      * @throws IllegalArgumentException if <code>dataType</code> is not
140      * one of the supported data types
141      */

142     public SampleModel JavaDoc createSubsetSampleModel(int bands[]) {
143     if (bands.length > bankIndices.length)
144         throw new RasterFormatException JavaDoc("There are only " +
145                         bankIndices.length +
146                         " bands");
147     int newBankIndices[] = new int[bands.length];
148     int newBandOffsets[] = new int[bands.length];
149
150     for (int i=0; i<bands.length; i++) {
151         newBankIndices[i] = bankIndices[bands[i]];
152         newBandOffsets[i] = bandOffsets[bands[i]];
153         }
154
155         return new BandedSampleModel JavaDoc(this.dataType, width, height,
156                      this.scanlineStride,
157                      newBankIndices, newBandOffsets);
158     }
159
160     /**
161      * Creates a DataBuffer that corresponds to this BandedSampleModel,
162      * The DataBuffer's data type, number of banks, and size
163      * will be consistent with this BandedSampleModel.
164      * @throws IllegalArgumentException if <code>dataType</code> is not
165      * one of the supported types.
166      */

167     public DataBuffer JavaDoc createDataBuffer() {
168         DataBuffer JavaDoc dataBuffer = null;
169
170     int size = scanlineStride * height;
171         switch (dataType) {
172         case DataBuffer.TYPE_BYTE:
173             dataBuffer = new DataBufferByte JavaDoc(size, numBanks);
174             break;
175         case DataBuffer.TYPE_USHORT:
176             dataBuffer = new DataBufferUShort JavaDoc(size, numBanks);
177             break;
178         case DataBuffer.TYPE_SHORT:
179             dataBuffer = new DataBufferShort JavaDoc(size, numBanks);
180             break;
181         case DataBuffer.TYPE_INT:
182             dataBuffer = new DataBufferInt JavaDoc(size, numBanks);
183             break;
184         case DataBuffer.TYPE_FLOAT:
185             dataBuffer = new DataBufferFloat JavaDoc(size, numBanks);
186             break;
187         case DataBuffer.TYPE_DOUBLE:
188             dataBuffer = new DataBufferDouble JavaDoc(size, numBanks);
189             break;
190         default:
191             throw new IllegalArgumentException JavaDoc("dataType is not one " +
192                 "of the supported types.");
193         }
194
195         return dataBuffer;
196     }
197
198
199     /**
200      * Returns data for a single pixel in a primitive array of type
201      * TransferType. For a BandedSampleModel, this will be the same
202      * as the data type, and samples will be returned one per array
203      * element. Generally, obj
204      * should be passed in as null, so that the Object will be created
205      * automatically and will be of the right primitive data type.
206      * <p>
207      * The following code illustrates transferring data for one pixel from
208      * DataBuffer <code>db1</code>, whose storage layout is described by
209      * BandedSampleModel <code>bsm1</code>, to DataBuffer <code>db2</code>,
210      * whose storage layout is described by
211      * BandedSampleModel <code>bsm2</code>.
212      * The transfer will generally be more efficient than using
213      * getPixel/setPixel.
214      * <pre>
215      * BandedSampleModel bsm1, bsm2;
216      * DataBufferInt db1, db2;
217      * bsm2.setDataElements(x, y, bsm1.getDataElements(x, y, null, db1),
218      * db2);
219      * </pre>
220      * Using getDataElements/setDataElements to transfer between two
221      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
222      * the same number of bands, corresponding bands have the same number of
223      * bits per sample, and the TransferTypes are the same.
224      * <p>
225      * If obj is non-null, it should be a primitive array of type TransferType.
226      * Otherwise, a ClassCastException is thrown. An
227      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
228      * not in bounds, or if obj is non-null and is not large enough to hold
229      * the pixel data.
230      * @param x,&nbsp;y The coordinates of the pixel location
231      * @param obj If non-null, a primitive array in which to return
232      * the pixel data.
233      * @param data The DataBuffer containing the image data.
234      * @return the data for the specified pixel.
235      * @see #setDataElements(int, int, Object, DataBuffer)
236      */

237     public Object JavaDoc getDataElements(int x, int y, Object JavaDoc obj, DataBuffer JavaDoc data) {
238         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
239             throw new ArrayIndexOutOfBoundsException JavaDoc
240                 ("Coordinate out of bounds!");
241         }
242     int type = getTransferType();
243     int numDataElems = getNumDataElements();
244     int pixelOffset = y*scanlineStride + x;
245
246     switch(type) {
247
248     case DataBuffer.TYPE_BYTE:
249
250         byte[] bdata;
251
252         if (obj == null) {
253         bdata = new byte[numDataElems];
254             } else {
255         bdata = (byte[])obj;
256             }
257
258         for (int i=0; i<numDataElems; i++) {
259         bdata[i] = (byte)data.getElem(bankIndices[i],
260                                               pixelOffset + bandOffsets[i]);
261         }
262
263         obj = (Object JavaDoc)bdata;
264         break;
265
266     case DataBuffer.TYPE_USHORT:
267         case DataBuffer.TYPE_SHORT:
268             
269         short[] sdata;
270
271         if (obj == null) {
272         sdata = new short[numDataElems];
273         } else {
274         sdata = (short[])obj;
275             }
276
277         for (int i=0; i<numDataElems; i++) {
278         sdata[i] = (short)data.getElem(bankIndices[i],
279                                                pixelOffset + bandOffsets[i]);
280         }
281
282         obj = (Object JavaDoc)sdata;
283         break;
284
285     case DataBuffer.TYPE_INT:
286
287         int[] idata;
288
289         if (obj == null) {
290         idata = new int[numDataElems];
291             } else {
292         idata = (int[])obj;
293             }
294
295         for (int i=0; i<numDataElems; i++) {
296         idata[i] = data.getElem(bankIndices[i],
297                                         pixelOffset + bandOffsets[i]);
298         }
299
300         obj = (Object JavaDoc)idata;
301         break;
302
303     case DataBuffer.TYPE_FLOAT:
304
305         float[] fdata;
306
307         if (obj == null) {
308         fdata = new float[numDataElems];
309             } else {
310         fdata = (float[])obj;
311             }
312
313         for (int i=0; i<numDataElems; i++) {
314         fdata[i] = data.getElemFloat(bankIndices[i],
315                                              pixelOffset + bandOffsets[i]);
316         }
317
318         obj = (Object JavaDoc)fdata;
319         break;
320
321     case DataBuffer.TYPE_DOUBLE:
322
323         double[] ddata;
324
325         if (obj == null) {
326         ddata = new double[numDataElems];
327             } else {
328         ddata = (double[])obj;
329             }
330
331         for (int i=0; i<numDataElems; i++) {
332         ddata[i] = data.getElemDouble(bankIndices[i],
333                                               pixelOffset + bandOffsets[i]);
334         }
335
336         obj = (Object JavaDoc)ddata;
337         break;
338     }
339
340     return obj;
341     }
342
343     /**
344      * Returns all samples for the specified pixel in an int array.
345      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
346      * not in bounds.
347      * @param x,&nbsp;y The coordinates of the pixel location
348      * @param iArray If non-null, returns the samples in this array
349      * @param data The DataBuffer containing the image data
350      * @return the samples for the specified pixel.
351      * @see #setPixel(int, int, int[], DataBuffer)
352      */

353     public int[] getPixel(int x, int y, int iArray[], DataBuffer JavaDoc data) {
354         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
355             throw new ArrayIndexOutOfBoundsException JavaDoc
356                 ("Coordinate out of bounds!");
357         }
358
359         int[] pixels;
360
361     if (iArray != null) {
362            pixels = iArray;
363         } else {
364            pixels = new int [numBands];
365         }
366
367         int pixelOffset = y*scanlineStride + x;
368         for (int i=0; i<numBands; i++) {
369             pixels[i] = data.getElem(bankIndices[i],
370                                      pixelOffset + bandOffsets[i]);
371         }
372         return pixels;
373     }
374
375     /**
376      * Returns all samples for the specified rectangle of pixels in
377      * an int array, one sample per data array element.
378      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
379      * not in bounds.
380      * @param x,&nbsp;y The coordinates of the upper left pixel location
381      * @param w The width of the pixel rectangle
382      * @param h The height of the pixel rectangle
383      * @param iArray If non-null, returns the samples in this array
384      * @param data The DataBuffer containing the image data
385      * @return the samples for the pixels within the specified region.
386      * @see #setPixels(int, int, int, int, int[], DataBuffer)
387      */

388     public int[] getPixels(int x, int y, int w, int h,
389                            int iArray[], DataBuffer JavaDoc data) {
390         if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
391             throw new ArrayIndexOutOfBoundsException JavaDoc
392                 ("Coordinate out of bounds!");
393         }
394         int[] pixels;
395
396         if (iArray != null) {
397            pixels = iArray;
398         } else {
399            pixels = new int[w*h*numBands];
400         }
401
402         for (int k = 0; k < numBands; k++) {
403             int lineOffset = y*scanlineStride + x + bandOffsets[k];
404             int srcOffset = k;
405             int bank = bankIndices[k];
406
407             for (int i = 0; i < h; i++) {
408                 int pixelOffset = lineOffset;
409                 for (int j = 0; j < w; j++) {
410                     pixels[srcOffset] = data.getElem(bank, pixelOffset++);
411                     srcOffset += numBands;
412                 }
413                 lineOffset += scanlineStride;
414             }
415         }
416         return pixels;
417     }
418
419     /**
420      * Returns as int the sample in a specified band for the pixel
421      * located at (x,y).
422      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
423      * not in bounds.
424      * @param x,&nbsp;y The coordinates of the pixel location
425      * @param b The band to return
426      * @param data The DataBuffer containing the image data
427      * @return the sample in the specified band for the specified pixel.
428      * @see #setSample(int, int, int, int, DataBuffer)
429      */

430     public int getSample(int x, int y, int b, DataBuffer JavaDoc data) {
431         // Bounds check for 'b' will be performed automatically
432
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
433             throw new ArrayIndexOutOfBoundsException JavaDoc
434                 ("Coordinate out of bounds!");
435         }
436         int sample =
437             data.getElem(bankIndices[b],
438                          y*scanlineStride + x + bandOffsets[b]);
439         return sample;
440     }
441
442     /**
443      * Returns the sample in a specified band
444      * for the pixel located at (x,y) as a float.
445      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
446      * not in bounds.
447      * @param x,&nbsp;y Thecoordinates of the pixel location
448      * @param b The band to return
449      * @param data The DataBuffer containing the image data
450      * @return a float value that represents the sample in the specified
451      * band for the specified pixel.
452      */

453     public float getSampleFloat(int x, int y, int b, DataBuffer JavaDoc data) {
454         // Bounds check for 'b' will be performed automatically
455
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
456             throw new ArrayIndexOutOfBoundsException JavaDoc
457                 ("Coordinate out of bounds!");
458         }
459
460         float sample = data.getElemFloat(bankIndices[b],
461                                     y*scanlineStride + x + bandOffsets[b]);
462         return sample;
463     }
464
465     /**
466      * Returns the sample in a specified band
467      * for a pixel located at (x,y) as a double.
468      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
469      * not in bounds.
470      * @param x,&nbsp;y The coordinates of the pixel location
471      * @param b The band to return
472      * @param data The DataBuffer containing the image data
473      * @return a double value that represents the sample in the specified
474      * band for the specified pixel.
475      */

476     public double getSampleDouble(int x, int y, int b, DataBuffer JavaDoc data) {
477         // Bounds check for 'b' will be performed automatically
478
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
479             throw new ArrayIndexOutOfBoundsException JavaDoc
480                 ("Coordinate out of bounds!");
481         }
482
483         double sample = data.getElemDouble(bankIndices[b],
484                                        y*scanlineStride + x + bandOffsets[b]);
485     return sample;
486     }
487
488     /**
489      * Returns the samples in a specified band for the specified rectangle
490      * of pixels in an int array, one sample per data array element.
491      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
492      * not in bounds.
493      * @param x,&nbsp;y The coordinates of the upper left pixel location
494      * @param w The width of the pixel rectangle
495      * @param h The height of the pixel rectangle
496      * @param b The band to return
497      * @param iArray If non-null, returns the samples in this array
498      * @param data The DataBuffer containing the image data
499      * @return the samples in the specified band for the pixels within
500      * the specified region.
501      * @see #setSamples(int, int, int, int, int, int[], DataBuffer)
502      */

503     public int[] getSamples(int x, int y, int w, int h, int b,
504                             int iArray[], DataBuffer JavaDoc data) {
505         // Bounds check for 'b' will be performed automatically
506
if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
507             throw new ArrayIndexOutOfBoundsException JavaDoc
508                 ("Coordinate out of bounds!");
509         }
510         int samples[];
511         if (iArray != null) {
512            samples = iArray;
513         } else {
514            samples = new int [w*h];
515         }
516
517         int lineOffset = y*scanlineStride + x + bandOffsets[b];
518         int srcOffset = 0;
519     int bank = bankIndices[b];
520
521         for (int i = 0; i < h; i++) {
522            int sampleOffset = lineOffset;
523            for (int j = 0; j < w; j++) {
524            samples[srcOffset++] = data.getElem(bank, sampleOffset++);
525            }
526            lineOffset += scanlineStride;
527         }
528         return samples;
529     }
530
531     /**
532      * Sets the data for a single pixel in the specified DataBuffer from a
533      * primitive array of type TransferType. For a BandedSampleModel,
534      * this will be the same as the data type, and samples are transferred
535      * one per array element.
536      * <p>
537      * The following code illustrates transferring data for one pixel from
538      * DataBuffer <code>db1</code>, whose storage layout is described by
539      * BandedSampleModel <code>bsm1</code>, to DataBuffer <code>db2</code>,
540      * whose storage layout is described by
541      * BandedSampleModel <code>bsm2</code>.
542      * The transfer will generally be more efficient than using
543      * getPixel/setPixel.
544      * <pre>
545      * BandedSampleModel bsm1, bsm2;
546      * DataBufferInt db1, db2;
547      * bsm2.setDataElements(x, y, bsm1.getDataElements(x, y, null, db1),
548      * db2);
549      * </pre>
550      * Using getDataElements/setDataElements to transfer between two
551      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
552      * the same number of bands, corresponding bands have the same number of
553      * bits per sample, and the TransferTypes are the same.
554      * <p>
555      * obj must be a primitive array of type TransferType. Otherwise,
556      * a ClassCastException is thrown. An
557      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
558      * not in bounds, or if obj is not large enough to hold the pixel data.
559      * @param x,&nbsp;y The coordinates of the pixel location
560      * @param obj If non-null, returns the primitive array in this
561      * object
562      * @param data The DataBuffer containing the image data
563      * @see #getDataElements(int, int, Object, DataBuffer)
564      */

565     public void setDataElements(int x, int y, Object JavaDoc obj, DataBuffer JavaDoc data) {
566         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
567             throw new ArrayIndexOutOfBoundsException JavaDoc
568                 ("Coordinate out of bounds!");
569         }
570     int type = getTransferType();
571     int numDataElems = getNumDataElements();
572     int pixelOffset = y*scanlineStride + x;
573
574     switch(type) {
575
576     case DataBuffer.TYPE_BYTE:
577
578         byte[] barray = (byte[])obj;
579
580         for (int i=0; i<numDataElems; i++) {
581         data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
582                  barray[i] & 0xff);
583         }
584         break;
585
586     case DataBuffer.TYPE_USHORT:
587         case DataBuffer.TYPE_SHORT:
588             
589         short[] sarray = (short[])obj;
590
591         for (int i=0; i<numDataElems; i++) {
592         data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
593                  sarray[i] & 0xffff);
594         }
595         break;
596
597     case DataBuffer.TYPE_INT:
598
599         int[] iarray = (int[])obj;
600
601         for (int i=0; i<numDataElems; i++) {
602         data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
603                              iarray[i]);
604         }
605         break;
606
607     case DataBuffer.TYPE_FLOAT:
608
609         float[] farray = (float[])obj;
610
611         for (int i=0; i<numDataElems; i++) {
612         data.setElemFloat(bankIndices[i], pixelOffset + bandOffsets[i],
613                                   farray[i]);
614         }
615         break;
616
617     case DataBuffer.TYPE_DOUBLE:
618
619         double[] darray = (double[])obj;
620
621         for (int i=0; i<numDataElems; i++) {
622         data.setElemDouble(bankIndices[i], pixelOffset + bandOffsets[i],
623                                    darray[i]);
624         }
625         break;
626
627     }
628     }
629
630     /**
631      * Sets a pixel in the DataBuffer using an int array of samples for input.
632      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
633      * not in bounds.
634      * @param x,&nbsp;y The coordinates of the pixel location
635      * @param iArray The input samples in an int array
636      * @param data The DataBuffer containing the image data
637      * @see #getPixel(int, int, int[], DataBuffer)
638      */

639     public void setPixel(int x, int y, int iArray[], DataBuffer JavaDoc data) {
640         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
641             throw new ArrayIndexOutOfBoundsException JavaDoc
642                 ("Coordinate out of bounds!");
643         }
644        int pixelOffset = y*scanlineStride + x;
645        for (int i=0; i<numBands; i++) {
646            data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
647                         iArray[i]);
648        }
649     }
650
651     /**
652      * Sets all samples for a rectangle of pixels from an int array containing
653      * one sample per array element.
654      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
655      * not in bounds.
656      * @param x,&nbsp;y The coordinates of the upper left pixel location
657      * @param w The width of the pixel rectangle
658      * @param h The height of the pixel rectangle
659      * @param iArray The input samples in an int array
660      * @param data The DataBuffer containing the image data
661      * @see #getPixels(int, int, int, int, int[], DataBuffer)
662      */

663     public void setPixels(int x, int y, int w, int h,
664                           int iArray[], DataBuffer JavaDoc data) {
665         if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
666             throw new ArrayIndexOutOfBoundsException JavaDoc
667                 ("Coordinate out of bounds!");
668         }
669
670         for (int k = 0; k < numBands; k++) {
671             int lineOffset = y*scanlineStride + x + bandOffsets[k];
672             int srcOffset = k;
673             int bank = bankIndices[k];
674
675             for (int i = 0; i < h; i++) {
676                 int pixelOffset = lineOffset;
677                 for (int j = 0; j < w; j++) {
678                     data.setElem(bank, pixelOffset++, iArray[srcOffset]);
679                     srcOffset += numBands;
680                 }
681                 lineOffset += scanlineStride;
682            }
683         }
684     }
685
686     /**
687      * Sets a sample in the specified band for the pixel located at (x,y)
688      * in the DataBuffer using an int for input.
689      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
690      * not in bounds.
691      * @param x,&nbsp;y The coordinates of the pixel location
692      * @param b The band to set
693      * @param s The input sample as an int
694      * @param data The DataBuffer containing the image data
695      * @see #getSample(int, int, int, DataBuffer)
696      */

697     public void setSample(int x, int y, int b, int s,
698                           DataBuffer JavaDoc data) {
699         // Bounds check for 'b' will be performed automatically
700
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
701             throw new ArrayIndexOutOfBoundsException JavaDoc
702                 ("Coordinate out of bounds!");
703         }
704         data.setElem(bankIndices[b],
705                      y*scanlineStride + x + bandOffsets[b], s);
706     }
707
708     /**
709      * Sets a sample in the specified band for the pixel located at (x,y)
710      * in the DataBuffer using a float for input.
711      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
712      * not in bounds.
713      * @param x,&nbsp;y The coordinates of the pixel location
714      * @param b The band to set
715      * @param s The input sample as a float
716      * @param data The DataBuffer containing the image data
717      * @see #getSample(int, int, int, DataBuffer)
718      */

719     public void setSample(int x, int y, int b,
720               float s ,
721               DataBuffer JavaDoc data) {
722         // Bounds check for 'b' will be performed automatically
723
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
724             throw new ArrayIndexOutOfBoundsException JavaDoc
725                 ("Coordinate out of bounds!");
726         }
727         data.setElemFloat(bankIndices[b],
728                           y*scanlineStride + x + bandOffsets[b], s);
729     }
730
731     /**
732      * Sets a sample in the specified band for the pixel located at (x,y)
733      * in the DataBuffer using a double for input.
734      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
735      * not in bounds.
736      * @param x,&nbsp;y The coordinates of the pixel location
737      * @param b The band to set
738      * @param s The input sample as a double
739      * @param data The DataBuffer containing the image data
740      * @see #getSample(int, int, int, DataBuffer)
741      */

742     public void setSample(int x, int y, int b,
743               double s,
744               DataBuffer JavaDoc data) {
745         // Bounds check for 'b' will be performed automatically
746
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
747             throw new ArrayIndexOutOfBoundsException JavaDoc
748                 ("Coordinate out of bounds!");
749         }
750         data.setElemDouble(bankIndices[b],
751                           y*scanlineStride + x + bandOffsets[b], s);
752     }
753
754     /**
755      * Sets the samples in the specified band for the specified rectangle
756      * of pixels from an int array containing one sample per data array element.
757      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
758      * not in bounds.
759      * @param x,&nbsp;y The coordinates of the upper left pixel location
760      * @param w The width of the pixel rectangle
761      * @param h The height of the pixel rectangle
762      * @param b The band to set
763      * @param iArray The input sample array
764      * @param data The DataBuffer containing the image data
765      * @see #getSamples(int, int, int, int, int, int[], DataBuffer)
766      */

767     public void setSamples(int x, int y, int w, int h, int b,
768                            int iArray[], DataBuffer JavaDoc data) {
769         // Bounds check for 'b' will be performed automatically
770
if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
771             throw new ArrayIndexOutOfBoundsException JavaDoc
772                 ("Coordinate out of bounds!");
773         }
774         int lineOffset = y*scanlineStride + x + bandOffsets[b];
775         int srcOffset = 0;
776     int bank = bankIndices[b];
777
778         for (int i = 0; i < h; i++) {
779            int sampleOffset = lineOffset;
780            for (int j = 0; j < w; j++) {
781               data.setElem(bank, sampleOffset++, iArray[srcOffset++]);
782            }
783            lineOffset += scanlineStride;
784         }
785     }
786
787     private static int[] createOffsetArray(int numBands) {
788         int[] bandOffsets = new int[numBands];
789         for (int i=0; i < numBands; i++) {
790             bandOffsets[i] = 0;
791         }
792         return bandOffsets;
793     }
794
795     private static int[] createIndicesArray(int numBands) {
796         int[] bankIndices = new int[numBands];
797         for (int i=0; i < numBands; i++) {
798             bankIndices[i] = i;
799         }
800         return bankIndices;
801     }
802
803     // Differentiate hash code from other ComponentSampleModel subclasses
804
public int hashCode() {
805         return super.hashCode() ^ 0x2;
806     }
807 }
808
Popular Tags