ComponentSampleModel.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /* Copyright (C) 2000, 2002, 2006, Free Software Foundation
  2. This file is part of GNU Classpath.
  3. GNU Classpath is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. GNU Classpath is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GNU Classpath; see the file COPYING. If not, write to the
  13. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  14. 02110-1301 USA.
  15. Linking this library statically or dynamically with other modules is
  16. making a combined work based on this library. Thus, the terms and
  17. conditions of the GNU General Public License cover the whole
  18. combination.
  19. As a special exception, the copyright holders of this library give you
  20. permission to link this library with independent modules to produce an
  21. executable, regardless of the license terms of these independent
  22. modules, and to copy and distribute the resulting executable under
  23. terms of your choice, provided that you also meet, for each linked
  24. independent module, the terms and conditions of the license of that
  25. module. An independent module is a module which is not derived from
  26. or based on this library. If you modify this library, you may extend
  27. this exception to your version of the library, but you are not
  28. obligated to do so. If you do not wish to do so, delete this
  29. exception statement from your version. */
  30. package java.awt.image;
  31. import java.util.Arrays;
  32. /**
  33. * ComponentSampleModel supports a flexible organization of pixel samples in
  34. * memory, permitting pixel samples to be interleaved by band, by scanline,
  35. * and by pixel.
  36. *
  37. * A DataBuffer for this sample model has K banks of data. Pixels have N
  38. * samples, so there are N bands in the DataBuffer. Each band is completely
  39. * contained in one bank of data, but a bank may contain more than one band.
  40. * Each pixel sample is stored in a single data element.
  41. *
  42. * Within a bank, each band begins at an offset stored in bandOffsets. The
  43. * banks containing the band is given by bankIndices. Within the bank, there
  44. * are three dimensions - band, pixel, and scanline. The dimension ordering
  45. * is controlled by bandOffset, pixelStride, and scanlineStride, which means
  46. * that any combination of interleavings is supported.
  47. *
  48. * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  49. */
  50. public class ComponentSampleModel extends SampleModel
  51. {
  52. /** The offsets to the first sample for each band. */
  53. protected int[] bandOffsets;
  54. /** The indices of the bank used to store each band in a data buffer. */
  55. protected int[] bankIndices;
  56. /**
  57. * The number of bands in the image.
  58. * @specnote This field shadows the protected numBands in SampleModel.
  59. */
  60. protected int numBands;
  61. /** Used when creating data buffers. */
  62. protected int numBanks;
  63. /**
  64. * The number of data elements between a sample in one row and the
  65. * corresponding sample in the next row.
  66. */
  67. protected int scanlineStride;
  68. /**
  69. * The number of data elements between a sample for one pixel and the
  70. * corresponding sample for the next pixel in the same row.
  71. */
  72. protected int pixelStride;
  73. /**
  74. * Creates a new sample model that assumes that all bands are stored in a
  75. * single bank of the {@link DataBuffer}.
  76. * <p>
  77. * Note that the <code>bandOffsets</code> array is copied to internal storage
  78. * to prevent subsequent changes to the array from affecting this object.
  79. *
  80. * @param dataType the data type (one of {@link DataBuffer#TYPE_BYTE},
  81. * {@link DataBuffer#TYPE_USHORT}, {@link DataBuffer#TYPE_SHORT},
  82. * {@link DataBuffer#TYPE_INT}, {@link DataBuffer#TYPE_FLOAT} or
  83. * {@link DataBuffer#TYPE_DOUBLE}).
  84. * @param w the width in pixels.
  85. * @param h the height in pixels.
  86. * @param pixelStride the number of data elements in the step from a sample
  87. * in one pixel to the corresponding sample in the next pixel.
  88. * @param scanlineStride the number of data elements in the step from a
  89. * sample in a pixel to the corresponding sample in the pixel in the next
  90. * row.
  91. * @param bandOffsets the offset to the first element for each band, with
  92. * the size of the array defining the number of bands (<code>null</code>
  93. * not permitted).
  94. *
  95. * @throws IllegalArgumentException if <code>dataType</code> is not one of
  96. * the specified values.
  97. * @throws IllegalArgumentException if <code>w</code> is less than or equal
  98. * to zero.
  99. * @throws IllegalArgumentException if <code>h</code> is less than or equal
  100. * to zero.
  101. * @throws IllegalArgumentException if <code>w * h</code> exceeds
  102. * {@link Integer#MAX_VALUE}.
  103. * @throws IllegalArgumentException if <code>pixelStride</code> is negative.
  104. * @throws IllegalArgumentException if <code>scanlineStride</code> is less
  105. * than or equal to zero.
  106. * @throws IllegalArgumentException if <code>bandOffsets</code> has zero
  107. * length.
  108. */
  109. public ComponentSampleModel(int dataType,
  110. int w, int h,
  111. int pixelStride,
  112. int scanlineStride,
  113. int[] bandOffsets)
  114. {
  115. this(dataType, w, h, pixelStride, scanlineStride,
  116. new int[bandOffsets.length], bandOffsets);
  117. }
  118. /**
  119. * Creates a new sample model that assumes that all bands are stored in a
  120. * single bank of the {@link DataBuffer}.
  121. *
  122. * @param dataType the data type (one of {@link DataBuffer#TYPE_BYTE},
  123. * {@link DataBuffer#TYPE_USHORT}, {@link DataBuffer#TYPE_SHORT},
  124. * {@link DataBuffer#TYPE_INT}, {@link DataBuffer#TYPE_FLOAT} or
  125. * {@link DataBuffer#TYPE_DOUBLE}).
  126. * @param w the width in pixels.
  127. * @param h the height in pixels.
  128. * @param pixelStride the number of data elements in the step from a sample
  129. * in one pixel to the corresponding sample in the next pixel.
  130. * @param scanlineStride the number of data elements in the step from a
  131. * sample in a pixel to the corresponding sample in the pixel in the next
  132. * row.
  133. * @param bankIndices the index of the bank in which each band is stored
  134. * (<code>null</code> not permitted). This array is copied to internal
  135. * storage so that subsequent updates to the array do not affect the sample
  136. * model.
  137. * @param bandOffsets the offset to the first element for each band, with
  138. * the size of the array defining the number of bands (<code>null</code>
  139. * not permitted). This array is copied to internal storage so that
  140. * subsequent updates to the array do not affect the sample model.
  141. *
  142. * @throws IllegalArgumentException if <code>dataType</code> is not one of
  143. * the specified values.
  144. * @throws IllegalArgumentException if <code>w</code> is less than or equal
  145. * to zero.
  146. * @throws IllegalArgumentException if <code>h</code> is less than or equal
  147. * to zero.
  148. * @throws IllegalArgumentException if <code>w * h</code> exceeds
  149. * {@link Integer#MAX_VALUE}.
  150. * @throws IllegalArgumentException if <code>pixelStride</code> is negative.
  151. * @throws IllegalArgumentException if <code>scanlineStride</code> is less
  152. * than or equal to zero.
  153. * @throws IllegalArgumentException if <code>bandOffsets</code> has zero
  154. * length.
  155. */
  156. public ComponentSampleModel(int dataType,
  157. int w, int h,
  158. int pixelStride,
  159. int scanlineStride,
  160. int[] bankIndices,
  161. int[] bandOffsets)
  162. {
  163. super(dataType, w, h, bandOffsets.length);
  164. // super permits DataBuffer.TYPE_UNDEFINED but this class doesn't...
  165. if (dataType == DataBuffer.TYPE_UNDEFINED)
  166. throw new IllegalArgumentException("Unsupported dataType.");
  167. if ((pixelStride < 0) || (scanlineStride < 0) || (bandOffsets.length < 1)
  168. || (bandOffsets.length != bankIndices.length))
  169. throw new IllegalArgumentException();
  170. this.bandOffsets = (int[]) bandOffsets.clone();
  171. this.bankIndices = (int[]) bankIndices.clone();
  172. this.numBands = bandOffsets.length;
  173. this.numBanks = 0;
  174. for (int b = 0; b < bankIndices.length; b++)
  175. this.numBanks = Math.max(this.numBanks, bankIndices[b] + 1);
  176. this.scanlineStride = scanlineStride;
  177. this.pixelStride = pixelStride;
  178. }
  179. /**
  180. * Creates a new sample model that is compatible with this one, but with the
  181. * specified dimensions.
  182. *
  183. * @param w the width (must be greater than zero).
  184. * @param h the height (must be greater than zero).
  185. *
  186. * @return A new sample model.
  187. */
  188. public SampleModel createCompatibleSampleModel(int w, int h)
  189. {
  190. return new ComponentSampleModel(dataType, w, h, pixelStride,
  191. scanlineStride, bankIndices,
  192. bandOffsets);
  193. }
  194. /**
  195. * Creates a new sample model that provides access to a subset of the bands
  196. * that this sample model supports.
  197. *
  198. * @param bands the bands (<code>null</code> not permitted).
  199. *
  200. * @return The new sample model.
  201. */
  202. public SampleModel createSubsetSampleModel(int[] bands)
  203. {
  204. int numBands = bands.length;
  205. int[] bankIndices = new int[numBands];
  206. int[] bandOffsets = new int[numBands];
  207. for (int b = 0; b < numBands; b++)
  208. {
  209. bankIndices[b] = this.bankIndices[bands[b]];
  210. bandOffsets[b] = this.bandOffsets[bands[b]];
  211. }
  212. return new ComponentSampleModel(dataType, width, height, pixelStride,
  213. scanlineStride, bankIndices,
  214. bandOffsets);
  215. }
  216. /**
  217. * Creates a new data buffer that is compatible with this sample model.
  218. *
  219. * @return The new data buffer.
  220. */
  221. public DataBuffer createDataBuffer()
  222. {
  223. // Maybe this value should be precalculated in the constructor?
  224. int highestOffset = 0;
  225. for (int b = 0; b < numBands; b++)
  226. highestOffset = Math.max(highestOffset, bandOffsets[b]);
  227. int size = pixelStride * (width - 1) + scanlineStride * (height - 1)
  228. + highestOffset + 1;
  229. DataBuffer buffer = null;
  230. switch (getTransferType())
  231. {
  232. case DataBuffer.TYPE_BYTE:
  233. buffer = new DataBufferByte(size, numBanks);
  234. break;
  235. case DataBuffer.TYPE_SHORT:
  236. buffer = new DataBufferShort(size, numBanks);
  237. break;
  238. case DataBuffer.TYPE_USHORT:
  239. buffer = new DataBufferUShort(size, numBanks);
  240. break;
  241. case DataBuffer.TYPE_INT:
  242. buffer = new DataBufferInt(size, numBanks);
  243. break;
  244. case DataBuffer.TYPE_FLOAT:
  245. buffer = new DataBufferFloat(size, numBanks);
  246. break;
  247. case DataBuffer.TYPE_DOUBLE:
  248. buffer = new DataBufferDouble(size, numBanks);
  249. break;
  250. }
  251. return buffer;
  252. }
  253. /**
  254. * Returns the offset of the sample in band 0 for the pixel at location
  255. * <code>(x, y)</code>. This offset can be used to read a sample value from
  256. * a {@link DataBuffer}.
  257. *
  258. * @param x the x-coordinate.
  259. * @param y the y-coordinate.
  260. *
  261. * @return The offset.
  262. *
  263. * @see #getOffset(int, int, int)
  264. */
  265. public int getOffset(int x, int y)
  266. {
  267. return getOffset(x, y, 0);
  268. }
  269. /**
  270. * Returns the offset of the sample in band <code>b</code> for the pixel at
  271. * location <code>(x, y)</code>. This offset can be used to read a sample
  272. * value from a {@link DataBuffer}.
  273. *
  274. * @param x the x-coordinate.
  275. * @param y the y-coordinate.
  276. * @param b the band index.
  277. *
  278. * @return The offset.
  279. */
  280. public int getOffset(int x, int y, int b)
  281. {
  282. return bandOffsets[b] + pixelStride * x + scanlineStride * y;
  283. }
  284. /**
  285. * Returns the size in bits for each sample (one per band). For this sample
  286. * model, each band has the same sample size and this is determined by the
  287. * data type for the sample model.
  288. *
  289. * @return The sample sizes.
  290. *
  291. * @see SampleModel#getDataType()
  292. */
  293. public final int[] getSampleSize()
  294. {
  295. int size = DataBuffer.getDataTypeSize(getDataType());
  296. int[] sizes = new int[numBands];
  297. java.util.Arrays.fill(sizes, size);
  298. return sizes;
  299. }
  300. /**
  301. * Returns the size in bits for the samples in the specified band. In this
  302. * class, the sample size is the same for every band and is determined from
  303. * the data type for the model.
  304. *
  305. * @param band the band index (ignored here).
  306. *
  307. * @return The sample size in bits.
  308. *
  309. * @see SampleModel#getDataType()
  310. */
  311. public final int getSampleSize(int band)
  312. {
  313. return DataBuffer.getDataTypeSize(getDataType());
  314. }
  315. /**
  316. * Returns the indices of the bank(s) in the {@link DataBuffer} used to
  317. * store the samples for each band. The returned array is a copy, so that
  318. * altering it will not impact the sample model.
  319. *
  320. * @return The bank indices.
  321. */
  322. public final int[] getBankIndices()
  323. {
  324. return (int[]) bankIndices.clone();
  325. }
  326. /**
  327. * Returns the offsets to the first sample in each band. The returned array
  328. * is a copy, so that altering it will not impact the sample model.
  329. *
  330. * @return The offsets.
  331. */
  332. public final int[] getBandOffsets()
  333. {
  334. return (int[]) bandOffsets.clone();
  335. }
  336. /**
  337. * Returns the distance (in terms of element indices) between the sample for
  338. * one pixel and the corresponding sample for the equivalent pixel in the
  339. * next row. This is used in the calculation of the element offset for
  340. * retrieving samples from a {@link DataBuffer}.
  341. *
  342. * @return The distance between pixel samples in consecutive rows.
  343. */
  344. public final int getScanlineStride()
  345. {
  346. return scanlineStride;
  347. }
  348. /**
  349. * Returns the distance (in terms of element indices) between the sample for
  350. * one pixel and the corresponding sample for the next pixel in a row. This
  351. * is used in the calculation of the element offset for retrieving samples
  352. * from a {@link DataBuffer}.
  353. *
  354. * @return The distance between pixel samples in the same row.
  355. */
  356. public final int getPixelStride()
  357. {
  358. return pixelStride;
  359. }
  360. /**
  361. * Returns the number of data elements used to store the samples for one
  362. * pixel. In this model, this is the same as the number of bands.
  363. *
  364. * @return The number of data elements used to store the samples for one
  365. * pixel.
  366. */
  367. public final int getNumDataElements()
  368. {
  369. return numBands;
  370. }
  371. /**
  372. * Returns the samples for the pixel at location <code>(x, y)</code> in
  373. * a primitive array (the array type is determined by the data type for
  374. * this model). The <code>obj</code> argument provides an option to supply
  375. * an existing array to hold the result, if this is <code>null</code> a new
  376. * array will be allocated.
  377. *
  378. * @param x the x-coordinate.
  379. * @param y the y-coordinate.
  380. * @param obj a primitive array that, if not <code>null</code>, will be
  381. * used to store and return the sample values.
  382. * @param data the data buffer (<code>null</code> not permitted).
  383. *
  384. * @return An array of sample values for the specified pixel.
  385. */
  386. public Object getDataElements(int x, int y, Object obj, DataBuffer data)
  387. {
  388. int type = getTransferType();
  389. int numDataEls = getNumDataElements();
  390. int offset = y * scanlineStride + x * pixelStride;
  391. switch (type)
  392. {
  393. case DataBuffer.TYPE_BYTE:
  394. byte[] bData;
  395. if (obj == null)
  396. bData = new byte[numDataEls];
  397. else
  398. bData = (byte[]) obj;
  399. for (int i = 0; i < numDataEls; i++)
  400. {
  401. bData[i] = (byte) data.getElem(bankIndices[i],
  402. offset + bandOffsets[i]);
  403. }
  404. obj = bData;
  405. break;
  406. case DataBuffer.TYPE_SHORT:
  407. case DataBuffer.TYPE_USHORT:
  408. short[] sData;
  409. if (obj == null)
  410. sData = new short[numDataEls];
  411. else
  412. sData = (short[]) obj;
  413. for (int i = 0; i < numDataEls; i++)
  414. {
  415. sData[i] = (short) data.getElem(bankIndices[i],
  416. offset + bandOffsets[i]);
  417. }
  418. obj = sData;
  419. break;
  420. case DataBuffer.TYPE_INT:
  421. int[] iData;
  422. if (obj == null)
  423. iData = new int[numDataEls];
  424. else
  425. iData = (int[]) obj;
  426. for (int i = 0; i < numDataEls; i++)
  427. {
  428. iData[i] = data.getElem(bankIndices[i], offset + bandOffsets[i]);
  429. }
  430. obj = iData;
  431. break;
  432. case DataBuffer.TYPE_FLOAT:
  433. float[] fData;
  434. if (obj == null)
  435. fData = new float[numDataEls];
  436. else
  437. fData = (float[]) obj;
  438. for (int i = 0; i < numDataEls; i++)
  439. {
  440. fData[i] = data.getElemFloat(bankIndices[i],
  441. offset + bandOffsets[i]);
  442. }
  443. obj = fData;
  444. break;
  445. case DataBuffer.TYPE_DOUBLE:
  446. double[] dData;
  447. if (obj == null)
  448. dData = new double[numDataEls];
  449. else
  450. dData = (double[]) obj;
  451. for (int i = 0; i < numDataEls; i++)
  452. {
  453. dData[i] = data.getElemDouble(bankIndices[i],
  454. offset + bandOffsets[i]);
  455. }
  456. obj = dData;
  457. break;
  458. }
  459. return obj;
  460. }
  461. /**
  462. * Returns all the samples for the pixel at location <code>(x, y)</code>
  463. * stored in the specified data buffer.
  464. *
  465. * @param x the x-coordinate.
  466. * @param y the y-coordinate.
  467. * @param iArray an array that will be populated with the sample values and
  468. * returned as the result. The size of this array should be equal to the
  469. * number of bands in the model. If the array is <code>null</code>, a new
  470. * array is created.
  471. * @param data the data buffer (<code>null</code> not permitted).
  472. *
  473. * @return The samples for the specified pixel.
  474. *
  475. * @see #setPixel(int, int, int[], DataBuffer)
  476. */
  477. public int[] getPixel(int x, int y, int[] iArray, DataBuffer data)
  478. {
  479. if (x < 0 || x >= width || y < 0 || y >= height)
  480. throw new ArrayIndexOutOfBoundsException("Pixel (" + x + ", " + y
  481. + ") is out of bounds.");
  482. int offset = pixelStride * x + scanlineStride * y;
  483. if (iArray == null)
  484. iArray = new int[numBands];
  485. for (int b = 0; b < numBands; b++)
  486. {
  487. iArray[b] = data.getElem(bankIndices[b], offset + bandOffsets[b]);
  488. }
  489. return iArray;
  490. }
  491. /**
  492. * Returns the samples for all the pixels in a rectangular region.
  493. *
  494. * @param x the x-coordinate.
  495. * @param y the y-coordinate.
  496. * @param w the width.
  497. * @param h the height.
  498. * @param iArray an array that if non-<code>null</code> will be populated
  499. * with the sample values and returned as the result.
  500. * @param data the data buffer (<code>null</code> not permitted).
  501. *
  502. * @return The samples for all the pixels in the rectangle.
  503. */
  504. public int[] getPixels(int x, int y, int w, int h, int[] iArray,
  505. DataBuffer data)
  506. {
  507. int offset = pixelStride * x + scanlineStride * y;
  508. if (iArray == null)
  509. iArray = new int[numBands * w * h];
  510. int outOffset = 0;
  511. for (y = 0; y < h; y++)
  512. {
  513. int lineOffset = offset;
  514. for (x = 0; x < w; x++)
  515. {
  516. for (int b = 0; b < numBands; b++)
  517. {
  518. iArray[outOffset++]
  519. = data.getElem(bankIndices[b], lineOffset+bandOffsets[b]);
  520. }
  521. lineOffset += pixelStride;
  522. }
  523. offset += scanlineStride;
  524. }
  525. return iArray;
  526. }
  527. /**
  528. * Returns the sample for band <code>b</code> of the pixel at
  529. * <code>(x, y)</code> that is stored in the specified data buffer.
  530. *
  531. * @param x the x-coordinate.
  532. * @param y the y-coordinate.
  533. * @param b the band index.
  534. * @param data the data buffer (<code>null</code> not permitted).
  535. *
  536. * @return The sample value.
  537. *
  538. * @throws ArrayIndexOutOfBoundsException if <code>(x, y)</code> is outside
  539. * the bounds <code>[0, 0, width, height]</code>.
  540. *
  541. * @see #setSample(int, int, int, int, DataBuffer)
  542. */
  543. public int getSample(int x, int y, int b, DataBuffer data)
  544. {
  545. if (x < 0 || x >= width || y < 0 || y >= height)
  546. throw new ArrayIndexOutOfBoundsException("Sample (" + x + ", " + y
  547. + ") is out of bounds.");
  548. return data.getElem(bankIndices[b], getOffset(x, y, b));
  549. }
  550. /**
  551. * Sets the samples for the pixel at location <code>(x, y)</code> from the
  552. * supplied primitive array (the array type must be consistent with the data
  553. * type for this model).
  554. *
  555. * @param x the x-coordinate.
  556. * @param y the y-coordinate.
  557. * @param obj a primitive array containing the pixel's sample values.
  558. * @param data the data buffer (<code>null</code> not permitted).
  559. *
  560. * @see #setDataElements(int, int, Object, DataBuffer)
  561. */
  562. public void setDataElements(int x, int y, Object obj, DataBuffer data)
  563. {
  564. int type = getTransferType();
  565. int numDataEls = getNumDataElements();
  566. int offset = y * scanlineStride + x * pixelStride;
  567. switch (type)
  568. {
  569. case DataBuffer.TYPE_BYTE:
  570. byte[] bData = (byte[]) obj;
  571. for (int i = 0; i < numDataEls; i++)
  572. {
  573. data.setElem(bankIndices[i], offset + bandOffsets[i],
  574. ((int) bData[i]) & 0xFF);
  575. }
  576. break;
  577. case DataBuffer.TYPE_SHORT:
  578. case DataBuffer.TYPE_USHORT:
  579. short[] sData = (short[]) obj;
  580. for (int i = 0; i < numDataEls; i++)
  581. {
  582. data.setElem(bankIndices[i], offset + bandOffsets[i],
  583. ((int) sData[i]) & 0xFFFF);
  584. }
  585. break;
  586. case DataBuffer.TYPE_INT:
  587. int[] iData = (int[]) obj;
  588. for (int i = 0; i < numDataEls; i++)
  589. {
  590. data.setElem(bankIndices[i], offset + bandOffsets[i], iData[i]);
  591. }
  592. break;
  593. case DataBuffer.TYPE_FLOAT:
  594. float[] fData = (float[]) obj;
  595. for (int i = 0; i < numDataEls; i++)
  596. {
  597. data.setElemFloat(bankIndices[i], offset + bandOffsets[i],
  598. fData[i]);
  599. }
  600. break;
  601. case DataBuffer.TYPE_DOUBLE:
  602. double[] dData = (double[]) obj;
  603. for (int i = 0; i < numDataEls; i++)
  604. {
  605. data.setElemDouble(bankIndices[i], offset + bandOffsets[i],
  606. dData[i]);
  607. }
  608. break;
  609. }
  610. }
  611. /**
  612. * Sets the sample values for the pixel at location <code>(x, y)</code>
  613. * stored in the specified data buffer.
  614. *
  615. * @param x the x-coordinate.
  616. * @param y the y-coordinate.
  617. * @param iArray the pixel sample values (<code>null</code> not permitted).
  618. * @param data the data buffer (<code>null</code> not permitted).
  619. *
  620. * @see #getPixel(int, int, int[], DataBuffer)
  621. */
  622. public void setPixel(int x, int y, int[] iArray, DataBuffer data)
  623. {
  624. int offset = pixelStride * x + scanlineStride * y;
  625. for (int b = 0; b < numBands; b++)
  626. data.setElem(bankIndices[b], offset + bandOffsets[b], iArray[b]);
  627. }
  628. /**
  629. * Sets the sample value for band <code>b</code> of the pixel at location
  630. * <code>(x, y)</code> in the specified data buffer.
  631. *
  632. * @param x the x-coordinate.
  633. * @param y the y-coordinate.
  634. * @param b the band index.
  635. * @param s the sample value.
  636. * @param data the data buffer (<code>null</code> not permitted).
  637. *
  638. * @see #getSample(int, int, int, DataBuffer)
  639. */
  640. public void setSample(int x, int y, int b, int s, DataBuffer data)
  641. {
  642. data.setElem(bankIndices[b], getOffset(x, y, b), s);
  643. }
  644. /**
  645. * Tests this sample model for equality with an arbitrary object. Returns
  646. * <code>true</code> if and only if:
  647. * <ul>
  648. * <li><code>obj</code> is not <code>null</code>;</li>
  649. * <li><code>obj</code> is an instance of <code>ComponentSampleModel</code>;
  650. * </li>
  651. * <li>both models have the same values for the <code>dataType</code>,
  652. * <code>width</code>, <code>height</code>, <code>pixelStride</code>,
  653. * <code>scanlineStride</code>, <code>bandOffsets</code> and
  654. * <code>bankIndices</code> fields.</li>
  655. * </ul>
  656. *
  657. * @param obj the object to test (<code>null</code> permitted).
  658. *
  659. * @return <code>true</code> if this sample model is equal to
  660. * <code>obj</code>, and <code>false</code> otherwise.
  661. */
  662. public boolean equals(Object obj)
  663. {
  664. if (obj == null)
  665. return false;
  666. if (! (obj instanceof ComponentSampleModel))
  667. return false;
  668. ComponentSampleModel that = (ComponentSampleModel) obj;
  669. if (this.dataType != that.dataType)
  670. return false;
  671. if (this.width != that.width)
  672. return false;
  673. if (this.height != that.height)
  674. return false;
  675. if (this.pixelStride != that.pixelStride)
  676. return false;
  677. if (this.scanlineStride != that.scanlineStride)
  678. return false;
  679. if (! Arrays.equals(this.bandOffsets, that.bandOffsets))
  680. return false;
  681. if (! Arrays.equals(this.bankIndices, that.bankIndices))
  682. return false;
  683. // couldn't find any difference, so...
  684. return true;
  685. }
  686. /**
  687. * Returns a hash code for this sample model.
  688. *
  689. * @return The hash code.
  690. */
  691. public int hashCode()
  692. {
  693. // this computation is based on the method described in Chapter 3
  694. // of Joshua Bloch's Effective Java...
  695. int result = 17;
  696. result = 37 * result + dataType;
  697. result = 37 * result + width;
  698. result = 37 * result + height;
  699. result = 37 * result + pixelStride;
  700. result = 37 * result + scanlineStride;
  701. for (int i = 0; i < bandOffsets.length; i++)
  702. result = 37 * result + bandOffsets[i];
  703. for (int i = 0; i < bankIndices.length; i++)
  704. result = 37 * result + bankIndices[i];
  705. return result;
  706. }
  707. }