1 /*
2 * Portions Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 /* ****************************************************************
27 ******************************************************************
28 ******************************************************************
29 *** COPYRIGHT (c) Eastman Kodak Company, 1997
30 *** As an unpublished work pursuant to Title 17 of the United
31 *** States Code. All rights reserved.
32 ******************************************************************
33 ******************************************************************
34 ******************************************************************/
35
36 package java.awt.image;
37
38 /**
39 * This class represents image data which is stored in a band interleaved
40 * fashion and for
41 * which each sample of a pixel occupies one data element of the DataBuffer.
42 * It subclasses ComponentSampleModel but provides a more efficent
43 * implementation for accessing band interleaved image data than is provided
44 * by ComponentSampleModel. This class should typically be used when working
45 * with images which store sample data for each band in a different bank of the
46 * DataBuffer. Accessor methods are provided so that image data can be
47 * manipulated directly. Pixel stride is the number of
48 * data array elements between two samples for the same band on the same
49 * scanline. The pixel stride for a BandedSampleModel is one.
50 * Scanline stride is the number of data array elements between
51 * a given sample and the corresponding sample in the same column of the next
52 * scanline. Band offsets denote the number
53 * of data array elements from the first data array element of the bank
54 * of the DataBuffer holding each band to the first sample of the band.
55 * The bands are numbered from 0 to N-1.
56 * Bank indices denote the correspondence between a bank of the data buffer
57 * and a band of image data. This class supports
58 * {@link DataBuffer#TYPE_BYTE TYPE_BYTE},
59 * {@link DataBuffer#TYPE_USHORT TYPE_USHORT},
60 * {@link DataBuffer#TYPE_SHORT TYPE_SHORT},
61 * {@link DataBuffer#TYPE_INT TYPE_INT},
62 * {@link DataBuffer#TYPE_FLOAT TYPE_FLOAT}, and
63 * {@link DataBuffer#TYPE_DOUBLE TYPE_DOUBLE} datatypes
64 */
65
66
67 public final class BandedSampleModel extends ComponentSampleModel
68 {
69
70 /**
71 * Constructs a BandedSampleModel with the specified parameters.
72 * The pixel stride will be one data element. The scanline stride
73 * will be the same as the width. Each band will be stored in
74 * a separate bank and all band offsets will be zero.
75 * @param dataType The data type for storing samples.
76 * @param w The width (in pixels) of the region of
77 * image data described.
78 * @param h The height (in pixels) of the region of image
79 * data described.
80 * @param numBands The number of bands for the image data.
81 * @throws IllegalArgumentException if <code>dataType</code> is not
82 * one of the supported data types
83 */
84 public BandedSampleModel(int dataType, int w, int h, int numBands) {
85 super(dataType, w, h, 1, w,
86 BandedSampleModel.createIndicesArray(numBands),
87 BandedSampleModel.createOffsetArray(numBands));
88 }
89
90 /**
91 * Constructs a BandedSampleModel with the specified parameters.
92 * The number of bands will be inferred from the lengths of the
93 * bandOffsets bankIndices arrays, which must be equal. The pixel
94 * stride will be one data element.
95 * @param dataType The data type for storing samples.
96 * @param w The width (in pixels) of the region of
97 * image data described.
98 * @param h The height (in pixels) of the region of
99 * image data described.
100 * @param scanlineStride The line stride of the of the image data.
101 * @param bankIndices The bank index for each band.
102 * @param bandOffsets The band offset for each band.
103 * @throws IllegalArgumentException if <code>dataType</code> is not
104 * one of the supported data types
105 */
106 public BandedSampleModel(int dataType,
107 int w, int h,
108 int scanlineStride,
109 int bankIndices[],
110 int bandOffsets[]) {
111
112 super(dataType, w, h, 1,scanlineStride, bankIndices, bandOffsets);
113 }
114
115 /**
116 * Creates a new BandedSampleModel with the specified
117 * width and height. The new BandedSampleModel will have the same
118 * number of bands, storage data type, and bank indices
119 * as this BandedSampleModel. The band offsets will be compressed
120 * such that the offset between bands will be w*pixelStride and
121 * the minimum of all of the band offsets is zero.
122 * @param w the width of the resulting <code>BandedSampleModel</code>
123 * @param h the height of the resulting <code>BandedSampleModel</code>
124 * @return a new <code>BandedSampleModel</code> with the specified
125 * width and height.
126 * @throws IllegalArgumentException if <code>w</code> or
127 * <code>h</code> equals either
128 * <code>Integer.MAX_VALUE</code> or
129 * <code>Integer.MIN_VALUE</code>
130 * @throws IllegalArgumentException if <code>dataType</code> is not
131 * one of the supported data types
132 */
133 public SampleModel createCompatibleSampleModel(int w, int h) {
134 int[] bandOffs;
135
136 if (numBanks == 1) {
137 bandOffs = orderBands(bandOffsets, w*h);
138 }
139 else {
140 bandOffs = new int[bandOffsets.length];
141 }
142
143 SampleModel sampleModel =
144 new BandedSampleModel(dataType, w, h, w, bankIndices, bandOffs);
145 return sampleModel;
146 }
147
148 /**
149 * Creates a new BandedSampleModel with a subset of the bands of this
150 * BandedSampleModel. The new BandedSampleModel can be
151 * used with any DataBuffer that the existing BandedSampleModel
152 * can be used with. The new BandedSampleModel/DataBuffer
153 * combination will represent an image with a subset of the bands
154 * of the original BandedSampleModel/DataBuffer combination.
155 * @throws RasterFormatException if the number of bands is greater than
156 * the number of banks in this sample model.
157 * @throws IllegalArgumentException if <code>dataType</code> is not
158 * one of the supported data types
159 */
160 public SampleModel createSubsetSampleModel(int bands[]) {
161 if (bands.length > bankIndices.length)
162 throw new RasterFormatException("There are only " +
163 bankIndices.length +
164 " bands");
165 int newBankIndices[] = new int[bands.length];
166 int newBandOffsets[] = new int[bands.length];
167
168 for (int i=0; i<bands.length; i++) {
169 newBankIndices[i] = bankIndices[bands[i]];
170 newBandOffsets[i] = bandOffsets[bands[i]];
171 }
172
173 return new BandedSampleModel(this.dataType, width, height,
174 this.scanlineStride,
175 newBankIndices, newBandOffsets);
176 }
177
178 /**
179 * Creates a DataBuffer that corresponds to this BandedSampleModel,
180 * The DataBuffer's data type, number of banks, and size
181 * will be consistent with this BandedSampleModel.
182 * @throws IllegalArgumentException if <code>dataType</code> is not
183 * one of the supported types.
184 */
185 public DataBuffer createDataBuffer() {
186 DataBuffer dataBuffer = null;
187
188 int size = scanlineStride * height;
189 switch (dataType) {
190 case DataBuffer.TYPE_BYTE:
191 dataBuffer = new DataBufferByte(size, numBanks);
192 break;
193 case DataBuffer.TYPE_USHORT:
194 dataBuffer = new DataBufferUShort(size, numBanks);
195 break;
196 case DataBuffer.TYPE_SHORT:
197 dataBuffer = new DataBufferShort(size, numBanks);
198 break;
199 case DataBuffer.TYPE_INT:
200 dataBuffer = new DataBufferInt(size, numBanks);
201 break;
202 case DataBuffer.TYPE_FLOAT:
203 dataBuffer = new DataBufferFloat(size, numBanks);
204 break;
205 case DataBuffer.TYPE_DOUBLE:
206 dataBuffer = new DataBufferDouble(size, numBanks);
207 break;
208 default:
209 throw new IllegalArgumentException("dataType is not one " +
210 "of the supported types.");
211 }
212
213 return dataBuffer;
214 }
215
216
217 /**
218 * Returns data for a single pixel in a primitive array of type
219 * TransferType. For a BandedSampleModel, this will be the same
220 * as the data type, and samples will be returned one per array
221 * element. Generally, obj
222 * should be passed in as null, so that the Object will be created
223 * automatically and will be of the right primitive data type.
224 * <p>
225 * The following code illustrates transferring data for one pixel from
226 * DataBuffer <code>db1</code>, whose storage layout is described by
227 * BandedSampleModel <code>bsm1</code>, to DataBuffer <code>db2</code>,
228 * whose storage layout is described by
229 * BandedSampleModel <code>bsm2</code>.
230 * The transfer will generally be more efficient than using
231 * getPixel/setPixel.
232 * <pre>
233 * BandedSampleModel bsm1, bsm2;
234 * DataBufferInt db1, db2;
235 * bsm2.setDataElements(x, y, bsm1.getDataElements(x, y, null, db1),
236 * db2);
237 * </pre>
238 * Using getDataElements/setDataElements to transfer between two
239 * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
240 * the same number of bands, corresponding bands have the same number of
241 * bits per sample, and the TransferTypes are the same.
242 * <p>
243 * If obj is non-null, it should be a primitive array of type TransferType.
244 * Otherwise, a ClassCastException is thrown. An
245 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
246 * not in bounds, or if obj is non-null and is not large enough to hold
247 * the pixel data.
248 * @param x The X coordinate of the pixel location
249 * @param y The Y coordinate of the pixel location
250 * @param obj If non-null, a primitive array in which to return
251 * the pixel data.
252 * @param data The DataBuffer containing the image data.
253 * @return the data for the specified pixel.
254 * @see #setDataElements(int, int, Object, DataBuffer)
255 */
256 public Object getDataElements(int x, int y, Object obj, DataBuffer data) {
257 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
258 throw new ArrayIndexOutOfBoundsException
259 ("Coordinate out of bounds!");
260 }
261 int type = getTransferType();
262 int numDataElems = getNumDataElements();
263 int pixelOffset = y*scanlineStride + x;
264
265 switch(type) {
266
267 case DataBuffer.TYPE_BYTE:
268
269 byte[] bdata;
270
271 if (obj == null) {
272 bdata = new byte[numDataElems];
273 } else {
274 bdata = (byte[])obj;
275 }
276
277 for (int i=0; i<numDataElems; i++) {
278 bdata[i] = (byte)data.getElem(bankIndices[i],
279 pixelOffset + bandOffsets[i]);
280 }
281
282 obj = (Object)bdata;
283 break;
284
285 case DataBuffer.TYPE_USHORT:
286 case DataBuffer.TYPE_SHORT:
287
288 short[] sdata;
289
290 if (obj == null) {
291 sdata = new short[numDataElems];
292 } else {
293 sdata = (short[])obj;
294 }
295
296 for (int i=0; i<numDataElems; i++) {
297 sdata[i] = (short)data.getElem(bankIndices[i],
298 pixelOffset + bandOffsets[i]);
299 }
300
301 obj = (Object)sdata;
302 break;
303
304 case DataBuffer.TYPE_INT:
305
306 int[] idata;
307
308 if (obj == null) {
309 idata = new int[numDataElems];
310 } else {
311 idata = (int[])obj;
312 }
313
314 for (int i=0; i<numDataElems; i++) {
315 idata[i] = data.getElem(bankIndices[i],
316 pixelOffset + bandOffsets[i]);
317 }
318
319 obj = (Object)idata;
320 break;
321
322 case DataBuffer.TYPE_FLOAT:
323
324 float[] fdata;
325
326 if (obj == null) {
327 fdata = new float[numDataElems];
328 } else {
329 fdata = (float[])obj;
330 }
331
332 for (int i=0; i<numDataElems; i++) {
333 fdata[i] = data.getElemFloat(bankIndices[i],
334 pixelOffset + bandOffsets[i]);
335 }
336
337 obj = (Object)fdata;
338 break;
339
340 case DataBuffer.TYPE_DOUBLE:
341
342 double[] ddata;
343
344 if (obj == null) {
345 ddata = new double[numDataElems];
346 } else {
347 ddata = (double[])obj;
348 }
349
350 for (int i=0; i<numDataElems; i++) {
351 ddata[i] = data.getElemDouble(bankIndices[i],
352 pixelOffset + bandOffsets[i]);
353 }
354
355 obj = (Object)ddata;
356 break;
357 }
358
359 return obj;
360 }
361
362 /**
363 * Returns all samples for the specified pixel in an int array.
364 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
365 * not in bounds.
366 * @param x The X coordinate of the pixel location
367 * @param y The Y coordinate of the pixel location
368 * @param iArray If non-null, returns the samples in this array
369 * @param data The DataBuffer containing the image data
370 * @return the samples for the specified pixel.
371 * @see #setPixel(int, int, int[], DataBuffer)
372 */
373 public int[] getPixel(int x, int y, int iArray[], DataBuffer data) {
374 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
375 throw new ArrayIndexOutOfBoundsException
376 ("Coordinate out of bounds!");
377 }
378
379 int[] pixels;
380
381 if (iArray != null) {
382 pixels = iArray;
383 } else {
384 pixels = new int [numBands];
385 }
386
387 int pixelOffset = y*scanlineStride + x;
388 for (int i=0; i<numBands; i++) {
389 pixels[i] = data.getElem(bankIndices[i],
390 pixelOffset + bandOffsets[i]);
391 }
392 return pixels;
393 }
394
395 /**
396 * Returns all samples for the specified rectangle of pixels in
397 * an int array, one sample per data array element.
398 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
399 * not in bounds.
400 * @param x The X coordinate of the upper left pixel location
401 * @param y The Y coordinate of the upper left pixel location
402 * @param w The width of the pixel rectangle
403 * @param h The height of the pixel rectangle
404 * @param iArray If non-null, returns the samples in this array
405 * @param data The DataBuffer containing the image data
406 * @return the samples for the pixels within the specified region.
407 * @see #setPixels(int, int, int, int, int[], DataBuffer)
408 */
409 public int[] getPixels(int x, int y, int w, int h,
410 int iArray[], DataBuffer data) {
411 if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
412 throw new ArrayIndexOutOfBoundsException
413 ("Coordinate out of bounds!");
414 }
415 int[] pixels;
416
417 if (iArray != null) {
418 pixels = iArray;
419 } else {
420 pixels = new int[w*h*numBands];
421 }
422
423 for (int k = 0; k < numBands; k++) {
424 int lineOffset = y*scanlineStride + x + bandOffsets[k];
425 int srcOffset = k;
426 int bank = bankIndices[k];
427
428 for (int i = 0; i < h; i++) {
429 int pixelOffset = lineOffset;
430 for (int j = 0; j < w; j++) {
431 pixels[srcOffset] = data.getElem(bank, pixelOffset++);
432 srcOffset += numBands;
433 }
434 lineOffset += scanlineStride;
435 }
436 }
437 return pixels;
438 }
439
440 /**
441 * Returns as int the sample in a specified band for the pixel
442 * located at (x,y).
443 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
444 * not in bounds.
445 * @param x The X coordinate of the pixel location
446 * @param y The Y coordinate of the pixel location
447 * @param b The band to return
448 * @param data The DataBuffer containing the image data
449 * @return the sample in the specified band for the specified pixel.
450 * @see #setSample(int, int, int, int, DataBuffer)
451 */
452 public int getSample(int x, int y, int b, DataBuffer data) {
453 // Bounds check for 'b' will be performed automatically
454 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
455 throw new ArrayIndexOutOfBoundsException
456 ("Coordinate out of bounds!");
457 }
458 int sample =
459 data.getElem(bankIndices[b],
460 y*scanlineStride + x + bandOffsets[b]);
461 return sample;
462 }
463
464 /**
465 * Returns the sample in a specified band
466 * for the pixel located at (x,y) as a float.
467 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
468 * not in bounds.
469 * @param x The X coordinate of the pixel location
470 * @param y The Y coordinate of the pixel location
471 * @param b The band to return
472 * @param data The DataBuffer containing the image data
473 * @return a float value that represents the sample in the specified
474 * band for the specified pixel.
475 */
476 public float getSampleFloat(int x, int y, int b, DataBuffer data) {
477 // Bounds check for 'b' will be performed automatically
478 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
479 throw new ArrayIndexOutOfBoundsException
480 ("Coordinate out of bounds!");
481 }
482
483 float sample = data.getElemFloat(bankIndices[b],
484 y*scanlineStride + x + bandOffsets[b]);
485 return sample;
486 }
487
488 /**
489 * Returns the sample in a specified band
490 * for a pixel located at (x,y) as a double.
491 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
492 * not in bounds.
493 * @param x The X coordinate of the pixel location
494 * @param y The Y coordinate of the pixel location
495 * @param b The band to return
496 * @param data The DataBuffer containing the image data
497 * @return a double value that represents the sample in the specified
498 * band for the specified pixel.
499 */
500 public double getSampleDouble(int x, int y, int b, DataBuffer data) {
501 // Bounds check for 'b' will be performed automatically
502 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
503 throw new ArrayIndexOutOfBoundsException
504 ("Coordinate out of bounds!");
505 }
506
507 double sample = data.getElemDouble(bankIndices[b],
508 y*scanlineStride + x + bandOffsets[b]);
509 return sample;
510 }
511
512 /**
513 * Returns the samples in a specified band for the specified rectangle
514 * of pixels in an int array, one sample per data array element.
515 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
516 * not in bounds.
517 * @param x The X coordinate of the upper left pixel location
518 * @param y The Y coordinate of the upper left pixel location
519 * @param w The width of the pixel rectangle
520 * @param h The height of the pixel rectangle
521 * @param b The band to return
522 * @param iArray If non-null, returns the samples in this array
523 * @param data The DataBuffer containing the image data
524 * @return the samples in the specified band for the pixels within
525 * the specified region.
526 * @see #setSamples(int, int, int, int, int, int[], DataBuffer)
527 */
528 public int[] getSamples(int x, int y, int w, int h, int b,
529 int iArray[], DataBuffer data) {
530 // Bounds check for 'b' will be performed automatically
531 if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
532 throw new ArrayIndexOutOfBoundsException
533 ("Coordinate out of bounds!");
534 }
535 int samples[];
536 if (iArray != null) {
537 samples = iArray;
538 } else {
539 samples = new int [w*h];
540 }
541
542 int lineOffset = y*scanlineStride + x + bandOffsets[b];
543 int srcOffset = 0;
544 int bank = bankIndices[b];
545
546 for (int i = 0; i < h; i++) {
547 int sampleOffset = lineOffset;
548 for (int j = 0; j < w; j++) {
549 samples[srcOffset++] = data.getElem(bank, sampleOffset++);
550 }
551 lineOffset += scanlineStride;
552 }
553 return samples;
554 }
555
556 /**
557 * Sets the data for a single pixel in the specified DataBuffer from a
558 * primitive array of type TransferType. For a BandedSampleModel,
559 * this will be the same as the data type, and samples are transferred
560 * one per array element.
561 * <p>
562 * The following code illustrates transferring data for one pixel from
563 * DataBuffer <code>db1</code>, whose storage layout is described by
564 * BandedSampleModel <code>bsm1</code>, to DataBuffer <code>db2</code>,
565 * whose storage layout is described by
566 * BandedSampleModel <code>bsm2</code>.
567 * The transfer will generally be more efficient than using
568 * getPixel/setPixel.
569 * <pre>
570 * BandedSampleModel bsm1, bsm2;
571 * DataBufferInt db1, db2;
572 * bsm2.setDataElements(x, y, bsm1.getDataElements(x, y, null, db1),
573 * db2);
574 * </pre>
575 * Using getDataElements/setDataElements to transfer between two
576 * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
577 * the same number of bands, corresponding bands have the same number of
578 * bits per sample, and the TransferTypes are the same.
579 * <p>
580 * obj must be a primitive array of type TransferType. Otherwise,
581 * a ClassCastException is thrown. An
582 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
583 * not in bounds, or if obj is not large enough to hold the pixel data.
584 * @param x The X coordinate of the pixel location
585 * @param y The Y coordinate of the pixel location
586 * @param obj If non-null, returns the primitive array in this
587 * object
588 * @param data The DataBuffer containing the image data
589 * @see #getDataElements(int, int, Object, DataBuffer)
590 */
591 public void setDataElements(int x, int y, Object obj, DataBuffer data) {
592 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
593 throw new ArrayIndexOutOfBoundsException
594 ("Coordinate out of bounds!");
595 }
596 int type = getTransferType();
597 int numDataElems = getNumDataElements();
598 int pixelOffset = y*scanlineStride + x;
599
600 switch(type) {
601
602 case DataBuffer.TYPE_BYTE:
603
604 byte[] barray = (byte[])obj;
605
606 for (int i=0; i<numDataElems; i++) {
607 data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
608 barray[i] & 0xff);
609 }
610 break;
611
612 case DataBuffer.TYPE_USHORT:
613 case DataBuffer.TYPE_SHORT:
614
615 short[] sarray = (short[])obj;
616
617 for (int i=0; i<numDataElems; i++) {
618 data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
619 sarray[i] & 0xffff);
620 }
621 break;
622
623 case DataBuffer.TYPE_INT:
624
625 int[] iarray = (int[])obj;
626
627 for (int i=0; i<numDataElems; i++) {
628 data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
629 iarray[i]);
630 }
631 break;
632
633 case DataBuffer.TYPE_FLOAT:
634
635 float[] farray = (float[])obj;
636
637 for (int i=0; i<numDataElems; i++) {
638 data.setElemFloat(bankIndices[i], pixelOffset + bandOffsets[i],
639 farray[i]);
640 }
641 break;
642
643 case DataBuffer.TYPE_DOUBLE:
644
645 double[] darray = (double[])obj;
646
647 for (int i=0; i<numDataElems; i++) {
648 data.setElemDouble(bankIndices[i], pixelOffset + bandOffsets[i],
649 darray[i]);
650 }
651 break;
652
653 }
654 }
655
656 /**
657 * Sets a pixel in the DataBuffer using an int array of samples for input.
658 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
659 * not in bounds.
660 * @param x The X coordinate of the pixel location
661 * @param y The Y coordinate of the pixel location
662 * @param iArray The input samples in an int array
663 * @param data The DataBuffer containing the image data
664 * @see #getPixel(int, int, int[], DataBuffer)
665 */
666 public void setPixel(int x, int y, int iArray[], DataBuffer data) {
667 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
668 throw new ArrayIndexOutOfBoundsException
669 ("Coordinate out of bounds!");
670 }
671 int pixelOffset = y*scanlineStride + x;
672 for (int i=0; i<numBands; i++) {
673 data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
674 iArray[i]);
675 }
676 }
677
678 /**
679 * Sets all samples for a rectangle of pixels from an int array containing
680 * one sample per array element.
681 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
682 * not in bounds.
683 * @param x The X coordinate of the upper left pixel location
684 * @param y The Y coordinate of the upper left pixel location
685 * @param w The width of the pixel rectangle
686 * @param h The height of the pixel rectangle
687 * @param iArray The input samples in an int array
688 * @param data The DataBuffer containing the image data
689 * @see #getPixels(int, int, int, int, int[], DataBuffer)
690 */
691 public void setPixels(int x, int y, int w, int h,
692 int iArray[], DataBuffer data) {
693 if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
694 throw new ArrayIndexOutOfBoundsException
695 ("Coordinate out of bounds!");
696 }
697
698 for (int k = 0; k < numBands; k++) {
699 int lineOffset = y*scanlineStride + x + bandOffsets[k];
700 int srcOffset = k;
701 int bank = bankIndices[k];
702
703 for (int i = 0; i < h; i++) {
704 int pixelOffset = lineOffset;
705 for (int j = 0; j < w; j++) {
706 data.setElem(bank, pixelOffset++, iArray[srcOffset]);
707 srcOffset += numBands;
708 }
709 lineOffset += scanlineStride;
710 }
711 }
712 }
713
714 /**
715 * Sets a sample in the specified band for the pixel located at (x,y)
716 * in the DataBuffer using an int for input.
717 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
718 * not in bounds.
719 * @param x The X coordinate of the pixel location
720 * @param y The Y coordinate of the pixel location
721 * @param b The band to set
722 * @param s The input sample as an int
723 * @param data The DataBuffer containing the image data
724 * @see #getSample(int, int, int, DataBuffer)
725 */
726 public void setSample(int x, int y, int b, int s,
727 DataBuffer data) {
728 // Bounds check for 'b' will be performed automatically
729 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
730 throw new ArrayIndexOutOfBoundsException
731 ("Coordinate out of bounds!");
732 }
733 data.setElem(bankIndices[b],
734 y*scanlineStride + x + bandOffsets[b], s);
735 }
736
737 /**
738 * Sets a sample in the specified band for the pixel located at (x,y)
739 * in the DataBuffer using a float for input.
740 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
741 * not in bounds.
742 * @param x The X coordinate of the pixel location
743 * @param y The Y coordinate of the pixel location
744 * @param b The band to set
745 * @param s The input sample as a float
746 * @param data The DataBuffer containing the image data
747 * @see #getSample(int, int, int, DataBuffer)
748 */
749 public void setSample(int x, int y, int b,
750 float s ,
751 DataBuffer data) {
752 // Bounds check for 'b' will be performed automatically
753 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
754 throw new ArrayIndexOutOfBoundsException
755 ("Coordinate out of bounds!");
756 }
757 data.setElemFloat(bankIndices[b],
758 y*scanlineStride + x + bandOffsets[b], s);
759 }
760
761 /**
762 * Sets a sample in the specified band for the pixel located at (x,y)
763 * in the DataBuffer using a double for input.
764 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
765 * not in bounds.
766 * @param x The X coordinate of the pixel location
767 * @param y The Y coordinate of the pixel location
768 * @param b The band to set
769 * @param s The input sample as a double
770 * @param data The DataBuffer containing the image data
771 * @see #getSample(int, int, int, DataBuffer)
772 */
773 public void setSample(int x, int y, int b,
774 double s,
775 DataBuffer data) {
776 // Bounds check for 'b' will be performed automatically
777 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
778 throw new ArrayIndexOutOfBoundsException
779 ("Coordinate out of bounds!");
780 }
781 data.setElemDouble(bankIndices[b],
782 y*scanlineStride + x + bandOffsets[b], s);
783 }
784
785 /**
786 * Sets the samples in the specified band for the specified rectangle
787 * of pixels from an int array containing one sample per data array element.
788 * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
789 * not in bounds.
790 * @param x The X coordinate of the upper left pixel location
791 * @param y The Y coordinate of the upper left pixel location
792 * @param w The width of the pixel rectangle
793 * @param h The height of the pixel rectangle
794 * @param b The band to set
795 * @param iArray The input sample array
796 * @param data The DataBuffer containing the image data
797 * @see #getSamples(int, int, int, int, int, int[], DataBuffer)
798 */
799 public void setSamples(int x, int y, int w, int h, int b,
800 int iArray[], DataBuffer data) {
801 // Bounds check for 'b' will be performed automatically
802 if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
803 throw new ArrayIndexOutOfBoundsException
804 ("Coordinate out of bounds!");
805 }
806 int lineOffset = y*scanlineStride + x + bandOffsets[b];
807 int srcOffset = 0;
808 int bank = bankIndices[b];
809
810 for (int i = 0; i < h; i++) {
811 int sampleOffset = lineOffset;
812 for (int j = 0; j < w; j++) {
813 data.setElem(bank, sampleOffset++, iArray[srcOffset++]);
814 }
815 lineOffset += scanlineStride;
816 }
817 }
818
819 private static int[] createOffsetArray(int numBands) {
820 int[] bandOffsets = new int[numBands];
821 for (int i=0; i < numBands; i++) {
822 bandOffsets[i] = 0;
823 }
824 return bandOffsets;
825 }
826
827 private static int[] createIndicesArray(int numBands) {
828 int[] bankIndices = new int[numBands];
829 for (int i=0; i < numBands; i++) {
830 bankIndices[i] = i;
831 }
832 return bankIndices;
833 }
834
835 // Differentiate hash code from other ComponentSampleModel subclasses
836 public int hashCode() {
837 return super.hashCode() ^ 0x2;
838 }
839 }