1. /*
  2. * @(#)Raster.java 1.38 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. import java.awt.Rectangle;
  18. import java.awt.Point;
  19. import sun.awt.image.ByteInterleavedRaster;
  20. import sun.awt.image.ShortInterleavedRaster;
  21. import sun.awt.image.IntegerInterleavedRaster;
  22. import sun.awt.image.ByteBandedRaster;
  23. import sun.awt.image.ShortBandedRaster;
  24. import sun.awt.image.BytePackedRaster;
  25. /**
  26. * A class representing a rectangular array of pixels. A Raster
  27. * encapsulates a DataBuffer that stores the sample values and a
  28. * SampleModel that describes how to locate a given sample value in a
  29. * DataBuffer.
  30. * <p>
  31. * A Raster defines values for pixels occupying a particular
  32. * rectangular area of the plane, not necessarily including (0, 0).
  33. * The rectangle, known as the Raster's bounding rectangle and
  34. * available by means of the getBounds method, is defined by minX,
  35. * minY, width, and height values. The minX and minY values define
  36. * the coordinate of the upper left corner of the Raster. References
  37. * to pixels outside of the bounding rectangle may result in an
  38. * exception being thrown, or may result in references to unintended
  39. * elements of the Raster's associated DataBuffer. It is the user's
  40. * responsibility to avoid accessing such pixels.
  41. * <p>
  42. * A SampleModel describes how samples of a Raster
  43. * are stored in the primitive array elements of a DataBuffer.
  44. * Samples may be stored one per data element, as in a
  45. * PixelInterleavedSampleModel or BandedSampleModel, or packed several to
  46. * an element, as in a SinglePixelPackedSampleModel or
  47. * MultiPixelPackedSampleModel. The SampleModel is also
  48. * controls whether samples are sign extended, allowing unsigned
  49. * data to be stored in signed Java data types such as byte, short, and
  50. * int.
  51. * <p>
  52. * Although a Raster may live anywhere in the plane, a SampleModel
  53. * makes use of a simple coordinate system that starts at (0, 0). A
  54. * Raster therefore contains a translation factor that allows pixel
  55. * locations to be mapped between the Raster's coordinate system and
  56. * that of the SampleModel. The translation from the SampleModel
  57. * coordinate system to that of the Raster may be obtained by the
  58. * getSampleModelTranslateX and getSampleModelTranslateY methods.
  59. * <p>
  60. * A Raster may share a DataBuffer with another Raster either by
  61. * explicit construction or by the use of the createChild and
  62. * createTranslatedChild methods. Rasters created by these methods
  63. * can return a reference to the Raster they were created from by
  64. * means of the getParent method. For a Raster that was not
  65. * constructed by means of a call to createTranslatedChild or
  66. * createChild, getParent will return null.
  67. * <p>
  68. * The createTranslatedChild method returns a new Raster that
  69. * shares all of the data of the current Raster, but occupies a
  70. * bounding rectangle of the same width and height but with a
  71. * different starting point. For example, if the parent Raster
  72. * occupied the region (10, 10) to (100, 100), and the translated
  73. * Raster was defined to start at (50, 50), then pixel (20, 20) of the
  74. * parent and pixel (60, 60) of the child occupy the same location in
  75. * the DataBuffer shared by the two Rasters. In the first case, (-10,
  76. * -10) should be added to a pixel coordinate to obtain the
  77. * corresponding SampleModel coordinate, and in the second case (-50,
  78. * -50) should be added.
  79. * <p>
  80. * The translation between a parent and child Raster may be
  81. * determined by subtracting the child's sampleModelTranslateX and
  82. * sampleModelTranslateY values from those of the parent.
  83. * <p>
  84. * The createChild method may be used to create a new Raster
  85. * occupying only a subset of its parent's bounding rectangle
  86. * (with the same or a translated coordinate system) or
  87. * with a subset of the bands of its parent.
  88. * <p>
  89. * All constructors are protected. The correct way to create a
  90. * Raster is to use one of the static create methods defined in this
  91. * class. These methods create instances of Raster that use the
  92. * standard Interleaved, Banded, and Packed SampleModels and that may
  93. * be processed more efficiently than a Raster created by combining
  94. * an externally generated SampleModel and DataBuffer.
  95. * @see java.awt.image.DataBuffer
  96. * @see java.awt.image.SampleModel
  97. * @see java.awt.image.PixelInterleavedSampleModel
  98. * @see java.awt.image.BandedSampleModel
  99. * @see java.awt.image.SinglePixelPackedSampleModel
  100. * @see java.awt.image.MultiPixelPackedSampleModel
  101. */
  102. // Code changes since beta 3:
  103. //
  104. // change baseRasterOffsetX to sampleModelTranslateX
  105. //
  106. // change baseRasterOffsetY to sampleModelTranslateY
  107. //
  108. // remove getBaseSubRasterOffsetX, replace with:
  109. // (getMinX() - getSampleModelTranslateX()
  110. //
  111. // remove getBaseSubRasterOffsetY, replace with:
  112. // (getMinY() - getSampleModelTranslateY()
  113. //
  114. // remove createSubRaster(Rectangle)
  115. //
  116. // remove createSubRaster(int x, int y, int width, int height),
  117. // replace with createChild(x, y, width, height, x, y, null)
  118. //
  119. // change createTranslatedRaster() to createTranslatedChild
  120. //
  121. // change createSubRaster() to createChild
  122. //
  123. // change getPixelData to getDataElements everywhere
  124. // -- need to propagate to SampleModels
  125. //
  126. // change getPixel() to getPixels() when getting a Rect
  127. // -- need to propagate to SampleModels
  128. //
  129. // change getSample() to getSamples() when getting a Rect
  130. // -- need to propagate to SampleModels
  131. //
  132. // change getBaseRasterOriginX() to getSampleModelTranslateX()
  133. //
  134. // change getBaseRasterOriginY() to getSampleModelTranslateY()
  135. public class Raster {
  136. /**
  137. * The SampleModel that describes how pixels from this Raster
  138. * are stored in the DataBuffer.
  139. */
  140. protected SampleModel sampleModel;
  141. /** The DataBuffer that stores the image data. */
  142. protected DataBuffer dataBuffer;
  143. /** The X coordinate of the upper-left pixel of this Raster. */
  144. protected int minX;
  145. /** The Y coordinate of the upper-left pixel of this Raster. */
  146. protected int minY;
  147. /** The width of this Raster. */
  148. protected int width;
  149. /** The height of this Raster. */
  150. protected int height;
  151. /**
  152. * The X translation from the coordinate space of the
  153. * Raster's SampleModel to that of the Raster.
  154. */
  155. protected int sampleModelTranslateX;
  156. /**
  157. * The Y translation from the coordinate space of the
  158. * Raster's SampleModel to that of the Raster.
  159. */
  160. protected int sampleModelTranslateY;
  161. /** The number of bands in the Raster. */
  162. protected int numBands;
  163. /** The number of DataBuffer data elements per pixel. */
  164. protected int numDataElements;
  165. /** The parent of this Raster, or null. */
  166. protected Raster parent;
  167. static private native void initIDs();
  168. static {
  169. ColorModel.loadLibraries();
  170. initIDs();
  171. }
  172. /**
  173. * Creates a Raster based on a PixelInterleavedSampleModel with the
  174. * specified data type, width, height, and number of bands.
  175. *
  176. * <p> The upper left corner of the Raster is given by the
  177. * location argument. If location is null, (0, 0) will be used.
  178. * The dataType parameter should be one of the enumerated values
  179. * defined in the DataBuffer class.
  180. *
  181. * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT,
  182. * and TYPE_INT.
  183. */
  184. public static WritableRaster createInterleavedRaster(int dataType,
  185. int w, int h,
  186. int bands,
  187. Point location) {
  188. int[] bandOffsets = new int[bands];
  189. for (int i = 0; i < bands; i++) {
  190. bandOffsets[i] = i;
  191. }
  192. return createInterleavedRaster(dataType, w, h, w*bands, bands,
  193. bandOffsets, location);
  194. }
  195. /**
  196. * Creates a Raster based on a PixelInterleavedSampleModel with the
  197. * specified data type, width, height, scanline stride, pixel
  198. * stride, and band offsets. The number of bands is inferred from
  199. * bandOffsets.length.
  200. *
  201. * <p> The upper left corner of the Raster is given by the
  202. * location argument. If location is null, (0, 0) will be used.
  203. * The dataType parameter should be one of the enumerated values
  204. * defined in the DataBuffer class.
  205. *
  206. * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT,
  207. * and TYPE_INT.
  208. */
  209. public static WritableRaster createInterleavedRaster(int dataType,
  210. int w, int h,
  211. int scanlineStride,
  212. int pixelStride,
  213. int bandOffsets[],
  214. Point location) {
  215. DataBuffer d;
  216. int bands = bandOffsets.length;
  217. int maxBandOff = bandOffsets[0];
  218. for (int i=1; i < bands; i++) {
  219. if (bandOffsets[i] > maxBandOff) {
  220. maxBandOff = bandOffsets[i];
  221. }
  222. }
  223. int size = maxBandOff + scanlineStride*(h-1) + pixelStride*(w-1) + 1;
  224. switch(dataType) {
  225. case DataBuffer.TYPE_BYTE:
  226. d = new DataBufferByte(size);
  227. break;
  228. case DataBuffer.TYPE_USHORT:
  229. d = new DataBufferUShort(size);
  230. break;
  231. case DataBuffer.TYPE_INT:
  232. d = new DataBufferInt(size);
  233. break;
  234. default:
  235. throw new IllegalArgumentException("Unsupported data type " +
  236. dataType);
  237. }
  238. return createInterleavedRaster(d, w, h, scanlineStride,
  239. pixelStride, bandOffsets, location);
  240. }
  241. /**
  242. * Creates a Raster based on a BandedSampleModel with the
  243. * specified data type, width, height, and number of bands.
  244. *
  245. * <p> The upper left corner of the Raster is given by the
  246. * location argument. If location is null, (0, 0) will be used.
  247. * The dataType parameter should be one of the enumerated values
  248. * defined in the DataBuffer class.
  249. *
  250. * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT,
  251. * and TYPE_INT.
  252. */
  253. public static WritableRaster createBandedRaster(int dataType,
  254. int w, int h,
  255. int bands,
  256. Point location) {
  257. if (bands < 1) {
  258. throw new ArrayIndexOutOfBoundsException("Number of bands ("+
  259. bands+") must"+
  260. " be greater than 0");
  261. }
  262. int[] bankIndices = new int[bands];
  263. int[] bandOffsets = new int[bands];
  264. for (int i = 0; i < bands; i++) {
  265. bankIndices[i] = i;
  266. bandOffsets[i] = 0;
  267. }
  268. return createBandedRaster(dataType, w, h, w,
  269. bankIndices, bandOffsets,
  270. location);
  271. }
  272. /**
  273. * Creates a Raster based on a BandedSampleModel with the
  274. * specified data type, width, height, scanline stride, bank
  275. * indices and band offsets. The number of bands is inferred from
  276. * bankIndices.length and bandOffsets.length, which must be the
  277. * same.
  278. *
  279. * <p> The upper left corner of the Raster is given by the
  280. * location argument. The dataType parameter should be one of the
  281. * enumerated values defined in the DataBuffer class.
  282. *
  283. * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT,
  284. * and TYPE_INT.
  285. */
  286. public static WritableRaster createBandedRaster(int dataType,
  287. int w, int h,
  288. int scanlineStride,
  289. int bankIndices[],
  290. int bandOffsets[],
  291. Point location) {
  292. DataBuffer d;
  293. int bands = bandOffsets.length;
  294. if (bankIndices == null) {
  295. throw new
  296. ArrayIndexOutOfBoundsException("Bank indices array is null");
  297. }
  298. if (bandOffsets == null) {
  299. throw new
  300. ArrayIndexOutOfBoundsException("Band offsets array is null");
  301. }
  302. // Figure out the #banks and the largest band offset
  303. int maxBank = bankIndices[0];
  304. int maxBandOff = bandOffsets[0];
  305. for (int i = 1; i < bands; i++) {
  306. if (bankIndices[i] > maxBank) {
  307. maxBank = bankIndices[i];
  308. }
  309. if (bandOffsets[i] > maxBandOff) {
  310. maxBandOff = bandOffsets[i];
  311. }
  312. }
  313. int banks = maxBank + 1;
  314. int size = maxBandOff + scanlineStride*(h-1) + (w-1) + 1;
  315. switch(dataType) {
  316. case DataBuffer.TYPE_BYTE:
  317. d = new DataBufferByte(size, banks);
  318. break;
  319. case DataBuffer.TYPE_USHORT:
  320. d = new DataBufferUShort(size, banks);
  321. break;
  322. case DataBuffer.TYPE_INT:
  323. d = new DataBufferInt(size, banks);
  324. break;
  325. default:
  326. throw new IllegalArgumentException("Unsupported data type " +
  327. dataType);
  328. }
  329. return createBandedRaster(d, w, h, scanlineStride,
  330. bankIndices, bandOffsets, location);
  331. }
  332. /**
  333. * Creates a Raster based on a SinglePixelPackedSampleModel with
  334. * the specified data type, width, height, and band masks.
  335. * The number of bands is inferred from bandMasks.length.
  336. *
  337. * <p> The upper left corner of the Raster is given by the
  338. * location argument. If location is null, (0, 0) will be used.
  339. * The dataType parameter should be one of the enumerated values
  340. * defined in the DataBuffer class.
  341. *
  342. * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT,
  343. * and TYPE_INT.
  344. */
  345. public static WritableRaster createPackedRaster(int dataType,
  346. int w, int h,
  347. int bandMasks[],
  348. Point location) {
  349. DataBuffer d;
  350. switch(dataType) {
  351. case DataBuffer.TYPE_BYTE:
  352. d = new DataBufferByte(w*h);
  353. break;
  354. case DataBuffer.TYPE_USHORT:
  355. d = new DataBufferUShort(w*h);
  356. break;
  357. case DataBuffer.TYPE_INT:
  358. d = new DataBufferInt(w*h);
  359. break;
  360. default:
  361. throw new IllegalArgumentException("Unsupported data type " +
  362. dataType);
  363. }
  364. return createPackedRaster(d, w, h, w, bandMasks, location);
  365. }
  366. /**
  367. * Creates a Raster based on a packed SampleModel with the
  368. * specified data type, width, height, number of bands, and bits
  369. * per band. If the number of bands is one, the SampleModel will
  370. * be a MultiPixelPackedSampleModel.
  371. *
  372. * <p> If the number of bands is more than one, the SampleModel
  373. * will be a SinglePixelPackedSampleModel, with each band having
  374. * bitsPerBand bits. In either case, the requirements on dataType
  375. * and bitsPerBand imposed by the corresponding SampleModel must
  376. * be met.
  377. *
  378. * <p> The upper left corner of the Raster is given by the
  379. * location argument. If location is null, (0, 0) will be used.
  380. * The dataType parameter should be one of the enumerated values
  381. * defined in the DataBuffer class.
  382. *
  383. * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT,
  384. * and TYPE_INT.
  385. */
  386. public static WritableRaster createPackedRaster(int dataType,
  387. int w, int h,
  388. int bands,
  389. int bitsPerBand,
  390. Point location) {
  391. DataBuffer d;
  392. if (bands != 1) {
  393. int[] masks = new int[bands];
  394. int mask = (1 << bitsPerBand) - 1;
  395. int shift = (bands-1)*bitsPerBand;
  396. /* Make sure the total mask size will fit in the data type */
  397. if (shift+bitsPerBand > DataBuffer.getDataTypeSize(dataType)) {
  398. throw new IllegalArgumentException("bitsPerBand("+
  399. bitsPerBand+") * bands is "+
  400. " greater than data type "+
  401. "size.");
  402. }
  403. switch(dataType) {
  404. case DataBuffer.TYPE_BYTE:
  405. case DataBuffer.TYPE_USHORT:
  406. case DataBuffer.TYPE_INT:
  407. break;
  408. default:
  409. throw new IllegalArgumentException("Unsupported data type " +
  410. dataType);
  411. }
  412. for (int i = 0; i < bands; i++) {
  413. masks[i] = mask << shift;
  414. shift = shift - bitsPerBand;
  415. }
  416. return createPackedRaster(dataType, w, h, masks, location);
  417. }
  418. else {
  419. double fw = w;
  420. switch(dataType) {
  421. case DataBuffer.TYPE_BYTE:
  422. d = new DataBufferByte((int)(Math.ceil(fw(8/bitsPerBand)))*h);
  423. break;
  424. case DataBuffer.TYPE_USHORT:
  425. d = new DataBufferUShort((int)(Math.ceil(fw(16/bitsPerBand)))*h);
  426. break;
  427. case DataBuffer.TYPE_INT:
  428. d = new DataBufferInt((int)(Math.ceil(fw(32/bitsPerBand)))*h);
  429. break;
  430. default:
  431. throw new IllegalArgumentException("Unsupported data type " +
  432. dataType);
  433. }
  434. return createPackedRaster(d, w, h, bitsPerBand, location);
  435. }
  436. }
  437. /**
  438. * Creates a Raster based on a PixelInterleavedSampleModel with the
  439. * specified DataBuffer, width, height, scanline stride, pixel
  440. * stride, and band offsets. The number of bands is inferred from
  441. * bandOffsets.length. The upper left corner of the Raster
  442. * is given by the location argument. If location is null, (0, 0)
  443. * will be used.
  444. */
  445. public static WritableRaster createInterleavedRaster(DataBuffer dataBuffer,
  446. int w, int h,
  447. int scanlineStride,
  448. int pixelStride,
  449. int bandOffsets[],
  450. Point location) {
  451. if (location == null) {
  452. location = new Point(0, 0);
  453. }
  454. int dataType = dataBuffer.getDataType();
  455. PixelInterleavedSampleModel csm =
  456. new PixelInterleavedSampleModel(dataType, w, h,
  457. pixelStride,
  458. scanlineStride,
  459. bandOffsets);
  460. switch(dataType) {
  461. case DataBuffer.TYPE_BYTE:
  462. return new ByteInterleavedRaster(csm, dataBuffer, location);
  463. case DataBuffer.TYPE_USHORT:
  464. return new ShortInterleavedRaster(csm, dataBuffer, location);
  465. case DataBuffer.TYPE_INT:
  466. return new IntegerInterleavedRaster(csm, dataBuffer, location);
  467. default:
  468. throw new IllegalArgumentException("Unsupported data type " +
  469. dataType);
  470. }
  471. }
  472. /**
  473. * Creates a Raster based on a BandedSampleModel with the
  474. * specified DataBuffer, width, height, scanline stride, bank
  475. * indices, and band offsets. The number of bands is inferred
  476. * from bankIndices.length and bandOffsets.length, which must be
  477. * the same. The upper left corner of the Raster is given by the
  478. * location argument. If location is null, (0, 0) will be used.
  479. */
  480. public static WritableRaster createBandedRaster(DataBuffer dataBuffer,
  481. int w, int h,
  482. int scanlineStride,
  483. int bankIndices[],
  484. int bandOffsets[],
  485. Point location) {
  486. if (location == null) {
  487. location = new Point(0,0);
  488. }
  489. int dataType = dataBuffer.getDataType();
  490. int bands = bankIndices.length;
  491. if (bandOffsets.length != bands) {
  492. throw new IllegalArgumentException(
  493. "bankIndices.length != bandOffsets.length");
  494. }
  495. BandedSampleModel bsm =
  496. new BandedSampleModel(dataType, w, h,
  497. scanlineStride,
  498. bankIndices, bandOffsets);
  499. switch(dataType) {
  500. case DataBuffer.TYPE_BYTE:
  501. return new ByteBandedRaster(bsm, dataBuffer, location);
  502. case DataBuffer.TYPE_USHORT:
  503. return new ShortBandedRaster(bsm, dataBuffer, location);
  504. case DataBuffer.TYPE_INT:
  505. return new WritableRaster(bsm, dataBuffer, location);
  506. default:
  507. throw new IllegalArgumentException("Unsupported data type " +
  508. dataType);
  509. }
  510. }
  511. /**
  512. * Creates a Raster based on a SinglePixelPackedSampleModel with
  513. * the specified DataBuffer, width, height, scanline stride, and
  514. * band masks. The number of bands is inferred from bandMasks.length.
  515. * The upper left corner of the Raster is given by
  516. * the location argument. If location is null, (0, 0) will be used.
  517. */
  518. public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
  519. int w, int h,
  520. int scanlineStride,
  521. int bandMasks[],
  522. Point location) {
  523. if (location == null) {
  524. location = new Point(0,0);
  525. }
  526. int dataType = dataBuffer.getDataType();
  527. SinglePixelPackedSampleModel sppsm =
  528. new SinglePixelPackedSampleModel(dataType, w, h, scanlineStride,
  529. bandMasks);
  530. switch(dataType) {
  531. case DataBuffer.TYPE_BYTE:
  532. return new ByteInterleavedRaster(sppsm, dataBuffer, location);
  533. case DataBuffer.TYPE_USHORT:
  534. return new ShortInterleavedRaster(sppsm, dataBuffer, location);
  535. case DataBuffer.TYPE_INT:
  536. return new IntegerInterleavedRaster(sppsm, dataBuffer, location);
  537. default:
  538. throw new IllegalArgumentException("Unsupported data type " +
  539. dataType);
  540. }
  541. }
  542. /**
  543. * Creates a Raster based on a MultiPixelPackedSampleModel with the
  544. * specified DataBuffer, width, height, and bits per pixel. The upper
  545. * left corner of the Raster is given by the location argument. If
  546. * location is null, (0, 0) will be used.
  547. */
  548. public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
  549. int w, int h,
  550. int bitsPerPixel,
  551. Point location) {
  552. if (location == null) {
  553. location = new Point(0,0);
  554. }
  555. int dataType = dataBuffer.getDataType();
  556. MultiPixelPackedSampleModel sbpsm =
  557. new MultiPixelPackedSampleModel(dataType, w, h, bitsPerPixel);
  558. switch(dataType) {
  559. case DataBuffer.TYPE_BYTE:
  560. return new BytePackedRaster(sbpsm, dataBuffer, location);
  561. case DataBuffer.TYPE_USHORT:
  562. return new WritableRaster(sbpsm, dataBuffer, location);
  563. case DataBuffer.TYPE_INT:
  564. return new WritableRaster(sbpsm, dataBuffer, location);
  565. default:
  566. throw new IllegalArgumentException("Unsupported data type " +
  567. dataType);
  568. }
  569. }
  570. /**
  571. * Creates a Raster with the specified SampleModel and DataBuffer.
  572. * The upper left corner of the Raster is given by the location argument.
  573. * If location is null, (0, 0) will be used.
  574. */
  575. public static Raster createRaster(SampleModel sm,
  576. DataBuffer db,
  577. Point location) {
  578. if (location == null) {
  579. location = new Point(0,0);
  580. }
  581. int dataType = sm.getDataType();
  582. if (sm instanceof PixelInterleavedSampleModel) {
  583. switch(dataType) {
  584. case DataBuffer.TYPE_BYTE:
  585. return new ByteInterleavedRaster(sm, db, location);
  586. case DataBuffer.TYPE_USHORT:
  587. return new ShortInterleavedRaster(sm, db, location);
  588. }
  589. } else if (sm instanceof SinglePixelPackedSampleModel) {
  590. switch(dataType) {
  591. case DataBuffer.TYPE_BYTE:
  592. return new ByteInterleavedRaster(sm, db, location);
  593. case DataBuffer.TYPE_USHORT:
  594. return new ShortInterleavedRaster(sm, db, location);
  595. case DataBuffer.TYPE_INT:
  596. return new IntegerInterleavedRaster(sm, db, location);
  597. }
  598. } else if (sm instanceof MultiPixelPackedSampleModel &&
  599. dataType == DataBuffer.TYPE_BYTE) {
  600. return new BytePackedRaster(sm, db, location);
  601. }
  602. // we couldn't do anything special - do the generic thing
  603. return new Raster(sm,db,location);
  604. }
  605. /**
  606. * Creates a WritableRaster with the specified SampleModel.
  607. * The upper left corner of the Raster is given by the location argument.
  608. * If location is null, (0, 0) will be used.
  609. */
  610. public static WritableRaster createWritableRaster(SampleModel sm,
  611. Point location) {
  612. if (location == null) {
  613. location = new Point(0,0);
  614. }
  615. return createWritableRaster(sm,
  616. sm.createDataBuffer(),
  617. location);
  618. }
  619. /**
  620. * Creates a WritableRaster with the specified SampleModel and DataBuffer.
  621. * The upper left corner of the Raster is given by the location argument.
  622. * If location is null, (0, 0) will be used.
  623. */
  624. public static WritableRaster createWritableRaster(SampleModel sm,
  625. DataBuffer db,
  626. Point location) {
  627. if (location == null) {
  628. location = new Point(0,0);
  629. }
  630. int dataType = sm.getDataType();
  631. if (sm instanceof PixelInterleavedSampleModel) {
  632. switch(dataType) {
  633. case DataBuffer.TYPE_BYTE:
  634. return new ByteInterleavedRaster(sm, db, location);
  635. case DataBuffer.TYPE_USHORT:
  636. return new ShortInterleavedRaster(sm, db, location);
  637. }
  638. } else if (sm instanceof SinglePixelPackedSampleModel) {
  639. switch(dataType) {
  640. case DataBuffer.TYPE_BYTE:
  641. return new ByteInterleavedRaster(sm, db, location);
  642. case DataBuffer.TYPE_USHORT:
  643. return new ShortInterleavedRaster(sm, db, location);
  644. case DataBuffer.TYPE_INT:
  645. return new IntegerInterleavedRaster(sm, db, location);
  646. }
  647. } else if (sm instanceof MultiPixelPackedSampleModel &&
  648. dataType == DataBuffer.TYPE_BYTE) {
  649. return new BytePackedRaster(sm, db, location);
  650. }
  651. // we couldn't do anything special - do the generic thing
  652. return new WritableRaster(sm,db,location);
  653. }
  654. /**
  655. * Constructs a Raster with the given SampleModel. The Raster's
  656. * upper left corner is origin and it is the same size as the
  657. * SampleModel. A DataBuffer large enough to describe the
  658. * Raster is automatically created.
  659. * @param sampleModel The SampleModel that specifies the layout.
  660. * @param origin The Point that specified the origin.
  661. */
  662. protected Raster(SampleModel sampleModel,
  663. Point origin) {
  664. this(sampleModel,
  665. sampleModel.createDataBuffer(),
  666. new Rectangle(origin.x,
  667. origin.y,
  668. sampleModel.getWidth(),
  669. sampleModel.getHeight()),
  670. origin,
  671. null);
  672. }
  673. /**
  674. * Constructs a Raster with the given SampleModel and DataBuffer.
  675. * The Raster's upper left corner is origin and it is the same size
  676. * as the SampleModel. The DataBuffer is not initialized and must
  677. * be compatible with SampleModel.
  678. * @param sampleModel The SampleModel that specifies the layout.
  679. * @param dataBuffer The DataBuffer that contains the image data.
  680. * @param origin The Point that specifies the origin.
  681. */
  682. protected Raster(SampleModel sampleModel,
  683. DataBuffer dataBuffer,
  684. Point origin) {
  685. this(sampleModel,
  686. dataBuffer,
  687. new Rectangle(origin.x,
  688. origin.y,
  689. sampleModel.getWidth(),
  690. sampleModel.getHeight()),
  691. origin,
  692. null);
  693. }
  694. /**
  695. * Constructs a Raster with the given SampleModel, DataBuffer, and
  696. * parent. aRegion specifies the bounding rectangle of the new
  697. * Raster. When translated into the base Raster's coordinate
  698. * system, aRegion must be contained by the base Raster.
  699. * (The base Raster is the Raster's ancestor which has no parent.)
  700. * sampleModelTranslate specifies the sampleModelTranslateX and
  701. * sampleModelTranslateY values of the new Raster.
  702. *
  703. * Note that this constructor should generally be called by other
  704. * constructors or create methods, it should not be used directly.
  705. * @param sampleModel The SampleModel that specifies the layout.
  706. * @param dataBuffer The DataBuffer that contains the image data.
  707. * @param aRegion The Rectangle that specifies the image area.
  708. * @param sampleModelTranslate The Point that specifies the translation
  709. * from SampleModel to Raster coordinates.
  710. * @param parent The parent (if any) of this raster.
  711. */
  712. protected Raster(SampleModel sampleModel,
  713. DataBuffer dataBuffer,
  714. Rectangle aRegion,
  715. Point sampleModelTranslate,
  716. Raster parent) {
  717. this.sampleModel = sampleModel;
  718. this.dataBuffer = dataBuffer;
  719. minX = aRegion.x;
  720. minY = aRegion.y;
  721. width = aRegion.width;
  722. height = aRegion.height;
  723. sampleModelTranslateX = sampleModelTranslate.x;
  724. sampleModelTranslateY = sampleModelTranslate.y;
  725. numBands = sampleModel.getNumBands();
  726. numDataElements = sampleModel.getNumDataElements();
  727. this.parent = parent;
  728. }
  729. /** Returns the parent Raster (if any) of this Raster, or else null. */
  730. public Raster getParent() {
  731. return parent;
  732. }
  733. /**
  734. * Returns the X translation from the coordinate system of the
  735. * SampleModel to that of the Raster. To convert a pixel's X
  736. * coordinate from the Raster coordinate system to the SampleModel
  737. * coordinate system, this value must be subtracted.
  738. */
  739. final public int getSampleModelTranslateX() {
  740. return sampleModelTranslateX;
  741. }
  742. /**
  743. * Returns the Y translation from the coordinate system of the
  744. * SampleModel to that of the Raster. To convert a pixel's Y
  745. * coordinate from the Raster coordinate system to the SampleModel
  746. * coordinate system, this value must be subtracted.
  747. */
  748. final public int getSampleModelTranslateY() {
  749. return sampleModelTranslateY;
  750. }
  751. /**
  752. * Create a compatible WritableRaster the same size as this Raster with
  753. * the same SampleModel and a new initialized DataBuffer.
  754. */
  755. public WritableRaster createCompatibleWritableRaster() {
  756. return new WritableRaster(sampleModel, new Point(0,0));
  757. }
  758. /**
  759. * Create a compatible WritableRaster with the specified size, a new
  760. * SampleModel, and a new initialized DataBuffer.
  761. * @exception RasterFormatException if the width or height is less than
  762. * or equal to zero.
  763. */
  764. public WritableRaster createCompatibleWritableRaster(int w, int h) {
  765. if (w <= 0 || h <=0) {
  766. throw new RasterFormatException("negative " +
  767. ((w <= 0) ? "width" : "height"));
  768. }
  769. SampleModel sm = sampleModel.createCompatibleSampleModel(w,h);
  770. return new WritableRaster(sm, new Point(0,0));
  771. }
  772. /**
  773. * Create a compatible WritableRaster with location (minX, minY)
  774. * and size (width, height) specified by rect, a
  775. * new SampleModel, and a new initialized DataBuffer.
  776. */
  777. public WritableRaster createCompatibleWritableRaster(Rectangle rect) {
  778. return createCompatibleWritableRaster(rect.x, rect.y,
  779. rect.width, rect.height);
  780. }
  781. /**
  782. * Create a compatible WritableRaster with the specified
  783. * location (minX, minY) and size (width, height), a
  784. * new SampleModel, and a new initialized DataBuffer.
  785. */
  786. public WritableRaster createCompatibleWritableRaster(int x, int y,
  787. int w, int h) {
  788. WritableRaster ret = createCompatibleWritableRaster(w, h);
  789. return ret.createWritableChild(0,0,w,h,x,y,null);
  790. }
  791. /**
  792. * Create a Raster with the same size, SampleModel and DataBuffer
  793. * as this one, but with a different location. The new Raster
  794. * will possess a reference to the current Raster, accessible
  795. * through its getParent() method.
  796. *
  797. * @param childMinX X coord of the upper left corner of the new Raster.
  798. * @param childMinY Y coord of the upper left corner of the new Raster.
  799. */
  800. public Raster createTranslatedChild(int childMinX, int childMinY) {
  801. return createChild(minX,minY,width,height,
  802. childMinX,childMinY,null);
  803. }
  804. /**
  805. * Returns a new Raster which shares all or part of this Raster's
  806. * DataBuffer. The new Raster will possess a reference to the
  807. * current Raster, accessible through its getParent() method.
  808. *
  809. * <p> The parentX, parentY, width and height parameters
  810. * form a Rectangle in this Raster's coordinate space,
  811. * indicating the area of pixels to be shared. An error will
  812. * be thrown if this Rectangle is not contained with the bounds
  813. * of the current Raster.
  814. *
  815. * <p> The new Raster may additionally be translated to a
  816. * different coordinate system for the plane than that used by the current
  817. * Raster. The childMinX and childMinY parameters give the new
  818. * (x, y) coordinate of the upper-left pixel of the returned
  819. * Raster; the coordinate (childMinX, childMinY) in the new Raster
  820. * will map to the same pixel as the coordinate (parentX, parentY)
  821. * in the current Raster.
  822. *
  823. * <p> The new Raster may be defined to contain only a subset of
  824. * the bands of the current Raster, possibly reordered, by means
  825. * of the bandList parameter. If bandList is null, it is taken to
  826. * include all of the bands of the current Raster in their current
  827. * order.
  828. *
  829. * <p> To create a new Raster that contains a subregion of the current
  830. * Raster, but shares its coordinate system and bands,
  831. * this method should be called with childMinX equal to parentX,
  832. * childMinY equal to parentY, and bandList equal to null.
  833. *
  834. * @param parentX X coordinate of the upper left corner in this Raster's
  835. * coordinates.
  836. * @param parentY Y coordinate of the upper left corner in this Raster's
  837. * coordinates.
  838. * @param width Width of the region starting at (parentX, parentY).
  839. * @param height Height of the region starting at (parentX, parentY).
  840. * @param childMinX X coordinate of the upper left corner of
  841. * the returned Raster.
  842. * @param childMinY Y coordinate of the upper left corner of
  843. * the returned Raster.
  844. * @param bandList Array of band indices, or null to use all bands.
  845. * @exception RasterFormatException if the specified subregion is outside
  846. * of the raster bounds.
  847. */
  848. public Raster createChild(int parentX, int parentY,
  849. int width, int height,
  850. int childMinX, int childMinY,
  851. int bandList[]) {
  852. if (parentX < this.minX) {
  853. throw new RasterFormatException("parentX lies outside raster");
  854. }
  855. if (parentY < this.minY) {
  856. throw new RasterFormatException("parentY lies outside raster");
  857. }
  858. if (parentX + width > this.width + this.minX) {
  859. throw new RasterFormatException("(parentX + width) is outside raster");
  860. }
  861. if (parentY + height > this.height + this.minY) {
  862. throw new RasterFormatException("(parentY + height) is outside raster");
  863. }
  864. SampleModel subSampleModel;
  865. if (bandList == null) {
  866. subSampleModel = sampleModel;
  867. } else {
  868. subSampleModel =
  869. sampleModel.createCompatibleSampleModel(width, height);
  870. subSampleModel =
  871. subSampleModel.createSubsetSampleModel(bandList);
  872. }
  873. int deltaX = childMinX - parentX;
  874. int deltaY = childMinY - parentY;
  875. return new Raster(subSampleModel, dataBuffer,
  876. new Rectangle(childMinX, childMinY, width, height),
  877. new Point(sampleModelTranslateX + deltaX,
  878. sampleModelTranslateY + deltaY), this);
  879. }
  880. /**
  881. * Returns the bounding Rectangle of this Raster. This function returns
  882. * the same information as getMinX/MinY/Width/Height.
  883. */
  884. public Rectangle getBounds() {
  885. return new Rectangle(minX, minY, width, height);
  886. }
  887. /** Returns the minimum valid X coordinate of the Raster. */
  888. final public int getMinX() {
  889. return minX;
  890. }
  891. /** Returns the minimum valid Y coordinate of the Raster. */
  892. final public int getMinY() {
  893. return minY;
  894. }
  895. /** Returns the width in pixels of the Raster. */
  896. final public int getWidth() {
  897. return width;
  898. }
  899. /** Returns the height in pixels of the Raster. */
  900. final public int getHeight() {
  901. return height;
  902. }
  903. /** Returns the number of bands (samples per pixel) in this Raster. */
  904. final public int getNumBands() {
  905. return numBands;
  906. }
  907. /** Returns the number of data elements needed to transfer one pixel
  908. * via the getDataElements and setDataElements methods. When pixels
  909. * are transferred via these methods, they may be transferred in a
  910. * packed or unpacked format, depending on the implementation of the
  911. * underlying SampleModel. Using these methods, pixels are transferred
  912. * as an array of getNumDataElements() elements of a primitive type given
  913. * by getTransferType(). The TransferType may or may not be the same
  914. * as the storage data type of the DataBuffer.
  915. */
  916. final public int getNumDataElements() {
  917. return sampleModel.getNumDataElements();
  918. }
  919. /** Returns the TransferType used to transfer pixels via the
  920. * getDataElements and setDataElements methods. When pixels
  921. * are transferred via these methods, they may be transferred in a
  922. * packed or unpacked format, depending on the implementation of the
  923. * underlying SampleModel. Using these methods, pixels are transferred
  924. * as an array of getNumDataElements() elements of a primitive type given
  925. * by getTransferType(). The TransferType may or may not be the same
  926. * as the storage data type of the DataBuffer. The TransferType will
  927. * be one of the types defined in DataBuffer.
  928. */
  929. final public int getTransferType() {
  930. return sampleModel.getTransferType();
  931. }
  932. /** Returns the DataBuffer associated with this Raster. */
  933. public DataBuffer getDataBuffer() {
  934. return dataBuffer;
  935. }
  936. /** Returns the SampleModel that describes the layout of the image data. */
  937. public SampleModel getSampleModel() {
  938. return sampleModel;
  939. }
  940. /**
  941. * Returns data for a single pixel in a primitive array of type
  942. * TransferType. For image data supported by the Java 2D API, this
  943. * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or
  944. * DataBuffer.TYPE_INT. Data may be returned in a packed format,
  945. * thus increasing efficiency for data transfers.
  946. * There will be no explicit bounds checking on the parameters.
  947. * An ArrayIndexOutOfBoundsException may be thrown
  948. * if the coordinates are not in bounds.
  949. * A ClassCastException will be thrown if the input object is non null
  950. * and references anything other than an array of TransferType.
  951. * @see java.awt.image.SampleModel#getDataElements(int, int, Object, DataBuffer)
  952. * @param x The X coordinate of the pixel location.
  953. * @param y The Y coordinate of the pixel location.
  954. * @param outData An object reference to an array of type defined by
  955. * getTransferType() and length getNumDataElements().
  956. * If null, an array of appropriate type and size will be
  957. * allocated.
  958. * @return An object reference to an array of type defined by
  959. * getTransferType() with the requested pixel data.
  960. */
  961. public Object getDataElements(int x, int y, Object outData) {
  962. return sampleModel.getDataElements(x - sampleModelTranslateX,
  963. y - sampleModelTranslateY,
  964. outData, dataBuffer);
  965. }
  966. /**
  967. * Returns the pixel data for the specified rectangle of pixels in a
  968. * primitive array of type TransferType.
  969. * For image data supported by the Java 2D API, this
  970. * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or
  971. * DataBuffer.TYPE_INT. Data may be returned in a packed format,
  972. * thus increasing efficiency for data transfers.
  973. * There will be no explicit bounds checking on the parameters.
  974. * An ArrayIndexOutOfBoundsException may be thrown
  975. * if the coordinates are not in bounds.
  976. * A ClassCastException will be thrown if the input object is non null
  977. * and references anything other than an array of TransferType.
  978. * @see java.awt.image.SampleModel#getDataElements(int, int, int, int, Object, DataBuffer)
  979. * @param x The X coordinate of the upper left pixel location.
  980. * @param y The Y coordinate of the upper left pixel location.
  981. * @param width Width of the pixel rectangle.
  982. * @param height Height of the pixel rectangle.
  983. * @param outData An object reference to an array of type defined by
  984. * getTransferType() and length w*h*getNumDataElements().
  985. * If null, an array of appropriate type and size will be
  986. * allocated.
  987. * @return An object reference to an array of type defined by
  988. * getTransferType() with the requested pixel data.
  989. */
  990. public Object getDataElements(int x, int y, int w, int h, Object outData) {
  991. return sampleModel.getDataElements(x - sampleModelTranslateX,
  992. y - sampleModelTranslateY,
  993. w, h, outData, dataBuffer);
  994. }
  995. /**
  996. * Returns the samples in an array of int for the specified pixel.
  997. * An ArrayIndexOutOfBoundsException may be thrown
  998. * if the coordinates are not in bounds.
  999. * @param x The X coordinate of the pixel location.
  1000. * @param y The Y coordinate of the pixel location.
  1001. * @param iArray An optionally preallocated int array.
  1002. */
  1003. public int[] getPixel(int x, int y, int iArray[]) {
  1004. return sampleModel.getPixel(x - sampleModelTranslateX,
  1005. y - sampleModelTranslateY,
  1006. iArray, dataBuffer);
  1007. }
  1008. /**
  1009. * Returns the samples in an array of float for the
  1010. * specified pixel.
  1011. * An ArrayIndexOutOfBoundsException may be thrown
  1012. * if the coordinates are not in bounds.
  1013. * @param x The X coordinate of the pixel location.
  1014. * @param y The Y coordinate of the pixel location.
  1015. * @param fArray An optionally preallocated float array.
  1016. */
  1017. public float[] getPixel(int x, int y, float fArray[]) {
  1018. return sampleModel.getPixel(x - sampleModelTranslateX,
  1019. y - sampleModelTranslateY,
  1020. fArray, dataBuffer);
  1021. }
  1022. /**
  1023. * Returns the samples in an array of double for the specified pixel.
  1024. * An ArrayIndexOutOfBoundsException may be thrown
  1025. * if the coordinates are not in bounds.
  1026. * @param x The X coordinate of the pixel location.
  1027. * @param y The Y coordinate of the pixel location.
  1028. * @param dArray An optionally preallocated double array.
  1029. */
  1030. public double[] getPixel(int x, int y, double dArray[]) {
  1031. return sampleModel.getPixel(x - sampleModelTranslateX,
  1032. y - sampleModelTranslateY,
  1033. dArray, dataBuffer);
  1034. }
  1035. /**
  1036. * Returns an int array containing all samples for a rectangle of pixels,
  1037. * one sample per array element.
  1038. * An ArrayIndexOutOfBoundsException may be thrown
  1039. * if the coordinates are not in bounds.
  1040. * @param x The X coordinate of the upper left pixel location.
  1041. * @param y The Y coordinate of the upper left pixel location.
  1042. * @param w Width of the pixel rectangle.
  1043. * @param h Height of the pixel rectangle.
  1044. * @param iArray An optionally pre-allocated int array.
  1045. */
  1046. public int[] getPixels(int x, int y, int w, int h, int iArray[]) {
  1047. return sampleModel.getPixels(x - sampleModelTranslateX,
  1048. y - sampleModelTranslateY, w, h,
  1049. iArray, dataBuffer);
  1050. }
  1051. /**
  1052. * Returns a float array containing all samples for a rectangle of pixels,
  1053. * one sample per array element.
  1054. * An ArrayIndexOutOfBoundsException may be thrown
  1055. * if the coordinates are not in bounds.
  1056. * @param x The X coordinate of the upper left pixel location.
  1057. * @param y The Y coordinate of the upper left pixel location.
  1058. * @param w Width of the pixel rectangle.
  1059. * @param h Height of the pixel rectangle.
  1060. * @param fArray An optionally pre-allocated float array.
  1061. */
  1062. public float[] getPixels(int x, int y, int w, int h,
  1063. float fArray[]) {
  1064. return sampleModel.getPixels(x - sampleModelTranslateX,
  1065. y - sampleModelTranslateY, w, h,
  1066. fArray, dataBuffer);
  1067. }
  1068. /**
  1069. * Returns a double array containing all samples for a rectangle of pixels,
  1070. * one sample per array element.
  1071. * An ArrayIndexOutOfBoundsException may be thrown
  1072. * if the coordinates are not in bounds.
  1073. * @param x The X coordinate of the upper left pixel location.
  1074. * @param y The Y coordinate of the upper left pixel location.
  1075. * @param w Width of the pixel rectangle.
  1076. * @param h Height of the pixel rectangle.
  1077. * @param dArray An optionally pre-allocated double array.
  1078. */
  1079. public double[] getPixels(int x, int y, int w, int h,
  1080. double dArray[]) {
  1081. return sampleModel.getPixels(x - sampleModelTranslateX,
  1082. y - sampleModelTranslateY,
  1083. w, h, dArray, dataBuffer);
  1084. }
  1085. /**
  1086. * Returns the sample in a specified band for the pixel located
  1087. * at (x,y) as an int.
  1088. * An ArrayIndexOutOfBoundsException may be thrown
  1089. * if the coordinates are not in bounds.
  1090. * @param x The X coordinate of the pixel location.
  1091. * @param y The Y coordinate of the pixel location.
  1092. * @param b The band to return.
  1093. */
  1094. public int getSample(int x, int y, int b) {
  1095. return sampleModel.getSample(x - sampleModelTranslateX,
  1096. y - sampleModelTranslateY, b,
  1097. dataBuffer);
  1098. }
  1099. /**
  1100. * Returns the sample in a specified band
  1101. * for the pixel located at (x,y) as a float.
  1102. * An ArrayIndexOutOfBoundsException may be thrown
  1103. * if the coordinates are not in bounds.
  1104. * @param x The X coordinate of the pixel location.
  1105. * @param y The Y coordinate of the pixel location.
  1106. * @param b The band to return.
  1107. */
  1108. public float getSampleFloat(int x, int y, int b) {
  1109. return sampleModel.getSampleFloat(x - sampleModelTranslateX,
  1110. y - sampleModelTranslateY, b,
  1111. dataBuffer);
  1112. }
  1113. /**
  1114. * Returns the sample in a specified band
  1115. * for a pixel located at (x,y) as a double.
  1116. * An ArrayIndexOutOfBoundsException may be thrown
  1117. * if the coordinates are not in bounds.
  1118. * @param x The X coordinate of the pixel location.
  1119. * @param y The Y coordinate of the pixel location.
  1120. * @param b The band to return.
  1121. */
  1122. public double getSampleDouble(int x, int y, int b) {
  1123. return sampleModel.getSampleDouble(x - sampleModelTranslateX,
  1124. y - sampleModelTranslateY,
  1125. b, dataBuffer);
  1126. }
  1127. /**
  1128. * Returns the samples for a specified band for the specified rectangle
  1129. * of pixels in an int array, one sample per array element.
  1130. * An ArrayIndexOutOfBoundsException may be thrown
  1131. * if the coordinates are not in bounds.
  1132. * @param x The X coordinate of the upper left pixel location.
  1133. * @param y The Y coordinate of the upper left pixel location.
  1134. * @param w Width of the pixel rectangle.
  1135. * @param h Height of the pixel rectangle.
  1136. * @param b The band to return.
  1137. * @param iArray An optionally pre-allocated int array.
  1138. */
  1139. public int[] getSamples(int x, int y, int w, int h, int b,
  1140. int iArray[]) {
  1141. return sampleModel.getSamples(x - sampleModelTranslateX,
  1142. y - sampleModelTranslateY,
  1143. w, h, b, iArray,
  1144. dataBuffer);
  1145. }
  1146. /**
  1147. * Returns the samples for a specified band for the specified rectangle
  1148. * of pixels in a float array, one sample per array element.
  1149. * An ArrayIndexOutOfBoundsException may be thrown
  1150. * if the coordinates are not in bounds.
  1151. * @param x The X coordinate of the upper left pixel location.
  1152. * @param y The Y coordinate of the upper left pixel location.
  1153. * @param w Width of the pixel rectangle.
  1154. * @param h Height of the pixel rectangle.
  1155. * @param b The band to return.
  1156. * @param fArray An optionally pre-allocated float array.
  1157. */
  1158. public float[] getSamples(int x, int y, int w, int h, int b,
  1159. float fArray[]) {
  1160. return sampleModel.getSamples(x - sampleModelTranslateX,
  1161. y - sampleModelTranslateY,
  1162. w, h, b, fArray, dataBuffer);
  1163. }
  1164. /**
  1165. * Returns the samples for a specified band for a specified rectangle
  1166. * of pixels in a double array, one sample per array element.
  1167. * An ArrayIndexOutOfBoundsException may be thrown
  1168. * if the coordinates are not in bounds.
  1169. * @param x The X coordinate of the upper left pixel location.
  1170. * @param y The Y coordinate of the upper left pixel location.
  1171. * @param w Width of the pixel rectangle.
  1172. * @param h Height of the pixel rectangle.
  1173. * @param b The band to return.
  1174. * @param dArray An optionally pre-allocated double array.
  1175. */
  1176. public double[] getSamples(int x, int y, int w, int h, int b,
  1177. double dArray[]) {
  1178. return sampleModel.getSamples(x - sampleModelTranslateX,
  1179. y - sampleModelTranslateY,
  1180. w, h, b, dArray, dataBuffer);
  1181. }
  1182. }