1. /*
  2. * @(#)SampleModel.java 1.24 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /* ****************************************************************
  8. ******************************************************************
  9. ******************************************************************
  10. *** COPYRIGHT (c) Eastman Kodak Company, 1997
  11. *** As an unpublished work pursuant to Title 17 of the United
  12. *** States Code. All rights reserved.
  13. ******************************************************************
  14. ******************************************************************
  15. ******************************************************************/
  16. package java.awt.image;
  17. /**
  18. * This abstract class defines an interface for extracting samples of pixels
  19. * in an image. All image data is expressed as a collection of pixels.
  20. * Each pixel consists of a number of samples. A sample is a datum
  21. * for one band of an image. In the Java 2D API, all built-in image
  22. * processing and display operators process samples which represent
  23. * unsigned integral values.
  24. * <p>
  25. * A collection of pixels is represented as a Raster, which consists of
  26. * a DataBuffer and a SampleModel. The SampleModel allows access to
  27. * samples in the DataBuffer and may provide low-level information that
  28. * a programmer can use to directly manipulate samples and pixels in the
  29. * DataBuffer.
  30. * <p>
  31. * This class is generally a fall back method for dealing with
  32. * images. More efficient code will cast the SampleModel to the
  33. * appropriate subclass and extract the information needed to directly
  34. * manipulate pixels in the DataBuffer.
  35. *
  36. * @see java.awt.image.DataBuffer
  37. * @see java.awt.image.Raster
  38. * @see java.awt.image.ComponentSampleModel
  39. * @see java.awt.image.PixelInterleavedSampleModel
  40. * @see java.awt.image.BandedSampleModel
  41. * @see java.awt.image.MultiPixelPackedSampleModel
  42. * @see java.awt.image.SinglePixelPackedSampleModel
  43. */
  44. public abstract class SampleModel
  45. {
  46. /** Width in pixels of the region of image data that this SampleModel
  47. * describes.
  48. */
  49. protected int width;
  50. /** Height in pixels of the region of image data that this SampleModel
  51. * describes.
  52. */
  53. protected int height;
  54. /** Number of bands of the image data that this SampleModel describes. */
  55. protected int numBands;
  56. /** Data type of the DataBuffer storing the pixel data.
  57. * @see java.awt.image.DataBuffer
  58. */
  59. protected int dataType;
  60. static private native void initIDs();
  61. static {
  62. ColorModel.loadLibraries();
  63. initIDs();
  64. }
  65. /**
  66. * Constructs a SampleModel with the specified parameters.
  67. * @param dataType The data type of the DataBuffer storing the pixel data.
  68. * @param w The width (in pixels) of the region of image data.
  69. * @param h The height (in pixels) of the region of image data.
  70. * @param numBands The number of bands of the image data.
  71. * @throws IllegalArgumentException if <code>w</code> or <code>h</code>
  72. * is not greater than 0
  73. * @throws IllegalArgumentException if the product of <code>w</code>
  74. * and <code>h</code> is greater than
  75. * <code>Integer.MAX_VALUE</code>
  76. * @throws IllegalArgumentException if <code>dataType</code> is not
  77. * one of the supported data types
  78. */
  79. public SampleModel(int dataType, int w, int h, int numBands)
  80. {
  81. float size = (float)w*h;
  82. if (w <= 0 || h <= 0) {
  83. throw new IllegalArgumentException("Width ("+w+") and height ("+
  84. height+") must be > 0");
  85. }
  86. if (size >= Integer.MAX_VALUE) {
  87. throw new IllegalArgumentException("Dimensions (width="+w+
  88. " height="+h+") are too large");
  89. }
  90. if (dataType < DataBuffer.TYPE_BYTE ||
  91. (dataType > DataBuffer.TYPE_DOUBLE &&
  92. dataType != DataBuffer.TYPE_UNDEFINED))
  93. {
  94. throw new IllegalArgumentException("Unsupported dataType: "+
  95. dataType);
  96. }
  97. this.dataType = dataType;
  98. this.width = w;
  99. this.height = h;
  100. this.numBands = numBands;
  101. }
  102. /** Returns the width in pixels. */
  103. final public int getWidth() {
  104. return width;
  105. }
  106. /** Returns the height in pixels. */
  107. final public int getHeight() {
  108. return height;
  109. }
  110. /** Returns the total number of bands of image data. */
  111. final public int getNumBands() {
  112. return numBands;
  113. }
  114. /** Returns the number of data elements needed to transfer a pixel
  115. * via the getDataElements and setDataElements methods. When pixels
  116. * are transferred via these methods, they may be transferred in a
  117. * packed or unpacked format, depending on the implementation of the
  118. * SampleModel. Using these methods, pixels are transferred as an
  119. * array of getNumDataElements() elements of a primitive type given
  120. * by getTransferType(). The TransferType may or may not be the same
  121. * as the storage DataType.
  122. * @see #getDataElements(int, int, Object, DataBuffer)
  123. * @see #getDataElements(int, int, int, int, Object, DataBuffer)
  124. * @see #setDataElements(int, int, Object, DataBuffer)
  125. * @see #setDataElements(int, int, int, int, Object, DataBuffer)
  126. * @see #getTransferType
  127. */
  128. public abstract int getNumDataElements();
  129. /** Returns the data type of the DataBuffer storing the pixel data. */
  130. final public int getDataType() {
  131. return dataType;
  132. }
  133. /** Returns the TransferType used to transfer pixels via the
  134. * getDataElements and setDataElements methods. When pixels
  135. * are transferred via these methods, they may be transferred in a
  136. * packed or unpacked format, depending on the implementation of the
  137. * SampleModel. Using these methods, pixels are transferred as an
  138. * array of getNumDataElements() elements of a primitive type given
  139. * by getTransferType(). The TransferType may or may not be the same
  140. * as the storage DataType. The TransferType will be one of the types
  141. * defined in DataBuffer.
  142. * @see #getDataElements(int, int, Object, DataBuffer)
  143. * @see #getDataElements(int, int, int, int, Object, DataBuffer)
  144. * @see #setDataElements(int, int, Object, DataBuffer)
  145. * @see #setDataElements(int, int, int, int, Object, DataBuffer)
  146. * @see #getNumDataElements
  147. * @see java.awt.image.DataBuffer
  148. */
  149. public int getTransferType() {
  150. return dataType;
  151. }
  152. /**
  153. * Returns the samples for a specified pixel in an int array,
  154. * one sample per array element.
  155. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  156. * not in bounds.
  157. * @param x The X coordinate of the pixel location.
  158. * @param y The Y coordinate of the pixel location.
  159. * @param iArray If non-null, returns the samples in this array.
  160. * @param data The DataBuffer containing the image data.
  161. */
  162. public int[] getPixel(int x, int y, int iArray[], DataBuffer data) {
  163. int pixels[];
  164. if (iArray != null)
  165. pixels = iArray;
  166. else
  167. pixels = new int[numBands];
  168. for (int i=0; i<numBands; i++) {
  169. pixels[i] = getSample(x, y, i, data);
  170. }
  171. return pixels;
  172. }
  173. /**
  174. * Returns data for a single pixel in a primitive array of type
  175. * TransferType. For image data supported by the Java 2D API, this
  176. * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or
  177. * DataBuffer.TYPE_INT. Data may be returned in a packed format,
  178. * thus increasing efficiency for data transfers. Generally, obj
  179. * should be passed in as null, so that the Object will be created
  180. * automatically and will be of the right primitive data type.
  181. * <p>
  182. * The following code illustrates transferring data for one pixel from
  183. * DataBuffer <code>db1</code>, whose storage layout is described by
  184. * SampleModel <code>sm1</code>, to DataBuffer <code>db2</code>, whose
  185. * storage layout is described by SampleModel <code>sm2</code>.
  186. * The transfer will generally be more efficient than using
  187. * getPixel/setPixel.
  188. * <pre>
  189. * SampleModel sm1, sm2;
  190. * DataBuffer db1, db2;
  191. * sm2.setDataElements(x, y, sm1.getDataElements(x, y, null, db1), db2);
  192. * </pre>
  193. * Using getDataElements/setDataElements to transfer between two
  194. * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
  195. * the same number of bands, corresponding bands have the same number of
  196. * bits per sample, and the TransferTypes are the same.
  197. * <p>
  198. * If obj is non-null, it should be a primitive array of type TransferType.
  199. * Otherwise, a ClassCastException is thrown. An
  200. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  201. * not in bounds, or if obj is non-null and is not large enough to hold
  202. * the pixel data.
  203. * @param x The X coordinate of the pixel location.
  204. * @param y The Y coordinate of the pixel location.
  205. * @param obj If non-null, a primitive array in which to return
  206. * the pixel data.
  207. * @param data The DataBuffer containing the image data.
  208. * @see #getNumDataElements
  209. * @see #getTransferType
  210. * @see java.awt.image.DataBuffer
  211. */
  212. public abstract Object getDataElements(int x, int y,
  213. Object obj, DataBuffer data);
  214. /**
  215. * Returns the pixel data for the specified rectangle of pixels in a
  216. * primitive array of type TransferType.
  217. * For image data supported by the Java 2D API, this
  218. * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or
  219. * DataBuffer.TYPE_INT. Data may be returned in a packed format,
  220. * thus increasing efficiency for data transfers. Generally, obj
  221. * should be passed in as null, so that the Object will be created
  222. * automatically and will be of the right primitive data type.
  223. * <p>
  224. * The following code illustrates transferring data for a rectangular
  225. * region of pixels from
  226. * DataBuffer <code>db1</code>, whose storage layout is described by
  227. * SampleModel <code>sm1</code>, to DataBuffer <code>db2</code>, whose
  228. * storage layout is described by SampleModel <code>sm2</code>.
  229. * The transfer will generally be more efficient than using
  230. * getPixels/setPixels.
  231. * <pre>
  232. * SampleModel sm1, sm2;
  233. * DataBuffer db1, db2;
  234. * sm2.setDataElements(x, y, w, h, sm1.getDataElements(x, y, w,
  235. * h, null, db1), db2);
  236. * </pre>
  237. * Using getDataElements/setDataElements to transfer between two
  238. * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
  239. * the same number of bands, corresponding bands have the same number of
  240. * bits per sample, and the TransferTypes are the same.
  241. * <p>
  242. * If obj is non-null, it should be a primitive array of type TransferType.
  243. * Otherwise, a ClassCastException is thrown. An
  244. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  245. * not in bounds, or if obj is non-null and is not large enough to hold
  246. * the pixel data.
  247. * @param x The minimum X coordinate of the pixel rectangle.
  248. * @param y The minimum Y coordinate of the pixel rectangle.
  249. * @param w The width of the pixel rectangle.
  250. * @param h The height of the pixel rectangle.
  251. * @param obj If non-null, a primitive array in which to return
  252. * the pixel data.
  253. * @param data The DataBuffer containing the image data.
  254. * @see #getNumDataElements
  255. * @see #getTransferType
  256. * @see java.awt.image.DataBuffer
  257. */
  258. public Object getDataElements(int x, int y, int w, int h,
  259. Object obj, DataBuffer data) {
  260. int type = getTransferType();
  261. int numDataElems = getNumDataElements();
  262. int cnt = 0;
  263. Object o = null;
  264. switch(type) {
  265. case DataBuffer.TYPE_BYTE:
  266. byte[] btemp;
  267. byte[] bdata;
  268. if (obj == null)
  269. bdata = new byte[numDataElems*w*h];
  270. else
  271. bdata = (byte[])obj;
  272. for (int i=y; i<y+h; i++) {
  273. for (int j=x; j<x+w; j++) {
  274. o = getDataElements(j, i, o, data);
  275. btemp = (byte[])o;
  276. for (int k=0; k<numDataElems; k++) {
  277. bdata[cnt++] = btemp[k];
  278. }
  279. }
  280. }
  281. obj = (Object)bdata;
  282. break;
  283. case DataBuffer.TYPE_USHORT:
  284. short[] sdata;
  285. short[] stemp;
  286. if (obj == null)
  287. sdata = new short[numDataElems*w*h];
  288. else
  289. sdata = (short[])obj;
  290. for (int i=y; i<y+h; i++) {
  291. for (int j=x; j<x+w; j++) {
  292. o = getDataElements(j, i, o, data);
  293. stemp = (short[])o;
  294. for (int k=0; k<numDataElems; k++) {
  295. sdata[cnt++] = stemp[k];
  296. }
  297. }
  298. }
  299. obj = (Object)sdata;
  300. break;
  301. case DataBuffer.TYPE_INT:
  302. int[] idata;
  303. int[] itemp;
  304. if (obj == null)
  305. idata = new int[numDataElems*w*h];
  306. else
  307. idata = (int[])obj;
  308. for (int i=y; i<y+h; i++) {
  309. for (int j=x; j<x+w; j++) {
  310. o = getDataElements(j, i, o, data);
  311. itemp = (int[])o;
  312. for (int k=0; k<numDataElems; k++) {
  313. idata[cnt++] = itemp[k];
  314. }
  315. }
  316. }
  317. obj = (Object)idata;
  318. break;
  319. }
  320. return obj;
  321. }
  322. /**
  323. * Sets the data for a single pixel in the specified DataBuffer from a
  324. * primitive array of type TransferType. For image data supported by
  325. * the Java 2D API, this will be one of DataBuffer.TYPE_BYTE,
  326. * DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT. Data in the array
  327. * may be in a packed format, thus increasing efficiency for data
  328. * transfers.
  329. * <p>
  330. * The following code illustrates transferring data for one pixel from
  331. * DataBuffer <code>db1</code>, whose storage layout is described by
  332. * SampleModel <code>sm1</code>, to DataBuffer <code>db2</code>, whose
  333. * storage layout is described by SampleModel <code>sm2</code>.
  334. * The transfer will generally be more efficient than using
  335. * getPixel/setPixel.
  336. * <pre>
  337. * SampleModel sm1, sm2;
  338. * DataBuffer db1, db2;
  339. * sm2.setDataElements(x, y, sm1.getDataElements(x, y, null, db1),
  340. * db2);
  341. * </pre>
  342. * Using getDataElements/setDataElements to transfer between two
  343. * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
  344. * the same number of bands, corresponding bands have the same number of
  345. * bits per sample, and the TransferTypes are the same.
  346. * <p>
  347. * obj must be a primitive array of type TransferType. Otherwise,
  348. * a ClassCastException is thrown. An
  349. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  350. * not in bounds, or if obj is not large enough to hold the pixel data.
  351. * @param x The X coordinate of the pixel location.
  352. * @param y The Y coordinate of the pixel location.
  353. * @param obj A primitive array containing pixel data.
  354. * @param data The DataBuffer containing the image data.
  355. * @see #getNumDataElements
  356. * @see #getTransferType
  357. * @see java.awt.image.DataBuffer
  358. */
  359. public abstract void setDataElements(int x, int y,
  360. Object obj, DataBuffer data);
  361. /**
  362. * Sets the data for a rectangle of pixels in the specified DataBuffer
  363. * from a primitive array of type TransferType. For image data supported
  364. * by the Java 2D API, this will be one of DataBuffer.TYPE_BYTE,
  365. * DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT. Data in the array
  366. * may be in a packed format, thus increasing efficiency for data
  367. * transfers.
  368. * <p>
  369. * The following code illustrates transferring data for a rectangular
  370. * region of pixels from
  371. * DataBuffer <code>db1</code>, whose storage layout is described by
  372. * SampleModel <code>sm1</code>, to DataBuffer <code>db2</code>, whose
  373. * storage layout is described by SampleModel <code>sm2</code>.
  374. * The transfer will generally be more efficient than using
  375. * getPixels/setPixels.
  376. * <pre>
  377. * SampleModel sm1, sm2;
  378. * DataBuffer db1, db2;
  379. * sm2.setDataElements(x, y, w, h, sm1.getDataElements(x, y, w, h,
  380. * null, db1), db2);
  381. * </pre>
  382. * Using getDataElements/setDataElements to transfer between two
  383. * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
  384. * the same number of bands, corresponding bands have the same number of
  385. * bits per sample, and the TransferTypes are the same.
  386. * <p>
  387. * obj must be a primitive array of type TransferType. Otherwise,
  388. * a ClassCastException is thrown. An
  389. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  390. * not in bounds, or if obj is not large enough to hold the pixel data.
  391. * @param x The minimum X coordinate of the pixel rectangle.
  392. * @param y The minimum Y coordinate of the pixel rectangle.
  393. * @param w The width of the pixel rectangle.
  394. * @param h The height of the pixel rectangle.
  395. * @param obj A primitive array containing pixel data.
  396. * @param data The DataBuffer containing the image data.
  397. * @see #getNumDataElements
  398. * @see #getTransferType
  399. * @see java.awt.image.DataBuffer
  400. */
  401. public void setDataElements(int x, int y, int w, int h,
  402. Object obj, DataBuffer data) {
  403. int cnt = 0;
  404. Object o = null;
  405. int type = getTransferType();
  406. int numDataElems = getNumDataElements();
  407. switch(type) {
  408. case DataBuffer.TYPE_BYTE:
  409. byte[] barray = (byte[])obj;
  410. byte[] btemp = new byte[numDataElems];
  411. for (int i=y; i<y+h; i++) {
  412. for (int j=x; j<x+w; j++) {
  413. for (int k=0; k<numDataElems; k++) {
  414. btemp[k] = barray[cnt++];
  415. }
  416. setDataElements(j, i, btemp, data);
  417. }
  418. }
  419. break;
  420. case DataBuffer.TYPE_USHORT:
  421. short[] sarray = (short[])obj;
  422. short[] stemp = new short[numDataElems];
  423. for (int i=y; i<y+h; i++) {
  424. for (int j=x; j<x+w; j++) {
  425. for (int k=0; k<numDataElems; k++) {
  426. stemp[k] = sarray[cnt++];
  427. }
  428. setDataElements(j, i, stemp, data);
  429. }
  430. }
  431. break;
  432. case DataBuffer.TYPE_INT:
  433. int[] iArray = (int[])obj;
  434. int[] itemp = new int[numDataElems];
  435. for (int i=y; i<y+h; i++) {
  436. for (int j=x; j<x+w; j++) {
  437. for (int k=0; k<numDataElems; k++) {
  438. itemp[k] = iArray[cnt++];
  439. }
  440. setDataElements(j, i, itemp, data);
  441. }
  442. }
  443. break;
  444. }
  445. }
  446. /**
  447. * Returns the samples for the specified pixel in an array of float.
  448. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  449. * not in bounds.
  450. * @param x The X coordinate of the pixel location.
  451. * @param y The Y coordinate of the pixel location.
  452. * @param fArray If non-null, returns the samples in this array.
  453. * @param data The DataBuffer containing the image data.
  454. */
  455. public float[] getPixel(int x, int y, float fArray[],
  456. DataBuffer data) {
  457. float pixels[];
  458. if (fArray != null)
  459. pixels = fArray;
  460. else
  461. pixels = new float[numBands];
  462. for (int i=0; i<numBands; i++)
  463. pixels[i] = getSampleFloat(x, y, i, data);
  464. return pixels;
  465. }
  466. /**
  467. * Returns the samples for the specified pixel in an array of double.
  468. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  469. * not in bounds.
  470. * @param x The X coordinate of the pixel location.
  471. * @param y The Y coordinate of the pixel location.
  472. * @param dArray If non-null, returns the samples in this array.
  473. * @param data The DataBuffer containing the image data.
  474. */
  475. public double[] getPixel(int x, int y, double dArray[],
  476. DataBuffer data) {
  477. double pixels[];
  478. if(dArray != null)
  479. pixels = dArray;
  480. else
  481. pixels = new double[numBands];
  482. for (int i=0; i<numBands; i++)
  483. pixels[i] = getSampleDouble(x, y, i, data);
  484. return pixels;
  485. }
  486. /**
  487. * Returns all samples for a rectangle of pixels in an
  488. * int array, one sample per array element.
  489. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  490. * not in bounds.
  491. * @param x The X coordinate of the upper left pixel location.
  492. * @param y The Y coordinate of the upper left pixel location.
  493. * @param w The width of the pixel rectangle.
  494. * @param h The height of the pixel rectangle.
  495. * @param iArray If non-null, returns the samples in this array.
  496. * @param data The DataBuffer containing the image data.
  497. */
  498. public int[] getPixels(int x, int y, int w, int h,
  499. int iArray[], DataBuffer data) {
  500. int pixels[];
  501. int Offset=0;
  502. if (iArray != null)
  503. pixels = iArray;
  504. else
  505. pixels = new int[numBands * w * h];
  506. for (int i=y; i<(h+y); i++) {
  507. for (int j=x; j<(w+x); j++) {
  508. for(int k=0; k<numBands; k++) {
  509. pixels[Offset++] = getSample(j, i, k, data);
  510. }
  511. }
  512. }
  513. return pixels;
  514. }
  515. /**
  516. * Returns all samples for a rectangle of pixels in a float
  517. * array, one sample per array element.
  518. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  519. * not in bounds.
  520. * @param x The X coordinate of the upper left pixel location.
  521. * @param y The Y coordinate of the upper left pixel location.
  522. * @param w The width of the pixel rectangle.
  523. * @param h The height of the pixel rectangle.
  524. * @param fArray If non-null, returns the samples in this array.
  525. * @param data The DataBuffer containing the image data.
  526. */
  527. public float[] getPixels(int x, int y, int w, int h,
  528. float fArray[], DataBuffer data) {
  529. float pixels[];
  530. int Offset = 0;
  531. if (fArray != null)
  532. pixels = fArray;
  533. else
  534. pixels = new float[numBands * w * h];
  535. for (int i=y; i<(h+y); i++) {
  536. for(int j=x; j<(w+x); j++) {
  537. for(int k=0; k<numBands; k++) {
  538. pixels[Offset++] = getSampleFloat(j, i, k, data);
  539. }
  540. }
  541. }
  542. return pixels;
  543. }
  544. /**
  545. * Returns all samples for a rectangle of pixels in a double
  546. * array, one sample per array element.
  547. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  548. * not in bounds.
  549. * @param x The X coordinate of the upper left pixel location.
  550. * @param y The Y coordinate of the upper left pixel location.
  551. * @param w The width of the pixel rectangle.
  552. * @param h The height of the pixel rectangle.
  553. * @param dArray If non-null, returns the samples in this array.
  554. * @param data The DataBuffer containing the image data.
  555. */
  556. public double[] getPixels(int x, int y, int w, int h,
  557. double dArray[], DataBuffer data) {
  558. double pixels[];
  559. int Offset = 0;
  560. if (dArray != null)
  561. pixels = dArray;
  562. else
  563. pixels = new double[numBands * w * h];
  564. for (int i=0; i<(h+y); i++) {
  565. for (int j=0; j<(w+x); j++) {
  566. for (int k=0; k<numBands; k++) {
  567. pixels[Offset++] = getSampleDouble(j, i, k, data);
  568. }
  569. }
  570. }
  571. return pixels;
  572. }
  573. /**
  574. * Returns the sample in a specified band for the pixel located
  575. * at (x,y) as an int.
  576. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  577. * not in bounds.
  578. * @param x The X coordinate of the pixel location.
  579. * @param y The Y coordinate of the pixel location.
  580. * @param b The band to return.
  581. * @param data The DataBuffer containing the image data.
  582. */
  583. public abstract int getSample(int x, int y, int b, DataBuffer data);
  584. /**
  585. * Returns the sample in a specified band
  586. * for the pixel located at (x,y) as a float.
  587. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  588. * not in bounds.
  589. * @param x The X coordinate of the pixel location.
  590. * @param y The Y coordinate of the pixel location.
  591. * @param b The band to return.
  592. * @param data The DataBuffer containing the image data.
  593. */
  594. public float getSampleFloat(int x, int y, int b, DataBuffer data) {
  595. float sample;
  596. sample = (float) getSample(x, y, b, data);
  597. return sample;
  598. }
  599. /**
  600. * Returns the sample in a specified band
  601. * for a pixel located at (x,y) as a double.
  602. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  603. * not in bounds.
  604. * @param x The X coordinate of the pixel location.
  605. * @param y The Y coordinate of the pixel location.
  606. * @param b The band to return.
  607. * @param data The DataBuffer containing the image data.
  608. */
  609. public double getSampleDouble(int x, int y, int b, DataBuffer data) {
  610. double sample;
  611. sample = (double) getSample(x, y, b, data);
  612. return sample;
  613. }
  614. /**
  615. * Returns the samples for a specified band for the specified rectangle
  616. * of pixels in an int array, one sample per array element.
  617. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  618. * not in bounds.
  619. * @param x The X coordinate of the upper left pixel location.
  620. * @param y The Y coordinate of the upper left pixel location.
  621. * @param w The width of the pixel rectangle.
  622. * @param h The height of the pixel rectangle.
  623. * @param b The band to return.
  624. * @param iArray If non-null, returns the samples in this array.
  625. * @param data The DataBuffer containing the image data.
  626. */
  627. public int[] getSamples(int x, int y, int w, int h, int b,
  628. int iArray[], DataBuffer data) {
  629. int pixels[];
  630. int Offset=0;
  631. if (iArray != null)
  632. pixels = iArray;
  633. else
  634. pixels = new int[w * h];
  635. for(int i=y; i<(h+y); i++) {
  636. for (int j=x; j<(w+x); j++) {
  637. pixels[Offset++] = getSample(j, i, b, data);
  638. }
  639. }
  640. return pixels;
  641. }
  642. /**
  643. * Returns the samples for a specified band for the specified rectangle
  644. * of pixels in a float array, one sample per array element.
  645. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  646. * not in bounds.
  647. * @param x The X coordinate of the upper left pixel location.
  648. * @param y The Y coordinate of the upper left pixel location.
  649. * @param w The width of the pixel rectangle.
  650. * @param h The height of the pixel rectangle.
  651. * @param b The band to return.
  652. * @param fArray If non-null, returns the samples in this array.
  653. * @param data The DataBuffer containing the image data.
  654. */
  655. public float[] getSamples(int x, int y, int w, int h,
  656. int b, float fArray[],
  657. DataBuffer data) {
  658. float pixels[];
  659. int Offset=0;
  660. if (fArray != null)
  661. pixels = fArray;
  662. else
  663. pixels = new float[w * h];
  664. for (int i=y; i<(h+y); i++) {
  665. for (int j=x; j<(w+x); j++) {
  666. pixels[Offset++] = getSampleFloat(j, i, b, data);
  667. }
  668. }
  669. return pixels;
  670. }
  671. /**
  672. * Returns the samples for a specified band for a specified rectangle
  673. * of pixels in a double array, one sample per array element.
  674. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  675. * not in bounds.
  676. * @param x The X coordinate of the upper left pixel location.
  677. * @param y The Y coordinate of the upper left pixel location.
  678. * @param w The width of the pixel rectangle.
  679. * @param h The height of the pixel rectangle.
  680. * @param b The band to return.
  681. * @param dArray If non-null, returns the samples in this array.
  682. * @param data The DataBuffer containing the image data.
  683. */
  684. public double[] getSamples(int x, int y, int w, int h,
  685. int b, double dArray[],
  686. DataBuffer data) {
  687. double pixels[];
  688. int Offset=0;
  689. if (dArray != null)
  690. pixels = dArray;
  691. else
  692. pixels = new double[w * h];
  693. for (int i=y; i<(y+h); i++) {
  694. for (int j=x; j<(x+w); j++) {
  695. pixels[Offset++] = getSampleDouble(j, i, b, data);
  696. }
  697. }
  698. return pixels;
  699. }
  700. /**
  701. * Sets a pixel in the DataBuffer using an int array of samples for input.
  702. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  703. * not in bounds.
  704. * @param x The X coordinate of the pixel location.
  705. * @param y The Y coordinate of the pixel location.
  706. * @param iArray The input samples in an int array.
  707. * @param data The DataBuffer containing the image data.
  708. */
  709. public void setPixel(int x, int y, int iArray[], DataBuffer data) {
  710. for (int i=0; i<numBands; i++)
  711. setSample(x, y, i, iArray[i], data);
  712. }
  713. /**
  714. * Sets a pixel in the DataBuffer using a float array of samples for input.
  715. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  716. * not in bounds.
  717. * @param x The X coordinate of the pixel location.
  718. * @param y The Y coordinate of the pixel location.
  719. * @param fArray The input samples in a float array.
  720. * @param data The DataBuffer containing the image data.
  721. */
  722. public void setPixel(int x, int y, float fArray[], DataBuffer data) {
  723. for (int i=0; i<numBands; i++)
  724. setSample(x, y, i, fArray[i], data);
  725. }
  726. /**
  727. * Sets a pixel in the DataBuffer using a double array of samples for input.
  728. * @param x The X coordinate of the pixel location.
  729. * @param y The Y coordinate of the pixel location.
  730. * @param dArray The input samples in a double array.
  731. * @param data The DataBuffer containing the image data.
  732. */
  733. public void setPixel(int x, int y, double dArray[], DataBuffer data) {
  734. for (int i=0; i<numBands; i++)
  735. setSample(x, y, i, dArray[i], data);
  736. }
  737. /**
  738. * Sets all samples for a rectangle of pixels from an int array containing
  739. * one sample per array element.
  740. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  741. * not in bounds.
  742. * @param x The X coordinate of the upper left pixel location.
  743. * @param y The Y coordinate of the upper left pixel location.
  744. * @param w The width of the pixel rectangle.
  745. * @param h The height of the pixel rectangle.
  746. * @param iArray The input samples in an int array.
  747. * @param data The DataBuffer containing the image data.
  748. */
  749. public void setPixels(int x, int y, int w, int h,
  750. int iArray[], DataBuffer data) {
  751. int Offset=0;
  752. for (int i=y; i<(y+h); i++) {
  753. for (int j=x; j<(x+w); j++) {
  754. for (int k=0; k<numBands; k++) {
  755. setSample(j, i, k, iArray[Offset++], data);
  756. }
  757. }
  758. }
  759. }
  760. /**
  761. * Sets all samples for a rectangle of pixels from a float array containing
  762. * one sample per array element.
  763. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  764. * not in bounds.
  765. * @param x The X coordinate of the upper left pixel location.
  766. * @param y The Y coordinate of the upper left pixel location.
  767. * @param w The width of the pixel rectangle.
  768. * @param h The height of the pixel rectangle.
  769. * @param fArray The input samples in a float array.
  770. * @param data The DataBuffer containing the image data.
  771. */
  772. public void setPixels(int x, int y, int w, int h,
  773. float fArray[], DataBuffer data) {
  774. int Offset=0;
  775. for (int i=y; i<(y+h); i++) {
  776. for (int j=x; j<(x+w); j++) {
  777. for(int k=0; k<numBands; k++) {
  778. setSample(j, i, k, fArray[Offset++], data);
  779. }
  780. }
  781. }
  782. }
  783. /**
  784. * Sets all samples for a rectangle of pixels from a double array containing
  785. * one sample per array element.
  786. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  787. * not in bounds.
  788. * @param x The X coordinate of the upper left pixel location.
  789. * @param y The Y coordinate of the upper left pixel location.
  790. * @param w The width of the pixel rectangle.
  791. * @param h The height of the pixel rectangle.
  792. * @param dArray The input samples in a double array.
  793. * @param data The DataBuffer containing the image data.
  794. */
  795. public void setPixels(int x, int y, int w, int h,
  796. double dArray[], DataBuffer data) {
  797. int Offset=0;
  798. for (int i=y; i<(y+h); i++) {
  799. for (int j=x; j<(x+w); j++) {
  800. for (int k=0; k<numBands; k++) {
  801. setSample(j, i, k, dArray[Offset++], data);
  802. }
  803. }
  804. }
  805. }
  806. /**
  807. * Sets a sample in the specified band for the pixel located at (x,y)
  808. * in the DataBuffer using an int for input.
  809. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  810. * not in bounds.
  811. * @param x The X coordinate of the pixel location.
  812. * @param y The Y coordinate of the pixel location.
  813. * @param b The band to set.
  814. * @param s The input sample as an int.
  815. * @param data The DataBuffer containing the image data.
  816. */
  817. public abstract void setSample(int x, int y, int b,
  818. int s,
  819. DataBuffer data);
  820. /**
  821. * Sets a sample in the specified band for the pixel located at (x,y)
  822. * in the DataBuffer using a float for input.
  823. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  824. * not in bounds.
  825. * @param x The X coordinate of the pixel location.
  826. * @param y The Y coordinate of the pixel location.
  827. * @param b The band to set.
  828. * @param s The input sample as a float.
  829. * @param data The DataBuffer containing the image data.
  830. */
  831. public void setSample(int x, int y, int b,
  832. float s ,
  833. DataBuffer data) {
  834. int sample = (int)s;
  835. setSample(x, y, b, sample, data);
  836. }
  837. /**
  838. * Sets a sample in the specified band for the pixel located at (x,y)
  839. * in the DataBuffer using a double for input.
  840. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  841. * not in bounds.
  842. * @param x The X coordinate of the pixel location.
  843. * @param y The Y coordinate of the pixel location.
  844. * @param b The band to set.
  845. * @param s The input sample as a double.
  846. * @param data The DataBuffer containing the image data.
  847. */
  848. public void setSample(int x, int y, int b,
  849. double s,
  850. DataBuffer data) {
  851. int sample = (int)s;
  852. setSample(x, y, b, sample, data);
  853. }
  854. /**
  855. * Sets the samples in the specified band for the specified rectangle
  856. * of pixels from an int array containing one sample per array element.
  857. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  858. * not in bounds.
  859. * @param x The X coordinate of the upper left pixel location.
  860. * @param y The Y coordinate of the upper left pixel location.
  861. * @param w The width of the pixel rectangle.
  862. * @param h The height of the pixel rectangle.
  863. * @param b The band to set.
  864. * @param iArray The input samples in an int array.
  865. * @param data The DataBuffer containing the image data.
  866. */
  867. public void setSamples(int x, int y, int w, int h, int b,
  868. int iArray[], DataBuffer data) {
  869. int Offset=0;
  870. for (int i=y; i<(y+h); i++) {
  871. for (int j=x; j<(x+w); j++) {
  872. setSample(j, i, b, iArray[Offset++], data);
  873. }
  874. }
  875. }
  876. /**
  877. * Sets the samples in the specified band for the specified rectangle
  878. * of pixels from a float array containing one sample per array element.
  879. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  880. * not in bounds.
  881. * @param x The X coordinate of the upper left pixel location.
  882. * @param y The Y coordinate of the upper left pixel location.
  883. * @param w The width of the pixel rectangle.
  884. * @param h The height of the pixel rectangle.
  885. * @param b The band to set.
  886. * @param fArray The input samples in a float array.
  887. * @param data The DataBuffer containing the image data.
  888. */
  889. public void setSamples(int x, int y, int w, int h, int b,
  890. float fArray[], DataBuffer data) {
  891. int Offset=0;
  892. for (int i=y; i<(y+h); i++) {
  893. for (int j=x; j<(x+w); j++) {
  894. setSample(j, i, b, fArray[Offset++], data);
  895. }
  896. }
  897. }
  898. /**
  899. * Sets the samples in the specified band for the specified rectangle
  900. * of pixels from a double array containing one sample per array element.
  901. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
  902. * not in bounds.
  903. * @param x The X coordinate of the upper left pixel location.
  904. * @param y The Y coordinate of the upper left pixel location.
  905. * @param w The width of the pixel rectangle.
  906. * @param h The height of the pixel rectangle.
  907. * @param b The band to set.
  908. * @param dArray The input samples in a double array.
  909. * @param data The DataBuffer containing the image data.
  910. */
  911. public void setSamples(int x, int y, int w, int h, int b,
  912. double dArray[], DataBuffer data) {
  913. int Offset=0;
  914. for (int i=y; i<(y+h); i++) {
  915. for (int j=x; j<(x+w); j++) {
  916. setSample(j, i, b, dArray[Offset++], data);
  917. }
  918. }
  919. }
  920. /**
  921. * Creates a SampleModel which describes data in this SampleModel's
  922. * format, but with a different width and height.
  923. */
  924. public abstract SampleModel createCompatibleSampleModel(int w, int h);
  925. /**
  926. * Creates a new SampleModel
  927. * with a subset of the bands of this
  928. * SampleModel.
  929. */
  930. public abstract SampleModel createSubsetSampleModel(int bands[]);
  931. /**
  932. * Creates a DataBuffer that corresponds to this SampleModel.
  933. * The DataBuffer's width and height will match this SampleModel's.
  934. */
  935. public abstract DataBuffer createDataBuffer();
  936. /** Returns the size in bits of samples for all bands. */
  937. public abstract int[] getSampleSize();
  938. /** Returns the size in bits of samples for the specified band. */
  939. public abstract int getSampleSize(int band);
  940. }