KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > TexturePaintContext


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
8 package java.awt;
9
10 import java.awt.image.BufferedImage JavaDoc;
11 import java.awt.image.Raster JavaDoc;
12 import java.awt.image.WritableRaster JavaDoc;
13 import java.awt.image.ColorModel JavaDoc;
14 import java.awt.image.DirectColorModel JavaDoc;
15 import java.awt.image.IndexColorModel JavaDoc;
16 import java.awt.geom.AffineTransform JavaDoc;
17 import java.awt.geom.NoninvertibleTransformException JavaDoc;
18 import java.lang.ref.WeakReference JavaDoc;
19 import sun.awt.image.IntegerInterleavedRaster;
20 import sun.awt.image.ByteInterleavedRaster;
21
22 abstract class TexturePaintContext implements PaintContext JavaDoc {
23     public static ColorModel JavaDoc xrgbmodel =
24     new DirectColorModel JavaDoc(24, 0xff0000, 0xff00, 0xff);
25     public static ColorModel JavaDoc argbmodel = ColorModel.getRGBdefault();
26
27     ColorModel JavaDoc colorModel;
28     int bWidth;
29     int bHeight;
30     int maxWidth;
31
32     WritableRaster JavaDoc outRas;
33
34     double xOrg;
35     double yOrg;
36     double incXAcross;
37     double incYAcross;
38     double incXDown;
39     double incYDown;
40
41     int colincx;
42     int colincy;
43     int colincxerr;
44     int colincyerr;
45     int rowincx;
46     int rowincy;
47     int rowincxerr;
48     int rowincyerr;
49
50     public static PaintContext JavaDoc getContext(BufferedImage JavaDoc bufImg,
51                       AffineTransform JavaDoc xform,
52                       RenderingHints JavaDoc hints,
53                       Rectangle JavaDoc devBounds) {
54         WritableRaster JavaDoc raster = bufImg.getRaster();
55     ColorModel JavaDoc cm = bufImg.getColorModel();
56     int maxw = devBounds.width;
57     Object JavaDoc val = hints.get(hints.KEY_INTERPOLATION);
58     boolean filter =
59         (val == null
60          ? (hints.get(hints.KEY_RENDERING) == hints.VALUE_RENDER_QUALITY)
61          : (val != hints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
62     if (raster instanceof IntegerInterleavedRaster &&
63         (!filter || isFilterableDCM(cm)))
64     {
65         IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster;
66         if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) {
67         return new Int(iir, cm, xform, maxw, filter);
68         }
69     } else if (raster instanceof ByteInterleavedRaster) {
70         ByteInterleavedRaster bir = (ByteInterleavedRaster) raster;
71         if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) {
72         if (filter) {
73             if (isFilterableICM(cm)) {
74             return new ByteFilter(bir, cm, xform, maxw);
75             }
76         } else {
77             return new Byte JavaDoc(bir, cm, xform, maxw);
78         }
79         }
80     }
81     return new Any(raster, cm, xform, maxw, filter);
82     }
83
84     public static boolean isFilterableICM(ColorModel JavaDoc cm) {
85     if (cm instanceof IndexColorModel JavaDoc) {
86         IndexColorModel JavaDoc icm = (IndexColorModel JavaDoc) cm;
87         if (icm.getMapSize() <= 256) {
88         return true;
89         }
90     }
91     return false;
92     }
93
94     public static boolean isFilterableDCM(ColorModel JavaDoc cm) {
95     if (cm instanceof DirectColorModel JavaDoc) {
96         DirectColorModel JavaDoc dcm = (DirectColorModel JavaDoc) cm;
97         return (isMaskOK(dcm.getAlphaMask(), true) &&
98             isMaskOK(dcm.getRedMask(), false) &&
99             isMaskOK(dcm.getGreenMask(), false) &&
100             isMaskOK(dcm.getBlueMask(), false));
101     }
102     return false;
103     }
104
105     public static boolean isMaskOK(int mask, boolean canbezero) {
106     if (canbezero && mask == 0) {
107         return true;
108     }
109     return (mask == 0xff ||
110         mask == 0xff00 ||
111         mask == 0xff0000 ||
112         mask == 0xff000000);
113     }
114
115     public static ColorModel JavaDoc getInternedColorModel(ColorModel JavaDoc cm) {
116     if (xrgbmodel == cm || xrgbmodel.equals(cm)) {
117         return xrgbmodel;
118     }
119     if (argbmodel == cm || argbmodel.equals(cm)) {
120         return argbmodel;
121     }
122     return cm;
123     }
124
125     TexturePaintContext(ColorModel JavaDoc cm, AffineTransform JavaDoc xform,
126             int bWidth, int bHeight, int maxw) {
127     this.colorModel = getInternedColorModel(cm);
128     this.bWidth = bWidth;
129     this.bHeight = bHeight;
130     this.maxWidth = maxw;
131
132     try {
133         xform = xform.createInverse();
134         } catch (NoninvertibleTransformException JavaDoc e) {
135         xform.setToScale(0, 0);
136     }
137     this.incXAcross = mod(xform.getScaleX(), bWidth);
138     this.incYAcross = mod(xform.getShearY(), bHeight);
139     this.incXDown = mod(xform.getShearX(), bWidth);
140     this.incYDown = mod(xform.getScaleY(), bHeight);
141         this.xOrg = xform.getTranslateX();
142         this.yOrg = xform.getTranslateY();
143     this.colincx = (int) incXAcross;
144     this.colincy = (int) incYAcross;
145     this.colincxerr = fractAsInt(incXAcross);
146     this.colincyerr = fractAsInt(incYAcross);
147     this.rowincx = (int) incXDown;
148     this.rowincy = (int) incYDown;
149     this.rowincxerr = fractAsInt(incXDown);
150     this.rowincyerr = fractAsInt(incYDown);
151
152     }
153
154     static int fractAsInt(double d) {
155     return (int) ((d % 1.0) * Integer.MAX_VALUE);
156     }
157
158     static double mod(double num, double den) {
159     num = num % den;
160     if (num < 0) {
161         num += den;
162         if (num >= den) {
163         // For very small negative numerators, the answer might
164
// be such a tiny bit less than den that the difference
165
// is smaller than the mantissa of a double allows and
166
// the result would then be rounded to den. If that is
167
// the case then we map that number to 0 as the nearest
168
// modulus representation.
169
num = 0;
170         }
171     }
172     return num;
173     }
174     
175     /**
176      * Release the resources allocated for the operation.
177      */

178     public void dispose() {
179     dropRaster(colorModel, outRas);
180     }
181
182     /**
183      * Return the ColorModel of the output.
184      */

185     public ColorModel JavaDoc getColorModel() {
186         return colorModel;
187     }
188
189     /**
190      * Return a Raster containing the colors generated for the graphics
191      * operation.
192      * @param x,y,w,h The area in device space for which colors are
193      * generated.
194      */

195     public Raster JavaDoc getRaster(int x, int y, int w, int h) {
196     if (outRas == null ||
197         outRas.getWidth() < w ||
198         outRas.getHeight() < h)
199     {
200         // If h==1, we will probably get lots of "scanline" rects
201
outRas = makeRaster((h == 1 ? Math.max(w, maxWidth) : w), h);
202     }
203     double X = mod(xOrg + x * incXAcross + y * incXDown, bWidth);
204     double Y = mod(yOrg + x * incYAcross + y * incYDown, bHeight);
205
206     setRaster((int) X, (int) Y, fractAsInt(X), fractAsInt(Y),
207           w, h, bWidth, bHeight,
208           colincx, colincxerr,
209           colincy, colincyerr,
210           rowincx, rowincxerr,
211           rowincy, rowincyerr);
212
213     return outRas;
214     }
215
216     private static WeakReference JavaDoc xrgbRasRef;
217     private static WeakReference JavaDoc argbRasRef;
218
219     synchronized static WritableRaster JavaDoc makeRaster(ColorModel JavaDoc cm,
220                           Raster JavaDoc srcRas,
221                           int w, int h)
222     {
223     if (xrgbmodel == cm) {
224         if (xrgbRasRef != null) {
225         WritableRaster JavaDoc wr = (WritableRaster JavaDoc) xrgbRasRef.get();
226         if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
227             xrgbRasRef = null;
228             return wr;
229         }
230         }
231         // If we are going to cache this Raster, make it non-tiny
232
if (w <= 32 && h <= 32) {
233         w = h = 32;
234         }
235     } else if (argbmodel == cm) {
236         if (argbRasRef != null) {
237         WritableRaster JavaDoc wr = (WritableRaster JavaDoc) argbRasRef.get();
238         if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
239             argbRasRef = null;
240             return wr;
241         }
242         }
243         // If we are going to cache this Raster, make it non-tiny
244
if (w <= 32 && h <= 32) {
245         w = h = 32;
246         }
247     }
248     if (srcRas != null) {
249         return srcRas.createCompatibleWritableRaster(w, h);
250     } else {
251         return cm.createCompatibleWritableRaster(w, h);
252     }
253     }
254
255     synchronized static void dropRaster(ColorModel JavaDoc cm, Raster JavaDoc outRas) {
256     if (outRas == null) {
257         return;
258     }
259     if (xrgbmodel == cm) {
260         xrgbRasRef = new WeakReference JavaDoc(outRas);
261     } else if (argbmodel == cm) {
262         argbRasRef = new WeakReference JavaDoc(outRas);
263     }
264     }
265
266     private static WeakReference JavaDoc byteRasRef;
267
268     synchronized static WritableRaster JavaDoc makeByteRaster(Raster JavaDoc srcRas,
269                               int w, int h)
270     {
271     if (byteRasRef != null) {
272         WritableRaster JavaDoc wr = (WritableRaster JavaDoc) byteRasRef.get();
273         if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
274         byteRasRef = null;
275         return wr;
276         }
277     }
278     // If we are going to cache this Raster, make it non-tiny
279
if (w <= 32 && h <= 32) {
280         w = h = 32;
281     }
282     return srcRas.createCompatibleWritableRaster(w, h);
283     }
284
285     synchronized static void dropByteRaster(Raster JavaDoc outRas) {
286     if (outRas == null) {
287         return;
288     }
289     byteRasRef = new WeakReference JavaDoc(outRas);
290     }
291
292     public abstract WritableRaster JavaDoc makeRaster(int w, int h);
293     public abstract void setRaster(int x, int y, int xerr, int yerr,
294                    int w, int h, int bWidth, int bHeight,
295                    int colincx, int colincxerr,
296                    int colincy, int colincyerr,
297                    int rowincx, int rowincxerr,
298                    int rowincy, int rowincyerr);
299
300     /*
301      * Blends the four ARGB values in the rgbs array using the factors
302      * described by xmul and ymul in the following ratio:
303      *
304      * rgbs[0] * (1-xmul) * (1-ymul) +
305      * rgbs[1] * ( xmul) * (1-ymul) +
306      * rgbs[2] * (1-xmul) * ( ymul) +
307      * rgbs[3] * ( xmul) * ( ymul)
308      *
309      * xmul and ymul are integer values in the half-open range [0, 2^31)
310      * where 0 == 0.0 and 2^31 == 1.0.
311      *
312      * Note that since the range is half-open, the values are always
313      * logically less than 1.0. This makes sense because while choosing
314      * pixels to blend, when the error values reach 1.0 we move to the
315      * next pixel and reset them to 0.0.
316      */

