1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.icns;
18
19 import java.nio.charset.StandardCharsets;
20
21 enum IcnsType {
22
23 ICNS_16x12_1BIT_IMAGE_AND_MASK("icm#", 16, 12, 1, true), ICNS_16x12_4BIT_IMAGE("icm4", 16, 12, 4, false), ICNS_16x12_8BIT_IMAGE("icm8", 16, 12, 8, false),
24
25 ICNS_16x16_8BIT_MASK("s8mk", 16, 16, 8, true), ICNS_16x16_1BIT_IMAGE_AND_MASK("ics#", 16, 16, 1, true), ICNS_16x16_4BIT_IMAGE("ics4", 16, 16, 4, false),
26 ICNS_16x16_8BIT_IMAGE("ics8", 16, 16, 8, false), ICNS_16x16_32BIT_IMAGE("is32", 16, 16, 32, false),
27
28 ICNS_32x32_8BIT_MASK("l8mk", 32, 32, 8, true), ICNS_32x32_1BIT_IMAGE("ICON", 32, 32, 1, false), ICNS_32x32_1BIT_IMAGE_AND_MASK("ICN#", 32, 32, 1, true),
29 ICNS_32x32_4BIT_IMAGE("icl4", 32, 32, 4, false), ICNS_32x32_8BIT_IMAGE("icl8", 32, 32, 8, false), ICNS_32x32_32BIT_IMAGE("il32", 32, 32, 32, false),
30
31 ICNS_48x48_8BIT_MASK("h8mk", 48, 48, 8, true), ICNS_48x48_1BIT_IMAGE_AND_MASK("ich#", 48, 48, 1, true), ICNS_48x48_4BIT_IMAGE("ich4", 48, 48, 4, false),
32 ICNS_48x48_8BIT_IMAGE("ich8", 48, 48, 8, false), ICNS_48x48_32BIT_IMAGE("ih32", 48, 48, 32, false),
33
34 ICNS_128x128_8BIT_MASK("t8mk", 128, 128, 8, true), ICNS_128x128_32BIT_IMAGE("it32", 128, 128, 32, false),
35
36 ICNS_16x16_32BIT_ARGB_IMAGE("icp4", 16, 16, 32, false), ICNS_32x32_32BIT_ARGB_IMAGE("icp5", 32, 32, 32, false),
37 ICNS_64x64_32BIT_ARGB_IMAGE("icp6", 64, 64, 32, false), ICNS_128x128_32BIT_ARGB_IMAGE("ic07", 128, 128, 32, false),
38 ICNS_256x256_32BIT_ARGB_IMAGE("ic08", 256, 256, 32, false), ICNS_512x512_32BIT_ARGB_IMAGE("ic09", 512, 512, 32, false),
39 ICNS_1024x1024_32BIT_ARGB_IMAGE("ic10", 1024, 1024, 32, false), ICNS_32x32_2x_32BIT_ARGB_IMAGE("ic11", 32, 32, 32, false),
40 ICNS_64x64_2x_32BIT_ARGB_IMAGE("ic12", 64, 64, 32, false), ICNS_256x256_2x_32BIT_ARGB_IMAGE("ic13", 256, 256, 32, false),
41 ICNS_512x512_2x_32BIT_ARGB_IMAGE("ic14", 512, 512, 32, false);
42
43 private static final IcnsType[] ALL_IMAGE_TYPES = { ICNS_16x12_1BIT_IMAGE_AND_MASK, ICNS_16x12_4BIT_IMAGE, ICNS_16x12_8BIT_IMAGE,
44 ICNS_16x16_1BIT_IMAGE_AND_MASK, ICNS_16x16_4BIT_IMAGE, ICNS_16x16_8BIT_IMAGE, ICNS_16x16_32BIT_IMAGE, ICNS_32x32_1BIT_IMAGE,
45 ICNS_32x32_1BIT_IMAGE_AND_MASK, ICNS_32x32_4BIT_IMAGE, ICNS_32x32_8BIT_IMAGE, ICNS_32x32_32BIT_IMAGE, ICNS_48x48_1BIT_IMAGE_AND_MASK,
46 ICNS_48x48_4BIT_IMAGE, ICNS_48x48_8BIT_IMAGE, ICNS_48x48_32BIT_IMAGE, ICNS_128x128_32BIT_IMAGE, ICNS_16x16_32BIT_ARGB_IMAGE,
47 ICNS_32x32_32BIT_ARGB_IMAGE, ICNS_64x64_32BIT_ARGB_IMAGE, ICNS_128x128_32BIT_ARGB_IMAGE, ICNS_256x256_32BIT_ARGB_IMAGE,
48 ICNS_512x512_32BIT_ARGB_IMAGE, ICNS_1024x1024_32BIT_ARGB_IMAGE, ICNS_32x32_2x_32BIT_ARGB_IMAGE, ICNS_64x64_2x_32BIT_ARGB_IMAGE,
49 ICNS_256x256_2x_32BIT_ARGB_IMAGE, ICNS_512x512_2x_32BIT_ARGB_IMAGE };
50
51 private static final IcnsType[] ALL_MASK_TYPES = { ICNS_16x12_1BIT_IMAGE_AND_MASK, ICNS_16x16_1BIT_IMAGE_AND_MASK, ICNS_16x16_8BIT_MASK,
52 ICNS_32x32_1BIT_IMAGE_AND_MASK, ICNS_32x32_8BIT_MASK, ICNS_48x48_1BIT_IMAGE_AND_MASK, ICNS_48x48_8BIT_MASK, ICNS_128x128_8BIT_MASK };
53
54 public static String describeType(final int type) {
55 final byte[] bytes = new byte[4];
56 bytes[0] = (byte) (0xff & type >> 24);
57 bytes[1] = (byte) (0xff & type >> 16);
58 bytes[2] = (byte) (0xff & type >> 8);
59 bytes[3] = (byte) (0xff & type);
60 return new String(bytes, StandardCharsets.US_ASCII);
61 }
62
63 public static IcnsType find1BPPMaskType(final IcnsType imageType) {
64 for (final IcnsType allMaskType : ALL_MASK_TYPES) {
65 if (allMaskType.getBitsPerPixel() == 1 && allMaskType.getWidth() == imageType.getWidth() && allMaskType.getHeight() == imageType.getHeight()) {
66 return allMaskType;
67 }
68 }
69 return null;
70 }
71
72 public static IcnsType find8BPPMaskType(final IcnsType imageType) {
73 for (final IcnsType allMaskType : ALL_MASK_TYPES) {
74 if (allMaskType.getBitsPerPixel() == 8 && allMaskType.getWidth() == imageType.getWidth() && allMaskType.getHeight() == imageType.getHeight()) {
75 return allMaskType;
76 }
77 }
78 return null;
79 }
80
81 public static IcnsType findAnyType(final int type) {
82 for (final IcnsType allImageType : ALL_IMAGE_TYPES) {
83 if (allImageType.getType() == type) {
84 return allImageType;
85 }
86 }
87 for (final IcnsType allMaskType : ALL_MASK_TYPES) {
88 if (allMaskType.getType() == type) {
89 return allMaskType;
90 }
91 }
92 return null;
93 }
94
95 public static IcnsType findImageType(final int type) {
96 for (final IcnsType allImageType : ALL_IMAGE_TYPES) {
97 if (allImageType.getType() == type) {
98 return allImageType;
99 }
100 }
101 return null;
102 }
103
104 public static int typeAsInt(final String type) {
105 final byte[] bytes = type.getBytes(StandardCharsets.US_ASCII);
106 if (bytes.length != 4) {
107 throw new IllegalArgumentException("Invalid ICNS type");
108 }
109 return (0xff & bytes[0]) << 24 | (0xff & bytes[1]) << 16 | (0xff & bytes[2]) << 8 | 0xff & bytes[3];
110 }
111
112 private final int type;
113
114 private final int width;
115
116 private final int height;
117
118 private final int bitsPerPixel;
119
120 private final boolean hasMask;
121
122 IcnsType(final String type, final int width, final int height, final int bitsPerPixel, final boolean hasMask) {
123 this.type = typeAsInt(type);
124 this.width = width;
125 this.height = height;
126 this.bitsPerPixel = bitsPerPixel;
127 this.hasMask = hasMask;
128 }
129
130 public int getBitsPerPixel() {
131 return bitsPerPixel;
132 }
133
134 public int getHeight() {
135 return height;
136 }
137
138 public int getType() {
139 return type;
140 }
141
142 public int getWidth() {
143 return width;
144 }
145
146 public boolean hasMask() {
147 return hasMask;
148 }
149
150 @Override
151 public String toString() {
152 return getClass().getName() + "[" + "width=" + width + "," + "height=" + height + "," + "bpp=" + bitsPerPixel + "," + "hasMask=" + hasMask + "]";
153 }
154 }