-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICNSWriterTest.java
118 lines (91 loc) · 3.58 KB
/
ICNSWriterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package de.pentabyte.imageio.icns;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import org.junit.Assert;
import org.junit.Test;
import de.pentabyte.imageio.icns.ICNSImageWriter;
import de.pentabyte.imageio.icns.ICNSWriteParam;
public class ICNSWriterTest {
@Test
public void test_registration() {
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(ICNS.NAME);
Assert.assertTrue(writers.hasNext());
}
@Test
public void createExample() throws IOException {
BufferedImage i = createExampleImage(64);
boolean success = ImageIO.write(i, ICNS.NAME, new FileOutputStream("src/test/resources/example.icns"));
Assert.assertTrue("no writer has been found", success);
}
@Test
public void test_file_type_ic09() throws IOException {
BufferedImage i = createExampleImage(512);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ICNSWriteParam param = new ICNSWriteParam();
param.setDevicePixelRatio(1);
ICNSImageWriter writer = lookupImageWriter();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(null, new IIOImage(i, null, null), param);
ios.close();
Assert.assertTrue(baos.toString().contains(OSType.ic09.name()));
}
@Test
public void test_file_type_ic14() throws IOException {
BufferedImage i = createExampleImage(512);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ICNSWriteParam param = new ICNSWriteParam();
param.setDevicePixelRatio(2);
ICNSImageWriter writer = lookupImageWriter();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(null, new IIOImage(i, null, null), param);
ios.close();
Assert.assertTrue(baos.toString().contains(OSType.ic14.name()));
}
@Test
public void test_file_type_icp4() throws IOException {
BufferedImage i = createExampleImage(16);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ICNSWriteParam param = new ICNSWriteParam();
ICNSImageWriter writer = lookupImageWriter();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(null, new IIOImage(i, null, null), param);
ios.close();
Assert.assertTrue(baos.toString().contains(OSType.icp4.name()));
}
public static ICNSImageWriter lookupImageWriter() {
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(ICNS.NAME);
return (ICNSImageWriter) writers.next();
}
public static BufferedImage createExampleImage(int width) {
BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.MAGENTA);
g.fill(new RoundRectangle2D.Float(0, 0, width, width, width / 3, width / 3));
g.setColor(Color.BLACK);
g.fill(new Ellipse2D.Double(width / 4, width / 4, width / 2, width / 2));
// ... then compositing the image on top,
// using the white shape from above as alpha source
g.setComposite(AlphaComposite.SrcAtop);
g.drawImage(image, 0, 0, null);
g.dispose();
return image;
}
}