KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > SVGComposite


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image;
19
20 import java.awt.AlphaComposite JavaDoc;
21 import java.awt.Composite JavaDoc;
22 import java.awt.CompositeContext JavaDoc;
23 import java.awt.RenderingHints JavaDoc;
24 import java.awt.color.ColorSpace JavaDoc;
25 import java.awt.image.ColorModel JavaDoc;
26 import java.awt.image.DataBufferInt JavaDoc;
27 import java.awt.image.PackedColorModel JavaDoc;
28 import java.awt.image.Raster JavaDoc;
29 import java.awt.image.SinglePixelPackedSampleModel JavaDoc;
30 import java.awt.image.WritableRaster JavaDoc;
31
32 /**
33  * This provides an implementation of all the composite rules in SVG.
34  *
35  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
36  * @version $Id: SVGComposite.java,v 1.8 2004/08/18 07:13:49 vhardy Exp $
37  */

38 public class SVGComposite
39     implements Composite JavaDoc {
40
41     public static final SVGComposite OVER
42         = new SVGComposite(CompositeRule.OVER);
43     
44     public static final SVGComposite IN
45         = new SVGComposite(CompositeRule.IN);
46     
47     public static final SVGComposite OUT
48         = new SVGComposite(CompositeRule.OUT);
49     
50     public static final SVGComposite ATOP
51         = new SVGComposite(CompositeRule.ATOP);
52     
53     public static final SVGComposite XOR
54         = new SVGComposite(CompositeRule.XOR);
55     
56     public static final SVGComposite MULTIPLY
57         = new SVGComposite(CompositeRule.MULTIPLY);
58     
59     public static final SVGComposite SCREEN
60         = new SVGComposite(CompositeRule.SCREEN);
61     
62     public static final SVGComposite DARKEN
63         = new SVGComposite(CompositeRule.DARKEN);
64     
65     public static final SVGComposite LIGHTEN
66         = new SVGComposite(CompositeRule.LIGHTEN);
67     
68
69     CompositeRule rule;
70
71     public CompositeRule getRule() { return rule; }
72
73     public SVGComposite(CompositeRule rule) {
74         this.rule = rule;
75     }
76
77     public boolean equals(Object JavaDoc o) {
78         if (o instanceof SVGComposite) {
79             SVGComposite svgc = (SVGComposite)o;
80             return (svgc.getRule() == getRule());
81         } else if (o instanceof AlphaComposite JavaDoc) {
82             AlphaComposite JavaDoc ac = (AlphaComposite JavaDoc)o;
83             switch (getRule().getRule()) {
84             case CompositeRule.RULE_OVER:
85                 return (ac == AlphaComposite.SrcOver);
86             case CompositeRule.RULE_IN:
87                 return (ac == AlphaComposite.SrcIn);
88             case CompositeRule.RULE_OUT:
89                 return (ac == AlphaComposite.SrcOut);
90             default:
91                 return false;
92             }
93         }
94         return false;
95     }
96
97     public boolean is_INT_PACK(ColorModel JavaDoc cm) {
98           // Check ColorModel is of type DirectColorModel
99
if(!(cm instanceof PackedColorModel JavaDoc)) return false;
100
101         PackedColorModel JavaDoc pcm = (PackedColorModel JavaDoc)cm;
102
103         int [] masks = pcm.getMasks();
104
105         // Check transfer type
106
if(masks.length != 4) return false;
107
108         if (masks[0] != 0x00ff0000) return false;
109         if (masks[1] != 0x0000ff00) return false;
110         if (masks[2] != 0x000000ff) return false;
111         if (masks[3] != 0xff000000) return false;
112
113         return true;
114    }
115
116     public CompositeContext JavaDoc createContext(ColorModel JavaDoc srcCM,
117                                           ColorModel JavaDoc dstCM,
118                                           RenderingHints JavaDoc hints) {
119         if (false) {
120             ColorSpace JavaDoc srcCS = srcCM.getColorSpace();
121             ColorSpace JavaDoc dstCS = dstCM.getColorSpace();
122             System.out.println("srcCS: " + srcCS);
123             System.out.println("dstCS: " + dstCS);
124             System.out.println
125                 ("lRGB: " + ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB));
126             System.out.println
127                 ("sRGB: " + ColorSpace.getInstance(ColorSpace.CS_sRGB));
128         }
129
130         // Orig Time no int_pack = 51792
131
// Simple int_pack = 19600
132
boolean use_int_pack = (is_INT_PACK(srcCM) && is_INT_PACK(dstCM));
133         // use_int_pack = false;
134

135         switch (rule.getRule()) {
136         case CompositeRule.RULE_OVER:
137             if (!dstCM.hasAlpha()) {
138                 if (use_int_pack)
139                     return new OverCompositeContext_INT_PACK_NA(srcCM, dstCM);
140                 else
141                     return new OverCompositeContext_NA (srcCM, dstCM);
142             }
143
144             if (!use_int_pack)
145                 return new OverCompositeContext(srcCM, dstCM);
146             
147             if (srcCM.isAlphaPremultiplied())
148                 return new OverCompositeContext_INT_PACK(srcCM, dstCM);
149             else
150                 return new OverCompositeContext_INT_PACK_UNPRE(srcCM, dstCM);
151
152         case CompositeRule.RULE_IN:
153             if (use_int_pack)
154                 return new InCompositeContext_INT_PACK(srcCM, dstCM);
155             else
156                 return new InCompositeContext (srcCM, dstCM);
157
158         case CompositeRule.RULE_OUT:
159             if (use_int_pack)
160                 return new OutCompositeContext_INT_PACK(srcCM, dstCM);
161             else
162                 return new OutCompositeContext (srcCM, dstCM);
163
164         case CompositeRule.RULE_ATOP:
165             if (use_int_pack)
166                 return new AtopCompositeContext_INT_PACK(srcCM, dstCM);
167             else
168                 return new AtopCompositeContext(srcCM, dstCM);
169
170         case CompositeRule.RULE_XOR:
171             if (use_int_pack)
172                 return new XorCompositeContext_INT_PACK(srcCM, dstCM);
173             else
174                 return new XorCompositeContext (srcCM, dstCM);
175
176         case CompositeRule.RULE_ARITHMETIC:
177             float [] coeff = rule.getCoefficients();
178             if (use_int_pack)
179                 return new ArithCompositeContext_INT_PACK_LUT
180                     (srcCM, dstCM, coeff[0], coeff[1], coeff[2], coeff[3]);
181             else
182                 return new ArithCompositeContext
183                     (srcCM, dstCM, coeff[0], coeff[1], coeff[2], coeff[3]);
184
185         case CompositeRule.RULE_MULTIPLY:
186             if (use_int_pack)
187                 return new MultiplyCompositeContext_INT_PACK(srcCM, dstCM);
188             else
189                 return new MultiplyCompositeContext(srcCM, dstCM);
190
191         case CompositeRule.RULE_SCREEN:
192             if (use_int_pack)
193                 return new ScreenCompositeContext_INT_PACK(srcCM, dstCM);
194             else
195                 return new ScreenCompositeContext (srcCM, dstCM);
196
197         case CompositeRule.RULE_DARKEN:
198             if (use_int_pack)
199                 return new DarkenCompositeContext_INT_PACK(srcCM, dstCM);
200             else
201                 return new DarkenCompositeContext (srcCM, dstCM);
202
203         case CompositeRule.RULE_LIGHTEN:
204             if (use_int_pack)
205                 return new LightenCompositeContext_INT_PACK(srcCM, dstCM);
206             else
207                 return new LightenCompositeContext (srcCM, dstCM);
208
209         default:
210             throw new UnsupportedOperationException JavaDoc
211                 ("Unknown composite rule requested.");
212         }
213         
214     }
215
216     public static abstract class AlphaPreCompositeContext
217         implements CompositeContext JavaDoc {
218
219         ColorModel JavaDoc srcCM, dstCM;
220         AlphaPreCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
221             this.srcCM = srcCM;
222             this.dstCM = dstCM;
223         }
224
225         public void dispose() {
226             srcCM = null;
227             dstCM = null;
228         }
229
230         protected abstract void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
231                                            WritableRaster JavaDoc dstOut);
232
233         public void compose(Raster JavaDoc src, Raster JavaDoc dstIn, WritableRaster JavaDoc dstOut) {
234             ColorModel JavaDoc srcPreCM = srcCM;
235             if (!srcCM.isAlphaPremultiplied())
236                 srcPreCM = GraphicsUtil.coerceData((WritableRaster JavaDoc)src,
237                                                    srcCM, true);
238
239             ColorModel JavaDoc dstPreCM = dstCM;
240             if (!dstCM.isAlphaPremultiplied())
241                 dstPreCM = GraphicsUtil.coerceData((WritableRaster JavaDoc)dstIn,
242                                                    dstCM, true);
243             
244             precompose(src, dstIn, dstOut);
245
246             if (!srcCM.isAlphaPremultiplied())
247                 GraphicsUtil.coerceData((WritableRaster JavaDoc)src,
248                                         srcPreCM, false);
249
250             if (!dstCM.isAlphaPremultiplied()) {
251                 GraphicsUtil.coerceData(dstOut, dstPreCM, false);
252                 
253                 if (dstIn != dstOut)
254                     GraphicsUtil.coerceData((WritableRaster JavaDoc)dstIn,
255                                             dstPreCM, false);
256             }
257         }
258     }
259
260     public static abstract class AlphaPreCompositeContext_INT_PACK
261         extends AlphaPreCompositeContext {
262
263         AlphaPreCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
264             super(srcCM, dstCM);
265         }
266
267         protected abstract void precompose_INT_PACK
268             (final int width, final int height,
269              final int [] srcPixels, final int srcAdjust, int srcSp,
270              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
271              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp);
272
273         protected void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
274                                            WritableRaster JavaDoc dstOut) {
275             
276             int x0=dstOut.getMinX();
277             int w =dstOut.getWidth();
278
279             int y0=dstOut.getMinY();
280             int h =dstOut.getHeight();
281
282             SinglePixelPackedSampleModel JavaDoc srcSPPSM;
283             srcSPPSM = (SinglePixelPackedSampleModel JavaDoc)src.getSampleModel();
284
285             final int srcScanStride = srcSPPSM.getScanlineStride();
286             DataBufferInt JavaDoc srcDB = (DataBufferInt JavaDoc)src.getDataBuffer();
287             final int [] srcPixels = srcDB.getBankData()[0];
288             final int srcBase =
289                 (srcDB.getOffset() +
290                  srcSPPSM.getOffset(x0-src.getSampleModelTranslateX(),
291                                     y0-src.getSampleModelTranslateY()));
292
293
294             SinglePixelPackedSampleModel JavaDoc dstInSPPSM;
295             dstInSPPSM = (SinglePixelPackedSampleModel JavaDoc)dstIn.getSampleModel();
296
297             final int dstInScanStride = dstInSPPSM.getScanlineStride();
298             DataBufferInt JavaDoc dstInDB = (DataBufferInt JavaDoc)dstIn.getDataBuffer();
299             final int [] dstInPixels = dstInDB.getBankData()[0];
300             final int dstInBase =
301                 (dstInDB.getOffset() +
302                  dstInSPPSM.getOffset(x0-dstIn.getSampleModelTranslateX(),
303                                       y0-dstIn.getSampleModelTranslateY()));
304
305             SinglePixelPackedSampleModel JavaDoc dstOutSPPSM
306                 = (SinglePixelPackedSampleModel JavaDoc)dstOut.getSampleModel();
307
308             final int dstOutScanStride = dstOutSPPSM.getScanlineStride();
309             DataBufferInt JavaDoc dstOutDB = (DataBufferInt JavaDoc)dstOut.getDataBuffer();
310             final int [] dstOutPixels = dstOutDB.getBankData()[0];
311             final int dstOutBase =
312                 (dstOutDB.getOffset() +
313                  dstOutSPPSM.getOffset(x0-dstOut.getSampleModelTranslateX(),
314                                        y0-dstOut.getSampleModelTranslateY()));
315
316             final int srcAdjust = srcScanStride - w;
317             final int dstInAdjust = dstInScanStride - w;
318             final int dstOutAdjust = dstOutScanStride - w;
319
320             precompose_INT_PACK(w, h,
321                                 srcPixels, srcAdjust, srcBase,
322                                 dstInPixels, dstInAdjust, dstInBase,
323                                 dstOutPixels, dstOutAdjust, dstOutBase);
324         }
325     }
326
327
328     /**
329      * This implements SRC_OVER for 4 band byte data.
330      */

