001 /* 002 * 003 004 Licensed under the Apache License, Version 2.0 (the "License"); 005 you may not use this file except in compliance with the License. 006 You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010 Unless required by applicable law or agreed to in writing, software 011 distributed under the License is distributed on an "AS IS" BASIS, 012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 See the License for the specific language governing permissions and 014 limitations under the License. 015 */ 016 017 package railo.runtime.img.filter;import java.awt.Image; 018 import java.awt.image.ImageObserver; 019 import java.awt.image.ImageProducer; 020 import java.awt.image.MemoryImageSource; 021 import java.awt.image.PixelGrabber; 022 023 public class ImageCombiningFilter { 024 025 public int filterRGB(int x, int y, int rgb1, int rgb2) { 026 int a1 = (rgb1 >> 24) & 0xff; 027 int r1 = (rgb1 >> 16) & 0xff; 028 //int g1 = (rgb1 >> 8) & 0xff; 029 //int b1 = rgb1 & 0xff; 030 //int a2 = (rgb2 >> 24) & 0xff; 031 int r2 = (rgb2 >> 16) & 0xff; 032 //int g2 = (rgb2 >> 8) & 0xff; 033 //int b2 = rgb2 & 0xff; 034 int r = PixelUtils.clamp(r1 + r2); 035 int g = PixelUtils.clamp(r1 + r2); 036 int b = PixelUtils.clamp(r1 + r2); 037 return (a1 << 24) | (r << 16) | (g << 8) | b; 038 } 039 040 public ImageProducer filter(Image image1, Image image2, int x, int y, int w, int h) { 041 int[] pixels1 = new int[w * h]; 042 int[] pixels2 = new int[w * h]; 043 int[] pixels3 = new int[w * h]; 044 PixelGrabber pg1 = new PixelGrabber(image1, x, y, w, h, pixels1, 0, w); 045 PixelGrabber pg2 = new PixelGrabber(image2, x, y, w, h, pixels2, 0, w); 046 try { 047 pg1.grabPixels(); 048 pg2.grabPixels(); 049 } catch (InterruptedException e) { 050 System.err.println("interrupted waiting for pixels!"); 051 return null; 052 } 053 if ((pg1.status() & ImageObserver.ABORT) != 0) { 054 System.err.println("image fetch aborted or errored"); 055 return null; 056 } 057 if ((pg2.status() & ImageObserver.ABORT) != 0) { 058 System.err.println("image fetch aborted or errored"); 059 return null; 060 } 061 062 for (int j = 0; j < h; j++) { 063 for (int i = 0; i < w; i++) { 064 int k = j * w + i; 065 pixels3[k] = filterRGB(x+i, y+j, pixels1[k], pixels2[k]); 066 } 067 } 068 return new MemoryImageSource(w, h, pixels3, 0, w); 069 } 070 }