1. /*
  2. * @(#)TexturePaintContext.java 1.28 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. 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.image.DirectColorModel;
  13. import java.awt.image.IndexColorModel;
  14. import java.awt.geom.AffineTransform;
  15. import java.awt.geom.NoninvertibleTransformException;
  16. import java.lang.ref.WeakReference;
  17. import sun.awt.image.IntegerInterleavedRaster;
  18. import sun.awt.image.ByteInterleavedRaster;
  19. abstract class TexturePaintContext implements PaintContext {
  20. public static ColorModel xrgbmodel =
  21. new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
  22. public static ColorModel argbmodel = ColorModel.getRGBdefault();
  23. ColorModel colorModel;
  24. int bWidth;
  25. int bHeight;
  26. int maxWidth;
  27. WritableRaster outRas;
  28. double xOrg;
  29. double yOrg;
  30. double incXAcross;
  31. double incYAcross;
  32. double incXDown;
  33. double incYDown;
  34. int colincx;
  35. int colincy;
  36. int colincxerr;
  37. int colincyerr;
  38. int rowincx;
  39. int rowincy;
  40. int rowincxerr;
  41. int rowincyerr;
  42. public static PaintContext getContext(BufferedImage bufImg,
  43. AffineTransform xform,
  44. RenderingHints hints,
  45. Rectangle devBounds) {
  46. WritableRaster raster = bufImg.getRaster();
  47. ColorModel cm = bufImg.getColorModel();
  48. int maxw = devBounds.width;
  49. Object val = hints.get(hints.KEY_INTERPOLATION);
  50. boolean filter =
  51. (val == null
  52. ? (hints.get(hints.KEY_RENDERING) == hints.VALUE_RENDER_QUALITY)
  53. : (val != hints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
  54. if (raster instanceof IntegerInterleavedRaster &&
  55. (!filter || isFilterableDCM(cm)))
  56. {
  57. IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster;
  58. if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) {
  59. return new Int(iir, cm, xform, maxw, filter);
  60. }
  61. } else if (raster instanceof ByteInterleavedRaster) {
  62. ByteInterleavedRaster bir = (ByteInterleavedRaster) raster;
  63. if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) {
  64. if (filter) {
  65. if (isFilterableICM(cm)) {
  66. return new ByteFilter(bir, cm, xform, maxw);
  67. }
  68. } else {
  69. return new Byte(bir, cm, xform, maxw);
  70. }
  71. }
  72. }
  73. return new Any(raster, cm, xform, maxw, filter);
  74. }
  75. public static boolean isFilterableICM(ColorModel cm) {
  76. if (cm instanceof IndexColorModel) {
  77. IndexColorModel icm = (IndexColorModel) cm;
  78. if (icm.getMapSize() <= 256) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. public static boolean isFilterableDCM(ColorModel cm) {
  85. if (cm instanceof DirectColorModel) {
  86. DirectColorModel dcm = (DirectColorModel) cm;
  87. return (isMaskOK(dcm.getAlphaMask(), true) &&
  88. isMaskOK(dcm.getRedMask(), false) &&
  89. isMaskOK(dcm.getGreenMask(), false) &&
  90. isMaskOK(dcm.getBlueMask(), false));
  91. }
  92. return false;
  93. }
  94. public static boolean isMaskOK(int mask, boolean canbezero) {
  95. if (canbezero && mask == 0) {
  96. return true;
  97. }
  98. return (mask == 0xff ||
  99. mask == 0xff00 ||
  100. mask == 0xff0000 ||
  101. mask == 0xff000000);
  102. }
  103. public static ColorModel getInternedColorModel(ColorModel cm) {
  104. if (xrgbmodel == cm || xrgbmodel.equals(cm)) {
  105. return xrgbmodel;
  106. }
  107. if (argbmodel == cm || argbmodel.equals(cm)) {
  108. return argbmodel;
  109. }
  110. return cm;
  111. }
  112. TexturePaintContext(ColorModel cm, AffineTransform xform,
  113. int bWidth, int bHeight, int maxw) {
  114. this.colorModel = getInternedColorModel(cm);
  115. this.bWidth = bWidth;
  116. this.bHeight = bHeight;
  117. this.maxWidth = maxw;
  118. try {
  119. xform = xform.createInverse();
  120. } catch (NoninvertibleTransformException e) {
  121. xform.setToScale(0, 0);
  122. }
  123. this.incXAcross = mod(xform.getScaleX(), bWidth);
  124. this.incYAcross = mod(xform.getShearY(), bHeight);
  125. this.incXDown = mod(xform.getShearX(), bWidth);
  126. this.incYDown = mod(xform.getScaleY(), bHeight);
  127. this.xOrg = xform.getTranslateX();
  128. this.yOrg = xform.getTranslateY();
  129. this.colincx = (int) incXAcross;
  130. this.colincy = (int) incYAcross;
  131. this.colincxerr = fractAsInt(incXAcross);
  132. this.colincyerr = fractAsInt(incYAcross);
  133. this.rowincx = (int) incXDown;
  134. this.rowincy = (int) incYDown;
  135. this.rowincxerr = fractAsInt(incXDown);
  136. this.rowincyerr = fractAsInt(incYDown);
  137. }
  138. static int fractAsInt(double d) {
  139. return (int) ((d % 1.0) * Integer.MAX_VALUE);
  140. }
  141. static double mod(double num, double den) {
  142. num = num % den;
  143. if (num < 0) {
  144. num += den;
  145. if (num >= den) {
  146. // For very small negative numerators, the answer might
  147. // be such a tiny bit less than den that the difference
  148. // is smaller than the mantissa of a double allows and
  149. // the result would then be rounded to den. If that is
  150. // the case then we map that number to 0 as the nearest
  151. // modulus representation.
  152. num = 0;
  153. }
  154. }
  155. return num;
  156. }
  157. /**
  158. * Release the resources allocated for the operation.
  159. */
  160. public void dispose() {
  161. dropRaster(colorModel, outRas);
  162. }
  163. /**
  164. * Return the ColorModel of the output.
  165. */
  166. public ColorModel getColorModel() {
  167. return colorModel;
  168. }
  169. /**
  170. * Return a Raster containing the colors generated for the graphics
  171. * operation.
  172. * @param x,y,w,h The area in device space for which colors are
  173. * generated.
  174. */
  175. public Raster getRaster(int x, int y, int w, int h) {
  176. if (outRas == null ||
  177. outRas.getWidth() < w ||
  178. outRas.getHeight() < h)
  179. {
  180. // If h==1, we will probably get lots of "scanline" rects
  181. outRas = makeRaster((h == 1 ? Math.max(w, maxWidth) : w), h);
  182. }
  183. double X = mod(xOrg + x * incXAcross + y * incXDown, bWidth);
  184. double Y = mod(yOrg + x * incYAcross + y * incYDown, bHeight);
  185. setRaster((int) X, (int) Y, fractAsInt(X), fractAsInt(Y),
  186. w, h, bWidth, bHeight,
  187. colincx, colincxerr,
  188. colincy, colincyerr,
  189. rowincx, rowincxerr,
  190. rowincy, rowincyerr);
  191. return outRas;
  192. }
  193. private static WeakReference xrgbRasRef;
  194. private static WeakReference argbRasRef;
  195. synchronized static WritableRaster makeRaster(ColorModel cm,
  196. Raster srcRas,
  197. int w, int h)
  198. {
  199. if (xrgbmodel == cm) {
  200. if (xrgbRasRef != null) {
  201. WritableRaster wr = (WritableRaster) xrgbRasRef.get();
  202. if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
  203. xrgbRasRef = null;
  204. return wr;
  205. }
  206. }
  207. // If we are going to cache this Raster, make it non-tiny
  208. if (w <= 32 && h <= 32) {
  209. w = h = 32;
  210. }
  211. } else if (argbmodel == cm) {
  212. if (argbRasRef != null) {
  213. WritableRaster wr = (WritableRaster) argbRasRef.get();
  214. if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
  215. argbRasRef = null;
  216. return wr;
  217. }
  218. }
  219. // If we are going to cache this Raster, make it non-tiny
  220. if (w <= 32 && h <= 32) {
  221. w = h = 32;
  222. }
  223. }
  224. if (srcRas != null) {
  225. return srcRas.createCompatibleWritableRaster(w, h);
  226. } else {
  227. return cm.createCompatibleWritableRaster(w, h);
  228. }
  229. }
  230. synchronized static void dropRaster(ColorModel cm, Raster outRas) {
  231. if (outRas == null) {
  232. return;
  233. }
  234. if (xrgbmodel == cm) {
  235. xrgbRasRef = new WeakReference(outRas);
  236. } else if (argbmodel == cm) {
  237. argbRasRef = new WeakReference(outRas);
  238. }
  239. }
  240. private static WeakReference byteRasRef;
  241. synchronized static WritableRaster makeByteRaster(Raster srcRas,
  242. int w, int h)
  243. {
  244. if (byteRasRef != null) {
  245. WritableRaster wr = (WritableRaster) byteRasRef.get();
  246. if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
  247. byteRasRef = null;
  248. return wr;
  249. }
  250. }
  251. // If we are going to cache this Raster, make it non-tiny
  252. if (w <= 32 && h <= 32) {
  253. w = h = 32;
  254. }
  255. return srcRas.createCompatibleWritableRaster(w, h);
  256. }
  257. synchronized static void dropByteRaster(Raster outRas) {
  258. if (outRas == null) {
  259. return;
  260. }
  261. byteRasRef = new WeakReference(outRas);
  262. }
  263. public abstract WritableRaster makeRaster(int w, int h);
  264. public abstract void setRaster(int x, int y, int xerr, int yerr,
  265. int w, int h, int bWidth, int bHeight,
  266. int colincx, int colincxerr,
  267. int colincy, int colincyerr,
  268. int rowincx, int rowincxerr,
  269. int rowincy, int rowincyerr);
  270. /*
  271. * Blends the four ARGB values in the rgbs array using the factors
  272. * described by xmul and ymul in the following ratio:
  273. *
  274. * rgbs[0] * (1-xmul) * (1-ymul) +
  275. * rgbs[1] * ( xmul) * (1-ymul) +
  276. * rgbs[2] * (1-xmul) * ( ymul) +
  277. * rgbs[3] * ( xmul) * ( ymul)
  278. *
  279. * xmul and ymul are integer values in the half-open range [0, 2^31)
  280. * where 0 == 0.0 and 2^31 == 1.0.
  281. *
  282. * Note that since the range is half-open, the values are always
  283. * logically less than 1.0. This makes sense because while choosing
  284. * pixels to blend, when the error values reach 1.0 we move to the
  285. * next pixel and reset them to 0.0.
  286. */
  287. public static int blend(int rgbs[], int xmul, int ymul) {
  288. // xmul/ymul are 31 bits wide, (0 => 2^31-1)
  289. // shift them to 12 bits wide, (0 => 2^12-1)
  290. xmul = (xmul >>> 19);
  291. ymul = (ymul >>> 19);
  292. int accumA, accumR, accumG, accumB;
  293. accumA = accumR = accumG = accumB = 0;
  294. for (int i = 0; i < 4; i++) {
  295. int rgb = rgbs[i];
  296. // The complement of the [xy]mul values (1-[xy]mul) can result
  297. // in new values in the range (1 => 2^12). Thus for any given
  298. // loop iteration, the values could be anywhere in (0 => 2^12).
  299. xmul = (1<<12) - xmul;
  300. if ((i & 1) == 0) {
  301. ymul = (1<<12) - ymul;
  302. }
  303. // xmul and ymul are each 12 bits (0 => 2^12)
  304. // factor is thus 24 bits (0 => 2^24)
  305. int factor = xmul * ymul;
  306. if (factor != 0) {
  307. // accum variables will accumulate 32 bits
  308. // bytes extracted from rgb fit in 8 bits (0 => 255)
  309. // byte * factor thus fits in 32 bits (0 => 255 * 2^24)
  310. accumA += (((rgb >>> 24) ) * factor);
  311. accumR += (((rgb >>> 16) & 0xff) * factor);
  312. accumG += (((rgb >>> 8) & 0xff) * factor);
  313. accumB += (((rgb ) & 0xff) * factor);
  314. }
  315. }
  316. return ((((accumA + (1<<23)) >>> 24) << 24) |
  317. (((accumR + (1<<23)) >>> 24) << 16) |
  318. (((accumG + (1<<23)) >>> 24) << 8) |
  319. (((accumB + (1<<23)) >>> 24) ));
  320. }
  321. static class Int extends TexturePaintContext {
  322. IntegerInterleavedRaster srcRas;
  323. int inData[];
  324. int inOff;
  325. int inSpan;
  326. int outData[];
  327. int outOff;
  328. int outSpan;
  329. boolean filter;
  330. public Int(IntegerInterleavedRaster srcRas, ColorModel cm,
  331. AffineTransform xform, int maxw, boolean filter)
  332. {
  333. super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
  334. this.srcRas = srcRas;
  335. this.inData = srcRas.getDataStorage();
  336. this.inSpan = srcRas.getScanlineStride();
  337. this.inOff = srcRas.getDataOffset(0);
  338. this.filter = filter;
  339. }
  340. public WritableRaster makeRaster(int w, int h) {
  341. WritableRaster ras = makeRaster(colorModel, srcRas, w, h);
  342. IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras;
  343. outData = iiRas.getDataStorage();
  344. outSpan = iiRas.getScanlineStride();
  345. outOff = iiRas.getDataOffset(0);
  346. return ras;
  347. }
  348. public void setRaster(int x, int y, int xerr, int yerr,
  349. int w, int h, int bWidth, int bHeight,
  350. int colincx, int colincxerr,
  351. int colincy, int colincyerr,
  352. int rowincx, int rowincxerr,
  353. int rowincy, int rowincyerr) {
  354. int[] inData = this.inData;
  355. int[] outData = this.outData;
  356. int out = outOff;
  357. int inSpan = this.inSpan;
  358. int inOff = this.inOff;
  359. int outSpan = this.outSpan;
  360. boolean filter = this.filter;
  361. boolean normalx = (colincx == 1 && colincxerr == 0 &&
  362. colincy == 0 && colincyerr == 0) && !filter;
  363. int rowx = x;
  364. int rowy = y;
  365. int rowxerr = xerr;
  366. int rowyerr = yerr;
  367. if (normalx) {
  368. outSpan -= w;
  369. }
  370. int rgbs[] = filter ? new int[4] : null;
  371. for (int j = 0; j < h; j++) {
  372. if (normalx) {
  373. int in = inOff + rowy * inSpan + bWidth;
  374. x = bWidth - rowx;
  375. out += w;
  376. if (bWidth >= 32) {
  377. int i = w;
  378. while (i > 0) {
  379. int copyw = (i < x) ? i : x;
  380. System.arraycopy(inData, in - x,
  381. outData, out - i,
  382. copyw);
  383. i -= copyw;
  384. if ((x -= copyw) == 0) {
  385. x = bWidth;
  386. }
  387. }
  388. } else {
  389. for (int i = w; i > 0; i--) {
  390. outData[out - i] = inData[in - x];
  391. if (--x == 0) {
  392. x = bWidth;
  393. }
  394. }
  395. }
  396. } else {
  397. x = rowx;
  398. y = rowy;
  399. xerr = rowxerr;
  400. yerr = rowyerr;
  401. for (int i = 0; i < w; i++) {
  402. if (filter) {
  403. int nextx, nexty;
  404. if ((nextx = x + 1) >= bWidth) {
  405. nextx = 0;
  406. }
  407. if ((nexty = y + 1) >= bHeight) {
  408. nexty = 0;
  409. }
  410. rgbs[0] = inData[inOff + y * inSpan + x];
  411. rgbs[1] = inData[inOff + y * inSpan + nextx];
  412. rgbs[2] = inData[inOff + nexty * inSpan + x];
  413. rgbs[3] = inData[inOff + nexty * inSpan + nextx];
  414. outData[out + i] =
  415. TexturePaintContext.blend(rgbs, xerr, yerr);
  416. } else {
  417. outData[out + i] = inData[inOff + y * inSpan + x];
  418. }
  419. if ((xerr += colincxerr) < 0) {
  420. xerr &= Integer.MAX_VALUE;
  421. x++;
  422. }
  423. if ((x += colincx) >= bWidth) {
  424. x -= bWidth;
  425. }
  426. if ((yerr += colincyerr) < 0) {
  427. yerr &= Integer.MAX_VALUE;
  428. y++;
  429. }
  430. if ((y += colincy) >= bHeight) {
  431. y -= bHeight;
  432. }
  433. }
  434. }
  435. if ((rowxerr += rowincxerr) < 0) {
  436. rowxerr &= Integer.MAX_VALUE;
  437. rowx++;
  438. }
  439. if ((rowx += rowincx) >= bWidth) {
  440. rowx -= bWidth;
  441. }
  442. if ((rowyerr += rowincyerr) < 0) {
  443. rowyerr &= Integer.MAX_VALUE;
  444. rowy++;
  445. }
  446. if ((rowy += rowincy) >= bHeight) {
  447. rowy -= bHeight;
  448. }
  449. out += outSpan;
  450. }
  451. }
  452. }
  453. static class Byte extends TexturePaintContext {
  454. ByteInterleavedRaster srcRas;
  455. byte inData[];
  456. int inOff;
  457. int inSpan;
  458. byte outData[];
  459. int outOff;
  460. int outSpan;
  461. public Byte(ByteInterleavedRaster srcRas, ColorModel cm,
  462. AffineTransform xform, int maxw)
  463. {
  464. super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
  465. this.srcRas = srcRas;
  466. this.inData = srcRas.getDataStorage();
  467. this.inSpan = srcRas.getScanlineStride();
  468. this.inOff = srcRas.getDataOffset(0);
  469. }
  470. public WritableRaster makeRaster(int w, int h) {
  471. WritableRaster ras = makeByteRaster(srcRas, w, h);
  472. ByteInterleavedRaster biRas = (ByteInterleavedRaster) ras;
  473. outData = biRas.getDataStorage();
  474. outSpan = biRas.getScanlineStride();
  475. outOff = biRas.getDataOffset(0);
  476. return ras;
  477. }
  478. public void dispose() {
  479. dropByteRaster(outRas);
  480. }
  481. public void setRaster(int x, int y, int xerr, int yerr,
  482. int w, int h, int bWidth, int bHeight,
  483. int colincx, int colincxerr,
  484. int colincy, int colincyerr,
  485. int rowincx, int rowincxerr,
  486. int rowincy, int rowincyerr) {
  487. byte[] inData = this.inData;
  488. byte[] outData = this.outData;
  489. int out = outOff;
  490. int inSpan = this.inSpan;
  491. int inOff = this.inOff;
  492. int outSpan = this.outSpan;
  493. boolean normalx = (colincx == 1 && colincxerr == 0 &&
  494. colincy == 0 && colincyerr == 0);
  495. int rowx = x;
  496. int rowy = y;
  497. int rowxerr = xerr;
  498. int rowyerr = yerr;
  499. if (normalx) {
  500. outSpan -= w;
  501. }
  502. for (int j = 0; j < h; j++) {
  503. if (normalx) {
  504. int in = inOff + rowy * inSpan + bWidth;
  505. x = bWidth - rowx;
  506. out += w;
  507. if (bWidth >= 32) {
  508. int i = w;
  509. while (i > 0) {
  510. int copyw = (i < x) ? i : x;
  511. System.arraycopy(inData, in - x,
  512. outData, out - i,
  513. copyw);
  514. i -= copyw;
  515. if ((x -= copyw) == 0) {
  516. x = bWidth;
  517. }
  518. }
  519. } else {
  520. for (int i = w; i > 0; i--) {
  521. outData[out - i] = inData[in - x];
  522. if (--x == 0) {
  523. x = bWidth;
  524. }
  525. }
  526. }
  527. } else {
  528. x = rowx;
  529. y = rowy;
  530. xerr = rowxerr;
  531. yerr = rowyerr;
  532. for (int i = 0; i < w; i++) {
  533. outData[out + i] = inData[inOff + y * inSpan + x];
  534. if ((xerr += colincxerr) < 0) {
  535. xerr &= Integer.MAX_VALUE;
  536. x++;
  537. }
  538. if ((x += colincx) >= bWidth) {
  539. x -= bWidth;
  540. }
  541. if ((yerr += colincyerr) < 0) {
  542. yerr &= Integer.MAX_VALUE;
  543. y++;
  544. }
  545. if ((y += colincy) >= bHeight) {
  546. y -= bHeight;
  547. }
  548. }
  549. }
  550. if ((rowxerr += rowincxerr) < 0) {
  551. rowxerr &= Integer.MAX_VALUE;
  552. rowx++;
  553. }
  554. if ((rowx += rowincx) >= bWidth) {
  555. rowx -= bWidth;
  556. }
  557. if ((rowyerr += rowincyerr) < 0) {
  558. rowyerr &= Integer.MAX_VALUE;
  559. rowy++;
  560. }
  561. if ((rowy += rowincy) >= bHeight) {
  562. rowy -= bHeight;
  563. }
  564. out += outSpan;
  565. }
  566. }
  567. }
  568. static class ByteFilter extends TexturePaintContext {
  569. ByteInterleavedRaster srcRas;
  570. int inPalette[];
  571. byte inData[];
  572. int inOff;
  573. int inSpan;
  574. int outData[];
  575. int outOff;
  576. int outSpan;
  577. public ByteFilter(ByteInterleavedRaster srcRas, ColorModel cm,
  578. AffineTransform xform, int maxw)
  579. {
  580. super((cm.getTransparency() == Transparency.OPAQUE
  581. ? xrgbmodel : argbmodel),
  582. xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
  583. this.inPalette = new int[256];
  584. ((IndexColorModel) cm).getRGBs(this.inPalette);
  585. this.srcRas = srcRas;
  586. this.inData = srcRas.getDataStorage();
  587. this.inSpan = srcRas.getScanlineStride();
  588. this.inOff = srcRas.getDataOffset(0);
  589. }
  590. public WritableRaster makeRaster(int w, int h) {
  591. // Note that we do not pass srcRas to makeRaster since it
  592. // is a Byte Raster and this colorModel needs an Int Raster
  593. WritableRaster ras = makeRaster(colorModel, null, w, h);
  594. IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras;
  595. outData = iiRas.getDataStorage();
  596. outSpan = iiRas.getScanlineStride();
  597. outOff = iiRas.getDataOffset(0);
  598. return ras;
  599. }
  600. public void setRaster(int x, int y, int xerr, int yerr,
  601. int w, int h, int bWidth, int bHeight,
  602. int colincx, int colincxerr,
  603. int colincy, int colincyerr,
  604. int rowincx, int rowincxerr,
  605. int rowincy, int rowincyerr) {
  606. byte[] inData = this.inData;
  607. int[] outData = this.outData;
  608. int out = outOff;
  609. int inSpan = this.inSpan;
  610. int inOff = this.inOff;
  611. int outSpan = this.outSpan;
  612. int rowx = x;
  613. int rowy = y;
  614. int rowxerr = xerr;
  615. int rowyerr = yerr;
  616. int rgbs[] = new int[4];
  617. for (int j = 0; j < h; j++) {
  618. x = rowx;
  619. y = rowy;
  620. xerr = rowxerr;
  621. yerr = rowyerr;
  622. for (int i = 0; i < w; i++) {
  623. int nextx, nexty;
  624. if ((nextx = x + 1) >= bWidth) {
  625. nextx = 0;
  626. }
  627. if ((nexty = y + 1) >= bHeight) {
  628. nexty = 0;
  629. }
  630. rgbs[0] = inPalette[0xff & inData[inOff + x +
  631. inSpan * y]];
  632. rgbs[1] = inPalette[0xff & inData[inOff + nextx +
  633. inSpan * y]];
  634. rgbs[2] = inPalette[0xff & inData[inOff + x +
  635. inSpan * nexty]];
  636. rgbs[3] = inPalette[0xff & inData[inOff + nextx +
  637. inSpan * nexty]];
  638. outData[out + i] =
  639. TexturePaintContext.blend(rgbs, xerr, yerr);
  640. if ((xerr += colincxerr) < 0) {
  641. xerr &= Integer.MAX_VALUE;
  642. x++;
  643. }
  644. if ((x += colincx) >= bWidth) {
  645. x -= bWidth;
  646. }
  647. if ((yerr += colincyerr) < 0) {
  648. yerr &= Integer.MAX_VALUE;
  649. y++;
  650. }
  651. if ((y += colincy) >= bHeight) {
  652. y -= bHeight;
  653. }
  654. }
  655. if ((rowxerr += rowincxerr) < 0) {
  656. rowxerr &= Integer.MAX_VALUE;
  657. rowx++;
  658. }
  659. if ((rowx += rowincx) >= bWidth) {
  660. rowx -= bWidth;
  661. }
  662. if ((rowyerr += rowincyerr) < 0) {
  663. rowyerr &= Integer.MAX_VALUE;
  664. rowy++;
  665. }
  666. if ((rowy += rowincy) >= bHeight) {
  667. rowy -= bHeight;
  668. }
  669. out += outSpan;
  670. }
  671. }
  672. }
  673. static class Any extends TexturePaintContext {
  674. WritableRaster srcRas;
  675. boolean filter;
  676. public Any(WritableRaster srcRas, ColorModel cm,
  677. AffineTransform xform, int maxw, boolean filter)
  678. {
  679. super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
  680. this.srcRas = srcRas;
  681. this.filter = filter;
  682. }
  683. public WritableRaster makeRaster(int w, int h) {
  684. return makeRaster(colorModel, srcRas, w, h);
  685. }
  686. public void setRaster(int x, int y, int xerr, int yerr,
  687. int w, int h, int bWidth, int bHeight,
  688. int colincx, int colincxerr,
  689. int colincy, int colincyerr,
  690. int rowincx, int rowincxerr,
  691. int rowincy, int rowincyerr) {
  692. Object data = null;
  693. int rowx = x;
  694. int rowy = y;
  695. int rowxerr = xerr;
  696. int rowyerr = yerr;
  697. WritableRaster srcRas = this.srcRas;
  698. WritableRaster outRas = this.outRas;
  699. int rgbs[] = filter ? new int[4] : null;
  700. for (int j = 0; j < h; j++) {
  701. x = rowx;
  702. y = rowy;
  703. xerr = rowxerr;
  704. yerr = rowyerr;
  705. for (int i = 0; i < w; i++) {
  706. data = srcRas.getDataElements(x, y, data);
  707. if (filter) {
  708. int nextx, nexty;
  709. if ((nextx = x + 1) >= bWidth) {
  710. nextx = 0;
  711. }
  712. if ((nexty = y + 1) >= bHeight) {
  713. nexty = 0;
  714. }
  715. rgbs[0] = colorModel.getRGB(data);
  716. data = srcRas.getDataElements(nextx, y, data);
  717. rgbs[1] = colorModel.getRGB(data);
  718. data = srcRas.getDataElements(x, nexty, data);
  719. rgbs[2] = colorModel.getRGB(data);
  720. data = srcRas.getDataElements(nextx, nexty, data);
  721. rgbs[3] = colorModel.getRGB(data);
  722. int rgb =
  723. TexturePaintContext.blend(rgbs, xerr, yerr);
  724. data = colorModel.getDataElements(rgb, data);
  725. }
  726. outRas.setDataElements(i, j, data);
  727. if ((xerr += colincxerr) < 0) {
  728. xerr &= Integer.MAX_VALUE;
  729. x++;
  730. }
  731. if ((x += colincx) >= bWidth) {
  732. x -= bWidth;
  733. }
  734. if ((yerr += colincyerr) < 0) {
  735. yerr &= Integer.MAX_VALUE;
  736. y++;
  737. }
  738. if ((y += colincy) >= bHeight) {
  739. y -= bHeight;
  740. }
  741. }
  742. if ((rowxerr += rowincxerr) < 0) {
  743. rowxerr &= Integer.MAX_VALUE;
  744. rowx++;
  745. }
  746. if ((rowx += rowincx) >= bWidth) {
  747. rowx -= bWidth;
  748. }
  749. if ((rowyerr += rowincyerr) < 0) {
  750. rowyerr &= Integer.MAX_VALUE;
  751. rowy++;
  752. }
  753. if ((rowy += rowincy) >= bHeight) {
  754. rowy -= bHeight;
  755. }
  756. }
  757. }
  758. }
  759. }