331     public static class OverCompositeContext
332         extends AlphaPreCompositeContext {
333         OverCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
334             super(srcCM, dstCM);
335         }
336
337         public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
338                                WritableRaster JavaDoc dstOut) {
339             int [] srcPix = null;
340             int [] dstPix = null;
341
342             int x=dstOut.getMinX();
343             int w=dstOut.getWidth();
344
345             int y0=dstOut.getMinY();
346             int y1=y0 + dstOut.getHeight();
347
348             final int norm = (1<<24)/255;
349             final int pt5 = (1<<23);
350
351             for (int y = y0; y<y1; y++) {
352                 srcPix = src.getPixels (x, y, w, 1, srcPix);
353                 dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
354                 int sp = 0;
355                 int end = w*4;
356                 while(sp<end) {
357                     final int dstM = (255-srcPix[sp+3])*norm;
358                     dstPix[sp] = srcPix[sp] + ((dstPix[sp]*dstM +pt5)>>>24);
359                     ++sp;
360                     dstPix[sp] = srcPix[sp] + ((dstPix[sp]*dstM +pt5)>>>24);
361                     ++sp;
362                     dstPix[sp] = srcPix[sp] + ((dstPix[sp]*dstM +pt5)>>>24);
363                     ++sp;
364                     dstPix[sp] = srcPix[sp] + ((dstPix[sp]*dstM +pt5)>>>24);
365                     ++sp;
366                 }
367                 dstOut.setPixels(x, y, w, 1, dstPix);
368             }
369
370         }
371     }
372
373     /**
374      * This implements SRC_OVER for 4 band byte src data and
375      * 3 band byte dst data.
376      */

377     public static class OverCompositeContext_NA
378         extends AlphaPreCompositeContext {
379         OverCompositeContext_NA(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
380             super(srcCM, dstCM);
381         }
382
383         public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
384                                WritableRaster JavaDoc dstOut) {
385             int [] srcPix = null;
386             int [] dstPix = null;
387
388             int x=dstOut.getMinX();
389             int w=dstOut.getWidth();
390
391             int y0=dstOut.getMinY();
392             int y1=y0 + dstOut.getHeight();
393
394             final int norm = (1<<24)/255;
395             final int pt5 = (1<<23);
396
397             for (int y = y0; y<y1; y++) {
398                 srcPix = src.getPixels (x, y, w, 1, srcPix);
399                 dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
400                 int srcSP = 0;
401                 int dstSP = 0;
402                 int end = w*4;
403                 while (srcSP<end) {
404                     final int dstM = (255-srcPix[srcSP+3])*norm;
405                     dstPix[dstSP] =
406                         srcPix[srcSP] + ((dstPix[dstSP]*dstM +pt5)>>>24);
407                     ++srcSP; ++dstSP;
408                     dstPix[dstSP] =
409                         srcPix[srcSP] + ((dstPix[dstSP]*dstM +pt5)>>>24);
410                     ++srcSP; ++dstSP;
411                     dstPix[dstSP] =
412                         srcPix[srcSP] + ((dstPix[dstSP]*dstM +pt5)>>>24);
413                     srcSP+=2; ++dstSP;
414                 }
415                 dstOut.setPixels(x, y, w, 1, dstPix);
416             }
417         }
418     }
419
420     /**
421      * This implements SRC_OVER for Int packed data where the src is
422      * premultiplied.
423      */

