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