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.Color;
038import java.awt.CompositeContext;
039import java.awt.RenderingHints;
040import java.awt.image.ColorModel;
041
042public final class SaturationComposite extends RGBComposite {
043
044        public SaturationComposite( float alpha ) {
045        super( alpha );
046        }
047
048        public CompositeContext createContext( ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints ) {
049                return new Context( extraAlpha, srcColorModel, dstColorModel );
050        }
051
052    static class Context extends RGBCompositeContext {
053                private float[] sHSB = new float[3];
054        private float[] dHSB = new float[3];
055
056        public Context( float alpha, ColorModel srcColorModel, ColorModel dstColorModel ) {
057            super( alpha, srcColorModel, dstColorModel );
058        }
059
060        public void composeRGB( int[] src, int[] dst, float alpha ) {
061            int w = src.length;
062
063            for ( int i = 0; i < w; i += 4 ) {
064                int sr = src[i];
065                int dir = dst[i];
066                int sg = src[i+1];
067                int dig = dst[i+1];
068                int sb = src[i+2];
069                int dib = dst[i+2];
070                int sa = src[i+3];
071                int dia = dst[i+3];
072                int dor, dog, dob;
073
074                Color.RGBtoHSB( sr, sg, sb, sHSB );
075                Color.RGBtoHSB( dir, dig, dib, dHSB );
076
077                dHSB[1] = sHSB[1];
078
079                int doRGB = Color.HSBtoRGB( dHSB[0], dHSB[1], dHSB[2] );
080                dor = (doRGB & 0xff0000) >> 16;
081                dog = (doRGB & 0xff00) >> 8;
082                dob = (doRGB & 0xff);
083
084                float a = alpha*sa/255f;
085                float ac = 1-a;
086
087                dst[i] = (int)(a*dor + ac*dir);
088                dst[i+1] = (int)(a*dog + ac*dig);
089                dst[i+2] = (int)(a*dob + ac*dib);
090                dst[i+3] = (int)(sa*alpha + dia*ac);
091            }
092        }
093    }
094
095}