424     public static class OverCompositeContext_INT_PACK
425         extends AlphaPreCompositeContext_INT_PACK {
426         OverCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
427             super(srcCM, dstCM);
428         }
429
430         public void precompose_INT_PACK
431             (final int width, final int height,
432              final int [] srcPixels, final int srcAdjust, int srcSp,
433              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
434              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
435
436             final int norm = (1<<24)/255;
437             final int pt5 = (1<<23);
438
439             int srcP, dstInP, dstM;
440             
441             for (int y = 0; y<height; y++) {
442                 final int end = dstOutSp+width;
443                 while (dstOutSp<end) {
444                     srcP = srcPixels [srcSp++];
445                     dstInP = dstInPixels[dstInSp++];
446                     
447                     dstM = (255-(srcP>>>24))*norm;
448                     dstOutPixels[dstOutSp++] =
449                         ((( srcP & 0xFF000000) +
450                           (((((dstInP>>>24) )*dstM+pt5)&0xFF000000) ))|
451                          (( srcP & 0x00FF0000) +
452                           (((((dstInP>> 16)&0xFF)*dstM+pt5)&0xFF000000)>>> 8))|
453                          (( srcP & 0x0000FF00) +
454                           (((((dstInP>> 8)&0xFF)*dstM+pt5)&0xFF000000)>>>16))|
455                          (( srcP & 0x000000FF) +
456                           (((((dstInP )&0xFF)*dstM+pt5) )>>>24)));
457                 }
458                 srcSp += srcAdjust;
459                 dstInSp += dstInAdjust;
460                 dstOutSp += dstOutAdjust;
461             }
462         }
463     }
464
465     /**
466      * This implements SRC_OVER for Int packed data and dest has no Alpha...
467      */

468     public static class OverCompositeContext_INT_PACK_NA
469         extends AlphaPreCompositeContext_INT_PACK {
470         OverCompositeContext_INT_PACK_NA(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
471             super (srcCM, dstCM);
472         }
473
474         // When we get here src data has been premultiplied.
475
public void precompose_INT_PACK
476             (final int width, final int height,
477              final int [] srcPixels, final int srcAdjust, int srcSp,
478              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
479              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
480
481             final int norm = (1<<24)/255;
482             final int pt5 = (1<<23);
483
484             int srcP, dstInP, dstM;
485             
486             for (int y = 0; y<height; y++) {
487                 final int end = dstOutSp+width;
488                 while (dstOutSp<end) {
489                     srcP = srcPixels [srcSp++];
490                     dstInP = dstInPixels[dstInSp++];
491                     
492                     dstM = (255-(srcP>>>24))*norm;
493                     dstOutPixels[dstOutSp++] =
494                         ((( srcP & 0x00FF0000) +
495                           (((((dstInP>> 16)&0xFF)*dstM+pt5)&0xFF000000)>>> 8))|
496                          (( srcP & 0x0000FF00) +
497                           (((((dstInP>> 8)&0xFF)*dstM+pt5)&0xFF000000)>>>16))|
498                          (( srcP & 0x000000FF) +
499                           (((((dstInP )&0xFF)*dstM+pt5) )>>>24)));
500                 }
501                 srcSp += srcAdjust;
502                 dstInSp += dstInAdjust;
503                 dstOutSp += dstOutAdjust;
504             }
505         }
506     }
507
508     /**
509      * This implements SRC_OVER for Int packed data where the src is
510      * unpremultiplied. This avoids having to multiply the alpha on the
511      * the source then divide it out again.
512      */