317     public static int blend(int rgbs[], int xmul, int ymul) {
318     // xmul/ymul are 31 bits wide, (0 => 2^31-1)
319
// shift them to 12 bits wide, (0 => 2^12-1)
320
xmul = (xmul >>> 19);
321     ymul = (ymul >>> 19);
322     int accumA, accumR, accumG, accumB;
323     accumA = accumR = accumG = accumB = 0;
324     for (int i = 0; i < 4; i++) {
325         int rgb = rgbs[i];
326         // The complement of the [xy]mul values (1-[xy]mul) can result
327
// in new values in the range (1 => 2^12). Thus for any given
328
// loop iteration, the values could be anywhere in (0 => 2^12).
329
xmul = (1<<12) - xmul;
330         if ((i & 1) == 0) {
331         ymul = (1<<12) - ymul;
332         }
333         // xmul and ymul are each 12 bits (0 => 2^12)
334
// factor is thus 24 bits (0 => 2^24)
335
int factor = xmul * ymul;
336         if (factor != 0) {
337         // accum variables will accumulate 32 bits
338
// bytes extracted from rgb fit in 8 bits (0 => 255)
339
// byte * factor thus fits in 32 bits (0 => 255 * 2^24)
340
accumA += (((rgb >>> 24) ) * factor);
341         accumR += (((rgb >>> 16) & 0xff) * factor);
342         accumG += (((rgb >>> 8) & 0xff) * factor);
343         accumB += (((rgb ) & 0xff) * factor);
344         }
345     }
346     return ((((accumA + (1<<23)) >>> 24) << 24) |
347         (((accumR + (1<<23)) >>> 24) << 16) |
348         (((accumG + (1<<23)) >>> 24) << 8) |
349         (((accumB + (1<<23)) >>> 24) ));
350     }
351
352     static class Int extends TexturePaintContext JavaDoc {
353     IntegerInterleavedRaster srcRas;
354     int inData[];
355     int inOff;
356     int inSpan;
357     int outData[];
358     int outOff;
359     int outSpan;
360     boolean filter;
361
362     public Int(IntegerInterleavedRaster srcRas, ColorModel JavaDoc cm,
363            AffineTransform JavaDoc xform, int maxw, boolean filter)
364     {
365         super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
366         this.srcRas = srcRas;
367         this.inData = srcRas.getDataStorage();
368         this.inSpan = srcRas.getScanlineStride();
369         this.inOff = srcRas.getDataOffset(0);
370         this.filter = filter;
371     }
372
373     public WritableRaster JavaDoc makeRaster(int w, int h) {
374         WritableRaster JavaDoc ras = makeRaster(colorModel, srcRas, w, h);
375         IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras;
376         outData = iiRas.getDataStorage();
377         outSpan = iiRas.getScanlineStride();
378         outOff = iiRas.getDataOffset(0);
379         return ras;
380     }
381
382     public void setRaster(int x, int y, int xerr, int yerr,
383                   int w, int h, int bWidth, int bHeight,
384                   int colincx, int colincxerr,
385                   int colincy, int colincyerr,
386                   int rowincx, int rowincxerr,
387                   int rowincy, int rowincyerr) {
388         int[] inData = this.inData;
389         int[] outData = this.outData;
390         int out = outOff;
391         int inSpan = this.inSpan;
392         int inOff = this.inOff;
393         int outSpan = this.outSpan;
394         boolean filter = this.filter;
395         boolean normalx = (colincx == 1 && colincxerr == 0 &&
396                    colincy == 0 && colincyerr == 0) && !filter;
397         int rowx = x;
398         int rowy = y;
399         int rowxerr = xerr;
400         int rowyerr = yerr;
401         if (normalx) {
402         outSpan -= w;
403         }
404         int rgbs[] = filter ? new int[4] : null;
405         for (int j = 0; j < h; j++) {
406         if (normalx) {
407             int in = inOff + rowy * inSpan + bWidth;
408             x = bWidth - rowx;
409             out += w;
410             if (bWidth >= 32) {
411             int i = w;
412             while (i > 0) {
413                 int copyw = (i < x) ? i : x;
414                 System.arraycopy(inData, in - x,
415                          outData, out - i,
416                          copyw);
417                 i -= copyw;
418                 if ((x -= copyw) == 0) {
419                 x = bWidth;
420                 }
421             }
422             } else {
423             for (int i = w; i > 0; i--) {
424                 outData[out - i] = inData[in - x];
425                 if (--x == 0) {
426                 x = bWidth;
427                 }
428             }
429             }
430         } else {
431             x = rowx;
432             y = rowy;
433             xerr = rowxerr;
434             yerr = rowyerr;
435             for (int i = 0; i < w; i++) {
436             if (filter) {
437                 int nextx, nexty;
438                 if ((nextx = x + 1) >= bWidth) {
439                 nextx = 0;
440                 }
441                 if ((nexty = y + 1) >= bHeight) {
442                 nexty = 0;
443                 }
444                 rgbs[0] = inData[inOff + y * inSpan + x];
445                 rgbs[1] = inData[inOff + y * inSpan + nextx];
446                 rgbs[2] = inData[inOff + nexty * inSpan + x];
447                 rgbs[3] = inData[inOff + nexty * inSpan + nextx];
448                 outData[out + i] =
449                 TexturePaintContext.blend(rgbs, xerr, yerr);
450             } else {
451                 outData[out + i] = inData[inOff + y * inSpan + x];
452             }
453             if ((xerr += colincxerr) < 0) {
454                 xerr &= Integer.MAX_VALUE;
455                 x++;
456             }
457             if ((x += colincx) >= bWidth) {
458                 x -= bWidth;
459             }
460             if ((yerr += colincyerr) < 0) {
461                 yerr &= Integer.MAX_VALUE;
462                 y++;
463             }
464             if ((y += colincy) >= bHeight) {
465                 y -= bHeight;
466             }
467             }
468         }
469         if ((rowxerr += rowincxerr) < 0) {
470             rowxerr &= Integer.MAX_VALUE;
471             rowx++;
472         }
473         if ((rowx += rowincx) >= bWidth) {
474             rowx -= bWidth;
475         }
476         if ((rowyerr += rowincyerr) < 0) {
477             rowyerr &= Integer.MAX_VALUE;
478             rowy++;
479         }
480         if ((rowy += rowincy) >= bHeight) {
481             rowy -= bHeight;
482         }
483         out += outSpan;
484         }
485     }
486     }
487
488     static class Byte extends TexturePaintContext JavaDoc {
489     ByteInterleavedRaster srcRas;
490     byte inData[];
491     int inOff;
492     int inSpan;
493     byte outData[];
494     int outOff;
495     int outSpan;
496
497     public Byte(ByteInterleavedRaster srcRas, ColorModel JavaDoc cm,
498             AffineTransform JavaDoc xform, int maxw)
499     {
500         super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
501         this.srcRas = srcRas;
502         this.inData = srcRas.getDataStorage();
503         this.inSpan = srcRas.getScanlineStride();
504         this.inOff = srcRas.getDataOffset(0);
505     }
506
507     public WritableRaster JavaDoc makeRaster(int w, int h) {
508         WritableRaster JavaDoc ras = makeByteRaster(srcRas, w, h);
509         ByteInterleavedRaster biRas = (ByteInterleavedRaster) ras;
510         outData = biRas.getDataStorage();
511         outSpan = biRas.getScanlineStride();
512         outOff = biRas.getDataOffset(0);
513         return ras;
514     }
515
516     public void dispose() {
517         dropByteRaster(outRas);
518     }
519
520     public void setRaster(int x, int y, int xerr, int yerr,
521                   int w, int h, int bWidth, int bHeight,
522                   int colincx, int colincxerr,
523                   int colincy, int colincyerr,
524                   int rowincx, int rowincxerr,
525                   int rowincy, int rowincyerr) {
526         byte[] inData = this.inData;
527         byte[] outData = this.outData;
528         int out = outOff;
529         int inSpan = this.inSpan;
530         int inOff = this.inOff;
531         int outSpan = this.outSpan;
532         boolean normalx = (colincx == 1 && colincxerr == 0 &&
533                    colincy == 0 && colincyerr == 0);
534         int rowx = x;
535         int rowy = y;
536         int rowxerr = xerr;
537         int rowyerr = yerr;
538         if (normalx) {
539         outSpan -= w;
540         }
541         for (int j = 0; j < h; j++) {
542         if (normalx) {
543             int in = inOff + rowy * inSpan + bWidth;
544             x = bWidth - rowx;
545             out += w;
546             if (bWidth >= 32) {
547             int i = w;
548             while (i > 0) {
549                 int copyw = (i < x) ? i : x;
550                 System.arraycopy(inData, in - x,
551                          outData, out - i,
552                          copyw);
553                 i -= copyw;
554                 if ((x -= copyw) == 0) {
555                 x = bWidth;
556                 }
557             }
558             } else {
559             for (int i = w; i > 0; i--) {
560                 outData[out - i] = inData[in - x];
561                 if (--x == 0) {
562                 x = bWidth;
563                 }
564             }
565             }
566         } else {
567             x = rowx;
568             y = rowy;
569             xerr = rowxerr;
570             yerr = rowyerr;
571             for (int i = 0; i < w; i++) {
572             outData[out + i] = inData[inOff + y * inSpan + x];
573             if ((xerr += colincxerr) < 0) {
574                 xerr &= Integer.MAX_VALUE;
575                 x++;
576             }
577             if ((x += colincx) >= bWidth) {
578                 x -= bWidth;
579             }
580             if ((yerr += colincyerr) < 0) {
581                 yerr &= Integer.MAX_VALUE;
582                 y++;
583             }
584             if ((y += colincy) >= bHeight) {
585                 y -= bHeight;
586             }
587             }
588         }
589         if ((rowxerr += rowincxerr) < 0) {
590             rowxerr &= Integer.MAX_VALUE;
591             rowx++;
592         }
593         if ((rowx += rowincx) >= bWidth) {
594             rowx -= bWidth;
595         }
596         if ((rowyerr += rowincyerr) < 0) {
597             rowyerr &= Integer.MAX_VALUE;
598             rowy++;
599         }
600         if ((rowy += rowincy) >= bHeight) {
601             rowy -= bHeight;
602         }
603         out += outSpan;
604         }
605     }
606     }
607
608     static class ByteFilter extends TexturePaintContext JavaDoc {
609     ByteInterleavedRaster srcRas;
610     int inPalette[];
611     byte inData[];
612     int inOff;
613     int inSpan;
614     int outData[];
615     int outOff;
616     int outSpan;
617
618     public ByteFilter(ByteInterleavedRaster srcRas, ColorModel JavaDoc cm,
619               AffineTransform JavaDoc xform, int maxw)
620     {
621         super((cm.getTransparency() == Transparency.OPAQUE
622            ? xrgbmodel : argbmodel),
623           xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
624         this.inPalette = new int[256];
625         ((IndexColorModel JavaDoc) cm).getRGBs(this.inPalette);
626         this.srcRas = srcRas;
627         this.inData = srcRas.getDataStorage();
628         this.inSpan = srcRas.getScanlineStride();
629         this.inOff = srcRas.getDataOffset(0);
630     }
631
632     public WritableRaster JavaDoc makeRaster(int w, int h) {
633         // Note that we do not pass srcRas to makeRaster since it
634
// is a Byte Raster and this colorModel needs an Int Raster
635
WritableRaster JavaDoc ras = makeRaster(colorModel, null, w, h);
636         IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras;
637         outData = iiRas.getDataStorage();
638         outSpan = iiRas.getScanlineStride();
639         outOff = iiRas.getDataOffset(0);
640         return ras;
641     }
642
643     public void setRaster(int x, int y, int xerr, int yerr,
644                   int w, int h, int bWidth, int bHeight,
645                   int colincx, int colincxerr,
646                   int colincy, int colincyerr,
647                   int rowincx, int rowincxerr,
648                   int rowincy, int rowincyerr) {
649         byte[] inData = this.inData;
650         int[] outData = this.outData;
651         int out = outOff;
652         int inSpan = this.inSpan;
653         int inOff = this.inOff;
654         int outSpan = this.outSpan;
655         int rowx = x;
656         int rowy = y;
657         int rowxerr = xerr;
658         int rowyerr = yerr;
659         int rgbs[] = new int[4];
660         for (int j = 0; j < h; j++) {
661         x = rowx;
662         y = rowy;
663         xerr = rowxerr;
664         yerr = rowyerr;
665         for (int i = 0; i < w; i++) {
666             int nextx, nexty;
667             if ((nextx = x + 1) >= bWidth) {
668             nextx = 0;
669             }
670             if ((nexty = y + 1) >= bHeight) {
671             nexty = 0;
672             }
673             rgbs[0] = inPalette[0xff & inData[inOff + x +
674                               inSpan * y]];
675             rgbs[1] = inPalette[0xff & inData[inOff + nextx +
676                               inSpan * y]];
677             rgbs[2] = inPalette[0xff & inData[inOff + x +
678                               inSpan * nexty]];
679             rgbs[3] = inPalette[0xff & inData[inOff + nextx +
680                               inSpan * nexty]];
681             outData[out + i] =
682             TexturePaintContext.blend(rgbs, xerr, yerr);
683             if ((xerr += colincxerr) < 0) {
684             xerr &= Integer.MAX_VALUE;
685             x++;
686             }
687             if ((x += colincx) >= bWidth) {
688             x -= bWidth;
689             }
690             if ((yerr += colincyerr) < 0) {
691             yerr &= Integer.MAX_VALUE;
692             y++;
693             }
694             if ((y += colincy) >= bHeight) {
695             y -= bHeight;
696             }
697         }
698         if ((rowxerr += rowincxerr) < 0) {
699             rowxerr &= Integer.MAX_VALUE;
700             rowx++;
701         }
702         if ((rowx += rowincx) >= bWidth) {
703             rowx -= bWidth;
704         }
705         if ((rowyerr += rowincyerr) < 0) {
706             rowyerr &= Integer.MAX_VALUE;
707             rowy++;
708         }
709         if ((rowy += rowincy) >= bHeight) {
710             rowy -= bHeight;
711         }
712         out += outSpan;
713         }
714     }
715     }
716
717     static class Any extends TexturePaintContext JavaDoc {
718     WritableRaster JavaDoc srcRas;
719     boolean filter;
720
721     public Any(WritableRaster JavaDoc srcRas, ColorModel JavaDoc cm,
722            AffineTransform JavaDoc xform, int maxw, boolean filter)
723     {
724         super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
725         this.srcRas = srcRas;
726         this.filter = filter;
727     }
728
729     public WritableRaster JavaDoc makeRaster(int w, int h) {
730         return makeRaster(colorModel, srcRas, w, h);
731     }
732
733     public void setRaster(int x, int y, int xerr, int yerr,
734                   int w, int h, int bWidth, int bHeight,
735                   int colincx, int colincxerr,
736                   int colincy, int colincyerr,
737                   int rowincx, int rowincxerr,
738                   int rowincy, int rowincyerr) {
739         Object JavaDoc data = null;
740         int rowx = x;
741         int rowy = y;
742         int rowxerr = xerr;
743         int rowyerr = yerr;
744         WritableRaster JavaDoc srcRas = this.srcRas;
745         WritableRaster JavaDoc outRas = this.outRas;
746         int rgbs[] = filter ? new int[4] : null;
747         for (int j = 0; j < h; j++) {
748         x = rowx;
749         y = rowy;
750         xerr = rowxerr;
751         yerr = rowyerr;
752         for (int i = 0; i < w; i++) {
753             data = srcRas.getDataElements(x, y, data);
754             if (filter) {
755             int nextx, nexty;
756             if ((nextx = x + 1) >= bWidth) {
757                 nextx = 0;
758             }
759             if ((nexty = y + 1) >= bHeight) {
760                 nexty = 0;
761             }
762             rgbs[0] = colorModel.getRGB(data);
763             data = srcRas.getDataElements(nextx, y, data);
764             rgbs[1] = colorModel.getRGB(data);
765             data = srcRas.getDataElements(x, nexty, data);
766             rgbs[2] = colorModel.getRGB(data);
767             data = srcRas.getDataElements(nextx, nexty, data);
768             rgbs[3] = colorModel.getRGB(data);
769             int rgb =
770                 TexturePaintContext.blend(rgbs, xerr, yerr);
771             data = colorModel.getDataElements(rgb, data);
772             }
773             outRas.setDataElements(i, j, data);
774             if ((xerr += colincxerr) < 0) {
775             xerr &= Integer.MAX_VALUE;
776             x++;
777             }
778             if ((x += colincx) >= bWidth) {
779             x -= bWidth;
780             }
781             if ((yerr += colincyerr) < 0) {
782             yerr &= Integer.MAX_VALUE;
783             y++;
784             }
785             if ((y += colincy) >= bHeight) {
786             y -= bHeight;
787             }
788         }
789         if ((rowxerr += rowincxerr) < 0) {
790             rowxerr &= Integer.MAX_VALUE;
791             rowx++;
792         }
793         if ((rowx += rowincx) >= bWidth) {
794             rowx -= bWidth;
795         }
796         if ((rowyerr += rowincyerr) < 0) {
797             rowyerr &= Integer.MAX_VALUE;
798             rowy++;
799         }
800         if ((rowy += rowincy) >= bHeight) {
801             rowy -= bHeight;
802         }
803         }
804     }
805     }
806 }
807
Popular Tags