001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.imaging.formats.webp; 018 019import java.io.IOException; 020 021import org.apache.commons.imaging.ImagingException; 022import org.apache.commons.imaging.common.BinaryFunctions; 023import org.apache.commons.imaging.formats.webp.chunks.WebPChunk; 024import org.apache.commons.imaging.formats.webp.chunks.WebPChunkAlph; 025import org.apache.commons.imaging.formats.webp.chunks.WebPChunkAnim; 026import org.apache.commons.imaging.formats.webp.chunks.WebPChunkAnmf; 027import org.apache.commons.imaging.formats.webp.chunks.WebPChunkExif; 028import org.apache.commons.imaging.formats.webp.chunks.WebPChunkIccp; 029import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVp8; 030import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVp8l; 031import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVp8x; 032import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXml; 033import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXyzw; 034 035/** 036 * WebP chunk type. 037 * 038 * @since 1.0-alpha4 039 */ 040public enum WebPChunkType { 041 042 /** 043 * @see WebPChunkAlph 044 */ 045 ALPH(WebPChunkAlph::new), 046 047 /** 048 * @see WebPChunkVp8 049 */ 050 VP8(WebPChunkVp8::new), 051 052 /** 053 * @see WebPChunkVp8l 054 */ 055 VP8L(WebPChunkVp8l::new), 056 057 /** 058 * @see WebPChunkVp8x 059 */ 060 VP8X(WebPChunkVp8x::new), 061 062 /** 063 * @see WebPChunkAnim 064 */ 065 ANIM(WebPChunkAnim::new), 066 067 /** 068 * @see WebPChunkAnmf 069 */ 070 ANMF(WebPChunkAnmf::new), 071 072 /** 073 * @see WebPChunkIccp 074 */ 075 ICCP(WebPChunkIccp::new), 076 077 /** 078 * @see WebPChunkExif 079 */ 080 EXIF(WebPChunkExif::new), 081 082 /** 083 * @see WebPChunkXml 084 */ 085 XMP(WebPChunkXml::new); 086 087 private interface ChunkConstructor { 088 WebPChunk make(int type, int size, byte[] bytes) throws IOException, ImagingException; 089 } 090 091 private static final WebPChunkType[] types = WebPChunkType.values(); 092 093 static WebPChunkType findType(final int chunkType) { 094 for (final WebPChunkType type : types) { 095 if (type.value == chunkType) { 096 return type; 097 } 098 } 099 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}