// Copyright 1997-2015 Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland // www.source-code.biz, www.inventec.ch/chdh // // This module is multi-licensed and may be used under the terms of any of the following licenses: // // LGPL, GNU Lesser General Public License, V2.1 or later, http://www.gnu.org/licenses/lgpl.html // EPL, Eclipse Public License, V1.0 or later, http://www.eclipse.org/legal // // Please contact the author if you need another license. // This module is provided "as is", without warranties of any kind. // // Home page: http://www.source-code.biz/snippets/java/1.htm import java.awt.image.ImageFilter; import java.awt.image.ColorModel; /** * An anti-aliasing image filter. * *

* This is an image filter class that can be used for anti-aliasing. * The input image is scaled down by the factor ScalingFactor. * The color value of an output pixel is computed by averaging the * color values of the corresponding input pixels. */ public class AntiAliasingFilter extends ImageFilter { private static final ColorModel rgbModel = ColorModel.getRGBdefault(); private int scalingFactor; private int scalingFactorSqr; private int srcWidth; private int srcHeight; private int destWidth; private int destHeight; private int expectedSrcY; private int accu[]; private int outLine[]; private void sendOutputLine (int destY) { consumer.setPixels(0, destY, destWidth, 1, rgbModel, outLine, 0, destWidth); } private void genOutputLine() { int p = 0; for (int i = 0; i>>= 8; accu[p+1] += rgb & 0xff; rgb >>>= 8; accu[p] += rgb & 0xff; } p += 3; } if (yMod == scalingFactor-1) { genOutputLine(); sendOutputLine(srcY / scalingFactor); }} private void procSetPixels (int x, int y, int w, int h, ColorModel model, byte pixelsB[], int pixelsI[], int offs0, int scansize) { if (x != 0 || w != srcWidth) { throw new IllegalArgumentException("Invalid x or w in setPixels."); } if (y != expectedSrcY) { throw new IllegalArgumentException("ExpectedSrcY does not match."); } int offs = offs0; for (int srcY=y; srcY