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