1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.webp;
18
19 import java.io.IOException;
20
21 import org.apache.commons.imaging.ImagingException;
22 import org.apache.commons.imaging.common.BinaryFunctions;
23 import org.apache.commons.imaging.formats.webp.chunks.WebPChunk;
24 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkAlph;
25 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkAnim;
26 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkAnmf;
27 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkExif;
28 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkIccp;
29 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVp8;
30 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVp8l;
31 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVp8x;
32 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXml;
33 import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXyzw;
34
35
36
37
38
39
40 public enum WebPChunkType {
41
42
43
44
45 ALPH(WebPChunkAlph::new),
46
47
48
49
50 VP8(WebPChunkVp8::new),
51
52
53
54
55 VP8L(WebPChunkVp8l::new),
56
57
58
59
60 VP8X(WebPChunkVp8x::new),
61
62
63
64
65 ANIM(WebPChunkAnim::new),
66
67
68
69
70 ANMF(WebPChunkAnmf::new),
71
72
73
74
75 ICCP(WebPChunkIccp::new),
76
77
78
79
80 EXIF(WebPChunkExif::new),
81
82
83
84
85 XMP(WebPChunkXml::new);
86
87 private interface ChunkConstructor {
88 WebPChunk make(int type, int size, byte[] bytes) throws IOException, ImagingException;
89 }
90
91 private static final WebPChunkType[] types = WebPChunkType.values();
92
93 static WebPChunkType findType(final int chunkType) {
94 for (final WebPChunkType type : types) {
95 if (type.value == chunkType) {
96 return type;
97 }
98 }
99 return null;
100 }
101
102 static WebPChunk makeChunk(final int chunkType, final int size, final byte[] bytes) throws IOException, ImagingException {
103 final WebPChunkType type = findType(chunkType);
104 return type != null ? type.constructor.make(chunkType, size, bytes) : new WebPChunkXyzw(chunkType, size, bytes);
105 }
106
107 private final ChunkConstructor constructor;
108 final int value;
109
110 WebPChunkType(final ChunkConstructor constructor) {
111 this.constructor = constructor;
112 this.value = BinaryFunctions.charsToQuad(name().length() == 4 ? name().charAt(3) : ' ', name().charAt(2), name().charAt(1), name().charAt(0));
113 }
114 }