1. /*
  2. * @(#)ComponentSampleModel.java 1.44 03/12/19
  3. *
  4. * Copyright 2004 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. import java.util.Arrays;
  18. /**
  19. * This class represents image data which is stored such that each sample
  20. * of a pixel occupies one data element of the DataBuffer. It stores the
  21. * N samples which make up a pixel in N separate data array elements.
  22. * Different bands may be in different banks of the DataBuffer.
  23. * Accessor methods are provided so that image data can be manipulated
  24. * directly. This class can support different kinds of interleaving, e.g.
  25. * band interleaving, scanline interleaving, and pixel interleaving.
  26. * Pixel stride is the number of data array elements between two samples
  27. * for the same band on the same scanline. Scanline stride is the number
  28. * of data array elements between a given sample and the corresponding sample
  29. * in the same column of the next scanline. Band offsets denote the number
  30. * of data array elements from the first data array element of the bank
  31. * of the DataBuffer holding each band to the first sample of the band.
  32. * The bands are numbered from 0 to N-1. This class can represent image
  33. * data for which each sample is an unsigned integral number which can be
  34. * stored in 8, 16, or 32 bits (using <code>DataBuffer.TYPE_BYTE</code>,
  35. * <code>DataBuffer.TYPE_USHORT</code>, or <code>DataBuffer.TYPE_INT</code>,
  36. * respectively), data for which each sample is a signed integral number
  37. * which can be stored in 16 bits (using <code>DataBuffer.TYPE_SHORT</code>),
  38. * or data for which each sample is a signed float or double quantity
  39. * (using <code>DataBuffer.TYPE_FLOAT</code> or
  40. * <code>DataBuffer.TYPE_DOUBLE</code>, respectively).
  41. * All samples of a given ComponentSampleModel
  42. * are stored with the same precision. All strides and offsets must be
  43. * non-negative. This class supports
  44. * {@link DataBuffer#TYPE_BYTE TYPE_BYTE},
  45. * {@link DataBuffer#TYPE_USHORT TYPE_USHORT},
  46. * {@link DataBuffer#TYPE_SHORT TYPE_SHORT},
  47. * {@link DataBuffer#TYPE_INT TYPE_INT},
  48. * {@link DataBuffer#TYPE_FLOAT TYPE_FLOAT},
  49. * {@link DataBuffer#TYPE_DOUBLE TYPE_DOUBLE},
  50. * @see java.awt.image.PixelInterleavedSampleModel
  51. * @see java.awt.image.BandedSampleModel
  52. */
  53. public class ComponentSampleModel extends SampleModel
  54. {
  55. /** Offsets for all bands in data array elements. */
  56. protected int bandOffsets[];
  57. /** Index for each bank storing a band of image data. */
  58. protected int[] bankIndices;
  59. /**
  60. * The number of bands in this
  61. * <code>ComponentSampleModel</code>.
  62. */
  63. protected int numBands = 1;
  64. /**
  65. * The number of banks in this
  66. * <code>ComponentSampleModel</code>.
  67. */
  68. protected int numBanks = 1;
  69. /**
  70. * Line stride (in data array elements) of the region of image
  71. * data described by this ComponentSampleModel.
  72. */
  73. protected int scanlineStride;
  74. /** Pixel stride (in data array elements) of the region of image
  75. * data described by this ComponentSampleModel.
  76. */
  77. protected int pixelStride;
  78. static private native void initIDs();
  79. static {
  80. ColorModel.loadLibraries();
  81. initIDs();
  82. }
  83. /**
  84. * Constructs a ComponentSampleModel with the specified parameters.
  85. * The number of bands will be given by the length of the bandOffsets array.
  86. * All bands will be stored in the first bank of the DataBuffer.
  87. * @param dataType the data type for storing samples
  88. * @param w the width (in pixels) of the region of
  89. * image data described
  90. * @param h the height (in pixels) of the region of
  91. * image data described
  92. * @param pixelStride the pixel stride of the region of image
  93. * data described
  94. * @param scanlineStride the line stride of the region of image
  95. * data described
  96. * @param bandOffsets the offsets of all bands
  97. * @throws IllegalArgumentException if <code>w</code> or
  98. * <code>h</code> is not greater than 0
  99. * @throws IllegalArgumentException if <code>pixelStride</code>
  100. * is less than 0
  101. * @throws IllegalArgumentException if <code>scanlineStride</code>
  102. * is less than 0
  103. * @throws IllegalArgumentException if <code>numBands</code>
  104. * is less than 1
  105. * @throws IllegalArgumentException if the product of <code>w</code>
  106. * and <code>h</code> is greater than
  107. * <code>Integer.MAX_VALUE</code>
  108. * @throws IllegalArgumentException if <code>dataType</code> is not
  109. * one of the supported data types
  110. */
  111. public ComponentSampleModel(int dataType,
  112. int w, int h,
  113. int pixelStride,
  114. int scanlineStride,
  115. int bandOffsets[]) {
  116. super(dataType, w, h, bandOffsets.length);
  117. this.dataType = dataType;
  118. this.pixelStride = pixelStride;
  119. this.scanlineStride = scanlineStride;
  120. this.bandOffsets = (int[])bandOffsets.clone();
  121. numBands = bandOffsets.length;
  122. if (pixelStride < 0) {
  123. throw new IllegalArgumentException("Pixel stride must be >= 0");
  124. }
  125. // TODO - bug 4296691 - remove this check
  126. if (scanlineStride < 0) {
  127. throw new IllegalArgumentException("Scanline stride must be >= 0");
  128. }
  129. if (numBands < 1) {
  130. throw new IllegalArgumentException("Must have at least one band.");
  131. }
  132. if ((dataType < DataBuffer.TYPE_BYTE) ||
  133. (dataType > DataBuffer.TYPE_DOUBLE)) {
  134. throw new IllegalArgumentException("Unsupported dataType.");
  135. }
  136. bankIndices = new int[numBands];
  137. for (int i=0; i<numBands; i++) {
  138. bankIndices[i] = 0;
  139. }
  140. }
  141. /**
  142. * Constructs a ComponentSampleModel with the specified parameters.
  143. * The number of bands will be given by the length of the bandOffsets array.
  144. * Different bands may be stored in different banks of the DataBuffer.
  145. *
  146. * @param dataType the data type for storing samples
  147. * @param w the width (in pixels) of the region of
  148. * image data described
  149. * @param h the height (in pixels) of the region of
  150. * image data described
  151. * @param pixelStride the pixel stride of the region of image
  152. * data described
  153. * @param scanlineStride The line stride of the region of image
  154. * data described
  155. * @param bankIndices the bank indices of all bands
  156. * @param bandOffsets the band offsets of all bands
  157. * @throws IllegalArgumentException if <code>w</code> or
  158. * <code>h</code> is not greater than 0
  159. * @throws IllegalArgumentException if <code>pixelStride</code>
  160. * is less than 0
  161. * @throws IllegalArgumentException if <code>scanlineStride</code>
  162. * is less than 0
  163. * @throws IllegalArgumentException if the length of
  164. * <code>bankIndices</code> does not equal the length of
  165. * <code>bankOffsets</code>
  166. * @throws IllegalArgumentException if any of the bank indices
  167. * of <code>bandIndices</code> is less than 0
  168. * @throws IllegalArgumentException if <code>dataType</code> is not
  169. * one of the supported data types
  170. */
  171. public ComponentSampleModel(int dataType,
  172. int w, int h,
  173. int pixelStride,
  174. int scanlineStride,
  175. int bankIndices[],
  176. int bandOffsets[]) {
  177. super(dataType, w, h, bandOffsets.length);
  178. this.dataType = dataType;
  179. this.pixelStride = pixelStride;
  180. this.scanlineStride = scanlineStride;
  181. this.bandOffsets = (int[])bandOffsets.clone();
  182. this.bankIndices = (int[]) bankIndices.clone();
  183. if (pixelStride < 0) {
  184. throw new IllegalArgumentException("Pixel stride must be >= 0");
  185. }
  186. // TODO - bug 4296691 - remove this check
  187. if (scanlineStride < 0) {
  188. throw new IllegalArgumentException("Scanline stride must be >= 0");
  189. }
  190. if ((dataType < DataBuffer.TYPE_BYTE) ||
  191. (dataType > DataBuffer.TYPE_DOUBLE)) {
  192. throw new IllegalArgumentException("Unsupported dataType.");
  193. }
  194. int maxBank = bankIndices[0];
  195. if (maxBank < 0) {
  196. throw new IllegalArgumentException("Index of bank 0 is less than "+
  197. "0 ("+maxBank+")");
  198. }
  199. for (int i=1; i < bankIndices.length; i++) {
  200. if (bankIndices[i] > maxBank) {
  201. maxBank = bankIndices[i];
  202. }
  203. else if (bankIndices[i] < 0) {
  204. throw new IllegalArgumentException("Index of bank "+i+
  205. " is less than 0 ("+
  206. maxBank+")");
  207. }
  208. }
  209. numBanks = maxBank+1;
  210. numBands = bandOffsets.length;
  211. if (bandOffsets.length != bankIndices.length) {
  212. throw new IllegalArgumentException("Length of bandOffsets must "+
  213. "equal length of bankIndices.");
  214. }
  215. }
  216. /**
  217. * Returns the size of the data buffer (in data elements) needed
  218. * for a data buffer that matches this ComponentSampleModel.
  219. */
  220. private long getBufferSize() {
  221. int maxBandOff=bandOffsets[0];
  222. for (int i=1; i<bandOffsets.length; i++)
  223. maxBandOff = Math.max(maxBandOff,bandOffsets[i]);
  224. long size = 0;
  225. if (maxBandOff >= 0)
  226. size += maxBandOff+1;
  227. if (pixelStride > 0)
  228. size += pixelStride * (width-1);
  229. if (scanlineStride > 0)
  230. size += scanlineStride*(height-1);
  231. return size;
  232. }
  233. /**
  234. * Preserves band ordering with new step factor...
  235. */
  236. int []orderBands(int orig[], int step) {
  237. int map[] = new int[orig.length];
  238. int ret[] = new int[orig.length];
  239. for (int i=0; i<map.length; i++) map[i] = i;
  240. for (int i = 0; i < ret.length; i++) {
  241. int index = i;
  242. for (int j = i+1; j < ret.length; j++) {
  243. if (orig[map[index]] > orig[map[j]]) {
  244. index = j;
  245. }
  246. }
  247. ret[map[index]] = i*step;
  248. map[index] = map[i];
  249. }
  250. return ret;
  251. }
  252. /**
  253. * Creates a new <code>ComponentSampleModel</code> with the specified
  254. * width and height. The new <code>SampleModel</code> will have the same
  255. * number of bands, storage data type, interleaving scheme, and
  256. * pixel stride as this <code>SampleModel</code>.
  257. * @param w the width of the resulting <code>SampleModel</code>
  258. * @param h the height of the resulting <code>SampleModel</code>
  259. * @return a new <code>ComponentSampleModel</code> with the specified size
  260. * @throws IllegalArgumentException if <code>w</code> or
  261. * <code>h</code> is not greater than 0
  262. */
  263. public SampleModel createCompatibleSampleModel(int w, int h) {
  264. SampleModel ret=null;
  265. long size;
  266. int minBandOff=bandOffsets[0];
  267. int maxBandOff=bandOffsets[0];
  268. for (int i=1; i<bandOffsets.length; i++) {
  269. minBandOff = Math.min(minBandOff,bandOffsets[i]);
  270. maxBandOff = Math.max(maxBandOff,bandOffsets[i]);
  271. }
  272. maxBandOff -= minBandOff;
  273. int bands = bandOffsets.length;
  274. int bandOff[];
  275. int pStride = Math.abs(pixelStride);
  276. int lStride = Math.abs(scanlineStride);
  277. int bStride = Math.abs(maxBandOff);
  278. if (pStride > lStride) {
  279. if (pStride > bStride) {
  280. if (lStride > bStride) { // pix > line > band
  281. bandOff = new int[bandOffsets.length];
  282. for (int i=0; i<bands; i++)
  283. bandOff[i] = bandOffsets[i]-minBandOff;
  284. lStride = bStride+1;
  285. pStride = lStride*h;
  286. } else { // pix > band > line
  287. bandOff = orderBands(bandOffsets,lStride*h);
  288. pStride = bands*lStride*h;
  289. }
  290. } else { // band > pix > line
  291. pStride = lStride*h;
  292. bandOff = orderBands(bandOffsets,pStride*w);
  293. }
  294. } else {
  295. if (pStride > bStride) { // line > pix > band
  296. bandOff = new int[bandOffsets.length];
  297. for (int i=0; i<bands; i++)
  298. bandOff[i] = bandOffsets[i]-minBandOff;
  299. pStride = bStride+1;
  300. lStride = pStride*w;
  301. } else {
  302. if (lStride > bStride) { // line > band > pix
  303. bandOff = orderBands(bandOffsets,pStride*w);
  304. lStride = bands*pStride*w;
  305. } else { // band > line > pix
  306. lStride = pStride*w;
  307. bandOff = orderBands(bandOffsets,lStride*h);
  308. }
  309. }
  310. }
  311. // make sure we make room for negative offsets...
  312. int base = 0;
  313. if (scanlineStride < 0) {
  314. base += lStride*h;
  315. lStride *= -1;
  316. }
  317. if (pixelStride < 0) {
  318. base += pStride*w;
  319. pStride *= -1;
  320. }
  321. for (int i=0; i<bands; i++)
  322. bandOff[i] += base;
  323. return new ComponentSampleModel(dataType, w, h, pStride,
  324. lStride, bankIndices, bandOff);
  325. }
  326. /**
  327. * Creates a new ComponentSampleModel with a subset of the bands
  328. * of this ComponentSampleModel. The new ComponentSampleModel can be
  329. * used with any DataBuffer that the existing ComponentSampleModel
  330. * can be used with. The new ComponentSampleModel/DataBuffer
  331. * combination will represent an image with a subset of the bands
  332. * of the original ComponentSampleModel/DataBuffer combination.
  333. * @param bands a subset of bands from this
  334. * <code>ComponentSampleModel</code>
  335. * @return a <code>ComponentSampleModel</code> created with a subset
  336. * of bands from this <code>ComponentSampleModel</code>.
  337. */
  338. public SampleModel createSubsetSampleModel(int bands[]) {
  339. if (bands.length > bankIndices.length)
  340. throw new RasterFormatException("There are only " +
  341. bankIndices.length +
  342. " bands");
  343. int newBankIndices[] = new int[bands.length];
  344. int newBandOffsets[] = new int[bands.length];
  345. for (int i=0; i<bands.length; i++) {
  346. newBankIndices[i] = bankIndices[bands[i]];
  347. newBandOffsets[i] = bandOffsets[bands[i]];
  348. }
  349. return new ComponentSampleModel(this.dataType, width, height,
  350. this.pixelStride,
  351. this.scanlineStride,
  352. newBankIndices, newBandOffsets);
  353. }
  354. /**
  355. * Creates a <code>DataBuffer</code> that corresponds to this
  356. * <code>ComponentSampleModel</code>.
  357. * The <code>DataBuffer</code> object's data type, number of banks,
  358. * and size are be consistent with this <code>ComponentSampleModel</code>.
  359. * @return a <code>DataBuffer</code> whose data type, number of banks
  360. * and size are consistent with this
  361. * <code>ComponentSampleModel</code>.
  362. */
  363. public DataBuffer createDataBuffer() {
  364. DataBuffer dataBuffer = null;
  365. int size = (int)getBufferSize();
  366. switch (dataType) {
  367. case DataBuffer.TYPE_BYTE:
  368. dataBuffer = new DataBufferByte(size, numBanks);
  369. break;
  370. case DataBuffer.TYPE_USHORT:
  371. dataBuffer = new DataBufferUShort(size, numBanks);
  372. break;
  373. case DataBuffer.TYPE_SHORT:
  374. dataBuffer = new DataBufferShort(size, numBanks);
  375. break;
  376. case DataBuffer.TYPE_INT:
  377. dataBuffer = new DataBufferInt(size, numBanks);
  378. break;
  379. case DataBuffer.TYPE_FLOAT:
  380. dataBuffer = new DataBufferFloat(size, numBanks);
  381. break;
  382. case DataBuffer.TYPE_DOUBLE:
  383. dataBuffer = new DataBufferDouble(size, numBanks);
  384. break;
  385. }
  386. return dataBuffer;
  387. }
  388. /** Gets the offset for the first band of pixel (x,y).
  389. * A sample of the first band can be retrieved from a
  390. * <code>DataBuffer</code>
  391. * <code>data</code> with a <code>ComponentSampleModel</code>
  392. * <code>csm</code> as
  393. * <pre>
  394. * data.getElem(csm.getOffset(x, y));
  395. * </pre>
  396. * @param x, y the location of the pixel
  397. * @return the offset for the first band of the specified pixel.
  398. */
  399. public int getOffset(int x, int y) {
  400. int offset = y*scanlineStride + x*pixelStride + bandOffsets[0];
  401. return offset;
  402. }
  403. /** Gets the offset for band b of pixel (x,y).
  404. * A sample of band <code>b</code> can be retrieved from a
  405. * <code>DataBuffer</code> <code>data</code>
  406. * with a <code>ComponentSampleModel</code> <code>csm</code> as
  407. * <pre>
  408. * data.getElem(csm.getOffset(x, y, b));
  409. * </pre>
  410. * @param x, y the location of the specified pixel
  411. * @param b the specified band
  412. * @return the offset for the specified band of the specified pixel.
  413. */
  414. public int getOffset(int x, int y, int b) {
  415. int offset = y*scanlineStride + x*pixelStride + bandOffsets[b];
  416. return offset;
  417. }
  418. /** Returns the number of bits per sample for all bands.
  419. * @return an array containing the number of bits per sample
  420. * for all bands, where each element in the array
  421. * represents a band.
  422. */
  423. public final int[] getSampleSize() {
  424. int sampleSize[] = new int [numBands];
  425. int sizeInBits = getSampleSize(0);
  426. for (int i=0; i<numBands; i++)
  427. sampleSize[i] = sizeInBits;
  428. return sampleSize;
  429. }
  430. /** Returns the number of bits per sample for the specified band.
  431. * @param band the specified band
  432. * @return the number of bits per sample for the specified band.
  433. */
  434. public final int getSampleSize(int band) {
  435. return DataBuffer.getDataTypeSize(dataType);
  436. }
  437. /** Returns the bank indices for all bands.
  438. * @return the bank indices for all bands.
  439. */
  440. public final int [] getBankIndices() {
  441. return (int[]) bankIndices.clone();
  442. }
  443. /** Returns the band offset for all bands.
  444. * @return the band offsets for all bands.
  445. */
  446. public final int [] getBandOffsets() {
  447. return (int[])bandOffsets.clone();
  448. }
  449. /** Returns the scanline stride of this ComponentSampleModel.
  450. * @return the scanline stride of this <code>ComponentSampleModel</code>.
  451. */
  452. public final int getScanlineStride() {
  453. return scanlineStride;
  454. }
  455. /** Returns the pixel stride of this ComponentSampleModel.
  456. * @return the pixel stride of this <code>ComponentSampleModel</code>.
  457. */
  458. public final int getPixelStride() {
  459. return pixelStride;
  460. }
  461. /**
  462. * Returns the number of data elements needed to transfer a pixel
  463. * with the
  464. * {@link #getDataElements(int, int, Object, DataBuffer) } and
  465. * {@link #setDataElements(int, int, Object, DataBuffer) }
  466. * methods.
  467. * For a <code>ComponentSampleModel</code>, this is identical to the
  468. * number of bands.
  469. * @return the number of data elements needed to transfer a pixel with
  470. * the <code>getDataElements</code> and
  471. * <code>setDataElements</code> methods.
  472. * @see java.awt.image.SampleModel#getNumDataElements
  473. * @see #getNumBands
  474. */
  475. public final int getNumDataElements() {
  476. return getNumBands();
  477. }
  478. /**
  479. * Returns data for a single pixel in a primitive array of type
  480. * <code>TransferType</code>. For a <code>ComponentSampleModel</code>,
  481. * this is the same as the data type, and samples are returned
  482. * one per array element. Generally, <code>obj</code> should
  483. * be passed in as <code>null</code>, so that the <code>Object</code>
  484. * is created automatically and is the right primitive data type.
  485. * <p>
  486. * The following code illustrates transferring data for one pixel from
  487. * <code>DataBuffer</code> <code>db1</code>, whose storage layout is
  488. * described by <code>ComponentSampleModel</code> <code>csm1</code>,
  489. * to <code>DataBuffer</code> <code>db2</code>, whose storage layout
  490. * is described by <code>ComponentSampleModel</code> <code>csm2</code>.
  491. * The transfer is usually more efficient than using
  492. * <code>getPixel</code> and <code>setPixel</code>.
  493. * <pre>
  494. * ComponentSampleModel csm1, csm2;
  495. * DataBufferInt db1, db2;
  496. * csm2.setDataElements(x, y,
  497. * csm1.getDataElements(x, y, null, db1), db2);
  498. * </pre>
  499. *
  500. * Using <code>getDataElements</code> and <code>setDataElements</code>
  501. * to transfer between two <code>DataBuffer/SampleModel</code>
  502. * pairs is legitimate if the <code>SampleModel</code> objects have
  503. * the same number of bands, corresponding bands have the same number of
  504. * bits per sample, and the <code>TransferType</code>s are the same.
  505. * <p>
  506. * If <code>obj</code> is not <code>null</code>, it should be a
  507. * primitive array of type <code>TransferType</code>.
  508. * Otherwise, a <code>ClassCastException</code> is thrown. An
  509. * <code>ArrayIndexOutOfBoundsException</code> might be thrown if the
  510. * coordinates are not in bounds, or if <code>obj</code> is not
  511. * <code>null</code> and is not large enough to hold
  512. * the pixel data.
  513. *
  514. * @param x, y the coordinates of the pixel location
  515. * @param obj if non-<code>null</code>, a primitive array
  516. * in which to return the pixel data
  517. * @param data the <code>DataBuffer</code> containing the image data
  518. * @return the data of the specified pixel
  519. * @see #setDataElements(int, int, Object, DataBuffer)
  520. *
  521. * @throws NullPointerException if data is null.
  522. * @throws ArrayIndexOutOfBoundsException if the coordinates are
  523. * not in bounds, or if obj is too small to hold the ouput.
  524. */
  525. public Object getDataElements(int x, int y, Object obj, DataBuffer data) {
  526. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  527. throw new ArrayIndexOutOfBoundsException
  528. ("Coordinate out of bounds!");
  529. }
  530. int type = getTransferType();
  531. int numDataElems = getNumDataElements();
  532. int pixelOffset = y*scanlineStride + x*pixelStride;
  533. switch(type) {
  534. case DataBuffer.TYPE_BYTE:
  535. byte[] bdata;
  536. if (obj == null)
  537. bdata = new byte[numDataElems];
  538. else
  539. bdata = (byte[])obj;
  540. for (int i=0; i<numDataElems; i++) {
  541. bdata[i] = (byte)data.getElem(bankIndices[i],
  542. pixelOffset + bandOffsets[i]);
  543. }
  544. obj = (Object)bdata;
  545. break;
  546. case DataBuffer.TYPE_USHORT:
  547. case DataBuffer.TYPE_SHORT:
  548. short[] sdata;
  549. if (obj == null)
  550. sdata = new short[numDataElems];
  551. else
  552. sdata = (short[])obj;
  553. for (int i=0; i<numDataElems; i++) {
  554. sdata[i] = (short)data.getElem(bankIndices[i],
  555. pixelOffset + bandOffsets[i]);
  556. }
  557. obj = (Object)sdata;
  558. break;
  559. case DataBuffer.TYPE_INT:
  560. int[] idata;
  561. if (obj == null)
  562. idata = new int[numDataElems];
  563. else
  564. idata = (int[])obj;
  565. for (int i=0; i<numDataElems; i++) {
  566. idata[i] = data.getElem(bankIndices[i],
  567. pixelOffset + bandOffsets[i]);
  568. }
  569. obj = (Object)idata;
  570. break;
  571. case DataBuffer.TYPE_FLOAT:
  572. float[] fdata;
  573. if (obj == null)
  574. fdata = new float[numDataElems];
  575. else
  576. fdata = (float[])obj;
  577. for (int i=0; i<numDataElems; i++) {
  578. fdata[i] = data.getElemFloat(bankIndices[i],
  579. pixelOffset + bandOffsets[i]);
  580. }
  581. obj = (Object)fdata;
  582. break;
  583. case DataBuffer.TYPE_DOUBLE:
  584. double[] ddata;
  585. if (obj == null)
  586. ddata = new double[numDataElems];
  587. else
  588. ddata = (double[])obj;
  589. for (int i=0; i<numDataElems; i++) {
  590. ddata[i] = data.getElemDouble(bankIndices[i],
  591. pixelOffset + bandOffsets[i]);
  592. }
  593. obj = (Object)ddata;
  594. break;
  595. }
  596. return obj;
  597. }
  598. /**
  599. * Returns all samples for the specified pixel in an int array,
  600. * one sample per array element.
  601. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if
  602. * the coordinates are not in bounds.
  603. * @param x, y The coordinates of the pixel location
  604. * @param iArray If non-null, returns the samples in this array
  605. * @param data The DataBuffer containing the image data
  606. * @return the samples of the specified pixel.
  607. * @see #setPixel(int, int, int[], DataBuffer)
  608. *
  609. * @throws NullPointerException if data is null.
  610. * @throws ArrayIndexOutOfBoundsException if the coordinates are
  611. * not in bounds, or if iArray is too small to hold the output.
  612. */
  613. public int[] getPixel(int x, int y, int iArray[], DataBuffer data) {
  614. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  615. throw new ArrayIndexOutOfBoundsException
  616. ("Coordinate out of bounds!");
  617. }
  618. int pixels[];
  619. if (iArray != null) {
  620. pixels = iArray;
  621. } else {
  622. pixels = new int [numBands];
  623. }
  624. int pixelOffset = y*scanlineStride + x*pixelStride;
  625. for (int i=0; i<numBands; i++) {
  626. pixels[i] = data.getElem(bankIndices[i],
  627. pixelOffset + bandOffsets[i]);
  628. }
  629. return pixels;
  630. }
  631. /**
  632. * Returns all samples for the specified rectangle of pixels in
  633. * an int array, one sample per array element.
  634. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if
  635. * the coordinates are not in bounds.
  636. * @param x, y the coordinates 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 iArray If non-null, returns the samples in this array
  640. * @param data The DataBuffer containing the image data
  641. * @return the samples of the pixels within the specified region.
  642. * @see #setPixels(int, int, int, int, int[], DataBuffer)
  643. */
  644. public int[] getPixels(int x, int y, int w, int h,
  645. int iArray[], DataBuffer data) {
  646. if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
  647. throw new ArrayIndexOutOfBoundsException
  648. ("Coordinate out of bounds!");
  649. }
  650. int pixels[];
  651. if (iArray != null) {
  652. pixels = iArray;
  653. } else {
  654. pixels = new int [w*h*numBands];
  655. }
  656. int lineOffset = y*scanlineStride + x*pixelStride;
  657. int srcOffset = 0;
  658. for (int i = 0; i < h; i++) {
  659. int pixelOffset = lineOffset;
  660. for (int j = 0; j < w; j++) {
  661. for (int k=0; k < numBands; k++) {
  662. pixels[srcOffset++] =
  663. data.getElem(bankIndices[k], pixelOffset + bandOffsets[k]);
  664. }
  665. pixelOffset += pixelStride;
  666. }
  667. lineOffset += scanlineStride;
  668. }
  669. return pixels;
  670. }
  671. /**
  672. * Returns as int the sample in a specified band for the pixel
  673. * located at (x,y).
  674. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if
  675. * the coordinates are not in bounds.
  676. * @param x, y the coordinates of the pixel location
  677. * @param b the band to return
  678. * @param data the <code>DataBuffer</code> containing the image data
  679. * @return the sample in a specified band for the specified pixel
  680. * @see #setSample(int, int, int, int, DataBuffer)
  681. */
  682. public int getSample(int x, int y, int b, DataBuffer data) {
  683. // Bounds check for 'b' will be performed automatically
  684. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  685. throw new ArrayIndexOutOfBoundsException
  686. ("Coordinate out of bounds!");
  687. }
  688. int sample = data.getElem(bankIndices[b],
  689. y*scanlineStride + x*pixelStride +
  690. bandOffsets[b]);
  691. return sample;
  692. }
  693. /**
  694. * Returns the sample in a specified band
  695. * for the pixel located at (x,y) as a float.
  696. * An <code>ArrayIndexOutOfBoundsException</code> might be
  697. * thrown if the coordinates are not in bounds.
  698. * @param x, y The coordinates of the pixel location
  699. * @param b The band to return
  700. * @param data The DataBuffer containing the image data
  701. * @return a float value representing the sample in the specified
  702. * band for the specified pixel.
  703. */
  704. public float getSampleFloat(int x, int y, int b, DataBuffer data) {
  705. // Bounds check for 'b' will be performed automatically
  706. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  707. throw new ArrayIndexOutOfBoundsException
  708. ("Coordinate out of bounds!");
  709. }
  710. float sample = data.getElemFloat(bankIndices[b],
  711. y*scanlineStride + x*pixelStride +
  712. bandOffsets[b]);
  713. return sample;
  714. }
  715. /**
  716. * Returns the sample in a specified band
  717. * for a pixel located at (x,y) as a double.
  718. * An <code>ArrayIndexOutOfBoundsException</code> might be
  719. * thrown if the coordinates are not in bounds.
  720. * @param x, y The coordinates of the pixel location
  721. * @param b The band to return
  722. * @param data The DataBuffer containing the image data
  723. * @return a double value representing the sample in the specified
  724. * band for the specified pixel.
  725. */
  726. public double getSampleDouble(int x, int y, int b, DataBuffer data) {
  727. // Bounds check for 'b' will be performed automatically
  728. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  729. throw new ArrayIndexOutOfBoundsException
  730. ("Coordinate out of bounds!");
  731. }
  732. double sample = data.getElemDouble(bankIndices[b],
  733. y*scanlineStride + x*pixelStride +
  734. bandOffsets[b]);
  735. return sample;
  736. }
  737. /**
  738. * Returns the samples in a specified band for the specified rectangle
  739. * of pixels in an int array, one sample per data array element.
  740. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if
  741. * the coordinates are not in bounds.
  742. * @param x, y the coordinates of the upper left pixel location
  743. * @param w the width of the pixel rectangle
  744. * @param h the height of the pixel rectangle
  745. * @param b the band to return
  746. * @param iArray if non-<code>null</code>, returns the samples
  747. * in this array
  748. * @param data the <code>DataBuffer</code> containing the image data
  749. * @return the samples in the specified band of the specified pixel
  750. * @see #setSamples(int, int, int, int, int, int[], DataBuffer)
  751. */
  752. public int[] getSamples(int x, int y, int w, int h, int b,
  753. int iArray[], DataBuffer data) {
  754. // Bounds check for 'b' will be performed automatically
  755. if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
  756. throw new ArrayIndexOutOfBoundsException
  757. ("Coordinate out of bounds!");
  758. }
  759. int samples[];
  760. if (iArray != null) {
  761. samples = iArray;
  762. } else {
  763. samples = new int [w*h];
  764. }
  765. int lineOffset = y*scanlineStride + x*pixelStride + bandOffsets[b];
  766. int srcOffset = 0;
  767. for (int i = 0; i < h; i++) {
  768. int sampleOffset = lineOffset;
  769. for (int j = 0; j < w; j++) {
  770. samples[srcOffset++] = data.getElem(bankIndices[b],
  771. sampleOffset);
  772. sampleOffset += pixelStride;
  773. }
  774. lineOffset += scanlineStride;
  775. }
  776. return samples;
  777. }
  778. /**
  779. * Sets the data for a single pixel in the specified
  780. * <code>DataBuffer</code> from a primitive array of type
  781. * <code>TransferType</code>. For a <code>ComponentSampleModel</code>,
  782. * this is the same as the data type, and samples are transferred
  783. * one per array element.
  784. * <p>
  785. * The following code illustrates transferring data for one pixel from
  786. * <code>DataBuffer</code> <code>db1</code>, whose storage layout is
  787. * described by <code>ComponentSampleModel</code> <code>csm1</code>,
  788. * to <code>DataBuffer</code> <code>db2</code>, whose storage layout
  789. * is described by <code>ComponentSampleModel</code> <code>csm2</code>.
  790. * The transfer is usually more efficient than using
  791. * <code>getPixel</code> and <code>setPixel</code>.
  792. * <pre>
  793. * ComponentSampleModel csm1, csm2;
  794. * DataBufferInt db1, db2;
  795. * csm2.setDataElements(x, y, csm1.getDataElements(x, y, null, db1),
  796. * db2);
  797. * </pre>
  798. * Using <code>getDataElements</code> and <code>setDataElements</code>
  799. * to transfer between two <code>DataBuffer/SampleModel</code> pairs
  800. * is legitimate if the <code>SampleModel</code> objects have
  801. * the same number of bands, corresponding bands have the same number of
  802. * bits per sample, and the <code>TransferType</code>s are the same.
  803. * <p>
  804. * A <code>ClassCastException</code> is thrown if <code>obj</code> is not
  805. * a primitive array of type <code>TransferType</code>.
  806. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if
  807. * the coordinates are not in bounds, or if <code>obj</code> is not large
  808. * enough to hold the pixel data.
  809. * @param x, y the coordinates of the pixel location
  810. * @param obj a primitive array containing pixel data
  811. * @param data the DataBuffer containing the image data
  812. * @see #getDataElements(int, int, Object, DataBuffer)
  813. */
  814. public void setDataElements(int x, int y, Object obj, DataBuffer data) {
  815. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  816. throw new ArrayIndexOutOfBoundsException
  817. ("Coordinate out of bounds!");
  818. }
  819. int type = getTransferType();
  820. int numDataElems = getNumDataElements();
  821. int pixelOffset = y*scanlineStride + x*pixelStride;
  822. switch(type) {
  823. case DataBuffer.TYPE_BYTE:
  824. byte[] barray = (byte[])obj;
  825. for (int i=0; i<numDataElems; i++) {
  826. data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
  827. ((int)barray[i])&0xff);
  828. }
  829. break;
  830. case DataBuffer.TYPE_USHORT:
  831. case DataBuffer.TYPE_SHORT:
  832. short[] sarray = (short[])obj;
  833. for (int i=0; i<numDataElems; i++) {
  834. data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
  835. ((int)sarray[i])&0xffff);
  836. }
  837. break;
  838. case DataBuffer.TYPE_INT:
  839. int[] iarray = (int[])obj;
  840. for (int i=0; i<numDataElems; i++) {
  841. data.setElem(bankIndices[i],
  842. pixelOffset + bandOffsets[i], iarray[i]);
  843. }
  844. break;
  845. case DataBuffer.TYPE_FLOAT:
  846. float[] farray = (float[])obj;
  847. for (int i=0; i<numDataElems; i++) {
  848. data.setElemFloat(bankIndices[i],
  849. pixelOffset + bandOffsets[i], farray[i]);
  850. }
  851. break;
  852. case DataBuffer.TYPE_DOUBLE:
  853. double[] darray = (double[])obj;
  854. for (int i=0; i<numDataElems; i++) {
  855. data.setElemDouble(bankIndices[i],
  856. pixelOffset + bandOffsets[i], darray[i]);
  857. }
  858. break;
  859. }
  860. }
  861. /**
  862. * Sets a pixel in the <code>DataBuffer</code> using an int array of
  863. * samples for input. An <code>ArrayIndexOutOfBoundsException</code>
  864. * might be thrown if the coordinates are
  865. * not in bounds.
  866. * @param x, y The coordinates of the pixel location
  867. * @param iArray The input samples in an int array
  868. * @param data The DataBuffer containing the image data
  869. * @see #getPixel(int, int, int[], DataBuffer)
  870. */
  871. public void setPixel(int x, int y, int iArray[], DataBuffer data) {
  872. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  873. throw new ArrayIndexOutOfBoundsException
  874. ("Coordinate out of bounds!");
  875. }
  876. int pixelOffset = y*scanlineStride + x*pixelStride;
  877. for (int i=0; i<numBands; i++) {
  878. data.setElem(bankIndices[i],
  879. pixelOffset + bandOffsets[i],iArray[i]);
  880. }
  881. }
  882. /**
  883. * Sets all samples for a rectangle of pixels from an int array containing
  884. * one sample per array element.
  885. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if the
  886. * coordinates are not in bounds.
  887. * @param x, y The coordinates of the upper left pixel location
  888. * @param w The width of the pixel rectangle
  889. * @param h The height of the pixel rectangle
  890. * @param iArray The input samples in an int array
  891. * @param data The DataBuffer containing the image data
  892. * @see #getPixels(int, int, int, int, int[], DataBuffer)
  893. */
  894. public void setPixels(int x, int y, int w, int h,
  895. int iArray[], DataBuffer data) {
  896. if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
  897. throw new ArrayIndexOutOfBoundsException
  898. ("Coordinate out of bounds!");
  899. }
  900. int lineOffset = y*scanlineStride + x*pixelStride;
  901. int srcOffset = 0;
  902. for (int i = 0; i < h; i++) {
  903. int pixelOffset = lineOffset;
  904. for (int j = 0; j < w; j++) {
  905. for (int k=0; k < numBands; k++) {
  906. data.setElem(bankIndices[k], pixelOffset + bandOffsets[k],
  907. iArray[srcOffset++]);
  908. }
  909. pixelOffset += pixelStride;
  910. }
  911. lineOffset += scanlineStride;
  912. }
  913. }
  914. /**
  915. * Sets a sample in the specified band for the pixel located at (x,y)
  916. * in the <code>DataBuffer</code> using an int for input.
  917. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if the
  918. * coordinates are not in bounds.
  919. * @param x, y the coordinates of the pixel location
  920. * @param b the band to set
  921. * @param s the input sample as an int
  922. * @param data the DataBuffer containing the image data
  923. * @see #getSample(int, int, int, DataBuffer)
  924. */
  925. public void setSample(int x, int y, int b, int s,
  926. DataBuffer data) {
  927. // Bounds check for 'b' will be performed automatically
  928. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  929. throw new ArrayIndexOutOfBoundsException
  930. ("Coordinate out of bounds!");
  931. }
  932. data.setElem(bankIndices[b],
  933. y*scanlineStride + x*pixelStride + bandOffsets[b], s);
  934. }
  935. /**
  936. * Sets a sample in the specified band for the pixel located at (x,y)
  937. * in the <code>DataBuffer</code> using a float for input.
  938. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if
  939. * the coordinates are not in bounds.
  940. * @param x, y The coordinates of the pixel location
  941. * @param b The band to set
  942. * @param s The input sample as a float
  943. * @param data The DataBuffer containing the image data
  944. * @see #getSample(int, int, int, DataBuffer)
  945. */
  946. public void setSample(int x, int y, int b,
  947. float s ,
  948. DataBuffer data) {
  949. // Bounds check for 'b' will be performed automatically
  950. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  951. throw new ArrayIndexOutOfBoundsException
  952. ("Coordinate out of bounds!");
  953. }
  954. data.setElemFloat(bankIndices[b],
  955. y*scanlineStride + x*pixelStride + bandOffsets[b],
  956. s);
  957. }
  958. /**
  959. * Sets a sample in the specified band for the pixel located at (x,y)
  960. * in the <code>DataBuffer</code> using a double for input.
  961. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if
  962. * the coordinates are not in bounds.
  963. * @param x, y The coordinates of the pixel location
  964. * @param b The band to set
  965. * @param s The input sample as a double
  966. * @param data The DataBuffer containing the image data
  967. * @see #getSample(int, int, int, DataBuffer)
  968. */
  969. public void setSample(int x, int y, int b,
  970. double s,
  971. DataBuffer data) {
  972. // Bounds check for 'b' will be performed automatically
  973. if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
  974. throw new ArrayIndexOutOfBoundsException
  975. ("Coordinate out of bounds!");
  976. }
  977. data.setElemDouble(bankIndices[b],
  978. y*scanlineStride + x*pixelStride + bandOffsets[b],
  979. s);
  980. }
  981. /**
  982. * Sets the samples in the specified band for the specified rectangle
  983. * of pixels from an int array containing one sample per data array element.
  984. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if the
  985. * coordinates are not in bounds.
  986. * @param x, y The coordinates of the upper left pixel location
  987. * @param w The width of the pixel rectangle
  988. * @param h The height of the pixel rectangle
  989. * @param b The band to set
  990. * @param iArray The input samples in an int array
  991. * @param data The DataBuffer containing the image data
  992. * @see #getSamples(int, int, int, int, int, int[], DataBuffer)
  993. */
  994. public void setSamples(int x, int y, int w, int h, int b,
  995. int iArray[], DataBuffer data) {
  996. // Bounds check for 'b' will be performed automatically
  997. if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
  998. throw new ArrayIndexOutOfBoundsException
  999. ("Coordinate out of bounds!");
  1000. }
  1001. int lineOffset = y*scanlineStride + x*pixelStride + bandOffsets[b];
  1002. int srcOffset = 0;
  1003. for (int i = 0; i < h; i++) {
  1004. int sampleOffset = lineOffset;
  1005. for (int j = 0; j < w; j++) {
  1006. data.setElem(bankIndices[b], sampleOffset, iArray[srcOffset++]);
  1007. sampleOffset += pixelStride;
  1008. }
  1009. lineOffset += scanlineStride;
  1010. }
  1011. }
  1012. public boolean equals(Object o) {
  1013. if ((o == null) || !(o instanceof ComponentSampleModel)) {
  1014. return false;
  1015. }
  1016. ComponentSampleModel that = (ComponentSampleModel)o;
  1017. return this.width == that.width &&
  1018. this.height == that.height &&
  1019. this.numBands == that.numBands &&
  1020. this.dataType == that.dataType &&
  1021. Arrays.equals(this.bandOffsets, that.bandOffsets) &&
  1022. Arrays.equals(this.bankIndices, that.bankIndices) &&
  1023. this.numBands == that.numBands &&
  1024. this.numBanks == that.numBanks &&
  1025. this.scanlineStride == that.scanlineStride &&
  1026. this.pixelStride == that.pixelStride;
  1027. }
  1028. // If we implement equals() we must also implement hashCode
  1029. public int hashCode() {
  1030. int hash = 0;
  1031. hash = width;
  1032. hash <<= 8;
  1033. hash ^= height;
  1034. hash <<= 8;
  1035. hash ^= numBands;
  1036. hash <<= 8;
  1037. hash ^= dataType;
  1038. hash <<= 8;
  1039. for (int i = 0; i < bandOffsets.length; i++) {
  1040. hash ^= bandOffsets[i];
  1041. hash <<= 8;
  1042. }
  1043. for (int i = 0; i < bankIndices.length; i++) {
  1044. hash ^= bankIndices[i];
  1045. hash <<= 8;
  1046. }
  1047. hash ^= numBands;
  1048. hash <<= 8;
  1049. hash ^= numBanks;
  1050. hash <<= 8;
  1051. hash ^= scanlineStride;
  1052. hash <<= 8;
  1053. hash ^= pixelStride;
  1054. return hash;
  1055. }
  1056. }