View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * WebP chunk type.
37   *
38   * @since 1.0-alpha4
39   */
40  public enum WebPChunkType {
41  
42      /**
43       * @see WebPChunkAlph
44       */
45      ALPH(WebPChunkAlph::new),
46  
47      /**
48       * @see WebPChunkVp8
49       */
50      VP8(WebPChunkVp8::new),
51  
52      /**
53       * @see WebPChunkVp8l
54       */
55      VP8L(WebPChunkVp8l::new),
56  
57      /**
58       * @see WebPChunkVp8x
59       */
60      VP8X(WebPChunkVp8x::new),
61  
62      /**
63       * @see WebPChunkAnim
64       */
65      ANIM(WebPChunkAnim::new),
66  
67      /**
68       * @see WebPChunkAnmf
69       */
70      ANMF(WebPChunkAnmf::new),
71  
72      /**
73       * @see WebPChunkIccp
74       */
75      ICCP(WebPChunkIccp::new),
76  
77      /**
78       * @see WebPChunkExif
79       */
80      EXIF(WebPChunkExif::new),
81  
82      /**
83       * @see WebPChunkXml
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 }