SinglePixelPackedSampleModel.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /* Copyright (C) 2000, 2002, 2003, 2004, 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. import gnu.java.awt.BitMaskExtent;
  33. import gnu.java.lang.CPStringBuilder;
  34. /**
  35. * A <code>SampleModel</code> used when all samples are stored in a single
  36. * data element in the {@link DataBuffer}, and each data element contains
  37. * samples for one pixel only.
  38. *
  39. * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  40. */
  41. public class SinglePixelPackedSampleModel extends SampleModel
  42. {
  43. private int scanlineStride;
  44. private int[] bitMasks;
  45. private int[] bitOffsets;
  46. private int[] sampleSize;
  47. /**
  48. * Creates a new <code>SinglePixelPackedSampleModel</code>.
  49. *
  50. * @param dataType the data buffer type.
  51. * @param w the width (in pixels).
  52. * @param h the height (in pixels).
  53. * @param bitMasks an array containing the bit mask used to extract the
  54. * sample value for each band.
  55. */
  56. public SinglePixelPackedSampleModel(int dataType, int w, int h,
  57. int[] bitMasks)
  58. {
  59. this(dataType, w, h, w, bitMasks);
  60. }
  61. /**
  62. * Creates a new <code>SinglePixelPackedSampleModel</code>.
  63. *
  64. * @param dataType the data buffer type.
  65. * @param w the width (in pixels).
  66. * @param h the height (in pixels).
  67. * @param scanlineStride the number of data elements between a pixel on one
  68. * row and the corresponding pixel on the next row.
  69. * @param bitMasks an array containing the bit mask used to extract the
  70. * sample value for each band.
  71. */
  72. public SinglePixelPackedSampleModel(int dataType, int w, int h,
  73. int scanlineStride, int[] bitMasks)
  74. {
  75. super(dataType, w, h, bitMasks.length);
  76. switch (dataType)
  77. {
  78. case DataBuffer.TYPE_BYTE:
  79. case DataBuffer.TYPE_USHORT:
  80. case DataBuffer.TYPE_INT:
  81. break;
  82. default:
  83. throw new IllegalArgumentException(
  84. "SinglePixelPackedSampleModel unsupported dataType");
  85. }
  86. this.scanlineStride = scanlineStride;
  87. this.bitMasks = bitMasks;
  88. bitOffsets = new int[numBands];
  89. sampleSize = new int[numBands];
  90. BitMaskExtent extent = new BitMaskExtent();
  91. for (int b = 0; b < numBands; b++)
  92. {
  93. // the mask is an unsigned integer
  94. long mask = bitMasks[b] & 0xFFFFFFFFL;
  95. extent.setMask(mask);
  96. sampleSize[b] = extent.bitWidth;
  97. bitOffsets[b] = extent.leastSignificantBit;
  98. }
  99. }
  100. /**
  101. * Returns the number of data elements.
  102. *
  103. * @return <code>1</code>.
  104. */
  105. public int getNumDataElements()
  106. {
  107. return 1;
  108. }
  109. /**
  110. * Creates a new <code>SampleModel</code> that is compatible with this
  111. * model and has the specified width and height.
  112. *
  113. * @param w the width (in pixels).
  114. * @param h the height (in pixels).
  115. *
  116. * @return The new sample model.
  117. */
  118. public SampleModel createCompatibleSampleModel(int w, int h)
  119. {
  120. /* FIXME: We can avoid recalculation of bit offsets and sample
  121. sizes here by passing these from the current instance to a
  122. special private constructor. */
  123. return new SinglePixelPackedSampleModel(dataType, w, h, bitMasks);
  124. }
  125. /**
  126. * Creates a DataBuffer for holding pixel data in the format and
  127. * layout described by this SampleModel. The returned buffer will
  128. * consist of one single bank.
  129. *
  130. * @return The data buffer.
  131. */
  132. public DataBuffer createDataBuffer()
  133. {
  134. // We can save (scanlineStride - width) pixels at the very end of
  135. // the buffer. The Sun reference implementation (J2SE 1.3.1 and
  136. // 1.4.1_01) seems to do this; tested with Mauve test code.
  137. int size = scanlineStride * (height - 1) + width;
  138. DataBuffer buffer = null;
  139. switch (getTransferType())
  140. {
  141. case DataBuffer.TYPE_BYTE:
  142. buffer = new DataBufferByte(size);
  143. break;
  144. case DataBuffer.TYPE_USHORT:
  145. buffer = new DataBufferUShort(size);
  146. break;
  147. case DataBuffer.TYPE_INT:
  148. buffer = new DataBufferInt(size);
  149. break;
  150. }
  151. return buffer;
  152. }
  153. /**
  154. * Returns an array containing the size (in bits) for each band accessed by
  155. * the <code>SampleModel</code>.
  156. *
  157. * @return An array.
  158. *
  159. * @see #getSampleSize(int)
  160. */
  161. public int[] getSampleSize()
  162. {
  163. return (int[]) sampleSize.clone();
  164. }
  165. /**
  166. * Returns the size (in bits) of the samples for the specified band.
  167. *
  168. * @param band the band (in the range <code>0</code> to
  169. * <code>getNumBands() - 1</code>).
  170. *
  171. * @return The sample size (in bits).
  172. */
  173. public int getSampleSize(int band)
  174. {
  175. return sampleSize[band];
  176. }
  177. /**
  178. * Returns the index in the data buffer that stores the pixel at (x, y).
  179. *
  180. * @param x the x-coordinate.
  181. * @param y the y-coordinate.
  182. *
  183. * @return The index in the data buffer that stores the pixel at (x, y).
  184. */
  185. public int getOffset(int x, int y)
  186. {
  187. return scanlineStride*y + x;
  188. }
  189. public int[] getBitOffsets()
  190. {
  191. return bitOffsets;
  192. }
  193. public int[] getBitMasks()
  194. {
  195. return bitMasks;
  196. }
  197. /**
  198. * Returns the number of data elements from a pixel in one row to the
  199. * corresponding pixel in the next row.
  200. *
  201. * @return The scanline stride.
  202. */
  203. public int getScanlineStride()
  204. {
  205. return scanlineStride;
  206. }
  207. /**
  208. * Creates a new <code>SinglePixelPackedSampleModel</code> that accesses
  209. * the specified subset of bands.
  210. *
  211. * @param bands an array containing band indices (<code>null</code> not
  212. * permitted).
  213. *
  214. * @return A new sample model.
  215. *
  216. * @throws NullPointerException if <code>bands</code> is <code>null</code>.
  217. * @throws RasterFormatException if <code>bands.length</code> is greater
  218. * than the number of bands in this model.
  219. */
  220. public SampleModel createSubsetSampleModel(int[] bands)
  221. {
  222. if (bands.length > numBands)
  223. throw new RasterFormatException("Too many bands.");
  224. int numBands = bands.length;
  225. int[] bitMasks = new int[numBands];
  226. for (int b = 0; b < numBands; b++)
  227. bitMasks[b] = this.bitMasks[bands[b]];
  228. return new SinglePixelPackedSampleModel(dataType, width, height,
  229. scanlineStride, bitMasks);
  230. }
  231. public Object getDataElements(int x, int y, Object obj,
  232. DataBuffer data)
  233. {
  234. int type = getTransferType();
  235. Object ret = null;
  236. switch (type)
  237. {
  238. case DataBuffer.TYPE_BYTE:
  239. {
  240. byte[] in = (byte[]) obj;
  241. if (in == null)
  242. in = new byte[1];
  243. in[0] = (byte) data.getElem(x + y * scanlineStride);
  244. ret = in;
  245. }
  246. break;
  247. case DataBuffer.TYPE_USHORT:
  248. {
  249. short[] in = (short[]) obj;
  250. if (in == null)
  251. in = new short[1];
  252. in[0] = (short) data.getElem(x + y * scanlineStride);
  253. ret = in;
  254. }
  255. break;
  256. case DataBuffer.TYPE_INT:
  257. {
  258. int[] in = (int[]) obj;
  259. if (in == null)
  260. in = new int[1];
  261. in[0] = data.getElem(x + y * scanlineStride);
  262. ret = in;
  263. }
  264. break;
  265. }
  266. return ret;
  267. }
  268. /**
  269. * Returns an array containing the samples for the pixel at (x, y) in the
  270. * specified data buffer. If <code>iArray</code> is not <code>null</code>,
  271. * it will be populated with the sample values and returned as the result of
  272. * this function (this avoids allocating a new array instance).
  273. *
  274. * @param x the x-coordinate of the pixel.
  275. * @param y the y-coordinate of the pixel.
  276. * @param iArray an array to populate with the sample values and return as
  277. * the result (if <code>null</code>, a new array will be allocated).
  278. * @param data the data buffer (<code>null</code> not permitted).
  279. *
  280. * @return The pixel sample values.
  281. *
  282. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  283. */
  284. public int[] getPixel(int x, int y, int[] iArray, DataBuffer data)
  285. {
  286. int offset = scanlineStride*y + x;
  287. if (iArray == null) iArray = new int[numBands];
  288. int samples = data.getElem(offset);
  289. for (int b = 0; b < numBands; b++)
  290. iArray[b] = (samples & bitMasks[b]) >>> bitOffsets[b];
  291. return iArray;
  292. }
  293. /**
  294. * Returns an array containing the samples for the pixels in the region
  295. * specified by (x, y, w, h) in the specified data buffer. The array is
  296. * ordered by pixels (that is, all the samples for the first pixel are
  297. * grouped together, followed by all the samples for the second pixel, and so
  298. * on). If <code>iArray</code> is not <code>null</code>, it will be
  299. * populated with the sample values and returned as the result of this
  300. * function (this avoids allocating a new array instance).
  301. *
  302. * @param x the x-coordinate of the top-left pixel.
  303. * @param y the y-coordinate of the top-left pixel.
  304. * @param w the width of the region of pixels.
  305. * @param h the height of the region of pixels.
  306. * @param iArray an array to populate with the sample values and return as
  307. * the result (if <code>null</code>, a new array will be allocated).
  308. * @param data the data buffer (<code>null</code> not permitted).
  309. *
  310. * @return The pixel sample values.
  311. *
  312. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  313. */
  314. public int[] getPixels(int x, int y, int w, int h, int[] iArray,
  315. DataBuffer data)
  316. {
  317. int offset = scanlineStride*y + x;
  318. if (iArray == null) iArray = new int[numBands*w*h];
  319. int outOffset = 0;
  320. for (y = 0; y < h; y++)
  321. {
  322. int lineOffset = offset;
  323. for (x = 0; x < w; x++)
  324. {
  325. int samples = data.getElem(lineOffset++);
  326. for (int b = 0; b < numBands; b++)
  327. iArray[outOffset++] = (samples & bitMasks[b]) >>> bitOffsets[b];
  328. }
  329. offset += scanlineStride;
  330. }
  331. return iArray;
  332. }
  333. /**
  334. * Returns the sample value for the pixel at (x, y) in the specified data
  335. * buffer.
  336. *
  337. * @param x the x-coordinate of the pixel.
  338. * @param y the y-coordinate of the pixel.
  339. * @param b the band (in the range <code>0</code> to
  340. * <code>getNumBands() - 1</code>).
  341. * @param data the data buffer (<code>null</code> not permitted).
  342. *
  343. * @return The sample value.
  344. *
  345. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  346. */
  347. public int getSample(int x, int y, int b, DataBuffer data)
  348. {
  349. int offset = scanlineStride*y + x;
  350. int samples = data.getElem(offset);
  351. return (samples & bitMasks[b]) >>> bitOffsets[b];
  352. }
  353. public void setDataElements(int x, int y, Object obj, DataBuffer data)
  354. {
  355. int transferType = getTransferType();
  356. switch (transferType)
  357. {
  358. case DataBuffer.TYPE_BYTE:
  359. {
  360. byte[] in = (byte[]) obj;
  361. data.setElem(y * scanlineStride + x, ((int) in[0]) & 0xff);
  362. }
  363. break;
  364. case DataBuffer.TYPE_USHORT:
  365. {
  366. short[] in = (short[]) obj;
  367. data.setElem(y * scanlineStride + x, ((int) in[0]) & 0xffff);
  368. }
  369. break;
  370. case DataBuffer.TYPE_INT:
  371. {
  372. int[] in = (int[]) obj;
  373. data.setElem(y * scanlineStride + x, in[0]);
  374. break;
  375. }
  376. }
  377. }
  378. /**
  379. * Sets the samples for the pixel at (x, y) in the specified data buffer to
  380. * the specified values.
  381. *
  382. * @param x the x-coordinate of the pixel.
  383. * @param y the y-coordinate of the pixel.
  384. * @param iArray the sample values (<code>null</code> not permitted).
  385. * @param data the data buffer (<code>null</code> not permitted).
  386. *
  387. * @throws NullPointerException if either <code>iArray</code> or
  388. * <code>data</code> is <code>null</code>.
  389. */
  390. public void setPixel(int x, int y, int[] iArray, DataBuffer data)
  391. {
  392. int offset = scanlineStride*y + x;
  393. int samples = 0;
  394. for (int b = 0; b < numBands; b++)
  395. samples |= (iArray[b] << bitOffsets[b]) & bitMasks[b];
  396. data.setElem(offset, samples);
  397. }
  398. /**
  399. * This method implements a more efficient way to set pixels than the default
  400. * implementation of the super class. It copies the pixel components directly
  401. * from the input array instead of creating a intermediate buffer.
  402. * @param x The x-coordinate of the pixel rectangle in <code>obj</code>.
  403. * @param y The y-coordinate of the pixel rectangle in <code>obj</code>.
  404. * @param w The width of the pixel rectangle in <code>obj</code>.
  405. * @param h The height of the pixel rectangle in <code>obj</code>.
  406. * @param iArray The primitive array containing the pixels to set.
  407. * @param data The DataBuffer to store the pixels into.
  408. * @see java.awt.image.SampleModel#setPixels(int, int, int, int, int[],
  409. * java.awt.image.DataBuffer)
  410. */
  411. public void setPixels(int x, int y, int w, int h, int[] iArray,
  412. DataBuffer data)
  413. {
  414. int inOffset = 0;
  415. for (int yy=y; yy<(y+h); yy++)
  416. {
  417. int offset = scanlineStride*yy + x;
  418. for (int xx=x; xx<(x+w); xx++)
  419. {
  420. int samples = 0;
  421. for (int b = 0; b < numBands; b++)
  422. samples |= (iArray[inOffset+b] << bitOffsets[b]) & bitMasks[b];
  423. data.setElem(0, offset, samples);
  424. inOffset += numBands;
  425. offset += 1;
  426. }
  427. }
  428. }
  429. /**
  430. * Sets the sample value for a band for the pixel at (x, y) in the
  431. * specified data buffer.
  432. *
  433. * @param x the x-coordinate of the pixel.
  434. * @param y the y-coordinate of the pixel.
  435. * @param b the band (in the range <code>0</code> to
  436. * <code>getNumBands() - 1</code>).
  437. * @param s the sample value.
  438. * @param data the data buffer (<code>null</code> not permitted).
  439. *
  440. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  441. */
  442. public void setSample(int x, int y, int b, int s, DataBuffer data)
  443. {
  444. int offset = scanlineStride*y + x;
  445. int samples = data.getElem(offset);
  446. int bitMask = bitMasks[b];
  447. samples &= ~bitMask;
  448. samples |= (s << bitOffsets[b]) & bitMask;
  449. data.setElem(offset, samples);
  450. }
  451. /**
  452. * Tests this sample model for equality with an arbitrary object. This
  453. * method returns <code>true</code> if and only if:
  454. * <ul>
  455. * <li><code>obj</code> is not <code>null</code>;
  456. * <li><code>obj</code> is an instance of
  457. * <code>SinglePixelPackedSampleModel</code>;
  458. * <li>both models have the same:
  459. * <ul>
  460. * <li><code>dataType</code>;
  461. * <li><code>width</code>;
  462. * <li><code>height</code>;
  463. * <li><code>numBands</code>;
  464. * <li><code>scanlineStride</code>;
  465. * <li><code>bitMasks</code>;
  466. * <li><code>bitOffsets</code>.
  467. * </ul>
  468. * </li>
  469. * </ul>
  470. *
  471. * @param obj the object (<code>null</code> permitted)
  472. *
  473. * @return <code>true</code> if this model is equal to <code>obj</code>, and
  474. * <code>false</code> otherwise.
  475. */
  476. public boolean equals(Object obj)
  477. {
  478. if (this == obj)
  479. return true;
  480. if (! (obj instanceof SinglePixelPackedSampleModel))
  481. return false;
  482. SinglePixelPackedSampleModel that = (SinglePixelPackedSampleModel) obj;
  483. if (this.dataType != that.dataType)
  484. return false;
  485. if (this.width != that.width)
  486. return false;
  487. if (this.height != that.height)
  488. return false;
  489. if (this.numBands != that.numBands)
  490. return false;
  491. if (this.scanlineStride != that.scanlineStride)
  492. return false;
  493. if (!Arrays.equals(this.bitMasks, that.bitMasks))
  494. return false;
  495. if (!Arrays.equals(this.bitOffsets, that.bitOffsets))
  496. return false;
  497. return true;
  498. }
  499. /**
  500. * Returns a hash code for this <code>SinglePixelPackedSampleModel</code>.
  501. *
  502. * @return A hash code.
  503. */
  504. public int hashCode()
  505. {
  506. // this hash code won't match Sun's, but that shouldn't matter...
  507. int result = 193;
  508. result = 37 * result + dataType;
  509. result = 37 * result + width;
  510. result = 37 * result + height;
  511. result = 37 * result + numBands;
  512. result = 37 * result + scanlineStride;
  513. for (int i = 0; i < bitMasks.length; i++)
  514. result = 37 * result + bitMasks[i];
  515. for (int i = 0; i < bitOffsets.length; i++)
  516. result = 37 * result + bitOffsets[i];
  517. return result;
  518. }
  519. /**
  520. * Creates a String with some information about this SampleModel.
  521. * @return A String describing this SampleModel.
  522. * @see java.lang.Object#toString()
  523. */
  524. public String toString()
  525. {
  526. CPStringBuilder result = new CPStringBuilder();
  527. result.append(getClass().getName());
  528. result.append("[");
  529. result.append("scanlineStride=").append(scanlineStride);
  530. for(int i = 0; i < bitMasks.length; i+=1)
  531. {
  532. result.append(", mask[").append(i).append("]=0x").append(
  533. Integer.toHexString(bitMasks[i]));
  534. }
  535. result.append("]");
  536. return result.toString();
  537. }
  538. }