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