001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: /**
020: * @author Oleg V. Khaschansky
021: * @version $Revision$
022: */package java.awt.image;
023:
024: import junit.framework.TestCase;
025:
026: import java.awt.geom.AffineTransform;
027: import java.awt.*;
028:
029: public class BufferedImageFilterTest extends TestCase {
030: private BufferedImageFilter filter;
031: private BufferedImageOp op;
032:
033: private BufferedImageFilter lookupFilter;
034: private LookupOp lop;
035:
036: public static void main(String[] args) {
037: junit.textui.TestRunner.run(BufferedImageFilterTest.class);
038: }
039:
040: public BufferedImageFilterTest(String name) {
041: super (name);
042: }
043:
044: @Override
045: protected void setUp() throws Exception {
046: super .setUp();
047:
048: //float kernel[] = {0.1f, 0.1f, 0.1f, 0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.1f};
049: op = new AffineTransformOp(AffineTransform
050: .getRotateInstance(Math.PI / 4.), null);
051: filter = new BufferedImageFilter(op);
052:
053: byte lut[] = new byte[256];
054: for (int i = 0; i < lut.length; i++) {
055: lut[i] = (byte) (255 - i);
056: }
057: ByteLookupTable blut = new ByteLookupTable(0, lut);
058: lop = new LookupOp(blut, null);
059: lookupFilter = new BufferedImageFilter(lop);
060: }
061:
062: public void testBufferedImageFilter() throws Exception {
063: BufferedImageOp op = new AffineTransformOp(AffineTransform
064: .getTranslateInstance(0, 0),
065: AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
066:
067: new BufferedImageFilter(op);
068:
069: try {
070: new BufferedImageFilter(null);
071: fail("Should throw NullPointerException, but was not.");
072: } catch (NullPointerException e) {
073: // Expected
074: }
075: }
076:
077: public void testGetBufferedImageOp() throws Exception {
078: assertEquals(op, filter.getBufferedImageOp());
079: }
080:
081: public void testFilterRGB() throws Exception {
082: // Create source
083: BufferedImage im = new BufferedImage(10, 10,
084: BufferedImage.TYPE_INT_RGB);
085: for (int i = 0; i < 100; i++) {
086: im.setRGB(i % 10, i / 10, i + ((i + 1) << 8)
087: + ((i + 2) << 16));
088: }
089: // Create filtered image
090: Image img = Toolkit.getDefaultToolkit().createImage(
091: new FilteredImageSource(im.getSource(), lookupFilter));
092: BufferedImage dstIm = new BufferedImage(10, 10,
093: BufferedImage.TYPE_INT_RGB);
094: dstIm.getGraphics().drawImage(img, 0, 0, null);
095: // Check the result
096: for (int i = 0; i < 100; i++) {
097: int k = 255 - i;
098: int rgb = dstIm.getRGB(i % 10, i / 10);
099: assertEquals(k, rgb & 0xFF);
100: assertEquals(k - 1, (rgb >> 8) & 0xFF);
101: assertEquals(k - 2, (rgb >> 16) & 0xFF);
102: }
103: }
104:
105: public void testFilterByteIndexed() throws Exception {
106: BufferedImage im = new BufferedImage(20, 20,
107: BufferedImage.TYPE_BYTE_INDEXED);
108: for (int i = 0; i < 400; i++) {
109: im.getRaster().setPixel(i % 20, i / 20, new int[] { i });
110: }
111: Image img = Toolkit.getDefaultToolkit().createImage(
112: new FilteredImageSource(im.getSource(), filter));
113:
114: BufferedImage expIm = op.filter(im, null);
115: BufferedImage wasIm = op.filter(im, null);
116: wasIm.getGraphics().drawImage(img, 0, 0, null);
117:
118: int idata[] = null;
119: int exp[] = expIm.getRaster().getPixels(0, 0, expIm.getWidth(),
120: expIm.getHeight(), idata);
121: int was[] = wasIm.getRaster().getPixels(0, 0, wasIm.getWidth(),
122: wasIm.getHeight(), idata);
123: for (int i = 0; i < exp.length; i++) {
124: assertEquals(exp[i], was[i]);
125: }
126: }
127:
128: public void testFilterByte() throws Exception {
129: BufferedImage im = new BufferedImage(20, 20,
130: BufferedImage.TYPE_3BYTE_BGR);
131: for (int i = 0; i < 400; i++) {
132: im.getRaster().setPixel(i % 20, i / 20,
133: new int[] { i, i + 1, i + 2 });
134: }
135: Image img = Toolkit.getDefaultToolkit().createImage(
136: new FilteredImageSource(im.getSource(), filter));
137:
138: BufferedImage expIm = op.filter(im, null);
139: BufferedImage wasIm = op.filter(im, null);
140: wasIm.getGraphics().drawImage(img, 0, 0, null);
141:
142: int idata[] = null;
143: int exp[] = expIm.getRaster().getPixels(0, 0, expIm.getWidth(),
144: expIm.getHeight(), idata);
145: int was[] = wasIm.getRaster().getPixels(0, 0, wasIm.getWidth(),
146: wasIm.getHeight(), idata);
147: for (int i = 0; i < exp.length; i++) {
148: assertEquals(exp[i], was[i]);
149: }
150: }
151: }
|