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.CompositeContext;
038import java.awt.RenderingHints;
039import java.awt.image.ColorModel;
040
041public final class PinLightComposite extends RGBComposite {
042
043        public PinLightComposite( float alpha ) {
044        super( alpha );
045        }
046
047        public CompositeContext createContext( ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints ) {
048                return new Context( extraAlpha, srcColorModel, dstColorModel );
049        }
050
051    static class Context extends RGBCompositeContext {
052        public Context( float alpha, ColorModel srcColorModel, ColorModel dstColorModel ) {
053            super( alpha, srcColorModel, dstColorModel );
054        }
055
056        public void composeRGB( int[] src, int[] dst, float alpha ) {
057            int w = src.length;
058
059            for ( int i = 0; i < w; i += 4 ) {
060                int sr = src[i];
061                int dir = dst[i];
062                int sg = src[i+1];
063                int dig = dst[i+1];
064                int sb = src[i+2];
065                int dib = dst[i+2];
066                int sa = src[i+3];
067                int dia = dst[i+3];
068                int dor, dog, dob;
069
070                dor = sr > 127 ? Math.max(sr, dir) : Math.min(sr, dir);
071                dog = sg > 127 ? Math.max(sg, dig) : Math.min(sg, dig);
072                dob = sb > 127 ? Math.max(sb, dib) : Math.min(sb, dib);
073
074                float a = alpha*sa/255f;
075                float ac = 1-a;
076
077                dst[i] = (int)(a*dor + ac*dir);
078                dst[i+1] = (int)(a*dog + ac*dig);
079                dst[i+2] = (int)(a*dob + ac*dib);
080                dst[i+3] = (int)(sa*alpha + dia*ac);
081            }
082        }
083    }
084
085}