1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18 package android.filterfw.format; 19 20 import android.filterfw.core.FrameFormat; 21 import android.filterfw.core.MutableFrameFormat; 22 import android.filterfw.core.NativeBuffer; 23 24 /** 25 * @hide 26 */ 27 public class ObjectFormat { 28 29 public static MutableFrameFormat fromClass(Class clazz, int count, int target) { 30 // Create frame format 31 MutableFrameFormat result = new MutableFrameFormat(FrameFormat.TYPE_OBJECT, target); 32 result.setObjectClass(getBoxedClass(clazz)); 33 if (count != FrameFormat.SIZE_UNSPECIFIED) { 34 result.setDimensions(count); 35 } 36 result.setBytesPerSample(bytesPerSampleForClass(clazz, target)); 37 return result; 38 } 39 40 public static MutableFrameFormat fromClass(Class clazz, int target) { 41 return fromClass(clazz, FrameFormat.SIZE_UNSPECIFIED, target); 42 } 43 44 public static MutableFrameFormat fromObject(Object object, int target) { 45 return object == null 46 ? new MutableFrameFormat(FrameFormat.TYPE_OBJECT, target) 47 : fromClass(object.getClass(), FrameFormat.SIZE_UNSPECIFIED, target); 48 } 49 50 public static MutableFrameFormat fromObject(Object object, int count, int target) { 51 return object == null 52 ? new MutableFrameFormat(FrameFormat.TYPE_OBJECT, target) 53 : fromClass(object.getClass(), count, target); 54 } 55 56 private static int bytesPerSampleForClass(Class clazz, int target) { 57 // Native targets have objects manifested in a byte buffer. Thus it is important to 58 // correctly determine the size of single element here. 59 if (target == FrameFormat.TARGET_NATIVE) { 60 if (!NativeBuffer.class.isAssignableFrom(clazz)) { 61 throw new IllegalArgumentException("Native object-based formats must be of a " + 62 "NativeBuffer subclass! (Received class: " + clazz + ")."); 63 } 64 try { 65 return ((NativeBuffer)clazz.newInstance()).getElementSize(); 66 } catch (Exception e) { 67 throw new RuntimeException("Could not determine the size of an element in a " 68 + "native object-based frame of type " + clazz + "! Perhaps it is missing a " 69 + "default constructor?"); 70 } 71 } else { 72 return FrameFormat.BYTES_PER_SAMPLE_UNSPECIFIED; 73 } 74 } 75 76 private static Class getBoxedClass(Class type) { 77 // Check if type is primitive 78 if (type.isPrimitive()) { 79 // Yes -> box it 80 if (type == boolean.class) { 81 return java.lang.Boolean.class; 82 } else if (type == byte.class) { 83 return java.lang.Byte.class; 84 } else if (type == char.class) { 85 return java.lang.Character.class; 86 } else if (type == short.class) { 87 return java.lang.Short.class; 88 } else if (type == int.class) { 89 return java.lang.Integer.class; 90 } else if (type == long.class) { 91 return java.lang.Long.class; 92 } else if (type == float.class) { 93 return java.lang.Float.class; 94 } else if (type == double.class) { 95 return java.lang.Double.class; 96 } else { 97 throw new IllegalArgumentException( 98 "Unknown primitive type: " + type.getSimpleName() + "!"); 99 } 100 } else { 101 // No -> return it 102 return type; 103 } 104 } 105 } 106