513     public static class OverCompositeContext_INT_PACK_UNPRE
514         extends AlphaPreCompositeContext_INT_PACK {
515         OverCompositeContext_INT_PACK_UNPRE
516             (ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
517             super(srcCM, dstCM);
518
519             if (srcCM.isAlphaPremultiplied())
520                 throw new IllegalArgumentException JavaDoc
521                     ("OverCompositeContext_INT_PACK_UNPRE is only for" +
522                      "sources with unpremultiplied alpha");
523         }
524
525         public void compose(Raster JavaDoc src, Raster JavaDoc dstIn, WritableRaster JavaDoc dstOut) {
526             ColorModel JavaDoc dstPreCM = dstCM;
527             if (!dstCM.isAlphaPremultiplied())
528                 dstPreCM = GraphicsUtil.coerceData((WritableRaster JavaDoc)dstIn,
529                                                    dstCM, true);
530             
531             precompose(src, dstIn, dstOut);
532
533             if (!dstCM.isAlphaPremultiplied()) {
534                 GraphicsUtil.coerceData(dstOut, dstPreCM, false);
535                 
536                 if (dstIn != dstOut)
537                     GraphicsUtil.coerceData((WritableRaster JavaDoc)dstIn,
538                                             dstPreCM, false);
539             }
540         }
541
542         public void precompose_INT_PACK
543             (final int width, final int height,
544              final int [] srcPixels, final int srcAdjust, int srcSp,
545              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
546              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
547
548             final int norm = (1<<24)/255;
549             final int pt5 = (1<<23);
550
551             int srcP, srcM, dstP, dstM;
552
553             for (int y = 0; y<height; y++) {
554                 final int end = dstOutSp+width;
555                 while (dstOutSp<end) {
556                     srcP = srcPixels [srcSp++];
557                     dstP = dstInPixels[dstInSp++];
558                     
559                     srcM = ( (srcP>>>24))*norm;
560                     dstM = (255-(srcP>>>24))*norm;
561
562                     dstOutPixels[dstOutSp++] =
563                         ((((( srcP&0xFF000000) +
564                             ((dstP>>>24) )*dstM + pt5)&0xFF000000) ) |
565                          (((((srcP>> 16)&0xFF)*srcM +
566                             ((dstP>> 16)&0xFF)*dstM + pt5)&0xFF000000)>>> 8) |
567                          (((((srcP>> 8)&0xFF)*srcM +
568                             ((dstP>> 8)&0xFF)*dstM + pt5)&0xFF000000)>>>16) |
569                          (((((srcP )&0xFF)*srcM +
570                             ((dstP )&0xFF)*dstM + pt5) )>>>24));
571                 }
572                 srcSp += srcAdjust;
573                 dstInSp += dstInAdjust;
574                 dstOutSp += dstOutAdjust;
575             }
576         }
577     }
578
579     public static class InCompositeContext
580         extends AlphaPreCompositeContext {
581         InCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
582             super(srcCM, dstCM);
583         }
584
585         public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
586                                WritableRaster JavaDoc dstOut) {
587             int [] srcPix = null;
588             int [] dstPix = null;
589
590             int x=dstOut.getMinX();
591             int w=dstOut.getWidth();
592
593             int y0=dstOut.getMinY();
594             int y1=y0 + dstOut.getHeight();
595
596             final int norm = (1<<24)/255;
597             final int pt5 = (1<<23);
598
599             for (int y = y0; y<y1; y++) {
600                 srcPix = src.getPixels (x, y, w, 1, srcPix);
601                 dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
602                 int sp = 0;
603                 int end = w*4;
604                 while(sp<end) {
605                     final int srcM = dstPix[sp+3]*norm;
606                     dstPix[sp] = (srcPix[sp]*srcM + pt5)>>>24; ++sp;
607                     dstPix[sp] = (srcPix[sp]*srcM + pt5)>>>24; ++sp;
608                     dstPix[sp] = (srcPix[sp]*srcM + pt5)>>>24; ++sp;
609                     dstPix[sp] = (srcPix[sp]*srcM + pt5)>>>24; ++sp;
610                 }
611                 dstOut.setPixels(x, y, w, 1, dstPix);
612             }
613
614         }
615     }
616
617     public static class InCompositeContext_INT_PACK
618         extends AlphaPreCompositeContext_INT_PACK {
619         InCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
620             super(srcCM, dstCM);
621         }
622
623         public void precompose_INT_PACK
624             (final int width, final int height,
625              final int [] srcPixels, final int srcAdjust, int srcSp,
626              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
627              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
628
629             final int norm = (1<<24)/255;
630             final int pt5 = (1<<23);
631
632             int srcP, srcM;
633
634             for (int y = 0; y<height; y++) {
635                 final int end = dstOutSp+width;
636                 while (dstOutSp<end) {
637                     srcM = (dstInPixels[dstInSp++]>>>24)*norm;
638                     srcP = srcPixels [srcSp++];
639                     dstOutPixels[dstOutSp++] =
640                         ((((((srcP>>>24) )*srcM + pt5)&0xFF000000) ) |
641                          (((((srcP>> 16)&0xFF)*srcM + pt5)&0xFF000000)>>> 8) |
642                          (((((srcP>> 8)&0xFF)*srcM + pt5)&0xFF000000)>>>16) |
643                          (((((srcP )&0xFF)*srcM + pt5) )>>>24));
644                 }
645                 srcSp += srcAdjust;
646                 dstInSp += dstInAdjust;
647                 dstOutSp += dstOutAdjust;
648             }
649         }
650     }
651
652     public static class OutCompositeContext
653         extends AlphaPreCompositeContext {
654         OutCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
655             super(srcCM, dstCM);
656         }
657
658         public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
659                                WritableRaster JavaDoc dstOut) {
660             int [] srcPix = null;
661             int [] dstPix = null;
662
663             int x=dstOut.getMinX();
664             int w=dstOut.getWidth();
665
666             int y0=dstOut.getMinY();
667             int y1=y0 + dstOut.getHeight();
668
669             final int norm = (1<<24)/255;
670             final int pt5 = (1<<23);
671
672             for (int y = y0; y<y1; y++) {
673                 srcPix = src.getPixels (x, y, w, 1, srcPix);
674                 dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
675                 int sp = 0;
676                 int end = w*4;
677                 while(sp<end) {
678                     final int srcM = (255-dstPix[sp+3])*norm;
679                     dstPix[sp] = (srcPix[sp]*srcM + pt5)>>>24; ++sp;
680                     dstPix[sp] = (srcPix[sp]*srcM + pt5)>>>24; ++sp;
681                     dstPix[sp] = (srcPix[sp]*srcM + pt5)>>>24; ++sp;
682                     dstPix[sp] = (srcPix[sp]*srcM + pt5)>>>24; ++sp;
683                 }
684                 dstOut.setPixels(x, y, w, 1, dstPix);
685             }
686
687         }
688     }
689
690     public static class OutCompositeContext_INT_PACK
691         extends AlphaPreCompositeContext_INT_PACK {
692         OutCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
693             super(srcCM, dstCM);
694         }
695
696         public void precompose_INT_PACK
697             (final int width, final int height,
698              final int [] srcPixels, final int srcAdjust, int srcSp,
699              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
700              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
701
702             final int norm = (1<<24)/255;
703             final int pt5 = (1<<23);
704
705             int srcP, srcM;
706
707             for (int y = 0; y<height; y++) {
708                 final int end = dstOutSp+width;
709                 while (dstOutSp<end) {
710                     srcM = (255-(dstInPixels[dstInSp++]>>>24))*norm;
711                     srcP = srcPixels [srcSp++];
712                     dstOutPixels[dstOutSp++] =
713                         ((((((srcP>>>24) )*srcM + pt5)&0xFF000000) ) |
714                          (((((srcP>> 16)&0xFF)*srcM + pt5)&0xFF000000)>>> 8) |
715                          (((((srcP>> 8)&0xFF)*srcM + pt5)&0xFF000000)>>>16) |
716                          (((((srcP )&0xFF)*srcM + pt5) )>>>24));
717                 }
718                 srcSp += srcAdjust;
719                 dstInSp += dstInAdjust;
720                 dstOutSp += dstOutAdjust;
721             }
722         }
723     }
724
725     public static class AtopCompositeContext
726         extends AlphaPreCompositeContext {
727         AtopCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
728             super(srcCM, dstCM);
729         }
730
731         public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
732                                WritableRaster JavaDoc dstOut) {
733             int [] srcPix = null;
734             int [] dstPix = null;
735
736             int x=dstOut.getMinX();
737             int w=dstOut.getWidth();
738
739             int y0=dstOut.getMinY();
740             int y1=y0 + dstOut.getHeight();
741
742             final int norm = (1<<24)/255;
743             final int pt5 = (1<<23);
744
745             for (int y = y0; y<y1; y++) {
746                 srcPix = src.getPixels (x, y, w, 1, srcPix);
747                 dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
748                 int sp = 0;
749                 int end = w*4;
750                 while(sp<end) {
751                     final int srcM = ( dstPix[sp+3])*norm;
752                     final int dstM = (255-srcPix[sp+3])*norm;
753                     dstPix[sp] =(srcPix[sp]*srcM + dstPix[sp]*dstM +pt5)>>>24;
754                     ++sp;
755                     dstPix[sp] =(srcPix[sp]*srcM + dstPix[sp]*dstM +pt5)>>>24;
756                     ++sp;
757                     dstPix[sp] =(srcPix[sp]*srcM + dstPix[sp]*dstM +pt5)>>>24;
758                     sp+=2;
759                 }
760                 dstOut.setPixels(x, y, w, 1, dstPix);
761             }
762
763         }
764     }
765
766     public static class AtopCompositeContext_INT_PACK
767         extends AlphaPreCompositeContext_INT_PACK {
768         AtopCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
769             super(srcCM, dstCM);
770         }
771
772         public void precompose_INT_PACK
773             (final int width, final int height,
774              final int [] srcPixels, final int srcAdjust, int srcSp,
775              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
776              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
777
778             final int norm = (1<<24)/255;
779             final int pt5 = (1<<23);
780
781             int srcP, srcM, dstP, dstM;
782
783             for (int y = 0; y<height; y++) {
784                 final int end = dstOutSp+width;
785                 while (dstOutSp<end) {
786                     srcP = srcPixels [srcSp++];
787                     dstP = dstInPixels[dstInSp++];
788
789                     srcM = ( dstP>>>24) *norm;
790                     dstM = (255-(srcP>>>24))*norm;
791
792                     dstOutPixels[dstOutSp++] =
793                         ((dstP&0xFF000000) |
794                          (((((srcP>> 16)&0xFF)*srcM +
795                             ((dstP>> 16)&0xFF)*dstM + pt5)&0xFF000000)>>> 8) |
796                          (((((srcP>> 8)&0xFF)*srcM +
797                             ((dstP>> 8)&0xFF)*dstM + pt5)&0xFF000000)>>>16) |
798                          (((((srcP )&0xFF)*srcM +
799                             ((dstP )&0xFF)*dstM + pt5) )>>>24));
800                 }
801                 srcSp += srcAdjust;
802                 dstInSp += dstInAdjust;
803                 dstOutSp += dstOutAdjust;
804             }
805         }
806     }
807
808     public static class XorCompositeContext
809         extends AlphaPreCompositeContext {
810
811         XorCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
812             super(srcCM, dstCM);
813         }
814
815         public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
816                                WritableRaster JavaDoc dstOut) {
817             int [] srcPix = null;
818             int [] dstPix = null;
819
820             int x=dstOut.getMinX();
821             int w=dstOut.getWidth();
822
823             int y0=dstOut.getMinY();
824             int y1=y0 + dstOut.getHeight();
825
826             final int norm = (1<<24)/255;
827             final int pt5 = (1<<23);
828
829             for (int y = y0; y<y1; y++) {
830                 srcPix = src.getPixels (x, y, w, 1, srcPix);
831                 dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
832                 int sp = 0;
833                 int end = w*4;
834                 while(sp<end) {
835                     final int srcM = (255-dstPix[sp+3])*norm;
836                     final int dstM = (255-srcPix[sp+3])*norm;
837
838                     dstPix[sp] = (srcPix[sp]*srcM +
839                                   dstPix[sp]*dstM + pt5)>>>24; ++sp;
840                     dstPix[sp] = (srcPix[sp]*srcM +
841                                   dstPix[sp]*dstM + pt5)>>>24; ++sp;
842                     dstPix[sp] = (srcPix[sp]*srcM +
843                                   dstPix[sp]*dstM + pt5)>>>24; ++sp;
844                     dstPix[sp] = (srcPix[sp]*srcM +
845                                   dstPix[sp]*dstM + pt5)>>>24; ++sp;
846                 }
847                 dstOut.setPixels(x, y, w, 1, dstPix);
848             }
849
850         }
851     }
852
853     public static class XorCompositeContext_INT_PACK
854         extends AlphaPreCompositeContext_INT_PACK {
855         XorCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
856             super(srcCM, dstCM);
857         }
858
859         public void precompose_INT_PACK
860             (final int width, final int height,
861              final int [] srcPixels, final int srcAdjust, int srcSp,
862              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
863              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
864
865             final int norm = (1<<24)/255;
866             final int pt5 = (1<<23);
867
868             int srcP, srcM, dstP, dstM;
869
870             for (int y = 0; y<height; y++) {
871                 final int end = dstOutSp+width;
872                 while (dstOutSp<end) {
873                     srcP = srcPixels [srcSp++];
874                     dstP = dstInPixels[dstInSp++];
875
876                     srcM = (255-(dstP>>>24))*norm;
877                     dstM = (255-(srcP>>>24))*norm;
878
879                     dstOutPixels[dstOutSp++] =
880                         ((((((srcP>>>24) )*srcM +
881                             ((dstP>>>24) )*dstM + pt5)&0xFF000000) ) |
882                          (((((srcP>> 16)&0xFF)*srcM +
883                             ((dstP>> 16)&0xFF)*dstM + pt5)&0xFF000000)>>> 8) |
884                          (((((srcP>> 8)&0xFF)*srcM +
885                             ((dstP>> 8)&0xFF)*dstM + pt5)&0xFF000000)>>>16) |
886                          (((((srcP )&0xFF)*srcM +
887                             ((dstP )&0xFF)*dstM + pt5) )>>>24));
888                 }
889                 srcSp += srcAdjust;
890                 dstInSp += dstInAdjust;
891                 dstOutSp += dstOutAdjust;
892             }
893         }
894     }
895
896     public static class ArithCompositeContext
897         extends AlphaPreCompositeContext {
898         float k1, k2, k3, k4;
899         ArithCompositeContext(ColorModel JavaDoc srcCM,
900                               ColorModel JavaDoc dstCM,
901                               float k1, float k2, float k3, float k4) {
902             super(srcCM, dstCM);
903             this.k1 = k1;
904             this.k2 = k2;
905             this.k3 = k3;
906             this.k4 = k4;
907         }
908
909         public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
910                                WritableRaster JavaDoc dstOut) {
911             int [] srcPix = null;
912             int [] dstPix = null;
913
914             int x=dstOut.getMinX();
915             int w=dstOut.getWidth();
916             int bands = dstOut.getNumBands();
917
918             int y0=dstOut.getMinY();
919             int y1=y0 + dstOut.getHeight();
920             
921             float kk1 = k1/255.0f;
922             float kk4 = k4*255.0f+0.5f;
923
924             int y, i, b, val, max;
925             for (y = y0; y<y1; y++) {
926                 srcPix = src.getPixels (x, y, w, 1, srcPix);
927                 dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
928                 for (i=0; i<srcPix.length; i++) {
929                     max=0;
930                     for (b=1; b<bands; b++, i++) {
931                         val =(int)((kk1*srcPix[i]*dstPix[i]) +
932                                    k2*srcPix[i] + k3*dstPix[i] + kk4);
933                         if ((val & 0xFFFFFF00) != 0)
934                             if ((val & 0x80000000) != 0) val = 0;
935                             else val = 255;
936                         if (val > max) max=val;
937                         dstPix[i] = val;
938                     }
939
940                     val =(int)((kk1*srcPix[i]*dstPix[i]) +
941                                k2*srcPix[i] + k3*dstPix[i] + kk4);
942                     if ((val & 0xFFFFFF00) != 0)
943                         if ((val & 0x80000000) != 0) val = 0;
944                         else val = 255;
945                     if (val > max)
946                         dstPix[i] = val;
947                     else
948                         dstPix[i] = max;
949                 }
950                 dstOut.setPixels(x, y, w, 1, dstPix);
951             }
952         }
953     }
954
955     public static class ArithCompositeContext_INT_PACK
956         extends AlphaPreCompositeContext_INT_PACK {
957         float k1, k2, k3, k4;
958         ArithCompositeContext_INT_PACK(ColorModel JavaDoc srcCM,
959                                        ColorModel JavaDoc dstCM,
960                                        float k1, float k2,
961                                        float k3, float k4) {
962             super(srcCM, dstCM);
963             this.k1 = k1/255.0f;
964             this.k2 = k2;
965             this.k3 = k3;
966             this.k4 = k4*255.0f+0.5f;
967         }
968
969         public void precompose_INT_PACK
970             (final int width, final int height,
971              final int [] srcPixels, final int srcAdjust, int srcSp,
972              final int [] dstInPixels, final int dstInAdjust, int dstInSp,
973              final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
974
975             int srcP, dstP, a, r, g, b;
976
977             for (int y = 0; y<height; y++) {
978                 final int end = dstOutSp+width;
979                 while (dstOutSp<end) {
980                     srcP = srcPixels [srcSp++];
981                     dstP = dstInPixels [dstInSp++];
982                     a = (int)((srcP>>>24)*(dstP>>>24)*k1 +
983                               (srcP>>>24)*k2 + (dstP>>>24)*k3 + k4);
984                     if ((a & 0xFFFFFF00) != 0)
985                         if ((a & 0x80000000) != 0) a = 0;
986                         else a = 255;
987
988                     r = (int)(((srcP>> 16)&0xFF)*((dstP>> 16)&0xFF)*k1 +
989                               ((srcP>> 16)&0xFF)*k2 +
990                               ((dstP>> 16)&0xFF)*k3 + k4);
991                     if ((r & 0xFFFFFF00) != 0)
992                         if ((r & 0x80000000) != 0) r = 0;
993                         else r = 255;
994                     if (a < r) a = r;
995
996                     g = (int)(((srcP>> 8)&0xFF)*((dstP>> 8)&0xFF)*k1 +
997                               ((srcP>> 8)&0xFF)*k2 +
998                               ((dstP>> 8)&0xFF)*k3 + k4);
999                     if ((g & 0xFFFFFF00) != 0)
1000                        if ((g & 0x80000000) != 0) g = 0;
1001                        else g = 255;
1002                    if (a < g) a = g;
1003
1004                    b = (int)((srcP&0xFF)*(dstP&0xFF)*k1 +
1005                              (srcP&0xFF)*k2 + (dstP&0xFF)*k3 + k4);
1006                    if ((b & 0xFFFFFF00) != 0)
1007                        if ((b & 0x80000000) != 0) b = 0;
1008                        else b = 255;
1009                    if (a < b) a = b;
1010                    
1011                    dstOutPixels[dstOutSp++]
1012                        = ((a<<24) | (r<<16) | (g<<8) | b);
1013                }
1014                srcSp += srcAdjust;
1015                dstInSp += dstInAdjust;
1016                dstOutSp += dstOutAdjust;
1017            }
1018            // long endTime = System.currentTimeMillis();
1019
// System.out.println("Arith Time: " + (endTime-startTime));
1020
}
1021    }
1022
1023    public static class ArithCompositeContext_INT_PACK_LUT
1024        extends AlphaPreCompositeContext_INT_PACK {
1025        byte [] lut;
1026        ArithCompositeContext_INT_PACK_LUT(ColorModel JavaDoc srcCM,
1027                                           ColorModel JavaDoc dstCM,
1028                                           float k1, float k2,
1029                                           float k3, float k4) {
1030            super(srcCM, dstCM);
1031            k1 = k1/255.0f;
1032            k4 = k4*255.0f+0.5f;
1033            int sz = 256*256;
1034            lut = new byte[sz];
1035            int val;
1036            for (int i=0; i<sz; i++) {
1037                val = (int)((i>>8)*(i&0xFF)*k1 + (i>>8)*k2 + (i&0xFF)*k3 + k4);
1038                if ((val & 0xFFFFFF00) != 0)
1039                    if ((val & 0x80000000) != 0) val = 0;
1040                    else val = 255;
1041                lut[i] = (byte)val;
1042            }
1043        }
1044
1045        public void precompose_INT_PACK
1046            (final int width, final int height,
1047             final int [] srcPixels, final int srcAdjust, int srcSp,
1048             final int [] dstInPixels, final int dstInAdjust, int dstInSp,
1049             final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
1050
1051            int srcP, dstP;
1052            int a, r, g, b;
1053            for (int y = 0; y<height; y++) {
1054                final int end = dstOutSp+width;
1055                while (dstOutSp<end) {
1056                    srcP = srcPixels [srcSp++];
1057                    dstP = dstInPixels[dstInSp++];
1058                    
1059                    a = lut[(((srcP>> 16)&0xFF00)|((dstP>>>24) ))];
1060                    a &= 0xFF;
1061                    r = lut[(((srcP>> 8)&0xFF00)|((dstP>> 16)&0x00FF))];
1062                    r &= 0xFF;
1063                    g = lut[(((srcP )&0xFF00)|((dstP>> 8)&0x00FF))];
1064                    g &= 0xFF;
1065                    b = lut[(((srcP<< 8)&0xFF00)|((dstP )&0x00FF))];
1066                    b &= 0xFF;
1067                    if (r>a) a = r;
1068                    if (g>a) a = g;
1069                    if (b>a) a = b;
1070                    dstOutPixels[dstOutSp++] = (a<<24)|(r<<16)|(g<<8)|(b);
1071                }
1072                srcSp += srcAdjust;
1073                dstInSp += dstInAdjust;
1074                dstOutSp += dstOutAdjust;
1075            }
1076            // long endTime = System.currentTimeMillis();
1077
// System.out.println("ArithLut Time: " + (endTime-startTime));
1078
}
1079    }
1080
1081
1082    /**
1083     * The following classes implement the various blend modes from SVG. */

