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 **/
019package lucee.runtime.img.composite;
020
021import java.awt.CompositeContext;
022import java.awt.RenderingHints;
023import java.awt.image.ColorModel;
024
025public final class AddComposite extends RGBComposite {
026
027        public AddComposite( float alpha ) {
028        super( alpha );
029        }
030
031        public CompositeContext createContext( ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints ) {
032                return new Context( extraAlpha, srcColorModel, dstColorModel );
033        }
034
035    static class Context extends RGBCompositeContext {
036        public Context( float alpha, ColorModel srcColorModel, ColorModel dstColorModel ) {
037            super( alpha, srcColorModel, dstColorModel );
038        }
039
040        public void composeRGB( int[] src, int[] dst, float alpha ) {
041            int w = src.length;
042
043            for ( int i = 0; i < w; i += 4 ) {
044                int sr = src[i];
045                int dir = dst[i];
046                int sg = src[i+1];
047                int dig = dst[i+1];
048                int sb = src[i+2];
049                int dib = dst[i+2];
050                int sa = src[i+3];
051                int dia = dst[i+3];
052                int dor, dog, dob;
053
054                dor = dir + sr;
055                if ( dor > 255 )
056                    dor = 255;
057                dog = dig + sg;
058                if ( dog > 255 )
059                    dog = 255;
060                dob = dib + sb;
061                if ( dob > 255 )
062                    dob = 255;
063
064                float a = alpha*sa/255f;
065                float ac = 1-a;
066
067                dst[i] = (int)(a*dor + ac*dir);
068                dst[i+1] = (int)(a*dog + ac*dig);
069                dst[i+2] = (int)(a*dob + ac*dib);
070                dst[i+3] = (int)(sa*alpha + dia*ac);
071            }
072        }
073    }
074
075}