Gridarta Editor
ColourFilter.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.face;
21 
22 import java.awt.image.RGBImageFilter;
23 import org.jetbrains.annotations.NotNull;
24 
29 public class ColourFilter extends RGBImageFilter {
30 
34  public static final int RED_MASK = 0x00FF0000;
35 
39  public static final int GREEN_MASK = 0x0000FF00;
40 
44  public static final int BLUE_MASK = 0x000000FF;
45 
49  public static final int RED_GREEN_BLUE_MASK = RED_MASK | GREEN_MASK | BLUE_MASK;
50 
54  private static final int ALPHA_MASK = 0xFF000000;
55 
59  private static final int RED_SHIFT = 16;
60 
64  private static final int GREEN_SHIFT = 8;
65 
69  private static final int BLUE_SHIFT = 0;
70 
74  private final int positiveMask;
75 
79  private final int negativeMask;
80 
85  public ColourFilter(final int mask) {
86  positiveMask = mask & RED_GREEN_BLUE_MASK;
87  negativeMask = ~mask & RED_GREEN_BLUE_MASK;
88  }
89 
93  @Override
94  public int filterRGB(final int x, final int y, final int rgb) {
95  final int alpha = rgb & ALPHA_MASK;
96  final float r = (float) (rgb >> RED_SHIFT & 0xFF);
97  final float g = (float) (rgb >> GREEN_SHIFT & 0xFF);
98  final float b = (float) (rgb >> BLUE_SHIFT & 0xFF);
99  int gray1 = (int) (0.66F * r + 0.66F * g + 0.66F * b);
100  int gray2 = gray1 - 256;
101  if (gray1 < 0) {
102  gray1 = 0;
103  }
104  if (gray1 > 255) {
105  gray1 = 255;
106  }
107  if (gray2 < 0) {
108  gray2 = 0;
109  }
110  if (gray2 > 255) {
111  gray2 = 255;
112  }
113  gray1 = gray1 << BLUE_SHIFT | gray1 << GREEN_SHIFT | gray1 << RED_SHIFT;
114  gray2 = gray2 << BLUE_SHIFT | gray2 << GREEN_SHIFT | gray2 << RED_SHIFT;
115 
116  return alpha | gray1 & positiveMask | gray2 & negativeMask;
117  }
118 
119  @NotNull
120  @Override
121  public Object clone() {
122  return super.clone();
123  }
124 
125 }
static final int BLUE_SHIFT
The bit-offset for the blue bits.
static final int GREEN_SHIFT
The bit-offset for the green bits.
int filterRGB(final int x, final int y, final int rgb)
Converts a pixel by applying an or operation.
final int positiveMask
The positive mask to apply.
ColourFilter(final int mask)
Create an ColourFilter.
static final int RED_GREEN_BLUE_MASK
The mask for selecting the red, green, and blue bits.
static final int GREEN_MASK
The mask for selecting the green bits.
final int negativeMask
The negative mask to apply.
static final int RED_MASK
The mask for selecting the red bits.
static final int ALPHA_MASK
The mask for selecting the alpha bits.
Class to filter images by simply applying a boolean OR operation.
static final int RED_SHIFT
The bit-offset for the red bits.
static final int BLUE_MASK
The mask for selecting the blue bits.