1 |
| package photospace.graphics; |
2 |
| |
3 |
| import java.awt.*; |
4 |
| import java.awt.image.*; |
5 |
| import java.io.*; |
6 |
| import org.apache.commons.logging.*; |
7 |
| import junit.framework.*; |
8 |
| |
9 |
| public class SamplerTest |
10 |
| extends TestCase |
11 |
| { |
12 |
| private static final Log log = LogFactory.getLog(SamplerTest.class); |
13 |
| |
14 |
1
| public void testRotate() throws Exception
|
15 |
| { |
16 |
1
| File file = new File(System.getProperty("project.root"), "build/test/exif-nordf.jpg");
|
17 |
1
| BufferedImage original = Sampler.read(file);
|
18 |
| |
19 |
1
| BufferedImage rotated = Sampler.rotate(original, 90);
|
20 |
| |
21 |
1
| assertEquals(original.getWidth(), rotated.getHeight(), 1);
|
22 |
1
| assertEquals(original.getHeight(), rotated.getWidth(), 1);
|
23 |
| |
24 |
1
| write("rotated-90.jpg", rotated);
|
25 |
| } |
26 |
| |
27 |
1
| public void testScaleToFit() throws Exception
|
28 |
| { |
29 |
1
| File file = new File(System.getProperty("project.root"), "build/test/exif-nordf.jpg");
|
30 |
1
| BufferedImage original = Sampler.read(file);
|
31 |
1
| assertNotNull(original);
|
32 |
1
| Dimension max = new Dimension(200, 100);
|
33 |
1
| Transform transform = new Transform(max, Transform.FIT);
|
34 |
1
| BufferedImage scaled = Sampler.scale(original, transform);
|
35 |
1
| assertEquals(original.getWidth()/(double) original.getHeight(),
|
36 |
| scaled.getWidth()/(double) scaled.getHeight(), .1); |
37 |
1
| assertTrue(max.getWidth() == scaled.getWidth() || max.getHeight() == scaled.getHeight());
|
38 |
1
| assertTrue(max.getWidth() >= scaled.getWidth());
|
39 |
1
| assertTrue(max.getHeight() >= scaled.getHeight());
|
40 |
| |
41 |
1
| write("scaleToFit.jpg", scaled);
|
42 |
| } |
43 |
| |
44 |
1
| public void testScaleToFill() throws Exception
|
45 |
| { |
46 |
1
| File file = new File(System.getProperty("project.root"), "build/test/exif-nordf.jpg");
|
47 |
1
| BufferedImage original = Sampler.read(file);
|
48 |
1
| assertNotNull(original);
|
49 |
1
| Dimension max = new Dimension(200, 100);
|
50 |
1
| Transform transform = new Transform(max, Transform.FILL);
|
51 |
1
| BufferedImage scaled = Sampler.scale(original, transform);
|
52 |
1
| assertEquals(max.getWidth(), scaled.getWidth(), .1);
|
53 |
1
| assertEquals(max.getHeight(), scaled.getHeight(), .1);
|
54 |
| |
55 |
1
| write("scaleToFill.jpg", scaled);
|
56 |
| } |
57 |
| |
58 |
1
| public void testGetBytes() throws Exception
|
59 |
| { |
60 |
| |
61 |
1
| File file = new File(System.getProperty("project.root"), "build/test/exif-nordf.jpg");
|
62 |
1
| BufferedImage image = Sampler.read(file);
|
63 |
1
| byte [] bytes = Sampler.getBytes(image);
|
64 |
1
| assertNotNull(bytes);
|
65 |
1
| assertTrue(bytes.length > 0);
|
66 |
| } |
67 |
| |
68 |
3
| private void write(String filename, BufferedImage image) throws IOException
|
69 |
| { |
70 |
3
| Sampler.write(image, new File(System.getProperty("java.io.tmpdir"), filename));
|
71 |
| } |
72 |
| |
73 |
| |
74 |
| } |