/**
*
* Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
**/
package lucee.runtime.img.composite;
import java.awt.CompositeContext;
import java.awt.RenderingHints;
import java.awt.image.ColorModel;
public final class AverageComposite extends RGBComposite {
public AverageComposite( float alpha ) {
super( alpha );
}
@Override
public CompositeContext createContext( ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints ) {
return new Context( extraAlpha, srcColorModel, dstColorModel );
}
static class Context extends RGBCompositeContext {
public Context( float alpha, ColorModel srcColorModel, ColorModel dstColorModel ) {
super( alpha, srcColorModel, dstColorModel );
}
@Override
public void composeRGB( int[] src, int[] dst, float alpha ) {
int w = src.length;
for ( int i = 0; i < w; i += 4 ) {
int sr = src[i];
int dir = dst[i];
int sg = src[i+1];
int dig = dst[i+1];
int sb = src[i+2];
int dib = dst[i+2];
int sa = src[i+3];
int dia = dst[i+3];
int dor, dog, dob;
dor = (dir + sr) / 2;
dog = (dig + sg) / 2;
dob = (dib + sb) / 2;
float a = alpha*sa/255f;
float ac = 1-a;
dst[i] = (int)(a*dor + ac*dir);
dst[i+1] = (int)(a*dog + ac*dig);
dst[i+2] = (int)(a*dob + ac*dib);
dst[i+3] = (int)(sa*alpha + dia*ac);
}
}
}
}