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