01: /*
02: GNU General Public License
03:
04: RGBGreyscaleFilter
05: Copyright (C) 2000 Howard A Kistler
06:
07: This program is free software; you can redistribute it and/or
08: modify it under the terms of the GNU General Public License
09: as published by the Free Software Foundation; either version 2
10: of the License, or (at your option) any later version.
11:
12: This program is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU General Public License for more details.
16:
17: You should have received a copy of the GNU General Public License
18: along with this program; if not, write to the Free Software
19: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package com.hexidec.filter;
23:
24: import java.awt.Color;
25: import java.awt.Image;
26: import java.awt.Toolkit;
27: import java.awt.image.FilteredImageSource;
28: import java.awt.image.ImageFilter;
29: import java.awt.image.ImageProducer;
30: import java.awt.image.RGBImageFilter;
31:
32: /** RGBGreyscaleFilter
33: * Creates a greyscale version of an image
34: *
35: * @author Howard Kistler
36: * @version 0.1
37: *
38: * REQUIREMENTS
39: * Java 2 (JDK 1.3)
40: *
41: * VERSION HISTORY
42: * 0.1 (10/31/2000) - initial creation (10/31/2000)
43: */
44:
45: public class RGBGreyscaleFilter extends RGBImageFilter {
46:
47: // Constructors ---------------------------------------------------------------------------->
48:
49: public RGBGreyscaleFilter() {
50: canFilterIndexColorModel = true;
51: }
52:
53: // Image Processing Methods ---------------------------------------------------------------->
54:
55: /** Modify and return a greyscale version of the image
56: */
57: public Image procImage(Image source) {
58: ImageProducer imageProducer = new FilteredImageSource(source
59: .getSource(), this );
60: Image recoloredImage = Toolkit.getDefaultToolkit().createImage(
61: imageProducer);
62: return recoloredImage;
63: }
64:
65: // Image Filtering Method ------------------------------------------------------------------>
66:
67: /** Called by the filter upon each pixel to determine its greyscale equivalent
68: * Conversion formula -> AVG(r & g & b)
69: */
70: public int filterRGB(int x, int y, int rgb) {
71: int alpha = (rgb >> 24) & 0xFF;
72: int red = (rgb >> 16) & 0xFF;
73: int green = (rgb >> 8) & 0xFF;
74: int blue = (rgb) & 0xFF;
75: /*
76: // Conversion formula -> luminance = .299 red + .587 green + .114 blue (luminance results are flakey)
77: float rGrey = 0.299f * (red / 255);
78: float gGrey = 0.587f * (green / 255);
79: float bGrey = 0.114f * (blue / 255);
80: float luminance = rGrey + gGrey + bGrey;
81: if(luminance > 1.0f) { luminance = 1.0f; }
82: */
83: int brightness = (int) ((red + green + blue) / 3);
84: Color grey = new Color(brightness, brightness, brightness);
85: rgb = grey.getRGB();
86: return (rgb & 0x00FFFFFF) | (alpha << 24);
87: }
88:
89: }
|