001/* 002Copyright 2006 Jerry Huxtable 003 004Licensed under the Apache License, Version 2.0 (the "License"); 005you may not use this file except in compliance with the License. 006You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010Unless required by applicable law or agreed to in writing, software 011distributed under the License is distributed on an "AS IS" BASIS, 012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013See the License for the specific language governing permissions and 014limitations under the License. 015*/ 016 017package com.jhlabs.image; 018 019import java.awt.*; 020import java.awt.image.*; 021 022/** 023 * A Filter to draw grids and check patterns. 024 */ 025public class CheckFilter extends PointFilter { 026 027 private int xScale = 8; 028 private int yScale = 8; 029 private int foreground = 0xffffffff; 030 private int background = 0xff000000; 031 private int fuzziness = 0; 032 private float angle = 0.0f; 033 private float m00 = 1.0f; 034 private float m01 = 0.0f; 035 private float m10 = 0.0f; 036 private float m11 = 1.0f; 037 038 public CheckFilter() { 039 } 040 041 /** 042 * Set the foreground color. 043 * @param foreground the color. 044 * @see #getForeground 045 */ 046 public void setForeground(int foreground) { 047 this.foreground = foreground; 048 } 049 050 /** 051 * Get the foreground color. 052 * @return the color. 053 * @see #setForeground 054 */ 055 public int getForeground() { 056 return foreground; 057 } 058 059 /** 060 * Set the background color. 061 * @param background the color. 062 * @see #getBackground 063 */ 064 public void setBackground(int background) { 065 this.background = background; 066 } 067 068 /** 069 * Get the background color. 070 * @return the color. 071 * @see #setBackground 072 */ 073 public int getBackground() { 074 return background; 075 } 076 077 /** 078 * Set the X scale of the texture. 079 * @param xScale the scale. 080 * @see #getXScale 081 */ 082 public void setXScale(int xScale) { 083 this.xScale = xScale; 084 } 085 086 /** 087 * Get the X scale of the texture. 088 * @return the scale. 089 * @see #setXScale 090 */ 091 public int getXScale() { 092 return xScale; 093 } 094 095 /** 096 * Set the Y scale of the texture. 097 * @param yScale the scale. 098 * @see #getYScale 099 */ 100 public void setYScale(int yScale) { 101 this.yScale = yScale; 102 } 103 104 /** 105 * Get the Y scale of the texture. 106 * @return the scale. 107 * @see #setYScale 108 */ 109 public int getYScale() { 110 return yScale; 111 } 112 113 /** 114 * Set the fuzziness of the texture. 115 * @param fuzziness the fuzziness. 116 * @see #getFuzziness 117 */ 118 public void setFuzziness(int fuzziness) { 119 this.fuzziness = fuzziness; 120 } 121 122 /** 123 * Get the fuzziness of the texture. 124 * @return the fuzziness. 125 * @see #setFuzziness 126 */ 127 public int getFuzziness() { 128 return fuzziness; 129 } 130 131 /** 132 * Set the angle of the texture. 133 * @param angle the angle of the texture. 134 * @angle 135 * @see #getAngle 136 */ 137 public void setAngle(float angle) { 138 this.angle = angle; 139 float cos = (float)Math.cos(angle); 140 float sin = (float)Math.sin(angle); 141 m00 = cos; 142 m01 = sin; 143 m10 = -sin; 144 m11 = cos; 145 } 146 147 /** 148 * Get the angle of the texture. 149 * @return the angle of the texture. 150 * @see #setAngle 151 */ 152 public float getAngle() { 153 return angle; 154 } 155 156 public int filterRGB(int x, int y, int rgb) { 157 float nx = (m00*x + m01*y) / xScale; 158 float ny = (m10*x + m11*y) / yScale; 159 float f = ((int)(nx+100000) % 2 != (int)(ny+100000) % 2) ? 1.0f : 0.0f; 160 if (fuzziness != 0) { 161 float fuzz = (fuzziness/100.0f); 162 float fx = ImageMath.smoothPulse(0, fuzz, 1-fuzz, 1, ImageMath.mod(nx, 1)); 163 float fy = ImageMath.smoothPulse(0, fuzz, 1-fuzz, 1, ImageMath.mod(ny, 1)); 164 f *= fx*fy; 165 } 166 return ImageMath.mixColors(f, foreground, background); 167 } 168 169 public String toString() { 170 return "Texture/Checkerboard..."; 171 } 172} 173