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 ColorDodgeComposite extends RGBComposite { 042 043 public ColorDodgeComposite( 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 if (sr != 255) 071 dor = Math.min((dir << 8) / (255-sr), 255); 072 else 073 dor = sr; 074 if (sg != 255) 075 dog = Math.min((dig << 8) / (255-sg), 255); 076 else 077 dog = sg; 078 if (sb != 255) 079 dob = Math.min((dib << 8) / (255-sb), 255); 080 else 081 dob = sb; 082 083 float a = alpha*sa/255f; 084 float ac = 1-a; 085 086 dst[i] = (int)(a*dor + ac*dir); 087 dst[i+1] = (int)(a*dog + ac*dig); 088 dst[i+2] = (int)(a*dob + ac*dib); 089 dst[i+3] = (int)(sa*alpha + dia*ac); 090 } 091 } 092 } 093 094}