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