MultiPixelPackedSampleModel.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /* Copyright (C) 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 gnu.java.awt.Buffers;
  32. import gnu.java.lang.CPStringBuilder;
  33. /**
  34. * MultiPixelPackedSampleModel provides a single band model that supports
  35. * multiple pixels in a single unit. Pixels have 2^n bits and 2^k pixels fit
  36. * per data element.
  37. *
  38. * @author Jerry Quinn (jlquinn@optonline.net)
  39. */
  40. public class MultiPixelPackedSampleModel extends SampleModel
  41. {
  42. private int scanlineStride;
  43. private int[] bitMasks;
  44. private int[] bitOffsets;
  45. private int[] sampleSize;
  46. private int dataBitOffset;
  47. private int elemBits;
  48. private int numberOfBits;
  49. private int numElems;
  50. /**
  51. * Creates a new <code>MultiPixelPackedSampleModel</code> with the specified
  52. * data type, which should be one of:
  53. * <ul>
  54. * <li>{@link DataBuffer#TYPE_BYTE};</li>
  55. * <li>{@link DataBuffer#TYPE_USHORT};</li>
  56. * <li>{@link DataBuffer#TYPE_INT};</li>
  57. * </ul>
  58. *
  59. * @param dataType the data type.
  60. * @param w the width (in pixels).
  61. * @param h the height (in pixels).
  62. * @param numberOfBits the number of bits per pixel (must be a power of 2).
  63. */
  64. public MultiPixelPackedSampleModel(int dataType, int w, int h,
  65. int numberOfBits)
  66. {
  67. this(dataType, w, h, numberOfBits, 0, 0);
  68. }
  69. /**
  70. * Creates a new <code>MultiPixelPackedSampleModel</code> with the specified
  71. * data type, which should be one of:
  72. * <ul>
  73. * <li>{@link DataBuffer#TYPE_BYTE};</li>
  74. * <li>{@link DataBuffer#TYPE_USHORT};</li>
  75. * <li>{@link DataBuffer#TYPE_INT};</li>
  76. * </ul>
  77. *
  78. * @param dataType the data type.
  79. * @param w the width (in pixels).
  80. * @param h the height (in pixels).
  81. * @param numberOfBits the number of bits per pixel (must be a power of 2).
  82. * @param scanlineStride the number of data elements from a pixel on one
  83. * row to the corresponding pixel in the next row.
  84. * @param dataBitOffset the offset to the first data bit.
  85. */
  86. public MultiPixelPackedSampleModel(int dataType, int w, int h,
  87. int numberOfBits, int scanlineStride,
  88. int dataBitOffset)
  89. {
  90. super(dataType, w, h, 1);
  91. switch (dataType)
  92. {
  93. case DataBuffer.TYPE_BYTE:
  94. elemBits = 8;
  95. break;
  96. case DataBuffer.TYPE_USHORT:
  97. elemBits = 16;
  98. break;
  99. case DataBuffer.TYPE_INT:
  100. elemBits = 32;
  101. break;
  102. default:
  103. throw new IllegalArgumentException("MultiPixelPackedSampleModel"
  104. + " unsupported dataType");
  105. }
  106. this.dataBitOffset = dataBitOffset;
  107. this.numberOfBits = numberOfBits;
  108. if (numberOfBits > elemBits)
  109. throw new RasterFormatException("MultiPixelPackedSampleModel pixel size"
  110. + " larger than dataType");
  111. switch (numberOfBits)
  112. {
  113. case 1: case 2: case 4: case 8: case 16: case 32: break;
  114. default:
  115. throw new RasterFormatException("MultiPixelPackedSampleModel pixel"
  116. + " size not 2^n bits");
  117. }
  118. numElems = elemBits / numberOfBits;
  119. // Compute scan line large enough for w pixels.
  120. if (scanlineStride == 0)
  121. scanlineStride = ((dataBitOffset + w * numberOfBits) - 1) / elemBits + 1;
  122. this.scanlineStride = scanlineStride;
  123. sampleSize = new int[1];
  124. sampleSize[0] = numberOfBits;
  125. bitMasks = new int[numElems];
  126. bitOffsets = new int[numElems];
  127. for (int i=0; i < numElems; i++)
  128. {
  129. bitOffsets[numElems - i- 1] = numberOfBits * i;
  130. bitMasks[numElems - i - 1] = ((1 << numberOfBits) - 1) <<
  131. bitOffsets[numElems - i - 1];
  132. }
  133. }
  134. /**
  135. * Creates a new <code>MultiPixelPackedSample</code> model with the same
  136. * data type and bits per pixel as this model, but with the specified
  137. * dimensions.
  138. *
  139. * @param w the width (in pixels).
  140. * @param h the height (in pixels).
  141. *
  142. * @return The new sample model.
  143. */
  144. public SampleModel createCompatibleSampleModel(int w, int h)
  145. {
  146. /* FIXME: We can avoid recalculation of bit offsets and sample
  147. sizes here by passing these from the current instance to a
  148. special private constructor. */
  149. return new MultiPixelPackedSampleModel(dataType, w, h, numberOfBits);
  150. }
  151. /**
  152. * Creates a DataBuffer for holding pixel data in the format and
  153. * layout described by this SampleModel. The returned buffer will
  154. * consist of one single bank.
  155. *
  156. * @return A new data buffer.
  157. */
  158. public DataBuffer createDataBuffer()
  159. {
  160. int size = scanlineStride * height;
  161. if (dataBitOffset > 0)
  162. size += (dataBitOffset - 1) / elemBits + 1;
  163. return Buffers.createBuffer(getDataType(), size);
  164. }
  165. /**
  166. * Returns the number of data elements required to transfer a pixel in the
  167. * get/setDataElements() methods.
  168. *
  169. * @return <code>1</code>.
  170. */
  171. public int getNumDataElements()
  172. {
  173. return 1;
  174. }
  175. /**
  176. * Returns an array containing the size (in bits) of the samples in each
  177. * band. The <code>MultiPixelPackedSampleModel</code> class supports only
  178. * one band, so this method returns an array with length <code>1</code>.
  179. *
  180. * @return An array containing the size (in bits) of the samples in band zero.
  181. *
  182. * @see #getSampleSize(int)
  183. */
  184. public int[] getSampleSize()
  185. {
  186. return (int[]) sampleSize.clone();
  187. }
  188. /**
  189. * Returns the size of the samples in the specified band. Note that the
  190. * <code>MultiPixelPackedSampleModel</code> supports only one band -- this
  191. * method ignored the <code>band</code> argument, and always returns the size
  192. * of band zero.
  193. *
  194. * @param band the band (this parameter is ignored).
  195. *
  196. * @return The size of the samples in band zero.
  197. *
  198. * @see #getSampleSize()
  199. */
  200. public int getSampleSize(int band)
  201. {
  202. return sampleSize[0];
  203. }
  204. /**
  205. * Returns the index in the data buffer that stores the pixel at (x, y).
  206. *
  207. * @param x the x-coordinate.
  208. * @param y the y-coordinate.
  209. *
  210. * @return The index in the data buffer that stores the pixel at (x, y).
  211. *
  212. * @see #getBitOffset(int)
  213. */
  214. public int getOffset(int x, int y)
  215. {
  216. return scanlineStride * y + ((dataBitOffset + x * numberOfBits) / elemBits);
  217. }
  218. /**
  219. * The bit offset (within an element in the data buffer) of the pixels with
  220. * the specified x-coordinate.
  221. *
  222. * @param x the x-coordinate.
  223. *
  224. * @return The bit offset.
  225. */
  226. public int getBitOffset(int x)
  227. {
  228. return (dataBitOffset + x * numberOfBits) % elemBits;
  229. }
  230. /**
  231. * Returns the offset to the first data bit.
  232. *
  233. * @return The offset to the first data bit.
  234. */
  235. public int getDataBitOffset()
  236. {
  237. return dataBitOffset;
  238. }
  239. /**
  240. * Returns the number of data elements from a pixel in one row to the
  241. * corresponding pixel in the next row.
  242. *
  243. * @return The scanline stride.
  244. */
  245. public int getScanlineStride()
  246. {
  247. return scanlineStride;
  248. }
  249. /**
  250. * Returns the number of bits per pixel.
  251. *
  252. * @return The number of bits per pixel.
  253. */
  254. public int getPixelBitStride()
  255. {
  256. return numberOfBits;
  257. }
  258. /**
  259. * Returns the transfer type, which is one of the following (depending on
  260. * the number of bits per sample for this model):
  261. * <ul>
  262. * <li>{@link DataBuffer#TYPE_BYTE};</li>
  263. * <li>{@link DataBuffer#TYPE_USHORT};</li>
  264. * <li>{@link DataBuffer#TYPE_INT};</li>
  265. * </ul>
  266. *
  267. * @return The transfer type.
  268. */
  269. public int getTransferType()
  270. {
  271. if (numberOfBits <= DataBuffer.getDataTypeSize(DataBuffer.TYPE_BYTE))
  272. return DataBuffer.TYPE_BYTE;
  273. else if (numberOfBits <= DataBuffer.getDataTypeSize(DataBuffer.TYPE_USHORT))
  274. return DataBuffer.TYPE_USHORT;
  275. return DataBuffer.TYPE_INT;
  276. }
  277. /**
  278. * Normally this method returns a sample model for accessing a subset of
  279. * bands of image data, but since <code>MultiPixelPackedSampleModel</code>
  280. * only supports a single band, this overridden implementation just returns
  281. * a new instance of <code>MultiPixelPackedSampleModel</code>, with the same
  282. * attributes as this instance.
  283. *
  284. * @param bands the bands to include in the subset (this is ignored, except
  285. * that if it is non-<code>null</code> a check is made to ensure that the
  286. * array length is equal to <code>1</code>).
  287. *
  288. * @throws RasterFormatException if <code>bands</code> is not
  289. * <code>null</code> and <code>bands.length != 1</code>.
  290. */
  291. public SampleModel createSubsetSampleModel(int[] bands)
  292. {
  293. if (bands != null && bands.length != 1)
  294. throw new RasterFormatException("MultiPixelPackedSampleModel only"
  295. + " supports one band");
  296. return new MultiPixelPackedSampleModel(dataType, width, height,
  297. numberOfBits, scanlineStride, dataBitOffset);
  298. }
  299. /**
  300. * Extract one pixel and return in an array of transfer type.
  301. *
  302. * Extracts the pixel at x, y from data and stores into the 0th index of the
  303. * array obj, since there is only one band. If obj is null, a new array of
  304. * getTransferType() is created.
  305. *
  306. * @param x The x-coordinate of the pixel rectangle to store in
  307. * <code>obj</code>.
  308. * @param y The y-coordinate of the pixel rectangle to store in
  309. * <code>obj</code>.
  310. * @param obj The primitive array to store the pixels into or null to force
  311. * creation.
  312. * @param data The DataBuffer that is the source of the pixel data.
  313. * @return The primitive array containing the pixel data.
  314. * @see java.awt.image.SampleModel#getDataElements(int, int, Object,
  315. * DataBuffer)
  316. */
  317. public Object getDataElements(int x, int y, Object obj, DataBuffer data)
  318. {
  319. int pixel = getSample(x, y, 0, data);
  320. switch (getTransferType())
  321. {
  322. case DataBuffer.TYPE_BYTE:
  323. if (obj == null)
  324. obj = new byte[1];
  325. ((byte[]) obj)[0] = (byte) pixel;
  326. return obj;
  327. case DataBuffer.TYPE_USHORT:
  328. if (obj == null)
  329. obj = new short[1];
  330. ((short[]) obj)[0] = (short) pixel;
  331. return obj;
  332. case DataBuffer.TYPE_INT:
  333. if (obj == null)
  334. obj = new int[1];
  335. ((int[]) obj)[0] = pixel;
  336. return obj;
  337. default:
  338. // Seems like the only sensible thing to do.
  339. throw new ClassCastException();
  340. }
  341. }
  342. /**
  343. * Returns an array (of length 1) containing the sample for the pixel at
  344. * (x, y) in the specified data buffer. If <code>iArray</code> is not
  345. * <code>null</code>, it will be populated with the sample value and
  346. * returned as the result of this function (this avoids allocating a new
  347. * array instance).
  348. *
  349. * @param x the x-coordinate of the pixel.
  350. * @param y the y-coordinate of the pixel.
  351. * @param iArray an array to populate with the sample values and return as
  352. * the result (if <code>null</code>, a new array will be allocated).
  353. * @param data the data buffer (<code>null</code> not permitted).
  354. *
  355. * @return An array containing the pixel sample value.
  356. *
  357. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  358. */
  359. public int[] getPixel(int x, int y, int[] iArray, DataBuffer data)
  360. {
  361. if (iArray == null)
  362. iArray = new int[1];
  363. iArray[0] = getSample(x, y, 0, data);
  364. return iArray;
  365. }
  366. /**
  367. * Returns the sample value for the pixel at (x, y) in the specified data
  368. * buffer.
  369. *
  370. * @param x the x-coordinate of the pixel.
  371. * @param y the y-coordinate of the pixel.
  372. * @param b the band (in the range <code>0</code> to
  373. * <code>getNumBands() - 1</code>).
  374. * @param data the data buffer (<code>null</code> not permitted).
  375. *
  376. * @return The sample value.
  377. *
  378. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  379. */
  380. public int getSample(int x, int y, int b, DataBuffer data)
  381. {
  382. int pos =
  383. ((dataBitOffset + x * numberOfBits) % elemBits) / numberOfBits;
  384. int offset = getOffset(x, y);
  385. int samples = data.getElem(offset);
  386. return (samples & bitMasks[pos]) >>> bitOffsets[pos];
  387. }
  388. /**
  389. * Set the pixel at x, y to the value in the first element of the primitive
  390. * array obj.
  391. *
  392. * @param x The x-coordinate of the data elements in <code>obj</code>.
  393. * @param y The y-coordinate of the data elements in <code>obj</code>.
  394. * @param obj The primitive array containing the data elements to set.
  395. * @param data The DataBuffer to store the data elements into.
  396. */
  397. public void setDataElements(int x, int y, Object obj, DataBuffer data)
  398. {
  399. int transferType = getTransferType();
  400. try
  401. {
  402. switch (transferType)
  403. {
  404. case DataBuffer.TYPE_BYTE:
  405. {
  406. byte[] in = (byte[]) obj;
  407. setSample(x, y, 0, in[0] & 0xFF, data);
  408. return;
  409. }
  410. case DataBuffer.TYPE_USHORT:
  411. {
  412. short[] in = (short[]) obj;
  413. setSample(x, y, 0, in[0] & 0xFFFF, data);
  414. return;
  415. }
  416. case DataBuffer.TYPE_INT:
  417. {
  418. int[] in = (int[]) obj;
  419. setSample(x, y, 0, in[0], data);
  420. return;
  421. }
  422. default:
  423. throw new ClassCastException("Unsupported data type");
  424. }
  425. }
  426. catch (ArrayIndexOutOfBoundsException aioobe)
  427. {
  428. String msg = "While writing data elements" +
  429. ", x=" + x + ", y=" + y +
  430. ", width=" + width + ", height=" + height +
  431. ", scanlineStride=" + scanlineStride +
  432. ", offset=" + getOffset(x, y) +
  433. ", data.getSize()=" + data.getSize() +
  434. ", data.getOffset()=" + data.getOffset() +
  435. ": " + aioobe;
  436. throw new ArrayIndexOutOfBoundsException(msg);
  437. }
  438. }
  439. /**
  440. * Sets the sample value for the pixel at (x, y) in the specified data
  441. * buffer to the specified value.
  442. *
  443. * @param x the x-coordinate of the pixel.
  444. * @param y the y-coordinate of the pixel.
  445. * @param iArray the sample value (<code>null</code> not permitted).
  446. * @param data the data buffer (<code>null</code> not permitted).
  447. *
  448. * @throws NullPointerException if either <code>iArray</code> or
  449. * <code>data</code> is <code>null</code>.
  450. *
  451. * @see #setSample(int, int, int, int, DataBuffer)
  452. */
  453. public void setPixel(int x, int y, int[] iArray, DataBuffer data)
  454. {
  455. setSample(x, y, 0, iArray[0], data);
  456. }
  457. /**
  458. * Sets the sample value for a band for the pixel at (x, y) in the
  459. * specified data buffer.
  460. *
  461. * @param x the x-coordinate of the pixel.
  462. * @param y the y-coordinate of the pixel.
  463. * @param b the band (in the range <code>0</code> to
  464. * <code>getNumBands() - 1</code>).
  465. * @param s the sample value.
  466. * @param data the data buffer (<code>null</code> not permitted).
  467. *
  468. * @throws NullPointerException if <code>data</code> is <code>null</code>.
  469. */
  470. public void setSample(int x, int y, int b, int s, DataBuffer data)
  471. {
  472. int bitpos =
  473. ((dataBitOffset + x * numberOfBits) % elemBits) / numberOfBits;
  474. int offset = getOffset(x, y);
  475. s = s << bitOffsets[bitpos];
  476. s = s & bitMasks[bitpos];
  477. int sample = data.getElem(offset);
  478. sample |= s;
  479. data.setElem(offset, sample);
  480. }
  481. /**
  482. * Tests this sample model for equality with an arbitrary object. This
  483. * method returns <code>true</code> if and only if:
  484. * <ul>
  485. * <li><code>obj</code> is not <code>null</code>;
  486. * <li><code>obj</code> is an instance of
  487. * <code>MultiPixelPackedSampleModel</code>;
  488. * <li>both models have the same:
  489. * <ul>
  490. * <li><code>dataType</code>;
  491. * <li><code>width</code>;
  492. * <li><code>height</code>;
  493. * <li><code>numberOfBits</code>;
  494. * <li><code>scanlineStride</code>;
  495. * <li><code>dataBitOffsets</code>.
  496. * </ul>
  497. * </li>
  498. * </ul>
  499. *
  500. * @param obj the object (<code>null</code> permitted)
  501. *
  502. * @return <code>true</code> if this model is equal to <code>obj</code>, and
  503. * <code>false</code> otherwise.
  504. */
  505. public boolean equals(Object obj)
  506. {
  507. if (this == obj)
  508. return true;
  509. if (! (obj instanceof MultiPixelPackedSampleModel))
  510. return false;
  511. MultiPixelPackedSampleModel that = (MultiPixelPackedSampleModel) obj;
  512. if (this.dataType != that.dataType)
  513. return false;
  514. if (this.width != that.width)
  515. return false;
  516. if (this.height != that.height)
  517. return false;
  518. if (this.numberOfBits != that.numberOfBits)
  519. return false;
  520. if (this.scanlineStride != that.scanlineStride)
  521. return false;
  522. if (this.dataBitOffset != that.dataBitOffset)
  523. return false;
  524. return true;
  525. }
  526. /**
  527. * Returns a hash code for this <code>MultiPixelPackedSampleModel</code>.
  528. *
  529. * @return A hash code.
  530. */
  531. public int hashCode()
  532. {
  533. // this hash code won't match Sun's, but that shouldn't matter...
  534. int result = 193;
  535. result = 37 * result + dataType;
  536. result = 37 * result + width;
  537. result = 37 * result + height;
  538. result = 37 * result + numberOfBits;
  539. result = 37 * result + scanlineStride;
  540. result = 37 * result + dataBitOffset;
  541. return result;
  542. }
  543. /**
  544. * Creates a String with some information about this SampleModel.
  545. * @return A String describing this SampleModel.
  546. * @see java.lang.Object#toString()
  547. */
  548. public String toString()
  549. {
  550. CPStringBuilder result = new CPStringBuilder();
  551. result.append(getClass().getName());
  552. result.append("[");
  553. result.append("scanlineStride=").append(scanlineStride);
  554. for(int i=0; i < bitMasks.length; i+=1)
  555. {
  556. result.append(", mask[").append(i).append("]=0x").append(Integer.toHexString(bitMasks[i]));
  557. }
  558. result.append("]");
  559. return result.toString();
  560. }
  561. }