001    /*
002    *
003    
004    Licensed under the Apache License, Version 2.0 (the "License");
005    you may not use this file except in compliance with the License.
006    You may obtain a copy of the License at
007    
008       http://www.apache.org/licenses/LICENSE-2.0
009    
010    Unless required by applicable law or agreed to in writing, software
011    distributed under the License is distributed on an "AS IS" BASIS,
012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    See the License for the specific language governing permissions and
014    limitations under the License.
015    */
016    
017    package railo.runtime.img.composite;
018    
019    import java.awt.AlphaComposite;
020    import java.awt.Composite;
021    import java.awt.CompositeContext;
022    import java.awt.RenderingHints;
023    import java.awt.image.ColorModel;
024    
025    public final class MiscComposite implements Composite {
026    
027            public final static int BLEND = 0;
028            public final static int ADD = 1;
029            public final static int SUBTRACT = 2;
030            public final static int DIFFERENCE = 3;
031    
032            public final static int MULTIPLY = 4;
033            public final static int DARKEN = 5;
034            public final static int BURN = 6;
035            public final static int COLOR_BURN = 7;
036    
037            public final static int SCREEN = 8;
038            public final static int LIGHTEN = 9;
039            public final static int DODGE = 10;
040            public final static int COLOR_DODGE = 11;
041    
042            public final static int HUE = 12;
043            public final static int SATURATION = 13;
044            public final static int VALUE = 14;
045            public final static int COLOR = 15;
046    
047            public final static int OVERLAY = 16;
048            public final static int SOFT_LIGHT = 17;
049            public final static int HARD_LIGHT = 18;
050            public final static int PIN_LIGHT = 19;
051    
052            public final static int EXCLUSION = 20;
053            public final static int NEGATION = 21;
054            public final static int AVERAGE = 22;
055    
056            public final static int STENCIL = 23;
057            public final static int SILHOUETTE = 24;
058    
059            private static final int MIN_RULE = BLEND;
060            private static final int MAX_RULE = SILHOUETTE;
061    
062            public static String[] RULE_NAMES = {
063                    "Normal",
064                    "Add",
065                    "Subtract",
066                    "Difference",
067    
068                    "Multiply",
069                    "Darken",
070                    "Burn",
071                    "Color Burn",
072                    
073                    "Screen",
074                    "Lighten",
075                    "Dodge",
076                    "Color Dodge",
077    
078                    "Hue",
079                    "Saturation",
080                    "Brightness",
081                    "Color",
082                    
083                    "Overlay",
084                    "Soft Light",
085                    "Hard Light",
086                    "Pin Light",
087    
088                    "Exclusion",
089                    "Negation",
090                    "Average",
091    
092                    "Stencil",
093                    "Silhouette",
094            };
095    
096            protected float extraAlpha;
097            protected int rule;
098    
099            private MiscComposite(int rule) {
100                    this(rule, 1.0f);
101            }
102    
103            private MiscComposite(int rule, float alpha) {
104                    if (alpha < 0.0f || alpha > 1.0f)
105                            throw new IllegalArgumentException("alpha value out of range");
106                    if (rule < MIN_RULE || rule > MAX_RULE)
107                            throw new IllegalArgumentException("unknown composite rule");
108                    this.rule = rule;
109                    this.extraAlpha = alpha;
110            }
111    
112            public static Composite getInstance(int rule, float alpha) {
113                    switch ( rule ) {
114                    case MiscComposite.BLEND:
115                            return AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha );
116                    case MiscComposite.ADD:
117                            return new AddComposite( alpha );
118                    case MiscComposite.SUBTRACT:
119                            return new SubtractComposite( alpha );
120                    case MiscComposite.DIFFERENCE:
121                            return new DifferenceComposite( alpha );
122                    case MiscComposite.MULTIPLY:
123                            return new MultiplyComposite( alpha );
124                    case MiscComposite.DARKEN:
125                            return new DarkenComposite( alpha );
126                    case MiscComposite.BURN:
127                            return new BurnComposite( alpha );
128                    case MiscComposite.COLOR_BURN:
129                            return new ColorBurnComposite( alpha );
130                    case MiscComposite.SCREEN:
131                            return new ScreenComposite( alpha );
132                    case MiscComposite.LIGHTEN:
133                            return new LightenComposite( alpha );
134                    case MiscComposite.DODGE:
135                            return new DodgeComposite( alpha );
136                    case MiscComposite.COLOR_DODGE:
137                            return new ColorDodgeComposite( alpha );
138                    case MiscComposite.HUE:
139                            return new HueComposite( alpha );
140                    case MiscComposite.SATURATION:
141                            return new SaturationComposite( alpha );
142                    case MiscComposite.VALUE:
143                            return new ValueComposite( alpha );
144                    case MiscComposite.COLOR:
145                            return new ColorComposite( alpha );
146                    case MiscComposite.OVERLAY:
147                            return new OverlayComposite( alpha );
148                    case MiscComposite.SOFT_LIGHT:
149                            return new SoftLightComposite( alpha );
150                    case MiscComposite.HARD_LIGHT:
151                            return new HardLightComposite( alpha );
152                    case MiscComposite.PIN_LIGHT:
153                            return new PinLightComposite( alpha );
154                    case MiscComposite.EXCLUSION:
155                            return new ExclusionComposite( alpha );
156                    case MiscComposite.NEGATION:
157                            return new NegationComposite( alpha );
158                    case MiscComposite.AVERAGE:
159                            return new AverageComposite( alpha );
160                    case MiscComposite.STENCIL:
161                            return AlphaComposite.getInstance( AlphaComposite.DST_IN, alpha );
162                    case MiscComposite.SILHOUETTE:
163                            return AlphaComposite.getInstance( AlphaComposite.DST_OUT, alpha );
164                    }
165                    return new MiscComposite(rule, alpha);
166            }
167    
168            public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints) {
169                    return new MiscCompositeContext( rule, extraAlpha, srcColorModel, dstColorModel );
170            }
171    
172            public float getAlpha() {
173                    return extraAlpha;
174            }
175    
176            public int getRule() {
177                    return rule;
178            }
179    
180            public int hashCode() {
181                    return (Float.floatToIntBits(extraAlpha) * 31 + rule);
182            }
183    
184            public boolean equals(Object o) {
185                    if (!(o instanceof MiscComposite))
186                            return false;
187                    MiscComposite c = (MiscComposite)o;
188    
189                    if (rule != c.rule)
190                            return false;
191                    if (extraAlpha != c.extraAlpha)
192                            return false;
193                    return true;
194            }
195                            
196    }