BandedSampleModel.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /* Copyright (C) 2004, 2005, 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 gnu.java.awt.Buffers;
  32. import gnu.java.lang.CPStringBuilder;
  33. /**
  34. * A sample model that reads each sample value from a separate band in the
  35. * {@link DataBuffer}.
  36. *
  37. * @author Jerry Quinn (jlquinn@optonline.net)
  38. */
  39. public final class BandedSampleModel extends ComponentSampleModel
  40. {
  41. private int[] bitMasks;
  42. private int[] bitOffsets;
  43. private int[] sampleSize;
  44. private int dataBitOffset;
  45. private int elemBits;
  46. private int numberOfBits;
  47. private int numElems;
  48. private static int[] createBankArray(int size)
  49. {
  50. int[] result = new int[size];
  51. for (int i = 0; i < size; i++)
  52. result[i] = i;
  53. return result;
  54. }
  55. /**
  56. * Creates a new <code>BandedSampleModel</code>.
  57. *
  58. * @param dataType the data buffer type.
  59. * @param w the width (in pixels).
  60. * @param h the height (in pixels).
  61. * @param numBands the number of bands.
  62. */
  63. public BandedSampleModel(int dataType, int w, int h, int numBands)
  64. {
  65. this(dataType, w, h, w, createBankArray(numBands), new int[numBands]);
  66. }
  67. /**
  68. * Creates a new <code>BandedSampleModel</code>.
  69. *
  70. * @param dataType the data buffer type.
  71. * @param w the width (in pixels).
  72. * @param h the height (in pixels).
  73. * @param scanlineStride the number of data elements from a pixel in one
  74. * row to the corresponding pixel in the next row.
  75. * @param bankIndices the bank indices.
  76. * @param bandOffsets the band offsets.
  77. */
  78. public BandedSampleModel(int dataType, int w, int h, int scanlineStride,
  79. int[] bankIndices, int[] bandOffsets)
  80. {
  81. super(dataType, w, h, 1, scanlineStride, bankIndices, bandOffsets);
  82. }
  83. /**
  84. * Creates a new data buffer that is compatible with this sample model.
  85. *
  86. * @return The new data buffer.
  87. */
  88. public DataBuffer createDataBuffer()
  89. {
  90. int size = scanlineStride * height;
  91. return Buffers.createBuffer(getDataType(), size, numBanks);
  92. }
  93. /**
  94. * Creates a new <code>SampleModel</code> that is compatible with this
  95. * model and has the specified width and height.
  96. *
  97. * @param w the width (in pixels, must be greater than zero).
  98. * @param h the height (in pixels, must be greater than zero).
  99. *
  100. * @return The new sample model.
  101. *
  102. * @throws IllegalArgumentException if <code>w</code> or <code>h</code> is
  103. * not greater than zero.
  104. * @throws IllegalArgumentException if <code>w * h</code> exceeds
  105. * <code>Integer.MAX_VALUE</code>.
  106. */
  107. public SampleModel createCompatibleSampleModel(int w, int h)
  108. {
  109. // NOTE: blackdown 1.4.1 sets all offsets to 0. Sun's 1.4.2 docs
  110. // disagree.
  111. // Compress offsets so minimum is 0, others w*scanlineStride
  112. int[] newoffsets = new int[bandOffsets.length];
  113. int[] order = new int[bandOffsets.length];
  114. for (int i = 0; i < bandOffsets.length; i++)
  115. order[i] = i;
  116. // FIXME: This is N^2, but not a big issue, unless there's a lot of
  117. // bands...
  118. for (int i = 0; i < bandOffsets.length; i++)
  119. for (int j = i + 1; j < bandOffsets.length; j++)
  120. if (bankIndices[order[i]] > bankIndices[order[j]]
  121. || (bankIndices[order[i]] == bankIndices[order[j]]
  122. && bandOffsets[order[i]] > bandOffsets[order[j]]))
  123. {
  124. int t = order[i]; order[i] = order[j]; order[j] = t;
  125. }
  126. int bank = 0;
  127. int offset = 0;
  128. for (int i = 0; i < bandOffsets.length; i++)
  129. {
  130. if (bankIndices[order[i]] != bank)
  131. {
  132. bank = bankIndices[order[i]];
  133. offset = 0;
  134. }
  135. newoffsets[order[i]] = offset;
  136. offset += w * scanlineStride;
  137. }
  138. return new BandedSampleModel(dataType, w, h, w, bankIndices, newoffsets);
  139. }
  140. public SampleModel createSubsetSampleModel(int[] bands)
  141. {
  142. if (bands.length > bankIndices.length)
  143. throw new
  144. RasterFormatException("BandedSampleModel createSubsetSampleModel too"
  145. +" many bands");
  146. int[] newoff = new int[bands.length];
  147. int[] newbanks = new int[bands.length];
  148. for (int i = 0; i < bands.length; i++)
  149. {
  150. int b = bands[i];
  151. newoff[i] = bandOffsets[b];
  152. newbanks[i] = bankIndices[b];
  153. }
  154. return new BandedSampleModel(dataType, width, height, scanlineStride,
  155. newbanks, newoff);
  156. }
  157. /**
  158. * Extract all samples of one pixel and return in an array of transfer type.
  159. *
  160. * Extracts the pixel at x, y from data and stores samples into the array
  161. * obj. If obj is null, a new array of getTransferType() is created.
  162. *
  163. * @param x The x-coordinate of the pixel rectangle to store in
  164. * <code>obj</code>.
  165. * @param y The y-coordinate of the pixel rectangle to store in
  166. * <code>obj</code>.
  167. * @param obj The primitive array to store the pixels into or null to force
  168. * creation.
  169. * @param data The DataBuffer that is the source of the pixel data.
  170. * @return The primitive array containing the pixel data.
  171. * @see java.awt.image.SampleModel#getDataElements(int, int,
  172. * java.lang.Object, java.awt.image.DataBuffer)
  173. */
  174. public Object getDataElements(int x, int y, Object obj, DataBuffer data)
  175. {
  176. if (x < 0 || y < 0)
  177. throw new ArrayIndexOutOfBoundsException(
  178. "x and y must not be less than 0.");
  179. int pixel = getSample(x, y, 0, data);
  180. switch (getTransferType())
  181. {
  182. case DataBuffer.TYPE_BYTE:
  183. {
  184. byte[] b = (byte[]) obj;
  185. if (b == null) b = new byte[numBands];
  186. for (int i = 0; i < numBands; i++)
  187. b[i] = (byte)getSample(x, y, i, data);
  188. return b;
  189. }
  190. case DataBuffer.TYPE_SHORT:
  191. case DataBuffer.TYPE_USHORT:
  192. {
  193. short[] b = (short[]) obj;
  194. if (b == null) b = new short[numBands];
  195. for (int i = 0; i < numBands; i++)
  196. b[i] = (short)getSample(x, y, i, data);
  197. return b;
  198. }
  199. case DataBuffer.TYPE_INT:
  200. {
  201. int[] b = (int[]) obj;
  202. if (b == null) b = new int[numBands];
  203. for (int i = 0; i < numBands; i++)
  204. b[i] = getSample(x, y, i, data);
  205. return b;
  206. }
  207. case DataBuffer.TYPE_FLOAT:
  208. {
  209. float[] b = (float[]) obj;
  210. if (b == null) b = new float[numBands];
  211. for (int i = 0; i < numBands; i++)
  212. b[i] = getSampleFloat(x, y, i, data);
  213. return b;
  214. }
  215. case DataBuffer.TYPE_DOUBLE:
  216. {
  217. double[] b = (double[]) obj;
  218. if (b == null)
  219. b = new double[numBands];
  220. for (int i = 0; i < numBands; i++)
  221. b[i] = getSample(x, y, i, data);
  222. return b;
  223. }
  224. default:
  225. // Seems like the only sensible thing to do.
  226. throw new ClassCastException();
  227. }
  228. }
  229. /**
  230. * Returns all the samples for the pixel at location <code>(x, y)</code>
  231. * stored in the specified data buffer.
  232. *
  233. * @param x the x-coordinate.
  234. * @param y the y-coordinate.
  235. * @param iArray an array that will be populated with the sample values and
  236. * returned as the result. The size of this array should be equal to the
  237. * number of bands in the model. If the array is <code>null</code>, a new
  238. * array is created.
  239. * @param data the data buffer (<code>null</code> not permitted).
  240. *
  241. * @return The samples for the specified pixel.
  242. *
  243. * @see #setPixel(int, int, int[], DataBuffer)
  244. */
  245. public int[] getPixel(int x, int y, int[] iArray, DataBuffer data)
  246. {
  247. if (iArray == null)
  248. iArray = new int[numBands];
  249. for (int i = 0; i < numBands; i++)
  250. iArray[i] = getSample(x, y, i, data);
  251. return iArray;
  252. }
  253. /**
  254. * Copy pixels from a region into an array.
  255. *
  256. * Copies the samples of the pixels in the rectangle starting at x, y that
  257. * is w pixels wide and h scanlines high. When there is more than one band,
  258. * the samples stored in order before the next pixel. This ordering isn't
  259. * well specified in Sun's docs as of 1.4.2.
  260. *
  261. * If iArray is null, a new array is allocated, filled, and returned.
  262. *
  263. * @param x The x-coordinate of the pixel rectangle to store in
  264. * <code>iArray</code>.
  265. * @param y The y-coordinate of the pixel rectangle to store in
  266. * <code>iArray</code>.
  267. * @param w The width in pixels of the rectangle.
  268. * @param h The height in pixels of the rectangle.
  269. * @param iArray The int array to store the pixels into or null to force
  270. * creation.
  271. * @param data The DataBuffer that is the source of the pixel data.
  272. * @return The primitive array containing the pixel data.
  273. */
  274. public int[] getPixels(int x, int y, int w, int h, int[] iArray,
  275. DataBuffer data)
  276. {
  277. if (x < 0 || y < 0)
  278. throw new ArrayIndexOutOfBoundsException(
  279. "x and y must not be less than 0.");
  280. if (iArray == null)
  281. iArray = new int[w * h * numBands];
  282. int outOffset = 0;
  283. int maxX = x + w;
  284. int maxY = y + h;
  285. for (int yy = x; yy < maxY; yy++)
  286. {
  287. for (int xx = x; xx < maxX; xx++)
  288. {
  289. for (int b = 0; b < numBands; b++)
  290. {
  291. int offset = bandOffsets[b] + yy * scanlineStride + xx;
  292. iArray[outOffset++] =
  293. data.getElem(bankIndices[b], offset);
  294. }
  295. }
  296. }
  297. return iArray;
  298. }
  299. /**
  300. * Returns a sample value for the pixel at (x, y) in the specified data
  301. * buffer.
  302. *
  303. * @param x the x-coordinate of the pixel.
  304. * @param y the y-coordinate of the pixel.
  305. * @param b the band (in the range <code>0</code> to
  306. * <code>getNumBands() - 1</code>).
  307. * @param data the data buffer (<code>null</code> not permitted).
  308. *
  309. * @return The sample value.
  310. *
  311. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  312. */
  313. public int getSample(int x, int y, int b, DataBuffer data)
  314. {
  315. int offset = bandOffsets[b] + y * scanlineStride + x;
  316. return data.getElem(bankIndices[b], offset);
  317. }
  318. /**
  319. * Returns a sample value for the pixel at (x, y) in the specified data
  320. * buffer.
  321. *
  322. * @param x the x-coordinate of the pixel.
  323. * @param y the y-coordinate of the pixel.
  324. * @param b the band (in the range <code>0</code> to
  325. * <code>getNumBands() - 1</code>).
  326. * @param data the data buffer (<code>null</code> not permitted).
  327. *
  328. * @return The sample value.
  329. *
  330. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  331. *
  332. * @see #getSample(int, int, int, DataBuffer)
  333. */
  334. public float getSampleFloat(int x, int y, int b, DataBuffer data)
  335. {
  336. int offset = bandOffsets[b] + y * scanlineStride + x;
  337. return data.getElemFloat(bankIndices[b], offset);
  338. }
  339. /**
  340. * Returns the sample value for the pixel at (x, y) in the specified data
  341. * buffer.
  342. *
  343. * @param x the x-coordinate of the pixel.
  344. * @param y the y-coordinate of the pixel.
  345. * @param b the band (in the range <code>0</code> to
  346. * <code>getNumBands() - 1</code>).
  347. * @param data the data buffer (<code>null</code> not permitted).
  348. *
  349. * @return The sample value.
  350. *
  351. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  352. *
  353. * @see #getSample(int, int, int, DataBuffer)
  354. */
  355. public double getSampleDouble(int x, int y, int b, DataBuffer data)
  356. {
  357. int offset = bandOffsets[b] + y * scanlineStride + x;
  358. return data.getElemDouble(bankIndices[b], offset);
  359. }
  360. /**
  361. * Copy one band's samples from a region into an array.
  362. *
  363. * Copies from one band the samples of the pixels in the rectangle starting
  364. * at x, y that is w pixels wide and h scanlines high.
  365. *
  366. * If iArray is null, a new array is allocated, filled, and returned.
  367. *
  368. * @param x The x-coordinate of the pixel rectangle to store in
  369. * <code>iArray</code>.
  370. * @param y The y-coordinate of the pixel rectangle to store in
  371. * <code>iArray</code>.
  372. * @param w The width in pixels of the rectangle.
  373. * @param h The height in pixels of the rectangle.
  374. * @param b The band to retrieve.
  375. * @param iArray The int array to store the pixels into or null to force
  376. * creation.
  377. * @param data The DataBuffer that is the source of the pixel data.
  378. * @return The primitive array containing the pixel data.
  379. */
  380. public int[] getSamples(int x, int y, int w, int h, int b, int[] iArray,
  381. DataBuffer data)
  382. {
  383. if (x < 0 || y < 0)
  384. throw new ArrayIndexOutOfBoundsException(
  385. "x and y must not be less than 0.");
  386. if (iArray == null)
  387. iArray = new int[w * h];
  388. int outOffset = 0;
  389. int maxX = x + w;
  390. int maxY = y + h;
  391. for (int yy = y; yy < maxY; yy++)
  392. {
  393. for (int xx = x; xx < maxX; xx++)
  394. {
  395. int offset = bandOffsets[b] + yy * scanlineStride + xx;
  396. iArray[outOffset++] =
  397. data.getElem(bankIndices[b], offset);
  398. }
  399. }
  400. return iArray;
  401. }
  402. /**
  403. * Set the pixel at x, y to the value in the first element of the primitive
  404. * array obj.
  405. *
  406. * @param x The x-coordinate of the data elements in <code>obj</code>.
  407. * @param y The y-coordinate of the data elements in <code>obj</code>.
  408. * @param obj The primitive array containing the data elements to set.
  409. * @param data The DataBuffer to store the data elements into.
  410. * @see java.awt.image.SampleModel#setDataElements(int, int, int, int, java.lang.Object, java.awt.image.DataBuffer)
  411. */
  412. public void setDataElements(int x, int y, Object obj, DataBuffer data)
  413. {
  414. int transferType = getTransferType();
  415. if (getTransferType() != data.getDataType())
  416. {
  417. throw new IllegalArgumentException("transfer type ("+
  418. getTransferType()+"), "+
  419. "does not match data "+
  420. "buffer type (" +
  421. data.getDataType() +
  422. ").");
  423. }
  424. int offset = y * scanlineStride + x;
  425. try
  426. {
  427. switch (transferType)
  428. {
  429. case DataBuffer.TYPE_BYTE:
  430. {
  431. DataBufferByte out = (DataBufferByte) data;
  432. byte[] in = (byte[]) obj;
  433. for (int i = 0; i < numBands; i++)
  434. out.getData(bankIndices[i])[offset + bandOffsets[i]] = in[i];
  435. return;
  436. }
  437. case DataBuffer.TYPE_SHORT:
  438. {
  439. DataBufferShort out = (DataBufferShort) data;
  440. short[] in = (short[]) obj;
  441. for (int i = 0; i < numBands; i++)
  442. out.getData(bankIndices[i])[offset + bandOffsets[i]] = in[i];
  443. return;
  444. }
  445. case DataBuffer.TYPE_USHORT:
  446. {
  447. DataBufferUShort out = (DataBufferUShort) data;
  448. short[] in = (short[]) obj;
  449. for (int i = 0; i < numBands; i++)
  450. out.getData(bankIndices[i])[offset + bandOffsets[i]] = in[i];
  451. return;
  452. }
  453. case DataBuffer.TYPE_INT:
  454. {
  455. DataBufferInt out = (DataBufferInt) data;
  456. int[] in = (int[]) obj;
  457. for (int i = 0; i < numBands; i++)
  458. out.getData(bankIndices[i])[offset + bandOffsets[i]] = in[i];
  459. return;
  460. }
  461. case DataBuffer.TYPE_FLOAT:
  462. {
  463. DataBufferFloat out = (DataBufferFloat) data;
  464. float[] in = (float[]) obj;
  465. for (int i = 0; i < numBands; i++)
  466. out.getData(bankIndices[i])[offset + bandOffsets[i]] = in[i];
  467. return;
  468. }
  469. case DataBuffer.TYPE_DOUBLE:
  470. {
  471. DataBufferDouble out = (DataBufferDouble) data;
  472. double[] in = (double[]) obj;
  473. for (int i = 0; i < numBands; i++)
  474. out.getData(bankIndices[i])[offset + bandOffsets[i]] = in[i];
  475. return;
  476. }
  477. default:
  478. throw new ClassCastException("Unsupported data type");
  479. }
  480. }
  481. catch (ArrayIndexOutOfBoundsException aioobe)
  482. {
  483. String msg = "While writing data elements"
  484. + ", x=" + x + ", y=" + y
  485. + ", width=" + width + ", height=" + height
  486. + ", scanlineStride=" + scanlineStride
  487. + ", offset=" + offset
  488. + ", data.getSize()=" + data.getSize()
  489. + ", data.getOffset()=" + data.getOffset()
  490. + ": " + aioobe;
  491. throw new ArrayIndexOutOfBoundsException(msg);
  492. }
  493. }
  494. /**
  495. * Sets the samples for the pixel at (x, y) in the specified data buffer to
  496. * the specified values.
  497. *
  498. * @param x the x-coordinate of the pixel.
  499. * @param y the y-coordinate of the pixel.
  500. * @param iArray the sample values (<code>null</code> not permitted).
  501. * @param data the data buffer (<code>null</code> not permitted).
  502. *
  503. * @throws NullPointerException if either <code>iArray</code> or
  504. * <code>data</code> is <code>null</code>.
  505. */
  506. public void setPixel(int x, int y, int[] iArray, DataBuffer data)
  507. {
  508. for (int b = 0; b < numBands; b++)
  509. data.setElem(bankIndices[b], bandOffsets[b] + y * scanlineStride + x,
  510. iArray[b]);
  511. }
  512. /**
  513. * Sets the sample values for the pixels in the region specified by
  514. * (x, y, w, h) in the specified data buffer. The array is
  515. * ordered by pixels (that is, all the samples for the first pixel are
  516. * grouped together, followed by all the samples for the second pixel, and so
  517. * on).
  518. *
  519. * @param x the x-coordinate of the top-left pixel.
  520. * @param y the y-coordinate of the top-left pixel.
  521. * @param w the width of the region of pixels.
  522. * @param h the height of the region of pixels.
  523. * @param iArray the pixel sample values (<code>null</code> not permitted).
  524. * @param data the data buffer (<code>null</code> not permitted).
  525. *
  526. * @throws NullPointerException if either <code>iArray</code> or
  527. * <code>data</code> is <code>null</code>.
  528. */
  529. public void setPixels(int x, int y, int w, int h, int[] iArray,
  530. DataBuffer data)
  531. {
  532. int inOffset = 0;
  533. for (int hh = 0; hh < h; hh++)
  534. {
  535. for (int ww = 0; ww < w; ww++)
  536. {
  537. int offset = y * scanlineStride + (x + ww);
  538. for (int b = 0; b < numBands; b++)
  539. data.setElem(bankIndices[b], bandOffsets[b] + offset,
  540. iArray[inOffset++]);
  541. }
  542. y++;
  543. }
  544. }
  545. /**
  546. * Sets the sample value for band <code>b</code> of the pixel at location
  547. * <code>(x, y)</code> in the specified data buffer.
  548. *
  549. * @param x the x-coordinate.
  550. * @param y the y-coordinate.
  551. * @param b the band index.
  552. * @param s the sample value.
  553. * @param data the data buffer (<code>null</code> not permitted).
  554. *
  555. * @see #getSample(int, int, int, DataBuffer)
  556. */
  557. public void setSample(int x, int y, int b, int s, DataBuffer data)
  558. {
  559. data.setElem(bankIndices[b], bandOffsets[b] + y * scanlineStride + x, s);
  560. }
  561. /**
  562. * Sets the sample value for a band for the pixel at (x, y) in the
  563. * specified data buffer.
  564. *
  565. * @param x the x-coordinate of the pixel.
  566. * @param y the y-coordinate of the pixel.
  567. * @param b the band (in the range <code>0</code> to
  568. * <code>getNumBands() - 1</code>).
  569. * @param s the sample value.
  570. * @param data the data buffer (<code>null</code> not permitted).
  571. *
  572. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  573. */
  574. public void setSample(int x, int y, int b, float s, DataBuffer data)
  575. {
  576. data.setElemFloat(bankIndices[b], bandOffsets[b] + y * scanlineStride + x,
  577. s);
  578. }
  579. /**
  580. * Sets the sample value for a band for the pixel at (x, y) in the
  581. * specified data buffer.
  582. *
  583. * @param x the x-coordinate of the pixel.
  584. * @param y the y-coordinate of the pixel.
  585. * @param b the band (in the range <code>0</code> to
  586. * <code>getNumBands() - 1</code>).
  587. * @param s the sample value.
  588. * @param data the data buffer (<code>null</code> not permitted).
  589. *
  590. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  591. */
  592. public void setSample(int x, int y, int b, double s, DataBuffer data)
  593. {
  594. data.setElemDouble(bankIndices[b], bandOffsets[b] + y * scanlineStride + x,
  595. s);
  596. }
  597. /**
  598. * Sets the sample values for one band for the pixels in the region
  599. * specified by (x, y, w, h) in the specified data buffer.
  600. *
  601. * @param x the x-coordinate of the top-left pixel.
  602. * @param y the y-coordinate of the top-left pixel.
  603. * @param w the width of the region of pixels.
  604. * @param h the height of the region of pixels.
  605. * @param b the band (in the range <code>0</code> to
  606. * </code>getNumBands() - 1</code>).
  607. * @param iArray the sample values (<code>null</code> not permitted).
  608. * @param data the data buffer (<code>null</code> not permitted).
  609. *
  610. * @throws NullPointerException if either <code>iArray</code> or
  611. * <code>data</code> is <code>null</code>.
  612. */
  613. public void setSamples(int x, int y, int w, int h, int b, int[] iArray,
  614. DataBuffer data)
  615. {
  616. if (x < 0 || y < 0)
  617. throw new ArrayIndexOutOfBoundsException(
  618. "x and y must not be less than 0.");
  619. int inOffset = 0;
  620. switch (getTransferType())
  621. {
  622. case DataBuffer.TYPE_BYTE:
  623. {
  624. DataBufferByte out = (DataBufferByte) data;
  625. byte[] bank = out.getData(bankIndices[b]);
  626. for (int hh = 0; hh < h; hh++)
  627. {
  628. for (int ww = 0; ww < w; ww++)
  629. {
  630. int offset = bandOffsets[b] + y * scanlineStride + (x + ww);
  631. bank[offset] = (byte)iArray[inOffset++];
  632. }
  633. y++;
  634. }
  635. return;
  636. }
  637. case DataBuffer.TYPE_SHORT:
  638. {
  639. DataBufferShort out = (DataBufferShort) data;
  640. short[] bank = out.getData(bankIndices[b]);
  641. for (int hh = 0; hh < h; hh++)
  642. {
  643. for (int ww = 0; ww < w; ww++)
  644. {
  645. int offset = bandOffsets[b] + y * scanlineStride + (x + ww);
  646. bank[offset] = (short)iArray[inOffset++];
  647. }
  648. y++;
  649. }
  650. return;
  651. }
  652. case DataBuffer.TYPE_USHORT:
  653. {
  654. DataBufferShort out = (DataBufferShort) data;
  655. short[] bank = out.getData(bankIndices[b]);
  656. for (int hh = 0; hh < h; hh++)
  657. {
  658. for (int ww = 0; ww < w; ww++)
  659. {
  660. int offset = bandOffsets[b] + y * scanlineStride + (x + ww);
  661. bank[offset] = (short)iArray[inOffset++];
  662. }
  663. y++;
  664. }
  665. return;
  666. }
  667. case DataBuffer.TYPE_INT:
  668. {
  669. DataBufferInt out = (DataBufferInt) data;
  670. int[] bank = out.getData(bankIndices[b]);
  671. for (int hh = 0; hh < h; hh++)
  672. {
  673. for (int ww = 0; ww < w; ww++)
  674. {
  675. int offset = bandOffsets[b] + y * scanlineStride + (x + ww);
  676. bank[offset] = iArray[inOffset++];
  677. }
  678. y++;
  679. }
  680. return;
  681. }
  682. case DataBuffer.TYPE_FLOAT:
  683. case DataBuffer.TYPE_DOUBLE:
  684. break;
  685. default:
  686. throw new ClassCastException("Unsupported data type");
  687. }
  688. // Default implementation probably slower for float and double
  689. for (int hh = 0; hh < h; hh++)
  690. {
  691. for (int ww = 0; ww < w; ww++)
  692. {
  693. int offset = bandOffsets[b] + y * scanlineStride + (x + ww);
  694. data.setElem(bankIndices[b], offset, iArray[inOffset++]);
  695. }
  696. y++;
  697. }
  698. }
  699. /**
  700. * Creates a String with some information about this SampleModel.
  701. * @return A String describing this SampleModel.
  702. * @see java.lang.Object#toString()
  703. */
  704. public String toString()
  705. {
  706. CPStringBuilder result = new CPStringBuilder();
  707. result.append(getClass().getName());
  708. result.append("[");
  709. result.append("scanlineStride=").append(scanlineStride);
  710. for(int i = 0; i < bitMasks.length; i+=1)
  711. {
  712. result.append(", mask[").append(i).append("]=0x").append(
  713. Integer.toHexString(bitMasks[i]));
  714. }
  715. result.append("]");
  716. return result.toString();
  717. }
  718. }