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.filter;import java.awt.Image; 036import java.awt.image.ImageObserver; 037import java.awt.image.ImageProducer; 038import java.awt.image.MemoryImageSource; 039import java.awt.image.PixelGrabber; 040 041public class ImageCombiningFilter { 042 043 public int filterRGB(int x, int y, int rgb1, int rgb2) { 044 int a1 = (rgb1 >> 24) & 0xff; 045 int r1 = (rgb1 >> 16) & 0xff; 046 //int g1 = (rgb1 >> 8) & 0xff; 047 //int b1 = rgb1 & 0xff; 048 //int a2 = (rgb2 >> 24) & 0xff; 049 int r2 = (rgb2 >> 16) & 0xff; 050 //int g2 = (rgb2 >> 8) & 0xff; 051 //int b2 = rgb2 & 0xff; 052 int r = PixelUtils.clamp(r1 + r2); 053 int g = PixelUtils.clamp(r1 + r2); 054 int b = PixelUtils.clamp(r1 + r2); 055 return (a1 << 24) | (r << 16) | (g << 8) | b; 056 } 057 058 public ImageProducer filter(Image image1, Image image2, int x, int y, int w, int h) { 059 int[] pixels1 = new int[w * h]; 060 int[] pixels2 = new int[w * h]; 061 int[] pixels3 = new int[w * h]; 062 PixelGrabber pg1 = new PixelGrabber(image1, x, y, w, h, pixels1, 0, w); 063 PixelGrabber pg2 = new PixelGrabber(image2, x, y, w, h, pixels2, 0, w); 064 try { 065 pg1.grabPixels(); 066 pg2.grabPixels(); 067 } catch (InterruptedException e) { 068 System.err.println("interrupted waiting for pixels!"); 069 return null; 070 } 071 if ((pg1.status() & ImageObserver.ABORT) != 0) { 072 System.err.println("image fetch aborted or errored"); 073 return null; 074 } 075 if ((pg2.status() & ImageObserver.ABORT) != 0) { 076 System.err.println("image fetch aborted or errored"); 077 return null; 078 } 079 080 for (int j = 0; j < h; j++) { 081 for (int i = 0; i < w; i++) { 082 int k = j * w + i; 083 pixels3[k] = filterRGB(x+i, y+j, pixels1[k], pixels2[k]); 084 } 085 } 086 return new MemoryImageSource(w, h, pixels3, 0, w); 087 } 088}