1084    public static class MultiplyCompositeContext
1085        extends AlphaPreCompositeContext {
1086
1087        MultiplyCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
1088            super(srcCM, dstCM);
1089        }
1090
1091        public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
1092                               WritableRaster JavaDoc dstOut) {
1093            int [] srcPix = null;
1094            int [] dstPix = null;
1095
1096            int x=dstOut.getMinX();
1097            int w=dstOut.getWidth();
1098
1099            int y0=dstOut.getMinY();
1100            int y1=y0 + dstOut.getHeight();
1101
1102            final int norm = (1<<24)/255;
1103            final int pt5 = (1<<23);
1104            int srcM, dstM;
1105
1106            for (int y = y0; y<y1; y++) {
1107                srcPix = src.getPixels (x, y, w, 1, srcPix);
1108                dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
1109                int sp = 0;
1110                int end = w*4;
1111                while(sp<end) {
1112                    srcM = 255-dstPix[sp+3];
1113                    dstM = 255-srcPix[sp+3];
1114
1115                    dstPix[sp] = ((srcPix[sp]*srcM + dstPix[sp]*dstM +
1116                                   srcPix[sp]*dstPix[sp])*norm + pt5)>>>24;
1117                    ++sp;
1118
1119                    dstPix[sp] = ((srcPix[sp]*srcM + dstPix[sp]*dstM +
1120                                   srcPix[sp]*dstPix[sp])*norm + pt5)>>>24;
1121                    ++sp;
1122
1123                    dstPix[sp] = ((srcPix[sp]*srcM + dstPix[sp]*dstM +
1124                                   srcPix[sp]*dstPix[sp])*norm + pt5)>>>24;
1125                    ++sp;
1126
1127                    dstPix[sp] = (srcPix[sp] + dstPix[sp] -
1128                                  ((dstPix[sp]*srcPix[sp]*norm + pt5)>>>24));
1129                    ++sp;
1130                }
1131                dstOut.setPixels(x, y, w, 1, dstPix);
1132            }
1133        }
1134    }
1135
1136    public static class MultiplyCompositeContext_INT_PACK
1137        extends AlphaPreCompositeContext_INT_PACK {
1138        MultiplyCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
1139            super(srcCM, dstCM);
1140        }
1141
1142        public void precompose_INT_PACK
1143            (final int width, final int height,
1144             final int [] srcPixels, final int srcAdjust, int srcSp,
1145             final int [] dstInPixels, final int dstInAdjust, int dstInSp,
1146             final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
1147
1148            final int norm = (1<<24)/255;
1149            final int pt5 = (1<<23);
1150
1151            int srcP, srcA, srcR, srcG, srcB, srcM;
1152            int dstP, dstA, dstR, dstG, dstB, dstM;
1153
1154            for (int y = 0; y<height; y++) {
1155                final int end = dstOutSp+width;
1156                while (dstOutSp<end) {
1157                    srcP = srcPixels [srcSp++];
1158                    dstP = dstInPixels[dstInSp++];
1159
1160                    srcA = (srcP>>>24);
1161                    dstA = (dstP>>>24);
1162                    srcR = (srcP>> 16)&0xFF;
1163                    dstR = (dstP>> 16)&0xFF;
1164                    srcG = (srcP>> 8)&0xFF;
1165                    dstG = (dstP>> 8)&0xFF;
1166                    srcB = (srcP )&0xFF;
1167                    dstB = (dstP )&0xFF;
1168
1169                    srcM = 255-dstA;
1170                    dstM = 255-srcA;
1171
1172                    dstOutPixels[dstOutSp++] =
1173                        (((((srcR*srcM + dstR*dstM + srcR*dstR)
1174                            *norm + pt5)&0xFF000000)>>> 8) |
1175                         ((((srcG*srcM + dstG*dstM + srcG*dstG)
1176                            *norm + pt5)&0xFF000000)>>>16) |
1177                         ((((srcB*srcM + dstB*dstM + srcB*dstB)
1178                            *norm + pt5) )>>>24) |
1179                         ((srcA + dstA - ((srcA*dstA*norm + pt5)>>>24))<<24));
1180                }
1181                srcSp += srcAdjust;
1182                dstInSp += dstInAdjust;
1183                dstOutSp += dstOutAdjust;
1184            }
1185        }
1186    }
1187
1188    public static class ScreenCompositeContext
1189        extends AlphaPreCompositeContext {
1190
1191        ScreenCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
1192            super(srcCM, dstCM);
1193        }
1194
1195        public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
1196                               WritableRaster JavaDoc dstOut) {
1197            int [] srcPix = null;
1198            int [] dstPix = null;
1199
1200            int x=dstOut.getMinX();
1201            int w=dstOut.getWidth();
1202
1203            int y0=dstOut.getMinY();
1204            int y1=y0 + dstOut.getHeight();
1205
1206            final int norm = (1<<24)/255;
1207            final int pt5 = (1<<23);
1208
1209            for (int y = y0; y<y1; y++) {
1210                srcPix = src.getPixels (x, y, w, 1, srcPix);
1211                dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
1212                int sp = 0;
1213                int end = w*4;
1214                while(sp<end) {
1215                    dstPix[sp] = (srcPix[sp] + dstPix[sp] -
1216                                  ((dstPix[sp]*srcPix[sp]*norm + pt5)>>>24));
1217                    ++sp;
1218                    dstPix[sp] = (srcPix[sp] + dstPix[sp] -
1219                                  ((dstPix[sp]*srcPix[sp]*norm + pt5)>>>24));
1220                    ++sp;
1221                    dstPix[sp] = (srcPix[sp] + dstPix[sp] -
1222                                  ((dstPix[sp]*srcPix[sp]*norm + pt5)>>>24));
1223                    ++sp;
1224                    dstPix[sp] = (srcPix[sp] + dstPix[sp] -
1225                                  ((dstPix[sp]*srcPix[sp]*norm + pt5)>>>24));
1226                    ++sp;
1227                }
1228                dstOut.setPixels(x, y, w, 1, dstPix);
1229            }
1230        }
1231    }
1232
1233    public static class ScreenCompositeContext_INT_PACK
1234        extends AlphaPreCompositeContext_INT_PACK {
1235        ScreenCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
1236            super(srcCM, dstCM);
1237        }
1238
1239        public void precompose_INT_PACK
1240            (final int width, final int height,
1241             final int [] srcPixels, final int srcAdjust, int srcSp,
1242             final int [] dstInPixels, final int dstInAdjust, int dstInSp,
1243             final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
1244
1245            final int norm = (1<<24)/255;
1246            final int pt5 = (1<<23);
1247
1248            int srcP, srcA, srcR, srcG, srcB;
1249            int dstP, dstA, dstR, dstG, dstB;
1250
1251            for (int y = 0; y<height; y++) {
1252                final int end = dstOutSp+width;
1253                while (dstOutSp<end) {
1254                    srcP = srcPixels [srcSp++];
1255                    dstP = dstInPixels[dstInSp++];
1256
1257                    srcA = (srcP>>>24);
1258                    dstA = (dstP>>>24);
1259                    srcR = (srcP>> 16)&0xFF;
1260                    dstR = (dstP>> 16)&0xFF;
1261                    srcG = (srcP>> 8)&0xFF;
1262                    dstG = (dstP>> 8)&0xFF;
1263                    srcB = (srcP )&0xFF;
1264                    dstB = (dstP )&0xFF;
1265
1266                    dstOutPixels[dstOutSp++] =
1267                        (((srcR + dstR - ((srcR*dstR*norm + pt5)>>>24))<<16)|
1268                         ((srcG + dstG - ((srcG*dstG*norm + pt5)>>>24))<< 8)|
1269                         ((srcB + dstB - ((srcB*dstB*norm + pt5)>>>24)) )|
1270                         ((srcA + dstA - ((srcA*dstA*norm + pt5)>>>24))<<24));
1271                }
1272                srcSp += srcAdjust;
1273                dstInSp += dstInAdjust;
1274                dstOutSp += dstOutAdjust;
1275            }
1276        }
1277    }
1278
1279    public static class DarkenCompositeContext
1280        extends AlphaPreCompositeContext {
1281
1282        DarkenCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
1283            super(srcCM, dstCM);
1284        }
1285
1286        public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
1287                               WritableRaster JavaDoc dstOut) {
1288            int [] srcPix = null;
1289            int [] dstPix = null;
1290
1291            int x=dstOut.getMinX();
1292            int w=dstOut.getWidth();
1293
1294            int y0=dstOut.getMinY();
1295            int y1=y0 + dstOut.getHeight();
1296
1297            final int norm = (1<<24)/255;
1298            final int pt5 = (1<<23);
1299
1300            int sp, srcM, dstM, t1, t2;
1301
1302            for (int y = y0; y<y1; y++) {
1303                srcPix = src.getPixels (x, y, w, 1, srcPix);
1304                dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
1305                sp = 0;
1306                final int end = w*4;
1307                while(sp<end) {
1308                    srcM = 255-dstPix[sp+3];
1309                    dstM = 255-srcPix[sp+3];
1310
1311                    t1 = ((srcM*srcPix[sp]*norm + pt5)>>>24) + dstPix[sp];
1312                    t2 = ((dstM*dstPix[sp]*norm + pt5)>>>24) + srcPix[sp];
1313                    if (t1 > t2) dstPix[sp] = t2;
1314                    else dstPix[sp] = t1;
1315                    ++sp;
1316
1317                    t1 = ((srcM*srcPix[sp]*norm + pt5)>>>24) + dstPix[sp];
1318                    t2 = ((dstM*dstPix[sp]*norm + pt5)>>>24) + srcPix[sp];
1319                    if (t1 > t2) dstPix[sp] = t2;
1320                    else dstPix[sp] = t1;
1321                    ++sp;
1322
1323                    t1 = ((srcM*srcPix[sp]*norm + pt5)>>>24) + dstPix[sp];
1324                    t2 = ((dstM*dstPix[sp]*norm + pt5)>>>24) + srcPix[sp];
1325                    if (t1 > t2) dstPix[sp] = t2;
1326                    else dstPix[sp] = t1;
1327                    ++sp;
1328
1329                    dstPix[sp] = (srcPix[sp] + dstPix[sp] -
1330                                  ((dstPix[sp]*srcPix[sp]*norm + pt5)>>>24));
1331                    ++sp;
1332                }
1333                dstOut.setPixels(x, y, w, 1, dstPix);
1334            }
1335        }
1336    }
1337
1338    public static class DarkenCompositeContext_INT_PACK
1339        extends AlphaPreCompositeContext_INT_PACK {
1340        DarkenCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
1341            super(srcCM, dstCM);
1342        }
1343
1344        public void precompose_INT_PACK
1345            (final int width, final int height,
1346             final int [] srcPixels, final int srcAdjust, int srcSp,
1347             final int [] dstInPixels, final int dstInAdjust, int dstInSp,
1348             final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
1349
1350            final int norm = (1<<24)/255;
1351            final int pt5 = (1<<23);
1352
1353            int srcP, srcM;
1354            int dstP, dstM, dstA, dstR, dstG, dstB;
1355
1356            int srcV, dstV, tmp;
1357
1358            for (int y = 0; y<height; y++) {
1359                final int end = dstOutSp+width;
1360                while (dstOutSp<end) {
1361                    srcP = srcPixels [srcSp++];
1362                    dstP = dstInPixels[dstInSp++];
1363
1364                    srcV = (srcP>>>24);
1365                    dstV = (dstP>>>24);
1366                    srcM = (255-dstV)*norm;
1367                    dstM = (255-srcV)*norm;
1368                    dstA = (srcV + dstV - ((srcV*dstV*norm + pt5)>>>24));
1369
1370                    srcV = (srcP>> 16)&0xFF;
1371                    dstV = (dstP>> 16)&0xFF;
1372                    dstR = ((srcM*srcV + pt5)>>>24) + dstV;
1373                    tmp = ((dstM*dstV + pt5)>>>24) + srcV;
1374                    if (dstR > tmp) dstR = tmp;
1375                    
1376                    srcV = (srcP>> 8)&0xFF;
1377                    dstV = (dstP>> 8)&0xFF;
1378                    dstG = ((srcM*srcV + pt5)>>>24) + dstV;
1379                    tmp = ((dstM*dstV + pt5)>>>24) + srcV;
1380                    if (dstG > tmp) dstG = tmp;
1381                    
1382
1383                    srcV = (srcP )&0xFF;
1384                    dstV = (dstP )&0xFF;
1385                    dstB = ((srcM*srcV + pt5)>>>24) + dstV;
1386                    tmp = ((dstM*dstV + pt5)>>>24) + srcV;
1387                    if (dstB > tmp) dstB = tmp;
1388
1389                    dstOutPixels[dstOutSp++] =
1390                        ((dstA<<24) | (dstR<<16) | (dstG<< 8) | dstB);
1391                }
1392                srcSp += srcAdjust;
1393                dstInSp += dstInAdjust;
1394                dstOutSp += dstOutAdjust;
1395            }
1396        }
1397    }
1398
1399    public static class LightenCompositeContext
1400        extends AlphaPreCompositeContext {
1401
1402        LightenCompositeContext(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
1403            super(srcCM, dstCM);
1404        }
1405
1406        public void precompose(Raster JavaDoc src, Raster JavaDoc dstIn,
1407                               WritableRaster JavaDoc dstOut) {
1408            int [] srcPix = null;
1409            int [] dstPix = null;
1410
1411            int x=dstOut.getMinX();
1412            int w=dstOut.getWidth();
1413
1414            int y0=dstOut.getMinY();
1415            int y1=y0 + dstOut.getHeight();
1416
1417            final int norm = (1<<24)/255;
1418            final int pt5 = (1<<23);
1419
1420            int sp, srcM, dstM, t1, t2;
1421
1422            for (int y = y0; y<y1; y++) {
1423                srcPix = src.getPixels (x, y, w, 1, srcPix);
1424                dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
1425                sp = 0;
1426                final int end = w*4;
1427                while(sp<end) {
1428                    srcM = 255-dstPix[sp+3];
1429                    dstM = 255-srcPix[sp+3];
1430
1431                    t1 = ((srcM*srcPix[sp]*norm + pt5)>>>24) + dstPix[sp];
1432                    t2 = ((dstM*dstPix[sp]*norm + pt5)>>>24) + srcPix[sp];
1433                    if (t1 > t2) dstPix[sp] = t1;
1434                    else dstPix[sp] = t2;
1435                    ++sp;
1436
1437                    t1 = ((srcM*srcPix[sp]*norm + pt5)>>>24) + dstPix[sp];
1438                    t2 = ((dstM*dstPix[sp]*norm + pt5)>>>24) + srcPix[sp];
1439                    if (t1 > t2) dstPix[sp] = t1;
1440                    else dstPix[sp] = t2;
1441                    ++sp;
1442
1443                    t1 = ((srcM*srcPix[sp]*norm + pt5)>>>24) + dstPix[sp];
1444                    t2 = ((dstM*dstPix[sp]*norm + pt5)>>>24) + srcPix[sp];
1445                    if (t1 > t2) dstPix[sp] = t1;
1446                    else dstPix[sp] = t2;
1447                    ++sp;
1448
1449                    dstPix[sp] = (srcPix[sp] + dstPix[sp] -
1450                                  ((dstPix[sp]*srcPix[sp]*norm + pt5)>>>24));
1451                    ++sp;
1452                }
1453                dstOut.setPixels(x, y, w, 1, dstPix);
1454            }
1455        }
1456    }
1457
1458    public static class LightenCompositeContext_INT_PACK
1459        extends AlphaPreCompositeContext_INT_PACK {
1460        LightenCompositeContext_INT_PACK(ColorModel JavaDoc srcCM, ColorModel JavaDoc dstCM) {
1461            super(srcCM, dstCM);
1462        }
1463
1464        public void precompose_INT_PACK
1465            (final int width, final int height,
1466             final int [] srcPixels, final int srcAdjust, int srcSp,
1467             final int [] dstInPixels, final int dstInAdjust, int dstInSp,
1468             final int [] dstOutPixels, final int dstOutAdjust, int dstOutSp) {
1469
1470            final int norm = (1<<24)/255;
1471            final int pt5 = (1<<23);
1472
1473            int srcP, srcM;
1474            int dstP, dstM, dstA, dstR, dstG, dstB;
1475
1476            int srcV, dstV, tmp;
1477
1478            for (int y = 0; y<height; y++) {
1479                final int end = dstOutSp+width;
1480                while (dstOutSp<end) {
1481                    srcP = srcPixels [srcSp++];
1482                    dstP = dstInPixels[dstInSp++];
1483
1484                    srcV = (srcP>>>24);
1485                    dstV = (dstP>>>24);
1486                    srcM = (255-dstV)*norm;
1487                    dstM = (255-srcV)*norm;
1488                    dstA = (srcV + dstV - ((srcV*dstV*norm + pt5)>>>24));
1489
1490                    srcV = (srcP>> 16)&0xFF;
1491                    dstV = (dstP>> 16)&0xFF;
1492                    dstR = ((srcM*srcV + pt5)>>>24) + dstV;
1493                    tmp = ((dstM*dstV + pt5)>>>24) + srcV;
1494                    if (dstR < tmp) dstR = tmp;
1495                    
1496                    srcV = (srcP>> 8)&0xFF;
1497                    dstV = (dstP>> 8)&0xFF;
1498                    dstG = ((srcM*srcV + pt5)>>>24) + dstV;
1499                    tmp = ((dstM*dstV + pt5)>>>24) + srcV;
1500                    if (dstG < tmp) dstG = tmp;
1501                    
1502
1503                    srcV = (srcP )&0xFF;
1504                    dstV = (dstP )&0xFF;
1505                    dstB = ((srcM*srcV + pt5)>>>24) + dstV;
1506                    tmp = ((dstM*dstV + pt5)>>>24) + srcV;
1507                    if (dstB < tmp) dstB = tmp;
1508
1509                    dstOutPixels[dstOutSp++] =
1510                        ((dstA<<24) | (dstR<<16) | (dstG<< 8) | dstB);
1511                }
1512                srcSp += srcAdjust;
1513                dstInSp += dstInAdjust;
1514                dstOutSp += dstOutAdjust;
1515            }
1516        }
1517    }
1518
1519}
1520
Popular Tags