1. /*
  2. * @(#)TexturePaintContext.java 1.21 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.image.BufferedImage;
  9. import java.awt.image.Raster;
  10. import java.awt.image.WritableRaster;
  11. import java.awt.image.ColorModel;
  12. import java.awt.geom.AffineTransform;
  13. import java.awt.geom.NoninvertibleTransformException;
  14. import sun.awt.image.IntegerInterleavedRaster;
  15. import sun.awt.image.ByteInterleavedRaster;
  16. abstract class TexturePaintContext implements PaintContext {
  17. ColorModel colorModel;
  18. int bWidth;
  19. int bHeight;
  20. int maxWidth;
  21. WritableRaster outRas;
  22. double xOrg;
  23. double yOrg;
  24. double incXAcross;
  25. double incYAcross;
  26. double incXDown;
  27. double incYDown;
  28. int colincx;
  29. int colincy;
  30. int colincxerr;
  31. int colincyerr;
  32. int rowincx;
  33. int rowincy;
  34. int rowincxerr;
  35. int rowincyerr;
  36. public static PaintContext getContext(BufferedImage bufImg,
  37. AffineTransform xform,
  38. RenderingHints hints,
  39. Rectangle devBounds) {
  40. WritableRaster raster = bufImg.getRaster();
  41. ColorModel cm = bufImg.getColorModel();
  42. int maxw = devBounds.width;
  43. if (raster instanceof IntegerInterleavedRaster) {
  44. IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster;
  45. if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) {
  46. return new Int(iir, cm, xform, maxw);
  47. }
  48. } else if (raster instanceof ByteInterleavedRaster) {
  49. ByteInterleavedRaster bir = (ByteInterleavedRaster) raster;
  50. if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) {
  51. return new Byte(bir, cm, xform, maxw);
  52. }
  53. }
  54. return new Any(raster, cm, xform, maxw);
  55. }
  56. TexturePaintContext(ColorModel cm, AffineTransform xform,
  57. int bWidth, int bHeight, int maxw) {
  58. this.colorModel = cm;
  59. this.bWidth = bWidth;
  60. this.bHeight = bHeight;
  61. this.maxWidth = maxw;
  62. try {
  63. xform = xform.createInverse();
  64. } catch (NoninvertibleTransformException e) {
  65. xform.setToScale(0, 0);
  66. }
  67. this.incXAcross = mod(xform.getScaleX(), bWidth);
  68. this.incYAcross = mod(xform.getShearY(), bHeight);
  69. this.incXDown = mod(xform.getShearX(), bWidth);
  70. this.incYDown = mod(xform.getScaleY(), bHeight);
  71. this.xOrg = xform.getTranslateX();
  72. this.yOrg = xform.getTranslateY();
  73. this.colincx = (int) incXAcross;
  74. this.colincy = (int) incYAcross;
  75. this.colincxerr = fractAsInt(incXAcross);
  76. this.colincyerr = fractAsInt(incYAcross);
  77. this.rowincx = (int) incXDown;
  78. this.rowincy = (int) incYDown;
  79. this.rowincxerr = fractAsInt(incXDown);
  80. this.rowincyerr = fractAsInt(incYDown);
  81. }
  82. static int fractAsInt(double d) {
  83. return (int) ((d % 1.0) * Integer.MAX_VALUE);
  84. }
  85. static double mod(double num, double den) {
  86. num = num % den;
  87. if (num < 0) {
  88. num += den;
  89. if (num >= den) {
  90. // For very small negative numerators, the answer might
  91. // be such a tiny bit less than den that the difference
  92. // is smaller than the mantissa of a double allows and
  93. // the result would then be rounded to den. If that is
  94. // the case then we map that number to 0 as the nearest
  95. // modulus representation.
  96. num = 0;
  97. }
  98. }
  99. return num;
  100. }
  101. /**
  102. * Release the resources allocated for the operation.
  103. */
  104. public void dispose() {
  105. // Nothing to dispose
  106. }
  107. /**
  108. * Return the ColorModel of the output.
  109. */
  110. public ColorModel getColorModel() {
  111. return colorModel;
  112. }
  113. /**
  114. * Return a Raster containing the colors generated for the graphics
  115. * operation.
  116. * @param x,y,w,h The area in device space for which colors are
  117. * generated.
  118. */
  119. public Raster getRaster(int x, int y, int w, int h) {
  120. if (outRas == null ||
  121. outRas.getWidth() < w ||
  122. outRas.getHeight() < h)
  123. {
  124. // If h==1, we will probably get lots of "scanline" rects
  125. outRas = makeRaster((h == 1 ? Math.max(w, maxWidth) : w), h);
  126. }
  127. double X = mod(xOrg + x * incXAcross + y * incXDown, bWidth);
  128. double Y = mod(yOrg + x * incYAcross + y * incYDown, bHeight);
  129. setRaster((int) X, (int) Y,
  130. (int) ((X % 1.0) * Integer.MAX_VALUE),
  131. (int) ((Y % 1.0) * Integer.MAX_VALUE),
  132. w, h, bWidth, bHeight,
  133. colincx, colincxerr,
  134. colincy, colincyerr,
  135. rowincx, rowincxerr,
  136. rowincy, rowincyerr);
  137. return outRas;
  138. }
  139. public abstract WritableRaster makeRaster(int w, int h);
  140. public abstract void setRaster(int x, int y, int xerr, int yerr,
  141. int w, int h, int bWidth, int bHeight,
  142. int colincx, int colincxerr,
  143. int colincy, int colincyerr,
  144. int rowincx, int rowincxerr,
  145. int rowincy, int rowincyerr);
  146. static class Int extends TexturePaintContext {
  147. IntegerInterleavedRaster srcRas;
  148. int inData[];
  149. int inOff;
  150. int inSpan;
  151. int outData[];
  152. int outOff;
  153. int outSpan;
  154. public Int(IntegerInterleavedRaster srcRas,
  155. ColorModel cm, AffineTransform xform, int maxw) {
  156. super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
  157. this.srcRas = srcRas;
  158. this.inData = srcRas.getDataStorage();
  159. this.inSpan = srcRas.getScanlineStride();
  160. this.inOff = srcRas.getDataOffset(0);
  161. }
  162. public WritableRaster makeRaster(int w, int h) {
  163. WritableRaster ras = srcRas.createCompatibleWritableRaster(w, h);
  164. IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras;
  165. outData = iiRas.getDataStorage();
  166. outSpan = iiRas.getScanlineStride();
  167. outOff = iiRas.getDataOffset(0);
  168. return ras;
  169. }
  170. public void setRaster(int x, int y, int xerr, int yerr,
  171. int w, int h, int bWidth, int bHeight,
  172. int colincx, int colincxerr,
  173. int colincy, int colincyerr,
  174. int rowincx, int rowincxerr,
  175. int rowincy, int rowincyerr) {
  176. int[] inData = this.inData;
  177. int[] outData = this.outData;
  178. int out = outOff;
  179. int inSpan = this.inSpan;
  180. int inOff = this.inOff;
  181. int outSpan = this.outSpan;
  182. boolean normalx = (colincx == 1 && colincxerr == 0 &&
  183. colincy == 0 && colincyerr == 0);
  184. int rowx = x;
  185. int rowy = y;
  186. int rowxerr = xerr;
  187. int rowyerr = yerr;
  188. if (normalx) {
  189. outSpan -= w;
  190. }
  191. for (int j = 0; j < h; j++) {
  192. if (normalx) {
  193. int in = inOff + rowy * inSpan + bWidth;
  194. x = bWidth - rowx;
  195. out += w;
  196. if (bWidth >= 32) {
  197. int i = w;
  198. while (i > 0) {
  199. int copyw = (i < x) ? i : x;
  200. System.arraycopy(inData, in - x,
  201. outData, out - i,
  202. copyw);
  203. i -= copyw;
  204. if ((x -= copyw) == 0) {
  205. x = bWidth;
  206. }
  207. }
  208. } else {
  209. for (int i = w; i > 0; i--) {
  210. outData[out - i] = inData[in - x];
  211. if (--x == 0) {
  212. x = bWidth;
  213. }
  214. }
  215. }
  216. } else {
  217. x = rowx;
  218. y = rowy;
  219. xerr = rowxerr;
  220. yerr = rowyerr;
  221. for (int i = 0; i < w; i++) {
  222. outData[out + i] = inData[inOff + y * inSpan + x];
  223. if ((xerr += colincxerr) < 0) {
  224. xerr &= Integer.MAX_VALUE;
  225. x++;
  226. }
  227. if ((x += colincx) >= bWidth) {
  228. x -= bWidth;
  229. }
  230. if ((yerr += colincyerr) < 0) {
  231. yerr &= Integer.MAX_VALUE;
  232. y++;
  233. }
  234. if ((y += colincy) >= bHeight) {
  235. y -= bHeight;
  236. }
  237. }
  238. }
  239. if ((rowxerr += rowincxerr) < 0) {
  240. rowxerr &= Integer.MAX_VALUE;
  241. rowx++;
  242. }
  243. if ((rowx += rowincx) >= bWidth) {
  244. rowx -= bWidth;
  245. }
  246. if ((rowyerr += rowincyerr) < 0) {
  247. rowyerr &= Integer.MAX_VALUE;
  248. rowy++;
  249. }
  250. if ((rowy += rowincy) >= bHeight) {
  251. rowy -= bHeight;
  252. }
  253. out += outSpan;
  254. }
  255. }
  256. }
  257. static class Byte extends TexturePaintContext {
  258. ByteInterleavedRaster srcRas;
  259. byte inData[];
  260. int inOff;
  261. int inSpan;
  262. byte outData[];
  263. int outOff;
  264. int outSpan;
  265. public Byte(ByteInterleavedRaster srcRas,
  266. ColorModel cm, AffineTransform xform, int maxw) {
  267. super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
  268. this.srcRas = srcRas;
  269. this.inData = srcRas.getDataStorage();
  270. this.inSpan = srcRas.getScanlineStride();
  271. this.inOff = srcRas.getDataOffset(0);
  272. }
  273. public WritableRaster makeRaster(int w, int h) {
  274. WritableRaster ras = srcRas.createCompatibleWritableRaster(w, h);
  275. ByteInterleavedRaster biRas = (ByteInterleavedRaster) ras;
  276. outData = biRas.getDataStorage();
  277. outSpan = biRas.getScanlineStride();
  278. outOff = biRas.getDataOffset(0);
  279. return ras;
  280. }
  281. public void setRaster(int x, int y, int xerr, int yerr,
  282. int w, int h, int bWidth, int bHeight,
  283. int colincx, int colincxerr,
  284. int colincy, int colincyerr,
  285. int rowincx, int rowincxerr,
  286. int rowincy, int rowincyerr) {
  287. byte[] inData = this.inData;
  288. byte[] outData = this.outData;
  289. int out = outOff;
  290. int inSpan = this.inSpan;
  291. int inOff = this.inOff;
  292. int outSpan = this.outSpan;
  293. boolean normalx = (colincx == 1 && colincxerr == 0 &&
  294. colincy == 0 && colincyerr == 0);
  295. int rowx = x;
  296. int rowy = y;
  297. int rowxerr = xerr;
  298. int rowyerr = yerr;
  299. if (normalx) {
  300. outSpan -= w;
  301. }
  302. for (int j = 0; j < h; j++) {
  303. if (normalx) {
  304. int in = inOff + rowy * inSpan + bWidth;
  305. x = bWidth - rowx;
  306. out += w;
  307. if (bWidth >= 32) {
  308. int i = w;
  309. while (i > 0) {
  310. int copyw = (i < x) ? i : x;
  311. System.arraycopy(inData, in - x,
  312. outData, out - i,
  313. copyw);
  314. i -= copyw;
  315. if ((x -= copyw) == 0) {
  316. x = bWidth;
  317. }
  318. }
  319. } else {
  320. for (int i = w; i > 0; i--) {
  321. outData[out - i] = inData[in - x];
  322. if (--x == 0) {
  323. x = bWidth;
  324. }
  325. }
  326. }
  327. } else {
  328. x = rowx;
  329. y = rowy;
  330. xerr = rowxerr;
  331. yerr = rowyerr;
  332. for (int i = 0; i < w; i++) {
  333. outData[out + i] = inData[inOff + y * inSpan + x];
  334. if ((xerr += colincxerr) < 0) {
  335. xerr &= Integer.MAX_VALUE;
  336. x++;
  337. }
  338. if ((x += colincx) >= bWidth) {
  339. x -= bWidth;
  340. }
  341. if ((yerr += colincyerr) < 0) {
  342. yerr &= Integer.MAX_VALUE;
  343. y++;
  344. }
  345. if ((y += colincy) >= bHeight) {
  346. y -= bHeight;
  347. }
  348. }
  349. }
  350. if ((rowxerr += rowincxerr) < 0) {
  351. rowxerr &= Integer.MAX_VALUE;
  352. rowx++;
  353. }
  354. if ((rowx += rowincx) >= bWidth) {
  355. rowx -= bWidth;
  356. }
  357. if ((rowyerr += rowincyerr) < 0) {
  358. rowyerr &= Integer.MAX_VALUE;
  359. rowy++;
  360. }
  361. if ((rowy += rowincy) >= bHeight) {
  362. rowy -= bHeight;
  363. }
  364. out += outSpan;
  365. }
  366. }
  367. }
  368. static class Any extends TexturePaintContext {
  369. WritableRaster srcRas;
  370. public Any(WritableRaster srcRas,
  371. ColorModel cm, AffineTransform xform, int maxw) {
  372. super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
  373. this.srcRas = srcRas;
  374. }
  375. public WritableRaster makeRaster(int w, int h) {
  376. return srcRas.createCompatibleWritableRaster(w, h);
  377. }
  378. public void setRaster(int x, int y, int xerr, int yerr,
  379. int w, int h, int bWidth, int bHeight,
  380. int colincx, int colincxerr,
  381. int colincy, int colincyerr,
  382. int rowincx, int rowincxerr,
  383. int rowincy, int rowincyerr) {
  384. Object data = null;
  385. int rowx = x;
  386. int rowy = y;
  387. int rowxerr = xerr;
  388. int rowyerr = yerr;
  389. WritableRaster srcRas = this.srcRas;
  390. WritableRaster outRas = this.outRas;
  391. for (int j = 0; j < h; j++) {
  392. x = rowx;
  393. y = rowy;
  394. xerr = rowxerr;
  395. yerr = rowyerr;
  396. for (int i = 0; i < w; i++) {
  397. data = srcRas.getDataElements(x, y, data);
  398. outRas.setDataElements(i, j, data);
  399. if ((xerr += colincxerr) < 0) {
  400. xerr &= Integer.MAX_VALUE;
  401. x++;
  402. }
  403. if ((x += colincx) >= bWidth) {
  404. x -= bWidth;
  405. }
  406. if ((yerr += colincyerr) < 0) {
  407. yerr &= Integer.MAX_VALUE;
  408. y++;
  409. }
  410. if ((y += colincy) >= bHeight) {
  411. y -= bHeight;
  412. }
  413. }
  414. if ((rowxerr += rowincxerr) < 0) {
  415. rowxerr &= Integer.MAX_VALUE;
  416. rowx++;
  417. }
  418. if ((rowx += rowincx) >= bWidth) {
  419. rowx -= bWidth;
  420. }
  421. if ((rowyerr += rowincyerr) < 0) {
  422. rowyerr &= Integer.MAX_VALUE;
  423. rowy++;
  424. }
  425. if ((rowy += rowincy) >= bHeight) {
  426. rowy -= bHeight;
  427. }
  428. }
  429. }
  430. }
  431. }