WritableRaster.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* Copyright (C) 2000, 2002, 2003, 2006, Free Software Foundation
  2. This file is part of GNU Classpath.
  3. GNU Classpath is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. GNU Classpath is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GNU Classpath; see the file COPYING. If not, write to the
  13. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  14. 02110-1301 USA.
  15. Linking this library statically or dynamically with other modules is
  16. making a combined work based on this library. Thus, the terms and
  17. conditions of the GNU General Public License cover the whole
  18. combination.
  19. As a special exception, the copyright holders of this library give you
  20. permission to link this library with independent modules to produce an
  21. executable, regardless of the license terms of these independent
  22. modules, and to copy and distribute the resulting executable under
  23. terms of your choice, provided that you also meet, for each linked
  24. independent module, the terms and conditions of the license of that
  25. module. An independent module is a module which is not derived from
  26. or based on this library. If you modify this library, you may extend
  27. this exception to your version of the library, but you are not
  28. obligated to do so. If you do not wish to do so, delete this
  29. exception statement from your version. */
  30. package java.awt.image;
  31. import java.awt.Point;
  32. import java.awt.Rectangle;
  33. /**
  34. * A raster with methods to support updating pixel values.
  35. *
  36. * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  37. */
  38. public class WritableRaster extends Raster
  39. {
  40. /**
  41. * Creates a new <code>WritableRaster</code>.
  42. *
  43. * @param sampleModel the sample model.
  44. * @param origin the origin.
  45. */
  46. protected WritableRaster(SampleModel sampleModel, Point origin)
  47. {
  48. this(sampleModel, sampleModel.createDataBuffer(), origin);
  49. }
  50. /**
  51. * Creates a new <code>WritableRaster</code> instance.
  52. *
  53. * @param sampleModel the sample model.
  54. * @param dataBuffer the data buffer.
  55. * @param origin the origin.
  56. */
  57. protected WritableRaster(SampleModel sampleModel, DataBuffer dataBuffer,
  58. Point origin)
  59. {
  60. this(sampleModel, dataBuffer,
  61. new Rectangle(origin != null ? origin.x : 0,
  62. origin != null ? origin.y : 0,
  63. sampleModel.getWidth(), sampleModel.getHeight()),
  64. origin, null);
  65. }
  66. /**
  67. * Creates a new <code>WritableRaster</code> instance.
  68. *
  69. * @param sampleModel the sample model.
  70. * @param dataBuffer the data buffer.
  71. * @param aRegion the raster's bounds.
  72. * @param sampleModelTranslate the translation.
  73. * @param parent the parent.
  74. */
  75. protected WritableRaster(SampleModel sampleModel,
  76. DataBuffer dataBuffer,
  77. Rectangle aRegion,
  78. Point sampleModelTranslate,
  79. WritableRaster parent)
  80. {
  81. super(sampleModel, dataBuffer, aRegion, sampleModelTranslate, parent);
  82. }
  83. /**
  84. * Returns the raster's parent, cast as a {@link WritableRaster}.
  85. *
  86. * @return The raster's parent.
  87. */
  88. public WritableRaster getWritableParent()
  89. {
  90. return (WritableRaster) getParent();
  91. }
  92. /**
  93. * @param childMinX
  94. * @param childMinY
  95. * @return
  96. */
  97. public WritableRaster createWritableTranslatedChild(int childMinX,
  98. int childMinY)
  99. {
  100. return createWritableChild(minX, minY, width, height,
  101. childMinX, childMinY, null);
  102. }
  103. /**
  104. *
  105. * @param parentX
  106. * @param parentY
  107. * @param w
  108. * @param h
  109. * @param childMinX
  110. * @param childMinY
  111. * @param bandList
  112. * @return
  113. */
  114. public WritableRaster createWritableChild(int parentX, int parentY,
  115. int w, int h, int childMinX, int childMinY, int[] bandList)
  116. {
  117. // This mirrors the code from the super class
  118. if (parentX < minX || parentX + w > minX + width
  119. || parentY < minY || parentY + h > minY + height)
  120. throw new RasterFormatException("Child raster extends beyond parent");
  121. SampleModel sm = (bandList == null) ?
  122. sampleModel :
  123. sampleModel.createSubsetSampleModel(bandList);
  124. return new WritableRaster(sm, getDataBuffer(),
  125. new Rectangle(childMinX, childMinY, w, h),
  126. new Point(sampleModelTranslateX + childMinX -
  127. parentX,
  128. sampleModelTranslateY + childMinY -
  129. parentY),
  130. this);
  131. }
  132. public Raster createChild(int parentX, int parentY, int width,
  133. int height, int childMinX, int childMinY,
  134. int[] bandList)
  135. {
  136. if (parentX < minX || parentX + width > minX + this.width
  137. || parentY < minY || parentY + height > minY + this.height)
  138. throw new RasterFormatException("Child raster extends beyond parent");
  139. SampleModel sm = (bandList == null) ?
  140. sampleModel :
  141. sampleModel.createSubsetSampleModel(bandList);
  142. return new WritableRaster(sm, dataBuffer,
  143. new Rectangle(childMinX, childMinY, width, height),
  144. new Point(sampleModelTranslateX + childMinX - parentX,
  145. sampleModelTranslateY + childMinY - parentY),
  146. this);
  147. }
  148. public void setDataElements(int x, int y, Object inData)
  149. {
  150. sampleModel.setDataElements(x - sampleModelTranslateX,
  151. y - sampleModelTranslateY, inData, dataBuffer);
  152. }
  153. public void setDataElements(int x, int y, Raster inRaster)
  154. {
  155. Object dataElements = getDataElements(0, 0, inRaster.getWidth(),
  156. inRaster.getHeight(), null);
  157. setDataElements(x, y, dataElements);
  158. }
  159. public void setDataElements(int x, int y, int w, int h, Object inData)
  160. {
  161. sampleModel.setDataElements(x - sampleModelTranslateX,
  162. y - sampleModelTranslateY, w, h, inData, dataBuffer);
  163. }
  164. /**
  165. *
  166. * @param srcRaster
  167. */
  168. public void setRect(Raster srcRaster)
  169. {
  170. setRect(0, 0, srcRaster);
  171. }
  172. /**
  173. *
  174. * @param dx
  175. * @param dy
  176. * @param srcRaster
  177. */
  178. public void setRect(int dx, int dy, Raster srcRaster)
  179. {
  180. Rectangle targetUnclipped = new Rectangle(srcRaster.getMinX() + dx,
  181. srcRaster.getMinY() + dy, srcRaster.getWidth(), srcRaster.getHeight());
  182. Rectangle target = getBounds().intersection(targetUnclipped);
  183. if (target.isEmpty()) return;
  184. int sx = target.x - dx;
  185. int sy = target.y - dy;
  186. // FIXME: Do tests on rasters and use get/set data instead.
  187. /* The JDK documentation seems to imply this implementation.
  188. (the trucation of higher bits), but an implementation using
  189. get/setDataElements would be more efficient. None of the
  190. implementations would do anything sensible when the sample
  191. models don't match.
  192. But this is probably not the place to consider such
  193. optimizations.*/
  194. int[] pixels = srcRaster.getPixels(sx, sy, target.width, target.height,
  195. (int[]) null);
  196. setPixels(target.x, target.y, target.width, target.height, pixels);
  197. }
  198. /**
  199. * Sets the samples for the pixel at (x, y) in the raster to the specified
  200. * values.
  201. *
  202. * @param x the x-coordinate of the pixel.
  203. * @param y the y-coordinate of the pixel.
  204. * @param iArray the sample values (<code>null</code> not permitted).
  205. *
  206. * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
  207. */
  208. public void setPixel(int x, int y, int[] iArray)
  209. {
  210. sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY,
  211. iArray, dataBuffer);
  212. }
  213. /**
  214. * Sets the samples for the pixel at (x, y) in the raster to the specified
  215. * values.
  216. *
  217. * @param x the x-coordinate of the pixel.
  218. * @param y the y-coordinate of the pixel.
  219. * @param fArray the sample values (<code>null</code> not permitted).
  220. *
  221. * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
  222. */
  223. public void setPixel(int x, int y, float[] fArray)
  224. {
  225. sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY,
  226. fArray, dataBuffer);
  227. }
  228. /**
  229. * Sets the samples for the pixel at (x, y) in the raster to the specified
  230. * values.
  231. *
  232. * @param x the x-coordinate of the pixel.
  233. * @param y the y-coordinate of the pixel.
  234. * @param dArray the sample values (<code>null</code> not permitted).
  235. *
  236. * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
  237. */
  238. public void setPixel(int x, int y, double[] dArray)
  239. {
  240. sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY,
  241. dArray, dataBuffer);
  242. }
  243. /**
  244. * Sets the sample values for the pixels in the region specified by
  245. * (x, y, w, h) in the raster. The array is ordered by pixels (that is, all
  246. * the samples for the first pixel are grouped together, followed by all the
  247. * samples for the second pixel, and so on).
  248. *
  249. * @param x the x-coordinate of the top-left pixel.
  250. * @param y the y-coordinate of the top-left pixel.
  251. * @param w the width of the region of pixels.
  252. * @param h the height of the region of pixels.
  253. * @param iArray the pixel sample values (<code>null</code> not permitted).
  254. *
  255. * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
  256. */
  257. public void setPixels(int x, int y, int w, int h, int[] iArray)
  258. {
  259. sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY,
  260. w, h, iArray, dataBuffer);
  261. }
  262. /**
  263. * Sets the sample values for the pixels in the region specified by
  264. * (x, y, w, h) in the raster. The array is ordered by pixels (that is, all
  265. * the samples for the first pixel are grouped together, followed by all the
  266. * samples for the second pixel, and so on).
  267. *
  268. * @param x the x-coordinate of the top-left pixel.
  269. * @param y the y-coordinate of the top-left pixel.
  270. * @param w the width of the region of pixels.
  271. * @param h the height of the region of pixels.
  272. * @param fArray the pixel sample values (<code>null</code> not permitted).
  273. *
  274. * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
  275. */
  276. public void setPixels(int x, int y, int w, int h, float[] fArray)
  277. {
  278. sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY,
  279. w, h, fArray, dataBuffer);
  280. }
  281. /**
  282. * Sets the sample values for the pixels in the region specified by
  283. * (x, y, w, h) in the raster. The array is ordered by pixels (that is, all
  284. * the samples for the first pixel are grouped together, followed by all the
  285. * samples for the second pixel, and so on).
  286. *
  287. * @param x the x-coordinate of the top-left pixel.
  288. * @param y the y-coordinate of the top-left pixel.
  289. * @param w the width of the region of pixels.
  290. * @param h the height of the region of pixels.
  291. * @param dArray the pixel sample values (<code>null</code> not permitted).
  292. *
  293. * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
  294. */
  295. public void setPixels(int x, int y, int w, int h, double[] dArray)
  296. {
  297. sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY,
  298. w, h, dArray, dataBuffer);
  299. }
  300. /**
  301. * Sets the sample value for a band for the pixel at (x, y) in the raster.
  302. *
  303. * @param x the x-coordinate of the pixel.
  304. * @param y the y-coordinate of the pixel.
  305. * @param b the band (in the range <code>0</code> to
  306. * <code>getNumBands() - 1</code>).
  307. * @param s the sample value.
  308. */
  309. public void setSample(int x, int y, int b, int s)
  310. {
  311. sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY,
  312. b, s, dataBuffer);
  313. }
  314. /**
  315. * Sets the sample value for a band for the pixel at (x, y) in the raster.
  316. *
  317. * @param x the x-coordinate of the pixel.
  318. * @param y the y-coordinate of the pixel.
  319. * @param b the band (in the range <code>0</code> to
  320. * <code>getNumBands() - 1</code>).
  321. * @param s the sample value.
  322. */
  323. public void setSample(int x, int y, int b, float s)
  324. {
  325. sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY,
  326. b, s, dataBuffer);
  327. }
  328. /**
  329. * Sets the sample value for a band for the pixel at (x, y) in the raster.
  330. *
  331. * @param x the x-coordinate of the pixel.
  332. * @param y the y-coordinate of the pixel.
  333. * @param b the band (in the range <code>0</code> to
  334. * <code>getNumBands() - 1</code>).
  335. * @param s the sample value.
  336. */
  337. public void setSample(int x, int y, int b, double s)
  338. {
  339. sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY,
  340. b, s, dataBuffer);
  341. }
  342. /**
  343. * Sets the sample values for one band for the pixels in the region
  344. * specified by (x, y, w, h) in the raster.
  345. *
  346. * @param x the x-coordinate of the top-left pixel.
  347. * @param y the y-coordinate of the top-left pixel.
  348. * @param w the width of the region of pixels.
  349. * @param h the height of the region of pixels.
  350. * @param b the band (in the range <code>0</code> to
  351. * </code>getNumBands() - 1</code>).
  352. * @param iArray the sample values (<code>null</code> not permitted).
  353. *
  354. * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
  355. */
  356. public void setSamples(int x, int y, int w, int h, int b,
  357. int[] iArray)
  358. {
  359. sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY,
  360. w, h, b, iArray, dataBuffer);
  361. }
  362. /**
  363. * Sets the sample values for one band for the pixels in the region
  364. * specified by (x, y, w, h) in the raster.
  365. *
  366. * @param x the x-coordinate of the top-left pixel.
  367. * @param y the y-coordinate of the top-left pixel.
  368. * @param w the width of the region of pixels.
  369. * @param h the height of the region of pixels.
  370. * @param b the band (in the range <code>0</code> to
  371. * </code>getNumBands() - 1</code>).
  372. * @param fArray the sample values (<code>null</code> not permitted).
  373. *
  374. * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
  375. */
  376. public void setSamples(int x, int y, int w, int h, int b,
  377. float[] fArray)
  378. {
  379. sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY,
  380. w, h, b, fArray, dataBuffer);
  381. }
  382. /**
  383. * Sets the sample values for one band for the pixels in the region
  384. * specified by (x, y, w, h) in the raster.
  385. *
  386. * @param x the x-coordinate of the top-left pixel.
  387. * @param y the y-coordinate of the top-left pixel.
  388. * @param w the width of the region of pixels.
  389. * @param h the height of the region of pixels.
  390. * @param b the band (in the range <code>0</code> to
  391. * </code>getNumBands() - 1</code>).
  392. * @param dArray the sample values (<code>null</code> not permitted).
  393. *
  394. * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
  395. */
  396. public void setSamples(int x, int y, int w, int h, int b,
  397. double[] dArray)
  398. {
  399. sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY,
  400. w, h, b, dArray, dataBuffer);
  401. }
  402. }