View Javadoc

1   /*
2    * VectorGraphics2D: Vector export for Java(R) Graphics2D
3    *
4    * (C) Copyright 2010 Erich Seifert <dev[at]erichseifert.de>
5    *
6    * This file is part of VectorGraphics2D.
7    *
8    * VectorGraphics2D is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU Lesser General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or
11   * (at your option) any later version.
12   *
13   * VectorGraphics2D is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with VectorGraphics2D.  If not, see <http://www.gnu.org/licenses/>.
20   */
21  
22  package de.erichseifert.vectorgraphics2d;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertFalse;
26  import static org.junit.Assert.assertNotNull;
27  import static org.junit.Assert.assertTrue;
28  
29  import java.awt.Image;
30  import java.awt.Toolkit;
31  import java.awt.image.BufferedImage;
32  import java.awt.image.FilteredImageSource;
33  import java.awt.image.RGBImageFilter;
34  
35  import org.junit.Test;
36  
37  
38  public class GraphicsUtilsTest {
39  
40  	@Test
41  	public void testToBufferedImage() {
42  		Image[] images = {
43  			new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB),
44  			new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB),
45  			Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(
46  				new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB).getSource(),
47  				new RGBImageFilter() {
48  					@Override
49  					public int filterRGB(int x, int y, int rgb) {
50  						return rgb & 0xff;
51  					}
52  				}
53  			))
54  		};
55  
56  		for (Image image : images) {
57  			BufferedImage bimage = GraphicsUtils.toBufferedImage(image);
58  			assertNotNull(bimage);
59  			assertEquals(BufferedImage.class, bimage.getClass());
60  			assertEquals(image.getWidth(null), bimage.getWidth());
61  			assertEquals(image.getHeight(null), bimage.getHeight());
62  		}
63  	}
64  
65  	@Test
66  	public void testHasAlpha() {
67  		Image image;
68  
69  		image = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
70  		assertTrue(GraphicsUtils.hasAlpha(image));
71  
72  		image = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB);
73  		assertFalse(GraphicsUtils.hasAlpha(image));
74  	}
75  
76  }